Improved padding code in Debug_Fmt
[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          int    bPadLeft = 0;
94
95         while((c = *format++) != 0)
96         {
97                 // Non control character
98                 if( c != '%' ) {
99                         Debug_Putchar(c);
100                         continue;
101                 }
102
103                 c = *format++;
104
105                 // Literal %
106                 if(c == '%') {
107                         Debug_Putchar('%');
108                         continue;
109                 }
110
111                 // Pointer
112                 if(c == 'p') {
113                         Uint    ptr = va_arg(*args, Uint);
114                         Debug_Putchar('*');     Debug_Putchar('0');     Debug_Putchar('x');
115                         p = tmpBuf;
116                         itoa(p, ptr, 16, BITS/4, '0');
117                         goto printString;
118                 }
119
120                 // Get Argument
121                 arg = va_arg(*args, Uint);
122
123                 // - Padding Side Flag
124                 if(c == '+') {
125                         bPadLeft = 1;
126                         c = *format++;
127                 }
128
129                 // Padding
130                 if(c == '0') {
131                         pad = '0';
132                         c = *format++;
133                 } else
134                         pad = ' ';
135
136                 // Minimum length
137                 minSize = 1;
138                 if('1' <= c && c <= '9')
139                 {
140                         minSize = 0;
141                         while('0' <= c && c <= '9')
142                         {
143                                 minSize *= 10;
144                                 minSize += c - '0';
145                                 c = *format++;
146                         }
147                 }
148
149                 // Long (default)
150                 isLongLong = 0;
151                 if(c == 'l') {
152                         c = *format++;
153                         if(c == 'l') {
154                                 #if BITS == 32
155                                 arg |= va_arg(*args, Uint);
156                                 #endif
157                                 c = *format++;
158                                 isLongLong = 1;
159                         }
160                 }
161
162                 p = tmpBuf;
163                 switch (c) {
164                 case 'd':
165                 case 'i':
166                         if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
167                                 Debug_Putchar('-');
168                                 arg = -arg;
169                         }
170                         itoa(p, arg, 10, minSize, pad);
171                         goto printString;
172                 case 'u':
173                         itoa(p, arg, 10, minSize, pad);
174                         goto printString;
175                 case 'x':
176                         itoa(p, arg, 16, minSize, pad);
177                         goto printString;
178                 case 'o':
179                         itoa(p, arg, 8, minSize, pad);
180                         goto printString;
181                 case 'b':
182                         itoa(p, arg, 2, minSize, pad);
183                         goto printString;
184
185                 printString:
186                         if(!p)          p = "(null)";
187                         while(*p)       Debug_Putchar(*p++);
188                         break;
189
190                 case 'B':       //Boolean
191                         if(arg) Debug_Puts("True");
192                         else    Debug_Puts("False");
193                         break;
194
195                 case 's':
196                         p = (char*)(Uint)arg;
197                         if(!p)          p = "(null)";
198                         len = strlen(p);
199                         if( !bPadLeft ) while(len++ < minSize)  Debug_Putchar(pad);
200                         while(*p)       Debug_Putchar(*p++);
201                         if( bPadLeft )  while(len++ < minSize)  Debug_Putchar(pad);
202                         break;
203
204                 // Single Character / Array
205                 case 'c':
206                         if(minSize == 1) {
207                                 Debug_Putchar(arg);
208                                 break;
209                         }
210                         p = (char*)(Uint)arg;
211                         if(!p)  goto printString;
212                         while(minSize--)        Debug_Putchar(*p++);
213                         break;
214
215                 default:
216                         Debug_Putchar(arg);
217                         break;
218                 }
219     }
220 }
221
222 /**
223  * \fn void LogF(char *Msg, ...)
224  */
225 void LogF(char *Fmt, ...)
226 {
227         va_list args;
228
229         va_start(args, Fmt);
230
231         Debug_Fmt(Fmt, &args);
232
233         va_end(args);
234 }
235 /**
236  * \fn void Log(char *Msg, ...)
237  */
238 void Log(char *Fmt, ...)
239 {
240         va_list args;
241
242         Debug_Puts("Log: ");
243         va_start(args, Fmt);
244         Debug_Fmt(Fmt, &args);
245         va_end(args);
246         Debug_Putchar('\n');
247 }
248 void Warning(char *Fmt, ...)
249 {
250         va_list args;
251         Debug_Puts("Warning: ");
252         va_start(args, Fmt);
253         Debug_Fmt(Fmt, &args);
254         va_end(args);
255         Debug_Putchar('\n');
256 }
257 void Panic(char *Fmt, ...)
258 {
259         va_list args;
260         Debug_Puts("Panic: ");
261         va_start(args, Fmt);
262         Debug_Fmt(Fmt, &args);
263         va_end(args);
264         Debug_Putchar('\n');
265
266         Threads_Dump();
267
268         __asm__ __volatile__ ("xchg %bx, %bx");
269         __asm__ __volatile__ ("cli;\n\thlt");
270         for(;;) __asm__ __volatile__ ("hlt");
271 }
272
273 void Debug_SetKTerminal(char *File)
274 {
275         if(giDebug_KTerm != -1)
276                 VFS_Close(giDebug_KTerm);
277         giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
278         Log_Log("Debug", "Opened '%s' as 0x%x", File, giDebug_KTerm);
279 }
280
281 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
282 {
283         va_list args;
284          int    i = gDebug_Level ++;
285          int    pos;
286
287         va_start(args, ArgTypes);
288
289         while(i--)      Debug_Putchar(' ');
290
291         Debug_Puts(FuncName);   Debug_Puts(": (");
292
293         while(*ArgTypes)
294         {
295                 pos = strpos(ArgTypes, ' ');
296                 if(pos != -1)   ArgTypes[pos] = '\0';
297                 if(pos == -1 || pos > 1) {
298                         Debug_Puts(ArgTypes+1);
299                         Debug_Putchar('=');
300                 }
301                 if(pos != -1)   ArgTypes[pos] = ' ';
302                 switch(*ArgTypes)
303                 {
304                 case 'p':       Debug_Fmt("%p", &args); break;
305                 case 's':       Debug_Fmt("'%s'", &args);       break;
306                 case 'i':       Debug_Fmt("%i", &args); break;
307                 case 'u':       Debug_Fmt("%u", &args); break;
308                 case 'x':       Debug_Fmt("0x%x", &args);       break;
309                 case 'b':       Debug_Fmt("0b%b", &args);       break;
310                 // Extended (64-Bit)
311                 case 'X':       Debug_Fmt("0x%llx", &args);     break;
312                 case 'B':       Debug_Fmt("0b%llb", &args);     break;
313                 }
314                 if(pos != -1) {
315                         Debug_Putchar(',');     Debug_Putchar(' ');
316                 }
317
318                 if(pos == -1)   break;
319                 ArgTypes = &ArgTypes[pos+1];
320         }
321
322         va_end(args);
323         Debug_Putchar(')');     Debug_Putchar('\n');
324 }
325
326 void Debug_Log(char *FuncName, char *Fmt, ...)
327 {
328         va_list args;
329          int    i = gDebug_Level;
330
331         va_start(args, Fmt);
332
333         while(i--)      Debug_Putchar(' ');
334
335         Debug_Puts(FuncName);   Debug_Puts(": ");
336         Debug_Fmt(Fmt, &args);
337
338         va_end(args);
339         Debug_Putchar('\n');
340 }
341
342 void Debug_Leave(char *FuncName, char RetType, ...)
343 {
344         va_list args;
345          int    i = --gDebug_Level;
346
347         va_start(args, RetType);
348
349         if( i == -1 ) {
350                 gDebug_Level = 0;
351                 i = 0;
352         }
353         // Indenting
354         while(i--)      Debug_Putchar(' ');
355
356         Debug_Puts(FuncName);   Debug_Puts(": RETURN");
357
358         // No Return
359         if(RetType == '-') {
360                 Debug_Putchar('\n');
361                 return;
362         }
363
364         Debug_Putchar(' ');
365         switch(RetType)
366         {
367         case 'n':       Debug_Puts("NULL");     break;
368         case 'p':       Debug_Fmt("%p", &args); break;
369         case 's':       Debug_Fmt("'%s'", &args);       break;
370         case 'i':       Debug_Fmt("%i", &args); break;
371         case 'u':       Debug_Fmt("%u", &args); break;
372         case 'x':       Debug_Fmt("0x%x", &args);       break;
373         // Extended (64-Bit)
374         case 'X':       Debug_Fmt("0x%llx", &args);     break;
375         }
376         Debug_Putchar('\n');
377
378         va_end(args);
379 }
380
381 void Debug_HexDump(char *Header, void *Data, Uint Length)
382 {
383         Uint8   *cdat = Data;
384         Uint    pos = 0;
385         Debug_Puts(Header);
386         LogF(" (Hexdump of %p)\n", Data);
387
388         while(Length >= 16)
389         {
390                 #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
391                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
392                         " %02x %02x %02x %02x %02x %02x %02x %02x"
393                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
394                         pos,
395                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
396                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
397                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
398                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
399                         );
400                 Length -= 16;
401                 cdat += 16;
402                 pos += 16;
403         }
404
405         LogF("Log: %04x: ", pos);
406         while(Length)
407         {
408                 Uint    byte = *cdat;
409                 LogF("%02x ", byte);
410                 Length--;
411                 cdat ++;
412         }
413         Debug_Putchar('\n');
414 }
415
416 // --- EXPORTS ---
417 EXPORT(Log);
418 EXPORT(Warning);
419 EXPORT(Debug_Enter);
420 EXPORT(Debug_Log);
421 EXPORT(Debug_Leave);

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