Debugging with PlatformIO: Part 1. Back to the Basics

Debugging is an inseparable part of the development process. People make mistakes, so our code doesn’t do what we expect once in a while. Often such problems happen at the worst possible time. That’s why it’s a good idea to master debugging techniques and skills to more precisely isolate the source code of a problem and fix it in a timely manner.

In the Debugging with PlatformIO series, we will be exploring the debugging capabilities of the PlatformIO ecosystem. Read more :point_right:

4 Likes

Hi,
Thanks for this great article. It helps a lot. It works perfectly when I use Visual Studio Code but I get an popup window with an error message when I try to use the debugger with VSCodium.
The error message I am getting is :
Failed to load symbols from executable file: ENOENT: no such file or directory, scandir

I have not found a hit in google yet. Is there something particular I need to configure in VSCodium ?
Any suggestion on how to progress to fine what is wrong will be greatly appreciated.
Best regards
Bruno

PlatformIO is designed for Microsoft VSCode and has several issues with VSCode derivatives, e.g. per Publish extensions to open-vsx · Issue #1802 · platformio/platformio-vscode-ide · GitHub for code intellisense.

If debugging is not working properly in some derivative, try opening an issue at Issues · platformio/platformio-vscode-ide · GitHub to get it tracked.

1 Like

This is a great article and it works perfectly for me. I have a question though…
I have a library project I have written which I include in many of my Arduino projects. I would now like to add unit tests to this library project, however I am confused about the use of src/include/lib folders. My library project uses src/include folders for the code. All the examples I have seen for creating unit tests require the source code to be in the lib folder. This implies either:

  1. I shouldnt be using the src/include folders for the code in my library project, I should put it in lib

…or…

  1. I can’t have unit tests in the same project as my library code. I.e. I must create a dedicated test project and copy the source code from my library project into the lib folder of the test project.

Could someone please clarify this for me.

Thanks a lot.

Could you share a link to your library?

I cant send a link because It is a private library. However my question was for a very simple generalised case, for any library written for Arduino. Assuming the src/include folders are being used for the library source files (multiple h/cpp files each for a class of single responsibility), is it possible to use the “test” folder in that same project to write unit tests for those classes? It seems I have to create a new project where the source for the system under test resides within the lib folder.

So my library project is structured like this (showing how I would LIKE the test folder to be used):

|--lib (empty, no external dependencies)
|--include
|  |- reader.h
|  |- writer.h
|--src
|  |- reader.cpp
|  |- writer.cpp
|--test
|  |- main.cpp
|  |- reader_tests.cpp
|  |- writer_tests.cpp

However it seems from all the examples that the structure has to be like this:

|--lib
|  |--my_library
|  |  |- reader.h
|  |  |- reader.cpp
|  |  |- writer.h
|  |  |- writer.cpp
|--include (empty)
|--src
|  |- main.cpp (entry for test)
|--test
|  |- reader_tests.cpp
|  |- writer_tests.cpp

which implies I cannot have the tests within the same project as my source.

Thanks again for your help.

For unit tests, the code in src/ is not compiled unless test_build_project_src = yes.

Code to be unit-tested should optimally be its own library.

Note the notes that the documentation has on this and the “Warning”.

1 Like

Hello, I tried to follow the article, but I’m stuck at the " Working with Debugger’ step …
When I try to start the debugger, it doesn’t work. I do not get the debug view with the control actions and so on. Everything is empty. All I get is an error message in the debug console :

undefinedunknown option --directory

usage: /Library/Frameworks/Python.framework/Versions/3.12/Resources/Python.app/Contents/MacOS/Python [option] … [-c cmd | -m mod | file | -] [arg] …

Try `python -h’ for more information.

I couldn’t find anything about this error on google.

It seems to be related to python more than platformio directly. But pyhton is correctly running on my computer and I tried upgrading it but it is already the last version.

So I don’t know how to solve the problem.

My computer is a macbook with a M2 processor. Since this is an ARM architecture maybe It can cause some troubles. I don’t know …

Can you help me please ?

Guillaume

Could you share the contents of your platformio.ini file?

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…