[V4 12/16] tools/perf: Use capstone_init and remove open_capstone_handle from disasm.c

Athira Rajeev
2024-06-14T17:27:34Z

capstone_init is made availbale for all archs to use and updated to
enable support for CS_ARCH_PPC as well. Patch removes
open_capstone_handle and uses capstone_init in all the places.

Signed-off-by: Athira Rajeev<atrajeev@linux.vnet.ibm.com>
---
tools/perf/util/disasm.c | 42 +++++++++++-------------------------
tools/perf/util/print_insn.c | 3 +++
2 files changed, 15 insertions(+), 30 deletions(-)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 86ff98e64890..43743ca4bdc9 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -26,6 +26,7 @@
#include "symbol.h"
#include "util.h"
#include "sort.h"
+#include "print_insn.h"

static regex_t file_lineno;

@@ -1517,32 +1518,6 @@ symbol__disassemble_bpf_image(struct symbol *sym,
#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);
-
- /*
- * Resolving address operands to symbols is implemented
- * on x86 by investigating instruction details.
- */
- cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
-
- return 0;
-}
-
struct find_file_offset_data {
u64 ip;
u64 offset;
@@ -1639,6 +1614,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
cs_insn *insn;
char disasm_buf[512];
struct disasm_line *dl;
+ bool disassembler_style = false;

if (args->options->objdump_path)
return -1;
@@ -1653,7 +1629,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
&is_64bit) == 0)
goto err;

- if (open_capstone_handle(args, is_64bit, &handle) < 0)
+ if (!args->options->disassembler_style ||
+ !strcmp(args->options->disassembler_style, "att"))
+ disassembler_style = true;
+
+ if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
goto err;

needs_cs_close = true;
@@ -1973,9 +1953,11 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
}

#ifdef HAVE_LIBCAPSTONE_SUPPORT
- err = symbol__disassemble_capstone(symfs_filename, sym, args);
- if (err == 0)
- goto out_remove_tmp;
+ if (arch__is(args->arch, "x86")) {
+ err = symbol__disassemble_capstone(symfs_filename, sym, args);
+ if (err == 0)
+ goto out_remove_tmp;
+ }
#endif

err = asprintf(&command,
diff --git a/tools/perf/util/print_insn.c b/tools/perf/util/print_insn.c
index a76aae81d7a0..79dec5ab3bef 100644
--- a/tools/perf/util/print_insn.c
+++ b/tools/perf/util/print_insn.c
@@ -52,6 +52,9 @@ int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disas
} else if (machine__normalized_is(machine, "s390")) {
arch = CS_ARCH_SYSZ;
mode = CS_MODE_BIG_ENDIAN;
+ } else if (machine__normalized_is(machine, "powerpc")) {
+ arch = CS_ARCH_PPC;
+ mode = CS_MODE_64;
} else {
return -1;
}
--
2.43.0