gpt4 book ai didi

gdb - GDB有 "step-to-next-call"指令吗?

转载 作者:行者123 更新时间:2023-12-01 16:02:51 26 4
gpt4 key购买 nike

WinDBG 和相关的 Windows 内核调试器支持“pc”命令,该命令运行目标直到到达下一个调用语句(在汇编中)。换句话说,它在创建新的堆栈帧之前中断,这与“完成”相反。 GDB 中的“开始”一直运行到 main 开始,但本质上我想要“开始”,但带有“任何下一帧”的通配符。

我试图在 GDB 中找到类似的功能,但还没有找到。

这可能吗?

WinDBG 文档示例:http://windbg.info/doc/1-common-cmds.html#4_expr_and_cmds

最佳答案

简单答案:不,step-to-next-call 不是 GDB 命令的一部分。

GDB/Python 感知答案:不,它不是 GDB 命令的一部分,但它很容易实现!

我不确定您是否想在call指令执行之前或之后停止。

  • 要在之前停止,您需要stepi/nexti(下一条汇编指令),直到在当前指令中看到call :

    import gdb

    class StepBeforeNextCall (gdb.Command):
    def __init__ (self):
    super (StepBeforeNextCall, self).__init__ ("step-before-next-call",
    gdb.COMMAND_OBSCURE)

    def invoke (self, arg, from_tty):
    arch = gdb.selected_frame().architecture()

    while True:
    current_pc = addr2num(gdb.selected_frame().read_register("pc"))
    disa = arch.disassemble(current_pc)[0]
    if "call" in disa["asm"]: # or startswith ?
    break

    SILENT=True
    gdb.execute("stepi", to_string=SILENT)

    print("step-before-next-call: next instruction is a call.")
    print("{}: {}".format(hex(int(disa["addr"])), disa["asm"]))

    def addr2num(addr):
    try:
    return int(addr) # Python 3
    except:
    return long(addr) # Python 2

    StepBeforeNextCall()
  • 要在调用之后停止,您需要计算当前堆栈深度,然后步进直到更深:

    import gdb

    def callstack_depth():
    depth = 1
    frame = gdb.newest_frame()
    while frame is not None:
    frame = frame.older()
    depth += 1
    return depth

    class StepToNextCall (gdb.Command):
    def __init__ (self):
    super (StepToNextCall, self).__init__ ("step-to-next-call",
    gdb.COMMAND_OBSCURE)

    def invoke (self, arg, from_tty):
    start_depth = current_depth =callstack_depth()

    # step until we're one step deeper
    while current_depth == start_depth:
    SILENT=True
    gdb.execute("step", to_string=SILENT)
    current_depth = callstack_depth()

    # display information about the new frame
    gdb.execute("frame 0")

    StepToNextCall()

只需将其放入一个文件中,使用 GDB(或在您的 .gdbinit 中)source 即可为您提供新命令step-before-下一次调用下一次调用的步骤

相关文档有:

关于gdb - GDB有 "step-to-next-call"指令吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34621532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com