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