2024-04-25 00:53:06

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH 1/2] perf annotate: Fallback to objdump when capstone fails

I found some cases that capstone failed to disassemble. Probably my
capstone is an old version but anyway there's a chance it can fail. And
then it silently stopped in the middle. In my case, it didn't
understand "RDPKRU" instruction.

Let's check if the capstone disassemble reached to the end of the
function. And fallback to objdump if not.

Signed-off-by: Namhyung Kim <[email protected]>
---
tools/perf/util/disasm.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 92937809be85..412101f2cf2a 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -1542,6 +1542,20 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
offset += insn[i].size;
}

+ /* It failed in the middle: probably due to unknown instructions */
+ if (offset != len) {
+ struct list_head *list = &notes->src->source;
+
+ /* Discard all lines and fallback to objdump */
+ while (!list_empty(list)) {
+ dl = list_first_entry(list, struct disasm_line, al.node);
+
+ list_del_init(&dl->al.node);
+ disasm_line__free(dl);
+ }
+ count = -1;
+ }
+
out:
if (needs_cs_close)
cs_close(&handle);
--
2.44.0.769.g3c40516874-goog



2024-04-25 14:02:49

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 1/2] perf annotate: Fallback to objdump when capstone fails

On Wed, Apr 24, 2024 at 05:51:56PM -0700, Namhyung Kim wrote:
> I found some cases that capstone failed to disassemble. Probably my
> capstone is an old version but anyway there's a chance it can fail. And
> then it silently stopped in the middle. In my case, it didn't
> understand "RDPKRU" instruction.
>
> Let's check if the capstone disassemble reached to the end of the
> function. And fallback to objdump if not.

Thanks, applied to perf-tools-next,

- Arnaldo

> Signed-off-by: Namhyung Kim <[email protected]>
> ---
> tools/perf/util/disasm.c | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 92937809be85..412101f2cf2a 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -1542,6 +1542,20 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
> offset += insn[i].size;
> }
>
> + /* It failed in the middle: probably due to unknown instructions */
> + if (offset != len) {
> + struct list_head *list = &notes->src->source;
> +
> + /* Discard all lines and fallback to objdump */
> + while (!list_empty(list)) {
> + dl = list_first_entry(list, struct disasm_line, al.node);
> +
> + list_del_init(&dl->al.node);
> + disasm_line__free(dl);
> + }
> + count = -1;
> + }
> +
> out:
> if (needs_cs_close)
> cs_close(&handle);
> --
> 2.44.0.769.g3c40516874-goog

2024-04-25 14:12:38

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 1/2] perf annotate: Fallback to objdump when capstone fails

On Wed, Apr 24, 2024 at 05:51:56PM -0700, Namhyung Kim wrote:
> I found some cases that capstone failed to disassemble. Probably my
> capstone is an old version but anyway there's a chance it can fail. And
> then it silently stopped in the middle. In my case, it didn't
> understand "RDPKRU" instruction.
>
> Let's check if the capstone disassemble reached to the end of the
> function. And fallback to objdump if not.

Humm:

⬢[acme@toolbox perf-tools-next]$ git am ./20240424_namhyung_perf_annotate_fallback_to_objdump_when_capstone_fails.mbx
Applying: perf annotate: Fallback to objdump when capstone fails
Applying: perf annotate: Update dso binary type when try build-id
error: patch failed: tools/perf/util/disasm.c:1156
error: tools/perf/util/disasm.c: patch does not apply
Patch failed at 0002 perf annotate: Update dso binary type when try build-id
hint: Use 'git am --show-current-patch=diff' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
⬢[acme@toolbox perf-tools-next]$ git am --abort
⬢[acme@toolbox perf-tools-next]$

checking...

- Arnado

> Signed-off-by: Namhyung Kim <[email protected]>
> ---
> tools/perf/util/disasm.c | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 92937809be85..412101f2cf2a 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -1542,6 +1542,20 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
> offset += insn[i].size;
> }
>
> + /* It failed in the middle: probably due to unknown instructions */
> + if (offset != len) {
> + struct list_head *list = &notes->src->source;
> +
> + /* Discard all lines and fallback to objdump */
> + while (!list_empty(list)) {
> + dl = list_first_entry(list, struct disasm_line, al.node);
> +
> + list_del_init(&dl->al.node);
> + disasm_line__free(dl);
> + }
> + count = -1;
> + }
> +
> out:
> if (needs_cs_close)
> cs_close(&handle);
> --
> 2.44.0.769.g3c40516874-goog