PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/net/bpf_jit_disasm.c

https://bitbucket.org/danhamilt1/linux
C | 199 lines | 148 code | 36 blank | 15 comment | 27 complexity | 2398496deb710aa03d222bd5938e9923 MD5 | raw file
  1. /*
  2. * Minimal BPF JIT image disassembler
  3. *
  4. * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
  5. * debugging or verification purposes.
  6. *
  7. * To get the disassembly of the JIT code, do the following:
  8. *
  9. * 1) `echo 2 > /proc/sys/net/core/bpf_jit_enable`
  10. * 2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`)
  11. * 3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code
  12. *
  13. * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
  14. * Licensed under the GNU General Public License, version 2.0 (GPLv2)
  15. */
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <assert.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <bfd.h>
  23. #include <dis-asm.h>
  24. #include <sys/klog.h>
  25. #include <sys/types.h>
  26. #include <regex.h>
  27. static void get_exec_path(char *tpath, size_t size)
  28. {
  29. char *path;
  30. ssize_t len;
  31. snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
  32. tpath[size - 1] = 0;
  33. path = strdup(tpath);
  34. assert(path);
  35. len = readlink(path, tpath, size);
  36. tpath[len] = 0;
  37. free(path);
  38. }
  39. static void get_asm_insns(uint8_t *image, size_t len, unsigned long base,
  40. int opcodes)
  41. {
  42. int count, i, pc = 0;
  43. char tpath[256];
  44. struct disassemble_info info;
  45. disassembler_ftype disassemble;
  46. bfd *bfdf;
  47. memset(tpath, 0, sizeof(tpath));
  48. get_exec_path(tpath, sizeof(tpath));
  49. bfdf = bfd_openr(tpath, NULL);
  50. assert(bfdf);
  51. assert(bfd_check_format(bfdf, bfd_object));
  52. init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf);
  53. info.arch = bfd_get_arch(bfdf);
  54. info.mach = bfd_get_mach(bfdf);
  55. info.buffer = image;
  56. info.buffer_length = len;
  57. disassemble_init_for_target(&info);
  58. disassemble = disassembler(bfdf);
  59. assert(disassemble);
  60. do {
  61. printf("%4x:\t", pc);
  62. count = disassemble(pc, &info);
  63. if (opcodes) {
  64. printf("\n\t");
  65. for (i = 0; i < count; ++i)
  66. printf("%02x ", (uint8_t) image[pc + i]);
  67. }
  68. printf("\n");
  69. pc += count;
  70. } while(count > 0 && pc < len);
  71. bfd_close(bfdf);
  72. }
  73. static char *get_klog_buff(int *klen)
  74. {
  75. int ret, len = klogctl(10, NULL, 0);
  76. char *buff = malloc(len);
  77. assert(buff && klen);
  78. ret = klogctl(3, buff, len);
  79. assert(ret >= 0);
  80. *klen = ret;
  81. return buff;
  82. }
  83. static void put_klog_buff(char *buff)
  84. {
  85. free(buff);
  86. }
  87. static int get_last_jit_image(char *haystack, size_t hlen,
  88. uint8_t *image, size_t ilen,
  89. unsigned long *base)
  90. {
  91. char *ptr, *pptr, *tmp;
  92. off_t off = 0;
  93. int ret, flen, proglen, pass, ulen = 0;
  94. regmatch_t pmatch[1];
  95. regex_t regex;
  96. if (hlen == 0)
  97. return 0;
  98. ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
  99. "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
  100. assert(ret == 0);
  101. ptr = haystack;
  102. while (1) {
  103. ret = regexec(&regex, ptr, 1, pmatch, 0);
  104. if (ret == 0) {
  105. ptr += pmatch[0].rm_eo;
  106. off += pmatch[0].rm_eo;
  107. assert(off < hlen);
  108. } else
  109. break;
  110. }
  111. ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
  112. ret = sscanf(ptr, "flen=%d proglen=%d pass=%d image=%lx",
  113. &flen, &proglen, &pass, base);
  114. if (ret != 4)
  115. return 0;
  116. tmp = ptr = haystack + off;
  117. while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) {
  118. tmp = NULL;
  119. if (!strstr(ptr, "JIT code"))
  120. continue;
  121. pptr = ptr;
  122. while ((ptr = strstr(pptr, ":")))
  123. pptr = ptr + 1;
  124. ptr = pptr;
  125. do {
  126. image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
  127. if (ptr == pptr || ulen >= ilen) {
  128. ulen--;
  129. break;
  130. }
  131. ptr = pptr;
  132. } while (1);
  133. }
  134. assert(ulen == proglen);
  135. printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
  136. proglen, pass, flen);
  137. printf("%lx + <x>:\n", *base);
  138. regfree(&regex);
  139. return ulen;
  140. }
  141. int main(int argc, char **argv)
  142. {
  143. int len, klen, opcodes = 0;
  144. char *kbuff;
  145. unsigned long base;
  146. uint8_t image[4096];
  147. if (argc > 1) {
  148. if (!strncmp("-o", argv[argc - 1], 2)) {
  149. opcodes = 1;
  150. } else {
  151. printf("usage: bpf_jit_disasm [-o: show opcodes]\n");
  152. exit(0);
  153. }
  154. }
  155. bfd_init();
  156. memset(image, 0, sizeof(image));
  157. kbuff = get_klog_buff(&klen);
  158. len = get_last_jit_image(kbuff, klen, image, sizeof(image), &base);
  159. if (len > 0 && base > 0)
  160. get_asm_insns(image, len, base, opcodes);
  161. put_klog_buff(kbuff);
  162. return 0;
  163. }