Source file src/runtime/traceback.go

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

View as plain text