如何在不退出和失去断点的情况下重新加载重新编译的二进制文件



根据此出色的指南,应该能够重新编译源文件,然后只需使用'r'即可让GDB开始调试新的,更改的二进制文件。

在GDB手册中,这似乎也暗示着"如果GDB上次读取其符号以来,符号文件的修改时间已更改,则GDB丢弃了其符号表,然后再次读取它。

我正在尝试在Ubuntu 16.10上调试一个简单的单个.cpp文件。通过g++ -ggdb -std=c++11 foo.cpp编译后,我可以像往常一样调试。

GNU gdb (Ubuntu 7.11.90.20161005-0ubuntu2) 7.11.90.20161005-git
[...]
(gdb) break main
Breakpoint 1 at 0x2754: file foo.cpp, line 204.
(gdb) r
Starting program: /home/code/foo
Breakpoint 1, main () at foo.cpp:204
(gdb) n
(gdb) k
Kill the program being debugged? (y or n) y

在这里,我对源文件进行了较小的更改,然后重新编译。尝试再次运行文件时:

(gdb) r
/home/code/foo' has changed; re-reading symbols.
Error in re-setting breakpoint 1: Cannot access memory at address 0x55555555674b
Starting program: /home/code/598
warning: Probes-based dynamic linker interface failed.
Reverting to original interface.
[Inferior 1 (process 20898) exited normally]

有没有办法在保持我的断点完整的同时成功地重新加载二进制?

编辑:这篇文章有我要寻找的答案。您可以使用file binaryname命令重新加载可执行文件。

(gdb) file foo
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
A program is being debugged already.
Load new symbol table from "foo"? (y or n) y
Reading symbols from foo...done.
Error in re-setting breakpoint 1: Cannot access memory at address 0x274b
Error in re-setting breakpoint 2: Cannot access memory at address 0x274b

我们看到断点仍然存在,只是禁用:

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x0000555555556754
        breakpoint already hit 1 time
2       breakpoint     keep n   0x000055555555677b 

,所以我们只是启用它们:

(gdb) enable
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555556754 
        breakpoint already hit 1 time
2       breakpoint     keep y   0x000055555555677b
(gdb) 

这起作用,但是我很想听听任何人是否有进一步的建议或投入有关仅使用run是否应该确实有效的建议。

当我使用GDB 5时,重新编译后仅使用run就足以重新加载符号。现在,使用GDB 8.1,我需要在run之前键入file /path/to/executable,以强迫GDB重新编译后重新加载符号。

这是我在GDB 8.3中使用的脚本(略适合此答案(:

define make
    shell make
    python gdb.execute("file " + gdb.current_progspace().filename)
    # clear cache
    directory
end

您需要具有python的GDB。注意更新源文件缓存的directory命令。

特别是断点和pie的问题似乎已在GDB 8.3.1中修复 - 请参见https://www.gnu.org/software/gdbare/gdb/news/and pr 25011。

由于该问题是由于独立于位置的可执行文件(PIE(引起的,因此使用-no-pie重新链接该程序也应解决。

让我进入这个问题的问题是,在新的GDB中,自动重新加载符号似乎已经破坏了,但是似乎变化不在GDB中,而是Linux发行版本开始在GCC中启用PIE。与-no-pie链接还为我修复了符号。

最新更新