Skip to content

Commit

Permalink
Dump std::exception.what() when possible
Browse files Browse the repository at this point in the history
When doing the panic on unhandled exceptions, try and grab the
.what() pointer and dump it as part of the termination info.
Makes it easy to see mem errors (std::bad_alloc) or std::runtime_error
strings.
  • Loading branch information
earlephilhower committed Nov 30, 2018
1 parent 9e05e46 commit f5e80fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 19 additions & 2 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,31 @@ static void do_global_ctors(void) {
(*--p)();
}

extern "C" { extern void __unhandled_exception(); }
extern "C" {
extern void __unhandled_exception(const char *str);

static void __unhandled_exception_cpp()
{
static bool terminating;
if (terminating)
abort();
terminating = true;
/* Use a trick from vterminate.cc to get any std::exception what() */
try {
__throw_exception_again;
} catch (const std::exception& e) {
__unhandled_exception( e.what() );
} catch (...) {
__unhandled_exception( "" );
}
}

}

void init_done() {
system_set_os_print(1);
gdb_init();
std::set_terminate(__unhandled_exception);
std::set_terminate(__unhandled_exception_cpp);
do_global_ctors();
esp_schedule();
}
Expand Down
8 changes: 4 additions & 4 deletions cores/esp8266/core_esp8266_postmortem.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static const char* s_panic_func = 0;
static const char* s_panic_what = 0;

static bool s_abort_called = false;
static bool s_unhandled_exception = false;
static const char* s_unhandled_exception = NULL;

void abort() __attribute__((noreturn));
static void uart_write_char_d(char c);
Expand Down Expand Up @@ -121,7 +121,7 @@ void __wrap_system_restart_local() {
ets_putc('\n');
}
else if (s_unhandled_exception) {
ets_printf_P("\nUnhandled exception\n");
ets_printf_P("\nUnhandled exception: %s\n", s_unhandled_exception);
}
else if (s_abort_called) {
ets_printf_P("\nAbort called\n");
Expand Down Expand Up @@ -237,8 +237,8 @@ void abort() {
raise_exception();
}

void __unhandled_exception() {
s_unhandled_exception = true;
void __unhandled_exception(const char *str) {
s_unhandled_exception = str;
raise_exception();
}

Expand Down

0 comments on commit f5e80fb

Please sign in to comment.