Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7162964ybi; Wed, 5 Jun 2019 12:19:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqx8r6xONNoLXUd3HjkLIyZdnSxx7m23DxiDmUwZncmzCfeKNTjuVCSWqGO9KBNWQ1xI4Z/K X-Received: by 2002:a62:ed09:: with SMTP id u9mr48464050pfh.23.1559762381462; Wed, 05 Jun 2019 12:19:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559762381; cv=none; d=google.com; s=arc-20160816; b=frDaRtWAsAikRcPE2Hj8v35/U0r+rDmo6MJHhH9Rup2ZR8N8kSSuhXdSb9SB+Q+QuI BLF/PnWmAagxkzIMRVdYk3YqrKZXKI7kjMs0/Oy3GS0t/NErybhIhTRlGfoZW2nyj57V 5I4ZGUdMdNqNnjkLgYqSR204gum5BgpUG3RovzB9npwg16wUsFalCPggaqTszoJ/LH0a 0L6IstIx3Z5J/Bld2QUJ4Dh5t6p2ZklGH+lwlVNnG4nGyEjQbywo3UOUh/ng17W/Jg8E xdiYMz/e5vrhRDte3Upr956/UKUqLJClHielEVSniAHzwwOkjUzgKNZuYiI5KDze6mVr zvzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=ZHKa8UUAI/1Pj5REqATA4zW8nm5JTzi55W6sOf5RHPs=; b=SGZY+q71faBn+lUobz93+9nC4O7Wwl6vHYGGf7dXMfHS3a7dhzeS+YfE6KuW2GNv8y M+NKkiqM5lECFGUtgsbIZ6TDpHl8y8Bz6H10IVQBTKXNaYegD0vXr+po+pYN9zjsvCrx 6YbtTovx3z7Ir5B9NLyjlvrQfB2DD12wlVgbzdhV4gY/z/fuCwrow1ptvL0owJ9VTrr4 RiQdjFStXVZ193qgTgmR8XiVTFDfhI8NZj14FJv6dUpQwvgLxM/ZAxZ/zgVJTyykW62I OHB2A0Q1EuhZ/80f+qUGJf8iTIjYNjEx6r4yMPYodh3y/ptBp12P4NOpM5KbXPGJgIPR QJig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kinvolk.io header.s=google header.b="Sys2i/MD"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m63si26026436pje.28.2019.06.05.12.19.24; Wed, 05 Jun 2019 12:19:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kinvolk.io header.s=google header.b="Sys2i/MD"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726735AbfFETSG (ORCPT + 99 others); Wed, 5 Jun 2019 15:18:06 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43056 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726530AbfFETSG (ORCPT ); Wed, 5 Jun 2019 15:18:06 -0400 Received: by mail-wr1-f67.google.com with SMTP id r18so11615192wrm.10 for ; Wed, 05 Jun 2019 12:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kinvolk.io; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZHKa8UUAI/1Pj5REqATA4zW8nm5JTzi55W6sOf5RHPs=; b=Sys2i/MD9135pbUQm2IA2lulB160+0KHKQmb/RLaKMSPNR5ERUFqkNZqBd6ZHpJhmd e4Cg+lnMWo4nsbbYIFn2iR36b7Enhy1CkpsuystlmGOc59GIDGdHSEUq6PDd+a7Yp3k3 IHAz9M3GbBgTv/AYPQ3sB4sFR0R4UHc5u3F/4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZHKa8UUAI/1Pj5REqATA4zW8nm5JTzi55W6sOf5RHPs=; b=VmUK2D6ttDcEQiNAopLWfE1ZHosV1npgRb3BVKbijadsB8+NV/FH8KTQy3R/E68UvD oTINMO9wtf7bLL/OtCOCal+J/+nFuQe8iyazLMt3vfIm7eViFJz6eJ6It3cpBJ6BYtzj YmG/elMDSDOPGO3bqpC+I7ikvHKvmVBuWV7PTHT0I+37YJBjZf2Mc/5SLYb5l4MBpB/e N1T19ZEQAgQF1Qn5YWHAtLdSHszD7z1vhi3cJZLkDNF5sIG4B03tRQD3GbaWNRLR2hVT MQhGvk8RZhvgNtinLg9f1k++oj/AOVuDOI489ILoqBLNJwo3eNY3PuXdnay5HslfsQiU kw4A== X-Gm-Message-State: APjAAAXWr3NIqvi6n7ffoissOt2M3frbTtNttbZZJuiXmcdLBNsGvmRk 45rQb9nL5mgZReXPaHSxPXP0Ng== X-Received: by 2002:a5d:680a:: with SMTP id w10mr5115453wru.42.1559762284615; Wed, 05 Jun 2019 12:18:04 -0700 (PDT) Received: from localhost.localdomain (ip5f5aebaf.dynamic.kabel-deutschland.de. [95.90.235.175]) by smtp.gmail.com with ESMTPSA id h23sm13781580wmb.25.2019.06.05.12.18.02 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 05 Jun 2019 12:18:03 -0700 (PDT) From: Krzesimir Nowak To: bpf@vger.kernel.org Cc: Alban Crequy , =?UTF-8?q?Iago=20L=C3=B3pez=20Galeiras?= , Krzesimir Nowak , Quentin Monnet , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Jakub Kicinski , Stanislav Fomichev , Prashant Bhole , Okash Khawaja , David Calavera , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [BPF v1] tools: bpftool: Fix JSON output when lookup fails Date: Wed, 5 Jun 2019 21:17:06 +0200 Message-Id: <20190605191707.24429-1-krzesimir@kinvolk.io> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions") one case of error reporting was special cased, so it could report a lookup error for a specific key when dumping the map element. What the code forgot to do is to wrap the key and value keys into a JSON object, so an example output of pretty JSON dump of a sockhash map (which does not support looking up its values) is: [ "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00" ], "value": { "error": "Operation not supported" }, "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01" ], "value": { "error": "Operation not supported" } ] Note the key-value pairs inside the toplevel array. They should be wrapped inside a JSON object, otherwise it is an invalid JSON. This commit fixes this, so the output now is: [{ "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00" ], "value": { "error": "Operation not supported" } },{ "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01" ], "value": { "error": "Operation not supported" } } ] Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions") Cc: Quentin Monnet Signed-off-by: Krzesimir Nowak --- tools/bpf/bpftool/map.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 3ec82904ccec..5da5a7311f13 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -716,12 +716,14 @@ static int dump_map_elem(int fd, void *key, void *value, return 0; if (json_output) { + jsonw_start_object(json_wtr); jsonw_name(json_wtr, "key"); print_hex_data_json(key, map_info->key_size); jsonw_name(json_wtr, "value"); jsonw_start_object(json_wtr); jsonw_string_field(json_wtr, "error", strerror(lookup_errno)); jsonw_end_object(json_wtr); + jsonw_end_object(json_wtr); } else { const char *msg = NULL; -- 2.20.1