Linux_Kernel编译调试env

运行主机环境

1
2
3
4
root@uubb-ThinkCentre-M930s-N000:/# uname -a
Linux uubb-ThinkCentre-M930s-N000 5.4.0-150-generic #167~18.04.1-Ubuntu SMP Wed May 24 00:51:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
root@uubb-ThinkCentre-M930s-N000:/# nproc
16

下载内核

内核下载链接: https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/

1
2
3
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.10.tar.gz
将压缩包放到自己指定的目录,解压
tar -zxf linux-5.10.tar.gz

下载 qemu

apt install qemu-system

编译内核

清除以前的配置,回到默认配置

1
2
3
4
5
6
7
8
9
10
11
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> sudo make mrproper
[sudo] uubb 的密码:

CLEAN scripts/basic
CLEAN scripts/gdb/linux
CLEAN scripts/kconfig
CLEAN scripts/mod
CLEAN scripts/selinux/genheaders
CLEAN scripts/selinux/mdp
CLEAN scripts
CLEAN include/config include/generated arch/x86/include/generated .config .config.old .version Module.symvers vmlinux-gdb.py

生成.config 文件

env ARCH=X86_64 make deconfig
或者
make deconfig // 根据机器默认生成
上面两个都生成不了,最终执行
make x86_64_deconfig

修改配置信息
make memuconfig

1
2
3
4
5
6
# Debugging
Kernel hacking ---> Compile-time checks and compiler options ---> Compile the kernel with debug info ---> yes
Kernel hacking ---> Compile-time checks and compiler options ---> Provide GDB scripts for kernel debugging ---> yes
General setup ---> Configure standard kernel features ---> yes
General setup ---> Configure standard kernel features ---> Load all symbols for debugging/ksymoops ---> yes
General setup ---> Configure standard kernel features ---> Include all symbols in kallsyms ---> yes

其他可选配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Only if 64 bits is selected
Binary Emulations ---> IA32 a.out support ---> yes
Binary Emulations ---> IA32 ABI for 64-bit mode ---> yes

# Make sure the following options are enabled:
General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support ---> yes
General setup ---> Configure standard kernel features ---> Multiple users, groups and capabilities support ---> yes
General setup ---> Configure standard kernel features ---> Sysfs syscall support ---> yes
Device Drivers ---> Generic Driver Options ---> Maintain a devtmpfs filesystem to mount at /dev ---> yes
Device Drivers ---> Generic Driver Options ---> Automount devtmpfs at /dev, after the kernel mounted the rootfs ---> yes
Device Drivers ---> Character devices ---> Enable TTY ---> yes
Device Drivers ---> Character devices ---> Serial drivers ---> 8250/16550 and compatible serial support ---> yes
Device Drivers ---> Character devices ---> Serial drivers ---> Console on 8250/16550 and compatible serial port ---> yes
File systems ---> Pseudo filesystems ---> /proc file system support ---> yes
File systems ---> Pseudo filesystems ---> sysfs file system support ---> yes

如果想要在此内核上测试漏洞或内容,需要禁用以下策略:

1
2
3
4
5
6
7
8
# Allocations at NULL to allow nullptr de-reference
Memory Management options ---> Low address space to protect from user allocation ---> 0
# SMAP disabled
Processor type and features ---> Supervisor Mode Access Prevention ---> no
# KASL, can be disabled by setting nokaslr in the bootloader cmdline
Processor type and features ---> Build a relocatable kernel ---> no
# Canary disabled
General architecture-dependent options ---> Stack Protector buffer overflow detection ---> no

开始编译

make -j 16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> env ARCH=x86_64 make -j16
SYSTBL arch/x86/include/generated/asm/syscalls_32.h
SYSHDR arch/x86/include/generated/asm/unistd_32_ia32.h
SYSHDR arch/x86/include/generated/asm/unistd_64_x32.h
SYSTBL arch/x86/include/generated/asm/syscalls_64.h
SYSHDR arch/x86/include/generated/uapi/asm/unistd_32.h
SYSHDR arch/x86/include/generated/uapi/asm/unistd_64.h
SYSHDR arch/x86/include/generated/uapi/asm/unistd_x32.h
WRAP arch/x86/include/generated/uapi/asm/bpf_perf_event.h
WRAP arch/x86/include/generated/uapi/asm/errno.h
WRAP arch/x86/include/generated/uapi/asm/fcntl.h
WRAP arch/x86/include/generated/uapi/asm/ioctl.h
WRAP arch/x86/include/generated/uapi/asm/ioctls.h
WRAP arch/x86/include/generated/uapi/asm/ipcbuf.h
WRAP arch/x86/include/generated/uapi/asm/param.h
WRAP arch/x86/include/generated/uapi/asm/poll.h
...
LD [M] fs/efivarfs/efivarfs.ko
CC arch/x86/boot/video-bios.o
HOSTCC arch/x86/boot/tools/build
HOSTCC arch/x86/boot/compressed/mkpiggy
LD [M] net/netfilter/nf_log_common.ko
CC arch/x86/boot/compressed/cpuflags.o
CC arch/x86/boot/compressed/early_serial_console.o
CPUSTR arch/x86/boot/cpustr.h
CC arch/x86/boot/compressed/kaslr.o
CC arch/x86/boot/compressed/ident_map_64.o
CC arch/x86/boot/compressed/idt_64.o
LD [M] net/netfilter/xt_MASQUERADE.ko
AS arch/x86/boot/compressed/idt_handlers_64.o
CC arch/x86/boot/cpu.o
AS arch/x86/boot/compressed/mem_encrypt.o
LD [M] net/netfilter/xt_addrtype.ko
CC arch/x86/boot/compressed/pgtable_64.o
CC arch/x86/boot/compressed/acpi.o
AS arch/x86/boot/compressed/efi_thunk_64.o
LD [M] net/netfilter/xt_LOG.ko
LD [M] net/netfilter/xt_mark.ko
LD [M] net/netfilter/xt_nat.ko
CC arch/x86/boot/compressed/misc.o
GZIP arch/x86/boot/compressed/vmlinux.bin.gz
MKPIGGY arch/x86/boot/compressed/piggy.S
AS arch/x86/boot/compressed/piggy.o
LD arch/x86/boot/compressed/vmlinux
ZOFFSET arch/x86/boot/zoffset.h
OBJCOPY arch/x86/boot/vmlinux.bin
AS arch/x86/boot/header.o
LD arch/x86/boot/setup.elf
OBJCOPY arch/x86/boot/setup.bin
BUILD arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready (#1)

看编译的日志信息,bzImage 显示在x86 文件内,在其实我指定了x86_64。在arch 目录内,会生成一个x86_64文件夹,并且有镜像。

编译后的目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> ls -al
总用量 1708
drwxrwxr-x 25 uubb uubb 4096 1月 17 11:32 ./
drwxrwxr-x 3 uubb uubb 4096 1月 16 16:31 ../
-rw-rw-r-- 1 uubb uubb 0 1月 16 18:04 1_readme_wuyou.txt
drwxrwxr-x 26 uubb uubb 4096 1月 17 11:19 arch/
drwxrwxr-x 3 uubb uubb 4096 1月 17 11:19 block/
-rw-rw-r-- 1 uubb uubb 423 1月 16 16:57 build_log.txt
drwxrwxr-x 2 uubb uubb 4096 1月 17 11:19 certs/
-rw-rw-r-- 1 uubb uubb 16673 12月 14 2020 .clang-format
-rw-rw-r-- 1 uubb uubb 59 12月 14 2020 .cocciconfig
-rw-rw-r-- 1 uubb uubb 248775 1月 17 11:32 .config
-rw-rw-r-- 1 uubb uubb 496 12月 14 2020 COPYING
-rw-rw-r-- 1 uubb uubb 100478 12月 14 2020 CREDITS
drwxrwxr-x 4 uubb uubb 12288 1月 17 11:19 crypto/
drwxr-xr-x 21 root root 4096 1月 16 17:28 debootstrap/
drwxrwxr-x 81 uubb uubb 4096 12月 14 2020 Documentation/
drwxrwxr-x 140 uubb uubb 4096 1月 17 11:19 drivers/
drwxrwxr-x 79 uubb uubb 12288 1月 17 11:19 fs/
-rw-rw-r-- 1 uubb uubb 71 12月 14 2020 .get_maintainer.ignore
-rw-rw-r-- 1 uubb uubb 62 12月 14 2020 .gitattributes
-rw-rw-r-- 1 uubb uubb 1889 12月 14 2020 .gitignore
drwxrwxr-x 31 uubb uubb 4096 1月 17 11:32 include/
drwxrwxr-x 2 uubb uubb 4096 1月 17 11:19 init/
drwxrwxr-x 2 uubb uubb 4096 1月 17 11:19 ipc/
-rw-rw-r-- 1 uubb uubb 1327 12月 14 2020 Kbuild
-rw-rw-r-- 1 uubb uubb 555 12月 14 2020 Kconfig
drwxrwxr-x 20 uubb uubb 12288 1月 17 11:19 kernel/
drwxrwxr-x 21 uubb uubb 20480 1月 17 11:19 lib/
drwxrwxr-x 6 uubb uubb 4096 12月 14 2020 LICENSES/
-rw-rw-r-- 1 uubb uubb 18204 12月 14 2020 .mailmap
-rw-rw-r-- 1 uubb uubb 576680 12月 14 2020 MAINTAINERS
-rw-rw-r-- 1 uubb uubb 64379 12月 14 2020 Makefile
drwxrwxr-x 3 uubb uubb 12288 1月 17 11:19 mm/
drwxrwxr-x 72 uubb uubb 4096 1月 17 11:19 net/
-rw-rw-r-- 1 uubb uubb 727 12月 14 2020 README
drwxrwxr-x 32 uubb uubb 4096 12月 14 2020 samples/
drwxrwxr-x 17 uubb uubb 4096 1月 17 11:19 scripts/
drwxrwxr-x 13 uubb uubb 4096 1月 17 11:19 security/
drwxrwxr-x 26 uubb uubb 4096 1月 17 11:19 sound/
drwxrwxr-x 36 uubb uubb 4096 12月 14 2020 tools/
drwxrwxr-x 3 uubb uubb 4096 1月 17 11:19 usr/
drwxrwxr-x 4 uubb uubb 4096 1月 17 11:19 virt/
-rw-r--r-- 1 root root 1853030400 1月 16 18:08 xenial-debootstrap.ext2.qcow2

依赖安装

sudo apt install -y build-essential flex bison
libssl-dev libelf-dev libncurses-dev dwarves zstd
gcc make openssl libc6-dev

make modules_install INSTALL_MOD_PATH=debootstrap/

make modules_install INSTALL_MOD_PATH=debootstrap/

安装内核

如果要单独编译

sudo make modules 编译模块
sudo make bzImage 编译内核映像文件
sudo make mudoles_install 安装模块
sudo make install 安装内核

查看内核是否安装

如果在本机中安装了内核,可以在 /boot/grub/grub.cfg 内看到刚刚新编译的内核选项。可以重启机器。

重启,按住F12,进入GRUB, 查看高级选项,可以看到编译的新的内核。


可以进入iniramfs 中。

测试编译的内核能否使用

如果出现kernel panic 是正常的,因为缺少initramfs 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> 
sudo qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -nographic -append "console=ttyS0" -enable-kvm
[sudo] uubb 的密码:
qemu-system-x86_64: warning: host doesn't support requested feature: CPUID.80000001H:ECX.svm [bit 2]
[ 0.000000] Linux version 5.10.0 (uubb@uubb-ThinkCentre-M930s-N000) (gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, GNU ld (GNU Binutils for Ubuntu) 2.30) #1 SMP Tue Jan 16 16:59:56 CST 2024
[ 0.000000] Command line: console=ttyS0
[ 0.000000] x86/fpu: x87 FPU will use FXSAVE
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000007fdffff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000007fe0000-0x0000000007ffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
[ 0.000000] NX (Execute Disable) protection: active
[ 0.000000] SMBIOS 2.8 present.
[ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
[ 0.000000] tsc: Fast TSC calibration using PIT
[ 0.000000] tsc: Detected 2904.313 MHz processor
[ 0.000472] last_pfn = 0x7fe0 max_arch_pfn = 0x400000000
...
[ 1.264167] kernel_init_freeable+0x1dc/0x1eb
[ 1.264432] ? rest_init+0xb0/0xb0
[ 1.264638] kernel_init+0x5/0x110
[ 1.264849] ret_from_fork+0x22/0x30
[ 1.265246] Kernel Offset: disabled
[ 1.265484] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0) ]---


安装Initramfs

make modules_install INSTALL_MOD_PATH=debootstrap/

sudo debootstrap
–include linux-image-generic
xenial
debootstrap
http://archive.ubuntu.com/ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
debootstrap : 一个用来建立chroot环境的工具

uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> make modules_install INSTALL_MOD_PATH=debootstrap/
INSTALL drivers/thermal/intel/x86_pkg_temp_thermal.ko
INSTALL fs/efivarfs/efivarfs.ko
INSTALL net/ipv4/netfilter/iptable_nat.ko
INSTALL net/ipv4/netfilter/nf_log_arp.ko
INSTALL net/ipv4/netfilter/nf_log_ipv4.ko
INSTALL net/ipv6/netfilter/nf_log_ipv6.ko
INSTALL net/netfilter/nf_log_common.ko
INSTALL net/netfilter/xt_LOG.ko
INSTALL net/netfilter/xt_MASQUERADE.ko
INSTALL net/netfilter/xt_addrtype.ko
INSTALL net/netfilter/xt_mark.ko
INSTALL net/netfilter/xt_nat.ko
DEPMOD 5.10.0

执行完之后,源码目录下会多一个debootstrap目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> sudo debootstrap \
--include linux-image-generic \
xenial \
debootstrap \
http://archive.ubuntu.com/ubuntu

I: Retrieving InRelease
I: Checking Release signature
I: Valid Release signature (key id 790BC7277767219C42C86F933B4FE6ACC0B21F32)
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Found additional base dependencies: crda iw libnl-3-200 libnl-genl-3-200 linux-firmware linux-image-4.4.0-21-generic linux-image-extra-4.4.0-21-generic wireless-regdb
I: Checking component main on http://archive.ubuntu.com/ubuntu...
I: Retrieving adduser 3.113+nmu3ubuntu4
I: Validating adduser 3.113+nmu3ubuntu4
I: Retrieving apt 1.2.10ubuntu1
I: Validating apt 1.2.10ubuntu1
I: Retrieving apt-utils 1.2.10ubuntu1
I: Validating apt-utils 1.2.10ubuntu1
I: Retrieving base-files 9.4ubuntu4
I: Validating base-files 9.4ubuntu4
I: Retrieving base-passwd 3.5.39
I: Validating base-passwd 3.5.39
I: Retrieving bash 4.3-14ubuntu1
I: Validating bash 4.3-14ubuntu1
I: Retrieving bsdutils 1:2.27.1-6ubuntu3
I: Validating bsdutils 1:2.27.1-6ubuntu3
I: Retrieving busybox-initramfs 1:1.22.0-15ubuntu1
I: Validating busybox-initramfs 1:1.22.0-15ubuntu1
I: Retrieving bzip2 1.0.6-8
I: Validating bzip2 1.0.6-8
I: Retrieving console-setup 1.108ubuntu15
I: Validating console-setup 1.108ubuntu15
I: Retrieving console-setup-linux 1.108ubuntu15
I: Validating console-setup-linux 1.108ubuntu15
I: Retrieving coreutils 8.25-2ubuntu2
I: Validating coreutils 8.25-2ubuntu2
...
I: Configuring libc-bin...
I: Configuring systemd...
I: Configuring initramfs-tools...
I: Configuring ureadahead...
I: Configuring resolvconf...
I: Base system installed successfully.

然后chroot 进去,创建用户组,挂载服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> sudo chroot debootstrap
root@uubb-ThinkCentre-M930s-N000:/# ls
bin dev home lib media opt root sbin sys usr vmlinuz
boot etc initrd.img lib64 mnt proc run srv tmp var
root@uubb-ThinkCentre-M930s-N000:/# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@uubb-ThinkCentre-M930s-N000:/#
root@uubb-ThinkCentre-M930s-N000:/#
root@uubb-ThinkCentre-M930s-N000:/#
root@uubb-ThinkCentre-M930s-N000:/# /usr/sbin/adduser user
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "zh_CN:zh",
LC_ALL = (unset),
LANG = "zh_CN.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Adding user `user' ...
Adding new group `user' (1000) ...
Adding new user `user' (1000) with group `user' ...
Creating home directory `/home/user' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for user
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]

root@uubb-ThinkCentre-M930s-N000:/etc# cat << EOF | sudo tee "debootstrap/etc/fstab"
> /dev/sda / ext4 errors=remount-ro,acl 0 1
> EOF
tee: debootstrap/etc/fstab: No such file or directory
root@uubb-ThinkCentre-M930s-N000:/etc# cat fstab
# UNCONFIGURED FSTAB FOR BASE SYSTEM
root@uubb-ThinkCentre-M930s-N000:/etc# echo "/dev/sda / ext4 errors=remount-ro,acl 0 1" >> fstab
root@uubb-ThinkCentre-M930s-N000:/etc# cat fstab
# UNCONFIGURED FSTAB FOR BASE SYSTEM
/dev/sda / ext4 errors=remount-ro,acl 0 1

root@uubb-ThinkCentre-M930s-N000:/etc# cat << EOF | sudo tee debootstrap/etc/systemd/system/dhclient.service
> [Unit]
> Description=DHCP Client
> Documentation=man:dhclient(8)
> Wants=network.target
> Before=network.target
> [Service]
> Type=forking
> PIDFile=/var/run/dhclient.pid
> ExecStart=/sbin/dhclient -4 -q
> [Install]
> WantedBy=multi-user.target
> EOF
tee: debootstrap/etc/systemd/system/dhclient.service: No such file or directory
[Unit]
Description=DHCP Client
Documentation=man:dhclient(8)
Wants=network.target
Before=network.target
[Service]
Type=forking
PIDFile=/var/run/dhclient.pid
ExecStart=/sbin/dhclient -4 -q
[Install]
WantedBy=multi-user.target
sudo ln -sf debootstrap/etc/systemd/system/dhclient.service \
debootstrap/etc/systemd/system/multi-user.target.wants/dhclient.service

然后将debootstrap 目录创建为qcow2 镜像,这可时候要知道你的主机是否支持虚拟化,因为我在服务器上搭建的时候,不支持
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

创建 qcow2 镜像,有的时候找不到这个命令,这是要下载 libguestfs

1
2
3
4
5
6
7
8
9
10
sudo virt-make-fs \
--format qcow2 \
--size +1G \
--type ext2 \
debootstrap \
xenial-debootstrap.ext2.qcow2


[sudo] uubb 的密码:
sudo: virt-make-fs:找不到命令

调试

使用qemu 将编译的内核跑起来,并且打开gdb调试的选项。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> 
sudo qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage \
-drive file=xenial-debootstrap.ext2.qcow2 \
-net nic -net user,hostfwd=tcp::2222-:22,hostfwd=tcp::9999-:9999,hostfwd=tcp::8000-:8000 \
-nographic -append "root=/dev/sda console=ttyS0" -s -S
qemu-system-x86_64: warning: TCG doesn't support requested feature: CPUID.01H:ECX.vmx [bit 5]
[ 0.000000] Linux version 5.10.0 (uubb@uubb-ThinkCentre-M930s-N000) (gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, GNU ld (GNU Binutils for Ubuntu) 2.30) #1 SMP Tue Jan 16 16:59:56 CST 2024
[ 0.000000] Command line: root=/dev/sda console=ttyS0
[ 0.000000] x86/fpu: x87 FPU will use FXSAVE
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000007fdffff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000007fe0000-0x0000000007ffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
[ 0.000000] NX (Execute Disable) protection: active
[ 0.000000] SMBIOS 2.8 present.
[ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
...
[ 1.310874] Call Trace:
[ 1.311062] dump_stack+0x57/0x6a
[ 1.311273] panic+0xf6/0x2b7
[ 1.311466] mount_block_root+0x1de/0x20a
[ 1.311725] ? __SCT__tp_func_drv_hw_scan+0x8/0x8
[ 1.312009] ? rdinit_setup+0x26/0x26
[ 1.312229] mount_root+0xec/0x10a
[ 1.312436] prepare_namespace+0x130/0x15f
[ 1.312685] kernel_init_freeable+0x1e4/0x1f4
[ 1.312947] ? rest_init+0xaf/0xaf
[ 1.313155] kernel_init+0x5/0xfa
[ 1.313362] ret_from_fork+0x22/0x30
[ 1.313709] Kernel Offset: 0x22400000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)
[ 1.314349] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(8,0) ]---

然后gdb 选择vmlinux 即可进行调试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> 
uubb@uubb-ThinkCentre-M930s-N000 ~/k/linux-5.10> gdb vmlinux
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...done.
warning: File "/home/uubb/kernel_Linux/linux-5.10/scripts/gdb/vmlinux-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
add-auto-load-safe-path /home/uubb/kernel_Linux/linux-5.10/scripts/gdb/vmlinux-gdb.py
line to your configuration file "/home/uubb/.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
line to your configuration file "/home/uubb/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual. E.g., run from the shell:
---Type <return> to continue, or q <return> to quit---
info "(gdb)Auto-loading safe path"
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
0x000000000000fff0 in exception_stacks ()
(gdb)
(gdb)
(gdb) b start_kernel
Breakpoint 1 at 0xffffffff82b1dadb: file init/main.c, line 849.
(gdb) c
Continuing.

Breakpoint 1, start_kernel () at init/main.c:849
849 {
(gdb)
Continuing.
(gdb)


Linux_Kernel编译调试env
https://tig3rhu.github.io/2024/04/07/20__Linux_Kernel编译调试env/
Author
Tig3rHu
Posted on
April 7, 2024
Licensed under