xref: /linux/tools/bpf/bpftool/xlated_dumper.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2018 Netronome Systems, Inc. */
3 
4 #ifndef _GNU_SOURCE
5 #define _GNU_SOURCE
6 #endif
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <bpf/libbpf.h>
13 #include <bpf/libbpf_internal.h>
14 
15 #include "disasm.h"
16 #include "json_writer.h"
17 #include "main.h"
18 #include "xlated_dumper.h"
19 
kernel_syms_cmp(const void * sym_a,const void * sym_b)20 static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
21 {
22 	return ((struct kernel_sym *)sym_a)->address -
23 	       ((struct kernel_sym *)sym_b)->address;
24 }
25 
kernel_syms_load(struct dump_data * dd)26 void kernel_syms_load(struct dump_data *dd)
27 {
28 	struct kernel_sym *sym;
29 	char buff[256];
30 	void *tmp, *address;
31 	FILE *fp;
32 
33 	fp = fopen("/proc/kallsyms", "r");
34 	if (!fp)
35 		return;
36 
37 	while (fgets(buff, sizeof(buff), fp)) {
38 		tmp = libbpf_reallocarray(dd->sym_mapping, dd->sym_count + 1,
39 					  sizeof(*dd->sym_mapping));
40 		if (!tmp) {
41 out:
42 			free(dd->sym_mapping);
43 			dd->sym_mapping = NULL;
44 			fclose(fp);
45 			return;
46 		}
47 		dd->sym_mapping = tmp;
48 		sym = &dd->sym_mapping[dd->sym_count];
49 
50 		/* module is optional */
51 		sym->module[0] = '\0';
52 		/* trim the square brackets around the module name */
53 		if (sscanf(buff, "%p %*c %s [%[^]]s", &address, sym->name, sym->module) < 2)
54 			continue;
55 		sym->address = (unsigned long)address;
56 		if (!strcmp(sym->name, "__bpf_call_base")) {
57 			dd->address_call_base = sym->address;
58 			/* sysctl kernel.kptr_restrict was set */
59 			if (!sym->address)
60 				goto out;
61 		}
62 		if (sym->address)
63 			dd->sym_count++;
64 	}
65 
66 	fclose(fp);
67 
68 	qsort(dd->sym_mapping, dd->sym_count,
69 	      sizeof(*dd->sym_mapping), kernel_syms_cmp);
70 }
71 
kernel_syms_destroy(struct dump_data * dd)72 void kernel_syms_destroy(struct dump_data *dd)
73 {
74 	free(dd->sym_mapping);
75 }
76 
kernel_syms_search(struct dump_data * dd,unsigned long key)77 struct kernel_sym *kernel_syms_search(struct dump_data *dd,
78 				      unsigned long key)
79 {
80 	struct kernel_sym sym = {
81 		.address = key,
82 	};
83 
84 	return dd->sym_mapping ?
85 	       bsearch(&sym, dd->sym_mapping, dd->sym_count,
86 		       sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
87 }
88 
print_insn(void * private_data,const char * fmt,...)89 static void __printf(2, 3) print_insn(void *private_data, const char *fmt, ...)
90 {
91 	va_list args;
92 
93 	va_start(args, fmt);
94 	vprintf(fmt, args);
95 	va_end(args);
96 }
97 
98 static void __printf(2, 3)
print_insn_for_graph(void * private_data,const char * fmt,...)99 print_insn_for_graph(void *private_data, const char *fmt, ...)
100 {
101 	char buf[64], *p;
102 	va_list args;
103 
104 	va_start(args, fmt);
105 	vsnprintf(buf, sizeof(buf), fmt, args);
106 	va_end(args);
107 
108 	p = buf;
109 	while (*p != '\0') {
110 		if (*p == '<' || *p == '>' || *p == '|' || *p == '&') {
111 			memmove(p + 1, p, strlen(buf) + 1 - (p - buf));
112 			/* Escape special character. */
113 			*p++ = '\\';
114 		}
115 
116 		p++;
117 	}
118 
119 	printf("%s", buf);
120 }
121 
122 static void __printf(2, 3)
print_insn_json(void * private_data,const char * fmt,...)123 print_insn_json(void *private_data, const char *fmt, ...)
124 {
125 	va_list args;
126 
127 	va_start(args, fmt);
128 	jsonw_vprintf_enquote(json_wtr, fmt, args);
129 	va_end(args);
130 }
131 
print_call_pcrel(struct dump_data * dd,struct kernel_sym * sym,unsigned long address,const struct bpf_insn * insn)132 static const char *print_call_pcrel(struct dump_data *dd,
133 				    struct kernel_sym *sym,
134 				    unsigned long address,
135 				    const struct bpf_insn *insn)
136 {
137 	if (!dd->nr_jited_ksyms)
138 		/* Do not show address for interpreted programs */
139 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
140 			"%+d", insn->off);
141 	else if (sym)
142 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
143 			 "%+d#%s", insn->off, sym->name);
144 	else
145 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
146 			 "%+d#0x%lx", insn->off, address);
147 	return dd->scratch_buff;
148 }
149 
print_call_helper(struct dump_data * dd,struct kernel_sym * sym,unsigned long address)150 static const char *print_call_helper(struct dump_data *dd,
151 				     struct kernel_sym *sym,
152 				     unsigned long address)
153 {
154 	if (sym)
155 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
156 			 "%s", sym->name);
157 	else
158 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
159 			 "0x%lx", address);
160 	return dd->scratch_buff;
161 }
162 
print_call(void * private_data,const struct bpf_insn * insn)163 static const char *print_call(void *private_data,
164 			      const struct bpf_insn *insn)
165 {
166 	struct dump_data *dd = private_data;
167 	unsigned long address = dd->address_call_base + insn->imm;
168 	struct kernel_sym *sym;
169 
170 	if (insn->src_reg == BPF_PSEUDO_CALL &&
171 	    (__u32) insn->imm < dd->nr_jited_ksyms && dd->jited_ksyms)
172 		address = dd->jited_ksyms[insn->imm];
173 
174 	sym = kernel_syms_search(dd, address);
175 	if (insn->src_reg == BPF_PSEUDO_CALL)
176 		return print_call_pcrel(dd, sym, address, insn);
177 	else
178 		return print_call_helper(dd, sym, address);
179 }
180 
print_imm(void * private_data,const struct bpf_insn * insn,__u64 full_imm)181 static const char *print_imm(void *private_data,
182 			     const struct bpf_insn *insn,
183 			     __u64 full_imm)
184 {
185 	struct dump_data *dd = private_data;
186 
187 	if (insn->src_reg == BPF_PSEUDO_MAP_FD)
188 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
189 			 "map[id:%d]", insn->imm);
190 	else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE)
191 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
192 			 "map[id:%d][0]+%d", insn->imm, (insn + 1)->imm);
193 	else if (insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE)
194 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
195 			 "map[idx:%d]+%d", insn->imm, (insn + 1)->imm);
196 	else if (insn->src_reg == BPF_PSEUDO_FUNC)
197 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
198 			 "subprog[%+d]", insn->imm);
199 	else
200 		snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
201 			 "0x%llx", (unsigned long long)full_imm);
202 	return dd->scratch_buff;
203 }
204 
dump_xlated_json(struct dump_data * dd,void * buf,unsigned int len,bool opcodes,bool linum)205 void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
206 		      bool opcodes, bool linum)
207 {
208 	const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
209 	const struct bpf_insn_cbs cbs = {
210 		.cb_print	= print_insn_json,
211 		.cb_call	= print_call,
212 		.cb_imm		= print_imm,
213 		.private_data	= dd,
214 	};
215 	struct bpf_func_info *record;
216 	struct bpf_insn *insn = buf;
217 	struct btf *btf = dd->btf;
218 	bool double_insn = false;
219 	unsigned int nr_skip = 0;
220 	char func_sig[1024];
221 	unsigned int i;
222 
223 	jsonw_start_array(json_wtr);
224 	record = dd->func_info;
225 	for (i = 0; i < len / sizeof(*insn); i++) {
226 		if (double_insn) {
227 			double_insn = false;
228 			continue;
229 		}
230 		double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
231 
232 		jsonw_start_object(json_wtr);
233 
234 		if (btf && record) {
235 			if (record->insn_off == i) {
236 				btf_dumper_type_only(btf, record->type_id,
237 						     func_sig,
238 						     sizeof(func_sig));
239 				if (func_sig[0] != '\0') {
240 					jsonw_name(json_wtr, "proto");
241 					jsonw_string(json_wtr, func_sig);
242 				}
243 				record = (void *)record + dd->finfo_rec_size;
244 			}
245 		}
246 
247 		if (prog_linfo) {
248 			const struct bpf_line_info *linfo;
249 
250 			linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip);
251 			if (linfo) {
252 				btf_dump_linfo_json(btf, linfo, linum);
253 				nr_skip++;
254 			}
255 		}
256 
257 		jsonw_name(json_wtr, "disasm");
258 		print_bpf_insn(&cbs, insn + i, true);
259 
260 		if (opcodes) {
261 			jsonw_name(json_wtr, "opcodes");
262 			jsonw_start_object(json_wtr);
263 
264 			jsonw_name(json_wtr, "code");
265 			jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
266 
267 			jsonw_name(json_wtr, "src_reg");
268 			jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
269 
270 			jsonw_name(json_wtr, "dst_reg");
271 			jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
272 
273 			jsonw_name(json_wtr, "off");
274 			print_hex_data_json((uint8_t *)(&insn[i].off), 2);
275 
276 			jsonw_name(json_wtr, "imm");
277 			if (double_insn && i < len - 1)
278 				print_hex_data_json((uint8_t *)(&insn[i].imm),
279 						    12);
280 			else
281 				print_hex_data_json((uint8_t *)(&insn[i].imm),
282 						    4);
283 			jsonw_end_object(json_wtr);
284 		}
285 		jsonw_end_object(json_wtr);
286 	}
287 	jsonw_end_array(json_wtr);
288 }
289 
dump_xlated_plain(struct dump_data * dd,void * buf,unsigned int len,bool opcodes,bool linum)290 void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
291 		       bool opcodes, bool linum)
292 {
293 	const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
294 	const struct bpf_insn_cbs cbs = {
295 		.cb_print	= print_insn,
296 		.cb_call	= print_call,
297 		.cb_imm		= print_imm,
298 		.private_data	= dd,
299 	};
300 	struct bpf_func_info *record;
301 	struct bpf_insn *insn = buf;
302 	struct btf *btf = dd->btf;
303 	unsigned int nr_skip = 0;
304 	bool double_insn = false;
305 	char func_sig[1024];
306 	unsigned int i;
307 
308 	record = dd->func_info;
309 	for (i = 0; i < len / sizeof(*insn); i++) {
310 		if (double_insn) {
311 			double_insn = false;
312 			continue;
313 		}
314 
315 		if (btf && record) {
316 			if (record->insn_off == i) {
317 				btf_dumper_type_only(btf, record->type_id,
318 						     func_sig,
319 						     sizeof(func_sig));
320 				if (func_sig[0] != '\0')
321 					printf("%s:\n", func_sig);
322 				record = (void *)record + dd->finfo_rec_size;
323 			}
324 		}
325 
326 		if (prog_linfo) {
327 			const struct bpf_line_info *linfo;
328 
329 			linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip);
330 			if (linfo) {
331 				btf_dump_linfo_plain(btf, linfo, "; ",
332 						     linum);
333 				nr_skip++;
334 			}
335 		}
336 
337 		double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
338 
339 		printf("%4u: ", i);
340 		print_bpf_insn(&cbs, insn + i, true);
341 		printf("\n");
342 
343 		if (opcodes) {
344 			printf("       ");
345 			fprint_hex(stdout, insn + i, 8, " ");
346 			if (double_insn && i < len - 1) {
347 				printf(" ");
348 				fprint_hex(stdout, insn + i + 1, 8, " ");
349 			}
350 			printf("\n");
351 		}
352 	}
353 }
354 
dump_xlated_for_graph(struct dump_data * dd,void * buf_start,void * buf_end,unsigned int start_idx,bool opcodes,bool linum)355 void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
356 			   unsigned int start_idx,
357 			   bool opcodes, bool linum)
358 {
359 	const struct bpf_insn_cbs cbs = {
360 		.cb_print	= print_insn_for_graph,
361 		.cb_call	= print_call,
362 		.cb_imm		= print_imm,
363 		.private_data	= dd,
364 	};
365 	const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
366 	const struct bpf_line_info *last_linfo = NULL;
367 	struct bpf_func_info *record = dd->func_info;
368 	struct bpf_insn *insn_start = buf_start;
369 	struct bpf_insn *insn_end = buf_end;
370 	struct bpf_insn *cur = insn_start;
371 	struct btf *btf = dd->btf;
372 	bool double_insn = false;
373 	char func_sig[1024];
374 
375 	for (; cur <= insn_end; cur++) {
376 		unsigned int insn_off;
377 
378 		if (double_insn) {
379 			double_insn = false;
380 			continue;
381 		}
382 		double_insn = cur->code == (BPF_LD | BPF_IMM | BPF_DW);
383 
384 		insn_off = (unsigned int)(cur - insn_start + start_idx);
385 		if (btf && record) {
386 			if (record->insn_off == insn_off) {
387 				btf_dumper_type_only(btf, record->type_id,
388 						     func_sig,
389 						     sizeof(func_sig));
390 				if (func_sig[0] != '\0')
391 					printf("; %s:\\l\\\n", func_sig);
392 				record = (void *)record + dd->finfo_rec_size;
393 			}
394 		}
395 
396 		if (prog_linfo) {
397 			const struct bpf_line_info *linfo;
398 
399 			linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
400 			if (linfo && linfo != last_linfo) {
401 				btf_dump_linfo_dotlabel(btf, linfo, linum);
402 				last_linfo = linfo;
403 			}
404 		}
405 
406 		printf("%u: ", insn_off);
407 		print_bpf_insn(&cbs, cur, true);
408 		printf("\\l\\\n");
409 
410 		if (opcodes) {
411 			printf("\\ \\ \\ \\ ");
412 			fprint_hex(stdout, cur, 8, " ");
413 			if (double_insn && cur <= insn_end - 1) {
414 				printf(" ");
415 				fprint_hex(stdout, cur + 1, 8, " ");
416 			}
417 			printf("\\l\\\n");
418 		}
419 
420 		if (cur != insn_end)
421 			printf("| ");
422 	}
423 }
424