2011-11-14 23:26:45 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "array.h"
|
|
|
|
#include "go-panic.h"
|
|
|
|
#include "go-string.h"
|
|
|
|
|
|
|
|
uint32 runtime_panicking;
|
|
|
|
|
2012-05-17 07:30:25 +02:00
|
|
|
int32
|
|
|
|
runtime_gotraceback(void)
|
|
|
|
{
|
|
|
|
const byte *p;
|
|
|
|
|
|
|
|
p = runtime_getenv("GOTRACEBACK");
|
|
|
|
if(p == nil || p[0] == '\0')
|
|
|
|
return 1; // default is on
|
|
|
|
return runtime_atoi(p);
|
|
|
|
}
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
static Lock paniclk;
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_startpanic(void)
|
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
M *m;
|
|
|
|
|
|
|
|
m = runtime_m();
|
2011-11-14 23:26:45 +01:00
|
|
|
if(m->dying) {
|
|
|
|
runtime_printf("panic during panic\n");
|
|
|
|
runtime_exit(3);
|
|
|
|
}
|
|
|
|
m->dying = 1;
|
|
|
|
runtime_xadd(&runtime_panicking, 1);
|
|
|
|
runtime_lock(&paniclk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_dopanic(int32 unused __attribute__ ((unused)))
|
|
|
|
{
|
2012-05-17 07:30:25 +02:00
|
|
|
G* g;
|
2011-11-14 23:26:45 +01:00
|
|
|
static bool didothers;
|
|
|
|
|
2012-05-17 07:30:25 +02:00
|
|
|
g = runtime_g();
|
2011-11-14 23:26:45 +01:00
|
|
|
if(g->sig != 0)
|
2012-05-17 07:30:25 +02:00
|
|
|
runtime_printf("[signal %x code=%p addr=%p]\n",
|
|
|
|
g->sig, (void*)(g->sigcode0), (void*)(g->sigcode1));
|
2011-11-14 23:26:45 +01:00
|
|
|
|
|
|
|
if(runtime_gotraceback()){
|
2012-05-17 07:30:25 +02:00
|
|
|
if(g != runtime_m()->g0) {
|
|
|
|
runtime_printf("\n");
|
|
|
|
runtime_goroutineheader(g);
|
|
|
|
runtime_traceback();
|
|
|
|
runtime_goroutinetrailer(g);
|
|
|
|
}
|
2011-11-14 23:26:45 +01:00
|
|
|
if(!didothers) {
|
|
|
|
didothers = true;
|
|
|
|
runtime_tracebackothers(g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime_unlock(&paniclk);
|
|
|
|
if(runtime_xadd(&runtime_panicking, -1) != 0) {
|
|
|
|
// Some other m is panicking too.
|
|
|
|
// Let it print what it needs to print.
|
|
|
|
// Wait forever without chewing up cpu.
|
|
|
|
// It will exit when it's done.
|
|
|
|
static Lock deadlock;
|
|
|
|
runtime_lock(&deadlock);
|
|
|
|
runtime_lock(&deadlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime_exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_throw(const char *s)
|
|
|
|
{
|
|
|
|
runtime_startpanic();
|
|
|
|
runtime_printf("throw: %s\n", s);
|
|
|
|
runtime_dopanic(0);
|
|
|
|
*(int32*)0 = 0; // not reached
|
|
|
|
runtime_exit(1); // even more not reached
|
|
|
|
}
|
|
|
|
|
2011-11-30 01:21:52 +01:00
|
|
|
void
|
|
|
|
runtime_panicstring(const char *s)
|
|
|
|
{
|
|
|
|
Eface err;
|
2012-03-31 00:09:55 +02:00
|
|
|
|
2011-11-30 01:21:52 +01:00
|
|
|
if(runtime_m()->gcing) {
|
|
|
|
runtime_printf("panic: %s\n", s);
|
|
|
|
runtime_throw("panic during gc");
|
|
|
|
}
|
|
|
|
runtime_newErrorString(runtime_gostringnocopy((const byte*)s), &err);
|
|
|
|
runtime_panic(err);
|
|
|
|
}
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
static int32 argc;
|
|
|
|
static byte** argv;
|
|
|
|
|
2012-05-15 00:08:42 +02:00
|
|
|
extern Slice os_Args asm ("os.Args");
|
|
|
|
extern Slice syscall_Envs asm ("syscall.Envs");
|
2011-11-14 23:26:45 +01:00
|
|
|
|
2012-10-03 07:27:36 +02:00
|
|
|
void (*runtime_sysargs)(int32, uint8**);
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
void
|
|
|
|
runtime_args(int32 c, byte **v)
|
|
|
|
{
|
|
|
|
argc = c;
|
|
|
|
argv = v;
|
2012-10-03 07:27:36 +02:00
|
|
|
if(runtime_sysargs != nil)
|
|
|
|
runtime_sysargs(c, v);
|
2011-11-14 23:26:45 +01:00
|
|
|
}
|
|
|
|
|
2012-10-05 15:44:40 +02:00
|
|
|
byte*
|
|
|
|
runtime_progname()
|
|
|
|
{
|
|
|
|
return argc == 0 ? nil : argv[0];
|
|
|
|
}
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
void
|
|
|
|
runtime_goargs(void)
|
|
|
|
{
|
|
|
|
String *s;
|
|
|
|
int32 i;
|
2012-03-31 00:09:55 +02:00
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
// for windows implementation see "os" package
|
|
|
|
if(Windows)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = runtime_malloc(argc*sizeof s[0]);
|
|
|
|
for(i=0; i<argc; i++)
|
2011-11-30 01:21:52 +01:00
|
|
|
s[i] = runtime_gostringnocopy((const byte*)argv[i]);
|
2011-11-14 23:26:45 +01:00
|
|
|
os_Args.__values = (void*)s;
|
|
|
|
os_Args.__count = argc;
|
|
|
|
os_Args.__capacity = argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-25 21:56:26 +01:00
|
|
|
runtime_goenvs_unix(void)
|
2011-11-14 23:26:45 +01:00
|
|
|
{
|
|
|
|
String *s;
|
|
|
|
int32 i, n;
|
2012-03-31 00:09:55 +02:00
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
for(n=0; argv[argc+1+n] != 0; n++)
|
|
|
|
;
|
|
|
|
|
|
|
|
s = runtime_malloc(n*sizeof s[0]);
|
|
|
|
for(i=0; i<n; i++)
|
|
|
|
s[i] = runtime_gostringnocopy(argv[argc+1+i]);
|
2011-12-13 00:40:51 +01:00
|
|
|
syscall_Envs.__values = (void*)s;
|
|
|
|
syscall_Envs.__count = n;
|
|
|
|
syscall_Envs.__capacity = n;
|
2011-11-14 23:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const byte*
|
|
|
|
runtime_getenv(const char *s)
|
|
|
|
{
|
|
|
|
int32 i, j, len;
|
|
|
|
const byte *v, *bs;
|
|
|
|
String* envv;
|
|
|
|
int32 envc;
|
|
|
|
|
|
|
|
bs = (const byte*)s;
|
|
|
|
len = runtime_findnull(bs);
|
2011-12-13 00:40:51 +01:00
|
|
|
envv = (String*)syscall_Envs.__values;
|
|
|
|
envc = syscall_Envs.__count;
|
2011-11-14 23:26:45 +01:00
|
|
|
for(i=0; i<envc; i++){
|
|
|
|
if(envv[i].__length <= len)
|
|
|
|
continue;
|
|
|
|
v = (const byte*)envv[i].__data;
|
|
|
|
for(j=0; j<len; j++)
|
|
|
|
if(bs[j] != v[j])
|
|
|
|
goto nomatch;
|
|
|
|
if(v[len] != '=')
|
|
|
|
goto nomatch;
|
|
|
|
return v+len+1;
|
|
|
|
nomatch:;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
runtime_atoi(const byte *p)
|
|
|
|
{
|
|
|
|
int32 n;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while('0' <= *p && *p <= '9')
|
|
|
|
n = n*10 + *p++ - '0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32
|
|
|
|
runtime_fastrand1(void)
|
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
M *m;
|
2011-11-14 23:26:45 +01:00
|
|
|
uint32 x;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
m = runtime_m();
|
2011-11-14 23:26:45 +01:00
|
|
|
x = m->fastrand;
|
|
|
|
x += x;
|
|
|
|
if(x & 0x80000000L)
|
|
|
|
x ^= 0x88888eefUL;
|
|
|
|
m->fastrand = x;
|
|
|
|
return x;
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
static struct root_list runtime_roots =
|
2012-05-17 07:30:25 +02:00
|
|
|
{ nil,
|
2012-03-06 18:57:23 +01:00
|
|
|
{ { &syscall_Envs, sizeof syscall_Envs },
|
|
|
|
{ &os_Args, sizeof os_Args },
|
2012-05-17 07:30:25 +02:00
|
|
|
{ nil, 0 } },
|
2012-03-06 18:57:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_check(void)
|
|
|
|
{
|
|
|
|
__go_register_gc_roots(&runtime_roots);
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
int64
|
|
|
|
runtime_cputicks(void)
|
|
|
|
{
|
|
|
|
#if defined(__386__) || defined(__x86_64__)
|
|
|
|
uint32 low, high;
|
|
|
|
asm("rdtsc" : "=a" (low), "=d" (high));
|
|
|
|
return (int64)(((uint64)high << 32) | (uint64)low);
|
|
|
|
#else
|
|
|
|
// FIXME: implement for other processors.
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-17 07:30:25 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
runtime_showframe(const unsigned char *s)
|
|
|
|
{
|
|
|
|
static int32 traceback = -1;
|
|
|
|
|
|
|
|
if(traceback < 0)
|
|
|
|
traceback = runtime_gotraceback();
|
2012-10-03 07:27:36 +02:00
|
|
|
return traceback > 1 || (s != nil && __builtin_strchr((const char*)s, '.') != nil && __builtin_memcmp(s, "runtime.", 7) != 0);
|
2012-05-17 07:30:25 +02:00
|
|
|
}
|
2012-05-24 22:44:34 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
runtime_isInf(float64 f, int32 sign)
|
|
|
|
{
|
|
|
|
if(!__builtin_isinf(f))
|
|
|
|
return false;
|
|
|
|
if(sign == 0)
|
|
|
|
return true;
|
|
|
|
if(sign > 0)
|
|
|
|
return f > 0;
|
|
|
|
return f < 0;
|
|
|
|
}
|