编译内核、busybox、配置Qemu参考:Qemu调试内核
一、修改启动脚本
1、修改Qemu启动脚本
#! /bin/shqemu-system-aarch64 \-machine virt,virtualization=true,gic-version=3 \-nographic \-m size=1024M \-cpu cortex-a72 \-smp 2 \-kernel Image \-drive format=raw,file=rootfs.img \-append "root=/dev/vda rw nokaslr" \-s \-S# nokaslr 是关闭随机地址,gdb调试随机地址导致无法正常调试
# -s 是-gdb tcp::1234 的简写,如果需要换端口可以用-gdb tcp::1234替换-s参数
# -S 是freeze cpu at startup的指令,也就是kernel 启动时就挂起,等待调试连接,如果不需要调试内核启动,这个参数也可以去掉
二、配置vscode
1、创建编辑 .vscode/launch.json
将文件创建在内核文件夹
{"version": "0.2.0","configurations": [{"name": "kernel debug","type": "cppdbg","request": "launch","program": "${workspaceFolder}/vmlinux","cwd": "${workspaceFolder}","MIMode": "gdb","miDebuggerPath": "/usr/bin/gdb-multiarch","miDebuggerServerAddress": "localhost:1234"}]
}
三、调试内核
1、打断点init/main.c
start_kernel是内核入口