xref: /linux/tools/perf/util/print_insn.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Instruction binary disassembler based on capstone.
4  *
5  * Author(s): Changbin Du <changbin.du@huawei.com>
6  */
7 #include <inttypes.h>
8 #include <string.h>
9 #include <stdbool.h>
10 #include "debug.h"
11 #include "sample.h"
12 #include "symbol.h"
13 #include "machine.h"
14 #include "thread.h"
15 #include "print_insn.h"
16 #include "dump-insn.h"
17 #include "map.h"
18 #include "dso.h"
19 
20 size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)
21 {
22 	int printed = 0;
23 
24 	for (int i = 0; i < sample->insn_len; i++) {
25 		printed += fprintf(fp, "%02x", (unsigned char)sample->insn[i]);
26 		if (sample->insn_len - i > 1)
27 			printed += fprintf(fp, " ");
28 	}
29 	return printed;
30 }
31 
32 #ifdef HAVE_LIBCAPSTONE_SUPPORT
33 #include <capstone/capstone.h>
34 
35 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style);
36 
37 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style)
38 {
39 	cs_arch arch;
40 	cs_mode mode;
41 
42 	if (machine__is(machine, "x86_64") && is64) {
43 		arch = CS_ARCH_X86;
44 		mode = CS_MODE_64;
45 	} else if (machine__normalized_is(machine, "x86")) {
46 		arch = CS_ARCH_X86;
47 		mode = CS_MODE_32;
48 	} else if (machine__normalized_is(machine, "arm64")) {
49 		arch = CS_ARCH_ARM64;
50 		mode = CS_MODE_ARM;
51 	} else if (machine__normalized_is(machine, "arm")) {
52 		arch = CS_ARCH_ARM;
53 		mode = CS_MODE_ARM + CS_MODE_V8;
54 	} else if (machine__normalized_is(machine, "s390")) {
55 		arch = CS_ARCH_SYSZ;
56 		mode = CS_MODE_BIG_ENDIAN;
57 	} else {
58 		return -1;
59 	}
60 
61 	if (cs_open(arch, mode, cs_handle) != CS_ERR_OK) {
62 		pr_warning_once("cs_open failed\n");
63 		return -1;
64 	}
65 
66 	if (machine__normalized_is(machine, "x86")) {
67 		/*
68 		 * In case of using capstone_init while symbol__disassemble
69 		 * setting CS_OPT_SYNTAX_ATT depends if disassembler_style opts
70 		 * is set via annotation args
71 		 */
72 		if (disassembler_style)
73 			cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
74 		/*
75 		 * Resolving address operands to symbols is implemented
76 		 * on x86 by investigating instruction details.
77 		 */
78 		cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
79 	}
80 
81 	return 0;
82 }
83 
84 static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *insn,
85 			     int print_opts, FILE *fp)
86 {
87 	struct addr_location al;
88 	size_t printed = 0;
89 
90 	if (insn->detail && insn->detail->x86.op_count == 1) {
91 		cs_x86_op *op = &insn->detail->x86.operands[0];
92 
93 		addr_location__init(&al);
94 		if (op->type == X86_OP_IMM &&
95 		    thread__find_symbol(thread, cpumode, op->imm, &al)) {
96 			printed += fprintf(fp, "%s ", insn[0].mnemonic);
97 			printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
98 			if (print_opts & PRINT_INSN_IMM_HEX)
99 				printed += fprintf(fp, " [%#" PRIx64 "]", op->imm);
100 			addr_location__exit(&al);
101 			return printed;
102 		}
103 		addr_location__exit(&al);
104 	}
105 
106 	printed += fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
107 	return printed;
108 }
109 
110 static bool is64bitip(struct machine *machine, struct addr_location *al)
111 {
112 	const struct dso *dso = al->map ? map__dso(al->map) : NULL;
113 
114 	if (dso)
115 		return dso__is_64_bit(dso);
116 
117 	return machine__is(machine, "x86_64") ||
118 		machine__normalized_is(machine, "arm64") ||
119 		machine__normalized_is(machine, "s390");
120 }
121 
122 ssize_t fprintf_insn_asm(struct machine *machine, struct thread *thread, u8 cpumode,
123 			 bool is64bit, const uint8_t *code, size_t code_size,
124 			 uint64_t ip, int *lenp, int print_opts, FILE *fp)
125 {
126 	size_t printed;
127 	cs_insn *insn;
128 	csh cs_handle;
129 	size_t count;
130 	int ret;
131 
132 	/* TODO: Try to initiate capstone only once but need a proper place. */
133 	ret = capstone_init(machine, &cs_handle, is64bit, true);
134 	if (ret < 0)
135 		return ret;
136 
137 	count = cs_disasm(cs_handle, code, code_size, ip, 1, &insn);
138 	if (count > 0) {
139 		if (machine__normalized_is(machine, "x86"))
140 			printed = print_insn_x86(thread, cpumode, &insn[0], print_opts, fp);
141 		else
142 			printed = fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
143 		if (lenp)
144 			*lenp = insn->size;
145 		cs_free(insn, count);
146 	} else {
147 		printed = -1;
148 	}
149 
150 	cs_close(&cs_handle);
151 	return printed;
152 }
153 
154 size_t sample__fprintf_insn_asm(struct perf_sample *sample, struct thread *thread,
155 				struct machine *machine, FILE *fp,
156 				struct addr_location *al)
157 {
158 	bool is64bit = is64bitip(machine, al);
159 	ssize_t printed;
160 
161 	printed = fprintf_insn_asm(machine, thread, sample->cpumode, is64bit,
162 				   (uint8_t *)sample->insn, sample->insn_len,
163 				   sample->ip, NULL, 0, fp);
164 	if (printed < 0)
165 		return sample__fprintf_insn_raw(sample, fp);
166 
167 	return printed;
168 }
169 #else
170 size_t sample__fprintf_insn_asm(struct perf_sample *sample __maybe_unused,
171 				struct thread *thread __maybe_unused,
172 				struct machine *machine __maybe_unused,
173 				FILE *fp __maybe_unused,
174 				struct addr_location *al __maybe_unused)
175 {
176 	return 0;
177 }
178 #endif
179