【问题标题】:Using gdb as a monitor?使用 gdb 作为监视器?
【发布时间】:2010-01-29 05:17:54
【问题描述】:

GDB 可以像传统的汇编监视器一样使用吗?

一旦你踏入例如。它返回的库代码:

No function contains program counter for selected frame

GDB 调试器 能够进入未知代码,但GDB UI 停止工作。

在这个相关的question 中,您可以找到一对建议的解决方案,但都不太让我满意。

如果二进制库没有附带调试符号包怎么办?如果程序跳转到运行时生成的代码怎么办?

反汇编代码也不是真正的解决方案,因为 UI 会忽略它,最重要的是,在您返回到原始已知代码之前,寄存器的值不会更新。 info registers 有效,但几乎没有互动性。

有什么建议吗?

谢谢!

【问题讨论】:

    标签: assembly gdb


    【解决方案1】:

    您可以使用display 命令执行此类操作。

    display/i $pc 将在提示符之前反汇编当前指令 每次打印:

    (gdb) b main
    Breakpoint 1 at 0x80483b5: file hw.c, line 5.
    (gdb) display/i $pc
    (gdb) r
    Starting program: /tmp/hw
    
    Breakpoint 1, main () at hw.c:5
    5         puts("Hello world");
    1: x/i $pc
    0x80483b5 <main+17>:    movl   $0x8048490,(%esp)
    

    现在执行一条指令(然后继续按 Enter 重复):

    (gdb) si
    0x080483bc      5         puts("Hello world");
    1: x/i $pc
    0x80483bc <main+24>:    call   0x80482d4 <puts@plt>
    (gdb) 
    0x080482d4 in puts@plt ()
    1: x/i $pc
    0x80482d4 <puts@plt>:   jmp    *0x804959c
    Current language:  auto; currently asm
    (gdb) 
    0x080482da in puts@plt ()
    1: x/i $pc
    0x80482da <puts@plt+6>: push   $0x10
    (gdb) 
    0x080482df in puts@plt ()
    1: x/i $pc
    0x80482df <puts@plt+11>:        jmp    0x80482a4 <_init+48>
    

    当我们到达这一点时它仍然有效:

    (gdb) 
    0x080482a4 in ?? ()
    1: x/i $pc
    0x80482a4 <_init+48>:   pushl  0x804958c
    (gdb) 
    0x080482aa in ?? ()
    1: x/i $pc
    0x80482aa <_init+54>:   jmp    *0x8049590
    (gdb) 
    0xb7f052d0 in _dl_runtime_resolve () from /lib/ld-linux.so.2
    1: x/i $pc
    0xb7f052d0 <_dl_runtime_resolve>:       push   %eax
    

    可以同时激活多个display 表达式(使用undisplay &lt;number&gt; 删除它们)。例如,观察%eax 发生了什么:

    (gdb) display/x $eax
    2: /x $eax = 0xbf90ab34
    (gdb) si
    0xb7f052d1 in _dl_runtime_resolve () from /lib/ld-linux.so.2
    2: /x $eax = 0xbf90ab34
    1: x/i $pc
    0xb7f052d1 <_dl_runtime_resolve+1>:     push   %ecx
    (gdb) 
    0xb7f052d2 in _dl_runtime_resolve () from /lib/ld-linux.so.2
    2: /x $eax = 0xbf90ab34
    1: x/i $pc
    0xb7f052d2 <_dl_runtime_resolve+2>:     push   %edx
    (gdb) 
    0xb7f052d3 in _dl_runtime_resolve () from /lib/ld-linux.so.2
    2: /x $eax = 0xbf90ab34
    1: x/i $pc
    0xb7f052d3 <_dl_runtime_resolve+3>:     mov    0x10(%esp),%edx
    (gdb) 
    0xb7f052d7 in _dl_runtime_resolve () from /lib/ld-linux.so.2
    2: /x $eax = 0xbf90ab34
    1: x/i $pc
    0xb7f052d7 <_dl_runtime_resolve+7>:     mov    0xc(%esp),%eax
    

    ...这里可以看到%eax 的变化:

    (gdb) 
    0xb7f052db in _dl_runtime_resolve () from /lib/ld-linux.so.2
    2: /x $eax = 0xb7f0d668
    1: x/i $pc
    0xb7f052db <_dl_runtime_resolve+11>:    call   0xb7eff780 <_dl_fixup>
    (gdb) 
    

    【讨论】:

    • 比替代方案更好,但它不适用于 TUI。一段时间后,我仍然会将其标记为已接受的答案,因为我意识到可能没有任何真正的解决方案。
    猜你喜欢
    • 2023-03-10
    • 2021-04-30
    • 1970-01-01
    • 2011-07-05
    • 2019-09-14
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多