使用jtag+gdb調試arm上的linux內核和驅動

調試對象爲公司一塊使用s3c2440的板子,調試器爲基於ft2232d的openjtag,pc操做系統爲ubunut14.04.2 x64,jtag->gdb橋爲openocd 0.9.0。
1.準備內核源碼
帶調試信息的內核二進制文件體積過大,不適合放在ARM板上。
拷貝出兩份徹底同樣的內核源碼,不加調試信息的一份燒寫/下載到板子上,加調試信息的一份用於調試。這裏用uboot+nfs的方式下載內核。
~/buildspacce/linux-2.6.32.2_debug
~/buildspacce/linux-2.6.32.2_release
debug版源碼作以下配置
Kernel hacking  ---> [*] Kernel debugging
         [*] Compile the kernel with debug info
須要調試的驅動,編譯時指定的內核源碼路徑,應當是debug版內核源碼。
【一】從頭開始調試內核
1.配置openocd
2.上電
  鏈接arm板與openjtag
  鏈接openjtag與pc
  給arm板上電
3.打開一個終端,啓動openocdlinux

sudo /opt/openocd/bin/openocd -f ~/.openocd/openjtag.cfg -f /opt/openocd/share/openocd/scripts/target/samsung_s3c2440.cfg

4.打開一個終端,經過telnet訪問openocdshell

telnet 127.0.0.1:4444

在telnet中輸入以下指令,復位ARM並中止運行,以便在內核啓動前加斷點。
jtag的緣由,你的系統可能不會本身啓動起來,reset後纔會啓動uboot。ssh

> reset halt

5.另打開一個終端,切換到內核源碼根目錄(須要先配置並編譯),啓動gdb。ui

PATH=$PATH:/opt/arm-linux-gcc/s3c2440_4.4.3/bin
cd ~/buildspacce/linux-2.6.32.2_debug
arm-none-linux-gnueabi-gdb ./vmlinux -d .

在gdb中鏈接openocd,並設置內核進入地址爲斷點spa

(gdb) target remote 127.0.0.1:3333
Remote debugging using :3333
0x000000ac in ?? ()
(gdb) b *0x30008040

(gdb) c
Continuing.

Breakpoint 1, 0x30008040 in ?? ()

0x30008040這個地址來源於uboot的打印(更早的時候開了minicom)操作系統

Entry Point:  30008040

 接下來你就能夠從頭開始調試內核了。debug

【二】調試正在運行中的內核。
1.鏈接arm、openjtag、pc,啓動openocd。
2.使用uboot下載內核,或從nand/nor啓動加載內核,讓arm系統正常運行起來(shell可用)。
3.telnet到openocd,使用調試

halt

指令中止arm處理器。
4.啓動gdbcode

PATH=$PATH:/opt/arm-linux-gcc/s3c2440_4.4.3/bin
cd ~/buildspacce/linux-2.6.32.2_debug
arm-none-linux-gnueabi-gdb ./vmlinux -d .

在gdb中鏈接openocd,內核中止在休眠狀態。對象

(gdb) target remote :3333
Remote debugging using :3333 s3c24xx_default_idle () at arch/arm/mach-s3c2410/include/mach/system.h:40 40 for (i = 0; i < 50; i++) {

5.內核是你的了……

【三】調試內核模塊
1.鏈接arm、openjtag、pc,啓動openocd,讓系統正常運行起來。
2.經過arm的串口、telnet、ssh等方式加載目標驅動,這裏被調試的內核模塊爲spi_slave.ko
3.查看內核模塊的加載地址(虛擬地址)

# lsmod
spi_slave 3055 0 - Live 0xbf000000
# cat /sys/module/spi_slave/sections/.data
0xbf000730
# cat /sys/module/spi_slave/sections/.bss
0xbf00091c

4.將arm系統halt掉,啓動gdb,gdb中鏈接openocd,加載內核模塊的符號表

(gdb) target remote :3333
Remote debugging using :3333
s3c24xx_default_idle () at arch/arm/mach-s3c2410/include/mach/system.h:40
40        for (i = 0; i < 50; i++) {
(gdb) add-symbol-file ~/buildspace/spi_slave/spi_slave.o 0xbf000000 -s .data 0xbf000730 -s .bss 0xbf00091c 
add symbol table from file "/home/cjh/buildspace/spi_slave/spi_slave.o" at
    .text_addr = 0xbf000000
    .data_addr = 0xbf000730
    .bss_addr = 0xbf00091c
(y or n) y
Reading symbols from /home/cjh/buildspace/spi_slave/spi_slave.o...done.

 

 

目前常常沒法將驅動中的變量打印出來、沒法加斷點,緣由都是虛擬地址到物理地址的轉換有問題。解決方法還不明確

相關文章
相關標籤/搜索