From: Namhyung Kim <namhyung@kernel.org>
To: Li Huafei <lihuafei1@huawei.com>
Cc: acme@kernel.org, leo.yan@linux.dev, james.clark@linaro.org,
mark.rutland@arm.com, john.g.garry@oracle.com, will@kernel.org,
irogers@google.com, mike.leach@linaro.org, peterz@infradead.org,
mingo@redhat.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, kjain@linux.ibm.com, mhiramat@kernel.org,
atrajeev@linux.vnet.ibm.com, sesse@google.com,
adrian.hunter@intel.com, kan.liang@linux.intel.com,
linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 4/7] perf annotate: Support for the 'extract_reg_offset' callback function in arm64
Date: Mon, 17 Mar 2025 18:45:42 -0700 [thread overview]
Message-ID: <Z9jQRl9mMpBhfc4q@google.com> (raw)
In-Reply-To: <20250314162137.528204-5-lihuafei1@huawei.com>
On Sat, Mar 15, 2025 at 12:21:34AM +0800, Li Huafei wrote:
> At present, only the following two addressing modes are supported:
>
> 1. Base register only (no offset): [base{, #0}]
> 2. Base plus offset (immediate): [base{, #imm}]
>
> For addressing modes where the offset needs to be calculated from the
> register value, it is difficult to know the specific value of the offset
> register, making it impossible to calculate the offset.
>
> Signed-off-by: Li Huafei <lihuafei1@huawei.com>
> ---
> tools/perf/arch/arm64/annotate/instructions.c | 62 +++++++++++++++++++
> tools/perf/util/Build | 1 +
> tools/perf/util/disasm.c | 1 +
> tools/perf/util/dwarf-regs-arm64.c | 25 ++++++++
> tools/perf/util/include/dwarf-regs.h | 7 +++
> 5 files changed, 96 insertions(+)
> create mode 100644 tools/perf/util/dwarf-regs-arm64.c
>
> diff --git a/tools/perf/arch/arm64/annotate/instructions.c b/tools/perf/arch/arm64/annotate/instructions.c
> index c212eb7341bd..54497b72a5c5 100644
> --- a/tools/perf/arch/arm64/annotate/instructions.c
> +++ b/tools/perf/arch/arm64/annotate/instructions.c
> @@ -188,3 +188,65 @@ static int arm64__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
> free(arm);
> return SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
> }
> +
> +
> +/*
> + * Get the base register number and access offset in load/store instructions.
> + * At present, only the following two addressing modes are supported:
> + *
> + * 1. Base register only (no offset): [base{, #0}]
> + * 2. Base plus offset (immediate): [base{, #imm}]
> + *
> + * For addressing modes where the offset needs to be calculated from the
> + * register value, it is difficult to know the specific value of the offset
> + * register, making it impossible to calculate the offset.
> + *
> + * Fills @reg and @offset when return 0.
> + */
> +static int
> +extract_reg_offset_arm64(struct arch *arch __maybe_unused,
> + struct disasm_line *dl __maybe_unused,
> + const char *insn_str, int insn_ops __maybe_unused,
> + struct annotated_op_loc *op_loc)
> +{
> + char *str;
> + regmatch_t match[4];
> + static regex_t reg_off_regex;
> + static bool regex_compiled;
> +
> + if (!regex_compiled) {
> + regcomp(®_off_regex, "^\\[(sp|[xw][0-9]{1,2})(, #(-?[0-9]+))?\\].*",
> + REG_EXTENDED);
> + regex_compiled = true;
Probably better to put it in the arch specific data and free it when you
add arch__annotate_exit().
> + }
> +
> + if (!op_loc->mem_ref)
> + return 0;
> +
> + if (regexec(®_off_regex, insn_str, 4, match, 0))
> + return -1;
> +
> + str = strdup(insn_str);
> + if (!str)
> + return -1;
> +
> + /* Get the base register number. */
> + str[match[1].rm_eo] = '\0';
> + op_loc->reg1 = get_arm64_regnum(str + match[1].rm_so);
> +
> + /*
> + * If there is an immediate offset, match[2] records the start and end
> + * positions of "#imm".
> + */
> + if (match[2].rm_so == -1) {
> + free(str);
> + return 0;
> + }
> +
> + /* Get the immediate offset. */
> + str[match[3].rm_eo] = '\0';
> + op_loc->offset = strtol(str + match[3].rm_so, NULL, 0);
Can you please clarify what match 1,2,3 mean - hopefully with an
example?
Thanks,
Namhyung
> +
> + free(str);
> + return 0;
> +}
> diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> index 5ec97e8d6b6d..d408cbe94fdd 100644
> --- a/tools/perf/util/Build
> +++ b/tools/perf/util/Build
> @@ -210,6 +210,7 @@ perf-util-$(CONFIG_LIBDW) += dwarf-regs.o
> perf-util-$(CONFIG_LIBDW) += dwarf-regs-csky.o
> perf-util-$(CONFIG_LIBDW) += dwarf-regs-powerpc.o
> perf-util-$(CONFIG_LIBDW) += dwarf-regs-x86.o
> +perf-util-$(CONFIG_LIBDW) += dwarf-regs-arm64.o
> perf-util-$(CONFIG_LIBDW) += debuginfo.o
> perf-util-$(CONFIG_LIBDW) += annotate-data.o
>
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 905eceb824a4..1035c60a8545 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -128,6 +128,7 @@ static struct arch architectures[] = {
> {
> .name = "arm64",
> .init = arm64__annotate_init,
> + .extract_reg_offset = extract_reg_offset_arm64,
> },
> {
> .name = "csky",
> diff --git a/tools/perf/util/dwarf-regs-arm64.c b/tools/perf/util/dwarf-regs-arm64.c
> new file mode 100644
> index 000000000000..edf41c059967
> --- /dev/null
> +++ b/tools/perf/util/dwarf-regs-arm64.c
> @@ -0,0 +1,25 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Mapping of DWARF debug register numbers into register names.
> + *
> + * Copyright (c) 2025 Huawei Inc, Li Huafei <lihuafei1@huawei.com>
> + */
> +#include <errno.h>
> +#include <string.h>
> +#include <dwarf-regs.h>
> +
> +int get_arm64_regnum(const char *name)
> +{
> + int reg;
> +
> + if (!strcmp(name, "sp"))
> + return 31;
> +
> + if (*name != 'x' && *name != 'w')
> + return -EINVAL;
> +
> + name++;
> + reg = strtol(name, NULL, 0);
> +
> + return reg >= 0 && reg <= 30 ? reg : -EINVAL;
> +}
> diff --git a/tools/perf/util/include/dwarf-regs.h b/tools/perf/util/include/dwarf-regs.h
> index 6f1b9f6b2466..81cc5f69a391 100644
> --- a/tools/perf/util/include/dwarf-regs.h
> +++ b/tools/perf/util/include/dwarf-regs.h
> @@ -101,6 +101,8 @@ const char *get_dwarf_regstr(unsigned int n, unsigned int machine, unsigned int
>
> int get_x86_regnum(const char *name);
>
> +int get_arm64_regnum(const char *name);
> +
> #if !defined(__x86_64__) && !defined(__i386__)
> int get_arch_regnum(const char *name);
> #endif
> @@ -128,6 +130,11 @@ static inline void get_powerpc_regs(u32 raw_insn __maybe_unused, int is_source _
> {
> return;
> }
> +
> +static inline int get_arm64_regnum(const char *name __maybe_unused)
> +{
> + return -1;
> +}
> #endif
>
> #endif
> --
> 2.25.1
>
next prev parent reply other threads:[~2025-03-18 1:47 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-14 16:21 [PATCH 0/7] Add data type profiling support for arm64 Li Huafei
2025-03-14 16:21 ` [PATCH 1/7] perf annotate: Handle arm64 load and store instructions Li Huafei
2025-03-18 1:32 ` Namhyung Kim
2025-03-18 17:15 ` Leo Yan
2025-03-14 16:21 ` [PATCH 2/7] perf annotate: Advance the mem_ref check to mov__parse() Li Huafei
2025-03-18 18:02 ` Leo Yan
2025-03-14 16:21 ` [PATCH 3/7] perf annotate: Add 'extract_reg_offset' callback function to extract register number and access offset Li Huafei
2025-03-14 16:21 ` [PATCH 4/7] perf annotate: Support for the 'extract_reg_offset' callback function in arm64 Li Huafei
2025-03-18 1:45 ` Namhyung Kim [this message]
2025-03-14 16:21 ` [PATCH 5/7] perf annotate-data: Support instruction tracking for arm64 Li Huafei
2025-03-18 1:51 ` Namhyung Kim
2025-03-14 16:21 ` [PATCH 6/7] perf annotate-data: Handle arm64 global variable access Li Huafei
2025-03-18 2:01 ` Namhyung Kim
2025-03-14 16:21 ` [PATCH 7/7] perf annotate-data: Handle the access to the 'current' pointer on arm64 Li Huafei
2025-03-18 2:06 ` Namhyung Kim
2025-03-18 1:25 ` [PATCH 0/7] Add data type profiling support for arm64 Namhyung Kim
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=Z9jQRl9mMpBhfc4q@google.com \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=atrajeev@linux.vnet.ibm.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=john.g.garry@oracle.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=kjain@linux.ibm.com \
--cc=leo.yan@linux.dev \
--cc=lihuafei1@huawei.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mike.leach@linaro.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=sesse@google.com \
--cc=will@kernel.org \
/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 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).