Skip to content

add k_sleep tracing in CTF, add ram tracing snippet #93327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions snippets/ram-tracing/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.. _snippet-ram-tracing:

RAM Tracing Snippet (ram-tracing)
#################################

.. code-block:: console

west build -S ram-tracing [...]

Overview
********

This snippet enables CTF Tracing using the RAM backend.

After connecting to the target in GDB, you can dump the RAM tracing data to a
binary file:

.. code-block:: console

(gdb) dump binary memory ram_tracing.bin ram_tracing ram_tracing+8192

Using `Babeltrace`_, you can view the binary file as a CTF trace:

.. code-block:: console

[19:00:00.023530800] (+?.?????????) thread_create: { thread_id = 2147621216, name = "unknown" }
[19:00:00.023567100] (+0.000036300) thread_info: { thread_id = 2147621216, name = "unknown", stack_base = 2147705856, stack_size = 3072 }
[19:00:00.023673700] (+0.000106600) thread_name_set: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.023731100] (+0.000057400) thread_wakeup: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.023827500] (+0.000096400) thread_switched_out: { thread_id = 2147621968, name = "main" }
[19:00:00.023904500] (+0.000077000) thread_switched_in: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.024807600] (+0.000903100) thread_create: { thread_id = 2147619320, name = "unknown" }
[19:00:00.024843800] (+0.000036200) thread_info: { thread_id = 2147619320, name = "unknown", stack_base = 2147693568, stack_size = 3072 }
[19:00:00.024898700] (+0.000054900) thread_wakeup: { thread_id = 2147619320, name = "unknown" }
[19:00:00.025020000] (+0.000121300) thread_switched_out: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.025130700] (+0.000110700) thread_switched_in: { thread_id = 2147621968, name = "main" }
[19:00:00.025249900] (+0.000119200) thread_switched_out: { thread_id = 2147621968, name = "main" }
[19:00:00.025353500] (+0.000103600) thread_switched_in: { thread_id = 2147619320, name = "unknown" }
[19:00:00.026049900] (+0.000696400) thread_switched_in: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.026759200] (+0.000709300) semaphore_give_enter: { id = 2147735664 }
[19:00:00.026773600] (+0.000014400) semaphore_give_exit: { id = 2147735664 }
[19:00:00.027346600] (+0.000573000) thread_switched_out: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.027457400] (+0.000110800) thread_switched_in: { thread_id = 2147621968, name = "main" }
[19:00:00.029552800] (+0.002095400) thread_create: { thread_id = 2147621216, name = "unknown" }
[19:00:00.029589000] (+0.000036200) thread_info: { thread_id = 2147621216, name = "unknown", stack_base = 2147705856, stack_size = 3072 }
[19:00:00.029697400] (+0.000108400) thread_name_set: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.029754800] (+0.000057400) thread_wakeup: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.029851200] (+0.000096400) thread_switched_out: { thread_id = 2147621968, name = "main" }
[19:00:00.029928200] (+0.000077000) thread_switched_in: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.029968400] (+0.000040200) semaphore_init: { id = 2147623436, ret = 0 }
[19:00:00.030851900] (+0.000883500) thread_create: { thread_id = 2147619320, name = "unknown" }
[19:00:00.030888100] (+0.000036200) thread_info: { thread_id = 2147619320, name = "unknown", stack_base = 2147693568, stack_size = 3072 }
[19:00:00.030943000] (+0.000054900) thread_wakeup: { thread_id = 2147619320, name = "unknown" }
[19:00:00.030989600] (+0.000046600) semaphore_take_enter: { id = 2147623436, timeout = 4294957296 }
[19:00:00.031005800] (+0.000016200) semaphore_take_blocking: { id = 2147623436, timeout = 4294957296 }
[19:00:00.031099600] (+0.000093800) thread_switched_out: { thread_id = 2147621216, name = "test_abort_from_isr" }
[19:00:00.031210300] (+0.000110700) thread_switched_in: { thread_id = 2147621968, name = "main" }
[19:00:00.031329500] (+0.000119200) thread_switched_out: { thread_id = 2147621968, name = "main" }
[19:00:00.031433100] (+0.000103600) thread_switched_in: { thread_id = 2147619320, name = "unknown" }
[19:00:00.031469100] (+0.000036000) semaphore_give_enter: { id = 2147623436 }

References
**********

.. target-notes::

.. _Babeltrace: https://babeltrace.org/
12 changes: 12 additions & 0 deletions snippets/ram-tracing/ram-tracing.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright Zephyr Project Contributors
# SPDX-License-Identifier: Apache-2.0


CONFIG_DEBUG_OPTIMIZATIONS=n
CONFIG_DEBUG_THREAD_INFO=y

CONFIG_TRACING=y
CONFIG_TRACING_CTF=y
CONFIG_TRACING_BACKEND_RAM=y
CONFIG_RAM_TRACING_BUFFER_SIZE=8192
CONFIG_TRACING_SYNC=y
6 changes: 6 additions & 0 deletions snippets/ram-tracing/snippet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright Zephyr Project Contributors
# SPDX-License-Identifier: Apache-2.0

name: ram-tracing
append:
EXTRA_CONF_FILE: ram-tracing.conf
15 changes: 15 additions & 0 deletions subsys/tracing/ctf/ctf_top.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ void sys_trace_k_thread_priority_set(struct k_thread *thread)
thread->base.prio, name);
}

void sys_trace_k_thread_sleep_enter(k_timeout_t timeout)
{
ctf_top_thread_sleep_enter(
k_ticks_to_us_floor32((uint32_t)timeout.ticks)
);
}

void sys_trace_k_thread_sleep_exit(k_timeout_t timeout, int ret)
{
ctf_top_thread_sleep_exit(
k_ticks_to_us_floor32((uint32_t)timeout.ticks),
(uint32_t)ret
);
}

void sys_trace_k_thread_create(struct k_thread *thread, size_t stack_size, int prio)
{
ctf_bounded_string_t name = { "unknown" };
Expand Down
12 changes: 12 additions & 0 deletions subsys/tracing/ctf/ctf_top.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ typedef enum {
CTF_EVENT_GPIO_GET_PENDING_INT_EXIT = 0x7C,
CTF_EVENT_GPIO_FIRE_CALLBACKS_ENTER = 0x7D,
CTF_EVENT_GPIO_FIRE_CALLBACK = 0x7E,
CTF_EVENT_THREAD_SLEEP_ENTER = 0x7F,
CTF_EVENT_THREAD_SLEEP_EXIT = 0x80,
} ctf_event_t;

typedef struct {
Expand Down Expand Up @@ -213,6 +215,16 @@ static inline void ctf_top_thread_priority_set(uint32_t thread_id, int8_t prio,
thread_id, name, prio);
}

static inline void ctf_top_thread_sleep_enter(uint32_t timeout)
{
CTF_EVENT(CTF_LITERAL(uint8_t, CTF_EVENT_THREAD_SLEEP_ENTER), timeout);
}

static inline void ctf_top_thread_sleep_exit(uint32_t timeout, int32_t ret)
{
CTF_EVENT(CTF_LITERAL(uint8_t, CTF_EVENT_THREAD_SLEEP_EXIT), timeout, ret);
}

static inline void ctf_top_thread_create(uint32_t thread_id, int8_t prio,
ctf_bounded_string_t name)
{
Expand Down
8 changes: 6 additions & 2 deletions subsys/tracing/ctf/tracing_ctf.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ extern "C" {
#define sys_port_trace_k_thread_join_enter(thread, timeout)
#define sys_port_trace_k_thread_join_blocking(thread, timeout)
#define sys_port_trace_k_thread_join_exit(thread, timeout, ret)
#define sys_port_trace_k_thread_sleep_enter(timeout)
#define sys_port_trace_k_thread_sleep_exit(timeout, ret)

#define sys_port_trace_k_thread_sleep_enter(timeout) \
sys_trace_k_thread_sleep_enter(timeout)
#define sys_port_trace_k_thread_sleep_exit(timeout, ret) \
sys_trace_k_thread_sleep_exit(timeout, ret)

#define sys_port_trace_k_thread_msleep_enter(ms)
#define sys_port_trace_k_thread_msleep_exit(ms, ret)
#define sys_port_trace_k_thread_usleep_enter(us)
Expand Down