1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
13 #include <bpf/libbpf.h>
17 #define BATCH_LINE_LEN_MAX 65536
18 #define BATCH_ARG_NB_MAX 4096
22 static char **last_argv;
23 static int (*last_do_help)(int argc, char **argv);
24 json_writer_t *json_wtr;
31 struct pinned_obj_table prog_table;
32 struct pinned_obj_table map_table;
33 struct pinned_obj_table link_table;
34 struct obj_refs_table refs_table;
36 static void __noreturn clean_and_exit(int i)
39 jsonw_destroy(&json_wtr);
46 last_do_help(last_argc - 1, last_argv + 1);
51 static int do_help(int argc, char **argv)
59 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
60 " %s batch file FILE\n"
63 " OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
64 " " HELP_SPEC_OPTIONS "\n"
66 bin_name, bin_name, bin_name);
71 static int do_version(int argc, char **argv)
73 #ifdef HAVE_LIBBFD_SUPPORT
74 const bool has_libbfd = true;
76 const bool has_libbfd = false;
78 #ifdef BPFTOOL_WITHOUT_SKELETONS
79 const bool has_skeletons = false;
81 const bool has_skeletons = true;
85 jsonw_start_object(json_wtr); /* root object */
87 jsonw_name(json_wtr, "version");
88 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
90 jsonw_name(json_wtr, "features");
91 jsonw_start_object(json_wtr); /* features */
92 jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
93 jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
94 jsonw_end_object(json_wtr); /* features */
96 jsonw_end_object(json_wtr); /* root object */
98 unsigned int nb_features = 0;
100 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
107 printf("%s skeletons", nb_features++ ? "," : "");
113 int cmd_select(const struct cmd *cmds, int argc, char **argv,
114 int (*help)(int argc, char **argv))
122 if (argc < 1 && cmds[0].func)
123 return cmds[0].func(argc, argv);
125 for (i = 0; cmds[i].cmd; i++) {
126 if (is_prefix(*argv, cmds[i].cmd)) {
128 p_err("command '%s' is not supported in bootstrap mode",
132 return cmds[i].func(argc - 1, argv + 1);
136 help(argc - 1, argv + 1);
141 bool is_prefix(const char *pfx, const char *str)
145 if (strlen(str) < strlen(pfx))
148 return !memcmp(str, pfx, strlen(pfx));
151 /* Last argument MUST be NULL pointer */
152 int detect_common_prefix(const char *arg, ...)
154 unsigned int count = 0;
159 snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
161 while ((ref = va_arg(ap, const char *))) {
162 if (!is_prefix(arg, ref))
166 strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
167 strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
170 strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
180 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
182 unsigned char *data = arg;
185 for (i = 0; i < n; i++) {
186 const char *pfx = "";
197 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
201 /* Split command line into argument vector. */
202 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
204 static const char ws[] = " \t\r\n";
209 /* Skip leading whitespace. */
210 cp += strspn(cp, ws);
215 if (n_argc >= (maxargs - 1)) {
216 p_err("too many arguments to command %d", cmd_nb);
220 /* Word begins with quote. */
221 if (*cp == '\'' || *cp == '"') {
224 n_argv[n_argc++] = cp;
225 /* Find ending quote. */
226 cp = strchr(cp, quote);
228 p_err("unterminated quoted string in command %d",
233 n_argv[n_argc++] = cp;
235 /* Find end of word. */
236 cp += strcspn(cp, ws);
241 /* Separate words. */
244 n_argv[n_argc] = NULL;
249 static int do_batch(int argc, char **argv);
251 static const struct cmd cmds[] = {
253 { "batch", do_batch },
257 { "cgroup", do_cgroup },
260 { "feature", do_feature },
263 { "struct_ops", do_struct_ops },
265 { "version", do_version },
269 static int do_batch(int argc, char **argv)
271 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
272 char *n_argv[BATCH_ARG_NB_MAX];
273 unsigned int lines = 0;
281 p_err("too few parameters for batch");
283 } else if (!is_prefix(*argv, "file")) {
284 p_err("expected 'file', got: %s", *argv);
286 } else if (argc > 2) {
287 p_err("too many parameters for batch");
292 if (!strcmp(*argv, "-"))
295 fp = fopen(*argv, "r");
297 p_err("Can't open file (%s): %s", *argv, strerror(errno));
302 jsonw_start_array(json_wtr);
303 while (fgets(buf, sizeof(buf), fp)) {
304 cp = strchr(buf, '#');
308 if (strlen(buf) == sizeof(buf) - 1) {
313 /* Append continuation lines if any (coming after a line ending
314 * with '\' in the batch file).
316 while ((cp = strstr(buf, "\\\n")) != NULL) {
317 if (!fgets(contline, sizeof(contline), fp) ||
318 strlen(contline) == 0) {
319 p_err("missing continuation line on command %d",
325 cp = strchr(contline, '#');
329 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
330 p_err("command %d is too long", lines);
334 buf[strlen(buf) - 2] = '\0';
335 strcat(buf, contline);
338 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
347 jsonw_start_object(json_wtr);
348 jsonw_name(json_wtr, "command");
349 jsonw_start_array(json_wtr);
350 for (i = 0; i < n_argc; i++)
351 jsonw_string(json_wtr, n_argv[i]);
352 jsonw_end_array(json_wtr);
353 jsonw_name(json_wtr, "output");
356 err = cmd_select(cmds, n_argc, n_argv, do_help);
359 jsonw_end_object(json_wtr);
367 if (errno && errno != ENOENT) {
368 p_err("reading batch file failed: %s", strerror(errno));
372 printf("processed %d commands\n", lines);
379 jsonw_end_array(json_wtr);
384 int main(int argc, char **argv)
386 static const struct option options[] = {
387 { "json", no_argument, NULL, 'j' },
388 { "help", no_argument, NULL, 'h' },
389 { "pretty", no_argument, NULL, 'p' },
390 { "version", no_argument, NULL, 'V' },
391 { "bpffs", no_argument, NULL, 'f' },
392 { "mapcompat", no_argument, NULL, 'm' },
393 { "nomount", no_argument, NULL, 'n' },
394 { "debug", no_argument, NULL, 'd' },
402 /* Libcap < 2.63 hooks before main() to compute the number of
403 * capabilities of the running kernel, and doing so it calls prctl()
404 * which may fail and set errno to non-zero.
405 * Let's reset errno to make sure this does not interfere with the
411 last_do_help = do_help;
412 pretty_output = false;
418 hash_init(prog_table.table);
419 hash_init(map_table.table);
420 hash_init(link_table.table);
423 while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
424 options, NULL)) >= 0) {
427 return do_version(argc, argv);
429 return do_help(argc, argv);
431 pretty_output = true;
435 json_wtr = jsonw_new(stdout);
437 p_err("failed to create JSON writer");
442 jsonw_pretty(json_wtr, pretty_output);
454 libbpf_set_print(print_all_levels);
455 verifier_logs = true;
458 p_err("unrecognized option '%s'", argv[optind - 1]);
471 ret = cmd_select(cmds, argc, argv, do_help);
474 jsonw_destroy(&json_wtr);
477 delete_pinned_obj_table(&prog_table);
478 delete_pinned_obj_table(&map_table);
479 delete_pinned_obj_table(&link_table);