Skip to content

Commit 8bf1e98

Browse files
committed
Improve debug output on critical errors
1 parent 590eefa commit 8bf1e98

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

cores/esp8266/abi.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
*/
1818

1919
#include <stdlib.h>
20+
#include <assert.h>
21+
#include <debug.h>
2022
extern "C" {
2123
#include "ets_sys.h"
2224
#include "os_type.h"
2325
#include "osapi.h"
2426
#include "mem.h"
25-
#include "user_interface.h"
2627
}
2728

29+
2830
void *operator new(size_t size) {
2931
size = ((size + 3) & ~((size_t)0x3));
3032
return os_malloc(size);
@@ -47,27 +49,26 @@ extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
4749
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
4850

4951
void __cxa_pure_virtual(void) {
50-
abort();
52+
panic();
5153
}
5254

5355
void __cxa_deleted_virtual(void) {
54-
abort();
56+
panic();
5557
}
5658

5759
namespace std {
5860
void __throw_bad_function_call() {
59-
abort();
61+
panic();
6062
}
6163

6264
void __throw_length_error(char const*) {
63-
abort();
65+
panic();
6466
}
6567

6668
void __throw_bad_alloc() {
67-
abort();
69+
panic();
6870
}
6971
}
7072

7173
// TODO: rebuild windows toolchain to make this unnecessary:
7274
void* __dso_handle;
73-

cores/esp8266/core_esp8266_main.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
6666

6767
static uint32_t g_micros_at_task_start;
6868

69-
70-
extern "C" void abort() {
71-
do {
72-
*((int*)0) = 0;
73-
} while(true);
74-
}
75-
7669
extern "C" void esp_yield() {
7770
if (cont_can_yield(&g_cont)) {
7871
cont_yield(&g_cont);
@@ -89,7 +82,7 @@ extern "C" void __yield() {
8982
esp_yield();
9083
}
9184
else {
92-
abort();
85+
panic();
9386
}
9487
}
9588

@@ -117,9 +110,8 @@ static void loop_wrapper() {
117110
static void loop_task(os_event_t *events) {
118111
g_micros_at_task_start = system_get_time();
119112
cont_run(&g_cont, &loop_wrapper);
120-
if(cont_check(&g_cont) != 0) {
121-
ets_printf("\r\nsketch stack overflow detected\r\n");
122-
abort();
113+
if (cont_check(&g_cont) != 0) {
114+
panic();
123115
}
124116
}
125117

cores/esp8266/core_esp8266_postmortem.c

+35-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <stdint.h>
2323
#include <stddef.h>
2424
#include <stdbool.h>
25+
#include "debug.h"
2526
#include "ets_sys.h"
2627
#include "user_interface.h"
2728
#include "esp8266_peri.h"
@@ -30,6 +31,11 @@
3031
extern void __real_system_restart_local();
3132
extern cont_t g_cont;
3233

34+
static const char* s_panic_file = 0;
35+
static int s_panic_line = 0;
36+
static const char* s_panic_func = 0;
37+
38+
static bool s_abort_called = false;
3339

3440
void uart_write_char_d(char c);
3541
static void uart0_write_char_d(char c);
@@ -56,7 +62,13 @@ void __wrap_system_restart_local() {
5662

5763
ets_install_putc1(&uart_write_char_d);
5864

59-
if (rst_info.reason == REASON_EXCEPTION_RST) {
65+
if (s_panic_line) {
66+
ets_printf("\nPanic %s:%d %s\n", s_panic_file, s_panic_line, s_panic_func);
67+
}
68+
else if (s_abort_called) {
69+
ets_printf("Abort called\n");
70+
}
71+
else if (rst_info.reason == REASON_EXCEPTION_RST) {
6072
ets_printf("\nException (%d):\nepc1=0x%08x epc2=0x%08x epc3=0x%08x excvaddr=0x%08x depc=0x%08x\n",
6173
rst_info.exccause, rst_info.epc1, rst_info.epc2, rst_info.epc3, rst_info.excvaddr, rst_info.depc);
6274
}
@@ -158,3 +170,25 @@ static void uart1_write_char_d(char c) {
158170
}
159171
USF(1) = c;
160172
}
173+
void abort() __attribute__((noreturn));
174+
175+
void abort(){
176+
// cause exception
177+
s_abort_called = true;
178+
do {
179+
*((int*)0) = 0;
180+
} while(true);
181+
}
182+
183+
void __assert_func(const char *file, int line, const char *func, const char *what) {
184+
s_panic_file = file;
185+
s_panic_line = line;
186+
s_panic_func = func;
187+
}
188+
189+
void __panic_func(const char* file, int line, const char* func) {
190+
s_panic_file = file;
191+
s_panic_line = line;
192+
s_panic_func = func;
193+
abort();
194+
}

cores/esp8266/debug.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#define ARD_DEBUG_H
33

44
#include <stddef.h>
5-
//#define DEBUGV(...) ets_printf(__VA_ARGS__)
5+
#include <stdint.h>
6+
7+
#define DEBUGV(...) ets_printf(__VA_ARGS__)
68

79
#ifndef DEBUGV
810
#define DEBUGV(...)
@@ -14,4 +16,16 @@ void hexdump(uint8_t *mem, uint32_t len, uint8_t cols = 16);
1416
void hexdump(uint8_t *mem, uint32_t len, uint8_t cols);
1517
#endif
1618

19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
void __panic_func(const char* file, int line, const char* func) __attribute__((noreturn));
24+
#define panic() __panic_func(__FILE__, __LINE__, __func__)
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif
29+
30+
1731
#endif//ARD_DEBUG_H

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ int WiFiClientSecure::available() {
273273
uint8_t WiFiClientSecure::connected() {
274274
if (!_client)
275275
return 0;
276-
276+
277277
if (_client->state() == ESTABLISHED)
278278
return 1;
279279

@@ -384,8 +384,7 @@ extern "C" void* ax_port_malloc(size_t size, const char* file, int line) {
384384

385385
if (result == nullptr) {
386386
DEBUG_TLS_MEM_PRINT("%s:%d malloc %d failed, left %d\r\n", file, line, size, ESP.getFreeHeap());
387-
388-
while(true){}
387+
panic();
389388
}
390389
if (size >= 1024)
391390
DEBUG_TLS_MEM_PRINT("%s:%d malloc %d, left %d\r\n", file, line, size, ESP.getFreeHeap());
@@ -402,7 +401,7 @@ extern "C" void* ax_port_realloc(void* ptr, size_t size, const char* file, int l
402401
void* result = realloc(ptr, size);
403402
if (result == nullptr) {
404403
DEBUG_TLS_MEM_PRINT("%s:%d realloc %d failed, left %d\r\n", file, line, size, ESP.getFreeHeap());
405-
while(true){}
404+
panic();
406405
}
407406
if (size >= 1024)
408407
DEBUG_TLS_MEM_PRINT("%s:%d realloc %d, left %d\r\n", file, line, size, ESP.getFreeHeap());

0 commit comments

Comments
 (0)