diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2018-12-05 23:09:51 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2018-12-05 23:09:51 +0000 |
commit | c43137e800bb9ca2ecda0a6b6189e0eb5c22f0d7 (patch) | |
tree | df5d750d82dff84b98ec03163cc8c2b2552a559a /libgo/runtime | |
parent | e4a9a572770b48375561c7ca424eb94eb45a9fcb (diff) | |
download | gcc-c43137e800bb9ca2ecda0a6b6189e0eb5c22f0d7.zip gcc-c43137e800bb9ca2ecda0a6b6189e0eb5c22f0d7.tar.gz gcc-c43137e800bb9ca2ecda0a6b6189e0eb5c22f0d7.tar.bz2 |
runtime: add precise stack scan support
This CL adds support of precise stack scan using stack maps to
the runtime. The stack maps are generated by the compiler (if
supported). Each safepoint is associated with a (real or dummy)
landing pad, and its "type info" in the exception table is a
pointer to the stack map. When a stack is scanned, the stack map
is found by the stack unwinding code by inspecting the exception
table (LSDA).
For precise stack scan we need to unwind the stack. There are
three cases:
- If a goroutine is scanning its own stack, it can unwind the
stack and scan the frames.
- If a goroutine is scanning another, stopped, goroutine, it
cannot directly unwind the target stack. We handle this by
switching (runtime.gogo) to the target g, letting it unwind
and scan the stack, and switch back.
- If we are scanning a goroutine that is blocked in a syscall,
we send a signal to the target goroutine's thread, and let the
signal handler unwind and scan the stack. Extra care is needed
as this races with enter/exit syscall.
Currently this is only implemented on linux.
Reviewed-on: https://go-review.googlesource.com/c/140518
From-SVN: r266832
Diffstat (limited to 'libgo/runtime')
-rw-r--r-- | libgo/runtime/go-unwind.c | 248 | ||||
-rw-r--r-- | libgo/runtime/proc.c | 73 | ||||
-rw-r--r-- | libgo/runtime/runtime.h | 13 | ||||
-rw-r--r-- | libgo/runtime/stack.c | 35 |
4 files changed, 355 insertions, 14 deletions
diff --git a/libgo/runtime/go-unwind.c b/libgo/runtime/go-unwind.c index a059acb..a1a9558 100644 --- a/libgo/runtime/go-unwind.c +++ b/libgo/runtime/go-unwind.c @@ -304,6 +304,26 @@ read_encoded_value (struct _Unwind_Context *context, uint8_t encoding, return p; } +static inline int +value_size (uint8_t encoding) +{ + switch (encoding & 0x0f) + { + case DW_EH_PE_sdata2: + case DW_EH_PE_udata2: + return 2; + case DW_EH_PE_sdata4: + case DW_EH_PE_udata4: + return 4; + case DW_EH_PE_sdata8: + case DW_EH_PE_udata8: + return 8; + default: + break; + } + abort (); +} + /* The rest of this code is really similar to gcc/unwind-c.c and libjava/exception.cc. */ @@ -563,3 +583,231 @@ PERSONALITY_FUNCTION (int version, _Unwind_SetIP (context, landing_pad); return _URC_INSTALL_CONTEXT; } + +// A dummy personality function, which doesn't capture any exception +// and simply passes by. This is used for functions that don't +// capture exceptions but need LSDA for stack maps. +_Unwind_Reason_Code +__gccgo_personality_dummy (int, _Unwind_Action, _Unwind_Exception_Class, + struct _Unwind_Exception *, struct _Unwind_Context *) + __attribute__ ((no_split_stack)); + +_Unwind_Reason_Code +__gccgo_personality_dummy (int version __attribute__ ((unused)), + _Unwind_Action actions __attribute__ ((unused)), + _Unwind_Exception_Class exception_class __attribute__ ((unused)), + struct _Unwind_Exception *ue_header __attribute__ ((unused)), + struct _Unwind_Context *context __attribute__ ((unused))) +{ + CONTINUE_UNWINDING; +} + +// A sentinel value for Go functions. +// A function is a Go function if it has LSDA, which has type info, +// and the first (dummy) landing pad's type info is a pointer to +// this value. +#define GO_FUNC_SENTINEL ((uint64)'G' | ((uint64)'O'<<8) | \ + ((uint64)'.'<<16) | ((uint64)'.'<<24) | \ + ((uint64)'F'<<32) | ((uint64)'U'<<40) | \ + ((uint64)'N'<<48) | ((uint64)'C'<<56)) + +struct _stackmap { + uint32 len; + uint8 data[1]; // variabe length +}; + +extern void + runtime_scanstackblockwithmap (uintptr ip, uintptr sp, uintptr size, uint8 *ptrmask, void* gcw) + __asm__ (GOSYM_PREFIX "runtime.scanstackblockwithmap"); + +#define FOUND 0 +#define NOTFOUND_OK 1 +#define NOTFOUND_BAD 2 + +// Helper function to search for stack maps in the unwinding records of a frame. +// If found, populate ip, sp, and stackmap. Returns the #define'd values above. +static int +findstackmaps (struct _Unwind_Context *context, _Unwind_Ptr *ip, _Unwind_Ptr *sp, struct _stackmap **stackmap) +{ + lsda_header_info info; + const unsigned char *language_specific_data, *p, *action_record; + bool first; + struct _stackmap *stackmap1; + _Unwind_Ptr ip1; + int ip_before_insn = 0; + _sleb128_t index; + int size; + + language_specific_data = (const unsigned char *) + _Unwind_GetLanguageSpecificData (context); + + /* If no LSDA, then there is no stack maps. */ + if (! language_specific_data) + return NOTFOUND_OK; + + p = parse_lsda_header (context, language_specific_data, &info); + + if (info.TType == NULL) + return NOTFOUND_OK; + +#ifdef HAVE_GETIPINFO + ip1 = _Unwind_GetIPInfo (context, &ip_before_insn); +#else + ip1 = _Unwind_GetIP (context); +#endif + if (! ip_before_insn) + --ip1; + + size = value_size (info.ttype_encoding); + + action_record = NULL; + first = true; + + /* Search the call-site table for the action associated with this IP. */ + while (p < info.action_table) + { + _Unwind_Ptr cs_start, cs_len, cs_lp; + _uleb128_t cs_action; + + /* Note that all call-site encodings are "absolute" displacements. */ + p = read_encoded_value (0, info.call_site_encoding, p, &cs_start); + p = read_encoded_value (0, info.call_site_encoding, p, &cs_len); + p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp); + p = read_uleb128 (p, &cs_action); + + if (first) + { + // For a Go function, the first entry points to the sentinel value. + // Check this here. + const unsigned char *p1, *action1; + uint64 *x; + + if (!cs_action) + return NOTFOUND_OK; + + action1 = info.action_table + cs_action - 1; + read_sleb128 (action1, &index); + p1 = info.TType - index*size; + read_encoded_value (context, info.ttype_encoding, p1, (_Unwind_Ptr*)&x); + if (x == NULL || *x != GO_FUNC_SENTINEL) + return NOTFOUND_OK; + + first = false; + continue; + } + + /* The table is sorted, so if we've passed the ip, stop. */ + if (ip1 < info.Start + cs_start) + return NOTFOUND_BAD; + else if (ip1 < info.Start + cs_start + cs_len) + { + if (cs_action) + action_record = info.action_table + cs_action - 1; + break; + } + } + + if (action_record == NULL) + return NOTFOUND_BAD; + + read_sleb128 (action_record, &index); + p = info.TType - index*size; + read_encoded_value (context, info.ttype_encoding, p, (_Unwind_Ptr*)&stackmap1); + if (stackmap1 == NULL) + return NOTFOUND_BAD; + + if (ip != NULL) + *ip = ip1; + if (sp != NULL) + *sp = _Unwind_GetCFA (context); + if (stackmap != NULL) + *stackmap = stackmap1; + return FOUND; +} + +// Callback function to scan a stack frame with stack maps. +// It skips non-Go functions. +static _Unwind_Reason_Code +scanstackwithmap_callback (struct _Unwind_Context *context, void *arg) +{ + struct _stackmap *stackmap; + _Unwind_Ptr ip, sp; + G* gp; + void *gcw = arg; + + switch (findstackmaps (context, &ip, &sp, &stackmap)) + { + case NOTFOUND_OK: + // Not a Go function. Skip this frame. + return _URC_NO_REASON; + case NOTFOUND_BAD: + { + // No stack map found. + // If we're scanning from the signal stack, the goroutine + // may be not stopped at a safepoint. Allow this case. + gp = runtime_g (); + if (gp != gp->m->gsignal) + { + // TODO: print gp, pc, sp + runtime_throw ("no stack map"); + } + return _URC_NORMAL_STOP; + } + case FOUND: + break; + default: + abort (); + } + + runtime_scanstackblockwithmap (ip, sp, (uintptr)(stackmap->len) * sizeof(uintptr), stackmap->data, gcw); + + return _URC_NO_REASON; +} + +// Scan the stack with stack maps. Return whether the scan +// succeeded. +bool +scanstackwithmap (void *gcw) +{ + _Unwind_Reason_Code code; + code = _Unwind_Backtrace (scanstackwithmap_callback, gcw); + return code == _URC_END_OF_STACK; +} + +// Returns whether stack map is enabled. +bool +usestackmaps () +{ + return runtime_usestackmaps; +} + +// Callback function to probe if a stack frame has stack maps. +static _Unwind_Reason_Code +probestackmaps_callback (struct _Unwind_Context *context, + void *arg __attribute__ ((unused))) +{ + switch (findstackmaps (context, NULL, NULL, NULL)) + { + case NOTFOUND_OK: + case NOTFOUND_BAD: + return _URC_NO_REASON; + case FOUND: + break; + default: + abort (); + } + + // Found a stack map. No need to keep unwinding. + runtime_usestackmaps = true; + return _URC_NORMAL_STOP; +} + +// Try to find a stack map, store the result in global variable runtime_usestackmaps. +// Called in start-up time from Go code, so there is a Go frame on the stack. +bool +probestackmaps () +{ + runtime_usestackmaps = false; + _Unwind_Backtrace (probestackmaps_callback, NULL); + return runtime_usestackmaps; +} diff --git a/libgo/runtime/proc.c b/libgo/runtime/proc.c index 7bd95a0..99b2cb1 100644 --- a/libgo/runtime/proc.c +++ b/libgo/runtime/proc.c @@ -59,6 +59,8 @@ uintptr runtime_stacks_sys; void gtraceback(G*) __asm__(GOSYM_PREFIX "runtime.gtraceback"); +static void gscanstack(G*); + #ifdef __rtems__ #define __thread #endif @@ -340,6 +342,8 @@ runtime_mcall(FuncVal *fv) if(gp->traceback != 0) gtraceback(gp); + if(gp->scang != 0) + gscanstack(gp); } if (gp == nil || !gp->fromgogo) { #ifdef USING_SPLIT_STACK @@ -469,6 +473,66 @@ gtraceback(G* gp) runtime_gogo(traceback->gp); } +void doscanstackswitch(G*, G*) __asm__(GOSYM_PREFIX "runtime.doscanstackswitch"); + +// Switch to gp and let it scan its stack. +// The first time gp->scang is set (to me). The second time here +// gp is done scanning, and has unset gp->scang, so we just return. +void +doscanstackswitch(G* me, G* gp) +{ + __go_assert(me->entry == nil); + me->fromgogo = false; + +#ifdef USING_SPLIT_STACK + __splitstack_getcontext((void*)(&me->stackcontext[0])); +#endif + getcontext(ucontext_arg(&me->context[0])); + + if(me->entry != nil) { + // Got here from mcall. + // The stack scanning code may call systemstack, which calls + // mcall, which calls setcontext. + // Run the function, which at the end will switch back to gp. + FuncVal *fv = me->entry; + void (*pfn)(G*) = (void (*)(G*))fv->fn; + G* gp1 = (G*)me->param; + __go_assert(gp1 == gp); + me->entry = nil; + me->param = nil; + __builtin_call_with_static_chain(pfn(gp1), fv); + abort(); + } + + if (gp->scang != 0) + runtime_gogo(gp); +} + +// Do a stack scan, then switch back to the g that triggers this scan. +// We come here from doscanstackswitch. +static void +gscanstack(G *gp) +{ + G *oldg, *oldcurg; + M* holdm; + + oldg = (G*)gp->scang; + oldcurg = oldg->m->curg; + holdm = gp->m; + if(holdm != nil && holdm != g->m) + runtime_throw("gscanstack: m is not nil"); + oldg->m->curg = gp; + gp->m = oldg->m; + gp->scang = 0; + + doscanstack(gp, (void*)gp->scangcw); + + gp->scangcw = 0; + gp->m = holdm; + oldg->m->curg = oldcurg; + runtime_gogo(oldg); +} + // Called by pthread_create to start an M. void* runtime_mstart(void *arg) @@ -516,6 +580,9 @@ runtime_mstart(void *arg) // may always go to the getcontext call in mcall. gtraceback(gp); } + if(gp->scang != 0) + // Got here from doscanswitch. Should not happen. + runtime_throw("mstart with scang"); if(gp->entry != nil) { // Got here from mcall. @@ -630,7 +697,8 @@ runtime_entersyscall() { // Save the registers in the g structure so that any pointers // held in registers will be seen by the garbage collector. - getcontext(ucontext_arg(&g->gcregs[0])); + if (!runtime_usestackmaps) + getcontext(ucontext_arg(&g->gcregs[0])); // Note that if this function does save any registers itself, // we might store the wrong value in the call to getcontext. @@ -676,7 +744,8 @@ runtime_entersyscallblock() { // Save the registers in the g structure so that any pointers // held in registers will be seen by the garbage collector. - getcontext(ucontext_arg(&g->gcregs[0])); + if (!runtime_usestackmaps) + getcontext(ucontext_arg(&g->gcregs[0])); // See comment in runtime_entersyscall. doentersyscallblock((uintptr)runtime_getcallerpc(), diff --git a/libgo/runtime/runtime.h b/libgo/runtime/runtime.h index 3c94532..21921ec 100644 --- a/libgo/runtime/runtime.h +++ b/libgo/runtime/runtime.h @@ -502,3 +502,16 @@ struct funcfileline_return struct funcfileline_return runtime_funcfileline (uintptr targetpc, int32 index) __asm__ (GOSYM_PREFIX "runtime.funcfileline"); + +/* + * helpers for stack scan. + */ +bool scanstackwithmap(void*) + __asm__(GOSYM_PREFIX "runtime.scanstackwithmap"); +bool doscanstack(G*, void*) + __asm__("runtime.doscanstack"); + +bool runtime_usestackmaps; + +bool probestackmaps(void) + __asm__("runtime.probestackmaps"); diff --git a/libgo/runtime/stack.c b/libgo/runtime/stack.c index a971e8f..2d5d1e0 100644 --- a/libgo/runtime/stack.c +++ b/libgo/runtime/stack.c @@ -23,33 +23,43 @@ extern void * __splitstack_find_context (void *context[10], size_t *, void **, extern void scanstackblock(void *addr, uintptr size, void *gcw) __asm__("runtime.scanstackblock"); -void doscanstack(G*, void*) - __asm__("runtime.doscanstack"); - -static void doscanstack1(G*, void*) +static bool doscanstack1(G*, void*) __attribute__ ((noinline)); // Scan gp's stack, passing stack chunks to scanstackblock. -void doscanstack(G *gp, void* gcw) { +bool doscanstack(G *gp, void* gcw) { // Save registers on the stack, so that if we are scanning our // own stack we will see them. - __builtin_unwind_init(); - flush_registers_to_secondary_stack(); + if (!runtime_usestackmaps) { + __builtin_unwind_init(); + flush_registers_to_secondary_stack(); + } - doscanstack1(gp, gcw); + return doscanstack1(gp, gcw); } // Scan gp's stack after saving registers. -static void doscanstack1(G *gp, void *gcw) { +static bool doscanstack1(G *gp, void *gcw) { #ifdef USING_SPLIT_STACK void* sp; size_t spsize; void* next_segment; void* next_sp; void* initial_sp; - - if (gp == runtime_g()) { + G* _g_; + + _g_ = runtime_g(); + if (runtime_usestackmaps) { + // If stack map is enabled, we get here only when we can unwind + // the stack being scanned. That is, either we are scanning our + // own stack, or we are scanning through a signal handler. + __go_assert((_g_ == gp) || ((_g_ == gp->m->gsignal) && (gp == gp->m->curg))); + return scanstackwithmap(gcw); + } + if (_g_ == gp) { // Scanning our own stack. + // If we are on a signal stack, it can unwind through the signal + // handler and see the g stack, so just scan our own stack. sp = __splitstack_find(nil, nil, &spsize, &next_segment, &next_sp, &initial_sp); } else { @@ -95,7 +105,7 @@ static void doscanstack1(G *gp, void *gcw) { // The goroutine is usually asleep (the world is stopped). bottom = (void*)gp->gcnextsp; if(bottom == nil) - return; + return true; nextsp2 = (void*)gp->gcnextsp2; } top = (byte*)(void*)(gp->gcinitialsp) + gp->gcstacksize; @@ -111,4 +121,5 @@ static void doscanstack1(G *gp, void *gcw) { scanstackblock(initialsp2, (uintptr)(nextsp2 - initialsp2), gcw); } #endif + return true; } |