简体   繁体   中英

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

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

You can use rbreak . See 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.

Example:

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

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

gdb session:

[ ~]$ 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.

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 . The gdb output will be something like this:

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:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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