xref: /openbmc/linux/tools/bpf/bpftool/prog.c (revision 7105e828c087de970fcb5a9509db51bfe6bd7894)
171bb428fSJakub Kicinski /*
271bb428fSJakub Kicinski  * Copyright (C) 2017 Netronome Systems, Inc.
371bb428fSJakub Kicinski  *
471bb428fSJakub Kicinski  * This software is dual licensed under the GNU General License Version 2,
571bb428fSJakub Kicinski  * June 1991 as shown in the file COPYING in the top-level directory of this
671bb428fSJakub Kicinski  * source tree or the BSD 2-Clause License provided below.  You have the
771bb428fSJakub Kicinski  * option to license this software under the complete terms of either license.
871bb428fSJakub Kicinski  *
971bb428fSJakub Kicinski  * The BSD 2-Clause License:
1071bb428fSJakub Kicinski  *
1171bb428fSJakub Kicinski  *     Redistribution and use in source and binary forms, with or
1271bb428fSJakub Kicinski  *     without modification, are permitted provided that the following
1371bb428fSJakub Kicinski  *     conditions are met:
1471bb428fSJakub Kicinski  *
1571bb428fSJakub Kicinski  *      1. Redistributions of source code must retain the above
1671bb428fSJakub Kicinski  *         copyright notice, this list of conditions and the following
1771bb428fSJakub Kicinski  *         disclaimer.
1871bb428fSJakub Kicinski  *
1971bb428fSJakub Kicinski  *      2. Redistributions in binary form must reproduce the above
2071bb428fSJakub Kicinski  *         copyright notice, this list of conditions and the following
2171bb428fSJakub Kicinski  *         disclaimer in the documentation and/or other materials
2271bb428fSJakub Kicinski  *         provided with the distribution.
2371bb428fSJakub Kicinski  *
2471bb428fSJakub Kicinski  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2571bb428fSJakub Kicinski  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2671bb428fSJakub Kicinski  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2771bb428fSJakub Kicinski  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2871bb428fSJakub Kicinski  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2971bb428fSJakub Kicinski  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3071bb428fSJakub Kicinski  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3171bb428fSJakub Kicinski  * SOFTWARE.
3271bb428fSJakub Kicinski  */
3371bb428fSJakub Kicinski 
3471bb428fSJakub Kicinski /* Author: Jakub Kicinski <kubakici@wp.pl> */
3571bb428fSJakub Kicinski 
3671bb428fSJakub Kicinski #include <errno.h>
3771bb428fSJakub Kicinski #include <fcntl.h>
38c9c35995SJakub Kicinski #include <stdarg.h>
3971bb428fSJakub Kicinski #include <stdio.h>
4071bb428fSJakub Kicinski #include <stdlib.h>
4171bb428fSJakub Kicinski #include <string.h>
4271bb428fSJakub Kicinski #include <time.h>
4371bb428fSJakub Kicinski #include <unistd.h>
4471bb428fSJakub Kicinski #include <sys/types.h>
4571bb428fSJakub Kicinski #include <sys/stat.h>
4671bb428fSJakub Kicinski 
4771bb428fSJakub Kicinski #include <bpf.h>
4849a086c2SRoman Gushchin #include <libbpf.h>
4971bb428fSJakub Kicinski 
5071bb428fSJakub Kicinski #include "main.h"
51c9c35995SJakub Kicinski #include "disasm.h"
5271bb428fSJakub Kicinski 
5371bb428fSJakub Kicinski static const char * const prog_type_name[] = {
5471bb428fSJakub Kicinski 	[BPF_PROG_TYPE_UNSPEC]		= "unspec",
5571bb428fSJakub Kicinski 	[BPF_PROG_TYPE_SOCKET_FILTER]	= "socket_filter",
5671bb428fSJakub Kicinski 	[BPF_PROG_TYPE_KPROBE]		= "kprobe",
5771bb428fSJakub Kicinski 	[BPF_PROG_TYPE_SCHED_CLS]	= "sched_cls",
5871bb428fSJakub Kicinski 	[BPF_PROG_TYPE_SCHED_ACT]	= "sched_act",
5971bb428fSJakub Kicinski 	[BPF_PROG_TYPE_TRACEPOINT]	= "tracepoint",
6071bb428fSJakub Kicinski 	[BPF_PROG_TYPE_XDP]		= "xdp",
6171bb428fSJakub Kicinski 	[BPF_PROG_TYPE_PERF_EVENT]	= "perf_event",
6271bb428fSJakub Kicinski 	[BPF_PROG_TYPE_CGROUP_SKB]	= "cgroup_skb",
6371bb428fSJakub Kicinski 	[BPF_PROG_TYPE_CGROUP_SOCK]	= "cgroup_sock",
6471bb428fSJakub Kicinski 	[BPF_PROG_TYPE_LWT_IN]		= "lwt_in",
6571bb428fSJakub Kicinski 	[BPF_PROG_TYPE_LWT_OUT]		= "lwt_out",
6671bb428fSJakub Kicinski 	[BPF_PROG_TYPE_LWT_XMIT]	= "lwt_xmit",
6771bb428fSJakub Kicinski 	[BPF_PROG_TYPE_SOCK_OPS]	= "sock_ops",
6871bb428fSJakub Kicinski 	[BPF_PROG_TYPE_SK_SKB]		= "sk_skb",
6971bb428fSJakub Kicinski };
7071bb428fSJakub Kicinski 
7171bb428fSJakub Kicinski static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
7271bb428fSJakub Kicinski {
7371bb428fSJakub Kicinski 	struct timespec real_time_ts, boot_time_ts;
7471bb428fSJakub Kicinski 	time_t wallclock_secs;
7571bb428fSJakub Kicinski 	struct tm load_tm;
7671bb428fSJakub Kicinski 
7771bb428fSJakub Kicinski 	buf[--size] = '\0';
7871bb428fSJakub Kicinski 
7971bb428fSJakub Kicinski 	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
8071bb428fSJakub Kicinski 	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
8171bb428fSJakub Kicinski 		perror("Can't read clocks");
8271bb428fSJakub Kicinski 		snprintf(buf, size, "%llu", nsecs / 1000000000);
8371bb428fSJakub Kicinski 		return;
8471bb428fSJakub Kicinski 	}
8571bb428fSJakub Kicinski 
8671bb428fSJakub Kicinski 	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
8771bb428fSJakub Kicinski 		nsecs / 1000000000;
8871bb428fSJakub Kicinski 
8971bb428fSJakub Kicinski 	if (!localtime_r(&wallclock_secs, &load_tm)) {
9071bb428fSJakub Kicinski 		snprintf(buf, size, "%llu", nsecs / 1000000000);
9171bb428fSJakub Kicinski 		return;
9271bb428fSJakub Kicinski 	}
9371bb428fSJakub Kicinski 
9471bb428fSJakub Kicinski 	strftime(buf, size, "%b %d/%H:%M", &load_tm);
9571bb428fSJakub Kicinski }
9671bb428fSJakub Kicinski 
9771bb428fSJakub Kicinski static int prog_fd_by_tag(unsigned char *tag)
9871bb428fSJakub Kicinski {
9971bb428fSJakub Kicinski 	struct bpf_prog_info info = {};
10071bb428fSJakub Kicinski 	__u32 len = sizeof(info);
10171bb428fSJakub Kicinski 	unsigned int id = 0;
10271bb428fSJakub Kicinski 	int err;
10371bb428fSJakub Kicinski 	int fd;
10471bb428fSJakub Kicinski 
10571bb428fSJakub Kicinski 	while (true) {
10671bb428fSJakub Kicinski 		err = bpf_prog_get_next_id(id, &id);
10771bb428fSJakub Kicinski 		if (err) {
1089a5ab8bfSQuentin Monnet 			p_err("%s", strerror(errno));
10971bb428fSJakub Kicinski 			return -1;
11071bb428fSJakub Kicinski 		}
11171bb428fSJakub Kicinski 
11271bb428fSJakub Kicinski 		fd = bpf_prog_get_fd_by_id(id);
11371bb428fSJakub Kicinski 		if (fd < 0) {
1149a5ab8bfSQuentin Monnet 			p_err("can't get prog by id (%u): %s",
11571bb428fSJakub Kicinski 			      id, strerror(errno));
11671bb428fSJakub Kicinski 			return -1;
11771bb428fSJakub Kicinski 		}
11871bb428fSJakub Kicinski 
11971bb428fSJakub Kicinski 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
12071bb428fSJakub Kicinski 		if (err) {
1219a5ab8bfSQuentin Monnet 			p_err("can't get prog info (%u): %s",
12271bb428fSJakub Kicinski 			      id, strerror(errno));
12371bb428fSJakub Kicinski 			close(fd);
12471bb428fSJakub Kicinski 			return -1;
12571bb428fSJakub Kicinski 		}
12671bb428fSJakub Kicinski 
12771bb428fSJakub Kicinski 		if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
12871bb428fSJakub Kicinski 			return fd;
12971bb428fSJakub Kicinski 
13071bb428fSJakub Kicinski 		close(fd);
13171bb428fSJakub Kicinski 	}
13271bb428fSJakub Kicinski }
13371bb428fSJakub Kicinski 
13471bb428fSJakub Kicinski int prog_parse_fd(int *argc, char ***argv)
13571bb428fSJakub Kicinski {
13671bb428fSJakub Kicinski 	int fd;
13771bb428fSJakub Kicinski 
13871bb428fSJakub Kicinski 	if (is_prefix(**argv, "id")) {
13971bb428fSJakub Kicinski 		unsigned int id;
14071bb428fSJakub Kicinski 		char *endptr;
14171bb428fSJakub Kicinski 
14271bb428fSJakub Kicinski 		NEXT_ARGP();
14371bb428fSJakub Kicinski 
14471bb428fSJakub Kicinski 		id = strtoul(**argv, &endptr, 0);
14571bb428fSJakub Kicinski 		if (*endptr) {
1469a5ab8bfSQuentin Monnet 			p_err("can't parse %s as ID", **argv);
14771bb428fSJakub Kicinski 			return -1;
14871bb428fSJakub Kicinski 		}
14971bb428fSJakub Kicinski 		NEXT_ARGP();
15071bb428fSJakub Kicinski 
15171bb428fSJakub Kicinski 		fd = bpf_prog_get_fd_by_id(id);
15271bb428fSJakub Kicinski 		if (fd < 0)
1539a5ab8bfSQuentin Monnet 			p_err("get by id (%u): %s", id, strerror(errno));
15471bb428fSJakub Kicinski 		return fd;
15571bb428fSJakub Kicinski 	} else if (is_prefix(**argv, "tag")) {
15671bb428fSJakub Kicinski 		unsigned char tag[BPF_TAG_SIZE];
15771bb428fSJakub Kicinski 
15871bb428fSJakub Kicinski 		NEXT_ARGP();
15971bb428fSJakub Kicinski 
16071bb428fSJakub Kicinski 		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
16171bb428fSJakub Kicinski 			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
16271bb428fSJakub Kicinski 		    != BPF_TAG_SIZE) {
1639a5ab8bfSQuentin Monnet 			p_err("can't parse tag");
16471bb428fSJakub Kicinski 			return -1;
16571bb428fSJakub Kicinski 		}
16671bb428fSJakub Kicinski 		NEXT_ARGP();
16771bb428fSJakub Kicinski 
16871bb428fSJakub Kicinski 		return prog_fd_by_tag(tag);
16971bb428fSJakub Kicinski 	} else if (is_prefix(**argv, "pinned")) {
17071bb428fSJakub Kicinski 		char *path;
17171bb428fSJakub Kicinski 
17271bb428fSJakub Kicinski 		NEXT_ARGP();
17371bb428fSJakub Kicinski 
17471bb428fSJakub Kicinski 		path = **argv;
17571bb428fSJakub Kicinski 		NEXT_ARGP();
17671bb428fSJakub Kicinski 
17771bb428fSJakub Kicinski 		return open_obj_pinned_any(path, BPF_OBJ_PROG);
17871bb428fSJakub Kicinski 	}
17971bb428fSJakub Kicinski 
1809a5ab8bfSQuentin Monnet 	p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
18171bb428fSJakub Kicinski 	return -1;
18271bb428fSJakub Kicinski }
18371bb428fSJakub Kicinski 
18471bb428fSJakub Kicinski static void show_prog_maps(int fd, u32 num_maps)
18571bb428fSJakub Kicinski {
18671bb428fSJakub Kicinski 	struct bpf_prog_info info = {};
18771bb428fSJakub Kicinski 	__u32 len = sizeof(info);
18871bb428fSJakub Kicinski 	__u32 map_ids[num_maps];
18971bb428fSJakub Kicinski 	unsigned int i;
19071bb428fSJakub Kicinski 	int err;
19171bb428fSJakub Kicinski 
19271bb428fSJakub Kicinski 	info.nr_map_ids = num_maps;
19371bb428fSJakub Kicinski 	info.map_ids = ptr_to_u64(map_ids);
19471bb428fSJakub Kicinski 
19571bb428fSJakub Kicinski 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
19671bb428fSJakub Kicinski 	if (err || !info.nr_map_ids)
19771bb428fSJakub Kicinski 		return;
19871bb428fSJakub Kicinski 
199743cc665SQuentin Monnet 	if (json_output) {
200743cc665SQuentin Monnet 		jsonw_name(json_wtr, "map_ids");
201743cc665SQuentin Monnet 		jsonw_start_array(json_wtr);
202743cc665SQuentin Monnet 		for (i = 0; i < info.nr_map_ids; i++)
203743cc665SQuentin Monnet 			jsonw_uint(json_wtr, map_ids[i]);
204743cc665SQuentin Monnet 		jsonw_end_array(json_wtr);
205743cc665SQuentin Monnet 	} else {
20671bb428fSJakub Kicinski 		printf("  map_ids ");
20771bb428fSJakub Kicinski 		for (i = 0; i < info.nr_map_ids; i++)
20871bb428fSJakub Kicinski 			printf("%u%s", map_ids[i],
20971bb428fSJakub Kicinski 			       i == info.nr_map_ids - 1 ? "" : ",");
21071bb428fSJakub Kicinski 	}
21171bb428fSJakub Kicinski }
21271bb428fSJakub Kicinski 
213743cc665SQuentin Monnet static void print_prog_json(struct bpf_prog_info *info, int fd)
214743cc665SQuentin Monnet {
215743cc665SQuentin Monnet 	char *memlock;
216743cc665SQuentin Monnet 
217743cc665SQuentin Monnet 	jsonw_start_object(json_wtr);
218743cc665SQuentin Monnet 	jsonw_uint_field(json_wtr, "id", info->id);
219743cc665SQuentin Monnet 	if (info->type < ARRAY_SIZE(prog_type_name))
220743cc665SQuentin Monnet 		jsonw_string_field(json_wtr, "type",
221743cc665SQuentin Monnet 				   prog_type_name[info->type]);
22271bb428fSJakub Kicinski 	else
223743cc665SQuentin Monnet 		jsonw_uint_field(json_wtr, "type", info->type);
22471bb428fSJakub Kicinski 
225743cc665SQuentin Monnet 	if (*info->name)
226743cc665SQuentin Monnet 		jsonw_string_field(json_wtr, "name", info->name);
22771bb428fSJakub Kicinski 
228743cc665SQuentin Monnet 	jsonw_name(json_wtr, "tag");
229743cc665SQuentin Monnet 	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
230743cc665SQuentin Monnet 		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
231743cc665SQuentin Monnet 		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
23271bb428fSJakub Kicinski 
233743cc665SQuentin Monnet 	if (info->load_time) {
23471bb428fSJakub Kicinski 		char buf[32];
23571bb428fSJakub Kicinski 
236743cc665SQuentin Monnet 		print_boot_time(info->load_time, buf, sizeof(buf));
23771bb428fSJakub Kicinski 
23871bb428fSJakub Kicinski 		/* Piggy back on load_time, since 0 uid is a valid one */
239743cc665SQuentin Monnet 		jsonw_string_field(json_wtr, "loaded_at", buf);
240743cc665SQuentin Monnet 		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
24171bb428fSJakub Kicinski 	}
24271bb428fSJakub Kicinski 
243743cc665SQuentin Monnet 	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
24471bb428fSJakub Kicinski 
245743cc665SQuentin Monnet 	if (info->jited_prog_len) {
246743cc665SQuentin Monnet 		jsonw_bool_field(json_wtr, "jited", true);
247743cc665SQuentin Monnet 		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
248743cc665SQuentin Monnet 	} else {
249743cc665SQuentin Monnet 		jsonw_bool_field(json_wtr, "jited", false);
250743cc665SQuentin Monnet 	}
251743cc665SQuentin Monnet 
252743cc665SQuentin Monnet 	memlock = get_fdinfo(fd, "memlock");
253743cc665SQuentin Monnet 	if (memlock)
254743cc665SQuentin Monnet 		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
255743cc665SQuentin Monnet 	free(memlock);
256743cc665SQuentin Monnet 
257743cc665SQuentin Monnet 	if (info->nr_map_ids)
258743cc665SQuentin Monnet 		show_prog_maps(fd, info->nr_map_ids);
259743cc665SQuentin Monnet 
2604990f1f4SPrashant Bhole 	if (!hash_empty(prog_table.table)) {
2614990f1f4SPrashant Bhole 		struct pinned_obj *obj;
2624990f1f4SPrashant Bhole 
2634990f1f4SPrashant Bhole 		jsonw_name(json_wtr, "pinned");
2644990f1f4SPrashant Bhole 		jsonw_start_array(json_wtr);
2654990f1f4SPrashant Bhole 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
2664990f1f4SPrashant Bhole 			if (obj->id == info->id)
2674990f1f4SPrashant Bhole 				jsonw_string(json_wtr, obj->path);
2684990f1f4SPrashant Bhole 		}
2694990f1f4SPrashant Bhole 		jsonw_end_array(json_wtr);
2704990f1f4SPrashant Bhole 	}
2714990f1f4SPrashant Bhole 
272743cc665SQuentin Monnet 	jsonw_end_object(json_wtr);
273743cc665SQuentin Monnet }
274743cc665SQuentin Monnet 
275743cc665SQuentin Monnet static void print_prog_plain(struct bpf_prog_info *info, int fd)
276743cc665SQuentin Monnet {
277743cc665SQuentin Monnet 	char *memlock;
278743cc665SQuentin Monnet 
279743cc665SQuentin Monnet 	printf("%u: ", info->id);
280743cc665SQuentin Monnet 	if (info->type < ARRAY_SIZE(prog_type_name))
281743cc665SQuentin Monnet 		printf("%s  ", prog_type_name[info->type]);
282743cc665SQuentin Monnet 	else
283743cc665SQuentin Monnet 		printf("type %u  ", info->type);
284743cc665SQuentin Monnet 
285743cc665SQuentin Monnet 	if (*info->name)
286743cc665SQuentin Monnet 		printf("name %s  ", info->name);
287743cc665SQuentin Monnet 
288743cc665SQuentin Monnet 	printf("tag ");
289743cc665SQuentin Monnet 	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
290743cc665SQuentin Monnet 	printf("\n");
291743cc665SQuentin Monnet 
292743cc665SQuentin Monnet 	if (info->load_time) {
293743cc665SQuentin Monnet 		char buf[32];
294743cc665SQuentin Monnet 
295743cc665SQuentin Monnet 		print_boot_time(info->load_time, buf, sizeof(buf));
296743cc665SQuentin Monnet 
297743cc665SQuentin Monnet 		/* Piggy back on load_time, since 0 uid is a valid one */
298743cc665SQuentin Monnet 		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
299743cc665SQuentin Monnet 	}
300743cc665SQuentin Monnet 
301743cc665SQuentin Monnet 	printf("\txlated %uB", info->xlated_prog_len);
302743cc665SQuentin Monnet 
303743cc665SQuentin Monnet 	if (info->jited_prog_len)
304743cc665SQuentin Monnet 		printf("  jited %uB", info->jited_prog_len);
30571bb428fSJakub Kicinski 	else
30671bb428fSJakub Kicinski 		printf("  not jited");
30771bb428fSJakub Kicinski 
30871bb428fSJakub Kicinski 	memlock = get_fdinfo(fd, "memlock");
30971bb428fSJakub Kicinski 	if (memlock)
31071bb428fSJakub Kicinski 		printf("  memlock %sB", memlock);
31171bb428fSJakub Kicinski 	free(memlock);
31271bb428fSJakub Kicinski 
313743cc665SQuentin Monnet 	if (info->nr_map_ids)
314743cc665SQuentin Monnet 		show_prog_maps(fd, info->nr_map_ids);
31571bb428fSJakub Kicinski 
3164990f1f4SPrashant Bhole 	if (!hash_empty(prog_table.table)) {
3174990f1f4SPrashant Bhole 		struct pinned_obj *obj;
3184990f1f4SPrashant Bhole 
3194990f1f4SPrashant Bhole 		printf("\n");
3204990f1f4SPrashant Bhole 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
3214990f1f4SPrashant Bhole 			if (obj->id == info->id)
3224990f1f4SPrashant Bhole 				printf("\tpinned %s\n", obj->path);
3234990f1f4SPrashant Bhole 		}
3244990f1f4SPrashant Bhole 	}
3254990f1f4SPrashant Bhole 
32671bb428fSJakub Kicinski 	printf("\n");
327743cc665SQuentin Monnet }
328743cc665SQuentin Monnet 
329743cc665SQuentin Monnet static int show_prog(int fd)
330743cc665SQuentin Monnet {
331743cc665SQuentin Monnet 	struct bpf_prog_info info = {};
332743cc665SQuentin Monnet 	__u32 len = sizeof(info);
333743cc665SQuentin Monnet 	int err;
334743cc665SQuentin Monnet 
335743cc665SQuentin Monnet 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
336743cc665SQuentin Monnet 	if (err) {
3379a5ab8bfSQuentin Monnet 		p_err("can't get prog info: %s", strerror(errno));
338743cc665SQuentin Monnet 		return -1;
339743cc665SQuentin Monnet 	}
340743cc665SQuentin Monnet 
341743cc665SQuentin Monnet 	if (json_output)
342743cc665SQuentin Monnet 		print_prog_json(&info, fd);
343743cc665SQuentin Monnet 	else
344743cc665SQuentin Monnet 		print_prog_plain(&info, fd);
34571bb428fSJakub Kicinski 
34671bb428fSJakub Kicinski 	return 0;
34771bb428fSJakub Kicinski }
34871bb428fSJakub Kicinski 
34971bb428fSJakub Kicinski static int do_show(int argc, char **argv)
350743cc665SQuentin Monnet {
351743cc665SQuentin Monnet 	__u32 id = 0;
35271bb428fSJakub Kicinski 	int err;
35371bb428fSJakub Kicinski 	int fd;
35471bb428fSJakub Kicinski 
355c541b734SPrashant Bhole 	if (show_pinned)
3564990f1f4SPrashant Bhole 		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
3574990f1f4SPrashant Bhole 
35871bb428fSJakub Kicinski 	if (argc == 2) {
35971bb428fSJakub Kicinski 		fd = prog_parse_fd(&argc, &argv);
36071bb428fSJakub Kicinski 		if (fd < 0)
36171bb428fSJakub Kicinski 			return -1;
36271bb428fSJakub Kicinski 
36371bb428fSJakub Kicinski 		return show_prog(fd);
36471bb428fSJakub Kicinski 	}
36571bb428fSJakub Kicinski 
36671bb428fSJakub Kicinski 	if (argc)
36771bb428fSJakub Kicinski 		return BAD_ARG();
36871bb428fSJakub Kicinski 
369743cc665SQuentin Monnet 	if (json_output)
370743cc665SQuentin Monnet 		jsonw_start_array(json_wtr);
37171bb428fSJakub Kicinski 	while (true) {
37271bb428fSJakub Kicinski 		err = bpf_prog_get_next_id(id, &id);
37371bb428fSJakub Kicinski 		if (err) {
3741739c26dSQuentin Monnet 			if (errno == ENOENT) {
3751739c26dSQuentin Monnet 				err = 0;
37671bb428fSJakub Kicinski 				break;
3771739c26dSQuentin Monnet 			}
3789a5ab8bfSQuentin Monnet 			p_err("can't get next program: %s%s", strerror(errno),
3799a5ab8bfSQuentin Monnet 			      errno == EINVAL ? " -- kernel too old?" : "");
380743cc665SQuentin Monnet 			err = -1;
381743cc665SQuentin Monnet 			break;
38271bb428fSJakub Kicinski 		}
38371bb428fSJakub Kicinski 
38471bb428fSJakub Kicinski 		fd = bpf_prog_get_fd_by_id(id);
38571bb428fSJakub Kicinski 		if (fd < 0) {
3869a5ab8bfSQuentin Monnet 			p_err("can't get prog by id (%u): %s",
38771bb428fSJakub Kicinski 			      id, strerror(errno));
388743cc665SQuentin Monnet 			err = -1;
389743cc665SQuentin Monnet 			break;
39071bb428fSJakub Kicinski 		}
39171bb428fSJakub Kicinski 
39271bb428fSJakub Kicinski 		err = show_prog(fd);
39371bb428fSJakub Kicinski 		close(fd);
39471bb428fSJakub Kicinski 		if (err)
395743cc665SQuentin Monnet 			break;
39671bb428fSJakub Kicinski 	}
39771bb428fSJakub Kicinski 
398743cc665SQuentin Monnet 	if (json_output)
399743cc665SQuentin Monnet 		jsonw_end_array(json_wtr);
400743cc665SQuentin Monnet 
401743cc665SQuentin Monnet 	return err;
40271bb428fSJakub Kicinski }
40371bb428fSJakub Kicinski 
404*7105e828SDaniel Borkmann #define SYM_MAX_NAME	256
405*7105e828SDaniel Borkmann 
406*7105e828SDaniel Borkmann struct kernel_sym {
407*7105e828SDaniel Borkmann 	unsigned long address;
408*7105e828SDaniel Borkmann 	char name[SYM_MAX_NAME];
409*7105e828SDaniel Borkmann };
410*7105e828SDaniel Borkmann 
411*7105e828SDaniel Borkmann struct dump_data {
412*7105e828SDaniel Borkmann 	unsigned long address_call_base;
413*7105e828SDaniel Borkmann 	struct kernel_sym *sym_mapping;
414*7105e828SDaniel Borkmann 	__u32 sym_count;
415*7105e828SDaniel Borkmann 	char scratch_buff[SYM_MAX_NAME];
416*7105e828SDaniel Borkmann };
417*7105e828SDaniel Borkmann 
418*7105e828SDaniel Borkmann static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
419*7105e828SDaniel Borkmann {
420*7105e828SDaniel Borkmann 	return ((struct kernel_sym *)sym_a)->address -
421*7105e828SDaniel Borkmann 	       ((struct kernel_sym *)sym_b)->address;
422*7105e828SDaniel Borkmann }
423*7105e828SDaniel Borkmann 
424*7105e828SDaniel Borkmann static void kernel_syms_load(struct dump_data *dd)
425*7105e828SDaniel Borkmann {
426*7105e828SDaniel Borkmann 	struct kernel_sym *sym;
427*7105e828SDaniel Borkmann 	char buff[256];
428*7105e828SDaniel Borkmann 	void *tmp, *address;
429*7105e828SDaniel Borkmann 	FILE *fp;
430*7105e828SDaniel Borkmann 
431*7105e828SDaniel Borkmann 	fp = fopen("/proc/kallsyms", "r");
432*7105e828SDaniel Borkmann 	if (!fp)
433*7105e828SDaniel Borkmann 		return;
434*7105e828SDaniel Borkmann 
435*7105e828SDaniel Borkmann 	while (!feof(fp)) {
436*7105e828SDaniel Borkmann 		if (!fgets(buff, sizeof(buff), fp))
437*7105e828SDaniel Borkmann 			break;
438*7105e828SDaniel Borkmann 		tmp = realloc(dd->sym_mapping,
439*7105e828SDaniel Borkmann 			      (dd->sym_count + 1) *
440*7105e828SDaniel Borkmann 			      sizeof(*dd->sym_mapping));
441*7105e828SDaniel Borkmann 		if (!tmp) {
442*7105e828SDaniel Borkmann out:
443*7105e828SDaniel Borkmann 			free(dd->sym_mapping);
444*7105e828SDaniel Borkmann 			dd->sym_mapping = NULL;
445*7105e828SDaniel Borkmann 			fclose(fp);
446*7105e828SDaniel Borkmann 			return;
447*7105e828SDaniel Borkmann 		}
448*7105e828SDaniel Borkmann 		dd->sym_mapping = tmp;
449*7105e828SDaniel Borkmann 		sym = &dd->sym_mapping[dd->sym_count];
450*7105e828SDaniel Borkmann 		if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
451*7105e828SDaniel Borkmann 			continue;
452*7105e828SDaniel Borkmann 		sym->address = (unsigned long)address;
453*7105e828SDaniel Borkmann 		if (!strcmp(sym->name, "__bpf_call_base")) {
454*7105e828SDaniel Borkmann 			dd->address_call_base = sym->address;
455*7105e828SDaniel Borkmann 			/* sysctl kernel.kptr_restrict was set */
456*7105e828SDaniel Borkmann 			if (!sym->address)
457*7105e828SDaniel Borkmann 				goto out;
458*7105e828SDaniel Borkmann 		}
459*7105e828SDaniel Borkmann 		if (sym->address)
460*7105e828SDaniel Borkmann 			dd->sym_count++;
461*7105e828SDaniel Borkmann 	}
462*7105e828SDaniel Borkmann 
463*7105e828SDaniel Borkmann 	fclose(fp);
464*7105e828SDaniel Borkmann 
465*7105e828SDaniel Borkmann 	qsort(dd->sym_mapping, dd->sym_count,
466*7105e828SDaniel Borkmann 	      sizeof(*dd->sym_mapping), kernel_syms_cmp);
467*7105e828SDaniel Borkmann }
468*7105e828SDaniel Borkmann 
469*7105e828SDaniel Borkmann static void kernel_syms_destroy(struct dump_data *dd)
470*7105e828SDaniel Borkmann {
471*7105e828SDaniel Borkmann 	free(dd->sym_mapping);
472*7105e828SDaniel Borkmann }
473*7105e828SDaniel Borkmann 
474*7105e828SDaniel Borkmann static struct kernel_sym *kernel_syms_search(struct dump_data *dd,
475*7105e828SDaniel Borkmann 					     unsigned long key)
476*7105e828SDaniel Borkmann {
477*7105e828SDaniel Borkmann 	struct kernel_sym sym = {
478*7105e828SDaniel Borkmann 		.address = key,
479*7105e828SDaniel Borkmann 	};
480*7105e828SDaniel Borkmann 
481*7105e828SDaniel Borkmann 	return dd->sym_mapping ?
482*7105e828SDaniel Borkmann 	       bsearch(&sym, dd->sym_mapping, dd->sym_count,
483*7105e828SDaniel Borkmann 		       sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
484*7105e828SDaniel Borkmann }
485*7105e828SDaniel Borkmann 
486c9c35995SJakub Kicinski static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
487c9c35995SJakub Kicinski {
488c9c35995SJakub Kicinski 	va_list args;
489c9c35995SJakub Kicinski 
490c9c35995SJakub Kicinski 	va_start(args, fmt);
491c9c35995SJakub Kicinski 	vprintf(fmt, args);
492c9c35995SJakub Kicinski 	va_end(args);
493c9c35995SJakub Kicinski }
494c9c35995SJakub Kicinski 
495*7105e828SDaniel Borkmann static const char *print_call_pcrel(struct dump_data *dd,
496*7105e828SDaniel Borkmann 				    struct kernel_sym *sym,
497*7105e828SDaniel Borkmann 				    unsigned long address,
498*7105e828SDaniel Borkmann 				    const struct bpf_insn *insn)
499c9c35995SJakub Kicinski {
500*7105e828SDaniel Borkmann 	if (sym)
501*7105e828SDaniel Borkmann 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
502*7105e828SDaniel Borkmann 			 "%+d#%s", insn->off, sym->name);
503*7105e828SDaniel Borkmann 	else
504*7105e828SDaniel Borkmann 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
505*7105e828SDaniel Borkmann 			 "%+d#0x%lx", insn->off, address);
506*7105e828SDaniel Borkmann 	return dd->scratch_buff;
507*7105e828SDaniel Borkmann }
508*7105e828SDaniel Borkmann 
509*7105e828SDaniel Borkmann static const char *print_call_helper(struct dump_data *dd,
510*7105e828SDaniel Borkmann 				     struct kernel_sym *sym,
511*7105e828SDaniel Borkmann 				     unsigned long address)
512*7105e828SDaniel Borkmann {
513*7105e828SDaniel Borkmann 	if (sym)
514*7105e828SDaniel Borkmann 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
515*7105e828SDaniel Borkmann 			 "%s", sym->name);
516*7105e828SDaniel Borkmann 	else
517*7105e828SDaniel Borkmann 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
518*7105e828SDaniel Borkmann 			 "0x%lx", address);
519*7105e828SDaniel Borkmann 	return dd->scratch_buff;
520*7105e828SDaniel Borkmann }
521*7105e828SDaniel Borkmann 
522*7105e828SDaniel Borkmann static const char *print_call(void *private_data,
523*7105e828SDaniel Borkmann 			      const struct bpf_insn *insn)
524*7105e828SDaniel Borkmann {
525*7105e828SDaniel Borkmann 	struct dump_data *dd = private_data;
526*7105e828SDaniel Borkmann 	unsigned long address = dd->address_call_base + insn->imm;
527*7105e828SDaniel Borkmann 	struct kernel_sym *sym;
528*7105e828SDaniel Borkmann 
529*7105e828SDaniel Borkmann 	sym = kernel_syms_search(dd, address);
530*7105e828SDaniel Borkmann 	if (insn->src_reg == BPF_PSEUDO_CALL)
531*7105e828SDaniel Borkmann 		return print_call_pcrel(dd, sym, address, insn);
532*7105e828SDaniel Borkmann 	else
533*7105e828SDaniel Borkmann 		return print_call_helper(dd, sym, address);
534*7105e828SDaniel Borkmann }
535*7105e828SDaniel Borkmann 
536*7105e828SDaniel Borkmann static const char *print_imm(void *private_data,
537*7105e828SDaniel Borkmann 			     const struct bpf_insn *insn,
538*7105e828SDaniel Borkmann 			     __u64 full_imm)
539*7105e828SDaniel Borkmann {
540*7105e828SDaniel Borkmann 	struct dump_data *dd = private_data;
541*7105e828SDaniel Borkmann 
542*7105e828SDaniel Borkmann 	if (insn->src_reg == BPF_PSEUDO_MAP_FD)
543*7105e828SDaniel Borkmann 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
544*7105e828SDaniel Borkmann 			 "map[id:%u]", insn->imm);
545*7105e828SDaniel Borkmann 	else
546*7105e828SDaniel Borkmann 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
547*7105e828SDaniel Borkmann 			 "0x%llx", (unsigned long long)full_imm);
548*7105e828SDaniel Borkmann 	return dd->scratch_buff;
549*7105e828SDaniel Borkmann }
550*7105e828SDaniel Borkmann 
551*7105e828SDaniel Borkmann static void dump_xlated_plain(struct dump_data *dd, void *buf,
552*7105e828SDaniel Borkmann 			      unsigned int len, bool opcodes)
553*7105e828SDaniel Borkmann {
554*7105e828SDaniel Borkmann 	const struct bpf_insn_cbs cbs = {
555*7105e828SDaniel Borkmann 		.cb_print	= print_insn,
556*7105e828SDaniel Borkmann 		.cb_call	= print_call,
557*7105e828SDaniel Borkmann 		.cb_imm		= print_imm,
558*7105e828SDaniel Borkmann 		.private_data	= dd,
559*7105e828SDaniel Borkmann 	};
560c9c35995SJakub Kicinski 	struct bpf_insn *insn = buf;
5619e2308c1SQuentin Monnet 	bool double_insn = false;
562c9c35995SJakub Kicinski 	unsigned int i;
563c9c35995SJakub Kicinski 
564c9c35995SJakub Kicinski 	for (i = 0; i < len / sizeof(*insn); i++) {
5659e2308c1SQuentin Monnet 		if (double_insn) {
5669e2308c1SQuentin Monnet 			double_insn = false;
5679e2308c1SQuentin Monnet 			continue;
5689e2308c1SQuentin Monnet 		}
5699e2308c1SQuentin Monnet 
5709e2308c1SQuentin Monnet 		double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
5719e2308c1SQuentin Monnet 
572c9c35995SJakub Kicinski 		printf("% 4d: ", i);
573*7105e828SDaniel Borkmann 		print_bpf_insn(&cbs, NULL, insn + i, true);
574c9c35995SJakub Kicinski 
575c9c35995SJakub Kicinski 		if (opcodes) {
576c9c35995SJakub Kicinski 			printf("       ");
5779cbe1f58SQuentin Monnet 			fprint_hex(stdout, insn + i, 8, " ");
5789e2308c1SQuentin Monnet 			if (double_insn && i < len - 1) {
5799e2308c1SQuentin Monnet 				printf(" ");
5809e2308c1SQuentin Monnet 				fprint_hex(stdout, insn + i + 1, 8, " ");
5819e2308c1SQuentin Monnet 			}
582c9c35995SJakub Kicinski 			printf("\n");
583c9c35995SJakub Kicinski 		}
584c9c35995SJakub Kicinski 	}
585c9c35995SJakub Kicinski }
586c9c35995SJakub Kicinski 
587f05e2c32SQuentin Monnet static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
588f05e2c32SQuentin Monnet {
589f05e2c32SQuentin Monnet 	unsigned int l = strlen(fmt);
590f05e2c32SQuentin Monnet 	char chomped_fmt[l];
591f05e2c32SQuentin Monnet 	va_list args;
592f05e2c32SQuentin Monnet 
593f05e2c32SQuentin Monnet 	va_start(args, fmt);
594f05e2c32SQuentin Monnet 	if (l > 0) {
595f05e2c32SQuentin Monnet 		strncpy(chomped_fmt, fmt, l - 1);
596f05e2c32SQuentin Monnet 		chomped_fmt[l - 1] = '\0';
597f05e2c32SQuentin Monnet 	}
598f05e2c32SQuentin Monnet 	jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
599f05e2c32SQuentin Monnet 	va_end(args);
600f05e2c32SQuentin Monnet }
601f05e2c32SQuentin Monnet 
602*7105e828SDaniel Borkmann static void dump_xlated_json(struct dump_data *dd, void *buf,
603*7105e828SDaniel Borkmann 			     unsigned int len, bool opcodes)
604f05e2c32SQuentin Monnet {
605*7105e828SDaniel Borkmann 	const struct bpf_insn_cbs cbs = {
606*7105e828SDaniel Borkmann 		.cb_print	= print_insn_json,
607*7105e828SDaniel Borkmann 		.cb_call	= print_call,
608*7105e828SDaniel Borkmann 		.cb_imm		= print_imm,
609*7105e828SDaniel Borkmann 		.private_data	= dd,
610*7105e828SDaniel Borkmann 	};
611f05e2c32SQuentin Monnet 	struct bpf_insn *insn = buf;
612f05e2c32SQuentin Monnet 	bool double_insn = false;
613f05e2c32SQuentin Monnet 	unsigned int i;
614f05e2c32SQuentin Monnet 
615f05e2c32SQuentin Monnet 	jsonw_start_array(json_wtr);
616f05e2c32SQuentin Monnet 	for (i = 0; i < len / sizeof(*insn); i++) {
617f05e2c32SQuentin Monnet 		if (double_insn) {
618f05e2c32SQuentin Monnet 			double_insn = false;
619f05e2c32SQuentin Monnet 			continue;
620f05e2c32SQuentin Monnet 		}
621f05e2c32SQuentin Monnet 		double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
622f05e2c32SQuentin Monnet 
623f05e2c32SQuentin Monnet 		jsonw_start_object(json_wtr);
624f05e2c32SQuentin Monnet 		jsonw_name(json_wtr, "disasm");
625*7105e828SDaniel Borkmann 		print_bpf_insn(&cbs, NULL, insn + i, true);
626f05e2c32SQuentin Monnet 
627f05e2c32SQuentin Monnet 		if (opcodes) {
628f05e2c32SQuentin Monnet 			jsonw_name(json_wtr, "opcodes");
629f05e2c32SQuentin Monnet 			jsonw_start_object(json_wtr);
630f05e2c32SQuentin Monnet 
631f05e2c32SQuentin Monnet 			jsonw_name(json_wtr, "code");
632f05e2c32SQuentin Monnet 			jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
633f05e2c32SQuentin Monnet 
634f05e2c32SQuentin Monnet 			jsonw_name(json_wtr, "src_reg");
635f05e2c32SQuentin Monnet 			jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
636f05e2c32SQuentin Monnet 
637f05e2c32SQuentin Monnet 			jsonw_name(json_wtr, "dst_reg");
638f05e2c32SQuentin Monnet 			jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
639f05e2c32SQuentin Monnet 
640f05e2c32SQuentin Monnet 			jsonw_name(json_wtr, "off");
641f05e2c32SQuentin Monnet 			print_hex_data_json((uint8_t *)(&insn[i].off), 2);
642f05e2c32SQuentin Monnet 
643f05e2c32SQuentin Monnet 			jsonw_name(json_wtr, "imm");
644f05e2c32SQuentin Monnet 			if (double_insn && i < len - 1)
645f05e2c32SQuentin Monnet 				print_hex_data_json((uint8_t *)(&insn[i].imm),
646f05e2c32SQuentin Monnet 						    12);
647f05e2c32SQuentin Monnet 			else
648f05e2c32SQuentin Monnet 				print_hex_data_json((uint8_t *)(&insn[i].imm),
649f05e2c32SQuentin Monnet 						    4);
650f05e2c32SQuentin Monnet 			jsonw_end_object(json_wtr);
651f05e2c32SQuentin Monnet 		}
652f05e2c32SQuentin Monnet 		jsonw_end_object(json_wtr);
653f05e2c32SQuentin Monnet 	}
654f05e2c32SQuentin Monnet 	jsonw_end_array(json_wtr);
655f05e2c32SQuentin Monnet }
656f05e2c32SQuentin Monnet 
65771bb428fSJakub Kicinski static int do_dump(int argc, char **argv)
65871bb428fSJakub Kicinski {
65971bb428fSJakub Kicinski 	struct bpf_prog_info info = {};
660*7105e828SDaniel Borkmann 	struct dump_data dd = {};
66171bb428fSJakub Kicinski 	__u32 len = sizeof(info);
66271bb428fSJakub Kicinski 	unsigned int buf_size;
66371bb428fSJakub Kicinski 	char *filepath = NULL;
66471bb428fSJakub Kicinski 	bool opcodes = false;
66571bb428fSJakub Kicinski 	unsigned char *buf;
66671bb428fSJakub Kicinski 	__u32 *member_len;
66771bb428fSJakub Kicinski 	__u64 *member_ptr;
66871bb428fSJakub Kicinski 	ssize_t n;
66971bb428fSJakub Kicinski 	int err;
67071bb428fSJakub Kicinski 	int fd;
67171bb428fSJakub Kicinski 
67271bb428fSJakub Kicinski 	if (is_prefix(*argv, "jited")) {
67371bb428fSJakub Kicinski 		member_len = &info.jited_prog_len;
67471bb428fSJakub Kicinski 		member_ptr = &info.jited_prog_insns;
67571bb428fSJakub Kicinski 	} else if (is_prefix(*argv, "xlated")) {
67671bb428fSJakub Kicinski 		member_len = &info.xlated_prog_len;
67771bb428fSJakub Kicinski 		member_ptr = &info.xlated_prog_insns;
67871bb428fSJakub Kicinski 	} else {
6799a5ab8bfSQuentin Monnet 		p_err("expected 'xlated' or 'jited', got: %s", *argv);
68071bb428fSJakub Kicinski 		return -1;
68171bb428fSJakub Kicinski 	}
68271bb428fSJakub Kicinski 	NEXT_ARG();
68371bb428fSJakub Kicinski 
68471bb428fSJakub Kicinski 	if (argc < 2)
68571bb428fSJakub Kicinski 		usage();
68671bb428fSJakub Kicinski 
68771bb428fSJakub Kicinski 	fd = prog_parse_fd(&argc, &argv);
68871bb428fSJakub Kicinski 	if (fd < 0)
68971bb428fSJakub Kicinski 		return -1;
69071bb428fSJakub Kicinski 
69171bb428fSJakub Kicinski 	if (is_prefix(*argv, "file")) {
69271bb428fSJakub Kicinski 		NEXT_ARG();
69371bb428fSJakub Kicinski 		if (!argc) {
6949a5ab8bfSQuentin Monnet 			p_err("expected file path");
69571bb428fSJakub Kicinski 			return -1;
69671bb428fSJakub Kicinski 		}
69771bb428fSJakub Kicinski 
69871bb428fSJakub Kicinski 		filepath = *argv;
69971bb428fSJakub Kicinski 		NEXT_ARG();
70071bb428fSJakub Kicinski 	} else if (is_prefix(*argv, "opcodes")) {
70171bb428fSJakub Kicinski 		opcodes = true;
70271bb428fSJakub Kicinski 		NEXT_ARG();
70371bb428fSJakub Kicinski 	}
70471bb428fSJakub Kicinski 
70571bb428fSJakub Kicinski 	if (argc) {
70671bb428fSJakub Kicinski 		usage();
70771bb428fSJakub Kicinski 		return -1;
70871bb428fSJakub Kicinski 	}
70971bb428fSJakub Kicinski 
71071bb428fSJakub Kicinski 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
71171bb428fSJakub Kicinski 	if (err) {
7129a5ab8bfSQuentin Monnet 		p_err("can't get prog info: %s", strerror(errno));
71371bb428fSJakub Kicinski 		return -1;
71471bb428fSJakub Kicinski 	}
71571bb428fSJakub Kicinski 
71671bb428fSJakub Kicinski 	if (!*member_len) {
7179a5ab8bfSQuentin Monnet 		p_info("no instructions returned");
71871bb428fSJakub Kicinski 		close(fd);
71971bb428fSJakub Kicinski 		return 0;
72071bb428fSJakub Kicinski 	}
72171bb428fSJakub Kicinski 
72271bb428fSJakub Kicinski 	buf_size = *member_len;
72371bb428fSJakub Kicinski 
72471bb428fSJakub Kicinski 	buf = malloc(buf_size);
72571bb428fSJakub Kicinski 	if (!buf) {
7269a5ab8bfSQuentin Monnet 		p_err("mem alloc failed");
72771bb428fSJakub Kicinski 		close(fd);
72871bb428fSJakub Kicinski 		return -1;
72971bb428fSJakub Kicinski 	}
73071bb428fSJakub Kicinski 
73171bb428fSJakub Kicinski 	memset(&info, 0, sizeof(info));
73271bb428fSJakub Kicinski 
73371bb428fSJakub Kicinski 	*member_ptr = ptr_to_u64(buf);
73471bb428fSJakub Kicinski 	*member_len = buf_size;
73571bb428fSJakub Kicinski 
73671bb428fSJakub Kicinski 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
73771bb428fSJakub Kicinski 	close(fd);
73871bb428fSJakub Kicinski 	if (err) {
7399a5ab8bfSQuentin Monnet 		p_err("can't get prog info: %s", strerror(errno));
74071bb428fSJakub Kicinski 		goto err_free;
74171bb428fSJakub Kicinski 	}
74271bb428fSJakub Kicinski 
74371bb428fSJakub Kicinski 	if (*member_len > buf_size) {
7449a5ab8bfSQuentin Monnet 		p_err("too many instructions returned");
74571bb428fSJakub Kicinski 		goto err_free;
74671bb428fSJakub Kicinski 	}
74771bb428fSJakub Kicinski 
748*7105e828SDaniel Borkmann 	if ((member_len == &info.jited_prog_len &&
749*7105e828SDaniel Borkmann 	     info.jited_prog_insns == 0) ||
750*7105e828SDaniel Borkmann 	    (member_len == &info.xlated_prog_len &&
751*7105e828SDaniel Borkmann 	     info.xlated_prog_insns == 0)) {
752*7105e828SDaniel Borkmann 		p_err("error retrieving insn dump: kernel.kptr_restrict set?");
753*7105e828SDaniel Borkmann 		goto err_free;
754*7105e828SDaniel Borkmann 	}
755*7105e828SDaniel Borkmann 
75671bb428fSJakub Kicinski 	if (filepath) {
75771bb428fSJakub Kicinski 		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
75871bb428fSJakub Kicinski 		if (fd < 0) {
7599a5ab8bfSQuentin Monnet 			p_err("can't open file %s: %s", filepath,
76071bb428fSJakub Kicinski 			      strerror(errno));
76171bb428fSJakub Kicinski 			goto err_free;
76271bb428fSJakub Kicinski 		}
76371bb428fSJakub Kicinski 
76471bb428fSJakub Kicinski 		n = write(fd, buf, *member_len);
76571bb428fSJakub Kicinski 		close(fd);
76671bb428fSJakub Kicinski 		if (n != *member_len) {
7679a5ab8bfSQuentin Monnet 			p_err("error writing output file: %s",
76871bb428fSJakub Kicinski 			      n < 0 ? strerror(errno) : "short write");
76971bb428fSJakub Kicinski 			goto err_free;
77071bb428fSJakub Kicinski 		}
77171bb428fSJakub Kicinski 	} else {
772*7105e828SDaniel Borkmann 		if (member_len == &info.jited_prog_len) {
77371bb428fSJakub Kicinski 			disasm_print_insn(buf, *member_len, opcodes);
774*7105e828SDaniel Borkmann 		} else {
775*7105e828SDaniel Borkmann 			kernel_syms_load(&dd);
776f05e2c32SQuentin Monnet 			if (json_output)
777*7105e828SDaniel Borkmann 				dump_xlated_json(&dd, buf, *member_len, opcodes);
778f05e2c32SQuentin Monnet 			else
779*7105e828SDaniel Borkmann 				dump_xlated_plain(&dd, buf, *member_len, opcodes);
780*7105e828SDaniel Borkmann 			kernel_syms_destroy(&dd);
781*7105e828SDaniel Borkmann 		}
78271bb428fSJakub Kicinski 	}
78371bb428fSJakub Kicinski 
78471bb428fSJakub Kicinski 	free(buf);
78571bb428fSJakub Kicinski 	return 0;
78671bb428fSJakub Kicinski 
78771bb428fSJakub Kicinski err_free:
78871bb428fSJakub Kicinski 	free(buf);
78971bb428fSJakub Kicinski 	return -1;
79071bb428fSJakub Kicinski }
79171bb428fSJakub Kicinski 
79271bb428fSJakub Kicinski static int do_pin(int argc, char **argv)
79371bb428fSJakub Kicinski {
794004b45c0SQuentin Monnet 	int err;
795004b45c0SQuentin Monnet 
796004b45c0SQuentin Monnet 	err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
797004b45c0SQuentin Monnet 	if (!err && json_output)
798004b45c0SQuentin Monnet 		jsonw_null(json_wtr);
799004b45c0SQuentin Monnet 	return err;
80071bb428fSJakub Kicinski }
80171bb428fSJakub Kicinski 
80249a086c2SRoman Gushchin static int do_load(int argc, char **argv)
80349a086c2SRoman Gushchin {
80449a086c2SRoman Gushchin 	struct bpf_object *obj;
80549a086c2SRoman Gushchin 	int prog_fd;
80649a086c2SRoman Gushchin 
80749a086c2SRoman Gushchin 	if (argc != 2)
80849a086c2SRoman Gushchin 		usage();
80949a086c2SRoman Gushchin 
81049a086c2SRoman Gushchin 	if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
81149a086c2SRoman Gushchin 		p_err("failed to load program\n");
81249a086c2SRoman Gushchin 		return -1;
81349a086c2SRoman Gushchin 	}
81449a086c2SRoman Gushchin 
81549a086c2SRoman Gushchin 	if (do_pin_fd(prog_fd, argv[1])) {
81649a086c2SRoman Gushchin 		p_err("failed to pin program\n");
81749a086c2SRoman Gushchin 		return -1;
81849a086c2SRoman Gushchin 	}
81949a086c2SRoman Gushchin 
82049a086c2SRoman Gushchin 	if (json_output)
82149a086c2SRoman Gushchin 		jsonw_null(json_wtr);
82249a086c2SRoman Gushchin 
82349a086c2SRoman Gushchin 	return 0;
82449a086c2SRoman Gushchin }
82549a086c2SRoman Gushchin 
82671bb428fSJakub Kicinski static int do_help(int argc, char **argv)
82771bb428fSJakub Kicinski {
828004b45c0SQuentin Monnet 	if (json_output) {
829004b45c0SQuentin Monnet 		jsonw_null(json_wtr);
830004b45c0SQuentin Monnet 		return 0;
831004b45c0SQuentin Monnet 	}
832004b45c0SQuentin Monnet 
83371bb428fSJakub Kicinski 	fprintf(stderr,
83471bb428fSJakub Kicinski 		"Usage: %s %s show [PROG]\n"
8358dfbc6d1SQuentin Monnet 		"       %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
8368dfbc6d1SQuentin Monnet 		"       %s %s dump jited  PROG [{ file FILE | opcodes }]\n"
83771bb428fSJakub Kicinski 		"       %s %s pin   PROG FILE\n"
83849a086c2SRoman Gushchin 		"       %s %s load  OBJ  FILE\n"
83971bb428fSJakub Kicinski 		"       %s %s help\n"
84071bb428fSJakub Kicinski 		"\n"
84171bb428fSJakub Kicinski 		"       " HELP_SPEC_PROGRAM "\n"
8420641c3c8SQuentin Monnet 		"       " HELP_SPEC_OPTIONS "\n"
84371bb428fSJakub Kicinski 		"",
84471bb428fSJakub Kicinski 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
84549a086c2SRoman Gushchin 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
84671bb428fSJakub Kicinski 
84771bb428fSJakub Kicinski 	return 0;
84871bb428fSJakub Kicinski }
84971bb428fSJakub Kicinski 
85071bb428fSJakub Kicinski static const struct cmd cmds[] = {
85171bb428fSJakub Kicinski 	{ "show",	do_show },
8529f606179SQuentin Monnet 	{ "help",	do_help },
85371bb428fSJakub Kicinski 	{ "dump",	do_dump },
85471bb428fSJakub Kicinski 	{ "pin",	do_pin },
85549a086c2SRoman Gushchin 	{ "load",	do_load },
85671bb428fSJakub Kicinski 	{ 0 }
85771bb428fSJakub Kicinski };
85871bb428fSJakub Kicinski 
85971bb428fSJakub Kicinski int do_prog(int argc, char **argv)
86071bb428fSJakub Kicinski {
86171bb428fSJakub Kicinski 	return cmd_select(cmds, argc, argv, do_help);
86271bb428fSJakub Kicinski }
863