microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.51k stars 1.55k forks source link

Cannot inspect values of stl containers #6937

Open danieltuzes opened 3 years ago

danieltuzes commented 3 years ago

Type: Debugger

I installed VSCode with the newest C/C++ extension, downloaded MinGW 17.1 from https://nuwen.net/mingw.html and tried to debug my CPP code, but there is either a bug, a wrong setting, or a missing feature in the debugger.

I also have MSVC but I tried to use g++ and gdb, and without installation, simply by copying them into my home directory, because in a corporate environment, one cannot use an online installer nor can install files into program files, for which one would need admin rights.

What is wrong

I cannot inspect the element of an stl container. In the example, I create a vector<int>, put a breakpoint into the code, and try to see its element. The debugger does not show it.

Versions

Code

Use the following code and put a breakpoint within the loop:

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> myints = {1, 2, 3};
    for (auto myint : myints)
        std::cout << myint; // put breakpoint here

    return 0;
}

Actual behavior

The debugger stops at the breakpoint but doesn't show the values of the vector in the VARIABLES (or Watch) tab. It only shows: vector

json files

lunch.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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:\\Users\\tuzes\\AppData\\Local\\MinGW\\bin",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Users\\tuzes\\AppData\\Local\\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"
        }
    ]
}

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Users\\tuzes\\AppData\\Local\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\Users\\tuzes\\AppData\\Local\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        },
        {
            "name": "GCC",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Users/tuzes/AppData/Local/MinGW/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
WardenGnaw commented 3 years ago

Do you have python installed in your MinGW installation? GDB uses python for pretty printing.

If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install' Python-enabled GDB e.g. MinGw-get.exe install gdb-python 1a) Get Python 2.7.x from http://python.org/download/ and install 1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment: PYTHONPATH should be C:\Python27\Lib (or similar) PYTHONHOME should be C:\Python27 1c) Add PYTHONHOME to your PATH %PYTHONHOME%;... 2a) Open a text enter, enter the following statements. Notice the 3rd line is pointing to where the python scripts are located. See notes below about this! python import sys sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python') from libstdcxx.v6.printers import register_libstdcxx_printers register_libstdcxx_printers (None) end 2b) Save as '.gdbinit' NOTE: Windows explorer will not let you name a file that starts with with a period from explorer. Most text edits (including Notepad) will let you. GDB init files are like 'scripts' of GDB commands that GBD will execute upon loading. 2c) The '.gdbinit' file needs to be in the working directory of GDB (most likely this is your projects root directory but your IDE can tell you.

danieltuzes commented 3 years ago

@WardenGnaw I don't know if I have, but I have no share nor python folder in the MinGW folder obtained from the link mentioned above. In the corporate environment I working with, I have very limited software I am allowed to use and I am afraid that MinGW-get and its network access request are not allowed. I installed python 2.7 from an Anaconda prompt with

conda create --name py2 python=2.7

And then python 2 can be found:

(base) C:\Users\tuzes>conda activate py2

(py2) C:\Users\tuzes>where python
C:\Users\tuzes\miniconda3\envs\py2\python.exe

I prefer not to pollute my path. I'd like to add lookup directories only for the relevant environments.

Could you please give further helping hand? It is also fine if you improve the user manual of this extension and I will try to follow that.

I also found the description on GDB's Wiki how to add it, but it is not comprehensive and refers to a 12 years old e-mail. Debugging is a key priority for a language support pack like vscode-cpptools, and inspecting STL containers is widely used, so it is in the inerest of vscode-cpptools to provide a useful guide.

yanglr commented 3 years ago

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-posix-dwarf

More details, see: https://stackoverflow.com/a/66238128/6075331

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

danieltuzes commented 3 years ago

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-posix-dwarf

More details, see: https://stackoverflow.com/a/66238128/6075331

Initially posted on #3921 (comment).

As I said in the section Versions: OS and Version: Windows 10 pro x64. What you offer is a workaround at most and not in line with the official recommendations.

danieltuzes commented 3 years ago

Do you have python installed in your MinGW installation? GDB uses python for pretty printing.

If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install' Python-enabled GDB e.g. MinGw-get.exe install gdb-python 1a) Get Python 2.7.x from http://python.org/download/ and install 1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment: PYTHONPATH should be C:\Python27\Lib (or similar) PYTHONHOME should be C:\Python27 1c) Add PYTHONHOME to your PATH %PYTHONHOME%;... 2a) Open a text enter, enter the following statements. Notice the 3rd line is pointing to where the python scripts are located. See notes below about this! python import sys sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python') from libstdcxx.v6.printers import register_libstdcxx_printers register_libstdcxx_printers (None) end 2b) Save as '.gdbinit' NOTE: Windows explorer will not let you name a file that starts with with a period from explorer. Most text edits (including Notepad) will let you. GDB init files are like 'scripts' of GDB commands that GBD will execute upon loading. 2c) The '.gdbinit' file needs to be in the working directory of GDB (most likely this is your projects root directory but your IDE can tell you.

@WardenGnaw Did I provide the extra info you needed? You labeled this as "more info is needed". Please let me know if I can give more details.