1 | /* $Id: assert.cpp 30111 2010-06-09 12:14:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Assertions, common code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/log.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/stdarg.h>
|
---|
38 | #ifdef IN_RING3
|
---|
39 | # include <stdio.h>
|
---|
40 | #endif
|
---|
41 | #include "internal/assert.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Global Variables *
|
---|
46 | *******************************************************************************/
|
---|
47 | /** The last assert message, 1st part. */
|
---|
48 | RTDATADECL(char) g_szRTAssertMsg1[1024];
|
---|
49 | RT_EXPORT_SYMBOL(g_szRTAssertMsg1);
|
---|
50 | /** The last assert message, 2nd part. */
|
---|
51 | RTDATADECL(char) g_szRTAssertMsg2[4096];
|
---|
52 | RT_EXPORT_SYMBOL(g_szRTAssertMsg2);
|
---|
53 | /** The length of the g_szRTAssertMsg2 content.
|
---|
54 | * @remarks Race. */
|
---|
55 | static uint32_t volatile g_cchRTAssertMsg2;
|
---|
56 | /** The last assert message, expression. */
|
---|
57 | RTDATADECL(const char * volatile) g_pszRTAssertExpr;
|
---|
58 | RT_EXPORT_SYMBOL(g_pszRTAssertExpr);
|
---|
59 | /** The last assert message, function name. */
|
---|
60 | RTDATADECL(const char * volatile) g_pszRTAssertFunction;
|
---|
61 | RT_EXPORT_SYMBOL(g_pszRTAssertFunction);
|
---|
62 | /** The last assert message, file name. */
|
---|
63 | RTDATADECL(const char * volatile) g_pszRTAssertFile;
|
---|
64 | RT_EXPORT_SYMBOL(g_pszRTAssertFile);
|
---|
65 | /** The last assert message, line number. */
|
---|
66 | RTDATADECL(uint32_t volatile) g_u32RTAssertLine;
|
---|
67 | RT_EXPORT_SYMBOL(g_u32RTAssertLine);
|
---|
68 |
|
---|
69 |
|
---|
70 | /** Set if assertions are quiet. */
|
---|
71 | static bool volatile g_fQuiet = false;
|
---|
72 | /** Set if assertions may panic. */
|
---|
73 | static bool volatile g_fMayPanic = true;
|
---|
74 |
|
---|
75 |
|
---|
76 | RTDECL(bool) RTAssertSetQuiet(bool fQuiet)
|
---|
77 | {
|
---|
78 | return ASMAtomicXchgBool(&g_fQuiet, fQuiet);
|
---|
79 | }
|
---|
80 | RT_EXPORT_SYMBOL(RTAssertSetQuiet);
|
---|
81 |
|
---|
82 |
|
---|
83 | RTDECL(bool) RTAssertAreQuiet(void)
|
---|
84 | {
|
---|
85 | return ASMAtomicUoReadBool(&g_fQuiet);
|
---|
86 | }
|
---|
87 | RT_EXPORT_SYMBOL(RTAssertAreQuiet);
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(bool) RTAssertSetMayPanic(bool fMayPanic)
|
---|
91 | {
|
---|
92 | return ASMAtomicXchgBool(&g_fMayPanic, fMayPanic);
|
---|
93 | }
|
---|
94 | RT_EXPORT_SYMBOL(RTAssertSetMayPanic);
|
---|
95 |
|
---|
96 |
|
---|
97 | RTDECL(bool) RTAssertMayPanic(void)
|
---|
98 | {
|
---|
99 | return ASMAtomicUoReadBool(&g_fMayPanic);
|
---|
100 | }
|
---|
101 | RT_EXPORT_SYMBOL(RTAssertMayPanic);
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(void) RTAssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
105 | {
|
---|
106 | /*
|
---|
107 | * Fill in the globals.
|
---|
108 | */
|
---|
109 | ASMAtomicUoWritePtr(&g_pszRTAssertExpr, pszExpr);
|
---|
110 | ASMAtomicUoWritePtr(&g_pszRTAssertFile, pszFile);
|
---|
111 | ASMAtomicUoWritePtr(&g_pszRTAssertFunction, pszFunction);
|
---|
112 | ASMAtomicUoWriteU32(&g_u32RTAssertLine, uLine);
|
---|
113 | RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
|
---|
114 | "\n!!Assertion Failed!!\n"
|
---|
115 | "Expression: %s\n"
|
---|
116 | "Location : %s(%d) %s\n",
|
---|
117 | pszExpr, pszFile, uLine, pszFunction);
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * If not quiet, make noise.
|
---|
121 | */
|
---|
122 | if (!RTAssertAreQuiet())
|
---|
123 | {
|
---|
124 | #ifdef IN_RING0
|
---|
125 | # ifdef IN_GUEST_R0
|
---|
126 | RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
|
---|
127 | "Expression: %s\n"
|
---|
128 | "Location : %s(%d) %s\n",
|
---|
129 | pszExpr, pszFile, uLine, pszFunction);
|
---|
130 | # endif
|
---|
131 | /** @todo fully integrate this with the logger... play safe a bit for now. */
|
---|
132 | rtR0AssertNativeMsg1(pszExpr, uLine, pszFile, pszFunction);
|
---|
133 |
|
---|
134 | #else /* !IN_RING0 */
|
---|
135 | # if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
136 | RTLogComPrintf("\n!!Assertion Failed!!\n"
|
---|
137 | "Expression: %s\n"
|
---|
138 | "Location : %s(%d) %s\n",
|
---|
139 | pszExpr, pszFile, uLine, pszFunction);
|
---|
140 | # endif
|
---|
141 |
|
---|
142 | PRTLOGGER pLog = RTLogRelDefaultInstance();
|
---|
143 | if (pLog)
|
---|
144 | {
|
---|
145 | RTLogRelPrintf("\n!!Assertion Failed!!\n"
|
---|
146 | "Expression: %s\n"
|
---|
147 | "Location : %s(%d) %s\n",
|
---|
148 | pszExpr, pszFile, uLine, pszFunction);
|
---|
149 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
150 | RTLogFlush(pLog);
|
---|
151 | # endif
|
---|
152 | }
|
---|
153 |
|
---|
154 | # ifndef LOG_ENABLED
|
---|
155 | if (!pLog)
|
---|
156 | # endif
|
---|
157 | {
|
---|
158 | pLog = RTLogDefaultInstance();
|
---|
159 | if (pLog)
|
---|
160 | {
|
---|
161 | RTLogPrintf("\n!!Assertion Failed!!\n"
|
---|
162 | "Expression: %s\n"
|
---|
163 | "Location : %s(%d) %s\n",
|
---|
164 | pszExpr, pszFile, uLine, pszFunction);
|
---|
165 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
166 | RTLogFlush(pLog);
|
---|
167 | # endif
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | # ifdef IN_RING3
|
---|
172 | /* print to stderr, helps user and gdb debugging. */
|
---|
173 | fprintf(stderr,
|
---|
174 | "\n!!Assertion Failed!!\n"
|
---|
175 | "Expression: %s\n"
|
---|
176 | "Location : %s(%d) %s\n",
|
---|
177 | VALID_PTR(pszExpr) ? pszExpr : "<none>",
|
---|
178 | VALID_PTR(pszFile) ? pszFile : "<none>",
|
---|
179 | uLine,
|
---|
180 | VALID_PTR(pszFunction) ? pszFunction : "");
|
---|
181 | fflush(stderr);
|
---|
182 | # endif
|
---|
183 | #endif /* !IN_RING0 */
|
---|
184 | }
|
---|
185 | }
|
---|
186 | RT_EXPORT_SYMBOL(RTAssertMsg1);
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Worker for RTAssertMsg2V and RTAssertMsg2AddV
|
---|
191 | *
|
---|
192 | * @param fInitial True if it's RTAssertMsg2V, otherwise false.
|
---|
193 | * @param pszFormat The message format string.
|
---|
194 | * @param va The format arguments.
|
---|
195 | */
|
---|
196 | static void rtAssertMsg2Worker(bool fInitial, const char *pszFormat, va_list va)
|
---|
197 | {
|
---|
198 | va_list vaCopy;
|
---|
199 | size_t cch;
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * The global first.
|
---|
203 | */
|
---|
204 | if (fInitial)
|
---|
205 | {
|
---|
206 | va_copy(vaCopy, va);
|
---|
207 | cch = RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, vaCopy);
|
---|
208 | ASMAtomicWriteU32(&g_cchRTAssertMsg2, (uint32_t)cch);
|
---|
209 | va_end(vaCopy);
|
---|
210 | }
|
---|
211 | else
|
---|
212 | {
|
---|
213 | cch = ASMAtomicReadU32(&g_cchRTAssertMsg2);
|
---|
214 | if (cch < sizeof(g_szRTAssertMsg2) - 4)
|
---|
215 | {
|
---|
216 | va_copy(vaCopy, va);
|
---|
217 | cch += RTStrPrintfV(&g_szRTAssertMsg2[cch], sizeof(g_szRTAssertMsg2) - cch, pszFormat, vaCopy);
|
---|
218 | ASMAtomicWriteU32(&g_cchRTAssertMsg2, (uint32_t)cch);
|
---|
219 | va_end(vaCopy);
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * If not quiet, make some noise.
|
---|
225 | */
|
---|
226 | if (!RTAssertAreQuiet())
|
---|
227 | {
|
---|
228 | #ifdef IN_RING0
|
---|
229 | # ifdef IN_GUEST_R0
|
---|
230 | va_copy(vaCopy, va);
|
---|
231 | RTLogBackdoorPrintfV(pszFormat, vaCopy);
|
---|
232 | va_end(vaCopy);
|
---|
233 | # endif
|
---|
234 | /** @todo fully integrate this with the logger... play safe a bit for now. */
|
---|
235 | rtR0AssertNativeMsg2V(fInitial, pszFormat, va);
|
---|
236 |
|
---|
237 | #else /* !IN_RING0 */
|
---|
238 | # if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
239 | va_copy(vaCopy, va);
|
---|
240 | RTLogComPrintfV(pszFormat, vaCopy);
|
---|
241 | va_end(vaCopy);
|
---|
242 | # endif
|
---|
243 |
|
---|
244 | PRTLOGGER pLog = RTLogRelDefaultInstance();
|
---|
245 | if (pLog)
|
---|
246 | {
|
---|
247 | va_copy(vaCopy, va);
|
---|
248 | RTLogRelPrintfV(pszFormat, vaCopy);
|
---|
249 | va_end(vaCopy);
|
---|
250 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
251 | RTLogFlush(pLog);
|
---|
252 | # endif
|
---|
253 | }
|
---|
254 |
|
---|
255 | pLog = RTLogDefaultInstance();
|
---|
256 | if (pLog)
|
---|
257 | {
|
---|
258 | va_copy(vaCopy, va);
|
---|
259 | RTLogPrintfV(pszFormat, vaCopy);
|
---|
260 | va_end(vaCopy);
|
---|
261 | # ifndef IN_RC /* flushing is done automatically in RC */
|
---|
262 | RTLogFlush(pLog);
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 |
|
---|
266 | # ifdef IN_RING3
|
---|
267 | /* print to stderr, helps user and gdb debugging. */
|
---|
268 | char szMsg[1024];
|
---|
269 | va_copy(vaCopy, va);
|
---|
270 | RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, vaCopy);
|
---|
271 | va_end(vaCopy);
|
---|
272 | fprintf(stderr, "%s", szMsg);
|
---|
273 | fflush(stderr);
|
---|
274 | # endif
|
---|
275 | #endif /* !IN_RING0 */
|
---|
276 | }
|
---|
277 |
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | RTDECL(void) RTAssertMsg2V(const char *pszFormat, va_list va)
|
---|
282 | {
|
---|
283 | rtAssertMsg2Worker(true /*fInitial*/, pszFormat, va);
|
---|
284 | }
|
---|
285 | RT_EXPORT_SYMBOL(RTAssertMsg2V);
|
---|
286 |
|
---|
287 |
|
---|
288 | RTDECL(void) RTAssertMsg2AddV(const char *pszFormat, va_list va)
|
---|
289 | {
|
---|
290 | rtAssertMsg2Worker(false /*fInitial*/, pszFormat, va);
|
---|
291 | }
|
---|
292 | RT_EXPORT_SYMBOL(RTAssertMsg2AddV);
|
---|
293 |
|
---|