Skip to contentSkip to conversation

perf regs: Refactor use of arch__sample_reg_masks to perf_reg_name

PATCH Applied Ian Rogers 4 participants 10 messages Jan 21 – Mar 24

Subsystems (4)
lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH v2 1/1 perf regs: Refactor use of arch__sample_reg_masks to perf_reg_name

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-	const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-	opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+	opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43

58 lines not on this page · raw message

tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68

77 lines not on this page · raw message

tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47

59 lines not on this page · raw message

tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
 	size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
 	struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +73 -43

138 lines not on this page · raw message

tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
 	return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
 	const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-	const char *name;
-	uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
 	SDT_ARG_VALID = 0,
 	SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
Signature · 1 line
2.52.0.457.g6b5491de43-goog
#1 lore raw
Quoted 454 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-	const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-	opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+	opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE	(1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(x0, PERF_REG_ARM64_X0),
-	SMPL_REG(x1, PERF_REG_ARM64_X1),
-	SMPL_REG(x2, PERF_REG_ARM64_X2),
-	SMPL_REG(x3, PERF_REG_ARM64_X3),
-	SMPL_REG(x4, PERF_REG_ARM64_X4),
-	SMPL_REG(x5, PERF_REG_ARM64_X5),
-	SMPL_REG(x6, PERF_REG_ARM64_X6),
-	SMPL_REG(x7, PERF_REG_ARM64_X7),
-	SMPL_REG(x8, PERF_REG_ARM64_X8),
-	SMPL_REG(x9, PERF_REG_ARM64_X9),
-	SMPL_REG(x10, PERF_REG_ARM64_X10),
-	SMPL_REG(x11, PERF_REG_ARM64_X11),
-	SMPL_REG(x12, PERF_REG_ARM64_X12),
-	SMPL_REG(x13, PERF_REG_ARM64_X13),
-	SMPL_REG(x14, PERF_REG_ARM64_X14),
-	SMPL_REG(x15, PERF_REG_ARM64_X15),
-	SMPL_REG(x16, PERF_REG_ARM64_X16),
-	SMPL_REG(x17, PERF_REG_ARM64_X17),
-	SMPL_REG(x18, PERF_REG_ARM64_X18),
-	SMPL_REG(x19, PERF_REG_ARM64_X19),
-	SMPL_REG(x20, PERF_REG_ARM64_X20),
-	SMPL_REG(x21, PERF_REG_ARM64_X21),
-	SMPL_REG(x22, PERF_REG_ARM64_X22),
-	SMPL_REG(x23, PERF_REG_ARM64_X23),
-	SMPL_REG(x24, PERF_REG_ARM64_X24),
-	SMPL_REG(x25, PERF_REG_ARM64_X25),
-	SMPL_REG(x26, PERF_REG_ARM64_X26),
-	SMPL_REG(x27, PERF_REG_ARM64_X27),
-	SMPL_REG(x28, PERF_REG_ARM64_X28),
-	SMPL_REG(x29, PERF_REG_ARM64_X29),
-	SMPL_REG(lr, PERF_REG_ARM64_LR),
-	SMPL_REG(sp, PERF_REG_ARM64_SP),
-	SMPL_REG(pc, PERF_REG_ARM64_PC),
-	SMPL_REG(vg, PERF_REG_ARM64_VG),
-	SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
 	}
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10		0x0080
 #define PVR_POWER11		0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(r0, PERF_REG_POWERPC_R0),
-	SMPL_REG(r1, PERF_REG_POWERPC_R1),
-	SMPL_REG(r2, PERF_REG_POWERPC_R2),
-	SMPL_REG(r3, PERF_REG_POWERPC_R3),
-	SMPL_REG(r4, PERF_REG_POWERPC_R4),
-	SMPL_REG(r5, PERF_REG_POWERPC_R5),
-	SMPL_REG(r6, PERF_REG_POWERPC_R6),
-	SMPL_REG(r7, PERF_REG_POWERPC_R7),
-	SMPL_REG(r8, PERF_REG_POWERPC_R8),
-	SMPL_REG(r9, PERF_REG_POWERPC_R9),
-	SMPL_REG(r10, PERF_REG_POWERPC_R10),
-	SMPL_REG(r11, PERF_REG_POWERPC_R11),
-	SMPL_REG(r12, PERF_REG_POWERPC_R12),
-	SMPL_REG(r13, PERF_REG_POWERPC_R13),
-	SMPL_REG(r14, PERF_REG_POWERPC_R14),
-	SMPL_REG(r15, PERF_REG_POWERPC_R15),
-	SMPL_REG(r16, PERF_REG_POWERPC_R16),
-	SMPL_REG(r17, PERF_REG_POWERPC_R17),
-	SMPL_REG(r18, PERF_REG_POWERPC_R18),
-	SMPL_REG(r19, PERF_REG_POWERPC_R19),
-	SMPL_REG(r20, PERF_REG_POWERPC_R20),
-	SMPL_REG(r21, PERF_REG_POWERPC_R21),
-	SMPL_REG(r22, PERF_REG_POWERPC_R22),
-	SMPL_REG(r23, PERF_REG_POWERPC_R23),
-	SMPL_REG(r24, PERF_REG_POWERPC_R24),
-	SMPL_REG(r25, PERF_REG_POWERPC_R25),
-	SMPL_REG(r26, PERF_REG_POWERPC_R26),
-	SMPL_REG(r27, PERF_REG_POWERPC_R27),
-	SMPL_REG(r28, PERF_REG_POWERPC_R28),
-	SMPL_REG(r29, PERF_REG_POWERPC_R29),
-	SMPL_REG(r30, PERF_REG_POWERPC_R30),
-	SMPL_REG(r31, PERF_REG_POWERPC_R31),
-	SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-	SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-	SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-	SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-	SMPL_REG(link, PERF_REG_POWERPC_LINK),
-	SMPL_REG(xer, PERF_REG_POWERPC_XER),
-	SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-	SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-	SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-	SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-	SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-	SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-	SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-	SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-	SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-	SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-	SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-	SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-	SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-	SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-	SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-	SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-	SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-	SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-	SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-	SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-	SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-	SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(AX, PERF_REG_X86_AX),
-	SMPL_REG(BX, PERF_REG_X86_BX),
-	SMPL_REG(CX, PERF_REG_X86_CX),
-	SMPL_REG(DX, PERF_REG_X86_DX),
-	SMPL_REG(SI, PERF_REG_X86_SI),
-	SMPL_REG(DI, PERF_REG_X86_DI),
-	SMPL_REG(BP, PERF_REG_X86_BP),
-	SMPL_REG(SP, PERF_REG_X86_SP),
-	SMPL_REG(IP, PERF_REG_X86_IP),
-	SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-	SMPL_REG(CS, PERF_REG_X86_CS),
-	SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-	SMPL_REG(R8, PERF_REG_X86_R8),
-	SMPL_REG(R9, PERF_REG_X86_R9),
-	SMPL_REG(R10, PERF_REG_X86_R10),
-	SMPL_REG(R11, PERF_REG_X86_R11),
-	SMPL_REG(R12, PERF_REG_X86_R12),
-	SMPL_REG(R13, PERF_REG_X86_R13),
-	SMPL_REG(R14, PERF_REG_X86_R14),
-	SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-	SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-	SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-	SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-	SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-	SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-	SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-	SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-	SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-	SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-	SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-	SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-	SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-	SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-	SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-	SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-	SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-	SMPL_REG_END
-};
-
 struct sdt_name_reg {
 	const char *sdt_name;
 	const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
 	return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
 	struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
 	size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
 	struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +1 -0
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>

tools/perf/util/parse-regs-options.c line 18

Quoted 10 lines
tools/perf/util/parse-regs-options.c +6 -0
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+	const char *last_name = NULL;
+
+	fprintf(fp, "available registers: ");
+	for (int reg = 0; reg < 64; reg++) {

Could we not use the magic number 64? It's not safe, the supported GP register number could exceed 64 in the future, then the exceeded register won't be iterated.

How about this?

(quoted diff) +0 -0

b/tools/perf/util/parse-regs-options.c
index c0d0ef9fd495..5177016a7ad4 100644

tools/perf/util/parse-regs-options.c +0 -0
@@ -15,7 +15,7 @@ static void list_perf_regs(FILE *fp, uint64_t mask)

        const char *last_name = NULL;

        fprintf(fp, "available registers: ");
-       for (int reg = 0; reg < 64; reg++) {
+       for (unsigned long reg = 0; reg < sizeof(mask) * BITS_PER_BYTE; reg++) {
                const char *name;

                if (((1ULL << reg) & mask) == 0)

Quoted 8 lines
tools/perf/util/parse-regs-options.c +8 -0
+		const char *name;
+
+		if (((1ULL << reg) & mask) == 0)
+			continue;
+
+		name = perf_reg_name(reg, EM_HOST);
+		if (name && (!last_name || strcmp(last_name, name)))
+			fprintf(fp, "%s%s", reg > 0 ? " " : "", name);

tools/perf/util/parse-regs-options.c line 36

Quoted 10 lines
tools/perf/util/parse-regs-options.c +10 -0
+		last_name = name;
+	}
+	fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+	uint64_t reg_mask = 0;
+
+	for (int reg = 0; reg < 64; reg++) {

ditto.

tools/perf/util/parse-regs-options.c line 47

Quoted 11 lines
tools/perf/util/parse-regs-options.c +11 -0
+		const char *name;
+
+		if (((1ULL << reg) & mask) == 0)
+			continue;
+
+		name = perf_reg_name(reg, EM_HOST);
+		if (!name)
+			continue;
+
+		if (!strcasecmp(to_match, name))
+			reg_mask |= 1ULL << reg;

How much register names are expected to match here? It seems the function expects to match only one register name. If so, we can "break" here.

Thanks.

Quoted 146 lines
tools/perf/util/parse-regs-options.c +37 -43
+	}
+	return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
 	uint64_t *mode = (uint64_t *)opt->value;
-	const struct sample_reg *r = NULL;
 	char *s, *os = NULL, *p;
 	int ret = -1;
 	uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 	if (*mode)
 		return -1;
 
-	if (intr)
-		mask = arch__intr_reg_mask();
-	else
-		mask = arch__user_reg_mask();
-
 	/* str may be NULL in case no arg is passed to -I */
-	if (str) {
-		/* because str is read-only */
-		s = os = strdup(str);
-		if (!s)
-			return -1;
-
-		for (;;) {
-			p = strchr(s, ',');
-			if (p)
-				*p = '\0';
-
-			if (!strcmp(s, "?")) {
-				fprintf(stderr, "available registers: ");
-				for (r = arch__sample_reg_masks(); r->name; r++) {
-					if (r->mask & mask)
-						fprintf(stderr, "%s ", r->name);
-				}
-				fputc('\n', stderr);
-				/* just printing available regs */
-				goto error;
-			}
-			for (r = arch__sample_reg_masks(); r->name; r++) {
-				if ((r->mask & mask) && !strcasecmp(s, r->name))
-					break;
-			}
-			if (!r || !r->name) {
-				ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-					    s, intr ? "-I" : "--user-regs=");
-				goto error;
-			}
-
-			*mode |= r->mask;
-
-			if (!p)
-				break;
-
-			s = p + 1;
+	if (!str)
+		return -1;
+
+	mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+	/* because str is read-only */
+	s = os = strdup(str);
+	if (!s)
+		return -1;
+
+	for (;;) {
+		uint64_t reg_mask;
+
+		p = strchr(s, ',');
+		if (p)
+			*p = '\0';
+
+		if (!strcmp(s, "?")) {
+			list_perf_regs(stderr, mask);
+			goto error;
+		}
+
+		reg_mask = name_to_perf_reg_mask(s, mask);
+		if (reg_mask == 0) {
+			ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+				s, intr ? "-I" : "--user-regs=");
+			goto error;
 		}
+		*mode |= reg_mask;
+
+		if (!p)
+			break;
+
+		s = p + 1;
 	}
 	ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
 	return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
 	const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-	const char *name;
-	uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
 	SDT_ARG_VALID = 0,
 	SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
author reviewer
#2 lore raw
Quoted 461 lines · Dapeng
Quoted 458 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-     const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-     opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+     opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE    (1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(x0, PERF_REG_ARM64_X0),
-     SMPL_REG(x1, PERF_REG_ARM64_X1),
-     SMPL_REG(x2, PERF_REG_ARM64_X2),
-     SMPL_REG(x3, PERF_REG_ARM64_X3),
-     SMPL_REG(x4, PERF_REG_ARM64_X4),
-     SMPL_REG(x5, PERF_REG_ARM64_X5),
-     SMPL_REG(x6, PERF_REG_ARM64_X6),
-     SMPL_REG(x7, PERF_REG_ARM64_X7),
-     SMPL_REG(x8, PERF_REG_ARM64_X8),
-     SMPL_REG(x9, PERF_REG_ARM64_X9),
-     SMPL_REG(x10, PERF_REG_ARM64_X10),
-     SMPL_REG(x11, PERF_REG_ARM64_X11),
-     SMPL_REG(x12, PERF_REG_ARM64_X12),
-     SMPL_REG(x13, PERF_REG_ARM64_X13),
-     SMPL_REG(x14, PERF_REG_ARM64_X14),
-     SMPL_REG(x15, PERF_REG_ARM64_X15),
-     SMPL_REG(x16, PERF_REG_ARM64_X16),
-     SMPL_REG(x17, PERF_REG_ARM64_X17),
-     SMPL_REG(x18, PERF_REG_ARM64_X18),
-     SMPL_REG(x19, PERF_REG_ARM64_X19),
-     SMPL_REG(x20, PERF_REG_ARM64_X20),
-     SMPL_REG(x21, PERF_REG_ARM64_X21),
-     SMPL_REG(x22, PERF_REG_ARM64_X22),
-     SMPL_REG(x23, PERF_REG_ARM64_X23),
-     SMPL_REG(x24, PERF_REG_ARM64_X24),
-     SMPL_REG(x25, PERF_REG_ARM64_X25),
-     SMPL_REG(x26, PERF_REG_ARM64_X26),
-     SMPL_REG(x27, PERF_REG_ARM64_X27),
-     SMPL_REG(x28, PERF_REG_ARM64_X28),
-     SMPL_REG(x29, PERF_REG_ARM64_X29),
-     SMPL_REG(lr, PERF_REG_ARM64_LR),
-     SMPL_REG(sp, PERF_REG_ARM64_SP),
-     SMPL_REG(pc, PERF_REG_ARM64_PC),
-     SMPL_REG(vg, PERF_REG_ARM64_VG),
-     SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
      }
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10          0x0080
 #define PVR_POWER11          0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(r0, PERF_REG_POWERPC_R0),
-     SMPL_REG(r1, PERF_REG_POWERPC_R1),
-     SMPL_REG(r2, PERF_REG_POWERPC_R2),
-     SMPL_REG(r3, PERF_REG_POWERPC_R3),
-     SMPL_REG(r4, PERF_REG_POWERPC_R4),
-     SMPL_REG(r5, PERF_REG_POWERPC_R5),
-     SMPL_REG(r6, PERF_REG_POWERPC_R6),
-     SMPL_REG(r7, PERF_REG_POWERPC_R7),
-     SMPL_REG(r8, PERF_REG_POWERPC_R8),
-     SMPL_REG(r9, PERF_REG_POWERPC_R9),
-     SMPL_REG(r10, PERF_REG_POWERPC_R10),
-     SMPL_REG(r11, PERF_REG_POWERPC_R11),
-     SMPL_REG(r12, PERF_REG_POWERPC_R12),
-     SMPL_REG(r13, PERF_REG_POWERPC_R13),
-     SMPL_REG(r14, PERF_REG_POWERPC_R14),
-     SMPL_REG(r15, PERF_REG_POWERPC_R15),
-     SMPL_REG(r16, PERF_REG_POWERPC_R16),
-     SMPL_REG(r17, PERF_REG_POWERPC_R17),
-     SMPL_REG(r18, PERF_REG_POWERPC_R18),
-     SMPL_REG(r19, PERF_REG_POWERPC_R19),
-     SMPL_REG(r20, PERF_REG_POWERPC_R20),
-     SMPL_REG(r21, PERF_REG_POWERPC_R21),
-     SMPL_REG(r22, PERF_REG_POWERPC_R22),
-     SMPL_REG(r23, PERF_REG_POWERPC_R23),
-     SMPL_REG(r24, PERF_REG_POWERPC_R24),
-     SMPL_REG(r25, PERF_REG_POWERPC_R25),
-     SMPL_REG(r26, PERF_REG_POWERPC_R26),
-     SMPL_REG(r27, PERF_REG_POWERPC_R27),
-     SMPL_REG(r28, PERF_REG_POWERPC_R28),
-     SMPL_REG(r29, PERF_REG_POWERPC_R29),
-     SMPL_REG(r30, PERF_REG_POWERPC_R30),
-     SMPL_REG(r31, PERF_REG_POWERPC_R31),
-     SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-     SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-     SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-     SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-     SMPL_REG(link, PERF_REG_POWERPC_LINK),
-     SMPL_REG(xer, PERF_REG_POWERPC_XER),
-     SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-     SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-     SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-     SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-     SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-     SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-     SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-     SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-     SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-     SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-     SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-     SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-     SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-     SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-     SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-     SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-     SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-     SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-     SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-     SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-     SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-     SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(AX, PERF_REG_X86_AX),
-     SMPL_REG(BX, PERF_REG_X86_BX),
-     SMPL_REG(CX, PERF_REG_X86_CX),
-     SMPL_REG(DX, PERF_REG_X86_DX),
-     SMPL_REG(SI, PERF_REG_X86_SI),
-     SMPL_REG(DI, PERF_REG_X86_DI),
-     SMPL_REG(BP, PERF_REG_X86_BP),
-     SMPL_REG(SP, PERF_REG_X86_SP),
-     SMPL_REG(IP, PERF_REG_X86_IP),
-     SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-     SMPL_REG(CS, PERF_REG_X86_CS),
-     SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-     SMPL_REG(R8, PERF_REG_X86_R8),
-     SMPL_REG(R9, PERF_REG_X86_R9),
-     SMPL_REG(R10, PERF_REG_X86_R10),
-     SMPL_REG(R11, PERF_REG_X86_R11),
-     SMPL_REG(R12, PERF_REG_X86_R12),
-     SMPL_REG(R13, PERF_REG_X86_R13),
-     SMPL_REG(R14, PERF_REG_X86_R14),
-     SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-     SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-     SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-     SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-     SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-     SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-     SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-     SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-     SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-     SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-     SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-     SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-     SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-     SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-     SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-     SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-     SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-     SMPL_REG_END
-};
-
 struct sdt_name_reg {
      const char *sdt_name;
      const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
      return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
      struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
      size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
      struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +1 -0
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
Quoted 10 lines

tools/perf/util/parse-regs-options.c line 18

Quoted 6 lines
tools/perf/util/parse-regs-options.c +6 -0
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+     const char *last_name = NULL;
+
+     fprintf(fp, "available registers: ");
+     for (int reg = 0; reg < 64; reg++) {

Could we not use the magic number 64? It's not safe, the supported GP register number could exceed 64 in the future, then the exceeded register won't be iterated.

How will such a register appear in sample_regs_user/sample_regs_intr? There's a requirement that perf registers be indices into the 64-bit mask.

Quoted 41 lines

How about this?

tools/perf/util/parse-regs-options.c +1 -1
index c0d0ef9fd495..5177016a7ad4 100644
@@ -15,7 +15,7 @@ static void list_perf_regs(FILE *fp, uint64_t mask)
        const char *last_name = NULL;
 
        fprintf(fp, "available registers: ");
-       for (int reg = 0; reg < 64; reg++) {
+       for (unsigned long reg = 0; reg < sizeof(mask) * BITS_PER_BYTE;
 reg++) {
                const char *name;
 
                if (((1ULL << reg) & mask) == 0)
Quoted 8 lines
tools/perf/util/parse-regs-options.c +8 -0
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (name && (!last_name || strcmp(last_name, name)))
+                     fprintf(fp, "%s%s", reg > 0 ? " " : "", name);

tools/perf/util/parse-regs-options.c line 36

Quoted 10 lines
tools/perf/util/parse-regs-options.c +10 -0
+             last_name = name;
+     }
+     fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+     uint64_t reg_mask = 0;
+
+     for (int reg = 0; reg < 64; reg++) {

ditto.

Quoted 14 lines

tools/perf/util/parse-regs-options.c line 47

Quoted 11 lines
tools/perf/util/parse-regs-options.c +11 -0
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (!name)
+                     continue;
+
+             if (!strcasecmp(to_match, name))
+                     reg_mask |= 1ULL << reg;

How much register names are expected to match here? It seems the function expects to match only one register name. If so, we can "break" here.

Registers like XMM0 double up so that they have two 64-bit slots in the sample data. You need to match the name multiple times so that all bits can be set in the mask.

Thanks,
Ian

Quoted 149 lines

Thanks.

Quoted 146 lines
tools/perf/util/parse-regs-options.c +37 -43
+     }
+     return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
      uint64_t *mode = (uint64_t *)opt->value;
-     const struct sample_reg *r = NULL;
      char *s, *os = NULL, *p;
      int ret = -1;
      uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
      if (*mode)
              return -1;
 
-     if (intr)
-             mask = arch__intr_reg_mask();
-     else
-             mask = arch__user_reg_mask();
-
      /* str may be NULL in case no arg is passed to -I */
-     if (str) {
-             /* because str is read-only */
-             s = os = strdup(str);
-             if (!s)
-                     return -1;
-
-             for (;;) {
-                     p = strchr(s, ',');
-                     if (p)
-                             *p = '\0';
-
-                     if (!strcmp(s, "?")) {
-                             fprintf(stderr, "available registers: ");
-                             for (r = arch__sample_reg_masks(); r->name; r++) {
-                                     if (r->mask & mask)
-                                             fprintf(stderr, "%s ", r->name);
-                             }
-                             fputc('\n', stderr);
-                             /* just printing available regs */
-                             goto error;
-                     }
-                     for (r = arch__sample_reg_masks(); r->name; r++) {
-                             if ((r->mask & mask) && !strcasecmp(s, r->name))
-                                     break;
-                     }
-                     if (!r || !r->name) {
-                             ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-                                         s, intr ? "-I" : "--user-regs=");
-                             goto error;
-                     }
-
-                     *mode |= r->mask;
-
-                     if (!p)
-                             break;
-
-                     s = p + 1;
+     if (!str)
+             return -1;
+
+     mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+     /* because str is read-only */
+     s = os = strdup(str);
+     if (!s)
+             return -1;
+
+     for (;;) {
+             uint64_t reg_mask;
+
+             p = strchr(s, ',');
+             if (p)
+                     *p = '\0';
+
+             if (!strcmp(s, "?")) {
+                     list_perf_regs(stderr, mask);
+                     goto error;
+             }
+
+             reg_mask = name_to_perf_reg_mask(s, mask);
+             if (reg_mask == 0) {
+                     ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+                             s, intr ? "-I" : "--user-regs=");
+                     goto error;
              }
+             *mode |= reg_mask;
+
+             if (!p)
+                     break;
+
+             s = p + 1;
      }
      ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
      return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
      const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-     const char *name;
-     uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
      SDT_ARG_VALID = 0,
      SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
#3 lore raw
Quoted 470 lines · Ian Rogers
Quoted 460 lines · Dapeng
Quoted 458 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-     const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-     opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+     opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE    (1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(x0, PERF_REG_ARM64_X0),
-     SMPL_REG(x1, PERF_REG_ARM64_X1),
-     SMPL_REG(x2, PERF_REG_ARM64_X2),
-     SMPL_REG(x3, PERF_REG_ARM64_X3),
-     SMPL_REG(x4, PERF_REG_ARM64_X4),
-     SMPL_REG(x5, PERF_REG_ARM64_X5),
-     SMPL_REG(x6, PERF_REG_ARM64_X6),
-     SMPL_REG(x7, PERF_REG_ARM64_X7),
-     SMPL_REG(x8, PERF_REG_ARM64_X8),
-     SMPL_REG(x9, PERF_REG_ARM64_X9),
-     SMPL_REG(x10, PERF_REG_ARM64_X10),
-     SMPL_REG(x11, PERF_REG_ARM64_X11),
-     SMPL_REG(x12, PERF_REG_ARM64_X12),
-     SMPL_REG(x13, PERF_REG_ARM64_X13),
-     SMPL_REG(x14, PERF_REG_ARM64_X14),
-     SMPL_REG(x15, PERF_REG_ARM64_X15),
-     SMPL_REG(x16, PERF_REG_ARM64_X16),
-     SMPL_REG(x17, PERF_REG_ARM64_X17),
-     SMPL_REG(x18, PERF_REG_ARM64_X18),
-     SMPL_REG(x19, PERF_REG_ARM64_X19),
-     SMPL_REG(x20, PERF_REG_ARM64_X20),
-     SMPL_REG(x21, PERF_REG_ARM64_X21),
-     SMPL_REG(x22, PERF_REG_ARM64_X22),
-     SMPL_REG(x23, PERF_REG_ARM64_X23),
-     SMPL_REG(x24, PERF_REG_ARM64_X24),
-     SMPL_REG(x25, PERF_REG_ARM64_X25),
-     SMPL_REG(x26, PERF_REG_ARM64_X26),
-     SMPL_REG(x27, PERF_REG_ARM64_X27),
-     SMPL_REG(x28, PERF_REG_ARM64_X28),
-     SMPL_REG(x29, PERF_REG_ARM64_X29),
-     SMPL_REG(lr, PERF_REG_ARM64_LR),
-     SMPL_REG(sp, PERF_REG_ARM64_SP),
-     SMPL_REG(pc, PERF_REG_ARM64_PC),
-     SMPL_REG(vg, PERF_REG_ARM64_VG),
-     SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
      }
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10          0x0080
 #define PVR_POWER11          0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(r0, PERF_REG_POWERPC_R0),
-     SMPL_REG(r1, PERF_REG_POWERPC_R1),
-     SMPL_REG(r2, PERF_REG_POWERPC_R2),
-     SMPL_REG(r3, PERF_REG_POWERPC_R3),
-     SMPL_REG(r4, PERF_REG_POWERPC_R4),
-     SMPL_REG(r5, PERF_REG_POWERPC_R5),
-     SMPL_REG(r6, PERF_REG_POWERPC_R6),
-     SMPL_REG(r7, PERF_REG_POWERPC_R7),
-     SMPL_REG(r8, PERF_REG_POWERPC_R8),
-     SMPL_REG(r9, PERF_REG_POWERPC_R9),
-     SMPL_REG(r10, PERF_REG_POWERPC_R10),
-     SMPL_REG(r11, PERF_REG_POWERPC_R11),
-     SMPL_REG(r12, PERF_REG_POWERPC_R12),
-     SMPL_REG(r13, PERF_REG_POWERPC_R13),
-     SMPL_REG(r14, PERF_REG_POWERPC_R14),
-     SMPL_REG(r15, PERF_REG_POWERPC_R15),
-     SMPL_REG(r16, PERF_REG_POWERPC_R16),
-     SMPL_REG(r17, PERF_REG_POWERPC_R17),
-     SMPL_REG(r18, PERF_REG_POWERPC_R18),
-     SMPL_REG(r19, PERF_REG_POWERPC_R19),
-     SMPL_REG(r20, PERF_REG_POWERPC_R20),
-     SMPL_REG(r21, PERF_REG_POWERPC_R21),
-     SMPL_REG(r22, PERF_REG_POWERPC_R22),
-     SMPL_REG(r23, PERF_REG_POWERPC_R23),
-     SMPL_REG(r24, PERF_REG_POWERPC_R24),
-     SMPL_REG(r25, PERF_REG_POWERPC_R25),
-     SMPL_REG(r26, PERF_REG_POWERPC_R26),
-     SMPL_REG(r27, PERF_REG_POWERPC_R27),
-     SMPL_REG(r28, PERF_REG_POWERPC_R28),
-     SMPL_REG(r29, PERF_REG_POWERPC_R29),
-     SMPL_REG(r30, PERF_REG_POWERPC_R30),
-     SMPL_REG(r31, PERF_REG_POWERPC_R31),
-     SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-     SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-     SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-     SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-     SMPL_REG(link, PERF_REG_POWERPC_LINK),
-     SMPL_REG(xer, PERF_REG_POWERPC_XER),
-     SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-     SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-     SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-     SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-     SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-     SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-     SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-     SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-     SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-     SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-     SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-     SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-     SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-     SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-     SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-     SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-     SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-     SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-     SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-     SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-     SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-     SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(AX, PERF_REG_X86_AX),
-     SMPL_REG(BX, PERF_REG_X86_BX),
-     SMPL_REG(CX, PERF_REG_X86_CX),
-     SMPL_REG(DX, PERF_REG_X86_DX),
-     SMPL_REG(SI, PERF_REG_X86_SI),
-     SMPL_REG(DI, PERF_REG_X86_DI),
-     SMPL_REG(BP, PERF_REG_X86_BP),
-     SMPL_REG(SP, PERF_REG_X86_SP),
-     SMPL_REG(IP, PERF_REG_X86_IP),
-     SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-     SMPL_REG(CS, PERF_REG_X86_CS),
-     SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-     SMPL_REG(R8, PERF_REG_X86_R8),
-     SMPL_REG(R9, PERF_REG_X86_R9),
-     SMPL_REG(R10, PERF_REG_X86_R10),
-     SMPL_REG(R11, PERF_REG_X86_R11),
-     SMPL_REG(R12, PERF_REG_X86_R12),
-     SMPL_REG(R13, PERF_REG_X86_R13),
-     SMPL_REG(R14, PERF_REG_X86_R14),
-     SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-     SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-     SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-     SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-     SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-     SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-     SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-     SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-     SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-     SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-     SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-     SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-     SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-     SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-     SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-     SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-     SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-     SMPL_REG_END
-};
-
 struct sdt_name_reg {
      const char *sdt_name;
      const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
      return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
      struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
      size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
      struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +1 -0
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
Quoted 9 lines

tools/perf/util/parse-regs-options.c line 18

Quoted 6 lines
tools/perf/util/parse-regs-options.c +6 -0
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+     const char *last_name = NULL;
+
+     fprintf(fp, "available registers: ");
+     for (int reg = 0; reg < 64; reg++) {

Could we not use the magic number 64? It's not safe, the supported GP register number could exceed 64 in the future, then the exceeded register won't be iterated.

Quoted 3 lines

How will such a register appear in sample_regs_user/sample_regs_intr? There's a requirement that perf registers be indices into the 64-bit mask.

In theory, if the GPRs exceeds 64, then sample_regs_user/sample_regs_intr could become an u64 array. So if the user missed to modify the magic number 64, then there would be something wrong. IMO, the below change looks safer and I personally doesn't like the magic number. :)

Quoted 54 lines
Quoted 41 lines

How about this?

tools/perf/util/parse-regs-options.c +1 -1
index c0d0ef9fd495..5177016a7ad4 100644
@@ -15,7 +15,7 @@ static void list_perf_regs(FILE *fp, uint64_t mask)
        const char *last_name = NULL;
 
        fprintf(fp, "available registers: ");
-       for (int reg = 0; reg < 64; reg++) {
+       for (unsigned long reg = 0; reg < sizeof(mask) * BITS_PER_BYTE;
 reg++) {
                const char *name;
 
                if (((1ULL << reg) & mask) == 0)
Quoted 8 lines
tools/perf/util/parse-regs-options.c +8 -0
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (name && (!last_name || strcmp(last_name, name)))
+                     fprintf(fp, "%s%s", reg > 0 ? " " : "", name);

tools/perf/util/parse-regs-options.c line 36

Quoted 10 lines
tools/perf/util/parse-regs-options.c +10 -0
+             last_name = name;
+     }
+     fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+     uint64_t reg_mask = 0;
+
+     for (int reg = 0; reg < 64; reg++) {

ditto.

Quoted 13 lines

tools/perf/util/parse-regs-options.c line 47

Quoted 11 lines
tools/perf/util/parse-regs-options.c +11 -0
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (!name)
+                     continue;
+
+             if (!strcasecmp(to_match, name))
+                     reg_mask |= 1ULL << reg;

How much register names are expected to match here? It seems the function expects to match only one register name. If so, we can "break" here.

Quoted 3 lines

Registers like XMM0 double up so that they have two 64-bit slots in the sample data. You need to match the name multiple times so that all bits can be set in the mask.

Oh, yes. I missed XMM registers. Thanks.

Quoted 153 lines

Thanks,
Ian

Quoted 149 lines

Thanks.

Quoted 146 lines
tools/perf/util/parse-regs-options.c +37 -43
+     }
+     return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
      uint64_t *mode = (uint64_t *)opt->value;
-     const struct sample_reg *r = NULL;
      char *s, *os = NULL, *p;
      int ret = -1;
      uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
      if (*mode)
              return -1;
 
-     if (intr)
-             mask = arch__intr_reg_mask();
-     else
-             mask = arch__user_reg_mask();
-
      /* str may be NULL in case no arg is passed to -I */
-     if (str) {
-             /* because str is read-only */
-             s = os = strdup(str);
-             if (!s)
-                     return -1;
-
-             for (;;) {
-                     p = strchr(s, ',');
-                     if (p)
-                             *p = '\0';
-
-                     if (!strcmp(s, "?")) {
-                             fprintf(stderr, "available registers: ");
-                             for (r = arch__sample_reg_masks(); r->name; r++) {
-                                     if (r->mask & mask)
-                                             fprintf(stderr, "%s ", r->name);
-                             }
-                             fputc('\n', stderr);
-                             /* just printing available regs */
-                             goto error;
-                     }
-                     for (r = arch__sample_reg_masks(); r->name; r++) {
-                             if ((r->mask & mask) && !strcasecmp(s, r->name))
-                                     break;
-                     }
-                     if (!r || !r->name) {
-                             ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-                                         s, intr ? "-I" : "--user-regs=");
-                             goto error;
-                     }
-
-                     *mode |= r->mask;
-
-                     if (!p)
-                             break;
-
-                     s = p + 1;
+     if (!str)
+             return -1;
+
+     mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+     /* because str is read-only */
+     s = os = strdup(str);
+     if (!s)
+             return -1;
+
+     for (;;) {
+             uint64_t reg_mask;
+
+             p = strchr(s, ',');
+             if (p)
+                     *p = '\0';
+
+             if (!strcmp(s, "?")) {
+                     list_perf_regs(stderr, mask);
+                     goto error;
+             }
+
+             reg_mask = name_to_perf_reg_mask(s, mask);
+             if (reg_mask == 0) {
+                     ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+                             s, intr ? "-I" : "--user-regs=");
+                     goto error;
              }
+             *mode |= reg_mask;
+
+             if (!p)
+                     break;
+
+             s = p + 1;
      }
      ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
      return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
      const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-     const char *name;
-     uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
      SDT_ARG_VALID = 0,
      SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
author reviewer
#4 lore raw
Quoted 473 lines · Dapeng
Quoted 470 lines · Ian Rogers
Quoted 469 lines · Dapeng
Quoted 454 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-     const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-     opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+     opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE    (1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(x0, PERF_REG_ARM64_X0),
-     SMPL_REG(x1, PERF_REG_ARM64_X1),
-     SMPL_REG(x2, PERF_REG_ARM64_X2),
-     SMPL_REG(x3, PERF_REG_ARM64_X3),
-     SMPL_REG(x4, PERF_REG_ARM64_X4),
-     SMPL_REG(x5, PERF_REG_ARM64_X5),
-     SMPL_REG(x6, PERF_REG_ARM64_X6),
-     SMPL_REG(x7, PERF_REG_ARM64_X7),
-     SMPL_REG(x8, PERF_REG_ARM64_X8),
-     SMPL_REG(x9, PERF_REG_ARM64_X9),
-     SMPL_REG(x10, PERF_REG_ARM64_X10),
-     SMPL_REG(x11, PERF_REG_ARM64_X11),
-     SMPL_REG(x12, PERF_REG_ARM64_X12),
-     SMPL_REG(x13, PERF_REG_ARM64_X13),
-     SMPL_REG(x14, PERF_REG_ARM64_X14),
-     SMPL_REG(x15, PERF_REG_ARM64_X15),
-     SMPL_REG(x16, PERF_REG_ARM64_X16),
-     SMPL_REG(x17, PERF_REG_ARM64_X17),
-     SMPL_REG(x18, PERF_REG_ARM64_X18),
-     SMPL_REG(x19, PERF_REG_ARM64_X19),
-     SMPL_REG(x20, PERF_REG_ARM64_X20),
-     SMPL_REG(x21, PERF_REG_ARM64_X21),
-     SMPL_REG(x22, PERF_REG_ARM64_X22),
-     SMPL_REG(x23, PERF_REG_ARM64_X23),
-     SMPL_REG(x24, PERF_REG_ARM64_X24),
-     SMPL_REG(x25, PERF_REG_ARM64_X25),
-     SMPL_REG(x26, PERF_REG_ARM64_X26),
-     SMPL_REG(x27, PERF_REG_ARM64_X27),
-     SMPL_REG(x28, PERF_REG_ARM64_X28),
-     SMPL_REG(x29, PERF_REG_ARM64_X29),
-     SMPL_REG(lr, PERF_REG_ARM64_LR),
-     SMPL_REG(sp, PERF_REG_ARM64_SP),
-     SMPL_REG(pc, PERF_REG_ARM64_PC),
-     SMPL_REG(vg, PERF_REG_ARM64_VG),
-     SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
      }
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10          0x0080
 #define PVR_POWER11          0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(r0, PERF_REG_POWERPC_R0),
-     SMPL_REG(r1, PERF_REG_POWERPC_R1),
-     SMPL_REG(r2, PERF_REG_POWERPC_R2),
-     SMPL_REG(r3, PERF_REG_POWERPC_R3),
-     SMPL_REG(r4, PERF_REG_POWERPC_R4),
-     SMPL_REG(r5, PERF_REG_POWERPC_R5),
-     SMPL_REG(r6, PERF_REG_POWERPC_R6),
-     SMPL_REG(r7, PERF_REG_POWERPC_R7),
-     SMPL_REG(r8, PERF_REG_POWERPC_R8),
-     SMPL_REG(r9, PERF_REG_POWERPC_R9),
-     SMPL_REG(r10, PERF_REG_POWERPC_R10),
-     SMPL_REG(r11, PERF_REG_POWERPC_R11),
-     SMPL_REG(r12, PERF_REG_POWERPC_R12),
-     SMPL_REG(r13, PERF_REG_POWERPC_R13),
-     SMPL_REG(r14, PERF_REG_POWERPC_R14),
-     SMPL_REG(r15, PERF_REG_POWERPC_R15),
-     SMPL_REG(r16, PERF_REG_POWERPC_R16),
-     SMPL_REG(r17, PERF_REG_POWERPC_R17),
-     SMPL_REG(r18, PERF_REG_POWERPC_R18),
-     SMPL_REG(r19, PERF_REG_POWERPC_R19),
-     SMPL_REG(r20, PERF_REG_POWERPC_R20),
-     SMPL_REG(r21, PERF_REG_POWERPC_R21),
-     SMPL_REG(r22, PERF_REG_POWERPC_R22),
-     SMPL_REG(r23, PERF_REG_POWERPC_R23),
-     SMPL_REG(r24, PERF_REG_POWERPC_R24),
-     SMPL_REG(r25, PERF_REG_POWERPC_R25),
-     SMPL_REG(r26, PERF_REG_POWERPC_R26),
-     SMPL_REG(r27, PERF_REG_POWERPC_R27),
-     SMPL_REG(r28, PERF_REG_POWERPC_R28),
-     SMPL_REG(r29, PERF_REG_POWERPC_R29),
-     SMPL_REG(r30, PERF_REG_POWERPC_R30),
-     SMPL_REG(r31, PERF_REG_POWERPC_R31),
-     SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-     SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-     SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-     SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-     SMPL_REG(link, PERF_REG_POWERPC_LINK),
-     SMPL_REG(xer, PERF_REG_POWERPC_XER),
-     SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-     SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-     SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-     SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-     SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-     SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-     SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-     SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-     SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-     SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-     SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-     SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-     SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-     SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-     SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-     SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-     SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-     SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-     SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-     SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-     SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-     SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(AX, PERF_REG_X86_AX),
-     SMPL_REG(BX, PERF_REG_X86_BX),
-     SMPL_REG(CX, PERF_REG_X86_CX),
-     SMPL_REG(DX, PERF_REG_X86_DX),
-     SMPL_REG(SI, PERF_REG_X86_SI),
-     SMPL_REG(DI, PERF_REG_X86_DI),
-     SMPL_REG(BP, PERF_REG_X86_BP),
-     SMPL_REG(SP, PERF_REG_X86_SP),
-     SMPL_REG(IP, PERF_REG_X86_IP),
-     SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-     SMPL_REG(CS, PERF_REG_X86_CS),
-     SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-     SMPL_REG(R8, PERF_REG_X86_R8),
-     SMPL_REG(R9, PERF_REG_X86_R9),
-     SMPL_REG(R10, PERF_REG_X86_R10),
-     SMPL_REG(R11, PERF_REG_X86_R11),
-     SMPL_REG(R12, PERF_REG_X86_R12),
-     SMPL_REG(R13, PERF_REG_X86_R13),
-     SMPL_REG(R14, PERF_REG_X86_R14),
-     SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-     SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-     SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-     SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-     SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-     SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-     SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-     SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-     SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-     SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-     SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-     SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-     SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-     SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-     SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-     SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-     SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-     SMPL_REG_END
-};
-
 struct sdt_name_reg {
      const char *sdt_name;
      const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
      return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
      struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
      size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
      struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +1 -0
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>

tools/perf/util/parse-regs-options.c line 18

Quoted 10 lines
tools/perf/util/parse-regs-options.c +6 -0
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+     const char *last_name = NULL;
+
+     fprintf(fp, "available registers: ");
+     for (int reg = 0; reg < 64; reg++) {

Could we not use the magic number 64? It's not safe, the supported GP register number could exceed 64 in the future, then the exceeded register won't be iterated.

Quoted 8 lines
Quoted 3 lines

How will such a register appear in sample_regs_user/sample_regs_intr? There's a requirement that perf registers be indices into the 64-bit mask.

In theory, if the GPRs exceeds 64, then sample_regs_user/sample_regs_intr could become an u64 array. So if the user missed to modify the magic number 64, then there would be something wrong. IMO, the below change looks safer and I personally doesn't like the magic number. :)

Hmm.. I'm not sold. 64 vs "sizeof(mask) * BITS_PER_BYTE" means you need to know the type of mask then work out the math. BITS_PER_U64 could be intention revealing but it is also just obviously a synonym for 64, and the coding style guide says to keep things short:
https://docs.kernel.org/process/coding-style.html#naming

Thanks,
Ian

Quoted 214 lines
Quoted 54 lines
Quoted 41 lines

How about this?

tools/perf/util/parse-regs-options.c +1 -1
index c0d0ef9fd495..5177016a7ad4 100644
@@ -15,7 +15,7 @@ static void list_perf_regs(FILE *fp, uint64_t mask)
        const char *last_name = NULL;
 
        fprintf(fp, "available registers: ");
-       for (int reg = 0; reg < 64; reg++) {
+       for (unsigned long reg = 0; reg < sizeof(mask) * BITS_PER_BYTE;
 reg++) {
                const char *name;
 
                if (((1ULL << reg) & mask) == 0)
Quoted 8 lines
tools/perf/util/parse-regs-options.c +8 -0
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (name && (!last_name || strcmp(last_name, name)))
+                     fprintf(fp, "%s%s", reg > 0 ? " " : "", name);

tools/perf/util/parse-regs-options.c line 36

Quoted 10 lines
tools/perf/util/parse-regs-options.c +10 -0
+             last_name = name;
+     }
+     fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+     uint64_t reg_mask = 0;
+
+     for (int reg = 0; reg < 64; reg++) {

ditto.

Quoted 13 lines

tools/perf/util/parse-regs-options.c line 47

Quoted 11 lines
tools/perf/util/parse-regs-options.c +11 -0
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (!name)
+                     continue;
+
+             if (!strcasecmp(to_match, name))
+                     reg_mask |= 1ULL << reg;

How much register names are expected to match here? It seems the function expects to match only one register name. If so, we can "break" here.

Quoted 3 lines

Registers like XMM0 double up so that they have two 64-bit slots in the sample data. You need to match the name multiple times so that all bits can be set in the mask.

Oh, yes. I missed XMM registers. Thanks.

Quoted 153 lines

Thanks,
Ian

Quoted 149 lines

Thanks.

Quoted 146 lines
tools/perf/util/parse-regs-options.c +37 -43
+     }
+     return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
      uint64_t *mode = (uint64_t *)opt->value;
-     const struct sample_reg *r = NULL;
      char *s, *os = NULL, *p;
      int ret = -1;
      uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
      if (*mode)
              return -1;
 
-     if (intr)
-             mask = arch__intr_reg_mask();
-     else
-             mask = arch__user_reg_mask();
-
      /* str may be NULL in case no arg is passed to -I */
-     if (str) {
-             /* because str is read-only */
-             s = os = strdup(str);
-             if (!s)
-                     return -1;
-
-             for (;;) {
-                     p = strchr(s, ',');
-                     if (p)
-                             *p = '\0';
-
-                     if (!strcmp(s, "?")) {
-                             fprintf(stderr, "available registers: ");
-                             for (r = arch__sample_reg_masks(); r->name; r++) {
-                                     if (r->mask & mask)
-                                             fprintf(stderr, "%s ", r->name);
-                             }
-                             fputc('\n', stderr);
-                             /* just printing available regs */
-                             goto error;
-                     }
-                     for (r = arch__sample_reg_masks(); r->name; r++) {
-                             if ((r->mask & mask) && !strcasecmp(s, r->name))
-                                     break;
-                     }
-                     if (!r || !r->name) {
-                             ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-                                         s, intr ? "-I" : "--user-regs=");
-                             goto error;
-                     }
-
-                     *mode |= r->mask;
-
-                     if (!p)
-                             break;
-
-                     s = p + 1;
+     if (!str)
+             return -1;
+
+     mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+     /* because str is read-only */
+     s = os = strdup(str);
+     if (!s)
+             return -1;
+
+     for (;;) {
+             uint64_t reg_mask;
+
+             p = strchr(s, ',');
+             if (p)
+                     *p = '\0';
+
+             if (!strcmp(s, "?")) {
+                     list_perf_regs(stderr, mask);
+                     goto error;
+             }
+
+             reg_mask = name_to_perf_reg_mask(s, mask);
+             if (reg_mask == 0) {
+                     ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+                             s, intr ? "-I" : "--user-regs=");
+                     goto error;
              }
+             *mode |= reg_mask;
+
+             if (!p)
+                     break;
+
+             s = p + 1;
      }
      ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
      return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
      const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-     const char *name;
-     uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
      SDT_ARG_VALID = 0,
      SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
Tested-by on v2 1/1
#5 lore raw
In reply to Ian Rogers
Quoted 632 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-	const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-	opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+	opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE	(1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(x0, PERF_REG_ARM64_X0),
-	SMPL_REG(x1, PERF_REG_ARM64_X1),
-	SMPL_REG(x2, PERF_REG_ARM64_X2),
-	SMPL_REG(x3, PERF_REG_ARM64_X3),
-	SMPL_REG(x4, PERF_REG_ARM64_X4),
-	SMPL_REG(x5, PERF_REG_ARM64_X5),
-	SMPL_REG(x6, PERF_REG_ARM64_X6),
-	SMPL_REG(x7, PERF_REG_ARM64_X7),
-	SMPL_REG(x8, PERF_REG_ARM64_X8),
-	SMPL_REG(x9, PERF_REG_ARM64_X9),
-	SMPL_REG(x10, PERF_REG_ARM64_X10),
-	SMPL_REG(x11, PERF_REG_ARM64_X11),
-	SMPL_REG(x12, PERF_REG_ARM64_X12),
-	SMPL_REG(x13, PERF_REG_ARM64_X13),
-	SMPL_REG(x14, PERF_REG_ARM64_X14),
-	SMPL_REG(x15, PERF_REG_ARM64_X15),
-	SMPL_REG(x16, PERF_REG_ARM64_X16),
-	SMPL_REG(x17, PERF_REG_ARM64_X17),
-	SMPL_REG(x18, PERF_REG_ARM64_X18),
-	SMPL_REG(x19, PERF_REG_ARM64_X19),
-	SMPL_REG(x20, PERF_REG_ARM64_X20),
-	SMPL_REG(x21, PERF_REG_ARM64_X21),
-	SMPL_REG(x22, PERF_REG_ARM64_X22),
-	SMPL_REG(x23, PERF_REG_ARM64_X23),
-	SMPL_REG(x24, PERF_REG_ARM64_X24),
-	SMPL_REG(x25, PERF_REG_ARM64_X25),
-	SMPL_REG(x26, PERF_REG_ARM64_X26),
-	SMPL_REG(x27, PERF_REG_ARM64_X27),
-	SMPL_REG(x28, PERF_REG_ARM64_X28),
-	SMPL_REG(x29, PERF_REG_ARM64_X29),
-	SMPL_REG(lr, PERF_REG_ARM64_LR),
-	SMPL_REG(sp, PERF_REG_ARM64_SP),
-	SMPL_REG(pc, PERF_REG_ARM64_PC),
-	SMPL_REG(vg, PERF_REG_ARM64_VG),
-	SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
 	}
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10		0x0080
 #define PVR_POWER11		0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(r0, PERF_REG_POWERPC_R0),
-	SMPL_REG(r1, PERF_REG_POWERPC_R1),
-	SMPL_REG(r2, PERF_REG_POWERPC_R2),
-	SMPL_REG(r3, PERF_REG_POWERPC_R3),
-	SMPL_REG(r4, PERF_REG_POWERPC_R4),
-	SMPL_REG(r5, PERF_REG_POWERPC_R5),
-	SMPL_REG(r6, PERF_REG_POWERPC_R6),
-	SMPL_REG(r7, PERF_REG_POWERPC_R7),
-	SMPL_REG(r8, PERF_REG_POWERPC_R8),
-	SMPL_REG(r9, PERF_REG_POWERPC_R9),
-	SMPL_REG(r10, PERF_REG_POWERPC_R10),
-	SMPL_REG(r11, PERF_REG_POWERPC_R11),
-	SMPL_REG(r12, PERF_REG_POWERPC_R12),
-	SMPL_REG(r13, PERF_REG_POWERPC_R13),
-	SMPL_REG(r14, PERF_REG_POWERPC_R14),
-	SMPL_REG(r15, PERF_REG_POWERPC_R15),
-	SMPL_REG(r16, PERF_REG_POWERPC_R16),
-	SMPL_REG(r17, PERF_REG_POWERPC_R17),
-	SMPL_REG(r18, PERF_REG_POWERPC_R18),
-	SMPL_REG(r19, PERF_REG_POWERPC_R19),
-	SMPL_REG(r20, PERF_REG_POWERPC_R20),
-	SMPL_REG(r21, PERF_REG_POWERPC_R21),
-	SMPL_REG(r22, PERF_REG_POWERPC_R22),
-	SMPL_REG(r23, PERF_REG_POWERPC_R23),
-	SMPL_REG(r24, PERF_REG_POWERPC_R24),
-	SMPL_REG(r25, PERF_REG_POWERPC_R25),
-	SMPL_REG(r26, PERF_REG_POWERPC_R26),
-	SMPL_REG(r27, PERF_REG_POWERPC_R27),
-	SMPL_REG(r28, PERF_REG_POWERPC_R28),
-	SMPL_REG(r29, PERF_REG_POWERPC_R29),
-	SMPL_REG(r30, PERF_REG_POWERPC_R30),
-	SMPL_REG(r31, PERF_REG_POWERPC_R31),
-	SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-	SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-	SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-	SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-	SMPL_REG(link, PERF_REG_POWERPC_LINK),
-	SMPL_REG(xer, PERF_REG_POWERPC_XER),
-	SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-	SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-	SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-	SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-	SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-	SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-	SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-	SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-	SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-	SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-	SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-	SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-	SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-	SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-	SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-	SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-	SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-	SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-	SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-	SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-	SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-	SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(AX, PERF_REG_X86_AX),
-	SMPL_REG(BX, PERF_REG_X86_BX),
-	SMPL_REG(CX, PERF_REG_X86_CX),
-	SMPL_REG(DX, PERF_REG_X86_DX),
-	SMPL_REG(SI, PERF_REG_X86_SI),
-	SMPL_REG(DI, PERF_REG_X86_DI),
-	SMPL_REG(BP, PERF_REG_X86_BP),
-	SMPL_REG(SP, PERF_REG_X86_SP),
-	SMPL_REG(IP, PERF_REG_X86_IP),
-	SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-	SMPL_REG(CS, PERF_REG_X86_CS),
-	SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-	SMPL_REG(R8, PERF_REG_X86_R8),
-	SMPL_REG(R9, PERF_REG_X86_R9),
-	SMPL_REG(R10, PERF_REG_X86_R10),
-	SMPL_REG(R11, PERF_REG_X86_R11),
-	SMPL_REG(R12, PERF_REG_X86_R12),
-	SMPL_REG(R13, PERF_REG_X86_R13),
-	SMPL_REG(R14, PERF_REG_X86_R14),
-	SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-	SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-	SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-	SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-	SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-	SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-	SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-	SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-	SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-	SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-	SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-	SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-	SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-	SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-	SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-	SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-	SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-	SMPL_REG_END
-};
-
 struct sdt_name_reg {
 	const char *sdt_name;
 	const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
 	return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
 	struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
 	size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
 	struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +73 -43
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+	const char *last_name = NULL;
+
+	fprintf(fp, "available registers: ");
+	for (int reg = 0; reg < 64; reg++) {
+		const char *name;
+
+		if (((1ULL << reg) & mask) == 0)
+			continue;
+
+		name = perf_reg_name(reg, EM_HOST);
+		if (name && (!last_name || strcmp(last_name, name)))
+			fprintf(fp, "%s%s", reg > 0 ? " " : "", name);
+		last_name = name;
+	}
+	fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+	uint64_t reg_mask = 0;
+
+	for (int reg = 0; reg < 64; reg++) {
+		const char *name;
+
+		if (((1ULL << reg) & mask) == 0)
+			continue;
+
+		name = perf_reg_name(reg, EM_HOST);
+		if (!name)
+			continue;
+
+		if (!strcasecmp(to_match, name))
+			reg_mask |= 1ULL << reg;
+	}
+	return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
 	uint64_t *mode = (uint64_t *)opt->value;
-	const struct sample_reg *r = NULL;
 	char *s, *os = NULL, *p;
 	int ret = -1;
 	uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 	if (*mode)
 		return -1;
 
-	if (intr)
-		mask = arch__intr_reg_mask();
-	else
-		mask = arch__user_reg_mask();
-
 	/* str may be NULL in case no arg is passed to -I */
-	if (str) {
-		/* because str is read-only */
-		s = os = strdup(str);
-		if (!s)
-			return -1;
-
-		for (;;) {
-			p = strchr(s, ',');
-			if (p)
-				*p = '\0';
-
-			if (!strcmp(s, "?")) {
-				fprintf(stderr, "available registers: ");
-				for (r = arch__sample_reg_masks(); r->name; r++) {
-					if (r->mask & mask)
-						fprintf(stderr, "%s ", r->name);
-				}
-				fputc('\n', stderr);
-				/* just printing available regs */
-				goto error;
-			}
-			for (r = arch__sample_reg_masks(); r->name; r++) {
-				if ((r->mask & mask) && !strcasecmp(s, r->name))
-					break;
-			}
-			if (!r || !r->name) {
-				ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-					    s, intr ? "-I" : "--user-regs=");
-				goto error;
-			}
-
-			*mode |= r->mask;
-
-			if (!p)
-				break;
-
-			s = p + 1;
+	if (!str)
+		return -1;
+
+	mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+	/* because str is read-only */
+	s = os = strdup(str);
+	if (!s)
+		return -1;
+
+	for (;;) {
+		uint64_t reg_mask;
+
+		p = strchr(s, ',');
+		if (p)
+			*p = '\0';
+
+		if (!strcmp(s, "?")) {
+			list_perf_regs(stderr, mask);
+			goto error;
+		}
+
+		reg_mask = name_to_perf_reg_mask(s, mask);
+		if (reg_mask == 0) {
+			ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+				s, intr ? "-I" : "--user-regs=");
+			goto error;
 		}
+		*mode |= reg_mask;
+
+		if (!p)
+			break;
+
+		s = p + 1;
 	}
 	ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
 	return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
 	const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -11
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-	const char *name;
-	uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
 	SDT_ARG_VALID = 0,
 	SDT_ARG_SKIP,

tools/perf/util/perf_regs.h enum { · line 20

Quoted 7 lines
tools/perf/util/perf_regs.h +0 -1
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);

I have tested this patch set for s390.

Signature · 9 lines
Thomas Richter, Dept 3303, IBM s390 Linux Development, Boeblingen, Germany
--
IBM Deutschland Research & Development GmbH

Vorsitzender des Aufsichtsrats: Wolfgang Wendt

Geschäftsführung: David Faller

Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
#6 lore raw
In reply to Ian Rogers
Quoted 544 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-	const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-	opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+	opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE	(1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(x0, PERF_REG_ARM64_X0),
-	SMPL_REG(x1, PERF_REG_ARM64_X1),
-	SMPL_REG(x2, PERF_REG_ARM64_X2),
-	SMPL_REG(x3, PERF_REG_ARM64_X3),
-	SMPL_REG(x4, PERF_REG_ARM64_X4),
-	SMPL_REG(x5, PERF_REG_ARM64_X5),
-	SMPL_REG(x6, PERF_REG_ARM64_X6),
-	SMPL_REG(x7, PERF_REG_ARM64_X7),
-	SMPL_REG(x8, PERF_REG_ARM64_X8),
-	SMPL_REG(x9, PERF_REG_ARM64_X9),
-	SMPL_REG(x10, PERF_REG_ARM64_X10),
-	SMPL_REG(x11, PERF_REG_ARM64_X11),
-	SMPL_REG(x12, PERF_REG_ARM64_X12),
-	SMPL_REG(x13, PERF_REG_ARM64_X13),
-	SMPL_REG(x14, PERF_REG_ARM64_X14),
-	SMPL_REG(x15, PERF_REG_ARM64_X15),
-	SMPL_REG(x16, PERF_REG_ARM64_X16),
-	SMPL_REG(x17, PERF_REG_ARM64_X17),
-	SMPL_REG(x18, PERF_REG_ARM64_X18),
-	SMPL_REG(x19, PERF_REG_ARM64_X19),
-	SMPL_REG(x20, PERF_REG_ARM64_X20),
-	SMPL_REG(x21, PERF_REG_ARM64_X21),
-	SMPL_REG(x22, PERF_REG_ARM64_X22),
-	SMPL_REG(x23, PERF_REG_ARM64_X23),
-	SMPL_REG(x24, PERF_REG_ARM64_X24),
-	SMPL_REG(x25, PERF_REG_ARM64_X25),
-	SMPL_REG(x26, PERF_REG_ARM64_X26),
-	SMPL_REG(x27, PERF_REG_ARM64_X27),
-	SMPL_REG(x28, PERF_REG_ARM64_X28),
-	SMPL_REG(x29, PERF_REG_ARM64_X29),
-	SMPL_REG(lr, PERF_REG_ARM64_LR),
-	SMPL_REG(sp, PERF_REG_ARM64_SP),
-	SMPL_REG(pc, PERF_REG_ARM64_PC),
-	SMPL_REG(vg, PERF_REG_ARM64_VG),
-	SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
 	}
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10		0x0080
 #define PVR_POWER11		0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(r0, PERF_REG_POWERPC_R0),
-	SMPL_REG(r1, PERF_REG_POWERPC_R1),
-	SMPL_REG(r2, PERF_REG_POWERPC_R2),
-	SMPL_REG(r3, PERF_REG_POWERPC_R3),
-	SMPL_REG(r4, PERF_REG_POWERPC_R4),
-	SMPL_REG(r5, PERF_REG_POWERPC_R5),
-	SMPL_REG(r6, PERF_REG_POWERPC_R6),
-	SMPL_REG(r7, PERF_REG_POWERPC_R7),
-	SMPL_REG(r8, PERF_REG_POWERPC_R8),
-	SMPL_REG(r9, PERF_REG_POWERPC_R9),
-	SMPL_REG(r10, PERF_REG_POWERPC_R10),
-	SMPL_REG(r11, PERF_REG_POWERPC_R11),
-	SMPL_REG(r12, PERF_REG_POWERPC_R12),
-	SMPL_REG(r13, PERF_REG_POWERPC_R13),
-	SMPL_REG(r14, PERF_REG_POWERPC_R14),
-	SMPL_REG(r15, PERF_REG_POWERPC_R15),
-	SMPL_REG(r16, PERF_REG_POWERPC_R16),
-	SMPL_REG(r17, PERF_REG_POWERPC_R17),
-	SMPL_REG(r18, PERF_REG_POWERPC_R18),
-	SMPL_REG(r19, PERF_REG_POWERPC_R19),
-	SMPL_REG(r20, PERF_REG_POWERPC_R20),
-	SMPL_REG(r21, PERF_REG_POWERPC_R21),
-	SMPL_REG(r22, PERF_REG_POWERPC_R22),
-	SMPL_REG(r23, PERF_REG_POWERPC_R23),
-	SMPL_REG(r24, PERF_REG_POWERPC_R24),
-	SMPL_REG(r25, PERF_REG_POWERPC_R25),
-	SMPL_REG(r26, PERF_REG_POWERPC_R26),
-	SMPL_REG(r27, PERF_REG_POWERPC_R27),
-	SMPL_REG(r28, PERF_REG_POWERPC_R28),
-	SMPL_REG(r29, PERF_REG_POWERPC_R29),
-	SMPL_REG(r30, PERF_REG_POWERPC_R30),
-	SMPL_REG(r31, PERF_REG_POWERPC_R31),
-	SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-	SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-	SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-	SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-	SMPL_REG(link, PERF_REG_POWERPC_LINK),
-	SMPL_REG(xer, PERF_REG_POWERPC_XER),
-	SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-	SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-	SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-	SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-	SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-	SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-	SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-	SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-	SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-	SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-	SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-	SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-	SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-	SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-	SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-	SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-	SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-	SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-	SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-	SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-	SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-	SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
 	return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
 	return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG(AX, PERF_REG_X86_AX),
-	SMPL_REG(BX, PERF_REG_X86_BX),
-	SMPL_REG(CX, PERF_REG_X86_CX),
-	SMPL_REG(DX, PERF_REG_X86_DX),
-	SMPL_REG(SI, PERF_REG_X86_SI),
-	SMPL_REG(DI, PERF_REG_X86_DI),
-	SMPL_REG(BP, PERF_REG_X86_BP),
-	SMPL_REG(SP, PERF_REG_X86_SP),
-	SMPL_REG(IP, PERF_REG_X86_IP),
-	SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-	SMPL_REG(CS, PERF_REG_X86_CS),
-	SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-	SMPL_REG(R8, PERF_REG_X86_R8),
-	SMPL_REG(R9, PERF_REG_X86_R9),
-	SMPL_REG(R10, PERF_REG_X86_R10),
-	SMPL_REG(R11, PERF_REG_X86_R11),
-	SMPL_REG(R12, PERF_REG_X86_R12),
-	SMPL_REG(R13, PERF_REG_X86_R13),
-	SMPL_REG(R14, PERF_REG_X86_R14),
-	SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-	SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-	SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-	SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-	SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-	SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-	SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-	SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-	SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-	SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-	SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-	SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-	SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-	SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-	SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-	SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-	SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-	SMPL_REG_END
-};
-
 struct sdt_name_reg {
 	const char *sdt_name;
 	const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
 	return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
 	struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
 	size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
 	struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +40 -35
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+	const char *last_name = NULL;
+
+	fprintf(fp, "available registers: ");
+	for (int reg = 0; reg < 64; reg++) {
+		const char *name;
+
+		if (((1ULL << reg) & mask) == 0)
+			continue;
+
+		name = perf_reg_name(reg, EM_HOST);
+		if (name && (!last_name || strcmp(last_name, name)))
+			fprintf(fp, "%s%s", reg > 0 ? " " : "", name);
+		last_name = name;
+	}
+	fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+	uint64_t reg_mask = 0;
+
+	for (int reg = 0; reg < 64; reg++) {
+		const char *name;
+
+		if (((1ULL << reg) & mask) == 0)
+			continue;
+
+		name = perf_reg_name(reg, EM_HOST);
+		if (!name)
+			continue;
+
+		if (!strcasecmp(to_match, name))
+			reg_mask |= 1ULL << reg;
+	}
+	return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
 	uint64_t *mode = (uint64_t *)opt->value;
-	const struct sample_reg *r = NULL;
 	char *s, *os = NULL, *p;
 	int ret = -1;
 	uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 	if (*mode)
 		return -1;
 
-	if (intr)
-		mask = arch__intr_reg_mask();
-	else
-		mask = arch__user_reg_mask();
-
 	/* str may be NULL in case no arg is passed to -I */
-	if (str) {
-		/* because str is read-only */
-		s = os = strdup(str);
-		if (!s)
-			return -1;
-
-		for (;;) {
-			p = strchr(s, ',');
-			if (p)
-				*p = '\0';
-
-			if (!strcmp(s, "?")) {
-				fprintf(stderr, "available registers: ");
-				for (r = arch__sample_reg_masks(); r->name; r++) {
-					if (r->mask & mask)
-						fprintf(stderr, "%s ", r->name);
-				}
-				fputc('\n', stderr);
-				/* just printing available regs */
-				goto error;
-			}
-			for (r = arch__sample_reg_masks(); r->name; r++) {
-				if ((r->mask & mask) && !strcasecmp(s, r->name))
-					break;
-			}
-			if (!r || !r->name) {
-				ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-					    s, intr ? "-I" : "--user-regs=");
-				goto error;

tools/perf/util/parse-regs-options.c __parse_regs(const struct option *opt, const char *str, int unset, bool intr) · line 71

Quoted 10 lines
tools/perf/util/parse-regs-options.c +2 -8
-			}
-
-			*mode |= r->mask;
-
-			if (!p)
-				break;
-
-			s = p + 1;
+	if (!str)
+		return -1;

Just found the above change would break the option "-I" or "--user-regs" which intends to sample all supported general purpose registers, e.g.,

$ sudo ./perf record -e cycles:p -I -- true

 Usage: perf record [<options>] [<command>]
    or: perf record [<options>] -- <command> [<options>]

    -I, --intr-regs[=<any register>]
                          sample selected machine registers on interrupt, use '-I?' to list register names

We may need the below changes to fix this issue.

```

diff --git a/tools/perf/util/parse-regs-options.c
b/tools/perf/util/parse-regs-options.c
index c0d0ef9fd495..37932fcd8c04 100644
--- a/tools/perf/util/parse-regs-options.c
+++ b/tools/perf/util/parse-regs-options.c
@@ -66,12 +66,14 @@ __parse_regs(const struct option *opt, const char *str,
int unset, bool intr)
        if (*mode)
                return -1;

-       /* str may be NULL in case no arg is passed to -I */
-       if (!str)
-               return -1;
-
        mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();

+       /* str may be NULL in case no arg is passed to -I */
+       if (!str) {
+               *mode = mask;
+               return 0;
+       }
+
        /* because str is read-only */
        s = os = strdup(str);
        if (!s)
@@ -104,9 +106,6 @@ __parse_regs(const struct option *opt, const char *str,
int unset, bool intr)
        }
        ret = 0;

-       /* default to all possible regs */
-       if (*mode == 0)
-               *mode = mask;
 error:
        free(os);
        return ret;

```

With above change, the "-I" option can be sampled as expect.

$ sudo ./perf record -e cycles:p -I -- true
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (4 samples) ]
Quoted 85 lines
tools/perf/util/parse-regs-options.c +31 -0
+
+	mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+	/* because str is read-only */
+	s = os = strdup(str);
+	if (!s)
+		return -1;
+
+	for (;;) {
+		uint64_t reg_mask;
+
+		p = strchr(s, ',');
+		if (p)
+			*p = '\0';
+
+		if (!strcmp(s, "?")) {
+			list_perf_regs(stderr, mask);
+			goto error;
+		}
+
+		reg_mask = name_to_perf_reg_mask(s, mask);
+		if (reg_mask == 0) {
+			ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+				s, intr ? "-I" : "--user-regs=");
+			goto error;
 		}
+		*mode |= reg_mask;
+
+		if (!p)
+			break;
+
+		s = p + 1;
 	}
 	ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
 	return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-	SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-	return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
 	const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-	const char *name;
-	uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
 	SDT_ARG_VALID = 0,
 	SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
author reviewer
#7 lore raw
Quoted 611 lines · Dapeng
Quoted 544 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-     const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-     opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+     opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE    (1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(x0, PERF_REG_ARM64_X0),
-     SMPL_REG(x1, PERF_REG_ARM64_X1),
-     SMPL_REG(x2, PERF_REG_ARM64_X2),
-     SMPL_REG(x3, PERF_REG_ARM64_X3),
-     SMPL_REG(x4, PERF_REG_ARM64_X4),
-     SMPL_REG(x5, PERF_REG_ARM64_X5),
-     SMPL_REG(x6, PERF_REG_ARM64_X6),
-     SMPL_REG(x7, PERF_REG_ARM64_X7),
-     SMPL_REG(x8, PERF_REG_ARM64_X8),
-     SMPL_REG(x9, PERF_REG_ARM64_X9),
-     SMPL_REG(x10, PERF_REG_ARM64_X10),
-     SMPL_REG(x11, PERF_REG_ARM64_X11),
-     SMPL_REG(x12, PERF_REG_ARM64_X12),
-     SMPL_REG(x13, PERF_REG_ARM64_X13),
-     SMPL_REG(x14, PERF_REG_ARM64_X14),
-     SMPL_REG(x15, PERF_REG_ARM64_X15),
-     SMPL_REG(x16, PERF_REG_ARM64_X16),
-     SMPL_REG(x17, PERF_REG_ARM64_X17),
-     SMPL_REG(x18, PERF_REG_ARM64_X18),
-     SMPL_REG(x19, PERF_REG_ARM64_X19),
-     SMPL_REG(x20, PERF_REG_ARM64_X20),
-     SMPL_REG(x21, PERF_REG_ARM64_X21),
-     SMPL_REG(x22, PERF_REG_ARM64_X22),
-     SMPL_REG(x23, PERF_REG_ARM64_X23),
-     SMPL_REG(x24, PERF_REG_ARM64_X24),
-     SMPL_REG(x25, PERF_REG_ARM64_X25),
-     SMPL_REG(x26, PERF_REG_ARM64_X26),
-     SMPL_REG(x27, PERF_REG_ARM64_X27),
-     SMPL_REG(x28, PERF_REG_ARM64_X28),
-     SMPL_REG(x29, PERF_REG_ARM64_X29),
-     SMPL_REG(lr, PERF_REG_ARM64_LR),
-     SMPL_REG(sp, PERF_REG_ARM64_SP),
-     SMPL_REG(pc, PERF_REG_ARM64_PC),
-     SMPL_REG(vg, PERF_REG_ARM64_VG),
-     SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
      }
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10          0x0080
 #define PVR_POWER11          0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(r0, PERF_REG_POWERPC_R0),
-     SMPL_REG(r1, PERF_REG_POWERPC_R1),
-     SMPL_REG(r2, PERF_REG_POWERPC_R2),
-     SMPL_REG(r3, PERF_REG_POWERPC_R3),
-     SMPL_REG(r4, PERF_REG_POWERPC_R4),
-     SMPL_REG(r5, PERF_REG_POWERPC_R5),
-     SMPL_REG(r6, PERF_REG_POWERPC_R6),
-     SMPL_REG(r7, PERF_REG_POWERPC_R7),
-     SMPL_REG(r8, PERF_REG_POWERPC_R8),
-     SMPL_REG(r9, PERF_REG_POWERPC_R9),
-     SMPL_REG(r10, PERF_REG_POWERPC_R10),
-     SMPL_REG(r11, PERF_REG_POWERPC_R11),
-     SMPL_REG(r12, PERF_REG_POWERPC_R12),
-     SMPL_REG(r13, PERF_REG_POWERPC_R13),
-     SMPL_REG(r14, PERF_REG_POWERPC_R14),
-     SMPL_REG(r15, PERF_REG_POWERPC_R15),
-     SMPL_REG(r16, PERF_REG_POWERPC_R16),
-     SMPL_REG(r17, PERF_REG_POWERPC_R17),
-     SMPL_REG(r18, PERF_REG_POWERPC_R18),
-     SMPL_REG(r19, PERF_REG_POWERPC_R19),
-     SMPL_REG(r20, PERF_REG_POWERPC_R20),
-     SMPL_REG(r21, PERF_REG_POWERPC_R21),
-     SMPL_REG(r22, PERF_REG_POWERPC_R22),
-     SMPL_REG(r23, PERF_REG_POWERPC_R23),
-     SMPL_REG(r24, PERF_REG_POWERPC_R24),
-     SMPL_REG(r25, PERF_REG_POWERPC_R25),
-     SMPL_REG(r26, PERF_REG_POWERPC_R26),
-     SMPL_REG(r27, PERF_REG_POWERPC_R27),
-     SMPL_REG(r28, PERF_REG_POWERPC_R28),
-     SMPL_REG(r29, PERF_REG_POWERPC_R29),
-     SMPL_REG(r30, PERF_REG_POWERPC_R30),
-     SMPL_REG(r31, PERF_REG_POWERPC_R31),
-     SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-     SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-     SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-     SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-     SMPL_REG(link, PERF_REG_POWERPC_LINK),
-     SMPL_REG(xer, PERF_REG_POWERPC_XER),
-     SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-     SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-     SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-     SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-     SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-     SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-     SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-     SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-     SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-     SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-     SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-     SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-     SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-     SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-     SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-     SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-     SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-     SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-     SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-     SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-     SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-     SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(AX, PERF_REG_X86_AX),
-     SMPL_REG(BX, PERF_REG_X86_BX),
-     SMPL_REG(CX, PERF_REG_X86_CX),
-     SMPL_REG(DX, PERF_REG_X86_DX),
-     SMPL_REG(SI, PERF_REG_X86_SI),
-     SMPL_REG(DI, PERF_REG_X86_DI),
-     SMPL_REG(BP, PERF_REG_X86_BP),
-     SMPL_REG(SP, PERF_REG_X86_SP),
-     SMPL_REG(IP, PERF_REG_X86_IP),
-     SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-     SMPL_REG(CS, PERF_REG_X86_CS),
-     SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-     SMPL_REG(R8, PERF_REG_X86_R8),
-     SMPL_REG(R9, PERF_REG_X86_R9),
-     SMPL_REG(R10, PERF_REG_X86_R10),
-     SMPL_REG(R11, PERF_REG_X86_R11),
-     SMPL_REG(R12, PERF_REG_X86_R12),
-     SMPL_REG(R13, PERF_REG_X86_R13),
-     SMPL_REG(R14, PERF_REG_X86_R14),
-     SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-     SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-     SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-     SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-     SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-     SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-     SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-     SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-     SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-     SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-     SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-     SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-     SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-     SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-     SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-     SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-     SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-     SMPL_REG_END
-};
-
 struct sdt_name_reg {
      const char *sdt_name;
      const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
      return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
      struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
      size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
      struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +40 -35
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+     const char *last_name = NULL;
+
+     fprintf(fp, "available registers: ");
+     for (int reg = 0; reg < 64; reg++) {
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (name && (!last_name || strcmp(last_name, name)))
+                     fprintf(fp, "%s%s", reg > 0 ? " " : "", name);
+             last_name = name;
+     }
+     fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+     uint64_t reg_mask = 0;
+
+     for (int reg = 0; reg < 64; reg++) {
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (!name)
+                     continue;
+
+             if (!strcasecmp(to_match, name))
+                     reg_mask |= 1ULL << reg;
+     }
+     return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
      uint64_t *mode = (uint64_t *)opt->value;
-     const struct sample_reg *r = NULL;
      char *s, *os = NULL, *p;
      int ret = -1;
      uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
      if (*mode)
              return -1;
 
-     if (intr)
-             mask = arch__intr_reg_mask();
-     else
-             mask = arch__user_reg_mask();
-
      /* str may be NULL in case no arg is passed to -I */
-     if (str) {
-             /* because str is read-only */
-             s = os = strdup(str);
-             if (!s)
-                     return -1;
-
-             for (;;) {
-                     p = strchr(s, ',');
-                     if (p)
-                             *p = '\0';
-
-                     if (!strcmp(s, "?")) {
-                             fprintf(stderr, "available registers: ");
-                             for (r = arch__sample_reg_masks(); r->name; r++) {
-                                     if (r->mask & mask)
-                                             fprintf(stderr, "%s ", r->name);
-                             }
-                             fputc('\n', stderr);
-                             /* just printing available regs */
-                             goto error;
-                     }
-                     for (r = arch__sample_reg_masks(); r->name; r++) {
-                             if ((r->mask & mask) && !strcasecmp(s, r->name))
-                                     break;
-                     }
-                     if (!r || !r->name) {
-                             ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-                                         s, intr ? "-I" : "--user-regs=");
-                             goto error;

tools/perf/util/parse-regs-options.c __parse_regs(const struct option *opt, const char *str, int unset, bool intr) · line 71

Quoted 10 lines
tools/perf/util/parse-regs-options.c +2 -8
-                     }
-
-                     *mode |= r->mask;
-
-                     if (!p)
-                             break;
-
-                     s = p + 1;
+     if (!str)
+             return -1;

Just found the above change would break the option "-I" or "--user-regs" which intends to sample all supported general purpose registers, e.g.,

$ sudo ./perf record -e cycles:p -I -- true

Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]

-I, --intr-regs[=<any register>]
sample selected machine registers on interrupt,
use '-I?' to list register names

We may need the below changes to fix this issue.

```

tools/perf/util/parse-regs-options.c +6 -4
index c0d0ef9fd495..37932fcd8c04 100644
@@ -66,12 +66,14 @@ __parse_regs(const struct option *opt, const char *str,
 int unset, bool intr)
        if (*mode)
                return -1;
 
-       /* str may be NULL in case no arg is passed to -I */
-       if (!str)
-               return -1;
-
        mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
 
+       /* str may be NULL in case no arg is passed to -I */
+       if (!str) {
+               *mode = mask;
+               return 0;
+       }
+
        /* because str is read-only */
        s = os = strdup(str);
        if (!s)
tools/perf/util/parse-regs-options.c +0 -3
@@ -104,9 +106,6 @@ __parse_regs(const struct option *opt, const char *str,
 int unset, bool intr)
        }
        ret = 0;
 
-       /* default to all possible regs */
-       if (*mode == 0)
-               *mode = mask;
 error:
        free(os);
        return ret;
Quoted 7 lines

```

With above change, the "-I" option can be sampled as expect.

$ sudo ./perf record -e cycles:p -I -- true
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (4 samples) ]

Yep, agreed. Could you create the patch?

Thanks,
Ian

Quoted 85 lines
Quoted 85 lines
tools/perf/util/parse-regs-options.c +31 -0
+
+     mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+     /* because str is read-only */
+     s = os = strdup(str);
+     if (!s)
+             return -1;
+
+     for (;;) {
+             uint64_t reg_mask;
+
+             p = strchr(s, ',');
+             if (p)
+                     *p = '\0';
+
+             if (!strcmp(s, "?")) {
+                     list_perf_regs(stderr, mask);
+                     goto error;
+             }
+
+             reg_mask = name_to_perf_reg_mask(s, mask);
+             if (reg_mask == 0) {
+                     ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+                             s, intr ? "-I" : "--user-regs=");
+                     goto error;
              }
+             *mode |= reg_mask;
+
+             if (!p)
+                     break;
+
+             s = p + 1;
      }
      ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
      return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
      const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-     const char *name;
-     uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
      SDT_ARG_VALID = 0,
      SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
#8 lore raw
Quoted 610 lines · Ian Rogers
Quoted 609 lines · Dapeng
Quoted 544 lines · Ian Rogers

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

---
v2: Correct the setting of reg mask for registers that need >1 bit set
like XMMs.

The idea for this patch came up in conversation with Dapeng Mi to avoid additional functions and tables for x86 APX support:
https://lore.kernel.org/lkml/CAP-5=fWOsvAXuu8qx0tk9q8j8BoXPMJB3Gxto+iu6wf06i4cVA@mail.gmail.com/

The patch is on top of the dwfl clean up patches that switch perf register functions to using e_machine as an argument rather than the arch string, this means that EM_HOST can be used with perf_reg_name:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/arch/arm/util/perf_regs.c          |   9 --
 tools/perf/arch/arm64/util/machine.c          |  14 +--
 tools/perf/arch/arm64/util/perf_regs.c        |  45 +------
 tools/perf/arch/csky/util/perf_regs.c         |   9 --
 tools/perf/arch/loongarch/util/perf_regs.c    |   9 --
 tools/perf/arch/mips/util/perf_regs.c         |   9 --
 tools/perf/arch/powerpc/util/perf_regs.c      |  68 ----------
 tools/perf/arch/riscv/util/perf_regs.c        |   9 --
 tools/perf/arch/s390/util/perf_regs.c         |   9 --
 tools/perf/arch/x86/util/perf_regs.c          |  47 -------
 .../util/arm64-frame-pointer-unwind-support.c |   3 +-
 tools/perf/util/parse-regs-options.c          | 116 +++++++++++-------
 tools/perf/util/perf_regs.c                   |   9 --
 tools/perf/util/perf_regs.h                   |  12 --
 14 files changed, 81 insertions(+), 287 deletions(-)
tools/perf/arch/arm/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/arm64/util/machine.c +4 -10
index aab1cc2bc283..80fb13c958d9 100644
@@ -1,18 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-#include "debug.h"
-#include "symbol.h"
-#include "callchain.h"
+#include "callchain.h" // prototype of arch__add_leaf_frame_record_opts
 #include "perf_regs.h"
 #include "record.h"
-#include "util/perf_regs.h"
+
+#define SMPL_REG_MASK(b) (1ULL << (b))
 
 void arch__add_leaf_frame_record_opts(struct record_opts *opts)
 {
-     const struct sample_reg *sample_reg_masks = arch__sample_reg_masks();
-
-     opts->sample_user_regs |= sample_reg_masks[PERF_REG_ARM64_LR].mask;
+     opts->sample_user_regs |= SMPL_REG_MASK(PERF_REG_ARM64_LR);
 }
tools/perf/arch/arm64/util/perf_regs.c +2 -43
index 09308665e28a..9bb768e1bea1 100644
@@ -12,48 +12,12 @@
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 #ifndef HWCAP_SVE
 #define HWCAP_SVE    (1 << 22)
 #endif
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(x0, PERF_REG_ARM64_X0),
-     SMPL_REG(x1, PERF_REG_ARM64_X1),
-     SMPL_REG(x2, PERF_REG_ARM64_X2),
-     SMPL_REG(x3, PERF_REG_ARM64_X3),
-     SMPL_REG(x4, PERF_REG_ARM64_X4),
-     SMPL_REG(x5, PERF_REG_ARM64_X5),
-     SMPL_REG(x6, PERF_REG_ARM64_X6),
-     SMPL_REG(x7, PERF_REG_ARM64_X7),
-     SMPL_REG(x8, PERF_REG_ARM64_X8),
-     SMPL_REG(x9, PERF_REG_ARM64_X9),
-     SMPL_REG(x10, PERF_REG_ARM64_X10),
-     SMPL_REG(x11, PERF_REG_ARM64_X11),
-     SMPL_REG(x12, PERF_REG_ARM64_X12),
-     SMPL_REG(x13, PERF_REG_ARM64_X13),
-     SMPL_REG(x14, PERF_REG_ARM64_X14),
-     SMPL_REG(x15, PERF_REG_ARM64_X15),
-     SMPL_REG(x16, PERF_REG_ARM64_X16),
-     SMPL_REG(x17, PERF_REG_ARM64_X17),
-     SMPL_REG(x18, PERF_REG_ARM64_X18),
-     SMPL_REG(x19, PERF_REG_ARM64_X19),
-     SMPL_REG(x20, PERF_REG_ARM64_X20),
-     SMPL_REG(x21, PERF_REG_ARM64_X21),
-     SMPL_REG(x22, PERF_REG_ARM64_X22),
-     SMPL_REG(x23, PERF_REG_ARM64_X23),
-     SMPL_REG(x24, PERF_REG_ARM64_X24),
-     SMPL_REG(x25, PERF_REG_ARM64_X25),
-     SMPL_REG(x26, PERF_REG_ARM64_X26),
-     SMPL_REG(x27, PERF_REG_ARM64_X27),
-     SMPL_REG(x28, PERF_REG_ARM64_X28),
-     SMPL_REG(x29, PERF_REG_ARM64_X29),
-     SMPL_REG(lr, PERF_REG_ARM64_LR),
-     SMPL_REG(sp, PERF_REG_ARM64_SP),
-     SMPL_REG(pc, PERF_REG_ARM64_PC),
-     SMPL_REG(vg, PERF_REG_ARM64_VG),
-     SMPL_REG_END
-};
-
 /* %xNUM */
 #define SDT_OP_REGEX1  "^(x[1-2]?[0-9]|3[0-1])$"
 
@@ -175,8 +139,3 @@ uint64_t arch__user_reg_mask(void)
      }
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/csky/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/loongarch/util/perf_regs.c +0 -9
index f94a0210c7b7..03a5bc0cf64c 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/mips/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/powerpc/util/perf_regs.c +0 -68
index bd36cfd420a2..779073f7e992 100644
@@ -18,69 +18,6 @@
 #define PVR_POWER10          0x0080
 #define PVR_POWER11          0x0082
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(r0, PERF_REG_POWERPC_R0),
-     SMPL_REG(r1, PERF_REG_POWERPC_R1),
-     SMPL_REG(r2, PERF_REG_POWERPC_R2),
-     SMPL_REG(r3, PERF_REG_POWERPC_R3),
-     SMPL_REG(r4, PERF_REG_POWERPC_R4),
-     SMPL_REG(r5, PERF_REG_POWERPC_R5),
-     SMPL_REG(r6, PERF_REG_POWERPC_R6),
-     SMPL_REG(r7, PERF_REG_POWERPC_R7),
-     SMPL_REG(r8, PERF_REG_POWERPC_R8),
-     SMPL_REG(r9, PERF_REG_POWERPC_R9),
-     SMPL_REG(r10, PERF_REG_POWERPC_R10),
-     SMPL_REG(r11, PERF_REG_POWERPC_R11),
-     SMPL_REG(r12, PERF_REG_POWERPC_R12),
-     SMPL_REG(r13, PERF_REG_POWERPC_R13),
-     SMPL_REG(r14, PERF_REG_POWERPC_R14),
-     SMPL_REG(r15, PERF_REG_POWERPC_R15),
-     SMPL_REG(r16, PERF_REG_POWERPC_R16),
-     SMPL_REG(r17, PERF_REG_POWERPC_R17),
-     SMPL_REG(r18, PERF_REG_POWERPC_R18),
-     SMPL_REG(r19, PERF_REG_POWERPC_R19),
-     SMPL_REG(r20, PERF_REG_POWERPC_R20),
-     SMPL_REG(r21, PERF_REG_POWERPC_R21),
-     SMPL_REG(r22, PERF_REG_POWERPC_R22),
-     SMPL_REG(r23, PERF_REG_POWERPC_R23),
-     SMPL_REG(r24, PERF_REG_POWERPC_R24),
-     SMPL_REG(r25, PERF_REG_POWERPC_R25),
-     SMPL_REG(r26, PERF_REG_POWERPC_R26),
-     SMPL_REG(r27, PERF_REG_POWERPC_R27),
-     SMPL_REG(r28, PERF_REG_POWERPC_R28),
-     SMPL_REG(r29, PERF_REG_POWERPC_R29),
-     SMPL_REG(r30, PERF_REG_POWERPC_R30),
-     SMPL_REG(r31, PERF_REG_POWERPC_R31),
-     SMPL_REG(nip, PERF_REG_POWERPC_NIP),
-     SMPL_REG(msr, PERF_REG_POWERPC_MSR),
-     SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
-     SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
-     SMPL_REG(link, PERF_REG_POWERPC_LINK),
-     SMPL_REG(xer, PERF_REG_POWERPC_XER),
-     SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
-     SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
-     SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
-     SMPL_REG(dar, PERF_REG_POWERPC_DAR),
-     SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
-     SMPL_REG(sier, PERF_REG_POWERPC_SIER),
-     SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA),
-     SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0),
-     SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1),
-     SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2),
-     SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3),
-     SMPL_REG(sier2, PERF_REG_POWERPC_SIER2),
-     SMPL_REG(sier3, PERF_REG_POWERPC_SIER3),
-     SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1),
-     SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2),
-     SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3),
-     SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4),
-     SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5),
-     SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6),
-     SMPL_REG(sdar, PERF_REG_POWERPC_SDAR),
-     SMPL_REG(siar, PERF_REG_POWERPC_SIAR),
-     SMPL_REG_END
-};
-
 /* REG or %rREG */
 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
 
@@ -233,8 +170,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/riscv/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/s390/util/perf_regs.c +0 -9
index 6b1665f41180..2cf7a54106e0 100644
@@ -2,10 +2,6 @@
 #include "perf_regs.h"
 #include "../../util/perf_regs.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
 uint64_t arch__intr_reg_mask(void)
 {
      return PERF_REGS_MASK;
@@ -15,8 +11,3 @@ uint64_t arch__user_reg_mask(void)
 {
      return PERF_REGS_MASK;
 }
-
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
tools/perf/arch/x86/util/perf_regs.c +0 -47
index 12fd93f04802..a7ca4154fdf9 100644
@@ -13,48 +13,6 @@
 #include "../../../util/pmu.h"
 #include "../../../util/pmus.h"
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG(AX, PERF_REG_X86_AX),
-     SMPL_REG(BX, PERF_REG_X86_BX),
-     SMPL_REG(CX, PERF_REG_X86_CX),
-     SMPL_REG(DX, PERF_REG_X86_DX),
-     SMPL_REG(SI, PERF_REG_X86_SI),
-     SMPL_REG(DI, PERF_REG_X86_DI),
-     SMPL_REG(BP, PERF_REG_X86_BP),
-     SMPL_REG(SP, PERF_REG_X86_SP),
-     SMPL_REG(IP, PERF_REG_X86_IP),
-     SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
-     SMPL_REG(CS, PERF_REG_X86_CS),
-     SMPL_REG(SS, PERF_REG_X86_SS),
-#ifdef HAVE_ARCH_X86_64_SUPPORT
-     SMPL_REG(R8, PERF_REG_X86_R8),
-     SMPL_REG(R9, PERF_REG_X86_R9),
-     SMPL_REG(R10, PERF_REG_X86_R10),
-     SMPL_REG(R11, PERF_REG_X86_R11),
-     SMPL_REG(R12, PERF_REG_X86_R12),
-     SMPL_REG(R13, PERF_REG_X86_R13),
-     SMPL_REG(R14, PERF_REG_X86_R14),
-     SMPL_REG(R15, PERF_REG_X86_R15),
-#endif
-     SMPL_REG2(XMM0, PERF_REG_X86_XMM0),
-     SMPL_REG2(XMM1, PERF_REG_X86_XMM1),
-     SMPL_REG2(XMM2, PERF_REG_X86_XMM2),
-     SMPL_REG2(XMM3, PERF_REG_X86_XMM3),
-     SMPL_REG2(XMM4, PERF_REG_X86_XMM4),
-     SMPL_REG2(XMM5, PERF_REG_X86_XMM5),
-     SMPL_REG2(XMM6, PERF_REG_X86_XMM6),
-     SMPL_REG2(XMM7, PERF_REG_X86_XMM7),
-     SMPL_REG2(XMM8, PERF_REG_X86_XMM8),
-     SMPL_REG2(XMM9, PERF_REG_X86_XMM9),
-     SMPL_REG2(XMM10, PERF_REG_X86_XMM10),
-     SMPL_REG2(XMM11, PERF_REG_X86_XMM11),
-     SMPL_REG2(XMM12, PERF_REG_X86_XMM12),
-     SMPL_REG2(XMM13, PERF_REG_X86_XMM13),
-     SMPL_REG2(XMM14, PERF_REG_X86_XMM14),
-     SMPL_REG2(XMM15, PERF_REG_X86_XMM15),
-     SMPL_REG_END
-};
-
 struct sdt_name_reg {
      const char *sdt_name;
      const char *uprobe_name;
@@ -276,11 +234,6 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
      return SDT_ARG_VALID;
 }
 
-const struct sample_reg *arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 uint64_t arch__intr_reg_mask(void)
 {
      struct perf_event_attr attr = {
tools/perf/util/arm64-frame-pointer-unwind-support.c +2 -1
index 958afe8b821e..858ce2b01812 100644
@@ -2,7 +2,6 @@
 #include "arm64-frame-pointer-unwind-support.h"
 #include "callchain.h"
 #include "event.h"
-#include "perf_regs.h" // SMPL_REG_MASK
 #include "unwind.h"
 #include <string.h>
 
@@ -15,6 +14,8 @@ struct entries {
      size_t length;
 };
 
+#define SMPL_REG_MASK(b) (1ULL << (b))
+
 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
 {
      struct regs_dump *regs;
tools/perf/util/parse-regs-options.c +40 -35
index cda1c620968e..c0d0ef9fd495 100644
@@ -5,15 +5,54 @@
 #include <string.h>
 #include <stdio.h>
 #include "util/debug.h"
+#include <dwarf-regs.h>
 #include <subcmd/parse-options.h>
 #include "util/perf_regs.h"
 #include "util/parse-regs-options.h"
 
+static void list_perf_regs(FILE *fp, uint64_t mask)
+{
+     const char *last_name = NULL;
+
+     fprintf(fp, "available registers: ");
+     for (int reg = 0; reg < 64; reg++) {
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (name && (!last_name || strcmp(last_name, name)))
+                     fprintf(fp, "%s%s", reg > 0 ? " " : "", name);
+             last_name = name;
+     }
+     fputc('\n', fp);
+}
+
+static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
+{
+     uint64_t reg_mask = 0;
+
+     for (int reg = 0; reg < 64; reg++) {
+             const char *name;
+
+             if (((1ULL << reg) & mask) == 0)
+                     continue;
+
+             name = perf_reg_name(reg, EM_HOST);
+             if (!name)
+                     continue;
+
+             if (!strcasecmp(to_match, name))
+                     reg_mask |= 1ULL << reg;
+     }
+     return reg_mask;
+}
+
 static int
 __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
 {
      uint64_t *mode = (uint64_t *)opt->value;
-     const struct sample_reg *r = NULL;
      char *s, *os = NULL, *p;
      int ret = -1;
      uint64_t mask;
@@ -27,50 +66,41 @@ __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
      if (*mode)
              return -1;
 
-     if (intr)
-             mask = arch__intr_reg_mask();
-     else
-             mask = arch__user_reg_mask();
-
      /* str may be NULL in case no arg is passed to -I */
-     if (str) {
-             /* because str is read-only */
-             s = os = strdup(str);
-             if (!s)
-                     return -1;
-
-             for (;;) {
-                     p = strchr(s, ',');
-                     if (p)
-                             *p = '\0';
-
-                     if (!strcmp(s, "?")) {
-                             fprintf(stderr, "available registers: ");
-                             for (r = arch__sample_reg_masks(); r->name; r++) {
-                                     if (r->mask & mask)
-                                             fprintf(stderr, "%s ", r->name);
-                             }
-                             fputc('\n', stderr);
-                             /* just printing available regs */
-                             goto error;
-                     }
-                     for (r = arch__sample_reg_masks(); r->name; r++) {
-                             if ((r->mask & mask) && !strcasecmp(s, r->name))
-                                     break;
-                     }
-                     if (!r || !r->name) {
-                             ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
-                                         s, intr ? "-I" : "--user-regs=");
-                             goto error;

tools/perf/util/parse-regs-options.c __parse_regs(const struct option *opt, const char *str, int unset, bool intr) · line 71

Quoted 10 lines
tools/perf/util/parse-regs-options.c +2 -8
-                     }
-
-                     *mode |= r->mask;
-
-                     if (!p)
-                             break;
-
-                     s = p + 1;
+     if (!str)
+             return -1;

Just found the above change would break the option "-I" or "--user-regs" which intends to sample all supported general purpose registers, e.g.,

$ sudo ./perf record -e cycles:p -I -- true

Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]

-I, --intr-regs[=<any register>]
sample selected machine registers on interrupt,
use '-I?' to list register names

We may need the below changes to fix this issue.

```

tools/perf/util/parse-regs-options.c +6 -4
index c0d0ef9fd495..37932fcd8c04 100644
@@ -66,12 +66,14 @@ __parse_regs(const struct option *opt, const char *str,
 int unset, bool intr)
        if (*mode)
                return -1;
 
-       /* str may be NULL in case no arg is passed to -I */
-       if (!str)
-               return -1;
-
        mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
 
+       /* str may be NULL in case no arg is passed to -I */
+       if (!str) {
+               *mode = mask;
+               return 0;
+       }
+
        /* because str is read-only */
        s = os = strdup(str);
        if (!s)
tools/perf/util/parse-regs-options.c +0 -3
@@ -104,9 +106,6 @@ __parse_regs(const struct option *opt, const char *str,
 int unset, bool intr)
        }
        ret = 0;
 
-       /* default to all possible regs */
-       if (*mode == 0)
-               *mode = mask;
 error:
        free(os);
        return ret;
Quoted 8 lines
Quoted 7 lines

```

With above change, the "-I" option can be sampled as expect.

$ sudo ./perf record -e cycles:p -I -- true
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (4 samples) ]

Yep, agreed. Could you create the patch?

Sure.

Quoted 89 lines

Thanks,
Ian

Quoted 85 lines
Quoted 85 lines
tools/perf/util/parse-regs-options.c +31 -0
+
+     mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
+
+     /* because str is read-only */
+     s = os = strdup(str);
+     if (!s)
+             return -1;
+
+     for (;;) {
+             uint64_t reg_mask;
+
+             p = strchr(s, ',');
+             if (p)
+                     *p = '\0';
+
+             if (!strcmp(s, "?")) {
+                     list_perf_regs(stderr, mask);
+                     goto error;
+             }
+
+             reg_mask = name_to_perf_reg_mask(s, mask);
+             if (reg_mask == 0) {
+                     ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
+                             s, intr ? "-I" : "--user-regs=");
+                     goto error;
              }
+             *mode |= reg_mask;
+
+             if (!p)
+                     break;
+
+             s = p + 1;
      }
      ret = 0;
 
tools/perf/util/perf_regs.c +0 -9
index b58d59b84fb1..ec602da1aeb6 100644
@@ -22,15 +22,6 @@ uint64_t __weak arch__user_reg_mask(void)
      return 0;
 }
 
-static const struct sample_reg sample_reg_masks[] = {
-     SMPL_REG_END
-};
-
-const struct sample_reg * __weak arch__sample_reg_masks(void)
-{
-     return sample_reg_masks;
-}
-
 const char *perf_reg_name(int id, uint16_t e_machine)
 {
      const char *reg_name = NULL;
tools/perf/util/perf_regs.h +0 -12
index 7bfc6a34c02b..2c2a8de6912d 100644
@@ -7,17 +7,6 @@
 
 struct regs_dump;
 
-struct sample_reg {
-     const char *name;
-     uint64_t mask;
-};
-
-#define SMPL_REG_MASK(b) (1ULL << (b))
-#define SMPL_REG(n, b) { .name = #n, .mask = SMPL_REG_MASK(b) }
-#define SMPL_REG2_MASK(b) (3ULL << (b))
-#define SMPL_REG2(n, b) { .name = #n, .mask = SMPL_REG2_MASK(b) }
-#define SMPL_REG_END { .name = NULL }
-
 enum {
      SDT_ARG_VALID = 0,
      SDT_ARG_SKIP,
@@ -26,7 +15,6 @@ enum {
 int arch_sdt_arg_parse_op(char *old_op, char **new_op);
 uint64_t arch__intr_reg_mask(void);
 uint64_t arch__user_reg_mask(void);
-const struct sample_reg *arch__sample_reg_masks(void);
 
 const char *perf_reg_name(int id, uint16_t e_machine);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
#9 lore raw
In reply to Ian Rogers
This patch was applied to riscv/linux.git (for-next) by Arnaldo Carvalho de Melo <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Arnaldo Carvalho de Melo <[email protected]>:

Quoted 11 lines · On Tue, 20 Jan 2026 18:17:35 -0800 you wrote:

arch__sample_reg_masks isn't supported on ARM(32), csky, loongarch, MIPS, RISC-V and s390. The table returned by the function just has the name of a register paired with the corresponding sample_regs_user mask value. For a given perf register we can compute the name with perf_reg_name and the mask is just 1 left-shifted by the perf register number. Change __parse_regs to use this method for finding registers rather than arch__sample_reg_masks, thereby adding __parse_regs support for ARM(32), csky, loongarch, MIPS, RISC-V and s390. As arch__sample_reg_masks is then unused, remove the now unneeded declarations.

Quoted 1 line

[...]

Here is the summary with links:
- [v2] perf regs: Refactor use of arch__sample_reg_masks to perf_reg_name

    https://git.kernel.org/riscv/c/3d06db9bad1a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html