VirtualBox

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

Last change on this file since 73895 was 73762, checked in by vboxsync, 6 years ago

IPRT/assert: Provide call stack dump on 64-bit windows (ring-3). Experimental.

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