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.
55 #include <sys/resource.h>
57 #include <stdlib.h> // atoi
61 #include <sys/resource.h>
65 #include <be/app/Application.h>
68 #include "nsIServiceManager.h"
69 #include "nsIAppStartup.h"
70 #include "nsXPFEComponentsCID.h"
73 #ifdef MOZ_WIDGET_PHOTON
74 #include <photon/PhProto.h>
77 static char _progname
[1024] = "huh?";
78 static unsigned int _gdb_sleep_duration
= 300;
80 #if defined(LINUX) && defined(DEBUG) && \
81 (defined(__i386) || defined(__x86_64) || defined(PPC))
82 #define CRAWL_STACK_ON_SIGSEGV
85 #ifdef MOZ_WIDGET_PHOTON
86 void abnormal_exit_handler(int signum
)
88 /* Free any shared memory that has been allocated */
92 if ( (signum
== SIGSEGV
)
94 || (signum
== SIGABRT
)
98 printf("prog = %s\npid = %d\nsignal = %s\n",
99 _progname
, getpid(), strsignal(signum
));
101 printf("Sleeping for %d seconds.\n",_gdb_sleep_duration
);
102 printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
103 _progname
, getpid());
105 sleep(_gdb_sleep_duration
);
107 printf("Done sleeping...\n");
113 #elif defined(CRAWL_STACK_ON_SIGSEGV)
116 #include "nsISupportsUtils.h"
117 #include "nsStackWalk.h"
121 PR_STATIC_CALLBACK(void) PrintStackFrame(void *aPC
, void *aClosure
)
124 nsCodeAddressDetails details
;
126 NS_DescribeCodeAddress(aPC
, &details
);
127 NS_FormatCodeAddressDetails(aPC
, &details
, buf
, sizeof(buf
));
128 fprintf(stdout
, buf
);
134 ah_crap_handler(int signum
)
136 printf("\nProgram %s (pid = %d) received signal %d.\n",
142 NS_StackWalk(PrintStackFrame
, 2, nsnull
);
144 printf("Sleeping for %d seconds.\n",_gdb_sleep_duration
);
145 printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
149 sleep(_gdb_sleep_duration
);
151 printf("Done sleeping...\n");
153 #endif // CRAWL_STACK_ON_SIGSEGV
156 void beos_signal_handler(int signum
) {
158 fprintf(stderr
, "beos_signal_handler: %d\n", signum
);
161 nsCOMPtr
<nsIAppStartup
> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID
, &rv
));
163 // Failed to get the appstartup service so shutdown the hard way
165 fprintf(stderr
, "beos_signal_handler: appShell->do_GetService() failed\n");
170 // Exit the appshell so that the app can shutdown normally
171 appStartup
->Quit(nsIAppStartup::eAttemptQuit
);
175 #ifdef MOZ_WIDGET_GTK2
176 // Need this include for version test below.
180 #if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
182 static GLogFunc orig_log_func
= NULL
;
186 my_glib_log_func(const gchar
*log_domain
, GLogLevelFlags log_level
,
187 const gchar
*message
, gpointer user_data
);
191 my_glib_log_func(const gchar
*log_domain
, GLogLevelFlags log_level
,
192 const gchar
*message
, gpointer user_data
)
194 if (log_level
& (G_LOG_LEVEL_ERROR
| G_LOG_FLAG_FATAL
| G_LOG_FLAG_RECURSION
)) {
195 NS_DebugBreak(NS_DEBUG_ASSERTION
, message
, "glib assertion", __FILE__
, __LINE__
);
196 } else if (log_level
& (G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_WARNING
)) {
197 NS_DebugBreak(NS_DEBUG_WARNING
, message
, "glib warning", __FILE__
, __LINE__
);
200 orig_log_func(log_domain
, log_level
, message
, NULL
);
205 void InstallUnixSignalHandlers(const char *ProgramName
)
207 PL_strncpy(_progname
,ProgramName
, (sizeof(_progname
)-1) );
209 const char *gdbSleep
= PR_GetEnv("MOZ_GDB_SLEEP");
210 if (gdbSleep
&& *gdbSleep
)
213 if (1 == sscanf(gdbSleep
, "%u", &s
)) {
214 _gdb_sleep_duration
= s
;
218 #if defined(MOZ_WIDGET_PHOTON)
219 /* Neutrino need this to free shared memory in case of a crash */
220 signal(SIGTERM
, abnormal_exit_handler
);
221 signal(SIGQUIT
, abnormal_exit_handler
);
222 signal(SIGINT
, abnormal_exit_handler
);
223 signal(SIGHUP
, abnormal_exit_handler
);
224 signal(SIGSEGV
, abnormal_exit_handler
);
225 signal(SIGILL
, abnormal_exit_handler
);
226 signal(SIGABRT
, abnormal_exit_handler
);
227 signal(SIGFPE
, abnormal_exit_handler
);
229 #elif defined(CRAWL_STACK_ON_SIGSEGV)
230 signal(SIGSEGV
, ah_crap_handler
);
231 signal(SIGILL
, ah_crap_handler
);
232 signal(SIGABRT
, ah_crap_handler
);
233 signal(SIGFPE
, ah_crap_handler
);
234 #endif // CRAWL_STACK_ON_SIGSEGV
236 #if defined(DEBUG) && defined(LINUX)
237 const char *memLimit
= PR_GetEnv("MOZ_MEM_LIMIT");
238 if (memLimit
&& *memLimit
)
240 long m
= atoi(memLimit
);
245 setrlimit(RLIMIT_AS
, &r
);
252 // Boost Solaris file descriptors
256 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
258 if (rl
.rlim_cur
< NOFILES
) {
259 rl
.rlim_cur
= NOFILES
;
261 if (setrlimit(RLIMIT_NOFILE
, &rl
) < 0) {
262 perror("setrlimit(RLIMIT_NOFILE)");
263 fprintf(stderr
, "Cannot exceed hard limit for open files");
266 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
267 printf("File descriptors set to %d\n", rl
.rlim_cur
);
274 signal(SIGTERM
, beos_signal_handler
);
277 #if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
278 const char *assertString
= PR_GetEnv("XPCOM_DEBUG_BREAK");
280 (!strcmp(assertString
, "suspend") ||
281 !strcmp(assertString
, "stack") ||
282 !strcmp(assertString
, "abort") ||
283 !strcmp(assertString
, "trap") ||
284 !strcmp(assertString
, "break"))) {
285 // Override the default glib logging function so we get stacks for it too.
286 orig_log_func
= g_log_set_default_handler(my_glib_log_func
, NULL
);