【发布时间】:2014-10-10 10:37:44
【问题描述】:
我使用 python 的ptrace 模块编写了一个调试器。调试程序在断点处停止后,我会这样做:
- 在断点处恢复原指令,
- 一步到位,
- 再次设置断点,
- 继续执行程序。
在我的系统上,如果第 2 步 (singleStep) 紧跟在第 3 步 (createBreakpoint) 之后,则会出现错误消息:
ptrace.error.PtraceError: ptrace(cmd=4, ...) error #3: No such process
但是如果我插入一个延迟(在下面的代码中使用sys.readline),那么所有的步骤和被调试的程序都会成功执行。
很可能,错误不是特定于ptrace 模块,也许我只是不知道正确的方法。欢迎任何帮助。
Python 代码:
import sys
import ptrace.debugger.child
import ptrace.debugger.debugger
pid = ptrace.debugger.child.createChild(['./sleeper'], 0)
dbg = ptrace.debugger.debugger.PtraceDebugger()
process = dbg.addProcess(pid, is_attached=1)
# use gdb "disassemble main" to find the address of
# the "movel"-instruction between the two "call"s
bp = process.createBreakpoint(0x08048432)
process.cont()
event = process.waitEvent()
print("New process event: %s" % event)
bp.desinstall(set_ip=1)
# Try to reinstall the breakpoint
process.singleStep()
if 1: # otherwise crash?
print 'Press any key to continue...'
sys.stdin.readline()
bp = process.createBreakpoint(bp.address)
print("Continue process execution")
process.cont()
C 代码:
#include <stdio.h>
int main() {
printf( "~~~~~~~~~~~~> Before breakpoint\n" );
// The breakpoint
printf( "~~~~~~~~~~~~> After breakpoint\n" );
return 0;
}
【问题讨论】: