OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[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 "malloc.h"
12 #include "go-alloc.h"
13 #include "go-defer.h"
14 #include "go-panic.h"
15 #include "go-string.h"
16 #include "interface.h"
17
18 /* Print the panic stack.  This is used when there is no recover.  */
19
20 static void
21 __printpanics (struct __go_panic_stack *p)
22 {
23   if (p->__next != NULL)
24     {
25       __printpanics (p->__next);
26       printf ("\t");
27     }
28   printf ("panic: ");
29   printany (p->__arg);
30   if (p->__was_recovered)
31     printf (" [recovered]");
32   putchar ('\n');
33 }
34
35 /* This implements __go_panic which is used for the panic
36    function.  */
37
38 void
39 __go_panic (struct __go_empty_interface arg)
40 {
41   struct __go_panic_stack *n;
42
43   if (__go_panic_defer == NULL)
44     __go_panic_defer = ((struct __go_panic_defer_struct *)
45                         __go_alloc (sizeof (struct __go_panic_defer_struct)));
46
47   n = (struct __go_panic_stack *) __go_alloc (sizeof (struct __go_panic_stack));
48   n->__arg = arg;
49   n->__next = __go_panic_defer->__panic;
50   __go_panic_defer->__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 = __go_panic_defer->__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               __go_panic_defer->__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
92       __go_panic_defer->__defer = d->__next;
93       __go_free (d);
94     }
95
96   /* The panic was not recovered.  */
97
98   __printpanics (__go_panic_defer->__panic);
99
100   /* FIXME: We should dump a call stack here.  */
101   abort ();
102 }
103
104 /* This is used by the runtime library.  */
105
106 void
107 __go_panic_msg (const char* msg)
108 {
109   size_t len;
110   unsigned char *sdata;
111   struct __go_string s;
112   struct __go_empty_interface arg;
113
114   len = __builtin_strlen (msg);
115   sdata = runtime_mallocgc (len, FlagNoPointers, 0, 0);
116   __builtin_memcpy (sdata, msg, len);
117   s.__data = sdata;
118   s.__length = len;
119   newErrorString(s, &arg);
120   __go_panic (arg);
121 }