From: Namhyung Kim <namhyung@kernel.org>
To: Tengda Wu <wutengda@huaweicloud.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
leo.yan@linux.dev, Li Huafei <lihuafei1@huawei.com>,
Ian Rogers <irogers@google.com>,
Kim Phillips <kim.phillips@arm.com>,
Mark Rutland <mark.rutland@arm.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Ingo Molnar <mingo@redhat.com>, Bill Wendling <morbo@google.com>,
Nick Desaulniers <nick.desaulniers+lkml@gmail.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Zecheng Li <zli94@ncsu.edu>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
llvm@lists.linux.dev
Subject: Re: [PATCH v2 02/16] perf capstone: Fix arm64 jump/adrp disassembly mismatch with objdump
Date: Mon, 6 Apr 2026 23:43:24 -0700 [thread overview]
Message-ID: <adSnjABSDeeg-9O8@z2> (raw)
In-Reply-To: <20260403094800.1418825-3-wutengda@huaweicloud.com>
On Fri, Apr 03, 2026 at 09:47:46AM +0000, Tengda Wu wrote:
> The jump and adrp instructions parsed by libcapstone currently lack
> symbolic representation and use a '#' prefix for addresses. This
> format is inconsistent with objdump's output, which causes subsequent
> parsing in jump__parse() and arm64_mov__parse() to fail.
>
> Example mismatch:
> Current: b #0xffff8000800114c8
> Fix: b ffff8000800114c8 <el0t_64_sync+0x108>
>
> Current: adrp x18, #0xffff800081f5f000
> Fix: adrp x18, ffff800081f5f000 <this_cpu_vector>
>
> Fix this by implementing extended formatting for these arm64
> instructions during symbol__disassemble_capstone(). This ensures
> the output matches objdump's expected style, including the raw
> address and the associated <symbol+offset> suffix.
>
> Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
> ---
> tools/perf/util/capstone.c | 107 ++++++++++++++++++++++++++++++++-----
> tools/perf/util/disasm.c | 5 ++
> tools/perf/util/disasm.h | 1 +
> 3 files changed, 101 insertions(+), 12 deletions(-)
>
> diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c
> index 25cf6e15ec27..1d8421d2d98c 100644
> --- a/tools/perf/util/capstone.c
> +++ b/tools/perf/util/capstone.c
> @@ -255,10 +255,6 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,
> struct map *map = args->ms->map;
> struct symbol *sym;
>
> - /* TODO: support more architectures */
> - if (!arch__is_x86(args->arch))
> - return;
> -
> if (insn->detail == NULL)
> return;
>
> @@ -305,6 +301,98 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,
> }
> }
>
> +static void format_capstone_insn_x86(struct cs_insn *insn, char *buf,
> + size_t len, struct annotate_args *args,
> + u64 addr)
> +{
> + int printed;
> +
> + printed = scnprintf(buf, len, " %-7s %s",
> + insn->mnemonic, insn->op_str);
> + buf += printed;
> + len -= printed;
> +
> + print_capstone_detail(insn, buf, len, args, addr);
> +}
> +
> +static void format_capstone_insn_arm64(struct cs_insn *insn, char *buf,
> + size_t len, struct annotate_args *args,
> + u64 addr)
> +{
> + struct map *map = args->ms->map;
> + struct symbol *sym;
> + char *last_imm, *endptr;
> + u64 orig_addr;
> +
> + scnprintf(buf, len, " %-7s %s",
> + insn->mnemonic, insn->op_str);
> + /*
> + * Adjust instructions to keep the existing behavior with objdump.
> + *
> + * Example conversion:
> + * From: b #0xffff8000800114c8
> + * To: b ffff8000800114c8 <el0t_64_sync+0x108>
> + */
> + switch (insn->id) {
> + case ARM64_INS_B:
> + case ARM64_INS_BL:
> + case ARM64_INS_CBNZ:
> + case ARM64_INS_CBZ:
> + case ARM64_INS_TBNZ:
> + case ARM64_INS_TBZ:
> + case ARM64_INS_ADRP:
> + /* Extract last immediate value as address */
> + last_imm = strrchr(buf, '#');
> + if (!last_imm)
> + return;
> +
> + orig_addr = strtoull(last_imm + 1, &endptr, 16);
> + if (endptr == last_imm + 1)
> + return;
> +
> + /* Relocate map that contains the address */
> + if (dso__kernel(map__dso(map))) {
> + map = maps__find(map__kmaps(map), orig_addr);
> + if (map == NULL)
> + return;
I know you copied the logic from x86, but I've realized that it leaks a
refcount for the new kernel map returned from maps__find(). This needs
to be fixed separately.
Thanks,
Namhyung
> + }
> +
> + /* Convert it to map-relative address for search */
> + addr = map__map_ip(map, orig_addr);
> +
> + sym = map__find_symbol(map, addr);
> + if (sym == NULL)
> + return;
> +
> + /* Symbolize the resolved address */
> + len = len - (last_imm - buf);
> + if (addr == sym->start) {
> + scnprintf(last_imm, len, "%"PRIx64" <%s>",
> + orig_addr, sym->name);
> + } else {
> + scnprintf(last_imm, len, "%"PRIx64" <%s+%#"PRIx64">",
> + orig_addr, sym->name, addr - sym->start);
> + }
> + break;
> + default:
> + break;
> + }
> +}
> +
> +static void format_capstone_insn(struct cs_insn *insn, char *buf, size_t len,
> + struct annotate_args *args, u64 addr)
> +{
> + /* TODO: support more architectures */
> + if (arch__is_x86(args->arch))
> + format_capstone_insn_x86(insn, buf, len, args, addr);
> + else if (arch__is_arm64(args->arch))
> + format_capstone_insn_arm64(insn, buf, len, args, addr);
> + else {
> + scnprintf(buf, len, " %-7s %s",
> + insn->mnemonic, insn->op_str);
> + }
> +}
> +
> struct find_file_offset_data {
> u64 ip;
> u64 offset;
> @@ -381,14 +469,9 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
>
> free_count = count = perf_cs_disasm(handle, buf, buf_len, start, buf_len, &insn);
> for (i = 0, offset = 0; i < count; i++) {
> - int printed;
> -
> - printed = scnprintf(disasm_buf, sizeof(disasm_buf),
> - " %-7s %s",
> - insn[i].mnemonic, insn[i].op_str);
> - print_capstone_detail(&insn[i], disasm_buf + printed,
> - sizeof(disasm_buf) - printed, args,
> - start + offset);
> + format_capstone_insn(&insn[i], disasm_buf,
> + sizeof(disasm_buf), args,
> + start + offset);
>
> args->offset = offset;
> args->line = disasm_buf;
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 40fcaed5d0b1..988b2b748e11 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -202,6 +202,11 @@ bool arch__is_powerpc(const struct arch *arch)
> return arch->id.e_machine == EM_PPC || arch->id.e_machine == EM_PPC64;
> }
>
> +bool arch__is_arm64(const struct arch *arch)
> +{
> + return arch->id.e_machine == EM_AARCH64;
> +}
> +
> static void ins_ops__delete(struct ins_operands *ops)
> {
> if (ops == NULL)
> diff --git a/tools/perf/util/disasm.h b/tools/perf/util/disasm.h
> index a6e478caf61a..d3730ed86dba 100644
> --- a/tools/perf/util/disasm.h
> +++ b/tools/perf/util/disasm.h
> @@ -111,6 +111,7 @@ struct annotate_args {
> const struct arch *arch__find(uint16_t e_machine, uint32_t e_flags, const char *cpuid);
> bool arch__is_x86(const struct arch *arch);
> bool arch__is_powerpc(const struct arch *arch);
> +bool arch__is_arm64(const struct arch *arch);
>
> extern const struct ins_ops call_ops;
> extern const struct ins_ops dec_ops;
> --
> 2.34.1
>
next prev parent reply other threads:[~2026-04-07 6:43 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-03 9:47 [PATCH v2 00/16] perf arm64: Support data type profiling Tengda Wu
2026-04-03 9:47 ` [PATCH v2 01/16] perf llvm: Fix arm64 adrp instruction disassembly mismatch with objdump Tengda Wu
2026-04-03 9:47 ` [PATCH v2 02/16] perf capstone: Fix arm64 jump/adrp " Tengda Wu
2026-04-07 6:43 ` Namhyung Kim [this message]
2026-04-10 9:08 ` Tengda Wu
2026-04-14 13:51 ` James Clark
2026-04-03 9:47 ` [PATCH v2 03/16] perf annotate-arm64: Generalize arm64_mov__parse to support standard operands Tengda Wu
2026-04-07 6:58 ` Namhyung Kim
2026-04-10 10:06 ` Tengda Wu
2026-04-14 14:13 ` James Clark
2026-04-03 9:47 ` [PATCH v2 04/16] perf annotate-arm64: Handle load and store instructions Tengda Wu
2026-04-07 7:09 ` Namhyung Kim
2026-04-10 10:16 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 05/16] perf annotate: Introduce extract_op_location callback for arch-specific parsing Tengda Wu
2026-04-03 9:47 ` [PATCH v2 06/16] perf dwarf-regs: Adapt get_dwarf_regnum() for arm64 Tengda Wu
2026-04-03 9:47 ` [PATCH v2 07/16] perf annotate-arm64: Implement extract_op_location() callback Tengda Wu
2026-04-07 7:26 ` Namhyung Kim
2026-04-10 10:27 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 08/16] perf annotate-arm64: Enable instruction tracking support Tengda Wu
2026-04-10 6:09 ` Namhyung Kim
2026-04-10 10:29 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 09/16] perf annotate-arm64: Support load instruction tracking Tengda Wu
2026-04-10 6:23 ` Namhyung Kim
2026-04-10 10:37 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 10/16] perf annotate-arm64: Support store " Tengda Wu
2026-04-03 9:47 ` [PATCH v2 11/16] perf annotate-arm64: Support stack variable tracking Tengda Wu
2026-04-10 6:29 ` Namhyung Kim
2026-04-10 10:41 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 12/16] perf annotate-arm64: Support 'mov' instruction tracking Tengda Wu
2026-04-10 6:39 ` Namhyung Kim
2026-04-10 10:53 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 13/16] perf annotate-arm64: Support 'add' " Tengda Wu
2026-04-10 6:42 ` Namhyung Kim
2026-04-10 10:49 ` Tengda Wu
2026-04-03 9:47 ` [PATCH v2 14/16] perf annotate-arm64: Support 'adrp' instruction to track global variables Tengda Wu
2026-04-03 9:47 ` [PATCH v2 15/16] perf annotate-arm64: Support per-cpu variable access tracking Tengda Wu
2026-04-03 9:48 ` [PATCH v2 16/16] perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer Tengda Wu
2026-04-10 6:52 ` Namhyung Kim
2026-04-10 10:44 ` Tengda Wu
2026-04-07 6:31 ` [PATCH v2 00/16] perf arm64: Support data type profiling Namhyung Kim
2026-04-08 11:35 ` Tengda Wu
2026-04-10 7:00 ` Namhyung Kim
2026-04-10 8:17 ` Tengda Wu
2026-04-14 15:10 ` James Clark
2026-04-15 1:34 ` Tengda Wu
2026-04-16 15:31 ` James Clark
2026-04-17 1:53 ` Tengda Wu
2026-04-20 9:31 ` James Clark
2026-04-22 9:50 ` James Clark
2026-04-27 8:43 ` Tengda Wu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=adSnjABSDeeg-9O8@z2 \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=kim.phillips@arm.com \
--cc=leo.yan@linux.dev \
--cc=lihuafei1@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=morbo@google.com \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=peterz@infradead.org \
--cc=wutengda@huaweicloud.com \
--cc=zli94@ncsu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.