prog.c 16.4 KB
Newer Older
J
Jakub Kicinski 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (C) 2017 Netronome Systems, Inc.
 *
 * This software is dual licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree or the BSD 2-Clause License provided below.  You have the
 * option to license this software under the complete terms of either license.
 *
 * The BSD 2-Clause License:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      1. Redistributions of source code must retain the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer.
 *
 *      2. Redistributions in binary form must reproduce the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer in the documentation and/or other materials
 *         provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Author: Jakub Kicinski <kubakici@wp.pl> */

#include <errno.h>
#include <fcntl.h>
38
#include <stdarg.h>
J
Jakub Kicinski 已提交
39 40 41 42 43 44 45 46 47
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <bpf.h>
48
#include <libbpf.h>
J
Jakub Kicinski 已提交
49

50
#include "cfg.h"
J
Jakub Kicinski 已提交
51
#include "main.h"
52
#include "xlated_dumper.h"
J
Jakub Kicinski 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

static const char * const prog_type_name[] = {
	[BPF_PROG_TYPE_UNSPEC]		= "unspec",
	[BPF_PROG_TYPE_SOCKET_FILTER]	= "socket_filter",
	[BPF_PROG_TYPE_KPROBE]		= "kprobe",
	[BPF_PROG_TYPE_SCHED_CLS]	= "sched_cls",
	[BPF_PROG_TYPE_SCHED_ACT]	= "sched_act",
	[BPF_PROG_TYPE_TRACEPOINT]	= "tracepoint",
	[BPF_PROG_TYPE_XDP]		= "xdp",
	[BPF_PROG_TYPE_PERF_EVENT]	= "perf_event",
	[BPF_PROG_TYPE_CGROUP_SKB]	= "cgroup_skb",
	[BPF_PROG_TYPE_CGROUP_SOCK]	= "cgroup_sock",
	[BPF_PROG_TYPE_LWT_IN]		= "lwt_in",
	[BPF_PROG_TYPE_LWT_OUT]		= "lwt_out",
	[BPF_PROG_TYPE_LWT_XMIT]	= "lwt_xmit",
	[BPF_PROG_TYPE_SOCK_OPS]	= "sock_ops",
	[BPF_PROG_TYPE_SK_SKB]		= "sk_skb",
70
	[BPF_PROG_TYPE_CGROUP_DEVICE]	= "cgroup_device",
71 72 73
	[BPF_PROG_TYPE_SK_MSG]		= "sk_msg",
	[BPF_PROG_TYPE_RAW_TRACEPOINT]	= "raw_tracepoint",
	[BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
74
	[BPF_PROG_TYPE_LIRC_MODE2]	= "lirc_mode2",
J
Jakub Kicinski 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
};

static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
{
	struct timespec real_time_ts, boot_time_ts;
	time_t wallclock_secs;
	struct tm load_tm;

	buf[--size] = '\0';

	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
		perror("Can't read clocks");
		snprintf(buf, size, "%llu", nsecs / 1000000000);
		return;
	}

	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
93 94 95
		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
		1000000000;

J
Jakub Kicinski 已提交
96 97 98 99 100 101

	if (!localtime_r(&wallclock_secs, &load_tm)) {
		snprintf(buf, size, "%llu", nsecs / 1000000000);
		return;
	}

102 103 104 105
	if (json_output)
		strftime(buf, size, "%s", &load_tm);
	else
		strftime(buf, size, "%FT%T%z", &load_tm);
J
Jakub Kicinski 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
}

static int prog_fd_by_tag(unsigned char *tag)
{
	struct bpf_prog_info info = {};
	__u32 len = sizeof(info);
	unsigned int id = 0;
	int err;
	int fd;

	while (true) {
		err = bpf_prog_get_next_id(id, &id);
		if (err) {
119
			p_err("%s", strerror(errno));
J
Jakub Kicinski 已提交
120 121 122 123 124
			return -1;
		}

		fd = bpf_prog_get_fd_by_id(id);
		if (fd < 0) {
125 126
			p_err("can't get prog by id (%u): %s",
			      id, strerror(errno));
J
Jakub Kicinski 已提交
127 128 129 130 131
			return -1;
		}

		err = bpf_obj_get_info_by_fd(fd, &info, &len);
		if (err) {
132 133
			p_err("can't get prog info (%u): %s",
			      id, strerror(errno));
J
Jakub Kicinski 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
			close(fd);
			return -1;
		}

		if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
			return fd;

		close(fd);
	}
}

int prog_parse_fd(int *argc, char ***argv)
{
	int fd;

	if (is_prefix(**argv, "id")) {
		unsigned int id;
		char *endptr;

		NEXT_ARGP();

		id = strtoul(**argv, &endptr, 0);
		if (*endptr) {
157
			p_err("can't parse %s as ID", **argv);
J
Jakub Kicinski 已提交
158 159 160 161 162 163
			return -1;
		}
		NEXT_ARGP();

		fd = bpf_prog_get_fd_by_id(id);
		if (fd < 0)
164
			p_err("get by id (%u): %s", id, strerror(errno));
J
Jakub Kicinski 已提交
165 166 167 168 169 170 171 172 173
		return fd;
	} else if (is_prefix(**argv, "tag")) {
		unsigned char tag[BPF_TAG_SIZE];

		NEXT_ARGP();

		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
		    != BPF_TAG_SIZE) {
174
			p_err("can't parse tag");
J
Jakub Kicinski 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
			return -1;
		}
		NEXT_ARGP();

		return prog_fd_by_tag(tag);
	} else if (is_prefix(**argv, "pinned")) {
		char *path;

		NEXT_ARGP();

		path = **argv;
		NEXT_ARGP();

		return open_obj_pinned_any(path, BPF_OBJ_PROG);
	}

191
	p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
J
Jakub Kicinski 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	return -1;
}

static void show_prog_maps(int fd, u32 num_maps)
{
	struct bpf_prog_info info = {};
	__u32 len = sizeof(info);
	__u32 map_ids[num_maps];
	unsigned int i;
	int err;

	info.nr_map_ids = num_maps;
	info.map_ids = ptr_to_u64(map_ids);

	err = bpf_obj_get_info_by_fd(fd, &info, &len);
	if (err || !info.nr_map_ids)
		return;

210 211 212 213 214 215 216 217 218 219 220 221
	if (json_output) {
		jsonw_name(json_wtr, "map_ids");
		jsonw_start_array(json_wtr);
		for (i = 0; i < info.nr_map_ids; i++)
			jsonw_uint(json_wtr, map_ids[i]);
		jsonw_end_array(json_wtr);
	} else {
		printf("  map_ids ");
		for (i = 0; i < info.nr_map_ids; i++)
			printf("%u%s", map_ids[i],
			       i == info.nr_map_ids - 1 ? "" : ",");
	}
J
Jakub Kicinski 已提交
222 223
}

224
static void print_prog_json(struct bpf_prog_info *info, int fd)
J
Jakub Kicinski 已提交
225 226 227
{
	char *memlock;

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	jsonw_start_object(json_wtr);
	jsonw_uint_field(json_wtr, "id", info->id);
	if (info->type < ARRAY_SIZE(prog_type_name))
		jsonw_string_field(json_wtr, "type",
				   prog_type_name[info->type]);
	else
		jsonw_uint_field(json_wtr, "type", info->type);

	if (*info->name)
		jsonw_string_field(json_wtr, "name", info->name);

	jsonw_name(json_wtr, "tag");
	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);

244 245
	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);

246 247
	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);

248 249 250 251 252 253
	if (info->load_time) {
		char buf[32];

		print_boot_time(info->load_time, buf, sizeof(buf));

		/* Piggy back on load_time, since 0 uid is a valid one */
254 255
		jsonw_name(json_wtr, "loaded_at");
		jsonw_printf(json_wtr, "%s", buf);
256 257 258 259 260 261 262 263 264 265
		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
	}

	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);

	if (info->jited_prog_len) {
		jsonw_bool_field(json_wtr, "jited", true);
		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
	} else {
		jsonw_bool_field(json_wtr, "jited", false);
J
Jakub Kicinski 已提交
266 267
	}

268 269 270 271 272 273 274 275
	memlock = get_fdinfo(fd, "memlock");
	if (memlock)
		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
	free(memlock);

	if (info->nr_map_ids)
		show_prog_maps(fd, info->nr_map_ids);

276 277 278 279 280 281 282 283 284 285 286 287
	if (!hash_empty(prog_table.table)) {
		struct pinned_obj *obj;

		jsonw_name(json_wtr, "pinned");
		jsonw_start_array(json_wtr);
		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
			if (obj->id == info->id)
				jsonw_string(json_wtr, obj->path);
		}
		jsonw_end_array(json_wtr);
	}

288 289 290 291 292 293 294 295 296 297
	jsonw_end_object(json_wtr);
}

static void print_prog_plain(struct bpf_prog_info *info, int fd)
{
	char *memlock;

	printf("%u: ", info->id);
	if (info->type < ARRAY_SIZE(prog_type_name))
		printf("%s  ", prog_type_name[info->type]);
J
Jakub Kicinski 已提交
298
	else
299
		printf("type %u  ", info->type);
J
Jakub Kicinski 已提交
300

301 302
	if (*info->name)
		printf("name %s  ", info->name);
J
Jakub Kicinski 已提交
303 304

	printf("tag ");
305
	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
306
	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
307
	printf("%s", info->gpl_compatible ? "  gpl" : "");
J
Jakub Kicinski 已提交
308 309
	printf("\n");

310
	if (info->load_time) {
J
Jakub Kicinski 已提交
311 312
		char buf[32];

313
		print_boot_time(info->load_time, buf, sizeof(buf));
J
Jakub Kicinski 已提交
314 315

		/* Piggy back on load_time, since 0 uid is a valid one */
316
		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
J
Jakub Kicinski 已提交
317 318
	}

319
	printf("\txlated %uB", info->xlated_prog_len);
J
Jakub Kicinski 已提交
320

321 322
	if (info->jited_prog_len)
		printf("  jited %uB", info->jited_prog_len);
J
Jakub Kicinski 已提交
323 324 325 326 327 328 329 330
	else
		printf("  not jited");

	memlock = get_fdinfo(fd, "memlock");
	if (memlock)
		printf("  memlock %sB", memlock);
	free(memlock);

331 332
	if (info->nr_map_ids)
		show_prog_maps(fd, info->nr_map_ids);
J
Jakub Kicinski 已提交
333

334 335 336 337 338 339 340 341 342 343
	if (!hash_empty(prog_table.table)) {
		struct pinned_obj *obj;

		printf("\n");
		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
			if (obj->id == info->id)
				printf("\tpinned %s\n", obj->path);
		}
	}

J
Jakub Kicinski 已提交
344
	printf("\n");
345 346 347 348 349 350 351 352 353 354
}

static int show_prog(int fd)
{
	struct bpf_prog_info info = {};
	__u32 len = sizeof(info);
	int err;

	err = bpf_obj_get_info_by_fd(fd, &info, &len);
	if (err) {
355
		p_err("can't get prog info: %s", strerror(errno));
356 357 358 359 360 361 362
		return -1;
	}

	if (json_output)
		print_prog_json(&info, fd);
	else
		print_prog_plain(&info, fd);
J
Jakub Kicinski 已提交
363 364 365 366 367

	return 0;
}

static int do_show(int argc, char **argv)
368 369
{
	__u32 id = 0;
J
Jakub Kicinski 已提交
370 371 372
	int err;
	int fd;

373 374
	if (show_pinned)
		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
375

J
Jakub Kicinski 已提交
376 377 378 379 380 381 382 383 384 385 386
	if (argc == 2) {
		fd = prog_parse_fd(&argc, &argv);
		if (fd < 0)
			return -1;

		return show_prog(fd);
	}

	if (argc)
		return BAD_ARG();

387 388
	if (json_output)
		jsonw_start_array(json_wtr);
J
Jakub Kicinski 已提交
389 390 391
	while (true) {
		err = bpf_prog_get_next_id(id, &id);
		if (err) {
392 393
			if (errno == ENOENT) {
				err = 0;
J
Jakub Kicinski 已提交
394
				break;
395
			}
396 397
			p_err("can't get next program: %s%s", strerror(errno),
			      errno == EINVAL ? " -- kernel too old?" : "");
398 399
			err = -1;
			break;
J
Jakub Kicinski 已提交
400 401 402 403
		}

		fd = bpf_prog_get_fd_by_id(id);
		if (fd < 0) {
404 405
			if (errno == ENOENT)
				continue;
406 407
			p_err("can't get prog by id (%u): %s",
			      id, strerror(errno));
408 409
			err = -1;
			break;
J
Jakub Kicinski 已提交
410 411 412 413 414
		}

		err = show_prog(fd);
		close(fd);
		if (err)
415
			break;
J
Jakub Kicinski 已提交
416 417
	}

418 419 420 421
	if (json_output)
		jsonw_end_array(json_wtr);

	return err;
J
Jakub Kicinski 已提交
422 423 424 425
}

static int do_dump(int argc, char **argv)
{
426
	unsigned long *func_ksyms = NULL;
J
Jakub Kicinski 已提交
427
	struct bpf_prog_info info = {};
428
	unsigned int *func_lens = NULL;
429
	unsigned int nr_func_ksyms;
430
	unsigned int nr_func_lens;
431
	struct dump_data dd = {};
J
Jakub Kicinski 已提交
432 433 434 435
	__u32 len = sizeof(info);
	unsigned int buf_size;
	char *filepath = NULL;
	bool opcodes = false;
436
	bool visual = false;
J
Jakub Kicinski 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450
	unsigned char *buf;
	__u32 *member_len;
	__u64 *member_ptr;
	ssize_t n;
	int err;
	int fd;

	if (is_prefix(*argv, "jited")) {
		member_len = &info.jited_prog_len;
		member_ptr = &info.jited_prog_insns;
	} else if (is_prefix(*argv, "xlated")) {
		member_len = &info.xlated_prog_len;
		member_ptr = &info.xlated_prog_insns;
	} else {
451
		p_err("expected 'xlated' or 'jited', got: %s", *argv);
J
Jakub Kicinski 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465
		return -1;
	}
	NEXT_ARG();

	if (argc < 2)
		usage();

	fd = prog_parse_fd(&argc, &argv);
	if (fd < 0)
		return -1;

	if (is_prefix(*argv, "file")) {
		NEXT_ARG();
		if (!argc) {
466
			p_err("expected file path");
J
Jakub Kicinski 已提交
467 468 469 470 471 472 473 474
			return -1;
		}

		filepath = *argv;
		NEXT_ARG();
	} else if (is_prefix(*argv, "opcodes")) {
		opcodes = true;
		NEXT_ARG();
475 476 477
	} else if (is_prefix(*argv, "visual")) {
		visual = true;
		NEXT_ARG();
J
Jakub Kicinski 已提交
478 479 480 481 482 483 484 485 486
	}

	if (argc) {
		usage();
		return -1;
	}

	err = bpf_obj_get_info_by_fd(fd, &info, &len);
	if (err) {
487
		p_err("can't get prog info: %s", strerror(errno));
J
Jakub Kicinski 已提交
488 489 490 491
		return -1;
	}

	if (!*member_len) {
492
		p_info("no instructions returned");
J
Jakub Kicinski 已提交
493 494 495 496 497 498 499 500
		close(fd);
		return 0;
	}

	buf_size = *member_len;

	buf = malloc(buf_size);
	if (!buf) {
501
		p_err("mem alloc failed");
J
Jakub Kicinski 已提交
502 503 504 505
		close(fd);
		return -1;
	}

506 507 508 509 510 511 512 513 514 515
	nr_func_ksyms = info.nr_jited_ksyms;
	if (nr_func_ksyms) {
		func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
		if (!func_ksyms) {
			p_err("mem alloc failed");
			close(fd);
			goto err_free;
		}
	}

516 517 518 519 520 521 522 523 524 525
	nr_func_lens = info.nr_jited_func_lens;
	if (nr_func_lens) {
		func_lens = malloc(nr_func_lens * sizeof(__u32));
		if (!func_lens) {
			p_err("mem alloc failed");
			close(fd);
			goto err_free;
		}
	}

J
Jakub Kicinski 已提交
526 527 528 529
	memset(&info, 0, sizeof(info));

	*member_ptr = ptr_to_u64(buf);
	*member_len = buf_size;
530 531
	info.jited_ksyms = ptr_to_u64(func_ksyms);
	info.nr_jited_ksyms = nr_func_ksyms;
532 533
	info.jited_func_lens = ptr_to_u64(func_lens);
	info.nr_jited_func_lens = nr_func_lens;
J
Jakub Kicinski 已提交
534 535 536 537

	err = bpf_obj_get_info_by_fd(fd, &info, &len);
	close(fd);
	if (err) {
538
		p_err("can't get prog info: %s", strerror(errno));
J
Jakub Kicinski 已提交
539 540 541 542
		goto err_free;
	}

	if (*member_len > buf_size) {
543
		p_err("too many instructions returned");
J
Jakub Kicinski 已提交
544 545 546
		goto err_free;
	}

547 548 549 550 551
	if (info.nr_jited_ksyms > nr_func_ksyms) {
		p_err("too many addresses returned");
		goto err_free;
	}

552 553 554 555 556
	if (info.nr_jited_func_lens > nr_func_lens) {
		p_err("too many values returned");
		goto err_free;
	}

557 558 559 560 561 562 563 564
	if ((member_len == &info.jited_prog_len &&
	     info.jited_prog_insns == 0) ||
	    (member_len == &info.xlated_prog_len &&
	     info.xlated_prog_insns == 0)) {
		p_err("error retrieving insn dump: kernel.kptr_restrict set?");
		goto err_free;
	}

J
Jakub Kicinski 已提交
565 566 567
	if (filepath) {
		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
		if (fd < 0) {
568 569
			p_err("can't open file %s: %s", filepath,
			      strerror(errno));
J
Jakub Kicinski 已提交
570 571 572 573 574 575
			goto err_free;
		}

		n = write(fd, buf, *member_len);
		close(fd);
		if (n != *member_len) {
576 577
			p_err("error writing output file: %s",
			      n < 0 ? strerror(errno) : "short write");
J
Jakub Kicinski 已提交
578 579
			goto err_free;
		}
580 581 582

		if (json_output)
			jsonw_null(json_wtr);
583 584 585 586 587 588 589 590 591
	} else if (member_len == &info.jited_prog_len) {
		const char *name = NULL;

		if (info.ifindex) {
			name = ifindex_to_bfd_name_ns(info.ifindex,
						      info.netns_dev,
						      info.netns_ino);
			if (!name)
				goto err_free;
592
		}
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
		if (info.nr_jited_func_lens && info.jited_func_lens) {
			struct kernel_sym *sym = NULL;
			char sym_name[SYM_MAX_NAME];
			unsigned char *img = buf;
			__u64 *ksyms = NULL;
			__u32 *lens;
			__u32 i;

			if (info.nr_jited_ksyms) {
				kernel_syms_load(&dd);
				ksyms = (__u64 *) info.jited_ksyms;
			}

			if (json_output)
				jsonw_start_array(json_wtr);

			lens = (__u32 *) info.jited_func_lens;
			for (i = 0; i < info.nr_jited_func_lens; i++) {
				if (ksyms) {
					sym = kernel_syms_search(&dd, ksyms[i]);
					if (sym)
						sprintf(sym_name, "%s", sym->name);
					else
						sprintf(sym_name, "0x%016llx", ksyms[i]);
				} else {
					strcpy(sym_name, "unknown");
				}

				if (json_output) {
					jsonw_start_object(json_wtr);
					jsonw_name(json_wtr, "name");
					jsonw_string(json_wtr, sym_name);
					jsonw_name(json_wtr, "insns");
				} else {
					printf("%s:\n", sym_name);
				}

				disasm_print_insn(img, lens[i], opcodes, name);
				img += lens[i];

				if (json_output)
					jsonw_end_object(json_wtr);
				else
					printf("\n");
			}

			if (json_output)
				jsonw_end_array(json_wtr);
		} else {
			disasm_print_insn(buf, *member_len, opcodes, name);
		}
645 646 647 648 649
	} else if (visual) {
		if (json_output)
			jsonw_null(json_wtr);
		else
			dump_xlated_cfg(buf, *member_len);
650 651
	} else {
		kernel_syms_load(&dd);
652 653 654
		dd.nr_jited_ksyms = info.nr_jited_ksyms;
		dd.jited_ksyms = (__u64 *) info.jited_ksyms;

655 656 657 658 659
		if (json_output)
			dump_xlated_json(&dd, buf, *member_len, opcodes);
		else
			dump_xlated_plain(&dd, buf, *member_len, opcodes);
		kernel_syms_destroy(&dd);
J
Jakub Kicinski 已提交
660 661 662
	}

	free(buf);
663
	free(func_ksyms);
664
	free(func_lens);
J
Jakub Kicinski 已提交
665 666 667 668
	return 0;

err_free:
	free(buf);
669
	free(func_ksyms);
670
	free(func_lens);
J
Jakub Kicinski 已提交
671 672 673 674 675
	return -1;
}

static int do_pin(int argc, char **argv)
{
676 677 678 679 680 681
	int err;

	err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
	if (!err && json_output)
		jsonw_null(json_wtr);
	return err;
J
Jakub Kicinski 已提交
682 683
}

684 685 686 687 688 689 690 691 692
static int do_load(int argc, char **argv)
{
	struct bpf_object *obj;
	int prog_fd;

	if (argc != 2)
		usage();

	if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
693
		p_err("failed to load program");
694 695 696
		return -1;
	}

697
	if (do_pin_fd(prog_fd, argv[1]))
698 699 700 701 702 703 704 705
		return -1;

	if (json_output)
		jsonw_null(json_wtr);

	return 0;
}

J
Jakub Kicinski 已提交
706 707
static int do_help(int argc, char **argv)
{
708 709 710 711 712
	if (json_output) {
		jsonw_null(json_wtr);
		return 0;
	}

J
Jakub Kicinski 已提交
713
	fprintf(stderr,
714
		"Usage: %s %s { show | list } [PROG]\n"
715
		"       %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
716
		"       %s %s dump jited  PROG [{ file FILE | opcodes }]\n"
J
Jakub Kicinski 已提交
717
		"       %s %s pin   PROG FILE\n"
718
		"       %s %s load  OBJ  FILE\n"
J
Jakub Kicinski 已提交
719 720 721
		"       %s %s help\n"
		"\n"
		"       " HELP_SPEC_PROGRAM "\n"
722
		"       " HELP_SPEC_OPTIONS "\n"
J
Jakub Kicinski 已提交
723 724
		"",
		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
725
		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
J
Jakub Kicinski 已提交
726 727 728 729 730 731

	return 0;
}

static const struct cmd cmds[] = {
	{ "show",	do_show },
732
	{ "list",	do_show },
733
	{ "help",	do_help },
J
Jakub Kicinski 已提交
734 735
	{ "dump",	do_dump },
	{ "pin",	do_pin },
736
	{ "load",	do_load },
J
Jakub Kicinski 已提交
737 738 739 740 741 742 743
	{ 0 }
};

int do_prog(int argc, char **argv)
{
	return cmd_select(cmds, argc, argv, do_help);
}