perf dwarf-regs: Add get_dwarf_regnum()

The get_dwarf_regnum() returns a DWARF register number from a register
name string according to the psABI.  Also add two pseudo encodings of
DWARF_REG_PC which is a register that are used by PC-relative addressing
and DWARF_REG_FB which is a frame base register.  They need to be
handled in a special way.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Namhyung Kim 2023-12-12 16:13:08 -08:00 committed by Arnaldo Carvalho de Melo
parent 60cb19b485
commit 3eee606757
3 changed files with 91 additions and 0 deletions

View file

@ -113,3 +113,41 @@ int regs_query_register_offset(const char *name)
return roff->offset;
return -EINVAL;
}
struct dwarf_regs_idx {
const char *name;
int idx;
};
static const struct dwarf_regs_idx x86_regidx_table[] = {
{ "rax", 0 }, { "eax", 0 }, { "ax", 0 }, { "al", 0 },
{ "rdx", 1 }, { "edx", 1 }, { "dx", 1 }, { "dl", 1 },
{ "rcx", 2 }, { "ecx", 2 }, { "cx", 2 }, { "cl", 2 },
{ "rbx", 3 }, { "edx", 3 }, { "bx", 3 }, { "bl", 3 },
{ "rsi", 4 }, { "esi", 4 }, { "si", 4 }, { "sil", 4 },
{ "rdi", 5 }, { "edi", 5 }, { "di", 5 }, { "dil", 5 },
{ "rbp", 6 }, { "ebp", 6 }, { "bp", 6 }, { "bpl", 6 },
{ "rsp", 7 }, { "esp", 7 }, { "sp", 7 }, { "spl", 7 },
{ "r8", 8 }, { "r8d", 8 }, { "r8w", 8 }, { "r8b", 8 },
{ "r9", 9 }, { "r9d", 9 }, { "r9w", 9 }, { "r9b", 9 },
{ "r10", 10 }, { "r10d", 10 }, { "r10w", 10 }, { "r10b", 10 },
{ "r11", 11 }, { "r11d", 11 }, { "r11w", 11 }, { "r11b", 11 },
{ "r12", 12 }, { "r12d", 12 }, { "r12w", 12 }, { "r12b", 12 },
{ "r13", 13 }, { "r13d", 13 }, { "r13w", 13 }, { "r13b", 13 },
{ "r14", 14 }, { "r14d", 14 }, { "r14w", 14 }, { "r14b", 14 },
{ "r15", 15 }, { "r15d", 15 }, { "r15w", 15 }, { "r15b", 15 },
{ "rip", DWARF_REG_PC },
};
int get_arch_regnum(const char *name)
{
unsigned int i;
if (*name != '%')
return -EINVAL;
for (i = 0; i < ARRAY_SIZE(x86_regidx_table); i++)
if (!strcmp(x86_regidx_table[i].name, name + 1))
return x86_regidx_table[i].idx;
return -ENOENT;
}

View file

@ -5,9 +5,12 @@
* Written by: Masami Hiramatsu <mhiramat@kernel.org>
*/
#include <stdlib.h>
#include <string.h>
#include <debug.h>
#include <dwarf-regs.h>
#include <elf.h>
#include <errno.h>
#include <linux/kernel.h>
#ifndef EM_AARCH64
@ -68,3 +71,34 @@ const char *get_dwarf_regstr(unsigned int n, unsigned int machine)
}
return NULL;
}
__weak int get_arch_regnum(const char *name __maybe_unused)
{
return -ENOTSUP;
}
/* Return DWARF register number from architecture register name */
int get_dwarf_regnum(const char *name, unsigned int machine)
{
char *regname = strdup(name);
int reg = -1;
char *p;
if (regname == NULL)
return -EINVAL;
/* For convenience, remove trailing characters */
p = strpbrk(regname, " ,)");
if (p)
*p = '\0';
switch (machine) {
case EM_NONE: /* Generic arch - use host arch */
reg = get_arch_regnum(regname);
break;
default:
pr_err("ELF MACHINE %x is not supported.\n", machine);
}
free(regname);
return reg;
}

View file

@ -2,6 +2,9 @@
#ifndef _PERF_DWARF_REGS_H_
#define _PERF_DWARF_REGS_H_
#define DWARF_REG_PC 0xd3af9c /* random number */
#define DWARF_REG_FB 0xd3affb /* random number */
#ifdef HAVE_DWARF_SUPPORT
const char *get_arch_regstr(unsigned int n);
/*
@ -10,6 +13,22 @@ const char *get_arch_regstr(unsigned int n);
* machine: ELF machine signature (EM_*)
*/
const char *get_dwarf_regstr(unsigned int n, unsigned int machine);
int get_arch_regnum(const char *name);
/*
* get_dwarf_regnum - Returns DWARF regnum from register name
* name: architecture register name
* machine: ELF machine signature (EM_*)
*/
int get_dwarf_regnum(const char *name, unsigned int machine);
#else /* HAVE_DWARF_SUPPORT */
static inline int get_dwarf_regnum(const char *name __maybe_unused,
unsigned int machine __maybe_unused)
{
return -1;
}
#endif
#ifdef HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET