1 // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Hot It Works
16 // ************
17 
18 // 1. Components Overview
19 // ======================
20 
21 // Xtensa has useful feature: TRAX debug module. It allows recording program execution flow at run-time without disturbing CPU.
22 // Exectution flow data are written to configurable Trace RAM block. Besides accessing Trace RAM itself TRAX module also allows to read/write
23 // trace memory via its registers by means of JTAG, APB or ERI transactions.
24 // ESP32 has two Xtensa cores with separate TRAX modules on them and provides two special memory regions to be used as trace memory.
25 // Chip allows muxing access to those trace memory blocks in such a way that while one block is accessed by CPUs another one can be accessed by host
26 // by means of reading/writing TRAX registers via JTAG. Blocks muxing is configurable at run-time and allows switching trace memory blocks between
27 // accessors in round-robin fashion so they can read/write separate memory blocks without disturbing each other.
28 // This module implements application tracing feature based on above mechanisms. It allows to transfer arbitrary user data to/from
29 // host via JTAG with minimal impact on system performance. This module is implied to be used in the following tracing scheme.
30 
31 //                                                        ------>------                                         ----- (host components) -----
32 //                                                        |           |                                         |                           |
33 // -------------------   -----------------------     -----------------------     ----------------    ------     ---------   -----------------
34 // |trace data source|-->|target tracing module|<--->|TRAX_MEM0 | TRAX_MEM1|---->|TRAX_DATA_REGS|<-->|JTAG|<--->|OpenOCD|-->|trace data sink|
35 // -------------------   -----------------------     -----------------------     ----------------    ------     ---------   -----------------
36 //                                 |                      |           |                                |
37 //                                 |                      ------<------          ----------------      |
38 //                                 |<------------------------------------------->|TRAX_CTRL_REGS|<---->|
39 //                                                                               ----------------
40 
41 // In general tracing goes in the following way. User application requests tracing module to send some data by calling esp_apptrace_buffer_get(),
42 // module allocates necessary buffer in current input trace block. Then user fills received buffer with data and calls esp_apptrace_buffer_put().
43 // When current input trace block is filled with app data it is exposed to host and the second block becomes input one and buffer filling restarts.
44 // While target application fills one TRAX block host reads another one via JTAG.
45 // This module also allows communication in the opposite direction: from host to target. As it was said ESP32 and host can access different TRAX blocks
46 // simultaneously, so while target writes trace data to one block host can write its own data (e.g. tracing commands) to another one then when
47 // blocks are switched host receives trace data and target receives data written by host application. Target user application can read host data
48 // by calling esp_apptrace_read() API.
49 // To control buffer switching and for other communication purposes this implementation uses some TRAX registers. It is safe since HW TRAX tracing
50 // can not be used along with application tracing feature so these registers are freely readable/writeable via JTAG from host and via ERI from ESP32 cores.
51 // Overhead of this implementation on target CPU is produced only by allocating/managing buffers and copying of data.
52 // On the host side special OpenOCD command must be used to read trace data.
53 
54 // 2. TRAX Registers layout
55 // ========================
56 
57 // This module uses two TRAX HW registers to communicate with host SW (OpenOCD).
58 //  - Control register uses TRAX_DELAYCNT as storage. Only lower 24 bits of TRAX_DELAYCNT are writable. Control register has the following bitfields:
59 //   | 31..XXXXXX..24 | 23 .(host_connect). 23| 22..(block_id)..15 | 14..(block_len)..0 |
60 //    14..0  bits - actual length of user data in trace memory block. Target updates it every time it fills memory block and exposes it to host.
61 //                  Host writes zero to this field when it finishes reading exposed block;
62 //    21..15 bits - trace memory block transfer ID. Block counter. It can overflow. Updated by target, host should not modify it. Actually can be 2 bits;
63 //    22     bit  - 'host data present' flag. If set to one there is data from host, otherwise - no host data;
64 //    23     bit  - 'host connected' flag. If zero then host is not connected and tracing module works in post-mortem mode, otherwise in streaming mode;
65 // - Status register uses TRAX_TRIGGERPC as storage. If this register is not zero then current CPU is changing TRAX registers and
66 //   this register holds address of the instruction which application will execute when it finishes with those registers modifications.
67 //   See 'Targets Connection' setion for details.
68 
69 // 3. Modes of operation
70 // =====================
71 
72 // This module supports two modes of operation:
73 //  - Post-mortem mode. This is the default mode. In this mode application tracing module does not check whether host has read all the data from block
74 //    exposed to it and switches block in any case. The mode does not need host interaction for operation and so can be useful when only the latest
75 //    trace data are necessary, e.g. for analyzing crashes. On panic the latest data from current input block are exposed to host and host can read them.
76 //    It can happen that system panic occurs when there are very small amount of data which are not exposed to host yet (e.g. crash just after the
77 //    TRAX block switch). In this case the previous 16KB of collected data will be dropped and host will see the latest, but very small piece of trace.
78 //    It can be insufficient to diagnose the problem. To avoid such situations there is menuconfig option
79 //    CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH
80 //    which controls the threshold for flushing data in case of panic.
81 //  - Streaming mode. Tracing module enters this mode when host connects to target and sets respective bits in control registers (per core).
82 //    In this mode before switching the block tracing module waits for the host to read all the data from the previously exposed block.
83 //    On panic tracing module also waits (timeout is configured via menuconfig via CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO) for the host to read all data.
84 
85 // 4. Communication Protocol
86 // =========================
87 
88 // 4.1 Trace Memory Blocks
89 // -----------------------
90 
91 // Communication is controlled via special register. Host periodically polls control register on each core to find out if there are any data available.
92 // When current input memory block is filled it is exposed to host and 'block_len' and 'block_id' fields are updated in the control register.
93 // Host reads new register value and according to it's value starts reading data from exposed block. Meanwhile target starts filling another trace block.
94 // When host finishes reading the block it clears 'block_len' field in control register indicating to the target that it is ready to accept the next one.
95 // If the host has some data to transfer to the target it writes them to trace memory block before clearing 'block_len' field. Then it sets
96 // 'host_data_present' bit and clears 'block_len' field in control register. Upon every block switch target checks 'host_data_present' bit and if it is set
97 // reads them to down buffer before writing any trace data to switched TRAX block.
98 
99 // 4.2 User Data Chunks Level
100 // --------------------------
101 
102 // Since trace memory block is shared between user data chunks and data copying is performed on behalf of the API user (in its normal context) in
103 // multithreading environment it can happen that task/ISR which copies data is preempted by another high prio task/ISR. So it is possible situation
104 // that task/ISR will fail to complete filling its data chunk before the whole trace block is exposed to the host. To handle such conditions tracing
105 // module prepends all user data chunks with header which contains allocated buffer size and actual data length within it. OpenOCD command
106 // which reads application traces reports error when it reads incomplete user data block.
107 // Data which are transffered from host to target are also prepended with a header. Down channel data header is simple and consists of one two bytes field
108 // containing length of host data following the header.
109 
110 // 4.3 Data Buffering
111 // ------------------
112 
113 // It takes some time for the host to read TRAX memory block via JTAG. In streaming mode it can happen that target has filled its TRAX block, but host
114 // has not completed reading of the previous one yet. So in this case time critical tracing calls (which can not be delayed for too long time due to
115 // the lack of free memory in TRAX block) can be dropped. To avoid such scenarios tracing module implements data buffering. Buffered data will be sent
116 // to the host later when TRAX block switch occurs. The maximum size of the buffered data is controlled by menuconfig option
117 // CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX.
118 
119 // 4.4 Target Connection/Disconnection
120 // -----------------------------------
121 
122 // When host is going to start tracing in streaming mode it needs to put both ESP32 cores into initial state when 'host connected' bit is set
123 // on both cores. To accomplish this host halts both cores and sets this bit in TRAX registers. But target code can be halted in state when it has read control
124 // register but has not updated its value. To handle such situations target code indicates to the host that it is updating control register by writing
125 // non-zero value to status register. Actually it writes address of the instruction which it will execute when it finishes with
126 // the registers update. When target is halted during control register update host sets breakpoint at the address from status register and resumes CPU.
127 // After target code finishes with register update it is halted on breakpoint, host detects it and safely sets 'host connected' bit. When both cores
128 // are set up they are resumed. Tracing starts without further intrusion into CPUs work.
129 // When host is going to stop tracing in streaming mode it needs to disconnect targets. Disconnection process is done using the same algorithm
130 // as for connecting, but 'host connected' bits are cleared on ESP32 cores.
131 
132 // 5. Module Access Synchronization
133 // ================================
134 
135 // Access to internal module's data is synchronized with custom mutex. Mutex is a wrapper for portMUX_TYPE and uses almost the same sync mechanism as in
136 // vPortCPUAcquireMutex/vPortCPUReleaseMutex. The mechanism uses S32C1I Xtensa instruction to implement exclusive access to module's data from tasks and
137 // ISRs running on both cores. Also custom mutex allows specifying timeout for locking operation. Locking routine checks underlaying mutex in cycle until
138 // it gets its ownership or timeout expires. The differences of application tracing module's mutex implementation from vPortCPUAcquireMutex/vPortCPUReleaseMutex are:
139 // - Support for timeouts.
140 // - Local IRQs for CPU which owns the mutex are disabled till the call to unlocking routine. This is made to avoid possible task's prio inversion.
141 //   When low prio task takes mutex and enables local IRQs gets preempted by high prio task which in its turn can try to acquire mutex using infinite timeout.
142 //   So no local task switch occurs when mutex is locked. But this does not apply to tasks on another CPU.
143 //   WARNING: Priority inversion can happen when low prio task works on one CPU and medium and high prio tasks work on another.
144 // WARNING: Care must be taken when selecting timeout values for trace calls from ISRs. Tracing module does not care about watchdogs when waiting
145 // on internal locks and for host to complete previous block reading, so if timeout value exceeds watchdog's one it can lead to the system reboot.
146 
147 // 6. Timeouts
148 // ===========
149 
150 // Timeout mechanism is based on xthal_get_ccount() routine and supports timeout values in microseconds.
151 // There are two situations when task/ISR can be delayed by tracing API call. Timeout mechanism takes into account both conditions:
152 // - Trace data are locked by another task/ISR. When wating on trace data lock.
153 // - Current TRAX memory input block is full when working in streaming mode (host is connected). When waiting for host to complete previous block reading.
154 // When wating for any of above conditions xthal_get_ccount() is called periodically to calculate time elapsed from trace API routine entry. When elapsed
155 // time exceeds specified timeout value operation is canceled and ESP_ERR_TIMEOUT code is returned.
156 
157 #include <string.h>
158 #include <sys/param.h>
159 #include "sdkconfig.h"
160 #include "soc/soc.h"
161 #include "soc/dport_access.h"
162 #if CONFIG_IDF_TARGET_ESP32
163 #include "soc/dport_reg.h"
164 #elif CONFIG_IDF_TARGET_ESP32S2
165 #include "soc/sensitive_reg.h"
166 #endif
167 #if __XTENSA__
168 #include "eri.h"
169 #include "trax.h"
170 #endif
171 #include "soc/timer_periph.h"
172 #include "freertos/FreeRTOS.h"
173 #include "esp_app_trace.h"
174 #include "esp_rom_sys.h"
175 
176 #if CONFIG_APPTRACE_ENABLE
177 #define ESP_APPTRACE_MAX_VPRINTF_ARGS           256
178 #define ESP_APPTRACE_HOST_BUF_SIZE              256
179 
180 #define ESP_APPTRACE_PRINT_LOCK                 0
181 
182 #include "esp_log.h"
183 const static char *TAG = "esp_apptrace";
184 
185 #if ESP_APPTRACE_PRINT_LOCK
186 #define ESP_APPTRACE_LOG( format, ... )   \
187     do { \
188         esp_apptrace_log_lock(); \
189         esp_rom_printf(format, ##__VA_ARGS__); \
190         esp_apptrace_log_unlock(); \
191     } while(0)
192 #else
193 #define ESP_APPTRACE_LOG( format, ... )   \
194     do { \
195         esp_rom_printf(format, ##__VA_ARGS__); \
196     } while(0)
197 #endif
198 
199 #define ESP_APPTRACE_LOG_LEV( _L_, level, format, ... )   \
200     do { \
201         if (LOG_LOCAL_LEVEL >= level) { \
202             ESP_APPTRACE_LOG(LOG_FORMAT(_L_, format), esp_log_early_timestamp(), TAG, ##__VA_ARGS__); \
203         } \
204     } while(0)
205 
206 #define ESP_APPTRACE_LOGE( format, ... )  ESP_APPTRACE_LOG_LEV(E, ESP_LOG_ERROR, format, ##__VA_ARGS__)
207 #define ESP_APPTRACE_LOGW( format, ... )  ESP_APPTRACE_LOG_LEV(W, ESP_LOG_WARN, format, ##__VA_ARGS__)
208 #define ESP_APPTRACE_LOGI( format, ... )  ESP_APPTRACE_LOG_LEV(I, ESP_LOG_INFO, format, ##__VA_ARGS__)
209 #define ESP_APPTRACE_LOGD( format, ... )  ESP_APPTRACE_LOG_LEV(D, ESP_LOG_DEBUG, format, ##__VA_ARGS__)
210 #define ESP_APPTRACE_LOGV( format, ... )  ESP_APPTRACE_LOG_LEV(V, ESP_LOG_VERBOSE, format, ##__VA_ARGS__)
211 #define ESP_APPTRACE_LOGO( format, ... )  ESP_APPTRACE_LOG_LEV(E, ESP_LOG_NONE, format, ##__VA_ARGS__)
212 
213 // TODO: move these (and same definitions in trax.c to dport_reg.h)
214 #if CONFIG_IDF_TARGET_ESP32
215 #define TRACEMEM_MUX_PROBLK0_APPBLK1            0
216 #define TRACEMEM_MUX_BLK0_ONLY                  1
217 #define TRACEMEM_MUX_BLK1_ONLY                  2
218 #define TRACEMEM_MUX_PROBLK1_APPBLK0            3
219 #elif CONFIG_IDF_TARGET_ESP32S2
220 #define TRACEMEM_MUX_BLK0_NUM                   19
221 #define TRACEMEM_MUX_BLK1_NUM                   20
222 #define TRACEMEM_BLK_NUM2ADDR(_n_)              (0x3FFB8000UL + 0x4000UL*((_n_)-4))
223 #endif
224 
225 // TRAX is disabled, so we use its registers for our own purposes
226 // | 31..XXXXXX..24 | 23 .(host_connect). 23 | 22 .(host_data). 22| 21..(block_id)..15 | 14..(block_len)..0 |
227 #define ESP_APPTRACE_TRAX_CTRL_REG              ERI_TRAX_DELAYCNT
228 #define ESP_APPTRACE_TRAX_STAT_REG              ERI_TRAX_TRIGGERPC
229 
230 #define ESP_APPTRACE_TRAX_BLOCK_LEN_MSK         0x7FFFUL
231 #define ESP_APPTRACE_TRAX_BLOCK_LEN(_l_)        ((_l_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
232 #define ESP_APPTRACE_TRAX_BLOCK_LEN_GET(_v_)    ((_v_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
233 #define ESP_APPTRACE_TRAX_BLOCK_ID_MSK          0x7FUL
234 #define ESP_APPTRACE_TRAX_BLOCK_ID(_id_)        (((_id_) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK) << 15)
235 #define ESP_APPTRACE_TRAX_BLOCK_ID_GET(_v_)     (((_v_) >> 15) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)
236 #define ESP_APPTRACE_TRAX_HOST_DATA             (1 << 22)
237 #define ESP_APPTRACE_TRAX_HOST_CONNECT          (1 << 23)
238 
239 #if CONFIG_SYSVIEW_ENABLE
240 #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_)      (0)
241 #define ESP_APPTRACE_USR_BLOCK_LEN(_v_)         (_v_)
242 #else
243 #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_)      ((_cid_) << 15)
244 #define ESP_APPTRACE_USR_BLOCK_LEN(_v_)         (~(1 << 15) & (_v_))
245 #endif
246 #define ESP_APPTRACE_USR_BLOCK_RAW_SZ(_s_)     ((_s_) + sizeof(esp_tracedata_hdr_t))
247 
248 #if CONFIG_IDF_TARGET_ESP32
249 static volatile uint8_t *s_trax_blocks[] = {
250     (volatile uint8_t *) 0x3FFFC000,
251     (volatile uint8_t *) 0x3FFF8000
252 };
253 #elif CONFIG_IDF_TARGET_ESP32S2
254 static volatile uint8_t *s_trax_blocks[] = {
255     (volatile uint8_t *)TRACEMEM_BLK_NUM2ADDR(TRACEMEM_MUX_BLK0_NUM),
256     (volatile uint8_t *)TRACEMEM_BLK_NUM2ADDR(TRACEMEM_MUX_BLK1_NUM)
257 };
258 #endif
259 
260 #define ESP_APPTRACE_TRAX_BLOCKS_NUM            (sizeof(s_trax_blocks)/sizeof(s_trax_blocks[0]))
261 
262 #define ESP_APPTRACE_TRAX_INBLOCK_START         0
263 
264 #define ESP_APPTRACE_TRAX_INBLOCK_MARKER()          (s_trace_buf.trax.state.markers[s_trace_buf.trax.state.in_block % 2])
265 #define ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(_v_)   do {s_trace_buf.trax.state.markers[s_trace_buf.trax.state.in_block % 2] += (_v_);}while(0)
266 #define ESP_APPTRACE_TRAX_INBLOCK_GET()             (&s_trace_buf.trax.blocks[s_trace_buf.trax.state.in_block % 2])
267 
268 #define ESP_APPTRACE_TRAX_BLOCK_SIZE            (0x4000UL)
269 #if CONFIG_SYSVIEW_ENABLE
270 #define ESP_APPTRACE_USR_DATA_LEN_MAX           255UL
271 #else
272 #define ESP_APPTRACE_USR_DATA_LEN_MAX           (ESP_APPTRACE_TRAX_BLOCK_SIZE - sizeof(esp_tracedata_hdr_t))
273 #endif
274 
275 #define ESP_APPTRACE_HW_TRAX                    0
276 #define ESP_APPTRACE_HW_MAX                     1
277 #define ESP_APPTRACE_HW(_i_)                    (&s_trace_hw[_i_])
278 
279 /** Trace data header. Every user data chunk is prepended with this header.
280  * User allocates block with esp_apptrace_buffer_get and then fills it with data,
281  * in multithreading environment it can happen that tasks gets buffer and then gets interrupted,
282  * so it is possible that user data are incomplete when TRAX memory block is exposed to the host.
283  * In this case host SW will see that wr_sz < block_sz and will report error.
284  */
285 typedef struct {
286 #if CONFIG_SYSVIEW_ENABLE
287     uint8_t   block_sz; // size of allocated block for user data
288     uint8_t   wr_sz;    // size of actually written data
289 #else
290     uint16_t   block_sz; // size of allocated block for user data
291     uint16_t   wr_sz;    // size of actually written data
292 #endif
293 } esp_tracedata_hdr_t;
294 
295 /** TODO: docs
296  */
297 typedef struct {
298     uint16_t   block_sz; // size of allocated block for user data
299 } esp_hostdata_hdr_t;
300 
301 /** TRAX HW transport state */
302 typedef struct {
303     uint32_t                   in_block;                                // input block ID
304     // TODO: change to uint16_t
305     uint32_t                   markers[ESP_APPTRACE_TRAX_BLOCKS_NUM];   // block filling level markers
306 } esp_apptrace_trax_state_t;
307 
308 /** memory block parameters */
309 typedef struct {
310     uint8_t   *start;   // start address
311     uint16_t   sz;      // size
312 } esp_apptrace_mem_block_t;
313 
314 /** TRAX HW transport data */
315 typedef struct {
316     volatile esp_apptrace_trax_state_t  state;                                  // state
317     esp_apptrace_mem_block_t            blocks[ESP_APPTRACE_TRAX_BLOCKS_NUM];   // memory blocks
318 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
319     // ring buffer control struct for pending user blocks
320     esp_apptrace_rb_t                   rb_pend;
321     // storage for pending user blocks
322     uint8_t                             pending_data[CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX + 1];
323 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
324     // ring buffer control struct for pending user data chunks sizes,
325     // every chunk contains whole number of user blocks and fit into TRAX memory block
326     esp_apptrace_rb_t                   rb_pend_chunk_sz;
327     // storage for above ring buffer data
328     uint16_t                            pending_chunk_sz[CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX/ESP_APPTRACE_TRAX_BLOCK_SIZE + 2];
329     // current (accumulated) pending user data chunk size
330     uint16_t                            cur_pending_chunk_sz;
331 #endif
332 #endif
333 } esp_apptrace_trax_data_t;
334 
335 /** tracing module internal data */
336 typedef struct {
337     esp_apptrace_lock_t         lock;   // sync lock
338     uint8_t                     inited; // module initialization state flag
339     // ring buffer control struct for data from host (down buffer)
340     esp_apptrace_rb_t           rb_down;
341     // storage for above ring buffer data
342     esp_apptrace_trax_data_t    trax;   // TRAX HW transport data
343 } esp_apptrace_buffer_t;
344 
345 static esp_apptrace_buffer_t    s_trace_buf;
346 
347 #if ESP_APPTRACE_PRINT_LOCK
348 static esp_apptrace_lock_t s_log_lock = {.irq_stat = 0, .portmux = portMUX_INITIALIZER_UNLOCKED};
349 #endif
350 
351 typedef struct {
352     uint8_t *(*get_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
353     esp_err_t (*put_up_buffer)(uint8_t *, esp_apptrace_tmo_t *);
354     esp_err_t (*flush_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
355     uint8_t *(*get_down_buffer)(uint32_t *, esp_apptrace_tmo_t *);
356     esp_err_t (*put_down_buffer)(uint8_t *, esp_apptrace_tmo_t *);
357     bool (*host_is_connected)(void);
358     esp_err_t (*status_reg_set)(uint32_t val);
359     esp_err_t (*status_reg_get)(uint32_t *val);
360 } esp_apptrace_hw_t;
361 
362 static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size);
363 static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo);
364 static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo);
365 static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
366 static bool esp_apptrace_trax_host_is_connected(void);
367 static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo);
368 static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
369 static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val);
370 static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val);
371 
372 static esp_apptrace_hw_t s_trace_hw[ESP_APPTRACE_HW_MAX] = {
373     {
374         .get_up_buffer = esp_apptrace_trax_get_buffer,
375         .put_up_buffer = esp_apptrace_trax_put_buffer,
376         .flush_up_buffer = esp_apptrace_trax_flush,
377         .get_down_buffer = esp_apptrace_trax_down_buffer_get,
378         .put_down_buffer = esp_apptrace_trax_down_buffer_put,
379         .host_is_connected = esp_apptrace_trax_host_is_connected,
380         .status_reg_set = esp_apptrace_trax_status_reg_set,
381         .status_reg_get = esp_apptrace_trax_status_reg_get
382     }
383 };
384 
esp_apptrace_log_lock(void)385 static inline int esp_apptrace_log_lock(void)
386 {
387 #if ESP_APPTRACE_PRINT_LOCK
388     esp_apptrace_tmo_t tmo;
389     esp_apptrace_tmo_init(&tmo, ESP_APPTRACE_TMO_INFINITE);
390     int ret = esp_apptrace_lock_take(&s_log_lock, &tmo);
391     return ret;
392 #else
393     return 0;
394 #endif
395 }
396 
esp_apptrace_log_unlock(void)397 static inline void esp_apptrace_log_unlock(void)
398 {
399  #if ESP_APPTRACE_PRINT_LOCK
400     esp_apptrace_lock_give(&s_log_lock);
401 #endif
402 }
403 
esp_apptrace_lock_initialize(esp_apptrace_lock_t * lock)404 static inline esp_err_t esp_apptrace_lock_initialize(esp_apptrace_lock_t *lock)
405 {
406 #if CONFIG_APPTRACE_LOCK_ENABLE
407     esp_apptrace_lock_init(lock);
408 #endif
409     return ESP_OK;
410 }
411 
esp_apptrace_lock_cleanup(void)412 static inline esp_err_t esp_apptrace_lock_cleanup(void)
413 {
414     return ESP_OK;
415 }
416 
esp_apptrace_lock(esp_apptrace_tmo_t * tmo)417 esp_err_t esp_apptrace_lock(esp_apptrace_tmo_t *tmo)
418 {
419 #if CONFIG_APPTRACE_LOCK_ENABLE
420     esp_err_t ret = esp_apptrace_lock_take(&s_trace_buf.lock, tmo);
421     if (ret != ESP_OK) {
422         return ESP_FAIL;
423     }
424 #endif
425     return ESP_OK;
426 }
427 
esp_apptrace_unlock(void)428 esp_err_t esp_apptrace_unlock(void)
429 {
430     esp_err_t ret = ESP_OK;
431 #if CONFIG_APPTRACE_LOCK_ENABLE
432     ret = esp_apptrace_lock_give(&s_trace_buf.lock);
433 #endif
434     return ret;
435 }
436 
437 #if CONFIG_APPTRACE_DEST_TRAX
438 
esp_apptrace_trax_select_memory_block(int block_num)439 static inline void esp_apptrace_trax_select_memory_block(int block_num)
440 {
441     // select memory block to be exposed to the TRAX module (accessed by host)
442 #if CONFIG_IDF_TARGET_ESP32
443     DPORT_WRITE_PERI_REG(DPORT_TRACEMEM_MUX_MODE_REG, block_num ? TRACEMEM_MUX_BLK0_ONLY : TRACEMEM_MUX_BLK1_ONLY);
444 #elif CONFIG_IDF_TARGET_ESP32S2
445     DPORT_WRITE_PERI_REG(DPORT_PMS_OCCUPY_3_REG, block_num ? BIT(TRACEMEM_MUX_BLK0_NUM-4) : BIT(TRACEMEM_MUX_BLK1_NUM-4));
446 #endif
447 }
448 
esp_apptrace_trax_init(void)449 static void esp_apptrace_trax_init(void)
450 {
451     // Stop trace, if any (on the current CPU)
452     eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TRSTP);
453     eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TMEN);
454     eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(ESP_APPTRACE_TRAX_INBLOCK_START));
455     // this is for OpenOCD to let him know where stub entries vector is resided
456     // must be read by host before any transfer using TRAX
457     eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0);
458 
459     ESP_APPTRACE_LOGI("Initialized TRAX on CPU%d", xPortGetCoreID());
460 }
461 
462 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
463 // keep the size of buffered data for copying to TRAX mem block.
464 // Only whole user blocks should be copied from buffer to TRAX block upon the switch
esp_apptrace_trax_pend_chunk_sz_update(uint16_t size)465 static void esp_apptrace_trax_pend_chunk_sz_update(uint16_t size)
466 {
467     ESP_APPTRACE_LOGD("Update chunk enter %d/%d  w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz, size,
468         s_trace_buf.trax.rb_pend_chunk_sz.wr, s_trace_buf.trax.rb_pend_chunk_sz.rd, s_trace_buf.trax.rb_pend_chunk_sz.cur_size);
469 
470     if ((uint32_t)s_trace_buf.trax.cur_pending_chunk_sz + (uint32_t)size <= ESP_APPTRACE_TRAX_BLOCK_SIZE) {
471         ESP_APPTRACE_LOGD("Update chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
472         s_trace_buf.trax.cur_pending_chunk_sz += size;
473     } else {
474         uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
475         if (!chunk_sz) {
476             assert(false && "Failed to alloc pended chunk sz slot!");
477         } else {
478             ESP_APPTRACE_LOGD("Update new chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
479             *chunk_sz = s_trace_buf.trax.cur_pending_chunk_sz;
480             s_trace_buf.trax.cur_pending_chunk_sz = size;
481         }
482     }
483 }
484 
esp_apptrace_trax_pend_chunk_sz_get(void)485 static uint16_t esp_apptrace_trax_pend_chunk_sz_get(void)
486 {
487     uint16_t ch_sz;
488     ESP_APPTRACE_LOGD("Get chunk enter %d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz,
489         s_trace_buf.trax.rb_pend_chunk_sz.wr, s_trace_buf.trax.rb_pend_chunk_sz.rd, s_trace_buf.trax.rb_pend_chunk_sz.cur_size);
490 
491     uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
492     if (!chunk_sz) {
493         ch_sz = s_trace_buf.trax.cur_pending_chunk_sz;
494         s_trace_buf.trax.cur_pending_chunk_sz = 0;
495     } else {
496         ch_sz = *chunk_sz;
497     }
498     return ch_sz;
499 }
500 #endif
501 
502 // assumed to be protected by caller from multi-core/thread access
esp_apptrace_trax_block_switch(void)503 static __attribute__((noinline)) esp_err_t esp_apptrace_trax_block_switch(void)
504 {
505     int prev_block_num = s_trace_buf.trax.state.in_block % 2;
506     int new_block_num = prev_block_num ? (0) : (1);
507     int res = ESP_OK;
508     extern uint32_t __esp_apptrace_trax_eri_updated;
509 
510     // indicate to host that we are about to update.
511     // this is used only to place CPU into streaming mode at tracing startup
512     // before starting streaming host can halt us after we read  ESP_APPTRACE_TRAX_CTRL_REG and before we updated it
513     // HACK: in this case host will set breakpoint just after ESP_APPTRACE_TRAX_CTRL_REG update,
514     // here we set address to set bp at
515     // enter ERI update critical section
516     eri_write(ESP_APPTRACE_TRAX_STAT_REG, (uint32_t)&__esp_apptrace_trax_eri_updated);
517 
518     uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
519     uint32_t host_connected = ESP_APPTRACE_TRAX_HOST_CONNECT & ctrl_reg;
520     if (host_connected) {
521         uint32_t acked_block = ESP_APPTRACE_TRAX_BLOCK_ID_GET(ctrl_reg);
522         uint32_t host_to_read = ESP_APPTRACE_TRAX_BLOCK_LEN_GET(ctrl_reg);
523         if (host_to_read != 0 || acked_block != (s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)) {
524             ESP_APPTRACE_LOGD("HC[%d]: Can not switch %x %d %x %x/%lx, m %d", xPortGetCoreID(), ctrl_reg, host_to_read, acked_block,
525                 s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK, s_trace_buf.trax.state.in_block,
526                 s_trace_buf.trax.state.markers[prev_block_num]);
527             res = ESP_ERR_NO_MEM;
528             goto _on_func_exit;
529         }
530     }
531     s_trace_buf.trax.state.markers[new_block_num] = 0;
532     // switch to new block
533     s_trace_buf.trax.state.in_block++;
534 
535     esp_apptrace_trax_select_memory_block(new_block_num);
536     // handle data from host
537     esp_hostdata_hdr_t *hdr = (esp_hostdata_hdr_t *)s_trace_buf.trax.blocks[new_block_num].start;
538     if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA && hdr->block_sz > 0) {
539         // TODO: add support for multiple blocks from host, currently there is no need for that
540         uint8_t *p = s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.blocks[new_block_num].sz;
541         ESP_APPTRACE_LOGD("Recvd %d bytes from host [%x %x %x %x %x %x %x %x .. %x %x %x %x %x %x %x %x]", hdr->block_sz,
542             *(s_trace_buf.trax.blocks[new_block_num].start+0), *(s_trace_buf.trax.blocks[new_block_num].start+1),
543             *(s_trace_buf.trax.blocks[new_block_num].start+2), *(s_trace_buf.trax.blocks[new_block_num].start+3),
544             *(s_trace_buf.trax.blocks[new_block_num].start+4), *(s_trace_buf.trax.blocks[new_block_num].start+5),
545             *(s_trace_buf.trax.blocks[new_block_num].start+6), *(s_trace_buf.trax.blocks[new_block_num].start+7),
546             *(p-8), *(p-7), *(p-6), *(p-5), *(p-4), *(p-3), *(p-2), *(p-1));
547         uint32_t sz = esp_apptrace_trax_down_buffer_write_nolock((uint8_t *)(hdr+1), hdr->block_sz);
548         if (sz != hdr->block_sz) {
549             ESP_APPTRACE_LOGE("Failed to write %d bytes to down buffer (%d %d)!", hdr->block_sz - sz, hdr->block_sz, sz);
550         }
551         hdr->block_sz = 0;
552     }
553 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
554     // copy pending data to TRAX block if any
555 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
556     uint16_t max_chunk_sz = esp_apptrace_trax_pend_chunk_sz_get();
557 #else
558     uint16_t max_chunk_sz = s_trace_buf.trax.blocks[new_block_num].sz;
559 #endif
560     while (s_trace_buf.trax.state.markers[new_block_num] < max_chunk_sz) {
561         uint32_t read_sz = esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend);
562         if (read_sz == 0) {
563 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
564             /* theres is a bug: esp_apptrace_trax_pend_chunk_sz_get returned wrong value,
565                it must be greater or equal to one returned by esp_apptrace_rb_read_size_get */
566             ESP_APPTRACE_LOGE("No pended bytes, must be > 0 and <= %d!", max_chunk_sz);
567 #endif
568             break;
569         }
570         if (read_sz > max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num]) {
571             read_sz = max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num];
572         }
573         uint8_t *ptr = esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend, read_sz);
574         if (!ptr) {
575             assert(false && "Failed to consume pended bytes!!");
576             break;
577         }
578         if (host_connected) {
579             ESP_APPTRACE_LOGD("Pump %d pend bytes [%x %x %x %x : %x %x %x %x : %x %x %x %x : %x %x...%x %x]",
580                 read_sz, *(ptr+0), *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
581                 *(ptr+5), *(ptr+6), *(ptr+7), *(ptr+8), *(ptr+9), *(ptr+10), *(ptr+11), *(ptr+12), *(ptr+13), *(ptr+read_sz-2), *(ptr+read_sz-1));
582         }
583         memcpy(s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.state.markers[new_block_num], ptr, read_sz);
584         s_trace_buf.trax.state.markers[new_block_num] += read_sz;
585     }
586 #endif
587     eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(s_trace_buf.trax.state.in_block) |
588               host_connected | ESP_APPTRACE_TRAX_BLOCK_LEN(s_trace_buf.trax.state.markers[prev_block_num]));
589 
590 _on_func_exit:
591     // exit ERI update critical section
592     eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0x0);
593     // TODO: currently host sets breakpoint, use break instruction to stop;
594     // it will allow to use ESP_APPTRACE_TRAX_STAT_REG for other purposes
595     asm volatile (
596         "    .global     __esp_apptrace_trax_eri_updated\n"
597         "__esp_apptrace_trax_eri_updated:\n"); // host will set bp here to resolve collision at streaming start
598     return res;
599 }
600 
esp_apptrace_trax_block_switch_waitus(esp_apptrace_tmo_t * tmo)601 static esp_err_t esp_apptrace_trax_block_switch_waitus(esp_apptrace_tmo_t *tmo)
602 {
603     int res;
604 
605     while ((res = esp_apptrace_trax_block_switch()) != ESP_OK) {
606         res = esp_apptrace_tmo_check(tmo);
607         if (res != ESP_OK) {
608             break;
609         }
610     }
611     return res;
612 }
613 
esp_apptrace_trax_down_buffer_get(uint32_t * size,esp_apptrace_tmo_t * tmo)614 static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo)
615 {
616     uint8_t *ptr = NULL;
617 
618     int res = esp_apptrace_lock(tmo);
619     if (res != ESP_OK) {
620         return NULL;
621     }
622     while (1) {
623         uint32_t sz = esp_apptrace_rb_read_size_get(&s_trace_buf.rb_down);
624         if (sz != 0) {
625             *size = MIN(*size, sz);
626             ptr = esp_apptrace_rb_consume(&s_trace_buf.rb_down, *size);
627             if (!ptr) {
628                 assert(false && "Failed to consume bytes from down buffer!");
629             }
630             break;
631         }
632         // may need to flush
633         uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
634         if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA) {
635             ESP_APPTRACE_LOGD("force flush");
636             res = esp_apptrace_trax_block_switch_waitus(tmo);
637             if (res != ESP_OK) {
638                 ESP_APPTRACE_LOGE("Failed to switch to another block to recv data from host!");
639                 /*do not return error because data can be in down buffer already*/
640             }
641         } else {
642             // check tmo only if there is no data from host
643             res = esp_apptrace_tmo_check(tmo);
644             if (res != ESP_OK) {
645                 return NULL;
646             }
647         }
648     }
649     if (esp_apptrace_unlock() != ESP_OK) {
650         assert(false && "Failed to unlock apptrace data!");
651     }
652     return ptr;
653 }
654 
esp_apptrace_trax_down_buffer_put(uint8_t * ptr,esp_apptrace_tmo_t * tmo)655 static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
656 {
657     /* nothing todo */
658     return ESP_OK;
659 }
660 
esp_apptrace_trax_down_buffer_write_nolock(uint8_t * data,uint32_t size)661 static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size)
662 {
663     uint32_t total_sz = 0;
664 
665     while (total_sz < size) {
666         ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock WRS %d-%d-%d %d", s_trace_buf.rb_down.wr, s_trace_buf.rb_down.rd,
667             s_trace_buf.rb_down.cur_size, size);
668         uint32_t wr_sz = esp_apptrace_rb_write_size_get(&s_trace_buf.rb_down);
669         if (wr_sz == 0) {
670             break;
671         }
672 
673         if (wr_sz > size - total_sz) {
674             wr_sz = size - total_sz;
675         }
676         ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d", wr_sz);
677         uint8_t *ptr = esp_apptrace_rb_produce(&s_trace_buf.rb_down, wr_sz);
678         if (!ptr) {
679             assert(false && "Failed to produce bytes to down buffer!");
680         }
681         ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d to 0x%x from 0x%x", wr_sz, ptr, data + total_sz + wr_sz);
682         memcpy(ptr, data + total_sz, wr_sz);
683         total_sz += wr_sz;
684         ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d/%d", wr_sz, total_sz);
685     }
686     return total_sz;
687 }
688 
esp_apptrace_data_header_init(uint8_t * ptr,uint16_t usr_size)689 static inline uint8_t *esp_apptrace_data_header_init(uint8_t *ptr, uint16_t usr_size)
690 {
691     // it is safe to use xPortGetCoreID() in macro call because arg is used only once inside it
692     ((esp_tracedata_hdr_t *)ptr)->block_sz = ESP_APPTRACE_USR_BLOCK_CORE(xPortGetCoreID()) | usr_size;
693     ((esp_tracedata_hdr_t *)ptr)->wr_sz = 0;
694     return ptr + sizeof(esp_tracedata_hdr_t);
695 }
696 
esp_apptrace_trax_wait4buf(uint16_t size,esp_apptrace_tmo_t * tmo,int * pended)697 static inline uint8_t *esp_apptrace_trax_wait4buf(uint16_t size, esp_apptrace_tmo_t *tmo, int *pended)
698 {
699     uint8_t *ptr = NULL;
700 
701     int res = esp_apptrace_trax_block_switch_waitus(tmo);
702     if (res != ESP_OK) {
703         return NULL;
704     }
705     // check if we still have pending data
706 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
707     if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
708         // if after TRAX block switch still have pending data (not all pending data have been pumped to TRAX block)
709         // alloc new pending buffer
710         *pended = 1;
711         ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
712         if (!ptr) {
713             ESP_APPTRACE_LOGE("Failed to alloc pend buf 1: w-r-s %d-%d-%d!", s_trace_buf.trax.rb_pend.wr, s_trace_buf.trax.rb_pend.rd, s_trace_buf.trax.rb_pend.cur_size);
714         }
715     } else
716 #endif
717     {
718         // update block pointers
719         if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + size > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
720 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
721             *pended = 1;
722             ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
723             if (ptr == NULL) {
724                 ESP_APPTRACE_LOGE("Failed to alloc pend buf 2: w-r-s %d-%d-%d!", s_trace_buf.trax.rb_pend.wr, s_trace_buf.trax.rb_pend.rd, s_trace_buf.trax.rb_pend.cur_size);
725             }
726 #endif
727         } else {
728             *pended = 0;
729             ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
730         }
731     }
732 
733     return ptr;
734 }
735 
esp_apptrace_trax_get_buffer(uint32_t size,esp_apptrace_tmo_t * tmo)736 static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo)
737 {
738     uint8_t *buf_ptr = NULL;
739 
740     if (size > ESP_APPTRACE_USR_DATA_LEN_MAX) {
741         ESP_APPTRACE_LOGE("Too large user data size %d!", size);
742         return NULL;
743     }
744 
745     int res = esp_apptrace_lock(tmo);
746     if (res != ESP_OK) {
747         return NULL;
748     }
749     // check for data in the pending buffer
750 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
751     if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
752         // if we have buffered data try to switch TRAX block
753         esp_apptrace_trax_block_switch();
754         // if switch was successful, part or all pended data have been copied to TRAX block
755     }
756     if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
757         // if we have buffered data alloc new pending buffer
758         ESP_APPTRACE_LOGD("Get %d bytes from PEND buffer", size);
759         buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
760         if (buf_ptr == NULL) {
761             int pended_buf;
762             buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
763             if (buf_ptr) {
764                 if (pended_buf) {
765 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
766                     esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
767 #endif
768                 } else {
769                     ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
770                     // update cur block marker
771                     ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
772                 }
773             }
774         } else {
775 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
776             esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
777 #endif
778         }
779     } else
780 #endif
781     if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + ESP_APPTRACE_USR_BLOCK_RAW_SZ(size) > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
782 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
783         ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from PEND buffer", size);
784         buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
785         if (buf_ptr) {
786 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
787             esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
788 #endif
789         }
790 #endif
791         if (buf_ptr == NULL) {
792             int pended_buf;
793             ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from pend buffer", size);
794             buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
795             if (buf_ptr) {
796                 if (pended_buf) {
797 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
798                     esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
799 #endif
800                 } else {
801                     ESP_APPTRACE_LOGD("Got %d bytes from TRAX buffer", size);
802                     // update cur block marker
803                     ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
804                 }
805             }
806         }
807     } else {
808         ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
809         // fit to curr TRAX nlock
810         buf_ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
811         // update cur block marker
812         ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
813     }
814     if (buf_ptr) {
815         buf_ptr = esp_apptrace_data_header_init(buf_ptr, size);
816     }
817 
818     // now we can safely unlock apptrace to allow other tasks/ISRs to get other buffers and write their data
819     if (esp_apptrace_unlock() != ESP_OK) {
820         assert(false && "Failed to unlock apptrace data!");
821     }
822 
823     return buf_ptr;
824 }
825 
esp_apptrace_trax_put_buffer(uint8_t * ptr,esp_apptrace_tmo_t * tmo)826 static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
827 {
828     int res = ESP_OK;
829     esp_tracedata_hdr_t *hdr = (esp_tracedata_hdr_t *)(ptr - sizeof(esp_tracedata_hdr_t));
830 
831     // update written size
832     hdr->wr_sz = hdr->block_sz;
833 
834     // TODO: mark block as busy in order not to re-use it for other tracing calls until it is completely written
835     // TODO: avoid potential situation when all memory is consumed by low prio tasks which can not complete writing due to
836     // higher prio tasks and the latter can not allocate buffers at all
837     // this is abnormal situation can be detected on host which will receive only uncompleted buffers
838     // workaround: use own memcpy which will kick-off dead tracing calls
839 
840     return res;
841 }
842 
esp_apptrace_trax_flush(uint32_t min_sz,esp_apptrace_tmo_t * tmo)843 static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo)
844 {
845     int res = ESP_OK;
846 
847     if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() < min_sz) {
848         ESP_APPTRACE_LOGI("Ignore flush request for min %d bytes. Bytes in TRAX block: %d.", min_sz, ESP_APPTRACE_TRAX_INBLOCK_MARKER());
849         return ESP_OK;
850     }
851     // switch TRAX block while size of data is more than min size
852     while (ESP_APPTRACE_TRAX_INBLOCK_MARKER() > 0) {
853         ESP_APPTRACE_LOGD("Try to flush %d bytes. Wait until block switch for %u us", ESP_APPTRACE_TRAX_INBLOCK_MARKER(), tmo->tmo);
854         res = esp_apptrace_trax_block_switch_waitus(tmo);
855         if (res != ESP_OK) {
856             ESP_APPTRACE_LOGE("Failed to switch to another block!");
857             return res;
858         }
859     }
860 
861     return res;
862 }
863 
esp_apptrace_trax_host_is_connected(void)864 static bool esp_apptrace_trax_host_is_connected(void)
865 {
866     return eri_read(ESP_APPTRACE_TRAX_CTRL_REG) & ESP_APPTRACE_TRAX_HOST_CONNECT ? true : false;
867 }
868 
esp_apptrace_trax_status_reg_set(uint32_t val)869 static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val)
870 {
871     eri_write(ESP_APPTRACE_TRAX_STAT_REG, val);
872     return ESP_OK;
873 }
874 
esp_apptrace_trax_status_reg_get(uint32_t * val)875 static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val)
876 {
877     *val = eri_read(ESP_APPTRACE_TRAX_STAT_REG);
878     return ESP_OK;
879 }
880 
esp_apptrace_trax_dest_init(void)881 static esp_err_t esp_apptrace_trax_dest_init(void)
882 {
883     for (size_t i = 0; i < ESP_APPTRACE_TRAX_BLOCKS_NUM; i++) {
884         s_trace_buf.trax.blocks[i].start = (uint8_t *)s_trax_blocks[i];
885         s_trace_buf.trax.blocks[i].sz = ESP_APPTRACE_TRAX_BLOCK_SIZE;
886         s_trace_buf.trax.state.markers[i] = 0;
887     }
888     s_trace_buf.trax.state.in_block = ESP_APPTRACE_TRAX_INBLOCK_START;
889 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
890     esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend, s_trace_buf.trax.pending_data,
891                         sizeof(s_trace_buf.trax.pending_data));
892 #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
893     s_trace_buf.trax.cur_pending_chunk_sz = 0;
894     esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend_chunk_sz, (uint8_t *)s_trace_buf.trax.pending_chunk_sz,
895                         sizeof(s_trace_buf.trax.pending_chunk_sz));
896 #endif
897 #endif
898 
899 #if CONFIG_IDF_TARGET_ESP32
900     DPORT_WRITE_PERI_REG(DPORT_PRO_TRACEMEM_ENA_REG, DPORT_PRO_TRACEMEM_ENA_M);
901 #if CONFIG_FREERTOS_UNICORE == 0
902     DPORT_WRITE_PERI_REG(DPORT_APP_TRACEMEM_ENA_REG, DPORT_APP_TRACEMEM_ENA_M);
903 #endif
904 #endif
905     esp_apptrace_trax_select_memory_block(0);
906 
907     return ESP_OK;
908 }
909 #endif
910 
esp_apptrace_init(void)911 esp_err_t esp_apptrace_init(void)
912 {
913     int res;
914 
915     if (!s_trace_buf.inited) {
916         memset(&s_trace_buf, 0, sizeof(s_trace_buf));
917         // disabled by default
918         esp_apptrace_rb_init(&s_trace_buf.rb_down, NULL, 0);
919         res = esp_apptrace_lock_initialize(&s_trace_buf.lock);
920         if (res != ESP_OK) {
921             ESP_APPTRACE_LOGE("Failed to init log lock (%d)!", res);
922             return res;
923         }
924 #if CONFIG_APPTRACE_DEST_TRAX
925         res = esp_apptrace_trax_dest_init();
926         if (res != ESP_OK) {
927             ESP_APPTRACE_LOGE("Failed to init TRAX dest data (%d)!", res);
928             esp_apptrace_lock_cleanup();
929             return res;
930         }
931 #endif
932     }
933 
934 #if CONFIG_APPTRACE_DEST_TRAX
935     // init TRAX on this CPU
936     esp_apptrace_trax_init();
937 #endif
938 
939     s_trace_buf.inited |= 1 << xPortGetCoreID(); // global and this CPU-specific data are inited
940 
941     return ESP_OK;
942 }
943 
esp_apptrace_down_buffer_config(uint8_t * buf,uint32_t size)944 void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)
945 {
946     esp_apptrace_rb_init(&s_trace_buf.rb_down, buf, size);
947 }
948 
esp_apptrace_read(esp_apptrace_dest_t dest,void * buf,uint32_t * size,uint32_t user_tmo)949 esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *buf, uint32_t *size, uint32_t user_tmo)
950 {
951     int res = ESP_OK;
952     esp_apptrace_tmo_t tmo;
953     esp_apptrace_hw_t *hw = NULL;
954 
955     if (dest == ESP_APPTRACE_DEST_TRAX) {
956 #if CONFIG_APPTRACE_DEST_TRAX
957         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
958 #else
959         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
960         return ESP_ERR_NOT_SUPPORTED;
961 #endif
962     } else {
963         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
964         return ESP_ERR_NOT_SUPPORTED;
965     }
966     if (buf == NULL || size == NULL || *size == 0) {
967         return ESP_ERR_INVALID_ARG;
968     }
969 
970     //TODO: callback system
971     esp_apptrace_tmo_init(&tmo, user_tmo);
972     uint32_t act_sz = *size;
973     *size = 0;
974     uint8_t * ptr = hw->get_down_buffer(&act_sz, &tmo);
975     if (ptr && act_sz > 0) {
976         ESP_APPTRACE_LOGD("Read %d bytes from host", act_sz);
977         memcpy(buf, ptr, act_sz);
978         res = hw->put_down_buffer(ptr, &tmo);
979         *size = act_sz;
980     } else {
981         res = ESP_ERR_TIMEOUT;
982     }
983 
984     return res;
985 }
986 
esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest,uint32_t * size,uint32_t user_tmo)987 uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t user_tmo)
988 {
989     esp_apptrace_tmo_t tmo;
990     esp_apptrace_hw_t *hw = NULL;
991 
992     if (dest == ESP_APPTRACE_DEST_TRAX) {
993 #if CONFIG_APPTRACE_DEST_TRAX
994         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
995 #else
996         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
997         return NULL;
998 #endif
999     } else {
1000         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1001         return NULL;
1002     }
1003     if (size == NULL || *size == 0) {
1004         return NULL;
1005     }
1006 
1007     esp_apptrace_tmo_init(&tmo, user_tmo);
1008     return hw->get_down_buffer(size, &tmo);
1009 }
1010 
esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest,uint8_t * ptr,uint32_t user_tmo)1011 esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
1012 {
1013     esp_apptrace_tmo_t tmo;
1014     esp_apptrace_hw_t *hw = NULL;
1015 
1016     if (dest == ESP_APPTRACE_DEST_TRAX) {
1017 #if CONFIG_APPTRACE_DEST_TRAX
1018         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1019 #else
1020         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1021         return ESP_ERR_NOT_SUPPORTED;
1022 #endif
1023     } else {
1024         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1025         return ESP_ERR_NOT_SUPPORTED;
1026     }
1027     if (ptr == NULL) {
1028         return ESP_ERR_INVALID_ARG;
1029     }
1030 
1031     esp_apptrace_tmo_init(&tmo, user_tmo);
1032     return hw->put_down_buffer(ptr, &tmo);
1033 }
1034 
esp_apptrace_write(esp_apptrace_dest_t dest,const void * data,uint32_t size,uint32_t user_tmo)1035 esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t user_tmo)
1036 {
1037     uint8_t *ptr = NULL;
1038     esp_apptrace_tmo_t tmo;
1039     esp_apptrace_hw_t *hw = NULL;
1040 
1041     if (dest == ESP_APPTRACE_DEST_TRAX) {
1042 #if CONFIG_APPTRACE_DEST_TRAX
1043         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1044 #else
1045         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1046         return ESP_ERR_NOT_SUPPORTED;
1047 #endif
1048     } else {
1049         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1050         return ESP_ERR_NOT_SUPPORTED;
1051     }
1052     if (data == NULL || size == 0) {
1053         return ESP_ERR_INVALID_ARG;
1054     }
1055 
1056     esp_apptrace_tmo_init(&tmo, user_tmo);
1057     ptr = hw->get_up_buffer(size, &tmo);
1058     if (ptr == NULL) {
1059         return ESP_ERR_NO_MEM;
1060     }
1061 
1062     // actually can be suspended here by higher prio tasks/ISRs
1063     //TODO: use own memcpy with dead trace calls kick-off algo and tmo expiration check
1064     memcpy(ptr, data, size);
1065 
1066     // now indicate that this buffer is ready to be sent off to host
1067     return hw->put_up_buffer(ptr, &tmo);
1068 }
1069 
esp_apptrace_vprintf_to(esp_apptrace_dest_t dest,uint32_t user_tmo,const char * fmt,va_list ap)1070 int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t user_tmo, const char *fmt, va_list ap)
1071 {
1072     uint16_t nargs = 0;
1073     uint8_t *pout, *p = (uint8_t *)fmt;
1074     esp_apptrace_tmo_t tmo;
1075     esp_apptrace_hw_t *hw = NULL;
1076 
1077     if (dest == ESP_APPTRACE_DEST_TRAX) {
1078 #if CONFIG_APPTRACE_DEST_TRAX
1079         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1080 #else
1081         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1082         return ESP_ERR_NOT_SUPPORTED;
1083 #endif
1084     } else {
1085         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1086         return ESP_ERR_NOT_SUPPORTED;
1087     }
1088     if (fmt == NULL) {
1089         return ESP_ERR_INVALID_ARG;
1090     }
1091 
1092     esp_apptrace_tmo_init(&tmo, user_tmo);
1093     ESP_APPTRACE_LOGD("fmt %x", fmt);
1094     while ((p = (uint8_t *)strchr((char *)p, '%')) && nargs < ESP_APPTRACE_MAX_VPRINTF_ARGS) {
1095         p++;
1096         if (*p != '%' && *p != 0) {
1097             nargs++;
1098         }
1099     }
1100     ESP_APPTRACE_LOGD("nargs = %d", nargs);
1101     if (p) {
1102         ESP_APPTRACE_LOGE("Failed to store all printf args!");
1103     }
1104 
1105     pout = hw->get_up_buffer(1 + sizeof(char *) + nargs * sizeof(uint32_t), &tmo);
1106     if (pout == NULL) {
1107         ESP_APPTRACE_LOGE("Failed to get buffer!");
1108         return -1;
1109     }
1110     p = pout;
1111     *pout = nargs;
1112     pout++;
1113     *(const char **)pout = fmt;
1114     pout += sizeof(char *);
1115     while (nargs-- > 0) {
1116         uint32_t arg = va_arg(ap, uint32_t);
1117         *(uint32_t *)pout = arg;
1118         pout += sizeof(uint32_t);
1119         ESP_APPTRACE_LOGD("arg %x", arg);
1120     }
1121 
1122     int ret = hw->put_up_buffer(p, &tmo);
1123     if (ret != ESP_OK) {
1124         ESP_APPTRACE_LOGE("Failed to put printf buf (%d)!", ret);
1125         return -1;
1126     }
1127 
1128     return (pout - p);
1129 }
1130 
esp_apptrace_vprintf(const char * fmt,va_list ap)1131 int esp_apptrace_vprintf(const char *fmt, va_list ap)
1132 {
1133     return esp_apptrace_vprintf_to(ESP_APPTRACE_DEST_TRAX, /*ESP_APPTRACE_TMO_INFINITE*/0, fmt, ap);
1134 }
1135 
esp_apptrace_buffer_get(esp_apptrace_dest_t dest,uint32_t size,uint32_t user_tmo)1136 uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t user_tmo)
1137 {
1138     esp_apptrace_tmo_t tmo;
1139     esp_apptrace_hw_t *hw = NULL;
1140 
1141     if (dest == ESP_APPTRACE_DEST_TRAX) {
1142 #if CONFIG_APPTRACE_DEST_TRAX
1143         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1144 #else
1145         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1146         return NULL;
1147 #endif
1148     } else {
1149         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1150         return NULL;
1151     }
1152     if (size == 0) {
1153         return NULL;
1154     }
1155 
1156     esp_apptrace_tmo_init(&tmo, user_tmo);
1157     return hw->get_up_buffer(size, &tmo);
1158 }
1159 
esp_apptrace_buffer_put(esp_apptrace_dest_t dest,uint8_t * ptr,uint32_t user_tmo)1160 esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
1161 {
1162     esp_apptrace_tmo_t tmo;
1163     esp_apptrace_hw_t *hw = NULL;
1164 
1165     if (dest == ESP_APPTRACE_DEST_TRAX) {
1166 #if CONFIG_APPTRACE_DEST_TRAX
1167         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1168 #else
1169         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1170         return ESP_ERR_NOT_SUPPORTED;
1171 #endif
1172     } else {
1173         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1174         return ESP_ERR_NOT_SUPPORTED;
1175     }
1176     if (ptr == NULL) {
1177         return ESP_ERR_INVALID_ARG;
1178     }
1179 
1180     esp_apptrace_tmo_init(&tmo, user_tmo);
1181     return hw->put_up_buffer(ptr, &tmo);
1182 }
1183 
esp_apptrace_flush_nolock(esp_apptrace_dest_t dest,uint32_t min_sz,uint32_t usr_tmo)1184 esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t usr_tmo)
1185 {
1186     esp_apptrace_tmo_t tmo;
1187     esp_apptrace_hw_t *hw = NULL;
1188 
1189     if (dest == ESP_APPTRACE_DEST_TRAX) {
1190 #if CONFIG_APPTRACE_DEST_TRAX
1191         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1192 #else
1193         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1194         return ESP_ERR_NOT_SUPPORTED;
1195 #endif
1196     } else {
1197         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1198         return ESP_ERR_NOT_SUPPORTED;
1199     }
1200 
1201     esp_apptrace_tmo_init(&tmo, usr_tmo);
1202     return hw->flush_up_buffer(min_sz, &tmo);
1203 }
1204 
esp_apptrace_flush(esp_apptrace_dest_t dest,uint32_t usr_tmo)1205 esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t usr_tmo)
1206 {
1207     int res;
1208     esp_apptrace_tmo_t tmo;
1209 
1210     esp_apptrace_tmo_init(&tmo, usr_tmo);
1211     res = esp_apptrace_lock(&tmo);
1212     if (res != ESP_OK) {
1213         ESP_APPTRACE_LOGE("Failed to lock apptrace data (%d)!", res);
1214         return res;
1215     }
1216 
1217     res = esp_apptrace_flush_nolock(dest, 0, esp_apptrace_tmo_remaining_us(&tmo));
1218     if (res != ESP_OK) {
1219         ESP_APPTRACE_LOGE("Failed to flush apptrace data (%d)!", res);
1220     }
1221 
1222     if (esp_apptrace_unlock() != ESP_OK) {
1223         assert(false && "Failed to unlock apptrace data!");
1224     }
1225 
1226     return res;
1227 }
1228 
esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)1229 bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)
1230 {
1231     esp_apptrace_hw_t *hw = NULL;
1232 
1233     if (dest == ESP_APPTRACE_DEST_TRAX) {
1234 #if CONFIG_APPTRACE_DEST_TRAX
1235         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1236 #else
1237         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1238         return false;
1239 #endif
1240     } else {
1241         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1242         return false;
1243     }
1244     return hw->host_is_connected();
1245 }
1246 
esp_apptrace_status_reg_set(esp_apptrace_dest_t dest,uint32_t val)1247 esp_err_t esp_apptrace_status_reg_set(esp_apptrace_dest_t dest, uint32_t val)
1248 {
1249     esp_apptrace_hw_t *hw = NULL;
1250 
1251     if (dest == ESP_APPTRACE_DEST_TRAX) {
1252 #if CONFIG_APPTRACE_DEST_TRAX
1253         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1254 #else
1255         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1256         return ESP_ERR_NOT_SUPPORTED;
1257 #endif
1258     } else {
1259         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1260         return ESP_ERR_NOT_SUPPORTED;
1261     }
1262     return hw->status_reg_set(val);
1263 }
1264 
esp_apptrace_status_reg_get(esp_apptrace_dest_t dest,uint32_t * val)1265 esp_err_t esp_apptrace_status_reg_get(esp_apptrace_dest_t dest, uint32_t *val)
1266 {
1267     esp_apptrace_hw_t *hw = NULL;
1268 
1269     if (dest == ESP_APPTRACE_DEST_TRAX) {
1270 #if CONFIG_APPTRACE_DEST_TRAX
1271         hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
1272 #else
1273         ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
1274         return ESP_ERR_NOT_SUPPORTED;
1275 #endif
1276     } else {
1277         ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
1278         return ESP_ERR_NOT_SUPPORTED;
1279     }
1280     return hw->status_reg_get(val);
1281 }
1282 
1283 #endif
1284