Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include "esp_cpu.h"
#include "esp_log.h"
#include "esp_app_trace.h"
#include "esp_app_trace_port.h"
#include "esp_private/startup_internal.h"
#define ESP_APPTRACE_DEST_UART_NUM
#define ESP_APPTRACE_DEST_UART_NUM
#define ESP_APPTRACE_DEST_UART_NUM
#define ESP_APPTRACE_DEST_UART_NUM
#define ESP_APPTRACE_DEST_UART_NUM
#define ESP_APPTRACE_MAX_VPRINTF_ARGS
#define ESP_APPTRACE_HOST_BUF_SIZE
TAG
esp_apptrace_channel_t
s_trace_channels
s_inited
esp_apptrace_init()
esp_system_init_fn_esp_apptrace_init
esp_apptrace_down_buffer_config(uint8_t *, uint32_t)
esp_apptrace_down_buffer_get(esp_apptrace_dest_t, uint32_t *, uint32_t)
esp_apptrace_down_buffer_put(esp_apptrace_dest_t, uint8_t *, uint32_t)
esp_apptrace_read(esp_apptrace_dest_t, void *, uint32_t *, uint32_t)
esp_apptrace_buffer_get(esp_apptrace_dest_t, uint32_t, uint32_t)
esp_apptrace_buffer_put(esp_apptrace_dest_t, uint8_t *, uint32_t)
esp_apptrace_write(esp_apptrace_dest_t, const void *, uint32_t, uint32_t)
esp_apptrace_vprintf_to(esp_apptrace_dest_t, uint32_t, const char *, va_list)
esp_apptrace_vprintf(const char *, va_list)
esp_apptrace_flush_nolock(esp_apptrace_dest_t, uint32_t, uint32_t)
esp_apptrace_flush(esp_apptrace_dest_t, uint32_t)
esp_apptrace_host_is_connected(esp_apptrace_dest_t)
esp_apptrace_jtag_hw_get(void **)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/app_trace/app_trace.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 OR MIT *//* ... */ #include <string.h> #include "esp_cpu.h" #include "esp_log.h" #include "esp_app_trace.h" #include "esp_app_trace_port.h" #include "esp_private/startup_internal.h"6 includes #ifdef CONFIG_APPTRACE_DEST_UART0 #define ESP_APPTRACE_DEST_UART_NUM 0 #elif CONFIG_APPTRACE_DEST_UART1 #define ESP_APPTRACE_DEST_UART_NUM 1 #elif CONFIG_APPTRACE_DEST_UART2 #define ESP_APPTRACE_DEST_UART_NUM 2 #elif CONFIG_APPTRACE_DEST_USB_CDC #define ESP_APPTRACE_DEST_UART_NUM 10 #else #define ESP_APPTRACE_DEST_UART_NUM 0 #endif #define ESP_APPTRACE_MAX_VPRINTF_ARGS 256 #define ESP_APPTRACE_HOST_BUF_SIZE 256 const static char *TAG = "esp_apptrace"; /** tracing module internal data */ typedef struct { esp_apptrace_hw_t *hw; void *hw_data; }{ ... } esp_apptrace_channel_t; static esp_apptrace_channel_t s_trace_channels[ESP_APPTRACE_DEST_MAX]; static bool s_inited; esp_err_t esp_apptrace_init(void) { int res; esp_apptrace_hw_t *hw = NULL; void *hw_data = NULL; // 'esp_apptrace_init()' is called on every core, so ensure to do main initialization only once if (esp_cpu_get_core_id() == 0) { memset(&s_trace_channels, 0, sizeof(s_trace_channels)); hw = esp_apptrace_jtag_hw_get(&hw_data); ESP_APPTRACE_LOGD("HW interface %p", hw); if (hw != NULL) { s_trace_channels[ESP_APPTRACE_DEST_JTAG].hw = hw; s_trace_channels[ESP_APPTRACE_DEST_JTAG].hw_data = hw_data; }{...} hw = esp_apptrace_uart_hw_get(ESP_APPTRACE_DEST_UART_NUM, &hw_data); if (hw != NULL) { s_trace_channels[ESP_APPTRACE_DEST_UART].hw = hw; s_trace_channels[ESP_APPTRACE_DEST_UART].hw_data = hw_data; }{...} s_inited = true; }{...} // esp_apptrace_init() is called on every core, so initialize trace channel on every core for (int i = 0; i < sizeof(s_trace_channels) / sizeof(s_trace_channels[0]); i++) { esp_apptrace_channel_t *ch = &s_trace_channels[i]; if (ch->hw) { res = ch->hw->init(ch->hw_data); if (res != ESP_OK) { ESP_APPTRACE_LOGE("Failed to init trace channel HW interface (%d)!", res); return res; }{...} }{...} }{...} return ESP_OK; }{ ... } ESP_SYSTEM_INIT_FN(esp_apptrace_init, SECONDARY, ESP_SYSTEM_INIT_ALL_CORES, 115) { return esp_apptrace_init(); }{ ... } void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size) { esp_apptrace_channel_t *ch; if (!s_inited) { return; }{...} // currently down buffer is supported for JTAG interface only // TODO: one more argument should be added to this function to specify HW interface: JTAG, UART0 etc ch = &s_trace_channels[ESP_APPTRACE_DEST_JTAG]; if (ch->hw != NULL) { if (ch->hw->down_buffer_config != NULL) { ch->hw->down_buffer_config(ch->hw_data, buf, size); }{...} }{...} else { ESP_APPTRACE_LOGD("Trace destination for JTAG not supported!"); }{...} ch = &s_trace_channels[ESP_APPTRACE_DEST_UART]; if (ch->hw != NULL) { if (ch->hw->down_buffer_config != NULL) { ch->hw->down_buffer_config(ch->hw_data, buf, size); }{...} }{...} else { ESP_APPTRACE_LOGD("Trace destination for UART not supported!"); }{...} }{ ... } uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t user_tmo) { esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return NULL; }{...} if (size == NULL || *size == 0) { return NULL; }{...} if (!s_inited) { return NULL; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return NULL; }{...} if (ch->hw->get_down_buffer == NULL) { return NULL; }{...} esp_apptrace_tmo_init(&tmo, user_tmo); return ch->hw->get_down_buffer(ch->hw_data, size, &tmo); }{ ... } esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo) { esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return ESP_ERR_INVALID_ARG; }{...} if (ptr == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!s_inited) { return ESP_ERR_INVALID_STATE; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return ESP_ERR_NOT_SUPPORTED; }{...} if (ch->hw->get_down_buffer == NULL) { return ESP_ERR_NOT_SUPPORTED; }{...} esp_apptrace_tmo_init(&tmo, user_tmo); return ch->hw->put_down_buffer(ch->hw_data, ptr, &tmo); }{ ... } esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *buf, uint32_t *size, uint32_t user_tmo) { int res = ESP_OK; esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return ESP_ERR_INVALID_ARG; }{...} if (buf == NULL || size == NULL || *size == 0) { return ESP_ERR_INVALID_ARG; }{...} if (!s_inited) { return ESP_ERR_INVALID_STATE; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return ESP_ERR_NOT_SUPPORTED; }{...} if (ch->hw->get_down_buffer == NULL || ch->hw->put_down_buffer == NULL) { return ESP_ERR_NOT_SUPPORTED; }{...} //TODO: callback system esp_apptrace_tmo_init(&tmo, user_tmo); uint32_t act_sz = *size; *size = 0; uint8_t *ptr = ch->hw->get_down_buffer(ch->hw_data, &act_sz, &tmo); if (ptr && act_sz > 0) { ESP_APPTRACE_LOGD("Read %" PRIu32 " bytes from host", act_sz); memcpy(buf, ptr, act_sz); res = ch->hw->put_down_buffer(ch->hw_data, ptr, &tmo); *size = act_sz; }{...} else { res = ESP_ERR_TIMEOUT; }{...} return res; }{ ... } uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t user_tmo) { esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return NULL; }{...} if (size == 0) { return NULL; }{...} if (!s_inited) { return NULL; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return NULL; }{...} if (ch->hw->get_up_buffer == NULL) { return NULL; }{...} esp_apptrace_tmo_init(&tmo, user_tmo); return ch->hw->get_up_buffer(ch->hw_data, size, &tmo); }{ ... } esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo) { esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return ESP_ERR_INVALID_ARG; }{...} if (ptr == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!s_inited) { return ESP_ERR_INVALID_STATE; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return ESP_ERR_NOT_SUPPORTED; }{...} if (ch->hw->put_up_buffer == NULL) { return ESP_ERR_NOT_SUPPORTED; }{...} esp_apptrace_tmo_init(&tmo, user_tmo); return ch->hw->put_up_buffer(ch->hw_data, ptr, &tmo); }{ ... } esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t user_tmo) { uint8_t *ptr = NULL; esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return ESP_ERR_INVALID_ARG; }{...} if (data == NULL || size == 0) { return ESP_ERR_INVALID_ARG; }{...} if (!s_inited) { return ESP_ERR_INVALID_STATE; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return ESP_ERR_NOT_SUPPORTED; }{...} if (ch->hw->get_up_buffer == NULL || ch->hw->put_up_buffer == NULL) { return ESP_ERR_NOT_SUPPORTED; }{...} esp_apptrace_tmo_init(&tmo, user_tmo); ptr = ch->hw->get_up_buffer(ch->hw_data, size, &tmo); if (ptr == NULL) { return ESP_ERR_NO_MEM; }{...} // actually can be suspended here by higher prio tasks/ISRs //TODO: use own memcpy with dead trace calls kick-off algo and tmo expiration check memcpy(ptr, data, size); // now indicate that this buffer is ready to be sent off to host return ch->hw->put_up_buffer(ch->hw_data, ptr, &tmo); }{ ... } int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t user_tmo, const char *fmt, va_list ap) { uint16_t nargs = 0; uint8_t *pout, *p = (uint8_t *)fmt; esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return -1; }{...} if (fmt == NULL) { return -1; }{...} if (!s_inited) { return -1; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return -1; }{...} if (ch->hw->get_up_buffer == NULL || ch->hw->put_up_buffer == NULL) { return -1; }{...} esp_apptrace_tmo_init(&tmo, user_tmo); ESP_APPTRACE_LOGD("fmt %p", fmt); while ((p = (uint8_t *)strchr((char *)p, '%')) && nargs < ESP_APPTRACE_MAX_VPRINTF_ARGS) { p++; if (*p != '%' && *p != 0) { nargs++; }{...} }{...} ESP_APPTRACE_LOGD("nargs = %d", nargs); if (p) { ESP_APPTRACE_LOGE("Failed to store all printf args!"); }{...} pout = ch->hw->get_up_buffer(ch->hw_data, 1 + sizeof(char *) + nargs * sizeof(uint32_t), &tmo); if (pout == NULL) { ESP_APPTRACE_LOGE("Failed to get buffer!"); return -1; }{...} p = pout; *pout = nargs; pout++; *(const char **)pout = fmt; pout += sizeof(char *); while (nargs-- > 0) { uint32_t arg = va_arg(ap, uint32_t); *(uint32_t *)pout = arg; pout += sizeof(uint32_t); ESP_APPTRACE_LOGD("arg %" PRIx32, arg); }{...} int ret = ch->hw->put_up_buffer(ch->hw_data, p, &tmo); if (ret != ESP_OK) { ESP_APPTRACE_LOGE("Failed to put printf buf (%d)!", ret); return -1; }{...} return (pout - p); }{ ... } int esp_apptrace_vprintf(const char *fmt, va_list ap) { return esp_apptrace_vprintf_to(ESP_APPTRACE_DEST_JTAG, 0, fmt, ap); }{ ... } esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t usr_tmo) { esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return ESP_ERR_INVALID_ARG; }{...} if (!s_inited) { return ESP_ERR_INVALID_STATE; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return ESP_ERR_NOT_SUPPORTED; }{...} if (ch->hw->flush_up_buffer_nolock == NULL) { return ESP_ERR_NOT_SUPPORTED; }{...} esp_apptrace_tmo_init(&tmo, usr_tmo); return ch->hw->flush_up_buffer_nolock(ch->hw_data, min_sz, &tmo); }{ ... } esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t usr_tmo) { esp_apptrace_tmo_t tmo; esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return ESP_ERR_INVALID_ARG; }{...} if (!s_inited) { return ESP_ERR_INVALID_STATE; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return ESP_ERR_NOT_SUPPORTED; }{...} if (ch->hw->flush_up_buffer == NULL) { return ESP_ERR_NOT_SUPPORTED; }{...} esp_apptrace_tmo_init(&tmo, usr_tmo); return ch->hw->flush_up_buffer(ch->hw_data, &tmo); }{ ... } bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest) { esp_apptrace_channel_t *ch; ESP_APPTRACE_LOGV("%s(): enter", __func__); if (dest >= ESP_APPTRACE_DEST_MAX) { return false; }{...} if (!s_inited) { return false; }{...} ch = &s_trace_channels[dest]; if (ch->hw == NULL) { ESP_APPTRACE_LOGE("Trace destination %d not supported!", dest); return false; }{...} if (ch->hw->host_is_connected == NULL) { return false; }{...} return ch->hw->host_is_connected(ch->hw_data); }{ ... } #if !CONFIG_APPTRACE_DEST_JTAG esp_apptrace_hw_t *esp_apptrace_jtag_hw_get(void **data) { return NULL; }{ ... } /* ... */#endif
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.