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