VirtualBox

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

Last change on this file since 17348 was 17348, checked in by vboxsync, 16 years ago

IPRT/assert: don't create a debug log on assert if we've got a release log and logging isn't enabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: assert.cpp 17348 2009-03-04 15:34:40Z 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.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 * 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 <iprt/log.h>
37#include <iprt/string.h>
38#include <iprt/stdarg.h>
39#ifdef IN_RING3
40# include <stdio.h>
41#endif
42
43
44#if defined(IN_GUEST_R0) && (defined(RT_OS_LINUX) || defined(RT_OS_WINDOWS))
45/*
46 * This is legacy that should be eliminated. OS specific code deals with
47 * R0 assertions now and it will do the backdoor printfs in addition to
48 * proper OS specific printfs and panics / BSODs / IPEs.
49 */
50#include <VBox/log.h>
51
52
53/**
54 * The 1st part of an assert message.
55 *
56 * @param pszExpr Expression. Can be NULL.
57 * @param uLine Location line number.
58 * @param pszFile Location file name.
59 * @param pszFunction Location function name.
60 * @remark This API exists in HC Ring-3 and GC.
61 */
62RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
63{
64 RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
65 "Expression: %s\n"
66 "Location : %s(%d) %s\n",
67 pszExpr, pszFile, uLine, pszFunction);
68}
69
70
71/**
72 * The 2nd (optional) part of an assert message.
73 *
74 * @param pszFormat Printf like format string.
75 * @param ... Arguments to that string.
76 * @remark This API exists in HC Ring-3 and GC.
77 */
78RTDECL(void) AssertMsg2(const char *pszFormat, ...)
79{ /* forwarder. */
80 va_list args;
81 va_start(args, pszFormat);
82 RTLogBackdoorPrintfV(pszFormat, args);
83 va_end(args);
84}
85
86# if defined(RT_OS_LINUX) && defined(IN_MODULE)
87/*
88 * When we build this in the Linux kernel module, we wish to make the
89 * symbols available to other modules as well.
90 */
91# include "the-linux-kernel.h"
92EXPORT_SYMBOL(AssertMsg1);
93EXPORT_SYMBOL(AssertMsg2);
94# endif
95
96#elif defined(IN_RING0)
97
98/* OS specific. */
99
100#else /* !IN_RING0 */
101
102
103/** The last assert message, 1st part. */
104RTDATADECL(char) g_szRTAssertMsg1[1024];
105/** The last assert message, 2nd part. */
106RTDATADECL(char) g_szRTAssertMsg2[2048];
107
108/**
109 * The 1st part of an assert message.
110 *
111 * @param pszExpr Expression. Can be NULL.
112 * @param uLine Location line number.
113 * @param pszFile Location file name.
114 * @param pszFunction Location function name.
115 * @remark This API exists in HC Ring-3 and GC.
116 */
117RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
118{
119#if !defined(IN_RING3) && !defined(LOG_NO_COM)
120 RTLogComPrintf("\n!!Assertion Failed!!\n"
121 "Expression: %s\n"
122 "Location : %s(%d) %s\n",
123 pszExpr, pszFile, uLine, pszFunction);
124#endif
125
126 PRTLOGGER pLog = RTLogRelDefaultInstance();
127 if (pLog)
128 {
129 RTLogRelPrintf("\n!!Assertion Failed!!\n"
130 "Expression: %s\n"
131 "Location : %s(%d) %s\n",
132 pszExpr, pszFile, uLine, pszFunction);
133 RTLogFlush(pLog);
134 }
135
136#ifndef LOG_ENABLED
137 if (!pLog)
138#endif
139 {
140 pLog = RTLogDefaultInstance();
141 if (pLog)
142 {
143 RTLogPrintf("\n!!Assertion Failed!!\n"
144 "Expression: %s\n"
145 "Location : %s(%d) %s\n",
146 pszExpr, pszFile, uLine, pszFunction);
147 RTLogFlush(pLog);
148 }
149 }
150
151#ifdef IN_RING3
152 /* print to stderr, helps user and gdb debugging. */
153 fprintf(stderr,
154 "\n!!Assertion Failed!!\n"
155 "Expression: %s\n"
156 "Location : %s(%d) %s\n",
157 VALID_PTR(pszExpr) ? pszExpr : "<none>",
158 VALID_PTR(pszFile) ? pszFile : "<none>",
159 uLine,
160 VALID_PTR(pszFunction) ? pszFunction : "");
161 fflush(stderr);
162#endif
163
164 RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
165 "\n!!Assertion Failed!!\n"
166 "Expression: %s\n"
167 "Location : %s(%d) %s\n",
168 pszExpr, pszFile, uLine, pszFunction);
169}
170
171
172/**
173 * The 2nd (optional) part of an assert message.
174 *
175 * @param pszFormat Printf like format string.
176 * @param ... Arguments to that string.
177 * @remark This API exists in HC Ring-3 and GC.
178 */
179RTDECL(void) AssertMsg2(const char *pszFormat, ...)
180{
181 va_list args;
182
183#if !defined(IN_RING3) && !defined(LOG_NO_COM)
184 va_start(args, pszFormat);
185 RTLogComPrintfV(pszFormat, args);
186 va_end(args);
187#endif
188
189 PRTLOGGER pLog = RTLogRelDefaultInstance();
190 if (pLog)
191 {
192 va_start(args, pszFormat);
193 RTLogRelPrintfV(pszFormat, args);
194 va_end(args);
195 RTLogFlush(pLog);
196 }
197
198 pLog = RTLogDefaultInstance();
199 if (pLog)
200 {
201 va_start(args, pszFormat);
202 RTLogPrintfV(pszFormat, args);
203 va_end(args);
204 RTLogFlush(pLog);
205 }
206
207#ifdef IN_RING3
208 /* print to stderr, helps user and gdb debugging. */
209 char szMsg[1024];
210 va_start(args, pszFormat);
211 RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, args);
212 va_end(args);
213 fprintf(stderr, "%s", szMsg);
214 fflush(stderr);
215#endif
216
217 va_start(args, pszFormat);
218 RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, args);
219 va_end(args);
220}
221
222#endif /* !IN_RING0 */
223
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