Skip to content

Commit be66ae7

Browse files
committed
OOM build option now saves file and line number for post mortem reporting.
1 parent 3669bf5 commit be66ae7

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

cores/esp8266/core_esp8266_postmortem.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ static int s_user_reset_reason = REASON_DEFAULT_RST;
6262
// From UMM, the last caller of a malloc/realloc/calloc which failed:
6363
extern void *umm_last_fail_alloc_addr;
6464
extern int umm_last_fail_alloc_size;
65+
#if defined(DEBUG_ESP_OOM)
66+
extern const char *umm_last_fail_alloc_file;
67+
extern int umm_last_fail_alloc_line;
68+
#endif
6569

6670
static void raise_exception() __attribute__((noreturn));
6771

@@ -181,7 +185,13 @@ void __wrap_system_restart_local() {
181185

182186
// Use cap-X formatting to ensure the standard EspExceptionDecoder doesn't match the address
183187
if (umm_last_fail_alloc_addr) {
184-
ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)\n"), (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size);
188+
#if defined(DEBUG_ESP_OOM)
189+
ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)@%S:%d\n"),
190+
(uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size,
191+
umm_last_fail_alloc_file, umm_last_fail_alloc_line);
192+
#else
193+
ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)\n"), (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size);
194+
#endif
185195
}
186196

187197
custom_crash_callback( &rst_info, sp_dump + offset, stack_end );

cores/esp8266/heap.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ extern "C" {
5858
// Debugging helper, last allocation which returned NULL
5959
void *umm_last_fail_alloc_addr = NULL;
6060
int umm_last_fail_alloc_size = 0;
61-
61+
#if defined(DEBUG_ESP_OOM)
62+
const char *umm_last_fail_alloc_file = NULL;
63+
int umm_last_fail_alloc_line = 0;
64+
#endif
6265

6366
#ifdef UMM_INTEGRITY_CHECK
6467
#define INTEGRITY_CHECK__ABORT() \
@@ -79,18 +82,31 @@ int umm_last_fail_alloc_size = 0;
7982

8083
#endif // UMM_INTEGRITY_CHECK
8184

82-
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a) \
85+
#if defined(DEBUG_ESP_OOM)
86+
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a, f, l) \
87+
if(0 != (s) && 0 == p)\
88+
{\
89+
umm_last_fail_alloc_addr = a;\
90+
umm_last_fail_alloc_size = s;\
91+
umm_last_fail_alloc_file = f;\
92+
umm_last_fail_alloc_line = l;\
93+
}
94+
#else
95+
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a, f, l) \
96+
(void)f;\
97+
(void)l;\
8398
if(0 != (s) && 0 == p)\
8499
{\
85100
umm_last_fail_alloc_addr = a;\
86101
umm_last_fail_alloc_size = s;\
87102
}
103+
#endif
88104

89105
void* _malloc_r(struct _reent* unused, size_t size)
90106
{
91107
(void) unused;
92108
void *ret = malloc(size);
93-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0))
109+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), NULL, 0);
94110
return ret;
95111
}
96112

@@ -104,15 +120,15 @@ void* _realloc_r(struct _reent* unused, void* ptr, size_t size)
104120
{
105121
(void) unused;
106122
void *ret = realloc(ptr, size);
107-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0))
123+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), NULL, 0);
108124
return ret;
109125
}
110126

111127
void* _calloc_r(struct _reent* unused, size_t count, size_t size)
112128
{
113129
(void) unused;
114130
void *ret = calloc(count, size);
115-
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0))
131+
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0), NULL, 0);
116132
return ret;
117133
}
118134

@@ -132,7 +148,7 @@ void ICACHE_RAM_ATTR print_loc(size_t size, const char* file, int line)
132148

133149
bool inISR = ETS_INTR_WITHINISR();
134150
if (inISR && (uint32_t)file >= 0x40200000) {
135-
DEBUG_HEAP_PRINTF("%p", file);
151+
DEBUG_HEAP_PRINTF("File: %p", file);
136152
} else if (!inISR && (uint32_t)file >= 0x40200000) {
137153
char buf[ets_strlen(file)] __attribute__ ((aligned(4)));
138154
ets_strcpy(buf, file);
@@ -176,7 +192,7 @@ void* ICACHE_RAM_ATTR malloc(size_t size)
176192
INTEGRITY_CHECK__ABORT();
177193
POISON_CHECK__ABORT();
178194
void* ret = __umm_malloc(size);
179-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
195+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), NULL, 0);
180196
OOM_CHECK__PRINT_OOM(ret, size);
181197
return ret;
182198
}
@@ -186,7 +202,7 @@ void* ICACHE_RAM_ATTR calloc(size_t count, size_t size)
186202
INTEGRITY_CHECK__ABORT();
187203
POISON_CHECK__ABORT();
188204
void* ret = __umm_calloc(count, size);
189-
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0));
205+
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0), NULL, 0);
190206
OOM_CHECK__PRINT_OOM(ret, size);
191207
return ret;
192208
}
@@ -196,7 +212,7 @@ void* ICACHE_RAM_ATTR realloc(void* ptr, size_t size)
196212
INTEGRITY_CHECK__ABORT();
197213
void* ret = __umm_realloc_fl(ptr, size, NULL, 0);
198214
POISON_CHECK__ABORT();
199-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
215+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), NULL, 0);
200216
OOM_CHECK__PRINT_OOM(ret, size);
201217
return ret;
202218
}
@@ -215,7 +231,7 @@ void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
215231
INTEGRITY_CHECK__PANIC_FL(file, line);
216232
POISON_CHECK__PANIC_FL(file, line);
217233
void* ret = __umm_malloc(size);
218-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
234+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), file, line);
219235
OOM_CHECK__PRINT_LOC(ret, size, file, line);
220236
return ret;
221237
}
@@ -225,7 +241,7 @@ void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file,
225241
INTEGRITY_CHECK__PANIC_FL(file, line);
226242
POISON_CHECK__PANIC_FL(file, line);
227243
void* ret = __umm_calloc(count, size);
228-
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0));
244+
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0), file, line);
229245
OOM_CHECK__PRINT_LOC(ret, size, file, line);
230246
return ret;
231247
}
@@ -235,7 +251,7 @@ void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, in
235251
INTEGRITY_CHECK__PANIC_FL(file, line);
236252
void* ret = __umm_realloc_fl(ptr, size, file, line);
237253
POISON_CHECK__PANIC_FL(file, line);
238-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
254+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), file, line);
239255
OOM_CHECK__PRINT_LOC(ret, size, file, line);
240256
return ret;
241257
}
@@ -245,7 +261,7 @@ void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
245261
INTEGRITY_CHECK__PANIC_FL(file, line);
246262
POISON_CHECK__PANIC_FL(file, line);
247263
void* ret = __umm_calloc(1, size);
248-
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
264+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0), file, line);
249265
OOM_CHECK__PRINT_LOC(ret, size, file, line);
250266
return ret;
251267
}

0 commit comments

Comments
 (0)