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