Create a branch for header work.
[reactos.git] / boot / freeldr / freeldr / debug.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 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 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.
18 */
19
20 #include <freeldr.h>
21
22 #include <debug.h>
23
24 #if DBG && !defined(_M_ARM)
25
26 //#define DEBUG_ALL
27 //#define DEBUG_INIFILE
28 //#define DEBUG_REACTOS
29 //#define DEBUG_CUSTOM
30 #define DEBUG_NONE
31
32 #if defined (DEBUG_ALL)
33 ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
34 DPRINT_UI | DPRINT_DISK | DPRINT_CACHE | DPRINT_REACTOS |
35 DPRINT_LINUX | DPRINT_HWDETECT;
36 #elif defined (DEBUG_INIFILE)
37 ULONG DebugPrintMask = DPRINT_INIFILE;
38 #elif defined (DEBUG_REACTOS)
39 ULONG DebugPrintMask = DPRINT_REACTOS | DPRINT_REGISTRY;
40 #elif defined (DEBUG_CUSTOM)
41 ULONG DebugPrintMask = DPRINT_WARNING |
42 DPRINT_UI | DPRINT_CACHE | DPRINT_REACTOS |
43 DPRINT_LINUX;
44 #else //#elif defined (DEBUG_NONE)
45 ULONG DebugPrintMask = 0;
46 #endif
47
48 #define SCREEN 1
49 #define RS232 2
50 #define BOCHS 4
51
52 #define COM1 1
53 #define COM2 2
54 #define COM3 3
55 #define COM4 4
56
57 #define BOCHS_OUTPUT_PORT 0xe9
58
59 ULONG DebugPort = RS232;
60 //ULONG DebugPort = SCREEN;
61 //ULONG DebugPort = BOCHS;
62 //ULONG DebugPort = SCREEN|BOCHS;
63 ULONG ComPort = COM1;
64 //ULONG BaudRate = 19200;
65 ULONG BaudRate = 115200;
66
67 BOOLEAN DebugStartOfLine = TRUE;
68
69 VOID DebugInit(VOID)
70 {
71 if (DebugPort & RS232)
72 {
73 Rs232PortInitialize(ComPort, BaudRate);
74 }
75 }
76
77 VOID DebugPrintChar(UCHAR Character)
78 {
79 if (Character == '\n')
80 {
81 DebugStartOfLine = TRUE;
82 }
83
84 if (DebugPort & RS232)
85 {
86 if (Character == '\n')
87 {
88 Rs232PortPutByte('\r');
89 }
90 Rs232PortPutByte(Character);
91 }
92 if (DebugPort & BOCHS)
93 {
94 WRITE_PORT_UCHAR((PUCHAR)BOCHS_OUTPUT_PORT, Character);
95 }
96 if (DebugPort & SCREEN)
97 {
98 MachConsPutChar(Character);
99 }
100 }
101
102 ULONG
103 DbgPrint(const char *Format, ...)
104 {
105 int i;
106 int Length;
107 va_list ap;
108 CHAR Buffer[512];
109
110 va_start(ap, Format);
111 Length = _vsnprintf(Buffer, sizeof(Buffer), Format, ap);
112 va_end(ap);
113
114 /* Check if we went past the buffer */
115 if (Length == -1)
116 {
117 /* Terminate it if we went over-board */
118 Buffer[sizeof(Buffer) - 1] = '\n';
119
120 /* Put maximum */
121 Length = sizeof(Buffer);
122 }
123
124 for (i = 0; i < Length; i++)
125 {
126 DebugPrintChar(Buffer[i]);
127 }
128
129 return 0;
130 }
131
132 VOID DebugPrintHeader(ULONG Mask)
133 {
134 /* No header */
135 if (Mask == 0)
136 return;
137
138 switch (Mask)
139 {
140 case DPRINT_WARNING:
141 DbgPrint("WARNING: ");
142 break;
143 case DPRINT_MEMORY:
144 DbgPrint("MEMORY: ");
145 break;
146 case DPRINT_FILESYSTEM:
147 DbgPrint("FILESYS: ");
148 break;
149 case DPRINT_INIFILE:
150 DbgPrint("INIFILE: ");
151 break;
152 case DPRINT_UI:
153 DbgPrint("UI: ");
154 break;
155 case DPRINT_DISK:
156 DbgPrint("DISK: ");
157 break;
158 case DPRINT_CACHE:
159 DbgPrint("CACHE: ");
160 break;
161 case DPRINT_REGISTRY:
162 DbgPrint("REGISTRY: ");
163 break;
164 case DPRINT_REACTOS:
165 DbgPrint("REACTOS: ");
166 break;
167 case DPRINT_LINUX:
168 DbgPrint("LINUX: ");
169 break;
170 case DPRINT_WINDOWS:
171 DbgPrint("WINLDR: ");
172 break;
173 case DPRINT_HWDETECT:
174 DbgPrint("HWDETECT: ");
175 break;
176 default:
177 DbgPrint("UNKNOWN: ");
178 break;
179 }
180 }
181
182 char* g_file;
183 int g_line;
184
185 VOID DbgPrintMask(ULONG Mask, char *format, ...)
186 {
187 va_list ap;
188 char Buffer[2096];
189 char *ptr = Buffer;
190
191 // Mask out unwanted debug messages
192 if (!(Mask & DebugPrintMask))
193 {
194 return;
195 }
196
197 // Print the header if we have started a new line
198 if (DebugStartOfLine)
199 {
200 DbgPrint("(%s:%d) ", g_file, g_line);
201 DebugPrintHeader(Mask);
202 DebugStartOfLine = FALSE;
203 }
204
205 va_start(ap, format);
206 vsprintf(Buffer, format, ap);
207 va_end(ap);
208
209 while (*ptr)
210 {
211 DebugPrintChar(*ptr++);
212 }
213 }
214
215 VOID DebugDumpBuffer(ULONG Mask, PVOID Buffer, ULONG Length)
216 {
217 PUCHAR BufPtr = (PUCHAR)Buffer;
218 ULONG Idx;
219 ULONG Idx2;
220
221 // Mask out unwanted debug messages
222 if (!(Mask & DebugPrintMask))
223 {
224 return;
225 }
226
227 DebugStartOfLine = FALSE; // We don't want line headers
228 DbgPrintMask(Mask, "Dumping buffer at 0x%x with length of %d bytes:\n", Buffer, Length);
229
230 for (Idx=0; Idx<Length; )
231 {
232 DebugStartOfLine = FALSE; // We don't want line headers
233
234 if (Idx < 0x0010)
235 {
236 DbgPrintMask(Mask, "000%x:\t", Idx);
237 }
238 else if (Idx < 0x0100)
239 {
240 DbgPrintMask(Mask, "00%x:\t", Idx);
241 }
242 else if (Idx < 0x1000)
243 {
244 DbgPrintMask(Mask, "0%x:\t", Idx);
245 }
246 else
247 {
248 DbgPrintMask(Mask, "%x:\t", Idx);
249 }
250
251 for (Idx2=0; Idx2<16; Idx2++,Idx++)
252 {
253 if (BufPtr[Idx] < 0x10)
254 {
255 DbgPrintMask(Mask, "0");
256 }
257 DbgPrintMask(Mask, "%x", BufPtr[Idx]);
258
259 if (Idx2 == 7)
260 {
261 DbgPrintMask(Mask, "-");
262 }
263 else
264 {
265 DbgPrintMask(Mask, " ");
266 }
267 }
268
269 Idx -= 16;
270 DbgPrintMask(Mask, " ");
271
272 for (Idx2=0; Idx2<16; Idx2++,Idx++)
273 {
274 if ((BufPtr[Idx] > 20) && (BufPtr[Idx] < 0x80))
275 {
276 DbgPrintMask(Mask, "%c", BufPtr[Idx]);
277 }
278 else
279 {
280 DbgPrintMask(Mask, ".");
281 }
282 }
283
284 DbgPrintMask(Mask, "\n");
285 }
286 }
287
288 #else
289
290 VOID DbgPrintMask(ULONG Mask, char *format, ...)
291 {
292 }
293
294 ULONG DbgPrint(PCCH Format, ...)
295 {
296 return 0;
297 }
298
299 #endif // DBG
300
301 ULONG
302 MsgBoxPrint(const char *Format, ...)
303 {
304 va_list ap;
305 CHAR Buffer[512];
306 ULONG Length;
307
308 va_start(ap, Format);
309
310 /* Construct a string */
311 Length = _vsnprintf(Buffer, 512, Format, ap);
312
313 /* Check if we went past the buffer */
314 if (Length == MAXULONG)
315 {
316 /* Terminate it if we went over-board */
317 Buffer[sizeof(Buffer) - 1] = '\n';
318
319 /* Put maximum */
320 Length = sizeof(Buffer);
321 }
322
323 /* Show it as a message box */
324 UiMessageBox(Buffer);
325
326 /* Cleanup and exit */
327 va_end(ap);
328 return 0;
329 }
330