2017-08-17 94 views
3

我有一個C++對象,它有20個構造函數,我想知道哪個特定的構造函數被調用。如何在gdb的所有構造函數中爲C++對象同時設置斷點?

+2

如果你有很多的構造函數,我會說你的設計是有缺陷的。或者你的設計的實現是。 –

+0

爲什麼你不能使用's'命令進入ctor? – dlmeetei

+0

你應該能夠找出從調用代碼中調用哪個構造函數。如果你仍然想設置一個斷點,那麼你可以從所有20個構造函數中調用一些虛擬方法,並在那裏設置一個斷點。 – VTT

回答

2

您可以使用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) 
0

只要運行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> 
+0

由於某種原因,我得到斷點上只有一個構造函數中斷std :: runtime_error :: runtime_error – avimonk

+0

您是否運行過'break std :: runtime_error :: runtime_error'或'break std :: runtime_error :: runtime_error()'? – OutOfBound

+0

b std :: runtime_error :: runtime_error。只需複製粘貼你的。也許一些舊的gdb版本或代碼沒有在調試中編譯? – avimonk

相關問題