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

View as plain text