查看內核版本: $ uname -sr
一、選擇調試的linux kernel版本
注意:
選擇kernel版本時,既要能下載到image和header二進制文件,也要下載符號文件vmlinux和source code。
搜索命令:
搜索linux內核版本 apt-cache search linux-image | grep linux-image-4.10.0 |grep generic
搜索含有dbgsym的內核版本:apt-cache search linux-image | grep dbgsym |grep 4.10
也可以用aptitude搜索:sudo apt``-``get install aptitude
;udo aptitude search ``'linux-image-'``$(uname ``-``r)``'-dbgsym'
。
搜索特定source code的內核版本 apt-cache search linux-source
二、安裝kernel image、dbsym(就是vmlinux)、source code
1. 安裝kernel image
方法一:
搜索要下載的linux內核版本 apt-cache search linux-image | grep linux-image-4.10.0 |grep generic
安裝內核 sudo apt-get install linux-image-4.10.0-19-generic
查看已安裝的內核版本 sudo dpkg --list | grep linux-image
方法二:
http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.10/
https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/stable-review/
2. 安裝dbgsym符號文件
增加符號文件對應的source.list ,更新源文件。
# 增加source.list
codename=$(lsb_release -c | awk '{print $2}')
sudo tee /etc/apt/sources.list.d/ddebs.list << EOF
deb http://ddebs.ubuntu.com/ ${codename} main restricted universe multiverse
deb http://ddebs.ubuntu.com/ ${codename}-security main restricted universe multiverse
deb http://ddebs.ubuntu.com/ ${codename}-updates main restricted universe multiverse
deb http://ddebs.ubuntu.com/ ${codename}-proposed main restricted universe multiverse
EOF
# 添加訪問符號服務器的秘鑰文件
wget -O - http://ddebs.ubuntu.com/dbgsym-release-key.asc | sudo apt-key add -
# 更新源文件
sudo apt-get update
通過apt-get下載dbgsym,默認保存在/usr/lib/debug/boot中,文件名是vmlinux-4.10.0-19-generic。
$ sudo apt-get install linux-image-`uname -r`-dbgsym
3. 獲取kernel對應的源碼
(1)方法一:apt安裝
打開/etc/apt/sources.list,啟用deb-src,執行sudo apt-get update更新源文件:
$ sudo nano /etc/apt/sources.list
# 加一句 deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted
- 搜索所有的source code:
apt-cache search linux-source
- 安裝指定版本的source code:
sudo apt-get install linux-source-4.10.0
發現:第一種方法搜索kernel source code時只存在幾個大版本,如4.10.0,下載之后發現可能是4.10.0的一個細分版本:4.10.0-xx(Makefile中的前幾行定義了源碼的版本)。所以猜測實際上運行的kernel和source code還是有一些出入的,gdb調試的時候對應關系可能不太一致。
源碼路徑:默認下載的源碼會放在/usr/src/linux-source-4.4.0/linux-source-4.4.0.tar.bz2
。並將其解壓到/build/linux-Ay7j_C/linux-4.4.0
目錄下就可以在調試的時候看到源碼。原因是調試符號中包含的路徑是編譯時的硬編碼路徑,因此其他Ubuntu版本在調試時可找到這個硬編碼路徑,將源碼解壓到此處即可。
(2)下載linux kernel源碼切換分支
查看git倉庫地址:https://wiki.ubuntu.com/Kernel/Dev/KernelGitGuide
git clone git://kernel.ubuntu.com/ubuntu/ubuntu-xenial.git
git tag | grep 4.10.0-19
git checkout ubuntu-4.10.0-19........
復制ubuntu,一份debugging(調試機),一份debuggee(被調試機、目標機)
三、搭建雙機調試環境
1.原理
原理:vmware利用串口通信,配置debugging 的串口為server,debuggee的串口為client。
命名管道設置:雙機都設置,物理機是Windows
系統,則為//./pipe/com_1
。Linux
系統為/tmp/serial
。
debuggee
啟動設置:ubuntu默認會選擇進入一個最新的kernel,也可以在開機時按住SHIFT
選擇想進入的kernel版本。
注意:debuggee
至少2個核;打印機設備可能占用/dev/ttyS0設備
,因此在debugging
和debuggee
中,均刪除這個硬件。
2. 配置debuggee
讓debuggee開機時進入KGDB被調試狀態,需修改/etc/grub.d/40_custom
文件,增加grub引導時的菜單項(menuentry)。可參照/boot/grub/grub.cfg
文件,來修改gnulinux-simple-xx號和本機鏡像版本(其實就是加了一句kgdbwait kgdb8250=io,03f8,ttyS0,115200,4 kgdboc=ttyS0,115200 kgdbcon nokaslr
)。
$ sudo vim /etv/grub.d/40_custom
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
menuentry 'Ubuntu, KGDB with nokaslr' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-bf306d0a-28c8-49c6-bffc-446be272ddcf' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_msdos
insmod ext2
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 bf306d0a-28c8-49c6-bffc-446be272ddcf
else
search --no-floppy --fs-uuid --set=root bf306d0a-28c8-49c6-bffc-446be272ddcf
fi
echo 'Loading Linux 4.10.0-19 with KGDB built by GEDU lab...'
linux /boot/vmlinuz-4.10.0-19-generic root=UUID=bf306d0a-28c8-49c6-bffc-446be272ddcf ro find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US quiet kgdbwait kgdb8250=io,03f8,ttyS0,115200,4 kgdboc=ttyS0,115200 kgdbcon nokaslr
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-4.10.0-19-generic
}
修改完后,執行命令:
$ sudo update-grub
$ reboot
3.配置debugging
調試腳本即gdb
所執行的命令,用於遠程調試debuggee
。此腳本需要sudo
執行。
gdb \
-ex "add-auto-load-safe-path $(pwd)" \
-ex "file /usr/lib/debug/boot/vmlinux-4.4.0-21-generic" \
-ex 'set arch i386:x86-64:intel' \
-ex 'target remote /dev/ttyS0' \
-ex 'continue' \
-ex 'disconnect' \
-ex 'set arch i386:x86-64' \
-ex 'target remote /dev/ttyS0'
4.進入調試
debuggee
啟動時,按住shift
,出現如下界面,選擇KGDB with nokaslr
。
debugging
中執行sudo ./gdb_kernel
,就可以遠程調試。
源碼查看:在gdb中使用l
可以查看相應的源碼,但是由於vmlinux編譯時似乎是使用絕對路徑編譯的,因此可以建立相應的絕對路徑把source code放進去。還有一種方法是,設置set substitute-path PATH1 PATH2
,PATH1是vmlinux中的路徑信息,PATH2是source code存放的真實路徑。
哇,這個速度真的感人。。。