github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/traceback.go (about)

     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 "unsafe"
     8  
     9  // The code in this file implements stack trace walking for all architectures.
    10  // The most important fact about a given architecture is whether it uses a link register.
    11  // On systems with link registers, the prologue for a non-leaf function stores the
    12  // incoming value of LR at the bottom of the newly allocated stack frame.
    13  // On systems without link registers, the architecture pushes a return PC during
    14  // the call instruction, so the return PC ends up above the stack frame.
    15  // In this file, the return PC is always called LR, no matter how it was found.
    16  //
    17  // To date, the opposite of a link register architecture is an x86 architecture.
    18  // This code may need to change if some other kind of non-link-register
    19  // architecture comes along.
    20  //
    21  // The other important fact is the size of a pointer: on 32-bit systems the LR
    22  // takes up only 4 bytes on the stack, while on 64-bit systems it takes up 8 bytes.
    23  // Typically this is ptrSize.
    24  //
    25  // As an exception, amd64p32 has ptrSize == 4 but the CALL instruction still
    26  // stores an 8-byte return PC onto the stack. To accommodate this, we use regSize
    27  // as the size of the architecture-pushed return PC.
    28  //
    29  // usesLR is defined below. ptrSize and regSize are defined in stubs.go.
    30  
    31  const usesLR = GOARCH != "amd64" && GOARCH != "amd64p32" && GOARCH != "386"
    32  
    33  var (
    34  	// initialized in tracebackinit
    35  	deferprocPC          uintptr
    36  	goexitPC             uintptr
    37  	jmpdeferPC           uintptr
    38  	mcallPC              uintptr
    39  	morestackPC          uintptr
    40  	mstartPC             uintptr
    41  	newprocPC            uintptr
    42  	rt0_goPC             uintptr
    43  	sigpanicPC           uintptr
    44  	systemstack_switchPC uintptr
    45  
    46  	externalthreadhandlerp uintptr // initialized elsewhere
    47  )
    48  
    49  func tracebackinit() {
    50  	// Go variable initialization happens late during runtime startup.
    51  	// Instead of initializing the variables above in the declarations,
    52  	// schedinit calls this function so that the variables are
    53  	// initialized and available earlier in the startup sequence.
    54  	deferprocPC = funcPC(deferproc)
    55  	goexitPC = funcPC(goexit)
    56  	jmpdeferPC = funcPC(jmpdefer)
    57  	mcallPC = funcPC(mcall)
    58  	morestackPC = funcPC(morestack)
    59  	mstartPC = funcPC(mstart)
    60  	newprocPC = funcPC(newproc)
    61  	rt0_goPC = funcPC(rt0_go)
    62  	sigpanicPC = funcPC(sigpanic)
    63  	systemstack_switchPC = funcPC(systemstack_switch)
    64  }
    65  
    66  // Traceback over the deferred function calls.
    67  // Report them like calls that have been invoked but not started executing yet.
    68  func tracebackdefers(gp *g, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer) {
    69  	var frame stkframe
    70  	for d := gp._defer; d != nil; d = d.link {
    71  		fn := d.fn
    72  		if fn == nil {
    73  			// Defer of nil function. Args don't matter.
    74  			frame.pc = 0
    75  			frame.fn = nil
    76  			frame.argp = 0
    77  			frame.arglen = 0
    78  			frame.argmap = nil
    79  		} else {
    80  			frame.pc = uintptr(fn.fn)
    81  			f := findfunc(frame.pc)
    82  			if f == nil {
    83  				print("runtime: unknown pc in defer ", hex(frame.pc), "\n")
    84  				gothrow("unknown pc")
    85  			}
    86  			frame.fn = f
    87  			frame.argp = uintptr(deferArgs(d))
    88  			setArgInfo(&frame, f, true)
    89  		}
    90  		frame.continpc = frame.pc
    91  		if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
    92  			return
    93  		}
    94  	}
    95  }
    96  
    97  // Generic traceback.  Handles runtime stack prints (pcbuf == nil),
    98  // the runtime.Callers function (pcbuf != nil), as well as the garbage
    99  // collector (callback != nil).  A little clunky to merge these, but avoids
   100  // duplicating the code and all its subtlety.
   101  func gentraceback(pc0 uintptr, sp0 uintptr, lr0 uintptr, gp *g, skip int, pcbuf *uintptr, max int, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer, flags uint) int {
   102  	if goexitPC == 0 {
   103  		gothrow("gentraceback before goexitPC initialization")
   104  	}
   105  	g := getg()
   106  	if g == gp && g == g.m.curg {
   107  		// The starting sp has been passed in as a uintptr, and the caller may
   108  		// have other uintptr-typed stack references as well.
   109  		// If during one of the calls that got us here or during one of the
   110  		// callbacks below the stack must be grown, all these uintptr references
   111  		// to the stack will not be updated, and gentraceback will continue
   112  		// to inspect the old stack memory, which may no longer be valid.
   113  		// Even if all the variables were updated correctly, it is not clear that
   114  		// we want to expose a traceback that begins on one stack and ends
   115  		// on another stack. That could confuse callers quite a bit.
   116  		// Instead, we require that gentraceback and any other function that
   117  		// accepts an sp for the current goroutine (typically obtained by
   118  		// calling getcallersp) must not run on that goroutine's stack but
   119  		// instead on the g0 stack.
   120  		gothrow("gentraceback cannot trace user goroutine on its own stack")
   121  	}
   122  	gotraceback := gotraceback(nil)
   123  	if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp.
   124  		if gp.syscallsp != 0 {
   125  			pc0 = gp.syscallpc
   126  			sp0 = gp.syscallsp
   127  			if usesLR {
   128  				lr0 = 0
   129  			}
   130  		} else {
   131  			pc0 = gp.sched.pc
   132  			sp0 = gp.sched.sp
   133  			if usesLR {
   134  				lr0 = gp.sched.lr
   135  			}
   136  		}
   137  	}
   138  
   139  	nprint := 0
   140  	var frame stkframe
   141  	frame.pc = pc0
   142  	frame.sp = sp0
   143  	if usesLR {
   144  		frame.lr = lr0
   145  	}
   146  	waspanic := false
   147  	wasnewproc := false
   148  	printing := pcbuf == nil && callback == nil
   149  	_defer := gp._defer
   150  
   151  	for _defer != nil && uintptr(_defer.argp) == _NoArgs {
   152  		_defer = _defer.link
   153  	}
   154  
   155  	// If the PC is zero, it's likely a nil function call.
   156  	// Start in the caller's frame.
   157  	if frame.pc == 0 {
   158  		if usesLR {
   159  			frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp))
   160  			frame.lr = 0
   161  		} else {
   162  			frame.pc = uintptr(*(*uintreg)(unsafe.Pointer(frame.sp)))
   163  			frame.sp += regSize
   164  		}
   165  	}
   166  
   167  	f := findfunc(frame.pc)
   168  	if f == nil {
   169  		if callback != nil {
   170  			print("runtime: unknown pc ", hex(frame.pc), "\n")
   171  			gothrow("unknown pc")
   172  		}
   173  		return 0
   174  	}
   175  	frame.fn = f
   176  
   177  	n := 0
   178  	for n < max {
   179  		// Typically:
   180  		//	pc is the PC of the running function.
   181  		//	sp is the stack pointer at that program counter.
   182  		//	fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown.
   183  		//	stk is the stack containing sp.
   184  		//	The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp.
   185  		f = frame.fn
   186  
   187  		// Found an actual function.
   188  		// Derive frame pointer and link register.
   189  		if frame.fp == 0 {
   190  			frame.fp = frame.sp + uintptr(funcspdelta(f, frame.pc))
   191  			if !usesLR {
   192  				// On x86, call instruction pushes return PC before entering new function.
   193  				frame.fp += regSize
   194  			}
   195  		}
   196  		var flr *_func
   197  		if topofstack(f) {
   198  			frame.lr = 0
   199  			flr = nil
   200  		} else if usesLR && f.entry == jmpdeferPC {
   201  			// jmpdefer modifies SP/LR/PC non-atomically.
   202  			// If a profiling interrupt arrives during jmpdefer,
   203  			// the stack unwind may see a mismatched register set
   204  			// and get confused. Stop if we see PC within jmpdefer
   205  			// to avoid that confusion.
   206  			// See golang.org/issue/8153.
   207  			if callback != nil {
   208  				gothrow("traceback_arm: found jmpdefer when tracing with callback")
   209  			}
   210  			frame.lr = 0
   211  		} else {
   212  			if usesLR {
   213  				if n == 0 && frame.sp < frame.fp || frame.lr == 0 {
   214  					frame.lr = *(*uintptr)(unsafe.Pointer(frame.sp))
   215  				}
   216  			} else {
   217  				if frame.lr == 0 {
   218  					frame.lr = uintptr(*(*uintreg)(unsafe.Pointer(frame.fp - regSize)))
   219  				}
   220  			}
   221  			flr = findfunc(frame.lr)
   222  			if flr == nil {
   223  				// This happens if you get a profiling interrupt at just the wrong time.
   224  				// In that context it is okay to stop early.
   225  				// But if callback is set, we're doing a garbage collection and must
   226  				// get everything, so crash loudly.
   227  				if callback != nil {
   228  					print("runtime: unexpected return pc for ", gofuncname(f), " called from ", hex(frame.lr), "\n")
   229  					gothrow("unknown caller pc")
   230  				}
   231  			}
   232  		}
   233  
   234  		frame.varp = frame.fp
   235  		if !usesLR {
   236  			// On x86, call instruction pushes return PC before entering new function.
   237  			frame.varp -= regSize
   238  		}
   239  
   240  		// Derive size of arguments.
   241  		// Most functions have a fixed-size argument block,
   242  		// so we can use metadata about the function f.
   243  		// Not all, though: there are some variadic functions
   244  		// in package runtime and reflect, and for those we use call-specific
   245  		// metadata recorded by f's caller.
   246  		if callback != nil || printing {
   247  			frame.argp = frame.fp
   248  			if usesLR {
   249  				frame.argp += ptrSize
   250  			}
   251  			setArgInfo(&frame, f, callback != nil)
   252  		}
   253  
   254  		// Determine function SP where deferproc would find its arguments.
   255  		var sparg uintptr
   256  		if usesLR {
   257  			// On link register architectures, that's the standard bottom-of-stack plus 1 word
   258  			// for the saved LR. If the previous frame was a direct call to newproc/deferproc,
   259  			// however, the SP is three words lower than normal.
   260  			// If the function has no frame at all - perhaps it just started, or perhaps
   261  			// it is a leaf with no local variables - then we cannot possibly find its
   262  			// SP in a defer, and we might confuse its SP for its caller's SP, so
   263  			// leave sparg=0 in that case.
   264  			if frame.fp != frame.sp {
   265  				sparg = frame.sp + regSize
   266  				if wasnewproc {
   267  					sparg += 3 * regSize
   268  				}
   269  			}
   270  		} else {
   271  			// On x86 that's the standard bottom-of-stack, so SP exactly.
   272  			// If the previous frame was a direct call to newproc/deferproc, however,
   273  			// the SP is two words lower than normal.
   274  			sparg = frame.sp
   275  			if wasnewproc {
   276  				sparg += 2 * ptrSize
   277  			}
   278  		}
   279  
   280  		// Determine frame's 'continuation PC', where it can continue.
   281  		// Normally this is the return address on the stack, but if sigpanic
   282  		// is immediately below this function on the stack, then the frame
   283  		// stopped executing due to a trap, and frame.pc is probably not
   284  		// a safe point for looking up liveness information. In this panicking case,
   285  		// the function either doesn't return at all (if it has no defers or if the
   286  		// defers do not recover) or it returns from one of the calls to
   287  		// deferproc a second time (if the corresponding deferred func recovers).
   288  		// It suffices to assume that the most recent deferproc is the one that
   289  		// returns; everything live at earlier deferprocs is still live at that one.
   290  		frame.continpc = frame.pc
   291  		if waspanic {
   292  			if _defer != nil && _defer.argp == sparg {
   293  				frame.continpc = _defer.pc
   294  			} else {
   295  				frame.continpc = 0
   296  			}
   297  		}
   298  
   299  		// Unwind our local defer stack past this frame.
   300  		for _defer != nil && (_defer.argp == sparg || _defer.argp == _NoArgs) {
   301  			_defer = _defer.link
   302  		}
   303  
   304  		if skip > 0 {
   305  			skip--
   306  			goto skipped
   307  		}
   308  
   309  		if pcbuf != nil {
   310  			(*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = frame.pc
   311  		}
   312  		if callback != nil {
   313  			if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
   314  				return n
   315  			}
   316  		}
   317  		if printing {
   318  			if (flags&_TraceRuntimeFrames) != 0 || showframe(f, gp) {
   319  				// Print during crash.
   320  				//	main(0x1, 0x2, 0x3)
   321  				//		/home/rsc/go/src/runtime/x.go:23 +0xf
   322  				//
   323  				tracepc := frame.pc // back up to CALL instruction for funcline.
   324  				if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic {
   325  					tracepc--
   326  				}
   327  				print(gofuncname(f), "(")
   328  				argp := (*[100]uintptr)(unsafe.Pointer(frame.argp))
   329  				for i := uintptr(0); i < frame.arglen/ptrSize; i++ {
   330  					if i >= 10 {
   331  						print(", ...")
   332  						break
   333  					}
   334  					if i != 0 {
   335  						print(", ")
   336  					}
   337  					print(hex(argp[i]))
   338  				}
   339  				print(")\n")
   340  				file, line := funcline(f, tracepc)
   341  				print("\t", file, ":", line)
   342  				if frame.pc > f.entry {
   343  					print(" +", hex(frame.pc-f.entry))
   344  				}
   345  				if g.m.throwing > 0 && gp == g.m.curg || gotraceback >= 2 {
   346  					print(" fp=", hex(frame.fp), " sp=", hex(frame.sp))
   347  				}
   348  				print("\n")
   349  				nprint++
   350  			}
   351  		}
   352  		n++
   353  
   354  	skipped:
   355  		waspanic = f.entry == sigpanicPC
   356  		wasnewproc = f.entry == newprocPC || f.entry == deferprocPC
   357  
   358  		// Do not unwind past the bottom of the stack.
   359  		if flr == nil {
   360  			break
   361  		}
   362  
   363  		// Unwind to next frame.
   364  		frame.fn = flr
   365  		frame.pc = frame.lr
   366  		frame.lr = 0
   367  		frame.sp = frame.fp
   368  		frame.fp = 0
   369  		frame.argmap = nil
   370  
   371  		// On link register architectures, sighandler saves the LR on stack
   372  		// before faking a call to sigpanic.
   373  		if usesLR && waspanic {
   374  			x := *(*uintptr)(unsafe.Pointer(frame.sp))
   375  			frame.sp += ptrSize
   376  			f = findfunc(frame.pc)
   377  			frame.fn = f
   378  			if f == nil {
   379  				frame.pc = x
   380  			} else if f.frame == 0 {
   381  				frame.lr = x
   382  			}
   383  		}
   384  	}
   385  
   386  	if pcbuf == nil && callback == nil {
   387  		n = nprint
   388  	}
   389  
   390  	// If callback != nil, we're being called to gather stack information during
   391  	// garbage collection or stack growth. In that context, require that we used
   392  	// up the entire defer stack. If not, then there is a bug somewhere and the
   393  	// garbage collection or stack growth may not have seen the correct picture
   394  	// of the stack. Crash now instead of silently executing the garbage collection
   395  	// or stack copy incorrectly and setting up for a mysterious crash later.
   396  	//
   397  	// Note that panic != nil is okay here: there can be leftover panics,
   398  	// because the defers on the panic stack do not nest in frame order as
   399  	// they do on the defer stack. If you have:
   400  	//
   401  	//	frame 1 defers d1
   402  	//	frame 2 defers d2
   403  	//	frame 3 defers d3
   404  	//	frame 4 panics
   405  	//	frame 4's panic starts running defers
   406  	//	frame 5, running d3, defers d4
   407  	//	frame 5 panics
   408  	//	frame 5's panic starts running defers
   409  	//	frame 6, running d4, garbage collects
   410  	//	frame 6, running d2, garbage collects
   411  	//
   412  	// During the execution of d4, the panic stack is d4 -> d3, which
   413  	// is nested properly, and we'll treat frame 3 as resumable, because we
   414  	// can find d3. (And in fact frame 3 is resumable. If d4 recovers
   415  	// and frame 5 continues running, d3, d3 can recover and we'll
   416  	// resume execution in (returning from) frame 3.)
   417  	//
   418  	// During the execution of d2, however, the panic stack is d2 -> d3,
   419  	// which is inverted. The scan will match d2 to frame 2 but having
   420  	// d2 on the stack until then means it will not match d3 to frame 3.
   421  	// This is okay: if we're running d2, then all the defers after d2 have
   422  	// completed and their corresponding frames are dead. Not finding d3
   423  	// for frame 3 means we'll set frame 3's continpc == 0, which is correct
   424  	// (frame 3 is dead). At the end of the walk the panic stack can thus
   425  	// contain defers (d3 in this case) for dead frames. The inversion here
   426  	// always indicates a dead frame, and the effect of the inversion on the
   427  	// scan is to hide those dead frames, so the scan is still okay:
   428  	// what's left on the panic stack are exactly (and only) the dead frames.
   429  	//
   430  	// We require callback != nil here because only when callback != nil
   431  	// do we know that gentraceback is being called in a "must be correct"
   432  	// context as opposed to a "best effort" context. The tracebacks with
   433  	// callbacks only happen when everything is stopped nicely.
   434  	// At other times, such as when gathering a stack for a profiling signal
   435  	// or when printing a traceback during a crash, everything may not be
   436  	// stopped nicely, and the stack walk may not be able to complete.
   437  	// It's okay in those situations not to use up the entire defer stack:
   438  	// incomplete information then is still better than nothing.
   439  	if callback != nil && n < max && _defer != nil {
   440  		if _defer != nil {
   441  			print("runtime: g", gp.goid, ": leftover defer argp=", hex(_defer.argp), " pc=", hex(_defer.pc), "\n")
   442  		}
   443  		for _defer = gp._defer; _defer != nil; _defer = _defer.link {
   444  			print("\tdefer ", _defer, " argp=", hex(_defer.argp), " pc=", hex(_defer.pc), "\n")
   445  		}
   446  		gothrow("traceback has leftover defers")
   447  	}
   448  
   449  	return n
   450  }
   451  
   452  func setArgInfo(frame *stkframe, f *_func, needArgMap bool) {
   453  	frame.arglen = uintptr(f.args)
   454  	if needArgMap && f.args == _ArgsSizeUnknown {
   455  		// Extract argument bitmaps for reflect stubs from the calls they made to reflect.
   456  		switch gofuncname(f) {
   457  		case "reflect.makeFuncStub", "reflect.methodValueCall":
   458  			arg0 := frame.sp
   459  			if usesLR {
   460  				arg0 += ptrSize
   461  			}
   462  			fn := *(**[2]uintptr)(unsafe.Pointer(arg0))
   463  			if fn[0] != f.entry {
   464  				print("runtime: confused by ", gofuncname(f), "\n")
   465  				gothrow("reflect mismatch")
   466  			}
   467  			bv := (*bitvector)(unsafe.Pointer(fn[1]))
   468  			frame.arglen = uintptr(bv.n / 2 * ptrSize)
   469  			frame.argmap = bv
   470  		}
   471  	}
   472  }
   473  
   474  func printcreatedby(gp *g) {
   475  	// Show what created goroutine, except main goroutine (goid 1).
   476  	pc := gp.gopc
   477  	f := findfunc(pc)
   478  	if f != nil && showframe(f, gp) && gp.goid != 1 {
   479  		print("created by ", gofuncname(f), "\n")
   480  		tracepc := pc // back up to CALL instruction for funcline.
   481  		if pc > f.entry {
   482  			tracepc -= _PCQuantum
   483  		}
   484  		file, line := funcline(f, tracepc)
   485  		print("\t", file, ":", line)
   486  		if pc > f.entry {
   487  			print(" +", hex(pc-f.entry))
   488  		}
   489  		print("\n")
   490  	}
   491  }
   492  
   493  func traceback(pc uintptr, sp uintptr, lr uintptr, gp *g) {
   494  	traceback1(pc, sp, lr, gp, 0)
   495  }
   496  
   497  // tracebacktrap is like traceback but expects that the PC and SP were obtained
   498  // from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or getcallerpc/getcallersp.
   499  // Because they are from a trap instead of from a saved pair,
   500  // the initial PC must not be rewound to the previous instruction.
   501  // (All the saved pairs record a PC that is a return address, so we
   502  // rewind it into the CALL instruction.)
   503  func tracebacktrap(pc uintptr, sp uintptr, lr uintptr, gp *g) {
   504  	traceback1(pc, sp, lr, gp, _TraceTrap)
   505  }
   506  
   507  func traceback1(pc uintptr, sp uintptr, lr uintptr, gp *g, flags uint) {
   508  	var n int
   509  	if readgstatus(gp)&^_Gscan == _Gsyscall {
   510  		// Override registers if blocked in system call.
   511  		pc = gp.syscallpc
   512  		sp = gp.syscallsp
   513  		flags &^= _TraceTrap
   514  	}
   515  	// Print traceback. By default, omits runtime frames.
   516  	// If that means we print nothing at all, repeat forcing all frames printed.
   517  	n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags)
   518  	if n == 0 && (flags&_TraceRuntimeFrames) == 0 {
   519  		n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags|_TraceRuntimeFrames)
   520  	}
   521  	if n == _TracebackMaxFrames {
   522  		print("...additional frames elided...\n")
   523  	}
   524  	printcreatedby(gp)
   525  }
   526  
   527  func callers(skip int, pcbuf *uintptr, m int) int {
   528  	sp := getcallersp(unsafe.Pointer(&skip))
   529  	pc := uintptr(getcallerpc(unsafe.Pointer(&skip)))
   530  	var n int
   531  	systemstack(func() {
   532  		n = gentraceback(pc, sp, 0, getg(), skip, pcbuf, m, nil, nil, 0)
   533  	})
   534  	return n
   535  }
   536  
   537  func gcallers(gp *g, skip int, pcbuf *uintptr, m int) int {
   538  	return gentraceback(^uintptr(0), ^uintptr(0), 0, gp, skip, pcbuf, m, nil, nil, 0)
   539  }
   540  
   541  func showframe(f *_func, gp *g) bool {
   542  	g := getg()
   543  	if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig) {
   544  		return true
   545  	}
   546  	traceback := gotraceback(nil)
   547  	name := gostringnocopy(funcname(f))
   548  
   549  	// Special case: always show runtime.panic frame, so that we can
   550  	// see where a panic started in the middle of a stack trace.
   551  	// See golang.org/issue/5832.
   552  	if name == "runtime.panic" {
   553  		return true
   554  	}
   555  
   556  	return traceback > 1 || f != nil && contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
   557  }
   558  
   559  // isExportedRuntime reports whether name is an exported runtime function.
   560  // It is only for runtime functions, so ASCII A-Z is fine.
   561  func isExportedRuntime(name string) bool {
   562  	const n = len("runtime.")
   563  	return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
   564  }
   565  
   566  var gStatusStrings = [...]string{
   567  	_Gidle:      "idle",
   568  	_Grunnable:  "runnable",
   569  	_Grunning:   "running",
   570  	_Gsyscall:   "syscall",
   571  	_Gwaiting:   "waiting",
   572  	_Gdead:      "dead",
   573  	_Genqueue:   "enqueue",
   574  	_Gcopystack: "copystack",
   575  }
   576  
   577  var gScanStatusStrings = [...]string{
   578  	0:          "scan",
   579  	_Grunnable: "scanrunnable",
   580  	_Grunning:  "scanrunning",
   581  	_Gsyscall:  "scansyscall",
   582  	_Gwaiting:  "scanwaiting",
   583  	_Gdead:     "scandead",
   584  	_Genqueue:  "scanenqueue",
   585  }
   586  
   587  func goroutineheader(gp *g) {
   588  	gpstatus := readgstatus(gp)
   589  
   590  	// Basic string status
   591  	var status string
   592  	if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
   593  		status = gStatusStrings[gpstatus]
   594  	} else if gpstatus&_Gscan != 0 && 0 <= gpstatus&^_Gscan && gpstatus&^_Gscan < uint32(len(gStatusStrings)) {
   595  		status = gStatusStrings[gpstatus&^_Gscan]
   596  	} else {
   597  		status = "???"
   598  	}
   599  
   600  	// Override.
   601  	if (gpstatus == _Gwaiting || gpstatus == _Gscanwaiting) && gp.waitreason != "" {
   602  		status = gp.waitreason
   603  	}
   604  
   605  	// approx time the G is blocked, in minutes
   606  	var waitfor int64
   607  	gpstatus &^= _Gscan // drop the scan bit
   608  	if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
   609  		waitfor = (nanotime() - gp.waitsince) / 60e9
   610  	}
   611  	print("goroutine ", gp.goid, " [", status)
   612  	if waitfor >= 1 {
   613  		print(", ", waitfor, " minutes")
   614  	}
   615  	if gp.lockedm != nil {
   616  		print(", locked to thread")
   617  	}
   618  	print("]:\n")
   619  }
   620  
   621  func tracebackothers(me *g) {
   622  	level := gotraceback(nil)
   623  
   624  	// Show the current goroutine first, if we haven't already.
   625  	g := getg()
   626  	gp := g.m.curg
   627  	if gp != nil && gp != me {
   628  		print("\n")
   629  		goroutineheader(gp)
   630  		traceback(^uintptr(0), ^uintptr(0), 0, gp)
   631  	}
   632  
   633  	lock(&allglock)
   634  	for _, gp := range allgs {
   635  		if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || gp.issystem && level < 2 {
   636  			continue
   637  		}
   638  		print("\n")
   639  		goroutineheader(gp)
   640  		if readgstatus(gp)&^_Gscan == _Grunning {
   641  			print("\tgoroutine running on other thread; stack unavailable\n")
   642  			printcreatedby(gp)
   643  		} else {
   644  			traceback(^uintptr(0), ^uintptr(0), 0, gp)
   645  		}
   646  	}
   647  	unlock(&allglock)
   648  }
   649  
   650  // Does f mark the top of a goroutine stack?
   651  func topofstack(f *_func) bool {
   652  	pc := f.entry
   653  	return pc == goexitPC ||
   654  		pc == mstartPC ||
   655  		pc == mcallPC ||
   656  		pc == morestackPC ||
   657  		pc == rt0_goPC ||
   658  		externalthreadhandlerp != 0 && pc == externalthreadhandlerp
   659  }