On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / xpcom / glue / nsDebug.h
blob31f5d4eaa48b7764a19653c4c3b88eb7b63e1731
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
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
13 * License.
15 * The Original Code is mozilla.org 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.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsDebug_h___
39 #define nsDebug_h___
41 #ifndef nscore_h___
42 #include "nscore.h"
43 #endif
45 #ifndef nsError_h__
46 #include "nsError.h"
47 #endif
49 #include "nsXPCOM.h"
51 #ifdef DEBUG
52 #define NS_DEBUG
53 #include "prprf.h"
54 #endif
56 #ifdef DEBUG
58 /**
59 * Abort the execution of the program if the expression evaluates to
60 * false.
62 * There is no status value returned from the macro.
64 * Note that the non-debug version of this macro does <b>not</b>
65 * evaluate the expression argument. Hence side effect statements
66 * as arguments to the macro will yield improper execution in a
67 * non-debug build. For example:
69 * NS_ABORT_IF_FALSE(0 == foo++, "yikes foo should be zero");
71 * Note also that the non-debug version of this macro does <b>not</b>
72 * evaluate the message argument.
74 #define NS_ABORT_IF_FALSE(_expr, _msg) \
75 PR_BEGIN_MACRO \
76 if (!(_expr)) { \
77 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_expr, __FILE__, __LINE__); \
78 } \
79 PR_END_MACRO
81 /**
82 * Warn if a given condition is false.
84 * Program execution continues past the usage of this macro.
86 * Note also that the non-debug version of this macro does <b>not</b>
87 * evaluate the message argument.
89 #define NS_WARN_IF_FALSE(_expr,_msg) \
90 PR_BEGIN_MACRO \
91 if (!(_expr)) { \
92 NS_DebugBreak(NS_DEBUG_WARNING, _msg, #_expr, __FILE__, __LINE__); \
93 } \
94 PR_END_MACRO
96 /**
97 * Test a precondition for truth. If the expression is not true then
98 * trigger a program failure.
100 #define NS_PRECONDITION(expr, str) \
101 PR_BEGIN_MACRO \
102 if (!(expr)) { \
103 NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
105 PR_END_MACRO
108 * Test an assertion for truth. If the expression is not true then
109 * trigger a program failure.
111 #define NS_ASSERTION(expr, str) \
112 PR_BEGIN_MACRO \
113 if (!(expr)) { \
114 NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
116 PR_END_MACRO
119 * Test a post-condition for truth. If the expression is not true then
120 * trigger a program failure.
122 #define NS_POSTCONDITION(expr, str) \
123 PR_BEGIN_MACRO \
124 if (!(expr)) { \
125 NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
127 PR_END_MACRO
130 * This macros triggers a program failure if executed. It indicates that
131 * an attempt was made to execute some unimplemented functionality.
133 #define NS_NOTYETIMPLEMENTED(str) \
134 NS_DebugBreak(NS_DEBUG_ASSERTION, str, "NotYetImplemented", __FILE__, __LINE__)
137 * This macros triggers a program failure if executed. It indicates that
138 * an attempt was made to execute some unimplemented functionality.
140 #define NS_NOTREACHED(str) \
141 NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Not Reached", __FILE__, __LINE__)
144 * Log an error message.
146 #define NS_ERROR(str) \
147 NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Error", __FILE__, __LINE__)
150 * Log a warning message.
152 #define NS_WARNING(str) \
153 NS_DebugBreak(NS_DEBUG_WARNING, str, nsnull, __FILE__, __LINE__)
156 * Trigger an abort
158 #define NS_ABORT() \
159 NS_DebugBreak(NS_DEBUG_ABORT, nsnull, nsnull, __FILE__, __LINE__)
162 * Cause a break
164 #define NS_BREAK() \
165 NS_DebugBreak(NS_DEBUG_BREAK, nsnull, nsnull, __FILE__, __LINE__)
167 #else /* NS_DEBUG */
170 * The non-debug version of these macros do not evaluate the
171 * expression or the message arguments to the macro.
173 #define NS_ABORT_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
174 #define NS_WARN_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
175 #define NS_PRECONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
176 #define NS_ASSERTION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
177 #define NS_POSTCONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
178 #define NS_NOTYETIMPLEMENTED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
179 #define NS_NOTREACHED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
180 #define NS_ERROR(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
181 #define NS_WARNING(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
182 #define NS_ABORT() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
183 #define NS_BREAK() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
185 #endif /* ! NS_DEBUG */
187 /* Macros for checking the trueness of an expression passed in within an
188 * interface implementation. These need to be compiled regardless of the */
189 /* NS_DEBUG flag
190 ******************************************************************************/
192 #define NS_ENSURE_TRUE(x, ret) \
193 PR_BEGIN_MACRO \
194 if (NS_UNLIKELY(!(x))) { \
195 NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \
196 return ret; \
198 PR_END_MACRO
200 #define NS_ENSURE_FALSE(x, ret) \
201 NS_ENSURE_TRUE(!(x), ret)
203 /******************************************************************************
204 ** Macros for checking results
205 ******************************************************************************/
207 #if defined(DEBUG) && !defined(XPCOM_GLUE_AVOID_NSPR)
209 #define NS_ENSURE_SUCCESS_BODY(res, ret) \
210 char *msg = PR_smprintf("NS_ENSURE_SUCCESS(%s, %s) failed with " \
211 "result 0x%X", #res, #ret, __rv); \
212 NS_WARNING(msg); \
213 PR_smprintf_free(msg);
215 #else
217 #define NS_ENSURE_SUCCESS_BODY(res, ret) \
218 NS_WARNING("NS_ENSURE_SUCCESS(" #res ", " #ret ") failed");
220 #endif
222 #define NS_ENSURE_SUCCESS(res, ret) \
223 PR_BEGIN_MACRO \
224 nsresult __rv = res; /* Don't evaluate |res| more than once */ \
225 if (NS_FAILED(__rv)) { \
226 NS_ENSURE_SUCCESS_BODY(res, ret) \
227 return ret; \
229 PR_END_MACRO
231 /******************************************************************************
232 ** Macros for checking state and arguments upon entering interface boundaries
233 ******************************************************************************/
235 #define NS_ENSURE_ARG(arg) \
236 NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG)
238 #define NS_ENSURE_ARG_POINTER(arg) \
239 NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER)
241 #define NS_ENSURE_ARG_MIN(arg, min) \
242 NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG)
244 #define NS_ENSURE_ARG_MAX(arg, max) \
245 NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG)
247 #define NS_ENSURE_ARG_RANGE(arg, min, max) \
248 NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG)
250 #define NS_ENSURE_STATE(state) \
251 NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED)
253 #define NS_ENSURE_NO_AGGREGATION(outer) \
254 NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION)
256 #define NS_ENSURE_PROPER_AGGREGATION(outer, iid) \
257 NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALID_ARG)
259 /*****************************************************************************/
261 #ifdef XPCOM_GLUE
262 #define NS_CheckThreadSafe
263 #else
264 #define NS_CheckThreadSafe(owningThread, msg) \
265 NS_ASSERTION(owningThread == PR_GetCurrentThread(), msg)
266 #endif
268 /* When compiling the XPCOM Glue on Windows, we pretend that it's going to
269 * be linked with a static CRT (-MT) even when it's not. This means that we
270 * cannot link to data exports from the CRT, only function exports. So,
271 * instead of referencing "stderr" directly, use fdopen.
273 PR_BEGIN_EXTERN_C
275 NS_COM_GLUE void
276 printf_stderr(const char *fmt, ...);
278 PR_END_EXTERN_C
280 #endif /* nsDebug_h___ */