Kernel/debug - Clean up Debug() method, bind to #define config
[tpg/acess2.git] / KernelLand / Kernel / debug.c
index 6132bd9..3db5387 100644 (file)
@@ -4,23 +4,23 @@
  */
 #include <acess.h>
 #include <stdarg.h>
+#include <debug_hooks.h>
 
 #define        DEBUG_MAX_LINE_LEN      256
-#define        LOCK_DEBUG_OUTPUT       1       // Avoid interleaving of output lines?
-#define TRACE_TO_KTERM 0       // Send ENTER/DEBUG/LEAVE to debug?
+#define        LOCK_DEBUG_OUTPUT       0       // Avoid interleaving of output lines?
+#define TRACE_TO_KTERM         0       // Send ENTER/DEBUG/LEAVE/Debug to the VTerm
 
 // === IMPORTS ===
-extern void    Threads_Dump(void);
 extern void    KernelPanic_SetMode(void);
 extern void    KernelPanic_PutChar(char Ch);
 extern void    IPStack_SendDebugText(const char *Text);
+extern void    VT_SetTerminal(int TerminalID);
 
 // === PROTOTYPES ===
 static void    Debug_Putchar(char ch);
 static void    Debug_Puts(int bUseKTerm, const char *Str);
-void   Debug_DbgOnlyFmt(const char *format, va_list args);
 void   Debug_FmtS(int bUseKTerm, const char *format, ...);
-void   Debug_Fmt(int bUseKTerm, const char *format, va_list args);
+bool   Debug_Fmt(int bUseKTerm, const char *format, va_list args);
 void   Debug_SetKTerminal(const char *File);
 
 // === GLOBALS ===
@@ -39,7 +39,10 @@ static void Debug_Putchar(char ch)
 {
        Debug_PutCharDebug(ch);
        
-       if( !gbDebug_IsKPanic )
+       if( gbDebug_IsKPanic )
+               KernelPanic_PutChar(ch);
+
+       if( gbDebug_IsKPanic < 2 )
        {
                if(gbInPutChar) return ;
                gbInPutChar = 1;
@@ -47,8 +50,6 @@ static void Debug_Putchar(char ch)
                        VFS_Write(giDebug_KTerm, 1, &ch);
                gbInPutChar = 0;
        }
-       else
-               KernelPanic_PutChar(ch);
        
        if( gbSendNetworkDebug )
        {
@@ -75,30 +76,26 @@ static void Debug_Puts(int UseKTerm, const char *Str)
                IPStack_SendDebugText(Str);
 
        // Output to the kernel terminal
-       if( UseKTerm && !gbDebug_IsKPanic && giDebug_KTerm != -1)
+       if( UseKTerm && gbDebug_IsKPanic < 2 && giDebug_KTerm != -1 && gbInPutChar == 0)
        {
-               if(gbInPutChar) return ;
                gbInPutChar = 1;
                VFS_Write(giDebug_KTerm, len, Str);
                gbInPutChar = 0;
        }
 }
 
-void Debug_DbgOnlyFmt(const char *format, va_list args)
-{
-       Debug_Fmt(0, format, args);
-}
-
-void Debug_Fmt(int bUseKTerm, const char *format, va_list args)
+bool Debug_Fmt(int bUseKTerm, const char *format, va_list args)
 {
        char    buf[DEBUG_MAX_LINE_LEN];
-//      int    len;
        buf[DEBUG_MAX_LINE_LEN-1] = 0;
-       /*len = */vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
-       //if( len < DEBUG_MAX_LINE )
-               // do something
+       size_t len = vsnprintf(buf, DEBUG_MAX_LINE_LEN-1, format, args);
        Debug_Puts(bUseKTerm, buf);
-       return ;
+       if( len > DEBUG_MAX_LINE_LEN-1 ) {
+               // do something
+               Debug_Puts(bUseKTerm, "[...]");
+               return false;
+       }
+       return true;
 }
 
 void Debug_FmtS(int bUseKTerm, const char *format, ...)
@@ -111,34 +108,44 @@ void Debug_FmtS(int bUseKTerm, const char *format, ...)
 
 void Debug_KernelPanic(void)
 {
-       #if LOCK_DEBUG_OUTPUT
-       SHORTREL(&glDebug_Lock);
-       #endif
-       gbDebug_IsKPanic = 1;
+       // 5 nested panics? Fuck it
+       if( gbDebug_IsKPanic > 5 )
+               HALT_CPU();
+       gbDebug_IsKPanic ++;
+       if( gbDebug_IsKPanic == 1 )
+       {
+               #if LOCK_DEBUG_OUTPUT
+               SHORTREL(&glDebug_Lock);
+               #endif
+               VT_SetTerminal(7);
+       }
        KernelPanic_SetMode();
 }
 
 /**
  * \fn void LogF(const char *Msg, ...)
  * \brief Raw debug log (no new line, no prefix)
+ * \return True if all of the provided text was printed
  */
-void LogF(const char *Fmt, ...)
+bool LogF(const char *Fmt, ...)
 {
-       va_list args;
-
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) {
+               Debug_Puts("[#]");
+               return true;
+       }
        SHORTLOCK(&glDebug_Lock);
        #endif
        
+       va_list args;
        va_start(args, Fmt);
-
-       Debug_Fmt(1, Fmt, args);
-
+       bool rv = Debug_Fmt(1, Fmt, args);
        va_end(args);
        
        #if LOCK_DEBUG_OUTPUT
        SHORTREL(&glDebug_Lock);
        #endif
+       return rv;
 }
 /**
  * \fn void Debug(const char *Msg, ...)
@@ -149,15 +156,15 @@ void Debug(const char *Fmt, ...)
        va_list args;
        
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
 
-       Debug_Puts(0, "Debug: ");
+       Debug_Puts(TRACE_TO_KTERM, "Debug: ");
        va_start(args, Fmt);
-       Debug_DbgOnlyFmt(Fmt, args);
+       Debug_Fmt(TRACE_TO_KTERM, Fmt, args);
        va_end(args);
-       Debug_PutCharDebug('\r');
-       Debug_PutCharDebug('\n');
+       Debug_Puts(TRACE_TO_KTERM, "\r\n");
        #if LOCK_DEBUG_OUTPUT
        SHORTREL(&glDebug_Lock);
        #endif
@@ -167,6 +174,7 @@ void Debug(const char *Fmt, ...)
 void LogFV(const char *Fmt, va_list args)
 {
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
 
@@ -180,6 +188,7 @@ void LogFV(const char *Fmt, va_list args)
 void LogV(const char *Fmt, va_list args)
 {
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
 
@@ -208,6 +217,7 @@ void Warning(const char *Fmt, ...)
        va_list args;
        
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
        
@@ -227,37 +237,36 @@ void Panic(const char *Fmt, ...)
        va_list args;
        
        #if LOCK_DEBUG_OUTPUT
-       SHORTLOCK(&glDebug_Lock);
+       if( !CPU_HAS_LOCK(&glDebug_Lock) )
+               SHORTLOCK(&glDebug_Lock);
        #endif
        // And never SHORTREL
        
        Debug_KernelPanic();
        
+       Debug_Puts(1, "\x1b[31m");
        Debug_Puts(1, "Panic: ");
        va_start(args, Fmt);
        Debug_Fmt(1, Fmt, args);
        va_end(args);
-       Debug_Putchar('\r');
-       Debug_Putchar('\n');
+       Debug_Puts(1, "\x1b[0m\r\n");
 
-       Threads_Dump();
-       Heap_Dump();
+       Proc_PrintBacktrace();
+       //Threads_Dump();
+       //Heap_Dump();
 
-       for(;;) ;
+       HALT_CPU();
 }
 
 void Debug_SetKTerminal(const char *File)
 {
-        int    tmp;
        if(giDebug_KTerm != -1) {
-               tmp = giDebug_KTerm;
+               // Clear FD to -1 before closing (prevents writes to closed FD)
+               int oldfd = giDebug_KTerm;
                giDebug_KTerm = -1;
-               VFS_Close(tmp);
+               VFS_Close(oldfd);
        }
-       tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
-//     Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
-       giDebug_KTerm = tmp;
-//     Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
+       giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE);
 }
 
 void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
@@ -268,6 +277,7 @@ void Debug_Enter(const char *FuncName, const char *ArgTypes, ...)
        tTID    tid = Threads_GetTID();
         
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
 
@@ -328,6 +338,7 @@ void Debug_Log(const char *FuncName, const char *Fmt, ...)
        tTID    tid = Threads_GetTID();
 
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
 
@@ -356,6 +367,7 @@ void Debug_Leave(const char *FuncName, char RetType, ...)
        tTID    tid = Threads_GetTID();
 
        #if LOCK_DEBUG_OUTPUT
+       if(CPU_HAS_LOCK(&glDebug_Lock)) return ;
        SHORTLOCK(&glDebug_Lock);
        #endif
        
@@ -411,16 +423,16 @@ void Debug_HexDump(const char *Header, const void *Data, size_t Length)
        Uint    pos = 0;
        LogF("%014lli ", now());
        Debug_Puts(1, Header);
-       LogF(" (Hexdump of %p)\r\n", Data);
+       LogF(" (Hexdump of %p+%i)\r\n", Data, Length);
 
        #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<0x7F) ? cdat[(n)] : '.')
 
        while(Length >= 16)
        {
                LogF("%014lli Log: %04x:"
-                       " %02x %02x %02x %02x %02x %02x %02x %02x"
-                       " %02x %02x %02x %02x %02x %02x %02x %02x"
-                       "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c\r\n",
+                       " %02x %02x %02x %02x %02x %02x %02x %02x "
+                       " %02x %02x %02x %02x %02x %02x %02x %02x "
+                       " %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c\r\n",
                        now(),
                        pos,
                        cdat[ 0], cdat[ 1], cdat[ 2], cdat[ 3], cdat[ 4], cdat[ 5], cdat[ 6], cdat[ 7],

UCC git Repository :: git.ucc.asn.au