Virturbox debugger & kgdb 雙劍合璧

通過上一篇的學(xué)習(xí),我們已經(jīng)可以用KGDB調(diào)試內(nèi)核了,但是GDB并不能查看所有寄存器,比如IDTR. 本篇將介紹使用Virtualbox自帶的調(diào)試器+kgdb雙劍合璧,來學(xué)習(xí)內(nèi)核。

VirtualBox 內(nèi)置的調(diào)試器默認(rèn)是disable的,首先通過下面的命令開啟VBoxDBG.

# 獲取Guest OS的Name和UUID
root@ubuntu:~/vagrant# VBoxManage list vms
"vagrant_default_1523049472371_88710" {bb455030-5669-4012-8dcb-65c6d0549c61}
# Enable VBoxDBG
root@ubuntu:~/vagrant/kernel# VBoxManage setextradata \
    "vagrant_default_1523049472371_88710" VBoxInternal/DBGC/Enabled 1

現(xiàn)在利用Vagrant啟動Guest Ubuntu:

root@ubuntu:~/vagrant# vagrant up
root@ubuntu:~/vagrant# vagrant ssh

現(xiàn)在進(jìn)入Guest Ubuntu使Guest kernel進(jìn)入被調(diào)試狀態(tài):

vagrant@ubuntu-xenial:~$ sudo sh -c 'echo g > /proc/sysrq-trigger'

開另外一個窗口,用gdb連接到Guest內(nèi)核:

root@ubuntu:~/vagrant/obj/x86_64# gdb vmlinux
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
...
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...done.
(gdb) target remote :1234
Remote debugging using :1234
kgdb_breakpoint () at /root/vagrant/linux/kernel/debug/debug_core.c:1073
1073            wmb(); /* Sync point after breakpoint */
(gdb)

再開另外一個窗口,連接到VBoxDBG,VBoxDBG默認(rèn)使用telnet協(xié)議監(jiān)聽在5000端口,

root@ubuntu:~/vagrant/# telnet  localhost 5000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome to the VirtualBox Debugger!
Current VM is 79be5000, CPU #0

現(xiàn)在我們要用DI來打印出中斷向量表,不帶參數(shù)會打印出256個中斷向量,太多了,這里我們只查看14號中斷向量,14號中斷是缺頁中斷:

VBoxDbg> help di
di          [int [..]]                     Dump the interrupt descriptor table (IDT).
             <0+ args>
    int          The interrupt vector or interrupt vector range. <optional+>

VBoxDbg> di 0xe
000e Int64  Sel:Off=0010:ffffffff818012c0     DPL=0 P  IST=0

可以看到14號中斷處理函數(shù)地址是: 0xffffffff818012c0,現(xiàn)在回到gdb:

(gdb) info symbol 0xffffffff818012c0
page_fault in section .text

(gdb) info line page_fault
Line 1158 of "/root/vagrant/linux/arch/x86/entry/entry_64.S" starts at address 0xffffffff81801273 <general_protection+3>
   and ends at 0xffffffff818012c3 <page_fault+3>.

第一個命令顯示地址0xffffffff818012c0位于代碼段,符號名稱為page_fault。
第二個命令顯示,page_fault位于arch/x86/entry/entry_64.S,第1158行。
現(xiàn)在看看源代碼吧:

1158 idtentry general_protection     do_general_protection   has_error_code=1

idtentry 是一個宏,定義在同一個文件中,它根據(jù)處理器spec進(jìn)行中斷進(jìn)入和退出的預(yù)處理。

901 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
902 ENTRY(\sym)
903         UNWIND_HINT_IRET_REGS offset=\has_error_code*8
...
950         call    \do_sym

這個宏太啰嗦,不如看匯編代碼來的方便:

(gdb) disassemble page_fault
Dump of assembler code for function page_fault:
   0xffffffff818012c0 <+0>:     nopl   (%rax)
   0xffffffff818012c3 <+3>:     testb  $0x3,0x10(%rsp)
   0xffffffff818012c8 <+8>:     jne    0xffffffff818012ea <page_fault+42>
   0xffffffff818012ca <+10>:    callq  0xffffffff818014a0 <error_entry>
   0xffffffff818012cf <+15>:    mov    %rsp,%rdi
   0xffffffff818012d2 <+18>:    mov    0x78(%rsp),%rsi
   0xffffffff818012d7 <+23>:    movq   $0xffffffffffffffff,0x78(%rsp)
   0xffffffff818012e0 <+32>:    callq  0xffffffff810566b0 <do_page_fault>
   0xffffffff818012e5 <+37>:    jmpq   0xffffffff81801590 <error_exit>
   0xffffffff818012ea <+42>:    callq  0xffffffff818014a0 <error_entry>
   0xffffffff818012ef <+47>:    mov    %rsp,%rdi
   0xffffffff818012f2 <+50>:    mov    0x78(%rsp),%rsi
   0xffffffff818012f7 <+55>:    movq   $0xffffffffffffffff,0x78(%rsp)
   0xffffffff81801300 <+64>:    callq  0xffffffff810566b0 <do_page_fault>
   0xffffffff81801305 <+69>:    jmpq   0xffffffff81801590 <error_exit>
End of assembler dump.

簡單的說,就是進(jìn)入中斷處理函數(shù)之前調(diào)用error_entry,做一些準(zhǔn)備工作,然后調(diào)用實(shí)際的中斷處理函數(shù)do_page_fault, 然后再退出。

(gdb) info line do_page_fault
Line 1466 of "/root/vagrant/linux/arch/x86/mm/fault.c"

看看實(shí)際 page fault處理函數(shù)吧。

dotraplinkage void notrace
do_page_fault(struct pt_regs *regs, unsigned long error_code)
{
        unsigned long address = read_cr2(); /* Get the faulting address */
        enum ctx_state prev_state;

        prev_state = exception_enter();
        if (trace_pagefault_enabled())
                trace_page_fault_entries(address, regs, error_code);

        __do_page_fault(regs, error_code, address);
        exception_exit(prev_state);
}

下一篇將介紹一個更加靈活的工具,GDB Python extension.
參考:
https://www.virtualbox.org/manual/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容