VS Code gdb 不能 pretty-printing
这个问题在 Windows MSYS2 中没有出现,但是在 WSL 上出现了。在调试时对 std::vector
的显示如下:
本来我是用的 CMake 上的调试启动功能的,StackOverflow 上面让用 .vscode/launch.json,我也试了,但是同样不行。这是我写的启动文件内容,主要是在 setupCommands
中打开了 pretty-printing 功能:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": [
{
"name": "gdb - debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{ // Display content in STL containers pretty
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
// "preLaunchTask": "C/C++: g++ build active file"
}
]
}
按照 StackOverflow 的 回答 1,我在 gdb 中用 info pretty-printer
看当前支持的 pretty-printer。结果:
global pretty-printers:
builtin
mpx_bound128
然后参考 回答 2 手动注册 /usr/share/gcc/python/libstdcxx/v6/printers.py 之后果然就能看到标准库一堆 STL 类型的 pretty-printers 了。但是我在 PATH
中加上 /usr/share/gcc/python 或者 /usr/share/gcc/python/libstdcxx/v6 都不能实现自动注册。没办法,只能在 ~/.gdbinit 中放上启动脚本:
python
import sys
sys.path.insert(0, '/usr/share/gcc/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
现在 STL 的调试时显示已经能够正常工作了:
Tip
一开始怀疑是 gdb 版本问题,我的 gcc 版本是 12.2,而 gdb 版本是 13.1。但是网上说两者是分开更新的,不需要是同一个版本,一般只要 gdb 版本比 gcc 新即可。