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