[NTOS:KD] Merge KdpReportExceptionStateChange() with kd64 version
authorHervé Poussineau <hpoussin@reactos.org>
Thu, 9 Apr 2020 12:31:47 +0000 (14:31 +0200)
committerHervé Poussineau <hpoussin@reactos.org>
Mon, 16 Nov 2020 07:55:04 +0000 (08:55 +0100)
ntoskrnl/include/internal/kd64.h
ntoskrnl/kd/kdio.c
ntoskrnl/kd/kdmain.c
ntoskrnl/kd64/kdapi.c
ntoskrnl/kd64/kdtrap.c

index 1806b2f..4802c9c 100644 (file)
@@ -281,10 +281,6 @@ NTAPI
 KdpReportExceptionStateChange(
     IN PEXCEPTION_RECORD ExceptionRecord,
     IN OUT PCONTEXT Context,
-#ifndef _WINKD_
-    IN PKTRAP_FRAME TrapFrame,
-    IN KPROCESSOR_MODE PreviousMode,
-#endif
     IN BOOLEAN SecondChanceException
 );
 
index d98ab45..20b3c14 100644 (file)
@@ -43,6 +43,12 @@ volatile ULONG KdbDmesgTotalWritten = 0;
 volatile BOOLEAN KdbpIsInDmesgMode = FALSE;
 static KSPIN_LOCK KdpDmesgLogSpinLock;
 
+static ULONG KdbgNextApiNumber = DbgKdContinueApi;
+static CONTEXT KdbgContext;
+static EXCEPTION_RECORD64 KdbgExceptionRecord;
+static BOOLEAN KdbgFirstChanceException;
+static NTSTATUS KdbgContinueStatus = STATUS_SUCCESS;
+
 /* LOCKING FUNCTIONS *********************************************************/
 
 KIRQL
@@ -560,6 +566,50 @@ KdSendPacket(
 #endif
             return;
         }
+        else if (WaitStateChange->NewState == DbgKdExceptionStateChange)
+        {
+            KdbgNextApiNumber = DbgKdGetContextApi;
+            KdbgExceptionRecord = WaitStateChange->u.Exception.ExceptionRecord;
+            KdbgFirstChanceException = WaitStateChange->u.Exception.FirstChance;
+            return;
+        }
+    }
+    else if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
+    {
+        PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
+        if (ManipulateState->ApiNumber == DbgKdGetContextApi)
+        {
+            KD_CONTINUE_TYPE Result;
+
+#ifdef KDBG
+            /* Check if this is an assertion failure */
+            if (KdbgExceptionRecord.ExceptionCode == STATUS_ASSERTION_FAILURE)
+            {
+                /* Bump EIP to the instruction following the int 2C */
+                KdbgContext.Eip += 2;
+            }
+
+            Result = KdbEnterDebuggerException(&KdbgExceptionRecord,
+                                               KernelMode, // FIXME
+                                               &KdbgContext,
+                                               KdbgFirstChanceException);
+#else
+            /* We'll manually dump the stack for the user... */
+            KeRosDumpStackFrames(NULL, 0);
+            Result = kdHandleException;
+#endif
+            if (Result != kdHandleException)
+                KdbgContinueStatus = STATUS_SUCCESS;
+            else
+                KdbgContinueStatus = STATUS_UNSUCCESSFUL;
+            KdbgNextApiNumber = DbgKdSetContextApi;
+            return;
+        }
+        else if (ManipulateState->ApiNumber == DbgKdSetContextApi)
+        {
+            KdbgNextApiNumber = DbgKdContinueApi;
+            return;
+        }
     }
     UNIMPLEMENTED;
 }
@@ -586,8 +636,32 @@ KdReceivePacket(
     if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
     {
         PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
+        RtlZeroMemory(MessageHeader->Buffer, MessageHeader->MaximumLength);
+        if (KdbgNextApiNumber == DbgKdGetContextApi)
+        {
+            ManipulateState->ApiNumber = DbgKdGetContextApi;
+            MessageData->Length = 0;
+            MessageData->Buffer = (PCHAR)&KdbgContext;
+            return KdPacketReceived;
+        }
+        else if (KdbgNextApiNumber == DbgKdSetContextApi)
+        {
+            ManipulateState->ApiNumber = DbgKdSetContextApi;
+            MessageData->Length = sizeof(KdbgContext);
+            MessageData->Buffer = (PCHAR)&KdbgContext;
+            return KdPacketReceived;
+        }
+        else if (KdbgNextApiNumber != DbgKdContinueApi)
+        {
+            UNIMPLEMENTED;
+        }
         ManipulateState->ApiNumber = DbgKdContinueApi;
-        ManipulateState->u.Continue.ContinueStatus = STATUS_SUCCESS;
+        ManipulateState->u.Continue.ContinueStatus = KdbgContinueStatus;
+
+        /* Prepare for next time */
+        KdbgNextApiNumber = DbgKdContinueApi;
+        KdbgContinueStatus = STATUS_SUCCESS;
+
         return KdPacketReceived;
     }
 
index dde2e1d..76e2896 100644 (file)
@@ -17,52 +17,6 @@ VOID NTAPI PspDumpThreads(BOOLEAN SystemThreads);
 
 extern ANSI_STRING KdpLogFileName;
 
-/* PRIVATE FUNCTIONS *********************************************************/
-
-BOOLEAN
-NTAPI
-KdpReportExceptionStateChange(IN PEXCEPTION_RECORD ExceptionRecord,
-                              IN OUT PCONTEXT ContextRecord,
-                              IN PKTRAP_FRAME TrapFrame,
-                              IN KPROCESSOR_MODE PreviousMode,
-                              IN BOOLEAN SecondChanceException)
-{
-    KD_CONTINUE_TYPE Return = kdHandleException;
-#ifdef KDBG
-    EXCEPTION_RECORD64 ExceptionRecord64;
-
-    /* Check if this is an assertion failure */
-    if (ExceptionRecord->ExceptionCode == STATUS_ASSERTION_FAILURE)
-    {
-        /* Bump EIP to the instruction following the int 2C */
-        ContextRecord->Eip += 2;
-    }
-
-    ExceptionRecord32To64((PEXCEPTION_RECORD32)ExceptionRecord,
-                          &ExceptionRecord64);
-#endif
-
-    /* Get out of here if the Debugger isn't connected */
-    if (KdDebuggerNotPresent) return FALSE;
-
-#ifdef KDBG
-    /* Call KDBG if available */
-    Return = KdbEnterDebuggerException(&ExceptionRecord64,
-                                       PreviousMode,
-                                       ContextRecord,
-                                       !SecondChanceException);
-#else /* not KDBG */
-    /* We'll manually dump the stack for the user... */
-    KeRosDumpStackFrames(NULL, 0);
-#endif /* not KDBG */
-
-    /* Debugger didn't handle it, please handle! */
-    if (Return == kdHandleException) return FALSE;
-
-    /* Debugger handled it */
-    return TRUE;
-}
-
 /* PUBLIC FUNCTIONS *********************************************************/
 
 static PCHAR
index f2c07de..28438a6 100644 (file)
@@ -1725,7 +1725,6 @@ KdpReportCommandStringStateChange(IN PSTRING NameString,
     } while (Status == ContinueProcessorReselected);
 }
 
-#ifdef _WINKD_
 BOOLEAN
 NTAPI
 KdpReportExceptionStateChange(IN PEXCEPTION_RECORD ExceptionRecord,
@@ -1780,7 +1779,6 @@ KdpReportExceptionStateChange(IN PEXCEPTION_RECORD ExceptionRecord,
     /* Return */
     return Status;
 }
-#endif
 
 VOID
 NTAPI
@@ -1844,10 +1842,6 @@ KdpSwitchProcessor(IN PEXCEPTION_RECORD ExceptionRecord,
     /* Report a state change */
     Status = KdpReportExceptionStateChange(ExceptionRecord,
                                            ContextRecord,
-#ifndef _WINKD_
-                                           NULL,
-                                           KernelMode,
-#endif
                                            SecondChanceException);
 
     /* Restore the port data and return */
index 8f09895..dc78b11 100644 (file)
@@ -116,10 +116,6 @@ KdpReport(IN PKTRAP_FRAME TrapFrame,
     Handled = KdpReportExceptionStateChange(ExceptionRecord,
                                             &Prcb->ProcessorState.
                                             ContextFrame,
-#ifndef _WINKD_
-                                            TrapFrame,
-                                            PreviousMode,
-#endif
                                             SecondChanceException);
 
     /* Now restore the processor state, manually again. */