关闭

Tutorial: Debugging Linux Kernel with GDB under VirtualBox

816人阅读 评论(0) 收藏 举报
分类:


http://sysprogs.com/VBoxGDB/tutorial/


This tutorial shows how to debug a Linux kernel running on VirtualBox using the VirtualBox debugger and VBoxGDB. In this tutorial we will use a Windows machine as a host machine and will run and debug Linux kernel inside VirtualBox.

  1. Install VirtualBox 4.1.x. Other versions are currently not supported!
  2. Download VBoxGDB and unpack the archive.
  3. Create a Linux VM in VirtualBox and install Linux there.Ensure that you have disabled hardware virtualization! Otherwise breakpoints won't work:
  4. Install Linux source code and prepare it for building. E.g. use the following commands on debian-based distros:
    sudo apt-get install linux-source libncurses5-dev
    mkdir mykernel
    cd mykernel
    tar xjf /usr/src/linux-source-<version>/linux-source-<version>.tar.bz2
    cd linux-source-<version>
    make oldconfig
  5. Run "make menuconfig" and enable "Kernel Hacking->Compile kernel with debug info":
  6. Build the Linux kernel by running the following commands:
    make
    make modules
  7. Copy the kernel to your Windows machine. E.g. you can do it by running creating a new Windows shared folder (e.g. 'share') and a new Windows user (e.g. virtual) and then mounting it from the Linux machine:
    sudo apt-get install smbfs
    sudo mkdir /mnt/share
    sudo smbmount //10.0.2.2/share /mnt/share -o user=virtual
    sudo cp vmlinux /mnt/share
  8. Once you have built the kernel but not installed it yet, it's a good time to make a VirtualBox snapshot. If anything goes wrong, you will be able to restore the snapshot later.
  9.  Install the new kernel on the Linux machine:
    sudo make modules_install
    sudo make install
  10. Shut down the Linux machine. Go to the directory where you have extracted VBoxGDB and run VBoxDebugMgr.exe. Select your VM from list and enable the "debugger enabled" checkbox. Alternatively you can change the the debugger port:
  11. Start your VM again. Wait until Linux starts up.
  12. Open Command Prompt and go to the directory where you have extracted VBoxGDB. Run the following command:
    VBoxGDB <port number>
    The port number should be the port you specified when enabling the debugger:VBoxGDB will connect to VirtualBox and start listening on port 2000 for GDB connections. If VBoxGDB fails to connect, close all VirtualBox-related processes (including VBoxSVC.exe) and enable the debugger again.
  13. Launch the cross-compiled GDB (provided with VBoxGDB) by running the following command in VBoxGDB directory:
    i686-linux-gnu-gdb.exe <path-to-your-vmlinux-file>
  14. If you have copied Linux sources to your Windows machine, tell GDB to search for sources in that directory:
    dir <directory-with-linux-source>
  15. Connect GDB to your Linux kernel by running the following command inside  GDB:
    target remote :2000
  16. Congratulations! You can now use normal GDB commands to debug your Linux kernel with GDB.
  17. If you are familiar with the VirtualBox debugger, you can send commands to it using the "mon " command. E.g. "mon help".
  18. To try a simple debugging scenario, set a breakpoint on the sys_open() function and run the target:
    b sys_open
    c
  19. Go to the VM and try launching an application. The breakpoint will soon trigger:
  20. You can use the normal GDB commands to examine the variable values, set breakpoints, etc. If you need to force the guest OS to stop when it's running (e.g. to set a breakpoint), press Ctrl-C either in GDB window or in the VBoxGDB window.
  21. When you exit GDB, VBoxGDB will stop as well. Re-run it if you want to debug your VM again.

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Linux内核之GDB基本调试方法

一般单板软件Linux内核出现crash如何采用GDB工具进行调试? 基本方法过程如下: 1. 为了测试GDB操作,故意在kernel/linux/fs/ioctl.c文件的do_vfs_ioct...
  • wuruixn
  • wuruixn
  • 2014-07-31 17:23
  • 7765

gdb调试linux系统调用

下载并编译内核 制作busybox所需要的rootfs 安装qemu 安装busybox 启动qemu进行调试 调试示例下载并编译内核内核源码可以在 https://www.kernel.org/pu...
  • lwyeluo
  • lwyeluo
  • 2017-03-18 17:18
  • 1129

GDB简单调试linux内核与模块的方法

gdb 对于看系统内部是非常有用. 在这个级别精通调试器的使用要求对 gdb 命令有信心, 需要理解目标平台的汇编代码,  以及对应源码和优化的汇编码的能力.      调试器必须把内核作为一...
  • wh8_2011
  • wh8_2011
  • 2016-07-28 07:16
  • 1606

debugging the linux kernel under ubuntu 11.04

以前有一段时间,因为某些原因,一直在使用windows xp,在上面用vmware装了一个CentOS5.5,后来升级到5.6,就一直在上面做一些内核的开发,在上面编译内核,在上面debug 内核。这...
  • lengyuex
  • lengyuex
  • 2011-05-22 22:11
  • 759

Debugging Under Unix: gdb Tutorial

Introduction This tutorial was originally written for CS 342 at Washington University. It is stil...
  • skyghost_s
  • skyghost_s
  • 2016-03-27 14:57
  • 164

Analyzing Linux kernel crash dumps with crash - The one tutorial that has it all

http://www.dedoimedo.com/computers/crash-analyze.html Finally, the big moment has come....
  • kyokowl
  • kyokowl
  • 2011-11-01 18:27
  • 2220

Button input interrupt under linux kernel 2.6.35.7 send message button event

//**************************************************************** // Button-ts.c // 2011-11-25 // L...
  • bmbm546
  • bmbm546
  • 2011-11-29 16:41
  • 889

linux kernel gdb

  • 2012-11-14 23:33
  • 4KB
  • 下载

Linux kernel --- VirtualBox

This article describe about how to roadmap android kernel on VirtualBox Software environment: Ubunt...
  • spark_fountain
  • spark_fountain
  • 2012-07-16 16:02
  • 1124

用QEMU+GDB 调试LINUX KERNEL

1。安装QEMU,安装个LINUX虚拟机。被调试的是虚拟机。 2。重新编译虚拟机里LINUX KERNEL,打开DEBUG相关设置,DEBUG_INFO。 3。用QEMU 启动被调试的虚拟机的时候...
  • pifangsione
  • pifangsione
  • 2013-08-29 14:43
  • 1413
    个人资料
    • 访问:1536942次
    • 积分:21233
    • 等级:
    • 排名:第415名
    • 原创:1816篇
    • 转载:1042篇
    • 译文:0篇
    • 评论:67条
    相册/老博客
    最新评论