Using KGDB to debug the kernel on QEMU

Source: Internet
Author: User
Tags bz2 mkdir
1: Compiling Linux + KGDB 1.1: Install the compilation tool

Please refer to other related tutorials and recommend that you select the compiler and support library 1.2 when installing the system : Download the latest kernel code

Download the latest kernel source in the, you can download the code directly using the browser, of course, you can use the command wget to download, download need to use tar to extract the source code. Here we choose linux- to demonstrate.

Suppose the default working directory is/usr/src/work? View Code BASH

sudo mkdir-p  /usr/src/work
sudo chmod 777/usr/src/work-r
wget http://
TAR-JXVF linux-
1.3: Configure kernel selection

enable KGDB debugging and use KGDBOC as communication module with GDB. View Code BASH

CD linux- make
defconfig make

make sure the following options are selected (refer to kgdb kernel option configuration)? View Code BASH

General Setup  ---> 
     [*] Prompt for development and/or incomplete code/drivers Kernel
      [*] Compile the kernel with debug Info
     [*] Compile the kernel with frame pointers
     [*] Kgdb:kernel Debugg ER  --->
           < * >   kgdb:use KGDB over the serial console
1.4: Compiling ? View Code BASH

If your machine is multi-core, you can use the-J+CPU number to compile in parallel, thus speeding up the compilation speed, such as:? View Code BASH


when the compilation is complete, copy bzimage and Vmlinux to the working directory for backup? View Code BASH

CP arch/x86/boot/bzimage/usr/src/work
CP vmlinux/usr/src/work
2: Make own file system 2.1: Download BusyBox

go to the busybox site to download a BusyBox source package, and extract.? View Code BASH

TAR-JXVF busybox-1.17.0.tar.bz2
CD busybox-1.17.0
2.2: Compiling BusyBox ? View Code BASH
Make Menuconfig
Busybox Settings---> Builds 
        [*] Build Busybox as a static binary (No shared Libs)
   Installation Options--->
        [*] Don ' t use/usr
miscellaneous Utilities  ---> 
[] flashcp 
[] F Lash_lock
[] flash_unlock
[] flash_eraseall
Note: [] indicates no selection

start compilation and installation after saving the configuration file? View Code make

Make make

In this case, a _install directory is generated in the current directory, which is the BusyBox executable file 2.3: Making the file system

Use the following command to create a virtual file system disk file.

create a file in the current directory named busybox.img, size 100M, and format it as a ext3 file system? View Code BUSYBOX

dd If=/dev/zero of=./busybox.img bs=1m count=100 mkfs.ext3 busybox.img

Take this virtual disk file to the local system so that we can access it like a local file,
and copy the generated busybox files into this file.
View Code MOUNT

sudo mkdir/mnt/disk
sudo mount-o loop/usr/src/work/busybox.img/mnt/disk
sudo cp-rf/usr/src/work/ busybox-1.17.0/_install/*/mnt/disk
? View Code MOUNT
Create the required file system directory
sudo mkdir dev sys proc etc lib mnt
use busybox default settings file
sudo cp-a/usr/src/w ork/busybox-1.17.0/examples/bootfloppy/etc/*/mnt/disk/etc
sudo vi/mnt/disk/etc/init.d/rcs
Copy the following to the RCS:
/bin/mount-t sysfs sysfs/sys the/BIN/MOUNT-T
tmp Fs/dev

View Code MOUNT

CD dev/
mknod-m 666 console C 5 1
mknod-m 666 null C 1 3

when we're done, we can uninstall the virtual disk file. View Code unmount

sudo umount/mnt/disk
3: Install Qemu ? View Code BASH
sudo apt-get install qemu
sudo yum install qemu
4: Use QEMU to run your own compiled kernel ?
View Code QEMU
Qemu-kernel/usr/src/work/bzimage-append "Root=/dev/hda"-boot c-hda
/usr/src/work/busybox.img-k en-US
If your hard drive is a SATA interface, you may need to replace the "Root=/dev/hda" above with "ROOT=/DEV/SDA".

If it goes well, your own compiled kernel + file system will be displayed in that qemu dark window,
Pour yourself a glass of water and celebrate. 5:gdb + kgdb Debug Kernel

enables KGDB to increase the enabling parameters when the kernel is started, or the parameters of the Echo Kgdboc module after the kernel starts, where we take the way to increase the startup parameters (kgdboc=ttys0,115200 kgdbwait) when the kernel starts up:? View Code QEMU

Qemu-kernel/usr/src/work/bzimage-append "Root=/dev/hda kgdboc=ttys0,115200
kgdbwait"-boot c-hda/usr/src/work/ Busybox.img-k en-us-serial Tcp::4321,server

At this point, the terminal running QEMU will be prompted to wait for a remote connection to local port 4321:
QEMU Waiting for connection on:tcp:,server

This is done using another console:? View Code GDB

(GDB) target remote localhost:4321

Then QEMU can continue to run normally, finally stop the kernel and display the following message:
Kgdb:waiting for connection from remote GDB ...

at this point, GDB can see the following tips:? View Code GDB

(GDB) Target remote localhost:4321
remote debugging using localhost:4321
kgdb_breakpoint () at kernel/debug/ debug_core.c:983
983		WMB ()/* Sync point after breakpoint * *

Start your journey to the core

If GDB prompts you with the following information:
Warning:invalid Remote reply:
You can use CTRL + C to terminate the current GDB operation, and then reconnect the kgdb once again using the command below:
(GDB) Target remote localhost:4321 6:gdb + kgdb Debug kernel operation sample

To be perfected ... 7: Reference/Extended reading:

1: Debug the Linux kernel with KGDB (on QEMU)
The article on QEMU and kgdb Some of the parameters are very detailed, this article 4 and 5 chapters are referred to it written, strongly recommend everyone to see.
2:setting up kgdb using Kvmqemu
The article is also about QEMU and KGDB, but it describes how QEMU is networked and so on. PS: Unlike the above article, it directs QEMU's virtual serial port to a local "pty" device,
And the front of us is directed to a socket port. Qemu-serial parameters are described below:?
View Code QEMU

-serial Dev
Redirect the virtual serial port to host character device Dev. The default device is
' VC ' in graphical mode and ' stdio ' in non graphical mode. This option can is
used several times to simulate up to 4 serials ports.
This article address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.