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 <keithp@keithp.com>
This commit is contained in:
Keith Packard 2022-04-22 13:12:20 -07:00 committed by Stephanos Ioannidis
commit 7ec1fb632b

View file

@ -22,7 +22,8 @@ void sys_trace_k_thread_switched_in(void)
int key = irq_lock(); int key = irq_lock();
__ASSERT_NO_MSG(nested_interrupts[_current_cpu->id] == 0); __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); irq_unlock(key);
} }
@ -32,7 +33,8 @@ void sys_trace_k_thread_switched_out(void)
int key = irq_lock(); int key = irq_lock();
__ASSERT_NO_MSG(nested_interrupts[_current_cpu->id] == 0); __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); irq_unlock(key);
} }