用于gdb条件中断的STL类型/函数,会使程序崩溃吗?

时间:2016-10-21 02:56:39

标签: c++ stl crash gdb breakpoints

我希望在下面的程序中测试:当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;
}

编译并运行a.out,没问题。然后我调试它

g++ 1.cpp -g
gdb a.out
...
(gdb) b main if strcmp(s.c_str(),"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.
__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还是我的命令引起的?如果我的命令有运行时问题,为什么gdb不会简单地报告错误,而是让程序崩溃?

希望得到一些解释,因为我没有得到这个错误原因。

1 个答案:

答案 0 :(得分:1)

这里发生的是你的命令:

(gdb) break main:6

...被gdb解释为与break main相同。您也可以通过输入后者来看到这一点:

(gdb) b main:6
Breakpoint 1 at 0x400919: file q.cc, line 10.
(gdb) b main
Note: breakpoint 1 also set at pc 0x400919.
Breakpoint 2 at 0x400919: file q.cc, line 10.

现在,这很奇怪,因为gdb可能应该警告你,尾随:6会被忽略。 (我建议提交一个错误,告知这会导致语法错误。)

如果要在文件中的某一行中断,则必须使用源文件名。大概你打算输入:

(gdb) break main.cc:6