GNU Linux-libre 4.14.303-gnu1
[releases.git] / tools / perf / arch / powerpc / util / dwarf-regs.c
1 /*
2  * Mapping of DWARF debug register numbers into register names.
3  *
4  * Copyright (C) 2010 Ian Munsie, IBM Corporation.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <stddef.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <dwarf-regs.h>
16 #include <linux/ptrace.h>
17 #include <linux/kernel.h>
18 #include <linux/stringify.h>
19 #include "util.h"
20
21 struct pt_regs_dwarfnum {
22         const char *name;
23         unsigned int dwarfnum;
24         unsigned int ptregs_offset;
25 };
26
27 #define REG_DWARFNUM_NAME(r, num)                                       \
28                 {.name = __stringify(%)__stringify(r), .dwarfnum = num,                 \
29                 .ptregs_offset = offsetof(struct pt_regs, r)}
30 #define GPR_DWARFNUM_NAME(num)                                          \
31                 {.name = __stringify(%gpr##num), .dwarfnum = num,               \
32                 .ptregs_offset = offsetof(struct pt_regs, gpr[num])}
33 #define REG_DWARFNUM_END {.name = NULL, .dwarfnum = 0, .ptregs_offset = 0}
34
35 /*
36  * Reference:
37  * http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
38  */
39 static const struct pt_regs_dwarfnum regdwarfnum_table[] = {
40         GPR_DWARFNUM_NAME(0),
41         GPR_DWARFNUM_NAME(1),
42         GPR_DWARFNUM_NAME(2),
43         GPR_DWARFNUM_NAME(3),
44         GPR_DWARFNUM_NAME(4),
45         GPR_DWARFNUM_NAME(5),
46         GPR_DWARFNUM_NAME(6),
47         GPR_DWARFNUM_NAME(7),
48         GPR_DWARFNUM_NAME(8),
49         GPR_DWARFNUM_NAME(9),
50         GPR_DWARFNUM_NAME(10),
51         GPR_DWARFNUM_NAME(11),
52         GPR_DWARFNUM_NAME(12),
53         GPR_DWARFNUM_NAME(13),
54         GPR_DWARFNUM_NAME(14),
55         GPR_DWARFNUM_NAME(15),
56         GPR_DWARFNUM_NAME(16),
57         GPR_DWARFNUM_NAME(17),
58         GPR_DWARFNUM_NAME(18),
59         GPR_DWARFNUM_NAME(19),
60         GPR_DWARFNUM_NAME(20),
61         GPR_DWARFNUM_NAME(21),
62         GPR_DWARFNUM_NAME(22),
63         GPR_DWARFNUM_NAME(23),
64         GPR_DWARFNUM_NAME(24),
65         GPR_DWARFNUM_NAME(25),
66         GPR_DWARFNUM_NAME(26),
67         GPR_DWARFNUM_NAME(27),
68         GPR_DWARFNUM_NAME(28),
69         GPR_DWARFNUM_NAME(29),
70         GPR_DWARFNUM_NAME(30),
71         GPR_DWARFNUM_NAME(31),
72         REG_DWARFNUM_NAME(msr,   66),
73         REG_DWARFNUM_NAME(ctr,   109),
74         REG_DWARFNUM_NAME(link,  108),
75         REG_DWARFNUM_NAME(xer,   101),
76         REG_DWARFNUM_NAME(dar,   119),
77         REG_DWARFNUM_NAME(dsisr, 118),
78         REG_DWARFNUM_END,
79 };
80
81 /**
82  * get_arch_regstr() - lookup register name from it's DWARF register number
83  * @n:  the DWARF register number
84  *
85  * get_arch_regstr() returns the name of the register in struct
86  * regdwarfnum_table from it's DWARF register number. If the register is not
87  * found in the table, this returns NULL;
88  */
89 const char *get_arch_regstr(unsigned int n)
90 {
91         const struct pt_regs_dwarfnum *roff;
92         for (roff = regdwarfnum_table; roff->name != NULL; roff++)
93                 if (roff->dwarfnum == n)
94                         return roff->name;
95         return NULL;
96 }
97
98 int regs_query_register_offset(const char *name)
99 {
100         const struct pt_regs_dwarfnum *roff;
101         for (roff = regdwarfnum_table; roff->name != NULL; roff++)
102                 if (!strcmp(roff->name, name))
103                         return roff->ptregs_offset;
104         return -EINVAL;
105 }