# Application Level Tracing

[中文]

## Overview

ESP-IDF provides a useful feature for application behavior analysis called Application Level Tracing. The feature can be enabled in menuconfig and allows transfer of arbitrary data between the host and ESP32-P4 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 types of information in the opposite direction at runtime. The main use cases of this library are:

1. Collecting application specific data, see Application Specific Tracing.

2. Lightweight logging to the host, see Logging to Host.

3. System behaviour analysis, see System Behavior Analysis with SEGGER SystemView.

## Application Examples

- system/app_trace_to_plot demonstrates how to use the Application Level Tracing Library to send and plot dummy sensor data to a host via JTAG, providing a faster alternative to logging via UART.

- system/app_trace_basic demonstrates how to use the Application Level Tracing Library to log messages to a host via JTAG, providing a faster alternative to UART logs.

## API Reference

### Header File

- components/app_trace/include/esp_app_trace.h

- This header file can be included with:

  >     #include "esp_app_trace.h"

- This header file is a part of the API provided by the `app_trace` component. To declare that your component depends on `app_trace`, add the following to your CMakeLists.txt:

  >     REQUIRES app_trace

  > or

  >     PRIV_REQUIRES app_trace

### Functions

esp_err_t esp_apptrace_init(const esp_apptrace_config_t *config)

  Initializes application tracing module for the selected destination and configuration.

  Note

  Should be called before any esp_apptrace_xxx call.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

esp_err_t esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)

  Configures down buffer.

  Note

  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.

  Parameters:

    - buf -- Address of buffer to use for down channel (host to target) data.

    - size -- Size of the buffer.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

uint8_t *esp_apptrace_buffer_get(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.

  Parameters:

    - size -- Size of data to write to trace buffer.

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

  Returns:

    non-NULL on success, otherwise NULL.

esp_err_t esp_apptrace_buffer_put(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.

  Parameters:

    - 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.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

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

  Writes data to trace buffer.

  Parameters:

    - 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.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

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

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

  Parameters:

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

    - fmt -- Address of format string.

    - ap -- List of arguments.

  Returns:

    Number of bytes written.

int esp_apptrace_vprintf(const char *fmt, va_list ap)

  vprintf-like function to send log messages to host.

  Parameters:

    - fmt -- Address of format string.

    - ap -- List of arguments.

  Returns:

    Number of bytes written.

esp_err_t esp_apptrace_flush(uint32_t tmo)

  Flushes remaining data in trace buffer to host.

  Parameters:

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

  Returns:

    ESP_OK on success, otherwise see esp_err_t

esp_err_t esp_apptrace_flush_nolock(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.

  Parameters:

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

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

  Returns:

    ESP_OK on success, otherwise see esp_err_t

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

  Reads host data from trace buffer.

  Parameters:

    - 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.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

uint8_t *esp_apptrace_down_buffer_get(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.

  Parameters:

    - 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.

  Returns:

    non-NULL on success, otherwise NULL.

esp_err_t esp_apptrace_down_buffer_put(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.

  Parameters:

    - 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.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

bool esp_apptrace_host_is_connected(void)

  Checks whether host is connected.

  Returns:

    true if host is connected, otherwise false

esp_apptrace_dest_t esp_apptrace_get_destination(void)

  Gets the destination of the application trace.

  Returns:

    The destination of the application trace.

esp_err_t esp_apptrace_set_header_size(esp_apptrace_header_size_t header_size)

  Sets the header size of the application trace packet.

  Parameters:

    header_size -- The header size to set.

  Returns:

    ESP_OK on success, otherwise see esp_err_t

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

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

  Parameters:

    - path -- Path to file.

    - mode -- Mode string. See fopen for details.

  Returns:

    non zero file handle on success, otherwise 0

int esp_apptrace_fclose(void *stream)

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

  Parameters:

    stream -- File handle returned by esp_apptrace_fopen.

  Returns:

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

size_t esp_apptrace_fwrite(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.

  Parameters:

    - 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.

  Returns:

    Number of written items. See fwrite for details.

size_t esp_apptrace_fread(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.

  Parameters:

    - 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.

  Returns:

    Number of read items. See fread for details.

int esp_apptrace_fseek(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.

  Parameters:

    - stream -- File handle returned by esp_apptrace_fopen.

    - offset -- Offset. See fseek for details.

    - whence -- Position in file. See fseek for details.

  Returns:

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

int esp_apptrace_ftell(void *stream)

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

  Parameters:

    stream -- File handle returned by esp_apptrace_fopen.

  Returns:

    Current position in file. See ftell for details.

int esp_apptrace_fstop(void)

  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.).

  Returns:

    ESP_OK on success, otherwise see esp_err_t

int esp_apptrace_feof(void *stream)

  Test end-of-file indicator on a stream. This function has the same semantic as 'feof' except for the first argument.

  Parameters:

    stream -- File handle returned by esp_apptrace_fopen.

  Returns:

    Non-Zero if end-of-file indicator is set for stream. See feof for details.

### Macros

APPTRACE_JTAG_CONFIG_DEFAULT()

APPTRACE_UART_CONFIG_DEFAULT()

APPTRACE_CONFIG_DEFAULT()

Was this page helpful?

- Thank you! We received your feedback.
- If you have any comments, fill in Espressif Documentation Feedback Form.

- We value your feedback.
- Let us know how we can improve this page by filling in Espressif Documentation Feedback Form.