linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] perf annotate: Use libcapstone as a disasssembler
@ 2024-03-28 23:20 Namhyung Kim
  2024-03-28 23:20 ` [PATCH 1/4] perf annotate: Use ins__is_xxx() if possible Namhyung Kim
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ 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

Hello,

As we've added libcapstone support, it's natural to use it for perf annotate
as well.  This change added the capstone support on x86 first.  Other archs
can be added later (by someone who can verify it doesn't break things).

For now it tries to use capstone (if available) before objdump.  But it
doesn't support source file and line number info.  So users should use the
objdump (by passing --objdump=PATH option) if they need them.  For example,
this command line will keep the existing behavior (i.e. using objdump).

  # not to use capstone for disassembly
  $ perf annotate --objdump=objdump

The capstone uses LLVM objdump style output which is slightly different than
the GNU objdump.  But it should not have differences besides that.  I've
verified the result of data type profiling and it produced the same output
but gave me ~3x speedups.

Thanks,
Namhyung


Cc: Changbin Du <changbin.du@huawei.com>


Namhyung Kim (4):
  perf annotate: Use ins__is_xxx() if possible
  perf annotate: Add and use ins__is_nop()
  perf annotate: Split out util/disasm.c
  perf annotate: Use libcapstone to disassemble

 tools/perf/util/Build      |    1 +
 tools/perf/util/annotate.c | 1711 ++---------------------------------
 tools/perf/util/annotate.h |   59 +-
 tools/perf/util/disasm.c   | 1739 ++++++++++++++++++++++++++++++++++++
 tools/perf/util/disasm.h   |  112 +++
 5 files changed, 1914 insertions(+), 1708 deletions(-)
 create mode 100644 tools/perf/util/disasm.c
 create mode 100644 tools/perf/util/disasm.h

-- 
2.44.0.478.gd926399ef9-goog


^ permalink raw reply	[flat|nested] 10+ messages in thread
* 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; 10+ 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@vger.kernel.org

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] 10+ messages in thread

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

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-28 23:20 [PATCH 0/4] perf annotate: Use libcapstone as a disasssembler Namhyung Kim
2024-03-28 23:20 ` [PATCH 1/4] perf annotate: Use ins__is_xxx() if possible Namhyung Kim
2024-03-28 23:20 ` [PATCH 2/4] perf annotate: Add and use ins__is_nop() Namhyung Kim
2024-03-28 23:20 ` [PATCH 3/4] perf annotate: Split out util/disasm.c Namhyung Kim
2024-03-28 23:20 ` [PATCH 4/4] perf annotate: Use libcapstone to disassemble Namhyung Kim
2024-03-29 16:32 ` [PATCH 0/4] perf annotate: Use libcapstone as a disasssembler Ian Rogers
2024-03-29 19:57   ` Namhyung Kim
2024-03-29 17:09 ` Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2024-03-29  2:53 [PATCH 4/4] perf annotate: Use libcapstone to disassemble duchangbin
2024-03-29 20:00 ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).