1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Netscape Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/NPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is mozilla.org code.
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998 Netscape Communications Corporation. All
25
#include "nsDebugImpl.h"
36
/* For DEBUGGER macros */
40
#if defined(XP_UNIX) || defined(_WIN32) || defined(XP_OS2) || defined(XP_BEOS)
41
/* for abort() and getenv() */
45
#if defined(XP_UNIX) && !defined(UNIX_CRASH_ON_ASSERT)
47
/* for nsTraceRefcnt::WalkTheStack() */
48
#include "nsISupportsUtils.h"
49
#include "nsTraceRefcntImpl.h"
51
#if defined(__GNUC__) && defined(__i386)
52
# define DebugBreak() { asm("int $3"); }
59
/* Added definitions for DebugBreak() for 2 different OS/2 compilers. Doing
60
* the int3 on purpose so that a developer can step over the
61
* instruction if so desired. Not always possible if trapping due to exception
64
#define INCL_WINDIALOGS // need for WinMessageBox
69
#define DebugBreak() { asm("int $3"); }
81
//------------------------
84
#include <Processes.h>
87
// TEMPORARY UNTIL WE HAVE MACINTOSH ENVIRONMENT VARIABLES THAT CAN TURN ON
88
// LOGGING ON MACINTOSH
89
// At this moment, NSPR's logging is a no-op on Macintosh.
96
#define PR_LOG(module,level,args) dprintf args
98
static void dprintf(const char *format, ...)
103
va_start(ap, format);
104
buffer[0] = std::vsnprintf((char *)buffer + 1, sizeof(buffer) - 1, format, ap);
106
if (PL_strcasestr((char *)&buffer[1], "warning"))
107
printf("ļæ½ļæ½ļæ½%s\n", (char*)buffer + 1);
111
#endif // TEMP_MAC_HACK
112
//------------------------
113
#elif defined(XP_UNIX)
118
* Determine if debugger is present in windows.
122
typedef WINBASEAPI BOOL (WINAPI* LPFNISDEBUGGERPRESENT)();
125
PRBool fReturn = PR_FALSE;
126
LPFNISDEBUGGERPRESENT lpfnIsDebuggerPresent = NULL;
127
HINSTANCE hKernel = LoadLibrary("Kernel32.dll");
131
lpfnIsDebuggerPresent =
132
(LPFNISDEBUGGERPRESENT)GetProcAddress(hKernel, "IsDebuggerPresent");
133
if(lpfnIsDebuggerPresent)
135
fReturn = (*lpfnIsDebuggerPresent)();
137
FreeLibrary(hKernel);
145
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDebugImpl, nsIDebug)
147
nsDebugImpl::nsDebugImpl()
152
* Implementation of the nsDebug methods. Note that this code is
153
* always compiled in, in case some other module that uses it is
154
* compiled with debugging even if this library is not.
156
static PRLogModuleInfo* gDebugLog;
158
static void InitLog(void)
160
if (0 == gDebugLog) {
161
gDebugLog = PR_NewLogModule("nsDebug");
162
gDebugLog->level = PR_LOG_DEBUG;
169
nsDebugImpl::Assertion(const char *aStr, const char *aExpr, const char *aFile, PRInt32 aLine)
174
PR_snprintf(buf, sizeof(buf),
175
"###!!! ASSERTION: %s: '%s', file %s, line %d",
176
aStr, aExpr, aFile, aLine);
178
// Write out the assertion message to the debug log
179
PR_LOG(gDebugLog, PR_LOG_ERROR, ("%s", buf));
182
// And write it out to the stderr
183
fprintf(stderr, "%s\n", buf);
187
char* assertBehavior = getenv("XPCOM_DEBUG_BREAK");
188
if (assertBehavior && strcmp(assertBehavior, "warn") == 0)
193
DWORD code = IDRETRY;
195
/* Create the debug dialog out of process to avoid the crashes caused by
196
* Windows events leaking into our event loop from an in process dialog.
197
* We do this by launching windbgdlg.exe (built in xpcom/windbgdlg).
198
* See http://bugzilla.mozilla.org/show_bug.cgi?id=54792
200
PROCESS_INFORMATION pi;
202
char executable[MAX_PATH];
205
memset(&pi, 0, sizeof(pi));
207
memset(&si, 0, sizeof(si));
209
si.wShowWindow = SW_SHOW;
211
if(GetModuleFileName(GetModuleHandle("xpcom.dll"), executable, MAX_PATH) &&
212
NULL != (pName = strrchr(executable, '\\')) &&
213
NULL != strcpy(pName+1, "windbgdlg.exe") &&
215
(printf("Launching %s\n", executable), PR_TRUE) &&
217
CreateProcess(executable, buf, NULL, NULL, PR_FALSE,
218
DETACHED_PROCESS | NORMAL_PRIORITY_CLASS,
219
NULL, NULL, &si, &pi) &&
220
WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess, INFINITE) &&
221
GetExitCodeProcess(pi.hProcess, &code))
223
CloseHandle(pi.hProcess);
229
//This should exit us
231
//If we are ignored exit this way..
243
char* assertBehavior = getenv("XPCOM_DEBUG_BREAK");
244
if (assertBehavior && strcmp(assertBehavior, "warn") == 0)
248
PR_snprintf(msg, sizeof(msg),
249
"%s\n\nClick Cancel to Debug Application.\n"
250
"Click Enter to continue running the Application.", buf);
251
ULONG code = MBID_ERROR;
252
code = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, msg,
253
"nsDebug::Assertion", 0,
254
MB_ERROR | MB_ENTERCANCEL);
256
/* It is possible that we are executing on a thread that doesn't have a
257
* message queue. In that case, the message won't appear, and code will
258
* be 0xFFFF. We'll give the user a chance to debug it by calling
260
* Actually, that's a really bad idea since this happens a lot with threadsafe
261
* assertions and since it means that you can't actually run the debug build
262
* outside a debugger without it crashing constantly.
264
if(( code == MBID_ENTER ) || (code == MBID_ERROR))
267
// If Retry, Fall Through
276
nsDebugImpl::Break(const char *aFile, PRInt32 aLine)
278
#ifndef TEMP_MAC_HACK
279
// Write out the assertion message to the debug log
282
PR_LOG(gDebugLog, PR_LOG_ERROR,
283
("###!!! Break: at file %s, line %d", aFile, aLine));
286
fprintf(stderr, "Break: at file %s, line %d\n",aFile, aLine); fflush(stderr);
293
#elif defined(XP_UNIX) && !defined(UNIX_CRASH_ON_ASSERT)
294
fprintf(stderr, "\07");
296
char *assertBehavior = getenv("XPCOM_DEBUG_BREAK");
298
if (!assertBehavior) {
300
// the default; nothing else to do
303
} else if ( strcmp(assertBehavior, "suspend")== 0 ) {
305
// the suspend case is first because we wanna send the signal before
306
// other threads have had a chance to get too far from the state that
307
// caused this assertion (in case they happen to have been involved).
309
fprintf(stderr, "Suspending process; attach with the debugger.\n");
312
} else if ( strcmp(assertBehavior, "warn")==0 ) {
314
// same as default; nothing else to do (see "suspend" case comment for
315
// why this compare isn't done as part of the default case)
320
else if ( strcmp(assertBehavior,"stack")==0 ) {
324
nsTraceRefcntImpl::WalkTheStack(stderr);
326
else if ( strcmp(assertBehavior,"abort")==0 ) {
328
// same as UNIX_CRASH_ON_ASSERT
332
} else if ( strcmp(assertBehavior,"trap")==0 ) {
338
fprintf(stderr, "unrecognized value of XPCOM_DEBUG_BREAK env var!\n");
341
fflush(stderr); // this shouldn't really be necessary, i don't think,
342
// but maybe there's some lame stdio that buffers stderr
344
#elif defined(XP_BEOS)
346
#ifdef UNIX_CRASH_ON_ASSERT
348
sprintf(buf, "Break: at file %s, line %d", aFile, aLine);
355
#endif // TEMP_MAC_HACK
360
nsDebugImpl::Abort(const char *aFile, PRInt32 aLine)
364
PR_LOG(gDebugLog, PR_LOG_ERROR,
365
("###!!! Abort: at file %s, line %d", aFile, aLine));
367
fprintf(stderr, "\07 Abort\n"); fflush(stderr);
372
long* __p = (long*) 0x7;
377
#elif defined(XP_MAC)
379
#elif defined(XP_UNIX)
381
#elif defined(XP_OS2)
384
#elif defined(XP_BEOS)
388
sprintf(buf, "Abort: at file %s, line %d", aFile, aLine);
397
nsDebugImpl::Warning(const char* aMessage,
398
const char* aFile, PRIntn aLine)
403
PR_snprintf(buf, sizeof(buf),
404
"WARNING: %s, file %s, line %d",
405
aMessage, aFile, aLine);
407
// Write out the warning message to the debug log
408
PR_LOG(gDebugLog, PR_LOG_ERROR, ("%s", buf));
410
// And write it out to the stdout
411
fprintf(stderr, "%s\n", buf);
417
nsDebugImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
419
*aInstancePtr = nsnull;
420
nsIDebug* debug = new nsDebugImpl();
422
return NS_ERROR_OUT_OF_MEMORY;
424
nsresult rv = debug->QueryInterface(aIID, aInstancePtr);
432
////////////////////////////////////////////////////////////////////////////////
435
NS_ErrorAccordingToNSPR()
437
PRErrorCode err = PR_GetError();
439
case PR_OUT_OF_MEMORY_ERROR: return NS_ERROR_OUT_OF_MEMORY;
440
case PR_WOULD_BLOCK_ERROR: return NS_BASE_STREAM_WOULD_BLOCK;
441
case PR_FILE_NOT_FOUND_ERROR: return NS_ERROR_FILE_NOT_FOUND;
442
case PR_READ_ONLY_FILESYSTEM_ERROR: return NS_ERROR_FILE_READ_ONLY;
443
case PR_NOT_DIRECTORY_ERROR: return NS_ERROR_FILE_NOT_DIRECTORY;
444
case PR_IS_DIRECTORY_ERROR: return NS_ERROR_FILE_IS_DIRECTORY;
445
case PR_LOOP_ERROR: return NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
446
case PR_FILE_EXISTS_ERROR: return NS_ERROR_FILE_ALREADY_EXISTS;
447
case PR_FILE_IS_LOCKED_ERROR: return NS_ERROR_FILE_IS_LOCKED;
448
case PR_FILE_TOO_BIG_ERROR: return NS_ERROR_FILE_TOO_BIG;
449
case PR_NO_DEVICE_SPACE_ERROR: return NS_ERROR_FILE_NO_DEVICE_SPACE;
450
case PR_NAME_TOO_LONG_ERROR: return NS_ERROR_FILE_NAME_TOO_LONG;
451
case PR_DIRECTORY_NOT_EMPTY_ERROR: return NS_ERROR_FILE_DIR_NOT_EMPTY;
452
case PR_NO_ACCESS_RIGHTS_ERROR: return NS_ERROR_FILE_ACCESS_DENIED;
453
default: return NS_ERROR_FAILURE;
457
////////////////////////////////////////////////////////////////////////////////