diff options
| author | Cherry Mui <cherryyz@google.com> | 2023-08-28 14:57:29 -0400 |
|---|---|---|
| committer | Cherry Mui <cherryyz@google.com> | 2023-10-26 18:46:50 +0000 |
| commit | 0262ea1ff9ac3b9fd268a48fcaaa6811c20cbea2 (patch) | |
| tree | 0b5ee8d3f2ce8c742b7fad74cdb70f17abed2c80 /src/runtime/asm_amd64.s | |
| parent | 29b80397a8f385ead0a9b3c11060a571438ef026 (diff) | |
| download | go-0262ea1ff9ac3b9fd268a48fcaaa6811c20cbea2.tar.xz | |
runtime: print a stack trace at "morestack on g0"
Error like "morestack on g0" is one of the errors that is very
hard to debug, because often it doesn't print a useful stack trace.
The runtime doesn't directly print a stack trace because it is
a bad stack state to call print. Sometimes the SIGABRT may trigger
a traceback, but sometimes not especially in a cgo binary. Even if
it triggers a traceback it often does not include the stack trace
of the bad stack.
This CL makes it explicitly print a stack trace and throw. The
idea is to have some space as an "emergency" crash stack. When the
stack is in a really bad state, we switch to the crash stack and
do a traceback.
Currently only implemented on AMD64 and ARM64.
TODO: also handle errors like "morestack on gsignal" and bad
systemstack. Also handle other architectures.
Change-Id: Ibfc397202f2bb0737c5cbe99f2763de83301c1c1
Reviewed-on: https://go-review.googlesource.com/c/go/+/419435
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/runtime/asm_amd64.s')
| -rw-r--r-- | src/runtime/asm_amd64.s | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/runtime/asm_amd64.s b/src/runtime/asm_amd64.s index ccc2bd21fe..ab845fbd8a 100644 --- a/src/runtime/asm_amd64.s +++ b/src/runtime/asm_amd64.s @@ -537,6 +537,30 @@ bad: CALL AX INT $3 +// func switchToCrashStack0(fn func()) +TEXT runtime·switchToCrashStack0<ABIInternal>(SB), NOSPLIT, $0-8 + MOVQ g_m(R14), BX // curm + + // set g to gcrash + LEAQ runtime·gcrash(SB), R14 // g = &gcrash + MOVQ BX, g_m(R14) // g.m = curm + MOVQ R14, m_g0(BX) // curm.g0 = g + get_tls(CX) + MOVQ R14, g(CX) + + // switch to crashstack + MOVQ (g_stack+stack_hi)(R14), BX + SUBQ $(4*8), BX + MOVQ BX, SP + + // call target function + MOVQ AX, DX + MOVQ 0(AX), AX + CALL AX + + // should never return + CALL runtime·abort(SB) + UNDEF /* * support for morestack @@ -551,17 +575,26 @@ bad: TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0 // Cannot grow scheduler stack (m->g0). get_tls(CX) - MOVQ g(CX), BX - MOVQ g_m(BX), BX - MOVQ m_g0(BX), SI - CMPQ g(CX), SI + MOVQ g(CX), DI // DI = g + MOVQ g_m(DI), BX // BX = m + + // Set g->sched to context in f. + MOVQ 0(SP), AX // f's PC + MOVQ AX, (g_sched+gobuf_pc)(DI) + LEAQ 8(SP), AX // f's SP + MOVQ AX, (g_sched+gobuf_sp)(DI) + MOVQ BP, (g_sched+gobuf_bp)(DI) + MOVQ DX, (g_sched+gobuf_ctxt)(DI) + + MOVQ m_g0(BX), SI // SI = m.g0 + CMPQ DI, SI JNE 3(PC) CALL runtime·badmorestackg0(SB) CALL runtime·abort(SB) // Cannot grow signal stack (m->gsignal). MOVQ m_gsignal(BX), SI - CMPQ g(CX), SI + CMPQ DI, SI JNE 3(PC) CALL runtime·badmorestackgsignal(SB) CALL runtime·abort(SB) @@ -573,17 +606,7 @@ TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0 MOVQ AX, (m_morebuf+gobuf_pc)(BX) LEAQ 16(SP), AX // f's caller's SP MOVQ AX, (m_morebuf+gobuf_sp)(BX) - get_tls(CX) - MOVQ g(CX), SI - MOVQ SI, (m_morebuf+gobuf_g)(BX) - - // Set g->sched to context in f. - MOVQ 0(SP), AX // f's PC - MOVQ AX, (g_sched+gobuf_pc)(SI) - LEAQ 8(SP), AX // f's SP - MOVQ AX, (g_sched+gobuf_sp)(SI) - MOVQ BP, (g_sched+gobuf_bp)(SI) - MOVQ DX, (g_sched+gobuf_ctxt)(SI) + MOVQ DI, (m_morebuf+gobuf_g)(BX) // Call newstack on m->g0's stack. MOVQ m_g0(BX), BX |
