Misc Changes, Added Logging Subsystem, Fixes to InitRD, Working on RTL8139 driver
[tpg/acess2.git] / Kernel / debug.c
1 /*
2  * AcessOS Microkernel Version
3  * debug.c
4  */
5 #include <acess.h>
6 #include <stdarg.h>
7
8 #define DEBUG_TO_E9     1
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT     0x3F8
11 #define GDB_SERIAL_PORT 0x2F8
12
13 // === IMPORTS ===
14 extern void Threads_Dump();
15
16 // === GLOBALS ===
17  int    gDebug_Level = 0;
18  int    giDebug_KTerm = -1;
19  int    gbDebug_SerialSetup = 0;
20  int    gbGDB_SerialSetup = 0;
21
22 // === CODE ===
23 int putDebugChar(char ch)
24 {
25         if(!gbGDB_SerialSetup) {
26                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
27                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
28                 outb(GDB_SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
29                 outb(GDB_SERIAL_PORT + 1, 0x00);    //                  (hi byte)
30                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
31                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
32                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
33                 gbDebug_SerialSetup = 1;
34         }
35         while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
36         outb(GDB_SERIAL_PORT, ch);
37         return 0;
38 }
39 int getDebugChar()
40 {
41         if(!gbGDB_SerialSetup) {
42                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
43                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
44                 outb(GDB_SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
45                 outb(GDB_SERIAL_PORT + 1, 0x00);    //                  (hi byte)
46                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
47                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
48                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
49                 gbDebug_SerialSetup = 1;
50         }
51         while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0)     ;
52         return inb(GDB_SERIAL_PORT);
53 }
54
55 static void Debug_Putchar(char ch)
56 {
57         if(giDebug_KTerm != -1)
58                 VFS_Write(giDebug_KTerm, 1, &ch);
59
60         #if DEBUG_TO_SERIAL
61         if(!gbDebug_SerialSetup) {
62                 outb(SERIAL_PORT + 1, 0x00);    // Disable all interrupts
63                 outb(SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
64                 outb(SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
65                 outb(SERIAL_PORT + 1, 0x00);    //                  (hi byte)
66                 outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
67                 outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
68                 outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
69                 gbDebug_SerialSetup = 1;
70         }
71         while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
72         outb(SERIAL_PORT, ch);
73         #endif
74
75         #if DEBUG_TO_E9
76         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
77         #endif
78 }
79
80 static void Debug_Puts(char *Str)
81 {
82         while(*Str)     Debug_Putchar(*Str++);
83 }
84
85 void Debug_Fmt(const char *format, va_list *args)
86 {
87         char    c, pad = ' ';
88          int    minSize = 0;
89         char    tmpBuf[34];     // For Integers
90         char    *p = NULL;
91          int    isLongLong = 0;
92         Uint64  arg;
93
94         while((c = *format++) != 0)
95         {
96                 // Non control character
97                 if( c != '%' ) {
98                         Debug_Putchar(c);
99                         continue;
100                 }
101
102                 c = *format++;
103
104                 // Literal %
105                 if(c == '%') {
106                         Debug_Putchar('%');
107                         continue;
108                 }
109
110                 // Pointer
111                 if(c == 'p') {
112                         Uint    ptr = va_arg(*args, Uint);
113                         Debug_Putchar('*');     Debug_Putchar('0');     Debug_Putchar('x');
114                         p = tmpBuf;
115                         itoa(p, ptr, 16, BITS/4, '0');
116                         goto printString;
117                 }
118
119                 // Get Argument
120                 arg = va_arg(*args, Uint);
121
122                 // Padding
123                 if(c == '0') {
124                         pad = '0';
125                         c = *format++;
126                 } else
127                         pad = ' ';
128
129                 // Minimum length
130                 minSize = 1;
131                 if('1' <= c && c <= '9')
132                 {
133                         minSize = 0;
134                         while('0' <= c && c <= '9')
135                         {
136                                 minSize *= 10;
137                                 minSize += c - '0';
138                                 c = *format++;
139                         }
140                 }
141
142                 // Long (default)
143                 isLongLong = 0;
144                 if(c == 'l') {
145                         c = *format++;
146                         if(c == 'l') {
147                                 #if BITS == 32
148                                 arg |= va_arg(*args, Uint);
149                                 #endif
150                                 c = *format++;
151                                 isLongLong = 1;
152                         }
153                 }
154
155                 p = tmpBuf;
156                 switch (c) {
157                 case 'd':
158                 case 'i':
159                         if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
160                                 Debug_Putchar('-');
161                                 arg = -arg;
162                         }
163                         itoa(p, arg, 10, minSize, pad);
164                         goto printString;
165                 case 'u':
166                         itoa(p, arg, 10, minSize, pad);
167                         goto printString;
168                 case 'x':
169                         itoa(p, arg, 16, minSize, pad);
170                         goto printString;
171                 case 'o':
172                         itoa(p, arg, 8, minSize, pad);
173                         goto printString;
174                 case 'b':
175                         itoa(p, arg, 2, minSize, pad);
176                         goto printString;
177
178                 case 'B':       //Boolean
179                         if(arg) Debug_Puts("True");
180                         else    Debug_Puts("False");
181                         break;
182
183                 case 's':
184                         p = (char*)(Uint)arg;
185                 printString:
186                         if(!p)          p = "(null)";
187                         while(*p)       Debug_Putchar(*p++);
188                         break;
189
190                 // Single Character / Array
191                 case 'c':
192                         if(minSize == 1) {
193                                 Debug_Putchar(arg);
194                                 break;
195                         }
196                         p = (char*)(Uint)arg;
197                         if(!p)  goto printString;
198                         while(minSize--)        Debug_Putchar(*p++);
199                         break;
200
201                 default:
202                         Debug_Putchar(arg);
203                         break;
204                 }
205     }
206 }
207
208 /**
209  * \fn void LogF(char *Msg, ...)
210  */
211 void LogF(char *Fmt, ...)
212 {
213         va_list args;
214
215         va_start(args, Fmt);
216
217         Debug_Fmt(Fmt, &args);
218
219         va_end(args);
220 }
221 /**
222  * \fn void Log(char *Msg, ...)
223  */
224 void Log(char *Fmt, ...)
225 {
226         va_list args;
227
228         Debug_Puts("Log: ");
229         va_start(args, Fmt);
230         Debug_Fmt(Fmt, &args);
231         va_end(args);
232         Debug_Putchar('\n');
233 }
234 void Warning(char *Fmt, ...)
235 {
236         va_list args;
237         Debug_Puts("Warning: ");
238         va_start(args, Fmt);
239         Debug_Fmt(Fmt, &args);
240         va_end(args);
241         Debug_Putchar('\n');
242 }
243 void Panic(char *Fmt, ...)
244 {
245         va_list args;
246         Debug_Puts("Panic: ");
247         va_start(args, Fmt);
248         Debug_Fmt(Fmt, &args);
249         va_end(args);
250         Debug_Putchar('\n');
251
252         Threads_Dump();
253
254         __asm__ __volatile__ ("xchg %bx, %bx");
255         __asm__ __volatile__ ("cli;\n\thlt");
256         for(;;) __asm__ __volatile__ ("hlt");
257 }
258
259 void Debug_SetKTerminal(char *File)
260 {
261         if(giDebug_KTerm != -1)
262                 VFS_Close(giDebug_KTerm);
263         giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
264         Log("Opened '%s' as 0x%x", File, giDebug_KTerm);
265 }
266
267 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
268 {
269         va_list args;
270          int    i = gDebug_Level ++;
271          int    pos;
272
273         va_start(args, ArgTypes);
274
275         while(i--)      Debug_Putchar(' ');
276
277         Debug_Puts(FuncName);   Debug_Puts(": (");
278
279         while(*ArgTypes)
280         {
281                 pos = strpos(ArgTypes, ' ');
282                 if(pos != -1)   ArgTypes[pos] = '\0';
283                 if(pos == -1 || pos > 1) {
284                         Debug_Puts(ArgTypes+1);
285                         Debug_Putchar('=');
286                 }
287                 if(pos != -1)   ArgTypes[pos] = ' ';
288                 switch(*ArgTypes)
289                 {
290                 case 'p':       Debug_Fmt("%p", &args); break;
291                 case 's':       Debug_Fmt("'%s'", &args);       break;
292                 case 'i':       Debug_Fmt("%i", &args); break;
293                 case 'u':       Debug_Fmt("%u", &args); break;
294                 case 'x':       Debug_Fmt("0x%x", &args);       break;
295                 case 'b':       Debug_Fmt("0b%b", &args);       break;
296                 // Extended (64-Bit)
297                 case 'X':       Debug_Fmt("0x%llx", &args);     break;
298                 case 'B':       Debug_Fmt("0b%llb", &args);     break;
299                 }
300                 if(pos != -1) {
301                         Debug_Putchar(',');     Debug_Putchar(' ');
302                 }
303
304                 if(pos == -1)   break;
305                 ArgTypes = &ArgTypes[pos+1];
306         }
307
308         va_end(args);
309         Debug_Putchar(')');     Debug_Putchar('\n');
310 }
311
312 void Debug_Log(char *FuncName, char *Fmt, ...)
313 {
314         va_list args;
315          int    i = gDebug_Level;
316
317         va_start(args, Fmt);
318
319         while(i--)      Debug_Putchar(' ');
320
321         Debug_Puts(FuncName);   Debug_Puts(": ");
322         Debug_Fmt(Fmt, &args);
323
324         va_end(args);
325         Debug_Putchar('\n');
326 }
327
328 void Debug_Leave(char *FuncName, char RetType, ...)
329 {
330         va_list args;
331          int    i = --gDebug_Level;
332
333         va_start(args, RetType);
334
335         if( i == -1 ) {
336                 gDebug_Level = 0;
337                 i = 0;
338         }
339         // Indenting
340         while(i--)      Debug_Putchar(' ');
341
342         Debug_Puts(FuncName);   Debug_Puts(": RETURN");
343
344         // No Return
345         if(RetType == '-') {
346                 Debug_Putchar('\n');
347                 return;
348         }
349
350         Debug_Putchar(' ');
351         switch(RetType)
352         {
353         case 'n':       Debug_Puts("NULL");     break;
354         case 'p':       Debug_Fmt("%p", &args); break;
355         case 's':       Debug_Fmt("'%s'", &args);       break;
356         case 'i':       Debug_Fmt("%i", &args); break;
357         case 'u':       Debug_Fmt("%u", &args); break;
358         case 'x':       Debug_Fmt("0x%x", &args);       break;
359         // Extended (64-Bit)
360         case 'X':       Debug_Fmt("0x%llx", &args);     break;
361         }
362         Debug_Putchar('\n');
363
364         va_end(args);
365 }
366
367 void Debug_HexDump(char *Header, void *Data, Uint Length)
368 {
369         Uint8   *cdat = Data;
370         Uint    pos = 0;
371         Debug_Puts(Header);
372         LogF(" (Hexdump of %p)\n", Data);
373
374         while(Length >= 16)
375         {
376                 #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
377                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
378                         " %02x %02x %02x %02x %02x %02x %02x %02x"
379                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
380                         pos,
381                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
382                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
383                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
384                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
385                         );
386                 Length -= 16;
387                 cdat += 16;
388                 pos += 16;
389         }
390
391         LogF("Log: %04x: ", pos);
392         while(Length)
393         {
394                 Uint    byte = *cdat;
395                 LogF("%02x ", byte);
396                 Length--;
397                 cdat ++;
398         }
399         Debug_Putchar('\n');
400 }
401
402 // --- EXPORTS ---
403 EXPORT(Log);
404 EXPORT(Warning);
405 EXPORT(Debug_Enter);
406 EXPORT(Debug_Log);
407 EXPORT(Debug_Leave);

UCC git Repository :: git.ucc.asn.au