Platformio-core: Add GDB debugger support for native platform

Created on 9 Jun 2017  Â·  23Comments  Â·  Source: platformio/platformio-core

Some of my projects include shared components targeting desktop machines as well as microcontrollers. It would be extremely helpful to have the PIO debugger integrated with GDB to enable local native debugging.

debugging feature

Most helpful comment

To be honest - it's a little bit disappointing that there is no progress in this issue after more than 3 years. This feature would be really helpful.

All 23 comments

Since there is no progress on this for (close to) 3 years, I'll provide my hotfix for linux (might work on mac as well, haven't tested that):
(note: this breaks debugging for platforms other than native. instructions to undo can be found at the bottom)

  1. open .vscode/launch.json
  2. delete its contents and replace it with (for example) the following:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/.pio/build/debug/program",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  1. change the file permissions to 444

If you want to change the file, change its permissions back to (f.e.) 666, change the contents and change the permissions back to 444.

Does that means currently I can not use the windows_x86 platform and debug via mingw gdb?

To be honest, I haven't thought of windows. You can try generating a configuration automatically from within vscode (there should be a button when you open the launch.json file) but I don't know how one would prevent platformio from overwriting it

I tried this

        {
            "name": "(gdb) Starten",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/.pio/build/windows_x86/program.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/.platformio/packages/toolchain-gccmingw32/bin",
            "setupCommands": [
                {
                    "description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }

But my miDebuggerPath seems to bad according to the pop up message.
But this path exsits. Hmm ...

worst-case you can always use gdb directly (optionally with a custom .gdbinit such as pwndbg).

Yes, that would really be the worst-case. :-) Having a nice looking IDE and using gdb on console. LOL

@ivankravets hey there - is there any chance to get this feature in the next time?

Atm i'm playing around with jumper.io 's virtual Lab.
But for debugging the code in the sim i need a native GDB solution - see: https://docs.jumper.io/docs/intgdb.html#visual-studio-code

Adding in the launch.json manually and reduce permissions to prevent for overwriting is not a nice solution at all :-D

Sorry, currently we do not have plans to implement this. It is still in our TODO list :(

Is there another way to configure this manually and prevent PIO for overwritting this every time?
Whats about debug_tool = custom? Is there a way to use this?

EDIT:
@qwerty123443 you can add your config also to the template file:
https://github.com/platformio/platformio-core/blob/b7ac59066fb9cda8f290ffda32afca020c9a72a5/platformio/ide/tpls/vscode/.vscode/launch.json.tpl
located under C:\Users\###\.platformio\penv\Lib\site-packages\platformio\ide\tpls\vscode\.vscode at my system. Then the auto-update from PIO will add your config as well to the given two.
Not as nice as a native implmenentation - but it works for the moment ...

you can add your config also to the template file:
https://github.com/platformio/platformio-core/blob/b7ac59066fb9cda8f290ffda32afca020c9a72a5/platformio/ide/tpls/vscode/.vscode/launch.json.tpl
located under C:\Users\###\.platformio\penv\Lib\site-packages\platformio\ide\tpls\vscode\.vscode at my system. Then the auto-update vrom PIO will add your config as well to the given two.

I see, haven't looked at this much. Won't this break with every platformIO update, however? (as, I think, that overwrites your penv folder)

@qwerty123443 Yes i think you're right - that maybe get lost after an update :(

You can disable it with https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-ide-autorebuildautocompleteindex

Thanks for this hint - I'll try this.

Is there a way to use a other (local installed) gdb with debug_tool = custom and connect to a still running gdbserver over tcp?

EDIT:

You can disable it with https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-ide-autorebuildautocompleteindex

Am i right, that this option will prevent from building all - also the intellisense (auto-complete) ?
If thats true - thats a really bad solution for development

I just can't believe that I can't tap an external debugger with platformIO - thats a really common usecase in the real world, that i want to debug a running system over a simple GDB <-> GDBServer connection.

Hey, great project, congrats!
It would be great to have this feature.

To be honest - it's a little bit disappointing that there is no progress in this issue after more than 3 years. This feature would be really helpful.

I have had success getting the VSCode debugger to launch by pasting the right values into launch.json, but of course it doesn't persist….

{
  "name": "Launch Sim",
  "type": "cppdbg",
  "request": "launch",
  "program": "${workspaceFolder}/.pio/build/simulator/TheSimulator",
  "miDebuggerPath": "/usr/local/bin/gdb",
  "MIMode": "gdb",
  "cwd": "${workspaceFolder}/.pio/build/simulator"
}

For the native build option in our project we only need support for native gdb debugging at the VSCode level and we can launch the VSCode debugger manually. It doesn't need to be integrated with PlatformIO beyond keeping our custom values in launch.json. If we could do it just by adding text to platformio.ini that would be great, but all we really need is an Advanced Scripting hook that will allow us to modify launch.json before it gets written out.

Thanks all for your ideas. Yes, it makes sense to use directly cppdbg debugger. We will add support for this soon.

Not sure what's possible on macOS, where gdb no longer works. I'm always forced to move to Linux for debugging a native build - this is obviously not PIO's fault, but I'd be nice to get _some_ sort of of cmd-line based debugging going - some day.

PS. This not a statement about macos vs linux, the former just happens to be my main dev environment :)

macOS, where gdb no longer works

What's interesting is that I did get the debugger in VSCode working at one point through ggdb in the MacPorts install. I don't recall whether I added any extra compile flags or did anything special to get the debugger to start working, but it was showing the current state of variables, single-stepping, and working reliably for a little while there.

However, in my more recent testing I have not been able to get the debugger to run reliably in VSCode, and in fact now it always gets hung up when trying to start the process, regardless of how I launch ggdb.

Maybe I just had a brief spot of good weather, but I'll continue to experiment and see whether any reliable debugging is possible on this system with some version of g++ and ggdb.

I have this config working on VSCode on Mac. It's generated by default using "add configuration button" and selecting "(lldb) Launch" option.

   {
       "name": "(lldb) Launch",
       "type": "cppdbg",
       "request": "launch",
       "program": "${workspaceFolder}/.pio/build/native/program",
       "args": [],
       "stopAtEntry": false,
       "cwd": "${workspaceFolder}",
       "environment": [],
       "externalConsole": false,
       "MIMode": "lldb"
   }

I am using command palette (Ctrl-Shift-P) and select 'C/C++: Build and Debug Active File' -> 'gcc build and debug active file' to start debugger. Works on Win10 and remote WSL:Ubuntu environment.

It's generated by default using "add configuration button" and selecting "(lldb) Launch" option.

If you make any edit to the platformio.ini file the launch option you added will be removed, and you will need to regenerate it again the next time you want to start a debug session. If your binary isn't named program (as is typical) then you will need to change the program field to match your binary's actual name.


As a side topic, I would like to hear from developers on macOS who have gotten either the gdb or gdb-apple packages (from MacPorts) working reliably. Our native build (and PlatformIO in-general) seems to be incompatible with LLVM and requires the use of a gcc that has been installed separately, and not all gcc / gdb combinations are created equal.

I went through the system rigamarole to give gdb the permissions it needs to launch and access other programs, and I had a brief success debugging with some combination of gcc / gdb versions. However, since my initial success I have not found a combination of compiler and debugger that can launch without hanging the process.

I'll be sure to ask around the PlatformIO community forum next time I'm on that task.

Here's a tip to automatically build before starting a session, and to get working stack traces.

First, put the following in platformio.ini (unimportant lines omitted):

[env:simulator]
platform = native
build_type = debug

Then, add the following section to your launch.json:

    {
      "name": "Debug simulator",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/.pio/build/simulator/program",
      "args": [],
      "environment": [],
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "PlatformIO: Build (simulator)"
    }

Note the preLaunchTask which refers to build the simulator env (as I called it), and the build_type = debug in that target.

There is no need to clear out any other debug configurations, just add yours to the existing ones, but as others noted, you'll have to do this every time you make a change to platformio.ini.

Was this page helpful?
0 / 5 - 0 ratings