app_trace.c 54 KB

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