xref: /openbmc/linux/tools/perf/arch/arm64/util/dwarf-regs.c (revision b746a1a2860f4a918f32d10dc569115d282aaf2f)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28ab596afSJean Pihet /*
38ab596afSJean Pihet  * Mapping of DWARF debug register numbers into register names.
48ab596afSJean Pihet  *
58ab596afSJean Pihet  * Copyright (C) 2010 Will Deacon, ARM Ltd.
68ab596afSJean Pihet  */
78ab596afSJean Pihet 
8a43783aeSArnaldo Carvalho de Melo #include <errno.h>
98ab596afSJean Pihet #include <stddef.h>
107a8ef4c4SArnaldo Carvalho de Melo #include <string.h>
118ab596afSJean Pihet #include <dwarf-regs.h>
123bb53c9fSHe Kuang #include <linux/ptrace.h> /* for struct user_pt_regs */
13aa8cc2f6SArnaldo Carvalho de Melo #include <linux/stringify.h>
148ab596afSJean Pihet 
158ab596afSJean Pihet struct pt_regs_dwarfnum {
168ab596afSJean Pihet 	const char *name;
178ab596afSJean Pihet 	unsigned int dwarfnum;
188ab596afSJean Pihet };
198ab596afSJean Pihet 
208ab596afSJean Pihet #define REG_DWARFNUM_NAME(r, num) {.name = r, .dwarfnum = num}
218ab596afSJean Pihet #define GPR_DWARFNUM_NAME(num) \
22aa8cc2f6SArnaldo Carvalho de Melo 	{.name = __stringify(%x##num), .dwarfnum = num}
238ab596afSJean Pihet #define REG_DWARFNUM_END {.name = NULL, .dwarfnum = 0}
243bb53c9fSHe Kuang #define DWARFNUM2OFFSET(index) \
253bb53c9fSHe Kuang 	(index * sizeof((struct user_pt_regs *)0)->regs[0])
268ab596afSJean Pihet 
278ab596afSJean Pihet /*
288ab596afSJean Pihet  * Reference:
298ab596afSJean Pihet  * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0057b/IHI0057B_aadwarf64.pdf
308ab596afSJean Pihet  */
318ab596afSJean Pihet static const struct pt_regs_dwarfnum regdwarfnum_table[] = {
328ab596afSJean Pihet 	GPR_DWARFNUM_NAME(0),
338ab596afSJean Pihet 	GPR_DWARFNUM_NAME(1),
348ab596afSJean Pihet 	GPR_DWARFNUM_NAME(2),
358ab596afSJean Pihet 	GPR_DWARFNUM_NAME(3),
368ab596afSJean Pihet 	GPR_DWARFNUM_NAME(4),
378ab596afSJean Pihet 	GPR_DWARFNUM_NAME(5),
388ab596afSJean Pihet 	GPR_DWARFNUM_NAME(6),
398ab596afSJean Pihet 	GPR_DWARFNUM_NAME(7),
408ab596afSJean Pihet 	GPR_DWARFNUM_NAME(8),
418ab596afSJean Pihet 	GPR_DWARFNUM_NAME(9),
428ab596afSJean Pihet 	GPR_DWARFNUM_NAME(10),
438ab596afSJean Pihet 	GPR_DWARFNUM_NAME(11),
448ab596afSJean Pihet 	GPR_DWARFNUM_NAME(12),
458ab596afSJean Pihet 	GPR_DWARFNUM_NAME(13),
468ab596afSJean Pihet 	GPR_DWARFNUM_NAME(14),
478ab596afSJean Pihet 	GPR_DWARFNUM_NAME(15),
488ab596afSJean Pihet 	GPR_DWARFNUM_NAME(16),
498ab596afSJean Pihet 	GPR_DWARFNUM_NAME(17),
508ab596afSJean Pihet 	GPR_DWARFNUM_NAME(18),
518ab596afSJean Pihet 	GPR_DWARFNUM_NAME(19),
528ab596afSJean Pihet 	GPR_DWARFNUM_NAME(20),
538ab596afSJean Pihet 	GPR_DWARFNUM_NAME(21),
548ab596afSJean Pihet 	GPR_DWARFNUM_NAME(22),
558ab596afSJean Pihet 	GPR_DWARFNUM_NAME(23),
568ab596afSJean Pihet 	GPR_DWARFNUM_NAME(24),
578ab596afSJean Pihet 	GPR_DWARFNUM_NAME(25),
588ab596afSJean Pihet 	GPR_DWARFNUM_NAME(26),
598ab596afSJean Pihet 	GPR_DWARFNUM_NAME(27),
608ab596afSJean Pihet 	GPR_DWARFNUM_NAME(28),
618ab596afSJean Pihet 	GPR_DWARFNUM_NAME(29),
628ab596afSJean Pihet 	REG_DWARFNUM_NAME("%lr", 30),
638ab596afSJean Pihet 	REG_DWARFNUM_NAME("%sp", 31),
648ab596afSJean Pihet 	REG_DWARFNUM_END,
658ab596afSJean Pihet };
668ab596afSJean Pihet 
678ab596afSJean Pihet /**
688ab596afSJean Pihet  * get_arch_regstr() - lookup register name from it's DWARF register number
698ab596afSJean Pihet  * @n:	the DWARF register number
708ab596afSJean Pihet  *
718ab596afSJean Pihet  * get_arch_regstr() returns the name of the register in struct
728ab596afSJean Pihet  * regdwarfnum_table from it's DWARF register number. If the register is not
738ab596afSJean Pihet  * found in the table, this returns NULL;
748ab596afSJean Pihet  */
get_arch_regstr(unsigned int n)758ab596afSJean Pihet const char *get_arch_regstr(unsigned int n)
768ab596afSJean Pihet {
778ab596afSJean Pihet 	const struct pt_regs_dwarfnum *roff;
788ab596afSJean Pihet 	for (roff = regdwarfnum_table; roff->name != NULL; roff++)
798ab596afSJean Pihet 		if (roff->dwarfnum == n)
808ab596afSJean Pihet 			return roff->name;
818ab596afSJean Pihet 	return NULL;
828ab596afSJean Pihet }
833bb53c9fSHe Kuang 
regs_query_register_offset(const char * name)843bb53c9fSHe Kuang int regs_query_register_offset(const char *name)
853bb53c9fSHe Kuang {
863bb53c9fSHe Kuang 	const struct pt_regs_dwarfnum *roff;
873bb53c9fSHe Kuang 
883bb53c9fSHe Kuang 	for (roff = regdwarfnum_table; roff->name != NULL; roff++)
893bb53c9fSHe Kuang 		if (!strcmp(roff->name, name))
903bb53c9fSHe Kuang 			return DWARFNUM2OFFSET(roff->dwarfnum);
913bb53c9fSHe Kuang 	return -EINVAL;
923bb53c9fSHe Kuang }
93