1 // Copyright 2012 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.
10 // Code related to defer, panic and recover.
12 uint32 runtime_panicking
;
15 // Run all deferred functions for the current goroutine.
23 while((d
= g
->defer
) != nil
) {
37 runtime_startpanic(void)
42 if(runtime_mheap
.cachealloc
.size
== 0) { // very early
43 runtime_printf("runtime: panic before malloc heap initialized\n");
44 m
->mallocing
= 1; // tell rest of panic not to try to malloc
45 } else if(m
->mcache
== nil
) // can happen if called from signal handler or throw
46 m
->mcache
= runtime_allocmcache();
48 runtime_printf("panic during panic\n");
52 if(runtime_g() != nil
)
53 runtime_g()->writebuf
= nil
;
54 runtime_xadd(&runtime_panicking
, 1);
55 runtime_lock(&paniclk
);
56 if(runtime_debug
.schedtrace
> 0 || runtime_debug
.scheddetail
> 0)
57 runtime_schedtrace(true);
58 runtime_freezetheworld();
62 runtime_dopanic(int32 unused
__attribute__ ((unused
)))
65 static bool didothers
;
71 runtime_printf("[signal %x code=%p addr=%p]\n",
72 g
->sig
, (void*)g
->sigcode0
, (void*)g
->sigcode1
);
74 if((t
= runtime_gotraceback(&crash
)) > 0){
75 if(g
!= runtime_m()->g0
) {
77 runtime_goroutineheader(g
);
79 runtime_printcreatedby(g
);
80 } else if(t
>= 2 || runtime_m()->throwing
> 0) {
81 runtime_printf("\nruntime stack:\n");
86 runtime_tracebackothers(g
);
89 runtime_unlock(&paniclk
);
90 if(runtime_xadd(&runtime_panicking
, -1) != 0) {
91 // Some other m is panicking too.
92 // Let it print what it needs to print.
93 // Wait forever without chewing up cpu.
94 // It will exit when it's done.
96 runtime_lock(&deadlock
);
97 runtime_lock(&deadlock
);
107 runtime_throw(const char *s
)
112 if(mp
->throwing
== 0)
114 runtime_startpanic();
115 runtime_printf("fatal error: %s\n", s
);
117 *(int32
*)0 = 0; // not reached
118 runtime_exit(1); // even more not reached
122 runtime_panicstring(const char *s
)
126 if(runtime_m()->mallocing
) {
127 runtime_printf("panic: %s\n", s
);
128 runtime_throw("panic during malloc");
130 if(runtime_m()->gcing
) {
131 runtime_printf("panic: %s\n", s
);
132 runtime_throw("panic during gc");
134 runtime_newErrorCString(s
, &err
);
138 void runtime_Goexit (void) __asm__ (GOSYM_PREFIX
"runtime.Goexit");