VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/assert.cpp@ 37596

Last change on this file since 37596 was 37233, checked in by vboxsync, 13 years ago

IRPT: Adding RTErrVarsSave/Restore/++ to preserve errno, h_error/WSAGetLastError and GetLastError accross assertions.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette