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

View as plain text