kdb 與 gdb 的用法相對簡單,這裡只講 kgdb。
環境:developer(192.168.16.5,COM1)與 target(192.168.16.30,COM2),兩台都預裝 Red Hat 9,加上一根序列埠線。需要下載三樣東西:Linux 核心 2.4.23、kgdb 核心補丁 1.9 版、以及可除錯核心模組的 gdb(gdbmod-1.9)。
接好線後,用 stty 設定埠參數並實測:
/* developer 上 */ stty ispeed 115200 ospeed 115200 -F /dev/ttyS0 echo hello > /dev/ttyS0 /* target 上 */ stty ispeed 115200 ospeed 115200 -F /dev/ttyS1 cat /dev/ttyS1
線沒問題的話,target 螢幕上會顯示 hello。
# cd /home/liangjian # bunzip2 linux-2.4.23.tar.bz2 && tar -xvf linux-2.4.23.tar # bunzip2 gdbmod-1.9.bz2 # cp gdbmod-1.9 /usr/local/bin # cd linux-2.4.23 # patch -p1 < ../linux-2.4.23-kgdb-1.9.patch # make menuconfig
在 Kernel hacking 中把三項編進核心:KGDB: Remote (serial) kernel debugging with gdb、KGDB: Thread analysis、KGDB: Console messages through gdb。編譯時務必加上 -g:
# make dep; make bzImage
然後把產物送到 target:
# scp arch/i386/boot/bzImage root@192.168.16.30:/boot/vmlinuz-2.4.23-kgdb # scp System.map root@192.168.16.30:/boot/System.map-2.4.23-kgdb # scp arch/i386/kernel/gdbstart root@192.168.16.30:/sbin
gdbstart 是 kgdb 附的工具,用來啟用核心鉤子、讓核心進入除錯狀態。
接著在核心原始碼目錄下編輯 .gdbinit 做 gdb 初始化,定義一個名為 rmt 的巨集來設定埠號與速率:
define rmt set remotebaud 115200 target remote /dev/ttyS0 end
編輯 /etc/grub.conf 加入新核心項目:
title Red Hat Linux (2.4.23-kgdb)
root (hd0,0)
kernel /boot/vmlinuz-2.4.23-kgdb ro root=/dev/hda1
並在 root 目錄建立一個腳本 debugkernel:
#!/bin/bash gdbstart -s 115200 -t /dev/ttyS1 <<EOF EOF
加上執行權限(chmod +x debugkernel)後,這個腳本的作用是呼叫 gdbstart 設定埠與速率,並讓核心進入除錯狀態。
在 target 上重開機、選擇 2.4.23-kgdb 核心,啟動完成後執行 ./debug,核心會停下來等待 developer 的連線:
About to activate GDB stub in the kernel on /dev/ttyS1 Waiting for connection from remote gdb...
在 developer 上:
# cd /home/liangjian/linux-2.4.23 # gdb vmlinux (gdb) rmt breakpoint () at kgdbstub.c:1005
此時核心已進入除錯狀態,可以像除錯使用者程式一樣查變數、設中斷點、看堆疊:
(gdb) bt #0 breakpoint () at kgdbstub.c:1005 #1 0xc0387f48 in init_task_union () #2 0xc01bc867 in gdb_interrupt (irq=3, dev_id=0x0, regs=0xc0387f98) at gdbserial.c:158 ... (gdb) p jiffies $1 = 76153 (gdb) continue Continuing.
kgdb 可以在引導階段介入,但並非整個引導過程都可除錯。在 kgdb 1.9 中,鉤子被插在 init/main.c 的 start_kernel() 裡、呼叫 gdb_hook() 之前是 smp_init(),因此 smp_init() 之前的初始化過程無法除錯。要讓核心在引導時就進入除錯狀態,把 grub.conf 改成:
title Red Hat Linux (2.4.23-kgdb)
root (hd0,0)
kernel /boot/vmlinuz-2.4.23-kgdb ro root=/dev/hda1 gdb gdbttyS=1 gdbbaud=115200
引導後核心會短暫運行再暫停並印出 Waiting for connection from remote gdb...;developer 端一樣下 rmt,接著就能設中斷點了:
(gdb) b do_basic_setup Breakpoint 1 at 0xc0388913: file current.h, line 9. (gdb) continue [New Thread 1] [Switching to Thread 1] Breakpoint 1, do_basic_setup () at current.h:9
要除錯核心模組,需要 gdb 的額外支援。kgdb 主頁提供的 gdbmod 修正了 gdb 6.0 解析模組位址時的錯誤,可以正確除錯模組。先寫一個測試模組 orig.c:
void xcspy_func()
{
printk("<1>xcspy_func\n");
printk("<1>aaaaaaaaaaa\n");
}
int xcspy_init()
{
printk("<1>xcspy_init_module\n");
return 0;
}
void xcspy_exit()
{
printk("<1>xcspy_cleanup_module\n");
}
module_init(xcspy_init);
module_exit(xcspy_exit);
編譯並送到 target:
# cd /home/liangjian/lkm
# gcc -D__KERNEL__ -DMODULE -I/home/liangjian/linux-2.4.23/include \
-O -Wall -g -c -o orig.o orig.c
# scp orig.o root@192.168.16.30:/root
然後用 gdbmod 啟動除錯,設定符號檔搜尋路徑、下 rmt,中斷點設在核心呼叫模組 init 函式的地方(module.c 第 566 行的 mod->init):
(gdb) set solib-search-path /home/liangjian/lkm (gdb) rmt (gdb) b module.c:566 Breakpoint 1 at 0xc011cd83: file module.c, line 566. (gdb) c Continuing. [New Thread 1352] [Switching to Thread 1352]
這時在 target 上執行 insmod orig.o,developer 端就會停在中斷點,再用 step 進入模組的 init 函式:
Breakpoint 1, sys_init_module (name_user=0xc03401bc, mod_user=0x80904d8) at module.c:566
566 if (mod->init && (error = mod->init()) != 0) {
(gdb) step
xcspy_init () at orig.c:12
12 printk("<1>xcspy_init_module\n");
除錯模組的非 init 函式比較簡單:先在 target 執行 insmod orig.o,符號被載入後就能直接在 gdb 裡對目標函式設中斷點(如 b xcspy_func),後續該函式被呼叫時便進入除錯狀態。
除錯 init 函式則麻煩一些,因為執行 insmod 之前符號還沒進記憶體,無法直接設中斷點。有兩種變通做法:一是如上所述,在核心呼叫模組 init 之前的位置設中斷點(如 b sys_init_module 或 b module.c:566);二是先讓核心保持運行,在 target 上跑一遍 insmod orig.o 讓符號進記憶體,接著在 gdb 中對 init 函式設中斷點(如 b xcspy_init),然後在 target 上 rmmod orig.o,下次重新載入時就會停在 init 函式入口。