OSDN Git Service

runtime: Copy runtime_printf from other Go library.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-panic.c
1 /* go-panic.c -- support for the go panic function.
2
3    Copyright 2009 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "runtime.h"
11 #include "arch.h"
12 #include "malloc.h"
13 #include "go-alloc.h"
14 #include "go-defer.h"
15 #include "go-panic.h"
16 #include "go-string.h"
17 #include "interface.h"
18
19 /* Print the panic stack.  This is used when there is no recover.  */
20
21 static void
22 __printpanics (struct __go_panic_stack *p)
23 {
24   if (p->__next != NULL)
25     {
26       __printpanics (p->__next);
27       runtime_printf ("\t");
28     }
29   runtime_printf ("panic: ");
30   runtime_printany (p->__arg);
31   if (p->__was_recovered)
32     runtime_printf (" [recovered]");
33   runtime_printf ("\n");
34 }
35
36 /* This implements __go_panic which is used for the panic
37    function.  */
38
39 void
40 __go_panic (struct __go_empty_interface arg)
41 {
42   G *g;
43   struct __go_panic_stack *n;
44
45   g = runtime_g ();
46
47   n = (struct __go_panic_stack *) __go_alloc (sizeof (struct __go_panic_stack));
48   n->__arg = arg;
49   n->__next = g->panic;
50   g->panic = n;
51
52   /* Run all the defer functions.  */
53
54   while (1)
55     {
56       struct __go_defer_stack *d;
57       void (*pfn) (void *);
58
59       d = g->defer;
60       if (d == NULL)
61         break;
62
63       pfn = d->__pfn;
64       d->__pfn = NULL;
65
66       if (pfn != NULL)
67         {
68           (*pfn) (d->__arg);
69
70           if (n->__was_recovered)
71             {
72               /* Some defer function called recover.  That means that
73                  we should stop running this panic.  */
74
75               g->panic = n->__next;
76               __go_free (n);
77
78               /* Now unwind the stack by throwing an exception.  The
79                  compiler has arranged to create exception handlers in
80                  each function which uses a defer statement.  These
81                  exception handlers will check whether the entry on
82                  the top of the defer stack is from the current
83                  function.  If it is, we have unwound the stack far
84                  enough.  */
85               __go_unwind_stack ();
86
87               /* __go_unwind_stack should not return.  */
88               abort ();
89             }
90
91           /* Because we executed that defer function by a panic, and
92              it did not call recover, we know that we are not
93              returning from the calling function--we are panicing
94              through it.  */
95           *d->__frame = 0;
96         }
97
98       g->defer = d->__next;
99       __go_free (d);
100     }
101
102   /* The panic was not recovered.  */
103
104   runtime_startpanic ();
105   __printpanics (g->panic);
106   runtime_dopanic (0);
107 }