2022
我们一起努力

怎么使用cgdb + qemu调试linux内核模块

本篇内容主要讲解“怎么使用cgdb + qemu调试linux内核模块”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用cgdb + qemu调试linux内核模块”吧!

如何使用cgdb + qemu调试linux内核模块

前言

Linux 代码庞大而繁杂,光看代码会使人头晕目眩,如果能通过调试工具对其代码执行流程进行调试,则对学习Linux kernel以及解决平时遇到的问题会大有帮助。本文将讲解如何使用cgdb + qemu的方式调试Linux内核代码,所使用的测试机操作系统版本是CentOS Linux release 7.2.1511 (Core)

1.编译额内核

1) 获取内核代码

内核代码下载地址:[
https://www.kernel.org/] (
https://www.kernel.org/),本文以4.9.153版本作为演示. 如下图,点击对应版本的 tarball 链接下载

下载完成后将tar文件拷贝到测试机/root目录并进行解压。

# cd /root
# tar xf linux-4.9.153.tar.xz
2) 编译出支持调试的内核
配置编译选项
# cd linux-4.9.153
# make menuconfig

定位到Enable loadable module support:

按空格键去掉选项Module signature verification,防止加载模块时如下报错:
module verification failed: signature and/or required key missing - tainting kernel

定位到Exit按钮,回到上级菜单。

定位到File systems, 按回车键:

选中EXT4 debugging support
JBD2 (ext4) debugging support 两项:

定位到Exit按钮,回到上级菜单。

定位到Kernel hacking,按回车键:

定位到Kernel debugging,按空格键选中。

定位到Compile-time checks and compiler options, 按回车键。

分别定位到
Compile the kernel with debug info
Provide GDB scripts for kernel debugging , 并按空格键选中。

保存,退出

开始编译
make -j 30

-j 30 表示并行编译的CPU核数

2.构建initramfs根文件系统

这里借助Busybox构建极简initramfs提供基本的用户态可执行程序.

1) 编译Busybox

[下载busybox-1.28.0] (
https://busybox.net/downloads/busybox-1.28.0.tar.bz2),并拷贝到测试机/root目录下解压。

配置CONFIG_STATIC参数,可编译出静态版Busybox, 使Busybox的可执行文件不依赖动态库,方便构建initramfs

# cd /root/busybox-1.28.0
# make menuconfig

选择Settings, 按回车。

选择Build static binary (no shared libs), 按回车。

退出,提示保存,选Yes

开始编译

# yum install glibc-static -y
# gcc --version
  gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
# make -j 30
# make install
2) 创建initramfs

创建initramfs, 其中包含BusyBox可执行程序,必要的设备文件,启动脚本init和需要调试的模块。在init脚本里只挂载了虚拟文件系统procfs和sysfs,没有挂载磁盘根文件系统,所有调试操作都在内存中进行,不会读写磁盘。本例中使用ext4.ko模块作为演示,所以需要将ext4.ko及其依赖模块一起放到initramfs。

# mkdir initramfs
# cd initramfs
# cp ../_install/* -rf ./
# mkdir dev proc sys
# sudo cp -a /dev/{null, console, tty1, tty2, tty3, tty4} dev/
# rm linuxrc
# touch init
# chmod a+x init
# mkdir -p lib/modules/4.9.153/
# cp /root/linux-4.9.153/fs/ext4/ext4.ko lib/modules/4.9.153/
# cp /root/linux-4.9.153/fs/jbd2/jbd2.ko lib/modules/4.9.153/
# cp /root/linux-4.9.153/fs/mbcache.ko lib/modules/4.9.153/
# ls
bin  dev  init  lib  proc  sbin  sys  usr

init文件的内容

#!/bin/busybox sh
mount -t proc mone /proc
mount -t sysfs none /sys
mdev -s
exec /sbin/init

打包initramfs:

# find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz

3.启动虚拟机

# yum install qemu-system-x86-2.0.0
# cd ..
# pwd
/root/busybox-1.28.0
# qemu-system-x86_64 -s -kernel /root/linux-4.9.153/arch/x86_64/boot/bzImage -initrd initramfs.cpio.gz -nographic -append "console=ttyS0"  -m 1G

qemu-system-x86_64 命令用到的参数说明:

  • -s
    -gdb tcp::1234 的缩写,表示监听1234端口,在gdb中可以通过
    target remote localhost:1234 连接;

  • -kernel 指定编译好的调试版内核;

  • -initrd 指定制作好的initramfs;

    怎么使用cgdb + qemu调试linux内核模块
  • -nographic 取消图形输出窗口,使QEMU成为简单的命令行程序;

  • -append console=ttyS0 将输出重定向到console,将会显示在标准输出中stdio, 注:这里ttyS0中的S大写;

  • -m 1G 设置虚拟机内存大小。

启动完成后可按回车键进入命令行交互界面

...
[    1.645828] Freeing unused kernel memory: 836K
[    1.659842] Freeing unused kernel memory: 748K
can't run '/etc/init.d/rcS': No such file or directory
Please press Enter to activate this console. [    2.144752] tsc: Refined TSC clocksource calibration: 2194.896 MHz
[    2.145315] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x1fa35d3c521, max_idle_ns: 440795261667 ns
[    2.377779] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
[    3.153834] clocksource: Switched to clocksource tsc
/ # ls
bin   dev   init  proc  root  sbin  sys   usr
/ #

4.使用cgdb进行调试

cgdb 是gdb的一个增强版,调试的时候看代码会美观许多。我们将在另外的一个窗口登录测试机,运行cgdb调试内核。

# yum install cgdb -y
# cgdb -v
  CGDB 0.6.8
# cd /root/linux-4.9.153
# cgdb vmlinux

在gdb命令行里输入target remote :1234进行远程调试,在函数register_filesystem里设置断点后输入c回车,然后在虚拟机里运行命令modprobe ext4加载ext4文件系统模块即可进入函数register_filesystem命中断点。命中断点后,我们打印fs->name,发现是ext3,这是因为在ext4的模块初始化函数ext4_init_fs里先调用了register_as_ext3();之后又调用了register_as_ext2();
register_filesystem(&ext4_fs_type);

(gdb) target remote :1234
Remote debugging using :1234
native_safe_halt () at ./arch/x86/include/asm/irqflags.h:57
(gdb) b register_filesystem
Breakpoint 1 at 0xffffffff81257dd0: file fs/filesystems.c, line 70.
(gdb) c
Continuing.
Breakpoint 1, register_filesystem (fs=0xffffffffa00a0ac0) at fs/filesystems.c:70
(gdb) p fs->name
$1 = 0xffffffffa0095cc0 "ext3"
(gdb)

到此,相信大家对“怎么使用cgdb + qemu调试linux内核模块”有了更深的了解,不妨来实际操作一番吧!这里是云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

赞(0)
文章名称:《怎么使用cgdb + qemu调试linux内核模块》
文章链接:https://www.fzvps.com/113832.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!