...

Source file src/runtime/traceback.go

Documentation: runtime

     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/bytealg"
     9  	"internal/goarch"
    10  	"runtime/internal/atomic"
    11  	"runtime/internal/sys"
    12  	"unsafe"
    13  )
    14  
    15  // The code in this file implements stack trace walking for all architectures.
    16  // The most important fact about a given architecture is whether it uses a link register.
    17  // On systems with link registers, the prologue for a non-leaf function stores the
    18  // incoming value of LR at the bottom of the newly allocated stack frame.
    19  // On systems without link registers (x86), the architecture pushes a return PC during
    20  // the call instruction, so the return PC ends up above the stack frame.
    21  // In this file, the return PC is always called LR, no matter how it was found.
    22  
    23  const usesLR = sys.MinFrameSize > 0
    24  
    25  // Generic traceback. Handles runtime stack prints (pcbuf == nil),
    26  // the runtime.Callers function (pcbuf != nil), as well as the garbage
    27  // collector (callback != nil).  A little clunky to merge these, but avoids
    28  // duplicating the code and all its subtlety.
    29  //
    30  // The skip argument is only valid with pcbuf != nil and counts the number
    31  // of logical frames to skip rather than physical frames (with inlining, a
    32  // PC in pcbuf can represent multiple calls).
    33  func gentraceback(pc0, sp0, lr0 uintptr, gp *g, skip int, pcbuf *uintptr, max int, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer, flags uint) int {
    34  	if skip > 0 && callback != nil {
    35  		throw("gentraceback callback cannot be used with non-zero skip")
    36  	}
    37  
    38  	// Don't call this "g"; it's too easy get "g" and "gp" confused.
    39  	if ourg := getg(); ourg == gp && ourg == ourg.m.curg {
    40  		// The starting sp has been passed in as a uintptr, and the caller may
    41  		// have other uintptr-typed stack references as well.
    42  		// If during one of the calls that got us here or during one of the
    43  		// callbacks below the stack must be grown, all these uintptr references
    44  		// to the stack will not be updated, and gentraceback will continue
    45  		// to inspect the old stack memory, which may no longer be valid.
    46  		// Even if all the variables were updated correctly, it is not clear that
    47  		// we want to expose a traceback that begins on one stack and ends
    48  		// on another stack. That could confuse callers quite a bit.
    49  		// Instead, we require that gentraceback and any other function that
    50  		// accepts an sp for the current goroutine (typically obtained by
    51  		// calling getcallersp) must not run on that goroutine's stack but
    52  		// instead on the g0 stack.
    53  		throw("gentraceback cannot trace user goroutine on its own stack")
    54  	}
    55  	level, _, _ := gotraceback()
    56  
    57  	var ctxt *funcval // Context pointer for unstarted goroutines. See issue #25897.
    58  
    59  	if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp.
    60  		if gp.syscallsp != 0 {
    61  			pc0 = gp.syscallpc
    62  			sp0 = gp.syscallsp
    63  			if usesLR {
    64  				lr0 = 0
    65  			}
    66  		} else {
    67  			pc0 = gp.sched.pc
    68  			sp0 = gp.sched.sp
    69  			if usesLR {
    70  				lr0 = gp.sched.lr
    71  			}
    72  			ctxt = (*funcval)(gp.sched.ctxt)
    73  		}
    74  	}
    75  
    76  	nprint := 0
    77  	var frame stkframe
    78  	frame.pc = pc0
    79  	frame.sp = sp0
    80  	if usesLR {
    81  		frame.lr = lr0
    82  	}
    83  	waspanic := false
    84  	cgoCtxt := gp.cgoCtxt
    85  	stack := gp.stack
    86  	printing := pcbuf == nil && callback == nil
    87  
    88  	// If the PC is zero, it's likely a nil function call.
    89  	// Start in the caller's frame.
    90  	if frame.pc == 0 {
    91  		if usesLR {
    92  			frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp))
    93  			frame.lr = 0
    94  		} else {
    95  			frame.pc = uintptr(*(*uintptr)(unsafe.Pointer(frame.sp)))
    96  			frame.sp += goarch.PtrSize
    97  		}
    98  	}
    99  
   100  	// runtime/internal/atomic functions call into kernel helpers on
   101  	// arm < 7. See runtime/internal/atomic/sys_linux_arm.s.
   102  	//
   103  	// Start in the caller's frame.
   104  	if GOARCH == "arm" && goarm < 7 && GOOS == "linux" && frame.pc&0xffff0000 == 0xffff0000 {
   105  		// Note that the calls are simple BL without pushing the return
   106  		// address, so we use LR directly.
   107  		//
   108  		// The kernel helpers are frameless leaf functions, so SP and
   109  		// LR are not touched.
   110  		frame.pc = frame.lr
   111  		frame.lr = 0
   112  	}
   113  
   114  	f := findfunc(frame.pc)
   115  	if !f.valid() {
   116  		if callback != nil || printing {
   117  			print("runtime: g ", gp.goid, ": unknown pc ", hex(frame.pc), "\n")
   118  			tracebackHexdump(stack, &frame, 0)
   119  		}
   120  		if callback != nil {
   121  			throw("unknown pc")
   122  		}
   123  		return 0
   124  	}
   125  	frame.fn = f
   126  
   127  	var cache pcvalueCache
   128  
   129  	lastFuncID := funcID_normal
   130  	n := 0
   131  	for n < max {
   132  		// Typically:
   133  		//	pc is the PC of the running function.
   134  		//	sp is the stack pointer at that program counter.
   135  		//	fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown.
   136  		//	stk is the stack containing sp.
   137  		//	The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp.
   138  		f = frame.fn
   139  		if f.pcsp == 0 {
   140  			// No frame information, must be external function, like race support.
   141  			// See golang.org/issue/13568.
   142  			break
   143  		}
   144  
   145  		// Compute function info flags.
   146  		flag := f.flag
   147  		if f.funcID == funcID_cgocallback {
   148  			// cgocallback does write SP to switch from the g0 to the curg stack,
   149  			// but it carefully arranges that during the transition BOTH stacks
   150  			// have cgocallback frame valid for unwinding through.
   151  			// So we don't need to exclude it with the other SP-writing functions.
   152  			flag &^= funcFlag_SPWRITE
   153  		}
   154  		if frame.pc == pc0 && frame.sp == sp0 && pc0 == gp.syscallpc && sp0 == gp.syscallsp {
   155  			// Some Syscall functions write to SP, but they do so only after
   156  			// saving the entry PC/SP using entersyscall.
   157  			// Since we are using the entry PC/SP, the later SP write doesn't matter.
   158  			flag &^= funcFlag_SPWRITE
   159  		}
   160  
   161  		// Found an actual function.
   162  		// Derive frame pointer and link register.
   163  		if frame.fp == 0 {
   164  			// Jump over system stack transitions. If we're on g0 and there's a user
   165  			// goroutine, try to jump. Otherwise this is a regular call.
   166  			if flags&_TraceJumpStack != 0 && gp == gp.m.g0 && gp.m.curg != nil {
   167  				switch f.funcID {
   168  				case funcID_morestack:
   169  					// morestack does not return normally -- newstack()
   170  					// gogo's to curg.sched. Match that.
   171  					// This keeps morestack() from showing up in the backtrace,
   172  					// but that makes some sense since it'll never be returned
   173  					// to.
   174  					frame.pc = gp.m.curg.sched.pc
   175  					frame.fn = findfunc(frame.pc)
   176  					f = frame.fn
   177  					flag = f.flag
   178  					frame.lr = gp.m.curg.sched.lr
   179  					frame.sp = gp.m.curg.sched.sp
   180  					stack = gp.m.curg.stack
   181  					cgoCtxt = gp.m.curg.cgoCtxt
   182  				case funcID_systemstack:
   183  					// systemstack returns normally, so just follow the
   184  					// stack transition.
   185  					if usesLR && funcspdelta(f, frame.pc, &cache) == 0 {
   186  						// We're at the function prologue and the stack
   187  						// switch hasn't happened, or epilogue where we're
   188  						// about to return. Just unwind normally.
   189  						// Do this only on LR machines because on x86
   190  						// systemstack doesn't have an SP delta (the CALL
   191  						// instruction opens the frame), therefore no way
   192  						// to check.
   193  						flag &^= funcFlag_SPWRITE
   194  						break
   195  					}
   196  					frame.sp = gp.m.curg.sched.sp
   197  					stack = gp.m.curg.stack
   198  					cgoCtxt = gp.m.curg.cgoCtxt
   199  					flag &^= funcFlag_SPWRITE
   200  				}
   201  			}
   202  			frame.fp = frame.sp + uintptr(funcspdelta(f, frame.pc, &cache))
   203  			if !usesLR {
   204  				// On x86, call instruction pushes return PC before entering new function.
   205  				frame.fp += goarch.PtrSize
   206  			}
   207  		}
   208  		var flr funcInfo
   209  		if flag&funcFlag_TOPFRAME != 0 {
   210  			// This function marks the top of the stack. Stop the traceback.
   211  			frame.lr = 0
   212  			flr = funcInfo{}
   213  		} else if flag&funcFlag_SPWRITE != 0 && (callback == nil || n > 0) {
   214  			// The function we are in does a write to SP that we don't know
   215  			// how to encode in the spdelta table. Examples include context
   216  			// switch routines like runtime.gogo but also any code that switches
   217  			// to the g0 stack to run host C code. Since we can't reliably unwind
   218  			// the SP (we might not even be on the stack we think we are),
   219  			// we stop the traceback here.
   220  			// This only applies for profiling signals (callback == nil).
   221  			//
   222  			// For a GC stack traversal (callback != nil), we should only see
   223  			// a function when it has voluntarily preempted itself on entry
   224  			// during the stack growth check. In that case, the function has
   225  			// not yet had a chance to do any writes to SP and is safe to unwind.
   226  			// isAsyncSafePoint does not allow assembly functions to be async preempted,
   227  			// and preemptPark double-checks that SPWRITE functions are not async preempted.
   228  			// So for GC stack traversal we leave things alone (this if body does not execute for n == 0)
   229  			// at the bottom frame of the stack. But farther up the stack we'd better not
   230  			// find any.
   231  			if callback != nil {
   232  				println("traceback: unexpected SPWRITE function", funcname(f))
   233  				throw("traceback")
   234  			}
   235  			frame.lr = 0
   236  			flr = funcInfo{}
   237  		} else {
   238  			var lrPtr uintptr
   239  			if usesLR {
   240  				if n == 0 && frame.sp < frame.fp || frame.lr == 0 {
   241  					lrPtr = frame.sp
   242  					frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr))
   243  				}
   244  			} else {
   245  				if frame.lr == 0 {
   246  					lrPtr = frame.fp - goarch.PtrSize
   247  					frame.lr = uintptr(*(*uintptr)(unsafe.Pointer(lrPtr)))
   248  				}
   249  			}
   250  			flr = findfunc(frame.lr)
   251  			if !flr.valid() {
   252  				// This happens if you get a profiling interrupt at just the wrong time.
   253  				// In that context it is okay to stop early.
   254  				// But if callback is set, we're doing a garbage collection and must
   255  				// get everything, so crash loudly.
   256  				doPrint := printing
   257  				if doPrint && gp.m.incgo && f.funcID == funcID_sigpanic {
   258  					// We can inject sigpanic
   259  					// calls directly into C code,
   260  					// in which case we'll see a C
   261  					// return PC. Don't complain.
   262  					doPrint = false
   263  				}
   264  				if callback != nil || doPrint {
   265  					print("runtime: g ", gp.goid, ": unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n")
   266  					tracebackHexdump(stack, &frame, lrPtr)
   267  				}
   268  				if callback != nil {
   269  					throw("unknown caller pc")
   270  				}
   271  			}
   272  		}
   273  
   274  		frame.varp = frame.fp
   275  		if !usesLR {
   276  			// On x86, call instruction pushes return PC before entering new function.
   277  			frame.varp -= goarch.PtrSize
   278  		}
   279  
   280  		// For architectures with frame pointers, if there's
   281  		// a frame, then there's a saved frame pointer here.
   282  		//
   283  		// NOTE: This code is not as general as it looks.
   284  		// On x86, the ABI is to save the frame pointer word at the
   285  		// top of the stack frame, so we have to back down over it.
   286  		// On arm64, the frame pointer should be at the bottom of
   287  		// the stack (with R29 (aka FP) = RSP), in which case we would
   288  		// not want to do the subtraction here. But we started out without
   289  		// any frame pointer, and when we wanted to add it, we didn't
   290  		// want to break all the assembly doing direct writes to 8(RSP)
   291  		// to set the first parameter to a called function.
   292  		// So we decided to write the FP link *below* the stack pointer
   293  		// (with R29 = RSP - 8 in Go functions).
   294  		// This is technically ABI-compatible but not standard.
   295  		// And it happens to end up mimicking the x86 layout.
   296  		// Other architectures may make different decisions.
   297  		if frame.varp > frame.sp && framepointer_enabled {
   298  			frame.varp -= goarch.PtrSize
   299  		}
   300  
   301  		// Derive size of arguments.
   302  		// Most functions have a fixed-size argument block,
   303  		// so we can use metadata about the function f.
   304  		// Not all, though: there are some variadic functions
   305  		// in package runtime and reflect, and for those we use call-specific
   306  		// metadata recorded by f's caller.
   307  		if callback != nil || printing {
   308  			frame.argp = frame.fp + sys.MinFrameSize
   309  			var ok bool
   310  			frame.arglen, frame.argmap, ok = getArgInfoFast(f, callback != nil)
   311  			if !ok {
   312  				frame.arglen, frame.argmap = getArgInfo(&frame, f, callback != nil, ctxt)
   313  			}
   314  		}
   315  		ctxt = nil // ctxt is only needed to get arg maps for the topmost frame
   316  
   317  		// Determine frame's 'continuation PC', where it can continue.
   318  		// Normally this is the return address on the stack, but if sigpanic
   319  		// is immediately below this function on the stack, then the frame
   320  		// stopped executing due to a trap, and frame.pc is probably not
   321  		// a safe point for looking up liveness information. In this panicking case,
   322  		// the function either doesn't return at all (if it has no defers or if the
   323  		// defers do not recover) or it returns from one of the calls to
   324  		// deferproc a second time (if the corresponding deferred func recovers).
   325  		// In the latter case, use a deferreturn call site as the continuation pc.
   326  		frame.continpc = frame.pc
   327  		if waspanic {
   328  			if frame.fn.deferreturn != 0 {
   329  				frame.continpc = frame.fn.entry() + uintptr(frame.fn.deferreturn) + 1
   330  				// Note: this may perhaps keep return variables alive longer than
   331  				// strictly necessary, as we are using "function has a defer statement"
   332  				// as a proxy for "function actually deferred something". It seems
   333  				// to be a minor drawback. (We used to actually look through the
   334  				// gp._defer for a defer corresponding to this function, but that
   335  				// is hard to do with defer records on the stack during a stack copy.)
   336  				// Note: the +1 is to offset the -1 that
   337  				// stack.go:getStackMap does to back up a return
   338  				// address make sure the pc is in the CALL instruction.
   339  			} else {
   340  				frame.continpc = 0
   341  			}
   342  		}
   343  
   344  		if callback != nil {
   345  			if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
   346  				return n
   347  			}
   348  		}
   349  
   350  		if pcbuf != nil {
   351  			pc := frame.pc
   352  			// backup to CALL instruction to read inlining info (same logic as below)
   353  			tracepc := pc
   354  			// Normally, pc is a return address. In that case, we want to look up
   355  			// file/line information using pc-1, because that is the pc of the
   356  			// call instruction (more precisely, the last byte of the call instruction).
   357  			// Callers expect the pc buffer to contain return addresses and do the
   358  			// same -1 themselves, so we keep pc unchanged.
   359  			// When the pc is from a signal (e.g. profiler or segv) then we want
   360  			// to look up file/line information using pc, and we store pc+1 in the
   361  			// pc buffer so callers can unconditionally subtract 1 before looking up.
   362  			// See issue 34123.
   363  			// The pc can be at function entry when the frame is initialized without
   364  			// actually running code, like runtime.mstart.
   365  			if (n == 0 && flags&_TraceTrap != 0) || waspanic || pc == f.entry() {
   366  				pc++
   367  			} else {
   368  				tracepc--
   369  			}
   370  
   371  			// If there is inlining info, record the inner frames.
   372  			if inldata := funcdata(f, _FUNCDATA_InlTree); inldata != nil {
   373  				inltree := (*[1 << 20]inlinedCall)(inldata)
   374  				for {
   375  					ix := pcdatavalue(f, _PCDATA_InlTreeIndex, tracepc, &cache)
   376  					if ix < 0 {
   377  						break
   378  					}
   379  					if inltree[ix].funcID == funcID_wrapper && elideWrapperCalling(lastFuncID) {
   380  						// ignore wrappers
   381  					} else if skip > 0 {
   382  						skip--
   383  					} else if n < max {
   384  						(*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = pc
   385  						n++
   386  					}
   387  					lastFuncID = inltree[ix].funcID
   388  					// Back up to an instruction in the "caller".
   389  					tracepc = frame.fn.entry() + uintptr(inltree[ix].parentPc)
   390  					pc = tracepc + 1
   391  				}
   392  			}
   393  			// Record the main frame.
   394  			if f.funcID == funcID_wrapper && elideWrapperCalling(lastFuncID) {
   395  				// Ignore wrapper functions (except when they trigger panics).
   396  			} else if skip > 0 {
   397  				skip--
   398  			} else if n < max {
   399  				(*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = pc
   400  				n++
   401  			}
   402  			lastFuncID = f.funcID
   403  			n-- // offset n++ below
   404  		}
   405  
   406  		if printing {
   407  			// assume skip=0 for printing.
   408  			//
   409  			// Never elide wrappers if we haven't printed
   410  			// any frames. And don't elide wrappers that
   411  			// called panic rather than the wrapped
   412  			// function. Otherwise, leave them out.
   413  
   414  			// backup to CALL instruction to read inlining info (same logic as below)
   415  			tracepc := frame.pc
   416  			if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry() && !waspanic {
   417  				tracepc--
   418  			}
   419  			// If there is inlining info, print the inner frames.
   420  			if inldata := funcdata(f, _FUNCDATA_InlTree); inldata != nil {
   421  				inltree := (*[1 << 20]inlinedCall)(inldata)
   422  				var inlFunc _func
   423  				inlFuncInfo := funcInfo{&inlFunc, f.datap}
   424  				for {
   425  					ix := pcdatavalue(f, _PCDATA_InlTreeIndex, tracepc, nil)
   426  					if ix < 0 {
   427  						break
   428  					}
   429  
   430  					// Create a fake _func for the
   431  					// inlined function.
   432  					inlFunc.nameoff = inltree[ix].func_
   433  					inlFunc.funcID = inltree[ix].funcID
   434  
   435  					if (flags&_TraceRuntimeFrames) != 0 || showframe(inlFuncInfo, gp, nprint == 0, inlFuncInfo.funcID, lastFuncID) {
   436  						name := funcname(inlFuncInfo)
   437  						file, line := funcline(f, tracepc)
   438  						print(name, "(...)\n")
   439  						print("\t", file, ":", line, "\n")
   440  						nprint++
   441  					}
   442  					lastFuncID = inltree[ix].funcID
   443  					// Back up to an instruction in the "caller".
   444  					tracepc = frame.fn.entry() + uintptr(inltree[ix].parentPc)
   445  				}
   446  			}
   447  			if (flags&_TraceRuntimeFrames) != 0 || showframe(f, gp, nprint == 0, f.funcID, lastFuncID) {
   448  				// Print during crash.
   449  				//	main(0x1, 0x2, 0x3)
   450  				//		/home/rsc/go/src/runtime/x.go:23 +0xf
   451  				//
   452  				name := funcname(f)
   453  				file, line := funcline(f, tracepc)
   454  				if name == "runtime.gopanic" {
   455  					name = "panic"
   456  				}
   457  				print(name, "(")
   458  				argp := unsafe.Pointer(frame.argp)
   459  				printArgs(f, argp, tracepc)
   460  				print(")\n")
   461  				print("\t", file, ":", line)
   462  				if frame.pc > f.entry() {
   463  					print(" +", hex(frame.pc-f.entry()))
   464  				}
   465  				if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 {
   466  					print(" fp=", hex(frame.fp), " sp=", hex(frame.sp), " pc=", hex(frame.pc))
   467  				}
   468  				print("\n")
   469  				nprint++
   470  			}
   471  			lastFuncID = f.funcID
   472  		}
   473  		n++
   474  
   475  		if f.funcID == funcID_cgocallback && len(cgoCtxt) > 0 {
   476  			ctxt := cgoCtxt[len(cgoCtxt)-1]
   477  			cgoCtxt = cgoCtxt[:len(cgoCtxt)-1]
   478  
   479  			// skip only applies to Go frames.
   480  			// callback != nil only used when we only care
   481  			// about Go frames.
   482  			if skip == 0 && callback == nil {
   483  				n = tracebackCgoContext(pcbuf, printing, ctxt, n, max)
   484  			}
   485  		}
   486  
   487  		waspanic = f.funcID == funcID_sigpanic
   488  		injectedCall := waspanic || f.funcID == funcID_asyncPreempt || f.funcID == funcID_debugCallV2
   489  
   490  		// Do not unwind past the bottom of the stack.
   491  		if !flr.valid() {
   492  			break
   493  		}
   494  
   495  		if frame.pc == frame.lr && frame.sp == frame.fp {
   496  			// If the next frame is identical to the current frame, we cannot make progress.
   497  			print("runtime: traceback stuck. pc=", hex(frame.pc), " sp=", hex(frame.sp), "\n")
   498  			tracebackHexdump(stack, &frame, frame.sp)
   499  			throw("traceback stuck")
   500  		}
   501  
   502  		// Unwind to next frame.
   503  		frame.fn = flr
   504  		frame.pc = frame.lr
   505  		frame.lr = 0
   506  		frame.sp = frame.fp
   507  		frame.fp = 0
   508  		frame.argmap = nil
   509  
   510  		// On link register architectures, sighandler saves the LR on stack
   511  		// before faking a call.
   512  		if usesLR && injectedCall {
   513  			x := *(*uintptr)(unsafe.Pointer(frame.sp))
   514  			frame.sp += alignUp(sys.MinFrameSize, sys.StackAlign)
   515  			f = findfunc(frame.pc)
   516  			frame.fn = f
   517  			if !f.valid() {
   518  				frame.pc = x
   519  			} else if funcspdelta(f, frame.pc, &cache) == 0 {
   520  				frame.lr = x
   521  			}
   522  		}
   523  	}
   524  
   525  	if printing {
   526  		n = nprint
   527  	}
   528  
   529  	// Note that panic != nil is okay here: there can be leftover panics,
   530  	// because the defers on the panic stack do not nest in frame order as
   531  	// they do on the defer stack. If you have:
   532  	//
   533  	//	frame 1 defers d1
   534  	//	frame 2 defers d2
   535  	//	frame 3 defers d3
   536  	//	frame 4 panics
   537  	//	frame 4's panic starts running defers
   538  	//	frame 5, running d3, defers d4
   539  	//	frame 5 panics
   540  	//	frame 5's panic starts running defers
   541  	//	frame 6, running d4, garbage collects
   542  	//	frame 6, running d2, garbage collects
   543  	//
   544  	// During the execution of d4, the panic stack is d4 -> d3, which
   545  	// is nested properly, and we'll treat frame 3 as resumable, because we
   546  	// can find d3. (And in fact frame 3 is resumable. If d4 recovers
   547  	// and frame 5 continues running, d3, d3 can recover and we'll
   548  	// resume execution in (returning from) frame 3.)
   549  	//
   550  	// During the execution of d2, however, the panic stack is d2 -> d3,
   551  	// which is inverted. The scan will match d2 to frame 2 but having
   552  	// d2 on the stack until then means it will not match d3 to frame 3.
   553  	// This is okay: if we're running d2, then all the defers after d2 have
   554  	// completed and their corresponding frames are dead. Not finding d3
   555  	// for frame 3 means we'll set frame 3's continpc == 0, which is correct
   556  	// (frame 3 is dead). At the end of the walk the panic stack can thus
   557  	// contain defers (d3 in this case) for dead frames. The inversion here
   558  	// always indicates a dead frame, and the effect of the inversion on the
   559  	// scan is to hide those dead frames, so the scan is still okay:
   560  	// what's left on the panic stack are exactly (and only) the dead frames.
   561  	//
   562  	// We require callback != nil here because only when callback != nil
   563  	// do we know that gentraceback is being called in a "must be correct"
   564  	// context as opposed to a "best effort" context. The tracebacks with
   565  	// callbacks only happen when everything is stopped nicely.
   566  	// At other times, such as when gathering a stack for a profiling signal
   567  	// or when printing a traceback during a crash, everything may not be
   568  	// stopped nicely, and the stack walk may not be able to complete.
   569  	if callback != nil && n < max && frame.sp != gp.stktopsp {
   570  		print("runtime: g", gp.goid, ": frame.sp=", hex(frame.sp), " top=", hex(gp.stktopsp), "\n")
   571  		print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "] n=", n, " max=", max, "\n")
   572  		throw("traceback did not unwind completely")
   573  	}
   574  
   575  	return n
   576  }
   577  
   578  // printArgs prints function arguments in traceback.
   579  func printArgs(f funcInfo, argp unsafe.Pointer, pc uintptr) {
   580  	// The "instruction" of argument printing is encoded in _FUNCDATA_ArgInfo.
   581  	// See cmd/compile/internal/ssagen.emitArgInfo for the description of the
   582  	// encoding.
   583  	// These constants need to be in sync with the compiler.
   584  	const (
   585  		_endSeq         = 0xff
   586  		_startAgg       = 0xfe
   587  		_endAgg         = 0xfd
   588  		_dotdotdot      = 0xfc
   589  		_offsetTooLarge = 0xfb
   590  	)
   591  
   592  	const (
   593  		limit    = 10                       // print no more than 10 args/components
   594  		maxDepth = 5                        // no more than 5 layers of nesting
   595  		maxLen   = (maxDepth*3+2)*limit + 1 // max length of _FUNCDATA_ArgInfo (see the compiler side for reasoning)
   596  	)
   597  
   598  	p := (*[maxLen]uint8)(funcdata(f, _FUNCDATA_ArgInfo))
   599  	if p == nil {
   600  		return
   601  	}
   602  
   603  	liveInfo := funcdata(f, _FUNCDATA_ArgLiveInfo)
   604  	liveIdx := pcdatavalue(f, _PCDATA_ArgLiveIndex, pc, nil)
   605  	startOffset := uint8(0xff) // smallest offset that needs liveness info (slots with a lower offset is always live)
   606  	if liveInfo != nil {
   607  		startOffset = *(*uint8)(liveInfo)
   608  	}
   609  
   610  	isLive := func(off, slotIdx uint8) bool {
   611  		if liveInfo == nil || liveIdx <= 0 {
   612  			return true // no liveness info, always live
   613  		}
   614  		if off < startOffset {
   615  			return true
   616  		}
   617  		bits := *(*uint8)(add(liveInfo, uintptr(liveIdx)+uintptr(slotIdx/8)))
   618  		return bits&(1<<(slotIdx%8)) != 0
   619  	}
   620  
   621  	print1 := func(off, sz, slotIdx uint8) {
   622  		x := readUnaligned64(add(argp, uintptr(off)))
   623  		// mask out irrelevant bits
   624  		if sz < 8 {
   625  			shift := 64 - sz*8
   626  			if goarch.BigEndian {
   627  				x = x >> shift
   628  			} else {
   629  				x = x << shift >> shift
   630  			}
   631  		}
   632  		print(hex(x))
   633  		if !isLive(off, slotIdx) {
   634  			print("?")
   635  		}
   636  	}
   637  
   638  	start := true
   639  	printcomma := func() {
   640  		if !start {
   641  			print(", ")
   642  		}
   643  	}
   644  	pi := 0
   645  	slotIdx := uint8(0) // register arg spill slot index
   646  printloop:
   647  	for {
   648  		o := p[pi]
   649  		pi++
   650  		switch o {
   651  		case _endSeq:
   652  			break printloop
   653  		case _startAgg:
   654  			printcomma()
   655  			print("{")
   656  			start = true
   657  			continue
   658  		case _endAgg:
   659  			print("}")
   660  		case _dotdotdot:
   661  			printcomma()
   662  			print("...")
   663  		case _offsetTooLarge:
   664  			printcomma()
   665  			print("_")
   666  		default:
   667  			printcomma()
   668  			sz := p[pi]
   669  			pi++
   670  			print1(o, sz, slotIdx)
   671  			if o >= startOffset {
   672  				slotIdx++
   673  			}
   674  		}
   675  		start = false
   676  	}
   677  }
   678  
   679  // reflectMethodValue is a partial duplicate of reflect.makeFuncImpl
   680  // and reflect.methodValue.
   681  type reflectMethodValue struct {
   682  	fn     uintptr
   683  	stack  *bitvector // ptrmap for both args and results
   684  	argLen uintptr    // just args
   685  }
   686  
   687  // getArgInfoFast returns the argument frame information for a call to f.
   688  // It is short and inlineable. However, it does not handle all functions.
   689  // If ok reports false, you must call getArgInfo instead.
   690  // TODO(josharian): once we do mid-stack inlining,
   691  // call getArgInfo directly from getArgInfoFast and stop returning an ok bool.
   692  func getArgInfoFast(f funcInfo, needArgMap bool) (arglen uintptr, argmap *bitvector, ok bool) {
   693  	return uintptr(f.args), nil, !(needArgMap && f.args == _ArgsSizeUnknown)
   694  }
   695  
   696  // getArgInfo returns the argument frame information for a call to f
   697  // with call frame frame.
   698  //
   699  // This is used for both actual calls with active stack frames and for
   700  // deferred calls or goroutines that are not yet executing. If this is an actual
   701  // call, ctxt must be nil (getArgInfo will retrieve what it needs from
   702  // the active stack frame). If this is a deferred call or unstarted goroutine,
   703  // ctxt must be the function object that was deferred or go'd.
   704  func getArgInfo(frame *stkframe, f funcInfo, needArgMap bool, ctxt *funcval) (arglen uintptr, argmap *bitvector) {
   705  	arglen = uintptr(f.args)
   706  	if needArgMap && f.args == _ArgsSizeUnknown {
   707  		// Extract argument bitmaps for reflect stubs from the calls they made to reflect.
   708  		switch funcname(f) {
   709  		case "reflect.makeFuncStub", "reflect.methodValueCall":
   710  			// These take a *reflect.methodValue as their
   711  			// context register.
   712  			var mv *reflectMethodValue
   713  			var retValid bool
   714  			if ctxt != nil {
   715  				// This is not an actual call, but a
   716  				// deferred call or an unstarted goroutine.
   717  				// The function value is itself the *reflect.methodValue.
   718  				mv = (*reflectMethodValue)(unsafe.Pointer(ctxt))
   719  			} else {
   720  				// This is a real call that took the
   721  				// *reflect.methodValue as its context
   722  				// register and immediately saved it
   723  				// to 0(SP). Get the methodValue from
   724  				// 0(SP).
   725  				arg0 := frame.sp + sys.MinFrameSize
   726  				mv = *(**reflectMethodValue)(unsafe.Pointer(arg0))
   727  				// Figure out whether the return values are valid.
   728  				// Reflect will update this value after it copies
   729  				// in the return values.
   730  				retValid = *(*bool)(unsafe.Pointer(arg0 + 4*goarch.PtrSize))
   731  			}
   732  			if mv.fn != f.entry() {
   733  				print("runtime: confused by ", funcname(f), "\n")
   734  				throw("reflect mismatch")
   735  			}
   736  			bv := mv.stack
   737  			arglen = uintptr(bv.n * goarch.PtrSize)
   738  			if !retValid {
   739  				arglen = uintptr(mv.argLen) &^ (goarch.PtrSize - 1)
   740  			}
   741  			argmap = bv
   742  		}
   743  	}
   744  	return
   745  }
   746  
   747  // tracebackCgoContext handles tracing back a cgo context value, from
   748  // the context argument to setCgoTraceback, for the gentraceback
   749  // function. It returns the new value of n.
   750  func tracebackCgoContext(pcbuf *uintptr, printing bool, ctxt uintptr, n, max int) int {
   751  	var cgoPCs [32]uintptr
   752  	cgoContextPCs(ctxt, cgoPCs[:])
   753  	var arg cgoSymbolizerArg
   754  	anySymbolized := false
   755  	for _, pc := range cgoPCs {
   756  		if pc == 0 || n >= max {
   757  			break
   758  		}
   759  		if pcbuf != nil {
   760  			(*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = pc
   761  		}
   762  		if printing {
   763  			if cgoSymbolizer == nil {
   764  				print("non-Go function at pc=", hex(pc), "\n")
   765  			} else {
   766  				c := printOneCgoTraceback(pc, max-n, &arg)
   767  				n += c - 1 // +1 a few lines down
   768  				anySymbolized = true
   769  			}
   770  		}
   771  		n++
   772  	}
   773  	if anySymbolized {
   774  		arg.pc = 0
   775  		callCgoSymbolizer(&arg)
   776  	}
   777  	return n
   778  }
   779  
   780  func printcreatedby(gp *g) {
   781  	// Show what created goroutine, except main goroutine (goid 1).
   782  	pc := gp.gopc
   783  	f := findfunc(pc)
   784  	if f.valid() && showframe(f, gp, false, funcID_normal, funcID_normal) && gp.goid != 1 {
   785  		printcreatedby1(f, pc)
   786  	}
   787  }
   788  
   789  func printcreatedby1(f funcInfo, pc uintptr) {
   790  	print("created by ", funcname(f), "\n")
   791  	tracepc := pc // back up to CALL instruction for funcline.
   792  	if pc > f.entry() {
   793  		tracepc -= sys.PCQuantum
   794  	}
   795  	file, line := funcline(f, tracepc)
   796  	print("\t", file, ":", line)
   797  	if pc > f.entry() {
   798  		print(" +", hex(pc-f.entry()))
   799  	}
   800  	print("\n")
   801  }
   802  
   803  func traceback(pc, sp, lr uintptr, gp *g) {
   804  	traceback1(pc, sp, lr, gp, 0)
   805  }
   806  
   807  // tracebacktrap is like traceback but expects that the PC and SP were obtained
   808  // from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or getcallerpc/getcallersp.
   809  // Because they are from a trap instead of from a saved pair,
   810  // the initial PC must not be rewound to the previous instruction.
   811  // (All the saved pairs record a PC that is a return address, so we
   812  // rewind it into the CALL instruction.)
   813  // If gp.m.libcall{g,pc,sp} information is available, it uses that information in preference to
   814  // the pc/sp/lr passed in.
   815  func tracebacktrap(pc, sp, lr uintptr, gp *g) {
   816  	if gp.m.libcallsp != 0 {
   817  		// We're in C code somewhere, traceback from the saved position.
   818  		traceback1(gp.m.libcallpc, gp.m.libcallsp, 0, gp.m.libcallg.ptr(), 0)
   819  		return
   820  	}
   821  	traceback1(pc, sp, lr, gp, _TraceTrap)
   822  }
   823  
   824  func traceback1(pc, sp, lr uintptr, gp *g, flags uint) {
   825  	// If the goroutine is in cgo, and we have a cgo traceback, print that.
   826  	if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 {
   827  		// Lock cgoCallers so that a signal handler won't
   828  		// change it, copy the array, reset it, unlock it.
   829  		// We are locked to the thread and are not running
   830  		// concurrently with a signal handler.
   831  		// We just have to stop a signal handler from interrupting
   832  		// in the middle of our copy.
   833  		atomic.Store(&gp.m.cgoCallersUse, 1)
   834  		cgoCallers := *gp.m.cgoCallers
   835  		gp.m.cgoCallers[0] = 0
   836  		atomic.Store(&gp.m.cgoCallersUse, 0)
   837  
   838  		printCgoTraceback(&cgoCallers)
   839  	}
   840  
   841  	if readgstatus(gp)&^_Gscan == _Gsyscall {
   842  		// Override registers if blocked in system call.
   843  		pc = gp.syscallpc
   844  		sp = gp.syscallsp
   845  		flags &^= _TraceTrap
   846  	}
   847  	if gp.m != nil && gp.m.vdsoSP != 0 {
   848  		// Override registers if running in VDSO. This comes after the
   849  		// _Gsyscall check to cover VDSO calls after entersyscall.
   850  		pc = gp.m.vdsoPC
   851  		sp = gp.m.vdsoSP
   852  		flags &^= _TraceTrap
   853  	}
   854  
   855  	// Print traceback. By default, omits runtime frames.
   856  	// If that means we print nothing at all, repeat forcing all frames printed.
   857  	n := gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags)
   858  	if n == 0 && (flags&_TraceRuntimeFrames) == 0 {
   859  		n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags|_TraceRuntimeFrames)
   860  	}
   861  	if n == _TracebackMaxFrames {
   862  		print("...additional frames elided...\n")
   863  	}
   864  	printcreatedby(gp)
   865  
   866  	if gp.ancestors == nil {
   867  		return
   868  	}
   869  	for _, ancestor := range *gp.ancestors {
   870  		printAncestorTraceback(ancestor)
   871  	}
   872  }
   873  
   874  // printAncestorTraceback prints the traceback of the given ancestor.
   875  // TODO: Unify this with gentraceback and CallersFrames.
   876  func printAncestorTraceback(ancestor ancestorInfo) {
   877  	print("[originating from goroutine ", ancestor.goid, "]:\n")
   878  	for fidx, pc := range ancestor.pcs {
   879  		f := findfunc(pc) // f previously validated
   880  		if showfuncinfo(f, fidx == 0, funcID_normal, funcID_normal) {
   881  			printAncestorTracebackFuncInfo(f, pc)
   882  		}
   883  	}
   884  	if len(ancestor.pcs) == _TracebackMaxFrames {
   885  		print("...additional frames elided...\n")
   886  	}
   887  	// Show what created goroutine, except main goroutine (goid 1).
   888  	f := findfunc(ancestor.gopc)
   889  	if f.valid() && showfuncinfo(f, false, funcID_normal, funcID_normal) && ancestor.goid != 1 {
   890  		printcreatedby1(f, ancestor.gopc)
   891  	}
   892  }
   893  
   894  // printAncestorTraceback prints the given function info at a given pc
   895  // within an ancestor traceback. The precision of this info is reduced
   896  // due to only have access to the pcs at the time of the caller
   897  // goroutine being created.
   898  func printAncestorTracebackFuncInfo(f funcInfo, pc uintptr) {
   899  	name := funcname(f)
   900  	if inldata := funcdata(f, _FUNCDATA_InlTree); inldata != nil {
   901  		inltree := (*[1 << 20]inlinedCall)(inldata)
   902  		ix := pcdatavalue(f, _PCDATA_InlTreeIndex, pc, nil)
   903  		if ix >= 0 {
   904  			name = funcnameFromNameoff(f, inltree[ix].func_)
   905  		}
   906  	}
   907  	file, line := funcline(f, pc)
   908  	if name == "runtime.gopanic" {
   909  		name = "panic"
   910  	}
   911  	print(name, "(...)\n")
   912  	print("\t", file, ":", line)
   913  	if pc > f.entry() {
   914  		print(" +", hex(pc-f.entry()))
   915  	}
   916  	print("\n")
   917  }
   918  
   919  func callers(skip int, pcbuf []uintptr) int {
   920  	sp := getcallersp()
   921  	pc := getcallerpc()
   922  	gp := getg()
   923  	var n int
   924  	systemstack(func() {
   925  		n = gentraceback(pc, sp, 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
   926  	})
   927  	return n
   928  }
   929  
   930  func gcallers(gp *g, skip int, pcbuf []uintptr) int {
   931  	return gentraceback(^uintptr(0), ^uintptr(0), 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
   932  }
   933  
   934  // showframe reports whether the frame with the given characteristics should
   935  // be printed during a traceback.
   936  func showframe(f funcInfo, gp *g, firstFrame bool, funcID, childID funcID) bool {
   937  	g := getg()
   938  	if g.m.throwing >= throwTypeRuntime && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) {
   939  		return true
   940  	}
   941  	return showfuncinfo(f, firstFrame, funcID, childID)
   942  }
   943  
   944  // showfuncinfo reports whether a function with the given characteristics should
   945  // be printed during a traceback.
   946  func showfuncinfo(f funcInfo, firstFrame bool, funcID, childID funcID) bool {
   947  	// Note that f may be a synthesized funcInfo for an inlined
   948  	// function, in which case only nameoff and funcID are set.
   949  
   950  	level, _, _ := gotraceback()
   951  	if level > 1 {
   952  		// Show all frames.
   953  		return true
   954  	}
   955  
   956  	if !f.valid() {
   957  		return false
   958  	}
   959  
   960  	if funcID == funcID_wrapper && elideWrapperCalling(childID) {
   961  		return false
   962  	}
   963  
   964  	name := funcname(f)
   965  
   966  	// Special case: always show runtime.gopanic frame
   967  	// in the middle of a stack trace, so that we can
   968  	// see the boundary between ordinary code and
   969  	// panic-induced deferred code.
   970  	// See golang.org/issue/5832.
   971  	if name == "runtime.gopanic" && !firstFrame {
   972  		return true
   973  	}
   974  
   975  	return bytealg.IndexByteString(name, '.') >= 0 && (!hasPrefix(name, "runtime.") || isExportedRuntime(name))
   976  }
   977  
   978  // isExportedRuntime reports whether name is an exported runtime function.
   979  // It is only for runtime functions, so ASCII A-Z is fine.
   980  func isExportedRuntime(name string) bool {
   981  	const n = len("runtime.")
   982  	return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
   983  }
   984  
   985  // elideWrapperCalling reports whether a wrapper function that called
   986  // function id should be elided from stack traces.
   987  func elideWrapperCalling(id funcID) bool {
   988  	// If the wrapper called a panic function instead of the
   989  	// wrapped function, we want to include it in stacks.
   990  	return !(id == funcID_gopanic || id == funcID_sigpanic || id == funcID_panicwrap)
   991  }
   992  
   993  var gStatusStrings = [...]string{
   994  	_Gidle:      "idle",
   995  	_Grunnable:  "runnable",
   996  	_Grunning:   "running",
   997  	_Gsyscall:   "syscall",
   998  	_Gwaiting:   "waiting",
   999  	_Gdead:      "dead",
  1000  	_Gcopystack: "copystack",
  1001  	_Gpreempted: "preempted",
  1002  }
  1003  
  1004  func goroutineheader(gp *g) {
  1005  	gpstatus := readgstatus(gp)
  1006  
  1007  	isScan := gpstatus&_Gscan != 0
  1008  	gpstatus &^= _Gscan // drop the scan bit
  1009  
  1010  	// Basic string status
  1011  	var status string
  1012  	if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
  1013  		status = gStatusStrings[gpstatus]
  1014  	} else {
  1015  		status = "???"
  1016  	}
  1017  
  1018  	// Override.
  1019  	if gpstatus == _Gwaiting && gp.waitreason != waitReasonZero {
  1020  		status = gp.waitreason.String()
  1021  	}
  1022  
  1023  	// approx time the G is blocked, in minutes
  1024  	var waitfor int64
  1025  	if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
  1026  		waitfor = (nanotime() - gp.waitsince) / 60e9
  1027  	}
  1028  	print("goroutine ", gp.goid, " [", status)
  1029  	if isScan {
  1030  		print(" (scan)")
  1031  	}
  1032  	if waitfor >= 1 {
  1033  		print(", ", waitfor, " minutes")
  1034  	}
  1035  	if gp.lockedm != 0 {
  1036  		print(", locked to thread")
  1037  	}
  1038  	print("]:\n")
  1039  }
  1040  
  1041  func tracebackothers(me *g) {
  1042  	level, _, _ := gotraceback()
  1043  
  1044  	// Show the current goroutine first, if we haven't already.
  1045  	curgp := getg().m.curg
  1046  	if curgp != nil && curgp != me {
  1047  		print("\n")
  1048  		goroutineheader(curgp)
  1049  		traceback(^uintptr(0), ^uintptr(0), 0, curgp)
  1050  	}
  1051  
  1052  	// We can't call locking forEachG here because this may be during fatal
  1053  	// throw/panic, where locking could be out-of-order or a direct
  1054  	// deadlock.
  1055  	//
  1056  	// Instead, use forEachGRace, which requires no locking. We don't lock
  1057  	// against concurrent creation of new Gs, but even with allglock we may
  1058  	// miss Gs created after this loop.
  1059  	forEachGRace(func(gp *g) {
  1060  		if gp == me || gp == curgp || readgstatus(gp) == _Gdead || isSystemGoroutine(gp, false) && level < 2 {
  1061  			return
  1062  		}
  1063  		print("\n")
  1064  		goroutineheader(gp)
  1065  		// Note: gp.m == g.m occurs when tracebackothers is
  1066  		// called from a signal handler initiated during a
  1067  		// systemstack call. The original G is still in the
  1068  		// running state, and we want to print its stack.
  1069  		if gp.m != getg().m && readgstatus(gp)&^_Gscan == _Grunning {
  1070  			print("\tgoroutine running on other thread; stack unavailable\n")
  1071  			printcreatedby(gp)
  1072  		} else {
  1073  			traceback(^uintptr(0), ^uintptr(0), 0, gp)
  1074  		}
  1075  	})
  1076  }
  1077  
  1078  // tracebackHexdump hexdumps part of stk around frame.sp and frame.fp
  1079  // for debugging purposes. If the address bad is included in the
  1080  // hexdumped range, it will mark it as well.
  1081  func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) {
  1082  	const expand = 32 * goarch.PtrSize
  1083  	const maxExpand = 256 * goarch.PtrSize
  1084  	// Start around frame.sp.
  1085  	lo, hi := frame.sp, frame.sp
  1086  	// Expand to include frame.fp.
  1087  	if frame.fp != 0 && frame.fp < lo {
  1088  		lo = frame.fp
  1089  	}
  1090  	if frame.fp != 0 && frame.fp > hi {
  1091  		hi = frame.fp
  1092  	}
  1093  	// Expand a bit more.
  1094  	lo, hi = lo-expand, hi+expand
  1095  	// But don't go too far from frame.sp.
  1096  	if lo < frame.sp-maxExpand {
  1097  		lo = frame.sp - maxExpand
  1098  	}
  1099  	if hi > frame.sp+maxExpand {
  1100  		hi = frame.sp + maxExpand
  1101  	}
  1102  	// And don't go outside the stack bounds.
  1103  	if lo < stk.lo {
  1104  		lo = stk.lo
  1105  	}
  1106  	if hi > stk.hi {
  1107  		hi = stk.hi
  1108  	}
  1109  
  1110  	// Print the hex dump.
  1111  	print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n")
  1112  	hexdumpWords(lo, hi, func(p uintptr) byte {
  1113  		switch p {
  1114  		case frame.fp:
  1115  			return '>'
  1116  		case frame.sp:
  1117  			return '<'
  1118  		case bad:
  1119  			return '!'
  1120  		}
  1121  		return 0
  1122  	})
  1123  }
  1124  
  1125  // isSystemGoroutine reports whether the goroutine g must be omitted
  1126  // in stack dumps and deadlock detector. This is any goroutine that
  1127  // starts at a runtime.* entry point, except for runtime.main,
  1128  // runtime.handleAsyncEvent (wasm only) and sometimes runtime.runfinq.
  1129  //
  1130  // If fixed is true, any goroutine that can vary between user and
  1131  // system (that is, the finalizer goroutine) is considered a user
  1132  // goroutine.
  1133  func isSystemGoroutine(gp *g, fixed bool) bool {
  1134  	// Keep this in sync with internal/trace.IsSystemGoroutine.
  1135  	f := findfunc(gp.startpc)
  1136  	if !f.valid() {
  1137  		return false
  1138  	}
  1139  	if f.funcID == funcID_runtime_main || f.funcID == funcID_handleAsyncEvent {
  1140  		return false
  1141  	}
  1142  	if f.funcID == funcID_runfinq {
  1143  		// We include the finalizer goroutine if it's calling
  1144  		// back into user code.
  1145  		if fixed {
  1146  			// This goroutine can vary. In fixed mode,
  1147  			// always consider it a user goroutine.
  1148  			return false
  1149  		}
  1150  		return !fingRunning
  1151  	}
  1152  	return hasPrefix(funcname(f), "runtime.")
  1153  }
  1154  
  1155  // SetCgoTraceback records three C functions to use to gather
  1156  // traceback information from C code and to convert that traceback
  1157  // information into symbolic information. These are used when printing
  1158  // stack traces for a program that uses cgo.
  1159  //
  1160  // The traceback and context functions may be called from a signal
  1161  // handler, and must therefore use only async-signal safe functions.
  1162  // The symbolizer function may be called while the program is
  1163  // crashing, and so must be cautious about using memory.  None of the
  1164  // functions may call back into Go.
  1165  //
  1166  // The context function will be called with a single argument, a
  1167  // pointer to a struct:
  1168  //
  1169  //	struct {
  1170  //		Context uintptr
  1171  //	}
  1172  //
  1173  // In C syntax, this struct will be
  1174  //
  1175  //	struct {
  1176  //		uintptr_t Context;
  1177  //	};
  1178  //
  1179  // If the Context field is 0, the context function is being called to
  1180  // record the current traceback context. It should record in the
  1181  // Context field whatever information is needed about the current
  1182  // point of execution to later produce a stack trace, probably the
  1183  // stack pointer and PC. In this case the context function will be
  1184  // called from C code.
  1185  //
  1186  // If the Context field is not 0, then it is a value returned by a
  1187  // previous call to the context function. This case is called when the
  1188  // context is no longer needed; that is, when the Go code is returning
  1189  // to its C code caller. This permits the context function to release
  1190  // any associated resources.
  1191  //
  1192  // While it would be correct for the context function to record a
  1193  // complete a stack trace whenever it is called, and simply copy that
  1194  // out in the traceback function, in a typical program the context
  1195  // function will be called many times without ever recording a
  1196  // traceback for that context. Recording a complete stack trace in a
  1197  // call to the context function is likely to be inefficient.
  1198  //
  1199  // The traceback function will be called with a single argument, a
  1200  // pointer to a struct:
  1201  //
  1202  //	struct {
  1203  //		Context    uintptr
  1204  //		SigContext uintptr
  1205  //		Buf        *uintptr
  1206  //		Max        uintptr
  1207  //	}
  1208  //
  1209  // In C syntax, this struct will be
  1210  //
  1211  //	struct {
  1212  //		uintptr_t  Context;
  1213  //		uintptr_t  SigContext;
  1214  //		uintptr_t* Buf;
  1215  //		uintptr_t  Max;
  1216  //	};
  1217  //
  1218  // The Context field will be zero to gather a traceback from the
  1219  // current program execution point. In this case, the traceback
  1220  // function will be called from C code.
  1221  //
  1222  // Otherwise Context will be a value previously returned by a call to
  1223  // the context function. The traceback function should gather a stack
  1224  // trace from that saved point in the program execution. The traceback
  1225  // function may be called from an execution thread other than the one
  1226  // that recorded the context, but only when the context is known to be
  1227  // valid and unchanging. The traceback function may also be called
  1228  // deeper in the call stack on the same thread that recorded the
  1229  // context. The traceback function may be called multiple times with
  1230  // the same Context value; it will usually be appropriate to cache the
  1231  // result, if possible, the first time this is called for a specific
  1232  // context value.
  1233  //
  1234  // If the traceback function is called from a signal handler on a Unix
  1235  // system, SigContext will be the signal context argument passed to
  1236  // the signal handler (a C ucontext_t* cast to uintptr_t). This may be
  1237  // used to start tracing at the point where the signal occurred. If
  1238  // the traceback function is not called from a signal handler,
  1239  // SigContext will be zero.
  1240  //
  1241  // Buf is where the traceback information should be stored. It should
  1242  // be PC values, such that Buf[0] is the PC of the caller, Buf[1] is
  1243  // the PC of that function's caller, and so on.  Max is the maximum
  1244  // number of entries to store.  The function should store a zero to
  1245  // indicate the top of the stack, or that the caller is on a different
  1246  // stack, presumably a Go stack.
  1247  //
  1248  // Unlike runtime.Callers, the PC values returned should, when passed
  1249  // to the symbolizer function, return the file/line of the call
  1250  // instruction.  No additional subtraction is required or appropriate.
  1251  //
  1252  // On all platforms, the traceback function is invoked when a call from
  1253  // Go to C to Go requests a stack trace. On linux/amd64, linux/ppc64le,
  1254  // linux/arm64, and freebsd/amd64, the traceback function is also invoked
  1255  // when a signal is received by a thread that is executing a cgo call.
  1256  // The traceback function should not make assumptions about when it is
  1257  // called, as future versions of Go may make additional calls.
  1258  //
  1259  // The symbolizer function will be called with a single argument, a
  1260  // pointer to a struct:
  1261  //
  1262  //	struct {
  1263  //		PC      uintptr // program counter to fetch information for
  1264  //		File    *byte   // file name (NUL terminated)
  1265  //		Lineno  uintptr // line number
  1266  //		Func    *byte   // function name (NUL terminated)
  1267  //		Entry   uintptr // function entry point
  1268  //		More    uintptr // set non-zero if more info for this PC
  1269  //		Data    uintptr // unused by runtime, available for function
  1270  //	}
  1271  //
  1272  // In C syntax, this struct will be
  1273  //
  1274  //	struct {
  1275  //		uintptr_t PC;
  1276  //		char*     File;
  1277  //		uintptr_t Lineno;
  1278  //		char*     Func;
  1279  //		uintptr_t Entry;
  1280  //		uintptr_t More;
  1281  //		uintptr_t Data;
  1282  //	};
  1283  //
  1284  // The PC field will be a value returned by a call to the traceback
  1285  // function.
  1286  //
  1287  // The first time the function is called for a particular traceback,
  1288  // all the fields except PC will be 0. The function should fill in the
  1289  // other fields if possible, setting them to 0/nil if the information
  1290  // is not available. The Data field may be used to store any useful
  1291  // information across calls. The More field should be set to non-zero
  1292  // if there is more information for this PC, zero otherwise. If More
  1293  // is set non-zero, the function will be called again with the same
  1294  // PC, and may return different information (this is intended for use
  1295  // with inlined functions). If More is zero, the function will be
  1296  // called with the next PC value in the traceback. When the traceback
  1297  // is complete, the function will be called once more with PC set to
  1298  // zero; this may be used to free any information. Each call will
  1299  // leave the fields of the struct set to the same values they had upon
  1300  // return, except for the PC field when the More field is zero. The
  1301  // function must not keep a copy of the struct pointer between calls.
  1302  //
  1303  // When calling SetCgoTraceback, the version argument is the version
  1304  // number of the structs that the functions expect to receive.
  1305  // Currently this must be zero.
  1306  //
  1307  // The symbolizer function may be nil, in which case the results of
  1308  // the traceback function will be displayed as numbers. If the
  1309  // traceback function is nil, the symbolizer function will never be
  1310  // called. The context function may be nil, in which case the
  1311  // traceback function will only be called with the context field set
  1312  // to zero.  If the context function is nil, then calls from Go to C
  1313  // to Go will not show a traceback for the C portion of the call stack.
  1314  //
  1315  // SetCgoTraceback should be called only once, ideally from an init function.
  1316  func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) {
  1317  	if version != 0 {
  1318  		panic("unsupported version")
  1319  	}
  1320  
  1321  	if cgoTraceback != nil && cgoTraceback != traceback ||
  1322  		cgoContext != nil && cgoContext != context ||
  1323  		cgoSymbolizer != nil && cgoSymbolizer != symbolizer {
  1324  		panic("call SetCgoTraceback only once")
  1325  	}
  1326  
  1327  	cgoTraceback = traceback
  1328  	cgoContext = context
  1329  	cgoSymbolizer = symbolizer
  1330  
  1331  	// The context function is called when a C function calls a Go
  1332  	// function. As such it is only called by C code in runtime/cgo.
  1333  	if _cgo_set_context_function != nil {
  1334  		cgocall(_cgo_set_context_function, context)
  1335  	}
  1336  }
  1337  
  1338  var cgoTraceback unsafe.Pointer
  1339  var cgoContext unsafe.Pointer
  1340  var cgoSymbolizer unsafe.Pointer
  1341  
  1342  // cgoTracebackArg is the type passed to cgoTraceback.
  1343  type cgoTracebackArg struct {
  1344  	context    uintptr
  1345  	sigContext uintptr
  1346  	buf        *uintptr
  1347  	max        uintptr
  1348  }
  1349  
  1350  // cgoContextArg is the type passed to the context function.
  1351  type cgoContextArg struct {
  1352  	context uintptr
  1353  }
  1354  
  1355  // cgoSymbolizerArg is the type passed to cgoSymbolizer.
  1356  type cgoSymbolizerArg struct {
  1357  	pc       uintptr
  1358  	file     *byte
  1359  	lineno   uintptr
  1360  	funcName *byte
  1361  	entry    uintptr
  1362  	more     uintptr
  1363  	data     uintptr
  1364  }
  1365  
  1366  // cgoTraceback prints a traceback of callers.
  1367  func printCgoTraceback(callers *cgoCallers) {
  1368  	if cgoSymbolizer == nil {
  1369  		for _, c := range callers {
  1370  			if c == 0 {
  1371  				break
  1372  			}
  1373  			print("non-Go function at pc=", hex(c), "\n")
  1374  		}
  1375  		return
  1376  	}
  1377  
  1378  	var arg cgoSymbolizerArg
  1379  	for _, c := range callers {
  1380  		if c == 0 {
  1381  			break
  1382  		}
  1383  		printOneCgoTraceback(c, 0x7fffffff, &arg)
  1384  	}
  1385  	arg.pc = 0
  1386  	callCgoSymbolizer(&arg)
  1387  }
  1388  
  1389  // printOneCgoTraceback prints the traceback of a single cgo caller.
  1390  // This can print more than one line because of inlining.
  1391  // Returns the number of frames printed.
  1392  func printOneCgoTraceback(pc uintptr, max int, arg *cgoSymbolizerArg) int {
  1393  	c := 0
  1394  	arg.pc = pc
  1395  	for c <= max {
  1396  		callCgoSymbolizer(arg)
  1397  		if arg.funcName != nil {
  1398  			// Note that we don't print any argument
  1399  			// information here, not even parentheses.
  1400  			// The symbolizer must add that if appropriate.
  1401  			println(gostringnocopy(arg.funcName))
  1402  		} else {
  1403  			println("non-Go function")
  1404  		}
  1405  		print("\t")
  1406  		if arg.file != nil {
  1407  			print(gostringnocopy(arg.file), ":", arg.lineno, " ")
  1408  		}
  1409  		print("pc=", hex(pc), "\n")
  1410  		c++
  1411  		if arg.more == 0 {
  1412  			break
  1413  		}
  1414  	}
  1415  	return c
  1416  }
  1417  
  1418  // callCgoSymbolizer calls the cgoSymbolizer function.
  1419  func callCgoSymbolizer(arg *cgoSymbolizerArg) {
  1420  	call := cgocall
  1421  	if panicking > 0 || getg().m.curg != getg() {
  1422  		// We do not want to call into the scheduler when panicking
  1423  		// or when on the system stack.
  1424  		call = asmcgocall
  1425  	}
  1426  	if msanenabled {
  1427  		msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{}))
  1428  	}
  1429  	if asanenabled {
  1430  		asanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{}))
  1431  	}
  1432  	call(cgoSymbolizer, noescape(unsafe.Pointer(arg)))
  1433  }
  1434  
  1435  // cgoContextPCs gets the PC values from a cgo traceback.
  1436  func cgoContextPCs(ctxt uintptr, buf []uintptr) {
  1437  	if cgoTraceback == nil {
  1438  		return
  1439  	}
  1440  	call := cgocall
  1441  	if panicking > 0 || getg().m.curg != getg() {
  1442  		// We do not want to call into the scheduler when panicking
  1443  		// or when on the system stack.
  1444  		call = asmcgocall
  1445  	}
  1446  	arg := cgoTracebackArg{
  1447  		context: ctxt,
  1448  		buf:     (*uintptr)(noescape(unsafe.Pointer(&buf[0]))),
  1449  		max:     uintptr(len(buf)),
  1450  	}
  1451  	if msanenabled {
  1452  		msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg))
  1453  	}
  1454  	if asanenabled {
  1455  		asanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg))
  1456  	}
  1457  	call(cgoTraceback, noescape(unsafe.Pointer(&arg)))
  1458  }
  1459  

View as plain text