linux下qemu调试linux内核

编译内核

  • 下载kernel源码
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
cd linux-stable/

-配置内核
make menuconfig:
在kernel hacking下的选项中选中(实用空格键,星号*代表选选中)compile the kernel with debug info
注意【记得提前安装libncurses5-dev】,或者其他编译内核需要的依赖包

│ │ [*] 64-bit kernel │ │
│ │ General setup —> │ │
│ │ [*] Enable loadable module support —> │ │
│ │ -*- Enable the block layer —> │ │
│ │ Processor type and features —> │ │
│ │ Power management and ACPI options —> │ │
│ │ Bus options (PCI etc.) —> │ │
│ │ Executable file formats / Emulations —> │ │
│ │ [*] Networking support —> │ │
│ │ Device Drivers —> │ │
│ │ Firmware Drivers —> │ │
│ │ File systems —> │ │
│ │ Kernel hacking —> │ │
│ │ Security options —> │ │
│ │ -*- Cryptographic API —> │ │
│ │ [*] Virtualization —> │ │
│ │ Library routines —> │ │

按K键选中Kernel hacking之后按enter键进入子选项,按K键选移动到 Kernel debugging选项并按空格键选中为星号*(如下)

│ │
[*] Magic SysRq key │ │
│ │ (0x1) Enable magic SysRq key functions by default │ │
│ │ [*] Enable magic SysRq key over serial │ │
│ │ [*] Kernel debugging │ │
│ │ Memory Debugging —> │ │
│ │ [ ] Code coverage for fuzzing │ │
│ │ [ ] Debug shared IRQ handlers │ │
│ │ Debug Lockups and Hangs —>
进入kernel debuging 选中compile the kernel with debug info选项
保存退出

  • 执行编译
    make -j16编译内核
    编译完成之后生成bzImage和vmlinux二进制内核镜像就可以用来调试。
linux-stable$ ls arch/x86_64/boot/bzImage -lh
lrwxrwxrwx 1 emindsoft emindsoft 22 1022 14:32 arch/x86_64/boot/bzImage -> ../../x86/boot/bzImage
linux-stable$ ls -lh
-rwxrwxr-x   1 emindsoft emindsoft 443M 1021 11:52 vmlinux

更新gdb

详细信息可以查看这里修改方法这里写代码片
由gdb-7.8.tar.gz下载最新的gdb(7.8)到/opt目录下【目录随意选】
解压缩:tar zxvf gdb-7.8.tar.gz
修改代码【否则调试内核会出现remote ‘g’ packet reply is too long的问题】
cd gdb-7.8/gdb
vim remote.c
按照如图所示修改代码【针对7.8版本】:

代码如下:

if (buf_len > 2 * rsa->sizeof_g_packet) {    
      rsa->sizeof_g_packet = buf_len ;    
      for (i = 0; i < gdbarch_num_regs (gdbarch); i++)    
      {    
         if (rsa->regs->pnum == -1)    
         continue;    

         if (rsa->regs->offset >= rsa->sizeof_g_packet)    
         rsa->regs->in_g_packet = 0;    
         else    
         rsa->regs->in_g_packet = 1;    
      }    
   }    

安装gdb
在主目录gdb-7.8下依次运行命令(你也可以选择自己想要的version):

./configure
make
make install

启动qemu

  • 在启动之前,我们需要initrd.img文件,至于该文件的作用大家可自行百度。这里我们直接从宿主ubuntu系统中cp过来:
    cd linux-stable/
    cp /boot/initrd.img-4.4.0-97-generic ./
  • 开始启动
    cd linux-stable/
    qemu-system-x86_64 -S -kernel arch/x86_64/boot/bzImage -initrd ./initrd.img-4.4.0-97-generic -m 1024
    // -m表示非配内存大小为128Mb, -S表示以调试模式启动,暂定在启动界面
    // -S表示“冷冻”虚拟机,等待调试器发出继续运行命令;
    // -kernel表示要调试的内核镜像
    注: 这里并没有制作文件系统,大家可以更具自己的需求添加

同时摁ctrl+alt+2进入qemu命令行界面, 输入gdbserver tcp::1234,建立并等待gdb连接(如下图)
这里写图片描述

  • 摁ctrl+alt+1返回qemu
    另外开一个终端到linux-stable主目录下,输入命令:gdb vmlinux
    在gdb下输入:target remote localhost:1234 连接qemu中开启的gdbserver(如下图)
    这里写图片描述
    设置断点【举例:b do_fork】
    输入命令c开始执行到断点处
    输入命令n执行下一个语句

参考链接:
ubuntu14.04 下qemu调试linux内核

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值