编译内核
make meauconfig 后:
Kernel hacking ---> Compile-time checks and compiler options ---> [*] Compile the kernel with debug info [*] Provide GDB scripts for kernel debugging [*] Kernel debugging Processor type and features ----> [] Randomize the address of the kernel image (KASLR)
配置vscode
配置vscode的launch.json
{ "version": "0.2.0", "configurations": [ { "name": "qemu_bare", "type": "cppdbg", "request": "launch", "program": "/home/user/work_linux/linux-5.15.104/vmlinux", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "miDebuggerServerAddress": "localhost:1234", } ] }
运行Qemu
编译x86_64平台的linux可使用如下脚本:
qemu-system-x86_64 \ -m 1024M \ -kernel ./linux-5.15.104/arch/x86_64/boot/bzImage \ -initrd ./buildroot/output/images/rootfs.cpio \ -nographic \ -append "nokaslr console=ttyS0" \ -S -s
-s
是 QEMU 的一个简短命令行选项,它等价于 -gdb tcp::1234
,即设置 QEMU 在本地的 1234
端口上等待 gdb 的连接。
-S
是 QEMU 在启动虚拟机后立即暂停执行 CPU。