如何在 Linux 上查看 main 之前的调用堆栈

How to see the call stack before main on Linux

为了查看 C++ 程序的启动过程(带有一个控制台应用程序和一个共享库),首先我在 Ubuntu 18.04 上安装了 CodeBlocks IDE,但它只显示 main 作为调用堆栈中的第一个条目。

然后我使用 gdb 运行 程序并在“_start”(应该是默认的 Unix 应用程序入口点)和“main”设置断点,但是当在“main”处断点时, backtrace 命令只显示一帧。

如何查看 main 之前的调用堆栈以及如何为 symbols/sources 系统库设置 dgb 以查看源代码?

您需要的 GDB 设置是 set backtrace past-main on:

Backtraces will continue past the user entry point.

(gdb) set backtrace past-main on
(gdb) start
(gdb) bt
#0  main (argc=argc@entry=1, argv=argv@entry=0x7ffffffee1a8) at hoist.c:7
#1  0x00007fffff0802e1 in __libc_start_main (main=0x8000580 <main>, argc=1, argv=0x7ffffffee1a8, init=<optimized out>,
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffee198) at ../csu/libc-start.c:291
#2  0x000000000800069a in _start ()

(gdb) fr 1
#1  0x00007fffff0802e1 in __libc_start_main (main=0x8000580 <main>, argc=1, argv=0x7ffffffee1a8, init=<optimized out>,
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffee198) at ../csu/libc-start.c:291
291     ../csu/libc-start.c: No such file or directory.

按照 GDB 抱怨缺少 raise.c 中所述安装 glibc 源后 :

(gdb) fr 1
#1  0x00007fffff0802e1 in __libc_start_main (main=0x8000580 <main>, argc=1, argv=0x7ffffffee1a8, init=<optimized out>,
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffee198) at ../csu/libc-start.c:291
291           result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);