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
|
#!/usr/bin/env python
from ptrace.debugger.debugger import PtraceDebugger
from ptrace.debugger.child import createChild
from ptrace.tools import locateProgram
from sys import stderr, argv, exit
def playWithProcess(process):
# Do anything you want with the process here...
print("Dump process registers")
process.dumpRegs()
print("Continue process execution")
process.cont()
print("Wait next process event...")
event = process.waitEvent()
print("New process event: %s" % event)
def traceProgram(arguments):
# Copy the environment variables
env = None
# Get the full path of the program
arguments[0] = locateProgram(arguments[0])
# Create the child process
return createChild(arguments, False, env)
def main():
# Check the command line
if len(argv) < 2:
print("usage: %s program [arg1 arg2 ...]" % argv[0], file=stderr)
print(" or: %s pid" % argv[0], file=stderr)
exit(1)
# Get the process identifier
is_attached = False
has_pid = False
if len(argv) == 2:
try:
# User asked to attach a process
pid = int(argv[1])
has_pid = True
except ValueError:
pass
if not has_pid:
# User asked to create a new program and trace it
arguments = argv[1:]
pid = traceProgram(arguments)
is_attached = True
# Create the debugger and attach the process
dbg = PtraceDebugger()
process = dbg.addProcess(pid, is_attached)
# Play with the process and then quit
playWithProcess(process)
dbg.quit()
if __name__ == "__main__":
main()
|