Debian Codegraph @

ptrace/binding/func.py

raw
1 from os import strerror
2 from ctypes import addressof, c_int, get_errno, set_errno, sizeof
3 from ptrace import PtraceError
4 from ptrace.ctypes_tools import formatAddress
5 from ptrace.os_tools import RUNNING_LINUX, RUNNING_BSD, RUNNING_OPENBSD
6 from ptrace.cpu_info import CPU_64BITS, CPU_WORD_SIZE, CPU_POWERPC, CPU_AARCH64, CPU_RISCV
7
8 if RUNNING_OPENBSD:
9 from ptrace.binding.openbsd_struct import (
10 reg as ptrace_registers_t,
11 fpreg as user_fpregs_struct)
12
13 elif RUNNING_BSD:
14 from ptrace.binding.freebsd_struct import (
15 reg as ptrace_registers_t)
16
17 elif RUNNING_LINUX:
18 from ptrace.binding.linux_struct import (
19 user_regs_struct as ptrace_registers_t,
20 user_fpregs_struct, siginfo, iovec_struct)
21 if not CPU_64BITS:
22 from ptrace.binding.linux_struct import user_fpxregs_struct
23 else:
24 raise NotImplementedError("Unknown OS!")
25 REGISTER_NAMES = tuple(name for name, type in ptrace_registers_t._fields_)
26
27 HAS_PTRACE_SINGLESTEP = True
28 HAS_PTRACE_EVENTS = False
29 HAS_PTRACE_IO = False
30 HAS_PTRACE_SIGINFO = False
31 HAS_PTRACE_GETREGS = False
32 HAS_PTRACE_GETREGSET = False
33 HAS_PTRACE_SETREGS = False
34 HAS_PTRACE_SETREGSET = False
35
36 # Special flags that are required to wait for cloned processes (threads)
37 # See wait(2)
38 THREAD_TRACE_FLAGS = 0x00000000
39
40 pid_t = c_int
41
42 # PTRACE_xxx constants from /usr/include/sys/ptrace.h
43 # (Linux 2.6.21 Ubuntu Feisty i386)
44 PTRACE_TRACEME = 0
45 PTRACE_PEEKTEXT = 1
46 PTRACE_PEEKDATA = 2
47 PTRACE_PEEKUSER = 3
48 PTRACE_POKETEXT = 4
49 PTRACE_POKEDATA = 5
50 PTRACE_POKEUSER = 6
51 PTRACE_CONT = 7
52 PTRACE_KILL = 8
53 if HAS_PTRACE_SINGLESTEP:
54 PTRACE_SINGLESTEP = 9
55
56 if RUNNING_OPENBSD:
57 # OpenBSD 4.2 i386
58 PTRACE_ATTACH = 9
59 PTRACE_DETACH = 10
60 HAS_PTRACE_GETREGS = True
61 PTRACE_GETREGS = 33
62 PTRACE_SETREGS = 34
63 PTRACE_GETFPREGS = 35
64 PTRACE_SETFPREGS = 36
65 HAS_PTRACE_IO = True
66 PTRACE_IO = 11
67 HAS_PTRACE_SINGLESTEP = True
68 PTRACE_SINGLESTEP = 32 # PT_STEP
69 # HAS_PTRACE_EVENTS = True
70 # PTRACE_SETOPTIONS = 12 # PT_SET_EVENT_MASK
71 # PTRACE_GETEVENTMSG = 14 # PT_GET_PROCESS_STATE
72 elif RUNNING_BSD:
73 # FreeBSD 7.0RC1 i386
74 PTRACE_ATTACH = 10
75 PTRACE_DETACH = 11
76 PTRACE_SYSCALL = 22
77 if not CPU_POWERPC:
78 HAS_PTRACE_GETREGS = True
79 PTRACE_GETREGS = 33
80 PTRACE_SETREGS = 34
81 HAS_PTRACE_IO = True
82 PTRACE_IO = 12
83 else:
84 # Linux
85 if not (CPU_AARCH64 or CPU_RISCV):
86 HAS_PTRACE_GETREGS = True
87 HAS_PTRACE_SETREGS = True
88 PTRACE_GETREGS = 12
89 PTRACE_SETREGS = 13
90
91 HAS_PTRACE_GETREGSET = True
92 HAS_PTRACE_SETREGSET = True
93 PTRACE_GETREGSET = 0x4204
94 PTRACE_SETREGSET = 0x4205
95 NT_PRSTATUS = 1
96
97 PTRACE_ATTACH = 16
98 PTRACE_DETACH = 17
99 PTRACE_SYSCALL = 24
100 if RUNNING_LINUX:
101 PTRACE_GETFPREGS = 14
102 PTRACE_SETFPREGS = 15
103 if not CPU_64BITS:
104 PTRACE_GETFPXREGS = 18
105 PTRACE_SETFPXREGS = 19
106 HAS_PTRACE_SIGINFO = True
107 PTRACE_GETSIGINFO = 0x4202
108 PTRACE_SETSIGINFO = 0x4203
109
110 HAS_PTRACE_EVENTS = True
111 PTRACE_SETOPTIONS = 0x4200
112 PTRACE_GETEVENTMSG = 0x4201
113
114 # Linux introduces the __WALL flag for wait
115 THREAD_TRACE_FLAGS = 0x40000000
116
117 PTRACE_O_TRACESYSGOOD = 0x00000001
118 PTRACE_O_TRACEFORK = 0x00000002
119 PTRACE_O_TRACEVFORK = 0x00000004
120 PTRACE_O_TRACECLONE = 0x00000008
121 PTRACE_O_TRACEEXEC = 0x00000010
122 PTRACE_O_TRACEVFORKDONE = 0x00000020
123 PTRACE_O_TRACEEXIT = 0x00000040
124
125 # Wait extended result codes for the above trace options
126 PTRACE_EVENT_FORK = 1
127 PTRACE_EVENT_VFORK = 2
128 PTRACE_EVENT_CLONE = 3
129 PTRACE_EVENT_EXEC = 4
130 PTRACE_EVENT_VFORK_DONE = 5
131 PTRACE_EVENT_EXIT = 6
132
133 try:
134 from cptrace import ptrace as _ptrace
135 HAS_CPTRACE = True
136 except ImportError:
137 HAS_CPTRACE = False
138 from ctypes import c_long, c_ulong
139 from ptrace.ctypes_libc import libc
140
141 # Load ptrace() function from the system C library
142 _ptrace = libc.ptrace
143 _ptrace.argtypes = (c_ulong, c_ulong, c_ulong, c_ulong)
144 _ptrace.restype = c_ulong
145
146
147 def ptrace(command, pid=0, arg1=0, arg2=0, check_errno=False):
148 if HAS_CPTRACE:
149 try:
150 set_errno(0)
151 result = _ptrace(command, pid, arg1, arg2, check_errno)
152 except ValueError as errobj:
153 message = str(errobj)
154 errno = get_errno()
155 raise PtraceError(message, errno=errno, pid=pid)
156 else:
157 result = _ptrace(command, pid, arg1, arg2)
158 result_signed = c_long(result).value
159 if result_signed == -1:
160 errno = get_errno()
161 # peek operations may returns -1 with errno=0:
162 # it's not an error. For other operations, -1
163 # is always an error
164 if not (check_errno) or errno:
165 message = "ptrace(cmd=%s, pid=%s, %r, %r) error #%s: %s" % (
166 command, pid, arg1, arg2,
167 errno, strerror(errno))
168 raise PtraceError(message, errno=errno, pid=pid)
169 return result
170
171
172 def ptrace_traceme():
173 ptrace(PTRACE_TRACEME)
174
175
176 def ptrace_attach(pid):
177 ptrace(PTRACE_ATTACH, pid)
178
179
180 def ptrace_detach(pid, signal=0):
181 ptrace(PTRACE_DETACH, pid, 0, signal)
182
183
184 def _peek(command, pid, address):
185 if address % CPU_WORD_SIZE:
186 raise PtraceError(
187 "ptrace can't read a word from an unaligned address (%s)!"
188 % formatAddress(address), pid=pid)
189 return ptrace(command, pid, address, check_errno=True)
190
191
192 def _poke(command, pid, address, word):
193 if address % CPU_WORD_SIZE:
194 raise PtraceError(
195 "ptrace can't write a word to an unaligned address (%s)!"
196 % formatAddress(address), pid=pid)
197 ptrace(command, pid, address, word)
198
199
200 def ptrace_peektext(pid, address):
201 return _peek(PTRACE_PEEKTEXT, pid, address)
202
203
204 def ptrace_peekdata(pid, address):
205 return _peek(PTRACE_PEEKDATA, pid, address)
206
207
208 def ptrace_peekuser(pid, address):
209 return _peek(PTRACE_PEEKUSER, pid, address)
210
211
212 def ptrace_poketext(pid, address, word):
213 _poke(PTRACE_POKETEXT, pid, address, word)
214
215
216 def ptrace_pokedata(pid, address, word):
217 _poke(PTRACE_POKEDATA, pid, address, word)
218
219
220 def ptrace_pokeuser(pid, address, word):
221 _poke(PTRACE_POKEUSER, pid, address, word)
222
223
224 def ptrace_kill(pid):
225 ptrace(PTRACE_KILL, pid)
226
227
228 if HAS_PTRACE_EVENTS:
229 def WPTRACEEVENT(status):
230 return status >> 16
231
232 def ptrace_setoptions(pid, options):
233 ptrace(PTRACE_SETOPTIONS, pid, 0, options)
234
235 def ptrace_geteventmsg(pid):
236 new_pid = pid_t()
237 ptrace(PTRACE_GETEVENTMSG, pid, 0, addressof(new_pid))
238 return new_pid.value
239
240 if RUNNING_LINUX:
241 def ptrace_syscall(pid, signum=0):
242 ptrace(PTRACE_SYSCALL, pid, 0, signum)
243
244 def ptrace_cont(pid, signum=0):
245 ptrace(PTRACE_CONT, pid, 0, signum)
246
247 def ptrace_getsiginfo(pid):
248 info = siginfo()
249 ptrace(PTRACE_GETSIGINFO, pid, 0, addressof(info))
250 return info
251
252 def ptrace_setsiginfo(pid, info):
253 ptrace(PTRACE_SETSIGINFO, pid, 0, addressof(info))
254
255 def ptrace_getfpregs(pid):
256 fpregs = user_fpregs_struct()
257 ptrace(PTRACE_GETFPREGS, pid, 0, addressof(fpregs))
258 return fpregs
259
260 def ptrace_setfpregs(pid, fpregs):
261 ptrace(PTRACE_SETFPREGS, pid, 0, addressof(fpregs))
262
263 if not CPU_64BITS:
264 def ptrace_getfpxregs(pid):
265 fpxregs = user_fpxregs_struct()
266 ptrace(PTRACE_GETFPXREGS, pid, 0, addressof(fpxregs))
267 return fpxregs
268
269 def ptrace_setfpxregs(pid, fpxregs):
270 ptrace(PTRACE_SETFPXREGS, pid, 0, addressof(fpxregs))
271
272 if HAS_PTRACE_GETREGS:
273 def ptrace_getregs(pid):
274 regs = ptrace_registers_t()
275 ptrace(PTRACE_GETREGS, pid, 0, addressof(regs))
276 return regs
277
278 elif HAS_PTRACE_GETREGSET:
279 def ptrace_getregs(pid):
280 regs = ptrace_registers_t()
281 iov = iovec_struct()
282 setattr(iov, "buf", addressof(regs))
283 setattr(iov, "len", sizeof(regs))
284 ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, addressof(iov))
285 return regs
286
287 if HAS_PTRACE_SETREGS:
288 def ptrace_setregs(pid, regs):
289 ptrace(PTRACE_SETREGS, pid, 0, addressof(regs))
290
291 elif HAS_PTRACE_SETREGSET:
292 def ptrace_setregs(pid, regs):
293 iov = iovec_struct()
294 setattr(iov, "buf", addressof(regs))
295 setattr(iov, "len", sizeof(regs))
296 ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, addressof(iov))
297
298 if HAS_PTRACE_SINGLESTEP:
299 def ptrace_singlestep(pid):
300 ptrace(PTRACE_SINGLESTEP, pid)
301
302 else:
303 def ptrace_syscall(pid, signum=0):
304 ptrace(PTRACE_SYSCALL, pid, 1, signum)
305
306 def ptrace_cont(pid, signum=0):
307 ptrace(PTRACE_CONT, pid, 1, signum)
308
309 if HAS_PTRACE_GETREGS:
310 def ptrace_getregs(pid):
311 regs = ptrace_registers_t()
312 ptrace(PTRACE_GETREGS, pid, addressof(regs))
313 return regs
314
315 def ptrace_setregs(pid, regs):
316 ptrace(PTRACE_SETREGS, pid, addressof(regs))
317
318 if HAS_PTRACE_SINGLESTEP:
319 def ptrace_singlestep(pid):
320 ptrace(PTRACE_SINGLESTEP, pid, 1)
321
322 if HAS_PTRACE_IO:
323 def ptrace_io(pid, io_desc):
324 ptrace(PTRACE_IO, pid, addressof(io_desc))