gpt4 book ai didi

c++ - gdb条件中断中使用的STL类型/函数,会导致程序崩溃吗?

转载 作者:行者123 更新时间:2023-11-28 05:27:55 28 4
gpt4 key购买 nike

我想在下面的程序中进行测试:当 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

关于c++ - gdb条件中断中使用的STL类型/函数,会导致程序崩溃吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167645/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com