OSDN Git Service

Introduce G structure and thread-local global g.
[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       printf ("\t");
28     }
29   printf ("panic: ");
30   printany (p->__arg);
31   if (p->__was_recovered)
32     printf (" [recovered]");
33   putchar ('\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   struct __go_panic_stack *n;
43
44   n = (struct __go_panic_stack *) __go_alloc (sizeof (struct __go_panic_stack));
45   n->__arg = arg;
46   n->__next = g->panic;
47   g->panic = n;
48
49   /* Run all the defer functions.  */
50
51   while (1)
52     {
53       struct __go_defer_stack *d;
54       void (*pfn) (void *);
55
56       d = g->defer;
57       if (d == NULL)
58         break;
59
60       pfn = d->__pfn;
61       d->__pfn = NULL;
62
63       if (pfn != NULL)
64         {
65           (*pfn) (d->__arg);
66
67           if (n->__was_recovered)
68             {
69               /* Some defer function called recover.  That means that
70                  we should stop running this panic.  */
71
72               g->panic = n->__next;
73               __go_free (n);
74
75               /* Now unwind the stack by throwing an exception.  The
76                  compiler has arranged to create exception handlers in
77                  each function which uses a defer statement.  These
78                  exception handlers will check whether the entry on
79                  the top of the defer stack is from the current
80                  function.  If it is, we have unwound the stack far
81                  enough.  */
82               __go_unwind_stack ();
83
84               /* __go_unwind_stack should not return.  */
85               abort ();
86             }
87
88           /* Because we executed that defer function by a panic, and
89              it did not call recover, we know that we are not
90              returning from the calling function--we are panicing
91              through it.  */
92           *d->__frame = 0;
93         }
94
95       g->defer = d->__next;
96       __go_free (d);
97     }
98
99   /* The panic was not recovered.  */
100
101   __printpanics (g->panic);
102
103   /* FIXME: We should dump a call stack here.  */
104   abort ();
105 }
106
107 /* This is used by the runtime library.  */
108
109 void
110 __go_panic_msg (const char* msg)
111 {
112   size_t len;
113   unsigned char *sdata;
114   struct __go_string s;
115   struct __go_empty_interface arg;
116
117   len = __builtin_strlen (msg);
118   sdata = runtime_mallocgc (len, FlagNoPointers, 0, 0);
119   __builtin_memcpy (sdata, msg, len);
120   s.__data = sdata;
121   s.__length = len;
122   newErrorString(s, &arg);
123   __go_panic (arg);
124 }