1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Jerry.Kirk@Nexwarecorp.com
24 * Chris Seawood <cls@seawood.org>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * This module is supposed to abstract signal handling away from the other
42 * platforms that do not support it.
45 #include "nsSigHandlers.h"
57 # include "nsXULAppAPI.h"
62 #include <sys/resource.h>
64 #include <stdlib.h> // atoi
69 #include <sys/resource.h>
74 #include <be/app/Application.h>
77 #include "nsIServiceManager.h"
78 #include "nsIAppStartup.h"
79 #include "nsToolkitCompsCID.h"
82 static char _progname
[1024] = "huh?";
83 static unsigned int _gdb_sleep_duration
= 300;
86 // NB: keep me up to date with the same variable in
87 // ipc/chromium/chrome/common/ipc_channel_posix.cc
88 static const int kClientChannelFd
= 3;
91 #if defined(LINUX) && defined(DEBUG) && \
92 (defined(__i386) || defined(__x86_64) || defined(PPC))
93 #define CRAWL_STACK_ON_SIGSEGV
96 #if defined(CRAWL_STACK_ON_SIGSEGV)
99 #include "nsISupportsUtils.h"
100 #include "nsStackWalk.h"
104 static void PrintStackFrame(void *aPC
, void *aClosure
)
107 nsCodeAddressDetails details
;
109 NS_DescribeCodeAddress(aPC
, &details
);
110 NS_FormatCodeAddressDetails(aPC
, &details
, buf
, sizeof(buf
));
117 ah_crap_handler(int signum
)
119 printf("\nProgram %s (pid = %d) received signal %d.\n",
125 NS_StackWalk(PrintStackFrame
, 2, nsnull
);
127 printf("Sleeping for %d seconds.\n",_gdb_sleep_duration
);
128 printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
132 sleep(_gdb_sleep_duration
);
134 printf("Done sleeping...\n");
141 child_ah_crap_handler(int signum
)
143 if (!getenv("MOZ_DONT_UNBLOCK_PARENT_ON_CHILD_CRASH"))
144 close(kClientChannelFd
);
145 ah_crap_handler(signum
);
149 #endif // CRAWL_STACK_ON_SIGSEGV
152 void beos_signal_handler(int signum
) {
154 fprintf(stderr
, "beos_signal_handler: %d\n", signum
);
157 nsCOMPtr
<nsIAppStartup
> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID
, &rv
));
159 // Failed to get the appstartup service so shutdown the hard way
161 fprintf(stderr
, "beos_signal_handler: appShell->do_GetService() failed\n");
166 // Exit the appshell so that the app can shutdown normally
167 appStartup
->Quit(nsIAppStartup::eAttemptQuit
);
171 #ifdef MOZ_WIDGET_GTK2
172 // Need this include for version test below.
176 #if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
178 static GLogFunc orig_log_func
= NULL
;
182 my_glib_log_func(const gchar
*log_domain
, GLogLevelFlags log_level
,
183 const gchar
*message
, gpointer user_data
);
187 my_glib_log_func(const gchar
*log_domain
, GLogLevelFlags log_level
,
188 const gchar
*message
, gpointer user_data
)
190 if (log_level
& (G_LOG_LEVEL_ERROR
| G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION
)) {
191 NS_DebugBreak(NS_DEBUG_ASSERTION
, message
, "glib assertion", __FILE__
, __LINE__
);
192 } else if (log_level
& (G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_WARNING
)) {
193 NS_DebugBreak(NS_DEBUG_WARNING
, message
, "glib warning", __FILE__
, __LINE__
);
196 orig_log_func(log_domain
, log_level
, message
, NULL
);
202 static void fpehandler(int signum
, siginfo_t
*si
, void *context
)
204 /* Integer divide by zero or integer overflow. */
205 /* Note: FPE_INTOVF is ignored on Intel, PowerPC and SPARC systems. */
206 if (si
->si_code
== FPE_INTDIV
|| si
->si_code
== FPE_INTOVF
) {
207 NS_DebugBreak(NS_DEBUG_ABORT
, "Divide by zero", nsnull
, __FILE__
, __LINE__
);
211 ucontext_t
*uc
= (ucontext_t
*)context
;
213 #if defined(__i386__) || defined(__amd64__)
214 _STRUCT_FP_CONTROL
*ctrl
= &uc
->uc_mcontext
->__fs
.__fpu_fcw
;
215 ctrl
->__invalid
= ctrl
->__denorm
= ctrl
->__zdiv
= ctrl
->__ovrfl
= ctrl
->__undfl
= ctrl
->__precis
= 1;
217 _STRUCT_FP_STATUS
*status
= &uc
->uc_mcontext
->__fs
.__fpu_fsw
;
218 status
->__invalid
= status
->__denorm
= status
->__zdiv
= status
->__ovrfl
= status
->__undfl
=
219 status
->__precis
= status
->__stkflt
= status
->__errsumm
= 0;
221 __uint32_t
*mxcsr
= &uc
->uc_mcontext
->__fs
.__fpu_mxcsr
;
222 *mxcsr
|= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
223 *mxcsr
&= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
227 ucontext_t
*uc
= (ucontext_t
*)context
;
229 #if defined(__i386__)
231 * It seems that we have no access to mxcsr on Linux. libc
232 * seems to be translating cw/sw to mxcsr.
234 unsigned long int *cw
= &uc
->uc_mcontext
.fpregs
->cw
;
235 *cw
|= FPU_EXCEPTION_MASK
;
237 unsigned long int *sw
= &uc
->uc_mcontext
.fpregs
->sw
;
238 *sw
&= ~FPU_STATUS_FLAGS
;
240 #if defined(__amd64__)
241 __uint16_t
*cw
= &uc
->uc_mcontext
.fpregs
->cwd
;
242 *cw
|= FPU_EXCEPTION_MASK
;
244 __uint16_t
*sw
= &uc
->uc_mcontext
.fpregs
->swd
;
245 *sw
&= ~FPU_STATUS_FLAGS
;
247 __uint32_t
*mxcsr
= &uc
->uc_mcontext
.fpregs
->mxcsr
;
248 *mxcsr
|= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
249 *mxcsr
&= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
253 ucontext_t
*uc
= (ucontext_t
*)context
;
256 uint32_t *cw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.state
[0];
257 *cw
|= FPU_EXCEPTION_MASK
;
259 uint32_t *sw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.state
[1];
260 *sw
&= ~FPU_STATUS_FLAGS
;
262 /* address of the instruction that caused the exception */
263 uint32_t *ip
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.state
[3];
264 uc
->uc_mcontext
.gregs
[REG_PC
] = *ip
;
266 #if defined(__amd64__)
267 uint16_t *cw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.cw
;
268 *cw
|= FPU_EXCEPTION_MASK
;
270 uint16_t *sw
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.sw
;
271 *sw
&= ~FPU_STATUS_FLAGS
;
273 uint32_t *mxcsr
= &uc
->uc_mcontext
.fpregs
.fp_reg_set
.fpchip_state
.mxcsr
;
274 *mxcsr
|= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
275 *mxcsr
&= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
281 void InstallSignalHandlers(const char *ProgramName
)
283 PL_strncpy(_progname
,ProgramName
, (sizeof(_progname
)-1) );
285 const char *gdbSleep
= PR_GetEnv("MOZ_GDB_SLEEP");
286 if (gdbSleep
&& *gdbSleep
)
289 if (1 == sscanf(gdbSleep
, "%u", &s
)) {
290 _gdb_sleep_duration
= s
;
294 #if defined(CRAWL_STACK_ON_SIGSEGV)
295 if (!getenv("XRE_NO_WINDOWS_CRASH_DIALOG")) {
296 void (*crap_handler
)(int) =
298 GeckoProcessType_Default
!= XRE_GetProcessType() ?
299 child_ah_crap_handler
:
302 signal(SIGSEGV
, crap_handler
);
303 signal(SIGILL
, crap_handler
);
304 signal(SIGABRT
, crap_handler
);
306 #endif // CRAWL_STACK_ON_SIGSEGV
309 /* Install a handler for floating point exceptions and disable them if they occur. */
310 struct sigaction sa
, osa
;
311 sa
.sa_flags
= SA_ONSTACK
| SA_RESTART
| SA_SIGINFO
;
312 sa
.sa_sigaction
= fpehandler
;
313 sigemptyset(&sa
.sa_mask
);
314 sigaction(SIGFPE
, &sa
, &osa
);
317 #if defined(DEBUG) && defined(LINUX)
318 const char *memLimit
= PR_GetEnv("MOZ_MEM_LIMIT");
319 if (memLimit
&& *memLimit
)
321 long m
= atoi(memLimit
);
326 setrlimit(RLIMIT_AS
, &r
);
333 // Boost Solaris file descriptors
337 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
339 if (rl
.rlim_cur
< NOFILES
) {
340 rl
.rlim_cur
= NOFILES
;
342 if (setrlimit(RLIMIT_NOFILE
, &rl
) < 0) {
343 perror("setrlimit(RLIMIT_NOFILE)");
344 fprintf(stderr
, "Cannot exceed hard limit for open files");
347 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
348 printf("File descriptors set to %d\n", rl
.rlim_cur
);
355 signal(SIGTERM
, beos_signal_handler
);
358 #if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
359 const char *assertString
= PR_GetEnv("XPCOM_DEBUG_BREAK");
361 (!strcmp(assertString
, "suspend") ||
362 !strcmp(assertString
, "stack") ||
363 !strcmp(assertString
, "abort") ||
364 !strcmp(assertString
, "trap") ||
365 !strcmp(assertString
, "break"))) {
366 // Override the default glib logging function so we get stacks for it too.
367 orig_log_func
= g_log_set_default_handler(my_glib_log_func
, NULL
);
378 * WinNT.h prior to SDK7 does not expose the structure of the ExtendedRegisters for ia86.
379 * We known that MxCsr is at offset 0x18 and is a DWORD.
381 #define MXCSR(ctx) (*(DWORD *)(((BYTE *)(ctx)->ExtendedRegisters) + 0x18))
385 #define MXCSR(ctx) (ctx)->MxCsr
388 #if defined(_M_IX86) || defined(_M_X64)
391 #define X87CW(ctx) (ctx)->FltSave.ControlWord
392 #define X87SW(ctx) (ctx)->FltSave.StatusWord
394 #define X87CW(ctx) (ctx)->FloatSave.ControlWord
395 #define X87SW(ctx) (ctx)->FloatSave.StatusWord
399 * SSE traps raise these exception codes, which are defined in internal NT headers
402 #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4
403 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5
405 static LPTOP_LEVEL_EXCEPTION_FILTER gFPEPreviousFilter
;
407 LONG __stdcall
FpeHandler(PEXCEPTION_POINTERS pe
)
409 PEXCEPTION_RECORD e
= (PEXCEPTION_RECORD
)pe
->ExceptionRecord
;
410 CONTEXT
*c
= (CONTEXT
*)pe
->ContextRecord
;
412 switch (e
->ExceptionCode
) {
413 case STATUS_FLOAT_DENORMAL_OPERAND
:
414 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
415 case STATUS_FLOAT_INEXACT_RESULT
:
416 case STATUS_FLOAT_INVALID_OPERATION
:
417 case STATUS_FLOAT_OVERFLOW
:
418 case STATUS_FLOAT_STACK_CHECK
:
419 case STATUS_FLOAT_UNDERFLOW
:
420 case STATUS_FLOAT_MULTIPLE_FAULTS
:
421 case STATUS_FLOAT_MULTIPLE_TRAPS
:
422 X87CW(c
) |= FPU_EXCEPTION_MASK
; /* disable all FPU exceptions */
423 X87SW(c
) &= ~FPU_STATUS_FLAGS
; /* clear all pending FPU exceptions */
425 if (c
->ContextFlags
& CONTEXT_EXTENDED_REGISTERS
) {
427 MXCSR(c
) |= SSE_EXCEPTION_MASK
; /* disable all SSE exceptions */
428 MXCSR(c
) &= ~SSE_STATUS_FLAGS
; /* clear all pending SSE exceptions */
432 return EXCEPTION_CONTINUE_EXECUTION
;
434 LONG action
= EXCEPTION_CONTINUE_SEARCH
;
435 if (gFPEPreviousFilter
)
436 action
= gFPEPreviousFilter(pe
);
441 void InstallSignalHandlers(const char *ProgramName
)
443 gFPEPreviousFilter
= SetUnhandledExceptionFilter(FpeHandler
);
448 void InstallSignalHandlers(const char *ProgramName
)
454 #elif defined(XP_OS2)
455 /* OS/2's FPE handler is implemented in NSPR */
458 #error No signal handling implementation for this platform.