2 * AcessOS Microkernel Version
5 * TODO: Move the Debug_putchar methods out to the arch/ tree
10 #define DEBUG_MAX_LINE_LEN 256
12 #define LOCK_DEBUG_OUTPUT 1
15 extern void Threads_Dump(void);
16 extern void KernelPanic_SetMode(void);
17 extern void KernelPanic_PutChar(char Ch);
20 static void Debug_Putchar(char ch);
21 static void Debug_Puts(int DbgOnly, const char *Str);
22 void Debug_DbgOnlyFmt(const char *format, va_list args);
23 void Debug_FmtS(const char *format, ...);
24 void Debug_Fmt(const char *format, va_list args);
25 void Debug_SetKTerminal(const char *File);
29 int giDebug_KTerm = -1;
30 int gbDebug_IsKPanic = 0;
31 volatile int gbInPutChar = 0;
33 tShortSpinlock glDebug_Lock;
37 static void Debug_Putchar(char ch)
39 Debug_PutCharDebug(ch);
40 if( !gbDebug_IsKPanic )
42 if(gbInPutChar) return ;
44 if(giDebug_KTerm != -1)
45 VFS_Write(giDebug_KTerm, 1, &ch);
49 KernelPanic_PutChar(ch);
52 static void Debug_Puts(int UseKTerm, const char *Str)
56 Debug_PutStringDebug(Str);
58 if( gbDebug_IsKPanic )
60 for( len = 0; Str[len]; len ++ )
61 KernelPanic_PutChar( Str[len] );
64 for( len = 0; Str[len]; len ++ );
66 // Output to the kernel terminal
67 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
69 if(gbInPutChar) return ;
71 VFS_Write(giDebug_KTerm, len, Str);
76 void Debug_DbgOnlyFmt(const char *format, va_list args)
78 char buf[DEBUG_MAX_LINE_LEN];
80 buf[DEBUG_MAX_LINE_LEN-1] = 0;
81 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
82 //if( len < DEBUG_MAX_LINE )
87 void Debug_Fmt(const char *format, va_list args)
89 char buf[DEBUG_MAX_LINE_LEN];
91 buf[DEBUG_MAX_LINE_LEN-1] = 0;
92 len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
93 //if( len < DEBUG_MAX_LINE )
99 void Debug_FmtS(const char *format, ...)
102 va_start(args, format);
103 Debug_Fmt(format, args);
107 void Debug_KernelPanic()
109 gbDebug_IsKPanic = 1;
110 KernelPanic_SetMode();
114 * \fn void LogF(const char *Msg, ...)
115 * \brief Raw debug log (no new line, no prefix)
117 void LogF(const char *Fmt, ...)
121 #if LOCK_DEBUG_OUTPUT
122 SHORTLOCK(&glDebug_Lock);
127 Debug_Fmt(Fmt, args);
131 #if LOCK_DEBUG_OUTPUT
132 SHORTREL(&glDebug_Lock);
136 * \fn void Debug(const char *Msg, ...)
137 * \brief Print only to the debug channel (not KTerm)
139 void Debug(const char *Fmt, ...)
143 #if LOCK_DEBUG_OUTPUT
144 SHORTLOCK(&glDebug_Lock);
147 Debug_Puts(0, "Debug: ");
149 Debug_DbgOnlyFmt(Fmt, args);
151 Debug_PutCharDebug('\n');
152 #if LOCK_DEBUG_OUTPUT
153 SHORTREL(&glDebug_Lock);
157 * \fn void Log(const char *Msg, ...)
159 void Log(const char *Fmt, ...)
163 #if LOCK_DEBUG_OUTPUT
164 SHORTLOCK(&glDebug_Lock);
167 Debug_Puts(1, "Log: ");
169 Debug_Fmt(Fmt, args);
173 #if LOCK_DEBUG_OUTPUT
174 SHORTREL(&glDebug_Lock);
177 void Warning(const char *Fmt, ...)
181 #if LOCK_DEBUG_OUTPUT
182 SHORTLOCK(&glDebug_Lock);
185 Debug_Puts(1, "Warning: ");
187 Debug_Fmt(Fmt, args);
191 #if LOCK_DEBUG_OUTPUT
192 SHORTREL(&glDebug_Lock);
195 void Panic(const char *Fmt, ...)
199 #if LOCK_DEBUG_OUTPUT
200 SHORTLOCK(&glDebug_Lock);
202 // And never SHORTREL
206 Debug_Puts(1, "Panic: ");
208 Debug_Fmt(Fmt, args);
214 // __asm__ __volatile__ ("xchg %bx, %bx");
215 // __asm__ __volatile__ ("cli;\n\thlt");
216 // for(;;) __asm__ __volatile__ ("hlt");
220 void Debug_SetKTerminal(const char *File)
223 if(giDebug_KTerm != -1) {
228 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
229 Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
231 Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
234 void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
239 tTID tid = Threads_GetTID();
241 #if LOCK_DEBUG_OUTPUT
242 SHORTLOCK(&glDebug_Lock);
247 va_start(args, ArgTypes);
249 LogF("%014lli ", now());
250 while(i--) Debug_Putchar(' ');
252 Debug_Puts(1, FuncName);
253 Debug_FmtS("[%i]", tid);
254 Debug_Puts(1, ": (");
258 pos = strpos(ArgTypes, ' ');
259 if(pos == -1 || pos > 1) {
261 Debug_Puts(1, ArgTypes+1);
263 for( i = 1; i < pos; i ++ )
264 Debug_Putchar(ArgTypes[i]);
270 case 'p': LogF("%p", va_arg(args, void*)); break;
271 case 's': LogF("'%s'", va_arg(args, char*)); break;
272 case 'i': LogF("%i", va_arg(args, int)); break;
273 case 'u': LogF("%u", va_arg(args, Uint)); break;
274 case 'x': LogF("0x%x", va_arg(args, Uint)); break;
275 case 'b': LogF("0b%b", va_arg(args, Uint)); break;
276 case 'X': LogF("0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
277 case 'B': LogF("0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
280 Debug_Putchar(','); Debug_Putchar(' ');
284 ArgTypes = &ArgTypes[pos+1];
288 Debug_Putchar(')'); Debug_Putchar('\n');
290 #if LOCK_DEBUG_OUTPUT
291 SHORTREL(&glDebug_Lock);
295 void Debug_Log(const char *FuncName, const char *Fmt, ...)
298 int i = gDebug_Level;
299 tTID tid = Threads_GetTID();
301 #if LOCK_DEBUG_OUTPUT
302 SHORTLOCK(&glDebug_Lock);
307 LogF("%014lli ", now());
308 while(i--) Debug_Putchar(' ');
310 Debug_Puts(1, FuncName);
311 Debug_FmtS("[%i]", tid);
313 Debug_Fmt(Fmt, args);
318 #if LOCK_DEBUG_OUTPUT
319 SHORTREL(&glDebug_Lock);
323 void Debug_Leave(const char *FuncName, char RetType, ...)
327 tTID tid = Threads_GetTID();
329 #if LOCK_DEBUG_OUTPUT
330 SHORTLOCK(&glDebug_Lock);
335 va_start(args, RetType);
341 LogF("%014lli ", now());
343 while(i--) Debug_Putchar(' ');
345 Debug_Puts(1, FuncName);
346 Debug_FmtS("[%i]", tid);
347 Debug_Puts(1, ": RETURN");
352 #if LOCK_DEBUG_OUTPUT
353 SHORTREL(&glDebug_Lock);
361 case 'n': Debug_Puts(1, "NULL"); break;
362 case 'p': Debug_Fmt("%p", args); break;
363 case 's': Debug_Fmt("'%s'", args); break;
364 case 'i': Debug_Fmt("%i", args); break;
365 case 'u': Debug_Fmt("%u", args); break;
366 case 'x': Debug_Fmt("0x%x", args); break;
368 case 'X': Debug_Fmt("0x%llx", args); break;
374 #if LOCK_DEBUG_OUTPUT
375 SHORTREL(&glDebug_Lock);
379 void Debug_HexDump(const char *Header, const void *Data, Uint Length)
381 const Uint8 *cdat = Data;
383 LogF("%014lli ", now());
384 Debug_Puts(1, Header);
385 LogF(" (Hexdump of %p)\n", Data);
387 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<0x7F) ? cdat[(n)] : '.')
391 LogF("%014lli Log: %04x:"
392 " %02x %02x %02x %02x %02x %02x %02x %02x"
393 " %02x %02x %02x %02x %02x %02x %02x %02x"
394 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c\n",
397 cdat[ 0], cdat[ 1], cdat[ 2], cdat[ 3], cdat[ 4], cdat[ 5], cdat[ 6], cdat[ 7],
398 cdat[ 8], cdat[ 9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
399 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
400 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
409 LogF("%014lli Log: %04x: ", now(), pos);
410 for(i = 0; i < Length; i ++)
412 LogF("%02x ", cdat[i]);
414 for( ; i < 16; i ++) LogF(" ");
416 for(i = 0; i < Length; i ++)
418 if( i == 8 ) LogF(" ");