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();
18 int giDebug_KTerm = -1;
19 int gbDebug_SerialSetup = 0;
20 int gbGDB_SerialSetup = 0;
23 int putDebugChar(char ch)
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;
35 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
36 outb(GDB_SERIAL_PORT, ch);
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;
51 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
52 return inb(GDB_SERIAL_PORT);
55 volatile int gbInPutChar = 0;
57 static void Debug_Putchar(char ch)
60 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
64 if(!gbDebug_SerialSetup) {
65 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
66 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
67 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
68 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
69 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
70 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
71 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
72 gbDebug_SerialSetup = 1;
74 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
75 outb(SERIAL_PORT, ch);
78 if(gbInPutChar) return ;
80 if(giDebug_KTerm != -1)
81 VFS_Write(giDebug_KTerm, 1, &ch);
85 static void Debug_Puts(char *Str)
87 while(*Str) Debug_Putchar(*Str++);
90 void Debug_Fmt(const char *format, va_list *args)
94 char tmpBuf[34]; // For Integers
100 while((c = *format++) != 0)
102 // Non control character
118 Uint ptr = va_arg(*args, Uint);
119 Debug_Putchar('*'); Debug_Putchar('0'); Debug_Putchar('x');
121 itoa(p, ptr, 16, BITS/4, '0');
126 arg = va_arg(*args, Uint);
128 // - Padding Side Flag
143 if('1' <= c && c <= '9')
146 while('0' <= c && c <= '9')
160 arg |= va_arg(*args, Uint);
171 if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
175 itoa(p, arg, 10, minSize, pad);
178 itoa(p, arg, 10, minSize, pad);
181 itoa(p, arg, 16, minSize, pad);
184 itoa(p, arg, 8, minSize, pad);
187 itoa(p, arg, 2, minSize, pad);
192 while(*p) Debug_Putchar(*p++);
196 if(arg) Debug_Puts("True");
197 else Debug_Puts("False");
201 p = (char*)(Uint)arg;
204 if( !bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
205 while(*p) Debug_Putchar(*p++);
206 if( bPadLeft ) while(len++ < minSize) Debug_Putchar(pad);
209 // Single Character / Array
215 p = (char*)(Uint)arg;
216 if(!p) goto printString;
217 while(minSize--) Debug_Putchar(*p++);
228 * \fn void LogF(char *Msg, ...)
230 void LogF(char *Fmt, ...)
236 Debug_Fmt(Fmt, &args);
241 * \fn void Log(char *Msg, ...)
243 void Log(char *Fmt, ...)
249 Debug_Fmt(Fmt, &args);
253 void Warning(char *Fmt, ...)
256 Debug_Puts("Warning: ");
258 Debug_Fmt(Fmt, &args);
262 void Panic(char *Fmt, ...)
265 Debug_Puts("Panic: ");
267 Debug_Fmt(Fmt, &args);
273 __asm__ __volatile__ ("xchg %bx, %bx");
274 __asm__ __volatile__ ("cli;\n\thlt");
275 for(;;) __asm__ __volatile__ ("hlt");
278 void Debug_SetKTerminal(char *File)
281 if(giDebug_KTerm != -1) {
286 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
287 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
289 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
292 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
295 int i = gDebug_Level ++;
298 va_start(args, ArgTypes);
300 while(i--) Debug_Putchar(' ');
302 Debug_Puts(FuncName); Debug_Puts(": (");
306 pos = strpos(ArgTypes, ' ');
307 if(pos != -1) ArgTypes[pos] = '\0';
308 if(pos == -1 || pos > 1) {
309 Debug_Puts(ArgTypes+1);
312 if(pos != -1) ArgTypes[pos] = ' ';
315 case 'p': Debug_Fmt("%p", &args); break;
316 case 's': Debug_Fmt("'%s'", &args); break;
317 case 'i': Debug_Fmt("%i", &args); break;
318 case 'u': Debug_Fmt("%u", &args); break;
319 case 'x': Debug_Fmt("0x%x", &args); break;
320 case 'b': Debug_Fmt("0b%b", &args); break;
322 case 'X': Debug_Fmt("0x%llx", &args); break;
323 case 'B': Debug_Fmt("0b%llb", &args); break;
326 Debug_Putchar(','); Debug_Putchar(' ');
330 ArgTypes = &ArgTypes[pos+1];
334 Debug_Putchar(')'); Debug_Putchar('\n');
337 void Debug_Log(char *FuncName, char *Fmt, ...)
340 int i = gDebug_Level;
344 while(i--) Debug_Putchar(' ');
346 Debug_Puts(FuncName); Debug_Puts(": ");
347 Debug_Fmt(Fmt, &args);
353 void Debug_Leave(char *FuncName, char RetType, ...)
356 int i = --gDebug_Level;
358 va_start(args, RetType);
365 while(i--) Debug_Putchar(' ');
367 Debug_Puts(FuncName); Debug_Puts(": RETURN");
378 case 'n': Debug_Puts("NULL"); break;
379 case 'p': Debug_Fmt("%p", &args); break;
380 case 's': Debug_Fmt("'%s'", &args); break;
381 case 'i': Debug_Fmt("%i", &args); break;
382 case 'u': Debug_Fmt("%u", &args); break;
383 case 'x': Debug_Fmt("0x%x", &args); break;
385 case 'X': Debug_Fmt("0x%llx", &args); break;
392 void Debug_HexDump(char *Header, void *Data, Uint Length)
397 LogF(" (Hexdump of %p)\n", Data);
401 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
402 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
403 " %02x %02x %02x %02x %02x %02x %02x %02x"
404 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
406 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
407 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
408 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
409 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
416 LogF("Log: %04x: ", pos);