Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / toolkit / xre / nsSigHandlers.cpp
blob024bca84289d2b6b14e088d0b6f80626edc4584b
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
13 * License.
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.
22 * Contributor(s):
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"
47 #ifdef XP_UNIX
49 #include <signal.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include "prthread.h"
53 #include "plstr.h"
54 #include "prenv.h"
55 #include "nsDebug.h"
56 #ifdef MOZ_IPC
57 # include "nsXULAppAPI.h"
58 #endif
60 #if defined(LINUX)
61 #include <sys/time.h>
62 #include <sys/resource.h>
63 #include <unistd.h>
64 #include <stdlib.h> // atoi
65 #include <ucontext.h>
66 #endif
68 #if defined(SOLARIS)
69 #include <sys/resource.h>
70 #include <ucontext.h>
71 #endif
73 #ifdef XP_BEOS
74 #include <be/app/Application.h>
75 #include <string.h>
76 #include "nsCOMPtr.h"
77 #include "nsIServiceManager.h"
78 #include "nsIAppStartup.h"
79 #include "nsToolkitCompsCID.h"
80 #endif
82 static char _progname[1024] = "huh?";
83 static unsigned int _gdb_sleep_duration = 300;
85 #ifdef MOZ_IPC
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;
89 #endif
91 #if defined(LINUX) && defined(DEBUG) && \
92 (defined(__i386) || defined(__x86_64) || defined(PPC))
93 #define CRAWL_STACK_ON_SIGSEGV
94 #endif
96 #if defined(CRAWL_STACK_ON_SIGSEGV)
98 #include <unistd.h>
99 #include "nsISupportsUtils.h"
100 #include "nsStackWalk.h"
102 extern "C" {
104 static void PrintStackFrame(void *aPC, void *aClosure)
106 char buf[1024];
107 nsCodeAddressDetails details;
109 NS_DescribeCodeAddress(aPC, &details);
110 NS_FormatCodeAddressDetails(aPC, &details, buf, sizeof(buf));
111 fputs(buf, stdout);
116 void
117 ah_crap_handler(int signum)
119 printf("\nProgram %s (pid = %d) received signal %d.\n",
120 _progname,
121 getpid(),
122 signum);
124 printf("Stack:\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",
129 _progname,
130 getpid());
132 sleep(_gdb_sleep_duration);
134 printf("Done sleeping...\n");
136 _exit(signum);
139 #ifdef MOZ_IPC
140 void
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);
147 #endif
149 #endif // CRAWL_STACK_ON_SIGSEGV
151 #ifdef XP_BEOS
152 void beos_signal_handler(int signum) {
153 #ifdef DEBUG
154 fprintf(stderr, "beos_signal_handler: %d\n", signum);
155 #endif
156 nsresult rv;
157 nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID, &rv));
158 if (NS_FAILED(rv)) {
159 // Failed to get the appstartup service so shutdown the hard way
160 #ifdef DEBUG
161 fprintf(stderr, "beos_signal_handler: appShell->do_GetService() failed\n");
162 #endif
163 exit(13);
166 // Exit the appshell so that the app can shutdown normally
167 appStartup->Quit(nsIAppStartup::eAttemptQuit);
169 #endif
171 #ifdef MOZ_WIDGET_GTK2
172 // Need this include for version test below.
173 #include <glib.h>
174 #endif
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;
180 extern "C" {
181 static void
182 my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
183 const gchar *message, gpointer user_data);
186 /* static */ void
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);
199 #endif
201 #ifdef SA_SIGINFO
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__);
210 #ifdef XP_MACOSX
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 */
224 #endif
225 #endif
226 #ifdef LINUX
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;
239 #endif
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 */
250 #endif
251 #endif
252 #ifdef SOLARIS
253 ucontext_t *uc = (ucontext_t *)context;
255 #if defined(__i386)
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;
265 #endif
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 */
276 #endif
277 #endif
279 #endif
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)
288 unsigned int s;
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) =
297 #ifdef MOZ_IPC
298 GeckoProcessType_Default != XRE_GetProcessType() ?
299 child_ah_crap_handler :
300 #endif
301 ah_crap_handler;
302 signal(SIGSEGV, crap_handler);
303 signal(SIGILL, crap_handler);
304 signal(SIGABRT, crap_handler);
306 #endif // CRAWL_STACK_ON_SIGSEGV
308 #ifdef SA_SIGINFO
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);
315 #endif
317 #if defined(DEBUG) && defined(LINUX)
318 const char *memLimit = PR_GetEnv("MOZ_MEM_LIMIT");
319 if (memLimit && *memLimit)
321 long m = atoi(memLimit);
322 m *= (1024*1024);
323 struct rlimit r;
324 r.rlim_cur = m;
325 r.rlim_max = m;
326 setrlimit(RLIMIT_AS, &r);
328 #endif
330 #if defined(SOLARIS)
331 #define NOFILES 512
333 // Boost Solaris file descriptors
335 struct rlimit rl;
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");
346 #if defined(DEBUG)
347 if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
348 printf("File descriptors set to %d\n", rl.rlim_cur);
349 #endif //DEBUG
352 #endif //SOLARIS
354 #ifdef XP_BEOS
355 signal(SIGTERM, beos_signal_handler);
356 #endif
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");
360 if (assertString &&
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);
369 #endif
372 #elif XP_WIN
374 #include <windows.h>
376 #ifdef _M_IX86
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))
382 #endif
384 #ifdef _M_X64
385 #define MXCSR(ctx) (ctx)->MxCsr
386 #endif
388 #if defined(_M_IX86) || defined(_M_X64)
390 #ifdef _M_X64
391 #define X87CW(ctx) (ctx)->FltSave.ControlWord
392 #define X87SW(ctx) (ctx)->FltSave.StatusWord
393 #else
394 #define X87CW(ctx) (ctx)->FloatSave.ControlWord
395 #define X87SW(ctx) (ctx)->FloatSave.StatusWord
396 #endif
399 * SSE traps raise these exception codes, which are defined in internal NT headers
400 * but not winbase.h
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 */
424 #ifdef _M_IX86
425 if (c->ContextFlags & CONTEXT_EXTENDED_REGISTERS) {
426 #endif
427 MXCSR(c) |= SSE_EXCEPTION_MASK; /* disable all SSE exceptions */
428 MXCSR(c) &= ~SSE_STATUS_FLAGS; /* clear all pending SSE exceptions */
429 #ifdef _M_IX86
431 #endif
432 return EXCEPTION_CONTINUE_EXECUTION;
434 LONG action = EXCEPTION_CONTINUE_SEARCH;
435 if (gFPEPreviousFilter)
436 action = gFPEPreviousFilter(pe);
438 return action;
441 void InstallSignalHandlers(const char *ProgramName)
443 gFPEPreviousFilter = SetUnhandledExceptionFilter(FpeHandler);
446 #else
448 void InstallSignalHandlers(const char *ProgramName)
452 #endif
454 #elif defined(XP_OS2)
455 /* OS/2's FPE handler is implemented in NSPR */
457 #else
458 #error No signal handling implementation for this platform.
459 #endif