VirtualBox

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

Last change on this file since 25518 was 25518, checked in by vboxsync, 15 years ago

iprt/assert.h: Added a quiet and maypanic setting so it's possible to do negative testing without a debugger.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1/* $Id: assert.cpp 25518 2009-12-20 16:40:37Z vboxsync $ */
2/** @file
3 * IPRT - Assertion Workers.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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
46
47/*******************************************************************************
48* Global Variables *
49*******************************************************************************/
50/** Set if assertions are quiet. */
51static bool volatile g_fQuiet = false;
52/** Set if assertions may panic. */
53static bool volatile g_fMayPanic = true;
54
55
56RTDECL(bool) RTAssertSetQuiet(bool fQuiet)
57{
58 return ASMAtomicXchgBool(&g_fQuiet, fQuiet);
59}
60
61
62RTDECL(bool) RTAssertAreQuiet(void)
63{
64 return ASMAtomicUoReadBool(&g_fQuiet);
65}
66
67
68RTDECL(bool) RTAssertSetMayPanic(bool fMayPanic)
69{
70 return ASMAtomicXchgBool(&g_fMayPanic, fMayPanic);
71}
72
73
74RTDECL(bool) RTAssertMayPanic(void)
75{
76 return ASMAtomicUoReadBool(&g_fMayPanic);
77}
78
79
80#if defined(IN_GUEST_R0) && defined(RT_OS_WINDOWS) /** @todo remove this, see defect XYZ. */
81/*
82 * This is legacy that should be eliminated. OS specific code deals with
83 * R0 assertions now and it will do the backdoor printfs in addition to
84 * proper OS specific printfs and panics / BSODs / IPEs.
85 */
86#include <VBox/log.h>
87
88
89/**
90 * The 1st part of an assert message.
91 *
92 * @param pszExpr Expression. Can be NULL.
93 * @param uLine Location line number.
94 * @param pszFile Location file name.
95 * @param pszFunction Location function name.
96 * @remark This API exists in HC Ring-3 and GC.
97 */
98RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
99{
100 RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
101 "Expression: %s\n"
102 "Location : %s(%d) %s\n",
103 pszExpr, pszFile, uLine, pszFunction);
104}
105RT_EXPORT_SYMBOL(AssertMsg1);
106
107
108/**
109 * The 2nd (optional) part of an assert message.
110 *
111 * @param pszFormat Printf like format string.
112 * @param ... Arguments to that string.
113 * @remark This API exists in HC Ring-3 and GC.
114 */
115RTDECL(void) AssertMsg2(const char *pszFormat, ...)
116{ /* forwarder. */
117 va_list args;
118 va_start(args, pszFormat);
119 RTLogBackdoorPrintfV(pszFormat, args);
120 va_end(args);
121}
122RT_EXPORT_SYMBOL(AssertMsg2);
123
124#elif defined(IN_RING0)
125
126/* OS specific. */
127
128#else /* !IN_RING0 */
129
130
131/** The last assert message, 1st part. */
132RTDATADECL(char) g_szRTAssertMsg1[1024];
133/** The last assert message, 2nd part. */
134RTDATADECL(char) g_szRTAssertMsg2[2048];
135
136/**
137 * The 1st part of an assert message.
138 *
139 * @param pszExpr Expression. Can be NULL.
140 * @param uLine Location line number.
141 * @param pszFile Location file name.
142 * @param pszFunction Location function name.
143 * @remark This API exists in HC Ring-3 and GC.
144 */
145RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
146{
147 if (!RTAssertAreQuiet())
148 {
149#if !defined(IN_RING3) && !defined(LOG_NO_COM)
150 RTLogComPrintf("\n!!Assertion Failed!!\n"
151 "Expression: %s\n"
152 "Location : %s(%d) %s\n",
153 pszExpr, pszFile, uLine, pszFunction);
154#endif
155
156 PRTLOGGER pLog = RTLogRelDefaultInstance();
157 if (pLog)
158 {
159 RTLogRelPrintf("\n!!Assertion Failed!!\n"
160 "Expression: %s\n"
161 "Location : %s(%d) %s\n",
162 pszExpr, pszFile, uLine, pszFunction);
163#ifndef IN_RC /* flushing is done automatically in RC */
164 RTLogFlush(pLog);
165#endif
166 }
167
168#ifndef LOG_ENABLED
169 if (!pLog)
170#endif
171 {
172 pLog = RTLogDefaultInstance();
173 if (pLog)
174 {
175 RTLogPrintf("\n!!Assertion Failed!!\n"
176 "Expression: %s\n"
177 "Location : %s(%d) %s\n",
178 pszExpr, pszFile, uLine, pszFunction);
179#ifndef IN_RC /* flushing is done automatically in RC */
180 RTLogFlush(pLog);
181#endif
182 }
183 }
184
185#ifdef IN_RING3
186 /* print to stderr, helps user and gdb debugging. */
187 fprintf(stderr,
188 "\n!!Assertion Failed!!\n"
189 "Expression: %s\n"
190 "Location : %s(%d) %s\n",
191 VALID_PTR(pszExpr) ? pszExpr : "<none>",
192 VALID_PTR(pszFile) ? pszFile : "<none>",
193 uLine,
194 VALID_PTR(pszFunction) ? pszFunction : "");
195 fflush(stderr);
196#endif
197 }
198
199 RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
200 "\n!!Assertion Failed!!\n"
201 "Expression: %s\n"
202 "Location : %s(%d) %s\n",
203 pszExpr, pszFile, uLine, pszFunction);
204}
205
206
207/**
208 * The 2nd (optional) part of an assert message.
209 *
210 * @param pszFormat Printf like format string.
211 * @param ... Arguments to that string.
212 * @remark This API exists in HC Ring-3 and GC.
213 */
214RTDECL(void) AssertMsg2(const char *pszFormat, ...)
215{
216 va_list args;
217
218 if (!RTAssertAreQuiet())
219 {
220#if !defined(IN_RING3) && !defined(LOG_NO_COM)
221 va_start(args, pszFormat);
222 RTLogComPrintfV(pszFormat, args);
223 va_end(args);
224#endif
225
226 PRTLOGGER pLog = RTLogRelDefaultInstance();
227 if (pLog)
228 {
229 va_start(args, pszFormat);
230 RTLogRelPrintfV(pszFormat, args);
231 va_end(args);
232#ifndef IN_RC /* flushing is done automatically in RC */
233 RTLogFlush(pLog);
234#endif
235 }
236
237 pLog = RTLogDefaultInstance();
238 if (pLog)
239 {
240 va_start(args, pszFormat);
241 RTLogPrintfV(pszFormat, args);
242 va_end(args);
243#ifndef IN_RC /* flushing is done automatically in RC */
244 RTLogFlush(pLog);
245#endif
246 }
247
248#ifdef IN_RING3
249 /* print to stderr, helps user and gdb debugging. */
250 char szMsg[1024];
251 va_start(args, pszFormat);
252 RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, args);
253 va_end(args);
254 fprintf(stderr, "%s", szMsg);
255 fflush(stderr);
256#endif
257 }
258
259 va_start(args, pszFormat);
260 RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, args);
261 va_end(args);
262}
263
264#endif /* !IN_RING0 */
265
Note: See TracBrowser for help on using the repository browser.

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