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 0
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 tSpinlock 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_KernelPanic()
160 gbDebug_IsKPanic = 1;
161 KernelPanic_SetMode();
165 * \fn void LogF(char *Msg, ...)
167 void LogF(char *Fmt, ...)
171 #if LOCK_DEBUG_OUTPUT
172 VTIGHTLOCK(&glDebug_Lock);
177 Debug_Fmt(Fmt, args);
181 #if LOCK_DEBUG_OUTPUT
182 RELEASE(&glDebug_Lock);
186 * \fn void Debug(char *Msg, ...)
187 * \brief Print only to the debug channel
189 void Debug(char *Fmt, ...)
193 #if LOCK_DEBUG_OUTPUT
197 Debug_Puts(0, "Debug: ");
199 Debug_DbgOnlyFmt(Fmt, args);
201 Debug_PutCharDebug('\r');
202 Debug_PutCharDebug('\n');
203 #if LOCK_DEBUG_OUTPUT
204 RELEASE(&glDebug_Lock);
208 * \fn void Log(char *Msg, ...)
210 void Log(char *Fmt, ...)
214 #if LOCK_DEBUG_OUTPUT
218 Debug_Puts(1, "Log: ");
220 Debug_Fmt(Fmt, args);
225 #if LOCK_DEBUG_OUTPUT
226 RELEASE(&glDebug_Lock);
229 void Warning(char *Fmt, ...)
233 #if LOCK_DEBUG_OUTPUT
237 Debug_Puts(1, "Warning: ");
239 Debug_Fmt(Fmt, args);
244 #if LOCK_DEBUG_OUTPUT
245 RELEASE(&glDebug_Lock);
248 void Panic(char *Fmt, ...)
252 #if LOCK_DEBUG_OUTPUT
259 Debug_Puts(1, "Panic: ");
261 Debug_Fmt(Fmt, args);
268 __asm__ __volatile__ ("xchg %bx, %bx");
269 __asm__ __volatile__ ("cli;\n\thlt");
270 for(;;) __asm__ __volatile__ ("hlt");
273 void Debug_SetKTerminal(char *File)
276 if(giDebug_KTerm != -1) {
281 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
282 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
284 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
287 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
290 int i = gDebug_Level ++;
293 va_start(args, ArgTypes);
295 while(i--) Debug_Putchar(' ');
297 Debug_Puts(1, FuncName); Debug_Puts(1, ": (");
301 pos = strpos(ArgTypes, ' ');
302 if(pos != -1) ArgTypes[pos] = '\0';
303 if(pos == -1 || pos > 1) {
304 Debug_Puts(1, ArgTypes+1);
307 if(pos != -1) ArgTypes[pos] = ' ';
310 case 'p': LogF("%p", va_arg(args, void*)); break;
311 case 's': LogF("'%s'", va_arg(args, char*)); break;
312 case 'i': LogF("%i", va_arg(args, int)); break;
313 case 'u': LogF("%u", va_arg(args, Uint)); break;
314 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
315 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
316 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
317 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
320 Debug_Putchar(','); Debug_Putchar(' ');
324 ArgTypes = &ArgTypes[pos+1];
328 Debug_Putchar(')'); Debug_Putchar('\r'); Debug_Putchar('\n');
331 void Debug_Log(char *FuncName, char *Fmt, ...)
334 int i = gDebug_Level;
338 while(i--) Debug_Putchar(' ');
340 Debug_Puts(1, FuncName); Debug_Puts(1, ": ");
341 Debug_Fmt(Fmt, args);
348 void Debug_Leave(char *FuncName, char RetType, ...)
351 int i = --gDebug_Level;
353 #if LOCK_DEBUG_OUTPUT
357 va_start(args, RetType);
364 while(i--) Debug_Putchar(' ');
366 Debug_Puts(1, FuncName); Debug_Puts(1, ": RETURN");
378 case 'n': Debug_Puts(1, "NULL"); break;
379 case 'p': Debug_Fmt("%p", args); break;
380 case 's': Debug_Fmt("'%s'", args); break;
381 case 'i': Debug_Fmt("%i", args); break;
382 case 'u': Debug_Fmt("%u", args); break;
383 case 'x': Debug_Fmt("0x%x", args); break;
385 case 'X': Debug_Fmt("0x%llx", args); break;
393 void Debug_HexDump(char *Header, void *Data, Uint Length)
397 Debug_Puts(1, Header);
398 LogF(" (Hexdump of %p)\r\n", Data);
402 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
403 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
404 " %02x %02x %02x %02x %02x %02x %02x %02x"
405 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
407 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
408 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
409 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
410 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
417 LogF("Log: %04x: ", pos);