1. 查看函數地址
看函數在代碼的哪一行,使用info line就可以看到類似下面這中輸出
點擊(此處)折疊或打開
- (gdb) info line a.cpp:10
- Line 10 of "a.cpp" starts at address 0x80487d4 <_ZN1B5test2Ev> and ends at 0x80487d7 <_ZN1B5test2Ev+3>.
- (gdb) p _ZN1B5test2Ev
- $1 = {void (B * const)} 0x80487d4
- (gdb)
可以直接根據函數地址去打斷點,當然如果知道文件和行號其實沒必要拐個彎來打這種費解的斷點,應用場合更多是想使用匯編調試的時候。
點擊(此處)折疊或打開
- 16 public:
- 17 virtual void test2(){
- 18 cout << "If I'm lying, I'm crying!" << endl;
- 19 }
- 20
- (gdb) info line 17
- Line 17 of "a.cpp" starts at address 0x8048808 <_ZN1D5test2Ev> and ends at 0x804880e <_ZN1D5test2Ev+6>.
- (gdb) b *0x804880e
- Note: breakpoint 2 also set at pc 0x804880e.
- Breakpoint 3 at 0x804880e: file a.cpp, line 18.
2. 查看虛函數表
如果僅僅是想知道當前對象的真實類別,那使用這句就可以看到了(示例里面的b實際上是D類對象,而非B類)。
點擊(此處)折疊或打開
- (gdb) set print object on
- (gdb) p b
- $3 = (D &) @0xbfe3116c: { = {_vptr.B = 0x8048948}, }
查看對象里面的虛指針,也很簡單,把虛指針指向的內容顯示一下就可以了。
點擊(此處)折疊或打開
- (gdb) p b
- $6 = (D &) @0xbfe3116c: { = {_vptr.B = 0x8048948}, }
- (gdb) p /a *(void**)0x8048948@2
- $7 = {0x8048834 <_ZN1D4testEv>, 0x8048808 <_ZN1D5test2Ev>}
點擊(此處)折疊或打開
- (gdb) p /a *((void**)0x8048948-1)
- $18 = 0x8048950 <_ZTI1D>
點擊(此處)折疊或打開
- sles10sp1:~/test # c++filt _ZTI