2010-12-03 05:34:57 +01:00
|
|
|
/* go-panic.c -- support for the go panic function.
|
|
|
|
|
|
|
|
Copyright 2009 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "runtime.h"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
#include "go-alloc.h"
|
|
|
|
#include "go-defer.h"
|
|
|
|
#include "go-panic.h"
|
|
|
|
#include "interface.h"
|
|
|
|
|
|
|
|
/* Print the panic stack. This is used when there is no recover. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
__printpanics (struct __go_panic_stack *p)
|
|
|
|
{
|
|
|
|
if (p->__next != NULL)
|
|
|
|
{
|
|
|
|
__printpanics (p->__next);
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf ("\t");
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf ("panic: ");
|
2012-03-02 17:38:43 +01:00
|
|
|
runtime_printany (p->__arg);
|
2010-12-03 05:34:57 +01:00
|
|
|
if (p->__was_recovered)
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf (" [recovered]");
|
|
|
|
runtime_printf ("\n");
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This implements __go_panic which is used for the panic
|
|
|
|
function. */
|
|
|
|
|
|
|
|
void
|
|
|
|
__go_panic (struct __go_empty_interface arg)
|
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
G *g;
|
2010-12-03 05:34:57 +01:00
|
|
|
struct __go_panic_stack *n;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
g = runtime_g ();
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
n = (struct __go_panic_stack *) __go_alloc (sizeof (struct __go_panic_stack));
|
|
|
|
n->__arg = arg;
|
2011-11-11 22:02:48 +01:00
|
|
|
n->__next = g->panic;
|
|
|
|
g->panic = n;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
/* Run all the defer functions. */
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
struct __go_defer_stack *d;
|
|
|
|
void (*pfn) (void *);
|
|
|
|
|
2011-11-11 22:02:48 +01:00
|
|
|
d = g->defer;
|
2010-12-03 05:34:57 +01:00
|
|
|
if (d == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pfn = d->__pfn;
|
|
|
|
d->__pfn = NULL;
|
|
|
|
|
|
|
|
if (pfn != NULL)
|
|
|
|
{
|
|
|
|
(*pfn) (d->__arg);
|
|
|
|
|
|
|
|
if (n->__was_recovered)
|
|
|
|
{
|
|
|
|
/* Some defer function called recover. That means that
|
|
|
|
we should stop running this panic. */
|
|
|
|
|
2011-11-11 22:02:48 +01:00
|
|
|
g->panic = n->__next;
|
2010-12-03 05:34:57 +01:00
|
|
|
__go_free (n);
|
|
|
|
|
|
|
|
/* Now unwind the stack by throwing an exception. The
|
|
|
|
compiler has arranged to create exception handlers in
|
|
|
|
each function which uses a defer statement. These
|
|
|
|
exception handlers will check whether the entry on
|
|
|
|
the top of the defer stack is from the current
|
|
|
|
function. If it is, we have unwound the stack far
|
|
|
|
enough. */
|
|
|
|
__go_unwind_stack ();
|
|
|
|
|
|
|
|
/* __go_unwind_stack should not return. */
|
|
|
|
abort ();
|
|
|
|
}
|
2011-09-16 07:47:20 +02:00
|
|
|
|
|
|
|
/* Because we executed that defer function by a panic, and
|
|
|
|
it did not call recover, we know that we are not
|
|
|
|
returning from the calling function--we are panicing
|
|
|
|
through it. */
|
|
|
|
*d->__frame = 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-11-11 22:02:48 +01:00
|
|
|
g->defer = d->__next;
|
2013-07-23 22:26:09 +02:00
|
|
|
|
|
|
|
/* This may be called by a cgo callback routine to defer the
|
|
|
|
call to syscall.CgocallBackDone, in which case we will not
|
|
|
|
have a memory context. Don't try to free anything in that
|
|
|
|
case--the GC will release it later. */
|
2014-06-07 00:37:27 +02:00
|
|
|
if (runtime_m () != NULL)
|
|
|
|
runtime_freedefer (d);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The panic was not recovered. */
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
runtime_startpanic ();
|
2011-11-11 22:02:48 +01:00
|
|
|
__printpanics (g->panic);
|
2011-11-14 23:26:45 +01:00
|
|
|
runtime_dopanic (0);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|