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)
27 //#define DEBUG_INIFILE
28 //#define DEBUG_REACTOS
29 //#define DEBUG_CUSTOM
32 #define DBG_DEFAULT_LEVELS (ERR_LEVEL|FIXME_LEVEL)
43 #define BOCHS_OUTPUT_PORT 0xe9
46 static UCHAR DbgChannels
[DBG_CHANNELS_COUNT
];
48 ULONG DebugPort
= RS232
;
49 //ULONG DebugPort = SCREEN;
50 //ULONG DebugPort = BOCHS;
51 //ULONG DebugPort = SCREEN|BOCHS;
53 //ULONG BaudRate = 19200;
54 ULONG BaudRate
= 115200;
56 BOOLEAN DebugStartOfLine
= TRUE
;
60 #if defined (DEBUG_ALL)
61 memset(DbgChannels
, MAX_LEVEL
, DBG_CHANNELS_COUNT
);
63 memset(DbgChannels
, 0, DBG_CHANNELS_COUNT
);
66 #if defined (DEBUG_INIFILE)
67 DbgChannels
[DPRINT_INIFILE
] = MAX_LEVEL
;
68 #elif defined (DEBUG_REACTOS)
69 DbgChannels
[DPRINT_REACTOS
] = MAX_LEVEL
;
70 DbgChannels
[DPRINT_REGISTRY
] = MAX_LEVEL
;
71 #elif defined (DEBUG_CUSTOM)
72 DbgChannels
[DPRINT_WARNING
] = MAX_LEVEL
;
73 DbgChannels
[DPRINT_WINDOWS
] = MAX_LEVEL
;
76 if (DebugPort
& RS232
)
78 Rs232PortInitialize(ComPort
, BaudRate
);
82 VOID
DebugPrintChar(UCHAR Character
)
84 if (Character
== '\n')
86 DebugStartOfLine
= TRUE
;
89 if (DebugPort
& RS232
)
91 if (Character
== '\n')
93 Rs232PortPutByte('\r');
95 Rs232PortPutByte(Character
);
97 if (DebugPort
& BOCHS
)
99 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_OUTPUT_PORT
, Character
);
101 if (DebugPort
& SCREEN
)
103 MachConsPutChar(Character
);
108 DbgPrint(const char *Format
, ...)
115 va_start(ap
, Format
);
116 Length
= _vsnprintf(Buffer
, sizeof(Buffer
), Format
, ap
);
119 /* Check if we went past the buffer */
122 /* Terminate it if we went over-board */
123 Buffer
[sizeof(Buffer
) - 1] = '\n';
126 Length
= sizeof(Buffer
);
129 for (i
= 0; i
< Length
; i
++)
131 DebugPrintChar(Buffer
[i
]);
138 DbgPrint2(ULONG Mask
, ULONG Level
, const char *File
, ULONG Line
, char *Format
, ...)
144 // Mask out unwanted debug messages
145 if (!(DbgChannels
[Mask
] & Level
) && !(Level
& DBG_DEFAULT_LEVELS
))
150 // Print the header if we have started a new line
151 if (DebugStartOfLine
)
153 DbgPrint("(%s:%lu) ", File
, Line
);
171 DebugStartOfLine
= FALSE
;
174 va_start(ap
, Format
);
175 vsprintf(Buffer
, Format
, ap
);
180 DebugPrintChar(*ptr
++);
185 DebugDumpBuffer(ULONG Mask
, PVOID Buffer
, ULONG Length
)
187 PUCHAR BufPtr
= (PUCHAR
)Buffer
;
191 // Mask out unwanted debug messages
192 if (!(DbgChannels
[Mask
] & TRACE_LEVEL
))
197 DebugStartOfLine
= FALSE
; // We don't want line headers
198 DbgPrint("Dumping buffer at %p with length of %lu bytes:\n", Buffer
, Length
);
200 for (Idx
=0; Idx
<Length
; )
202 DebugStartOfLine
= FALSE
; // We don't want line headers
206 DbgPrint("000%x:\t", Idx
);
208 else if (Idx
< 0x0100)
210 DbgPrint("00%x:\t", Idx
);
212 else if (Idx
< 0x1000)
214 DbgPrint("0%x:\t", Idx
);
218 DbgPrint("%x:\t", Idx
);
221 for (Idx2
=0; Idx2
<16; Idx2
++,Idx
++)
223 if (BufPtr
[Idx
] < 0x10)
227 DbgPrint("%x", BufPtr
[Idx
]);
242 for (Idx2
=0; Idx2
<16; Idx2
++,Idx
++)
244 if ((BufPtr
[Idx
] > 20) && (BufPtr
[Idx
] < 0x80))
246 DbgPrint("%c", BufPtr
[Idx
]);
259 DbgAddDebugChannel( CHAR
* channel
, CHAR
* level
, CHAR op
)
261 int iLevel
, iChannel
;
263 if(channel
== NULL
|| *channel
== L
'\0' ||strlen(channel
) == 0 )
266 if(level
== NULL
|| *level
== L
'\0' ||strlen(level
) == 0 )
268 else if(strcmp(level
, "err") == 0)
270 else if(strcmp(level
, "fixme") == 0)
271 iLevel
= FIXME_LEVEL
;
272 else if(strcmp(level
, "warn") == 0)
274 else if (strcmp(level
, "trace") == 0)
275 iLevel
= TRACE_LEVEL
;
279 if(strcmp(channel
, "memory") == 0) iChannel
= DPRINT_MEMORY
;
280 else if(strcmp(channel
, "filesystem") == 0) iChannel
= DPRINT_FILESYSTEM
;
281 else if(strcmp(channel
, "inifile") == 0) iChannel
= DPRINT_INIFILE
;
282 else if(strcmp(channel
, "ui") == 0) iChannel
= DPRINT_UI
;
283 else if(strcmp(channel
, "disk") == 0) iChannel
= DPRINT_DISK
;
284 else if(strcmp(channel
, "cache") == 0) iChannel
= DPRINT_CACHE
;
285 else if(strcmp(channel
, "registry") == 0) iChannel
= DPRINT_REGISTRY
;
286 else if(strcmp(channel
, "linux") == 0) iChannel
= DPRINT_LINUX
;
287 else if(strcmp(channel
, "hwdetect") == 0) iChannel
= DPRINT_HWDETECT
;
288 else if(strcmp(channel
, "windows") == 0) iChannel
= DPRINT_WINDOWS
;
289 else if(strcmp(channel
, "peloader") == 0) iChannel
= DPRINT_PELOADER
;
290 else if(strcmp(channel
, "scsiport") == 0) iChannel
= DPRINT_SCSIPORT
;
291 else if(strcmp(channel
, "heap") == 0) iChannel
= DPRINT_HEAP
;
292 else if(strcmp(channel
, "all") == 0)
296 for(i
= 0 ; i
< DBG_CHANNELS_COUNT
; i
++)
299 DbgChannels
[i
] |= iLevel
;
301 DbgChannels
[i
] &= ~iLevel
;
308 DbgChannels
[iChannel
] |= iLevel
;
310 DbgChannels
[iChannel
] &= ~iLevel
;
316 DbgParseDebugChannels(PCHAR Value
)
318 CHAR
*str
, *separator
, *c
, op
;
324 separator
= strchr(str
, L
',');
325 if(separator
!= NULL
)
328 c
= strchr(str
, L
'+');
330 c
= strchr(str
, L
'-');
338 DbgAddDebugChannel(c
, str
, op
);
342 } while(separator
!= NULL
);
348 DbgPrint(PCCH Format
, ...)
356 MsgBoxPrint(const char *Format
, ...)
362 va_start(ap
, Format
);
364 /* Construct a string */
365 Length
= _vsnprintf(Buffer
, 512, Format
, ap
);
367 /* Check if we went past the buffer */
368 if (Length
== MAXULONG
)
370 /* Terminate it if we went over-board */
371 Buffer
[sizeof(Buffer
) - 1] = '\n';
374 Length
= sizeof(Buffer
);
377 /* Show it as a message box */
378 UiMessageBox(Buffer
);
380 /* Cleanup and exit */
389 IN ULONG BugCheckCode
,
390 IN ULONG_PTR BugCheckParameter1
,
391 IN ULONG_PTR BugCheckParameter2
,
392 IN ULONG_PTR BugCheckParameter3
,
393 IN ULONG_PTR BugCheckParameter4
)
396 sprintf(Buffer
, "*** STOP: 0x%08lX (0x%08lX, 0x%08lX, 0x%08lX, 0x%08lX)",
397 BugCheckCode
, BugCheckParameter1
, BugCheckParameter2
,
398 BugCheckParameter3
, BugCheckParameter4
);
399 UiMessageBoxCritical(Buffer
);
406 RtlAssert(IN PVOID FailedAssertion
,
409 IN PCHAR Message OPTIONAL
)
413 DbgPrint("Assertion \'%s\' failed at %s line %d: %s\n",
414 (PCHAR
)FailedAssertion
,
421 DbgPrint("Assertion \'%s\' failed at %s line %d\n",
422 (PCHAR
)FailedAssertion
,