2012-10-23 06:31:11 +02:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
2013-07-16 08:54:42 +02:00
|
|
|
#include "malloc.h"
|
2012-10-23 06:31:11 +02:00
|
|
|
#include "go-defer.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "go-panic.h"
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
// Code related to defer, panic and recover.
|
|
|
|
|
|
|
|
uint32 runtime_panicking;
|
|
|
|
static Lock paniclk;
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
// Allocate a Defer, usually using per-P pool.
|
|
|
|
// Each defer must be released with freedefer.
|
|
|
|
Defer*
|
|
|
|
runtime_newdefer()
|
|
|
|
{
|
|
|
|
Defer *d;
|
|
|
|
P *p;
|
|
|
|
|
|
|
|
d = nil;
|
|
|
|
p = runtime_m()->p;
|
|
|
|
d = p->deferpool;
|
|
|
|
if(d)
|
|
|
|
p->deferpool = d->__next;
|
|
|
|
if(d == nil) {
|
|
|
|
// deferpool is empty
|
|
|
|
d = runtime_malloc(sizeof(Defer));
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the given defer.
|
|
|
|
// The defer cannot be used after this call.
|
|
|
|
void
|
|
|
|
runtime_freedefer(Defer *d)
|
|
|
|
{
|
|
|
|
P *p;
|
|
|
|
|
|
|
|
if(d->__special)
|
|
|
|
return;
|
|
|
|
p = runtime_m()->p;
|
|
|
|
d->__next = p->deferpool;
|
|
|
|
p->deferpool = d;
|
|
|
|
// No need to wipe out pointers in argp/pc/fn/args,
|
|
|
|
// because we empty the pool before GC.
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// Run all deferred functions for the current goroutine.
|
2014-10-08 16:03:13 +02:00
|
|
|
// This is noinline for go_can_recover.
|
|
|
|
static void __go_rundefer (void) __attribute__ ((noinline));
|
2012-10-23 06:31:11 +02:00
|
|
|
static void
|
2014-10-08 16:03:13 +02:00
|
|
|
__go_rundefer(void)
|
2012-10-23 06:31:11 +02:00
|
|
|
{
|
|
|
|
G *g;
|
|
|
|
Defer *d;
|
|
|
|
|
|
|
|
g = runtime_g();
|
|
|
|
while((d = g->defer) != nil) {
|
|
|
|
void (*pfn)(void*);
|
|
|
|
|
|
|
|
g->defer = d->__next;
|
|
|
|
pfn = d->__pfn;
|
|
|
|
d->__pfn = nil;
|
|
|
|
if (pfn != nil)
|
|
|
|
(*pfn)(d->__arg);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_freedefer(d);
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_startpanic(void)
|
|
|
|
{
|
|
|
|
M *m;
|
|
|
|
|
|
|
|
m = runtime_m();
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_mheap.cachealloc.size == 0) { // very early
|
2013-07-16 08:54:42 +02:00
|
|
|
runtime_printf("runtime: panic before malloc heap initialized\n");
|
|
|
|
m->mallocing = 1; // tell rest of panic not to try to malloc
|
|
|
|
} else if(m->mcache == nil) // can happen if called from signal handler or throw
|
|
|
|
m->mcache = runtime_allocmcache();
|
2014-06-07 00:37:27 +02:00
|
|
|
switch(m->dying) {
|
|
|
|
case 0:
|
|
|
|
m->dying = 1;
|
|
|
|
if(runtime_g() != nil)
|
|
|
|
runtime_g()->writebuf = nil;
|
|
|
|
runtime_xadd(&runtime_panicking, 1);
|
|
|
|
runtime_lock(&paniclk);
|
|
|
|
if(runtime_debug.schedtrace > 0 || runtime_debug.scheddetail > 0)
|
|
|
|
runtime_schedtrace(true);
|
|
|
|
runtime_freezetheworld();
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
// Something failed while panicing, probably the print of the
|
|
|
|
// argument to panic(). Just print a stack trace and exit.
|
|
|
|
m->dying = 2;
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_printf("panic during panic\n");
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_dopanic(0);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_exit(3);
|
2014-06-07 00:37:27 +02:00
|
|
|
case 2:
|
|
|
|
// This is a genuine bug in the runtime, we couldn't even
|
|
|
|
// print the stack trace successfully.
|
|
|
|
m->dying = 3;
|
|
|
|
runtime_printf("stack trace unavailable\n");
|
|
|
|
runtime_exit(4);
|
|
|
|
default:
|
|
|
|
// Can't even print! Just exit.
|
|
|
|
runtime_exit(5);
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_dopanic(int32 unused __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
G *g;
|
|
|
|
static bool didothers;
|
2013-07-16 08:54:42 +02:00
|
|
|
bool crash;
|
2013-11-06 20:49:01 +01:00
|
|
|
int32 t;
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
g = runtime_g();
|
|
|
|
if(g->sig != 0)
|
|
|
|
runtime_printf("[signal %x code=%p addr=%p]\n",
|
|
|
|
g->sig, (void*)g->sigcode0, (void*)g->sigcode1);
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if((t = runtime_gotraceback(&crash)) > 0){
|
2012-10-23 06:31:11 +02:00
|
|
|
if(g != runtime_m()->g0) {
|
|
|
|
runtime_printf("\n");
|
|
|
|
runtime_goroutineheader(g);
|
|
|
|
runtime_traceback();
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_printcreatedby(g);
|
|
|
|
} else if(t >= 2 || runtime_m()->throwing > 0) {
|
|
|
|
runtime_printf("\nruntime stack:\n");
|
|
|
|
runtime_traceback();
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
if(!didothers) {
|
|
|
|
didothers = true;
|
|
|
|
runtime_tracebackothers(g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime_unlock(&paniclk);
|
|
|
|
if(runtime_xadd(&runtime_panicking, -1) != 0) {
|
|
|
|
// Some other m is panicking too.
|
|
|
|
// Let it print what it needs to print.
|
|
|
|
// Wait forever without chewing up cpu.
|
|
|
|
// It will exit when it's done.
|
|
|
|
static Lock deadlock;
|
|
|
|
runtime_lock(&deadlock);
|
|
|
|
runtime_lock(&deadlock);
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
if(crash)
|
|
|
|
runtime_crash();
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
runtime_exit(2);
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
bool
|
|
|
|
runtime_canpanic(G *gp)
|
|
|
|
{
|
|
|
|
M *m = runtime_m();
|
|
|
|
byte g;
|
|
|
|
|
|
|
|
USED(&g); // don't use global g, it points to gsignal
|
|
|
|
|
|
|
|
// Is it okay for gp to panic instead of crashing the program?
|
|
|
|
// Yes, as long as it is running Go code, not runtime code,
|
|
|
|
// and not stuck in a system call.
|
|
|
|
if(gp == nil || gp != m->curg)
|
|
|
|
return false;
|
|
|
|
if(m->locks-m->softfloat != 0 || m->mallocing != 0 || m->throwing != 0 || m->gcing != 0 || m->dying != 0)
|
|
|
|
return false;
|
|
|
|
if(gp->status != Grunning)
|
|
|
|
return false;
|
|
|
|
#ifdef GOOS_windows
|
|
|
|
if(m->libcallsp != 0)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
void
|
|
|
|
runtime_throw(const char *s)
|
|
|
|
{
|
2013-01-30 02:37:13 +01:00
|
|
|
M *mp;
|
|
|
|
|
|
|
|
mp = runtime_m();
|
|
|
|
if(mp->throwing == 0)
|
|
|
|
mp->throwing = 1;
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_startpanic();
|
2013-01-29 21:52:43 +01:00
|
|
|
runtime_printf("fatal error: %s\n", s);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_dopanic(0);
|
|
|
|
*(int32*)0 = 0; // not reached
|
|
|
|
runtime_exit(1); // even more not reached
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_panicstring(const char *s)
|
|
|
|
{
|
|
|
|
Eface err;
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_m()->mallocing) {
|
|
|
|
runtime_printf("panic: %s\n", s);
|
|
|
|
runtime_throw("panic during malloc");
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
if(runtime_m()->gcing) {
|
|
|
|
runtime_printf("panic: %s\n", s);
|
|
|
|
runtime_throw("panic during gc");
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
if(runtime_m()->locks) {
|
|
|
|
runtime_printf("panic: %s\n", s);
|
|
|
|
runtime_throw("panic holding locks");
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_newErrorCString(s, &err);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_panic(err);
|
|
|
|
}
|
|
|
|
|
2013-01-24 20:44:23 +01:00
|
|
|
void runtime_Goexit (void) __asm__ (GOSYM_PREFIX "runtime.Goexit");
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime_Goexit(void)
|
|
|
|
{
|
2014-10-08 16:03:13 +02:00
|
|
|
__go_rundefer();
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_goexit();
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime_panicdivide(void)
|
|
|
|
{
|
|
|
|
runtime_panicstring("integer divide by zero");
|
|
|
|
}
|