The whole process of gdb debugging kernel

Compile the kernel

make i386_defconfig //32位架构
make menuconfig

It can debug
Kernel hacking —>
Compile-time checks and compiler options —>
[] Compile the kernel with debug info
You can use the bridge to network
Device Drivers —>
Network device support —>
[M] Universal TUN/TAP device driver support

make -j16  //编译

There will be a kernel image in the compiled linux kernel
Insert picture description here

busybox

BusyBox can be run as a user space in the kernel started by qemu. The idea is to package BusyBox into a small file system structure and archive it in the cpio file to run as the initramfs for system startup, so that we can have a Linux-like initramfs The operation interface and tool set.
Download and compile

wget https://busybox.net/downloads/busybox-1.27.2.tar.bz2
tar -xf busybox-1.27.2.tar.bz2
cd busybox-1.27.2
make menuconfig

Insert picture description here

make -j8
make install

In the end we get
Insert picture description here

Create a file system

See my other blog about the kernel and file system.
1.
Create a new folder, open a terminal, and create a new rootfs folder,
where qemu_rootfs.img is the file name and 1g is the disk size, modify it as needed.
Create an ext4 file system and
mount the img file to the host system:

qemu-img create qemu_rootfs.img  1g
mkfs.ext4 qemu_rootfs.img    
sudo mount -o loop qemu_rootfs.img  rootfs

2.
Create the required files in the mounted rootfs file, including the init file

cd rootfs
sudo mkdir proc sys dev etc etc/init.d
cd etc/init.d
vim rcS
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
chmod u+x rcS
cd ../../..
sudo umount rootfs

3. Copy the files in busybox into rootfs, and then cancel the mount

cp -a  _install/*  rootfs
umount rootfs

Start debugging

Enter the folder where the kernel image is located
Insert picture description here

Excuting an order

qemu-system-i386 -s -kernel bzImage -m 512M -hda qemu_rootfs.img -append "root=/dev/sda rootfstype=ext4 rw"

i386 starts qemu, specifies the kernel, specifies the file system, specifies root,
-s means -gdb tcp::1234 abbreviation, monitors port 1234, does not open the image

Create a new terminal and find this
Insert picture description here
Insert picture description here

target remote tcp:localhost:1234
can enter debugging

PS: Close qemu—ctrl+a and then press x on the qemu interface

Guess you like

Origin blog.csdn.net/qq_42882717/article/details/114646469