Skip to contentSkip to conversation

perf annotate: Use libcapstone to disassemble

PATCH duchangbin 2 participants 2 messages Mar 29, 2024

lore
Share & tools
lore

duchangbin

author
#0 lore raw

Hi, Namhyung,

Quoted 54 lines · Namhyung Kim

Now it can use the capstone library to disassemble the instructions. Let's use that (if available) for perf annotate to speed up. Currently it only supports x86 architecture. With this change I can see ~3x speed up in data type profiling.

But note that capstone cannot give the source file and line number info. For now, users should use the external objdump for that by specifying the --objdump option explicitly.

---
 tools/perf/util/disasm.c | 153 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)
tools/perf/util/disasm.c +17 -0
index 59ac37723990..c58ea6d822ed 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <ctype.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <inttypes.h>
 #include <libgen.h>
 #include <regex.h>
@@ -18,6 +19,7 @@
 #include "evsel.h"
 #include "map.h"
 #include "maps.h"
+#include "namespaces.h"
 #include "srcline.h"
 #include "symbol.h"
 
@@ -1341,6 +1343,151 @@ symbol__disassemble_bpf_image(struct symbol *sym,
      return 0;
 }
 
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+#include <capstone/capstone.h>
+
+static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
+                             csh *handle)
+{
+     struct annotation_options *opt = args->options;
+     cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
+
+     /* TODO: support more architectures */
+     if (!arch__is(args->arch, "x86"))
+             return -1;
+
+     if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
+             return -1;

tools/perf/util/disasm.c symbol__disassemble_bpf_image(struct symbol *sym, · line 1370

Quoted 10 lines
tools/perf/util/disasm.c +10 -0
+
+     if (!opt->disassembler_style ||
+         !strcmp(opt->disassembler_style, "att"))
+             cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
+
+     /*
+      * Resolving address operands to symbols is implemented
+      * on x86 by investigating instruction details.
+      */
+     cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);

Enabling CS_OPT_DETAIL is to symbolize branch target address. You can refer to print_insn_x86() in print_insn.c.

tools/perf/util/disasm.c symbol__disassemble_bpf_image(struct symbol *sym, · line 1374

Quoted 4 lines
tools/perf/util/disasm.c +4 -0
+
+     return 0;
+}
+

--
Cheers,
Changbin Du

#1 lore raw
Quoted 61 lines · duchangbin

Hi, Namhyung,

Quoted 58 lines · Namhyung Kim

Now it can use the capstone library to disassemble the instructions. Let's use that (if available) for perf annotate to speed up. Currently it only supports x86 architecture. With this change I can see ~3x speed up in data type profiling.

But note that capstone cannot give the source file and line number info. For now, users should use the external objdump for that by specifying the --objdump option explicitly.

---
 tools/perf/util/disasm.c | 153 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)
tools/perf/util/disasm.c +21 -0
index 59ac37723990..c58ea6d822ed 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <ctype.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <inttypes.h>
 #include <libgen.h>
 #include <regex.h>
@@ -18,6 +19,7 @@
 #include "evsel.h"
 #include "map.h"
 #include "maps.h"
+#include "namespaces.h"
 #include "srcline.h"
 #include "symbol.h"
 
@@ -1341,6 +1343,151 @@ symbol__disassemble_bpf_image(struct symbol *sym,
      return 0;
 }
 
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+#include <capstone/capstone.h>
+
+static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
+                             csh *handle)
+{
+     struct annotation_options *opt = args->options;
+     cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
+
+     /* TODO: support more architectures */
+     if (!arch__is(args->arch, "x86"))
+             return -1;
+
+     if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
+             return -1;
+
+     if (!opt->disassembler_style ||
+         !strcmp(opt->disassembler_style, "att"))
+             cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
Quoted 8 lines

tools/perf/util/disasm.c symbol__disassemble_bpf_image(struct symbol *sym, · line 1370

Quoted 6 lines
tools/perf/util/disasm.c +6 -0
+
+     /*
+      * Resolving address operands to symbols is implemented
+      * on x86 by investigating instruction details.
+      */
+     cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);

Enabling CS_OPT_DETAIL is to symbolize branch target address. You can refer to print_insn_x86() in print_insn.c.

Right, I think we can add it as a comment.

Thanks,
Namhyung