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 E9(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 E9_Str(char *Str)
82 while(*Str) E9(*Str++);
85 void E9_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 E9('*'); E9('0'); E9('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) E9_Str("True");
180 else E9_Str("False");
184 p = (char*)(Uint)arg;
190 // Single Character / Array
196 p = (char*)(Uint)arg;
197 if(!p) goto printString;
198 while(minSize--) E9(*p++);
201 default: E9(arg); break;
207 * \fn void LogV(char *Fmt, va_list Args)
209 void LogV(char *Fmt, va_list Args)
216 * \fn void LogF(char *Msg, ...)
218 void LogF(char *Fmt, ...)
229 * \fn void Log(char *Msg, ...)
231 void Log(char *Fmt, ...)
241 void Warning(char *Fmt, ...)
250 void Panic(char *Fmt, ...)
261 __asm__ __volatile__ ("xchg %bx, %bx");
262 __asm__ __volatile__ ("cli;\n\thlt");
263 for(;;) __asm__ __volatile__ ("hlt");
266 void Debug_SetKTerminal(char *File)
268 if(giDebug_KTerm != -1)
269 VFS_Close(giDebug_KTerm);
270 giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
271 Log("Opened '%s' as 0x%x", File, giDebug_KTerm);
274 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
277 int i = gDebug_Level ++;
280 va_start(args, ArgTypes);
284 E9_Str(FuncName); E9_Str(": (");
288 pos = strpos(ArgTypes, ' ');
289 if(pos != -1) ArgTypes[pos] = '\0';
290 if(pos == -1 || pos > 1) {
294 if(pos != -1) ArgTypes[pos] = ' ';
297 case 'p': E9_Fmt("%p", &args); break;
298 case 's': E9_Fmt("'%s'", &args); break;
299 case 'i': E9_Fmt("%i", &args); break;
300 case 'u': E9_Fmt("%u", &args); break;
301 case 'x': E9_Fmt("0x%x", &args); break;
302 case 'b': E9_Fmt("0b%b", &args); break;
304 case 'X': E9_Fmt("0x%llx", &args); break;
305 case 'B': E9_Fmt("0b%llb", &args); break;
312 ArgTypes = &ArgTypes[pos+1];
319 void Debug_Log(char *FuncName, char *Fmt, ...)
322 int i = gDebug_Level;
328 E9_Str(FuncName); E9_Str(": ");
335 void Debug_Leave(char *FuncName, char RetType, ...)
338 int i = --gDebug_Level;
340 va_start(args, RetType);
349 E9_Str(FuncName); E9_Str(": RETURN");
360 case 'n': E9_Str("NULL"); break;
361 case 'p': E9_Fmt("%p", &args); break;
362 case 's': E9_Fmt("'%s'", &args); break;
363 case 'i': E9_Fmt("%i", &args); break;
364 case 'u': E9_Fmt("%u", &args); break;
365 case 'x': E9_Fmt("0x%x", &args); break;
367 case 'X': E9_Fmt("0x%llx", &args); break;
374 void Debug_HexDump(char *Header, void *Data, Uint Length)
379 LogF(" (Hexdump of %p)\n", Data);
383 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
385 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
386 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15]
393 LogF("Log: %04x: ", pos);