Tutorial: Debugging Linux Kernel with GDB under VirtualBox
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.
- Install VirtualBox 4.1.x. Other versions are currently not supported!
- Download VBoxGDB and unpack the archive.
- Create a Linux VM in VirtualBox and install Linux there.Ensure that you have disabled hardware virtualization! Otherwise breakpoints won't work:
- 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 - Run "make menuconfig" and enable "Kernel Hacking->Compile kernel with debug info":
- Build the Linux kernel by running the following commands:
make
make modules - 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 - 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.
- Install the new kernel on the Linux machine:
sudo make modules_install
sudo make install - 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:
- Start your VM again. Wait until Linux starts up.
- 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.
- 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>
- If you have copied Linux sources to your Windows machine, tell GDB to search for sources in that directory:
dir <directory-with-linux-source>
- Connect GDB to your Linux kernel by running the following command inside GDB:
target remote :2000
- Congratulations! You can now use normal GDB commands to debug your Linux kernel with GDB.
- If you are familiar with the VirtualBox debugger, you can send commands to it using the "mon " command. E.g. "mon help".
- To try a simple debugging scenario, set a breakpoint on the sys_open() function and run the target:
b sys_open
c - Go to the VM and try launching an application. The breakpoint will soon trigger:
- 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.
- When you exit GDB, VBoxGDB will stop as well. Re-run it if you want to debug your VM again.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通