1 // SPDX-License-Identifier: GPL-2.0-only
3 * Minimal BPF JIT image disassembler
5 * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
6 * debugging or verification purposes.
8 * To get the disassembly of the JIT code, do the following:
10 * 1) `echo 2 > /proc/sys/net/core/bpf_jit_enable`
11 * 2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`)
12 * 3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code
28 #include <sys/types.h>
31 #include <tools/dis-asm-compat.h>
33 #define CMD_ACTION_SIZE_BUFFER 10
34 #define CMD_ACTION_READ_ALL 3
36 static void get_exec_path(char *tpath, size_t size)
41 snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
47 len = readlink(path, tpath, size);
53 static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
57 struct disassemble_info info;
58 disassembler_ftype disassemble;
61 memset(tpath, 0, sizeof(tpath));
62 get_exec_path(tpath, sizeof(tpath));
64 bfdf = bfd_openr(tpath, NULL);
66 assert(bfd_check_format(bfdf, bfd_object));
68 init_disassemble_info_compat(&info, stdout,
69 (fprintf_ftype) fprintf,
71 info.arch = bfd_get_arch(bfdf);
72 info.mach = bfd_get_mach(bfdf);
74 info.buffer_length = len;
76 disassemble_init_for_target(&info);
78 #ifdef DISASM_FOUR_ARGS_SIGNATURE
79 disassemble = disassembler(info.arch,
84 disassemble = disassembler(bfdf);
91 count = disassemble(pc, &info);
95 for (i = 0; i < count; ++i)
96 printf("%02x ", (uint8_t) image[pc + i]);
101 } while(count > 0 && pc < len);
106 static char *get_klog_buff(unsigned int *klen)
111 len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
119 ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
129 static char *get_flog_buff(const char *file, unsigned int *klen)
135 fd = open(file, O_RDONLY);
139 ret = fstat(fd, &fi);
140 if (ret < 0 || !S_ISREG(fi.st_mode))
143 len = fi.st_size + 1;
148 memset(buff, 0, len);
149 ret = read(fd, buff, len - 1);
163 static char *get_log_buff(const char *file, unsigned int *klen)
165 return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
168 static void put_log_buff(char *buff)
173 static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
176 char *ptr, *pptr, *tmp;
178 unsigned int proglen;
179 int ret, flen, pass, ulen = 0;
180 regmatch_t pmatch[1];
188 ret = regcomp(®ex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
189 "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
193 memset(pmatch, 0, sizeof(pmatch));
196 ret = regexec(®ex, ptr, 1, pmatch, 0);
198 ptr += pmatch[0].rm_eo;
199 off += pmatch[0].rm_eo;
205 ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
206 ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
207 &flen, &proglen, &pass, &base);
212 if (proglen > 1000000) {
213 printf("proglen of %u too big, stopping\n", proglen);
217 image = malloc(proglen);
219 printf("Out of memory\n");
222 memset(image, 0, proglen);
224 tmp = ptr = haystack + off;
225 while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
227 if (!strstr(ptr, "JIT code"))
230 while ((ptr = strstr(pptr, ":")))
234 image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
245 assert(ulen == proglen);
246 printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
247 proglen, pass, flen);
248 printf("%lx + <x>:\n", base);
255 static void usage(void)
257 printf("Usage: bpf_jit_disasm [...]\n");
258 printf(" -o Also display related opcodes (default: off).\n");
259 printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n");
260 printf(" -f <file> Read last image dump from file or stdin (default: klog).\n");
261 printf(" -h Display this help.\n");
264 int main(int argc, char **argv)
266 unsigned int len, klen, opt, opcodes = 0;
267 char *kbuff, *file = NULL;
272 uint8_t *image = NULL;
274 while ((opt = getopt(argc, argv, "of:O:")) != -1) {
293 kbuff = get_log_buff(file, &klen);
295 fprintf(stderr, "Could not retrieve log buffer!\n");
299 image = get_last_jit_image(kbuff, klen, &len);
301 fprintf(stderr, "No JIT image found!\n");
305 get_asm_insns(image, len, opcodes);
309 ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
311 fprintf(stderr, "Could not open file %s for writing: ", ofile);
317 nr = write(ofd, pos, len);
319 fprintf(stderr, "Could not write data to %s: ", ofile);