xref: /openbmc/linux/tools/bpf/bpftool/prog.c (revision a5907065)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #define _GNU_SOURCE
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <net/if.h>
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/syscall.h>
19 
20 #include <linux/err.h>
21 #include <linux/perf_event.h>
22 #include <linux/sizes.h>
23 
24 #include <bpf/bpf.h>
25 #include <bpf/btf.h>
26 #include <bpf/libbpf.h>
27 
28 #include "cfg.h"
29 #include "main.h"
30 #include "xlated_dumper.h"
31 
32 #define BPF_METADATA_PREFIX "bpf_metadata_"
33 #define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1)
34 
35 const char * const prog_type_name[] = {
36 	[BPF_PROG_TYPE_UNSPEC]			= "unspec",
37 	[BPF_PROG_TYPE_SOCKET_FILTER]		= "socket_filter",
38 	[BPF_PROG_TYPE_KPROBE]			= "kprobe",
39 	[BPF_PROG_TYPE_SCHED_CLS]		= "sched_cls",
40 	[BPF_PROG_TYPE_SCHED_ACT]		= "sched_act",
41 	[BPF_PROG_TYPE_TRACEPOINT]		= "tracepoint",
42 	[BPF_PROG_TYPE_XDP]			= "xdp",
43 	[BPF_PROG_TYPE_PERF_EVENT]		= "perf_event",
44 	[BPF_PROG_TYPE_CGROUP_SKB]		= "cgroup_skb",
45 	[BPF_PROG_TYPE_CGROUP_SOCK]		= "cgroup_sock",
46 	[BPF_PROG_TYPE_LWT_IN]			= "lwt_in",
47 	[BPF_PROG_TYPE_LWT_OUT]			= "lwt_out",
48 	[BPF_PROG_TYPE_LWT_XMIT]		= "lwt_xmit",
49 	[BPF_PROG_TYPE_SOCK_OPS]		= "sock_ops",
50 	[BPF_PROG_TYPE_SK_SKB]			= "sk_skb",
51 	[BPF_PROG_TYPE_CGROUP_DEVICE]		= "cgroup_device",
52 	[BPF_PROG_TYPE_SK_MSG]			= "sk_msg",
53 	[BPF_PROG_TYPE_RAW_TRACEPOINT]		= "raw_tracepoint",
54 	[BPF_PROG_TYPE_CGROUP_SOCK_ADDR]	= "cgroup_sock_addr",
55 	[BPF_PROG_TYPE_LWT_SEG6LOCAL]		= "lwt_seg6local",
56 	[BPF_PROG_TYPE_LIRC_MODE2]		= "lirc_mode2",
57 	[BPF_PROG_TYPE_SK_REUSEPORT]		= "sk_reuseport",
58 	[BPF_PROG_TYPE_FLOW_DISSECTOR]		= "flow_dissector",
59 	[BPF_PROG_TYPE_CGROUP_SYSCTL]		= "cgroup_sysctl",
60 	[BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE]	= "raw_tracepoint_writable",
61 	[BPF_PROG_TYPE_CGROUP_SOCKOPT]		= "cgroup_sockopt",
62 	[BPF_PROG_TYPE_TRACING]			= "tracing",
63 	[BPF_PROG_TYPE_STRUCT_OPS]		= "struct_ops",
64 	[BPF_PROG_TYPE_EXT]			= "ext",
65 	[BPF_PROG_TYPE_LSM]			= "lsm",
66 	[BPF_PROG_TYPE_SK_LOOKUP]		= "sk_lookup",
67 };
68 
69 const size_t prog_type_name_size = ARRAY_SIZE(prog_type_name);
70 
71 enum dump_mode {
72 	DUMP_JITED,
73 	DUMP_XLATED,
74 };
75 
76 static const char * const attach_type_strings[] = {
77 	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
78 	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
79 	[BPF_SK_MSG_VERDICT] = "msg_verdict",
80 	[BPF_FLOW_DISSECTOR] = "flow_dissector",
81 	[__MAX_BPF_ATTACH_TYPE] = NULL,
82 };
83 
84 static enum bpf_attach_type parse_attach_type(const char *str)
85 {
86 	enum bpf_attach_type type;
87 
88 	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
89 		if (attach_type_strings[type] &&
90 		    is_prefix(str, attach_type_strings[type]))
91 			return type;
92 	}
93 
94 	return __MAX_BPF_ATTACH_TYPE;
95 }
96 
97 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
98 {
99 	struct timespec real_time_ts, boot_time_ts;
100 	time_t wallclock_secs;
101 	struct tm load_tm;
102 
103 	buf[--size] = '\0';
104 
105 	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
106 	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
107 		perror("Can't read clocks");
108 		snprintf(buf, size, "%llu", nsecs / 1000000000);
109 		return;
110 	}
111 
112 	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
113 		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
114 		1000000000;
115 
116 
117 	if (!localtime_r(&wallclock_secs, &load_tm)) {
118 		snprintf(buf, size, "%llu", nsecs / 1000000000);
119 		return;
120 	}
121 
122 	if (json_output)
123 		strftime(buf, size, "%s", &load_tm);
124 	else
125 		strftime(buf, size, "%FT%T%z", &load_tm);
126 }
127 
128 static void show_prog_maps(int fd, __u32 num_maps)
129 {
130 	struct bpf_prog_info info = {};
131 	__u32 len = sizeof(info);
132 	__u32 map_ids[num_maps];
133 	unsigned int i;
134 	int err;
135 
136 	info.nr_map_ids = num_maps;
137 	info.map_ids = ptr_to_u64(map_ids);
138 
139 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
140 	if (err || !info.nr_map_ids)
141 		return;
142 
143 	if (json_output) {
144 		jsonw_name(json_wtr, "map_ids");
145 		jsonw_start_array(json_wtr);
146 		for (i = 0; i < info.nr_map_ids; i++)
147 			jsonw_uint(json_wtr, map_ids[i]);
148 		jsonw_end_array(json_wtr);
149 	} else {
150 		printf("  map_ids ");
151 		for (i = 0; i < info.nr_map_ids; i++)
152 			printf("%u%s", map_ids[i],
153 			       i == info.nr_map_ids - 1 ? "" : ",");
154 	}
155 }
156 
157 static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)
158 {
159 	struct bpf_prog_info prog_info;
160 	__u32 prog_info_len;
161 	__u32 map_info_len;
162 	void *value = NULL;
163 	__u32 *map_ids;
164 	int nr_maps;
165 	int key = 0;
166 	int map_fd;
167 	int ret;
168 	__u32 i;
169 
170 	memset(&prog_info, 0, sizeof(prog_info));
171 	prog_info_len = sizeof(prog_info);
172 	ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
173 	if (ret)
174 		return NULL;
175 
176 	if (!prog_info.nr_map_ids)
177 		return NULL;
178 
179 	map_ids = calloc(prog_info.nr_map_ids, sizeof(__u32));
180 	if (!map_ids)
181 		return NULL;
182 
183 	nr_maps = prog_info.nr_map_ids;
184 	memset(&prog_info, 0, sizeof(prog_info));
185 	prog_info.nr_map_ids = nr_maps;
186 	prog_info.map_ids = ptr_to_u64(map_ids);
187 	prog_info_len = sizeof(prog_info);
188 
189 	ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
190 	if (ret)
191 		goto free_map_ids;
192 
193 	for (i = 0; i < prog_info.nr_map_ids; i++) {
194 		map_fd = bpf_map_get_fd_by_id(map_ids[i]);
195 		if (map_fd < 0)
196 			goto free_map_ids;
197 
198 		memset(map_info, 0, sizeof(*map_info));
199 		map_info_len = sizeof(*map_info);
200 		ret = bpf_obj_get_info_by_fd(map_fd, map_info, &map_info_len);
201 		if (ret < 0) {
202 			close(map_fd);
203 			goto free_map_ids;
204 		}
205 
206 		if (map_info->type != BPF_MAP_TYPE_ARRAY ||
207 		    map_info->key_size != sizeof(int) ||
208 		    map_info->max_entries != 1 ||
209 		    !map_info->btf_value_type_id ||
210 		    !strstr(map_info->name, ".rodata")) {
211 			close(map_fd);
212 			continue;
213 		}
214 
215 		value = malloc(map_info->value_size);
216 		if (!value) {
217 			close(map_fd);
218 			goto free_map_ids;
219 		}
220 
221 		if (bpf_map_lookup_elem(map_fd, &key, value)) {
222 			close(map_fd);
223 			free(value);
224 			value = NULL;
225 			goto free_map_ids;
226 		}
227 
228 		close(map_fd);
229 		break;
230 	}
231 
232 free_map_ids:
233 	free(map_ids);
234 	return value;
235 }
236 
237 static bool has_metadata_prefix(const char *s)
238 {
239 	return strncmp(s, BPF_METADATA_PREFIX, BPF_METADATA_PREFIX_LEN) == 0;
240 }
241 
242 static void show_prog_metadata(int fd, __u32 num_maps)
243 {
244 	const struct btf_type *t_datasec, *t_var;
245 	struct bpf_map_info map_info;
246 	struct btf_var_secinfo *vsi;
247 	bool printed_header = false;
248 	struct btf *btf = NULL;
249 	unsigned int i, vlen;
250 	void *value = NULL;
251 	const char *name;
252 	int err;
253 
254 	if (!num_maps)
255 		return;
256 
257 	memset(&map_info, 0, sizeof(map_info));
258 	value = find_metadata(fd, &map_info);
259 	if (!value)
260 		return;
261 
262 	err = btf__get_from_id(map_info.btf_id, &btf);
263 	if (err || !btf)
264 		goto out_free;
265 
266 	t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
267 	if (!btf_is_datasec(t_datasec))
268 		goto out_free;
269 
270 	vlen = btf_vlen(t_datasec);
271 	vsi = btf_var_secinfos(t_datasec);
272 
273 	/* We don't proceed to check the kinds of the elements of the DATASEC.
274 	 * The verifier enforces them to be BTF_KIND_VAR.
275 	 */
276 
277 	if (json_output) {
278 		struct btf_dumper d = {
279 			.btf = btf,
280 			.jw = json_wtr,
281 			.is_plain_text = false,
282 		};
283 
284 		for (i = 0; i < vlen; i++, vsi++) {
285 			t_var = btf__type_by_id(btf, vsi->type);
286 			name = btf__name_by_offset(btf, t_var->name_off);
287 
288 			if (!has_metadata_prefix(name))
289 				continue;
290 
291 			if (!printed_header) {
292 				jsonw_name(json_wtr, "metadata");
293 				jsonw_start_object(json_wtr);
294 				printed_header = true;
295 			}
296 
297 			jsonw_name(json_wtr, name + BPF_METADATA_PREFIX_LEN);
298 			err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
299 			if (err) {
300 				p_err("btf dump failed: %d", err);
301 				break;
302 			}
303 		}
304 		if (printed_header)
305 			jsonw_end_object(json_wtr);
306 	} else {
307 		json_writer_t *btf_wtr = jsonw_new(stdout);
308 		struct btf_dumper d = {
309 			.btf = btf,
310 			.jw = btf_wtr,
311 			.is_plain_text = true,
312 		};
313 
314 		if (!btf_wtr) {
315 			p_err("jsonw alloc failed");
316 			goto out_free;
317 		}
318 
319 		for (i = 0; i < vlen; i++, vsi++) {
320 			t_var = btf__type_by_id(btf, vsi->type);
321 			name = btf__name_by_offset(btf, t_var->name_off);
322 
323 			if (!has_metadata_prefix(name))
324 				continue;
325 
326 			if (!printed_header) {
327 				printf("\tmetadata:");
328 				printed_header = true;
329 			}
330 
331 			printf("\n\t\t%s = ", name + BPF_METADATA_PREFIX_LEN);
332 
333 			jsonw_reset(btf_wtr);
334 			err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
335 			if (err) {
336 				p_err("btf dump failed: %d", err);
337 				break;
338 			}
339 		}
340 		if (printed_header)
341 			jsonw_destroy(&btf_wtr);
342 	}
343 
344 out_free:
345 	btf__free(btf);
346 	free(value);
347 }
348 
349 static void print_prog_header_json(struct bpf_prog_info *info)
350 {
351 	jsonw_uint_field(json_wtr, "id", info->id);
352 	if (info->type < ARRAY_SIZE(prog_type_name))
353 		jsonw_string_field(json_wtr, "type",
354 				   prog_type_name[info->type]);
355 	else
356 		jsonw_uint_field(json_wtr, "type", info->type);
357 
358 	if (*info->name)
359 		jsonw_string_field(json_wtr, "name", info->name);
360 
361 	jsonw_name(json_wtr, "tag");
362 	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
363 		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
364 		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
365 
366 	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
367 	if (info->run_time_ns) {
368 		jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
369 		jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
370 	}
371 	if (info->recursion_misses)
372 		jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses);
373 }
374 
375 static void print_prog_json(struct bpf_prog_info *info, int fd)
376 {
377 	char *memlock;
378 
379 	jsonw_start_object(json_wtr);
380 	print_prog_header_json(info);
381 	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
382 
383 	if (info->load_time) {
384 		char buf[32];
385 
386 		print_boot_time(info->load_time, buf, sizeof(buf));
387 
388 		/* Piggy back on load_time, since 0 uid is a valid one */
389 		jsonw_name(json_wtr, "loaded_at");
390 		jsonw_printf(json_wtr, "%s", buf);
391 		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
392 	}
393 
394 	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
395 
396 	if (info->jited_prog_len) {
397 		jsonw_bool_field(json_wtr, "jited", true);
398 		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
399 	} else {
400 		jsonw_bool_field(json_wtr, "jited", false);
401 	}
402 
403 	memlock = get_fdinfo(fd, "memlock");
404 	if (memlock)
405 		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
406 	free(memlock);
407 
408 	if (info->nr_map_ids)
409 		show_prog_maps(fd, info->nr_map_ids);
410 
411 	if (info->btf_id)
412 		jsonw_int_field(json_wtr, "btf_id", info->btf_id);
413 
414 	if (!hash_empty(prog_table.table)) {
415 		struct pinned_obj *obj;
416 
417 		jsonw_name(json_wtr, "pinned");
418 		jsonw_start_array(json_wtr);
419 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
420 			if (obj->id == info->id)
421 				jsonw_string(json_wtr, obj->path);
422 		}
423 		jsonw_end_array(json_wtr);
424 	}
425 
426 	emit_obj_refs_json(&refs_table, info->id, json_wtr);
427 
428 	show_prog_metadata(fd, info->nr_map_ids);
429 
430 	jsonw_end_object(json_wtr);
431 }
432 
433 static void print_prog_header_plain(struct bpf_prog_info *info)
434 {
435 	printf("%u: ", info->id);
436 	if (info->type < ARRAY_SIZE(prog_type_name))
437 		printf("%s  ", prog_type_name[info->type]);
438 	else
439 		printf("type %u  ", info->type);
440 
441 	if (*info->name)
442 		printf("name %s  ", info->name);
443 
444 	printf("tag ");
445 	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
446 	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
447 	printf("%s", info->gpl_compatible ? "  gpl" : "");
448 	if (info->run_time_ns)
449 		printf(" run_time_ns %lld run_cnt %lld",
450 		       info->run_time_ns, info->run_cnt);
451 	if (info->recursion_misses)
452 		printf(" recursion_misses %lld", info->recursion_misses);
453 	printf("\n");
454 }
455 
456 static void print_prog_plain(struct bpf_prog_info *info, int fd)
457 {
458 	char *memlock;
459 
460 	print_prog_header_plain(info);
461 
462 	if (info->load_time) {
463 		char buf[32];
464 
465 		print_boot_time(info->load_time, buf, sizeof(buf));
466 
467 		/* Piggy back on load_time, since 0 uid is a valid one */
468 		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
469 	}
470 
471 	printf("\txlated %uB", info->xlated_prog_len);
472 
473 	if (info->jited_prog_len)
474 		printf("  jited %uB", info->jited_prog_len);
475 	else
476 		printf("  not jited");
477 
478 	memlock = get_fdinfo(fd, "memlock");
479 	if (memlock)
480 		printf("  memlock %sB", memlock);
481 	free(memlock);
482 
483 	if (info->nr_map_ids)
484 		show_prog_maps(fd, info->nr_map_ids);
485 
486 	if (!hash_empty(prog_table.table)) {
487 		struct pinned_obj *obj;
488 
489 		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
490 			if (obj->id == info->id)
491 				printf("\n\tpinned %s", obj->path);
492 		}
493 	}
494 
495 	if (info->btf_id)
496 		printf("\n\tbtf_id %d", info->btf_id);
497 
498 	emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
499 
500 	printf("\n");
501 
502 	show_prog_metadata(fd, info->nr_map_ids);
503 }
504 
505 static int show_prog(int fd)
506 {
507 	struct bpf_prog_info info = {};
508 	__u32 len = sizeof(info);
509 	int err;
510 
511 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
512 	if (err) {
513 		p_err("can't get prog info: %s", strerror(errno));
514 		return -1;
515 	}
516 
517 	if (json_output)
518 		print_prog_json(&info, fd);
519 	else
520 		print_prog_plain(&info, fd);
521 
522 	return 0;
523 }
524 
525 static int do_show_subset(int argc, char **argv)
526 {
527 	int *fds = NULL;
528 	int nb_fds, i;
529 	int err = -1;
530 
531 	fds = malloc(sizeof(int));
532 	if (!fds) {
533 		p_err("mem alloc failed");
534 		return -1;
535 	}
536 	nb_fds = prog_parse_fds(&argc, &argv, &fds);
537 	if (nb_fds < 1)
538 		goto exit_free;
539 
540 	if (json_output && nb_fds > 1)
541 		jsonw_start_array(json_wtr);	/* root array */
542 	for (i = 0; i < nb_fds; i++) {
543 		err = show_prog(fds[i]);
544 		if (err) {
545 			for (; i < nb_fds; i++)
546 				close(fds[i]);
547 			break;
548 		}
549 		close(fds[i]);
550 	}
551 	if (json_output && nb_fds > 1)
552 		jsonw_end_array(json_wtr);	/* root array */
553 
554 exit_free:
555 	free(fds);
556 	return err;
557 }
558 
559 static int do_show(int argc, char **argv)
560 {
561 	__u32 id = 0;
562 	int err;
563 	int fd;
564 
565 	if (show_pinned)
566 		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
567 	build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
568 
569 	if (argc == 2)
570 		return do_show_subset(argc, argv);
571 
572 	if (argc)
573 		return BAD_ARG();
574 
575 	if (json_output)
576 		jsonw_start_array(json_wtr);
577 	while (true) {
578 		err = bpf_prog_get_next_id(id, &id);
579 		if (err) {
580 			if (errno == ENOENT) {
581 				err = 0;
582 				break;
583 			}
584 			p_err("can't get next program: %s%s", strerror(errno),
585 			      errno == EINVAL ? " -- kernel too old?" : "");
586 			err = -1;
587 			break;
588 		}
589 
590 		fd = bpf_prog_get_fd_by_id(id);
591 		if (fd < 0) {
592 			if (errno == ENOENT)
593 				continue;
594 			p_err("can't get prog by id (%u): %s",
595 			      id, strerror(errno));
596 			err = -1;
597 			break;
598 		}
599 
600 		err = show_prog(fd);
601 		close(fd);
602 		if (err)
603 			break;
604 	}
605 
606 	if (json_output)
607 		jsonw_end_array(json_wtr);
608 
609 	delete_obj_refs_table(&refs_table);
610 
611 	return err;
612 }
613 
614 static int
615 prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
616 	  char *filepath, bool opcodes, bool visual, bool linum)
617 {
618 	struct bpf_prog_linfo *prog_linfo = NULL;
619 	const char *disasm_opt = NULL;
620 	struct dump_data dd = {};
621 	void *func_info = NULL;
622 	struct btf *btf = NULL;
623 	char func_sig[1024];
624 	unsigned char *buf;
625 	__u32 member_len;
626 	ssize_t n;
627 	int fd;
628 
629 	if (mode == DUMP_JITED) {
630 		if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
631 			p_info("no instructions returned");
632 			return -1;
633 		}
634 		buf = u64_to_ptr(info->jited_prog_insns);
635 		member_len = info->jited_prog_len;
636 	} else {	/* DUMP_XLATED */
637 		if (info->xlated_prog_len == 0 || !info->xlated_prog_insns) {
638 			p_err("error retrieving insn dump: kernel.kptr_restrict set?");
639 			return -1;
640 		}
641 		buf = u64_to_ptr(info->xlated_prog_insns);
642 		member_len = info->xlated_prog_len;
643 	}
644 
645 	if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
646 		p_err("failed to get btf");
647 		return -1;
648 	}
649 
650 	func_info = u64_to_ptr(info->func_info);
651 
652 	if (info->nr_line_info) {
653 		prog_linfo = bpf_prog_linfo__new(info);
654 		if (!prog_linfo)
655 			p_info("error in processing bpf_line_info.  continue without it.");
656 	}
657 
658 	if (filepath) {
659 		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
660 		if (fd < 0) {
661 			p_err("can't open file %s: %s", filepath,
662 			      strerror(errno));
663 			return -1;
664 		}
665 
666 		n = write(fd, buf, member_len);
667 		close(fd);
668 		if (n != (ssize_t)member_len) {
669 			p_err("error writing output file: %s",
670 			      n < 0 ? strerror(errno) : "short write");
671 			return -1;
672 		}
673 
674 		if (json_output)
675 			jsonw_null(json_wtr);
676 	} else if (mode == DUMP_JITED) {
677 		const char *name = NULL;
678 
679 		if (info->ifindex) {
680 			name = ifindex_to_bfd_params(info->ifindex,
681 						     info->netns_dev,
682 						     info->netns_ino,
683 						     &disasm_opt);
684 			if (!name)
685 				return -1;
686 		}
687 
688 		if (info->nr_jited_func_lens && info->jited_func_lens) {
689 			struct kernel_sym *sym = NULL;
690 			struct bpf_func_info *record;
691 			char sym_name[SYM_MAX_NAME];
692 			unsigned char *img = buf;
693 			__u64 *ksyms = NULL;
694 			__u32 *lens;
695 			__u32 i;
696 			if (info->nr_jited_ksyms) {
697 				kernel_syms_load(&dd);
698 				ksyms = u64_to_ptr(info->jited_ksyms);
699 			}
700 
701 			if (json_output)
702 				jsonw_start_array(json_wtr);
703 
704 			lens = u64_to_ptr(info->jited_func_lens);
705 			for (i = 0; i < info->nr_jited_func_lens; i++) {
706 				if (ksyms) {
707 					sym = kernel_syms_search(&dd, ksyms[i]);
708 					if (sym)
709 						sprintf(sym_name, "%s", sym->name);
710 					else
711 						sprintf(sym_name, "0x%016llx", ksyms[i]);
712 				} else {
713 					strcpy(sym_name, "unknown");
714 				}
715 
716 				if (func_info) {
717 					record = func_info + i * info->func_info_rec_size;
718 					btf_dumper_type_only(btf, record->type_id,
719 							     func_sig,
720 							     sizeof(func_sig));
721 				}
722 
723 				if (json_output) {
724 					jsonw_start_object(json_wtr);
725 					if (func_info && func_sig[0] != '\0') {
726 						jsonw_name(json_wtr, "proto");
727 						jsonw_string(json_wtr, func_sig);
728 					}
729 					jsonw_name(json_wtr, "name");
730 					jsonw_string(json_wtr, sym_name);
731 					jsonw_name(json_wtr, "insns");
732 				} else {
733 					if (func_info && func_sig[0] != '\0')
734 						printf("%s:\n", func_sig);
735 					printf("%s:\n", sym_name);
736 				}
737 
738 				disasm_print_insn(img, lens[i], opcodes,
739 						  name, disasm_opt, btf,
740 						  prog_linfo, ksyms[i], i,
741 						  linum);
742 
743 				img += lens[i];
744 
745 				if (json_output)
746 					jsonw_end_object(json_wtr);
747 				else
748 					printf("\n");
749 			}
750 
751 			if (json_output)
752 				jsonw_end_array(json_wtr);
753 		} else {
754 			disasm_print_insn(buf, member_len, opcodes, name,
755 					  disasm_opt, btf, NULL, 0, 0, false);
756 		}
757 	} else if (visual) {
758 		if (json_output)
759 			jsonw_null(json_wtr);
760 		else
761 			dump_xlated_cfg(buf, member_len);
762 	} else {
763 		kernel_syms_load(&dd);
764 		dd.nr_jited_ksyms = info->nr_jited_ksyms;
765 		dd.jited_ksyms = u64_to_ptr(info->jited_ksyms);
766 		dd.btf = btf;
767 		dd.func_info = func_info;
768 		dd.finfo_rec_size = info->func_info_rec_size;
769 		dd.prog_linfo = prog_linfo;
770 
771 		if (json_output)
772 			dump_xlated_json(&dd, buf, member_len, opcodes,
773 					 linum);
774 		else
775 			dump_xlated_plain(&dd, buf, member_len, opcodes,
776 					  linum);
777 		kernel_syms_destroy(&dd);
778 	}
779 
780 	return 0;
781 }
782 
783 static int do_dump(int argc, char **argv)
784 {
785 	struct bpf_prog_info_linear *info_linear;
786 	char *filepath = NULL;
787 	bool opcodes = false;
788 	bool visual = false;
789 	enum dump_mode mode;
790 	bool linum = false;
791 	int *fds = NULL;
792 	int nb_fds, i = 0;
793 	int err = -1;
794 	__u64 arrays;
795 
796 	if (is_prefix(*argv, "jited")) {
797 		if (disasm_init())
798 			return -1;
799 		mode = DUMP_JITED;
800 	} else if (is_prefix(*argv, "xlated")) {
801 		mode = DUMP_XLATED;
802 	} else {
803 		p_err("expected 'xlated' or 'jited', got: %s", *argv);
804 		return -1;
805 	}
806 	NEXT_ARG();
807 
808 	if (argc < 2)
809 		usage();
810 
811 	fds = malloc(sizeof(int));
812 	if (!fds) {
813 		p_err("mem alloc failed");
814 		return -1;
815 	}
816 	nb_fds = prog_parse_fds(&argc, &argv, &fds);
817 	if (nb_fds < 1)
818 		goto exit_free;
819 
820 	if (is_prefix(*argv, "file")) {
821 		NEXT_ARG();
822 		if (!argc) {
823 			p_err("expected file path");
824 			goto exit_close;
825 		}
826 		if (nb_fds > 1) {
827 			p_err("several programs matched");
828 			goto exit_close;
829 		}
830 
831 		filepath = *argv;
832 		NEXT_ARG();
833 	} else if (is_prefix(*argv, "opcodes")) {
834 		opcodes = true;
835 		NEXT_ARG();
836 	} else if (is_prefix(*argv, "visual")) {
837 		if (nb_fds > 1) {
838 			p_err("several programs matched");
839 			goto exit_close;
840 		}
841 
842 		visual = true;
843 		NEXT_ARG();
844 	} else if (is_prefix(*argv, "linum")) {
845 		linum = true;
846 		NEXT_ARG();
847 	}
848 
849 	if (argc) {
850 		usage();
851 		goto exit_close;
852 	}
853 
854 	if (mode == DUMP_JITED)
855 		arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
856 	else
857 		arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
858 
859 	arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
860 	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
861 	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
862 	arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
863 	arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
864 
865 	if (json_output && nb_fds > 1)
866 		jsonw_start_array(json_wtr);	/* root array */
867 	for (i = 0; i < nb_fds; i++) {
868 		info_linear = bpf_program__get_prog_info_linear(fds[i], arrays);
869 		if (IS_ERR_OR_NULL(info_linear)) {
870 			p_err("can't get prog info: %s", strerror(errno));
871 			break;
872 		}
873 
874 		if (json_output && nb_fds > 1) {
875 			jsonw_start_object(json_wtr);	/* prog object */
876 			print_prog_header_json(&info_linear->info);
877 			jsonw_name(json_wtr, "insns");
878 		} else if (nb_fds > 1) {
879 			print_prog_header_plain(&info_linear->info);
880 		}
881 
882 		err = prog_dump(&info_linear->info, mode, filepath, opcodes,
883 				visual, linum);
884 
885 		if (json_output && nb_fds > 1)
886 			jsonw_end_object(json_wtr);	/* prog object */
887 		else if (i != nb_fds - 1 && nb_fds > 1)
888 			printf("\n");
889 
890 		free(info_linear);
891 		if (err)
892 			break;
893 		close(fds[i]);
894 	}
895 	if (json_output && nb_fds > 1)
896 		jsonw_end_array(json_wtr);	/* root array */
897 
898 exit_close:
899 	for (; i < nb_fds; i++)
900 		close(fds[i]);
901 exit_free:
902 	free(fds);
903 	return err;
904 }
905 
906 static int do_pin(int argc, char **argv)
907 {
908 	int err;
909 
910 	err = do_pin_any(argc, argv, prog_parse_fd);
911 	if (!err && json_output)
912 		jsonw_null(json_wtr);
913 	return err;
914 }
915 
916 struct map_replace {
917 	int idx;
918 	int fd;
919 	char *name;
920 };
921 
922 static int map_replace_compar(const void *p1, const void *p2)
923 {
924 	const struct map_replace *a = p1, *b = p2;
925 
926 	return a->idx - b->idx;
927 }
928 
929 static int parse_attach_detach_args(int argc, char **argv, int *progfd,
930 				    enum bpf_attach_type *attach_type,
931 				    int *mapfd)
932 {
933 	if (!REQ_ARGS(3))
934 		return -EINVAL;
935 
936 	*progfd = prog_parse_fd(&argc, &argv);
937 	if (*progfd < 0)
938 		return *progfd;
939 
940 	*attach_type = parse_attach_type(*argv);
941 	if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
942 		p_err("invalid attach/detach type");
943 		return -EINVAL;
944 	}
945 
946 	if (*attach_type == BPF_FLOW_DISSECTOR) {
947 		*mapfd = 0;
948 		return 0;
949 	}
950 
951 	NEXT_ARG();
952 	if (!REQ_ARGS(2))
953 		return -EINVAL;
954 
955 	*mapfd = map_parse_fd(&argc, &argv);
956 	if (*mapfd < 0)
957 		return *mapfd;
958 
959 	return 0;
960 }
961 
962 static int do_attach(int argc, char **argv)
963 {
964 	enum bpf_attach_type attach_type;
965 	int err, progfd;
966 	int mapfd;
967 
968 	err = parse_attach_detach_args(argc, argv,
969 				       &progfd, &attach_type, &mapfd);
970 	if (err)
971 		return err;
972 
973 	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
974 	if (err) {
975 		p_err("failed prog attach to map");
976 		return -EINVAL;
977 	}
978 
979 	if (json_output)
980 		jsonw_null(json_wtr);
981 	return 0;
982 }
983 
984 static int do_detach(int argc, char **argv)
985 {
986 	enum bpf_attach_type attach_type;
987 	int err, progfd;
988 	int mapfd;
989 
990 	err = parse_attach_detach_args(argc, argv,
991 				       &progfd, &attach_type, &mapfd);
992 	if (err)
993 		return err;
994 
995 	err = bpf_prog_detach2(progfd, mapfd, attach_type);
996 	if (err) {
997 		p_err("failed prog detach from map");
998 		return -EINVAL;
999 	}
1000 
1001 	if (json_output)
1002 		jsonw_null(json_wtr);
1003 	return 0;
1004 }
1005 
1006 static int check_single_stdin(char *file_data_in, char *file_ctx_in)
1007 {
1008 	if (file_data_in && file_ctx_in &&
1009 	    !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
1010 		p_err("cannot use standard input for both data_in and ctx_in");
1011 		return -1;
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
1018 {
1019 	size_t block_size = 256;
1020 	size_t buf_size = block_size;
1021 	size_t nb_read = 0;
1022 	void *tmp;
1023 	FILE *f;
1024 
1025 	if (!fname) {
1026 		*data_ptr = NULL;
1027 		*size = 0;
1028 		return 0;
1029 	}
1030 
1031 	if (!strcmp(fname, "-"))
1032 		f = stdin;
1033 	else
1034 		f = fopen(fname, "r");
1035 	if (!f) {
1036 		p_err("failed to open %s: %s", fname, strerror(errno));
1037 		return -1;
1038 	}
1039 
1040 	*data_ptr = malloc(block_size);
1041 	if (!*data_ptr) {
1042 		p_err("failed to allocate memory for data_in/ctx_in: %s",
1043 		      strerror(errno));
1044 		goto err_fclose;
1045 	}
1046 
1047 	while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
1048 		if (feof(f))
1049 			break;
1050 		if (ferror(f)) {
1051 			p_err("failed to read data_in/ctx_in from %s: %s",
1052 			      fname, strerror(errno));
1053 			goto err_free;
1054 		}
1055 		if (nb_read > buf_size - block_size) {
1056 			if (buf_size == UINT32_MAX) {
1057 				p_err("data_in/ctx_in is too long (max: %d)",
1058 				      UINT32_MAX);
1059 				goto err_free;
1060 			}
1061 			/* No space for fread()-ing next chunk; realloc() */
1062 			buf_size *= 2;
1063 			tmp = realloc(*data_ptr, buf_size);
1064 			if (!tmp) {
1065 				p_err("failed to reallocate data_in/ctx_in: %s",
1066 				      strerror(errno));
1067 				goto err_free;
1068 			}
1069 			*data_ptr = tmp;
1070 		}
1071 	}
1072 	if (f != stdin)
1073 		fclose(f);
1074 
1075 	*size = nb_read;
1076 	return 0;
1077 
1078 err_free:
1079 	free(*data_ptr);
1080 	*data_ptr = NULL;
1081 err_fclose:
1082 	if (f != stdin)
1083 		fclose(f);
1084 	return -1;
1085 }
1086 
1087 static void hex_print(void *data, unsigned int size, FILE *f)
1088 {
1089 	size_t i, j;
1090 	char c;
1091 
1092 	for (i = 0; i < size; i += 16) {
1093 		/* Row offset */
1094 		fprintf(f, "%07zx\t", i);
1095 
1096 		/* Hexadecimal values */
1097 		for (j = i; j < i + 16 && j < size; j++)
1098 			fprintf(f, "%02x%s", *(uint8_t *)(data + j),
1099 				j % 2 ? " " : "");
1100 		for (; j < i + 16; j++)
1101 			fprintf(f, "  %s", j % 2 ? " " : "");
1102 
1103 		/* ASCII values (if relevant), '.' otherwise */
1104 		fprintf(f, "| ");
1105 		for (j = i; j < i + 16 && j < size; j++) {
1106 			c = *(char *)(data + j);
1107 			if (c < ' ' || c > '~')
1108 				c = '.';
1109 			fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
1110 		}
1111 
1112 		fprintf(f, "\n");
1113 	}
1114 }
1115 
1116 static int
1117 print_run_output(void *data, unsigned int size, const char *fname,
1118 		 const char *json_key)
1119 {
1120 	size_t nb_written;
1121 	FILE *f;
1122 
1123 	if (!fname)
1124 		return 0;
1125 
1126 	if (!strcmp(fname, "-")) {
1127 		f = stdout;
1128 		if (json_output) {
1129 			jsonw_name(json_wtr, json_key);
1130 			print_data_json(data, size);
1131 		} else {
1132 			hex_print(data, size, f);
1133 		}
1134 		return 0;
1135 	}
1136 
1137 	f = fopen(fname, "w");
1138 	if (!f) {
1139 		p_err("failed to open %s: %s", fname, strerror(errno));
1140 		return -1;
1141 	}
1142 
1143 	nb_written = fwrite(data, 1, size, f);
1144 	fclose(f);
1145 	if (nb_written != size) {
1146 		p_err("failed to write output data/ctx: %s", strerror(errno));
1147 		return -1;
1148 	}
1149 
1150 	return 0;
1151 }
1152 
1153 static int alloc_run_data(void **data_ptr, unsigned int size_out)
1154 {
1155 	*data_ptr = calloc(size_out, 1);
1156 	if (!*data_ptr) {
1157 		p_err("failed to allocate memory for output data/ctx: %s",
1158 		      strerror(errno));
1159 		return -1;
1160 	}
1161 
1162 	return 0;
1163 }
1164 
1165 static int do_run(int argc, char **argv)
1166 {
1167 	char *data_fname_in = NULL, *data_fname_out = NULL;
1168 	char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
1169 	struct bpf_prog_test_run_attr test_attr = {0};
1170 	const unsigned int default_size = SZ_32K;
1171 	void *data_in = NULL, *data_out = NULL;
1172 	void *ctx_in = NULL, *ctx_out = NULL;
1173 	unsigned int repeat = 1;
1174 	int fd, err;
1175 
1176 	if (!REQ_ARGS(4))
1177 		return -1;
1178 
1179 	fd = prog_parse_fd(&argc, &argv);
1180 	if (fd < 0)
1181 		return -1;
1182 
1183 	while (argc) {
1184 		if (detect_common_prefix(*argv, "data_in", "data_out",
1185 					 "data_size_out", NULL))
1186 			return -1;
1187 		if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
1188 					 "ctx_size_out", NULL))
1189 			return -1;
1190 
1191 		if (is_prefix(*argv, "data_in")) {
1192 			NEXT_ARG();
1193 			if (!REQ_ARGS(1))
1194 				return -1;
1195 
1196 			data_fname_in = GET_ARG();
1197 			if (check_single_stdin(data_fname_in, ctx_fname_in))
1198 				return -1;
1199 		} else if (is_prefix(*argv, "data_out")) {
1200 			NEXT_ARG();
1201 			if (!REQ_ARGS(1))
1202 				return -1;
1203 
1204 			data_fname_out = GET_ARG();
1205 		} else if (is_prefix(*argv, "data_size_out")) {
1206 			char *endptr;
1207 
1208 			NEXT_ARG();
1209 			if (!REQ_ARGS(1))
1210 				return -1;
1211 
1212 			test_attr.data_size_out = strtoul(*argv, &endptr, 0);
1213 			if (*endptr) {
1214 				p_err("can't parse %s as output data size",
1215 				      *argv);
1216 				return -1;
1217 			}
1218 			NEXT_ARG();
1219 		} else if (is_prefix(*argv, "ctx_in")) {
1220 			NEXT_ARG();
1221 			if (!REQ_ARGS(1))
1222 				return -1;
1223 
1224 			ctx_fname_in = GET_ARG();
1225 			if (check_single_stdin(data_fname_in, ctx_fname_in))
1226 				return -1;
1227 		} else if (is_prefix(*argv, "ctx_out")) {
1228 			NEXT_ARG();
1229 			if (!REQ_ARGS(1))
1230 				return -1;
1231 
1232 			ctx_fname_out = GET_ARG();
1233 		} else if (is_prefix(*argv, "ctx_size_out")) {
1234 			char *endptr;
1235 
1236 			NEXT_ARG();
1237 			if (!REQ_ARGS(1))
1238 				return -1;
1239 
1240 			test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
1241 			if (*endptr) {
1242 				p_err("can't parse %s as output context size",
1243 				      *argv);
1244 				return -1;
1245 			}
1246 			NEXT_ARG();
1247 		} else if (is_prefix(*argv, "repeat")) {
1248 			char *endptr;
1249 
1250 			NEXT_ARG();
1251 			if (!REQ_ARGS(1))
1252 				return -1;
1253 
1254 			repeat = strtoul(*argv, &endptr, 0);
1255 			if (*endptr) {
1256 				p_err("can't parse %s as repeat number",
1257 				      *argv);
1258 				return -1;
1259 			}
1260 			NEXT_ARG();
1261 		} else {
1262 			p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1263 			      *argv);
1264 			return -1;
1265 		}
1266 	}
1267 
1268 	err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1269 	if (err)
1270 		return -1;
1271 
1272 	if (data_in) {
1273 		if (!test_attr.data_size_out)
1274 			test_attr.data_size_out = default_size;
1275 		err = alloc_run_data(&data_out, test_attr.data_size_out);
1276 		if (err)
1277 			goto free_data_in;
1278 	}
1279 
1280 	err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1281 	if (err)
1282 		goto free_data_out;
1283 
1284 	if (ctx_in) {
1285 		if (!test_attr.ctx_size_out)
1286 			test_attr.ctx_size_out = default_size;
1287 		err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1288 		if (err)
1289 			goto free_ctx_in;
1290 	}
1291 
1292 	test_attr.prog_fd	= fd;
1293 	test_attr.repeat	= repeat;
1294 	test_attr.data_in	= data_in;
1295 	test_attr.data_out	= data_out;
1296 	test_attr.ctx_in	= ctx_in;
1297 	test_attr.ctx_out	= ctx_out;
1298 
1299 	err = bpf_prog_test_run_xattr(&test_attr);
1300 	if (err) {
1301 		p_err("failed to run program: %s", strerror(errno));
1302 		goto free_ctx_out;
1303 	}
1304 
1305 	err = 0;
1306 
1307 	if (json_output)
1308 		jsonw_start_object(json_wtr);	/* root */
1309 
1310 	/* Do not exit on errors occurring when printing output data/context,
1311 	 * we still want to print return value and duration for program run.
1312 	 */
1313 	if (test_attr.data_size_out)
1314 		err += print_run_output(test_attr.data_out,
1315 					test_attr.data_size_out,
1316 					data_fname_out, "data_out");
1317 	if (test_attr.ctx_size_out)
1318 		err += print_run_output(test_attr.ctx_out,
1319 					test_attr.ctx_size_out,
1320 					ctx_fname_out, "ctx_out");
1321 
1322 	if (json_output) {
1323 		jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1324 		jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1325 		jsonw_end_object(json_wtr);	/* root */
1326 	} else {
1327 		fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1328 			test_attr.retval,
1329 			repeat > 1 ? " (average)" : "", test_attr.duration);
1330 	}
1331 
1332 free_ctx_out:
1333 	free(ctx_out);
1334 free_ctx_in:
1335 	free(ctx_in);
1336 free_data_out:
1337 	free(data_out);
1338 free_data_in:
1339 	free(data_in);
1340 
1341 	return err;
1342 }
1343 
1344 static int
1345 get_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
1346 		      enum bpf_attach_type *expected_attach_type)
1347 {
1348 	libbpf_print_fn_t print_backup;
1349 	int ret;
1350 
1351 	ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
1352 	if (!ret)
1353 		return ret;
1354 
1355 	/* libbpf_prog_type_by_name() failed, let's re-run with debug level */
1356 	print_backup = libbpf_set_print(print_all_levels);
1357 	ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
1358 	libbpf_set_print(print_backup);
1359 
1360 	return ret;
1361 }
1362 
1363 static int load_with_options(int argc, char **argv, bool first_prog_only)
1364 {
1365 	enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
1366 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
1367 		.relaxed_maps = relaxed_maps,
1368 	);
1369 	struct bpf_object_load_attr load_attr = { 0 };
1370 	enum bpf_attach_type expected_attach_type;
1371 	struct map_replace *map_replace = NULL;
1372 	struct bpf_program *prog = NULL, *pos;
1373 	unsigned int old_map_fds = 0;
1374 	const char *pinmaps = NULL;
1375 	struct bpf_object *obj;
1376 	struct bpf_map *map;
1377 	const char *pinfile;
1378 	unsigned int i, j;
1379 	__u32 ifindex = 0;
1380 	const char *file;
1381 	int idx, err;
1382 
1383 
1384 	if (!REQ_ARGS(2))
1385 		return -1;
1386 	file = GET_ARG();
1387 	pinfile = GET_ARG();
1388 
1389 	while (argc) {
1390 		if (is_prefix(*argv, "type")) {
1391 			char *type;
1392 
1393 			NEXT_ARG();
1394 
1395 			if (common_prog_type != BPF_PROG_TYPE_UNSPEC) {
1396 				p_err("program type already specified");
1397 				goto err_free_reuse_maps;
1398 			}
1399 			if (!REQ_ARGS(1))
1400 				goto err_free_reuse_maps;
1401 
1402 			/* Put a '/' at the end of type to appease libbpf */
1403 			type = malloc(strlen(*argv) + 2);
1404 			if (!type) {
1405 				p_err("mem alloc failed");
1406 				goto err_free_reuse_maps;
1407 			}
1408 			*type = 0;
1409 			strcat(type, *argv);
1410 			strcat(type, "/");
1411 
1412 			err = get_prog_type_by_name(type, &common_prog_type,
1413 						    &expected_attach_type);
1414 			free(type);
1415 			if (err < 0)
1416 				goto err_free_reuse_maps;
1417 
1418 			NEXT_ARG();
1419 		} else if (is_prefix(*argv, "map")) {
1420 			void *new_map_replace;
1421 			char *endptr, *name;
1422 			int fd;
1423 
1424 			NEXT_ARG();
1425 
1426 			if (!REQ_ARGS(4))
1427 				goto err_free_reuse_maps;
1428 
1429 			if (is_prefix(*argv, "idx")) {
1430 				NEXT_ARG();
1431 
1432 				idx = strtoul(*argv, &endptr, 0);
1433 				if (*endptr) {
1434 					p_err("can't parse %s as IDX", *argv);
1435 					goto err_free_reuse_maps;
1436 				}
1437 				name = NULL;
1438 			} else if (is_prefix(*argv, "name")) {
1439 				NEXT_ARG();
1440 
1441 				name = *argv;
1442 				idx = -1;
1443 			} else {
1444 				p_err("expected 'idx' or 'name', got: '%s'?",
1445 				      *argv);
1446 				goto err_free_reuse_maps;
1447 			}
1448 			NEXT_ARG();
1449 
1450 			fd = map_parse_fd(&argc, &argv);
1451 			if (fd < 0)
1452 				goto err_free_reuse_maps;
1453 
1454 			new_map_replace = reallocarray(map_replace,
1455 						       old_map_fds + 1,
1456 						       sizeof(*map_replace));
1457 			if (!new_map_replace) {
1458 				p_err("mem alloc failed");
1459 				goto err_free_reuse_maps;
1460 			}
1461 			map_replace = new_map_replace;
1462 
1463 			map_replace[old_map_fds].idx = idx;
1464 			map_replace[old_map_fds].name = name;
1465 			map_replace[old_map_fds].fd = fd;
1466 			old_map_fds++;
1467 		} else if (is_prefix(*argv, "dev")) {
1468 			NEXT_ARG();
1469 
1470 			if (ifindex) {
1471 				p_err("offload device already specified");
1472 				goto err_free_reuse_maps;
1473 			}
1474 			if (!REQ_ARGS(1))
1475 				goto err_free_reuse_maps;
1476 
1477 			ifindex = if_nametoindex(*argv);
1478 			if (!ifindex) {
1479 				p_err("unrecognized netdevice '%s': %s",
1480 				      *argv, strerror(errno));
1481 				goto err_free_reuse_maps;
1482 			}
1483 			NEXT_ARG();
1484 		} else if (is_prefix(*argv, "pinmaps")) {
1485 			NEXT_ARG();
1486 
1487 			if (!REQ_ARGS(1))
1488 				goto err_free_reuse_maps;
1489 
1490 			pinmaps = GET_ARG();
1491 		} else {
1492 			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1493 			      *argv);
1494 			goto err_free_reuse_maps;
1495 		}
1496 	}
1497 
1498 	set_max_rlimit();
1499 
1500 	obj = bpf_object__open_file(file, &open_opts);
1501 	if (IS_ERR_OR_NULL(obj)) {
1502 		p_err("failed to open object file");
1503 		goto err_free_reuse_maps;
1504 	}
1505 
1506 	bpf_object__for_each_program(pos, obj) {
1507 		enum bpf_prog_type prog_type = common_prog_type;
1508 
1509 		if (prog_type == BPF_PROG_TYPE_UNSPEC) {
1510 			const char *sec_name = bpf_program__section_name(pos);
1511 
1512 			err = get_prog_type_by_name(sec_name, &prog_type,
1513 						    &expected_attach_type);
1514 			if (err < 0)
1515 				goto err_close_obj;
1516 		}
1517 
1518 		bpf_program__set_ifindex(pos, ifindex);
1519 		bpf_program__set_type(pos, prog_type);
1520 		bpf_program__set_expected_attach_type(pos, expected_attach_type);
1521 	}
1522 
1523 	qsort(map_replace, old_map_fds, sizeof(*map_replace),
1524 	      map_replace_compar);
1525 
1526 	/* After the sort maps by name will be first on the list, because they
1527 	 * have idx == -1.  Resolve them.
1528 	 */
1529 	j = 0;
1530 	while (j < old_map_fds && map_replace[j].name) {
1531 		i = 0;
1532 		bpf_object__for_each_map(map, obj) {
1533 			if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
1534 				map_replace[j].idx = i;
1535 				break;
1536 			}
1537 			i++;
1538 		}
1539 		if (map_replace[j].idx == -1) {
1540 			p_err("unable to find map '%s'", map_replace[j].name);
1541 			goto err_close_obj;
1542 		}
1543 		j++;
1544 	}
1545 	/* Resort if any names were resolved */
1546 	if (j)
1547 		qsort(map_replace, old_map_fds, sizeof(*map_replace),
1548 		      map_replace_compar);
1549 
1550 	/* Set ifindex and name reuse */
1551 	j = 0;
1552 	idx = 0;
1553 	bpf_object__for_each_map(map, obj) {
1554 		if (!bpf_map__is_offload_neutral(map))
1555 			bpf_map__set_ifindex(map, ifindex);
1556 
1557 		if (j < old_map_fds && idx == map_replace[j].idx) {
1558 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
1559 			if (err) {
1560 				p_err("unable to set up map reuse: %d", err);
1561 				goto err_close_obj;
1562 			}
1563 
1564 			/* Next reuse wants to apply to the same map */
1565 			if (j < old_map_fds && map_replace[j].idx == idx) {
1566 				p_err("replacement for map idx %d specified more than once",
1567 				      idx);
1568 				goto err_close_obj;
1569 			}
1570 		}
1571 
1572 		idx++;
1573 	}
1574 	if (j < old_map_fds) {
1575 		p_err("map idx '%d' not used", map_replace[j].idx);
1576 		goto err_close_obj;
1577 	}
1578 
1579 	load_attr.obj = obj;
1580 	if (verifier_logs)
1581 		/* log_level1 + log_level2 + stats, but not stable UAPI */
1582 		load_attr.log_level = 1 + 2 + 4;
1583 
1584 	err = bpf_object__load_xattr(&load_attr);
1585 	if (err) {
1586 		p_err("failed to load object file");
1587 		goto err_close_obj;
1588 	}
1589 
1590 	err = mount_bpffs_for_pin(pinfile);
1591 	if (err)
1592 		goto err_close_obj;
1593 
1594 	if (first_prog_only) {
1595 		prog = bpf_program__next(NULL, obj);
1596 		if (!prog) {
1597 			p_err("object file doesn't contain any bpf program");
1598 			goto err_close_obj;
1599 		}
1600 
1601 		err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1602 		if (err) {
1603 			p_err("failed to pin program %s",
1604 			      bpf_program__section_name(prog));
1605 			goto err_close_obj;
1606 		}
1607 	} else {
1608 		err = bpf_object__pin_programs(obj, pinfile);
1609 		if (err) {
1610 			p_err("failed to pin all programs");
1611 			goto err_close_obj;
1612 		}
1613 	}
1614 
1615 	if (pinmaps) {
1616 		err = bpf_object__pin_maps(obj, pinmaps);
1617 		if (err) {
1618 			p_err("failed to pin all maps");
1619 			goto err_unpin;
1620 		}
1621 	}
1622 
1623 	if (json_output)
1624 		jsonw_null(json_wtr);
1625 
1626 	bpf_object__close(obj);
1627 	for (i = 0; i < old_map_fds; i++)
1628 		close(map_replace[i].fd);
1629 	free(map_replace);
1630 
1631 	return 0;
1632 
1633 err_unpin:
1634 	if (first_prog_only)
1635 		unlink(pinfile);
1636 	else
1637 		bpf_object__unpin_programs(obj, pinfile);
1638 err_close_obj:
1639 	bpf_object__close(obj);
1640 err_free_reuse_maps:
1641 	for (i = 0; i < old_map_fds; i++)
1642 		close(map_replace[i].fd);
1643 	free(map_replace);
1644 	return -1;
1645 }
1646 
1647 static int do_load(int argc, char **argv)
1648 {
1649 	return load_with_options(argc, argv, true);
1650 }
1651 
1652 static int do_loadall(int argc, char **argv)
1653 {
1654 	return load_with_options(argc, argv, false);
1655 }
1656 
1657 #ifdef BPFTOOL_WITHOUT_SKELETONS
1658 
1659 static int do_profile(int argc, char **argv)
1660 {
1661 	p_err("bpftool prog profile command is not supported. Please build bpftool with clang >= 10.0.0");
1662 	return 0;
1663 }
1664 
1665 #else /* BPFTOOL_WITHOUT_SKELETONS */
1666 
1667 #include "profiler.skel.h"
1668 
1669 struct profile_metric {
1670 	const char *name;
1671 	struct bpf_perf_event_value val;
1672 	struct perf_event_attr attr;
1673 	bool selected;
1674 
1675 	/* calculate ratios like instructions per cycle */
1676 	const int ratio_metric; /* 0 for N/A, 1 for index 0 (cycles) */
1677 	const char *ratio_desc;
1678 	const float ratio_mul;
1679 } metrics[] = {
1680 	{
1681 		.name = "cycles",
1682 		.attr = {
1683 			.type = PERF_TYPE_HARDWARE,
1684 			.config = PERF_COUNT_HW_CPU_CYCLES,
1685 			.exclude_user = 1,
1686 		},
1687 	},
1688 	{
1689 		.name = "instructions",
1690 		.attr = {
1691 			.type = PERF_TYPE_HARDWARE,
1692 			.config = PERF_COUNT_HW_INSTRUCTIONS,
1693 			.exclude_user = 1,
1694 		},
1695 		.ratio_metric = 1,
1696 		.ratio_desc = "insns per cycle",
1697 		.ratio_mul = 1.0,
1698 	},
1699 	{
1700 		.name = "l1d_loads",
1701 		.attr = {
1702 			.type = PERF_TYPE_HW_CACHE,
1703 			.config =
1704 				PERF_COUNT_HW_CACHE_L1D |
1705 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1706 				(PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
1707 			.exclude_user = 1,
1708 		},
1709 	},
1710 	{
1711 		.name = "llc_misses",
1712 		.attr = {
1713 			.type = PERF_TYPE_HW_CACHE,
1714 			.config =
1715 				PERF_COUNT_HW_CACHE_LL |
1716 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1717 				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
1718 			.exclude_user = 1
1719 		},
1720 		.ratio_metric = 2,
1721 		.ratio_desc = "LLC misses per million insns",
1722 		.ratio_mul = 1e6,
1723 	},
1724 	{
1725 		.name = "itlb_misses",
1726 		.attr = {
1727 			.type = PERF_TYPE_HW_CACHE,
1728 			.config =
1729 				PERF_COUNT_HW_CACHE_ITLB |
1730 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1731 				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
1732 			.exclude_user = 1
1733 		},
1734 		.ratio_metric = 2,
1735 		.ratio_desc = "itlb misses per million insns",
1736 		.ratio_mul = 1e6,
1737 	},
1738 	{
1739 		.name = "dtlb_misses",
1740 		.attr = {
1741 			.type = PERF_TYPE_HW_CACHE,
1742 			.config =
1743 				PERF_COUNT_HW_CACHE_DTLB |
1744 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
1745 				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
1746 			.exclude_user = 1
1747 		},
1748 		.ratio_metric = 2,
1749 		.ratio_desc = "dtlb misses per million insns",
1750 		.ratio_mul = 1e6,
1751 	},
1752 };
1753 
1754 static __u64 profile_total_count;
1755 
1756 #define MAX_NUM_PROFILE_METRICS 4
1757 
1758 static int profile_parse_metrics(int argc, char **argv)
1759 {
1760 	unsigned int metric_cnt;
1761 	int selected_cnt = 0;
1762 	unsigned int i;
1763 
1764 	metric_cnt = sizeof(metrics) / sizeof(struct profile_metric);
1765 
1766 	while (argc > 0) {
1767 		for (i = 0; i < metric_cnt; i++) {
1768 			if (is_prefix(argv[0], metrics[i].name)) {
1769 				if (!metrics[i].selected)
1770 					selected_cnt++;
1771 				metrics[i].selected = true;
1772 				break;
1773 			}
1774 		}
1775 		if (i == metric_cnt) {
1776 			p_err("unknown metric %s", argv[0]);
1777 			return -1;
1778 		}
1779 		NEXT_ARG();
1780 	}
1781 	if (selected_cnt > MAX_NUM_PROFILE_METRICS) {
1782 		p_err("too many (%d) metrics, please specify no more than %d metrics at at time",
1783 		      selected_cnt, MAX_NUM_PROFILE_METRICS);
1784 		return -1;
1785 	}
1786 	return selected_cnt;
1787 }
1788 
1789 static void profile_read_values(struct profiler_bpf *obj)
1790 {
1791 	__u32 m, cpu, num_cpu = obj->rodata->num_cpu;
1792 	int reading_map_fd, count_map_fd;
1793 	__u64 counts[num_cpu];
1794 	__u32 key = 0;
1795 	int err;
1796 
1797 	reading_map_fd = bpf_map__fd(obj->maps.accum_readings);
1798 	count_map_fd = bpf_map__fd(obj->maps.counts);
1799 	if (reading_map_fd < 0 || count_map_fd < 0) {
1800 		p_err("failed to get fd for map");
1801 		return;
1802 	}
1803 
1804 	err = bpf_map_lookup_elem(count_map_fd, &key, counts);
1805 	if (err) {
1806 		p_err("failed to read count_map: %s", strerror(errno));
1807 		return;
1808 	}
1809 
1810 	profile_total_count = 0;
1811 	for (cpu = 0; cpu < num_cpu; cpu++)
1812 		profile_total_count += counts[cpu];
1813 
1814 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1815 		struct bpf_perf_event_value values[num_cpu];
1816 
1817 		if (!metrics[m].selected)
1818 			continue;
1819 
1820 		err = bpf_map_lookup_elem(reading_map_fd, &key, values);
1821 		if (err) {
1822 			p_err("failed to read reading_map: %s",
1823 			      strerror(errno));
1824 			return;
1825 		}
1826 		for (cpu = 0; cpu < num_cpu; cpu++) {
1827 			metrics[m].val.counter += values[cpu].counter;
1828 			metrics[m].val.enabled += values[cpu].enabled;
1829 			metrics[m].val.running += values[cpu].running;
1830 		}
1831 		key++;
1832 	}
1833 }
1834 
1835 static void profile_print_readings_json(void)
1836 {
1837 	__u32 m;
1838 
1839 	jsonw_start_array(json_wtr);
1840 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1841 		if (!metrics[m].selected)
1842 			continue;
1843 		jsonw_start_object(json_wtr);
1844 		jsonw_string_field(json_wtr, "metric", metrics[m].name);
1845 		jsonw_lluint_field(json_wtr, "run_cnt", profile_total_count);
1846 		jsonw_lluint_field(json_wtr, "value", metrics[m].val.counter);
1847 		jsonw_lluint_field(json_wtr, "enabled", metrics[m].val.enabled);
1848 		jsonw_lluint_field(json_wtr, "running", metrics[m].val.running);
1849 
1850 		jsonw_end_object(json_wtr);
1851 	}
1852 	jsonw_end_array(json_wtr);
1853 }
1854 
1855 static void profile_print_readings_plain(void)
1856 {
1857 	__u32 m;
1858 
1859 	printf("\n%18llu %-20s\n", profile_total_count, "run_cnt");
1860 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1861 		struct bpf_perf_event_value *val = &metrics[m].val;
1862 		int r;
1863 
1864 		if (!metrics[m].selected)
1865 			continue;
1866 		printf("%18llu %-20s", val->counter, metrics[m].name);
1867 
1868 		r = metrics[m].ratio_metric - 1;
1869 		if (r >= 0 && metrics[r].selected &&
1870 		    metrics[r].val.counter > 0) {
1871 			printf("# %8.2f %-30s",
1872 			       val->counter * metrics[m].ratio_mul /
1873 			       metrics[r].val.counter,
1874 			       metrics[m].ratio_desc);
1875 		} else {
1876 			printf("%-41s", "");
1877 		}
1878 
1879 		if (val->enabled > val->running)
1880 			printf("(%4.2f%%)",
1881 			       val->running * 100.0 / val->enabled);
1882 		printf("\n");
1883 	}
1884 }
1885 
1886 static void profile_print_readings(void)
1887 {
1888 	if (json_output)
1889 		profile_print_readings_json();
1890 	else
1891 		profile_print_readings_plain();
1892 }
1893 
1894 static char *profile_target_name(int tgt_fd)
1895 {
1896 	struct bpf_prog_info_linear *info_linear;
1897 	struct bpf_func_info *func_info;
1898 	const struct btf_type *t;
1899 	char *name = NULL;
1900 	struct btf *btf;
1901 
1902 	info_linear = bpf_program__get_prog_info_linear(
1903 		tgt_fd, 1UL << BPF_PROG_INFO_FUNC_INFO);
1904 	if (IS_ERR_OR_NULL(info_linear)) {
1905 		p_err("failed to get info_linear for prog FD %d", tgt_fd);
1906 		return NULL;
1907 	}
1908 
1909 	if (info_linear->info.btf_id == 0 ||
1910 	    btf__get_from_id(info_linear->info.btf_id, &btf)) {
1911 		p_err("prog FD %d doesn't have valid btf", tgt_fd);
1912 		goto out;
1913 	}
1914 
1915 	func_info = u64_to_ptr(info_linear->info.func_info);
1916 	t = btf__type_by_id(btf, func_info[0].type_id);
1917 	if (!t) {
1918 		p_err("btf %d doesn't have type %d",
1919 		      info_linear->info.btf_id, func_info[0].type_id);
1920 		goto out;
1921 	}
1922 	name = strdup(btf__name_by_offset(btf, t->name_off));
1923 out:
1924 	free(info_linear);
1925 	return name;
1926 }
1927 
1928 static struct profiler_bpf *profile_obj;
1929 static int profile_tgt_fd = -1;
1930 static char *profile_tgt_name;
1931 static int *profile_perf_events;
1932 static int profile_perf_event_cnt;
1933 
1934 static void profile_close_perf_events(struct profiler_bpf *obj)
1935 {
1936 	int i;
1937 
1938 	for (i = profile_perf_event_cnt - 1; i >= 0; i--)
1939 		close(profile_perf_events[i]);
1940 
1941 	free(profile_perf_events);
1942 	profile_perf_event_cnt = 0;
1943 }
1944 
1945 static int profile_open_perf_events(struct profiler_bpf *obj)
1946 {
1947 	unsigned int cpu, m;
1948 	int map_fd, pmu_fd;
1949 
1950 	profile_perf_events = calloc(
1951 		sizeof(int), obj->rodata->num_cpu * obj->rodata->num_metric);
1952 	if (!profile_perf_events) {
1953 		p_err("failed to allocate memory for perf_event array: %s",
1954 		      strerror(errno));
1955 		return -1;
1956 	}
1957 	map_fd = bpf_map__fd(obj->maps.events);
1958 	if (map_fd < 0) {
1959 		p_err("failed to get fd for events map");
1960 		return -1;
1961 	}
1962 
1963 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
1964 		if (!metrics[m].selected)
1965 			continue;
1966 		for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) {
1967 			pmu_fd = syscall(__NR_perf_event_open, &metrics[m].attr,
1968 					 -1/*pid*/, cpu, -1/*group_fd*/, 0);
1969 			if (pmu_fd < 0 ||
1970 			    bpf_map_update_elem(map_fd, &profile_perf_event_cnt,
1971 						&pmu_fd, BPF_ANY) ||
1972 			    ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
1973 				p_err("failed to create event %s on cpu %d",
1974 				      metrics[m].name, cpu);
1975 				return -1;
1976 			}
1977 			profile_perf_events[profile_perf_event_cnt++] = pmu_fd;
1978 		}
1979 	}
1980 	return 0;
1981 }
1982 
1983 static void profile_print_and_cleanup(void)
1984 {
1985 	profile_close_perf_events(profile_obj);
1986 	profile_read_values(profile_obj);
1987 	profile_print_readings();
1988 	profiler_bpf__destroy(profile_obj);
1989 
1990 	close(profile_tgt_fd);
1991 	free(profile_tgt_name);
1992 }
1993 
1994 static void int_exit(int signo)
1995 {
1996 	profile_print_and_cleanup();
1997 	exit(0);
1998 }
1999 
2000 static int do_profile(int argc, char **argv)
2001 {
2002 	int num_metric, num_cpu, err = -1;
2003 	struct bpf_program *prog;
2004 	unsigned long duration;
2005 	char *endptr;
2006 
2007 	/* we at least need two args for the prog and one metric */
2008 	if (!REQ_ARGS(3))
2009 		return -EINVAL;
2010 
2011 	/* parse target fd */
2012 	profile_tgt_fd = prog_parse_fd(&argc, &argv);
2013 	if (profile_tgt_fd < 0) {
2014 		p_err("failed to parse fd");
2015 		return -1;
2016 	}
2017 
2018 	/* parse profiling optional duration */
2019 	if (argc > 2 && is_prefix(argv[0], "duration")) {
2020 		NEXT_ARG();
2021 		duration = strtoul(*argv, &endptr, 0);
2022 		if (*endptr)
2023 			usage();
2024 		NEXT_ARG();
2025 	} else {
2026 		duration = UINT_MAX;
2027 	}
2028 
2029 	num_metric = profile_parse_metrics(argc, argv);
2030 	if (num_metric <= 0)
2031 		goto out;
2032 
2033 	num_cpu = libbpf_num_possible_cpus();
2034 	if (num_cpu <= 0) {
2035 		p_err("failed to identify number of CPUs");
2036 		goto out;
2037 	}
2038 
2039 	profile_obj = profiler_bpf__open();
2040 	if (!profile_obj) {
2041 		p_err("failed to open and/or load BPF object");
2042 		goto out;
2043 	}
2044 
2045 	profile_obj->rodata->num_cpu = num_cpu;
2046 	profile_obj->rodata->num_metric = num_metric;
2047 
2048 	/* adjust map sizes */
2049 	bpf_map__resize(profile_obj->maps.events, num_metric * num_cpu);
2050 	bpf_map__resize(profile_obj->maps.fentry_readings, num_metric);
2051 	bpf_map__resize(profile_obj->maps.accum_readings, num_metric);
2052 	bpf_map__resize(profile_obj->maps.counts, 1);
2053 
2054 	/* change target name */
2055 	profile_tgt_name = profile_target_name(profile_tgt_fd);
2056 	if (!profile_tgt_name)
2057 		goto out;
2058 
2059 	bpf_object__for_each_program(prog, profile_obj->obj) {
2060 		err = bpf_program__set_attach_target(prog, profile_tgt_fd,
2061 						     profile_tgt_name);
2062 		if (err) {
2063 			p_err("failed to set attach target\n");
2064 			goto out;
2065 		}
2066 	}
2067 
2068 	set_max_rlimit();
2069 	err = profiler_bpf__load(profile_obj);
2070 	if (err) {
2071 		p_err("failed to load profile_obj");
2072 		goto out;
2073 	}
2074 
2075 	err = profile_open_perf_events(profile_obj);
2076 	if (err)
2077 		goto out;
2078 
2079 	err = profiler_bpf__attach(profile_obj);
2080 	if (err) {
2081 		p_err("failed to attach profile_obj");
2082 		goto out;
2083 	}
2084 	signal(SIGINT, int_exit);
2085 
2086 	sleep(duration);
2087 	profile_print_and_cleanup();
2088 	return 0;
2089 
2090 out:
2091 	profile_close_perf_events(profile_obj);
2092 	if (profile_obj)
2093 		profiler_bpf__destroy(profile_obj);
2094 	close(profile_tgt_fd);
2095 	free(profile_tgt_name);
2096 	return err;
2097 }
2098 
2099 #endif /* BPFTOOL_WITHOUT_SKELETONS */
2100 
2101 static int do_help(int argc, char **argv)
2102 {
2103 	if (json_output) {
2104 		jsonw_null(json_wtr);
2105 		return 0;
2106 	}
2107 
2108 	fprintf(stderr,
2109 		"Usage: %1$s %2$s { show | list } [PROG]\n"
2110 		"       %1$s %2$s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
2111 		"       %1$s %2$s dump jited  PROG [{ file FILE | opcodes | linum }]\n"
2112 		"       %1$s %2$s pin   PROG FILE\n"
2113 		"       %1$s %2$s { load | loadall } OBJ  PATH \\\n"
2114 		"                         [type TYPE] [dev NAME] \\\n"
2115 		"                         [map { idx IDX | name NAME } MAP]\\\n"
2116 		"                         [pinmaps MAP_DIR]\n"
2117 		"       %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n"
2118 		"       %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n"
2119 		"       %1$s %2$s run PROG \\\n"
2120 		"                         data_in FILE \\\n"
2121 		"                         [data_out FILE [data_size_out L]] \\\n"
2122 		"                         [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
2123 		"                         [repeat N]\n"
2124 		"       %1$s %2$s profile PROG [duration DURATION] METRICs\n"
2125 		"       %1$s %2$s tracelog\n"
2126 		"       %1$s %2$s help\n"
2127 		"\n"
2128 		"       " HELP_SPEC_MAP "\n"
2129 		"       " HELP_SPEC_PROGRAM "\n"
2130 		"       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
2131 		"                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
2132 		"                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
2133 		"                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
2134 		"                 sk_reuseport | flow_dissector | cgroup/sysctl |\n"
2135 		"                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
2136 		"                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
2137 		"                 cgroup/getpeername4 | cgroup/getpeername6 |\n"
2138 		"                 cgroup/getsockname4 | cgroup/getsockname6 | cgroup/sendmsg4 |\n"
2139 		"                 cgroup/sendmsg6 | cgroup/recvmsg4 | cgroup/recvmsg6 |\n"
2140 		"                 cgroup/getsockopt | cgroup/setsockopt |\n"
2141 		"                 struct_ops | fentry | fexit | freplace | sk_lookup }\n"
2142 		"       ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n"
2143 		"                        flow_dissector }\n"
2144 		"       METRIC := { cycles | instructions | l1d_loads | llc_misses | itlb_misses | dtlb_misses }\n"
2145 		"       " HELP_SPEC_OPTIONS "\n"
2146 		"",
2147 		bin_name, argv[-2]);
2148 
2149 	return 0;
2150 }
2151 
2152 static const struct cmd cmds[] = {
2153 	{ "show",	do_show },
2154 	{ "list",	do_show },
2155 	{ "help",	do_help },
2156 	{ "dump",	do_dump },
2157 	{ "pin",	do_pin },
2158 	{ "load",	do_load },
2159 	{ "loadall",	do_loadall },
2160 	{ "attach",	do_attach },
2161 	{ "detach",	do_detach },
2162 	{ "tracelog",	do_tracelog },
2163 	{ "run",	do_run },
2164 	{ "profile",	do_profile },
2165 	{ 0 }
2166 };
2167 
2168 int do_prog(int argc, char **argv)
2169 {
2170 	return cmd_select(cmds, argc, argv, do_help);
2171 }
2172