利用buildroot(内置qemu)和vscode开发调试linux
我的其他博客有关于buildroot怎么用的介绍。
比如 buildroot-2020.11.1/configs/qemu_arm_vexpress_defconfig 这个配置。
make qemu_arm_vexpress_defconfig make
就能得到:
./start-qemu.sh 是运行qemu的shell脚本,同时加载kernel(zImage),dtb,rootfs。
用vscode调试:
buildroot:
buildroot默认不下载编译gdb,需要 make menuconfig勾选: Toolchain ---> [*] Build cross gdb for the host buildroot里面的linux,默认配置一般也是关闭了debug,直接 make linux-menuconfig 去勾选: Kernel hacking ---> [*] Kernel debugging Compile-time checks and compiler options ---> [*] Compile the kernel with debug info
可以用这条命令,验证kernel的编译输出里,有没有debug info:
readelf -e /home/mmm/buildroot-2020.11.1/output/build/linux-rel_imx_5.4.24_2.1.0/vmlinux
有这样的段就对了:
[32] .debug_line PROGBITS 00000000 d1fe36 f1d5ed 00 0 0 1
[33] .debug_info PROGBITS 00000000 1c3d423 7b50e92 00 0 0 1
[34] .debug_abbrev PROGBITS 00000000 978e2b5 46824b 00 0 0 1
[35] .debug_aranges PROGBITS 00000000 9bf6500 015e70 00 0 0 8
[36] .debug_str PROGBITS 00000000 9c0c370 2727f8 01 MS 0 0 1
[37] .debug_ranges PROGBITS 00000000 9e7eb68 4c78e0 00 0 0 8
[38] .debug_frame PROGBITS 00000000 a346448 16279c 00 0 0 4
[39] .debug_loc PROGBITS 00000000 a4a8be4 5b328b 00 0 0 1
qemu:
export PATH="/home/mmm/buildroot-2020.11.1/output/host/bin:${PATH}" # dtc -I dtb -O dts vexpress-v2p-ca9.dtb -o vexpress-v2p-ca9-all.dts qemu-system-arm \ -M vexpress-a9 \ -smp 1 \ -m 256 \ -kernel ${IMAGE_DIR}/zImage \ -dtb ${IMAGE_DIR}/vexpress-v2p-ca9.dtb \ -drive file=${IMAGE_DIR}/rootfs.ext2,if=sd,format=raw \ -append "console=ttyAMA0,115200 rootwait root=/dev/mmcblk0" \ -nographic \ -gdb tcp::12451 \ -S \
-gdb tcp::12451 //从qemu里引出gdb server到宿主机的 tcp::12451端口
-S //在第一条指令处暂停,等待gdb连接
-smp 1 //我在调试单核CPU,调试多核的linux(SMP)要改这个地方
vscode:自带gdb插件,操作方法自己搜,关键是这个文件
buildroot-2020.11.1/output/build/linux-5.4.58/.vscode/launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "/home/mmm/buildroot-2020.11.1/output/build/linux-5.4.58/vmlinux", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerServerAddress": "localhost:12451", "miDebuggerPath": "/home/mmm/buildroot-2020.11.1/output/host/bin/arm-buildroot-linux-uclibcgnueabihf-gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
另外:configs下的其他配置也可以用qemu跑,只是外设等可能没有。
比如 imx6-sabresd_defconfig:
#!/bin/sh IMAGE_DIR="${0%/*}/" # if [ "${1}" = "serial-only" ]; then # EXTRA_ARGS='-nographic' # else # EXTRA_ARGS='-serial stdio' # fi export PATH="/home/mmm/buildroot-2020.11.1/output/host/bin:${PATH}" # dtc -I dtb -O dts imx6dl-sabresd.dtb -o dec.dts # qemu-img resize -f raw --shrink ${IMAGE_DIR}/rootfs.ext2 32M qemu-system-arm \ -M sabrelite \ -smp 1 \ -m 1G \ -nographic \ -kernel ${IMAGE_DIR}/zImage \ -dtb ${IMAGE_DIR}/imx6dl-sabresd.dtb \ -drive file=${IMAGE_DIR}/rootfs.ext2,format=raw,id=mysdcard \ -device sd-card,drive=mysdcard \ -append "console=ttymxc0 rootfstype=ext2 rootwait root=/dev/mmcblk3" \ -gdb tcp::12451 \ -S \
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "/home/mmm/buildroot-2020.11.1/output/build/linux-rel_imx_5.4.24_2.1.0/vmlinux", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerServerAddress": "localhost:12451", "miDebuggerPath": "/home/mmm/buildroot-2020.11.1/output/host/bin/arm-buildroot-linux-uclibcgnueabihf-gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
树莓派也可以这么做,qemu对树莓派的支持很好。
查看qemu对外设的支持可以在编译完成后。看这个文件夹:buildroot-2020.11.1/output/build/host-qemu-5.1.0/hw/arm/vexpress.c