Updating drivers to use the Log_ functions instead of Log() and Warning()
[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, len;
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                         if(!p)          p = "(null)";
186                         len = strlen(p);
187                         while(len++ < minSize)  Debug_Putchar(pad);
188                 printString:
189                         if(!p)          p = "(null)";
190                         while(*p)       Debug_Putchar(*p++);
191                         break;
192
193                 // Single Character / Array
194                 case 'c':
195                         if(minSize == 1) {
196                                 Debug_Putchar(arg);
197                                 break;
198                         }
199                         p = (char*)(Uint)arg;
200                         if(!p)  goto printString;
201                         while(minSize--)        Debug_Putchar(*p++);
202                         break;
203
204                 default:
205                         Debug_Putchar(arg);
206                         break;
207                 }
208     }
209 }
210
211 /**
212  * \fn void LogF(char *Msg, ...)
213  */
214 void LogF(char *Fmt, ...)
215 {
216         va_list args;
217
218         va_start(args, Fmt);
219
220         Debug_Fmt(Fmt, &args);
221
222         va_end(args);
223 }
224 /**
225  * \fn void Log(char *Msg, ...)
226  */
227 void Log(char *Fmt, ...)
228 {
229         va_list args;
230
231         Debug_Puts("Log: ");
232         va_start(args, Fmt);
233         Debug_Fmt(Fmt, &args);
234         va_end(args);
235         Debug_Putchar('\n');
236 }
237 void Warning(char *Fmt, ...)
238 {
239         va_list args;
240         Debug_Puts("Warning: ");
241         va_start(args, Fmt);
242         Debug_Fmt(Fmt, &args);
243         va_end(args);
244         Debug_Putchar('\n');
245 }
246 void Panic(char *Fmt, ...)
247 {
248         va_list args;
249         Debug_Puts("Panic: ");
250         va_start(args, Fmt);
251         Debug_Fmt(Fmt, &args);
252         va_end(args);
253         Debug_Putchar('\n');
254
255         Threads_Dump();
256
257         __asm__ __volatile__ ("xchg %bx, %bx");
258         __asm__ __volatile__ ("cli;\n\thlt");
259         for(;;) __asm__ __volatile__ ("hlt");
260 }
261
262 void Debug_SetKTerminal(char *File)
263 {
264         if(giDebug_KTerm != -1)
265                 VFS_Close(giDebug_KTerm);
266         giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
267         Log("Opened '%s' as 0x%x", File, giDebug_KTerm);
268 }
269
270 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
271 {
272         va_list args;
273          int    i = gDebug_Level ++;
274          int    pos;
275
276         va_start(args, ArgTypes);
277
278         while(i--)      Debug_Putchar(' ');
279
280         Debug_Puts(FuncName);   Debug_Puts(": (");
281
282         while(*ArgTypes)
283         {
284                 pos = strpos(ArgTypes, ' ');
285                 if(pos != -1)   ArgTypes[pos] = '\0';
286                 if(pos == -1 || pos > 1) {
287                         Debug_Puts(ArgTypes+1);
288                         Debug_Putchar('=');
289                 }
290                 if(pos != -1)   ArgTypes[pos] = ' ';
291                 switch(*ArgTypes)
292                 {
293                 case 'p':       Debug_Fmt("%p", &args); break;
294                 case 's':       Debug_Fmt("'%s'", &args);       break;
295                 case 'i':       Debug_Fmt("%i", &args); break;
296                 case 'u':       Debug_Fmt("%u", &args); break;
297                 case 'x':       Debug_Fmt("0x%x", &args);       break;
298                 case 'b':       Debug_Fmt("0b%b", &args);       break;
299                 // Extended (64-Bit)
300                 case 'X':       Debug_Fmt("0x%llx", &args);     break;
301                 case 'B':       Debug_Fmt("0b%llb", &args);     break;
302                 }
303                 if(pos != -1) {
304                         Debug_Putchar(',');     Debug_Putchar(' ');
305                 }
306
307                 if(pos == -1)   break;
308                 ArgTypes = &ArgTypes[pos+1];
309         }
310
311         va_end(args);
312         Debug_Putchar(')');     Debug_Putchar('\n');
313 }
314
315 void Debug_Log(char *FuncName, char *Fmt, ...)
316 {
317         va_list args;
318          int    i = gDebug_Level;
319
320         va_start(args, Fmt);
321
322         while(i--)      Debug_Putchar(' ');
323
324         Debug_Puts(FuncName);   Debug_Puts(": ");
325         Debug_Fmt(Fmt, &args);
326
327         va_end(args);
328         Debug_Putchar('\n');
329 }
330
331 void Debug_Leave(char *FuncName, char RetType, ...)
332 {
333         va_list args;
334          int    i = --gDebug_Level;
335
336         va_start(args, RetType);
337
338         if( i == -1 ) {
339                 gDebug_Level = 0;
340                 i = 0;
341         }
342         // Indenting
343         while(i--)      Debug_Putchar(' ');
344
345         Debug_Puts(FuncName);   Debug_Puts(": RETURN");
346
347         // No Return
348         if(RetType == '-') {
349                 Debug_Putchar('\n');
350                 return;
351         }
352
353         Debug_Putchar(' ');
354         switch(RetType)
355         {
356         case 'n':       Debug_Puts("NULL");     break;
357         case 'p':       Debug_Fmt("%p", &args); break;
358         case 's':       Debug_Fmt("'%s'", &args);       break;
359         case 'i':       Debug_Fmt("%i", &args); break;
360         case 'u':       Debug_Fmt("%u", &args); break;
361         case 'x':       Debug_Fmt("0x%x", &args);       break;
362         // Extended (64-Bit)
363         case 'X':       Debug_Fmt("0x%llx", &args);     break;
364         }
365         Debug_Putchar('\n');
366
367         va_end(args);
368 }
369
370 void Debug_HexDump(char *Header, void *Data, Uint Length)
371 {
372         Uint8   *cdat = Data;
373         Uint    pos = 0;
374         Debug_Puts(Header);
375         LogF(" (Hexdump of %p)\n", Data);
376
377         while(Length >= 16)
378         {
379                 #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
380                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
381                         " %02x %02x %02x %02x %02x %02x %02x %02x"
382                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
383                         pos,
384                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
385                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
386                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
387                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
388                         );
389                 Length -= 16;
390                 cdat += 16;
391                 pos += 16;
392         }
393
394         LogF("Log: %04x: ", pos);
395         while(Length)
396         {
397                 Uint    byte = *cdat;
398                 LogF("%02x ", byte);
399                 Length--;
400                 cdat ++;
401         }
402         Debug_Putchar('\n');
403 }
404
405 // --- EXPORTS ---
406 EXPORT(Log);
407 EXPORT(Warning);
408 EXPORT(Debug_Enter);
409 EXPORT(Debug_Log);
410 EXPORT(Debug_Leave);

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