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
48 static UCHAR DbgChannels
[DBG_CHANNELS_COUNT
];
50 ULONG DebugPort
= RS232
;
51 //ULONG DebugPort = SCREEN;
52 //ULONG DebugPort = BOCHS;
53 //ULONG DebugPort = SCREEN|BOCHS;
55 /* COM1 is the WinDbg port */
60 //ULONG BaudRate = 19200;
61 ULONG BaudRate
= 115200;
63 BOOLEAN DebugStartOfLine
= TRUE
;
67 #if defined (DEBUG_ALL)
68 memset(DbgChannels
, MAX_LEVEL
, DBG_CHANNELS_COUNT
);
69 #elif defined (DEBUG_WARN)
70 memset(DbgChannels
, WARN_LEVEL
|FIXME_LEVEL
|ERR_LEVEL
, DBG_CHANNELS_COUNT
);
71 #elif defined (DEBUG_ERR)
72 memset(DbgChannels
, ERR_LEVEL
, DBG_CHANNELS_COUNT
);
74 memset(DbgChannels
, 0, DBG_CHANNELS_COUNT
);
77 #if defined (DEBUG_INIFILE)
78 DbgChannels
[DPRINT_INIFILE
] = MAX_LEVEL
;
79 #elif defined (DEBUG_REACTOS)
80 DbgChannels
[DPRINT_REACTOS
] = MAX_LEVEL
;
81 DbgChannels
[DPRINT_REGISTRY
] = MAX_LEVEL
;
82 #elif defined (DEBUG_CUSTOM)
83 DbgChannels
[DPRINT_WARNING
] = MAX_LEVEL
;
84 DbgChannels
[DPRINT_WINDOWS
] = MAX_LEVEL
;
87 if (DebugPort
& RS232
)
89 Rs232PortInitialize(ComPort
, BaudRate
);
93 VOID
DebugPrintChar(UCHAR Character
)
95 if (Character
== '\n')
97 DebugStartOfLine
= TRUE
;
100 if (DebugPort
& RS232
)
102 if (Character
== '\n')
104 Rs232PortPutByte('\r');
106 Rs232PortPutByte(Character
);
108 if (DebugPort
& BOCHS
)
110 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_OUTPUT_PORT
, Character
);
112 if (DebugPort
& SCREEN
)
114 MachConsPutChar(Character
);
119 DbgPrint(const char *Format
, ...)
126 va_start(ap
, Format
);
127 Length
= _vsnprintf(Buffer
, sizeof(Buffer
), Format
, ap
);
130 /* Check if we went past the buffer */
133 /* Terminate it if we went over-board */
134 Buffer
[sizeof(Buffer
) - 1] = '\n';
137 Length
= sizeof(Buffer
);
140 for (i
= 0; i
< Length
; i
++)
142 DebugPrintChar(Buffer
[i
]);
149 DbgPrint2(ULONG Mask
, ULONG Level
, const char *File
, ULONG Line
, char *Format
, ...)
155 // Mask out unwanted debug messages
156 if (!(DbgChannels
[Mask
] & Level
) && !(Level
& DBG_DEFAULT_LEVELS
))
161 // Print the header if we have started a new line
162 if (DebugStartOfLine
)
164 DbgPrint("(%s:%lu) ", File
, Line
);
182 DebugStartOfLine
= FALSE
;
185 va_start(ap
, Format
);
186 vsprintf(Buffer
, Format
, ap
);
191 DebugPrintChar(*ptr
++);
196 DebugDumpBuffer(ULONG Mask
, PVOID Buffer
, ULONG Length
)
198 PUCHAR BufPtr
= (PUCHAR
)Buffer
;
202 // Mask out unwanted debug messages
203 if (!(DbgChannels
[Mask
] & TRACE_LEVEL
))
208 DebugStartOfLine
= FALSE
; // We don't want line headers
209 DbgPrint("Dumping buffer at %p with length of %lu bytes:\n", Buffer
, Length
);
211 for (Idx
=0; Idx
<Length
; )
213 DebugStartOfLine
= FALSE
; // We don't want line headers
217 DbgPrint("000%x:\t", Idx
);
219 else if (Idx
< 0x0100)
221 DbgPrint("00%x:\t", Idx
);
223 else if (Idx
< 0x1000)
225 DbgPrint("0%x:\t", Idx
);
229 DbgPrint("%x:\t", Idx
);
232 for (Idx2
=0; Idx2
<16; Idx2
++,Idx
++)
234 if (BufPtr
[Idx
] < 0x10)
238 DbgPrint("%x", BufPtr
[Idx
]);
253 for (Idx2
=0; Idx2
<16; Idx2
++,Idx
++)
255 if ((BufPtr
[Idx
] > 20) && (BufPtr
[Idx
] < 0x80))
257 DbgPrint("%c", BufPtr
[Idx
]);
270 DbgAddDebugChannel( CHAR
* channel
, CHAR
* level
, CHAR op
)
272 int iLevel
, iChannel
;
274 if(channel
== NULL
|| *channel
== L
'\0' ||strlen(channel
) == 0 )
277 if(level
== NULL
|| *level
== L
'\0' ||strlen(level
) == 0 )
279 else if(strcmp(level
, "err") == 0)
281 else if(strcmp(level
, "fixme") == 0)
282 iLevel
= FIXME_LEVEL
;
283 else if(strcmp(level
, "warn") == 0)
285 else if (strcmp(level
, "trace") == 0)
286 iLevel
= TRACE_LEVEL
;
290 if(strcmp(channel
, "memory") == 0) iChannel
= DPRINT_MEMORY
;
291 else if(strcmp(channel
, "filesystem") == 0) iChannel
= DPRINT_FILESYSTEM
;
292 else if(strcmp(channel
, "inifile") == 0) iChannel
= DPRINT_INIFILE
;
293 else if(strcmp(channel
, "ui") == 0) iChannel
= DPRINT_UI
;
294 else if(strcmp(channel
, "disk") == 0) iChannel
= DPRINT_DISK
;
295 else if(strcmp(channel
, "cache") == 0) iChannel
= DPRINT_CACHE
;
296 else if(strcmp(channel
, "registry") == 0) iChannel
= DPRINT_REGISTRY
;
297 else if(strcmp(channel
, "linux") == 0) iChannel
= DPRINT_LINUX
;
298 else if(strcmp(channel
, "hwdetect") == 0) iChannel
= DPRINT_HWDETECT
;
299 else if(strcmp(channel
, "windows") == 0) iChannel
= DPRINT_WINDOWS
;
300 else if(strcmp(channel
, "peloader") == 0) iChannel
= DPRINT_PELOADER
;
301 else if(strcmp(channel
, "scsiport") == 0) iChannel
= DPRINT_SCSIPORT
;
302 else if(strcmp(channel
, "heap") == 0) iChannel
= DPRINT_HEAP
;
303 else if(strcmp(channel
, "all") == 0)
307 for(i
= 0 ; i
< DBG_CHANNELS_COUNT
; i
++)
310 DbgChannels
[i
] |= iLevel
;
312 DbgChannels
[i
] &= ~iLevel
;
320 DbgChannels
[iChannel
] |= iLevel
;
322 DbgChannels
[iChannel
] &= ~iLevel
;
328 DbgParseDebugChannels(PCHAR Value
)
330 CHAR
*str
, *separator
, *c
, op
;
336 separator
= strchr(str
, L
',');
337 if(separator
!= NULL
)
340 c
= strchr(str
, L
'+');
342 c
= strchr(str
, L
'-');
350 DbgAddDebugChannel(c
, str
, op
);
354 } while(separator
!= NULL
);
360 DbgPrint(PCCH Format
, ...)
368 MsgBoxPrint(const char *Format
, ...)
374 va_start(ap
, Format
);
376 /* Construct a string */
377 Length
= _vsnprintf(Buffer
, 512, Format
, ap
);
379 /* Check if we went past the buffer */
380 if (Length
== MAXULONG
)
382 /* Terminate it if we went over-board */
383 Buffer
[sizeof(Buffer
) - 1] = '\n';
386 Length
= sizeof(Buffer
);
389 /* Show it as a message box */
390 UiMessageBox(Buffer
);
392 /* Cleanup and exit */
401 IN ULONG BugCheckCode
,
402 IN ULONG_PTR BugCheckParameter1
,
403 IN ULONG_PTR BugCheckParameter2
,
404 IN ULONG_PTR BugCheckParameter3
,
405 IN ULONG_PTR BugCheckParameter4
)
408 sprintf(Buffer
, "*** STOP: 0x%08lX (0x%08lX, 0x%08lX, 0x%08lX, 0x%08lX)",
409 BugCheckCode
, BugCheckParameter1
, BugCheckParameter2
,
410 BugCheckParameter3
, BugCheckParameter4
);
411 UiMessageBoxCritical(Buffer
);
418 RtlAssert(IN PVOID FailedAssertion
,
421 IN PCHAR Message OPTIONAL
)
425 DbgPrint("Assertion \'%s\' failed at %s line %u: %s\n",
426 (PCHAR
)FailedAssertion
,
433 DbgPrint("Assertion \'%s\' failed at %s line %u\n",
434 (PCHAR
)FailedAssertion
,
442 char *BugCodeStrings
[] =
445 "MISSING_HARDWARE_REQUIREMENTS",
446 "FREELDR_IMAGE_CORRUPTION",
447 "MEMORY_INIT_FAILURE",
450 ULONG_PTR BugCheckInfo
[5];