From 7ec1fb632b0793c75a1c0e992244d1180ff0756a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 22 Apr 2022 13:12:20 -0700 Subject: [PATCH] subsys/tracing: Use z_current_get for thread tracing The thread switching hooks are invoked in the middle of thread switching, after the out-going thread registers are saved, but before the in-coming thread registers are restored, and also before z_thread_entry is called if the thread is just starting. When the core is first starting, the TLS base register won't be set at all, so accessing variables will fault. When switching threads, the in-coming thread TLS base register will not have been restored, so the z_tls_current value will end up getting the out-going thread instead. To fix this, switch from k_current_get() to z_current_get(). Signed-off-by: Keith Packard --- subsys/tracing/user/tracing_user.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/subsys/tracing/user/tracing_user.c b/subsys/tracing/user/tracing_user.c index e045dc613a5..9435c479df8 100644 --- a/subsys/tracing/user/tracing_user.c +++ b/subsys/tracing/user/tracing_user.c @@ -22,7 +22,8 @@ void sys_trace_k_thread_switched_in(void) int key = irq_lock(); __ASSERT_NO_MSG(nested_interrupts[_current_cpu->id] == 0); - sys_trace_thread_switched_in_user(k_current_get()); + /* Can't use k_current_get as thread base and z_tls_current might be incorrect */ + sys_trace_thread_switched_in_user(z_current_get()); irq_unlock(key); } @@ -32,7 +33,8 @@ void sys_trace_k_thread_switched_out(void) int key = irq_lock(); __ASSERT_NO_MSG(nested_interrupts[_current_cpu->id] == 0); - sys_trace_thread_switched_out_user(k_current_get()); + /* Can't use k_current_get as thread base and z_tls_current might be incorrect */ + sys_trace_thread_switched_out_user(z_current_get()); irq_unlock(key); }