本人想让本人下面的代码,当s=”abc”的时候, 断到”f()”函数里面,让本人能查看”i”的值。
#include<string> using namespace std; int i=0; void f(const string& s1) { ++i; // line 6 } int main() { string s="a"; s+="b"; s+="c"; s+="d"; s+="e"; s+="f"; return 0; }
编译运行,没有问题。但是调试呢?
g++ 1.cpp -g gdb a.out ... (gdb) b main:6 if strcmp(s.c_str(),"abc")==0 Breakpoint 1 at 0x400979: file 1.cpp, line 9.
这个b main的输出很奇怪: 本人明明是想断在第6行,它却硬说是 “line9″断了。
然后本人运行它,看起来是本人的程序或gdb本人崩溃了:
(gdb) r Starting program: /home/dev/a.out Program received signal SIGSEGV, Segmentation fault. __strcmp_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S:31 31 ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S: No such file or directory. Error in testing breakpoint condition: The program being debugged was signaled while in a function called from GDB. GDB remains in the frame where the signal was received. To change this behavior use "set unwindonsignal on". Evaluation of the expression containing the function (__strcmp_sse2_unaligned) will be abandoned. When the function is done executing, GDB will silently stop. Program received signal SIGSEGV, Segmentation fault. Breakpoint 1, __strcmp_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S:31 31 in ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S
假如本人把这个断点的信息修改一下:
(gdb) b main:6 if s.compare("abc")==0 Breakpoint 1 at 0x400979: file 1.cpp, line 9.
这次输出信息不一样,但是还是貌似崩溃。
(gdb) r Starting program: /home/dev/a.out Program received signal SIGSEGV, Segmentation fault. __memcmp_sse4_1 () at ../sysdeps/x86_64/multiarch/memcmp-sse4.S:1024 1024 ../sysdeps/x86_64/multiarch/memcmp-sse4.S: No such file or directory. Error in testing breakpoint condition: The program being debugged was signaled while in a function called from GDB. GDB remains in the frame where the signal was received. To change this behavior use "set unwindonsignal on". Evaluation of the expression containing the function (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(char const*) const) will be abandoned. When the function is done executing, GDB will silently stop. Program received signal SIGSEGV, Segmentation fault. Breakpoint 1, __memcmp_sse4_1 () at ../sysdeps/x86_64/multiarch/memcmp-sse4.S:1024 1024 in ../sysdeps/x86_64/multiarch/memcmp-sse4.S
到底是gdb还是本人的程序收到了段错误的信号? 原因是什么?
解决方案
40
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件例如‘core.1234’
使用命令
gdb 运行程序名 core或core.数字
进入gdb然后使用bt命令
可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
假如进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
使用命令
gdb 运行程序名 core或core.数字
进入gdb然后使用bt命令
可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
假如进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令