VirtualBox

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

Last change on this file since 58090 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.6 KB
Line 
1/* $Id: assert.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Assertions, common code.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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# if 0 /* Enable this iff you have a COM port and really want this debug info. */
141 RTLogComPrintf("\n!!Assertion Failed!!\n"
142 "Expression: %s\n"
143 "Location : %s(%d) %s\n",
144 pszExpr, pszFile, uLine, pszFunction);
145# endif
146# endif
147
148 PRTLOGGER pLog = RTLogRelGetDefaultInstance();
149 if (pLog)
150 {
151 RTLogRelPrintf("\n!!Assertion Failed!!\n"
152 "Expression: %s\n"
153 "Location : %s(%d) %s\n",
154 pszExpr, pszFile, uLine, pszFunction);
155# ifndef IN_RC /* flushing is done automatically in RC */
156 RTLogFlush(pLog);
157# endif
158 }
159
160# ifndef LOG_ENABLED
161 if (!pLog)
162# endif
163 {
164 pLog = RTLogDefaultInstance();
165 if (pLog)
166 {
167 RTLogPrintf("\n!!Assertion Failed!!\n"
168 "Expression: %s\n"
169 "Location : %s(%d) %s\n",
170 pszExpr, pszFile, uLine, pszFunction);
171# ifndef IN_RC /* flushing is done automatically in RC */
172 RTLogFlush(pLog);
173# endif
174 }
175 }
176
177# ifdef IN_RING3
178 /* print to stderr, helps user and gdb debugging. */
179 fprintf(stderr,
180 "\n!!Assertion Failed!!\n"
181 "Expression: %s\n"
182 "Location : %s(%d) %s\n",
183 VALID_PTR(pszExpr) ? pszExpr : "<none>",
184 VALID_PTR(pszFile) ? pszFile : "<none>",
185 uLine,
186 VALID_PTR(pszFunction) ? pszFunction : "");
187 fflush(stderr);
188# endif
189#endif /* !IN_RING0 */
190
191 RTErrVarsRestore(&SavedErrVars);
192 }
193}
194RT_EXPORT_SYMBOL(RTAssertMsg1);
195
196
197/**
198 * Worker for RTAssertMsg2V and RTAssertMsg2AddV
199 *
200 * @param fInitial True if it's RTAssertMsg2V, otherwise false.
201 * @param pszFormat The message format string.
202 * @param va The format arguments.
203 */
204static void rtAssertMsg2Worker(bool fInitial, const char *pszFormat, va_list va)
205{
206 va_list vaCopy;
207 size_t cch;
208
209 /*
210 * The global first.
211 */
212 if (fInitial)
213 {
214 va_copy(vaCopy, va);
215 cch = RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, vaCopy);
216 ASMAtomicWriteU32(&g_cchRTAssertMsg2, (uint32_t)cch);
217 va_end(vaCopy);
218 }
219 else
220 {
221 cch = ASMAtomicReadU32(&g_cchRTAssertMsg2);
222 if (cch < sizeof(g_szRTAssertMsg2) - 4)
223 {
224 va_copy(vaCopy, va);
225 cch += RTStrPrintfV(&g_szRTAssertMsg2[cch], sizeof(g_szRTAssertMsg2) - cch, pszFormat, vaCopy);
226 ASMAtomicWriteU32(&g_cchRTAssertMsg2, (uint32_t)cch);
227 va_end(vaCopy);
228 }
229 }
230
231 /*
232 * If not quiet, make some noise.
233 */
234 if (!RTAssertAreQuiet())
235 {
236 RTERRVARS SavedErrVars;
237 RTErrVarsSave(&SavedErrVars);
238
239#ifdef IN_RING0
240# ifdef IN_GUEST_R0
241 va_copy(vaCopy, va);
242 RTLogBackdoorPrintfV(pszFormat, vaCopy);
243 va_end(vaCopy);
244# endif
245 /** @todo fully integrate this with the logger... play safe a bit for now. */
246 rtR0AssertNativeMsg2V(fInitial, pszFormat, va);
247
248#else /* !IN_RING0 */
249# if !defined(IN_RING3) && !defined(LOG_NO_COM)
250# if 0 /* Enable this iff you have a COM port and really want this debug info. */
251 va_copy(vaCopy, va);
252 RTLogComPrintfV(pszFormat, vaCopy);
253 va_end(vaCopy);
254# endif
255# endif
256
257 PRTLOGGER pLog = RTLogRelGetDefaultInstance();
258 if (pLog)
259 {
260 va_copy(vaCopy, va);
261 RTLogRelPrintfV(pszFormat, vaCopy);
262 va_end(vaCopy);
263# ifndef IN_RC /* flushing is done automatically in RC */
264 RTLogFlush(pLog);
265# endif
266 }
267
268 pLog = RTLogDefaultInstance();
269 if (pLog)
270 {
271 va_copy(vaCopy, va);
272 RTLogPrintfV(pszFormat, vaCopy);
273 va_end(vaCopy);
274# ifndef IN_RC /* flushing is done automatically in RC */
275 RTLogFlush(pLog);
276#endif
277 }
278
279# ifdef IN_RING3
280 /* print to stderr, helps user and gdb debugging. */
281 char szMsg[sizeof(g_szRTAssertMsg2)];
282 va_copy(vaCopy, va);
283 RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, vaCopy);
284 va_end(vaCopy);
285 fprintf(stderr, "%s", szMsg);
286 fflush(stderr);
287# endif
288#endif /* !IN_RING0 */
289
290 RTErrVarsRestore(&SavedErrVars);
291 }
292}
293
294
295RTDECL(void) RTAssertMsg2V(const char *pszFormat, va_list va)
296{
297 rtAssertMsg2Worker(true /*fInitial*/, pszFormat, va);
298}
299RT_EXPORT_SYMBOL(RTAssertMsg2V);
300
301
302RTDECL(void) RTAssertMsg2AddV(const char *pszFormat, va_list va)
303{
304 rtAssertMsg2Worker(false /*fInitial*/, pszFormat, va);
305}
306RT_EXPORT_SYMBOL(RTAssertMsg2AddV);
307
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