microsoft / vscode-cpptools

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

C++ debugging character arrays and strings: <error reading variable> and "Converting character sets: Invalid argument." #6790

Open georgetian3 opened 3 years ago

georgetian3 commented 3 years ago

When debugging a C++ file containing character arrays or strings, the contents of the array/string are not shown, but instead shows "\<error reading variable>" for character arrays and "Converting character sets: Invalid argument." for strings. Integer arrays are not affected:

bad

However, what puzzles me is that on a different machine but with the exact same VSCode version (1.52.1), C/C++ extension version (v1.1.3) and C++ compiler (mingw-w64 x86_64-8.1.0-posix-seh-rt_v6-rev0), and launch.json file, the contents of the array/string appears normally:

good

The contents of launch.json are given below:

{
    // 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:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin",
            "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++: g++.exe build active file"
        }
    ]
}

This problem has been posted elsewhere but with no solutions, as seen here and here

Steps to Reproduce:

  1. Create a character array in C++ file
  2. Start debugging

The issue still persists after disabling all extensions apart from the C/C++ tools extension.

WardenGnaw commented 3 years ago

Does the machine that are not displaying the characters have python installed? GDB visualizers are dependent on python.

georgetian3 commented 3 years ago

Python 3.9.0 is installed and is in PATH.

alanlivio commented 3 years ago

Hi. I have the same problem with python 3.8 in PATH. Is there a workaround?

alanlivio commented 3 years ago

I found a workaround in this chinese blog https://blog.csdn.net/m0_46304383/article/details/113487503 (translated with google transator). It seems a Windows 10 UTF-8 problem. I hope it can also help you @georgetian3.

The workaround is: "Windows Settings" -> "Time and Language" -> "Region" -> "Other Date -> Time and Regional Settings" -> "Change Date -> Time or Number Format" -> "Manage" -> "Change System Regional Settings" -> Uncheck "Beta version: Use Unicode UTF-8 to provide global language support" -> restart the computer

georgetian3 commented 3 years ago

That seems to have been the issue, problem solved, thank you!

alanlivio commented 3 years ago

Hi @georgetian3 I happy that worked for you. 👍 But, for me, the solution is only a work around. May the @WardenGnaw and cpptools team may evaluate if it is a bug that requeres be fixed.

georgetian3 commented 3 years ago

That's true, I'll reopen and hope they'll be able to have a look.

void-ecs commented 2 years ago

I had the same issue and this stackoverflow answer solved the problem perfectly

legends2k commented 1 year ago

Summary of the solutions:

  1. Turn off Unicode UTF-8 on Windows
  2. Enable UTF-8 on GDB

I find (2) to be better. Create a .gdbinit with set charset UTF-8 and things start working. I don't use VSCode but plain GDB on MSYS2 but found this issue discussion useful. Thanks!

Pika-Lee commented 1 year ago

Summary of the solutions:

  1. Turn off Unicode UTF-8 on Windows
  2. Enable UTF-8 on GDB

I find (2) to be better. Create a .gdbinit with set charset UTF-8 and things start working. I don't use VSCode but plain GDB on MSYS2 but found this issue discussion useful. Thanks!

Thanks for your awesome answer! If combining 1 (turn on) and 2 (enable) together, the result is amazing! (fixed 2 bugs simultaneously)

xparq commented 3 months ago

FTR, one additional glitch I've found with GDB: if you have set new-console in its config (which is kinda the only sane way to use it with stuff printing lots of diag. messages to stderr), then despite set charset UTF-8 and despite setting up the local terminal code page with chcp 65001, the opened new terminal window will have the wrong idea about the code page (and conversions). If you disable set new-console (opening new terminals for executables), and just use the same terminal GDB is using, it's fine. (It looks like GDB doesn't initialize the new console properly, forgetting about replicating the code page (conversion) of the original.)