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.
5 //go:build amd64 || arm64
15 debugCallSystemStack = "executing on Go runtime stack"
16 debugCallUnknownFunc = "call from unknown function"
17 debugCallRuntime = "call from within the Go runtime"
18 debugCallUnsafePoint = "call not at safe point"
22 func debugCallPanicked(val any)
24 // debugCallCheck checks whether it is safe to inject a debugger
25 // function call with return PC pc. If not, it returns a string
29 func debugCallCheck(pc uintptr) string {
30 // No user calls from the system stack.
31 if getg() != getg().m.curg {
32 return debugCallSystemStack
34 if sp := getcallersp(); !(getg().stack.lo < sp && sp <= getg().stack.hi) {
35 // Fast syscalls (nanotime) and racecall switch to the
36 // g0 stack without switching g. We can't safely make
37 // a call in this state. (We can't even safely
39 return debugCallSystemStack
42 // Switch to the system stack to avoid overflowing the user
48 ret = debugCallUnknownFunc
67 // These functions are allowed so that the debugger can initiate multiple function calls.
68 // See: https://golang.org/cl/161137/
72 // Disallow calls from the runtime. We could
73 // potentially make this condition tighter (e.g., not
74 // when locks are held), but there are enough tightly
75 // coded sequences (e.g., defer handling) that it's
76 // better to play it safe.
77 if pfx := "runtime."; len(name) > len(pfx) && name[:len(pfx)] == pfx {
78 ret = debugCallRuntime
82 // Check that this isn't an unsafe-point.
86 up := pcdatavalue(f, abi.PCDATA_UnsafePoint, pc)
87 if up != abi.UnsafePointSafe {
88 // Not at a safe point.
89 ret = debugCallUnsafePoint
95 // debugCallWrap starts a new goroutine to run a debug call and blocks
96 // the calling goroutine. On the goroutine, it prepares to recover
97 // panics from the debug call, and then calls the call dispatching
98 // function at PC dispatch.
100 // This must be deeply nosplit because there are untyped values on the
101 // stack from debugCallV2.
104 func debugCallWrap(dispatch uintptr) {
107 callerpc := getcallerpc()
110 // Create a new goroutine to execute the call on. Run this on
111 // the system stack to avoid growing our stack.
113 // TODO(mknyszek): It would be nice to wrap these arguments in an allocated
114 // closure and start the goroutine with that closure, but the compiler disallows
115 // implicit closure allocation in the runtime.
117 newg := newproc1(*(**funcval)(unsafe.Pointer(&fn)), gp, callerpc)
118 args := &debugCallWrapArgs{
122 newg.param = unsafe.Pointer(args)
124 // If the current G is locked, then transfer that
125 // locked-ness to the new goroutine.
127 // Save lock state to restore later.
129 if mp != gp.lockedm.ptr() {
130 throw("inconsistent lockedm")
134 lockedExt = mp.lockedExt
136 // Transfer external lock count to internal so
137 // it can't be unlocked from the debug call.
146 // Mark the calling goroutine as being at an async
147 // safe-point, since it has a few conservative frames
148 // at the bottom of the stack. This also prevents
150 gp.asyncSafePoint = true
152 // Stash newg away so we can execute it below (mcall's
153 // closure can't capture anything).
154 gp.schedlink.set(newg)
157 // Switch to the new goroutine.
160 newg := gp.schedlink.ptr()
163 // Park the calling goroutine.
165 traceGoPark(traceBlockDebugCall, 1)
167 casGToWaiting(gp, _Grunning, waitReasonDebugCall)
170 // Directly execute the new goroutine. The debug
171 // protocol will continue on the new goroutine, so
172 // it's important we not just let the scheduler do
173 // this or it may resume a different goroutine.
177 // We'll resume here when the call returns.
179 // Restore locked state.
182 mp.lockedExt = lockedExt
188 gp.asyncSafePoint = false
191 type debugCallWrapArgs struct {
196 // debugCallWrap1 is the continuation of debugCallWrap on the callee
198 func debugCallWrap1() {
200 args := (*debugCallWrapArgs)(gp.param)
201 dispatch, callingG := args.dispatch, args.callingG
204 // Dispatch call and trap panics.
205 debugCallWrap2(dispatch)
207 // Resume the caller goroutine.
208 getg().schedlink.set(callingG)
210 callingG := gp.schedlink.ptr()
213 // Unlock this goroutine from the M if necessary. The
214 // calling G will relock.
220 // Switch back to the calling goroutine. At some point
221 // the scheduler will schedule us again and we'll
226 casgstatus(gp, _Grunning, _Grunnable)
233 traceGoUnpark(callingG, 0)
235 casgstatus(callingG, _Gwaiting, _Grunnable)
236 execute(callingG, true)
240 func debugCallWrap2(dispatch uintptr) {
241 // Call the dispatch function and trap panics.
243 dispatchFV := funcval{dispatch}
244 *(*unsafe.Pointer)(unsafe.Pointer(&dispatchF)) = noescape(unsafe.Pointer(&dispatchFV))
250 debugCallPanicked(err)