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_USE_VSNPRINTF 0
15 #define DEBUG_MAX_LINE_LEN 256
18 extern void Threads_Dump(void);
19 extern void KernelPanic_SetMode(void);
20 extern void KernelPanic_PutChar(char Ch);
23 int putDebugChar(char ch);
24 int getDebugChar(void);
25 static void Debug_Putchar(char ch);
26 static void Debug_Puts(int DbgOnly, char *Str);
27 void Debug_Fmt(const char *format, va_list args);
31 int giDebug_KTerm = -1;
32 int gbDebug_SerialSetup = 0;
33 int gbGDB_SerialSetup = 0;
34 int gbDebug_IsKPanic = 0;
35 volatile int gbInPutChar = 0;
38 int putDebugChar(char ch)
40 if(!gbGDB_SerialSetup) {
41 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
42 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
43 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
44 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
45 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
46 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
47 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
48 gbDebug_SerialSetup = 1;
50 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
51 outb(GDB_SERIAL_PORT, ch);
54 int getDebugChar(void)
56 if(!gbGDB_SerialSetup) {
57 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
58 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
59 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
60 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
61 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
62 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
63 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
64 gbDebug_SerialSetup = 1;
66 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
67 return inb(GDB_SERIAL_PORT);
70 static void Debug_PutCharDebug(char ch)
73 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
77 if(!gbDebug_SerialSetup) {
78 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
79 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
80 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
81 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
82 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
83 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
84 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
85 gbDebug_SerialSetup = 1;
87 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
88 outb(SERIAL_PORT, ch);
92 static void Debug_Putchar(char ch)
94 Debug_PutCharDebug(ch);
95 if( !gbDebug_IsKPanic )
97 if(gbInPutChar) return ;
99 if(giDebug_KTerm != -1)
100 VFS_Write(giDebug_KTerm, 1, &ch);
104 KernelPanic_PutChar(ch);
107 static void Debug_Puts(int UseKTerm, char *Str)
112 Debug_PutCharDebug( *Str );
114 if( gbDebug_IsKPanic )
115 KernelPanic_PutChar(*Str);
122 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
124 if(gbInPutChar) return ;
126 VFS_Write(giDebug_KTerm, len, Str);
131 void Debug_DbgOnlyFmt(const char *format, va_list args)
133 char buf[DEBUG_MAX_LINE_LEN];
135 buf[DEBUG_MAX_LINE_LEN-1] = 0;
136 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
137 //if( len < DEBUG_MAX_LINE )
142 void Debug_Fmt(const char *format, va_list args)
144 char buf[DEBUG_MAX_LINE_LEN];
146 buf[DEBUG_MAX_LINE_LEN-1] = 0;
147 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
148 //if( len < DEBUG_MAX_LINE )
154 void Debug_KernelPanic()
156 gbDebug_IsKPanic = 1;
157 KernelPanic_SetMode();
161 * \fn void LogF(char *Msg, ...)
163 void LogF(char *Fmt, ...)
169 Debug_Fmt(Fmt, args);
174 * \fn void Debug(char *Msg, ...)
175 * \brief Print only to the debug channel
177 void Debug(char *Fmt, ...)
181 Debug_Puts(0, "Debug: ");
183 Debug_DbgOnlyFmt(Fmt, args);
185 Debug_PutCharDebug('\n');
188 * \fn void Log(char *Msg, ...)
190 void Log(char *Fmt, ...)
194 Debug_Puts(1, "Log: ");
196 Debug_Fmt(Fmt, args);
200 void Warning(char *Fmt, ...)
203 Debug_Puts(1, "Warning: ");
205 Debug_Fmt(Fmt, args);
209 void Panic(char *Fmt, ...)
215 Debug_Puts(1, "Panic: ");
217 Debug_Fmt(Fmt, args);
223 __asm__ __volatile__ ("xchg %bx, %bx");
224 __asm__ __volatile__ ("cli;\n\thlt");
225 for(;;) __asm__ __volatile__ ("hlt");
228 void Debug_SetKTerminal(char *File)
231 if(giDebug_KTerm != -1) {
236 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
237 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
239 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
242 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
245 int i = gDebug_Level ++;
248 va_start(args, ArgTypes);
250 while(i--) Debug_Putchar(' ');
252 Debug_Puts(1, FuncName); Debug_Puts(1, ": (");
256 pos = strpos(ArgTypes, ' ');
257 if(pos != -1) ArgTypes[pos] = '\0';
258 if(pos == -1 || pos > 1) {
259 Debug_Puts(1, ArgTypes+1);
262 if(pos != -1) ArgTypes[pos] = ' ';
265 case 'p': LogF("%p", va_arg(args, void*)); break;
266 case 's': LogF("'%s'", va_arg(args, char*)); break;
267 case 'i': LogF("%i", va_arg(args, int)); break;
268 case 'u': LogF("%u", va_arg(args, Uint)); break;
269 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
270 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
271 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
272 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
275 Debug_Putchar(','); Debug_Putchar(' ');
279 ArgTypes = &ArgTypes[pos+1];
283 Debug_Putchar(')'); Debug_Putchar('\n');
286 void Debug_Log(char *FuncName, char *Fmt, ...)
289 int i = gDebug_Level;
293 while(i--) Debug_Putchar(' ');
295 Debug_Puts(1, FuncName); Debug_Puts(1, ": ");
296 Debug_Fmt(Fmt, args);
302 void Debug_Leave(char *FuncName, char RetType, ...)
305 int i = --gDebug_Level;
307 va_start(args, RetType);
314 while(i--) Debug_Putchar(' ');
316 Debug_Puts(1, FuncName); Debug_Puts(1, ": RETURN");
327 case 'n': Debug_Puts(1, "NULL"); break;
328 case 'p': Debug_Fmt("%p", args); break;
329 case 's': Debug_Fmt("'%s'", args); break;
330 case 'i': Debug_Fmt("%i", args); break;
331 case 'u': Debug_Fmt("%u", args); break;
332 case 'x': Debug_Fmt("0x%x", args); break;
334 case 'X': Debug_Fmt("0x%llx", args); break;
341 void Debug_HexDump(char *Header, void *Data, Uint Length)
345 Debug_Puts(1, Header);
346 LogF(" (Hexdump of %p)\n", Data);
350 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
351 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
352 " %02x %02x %02x %02x %02x %02x %02x %02x"
353 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
355 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
356 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
357 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
358 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
365 LogF("Log: %04x: ", pos);