Debugging with PlatformIO: Part 1. Back to the Basics

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