^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define BATCH_LINE_LEN_MAX 65536
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define BATCH_ARG_NB_MAX 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const char *bin_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int last_argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static char **last_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int (*last_do_help)(int argc, char **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) json_writer_t *json_wtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool pretty_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) bool json_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) bool show_pinned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) bool block_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) bool verifier_logs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bool relaxed_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct pinned_obj_table prog_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct pinned_obj_table map_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct pinned_obj_table link_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct obj_refs_table refs_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void __noreturn clean_and_exit(int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) jsonw_destroy(&json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) exit(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) last_do_help(last_argc - 1, last_argv + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) clean_and_exit(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int do_help(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) " %s batch file FILE\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) " %s version\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) " OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) " " HELP_SPEC_OPTIONS "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) bin_name, bin_name, bin_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int do_version(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #ifdef HAVE_LIBBFD_SUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const bool has_libbfd = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const bool has_libbfd = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #ifdef BPFTOOL_WITHOUT_SKELETONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) const bool has_skeletons = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) const bool has_skeletons = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) jsonw_start_object(json_wtr); /* root object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) jsonw_name(json_wtr, "version");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) jsonw_name(json_wtr, "features");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) jsonw_start_object(json_wtr); /* features */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) jsonw_end_object(json_wtr); /* features */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) jsonw_end_object(json_wtr); /* root object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned int nb_features = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) printf("features:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (has_libbfd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) printf(" libbfd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) nb_features++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (has_skeletons)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) printf("%s skeletons", nb_features++ ? "," : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int cmd_select(const struct cmd *cmds, int argc, char **argv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int (*help)(int argc, char **argv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) last_argc = argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) last_argv = argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) last_do_help = help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (argc < 1 && cmds[0].func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return cmds[0].func(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (i = 0; cmds[i].cmd; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (is_prefix(*argv, cmds[i].cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!cmds[i].func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) p_err("command '%s' is not supported in bootstrap mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) cmds[i].cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return cmds[i].func(argc - 1, argv + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) help(argc - 1, argv + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) bool is_prefix(const char *pfx, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!pfx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (strlen(str) < strlen(pfx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return !memcmp(str, pfx, strlen(pfx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Last argument MUST be NULL pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int detect_common_prefix(const char *arg, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) const char *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) char msg[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) va_start(ap, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) while ((ref = va_arg(ap, const char *))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!is_prefix(arg, ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (count >= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) p_err("%s", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) unsigned char *data = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) for (i = 0; i < n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) const char *pfx = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (!i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) else if (!(i % 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fprintf(f, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) else if (!(i % 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pfx = sep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* Split command line into argument vector. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static const char ws[] = " \t\r\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) char *cp = line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int n_argc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) while (*cp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* Skip leading whitespace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) cp += strspn(cp, ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (*cp == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (n_argc >= (maxargs - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) p_err("too many arguments to command %d", cmd_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* Word begins with quote. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (*cp == '\'' || *cp == '"') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) char quote = *cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) n_argv[n_argc++] = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* Find ending quote. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) cp = strchr(cp, quote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!cp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) p_err("unterminated quoted string in command %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) cmd_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) n_argv[n_argc++] = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Find end of word. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) cp += strcspn(cp, ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (*cp == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* Separate words. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *cp++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) n_argv[n_argc] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return n_argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int do_batch(int argc, char **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static const struct cmd cmds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) { "help", do_help },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) { "batch", do_batch },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) { "prog", do_prog },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) { "map", do_map },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) { "link", do_link },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) { "cgroup", do_cgroup },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) { "perf", do_perf },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) { "net", do_net },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) { "feature", do_feature },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) { "btf", do_btf },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) { "gen", do_gen },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) { "struct_ops", do_struct_ops },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) { "iter", do_iter },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) { "version", do_version },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) { 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int do_batch(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) char *n_argv[BATCH_ARG_NB_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) unsigned int lines = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int n_argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (argc < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) p_err("too few parameters for batch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) } else if (!is_prefix(*argv, "file")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) p_err("expected 'file', got: %s", *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) } else if (argc > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) p_err("too many parameters for batch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!strcmp(*argv, "-"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) fp = stdin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) fp = fopen(*argv, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) p_err("Can't open file (%s): %s", *argv, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) while (fgets(buf, sizeof(buf), fp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) cp = strchr(buf, '#');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *cp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (strlen(buf) == sizeof(buf) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) errno = E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Append continuation lines if any (coming after a line ending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * with '\' in the batch file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) while ((cp = strstr(buf, "\\\n")) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!fgets(contline, sizeof(contline), fp) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) strlen(contline) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) p_err("missing continuation line on command %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) cp = strchr(contline, '#');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) *cp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) p_err("command %d is too long", lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) buf[strlen(buf) - 2] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) strcat(buf, contline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!n_argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (n_argc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err = n_argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) jsonw_start_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) jsonw_name(json_wtr, "command");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) for (i = 0; i < n_argc; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) jsonw_string(json_wtr, n_argv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) jsonw_name(json_wtr, "output");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) err = cmd_select(cmds, n_argc, n_argv, do_help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) jsonw_end_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) lines++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (errno && errno != ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) p_err("reading batch file failed: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) printf("processed %d commands\n", lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) err_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (fp != stdin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static const struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) { "json", no_argument, NULL, 'j' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) { "help", no_argument, NULL, 'h' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) { "pretty", no_argument, NULL, 'p' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) { "version", no_argument, NULL, 'V' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) { "bpffs", no_argument, NULL, 'f' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) { "mapcompat", no_argument, NULL, 'm' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) { "nomount", no_argument, NULL, 'n' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) { "debug", no_argument, NULL, 'd' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) { 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int opt, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) setlinebuf(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) last_do_help = do_help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) pretty_output = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) json_output = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) show_pinned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) block_mount = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) bin_name = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) hash_init(prog_table.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) hash_init(map_table.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) hash_init(link_table.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) opterr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) options, NULL)) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) case 'V':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return do_version(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return do_help(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) pretty_output = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) case 'j':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) json_wtr = jsonw_new(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (!json_wtr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) p_err("failed to create JSON writer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) json_output = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) jsonw_pretty(json_wtr, pretty_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) show_pinned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) case 'm':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) relaxed_maps = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) block_mount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) libbpf_set_print(print_all_levels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) verifier_logs = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) p_err("unrecognized option '%s'", argv[optind - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) clean_and_exit(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) argc -= optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) argv += optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (argc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) ret = cmd_select(cmds, argc, argv, do_help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) jsonw_destroy(&json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (show_pinned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) delete_pinned_obj_table(&prog_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) delete_pinned_obj_table(&map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) delete_pinned_obj_table(&link_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }