app_trace.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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 "soc/soc.h"
  137. #include "soc/dport_reg.h"
  138. #if CONFIG_IDF_TARGET_ESP32S2
  139. #include "soc/sensitive_reg.h"
  140. #endif
  141. #include "eri.h"
  142. #include "trax.h"
  143. #include "soc/timer_periph.h"
  144. #include "freertos/FreeRTOS.h"
  145. #include "esp_app_trace.h"
  146. #if CONFIG_APPTRACE_ENABLE
  147. #define ESP_APPTRACE_MAX_VPRINTF_ARGS 256
  148. #define ESP_APPTRACE_HOST_BUF_SIZE 256
  149. #define ESP_APPTRACE_PRINT_LOCK 0
  150. #include "esp_log.h"
  151. const static char *TAG = "esp_apptrace";
  152. #if ESP_APPTRACE_PRINT_LOCK
  153. #define ESP_APPTRACE_LOG( format, ... ) \
  154. do { \
  155. esp_apptrace_log_lock(); \
  156. ets_printf(format, ##__VA_ARGS__); \
  157. esp_apptrace_log_unlock(); \
  158. } while(0)
  159. #else
  160. #define ESP_APPTRACE_LOG( format, ... ) \
  161. do { \
  162. ets_printf(format, ##__VA_ARGS__); \
  163. } while(0)
  164. #endif
  165. #define ESP_APPTRACE_LOG_LEV( _L_, level, format, ... ) \
  166. do { \
  167. if (LOG_LOCAL_LEVEL >= level) { \
  168. ESP_APPTRACE_LOG(LOG_FORMAT(_L_, format), esp_log_early_timestamp(), TAG, ##__VA_ARGS__); \
  169. } \
  170. } while(0)
  171. #define ESP_APPTRACE_LOGE( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_ERROR, format, ##__VA_ARGS__)
  172. #define ESP_APPTRACE_LOGW( format, ... ) ESP_APPTRACE_LOG_LEV(W, ESP_LOG_WARN, format, ##__VA_ARGS__)
  173. #define ESP_APPTRACE_LOGI( format, ... ) ESP_APPTRACE_LOG_LEV(I, ESP_LOG_INFO, format, ##__VA_ARGS__)
  174. #define ESP_APPTRACE_LOGD( format, ... ) ESP_APPTRACE_LOG_LEV(D, ESP_LOG_DEBUG, format, ##__VA_ARGS__)
  175. #define ESP_APPTRACE_LOGV( format, ... ) ESP_APPTRACE_LOG_LEV(V, ESP_LOG_VERBOSE, format, ##__VA_ARGS__)
  176. #define ESP_APPTRACE_LOGO( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_NONE, format, ##__VA_ARGS__)
  177. // TODO: move these (and same definitions in trax.c to dport_reg.h)
  178. #if CONFIG_IDF_TARGET_ESP32
  179. #define TRACEMEM_MUX_PROBLK0_APPBLK1 0
  180. #define TRACEMEM_MUX_BLK0_ONLY 1
  181. #define TRACEMEM_MUX_BLK1_ONLY 2
  182. #define TRACEMEM_MUX_PROBLK1_APPBLK0 3
  183. #elif CONFIG_IDF_TARGET_ESP32S2
  184. #define TRACEMEM_MUX_BLK0_NUM 19
  185. #define TRACEMEM_MUX_BLK1_NUM 20
  186. #define TRACEMEM_BLK_NUM2ADDR(_n_) (0x3FFB8000UL + 0x4000UL*((_n_)-4))
  187. #endif
  188. // TRAX is disabled, so we use its registers for our own purposes
  189. // | 31..XXXXXX..24 | 23 .(host_connect). 23 | 22 .(host_data). 22| 21..(block_id)..15 | 14..(block_len)..0 |
  190. #define ESP_APPTRACE_TRAX_CTRL_REG ERI_TRAX_DELAYCNT
  191. #define ESP_APPTRACE_TRAX_STAT_REG ERI_TRAX_TRIGGERPC
  192. #define ESP_APPTRACE_TRAX_BLOCK_LEN_MSK 0x7FFFUL
  193. #define ESP_APPTRACE_TRAX_BLOCK_LEN(_l_) ((_l_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
  194. #define ESP_APPTRACE_TRAX_BLOCK_LEN_GET(_v_) ((_v_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
  195. #define ESP_APPTRACE_TRAX_BLOCK_ID_MSK 0x7FUL
  196. #define ESP_APPTRACE_TRAX_BLOCK_ID(_id_) (((_id_) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK) << 15)
  197. #define ESP_APPTRACE_TRAX_BLOCK_ID_GET(_v_) (((_v_) >> 15) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)
  198. #define ESP_APPTRACE_TRAX_HOST_DATA (1 << 22)
  199. #define ESP_APPTRACE_TRAX_HOST_CONNECT (1 << 23)
  200. #if CONFIG_SYSVIEW_ENABLE
  201. #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_) (0)
  202. #define ESP_APPTRACE_USR_BLOCK_LEN(_v_) (_v_)
  203. #else
  204. #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_) ((_cid_) << 15)
  205. #define ESP_APPTRACE_USR_BLOCK_LEN(_v_) (~(1 << 15) & (_v_))
  206. #endif
  207. #define ESP_APPTRACE_USR_BLOCK_RAW_SZ(_s_) ((_s_) + sizeof(esp_tracedata_hdr_t))
  208. #if CONFIG_IDF_TARGET_ESP32
  209. static volatile uint8_t *s_trax_blocks[] = {
  210. (volatile uint8_t *) 0x3FFFC000,
  211. (volatile uint8_t *) 0x3FFF8000
  212. };
  213. #elif CONFIG_IDF_TARGET_ESP32S2
  214. static volatile uint8_t *s_trax_blocks[] = {
  215. (volatile uint8_t *)TRACEMEM_BLK_NUM2ADDR(TRACEMEM_MUX_BLK0_NUM),
  216. (volatile uint8_t *)TRACEMEM_BLK_NUM2ADDR(TRACEMEM_MUX_BLK1_NUM)
  217. };
  218. #endif
  219. #define ESP_APPTRACE_TRAX_BLOCKS_NUM (sizeof(s_trax_blocks)/sizeof(s_trax_blocks[0]))
  220. #define ESP_APPTRACE_TRAX_INBLOCK_START 0
  221. #define ESP_APPTRACE_TRAX_INBLOCK_MARKER() (s_trace_buf.trax.state.markers[s_trace_buf.trax.state.in_block % 2])
  222. #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)
  223. #define ESP_APPTRACE_TRAX_INBLOCK_GET() (&s_trace_buf.trax.blocks[s_trace_buf.trax.state.in_block % 2])
  224. #define ESP_APPTRACE_TRAX_BLOCK_SIZE (0x4000UL)
  225. #if CONFIG_SYSVIEW_ENABLE
  226. #define ESP_APPTRACE_USR_DATA_LEN_MAX 255UL
  227. #else
  228. #define ESP_APPTRACE_USR_DATA_LEN_MAX (ESP_APPTRACE_TRAX_BLOCK_SIZE - sizeof(esp_tracedata_hdr_t))
  229. #endif
  230. #define ESP_APPTRACE_HW_TRAX 0
  231. #define ESP_APPTRACE_HW_MAX 1
  232. #define ESP_APPTRACE_HW(_i_) (&s_trace_hw[_i_])
  233. /** Trace data header. Every user data chunk is prepended with this header.
  234. * User allocates block with esp_apptrace_buffer_get and then fills it with data,
  235. * in multithreading environment it can happen that tasks gets buffer and then gets interrupted,
  236. * so it is possible that user data are incomplete when TRAX memory block is exposed to the host.
  237. * In this case host SW will see that wr_sz < block_sz and will report error.
  238. */
  239. typedef struct {
  240. #if CONFIG_SYSVIEW_ENABLE
  241. uint8_t block_sz; // size of allocated block for user data
  242. uint8_t wr_sz; // size of actually written data
  243. #else
  244. uint16_t block_sz; // size of allocated block for user data
  245. uint16_t wr_sz; // size of actually written data
  246. #endif
  247. } esp_tracedata_hdr_t;
  248. /** TODO: docs
  249. */
  250. typedef struct {
  251. uint16_t block_sz; // size of allocated block for user data
  252. } esp_hostdata_hdr_t;
  253. /** TRAX HW transport state */
  254. typedef struct {
  255. uint32_t in_block; // input block ID
  256. // TODO: change to uint16_t
  257. uint32_t markers[ESP_APPTRACE_TRAX_BLOCKS_NUM]; // block filling level markers
  258. } esp_apptrace_trax_state_t;
  259. /** memory block parameters */
  260. typedef struct {
  261. uint8_t *start; // start address
  262. uint16_t sz; // size
  263. } esp_apptrace_mem_block_t;
  264. /** TRAX HW transport data */
  265. typedef struct {
  266. volatile esp_apptrace_trax_state_t state; // state
  267. esp_apptrace_mem_block_t blocks[ESP_APPTRACE_TRAX_BLOCKS_NUM]; // memory blocks
  268. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  269. // ring buffer control struct for pending user blocks
  270. esp_apptrace_rb_t rb_pend;
  271. // storage for pending user blocks
  272. uint8_t pending_data[CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX + 1];
  273. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  274. // ring buffer control struct for pending user data chunks sizes,
  275. // every chunk contains whole number of user blocks and fit into TRAX memory block
  276. esp_apptrace_rb_t rb_pend_chunk_sz;
  277. // storage for above ring buffer data
  278. uint16_t pending_chunk_sz[CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX/ESP_APPTRACE_TRAX_BLOCK_SIZE + 2];
  279. // current (accumulated) pending user data chunk size
  280. uint16_t cur_pending_chunk_sz;
  281. #endif
  282. #endif
  283. } esp_apptrace_trax_data_t;
  284. /** tracing module internal data */
  285. typedef struct {
  286. esp_apptrace_lock_t lock; // sync lock
  287. uint8_t inited; // module initialization state flag
  288. // ring buffer control struct for data from host (down buffer)
  289. esp_apptrace_rb_t rb_down;
  290. // storage for above ring buffer data
  291. esp_apptrace_trax_data_t trax; // TRAX HW transport data
  292. } esp_apptrace_buffer_t;
  293. static esp_apptrace_buffer_t s_trace_buf;
  294. #if ESP_APPTRACE_PRINT_LOCK
  295. static esp_apptrace_lock_t s_log_lock = {.irq_stat = 0, .portmux = portMUX_INITIALIZER_UNLOCKED};
  296. #endif
  297. typedef struct {
  298. uint8_t *(*get_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
  299. esp_err_t (*put_up_buffer)(uint8_t *, esp_apptrace_tmo_t *);
  300. esp_err_t (*flush_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
  301. uint8_t *(*get_down_buffer)(uint32_t *, esp_apptrace_tmo_t *);
  302. esp_err_t (*put_down_buffer)(uint8_t *, esp_apptrace_tmo_t *);
  303. bool (*host_is_connected)(void);
  304. esp_err_t (*status_reg_set)(uint32_t val);
  305. esp_err_t (*status_reg_get)(uint32_t *val);
  306. } esp_apptrace_hw_t;
  307. static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size);
  308. static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo);
  309. static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo);
  310. static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
  311. static bool esp_apptrace_trax_host_is_connected(void);
  312. static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo);
  313. static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
  314. static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val);
  315. static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val);
  316. static esp_apptrace_hw_t s_trace_hw[ESP_APPTRACE_HW_MAX] = {
  317. {
  318. .get_up_buffer = esp_apptrace_trax_get_buffer,
  319. .put_up_buffer = esp_apptrace_trax_put_buffer,
  320. .flush_up_buffer = esp_apptrace_trax_flush,
  321. .get_down_buffer = esp_apptrace_trax_down_buffer_get,
  322. .put_down_buffer = esp_apptrace_trax_down_buffer_put,
  323. .host_is_connected = esp_apptrace_trax_host_is_connected,
  324. .status_reg_set = esp_apptrace_trax_status_reg_set,
  325. .status_reg_get = esp_apptrace_trax_status_reg_get
  326. }
  327. };
  328. static inline int esp_apptrace_log_lock(void)
  329. {
  330. #if ESP_APPTRACE_PRINT_LOCK
  331. esp_apptrace_tmo_t tmo;
  332. esp_apptrace_tmo_init(&tmo, ESP_APPTRACE_TMO_INFINITE);
  333. int ret = esp_apptrace_lock_take(&s_log_lock, &tmo);
  334. return ret;
  335. #else
  336. return 0;
  337. #endif
  338. }
  339. static inline void esp_apptrace_log_unlock(void)
  340. {
  341. #if ESP_APPTRACE_PRINT_LOCK
  342. esp_apptrace_lock_give(&s_log_lock);
  343. #endif
  344. }
  345. static inline esp_err_t esp_apptrace_lock_initialize(esp_apptrace_lock_t *lock)
  346. {
  347. #if CONFIG_APPTRACE_LOCK_ENABLE
  348. esp_apptrace_lock_init(lock);
  349. #endif
  350. return ESP_OK;
  351. }
  352. static inline esp_err_t esp_apptrace_lock_cleanup(void)
  353. {
  354. return ESP_OK;
  355. }
  356. esp_err_t esp_apptrace_lock(esp_apptrace_tmo_t *tmo)
  357. {
  358. #if CONFIG_APPTRACE_LOCK_ENABLE
  359. esp_err_t ret = esp_apptrace_lock_take(&s_trace_buf.lock, tmo);
  360. if (ret != ESP_OK) {
  361. return ESP_FAIL;
  362. }
  363. #endif
  364. return ESP_OK;
  365. }
  366. esp_err_t esp_apptrace_unlock(void)
  367. {
  368. esp_err_t ret = ESP_OK;
  369. #if CONFIG_APPTRACE_LOCK_ENABLE
  370. ret = esp_apptrace_lock_give(&s_trace_buf.lock);
  371. #endif
  372. return ret;
  373. }
  374. #if CONFIG_APPTRACE_DEST_TRAX
  375. static inline void esp_apptrace_trax_select_memory_block(int block_num)
  376. {
  377. // select memory block to be exposed to the TRAX module (accessed by host)
  378. #if CONFIG_IDF_TARGET_ESP32
  379. DPORT_WRITE_PERI_REG(DPORT_TRACEMEM_MUX_MODE_REG, block_num ? TRACEMEM_MUX_BLK0_ONLY : TRACEMEM_MUX_BLK1_ONLY);
  380. #elif CONFIG_IDF_TARGET_ESP32S2
  381. DPORT_WRITE_PERI_REG(DPORT_PMS_OCCUPY_3_REG, block_num ? BIT(TRACEMEM_MUX_BLK0_NUM-4) : BIT(TRACEMEM_MUX_BLK1_NUM-4));
  382. #endif
  383. }
  384. static void esp_apptrace_trax_init(void)
  385. {
  386. // Stop trace, if any (on the current CPU)
  387. eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TRSTP);
  388. eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TMEN);
  389. eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(ESP_APPTRACE_TRAX_INBLOCK_START));
  390. // this is for OpenOCD to let him know where stub entries vector is resided
  391. // must be read by host before any transfer using TRAX
  392. eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0);
  393. ESP_APPTRACE_LOGI("Initialized TRAX on CPU%d", xPortGetCoreID());
  394. }
  395. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  396. // keep the size of buffered data for copying to TRAX mem block.
  397. // Only whole user blocks should be copied from buffer to TRAX block upon the switch
  398. static void esp_apptrace_trax_pend_chunk_sz_update(uint16_t size)
  399. {
  400. ESP_APPTRACE_LOGD("Update chunk enter %d/%d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz, size,
  401. 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);
  402. if ((uint32_t)s_trace_buf.trax.cur_pending_chunk_sz + (uint32_t)size <= ESP_APPTRACE_TRAX_BLOCK_SIZE) {
  403. ESP_APPTRACE_LOGD("Update chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
  404. s_trace_buf.trax.cur_pending_chunk_sz += size;
  405. } else {
  406. uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
  407. if (!chunk_sz) {
  408. assert(false && "Failed to alloc pended chunk sz slot!");
  409. } else {
  410. ESP_APPTRACE_LOGD("Update new chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
  411. *chunk_sz = s_trace_buf.trax.cur_pending_chunk_sz;
  412. s_trace_buf.trax.cur_pending_chunk_sz = size;
  413. }
  414. }
  415. }
  416. static uint16_t esp_apptrace_trax_pend_chunk_sz_get(void)
  417. {
  418. uint16_t ch_sz;
  419. ESP_APPTRACE_LOGD("Get chunk enter %d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz,
  420. 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);
  421. uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
  422. if (!chunk_sz) {
  423. ch_sz = s_trace_buf.trax.cur_pending_chunk_sz;
  424. s_trace_buf.trax.cur_pending_chunk_sz = 0;
  425. } else {
  426. ch_sz = *chunk_sz;
  427. }
  428. return ch_sz;
  429. }
  430. #endif
  431. // assumed to be protected by caller from multi-core/thread access
  432. static esp_err_t esp_apptrace_trax_block_switch(void)
  433. {
  434. int prev_block_num = s_trace_buf.trax.state.in_block % 2;
  435. int new_block_num = prev_block_num ? (0) : (1);
  436. int res = ESP_OK;
  437. extern uint32_t __esp_apptrace_trax_eri_updated;
  438. // indicate to host that we are about to update.
  439. // this is used only to place CPU into streaming mode at tracing startup
  440. // before starting streaming host can halt us after we read ESP_APPTRACE_TRAX_CTRL_REG and before we updated it
  441. // HACK: in this case host will set breakpoint just after ESP_APPTRACE_TRAX_CTRL_REG update,
  442. // here we set address to set bp at
  443. // enter ERI update critical section
  444. eri_write(ESP_APPTRACE_TRAX_STAT_REG, (uint32_t)&__esp_apptrace_trax_eri_updated);
  445. uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
  446. uint32_t host_connected = ESP_APPTRACE_TRAX_HOST_CONNECT & ctrl_reg;
  447. if (host_connected) {
  448. uint32_t acked_block = ESP_APPTRACE_TRAX_BLOCK_ID_GET(ctrl_reg);
  449. uint32_t host_to_read = ESP_APPTRACE_TRAX_BLOCK_LEN_GET(ctrl_reg);
  450. if (host_to_read != 0 || acked_block != (s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)) {
  451. ESP_APPTRACE_LOGD("HC[%d]: Can not switch %x %d %x %x/%lx, m %d", xPortGetCoreID(), ctrl_reg, host_to_read, acked_block,
  452. s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK, s_trace_buf.trax.state.in_block,
  453. s_trace_buf.trax.state.markers[prev_block_num]);
  454. res = ESP_ERR_NO_MEM;
  455. goto _on_func_exit;
  456. }
  457. }
  458. s_trace_buf.trax.state.markers[new_block_num] = 0;
  459. // switch to new block
  460. s_trace_buf.trax.state.in_block++;
  461. esp_apptrace_trax_select_memory_block(new_block_num);
  462. // handle data from host
  463. esp_hostdata_hdr_t *hdr = (esp_hostdata_hdr_t *)s_trace_buf.trax.blocks[new_block_num].start;
  464. if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA && hdr->block_sz > 0) {
  465. // TODO: add support for multiple blocks from host, currently there is no need for that
  466. uint8_t *p = s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.blocks[new_block_num].sz;
  467. 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,
  468. *(s_trace_buf.trax.blocks[new_block_num].start+0), *(s_trace_buf.trax.blocks[new_block_num].start+1),
  469. *(s_trace_buf.trax.blocks[new_block_num].start+2), *(s_trace_buf.trax.blocks[new_block_num].start+3),
  470. *(s_trace_buf.trax.blocks[new_block_num].start+4), *(s_trace_buf.trax.blocks[new_block_num].start+5),
  471. *(s_trace_buf.trax.blocks[new_block_num].start+6), *(s_trace_buf.trax.blocks[new_block_num].start+7),
  472. *(p-8), *(p-7), *(p-6), *(p-5), *(p-4), *(p-3), *(p-2), *(p-1));
  473. uint32_t sz = esp_apptrace_trax_down_buffer_write_nolock((uint8_t *)(hdr+1), hdr->block_sz);
  474. if (sz != hdr->block_sz) {
  475. ESP_APPTRACE_LOGE("Failed to write %d bytes to down buffer (%d %d)!", hdr->block_sz - sz, hdr->block_sz, sz);
  476. }
  477. hdr->block_sz = 0;
  478. }
  479. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  480. // copy pending data to TRAX block if any
  481. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  482. uint16_t max_chunk_sz = esp_apptrace_trax_pend_chunk_sz_get();
  483. #else
  484. uint16_t max_chunk_sz = s_trace_buf.trax.blocks[new_block_num].sz;
  485. #endif
  486. while (s_trace_buf.trax.state.markers[new_block_num] < max_chunk_sz) {
  487. uint32_t read_sz = esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend);
  488. if (read_sz == 0) {
  489. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  490. /* theres is a bug: esp_apptrace_trax_pend_chunk_sz_get returned wrong value,
  491. it must be greater or equal to one returned by esp_apptrace_rb_read_size_get */
  492. ESP_APPTRACE_LOGE("No pended bytes, must be > 0 and <= %d!", max_chunk_sz);
  493. #endif
  494. break;
  495. }
  496. if (read_sz > max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num]) {
  497. read_sz = max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num];
  498. }
  499. uint8_t *ptr = esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend, read_sz);
  500. if (!ptr) {
  501. assert(false && "Failed to consume pended bytes!!");
  502. break;
  503. }
  504. if (host_connected) {
  505. ESP_APPTRACE_LOGD("Pump %d pend bytes [%x %x %x %x : %x %x %x %x : %x %x %x %x : %x %x...%x %x]",
  506. read_sz, *(ptr+0), *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
  507. *(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));
  508. }
  509. memcpy(s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.state.markers[new_block_num], ptr, read_sz);
  510. s_trace_buf.trax.state.markers[new_block_num] += read_sz;
  511. }
  512. #endif
  513. eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(s_trace_buf.trax.state.in_block) |
  514. host_connected | ESP_APPTRACE_TRAX_BLOCK_LEN(s_trace_buf.trax.state.markers[prev_block_num]));
  515. _on_func_exit:
  516. // exit ERI update critical section
  517. eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0x0);
  518. // TODO: currently host sets breakpoint, use break instruction to stop;
  519. // it will allow to use ESP_APPTRACE_TRAX_STAT_REG for other purposes
  520. asm volatile (
  521. " .global __esp_apptrace_trax_eri_updated\n"
  522. "__esp_apptrace_trax_eri_updated:\n"); // host will set bp here to resolve collision at streaming start
  523. return res;
  524. }
  525. static esp_err_t esp_apptrace_trax_block_switch_waitus(esp_apptrace_tmo_t *tmo)
  526. {
  527. int res;
  528. while ((res = esp_apptrace_trax_block_switch()) != ESP_OK) {
  529. res = esp_apptrace_tmo_check(tmo);
  530. if (res != ESP_OK) {
  531. break;
  532. }
  533. }
  534. return res;
  535. }
  536. static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo)
  537. {
  538. uint8_t *ptr = NULL;
  539. int res = esp_apptrace_lock(tmo);
  540. if (res != ESP_OK) {
  541. return NULL;
  542. }
  543. while (1) {
  544. uint32_t sz = esp_apptrace_rb_read_size_get(&s_trace_buf.rb_down);
  545. if (sz != 0) {
  546. *size = MIN(*size, sz);
  547. ptr = esp_apptrace_rb_consume(&s_trace_buf.rb_down, *size);
  548. if (!ptr) {
  549. assert(false && "Failed to consume bytes from down buffer!");
  550. }
  551. break;
  552. }
  553. // may need to flush
  554. uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
  555. if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA) {
  556. ESP_APPTRACE_LOGD("force flush");
  557. res = esp_apptrace_trax_block_switch_waitus(tmo);
  558. if (res != ESP_OK) {
  559. ESP_APPTRACE_LOGE("Failed to switch to another block to recv data from host!");
  560. /*do not return error because data can be in down buffer already*/
  561. }
  562. } else {
  563. // check tmo only if there is no data from host
  564. res = esp_apptrace_tmo_check(tmo);
  565. if (res != ESP_OK) {
  566. return NULL;
  567. }
  568. }
  569. }
  570. if (esp_apptrace_unlock() != ESP_OK) {
  571. assert(false && "Failed to unlock apptrace data!");
  572. }
  573. return ptr;
  574. }
  575. static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
  576. {
  577. /* nothing todo */
  578. return ESP_OK;
  579. }
  580. static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size)
  581. {
  582. uint32_t total_sz = 0;
  583. while (total_sz < size) {
  584. 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,
  585. s_trace_buf.rb_down.cur_size, size);
  586. uint32_t wr_sz = esp_apptrace_rb_write_size_get(&s_trace_buf.rb_down);
  587. if (wr_sz == 0) {
  588. break;
  589. }
  590. if (wr_sz > size - total_sz) {
  591. wr_sz = size - total_sz;
  592. }
  593. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d", wr_sz);
  594. uint8_t *ptr = esp_apptrace_rb_produce(&s_trace_buf.rb_down, wr_sz);
  595. if (!ptr) {
  596. assert(false && "Failed to produce bytes to down buffer!");
  597. }
  598. 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);
  599. memcpy(ptr, data + total_sz, wr_sz);
  600. total_sz += wr_sz;
  601. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d/%d", wr_sz, total_sz);
  602. }
  603. return total_sz;
  604. }
  605. static inline uint8_t *esp_apptrace_data_header_init(uint8_t *ptr, uint16_t usr_size)
  606. {
  607. // it is safe to use xPortGetCoreID() in macro call because arg is used only once inside it
  608. ((esp_tracedata_hdr_t *)ptr)->block_sz = ESP_APPTRACE_USR_BLOCK_CORE(xPortGetCoreID()) | usr_size;
  609. ((esp_tracedata_hdr_t *)ptr)->wr_sz = 0;
  610. return ptr + sizeof(esp_tracedata_hdr_t);
  611. }
  612. static inline uint8_t *esp_apptrace_trax_wait4buf(uint16_t size, esp_apptrace_tmo_t *tmo, int *pended)
  613. {
  614. uint8_t *ptr = NULL;
  615. int res = esp_apptrace_trax_block_switch_waitus(tmo);
  616. if (res != ESP_OK) {
  617. return NULL;
  618. }
  619. // check if we still have pending data
  620. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  621. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  622. // if after TRAX block switch still have pending data (not all pending data have been pumped to TRAX block)
  623. // alloc new pending buffer
  624. *pended = 1;
  625. ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
  626. if (!ptr) {
  627. 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);
  628. }
  629. } else
  630. #endif
  631. {
  632. // update block pointers
  633. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + size > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
  634. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  635. *pended = 1;
  636. ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
  637. if (ptr == NULL) {
  638. 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);
  639. }
  640. #endif
  641. } else {
  642. *pended = 0;
  643. ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
  644. }
  645. }
  646. return ptr;
  647. }
  648. static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo)
  649. {
  650. uint8_t *buf_ptr = NULL;
  651. if (size > ESP_APPTRACE_USR_DATA_LEN_MAX) {
  652. ESP_APPTRACE_LOGE("Too large user data size %d!", size);
  653. return NULL;
  654. }
  655. int res = esp_apptrace_lock(tmo);
  656. if (res != ESP_OK) {
  657. return NULL;
  658. }
  659. // check for data in the pending buffer
  660. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  661. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  662. // if we have buffered data try to switch TRAX block
  663. esp_apptrace_trax_block_switch();
  664. // if switch was successful, part or all pended data have been copied to TRAX block
  665. }
  666. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  667. // if we have buffered data alloc new pending buffer
  668. ESP_APPTRACE_LOGD("Get %d bytes from PEND buffer", size);
  669. buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  670. if (buf_ptr == NULL) {
  671. int pended_buf;
  672. buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
  673. if (buf_ptr) {
  674. if (pended_buf) {
  675. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  676. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  677. #endif
  678. } else {
  679. ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
  680. // update cur block marker
  681. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  682. }
  683. }
  684. } else {
  685. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  686. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  687. #endif
  688. }
  689. } else
  690. #endif
  691. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + ESP_APPTRACE_USR_BLOCK_RAW_SZ(size) > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
  692. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  693. ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from PEND buffer", size);
  694. buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  695. if (buf_ptr) {
  696. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  697. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  698. #endif
  699. }
  700. #endif
  701. if (buf_ptr == NULL) {
  702. int pended_buf;
  703. ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from pend buffer", size);
  704. buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
  705. if (buf_ptr) {
  706. if (pended_buf) {
  707. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  708. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  709. #endif
  710. } else {
  711. ESP_APPTRACE_LOGD("Got %d bytes from TRAX buffer", size);
  712. // update cur block marker
  713. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  714. }
  715. }
  716. }
  717. } else {
  718. ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
  719. // fit to curr TRAX nlock
  720. buf_ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
  721. // update cur block marker
  722. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  723. }
  724. if (buf_ptr) {
  725. buf_ptr = esp_apptrace_data_header_init(buf_ptr, size);
  726. }
  727. // now we can safely unlock apptrace to allow other tasks/ISRs to get other buffers and write their data
  728. if (esp_apptrace_unlock() != ESP_OK) {
  729. assert(false && "Failed to unlock apptrace data!");
  730. }
  731. return buf_ptr;
  732. }
  733. static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
  734. {
  735. int res = ESP_OK;
  736. esp_tracedata_hdr_t *hdr = (esp_tracedata_hdr_t *)(ptr - sizeof(esp_tracedata_hdr_t));
  737. // update written size
  738. hdr->wr_sz = hdr->block_sz;
  739. // TODO: mark block as busy in order not to re-use it for other tracing calls until it is completely written
  740. // TODO: avoid potential situation when all memory is consumed by low prio tasks which can not complete writing due to
  741. // higher prio tasks and the latter can not allocate buffers at all
  742. // this is abnormal situation can be detected on host which will receive only uncompleted buffers
  743. // workaround: use own memcpy which will kick-off dead tracing calls
  744. return res;
  745. }
  746. static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo)
  747. {
  748. int res = ESP_OK;
  749. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() < min_sz) {
  750. ESP_APPTRACE_LOGI("Ignore flush request for min %d bytes. Bytes in TRAX block: %d.", min_sz, ESP_APPTRACE_TRAX_INBLOCK_MARKER());
  751. return ESP_OK;
  752. }
  753. // switch TRAX block while size of data is more than min size
  754. while (ESP_APPTRACE_TRAX_INBLOCK_MARKER() > 0) {
  755. ESP_APPTRACE_LOGD("Try to flush %d bytes. Wait until block switch for %u us", ESP_APPTRACE_TRAX_INBLOCK_MARKER(), tmo->tmo);
  756. res = esp_apptrace_trax_block_switch_waitus(tmo);
  757. if (res != ESP_OK) {
  758. ESP_APPTRACE_LOGE("Failed to switch to another block!");
  759. return res;
  760. }
  761. }
  762. return res;
  763. }
  764. static bool esp_apptrace_trax_host_is_connected(void)
  765. {
  766. return eri_read(ESP_APPTRACE_TRAX_CTRL_REG) & ESP_APPTRACE_TRAX_HOST_CONNECT ? true : false;
  767. }
  768. static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val)
  769. {
  770. eri_write(ESP_APPTRACE_TRAX_STAT_REG, val);
  771. return ESP_OK;
  772. }
  773. static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val)
  774. {
  775. *val = eri_read(ESP_APPTRACE_TRAX_STAT_REG);
  776. return ESP_OK;
  777. }
  778. static esp_err_t esp_apptrace_trax_dest_init(void)
  779. {
  780. for (int i = 0; i < ESP_APPTRACE_TRAX_BLOCKS_NUM; i++) {
  781. s_trace_buf.trax.blocks[i].start = (uint8_t *)s_trax_blocks[i];
  782. s_trace_buf.trax.blocks[i].sz = ESP_APPTRACE_TRAX_BLOCK_SIZE;
  783. s_trace_buf.trax.state.markers[i] = 0;
  784. }
  785. s_trace_buf.trax.state.in_block = ESP_APPTRACE_TRAX_INBLOCK_START;
  786. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  787. esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend, s_trace_buf.trax.pending_data,
  788. sizeof(s_trace_buf.trax.pending_data));
  789. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  790. s_trace_buf.trax.cur_pending_chunk_sz = 0;
  791. esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend_chunk_sz, (uint8_t *)s_trace_buf.trax.pending_chunk_sz,
  792. sizeof(s_trace_buf.trax.pending_chunk_sz));
  793. #endif
  794. #endif
  795. #if CONFIG_IDF_TARGET_ESP32
  796. DPORT_WRITE_PERI_REG(DPORT_PRO_TRACEMEM_ENA_REG, DPORT_PRO_TRACEMEM_ENA_M);
  797. #if CONFIG_FREERTOS_UNICORE == 0
  798. DPORT_WRITE_PERI_REG(DPORT_APP_TRACEMEM_ENA_REG, DPORT_APP_TRACEMEM_ENA_M);
  799. #endif
  800. #endif
  801. esp_apptrace_trax_select_memory_block(0);
  802. return ESP_OK;
  803. }
  804. #endif
  805. esp_err_t esp_apptrace_init(void)
  806. {
  807. int res;
  808. if (!s_trace_buf.inited) {
  809. memset(&s_trace_buf, 0, sizeof(s_trace_buf));
  810. // disabled by default
  811. esp_apptrace_rb_init(&s_trace_buf.rb_down, NULL, 0);
  812. res = esp_apptrace_lock_initialize(&s_trace_buf.lock);
  813. if (res != ESP_OK) {
  814. ESP_APPTRACE_LOGE("Failed to init log lock (%d)!", res);
  815. return res;
  816. }
  817. #if CONFIG_APPTRACE_DEST_TRAX
  818. res = esp_apptrace_trax_dest_init();
  819. if (res != ESP_OK) {
  820. ESP_APPTRACE_LOGE("Failed to init TRAX dest data (%d)!", res);
  821. esp_apptrace_lock_cleanup();
  822. return res;
  823. }
  824. #endif
  825. }
  826. #if CONFIG_APPTRACE_DEST_TRAX
  827. // init TRAX on this CPU
  828. esp_apptrace_trax_init();
  829. #endif
  830. s_trace_buf.inited |= 1 << xPortGetCoreID(); // global and this CPU-specific data are inited
  831. return ESP_OK;
  832. }
  833. void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)
  834. {
  835. esp_apptrace_rb_init(&s_trace_buf.rb_down, buf, size);
  836. }
  837. esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *buf, uint32_t *size, uint32_t user_tmo)
  838. {
  839. int res = ESP_OK;
  840. esp_apptrace_tmo_t tmo;
  841. esp_apptrace_hw_t *hw = NULL;
  842. if (dest == ESP_APPTRACE_DEST_TRAX) {
  843. #if CONFIG_APPTRACE_DEST_TRAX
  844. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  845. #else
  846. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  847. return ESP_ERR_NOT_SUPPORTED;
  848. #endif
  849. } else {
  850. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  851. return ESP_ERR_NOT_SUPPORTED;
  852. }
  853. if (buf == NULL || size == NULL || *size == 0) {
  854. return ESP_ERR_INVALID_ARG;
  855. }
  856. //TODO: callback system
  857. esp_apptrace_tmo_init(&tmo, user_tmo);
  858. uint32_t act_sz = *size;
  859. *size = 0;
  860. uint8_t * ptr = hw->get_down_buffer(&act_sz, &tmo);
  861. if (ptr && act_sz > 0) {
  862. ESP_APPTRACE_LOGD("Read %d bytes from host", act_sz);
  863. memcpy(buf, ptr, act_sz);
  864. res = hw->put_down_buffer(ptr, &tmo);
  865. *size = act_sz;
  866. } else {
  867. res = ESP_ERR_TIMEOUT;
  868. }
  869. return res;
  870. }
  871. uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t user_tmo)
  872. {
  873. esp_apptrace_tmo_t tmo;
  874. esp_apptrace_hw_t *hw = NULL;
  875. if (dest == ESP_APPTRACE_DEST_TRAX) {
  876. #if CONFIG_APPTRACE_DEST_TRAX
  877. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  878. #else
  879. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  880. return NULL;
  881. #endif
  882. } else {
  883. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  884. return NULL;
  885. }
  886. if (size == NULL || *size == 0) {
  887. return NULL;
  888. }
  889. esp_apptrace_tmo_init(&tmo, user_tmo);
  890. return hw->get_down_buffer(size, &tmo);
  891. }
  892. esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  893. {
  894. esp_apptrace_tmo_t tmo;
  895. esp_apptrace_hw_t *hw = NULL;
  896. if (dest == ESP_APPTRACE_DEST_TRAX) {
  897. #if CONFIG_APPTRACE_DEST_TRAX
  898. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  899. #else
  900. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  901. return ESP_ERR_NOT_SUPPORTED;
  902. #endif
  903. } else {
  904. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  905. return ESP_ERR_NOT_SUPPORTED;
  906. }
  907. if (ptr == NULL) {
  908. return ESP_ERR_INVALID_ARG;
  909. }
  910. esp_apptrace_tmo_init(&tmo, user_tmo);
  911. return hw->put_down_buffer(ptr, &tmo);
  912. }
  913. esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t user_tmo)
  914. {
  915. uint8_t *ptr = NULL;
  916. esp_apptrace_tmo_t tmo;
  917. esp_apptrace_hw_t *hw = NULL;
  918. if (dest == ESP_APPTRACE_DEST_TRAX) {
  919. #if CONFIG_APPTRACE_DEST_TRAX
  920. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  921. #else
  922. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  923. return ESP_ERR_NOT_SUPPORTED;
  924. #endif
  925. } else {
  926. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  927. return ESP_ERR_NOT_SUPPORTED;
  928. }
  929. if (data == NULL || size == 0) {
  930. return ESP_ERR_INVALID_ARG;
  931. }
  932. esp_apptrace_tmo_init(&tmo, user_tmo);
  933. ptr = hw->get_up_buffer(size, &tmo);
  934. if (ptr == NULL) {
  935. return ESP_ERR_NO_MEM;
  936. }
  937. // actually can be suspended here by higher prio tasks/ISRs
  938. //TODO: use own memcpy with dead trace calls kick-off algo and tmo expiration check
  939. memcpy(ptr, data, size);
  940. // now indicate that this buffer is ready to be sent off to host
  941. return hw->put_up_buffer(ptr, &tmo);
  942. }
  943. int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t user_tmo, const char *fmt, va_list ap)
  944. {
  945. uint16_t nargs = 0;
  946. uint8_t *pout, *p = (uint8_t *)fmt;
  947. esp_apptrace_tmo_t tmo;
  948. esp_apptrace_hw_t *hw = NULL;
  949. if (dest == ESP_APPTRACE_DEST_TRAX) {
  950. #if CONFIG_APPTRACE_DEST_TRAX
  951. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  952. #else
  953. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  954. return ESP_ERR_NOT_SUPPORTED;
  955. #endif
  956. } else {
  957. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  958. return ESP_ERR_NOT_SUPPORTED;
  959. }
  960. if (fmt == NULL) {
  961. return ESP_ERR_INVALID_ARG;
  962. }
  963. esp_apptrace_tmo_init(&tmo, user_tmo);
  964. ESP_APPTRACE_LOGD("fmt %x", fmt);
  965. while ((p = (uint8_t *)strchr((char *)p, '%')) && nargs < ESP_APPTRACE_MAX_VPRINTF_ARGS) {
  966. p++;
  967. if (*p != '%' && *p != 0) {
  968. nargs++;
  969. }
  970. }
  971. ESP_APPTRACE_LOGD("nargs = %d", nargs);
  972. if (p) {
  973. ESP_APPTRACE_LOGE("Failed to store all printf args!");
  974. }
  975. pout = hw->get_up_buffer(1 + sizeof(char *) + nargs * sizeof(uint32_t), &tmo);
  976. if (pout == NULL) {
  977. ESP_APPTRACE_LOGE("Failed to get buffer!");
  978. return -1;
  979. }
  980. p = pout;
  981. *pout = nargs;
  982. pout++;
  983. *(const char **)pout = fmt;
  984. pout += sizeof(char *);
  985. while (nargs-- > 0) {
  986. uint32_t arg = va_arg(ap, uint32_t);
  987. *(uint32_t *)pout = arg;
  988. pout += sizeof(uint32_t);
  989. ESP_APPTRACE_LOGD("arg %x", arg);
  990. }
  991. int ret = hw->put_up_buffer(p, &tmo);
  992. if (ret != ESP_OK) {
  993. ESP_APPTRACE_LOGE("Failed to put printf buf (%d)!", ret);
  994. return -1;
  995. }
  996. return (pout - p);
  997. }
  998. int esp_apptrace_vprintf(const char *fmt, va_list ap)
  999. {
  1000. return esp_apptrace_vprintf_to(ESP_APPTRACE_DEST_TRAX, /*ESP_APPTRACE_TMO_INFINITE*/0, fmt, ap);
  1001. }
  1002. uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t user_tmo)
  1003. {
  1004. esp_apptrace_tmo_t tmo;
  1005. esp_apptrace_hw_t *hw = NULL;
  1006. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1007. #if CONFIG_APPTRACE_DEST_TRAX
  1008. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1009. #else
  1010. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1011. return NULL;
  1012. #endif
  1013. } else {
  1014. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1015. return NULL;
  1016. }
  1017. if (size == 0) {
  1018. return NULL;
  1019. }
  1020. esp_apptrace_tmo_init(&tmo, user_tmo);
  1021. return hw->get_up_buffer(size, &tmo);
  1022. }
  1023. esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  1024. {
  1025. esp_apptrace_tmo_t tmo;
  1026. esp_apptrace_hw_t *hw = NULL;
  1027. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1028. #if CONFIG_APPTRACE_DEST_TRAX
  1029. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1030. #else
  1031. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1032. return ESP_ERR_NOT_SUPPORTED;
  1033. #endif
  1034. } else {
  1035. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1036. return ESP_ERR_NOT_SUPPORTED;
  1037. }
  1038. if (ptr == NULL) {
  1039. return ESP_ERR_INVALID_ARG;
  1040. }
  1041. esp_apptrace_tmo_init(&tmo, user_tmo);
  1042. return hw->put_up_buffer(ptr, &tmo);
  1043. }
  1044. esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t usr_tmo)
  1045. {
  1046. esp_apptrace_tmo_t tmo;
  1047. esp_apptrace_hw_t *hw = NULL;
  1048. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1049. #if CONFIG_APPTRACE_DEST_TRAX
  1050. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1051. #else
  1052. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1053. return ESP_ERR_NOT_SUPPORTED;
  1054. #endif
  1055. } else {
  1056. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1057. return ESP_ERR_NOT_SUPPORTED;
  1058. }
  1059. esp_apptrace_tmo_init(&tmo, usr_tmo);
  1060. return hw->flush_up_buffer(min_sz, &tmo);
  1061. }
  1062. esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t usr_tmo)
  1063. {
  1064. int res;
  1065. esp_apptrace_tmo_t tmo;
  1066. esp_apptrace_tmo_init(&tmo, usr_tmo);
  1067. res = esp_apptrace_lock(&tmo);
  1068. if (res != ESP_OK) {
  1069. ESP_APPTRACE_LOGE("Failed to lock apptrace data (%d)!", res);
  1070. return res;
  1071. }
  1072. res = esp_apptrace_flush_nolock(dest, 0, esp_apptrace_tmo_remaining_us(&tmo));
  1073. if (res != ESP_OK) {
  1074. ESP_APPTRACE_LOGE("Failed to flush apptrace data (%d)!", res);
  1075. }
  1076. if (esp_apptrace_unlock() != ESP_OK) {
  1077. assert(false && "Failed to unlock apptrace data!");
  1078. }
  1079. return res;
  1080. }
  1081. bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)
  1082. {
  1083. esp_apptrace_hw_t *hw = NULL;
  1084. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1085. #if CONFIG_APPTRACE_DEST_TRAX
  1086. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1087. #else
  1088. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1089. return false;
  1090. #endif
  1091. } else {
  1092. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1093. return false;
  1094. }
  1095. return hw->host_is_connected();
  1096. }
  1097. esp_err_t esp_apptrace_status_reg_set(esp_apptrace_dest_t dest, uint32_t val)
  1098. {
  1099. esp_apptrace_hw_t *hw = NULL;
  1100. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1101. #if CONFIG_APPTRACE_DEST_TRAX
  1102. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1103. #else
  1104. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1105. return ESP_ERR_NOT_SUPPORTED;
  1106. #endif
  1107. } else {
  1108. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1109. return ESP_ERR_NOT_SUPPORTED;
  1110. }
  1111. return hw->status_reg_set(val);
  1112. }
  1113. esp_err_t esp_apptrace_status_reg_get(esp_apptrace_dest_t dest, uint32_t *val)
  1114. {
  1115. esp_apptrace_hw_t *hw = NULL;
  1116. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1117. #if CONFIG_APPTRACE_DEST_TRAX
  1118. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1119. #else
  1120. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1121. return ESP_ERR_NOT_SUPPORTED;
  1122. #endif
  1123. } else {
  1124. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1125. return ESP_ERR_NOT_SUPPORTED;
  1126. }
  1127. return hw->status_reg_get(val);
  1128. }
  1129. #endif