1 | /* $Id: assert.cpp 938 2007-02-15 21:00:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Assertion Workers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/log.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/stdarg.h>
|
---|
30 | #ifdef IN_RING3
|
---|
31 | # include <stdio.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 |
|
---|
35 | #ifdef IN_GUEST_R0
|
---|
36 | #include <VBox/log.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * The 1st part of an assert message.
|
---|
41 | *
|
---|
42 | * @param pszExpr Expression. Can be NULL.
|
---|
43 | * @param uLine Location line number.
|
---|
44 | * @param pszFile Location file name.
|
---|
45 | * @param pszFunction Location function name.
|
---|
46 | * @remark This API exists in HC Ring-3 and GC.
|
---|
47 | */
|
---|
48 | RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
49 | {
|
---|
50 | RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
|
---|
51 | "Expression: %s\n"
|
---|
52 | "Location : %s(%d) %s\n",
|
---|
53 | pszExpr, pszFile, uLine, pszFunction);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * The 2nd (optional) part of an assert message.
|
---|
59 | *
|
---|
60 | * @param pszFormat Printf like format string.
|
---|
61 | * @param ... Arguments to that string.
|
---|
62 | * @remark This API exists in HC Ring-3 and GC.
|
---|
63 | */
|
---|
64 | RTDECL(void) AssertMsg2(const char *pszFormat, ...)
|
---|
65 | { /* forwarder. */
|
---|
66 | va_list args;
|
---|
67 | va_start(args, pszFormat);
|
---|
68 | RTLogBackdoorPrintfV(pszFormat, args);
|
---|
69 | va_end(args);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | #elif defined(IN_RING0)
|
---|
74 |
|
---|
75 |
|
---|
76 | #if 0 /* this code is totally unused */
|
---|
77 |
|
---|
78 | #include <VBox/sup.h>
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * The 1st part of an assert message.
|
---|
83 | *
|
---|
84 | * @param pszExpr Expression. Can be NULL.
|
---|
85 | * @param uLine Location line number.
|
---|
86 | * @param pszFile Location file name.
|
---|
87 | * @param pszFunction Location function name.
|
---|
88 | * @remark This API exists in HC Ring-3 and GC.
|
---|
89 | */
|
---|
90 | RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
91 | {
|
---|
92 | SUPR0Printf("\n!!Assertion Failed!!\n"
|
---|
93 | "Expression: %s\n"
|
---|
94 | "Location : %s(%d) %s\n",
|
---|
95 | pszExpr, pszFile, uLine, pszFunction);
|
---|
96 | #if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
97 | RTLogComPrintf("\n!!Assertion Failed!!\n"
|
---|
98 | "Expression: %s\n"
|
---|
99 | "Location : %s(%d) %s\n",
|
---|
100 | pszExpr, pszFile, uLine, pszFunction);
|
---|
101 | #endif
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The 2nd (optional) part of an assert message.
|
---|
107 | *
|
---|
108 | * @param pszFormat Printf like format string.
|
---|
109 | * @param ... Arguments to that string.
|
---|
110 | * @remark This API exists in HC Ring-3 and GC.
|
---|
111 | */
|
---|
112 | #ifdef __GNUC__
|
---|
113 | /* asm (".globl AssertMsg2; AssertMsg2: jmp *SUPR0Printf"); - DEADLY! */
|
---|
114 | #else
|
---|
115 | __declspec(naked) void AssertMsg2(const char *pszFormat, ...)
|
---|
116 | { /* forwarder. */
|
---|
117 | __asm jmp dword ptr [SUPR0Printf];
|
---|
118 | }
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | #endif /* dead code */
|
---|
122 |
|
---|
123 | #else /* !IN_RING0 */
|
---|
124 |
|
---|
125 |
|
---|
126 | /** The last assert message, 1st part. */
|
---|
127 | RTDATADECL(char) g_szRTAssertMsg1[1024];
|
---|
128 | /** The last assert message, 2nd part. */
|
---|
129 | RTDATADECL(char) g_szRTAssertMsg2[2048];
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * The 1st part of an assert message.
|
---|
133 | *
|
---|
134 | * @param pszExpr Expression. Can be NULL.
|
---|
135 | * @param uLine Location line number.
|
---|
136 | * @param pszFile Location file name.
|
---|
137 | * @param pszFunction Location function name.
|
---|
138 | * @remark This API exists in HC Ring-3 and GC.
|
---|
139 | */
|
---|
140 | RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
141 | {
|
---|
142 | #if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
143 | RTLogComPrintf("\n!!Assertion Failed!!\n"
|
---|
144 | "Expression: %s\n"
|
---|
145 | "Location : %s(%d) %s\n",
|
---|
146 | pszExpr, pszFile, uLine, pszFunction);
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | PRTLOGGER pLog = RTLogRelDefaultInstance();
|
---|
150 | if (pLog)
|
---|
151 | {
|
---|
152 | RTLogRelPrintf("\n!!Assertion Failed!!\n"
|
---|
153 | "Expression: %s\n"
|
---|
154 | "Location : %s(%d) %s\n",
|
---|
155 | pszExpr, pszFile, uLine, pszFunction);
|
---|
156 | RTLogFlush(pLog);
|
---|
157 | }
|
---|
158 |
|
---|
159 | pLog = RTLogDefaultInstance();
|
---|
160 | if (pLog)
|
---|
161 | {
|
---|
162 | RTLogPrintf("\n!!Assertion Failed!!\n"
|
---|
163 | "Expression: %s\n"
|
---|
164 | "Location : %s(%d) %s\n",
|
---|
165 | pszExpr, pszFile, uLine, pszFunction);
|
---|
166 | RTLogFlush(pLog);
|
---|
167 | }
|
---|
168 |
|
---|
169 | #ifdef IN_RING3
|
---|
170 | /* print to stderr, helps user and gdb debugging. */
|
---|
171 | fprintf(stderr,
|
---|
172 | "\n!!Assertion Failed!!\n"
|
---|
173 | "Expression: %s\n"
|
---|
174 | "Location : %s(%d) %s\n",
|
---|
175 | pszExpr, pszFile, uLine, pszFunction);
|
---|
176 | fflush(stderr);
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
|
---|
180 | "\n!!Assertion Failed!!\n"
|
---|
181 | "Expression: %s\n"
|
---|
182 | "Location : %s(%d) %s\n",
|
---|
183 | pszExpr, pszFile, uLine, pszFunction);
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * The 2nd (optional) part of an assert message.
|
---|
189 | *
|
---|
190 | * @param pszFormat Printf like format string.
|
---|
191 | * @param ... Arguments to that string.
|
---|
192 | * @remark This API exists in HC Ring-3 and GC.
|
---|
193 | */
|
---|
194 | RTDECL(void) AssertMsg2(const char *pszFormat, ...)
|
---|
195 | {
|
---|
196 | va_list args;
|
---|
197 |
|
---|
198 | #if !defined(IN_RING3) && !defined(LOG_NO_COM)
|
---|
199 | va_start(args, pszFormat);
|
---|
200 | RTLogComPrintfV(pszFormat, args);
|
---|
201 | va_end(args);
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | PRTLOGGER pLog = RTLogRelDefaultInstance();
|
---|
205 | if (pLog)
|
---|
206 | {
|
---|
207 | va_start(args, pszFormat);
|
---|
208 | RTLogRelPrintfV(pszFormat, args);
|
---|
209 | va_end(args);
|
---|
210 | RTLogFlush(pLog);
|
---|
211 | }
|
---|
212 |
|
---|
213 | pLog = RTLogDefaultInstance();
|
---|
214 | if (pLog)
|
---|
215 | {
|
---|
216 | va_start(args, pszFormat);
|
---|
217 | RTLogPrintfV(pszFormat, args);
|
---|
218 | va_end(args);
|
---|
219 | RTLogFlush(pLog);
|
---|
220 | }
|
---|
221 |
|
---|
222 | #ifdef IN_RING3
|
---|
223 | /* print to stderr, helps user and gdb debugging. */
|
---|
224 | char szMsg[1024];
|
---|
225 | va_start(args, pszFormat);
|
---|
226 | RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, args);
|
---|
227 | va_end(args);
|
---|
228 | fprintf(stderr, "%s", szMsg);
|
---|
229 | fflush(stderr);
|
---|
230 | #endif
|
---|
231 |
|
---|
232 | va_start(args, pszFormat);
|
---|
233 | RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, args);
|
---|
234 | va_end(args);
|
---|
235 | }
|
---|
236 |
|
---|
237 | #endif /* !IN_RING0 */
|
---|
238 |
|
---|