Skip to contentSkip to conversation

Support register names of all architectures

PATCH In review German Gomez 4 participants 9 messages Dec 7, 2021 – Dec 14, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Support register names of all architectures

The following changeset applies some corrections to the way system registers are processed and presented when reading perf.data files using the various perf tools.

The commit message from [3/3] shows how register names aren't correctly presented when performing x-arch analysis of perf.data files (recording in one arch, then reading the file from a different arch).

- [PATCH 1/3] Fixes a potential out-of-bounds access when reading the values of the registers in the perf.data file.
- [PATCH 2/3] Fixes an issue of ARM and ARM64 registers having the same enum name.
- [PATCH 3/3] Refactors the function "perf_reg_name" declared in the "tools/perf/util/perf_regs.h" header, in order to support every arch.

Thanks,
German

--
Changes since v1

- Added "Reported-by" tags.
- Removed [PATCH 2/4] because it's not needed (suggested by Athira Rajeev).
- Removed [PATCH 3/4] which created additional header files with the register names of every arch.
- Introduced [PATCH 2/3] to deal with ARM and ARM64 registers having the same enum name across "/tools/perf/".
- Reworked the refactor of "perf_reg_name" function (now implemented in perf_regs.c, rather than in the header file) in [PATCH 3/3].

German Gomez (3):
perf tools: Prevent out-of-bounds access to registers perf tools: Rename perf_event_arm_regs for ARM64 registers perf tools: Support register names from all archs

 tools/perf/arch/arm/include/perf_regs.h       |  42 --
 tools/perf/arch/arm64/include/perf_regs.h     |  78 +-
 tools/perf/arch/csky/include/perf_regs.h      |  82 ---
 tools/perf/arch/mips/include/perf_regs.h      |  69 --
 tools/perf/arch/powerpc/include/perf_regs.h   |  66 --
 tools/perf/arch/riscv/include/perf_regs.h     |  74 --
 tools/perf/arch/s390/include/perf_regs.h      |  78 --
 tools/perf/arch/x86/include/perf_regs.h       |  82 ---
 tools/perf/builtin-script.c                   |  18 +-
 tools/perf/util/event.h                       |   5 +-
 tools/perf/util/libunwind/arm64.c             |   2 +
 tools/perf/util/perf_regs.c                   | 671 +++++++++++++++++-
 tools/perf/util/perf_regs.h                   |  10 +-
 .../scripting-engines/trace-event-python.c    |  10 +-
 tools/perf/util/session.c                     |  25 +-
 15 files changed, 709 insertions(+), 603 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/3 perf tools: Prevent out-of-bounds access to registers

The size of the cache of register values is arch-dependant (PERF_REGS_MAX). This has the potential of causing an out-of-bounds access in the function "perf_reg_value" if the local architecture contains less registers than the one the perf.data file was recorded on.

Since the maximum number of registers is bound by the bitmask "u64 cache_mask", and the size of the cache when running under x86 systems is 64 already, fix the size to 64 and add a range-check to the function "perf_reg_value" to prevent out-of-bounds access.

---
 tools/perf/util/event.h     | 5 ++++-
 tools/perf/util/perf_regs.c | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/event.h +4 -1
index 95ffed663..c59331eea 100644
@@ -44,13 +44,16 @@ struct perf_event_attr;
 /* perf sample has 16 bits size limit */
 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
 
+/* number of register is bound by the number of bits in regs_dump::mask (64) */
+#define PERF_SAMPLE_REGS_CACHE_SIZE (8 * sizeof(u64))
+
 struct regs_dump {
 	u64 abi;
 	u64 mask;
 	u64 *regs;
 
 	/* Cached values/mask filled by first register access. */
-	u64 cache_regs[PERF_REGS_MAX];
+	u64 cache_regs[PERF_SAMPLE_REGS_CACHE_SIZE];
 	u64 cache_mask;
 };
 
tools/perf/util/perf_regs.c +3 -0
index 5ee47ae15..06a7461ba 100644
@@ -25,6 +25,9 @@ int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 	int i, idx = 0;
 	u64 mask = regs->mask;
 
+	if ((u64)id >= PERF_SAMPLE_REGS_CACHE_SIZE)
+		return -EINVAL;
+
 	if (regs->cache_mask & (1ULL << id))
 		goto out;
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/3 perf tools: Rename perf_event_arm_regs for ARM64 registers

In reply to German Gomez

The registers for ARM and ARM64 are enumerated using two enums that have the same name. In order to be able to import both headers, the name of one can be replaced using the C preprocessor like so:

  #define perf_event_arm_regs perf_event_arm64_regs
  #include <asm/perf_regs.h>
  #undef perf_event_arm_regs

This patch updates all imports of ARM64's perf_regs.h in order to prevent the naming collision.

---
 tools/perf/arch/arm64/include/perf_regs.h | 2 ++
 tools/perf/util/libunwind/arm64.c         | 2 ++
 2 files changed, 4 insertions(+)
tools/perf/arch/arm64/include/perf_regs.h +2 -0
index fa3e07459..1f0d78b9f 100644
@@ -4,7 +4,9 @@
 
 #include <stdlib.h>
 #include <linux/types.h>
+#define perf_event_arm_regs perf_event_arm64_regs
 #include <asm/perf_regs.h>
+#undef perf_event_arm_regs
 
 void perf_regs_load(u64 *regs);
 
tools/perf/util/libunwind/arm64.c +2 -0
index c397be0c2..15f60fd09 100644
@@ -23,7 +23,9 @@
 
 #include "unwind.h"
 #include "libunwind-aarch64.h"
+#define perf_event_arm_regs perf_event_arm64_regs
 #include <../../../../arch/arm64/include/uapi/asm/perf_regs.h>
+#undef perf_event_arm_regs
 #include "../../arch/arm64/util/unwind-libunwind.c"
 
 /* NO_LIBUNWIND_DEBUG_FRAME is a feature flag for local libunwind,
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 3/3 perf tools: Support register names from all archs

In reply to German Gomez

When reading a perf.data file with register values, there is a mismatch between the names and the values of the registers because the tool is built using only the register names from the local architecture.

Reading a perf.data file that was recorded on ARM64, gives the following erroneous output on an X86 machine:

  # perf report -i perf_arm64.data -D
  [...]
  24661932634451 0x698 [0x21d0]: PERF_RECORD_SAMPLE(IP, 0x1): 43239/43239: 0xffffc5be8f100f98 period: 1 addr: 0
  ... user regs: mask 0x1ffffffff ABI 64-bit
  .... AX    0x0000ffffd1515817
  .... BX    0x0000ffffd1515480
  .... CX    0x0000aaaadabf6c80
  .... DX    0x000000000000002e
  .... SI    0x0000000040100401
  .... DI    0x0040600200000080
  .... BP    0x0000ffffd1510e10
  .... SP    0x0000000000000000
  .... IP    0x00000000000000dd
  .... FLAGS 0x0000ffffd1510cd0
  .... CS    0x0000000000000000
  .... SS    0x0000000000000030
  .... DS    0x0000ffffa569a208
  .... ES    0x0000000000000000
  .... FS    0x0000000000000000
  .... GS    0x0000000000000000
  .... R8    0x0000aaaad3de9650
  .... R9    0x0000ffffa57397f0
  .... R10   0x0000000000000001
  .... R11   0x0000ffffa57fd000
  .... R12   0x0000ffffd1515817
  .... R13   0x0000ffffd1515480
  .... R14   0x0000aaaadabf6c80
  .... R15   0x0000000000000000
  .... unknown 0x0000000000000001
  .... unknown 0x0000000000000000
  .... unknown 0x0000000000000000
  .... unknown 0x0000000000000000
  .... unknown 0x0000000000000000
  .... unknown 0x0000ffffd1510d90
  .... unknown 0x0000ffffa5739b90
  .... unknown 0x0000ffffd1510d80
  .... XMM0  0x0000ffffa57392c8
   ... thread: perf-exec:43239
   ...... dso: [kernel.kallsyms]

As can be seen, the register names correspond to X86 registers, even though the perf.data file was recorded on an ARM64 system. After this patch, the output of the command displays the correct register names:

  # perf report -i perf_arm64.data -D
  [...]
  24661932634451 0x698 [0x21d0]: PERF_RECORD_SAMPLE(IP, 0x1): 43239/43239: 0xffffc5be8f100f98 period: 1 addr: 0
  ... user regs: mask 0x1ffffffff ABI 64-bit
  .... x0    0x0000ffffd1515817
  .... x1    0x0000ffffd1515480
  .... x2    0x0000aaaadabf6c80
  .... x3    0x000000000000002e
  .... x4    0x0000000040100401
  .... x5    0x0040600200000080
  .... x6    0x0000ffffd1510e10
  .... x7    0x0000000000000000
  .... x8    0x00000000000000dd
  .... x9    0x0000ffffd1510cd0
  .... x10   0x0000000000000000
  .... x11   0x0000000000000030
  .... x12   0x0000ffffa569a208
  .... x13   0x0000000000000000
  .... x14   0x0000000000000000
  .... x15   0x0000000000000000
  .... x16   0x0000aaaad3de9650
  .... x17   0x0000ffffa57397f0
  .... x18   0x0000000000000001
  .... x19   0x0000ffffa57fd000
  .... x20   0x0000ffffd1515817
  .... x21   0x0000ffffd1515480
  .... x22   0x0000aaaadabf6c80
  .... x23   0x0000000000000000
  .... x24   0x0000000000000001
  .... x25   0x0000000000000000
  .... x26   0x0000000000000000
  .... x27   0x0000000000000000
  .... x28   0x0000000000000000
  .... x29   0x0000ffffd1510d90
  .... lr    0x0000ffffa5739b90
  .... sp    0x0000ffffd1510d80
  .... pc    0x0000ffffa57392c8
   ... thread: perf-exec:43239
   ...... dso: [kernel.kallsyms]
---
 tools/perf/arch/arm/include/perf_regs.h       |  42 --
 tools/perf/arch/arm64/include/perf_regs.h     |  76 --
 tools/perf/arch/csky/include/perf_regs.h      |  82 ---
 tools/perf/arch/mips/include/perf_regs.h      |  69 --
 tools/perf/arch/powerpc/include/perf_regs.h   |  66 --
 tools/perf/arch/riscv/include/perf_regs.h     |  74 --
 tools/perf/arch/s390/include/perf_regs.h      |  78 --
 tools/perf/arch/x86/include/perf_regs.h       |  82 ---
 tools/perf/builtin-script.c                   |  18 +-
 tools/perf/util/perf_regs.c                   | 666 ++++++++++++++++++
 tools/perf/util/perf_regs.h                   |  10 +-
 .../scripting-engines/trace-event-python.c    |  10 +-
 tools/perf/util/session.c                     |  25 +-
 13 files changed, 697 insertions(+), 601 deletions(-)
tools/perf/arch/arm/include/perf_regs.h +0 -42
index 408541928..99a06550e 100644
@@ -15,46 +15,4 @@ void perf_regs_load(u64 *regs);
 #define PERF_REG_IP	PERF_REG_ARM_PC
 #define PERF_REG_SP	PERF_REG_ARM_SP
 
-static inline const char *__perf_reg_name(int id)
-{
-	switch (id) {
-	case PERF_REG_ARM_R0:
-		return "r0";
-	case PERF_REG_ARM_R1:
-		return "r1";
-	case PERF_REG_ARM_R2:
-		return "r2";
-	case PERF_REG_ARM_R3:
-		return "r3";
-	case PERF_REG_ARM_R4:
-		return "r4";
-	case PERF_REG_ARM_R5:
-		return "r5";
-	case PERF_REG_ARM_R6:
-		return "r6";
-	case PERF_REG_ARM_R7:
-		return "r7";
-	case PERF_REG_ARM_R8:
-		return "r8";
-	case PERF_REG_ARM_R9:
-		return "r9";
-	case PERF_REG_ARM_R10:
-		return "r10";
-	case PERF_REG_ARM_FP:
-		return "fp";
-	case PERF_REG_ARM_IP:
-		return "ip";
-	case PERF_REG_ARM_SP:
-		return "sp";
-	case PERF_REG_ARM_LR:
-		return "lr";
-	case PERF_REG_ARM_PC:
-		return "pc";
-	default:
-		return NULL;
-	}
-
-	return NULL;
-}
-
 #endif /* ARCH_PERF_REGS_H */
tools/perf/arch/arm64/include/perf_regs.h +0 -76

80 lines not on this page · raw message

tools/perf/arch/csky/include/perf_regs.h +0 -82

86 lines not on this page · raw message

tools/perf/arch/mips/include/perf_regs.h +0 -69

73 lines not on this page · raw message

tools/perf/arch/powerpc/include/perf_regs.h +0 -66

70 lines not on this page · raw message

tools/perf/arch/riscv/include/perf_regs.h +0 -74

78 lines not on this page · raw message

tools/perf/arch/s390/include/perf_regs.h +0 -78

82 lines not on this page · raw message

tools/perf/arch/x86/include/perf_regs.h +0 -82

86 lines not on this page · raw message

tools/perf/builtin-script.c +10 -8
index 9434367af..da2175d70 100644
@@ -15,6 +15,7 @@
 #include "util/symbol.h"
 #include "util/thread.h"
 #include "util/trace-event.h"
+#include "util/env.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/evsel_fprintf.h"
@@ -648,7 +649,7 @@ static int perf_session__check_output_opt(struct perf_session *session)
 	return 0;
 }
 
-static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
+static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask, const char *arch,
 				     FILE *fp)
 {
 	unsigned i = 0, r;
@@ -661,7 +662,7 @@ static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
 
 	for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
 		u64 val = regs->regs[i++];
-		printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
+		printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r, arch), val);
 	}
 
 	return printed;
@@ -718,17 +719,17 @@ tod_scnprintf(struct perf_script *script, char *buf, int buflen,
 }
 
 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
-				      struct perf_event_attr *attr, FILE *fp)
+				      struct perf_event_attr *attr, const char *arch, FILE *fp)
 {
 	return perf_sample__fprintf_regs(&sample->intr_regs,
-					 attr->sample_regs_intr, fp);
+					 attr->sample_regs_intr, arch, fp);
 }
 
 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
-				      struct perf_event_attr *attr, FILE *fp)
+				      struct perf_event_attr *attr, const char *arch, FILE *fp)
 {
 	return perf_sample__fprintf_regs(&sample->user_regs,
-					 attr->sample_regs_user, fp);
+					 attr->sample_regs_user, arch, fp);
 }
 
 static int perf_sample__fprintf_start(struct perf_script *script,
@@ -2000,6 +2001,7 @@ static void process_event(struct perf_script *script,
 	struct evsel_script *es = evsel->priv;
 	FILE *fp = es->fp;
 	char str[PAGE_SIZE_NAME_LEN];
+	const char *arch = perf_env__arch(machine->env);
 
 	if (output[type].fields == 0)
 		return;
@@ -2066,10 +2068,10 @@ static void process_event(struct perf_script *script,
 	}
 
 	if (PRINT_FIELD(IREGS))
-		perf_sample__fprintf_iregs(sample, attr, fp);
+		perf_sample__fprintf_iregs(sample, attr, arch, fp);
 
 	if (PRINT_FIELD(UREGS))
-		perf_sample__fprintf_uregs(sample, attr, fp);
+		perf_sample__fprintf_uregs(sample, attr, arch, fp);
 
 	if (PRINT_FIELD(BRSTACK))
 		perf_sample__fprintf_brstack(sample, thread, attr, fp);
tools/perf/util/perf_regs.c +666 -0

677 lines not on this page · raw message

tools/perf/util/perf_regs.h +2 -8
index eeac181eb..4e6b1299c 100644
@@ -31,22 +31,16 @@ extern const struct sample_reg sample_reg_masks[];
 
 #define DWARF_MINIMAL_REGS ((1ULL << PERF_REG_IP) | (1ULL << PERF_REG_SP))
 
+const char *perf_reg_name(int id, const char *arch);
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
 
-static inline const char *perf_reg_name(int id)
-{
-	const char *reg_name = __perf_reg_name(id);
-
-	return reg_name ?: "unknown";
-}
-
 #else
 #define PERF_REGS_MASK	0
 #define PERF_REGS_MAX	0
 
 #define DWARF_MINIMAL_REGS PERF_REGS_MASK
 
-static inline const char *perf_reg_name(int id __maybe_unused)
+static inline const char *perf_reg_name(int id __maybe_unused, const char *arch __maybe_unused)
 {
 	return "unknown";
 }
tools/perf/util/scripting-engines/trace-event-python.c +6 -4
index c0c010350..0445bee92 100644
@@ -36,6 +36,7 @@
 #include "../debug.h"
 #include "../dso.h"
 #include "../callchain.h"
+#include "../env.h"
 #include "../evsel.h"
 #include "../event.h"
 #include "../thread.h"
@@ -687,7 +688,7 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
 			_PyUnicode_FromString(decode));
 }
 
-static void regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
+static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, char *bf, int size)
 {
 	unsigned int i = 0, r;
 	int printed = 0;
@@ -702,7 +703,7 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
 
 		printed += scnprintf(bf + printed, size - printed,
 				     "%5s:0x%" PRIx64 " ",
-				     perf_reg_name(r), val);
+				     perf_reg_name(r, arch), val);
 	}
 }
 
@@ -711,6 +712,7 @@ static void set_regs_in_dict(PyObject *dict,
 			     struct evsel *evsel)
 {
 	struct perf_event_attr *attr = &evsel->core.attr;
+	const char *arch = perf_env__arch(evsel__env(evsel));
 
 	/*
 	 * Here value 28 is a constant size which can be used to print
@@ -722,12 +724,12 @@ static void set_regs_in_dict(PyObject *dict,
 	int size = __sw_hweight64(attr->sample_regs_intr) * 28;
 	char bf[size];
 
-	regs_map(&sample->intr_regs, attr->sample_regs_intr, bf, sizeof(bf));
+	regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, sizeof(bf));
 
 	pydict_set_item_string_decref(dict, "iregs",
 			_PyUnicode_FromString(bf));
 
-	regs_map(&sample->user_regs, attr->sample_regs_user, bf, sizeof(bf));
+	regs_map(&sample->user_regs, attr->sample_regs_user, arch, bf, sizeof(bf));
 
 	pydict_set_item_string_decref(dict, "uregs",
 			_PyUnicode_FromString(bf));
tools/perf/util/session.c +13 -12
index d8857d1b6..e1a273048 100644
@@ -15,6 +15,7 @@
 #include "map_symbol.h"
 #include "branch.h"
 #include "debug.h"
+#include "env.h"
 #include "evlist.h"
 #include "evsel.h"
 #include "memswap.h"
@@ -1168,7 +1169,7 @@ static void branch_stack__printf(struct perf_sample *sample, bool callstack)
 	}
 }
 
-static void regs_dump__printf(u64 mask, u64 *regs)
+static void regs_dump__printf(u64 mask, u64 *regs, const char *arch)
 {
 	unsigned rid, i = 0;
 
@@ -1176,7 +1177,7 @@ static void regs_dump__printf(u64 mask, u64 *regs)
 		u64 val = regs[i++];
 
 		printf(".... %-5s 0x%016" PRIx64 "\n",
-		       perf_reg_name(rid), val);
+		       perf_reg_name(rid, arch), val);
 	}
 }
 
@@ -1194,7 +1195,7 @@ static inline const char *regs_dump_abi(struct regs_dump *d)
 	return regs_abi[d->abi];
 }
 
-static void regs__printf(const char *type, struct regs_dump *regs)
+static void regs__printf(const char *type, struct regs_dump *regs, const char *arch)
 {
 	u64 mask = regs->mask;
 
@@ -1203,23 +1204,23 @@ static void regs__printf(const char *type, struct regs_dump *regs)
 	       mask,
 	       regs_dump_abi(regs));
 
-	regs_dump__printf(mask, regs->regs);
+	regs_dump__printf(mask, regs->regs, arch);
 }
 
-static void regs_user__printf(struct perf_sample *sample)
+static void regs_user__printf(struct perf_sample *sample, const char *arch)
 {
 	struct regs_dump *user_regs = &sample->user_regs;
 
 	if (user_regs->regs)
-		regs__printf("user", user_regs);
+		regs__printf("user", user_regs, arch);
 }
 
-static void regs_intr__printf(struct perf_sample *sample)
+static void regs_intr__printf(struct perf_sample *sample, const char *arch)
 {
 	struct regs_dump *intr_regs = &sample->intr_regs;
 
 	if (intr_regs->regs)
-		regs__printf("intr", intr_regs);
+		regs__printf("intr", intr_regs, arch);
 }
 
 static void stack_user__printf(struct stack_dump *dump)
@@ -1304,7 +1305,7 @@ char *get_page_size_name(u64 size, char *str)
 }
 
 static void dump_sample(struct evsel *evsel, union perf_event *event,
-			struct perf_sample *sample)
+			struct perf_sample *sample, const char *arch)
 {
 	u64 sample_type;
 	char str[PAGE_SIZE_NAME_LEN];
@@ -1325,10 +1326,10 @@ static void dump_sample(struct evsel *evsel, union perf_event *event,
 		branch_stack__printf(sample, evsel__has_branch_callstack(evsel));
 
 	if (sample_type & PERF_SAMPLE_REGS_USER)
-		regs_user__printf(sample);
+		regs_user__printf(sample, arch);
 
 	if (sample_type & PERF_SAMPLE_REGS_INTR)
-		regs_intr__printf(sample);
+		regs_intr__printf(sample, arch);
 
 	if (sample_type & PERF_SAMPLE_STACK_USER)
 		stack_user__printf(&sample->user_stack);
@@ -1502,7 +1503,7 @@ static int machines__deliver_event(struct machines *machines,
 			++evlist->stats.nr_unknown_id;
 			return 0;
 		}
-		dump_sample(evsel, event, sample);
+		dump_sample(evsel, event, sample, perf_env__arch(machine->env));
 		if (machine == NULL) {
 			++evlist->stats.nr_unprocessable_samples;
 			return 0;
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v2 3/3] perf tools: Support register names from all archs

Quoted 6 lines · German Gomez
  tools/perf/arch/arm/include/perf_regs.h       |  42 --
  tools/perf/arch/arm64/include/perf_regs.h     |  76 --
  tools/perf/arch/csky/include/perf_regs.h      |  82 ---
  tools/perf/arch/mips/include/perf_regs.h      |  69 --
  tools/perf/arch/powerpc/include/perf_regs.h   |  66 --
  tools/perf/arch/riscv/include/perf_regs.h     |  74 --
Quoted 8 lines
  tools/perf/arch/s390/include/perf_regs.h      |  78 --
  tools/perf/arch/x86/include/perf_regs.h       |  82 ---
  tools/perf/builtin-script.c                   |  18 +-
  tools/perf/util/perf_regs.c                   | 666 ++++++++++++++++++
  tools/perf/util/perf_regs.h                   |  10 +-
  .../scripting-engines/trace-event-python.c    |  10 +-
  tools/perf/util/session.c                     |  25 +-
  13 files changed, 697 insertions(+), 601 deletions(-)

Did you consider leaving the register structures where they are while
renaming to include the arch name and then having as externs or similar? I see an example of that idea for arm64_unwind_libunwind_ops.

Cheers,
John

#5 lore raw

Re: [PATCH v2 3/3] perf tools: Support register names from all archs

Hi John,

Quoted 16 lines · John Garry
Quoted 8 lines · German Gomez

  tools/perf/arch/arm/include/perf_regs.h       |  42 -- tools/perf/arch/arm64/include/perf_regs.h     |  76 -- tools/perf/arch/csky/include/perf_regs.h      |  82 --- tools/perf/arch/mips/include/perf_regs.h      |  69 -- tools/perf/arch/powerpc/include/perf_regs.h   |  66 -- tools/perf/arch/riscv/include/perf_regs.h     |  74 -- tools/perf/arch/s390/include/perf_regs.h      |  78 -- tools/perf/arch/x86/include/perf_regs.h       |  82 ---

Quoted 6 lines
  tools/perf/builtin-script.c                   |  18 +-
  tools/perf/util/perf_regs.c                   | 666 ++++++++++++++++++
  tools/perf/util/perf_regs.h                   |  10 +-
  .../scripting-engines/trace-event-python.c    |  10 +-
  tools/perf/util/session.c                     |  25 +-
  13 files changed, 697 insertions(+), 601 deletions(-)
Quoted 3 lines

Did you consider leaving the register structures where they are while
renaming to include the arch name and then having as externs or similar? I see an example of that idea for arm64_unwind_libunwind_ops.

If by register structures you are referring to "__perf_reg_name(int)", I can't leave them where they are. Only one of them would be included in the build.

I think the idea from arm64_unwind_libunwind_ops makes more sense in that case because perf might not link against libunwind-arm64. In the case of registers, we always have this info available in /tools/.

Thanks,
German

Quoted 2 lines

Cheers,
John

#6 lore raw

Re: [PATCH v2 3/3] perf tools: Support register names from all archs

Quoted 18 lines · German Gomez

Hi John,

Quoted 15 lines · John Garry
Quoted 14 lines · German Gomez

  tools/perf/arch/arm/include/perf_regs.h       |  42 -- tools/perf/arch/arm64/include/perf_regs.h     |  76 -- tools/perf/arch/csky/include/perf_regs.h      |  82 --- tools/perf/arch/mips/include/perf_regs.h      |  69 -- tools/perf/arch/powerpc/include/perf_regs.h   |  66 -- tools/perf/arch/riscv/include/perf_regs.h     |  74 -- tools/perf/arch/s390/include/perf_regs.h      |  78 -- tools/perf/arch/x86/include/perf_regs.h       |  82 ---

  tools/perf/builtin-script.c                   |  18 +-
  tools/perf/util/perf_regs.c                   | 666 ++++++++++++++++++
  tools/perf/util/perf_regs.h                   |  10 +-
  .../scripting-engines/trace-event-python.c    |  10 +-
  tools/perf/util/session.c                     |  25 +-
  13 files changed, 697 insertions(+), 601 deletions(-)
Quoted 6 lines
Quoted 3 lines

Did you consider leaving the register structures where they are while
renaming to include the arch name and then having as externs or similar? I see an example of that idea for arm64_unwind_libunwind_ops.

If by register structures you are referring to "__perf_reg_name(int)", I can't leave them where they are. Only one of them would be included in the build.

I think I need to elaborate a bit more on this since some of the files involved share the same name but are serving different purposes and it could lead to confusion.

The linux repo has "perf_regs.h" for each architecture enumerating the registers from each architecture. These are the files I #include'd in "/tools/perf/util/perf_regs.c".

The other "perf_regs.h" affected by this patch are local only to perf. Likewise there is one file for each architecture, but contrary to the linux ones, they are mutually exclusive, so I can't #include them all:

#ifndef ARCH_PERF_REGS_H
#define ARCH_PERF_REGS_H
//...
#undef ARCH_PERF_REGS_H

Before the patch, the functions "__perf_reg_name" were declared & implemented in these headers, so I had to take them out.

Thanks,
German

#7 lore raw
In reply to German Gomez
Quoted 19 lines · German Gomez

The following changeset applies some corrections to the way system registers are processed and presented when reading perf.data files using the various perf tools.

The commit message from [3/3] shows how register names aren't correctly presented when performing x-arch analysis of perf.data files (recording in one arch, then reading the file from a different arch).

- [PATCH 1/3] Fixes a potential out-of-bounds access when reading the values of the registers in the perf.data file.
- [PATCH 2/3] Fixes an issue of ARM and ARM64 registers having the same enum name.
- [PATCH 3/3] Refactors the function "perf_reg_name" declared in the "tools/perf/util/perf_regs.h" header, in order to support every arch.

Quoted 2 lines

Thanks,
German

Looks good to me. Tested this patchset in powerpc by capturing regs in powerpc and doing perf report to read the data from x86.

Reviewed-and-Tested-by Athira Rajeev <[email protected]>
Quoted 39 lines

--
Changes since v1

- Added "Reported-by" tags.
- Removed [PATCH 2/4] because it's not needed (suggested by Athira Rajeev).
- Removed [PATCH 3/4] which created additional header files with the register names of every arch.
- Introduced [PATCH 2/3] to deal with ARM and ARM64 registers having the same enum name across "/tools/perf/".
- Reworked the refactor of "perf_reg_name" function (now implemented in perf_regs.c, rather than in the header file) in [PATCH 3/3].

German Gomez (3):
perf tools: Prevent out-of-bounds access to registers perf tools: Rename perf_event_arm_regs for ARM64 registers perf tools: Support register names from all archs

tools/perf/arch/arm/include/perf_regs.h       |  42 --
tools/perf/arch/arm64/include/perf_regs.h     |  78 +-
tools/perf/arch/csky/include/perf_regs.h      |  82 ---
tools/perf/arch/mips/include/perf_regs.h      |  69 --
tools/perf/arch/powerpc/include/perf_regs.h   |  66 --
tools/perf/arch/riscv/include/perf_regs.h     |  74 --
tools/perf/arch/s390/include/perf_regs.h      |  78 --
tools/perf/arch/x86/include/perf_regs.h       |  82 ---
tools/perf/builtin-script.c                   |  18 +-
tools/perf/util/event.h                       |   5 +-
tools/perf/util/libunwind/arm64.c             |   2 +
tools/perf/util/perf_regs.c                   | 671 +++++++++++++++++-
tools/perf/util/perf_regs.h                   |  10 +-
.../scripting-engines/trace-event-python.c    |  10 +-
tools/perf/util/session.c                     |  25 +-
15 files changed, 709 insertions(+), 603 deletions(-)
Signature · 1 line
2.25.1
#8 lore raw

Em Tue, Dec 14, 2021 at 02:20:26PM +0530, Athira Rajeev escreveu:

Quoted 19 lines
Quoted 19 lines · German Gomez

The following changeset applies some corrections to the way system registers are processed and presented when reading perf.data files using the various perf tools.

The commit message from [3/3] shows how register names aren't correctly presented when performing x-arch analysis of perf.data files (recording in one arch, then reading the file from a different arch).

- [PATCH 1/3] Fixes a potential out-of-bounds access when reading the values of the registers in the perf.data file.
- [PATCH 2/3] Fixes an issue of ARM and ARM64 registers having the same enum name.
- [PATCH 3/3] Refactors the function "perf_reg_name" declared in the "tools/perf/util/perf_regs.h" header, in order to support every arch.

Quoted 7 lines
Quoted 2 lines

Thanks,
German

Looks good to me. Tested this patchset in powerpc by capturing regs in powerpc and doing perf report to read the data from x86.

Reviewed-and-Tested-by Athira Rajeev <[email protected]>

Thanks, added to the commit log message.

- Arnaldo

Quoted 38 lines
Quoted 38 lines

--
Changes since v1

- Added "Reported-by" tags.
- Removed [PATCH 2/4] because it's not needed (suggested by Athira Rajeev).
- Removed [PATCH 3/4] which created additional header files with the register names of every arch.
- Introduced [PATCH 2/3] to deal with ARM and ARM64 registers having the same enum name across "/tools/perf/".
- Reworked the refactor of "perf_reg_name" function (now implemented in perf_regs.c, rather than in the header file) in [PATCH 3/3].

German Gomez (3):
perf tools: Prevent out-of-bounds access to registers perf tools: Rename perf_event_arm_regs for ARM64 registers perf tools: Support register names from all archs

tools/perf/arch/arm/include/perf_regs.h       |  42 --
tools/perf/arch/arm64/include/perf_regs.h     |  78 +-
tools/perf/arch/csky/include/perf_regs.h      |  82 ---
tools/perf/arch/mips/include/perf_regs.h      |  69 --
tools/perf/arch/powerpc/include/perf_regs.h   |  66 --
tools/perf/arch/riscv/include/perf_regs.h     |  74 --
tools/perf/arch/s390/include/perf_regs.h      |  78 --
tools/perf/arch/x86/include/perf_regs.h       |  82 ---
tools/perf/builtin-script.c                   |  18 +-
tools/perf/util/event.h                       |   5 +-
tools/perf/util/libunwind/arm64.c             |   2 +
tools/perf/util/perf_regs.c                   | 671 +++++++++++++++++-
tools/perf/util/perf_regs.h                   |  10 +-
.../scripting-engines/trace-event-python.c    |  10 +-
tools/perf/util/session.c                     |  25 +-
15 files changed, 709 insertions(+), 603 deletions(-)
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo