app_trace.c 52 KB

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