Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GDB debugger support for native platform #980

Closed
buzzdavidson opened this issue Jun 9, 2017 · 30 comments
Closed

Add GDB debugger support for native platform #980

buzzdavidson opened this issue Jun 9, 2017 · 30 comments

Comments

@buzzdavidson
Copy link

buzzdavidson commented Jun 9, 2017

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.

@qwerty123443
Copy link

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.

@BlueAndi
Copy link

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

@qwerty123443
Copy link

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

@BlueAndi
Copy link

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 ...

@qwerty123443
Copy link

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

@BlueAndi
Copy link

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

@Schrolli91
Copy link

Schrolli91 commented Apr 27, 2020

@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

@ivankravets
Copy link
Member

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

@Schrolli91
Copy link

Schrolli91 commented Apr 28, 2020

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 ...

@qwerty123443
Copy link

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)

@Schrolli91
Copy link

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

@ivankravets
Copy link
Member

@Schrolli91
Copy link

Schrolli91 commented Apr 28, 2020

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.

@gmagno
Copy link

gmagno commented May 14, 2020

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

@arduhe
Copy link

arduhe commented Jul 7, 2020

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.

@thinkyhead
Copy link
Contributor

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.

@ivankravets ivankravets added this to the 4.5.0 milestone Aug 15, 2020
@ivankravets
Copy link
Member

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

@jcw
Copy link

jcw commented Sep 1, 2020

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 :)

@thinkyhead
Copy link
Contributor

thinkyhead commented Sep 10, 2020

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.

@javiercuadrado
Copy link

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"
   }

@dillon1110
Copy link

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.

@thinkyhead
Copy link
Contributor

thinkyhead commented Sep 24, 2020

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.

@poelstra
Copy link

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.

@ivankravets
Copy link
Member

Debugging for native (desktop) applications is implemented. Please note that there are some issues with our VSCode extension. We will fix them soon. See platformio/platformio-vscode-ide#2447

In any case, using this configuration as @thinkyhead proposed is a good idea:

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

The latest PlatformIO Core does not overwrite launch.json anymore.

@ivankravets
Copy link
Member

A demo

pio debug --interface=gdb -x .pioinit

Screen Shot 2021-03-19 at 17 17 17

@jcw
Copy link

jcw commented Apr 10, 2021

To follow up on an old comment: native debugging on macOS with gdb still doesn't work for me, but lldb works fine and appears to be a drop-in replacement. So debugging on macOS is again possible. Thee's a popup eqch time you start debugging an app, the get permission to place the app under debugger control, which needs authorisation, that's all.

UPDATE: I haven't trid the latest changes mentioned above. Maybe it all works now. Just wanted to mention lldb as option.

@ivankravets
Copy link
Member

native debugging on macOS with gdb still doesn't work for me

I'm on the macOS. The screen above is mine. I used GDB from brew install gdb. You also need to generate a certificate, etc. There is instruction for this.

@thinkyhead
Copy link
Contributor

The latest PlatformIO Core does not overwrite launch.json anymore.

Great! I've been doing a lot of remote debugging lately in the CLI and it'll be great to get back into proper GUI mode.

@ivankravets
Copy link
Member

@thinkyhead please note that the latest PIO Core also has support for native debugging :) See https://github.com/platformio/platformio-core/blob/develop/HISTORY.rst

P.S: Happy to see how PlatformIO helps other open-source projects. Marlin is a great example!!! Great job! 🚀

@pharapeti
Copy link

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"
   }

Magic - just tried this and it worked first time with zero additional configuration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests