Skip to content

Commit

Permalink
aarch64: add thread_main to improve stacktraces
Browse files Browse the repository at this point in the history
This patch improves debugging experience by making gdb
show correct stack traces that look similar to what one can
see in x86_64 port. It also solves sporadic errors when
iterating over list of threads using 'osv info threads'
when gdb would break like so:

`Python Exception <class 'gdb.error'> Attempt to assign to an unmodifiable value.:
 Error occurred in Python: Attempt to assign to an unmodifiable value.'

So in essence this patch adds new function implemented in assembly - 'thread_main' -
which calls 'thread_main_c'. The address of this new function is also what we
set 'pc' field of the thread _state instead of 'thread_main_c'. The main
benefit of adding this extra redirection is that we can add proper CFI
(Call Frame Information) directive - '.cfi_undefined x30` - to force gdb not
to look up the stack for call frames beyond the 'thread_main' one. This
is similar to what we do in x86_64 port.

So in the end, besides fixing sporadic gdb errors when iterating over
threads, we also get much better and correct looking stacktraces
that end with (or start with from the bottom):

Before the patch:
-----------------
#0  reschedule_from_interrupt () at arch/aarch64/sched.S:54
#1  0x00000000402e36d4 in sched::cpu::schedule () at core/sched.cc:229
#2  0x00000000402e79a0 in sched::thread::wait (this=<optimized out>) at core/sched.cc:1273
#3  sched::thread::do_wait_until<sched::noninterruptible, sched::thread::dummy_lock, sched::cpu::load_balance()::<lambda()> > (mtx=<synthetic pointer>..., pred=...)
    at include/osv/sched.hh:1101
#4  sched::thread::wait_until<sched::cpu::load_balance()::<lambda()> > (pred=...) at include/osv/sched.hh:1112
#5  sched::cpu::load_balance (
    this=0x40700960 <construction vtable for std::ostream-in-std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >+24>)
    at core/sched.cc:730
#6  0x00000000402e72b4 in sched::thread::main (this=0xffffa000408a39a0) at core/sched.cc:1267
#7  sched::thread_main_c (t=0xffffa000408a39a0) at arch/aarch64/arch-switch.hh:161
#8  0x00000000402c04e0 in reschedule_from_interrupt () at arch/aarch64/sched.S:50
#9  0x0000000000000000 in ?? ()

After the patch:
----------------
#0  reschedule_from_interrupt () at arch/aarch64/sched.S:54
#1  0x00000000402e36d4 in sched::cpu::schedule () at core/sched.cc:229
#2  0x00000000402e79a0 in sched::thread::wait (this=<optimized out>) at core/sched.cc:1273
#3  sched::thread::do_wait_until<sched::noninterruptible, sched::thread::dummy_lock, sched::cpu::load_balance()::<lambda()> > (mtx=<synthetic pointer>..., pred=...)
    at include/osv/sched.hh:1101
#4  sched::thread::wait_until<sched::cpu::load_balance()::<lambda()> > (pred=...) at include/osv/sched.hh:1112
#5  sched::cpu::load_balance (
    this=0x407009b8 <construction vtable for std::ostream-in-std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >+24>)
    at core/sched.cc:730
#6  0x00000000402e72b4 in sched::thread::main (this=0xffffa000408a39a0) at core/sched.cc:1267
#7  sched::thread_main_c (t=0xffffa000408a39a0) at arch/aarch64/arch-switch.hh:162
#8  0x000000004020b788 in thread_main () at arch/aarch64/entry.S:112

Refs #1128

Signed-off-by: Waldemar Kozaczuk <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
wkozaczuk authored and nyh committed Mar 29, 2021
1 parent 2e4ce28 commit 7feab52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion arch/aarch64/arch-switch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "arch-setup.hh"

extern "C" {
void thread_main(void);
void thread_main_c(sched::thread* t);
}

Expand Down Expand Up @@ -57,7 +58,7 @@ void thread::init_stack()
_state.fp = 0;
_state.thread = this;
_state.sp = stacktop;
_state.pc = reinterpret_cast<void*>(thread_main_c);
_state.pc = reinterpret_cast<void*>(thread_main);
}

void thread::setup_tcb()
Expand Down
9 changes: 9 additions & 0 deletions arch/aarch64/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ exception_vectors:
ldr x30, [sp], #48
.endm /* pop_state_to_exception_frame */

.global thread_main
thread_main:
.type thread_main, @function
.cfi_startproc simple
.cfi_undefined %x30
.cfi_def_cfa %sp, 0
bl thread_main_c
.cfi_endproc

.equ ESR_EC_BEG,26 // Exception Class field begin in ESR
.equ ESR_EC_END,31 // Exception Class field end in ESR
.equ ESR_EC_DATA_ABORT,0x25 // Exception Class Data Abort value
Expand Down

0 comments on commit 7feab52

Please sign in to comment.