| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 Netronome Systems, Inc. | 
|  | 3 | * | 
|  | 4 | * This software is dual licensed under the GNU General License Version 2, | 
|  | 5 | * June 1991 as shown in the file COPYING in the top-level directory of this | 
|  | 6 | * source tree or the BSD 2-Clause License provided below.  You have the | 
|  | 7 | * option to license this software under the complete terms of either license. | 
|  | 8 | * | 
|  | 9 | * The BSD 2-Clause License: | 
|  | 10 | * | 
|  | 11 | *     Redistribution and use in source and binary forms, with or | 
|  | 12 | *     without modification, are permitted provided that the following | 
|  | 13 | *     conditions are met: | 
|  | 14 | * | 
|  | 15 | *      1. Redistributions of source code must retain the above | 
|  | 16 | *         copyright notice, this list of conditions and the following | 
|  | 17 | *         disclaimer. | 
|  | 18 | * | 
|  | 19 | *      2. Redistributions in binary form must reproduce the above | 
|  | 20 | *         copyright notice, this list of conditions and the following | 
|  | 21 | *         disclaimer in the documentation and/or other materials | 
|  | 22 | *         provided with the distribution. | 
|  | 23 | * | 
|  | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
|  | 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
|  | 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
|  | 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | 
|  | 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | 
|  | 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
|  | 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
|  | 31 | * SOFTWARE. | 
|  | 32 | */ | 
|  | 33 |  | 
|  | 34 | /* Author: Jakub Kicinski <kubakici@wp.pl> */ | 
|  | 35 |  | 
|  | 36 | #include <bfd.h> | 
|  | 37 | #include <ctype.h> | 
|  | 38 | #include <errno.h> | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 39 | #include <getopt.h> | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 40 | #include <linux/bpf.h> | 
|  | 41 | #include <stdio.h> | 
|  | 42 | #include <stdlib.h> | 
|  | 43 | #include <string.h> | 
|  | 44 |  | 
|  | 45 | #include <bpf.h> | 
|  | 46 |  | 
|  | 47 | #include "main.h" | 
|  | 48 |  | 
| Quentin Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 49 | #define BATCH_LINE_LEN_MAX 65536 | 
|  | 50 | #define BATCH_ARG_NB_MAX 4096 | 
|  | 51 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 52 | const char *bin_name; | 
|  | 53 | static int last_argc; | 
|  | 54 | static char **last_argv; | 
|  | 55 | static int (*last_do_help)(int argc, char **argv); | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 56 | json_writer_t *json_wtr; | 
|  | 57 | bool pretty_output; | 
|  | 58 | bool json_output; | 
| Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 59 | bool show_pinned; | 
| Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 60 | struct pinned_obj_table prog_table; | 
|  | 61 | struct pinned_obj_table map_table; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 62 |  | 
| Quentin Monnet | 7868620 | 2017-11-28 17:44:29 -0800 | [diff] [blame] | 63 | static void __noreturn clean_and_exit(int i) | 
|  | 64 | { | 
|  | 65 | if (json_output) | 
|  | 66 | jsonw_destroy(&json_wtr); | 
|  | 67 |  | 
|  | 68 | exit(i); | 
|  | 69 | } | 
|  | 70 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 71 | void usage(void) | 
|  | 72 | { | 
|  | 73 | last_do_help(last_argc - 1, last_argv + 1); | 
|  | 74 |  | 
| Quentin Monnet | 7868620 | 2017-11-28 17:44:29 -0800 | [diff] [blame] | 75 | clean_and_exit(-1); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
|  | 78 | static int do_help(int argc, char **argv) | 
|  | 79 | { | 
| Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 80 | if (json_output) { | 
|  | 81 | jsonw_null(json_wtr); | 
|  | 82 | return 0; | 
|  | 83 | } | 
|  | 84 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 85 | fprintf(stderr, | 
| Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame] | 86 | "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n" | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 87 | "       %s batch file FILE\n" | 
| Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 88 | "       %s version\n" | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 89 | "\n" | 
| Yonghong Song | b04df40 | 2018-05-24 11:21:58 -0700 | [diff] [blame] | 90 | "       OBJECT := { prog | map | cgroup | perf }\n" | 
| Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame] | 91 | "       " HELP_SPEC_OPTIONS "\n" | 
|  | 92 | "", | 
| Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 93 | bin_name, bin_name, bin_name); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | return 0; | 
|  | 96 | } | 
|  | 97 |  | 
| Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 98 | static int do_version(int argc, char **argv) | 
|  | 99 | { | 
| Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 100 | if (json_output) { | 
|  | 101 | jsonw_start_object(json_wtr); | 
|  | 102 | jsonw_name(json_wtr, "version"); | 
| Roman Gushchin | 4bfe3bd | 2017-12-27 19:16:28 +0000 | [diff] [blame] | 103 | jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION); | 
| Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 104 | jsonw_end_object(json_wtr); | 
|  | 105 | } else { | 
| Roman Gushchin | 4bfe3bd | 2017-12-27 19:16:28 +0000 | [diff] [blame] | 106 | printf("%s v%s\n", bin_name, BPFTOOL_VERSION); | 
| Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 107 | } | 
| Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 108 | return 0; | 
|  | 109 | } | 
|  | 110 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 111 | int cmd_select(const struct cmd *cmds, int argc, char **argv, | 
|  | 112 | int (*help)(int argc, char **argv)) | 
|  | 113 | { | 
|  | 114 | unsigned int i; | 
|  | 115 |  | 
|  | 116 | last_argc = argc; | 
|  | 117 | last_argv = argv; | 
|  | 118 | last_do_help = help; | 
|  | 119 |  | 
|  | 120 | if (argc < 1 && cmds[0].func) | 
|  | 121 | return cmds[0].func(argc, argv); | 
|  | 122 |  | 
|  | 123 | for (i = 0; cmds[i].func; i++) | 
|  | 124 | if (is_prefix(*argv, cmds[i].cmd)) | 
|  | 125 | return cmds[i].func(argc - 1, argv + 1); | 
|  | 126 |  | 
|  | 127 | help(argc - 1, argv + 1); | 
|  | 128 |  | 
|  | 129 | return -1; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | bool is_prefix(const char *pfx, const char *str) | 
|  | 133 | { | 
|  | 134 | if (!pfx) | 
|  | 135 | return false; | 
|  | 136 | if (strlen(str) < strlen(pfx)) | 
|  | 137 | return false; | 
|  | 138 |  | 
|  | 139 | return !memcmp(str, pfx, strlen(pfx)); | 
|  | 140 | } | 
|  | 141 |  | 
| Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 142 | void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep) | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 143 | { | 
|  | 144 | unsigned char *data = arg; | 
|  | 145 | unsigned int i; | 
|  | 146 |  | 
|  | 147 | for (i = 0; i < n; i++) { | 
|  | 148 | const char *pfx = ""; | 
|  | 149 |  | 
|  | 150 | if (!i) | 
|  | 151 | /* nothing */; | 
|  | 152 | else if (!(i % 16)) | 
| Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 153 | fprintf(f, "\n"); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 154 | else if (!(i % 8)) | 
| Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 155 | fprintf(f, "  "); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 156 | else | 
|  | 157 | pfx = sep; | 
|  | 158 |  | 
| Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 159 | fprintf(f, "%s%02hhx", i ? pfx : "", data[i]); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
| Quentin Monnet | 668da74 | 2018-03-01 20:20:11 -0800 | [diff] [blame] | 163 | /* Split command line into argument vector. */ | 
|  | 164 | static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb) | 
|  | 165 | { | 
|  | 166 | static const char ws[] = " \t\r\n"; | 
|  | 167 | char *cp = line; | 
|  | 168 | int n_argc = 0; | 
|  | 169 |  | 
|  | 170 | while (*cp) { | 
|  | 171 | /* Skip leading whitespace. */ | 
|  | 172 | cp += strspn(cp, ws); | 
|  | 173 |  | 
|  | 174 | if (*cp == '\0') | 
|  | 175 | break; | 
|  | 176 |  | 
|  | 177 | if (n_argc >= (maxargs - 1)) { | 
|  | 178 | p_err("too many arguments to command %d", cmd_nb); | 
|  | 179 | return -1; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | /* Word begins with quote. */ | 
|  | 183 | if (*cp == '\'' || *cp == '"') { | 
|  | 184 | char quote = *cp++; | 
|  | 185 |  | 
|  | 186 | n_argv[n_argc++] = cp; | 
|  | 187 | /* Find ending quote. */ | 
|  | 188 | cp = strchr(cp, quote); | 
|  | 189 | if (!cp) { | 
|  | 190 | p_err("unterminated quoted string in command %d", | 
|  | 191 | cmd_nb); | 
|  | 192 | return -1; | 
|  | 193 | } | 
|  | 194 | } else { | 
|  | 195 | n_argv[n_argc++] = cp; | 
|  | 196 |  | 
|  | 197 | /* Find end of word. */ | 
|  | 198 | cp += strcspn(cp, ws); | 
|  | 199 | if (*cp == '\0') | 
|  | 200 | break; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | /* Separate words. */ | 
|  | 204 | *cp++ = 0; | 
|  | 205 | } | 
|  | 206 | n_argv[n_argc] = NULL; | 
|  | 207 |  | 
|  | 208 | return n_argc; | 
|  | 209 | } | 
|  | 210 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 211 | static int do_batch(int argc, char **argv); | 
|  | 212 |  | 
|  | 213 | static const struct cmd cmds[] = { | 
|  | 214 | { "help",	do_help }, | 
|  | 215 | { "batch",	do_batch }, | 
|  | 216 | { "prog",	do_prog }, | 
|  | 217 | { "map",	do_map }, | 
| Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 218 | { "cgroup",	do_cgroup }, | 
| Yonghong Song | b04df40 | 2018-05-24 11:21:58 -0700 | [diff] [blame] | 219 | { "perf",	do_perf }, | 
| Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 220 | { "version",	do_version }, | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 221 | { 0 } | 
|  | 222 | }; | 
|  | 223 |  | 
|  | 224 | static int do_batch(int argc, char **argv) | 
|  | 225 | { | 
| Quentin Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 226 | char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX]; | 
|  | 227 | char *n_argv[BATCH_ARG_NB_MAX]; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 228 | unsigned int lines = 0; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 229 | int n_argc; | 
|  | 230 | FILE *fp; | 
| Quentin Monnet | 06cc7fe | 2018-03-01 20:20:08 -0800 | [diff] [blame] | 231 | char *cp; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 232 | int err; | 
| Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 233 | int i; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 234 |  | 
|  | 235 | if (argc < 2) { | 
| Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 236 | p_err("too few parameters for batch"); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 237 | return -1; | 
|  | 238 | } else if (!is_prefix(*argv, "file")) { | 
| Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 239 | p_err("expected 'file', got: %s", *argv); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 240 | return -1; | 
|  | 241 | } else if (argc > 2) { | 
| Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 242 | p_err("too many parameters for batch"); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 243 | return -1; | 
|  | 244 | } | 
|  | 245 | NEXT_ARG(); | 
|  | 246 |  | 
| Quentin Monnet | 416656b | 2018-03-01 20:20:10 -0800 | [diff] [blame] | 247 | if (!strcmp(*argv, "-")) | 
|  | 248 | fp = stdin; | 
|  | 249 | else | 
|  | 250 | fp = fopen(*argv, "r"); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 251 | if (!fp) { | 
| Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 252 | p_err("Can't open file (%s): %s", *argv, strerror(errno)); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 253 | return -1; | 
|  | 254 | } | 
|  | 255 |  | 
| Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 256 | if (json_output) | 
|  | 257 | jsonw_start_array(json_wtr); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 258 | while (fgets(buf, sizeof(buf), fp)) { | 
| Quentin Monnet | 06cc7fe | 2018-03-01 20:20:08 -0800 | [diff] [blame] | 259 | cp = strchr(buf, '#'); | 
|  | 260 | if (cp) | 
|  | 261 | *cp = '\0'; | 
|  | 262 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 263 | if (strlen(buf) == sizeof(buf) - 1) { | 
|  | 264 | errno = E2BIG; | 
|  | 265 | break; | 
|  | 266 | } | 
|  | 267 |  | 
| Quentin Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 268 | /* Append continuation lines if any (coming after a line ending | 
|  | 269 | * with '\' in the batch file). | 
|  | 270 | */ | 
|  | 271 | while ((cp = strstr(buf, "\\\n")) != NULL) { | 
|  | 272 | if (!fgets(contline, sizeof(contline), fp) || | 
|  | 273 | strlen(contline) == 0) { | 
|  | 274 | p_err("missing continuation line on command %d", | 
|  | 275 | lines); | 
|  | 276 | err = -1; | 
|  | 277 | goto err_close; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | cp = strchr(contline, '#'); | 
|  | 281 | if (cp) | 
|  | 282 | *cp = '\0'; | 
|  | 283 |  | 
|  | 284 | if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) { | 
|  | 285 | p_err("command %d is too long", lines); | 
|  | 286 | err = -1; | 
|  | 287 | goto err_close; | 
|  | 288 | } | 
|  | 289 | buf[strlen(buf) - 2] = '\0'; | 
|  | 290 | strcat(buf, contline); | 
|  | 291 | } | 
|  | 292 |  | 
| Quentin Monnet | 668da74 | 2018-03-01 20:20:11 -0800 | [diff] [blame] | 293 | n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 294 | if (!n_argc) | 
|  | 295 | continue; | 
| Quentin Monnet | 668da74 | 2018-03-01 20:20:11 -0800 | [diff] [blame] | 296 | if (n_argc < 0) | 
|  | 297 | goto err_close; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 298 |  | 
| Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 299 | if (json_output) { | 
|  | 300 | jsonw_start_object(json_wtr); | 
|  | 301 | jsonw_name(json_wtr, "command"); | 
|  | 302 | jsonw_start_array(json_wtr); | 
|  | 303 | for (i = 0; i < n_argc; i++) | 
|  | 304 | jsonw_string(json_wtr, n_argv[i]); | 
|  | 305 | jsonw_end_array(json_wtr); | 
|  | 306 | jsonw_name(json_wtr, "output"); | 
|  | 307 | } | 
|  | 308 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 309 | err = cmd_select(cmds, n_argc, n_argv, do_help); | 
| Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 310 |  | 
|  | 311 | if (json_output) | 
|  | 312 | jsonw_end_object(json_wtr); | 
|  | 313 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 314 | if (err) | 
|  | 315 | goto err_close; | 
|  | 316 |  | 
|  | 317 | lines++; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | if (errno && errno != ENOENT) { | 
| Quentin Monnet | 9be6d41 | 2018-02-14 22:42:55 -0800 | [diff] [blame] | 321 | p_err("reading batch file failed: %s", strerror(errno)); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 322 | err = -1; | 
|  | 323 | } else { | 
| Quentin Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 324 | p_info("processed %d commands", lines); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 325 | err = 0; | 
|  | 326 | } | 
|  | 327 | err_close: | 
| Quentin Monnet | 416656b | 2018-03-01 20:20:10 -0800 | [diff] [blame] | 328 | if (fp != stdin) | 
|  | 329 | fclose(fp); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 330 |  | 
| Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 331 | if (json_output) | 
|  | 332 | jsonw_end_array(json_wtr); | 
|  | 333 |  | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 334 | return err; | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | int main(int argc, char **argv) | 
|  | 338 | { | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 339 | static const struct option options[] = { | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 340 | { "json",	no_argument,	NULL,	'j' }, | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 341 | { "help",	no_argument,	NULL,	'h' }, | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 342 | { "pretty",	no_argument,	NULL,	'p' }, | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 343 | { "version",	no_argument,	NULL,	'V' }, | 
| Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 344 | { "bpffs",	no_argument,	NULL,	'f' }, | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 345 | { 0 } | 
|  | 346 | }; | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 347 | int opt, ret; | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 348 |  | 
|  | 349 | last_do_help = do_help; | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 350 | pretty_output = false; | 
|  | 351 | json_output = false; | 
| Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 352 | show_pinned = false; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 353 | bin_name = argv[0]; | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 354 |  | 
| Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 355 | hash_init(prog_table.table); | 
|  | 356 | hash_init(map_table.table); | 
|  | 357 |  | 
| Quentin Monnet | 146882a | 2017-11-28 17:44:30 -0800 | [diff] [blame] | 358 | opterr = 0; | 
| Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 359 | while ((opt = getopt_long(argc, argv, "Vhpjf", | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 360 | options, NULL)) >= 0) { | 
|  | 361 | switch (opt) { | 
|  | 362 | case 'V': | 
|  | 363 | return do_version(argc, argv); | 
|  | 364 | case 'h': | 
|  | 365 | return do_help(argc, argv); | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 366 | case 'p': | 
|  | 367 | pretty_output = true; | 
|  | 368 | /* fall through */ | 
|  | 369 | case 'j': | 
| Quentin Monnet | 9b85c2d | 2017-11-28 17:44:28 -0800 | [diff] [blame] | 370 | if (!json_output) { | 
|  | 371 | json_wtr = jsonw_new(stdout); | 
|  | 372 | if (!json_wtr) { | 
|  | 373 | p_err("failed to create JSON writer"); | 
|  | 374 | return -1; | 
|  | 375 | } | 
|  | 376 | json_output = true; | 
|  | 377 | } | 
|  | 378 | jsonw_pretty(json_wtr, pretty_output); | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 379 | break; | 
| Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 380 | case 'f': | 
|  | 381 | show_pinned = true; | 
|  | 382 | break; | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 383 | default: | 
| Quentin Monnet | 146882a | 2017-11-28 17:44:30 -0800 | [diff] [blame] | 384 | p_err("unrecognized option '%s'", argv[optind - 1]); | 
|  | 385 | if (json_output) | 
|  | 386 | clean_and_exit(-1); | 
|  | 387 | else | 
|  | 388 | usage(); | 
| Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 389 | } | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | argc -= optind; | 
|  | 393 | argv += optind; | 
|  | 394 | if (argc < 0) | 
|  | 395 | usage(); | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 396 |  | 
|  | 397 | bfd_init(); | 
|  | 398 |  | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 399 | ret = cmd_select(cmds, argc, argv, do_help); | 
|  | 400 |  | 
|  | 401 | if (json_output) | 
|  | 402 | jsonw_destroy(&json_wtr); | 
|  | 403 |  | 
| Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 404 | if (show_pinned) { | 
|  | 405 | delete_pinned_obj_table(&prog_table); | 
|  | 406 | delete_pinned_obj_table(&map_table); | 
|  | 407 | } | 
| Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 408 |  | 
| Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 409 | return ret; | 
| Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 410 | } |