xref: /linux/tools/perf/util/print_insn.c (revision 151ebcf0797b1a3ba53c8843dc21748c80e098c7)
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 static int capstone_init(struct machine *machine, csh *cs_handle, bool is64)
36 {
37 	cs_arch arch;
38 	cs_mode mode;
39 
40 	if (machine__is(machine, "x86_64") && is64) {
41 		arch = CS_ARCH_X86;
42 		mode = CS_MODE_64;
43 	} else if (machine__normalized_is(machine, "x86")) {
44 		arch = CS_ARCH_X86;
45 		mode = CS_MODE_32;
46 	} else if (machine__normalized_is(machine, "arm64")) {
47 		arch = CS_ARCH_ARM64;
48 		mode = CS_MODE_ARM;
49 	} else if (machine__normalized_is(machine, "arm")) {
50 		arch = CS_ARCH_ARM;
51 		mode = CS_MODE_ARM + CS_MODE_V8;
52 	} else if (machine__normalized_is(machine, "s390")) {
53 		arch = CS_ARCH_SYSZ;
54 		mode = CS_MODE_BIG_ENDIAN;
55 	} else {
56 		return -1;
57 	}
58 
59 	if (cs_open(arch, mode, cs_handle) != CS_ERR_OK) {
60 		pr_warning_once("cs_open failed\n");
61 		return -1;
62 	}
63 
64 	if (machine__normalized_is(machine, "x86")) {
65 		cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
66 		/*
67 		 * Resolving address operands to symbols is implemented
68 		 * on x86 by investigating instruction details.
69 		 */
70 		cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
71 	}
72 
73 	return 0;
74 }
75 
76 static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *insn,
77 			     int print_opts, FILE *fp)
78 {
79 	struct addr_location al;
80 	size_t printed = 0;
81 
82 	if (insn->detail && insn->detail->x86.op_count == 1) {
83 		cs_x86_op *op = &insn->detail->x86.operands[0];
84 
85 		addr_location__init(&al);
86 		if (op->type == X86_OP_IMM &&
87 		    thread__find_symbol(thread, cpumode, op->imm, &al)) {
88 			printed += fprintf(fp, "%s ", insn[0].mnemonic);
89 			printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
90 			if (print_opts & PRINT_INSN_IMM_HEX)
91 				printed += fprintf(fp, " [%#" PRIx64 "]", op->imm);
92 			addr_location__exit(&al);
93 			return printed;
94 		}
95 		addr_location__exit(&al);
96 	}
97 
98 	printed += fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
99 	return printed;
100 }
101 
102 static bool is64bitip(struct machine *machine, struct addr_location *al)
103 {
104 	const struct dso *dso = al->map ? map__dso(al->map) : NULL;
105 
106 	if (dso)
107 		return dso__is_64_bit(dso);
108 
109 	return machine__is(machine, "x86_64") ||
110 		machine__normalized_is(machine, "arm64") ||
111 		machine__normalized_is(machine, "s390");
112 }
113 
114 ssize_t fprintf_insn_asm(struct machine *machine, struct thread *thread, u8 cpumode,
115 			 bool is64bit, const uint8_t *code, size_t code_size,
116 			 uint64_t ip, int *lenp, int print_opts, FILE *fp)
117 {
118 	size_t printed;
119 	cs_insn *insn;
120 	csh cs_handle;
121 	size_t count;
122 	int ret;
123 
124 	/* TODO: Try to initiate capstone only once but need a proper place. */
125 	ret = capstone_init(machine, &cs_handle, is64bit);
126 	if (ret < 0)
127 		return ret;
128 
129 	count = cs_disasm(cs_handle, code, code_size, ip, 1, &insn);
130 	if (count > 0) {
131 		if (machine__normalized_is(machine, "x86"))
132 			printed = print_insn_x86(thread, cpumode, &insn[0], print_opts, fp);
133 		else
134 			printed = fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
135 		if (lenp)
136 			*lenp = insn->size;
137 		cs_free(insn, count);
138 	} else {
139 		printed = -1;
140 	}
141 
142 	cs_close(&cs_handle);
143 	return printed;
144 }
145 
146 size_t sample__fprintf_insn_asm(struct perf_sample *sample, struct thread *thread,
147 				struct machine *machine, FILE *fp,
148 				struct addr_location *al)
149 {
150 	bool is64bit = is64bitip(machine, al);
151 	ssize_t printed;
152 
153 	printed = fprintf_insn_asm(machine, thread, sample->cpumode, is64bit,
154 				   (uint8_t *)sample->insn, sample->insn_len,
155 				   sample->ip, NULL, 0, fp);
156 	if (printed < 0)
157 		return sample__fprintf_insn_raw(sample, fp);
158 
159 	return printed;
160 }
161 #else
162 size_t sample__fprintf_insn_asm(struct perf_sample *sample __maybe_unused,
163 				struct thread *thread __maybe_unused,
164 				struct machine *machine __maybe_unused,
165 				FILE *fp __maybe_unused,
166 				struct addr_location *al __maybe_unused)
167 {
168 	return 0;
169 }
170 #endif
171