1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * This module is supposed to abstract signal handling away from the other
8 * platforms that do not support it.
11 #include "nsSigHandlers.h"
22 #include "nsXULAppAPI.h"
26 #include <sys/resource.h>
28 #include <stdlib.h> // atoi
29 #ifndef ANDROID // no Android impl
30 # include <ucontext.h>
35 #include <sys/resource.h>
39 static char _progname
[1024] = "huh?";
40 static unsigned int _gdb_sleep_duration
= 300;
42 // NB: keep me up to date with the same variable in
43 // ipc/chromium/chrome/common/ipc_channel_posix.cc
44 static const int kClientChannelFd
= 3;
46 #if defined(LINUX) && defined(DEBUG) && \
47 (defined(__i386) || defined(__x86_64) || defined(PPC))
48 #define CRAWL_STACK_ON_SIGSEGV
51 #if defined(CRAWL_STACK_ON_SIGSEGV)
54 #include "nsISupportsUtils.h"
55 #include "nsStackWalk.h"
59 static void PrintStackFrame(void *aPC
, void *aSP
, void *aClosure
)
62 nsCodeAddressDetails details
;
64 NS_DescribeCodeAddress(aPC
, &details
);
65 NS_FormatCodeAddressDetails(aPC
, &details
, buf
, sizeof(buf
));
72 ah_crap_handler(int signum
)
74 printf("\nProgram %s (pid = %d) received signal %d.\n",
80 NS_StackWalk(PrintStackFrame
, /* skipFrames */ 2, /* maxFrames */ 0,
83 printf("Sleeping for %d seconds.\n",_gdb_sleep_duration
);
84 printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
88 sleep(_gdb_sleep_duration
);
90 printf("Done sleeping...\n");
96 child_ah_crap_handler(int signum
)
98 if (!getenv("MOZ_DONT_UNBLOCK_PARENT_ON_CHILD_CRASH"))
99 close(kClientChannelFd
);
100 ah_crap_handler(signum
);
103 #endif // CRAWL_STACK_ON_SIGSEGV
105 #ifdef MOZ_WIDGET_GTK
106 // Need this include for version test below.
110 #if defined(MOZ_WIDGET_GTK) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
112 static GLogFunc orig_log_func
= nullptr;
116 my_glib_log_func(const gchar
*log_domain
, GLogLevelFlags log_level
,
117 const gchar
*message
, gpointer user_data
);
121 my_glib_log_func(const gchar
*log_domain
, GLogLevelFlags log_level
,
122 const gchar
*message
, gpointer user_data
)
124 if (log_level
& (G_LOG_LEVEL_ERROR
| G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION
)) {
125 NS_DebugBreak(NS_DEBUG_ASSERTION
, message
, "glib assertion", __FILE__
, __LINE__
);
126 } else if (log_level
& (G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_WARNING
)) {
127 NS_DebugBreak(NS_DEBUG_WARNING
, message
, "glib warning", __FILE__
, __LINE__
);
130 orig_log_func(log_domain
, log_level
, message
, nullptr);
136 static void fpehandler(int signum
, siginfo_t
*si
, void *context
)
138 /* Integer divide by zero or integer overflow. */
139 /* Note: FPE_INTOVF is ignored on Intel, PowerPC and SPARC systems. */
140 if (si
->si_code
== FPE_INTDIV
|| si
->si_code
== FPE_INTOVF
) {
141 NS_DebugBreak(NS_DEBUG_ABORT
, "Divide by zero", nullptr, __FILE__
, __LINE__
);
145 ucontext_t
*uc
= (ucontext_t
*)context
;
147 #if defined(__i386__) || defined(__amd64__)
148 _STRUCT_FP_CONTROL
*ctrl
= &uc
->uc_mcontext
->__fs
.__fpu_fcw
;
149 ctrl
->__invalid
= ctrl
->__denorm
= ctrl
->__zdiv
= ctrl
->__ovrfl
= ctrl
->__undfl
= ctrl
->__precis
= 1;
151 _STRUCT_FP_STATUS
*status
= &uc
->uc_mcontext
->__fs
.__fpu_fsw
;
152 status
->__invalid
= status
->__denorm
= status
->__zdiv
= status
->__ovrfl
= status
->__undfl
=
153 status
->__precis
= status
->__stkflt
= status
->__errsumm
= 0;
155 __uint32_t
*mxcsr
= &uc
->uc_mcontext
->__fs
.__fpu_mxcsr
;
156 *mxcsr
|= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
157 *mxcsr
&= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
160 #if defined(LINUX) && !defined(ANDROID)
161 ucontext_t
*uc
= (ucontext_t
*)context
;
163 #if defined(__i386__)
165 * It seems that we have no access to mxcsr on Linux. libc
166 * seems to be translating cw/sw to mxcsr.
168 unsigned long int *cw
= &uc
->uc_mcontext
.fpregs
->cw
;
169 *cw
|= FPU_EXCEPTION_MASK
;
171 unsigned long int *sw
= &uc
->uc_mcontext
.fpregs
->sw
;
172 *sw
&= ~FPU_STATUS_FLAGS
;
174 #if defined(__amd64__)
175 __uint16_t
*cw
= &uc
->uc_mcontext
.fpregs
->cwd
;
176 *cw
|= FPU_EXCEPTION_MASK
;
178 __uint16_t
*sw
= &uc
->uc_mcontext
.fpregs
->swd
;
179 *sw
&= ~FPU_STATUS_FLAGS
;
181 __uint32_t
*mxcsr
= &uc
->uc_mcontext
.fpregs
->mxcsr
;
182 *mxcsr
|= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
183 *mxcsr
&= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
187 ucontext_t
*uc
= (ucontext_t
*)context
;
190 uint32_t *cw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.state
[0];
191 *cw
|= FPU_EXCEPTION_MASK
;
193 uint32_t *sw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.state
[1];
194 *sw
&= ~FPU_STATUS_FLAGS
;
196 /* address of the instruction that caused the exception */
197 uint32_t *ip
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.state
[3];
198 uc
->uc_mcontext
.gregs
[REG_PC
] = *ip
;
200 #if defined(__amd64__)
201 uint16_t *cw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.cw
;
202 *cw
|= FPU_EXCEPTION_MASK
;
204 uint16_t *sw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.sw
;
205 *sw
&= ~FPU_STATUS_FLAGS
;
207 uint32_t *mxcsr
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.mxcsr
;
208 *mxcsr
|= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
209 *mxcsr
&= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
215 void InstallSignalHandlers(const char *ProgramName
)
217 PL_strncpy(_progname
,ProgramName
, (sizeof(_progname
)-1) );
219 const char *gdbSleep
= PR_GetEnv("MOZ_GDB_SLEEP");
220 if (gdbSleep
&& *gdbSleep
)
223 if (1 == sscanf(gdbSleep
, "%u", &s
)) {
224 _gdb_sleep_duration
= s
;
228 #if defined(CRAWL_STACK_ON_SIGSEGV)
229 if (!getenv("XRE_NO_WINDOWS_CRASH_DIALOG")) {
230 void (*crap_handler
)(int) =
231 GeckoProcessType_Default
!= XRE_GetProcessType() ?
232 child_ah_crap_handler
:
234 signal(SIGSEGV
, crap_handler
);
235 signal(SIGILL
, crap_handler
);
236 signal(SIGABRT
, crap_handler
);
238 #endif // CRAWL_STACK_ON_SIGSEGV
241 /* Install a handler for floating point exceptions and disable them if they occur. */
242 struct sigaction sa
, osa
;
243 sa
.sa_flags
= SA_ONSTACK
| SA_RESTART
| SA_SIGINFO
;
244 sa
.sa_sigaction
= fpehandler
;
245 sigemptyset(&sa
.sa_mask
);
246 sigaction(SIGFPE
, &sa
, &osa
);
249 if (XRE_GetProcessType() == GeckoProcessType_Content
) {
251 * If the user is debugging a Gecko parent process in gdb and hits ^C to
252 * suspend, a SIGINT signal will be sent to the child. We ignore this signal
253 * so the child isn't killed.
255 signal(SIGINT
, SIG_IGN
);
258 #if defined(DEBUG) && defined(LINUX)
259 const char *memLimit
= PR_GetEnv("MOZ_MEM_LIMIT");
260 if (memLimit
&& *memLimit
)
262 long m
= atoi(memLimit
);
267 setrlimit(RLIMIT_AS
, &r
);
274 // Boost Solaris file descriptors
278 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
280 if (rl
.rlim_cur
< NOFILES
) {
281 rl
.rlim_cur
= NOFILES
;
283 if (setrlimit(RLIMIT_NOFILE
, &rl
) < 0) {
284 perror("setrlimit(RLIMIT_NOFILE)");
285 fprintf(stderr
, "Cannot exceed hard limit for open files");
288 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
289 printf("File descriptors set to %d\n", rl
.rlim_cur
);
295 #if defined(MOZ_WIDGET_GTK) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
296 const char *assertString
= PR_GetEnv("XPCOM_DEBUG_BREAK");
298 (!strcmp(assertString
, "suspend") ||
299 !strcmp(assertString
, "stack") ||
300 !strcmp(assertString
, "abort") ||
301 !strcmp(assertString
, "trap") ||
302 !strcmp(assertString
, "break"))) {
303 // Override the default glib logging function so we get stacks for it too.
304 orig_log_func
= g_log_set_default_handler(my_glib_log_func
, nullptr);
315 * WinNT.h prior to SDK7 does not expose the structure of the ExtendedRegisters for ia86.
316 * We known that MxCsr is at offset 0x18 and is a DWORD.
318 #define MXCSR(ctx) (*(DWORD *)(((BYTE *)(ctx)->ExtendedRegisters) + 0x18))
322 #define MXCSR(ctx) (ctx)->MxCsr
325 #if defined(_M_IX86) || defined(_M_X64)
328 #define X87CW(ctx) (ctx)->FltSave.ControlWord
329 #define X87SW(ctx) (ctx)->FltSave.StatusWord
331 #define X87CW(ctx) (ctx)->FloatSave.ControlWord
332 #define X87SW(ctx) (ctx)->FloatSave.StatusWord
336 * SSE traps raise these exception codes, which are defined in internal NT headers
339 #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4
340 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5
342 static LPTOP_LEVEL_EXCEPTION_FILTER gFPEPreviousFilter
;
344 LONG __stdcall
FpeHandler(PEXCEPTION_POINTERS pe
)
346 PEXCEPTION_RECORD e
= (PEXCEPTION_RECORD
)pe
->ExceptionRecord
;
347 CONTEXT
*c
= (CONTEXT
*)pe
->ContextRecord
;
349 switch (e
->ExceptionCode
) {
350 case STATUS_FLOAT_DENORMAL_OPERAND
:
351 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
352 case STATUS_FLOAT_INEXACT_RESULT
:
353 case STATUS_FLOAT_INVALID_OPERATION
:
354 case STATUS_FLOAT_OVERFLOW
:
355 case STATUS_FLOAT_STACK_CHECK
:
356 case STATUS_FLOAT_UNDERFLOW
:
357 case STATUS_FLOAT_MULTIPLE_FAULTS
:
358 case STATUS_FLOAT_MULTIPLE_TRAPS
:
359 X87CW(c
) |= FPU_EXCEPTION_MASK
; /* disable all FPU exceptions */
360 X87SW(c
) &= ~FPU_STATUS_FLAGS
; /* clear all pending FPU exceptions */
362 if (c
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) {
364 MXCSR(c
) |= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
365 MXCSR(c
) &= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
369 return EXCEPTION_CONTINUE_EXECUTION
;
371 LONG action
= EXCEPTION_CONTINUE_SEARCH
;
372 if (gFPEPreviousFilter
)
373 action
= gFPEPreviousFilter(pe
);
378 void InstallSignalHandlers(const char *ProgramName
)
380 gFPEPreviousFilter
= SetUnhandledExceptionFilter(FpeHandler
);
385 void InstallSignalHandlers(const char *ProgramName
)
391 #elif defined(XP_OS2)
392 /* OS/2's FPE handler is implemented in NSPR */
395 #error No signal handling implementation for this platform.