2 * Minimal BPF JIT image disassembler
4 * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
5 * debugging or verification purposes.
7 * To get the disassembly of the JIT code, do the following:
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
13 * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
14 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
26 #include <sys/types.h>
29 static void get_exec_path(char *tpath
, size_t size
)
34 snprintf(tpath
, size
, "/proc/%d/exe", (int) getpid());
40 len
= readlink(path
, tpath
, size
);
46 static void get_asm_insns(uint8_t *image
, size_t len
, int opcodes
)
50 struct disassemble_info info
;
51 disassembler_ftype disassemble
;
54 memset(tpath
, 0, sizeof(tpath
));
55 get_exec_path(tpath
, sizeof(tpath
));
57 bfdf
= bfd_openr(tpath
, NULL
);
59 assert(bfd_check_format(bfdf
, bfd_object
));
61 init_disassemble_info(&info
, stdout
, (fprintf_ftype
) fprintf
);
62 info
.arch
= bfd_get_arch(bfdf
);
63 info
.mach
= bfd_get_mach(bfdf
);
65 info
.buffer_length
= len
;
67 disassemble_init_for_target(&info
);
69 disassemble
= disassembler(bfdf
);
75 count
= disassemble(pc
, &info
);
79 for (i
= 0; i
< count
; ++i
)
80 printf("%02x ", (uint8_t) image
[pc
+ i
]);
85 } while(count
> 0 && pc
< len
);
90 static char *get_klog_buff(int *klen
)
92 int ret
, len
= klogctl(10, NULL
, 0);
93 char *buff
= malloc(len
);
96 ret
= klogctl(3, buff
, len
);
103 static void put_klog_buff(char *buff
)
108 static int get_last_jit_image(char *haystack
, size_t hlen
,
109 uint8_t *image
, size_t ilen
)
111 char *ptr
, *pptr
, *tmp
;
113 int ret
, flen
, proglen
, pass
, ulen
= 0;
114 regmatch_t pmatch
[1];
121 ret
= regcomp(®ex
, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
122 "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED
);
126 memset(pmatch
, 0, sizeof(pmatch
));
129 ret
= regexec(®ex
, ptr
, 1, pmatch
, 0);
131 ptr
+= pmatch
[0].rm_eo
;
132 off
+= pmatch
[0].rm_eo
;
138 ptr
= haystack
+ off
- (pmatch
[0].rm_eo
- pmatch
[0].rm_so
);
139 ret
= sscanf(ptr
, "flen=%d proglen=%d pass=%d image=%lx",
140 &flen
, &proglen
, &pass
, &base
);
144 tmp
= ptr
= haystack
+ off
;
145 while ((ptr
= strtok(tmp
, "\n")) != NULL
&& ulen
< ilen
) {
147 if (!strstr(ptr
, "JIT code"))
150 while ((ptr
= strstr(pptr
, ":")))
154 image
[ulen
++] = (uint8_t) strtoul(pptr
, &pptr
, 16);
155 if (ptr
== pptr
|| ulen
>= ilen
) {
163 assert(ulen
== proglen
);
164 printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
165 proglen
, pass
, flen
);
166 printf("%lx + <x>:\n", base
);
172 int main(int argc
, char **argv
)
174 int len
, klen
, opcodes
= 0;
176 static uint8_t image
[32768];
179 if (!strncmp("-o", argv
[argc
- 1], 2)) {
182 printf("usage: bpf_jit_disasm [-o: show opcodes]\n");
188 memset(image
, 0, sizeof(image
));
190 kbuff
= get_klog_buff(&klen
);
192 len
= get_last_jit_image(kbuff
, klen
, image
, sizeof(image
));
194 get_asm_insns(image
, len
, opcodes
);
196 put_klog_buff(kbuff
);