STL type/function 用于gdb conditional break,会导致程序崩溃吗?

STL type/function used in gdb conditional break, will crash the program?

我想在下面的程序中进行测试:当 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 不简单地报告错误,而是使程序崩溃?

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

这里发生的事情是你的命令:

(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