2 * AcessOS Microkernel Version
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT 0x3F8
11 #define GDB_SERIAL_PORT 0x2F8
12 #define DEBUG_USE_VSNPRINTF 1
13 #define DEBUG_MAX_LINE_LEN 256
16 extern void Threads_Dump(void);
17 extern void KernelPanic_SetMode(void);
18 extern void KernelPanic_PutChar(char Ch);
21 int putDebugChar(char ch);
23 static void Debug_Putchar(char ch);
24 static void Debug_Puts(char *Str);
25 void Debug_Fmt(const char *format, va_list args);
29 int giDebug_KTerm = -1;
30 int gbDebug_SerialSetup = 0;
31 int gbGDB_SerialSetup = 0;
32 int gbDebug_IsKPanic = 0;
33 volatile int gbInPutChar = 0;
36 int putDebugChar(char ch)
38 if(!gbGDB_SerialSetup) {
39 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
40 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
41 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
42 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
43 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
44 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
45 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
46 gbDebug_SerialSetup = 1;
48 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
49 outb(GDB_SERIAL_PORT, ch);
54 if(!gbGDB_SerialSetup) {
55 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
56 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
57 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
58 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
59 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
60 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
61 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
62 gbDebug_SerialSetup = 1;
64 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
65 return inb(GDB_SERIAL_PORT);
68 static void Debug_Putchar(char ch)
71 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
75 if(!gbDebug_SerialSetup) {
76 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
77 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
78 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
79 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
80 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
81 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
82 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
83 gbDebug_SerialSetup = 1;
85 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
86 outb(SERIAL_PORT, ch);
89 if( !gbDebug_IsKPanic )
91 if(gbInPutChar) return ;
93 if(giDebug_KTerm != -1)
94 VFS_Write(giDebug_KTerm, 1, &ch);
98 KernelPanic_PutChar(ch);
101 static void Debug_Puts(char *Str)
107 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a" ((Uint8)*Str) );
111 if(!gbDebug_SerialSetup) {
112 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
113 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
114 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
115 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
116 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
117 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
118 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
119 gbDebug_SerialSetup = 1;
121 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
122 outb(SERIAL_PORT, *Str);
125 if( gbDebug_IsKPanic )
126 KernelPanic_PutChar(*Str);
133 if( !gbDebug_IsKPanic && giDebug_KTerm != -1)
135 if(gbInPutChar) return ;
137 VFS_Write(giDebug_KTerm, len, Str);
142 void Debug_Fmt(const char *format, va_list args)
144 #if DEBUG_USE_VSNPRINTF
145 char buf[DEBUG_MAX_LINE_LEN];
147 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
148 //if( len < DEBUG_MAX_LINE )
154 int minSize = 0, len;
155 char tmpBuf[34]; // For Integers
161 while((c = *format++) != 0)
163 // Non control character
179 Uint ptr = va_arg(*args, Uint);
180 Debug_Putchar('*'); Debug_Putchar('0'); Debug_Putchar('x');
182 itoa(p, ptr, 16, BITS/4, '0');
187 arg = va_arg(*args, Uint);
189 // - Padding Side Flag
204 if('1' <= c && c <= '9')
207 while('0' <= c && c <= '9')
221 arg |= va_arg(*args, Uint);
232 if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
236 itoa(p, arg, 10, minSize, pad);
239 itoa(p, arg, 10, minSize, pad);
242 itoa(p, arg, 16, minSize, pad);
245 itoa(p, arg, 8, minSize, pad);
248 itoa(p, arg, 2, minSize, pad);
253 while(*p) Debug_Putchar(*p++);
257 if(arg) Debug_Puts("True");
258 else Debug_Puts("False");
262 p = (char*)(Uint)arg;
265 if( !bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
266 while(*p) Debug_Putchar(*p++);
267 if( bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
270 // Single Character / Array
276 p = (char*)(Uint)arg;
277 if(!p) goto printString;
278 while(minSize--) Debug_Putchar(*p++);
289 void Debug_KernelPanic()
291 gbDebug_IsKPanic = 1;
292 KernelPanic_SetMode();
296 * \fn void LogF(char *Msg, ...)
298 void LogF(char *Fmt, ...)
304 Debug_Fmt(Fmt, args);
309 * \fn void Log(char *Msg, ...)
311 void Log(char *Fmt, ...)
317 Debug_Fmt(Fmt, args);
321 void Warning(char *Fmt, ...)
324 Debug_Puts("Warning: ");
326 Debug_Fmt(Fmt, args);
330 void Panic(char *Fmt, ...)
336 Debug_Puts("Panic: ");
338 Debug_Fmt(Fmt, args);
344 __asm__ __volatile__ ("xchg %bx, %bx");
345 __asm__ __volatile__ ("cli;\n\thlt");
346 for(;;) __asm__ __volatile__ ("hlt");
349 void Debug_SetKTerminal(char *File)
352 if(giDebug_KTerm != -1) {
357 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
358 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
360 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
363 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
366 int i = gDebug_Level ++;
369 va_start(args, ArgTypes);
371 while(i--) Debug_Putchar(' ');
373 Debug_Puts(FuncName); Debug_Puts(": (");
377 pos = strpos(ArgTypes, ' ');
378 if(pos != -1) ArgTypes[pos] = '\0';
379 if(pos == -1 || pos > 1) {
380 Debug_Puts(ArgTypes+1);
383 if(pos != -1) ArgTypes[pos] = ' ';
386 case 'p': Debug_Fmt("%p", args); break;
387 case 's': Debug_Fmt("'%s'", args); break;
388 case 'i': Debug_Fmt("%i", args); break;
389 case 'u': Debug_Fmt("%u", args); break;
390 case 'x': Debug_Fmt("0x%x", args); break;
391 case 'b': Debug_Fmt("0b%b", args); break;
393 case 'X': Debug_Fmt("0x%llx", args); break;
394 case 'B': Debug_Fmt("0b%llb", args); break;
397 Debug_Putchar(','); Debug_Putchar(' ');
401 ArgTypes = &ArgTypes[pos+1];
405 Debug_Putchar(')'); Debug_Putchar('\n');
408 void Debug_Log(char *FuncName, char *Fmt, ...)
411 int i = gDebug_Level;
415 while(i--) Debug_Putchar(' ');
417 Debug_Puts(FuncName); Debug_Puts(": ");
418 Debug_Fmt(Fmt, args);
424 void Debug_Leave(char *FuncName, char RetType, ...)
427 int i = --gDebug_Level;
429 va_start(args, RetType);
436 while(i--) Debug_Putchar(' ');
438 Debug_Puts(FuncName); Debug_Puts(": RETURN");
449 case 'n': Debug_Puts("NULL"); break;
450 case 'p': Debug_Fmt("%p", args); break;
451 case 's': Debug_Fmt("'%s'", args); break;
452 case 'i': Debug_Fmt("%i", args); break;
453 case 'u': Debug_Fmt("%u", args); break;
454 case 'x': Debug_Fmt("0x%x", args); break;
456 case 'X': Debug_Fmt("0x%llx", args); break;
463 void Debug_HexDump(char *Header, void *Data, Uint Length)
468 LogF(" (Hexdump of %p)\n", Data);
472 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
473 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
474 " %02x %02x %02x %02x %02x %02x %02x %02x"
475 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
477 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
478 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
479 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
480 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
487 LogF("Log: %04x: ", pos);