如何在 gdb 中为 C++ 对象的所有构造函数同时设置断点?
How to set breakpoint simultaneously on all constructors in gdb for a C++ object?
我有一个 C++ 对象,它有大约 20 个构造函数,我想知道调用了哪个特定的构造函数。
只需 运行 break myNamespace::myClass::myClass
并且 gdb 将在每个构造函数上中断。
例如,如果您想中断任何 runtime_error 的创建,它至少有 2 个构造函数,您可以 运行 break std::runtime_error::runtime_error
。 gdb 输出将是这样的:
Breakpoint 4 at 0xaf20 (4 locations)
这表明,断点被设置为多个构造函数。要检查断点的位置 运行ning 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>
您可以使用 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)
我有一个 C++ 对象,它有大约 20 个构造函数,我想知道调用了哪个特定的构造函数。
只需 运行 break myNamespace::myClass::myClass
并且 gdb 将在每个构造函数上中断。
例如,如果您想中断任何 runtime_error 的创建,它至少有 2 个构造函数,您可以 运行 break std::runtime_error::runtime_error
。 gdb 输出将是这样的:
Breakpoint 4 at 0xaf20 (4 locations)
这表明,断点被设置为多个构造函数。要检查断点的位置 运行ning 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>
您可以使用 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)