-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathptracer.cpp
181 lines (170 loc) · 3.89 KB
/
ptracer.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "ptracer.h"
#include "log.h"
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <stdint.h>
ptracer::ptracer (pid_t tid) : tid_ (tid), attached_ (false) {}
ptracer::~ptracer () { detach (); }
bool
ptracer::attach ()
{
if (attached_)
return true;
int ret = ptrace (PTRACE_ATTACH, tid_, 0, 0);
if (ret == -1)
{
LOGE ("ptracer::attach:fails to attach : %s\n", strerror (errno));
return false;
}
int status;
ret = waitpid (tid_, &status, 0);
if (ret == -1)
{
LOGE ("ptracer::attach:fails to wait: %s\n", strerror (errno));
ptrace (PTRACE_DETACH, tid_, NULL, NULL);
return false;
}
attached_ = true;
return true;
}
void
ptracer::detach ()
{
if (attached_)
ptrace (PTRACE_DETACH, tid_, NULL, NULL);
attached_ = false;
}
void
ptracer::continue_and_wait ()
{
if (!attached_)
return;
ptrace (PTRACE_CONT, tid_, NULL, NULL);
int status;
int ret;
ret = waitpid (tid_, &status, 0);
if (ret == -1)
{
LOGE ("ptracer::continue_and_wait:fails to wait: %s\n",
strerror (errno));
}
}
template <class T>
inline static bool
check_align (T t)
{
intptr_t i = (intptr_t)(t);
if (i & (sizeof (intptr_t) - 1))
return false;
return true;
}
bool
ptracer::read_memory (void *buffer, size_t s, intptr_t dest)
{
if (!attached_)
{
LOGE ("ptracer::read_memory: not attached %d.\n", tid_);
return false;
}
if (!check_align (buffer))
{
LOGE ("ptracer::read_memory: buffer not align to word size of the "
"machine.\n");
return false;
}
if (!check_align (s))
{
LOGE (
"ptracer::read_memory: s not align to word size of the machine.\n");
return false;
}
size_t count = s / sizeof (intptr_t);
intptr_t *_buffer = static_cast<intptr_t *> (buffer);
for (size_t i = 0; i < count; ++i, dest += sizeof (intptr_t))
{
long v
= ptrace (PTRACE_PEEKDATA, tid_, reinterpret_cast<void *> (dest), 0);
if (errno)
{
LOGE ("ptracer::read_memory: peek data fail %s.\n",
strerror (errno));
return false;
}
_buffer[i] = v;
}
return true;
}
bool
ptracer::write_memory (void *buffer, size_t s, intptr_t dest)
{
if (!attached_)
return false;
if (!check_align (buffer))
{
LOGE ("ptracer::read_memory: buffer not align to word size of the "
"machine.\n");
return false;
}
if (!check_align (s))
{
LOGE (
"ptracer::read_memory: s not align to word size of the machine.\n");
return false;
}
size_t count = s / sizeof (intptr_t);
intptr_t *_buffer = static_cast<intptr_t *> (buffer);
for (size_t i = 0; i < count; ++i, dest += sizeof (intptr_t))
{
long r = ptrace (PTRACE_POKEDATA, tid_, reinterpret_cast<void *> (dest),
reinterpret_cast<void *> (_buffer[i]));
if (r == -1)
{
LOGE ("ptracer::read_memory: poke data fail %s.\n",
strerror (errno));
return false;
}
}
return true;
}
bool
ptracer::get_regs (user_regs_struct *regs)
{
if (!attached_)
return false;
int ret = ptrace (PTRACE_GETREGS, tid_, NULL, regs);
if (ret == -1)
{
LOGE ("ptracer::get_regs: %s\n", strerror (errno));
return false;
}
return true;
}
bool
ptracer::set_regs (user_regs_struct *regs)
{
if (!attached_)
return false;
int ret = ptrace (PTRACE_SETREGS, tid_, NULL, regs);
if (ret == -1)
{
LOGE ("ptracer::set_regs : %s\n", strerror (errno));
return false;
}
return true;
}
bool
ptracer::get_siginfo (siginfo_t *info)
{
if (!attached_)
return false;
int ret = ptrace (PTRACE_GETSIGINFO, tid_, NULL, info);
if (ret == -1)
{
LOGE ("ptracer::get_siginfo : %s\n", strerror (errno));
return false;
}
return true;
}