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.
10 #include "go-string.h"
12 uint32 runtime_panicking;
15 runtime_gotraceback(void)
19 p = runtime_getenv("GOTRACEBACK");
20 if(p == nil || p[0] == '\0')
21 return 1; // default is on
22 return runtime_atoi(p);
28 runtime_startpanic(void)
34 runtime_printf("panic during panic\n");
38 runtime_xadd(&runtime_panicking, 1);
39 runtime_lock(&paniclk);
43 runtime_dopanic(int32 unused __attribute__ ((unused)))
46 static bool didothers;
50 runtime_printf("[signal %x code=%p addr=%p]\n",
51 g->sig, (void*)(g->sigcode0), (void*)(g->sigcode1));
53 if(runtime_gotraceback()){
54 if(g != runtime_m()->g0) {
56 runtime_goroutineheader(g);
58 runtime_goroutinetrailer(g);
62 runtime_tracebackothers(g);
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.
73 runtime_lock(&deadlock);
74 runtime_lock(&deadlock);
81 runtime_throw(const char *s)
84 runtime_printf("throw: %s\n", s);
86 *(int32*)0 = 0; // not reached
87 runtime_exit(1); // even more not reached
91 runtime_panicstring(const char *s)
95 if(runtime_m()->gcing) {
96 runtime_printf("panic: %s\n", s);
97 runtime_throw("panic during gc");
99 runtime_newErrorString(runtime_gostringnocopy((const byte*)s), &err);
106 extern Slice os_Args asm ("os.Args");
107 extern Slice syscall_Envs asm ("syscall.Envs");
109 void (*runtime_sysargs)(int32, uint8**);
112 runtime_args(int32 c, byte **v)
116 if(runtime_sysargs != nil)
117 runtime_sysargs(c, v);
126 // for windows implementation see "os" package
130 s = runtime_malloc(argc*sizeof s[0]);
131 for(i=0; i<argc; i++)
132 s[i] = runtime_gostringnocopy((const byte*)argv[i]);
133 os_Args.__values = (void*)s;
134 os_Args.__count = argc;
135 os_Args.__capacity = argc;
139 runtime_goenvs_unix(void)
144 for(n=0; argv[argc+1+n] != 0; n++)
147 s = runtime_malloc(n*sizeof s[0]);
149 s[i] = runtime_gostringnocopy(argv[argc+1+i]);
150 syscall_Envs.__values = (void*)s;
151 syscall_Envs.__count = n;
152 syscall_Envs.__capacity = n;
156 runtime_getenv(const char *s)
164 len = runtime_findnull(bs);
165 envv = (String*)syscall_Envs.__values;
166 envc = syscall_Envs.__count;
167 for(i=0; i<envc; i++){
168 if(envv[i].__length <= len)
170 v = (const byte*)envv[i].__data;
183 runtime_atoi(const byte *p)
188 while('0' <= *p && *p <= '9')
189 n = n*10 + *p++ - '0';
194 runtime_fastrand1(void)
208 static struct root_list runtime_roots =
210 { { &syscall_Envs, sizeof syscall_Envs },
211 { &os_Args, sizeof os_Args },
218 __go_register_gc_roots(&runtime_roots);
222 runtime_cputicks(void)
224 #if defined(__386__) || defined(__x86_64__)
226 asm("rdtsc" : "=a" (low), "=d" (high));
227 return (int64)(((uint64)high << 32) | (uint64)low);
229 // FIXME: implement for other processors.
235 runtime_showframe(const unsigned char *s)
237 static int32 traceback = -1;
240 traceback = runtime_gotraceback();
241 return traceback > 1 || (s != nil && __builtin_strchr((const char*)s, '.') != nil && __builtin_memcmp(s, "runtime.", 7) != 0);
245 runtime_isInf(float64 f, int32 sign)
247 if(!__builtin_isinf(f))