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