【问题标题】:ptrace single step followed by set breakpoint failsptrace 单步后跟设置断点失败
【发布时间】: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;
}

【问题讨论】:

    标签: python ptrace


    【解决方案1】:

    ptrace 的手册页说:

    PTRACE_SYSCALL,PTRACE_SINGLESTEP

    重启停止的tracee PTRACE_CONT,但安排被跟踪者在下一次停止 进入或退出系统调用,或在执行单个 指令,分别。 (被跟踪者也将像往常一样被阻止 收到信号后。)从跟踪器的角度来看,被跟踪者似乎已因收到 SIGTRAP 而停止。因此对于 例如,PTRACE_SYSCALL 的想法是检查参数 在第一站调用系统调用,然后执行另一个 PTRACE_SYSCALL 和 在第二站检查系统调用的返回值。这 数据参数被视为 PTRACE_CONT。 (地址被忽略。)

    我试图通过什么都不做来处理信号,但它有所帮助。

    process.singleStep()
    event = process.waitEvent() # Repair-line
    bp = process.createBreakpoint(bp.address)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2019-03-26
      • 2015-09-16
      • 2016-04-23
      相关资源
      最近更新 更多