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_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, const 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(const char *Msg, ...)
174 * \brief Raw debug log (no new line, no prefix)
176 void LogF(const char *Fmt, ...)
180 #if LOCK_DEBUG_OUTPUT
181 SHORTLOCK(&glDebug_Lock);
186 Debug_Fmt(Fmt, args);
190 #if LOCK_DEBUG_OUTPUT
191 SHORTREL(&glDebug_Lock);
195 * \fn void Debug(const char *Msg, ...)
196 * \brief Print only to the debug channel
198 void Debug(const char *Fmt, ...)
202 #if LOCK_DEBUG_OUTPUT
203 SHORTLOCK(&glDebug_Lock);
206 Debug_Puts(0, "Debug: ");
208 Debug_DbgOnlyFmt(Fmt, args);
210 Debug_PutCharDebug('\r');
211 Debug_PutCharDebug('\n');
212 #if LOCK_DEBUG_OUTPUT
213 SHORTREL(&glDebug_Lock);
217 * \fn void Log(const char *Msg, ...)
219 void Log(const char *Fmt, ...)
223 #if LOCK_DEBUG_OUTPUT
224 SHORTLOCK(&glDebug_Lock);
227 Debug_Puts(1, "Log: ");
229 Debug_Fmt(Fmt, args);
234 #if LOCK_DEBUG_OUTPUT
235 SHORTREL(&glDebug_Lock);
238 void Warning(const char *Fmt, ...)
242 #if LOCK_DEBUG_OUTPUT
243 SHORTLOCK(&glDebug_Lock);
246 Debug_Puts(1, "Warning: ");
248 Debug_Fmt(Fmt, args);
253 #if LOCK_DEBUG_OUTPUT
254 SHORTREL(&glDebug_Lock);
257 void Panic(const char *Fmt, ...)
261 #if LOCK_DEBUG_OUTPUT
262 SHORTLOCK(&glDebug_Lock);
264 // And never SHORTREL
268 Debug_Puts(1, "Panic: ");
270 Debug_Fmt(Fmt, args);
277 __asm__ __volatile__ ("xchg %bx, %bx");
278 __asm__ __volatile__ ("cli;\n\thlt");
279 for(;;) __asm__ __volatile__ ("hlt");
282 void Debug_SetKTerminal(const char *File)
285 if(giDebug_KTerm != -1) {
290 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
291 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
293 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
296 void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
301 tTID tid = Threads_GetTID();
303 #if LOCK_DEBUG_OUTPUT
304 SHORTLOCK(&glDebug_Lock);
309 va_start(args, ArgTypes);
311 while(i--) Debug_Putchar(' ');
313 Debug_Puts(1, FuncName);
314 Debug_FmtS("[%i]", tid);
315 Debug_Puts(1, ": (");
319 pos = strpos(ArgTypes, ' ');
320 if(pos == -1 || pos > 1) {
322 Debug_Puts(1, ArgTypes+1);
324 for( i = 1; i < pos; i ++ )
325 Debug_Putchar(ArgTypes[i]);
331 case 'p': LogF("%p", va_arg(args, void*)); break;
332 case 's': LogF("'%s'", va_arg(args, char*)); break;
333 case 'i': LogF("%i", va_arg(args, int)); break;
334 case 'u': LogF("%u", va_arg(args, Uint)); break;
335 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
336 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
337 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
338 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
341 Debug_Putchar(','); Debug_Putchar(' ');
345 ArgTypes = &ArgTypes[pos+1];
349 Debug_Putchar(')'); Debug_Putchar('\r'); Debug_Putchar('\n');
351 #if LOCK_DEBUG_OUTPUT
352 SHORTREL(&glDebug_Lock);
356 void Debug_Log(const char *FuncName, const char *Fmt, ...)
359 int i = gDebug_Level;
360 tTID tid = Threads_GetTID();
362 #if LOCK_DEBUG_OUTPUT
363 SHORTLOCK(&glDebug_Lock);
368 while(i--) Debug_Putchar(' ');
370 Debug_Puts(1, FuncName);
371 Debug_FmtS("[%i]", tid);
373 Debug_Fmt(Fmt, args);
379 #if LOCK_DEBUG_OUTPUT
380 SHORTREL(&glDebug_Lock);
384 void Debug_Leave(const char *FuncName, char RetType, ...)
388 tTID tid = Threads_GetTID();
390 #if LOCK_DEBUG_OUTPUT
391 SHORTLOCK(&glDebug_Lock);
396 va_start(args, RetType);
403 while(i--) Debug_Putchar(' ');
405 Debug_Puts(1, FuncName);
406 Debug_FmtS("[%i]", tid);
407 Debug_Puts(1, ": RETURN");
413 #if LOCK_DEBUG_OUTPUT
414 SHORTREL(&glDebug_Lock);
422 case 'n': Debug_Puts(1, "NULL"); break;
423 case 'p': Debug_Fmt("%p", args); break;
424 case 's': Debug_Fmt("'%s'", args); break;
425 case 'i': Debug_Fmt("%i", args); break;
426 case 'u': Debug_Fmt("%u", args); break;
427 case 'x': Debug_Fmt("0x%x", args); break;
429 case 'X': Debug_Fmt("0x%llx", args); break;
436 #if LOCK_DEBUG_OUTPUT
437 SHORTREL(&glDebug_Lock);
441 void Debug_HexDump(const char *Header, const void *Data, Uint Length)
443 const Uint8 *cdat = Data;
445 Debug_Puts(1, Header);
446 LogF(" (Hexdump of %p)\r\n", Data);
448 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
452 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
453 " %02x %02x %02x %02x %02x %02x %02x %02x"
454 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
456 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
457 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
458 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
459 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
468 LogF("Log: %04x: ", pos);
469 for(i = 0; i < Length; i ++)
471 LogF("%02x ", cdat[i]);
473 for( ; i < 16; i ++) LogF(" ");
475 for(i = 0; i < Length; i ++)
477 if( i == 8 ) LogF(" ");