xlated_dumper.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /*
  3. * Copyright (C) 2018 Netronome Systems, Inc.
  4. *
  5. * This software is dual licensed under the GNU General License Version 2,
  6. * June 1991 as shown in the file COPYING in the top-level directory of this
  7. * source tree or the BSD 2-Clause License provided below. You have the
  8. * option to license this software under the complete terms of either license.
  9. *
  10. * The BSD 2-Clause License:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #define _GNU_SOURCE
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <sys/types.h>
  43. #include "disasm.h"
  44. #include "json_writer.h"
  45. #include "main.h"
  46. #include "xlated_dumper.h"
  47. static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
  48. {
  49. return ((struct kernel_sym *)sym_a)->address -
  50. ((struct kernel_sym *)sym_b)->address;
  51. }
  52. void kernel_syms_load(struct dump_data *dd)
  53. {
  54. struct kernel_sym *sym;
  55. char buff[256];
  56. void *tmp, *address;
  57. FILE *fp;
  58. fp = fopen("/proc/kallsyms", "r");
  59. if (!fp)
  60. return;
  61. while (!feof(fp)) {
  62. if (!fgets(buff, sizeof(buff), fp))
  63. break;
  64. tmp = reallocarray(dd->sym_mapping, dd->sym_count + 1,
  65. sizeof(*dd->sym_mapping));
  66. if (!tmp) {
  67. out:
  68. free(dd->sym_mapping);
  69. dd->sym_mapping = NULL;
  70. fclose(fp);
  71. return;
  72. }
  73. dd->sym_mapping = tmp;
  74. sym = &dd->sym_mapping[dd->sym_count];
  75. if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
  76. continue;
  77. sym->address = (unsigned long)address;
  78. if (!strcmp(sym->name, "__bpf_call_base")) {
  79. dd->address_call_base = sym->address;
  80. /* sysctl kernel.kptr_restrict was set */
  81. if (!sym->address)
  82. goto out;
  83. }
  84. if (sym->address)
  85. dd->sym_count++;
  86. }
  87. fclose(fp);
  88. qsort(dd->sym_mapping, dd->sym_count,
  89. sizeof(*dd->sym_mapping), kernel_syms_cmp);
  90. }
  91. void kernel_syms_destroy(struct dump_data *dd)
  92. {
  93. free(dd->sym_mapping);
  94. }
  95. struct kernel_sym *kernel_syms_search(struct dump_data *dd,
  96. unsigned long key)
  97. {
  98. struct kernel_sym sym = {
  99. .address = key,
  100. };
  101. return dd->sym_mapping ?
  102. bsearch(&sym, dd->sym_mapping, dd->sym_count,
  103. sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
  104. }
  105. static void print_insn(void *private_data, const char *fmt, ...)
  106. {
  107. va_list args;
  108. va_start(args, fmt);
  109. vprintf(fmt, args);
  110. va_end(args);
  111. }
  112. static void
  113. print_insn_for_graph(void *private_data, const char *fmt, ...)
  114. {
  115. char buf[64], *p;
  116. va_list args;
  117. va_start(args, fmt);
  118. vsnprintf(buf, sizeof(buf), fmt, args);
  119. va_end(args);
  120. p = buf;
  121. while (*p != '\0') {
  122. if (*p == '\n') {
  123. memmove(p + 3, p, strlen(buf) + 1 - (p - buf));
  124. /* Align each instruction dump row left. */
  125. *p++ = '\\';
  126. *p++ = 'l';
  127. /* Output multiline concatenation. */
  128. *p++ = '\\';
  129. } else if (*p == '<' || *p == '>' || *p == '|' || *p == '&') {
  130. memmove(p + 1, p, strlen(buf) + 1 - (p - buf));
  131. /* Escape special character. */
  132. *p++ = '\\';
  133. }
  134. p++;
  135. }
  136. printf("%s", buf);
  137. }
  138. static void print_insn_json(void *private_data, const char *fmt, ...)
  139. {
  140. unsigned int l = strlen(fmt);
  141. char chomped_fmt[l];
  142. va_list args;
  143. va_start(args, fmt);
  144. if (l > 0) {
  145. strncpy(chomped_fmt, fmt, l - 1);
  146. chomped_fmt[l - 1] = '\0';
  147. }
  148. jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
  149. va_end(args);
  150. }
  151. static const char *print_call_pcrel(struct dump_data *dd,
  152. struct kernel_sym *sym,
  153. unsigned long address,
  154. const struct bpf_insn *insn)
  155. {
  156. if (!dd->nr_jited_ksyms)
  157. /* Do not show address for interpreted programs */
  158. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  159. "%+d", insn->off);
  160. else if (sym)
  161. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  162. "%+d#%s", insn->off, sym->name);
  163. else
  164. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  165. "%+d#0x%lx", insn->off, address);
  166. return dd->scratch_buff;
  167. }
  168. static const char *print_call_helper(struct dump_data *dd,
  169. struct kernel_sym *sym,
  170. unsigned long address)
  171. {
  172. if (sym)
  173. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  174. "%s", sym->name);
  175. else
  176. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  177. "0x%lx", address);
  178. return dd->scratch_buff;
  179. }
  180. static const char *print_call(void *private_data,
  181. const struct bpf_insn *insn)
  182. {
  183. struct dump_data *dd = private_data;
  184. unsigned long address = dd->address_call_base + insn->imm;
  185. struct kernel_sym *sym;
  186. if (insn->src_reg == BPF_PSEUDO_CALL &&
  187. (__u32) insn->imm < dd->nr_jited_ksyms)
  188. address = dd->jited_ksyms[insn->imm];
  189. sym = kernel_syms_search(dd, address);
  190. if (insn->src_reg == BPF_PSEUDO_CALL)
  191. return print_call_pcrel(dd, sym, address, insn);
  192. else
  193. return print_call_helper(dd, sym, address);
  194. }
  195. static const char *print_imm(void *private_data,
  196. const struct bpf_insn *insn,
  197. __u64 full_imm)
  198. {
  199. struct dump_data *dd = private_data;
  200. if (insn->src_reg == BPF_PSEUDO_MAP_FD)
  201. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  202. "map[id:%u]", insn->imm);
  203. else
  204. snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
  205. "0x%llx", (unsigned long long)full_imm);
  206. return dd->scratch_buff;
  207. }
  208. void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
  209. bool opcodes)
  210. {
  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. };
  217. struct bpf_insn *insn = buf;
  218. bool double_insn = false;
  219. unsigned int i;
  220. jsonw_start_array(json_wtr);
  221. for (i = 0; i < len / sizeof(*insn); i++) {
  222. if (double_insn) {
  223. double_insn = false;
  224. continue;
  225. }
  226. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  227. jsonw_start_object(json_wtr);
  228. jsonw_name(json_wtr, "disasm");
  229. print_bpf_insn(&cbs, insn + i, true);
  230. if (opcodes) {
  231. jsonw_name(json_wtr, "opcodes");
  232. jsonw_start_object(json_wtr);
  233. jsonw_name(json_wtr, "code");
  234. jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
  235. jsonw_name(json_wtr, "src_reg");
  236. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
  237. jsonw_name(json_wtr, "dst_reg");
  238. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
  239. jsonw_name(json_wtr, "off");
  240. print_hex_data_json((uint8_t *)(&insn[i].off), 2);
  241. jsonw_name(json_wtr, "imm");
  242. if (double_insn && i < len - 1)
  243. print_hex_data_json((uint8_t *)(&insn[i].imm),
  244. 12);
  245. else
  246. print_hex_data_json((uint8_t *)(&insn[i].imm),
  247. 4);
  248. jsonw_end_object(json_wtr);
  249. }
  250. jsonw_end_object(json_wtr);
  251. }
  252. jsonw_end_array(json_wtr);
  253. }
  254. void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
  255. bool opcodes)
  256. {
  257. const struct bpf_insn_cbs cbs = {
  258. .cb_print = print_insn,
  259. .cb_call = print_call,
  260. .cb_imm = print_imm,
  261. .private_data = dd,
  262. };
  263. struct bpf_insn *insn = buf;
  264. bool double_insn = false;
  265. unsigned int i;
  266. for (i = 0; i < len / sizeof(*insn); i++) {
  267. if (double_insn) {
  268. double_insn = false;
  269. continue;
  270. }
  271. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  272. printf("% 4d: ", i);
  273. print_bpf_insn(&cbs, insn + i, true);
  274. if (opcodes) {
  275. printf(" ");
  276. fprint_hex(stdout, insn + i, 8, " ");
  277. if (double_insn && i < len - 1) {
  278. printf(" ");
  279. fprint_hex(stdout, insn + i + 1, 8, " ");
  280. }
  281. printf("\n");
  282. }
  283. }
  284. }
  285. void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
  286. unsigned int start_idx)
  287. {
  288. const struct bpf_insn_cbs cbs = {
  289. .cb_print = print_insn_for_graph,
  290. .cb_call = print_call,
  291. .cb_imm = print_imm,
  292. .private_data = dd,
  293. };
  294. struct bpf_insn *insn_start = buf_start;
  295. struct bpf_insn *insn_end = buf_end;
  296. struct bpf_insn *cur = insn_start;
  297. for (; cur <= insn_end; cur++) {
  298. printf("% 4d: ", (int)(cur - insn_start + start_idx));
  299. print_bpf_insn(&cbs, cur, true);
  300. if (cur != insn_end)
  301. printf(" | ");
  302. }
  303. }