GDB in Visual Studio Code doesn't show printf() output to stdout

Viewed 7465

When debugging a C project in VSCode with GDB, the output to stdout (through printf()) does not appear on the Debug Console. When using cppvsdebug, however, it works.

This is my launch.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": "Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
              {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": false
              }
            ],
            "windows": {
              "miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe",
            },
            "osx": {
              "MIMode": "lldb"
            },
            "linux": {
              "miDebuggerPath": "/usr/bin/gdb",
            },
            "preLaunchTask": "build debug"
          },
          {
            "name": "Run",
            "type": "cppdbg",
            "request": "launch",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "program": "${workspaceFolder}/main",
            "MIMode": "gdb",
            "windows": {
              "program": "${workspaceFolder}/main.exe",
              "miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe"
            },
            "osx": {
              "MIMode": "lldb"
            },
            "linux": {
              "miDebuggerPath": "/usr/bin/gdb"
            },
            "preLaunchTask": "build release",
          }
        ]
      }

This is the command that is executed to run the debug session (according to the terminal window):

'c:\Users\...\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-2dvbqvix.1cb' '--stdout=Microsoft-MIEngine-Out-1m5j1iy0.ufe' '--stderr=Microsoft-MIEngine-Error-oqyothgz.h5l' '--pid=Microsoft-MIEngine-Pid-hal3wx4b.uld' '--dbgExe=C:/raylib/mingw/bin/gdb.exe' '--interpreter=mi'

The Debug Console shows the "splash screen" info of GDB, but doesn't display any of the output from the printf() statements in my code:

=thread-group-added,id="i1"
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
[New Thread 9488.0x54dc]
[New Thread 9488.0x577c]
[New Thread 9488.0xff4]
[New Thread 9488.0x4918]

When I run the Exe file with GDB from the commandline (Git Bash), all the output is displayed normally.

Setting externalConsole to true doesn't work (it also doesn't open an external console to begin with).

I'm on Windows 10.

Is there any specific setting I need to adjust for this to work?

5 Answers

Setting "externalConsole" to true solved the issue for me.

I ended up somewhat fixing this. In my launch.json I set filterStdout to true (I know it's default but lol): https://code.visualstudio.com/docs/cpp/launch-json-reference#_filterstdout

Setting filterStdout to false does the same thing. So I'm sure there's a bug somewhere. I believe the intention is that setting it to true is supposed to get stdout output into the Debug Console tab.

Now when I call my method that calls printf, I just go into the terminal tab and lo and behold my call is in the Terminal tab!!!!

fflush(stdout);

after your printf statement worked for me.

printf writes to the output stream stdout. stdout waits to be flushed (so it can then be displayed in the terminal). It must be flushed to be displayed, which is normally accomplished via newline characters (\n) since stdout is line-buffered. (gh)

I had this problem just now, too, the program output was not shown in the "Debug Console". Restarting Visual Studio Code helped, now the output is visible again.

It will however output on newline.. so as a workaround, add a '\n' to flush.

Related