app_trace.c 54 KB

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