re PR middle-end/61558 (ICE: Segmentation fault)
[official-gcc.git] / libgo / runtime / signal_unix.c
blob66638dec56bbffbdef44471f8085803e79af10b0
1 // Copyright 2012 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.
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
7 #include <sys/time.h>
9 #include "runtime.h"
10 #include "defs.h"
11 #include "signal_unix.h"
13 extern SigTab runtime_sigtab[];
15 void
16 runtime_initsig(void)
18 int32 i;
19 SigTab *t;
21 // First call: basic setup.
22 for(i = 0; runtime_sigtab[i].sig != -1; i++) {
23 t = &runtime_sigtab[i];
24 if((t->flags == 0) || (t->flags & SigDefault))
25 continue;
27 // For some signals, we respect an inherited SIG_IGN handler
28 // rather than insist on installing our own default handler.
29 // Even these signals can be fetched using the os/signal package.
30 switch(t->sig) {
31 case SIGHUP:
32 case SIGINT:
33 if(runtime_getsig(i) == GO_SIG_IGN) {
34 t->flags = SigNotify | SigIgnored;
35 continue;
39 t->flags |= SigHandling;
40 runtime_setsig(i, runtime_sighandler, true);
44 void
45 runtime_sigenable(uint32 sig)
47 int32 i;
48 SigTab *t;
50 t = nil;
51 for(i = 0; runtime_sigtab[i].sig != -1; i++) {
52 if(runtime_sigtab[i].sig == (int32)sig) {
53 t = &runtime_sigtab[i];
54 break;
58 if(t == nil)
59 return;
61 if((t->flags & SigNotify) && !(t->flags & SigHandling)) {
62 t->flags |= SigHandling;
63 if(runtime_getsig(i) == GO_SIG_IGN)
64 t->flags |= SigIgnored;
65 runtime_setsig(i, runtime_sighandler, true);
69 void
70 runtime_sigdisable(uint32 sig)
72 int32 i;
73 SigTab *t;
75 t = nil;
76 for(i = 0; runtime_sigtab[i].sig != -1; i++) {
77 if(runtime_sigtab[i].sig == (int32)sig) {
78 t = &runtime_sigtab[i];
79 break;
83 if(t == nil)
84 return;
86 if((t->flags & SigNotify) && (t->flags & SigHandling)) {
87 t->flags &= ~SigHandling;
88 if(t->flags & SigIgnored)
89 runtime_setsig(i, GO_SIG_IGN, true);
90 else
91 runtime_setsig(i, GO_SIG_DFL, true);
95 void
96 runtime_resetcpuprofiler(int32 hz)
98 struct itimerval it;
100 runtime_memclr((byte*)&it, sizeof it);
101 if(hz == 0) {
102 runtime_setitimer(ITIMER_PROF, &it, nil);
103 } else {
104 it.it_interval.tv_sec = 0;
105 it.it_interval.tv_usec = 1000000 / hz;
106 it.it_value = it.it_interval;
107 runtime_setitimer(ITIMER_PROF, &it, nil);
109 runtime_m()->profilehz = hz;
112 void
113 os_sigpipe(void)
115 int32 i;
117 for(i = 0; runtime_sigtab[i].sig != -1; i++)
118 if(runtime_sigtab[i].sig == SIGPIPE)
119 break;
120 runtime_setsig(i, GO_SIG_DFL, false);
121 runtime_raise(SIGPIPE);
124 void
125 runtime_unblocksignals(void)
127 sigset_t sigset_none;
128 sigemptyset(&sigset_none);
129 pthread_sigmask(SIG_SETMASK, &sigset_none, nil);
132 void
133 runtime_crash(void)
135 int32 i;
137 #ifdef GOOS_darwin
138 // OS X core dumps are linear dumps of the mapped memory,
139 // from the first virtual byte to the last, with zeros in the gaps.
140 // Because of the way we arrange the address space on 64-bit systems,
141 // this means the OS X core file will be >128 GB and even on a zippy
142 // workstation can take OS X well over an hour to write (uninterruptible).
143 // Save users from making that mistake.
144 if(sizeof(void*) == 8)
145 return;
146 #endif
148 runtime_unblocksignals();
149 for(i = 0; runtime_sigtab[i].sig != -1; i++)
150 if(runtime_sigtab[i].sig == SIGABRT)
151 break;
152 runtime_setsig(i, GO_SIG_DFL, false);
153 runtime_raise(SIGABRT);
156 void
157 runtime_raise(int32 sig)
159 raise(sig);