- Allow shared read access to the debug.log file.
[reactos.git] / reactos / ntoskrnl / kd / dlog.c
index b54c637..2bd3c10 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: dlog.c,v 1.8 2003/10/12 17:05:45 hbirr Exp $
+/* $Id: dlog.c,v 1.16 2004/09/01 00:15:08 navaraf Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel
 
 /* INCLUDES ******************************************************************/
 
-#include <ddk/ntddk.h>
-#include <internal/ntoskrnl.h>
-#include <roscfg.h>
-#include <internal/kd.h>
-#include <ntos/minmax.h>
+#include <ntoskrnl.h>
 
 /* GLOBALS *******************************************************************/
 
 #define DEBUGLOG_SIZE (32*1024)
 
-#ifdef DBGPRINT_FILE_LOG
-
 static CHAR DebugLog[DEBUGLOG_SIZE];
 static ULONG DebugLogStart;
 static ULONG DebugLogEnd;
@@ -32,13 +26,41 @@ static ULONG DebugLogOverflow;
 static HANDLE DebugLogThreadHandle;
 static CLIENT_ID DebugLogThreadCid;
 static HANDLE DebugLogFile;
-static KSEMAPHORE DebugLogSem;
-
-#endif /* DBGPRINT_FILE_LOG */
+static KEVENT DebugLogEvent;
 
 /* FUNCTIONS *****************************************************************/
 
-#ifdef DBGPRINT_FILE_LOG
+VOID
+DebugLogDumpMessages(VOID)
+{
+  static CHAR Buffer[256];
+  ULONG Offset;
+  ULONG Length;
+
+  if (!(KdDebugState & KD_DEBUG_BOOTLOG))
+    {
+      return;
+    }
+  KdDebugState &= ~KD_DEBUG_BOOTLOG;
+  Offset = (DebugLogEnd + 1) % DEBUGLOG_SIZE;
+  do
+    {
+      if (Offset <= DebugLogEnd)
+       {
+         Length = min(255, DebugLogEnd - Offset);
+       }
+      else
+       {
+         Length = min(255, DEBUGLOG_SIZE - Offset);
+       }
+      memcpy(Buffer, DebugLog + Offset, Length);
+      Buffer[Length] = 0;
+      DbgPrint(Buffer);
+      Offset = (Offset + Length) % DEBUGLOG_SIZE;
+    }
+  while (Length > 0);
+}
 
 VOID INIT_FUNCTION
 DebugLogInit(VOID)
@@ -48,10 +70,10 @@ DebugLogInit(VOID)
   DebugLogEnd = 0;
   DebugLogOverflow = 0;
   DebugLogCount = 0;
-  KeInitializeSemaphore(&DebugLogSem, 0, 255);
+  KeInitializeEvent(&DebugLogEvent, NotificationEvent, FALSE);
 }
 
-NTSTATUS
+VOID STDCALL
 DebugLogThreadMain(PVOID Context)
 {
   KIRQL oldIrql;
@@ -61,11 +83,13 @@ DebugLogThreadMain(PVOID Context)
 
   for (;;)
     {
-      KeWaitForSingleObject(&DebugLogSem,
+      LARGE_INTEGER TimeOut;
+      TimeOut.QuadPart = -5000000; /* Half a second. */
+      KeWaitForSingleObject(&DebugLogEvent,
                            0,
                            KernelMode,
                            FALSE,
-                           NULL);
+                           &TimeOut);
       KeAcquireSpinLock(&DebugLogLock, &oldIrql);
       while (DebugLogCount > 0)
        {
@@ -73,6 +97,7 @@ DebugLogThreadMain(PVOID Context)
            {
              WLen = min(256, DEBUGLOG_SIZE - DebugLogStart);
              memcpy(Buffer, &DebugLog[DebugLogStart], WLen);
+              Buffer[WLen + 1] = '\n';
              DebugLogStart = 
                (DebugLogStart + WLen) % DEBUGLOG_SIZE;
              DebugLogCount = DebugLogCount - WLen;
@@ -83,13 +108,13 @@ DebugLogThreadMain(PVOID Context)
                          NULL,
                          &Iosb,
                          Buffer,
-                         WLen,
+                         WLen + 1,
                          NULL,
                          NULL);
            }
          else
            {
-             WLen = min(256, DebugLogEnd - DebugLogStart);
+             WLen = min(255, DebugLogEnd - DebugLogStart);
              memcpy(Buffer, &DebugLog[DebugLogStart], WLen);
              DebugLogStart = 
                (DebugLogStart + WLen) % DEBUGLOG_SIZE;
@@ -107,6 +132,7 @@ DebugLogThreadMain(PVOID Context)
            }
          KeAcquireSpinLock(&DebugLogLock, &oldIrql);
        }
+      KeResetEvent(&DebugLogEvent);
       KeReleaseSpinLock(&DebugLogLock, oldIrql);
     }
 }
@@ -119,7 +145,7 @@ DebugLogInit2(VOID)
   UNICODE_STRING FileName;
   IO_STATUS_BLOCK Iosb;
 
-  RtlInitUnicodeStringFromLiteral(&FileName, L"\\SystemRoot\\debug.log");
+  RtlInitUnicodeString(&FileName, L"\\SystemRoot\\debug.log");
   InitializeObjectAttributes(&ObjectAttributes,
                             &FileName,
                             0,
@@ -132,7 +158,7 @@ DebugLogInit2(VOID)
                        &Iosb,
                        NULL,
                        FILE_ATTRIBUTE_NORMAL,
-                       0,
+                       FILE_SHARE_READ,
                        FILE_SUPERSEDE,
                        FILE_WRITE_THROUGH | FILE_SYNCHRONOUS_IO_NONALERT,
                        NULL,
@@ -152,71 +178,54 @@ DebugLogInit2(VOID)
                                NULL);
 }
 
- VOID 
- DebugLogWrite(PCH String)
- {
-   KIRQL oldIrql;
+VOID 
+DebugLogWrite(PCH String)
+{
+  KIRQL oldIrql;
 
    if (KeGetCurrentIrql() > DISPATCH_LEVEL)
-     {
-       DebugLogOverflow++;
-       return;
-     }
+    {
+      DebugLogOverflow++;
+      return;
+    }
 
    KeAcquireSpinLock(&DebugLogLock, &oldIrql);
 
    if (DebugLogCount == DEBUGLOG_SIZE)
-     {
-       DebugLogOverflow++;
-       KeReleaseSpinLock(&DebugLogLock, oldIrql);
-       if (oldIrql < DISPATCH_LEVEL)
-        {
-          KeReleaseSemaphore(&DebugLogSem, IO_NO_INCREMENT, 1, FALSE);
-        }
-       return;
-     }
+    {
+      DebugLogOverflow++;
+      KeReleaseSpinLock(&DebugLogLock, oldIrql);
+      if (oldIrql < DISPATCH_LEVEL)
+       {
+         KeSetEvent(&DebugLogEvent, IO_NO_INCREMENT, FALSE);
+       }
+       return;
+    }
 
    while ((*String) != 0)
-     {
-       DebugLog[DebugLogEnd] = *String;
-       String++;
-       DebugLogCount++;
-       if (DebugLogCount == DEBUGLOG_SIZE)
-        {         
-          DebugLogOverflow++;
-          KeReleaseSpinLock(&DebugLogLock, oldIrql);
-          if (oldIrql < DISPATCH_LEVEL)
-            {
-              KeReleaseSemaphore(&DebugLogSem, IO_NO_INCREMENT, 1, FALSE);
-            }
-          return;
-        }
-      DebugLogEnd = (DebugLogEnd + 1) % DEBUGLOG_SIZE;
-     }
-
-   KeReleaseSpinLock(&DebugLogLock, oldIrql);
-   if (oldIrql < DISPATCH_LEVEL)
-     {
-       KeReleaseSemaphore(&DebugLogSem, IO_NO_INCREMENT, 1, FALSE);
-     }
- }
-
- #else /* not DBGPRINT_FILE_LOG */
-
- VOID INIT_FUNCTION
- DebugLogInit(VOID)
- {
- }
+    {
+      DebugLog[DebugLogEnd] = *String;
+      String++;
+      DebugLogCount++;
+
+       if (DebugLogCount == DEBUGLOG_SIZE)
+       {          
+         DebugLogOverflow++;
+         KeReleaseSpinLock(&DebugLogLock, oldIrql);
+         if (oldIrql < DISPATCH_LEVEL)
+           {
+             KeSetEvent(&DebugLogEvent, IO_NO_INCREMENT, FALSE);
+           }
+         return;
+       }
+     DebugLogEnd = (DebugLogEnd + 1) % DEBUGLOG_SIZE;
+    }
 
-VOID INIT_FUNCTION
-DebugLogInit2(VOID)
-{
-}
+    KeReleaseSpinLock(&DebugLogLock, oldIrql);
 
-VOID
-DebugLogWrite(PCH String)
-{
+    if (oldIrql < DISPATCH_LEVEL)
+    {
+      KeSetEvent(&DebugLogEvent, IO_NO_INCREMENT, FALSE);
+    }
 }
 
-#endif /* DBGPRINT_FILE_LOG */
-