Skip to content

Commit f5e80fb

Browse files
Dump std::exception.what() when possible
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.
1 parent 9e05e46 commit f5e80fb

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

cores/esp8266/core_esp8266_main.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,31 @@ static void do_global_ctors(void) {
150150
(*--p)();
151151
}
152152

153-
extern "C" { extern void __unhandled_exception(); }
153+
extern "C" {
154+
extern void __unhandled_exception(const char *str);
154155

156+
static void __unhandled_exception_cpp()
157+
{
158+
static bool terminating;
159+
if (terminating)
160+
abort();
161+
terminating = true;
162+
/* Use a trick from vterminate.cc to get any std::exception what() */
163+
try {
164+
__throw_exception_again;
165+
} catch (const std::exception& e) {
166+
__unhandled_exception( e.what() );
167+
} catch (...) {
168+
__unhandled_exception( "" );
169+
}
170+
}
155171

172+
}
156173

157174
void init_done() {
158175
system_set_os_print(1);
159176
gdb_init();
160-
std::set_terminate(__unhandled_exception);
177+
std::set_terminate(__unhandled_exception_cpp);
161178
do_global_ctors();
162179
esp_schedule();
163180
}

cores/esp8266/core_esp8266_postmortem.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static const char* s_panic_func = 0;
4040
static const char* s_panic_what = 0;
4141

4242
static bool s_abort_called = false;
43-
static bool s_unhandled_exception = false;
43+
static const char* s_unhandled_exception = NULL;
4444

4545
void abort() __attribute__((noreturn));
4646
static void uart_write_char_d(char c);
@@ -121,7 +121,7 @@ void __wrap_system_restart_local() {
121121
ets_putc('\n');
122122
}
123123
else if (s_unhandled_exception) {
124-
ets_printf_P("\nUnhandled exception\n");
124+
ets_printf_P("\nUnhandled exception: %s\n", s_unhandled_exception);
125125
}
126126
else if (s_abort_called) {
127127
ets_printf_P("\nAbort called\n");
@@ -237,8 +237,8 @@ void abort() {
237237
raise_exception();
238238
}
239239

240-
void __unhandled_exception() {
241-
s_unhandled_exception = true;
240+
void __unhandled_exception(const char *str) {
241+
s_unhandled_exception = str;
242242
raise_exception();
243243
}
244244

0 commit comments

Comments
 (0)