1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <stddef.h>
24#include <errno.h>
25#include <string.h>
26#include <linux/ptrace.h>
27#include <linux/kernel.h>
28#include <dwarf-regs.h>
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51struct pt_regs_offset {
52 const char *name;
53 int offset;
54};
55
56#define REG_OFFSET_END {.name = NULL, .offset = 0}
57
58#ifdef __x86_64__
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}
61#else
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)}
64#endif
65
66
67#ifndef __x86_64__
68static const struct pt_regs_offset x86_32_regoffset_table[] = {
69 REG_OFFSET_NAME_32("%ax", eax),
70 REG_OFFSET_NAME_32("%cx", ecx),
71 REG_OFFSET_NAME_32("%dx", edx),
72 REG_OFFSET_NAME_32("%bx", ebx),
73 REG_OFFSET_NAME_32("$stack", esp),
74 REG_OFFSET_NAME_32("%bp", ebp),
75 REG_OFFSET_NAME_32("%si", esi),
76 REG_OFFSET_NAME_32("%di", edi),
77 REG_OFFSET_END,
78};
79
80#define regoffset_table x86_32_regoffset_table
81#else
82static const struct pt_regs_offset x86_64_regoffset_table[] = {
83 REG_OFFSET_NAME_64("%ax", rax),
84 REG_OFFSET_NAME_64("%dx", rdx),
85 REG_OFFSET_NAME_64("%cx", rcx),
86 REG_OFFSET_NAME_64("%bx", rbx),
87 REG_OFFSET_NAME_64("%si", rsi),
88 REG_OFFSET_NAME_64("%di", rdi),
89 REG_OFFSET_NAME_64("%bp", rbp),
90 REG_OFFSET_NAME_64("%sp", rsp),
91 REG_OFFSET_NAME_64("%r8", r8),
92 REG_OFFSET_NAME_64("%r9", r9),
93 REG_OFFSET_NAME_64("%r10", r10),
94 REG_OFFSET_NAME_64("%r11", r11),
95 REG_OFFSET_NAME_64("%r12", r12),
96 REG_OFFSET_NAME_64("%r13", r13),
97 REG_OFFSET_NAME_64("%r14", r14),
98 REG_OFFSET_NAME_64("%r15", r15),
99 REG_OFFSET_END,
100};
101
102#define regoffset_table x86_64_regoffset_table
103#endif
104
105
106#define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
107
108
109const char *get_arch_regstr(unsigned int n)
110{
111 return (n < ARCH_MAX_REGS) ? regoffset_table[n].name : NULL;
112}
113
114
115
116
117
118
119
120
121
122int regs_query_register_offset(const char *name)
123{
124 const struct pt_regs_offset *roff;
125 for (roff = regoffset_table; roff->name != NULL; roff++)
126 if (!strcmp(roff->name, name))
127 return roff->offset;
128 return -EINVAL;
129}
130