Skip to content

Exceptions are not enabled correctly for Arduino-ESP8266 #237

@maxgerhardt

Description

@maxgerhardt

The documentation (https://docs.platformio.org/en/latest/platforms/espressif8266.html#exceptions) tells us that in order to activate exceptions, we must add

; Remove default exceptions disabled flag
build_unflags = -fno-exceptions

; Enable exceptions
build_flags = -fexceptions

to the platformio.ini.

This is wrong for Arduino-ESP8266, as documented in this topic: https://community.platformio.org/t/platformio-cannot-handle-exceptions-with-esp8266/16738

The problem is that when a user selects "Exceptions: Enabled" in the Arduino IDE, it links the firmware against a different C++ standard library which has exceptions enabled: libstdc++-exc.a instead of libstdc++.a. This can be seen in https://github.com/esp8266/Arduino/tree/master/tools/sdk/lib.

The framework's builder code links blindly only against the non-exception-enabled C++ library

https://github.com/esp8266/Arduino/blob/7f38e141c7502032af594fdd502c1b1f6049377c/tools/platformio-build.py#L124-L128

This should be adapted so that e.g a macro like PIO_FRAMEWORK_ARDUINO_EXCEPTIONS_ENABLED changes the LIBS correctly.

For testing, the code

#include <Arduino.h>
#include <iostream>
#include <exception>

using namespace std; 

void func(void) {
    throw runtime_error("exception thrown.");
}

void setup() { 
    Serial.begin(115200);
    delay(200);
  
    cout << "Program started." << endl;
    try {
        func();
    } catch (const exception& e) {
        cout << "exception caught: " << e.what() << endl;
    }
    Serial.println("Program continues");
}

void loop() {
    while(true) {
        yield();
    }
}

and platformio.ini

[env:esp8266]
platform = espressif8266
board = nodemcuv2
framework = arduino
build_unflags = -fno-exceptions
build_flags = -fexceptions

Without a modified LIBS value (replace stdc++ with stdc++-exc), the firmware does an immediate abort() when an exception is encountered, even though it is catched.

User exception (panic/abort/assert)
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Abort called

            stack>>>

ctx: cont
sp: 3ffffe60 end: 3fffffc0 offset: 0000
3ffffe60: 00000000 00000000 00000000 00000000
3ffffe70: 000000fe 00000000 00000000 00000000
3ffffe80: 00000000 00000000 00000000 b8b1aabc
3ffffe90: 3fff0230 3fff1aac 00000000 3ffeee40
3ffffea0: 3ffeedd8 3ffef348 3fff1adc 40201f12
3ffffeb0: 00000000 00000000 00000000 40201f24
3ffffec0: 00000000 3fffff5c 00000000 402018e9
3ffffed0: 3fffff54 00000000 00000000 00000000
3ffffee0: 00000000 00000000 00000000 40212d30
3ffffef0: 00000000 3ffeee40 3ffeedd8 40212d41
3fffff00: 3fff1adc 3fff1aac 3fff1adc 40213534
3fffff10: 00000000 00000000 3fff1adc 402018f4
3fffff20: 40000000 00000000 00000000 00000000
3fffff30: 00000000 00000000 00000000 40212d30
3fffff40: 3ffeedd8 3ffeee40 3ffeedd8 40212d41
3fffff50: 3fff1adc 40213468 3fff1adc 402134df
3fffff60: f3f2f101 3ffef348 3fff1afc 40201060
3fffff70: 3fff1b18 3ffef348 3ffefe38 402147fe
3fffff80: 3fffdad0 3ffef348 3ffeedd8 402010e2
3fffff90: feefeffe feefeffe feefeffe feefeffe
3fffffa0: 3fffdad0 00000000 3ffeee00 40201b20
3fffffb0: feefeffe feefeffe 3ffe85b4 40100c11
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

in the fixed version the output is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions