2 * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
3 * Extracted from probe-finder.c
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <errno.h> /* for EINVAL */
25 #include <string.h> /* for strcmp */
26 #include <linux/ptrace.h> /* for struct pt_regs */
27 #include <linux/kernel.h> /* for offsetof */
28 #include <dwarf-regs.h>
31 * See arch/x86/kernel/ptrace.c.
34 * - Since struct pt_regs is defined differently for user and kernel,
35 * but we want to use 'ax, bx' instead of 'rax, rbx' (which is struct
36 * field name of user's pt_regs), we make REG_OFFSET_NAME to accept
37 * both string name and reg field name.
39 * - Since accessing x86_32's pt_regs from x86_64 building is difficult
40 * and vise versa, we simply fill offset with -1, so
41 * get_arch_regstr() still works but regs_query_register_offset()
43 * The only inconvenience caused by it now is that we are not allowed
44 * to generate BPF prologue for a x86_64 kernel if perf is built for
45 * x86_32. This is really a rare usecase.
47 * - Order is different from kernel's ptrace.c for get_arch_regstr(). Use
48 * the order defined by dwarf.
51 struct pt_regs_offset
{
56 #define REG_OFFSET_END {.name = NULL, .offset = 0}
59 # define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
60 # define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = -1}
62 # define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = -1}
63 # define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
66 static const struct pt_regs_offset x86_32_regoffset_table
[] = {
67 REG_OFFSET_NAME_32("%ax", eax
),
68 REG_OFFSET_NAME_32("%cx", ecx
),
69 REG_OFFSET_NAME_32("%dx", edx
),
70 REG_OFFSET_NAME_32("%bx", ebx
),
71 REG_OFFSET_NAME_32("$stack", esp
), /* Stack address instead of %sp */
72 REG_OFFSET_NAME_32("%bp", ebp
),
73 REG_OFFSET_NAME_32("%si", esi
),
74 REG_OFFSET_NAME_32("%di", edi
),
78 static const struct pt_regs_offset x86_64_regoffset_table
[] = {
79 REG_OFFSET_NAME_64("%ax", rax
),
80 REG_OFFSET_NAME_64("%dx", rdx
),
81 REG_OFFSET_NAME_64("%cx", rcx
),
82 REG_OFFSET_NAME_64("%bx", rbx
),
83 REG_OFFSET_NAME_64("%si", rsi
),
84 REG_OFFSET_NAME_64("%di", rdi
),
85 REG_OFFSET_NAME_64("%bp", rbp
),
86 REG_OFFSET_NAME_64("%sp", rsp
),
87 REG_OFFSET_NAME_64("%r8", r8
),
88 REG_OFFSET_NAME_64("%r9", r9
),
89 REG_OFFSET_NAME_64("%r10", r10
),
90 REG_OFFSET_NAME_64("%r11", r11
),
91 REG_OFFSET_NAME_64("%r12", r12
),
92 REG_OFFSET_NAME_64("%r13", r13
),
93 REG_OFFSET_NAME_64("%r14", r14
),
94 REG_OFFSET_NAME_64("%r15", r15
),
98 /* TODO: switching by dwarf address size */
100 #define regoffset_table x86_64_regoffset_table
102 #define regoffset_table x86_32_regoffset_table
105 /* Minus 1 for the ending REG_OFFSET_END */
106 #define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
108 /* Return architecture dependent register string (for kprobe-tracer) */
109 const char *get_arch_regstr(unsigned int n
)
111 return (n
< ARCH_MAX_REGS
) ? regoffset_table
[n
].name
: NULL
;
114 /* Reuse code from arch/x86/kernel/ptrace.c */
116 * regs_query_register_offset() - query register offset from its name
117 * @name: the name of a register
119 * regs_query_register_offset() returns the offset of a register in struct
120 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
122 int regs_query_register_offset(const char *name
)
124 const struct pt_regs_offset
*roff
;
125 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
126 if (!strcmp(roff
->name
, name
))