1// Copyright 2009 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.
4
5package runtime
6
7import (
8	"internal/abi"
9	"internal/bytealg"
10	"internal/goarch"
11	"internal/stringslite"
12	"runtime/internal/sys"
13	"unsafe"
14)
15
16// The code in this file implements stack trace walking for all architectures.
17// The most important fact about a given architecture is whether it uses a link register.
18// On systems with link registers, the prologue for a non-leaf function stores the
19// incoming value of LR at the bottom of the newly allocated stack frame.
20// On systems without link registers (x86), the architecture pushes a return PC during
21// the call instruction, so the return PC ends up above the stack frame.
22// In this file, the return PC is always called LR, no matter how it was found.
23
24const usesLR = sys.MinFrameSize > 0
25
26const (
27	// tracebackInnerFrames is the number of innermost frames to print in a
28	// stack trace. The total maximum frames is tracebackInnerFrames +
29	// tracebackOuterFrames.
30	tracebackInnerFrames = 50
31
32	// tracebackOuterFrames is the number of outermost frames to print in a
33	// stack trace.
34	tracebackOuterFrames = 50
35)
36
37// unwindFlags control the behavior of various unwinders.
38type unwindFlags uint8
39
40const (
41	// unwindPrintErrors indicates that if unwinding encounters an error, it
42	// should print a message and stop without throwing. This is used for things
43	// like stack printing, where it's better to get incomplete information than
44	// to crash. This is also used in situations where everything may not be
45	// stopped nicely and the stack walk may not be able to complete, such as
46	// during profiling signals or during a crash.
47	//
48	// If neither unwindPrintErrors or unwindSilentErrors are set, unwinding
49	// performs extra consistency checks and throws on any error.
50	//
51	// Note that there are a small number of fatal situations that will throw
52	// regardless of unwindPrintErrors or unwindSilentErrors.
53	unwindPrintErrors unwindFlags = 1 << iota
54
55	// unwindSilentErrors silently ignores errors during unwinding.
56	unwindSilentErrors
57
58	// unwindTrap indicates that the initial PC and SP are from a trap, not a
59	// return PC from a call.
60	//
61	// The unwindTrap flag is updated during unwinding. If set, frame.pc is the
62	// address of a faulting instruction instead of the return address of a
63	// call. It also means the liveness at pc may not be known.
64	//
65	// TODO: Distinguish frame.continpc, which is really the stack map PC, from
66	// the actual continuation PC, which is computed differently depending on
67	// this flag and a few other things.
68	unwindTrap
69
70	// unwindJumpStack indicates that, if the traceback is on a system stack, it
71	// should resume tracing at the user stack when the system stack is
72	// exhausted.
73	unwindJumpStack
74)
75
76// An unwinder iterates the physical stack frames of a Go sack.
77//
78// Typical use of an unwinder looks like:
79//
80//	var u unwinder
81//	for u.init(gp, 0); u.valid(); u.next() {
82//		// ... use frame info in u ...
83//	}
84//
85// Implementation note: This is carefully structured to be pointer-free because
86// tracebacks happen in places that disallow write barriers (e.g., signals).
87// Even if this is stack-allocated, its pointer-receiver methods don't know that
88// their receiver is on the stack, so they still emit write barriers. Here we
89// address that by carefully avoiding any pointers in this type. Another
90// approach would be to split this into a mutable part that's passed by pointer
91// but contains no pointers itself and an immutable part that's passed and
92// returned by value and can contain pointers. We could potentially hide that
93// we're doing that in trivial methods that are inlined into the caller that has
94// the stack allocation, but that's fragile.
95type unwinder struct {
96	// frame is the current physical stack frame, or all 0s if
97	// there is no frame.
98	frame stkframe
99
100	// g is the G who's stack is being unwound. If the
101	// unwindJumpStack flag is set and the unwinder jumps stacks,
102	// this will be different from the initial G.
103	g guintptr
104
105	// cgoCtxt is the index into g.cgoCtxt of the next frame on the cgo stack.
106	// The cgo stack is unwound in tandem with the Go stack as we find marker frames.
107	cgoCtxt int
108
109	// calleeFuncID is the function ID of the caller of the current
110	// frame.
111	calleeFuncID abi.FuncID
112
113	// flags are the flags to this unwind. Some of these are updated as we
114	// unwind (see the flags documentation).
115	flags unwindFlags
116}
117
118// init initializes u to start unwinding gp's stack and positions the
119// iterator on gp's innermost frame. gp must not be the current G.
120//
121// A single unwinder can be reused for multiple unwinds.
122func (u *unwinder) init(gp *g, flags unwindFlags) {
123	// Implementation note: This starts the iterator on the first frame and we
124	// provide a "valid" method. Alternatively, this could start in a "before
125	// the first frame" state and "next" could return whether it was able to
126	// move to the next frame, but that's both more awkward to use in a "for"
127	// loop and is harder to implement because we have to do things differently
128	// for the first frame.
129	u.initAt(^uintptr(0), ^uintptr(0), ^uintptr(0), gp, flags)
130}
131
132func (u *unwinder) initAt(pc0, sp0, lr0 uintptr, gp *g, flags unwindFlags) {
133	// Don't call this "g"; it's too easy get "g" and "gp" confused.
134	if ourg := getg(); ourg == gp && ourg == ourg.m.curg {
135		// The starting sp has been passed in as a uintptr, and the caller may
136		// have other uintptr-typed stack references as well.
137		// If during one of the calls that got us here or during one of the
138		// callbacks below the stack must be grown, all these uintptr references
139		// to the stack will not be updated, and traceback will continue
140		// to inspect the old stack memory, which may no longer be valid.
141		// Even if all the variables were updated correctly, it is not clear that
142		// we want to expose a traceback that begins on one stack and ends
143		// on another stack. That could confuse callers quite a bit.
144		// Instead, we require that initAt and any other function that
145		// accepts an sp for the current goroutine (typically obtained by
146		// calling getcallersp) must not run on that goroutine's stack but
147		// instead on the g0 stack.
148		throw("cannot trace user goroutine on its own stack")
149	}
150
151	if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp.
152		if gp.syscallsp != 0 {
153			pc0 = gp.syscallpc
154			sp0 = gp.syscallsp
155			if usesLR {
156				lr0 = 0
157			}
158		} else {
159			pc0 = gp.sched.pc
160			sp0 = gp.sched.sp
161			if usesLR {
162				lr0 = gp.sched.lr
163			}
164		}
165	}
166
167	var frame stkframe
168	frame.pc = pc0
169	frame.sp = sp0
170	if usesLR {
171		frame.lr = lr0
172	}
173
174	// If the PC is zero, it's likely a nil function call.
175	// Start in the caller's frame.
176	if frame.pc == 0 {
177		if usesLR {
178			frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp))
179			frame.lr = 0
180		} else {
181			frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp))
182			frame.sp += goarch.PtrSize
183		}
184	}
185
186	// internal/runtime/atomic functions call into kernel helpers on
187	// arm < 7. See internal/runtime/atomic/sys_linux_arm.s.
188	//
189	// Start in the caller's frame.
190	if GOARCH == "arm" && goarm < 7 && GOOS == "linux" && frame.pc&0xffff0000 == 0xffff0000 {
191		// Note that the calls are simple BL without pushing the return
192		// address, so we use LR directly.
193		//
194		// The kernel helpers are frameless leaf functions, so SP and
195		// LR are not touched.
196		frame.pc = frame.lr
197		frame.lr = 0
198	}
199
200	f := findfunc(frame.pc)
201	if !f.valid() {
202		if flags&unwindSilentErrors == 0 {
203			print("runtime: g ", gp.goid, " gp=", gp, ": unknown pc ", hex(frame.pc), "\n")
204			tracebackHexdump(gp.stack, &frame, 0)
205		}
206		if flags&(unwindPrintErrors|unwindSilentErrors) == 0 {
207			throw("unknown pc")
208		}
209		*u = unwinder{}
210		return
211	}
212	frame.fn = f
213
214	// Populate the unwinder.
215	*u = unwinder{
216		frame:        frame,
217		g:            gp.guintptr(),
218		cgoCtxt:      len(gp.cgoCtxt) - 1,
219		calleeFuncID: abi.FuncIDNormal,
220		flags:        flags,
221	}
222
223	isSyscall := frame.pc == pc0 && frame.sp == sp0 && pc0 == gp.syscallpc && sp0 == gp.syscallsp
224	u.resolveInternal(true, isSyscall)
225}
226
227func (u *unwinder) valid() bool {
228	return u.frame.pc != 0
229}
230
231// resolveInternal fills in u.frame based on u.frame.fn, pc, and sp.
232//
233// innermost indicates that this is the first resolve on this stack. If
234// innermost is set, isSyscall indicates that the PC/SP was retrieved from
235// gp.syscall*; this is otherwise ignored.
236//
237// On entry, u.frame contains:
238//   - fn is the running function.
239//   - pc is the PC in the running function.
240//   - sp is the stack pointer at that program counter.
241//   - For the innermost frame on LR machines, lr is the program counter that called fn.
242//
243// On return, u.frame contains:
244//   - fp is the stack pointer of the caller.
245//   - lr is the program counter that called fn.
246//   - varp, argp, and continpc are populated for the current frame.
247//
248// If fn is a stack-jumping function, resolveInternal can change the entire
249// frame state to follow that stack jump.
250//
251// This is internal to unwinder.
252func (u *unwinder) resolveInternal(innermost, isSyscall bool) {
253	frame := &u.frame
254	gp := u.g.ptr()
255
256	f := frame.fn
257	if f.pcsp == 0 {
258		// No frame information, must be external function, like race support.
259		// See golang.org/issue/13568.
260		u.finishInternal()
261		return
262	}
263
264	// Compute function info flags.
265	flag := f.flag
266	if f.funcID == abi.FuncID_cgocallback {
267		// cgocallback does write SP to switch from the g0 to the curg stack,
268		// but it carefully arranges that during the transition BOTH stacks
269		// have cgocallback frame valid for unwinding through.
270		// So we don't need to exclude it with the other SP-writing functions.
271		flag &^= abi.FuncFlagSPWrite
272	}
273	if isSyscall {
274		// Some Syscall functions write to SP, but they do so only after
275		// saving the entry PC/SP using entersyscall.
276		// Since we are using the entry PC/SP, the later SP write doesn't matter.
277		flag &^= abi.FuncFlagSPWrite
278	}
279
280	// Found an actual function.
281	// Derive frame pointer.
282	if frame.fp == 0 {
283		// Jump over system stack transitions. If we're on g0 and there's a user
284		// goroutine, try to jump. Otherwise this is a regular call.
285		// We also defensively check that this won't switch M's on us,
286		// which could happen at critical points in the scheduler.
287		// This ensures gp.m doesn't change from a stack jump.
288		if u.flags&unwindJumpStack != 0 && gp == gp.m.g0 && gp.m.curg != nil && gp.m.curg.m == gp.m {
289			switch f.funcID {
290			case abi.FuncID_morestack:
291				// morestack does not return normally -- newstack()
292				// gogo's to curg.sched. Match that.
293				// This keeps morestack() from showing up in the backtrace,
294				// but that makes some sense since it'll never be returned
295				// to.
296				gp = gp.m.curg
297				u.g.set(gp)
298				frame.pc = gp.sched.pc
299				frame.fn = findfunc(frame.pc)
300				f = frame.fn
301				flag = f.flag
302				frame.lr = gp.sched.lr
303				frame.sp = gp.sched.sp
304				u.cgoCtxt = len(gp.cgoCtxt) - 1
305			case abi.FuncID_systemstack:
306				// systemstack returns normally, so just follow the
307				// stack transition.
308				if usesLR && funcspdelta(f, frame.pc) == 0 {
309					// We're at the function prologue and the stack
310					// switch hasn't happened, or epilogue where we're
311					// about to return. Just unwind normally.
312					// Do this only on LR machines because on x86
313					// systemstack doesn't have an SP delta (the CALL
314					// instruction opens the frame), therefore no way
315					// to check.
316					flag &^= abi.FuncFlagSPWrite
317					break
318				}
319				gp = gp.m.curg
320				u.g.set(gp)
321				frame.sp = gp.sched.sp
322				u.cgoCtxt = len(gp.cgoCtxt) - 1
323				flag &^= abi.FuncFlagSPWrite
324			}
325		}
326		frame.fp = frame.sp + uintptr(funcspdelta(f, frame.pc))
327		if !usesLR {
328			// On x86, call instruction pushes return PC before entering new function.
329			frame.fp += goarch.PtrSize
330		}
331	}
332
333	// Derive link register.
334	if flag&abi.FuncFlagTopFrame != 0 {
335		// This function marks the top of the stack. Stop the traceback.
336		frame.lr = 0
337	} else if flag&abi.FuncFlagSPWrite != 0 && (!innermost || u.flags&(unwindPrintErrors|unwindSilentErrors) != 0) {
338		// The function we are in does a write to SP that we don't know
339		// how to encode in the spdelta table. Examples include context
340		// switch routines like runtime.gogo but also any code that switches
341		// to the g0 stack to run host C code.
342		// We can't reliably unwind the SP (we might not even be on
343		// the stack we think we are), so stop the traceback here.
344		//
345		// The one exception (encoded in the complex condition above) is that
346		// we assume if we're doing a precise traceback, and this is the
347		// innermost frame, that the SPWRITE function voluntarily preempted itself on entry
348		// during the stack growth check. In that case, the function has
349		// not yet had a chance to do any writes to SP and is safe to unwind.
350		// isAsyncSafePoint does not allow assembly functions to be async preempted,
351		// and preemptPark double-checks that SPWRITE functions are not async preempted.
352		// So for GC stack traversal, we can safely ignore SPWRITE for the innermost frame,
353		// but farther up the stack we'd better not find any.
354		// This is somewhat imprecise because we're just guessing that we're in the stack
355		// growth check. It would be better if SPWRITE were encoded in the spdelta
356		// table so we would know for sure that we were still in safe code.
357		//
358		// uSE uPE inn | action
359		//  T   _   _  | frame.lr = 0
360		//  F   T   _  | frame.lr = 0
361		//  F   F   F  | print; panic
362		//  F   F   T  | ignore SPWrite
363		if u.flags&(unwindPrintErrors|unwindSilentErrors) == 0 && !innermost {
364			println("traceback: unexpected SPWRITE function", funcname(f))
365			throw("traceback")
366		}
367		frame.lr = 0
368	} else {
369		var lrPtr uintptr
370		if usesLR {
371			if innermost && frame.sp < frame.fp || frame.lr == 0 {
372				lrPtr = frame.sp
373				frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr))
374			}
375		} else {
376			if frame.lr == 0 {
377				lrPtr = frame.fp - goarch.PtrSize
378				frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr))
379			}
380		}
381	}
382
383	frame.varp = frame.fp
384	if !usesLR {
385		// On x86, call instruction pushes return PC before entering new function.
386		frame.varp -= goarch.PtrSize
387	}
388
389	// For architectures with frame pointers, if there's
390	// a frame, then there's a saved frame pointer here.
391	//
392	// NOTE: This code is not as general as it looks.
393	// On x86, the ABI is to save the frame pointer word at the
394	// top of the stack frame, so we have to back down over it.
395	// On arm64, the frame pointer should be at the bottom of
396	// the stack (with R29 (aka FP) = RSP), in which case we would
397	// not want to do the subtraction here. But we started out without
398	// any frame pointer, and when we wanted to add it, we didn't
399	// want to break all the assembly doing direct writes to 8(RSP)
400	// to set the first parameter to a called function.
401	// So we decided to write the FP link *below* the stack pointer
402	// (with R29 = RSP - 8 in Go functions).
403	// This is technically ABI-compatible but not standard.
404	// And it happens to end up mimicking the x86 layout.
405	// Other architectures may make different decisions.
406	if frame.varp > frame.sp && framepointer_enabled {
407		frame.varp -= goarch.PtrSize
408	}
409
410	frame.argp = frame.fp + sys.MinFrameSize
411
412	// Determine frame's 'continuation PC', where it can continue.
413	// Normally this is the return address on the stack, but if sigpanic
414	// is immediately below this function on the stack, then the frame
415	// stopped executing due to a trap, and frame.pc is probably not
416	// a safe point for looking up liveness information. In this panicking case,
417	// the function either doesn't return at all (if it has no defers or if the
418	// defers do not recover) or it returns from one of the calls to
419	// deferproc a second time (if the corresponding deferred func recovers).
420	// In the latter case, use a deferreturn call site as the continuation pc.
421	frame.continpc = frame.pc
422	if u.calleeFuncID == abi.FuncID_sigpanic {
423		if frame.fn.deferreturn != 0 {
424			frame.continpc = frame.fn.entry() + uintptr(frame.fn.deferreturn) + 1
425			// Note: this may perhaps keep return variables alive longer than
426			// strictly necessary, as we are using "function has a defer statement"
427			// as a proxy for "function actually deferred something". It seems
428			// to be a minor drawback. (We used to actually look through the
429			// gp._defer for a defer corresponding to this function, but that
430			// is hard to do with defer records on the stack during a stack copy.)
431			// Note: the +1 is to offset the -1 that
432			// stack.go:getStackMap does to back up a return
433			// address make sure the pc is in the CALL instruction.
434		} else {
435			frame.continpc = 0
436		}
437	}
438}
439
440func (u *unwinder) next() {
441	frame := &u.frame
442	f := frame.fn
443	gp := u.g.ptr()
444
445	// Do not unwind past the bottom of the stack.
446	if frame.lr == 0 {
447		u.finishInternal()
448		return
449	}
450	flr := findfunc(frame.lr)
451	if !flr.valid() {
452		// This happens if you get a profiling interrupt at just the wrong time.
453		// In that context it is okay to stop early.
454		// But if no error flags are set, we're doing a garbage collection and must
455		// get everything, so crash loudly.
456		fail := u.flags&(unwindPrintErrors|unwindSilentErrors) == 0
457		doPrint := u.flags&unwindSilentErrors == 0
458		if doPrint && gp.m.incgo && f.funcID == abi.FuncID_sigpanic {
459			// We can inject sigpanic
460			// calls directly into C code,
461			// in which case we'll see a C
462			// return PC. Don't complain.
463			doPrint = false
464		}
465		if fail || doPrint {
466			print("runtime: g ", gp.goid, ": unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n")
467			tracebackHexdump(gp.stack, frame, 0)
468		}
469		if fail {
470			throw("unknown caller pc")
471		}
472		frame.lr = 0
473		u.finishInternal()
474		return
475	}
476
477	if frame.pc == frame.lr && frame.sp == frame.fp {
478		// If the next frame is identical to the current frame, we cannot make progress.
479		print("runtime: traceback stuck. pc=", hex(frame.pc), " sp=", hex(frame.sp), "\n")
480		tracebackHexdump(gp.stack, frame, frame.sp)
481		throw("traceback stuck")
482	}
483
484	injectedCall := f.funcID == abi.FuncID_sigpanic || f.funcID == abi.FuncID_asyncPreempt || f.funcID == abi.FuncID_debugCallV2
485	if injectedCall {
486		u.flags |= unwindTrap
487	} else {
488		u.flags &^= unwindTrap
489	}
490
491	// Unwind to next frame.
492	u.calleeFuncID = f.funcID
493	frame.fn = flr
494	frame.pc = frame.lr
495	frame.lr = 0
496	frame.sp = frame.fp
497	frame.fp = 0
498
499	// On link register architectures, sighandler saves the LR on stack
500	// before faking a call.
501	if usesLR && injectedCall {
502		x := *(*uintptr)(unsafe.Pointer(frame.sp))
503		frame.sp += alignUp(sys.MinFrameSize, sys.StackAlign)
504		f = findfunc(frame.pc)
505		frame.fn = f
506		if !f.valid() {
507			frame.pc = x
508		} else if funcspdelta(f, frame.pc) == 0 {
509			frame.lr = x
510		}
511	}
512
513	u.resolveInternal(false, false)
514}
515
516// finishInternal is an unwinder-internal helper called after the stack has been
517// exhausted. It sets the unwinder to an invalid state and checks that it
518// successfully unwound the entire stack.
519func (u *unwinder) finishInternal() {
520	u.frame.pc = 0
521
522	// Note that panic != nil is okay here: there can be leftover panics,
523	// because the defers on the panic stack do not nest in frame order as
524	// they do on the defer stack. If you have:
525	//
526	//	frame 1 defers d1
527	//	frame 2 defers d2
528	//	frame 3 defers d3
529	//	frame 4 panics
530	//	frame 4's panic starts running defers
531	//	frame 5, running d3, defers d4
532	//	frame 5 panics
533	//	frame 5's panic starts running defers
534	//	frame 6, running d4, garbage collects
535	//	frame 6, running d2, garbage collects
536	//
537	// During the execution of d4, the panic stack is d4 -> d3, which
538	// is nested properly, and we'll treat frame 3 as resumable, because we
539	// can find d3. (And in fact frame 3 is resumable. If d4 recovers
540	// and frame 5 continues running, d3, d3 can recover and we'll
541	// resume execution in (returning from) frame 3.)
542	//
543	// During the execution of d2, however, the panic stack is d2 -> d3,
544	// which is inverted. The scan will match d2 to frame 2 but having
545	// d2 on the stack until then means it will not match d3 to frame 3.
546	// This is okay: if we're running d2, then all the defers after d2 have
547	// completed and their corresponding frames are dead. Not finding d3
548	// for frame 3 means we'll set frame 3's continpc == 0, which is correct
549	// (frame 3 is dead). At the end of the walk the panic stack can thus
550	// contain defers (d3 in this case) for dead frames. The inversion here
551	// always indicates a dead frame, and the effect of the inversion on the
552	// scan is to hide those dead frames, so the scan is still okay:
553	// what's left on the panic stack are exactly (and only) the dead frames.
554	//
555	// We require callback != nil here because only when callback != nil
556	// do we know that gentraceback is being called in a "must be correct"
557	// context as opposed to a "best effort" context. The tracebacks with
558	// callbacks only happen when everything is stopped nicely.
559	// At other times, such as when gathering a stack for a profiling signal
560	// or when printing a traceback during a crash, everything may not be
561	// stopped nicely, and the stack walk may not be able to complete.
562	gp := u.g.ptr()
563	if u.flags&(unwindPrintErrors|unwindSilentErrors) == 0 && u.frame.sp != gp.stktopsp {
564		print("runtime: g", gp.goid, ": frame.sp=", hex(u.frame.sp), " top=", hex(gp.stktopsp), "\n")
565		print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "\n")
566		throw("traceback did not unwind completely")
567	}
568}
569
570// symPC returns the PC that should be used for symbolizing the current frame.
571// Specifically, this is the PC of the last instruction executed in this frame.
572//
573// If this frame did a normal call, then frame.pc is a return PC, so this will
574// return frame.pc-1, which points into the CALL instruction. If the frame was
575// interrupted by a signal (e.g., profiler, segv, etc) then frame.pc is for the
576// trapped instruction, so this returns frame.pc. See issue #34123. Finally,
577// frame.pc can be at function entry when the frame is initialized without
578// actually running code, like in runtime.mstart, in which case this returns
579// frame.pc because that's the best we can do.
580func (u *unwinder) symPC() uintptr {
581	if u.flags&unwindTrap == 0 && u.frame.pc > u.frame.fn.entry() {
582		// Regular call.
583		return u.frame.pc - 1
584	}
585	// Trapping instruction or we're at the function entry point.
586	return u.frame.pc
587}
588
589// cgoCallers populates pcBuf with the cgo callers of the current frame using
590// the registered cgo unwinder. It returns the number of PCs written to pcBuf.
591// If the current frame is not a cgo frame or if there's no registered cgo
592// unwinder, it returns 0.
593func (u *unwinder) cgoCallers(pcBuf []uintptr) int {
594	if cgoTraceback == nil || u.frame.fn.funcID != abi.FuncID_cgocallback || u.cgoCtxt < 0 {
595		// We don't have a cgo unwinder (typical case), or we do but we're not
596		// in a cgo frame or we're out of cgo context.
597		return 0
598	}
599
600	ctxt := u.g.ptr().cgoCtxt[u.cgoCtxt]
601	u.cgoCtxt--
602	cgoContextPCs(ctxt, pcBuf)
603	for i, pc := range pcBuf {
604		if pc == 0 {
605			return i
606		}
607	}
608	return len(pcBuf)
609}
610
611// tracebackPCs populates pcBuf with the return addresses for each frame from u
612// and returns the number of PCs written to pcBuf. The returned PCs correspond
613// to "logical frames" rather than "physical frames"; that is if A is inlined
614// into B, this will still return a PCs for both A and B. This also includes PCs
615// generated by the cgo unwinder, if one is registered.
616//
617// If skip != 0, this skips this many logical frames.
618//
619// Callers should set the unwindSilentErrors flag on u.
620func tracebackPCs(u *unwinder, skip int, pcBuf []uintptr) int {
621	var cgoBuf [32]uintptr
622	n := 0
623	for ; n < len(pcBuf) && u.valid(); u.next() {
624		f := u.frame.fn
625		cgoN := u.cgoCallers(cgoBuf[:])
626
627		// TODO: Why does &u.cache cause u to escape? (Same in traceback2)
628		for iu, uf := newInlineUnwinder(f, u.symPC()); n < len(pcBuf) && uf.valid(); uf = iu.next(uf) {
629			sf := iu.srcFunc(uf)
630			if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(u.calleeFuncID) {
631				// ignore wrappers
632			} else if skip > 0 {
633				skip--
634			} else {
635				// Callers expect the pc buffer to contain return addresses
636				// and do the -1 themselves, so we add 1 to the call pc to
637				// create a "return pc". Since there is no actual call, here
638				// "return pc" just means a pc you subtract 1 from to get
639				// the pc of the "call". The actual no-op we insert may or
640				// may not be 1 byte.
641				pcBuf[n] = uf.pc + 1
642				n++
643			}
644			u.calleeFuncID = sf.funcID
645		}
646		// Add cgo frames (if we're done skipping over the requested number of
647		// Go frames).
648		if skip == 0 {
649			n += copy(pcBuf[n:], cgoBuf[:cgoN])
650		}
651	}
652	return n
653}
654
655// printArgs prints function arguments in traceback.
656func printArgs(f funcInfo, argp unsafe.Pointer, pc uintptr) {
657	p := (*[abi.TraceArgsMaxLen]uint8)(funcdata(f, abi.FUNCDATA_ArgInfo))
658	if p == nil {
659		return
660	}
661
662	liveInfo := funcdata(f, abi.FUNCDATA_ArgLiveInfo)
663	liveIdx := pcdatavalue(f, abi.PCDATA_ArgLiveIndex, pc)
664	startOffset := uint8(0xff) // smallest offset that needs liveness info (slots with a lower offset is always live)
665	if liveInfo != nil {
666		startOffset = *(*uint8)(liveInfo)
667	}
668
669	isLive := func(off, slotIdx uint8) bool {
670		if liveInfo == nil || liveIdx <= 0 {
671			return true // no liveness info, always live
672		}
673		if off < startOffset {
674			return true
675		}
676		bits := *(*uint8)(add(liveInfo, uintptr(liveIdx)+uintptr(slotIdx/8)))
677		return bits&(1<<(slotIdx%8)) != 0
678	}
679
680	print1 := func(off, sz, slotIdx uint8) {
681		x := readUnaligned64(add(argp, uintptr(off)))
682		// mask out irrelevant bits
683		if sz < 8 {
684			shift := 64 - sz*8
685			if goarch.BigEndian {
686				x = x >> shift
687			} else {
688				x = x << shift >> shift
689			}
690		}
691		print(hex(x))
692		if !isLive(off, slotIdx) {
693			print("?")
694		}
695	}
696
697	start := true
698	printcomma := func() {
699		if !start {
700			print(", ")
701		}
702	}
703	pi := 0
704	slotIdx := uint8(0) // register arg spill slot index
705printloop:
706	for {
707		o := p[pi]
708		pi++
709		switch o {
710		case abi.TraceArgsEndSeq:
711			break printloop
712		case abi.TraceArgsStartAgg:
713			printcomma()
714			print("{")
715			start = true
716			continue
717		case abi.TraceArgsEndAgg:
718			print("}")
719		case abi.TraceArgsDotdotdot:
720			printcomma()
721			print("...")
722		case abi.TraceArgsOffsetTooLarge:
723			printcomma()
724			print("_")
725		default:
726			printcomma()
727			sz := p[pi]
728			pi++
729			print1(o, sz, slotIdx)
730			if o >= startOffset {
731				slotIdx++
732			}
733		}
734		start = false
735	}
736}
737
738// funcNamePiecesForPrint returns the function name for printing to the user.
739// It returns three pieces so it doesn't need an allocation for string
740// concatenation.
741func funcNamePiecesForPrint(name string) (string, string, string) {
742	// Replace the shape name in generic function with "...".
743	i := bytealg.IndexByteString(name, '[')
744	if i < 0 {
745		return name, "", ""
746	}
747	j := len(name) - 1
748	for name[j] != ']' {
749		j--
750	}
751	if j <= i {
752		return name, "", ""
753	}
754	return name[:i], "[...]", name[j+1:]
755}
756
757// funcNameForPrint returns the function name for printing to the user.
758func funcNameForPrint(name string) string {
759	a, b, c := funcNamePiecesForPrint(name)
760	return a + b + c
761}
762
763// printFuncName prints a function name. name is the function name in
764// the binary's func data table.
765func printFuncName(name string) {
766	if name == "runtime.gopanic" {
767		print("panic")
768		return
769	}
770	a, b, c := funcNamePiecesForPrint(name)
771	print(a, b, c)
772}
773
774func printcreatedby(gp *g) {
775	// Show what created goroutine, except main goroutine (goid 1).
776	pc := gp.gopc
777	f := findfunc(pc)
778	if f.valid() && showframe(f.srcFunc(), gp, false, abi.FuncIDNormal) && gp.goid != 1 {
779		printcreatedby1(f, pc, gp.parentGoid)
780	}
781}
782
783func printcreatedby1(f funcInfo, pc uintptr, goid uint64) {
784	print("created by ")
785	printFuncName(funcname(f))
786	if goid != 0 {
787		print(" in goroutine ", goid)
788	}
789	print("\n")
790	tracepc := pc // back up to CALL instruction for funcline.
791	if pc > f.entry() {
792		tracepc -= sys.PCQuantum
793	}
794	file, line := funcline(f, tracepc)
795	print("\t", file, ":", line)
796	if pc > f.entry() {
797		print(" +", hex(pc-f.entry()))
798	}
799	print("\n")
800}
801
802func traceback(pc, sp, lr uintptr, gp *g) {
803	traceback1(pc, sp, lr, gp, 0)
804}
805
806// tracebacktrap is like traceback but expects that the PC and SP were obtained
807// from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or getcallerpc/getcallersp.
808// Because they are from a trap instead of from a saved pair,
809// the initial PC must not be rewound to the previous instruction.
810// (All the saved pairs record a PC that is a return address, so we
811// rewind it into the CALL instruction.)
812// If gp.m.libcall{g,pc,sp} information is available, it uses that information in preference to
813// the pc/sp/lr passed in.
814func tracebacktrap(pc, sp, lr uintptr, gp *g) {
815	if gp.m.libcallsp != 0 {
816		// We're in C code somewhere, traceback from the saved position.
817		traceback1(gp.m.libcallpc, gp.m.libcallsp, 0, gp.m.libcallg.ptr(), 0)
818		return
819	}
820	traceback1(pc, sp, lr, gp, unwindTrap)
821}
822
823func traceback1(pc, sp, lr uintptr, gp *g, flags unwindFlags) {
824	// If the goroutine is in cgo, and we have a cgo traceback, print that.
825	if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 {
826		// Lock cgoCallers so that a signal handler won't
827		// change it, copy the array, reset it, unlock it.
828		// We are locked to the thread and are not running
829		// concurrently with a signal handler.
830		// We just have to stop a signal handler from interrupting
831		// in the middle of our copy.
832		gp.m.cgoCallersUse.Store(1)
833		cgoCallers := *gp.m.cgoCallers
834		gp.m.cgoCallers[0] = 0
835		gp.m.cgoCallersUse.Store(0)
836
837		printCgoTraceback(&cgoCallers)
838	}
839
840	if readgstatus(gp)&^_Gscan == _Gsyscall {
841		// Override registers if blocked in system call.
842		pc = gp.syscallpc
843		sp = gp.syscallsp
844		flags &^= unwindTrap
845	}
846	if gp.m != nil && gp.m.vdsoSP != 0 {
847		// Override registers if running in VDSO. This comes after the
848		// _Gsyscall check to cover VDSO calls after entersyscall.
849		pc = gp.m.vdsoPC
850		sp = gp.m.vdsoSP
851		flags &^= unwindTrap
852	}
853
854	// Print traceback.
855	//
856	// We print the first tracebackInnerFrames frames, and the last
857	// tracebackOuterFrames frames. There are many possible approaches to this.
858	// There are various complications to this:
859	//
860	// - We'd prefer to walk the stack once because in really bad situations
861	//   traceback may crash (and we want as much output as possible) or the stack
862	//   may be changing.
863	//
864	// - Each physical frame can represent several logical frames, so we might
865	//   have to pause in the middle of a physical frame and pick up in the middle
866	//   of a physical frame.
867	//
868	// - The cgo symbolizer can expand a cgo PC to more than one logical frame,
869	//   and involves juggling state on the C side that we don't manage. Since its
870	//   expansion state is managed on the C side, we can't capture the expansion
871	//   state part way through, and because the output strings are managed on the
872	//   C side, we can't capture the output. Thus, our only choice is to replay a
873	//   whole expansion, potentially discarding some of it.
874	//
875	// Rejected approaches:
876	//
877	// - Do two passes where the first pass just counts and the second pass does
878	//   all the printing. This is undesirable if the stack is corrupted or changing
879	//   because we won't see a partial stack if we panic.
880	//
881	// - Keep a ring buffer of the last N logical frames and use this to print
882	//   the bottom frames once we reach the end of the stack. This works, but
883	//   requires keeping a surprising amount of state on the stack, and we have
884	//   to run the cgo symbolizer twice—once to count frames, and a second to
885	//   print them—since we can't retain the strings it returns.
886	//
887	// Instead, we print the outer frames, and if we reach that limit, we clone
888	// the unwinder, count the remaining frames, and then skip forward and
889	// finish printing from the clone. This makes two passes over the outer part
890	// of the stack, but the single pass over the inner part ensures that's
891	// printed immediately and not revisited. It keeps minimal state on the
892	// stack. And through a combination of skip counts and limits, we can do all
893	// of the steps we need with a single traceback printer implementation.
894	//
895	// We could be more lax about exactly how many frames we print, for example
896	// always stopping and resuming on physical frame boundaries, or at least
897	// cgo expansion boundaries. It's not clear that's much simpler.
898	flags |= unwindPrintErrors
899	var u unwinder
900	tracebackWithRuntime := func(showRuntime bool) int {
901		const maxInt int = 0x7fffffff
902		u.initAt(pc, sp, lr, gp, flags)
903		n, lastN := traceback2(&u, showRuntime, 0, tracebackInnerFrames)
904		if n < tracebackInnerFrames {
905			// We printed the whole stack.
906			return n
907		}
908		// Clone the unwinder and figure out how many frames are left. This
909		// count will include any logical frames already printed for u's current
910		// physical frame.
911		u2 := u
912		remaining, _ := traceback2(&u, showRuntime, maxInt, 0)
913		elide := remaining - lastN - tracebackOuterFrames
914		if elide > 0 {
915			print("...", elide, " frames elided...\n")
916			traceback2(&u2, showRuntime, lastN+elide, tracebackOuterFrames)
917		} else if elide <= 0 {
918			// There are tracebackOuterFrames or fewer frames left to print.
919			// Just print the rest of the stack.
920			traceback2(&u2, showRuntime, lastN, tracebackOuterFrames)
921		}
922		return n
923	}
924	// By default, omits runtime frames. If that means we print nothing at all,
925	// repeat forcing all frames printed.
926	if tracebackWithRuntime(false) == 0 {
927		tracebackWithRuntime(true)
928	}
929	printcreatedby(gp)
930
931	if gp.ancestors == nil {
932		return
933	}
934	for _, ancestor := range *gp.ancestors {
935		printAncestorTraceback(ancestor)
936	}
937}
938
939// traceback2 prints a stack trace starting at u. It skips the first "skip"
940// logical frames, after which it prints at most "max" logical frames. It
941// returns n, which is the number of logical frames skipped and printed, and
942// lastN, which is the number of logical frames skipped or printed just in the
943// physical frame that u references.
944func traceback2(u *unwinder, showRuntime bool, skip, max int) (n, lastN int) {
945	// commitFrame commits to a logical frame and returns whether this frame
946	// should be printed and whether iteration should stop.
947	commitFrame := func() (pr, stop bool) {
948		if skip == 0 && max == 0 {
949			// Stop
950			return false, true
951		}
952		n++
953		lastN++
954		if skip > 0 {
955			// Skip
956			skip--
957			return false, false
958		}
959		// Print
960		max--
961		return true, false
962	}
963
964	gp := u.g.ptr()
965	level, _, _ := gotraceback()
966	var cgoBuf [32]uintptr
967	for ; u.valid(); u.next() {
968		lastN = 0
969		f := u.frame.fn
970		for iu, uf := newInlineUnwinder(f, u.symPC()); uf.valid(); uf = iu.next(uf) {
971			sf := iu.srcFunc(uf)
972			callee := u.calleeFuncID
973			u.calleeFuncID = sf.funcID
974			if !(showRuntime || showframe(sf, gp, n == 0, callee)) {
975				continue
976			}
977
978			if pr, stop := commitFrame(); stop {
979				return
980			} else if !pr {
981				continue
982			}
983
984			name := sf.name()
985			file, line := iu.fileLine(uf)
986			// Print during crash.
987			//	main(0x1, 0x2, 0x3)
988			//		/home/rsc/go/src/runtime/x.go:23 +0xf
989			//
990			printFuncName(name)
991			print("(")
992			if iu.isInlined(uf) {
993				print("...")
994			} else {
995				argp := unsafe.Pointer(u.frame.argp)
996				printArgs(f, argp, u.symPC())
997			}
998			print(")\n")
999			print("\t", file, ":", line)
1000			if !iu.isInlined(uf) {
1001				if u.frame.pc > f.entry() {
1002					print(" +", hex(u.frame.pc-f.entry()))
1003				}
1004				if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 {
1005					print(" fp=", hex(u.frame.fp), " sp=", hex(u.frame.sp), " pc=", hex(u.frame.pc))
1006				}
1007			}
1008			print("\n")
1009		}
1010
1011		// Print cgo frames.
1012		if cgoN := u.cgoCallers(cgoBuf[:]); cgoN > 0 {
1013			var arg cgoSymbolizerArg
1014			anySymbolized := false
1015			stop := false
1016			for _, pc := range cgoBuf[:cgoN] {
1017				if cgoSymbolizer == nil {
1018					if pr, stop := commitFrame(); stop {
1019						break
1020					} else if pr {
1021						print("non-Go function at pc=", hex(pc), "\n")
1022					}
1023				} else {
1024					stop = printOneCgoTraceback(pc, commitFrame, &arg)
1025					anySymbolized = true
1026					if stop {
1027						break
1028					}
1029				}
1030			}
1031			if anySymbolized {
1032				// Free symbolization state.
1033				arg.pc = 0
1034				callCgoSymbolizer(&arg)
1035			}
1036			if stop {
1037				return
1038			}
1039		}
1040	}
1041	return n, 0
1042}
1043
1044// printAncestorTraceback prints the traceback of the given ancestor.
1045// TODO: Unify this with gentraceback and CallersFrames.
1046func printAncestorTraceback(ancestor ancestorInfo) {
1047	print("[originating from goroutine ", ancestor.goid, "]:\n")
1048	for fidx, pc := range ancestor.pcs {
1049		f := findfunc(pc) // f previously validated
1050		if showfuncinfo(f.srcFunc(), fidx == 0, abi.FuncIDNormal) {
1051			printAncestorTracebackFuncInfo(f, pc)
1052		}
1053	}
1054	if len(ancestor.pcs) == tracebackInnerFrames {
1055		print("...additional frames elided...\n")
1056	}
1057	// Show what created goroutine, except main goroutine (goid 1).
1058	f := findfunc(ancestor.gopc)
1059	if f.valid() && showfuncinfo(f.srcFunc(), false, abi.FuncIDNormal) && ancestor.goid != 1 {
1060		// In ancestor mode, we'll already print the goroutine ancestor.
1061		// Pass 0 for the goid parameter so we don't print it again.
1062		printcreatedby1(f, ancestor.gopc, 0)
1063	}
1064}
1065
1066// printAncestorTracebackFuncInfo prints the given function info at a given pc
1067// within an ancestor traceback. The precision of this info is reduced
1068// due to only have access to the pcs at the time of the caller
1069// goroutine being created.
1070func printAncestorTracebackFuncInfo(f funcInfo, pc uintptr) {
1071	u, uf := newInlineUnwinder(f, pc)
1072	file, line := u.fileLine(uf)
1073	printFuncName(u.srcFunc(uf).name())
1074	print("(...)\n")
1075	print("\t", file, ":", line)
1076	if pc > f.entry() {
1077		print(" +", hex(pc-f.entry()))
1078	}
1079	print("\n")
1080}
1081
1082// callers should be an internal detail,
1083// (and is almost identical to Callers),
1084// but widely used packages access it using linkname.
1085// Notable members of the hall of shame include:
1086//   - github.com/phuslu/log
1087//
1088// Do not remove or change the type signature.
1089// See go.dev/issue/67401.
1090//
1091//go:linkname callers
1092func callers(skip int, pcbuf []uintptr) int {
1093	sp := getcallersp()
1094	pc := getcallerpc()
1095	gp := getg()
1096	var n int
1097	systemstack(func() {
1098		var u unwinder
1099		u.initAt(pc, sp, 0, gp, unwindSilentErrors)
1100		n = tracebackPCs(&u, skip, pcbuf)
1101	})
1102	return n
1103}
1104
1105func gcallers(gp *g, skip int, pcbuf []uintptr) int {
1106	var u unwinder
1107	u.init(gp, unwindSilentErrors)
1108	return tracebackPCs(&u, skip, pcbuf)
1109}
1110
1111// showframe reports whether the frame with the given characteristics should
1112// be printed during a traceback.
1113func showframe(sf srcFunc, gp *g, firstFrame bool, calleeID abi.FuncID) bool {
1114	mp := getg().m
1115	if mp.throwing >= throwTypeRuntime && gp != nil && (gp == mp.curg || gp == mp.caughtsig.ptr()) {
1116		return true
1117	}
1118	return showfuncinfo(sf, firstFrame, calleeID)
1119}
1120
1121// showfuncinfo reports whether a function with the given characteristics should
1122// be printed during a traceback.
1123func showfuncinfo(sf srcFunc, firstFrame bool, calleeID abi.FuncID) bool {
1124	level, _, _ := gotraceback()
1125	if level > 1 {
1126		// Show all frames.
1127		return true
1128	}
1129
1130	if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(calleeID) {
1131		return false
1132	}
1133
1134	name := sf.name()
1135
1136	// Special case: always show runtime.gopanic frame
1137	// in the middle of a stack trace, so that we can
1138	// see the boundary between ordinary code and
1139	// panic-induced deferred code.
1140	// See golang.org/issue/5832.
1141	if name == "runtime.gopanic" && !firstFrame {
1142		return true
1143	}
1144
1145	return bytealg.IndexByteString(name, '.') >= 0 && (!stringslite.HasPrefix(name, "runtime.") || isExportedRuntime(name))
1146}
1147
1148// isExportedRuntime reports whether name is an exported runtime function.
1149// It is only for runtime functions, so ASCII A-Z is fine.
1150func isExportedRuntime(name string) bool {
1151	// Check and remove package qualifier.
1152	n := len("runtime.")
1153	if len(name) <= n || name[:n] != "runtime." {
1154		return false
1155	}
1156	name = name[n:]
1157	rcvr := ""
1158
1159	// Extract receiver type, if any.
1160	// For example, runtime.(*Func).Entry
1161	i := len(name) - 1
1162	for i >= 0 && name[i] != '.' {
1163		i--
1164	}
1165	if i >= 0 {
1166		rcvr = name[:i]
1167		name = name[i+1:]
1168		// Remove parentheses and star for pointer receivers.
1169		if len(rcvr) >= 3 && rcvr[0] == '(' && rcvr[1] == '*' && rcvr[len(rcvr)-1] == ')' {
1170			rcvr = rcvr[2 : len(rcvr)-1]
1171		}
1172	}
1173
1174	// Exported functions and exported methods on exported types.
1175	return len(name) > 0 && 'A' <= name[0] && name[0] <= 'Z' && (len(rcvr) == 0 || 'A' <= rcvr[0] && rcvr[0] <= 'Z')
1176}
1177
1178// elideWrapperCalling reports whether a wrapper function that called
1179// function id should be elided from stack traces.
1180func elideWrapperCalling(id abi.FuncID) bool {
1181	// If the wrapper called a panic function instead of the
1182	// wrapped function, we want to include it in stacks.
1183	return !(id == abi.FuncID_gopanic || id == abi.FuncID_sigpanic || id == abi.FuncID_panicwrap)
1184}
1185
1186var gStatusStrings = [...]string{
1187	_Gidle:      "idle",
1188	_Grunnable:  "runnable",
1189	_Grunning:   "running",
1190	_Gsyscall:   "syscall",
1191	_Gwaiting:   "waiting",
1192	_Gdead:      "dead",
1193	_Gcopystack: "copystack",
1194	_Gpreempted: "preempted",
1195}
1196
1197func goroutineheader(gp *g) {
1198	level, _, _ := gotraceback()
1199
1200	gpstatus := readgstatus(gp)
1201
1202	isScan := gpstatus&_Gscan != 0
1203	gpstatus &^= _Gscan // drop the scan bit
1204
1205	// Basic string status
1206	var status string
1207	if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
1208		status = gStatusStrings[gpstatus]
1209	} else {
1210		status = "???"
1211	}
1212
1213	// Override.
1214	if gpstatus == _Gwaiting && gp.waitreason != waitReasonZero {
1215		status = gp.waitreason.String()
1216	}
1217
1218	// approx time the G is blocked, in minutes
1219	var waitfor int64
1220	if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
1221		waitfor = (nanotime() - gp.waitsince) / 60e9
1222	}
1223	print("goroutine ", gp.goid)
1224	if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 {
1225		print(" gp=", gp)
1226		if gp.m != nil {
1227			print(" m=", gp.m.id, " mp=", gp.m)
1228		} else {
1229			print(" m=nil")
1230		}
1231	}
1232	print(" [", status)
1233	if isScan {
1234		print(" (scan)")
1235	}
1236	if waitfor >= 1 {
1237		print(", ", waitfor, " minutes")
1238	}
1239	if gp.lockedm != 0 {
1240		print(", locked to thread")
1241	}
1242	print("]:\n")
1243}
1244
1245func tracebackothers(me *g) {
1246	level, _, _ := gotraceback()
1247
1248	// Show the current goroutine first, if we haven't already.
1249	curgp := getg().m.curg
1250	if curgp != nil && curgp != me {
1251		print("\n")
1252		goroutineheader(curgp)
1253		traceback(^uintptr(0), ^uintptr(0), 0, curgp)
1254	}
1255
1256	// We can't call locking forEachG here because this may be during fatal
1257	// throw/panic, where locking could be out-of-order or a direct
1258	// deadlock.
1259	//
1260	// Instead, use forEachGRace, which requires no locking. We don't lock
1261	// against concurrent creation of new Gs, but even with allglock we may
1262	// miss Gs created after this loop.
1263	forEachGRace(func(gp *g) {
1264		if gp == me || gp == curgp || readgstatus(gp) == _Gdead || isSystemGoroutine(gp, false) && level < 2 {
1265			return
1266		}
1267		print("\n")
1268		goroutineheader(gp)
1269		// Note: gp.m == getg().m occurs when tracebackothers is called
1270		// from a signal handler initiated during a systemstack call.
1271		// The original G is still in the running state, and we want to
1272		// print its stack.
1273		if gp.m != getg().m && readgstatus(gp)&^_Gscan == _Grunning {
1274			print("\tgoroutine running on other thread; stack unavailable\n")
1275			printcreatedby(gp)
1276		} else {
1277			traceback(^uintptr(0), ^uintptr(0), 0, gp)
1278		}
1279	})
1280}
1281
1282// tracebackHexdump hexdumps part of stk around frame.sp and frame.fp
1283// for debugging purposes. If the address bad is included in the
1284// hexdumped range, it will mark it as well.
1285func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) {
1286	const expand = 32 * goarch.PtrSize
1287	const maxExpand = 256 * goarch.PtrSize
1288	// Start around frame.sp.
1289	lo, hi := frame.sp, frame.sp
1290	// Expand to include frame.fp.
1291	if frame.fp != 0 && frame.fp < lo {
1292		lo = frame.fp
1293	}
1294	if frame.fp != 0 && frame.fp > hi {
1295		hi = frame.fp
1296	}
1297	// Expand a bit more.
1298	lo, hi = lo-expand, hi+expand
1299	// But don't go too far from frame.sp.
1300	if lo < frame.sp-maxExpand {
1301		lo = frame.sp - maxExpand
1302	}
1303	if hi > frame.sp+maxExpand {
1304		hi = frame.sp + maxExpand
1305	}
1306	// And don't go outside the stack bounds.
1307	if lo < stk.lo {
1308		lo = stk.lo
1309	}
1310	if hi > stk.hi {
1311		hi = stk.hi
1312	}
1313
1314	// Print the hex dump.
1315	print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n")
1316	hexdumpWords(lo, hi, func(p uintptr) byte {
1317		switch p {
1318		case frame.fp:
1319			return '>'
1320		case frame.sp:
1321			return '<'
1322		case bad:
1323			return '!'
1324		}
1325		return 0
1326	})
1327}
1328
1329// isSystemGoroutine reports whether the goroutine g must be omitted
1330// in stack dumps and deadlock detector. This is any goroutine that
1331// starts at a runtime.* entry point, except for runtime.main,
1332// runtime.handleAsyncEvent (wasm only) and sometimes runtime.runfinq.
1333//
1334// If fixed is true, any goroutine that can vary between user and
1335// system (that is, the finalizer goroutine) is considered a user
1336// goroutine.
1337func isSystemGoroutine(gp *g, fixed bool) bool {
1338	// Keep this in sync with internal/trace.IsSystemGoroutine.
1339	f := findfunc(gp.startpc)
1340	if !f.valid() {
1341		return false
1342	}
1343	if f.funcID == abi.FuncID_runtime_main || f.funcID == abi.FuncID_corostart || f.funcID == abi.FuncID_handleAsyncEvent {
1344		return false
1345	}
1346	if f.funcID == abi.FuncID_runfinq {
1347		// We include the finalizer goroutine if it's calling
1348		// back into user code.
1349		if fixed {
1350			// This goroutine can vary. In fixed mode,
1351			// always consider it a user goroutine.
1352			return false
1353		}
1354		return fingStatus.Load()&fingRunningFinalizer == 0
1355	}
1356	return stringslite.HasPrefix(funcname(f), "runtime.")
1357}
1358
1359// SetCgoTraceback records three C functions to use to gather
1360// traceback information from C code and to convert that traceback
1361// information into symbolic information. These are used when printing
1362// stack traces for a program that uses cgo.
1363//
1364// The traceback and context functions may be called from a signal
1365// handler, and must therefore use only async-signal safe functions.
1366// The symbolizer function may be called while the program is
1367// crashing, and so must be cautious about using memory.  None of the
1368// functions may call back into Go.
1369//
1370// The context function will be called with a single argument, a
1371// pointer to a struct:
1372//
1373//	struct {
1374//		Context uintptr
1375//	}
1376//
1377// In C syntax, this struct will be
1378//
1379//	struct {
1380//		uintptr_t Context;
1381//	};
1382//
1383// If the Context field is 0, the context function is being called to
1384// record the current traceback context. It should record in the
1385// Context field whatever information is needed about the current
1386// point of execution to later produce a stack trace, probably the
1387// stack pointer and PC. In this case the context function will be
1388// called from C code.
1389//
1390// If the Context field is not 0, then it is a value returned by a
1391// previous call to the context function. This case is called when the
1392// context is no longer needed; that is, when the Go code is returning
1393// to its C code caller. This permits the context function to release
1394// any associated resources.
1395//
1396// While it would be correct for the context function to record a
1397// complete a stack trace whenever it is called, and simply copy that
1398// out in the traceback function, in a typical program the context
1399// function will be called many times without ever recording a
1400// traceback for that context. Recording a complete stack trace in a
1401// call to the context function is likely to be inefficient.
1402//
1403// The traceback function will be called with a single argument, a
1404// pointer to a struct:
1405//
1406//	struct {
1407//		Context    uintptr
1408//		SigContext uintptr
1409//		Buf        *uintptr
1410//		Max        uintptr
1411//	}
1412//
1413// In C syntax, this struct will be
1414//
1415//	struct {
1416//		uintptr_t  Context;
1417//		uintptr_t  SigContext;
1418//		uintptr_t* Buf;
1419//		uintptr_t  Max;
1420//	};
1421//
1422// The Context field will be zero to gather a traceback from the
1423// current program execution point. In this case, the traceback
1424// function will be called from C code.
1425//
1426// Otherwise Context will be a value previously returned by a call to
1427// the context function. The traceback function should gather a stack
1428// trace from that saved point in the program execution. The traceback
1429// function may be called from an execution thread other than the one
1430// that recorded the context, but only when the context is known to be
1431// valid and unchanging. The traceback function may also be called
1432// deeper in the call stack on the same thread that recorded the
1433// context. The traceback function may be called multiple times with
1434// the same Context value; it will usually be appropriate to cache the
1435// result, if possible, the first time this is called for a specific
1436// context value.
1437//
1438// If the traceback function is called from a signal handler on a Unix
1439// system, SigContext will be the signal context argument passed to
1440// the signal handler (a C ucontext_t* cast to uintptr_t). This may be
1441// used to start tracing at the point where the signal occurred. If
1442// the traceback function is not called from a signal handler,
1443// SigContext will be zero.
1444//
1445// Buf is where the traceback information should be stored. It should
1446// be PC values, such that Buf[0] is the PC of the caller, Buf[1] is
1447// the PC of that function's caller, and so on.  Max is the maximum
1448// number of entries to store.  The function should store a zero to
1449// indicate the top of the stack, or that the caller is on a different
1450// stack, presumably a Go stack.
1451//
1452// Unlike runtime.Callers, the PC values returned should, when passed
1453// to the symbolizer function, return the file/line of the call
1454// instruction.  No additional subtraction is required or appropriate.
1455//
1456// On all platforms, the traceback function is invoked when a call from
1457// Go to C to Go requests a stack trace. On linux/amd64, linux/ppc64le,
1458// linux/arm64, and freebsd/amd64, the traceback function is also invoked
1459// when a signal is received by a thread that is executing a cgo call.
1460// The traceback function should not make assumptions about when it is
1461// called, as future versions of Go may make additional calls.
1462//
1463// The symbolizer function will be called with a single argument, a
1464// pointer to a struct:
1465//
1466//	struct {
1467//		PC      uintptr // program counter to fetch information for
1468//		File    *byte   // file name (NUL terminated)
1469//		Lineno  uintptr // line number
1470//		Func    *byte   // function name (NUL terminated)
1471//		Entry   uintptr // function entry point
1472//		More    uintptr // set non-zero if more info for this PC
1473//		Data    uintptr // unused by runtime, available for function
1474//	}
1475//
1476// In C syntax, this struct will be
1477//
1478//	struct {
1479//		uintptr_t PC;
1480//		char*     File;
1481//		uintptr_t Lineno;
1482//		char*     Func;
1483//		uintptr_t Entry;
1484//		uintptr_t More;
1485//		uintptr_t Data;
1486//	};
1487//
1488// The PC field will be a value returned by a call to the traceback
1489// function.
1490//
1491// The first time the function is called for a particular traceback,
1492// all the fields except PC will be 0. The function should fill in the
1493// other fields if possible, setting them to 0/nil if the information
1494// is not available. The Data field may be used to store any useful
1495// information across calls. The More field should be set to non-zero
1496// if there is more information for this PC, zero otherwise. If More
1497// is set non-zero, the function will be called again with the same
1498// PC, and may return different information (this is intended for use
1499// with inlined functions). If More is zero, the function will be
1500// called with the next PC value in the traceback. When the traceback
1501// is complete, the function will be called once more with PC set to
1502// zero; this may be used to free any information. Each call will
1503// leave the fields of the struct set to the same values they had upon
1504// return, except for the PC field when the More field is zero. The
1505// function must not keep a copy of the struct pointer between calls.
1506//
1507// When calling SetCgoTraceback, the version argument is the version
1508// number of the structs that the functions expect to receive.
1509// Currently this must be zero.
1510//
1511// The symbolizer function may be nil, in which case the results of
1512// the traceback function will be displayed as numbers. If the
1513// traceback function is nil, the symbolizer function will never be
1514// called. The context function may be nil, in which case the
1515// traceback function will only be called with the context field set
1516// to zero.  If the context function is nil, then calls from Go to C
1517// to Go will not show a traceback for the C portion of the call stack.
1518//
1519// SetCgoTraceback should be called only once, ideally from an init function.
1520func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) {
1521	if version != 0 {
1522		panic("unsupported version")
1523	}
1524
1525	if cgoTraceback != nil && cgoTraceback != traceback ||
1526		cgoContext != nil && cgoContext != context ||
1527		cgoSymbolizer != nil && cgoSymbolizer != symbolizer {
1528		panic("call SetCgoTraceback only once")
1529	}
1530
1531	cgoTraceback = traceback
1532	cgoContext = context
1533	cgoSymbolizer = symbolizer
1534
1535	// The context function is called when a C function calls a Go
1536	// function. As such it is only called by C code in runtime/cgo.
1537	if _cgo_set_context_function != nil {
1538		cgocall(_cgo_set_context_function, context)
1539	}
1540}
1541
1542var cgoTraceback unsafe.Pointer
1543var cgoContext unsafe.Pointer
1544var cgoSymbolizer unsafe.Pointer
1545
1546// cgoTracebackArg is the type passed to cgoTraceback.
1547type cgoTracebackArg struct {
1548	context    uintptr
1549	sigContext uintptr
1550	buf        *uintptr
1551	max        uintptr
1552}
1553
1554// cgoContextArg is the type passed to the context function.
1555type cgoContextArg struct {
1556	context uintptr
1557}
1558
1559// cgoSymbolizerArg is the type passed to cgoSymbolizer.
1560type cgoSymbolizerArg struct {
1561	pc       uintptr
1562	file     *byte
1563	lineno   uintptr
1564	funcName *byte
1565	entry    uintptr
1566	more     uintptr
1567	data     uintptr
1568}
1569
1570// printCgoTraceback prints a traceback of callers.
1571func printCgoTraceback(callers *cgoCallers) {
1572	if cgoSymbolizer == nil {
1573		for _, c := range callers {
1574			if c == 0 {
1575				break
1576			}
1577			print("non-Go function at pc=", hex(c), "\n")
1578		}
1579		return
1580	}
1581
1582	commitFrame := func() (pr, stop bool) { return true, false }
1583	var arg cgoSymbolizerArg
1584	for _, c := range callers {
1585		if c == 0 {
1586			break
1587		}
1588		printOneCgoTraceback(c, commitFrame, &arg)
1589	}
1590	arg.pc = 0
1591	callCgoSymbolizer(&arg)
1592}
1593
1594// printOneCgoTraceback prints the traceback of a single cgo caller.
1595// This can print more than one line because of inlining.
1596// It returns the "stop" result of commitFrame.
1597func printOneCgoTraceback(pc uintptr, commitFrame func() (pr, stop bool), arg *cgoSymbolizerArg) bool {
1598	arg.pc = pc
1599	for {
1600		if pr, stop := commitFrame(); stop {
1601			return true
1602		} else if !pr {
1603			continue
1604		}
1605
1606		callCgoSymbolizer(arg)
1607		if arg.funcName != nil {
1608			// Note that we don't print any argument
1609			// information here, not even parentheses.
1610			// The symbolizer must add that if appropriate.
1611			println(gostringnocopy(arg.funcName))
1612		} else {
1613			println("non-Go function")
1614		}
1615		print("\t")
1616		if arg.file != nil {
1617			print(gostringnocopy(arg.file), ":", arg.lineno, " ")
1618		}
1619		print("pc=", hex(pc), "\n")
1620		if arg.more == 0 {
1621			return false
1622		}
1623	}
1624}
1625
1626// callCgoSymbolizer calls the cgoSymbolizer function.
1627func callCgoSymbolizer(arg *cgoSymbolizerArg) {
1628	call := cgocall
1629	if panicking.Load() > 0 || getg().m.curg != getg() {
1630		// We do not want to call into the scheduler when panicking
1631		// or when on the system stack.
1632		call = asmcgocall
1633	}
1634	if msanenabled {
1635		msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{}))
1636	}
1637	if asanenabled {
1638		asanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{}))
1639	}
1640	call(cgoSymbolizer, noescape(unsafe.Pointer(arg)))
1641}
1642
1643// cgoContextPCs gets the PC values from a cgo traceback.
1644func cgoContextPCs(ctxt uintptr, buf []uintptr) {
1645	if cgoTraceback == nil {
1646		return
1647	}
1648	call := cgocall
1649	if panicking.Load() > 0 || getg().m.curg != getg() {
1650		// We do not want to call into the scheduler when panicking
1651		// or when on the system stack.
1652		call = asmcgocall
1653	}
1654	arg := cgoTracebackArg{
1655		context: ctxt,
1656		buf:     (*uintptr)(noescape(unsafe.Pointer(&buf[0]))),
1657		max:     uintptr(len(buf)),
1658	}
1659	if msanenabled {
1660		msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg))
1661	}
1662	if asanenabled {
1663		asanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg))
1664	}
1665	call(cgoTraceback, noescape(unsafe.Pointer(&arg)))
1666}
1667