toolkit/xre/nsSigHandlers.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/xre/nsSigHandlers.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,393 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * This module is supposed to abstract signal handling away from the other
    1.11 + * platforms that do not support it.
    1.12 + */
    1.13 +
    1.14 +#include "nsSigHandlers.h"
    1.15 +
    1.16 +#ifdef XP_UNIX
    1.17 +
    1.18 +#include <signal.h>
    1.19 +#include <stdio.h>
    1.20 +#include <string.h>
    1.21 +#include "prthread.h"
    1.22 +#include "plstr.h"
    1.23 +#include "prenv.h"
    1.24 +#include "nsDebug.h"
    1.25 +#include "nsXULAppAPI.h"
    1.26 +
    1.27 +#if defined(LINUX)
    1.28 +#include <sys/time.h>
    1.29 +#include <sys/resource.h>
    1.30 +#include <unistd.h>
    1.31 +#include <stdlib.h> // atoi
    1.32 +#ifndef ANDROID // no Android impl
    1.33 +#  include <ucontext.h>
    1.34 +#endif
    1.35 +#endif
    1.36 +
    1.37 +#if defined(SOLARIS)
    1.38 +#include <sys/resource.h>
    1.39 +#include <ucontext.h>
    1.40 +#endif
    1.41 +
    1.42 +static char _progname[1024] = "huh?";
    1.43 +static unsigned int _gdb_sleep_duration = 300;
    1.44 +
    1.45 +// NB: keep me up to date with the same variable in
    1.46 +// ipc/chromium/chrome/common/ipc_channel_posix.cc
    1.47 +static const int kClientChannelFd = 3;
    1.48 +
    1.49 +#if defined(LINUX) && defined(DEBUG) && \
    1.50 +      (defined(__i386) || defined(__x86_64) || defined(PPC))
    1.51 +#define CRAWL_STACK_ON_SIGSEGV
    1.52 +#endif
    1.53 +
    1.54 +#if defined(CRAWL_STACK_ON_SIGSEGV)
    1.55 +
    1.56 +#include <unistd.h>
    1.57 +#include "nsISupportsUtils.h"
    1.58 +#include "nsStackWalk.h"
    1.59 +
    1.60 +extern "C" {
    1.61 +
    1.62 +static void PrintStackFrame(void *aPC, void *aSP, void *aClosure)
    1.63 +{
    1.64 +  char buf[1024];
    1.65 +  nsCodeAddressDetails details;
    1.66 +
    1.67 +  NS_DescribeCodeAddress(aPC, &details);
    1.68 +  NS_FormatCodeAddressDetails(aPC, &details, buf, sizeof(buf));
    1.69 +  fputs(buf, stdout);
    1.70 +}
    1.71 +
    1.72 +}
    1.73 +
    1.74 +void
    1.75 +ah_crap_handler(int signum)
    1.76 +{
    1.77 +  printf("\nProgram %s (pid = %d) received signal %d.\n",
    1.78 +         _progname,
    1.79 +         getpid(),
    1.80 +         signum);
    1.81 +
    1.82 +  printf("Stack:\n");
    1.83 +  NS_StackWalk(PrintStackFrame, /* skipFrames */ 2, /* maxFrames */ 0,
    1.84 +               nullptr, 0, nullptr);
    1.85 +
    1.86 +  printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
    1.87 +  printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
    1.88 +         _progname,
    1.89 +         getpid());
    1.90 +
    1.91 +  sleep(_gdb_sleep_duration);
    1.92 +
    1.93 +  printf("Done sleeping...\n");
    1.94 +
    1.95 +  _exit(signum);
    1.96 +}
    1.97 +
    1.98 +void
    1.99 +child_ah_crap_handler(int signum)
   1.100 +{
   1.101 +  if (!getenv("MOZ_DONT_UNBLOCK_PARENT_ON_CHILD_CRASH"))
   1.102 +    close(kClientChannelFd);
   1.103 +  ah_crap_handler(signum);
   1.104 +}
   1.105 +
   1.106 +#endif // CRAWL_STACK_ON_SIGSEGV
   1.107 +
   1.108 +#ifdef MOZ_WIDGET_GTK
   1.109 +// Need this include for version test below.
   1.110 +#include <glib.h>
   1.111 +#endif
   1.112 +
   1.113 +#if defined(MOZ_WIDGET_GTK) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
   1.114 +
   1.115 +static GLogFunc orig_log_func = nullptr;
   1.116 +
   1.117 +extern "C" {
   1.118 +static void
   1.119 +my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
   1.120 +                 const gchar *message, gpointer user_data);
   1.121 +}
   1.122 +
   1.123 +/* static */ void
   1.124 +my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
   1.125 +                 const gchar *message, gpointer user_data)
   1.126 +{
   1.127 +  if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)) {
   1.128 +    NS_DebugBreak(NS_DEBUG_ASSERTION, message, "glib assertion", __FILE__, __LINE__);
   1.129 +  } else if (log_level & (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)) {
   1.130 +    NS_DebugBreak(NS_DEBUG_WARNING, message, "glib warning", __FILE__, __LINE__);
   1.131 +  }
   1.132 +
   1.133 +  orig_log_func(log_domain, log_level, message, nullptr);
   1.134 +}
   1.135 +
   1.136 +#endif
   1.137 +
   1.138 +#ifdef SA_SIGINFO
   1.139 +static void fpehandler(int signum, siginfo_t *si, void *context)
   1.140 +{
   1.141 +  /* Integer divide by zero or integer overflow. */
   1.142 +  /* Note: FPE_INTOVF is ignored on Intel, PowerPC and SPARC systems. */
   1.143 +  if (si->si_code == FPE_INTDIV || si->si_code == FPE_INTOVF) {
   1.144 +    NS_DebugBreak(NS_DEBUG_ABORT, "Divide by zero", nullptr, __FILE__, __LINE__);
   1.145 +  }
   1.146 +
   1.147 +#ifdef XP_MACOSX
   1.148 +  ucontext_t *uc = (ucontext_t *)context;
   1.149 +
   1.150 +#if defined(__i386__) || defined(__amd64__)
   1.151 +  _STRUCT_FP_CONTROL *ctrl = &uc->uc_mcontext->__fs.__fpu_fcw;
   1.152 +  ctrl->__invalid = ctrl->__denorm = ctrl->__zdiv = ctrl->__ovrfl = ctrl->__undfl = ctrl->__precis = 1;
   1.153 +
   1.154 +  _STRUCT_FP_STATUS *status = &uc->uc_mcontext->__fs.__fpu_fsw;
   1.155 +  status->__invalid = status->__denorm = status->__zdiv = status->__ovrfl = status->__undfl =
   1.156 +    status->__precis = status->__stkflt = status->__errsumm = 0;
   1.157 +
   1.158 +  __uint32_t *mxcsr = &uc->uc_mcontext->__fs.__fpu_mxcsr;
   1.159 +  *mxcsr |= SSE_EXCEPTION_MASK; /* disable all SSE exceptions */
   1.160 +  *mxcsr &= ~SSE_STATUS_FLAGS; /* clear all pending SSE exceptions */
   1.161 +#endif
   1.162 +#endif
   1.163 +#if defined(LINUX) && !defined(ANDROID)
   1.164 +  ucontext_t *uc = (ucontext_t *)context;
   1.165 +
   1.166 +#if defined(__i386__)
   1.167 +  /*
   1.168 +   * It seems that we have no access to mxcsr on Linux. libc
   1.169 +   * seems to be translating cw/sw to mxcsr.
   1.170 +   */
   1.171 +  unsigned long int *cw = &uc->uc_mcontext.fpregs->cw;
   1.172 +  *cw |= FPU_EXCEPTION_MASK;
   1.173 +
   1.174 +  unsigned long int *sw = &uc->uc_mcontext.fpregs->sw;
   1.175 +  *sw &= ~FPU_STATUS_FLAGS;
   1.176 +#endif
   1.177 +#if defined(__amd64__)
   1.178 +  __uint16_t *cw = &uc->uc_mcontext.fpregs->cwd;
   1.179 +  *cw |= FPU_EXCEPTION_MASK;
   1.180 +
   1.181 +  __uint16_t *sw = &uc->uc_mcontext.fpregs->swd;
   1.182 +  *sw &= ~FPU_STATUS_FLAGS;
   1.183 +
   1.184 +  __uint32_t *mxcsr = &uc->uc_mcontext.fpregs->mxcsr;
   1.185 +  *mxcsr |= SSE_EXCEPTION_MASK; /* disable all SSE exceptions */
   1.186 +  *mxcsr &= ~SSE_STATUS_FLAGS; /* clear all pending SSE exceptions */
   1.187 +#endif
   1.188 +#endif
   1.189 +#ifdef SOLARIS
   1.190 +  ucontext_t *uc = (ucontext_t *)context;
   1.191 +
   1.192 +#if defined(__i386)
   1.193 +  uint32_t *cw = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state.state[0];
   1.194 +  *cw |= FPU_EXCEPTION_MASK;
   1.195 +
   1.196 +  uint32_t *sw = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state.state[1];
   1.197 +  *sw &= ~FPU_STATUS_FLAGS;
   1.198 +
   1.199 +  /* address of the instruction that caused the exception */
   1.200 +  uint32_t *ip = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state.state[3];
   1.201 +  uc->uc_mcontext.gregs[REG_PC] = *ip;
   1.202 +#endif
   1.203 +#if defined(__amd64__)
   1.204 +  uint16_t *cw = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state.cw;
   1.205 +  *cw |= FPU_EXCEPTION_MASK;
   1.206 +
   1.207 +  uint16_t *sw = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state.sw;
   1.208 +  *sw &= ~FPU_STATUS_FLAGS;
   1.209 +
   1.210 +  uint32_t *mxcsr = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state.mxcsr;
   1.211 +  *mxcsr |= SSE_EXCEPTION_MASK; /* disable all SSE exceptions */
   1.212 +  *mxcsr &= ~SSE_STATUS_FLAGS; /* clear all pending SSE exceptions */
   1.213 +#endif
   1.214 +#endif
   1.215 +}
   1.216 +#endif
   1.217 +
   1.218 +void InstallSignalHandlers(const char *ProgramName)
   1.219 +{
   1.220 +  PL_strncpy(_progname,ProgramName, (sizeof(_progname)-1) );
   1.221 +
   1.222 +  const char *gdbSleep = PR_GetEnv("MOZ_GDB_SLEEP");
   1.223 +  if (gdbSleep && *gdbSleep)
   1.224 +  {
   1.225 +    unsigned int s;
   1.226 +    if (1 == sscanf(gdbSleep, "%u", &s)) {
   1.227 +      _gdb_sleep_duration = s;
   1.228 +    }
   1.229 +  }
   1.230 +
   1.231 +#if defined(CRAWL_STACK_ON_SIGSEGV)
   1.232 +  if (!getenv("XRE_NO_WINDOWS_CRASH_DIALOG")) {
   1.233 +    void (*crap_handler)(int) =
   1.234 +      GeckoProcessType_Default != XRE_GetProcessType() ?
   1.235 +          child_ah_crap_handler :
   1.236 +          ah_crap_handler;
   1.237 +    signal(SIGSEGV, crap_handler);
   1.238 +    signal(SIGILL, crap_handler);
   1.239 +    signal(SIGABRT, crap_handler);
   1.240 +  }
   1.241 +#endif // CRAWL_STACK_ON_SIGSEGV
   1.242 +
   1.243 +#ifdef SA_SIGINFO
   1.244 +  /* Install a handler for floating point exceptions and disable them if they occur. */
   1.245 +  struct sigaction sa, osa;
   1.246 +  sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
   1.247 +  sa.sa_sigaction = fpehandler;
   1.248 +  sigemptyset(&sa.sa_mask);
   1.249 +  sigaction(SIGFPE, &sa, &osa);
   1.250 +#endif
   1.251 +
   1.252 +  if (XRE_GetProcessType() == GeckoProcessType_Content) {
   1.253 +    /*
   1.254 +     * If the user is debugging a Gecko parent process in gdb and hits ^C to
   1.255 +     * suspend, a SIGINT signal will be sent to the child. We ignore this signal
   1.256 +     * so the child isn't killed.
   1.257 +     */
   1.258 +    signal(SIGINT, SIG_IGN);
   1.259 +  }
   1.260 +
   1.261 +#if defined(DEBUG) && defined(LINUX)
   1.262 +  const char *memLimit = PR_GetEnv("MOZ_MEM_LIMIT");
   1.263 +  if (memLimit && *memLimit)
   1.264 +  {
   1.265 +    long m = atoi(memLimit);
   1.266 +    m *= (1024*1024);
   1.267 +    struct rlimit r;
   1.268 +    r.rlim_cur = m;
   1.269 +    r.rlim_max = m;
   1.270 +    setrlimit(RLIMIT_AS, &r);
   1.271 +  }
   1.272 +#endif
   1.273 +
   1.274 +#if defined(SOLARIS)
   1.275 +#define NOFILES 512
   1.276 +
   1.277 +    // Boost Solaris file descriptors
   1.278 +    {
   1.279 +	struct rlimit rl;
   1.280 +	
   1.281 +	if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
   1.282 +
   1.283 +	    if (rl.rlim_cur < NOFILES) {
   1.284 +		rl.rlim_cur = NOFILES;
   1.285 +
   1.286 +		if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
   1.287 +		    perror("setrlimit(RLIMIT_NOFILE)");
   1.288 +		    fprintf(stderr, "Cannot exceed hard limit for open files");
   1.289 +		}
   1.290 +#if defined(DEBUG)
   1.291 +	    	if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
   1.292 +		    printf("File descriptors set to %d\n", rl.rlim_cur);
   1.293 +#endif //DEBUG
   1.294 +	    }
   1.295 +    }
   1.296 +#endif //SOLARIS
   1.297 +
   1.298 +#if defined(MOZ_WIDGET_GTK) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
   1.299 +  const char *assertString = PR_GetEnv("XPCOM_DEBUG_BREAK");
   1.300 +  if (assertString &&
   1.301 +      (!strcmp(assertString, "suspend") ||
   1.302 +       !strcmp(assertString, "stack") ||
   1.303 +       !strcmp(assertString, "abort") ||
   1.304 +       !strcmp(assertString, "trap") ||
   1.305 +       !strcmp(assertString, "break"))) {
   1.306 +    // Override the default glib logging function so we get stacks for it too.
   1.307 +    orig_log_func = g_log_set_default_handler(my_glib_log_func, nullptr);
   1.308 +  }
   1.309 +#endif
   1.310 +}
   1.311 +
   1.312 +#elif XP_WIN
   1.313 +
   1.314 +#include <windows.h>
   1.315 +
   1.316 +#ifdef _M_IX86
   1.317 +/*
   1.318 + * WinNT.h prior to SDK7 does not expose the structure of the ExtendedRegisters for ia86.
   1.319 + * We known that MxCsr is at offset 0x18 and is a DWORD.
   1.320 + */
   1.321 +#define MXCSR(ctx) (*(DWORD *)(((BYTE *)(ctx)->ExtendedRegisters) + 0x18))
   1.322 +#endif
   1.323 +
   1.324 +#ifdef _M_X64
   1.325 +#define MXCSR(ctx) (ctx)->MxCsr
   1.326 +#endif
   1.327 +
   1.328 +#if defined(_M_IX86) || defined(_M_X64)
   1.329 +
   1.330 +#ifdef _M_X64
   1.331 +#define X87CW(ctx) (ctx)->FltSave.ControlWord
   1.332 +#define X87SW(ctx) (ctx)->FltSave.StatusWord
   1.333 +#else
   1.334 +#define X87CW(ctx) (ctx)->FloatSave.ControlWord
   1.335 +#define X87SW(ctx) (ctx)->FloatSave.StatusWord
   1.336 +#endif
   1.337 +
   1.338 +/*
   1.339 + * SSE traps raise these exception codes, which are defined in internal NT headers
   1.340 + * but not winbase.h
   1.341 + */
   1.342 +#define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4
   1.343 +#define STATUS_FLOAT_MULTIPLE_TRAPS  0xC00002B5
   1.344 +
   1.345 +static LPTOP_LEVEL_EXCEPTION_FILTER gFPEPreviousFilter;
   1.346 +
   1.347 +LONG __stdcall FpeHandler(PEXCEPTION_POINTERS pe)
   1.348 +{
   1.349 +  PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord;
   1.350 +  CONTEXT *c = (CONTEXT*)pe->ContextRecord;
   1.351 +
   1.352 +  switch (e->ExceptionCode) {
   1.353 +    case STATUS_FLOAT_DENORMAL_OPERAND:
   1.354 +    case STATUS_FLOAT_DIVIDE_BY_ZERO:
   1.355 +    case STATUS_FLOAT_INEXACT_RESULT:
   1.356 +    case STATUS_FLOAT_INVALID_OPERATION:
   1.357 +    case STATUS_FLOAT_OVERFLOW:
   1.358 +    case STATUS_FLOAT_STACK_CHECK:
   1.359 +    case STATUS_FLOAT_UNDERFLOW:
   1.360 +    case STATUS_FLOAT_MULTIPLE_FAULTS:
   1.361 +    case STATUS_FLOAT_MULTIPLE_TRAPS:
   1.362 +      X87CW(c) |= FPU_EXCEPTION_MASK; /* disable all FPU exceptions */
   1.363 +      X87SW(c) &= ~FPU_STATUS_FLAGS;  /* clear all pending FPU exceptions */
   1.364 +#ifdef _M_IX86
   1.365 +      if (c->ContextFlags & CONTEXT_EXTENDED_REGISTERS) {
   1.366 +#endif
   1.367 +        MXCSR(c) |= SSE_EXCEPTION_MASK; /* disable all SSE exceptions */
   1.368 +        MXCSR(c) &= ~SSE_STATUS_FLAGS;  /* clear all pending SSE exceptions */
   1.369 +#ifdef _M_IX86
   1.370 +      }
   1.371 +#endif
   1.372 +      return EXCEPTION_CONTINUE_EXECUTION;
   1.373 +  }
   1.374 +  LONG action = EXCEPTION_CONTINUE_SEARCH;
   1.375 +  if (gFPEPreviousFilter)
   1.376 +    action = gFPEPreviousFilter(pe);
   1.377 +
   1.378 +  return action;
   1.379 +}
   1.380 +
   1.381 +void InstallSignalHandlers(const char *ProgramName)
   1.382 +{
   1.383 +  gFPEPreviousFilter = SetUnhandledExceptionFilter(FpeHandler);
   1.384 +}
   1.385 +
   1.386 +#else
   1.387 +
   1.388 +void InstallSignalHandlers(const char *ProgramName)
   1.389 +{
   1.390 +}
   1.391 +
   1.392 +#endif
   1.393 +
   1.394 +#else
   1.395 +#error No signal handling implementation for this platform.
   1.396 +#endif

mercurial