2024-01-16 11:52:43

by Changbin Du

[permalink] [raw]
Subject: [PATCH 0/3] perf: script: Intro capstone disasm engine to show instruction trace

This series introduces capstone disassembler engine to print instructions of
Intel PT trace, which was printed via the XED tool.

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

Before:
$ sudo perf record --event intel_pt//u -- ls
$ sudo perf script --insn-trace
perf 17423 [000] 423271.557970005: 7f2d95f16217 __GI___ioctl+0x7 (/lib/x86_64-linux-gnu/libc-2.27.so) insn: 48 3d 01 f0 ff ff
perf 17423 [000] 423271.557970005: 7f2d95f1621d __GI___ioctl+0xd (/lib/x86_64-linux-gnu/libc-2.27.so) insn: 73 01
perf 17423 [000] 423271.557970338: 7f2d95f1621f __GI___ioctl+0xf (/lib/x86_64-linux-gnu/libc-2.27.so) insn: c3
perf 17423 [000] 423271.557970338: 5593ad3346d7 perf_evsel__enable_cpu+0x97 (/work/linux/tools/perf/perf) insn: 85 c0
perf 17423 [000] 423271.557970338: 5593ad3346d9 perf_evsel__enable_cpu+0x99 (/work/linux/tools/perf/perf) insn: 75 12
perf 17423 [000] 423271.557970338: 5593ad3346db perf_evsel__enable_cpu+0x9b (/work/linux/tools/perf/perf) insn: 49 8b 84 24 a8 00 00 00
perf 17423 [000] 423271.557970338: 5593ad3346e3 perf_evsel__enable_cpu+0xa3 (/work/linux/tools/perf/perf) insn: 48 8b 50 20

After:
$ sudo perf script --insn-trace
perf 17423 [000] 423271.557970005: 7f2d95f16217 __GI___ioctl+0x7 (/lib/x86_64-linux-gnu/libc-2.27.so) insn: cmpq $-0xfff, %rax
perf 17423 [000] 423271.557970005: 7f2d95f1621d __GI___ioctl+0xd (/lib/x86_64-linux-gnu/libc-2.27.so) insn: jae __GI___ioctl+0x10
perf 17423 [000] 423271.557970338: 7f2d95f1621f __GI___ioctl+0xf (/lib/x86_64-linux-gnu/libc-2.27.so) insn: retq
perf 17423 [000] 423271.557970338: 5593ad3346d7 perf_evsel__enable_cpu+0x97 (/work/linux/tools/perf/perf) insn: testl %eax, %eax
perf 17423 [000] 423271.557970338: 5593ad3346d9 perf_evsel__enable_cpu+0x99 (/work/linux/tools/perf/perf) insn: jne perf_evsel__enable_cpu+0xad
perf 17423 [000] 423271.557970338: 5593ad3346db perf_evsel__enable_cpu+0x9b (/work/linux/tools/perf/perf) insn: movq 0xa8(%r12), %rax
perf 17423 [000] 423271.557970338: 5593ad3346e3 perf_evsel__enable_cpu+0xa3 (/work/linux/tools/perf/perf) insn: movq 0x20(%rax), %rdx
perf 17423 [000] 423271.557970338: 5593ad3346e7 perf_evsel__enable_cpu+0xa7 (/work/linux/tools/perf/perf) insn: cmpl %edx, %ebx
perf 17423 [000] 423271.557970338: 5593ad3346e9 perf_evsel__enable_cpu+0xa9 (/work/linux/tools/perf/perf) insn: jl perf_evsel__enable_cpu+0x60
perf 17423 [000] 423271.557970338: 5593ad3346eb perf_evsel__enable_cpu+0xab (/work/linux/tools/perf/perf) insn: xorl %eax, %eax


Changbin Du (3):
perf: build: introduce the libcapstone
perf: script: use capstone disasm engine to show assembly instructions
perf: script: deprecate the '--xed' option

tools/build/Makefile.feature | 2 +
tools/build/feature/Makefile | 4 +
tools/build/feature/test-all.c | 4 +
tools/build/feature/test-libcapstone.c | 11 ++
tools/perf/Documentation/build-xed.txt | 19 ----
tools/perf/Documentation/perf-intel-pt.txt | 17 +--
tools/perf/Documentation/perf-script.txt | 5 +-
tools/perf/Documentation/tips.txt | 2 +-
tools/perf/Makefile.config | 21 ++++
tools/perf/Makefile.perf | 3 +
tools/perf/builtin-script.c | 15 +--
tools/perf/ui/browsers/res_sample.c | 2 +-
tools/perf/ui/browsers/scripts.c | 4 +-
tools/perf/util/Build | 1 +
tools/perf/util/print_insn.c | 118 +++++++++++++++++++++
tools/perf/util/print_insn.h | 14 +++
16 files changed, 194 insertions(+), 48 deletions(-)
create mode 100644 tools/build/feature/test-libcapstone.c
delete mode 100644 tools/perf/Documentation/build-xed.txt
create mode 100644 tools/perf/util/print_insn.c
create mode 100644 tools/perf/util/print_insn.h

--
2.25.1



2024-01-16 13:53:51

by Andi Kleen

[permalink] [raw]
Subject: Re: [PATCH 0/3] perf: script: Intro capstone disasm engine to show instruction trace

Changbin Du <[email protected]> writes:

> This series introduces capstone disassembler engine to print instructions of
> Intel PT trace, which was printed via the XED tool.

FWIW at least on x86 in my experience capstone isn't that great an
disassembler. I used it in another project and ran into many decoding bugs.
They're mostly in obscure corners, but can be fairly annoying.

My other concern with your patchkit is that you change the default
output formats. Since perf script is often used with scripts
(as the name implies) there is a certain expectation that the output
remains stable and parse-able. There are actually use cases where
the raw bytes "insn" output is needed.

I would rather define new perf script output types for the new decoded output,
but keep the old alone.

-Andi

2024-01-17 02:46:12

by Changbin Du

[permalink] [raw]
Subject: Re: [PATCH 0/3] perf: script: Intro capstone disasm engine to show instruction trace

On Tue, Jan 16, 2024 at 05:53:40AM -0800, Andi Kleen wrote:
> Changbin Du <[email protected]> writes:
>
> > This series introduces capstone disassembler engine to print instructions of
> > Intel PT trace, which was printed via the XED tool.
>
> FWIW at least on x86 in my experience capstone isn't that great an
> disassembler. I used it in another project and ran into many decoding bugs.
> They're mostly in obscure corners, but can be fairly annoying.
>
If so, I will preserve the old --xed option as an alternate.

> My other concern with your patchkit is that you change the default
> output formats. Since perf script is often used with scripts
> (as the name implies) there is a certain expectation that the output
> remains stable and parse-able. There are actually use cases where
> the raw bytes "insn" output is needed.
>
> I would rather define new perf script output types for the new decoded output,
> but keep the old alone.
Agreed. What about these?
- Add a new field 'insn_disam' to display decoded output.
- Add params to '--insn-trace' option, that is '--insn-trace=[raw|disam]'.
'raw' is the default value.

>
> -Andi

--
Cheers,
Changbin Du

2024-01-17 03:48:57

by Namhyung Kim

[permalink] [raw]
Subject: Re: [PATCH 0/3] perf: script: Intro capstone disasm engine to show instruction trace

Hello,

On Tue, Jan 16, 2024 at 6:46 PM Changbin Du <[email protected]> wrote:
>
> On Tue, Jan 16, 2024 at 05:53:40AM -0800, Andi Kleen wrote:
> > Changbin Du <[email protected]> writes:
> >
> > > This series introduces capstone disassembler engine to print instructions of
> > > Intel PT trace, which was printed via the XED tool.
> >
> > FWIW at least on x86 in my experience capstone isn't that great an
> > disassembler. I used it in another project and ran into many decoding bugs.
> > They're mostly in obscure corners, but can be fairly annoying.
> >
> If so, I will preserve the old --xed option as an alternate.
>
> > My other concern with your patchkit is that you change the default
> > output formats. Since perf script is often used with scripts
> > (as the name implies) there is a certain expectation that the output
> > remains stable and parse-able. There are actually use cases where
> > the raw bytes "insn" output is needed.
> >
> > I would rather define new perf script output types for the new decoded output,
> > but keep the old alone.
> Agreed. What about these?
> - Add a new field 'insn_disam' to display decoded output.
> - Add params to '--insn-trace' option, that is '--insn-trace=[raw|disam]'.
> 'raw' is the default value.

Sounds good. FWIW capstone has been working fine in my
experience. :)

Thanks,
Namhyung