【问题标题】:GDB doesn't quit execution after main() returns 0main() 返回 0 后 GDB 不退出执行
【发布时间】:2012-09-19 18:24:41
【问题描述】:

我正在玩指针,只是做一些基本的事情来巩固我对它们的理解。当我尝试使用 GDB 的“下一步”和“步骤”调试并遵循我在网上找到的这个示例时,GDB 会在函数的末尾运行。在它到达语句“return 0;”之后它告诉我它“无法在 start() 中访问地址 0x0 0x0000000100000de4 的内存

这是代码:

#include <cstdio>
#include <ctype.h>


int main()
{
    char my_str[] = "hello world";
    *my_str = toupper(*my_str);
    *(my_str + 6) = toupper(*(my_str + 6));
    printf("%s", my_str); // prints, "Hello World"
    return 0;
}

这是 gdb 的输出:

Breakpoint 1, main () at pwp.cpp:10
10       return 0;
(gdb) n
Cannot access memory at address 0x0
0x0000000100000de4 in start ()
(gdb) s
Single stepping until exit from function start, 
which has no line number information.
0x0000000100000ed6 in dyld_stub_exit ()
(gdb) n
Single stepping until exit from function dyld_stub_exit, 
which has no line number information.
0x0000000100000f08 in dyld_stub_printf ()
(gdb) n
Cannot find bounds of current function
(gdb) q

这是怎么回事?

【问题讨论】:

    标签: gdb bounds


    【解决方案1】:

    main() 返回并不会立即退出您的程序——libc 在退出之前会进行一些清理,其中包括刷新文件描述符上的输出,例如stdout(这里是必需的,因为您没有包含@987654323 @ 在你的printf)。

    默认情况下,GDB 中的n 命令会尝试逐行执行源代码。由于您正在单步执行您没有可用于 (libc) 的源代码,并且由于您正在执行的代码有些奇怪(它是一个动态库“存根”函数),因此该命令不起作用正确。如果您真的想一次执行一条指令,请使用si

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2018-09-02
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多