gdb 在完成 main 后跳转前几行

gdb jumps previous lines after finish main

我想弄清楚为什么 gdb 没有按预期运行。不确定是什么触发了这个。看到这个问题后,我写了一个主函数的最小版本。它完成了最后一行并正确退出。又加了一项功能后,跳转又回来了。即使在注释掉添加的行后,跳回也不会消失。

我不确定这是否与链接到我自己的库有关,即使在注释掉大部分代码(不是单行引用我自己的库)之后,构建过程仍然使用链接(automake):

LDADD = ../libmyown.la

此行用于将库添加到此目录中构建的许多其他程序。我测试了不同版本的 gdb 和 gcc,这两个程序的最新版本都是 8.2。已使用 -g -O0、-ggdb -O0 选项。我在 Whosebug 中看到过有关此类行为的帖子。不确定我是否遇到了这种行为,或者这是我自己的库中的一些错误,其链接导致了这种回跳行为。虽然这个帖子看起来是多余的,但我试图提供更多的上下文信息。希望我自己的其他人会找到解决方案并帮助他人。

除gdb跳转问题外,二进制程序正常执行。

这里是主要演示跳转问题的稍微扩展版本:

Breakpoint 1, main (argc=1, argv=0x7fffffffdeb8) at alnlocalmany.cpp:66
66     if (isDNA(reffile)) {
(gdb) n
67        alignDNAMany(reffile, dbfile, outfile);
(gdb) 
68        return 0;
(gdb) s
39     string reffile, dbfile, outfile;
(gdb) s
73  }
(gdb) s
0x00007ffff6d97b97 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) s
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 15621) exited normally]

注意它从 68 跳到 39(main 的第一行)然后 73(main 的结束大括号)

有一个类似的 gcc bug 49951 in the past which was later closed with the status RESOLVED FIXED. However it can be reproduced as of now with the current version of gcc (I tested with gcc 8.2.1). Other people also claim that the bug was not fixed, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49951#c20

作为解决方法,您可以尝试使用 clang 而不是 gcc,至少在使用 clang 构建时,该错误没有在 gcc bug 49951 的测试示例中重现。您也可以将 gcc 降级到 4.4 版,但它现在太旧了。