GDB核心转储:崩溃后恢复argc argv值

时间:2011-08-10 18:10:00

标签: c++ linux gdb coredump

应用程序崩溃后,是否可以恢复main的argv和argc参数的确切值?

我只需要在Linux上使用应用程序core-dump和gdb调试器。

2 个答案:

答案 0 :(得分:2)

是的,如果应用程序是使用调试信息编译的。在gdb中打开核心转储并查找包含main函数的框架。然后转到此框架并打印 argv argc 的值。这是示例gdb会话。

[root@localhost ~]# gdb ./a.out core.2020
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/a.out...done.
[New Thread 2020]

warning: Can't read pathname for load map: Input/output error.
Reading symbols from /usr/lib/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./a.out'.
Program terminated with signal 6, Aborted.
#0  0x0027b424 in __kernel_vsyscall ()
(gdb) bt
#0  0x0027b424 in __kernel_vsyscall ()
#1  0x00b28b91 in raise () from /lib/libc.so.6
#2  0x00b2a46a in abort () from /lib/libc.so.6
#3  0x007d3397 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/libstdc++.so.6
#4  0x007d1226 in ?? () from /usr/lib/libstdc++.so.6
#5  0x007d1263 in std::terminate() () from /usr/lib/libstdc++.so.6
#6  0x007d13a2 in __cxa_throw () from /usr/lib/libstdc++.so.6
#7  0x08048940 in main (argv=1, argc=0xbfcf1754) at test.cpp:14
(gdb) f 7
#7  0x08048940 in main (argv=1, argc=0xbfcf1754) at test.cpp:14
14              throw std::runtime_error("123");
(gdb) p argv
$1 = 1
(gdb) p argc
$2 = (char **) 0xbfcf1754
(gdb)

答案 1 :(得分:1)

看起来你需要从基础开始...... !!

使用-g标志编译应用程序代码,确保不要剥离它。

说我是否想编译hello.c

gcc -c -g hello.c -o hello.o
gcc hello.o -o hello

现在如果您不想调试

ulimit -c unlimited
./hello

当应用程序崩溃时,将生成核心文件。

检查核心文件

"gdb ./hello core.$$$" this will list you your stack.

您也可以选择调试图像 gdb你好

互联网上有很多关于GDB的东西,请仔细阅读。