app_trace.c 54 KB

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