使用kgdb調試linux核心及核心模組

來源:互聯網
上載者:User
1. 幾種核心調試工具比較

kdb:只能在彙編代碼級進行調試;
     優點是不需要兩台機器進行調試。

gdb:在調試模組時缺少一些至關重要的功能,它可用來查看核心的運行情況,包括反組譯碼核心功能。

kgdb:能很方便的在源碼級對核心進行調試,缺點是kgdb只能進行遠端偵錯,它需要一根串口線及兩台機器來調試核心(也可以是在同一台主機上用vmware軟體運行兩個作業系統來調試)

使用kdb和gdb調試核心的方法相對比較簡單,這裡只描述如何使用kgdb來調試核心。

2.軟硬體準備

環境:
一台開發機developer(192.168.16.5 com1),一台測試機target(192.168.16.30 com2),都預裝redhat 9;一根串口線

下載以下軟體包:
linux核心2.4.23         linux-2.4.23.tar.bz2
kgdb核心補丁1.9版       linux-2.4.23-kgdb-1.9.patch
可調試核心模組的gdb     gdbmod-1.9.bz2

3.ok,開始

3.1 測試串口線
物理串連好串口線後,使用一下命令進行測試,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

3.2 安裝與配置

3.2.1 安裝

下載linux-2.4.23.tar.bz2,linux-2.4.23-kgdb-1.9.patch,gdbmod-1.9.bz2到developer的/home/liangjian目錄

*在developer機器上

#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

使用scp進行將相關檔案拷貝到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提供的一個工具,用於啟用核心鉤子,使核心處於調試狀態

3.2.2 配置

*在developer機器上

在核心源碼目錄下編輯一檔案.gdbinit(該檔案用以對gdb進行初始化),內容如下:
#vi .gdbinit
define rmt
set remotebaud 115200
target remote /dev/ttyS0
end
#
以上在.gdbinit中定義了一個宏rmt,該宏主要是設定使用的串口號和速率

*在target機器上

編輯/etc/grub.conf檔案,加入以下行:
#vi /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,內容如下:
#vi debug
#!/bin/bash
gdbstart -s 115200 -t /dev/ttyS1 <<EOF

EOF
#chmod +x debugkernel
這個指令碼主要是調用gdbstart程式設定target機上使用的串口及其速率,並使核心處於調試狀態

3.3 開始調試

target上的核心或核心模組處於調試狀態時,可以查看其變數、設定斷點、查看堆棧等,並且是源碼級的調試,和用gdb調試使用者程式一樣

3.3.1 核心啟動後調試

*在target機器上

重啟系統,選擇以 2.4.23-kgdb核心啟動,啟動完成後運行debugkenel,
這時核心將停止運行,在控制台螢幕上顯示資訊,並等待來自developer的
串口串連

#./debug
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
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...

執行rmt宏
(gdb) rmt
breakpoint () at kgdbstub.c:1005
1005                    atomic_set(&kgdb_setting_breakpoint, 0);

這時target上的核心處於調試狀態,可以查看其變數、設定斷點、查看堆棧等,和用gdb調試使用者程式一樣

查看堆棧
(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
#3  0xc010937b in handle_IRQ_event (irq=3, regs=0xc0387f98, action=0xce5a9860)
at irq.c:452
#4  0xc0109597 in do_IRQ (regs=
      {ebx = -1072671776, ecx = -1, edx = -1070047232, esi = -1070047232, edi
= -1070047232, ebp = -1070039092, eax = 0, xds
= -1070071784, xes = -1070071784, orig_eax = -253, eip = -1072671729, xcs =
16, eflags = 582, esp = -1070039072, xss = -1072671582}) at irq.c:639
#5  0xc010c0e8 in call_do_IRQ ()

查看jiffies變數的值
(gdb) p jiffies
$1 = 76153

如果想讓target上的核心繼續運行,執行continue命令
(gdb) continue
Continuing.

3.3.2 核心在引導時調試

kgdb可以在核心引導時就對其進行調試,但並不是所有引導過程都是可調試的,如在kgdb 1.9版中,它在init/main.c的start_kernel()函數中插入以下代碼:
start_kernel()
{
    ......
        smp_init();
#ifdef CONFIG_KGDB
        if (gdb_enter) {
                gdb_hook();             /* right at boot time */
        }
#endif
    ......
}

所以在smp_init()之前的初始化引導過程是不能調試的。

另外要想讓target的核心在引導時就處於調試狀態,需要修改其/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 gdb gdbttyS=1 gdbbaud=115200

*在target機器上

引導2.4.23-kgdb核心,核心將在短暫的運行後暫停並進入調試狀態,列印如下資訊:
Waiting for connection from remote gdb...

*在developer機器上

#cd /home/liangjian/linux-2.4.23
# gdb vmlinux
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...

執行rmt宏
(gdb) rmt
breakpoint () at kgdbstub.c:1005
1005                    atomic_set(&kgdb_setting_breakpoint, 0);

查看當前堆棧
(gdb) bt
#0  breakpoint () at kgdbstub.c:1005
#1  0xc0387fe0 in init_task_union ()
#2  0xc01bc984 in gdb_hook () at gdbserial.c:250
#3  0xc0388898 in start_kernel () at init/main.c:443

在do_basic_setup函數處設定斷點,並讓核心恢複運行
(gdb) b do_basic_setup
Breakpoint 1 at 0xc0388913: file current.h, line 9.
(gdb) continue
Continuing.
[New Thread 1]
[Switching to Thread 1]

Breakpoint 1, do_basic_setup () at current.h:9
9               __asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));

核心在do_basic_setup斷點處停止運行後查看當前堆棧
(gdb) bt
#0  do_basic_setup () at current.h:9
(gdb)

3.3.3 核心模組調試調試

要想調試核心模組,需要相應的gdb支援,kgdb的首頁上提供了一個工具gdbmod,它修正了gdb 6.0在解析模組地址時的錯誤,可以用來正確的調試核心模組

*在developer機器上

寫了個測試用的核心模組orig,如下:
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);

編譯該模組:
#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 vmlinux
GNU gdb 6.0
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...

設定符號檔案的搜尋路徑
(gdb) set solib-search-path /home/liangjian/lkm

執行rmt宏
(gdb) rmt
breakpoint () at kgdbstub.c:1005
1005                    atomic_set(&kgdb_setting_breakpoint, 0);

設定斷點使得可以調試核心模組的init函數,查核心源碼可知,核心是通過module.c檔案的第566行(sys_init_module函數中)mod->init來調用模組的init函數的
(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則相應的在斷點處被暫停,如下
                                                                                                                          
Breakpoint 1, sys_init_module (name_user=0xc03401bc "/001",
mod_user=0x80904d8) at module.c:566
566             if (mod->init && (error = mod->init()) != 0) {

使用step命令進入模組的init函數
(gdb) step
xcspy_init () at orig.c:12
12              printk("<1>xcspy_init_module/n");
(gdb) n
15      }
(gdb)

說明:
調試核心模組的非init函數相對比較簡單,只要先在target上執行insmod orig.o,這時由於模組的符號被載入,可以直接在developer的gdb中對想調試的模組函數設定斷點,如bt xcspy_func,後面當xcspy_func被調用時就進入了調試狀態。
如果想調試核心模組的init函數,由於在執行insmod之前模組的符號還沒有被載入,不能直接對模組的init函數設定斷點,所以相對來說要困難一些。可以採用兩種變通的方法:1,採用上面介紹的在核心調用模組的 init函數被調用之前的某處插入斷點,如bt sys_init_module()或bt module.c:566;2,在developer上讓核心處於運行狀態,在target上先執行一遍insmod orig.o,這時orig.o的符號已經被載入到記憶體中,可以直接在developer的gdb中對模組的init函數設定斷點,如bt xcspy_init,然後在target上rmmod orig.o,當下次在target上重新載入orig.o時就進入了調試狀態,developer在xcspy_init處被暫停。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.