Unable to Debug

Hi,
I have a project that is using the Bear SLL libraries for Arduino.

When I use the Platform IO tick to compile, it all compiles fine, downloads and runs on the Arduino Nano IoT, but if I use the debugger in VSCode, it fails with a complication error message in one of the files

i15_montmul.c: r7 cannot be used in asm here

What really confuses me is I can compile and run this without debug, but not with which I think means there is some difference in the VSCode setup between the Debug and the Running.

If I do an Inspect from the Platform IO page, I get this error, which seems to correlate with the error during compiling.

PIO Core Call Error: "Processing nano_DEBUG (platform: atmelsam; board:
nano_33_iot; framework: arduino)\n--------------------------------------------------------------------------------\nVerbose mode can be enabled via -v, --verbose option\nCONFIGURATION: https://docs.platformio.org/page/boards/atmelsam/nano_33_iot.html\nPLATFORM: Atmel SAM (6.3.1) > NANO 33 IoT\nHARDWARE: SAMD21G18A 48MHz, 32KB RAM, 256KB Flash\nDEBUG: Current (jlink) External (atmel-ice, blackmagic, jlink)\nPACKAGES: \n - framework-arduino-samd 1.8.11 \n - framework-cmsis 1.40500.0 (4.5.0) \n - framework-cmsis-atmel 1.2.2 \n - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)\nLDF: Library Dependency Finder → http://bit.ly/configure-pio-ldf\nLDF Modes: Finder ~ chain, Compatibility ~ soft\nFound 18 compatible libraries\nScanning dependencies…\nDependency Graph\n|-- 1.7.0\n| |-- 1.3.5\n| | |-- 1.0\n| |-- 1.0\n|-- 1.8.12\n| |-- 1.0\n|-- 1.3.5\n| |-- 1.0\n|-- 6.18.1\n|-- 0.1.5\n|-- \nBuilding in debug mode\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/i15_montmul.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/i32_tmont.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/i62_modpow2.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/md5.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/md5sha1.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/mgf1.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/multihash.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/pemdec.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/pemenc.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/poly1305_ctmul.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/poly1305_ctmul32.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/poly1305_ctmulq.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/poly1305_i15.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/prf.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/prf_md5sha1.c.o\nCompiling .pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/prf_sha256.c.o\n\n\n\n.pio/libdeps/nano_DEBUG/ArduinoBearSSL/src/bearssl/i15_montmul.c: In function ‘br_i15_montymul’:\n.pio/libdeps/nano_DEBUG/ArduinoBearSSL/src/bearssl/i15_montmul.c:184:1: error: r7 cannot be used in asm here\n }\n ^\n*** [.pio/build/nano_DEBUG/libd74/ArduinoBearSSL/bearssl/i15_montmul.c.o] Error 1\n========================== [FAILED] Took 6.20 seconds ==========================\nEnvironment Status Duration\n------------- -------- ------------\nnano_DEBUG FAILED 00:00:06.196\n==================== 1 failed, 0 succeeded in 00:00:06.196 ====================“

My Platformio.ini file appears to be identical

default_envs = nano_DEBUG

[env:nano_RUNNING]
platform = atmelsam
board = nano_33_iot
framework = arduino

lib_deps =
   arduino-libraries/ArduinoBearSSL @ ^1.7.0
   arduino-libraries/WiFiNINA @ ^1.8.12
   arduino-libraries/ArduinoECCX08 @ ^1.3.5
   bblanchon/ArduinoJson @ ^6.18.0
   arduino-libraries/ArduinoMqttClient @ ^0.1.5

[env:nano_DEBUG]
platform = atmelsam
board = nano_33_iot
framework = arduino

lib_deps =
   arduino-libraries/ArduinoBearSSL @ ^1.7.0
   arduino-libraries/WiFiNINA @ ^1.8.12
   arduino-libraries/ArduinoECCX08 @ ^1.3.5
   bblanchon/ArduinoJson @ ^6.18.0
   arduino-libraries/ArduinoMqttClient @ ^0.1.5
debug_tool = jlink
debug_init_break = tbreak setup
upload_protocol = jlink
debug_build_flags = -g3

Although the .ipo/lib_deps list is different between the 2 environments, the RUNNING environment has ArduinoMQTT listed, even if it is not in the list above.

Does anyone have any ideas of how this could be resolved so I can debug code again?

Exactly, in debug mode you’re compiling with -Og optimization (default debug_build_flags). You’ll get the same effect when you add

build_type = debug

(docs) in your debug environment, then a normal “Build” will build in debug mode, just as what the “PIO Debug” configuration uses.

That’s a compiler-internal error when you’re trying to compile a C file which has explicit assembly instructions which clashes with the code optimization flags. The offending file is

Two ways:

  • chose a higher or different optimization setting than the default -Og through the debug_build_flags, such as -O0, -O1, -O2, etc. The compiler may than be able to build the code, at the cost of a slightly worse debugging experience (optimized code likes to jump around a lot and optimize away parameters)
  • make BearSSL not use the optimized assembly when compiling in debug mode. As I’ve linked above, the macro BR_ARMEL_CORTEXM_GCC controls whether BearSSL uses low-level ARM assembly implementations for some critical functions. As you can see in

the macro is defined there in the config.h, So, if you locally go into .pio\libdeps\nano_DEBUG\ArduinoBearSSL\src\bearssl\config.h and rewrite it to #define BR_ARMEL_CORTEXM_GCC 0, BearSSL shouldn’t be using assembly optimization and you should be able to debug. For a more permanent way you should fork the library so that it does not always set BR_ARMEL_CORTEXM_GCC to 1 but e.g. checks for an #ifndef DEBUG macro so that you can then set debug_build_flags = -D DEBUG -Og -ggdb3 to disable the code for debug sessions.

EDIT: Updated to the actual correct place to modify the macro