Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions are not enabled correctly for Arduino-ESP8266 #237

Closed
maxgerhardt opened this issue Dec 28, 2020 · 1 comment
Closed

Exceptions are not enabled correctly for Arduino-ESP8266 #237

maxgerhardt opened this issue Dec 28, 2020 · 1 comment

Comments

@maxgerhardt
Copy link

maxgerhardt commented Dec 28, 2020

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.

@maxgerhardt maxgerhardt changed the title Exceptions are not enabled correctly Exceptions are not enabled correctly for Arduino-ESP8266 Dec 28, 2020
@maxgerhardt
Copy link
Author

@valeros I see that your PR in Arduino-ESP8266 has landed and added PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS but the docs were not updated yet at https://github.com/platformio/platformio-docs/commits/develop, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant