1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
14 #include <bpf/hashmap.h>
15 #include <bpf/libbpf.h>
19 #define BATCH_LINE_LEN_MAX 65536
20 #define BATCH_ARG_NB_MAX 4096
24 static char **last_argv
;
25 static int (*last_do_help
)(int argc
, char **argv
);
26 json_writer_t
*json_wtr
;
35 struct hashmap
*refs_table
;
37 static void __noreturn
clean_and_exit(int i
)
40 jsonw_destroy(&json_wtr
);
47 last_do_help(last_argc
- 1, last_argv
+ 1);
52 static int do_help(int argc
, char **argv
)
60 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
61 " %s batch file FILE\n"
64 " OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
65 " " HELP_SPEC_OPTIONS
" |\n"
68 bin_name
, bin_name
, bin_name
);
73 static int do_batch(int argc
, char **argv
);
74 static int do_version(int argc
, char **argv
);
76 static const struct cmd commands
[] = {
78 { "batch", do_batch
},
82 { "cgroup", do_cgroup
},
85 { "feature", do_feature
},
88 { "struct_ops", do_struct_ops
},
90 { "version", do_version
},
94 #ifndef BPFTOOL_VERSION
95 /* bpftool's major and minor version numbers are aligned on libbpf's. There is
96 * an offset of 6 for the version number, because bpftool's version was higher
97 * than libbpf's when we adopted this scheme. The patch number remains at 0
98 * for now. Set BPFTOOL_VERSION to override.
100 #define BPFTOOL_MAJOR_VERSION (LIBBPF_MAJOR_VERSION + 6)
101 #define BPFTOOL_MINOR_VERSION LIBBPF_MINOR_VERSION
102 #define BPFTOOL_PATCH_VERSION 0
106 print_feature(const char *feature
, bool state
, unsigned int *nb_features
)
109 printf("%s %s", *nb_features
? "," : "", feature
);
110 *nb_features
= *nb_features
+ 1;
114 static int do_version(int argc
, char **argv
)
116 #ifdef HAVE_LIBBFD_SUPPORT
117 const bool has_libbfd
= true;
119 const bool has_libbfd
= false;
121 #ifdef HAVE_LLVM_SUPPORT
122 const bool has_llvm
= true;
124 const bool has_llvm
= false;
126 #ifdef BPFTOOL_WITHOUT_SKELETONS
127 const bool has_skeletons
= false;
129 const bool has_skeletons
= true;
131 bool bootstrap
= false;
134 for (i
= 0; commands
[i
].cmd
; i
++) {
135 if (!strcmp(commands
[i
].cmd
, "prog")) {
136 /* Assume we run a bootstrap version if "bpftool prog"
139 bootstrap
= !commands
[i
].func
;
145 jsonw_start_object(json_wtr
); /* root object */
147 jsonw_name(json_wtr
, "version");
148 #ifdef BPFTOOL_VERSION
149 jsonw_printf(json_wtr
, "\"%s\"", BPFTOOL_VERSION
);
151 jsonw_printf(json_wtr
, "\"%d.%d.%d\"", BPFTOOL_MAJOR_VERSION
,
152 BPFTOOL_MINOR_VERSION
, BPFTOOL_PATCH_VERSION
);
154 jsonw_name(json_wtr
, "libbpf_version");
155 jsonw_printf(json_wtr
, "\"%d.%d\"",
156 libbpf_major_version(), libbpf_minor_version());
158 jsonw_name(json_wtr
, "features");
159 jsonw_start_object(json_wtr
); /* features */
160 jsonw_bool_field(json_wtr
, "libbfd", has_libbfd
);
161 jsonw_bool_field(json_wtr
, "llvm", has_llvm
);
162 jsonw_bool_field(json_wtr
, "skeletons", has_skeletons
);
163 jsonw_bool_field(json_wtr
, "bootstrap", bootstrap
);
164 jsonw_end_object(json_wtr
); /* features */
166 jsonw_end_object(json_wtr
); /* root object */
168 unsigned int nb_features
= 0;
170 #ifdef BPFTOOL_VERSION
171 printf("%s v%s\n", bin_name
, BPFTOOL_VERSION
);
173 printf("%s v%d.%d.%d\n", bin_name
, BPFTOOL_MAJOR_VERSION
,
174 BPFTOOL_MINOR_VERSION
, BPFTOOL_PATCH_VERSION
);
176 printf("using libbpf %s\n", libbpf_version_string());
178 print_feature("libbfd", has_libbfd
, &nb_features
);
179 print_feature("llvm", has_llvm
, &nb_features
);
180 print_feature("skeletons", has_skeletons
, &nb_features
);
181 print_feature("bootstrap", bootstrap
, &nb_features
);
187 int cmd_select(const struct cmd
*cmds
, int argc
, char **argv
,
188 int (*help
)(int argc
, char **argv
))
196 if (argc
< 1 && cmds
[0].func
)
197 return cmds
[0].func(argc
, argv
);
199 for (i
= 0; cmds
[i
].cmd
; i
++) {
200 if (is_prefix(*argv
, cmds
[i
].cmd
)) {
202 p_err("command '%s' is not supported in bootstrap mode",
206 return cmds
[i
].func(argc
- 1, argv
+ 1);
210 help(argc
- 1, argv
+ 1);
215 bool is_prefix(const char *pfx
, const char *str
)
219 if (strlen(str
) < strlen(pfx
))
222 return !memcmp(str
, pfx
, strlen(pfx
));
225 /* Last argument MUST be NULL pointer */
226 int detect_common_prefix(const char *arg
, ...)
228 unsigned int count
= 0;
233 snprintf(msg
, sizeof(msg
), "ambiguous prefix: '%s' could be '", arg
);
235 while ((ref
= va_arg(ap
, const char *))) {
236 if (!is_prefix(arg
, ref
))
240 strncat(msg
, "' or '", sizeof(msg
) - strlen(msg
) - 1);
241 strncat(msg
, ref
, sizeof(msg
) - strlen(msg
) - 1);
244 strncat(msg
, "'", sizeof(msg
) - strlen(msg
) - 1);
254 void fprint_hex(FILE *f
, void *arg
, unsigned int n
, const char *sep
)
256 unsigned char *data
= arg
;
259 for (i
= 0; i
< n
; i
++) {
260 const char *pfx
= "";
271 fprintf(f
, "%s%02hhx", i
? pfx
: "", data
[i
]);
275 /* Split command line into argument vector. */
276 static int make_args(char *line
, char *n_argv
[], int maxargs
, int cmd_nb
)
278 static const char ws
[] = " \t\r\n";
283 /* Skip leading whitespace. */
284 cp
+= strspn(cp
, ws
);
289 if (n_argc
>= (maxargs
- 1)) {
290 p_err("too many arguments to command %d", cmd_nb
);
294 /* Word begins with quote. */
295 if (*cp
== '\'' || *cp
== '"') {
298 n_argv
[n_argc
++] = cp
;
299 /* Find ending quote. */
300 cp
= strchr(cp
, quote
);
302 p_err("unterminated quoted string in command %d",
307 n_argv
[n_argc
++] = cp
;
309 /* Find end of word. */
310 cp
+= strcspn(cp
, ws
);
315 /* Separate words. */
318 n_argv
[n_argc
] = NULL
;
323 static int do_batch(int argc
, char **argv
)
325 char buf
[BATCH_LINE_LEN_MAX
], contline
[BATCH_LINE_LEN_MAX
];
326 char *n_argv
[BATCH_ARG_NB_MAX
];
327 unsigned int lines
= 0;
335 p_err("too few parameters for batch");
337 } else if (argc
> 2) {
338 p_err("too many parameters for batch");
340 } else if (!is_prefix(*argv
, "file")) {
341 p_err("expected 'file', got: %s", *argv
);
346 if (!strcmp(*argv
, "-"))
349 fp
= fopen(*argv
, "r");
351 p_err("Can't open file (%s): %s", *argv
, strerror(errno
));
356 jsonw_start_array(json_wtr
);
357 while (fgets(buf
, sizeof(buf
), fp
)) {
358 cp
= strchr(buf
, '#');
362 if (strlen(buf
) == sizeof(buf
) - 1) {
367 /* Append continuation lines if any (coming after a line ending
368 * with '\' in the batch file).
370 while ((cp
= strstr(buf
, "\\\n")) != NULL
) {
371 if (!fgets(contline
, sizeof(contline
), fp
) ||
372 strlen(contline
) == 0) {
373 p_err("missing continuation line on command %d",
379 cp
= strchr(contline
, '#');
383 if (strlen(buf
) + strlen(contline
) + 1 > sizeof(buf
)) {
384 p_err("command %d is too long", lines
);
388 buf
[strlen(buf
) - 2] = '\0';
389 strcat(buf
, contline
);
392 n_argc
= make_args(buf
, n_argv
, BATCH_ARG_NB_MAX
, lines
);
401 jsonw_start_object(json_wtr
);
402 jsonw_name(json_wtr
, "command");
403 jsonw_start_array(json_wtr
);
404 for (i
= 0; i
< n_argc
; i
++)
405 jsonw_string(json_wtr
, n_argv
[i
]);
406 jsonw_end_array(json_wtr
);
407 jsonw_name(json_wtr
, "output");
410 err
= cmd_select(commands
, n_argc
, n_argv
, do_help
);
413 jsonw_end_object(json_wtr
);
421 if (errno
&& errno
!= ENOENT
) {
422 p_err("reading batch file failed: %s", strerror(errno
));
426 printf("processed %d commands\n", lines
);
433 jsonw_end_array(json_wtr
);
438 int main(int argc
, char **argv
)
440 static const struct option options
[] = {
441 { "json", no_argument
, NULL
, 'j' },
442 { "help", no_argument
, NULL
, 'h' },
443 { "pretty", no_argument
, NULL
, 'p' },
444 { "version", no_argument
, NULL
, 'V' },
445 { "bpffs", no_argument
, NULL
, 'f' },
446 { "mapcompat", no_argument
, NULL
, 'm' },
447 { "nomount", no_argument
, NULL
, 'n' },
448 { "debug", no_argument
, NULL
, 'd' },
449 { "use-loader", no_argument
, NULL
, 'L' },
450 { "base-btf", required_argument
, NULL
, 'B' },
453 bool version_requested
= false;
459 /* Libcap < 2.63 hooks before main() to compute the number of
460 * capabilities of the running kernel, and doing so it calls prctl()
461 * which may fail and set errno to non-zero.
462 * Let's reset errno to make sure this does not interfere with the
468 last_do_help
= do_help
;
469 pretty_output
= false;
473 bin_name
= "bpftool";
476 while ((opt
= getopt_long(argc
, argv
, "VhpjfLmndB:l",
477 options
, NULL
)) >= 0) {
480 version_requested
= true;
483 return do_help(argc
, argv
);
485 pretty_output
= true;
489 json_wtr
= jsonw_new(stdout
);
491 p_err("failed to create JSON writer");
496 jsonw_pretty(json_wtr
, pretty_output
);
508 libbpf_set_print(print_all_levels
);
509 verifier_logs
= true;
512 base_btf
= btf__parse(optarg
, NULL
);
514 p_err("failed to parse base BTF at '%s': %d\n",
523 p_err("unrecognized option '%s'", argv
[optind
- 1]);
536 if (version_requested
)
537 return do_version(argc
, argv
);
539 ret
= cmd_select(commands
, argc
, argv
, do_help
);
542 jsonw_destroy(&json_wtr
);