Application Level Tracing

Overview

IDF provides a useful feature for program behavior analysis called Application Level Tracing. The feature can be enabled in menuconfig and allows transfer of arbitrary data between the host and ESP32 via JTAG interface with minimal overhead on program execution. Developers can use this library to send application specific state of execution to the host and receive commands or other type of info in the opposite direction at runtime. The main use cases of this library are:

  1. Collecting application specific data, see 特定应用程序的跟踪

  2. Lightweight logging to the host, see 记录日志到主机

  3. System behaviour analysis, see 基于 SEGGER SystemView 的系统行为分析

API Reference

Functions

esp_err_t esp_apptrace_init(void)

Initializes application tracing module.

备注

Should be called before any esp_apptrace_xxx call.

返回

ESP_OK on success, otherwise see esp_err_t

void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)

Configures down buffer.

备注

Needs to be called before attempting to receive any data using esp_apptrace_down_buffer_get and esp_apptrace_read. This function does not protect internal data by lock.

参数
  • buf – Address of buffer to use for down channel (host to target) data.

  • size – Size of the buffer.

uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t tmo)

Allocates buffer for trace data. Once the data in the buffer is ready to be sent, esp_apptrace_buffer_put must be called to indicate it.

参数
  • dest – Indicates HW interface to send data.

  • size – Size of data to write to trace buffer.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

non-NULL on success, otherwise NULL.

esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t tmo)

Indicates that the data in the buffer is ready to be sent. This function is a counterpart of and must be preceded by esp_apptrace_buffer_get.

参数
  • dest – Indicates HW interface to send data. Should be identical to the same parameter in call to esp_apptrace_buffer_get.

  • ptr – Address of trace buffer to release. Should be the value returned by call to esp_apptrace_buffer_get.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

ESP_OK on success, otherwise see esp_err_t

esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t tmo)

Writes data to trace buffer.

参数
  • dest – Indicates HW interface to send data.

  • data – Address of data to write to trace buffer.

  • size – Size of data to write to trace buffer.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

ESP_OK on success, otherwise see esp_err_t

int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t tmo, const char *fmt, va_list ap)

vprintf-like function to send log messages to host via specified HW interface.

参数
  • dest – Indicates HW interface to send data.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

  • fmt – Address of format string.

  • ap – List of arguments.

返回

Number of bytes written.

int esp_apptrace_vprintf(const char *fmt, va_list ap)

vprintf-like function to send log messages to host.

参数
  • fmt – Address of format string.

  • ap – List of arguments.

返回

Number of bytes written.

esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t tmo)

Flushes remaining data in trace buffer to host.

参数
  • dest – Indicates HW interface to flush data on.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

ESP_OK on success, otherwise see esp_err_t

esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t tmo)

Flushes remaining data in trace buffer to host without locking internal data. This is a special version of esp_apptrace_flush which should be called from panic handler.

参数
  • dest – Indicates HW interface to flush data on.

  • min_sz – Threshold for flushing data. If current filling level is above this value, data will be flushed. TRAX destinations only.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

ESP_OK on success, otherwise see esp_err_t

esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *data, uint32_t *size, uint32_t tmo)

Reads host data from trace buffer.

参数
  • dest – Indicates HW interface to read the data on.

  • data – Address of buffer to put data from trace buffer.

  • size – Pointer to store size of read data. Before call to this function pointed memory must hold requested size of data

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

ESP_OK on success, otherwise see esp_err_t

uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t tmo)

Retrieves incoming data buffer if any. Once data in the buffer is processed, esp_apptrace_down_buffer_put must be called to indicate it.

参数
  • dest – Indicates HW interface to receive data.

  • size – Address to store size of available data in down buffer. Must be initialized with requested value.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

non-NULL on success, otherwise NULL.

esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t tmo)

Indicates that the data in the down buffer is processed. This function is a counterpart of and must be preceded by esp_apptrace_down_buffer_get.

参数
  • dest – Indicates HW interface to receive data. Should be identical to the same parameter in call to esp_apptrace_down_buffer_get.

  • ptr – Address of trace buffer to release. Should be the value returned by call to esp_apptrace_down_buffer_get.

  • tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinitely.

返回

ESP_OK on success, otherwise see esp_err_t

bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)

Checks whether host is connected.

参数

dest – Indicates HW interface to use.

返回

true if host is connected, otherwise false

void *esp_apptrace_fopen(esp_apptrace_dest_t dest, const char *path, const char *mode)

Opens file on host. This function has the same semantic as ‘fopen’ except for the first argument.

参数
  • dest – Indicates HW interface to use.

  • path – Path to file.

  • mode – Mode string. See fopen for details.

返回

non zero file handle on success, otherwise 0

int esp_apptrace_fclose(esp_apptrace_dest_t dest, void *stream)

Closes file on host. This function has the same semantic as ‘fclose’ except for the first argument.

参数
  • dest – Indicates HW interface to use.

  • stream – File handle returned by esp_apptrace_fopen.

返回

Zero on success, otherwise non-zero. See fclose for details.

size_t esp_apptrace_fwrite(esp_apptrace_dest_t dest, const void *ptr, size_t size, size_t nmemb, void *stream)

Writes to file on host. This function has the same semantic as ‘fwrite’ except for the first argument.

参数
  • dest – Indicates HW interface to use.

  • ptr – Address of data to write.

  • size – Size of an item.

  • nmemb – Number of items to write.

  • stream – File handle returned by esp_apptrace_fopen.

返回

Number of written items. See fwrite for details.

size_t esp_apptrace_fread(esp_apptrace_dest_t dest, void *ptr, size_t size, size_t nmemb, void *stream)

Read file on host. This function has the same semantic as ‘fread’ except for the first argument.

参数
  • dest – Indicates HW interface to use.

  • ptr – Address to store read data.

  • size – Size of an item.

  • nmemb – Number of items to read.

  • stream – File handle returned by esp_apptrace_fopen.

返回

Number of read items. See fread for details.

int esp_apptrace_fseek(esp_apptrace_dest_t dest, void *stream, long offset, int whence)

Set position indicator in file on host. This function has the same semantic as ‘fseek’ except for the first argument.

参数
  • dest – Indicates HW interface to use.

  • stream – File handle returned by esp_apptrace_fopen.

  • offset – Offset. See fseek for details.

  • whence – Position in file. See fseek for details.

返回

Zero on success, otherwise non-zero. See fseek for details.

int esp_apptrace_ftell(esp_apptrace_dest_t dest, void *stream)

Get current position indicator for file on host. This function has the same semantic as ‘ftell’ except for the first argument.

参数
  • dest – Indicates HW interface to use.

  • stream – File handle returned by esp_apptrace_fopen.

返回

Current position in file. See ftell for details.

int esp_apptrace_fstop(esp_apptrace_dest_t dest)

Indicates to the host that all file operations are complete. This function should be called after all file operations are finished and indicate to the host that it can perform cleanup operations (close open files etc.).

参数

dest – Indicates HW interface to use.

返回

ESP_OK on success, otherwise see esp_err_t

void esp_gcov_dump(void)

Triggers gcov info dump. This function waits for the host to connect to target before dumping data.

Enumerations

enum esp_apptrace_dest_t

Application trace data destinations bits.

Values:

enumerator ESP_APPTRACE_DEST_JTAG

JTAG destination.

enumerator ESP_APPTRACE_DEST_TRAX

xxx_TRAX name is obsolete, use more common xxx_JTAG

enumerator ESP_APPTRACE_DEST_UART

UART destination.

enumerator ESP_APPTRACE_DEST_MAX
enumerator ESP_APPTRACE_DEST_NUM

Functions

static inline esp_err_t esp_sysview_flush(uint32_t tmo)

Flushes remaining data in SystemView trace buffer to host.

参数

tmo – Timeout for operation (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinetly.

返回

ESP_OK.

int esp_sysview_vprintf(const char *format, va_list args)

vprintf-like function to sent log messages to the host.

参数
  • format – Address of format string.

  • args – List of arguments.

返回

Number of bytes written.

esp_err_t esp_sysview_heap_trace_start(uint32_t tmo)

Starts SystemView heap tracing.

参数

tmo – Timeout (in us) to wait for the host to be connected. Use -1 to wait forever.

返回

ESP_OK on success, ESP_ERR_TIMEOUT if operation has been timed out.

esp_err_t esp_sysview_heap_trace_stop(void)

Stops SystemView heap tracing.

返回

ESP_OK.

void esp_sysview_heap_trace_alloc(void *addr, uint32_t size, const void *callers)

Sends heap allocation event to the host.

参数
  • addr – Address of allocated block.

  • size – Size of allocated block.

  • callers – Pointer to array with callstack addresses. Array size must be CONFIG_HEAP_TRACING_STACK_DEPTH.

void esp_sysview_heap_trace_free(void *addr, const void *callers)

Sends heap de-allocation event to the host.

参数
  • addr – Address of de-allocated block.

  • callers – Pointer to array with callstack addresses. Array size must be CONFIG_HEAP_TRACING_STACK_DEPTH.