main()返回0后,GDB不会退出执行

时间:2012-09-19 18:24:41

标签: gdb bounds

我正在玩指针,只是做一些基本的东西来巩固我对它们的理解。当我尝试调试并遵循我在网上使用GDB的'next'和'step'找到的这个例子时,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

导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:1)

main()返回并不会立即退出程序 - libc在退出之前会进行一些清理,其中包括在stdout等文件描述符上刷新输出(这是必需的,因为你没有在\n中添加printf

GDB中的n命令默认尝试逐行处理一行源代码。既然你单步执行没有源代码可用于(libc)的代码,并且因为你进入的代码有些奇怪(它是动态库“存根”函数),该命令不起作用正确。如果您确实想一次执行一条指令,请使用si