2 * AcessOS Microkernel Version
8 #define DEBUG_MAX_LINE_LEN 256
9 #define LOCK_DEBUG_OUTPUT 1 // Avoid interleaving of output lines?
10 #define TRACE_TO_KTERM 0 // Send ENTER/DEBUG/LEAVE to debug?
13 extern void Threads_Dump(void);
14 extern void KernelPanic_SetMode(void);
15 extern void KernelPanic_PutChar(char Ch);
16 extern void IPStack_SendDebugText(const char *Text);
19 static void Debug_Putchar(char ch);
20 static void Debug_Puts(int bUseKTerm, const char *Str);
21 void Debug_DbgOnlyFmt(const char *format, va_list args);
22 void Debug_FmtS(int bUseKTerm, const char *format, ...);
23 void Debug_Fmt(int bUseKTerm, const char *format, va_list args);
24 void Debug_SetKTerminal(const char *File);
28 int giDebug_KTerm = -1;
29 int gbDebug_IsKPanic = 0;
30 volatile int gbInPutChar = 0;
32 tShortSpinlock glDebug_Lock;
34 // - Disabled because it breaks shit
35 int gbSendNetworkDebug = 0;
38 static void Debug_Putchar(char ch)
40 Debug_PutCharDebug(ch);
42 if( !gbDebug_IsKPanic )
44 if(gbInPutChar) return ;
46 if(giDebug_KTerm != -1)
47 VFS_Write(giDebug_KTerm, 1, &ch);
51 KernelPanic_PutChar(ch);
53 if( gbSendNetworkDebug )
55 char str[2] = {ch, 0};
56 IPStack_SendDebugText(str);
60 static void Debug_Puts(int UseKTerm, const char *Str)
64 Debug_PutStringDebug(Str);
66 if( gbDebug_IsKPanic )
68 for( len = 0; Str[len]; len ++ )
69 KernelPanic_PutChar( Str[len] );
72 for( len = 0; Str[len]; len ++ );
74 if( gbSendNetworkDebug )
75 IPStack_SendDebugText(Str);
77 // Output to the kernel terminal
78 if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
80 if(gbInPutChar) return ;
82 VFS_Write(giDebug_KTerm, len, Str);
87 void Debug_DbgOnlyFmt(const char *format, va_list args)
89 Debug_Fmt(0, format, args);
92 void Debug_Fmt(int bUseKTerm, const char *format, va_list args)
94 char buf[DEBUG_MAX_LINE_LEN];
96 buf[DEBUG_MAX_LINE_LEN-1] = 0;
97 /*len = */vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
98 //if( len < DEBUG_MAX_LINE )
100 Debug_Puts(bUseKTerm, buf);
104 void Debug_FmtS(int bUseKTerm, const char *format, ...)
107 va_start(args, format);
108 Debug_Fmt(bUseKTerm, format, args);
112 void Debug_KernelPanic(void)
114 #if LOCK_DEBUG_OUTPUT
115 SHORTREL(&glDebug_Lock);
117 gbDebug_IsKPanic = 1;
118 KernelPanic_SetMode();
122 * \fn void LogF(const char *Msg, ...)
123 * \brief Raw debug log (no new line, no prefix)
125 void LogF(const char *Fmt, ...)
129 #if LOCK_DEBUG_OUTPUT
130 SHORTLOCK(&glDebug_Lock);
135 Debug_Fmt(1, Fmt, args);
139 #if LOCK_DEBUG_OUTPUT
140 SHORTREL(&glDebug_Lock);
144 * \fn void Debug(const char *Msg, ...)
145 * \brief Print only to the debug channel (not KTerm)
147 void Debug(const char *Fmt, ...)
151 #if LOCK_DEBUG_OUTPUT
152 SHORTLOCK(&glDebug_Lock);
155 Debug_Puts(0, "Debug: ");
157 Debug_DbgOnlyFmt(Fmt, args);
159 Debug_PutCharDebug('\r');
160 Debug_PutCharDebug('\n');
161 #if LOCK_DEBUG_OUTPUT
162 SHORTREL(&glDebug_Lock);
166 * \fn void Log(const char *Msg, ...)
168 void Log(const char *Fmt, ...)
172 #if LOCK_DEBUG_OUTPUT
173 SHORTLOCK(&glDebug_Lock);
176 Debug_Puts(1, "Log: ");
178 Debug_Fmt(1, Fmt, args);
180 Debug_Puts(1, "\r\n");
182 #if LOCK_DEBUG_OUTPUT
183 SHORTREL(&glDebug_Lock);
186 void Warning(const char *Fmt, ...)
190 #if LOCK_DEBUG_OUTPUT
191 SHORTLOCK(&glDebug_Lock);
194 Debug_Puts(1, "Warning: ");
196 Debug_Fmt(1, Fmt, args);
201 #if LOCK_DEBUG_OUTPUT
202 SHORTREL(&glDebug_Lock);
205 void Panic(const char *Fmt, ...)
209 #if LOCK_DEBUG_OUTPUT
210 SHORTLOCK(&glDebug_Lock);
212 // And never SHORTREL
216 Debug_Puts(1, "Panic: ");
218 Debug_Fmt(1, Fmt, args);
229 void Debug_SetKTerminal(const char *File)
232 if(giDebug_KTerm != -1) {
237 tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
238 // Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
240 // Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
243 void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
248 tTID tid = Threads_GetTID();
250 #if LOCK_DEBUG_OUTPUT
251 SHORTLOCK(&glDebug_Lock);
256 va_start(args, ArgTypes);
258 Debug_FmtS(TRACE_TO_KTERM, "%014lli ", now());
259 while(i--) Debug_Puts(TRACE_TO_KTERM, " ");
261 Debug_Puts(TRACE_TO_KTERM, FuncName);
262 Debug_FmtS(TRACE_TO_KTERM, "[%i]", tid);
263 Debug_Puts(TRACE_TO_KTERM, ": (");
267 pos = strpos(ArgTypes, ' ');
268 if(pos == -1 || pos > 1) {
270 Debug_Puts(TRACE_TO_KTERM, ArgTypes+1);
272 Debug_FmtS(TRACE_TO_KTERM, "%.*s", pos-1, ArgTypes+1);
274 Debug_Puts(TRACE_TO_KTERM, "=");
278 case 'p': Debug_FmtS(TRACE_TO_KTERM, "%p", va_arg(args, void*)); break;
279 case 'P': Debug_FmtS(TRACE_TO_KTERM, "%P", va_arg(args, tPAddr)); break;
280 case 's': Debug_FmtS(TRACE_TO_KTERM, "'%s'", va_arg(args, char*)); break;
281 case 'i': Debug_FmtS(TRACE_TO_KTERM, "%i", va_arg(args, int)); break;
282 case 'u': Debug_FmtS(TRACE_TO_KTERM, "%u", va_arg(args, Uint)); break;
283 case 'x': Debug_FmtS(TRACE_TO_KTERM, "0x%x", va_arg(args, Uint)); break;
284 case 'b': Debug_FmtS(TRACE_TO_KTERM, "0b%b", va_arg(args, Uint)); break;
285 case 'X': Debug_FmtS(TRACE_TO_KTERM, "0x%llx", va_arg(args, Uint64)); break; // Extended (64-Bit)
286 case 'B': Debug_FmtS(TRACE_TO_KTERM, "0b%llb", va_arg(args, Uint64)); break; // Extended (64-Bit)
289 Debug_Puts(TRACE_TO_KTERM, ", ");
293 ArgTypes = &ArgTypes[pos+1];
297 Debug_Puts(TRACE_TO_KTERM, ")\r\n");
299 #if LOCK_DEBUG_OUTPUT
300 SHORTREL(&glDebug_Lock);
304 void Debug_Log(const char *FuncName, const char *Fmt, ...)
307 int i = gDebug_Level;
308 tTID tid = Threads_GetTID();
310 #if LOCK_DEBUG_OUTPUT
311 SHORTLOCK(&glDebug_Lock);
314 Debug_FmtS(TRACE_TO_KTERM, "%014lli ", now());
315 while(i--) Debug_Puts(TRACE_TO_KTERM, " ");
317 Debug_Puts(TRACE_TO_KTERM, FuncName);
318 Debug_FmtS(TRACE_TO_KTERM, "[%i]", tid);
319 Debug_Puts(TRACE_TO_KTERM, ": ");
322 Debug_Fmt(TRACE_TO_KTERM, Fmt, args);
325 Debug_Puts(TRACE_TO_KTERM, "\r\n");
327 #if LOCK_DEBUG_OUTPUT
328 SHORTREL(&glDebug_Lock);
332 void Debug_Leave(const char *FuncName, char RetType, ...)
336 tTID tid = Threads_GetTID();
338 #if LOCK_DEBUG_OUTPUT
339 SHORTLOCK(&glDebug_Lock);
344 va_start(args, RetType);
350 Debug_FmtS(TRACE_TO_KTERM, "%014lli ", now());
352 while(i--) Debug_Puts(TRACE_TO_KTERM, " ");
354 Debug_Puts(TRACE_TO_KTERM, FuncName);
355 Debug_FmtS(TRACE_TO_KTERM, "[%i]", tid);
356 Debug_Puts(TRACE_TO_KTERM, ": RETURN");
360 Debug_Puts(TRACE_TO_KTERM, "\r\n");
361 #if LOCK_DEBUG_OUTPUT
362 SHORTREL(&glDebug_Lock);
369 case 'n': Debug_Puts(TRACE_TO_KTERM, " NULL"); break;
370 case 'p': Debug_Fmt(TRACE_TO_KTERM, " %p", args); break;
371 case 'P': Debug_Fmt(TRACE_TO_KTERM, " %P", args); break; // PAddr
372 case 's': Debug_Fmt(TRACE_TO_KTERM, " '%s'", args); break;
373 case 'i': Debug_Fmt(TRACE_TO_KTERM, " %i", args); break;
374 case 'u': Debug_Fmt(TRACE_TO_KTERM, " %u", args); break;
375 case 'x': Debug_Fmt(TRACE_TO_KTERM, " 0x%x", args); break;
377 case 'X': Debug_Fmt(TRACE_TO_KTERM, " 0x%llx", args); break;
379 Debug_Puts(TRACE_TO_KTERM, "\r\n");
383 #if LOCK_DEBUG_OUTPUT
384 SHORTREL(&glDebug_Lock);
388 void Debug_HexDump(const char *Header, const void *Data, Uint Length)
390 const Uint8 *cdat = Data;
392 LogF("%014lli ", now());
393 Debug_Puts(1, Header);
394 LogF(" (Hexdump of %p)\r\n", Data);
396 #define CH(n) ((' '<=cdat[(n)]&&cdat[(n)]<0x7F) ? cdat[(n)] : '.')
400 LogF("%014lli Log: %04x:"
401 " %02x %02x %02x %02x %02x %02x %02x %02x"
402 " %02x %02x %02x %02x %02x %02x %02x %02x"
403 " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c\r\n",
406 cdat[ 0], cdat[ 1], cdat[ 2], cdat[ 3], cdat[ 4], cdat[ 5], cdat[ 6], cdat[ 7],
407 cdat[ 8], cdat[ 9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
408 CH(0), CH(1), CH(2), CH(3), CH(4), CH(5), CH(6), CH(7),
409 CH(8), CH(9), CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
418 LogF("%014lli Log: %04x: ", now(), pos);
419 for(i = 0; i < Length; i ++)
421 LogF("%02x ", cdat[i]);
423 for( ; i < 16; i ++) LogF(" ");
425 for(i = 0; i < Length; i ++)
427 if( i == 8 ) LogF(" ");