forked from WuBingzheng/memleax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machines.h
151 lines (142 loc) · 3.62 KB
/
machines.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef MLX_MACHINES_H
#define MLX_MACHINES_H
#include "ptrace_utils.h"
/* We use the names in Linux, and define the aliases for FreeBSD. */
#ifdef MLX_FREEBSD
#define esp r_esp
#define eax r_eax
#define esi r_esi
#define edi r_edi
#define eip r_eip
#define rsp r_rsp
#define rax r_rax
#define rsi r_rsi
#define rdi r_rdi
#define rip r_rip
#endif
#if defined(MLX_X86)
static inline uintptr_t call_return_address(pid_t pid, registers_info_t *regs)
{
return ptrace_get_data(pid, regs->esp);
}
static inline uintptr_t call_return_value(registers_info_t *regs)
{
return regs->eax;
}
static inline uintptr_t call_arg1(pid_t pid, registers_info_t *regs)
{
return ptrace_get_data(pid, regs->esp + 4);
}
static inline uintptr_t call_arg2(pid_t pid, registers_info_t *regs)
{
return ptrace_get_data(pid, regs->esp + 8);
}
static inline uintptr_t pc_unwind(pid_t pid, registers_info_t *regs)
{
regs->eip--;
ptrace_set_regs(pid, regs);
return regs->eip;
}
static inline void set_breakpoint(pid_t pid, uintptr_t address, uintptr_t code)
{
ptrace_set_data(pid, address, (code & 0xFFFFFF00U) | 0xCC);
}
static inline int is_breakpoint(pid_t pid, uintptr_t address)
{
return (ptrace_get_data(pid, address) & 0xFF) == 0xCC;
}
#elif defined(MLX_X86_64)
static inline uintptr_t call_return_address(pid_t pid, registers_info_t *regs)
{
return ptrace_get_data(pid, regs->rsp);
}
static inline uintptr_t call_return_value(registers_info_t *regs)
{
return regs->rax;
}
static inline uintptr_t call_arg1(pid_t pid, registers_info_t *regs)
{
return regs->rdi;
}
static inline uintptr_t call_arg2(pid_t pid, registers_info_t *regs)
{
return regs->rsi;
}
static inline uintptr_t pc_unwind(pid_t pid, registers_info_t *regs)
{
regs->rip--;
ptrace_set_regs(pid, regs);
return regs->rip;
}
static inline void set_breakpoint(pid_t pid, uintptr_t address, uintptr_t code)
{
#ifdef MLX_LINUX
ptrace_set_data(pid, address, (code & 0xFFFFFFFFFFFFFF00UL) | 0xCC);
#else // MLX_FREEBSD
ptrace_set_data(pid, address, (code & 0xFFFFFF00U) | 0xCC);
#endif
}
static inline int is_breakpoint(pid_t pid, uintptr_t address)
{
return (ptrace_get_data(pid, address) & 0xFF) == 0xCC;
}
#elif defined(MLX_ARMv7)
static inline uintptr_t call_return_address(pid_t pid, registers_info_t *regs)
{
return regs->uregs[14];
}
static inline uintptr_t call_return_value(registers_info_t *regs)
{
return regs->uregs[0];
}
static inline uintptr_t call_arg1(pid_t pid, registers_info_t *regs)
{
return regs->uregs[0];
}
static inline uintptr_t call_arg2(pid_t pid, registers_info_t *regs)
{
return regs->uregs[1];
}
static inline uintptr_t pc_unwind(pid_t pid, registers_info_t *regs)
{
return regs->uregs[15];
}
static inline void set_breakpoint(pid_t pid, uintptr_t address, uintptr_t code)
{
ptrace_set_data(pid, address, 0xE7F001F0);
}
static inline int is_breakpoint(pid_t pid, uintptr_t address)
{
return ptrace_get_data(pid, address) == 0xE7F001F0;
}
#elif defined(MLX_AARCH64)
static inline uintptr_t call_return_address(pid_t pid, registers_info_t *regs)
{
return regs->regs[30];
}
static inline uintptr_t call_return_value(registers_info_t *regs)
{
return regs->regs[0];
}
static inline uintptr_t call_arg1(pid_t pid, registers_info_t *regs)
{
return regs->regs[0];
}
static inline uintptr_t call_arg2(pid_t pid, registers_info_t *regs)
{
return regs->regs[1];
}
static inline uintptr_t pc_unwind(pid_t pid, registers_info_t *regs)
{
return regs->pc;
}
static inline void set_breakpoint(pid_t pid, uintptr_t address, uintptr_t code)
{
ptrace_set_data(pid, address, 0xd4200000);
}
static inline int is_breakpoint(pid_t pid, uintptr_t address)
{
return ptrace_get_data(pid, address) == 0xd4200000;
}
#endif
#endif