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 0
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 buf[DEBUG_MAX_LINE_LEN-1] = 0;
148 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
149 //if( len < DEBUG_MAX_LINE )
155 int minSize = 0, len;
156 char tmpBuf[34]; // For Integers
162 while((c = *format++) != 0)
164 // Non control character
180 Uint ptr = va_arg(args, Uint);
181 Debug_Putchar('*'); Debug_Putchar('0'); Debug_Putchar('x');
183 itoa(p, ptr, 16, BITS/4, '0');
188 arg = va_arg(args, Uint);
190 // - Padding Side Flag
205 if('1' <= c && c <= '9')
208 while('0' <= c && c <= '9')
222 arg |= va_arg(args, Uint);
233 if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
237 itoa(p, arg, 10, minSize, pad);
240 itoa(p, arg, 10, minSize, pad);
243 itoa(p, arg, 16, minSize, pad);
246 itoa(p, arg, 8, minSize, pad);
249 itoa(p, arg, 2, minSize, pad);
254 while(*p) Debug_Putchar(*p++);
258 if(arg) Debug_Puts("True");
259 else Debug_Puts("False");
263 p = (char*)(Uint)arg;
266 if( !bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
267 while(*p) Debug_Putchar(*p++);
268 if( bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
271 // Single Character / Array
277 p = (char*)(Uint)arg;
278 if(!p) goto printString;
279 while(minSize--) Debug_Putchar(*p++);
290 void Debug_KernelPanic()
292 gbDebug_IsKPanic = 1;
293 KernelPanic_SetMode();
297 * \fn void LogF(char *Msg, ...)
299 void LogF(char *Fmt, ...)
305 Debug_Fmt(Fmt, args);
310 * \fn void Log(char *Msg, ...)
312 void Log(char *Fmt, ...)
318 Debug_Fmt(Fmt, args);
322 void Warning(char *Fmt, ...)
325 Debug_Puts("Warning: ");
327 Debug_Fmt(Fmt, args);
331 void Panic(char *Fmt, ...)
337 Debug_Puts("Panic: ");
339 Debug_Fmt(Fmt, args);
345 __asm__ __volatile__ ("xchg %bx, %bx");
346 __asm__ __volatile__ ("cli;\n\thlt");
347 for(;;) __asm__ __volatile__ ("hlt");
350 void Debug_SetKTerminal(char *File)
353 if(giDebug_KTerm != -1) {
358 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
359 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
361 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
364 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
367 int i = gDebug_Level ++;
370 va_start(args, ArgTypes);
372 while(i--) Debug_Putchar(' ');
374 Debug_Puts(FuncName); Debug_Puts(": (");
378 pos = strpos(ArgTypes, ' ');
379 if(pos != -1) ArgTypes[pos] = '\0';
380 if(pos == -1 || pos > 1) {
381 Debug_Puts(ArgTypes+1);
384 if(pos != -1) ArgTypes[pos] = ' ';
387 case 'p': Debug_Fmt("%p", args); break;
388 case 's': Debug_Fmt("'%s'", args); break;
389 case 'i': Debug_Fmt("%i", args); break;
390 case 'u': Debug_Fmt("%u", args); break;
391 case 'x': Debug_Fmt("0x%x", args); break;
392 case 'b': Debug_Fmt("0b%b", args); break;
393 case 'X': Debug_Fmt("0x%llx", args); break; // Extended (64-Bit)
394 case 'B': Debug_Fmt("0b%llb", args); break; // Extended (64-Bit)
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);