1 | /* $Id: assert.cpp 25528 2009-12-20 23:24:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Assertions, common code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/log.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/stdarg.h>
|
---|
42 | #ifdef IN_RING3
|
---|
43 | # include <stdio.h>
|
---|
44 | #endif
|
---|
45 | #include "internal/assert.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Global Variables *
|
---|
50 | *******************************************************************************/
|
---|
51 | /** The last assert message, 1st part. */
|
---|
52 | RTDATADECL(char) g_szRTAssertMsg1[1024];
|
---|
53 | RT_EXPORT_SYMBOL(g_szRTAssertMsg1);
|
---|
54 | /** The last assert message, 2nd part. */
|
---|
55 | RTDATADECL(char) g_szRTAssertMsg2[2048];
|
---|
56 | RT_EXPORT_SYMBOL(g_szRTAssertMsg2);
|
---|
57 | /** The last assert message, expression. */
|
---|
58 | RTDATADECL(const char * volatile) g_pszRTAssertExpr;
|
---|
59 | RT_EXPORT_SYMBOL(g_pszRTAssertExpr);
|
---|
60 | /** The last assert message, function name. */
|
---|
61 | RTDATADECL(const char * volatile) g_pszRTAssertFunction;
|
---|
62 | RT_EXPORT_SYMBOL(g_pszRTAssertFunction);
|
---|
63 | /** The last assert message, file name. */
|
---|
64 | RTDATADECL(const char * volatile) g_pszRTAssertFile;
|
---|
65 | RT_EXPORT_SYMBOL(g_pszRTAssertFile);
|
---|
66 | /** The last assert message, line number. */
|
---|
67 | RTDATADECL(uint32_t volatile) g_u32RTAssertLine;
|
---|
68 | RT_EXPORT_SYMBOL(g_u32RTAssertLine);
|
---|
69 |
|
---|
70 |
|
---|
71 | /** Set if assertions are quiet. */
|
---|
72 | static bool volatile g_fQuiet = false;
|
---|
73 | /** Set if assertions may panic. */
|
---|
74 | static bool volatile g_fMayPanic = true;
|
---|
75 |
|
---|
76 |
|
---|
77 | RTDECL(bool) RTAssertSetQuiet(bool fQuiet)
|
---|
78 | {
|
---|
79 | return ASMAtomicXchgBool(&g_fQuiet, fQuiet);
|
---|
80 | }
|
---|
81 | RT_EXPORT_SYMBOL(RTAssertSetQuiet);
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(bool) RTAssertAreQuiet(void)
|
---|
85 | {
|
---|
86 | return ASMAtomicUoReadBool(&g_fQuiet);
|
---|
87 | }
|
---|
88 | RT_EXPORT_SYMBOL(RTAssertAreQuiet);
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(bool) RTAssertSetMayPanic(bool fMayPanic)
|
---|
92 | {
|
---|
93 | return ASMAtomicXchgBool(&g_fMayPanic, fMayPanic);
|
---|
94 | }
|
---|
95 | RT_EXPORT_SYMBOL(RTAssertSetMayPanic);
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(bool) RTAssertMayPanic(void)
|
---|
99 | {
|
---|
100 | return ASMAtomicUoReadBool(&g_fMayPanic);
|
---|
101 | }
|
---|
102 | RT_EXPORT_SYMBOL(RTAssertMayPanic);
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(void) RTAssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
106 | {
|
---|
107 | /*
|
---|
108 | * Fill in the globals.
|
---|
109 | */
|
---|
110 | ASMAtomicUoWritePtr((void * volatile *)&g_pszRTAssertExpr, (void *)pszExpr);
|
---|
111 | ASMAtomicUoWritePtr((void * volatile *)&g_pszRTAssertFile, (void *)pszFile);
|
---|
112 | ASMAtomicUoWritePtr((void * volatile *)&g_pszRTAssertFunction, (void *)pszFunction);
|
---|
113 | ASMAtomicUoWriteU32(&g_u32RTAssertLine, uLine);
|
---|
114 | RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
|
---|
115 | "\n!!Assertion Failed!!\n"
|
---|
116 | "Expression: %s\n"
|
---|
117 | "Location : %s(%d) %s\n",
|
---|
118 | pszExpr, pszFile, uLine, pszFunction);
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * If not quiet, make noise.
|
---|
122 | */
|
---|
123 | if (!RTAssertAreQuiet())
|
---|
124 | {
|
---|
125 | #ifdef IN_RING0
|
---|
126 | # ifdef IN_GUEST_R0
|
---|
127 | RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
|
---|
128 | "Expression: %s\n"
|
---|
129 | "Location : %s(%d) %s\n",
|
---|
130 | pszExpr, pszFile, uLine, pszFunction);
|
---|
131 | # endif
|
---|
132 | /** @todo fully integrate this with the logger... play safe a bit for now. */
|
---|
133 | rtR0AssertNativeMsg1(pszExpr, uLine, pszFile, pszFunction);
|
---|
134 |
|
---|
135 | #else /* !IN_RING0 */
|
---|
136 | # if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
137 | RTLogComPrintf("\n!!Assertion Failed!!\n"
|
---|
138 | "Expression: %s\n"
|
---|
139 | "Location : %s(%d) %s\n",
|
---|
140 | pszExpr, pszFile, uLine, pszFunction);
|
---|
141 | # endif
|
---|
142 |
|
---|
143 | PRTLOGGER pLog = RTLogRelDefaultInstance();
|
---|
144 | if (pLog)
|
---|
145 | {
|
---|
146 | RTLogRelPrintf("\n!!Assertion Failed!!\n"
|
---|
147 | "Expression: %s\n"
|
---|
148 | "Location : %s(%d) %s\n",
|
---|
149 | pszExpr, pszFile, uLine, pszFunction);
|
---|
150 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
151 | RTLogFlush(pLog);
|
---|
152 | # endif
|
---|
153 | }
|
---|
154 |
|
---|
155 | # ifndef LOG_ENABLED
|
---|
156 | if (!pLog)
|
---|
157 | # endif
|
---|
158 | {
|
---|
159 | pLog = RTLogDefaultInstance();
|
---|
160 | if (pLog)
|
---|
161 | {
|
---|
162 | RTLogPrintf("\n!!Assertion Failed!!\n"
|
---|
163 | "Expression: %s\n"
|
---|
164 | "Location : %s(%d) %s\n",
|
---|
165 | pszExpr, pszFile, uLine, pszFunction);
|
---|
166 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
167 | RTLogFlush(pLog);
|
---|
168 | # endif
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | # ifdef IN_RING3
|
---|
173 | /* print to stderr, helps user and gdb debugging. */
|
---|
174 | fprintf(stderr,
|
---|
175 | "\n!!Assertion Failed!!\n"
|
---|
176 | "Expression: %s\n"
|
---|
177 | "Location : %s(%d) %s\n",
|
---|
178 | VALID_PTR(pszExpr) ? pszExpr : "<none>",
|
---|
179 | VALID_PTR(pszFile) ? pszFile : "<none>",
|
---|
180 | uLine,
|
---|
181 | VALID_PTR(pszFunction) ? pszFunction : "");
|
---|
182 | fflush(stderr);
|
---|
183 | # endif
|
---|
184 | #endif /* !IN_RING0 */
|
---|
185 | }
|
---|
186 | }
|
---|
187 | RT_EXPORT_SYMBOL(RTAssertMsg1);
|
---|
188 |
|
---|
189 |
|
---|
190 | RTDECL(void) RTAssertMsg2V(const char *pszFormat, va_list va)
|
---|
191 | {
|
---|
192 | va_list vaCopy;
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * The global first.
|
---|
196 | */
|
---|
197 | va_copy(vaCopy, va);
|
---|
198 | RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, vaCopy);
|
---|
199 | va_end(vaCopy);
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * If not quiet, make some noise.
|
---|
203 | */
|
---|
204 | if (!RTAssertAreQuiet())
|
---|
205 | {
|
---|
206 | #ifdef IN_RING0
|
---|
207 | # ifdef IN_GUEST_R0
|
---|
208 | va_copy(vaCopy, va);
|
---|
209 | RTLogBackdoorPrintfV(pszFormat, vaCopy);
|
---|
210 | va_end(vaCopy);
|
---|
211 | # endif
|
---|
212 | /** @todo fully integrate this with the logger... play safe a bit for now. */
|
---|
213 | rtR0AssertNativeMsg2V(pszFormat, va);
|
---|
214 |
|
---|
215 | #else /* !IN_RING0 */
|
---|
216 | # if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
217 | va_copy(vaCopy, va);
|
---|
218 | RTLogComPrintfV(pszFormat, vaCopy);
|
---|
219 | va_end(vaCopy);
|
---|
220 | # endif
|
---|
221 |
|
---|
222 | PRTLOGGER pLog = RTLogRelDefaultInstance();
|
---|
223 | if (pLog)
|
---|
224 | {
|
---|
225 | va_copy(vaCopy, va);
|
---|
226 | RTLogRelPrintfV(pszFormat, vaCopy);
|
---|
227 | va_end(vaCopy);
|
---|
228 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
229 | RTLogFlush(pLog);
|
---|
230 | # endif
|
---|
231 | }
|
---|
232 |
|
---|
233 | pLog = RTLogDefaultInstance();
|
---|
234 | if (pLog)
|
---|
235 | {
|
---|
236 | va_copy(vaCopy, va);
|
---|
237 | RTLogPrintfV(pszFormat, vaCopy);
|
---|
238 | va_end(vaCopy);
|
---|
239 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
240 | RTLogFlush(pLog);
|
---|
241 | #endif
|
---|
242 | }
|
---|
243 |
|
---|
244 | # ifdef IN_RING3
|
---|
245 | /* print to stderr, helps user and gdb debugging. */
|
---|
246 | char szMsg[1024];
|
---|
247 | va_copy(vaCopy, va);
|
---|
248 | RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, vaCopy);
|
---|
249 | va_end(vaCopy);
|
---|
250 | fprintf(stderr, "%s", szMsg);
|
---|
251 | fflush(stderr);
|
---|
252 | # endif
|
---|
253 | #endif /* !IN_RING0 */
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|
257 | RT_EXPORT_SYMBOL(RTAssertMsg2V);
|
---|
258 |
|
---|