Skip to content

Commit d045e36

Browse files
committed
druntime: Fix backtraces in Fibers for LoongArch
1 parent 2a8eb0a commit d045e36

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

runtime/druntime/src/core/thread/fiber.d

+3-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ private
229229
extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc;
230230
version (AArch64)
231231
extern (C) void fiber_trampoline() nothrow;
232+
version (LoongArch64)
233+
extern (C) void fiber_trampoline() nothrow;
232234
}
233235
else version (LDC_Windows)
234236
{
@@ -1835,7 +1837,7 @@ private:
18351837
// Only need to set return address ($r1). Everything else is fine
18361838
// zero initialized.
18371839
pstack -= size_t.sizeof * 10; // skip past space reserved for $r22-$r31
1838-
push (cast(size_t) &fiber_entryPoint);
1840+
push(cast(size_t) &fiber_trampoline); // see threadasm.S for docs
18391841
pstack += size_t.sizeof; // adjust sp (newp) above lr
18401842
}
18411843
else version (AsmAArch64_Posix)

runtime/druntime/src/core/threadasm.S

+24-1
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,10 @@ fiber_switchContext:
544544
*/
545545
.text
546546
.globl fiber_switchContext
547+
.type fiber_switchContext, %function
547548
fiber_switchContext:
548549
.cfi_startproc
549-
.cfi_undefined $ra
550+
.cfi_undefined 1
550551
# reserve space on stack
551552
addi.d $sp, $sp, -19 * 8
552553

@@ -610,6 +611,28 @@ fiber_switchContext:
610611

611612
jr $ra
612613
.cfi_endproc
614+
615+
/**
616+
* When generating any kind of backtrace (gdb, exception handling) for
617+
* a function called in a Fiber, we need to tell the unwinder to stop
618+
* at our Fiber main entry point, i.e. we need to mark the bottom of
619+
* the call stack. This can be done by clearing the return address register $ra
620+
* prior to calling fiber_entryPoint (i.e. in fiber_switchContext) or
621+
* using a .cfi_undefined directive for the return address register in the
622+
* Fiber entry point. cfi_undefined seems to yield better results in gdb.
623+
* Unfortunately we can't place it into fiber_entryPoint using inline
624+
* asm, so we use this trampoline instead.
625+
*/
626+
.text
627+
.global CSYM(fiber_trampoline)
628+
.p2align 2
629+
.type fiber_trampoline, %function
630+
CSYM(fiber_trampoline):
631+
.cfi_startproc
632+
.cfi_undefined 1
633+
// fiber_entryPoint never returns
634+
bl CSYM(fiber_entryPoint)
635+
.cfi_endproc
613636
#elif defined(__arm__) && (defined(__ARM_EABI__) || defined(__APPLE__))
614637
/************************************************************************************
615638
* ARM ASM BITS

0 commit comments

Comments
 (0)