github.com/eun/go@v0.0.0-20170811110501-92cfd07a6cfd/src/runtime/panic.go (about)

     1  // Copyright 2014 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  	"runtime/internal/atomic"
     9  	"runtime/internal/sys"
    10  	"unsafe"
    11  )
    12  
    13  // Calling panic with one of the errors below will call errorString.Error
    14  // which will call mallocgc to concatenate strings. That will fail if
    15  // malloc is locked, causing a confusing error message. Throw a better
    16  // error message instead.
    17  func panicCheckMalloc(err error) {
    18  	gp := getg()
    19  	if gp != nil && gp.m != nil && gp.m.mallocing != 0 {
    20  		throw(string(err.(errorString)))
    21  	}
    22  }
    23  
    24  var indexError = error(errorString("index out of range"))
    25  
    26  func panicindex() {
    27  	panicCheckMalloc(indexError)
    28  	panic(indexError)
    29  }
    30  
    31  var sliceError = error(errorString("slice bounds out of range"))
    32  
    33  func panicslice() {
    34  	panicCheckMalloc(sliceError)
    35  	panic(sliceError)
    36  }
    37  
    38  var divideError = error(errorString("integer divide by zero"))
    39  
    40  func panicdivide() {
    41  	panicCheckMalloc(divideError)
    42  	panic(divideError)
    43  }
    44  
    45  var overflowError = error(errorString("integer overflow"))
    46  
    47  func panicoverflow() {
    48  	panicCheckMalloc(overflowError)
    49  	panic(overflowError)
    50  }
    51  
    52  var floatError = error(errorString("floating point error"))
    53  
    54  func panicfloat() {
    55  	panicCheckMalloc(floatError)
    56  	panic(floatError)
    57  }
    58  
    59  var memoryError = error(errorString("invalid memory address or nil pointer dereference"))
    60  
    61  func panicmem() {
    62  	panicCheckMalloc(memoryError)
    63  	panic(memoryError)
    64  }
    65  
    66  func throwinit() {
    67  	throw("recursive call during initialization - linker skew")
    68  }
    69  
    70  // Create a new deferred function fn with siz bytes of arguments.
    71  // The compiler turns a defer statement into a call to this.
    72  //go:nosplit
    73  func deferproc(siz int32, fn *funcval) { // arguments of fn follow fn
    74  	if getg().m.curg != getg() {
    75  		// go code on the system stack can't defer
    76  		throw("defer on system stack")
    77  	}
    78  
    79  	// the arguments of fn are in a perilous state. The stack map
    80  	// for deferproc does not describe them. So we can't let garbage
    81  	// collection or stack copying trigger until we've copied them out
    82  	// to somewhere safe. The memmove below does that.
    83  	// Until the copy completes, we can only call nosplit routines.
    84  	sp := getcallersp(unsafe.Pointer(&siz))
    85  	argp := uintptr(unsafe.Pointer(&fn)) + unsafe.Sizeof(fn)
    86  	callerpc := getcallerpc(unsafe.Pointer(&siz))
    87  
    88  	d := newdefer(siz)
    89  	if d._panic != nil {
    90  		throw("deferproc: d.panic != nil after newdefer")
    91  	}
    92  	d.fn = fn
    93  	d.pc = callerpc
    94  	d.sp = sp
    95  	switch siz {
    96  	case 0:
    97  		// Do nothing.
    98  	case sys.PtrSize:
    99  		*(*uintptr)(deferArgs(d)) = *(*uintptr)(unsafe.Pointer(argp))
   100  	default:
   101  		memmove(deferArgs(d), unsafe.Pointer(argp), uintptr(siz))
   102  	}
   103  
   104  	// deferproc returns 0 normally.
   105  	// a deferred func that stops a panic
   106  	// makes the deferproc return 1.
   107  	// the code the compiler generates always
   108  	// checks the return value and jumps to the
   109  	// end of the function if deferproc returns != 0.
   110  	return0()
   111  	// No code can go here - the C return register has
   112  	// been set and must not be clobbered.
   113  }
   114  
   115  // Small malloc size classes >= 16 are the multiples of 16: 16, 32, 48, 64, 80, 96, 112, 128, 144, ...
   116  // Each P holds a pool for defers with small arg sizes.
   117  // Assign defer allocations to pools by rounding to 16, to match malloc size classes.
   118  
   119  const (
   120  	deferHeaderSize = unsafe.Sizeof(_defer{})
   121  	minDeferAlloc   = (deferHeaderSize + 15) &^ 15
   122  	minDeferArgs    = minDeferAlloc - deferHeaderSize
   123  )
   124  
   125  // defer size class for arg size sz
   126  //go:nosplit
   127  func deferclass(siz uintptr) uintptr {
   128  	if siz <= minDeferArgs {
   129  		return 0
   130  	}
   131  	return (siz - minDeferArgs + 15) / 16
   132  }
   133  
   134  // total size of memory block for defer with arg size sz
   135  func totaldefersize(siz uintptr) uintptr {
   136  	if siz <= minDeferArgs {
   137  		return minDeferAlloc
   138  	}
   139  	return deferHeaderSize + siz
   140  }
   141  
   142  // Ensure that defer arg sizes that map to the same defer size class
   143  // also map to the same malloc size class.
   144  func testdefersizes() {
   145  	var m [len(p{}.deferpool)]int32
   146  
   147  	for i := range m {
   148  		m[i] = -1
   149  	}
   150  	for i := uintptr(0); ; i++ {
   151  		defersc := deferclass(i)
   152  		if defersc >= uintptr(len(m)) {
   153  			break
   154  		}
   155  		siz := roundupsize(totaldefersize(i))
   156  		if m[defersc] < 0 {
   157  			m[defersc] = int32(siz)
   158  			continue
   159  		}
   160  		if m[defersc] != int32(siz) {
   161  			print("bad defer size class: i=", i, " siz=", siz, " defersc=", defersc, "\n")
   162  			throw("bad defer size class")
   163  		}
   164  	}
   165  }
   166  
   167  // The arguments associated with a deferred call are stored
   168  // immediately after the _defer header in memory.
   169  //go:nosplit
   170  func deferArgs(d *_defer) unsafe.Pointer {
   171  	if d.siz == 0 {
   172  		// Avoid pointer past the defer allocation.
   173  		return nil
   174  	}
   175  	return add(unsafe.Pointer(d), unsafe.Sizeof(*d))
   176  }
   177  
   178  var deferType *_type // type of _defer struct
   179  
   180  func init() {
   181  	var x interface{}
   182  	x = (*_defer)(nil)
   183  	deferType = (*(**ptrtype)(unsafe.Pointer(&x))).elem
   184  }
   185  
   186  // Allocate a Defer, usually using per-P pool.
   187  // Each defer must be released with freedefer.
   188  //
   189  // This must not grow the stack because there may be a frame without
   190  // stack map information when this is called.
   191  //
   192  //go:nosplit
   193  func newdefer(siz int32) *_defer {
   194  	var d *_defer
   195  	sc := deferclass(uintptr(siz))
   196  	gp := getg()
   197  	if sc < uintptr(len(p{}.deferpool)) {
   198  		pp := gp.m.p.ptr()
   199  		if len(pp.deferpool[sc]) == 0 && sched.deferpool[sc] != nil {
   200  			// Take the slow path on the system stack so
   201  			// we don't grow newdefer's stack.
   202  			systemstack(func() {
   203  				lock(&sched.deferlock)
   204  				for len(pp.deferpool[sc]) < cap(pp.deferpool[sc])/2 && sched.deferpool[sc] != nil {
   205  					d := sched.deferpool[sc]
   206  					sched.deferpool[sc] = d.link
   207  					d.link = nil
   208  					pp.deferpool[sc] = append(pp.deferpool[sc], d)
   209  				}
   210  				unlock(&sched.deferlock)
   211  			})
   212  		}
   213  		if n := len(pp.deferpool[sc]); n > 0 {
   214  			d = pp.deferpool[sc][n-1]
   215  			pp.deferpool[sc][n-1] = nil
   216  			pp.deferpool[sc] = pp.deferpool[sc][:n-1]
   217  		}
   218  	}
   219  	if d == nil {
   220  		// Allocate new defer+args.
   221  		systemstack(func() {
   222  			total := roundupsize(totaldefersize(uintptr(siz)))
   223  			d = (*_defer)(mallocgc(total, deferType, true))
   224  		})
   225  	}
   226  	d.siz = siz
   227  	d.link = gp._defer
   228  	gp._defer = d
   229  	return d
   230  }
   231  
   232  // Free the given defer.
   233  // The defer cannot be used after this call.
   234  //
   235  // This must not grow the stack because there may be a frame without a
   236  // stack map when this is called.
   237  //
   238  //go:nosplit
   239  func freedefer(d *_defer) {
   240  	if d._panic != nil {
   241  		freedeferpanic()
   242  	}
   243  	if d.fn != nil {
   244  		freedeferfn()
   245  	}
   246  	sc := deferclass(uintptr(d.siz))
   247  	if sc < uintptr(len(p{}.deferpool)) {
   248  		pp := getg().m.p.ptr()
   249  		if len(pp.deferpool[sc]) == cap(pp.deferpool[sc]) {
   250  			// Transfer half of local cache to the central cache.
   251  			//
   252  			// Take this slow path on the system stack so
   253  			// we don't grow freedefer's stack.
   254  			systemstack(func() {
   255  				var first, last *_defer
   256  				for len(pp.deferpool[sc]) > cap(pp.deferpool[sc])/2 {
   257  					n := len(pp.deferpool[sc])
   258  					d := pp.deferpool[sc][n-1]
   259  					pp.deferpool[sc][n-1] = nil
   260  					pp.deferpool[sc] = pp.deferpool[sc][:n-1]
   261  					if first == nil {
   262  						first = d
   263  					} else {
   264  						last.link = d
   265  					}
   266  					last = d
   267  				}
   268  				lock(&sched.deferlock)
   269  				last.link = sched.deferpool[sc]
   270  				sched.deferpool[sc] = first
   271  				unlock(&sched.deferlock)
   272  			})
   273  		}
   274  		*d = _defer{}
   275  		pp.deferpool[sc] = append(pp.deferpool[sc], d)
   276  	}
   277  }
   278  
   279  // Separate function so that it can split stack.
   280  // Windows otherwise runs out of stack space.
   281  func freedeferpanic() {
   282  	// _panic must be cleared before d is unlinked from gp.
   283  	throw("freedefer with d._panic != nil")
   284  }
   285  
   286  func freedeferfn() {
   287  	// fn must be cleared before d is unlinked from gp.
   288  	throw("freedefer with d.fn != nil")
   289  }
   290  
   291  // Run a deferred function if there is one.
   292  // The compiler inserts a call to this at the end of any
   293  // function which calls defer.
   294  // If there is a deferred function, this will call runtime·jmpdefer,
   295  // which will jump to the deferred function such that it appears
   296  // to have been called by the caller of deferreturn at the point
   297  // just before deferreturn was called. The effect is that deferreturn
   298  // is called again and again until there are no more deferred functions.
   299  // Cannot split the stack because we reuse the caller's frame to
   300  // call the deferred function.
   301  
   302  // The single argument isn't actually used - it just has its address
   303  // taken so it can be matched against pending defers.
   304  //go:nosplit
   305  func deferreturn(arg0 uintptr) {
   306  	gp := getg()
   307  	d := gp._defer
   308  	if d == nil {
   309  		return
   310  	}
   311  	sp := getcallersp(unsafe.Pointer(&arg0))
   312  	if d.sp != sp {
   313  		return
   314  	}
   315  
   316  	// Moving arguments around.
   317  	//
   318  	// Everything called after this point must be recursively
   319  	// nosplit because the garbage collector won't know the form
   320  	// of the arguments until the jmpdefer can flip the PC over to
   321  	// fn.
   322  	switch d.siz {
   323  	case 0:
   324  		// Do nothing.
   325  	case sys.PtrSize:
   326  		*(*uintptr)(unsafe.Pointer(&arg0)) = *(*uintptr)(deferArgs(d))
   327  	default:
   328  		memmove(unsafe.Pointer(&arg0), deferArgs(d), uintptr(d.siz))
   329  	}
   330  	fn := d.fn
   331  	d.fn = nil
   332  	gp._defer = d.link
   333  	freedefer(d)
   334  	jmpdefer(fn, uintptr(unsafe.Pointer(&arg0)))
   335  }
   336  
   337  // Goexit terminates the goroutine that calls it. No other goroutine is affected.
   338  // Goexit runs all deferred calls before terminating the goroutine. Because Goexit
   339  // is not panic, however, any recover calls in those deferred functions will return nil.
   340  //
   341  // Calling Goexit from the main goroutine terminates that goroutine
   342  // without func main returning. Since func main has not returned,
   343  // the program continues execution of other goroutines.
   344  // If all other goroutines exit, the program crashes.
   345  func Goexit() {
   346  	// Run all deferred functions for the current goroutine.
   347  	// This code is similar to gopanic, see that implementation
   348  	// for detailed comments.
   349  	gp := getg()
   350  	for {
   351  		d := gp._defer
   352  		if d == nil {
   353  			break
   354  		}
   355  		if d.started {
   356  			if d._panic != nil {
   357  				d._panic.aborted = true
   358  				d._panic = nil
   359  			}
   360  			d.fn = nil
   361  			gp._defer = d.link
   362  			freedefer(d)
   363  			continue
   364  		}
   365  		d.started = true
   366  		reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
   367  		if gp._defer != d {
   368  			throw("bad defer entry in Goexit")
   369  		}
   370  		d._panic = nil
   371  		d.fn = nil
   372  		gp._defer = d.link
   373  		freedefer(d)
   374  		// Note: we ignore recovers here because Goexit isn't a panic
   375  	}
   376  	goexit1()
   377  }
   378  
   379  // Call all Error and String methods before freezing the world.
   380  // Used when crashing with panicking.
   381  // This must match types handled by printany.
   382  func preprintpanics(p *_panic) {
   383  	defer func() {
   384  		if recover() != nil {
   385  			throw("panic while printing panic value")
   386  		}
   387  	}()
   388  	for p != nil {
   389  		switch v := p.arg.(type) {
   390  		case error:
   391  			p.arg = v.Error()
   392  		case stringer:
   393  			p.arg = v.String()
   394  		}
   395  		p = p.link
   396  	}
   397  }
   398  
   399  // Print all currently active panics. Used when crashing.
   400  func printpanics(p *_panic) {
   401  	if p.link != nil {
   402  		printpanics(p.link)
   403  		print("\t")
   404  	}
   405  	print("panic: ")
   406  	printany(p.arg)
   407  	if p.recovered {
   408  		print(" [recovered]")
   409  	}
   410  	print("\n")
   411  }
   412  
   413  // The implementation of the predeclared function panic.
   414  func gopanic(e interface{}) {
   415  	gp := getg()
   416  	if gp.m.curg != gp {
   417  		print("panic: ")
   418  		printany(e)
   419  		print("\n")
   420  		throw("panic on system stack")
   421  	}
   422  
   423  	// m.softfloat is set during software floating point.
   424  	// It increments m.locks to avoid preemption.
   425  	// We moved the memory loads out, so there shouldn't be
   426  	// any reason for it to panic anymore.
   427  	if gp.m.softfloat != 0 {
   428  		gp.m.locks--
   429  		gp.m.softfloat = 0
   430  		throw("panic during softfloat")
   431  	}
   432  	if gp.m.mallocing != 0 {
   433  		print("panic: ")
   434  		printany(e)
   435  		print("\n")
   436  		throw("panic during malloc")
   437  	}
   438  	if gp.m.preemptoff != "" {
   439  		print("panic: ")
   440  		printany(e)
   441  		print("\n")
   442  		print("preempt off reason: ")
   443  		print(gp.m.preemptoff)
   444  		print("\n")
   445  		throw("panic during preemptoff")
   446  	}
   447  	if gp.m.locks != 0 {
   448  		print("panic: ")
   449  		printany(e)
   450  		print("\n")
   451  		throw("panic holding locks")
   452  	}
   453  
   454  	var p _panic
   455  	p.arg = e
   456  	p.link = gp._panic
   457  	gp._panic = (*_panic)(noescape(unsafe.Pointer(&p)))
   458  
   459  	atomic.Xadd(&runningPanicDefers, 1)
   460  
   461  	for {
   462  		d := gp._defer
   463  		if d == nil {
   464  			break
   465  		}
   466  
   467  		// If defer was started by earlier panic or Goexit (and, since we're back here, that triggered a new panic),
   468  		// take defer off list. The earlier panic or Goexit will not continue running.
   469  		if d.started {
   470  			if d._panic != nil {
   471  				d._panic.aborted = true
   472  			}
   473  			d._panic = nil
   474  			d.fn = nil
   475  			gp._defer = d.link
   476  			freedefer(d)
   477  			continue
   478  		}
   479  
   480  		// Mark defer as started, but keep on list, so that traceback
   481  		// can find and update the defer's argument frame if stack growth
   482  		// or a garbage collection happens before reflectcall starts executing d.fn.
   483  		d.started = true
   484  
   485  		// Record the panic that is running the defer.
   486  		// If there is a new panic during the deferred call, that panic
   487  		// will find d in the list and will mark d._panic (this panic) aborted.
   488  		d._panic = (*_panic)(noescape(unsafe.Pointer(&p)))
   489  
   490  		p.argp = unsafe.Pointer(getargp(0))
   491  		reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
   492  		p.argp = nil
   493  
   494  		// reflectcall did not panic. Remove d.
   495  		if gp._defer != d {
   496  			throw("bad defer entry in panic")
   497  		}
   498  		d._panic = nil
   499  		d.fn = nil
   500  		gp._defer = d.link
   501  
   502  		// trigger shrinkage to test stack copy. See stack_test.go:TestStackPanic
   503  		//GC()
   504  
   505  		pc := d.pc
   506  		sp := unsafe.Pointer(d.sp) // must be pointer so it gets adjusted during stack copy
   507  		freedefer(d)
   508  		if p.recovered {
   509  			atomic.Xadd(&runningPanicDefers, -1)
   510  
   511  			gp._panic = p.link
   512  			// Aborted panics are marked but remain on the g.panic list.
   513  			// Remove them from the list.
   514  			for gp._panic != nil && gp._panic.aborted {
   515  				gp._panic = gp._panic.link
   516  			}
   517  			if gp._panic == nil { // must be done with signal
   518  				gp.sig = 0
   519  			}
   520  			// Pass information about recovering frame to recovery.
   521  			gp.sigcode0 = uintptr(sp)
   522  			gp.sigcode1 = pc
   523  			mcall(recovery)
   524  			throw("recovery failed") // mcall should not return
   525  		}
   526  	}
   527  
   528  	// ran out of deferred calls - old-school panic now
   529  	// Because it is unsafe to call arbitrary user code after freezing
   530  	// the world, we call preprintpanics to invoke all necessary Error
   531  	// and String methods to prepare the panic strings before startpanic.
   532  	preprintpanics(gp._panic)
   533  	startpanic()
   534  
   535  	// startpanic set panicking, which will block main from exiting,
   536  	// so now OK to decrement runningPanicDefers.
   537  	atomic.Xadd(&runningPanicDefers, -1)
   538  
   539  	printpanics(gp._panic)
   540  	dopanic(0)       // should not return
   541  	*(*int)(nil) = 0 // not reached
   542  }
   543  
   544  // getargp returns the location where the caller
   545  // writes outgoing function call arguments.
   546  //go:nosplit
   547  //go:noinline
   548  func getargp(x int) uintptr {
   549  	// x is an argument mainly so that we can return its address.
   550  	return uintptr(noescape(unsafe.Pointer(&x)))
   551  }
   552  
   553  // The implementation of the predeclared function recover.
   554  // Cannot split the stack because it needs to reliably
   555  // find the stack segment of its caller.
   556  //
   557  // TODO(rsc): Once we commit to CopyStackAlways,
   558  // this doesn't need to be nosplit.
   559  //go:nosplit
   560  func gorecover(argp uintptr) interface{} {
   561  	// Must be in a function running as part of a deferred call during the panic.
   562  	// Must be called from the topmost function of the call
   563  	// (the function used in the defer statement).
   564  	// p.argp is the argument pointer of that topmost deferred function call.
   565  	// Compare against argp reported by caller.
   566  	// If they match, the caller is the one who can recover.
   567  	gp := getg()
   568  	p := gp._panic
   569  	if p != nil && !p.recovered && argp == uintptr(p.argp) {
   570  		p.recovered = true
   571  		return p.arg
   572  	}
   573  	return nil
   574  }
   575  
   576  //go:nosplit
   577  func startpanic() {
   578  	systemstack(startpanic_m)
   579  }
   580  
   581  //go:nosplit
   582  func dopanic(unused int) {
   583  	pc := getcallerpc(unsafe.Pointer(&unused))
   584  	sp := getcallersp(unsafe.Pointer(&unused))
   585  	gp := getg()
   586  	systemstack(func() {
   587  		dopanic_m(gp, pc, sp) // should never return
   588  	})
   589  	*(*int)(nil) = 0
   590  }
   591  
   592  //go:linkname sync_throw sync.throw
   593  func sync_throw(s string) {
   594  	throw(s)
   595  }
   596  
   597  //go:nosplit
   598  func throw(s string) {
   599  	print("fatal error: ", s, "\n")
   600  	gp := getg()
   601  	if gp.m.throwing == 0 {
   602  		gp.m.throwing = 1
   603  	}
   604  	startpanic()
   605  	dopanic(0)
   606  	*(*int)(nil) = 0 // not reached
   607  }
   608  
   609  // runningPanicDefers is non-zero while running deferred functions for panic.
   610  // runningPanicDefers is incremented and decremented atomically.
   611  // This is used to try hard to get a panic stack trace out when exiting.
   612  var runningPanicDefers uint32
   613  
   614  // panicking is non-zero when crashing the program for an unrecovered panic.
   615  // panicking is incremented and decremented atomically.
   616  var panicking uint32
   617  
   618  // paniclk is held while printing the panic information and stack trace,
   619  // so that two concurrent panics don't overlap their output.
   620  var paniclk mutex
   621  
   622  // Unwind the stack after a deferred function calls recover
   623  // after a panic. Then arrange to continue running as though
   624  // the caller of the deferred function returned normally.
   625  func recovery(gp *g) {
   626  	// Info about defer passed in G struct.
   627  	sp := gp.sigcode0
   628  	pc := gp.sigcode1
   629  
   630  	// d's arguments need to be in the stack.
   631  	if sp != 0 && (sp < gp.stack.lo || gp.stack.hi < sp) {
   632  		print("recover: ", hex(sp), " not in [", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n")
   633  		throw("bad recovery")
   634  	}
   635  
   636  	// Make the deferproc for this d return again,
   637  	// this time returning 1.  The calling function will
   638  	// jump to the standard return epilogue.
   639  	gp.sched.sp = sp
   640  	gp.sched.pc = pc
   641  	gp.sched.lr = 0
   642  	gp.sched.ret = 1
   643  	gogo(&gp.sched)
   644  }
   645  
   646  func startpanic_m() {
   647  	_g_ := getg()
   648  	if mheap_.cachealloc.size == 0 { // very early
   649  		print("runtime: panic before malloc heap initialized\n")
   650  		_g_.m.mallocing = 1 // tell rest of panic not to try to malloc
   651  	} else if _g_.m.mcache == nil { // can happen if called from signal handler or throw
   652  		_g_.m.mcache = allocmcache()
   653  	}
   654  
   655  	switch _g_.m.dying {
   656  	case 0:
   657  		_g_.m.dying = 1
   658  		_g_.writebuf = nil
   659  		atomic.Xadd(&panicking, 1)
   660  		lock(&paniclk)
   661  		if debug.schedtrace > 0 || debug.scheddetail > 0 {
   662  			schedtrace(true)
   663  		}
   664  		freezetheworld()
   665  		return
   666  	case 1:
   667  		// Something failed while panicking, probably the print of the
   668  		// argument to panic().  Just print a stack trace and exit.
   669  		_g_.m.dying = 2
   670  		print("panic during panic\n")
   671  		dopanic(0)
   672  		exit(3)
   673  		fallthrough
   674  	case 2:
   675  		// This is a genuine bug in the runtime, we couldn't even
   676  		// print the stack trace successfully.
   677  		_g_.m.dying = 3
   678  		print("stack trace unavailable\n")
   679  		exit(4)
   680  		fallthrough
   681  	default:
   682  		// Can't even print!  Just exit.
   683  		exit(5)
   684  	}
   685  }
   686  
   687  var didothers bool
   688  var deadlock mutex
   689  
   690  func dopanic_m(gp *g, pc, sp uintptr) {
   691  	if gp.sig != 0 {
   692  		signame := signame(gp.sig)
   693  		if signame != "" {
   694  			print("[signal ", signame)
   695  		} else {
   696  			print("[signal ", hex(gp.sig))
   697  		}
   698  		print(" code=", hex(gp.sigcode0), " addr=", hex(gp.sigcode1), " pc=", hex(gp.sigpc), "]\n")
   699  	}
   700  
   701  	level, all, docrash := gotraceback()
   702  	_g_ := getg()
   703  	if level > 0 {
   704  		if gp != gp.m.curg {
   705  			all = true
   706  		}
   707  		if gp != gp.m.g0 {
   708  			print("\n")
   709  			goroutineheader(gp)
   710  			traceback(pc, sp, 0, gp)
   711  		} else if level >= 2 || _g_.m.throwing > 0 {
   712  			print("\nruntime stack:\n")
   713  			traceback(pc, sp, 0, gp)
   714  		}
   715  		if !didothers && all {
   716  			didothers = true
   717  			tracebackothers(gp)
   718  		}
   719  	}
   720  	unlock(&paniclk)
   721  
   722  	if atomic.Xadd(&panicking, -1) != 0 {
   723  		// Some other m is panicking too.
   724  		// Let it print what it needs to print.
   725  		// Wait forever without chewing up cpu.
   726  		// It will exit when it's done.
   727  		lock(&deadlock)
   728  		lock(&deadlock)
   729  	}
   730  
   731  	if docrash {
   732  		crash()
   733  	}
   734  
   735  	exit(2)
   736  }
   737  
   738  //go:nosplit
   739  func canpanic(gp *g) bool {
   740  	// Note that g is m->gsignal, different from gp.
   741  	// Note also that g->m can change at preemption, so m can go stale
   742  	// if this function ever makes a function call.
   743  	_g_ := getg()
   744  	_m_ := _g_.m
   745  
   746  	// Is it okay for gp to panic instead of crashing the program?
   747  	// Yes, as long as it is running Go code, not runtime code,
   748  	// and not stuck in a system call.
   749  	if gp == nil || gp != _m_.curg {
   750  		return false
   751  	}
   752  	if _m_.locks-_m_.softfloat != 0 || _m_.mallocing != 0 || _m_.throwing != 0 || _m_.preemptoff != "" || _m_.dying != 0 {
   753  		return false
   754  	}
   755  	status := readgstatus(gp)
   756  	if status&^_Gscan != _Grunning || gp.syscallsp != 0 {
   757  		return false
   758  	}
   759  	if GOOS == "windows" && _m_.libcallsp != 0 {
   760  		return false
   761  	}
   762  	return true
   763  }