2 * AcessOS Microkernel Version
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT 0x3F8
11 #define GDB_SERIAL_PORT 0x2F8
14 extern void Threads_Dump(void);
15 extern void KernelPanic_SetMode(void);
16 extern void KernelPanic_PutChar(char Ch);
20 int giDebug_KTerm = -1;
21 int gbDebug_SerialSetup = 0;
22 int gbGDB_SerialSetup = 0;
23 int gbDebug_IsKPanic = 0;
26 int putDebugChar(char ch)
28 if(!gbGDB_SerialSetup) {
29 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
30 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
31 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
32 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
33 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
34 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
35 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
36 gbDebug_SerialSetup = 1;
38 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
39 outb(GDB_SERIAL_PORT, ch);
44 if(!gbGDB_SerialSetup) {
45 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
46 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
47 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
48 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
49 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
50 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
51 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
52 gbDebug_SerialSetup = 1;
54 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
55 return inb(GDB_SERIAL_PORT);
58 volatile int gbInPutChar = 0;
60 static void Debug_Putchar(char ch)
63 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
67 if(!gbDebug_SerialSetup) {
68 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
69 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
70 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
71 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
72 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
73 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
74 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
75 gbDebug_SerialSetup = 1;
77 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
78 outb(SERIAL_PORT, ch);
81 if( !gbDebug_IsKPanic )
83 if(gbInPutChar) return ;
85 if(giDebug_KTerm != -1)
86 VFS_Write(giDebug_KTerm, 1, &ch);
90 KernelPanic_PutChar(ch);
93 static void Debug_Puts(char *Str)
95 while(*Str) Debug_Putchar(*Str++);
98 void Debug_Fmt(const char *format, va_list *args)
101 int minSize = 0, len;
102 char tmpBuf[34]; // For Integers
108 while((c = *format++) != 0)
110 // Non control character
126 Uint ptr = va_arg(*args, Uint);
127 Debug_Putchar('*'); Debug_Putchar('0'); Debug_Putchar('x');
129 itoa(p, ptr, 16, BITS/4, '0');
134 arg = va_arg(*args, Uint);
136 // - Padding Side Flag
151 if('1' <= c && c <= '9')
154 while('0' <= c && c <= '9')
168 arg |= va_arg(*args, Uint);
179 if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
183 itoa(p, arg, 10, minSize, pad);
186 itoa(p, arg, 10, minSize, pad);
189 itoa(p, arg, 16, minSize, pad);
192 itoa(p, arg, 8, minSize, pad);
195 itoa(p, arg, 2, minSize, pad);
200 while(*p) Debug_Putchar(*p++);
204 if(arg) Debug_Puts("True");
205 else Debug_Puts("False");
209 p = (char*)(Uint)arg;
212 if( !bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
213 while(*p) Debug_Putchar(*p++);
214 if( bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
217 // Single Character / Array
223 p = (char*)(Uint)arg;
224 if(!p) goto printString;
225 while(minSize--) Debug_Putchar(*p++);
235 void Debug_KernelPanic()
237 gbDebug_IsKPanic = 1;
238 KernelPanic_SetMode();
242 * \fn void LogF(char *Msg, ...)
244 void LogF(char *Fmt, ...)
250 Debug_Fmt(Fmt, &args);
255 * \fn void Log(char *Msg, ...)
257 void Log(char *Fmt, ...)
263 Debug_Fmt(Fmt, &args);
267 void Warning(char *Fmt, ...)
270 Debug_Puts("Warning: ");
272 Debug_Fmt(Fmt, &args);
276 void Panic(char *Fmt, ...)
282 Debug_Puts("Panic: ");
284 Debug_Fmt(Fmt, &args);
290 __asm__ __volatile__ ("xchg %bx, %bx");
291 __asm__ __volatile__ ("cli;\n\thlt");
292 for(;;) __asm__ __volatile__ ("hlt");
295 void Debug_SetKTerminal(char *File)
298 if(giDebug_KTerm != -1) {
303 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
304 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
306 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
309 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
312 int i = gDebug_Level ++;
315 va_start(args, ArgTypes);
317 while(i--) Debug_Putchar(' ');
319 Debug_Puts(FuncName); Debug_Puts(": (");
323 pos = strpos(ArgTypes, ' ');
324 if(pos != -1) ArgTypes[pos] = '\0';
325 if(pos == -1 || pos > 1) {
326 Debug_Puts(ArgTypes+1);
329 if(pos != -1) ArgTypes[pos] = ' ';
332 case 'p': Debug_Fmt("%p", &args); break;
333 case 's': Debug_Fmt("'%s'", &args); break;
334 case 'i': Debug_Fmt("%i", &args); break;
335 case 'u': Debug_Fmt("%u", &args); break;
336 case 'x': Debug_Fmt("0x%x", &args); break;
337 case 'b': Debug_Fmt("0b%b", &args); break;
339 case 'X': Debug_Fmt("0x%llx", &args); break;
340 case 'B': Debug_Fmt("0b%llb", &args); break;
343 Debug_Putchar(','); Debug_Putchar(' ');
347 ArgTypes = &ArgTypes[pos+1];
351 Debug_Putchar(')'); Debug_Putchar('\n');
354 void Debug_Log(char *FuncName, char *Fmt, ...)
357 int i = gDebug_Level;
361 while(i--) Debug_Putchar(' ');
363 Debug_Puts(FuncName); Debug_Puts(": ");
364 Debug_Fmt(Fmt, &args);
370 void Debug_Leave(char *FuncName, char RetType, ...)
373 int i = --gDebug_Level;
375 va_start(args, RetType);
382 while(i--) Debug_Putchar(' ');
384 Debug_Puts(FuncName); Debug_Puts(": RETURN");
395 case 'n': Debug_Puts("NULL"); break;
396 case 'p': Debug_Fmt("%p", &args); break;
397 case 's': Debug_Fmt("'%s'", &args); break;
398 case 'i': Debug_Fmt("%i", &args); break;
399 case 'u': Debug_Fmt("%u", &args); break;
400 case 'x': Debug_Fmt("0x%x", &args); break;
402 case 'X': Debug_Fmt("0x%llx", &args); break;
409 void Debug_HexDump(char *Header, void *Data, Uint Length)
414 LogF(" (Hexdump of %p)\n", Data);
418 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
419 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
420 " %02x %02x %02x %02x %02x %02x %02x %02x"
421 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
423 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
424 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
425 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
426 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
433 LogF("Log: %04x: ", pos);