如何使用cgdb + qemu調試linux內核模塊

如何使用cgdb + qemu調試linux內核模塊

前言

Linux 代碼龐大而繁雜,光看代碼會使人頭暈目眩,如果能通過調試工具對其代碼執行流程進行調試,則對學習Linux kernel以及解決平時遇到的問題會大有幫助。本文將講解如何使用cgdb + qemu的方式調試Linux內核代碼,所使用的測試機操作系統版本是CentOS Linux release 7.2.1511 (Core)

1.編譯額內核

1) 獲取內核代碼

內核代碼下載地址:[https://www.kernel.org/] (https://www.kernel.org/),本文以4.9.153版本作爲演示. 如下圖,點擊對應版本的 tarball 鏈接下載 version

下載完成後將tar文件拷貝到測試機/root目錄並進行解壓。

# cd /root
# tar xf linux-4.9.153.tar.xz

2) 編譯出支持調試的內核

配置編譯選項
# cd linux-4.9.153
# make menuconfig

定位到Enable loadable module support:

version

按空格鍵去掉選項Module signature verification,防止加載模塊時如下報錯: module verification failed: signature and/or required key missing - tainting kernel

version

定位到Exit按鈕,回到上級菜單。

定位到File systems, 按回車鍵:

version

選中EXT4 debugging supportJBD2 (ext4) debugging support 兩項:

version

定位到Exit按鈕,回到上級菜單。

定位到Kernel hacking,按回車鍵:

version

定位到Kernel debugging,按空格鍵選中。

version

定位到Compile-time checks and compiler options, 按回車鍵。

version

分別定位到 Compile the kernel with debug infoProvide GDB scripts for kernel debugging , 並按空格鍵選中。

version

保存,退出

version

version

version

開始編譯
make -j 30

-j 30 表示並行編譯的CPU核數

2.構建initramfs根文件系統

這裏藉助Busybox構建極簡initramfs提供基本的用戶態可執行程序.

1) 編譯Busybox

[下載busybox-1.28.0] (https://busybox.net/downloads/busybox-1.28.0.tar.bz2),並拷貝到測試機/root目錄下解壓。

配置CONFIG_STATIC參數,可編譯出靜態版Busybox, 使Busybox的可執行文件不依賴動態庫,方便構建initramfs

# cd /root/busybox-1.28.0
# make menuconfig

選擇Settings, 按回車。

version

選擇Build static binary (no shared libs), 按回車。

version

退出,提示保存,選Yes

開始編譯

# yum install glibc-static -y
# gcc --version
  gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
# make -j 30
# make install

2) 創建initramfs

創建initramfs, 其中包含BusyBox可執行程序,必要的設備文件,啓動腳本init和需要調試的模塊。在init腳本里只掛載了虛擬文件系統procfs和sysfs,沒有掛載磁盤根文件系統,所有調試操作都在內存中進行,不會讀寫磁盤。本例中使用ext4.ko模塊作爲演示,所以需要將ext4.ko及其依賴模塊一起放到initramfs。

# mkdir initramfs
# cd initramfs
# cp ../_install/* -rf ./
# mkdir dev proc sys
# sudo cp -a /dev/{null, console, tty1, tty2, tty3, tty4} dev/
# rm linuxrc
# touch init
# chmod a+x init
# mkdir -p lib/modules/4.9.153/
# cp /root/linux-4.9.153/fs/ext4/ext4.ko lib/modules/4.9.153/
# cp /root/linux-4.9.153/fs/jbd2/jbd2.ko lib/modules/4.9.153/
# cp /root/linux-4.9.153/fs/mbcache.ko lib/modules/4.9.153/
# ls
bin  dev  init  lib  proc  sbin  sys  usr

init文件的內容

#!/bin/busybox sh
mount -t proc mone /proc
mount -t sysfs none /sys
mdev -s
exec /sbin/init

打包initramfs:

# find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz

3.啓動虛擬機

# yum install qemu-system-x86-2.0.0
# cd ..
# pwd
/root/busybox-1.28.0
# qemu-system-x86_64 -s -kernel /root/linux-4.9.153/arch/x86_64/boot/bzImage -initrd initramfs.cpio.gz -nographic -append "console=ttyS0"  -m 1G

qemu-system-x86_64 命令用到的參數說明:

  • -s-gdb tcp::1234 的縮寫,表示監聽1234端口,在gdb中可以通過 target remote localhost:1234 連接;
  • -kernel 指定編譯好的調試版內核;
  • -initrd 指定製作好的initramfs;
  • -nographic 取消圖形輸出窗口,使QEMU成爲簡單的命令行程序;
  • -append console=ttyS0 將輸出重定向到console,將會顯示在標準輸出中stdio, 注:這裏ttyS0中的S大寫;
  • -m 1G 設置虛擬機內存大小。

啓動完成後可按回車鍵進入命令行交互界面

...
[    1.645828] Freeing unused kernel memory: 836K
[    1.659842] Freeing unused kernel memory: 748K
can't run '/etc/init.d/rcS': No such file or directory

Please press Enter to activate this console. [    2.144752] tsc: Refined TSC clocksource calibration: 2194.896 MHz
[    2.145315] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x1fa35d3c521, max_idle_ns: 440795261667 ns
[    2.377779] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
[    3.153834] clocksource: Switched to clocksource tsc

/ # ls
bin   dev   init  proc  root  sbin  sys   usr
/ #

4.使用cgdb進行調試

cgdb 是gdb的一個增強版,調試的時候看代碼會美觀許多。我們將在另外的一個窗口登錄測試機,運行cgdb調試內核。

# yum install cgdb -y
# cgdb -v
  CGDB 0.6.8
# cd /root/linux-4.9.153
# cgdb vmlinux

在gdb命令行裏輸入target remote :1234進行遠程調試,在函數register_filesystem裏設置斷點後輸入c回車,然後在虛擬機裏運行命令modprobe ext4加載ext4文件系統模塊即可進入函數register_filesystem命中斷點。命中斷點後,我們打印fs->name,發現是ext3,這是因爲在ext4的模塊初始化函數ext4_init_fs裏先調用了register_as_ext3();之後又調用了register_as_ext2();register_filesystem(&ext4_fs_type);

(gdb) target remote :1234
Remote debugging using :1234
native_safe_halt () at ./arch/x86/include/asm/irqflags.h:57
(gdb) b register_filesystem
Breakpoint 1 at 0xffffffff81257dd0: file fs/filesystems.c, line 70.
(gdb) c
Continuing.

Breakpoint 1, register_filesystem (fs=0xffffffffa00a0ac0) at fs/filesystems.c:70
(gdb) p fs->name
$1 = 0xffffffffa0095cc0 "ext3"
(gdb)

cgdb

作者:袁鑫【滴滴出行高級軟件開發工程師】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章