5

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

Maqsud
  • 593
  • 1
  • 6
  • 18
  • 1
    Did you expand `_M_start`? – sweenish Oct 04 '20 at 00:10
  • @sweenish, It just gives me the first element of my vector. – Maqsud Oct 04 '20 at 00:12
  • Yes, and there's likely another member in there, that when expanded, shows you the next member. That's just how it's going to be. All I see are `push_back()`s, you could probably watch the value of `a.back()`, but at the same time the vector is behaving correctly. The visible breakpoint looks pointless. – sweenish Oct 04 '20 at 00:15

2 Answers2

4

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.

Bravo Yeung
  • 6,946
  • 3
  • 31
  • 41
1

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.

requizm
  • 33
  • 2
  • 6