2 * AcessOS Microkernel Version
5 * TODO: Move the Debug_putchar methods out to the arch/ tree
11 #define DEBUG_TO_SERIAL 1
12 #define SERIAL_PORT 0x3F8
13 #define GDB_SERIAL_PORT 0x2F8
14 #define DEBUG_MAX_LINE_LEN 256
16 #define LOCK_DEBUG_OUTPUT 1
19 extern void Threads_Dump(void);
20 extern void KernelPanic_SetMode(void);
21 extern void KernelPanic_PutChar(char Ch);
24 int putDebugChar(char ch);
25 int getDebugChar(void);
26 static void Debug_Putchar(char ch);
27 static void Debug_Puts(int DbgOnly, char *Str);
28 void Debug_Fmt(const char *format, va_list args);
32 int giDebug_KTerm = -1;
33 int gbDebug_SerialSetup = 0;
34 int gbGDB_SerialSetup = 0;
35 int gbDebug_IsKPanic = 0;
36 volatile int gbInPutChar = 0;
38 tShortSpinlock glDebug_Lock;
42 int putDebugChar(char ch)
44 if(!gbGDB_SerialSetup) {
45 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
46 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
47 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
48 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
49 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
50 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
51 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
52 gbDebug_SerialSetup = 1;
54 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
55 outb(GDB_SERIAL_PORT, ch);
58 int getDebugChar(void)
60 if(!gbGDB_SerialSetup) {
61 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
62 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
63 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
64 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
65 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
66 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
67 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
68 gbDebug_SerialSetup = 1;
70 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
71 return inb(GDB_SERIAL_PORT);
74 static void Debug_PutCharDebug(char ch)
77 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
81 if(!gbDebug_SerialSetup) {
82 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
83 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
84 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
85 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
86 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
87 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
88 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
89 gbDebug_SerialSetup = 1;
91 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
92 outb(SERIAL_PORT, ch);
96 static void Debug_Putchar(char ch)
98 Debug_PutCharDebug(ch);
99 if( !gbDebug_IsKPanic )
101 if(gbInPutChar) return ;
103 if(giDebug_KTerm != -1)
104 VFS_Write(giDebug_KTerm, 1, &ch);
108 KernelPanic_PutChar(ch);
111 static void Debug_Puts(int UseKTerm, char *Str)
116 Debug_PutCharDebug( *Str );
118 if( gbDebug_IsKPanic )
119 KernelPanic_PutChar(*Str);
126 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
128 if(gbInPutChar) return ;
130 VFS_Write(giDebug_KTerm, len, Str);
135 void Debug_DbgOnlyFmt(const char *format, va_list args)
137 char buf[DEBUG_MAX_LINE_LEN];
139 buf[DEBUG_MAX_LINE_LEN-1] = 0;
140 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
141 //if( len < DEBUG_MAX_LINE )
146 void Debug_Fmt(const char *format, va_list args)
148 char buf[DEBUG_MAX_LINE_LEN];
150 buf[DEBUG_MAX_LINE_LEN-1] = 0;
151 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
152 //if( len < DEBUG_MAX_LINE )
158 void Debug_FmtS(const char *format, ...)
161 va_start(args, format);
162 Debug_Fmt(format, args);
166 void Debug_KernelPanic()
168 gbDebug_IsKPanic = 1;
169 KernelPanic_SetMode();
173 * \fn void LogF(char *Msg, ...)
175 void LogF(char *Fmt, ...)
179 #if LOCK_DEBUG_OUTPUT
180 SHORTLOCK(&glDebug_Lock);
185 Debug_Fmt(Fmt, args);
189 #if LOCK_DEBUG_OUTPUT
190 SHORTREL(&glDebug_Lock);
194 * \fn void Debug(char *Msg, ...)
195 * \brief Print only to the debug channel
197 void Debug(char *Fmt, ...)
201 #if LOCK_DEBUG_OUTPUT
202 SHORTLOCK(&glDebug_Lock);
205 Debug_Puts(0, "Debug: ");
207 Debug_DbgOnlyFmt(Fmt, args);
209 Debug_PutCharDebug('\r');
210 Debug_PutCharDebug('\n');
211 #if LOCK_DEBUG_OUTPUT
212 SHORTREL(&glDebug_Lock);
216 * \fn void Log(char *Msg, ...)
218 void Log(char *Fmt, ...)
222 #if LOCK_DEBUG_OUTPUT
223 SHORTLOCK(&glDebug_Lock);
226 Debug_Puts(1, "Log: ");
228 Debug_Fmt(Fmt, args);
233 #if LOCK_DEBUG_OUTPUT
234 SHORTREL(&glDebug_Lock);
237 void Warning(char *Fmt, ...)
241 #if LOCK_DEBUG_OUTPUT
242 SHORTLOCK(&glDebug_Lock);
245 Debug_Puts(1, "Warning: ");
247 Debug_Fmt(Fmt, args);
252 #if LOCK_DEBUG_OUTPUT
253 SHORTREL(&glDebug_Lock);
256 void Panic(char *Fmt, ...)
260 #if LOCK_DEBUG_OUTPUT
261 SHORTLOCK(&glDebug_Lock);
263 // And never SHORTREL
267 Debug_Puts(1, "Panic: ");
269 Debug_Fmt(Fmt, args);
276 __asm__ __volatile__ ("xchg %bx, %bx");
277 __asm__ __volatile__ ("cli;\n\thlt");
278 for(;;) __asm__ __volatile__ ("hlt");
281 void Debug_SetKTerminal(char *File)
284 if(giDebug_KTerm != -1) {
289 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
290 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
292 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
295 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
300 tTID tid = Threads_GetTID();
302 #if LOCK_DEBUG_OUTPUT
303 SHORTLOCK(&glDebug_Lock);
308 va_start(args, ArgTypes);
310 while(i--) Debug_Putchar(' ');
312 Debug_Puts(1, FuncName);
313 Debug_FmtS("[%i]", tid);
314 Debug_Puts(1, ": (");
318 pos = strpos(ArgTypes, ' ');
319 if(pos != -1) ArgTypes[pos] = '\0';
320 if(pos == -1 || pos > 1) {
321 Debug_Puts(1, ArgTypes+1);
324 if(pos != -1) ArgTypes[pos] = ' ';
327 case 'p': LogF("%p", va_arg(args, void*)); break;
328 case 's': LogF("'%s'", va_arg(args, char*)); break;
329 case 'i': LogF("%i", va_arg(args, int)); break;
330 case 'u': LogF("%u", va_arg(args, Uint)); break;
331 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
332 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
333 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
334 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
337 Debug_Putchar(','); Debug_Putchar(' ');
341 ArgTypes = &ArgTypes[pos+1];
345 Debug_Putchar(')'); Debug_Putchar('\r'); Debug_Putchar('\n');
347 #if LOCK_DEBUG_OUTPUT
348 SHORTREL(&glDebug_Lock);
352 void Debug_Log(char *FuncName, char *Fmt, ...)
355 int i = gDebug_Level;
356 tTID tid = Threads_GetTID();
358 #if LOCK_DEBUG_OUTPUT
359 SHORTLOCK(&glDebug_Lock);
364 while(i--) Debug_Putchar(' ');
366 Debug_Puts(1, FuncName);
367 Debug_FmtS("[%i]", tid);
369 Debug_Fmt(Fmt, args);
375 #if LOCK_DEBUG_OUTPUT
376 SHORTREL(&glDebug_Lock);
380 void Debug_Leave(char *FuncName, char RetType, ...)
384 tTID tid = Threads_GetTID();
386 #if LOCK_DEBUG_OUTPUT
387 SHORTLOCK(&glDebug_Lock);
392 va_start(args, RetType);
399 while(i--) Debug_Putchar(' ');
401 Debug_Puts(1, FuncName);
402 Debug_FmtS("[%i]", tid);
403 Debug_Puts(1, ": RETURN");
409 #if LOCK_DEBUG_OUTPUT
410 SHORTREL(&glDebug_Lock);
418 case 'n': Debug_Puts(1, "NULL"); break;
419 case 'p': Debug_Fmt("%p", args); break;
420 case 's': Debug_Fmt("'%s'", args); break;
421 case 'i': Debug_Fmt("%i", args); break;
422 case 'u': Debug_Fmt("%u", args); break;
423 case 'x': Debug_Fmt("0x%x", args); break;
425 case 'X': Debug_Fmt("0x%llx", args); break;
432 #if LOCK_DEBUG_OUTPUT
433 SHORTREL(&glDebug_Lock);
437 void Debug_HexDump(char *Header, void *Data, Uint Length)
441 Debug_Puts(1, Header);
442 LogF(" (Hexdump of %p)\r\n", Data);
444 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
448 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
449 " %02x %02x %02x %02x %02x %02x %02x %02x"
450 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
452 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
453 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
454 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
455 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
464 LogF("Log: %04x: ", pos);
465 for(i = 0; i < Length; i ++)
467 LogF("%02x ", cdat[i]);
469 for( ; i < 16; i ++) LogF(" ");
471 for(i = 0; i < Length; i ++)
473 if( i == 8 ) LogF(" ");