runtime: Copy runtime_printf from other Go library.
[gcc.git] / libgo / runtime / runtime.c
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include <unistd.h>
6
7 #include "runtime.h"
8 #include "array.h"
9 #include "go-panic.h"
10 #include "go-string.h"
11
12 uint32 runtime_panicking;
13
14 int32
15 runtime_gotraceback(void)
16 {
17 const byte *p;
18
19 p = runtime_getenv("GOTRACEBACK");
20 if(p == nil || p[0] == '\0')
21 return 1; // default is on
22 return runtime_atoi(p);
23 }
24
25 static Lock paniclk;
26
27 void
28 runtime_startpanic(void)
29 {
30 M *m;
31
32 m = runtime_m();
33 if(m->dying) {
34 runtime_printf("panic during panic\n");
35 runtime_exit(3);
36 }
37 m->dying = 1;
38 runtime_xadd(&runtime_panicking, 1);
39 runtime_lock(&paniclk);
40 }
41
42 void
43 runtime_dopanic(int32 unused __attribute__ ((unused)))
44 {
45 G* g;
46 static bool didothers;
47
48 g = runtime_g();
49 if(g->sig != 0)
50 runtime_printf("[signal %x code=%p addr=%p]\n",
51 g->sig, (void*)(g->sigcode0), (void*)(g->sigcode1));
52
53 if(runtime_gotraceback()){
54 if(g != runtime_m()->g0) {
55 runtime_printf("\n");
56 runtime_goroutineheader(g);
57 runtime_traceback();
58 runtime_goroutinetrailer(g);
59 }
60 if(!didothers) {
61 didothers = true;
62 runtime_tracebackothers(g);
63 }
64 }
65
66 runtime_unlock(&paniclk);
67 if(runtime_xadd(&runtime_panicking, -1) != 0) {
68 // Some other m is panicking too.
69 // Let it print what it needs to print.
70 // Wait forever without chewing up cpu.
71 // It will exit when it's done.
72 static Lock deadlock;
73 runtime_lock(&deadlock);
74 runtime_lock(&deadlock);
75 }
76
77 runtime_exit(2);
78 }
79
80 void
81 runtime_throw(const char *s)
82 {
83 runtime_startpanic();
84 runtime_printf("throw: %s\n", s);
85 runtime_dopanic(0);
86 *(int32*)0 = 0; // not reached
87 runtime_exit(1); // even more not reached
88 }
89
90 void
91 runtime_panicstring(const char *s)
92 {
93 Eface err;
94
95 if(runtime_m()->gcing) {
96 runtime_printf("panic: %s\n", s);
97 runtime_throw("panic during gc");
98 }
99 runtime_newErrorString(runtime_gostringnocopy((const byte*)s), &err);
100 runtime_panic(err);
101 }
102
103 static int32 argc;
104 static byte** argv;
105
106 extern Slice os_Args asm ("os.Args");
107 extern Slice syscall_Envs asm ("syscall.Envs");
108
109 void
110 runtime_args(int32 c, byte **v)
111 {
112 argc = c;
113 argv = v;
114 }
115
116 void
117 runtime_goargs(void)
118 {
119 String *s;
120 int32 i;
121
122 // for windows implementation see "os" package
123 if(Windows)
124 return;
125
126 s = runtime_malloc(argc*sizeof s[0]);
127 for(i=0; i<argc; i++)
128 s[i] = runtime_gostringnocopy((const byte*)argv[i]);
129 os_Args.__values = (void*)s;
130 os_Args.__count = argc;
131 os_Args.__capacity = argc;
132 }
133
134 void
135 runtime_goenvs_unix(void)
136 {
137 String *s;
138 int32 i, n;
139
140 for(n=0; argv[argc+1+n] != 0; n++)
141 ;
142
143 s = runtime_malloc(n*sizeof s[0]);
144 for(i=0; i<n; i++)
145 s[i] = runtime_gostringnocopy(argv[argc+1+i]);
146 syscall_Envs.__values = (void*)s;
147 syscall_Envs.__count = n;
148 syscall_Envs.__capacity = n;
149 }
150
151 const byte*
152 runtime_getenv(const char *s)
153 {
154 int32 i, j, len;
155 const byte *v, *bs;
156 String* envv;
157 int32 envc;
158
159 bs = (const byte*)s;
160 len = runtime_findnull(bs);
161 envv = (String*)syscall_Envs.__values;
162 envc = syscall_Envs.__count;
163 for(i=0; i<envc; i++){
164 if(envv[i].__length <= len)
165 continue;
166 v = (const byte*)envv[i].__data;
167 for(j=0; j<len; j++)
168 if(bs[j] != v[j])
169 goto nomatch;
170 if(v[len] != '=')
171 goto nomatch;
172 return v+len+1;
173 nomatch:;
174 }
175 return nil;
176 }
177
178 int32
179 runtime_atoi(const byte *p)
180 {
181 int32 n;
182
183 n = 0;
184 while('0' <= *p && *p <= '9')
185 n = n*10 + *p++ - '0';
186 return n;
187 }
188
189 uint32
190 runtime_fastrand1(void)
191 {
192 M *m;
193 uint32 x;
194
195 m = runtime_m();
196 x = m->fastrand;
197 x += x;
198 if(x & 0x80000000L)
199 x ^= 0x88888eefUL;
200 m->fastrand = x;
201 return x;
202 }
203
204 static struct root_list runtime_roots =
205 { nil,
206 { { &syscall_Envs, sizeof syscall_Envs },
207 { &os_Args, sizeof os_Args },
208 { nil, 0 } },
209 };
210
211 void
212 runtime_check(void)
213 {
214 __go_register_gc_roots(&runtime_roots);
215 }
216
217 int64
218 runtime_cputicks(void)
219 {
220 #if defined(__386__) || defined(__x86_64__)
221 uint32 low, high;
222 asm("rdtsc" : "=a" (low), "=d" (high));
223 return (int64)(((uint64)high << 32) | (uint64)low);
224 #else
225 // FIXME: implement for other processors.
226 return 0;
227 #endif
228 }
229
230 bool
231 runtime_showframe(const unsigned char *s)
232 {
233 static int32 traceback = -1;
234
235 if(traceback < 0)
236 traceback = runtime_gotraceback();
237 return traceback > 1 || (__builtin_strchr((const char*)s, '.') != nil && __builtin_memcmp(s, "runtime.", 7) != 0);
238 }
239
240 bool
241 runtime_isInf(float64 f, int32 sign)
242 {
243 if(!__builtin_isinf(f))
244 return false;
245 if(sign == 0)
246 return true;
247 if(sign > 0)
248 return f > 0;
249 return f < 0;
250 }