app_trace.c 52 KB

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