2 * AcessOS Microkernel Version
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT 0x3F8
13 extern void Threads_Dump();
17 int giDebug_KTerm = -1;
18 int gbDebug_SerialSetup = 0;
21 static void E9(char ch)
23 if(giDebug_KTerm != -1)
24 VFS_Write(giDebug_KTerm, 1, &ch);
27 if(!gbDebug_SerialSetup) {
28 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
29 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
30 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
31 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
32 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
33 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
34 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
35 gbDebug_SerialSetup = 1;
37 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
38 outb(SERIAL_PORT, ch);
42 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
46 static void E9_Str(char *Str)
48 while(*Str) E9(*Str++);
51 void E9_Fmt(const char *format, va_list *args)
55 char tmpBuf[34]; // For Integers
60 while((c = *format++) != 0)
62 // Non control character
78 Uint ptr = va_arg(*args, Uint);
79 E9('*'); E9('0'); E9('x');
81 itoa(p, ptr, 16, BITS/4, '0');
86 arg = va_arg(*args, Uint);
97 if('1' <= c && c <= '9')
100 while('0' <= c && c <= '9')
114 arg |= va_arg(*args, Uint);
125 if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
129 itoa(p, arg, 10, minSize, pad);
132 itoa(p, arg, 10, minSize, pad);
135 itoa(p, arg, 16, minSize, pad);
138 itoa(p, arg, 8, minSize, pad);
141 itoa(p, arg, 2, minSize, pad);
145 if(arg) E9_Str("True");
146 else E9_Str("False");
150 p = (char*)(Uint)arg;
156 // Single Character / Array
162 p = (char*)(Uint)arg;
163 if(!p) goto printString;
164 while(minSize--) E9(*p++);
167 default: E9(arg); break;
173 * \fn void LogV(char *Fmt, va_list Args)
175 void LogV(char *Fmt, va_list Args)
182 * \fn void LogF(char *Msg, ...)
184 void LogF(char *Fmt, ...)
195 * \fn void Log(char *Msg, ...)
197 void Log(char *Fmt, ...)
207 void Warning(char *Fmt, ...)
216 void Panic(char *Fmt, ...)
227 __asm__ __volatile__ ("xchg %bx, %bx");
228 __asm__ __volatile__ ("cli;\n\thlt");
229 for(;;) __asm__ __volatile__ ("hlt");
232 void Debug_SetKTerminal(char *File)
234 if(giDebug_KTerm != -1)
235 VFS_Close(giDebug_KTerm);
236 giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
237 Log("Opened '%s' as 0x%x", File, giDebug_KTerm);
240 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
243 int i = gDebug_Level ++;
246 va_start(args, ArgTypes);
250 E9_Str(FuncName); E9_Str(": (");
254 pos = strpos(ArgTypes, ' ');
255 if(pos != -1) ArgTypes[pos] = '\0';
256 if(pos == -1 || pos > 1) {
260 if(pos != -1) ArgTypes[pos] = ' ';
263 case 'p': E9_Fmt("%p", &args); break;
264 case 's': E9_Fmt("'%s'", &args); break;
265 case 'i': E9_Fmt("%i", &args); break;
266 case 'u': E9_Fmt("%u", &args); break;
267 case 'x': E9_Fmt("0x%x", &args); break;
268 case 'b': E9_Fmt("0b%b", &args); break;
270 case 'X': E9_Fmt("0x%llx", &args); break;
271 case 'B': E9_Fmt("0b%llb", &args); break;
278 ArgTypes = &ArgTypes[pos+1];
285 void Debug_Log(char *FuncName, char *Fmt, ...)
288 int i = gDebug_Level;
294 E9_Str(FuncName); E9_Str(": ");
301 void Debug_Leave(char *FuncName, char RetType, ...)
304 int i = --gDebug_Level;
306 va_start(args, RetType);
311 E9_Str(FuncName); E9_Str(": RETURN");
322 case 'n': E9_Str("NULL"); break;
323 case 'p': E9_Fmt("%p", &args); break;
324 case 's': E9_Fmt("'%s'", &args); break;
325 case 'i': E9_Fmt("%i", &args); break;
326 case 'u': E9_Fmt("%u", &args); break;
327 case 'x': E9_Fmt("0x%x", &args); break;
329 case 'X': E9_Fmt("0x%llx", &args); break;
336 void Debug_HexDump(char *Header, void *Data, Uint Length)
341 LogF(" (Hexdump of %p)\n", Data);
345 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
347 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
348 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15]
355 LogF("Log: %04x: ", pos);