linux双机调试

查看内核版本: $ 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 aptitudeudo 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_1Linux系统为/tmp/serial

debuggee启动设置:ubuntu默认会选择进入一个最新的kernel,也可以在开机时按住SHIFT选择想进入的kernel版本。

注意debuggee至少2个核;打印机设备可能占用/dev/ttyS0设备,因此在debuggingdebuggee中,均删除这个硬件。

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存放的真实路径。

哇,这个速度真的感人。。。

参考:

https://xz.aliyun.com/t/4133

https://bbs.pediy.com/thread-249192.htm

posted on 2019-10-08 10:50  bsauce  阅读(1727)  评论(0编辑  收藏  举报