Using KGDB to debug the kernel on QEMU

Source: Internet
Author: User
Tags bz2 mkdir
http://www.kgdb.info/kgdb/use_kgdb/using_kgdb_base_qemu/
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 lmkl.org, 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-2.6.34.1 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
cd/usr/src/work
wget http:// www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.1.tar.bz2
TAR-JXVF linux-2.6.34.1.tar.bz2
1.3: Configure kernel selection

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

CD linux-2.6.34.1 make
defconfig make
menuconfig

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
hacking--->
     
      [*] 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
Make

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

Make-j2

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

Cd/usr/src/work
wget http://www.busybox.net/downloads/busybox-1.17.0.tar.bz2
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 
   Options--->
        [*] 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
Install

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

Cd/usr/src/work
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
cd/mnt/disk/
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/sh
/bin/mount-a
/bin/mount-t sysfs sysfs/sys the/BIN/MOUNT-T
tmp Fs/dev
#动态添加虚拟机环境中的设备
/sbin/mdev-s


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

Cd/usr/src/work
sudo umount/mnt/disk
3: Install Qemu ? View Code BASH
Ubuntu/debian:
sudo apt-get install qemu
Fedora:
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
C11/>note:
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:0.0.0.0:4321,server

This is done using another console:? View Code GDB

Gdb/usr/src/work/vmlinux
(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 * *
(GDB)

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:
http://www.kgdb.info/kgdb/use_kgdb/using_kgdb_base_qemu/

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: info-contact@alibabacloud.com 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.