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