1 /* go-signal.c -- signal handling for Go.
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
11 #include "go-assert.h"
13 #include "go-signal.h"
21 /* What to do for a signal. */
27 /* Nonzero if the signal should be ignored. */
29 /* Nonzero if we should restart system calls. */
33 /* What to do for signals. */
35 static struct sigtab signals
[] =
95 /* The Go signal handler. */
106 runtime_sigprof (0, 0, nil
);
110 /* FIXME: Should check siginfo for more information when
117 msg
= "invalid memory address or nil pointer dereference";
123 msg
= "integer divide by zero or floating point error";
129 msg
= "invalid memory address or nil pointer dereference";
141 if (__sync_bool_compare_and_swap (&m
->mallocing
, 1, 1))
143 fprintf (stderr
, "caught signal while mallocing: %s\n", msg
);
147 /* The signal handler blocked signals; unblock them. */
148 i
= sigfillset (&clear
);
149 __go_assert (i
== 0);
150 i
= sigprocmask (SIG_UNBLOCK
, &clear
, NULL
);
151 __go_assert (i
== 0);
153 __go_panic_msg (msg
);
156 if (__go_sigsend (sig
))
158 for (i
= 0; signals
[i
].sig
!= -1; ++i
)
160 if (signals
[i
].sig
== sig
)
164 if (signals
[i
].ignore
)
167 memset (&sa
, 0, sizeof sa
);
169 sa
.sa_handler
= SIG_DFL
;
171 i
= sigemptyset (&sa
.sa_mask
);
172 __go_assert (i
== 0);
174 if (sigaction (sig
, &sa
, NULL
) != 0)
184 /* Initialize signal handling for Go. This is called when the program
195 memset (&sa
, 0, sizeof sa
);
197 sa
.sa_handler
= sighandler
;
199 i
= sigfillset (&sa
.sa_mask
);
200 __go_assert (i
== 0);
202 for (i
= 0; signals
[i
].sig
!= -1; ++i
)
204 sa
.sa_flags
= signals
[i
].restart
? SA_RESTART
: 0;
205 if (sigaction (signals
[i
].sig
, &sa
, NULL
) != 0)
211 runtime_resetcpuprofiler(int32 hz
)
217 memset (&it
, 0, sizeof it
);
219 memset (&sa
, 0, sizeof sa
);
220 i
= sigfillset (&sa
.sa_mask
);
221 __go_assert (i
== 0);
225 i
= setitimer (ITIMER_PROF
, &it
, NULL
);
226 __go_assert (i
== 0);
228 sa
.sa_handler
= SIG_IGN
;
229 i
= sigaction (SIGPROF
, &sa
, NULL
);
230 __go_assert (i
== 0);
234 sa
.sa_handler
= sighandler
;
235 sa
.sa_flags
= SA_RESTART
;
236 i
= sigaction (SIGPROF
, &sa
, NULL
);
237 __go_assert (i
== 0);
239 it
.it_interval
.tv_sec
= 0;
240 it
.it_interval
.tv_usec
= 1000000 / hz
;
241 it
.it_value
= it
.it_interval
;
242 i
= setitimer (ITIMER_PROF
, &it
, NULL
);
243 __go_assert (i
== 0);
249 /* Used by the os package to raise SIGPIPE. */
251 void os_sigpipe (void) __asm__ ("libgo_os.os.sigpipe");
259 memset (&sa
, 0, sizeof sa
);
261 sa
.sa_handler
= SIG_DFL
;
263 i
= sigemptyset (&sa
.sa_mask
);
264 __go_assert (i
== 0);
266 if (sigaction (SIGPIPE
, &sa
, NULL
) != 0)