https://zhuanlan.zhihu.com/p/4185359719
本文发布于微信公众号:Linux底层小工,欢迎关注,获取更多原创技术文章!
“VSCode+qemu+gdb调试OpenSBI确实很爽,那怎能少了调试kernel呢~~”
01 VScode调试MMU开启之前的kernel
有了调试OpenSBI的基础,再调试kernel,那设置相当简单了,如果你还没看过VSCode调试OpenSBI,请先查看另一篇文章:《VSCode+qemu+gdb可视化调试OpenSBI》
现在让我们来配置调试kerenl,先在VSCode中远程打开kernel目录
然后点击debug图标:
然后点击create a launch.json file:
选择Node.js:
然后输入如下内容:
{// 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": "kernel-before-mmu-debug","type": "cppdbg","request": "launch","program": "${workspaceFolder}/vmlinux","args": [],"stopAtEntry": true,"stopAtConnect": true,"cwd": "${workspaceFolder}","postRemoteConnectCommands": [{"description": "remove symbol file","text": "symbol-file"}{"description": "add symbol file","text": "add-symbol-file vmlinux 0x80202000 -s .head.text 0x80200000 -s .init.text 0x80c00000 -s .rodata 0x81000000"}],"MIMode": "gdb","miDebuggerPath":"/usr/bin/gdb-multiarch","miDebuggerServerAddress": "localhost:1234"}]
}
注意,其中的program就是vmlinux的路径,还有postRemoteConnectCommands要在连接之后先使用symbol-file命令将vscode启动gdb时默认加载的symbol清除掉,然后再使用add-symbol-file命令将各个段的symbol添加到指定位置(这些位置怎么计算来的,请观看RISC-V篇-qemu+gdb调试Linux kernel源码),否则后面debug时会有问题,比如,设置断点,会命中多个地址等
现在可以在VSCode中调试了,先在VSCode中打开终端,并启动qemu:
然后分别点击debug按钮,Start Debugging按钮:
启动debug之后,如下图:
现在打开head.S文件,可以直接在代码的左侧点击设置断点,或者在DEBUG CONSOLE下输入 -exec b xxx 设置断点,如下图,左侧红色的小点就是设置的断点:
点击countinue按钮运行:
就会命中断点,左侧还提供了非常丰富的信息:
02 VScode调试MMU开启之后的kernel
有了以上的基础,调试MMU开启之后的kernel就很简单了,只需修改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": "kernel-before-mmu-debug","type": "cppdbg","request": "launch","program": "${workspaceFolder}/vmlinux","args": [],"stopAtEntry": true,"stopAtConnect": true,"cwd": "${workspaceFolder}","MIMode": "gdb","miDebuggerPath":"/usr/bin/gdb-multiarch","miDebuggerServerAddress": "localhost:1234"}]
}
对的,你没有看错,就是删除postRemoteConnectCommands字段即可
启动qemu,启动debugger,设置断点,continue运行,与上面都一样的,这里就省略了,最终运行到start_kernel的效果如下:
其他操作留给你自己探索去吧~~
最后,不得不说,VSCode是真强大啊,再安装上vim插件,用vim命令,简直爽到飞起
俗话说,磨刀不误砍柴工,关于环境设置这一块,其实就是在磨刀,不过截至目前,我感觉环境设置已经基本ok了,对以后的效率会有很大的提升,后面会更多的专注于RISC-V知识的学习了,包括spec文档,代码等~~
敬请期待!!!
如果你觉得本文对你有帮助,请点赞,关注~
本文发布于微信公众号:Linux底层小工,欢迎关注,获取更多原创技术文章!