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 default: E9(arg); break;
162 * \fn void LogV(char *Fmt, va_list Args)
164 void LogV(char *Fmt, va_list Args)
171 * \fn void LogF(char *Msg, ...)
173 void LogF(char *Fmt, ...)
184 * \fn void Log(char *Msg, ...)
186 void Log(char *Fmt, ...)
196 void Warning(char *Fmt, ...)
205 void Panic(char *Fmt, ...)
216 __asm__ __volatile__ ("xchg %bx, %bx");
217 __asm__ __volatile__ ("cli;\n\thlt");
218 for(;;) __asm__ __volatile__ ("hlt");
221 void Debug_SetKTerminal(char *File)
223 if(giDebug_KTerm != -1)
224 VFS_Close(giDebug_KTerm);
225 giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
226 Log("Opened '%s' as %i\n", File, giDebug_KTerm);
229 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
232 int i = gDebug_Level ++;
235 va_start(args, ArgTypes);
239 E9_Str(FuncName); E9_Str(": (");
243 pos = strpos(ArgTypes, ' ');
244 if(pos != -1) ArgTypes[pos] = '\0';
245 if(pos == -1 || pos > 1) {
249 if(pos != -1) ArgTypes[pos] = ' ';
252 case 'p': E9_Fmt("%p", &args); break;
253 case 's': E9_Fmt("'%s'", &args); break;
254 case 'i': E9_Fmt("%i", &args); break;
255 case 'u': E9_Fmt("%u", &args); break;
256 case 'x': E9_Fmt("0x%x", &args); break;
257 case 'b': E9_Fmt("0b%b", &args); break;
259 case 'X': E9_Fmt("0x%llx", &args); break;
260 case 'B': E9_Fmt("0b%llb", &args); break;
267 ArgTypes = &ArgTypes[pos+1];
274 void Debug_Log(char *FuncName, char *Fmt, ...)
277 int i = gDebug_Level;
283 E9_Str(FuncName); E9_Str(": ");
290 void Debug_Leave(char *FuncName, char RetType, ...)
293 int i = --gDebug_Level;
295 va_start(args, RetType);
300 E9_Str(FuncName); E9_Str(": RETURN");
311 case 'n': E9_Str("NULL"); break;
312 case 'p': E9_Fmt("%p", &args); break;
313 case 's': E9_Fmt("'%s'", &args); break;
314 case 'i': E9_Fmt("%i", &args); break;
315 case 'u': E9_Fmt("%u", &args); break;
316 case 'x': E9_Fmt("0x%x", &args); break;
318 case 'X': E9_Fmt("0x%llx", &args); break;
325 void Debug_HexDump(char *Header, void *Data, Uint Length)
330 LogF(" (Hexdump of %p)\n", Data);
334 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
336 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
337 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15]
344 LogF("Log: %04x: ", pos);