Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf: util: use capstone disasm engine to show assembly instructions

Currently, the instructions of samples are shown as raw hex strings
which are hard to read. x86 has a special option '--xed' to disassemble
the hex string via intel XED tool.

Here we use capstone as our disassembler engine to give more friendly
instructions. We select libcapstone because capstone can provide more
insn details. Perf will fallback to raw instructions if libcapstone is
not available.

The advantages compared to XED tool:
* Support arm, arm64, x86-32, x86_64 (more could be supported),
xed only for x86_64.
* Immediate address operands are shown as symbol+offs.

Signed-off-by: Changbin Du <changbin.du@huawei.com>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: changbin.du@gmail.com
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240217074046.4100789-3-changbin.du@huawei.com

authored by

Changbin Du and committed by
Namhyung Kim
8f0ec15f 8b767db3

+155 -6
+3 -5
tools/perf/builtin-script.c
··· 34 34 #include "util/event.h" 35 35 #include "ui/ui.h" 36 36 #include "print_binary.h" 37 + #include "print_insn.h" 37 38 #include "archinsn.h" 38 39 #include <linux/bitmap.h> 39 40 #include <linux/kernel.h> ··· 1524 1523 if (PRINT_FIELD(INSNLEN)) 1525 1524 printed += fprintf(fp, " ilen: %d", sample->insn_len); 1526 1525 if (PRINT_FIELD(INSN) && sample->insn_len) { 1527 - int i; 1528 - 1529 - printed += fprintf(fp, " insn:"); 1530 - for (i = 0; i < sample->insn_len; i++) 1531 - printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]); 1526 + printed += fprintf(fp, " insn: "); 1527 + printed += sample__fprintf_insn_raw(sample, fp); 1532 1528 } 1533 1529 if (PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN)) 1534 1530 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
+1
tools/perf/util/Build
··· 32 32 perf-y += perf-regs-arch/ 33 33 perf-y += path.o 34 34 perf-y += print_binary.o 35 + perf-y += print_insn.o 35 36 perf-y += rlimit.o 36 37 perf-y += argv_split.o 37 38 perf-y += rbtree.o
+135
tools/perf/util/print_insn.c
··· 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
+16
tools/perf/util/print_insn.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef PERF_PRINT_INSN_H 3 + #define PERF_PRINT_INSN_H 4 + 5 + #include <stddef.h> 6 + #include <stdio.h> 7 + 8 + struct perf_sample; 9 + struct thread; 10 + struct machine; 11 + 12 + size_t sample__fprintf_insn_asm(struct perf_sample *sample, struct thread *thread, 13 + struct machine *machine, FILE *fp); 14 + size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp); 15 + 16 + #endif /* PERF_PRINT_INSN_H */
-1
tools/perf/util/thread.h
··· 13 13 #include <strlist.h> 14 14 #include <intlist.h> 15 15 #include "rwsem.h" 16 - #include "event.h" 17 16 #include "callchain.h" 18 17 #include <internal/rc_check.h> 19 18