Linux Audio

Check our new training course

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