Unable to Inspect C++ STL content in VS Code

6.3k views Asked by At

Problem Statement:

The debugger is not able to provide the content of an STL container (i.e. vector or string).


Overview:

Below is my launch.json, where I have added -enable-pretty-printing as per this thread but I'm unable to see the content of the STL Container.

{

    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.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:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true,
                    
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

I tried even adding expression in the watch window. But that also didn't work for me. Or maybe I am missing something. this thread

cannot debug stl

3

There are 3 answers

3
Bravo Yeung On BEST ANSWER

Firstly, you probably used x64 Windows.

I found one valid solution, when installing MinGW in x64 Windows, install i686 (win32) version (the bottom of this comment gives its official download link) of MinGW instead of x86_64 version, see below:

pretty-printing-not-work-with-MinGW GDB-solution

win32 version of MinGW download:

i686-win32-dwarf

I just extracted the downloaded file into the folder D:\MinGW, then add the bin path of MinGW D:\MinGW\i686-8.1.0-release-posix-dwarf-rt_v6-rev0\mingw32\bin to the PATH of System Variable of environment.

Add MinGW to system variable

The related config files are below:

.vscode\tasks.json

{
    "tasks": [
        {
            // "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

.vscode\launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.    
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.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": "gdb",
            "setupCommands": [
                {   // Display content in STL containers pretty
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

.vscode\c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "g++", // Or complete absolute path "D:/MinGW/i686-8.1.0-release-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe"
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

Environment of my PC

VSCode Version: 1.53.2 (system setup)
OS Version: Windows 10 x64 (Windows_NT x64 10.0.19041)
MinGW version: i686-8.1.0-release-posix-dwarf-rt_v6-rev0
GDB version: 8.1.0 (Target: i686-w64-mingw32)

Initially posted on https://github.com/microsoft/vscode-cpptools/issues/3921#issuecomment-780379258.

May it be helpful for you.

0
revanth kalavala On

As Bravo Yeung mentioned the problem is not with the architecture, C++ debugger which is GDB uses python for printing vectors or other STL containers. The MinGW which you might be using is the culprit Here. Because it did not have

  1. python functions to print the STL containers or
  2. The python files which prints were not placed correctly inside mingw folder or the VScode was unable to pick up python files

Things to check:

  1. If your computer is 64 bit, using x86_64 is better, x86 would also work
  2. If your computer is 32 bit, using x86 is only option

This is the repository I took the MinGW and I used

x86_64-12.2.0-release-win32-seh-rt_v10-rev1.7z as x86_64 meant for 64 bit.

C:\mingw64\share\gdb\python\gdb The python which helps in debugging should be placed in a path like above.

In my case, the MinGW which I downloaded did not not have GDB folder in shared directory which lead to this problem.

0
requizm On

Are you sure you've done every step on this thread?

  1. MinGw-get.exe install gdb-python
  2. Install Python 2.7
  3. Make sure PYTHONPATH and PYTHONHOME variables on environment variables.
  4. Make sure %PYTHONHOME% on path variables.
  5. Create .gdbinit file on cwd folder. Don't forget to change sys.path.insert(0, 'your\MinGw\share\gcc-x.y.z\python')
  6. Dont forget to change miDebuggerPath="your\\mingw\\bin\\gdb-python27.exe" instead of miDebuggerPath="your\\mingw\\bin\\gdb.exe"

Note: If you are using MinGW x86, make sure the Python architecture you install is x86.