gdb 在呈现数据时,默认最多显示 240 个字符(在我在 Linux 环境下是这样的,其它平台有说 200 的,有说其它长度的),后续数据会以省略号显示,这在观察字符串的值时非常不便,甚至会导致一些工具受影响,比如我在 Visual Studio Code 中使用 Debug Visuallizer 时就因为 json 字符串过长被省略导致不能可视化,经过摸索可行的方案如下:

直接执行设置指令

如果你在 gdb 环境里,则可以直接执行如下命令

1
set print elements 0

其中 0 可以改为你指定的长度,使用 0 表示不截断

解释参见:http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_57.html#IDX353

设置 gdb 的全局配置文件

1
sudo vim /etc/gdb/gdbinit

其中添加如下语句

1
set print elements 0

使用自定的 .gdbinit 文件

将上述指令添加到 .gdbinit 然后让 gdb 启动时自动设置也可,如果不能自动加载,则可以在 gdb 环境中使用如下指令加载

1
source .gdbinit

或者启动 gdb 时加载

1
gdb -command=.gdbinit

在 VS Code 的 Debug Console 中设置

1
-exec set print elements 0

在 launch.json 中设定

示例参见:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
// 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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Enable full length display",
"text": "set print elements 0",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}