2 * AcessOS Microkernel Version
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT 0x3F8
11 #define GDB_SERIAL_PORT 0x2F8
12 #define DEBUG_USE_VSNPRINTF 0
13 #define DEBUG_MAX_LINE_LEN 256
16 extern void Threads_Dump(void);
17 extern void KernelPanic_SetMode(void);
18 extern void KernelPanic_PutChar(char Ch);
21 int putDebugChar(char ch);
22 int getDebugChar(void);
23 static void Debug_Putchar(char ch);
24 static void Debug_Puts(int DbgOnly, char *Str);
25 void Debug_Fmt(const char *format, va_list args);
29 int giDebug_KTerm = -1;
30 int gbDebug_SerialSetup = 0;
31 int gbGDB_SerialSetup = 0;
32 int gbDebug_IsKPanic = 0;
33 volatile int gbInPutChar = 0;
36 int putDebugChar(char ch)
38 if(!gbGDB_SerialSetup) {
39 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
40 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
41 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
42 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
43 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
44 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
45 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
46 gbDebug_SerialSetup = 1;
48 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
49 outb(GDB_SERIAL_PORT, ch);
52 int getDebugChar(void)
54 if(!gbGDB_SerialSetup) {
55 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
56 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
57 outb(GDB_SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
58 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
59 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
60 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
61 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
62 gbDebug_SerialSetup = 1;
64 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
65 return inb(GDB_SERIAL_PORT);
68 static void Debug_PutCharDebug(char ch)
71 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
75 if(!gbDebug_SerialSetup) {
76 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
77 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
78 outb(SERIAL_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
79 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
80 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
81 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
82 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
83 gbDebug_SerialSetup = 1;
85 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
86 outb(SERIAL_PORT, ch);
90 static void Debug_Putchar(char ch)
92 Debug_PutCharDebug(ch);
93 if( !gbDebug_IsKPanic )
95 if(gbInPutChar) return ;
97 if(giDebug_KTerm != -1)
98 VFS_Write(giDebug_KTerm, 1, &ch);
102 KernelPanic_PutChar(ch);
105 static void Debug_Puts(int UseKTerm, char *Str)
110 Debug_PutCharDebug( *Str );
112 if( gbDebug_IsKPanic )
113 KernelPanic_PutChar(*Str);
120 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
122 if(gbInPutChar) return ;
124 VFS_Write(giDebug_KTerm, len, Str);
129 void Debug_DbgOnlyFmt(const char *format, va_list args)
131 char buf[DEBUG_MAX_LINE_LEN];
133 buf[DEBUG_MAX_LINE_LEN-1] = 0;
134 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
135 //if( len < DEBUG_MAX_LINE )
140 void Debug_Fmt(const char *format, va_list args)
142 char buf[DEBUG_MAX_LINE_LEN];
144 buf[DEBUG_MAX_LINE_LEN-1] = 0;
145 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
146 //if( len < DEBUG_MAX_LINE )
152 void Debug_KernelPanic()
154 gbDebug_IsKPanic = 1;
155 KernelPanic_SetMode();
159 * \fn void LogF(char *Msg, ...)
161 void LogF(char *Fmt, ...)
167 Debug_Fmt(Fmt, args);
172 * \fn void Debug(char *Msg, ...)
173 * \brief Print only to the debug channel
175 void Debug(char *Fmt, ...)
179 Debug_Puts(0, "Debug: ");
181 Debug_DbgOnlyFmt(Fmt, args);
183 Debug_PutCharDebug('\n');
186 * \fn void Log(char *Msg, ...)
188 void Log(char *Fmt, ...)
192 Debug_Puts(1, "Log: ");
194 Debug_Fmt(Fmt, args);
198 void Warning(char *Fmt, ...)
201 Debug_Puts(1, "Warning: ");
203 Debug_Fmt(Fmt, args);
207 void Panic(char *Fmt, ...)
213 Debug_Puts(1, "Panic: ");
215 Debug_Fmt(Fmt, args);
221 __asm__ __volatile__ ("xchg %bx, %bx");
222 __asm__ __volatile__ ("cli;\n\thlt");
223 for(;;) __asm__ __volatile__ ("hlt");
226 void Debug_SetKTerminal(char *File)
229 if(giDebug_KTerm != -1) {
234 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
235 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
237 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
240 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
243 int i = gDebug_Level ++;
246 va_start(args, ArgTypes);
248 while(i--) Debug_Putchar(' ');
250 Debug_Puts(1, FuncName); Debug_Puts(1, ": (");
254 pos = strpos(ArgTypes, ' ');
255 if(pos != -1) ArgTypes[pos] = '\0';
256 if(pos == -1 || pos > 1) {
257 Debug_Puts(1, ArgTypes+1);
260 if(pos != -1) ArgTypes[pos] = ' ';
263 case 'p': Debug_Fmt("%p", args); break;
264 case 's': Debug_Fmt("'%s'", args); break;
265 case 'i': Debug_Fmt("%i", args); break;
266 case 'u': Debug_Fmt("%u", args); break;
267 case 'x': Debug_Fmt("0x%x", args); break;
268 case 'b': Debug_Fmt("0b%b", args); break;
269 case 'X': Debug_Fmt("0x%llx", args); break; // Extended (64-Bit)
270 case 'B': Debug_Fmt("0b%llb", args); break; // Extended (64-Bit)
273 Debug_Putchar(','); Debug_Putchar(' ');
277 ArgTypes = &ArgTypes[pos+1];
281 Debug_Putchar(')'); Debug_Putchar('\n');
284 void Debug_Log(char *FuncName, char *Fmt, ...)
287 int i = gDebug_Level;
291 while(i--) Debug_Putchar(' ');
293 Debug_Puts(1, FuncName); Debug_Puts(1, ": ");
294 Debug_Fmt(Fmt, args);
300 void Debug_Leave(char *FuncName, char RetType, ...)
303 int i = --gDebug_Level;
305 va_start(args, RetType);
312 while(i--) Debug_Putchar(' ');
314 Debug_Puts(1, FuncName); Debug_Puts(1, ": RETURN");
325 case 'n': Debug_Puts(1, "NULL"); break;
326 case 'p': Debug_Fmt("%p", args); break;
327 case 's': Debug_Fmt("'%s'", args); break;
328 case 'i': Debug_Fmt("%i", args); break;
329 case 'u': Debug_Fmt("%u", args); break;
330 case 'x': Debug_Fmt("0x%x", args); break;
332 case 'X': Debug_Fmt("0x%llx", args); break;
339 void Debug_HexDump(char *Header, void *Data, Uint Length)
343 Debug_Puts(1, Header);
344 LogF(" (Hexdump of %p)\n", Data);
348 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
349 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
350 " %02x %02x %02x %02x %02x %02x %02x %02x"
351 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
353 cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
354 cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
355 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
356 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
363 LogF("Log: %04x: ", pos);