Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: bpftool: introduce --json and --pretty options

These two options can be used to ask for a JSON output (--j or -json),
and to make this JSON human-readable (-p or --pretty).

A json_writer object is created when JSON is required, and will be used
in follow-up commits to produce JSON output.

Note that --pretty implies --json.

Update for the manual pages and interactive help messages comes in a
later patch of the series.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Quentin Monnet and committed by
David S. Miller
d35efba9 a2bc2e5c

+35 -3
+30 -3
tools/bpf/bpftool/main.c
··· 51 51 static int last_argc; 52 52 static char **last_argv; 53 53 static int (*last_do_help)(int argc, char **argv); 54 + json_writer_t *json_wtr; 55 + bool pretty_output; 56 + bool json_output; 54 57 55 58 void usage(void) 56 59 { ··· 220 217 int main(int argc, char **argv) 221 218 { 222 219 static const struct option options[] = { 220 + { "json", no_argument, NULL, 'j' }, 223 221 { "help", no_argument, NULL, 'h' }, 222 + { "pretty", no_argument, NULL, 'p' }, 224 223 { "version", no_argument, NULL, 'V' }, 225 224 { 0 } 226 225 }; 227 - int opt; 226 + int opt, ret; 228 227 229 228 last_do_help = do_help; 229 + pretty_output = false; 230 + json_output = false; 230 231 bin_name = argv[0]; 231 232 232 - while ((opt = getopt_long(argc, argv, "Vh", 233 + while ((opt = getopt_long(argc, argv, "Vhpj", 233 234 options, NULL)) >= 0) { 234 235 switch (opt) { 235 236 case 'V': 236 237 return do_version(argc, argv); 237 238 case 'h': 238 239 return do_help(argc, argv); 240 + case 'p': 241 + pretty_output = true; 242 + /* fall through */ 243 + case 'j': 244 + json_output = true; 245 + break; 239 246 default: 240 247 usage(); 241 248 } ··· 256 243 if (argc < 0) 257 244 usage(); 258 245 246 + if (json_output) { 247 + json_wtr = jsonw_new(stdout); 248 + if (!json_wtr) { 249 + err("failed to create JSON writer\n"); 250 + return -1; 251 + } 252 + jsonw_pretty(json_wtr, pretty_output); 253 + } 254 + 259 255 bfd_init(); 260 256 261 - return cmd_select(cmds, argc, argv, do_help); 257 + ret = cmd_select(cmds, argc, argv, do_help); 258 + 259 + if (json_output) 260 + jsonw_destroy(&json_wtr); 261 + 262 + return ret; 262 263 }
+5
tools/bpf/bpftool/main.h
··· 43 43 #include <linux/bpf.h> 44 44 #include <linux/kernel.h> 45 45 46 + #include "json_writer.h" 47 + 46 48 #define err(msg...) fprintf(stderr, "Error: " msg) 47 49 #define warn(msg...) fprintf(stderr, "Warning: " msg) 48 50 #define info(msg...) fprintf(stderr, msg) ··· 67 65 }; 68 66 69 67 extern const char *bin_name; 68 + 69 + extern json_writer_t *json_wtr; 70 + extern bool json_output; 70 71 71 72 bool is_prefix(const char *pfx, const char *str); 72 73 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);