Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
Jakub Kicinski | 02ff58d | 2018-12-12 19:59:25 -0800 | [diff] [blame] | 2 | /* Copyright (C) 2018 Netronome Systems, Inc. */ |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 3 | |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 4 | #define _GNU_SOURCE |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 5 | #include <stdarg.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/types.h> |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 10 | #include <libbpf.h> |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 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; |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 37 | tmp = reallocarray(dd->sym_mapping, dd->sym_count + 1, |
| 38 | sizeof(*dd->sym_mapping)); |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 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 | |
Sandipan Das | f7f62c7 | 2018-05-24 12:26:54 +0530 | [diff] [blame] | 72 | struct kernel_sym *kernel_syms_search(struct dump_data *dd, |
| 73 | unsigned long key) |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 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 | |
Quentin Monnet | c101189 | 2018-12-14 13:56:01 +0000 | [diff] [blame] | 84 | static void __printf(2, 3) print_insn(void *private_data, const char *fmt, ...) |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 85 | { |
| 86 | va_list args; |
| 87 | |
| 88 | va_start(args, fmt); |
| 89 | vprintf(fmt, args); |
| 90 | va_end(args); |
| 91 | } |
| 92 | |
Quentin Monnet | c101189 | 2018-12-14 13:56:01 +0000 | [diff] [blame] | 93 | static void __printf(2, 3) |
Jiri Olsa | 337682c | 2018-03-23 11:41:29 +0100 | [diff] [blame] | 94 | print_insn_for_graph(void *private_data, const char *fmt, ...) |
Jiong Wang | efcef17 | 2018-03-01 18:01:21 -0800 | [diff] [blame] | 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 | |
Quentin Monnet | c101189 | 2018-12-14 13:56:01 +0000 | [diff] [blame] | 124 | static void __printf(2, 3) |
| 125 | print_insn_json(void *private_data, const char *fmt, ...) |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 126 | { |
| 127 | unsigned int l = strlen(fmt); |
| 128 | char chomped_fmt[l]; |
| 129 | va_list args; |
| 130 | |
| 131 | va_start(args, fmt); |
| 132 | if (l > 0) { |
| 133 | strncpy(chomped_fmt, fmt, l - 1); |
| 134 | chomped_fmt[l - 1] = '\0'; |
| 135 | } |
| 136 | jsonw_vprintf_enquote(json_wtr, chomped_fmt, args); |
| 137 | va_end(args); |
| 138 | } |
| 139 | |
| 140 | static const char *print_call_pcrel(struct dump_data *dd, |
| 141 | struct kernel_sym *sym, |
| 142 | unsigned long address, |
| 143 | const struct bpf_insn *insn) |
| 144 | { |
Sandipan Das | f84192e | 2018-05-24 12:26:50 +0530 | [diff] [blame] | 145 | if (!dd->nr_jited_ksyms) |
| 146 | /* Do not show address for interpreted programs */ |
| 147 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 148 | "%+d", insn->off); |
| 149 | else if (sym) |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 150 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 151 | "%+d#%s", insn->off, sym->name); |
| 152 | else |
| 153 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 154 | "%+d#0x%lx", insn->off, address); |
| 155 | return dd->scratch_buff; |
| 156 | } |
| 157 | |
| 158 | static const char *print_call_helper(struct dump_data *dd, |
| 159 | struct kernel_sym *sym, |
| 160 | unsigned long address) |
| 161 | { |
| 162 | if (sym) |
| 163 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 164 | "%s", sym->name); |
| 165 | else |
| 166 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 167 | "0x%lx", address); |
| 168 | return dd->scratch_buff; |
| 169 | } |
| 170 | |
| 171 | static const char *print_call(void *private_data, |
| 172 | const struct bpf_insn *insn) |
| 173 | { |
| 174 | struct dump_data *dd = private_data; |
| 175 | unsigned long address = dd->address_call_base + insn->imm; |
| 176 | struct kernel_sym *sym; |
| 177 | |
Sandipan Das | f84192e | 2018-05-24 12:26:50 +0530 | [diff] [blame] | 178 | if (insn->src_reg == BPF_PSEUDO_CALL && |
| 179 | (__u32) insn->imm < dd->nr_jited_ksyms) |
| 180 | address = dd->jited_ksyms[insn->imm]; |
| 181 | |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 182 | sym = kernel_syms_search(dd, address); |
| 183 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 184 | return print_call_pcrel(dd, sym, address, insn); |
| 185 | else |
| 186 | return print_call_helper(dd, sym, address); |
| 187 | } |
| 188 | |
| 189 | static const char *print_imm(void *private_data, |
| 190 | const struct bpf_insn *insn, |
| 191 | __u64 full_imm) |
| 192 | { |
| 193 | struct dump_data *dd = private_data; |
| 194 | |
| 195 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) |
| 196 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 197 | "map[id:%u]", insn->imm); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 198 | else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) |
| 199 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 200 | "map[id:%u][0]+%u", insn->imm, (insn + 1)->imm); |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 201 | else |
| 202 | snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), |
| 203 | "0x%llx", (unsigned long long)full_imm); |
| 204 | return dd->scratch_buff; |
| 205 | } |
| 206 | |
| 207 | void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len, |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 208 | bool opcodes, bool linum) |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 209 | { |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 210 | const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 211 | const struct bpf_insn_cbs cbs = { |
| 212 | .cb_print = print_insn_json, |
| 213 | .cb_call = print_call, |
| 214 | .cb_imm = print_imm, |
| 215 | .private_data = dd, |
| 216 | }; |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 217 | struct bpf_func_info *record; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 218 | struct bpf_insn *insn = buf; |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 219 | struct btf *btf = dd->btf; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 220 | bool double_insn = false; |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 221 | unsigned int nr_skip = 0; |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 222 | char func_sig[1024]; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 223 | unsigned int i; |
| 224 | |
| 225 | jsonw_start_array(json_wtr); |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 226 | record = dd->func_info; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 227 | for (i = 0; i < len / sizeof(*insn); i++) { |
| 228 | if (double_insn) { |
| 229 | double_insn = false; |
| 230 | continue; |
| 231 | } |
| 232 | double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW); |
| 233 | |
| 234 | jsonw_start_object(json_wtr); |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 235 | |
| 236 | if (btf && record) { |
Martin KaFai Lau | 84ecc1f | 2018-12-05 17:35:47 -0800 | [diff] [blame] | 237 | if (record->insn_off == i) { |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 238 | btf_dumper_type_only(btf, record->type_id, |
| 239 | func_sig, |
| 240 | sizeof(func_sig)); |
| 241 | if (func_sig[0] != '\0') { |
| 242 | jsonw_name(json_wtr, "proto"); |
| 243 | jsonw_string(json_wtr, func_sig); |
| 244 | } |
| 245 | record = (void *)record + dd->finfo_rec_size; |
| 246 | } |
| 247 | } |
| 248 | |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 249 | if (prog_linfo) { |
| 250 | const struct bpf_line_info *linfo; |
| 251 | |
| 252 | linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip); |
| 253 | if (linfo) { |
| 254 | btf_dump_linfo_json(btf, linfo, linum); |
| 255 | nr_skip++; |
| 256 | } |
| 257 | } |
| 258 | |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 259 | jsonw_name(json_wtr, "disasm"); |
Jiri Olsa | 337682c | 2018-03-23 11:41:29 +0100 | [diff] [blame] | 260 | print_bpf_insn(&cbs, insn + i, true); |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 261 | |
| 262 | if (opcodes) { |
| 263 | jsonw_name(json_wtr, "opcodes"); |
| 264 | jsonw_start_object(json_wtr); |
| 265 | |
| 266 | jsonw_name(json_wtr, "code"); |
| 267 | jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code); |
| 268 | |
| 269 | jsonw_name(json_wtr, "src_reg"); |
| 270 | jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg); |
| 271 | |
| 272 | jsonw_name(json_wtr, "dst_reg"); |
| 273 | jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg); |
| 274 | |
| 275 | jsonw_name(json_wtr, "off"); |
| 276 | print_hex_data_json((uint8_t *)(&insn[i].off), 2); |
| 277 | |
| 278 | jsonw_name(json_wtr, "imm"); |
| 279 | if (double_insn && i < len - 1) |
| 280 | print_hex_data_json((uint8_t *)(&insn[i].imm), |
| 281 | 12); |
| 282 | else |
| 283 | print_hex_data_json((uint8_t *)(&insn[i].imm), |
| 284 | 4); |
| 285 | jsonw_end_object(json_wtr); |
| 286 | } |
| 287 | jsonw_end_object(json_wtr); |
| 288 | } |
| 289 | jsonw_end_array(json_wtr); |
| 290 | } |
| 291 | |
| 292 | void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len, |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 293 | bool opcodes, bool linum) |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 294 | { |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 295 | const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 296 | const struct bpf_insn_cbs cbs = { |
| 297 | .cb_print = print_insn, |
| 298 | .cb_call = print_call, |
| 299 | .cb_imm = print_imm, |
| 300 | .private_data = dd, |
| 301 | }; |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 302 | struct bpf_func_info *record; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 303 | struct bpf_insn *insn = buf; |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 304 | struct btf *btf = dd->btf; |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 305 | unsigned int nr_skip = 0; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 306 | bool double_insn = false; |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 307 | char func_sig[1024]; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 308 | unsigned int i; |
| 309 | |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 310 | record = dd->func_info; |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 311 | for (i = 0; i < len / sizeof(*insn); i++) { |
| 312 | if (double_insn) { |
| 313 | double_insn = false; |
| 314 | continue; |
| 315 | } |
| 316 | |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 317 | if (btf && record) { |
Martin KaFai Lau | 84ecc1f | 2018-12-05 17:35:47 -0800 | [diff] [blame] | 318 | if (record->insn_off == i) { |
Yonghong Song | 254471e | 2018-11-19 15:29:21 -0800 | [diff] [blame] | 319 | btf_dumper_type_only(btf, record->type_id, |
| 320 | func_sig, |
| 321 | sizeof(func_sig)); |
| 322 | if (func_sig[0] != '\0') |
| 323 | printf("%s:\n", func_sig); |
| 324 | record = (void *)record + dd->finfo_rec_size; |
| 325 | } |
| 326 | } |
| 327 | |
Martin KaFai Lau | b053b43 | 2018-12-07 16:42:32 -0800 | [diff] [blame] | 328 | if (prog_linfo) { |
| 329 | const struct bpf_line_info *linfo; |
| 330 | |
| 331 | linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip); |
| 332 | if (linfo) { |
| 333 | btf_dump_linfo_plain(btf, linfo, "; ", |
| 334 | linum); |
| 335 | nr_skip++; |
| 336 | } |
| 337 | } |
| 338 | |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 339 | double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW); |
| 340 | |
| 341 | printf("% 4d: ", i); |
Jiri Olsa | 337682c | 2018-03-23 11:41:29 +0100 | [diff] [blame] | 342 | print_bpf_insn(&cbs, insn + i, true); |
Jiong Wang | 73bb5b4 | 2018-03-01 18:01:17 -0800 | [diff] [blame] | 343 | |
| 344 | if (opcodes) { |
| 345 | printf(" "); |
| 346 | fprint_hex(stdout, insn + i, 8, " "); |
| 347 | if (double_insn && i < len - 1) { |
| 348 | printf(" "); |
| 349 | fprint_hex(stdout, insn + i + 1, 8, " "); |
| 350 | } |
| 351 | printf("\n"); |
| 352 | } |
| 353 | } |
| 354 | } |
Jiong Wang | efcef17 | 2018-03-01 18:01:21 -0800 | [diff] [blame] | 355 | |
| 356 | void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end, |
| 357 | unsigned int start_idx) |
| 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 | struct bpf_insn *insn_start = buf_start; |
| 366 | struct bpf_insn *insn_end = buf_end; |
| 367 | struct bpf_insn *cur = insn_start; |
| 368 | |
| 369 | for (; cur <= insn_end; cur++) { |
| 370 | printf("% 4d: ", (int)(cur - insn_start + start_idx)); |
Jiri Olsa | 337682c | 2018-03-23 11:41:29 +0100 | [diff] [blame] | 371 | print_bpf_insn(&cbs, cur, true); |
Jiong Wang | efcef17 | 2018-03-01 18:01:21 -0800 | [diff] [blame] | 372 | if (cur != insn_end) |
| 373 | printf(" | "); |
| 374 | } |
| 375 | } |