【问题标题】:STL type/function used in gdb conditional break, will crash the program?gdb条件中断中使用的STL类型/函数,会导致程序崩溃吗?
【发布时间】:2016-10-21 02:56:39
【问题描述】:

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

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

【问题讨论】:

    标签: c++ stl crash gdb breakpoints


    【解决方案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
    

    【讨论】:

    • 我在 C++ 代码中看到了这个问题,即使我准确地设置了断点,即 :.有谁知道还有什么原因?
    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多