如何回溯使用gdb调用main的方式?

时间:2011-03-28 15:59:47

标签: gdb main

main内,是否有任何命令来显示它是如何被调用的?

1 个答案:

答案 0 :(得分:1)

如果我得到正确的答案,这可能就是你要找的东西:

(gdb) help set backtrace past-main 
Set whether backtraces should continue past "main".
Normally the caller of "main" is not of interest, so GDB will terminate
the backtrace at "main".  Set this variable if you need to see the rest
of the stack trace.

因此,如果您将其设置为on(并且libc的调试信息可用,请参阅this anwser),您将看到如下所示的堆栈:

(gdb) where
#0  main () at ./functionPtr.c:8
#1  0x0000003c47e2139d in __libc_start_main (main=0x40052b <main>, argc=1, ubp_av=0x7fffffffde28, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>,     stack_end=0x7fffffffde18) at libc-start.c:226
#2  0x0000000000400449 in _start ()

周围的libc-start.c代码看起来像这样:

struct pthread *self = THREAD_SELF;

/* Store old info.  */
unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);

/* Store the new cleanup handler info.  */
THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);

/* Run the program.  */
result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);