app_trace.c 54 KB

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