Debugging with PlatformIO: Part 1. Back to the Basics

It is the basic default configuration of the example

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter, extra scripting
;   Upload options: custom port, speed and extra flags
;   Library options: dependencies, extra library storages
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:native]
platform = native

Could you open PlatformIO Core CLI and type

pio upgrade --dev

Restart VSCode. Does it work now?

I upgraded pio and restarted vscode but I still have the same issue.

Can you build a project without any issues?

Yes, I have no problems for the build. It is only the debug part which has a problem.

Processing native (platform: native)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio/build/native/src/main.o
Linking .pio/build/native/program
========================================================================================= [SUCCESS] Took 0.29 seconds =========================================================================================
 *  Terminal will be reused by tasks, press any key to close it. 

What is your version of local gdb?

$ which gdb
$ gdb --versiom

After checking, gdb is not installed on my computer. And it seems to be impossible to install it on a arm processor. When i try to install it manually, I get :
gdb: The x86_64 architecture is required for this software.
Error: gdb: An unsatisfied requirement failed this build.

From what I could see on the internet, lldb should be used instead of gdb on mac m1 and m2 :

lldb is already installed on my computer : lldb-1500.0.22.8

But I don’t know if the platformio debugger can work with it instead of gdb.

Then you might use lldb per Debugging using macOS lldb instead of gdb - #3 by thomas.kilian.0815

1 Like

Late here already, so I look in romorrow. It was fairly easy once you know how to enable it. Will come back later with the details

Thank you both ! I don’t think I will have time to test it before xmas holidays. But I test it when I come back in January and I keep you informed.

@guillaume.costes Yeah LOL I just looked up my projects and it was more or less only once I really made a debug session and I can’t find a trace. Hopefully I’ll find the time to reconstruct it stll this year…

@thomas.kilian.0815 I’m not sure I did the right thing. Can you tell me what I did is correct please ?

In my platformio projet, I found inside the .vscode folder a file “launch.json” that I modified to add the configuration you mentioned. So my “launch.json” looks like this :

// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
//
// PlatformIO Debugging Solution
//
// Documentation: https://docs.platformio.org/en/latest/plus/debugging.html
// Configuration: https://docs.platformio.org/en/latest/projectconf/sections/env/options/debug/index.html

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Starten",
            "type": "cppdbg",
            "request": "launch",
            "program": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug",
            "executable": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "projectEnvName": "native",
            "toolchainBinDir": "/usr/bin",
            "internalConsoleOptions": "openOnSessionStart",
            "preLaunchTask": {
                "type": "PlatformIO",
                "task": "Pre-Debug"
            }
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug (skip Pre-Debug)",
            "executable": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "projectEnvName": "native",
            "toolchainBinDir": "/usr/bin",
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug (without uploading)",
            "executable": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "projectEnvName": "native",
            "toolchainBinDir": "/usr/bin",
            "internalConsoleOptions": "openOnSessionStart",
            "loadMode": "manual"
        }
    ]
}

And I modified my platformio.ini like this :

[env:native]
platform = native

build_flags = 
  -g 

But I still have the same problem when i try to start debugger.

I’m still busy with other things… I’ll try to look into it soon (latest the weekend)

Got it working again. Inside launch.json I hardcoded the executable path

 "version": "0.2.0",
  "configurations": [
    {
      "name": "(lldb) Starten",
      "type": "cppdbg",
      "request": "launch",
      "program": "/Users/thomaskilian/Documents/PlatformIO/Projects/240105-130152-hello-world/src/build/Debug/main.o",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb"
    },

I guess you can use env-vars, but that started the lldb. It will ask for passwort/fingerprint but that’s (more or less) okay since you can debug aftrewards. Keeps that for a while so you can restart debugging without the prompt.

1 Like

This does not seem like the program file created by PlatformIO, wihh would be <project folder>/.pio/build/<environment name>/program, so you might be using some other extension for building.

You don’t need to do this. Just add

[env:native]
...
build_type = debug

Build firmware in the debug mode and try debugging with cppdbg extension.

1 Like

Well, this is my path and you have to replace it with yours or use the env-vars. The path ia where PIO compiled the hello-world to.

Thank you @ivankravets I tried it. But if by “cppdbg extension” you mean the native debug extension for vscode, it is not working, I still have the same error.

Thank you for the tips @thomas.kilian.0815.

I modified my launch.json like this with my own main.o path:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Starten",
            "targetArchitecture": "arm64",
            "type": "cppdbg",
            "request": "launch",
            "program": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/src/main.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug",
            "executable": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "projectEnvName": "native",
            "toolchainBinDir": "/usr/bin",
            "internalConsoleOptions": "openOnSessionStart",
            "preLaunchTask": {
                "type": "PlatformIO",
                "task": "Pre-Debug"
            }
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug (skip Pre-Debug)",
            "executable": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "projectEnvName": "native",
            "toolchainBinDir": "/usr/bin",
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug (without uploading)",
            "executable": "/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/program",
            "projectEnvName": "native",
            "toolchainBinDir": "/usr/bin",
            "internalConsoleOptions": "openOnSessionStart",
            "loadMode": "manual"
        }
    ]
}

It is kind of working since I do not get the previous error. And if i forget to make a build, It tells me that my path “…/main.o” does not exists. But after building, I do not have this error, so I think it can find the main.o file.

BUT now i got this error :

ERROR: Unable to start debugging. Unexpected LLDB output from command "-exec-run". Command 'exec-run'. Invalid process during debug session
The program '/Users/guillaumecostes/Documents/PlatformIO/Projects/231218-164431-hello-world/.pio/build/native/src/main.o' has exited with code 42 (0x0000002a).

I did some research. I found the exact same issue on a forum :

so I tried the solution and installed the CodeLLDB extension, then modified “type”: “cppdbg”, to “type”: “lldb”. I tried again to debug. This time, I got a popup message with “Permission denied” that proposes me to check the launch.json file. So I check, some field of the configuration are highlighted as “not allowed properties” so i removed those fields, but I still get the “Permission denied” popup.

A made some research about the permission denied with debugger, but nothing that could match my case.

So I don’t know what to do to make it work.

You should make a new project from the hello-world template and try that before doing complicated stuff…

I’m using a simple projet, I just followed the tutorial. So it is based on the hello-world template, only the main code is modified with the add of a sort array function like in the tutorial. So I don’t think the problem comes from the project.