如何在gdb的所有构造函数中为C++对象同时设置断点?
您可以使用rbreak
。见documentation:
rbreak regex
Set breakpoints on all functions matching the regular expression regex. This command sets an unconditional breakpoint on all matches, printing a list of all breakpoints it set. Once these breakpoints are set, they are treated just like the breakpoints set with the break command. You can delete them, disable them, or make them conditional the same way as any other breakpoint.
例子:
class Foo {
public:
Foo() {}
Foo(int) {}
};
int main() {
Foo f1;
Foo f2(1);
return 0;
}
GDB会话:
[ ~]$ gdb -q a.out
Reading symbols from a.out...done.
(gdb) rbreak Foo::Foo
Breakpoint 1 at 0x4004dc: file so-rbr.cpp, line 3.
void Foo::Foo();
Breakpoint 2 at 0x4004eb: file so-rbr.cpp, line 4.
void Foo::Foo(int);
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004004dc in Foo::Foo() at so-rbr.cpp:3
2 breakpoint keep y 0x00000000004004eb in Foo::Foo(int) at so-rbr.cpp:4
(gdb)
只要运行break myNamespace::myClass::myClass
,gdb就会在每个构造函数中断开。
例如,如果您想要创建任何具有至少2个构造函数的runtime_error,您可以运行break std::runtime_error::runtime_error
。 gdb输出将如下所示:
Breakpoint 4 at 0xaf20 (4 locations)
这表明断点设置为多个构造函数。要检查运行info breakpoints
将提供输出这样的断点的位置:
Num Type Disp Enb Address What
1 breakpoint keep y <MULTIPLE>
1.1 y 0x000000000000af20 <std::runtime_error::runtime_error(char const*)@plt>
1.2 y 0x000000000000b300 <std::runtime_error::runtime_error(std::runtime_error const&)@plt>
1.3 y 0x000000000000b460 <std::runtime_error::runtime_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)@plt>
1.4 y 0x000000000000b5e0 <std::runtime_error::runtime_error(char const*)@plt>
由于某种原因,我得到断点上只有一个构造函数中断std :: runtime_error :: runtime_error – avimonk
您是否运行过'break std :: runtime_error :: runtime_error'或'break std :: runtime_error :: runtime_error()'? – OutOfBound
b std :: runtime_error :: runtime_error。只需复制粘贴你的。也许一些旧的gdb版本或代码没有在调试中编译? – avimonk
如果你有很多的构造函数,我会说你的设计是有缺陷的。或者你的设计的实现是。 –
为什么你不能使用's'命令进入ctor? – dlmeetei
你应该能够找出从调用代码中调用哪个构造函数。如果你仍然想设置一个断点,那么你可以从所有20个构造函数中调用一些虚拟方法,并在那里设置一个断点。 – VTT