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, const char *Str);
28 void Debug_DbgOnlyFmt(const char *format, va_list args);
29 void Debug_FmtS(const char *format, ...);
30 void Debug_Fmt(const char *format, va_list args);
31 void Debug_SetKTerminal(const char *File);
35 int giDebug_KTerm = -1;
36 int gbDebug_SerialSetup = 0;
37 int gbGDB_SerialSetup = 0;
38 int gbDebug_IsKPanic = 0;
39 volatile int gbInPutChar = 0;
41 tShortSpinlock glDebug_Lock;
45 int putDebugChar(char ch)
47 if(!gbGDB_SerialSetup) {
48 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
49 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
50 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
51 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
52 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
53 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
54 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
55 gbDebug_SerialSetup = 1;
57 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
58 outb(GDB_SERIAL_PORT, ch);
61 int getDebugChar(void)
63 if(!gbGDB_SerialSetup) {
64 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
65 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
66 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
67 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
68 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
69 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
70 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
71 gbDebug_SerialSetup = 1;
73 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
74 return inb(GDB_SERIAL_PORT);
77 static void Debug_PutCharDebug(char ch)
80 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
84 if(!gbDebug_SerialSetup) {
85 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
86 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
87 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
88 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
89 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
90 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
91 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
92 gbDebug_SerialSetup = 1;
94 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
95 outb(SERIAL_PORT, ch);
99 static void Debug_Putchar(char ch)
101 Debug_PutCharDebug(ch);
102 if( !gbDebug_IsKPanic )
104 if(gbInPutChar) return ;
106 if(giDebug_KTerm != -1)
107 VFS_Write(giDebug_KTerm, 1, &ch);
111 KernelPanic_PutChar(ch);
114 static void Debug_Puts(int UseKTerm, const char *Str)
119 Debug_PutCharDebug( *Str );
121 if( gbDebug_IsKPanic )
122 KernelPanic_PutChar(*Str);
129 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
131 if(gbInPutChar) return ;
133 VFS_Write(giDebug_KTerm, len, Str);
138 void Debug_DbgOnlyFmt(const char *format, va_list args)
140 char buf[DEBUG_MAX_LINE_LEN];
142 buf[DEBUG_MAX_LINE_LEN-1] = 0;
143 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
144 //if( len < DEBUG_MAX_LINE )
149 void Debug_Fmt(const char *format, va_list args)
151 char buf[DEBUG_MAX_LINE_LEN];
153 buf[DEBUG_MAX_LINE_LEN-1] = 0;
154 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
155 //if( len < DEBUG_MAX_LINE )
161 void Debug_FmtS(const char *format, ...)
164 va_start(args, format);
165 Debug_Fmt(format, args);
169 void Debug_KernelPanic()
171 gbDebug_IsKPanic = 1;
172 KernelPanic_SetMode();
176 * \fn void LogF(const char *Msg, ...)
177 * \brief Raw debug log (no new line, no prefix)
179 void LogF(const char *Fmt, ...)
183 #if LOCK_DEBUG_OUTPUT
184 SHORTLOCK(&glDebug_Lock);
189 Debug_Fmt(Fmt, args);
193 #if LOCK_DEBUG_OUTPUT
194 SHORTREL(&glDebug_Lock);
198 * \fn void Debug(const char *Msg, ...)
199 * \brief Print only to the debug channel
201 void Debug(const char *Fmt, ...)
205 #if LOCK_DEBUG_OUTPUT
206 SHORTLOCK(&glDebug_Lock);
209 Debug_Puts(0, "Debug: ");
211 Debug_DbgOnlyFmt(Fmt, args);
213 Debug_PutCharDebug('\r');
214 Debug_PutCharDebug('\n');
215 #if LOCK_DEBUG_OUTPUT
216 SHORTREL(&glDebug_Lock);
220 * \fn void Log(const char *Msg, ...)
222 void Log(const char *Fmt, ...)
226 #if LOCK_DEBUG_OUTPUT
227 SHORTLOCK(&glDebug_Lock);
230 Debug_Puts(1, "Log: ");
232 Debug_Fmt(Fmt, args);
237 #if LOCK_DEBUG_OUTPUT
238 SHORTREL(&glDebug_Lock);
241 void Warning(const char *Fmt, ...)
245 #if LOCK_DEBUG_OUTPUT
246 SHORTLOCK(&glDebug_Lock);
249 Debug_Puts(1, "Warning: ");
251 Debug_Fmt(Fmt, args);
256 #if LOCK_DEBUG_OUTPUT
257 SHORTREL(&glDebug_Lock);
260 void Panic(const char *Fmt, ...)
264 #if LOCK_DEBUG_OUTPUT
265 SHORTLOCK(&glDebug_Lock);
267 // And never SHORTREL
271 Debug_Puts(1, "Panic: ");
273 Debug_Fmt(Fmt, args);
280 __asm__ __volatile__ ("xchg %bx, %bx");
281 __asm__ __volatile__ ("cli;\n\thlt");
282 for(;;) __asm__ __volatile__ ("hlt");
285 void Debug_SetKTerminal(const char *File)
288 if(giDebug_KTerm != -1) {
293 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
294 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
296 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
299 void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
304 tTID tid = Threads_GetTID();
306 #if LOCK_DEBUG_OUTPUT
307 SHORTLOCK(&glDebug_Lock);
312 va_start(args, ArgTypes);
314 LogF("%012lli ", now());
315 while(i--) Debug_Putchar(' ');
317 Debug_Puts(1, FuncName);
318 Debug_FmtS("[%i]", tid);
319 Debug_Puts(1, ": (");
323 pos = strpos(ArgTypes, ' ');
324 if(pos == -1 || pos > 1) {
326 Debug_Puts(1, ArgTypes+1);
328 for( i = 1; i < pos; i ++ )
329 Debug_Putchar(ArgTypes[i]);
335 case 'p': LogF("%p", va_arg(args, void*)); break;
336 case 's': LogF("'%s'", va_arg(args, char*)); break;
337 case 'i': LogF("%i", va_arg(args, int)); break;
338 case 'u': LogF("%u", va_arg(args, Uint)); break;
339 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
340 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
341 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
342 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
345 Debug_Putchar(','); Debug_Putchar(' ');
349 ArgTypes = &ArgTypes[pos+1];
353 Debug_Putchar(')'); Debug_Putchar('\r'); Debug_Putchar('\n');
355 #if LOCK_DEBUG_OUTPUT
356 SHORTREL(&glDebug_Lock);
360 void Debug_Log(const char *FuncName, const char *Fmt, ...)
363 int i = gDebug_Level;
364 tTID tid = Threads_GetTID();
366 #if LOCK_DEBUG_OUTPUT
367 SHORTLOCK(&glDebug_Lock);
372 LogF("%012lli ", now());
373 while(i--) Debug_Putchar(' ');
375 Debug_Puts(1, FuncName);
376 Debug_FmtS("[%i]", tid);
378 Debug_Fmt(Fmt, args);
384 #if LOCK_DEBUG_OUTPUT
385 SHORTREL(&glDebug_Lock);
389 void Debug_Leave(const char *FuncName, char RetType, ...)
393 tTID tid = Threads_GetTID();
395 #if LOCK_DEBUG_OUTPUT
396 SHORTLOCK(&glDebug_Lock);
401 va_start(args, RetType);
407 LogF("%012lli ", now());
409 while(i--) Debug_Putchar(' ');
411 Debug_Puts(1, FuncName);
412 Debug_FmtS("[%i]", tid);
413 Debug_Puts(1, ": RETURN");
419 #if LOCK_DEBUG_OUTPUT
420 SHORTREL(&glDebug_Lock);
428 case 'n': Debug_Puts(1, "NULL"); break;
429 case 'p': Debug_Fmt("%p", args); break;
430 case 's': Debug_Fmt("'%s'", args); break;
431 case 'i': Debug_Fmt("%i", args); break;
432 case 'u': Debug_Fmt("%u", args); break;
433 case 'x': Debug_Fmt("0x%x", args); break;
435 case 'X': Debug_Fmt("0x%llx", args); break;
442 #if LOCK_DEBUG_OUTPUT
443 SHORTREL(&glDebug_Lock);
447 void Debug_HexDump(const char *Header, const void *Data, Uint Length)
449 const Uint8 *cdat = Data;
451 Debug_Puts(1, Header);
452 LogF(" (Hexdump of %p)\r\n", Data);
454 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
458 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
459 " %02x %02x %02x %02x %02x %02x %02x %02x"
460 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
462 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
463 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
464 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
465 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
474 LogF("Log: %04x: ", pos);
475 for(i = 0; i < Length; i ++)
477 LogF("%02x ", cdat[i]);
479 for( ; i < 16; i ++) LogF(" ");
481 for(i = 0; i < Length; i ++)
483 if( i == 8 ) LogF(" ");