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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
from os import strerror
from ctypes import addressof, c_int, get_errno, set_errno, sizeof
from ptrace import PtraceError
from ptrace.ctypes_tools import formatAddress
from ptrace.os_tools import RUNNING_LINUX, RUNNING_BSD, RUNNING_OPENBSD
from ptrace.cpu_info import CPU_64BITS, CPU_WORD_SIZE, CPU_POWERPC, CPU_AARCH64, CPU_RISCV
if RUNNING_OPENBSD:
from ptrace.binding.openbsd_struct import (
reg as ptrace_registers_t,
fpreg as user_fpregs_struct)
elif RUNNING_BSD:
from ptrace.binding.freebsd_struct import (
reg as ptrace_registers_t)
elif RUNNING_LINUX:
from ptrace.binding.linux_struct import (
user_regs_struct as ptrace_registers_t,
user_fpregs_struct, siginfo, iovec_struct)
if not CPU_64BITS:
from ptrace.binding.linux_struct import user_fpxregs_struct
else:
raise NotImplementedError("Unknown OS!")
REGISTER_NAMES = tuple(name for name, type in ptrace_registers_t._fields_)
HAS_PTRACE_SINGLESTEP = True
HAS_PTRACE_EVENTS = False
HAS_PTRACE_IO = False
HAS_PTRACE_SIGINFO = False
HAS_PTRACE_GETREGS = False
HAS_PTRACE_GETREGSET = False
HAS_PTRACE_SETREGS = False
HAS_PTRACE_SETREGSET = False
# Special flags that are required to wait for cloned processes (threads)
# See wait(2)
THREAD_TRACE_FLAGS = 0x00000000
pid_t = c_int
# PTRACE_xxx constants from /usr/include/sys/ptrace.h
# (Linux 2.6.21 Ubuntu Feisty i386)
PTRACE_TRACEME = 0
PTRACE_PEEKTEXT = 1
PTRACE_PEEKDATA = 2
PTRACE_PEEKUSER = 3
PTRACE_POKETEXT = 4
PTRACE_POKEDATA = 5
PTRACE_POKEUSER = 6
PTRACE_CONT = 7
PTRACE_KILL = 8
if HAS_PTRACE_SINGLESTEP:
PTRACE_SINGLESTEP = 9
if RUNNING_OPENBSD:
# OpenBSD 4.2 i386
PTRACE_ATTACH = 9
PTRACE_DETACH = 10
HAS_PTRACE_GETREGS = True
PTRACE_GETREGS = 33
PTRACE_SETREGS = 34
PTRACE_GETFPREGS = 35
PTRACE_SETFPREGS = 36
HAS_PTRACE_IO = True
PTRACE_IO = 11
HAS_PTRACE_SINGLESTEP = True
PTRACE_SINGLESTEP = 32 # PT_STEP
# HAS_PTRACE_EVENTS = True
# PTRACE_SETOPTIONS = 12 # PT_SET_EVENT_MASK
# PTRACE_GETEVENTMSG = 14 # PT_GET_PROCESS_STATE
elif RUNNING_BSD:
# FreeBSD 7.0RC1 i386
PTRACE_ATTACH = 10
PTRACE_DETACH = 11
PTRACE_SYSCALL = 22
if not CPU_POWERPC:
HAS_PTRACE_GETREGS = True
PTRACE_GETREGS = 33
PTRACE_SETREGS = 34
HAS_PTRACE_IO = True
PTRACE_IO = 12
else:
# Linux
if not (CPU_AARCH64 or CPU_RISCV):
HAS_PTRACE_GETREGS = True
HAS_PTRACE_SETREGS = True
PTRACE_GETREGS = 12
PTRACE_SETREGS = 13
HAS_PTRACE_GETREGSET = True
HAS_PTRACE_SETREGSET = True
PTRACE_GETREGSET = 0x4204
PTRACE_SETREGSET = 0x4205
NT_PRSTATUS = 1
PTRACE_ATTACH = 16
PTRACE_DETACH = 17
PTRACE_SYSCALL = 24
if RUNNING_LINUX:
PTRACE_GETFPREGS = 14
PTRACE_SETFPREGS = 15
if not CPU_64BITS:
PTRACE_GETFPXREGS = 18
PTRACE_SETFPXREGS = 19
HAS_PTRACE_SIGINFO = True
PTRACE_GETSIGINFO = 0x4202
PTRACE_SETSIGINFO = 0x4203
HAS_PTRACE_EVENTS = True
PTRACE_SETOPTIONS = 0x4200
PTRACE_GETEVENTMSG = 0x4201
# Linux introduces the __WALL flag for wait
THREAD_TRACE_FLAGS = 0x40000000
PTRACE_O_TRACESYSGOOD = 0x00000001
PTRACE_O_TRACEFORK = 0x00000002
PTRACE_O_TRACEVFORK = 0x00000004
PTRACE_O_TRACECLONE = 0x00000008
PTRACE_O_TRACEEXEC = 0x00000010
PTRACE_O_TRACEVFORKDONE = 0x00000020
PTRACE_O_TRACEEXIT = 0x00000040
# Wait extended result codes for the above trace options
PTRACE_EVENT_FORK = 1
PTRACE_EVENT_VFORK = 2
PTRACE_EVENT_CLONE = 3
PTRACE_EVENT_EXEC = 4
PTRACE_EVENT_VFORK_DONE = 5
PTRACE_EVENT_EXIT = 6
try:
from cptrace import ptrace as _ptrace
HAS_CPTRACE = True
except ImportError:
HAS_CPTRACE = False
from ctypes import c_long, c_ulong
from ptrace.ctypes_libc import libc
# Load ptrace() function from the system C library
_ptrace = libc.ptrace
_ptrace.argtypes = (c_ulong, c_ulong, c_ulong, c_ulong)
_ptrace.restype = c_ulong
def ptrace(command, pid=0, arg1=0, arg2=0, check_errno=False):
if HAS_CPTRACE:
try:
set_errno(0)
result = _ptrace(command, pid, arg1, arg2, check_errno)
except ValueError as errobj:
message = str(errobj)
errno = get_errno()
raise PtraceError(message, errno=errno, pid=pid)
else:
result = _ptrace(command, pid, arg1, arg2)
result_signed = c_long(result).value
if result_signed == -1:
errno = get_errno()
# peek operations may returns -1 with errno=0:
# it's not an error. For other operations, -1
# is always an error
if not (check_errno) or errno:
message = "ptrace(cmd=%s, pid=%s, %r, %r) error #%s: %s" % (
command, pid, arg1, arg2,
errno, strerror(errno))
raise PtraceError(message, errno=errno, pid=pid)
return result
def ptrace_traceme():
ptrace(PTRACE_TRACEME)
def ptrace_attach(pid):
ptrace(PTRACE_ATTACH, pid)
def ptrace_detach(pid, signal=0):
ptrace(PTRACE_DETACH, pid, 0, signal)
def _peek(command, pid, address):
if address % CPU_WORD_SIZE:
raise PtraceError(
"ptrace can't read a word from an unaligned address (%s)!"
% formatAddress(address), pid=pid)
return ptrace(command, pid, address, check_errno=True)
def _poke(command, pid, address, word):
if address % CPU_WORD_SIZE:
raise PtraceError(
"ptrace can't write a word to an unaligned address (%s)!"
% formatAddress(address), pid=pid)
ptrace(command, pid, address, word)
def ptrace_peektext(pid, address):
return _peek(PTRACE_PEEKTEXT, pid, address)
def ptrace_peekdata(pid, address):
return _peek(PTRACE_PEEKDATA, pid, address)
def ptrace_peekuser(pid, address):
return _peek(PTRACE_PEEKUSER, pid, address)
def ptrace_poketext(pid, address, word):
_poke(PTRACE_POKETEXT, pid, address, word)
def ptrace_pokedata(pid, address, word):
_poke(PTRACE_POKEDATA, pid, address, word)
def ptrace_pokeuser(pid, address, word):
_poke(PTRACE_POKEUSER, pid, address, word)
def ptrace_kill(pid):
ptrace(PTRACE_KILL, pid)
if HAS_PTRACE_EVENTS:
def WPTRACEEVENT(status):
return status >> 16
def ptrace_setoptions(pid, options):
ptrace(PTRACE_SETOPTIONS, pid, 0, options)
def ptrace_geteventmsg(pid):
new_pid = pid_t()
ptrace(PTRACE_GETEVENTMSG, pid, 0, addressof(new_pid))
return new_pid.value
if RUNNING_LINUX:
def ptrace_syscall(pid, signum=0):
ptrace(PTRACE_SYSCALL, pid, 0, signum)
def ptrace_cont(pid, signum=0):
ptrace(PTRACE_CONT, pid, 0, signum)
def ptrace_getsiginfo(pid):
info = siginfo()
ptrace(PTRACE_GETSIGINFO, pid, 0, addressof(info))
return info
def ptrace_setsiginfo(pid, info):
ptrace(PTRACE_SETSIGINFO, pid, 0, addressof(info))
def ptrace_getfpregs(pid):
fpregs = user_fpregs_struct()
ptrace(PTRACE_GETFPREGS, pid, 0, addressof(fpregs))
return fpregs
def ptrace_setfpregs(pid, fpregs):
ptrace(PTRACE_SETFPREGS, pid, 0, addressof(fpregs))
if not CPU_64BITS:
def ptrace_getfpxregs(pid):
fpxregs = user_fpxregs_struct()
ptrace(PTRACE_GETFPXREGS, pid, 0, addressof(fpxregs))
return fpxregs
def ptrace_setfpxregs(pid, fpxregs):
ptrace(PTRACE_SETFPXREGS, pid, 0, addressof(fpxregs))
if HAS_PTRACE_GETREGS:
def ptrace_getregs(pid):
regs = ptrace_registers_t()
ptrace(PTRACE_GETREGS, pid, 0, addressof(regs))
return regs
elif HAS_PTRACE_GETREGSET:
def ptrace_getregs(pid):
regs = ptrace_registers_t()
iov = iovec_struct()
setattr(iov, "buf", addressof(regs))
setattr(iov, "len", sizeof(regs))
ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, addressof(iov))
return regs
if HAS_PTRACE_SETREGS:
def ptrace_setregs(pid, regs):
ptrace(PTRACE_SETREGS, pid, 0, addressof(regs))
elif HAS_PTRACE_SETREGSET:
def ptrace_setregs(pid, regs):
iov = iovec_struct()
setattr(iov, "buf", addressof(regs))
setattr(iov, "len", sizeof(regs))
ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, addressof(iov))
if HAS_PTRACE_SINGLESTEP:
def ptrace_singlestep(pid):
ptrace(PTRACE_SINGLESTEP, pid)
else:
def ptrace_syscall(pid, signum=0):
ptrace(PTRACE_SYSCALL, pid, 1, signum)
def ptrace_cont(pid, signum=0):
ptrace(PTRACE_CONT, pid, 1, signum)
if HAS_PTRACE_GETREGS:
def ptrace_getregs(pid):
regs = ptrace_registers_t()
ptrace(PTRACE_GETREGS, pid, addressof(regs))
return regs
def ptrace_setregs(pid, regs):
ptrace(PTRACE_SETREGS, pid, addressof(regs))
if HAS_PTRACE_SINGLESTEP:
def ptrace_singlestep(pid):
ptrace(PTRACE_SINGLESTEP, pid, 1)
if HAS_PTRACE_IO:
def ptrace_io(pid, io_desc):
ptrace(PTRACE_IO, pid, addressof(io_desc))
|