Is it possible to debug an ESP32 without uploading the code.
For testing it would be nice to debug basic code, what does not effect the ESP32 core.
For Instance String routines, Usually I do the off Code debugging with Visual Studio but sadly they don’t have a a String Class like the Arduino environment.
You can test Arduino logic like Strings etc. against Mock libraries running on the computer with PlatformIO’s unit test feature. See the repository GitHub - FabioBatSilva/ArduinoFake: Arduino mocking made easy and especially the String
test.
Wow will try it thank you Max. Have a wonderful week
When I try to compile I get
c:/mingw/bin/…/lib/gcc/mingw32/8.2.0/…/…/…/…/mingw32/bin/ld.exe: c:/mingw/bin/…/lib/gcc/mingw32/8.2.0/…/…/…/libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain@16’
the main file is in the src Folder what I’m missing
Hm I got the same error because they don’t define main()
in the library itself.
But what works is taking the wiring-blink
example and adding
test_build_project_src = yes
to the platformio.ini
and then using pio test
.
C:\Users\Maxi\Desktop\ArduinoFake-master\examples\wiring-blink>pio test -v
Collected 1 items
Processing * in native environment
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Building...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Framework incompatible library C:\Users\Maxi\.platformio\lib\MCCI LoRaWAN LMIC library
Framework incompatible library C:\Users\Maxi\.platformio\lib\Nextion_ID37
Framework incompatible library C:\Users\Maxi\.platformio\lib\dcf77_xtal_ID1482
Framework incompatible library C:\Users\Maxi\.platformio\lib\esp32ModbusRTU_ID5876
Found 1 compatible libraries
More details about "Library Compatibility Mode": https://docs.platformio.org/page/librarymanager/ldf.html#ldf-compat-mode
Scanning dependencies...
Dependency Graph
|-- <ArduinoFake> 0.2.2 (C:\Users\Maxi\Desktop\ArduinoFake-master\examples\wiring-blink\.pio\libdeps\native\ArduinoFake)
Building in release mode
g++ -o .pio\build\native\test\test_main.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -Isrc -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity test\test_main.cpp
gcc -o .pio\build\native\test\tmp_pio_test_transport.o -c -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -Isrc -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity test\tmp_pio_test_transport.c
g++ -o .pio\build\native\src\main.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -Isrc -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity src\main.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\ArduinoFake.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\ArduinoFake.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\ClientFake.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\ClientFake.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\FunctionFake.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\FunctionFake.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\PrintFake.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\PrintFake.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\SerialFake.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\SerialFake.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\StreamFake.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\StreamFake.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\arduino\IPAddress.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\arduino\IPAddress.cpp
g++ -o .pio\build\native\lib400\ArduinoFake\arduino\WString.o -c -std=gnu++11 -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\arduino\WString.cpp
gcc -o .pio\build\native\lib400\ArduinoFake\arduino\noniso.o -c -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\libdeps\native\ArduinoFake\src -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity .pio\libdeps\native\ArduinoFake\src\arduino\noniso.c
gcc -o .pio\build\native\UnityTestLib\unity.o -c -DPLATFORMIO=40400 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -I.pio\build\native\UnityTestLib -IC:\Users\Maxi\.platformio\packages\tool-unity C:\Users\Maxi\.platformio\packages\tool-unity\unity.c
ar rc .pio\build\native\libUnityTestLib.a .pio\build\native\UnityTestLib\unity.o
ranlib .pio\build\native\libUnityTestLib.a
ar rc .pio\build\native\lib400\libArduinoFake.a .pio\build\native\lib400\ArduinoFake\ArduinoFake.o .pio\build\native\lib400\ArduinoFake\ClientFake.o .pio\build\native\lib400\ArduinoFake\FunctionFake.o .pio\build\native\lib400\ArduinoFake\PrintFake.o .pio\build\native\lib400\ArduinoFake\SerialFake.o .pio\build\native\lib400\ArduinoFake\StreamFake.o .pio\build\native\lib400\ArduinoFake\arduino\IPAddress.o .pio\build\native\lib400\ArduinoFake\arduino\WString.o .pio\build\native\lib400\ArduinoFake\arduino\noniso.o
ranlib .pio\build\native\lib400\libArduinoFake.a
g++ -o .pio\build\native\program.exe .pio\build\native\test\test_main.o .pio\build\native\test\tmp_pio_test_transport.o .pio\build\native\src\main.o -L.pio\build\native -Wl,--start-group .pio\build\native\lib400\libArduinoFake.a .pio\build\native\libUnityTestLib.a -Wl,--end-group
Testing...
test\test_main.cpp:36:test_setup [PASSED]
test\test_main.cpp:37:test_loop [PASSED]
-----------------------
2 Tests 0 Failures 0 Ignored
OK
======================================================================================================== [PASSED] Took 6.16 seconds ========================================================================================================
Test Environment Status Duration
------ ------------- -------- ------------
* native PASSED 00:00:06.160
======================================================================================================== 1 succeeded in 00:00:06.160 ========================================================================================================
Thx Max
I figured out that we have to use the original c Syntax like
#include "ArduinoFake.h"
#include <stdio.h>
String a;
void setup()
{
a = "Matin Michael";
}
int main()
{
setup();
printf("hello is String %s working\n" , a.c_str() );
Serial.print("Hallo");
}
this works but one thing to do the main reason Debugging when I try now I get error
Error: Please setup environments in platformio.ini
file
my ini is
[env:native]
platform = native
build_flags = -std=gnu++11
When I add build_type = debug I can see that the Program.exe file changes from 900 Kb to 6 MB file so the debug information are added but the debugger does not start and PlatformIO tells me somerhing is missing in platforIO. inni
Debugging of native applications is not natively possible (Using platformio and vscode to debug native linux (x86_x64) application - #2 by ivankravets) but
- that’s why there are unit tests to run statically using unity + unit test frameworks (Redirecting... and ArduinoFake/test_my_service.cpp at master · FabioBatSilva/ArduinoFake · GitHub)
- can be added manually
E.g. with the src\main.cpp
code as
#include "ArduinoFake.h"
String a;
void setup()
{
a = "Matin Michael";
}
void loop()
{
printf("hello is String %s working\n" , a.c_str() );
}
int main() {
printf("Main\n");
setup();
while(true) {
//serialEventRun();
loop();
}
return 0;
}
and a platformio.ini
with
[env:native]
platform = native
build_flags = -std=gnu++11
lib_deps = ArduinoFake
# lib_deps = git@github.com:FabioBatSilva/ArduinoFake.git
test_build_project_src = yes
build_type = debug
Then press “Build” as normal.
You then open the .vscode\launch.json
in VSCode and press the “Add configuration” → “GDB: Start” configuration with adapted path information e.g.
Adapt the program
and miDebuggerPath
accordingly to the location of the compiled program and GDB path. Found mine using
>where gdb
C:\Program Files (x86)\mingw-w64\i686-7.2.0-posix-dwarf-rt_v5-rev1\mingw32\bin\gdb.exe
Now you can select the newly created configuration in the Debug menu
And press the “Run” button to debug the program
You see that the string was printed perfectly fine.
Changes to the launch.json
will be overwritten by PlatformIO when the IntelliSense is rebuilt, so it’s wise to keep the new configuration in a notepad file or something.
Thank you Max
that is my launch.json
// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
// PIO Unified Debugger
//
// Documentation: https://docs.platformio.org/page/plus/debugging.html
// Configuration: https://docs.platformio.org/page/projectconf/section_env_debug.html
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Anfügen",
"type": "cppdbg",
"request": "attach",
"program": "Programmnamen eingeben, z. B. \"${workspaceFolder}/.pio/build/native/program.exe",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "Programmnamen eingeben, z. B. \"${workspaceFolder}/.pio/build/native/program.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug",
"executable": "d:/__Dokus/Documents/PlatformIO/ArduinoFake-0.2.2/.pio/build/native/program2.exe",
"toolchainBinDir": "",
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug"
},
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (skip Pre-Debug)",
"executable": "d:/__Dokus/Documents/PlatformIO/ArduinoFake-0.2.2/.pio/build/native/program2.exe",
"toolchainBinDir": "",
"internalConsoleOptions": "openOnSessionStart"
}
]
}
my ini
[env:native]
platform = native
build_flags = -std=gnu++11
lib_deps = ArduinoFake
# lib_deps = git@github.com:FabioBatSilva/ArduinoFake.git
test_build_project_src = yes
build_type = debug
PlatformIO always tries to overwrite my launch.json File and still PlatformIO complains about something missing in platformio.ini
When I change program.exe to “executable”: “d:/__Dokus/Documents/PlatformIO/ArduinoFake-0.2.2/.pio/build/native/program2.exe”,
PlatformIO does not care it is writing a Program.exe file that is my Problem PlatforIO does not access/care the launch.json file THe overwriting is solve can do this in launch.json.tpl
Even when I delete launch.json compiling is fine. so where does PlatformIO get the information about linking to make a program.exe file
You must select the new launch configuration name in the Debug panel, not the PlatformIO Unified Debugger (which will complain). Also your config uses the “attach” config and not the launch one.
Also your config uses the “attach” config and not the launch one.
I don’t understand attach config what file name is this.
When I start Debug with gdb start … I get program.exeare missing but there is a prgram.exe is this folder with 7.5 M bytes
I got it Thx Max
i had to change
"Programmnamen eingeben, z. B. “${workspaceFolder}/.pio/build/native/program.exe”,
with
“d:/__Dokus/Documents/PlatformIO/ArduinoFake-0.2.2/.pio/build/native/program.exe”
Thank you Max for your Patience
That’s the launch.json configuration part which I used:
{
"name": "(gdb) Start",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/.pio/build/native/program.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-posix-dwarf-rt_v5-rev1\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Pretty printing for \"gdb\"",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},