-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
heap_trace_standalone.c
349 lines (265 loc) · 9.29 KB
/
heap_trace_standalone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <sdkconfig.h>
#define HEAP_TRACE_SRCFILE /* don't warn on inclusion here */
#include "esp_heap_trace.h"
#undef HEAP_TRACE_SRCFILE
#include "esp_attr.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_memory_utils.h"
#define STACK_DEPTH CONFIG_HEAP_TRACING_STACK_DEPTH
#if CONFIG_HEAP_TRACING_STANDALONE
static portMUX_TYPE trace_mux = portMUX_INITIALIZER_UNLOCKED;
static bool tracing;
static heap_trace_mode_t mode;
typedef struct {
/* Buffer used for records, starting at offset 0 */
heap_trace_record_t *buffer;
/* capacity of the buffer */
size_t capacity;
/* Count of entries logged in the buffer.*/
size_t count;
/* During execution, we remember the maximum
value of 'count'. This can help you
choose the right size for your buffer capacity.*/
size_t high_water_mark;
/* Has the buffer overflowed and lost trace entries? */
bool has_overflowed;
} records_t;
// Forward Defines
static void remove_record(records_t *r, int index);
static void heap_trace_dump_base(bool internal_ram, bool psram);
/* The actual records. */
static records_t records;
/* Actual number of allocations logged */
static size_t total_allocations;
/* Actual number of frees logged */
static size_t total_frees;
esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records)
{
if (tracing) {
return ESP_ERR_INVALID_STATE;
}
records.buffer = record_buffer;
records.capacity = num_records;
memset(records.buffer, 0, num_records * sizeof(heap_trace_record_t));
return ESP_OK;
}
esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
{
if (records.buffer == NULL || records.capacity == 0) {
return ESP_ERR_INVALID_STATE;
}
portENTER_CRITICAL(&trace_mux);
tracing = false;
mode = mode_param;
records.count = 0;
records.has_overflowed = false;
total_allocations = 0;
total_frees = 0;
heap_trace_resume();
portEXIT_CRITICAL(&trace_mux);
return ESP_OK;
}
static esp_err_t set_tracing(bool enable)
{
if (tracing == enable) {
return ESP_ERR_INVALID_STATE;
}
tracing = enable;
return ESP_OK;
}
esp_err_t heap_trace_stop(void)
{
return set_tracing(false);
}
esp_err_t heap_trace_resume(void)
{
return set_tracing(true);
}
size_t heap_trace_get_count(void)
{
return records.count;
}
esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record)
{
if (record == NULL) {
return ESP_ERR_INVALID_STATE;
}
esp_err_t result = ESP_OK;
portENTER_CRITICAL(&trace_mux);
if (index >= records.count) {
result = ESP_ERR_INVALID_ARG; /* out of range for 'count' */
} else {
memcpy(record, &records.buffer[index], sizeof(heap_trace_record_t));
}
portEXIT_CRITICAL(&trace_mux);
return result;
}
esp_err_t heap_trace_summary(heap_trace_summary_t *summary)
{
if (summary == NULL) {
return ESP_ERR_INVALID_ARG;
}
portENTER_CRITICAL(&trace_mux);
summary->mode = mode;
summary->total_allocations = total_allocations;
summary->total_frees = total_frees;
summary->count = records.count;
summary->capacity = records.capacity;
summary->high_water_mark = records.high_water_mark;
summary->has_overflowed = records.has_overflowed;
portEXIT_CRITICAL(&trace_mux);
return ESP_OK;
}
void heap_trace_dump(void) {
heap_trace_dump_caps(MALLOC_CAP_INTERNAL | MALLOC_CAP_SPIRAM);
}
void heap_trace_dump_caps(const uint32_t caps) {
heap_trace_dump_base(caps & MALLOC_CAP_INTERNAL, caps & MALLOC_CAP_SPIRAM);
}
static void heap_trace_dump_base(bool internal_ram, bool psram)
{
size_t delta_size = 0;
size_t delta_allocs = 0;
size_t start_count = records.count;
printf("====== Heap Trace: %u records (%u capacity) ======\n",
records.count, records.capacity);
for (int i = 0; i < records.count; i++) {
heap_trace_record_t *rec = &records.buffer[i];
bool should_print = rec->address != NULL &&
((psram && internal_ram) ||
(internal_ram && esp_ptr_internal(rec->address)) ||
(psram && esp_ptr_external_ram(rec->address)));
if (should_print) {
const char* label = "";
if (esp_ptr_internal(rec->address)) {
label = ", Internal";
}
if (esp_ptr_external_ram(rec->address)) {
label = ", PSRAM";
}
printf("%6d bytes (@ %p%s) allocated CPU %d ccount 0x%08x caller ",
rec->size, rec->address, label, rec->ccount & 1, rec->ccount & ~3);
for (int j = 0; j < STACK_DEPTH && rec->alloced_by[j] != 0; j++) {
printf("%p%s", rec->alloced_by[j],
(j < STACK_DEPTH - 1) ? ":" : "");
}
if (mode != HEAP_TRACE_ALL || STACK_DEPTH == 0 || rec->freed_by[0] == NULL) {
delta_size += rec->size;
delta_allocs++;
printf("\n");
} else {
printf("\nfreed by ");
for (int j = 0; j < STACK_DEPTH; j++) {
printf("%p%s", rec->freed_by[j],
(j < STACK_DEPTH - 1) ? ":" : "\n");
}
}
}
}
printf("====== Heap Trace Summary ======\n");
if (mode == HEAP_TRACE_ALL) {
printf("Mode: Heap Trace All\n");
printf("%u bytes alive in trace (%u/%u allocations)\n",
delta_size, delta_allocs, heap_trace_get_count());
} else {
printf("Mode: Heap Trace Leaks\n");
printf("%u bytes 'leaked' in trace (%u allocations)\n", delta_size, delta_allocs);
}
printf("records: %u (%u capacity, %u high water mark)\n",
records.count, records.capacity, records.high_water_mark);
printf("total allocations: %u\n", total_allocations);
printf("total frees: %u\n", total_frees);
if (start_count != records.count) { // only a problem if trace isn't stopped before dumping
printf("(NB: New entries were traced while dumping, so trace dump may have duplicate entries.)\n");
}
if (records.has_overflowed) {
printf("(NB: Internal Buffer has overflowed, so trace data is incomplete.)\n");
}
printf("================================\n");
}
/* Add a new allocation to the heap trace records */
static IRAM_ATTR void record_allocation(const heap_trace_record_t *record)
{
if (!tracing || record->address == NULL) {
return;
}
portENTER_CRITICAL(&trace_mux);
if (tracing) {
if (records.count == records.capacity) {
records.has_overflowed = true;
/* Move the whole buffer back one slot.
This is a bit slow, compared to treating this buffer as a
ringbuffer and rotating a head pointer.
However, ringbuffer code gets tricky when we remove elements
in mid-buffer (for leak trace mode) while trying to keep
track of an item count that may overflow.
*/
memmove(&records.buffer[0], &records.buffer[1],
sizeof(heap_trace_record_t) * (records.capacity -1));
records.count--;
}
// Copy new record into place
memcpy(&records.buffer[records.count], record, sizeof(heap_trace_record_t));
records.count++;
// high water mark
if (records.count > records.high_water_mark) {
records.high_water_mark = records.count;
}
total_allocations++;
}
portEXIT_CRITICAL(&trace_mux);
}
/* record a free event in the heap trace log
For HEAP_TRACE_ALL, this means filling in the freed_by pointer.
For HEAP_TRACE_LEAKS, this means removing the record from the log.
*/
static IRAM_ATTR void record_free(void *p, void **callers)
{
if (!tracing || p == NULL) {
return;
}
portENTER_CRITICAL(&trace_mux);
if (tracing) {
total_frees++;
/* search backwards for the allocation record matching this free */
int i = -1;
for (i = records.count - 1; i >= 0; i--) {
if (records.buffer[i].address == p) {
break;
}
}
if (i >= 0) {
if (mode == HEAP_TRACE_ALL) {
// add 'freed_by' info to the record
memcpy(records.buffer[i].freed_by, callers, sizeof(void *) * STACK_DEPTH);
} else { // HEAP_TRACE_LEAKS
// Leak trace mode, once an allocation is freed
// we remove it from the list
remove_record(&records, i);
}
}
}
portEXIT_CRITICAL(&trace_mux);
}
/* remove the entry at 'index' from the ringbuffer of saved records */
static IRAM_ATTR void remove_record(records_t *r, int index)
{
if (index < r->count - 1) {
// Remove the buffer entry from the list
memmove(&r->buffer[index], &r->buffer[index+1],
sizeof(heap_trace_record_t) * (r->capacity - index - 1));
} else {
// For last element, just zero it out to avoid ambiguity
memset(&r->buffer[index], 0, sizeof(heap_trace_record_t));
}
r->count--;
}
#include "heap_trace.inc"
#endif // CONFIG_HEAP_TRACING_STANDALONE