d4a491a0f91582a6fb958d4033992b85350624e4
[reactos.git] / freeldr / freeldr / debug.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <debug.h>
22 #include <rtl.h>
23 #include <comm.h>
24
25 #ifdef DEBUG
26
27 //#define DEBUG_ULTRA
28 //#define DEBUG_INIFILE
29 //#define DEBUG_REACTOS
30 //#define DEBUG_CUSTOM
31 #define DEBUG_NONE
32
33 #ifdef DEBUG_ULTRA
34 ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
35 DPRINT_UI | DPRINT_DISK | DPRINT_CACHE | DPRINT_REACTOS |
36 DPRINT_LINUX;
37 #endif
38
39 #ifdef DEBUG_INIFILE
40 ULONG DebugPrintMask = DPRINT_INIFILE;
41 #endif
42
43 #ifdef DEBUG_REACTOS
44 ULONG DebugPrintMask = DPRINT_REACTOS | DPRINT_REGISTRY;
45 #endif
46
47 #ifdef DEBUG_CUSTOM
48 ULONG DebugPrintMask = 0;
49 #endif
50
51 #ifdef DEBUG_NONE
52 ULONG DebugPrintMask = 0;
53 #endif
54
55 #define SCREEN 0
56 #define RS232 1
57 #define BOCHS 2
58
59 #define COM1 1
60 #define COM2 2
61 #define COM3 3
62 #define COM4 4
63
64 #define BOCHS_OUTPUT_PORT 0xe9
65
66 //ULONG DebugPort = RS232;
67 //ULONG DebugPort = SCREEN;
68 ULONG DebugPort = BOCHS;
69 ULONG ComPort = COM1;
70 //ULONG BaudRate = 19200;
71 ULONG BaudRate = 115200;
72
73 BOOL DebugStartOfLine = TRUE;
74
75 VOID DebugInit(VOID)
76 {
77 if (DebugPort == RS232)
78 {
79 Rs232PortInitialize(ComPort, BaudRate);
80 }
81 }
82
83 VOID DebugPrintChar(UCHAR Character)
84 {
85 if (Character == '\n')
86 {
87 DebugStartOfLine = TRUE;
88 }
89
90 if (DebugPort == RS232)
91 {
92 if (Character == '\n')
93 {
94 Rs232PortPutByte('\r');
95 }
96 Rs232PortPutByte(Character);
97 }
98 else if (DebugPort == BOCHS)
99 {
100 WRITE_PORT_UCHAR((PUCHAR)BOCHS_OUTPUT_PORT, Character);
101 }
102 else
103 {
104 putchar(Character);
105 }
106 }
107
108 VOID DebugPrintHeader(ULONG Mask)
109 {
110 /* No header */
111 if (Mask == 0)
112 return;
113
114 switch (Mask)
115 {
116 case DPRINT_WARNING:
117 DebugPrintChar('W');
118 DebugPrintChar('A');
119 DebugPrintChar('R');
120 DebugPrintChar('N');
121 DebugPrintChar('I');
122 DebugPrintChar('N');
123 DebugPrintChar('G');
124 DebugPrintChar(':');
125 DebugPrintChar(' ');
126 break;
127 case DPRINT_MEMORY:
128 DebugPrintChar('M');
129 DebugPrintChar('E');
130 DebugPrintChar('M');
131 DebugPrintChar('O');
132 DebugPrintChar('R');
133 DebugPrintChar('Y');
134 DebugPrintChar(':');
135 DebugPrintChar(' ');
136 break;
137 case DPRINT_FILESYSTEM:
138 DebugPrintChar('F');
139 DebugPrintChar('I');
140 DebugPrintChar('L');
141 DebugPrintChar('E');
142 DebugPrintChar('S');
143 DebugPrintChar('Y');
144 DebugPrintChar('S');
145 DebugPrintChar(':');
146 DebugPrintChar(' ');
147 break;
148 case DPRINT_INIFILE:
149 DebugPrintChar('I');
150 DebugPrintChar('N');
151 DebugPrintChar('I');
152 DebugPrintChar('F');
153 DebugPrintChar('I');
154 DebugPrintChar('L');
155 DebugPrintChar('E');
156 DebugPrintChar(':');
157 DebugPrintChar(' ');
158 break;
159 case DPRINT_UI:
160 DebugPrintChar('U');
161 DebugPrintChar('I');
162 DebugPrintChar(':');
163 DebugPrintChar(' ');
164 break;
165 case DPRINT_DISK:
166 DebugPrintChar('D');
167 DebugPrintChar('I');
168 DebugPrintChar('S');
169 DebugPrintChar('K');
170 DebugPrintChar(':');
171 DebugPrintChar(' ');
172 break;
173 case DPRINT_CACHE:
174 DebugPrintChar('C');
175 DebugPrintChar('A');
176 DebugPrintChar('C');
177 DebugPrintChar('H');
178 DebugPrintChar('E');
179 DebugPrintChar(':');
180 DebugPrintChar(' ');
181 break;
182 case DPRINT_REGISTRY:
183 DebugPrintChar('R');
184 DebugPrintChar('E');
185 DebugPrintChar('G');
186 DebugPrintChar('I');
187 DebugPrintChar('S');
188 DebugPrintChar('T');
189 DebugPrintChar('R');
190 DebugPrintChar('Y');
191 DebugPrintChar(':');
192 DebugPrintChar(' ');
193 break;
194 case DPRINT_REACTOS:
195 DebugPrintChar('R');
196 DebugPrintChar('E');
197 DebugPrintChar('A');
198 DebugPrintChar('C');
199 DebugPrintChar('T');
200 DebugPrintChar('O');
201 DebugPrintChar('S');
202 DebugPrintChar(':');
203 DebugPrintChar(' ');
204 break;
205 case DPRINT_LINUX:
206 DebugPrintChar('L');
207 DebugPrintChar('I');
208 DebugPrintChar('N');
209 DebugPrintChar('U');
210 DebugPrintChar('X');
211 DebugPrintChar(':');
212 DebugPrintChar(' ');
213 break;
214 default:
215 DebugPrintChar('U');
216 DebugPrintChar('N');
217 DebugPrintChar('K');
218 DebugPrintChar('N');
219 DebugPrintChar('O');
220 DebugPrintChar('W');
221 DebugPrintChar('N');
222 DebugPrintChar(':');
223 DebugPrintChar(' ');
224 break;
225 }
226 }
227
228 VOID DebugPrint(ULONG Mask, char *format, ...)
229 {
230 int *dataptr = (int *) &format;
231 char c, *ptr, str[16];
232
233 // Mask out unwanted debug messages
234 if (!(Mask & DebugPrintMask))
235 {
236 return;
237 }
238
239 // Print the header if we have started a new line
240 if (DebugStartOfLine)
241 {
242 DebugPrintHeader(Mask);
243 DebugStartOfLine = FALSE;
244 }
245
246 dataptr++;
247
248 while ((c = *(format++)))
249 {
250 if (c != '%')
251 {
252 DebugPrintChar(c);
253 }
254 else
255 {
256 switch (c = *(format++))
257 {
258 case 'd': case 'u': case 'x':
259
260 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
261
262 ptr = str;
263
264 while (*ptr)
265 {
266 DebugPrintChar(*(ptr++));
267 }
268 break;
269
270 case 'c':
271
272 DebugPrintChar((*(dataptr++))&0xff);
273 break;
274
275 case 's':
276
277 ptr = (char *)(*(dataptr++));
278
279 while ((c = *(ptr++)))
280 {
281 DebugPrintChar(c);
282 }
283 break;
284 }
285 }
286 }
287
288
289 if (DebugPort == SCREEN)
290 {
291 //getch();
292 }
293
294 }
295
296 VOID DebugDumpBuffer(ULONG Mask, PVOID Buffer, ULONG Length)
297 {
298 PUCHAR BufPtr = (PUCHAR)Buffer;
299 ULONG Idx;
300 ULONG Idx2;
301
302 // Mask out unwanted debug messages
303 if (!(Mask & DebugPrintMask))
304 {
305 return;
306 }
307
308 DebugStartOfLine = FALSE; // We don't want line headers
309 DebugPrint(Mask, "Dumping buffer at 0x%x with length of %d bytes:\n", Buffer, Length);
310
311 for (Idx=0; Idx<Length; )
312 {
313 DebugStartOfLine = FALSE; // We don't want line headers
314
315 if (Idx < 0x0010)
316 {
317 DebugPrint(Mask, "000%x:\t", Idx);
318 }
319 else if (Idx < 0x0100)
320 {
321 DebugPrint(Mask, "00%x:\t", Idx);
322 }
323 else if (Idx < 0x1000)
324 {
325 DebugPrint(Mask, "0%x:\t", Idx);
326 }
327 else
328 {
329 DebugPrint(Mask, "%x:\t", Idx);
330 }
331
332 for (Idx2=0; Idx2<16; Idx2++,Idx++)
333 {
334 if (BufPtr[Idx] < 0x10)
335 {
336 DebugPrint(Mask, "0");
337 }
338 DebugPrint(Mask, "%x", BufPtr[Idx]);
339
340 if (Idx2 == 7)
341 {
342 DebugPrint(Mask, "-");
343 }
344 else
345 {
346 DebugPrint(Mask, " ");
347 }
348 }
349
350 Idx -= 16;
351 DebugPrint(Mask, " ");
352
353 for (Idx2=0; Idx2<16; Idx2++,Idx++)
354 {
355 if ((BufPtr[Idx] > 20) && (BufPtr[Idx] < 0x80))
356 {
357 DebugPrint(Mask, "%c", BufPtr[Idx]);
358 }
359 else
360 {
361 DebugPrint(Mask, ".");
362 }
363 }
364
365 DebugPrint(Mask, "\n");
366 }
367 }
368
369 #endif // defined DEBUG