Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux 核心

Linux 內核驅動開發:KGDB 單步調試內核實操作流程

如何單步調試 Linux 內核一直困擾著驅動開發人員——內核程式碼量大、邏輯複雜、又與硬體緊密交互,因此需要有別於應用程式的調試手段。統計顯示,內核開發者最普遍使用的仍是 printk,其他方法多在特殊情況下才出動。本文介紹的 Kgdb 屬於源碼級調試器,需搭配 gdb 使用,可對內核做單步執行、設定斷點、觀察變數與暫存器,功能與應用程式調試相近,但也有其限制條件(見原理篇)。本文只談使用流程。

Kgdb 調試原理及 kgdboe 方式配置請參閱博文《Linux內核驅動開發之KGDB原理介紹及kgdboe方式配置》。 https://www.linuxidc.com/Linux/2013-06/86406.htm

關鍵步驟

一、配置內核使其支援 KGDB。依次進行如下設定(以下為必須項):

 $make menuconfig(下面配置為必須進行的)

  Kernel hacking --->
     -*- Magic SysRq key
     [*] Kernel debugging
     [*] Compile the kernel with debug info
     [*] KGDB: kernel debugging with remote gdb --->
         <*> KGDB: use kgdb over the serial console

注意:博主已在內核中對 KGDB 進行串列埠驅動支援,方法請參考《嵌入式Linux應用開發完全手冊》相關章節 見 https://www.linuxidc.com/Linux/2011-01/31114.htm

設定完成後編譯內核,make uImage 並把 uImage 複製到 nfs/tftp 伺服器,等待 u-boot 下載,或直接燒進 flash 從 flash 啟動。

二、修改 u-boot 的 bootargs 以支援 kgdb:

setenv bootargs 'console=ttyS0,115200n8 kgdboc=ttyS0,115200 kgdbwait ...... nfsroot=......'

主要增加紅色字體部分,用來指定 kgdb 使用開發板上的哪一個串列埠。注意這裡填的是串列埠名稱,而不是 /dev/ttyS0 這樣的裝置檔路徑。

三、配置虛擬機與 PC 機的串列埠映射。例如博主的 Vmware 虛擬機使用 usb 轉串列埠,在 linux 下的裝置名為 /dev/ttyS1,可用 minicom 實際測試。

四、在虛擬機的 linux 下用 minicom 啟動開發板內核。先以 tftp 下載內核:

tftp進行下載內核
OMAP3_EVM # tftp
smc911x: detected LAN9220 controller
smc911x: phy initialized  #by guoqingbo
smc911x: MAC 02:01:05:96:02:01
Using smsc911x-0 device
TFTP from server 192.168.1.93; our IP address is 192.168.1.94
Filename 'uImage'.
Load address: 0x82000000
Loading: #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         ##########################
done
Bytes transferred = 2459200 (258640 hex)

用 bootm 啟動後,輸出會停在下面這幾行,等待 gdb 連線:

Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
serial8250.0: ttyS0 at MMIO 0x4806a000 (irq = 72) is a ST16654
console [ttyS0] enabled  #by guoingbo
serial8250.1: ttyS1 at MMIO 0x4806c000 (irq = 73) is a ST16654
serial8250.2: ttyS2 at MMIO 0x49020000 (irq = 74) is a ST16654
kgdb: Registered I/O driver kgdboc.
kgdb: Waiting for connection from remote gdb...

看到 Waiting for connection from remote gdb... 就代表目標端已就緒,此時在主機端以 gdb 遠程連線即可開始調試。

截圖來說明一下:

Copyright © Linux教程網 All Rights Reserved