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
64 // These functions are allowed so that the debugger can initiate multiple function calls.
65 // See: https://golang.org/cl/161137/
69 // Disallow calls from the runtime. We could
70 // potentially make this condition tighter (e.g., not
71 // when locks are held), but there are enough tightly
72 // coded sequences (e.g., defer handling) that it's
73 // better to play it safe.
74 if pfx := "runtime."; len(name) > len(pfx) && name[:len(pfx)] == pfx {
75 ret = debugCallRuntime
79 // Check that this isn't an unsafe-point.
83 up := pcdatavalue(f, _PCDATA_UnsafePoint, pc, nil)
84 if up != _PCDATA_UnsafePointSafe {
85 // Not at a safe point.
86 ret = debugCallUnsafePoint
92 // debugCallWrap starts a new goroutine to run a debug call and blocks
93 // the calling goroutine. On the goroutine, it prepares to recover
94 // panics from the debug call, and then calls the call dispatching
95 // function at PC dispatch.
97 // This must be deeply nosplit because there are untyped values on the
98 // stack from debugCallV2.
101 func debugCallWrap(dispatch uintptr) {
104 callerpc := getcallerpc()
107 // Create a new goroutine to execute the call on. Run this on
108 // the system stack to avoid growing our stack.
110 // TODO(mknyszek): It would be nice to wrap these arguments in an allocated
111 // closure and start the goroutine with that closure, but the compiler disallows
112 // implicit closure allocation in the runtime.
114 newg := newproc1(*(**funcval)(unsafe.Pointer(&fn)), gp, callerpc)
115 args := &debugCallWrapArgs{
119 newg.param = unsafe.Pointer(args)
121 // If the current G is locked, then transfer that
122 // locked-ness to the new goroutine.
124 // Save lock state to restore later.
126 if mp != gp.lockedm.ptr() {
127 throw("inconsistent lockedm")
131 lockedExt = mp.lockedExt
133 // Transfer external lock count to internal so
134 // it can't be unlocked from the debug call.
143 // Mark the calling goroutine as being at an async
144 // safe-point, since it has a few conservative frames
145 // at the bottom of the stack. This also prevents
147 gp.asyncSafePoint = true
149 // Stash newg away so we can execute it below (mcall's
150 // closure can't capture anything).
151 gp.schedlink.set(newg)
154 // Switch to the new goroutine.
157 newg := gp.schedlink.ptr()
160 // Park the calling goroutine.
161 gp.waitreason = waitReasonDebugCall
163 traceGoPark(traceEvGoBlock, 1)
165 casgstatus(gp, _Grunning, _Gwaiting)
168 // Directly execute the new goroutine. The debug
169 // protocol will continue on the new goroutine, so
170 // it's important we not just let the scheduler do
171 // this or it may resume a different goroutine.
175 // We'll resume here when the call returns.
177 // Restore locked state.
180 mp.lockedExt = lockedExt
186 gp.asyncSafePoint = false
189 type debugCallWrapArgs struct {
194 // debugCallWrap1 is the continuation of debugCallWrap on the callee
196 func debugCallWrap1() {
198 args := (*debugCallWrapArgs)(gp.param)
199 dispatch, callingG := args.dispatch, args.callingG
202 // Dispatch call and trap panics.
203 debugCallWrap2(dispatch)
205 // Resume the caller goroutine.
206 getg().schedlink.set(callingG)
208 callingG := gp.schedlink.ptr()
211 // Unlock this goroutine from the M if necessary. The
212 // calling G will relock.
218 // Switch back to the calling goroutine. At some point
219 // the scheduler will schedule us again and we'll
224 casgstatus(gp, _Grunning, _Grunnable)
231 traceGoUnpark(callingG, 0)
233 casgstatus(callingG, _Gwaiting, _Grunnable)
234 execute(callingG, true)
238 func debugCallWrap2(dispatch uintptr) {
239 // Call the dispatch function and trap panics.
241 dispatchFV := funcval{dispatch}
242 *(*unsafe.Pointer)(unsafe.Pointer(&dispatchF)) = noescape(unsafe.Pointer(&dispatchFV))
248 debugCallPanicked(err)