1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 debugCallSystemStack = "executing on Go runtime stack"
13 debugCallUnknownFunc = "call from unknown function"
14 debugCallRuntime = "call from within the Go runtime"
15 debugCallUnsafePoint = "call not at safe point"
19 func debugCallPanicked(val interface{})
21 // debugCallCheck checks whether it is safe to inject a debugger
22 // function call with return PC pc. If not, it returns a string
26 func debugCallCheck(pc uintptr) string {
27 // No user calls from the system stack.
28 if getg() != getg().m.curg {
29 return debugCallSystemStack
31 if sp := getcallersp(); !(getg().stack.lo < sp && sp <= getg().stack.hi) {
32 // Fast syscalls (nanotime) and racecall switch to the
33 // g0 stack without switching g. We can't safely make
34 // a call in this state. (We can't even safely
36 return debugCallSystemStack
39 // Switch to the system stack to avoid overflowing the user
45 ret = debugCallUnknownFunc
49 // Disallow calls from the runtime. We could
50 // potentially make this condition tighter (e.g., not
51 // when locks are held), but there are enough tightly
52 // coded sequences (e.g., defer handling) that it's
53 // better to play it safe.
54 if name, pfx := funcname(f), "runtime."; len(name) > len(pfx) && name[:len(pfx)] == pfx {
55 ret = debugCallRuntime
59 // Look up PC's register map.
63 pcdata = pcdatavalue(f, _PCDATA_RegMapIndex, pc, nil)
66 pcdata = 0 // in prologue
68 stkmap := (*stackmap)(funcdata(f, _FUNCDATA_RegPointerMaps))
69 if pcdata == -2 || stkmap == nil {
70 // Not at a safe point.
71 ret = debugCallUnsafePoint
78 // debugCallWrap pushes a defer to recover from panics in debug calls
79 // and then calls the dispatching function at PC dispatch.
80 func debugCallWrap(dispatch uintptr) {
82 dispatchFV := funcval{dispatch}
83 *(*unsafe.Pointer)(unsafe.Pointer(&dispatchF)) = noescape(unsafe.Pointer(&dispatchFV))
89 debugCallPanicked(err)