Skip to contentSkip to conversation

Support register names from all architectures

PATCH Applied German Gomez 6 participants 16 messages Dec 1, 2021 – Dec 13, 2021

Subsystems (4)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Support register names from 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 [4/4] shows how register names aren't correctly presented when performing x-arch analysis of perf.data files (i.e. recording in one arch, then reading the file from a different arch).

Please let me know if there are any concerns with this approach, or if any improvements can be made for subsequent re-rolls of the changeset.

- [PATCH 1/4] Fixes a potential out-of-bounds access when reading the values of the registers in the perf.data file.
- [PATCH 2/4] Modifies one of the callbacks in "struct scripting_ops" so that the python version can obtain the correct register names.
- [PATCH 3/4] Adds header files with the register indices and names from all architectures, which are used by the next patch.
- [PATCH 4/4] Refactors the function "perf_reg_name" so that it can support registers from all architectures, not just the local one.

Thanks,
German

German Gomez (4):
perf tools: Prevent out-of-bounds access to registers
perf script: Add "struct machine" parameter to process_event callback perf tools: Crete header files with register names
perf tools: Support register names from all architectures

 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                   |  26 ++---
 tools/perf/util/event.h                       |   5 +-
 tools/perf/util/perf_regs.c                   |   3 +
 tools/perf/util/perf_regs.h                   |  33 +++++-
 tools/perf/util/perf_regs_arm.h               |  57 ++++++++++
 tools/perf/util/perf_regs_arm64.h             |  83 ++++++++++++++
 tools/perf/util/perf_regs_csky.h              | 101 ++++++++++++++++++
 tools/perf/util/perf_regs_mips.h              |  76 +++++++++++++
 tools/perf/util/perf_regs_powerpc.h           |  74 +++++++++++++
 tools/perf/util/perf_regs_riscv.h             |  81 ++++++++++++++
 tools/perf/util/perf_regs_s390.h              |  85 +++++++++++++++
 tools/perf/util/perf_regs_x86.h               |  87 +++++++++++++++
 .../util/scripting-engines/trace-event-perl.c |   3 +-
 .../scripting-engines/trace-event-python.c    |  33 +++---
 tools/perf/util/session.c                     |  25 ++---
 tools/perf/util/trace-event-scripting.c       |   3 +-
 tools/perf/util/trace-event.h                 |   3 +-
 25 files changed, 733 insertions(+), 614 deletions(-)
 create mode 100644 tools/perf/util/perf_regs_arm.h
 create mode 100644 tools/perf/util/perf_regs_arm64.h
 create mode 100644 tools/perf/util/perf_regs_csky.h
 create mode 100644 tools/perf/util/perf_regs_mips.h
 create mode 100644 tools/perf/util/perf_regs_powerpc.h
 create mode 100644 tools/perf/util/perf_regs_riscv.h
 create mode 100644 tools/perf/util/perf_regs_s390.h
 create mode 100644 tools/perf/util/perf_regs_x86.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 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
Acked-by on v1 1/4
#2 lore raw

Re: [PATCH v1 1/4] perf tools: Prevent out-of-bounds access to registers

Quoted 11 lines · German Gomez

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.

thanks,
jirka

Quoted 44 lines
---
 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
Reviewed-by on v1 1/4
#3 lore raw

Re: [PATCH v1 1/4] perf tools: Prevent out-of-bounds access to registers

In reply to German Gomez
Quoted 10 lines · German Gomez

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.

Patch looks good to me.

Thanks,
Kajol Jain

Quoted 43 lines
---
 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;
 
maintainer Applied
#4 lore raw

Re: [PATCH v1 1/4] perf tools: Prevent out-of-bounds access to registers

Em Fri, Dec 10, 2021 at 02:47:49PM +0530, kajoljain escreveu:

Quoted 14 lines · German Gomez
Quoted 10 lines

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.

Quoted 3 lines

Patch looks good to me.

Thanks, applied.

- Arnaldo

Quoted 46 lines

Thanks,
Kajol Jain

Quoted 43 lines
---
 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
- Arnaldo
#5 lore raw

Re: [PATCH v1 1/4] perf tools: Prevent out-of-bounds access to registers

Quoted 13 lines · Arnaldo Carvalho de Melo

Em Fri, Dec 10, 2021 at 02:47:49PM +0530, kajoljain escreveu:

Quoted 12 lines · German Gomez
Quoted 10 lines

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.

Quoted 6 lines
Quoted 3 lines

Patch looks good to me.

Thanks, applied.

- Arnaldo

Thanks Arnaldo, and the rest for the review.

I did send a v2 of this patch afterwards. The only difference was to give credit to the reporter in the commit message with:

Thanks,
German

Quoted 47 lines
Quoted 47 lines

Thanks,
Kajol Jain

Quoted 43 lines
---
 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;
 
#6 lore raw

Re: [PATCH v1 1/4] perf tools: Prevent out-of-bounds access to registers

Quoted 20 lines · German Gomez
Quoted 18 lines · Arnaldo Carvalho de Melo

Em Fri, Dec 10, 2021 at 02:47:49PM +0530, kajoljain escreveu:

Quoted 7 lines · German Gomez
Quoted 5 lines

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.

Quoted 8 lines
Quoted 5 lines

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.

Patch looks good to me.

Thanks, applied.

Quoted 8 lines
Quoted 1 line

- Arnaldo

Thanks Arnaldo, and the rest for the review.

I did send a v2 of this patch afterwards. The only difference was to give credit to the reporter in the commit message with:

I'll add it.

- Arnaldo

Quoted 51 lines

Thanks,
German

Quoted 47 lines
Quoted 47 lines

Thanks,
Kajol Jain

Quoted 43 lines
---
 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;
 
#7 lore raw

PATCH 2/4 perf script: Add "struct machine" parameter to process_event callback

In reply to German Gomez

Include a "struct machine*" parameter to the process_event callback in the scripting layer. This will allow access to the perf_env from within this callback.

Followup patches will build on top of this to report the correct name of the registers in a perf.data file, consistently with the architecture the file was recorded in.

---
 tools/perf/builtin-script.c                   |  2 +-
 .../util/scripting-engines/trace-event-perl.c |  3 ++-
 .../scripting-engines/trace-event-python.c    | 23 +++++++++++--------
 tools/perf/util/trace-event-scripting.c       |  3 ++-
 tools/perf/util/trace-event.h                 |  3 ++-
 5 files changed, 21 insertions(+), 13 deletions(-)
tools/perf/builtin-script.c +1 -1
index 9434367af..711132f0b 100644
@@ -2256,7 +2256,7 @@ static int process_sample_event(struct perf_tool *tool,
 				thread__resolve(al.thread, &addr_al, sample);
 			addr_al_ptr = &addr_al;
 		}
-		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
+		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr, machine);
 	} else {
 		process_event(scr, sample, evsel, &al, &addr_al, machine);
 	}
tools/perf/util/scripting-engines/trace-event-perl.c +2 -1
index 32a721b3e..6017c4660 100644
@@ -454,7 +454,8 @@ static void perl_process_event(union perf_event *event,
 			       struct perf_sample *sample,
 			       struct evsel *evsel,
 			       struct addr_location *al,
-			       struct addr_location *addr_al)
+			       struct addr_location *addr_al,
+			       struct machine *machine __maybe_unused)
 {
 	scripting_context__update(scripting_context, event, sample, evsel, al, addr_al);
 	perl_process_tracepoint(sample, evsel, al);
tools/perf/util/scripting-engines/trace-event-python.c +14 -9
index c0c010350..e164f8d00 100644
@@ -708,7 +708,8 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
 
 static void set_regs_in_dict(PyObject *dict,
 			     struct perf_sample *sample,
-			     struct evsel *evsel)
+			     struct evsel *evsel,
+			     struct machine *machine __maybe_unused)
 {
 	struct perf_event_attr *attr = &evsel->core.attr;
 
@@ -776,6 +777,7 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 					 struct evsel *evsel,
 					 struct addr_location *al,
 					 struct addr_location *addr_al,
+					 struct machine *machine,
 					 PyObject *callchain)
 {
 	PyObject *dict, *dict_sample, *brstack, *brstacksym;
@@ -849,7 +851,7 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 			PyLong_FromUnsignedLongLong(sample->cyc_cnt));
 	}
 
-	set_regs_in_dict(dict, sample, evsel);
+	set_regs_in_dict(dict, sample, evsel, machine);
 
 	return dict;
 }
@@ -857,7 +859,8 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 static void python_process_tracepoint(struct perf_sample *sample,
 				      struct evsel *evsel,
 				      struct addr_location *al,
-				      struct addr_location *addr_al)
+				      struct addr_location *addr_al,
+				      struct machine *machine)
 {
 	struct tep_event *event = evsel->tp_format;
 	PyObject *handler, *context, *t, *obj = NULL, *callchain;
@@ -964,7 +967,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
 		PyTuple_SetItem(t, n++, dict);
 
 	if (get_argument_count(handler) == (int) n + 1) {
-		all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al,
+		all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al, machine,
 			callchain);
 		PyTuple_SetItem(t, n++,	all_entries_dict);
 	} else {
@@ -1366,7 +1369,8 @@ static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
 static void python_process_general_event(struct perf_sample *sample,
 					 struct evsel *evsel,
 					 struct addr_location *al,
-					 struct addr_location *addr_al)
+					 struct addr_location *addr_al,
+					 struct machine *machine)
 {
 	PyObject *handler, *t, *dict, *callchain;
 	static char handler_name[64];
@@ -1388,7 +1392,7 @@ static void python_process_general_event(struct perf_sample *sample,
 
 	/* ip unwinding */
 	callchain = python_process_callchain(sample, evsel, al);
-	dict = get_perf_sample_dict(sample, evsel, al, addr_al, callchain);
+	dict = get_perf_sample_dict(sample, evsel, al, addr_al, machine, callchain);
 
 	PyTuple_SetItem(t, n++, dict);
 	if (_PyTuple_Resize(&t, n) == -1)
@@ -1403,7 +1407,8 @@ static void python_process_event(union perf_event *event,
 				 struct perf_sample *sample,
 				 struct evsel *evsel,
 				 struct addr_location *al,
-				 struct addr_location *addr_al)
+				 struct addr_location *addr_al,
+				 struct machine *machine)
 {
 	struct tables *tables = &tables_global;
 
@@ -1411,14 +1416,14 @@ static void python_process_event(union perf_event *event,
 
 	switch (evsel->core.attr.type) {
 	case PERF_TYPE_TRACEPOINT:
-		python_process_tracepoint(sample, evsel, al, addr_al);
+		python_process_tracepoint(sample, evsel, al, addr_al, machine);
 		break;
 	/* Reserve for future process_hw/sw/raw APIs */
 	default:
 		if (tables->db_export_mode)
 			db_export__sample(&tables->dbe, event, sample, evsel, al, addr_al);
 		else
-			python_process_general_event(sample, evsel, al, addr_al);
+			python_process_general_event(sample, evsel, al, addr_al, machine);
 	}
 }
 
tools/perf/util/trace-event-scripting.c +2 -1
index 7172ca052..089a2c905 100644
@@ -51,7 +51,8 @@ static void process_event_unsupported(union perf_event *event __maybe_unused,
 				      struct perf_sample *sample __maybe_unused,
 				      struct evsel *evsel __maybe_unused,
 				      struct addr_location *al __maybe_unused,
-				      struct addr_location *addr_al __maybe_unused)
+				      struct addr_location *addr_al __maybe_unused,
+				      struct machine *machine __maybe_unused)
 {
 }
 
tools/perf/util/trace-event.h +2 -1
index 640981105..6f5b1a6d5 100644
@@ -81,7 +81,8 @@ struct scripting_ops {
 			       struct perf_sample *sample,
 			       struct evsel *evsel,
 			       struct addr_location *al,
-			       struct addr_location *addr_al);
+			       struct addr_location *addr_al,
+			       struct machine *machine);
 	void (*process_switch)(union perf_event *event,
 			       struct perf_sample *sample,
 			       struct machine *machine);
Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH v1 2/4] perf script: Add "struct machine" parameter to process_event callback

Quoted 26 lines · German Gomez

Include a "struct machine*" parameter to the process_event callback in the scripting layer. This will allow access to the perf_env from within this callback.

Followup patches will build on top of this to report the correct name of the registers in a perf.data file, consistently with the architecture the file was recorded in.

---

tools/perf/builtin-script.c                   |  2 +-
.../util/scripting-engines/trace-event-perl.c |  3 ++-
.../scripting-engines/trace-event-python.c    | 23 +++++++++++--------
tools/perf/util/trace-event-scripting.c       |  3 ++-
tools/perf/util/trace-event.h                 |  3 ++-
5 files changed, 21 insertions(+), 13 deletions(-)
tools/perf/builtin-script.c +0 -0
index 9434367af..711132f0b 100644

tools/perf/builtin-script.c static int process_sample_event(struct perf_tool *tool, · line 2259

Quoted 5 lines
tools/perf/builtin-script.c +1 -1
@@ -2256,7 +2256,7 @@ static int process_sample_event(struct perf_tool *tool,
 				thread__resolve(al.thread, &addr_al, sample);
 			addr_al_ptr = &addr_al;
 		}
-		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
+		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr, machine);

Hi,

Looks like the patch is using “machine” to allow access to perf_env__arch and there by to get the “arch” value.
But can we use from evsel, like "perf_env__arch(evsel__env(evsel))” to get arch value instead of including new parameter for “struct machine” ?

Thanks
Athira

Quoted 145 lines
tools/perf/builtin-script.c +0 -0
 	} else {
 		process_event(scr, sample, evsel, &al, &addr_al, machine);
 	}
tools/perf/util/scripting-engines/trace-event-perl.c +2 -1
index 32a721b3e..6017c4660 100644
@@ -454,7 +454,8 @@ static void perl_process_event(union perf_event *event,
 			       struct perf_sample *sample,
 			       struct evsel *evsel,
 			       struct addr_location *al,
-			       struct addr_location *addr_al)
+			       struct addr_location *addr_al,
+			       struct machine *machine __maybe_unused)
 {
 	scripting_context__update(scripting_context, event, sample, evsel, al, addr_al);
 	perl_process_tracepoint(sample, evsel, al);
tools/perf/util/scripting-engines/trace-event-python.c +14 -9
index c0c010350..e164f8d00 100644
@@ -708,7 +708,8 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
 
 static void set_regs_in_dict(PyObject *dict,
 			     struct perf_sample *sample,
-			     struct evsel *evsel)
+			     struct evsel *evsel,
+			     struct machine *machine __maybe_unused)
 {
 	struct perf_event_attr *attr = &evsel->core.attr;
 
@@ -776,6 +777,7 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 					 struct evsel *evsel,
 					 struct addr_location *al,
 					 struct addr_location *addr_al,
+					 struct machine *machine,
 					 PyObject *callchain)
 {
 	PyObject *dict, *dict_sample, *brstack, *brstacksym;
@@ -849,7 +851,7 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 			PyLong_FromUnsignedLongLong(sample->cyc_cnt));
 	}
 
-	set_regs_in_dict(dict, sample, evsel);
+	set_regs_in_dict(dict, sample, evsel, machine);
 
 	return dict;
 }
@@ -857,7 +859,8 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 static void python_process_tracepoint(struct perf_sample *sample,
 				      struct evsel *evsel,
 				      struct addr_location *al,
-				      struct addr_location *addr_al)
+				      struct addr_location *addr_al,
+				      struct machine *machine)
 {
 	struct tep_event *event = evsel->tp_format;
 	PyObject *handler, *context, *t, *obj = NULL, *callchain;
@@ -964,7 +967,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
 		PyTuple_SetItem(t, n++, dict);
 
 	if (get_argument_count(handler) == (int) n + 1) {
-		all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al,
+		all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al, machine,
 			callchain);
 		PyTuple_SetItem(t, n++,	all_entries_dict);
 	} else {
@@ -1366,7 +1369,8 @@ static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
 static void python_process_general_event(struct perf_sample *sample,
 					 struct evsel *evsel,
 					 struct addr_location *al,
-					 struct addr_location *addr_al)
+					 struct addr_location *addr_al,
+					 struct machine *machine)
 {
 	PyObject *handler, *t, *dict, *callchain;
 	static char handler_name[64];
@@ -1388,7 +1392,7 @@ static void python_process_general_event(struct perf_sample *sample,
 
 	/* ip unwinding */
 	callchain = python_process_callchain(sample, evsel, al);
-	dict = get_perf_sample_dict(sample, evsel, al, addr_al, callchain);
+	dict = get_perf_sample_dict(sample, evsel, al, addr_al, machine, callchain);
 
 	PyTuple_SetItem(t, n++, dict);
 	if (_PyTuple_Resize(&t, n) == -1)
@@ -1403,7 +1407,8 @@ static void python_process_event(union perf_event *event,
 				 struct perf_sample *sample,
 				 struct evsel *evsel,
 				 struct addr_location *al,
-				 struct addr_location *addr_al)
+				 struct addr_location *addr_al,
+				 struct machine *machine)
 {
 	struct tables *tables = &tables_global;
 
@@ -1411,14 +1416,14 @@ static void python_process_event(union perf_event *event,
 
 	switch (evsel->core.attr.type) {
 	case PERF_TYPE_TRACEPOINT:
-		python_process_tracepoint(sample, evsel, al, addr_al);
+		python_process_tracepoint(sample, evsel, al, addr_al, machine);
 		break;
 	/* Reserve for future process_hw/sw/raw APIs */
 	default:
 		if (tables->db_export_mode)
 			db_export__sample(&tables->dbe, event, sample, evsel, al, addr_al);
 		else
-			python_process_general_event(sample, evsel, al, addr_al);
+			python_process_general_event(sample, evsel, al, addr_al, machine);
 	}
 }
 
tools/perf/util/trace-event-scripting.c +2 -1
index 7172ca052..089a2c905 100644
@@ -51,7 +51,8 @@ static void process_event_unsupported(union perf_event *event __maybe_unused,
 				      struct perf_sample *sample __maybe_unused,
 				      struct evsel *evsel __maybe_unused,
 				      struct addr_location *al __maybe_unused,
-				      struct addr_location *addr_al __maybe_unused)
+				      struct addr_location *addr_al __maybe_unused,
+				      struct machine *machine __maybe_unused)
 {
 }
 
tools/perf/util/trace-event.h +2 -1
index 640981105..6f5b1a6d5 100644
@@ -81,7 +81,8 @@ struct scripting_ops {
 			       struct perf_sample *sample,
 			       struct evsel *evsel,
 			       struct addr_location *al,
-			       struct addr_location *addr_al);
+			       struct addr_location *addr_al,
+			       struct machine *machine);
 	void (*process_switch)(union perf_event *event,
 			       struct perf_sample *sample,
 			       struct machine *machine);
Signature · 1 line
2.25.1
#9 lore raw

Re: [PATCH v1 2/4] perf script: Add "struct machine" parameter to process_event callback

Hi Athira,

Quoted 32 lines · Athira Rajeev
Quoted 25 lines · German Gomez

Include a "struct machine*" parameter to the process_event callback in the scripting layer. This will allow access to the perf_env from within this callback.

Followup patches will build on top of this to report the correct name of the registers in a perf.data file, consistently with the architecture the file was recorded in.

---

tools/perf/builtin-script.c                   |  2 +-
.../util/scripting-engines/trace-event-perl.c |  3 ++-
.../scripting-engines/trace-event-python.c    | 23 +++++++++++--------
tools/perf/util/trace-event-scripting.c       |  3 ++-
tools/perf/util/trace-event.h                 |  3 ++-
5 files changed, 21 insertions(+), 13 deletions(-)
tools/perf/builtin-script.c +0 -0
index 9434367af..711132f0b 100644

tools/perf/builtin-script.c static int process_sample_event(struct perf_tool *tool, · line 2259

Quoted 5 lines
tools/perf/builtin-script.c +1 -1
@@ -2256,7 +2256,7 @@ static int process_sample_event(struct perf_tool *tool,
 				thread__resolve(al.thread, &addr_al, sample);
 			addr_al_ptr = &addr_al;
 		}
-		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
+		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr, machine);

Hi,

Quoted 5 lines

Looks like the patch is using “machine” to allow access to perf_env__arch and there by to get the “arch” value.
But can we use from evsel, like "perf_env__arch(evsel__env(evsel))” to get arch value instead of including new parameter for “struct machine” ?

Thanks
Athira

Thanks for the suggestion. It looks like we can skip this patch if we can get the arch value that way.

Thanks!
German

#10 lore raw

Re: [PATCH v1 2/4] perf script: Add "struct machine" parameter to process_event callback

Em Fri, Dec 03, 2021 at 12:00:26PM +0000, German Gomez escreveu:

Quoted 35 lines

Hi Athira,

Quoted 32 lines · Athira Rajeev
Quoted 25 lines · German Gomez

Include a "struct machine*" parameter to the process_event callback in the scripting layer. This will allow access to the perf_env from within this callback.

Followup patches will build on top of this to report the correct name of the registers in a perf.data file, consistently with the architecture the file was recorded in.

---

tools/perf/builtin-script.c                   |  2 +-
.../util/scripting-engines/trace-event-perl.c |  3 ++-
.../scripting-engines/trace-event-python.c    | 23 +++++++++++--------
tools/perf/util/trace-event-scripting.c       |  3 ++-
tools/perf/util/trace-event.h                 |  3 ++-
5 files changed, 21 insertions(+), 13 deletions(-)
tools/perf/builtin-script.c +0 -0
index 9434367af..711132f0b 100644

tools/perf/builtin-script.c static int process_sample_event(struct perf_tool *tool, · line 2259

Quoted 5 lines
tools/perf/builtin-script.c +1 -1
@@ -2256,7 +2256,7 @@ static int process_sample_event(struct perf_tool *tool,
 				thread__resolve(al.thread, &addr_al, sample);
 			addr_al_ptr = &addr_al;
 		}
-		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
+		scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr, machine);

Hi,

Quoted 8 lines
Quoted 5 lines

Looks like the patch is using “machine” to allow access to perf_env__arch and there by to get the “arch” value.
But can we use from evsel, like "perf_env__arch(evsel__env(evsel))” to get arch value instead of including new parameter for “struct machine” ?

Thanks
Athira

Thanks for the suggestion. It looks like we can skip this patch if we can get the arch value that way.

So the first one is already upstream, will you post a v2?

- Arnaldo

#11 lore raw

Re: [PATCH v1 2/4] perf script: Add "struct machine" parameter to process_event callback

Quoted 7 lines · Arnaldo Carvalho de Melo

[...]

Quoted 2 lines

Thanks for the suggestion. It looks like we can skip this patch if we can get the arch value that way.

So the first one is already upstream, will you post a v2?

- Arnaldo

Hi Arnaldo,

I sent a v2 where this patch was removed. The upstreamed patch (1/4 here) remained the same.

https://lore.kernel.org/all/[email protected]/

#12 lore raw

Re: [PATCH v1 2/4] perf script: Add "struct machine" parameter to process_event callback

Quoted 6 lines · German Gomez
Quoted 4 lines · Arnaldo Carvalho de Melo

[...]

Quoted 2 lines

Thanks for the suggestion. It looks like we can skip this patch if we can get the arch value that way.

Quoted 8 lines
Quoted 3 lines

So the first one is already upstream, will you post a v2?

- Arnaldo

Hi Arnaldo,

I sent a v2 where this patch was removed. The upstreamed patch (1/4 here) remained the same.

Thanks, I'll check.

- Arnaldo

Quoted 2 lines

https://lore.kernel.org/all/[email protected]/

#13 lore raw

PATCH 3/4 perf tools: Crete header files with register names

In reply to German Gomez

This patch adds C headers containing the names of the registers of all architectures (that currently support registers in perf).

A followup patch will re-implement the function "perf_reg_name" using this headers to display register names consistent with the achitecture of the perf.data files.

---
 tools/perf/util/perf_regs_arm.h     |  57 ++++++++++++++++
 tools/perf/util/perf_regs_arm64.h   |  83 +++++++++++++++++++++++
 tools/perf/util/perf_regs_csky.h    | 101 ++++++++++++++++++++++++++++
 tools/perf/util/perf_regs_mips.h    |  76 +++++++++++++++++++++
 tools/perf/util/perf_regs_powerpc.h |  74 ++++++++++++++++++++
 tools/perf/util/perf_regs_riscv.h   |  81 ++++++++++++++++++++++
 tools/perf/util/perf_regs_s390.h    |  85 +++++++++++++++++++++++
 tools/perf/util/perf_regs_x86.h     |  87 ++++++++++++++++++++++++
 8 files changed, 644 insertions(+)
 create mode 100644 tools/perf/util/perf_regs_arm.h
 create mode 100644 tools/perf/util/perf_regs_arm64.h
 create mode 100644 tools/perf/util/perf_regs_csky.h
 create mode 100644 tools/perf/util/perf_regs_mips.h
 create mode 100644 tools/perf/util/perf_regs_powerpc.h
 create mode 100644 tools/perf/util/perf_regs_riscv.h
 create mode 100644 tools/perf/util/perf_regs_s390.h
 create mode 100644 tools/perf/util/perf_regs_x86.h
tools/perf/util/perf_regs_arm.h new file +57 -0
new file mode 100644
index 000000000..779b40d6c
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef __PERF_REGS_ARM_H
+#define __PERF_REGS_ARM_H
+
+/*
+ * ARM and ARM64 registers are grouped under enums of the same name.
+ * Temporarily rename the name of the enum to prevent the naming collision.
+ */
+#define perf_event_arm_regs perf_event_arm_regs_workaround
+
+#include "../../arch/arm/include/uapi/asm/perf_regs.h"
+
+static inline const char *__perf_reg_name_arm(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;
+}
+
+#undef perf_event_arm_regs
+
+#endif /* __PERF_REGS_ARM_H */
tools/perf/util/perf_regs_arm64.h new file +83 -0

83 lines not on this page · raw message

tools/perf/util/perf_regs_csky.h new file +101 -0

101 lines not on this page · raw message

tools/perf/util/perf_regs_mips.h new file +76 -0

76 lines not on this page · raw message

tools/perf/util/perf_regs_powerpc.h new file +74 -0

74 lines not on this page · raw message

tools/perf/util/perf_regs_riscv.h new file +81 -0

81 lines not on this page · raw message

tools/perf/util/perf_regs_s390.h new file +85 -0

85 lines not on this page · raw message

tools/perf/util/perf_regs_x86.h new file +87 -0

88 lines not on this page · raw message

Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#14 lore raw

PATCH 4/4 perf tools: Support register names from all architectures

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                   | 24 +++---
 tools/perf/util/perf_regs.h                   | 33 +++++++-
 .../scripting-engines/trace-event-python.c    | 12 +--
 tools/perf/util/session.c                     | 25 +++---
 12 files changed, 62 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 +12 -12
index 711132f0b..3da0c95c6 100644
@@ -4,6 +4,7 @@
 #include "util/counts.h"
 #include "util/debug.h"
 #include "util/dso.h"
+#include "util/env.h"
 #include <subcmd/exec-cmd.h>
 #include "util/header.h"
 #include <subcmd/parse-options.h>
@@ -649,7 +650,7 @@ static int perf_session__check_output_opt(struct perf_session *session)
 }
 
 static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
-				     FILE *fp)
+				     const char *arch, FILE *fp)
 {
 	unsigned i = 0, r;
 	int printed = 0;
@@ -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;
@@ -717,18 +718,16 @@ tod_scnprintf(struct perf_script *script, char *buf, int buflen,
 	return buf;
 }
 
-static int perf_sample__fprintf_iregs(struct perf_sample *sample,
-				      struct perf_event_attr *attr, FILE *fp)
+static int perf_sample__fprintf_iregs(struct perf_sample *sample, struct perf_event_attr *attr,
+				      const char *arch, FILE *fp)
 {
-	return perf_sample__fprintf_regs(&sample->intr_regs,
-					 attr->sample_regs_intr, fp);
+	return perf_sample__fprintf_regs(&sample->intr_regs, attr->sample_regs_intr, arch, fp);
 }
 
-static int perf_sample__fprintf_uregs(struct perf_sample *sample,
-				      struct perf_event_attr *attr, FILE *fp)
+static int perf_sample__fprintf_uregs(struct perf_sample *sample, struct perf_event_attr *attr,
+				      const char *arch, FILE *fp)
 {
-	return perf_sample__fprintf_regs(&sample->user_regs,
-					 attr->sample_regs_user, fp);
+	return perf_sample__fprintf_regs(&sample->user_regs, attr->sample_regs_user, arch, fp);
 }
 
 static int perf_sample__fprintf_start(struct perf_script *script,
@@ -2000,6 +1999,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 +2066,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.h +30 -3
index eeac181eb..a201181fc 100644
@@ -27,15 +27,42 @@ uint64_t arch__user_reg_mask(void);
 #ifdef HAVE_PERF_REGS_SUPPORT
 extern const struct sample_reg sample_reg_masks[];
 
+#include <string.h>
 #include <perf_regs.h>
 
 #define DWARF_MINIMAL_REGS ((1ULL << PERF_REG_IP) | (1ULL << PERF_REG_SP))
 
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
 
-static inline const char *perf_reg_name(int id)
+#include "perf_regs_csky.h"
+#include "perf_regs_mips.h"
+#include "perf_regs_powerpc.h"
+#include "perf_regs_riscv.h"
+#include "perf_regs_s390.h"
+#include "perf_regs_x86.h"
+#include "perf_regs_arm.h"
+#include "perf_regs_arm64.h"
+
+static inline const char *perf_reg_name(int id, const char *arch)
 {
-	const char *reg_name = __perf_reg_name(id);
+	const char *reg_name = NULL;
+
+	if (!strcmp(arch, "csky"))
+		reg_name = __perf_reg_name_csky(id);
+	else if (!strcmp(arch, "mips"))
+		reg_name = __perf_reg_name_mips(id);
+	else if (!strcmp(arch, "powerpc"))
+		reg_name = __perf_reg_name_powerpc(id);
+	else if (!strcmp(arch, "riscv"))
+		reg_name = __perf_reg_name_riscv(id);
+	else if (!strcmp(arch, "s390"))
+		reg_name = __perf_reg_name_s390(id);
+	else if (!strcmp(arch, "x86"))
+		reg_name = __perf_reg_name_x86(id);
+	else if (!strcmp(arch, "arm"))
+		reg_name = __perf_reg_name_arm(id);
+	else if (!strcmp(arch, "arm64"))
+		reg_name = __perf_reg_name_arm64(id);
 
 	return reg_name ?: "unknown";
 }
@@ -46,7 +73,7 @@ static inline const char *perf_reg_name(int id)
 
 #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 +7 -5
index e164f8d00..373fdd798 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,16 +703,17 @@ 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);
 	}
 }
 
 static void set_regs_in_dict(PyObject *dict,
 			     struct perf_sample *sample,
 			     struct evsel *evsel,
-			     struct machine *machine __maybe_unused)
+			     struct machine *machine)
 {
 	struct perf_event_attr *attr = &evsel->core.attr;
+	const char *arch = perf_env__arch(machine->env);
 
 	/*
 	 * Here value 28 is a constant size which can be used to print
@@ -723,12 +725,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
#15 lore raw

Re: [PATCH v1 4/4] perf tools: Support register names from all architectures

Quoted 16 lines · German Gomez

[...]

tools/perf/util/perf_regs.h +1 -0
index eeac181eb..a201181fc 100644
@@ -27,15 +27,42 @@ uint64_t arch__user_reg_mask(void);
 #ifdef HAVE_PERF_REGS_SUPPORT
 extern const struct sample_reg sample_reg_masks[];
 
+#include <string.h>
 #include <perf_regs.h>
 
 #define DWARF_MINIMAL_REGS ((1ULL << PERF_REG_IP) | (1ULL << PERF_REG_SP))
 
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);

tools/perf/util/perf_regs.h uint64_t arch__user_reg_mask(void); · line 44

Quoted 10 lines
tools/perf/util/perf_regs.h +8 -1
 
-static inline const char *perf_reg_name(int id)
+#include "perf_regs_csky.h"
+#include "perf_regs_mips.h"
+#include "perf_regs_powerpc.h"
+#include "perf_regs_riscv.h"
+#include "perf_regs_s390.h"
+#include "perf_regs_x86.h"
+#include "perf_regs_arm.h"
+#include "perf_regs_arm64.h"

Something that slipped through: this is failing to compile perf on ARM32 due to the order of the imports:

util/../../arch/arm64/include/uapi/asm/perf_regs.h:5:6: error: nested redefinition of ‘enum perf_event_arm_regs’ 5 | enum perf_event_arm_regs {
      |      ^~~~~~~~~~~~~~~~~~~

Both #import <perf_regs.h> and "perf_regs_arm.h" are importing the same header (/tools/arch/arm/include/uapi/asm/perf_regs.h) so this part of the [PATCH 3/4] isn't doing anything:

tools/perf/util/perf_regs_arm.h new file +11 -0
new file mode 100644
index 000000000..779b40d6c
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef __PERF_REGS_ARM_H
+#define __PERF_REGS_ARM_H
+
+/*
+ * ARM and ARM64 registers are grouped under enums of the same name.
+ * Temporarily rename the name of the enum to prevent the naming collision.
+ */
+#define perf_event_arm_regs perf_event_arm_regs_workaround
+
+#include "../../arch/arm/include/uapi/asm/perf_regs.h"

There is a similar workaround in the csky version. Although it should compile, I think some of the register names might be missing (the ones under #if defined(__CSKYABIV2__)).

I'm wondering if it would be wiser to update this changeset to only consider a small number of platforms (maybe x86 and arm64) and see how it goes.