senoron
senoron

Reputation: 344

gdb: <error reading variable> in array while debugging VS code

I'm having trouble trying to view the contents of an array during debugging. Instead of characters I see .

My code:

#include <stdio.h>

int main()
{
    char str[100] = {0};
    fgets(str, 100, stdin);

    return 0;
}

What I see in the VARIABLES window:

enter image description here

But when I try to debug int arrays everything works fine.

Exemple:

#include <stdio.h>

int main()
{
    int str[100];
    for (int i = 0; i < 100; i++)
    {
        str[i] = i+1;
    }
    

    return 0;
}

What I see:

enter image description here

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
        }
    ]
}

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": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}

What can I do to see the characters in the array?

Upvotes: 3

Views: 1343

Answers (1)

Ielixmar
Ielixmar

Reputation: 58

Because pretty-printer is not displaying utf-8 char correctly. To display utf-8 char, edit the setupCommands in launch.json:

  "setupCommands": [
    {
      "description": "Enable pretty-printing for gdb",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true
    },
    {
      "description": "Fix pretty-printing for gdb",
      "text": "set charset UTF-8"
    }
  ],

Original answer: VSCode debugger having issues with character encoding

Upvotes: 3

Related Questions