xref: /aosp_15_r20/external/bpftool/src/link.c (revision 858ea5e570667251cdc31d3fe7b846b591105938)
1*858ea5e5SAndroid Build Coastguard Worker // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2*858ea5e5SAndroid Build Coastguard Worker /* Copyright (C) 2020 Facebook */
3*858ea5e5SAndroid Build Coastguard Worker 
4*858ea5e5SAndroid Build Coastguard Worker #include <errno.h>
5*858ea5e5SAndroid Build Coastguard Worker #include <linux/err.h>
6*858ea5e5SAndroid Build Coastguard Worker #include <netinet/in.h>
7*858ea5e5SAndroid Build Coastguard Worker #include <linux/netfilter.h>
8*858ea5e5SAndroid Build Coastguard Worker #include <linux/netfilter_arp.h>
9*858ea5e5SAndroid Build Coastguard Worker #include <linux/perf_event.h>
10*858ea5e5SAndroid Build Coastguard Worker #include <net/if.h>
11*858ea5e5SAndroid Build Coastguard Worker #include <stdio.h>
12*858ea5e5SAndroid Build Coastguard Worker #include <unistd.h>
13*858ea5e5SAndroid Build Coastguard Worker 
14*858ea5e5SAndroid Build Coastguard Worker #include <bpf/bpf.h>
15*858ea5e5SAndroid Build Coastguard Worker #include <bpf/hashmap.h>
16*858ea5e5SAndroid Build Coastguard Worker 
17*858ea5e5SAndroid Build Coastguard Worker #include "json_writer.h"
18*858ea5e5SAndroid Build Coastguard Worker #include "main.h"
19*858ea5e5SAndroid Build Coastguard Worker #include "xlated_dumper.h"
20*858ea5e5SAndroid Build Coastguard Worker 
21*858ea5e5SAndroid Build Coastguard Worker #define PERF_HW_CACHE_LEN 128
22*858ea5e5SAndroid Build Coastguard Worker 
23*858ea5e5SAndroid Build Coastguard Worker static struct hashmap *link_table;
24*858ea5e5SAndroid Build Coastguard Worker static struct dump_data dd;
25*858ea5e5SAndroid Build Coastguard Worker 
26*858ea5e5SAndroid Build Coastguard Worker static const char *perf_type_name[PERF_TYPE_MAX] = {
27*858ea5e5SAndroid Build Coastguard Worker 	[PERF_TYPE_HARDWARE]			= "hardware",
28*858ea5e5SAndroid Build Coastguard Worker 	[PERF_TYPE_SOFTWARE]			= "software",
29*858ea5e5SAndroid Build Coastguard Worker 	[PERF_TYPE_TRACEPOINT]			= "tracepoint",
30*858ea5e5SAndroid Build Coastguard Worker 	[PERF_TYPE_HW_CACHE]			= "hw-cache",
31*858ea5e5SAndroid Build Coastguard Worker 	[PERF_TYPE_RAW]				= "raw",
32*858ea5e5SAndroid Build Coastguard Worker 	[PERF_TYPE_BREAKPOINT]			= "breakpoint",
33*858ea5e5SAndroid Build Coastguard Worker };
34*858ea5e5SAndroid Build Coastguard Worker 
35*858ea5e5SAndroid Build Coastguard Worker const char *event_symbols_hw[PERF_COUNT_HW_MAX] = {
36*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CPU_CYCLES]		= "cpu-cycles",
37*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_INSTRUCTIONS]		= "instructions",
38*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_REFERENCES]	= "cache-references",
39*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_MISSES]		= "cache-misses",
40*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= "branch-instructions",
41*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_BRANCH_MISSES]		= "branch-misses",
42*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_BUS_CYCLES]		= "bus-cycles",
43*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= "stalled-cycles-frontend",
44*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND]	= "stalled-cycles-backend",
45*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_REF_CPU_CYCLES]		= "ref-cycles",
46*858ea5e5SAndroid Build Coastguard Worker };
47*858ea5e5SAndroid Build Coastguard Worker 
48*858ea5e5SAndroid Build Coastguard Worker const char *event_symbols_sw[PERF_COUNT_SW_MAX] = {
49*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_CPU_CLOCK]		= "cpu-clock",
50*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_TASK_CLOCK]		= "task-clock",
51*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_PAGE_FAULTS]		= "page-faults",
52*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_CONTEXT_SWITCHES]	= "context-switches",
53*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_CPU_MIGRATIONS]		= "cpu-migrations",
54*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_PAGE_FAULTS_MIN]		= "minor-faults",
55*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ]		= "major-faults",
56*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_ALIGNMENT_FAULTS]	= "alignment-faults",
57*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_EMULATION_FAULTS]	= "emulation-faults",
58*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_DUMMY]			= "dummy",
59*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_BPF_OUTPUT]		= "bpf-output",
60*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_SW_CGROUP_SWITCHES]		= "cgroup-switches",
61*858ea5e5SAndroid Build Coastguard Worker };
62*858ea5e5SAndroid Build Coastguard Worker 
63*858ea5e5SAndroid Build Coastguard Worker const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] = {
64*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_L1D]		= "L1-dcache",
65*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_L1I]		= "L1-icache",
66*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_LL]		= "LLC",
67*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_DTLB]		= "dTLB",
68*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_ITLB]		= "iTLB",
69*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_BPU]		= "branch",
70*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_NODE]		= "node",
71*858ea5e5SAndroid Build Coastguard Worker };
72*858ea5e5SAndroid Build Coastguard Worker 
73*858ea5e5SAndroid Build Coastguard Worker const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] = {
74*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_OP_READ]		= "load",
75*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_OP_WRITE]		= "store",
76*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_OP_PREFETCH]	= "prefetch",
77*858ea5e5SAndroid Build Coastguard Worker };
78*858ea5e5SAndroid Build Coastguard Worker 
79*858ea5e5SAndroid Build Coastguard Worker const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] = {
80*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_RESULT_ACCESS]	= "refs",
81*858ea5e5SAndroid Build Coastguard Worker 	[PERF_COUNT_HW_CACHE_RESULT_MISS]	= "misses",
82*858ea5e5SAndroid Build Coastguard Worker };
83*858ea5e5SAndroid Build Coastguard Worker 
84*858ea5e5SAndroid Build Coastguard Worker #define perf_event_name(array, id) ({			\
85*858ea5e5SAndroid Build Coastguard Worker 	const char *event_str = NULL;			\
86*858ea5e5SAndroid Build Coastguard Worker 							\
87*858ea5e5SAndroid Build Coastguard Worker 	if ((id) < ARRAY_SIZE(array))			\
88*858ea5e5SAndroid Build Coastguard Worker 		event_str = array[id];			\
89*858ea5e5SAndroid Build Coastguard Worker 	event_str;					\
90*858ea5e5SAndroid Build Coastguard Worker })
91*858ea5e5SAndroid Build Coastguard Worker 
link_parse_fd(int * argc,char *** argv)92*858ea5e5SAndroid Build Coastguard Worker static int link_parse_fd(int *argc, char ***argv)
93*858ea5e5SAndroid Build Coastguard Worker {
94*858ea5e5SAndroid Build Coastguard Worker 	int fd;
95*858ea5e5SAndroid Build Coastguard Worker 
96*858ea5e5SAndroid Build Coastguard Worker 	if (is_prefix(**argv, "id")) {
97*858ea5e5SAndroid Build Coastguard Worker 		unsigned int id;
98*858ea5e5SAndroid Build Coastguard Worker 		char *endptr;
99*858ea5e5SAndroid Build Coastguard Worker 
100*858ea5e5SAndroid Build Coastguard Worker 		NEXT_ARGP();
101*858ea5e5SAndroid Build Coastguard Worker 
102*858ea5e5SAndroid Build Coastguard Worker 		id = strtoul(**argv, &endptr, 0);
103*858ea5e5SAndroid Build Coastguard Worker 		if (*endptr) {
104*858ea5e5SAndroid Build Coastguard Worker 			p_err("can't parse %s as ID", **argv);
105*858ea5e5SAndroid Build Coastguard Worker 			return -1;
106*858ea5e5SAndroid Build Coastguard Worker 		}
107*858ea5e5SAndroid Build Coastguard Worker 		NEXT_ARGP();
108*858ea5e5SAndroid Build Coastguard Worker 
109*858ea5e5SAndroid Build Coastguard Worker 		fd = bpf_link_get_fd_by_id(id);
110*858ea5e5SAndroid Build Coastguard Worker 		if (fd < 0)
111*858ea5e5SAndroid Build Coastguard Worker 			p_err("failed to get link with ID %d: %s", id, strerror(errno));
112*858ea5e5SAndroid Build Coastguard Worker 		return fd;
113*858ea5e5SAndroid Build Coastguard Worker 	} else if (is_prefix(**argv, "pinned")) {
114*858ea5e5SAndroid Build Coastguard Worker 		char *path;
115*858ea5e5SAndroid Build Coastguard Worker 
116*858ea5e5SAndroid Build Coastguard Worker 		NEXT_ARGP();
117*858ea5e5SAndroid Build Coastguard Worker 
118*858ea5e5SAndroid Build Coastguard Worker 		path = **argv;
119*858ea5e5SAndroid Build Coastguard Worker 		NEXT_ARGP();
120*858ea5e5SAndroid Build Coastguard Worker 
121*858ea5e5SAndroid Build Coastguard Worker 		return open_obj_pinned_any(path, BPF_OBJ_LINK);
122*858ea5e5SAndroid Build Coastguard Worker 	}
123*858ea5e5SAndroid Build Coastguard Worker 
124*858ea5e5SAndroid Build Coastguard Worker 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
125*858ea5e5SAndroid Build Coastguard Worker 	return -1;
126*858ea5e5SAndroid Build Coastguard Worker }
127*858ea5e5SAndroid Build Coastguard Worker 
128*858ea5e5SAndroid Build Coastguard Worker static void
show_link_header_json(struct bpf_link_info * info,json_writer_t * wtr)129*858ea5e5SAndroid Build Coastguard Worker show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
130*858ea5e5SAndroid Build Coastguard Worker {
131*858ea5e5SAndroid Build Coastguard Worker 	const char *link_type_str;
132*858ea5e5SAndroid Build Coastguard Worker 
133*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "id", info->id);
134*858ea5e5SAndroid Build Coastguard Worker 	link_type_str = libbpf_bpf_link_type_str(info->type);
135*858ea5e5SAndroid Build Coastguard Worker 	if (link_type_str)
136*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(wtr, "type", link_type_str);
137*858ea5e5SAndroid Build Coastguard Worker 	else
138*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(wtr, "type", info->type);
139*858ea5e5SAndroid Build Coastguard Worker 
140*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
141*858ea5e5SAndroid Build Coastguard Worker }
142*858ea5e5SAndroid Build Coastguard Worker 
show_link_attach_type_json(__u32 attach_type,json_writer_t * wtr)143*858ea5e5SAndroid Build Coastguard Worker static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
144*858ea5e5SAndroid Build Coastguard Worker {
145*858ea5e5SAndroid Build Coastguard Worker 	const char *attach_type_str;
146*858ea5e5SAndroid Build Coastguard Worker 
147*858ea5e5SAndroid Build Coastguard Worker 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
148*858ea5e5SAndroid Build Coastguard Worker 	if (attach_type_str)
149*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(wtr, "attach_type", attach_type_str);
150*858ea5e5SAndroid Build Coastguard Worker 	else
151*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(wtr, "attach_type", attach_type);
152*858ea5e5SAndroid Build Coastguard Worker }
153*858ea5e5SAndroid Build Coastguard Worker 
show_link_ifindex_json(__u32 ifindex,json_writer_t * wtr)154*858ea5e5SAndroid Build Coastguard Worker static void show_link_ifindex_json(__u32 ifindex, json_writer_t *wtr)
155*858ea5e5SAndroid Build Coastguard Worker {
156*858ea5e5SAndroid Build Coastguard Worker 	char devname[IF_NAMESIZE] = "(unknown)";
157*858ea5e5SAndroid Build Coastguard Worker 
158*858ea5e5SAndroid Build Coastguard Worker 	if (ifindex)
159*858ea5e5SAndroid Build Coastguard Worker 		if_indextoname(ifindex, devname);
160*858ea5e5SAndroid Build Coastguard Worker 	else
161*858ea5e5SAndroid Build Coastguard Worker 		snprintf(devname, sizeof(devname), "(detached)");
162*858ea5e5SAndroid Build Coastguard Worker 	jsonw_string_field(wtr, "devname", devname);
163*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "ifindex", ifindex);
164*858ea5e5SAndroid Build Coastguard Worker }
165*858ea5e5SAndroid Build Coastguard Worker 
is_iter_map_target(const char * target_name)166*858ea5e5SAndroid Build Coastguard Worker static bool is_iter_map_target(const char *target_name)
167*858ea5e5SAndroid Build Coastguard Worker {
168*858ea5e5SAndroid Build Coastguard Worker 	return strcmp(target_name, "bpf_map_elem") == 0 ||
169*858ea5e5SAndroid Build Coastguard Worker 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
170*858ea5e5SAndroid Build Coastguard Worker }
171*858ea5e5SAndroid Build Coastguard Worker 
is_iter_cgroup_target(const char * target_name)172*858ea5e5SAndroid Build Coastguard Worker static bool is_iter_cgroup_target(const char *target_name)
173*858ea5e5SAndroid Build Coastguard Worker {
174*858ea5e5SAndroid Build Coastguard Worker 	return strcmp(target_name, "cgroup") == 0;
175*858ea5e5SAndroid Build Coastguard Worker }
176*858ea5e5SAndroid Build Coastguard Worker 
cgroup_order_string(__u32 order)177*858ea5e5SAndroid Build Coastguard Worker static const char *cgroup_order_string(__u32 order)
178*858ea5e5SAndroid Build Coastguard Worker {
179*858ea5e5SAndroid Build Coastguard Worker 	switch (order) {
180*858ea5e5SAndroid Build Coastguard Worker 	case BPF_CGROUP_ITER_ORDER_UNSPEC:
181*858ea5e5SAndroid Build Coastguard Worker 		return "order_unspec";
182*858ea5e5SAndroid Build Coastguard Worker 	case BPF_CGROUP_ITER_SELF_ONLY:
183*858ea5e5SAndroid Build Coastguard Worker 		return "self_only";
184*858ea5e5SAndroid Build Coastguard Worker 	case BPF_CGROUP_ITER_DESCENDANTS_PRE:
185*858ea5e5SAndroid Build Coastguard Worker 		return "descendants_pre";
186*858ea5e5SAndroid Build Coastguard Worker 	case BPF_CGROUP_ITER_DESCENDANTS_POST:
187*858ea5e5SAndroid Build Coastguard Worker 		return "descendants_post";
188*858ea5e5SAndroid Build Coastguard Worker 	case BPF_CGROUP_ITER_ANCESTORS_UP:
189*858ea5e5SAndroid Build Coastguard Worker 		return "ancestors_up";
190*858ea5e5SAndroid Build Coastguard Worker 	default: /* won't happen */
191*858ea5e5SAndroid Build Coastguard Worker 		return "unknown";
192*858ea5e5SAndroid Build Coastguard Worker 	}
193*858ea5e5SAndroid Build Coastguard Worker }
194*858ea5e5SAndroid Build Coastguard Worker 
is_iter_task_target(const char * target_name)195*858ea5e5SAndroid Build Coastguard Worker static bool is_iter_task_target(const char *target_name)
196*858ea5e5SAndroid Build Coastguard Worker {
197*858ea5e5SAndroid Build Coastguard Worker 	return strcmp(target_name, "task") == 0 ||
198*858ea5e5SAndroid Build Coastguard Worker 		strcmp(target_name, "task_file") == 0 ||
199*858ea5e5SAndroid Build Coastguard Worker 		strcmp(target_name, "task_vma") == 0;
200*858ea5e5SAndroid Build Coastguard Worker }
201*858ea5e5SAndroid Build Coastguard Worker 
show_iter_json(struct bpf_link_info * info,json_writer_t * wtr)202*858ea5e5SAndroid Build Coastguard Worker static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
203*858ea5e5SAndroid Build Coastguard Worker {
204*858ea5e5SAndroid Build Coastguard Worker 	const char *target_name = u64_to_ptr(info->iter.target_name);
205*858ea5e5SAndroid Build Coastguard Worker 
206*858ea5e5SAndroid Build Coastguard Worker 	jsonw_string_field(wtr, "target_name", target_name);
207*858ea5e5SAndroid Build Coastguard Worker 
208*858ea5e5SAndroid Build Coastguard Worker 	if (is_iter_map_target(target_name))
209*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
210*858ea5e5SAndroid Build Coastguard Worker 	else if (is_iter_task_target(target_name)) {
211*858ea5e5SAndroid Build Coastguard Worker 		if (info->iter.task.tid)
212*858ea5e5SAndroid Build Coastguard Worker 			jsonw_uint_field(wtr, "tid", info->iter.task.tid);
213*858ea5e5SAndroid Build Coastguard Worker 		else if (info->iter.task.pid)
214*858ea5e5SAndroid Build Coastguard Worker 			jsonw_uint_field(wtr, "pid", info->iter.task.pid);
215*858ea5e5SAndroid Build Coastguard Worker 	}
216*858ea5e5SAndroid Build Coastguard Worker 
217*858ea5e5SAndroid Build Coastguard Worker 	if (is_iter_cgroup_target(target_name)) {
218*858ea5e5SAndroid Build Coastguard Worker 		jsonw_lluint_field(wtr, "cgroup_id", info->iter.cgroup.cgroup_id);
219*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(wtr, "order",
220*858ea5e5SAndroid Build Coastguard Worker 				   cgroup_order_string(info->iter.cgroup.order));
221*858ea5e5SAndroid Build Coastguard Worker 	}
222*858ea5e5SAndroid Build Coastguard Worker }
223*858ea5e5SAndroid Build Coastguard Worker 
netfilter_dump_json(const struct bpf_link_info * info,json_writer_t * wtr)224*858ea5e5SAndroid Build Coastguard Worker void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr)
225*858ea5e5SAndroid Build Coastguard Worker {
226*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "pf",
227*858ea5e5SAndroid Build Coastguard Worker 			 info->netfilter.pf);
228*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "hook",
229*858ea5e5SAndroid Build Coastguard Worker 			 info->netfilter.hooknum);
230*858ea5e5SAndroid Build Coastguard Worker 	jsonw_int_field(json_wtr, "prio",
231*858ea5e5SAndroid Build Coastguard Worker 			 info->netfilter.priority);
232*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "flags",
233*858ea5e5SAndroid Build Coastguard Worker 			 info->netfilter.flags);
234*858ea5e5SAndroid Build Coastguard Worker }
235*858ea5e5SAndroid Build Coastguard Worker 
get_prog_info(int prog_id,struct bpf_prog_info * info)236*858ea5e5SAndroid Build Coastguard Worker static int get_prog_info(int prog_id, struct bpf_prog_info *info)
237*858ea5e5SAndroid Build Coastguard Worker {
238*858ea5e5SAndroid Build Coastguard Worker 	__u32 len = sizeof(*info);
239*858ea5e5SAndroid Build Coastguard Worker 	int err, prog_fd;
240*858ea5e5SAndroid Build Coastguard Worker 
241*858ea5e5SAndroid Build Coastguard Worker 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
242*858ea5e5SAndroid Build Coastguard Worker 	if (prog_fd < 0)
243*858ea5e5SAndroid Build Coastguard Worker 		return prog_fd;
244*858ea5e5SAndroid Build Coastguard Worker 
245*858ea5e5SAndroid Build Coastguard Worker 	memset(info, 0, sizeof(*info));
246*858ea5e5SAndroid Build Coastguard Worker 	err = bpf_prog_get_info_by_fd(prog_fd, info, &len);
247*858ea5e5SAndroid Build Coastguard Worker 	if (err)
248*858ea5e5SAndroid Build Coastguard Worker 		p_err("can't get prog info: %s", strerror(errno));
249*858ea5e5SAndroid Build Coastguard Worker 	close(prog_fd);
250*858ea5e5SAndroid Build Coastguard Worker 	return err;
251*858ea5e5SAndroid Build Coastguard Worker }
252*858ea5e5SAndroid Build Coastguard Worker 
253*858ea5e5SAndroid Build Coastguard Worker struct addr_cookie {
254*858ea5e5SAndroid Build Coastguard Worker 	__u64 addr;
255*858ea5e5SAndroid Build Coastguard Worker 	__u64 cookie;
256*858ea5e5SAndroid Build Coastguard Worker };
257*858ea5e5SAndroid Build Coastguard Worker 
cmp_addr_cookie(const void * A,const void * B)258*858ea5e5SAndroid Build Coastguard Worker static int cmp_addr_cookie(const void *A, const void *B)
259*858ea5e5SAndroid Build Coastguard Worker {
260*858ea5e5SAndroid Build Coastguard Worker 	const struct addr_cookie *a = A, *b = B;
261*858ea5e5SAndroid Build Coastguard Worker 
262*858ea5e5SAndroid Build Coastguard Worker 	if (a->addr == b->addr)
263*858ea5e5SAndroid Build Coastguard Worker 		return 0;
264*858ea5e5SAndroid Build Coastguard Worker 	return a->addr < b->addr ? -1 : 1;
265*858ea5e5SAndroid Build Coastguard Worker }
266*858ea5e5SAndroid Build Coastguard Worker 
267*858ea5e5SAndroid Build Coastguard Worker static struct addr_cookie *
get_addr_cookie_array(__u64 * addrs,__u64 * cookies,__u32 count)268*858ea5e5SAndroid Build Coastguard Worker get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count)
269*858ea5e5SAndroid Build Coastguard Worker {
270*858ea5e5SAndroid Build Coastguard Worker 	struct addr_cookie *data;
271*858ea5e5SAndroid Build Coastguard Worker 	__u32 i;
272*858ea5e5SAndroid Build Coastguard Worker 
273*858ea5e5SAndroid Build Coastguard Worker 	data = calloc(count, sizeof(data[0]));
274*858ea5e5SAndroid Build Coastguard Worker 	if (!data) {
275*858ea5e5SAndroid Build Coastguard Worker 		p_err("mem alloc failed");
276*858ea5e5SAndroid Build Coastguard Worker 		return NULL;
277*858ea5e5SAndroid Build Coastguard Worker 	}
278*858ea5e5SAndroid Build Coastguard Worker 	for (i = 0; i < count; i++) {
279*858ea5e5SAndroid Build Coastguard Worker 		data[i].addr = addrs[i];
280*858ea5e5SAndroid Build Coastguard Worker 		data[i].cookie = cookies[i];
281*858ea5e5SAndroid Build Coastguard Worker 	}
282*858ea5e5SAndroid Build Coastguard Worker 	qsort(data, count, sizeof(data[0]), cmp_addr_cookie);
283*858ea5e5SAndroid Build Coastguard Worker 	return data;
284*858ea5e5SAndroid Build Coastguard Worker }
285*858ea5e5SAndroid Build Coastguard Worker 
286*858ea5e5SAndroid Build Coastguard Worker static void
show_kprobe_multi_json(struct bpf_link_info * info,json_writer_t * wtr)287*858ea5e5SAndroid Build Coastguard Worker show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
288*858ea5e5SAndroid Build Coastguard Worker {
289*858ea5e5SAndroid Build Coastguard Worker 	struct addr_cookie *data;
290*858ea5e5SAndroid Build Coastguard Worker 	__u32 i, j = 0;
291*858ea5e5SAndroid Build Coastguard Worker 
292*858ea5e5SAndroid Build Coastguard Worker 	jsonw_bool_field(json_wtr, "retprobe",
293*858ea5e5SAndroid Build Coastguard Worker 			 info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN);
294*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
295*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "missed", info->kprobe_multi.missed);
296*858ea5e5SAndroid Build Coastguard Worker 	jsonw_name(json_wtr, "funcs");
297*858ea5e5SAndroid Build Coastguard Worker 	jsonw_start_array(json_wtr);
298*858ea5e5SAndroid Build Coastguard Worker 	data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs),
299*858ea5e5SAndroid Build Coastguard Worker 				     u64_to_ptr(info->kprobe_multi.cookies),
300*858ea5e5SAndroid Build Coastguard Worker 				     info->kprobe_multi.count);
301*858ea5e5SAndroid Build Coastguard Worker 	if (!data)
302*858ea5e5SAndroid Build Coastguard Worker 		return;
303*858ea5e5SAndroid Build Coastguard Worker 
304*858ea5e5SAndroid Build Coastguard Worker 	/* Load it once for all. */
305*858ea5e5SAndroid Build Coastguard Worker 	if (!dd.sym_count)
306*858ea5e5SAndroid Build Coastguard Worker 		kernel_syms_load(&dd);
307*858ea5e5SAndroid Build Coastguard Worker 	if (!dd.sym_count)
308*858ea5e5SAndroid Build Coastguard Worker 		goto error;
309*858ea5e5SAndroid Build Coastguard Worker 
310*858ea5e5SAndroid Build Coastguard Worker 	for (i = 0; i < dd.sym_count; i++) {
311*858ea5e5SAndroid Build Coastguard Worker 		if (dd.sym_mapping[i].address != data[j].addr)
312*858ea5e5SAndroid Build Coastguard Worker 			continue;
313*858ea5e5SAndroid Build Coastguard Worker 		jsonw_start_object(json_wtr);
314*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "addr", dd.sym_mapping[i].address);
315*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(json_wtr, "func", dd.sym_mapping[i].name);
316*858ea5e5SAndroid Build Coastguard Worker 		/* Print null if it is vmlinux */
317*858ea5e5SAndroid Build Coastguard Worker 		if (dd.sym_mapping[i].module[0] == '\0') {
318*858ea5e5SAndroid Build Coastguard Worker 			jsonw_name(json_wtr, "module");
319*858ea5e5SAndroid Build Coastguard Worker 			jsonw_null(json_wtr);
320*858ea5e5SAndroid Build Coastguard Worker 		} else {
321*858ea5e5SAndroid Build Coastguard Worker 			jsonw_string_field(json_wtr, "module", dd.sym_mapping[i].module);
322*858ea5e5SAndroid Build Coastguard Worker 		}
323*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "cookie", data[j].cookie);
324*858ea5e5SAndroid Build Coastguard Worker 		jsonw_end_object(json_wtr);
325*858ea5e5SAndroid Build Coastguard Worker 		if (j++ == info->kprobe_multi.count)
326*858ea5e5SAndroid Build Coastguard Worker 			break;
327*858ea5e5SAndroid Build Coastguard Worker 	}
328*858ea5e5SAndroid Build Coastguard Worker 	jsonw_end_array(json_wtr);
329*858ea5e5SAndroid Build Coastguard Worker error:
330*858ea5e5SAndroid Build Coastguard Worker 	free(data);
331*858ea5e5SAndroid Build Coastguard Worker }
332*858ea5e5SAndroid Build Coastguard Worker 
u64_to_arr(__u64 val)333*858ea5e5SAndroid Build Coastguard Worker static __u64 *u64_to_arr(__u64 val)
334*858ea5e5SAndroid Build Coastguard Worker {
335*858ea5e5SAndroid Build Coastguard Worker 	return (__u64 *) u64_to_ptr(val);
336*858ea5e5SAndroid Build Coastguard Worker }
337*858ea5e5SAndroid Build Coastguard Worker 
338*858ea5e5SAndroid Build Coastguard Worker static void
show_uprobe_multi_json(struct bpf_link_info * info,json_writer_t * wtr)339*858ea5e5SAndroid Build Coastguard Worker show_uprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
340*858ea5e5SAndroid Build Coastguard Worker {
341*858ea5e5SAndroid Build Coastguard Worker 	__u32 i;
342*858ea5e5SAndroid Build Coastguard Worker 
343*858ea5e5SAndroid Build Coastguard Worker 	jsonw_bool_field(json_wtr, "retprobe",
344*858ea5e5SAndroid Build Coastguard Worker 			 info->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN);
345*858ea5e5SAndroid Build Coastguard Worker 	jsonw_string_field(json_wtr, "path", (char *) u64_to_ptr(info->uprobe_multi.path));
346*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(json_wtr, "func_cnt", info->uprobe_multi.count);
347*858ea5e5SAndroid Build Coastguard Worker 	jsonw_int_field(json_wtr, "pid", (int) info->uprobe_multi.pid);
348*858ea5e5SAndroid Build Coastguard Worker 	jsonw_name(json_wtr, "funcs");
349*858ea5e5SAndroid Build Coastguard Worker 	jsonw_start_array(json_wtr);
350*858ea5e5SAndroid Build Coastguard Worker 
351*858ea5e5SAndroid Build Coastguard Worker 	for (i = 0; i < info->uprobe_multi.count; i++) {
352*858ea5e5SAndroid Build Coastguard Worker 		jsonw_start_object(json_wtr);
353*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "offset",
354*858ea5e5SAndroid Build Coastguard Worker 				 u64_to_arr(info->uprobe_multi.offsets)[i]);
355*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "ref_ctr_offset",
356*858ea5e5SAndroid Build Coastguard Worker 				 u64_to_arr(info->uprobe_multi.ref_ctr_offsets)[i]);
357*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "cookie",
358*858ea5e5SAndroid Build Coastguard Worker 				 u64_to_arr(info->uprobe_multi.cookies)[i]);
359*858ea5e5SAndroid Build Coastguard Worker 		jsonw_end_object(json_wtr);
360*858ea5e5SAndroid Build Coastguard Worker 	}
361*858ea5e5SAndroid Build Coastguard Worker 	jsonw_end_array(json_wtr);
362*858ea5e5SAndroid Build Coastguard Worker }
363*858ea5e5SAndroid Build Coastguard Worker 
364*858ea5e5SAndroid Build Coastguard Worker static void
show_perf_event_kprobe_json(struct bpf_link_info * info,json_writer_t * wtr)365*858ea5e5SAndroid Build Coastguard Worker show_perf_event_kprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
366*858ea5e5SAndroid Build Coastguard Worker {
367*858ea5e5SAndroid Build Coastguard Worker 	jsonw_bool_field(wtr, "retprobe", info->perf_event.type == BPF_PERF_EVENT_KRETPROBE);
368*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "addr", info->perf_event.kprobe.addr);
369*858ea5e5SAndroid Build Coastguard Worker 	jsonw_string_field(wtr, "func",
370*858ea5e5SAndroid Build Coastguard Worker 			   u64_to_ptr(info->perf_event.kprobe.func_name));
371*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "offset", info->perf_event.kprobe.offset);
372*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "missed", info->perf_event.kprobe.missed);
373*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "cookie", info->perf_event.kprobe.cookie);
374*858ea5e5SAndroid Build Coastguard Worker }
375*858ea5e5SAndroid Build Coastguard Worker 
376*858ea5e5SAndroid Build Coastguard Worker static void
show_perf_event_uprobe_json(struct bpf_link_info * info,json_writer_t * wtr)377*858ea5e5SAndroid Build Coastguard Worker show_perf_event_uprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
378*858ea5e5SAndroid Build Coastguard Worker {
379*858ea5e5SAndroid Build Coastguard Worker 	jsonw_bool_field(wtr, "retprobe", info->perf_event.type == BPF_PERF_EVENT_URETPROBE);
380*858ea5e5SAndroid Build Coastguard Worker 	jsonw_string_field(wtr, "file",
381*858ea5e5SAndroid Build Coastguard Worker 			   u64_to_ptr(info->perf_event.uprobe.file_name));
382*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "offset", info->perf_event.uprobe.offset);
383*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "cookie", info->perf_event.uprobe.cookie);
384*858ea5e5SAndroid Build Coastguard Worker }
385*858ea5e5SAndroid Build Coastguard Worker 
386*858ea5e5SAndroid Build Coastguard Worker static void
show_perf_event_tracepoint_json(struct bpf_link_info * info,json_writer_t * wtr)387*858ea5e5SAndroid Build Coastguard Worker show_perf_event_tracepoint_json(struct bpf_link_info *info, json_writer_t *wtr)
388*858ea5e5SAndroid Build Coastguard Worker {
389*858ea5e5SAndroid Build Coastguard Worker 	jsonw_string_field(wtr, "tracepoint",
390*858ea5e5SAndroid Build Coastguard Worker 			   u64_to_ptr(info->perf_event.tracepoint.tp_name));
391*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "cookie", info->perf_event.tracepoint.cookie);
392*858ea5e5SAndroid Build Coastguard Worker }
393*858ea5e5SAndroid Build Coastguard Worker 
perf_config_hw_cache_str(__u64 config)394*858ea5e5SAndroid Build Coastguard Worker static char *perf_config_hw_cache_str(__u64 config)
395*858ea5e5SAndroid Build Coastguard Worker {
396*858ea5e5SAndroid Build Coastguard Worker 	const char *hw_cache, *result, *op;
397*858ea5e5SAndroid Build Coastguard Worker 	char *str = malloc(PERF_HW_CACHE_LEN);
398*858ea5e5SAndroid Build Coastguard Worker 
399*858ea5e5SAndroid Build Coastguard Worker 	if (!str) {
400*858ea5e5SAndroid Build Coastguard Worker 		p_err("mem alloc failed");
401*858ea5e5SAndroid Build Coastguard Worker 		return NULL;
402*858ea5e5SAndroid Build Coastguard Worker 	}
403*858ea5e5SAndroid Build Coastguard Worker 
404*858ea5e5SAndroid Build Coastguard Worker 	hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
405*858ea5e5SAndroid Build Coastguard Worker 	if (hw_cache)
406*858ea5e5SAndroid Build Coastguard Worker 		snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache);
407*858ea5e5SAndroid Build Coastguard Worker 	else
408*858ea5e5SAndroid Build Coastguard Worker 		snprintf(str, PERF_HW_CACHE_LEN, "%lld-", config & 0xff);
409*858ea5e5SAndroid Build Coastguard Worker 
410*858ea5e5SAndroid Build Coastguard Worker 	op = perf_event_name(evsel__hw_cache_op, (config >> 8) & 0xff);
411*858ea5e5SAndroid Build Coastguard Worker 	if (op)
412*858ea5e5SAndroid Build Coastguard Worker 		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
413*858ea5e5SAndroid Build Coastguard Worker 			 "%s-", op);
414*858ea5e5SAndroid Build Coastguard Worker 	else
415*858ea5e5SAndroid Build Coastguard Worker 		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
416*858ea5e5SAndroid Build Coastguard Worker 			 "%lld-", (config >> 8) & 0xff);
417*858ea5e5SAndroid Build Coastguard Worker 
418*858ea5e5SAndroid Build Coastguard Worker 	result = perf_event_name(evsel__hw_cache_result, config >> 16);
419*858ea5e5SAndroid Build Coastguard Worker 	if (result)
420*858ea5e5SAndroid Build Coastguard Worker 		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
421*858ea5e5SAndroid Build Coastguard Worker 			 "%s", result);
422*858ea5e5SAndroid Build Coastguard Worker 	else
423*858ea5e5SAndroid Build Coastguard Worker 		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
424*858ea5e5SAndroid Build Coastguard Worker 			 "%lld", config >> 16);
425*858ea5e5SAndroid Build Coastguard Worker 	return str;
426*858ea5e5SAndroid Build Coastguard Worker }
427*858ea5e5SAndroid Build Coastguard Worker 
perf_config_str(__u32 type,__u64 config)428*858ea5e5SAndroid Build Coastguard Worker static const char *perf_config_str(__u32 type, __u64 config)
429*858ea5e5SAndroid Build Coastguard Worker {
430*858ea5e5SAndroid Build Coastguard Worker 	const char *perf_config;
431*858ea5e5SAndroid Build Coastguard Worker 
432*858ea5e5SAndroid Build Coastguard Worker 	switch (type) {
433*858ea5e5SAndroid Build Coastguard Worker 	case PERF_TYPE_HARDWARE:
434*858ea5e5SAndroid Build Coastguard Worker 		perf_config = perf_event_name(event_symbols_hw, config);
435*858ea5e5SAndroid Build Coastguard Worker 		break;
436*858ea5e5SAndroid Build Coastguard Worker 	case PERF_TYPE_SOFTWARE:
437*858ea5e5SAndroid Build Coastguard Worker 		perf_config = perf_event_name(event_symbols_sw, config);
438*858ea5e5SAndroid Build Coastguard Worker 		break;
439*858ea5e5SAndroid Build Coastguard Worker 	case PERF_TYPE_HW_CACHE:
440*858ea5e5SAndroid Build Coastguard Worker 		perf_config = perf_config_hw_cache_str(config);
441*858ea5e5SAndroid Build Coastguard Worker 		break;
442*858ea5e5SAndroid Build Coastguard Worker 	default:
443*858ea5e5SAndroid Build Coastguard Worker 		perf_config = NULL;
444*858ea5e5SAndroid Build Coastguard Worker 		break;
445*858ea5e5SAndroid Build Coastguard Worker 	}
446*858ea5e5SAndroid Build Coastguard Worker 	return perf_config;
447*858ea5e5SAndroid Build Coastguard Worker }
448*858ea5e5SAndroid Build Coastguard Worker 
449*858ea5e5SAndroid Build Coastguard Worker static void
show_perf_event_event_json(struct bpf_link_info * info,json_writer_t * wtr)450*858ea5e5SAndroid Build Coastguard Worker show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr)
451*858ea5e5SAndroid Build Coastguard Worker {
452*858ea5e5SAndroid Build Coastguard Worker 	__u64 config = info->perf_event.event.config;
453*858ea5e5SAndroid Build Coastguard Worker 	__u32 type = info->perf_event.event.type;
454*858ea5e5SAndroid Build Coastguard Worker 	const char *perf_type, *perf_config;
455*858ea5e5SAndroid Build Coastguard Worker 
456*858ea5e5SAndroid Build Coastguard Worker 	perf_type = perf_event_name(perf_type_name, type);
457*858ea5e5SAndroid Build Coastguard Worker 	if (perf_type)
458*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(wtr, "event_type", perf_type);
459*858ea5e5SAndroid Build Coastguard Worker 	else
460*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(wtr, "event_type", type);
461*858ea5e5SAndroid Build Coastguard Worker 
462*858ea5e5SAndroid Build Coastguard Worker 	perf_config = perf_config_str(type, config);
463*858ea5e5SAndroid Build Coastguard Worker 	if (perf_config)
464*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(wtr, "event_config", perf_config);
465*858ea5e5SAndroid Build Coastguard Worker 	else
466*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(wtr, "event_config", config);
467*858ea5e5SAndroid Build Coastguard Worker 
468*858ea5e5SAndroid Build Coastguard Worker 	jsonw_uint_field(wtr, "cookie", info->perf_event.event.cookie);
469*858ea5e5SAndroid Build Coastguard Worker 
470*858ea5e5SAndroid Build Coastguard Worker 	if (type == PERF_TYPE_HW_CACHE && perf_config)
471*858ea5e5SAndroid Build Coastguard Worker 		free((void *)perf_config);
472*858ea5e5SAndroid Build Coastguard Worker }
473*858ea5e5SAndroid Build Coastguard Worker 
show_link_close_json(int fd,struct bpf_link_info * info)474*858ea5e5SAndroid Build Coastguard Worker static int show_link_close_json(int fd, struct bpf_link_info *info)
475*858ea5e5SAndroid Build Coastguard Worker {
476*858ea5e5SAndroid Build Coastguard Worker 	struct bpf_prog_info prog_info;
477*858ea5e5SAndroid Build Coastguard Worker 	const char *prog_type_str;
478*858ea5e5SAndroid Build Coastguard Worker 	int err;
479*858ea5e5SAndroid Build Coastguard Worker 
480*858ea5e5SAndroid Build Coastguard Worker 	jsonw_start_object(json_wtr);
481*858ea5e5SAndroid Build Coastguard Worker 
482*858ea5e5SAndroid Build Coastguard Worker 	show_link_header_json(info, json_wtr);
483*858ea5e5SAndroid Build Coastguard Worker 
484*858ea5e5SAndroid Build Coastguard Worker 	switch (info->type) {
485*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
486*858ea5e5SAndroid Build Coastguard Worker 		jsonw_string_field(json_wtr, "tp_name",
487*858ea5e5SAndroid Build Coastguard Worker 				   u64_to_ptr(info->raw_tracepoint.tp_name));
488*858ea5e5SAndroid Build Coastguard Worker 		break;
489*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_TRACING:
490*858ea5e5SAndroid Build Coastguard Worker 		err = get_prog_info(info->prog_id, &prog_info);
491*858ea5e5SAndroid Build Coastguard Worker 		if (err)
492*858ea5e5SAndroid Build Coastguard Worker 			return err;
493*858ea5e5SAndroid Build Coastguard Worker 
494*858ea5e5SAndroid Build Coastguard Worker 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
495*858ea5e5SAndroid Build Coastguard Worker 		/* libbpf will return NULL for variants unknown to it. */
496*858ea5e5SAndroid Build Coastguard Worker 		if (prog_type_str)
497*858ea5e5SAndroid Build Coastguard Worker 			jsonw_string_field(json_wtr, "prog_type", prog_type_str);
498*858ea5e5SAndroid Build Coastguard Worker 		else
499*858ea5e5SAndroid Build Coastguard Worker 			jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
500*858ea5e5SAndroid Build Coastguard Worker 
501*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_json(info->tracing.attach_type,
502*858ea5e5SAndroid Build Coastguard Worker 					   json_wtr);
503*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "target_obj_id", info->tracing.target_obj_id);
504*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "target_btf_id", info->tracing.target_btf_id);
505*858ea5e5SAndroid Build Coastguard Worker 		break;
506*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_CGROUP:
507*858ea5e5SAndroid Build Coastguard Worker 		jsonw_lluint_field(json_wtr, "cgroup_id",
508*858ea5e5SAndroid Build Coastguard Worker 				   info->cgroup.cgroup_id);
509*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
510*858ea5e5SAndroid Build Coastguard Worker 		break;
511*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_ITER:
512*858ea5e5SAndroid Build Coastguard Worker 		show_iter_json(info, json_wtr);
513*858ea5e5SAndroid Build Coastguard Worker 		break;
514*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_NETNS:
515*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "netns_ino",
516*858ea5e5SAndroid Build Coastguard Worker 				 info->netns.netns_ino);
517*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_json(info->netns.attach_type, json_wtr);
518*858ea5e5SAndroid Build Coastguard Worker 		break;
519*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_NETFILTER:
520*858ea5e5SAndroid Build Coastguard Worker 		netfilter_dump_json(info, json_wtr);
521*858ea5e5SAndroid Build Coastguard Worker 		break;
522*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_TCX:
523*858ea5e5SAndroid Build Coastguard Worker 		show_link_ifindex_json(info->tcx.ifindex, json_wtr);
524*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_json(info->tcx.attach_type, json_wtr);
525*858ea5e5SAndroid Build Coastguard Worker 		break;
526*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_NETKIT:
527*858ea5e5SAndroid Build Coastguard Worker 		show_link_ifindex_json(info->netkit.ifindex, json_wtr);
528*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_json(info->netkit.attach_type, json_wtr);
529*858ea5e5SAndroid Build Coastguard Worker 		break;
530*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_XDP:
531*858ea5e5SAndroid Build Coastguard Worker 		show_link_ifindex_json(info->xdp.ifindex, json_wtr);
532*858ea5e5SAndroid Build Coastguard Worker 		break;
533*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_STRUCT_OPS:
534*858ea5e5SAndroid Build Coastguard Worker 		jsonw_uint_field(json_wtr, "map_id",
535*858ea5e5SAndroid Build Coastguard Worker 				 info->struct_ops.map_id);
536*858ea5e5SAndroid Build Coastguard Worker 		break;
537*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_KPROBE_MULTI:
538*858ea5e5SAndroid Build Coastguard Worker 		show_kprobe_multi_json(info, json_wtr);
539*858ea5e5SAndroid Build Coastguard Worker 		break;
540*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_UPROBE_MULTI:
541*858ea5e5SAndroid Build Coastguard Worker 		show_uprobe_multi_json(info, json_wtr);
542*858ea5e5SAndroid Build Coastguard Worker 		break;
543*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_PERF_EVENT:
544*858ea5e5SAndroid Build Coastguard Worker 		switch (info->perf_event.type) {
545*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_EVENT:
546*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_event_json(info, json_wtr);
547*858ea5e5SAndroid Build Coastguard Worker 			break;
548*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_TRACEPOINT:
549*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_tracepoint_json(info, json_wtr);
550*858ea5e5SAndroid Build Coastguard Worker 			break;
551*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_KPROBE:
552*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_KRETPROBE:
553*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_kprobe_json(info, json_wtr);
554*858ea5e5SAndroid Build Coastguard Worker 			break;
555*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_UPROBE:
556*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_URETPROBE:
557*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_uprobe_json(info, json_wtr);
558*858ea5e5SAndroid Build Coastguard Worker 			break;
559*858ea5e5SAndroid Build Coastguard Worker 		default:
560*858ea5e5SAndroid Build Coastguard Worker 			break;
561*858ea5e5SAndroid Build Coastguard Worker 		}
562*858ea5e5SAndroid Build Coastguard Worker 		break;
563*858ea5e5SAndroid Build Coastguard Worker 	default:
564*858ea5e5SAndroid Build Coastguard Worker 		break;
565*858ea5e5SAndroid Build Coastguard Worker 	}
566*858ea5e5SAndroid Build Coastguard Worker 
567*858ea5e5SAndroid Build Coastguard Worker 	if (!hashmap__empty(link_table)) {
568*858ea5e5SAndroid Build Coastguard Worker 		struct hashmap_entry *entry;
569*858ea5e5SAndroid Build Coastguard Worker 
570*858ea5e5SAndroid Build Coastguard Worker 		jsonw_name(json_wtr, "pinned");
571*858ea5e5SAndroid Build Coastguard Worker 		jsonw_start_array(json_wtr);
572*858ea5e5SAndroid Build Coastguard Worker 		hashmap__for_each_key_entry(link_table, entry, info->id)
573*858ea5e5SAndroid Build Coastguard Worker 			jsonw_string(json_wtr, entry->pvalue);
574*858ea5e5SAndroid Build Coastguard Worker 		jsonw_end_array(json_wtr);
575*858ea5e5SAndroid Build Coastguard Worker 	}
576*858ea5e5SAndroid Build Coastguard Worker 
577*858ea5e5SAndroid Build Coastguard Worker 	emit_obj_refs_json(refs_table, info->id, json_wtr);
578*858ea5e5SAndroid Build Coastguard Worker 
579*858ea5e5SAndroid Build Coastguard Worker 	jsonw_end_object(json_wtr);
580*858ea5e5SAndroid Build Coastguard Worker 
581*858ea5e5SAndroid Build Coastguard Worker 	return 0;
582*858ea5e5SAndroid Build Coastguard Worker }
583*858ea5e5SAndroid Build Coastguard Worker 
show_link_header_plain(struct bpf_link_info * info)584*858ea5e5SAndroid Build Coastguard Worker static void show_link_header_plain(struct bpf_link_info *info)
585*858ea5e5SAndroid Build Coastguard Worker {
586*858ea5e5SAndroid Build Coastguard Worker 	const char *link_type_str;
587*858ea5e5SAndroid Build Coastguard Worker 
588*858ea5e5SAndroid Build Coastguard Worker 	printf("%u: ", info->id);
589*858ea5e5SAndroid Build Coastguard Worker 	link_type_str = libbpf_bpf_link_type_str(info->type);
590*858ea5e5SAndroid Build Coastguard Worker 	if (link_type_str)
591*858ea5e5SAndroid Build Coastguard Worker 		printf("%s  ", link_type_str);
592*858ea5e5SAndroid Build Coastguard Worker 	else
593*858ea5e5SAndroid Build Coastguard Worker 		printf("type %u  ", info->type);
594*858ea5e5SAndroid Build Coastguard Worker 
595*858ea5e5SAndroid Build Coastguard Worker 	if (info->type == BPF_LINK_TYPE_STRUCT_OPS)
596*858ea5e5SAndroid Build Coastguard Worker 		printf("map %u  ", info->struct_ops.map_id);
597*858ea5e5SAndroid Build Coastguard Worker 	else
598*858ea5e5SAndroid Build Coastguard Worker 		printf("prog %u  ", info->prog_id);
599*858ea5e5SAndroid Build Coastguard Worker }
600*858ea5e5SAndroid Build Coastguard Worker 
show_link_attach_type_plain(__u32 attach_type)601*858ea5e5SAndroid Build Coastguard Worker static void show_link_attach_type_plain(__u32 attach_type)
602*858ea5e5SAndroid Build Coastguard Worker {
603*858ea5e5SAndroid Build Coastguard Worker 	const char *attach_type_str;
604*858ea5e5SAndroid Build Coastguard Worker 
605*858ea5e5SAndroid Build Coastguard Worker 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
606*858ea5e5SAndroid Build Coastguard Worker 	if (attach_type_str)
607*858ea5e5SAndroid Build Coastguard Worker 		printf("attach_type %s  ", attach_type_str);
608*858ea5e5SAndroid Build Coastguard Worker 	else
609*858ea5e5SAndroid Build Coastguard Worker 		printf("attach_type %u  ", attach_type);
610*858ea5e5SAndroid Build Coastguard Worker }
611*858ea5e5SAndroid Build Coastguard Worker 
show_link_ifindex_plain(__u32 ifindex)612*858ea5e5SAndroid Build Coastguard Worker static void show_link_ifindex_plain(__u32 ifindex)
613*858ea5e5SAndroid Build Coastguard Worker {
614*858ea5e5SAndroid Build Coastguard Worker 	char devname[IF_NAMESIZE * 2] = "(unknown)";
615*858ea5e5SAndroid Build Coastguard Worker 	char tmpname[IF_NAMESIZE];
616*858ea5e5SAndroid Build Coastguard Worker 	char *ret = NULL;
617*858ea5e5SAndroid Build Coastguard Worker 
618*858ea5e5SAndroid Build Coastguard Worker 	if (ifindex)
619*858ea5e5SAndroid Build Coastguard Worker 		ret = if_indextoname(ifindex, tmpname);
620*858ea5e5SAndroid Build Coastguard Worker 	else
621*858ea5e5SAndroid Build Coastguard Worker 		snprintf(devname, sizeof(devname), "(detached)");
622*858ea5e5SAndroid Build Coastguard Worker 	if (ret)
623*858ea5e5SAndroid Build Coastguard Worker 		snprintf(devname, sizeof(devname), "%s(%d)",
624*858ea5e5SAndroid Build Coastguard Worker 			 tmpname, ifindex);
625*858ea5e5SAndroid Build Coastguard Worker 	printf("ifindex %s  ", devname);
626*858ea5e5SAndroid Build Coastguard Worker }
627*858ea5e5SAndroid Build Coastguard Worker 
show_iter_plain(struct bpf_link_info * info)628*858ea5e5SAndroid Build Coastguard Worker static void show_iter_plain(struct bpf_link_info *info)
629*858ea5e5SAndroid Build Coastguard Worker {
630*858ea5e5SAndroid Build Coastguard Worker 	const char *target_name = u64_to_ptr(info->iter.target_name);
631*858ea5e5SAndroid Build Coastguard Worker 
632*858ea5e5SAndroid Build Coastguard Worker 	printf("target_name %s  ", target_name);
633*858ea5e5SAndroid Build Coastguard Worker 
634*858ea5e5SAndroid Build Coastguard Worker 	if (is_iter_map_target(target_name))
635*858ea5e5SAndroid Build Coastguard Worker 		printf("map_id %u  ", info->iter.map.map_id);
636*858ea5e5SAndroid Build Coastguard Worker 	else if (is_iter_task_target(target_name)) {
637*858ea5e5SAndroid Build Coastguard Worker 		if (info->iter.task.tid)
638*858ea5e5SAndroid Build Coastguard Worker 			printf("tid %u ", info->iter.task.tid);
639*858ea5e5SAndroid Build Coastguard Worker 		else if (info->iter.task.pid)
640*858ea5e5SAndroid Build Coastguard Worker 			printf("pid %u ", info->iter.task.pid);
641*858ea5e5SAndroid Build Coastguard Worker 	}
642*858ea5e5SAndroid Build Coastguard Worker 
643*858ea5e5SAndroid Build Coastguard Worker 	if (is_iter_cgroup_target(target_name)) {
644*858ea5e5SAndroid Build Coastguard Worker 		printf("cgroup_id %llu  ", info->iter.cgroup.cgroup_id);
645*858ea5e5SAndroid Build Coastguard Worker 		printf("order %s  ",
646*858ea5e5SAndroid Build Coastguard Worker 		       cgroup_order_string(info->iter.cgroup.order));
647*858ea5e5SAndroid Build Coastguard Worker 	}
648*858ea5e5SAndroid Build Coastguard Worker }
649*858ea5e5SAndroid Build Coastguard Worker 
650*858ea5e5SAndroid Build Coastguard Worker static const char * const pf2name[] = {
651*858ea5e5SAndroid Build Coastguard Worker 	[NFPROTO_INET] = "inet",
652*858ea5e5SAndroid Build Coastguard Worker 	[NFPROTO_IPV4] = "ip",
653*858ea5e5SAndroid Build Coastguard Worker 	[NFPROTO_ARP] = "arp",
654*858ea5e5SAndroid Build Coastguard Worker 	[NFPROTO_NETDEV] = "netdev",
655*858ea5e5SAndroid Build Coastguard Worker 	[NFPROTO_BRIDGE] = "bridge",
656*858ea5e5SAndroid Build Coastguard Worker 	[NFPROTO_IPV6] = "ip6",
657*858ea5e5SAndroid Build Coastguard Worker };
658*858ea5e5SAndroid Build Coastguard Worker 
659*858ea5e5SAndroid Build Coastguard Worker static const char * const inethook2name[] = {
660*858ea5e5SAndroid Build Coastguard Worker 	[NF_INET_PRE_ROUTING] = "prerouting",
661*858ea5e5SAndroid Build Coastguard Worker 	[NF_INET_LOCAL_IN] = "input",
662*858ea5e5SAndroid Build Coastguard Worker 	[NF_INET_FORWARD] = "forward",
663*858ea5e5SAndroid Build Coastguard Worker 	[NF_INET_LOCAL_OUT] = "output",
664*858ea5e5SAndroid Build Coastguard Worker 	[NF_INET_POST_ROUTING] = "postrouting",
665*858ea5e5SAndroid Build Coastguard Worker };
666*858ea5e5SAndroid Build Coastguard Worker 
667*858ea5e5SAndroid Build Coastguard Worker static const char * const arphook2name[] = {
668*858ea5e5SAndroid Build Coastguard Worker 	[NF_ARP_IN] = "input",
669*858ea5e5SAndroid Build Coastguard Worker 	[NF_ARP_OUT] = "output",
670*858ea5e5SAndroid Build Coastguard Worker };
671*858ea5e5SAndroid Build Coastguard Worker 
netfilter_dump_plain(const struct bpf_link_info * info)672*858ea5e5SAndroid Build Coastguard Worker void netfilter_dump_plain(const struct bpf_link_info *info)
673*858ea5e5SAndroid Build Coastguard Worker {
674*858ea5e5SAndroid Build Coastguard Worker 	const char *hookname = NULL, *pfname = NULL;
675*858ea5e5SAndroid Build Coastguard Worker 	unsigned int hook = info->netfilter.hooknum;
676*858ea5e5SAndroid Build Coastguard Worker 	unsigned int pf = info->netfilter.pf;
677*858ea5e5SAndroid Build Coastguard Worker 
678*858ea5e5SAndroid Build Coastguard Worker 	if (pf < ARRAY_SIZE(pf2name))
679*858ea5e5SAndroid Build Coastguard Worker 		pfname = pf2name[pf];
680*858ea5e5SAndroid Build Coastguard Worker 
681*858ea5e5SAndroid Build Coastguard Worker 	switch (pf) {
682*858ea5e5SAndroid Build Coastguard Worker 	case NFPROTO_BRIDGE: /* bridge shares numbers with enum nf_inet_hooks */
683*858ea5e5SAndroid Build Coastguard Worker 	case NFPROTO_IPV4:
684*858ea5e5SAndroid Build Coastguard Worker 	case NFPROTO_IPV6:
685*858ea5e5SAndroid Build Coastguard Worker 	case NFPROTO_INET:
686*858ea5e5SAndroid Build Coastguard Worker 		if (hook < ARRAY_SIZE(inethook2name))
687*858ea5e5SAndroid Build Coastguard Worker 			hookname = inethook2name[hook];
688*858ea5e5SAndroid Build Coastguard Worker 		break;
689*858ea5e5SAndroid Build Coastguard Worker 	case NFPROTO_ARP:
690*858ea5e5SAndroid Build Coastguard Worker 		if (hook < ARRAY_SIZE(arphook2name))
691*858ea5e5SAndroid Build Coastguard Worker 			hookname = arphook2name[hook];
692*858ea5e5SAndroid Build Coastguard Worker 	default:
693*858ea5e5SAndroid Build Coastguard Worker 		break;
694*858ea5e5SAndroid Build Coastguard Worker 	}
695*858ea5e5SAndroid Build Coastguard Worker 
696*858ea5e5SAndroid Build Coastguard Worker 	if (pfname)
697*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\t%s", pfname);
698*858ea5e5SAndroid Build Coastguard Worker 	else
699*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tpf: %d", pf);
700*858ea5e5SAndroid Build Coastguard Worker 
701*858ea5e5SAndroid Build Coastguard Worker 	if (hookname)
702*858ea5e5SAndroid Build Coastguard Worker 		printf(" %s", hookname);
703*858ea5e5SAndroid Build Coastguard Worker 	else
704*858ea5e5SAndroid Build Coastguard Worker 		printf(", hook %u,", hook);
705*858ea5e5SAndroid Build Coastguard Worker 
706*858ea5e5SAndroid Build Coastguard Worker 	printf(" prio %d", info->netfilter.priority);
707*858ea5e5SAndroid Build Coastguard Worker 
708*858ea5e5SAndroid Build Coastguard Worker 	if (info->netfilter.flags)
709*858ea5e5SAndroid Build Coastguard Worker 		printf(" flags 0x%x", info->netfilter.flags);
710*858ea5e5SAndroid Build Coastguard Worker }
711*858ea5e5SAndroid Build Coastguard Worker 
show_kprobe_multi_plain(struct bpf_link_info * info)712*858ea5e5SAndroid Build Coastguard Worker static void show_kprobe_multi_plain(struct bpf_link_info *info)
713*858ea5e5SAndroid Build Coastguard Worker {
714*858ea5e5SAndroid Build Coastguard Worker 	struct addr_cookie *data;
715*858ea5e5SAndroid Build Coastguard Worker 	__u32 i, j = 0;
716*858ea5e5SAndroid Build Coastguard Worker 
717*858ea5e5SAndroid Build Coastguard Worker 	if (!info->kprobe_multi.count)
718*858ea5e5SAndroid Build Coastguard Worker 		return;
719*858ea5e5SAndroid Build Coastguard Worker 
720*858ea5e5SAndroid Build Coastguard Worker 	if (info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN)
721*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tkretprobe.multi  ");
722*858ea5e5SAndroid Build Coastguard Worker 	else
723*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tkprobe.multi  ");
724*858ea5e5SAndroid Build Coastguard Worker 	printf("func_cnt %u  ", info->kprobe_multi.count);
725*858ea5e5SAndroid Build Coastguard Worker 	if (info->kprobe_multi.missed)
726*858ea5e5SAndroid Build Coastguard Worker 		printf("missed %llu  ", info->kprobe_multi.missed);
727*858ea5e5SAndroid Build Coastguard Worker 	data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs),
728*858ea5e5SAndroid Build Coastguard Worker 				     u64_to_ptr(info->kprobe_multi.cookies),
729*858ea5e5SAndroid Build Coastguard Worker 				     info->kprobe_multi.count);
730*858ea5e5SAndroid Build Coastguard Worker 	if (!data)
731*858ea5e5SAndroid Build Coastguard Worker 		return;
732*858ea5e5SAndroid Build Coastguard Worker 
733*858ea5e5SAndroid Build Coastguard Worker 	/* Load it once for all. */
734*858ea5e5SAndroid Build Coastguard Worker 	if (!dd.sym_count)
735*858ea5e5SAndroid Build Coastguard Worker 		kernel_syms_load(&dd);
736*858ea5e5SAndroid Build Coastguard Worker 	if (!dd.sym_count)
737*858ea5e5SAndroid Build Coastguard Worker 		goto error;
738*858ea5e5SAndroid Build Coastguard Worker 
739*858ea5e5SAndroid Build Coastguard Worker 	printf("\n\t%-16s %-16s %s", "addr", "cookie", "func [module]");
740*858ea5e5SAndroid Build Coastguard Worker 	for (i = 0; i < dd.sym_count; i++) {
741*858ea5e5SAndroid Build Coastguard Worker 		if (dd.sym_mapping[i].address != data[j].addr)
742*858ea5e5SAndroid Build Coastguard Worker 			continue;
743*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\t%016lx %-16llx %s",
744*858ea5e5SAndroid Build Coastguard Worker 		       dd.sym_mapping[i].address, data[j].cookie, dd.sym_mapping[i].name);
745*858ea5e5SAndroid Build Coastguard Worker 		if (dd.sym_mapping[i].module[0] != '\0')
746*858ea5e5SAndroid Build Coastguard Worker 			printf(" [%s]  ", dd.sym_mapping[i].module);
747*858ea5e5SAndroid Build Coastguard Worker 		else
748*858ea5e5SAndroid Build Coastguard Worker 			printf("  ");
749*858ea5e5SAndroid Build Coastguard Worker 
750*858ea5e5SAndroid Build Coastguard Worker 		if (j++ == info->kprobe_multi.count)
751*858ea5e5SAndroid Build Coastguard Worker 			break;
752*858ea5e5SAndroid Build Coastguard Worker 	}
753*858ea5e5SAndroid Build Coastguard Worker error:
754*858ea5e5SAndroid Build Coastguard Worker 	free(data);
755*858ea5e5SAndroid Build Coastguard Worker }
756*858ea5e5SAndroid Build Coastguard Worker 
show_uprobe_multi_plain(struct bpf_link_info * info)757*858ea5e5SAndroid Build Coastguard Worker static void show_uprobe_multi_plain(struct bpf_link_info *info)
758*858ea5e5SAndroid Build Coastguard Worker {
759*858ea5e5SAndroid Build Coastguard Worker 	__u32 i;
760*858ea5e5SAndroid Build Coastguard Worker 
761*858ea5e5SAndroid Build Coastguard Worker 	if (!info->uprobe_multi.count)
762*858ea5e5SAndroid Build Coastguard Worker 		return;
763*858ea5e5SAndroid Build Coastguard Worker 
764*858ea5e5SAndroid Build Coastguard Worker 	if (info->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN)
765*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\turetprobe.multi  ");
766*858ea5e5SAndroid Build Coastguard Worker 	else
767*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tuprobe.multi  ");
768*858ea5e5SAndroid Build Coastguard Worker 
769*858ea5e5SAndroid Build Coastguard Worker 	printf("path %s  ", (char *) u64_to_ptr(info->uprobe_multi.path));
770*858ea5e5SAndroid Build Coastguard Worker 	printf("func_cnt %u  ", info->uprobe_multi.count);
771*858ea5e5SAndroid Build Coastguard Worker 
772*858ea5e5SAndroid Build Coastguard Worker 	if (info->uprobe_multi.pid)
773*858ea5e5SAndroid Build Coastguard Worker 		printf("pid %d  ", info->uprobe_multi.pid);
774*858ea5e5SAndroid Build Coastguard Worker 
775*858ea5e5SAndroid Build Coastguard Worker 	printf("\n\t%-16s   %-16s   %-16s", "offset", "ref_ctr_offset", "cookies");
776*858ea5e5SAndroid Build Coastguard Worker 	for (i = 0; i < info->uprobe_multi.count; i++) {
777*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\t0x%-16llx 0x%-16llx 0x%-16llx",
778*858ea5e5SAndroid Build Coastguard Worker 			u64_to_arr(info->uprobe_multi.offsets)[i],
779*858ea5e5SAndroid Build Coastguard Worker 			u64_to_arr(info->uprobe_multi.ref_ctr_offsets)[i],
780*858ea5e5SAndroid Build Coastguard Worker 			u64_to_arr(info->uprobe_multi.cookies)[i]);
781*858ea5e5SAndroid Build Coastguard Worker 	}
782*858ea5e5SAndroid Build Coastguard Worker }
783*858ea5e5SAndroid Build Coastguard Worker 
show_perf_event_kprobe_plain(struct bpf_link_info * info)784*858ea5e5SAndroid Build Coastguard Worker static void show_perf_event_kprobe_plain(struct bpf_link_info *info)
785*858ea5e5SAndroid Build Coastguard Worker {
786*858ea5e5SAndroid Build Coastguard Worker 	const char *buf;
787*858ea5e5SAndroid Build Coastguard Worker 
788*858ea5e5SAndroid Build Coastguard Worker 	buf = u64_to_ptr(info->perf_event.kprobe.func_name);
789*858ea5e5SAndroid Build Coastguard Worker 	if (buf[0] == '\0' && !info->perf_event.kprobe.addr)
790*858ea5e5SAndroid Build Coastguard Worker 		return;
791*858ea5e5SAndroid Build Coastguard Worker 
792*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.type == BPF_PERF_EVENT_KRETPROBE)
793*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tkretprobe ");
794*858ea5e5SAndroid Build Coastguard Worker 	else
795*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tkprobe ");
796*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.kprobe.addr)
797*858ea5e5SAndroid Build Coastguard Worker 		printf("%llx ", info->perf_event.kprobe.addr);
798*858ea5e5SAndroid Build Coastguard Worker 	printf("%s", buf);
799*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.kprobe.offset)
800*858ea5e5SAndroid Build Coastguard Worker 		printf("+%#x", info->perf_event.kprobe.offset);
801*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.kprobe.missed)
802*858ea5e5SAndroid Build Coastguard Worker 		printf("  missed %llu", info->perf_event.kprobe.missed);
803*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.kprobe.cookie)
804*858ea5e5SAndroid Build Coastguard Worker 		printf("  cookie %llu", info->perf_event.kprobe.cookie);
805*858ea5e5SAndroid Build Coastguard Worker 	printf("  ");
806*858ea5e5SAndroid Build Coastguard Worker }
807*858ea5e5SAndroid Build Coastguard Worker 
show_perf_event_uprobe_plain(struct bpf_link_info * info)808*858ea5e5SAndroid Build Coastguard Worker static void show_perf_event_uprobe_plain(struct bpf_link_info *info)
809*858ea5e5SAndroid Build Coastguard Worker {
810*858ea5e5SAndroid Build Coastguard Worker 	const char *buf;
811*858ea5e5SAndroid Build Coastguard Worker 
812*858ea5e5SAndroid Build Coastguard Worker 	buf = u64_to_ptr(info->perf_event.uprobe.file_name);
813*858ea5e5SAndroid Build Coastguard Worker 	if (buf[0] == '\0')
814*858ea5e5SAndroid Build Coastguard Worker 		return;
815*858ea5e5SAndroid Build Coastguard Worker 
816*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.type == BPF_PERF_EVENT_URETPROBE)
817*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\turetprobe ");
818*858ea5e5SAndroid Build Coastguard Worker 	else
819*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tuprobe ");
820*858ea5e5SAndroid Build Coastguard Worker 	printf("%s+%#x  ", buf, info->perf_event.uprobe.offset);
821*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.uprobe.cookie)
822*858ea5e5SAndroid Build Coastguard Worker 		printf("cookie %llu  ", info->perf_event.uprobe.cookie);
823*858ea5e5SAndroid Build Coastguard Worker }
824*858ea5e5SAndroid Build Coastguard Worker 
show_perf_event_tracepoint_plain(struct bpf_link_info * info)825*858ea5e5SAndroid Build Coastguard Worker static void show_perf_event_tracepoint_plain(struct bpf_link_info *info)
826*858ea5e5SAndroid Build Coastguard Worker {
827*858ea5e5SAndroid Build Coastguard Worker 	const char *buf;
828*858ea5e5SAndroid Build Coastguard Worker 
829*858ea5e5SAndroid Build Coastguard Worker 	buf = u64_to_ptr(info->perf_event.tracepoint.tp_name);
830*858ea5e5SAndroid Build Coastguard Worker 	if (buf[0] == '\0')
831*858ea5e5SAndroid Build Coastguard Worker 		return;
832*858ea5e5SAndroid Build Coastguard Worker 
833*858ea5e5SAndroid Build Coastguard Worker 	printf("\n\ttracepoint %s  ", buf);
834*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.tracepoint.cookie)
835*858ea5e5SAndroid Build Coastguard Worker 		printf("cookie %llu  ", info->perf_event.tracepoint.cookie);
836*858ea5e5SAndroid Build Coastguard Worker }
837*858ea5e5SAndroid Build Coastguard Worker 
show_perf_event_event_plain(struct bpf_link_info * info)838*858ea5e5SAndroid Build Coastguard Worker static void show_perf_event_event_plain(struct bpf_link_info *info)
839*858ea5e5SAndroid Build Coastguard Worker {
840*858ea5e5SAndroid Build Coastguard Worker 	__u64 config = info->perf_event.event.config;
841*858ea5e5SAndroid Build Coastguard Worker 	__u32 type = info->perf_event.event.type;
842*858ea5e5SAndroid Build Coastguard Worker 	const char *perf_type, *perf_config;
843*858ea5e5SAndroid Build Coastguard Worker 
844*858ea5e5SAndroid Build Coastguard Worker 	printf("\n\tevent ");
845*858ea5e5SAndroid Build Coastguard Worker 	perf_type = perf_event_name(perf_type_name, type);
846*858ea5e5SAndroid Build Coastguard Worker 	if (perf_type)
847*858ea5e5SAndroid Build Coastguard Worker 		printf("%s:", perf_type);
848*858ea5e5SAndroid Build Coastguard Worker 	else
849*858ea5e5SAndroid Build Coastguard Worker 		printf("%u :", type);
850*858ea5e5SAndroid Build Coastguard Worker 
851*858ea5e5SAndroid Build Coastguard Worker 	perf_config = perf_config_str(type, config);
852*858ea5e5SAndroid Build Coastguard Worker 	if (perf_config)
853*858ea5e5SAndroid Build Coastguard Worker 		printf("%s  ", perf_config);
854*858ea5e5SAndroid Build Coastguard Worker 	else
855*858ea5e5SAndroid Build Coastguard Worker 		printf("%llu  ", config);
856*858ea5e5SAndroid Build Coastguard Worker 
857*858ea5e5SAndroid Build Coastguard Worker 	if (info->perf_event.event.cookie)
858*858ea5e5SAndroid Build Coastguard Worker 		printf("cookie %llu  ", info->perf_event.event.cookie);
859*858ea5e5SAndroid Build Coastguard Worker 
860*858ea5e5SAndroid Build Coastguard Worker 	if (type == PERF_TYPE_HW_CACHE && perf_config)
861*858ea5e5SAndroid Build Coastguard Worker 		free((void *)perf_config);
862*858ea5e5SAndroid Build Coastguard Worker }
863*858ea5e5SAndroid Build Coastguard Worker 
show_link_close_plain(int fd,struct bpf_link_info * info)864*858ea5e5SAndroid Build Coastguard Worker static int show_link_close_plain(int fd, struct bpf_link_info *info)
865*858ea5e5SAndroid Build Coastguard Worker {
866*858ea5e5SAndroid Build Coastguard Worker 	struct bpf_prog_info prog_info;
867*858ea5e5SAndroid Build Coastguard Worker 	const char *prog_type_str;
868*858ea5e5SAndroid Build Coastguard Worker 	int err;
869*858ea5e5SAndroid Build Coastguard Worker 
870*858ea5e5SAndroid Build Coastguard Worker 	show_link_header_plain(info);
871*858ea5e5SAndroid Build Coastguard Worker 
872*858ea5e5SAndroid Build Coastguard Worker 	switch (info->type) {
873*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
874*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\ttp '%s'  ",
875*858ea5e5SAndroid Build Coastguard Worker 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
876*858ea5e5SAndroid Build Coastguard Worker 		break;
877*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_TRACING:
878*858ea5e5SAndroid Build Coastguard Worker 		err = get_prog_info(info->prog_id, &prog_info);
879*858ea5e5SAndroid Build Coastguard Worker 		if (err)
880*858ea5e5SAndroid Build Coastguard Worker 			return err;
881*858ea5e5SAndroid Build Coastguard Worker 
882*858ea5e5SAndroid Build Coastguard Worker 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
883*858ea5e5SAndroid Build Coastguard Worker 		/* libbpf will return NULL for variants unknown to it. */
884*858ea5e5SAndroid Build Coastguard Worker 		if (prog_type_str)
885*858ea5e5SAndroid Build Coastguard Worker 			printf("\n\tprog_type %s  ", prog_type_str);
886*858ea5e5SAndroid Build Coastguard Worker 		else
887*858ea5e5SAndroid Build Coastguard Worker 			printf("\n\tprog_type %u  ", prog_info.type);
888*858ea5e5SAndroid Build Coastguard Worker 
889*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_plain(info->tracing.attach_type);
890*858ea5e5SAndroid Build Coastguard Worker 		if (info->tracing.target_obj_id || info->tracing.target_btf_id)
891*858ea5e5SAndroid Build Coastguard Worker 			printf("\n\ttarget_obj_id %u  target_btf_id %u  ",
892*858ea5e5SAndroid Build Coastguard Worker 			       info->tracing.target_obj_id,
893*858ea5e5SAndroid Build Coastguard Worker 			       info->tracing.target_btf_id);
894*858ea5e5SAndroid Build Coastguard Worker 		break;
895*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_CGROUP:
896*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
897*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_plain(info->cgroup.attach_type);
898*858ea5e5SAndroid Build Coastguard Worker 		break;
899*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_ITER:
900*858ea5e5SAndroid Build Coastguard Worker 		show_iter_plain(info);
901*858ea5e5SAndroid Build Coastguard Worker 		break;
902*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_NETNS:
903*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
904*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_plain(info->netns.attach_type);
905*858ea5e5SAndroid Build Coastguard Worker 		break;
906*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_NETFILTER:
907*858ea5e5SAndroid Build Coastguard Worker 		netfilter_dump_plain(info);
908*858ea5e5SAndroid Build Coastguard Worker 		break;
909*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_TCX:
910*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\t");
911*858ea5e5SAndroid Build Coastguard Worker 		show_link_ifindex_plain(info->tcx.ifindex);
912*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_plain(info->tcx.attach_type);
913*858ea5e5SAndroid Build Coastguard Worker 		break;
914*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_NETKIT:
915*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\t");
916*858ea5e5SAndroid Build Coastguard Worker 		show_link_ifindex_plain(info->netkit.ifindex);
917*858ea5e5SAndroid Build Coastguard Worker 		show_link_attach_type_plain(info->netkit.attach_type);
918*858ea5e5SAndroid Build Coastguard Worker 		break;
919*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_XDP:
920*858ea5e5SAndroid Build Coastguard Worker 		printf("\n\t");
921*858ea5e5SAndroid Build Coastguard Worker 		show_link_ifindex_plain(info->xdp.ifindex);
922*858ea5e5SAndroid Build Coastguard Worker 		break;
923*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_KPROBE_MULTI:
924*858ea5e5SAndroid Build Coastguard Worker 		show_kprobe_multi_plain(info);
925*858ea5e5SAndroid Build Coastguard Worker 		break;
926*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_UPROBE_MULTI:
927*858ea5e5SAndroid Build Coastguard Worker 		show_uprobe_multi_plain(info);
928*858ea5e5SAndroid Build Coastguard Worker 		break;
929*858ea5e5SAndroid Build Coastguard Worker 	case BPF_LINK_TYPE_PERF_EVENT:
930*858ea5e5SAndroid Build Coastguard Worker 		switch (info->perf_event.type) {
931*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_EVENT:
932*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_event_plain(info);
933*858ea5e5SAndroid Build Coastguard Worker 			break;
934*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_TRACEPOINT:
935*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_tracepoint_plain(info);
936*858ea5e5SAndroid Build Coastguard Worker 			break;
937*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_KPROBE:
938*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_KRETPROBE:
939*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_kprobe_plain(info);
940*858ea5e5SAndroid Build Coastguard Worker 			break;
941*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_UPROBE:
942*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_URETPROBE:
943*858ea5e5SAndroid Build Coastguard Worker 			show_perf_event_uprobe_plain(info);
944*858ea5e5SAndroid Build Coastguard Worker 			break;
945*858ea5e5SAndroid Build Coastguard Worker 		default:
946*858ea5e5SAndroid Build Coastguard Worker 			break;
947*858ea5e5SAndroid Build Coastguard Worker 		}
948*858ea5e5SAndroid Build Coastguard Worker 		break;
949*858ea5e5SAndroid Build Coastguard Worker 	default:
950*858ea5e5SAndroid Build Coastguard Worker 		break;
951*858ea5e5SAndroid Build Coastguard Worker 	}
952*858ea5e5SAndroid Build Coastguard Worker 
953*858ea5e5SAndroid Build Coastguard Worker 	if (!hashmap__empty(link_table)) {
954*858ea5e5SAndroid Build Coastguard Worker 		struct hashmap_entry *entry;
955*858ea5e5SAndroid Build Coastguard Worker 
956*858ea5e5SAndroid Build Coastguard Worker 		hashmap__for_each_key_entry(link_table, entry, info->id)
957*858ea5e5SAndroid Build Coastguard Worker 			printf("\n\tpinned %s", (char *)entry->pvalue);
958*858ea5e5SAndroid Build Coastguard Worker 	}
959*858ea5e5SAndroid Build Coastguard Worker 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
960*858ea5e5SAndroid Build Coastguard Worker 
961*858ea5e5SAndroid Build Coastguard Worker 	printf("\n");
962*858ea5e5SAndroid Build Coastguard Worker 
963*858ea5e5SAndroid Build Coastguard Worker 	return 0;
964*858ea5e5SAndroid Build Coastguard Worker }
965*858ea5e5SAndroid Build Coastguard Worker 
do_show_link(int fd)966*858ea5e5SAndroid Build Coastguard Worker static int do_show_link(int fd)
967*858ea5e5SAndroid Build Coastguard Worker {
968*858ea5e5SAndroid Build Coastguard Worker 	__u64 *ref_ctr_offsets = NULL, *offsets = NULL, *cookies = NULL;
969*858ea5e5SAndroid Build Coastguard Worker 	struct bpf_link_info info;
970*858ea5e5SAndroid Build Coastguard Worker 	__u32 len = sizeof(info);
971*858ea5e5SAndroid Build Coastguard Worker 	char path_buf[PATH_MAX];
972*858ea5e5SAndroid Build Coastguard Worker 	__u64 *addrs = NULL;
973*858ea5e5SAndroid Build Coastguard Worker 	char buf[PATH_MAX];
974*858ea5e5SAndroid Build Coastguard Worker 	int count;
975*858ea5e5SAndroid Build Coastguard Worker 	int err;
976*858ea5e5SAndroid Build Coastguard Worker 
977*858ea5e5SAndroid Build Coastguard Worker 	memset(&info, 0, sizeof(info));
978*858ea5e5SAndroid Build Coastguard Worker 	buf[0] = '\0';
979*858ea5e5SAndroid Build Coastguard Worker again:
980*858ea5e5SAndroid Build Coastguard Worker 	err = bpf_link_get_info_by_fd(fd, &info, &len);
981*858ea5e5SAndroid Build Coastguard Worker 	if (err) {
982*858ea5e5SAndroid Build Coastguard Worker 		p_err("can't get link info: %s",
983*858ea5e5SAndroid Build Coastguard Worker 		      strerror(errno));
984*858ea5e5SAndroid Build Coastguard Worker 		close(fd);
985*858ea5e5SAndroid Build Coastguard Worker 		return err;
986*858ea5e5SAndroid Build Coastguard Worker 	}
987*858ea5e5SAndroid Build Coastguard Worker 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
988*858ea5e5SAndroid Build Coastguard Worker 	    !info.raw_tracepoint.tp_name) {
989*858ea5e5SAndroid Build Coastguard Worker 		info.raw_tracepoint.tp_name = ptr_to_u64(&buf);
990*858ea5e5SAndroid Build Coastguard Worker 		info.raw_tracepoint.tp_name_len = sizeof(buf);
991*858ea5e5SAndroid Build Coastguard Worker 		goto again;
992*858ea5e5SAndroid Build Coastguard Worker 	}
993*858ea5e5SAndroid Build Coastguard Worker 	if (info.type == BPF_LINK_TYPE_ITER &&
994*858ea5e5SAndroid Build Coastguard Worker 	    !info.iter.target_name) {
995*858ea5e5SAndroid Build Coastguard Worker 		info.iter.target_name = ptr_to_u64(&buf);
996*858ea5e5SAndroid Build Coastguard Worker 		info.iter.target_name_len = sizeof(buf);
997*858ea5e5SAndroid Build Coastguard Worker 		goto again;
998*858ea5e5SAndroid Build Coastguard Worker 	}
999*858ea5e5SAndroid Build Coastguard Worker 	if (info.type == BPF_LINK_TYPE_KPROBE_MULTI &&
1000*858ea5e5SAndroid Build Coastguard Worker 	    !info.kprobe_multi.addrs) {
1001*858ea5e5SAndroid Build Coastguard Worker 		count = info.kprobe_multi.count;
1002*858ea5e5SAndroid Build Coastguard Worker 		if (count) {
1003*858ea5e5SAndroid Build Coastguard Worker 			addrs = calloc(count, sizeof(__u64));
1004*858ea5e5SAndroid Build Coastguard Worker 			if (!addrs) {
1005*858ea5e5SAndroid Build Coastguard Worker 				p_err("mem alloc failed");
1006*858ea5e5SAndroid Build Coastguard Worker 				close(fd);
1007*858ea5e5SAndroid Build Coastguard Worker 				return -ENOMEM;
1008*858ea5e5SAndroid Build Coastguard Worker 			}
1009*858ea5e5SAndroid Build Coastguard Worker 			info.kprobe_multi.addrs = ptr_to_u64(addrs);
1010*858ea5e5SAndroid Build Coastguard Worker 			cookies = calloc(count, sizeof(__u64));
1011*858ea5e5SAndroid Build Coastguard Worker 			if (!cookies) {
1012*858ea5e5SAndroid Build Coastguard Worker 				p_err("mem alloc failed");
1013*858ea5e5SAndroid Build Coastguard Worker 				free(addrs);
1014*858ea5e5SAndroid Build Coastguard Worker 				close(fd);
1015*858ea5e5SAndroid Build Coastguard Worker 				return -ENOMEM;
1016*858ea5e5SAndroid Build Coastguard Worker 			}
1017*858ea5e5SAndroid Build Coastguard Worker 			info.kprobe_multi.cookies = ptr_to_u64(cookies);
1018*858ea5e5SAndroid Build Coastguard Worker 			goto again;
1019*858ea5e5SAndroid Build Coastguard Worker 		}
1020*858ea5e5SAndroid Build Coastguard Worker 	}
1021*858ea5e5SAndroid Build Coastguard Worker 	if (info.type == BPF_LINK_TYPE_UPROBE_MULTI &&
1022*858ea5e5SAndroid Build Coastguard Worker 	    !info.uprobe_multi.offsets) {
1023*858ea5e5SAndroid Build Coastguard Worker 		count = info.uprobe_multi.count;
1024*858ea5e5SAndroid Build Coastguard Worker 		if (count) {
1025*858ea5e5SAndroid Build Coastguard Worker 			offsets = calloc(count, sizeof(__u64));
1026*858ea5e5SAndroid Build Coastguard Worker 			if (!offsets) {
1027*858ea5e5SAndroid Build Coastguard Worker 				p_err("mem alloc failed");
1028*858ea5e5SAndroid Build Coastguard Worker 				close(fd);
1029*858ea5e5SAndroid Build Coastguard Worker 				return -ENOMEM;
1030*858ea5e5SAndroid Build Coastguard Worker 			}
1031*858ea5e5SAndroid Build Coastguard Worker 			info.uprobe_multi.offsets = ptr_to_u64(offsets);
1032*858ea5e5SAndroid Build Coastguard Worker 			ref_ctr_offsets = calloc(count, sizeof(__u64));
1033*858ea5e5SAndroid Build Coastguard Worker 			if (!ref_ctr_offsets) {
1034*858ea5e5SAndroid Build Coastguard Worker 				p_err("mem alloc failed");
1035*858ea5e5SAndroid Build Coastguard Worker 				free(offsets);
1036*858ea5e5SAndroid Build Coastguard Worker 				close(fd);
1037*858ea5e5SAndroid Build Coastguard Worker 				return -ENOMEM;
1038*858ea5e5SAndroid Build Coastguard Worker 			}
1039*858ea5e5SAndroid Build Coastguard Worker 			info.uprobe_multi.ref_ctr_offsets = ptr_to_u64(ref_ctr_offsets);
1040*858ea5e5SAndroid Build Coastguard Worker 			cookies = calloc(count, sizeof(__u64));
1041*858ea5e5SAndroid Build Coastguard Worker 			if (!cookies) {
1042*858ea5e5SAndroid Build Coastguard Worker 				p_err("mem alloc failed");
1043*858ea5e5SAndroid Build Coastguard Worker 				free(ref_ctr_offsets);
1044*858ea5e5SAndroid Build Coastguard Worker 				free(offsets);
1045*858ea5e5SAndroid Build Coastguard Worker 				close(fd);
1046*858ea5e5SAndroid Build Coastguard Worker 				return -ENOMEM;
1047*858ea5e5SAndroid Build Coastguard Worker 			}
1048*858ea5e5SAndroid Build Coastguard Worker 			info.uprobe_multi.cookies = ptr_to_u64(cookies);
1049*858ea5e5SAndroid Build Coastguard Worker 			info.uprobe_multi.path = ptr_to_u64(path_buf);
1050*858ea5e5SAndroid Build Coastguard Worker 			info.uprobe_multi.path_size = sizeof(path_buf);
1051*858ea5e5SAndroid Build Coastguard Worker 			goto again;
1052*858ea5e5SAndroid Build Coastguard Worker 		}
1053*858ea5e5SAndroid Build Coastguard Worker 	}
1054*858ea5e5SAndroid Build Coastguard Worker 	if (info.type == BPF_LINK_TYPE_PERF_EVENT) {
1055*858ea5e5SAndroid Build Coastguard Worker 		switch (info.perf_event.type) {
1056*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_TRACEPOINT:
1057*858ea5e5SAndroid Build Coastguard Worker 			if (!info.perf_event.tracepoint.tp_name) {
1058*858ea5e5SAndroid Build Coastguard Worker 				info.perf_event.tracepoint.tp_name = ptr_to_u64(&buf);
1059*858ea5e5SAndroid Build Coastguard Worker 				info.perf_event.tracepoint.name_len = sizeof(buf);
1060*858ea5e5SAndroid Build Coastguard Worker 				goto again;
1061*858ea5e5SAndroid Build Coastguard Worker 			}
1062*858ea5e5SAndroid Build Coastguard Worker 			break;
1063*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_KPROBE:
1064*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_KRETPROBE:
1065*858ea5e5SAndroid Build Coastguard Worker 			if (!info.perf_event.kprobe.func_name) {
1066*858ea5e5SAndroid Build Coastguard Worker 				info.perf_event.kprobe.func_name = ptr_to_u64(&buf);
1067*858ea5e5SAndroid Build Coastguard Worker 				info.perf_event.kprobe.name_len = sizeof(buf);
1068*858ea5e5SAndroid Build Coastguard Worker 				goto again;
1069*858ea5e5SAndroid Build Coastguard Worker 			}
1070*858ea5e5SAndroid Build Coastguard Worker 			break;
1071*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_UPROBE:
1072*858ea5e5SAndroid Build Coastguard Worker 		case BPF_PERF_EVENT_URETPROBE:
1073*858ea5e5SAndroid Build Coastguard Worker 			if (!info.perf_event.uprobe.file_name) {
1074*858ea5e5SAndroid Build Coastguard Worker 				info.perf_event.uprobe.file_name = ptr_to_u64(&buf);
1075*858ea5e5SAndroid Build Coastguard Worker 				info.perf_event.uprobe.name_len = sizeof(buf);
1076*858ea5e5SAndroid Build Coastguard Worker 				goto again;
1077*858ea5e5SAndroid Build Coastguard Worker 			}
1078*858ea5e5SAndroid Build Coastguard Worker 			break;
1079*858ea5e5SAndroid Build Coastguard Worker 		default:
1080*858ea5e5SAndroid Build Coastguard Worker 			break;
1081*858ea5e5SAndroid Build Coastguard Worker 		}
1082*858ea5e5SAndroid Build Coastguard Worker 	}
1083*858ea5e5SAndroid Build Coastguard Worker 
1084*858ea5e5SAndroid Build Coastguard Worker 	if (json_output)
1085*858ea5e5SAndroid Build Coastguard Worker 		show_link_close_json(fd, &info);
1086*858ea5e5SAndroid Build Coastguard Worker 	else
1087*858ea5e5SAndroid Build Coastguard Worker 		show_link_close_plain(fd, &info);
1088*858ea5e5SAndroid Build Coastguard Worker 
1089*858ea5e5SAndroid Build Coastguard Worker 	free(ref_ctr_offsets);
1090*858ea5e5SAndroid Build Coastguard Worker 	free(cookies);
1091*858ea5e5SAndroid Build Coastguard Worker 	free(offsets);
1092*858ea5e5SAndroid Build Coastguard Worker 	free(addrs);
1093*858ea5e5SAndroid Build Coastguard Worker 	close(fd);
1094*858ea5e5SAndroid Build Coastguard Worker 	return 0;
1095*858ea5e5SAndroid Build Coastguard Worker }
1096*858ea5e5SAndroid Build Coastguard Worker 
do_show(int argc,char ** argv)1097*858ea5e5SAndroid Build Coastguard Worker static int do_show(int argc, char **argv)
1098*858ea5e5SAndroid Build Coastguard Worker {
1099*858ea5e5SAndroid Build Coastguard Worker 	__u32 id = 0;
1100*858ea5e5SAndroid Build Coastguard Worker 	int err, fd;
1101*858ea5e5SAndroid Build Coastguard Worker 
1102*858ea5e5SAndroid Build Coastguard Worker 	if (show_pinned) {
1103*858ea5e5SAndroid Build Coastguard Worker 		link_table = hashmap__new(hash_fn_for_key_as_id,
1104*858ea5e5SAndroid Build Coastguard Worker 					  equal_fn_for_key_as_id, NULL);
1105*858ea5e5SAndroid Build Coastguard Worker 		if (IS_ERR(link_table)) {
1106*858ea5e5SAndroid Build Coastguard Worker 			p_err("failed to create hashmap for pinned paths");
1107*858ea5e5SAndroid Build Coastguard Worker 			return -1;
1108*858ea5e5SAndroid Build Coastguard Worker 		}
1109*858ea5e5SAndroid Build Coastguard Worker 		build_pinned_obj_table(link_table, BPF_OBJ_LINK);
1110*858ea5e5SAndroid Build Coastguard Worker 	}
1111*858ea5e5SAndroid Build Coastguard Worker 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
1112*858ea5e5SAndroid Build Coastguard Worker 
1113*858ea5e5SAndroid Build Coastguard Worker 	if (argc == 2) {
1114*858ea5e5SAndroid Build Coastguard Worker 		fd = link_parse_fd(&argc, &argv);
1115*858ea5e5SAndroid Build Coastguard Worker 		if (fd < 0)
1116*858ea5e5SAndroid Build Coastguard Worker 			return fd;
1117*858ea5e5SAndroid Build Coastguard Worker 		do_show_link(fd);
1118*858ea5e5SAndroid Build Coastguard Worker 		goto out;
1119*858ea5e5SAndroid Build Coastguard Worker 	}
1120*858ea5e5SAndroid Build Coastguard Worker 
1121*858ea5e5SAndroid Build Coastguard Worker 	if (argc)
1122*858ea5e5SAndroid Build Coastguard Worker 		return BAD_ARG();
1123*858ea5e5SAndroid Build Coastguard Worker 
1124*858ea5e5SAndroid Build Coastguard Worker 	if (json_output)
1125*858ea5e5SAndroid Build Coastguard Worker 		jsonw_start_array(json_wtr);
1126*858ea5e5SAndroid Build Coastguard Worker 	while (true) {
1127*858ea5e5SAndroid Build Coastguard Worker 		err = bpf_link_get_next_id(id, &id);
1128*858ea5e5SAndroid Build Coastguard Worker 		if (err) {
1129*858ea5e5SAndroid Build Coastguard Worker 			if (errno == ENOENT)
1130*858ea5e5SAndroid Build Coastguard Worker 				break;
1131*858ea5e5SAndroid Build Coastguard Worker 			p_err("can't get next link: %s%s", strerror(errno),
1132*858ea5e5SAndroid Build Coastguard Worker 			      errno == EINVAL ? " -- kernel too old?" : "");
1133*858ea5e5SAndroid Build Coastguard Worker 			break;
1134*858ea5e5SAndroid Build Coastguard Worker 		}
1135*858ea5e5SAndroid Build Coastguard Worker 
1136*858ea5e5SAndroid Build Coastguard Worker 		fd = bpf_link_get_fd_by_id(id);
1137*858ea5e5SAndroid Build Coastguard Worker 		if (fd < 0) {
1138*858ea5e5SAndroid Build Coastguard Worker 			if (errno == ENOENT)
1139*858ea5e5SAndroid Build Coastguard Worker 				continue;
1140*858ea5e5SAndroid Build Coastguard Worker 			p_err("can't get link by id (%u): %s",
1141*858ea5e5SAndroid Build Coastguard Worker 			      id, strerror(errno));
1142*858ea5e5SAndroid Build Coastguard Worker 			break;
1143*858ea5e5SAndroid Build Coastguard Worker 		}
1144*858ea5e5SAndroid Build Coastguard Worker 
1145*858ea5e5SAndroid Build Coastguard Worker 		err = do_show_link(fd);
1146*858ea5e5SAndroid Build Coastguard Worker 		if (err)
1147*858ea5e5SAndroid Build Coastguard Worker 			break;
1148*858ea5e5SAndroid Build Coastguard Worker 	}
1149*858ea5e5SAndroid Build Coastguard Worker 	if (json_output)
1150*858ea5e5SAndroid Build Coastguard Worker 		jsonw_end_array(json_wtr);
1151*858ea5e5SAndroid Build Coastguard Worker 
1152*858ea5e5SAndroid Build Coastguard Worker 	delete_obj_refs_table(refs_table);
1153*858ea5e5SAndroid Build Coastguard Worker 
1154*858ea5e5SAndroid Build Coastguard Worker 	if (show_pinned)
1155*858ea5e5SAndroid Build Coastguard Worker 		delete_pinned_obj_table(link_table);
1156*858ea5e5SAndroid Build Coastguard Worker 
1157*858ea5e5SAndroid Build Coastguard Worker out:
1158*858ea5e5SAndroid Build Coastguard Worker 	if (dd.sym_count)
1159*858ea5e5SAndroid Build Coastguard Worker 		kernel_syms_destroy(&dd);
1160*858ea5e5SAndroid Build Coastguard Worker 	return errno == ENOENT ? 0 : -1;
1161*858ea5e5SAndroid Build Coastguard Worker }
1162*858ea5e5SAndroid Build Coastguard Worker 
do_pin(int argc,char ** argv)1163*858ea5e5SAndroid Build Coastguard Worker static int do_pin(int argc, char **argv)
1164*858ea5e5SAndroid Build Coastguard Worker {
1165*858ea5e5SAndroid Build Coastguard Worker 	int err;
1166*858ea5e5SAndroid Build Coastguard Worker 
1167*858ea5e5SAndroid Build Coastguard Worker 	err = do_pin_any(argc, argv, link_parse_fd);
1168*858ea5e5SAndroid Build Coastguard Worker 	if (!err && json_output)
1169*858ea5e5SAndroid Build Coastguard Worker 		jsonw_null(json_wtr);
1170*858ea5e5SAndroid Build Coastguard Worker 	return err;
1171*858ea5e5SAndroid Build Coastguard Worker }
1172*858ea5e5SAndroid Build Coastguard Worker 
do_detach(int argc,char ** argv)1173*858ea5e5SAndroid Build Coastguard Worker static int do_detach(int argc, char **argv)
1174*858ea5e5SAndroid Build Coastguard Worker {
1175*858ea5e5SAndroid Build Coastguard Worker 	int err, fd;
1176*858ea5e5SAndroid Build Coastguard Worker 
1177*858ea5e5SAndroid Build Coastguard Worker 	if (argc != 2) {
1178*858ea5e5SAndroid Build Coastguard Worker 		p_err("link specifier is invalid or missing\n");
1179*858ea5e5SAndroid Build Coastguard Worker 		return 1;
1180*858ea5e5SAndroid Build Coastguard Worker 	}
1181*858ea5e5SAndroid Build Coastguard Worker 
1182*858ea5e5SAndroid Build Coastguard Worker 	fd = link_parse_fd(&argc, &argv);
1183*858ea5e5SAndroid Build Coastguard Worker 	if (fd < 0)
1184*858ea5e5SAndroid Build Coastguard Worker 		return 1;
1185*858ea5e5SAndroid Build Coastguard Worker 
1186*858ea5e5SAndroid Build Coastguard Worker 	err = bpf_link_detach(fd);
1187*858ea5e5SAndroid Build Coastguard Worker 	if (err)
1188*858ea5e5SAndroid Build Coastguard Worker 		err = -errno;
1189*858ea5e5SAndroid Build Coastguard Worker 	close(fd);
1190*858ea5e5SAndroid Build Coastguard Worker 	if (err) {
1191*858ea5e5SAndroid Build Coastguard Worker 		p_err("failed link detach: %s", strerror(-err));
1192*858ea5e5SAndroid Build Coastguard Worker 		return 1;
1193*858ea5e5SAndroid Build Coastguard Worker 	}
1194*858ea5e5SAndroid Build Coastguard Worker 
1195*858ea5e5SAndroid Build Coastguard Worker 	if (json_output)
1196*858ea5e5SAndroid Build Coastguard Worker 		jsonw_null(json_wtr);
1197*858ea5e5SAndroid Build Coastguard Worker 
1198*858ea5e5SAndroid Build Coastguard Worker 	return 0;
1199*858ea5e5SAndroid Build Coastguard Worker }
1200*858ea5e5SAndroid Build Coastguard Worker 
do_help(int argc,char ** argv)1201*858ea5e5SAndroid Build Coastguard Worker static int do_help(int argc, char **argv)
1202*858ea5e5SAndroid Build Coastguard Worker {
1203*858ea5e5SAndroid Build Coastguard Worker 	if (json_output) {
1204*858ea5e5SAndroid Build Coastguard Worker 		jsonw_null(json_wtr);
1205*858ea5e5SAndroid Build Coastguard Worker 		return 0;
1206*858ea5e5SAndroid Build Coastguard Worker 	}
1207*858ea5e5SAndroid Build Coastguard Worker 
1208*858ea5e5SAndroid Build Coastguard Worker 	fprintf(stderr,
1209*858ea5e5SAndroid Build Coastguard Worker 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
1210*858ea5e5SAndroid Build Coastguard Worker 		"       %1$s %2$s pin        LINK  FILE\n"
1211*858ea5e5SAndroid Build Coastguard Worker 		"       %1$s %2$s detach     LINK\n"
1212*858ea5e5SAndroid Build Coastguard Worker 		"       %1$s %2$s help\n"
1213*858ea5e5SAndroid Build Coastguard Worker 		"\n"
1214*858ea5e5SAndroid Build Coastguard Worker 		"       " HELP_SPEC_LINK "\n"
1215*858ea5e5SAndroid Build Coastguard Worker 		"       " HELP_SPEC_OPTIONS " |\n"
1216*858ea5e5SAndroid Build Coastguard Worker 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
1217*858ea5e5SAndroid Build Coastguard Worker 		"",
1218*858ea5e5SAndroid Build Coastguard Worker 		bin_name, argv[-2]);
1219*858ea5e5SAndroid Build Coastguard Worker 
1220*858ea5e5SAndroid Build Coastguard Worker 	return 0;
1221*858ea5e5SAndroid Build Coastguard Worker }
1222*858ea5e5SAndroid Build Coastguard Worker 
1223*858ea5e5SAndroid Build Coastguard Worker static const struct cmd cmds[] = {
1224*858ea5e5SAndroid Build Coastguard Worker 	{ "show",	do_show },
1225*858ea5e5SAndroid Build Coastguard Worker 	{ "list",	do_show },
1226*858ea5e5SAndroid Build Coastguard Worker 	{ "help",	do_help },
1227*858ea5e5SAndroid Build Coastguard Worker 	{ "pin",	do_pin },
1228*858ea5e5SAndroid Build Coastguard Worker 	{ "detach",	do_detach },
1229*858ea5e5SAndroid Build Coastguard Worker 	{ 0 }
1230*858ea5e5SAndroid Build Coastguard Worker };
1231*858ea5e5SAndroid Build Coastguard Worker 
do_link(int argc,char ** argv)1232*858ea5e5SAndroid Build Coastguard Worker int do_link(int argc, char **argv)
1233*858ea5e5SAndroid Build Coastguard Worker {
1234*858ea5e5SAndroid Build Coastguard Worker 	return cmd_select(cmds, argc, argv, do_help);
1235*858ea5e5SAndroid Build Coastguard Worker }
1236