3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #if DBG && !defined(_M_ARM)
29 // #define DEBUG_INIFILE
30 // #define DEBUG_REACTOS
31 // #define DEBUG_CUSTOM
34 #define DBG_DEFAULT_LEVELS (ERR_LEVEL|FIXME_LEVEL)
45 #define BOCHS_OUTPUT_PORT 0xe9
47 static UCHAR DbgChannels
[DBG_CHANNELS_COUNT
];
49 ULONG DebugPort
= RS232
;
50 // ULONG DebugPort = SCREEN;
51 // ULONG DebugPort = BOCHS;
52 // ULONG DebugPort = SCREEN|BOCHS;
54 /* COM1 is the WinDbg port */
59 // ULONG BaudRate = 19200;
60 ULONG BaudRate
= 115200;
62 BOOLEAN DebugStartOfLine
= TRUE
;
66 #if defined (DEBUG_ALL)
67 memset(DbgChannels
, MAX_LEVEL
, DBG_CHANNELS_COUNT
);
68 #elif defined (DEBUG_WARN)
69 memset(DbgChannels
, WARN_LEVEL
|FIXME_LEVEL
|ERR_LEVEL
, DBG_CHANNELS_COUNT
);
70 #elif defined (DEBUG_ERR)
71 memset(DbgChannels
, ERR_LEVEL
, DBG_CHANNELS_COUNT
);
73 memset(DbgChannels
, 0, DBG_CHANNELS_COUNT
);
76 #if defined (DEBUG_INIFILE)
77 DbgChannels
[DPRINT_INIFILE
] = MAX_LEVEL
;
78 #elif defined (DEBUG_REACTOS)
79 DbgChannels
[DPRINT_REACTOS
] = MAX_LEVEL
;
80 DbgChannels
[DPRINT_REGISTRY
] = MAX_LEVEL
;
81 #elif defined (DEBUG_CUSTOM)
82 DbgChannels
[DPRINT_WARNING
] = MAX_LEVEL
;
83 DbgChannels
[DPRINT_WINDOWS
] = MAX_LEVEL
;
86 if (DebugPort
& RS232
)
88 Rs232PortInitialize(ComPort
, BaudRate
);
92 VOID
DebugPrintChar(UCHAR Character
)
94 if (Character
== '\n')
95 DebugStartOfLine
= TRUE
;
97 if (DebugPort
& RS232
)
99 if (Character
== '\n')
100 Rs232PortPutByte('\r');
102 Rs232PortPutByte(Character
);
104 if (DebugPort
& BOCHS
)
106 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_OUTPUT_PORT
, Character
);
108 if (DebugPort
& SCREEN
)
110 MachConsPutChar(Character
);
115 DbgPrint(const char *Format
, ...)
122 va_start(ap
, Format
);
123 Length
= _vsnprintf(Buffer
, sizeof(Buffer
), Format
, ap
);
126 /* Check if we went past the buffer */
129 /* Terminate it if we went over-board */
130 Buffer
[sizeof(Buffer
) - 1] = '\n';
133 Length
= sizeof(Buffer
);
136 for (i
= 0; i
< Length
; i
++)
138 DebugPrintChar(Buffer
[i
]);
145 DbgPrint2(ULONG Mask
, ULONG Level
, const char *File
, ULONG Line
, char *Format
, ...)
151 /* Mask out unwanted debug messages */
152 if (!(DbgChannels
[Mask
] & Level
) && !(Level
& DBG_DEFAULT_LEVELS
))
157 /* Print the header if we have started a new line */
158 if (DebugStartOfLine
)
160 DbgPrint("(%s:%lu) ", File
, Line
);
178 DebugStartOfLine
= FALSE
;
181 va_start(ap
, Format
);
182 vsprintf(Buffer
, Format
, ap
);
187 DebugPrintChar(*ptr
++);
192 DebugDumpBuffer(ULONG Mask
, PVOID Buffer
, ULONG Length
)
194 PUCHAR BufPtr
= (PUCHAR
)Buffer
;
198 /* Mask out unwanted debug messages */
199 if (!(DbgChannels
[Mask
] & TRACE_LEVEL
))
202 DebugStartOfLine
= FALSE
; // We don't want line headers
203 DbgPrint("Dumping buffer at %p with length of %lu bytes:\n", Buffer
, Length
);
205 for (Idx
=0; Idx
<Length
; )
207 DebugStartOfLine
= FALSE
; // We don't want line headers
210 DbgPrint("000%x:\t", Idx
);
211 else if (Idx
< 0x0100)
212 DbgPrint("00%x:\t", Idx
);
213 else if (Idx
< 0x1000)
214 DbgPrint("0%x:\t", Idx
);
216 DbgPrint("%x:\t", Idx
);
218 for (Idx2
=0; Idx2
<16; Idx2
++,Idx
++)
220 if (BufPtr
[Idx
] < 0x10)
224 DbgPrint("%x", BufPtr
[Idx
]);
239 for (Idx2
=0; Idx2
<16; Idx2
++,Idx
++)
241 if ((BufPtr
[Idx
] > 20) && (BufPtr
[Idx
] < 0x80))
243 DbgPrint("%c", BufPtr
[Idx
]);
256 DbgAddDebugChannel(CHAR
* channel
, CHAR
* level
, CHAR op
)
258 int iLevel
, iChannel
;
260 if (channel
== NULL
|| *channel
== L
'\0' || strlen(channel
) == 0 )
263 if (level
== NULL
|| *level
== L
'\0' || strlen(level
) == 0 )
265 else if (strcmp(level
, "err") == 0)
267 else if (strcmp(level
, "fixme") == 0)
268 iLevel
= FIXME_LEVEL
;
269 else if (strcmp(level
, "warn") == 0)
271 else if (strcmp(level
, "trace") == 0)
272 iLevel
= TRACE_LEVEL
;
276 if (strcmp(channel
, "memory" ) == 0) iChannel
= DPRINT_MEMORY
;
277 else if (strcmp(channel
, "filesystem") == 0) iChannel
= DPRINT_FILESYSTEM
;
278 else if (strcmp(channel
, "inifile" ) == 0) iChannel
= DPRINT_INIFILE
;
279 else if (strcmp(channel
, "ui" ) == 0) iChannel
= DPRINT_UI
;
280 else if (strcmp(channel
, "disk" ) == 0) iChannel
= DPRINT_DISK
;
281 else if (strcmp(channel
, "cache" ) == 0) iChannel
= DPRINT_CACHE
;
282 else if (strcmp(channel
, "registry" ) == 0) iChannel
= DPRINT_REGISTRY
;
283 else if (strcmp(channel
, "linux" ) == 0) iChannel
= DPRINT_LINUX
;
284 else if (strcmp(channel
, "hwdetect" ) == 0) iChannel
= DPRINT_HWDETECT
;
285 else if (strcmp(channel
, "windows" ) == 0) iChannel
= DPRINT_WINDOWS
;
286 else if (strcmp(channel
, "peloader" ) == 0) iChannel
= DPRINT_PELOADER
;
287 else if (strcmp(channel
, "scsiport" ) == 0) iChannel
= DPRINT_SCSIPORT
;
288 else if (strcmp(channel
, "heap" ) == 0) iChannel
= DPRINT_HEAP
;
289 else if (strcmp(channel
, "all" ) == 0)
293 for(i
= 0 ; i
< DBG_CHANNELS_COUNT
; i
++)
296 DbgChannels
[i
] |= iLevel
;
298 DbgChannels
[i
] &= ~iLevel
;
306 DbgChannels
[iChannel
] |= iLevel
;
308 DbgChannels
[iChannel
] &= ~iLevel
;
314 DbgParseDebugChannels(PCHAR Value
)
316 CHAR
*str
, *separator
, *c
, op
;
322 separator
= strchr(str
, ',');
323 if (separator
!= NULL
)
326 c
= strchr(str
, '+');
328 c
= strchr(str
, '-');
336 DbgAddDebugChannel(c
, str
, op
);
340 } while (separator
!= NULL
);
346 DbgPrint(PCCH Format
, ...)
354 MsgBoxPrint(const char *Format
, ...)
360 va_start(ap
, Format
);
362 /* Construct a string */
363 Length
= _vsnprintf(Buffer
, 512, Format
, ap
);
365 /* Check if we went past the buffer */
366 if (Length
== MAXULONG
)
368 /* Terminate it if we went over-board */
369 Buffer
[sizeof(Buffer
) - 1] = '\n';
372 Length
= sizeof(Buffer
);
375 /* Show it as a message box */
376 UiMessageBox(Buffer
);
378 /* Cleanup and exit */
387 IN ULONG BugCheckCode
,
388 IN ULONG_PTR BugCheckParameter1
,
389 IN ULONG_PTR BugCheckParameter2
,
390 IN ULONG_PTR BugCheckParameter3
,
391 IN ULONG_PTR BugCheckParameter4
)
394 sprintf(Buffer
, "*** STOP: 0x%08lX (0x%08lX, 0x%08lX, 0x%08lX, 0x%08lX)",
395 BugCheckCode
, BugCheckParameter1
, BugCheckParameter2
,
396 BugCheckParameter3
, BugCheckParameter4
);
397 UiMessageBoxCritical(Buffer
);
404 RtlAssert(IN PVOID FailedAssertion
,
407 IN PCHAR Message OPTIONAL
)
411 DbgPrint("Assertion \'%s\' failed at %s line %u: %s\n",
412 (PCHAR
)FailedAssertion
,
419 DbgPrint("Assertion \'%s\' failed at %s line %u\n",
420 (PCHAR
)FailedAssertion
,
428 char *BugCodeStrings
[] =
431 "MISSING_HARDWARE_REQUIREMENTS",
432 "FREELDR_IMAGE_CORRUPTION",
433 "MEMORY_INIT_FAILURE",
436 ULONG_PTR BugCheckInfo
[5];