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 static void Debug_Putchar(char ch)
57 if(giDebug_KTerm != -1)
58 VFS_Write(giDebug_KTerm, 1, &ch);
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;
71 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
72 outb(SERIAL_PORT, ch);
76 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
80 static void Debug_Puts(char *Str)
82 while(*Str) Debug_Putchar(*Str++);
85 void Debug_Fmt(const char *format, va_list *args)
89 char tmpBuf[34]; // For Integers
94 while((c = *format++) != 0)
96 // Non control character
112 Uint ptr = va_arg(*args, Uint);
113 Debug_Putchar('*'); Debug_Putchar('0'); Debug_Putchar('x');
115 itoa(p, ptr, 16, BITS/4, '0');
120 arg = va_arg(*args, Uint);
131 if('1' <= c && c <= '9')
134 while('0' <= c && c <= '9')
148 arg |= va_arg(*args, Uint);
159 if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
163 itoa(p, arg, 10, minSize, pad);
166 itoa(p, arg, 10, minSize, pad);
169 itoa(p, arg, 16, minSize, pad);
172 itoa(p, arg, 8, minSize, pad);
175 itoa(p, arg, 2, minSize, pad);
179 if(arg) Debug_Puts("True");
180 else Debug_Puts("False");
184 p = (char*)(Uint)arg;
187 while(len++ < minSize) Debug_Putchar(pad);
190 while(*p) Debug_Putchar(*p++);
193 // Single Character / Array
199 p = (char*)(Uint)arg;
200 if(!p) goto printString;
201 while(minSize--) Debug_Putchar(*p++);
212 * \fn void LogF(char *Msg, ...)
214 void LogF(char *Fmt, ...)
220 Debug_Fmt(Fmt, &args);
225 * \fn void Log(char *Msg, ...)
227 void Log(char *Fmt, ...)
233 Debug_Fmt(Fmt, &args);
237 void Warning(char *Fmt, ...)
240 Debug_Puts("Warning: ");
242 Debug_Fmt(Fmt, &args);
246 void Panic(char *Fmt, ...)
249 Debug_Puts("Panic: ");
251 Debug_Fmt(Fmt, &args);
257 __asm__ __volatile__ ("xchg %bx, %bx");
258 __asm__ __volatile__ ("cli;\n\thlt");
259 for(;;) __asm__ __volatile__ ("hlt");
262 void Debug_SetKTerminal(char *File)
264 if(giDebug_KTerm != -1)
265 VFS_Close(giDebug_KTerm);
266 giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
267 Log_Log("Debug", "Opened '%s' as 0x%x", File, giDebug_KTerm);
270 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
273 int i = gDebug_Level ++;
276 va_start(args, ArgTypes);
278 while(i--) Debug_Putchar(' ');
280 Debug_Puts(FuncName); Debug_Puts(": (");
284 pos = strpos(ArgTypes, ' ');
285 if(pos != -1) ArgTypes[pos] = '\0';
286 if(pos == -1 || pos > 1) {
287 Debug_Puts(ArgTypes+1);
290 if(pos != -1) ArgTypes[pos] = ' ';
293 case 'p': Debug_Fmt("%p", &args); break;
294 case 's': Debug_Fmt("'%s'", &args); break;
295 case 'i': Debug_Fmt("%i", &args); break;
296 case 'u': Debug_Fmt("%u", &args); break;
297 case 'x': Debug_Fmt("0x%x", &args); break;
298 case 'b': Debug_Fmt("0b%b", &args); break;
300 case 'X': Debug_Fmt("0x%llx", &args); break;
301 case 'B': Debug_Fmt("0b%llb", &args); break;
304 Debug_Putchar(','); Debug_Putchar(' ');
308 ArgTypes = &ArgTypes[pos+1];
312 Debug_Putchar(')'); Debug_Putchar('\n');
315 void Debug_Log(char *FuncName, char *Fmt, ...)
318 int i = gDebug_Level;
322 while(i--) Debug_Putchar(' ');
324 Debug_Puts(FuncName); Debug_Puts(": ");
325 Debug_Fmt(Fmt, &args);
331 void Debug_Leave(char *FuncName, char RetType, ...)
334 int i = --gDebug_Level;
336 va_start(args, RetType);
343 while(i--) Debug_Putchar(' ');
345 Debug_Puts(FuncName); Debug_Puts(": RETURN");
356 case 'n': Debug_Puts("NULL"); break;
357 case 'p': Debug_Fmt("%p", &args); break;
358 case 's': Debug_Fmt("'%s'", &args); break;
359 case 'i': Debug_Fmt("%i", &args); break;
360 case 'u': Debug_Fmt("%u", &args); break;
361 case 'x': Debug_Fmt("0x%x", &args); break;
363 case 'X': Debug_Fmt("0x%llx", &args); break;
370 void Debug_HexDump(char *Header, void *Data, Uint Length)
375 LogF(" (Hexdump of %p)\n", Data);
379 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
380 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
381 " %02x %02x %02x %02x %02x %02x %02x %02x"
382 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
384 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
385 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
386 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
387 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
394 LogF("Log: %04x: ", pos);