首页 > 系统相关 >Debugging the Ubuntu kernel with GDB and QEMU

Debugging the Ubuntu kernel with GDB and QEMU

时间:2022-11-11 17:02:54浏览次数:63  
标签:kernel console Debugging -- VM GDB ubuntu debug

参考:https://mhcerri.github.io/posts/debugging-the-ubuntu-kernel-with-gdb-and-qemu/

This tutorial focuses on the practical aspects of preparing an Ubuntu libvirt virtual machine to debug the ubuntu kernel.

Preparing the VM

Create an Ubuntu VM using uvt-kvm or virt-manager with the desired configurations.

On the host machine, edit the VM domain XML:

virsh edit "$GUESTNAME" 

Replacing the first line:

<domain type='kvm'> 

With:

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
    <qemu:commandline>
        <qemu:arg value='-s'/>
    </qemu:commandline> 

-s tells QEMU to start a GDB server on port 1234 (it’s equivalent to -gdb tcp::1234). -S can be appended to pause the VM before starting to run.

Optional configurations

Serial console

With serial console, you can follow the guest boot via virsh console or virsh start --console.

echo 'GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT console=tty0 console=ttyS0,38400n8"
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --word=8 --parity=no --stop=1"' |
sudo tee /etc/default/grub.d/99console.cfg
sudo update-grub 

mDNS daemon

With mDNS daemon you can access the VM via ssh using a .local domain.

On guest:

apt-get install avahi-daemon 

On host:

ssh "${GUEST_USER}@${GUEST_DOMAIN}.local" 

Obtaining the debug symbols

In order to avoid including additional repositories to the host machines, the following steps uses the guest VM to download the debug packages.

On the guest, include the repository for debug packages:

# Add repos
echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse
deb http://ddebs.ubuntu.com $(lsb_release -cs)-updates main restricted universe multiverse
deb http://ddebs.ubuntu.com $(lsb_release -cs)-proposed main restricted universe multiverse" | \
sudo tee -a /etc/apt/sources.list.d/ddebs.list
# Import keys
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 428D7C01 C8CAB6595FDFF622
# Update package list
apt-get update 

Install the package with the debug symbols for the current kernel:

apt install "linux-image-$(uname -r)-dbgsym" 

On the host, copy the debug symbol to some location:

scp -r "${GUEST_USER}@${GUEST_DOMAIN}.local:/usr/lib/debug" /tmp/ 

Debugging the kernel

Clone the corresponding kernel git repository, for example:

git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/xenial linux-xenial
cd linux-xenial
git checkout Ubuntu-4.4.0-112.135 

Start the VM (on a terminal apart):

virsh start --console "" 

Enable auto-loading of GDB scripts:

echo "add-auto-load-safe-path $HOME" >> ~/.gdbinit 

Start GDB pointing to the vmlinux:

gdb -tui /tmp/debug/boot/vmlinux-4.4.0-112-generic 

Change the kernel version as appropriate

In GDB console, attach to the VM:

target remote :1234 

(?) Load main kernel symbols: lx-symbols fakeroot debian/rules clean binary-headers binary-generic binary-perarch

Set the source path:

set substitute-path /build/linux-HSAA8v/linux-4.4.0/ . 

The original base path will vary from kernel to kernel.

Set a breakpoint:

b <some point> 

And continue:

c 

Use Ctrl-x Ctrl-a to switch to tui and follow the source code.

  1. RHEL - Debugging a kernel in QEMU/libvirt.
  2. Ubuntu Wiki - Debug Symbol Packages .
  3. GDB docs - Specifying Source Directories.

标签:kernel,console,Debugging,--,VM,GDB,ubuntu,debug
From: https://www.cnblogs.com/kuangke/p/16881037.html

相关文章

  • C温故补缺(四):GDB
    gdbgdb是由GNU软件社区提供的CDebug工具Pre在调试前,需要先编译.c程序,且要加上-g使输出文件变得可调式gcctest.c-g-otest用gdbtest来调试程序,用quit退出调试......
  • RK3568开发笔记(五):在虚拟机上使用SDK编译制作uboot、kernel和ubuntu镜像
    前言  buildroot虽然灵活,但是基于实际情况,本身是侧重驱动和应用定制开发的只定制一次文件系统投入有点多,还不如直接ubunt自己交叉编译依赖库,做一些库的移植裁剪。 ......
  • GDB_print_derived_type
    GDBprintderivedtype当我们在使用GDB调试时,print默认打印多态指针或多态引用的声明类型,如果我们需要查看其实际的派生类类型,方法是:首先在GDB命令行输入命令:setpr......
  • Linux命令基础——makefile+gdb+IO
    在学习Linux命令基础总结了笔记,并分享出来。08-linux-day03(makefile-gdb-IO)目录:附:ftp工具介绍——FlashFXP一、学习目标二、makefile1、makefile编写12、makefile编写23、......
  • GDB-3——GDB实用调试技巧
     1.将print显示的字符串或字符数组显示完整当我们使用print命令打印一个字符串或者字符数组时,如果该字符串太长,print命令默认显示不全的,我们可以通过在gdb中输......
  • GDB-4——Android设备中使用GDB调试程序的例子
     一、一个简单的例子1.实验Demo(1)Android.mkLOCAL_PATH:=$(callmy-dir)include$(CLEAR_VARS)LOCAL_SRC_FILES:=mytest.cLOCAL_MODULE:=mytestLOCAL_C_F......
  • GDB-2——GDB调试多线程
     一、简介前一博文实际上已经介绍了多线程的调试方法,这节专门进行一下总结。 二、调试多线程1.使用gdb将程序跑起来,然后按Ctrl+C将程序中断下来,使用infoth......
  • 关于 使用不同的交叉编译工具链,对gdb-8.1.1进行编译的结果
    gdb-8.1.1解压编译,使用厂家自带交叉编译工具../configureCC=aarch64-buildroot-linux-gnu-gcc-host=aarch64-buildroot-linux-gnu-target=aarch64-buildroot-linux-gnum......
  • 如何实现nvidia显卡的cuda的多kernel并发执行???
    相关:CPU端多进程/多线程调用CUDA是否可以加速??? ====================================================......
  • build opencv with qt to make debugging easier
    最近发现使用自己编译的opencv,可以增强opencv的imshow函数功能。从哪里得到的线索已经忘记了。可能是无意中发现的某一张截图吧。要实现cv::imshow函数功能增强,需要在cmak......