通過上一篇的學習,我們已經(jīng)可以用KGDB調(diào)試內(nèi)核了唆涝,但是GDB并不能查看所有寄存器找都,比如IDTR. 本篇將介紹使用Virtualbox自帶的調(diào)試器+kgdb雙劍合璧,來學習內(nèi)核廊酣。
VirtualBox 內(nèi)置的調(diào)試器默認是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)在進入Guest Ubuntu使Guest kernel進入被調(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默認使用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進行中斷進入和退出的預處理客情。
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.
簡單的說,就是進入中斷處理函數(shù)之前調(diào)用error_entry膀斋,做一些準備工作梭伐,然后調(diào)用實際的中斷處理函數(shù)do_page_fault, 然后再退出仰担。
(gdb) info line do_page_fault
Line 1466 of "/root/vagrant/linux/arch/x86/mm/fault.c"
看看實際 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/