1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * IBM Corp.
|
---|
24 | * Henry Sobotka
|
---|
25 | *
|
---|
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.
|
---|
37 | *
|
---|
38 | * ***** END LICENSE BLOCK ***** */
|
---|
39 |
|
---|
40 | #include "nsXPCOMPrivate.h"
|
---|
41 | #include "nsDebugImpl.h"
|
---|
42 | #include "nsDebug.h"
|
---|
43 | #include "prprf.h"
|
---|
44 | #include "prinit.h"
|
---|
45 | #include "plstr.h"
|
---|
46 | #include "nsError.h"
|
---|
47 |
|
---|
48 | #if defined(XP_UNIX) && !defined(UNIX_CRASH_ON_ASSERT)
|
---|
49 | #include <signal.h>
|
---|
50 | /* for nsTraceRefcnt::WalkTheStack() */
|
---|
51 | #include "nsISupportsUtils.h"
|
---|
52 | #include "nsTraceRefcntImpl.h"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #include <iprt/cdefs.h>
|
---|
56 | #include <iprt/env.h>
|
---|
57 | #include <VBox/log.h>
|
---|
58 |
|
---|
59 | NS_IMPL_THREADSAFE_ISUPPORTS1(nsDebugImpl, nsIDebug)
|
---|
60 |
|
---|
61 | nsDebugImpl::nsDebugImpl()
|
---|
62 | {
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Implementation of the nsDebug methods. Note that this code is
|
---|
67 | * always compiled in, in case some other module that uses it is
|
---|
68 | * compiled with debugging even if this library is not.
|
---|
69 | */
|
---|
70 |
|
---|
71 | NS_IMETHODIMP
|
---|
72 | nsDebugImpl::Assertion(const char *aStr, const char *aExpr, const char *aFile, PRInt32 aLine)
|
---|
73 | {
|
---|
74 | RTAssertMsg1(aExpr, aLine, aFile, NULL);
|
---|
75 | RTAssertMsg2("%s\n", aStr);
|
---|
76 | return NS_OK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | NS_IMETHODIMP
|
---|
80 | nsDebugImpl::Break(const char *aFile, PRInt32 aLine)
|
---|
81 | {
|
---|
82 | #if defined(XP_UNIX) && !defined(UNIX_CRASH_ON_ASSERT)
|
---|
83 | fprintf(stderr, "\07");
|
---|
84 |
|
---|
85 | const char *assertBehavior = RTEnvGet("XPCOM_DEBUG_BREAK");
|
---|
86 |
|
---|
87 | if (!assertBehavior) {
|
---|
88 |
|
---|
89 | // the default; nothing else to do
|
---|
90 | ;
|
---|
91 |
|
---|
92 | } else if ( strcmp(assertBehavior, "suspend")== 0 ) {
|
---|
93 |
|
---|
94 | // the suspend case is first because we wanna send the signal before
|
---|
95 | // other threads have had a chance to get too far from the state that
|
---|
96 | // caused this assertion (in case they happen to have been involved).
|
---|
97 | //
|
---|
98 | fprintf(stderr, "Suspending process; attach with the debugger.\n");
|
---|
99 | kill(0, SIGSTOP);
|
---|
100 |
|
---|
101 | } else if ( strcmp(assertBehavior, "warn")==0 ) {
|
---|
102 |
|
---|
103 | // same as default; nothing else to do (see "suspend" case comment for
|
---|
104 | // why this compare isn't done as part of the default case)
|
---|
105 | //
|
---|
106 | ;
|
---|
107 |
|
---|
108 | }
|
---|
109 | else if ( strcmp(assertBehavior,"stack")==0 ) {
|
---|
110 |
|
---|
111 | // walk the stack
|
---|
112 | //
|
---|
113 | nsTraceRefcntImpl::WalkTheStack(stderr);
|
---|
114 | }
|
---|
115 | else if ( strcmp(assertBehavior,"abort")==0 ) {
|
---|
116 |
|
---|
117 | // same as UNIX_CRASH_ON_ASSERT
|
---|
118 | //
|
---|
119 | Abort(aFile, aLine);
|
---|
120 |
|
---|
121 | } else if ( strcmp(assertBehavior,"trap")==0 ) {
|
---|
122 | RT_BREAKPOINT();
|
---|
123 | } else {
|
---|
124 |
|
---|
125 | fprintf(stderr, "unrecognized value of XPCOM_DEBUG_BREAK env var!\n");
|
---|
126 |
|
---|
127 | }
|
---|
128 | fflush(stderr); // this shouldn't really be necessary, i don't think,
|
---|
129 | // but maybe there's some lame stdio that buffers stderr
|
---|
130 | #else
|
---|
131 | Abort(aFile, aLine);
|
---|
132 | #endif
|
---|
133 | return NS_OK;
|
---|
134 | }
|
---|
135 |
|
---|
136 | NS_IMETHODIMP
|
---|
137 | nsDebugImpl::Abort(const char *aFile, PRInt32 aLine)
|
---|
138 | {
|
---|
139 | AssertReleaseMsgFailed(("###!!! Abort: at file %s, line %d", aFile, aLine));
|
---|
140 | return NS_OK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | NS_IMETHODIMP
|
---|
144 | nsDebugImpl::Warning(const char* aMessage,
|
---|
145 | const char* aFile, PRIntn aLine)
|
---|
146 | {
|
---|
147 | /* Debug log. */
|
---|
148 | Log(("WARNING: %s, file %s, line %d", aMessage, aFile, aLine));
|
---|
149 |
|
---|
150 | // And write it out to the stdout
|
---|
151 | fprintf(stderr, "WARNING: %s, file %s, line %d", aMessage, aFile, aLine);
|
---|
152 | fflush(stderr);
|
---|
153 | return NS_OK;
|
---|
154 | }
|
---|
155 |
|
---|
156 | NS_METHOD
|
---|
157 | nsDebugImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
---|
158 | {
|
---|
159 | *aInstancePtr = nsnull;
|
---|
160 | nsIDebug* debug = new nsDebugImpl();
|
---|
161 | if (!debug)
|
---|
162 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
163 |
|
---|
164 | nsresult rv = debug->QueryInterface(aIID, aInstancePtr);
|
---|
165 | if (NS_FAILED(rv)) {
|
---|
166 | delete debug;
|
---|
167 | }
|
---|
168 |
|
---|
169 | return rv;
|
---|
170 | }
|
---|
171 |
|
---|