xref: /linux/tools/perf/util/print_insn.c (revision 40d269c000bda9fcd276a0412a9cebd3f6e344c5)
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 <string.h>
8 #include <stdbool.h>
9 #include "debug.h"
10 #include "sample.h"
11 #include "symbol.h"
12 #include "machine.h"
13 #include "thread.h"
14 #include "print_insn.h"
15 
16 size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)
17 {
18 	int printed = 0;
19 
20 	for (int i = 0; i < sample->insn_len; i++) {
21 		printed += fprintf(fp, "%02x", (unsigned char)sample->insn[i]);
22 		if (sample->insn_len - i > 1)
23 			printed += fprintf(fp, " ");
24 	}
25 	return printed;
26 }
27 
28 #ifdef HAVE_LIBCAPSTONE_SUPPORT
29 #include <capstone/capstone.h>
30 
31 static int capstone_init(struct machine *machine, csh *cs_handle)
32 {
33 	cs_arch arch;
34 	cs_mode mode;
35 
36 	if (machine__is(machine, "x86_64")) {
37 		arch = CS_ARCH_X86;
38 		mode = CS_MODE_64;
39 	} else if (machine__normalized_is(machine, "x86")) {
40 		arch = CS_ARCH_X86;
41 		mode = CS_MODE_32;
42 	} else if (machine__normalized_is(machine, "arm64")) {
43 		arch = CS_ARCH_ARM64;
44 		mode = CS_MODE_ARM;
45 	} else if (machine__normalized_is(machine, "arm")) {
46 		arch = CS_ARCH_ARM;
47 		mode = CS_MODE_ARM + CS_MODE_V8;
48 	} else if (machine__normalized_is(machine, "s390")) {
49 		arch = CS_ARCH_SYSZ;
50 		mode = CS_MODE_BIG_ENDIAN;
51 	} else {
52 		return -1;
53 	}
54 
55 	if (cs_open(arch, mode, cs_handle) != CS_ERR_OK) {
56 		pr_warning_once("cs_open failed\n");
57 		return -1;
58 	}
59 
60 	if (machine__normalized_is(machine, "x86")) {
61 		cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
62 		/*
63 		 * Resolving address operands to symbols is implemented
64 		 * on x86 by investigating instruction details.
65 		 */
66 		cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
67 	}
68 
69 	return 0;
70 }
71 
72 static size_t print_insn_x86(struct perf_sample *sample, struct thread *thread,
73 			     cs_insn *insn, FILE *fp)
74 {
75 	struct addr_location al;
76 	size_t printed = 0;
77 
78 	if (insn->detail && insn->detail->x86.op_count == 1) {
79 		cs_x86_op *op = &insn->detail->x86.operands[0];
80 
81 		addr_location__init(&al);
82 		if (op->type == X86_OP_IMM &&
83 		    thread__find_symbol(thread, sample->cpumode, op->imm, &al)) {
84 			printed += fprintf(fp, "%s ", insn[0].mnemonic);
85 			printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
86 			addr_location__exit(&al);
87 			return printed;
88 		}
89 		addr_location__exit(&al);
90 	}
91 
92 	printed += fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
93 	return printed;
94 }
95 
96 size_t sample__fprintf_insn_asm(struct perf_sample *sample, struct thread *thread,
97 				struct machine *machine, FILE *fp)
98 {
99 	csh cs_handle;
100 	cs_insn *insn;
101 	size_t count;
102 	size_t printed = 0;
103 	int ret;
104 
105 	/* TODO: Try to initiate capstone only once but need a proper place. */
106 	ret = capstone_init(machine, &cs_handle);
107 	if (ret < 0) {
108 		/* fallback */
109 		return sample__fprintf_insn_raw(sample, fp);
110 	}
111 
112 	count = cs_disasm(cs_handle, (uint8_t *)sample->insn, sample->insn_len,
113 			  sample->ip, 1, &insn);
114 	if (count > 0) {
115 		if (machine__normalized_is(machine, "x86"))
116 			printed += print_insn_x86(sample, thread, &insn[0], fp);
117 		else
118 			printed += fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
119 		cs_free(insn, count);
120 	} else {
121 		printed += fprintf(fp, "illegal instruction");
122 	}
123 
124 	cs_close(&cs_handle);
125 	return printed;
126 }
127 #else
128 size_t sample__fprintf_insn_asm(struct perf_sample *sample __maybe_unused,
129 				struct thread *thread __maybe_unused,
130 				struct machine *machine __maybe_unused,
131 				FILE *fp __maybe_unused)
132 {
133 	return 0;
134 }
135 #endif
136