* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / libgo / go / runtime / panic.go
blob2f656038a9e517a19ae19a427542d1f39afebbc6
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.
5 package runtime
7 import (
8 "runtime/internal/atomic"
9 "unsafe"
12 // For gccgo, use go:linkname to rename compiler-called functions to
13 // themselves, so that the compiler will export them.
15 //go:linkname deferproc runtime.deferproc
16 //go:linkname deferreturn runtime.deferreturn
17 //go:linkname setdeferretaddr runtime.setdeferretaddr
18 //go:linkname checkdefer runtime.checkdefer
19 //go:linkname gopanic runtime.gopanic
20 //go:linkname canrecover runtime.canrecover
21 //go:linkname makefuncfficanrecover runtime.makefuncfficanrecover
22 //go:linkname makefuncreturning runtime.makefuncreturning
23 //go:linkname gorecover runtime.gorecover
24 //go:linkname deferredrecover runtime.deferredrecover
25 // Temporary for C code to call:
26 //go:linkname throw runtime.throw
28 // Calling panic with one of the errors below will call errorString.Error
29 // which will call mallocgc to concatenate strings. That will fail if
30 // malloc is locked, causing a confusing error message. Throw a better
31 // error message instead.
32 func panicCheckMalloc(err error) {
33 gp := getg()
34 if gp != nil && gp.m != nil && gp.m.mallocing != 0 {
35 throw(string(err.(errorString)))
39 var indexError = error(errorString("index out of range"))
41 func panicindex() {
42 panicCheckMalloc(indexError)
43 panic(indexError)
46 var sliceError = error(errorString("slice bounds out of range"))
48 func panicslice() {
49 panicCheckMalloc(sliceError)
50 panic(sliceError)
53 var divideError = error(errorString("integer divide by zero"))
55 func panicdivide() {
56 panicCheckMalloc(divideError)
57 panic(divideError)
60 var overflowError = error(errorString("integer overflow"))
62 func panicoverflow() {
63 panicCheckMalloc(overflowError)
64 panic(overflowError)
67 var floatError = error(errorString("floating point error"))
69 func panicfloat() {
70 panicCheckMalloc(floatError)
71 panic(floatError)
74 var memoryError = error(errorString("invalid memory address or nil pointer dereference"))
76 func panicmem() {
77 panicCheckMalloc(memoryError)
78 panic(memoryError)
81 func throwinit() {
82 throw("recursive call during initialization - linker skew")
85 // deferproc creates a new deferred function.
86 // The compiler turns a defer statement into a call to this.
87 // frame points into the stack frame; it is used to determine which
88 // deferred functions are for the current stack frame, and whether we
89 // have already deferred functions for this frame.
90 // pfn is a C function pointer.
91 // arg is a value to pass to pfn.
92 func deferproc(frame *bool, pfn uintptr, arg unsafe.Pointer) {
93 d := newdefer()
94 if d._panic != nil {
95 throw("deferproc: d.panic != nil after newdefer")
97 d.frame = frame
98 d.panicStack = getg()._panic
99 d.pfn = pfn
100 d.arg = arg
101 d.retaddr = 0
102 d.makefunccanrecover = false
105 // Allocate a Defer, usually using per-P pool.
106 // Each defer must be released with freedefer.
107 func newdefer() *_defer {
108 var d *_defer
109 gp := getg()
110 pp := gp.m.p.ptr()
111 if len(pp.deferpool) == 0 && sched.deferpool != nil {
112 systemstack(func() {
113 lock(&sched.deferlock)
114 for len(pp.deferpool) < cap(pp.deferpool)/2 && sched.deferpool != nil {
115 d := sched.deferpool
116 sched.deferpool = d.link
117 d.link = nil
118 pp.deferpool = append(pp.deferpool, d)
120 unlock(&sched.deferlock)
123 if n := len(pp.deferpool); n > 0 {
124 d = pp.deferpool[n-1]
125 pp.deferpool[n-1] = nil
126 pp.deferpool = pp.deferpool[:n-1]
128 if d == nil {
129 systemstack(func() {
130 d = new(_defer)
133 d.link = gp._defer
134 gp._defer = d
135 return d
138 // Free the given defer.
139 // The defer cannot be used after this call.
141 // This must not grow the stack because there may be a frame without a
142 // stack map when this is called.
144 //go:nosplit
145 func freedefer(d *_defer) {
146 pp := getg().m.p.ptr()
147 if len(pp.deferpool) == cap(pp.deferpool) {
148 // Transfer half of local cache to the central cache.
150 // Take this slow path on the system stack so
151 // we don't grow freedefer's stack.
152 systemstack(func() {
153 var first, last *_defer
154 for len(pp.deferpool) > cap(pp.deferpool)/2 {
155 n := len(pp.deferpool)
156 d := pp.deferpool[n-1]
157 pp.deferpool[n-1] = nil
158 pp.deferpool = pp.deferpool[:n-1]
159 if first == nil {
160 first = d
161 } else {
162 last.link = d
164 last = d
166 lock(&sched.deferlock)
167 last.link = sched.deferpool
168 sched.deferpool = first
169 unlock(&sched.deferlock)
172 *d = _defer{}
173 pp.deferpool = append(pp.deferpool, d)
176 // deferreturn is called to undefer the stack.
177 // The compiler inserts a call to this function as a finally clause
178 // wrapped around the body of any function that calls defer.
179 // The frame argument points to the stack frame of the function.
180 func deferreturn(frame *bool) {
181 gp := getg()
182 for gp._defer != nil && gp._defer.frame == frame {
183 d := gp._defer
184 pfn := d.pfn
185 d.pfn = 0
187 if pfn != 0 {
188 // This is rather awkward.
189 // The gc compiler does this using assembler
190 // code in jmpdefer.
191 var fn func(unsafe.Pointer)
192 *(*uintptr)(unsafe.Pointer(&fn)) = uintptr(unsafe.Pointer(&pfn))
193 fn(d.arg)
196 // If we are returning from a Go function called by a
197 // C function running in a C thread, g may now be nil,
198 // in which case CgocallBackDone will have cleared _defer.
199 // In that case some other goroutine may already be using gp.
200 if getg() == nil {
201 *frame = true
202 return
205 gp._defer = d.link
207 freedefer(d)
209 // Since we are executing a defer function now, we
210 // know that we are returning from the calling
211 // function. If the calling function, or one of its
212 // callees, panicked, then the defer functions would
213 // be executed by panic.
214 *frame = true
218 // __builtin_extract_return_addr is a GCC intrinsic that converts an
219 // address returned by __builtin_return_address(0) to a real address.
220 // On most architectures this is a nop.
221 //extern __builtin_extract_return_addr
222 func __builtin_extract_return_addr(uintptr) uintptr
224 // setdeferretaddr records the address to which the deferred function
225 // returns. This is check by canrecover. The frontend relies on this
226 // function returning false.
227 func setdeferretaddr(retaddr uintptr) bool {
228 gp := getg()
229 if gp._defer != nil {
230 gp._defer.retaddr = __builtin_extract_return_addr(retaddr)
232 return false
235 // checkdefer is called by exception handlers used when unwinding the
236 // stack after a recovered panic. The exception handler is simply
237 // checkdefer(frame)
238 // return;
239 // If we have not yet reached the frame we are looking for, we
240 // continue unwinding.
241 func checkdefer(frame *bool) {
242 gp := getg()
243 if gp == nil {
244 // We should never wind up here. Even if some other
245 // language throws an exception, the cgo code
246 // should ensure that g is set.
247 throw("no g in checkdefer")
248 } else if gp.isforeign {
249 // Some other language has thrown an exception.
250 // We need to run the local defer handlers.
251 // If they call recover, we stop unwinding here.
252 var p _panic
253 p.isforeign = true
254 p.link = gp._panic
255 gp._panic = &p
256 for {
257 d := gp._defer
258 if d == nil || d.frame != frame || d.pfn == 0 {
259 break
262 pfn := d.pfn
263 gp._defer = d.link
265 var fn func(unsafe.Pointer)
266 *(*uintptr)(unsafe.Pointer(&fn)) = uintptr(unsafe.Pointer(&pfn))
267 fn(d.arg)
269 freedefer(d)
271 if p.recovered {
272 // The recover function caught the panic
273 // thrown by some other language.
274 break
278 recovered := p.recovered
279 gp._panic = p.link
281 if recovered {
282 // Just return and continue executing Go code.
283 *frame = true
284 return
287 // We are panicking through this function.
288 *frame = false
289 } else if gp._defer != nil && gp._defer.pfn == 0 && gp._defer.frame == frame {
290 // This is the defer function that called recover.
291 // Simply return to stop the stack unwind, and let the
292 // Go code continue to execute.
293 d := gp._defer
294 gp._defer = d.link
295 freedefer(d)
297 // We are returning from this function.
298 *frame = true
300 return
303 // This is some other defer function. It was already run by
304 // the call to panic, or just above. Rethrow the exception.
305 rethrowException()
306 throw("rethrowException returned")
309 // unwindStack starts unwinding the stack for a panic. We unwind
310 // function calls until we reach the one which used a defer function
311 // which called recover. Each function which uses a defer statement
312 // will have an exception handler, as shown above for checkdefer.
313 func unwindStack() {
314 // Allocate the exception type used by the unwind ABI.
315 // It would be nice to define it in runtime_sysinfo.go,
316 // but current definitions don't work because the required
317 // alignment is larger than can be represented in Go.
318 // The type never contains any Go pointers.
319 size := unwindExceptionSize()
320 usize := uintptr(unsafe.Sizeof(uintptr(0)))
321 c := (size + usize - 1) / usize
322 s := make([]uintptr, c)
323 getg().exception = unsafe.Pointer(&s[0])
324 throwException()
327 // Goexit terminates the goroutine that calls it. No other goroutine is affected.
328 // Goexit runs all deferred calls before terminating the goroutine. Because Goexit
329 // is not panic, however, any recover calls in those deferred functions will return nil.
331 // Calling Goexit from the main goroutine terminates that goroutine
332 // without func main returning. Since func main has not returned,
333 // the program continues execution of other goroutines.
334 // If all other goroutines exit, the program crashes.
335 func Goexit() {
336 // Run all deferred functions for the current goroutine.
337 // This code is similar to gopanic, see that implementation
338 // for detailed comments.
339 gp := getg()
340 for {
341 d := gp._defer
342 if d == nil {
343 break
346 pfn := d.pfn
347 if pfn == 0 {
348 if d._panic != nil {
349 d._panic.aborted = true
350 d._panic = nil
352 gp._defer = d.link
353 freedefer(d)
354 continue
356 d.pfn = 0
358 var fn func(unsafe.Pointer)
359 *(*uintptr)(unsafe.Pointer(&fn)) = uintptr(unsafe.Pointer(&pfn))
360 fn(d.arg)
362 if gp._defer != d {
363 throw("bad defer entry in Goexit")
365 d._panic = nil
366 gp._defer = d.link
367 freedefer(d)
368 // Note: we ignore recovers here because Goexit isn't a panic
370 goexit1()
373 // Call all Error and String methods before freezing the world.
374 // Used when crashing with panicking.
375 // This must match types handled by printany.
376 func preprintpanics(p *_panic) {
377 defer func() {
378 if recover() != nil {
379 throw("panic while printing panic value")
382 for p != nil {
383 switch v := p.arg.(type) {
384 case error:
385 p.arg = v.Error()
386 case stringer:
387 p.arg = v.String()
389 p = p.link
393 // Print all currently active panics. Used when crashing.
394 func printpanics(p *_panic) {
395 if p.link != nil {
396 printpanics(p.link)
397 print("\t")
399 print("panic: ")
400 printany(p.arg)
401 if p.recovered {
402 print(" [recovered]")
404 print("\n")
407 // The implementation of the predeclared function panic.
408 func gopanic(e interface{}) {
409 gp := getg()
410 if gp.m.curg != gp {
411 print("panic: ")
412 printany(e)
413 print("\n")
414 throw("panic on system stack")
417 if gp.m.mallocing != 0 {
418 print("panic: ")
419 printany(e)
420 print("\n")
421 throw("panic during malloc")
423 if gp.m.preemptoff != "" {
424 print("panic: ")
425 printany(e)
426 print("\n")
427 print("preempt off reason: ")
428 print(gp.m.preemptoff)
429 print("\n")
430 throw("panic during preemptoff")
432 if gp.m.locks != 0 {
433 print("panic: ")
434 printany(e)
435 print("\n")
436 throw("panic holding locks")
439 // The gc compiler allocates this new _panic struct on the
440 // stack. We can't do that, because when a deferred function
441 // recovers the panic we unwind the stack. We unlink this
442 // entry before unwinding the stack, but that doesn't help in
443 // the case where we panic, a deferred function recovers and
444 // then panics itself, that panic is in turn recovered, and
445 // unwinds the stack past this stack frame.
447 p := &_panic{
448 arg: e,
449 link: gp._panic,
451 gp._panic = p
453 atomic.Xadd(&runningPanicDefers, 1)
455 for {
456 d := gp._defer
457 if d == nil {
458 break
461 pfn := d.pfn
463 // If defer was started by earlier panic or Goexit (and, since we're back here, that triggered a new panic),
464 // take defer off list. The earlier panic or Goexit will not continue running.
465 if pfn == 0 {
466 if d._panic != nil {
467 d._panic.aborted = true
469 d._panic = nil
470 gp._defer = d.link
471 freedefer(d)
472 continue
474 d.pfn = 0
476 // Record the panic that is running the defer.
477 // If there is a new panic during the deferred call, that panic
478 // will find d in the list and will mark d._panic (this panic) aborted.
479 d._panic = p
481 var fn func(unsafe.Pointer)
482 *(*uintptr)(unsafe.Pointer(&fn)) = uintptr(unsafe.Pointer(&pfn))
483 fn(d.arg)
485 if gp._defer != d {
486 throw("bad defer entry in panic")
488 d._panic = nil
490 if p.recovered {
491 atomic.Xadd(&runningPanicDefers, -1)
493 gp._panic = p.link
495 // Aborted panics are marked but remain on the g.panic list.
496 // Remove them from the list.
497 for gp._panic != nil && gp._panic.aborted {
498 gp._panic = gp._panic.link
500 if gp._panic == nil { // must be done with signal
501 gp.sig = 0
504 // Unwind the stack by throwing an exception.
505 // The compiler has arranged to create
506 // exception handlers in each function
507 // that uses a defer statement. These
508 // exception handlers will check whether
509 // the entry on the top of the defer stack
510 // is from the current function. If it is,
511 // we have unwound the stack far enough.
512 unwindStack()
514 throw("unwindStack returned")
517 // Because we executed that defer function by a panic,
518 // and it did not call recover, we know that we are
519 // not returning from the calling function--we are
520 // panicking through it.
521 *d.frame = false
523 // Deferred function did not panic. Remove d.
524 // In the p.recovered case, d will be removed by checkdefer.
525 gp._defer = d.link
527 freedefer(d)
530 // ran out of deferred calls - old-school panic now
531 // Because it is unsafe to call arbitrary user code after freezing
532 // the world, we call preprintpanics to invoke all necessary Error
533 // and String methods to prepare the panic strings before startpanic.
534 preprintpanics(gp._panic)
535 startpanic()
537 // startpanic set panicking, which will block main from exiting,
538 // so now OK to decrement runningPanicDefers.
539 atomic.Xadd(&runningPanicDefers, -1)
541 printpanics(gp._panic)
542 dopanic(0) // should not return
543 *(*int)(nil) = 0 // not reached
546 // currentDefer returns the top of the defer stack if it can be recovered.
547 // Otherwise it returns nil.
548 func currentDefer() *_defer {
549 gp := getg()
550 d := gp._defer
551 if d == nil {
552 return nil
555 // The panic that would be recovered is the one on the top of
556 // the panic stack. We do not want to recover it if that panic
557 // was on the top of the panic stack when this function was
558 // deferred.
559 if d.panicStack == gp._panic {
560 return nil
563 // The deferred thunk will call setdeferretaddr. If this has
564 // not happened, then we have not been called via defer, and
565 // we can not recover.
566 if d.retaddr == 0 {
567 return nil
570 return d
573 // canrecover is called by a thunk to see if the real function would
574 // be permitted to recover a panic value. Recovering a value is
575 // permitted if the thunk was called directly by defer. retaddr is the
576 // return address of the function that is calling canrecover--that is,
577 // the thunk.
578 func canrecover(retaddr uintptr) bool {
579 d := currentDefer()
580 if d == nil {
581 return false
584 ret := __builtin_extract_return_addr(retaddr)
585 dret := d.retaddr
586 if ret <= dret && ret+16 >= dret {
587 return true
590 // On some systems, in some cases, the return address does not
591 // work reliably. See http://gcc.gnu.org/PR60406. If we are
592 // permitted to call recover, the call stack will look like this:
593 // runtime.gopanic, runtime.deferreturn, etc.
594 // thunk to call deferred function (calls __go_set_defer_retaddr)
595 // function that calls __go_can_recover (passing return address)
596 // runtime.canrecover
597 // Calling callers will skip the thunks. So if our caller's
598 // caller starts with "runtime.", then we are permitted to
599 // call recover.
600 var locs [16]location
601 if callers(2, locs[:2]) < 2 {
602 return false
605 name := locs[1].function
606 if hasprefix(name, "runtime.") {
607 return true
610 // If the function calling recover was created by reflect.MakeFunc,
611 // then makefuncfficanrecover will have set makefunccanrecover.
612 if !d.makefunccanrecover {
613 return false
616 // We look up the stack, ignoring libffi functions and
617 // functions in the reflect package, until we find
618 // reflect.makeFuncStub or reflect.ffi_callback called by FFI
619 // functions. Then we check the caller of that function.
621 n := callers(3, locs[:])
622 foundFFICallback := false
623 i := 0
624 for ; i < n; i++ {
625 name = locs[i].function
626 if name == "" {
627 // No function name means this caller isn't Go code.
628 // Assume that this is libffi.
629 continue
632 // Ignore function in libffi.
633 if hasprefix(name, "ffi_") {
634 continue
637 if foundFFICallback {
638 break
641 if name == "reflect.ffi_callback" {
642 foundFFICallback = true
643 continue
646 // Ignore other functions in the reflect package.
647 if hasprefix(name, "reflect.") {
648 continue
651 // We should now be looking at the real caller.
652 break
655 if i < n {
656 name = locs[i].function
657 if hasprefix(name, "runtime.") {
658 return true
662 return false
665 // This function is called when code is about to enter a function
666 // created by the libffi version of reflect.MakeFunc. This function is
667 // passed the names of the callers of the libffi code that called the
668 // stub. It uses them to decide whether it is permitted to call
669 // recover, and sets d.makefunccanrecover so that gorecover can make
670 // the same decision.
671 func makefuncfficanrecover(loc []location) {
672 d := currentDefer()
673 if d == nil {
674 return
677 // If we are already in a call stack of MakeFunc functions,
678 // there is nothing we can usefully check here.
679 if d.makefunccanrecover {
680 return
683 // loc starts with the caller of our caller. That will be a thunk.
684 // If its caller was a function function, then it was called
685 // directly by defer.
686 if len(loc) < 2 {
687 return
690 name := loc[1].function
691 if hasprefix(name, "runtime.") {
692 d.makefunccanrecover = true
696 // makefuncreturning is called when code is about to exit a function
697 // created by reflect.MakeFunc. It is called by the function stub used
698 // by reflect.MakeFunc. It clears the makefunccanrecover field. It's
699 // OK to always clear this field, because canrecover will only be
700 // called by a stub created for a function that calls recover. That
701 // stub will not call a function created by reflect.MakeFunc, so by
702 // the time we get here any caller higher up on the call stack no
703 // longer needs the information.
704 func makefuncreturning() {
705 d := getg()._defer
706 if d != nil {
707 d.makefunccanrecover = false
711 // The implementation of the predeclared function recover.
712 func gorecover() interface{} {
713 gp := getg()
714 p := gp._panic
715 if p != nil && !p.recovered {
716 p.recovered = true
717 return p.arg
719 return nil
722 // deferredrecover is called when a call to recover is deferred. That
723 // is, something like
724 // defer recover()
726 // We need to handle this specially. In gc, the recover function
727 // looks up the stack frame. In particular, that means that a deferred
728 // recover will not recover a panic thrown in the same function that
729 // defers the recover. It will only recover a panic thrown in a
730 // function that defers the deferred call to recover.
732 // In other words:
734 // func f1() {
735 // defer recover() // does not stop panic
736 // panic(0)
737 // }
739 // func f2() {
740 // defer func() {
741 // defer recover() // stops panic(0)
742 // }()
743 // panic(0)
744 // }
746 // func f3() {
747 // defer func() {
748 // defer recover() // does not stop panic
749 // panic(0)
750 // }()
751 // panic(1)
752 // }
754 // func f4() {
755 // defer func() {
756 // defer func() {
757 // defer recover() // stops panic(0)
758 // }()
759 // panic(0)
760 // }()
761 // panic(1)
762 // }
764 // The interesting case here is f3. As can be seen from f2, the
765 // deferred recover could pick up panic(1). However, this does not
766 // happen because it is blocked by the panic(0).
768 // When a function calls recover, then when we invoke it we pass a
769 // hidden parameter indicating whether it should recover something.
770 // This parameter is set based on whether the function is being
771 // invoked directly from defer. The parameter winds up determining
772 // whether __go_recover or __go_deferred_recover is called at all.
774 // In the case of a deferred recover, the hidden parameter that
775 // controls the call is actually the one set up for the function that
776 // runs the defer recover() statement. That is the right thing in all
777 // the cases above except for f3. In f3 the function is permitted to
778 // call recover, but the deferred recover call is not. We address that
779 // here by checking for that specific case before calling recover. If
780 // this function was deferred when there is already a panic on the
781 // panic stack, then we can only recover that panic, not any other.
783 // Note that we can get away with using a special function here
784 // because you are not permitted to take the address of a predeclared
785 // function like recover.
786 func deferredrecover() interface{} {
787 gp := getg()
788 if gp._defer == nil || gp._defer.panicStack != gp._panic {
789 return nil
791 return gorecover()
794 //go:linkname sync_throw sync.throw
795 func sync_throw(s string) {
796 throw(s)
799 //go:nosplit
800 func throw(s string) {
801 print("fatal error: ", s, "\n")
802 gp := getg()
803 if gp.m.throwing == 0 {
804 gp.m.throwing = 1
806 startpanic()
807 dopanic(0)
808 *(*int)(nil) = 0 // not reached
811 // runningPanicDefers is non-zero while running deferred functions for panic.
812 // runningPanicDefers is incremented and decremented atomically.
813 // This is used to try hard to get a panic stack trace out when exiting.
814 var runningPanicDefers uint32
816 // panicking is non-zero when crashing the program for an unrecovered panic.
817 // panicking is incremented and decremented atomically.
818 var panicking uint32
820 // paniclk is held while printing the panic information and stack trace,
821 // so that two concurrent panics don't overlap their output.
822 var paniclk mutex
824 func startpanic() {
825 _g_ := getg()
826 // Uncomment when mheap_ is in Go.
827 // if mheap_.cachealloc.size == 0 { // very early
828 // print("runtime: panic before malloc heap initialized\n")
829 // _g_.m.mallocing = 1 // tell rest of panic not to try to malloc
830 // } else
831 if _g_.m.mcache == nil { // can happen if called from signal handler or throw
832 _g_.m.mcache = allocmcache()
835 switch _g_.m.dying {
836 case 0:
837 _g_.m.dying = 1
838 _g_.writebuf = nil
839 atomic.Xadd(&panicking, 1)
840 lock(&paniclk)
841 if debug.schedtrace > 0 || debug.scheddetail > 0 {
842 schedtrace(true)
844 freezetheworld()
845 return
846 case 1:
847 // Something failed while panicking, probably the print of the
848 // argument to panic(). Just print a stack trace and exit.
849 _g_.m.dying = 2
850 print("panic during panic\n")
851 dopanic(0)
852 exit(3)
853 fallthrough
854 case 2:
855 // This is a genuine bug in the runtime, we couldn't even
856 // print the stack trace successfully.
857 _g_.m.dying = 3
858 print("stack trace unavailable\n")
859 exit(4)
860 fallthrough
861 default:
862 // Can't even print! Just exit.
863 exit(5)
867 var didothers bool
868 var deadlock mutex
870 func dopanic(unused int) {
871 gp := getg()
872 if gp.sig != 0 {
873 signame := signame(gp.sig)
874 if signame != "" {
875 print("[signal ", signame)
876 } else {
877 print("[signal ", hex(gp.sig))
879 print(" code=", hex(gp.sigcode0), " addr=", hex(gp.sigcode1), " pc=", hex(gp.sigpc), "]\n")
882 level, all, docrash := gotraceback()
883 _g_ := getg()
884 if level > 0 {
885 if gp != gp.m.curg {
886 all = true
888 if gp != gp.m.g0 {
889 print("\n")
890 goroutineheader(gp)
891 traceback(0)
892 } else if level >= 2 || _g_.m.throwing > 0 {
893 print("\nruntime stack:\n")
894 traceback(0)
896 if !didothers && all {
897 didothers = true
898 tracebackothers(gp)
901 unlock(&paniclk)
903 if atomic.Xadd(&panicking, -1) != 0 {
904 // Some other m is panicking too.
905 // Let it print what it needs to print.
906 // Wait forever without chewing up cpu.
907 // It will exit when it's done.
908 lock(&deadlock)
909 lock(&deadlock)
912 if docrash {
913 crash()
916 exit(2)
919 //go:nosplit
920 func canpanic(gp *g) bool {
921 // Note that g is m->gsignal, different from gp.
922 // Note also that g->m can change at preemption, so m can go stale
923 // if this function ever makes a function call.
924 _g_ := getg()
925 _m_ := _g_.m
927 // Is it okay for gp to panic instead of crashing the program?
928 // Yes, as long as it is running Go code, not runtime code,
929 // and not stuck in a system call.
930 if gp == nil || gp != _m_.curg {
931 return false
933 if _m_.locks-_m_.softfloat != 0 || _m_.mallocing != 0 || _m_.throwing != 0 || _m_.preemptoff != "" || _m_.dying != 0 {
934 return false
936 status := readgstatus(gp)
937 if status&^_Gscan != _Grunning || gp.syscallsp != 0 {
938 return false
940 return true