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");
110 runtime_args(int32 c
, byte
**v
)
122 // for windows implementation see "os" package
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
;
135 runtime_goenvs_unix(void)
140 for(n
=0; argv
[argc
+1+n
] != 0; n
++)
143 s
= runtime_malloc(n
*sizeof s
[0]);
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
;
152 runtime_getenv(const char *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
)
166 v
= (const byte
*)envv
[i
].__data
;
179 runtime_atoi(const byte
*p
)
184 while('0' <= *p
&& *p
<= '9')
185 n
= n
*10 + *p
++ - '0';
190 runtime_fastrand1(void)
204 static struct root_list runtime_roots
=
206 { { &syscall_Envs
, sizeof syscall_Envs
},
207 { &os_Args
, sizeof os_Args
},
214 __go_register_gc_roots(&runtime_roots
);
218 runtime_cputicks(void)
220 #if defined(__386__) || defined(__x86_64__)
222 asm("rdtsc" : "=a" (low
), "=d" (high
));
223 return (int64
)(((uint64
)high
<< 32) | (uint64
)low
);
225 // FIXME: implement for other processors.
231 runtime_showframe(const unsigned char *s
)
233 static int32 traceback
= -1;
236 traceback
= runtime_gotraceback();
237 return traceback
> 1 || (__builtin_strchr((const char*)s
, '.') != nil
&& __builtin_memcmp(s
, "runtime.", 7) != 0);
241 runtime_isInf(float64 f
, int32 sign
)
243 if(!__builtin_isinf(f
))