2013-06-18 30 views
0

在Linux上編碼C++時,我使用Eclipse CDT。當進入C/C++ OS函數時,我可以看到彙編程序,但是考慮到這些文件都存儲在/ usr/include /中,我認爲調試程序會進入C/C++的每一行。調試 - 步入OS功能?

那麼,有沒有什麼辦法可以在Linux上調試C++,它可以讓你進入OS功能的C/C++?

+0

對於在內核中運行的代碼,您無法運行調試器,因爲如果調試器在不方便的時間點擊斷點,它可能會在內核中創建死鎖。在許多系統上都有一個內核調試器,但它通常不能鏈接到用戶級代碼,有時必須通過另一臺機器的串行端口訪問。如果您對正在調試的系統進行實時內核調試感興趣,請在Mac OS X,BSD或Solaris上查看[DTrace](http://en.wikipedia.org/wiki/DTrace)。 – Dan

回答

1

是的,你可以在Linux下使用gdb(GNU調試器)。界面非常粗魯,但它完成了這項工作。一個小例子:首先,啓動gdb。

gdb your_program 

然後,你就在裏面。

.... 
Reading symbols from foobar...done. 
(gdb) start # begin the debug session 
... 
(gdb) disas # show the disassembly code of the current function (main) 
.... # lot of asm 
0x00000000004007d4 <+17>: call 0x400440 <[email protected]> 
0x00000000004007d9 <+22>: mov QWORD PTR [rax],0x0 
0x00000000004007e0 <+29>: push rax 
... # lot of asm 
(gdb) break *0x4007d4 # set a break point at the address of the call malloc 
Breakpoint 2 at 0x4007d4 
(gdb) run # run until breakpoint 
... 
Breakpoint 2, 0x00000000004007d4 in main() # the breakpoint has been reached 
=> 0x00000000004007d4 <main+17>:  e8 67 fc ff ff call 0x400440 <[email protected]> 
(gdb) si # step into the malloc 
0x0000000000400440 in [email protected]() 
=> 0x0000000000400440 <[email protected]+0>: ff 25 92 11 20 00  jmp QWORD PTR [rip+0x201192]  # 0x6015d8 <[email protected]> # you see code from malloc now 
(gdb) ni # next instruction in malloc 
... 
(gdb) finish # quit the current function, actually malloc 
(gdb) 

但是,您將無法輕鬆顯示高級對應的源代碼。你可以做的最好的是同時讀取庫/內核代碼。

例如,您可以從glibc(GNU標準C庫)here,malloc/malloc.c中讀取malloc的代碼。