[NTOS:KD] Pass PreviousMode down to KdpPrintString, since ExGetPreviousMode is not...
[reactos.git] / ntoskrnl / kd / kdmain.c
index 0b13fae..d219f1b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Kernel
- * FILE:            ntoskrnl/kd/kdinit.c
+ * FILE:            ntoskrnl/kd/kdmain.c
  * PURPOSE:         Kernel Debugger Initialization
  *
  * PROGRAMMERS:     Alex Ionescu (alex@relsoft.net)
@@ -26,8 +26,8 @@ VOID NTAPI PspDumpThreads(BOOLEAN SystemThreads);
 
 typedef struct
 {
-       ULONG ComponentId;
-       ULONG Level;
+    ULONG ComponentId;
+    ULONG Level;
 } KD_COMPONENT_DATA;
 #define MAX_KD_COMPONENT_TABLE_ENTRIES 128
 KD_COMPONENT_DATA KdComponentTable[MAX_KD_COMPONENT_TABLE_ENTRIES];
@@ -41,14 +41,15 @@ ULONG
 NTAPI
 KdpServiceDispatcher(ULONG Service,
                      PVOID Buffer1,
-                     ULONG Buffer1Length)
+                     ULONG Buffer1Length,
+                     KPROCESSOR_MODE PreviousMode)
 {
     ULONG Result = 0;
 
     switch (Service)
     {
         case BREAKPOINT_PRINT: /* DbgPrint */
-            Result = KdpPrintString(Buffer1, Buffer1Length);
+            Result = KdpPrintString(Buffer1, Buffer1Length, PreviousMode);
             break;
 
 #if DBG
@@ -74,6 +75,7 @@ KdpServiceDispatcher(ULONG Service,
             break;
         }
 
+#if defined(_M_IX86) && !defined(_WINKD_) // See ke/i386/traphdlr.c
         /* Register a debug callback */
         case 'CsoR':
         {
@@ -90,6 +92,7 @@ KdpServiceDispatcher(ULONG Service,
             }
             break;
         }
+#endif
 
         /* Special  case for stack frame dumps */
         case 'DsoR':
@@ -98,7 +101,7 @@ KdpServiceDispatcher(ULONG Service,
             break;
         }
 
-#if KDBG
+#if defined(KDBG)
         /* Register KDBG CLI callback */
         case 'RbdK':
         {
@@ -109,7 +112,7 @@ KdpServiceDispatcher(ULONG Service,
 #endif /* DBG */
         default:
             DPRINT1("Invalid debug service call!\n");
-            HalDisplayString("Invalid debug service call!\n");
+            HalDisplayString("Invalid debug service call!\r\n");
             break;
     }
 
@@ -143,7 +146,8 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
             /* Print the string */
             KdpServiceDispatcher(BREAKPOINT_PRINT,
                                  (PVOID)ExceptionRecord->ExceptionInformation[1],
-                                 ExceptionRecord->ExceptionInformation[2]);
+                                 ExceptionRecord->ExceptionInformation[2],
+                                 PreviousMode);
 
             /* Return success */
             KeSetContextReturnRegister(Context, STATUS_SUCCESS);
@@ -151,11 +155,38 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
 #ifdef KDBG
         else if (ExceptionCommand == BREAKPOINT_LOAD_SYMBOLS)
         {
+            PKD_SYMBOLS_INFO SymbolsInfo;
+            KD_SYMBOLS_INFO CapturedSymbolsInfo;
             PLDR_DATA_TABLE_ENTRY LdrEntry;
 
-            /* Load symbols. Currently implemented only for KDBG! */
-            if(KdbpSymFindModule(((PKD_SYMBOLS_INFO)ExceptionRecord->ExceptionInformation[2])->BaseOfDll, NULL, -1, &LdrEntry))
-                KdbSymProcessSymbols(LdrEntry);
+            SymbolsInfo = (PKD_SYMBOLS_INFO)ExceptionRecord->ExceptionInformation[2];
+            if (PreviousMode != KernelMode)
+            {
+                _SEH2_TRY
+                {
+                    ProbeForRead(SymbolsInfo,
+                                 sizeof(*SymbolsInfo),
+                                 1);
+                    RtlCopyMemory(&CapturedSymbolsInfo,
+                                  SymbolsInfo,
+                                  sizeof(*SymbolsInfo));
+                    SymbolsInfo = &CapturedSymbolsInfo;
+                }
+                _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+                {
+                    SymbolsInfo = NULL;
+                }
+                _SEH2_END;
+            }
+
+            if (SymbolsInfo != NULL)
+            {
+                /* Load symbols. Currently implemented only for KDBG! */
+                if (KdbpSymFindModule(SymbolsInfo->BaseOfDll, NULL, -1, &LdrEntry))
+                {
+                    KdbSymProcessSymbols(LdrEntry);
+                }
+            }
         }
         else if (ExceptionCommand == BREAKPOINT_PROMPT)
         {
@@ -173,7 +204,8 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
                                     (USHORT)ExceptionRecord->
                                     ExceptionInformation[2],
                                     OutString,
-                                    OutStringLength);
+                                    OutStringLength,
+                                    PreviousMode);
 
             /* Return the number of characters that we received */
             Context->Eax = ReturnValue;
@@ -189,13 +221,8 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
     /* Check if this is an assertion failure */
     if (ExceptionRecord->ExceptionCode == STATUS_ASSERTION_FAILURE)
     {
-        /* Warn about it */
-        DbgPrint("\n!!! Assertion Failure at Address 0x%p !!!\n\n",
-                 (PVOID)Context->Eip);
-
-        /* Bump EIP to the instruction following the int 2C and return */
+        /* Bump EIP to the instruction following the int 2C */
         Context->Eip += 2;
-        return TRUE;
     }
 #endif
 
@@ -298,6 +325,7 @@ KdDisableDebugger(VOID)
 
     /* Disable the Debugger */
     KdDebuggerEnabled = FALSE;
+    SharedUserData->KdDebuggerEnabled = FALSE;
 
     /* Lower the IRQL */
     KeLowerIrql(OldIrql);
@@ -322,6 +350,7 @@ KdEnableDebugger(VOID)
 
     /* Enable the Debugger */
     KdDebuggerEnabled = TRUE;
+    SharedUserData->KdDebuggerEnabled = TRUE;
 
     /* Lower the IRQL */
     KeLowerIrql(OldIrql);
@@ -380,7 +409,7 @@ NtQueryDebugFilterState(IN ULONG ComponentId,
         Level = 1 << Level;
 
     /* Check if it is not the default component */
-    if (ComponentId != DPFLTR_DEFAULT_ID)
+    if (ComponentId != MAXULONG)
     {
         /* No, search for an existing entry in the table */
         for (i = 0; i < KdComponentTableEntries; i++)
@@ -412,7 +441,7 @@ NtSetDebugFilterState(IN ULONG ComponentId,
     Level &= ~DPFLTR_MASK;
 
     /* Check if it is the default component */
-    if (ComponentId == DPFLTR_DEFAULT_ID)
+    if (ComponentId == MAXULONG)
     {
         /* Yes, modify the default mask */
         if (State)
@@ -466,7 +495,10 @@ KdSystemDebugControl(IN SYSDBG_COMMAND Command,
                      IN KPROCESSOR_MODE PreviousMode)
 {
     /* HACK */
-    return KdpServiceDispatcher(Command, InputBuffer, InputBufferLength);
+    return KdpServiceDispatcher(Command,
+                                InputBuffer,
+                                InputBufferLength,
+                                PreviousMode);
 }
 
 PKDEBUG_ROUTINE KiDebugRoutine = KdpEnterDebuggerException;