调试 - 进入OS功能?

时间:2013-06-18 18:46:11

标签: c++ c linux unix eclipse-cdt

在Linux上编写C ++时,我使用的是Eclipse CDT。当进入C / C ++ OS函数时,我可以看到汇编程序,但考虑到文件都存储在/ usr / include /中,我认为调试器会进入C / C ++的每一行。

那么,有没有办法在Linux上调试C ++,它允许你进入OS函数的C / C ++?

1 个答案:

答案 0 :(得分: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 <malloc@plt>
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 <malloc@plt>
(gdb) si # step into the malloc
0x0000000000400440 in malloc@plt ()
=> 0x0000000000400440 <malloc@plt+0>:   ff 25 92 11 20 00       jmp    QWORD PTR [rip+0x201192]        # 0x6015d8 <malloc@got.plt> # you see code from malloc now
(gdb) ni # next instruction in malloc
...
(gdb) finish # quit the current function, actually malloc
(gdb)

但是,您将无法轻松显示高级别的相应源代码。您可以做的最好的事情是同时读取库/内核代码。

例如,您可以在malloc/malloc.c中从glibc(GNU标准C库)here中读取malloc的代码。