2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 20:52:41 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-11-19 04:55:19 +03:00
|
|
|
*
|
2004-04-17 20:52:41 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-11-19 04:55:19 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-11-19 04:55:19 +03:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
2004-04-17 20:52:41 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-19 04:55:19 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
2004-04-17 20:52:41 +04:00
|
|
|
* Jerry.Kirk@Nexwarecorp.com
|
|
|
|
* Chris Seawood <cls@seawood.org>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 20:52:41 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-17 20:52:41 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-17 20:52:41 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-11-19 04:55:19 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This module is supposed to abstract signal handling away from the other
|
|
|
|
* platforms that do not support it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <signal.h>
|
1999-12-07 16:12:26 +03:00
|
|
|
#include <stdio.h>
|
2007-05-23 20:13:22 +04:00
|
|
|
#include <string.h>
|
1999-11-19 04:55:19 +03:00
|
|
|
#include "prthread.h"
|
1999-11-20 13:05:44 +03:00
|
|
|
#include "plstr.h"
|
2000-05-17 08:06:08 +04:00
|
|
|
#include "prenv.h"
|
2007-05-23 20:13:22 +04:00
|
|
|
#include "nsDebug.h"
|
2000-05-17 08:06:08 +04:00
|
|
|
|
|
|
|
#if defined(LINUX)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <unistd.h>
|
2000-08-01 10:09:14 +04:00
|
|
|
#include <stdlib.h> // atoi
|
2000-05-17 08:06:08 +04:00
|
|
|
#endif
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2000-08-22 07:42:32 +04:00
|
|
|
#if defined(SOLARIS)
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
1999-11-20 13:53:34 +03:00
|
|
|
#ifdef XP_BEOS
|
2001-07-28 02:16:33 +04:00
|
|
|
#include <be/app/Application.h>
|
1999-11-20 13:53:34 +03:00
|
|
|
#include <string.h>
|
2001-09-19 06:51:32 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2004-10-29 23:28:38 +04:00
|
|
|
#include "nsIAppStartup.h"
|
2004-12-01 02:24:59 +03:00
|
|
|
#include "nsXPFEComponentsCID.h"
|
2003-02-22 22:22:27 +03:00
|
|
|
#endif
|
1999-11-20 13:53:34 +03:00
|
|
|
|
2002-01-14 11:22:35 +03:00
|
|
|
#ifdef MOZ_WIDGET_PHOTON
|
1999-12-07 16:12:26 +03:00
|
|
|
#include <photon/PhProto.h>
|
|
|
|
#endif
|
|
|
|
|
1999-11-19 04:55:19 +03:00
|
|
|
static char _progname[1024] = "huh?";
|
2006-02-09 00:38:52 +03:00
|
|
|
static unsigned int _gdb_sleep_duration = 300;
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2006-08-06 22:48:14 +04:00
|
|
|
#if defined(LINUX) && defined(DEBUG) && \
|
|
|
|
(defined(__i386) || defined(__x86_64) || defined(PPC))
|
1999-11-19 04:55:19 +03:00
|
|
|
#define CRAWL_STACK_ON_SIGSEGV
|
2002-03-06 22:03:32 +03:00
|
|
|
#endif
|
2005-12-22 20:57:13 +03:00
|
|
|
|
2002-01-14 11:22:35 +03:00
|
|
|
#ifdef MOZ_WIDGET_PHOTON
|
1999-11-19 04:55:19 +03:00
|
|
|
void abnormal_exit_handler(int signum)
|
|
|
|
{
|
|
|
|
/* Free any shared memory that has been allocated */
|
|
|
|
PgShmemCleanup();
|
|
|
|
|
1999-12-07 16:12:26 +03:00
|
|
|
#if defined(DEBUG)
|
1999-11-19 04:55:19 +03:00
|
|
|
if ( (signum == SIGSEGV)
|
|
|
|
|| (signum == SIGILL)
|
2001-11-03 08:43:25 +03:00
|
|
|
|| (signum == SIGABRT)
|
2006-04-12 03:41:37 +04:00
|
|
|
|| (signum == SIGFPE)
|
2001-11-03 08:43:25 +03:00
|
|
|
)
|
1999-11-19 04:55:19 +03:00
|
|
|
{
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("prog = %s\npid = %d\nsignal = %s\n",
|
2001-11-03 08:43:25 +03:00
|
|
|
_progname, getpid(), strsignal(signum));
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2006-02-09 00:38:52 +03:00
|
|
|
printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
|
2003-07-21 01:08:42 +04:00
|
|
|
printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
|
2001-11-03 08:43:25 +03:00
|
|
|
_progname, getpid());
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2006-02-09 00:38:52 +03:00
|
|
|
sleep(_gdb_sleep_duration);
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("Done sleeping...\n");
|
1999-11-19 04:55:19 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_exit(1);
|
2005-12-22 20:57:13 +03:00
|
|
|
}
|
1999-11-19 04:55:19 +03:00
|
|
|
#elif defined(CRAWL_STACK_ON_SIGSEGV)
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2001-11-03 08:43:25 +03:00
|
|
|
#include "nsISupportsUtils.h"
|
2007-08-11 01:28:22 +04:00
|
|
|
#include "nsStackWalk.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void PrintStackFrame(void *aPC, void *aClosure)
|
2007-08-11 01:28:22 +04:00
|
|
|
{
|
2007-08-11 01:29:37 +04:00
|
|
|
char buf[1024];
|
|
|
|
nsCodeAddressDetails details;
|
|
|
|
|
|
|
|
NS_DescribeCodeAddress(aPC, &details);
|
|
|
|
NS_FormatCodeAddressDetails(aPC, &details, buf, sizeof(buf));
|
|
|
|
fprintf(stdout, buf);
|
2007-08-11 01:28:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-11-19 04:55:19 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
ah_crap_handler(int signum)
|
|
|
|
{
|
2001-11-03 08:43:25 +03:00
|
|
|
printf("\nProgram %s (pid = %d) received signal %d.\n",
|
|
|
|
_progname,
|
|
|
|
getpid(),
|
|
|
|
signum);
|
2005-12-22 20:57:13 +03:00
|
|
|
|
2001-11-03 08:43:25 +03:00
|
|
|
printf("Stack:\n");
|
2007-08-11 01:28:22 +04:00
|
|
|
NS_StackWalk(PrintStackFrame, 2, nsnull);
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2006-02-09 00:38:52 +03:00
|
|
|
printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
|
2003-07-21 01:08:42 +04:00
|
|
|
printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
|
1999-11-19 04:55:19 +03:00
|
|
|
_progname,
|
|
|
|
getpid());
|
|
|
|
|
2006-02-09 00:38:52 +03:00
|
|
|
sleep(_gdb_sleep_duration);
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("Done sleeping...\n");
|
2009-02-26 09:56:10 +03:00
|
|
|
|
|
|
|
_exit(signum);
|
2001-11-03 08:43:25 +03:00
|
|
|
}
|
1999-11-19 04:55:19 +03:00
|
|
|
#endif // CRAWL_STACK_ON_SIGSEGV
|
|
|
|
|
2001-07-28 02:16:33 +04:00
|
|
|
#ifdef XP_BEOS
|
|
|
|
void beos_signal_handler(int signum) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "beos_signal_handler: %d\n", signum);
|
|
|
|
#endif
|
2001-09-19 06:51:32 +04:00
|
|
|
nsresult rv;
|
2004-10-29 23:28:38 +04:00
|
|
|
nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID, &rv));
|
2001-09-19 06:51:32 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2004-10-29 23:28:38 +04:00
|
|
|
// Failed to get the appstartup service so shutdown the hard way
|
2001-09-19 06:51:32 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "beos_signal_handler: appShell->do_GetService() failed\n");
|
|
|
|
#endif
|
|
|
|
exit(13);
|
2001-07-28 02:16:33 +04:00
|
|
|
}
|
2001-09-19 06:51:32 +04:00
|
|
|
|
|
|
|
// Exit the appshell so that the app can shutdown normally
|
2004-10-29 23:28:38 +04:00
|
|
|
appStartup->Quit(nsIAppStartup::eAttemptQuit);
|
2001-07-28 02:16:33 +04:00
|
|
|
}
|
2005-12-22 20:57:13 +03:00
|
|
|
#endif
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2007-06-12 22:24:32 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
// Need this include for version test below.
|
2007-05-23 19:55:36 +04:00
|
|
|
#include <glib.h>
|
2007-06-12 22:24:32 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
|
2007-05-23 19:55:36 +04:00
|
|
|
|
|
|
|
static GLogFunc orig_log_func = NULL;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
static void
|
|
|
|
my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
|
|
|
|
const gchar *message, gpointer user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
|
|
|
|
const gchar *message, gpointer user_data)
|
|
|
|
{
|
2008-01-07 12:57:28 +03:00
|
|
|
if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)) {
|
2007-05-23 19:55:36 +04:00
|
|
|
NS_DebugBreak(NS_DEBUG_ASSERTION, message, "glib assertion", __FILE__, __LINE__);
|
2008-01-07 12:57:28 +03:00
|
|
|
} else if (log_level & (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)) {
|
|
|
|
NS_DebugBreak(NS_DEBUG_WARNING, message, "glib warning", __FILE__, __LINE__);
|
2007-05-23 19:55:36 +04:00
|
|
|
}
|
2008-01-07 12:57:28 +03:00
|
|
|
|
|
|
|
orig_log_func(log_domain, log_level, message, NULL);
|
2007-05-23 19:55:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1999-11-19 04:55:19 +03:00
|
|
|
void InstallUnixSignalHandlers(const char *ProgramName)
|
|
|
|
{
|
1999-11-20 13:05:44 +03:00
|
|
|
PL_strncpy(_progname,ProgramName, (sizeof(_progname)-1) );
|
1999-11-19 04:55:19 +03:00
|
|
|
|
2006-02-09 00:38:52 +03:00
|
|
|
const char *gdbSleep = PR_GetEnv("MOZ_GDB_SLEEP");
|
|
|
|
if (gdbSleep && *gdbSleep)
|
|
|
|
{
|
|
|
|
unsigned int s;
|
|
|
|
if (1 == sscanf(gdbSleep, "%u", &s)) {
|
|
|
|
_gdb_sleep_duration = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-14 11:22:35 +03:00
|
|
|
#if defined(MOZ_WIDGET_PHOTON)
|
1999-11-19 04:55:19 +03:00
|
|
|
/* Neutrino need this to free shared memory in case of a crash */
|
|
|
|
signal(SIGTERM, abnormal_exit_handler);
|
|
|
|
signal(SIGQUIT, abnormal_exit_handler);
|
|
|
|
signal(SIGINT, abnormal_exit_handler);
|
|
|
|
signal(SIGHUP, abnormal_exit_handler);
|
|
|
|
signal(SIGSEGV, abnormal_exit_handler);
|
|
|
|
signal(SIGILL, abnormal_exit_handler);
|
|
|
|
signal(SIGABRT, abnormal_exit_handler);
|
2006-04-12 03:41:37 +04:00
|
|
|
signal(SIGFPE, abnormal_exit_handler);
|
1999-12-07 16:12:26 +03:00
|
|
|
|
1999-11-19 04:55:19 +03:00
|
|
|
#elif defined(CRAWL_STACK_ON_SIGSEGV)
|
|
|
|
signal(SIGSEGV, ah_crap_handler);
|
|
|
|
signal(SIGILL, ah_crap_handler);
|
|
|
|
signal(SIGABRT, ah_crap_handler);
|
2006-04-12 03:41:37 +04:00
|
|
|
signal(SIGFPE, ah_crap_handler);
|
1999-11-19 04:55:19 +03:00
|
|
|
#endif // CRAWL_STACK_ON_SIGSEGV
|
2000-05-17 08:06:08 +04:00
|
|
|
|
|
|
|
#if defined(DEBUG) && defined(LINUX)
|
2005-12-22 20:57:13 +03:00
|
|
|
const char *memLimit = PR_GetEnv("MOZ_MEM_LIMIT");
|
|
|
|
if (memLimit && *memLimit)
|
2000-08-01 05:46:52 +04:00
|
|
|
{
|
2005-12-22 20:57:13 +03:00
|
|
|
long m = atoi(memLimit);
|
|
|
|
m *= (1024*1024);
|
2000-05-17 08:06:08 +04:00
|
|
|
struct rlimit r;
|
|
|
|
r.rlim_cur = m;
|
|
|
|
r.rlim_max = m;
|
2000-05-17 10:59:50 +04:00
|
|
|
setrlimit(RLIMIT_AS, &r);
|
2000-05-17 08:06:08 +04:00
|
|
|
}
|
|
|
|
#endif
|
2000-08-22 07:42:32 +04:00
|
|
|
|
|
|
|
#if defined(SOLARIS)
|
2005-12-22 20:57:13 +03:00
|
|
|
#define NOFILES 512
|
2000-08-22 07:42:32 +04:00
|
|
|
|
|
|
|
// Boost Solaris file descriptors
|
|
|
|
{
|
|
|
|
struct rlimit rl;
|
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
|
|
|
|
|
|
|
|
if (rl.rlim_cur < NOFILES) {
|
|
|
|
rl.rlim_cur = NOFILES;
|
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
|
|
|
|
perror("setrlimit(RLIMIT_NOFILE)");
|
2000-10-29 02:17:53 +04:00
|
|
|
fprintf(stderr, "Cannot exceed hard limit for open files");
|
2000-08-22 07:42:32 +04:00
|
|
|
}
|
|
|
|
#if defined(DEBUG)
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("File descriptors set to %d\n", rl.rlim_cur);
|
2000-08-22 07:42:32 +04:00
|
|
|
#endif //DEBUG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif //SOLARIS
|
2001-07-28 02:16:33 +04:00
|
|
|
|
|
|
|
#ifdef XP_BEOS
|
|
|
|
signal(SIGTERM, beos_signal_handler);
|
|
|
|
#endif
|
2007-05-23 19:55:36 +04:00
|
|
|
|
2007-05-23 20:33:12 +04:00
|
|
|
#if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
|
2007-05-23 19:55:36 +04:00
|
|
|
const char *assertString = PR_GetEnv("XPCOM_DEBUG_BREAK");
|
|
|
|
if (assertString &&
|
|
|
|
(!strcmp(assertString, "suspend") ||
|
|
|
|
!strcmp(assertString, "stack") ||
|
|
|
|
!strcmp(assertString, "abort") ||
|
|
|
|
!strcmp(assertString, "trap") ||
|
|
|
|
!strcmp(assertString, "break"))) {
|
|
|
|
// Override the default glib logging function so we get stacks for it too.
|
|
|
|
orig_log_func = g_log_set_default_handler(my_glib_log_func, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
1999-11-20 13:05:44 +03:00
|
|
|
}
|