简体   繁体   English

如何在gdb中为C ++对象在所有构造函数上同时设置断点?

[英]How to set breakpoint simultaneously on all constructors in gdb for a C++ object?

我有一个C ++对象,它有20个构造函数左右,我想知道哪个特定的构造函数被调用。

You can use rbreak . 你可以使用rbreak See documentation : 文档

rbreak regex rbreak正则表达式

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. 设置这些断点后,它们将被视为与使用break命令设置的断点一样。 You can delete them, disable them, or make them conditional the same way as any other breakpoint. 您可以删除它们,禁用它们,或者以与任何其他断点相同的方式使它们成为条件。

Example: 例:

class Foo {
public:
  Foo() {}
  Foo(int) {}
};

int main() {
  Foo f1;
  Foo f2(1);
  return 0;
}

gdb session: 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)

Just run break myNamespace::myClass::myClass and gdb will break on every constructor. 只需运行break myNamespace::myClass::myClass ,gdb将在每个构造函数上中断。

For example if you want to break on the creation of any runtime_error, which has at least 2 constructors you can run break std::runtime_error::runtime_error . 例如,如果要中断任何具有至少2个构造函数的runtime_error的创建,则可以运行break std::runtime_error::runtime_error The gdb output will be something like this: gdb输出将是这样的:

Breakpoint 4 at 0xaf20 (4 locations)

This is indicating, that the breakpoint is set to multiple constructors. 这表明断点设置为多个构造函数。 To check the locations of the breakpoint running info breakpoints will deliver output like this: 要检查断点运行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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM