VirtualBox

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

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

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/* $Id: assert.cpp 21337 2009-07-07 14:58:27Z 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 "internal/iprt.h"
37
38#include <iprt/log.h>
39#include <iprt/string.h>
40#include <iprt/stdarg.h>
41#ifdef IN_RING3
42# include <stdio.h>
43#endif
44
45
46#if defined(IN_GUEST_R0) \
47 && ( (defined(RT_OS_LINUX) && !defined(VBOX_WITH_COMMON_VBOXGUEST_ON_LINUX)) \
48 || defined(RT_OS_WINDOWS))
49/*
50 * This is legacy that should be eliminated. OS specific code deals with
51 * R0 assertions now and it will do the backdoor printfs in addition to
52 * proper OS specific printfs and panics / BSODs / IPEs.
53 */
54#include <VBox/log.h>
55
56
57/**
58 * The 1st part of an assert message.
59 *
60 * @param pszExpr Expression. Can be NULL.
61 * @param uLine Location line number.
62 * @param pszFile Location file name.
63 * @param pszFunction Location function name.
64 * @remark This API exists in HC Ring-3 and GC.
65 */
66RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
67{
68 RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
69 "Expression: %s\n"
70 "Location : %s(%d) %s\n",
71 pszExpr, pszFile, uLine, pszFunction);
72}
73RT_EXPORT_SYMBOL(AssertMsg1);
74
75
76/**
77 * The 2nd (optional) part of an assert message.
78 *
79 * @param pszFormat Printf like format string.
80 * @param ... Arguments to that string.
81 * @remark This API exists in HC Ring-3 and GC.
82 */
83RTDECL(void) AssertMsg2(const char *pszFormat, ...)
84{ /* forwarder. */
85 va_list args;
86 va_start(args, pszFormat);
87 RTLogBackdoorPrintfV(pszFormat, args);
88 va_end(args);
89}
90RT_EXPORT_SYMBOL(AssertMsg2);
91
92#elif defined(IN_RING0)
93
94/* OS specific. */
95
96#else /* !IN_RING0 */
97
98
99/** The last assert message, 1st part. */
100RTDATADECL(char) g_szRTAssertMsg1[1024];
101/** The last assert message, 2nd part. */
102RTDATADECL(char) g_szRTAssertMsg2[2048];
103
104/**
105 * The 1st part of an assert message.
106 *
107 * @param pszExpr Expression. Can be NULL.
108 * @param uLine Location line number.
109 * @param pszFile Location file name.
110 * @param pszFunction Location function name.
111 * @remark This API exists in HC Ring-3 and GC.
112 */
113RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
114{
115#if !defined(IN_RING3) && !defined(LOG_NO_COM)
116 RTLogComPrintf("\n!!Assertion Failed!!\n"
117 "Expression: %s\n"
118 "Location : %s(%d) %s\n",
119 pszExpr, pszFile, uLine, pszFunction);
120#endif
121
122 PRTLOGGER pLog = RTLogRelDefaultInstance();
123 if (pLog)
124 {
125 RTLogRelPrintf("\n!!Assertion Failed!!\n"
126 "Expression: %s\n"
127 "Location : %s(%d) %s\n",
128 pszExpr, pszFile, uLine, pszFunction);
129#ifndef IN_RC /* flushing is done automatically in RC */
130 RTLogFlush(pLog);
131#endif
132 }
133
134#ifndef LOG_ENABLED
135 if (!pLog)
136#endif
137 {
138 pLog = RTLogDefaultInstance();
139 if (pLog)
140 {
141 RTLogPrintf("\n!!Assertion Failed!!\n"
142 "Expression: %s\n"
143 "Location : %s(%d) %s\n",
144 pszExpr, pszFile, uLine, pszFunction);
145#ifndef IN_RC /* flushing is done automatically in RC */
146 RTLogFlush(pLog);
147#endif
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#ifndef IN_RC /* flushing is done automatically in RC */
196 RTLogFlush(pLog);
197#endif
198 }
199
200 pLog = RTLogDefaultInstance();
201 if (pLog)
202 {
203 va_start(args, pszFormat);
204 RTLogPrintfV(pszFormat, args);
205 va_end(args);
206#ifndef IN_RC /* flushing is done automatically in RC */
207 RTLogFlush(pLog);
208#endif
209 }
210
211#ifdef IN_RING3
212 /* print to stderr, helps user and gdb debugging. */
213 char szMsg[1024];
214 va_start(args, pszFormat);
215 RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, args);
216 va_end(args);
217 fprintf(stderr, "%s", szMsg);
218 fflush(stderr);
219#endif
220
221 va_start(args, pszFormat);
222 RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, args);
223 va_end(args);
224}
225
226#endif /* !IN_RING0 */
227
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