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
17 extern void Threads_Dump(void);
18 extern void KernelPanic_SetMode(void);
19 extern void KernelPanic_PutChar(char Ch);
22 int putDebugChar(char ch);
23 int getDebugChar(void);
24 static void Debug_Putchar(char ch);
25 static void Debug_Puts(int DbgOnly, char *Str);
26 void Debug_Fmt(const char *format, va_list args);
30 int giDebug_KTerm = -1;
31 int gbDebug_SerialSetup = 0;
32 int gbGDB_SerialSetup = 0;
33 int gbDebug_IsKPanic = 0;
34 volatile int gbInPutChar = 0;
37 int putDebugChar(char ch)
39 if(!gbGDB_SerialSetup) {
40 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
41 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
42 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
43 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
44 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
45 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
46 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
47 gbDebug_SerialSetup = 1;
49 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
50 outb(GDB_SERIAL_PORT, ch);
53 int getDebugChar(void)
55 if(!gbGDB_SerialSetup) {
56 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
57 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
58 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
59 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
60 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
61 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
62 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
63 gbDebug_SerialSetup = 1;
65 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
66 return inb(GDB_SERIAL_PORT);
69 static void Debug_PutCharDebug(char ch)
72 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
76 if(!gbDebug_SerialSetup) {
77 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
78 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
79 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
80 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
81 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
82 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
83 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
84 gbDebug_SerialSetup = 1;
86 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
87 outb(SERIAL_PORT, ch);
91 static void Debug_Putchar(char ch)
93 Debug_PutCharDebug(ch);
94 if( !gbDebug_IsKPanic )
96 if(gbInPutChar) return ;
98 if(giDebug_KTerm != -1)
99 VFS_Write(giDebug_KTerm, 1, &ch);
103 KernelPanic_PutChar(ch);
106 static void Debug_Puts(int UseKTerm, char *Str)
111 Debug_PutCharDebug( *Str );
113 if( gbDebug_IsKPanic )
114 KernelPanic_PutChar(*Str);
121 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
123 if(gbInPutChar) return ;
125 VFS_Write(giDebug_KTerm, len, Str);
130 void Debug_DbgOnlyFmt(const char *format, va_list args)
132 char buf[DEBUG_MAX_LINE_LEN];
134 buf[DEBUG_MAX_LINE_LEN-1] = 0;
135 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
136 //if( len < DEBUG_MAX_LINE )
141 void Debug_Fmt(const char *format, va_list args)
143 char buf[DEBUG_MAX_LINE_LEN];
145 buf[DEBUG_MAX_LINE_LEN-1] = 0;
146 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
147 //if( len < DEBUG_MAX_LINE )
153 void Debug_KernelPanic()
155 gbDebug_IsKPanic = 1;
156 KernelPanic_SetMode();
160 * \fn void LogF(char *Msg, ...)
162 void LogF(char *Fmt, ...)
168 Debug_Fmt(Fmt, args);
173 * \fn void Debug(char *Msg, ...)
174 * \brief Print only to the debug channel
176 void Debug(char *Fmt, ...)
180 Debug_Puts(0, "Debug: ");
182 Debug_DbgOnlyFmt(Fmt, args);
184 Debug_PutCharDebug('\r');
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);
201 void Warning(char *Fmt, ...)
204 Debug_Puts(1, "Warning: ");
206 Debug_Fmt(Fmt, args);
211 void Panic(char *Fmt, ...)
217 Debug_Puts(1, "Panic: ");
219 Debug_Fmt(Fmt, args);
226 __asm__ __volatile__ ("xchg %bx, %bx");
227 __asm__ __volatile__ ("cli;\n\thlt");
228 for(;;) __asm__ __volatile__ ("hlt");
231 void Debug_SetKTerminal(char *File)
234 if(giDebug_KTerm != -1) {
239 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
240 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
242 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
245 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
248 int i = gDebug_Level ++;
251 va_start(args, ArgTypes);
253 while(i--) Debug_Putchar(' ');
255 Debug_Puts(1, FuncName); Debug_Puts(1, ": (");
259 pos = strpos(ArgTypes, ' ');
260 if(pos != -1) ArgTypes[pos] = '\0';
261 if(pos == -1 || pos > 1) {
262 Debug_Puts(1, ArgTypes+1);
265 if(pos != -1) ArgTypes[pos] = ' ';
268 case 'p': LogF("%p", va_arg(args, void*)); break;
269 case 's': LogF("'%s'", va_arg(args, char*)); break;
270 case 'i': LogF("%i", va_arg(args, int)); break;
271 case 'u': LogF("%u", va_arg(args, Uint)); break;
272 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
273 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
274 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
275 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
278 Debug_Putchar(','); Debug_Putchar(' ');
282 ArgTypes = &ArgTypes[pos+1];
286 Debug_Putchar(')'); Debug_Putchar('\n'); Debug_Putchar('\r');
289 void Debug_Log(char *FuncName, char *Fmt, ...)
292 int i = gDebug_Level;
296 while(i--) Debug_Putchar(' ');
298 Debug_Puts(1, FuncName); Debug_Puts(1, ": ");
299 Debug_Fmt(Fmt, args);
306 void Debug_Leave(char *FuncName, char RetType, ...)
309 int i = --gDebug_Level;
311 va_start(args, RetType);
318 while(i--) Debug_Putchar(' ');
320 Debug_Puts(1, FuncName); Debug_Puts(1, ": RETURN");
332 case 'n': Debug_Puts(1, "NULL"); break;
333 case 'p': Debug_Fmt("%p", args); break;
334 case 's': Debug_Fmt("'%s'", args); break;
335 case 'i': Debug_Fmt("%i", args); break;
336 case 'u': Debug_Fmt("%u", args); break;
337 case 'x': Debug_Fmt("0x%x", args); break;
339 case 'X': Debug_Fmt("0x%llx", args); break;
347 void Debug_HexDump(char *Header, void *Data, Uint Length)
351 Debug_Puts(1, Header);
352 LogF(" (Hexdump of %p)\r\n", Data);
356 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
357 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
358 " %02x %02x %02x %02x %02x %02x %02x %02x"
359 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
361 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
362 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
363 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
364 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
371 LogF("Log: %04x: ", pos);