All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH 4/4] perf annotate: Use libcapstone to disassemble
@ 2024-03-29  2:53 duchangbin
  2024-03-29 20:00 ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: duchangbin @ 2024-03-29  2:53 UTC (permalink / raw)
  To: Namhyung Kim, Arnaldo Carvalho de Melo, Ian Rogers, Kan Liang
  Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
	linux-perf-users

Hi, Namhyung,
On Thu, Mar 28, 2024 at 04:20:09PM -0700, Namhyung Kim wrote:
> 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.
>
> Cc: Changbin Du <changbin.du@huawei.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/util/disasm.c | 153 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 153 insertions(+)
>
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 59ac37723990..c58ea6d822ed 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -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);
> +
> +     /*
> +      * 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.

> +
> +     return 0;
> +}
> +

--
Cheers,
Changbin Du

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 4/4] perf annotate: Use libcapstone to disassemble
  2024-03-29  2:53 [PATCH 4/4] perf annotate: Use libcapstone to disassemble duchangbin
@ 2024-03-29 20:00 ` Namhyung Kim
  0 siblings, 0 replies; 3+ messages in thread
From: Namhyung Kim @ 2024-03-29 20:00 UTC (permalink / raw)
  To: duchangbin
  Cc: Arnaldo Carvalho de Melo, Ian Rogers, Kan Liang, Jiri Olsa,
	Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
	linux-perf-users

On Thu, Mar 28, 2024 at 7:53 PM duchangbin <changbin.du@huawei.com> wrote:
>
> Hi, Namhyung,
> On Thu, Mar 28, 2024 at 04:20:09PM -0700, Namhyung Kim wrote:
> > 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.
> >
> > Cc: Changbin Du <changbin.du@huawei.com>
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> >  tools/perf/util/disasm.c | 153 +++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 153 insertions(+)
> >
> > diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> > index 59ac37723990..c58ea6d822ed 100644
> > --- a/tools/perf/util/disasm.c
> > +++ b/tools/perf/util/disasm.c
> > @@ -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);
> > +
> > +     /*
> > +      * 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 4/4] perf annotate: Use libcapstone to disassemble
  2024-03-28 23:20 [PATCH 0/4] perf annotate: Use libcapstone as a disasssembler Namhyung Kim
@ 2024-03-28 23:20 ` Namhyung Kim
  0 siblings, 0 replies; 3+ messages in thread
From: Namhyung Kim @ 2024-03-28 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Ian Rogers, Kan Liang
  Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
	linux-perf-users, Changbin Du

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.

Cc: Changbin Du <changbin.du@huawei.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/disasm.c | 153 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 59ac37723990..c58ea6d822ed 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -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);
+
+	/*
+	 * 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;
+};
+
+/* This will be called for each PHDR in an ELF binary */
+static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
+{
+	struct find_file_offset_data *data = arg;
+
+	if (start <= data->ip && data->ip < start + len) {
+		data->offset = pgoff + data->ip - start;
+		return 1;
+	}
+	return 0;
+}
+
+static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
+					struct annotate_args *args)
+{
+	struct annotation *notes = symbol__annotation(sym);
+	struct map *map = args->ms.map;
+	struct dso *dso = map__dso(map);
+	struct nscookie nsc;
+	u64 start = map__rip_2objdump(map, sym->start);
+	u64 end = map__rip_2objdump(map, sym->end);
+	u64 len = end - start;
+	u64 offset;
+	int i, fd, count;
+	bool is_64bit = false;
+	bool needs_cs_close = false;
+	u8 *buf = NULL;
+	struct find_file_offset_data data = {
+		.ip = start,
+	};
+	csh handle;
+	cs_insn *insn;
+
+	if (args->options->objdump_path)
+		return -1;
+
+	nsinfo__mountns_enter(dso->nsinfo, &nsc);
+	fd = open(filename, O_RDONLY);
+	nsinfo__mountns_exit(&nsc);
+	if (fd < 0)
+		return -1;
+
+	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
+			    &is_64bit) == 0)
+		goto err;
+
+	if (open_capstone_handle(args, is_64bit, &handle) < 0)
+		goto err;
+
+	needs_cs_close = true;
+
+	buf = malloc(len);
+	if (buf == NULL)
+		goto err;
+
+	count = pread(fd, buf, len, data.offset);
+	close(fd);
+	fd = -1;
+
+	if ((u64)count != len)
+		goto err;
+
+	count = cs_disasm(handle, buf, len, start, len, &insn);
+	for (i = 0, offset = 0; i < count; i++) {
+		char disasm_buf[256];
+		struct disasm_line *dl;
+
+		scnprintf(disasm_buf, sizeof(disasm_buf), "%s %s",
+			  insn[i].mnemonic, insn[i].op_str);
+
+		args->offset = offset;
+		args->line = disasm_buf;
+		args->line_nr = 0;
+		args->fileloc = NULL;
+		args->ms.sym  = sym;
+
+		dl = disasm_line__new(args);
+		if (dl == NULL)
+			goto err;
+
+		annotation_line__add(&dl->al, &notes->src->source);
+
+		offset += insn[i].size;
+	}
+
+out:
+	if (needs_cs_close)
+		cs_close(&handle);
+	free(buf);
+	return count < 0 ? count : 0;
+
+err:
+	if (fd >= 0)
+		close(fd);
+	if (needs_cs_close) {
+		struct disasm_line *dl, *tmp;
+
+		/*
+		 * It probably failed in the middle of the above loop.
+		 * Release any resources it might add.
+		 */
+		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
+			list_del(&dl->al.node);
+			free(dl);
+		}
+	}
+	count = -1;
+	goto out;
+}
+#endif
+
 /*
  * Possibly create a new version of line with tabs expanded. Returns the
  * existing or new line, storage is updated if a new line is allocated. If
@@ -1463,6 +1610,12 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 		strcpy(symfs_filename, tmp);
 	}
 
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+	err = symbol__disassemble_capstone(symfs_filename, sym, args);
+	if (err == 0)
+		goto out_remove_tmp;
+#endif
+
 	err = asprintf(&command,
 		 "%s %s%s --start-address=0x%016" PRIx64
 		 " --stop-address=0x%016" PRIx64
-- 
2.44.0.478.gd926399ef9-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-03-29 20:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-29  2:53 [PATCH 4/4] perf annotate: Use libcapstone to disassemble duchangbin
2024-03-29 20:00 ` Namhyung Kim
  -- strict thread matches above, loose matches on Subject: below --
2024-03-28 23:20 [PATCH 0/4] perf annotate: Use libcapstone as a disasssembler Namhyung Kim
2024-03-28 23:20 ` [PATCH 4/4] perf annotate: Use libcapstone to disassemble Namhyung Kim

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.