20

I'm currently using VS Code to learn C++ because it was easy to setup and is much lighter than VS Studio. However, one thing that I'm unable to do is to see the elements of an array (or string, etc), in debug mode.

vector

I've searched for solutions here, and it seems that enabling pretty printing would solve the issue, but unfortunetly it did not (i have Python 3.6 installed). I've also tried using VS Studio compiler and debugger, but was unable to get it to work the way i wanted (basically clicking F5 to compile single cpp files without needing to change any options, just a single click).

Can you guys help me out on this? I'm currently using MinGW compiler on windows 10, with the following tasks file:

"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g", "${relativeFile}", "-o", "example"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

and launch:

"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/example.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "preLaunchTask": "echo",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]
genpfault
  • 51,148
  • 11
  • 85
  • 139
tomas-silveira
  • 593
  • 3
  • 5
  • 14
  • 2
    I am facing exactly the same issue on my mac. Looks like a bug in VS Code – Georgy Pashkov Jun 30 '19 at 23:16
  • But have you managed to find a workaround? maybe with another debugger? _I've tried this one [link](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb) but didn't find it helpful – tomas-silveira Jul 01 '19 at 09:33
  • 2
    My work-around is to look inside the vector. Add a watch and type something like this: `(int(*)[5])ints._M_impl._M_start` (I am using a different implementation of STL so my expression is a little bit different). The `_M_start` points to the storage inside vector. Cast it to an array and you get the elements – Georgy Pashkov Jul 03 '19 at 20:56
  • When vscode shows `{...}` sometimes it is actually "working". It could either be mean there are zero elements or it's broken. Hard to establish trust. I wish it would show the number of elements when it knew. – jozxyqk Jun 01 '22 at 22:16

7 Answers7

6

For me this works, but check first if you have 'c:\msys64\mingw64\share\gcc-8.3.0\python' or 'c:\usr\share\gcc-8.3.0\python' folder and adjust the snippet

launch.json
...
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "python import sys;sys.path.insert(0, 'c:\\msys64\\mingw64\\share\\gcc-8.3.0\\python');from libstdcxx.v6.printers import register_libstdcxx_printers;register_libstdcxx_printers(None)",
                    "ignoreFailures": false
                },
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
...
pawel
  • 209
  • 3
  • 7
2

For Windows:

If you only see memory address for STL containers when debugging, 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
  • 8,654
  • 5
  • 38
  • 45
1

I used the method just like Georgy described above.

*((int(*)[10])array._M_impl._M_start)

Debug console output:
-exec p array
$7 = {<std::_Vector_base<int, std::allocator<int> >> = {_M_impl = {<std::allocator<int>> = {<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>}, _M_start = 0x2924cb0, _M_finish = 0x2924cd8, _M_end_of_storage = 0x2924cf0}}, <No data fields>}

-exec p *((int(*)[10])array._M_impl._M_start)
$8 = {41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464}
vbirdchong
  • 19
  • 5
1

I also faced a similar problem using MinGW for Windows.

Problem was resolved for me when I removed MINGW and installed mingw-w64 instead.

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • " Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems. " as written on the website. – Sachin Yadav May 25 '20 at 18:55
1

What worked for me in a remote linux machine.

Please check the path '/usr/share/gcc-10/python' on your server. It might be different for you.

"setupCommands": [
    {
        "description": "Enable pretty-printing for gdb",
        "text": "python import sys; sys.path.append('/usr/share/gcc-10/python');sys.path.insert(0, '/usr/bin/python');from libstdcxx.v6.printers import register_libstdcxx_printers;register_libstdcxx_printers(None)",
        "ignoreFailures": false
    },
    {
        "description": "Enable pretty-printing for gdb",
        "text": "-enable-pretty-printing",
        "ignoreFailures": true
    }
]
0

This problem may be due to a python issue with gdb. I recommend to use msys2 + mingw. It work fine and you get last version of gcc. How to install mingw with msys2:

  • install msys2
  • pacman -Suy
  • pacman -S mingw-w64-x86_64-gcc
  • pacman -S mingw-w64-x86_64-gdb
  • pacman -S mingw-w64-x86_64-make
  • add 'D:\msys64\mingw64\bin' to path ENV
user3324131
  • 923
  • 8
  • 10
0

I had similar issue, eventually using user3324131 solution with a tiny modification I was able to get it to install to the right place:

  • Install msys2
  • $pacman -Suy
  • $pacman -Suy mingw-w64-x86_64-gcc (note the -Suy)
  • $pacman -Suy mingw-w64-x86_64-gdb (note the -Suy)
  • $pacman -Suy mingw-w64-x86_64-make (note the -Suy)
  • Add C:\msys64\mingw64\bin to path ENV
  • Reopen vscode, and all works

Here is the result: enter image description here

AMAZING, Thanks and Enjoy!

user4581301
  • 33,082
  • 7
  • 33
  • 54
dev-one
  • 7
  • 3