本篇內(nèi)容主要講解“怎么使用cgdb + qemu調(diào)試linux內(nèi)核模塊”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用cgdb + qemu調(diào)試linux內(nèi)核模塊”吧!
在成都網(wǎng)站建設(shè)、做網(wǎng)站中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報的無錫營銷推廣。創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站建設(shè)10余年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。
Linux 代碼龐大而繁雜,光看代碼會使人頭暈?zāi)垦?,如果能通過調(diào)試工具對其代碼執(zhí)行流程進(jìn)行調(diào)試,則對學(xué)習(xí)Linux kernel以及解決平時遇到的問題會大有幫助。本文將講解如何使用cgdb + qemu的方式調(diào)試Linux內(nèi)核代碼,所使用的測試機(jī)操作系統(tǒng)版本是CentOS Linux release 7.2.1511 (Core)
內(nèi)核代碼下載地址:[
https://www.kernel.org/] (
https://www.kernel.org/),本文以4.9.153版本作為演示. 如下圖,點(diǎn)擊對應(yīng)版本的 tarball 鏈接下載
下載完成后將tar文件拷貝到測試機(jī)/root目錄并進(jìn)行解壓。
# cd /root # tar xf linux-4.9.153.tar.xz
# cd linux-4.9.153 # make menuconfig
定位到Enable loadable module support
:
按空格鍵去掉
選項(xiàng)Module signature verification
,防止加載模塊時如下報錯:module verification failed: signature and/or required key missing - tainting kernel
定位到Exit
按鈕,回到上級菜單。
定位到File systems
, 按回車鍵:
選中EXT4 debugging support
和
JBD2 (ext4) debugging support
兩項(xiàng):
定位到Exit
按鈕,回到上級菜單。
定位到Kernel hacking
,按回車鍵:
定位到Kernel debugging
,按空格鍵選中。
定位到Compile-time checks and compiler options
, 按回車鍵。
分別定位到
Compile the kernel with debug info
和
Provide GDB scripts for kernel debugging
, 并按空格鍵選中。
保存,退出
make -j 30
-j 30
表示并行編譯的CPU核數(shù)
這里借助Busybox構(gòu)建極簡initramfs提供基本的用戶態(tài)可執(zhí)行程序.
[下載busybox-1.28.0] ( https://busybox.net/downloads/busybox-1.28.0.tar.bz2),并拷貝到測試機(jī)/root目錄下解壓。
配置CONFIG_STATIC
參數(shù),可編譯出靜態(tài)版Busybox, 使Busybox的可執(zhí)行文件不依賴動態(tài)庫,方便構(gòu)建initramfs
# cd /root/busybox-1.28.0 # make menuconfig
選擇Settings
, 按回車。
選擇Build static binary (no shared libs)
, 按回車。
退出,提示保存,選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
創(chuàng)建initramfs, 其中包含BusyBox可執(zhí)行程序,必要的設(shè)備文件,啟動腳本init和需要調(diào)試的模塊。在init腳本里只掛載了虛擬文件系統(tǒng)procfs和sysfs,沒有掛載磁盤根文件系統(tǒng),所有調(diào)試操作都在內(nèi)存中進(jìn)行,不會讀寫磁盤。本例中使用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文件的內(nèi)容
#!/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
# 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 命令用到的參數(shù)說明:
-s
是
-gdb tcp::1234
的縮寫,表示監(jiān)聽1234端口,在gdb中可以通過
target remote localhost:1234
連接;
-kernel
指定編譯好的調(diào)試版內(nèi)核;
-initrd
指定制作好的initramfs;
-nographic
取消圖形輸出窗口,使QEMU成為簡單的命令行程序;
-append console=ttyS0
將輸出重定向到console,將會顯示在標(biāo)準(zhǔn)輸出中stdio, 注:這里ttyS0中的S大寫;
-m 1G
設(shè)置虛擬機(jī)內(nèi)存大小。
啟動完成后可按回車鍵進(jìn)入命令行交互界面
... [ 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 / #
cgdb 是gdb的一個增強(qiáng)版,調(diào)試的時候看代碼會美觀許多。我們將在另外的一個窗口登錄測試機(jī),運(yùn)行cgdb調(diào)試內(nèi)核。
# yum install cgdb -y # cgdb -v CGDB 0.6.8 # cd /root/linux-4.9.153 # cgdb vmlinux
在gdb命令行里輸入target remote :1234
進(jìn)行遠(yuǎn)程調(diào)試,在函數(shù)register_filesystem
里設(shè)置斷點(diǎn)后輸入c
回車,然后在虛擬機(jī)里運(yùn)行命令modprobe ext4
加載ext4文件系統(tǒng)模塊即可進(jìn)入函數(shù)register_filesystem
命中斷點(diǎn)。命中斷點(diǎn)后,我們打印fs->name
,發(fā)現(xiàn)是ext3
,這是因?yàn)樵趀xt4的模塊初始化函數(shù)ext4_init_fs
里先調(diào)用了register_as_ext3();
之后又調(diào)用了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 + qemu調(diào)試linux內(nèi)核模塊”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文名稱:怎么使用cgdb+qemu調(diào)試linux內(nèi)核模塊
本文URL:http://www.rwnh.cn/article22/igehjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、軟件開發(fā)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)