VirtualBox

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

Last change on this file since 74978 was 74344, checked in by vboxsync, 6 years ago

IPRT/assert: Added check for env var IPRT_ASSERT_NO_STACK and IsDebuggerPresent() before dumping the stack.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.9 KB
Line 
1/* $Id: assert.cpp 74344 2018-09-18 14:24:24Z vboxsync $ */
2/** @file
3 * IPRT - Assertions, common code.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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#ifdef IPRT_WITH_ASSERT_STACK
36# ifndef IN_RING3
37# error "IPRT_WITH_ASSERT_STACK is only for ring-3 at present."
38# endif
39# include <iprt/dbg.h>
40#endif
41#include <iprt/err.h>
42#include <iprt/log.h>
43#include <iprt/string.h>
44#include <iprt/stdarg.h>
45#ifdef IN_RING3
46# include <iprt/env.h>
47# include <stdio.h>
48# ifdef RT_OS_WINDOWS
49# include <iprt/win/windows.h>
50# endif
51#endif
52#include "internal/assert.h"
53
54
55/*********************************************************************************************************************************
56* Global Variables *
57*********************************************************************************************************************************/
58/** The last assertion message, 1st part. */
59RTDATADECL(char) g_szRTAssertMsg1[1024];
60RT_EXPORT_SYMBOL(g_szRTAssertMsg1);
61/** The last assertion message, 2nd part. */
62RTDATADECL(char) g_szRTAssertMsg2[4096];
63RT_EXPORT_SYMBOL(g_szRTAssertMsg2);
64#ifdef IPRT_WITH_ASSERT_STACK
65/** The last assertion message, stack part. */
66RTDATADECL(char) g_szRTAssertStack[4096];
67RT_EXPORT_SYMBOL(g_szRTAssertStack);
68#endif
69/** The length of the g_szRTAssertMsg2 content.
70 * @remarks Race. */
71static uint32_t volatile g_cchRTAssertMsg2;
72/** The last assertion message, expression. */
73RTDATADECL(const char * volatile) g_pszRTAssertExpr;
74RT_EXPORT_SYMBOL(g_pszRTAssertExpr);
75/** The last assertion message, function name. */
76RTDATADECL(const char * volatile) g_pszRTAssertFunction;
77RT_EXPORT_SYMBOL(g_pszRTAssertFunction);
78/** The last assertion message, file name. */
79RTDATADECL(const char * volatile) g_pszRTAssertFile;
80RT_EXPORT_SYMBOL(g_pszRTAssertFile);
81/** The last assertion message, line number. */
82RTDATADECL(uint32_t volatile) g_u32RTAssertLine;
83RT_EXPORT_SYMBOL(g_u32RTAssertLine);
84
85
86/** Set if assertions are quiet. */
87static bool volatile g_fQuiet = false;
88/** Set if assertions may panic. */
89static bool volatile g_fMayPanic = true;
90
91
92RTDECL(bool) RTAssertSetQuiet(bool fQuiet)
93{
94 return ASMAtomicXchgBool(&g_fQuiet, fQuiet);
95}
96RT_EXPORT_SYMBOL(RTAssertSetQuiet);
97
98
99RTDECL(bool) RTAssertAreQuiet(void)
100{
101 return ASMAtomicUoReadBool(&g_fQuiet);
102}
103RT_EXPORT_SYMBOL(RTAssertAreQuiet);
104
105
106RTDECL(bool) RTAssertSetMayPanic(bool fMayPanic)
107{
108 return ASMAtomicXchgBool(&g_fMayPanic, fMayPanic);
109}
110RT_EXPORT_SYMBOL(RTAssertSetMayPanic);
111
112
113RTDECL(bool) RTAssertMayPanic(void)
114{
115 return ASMAtomicUoReadBool(&g_fMayPanic);
116}
117RT_EXPORT_SYMBOL(RTAssertMayPanic);
118
119
120RTDECL(void) RTAssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
121{
122 /*
123 * Fill in the globals.
124 */
125 ASMAtomicUoWritePtr(&g_pszRTAssertExpr, pszExpr);
126 ASMAtomicUoWritePtr(&g_pszRTAssertFile, pszFile);
127 ASMAtomicUoWritePtr(&g_pszRTAssertFunction, pszFunction);
128 ASMAtomicUoWriteU32(&g_u32RTAssertLine, uLine);
129 RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
130 "\n!!Assertion Failed!!\n"
131 "Expression: %s\n"
132 "Location : %s(%d) %s\n",
133 pszExpr, pszFile, uLine, pszFunction);
134
135 /*
136 * If not quiet, make noise.
137 */
138 if (!RTAssertAreQuiet())
139 {
140 RTERRVARS SavedErrVars;
141 RTErrVarsSave(&SavedErrVars);
142
143#ifdef IPRT_WITH_ASSERT_STACK
144 /* The stack dump. */
145 char szStack[sizeof(g_szRTAssertStack)];
146 size_t cchStack = 0;
147# if defined(IN_RING3) && defined(RT_OS_WINDOWS) /** @todo make this stack on/off thing more modular. */
148 bool fStack = !IsDebuggerPresent() && !RTEnvExist("IPRT_ASSERT_NO_STACK");
149# elif defined(IN_RING3)
150 bool fStack = !RTEnvExist("IPRT_ASSERT_NO_STACK");
151# else
152 bool fStack = true;
153# endif
154 if (fStack)
155 cchStack = RTDbgStackDumpSelf(szStack, sizeof(szStack), 0);
156 else
157 szStack[0] = '\0';
158 memcpy(g_szRTAssertStack, szStack, cchStack + 1);
159#endif
160
161#ifdef IN_RING0
162# ifdef IN_GUEST_R0
163 RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
164 "Expression: %s\n"
165 "Location : %s(%d) %s\n",
166 pszExpr, pszFile, uLine, pszFunction);
167# endif
168 /** @todo fully integrate this with the logger... play safe a bit for now. */
169 rtR0AssertNativeMsg1(pszExpr, uLine, pszFile, pszFunction);
170
171#else /* !IN_RING0 */
172# if !defined(IN_RING3) && !defined(LOG_NO_COM)
173# if 0 /* Enable this iff you have a COM port and really want this debug info. */
174 RTLogComPrintf("\n!!Assertion Failed!!\n"
175 "Expression: %s\n"
176 "Location : %s(%d) %s\n",
177 pszExpr, pszFile, uLine, pszFunction);
178# endif
179# endif
180
181 PRTLOGGER pLog = RTLogRelGetDefaultInstance();
182 if (pLog)
183 {
184 RTLogRelPrintf("\n!!Assertion Failed!!\n"
185 "Expression: %s\n"
186 "Location : %s(%d) %s\n",
187 pszExpr, pszFile, uLine, pszFunction);
188# ifdef IPRT_WITH_ASSERT_STACK
189 RTLogRelPrintf("Stack :\n%s\n", szStack);
190# endif
191# ifndef IN_RC /* flushing is done automatically in RC */
192 RTLogFlush(pLog);
193# endif
194 }
195
196# ifndef LOG_ENABLED
197 if (!pLog)
198# endif
199 {
200 pLog = RTLogDefaultInstance();
201 if (pLog)
202 {
203 RTLogPrintf("\n!!Assertion Failed!!\n"
204 "Expression: %s\n"
205 "Location : %s(%d) %s\n",
206 pszExpr, pszFile, uLine, pszFunction);
207# ifdef IPRT_WITH_ASSERT_STACK
208 RTLogPrintf("Stack :\n%s\n", szStack);
209# endif
210# ifndef IN_RC /* flushing is done automatically in RC */
211 RTLogFlush(pLog);
212# endif
213 }
214 }
215
216# ifdef IN_RING3
217 /* print to stderr, helps user and gdb debugging. */
218 fprintf(stderr,
219 "\n!!Assertion Failed!!\n"
220 "Expression: %s\n"
221 "Location : %s(%d) %s\n",
222 VALID_PTR(pszExpr) ? pszExpr : "<none>",
223 VALID_PTR(pszFile) ? pszFile : "<none>",
224 uLine,
225 VALID_PTR(pszFunction) ? pszFunction : "");
226# ifdef IPRT_WITH_ASSERT_STACK
227 fprintf(stderr, "Stack :\n%s\n", szStack);
228# endif
229 fflush(stderr);
230# endif
231#endif /* !IN_RING0 */
232
233 RTErrVarsRestore(&SavedErrVars);
234 }
235}
236RT_EXPORT_SYMBOL(RTAssertMsg1);
237
238
239/**
240 * Worker for RTAssertMsg2V and RTAssertMsg2AddV
241 *
242 * @param fInitial True if it's RTAssertMsg2V, otherwise false.
243 * @param pszFormat The message format string.
244 * @param va The format arguments.
245 */
246static void rtAssertMsg2Worker(bool fInitial, const char *pszFormat, va_list va)
247{
248 va_list vaCopy;
249 size_t cch;
250
251 /*
252 * The global first.
253 */
254 if (fInitial)
255 {
256 va_copy(vaCopy, va);
257 cch = RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, vaCopy);
258 ASMAtomicWriteU32(&g_cchRTAssertMsg2, (uint32_t)cch);
259 va_end(vaCopy);
260 }
261 else
262 {
263 cch = ASMAtomicReadU32(&g_cchRTAssertMsg2);
264 if (cch < sizeof(g_szRTAssertMsg2) - 4)
265 {
266 va_copy(vaCopy, va);
267 cch += RTStrPrintfV(&g_szRTAssertMsg2[cch], sizeof(g_szRTAssertMsg2) - cch, pszFormat, vaCopy);
268 ASMAtomicWriteU32(&g_cchRTAssertMsg2, (uint32_t)cch);
269 va_end(vaCopy);
270 }
271 }
272
273 /*
274 * If not quiet, make some noise.
275 */
276 if (!RTAssertAreQuiet())
277 {
278 RTERRVARS SavedErrVars;
279 RTErrVarsSave(&SavedErrVars);
280
281#ifdef IN_RING0
282# ifdef IN_GUEST_R0
283 va_copy(vaCopy, va);
284 RTLogBackdoorPrintfV(pszFormat, vaCopy);
285 va_end(vaCopy);
286# endif
287 /** @todo fully integrate this with the logger... play safe a bit for now. */
288 rtR0AssertNativeMsg2V(fInitial, pszFormat, va);
289
290#else /* !IN_RING0 */
291# if !defined(IN_RING3) && !defined(LOG_NO_COM)
292# if 0 /* Enable this iff you have a COM port and really want this debug info. */
293 va_copy(vaCopy, va);
294 RTLogComPrintfV(pszFormat, vaCopy);
295 va_end(vaCopy);
296# endif
297# endif
298
299 PRTLOGGER pLog = RTLogRelGetDefaultInstance();
300 if (pLog)
301 {
302 va_copy(vaCopy, va);
303 RTLogRelPrintfV(pszFormat, vaCopy);
304 va_end(vaCopy);
305# ifndef IN_RC /* flushing is done automatically in RC */
306 RTLogFlush(pLog);
307# endif
308 }
309
310 pLog = RTLogDefaultInstance();
311 if (pLog)
312 {
313 va_copy(vaCopy, va);
314 RTLogPrintfV(pszFormat, vaCopy);
315 va_end(vaCopy);
316# ifndef IN_RC /* flushing is done automatically in RC */
317 RTLogFlush(pLog);
318#endif
319 }
320
321# ifdef IN_RING3
322 /* print to stderr, helps user and gdb debugging. */
323 char szMsg[sizeof(g_szRTAssertMsg2)];
324 va_copy(vaCopy, va);
325 RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, vaCopy);
326 va_end(vaCopy);
327 fprintf(stderr, "%s", szMsg);
328 fflush(stderr);
329# endif
330#endif /* !IN_RING0 */
331
332 RTErrVarsRestore(&SavedErrVars);
333 }
334}
335
336
337RTDECL(void) RTAssertMsg2V(const char *pszFormat, va_list va)
338{
339 rtAssertMsg2Worker(true /*fInitial*/, pszFormat, va);
340}
341RT_EXPORT_SYMBOL(RTAssertMsg2V);
342
343
344RTDECL(void) RTAssertMsg2AddV(const char *pszFormat, va_list va)
345{
346 rtAssertMsg2Worker(false /*fInitial*/, pszFormat, va);
347}
348RT_EXPORT_SYMBOL(RTAssertMsg2AddV);
349
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