|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * SystemView backend for esp_trace compiler-instrumented function tracing. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <stdbool.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include "esp_cpu.h" |
| 14 | +#include "esp_trace_port_encoder.h" |
| 15 | +#include "adapter_encoder_sysview.h" |
| 16 | +#include "SEGGER_SYSVIEW.h" |
| 17 | + |
| 18 | +/* Event IDs sent as EventOffset + id. EventOffset is allocated in registration order starting at 512. */ |
| 19 | +enum { |
| 20 | + FT_EVT_ENTER = 0, |
| 21 | + FT_EVT_EXIT, |
| 22 | + FT_EVT_COUNT, |
| 23 | +}; |
| 24 | + |
| 25 | +static SEGGER_SYSVIEW_MODULE s_ft_module = { |
| 26 | + .sModule = |
| 27 | + "M=ESP_FunctionTrace, " |
| 28 | + "0 function_enter func=%p call_site=%p, " |
| 29 | + "1 function_exit func=%p call_site=%p", |
| 30 | + .NumEvents = FT_EVT_COUNT, |
| 31 | +}; |
| 32 | + |
| 33 | +static bool s_registered; |
| 34 | + |
| 35 | +static inline bool should_drop_for_cpu(struct esp_trace_encoder *enc) |
| 36 | +{ |
| 37 | + sysview_encoder_ctx_t *ctx = enc->ctx; |
| 38 | + return ctx && ctx->filter_by_cpu && (esp_cpu_get_core_id() != ctx->dest_cpu); |
| 39 | +} |
| 40 | + |
| 41 | +void esp_sysview_function_trace_register(void) |
| 42 | +{ |
| 43 | + if (!s_registered) { |
| 44 | + SEGGER_SYSVIEW_RegisterModule(&s_ft_module); |
| 45 | + s_registered = true; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +void esp_sysview_function_enter(struct esp_trace_encoder *enc, void *func, void *call_site) |
| 50 | +{ |
| 51 | + if (!s_registered || should_drop_for_cpu(enc)) { |
| 52 | + return; |
| 53 | + } |
| 54 | + U8 aPacket[SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32]; |
| 55 | + U8 *pPayload = SEGGER_SYSVIEW_PREPARE_PACKET(aPacket); |
| 56 | + pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, (U32)(uintptr_t)func); |
| 57 | + pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, (U32)(uintptr_t)call_site); |
| 58 | + SEGGER_SYSVIEW_SendPacket(aPacket, pPayload, s_ft_module.EventOffset + FT_EVT_ENTER); |
| 59 | +} |
| 60 | + |
| 61 | +void esp_sysview_function_exit(struct esp_trace_encoder *enc, void *func, void *call_site) |
| 62 | +{ |
| 63 | + if (!s_registered || should_drop_for_cpu(enc)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + U8 aPacket[SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32]; |
| 67 | + U8 *pPayload = SEGGER_SYSVIEW_PREPARE_PACKET(aPacket); |
| 68 | + pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, (U32)(uintptr_t)func); |
| 69 | + pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, (U32)(uintptr_t)call_site); |
| 70 | + SEGGER_SYSVIEW_SendPacket(aPacket, pPayload, s_ft_module.EventOffset + FT_EVT_EXIT); |
| 71 | +} |
0 commit comments