https://zhuanlan.zhihu.com/p/105069730
在能够使用 qemu + gdb 调试内核之后,怎么连上 vscode 呢?
看看参考的知乎文章,再看看这个视频: https://www.bilibili.com/video/BV19Y41157bK/?spm_id_from=333.788&vd_source=7a1a0bc74158c6993c7355c5490fc600
先安装这个插件
然后创建 .vscode/launch.json 文件,点击 add configuration,创建template
这是我的launch.json配置:
{
"name": "kernel-debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/kernel.bin",
"stopAtEntry": false,
"args": [],
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerServerAddress": "127.0.0.1:1234",
"logging": {
"engineLogging": false
},
}
如果要调试 nasm 程序的话:
launch.json
{
"name": "nasm",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "nasm",
"miDebuggerPath": "/usr/bin/gdb"
}
总的来说, launch.json 是
{
"configurations": [
{
"name": "kernel-debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/kernel.bin",
"stopAtEntry": false,
"args": [],
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerServerAddress": "127.0.0.1:1234",
"logging": {
"engineLogging": false
},
},
{
"name": "nasm",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "nasm",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json长得不太一样
tasks.json:
{
"type": "shell",
"label": "nasm",
"command": "",
"args": [
"/usr/bin/nasm",
"-f",
"elf32",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.o",
";",
"/usr/bin/gcc",
"-fdiagnostics-color=always",
"-m32",
"-g",
"-static",
"${fileDirname}/${fileBasenameNoExtension}.o",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
注意,nasm 的 -gdwarf 选项是使用 gdb 调试 nasm 程序所必须的,且这个选项不能与 -f bin 混合使用 (也正是这个原因,boot.asm 无法用 gcc 调试,但是这种小规模的代码,有 qemu+gdb 在汇编进行调试就够了)