blob: 1711d0b5df3a5c38ae21e29bbf7bc7008126d148 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Based on:
4 *
5 * Minimal BPF JIT image disassembler
6 *
7 * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
8 * debugging or verification purposes.
9 *
10 * Copyright 2013 Daniel Borkmann <daniel@iogearbox.net>
11 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
12 */
13
David Brazdil0f672f62019-12-10 10:32:29 +000014#define _GNU_SOURCE
15#include <stdio.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <stdarg.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <assert.h>
21#include <unistd.h>
22#include <string.h>
23#include <bfd.h>
24#include <dis-asm.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025#include <sys/stat.h>
26#include <limits.h>
David Brazdil0f672f62019-12-10 10:32:29 +000027#include <libbpf.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028
29#include "json_writer.h"
30#include "main.h"
31
32static void get_exec_path(char *tpath, size_t size)
33{
David Brazdil0f672f62019-12-10 10:32:29 +000034 const char *path = "/proc/self/exe";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 ssize_t len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036
37 len = readlink(path, tpath, size - 1);
38 assert(len > 0);
39 tpath[len] = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040}
41
42static int oper_count;
43static int fprintf_json(void *out, const char *fmt, ...)
44{
45 va_list ap;
46 char *s;
Olivier Deprez0e641232021-09-23 10:07:05 +020047 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
49 va_start(ap, fmt);
Olivier Deprez0e641232021-09-23 10:07:05 +020050 err = vasprintf(&s, fmt, ap);
David Brazdil0f672f62019-12-10 10:32:29 +000051 va_end(ap);
Olivier Deprez0e641232021-09-23 10:07:05 +020052 if (err < 0)
53 return -1;
David Brazdil0f672f62019-12-10 10:32:29 +000054
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 if (!oper_count) {
56 int i;
57
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058 /* Strip trailing spaces */
59 i = strlen(s) - 1;
60 while (s[i] == ' ')
61 s[i--] = '\0';
62
63 jsonw_string_field(json_wtr, "operation", s);
64 jsonw_name(json_wtr, "operands");
65 jsonw_start_array(json_wtr);
66 oper_count++;
67 } else if (!strcmp(fmt, ",")) {
68 /* Skip */
69 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 jsonw_string(json_wtr, s);
71 oper_count++;
72 }
David Brazdil0f672f62019-12-10 10:32:29 +000073 free(s);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 return 0;
75}
76
77void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
David Brazdil0f672f62019-12-10 10:32:29 +000078 const char *arch, const char *disassembler_options,
79 const struct btf *btf,
80 const struct bpf_prog_linfo *prog_linfo,
81 __u64 func_ksym, unsigned int func_idx,
82 bool linum)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083{
David Brazdil0f672f62019-12-10 10:32:29 +000084 const struct bpf_line_info *linfo = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085 disassembler_ftype disassemble;
86 struct disassemble_info info;
David Brazdil0f672f62019-12-10 10:32:29 +000087 unsigned int nr_skip = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088 int count, i, pc = 0;
89 char tpath[PATH_MAX];
90 bfd *bfdf;
91
92 if (!len)
93 return;
94
95 memset(tpath, 0, sizeof(tpath));
96 get_exec_path(tpath, sizeof(tpath));
97
98 bfdf = bfd_openr(tpath, NULL);
99 assert(bfdf);
100 assert(bfd_check_format(bfdf, bfd_object));
101
102 if (json_output)
103 init_disassemble_info(&info, stdout,
104 (fprintf_ftype) fprintf_json);
105 else
106 init_disassemble_info(&info, stdout,
107 (fprintf_ftype) fprintf);
108
109 /* Update architecture info for offload. */
110 if (arch) {
111 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
112
113 if (inf) {
114 bfdf->arch_info = inf;
115 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000116 p_err("No libbfd support for %s", arch);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117 return;
118 }
119 }
120
121 info.arch = bfd_get_arch(bfdf);
122 info.mach = bfd_get_mach(bfdf);
David Brazdil0f672f62019-12-10 10:32:29 +0000123 if (disassembler_options)
124 info.disassembler_options = disassembler_options;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 info.buffer = image;
126 info.buffer_length = len;
127
128 disassemble_init_for_target(&info);
129
130#ifdef DISASM_FOUR_ARGS_SIGNATURE
131 disassemble = disassembler(info.arch,
132 bfd_big_endian(bfdf),
133 info.mach,
134 bfdf);
135#else
136 disassemble = disassembler(bfdf);
137#endif
138 assert(disassemble);
139
140 if (json_output)
141 jsonw_start_array(json_wtr);
142 do {
David Brazdil0f672f62019-12-10 10:32:29 +0000143 if (prog_linfo) {
144 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
145 func_ksym + pc,
146 func_idx,
147 nr_skip);
148 if (linfo)
149 nr_skip++;
150 }
151
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152 if (json_output) {
153 jsonw_start_object(json_wtr);
154 oper_count = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000155 if (linfo)
156 btf_dump_linfo_json(btf, linfo, linum);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000157 jsonw_name(json_wtr, "pc");
158 jsonw_printf(json_wtr, "\"0x%x\"", pc);
159 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000160 if (linfo)
161 btf_dump_linfo_plain(btf, linfo, "; ",
162 linum);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163 printf("%4x:\t", pc);
164 }
165
166 count = disassemble(pc, &info);
167 if (json_output) {
168 /* Operand array, was started in fprintf_json. Before
169 * that, make sure we have a _null_ value if no operand
170 * other than operation code was present.
171 */
172 if (oper_count == 1)
173 jsonw_null(json_wtr);
174 jsonw_end_array(json_wtr);
175 }
176
177 if (opcodes) {
178 if (json_output) {
179 jsonw_name(json_wtr, "opcodes");
180 jsonw_start_array(json_wtr);
181 for (i = 0; i < count; ++i)
182 jsonw_printf(json_wtr, "\"0x%02hhx\"",
183 (uint8_t)image[pc + i]);
184 jsonw_end_array(json_wtr);
185 } else {
186 printf("\n\t");
187 for (i = 0; i < count; ++i)
188 printf("%02x ",
189 (uint8_t)image[pc + i]);
190 }
191 }
192 if (json_output)
193 jsonw_end_object(json_wtr);
194 else
195 printf("\n");
196
197 pc += count;
198 } while (count > 0 && pc < len);
199 if (json_output)
200 jsonw_end_array(json_wtr);
201
202 bfd_close(bfdf);
203}
David Brazdil0f672f62019-12-10 10:32:29 +0000204
205int disasm_init(void)
206{
207 bfd_init();
208 return 0;
209}