[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / kdbg / kdb.c
index 1888515..6839044 100644 (file)
@@ -209,6 +209,7 @@ KdbpKdbTrapFrameFromKernelStack(
 
     RtlZeroMemory(KdbTrapFrame, sizeof(KDB_KTRAP_FRAME));
     StackPtr = (ULONG_PTR *) KernelStack;
+#ifdef _M_IX86
     KdbTrapFrame->Tf.Ebp = StackPtr[3];
     KdbTrapFrame->Tf.Edi = StackPtr[4];
     KdbTrapFrame->Tf.Esi = StackPtr[5];
@@ -220,6 +221,7 @@ KdbpKdbTrapFrameFromKernelStack(
     KdbTrapFrame->Tf.SegDs = KGDT_R0_DATA;
     KdbTrapFrame->Tf.SegEs = KGDT_R0_DATA;
     KdbTrapFrame->Tf.SegGs = KGDT_R0_DATA;
+#endif
 
     /* FIXME: what about the other registers??? */
 }
@@ -413,7 +415,7 @@ KdbpStepIntoInstruction(
     }
 
     /* Read the interrupt descriptor table register  */
-    Ke386GetInterruptDescriptorTable(*(PKDESCRIPTOR)&Idtr.Limit);
+    __sidt(&Idtr.Limit);
     if (IntVect >= (Idtr.Limit + 1) / 8)
     {
         /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
@@ -421,7 +423,7 @@ KdbpStepIntoInstruction(
     }
 
     /* Get the interrupt descriptor */
-    if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc, (PVOID)(Idtr.Base + (IntVect * 8)), sizeof (IntDesc))))
+    if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc, (PVOID)(ULONG_PTR)(Idtr.Base + (IntVect * 8)), sizeof (IntDesc))))
     {
         /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
         return FALSE;
@@ -1008,7 +1010,7 @@ KdbpDisableBreakPoint(
             }
         }
 
-        if (i != (ULONG)-1) /* not found */
+        if (i != MAXULONG) /* not found */
             ASSERT(0);
     }
     else
@@ -1033,7 +1035,7 @@ KdbpDisableBreakPoint(
             }
         }
 
-        if (i != (ULONG)-1) /* not found */
+        if (i != MAXULONG) /* not found */
             ASSERT(0);
     }
 
@@ -1126,7 +1128,7 @@ KdbpAttachToThread(
     /* Get a pointer to the thread */
     if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId, &Thread)))
     {
-        KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG)ThreadId);
+        KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG_PTR)ThreadId);
         return FALSE;
     }
     Process = Thread->ThreadsProcess;
@@ -1208,7 +1210,7 @@ KdbpAttachToProcess(
     /* Get a pointer to the process */
     if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId, &Process)))
     {
-        KdbpPrint("Invalid process id: 0x%08x\n", (ULONG)ProcessId);
+        KdbpPrint("Invalid process id: 0x%08x\n", (ULONG_PTR)ProcessId);
         return FALSE;
     }
 
@@ -1216,7 +1218,7 @@ KdbpAttachToProcess(
     ObDereferenceObject(Process);
     if (Entry == &KdbCurrentProcess->ThreadListHead)
     {
-        KdbpPrint("No threads in process 0x%08x, cannot attach to process!\n", (ULONG)ProcessId);
+        KdbpPrint("No threads in process 0x%p, cannot attach to process!\n", ProcessId);
         return FALSE;
     }
 
@@ -1228,7 +1230,7 @@ KdbpAttachToProcess(
 /*!\brief Calls the main loop ...
  */
 static VOID
-KdbpCallMainLoop()
+KdbpCallMainLoop(VOID)
 {
     KdbpCliMainLoop(KdbEnteredOnSingleStep);
 }
@@ -1257,7 +1259,7 @@ KdbpInternalEnter()
     SavedStackLimit = Thread->Tcb.StackLimit;
     SavedKernelStack = Thread->Tcb.KernelStack;
     Thread->Tcb.InitialStack = Thread->Tcb.StackBase = (char*)KdbStack + KDB_STACK_SIZE;
-    Thread->Tcb.StackLimit = (ULONG)KdbStack;
+    Thread->Tcb.StackLimit = (ULONG_PTR)KdbStack;
     Thread->Tcb.KernelStack = (char*)KdbStack + KDB_STACK_SIZE;
 
     /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);*/
@@ -1350,6 +1352,7 @@ KdbEnterDebuggerException(
     BOOLEAN Resume = FALSE;
     BOOLEAN EnterConditionMet = TRUE;
     ULONG OldEflags;
+    KIRQL OldIrql;
     NTSTATUS ExceptionCode;
 
     ExceptionCode = (ExceptionRecord ? ExceptionRecord->ExceptionCode : STATUS_BREAKPOINT);
@@ -1410,6 +1413,8 @@ KdbEnterDebuggerException(
             /* Delete the temporary breakpoint which was used to step over or into the instruction. */
             KdbpDeleteBreakPoint(-1, BreakPoint);
 
+            TrapFrame->Eip--;
+
             if (--KdbNumSingleSteps > 0)
             {
                 if ((KdbSingleStepOver && !KdbpStepOverInstruction(TrapFrame->Eip)) ||
@@ -1573,14 +1578,7 @@ KdbEnterDebuggerException(
             ULONG_PTR TrapCr2;
             ULONG Err;
 
-#ifdef __GNUC__
-            asm volatile("movl %%cr2, %0" : "=r"(TrapCr2));
-#elif _MSC_VER
-            __asm mov eax, cr2;
-            __asm mov TrapCr2, eax;
-#else
-#error Unknown compiler for inline assembler
-#endif
+            TrapCr2 = __readcr2();
 
             Err = TrapFrame->ErrCode;
             KdbpPrint("Memory at 0x%p could not be %s: ", TrapCr2, (Err & (1 << 1)) ? "written" : "read");
@@ -1611,13 +1609,18 @@ KdbEnterDebuggerException(
     KdbpTrapFrameToKdbTrapFrame(TrapFrame, &KdbTrapFrame);
 
     /* Enter critical section */
-    Ke386SaveFlags(OldEflags);
+    OldEflags = __readeflags();
     _disable();
 
+    /* HACK: Save the current IRQL and pretend we are at passive level,
+     * although interrupts are off. Needed because KDBG calls pageable code. */
+    OldIrql = KeGetCurrentIrql();
+    KeLowerIrql(PASSIVE_LEVEL);
+
     /* Exception inside the debugger? Game over. */
     if (InterlockedIncrement(&KdbEntryCount) > 1)
     {
-        Ke386RestoreFlags(OldEflags);
+        __writeeflags(OldEflags);
         return kdHandleException;
     }
 
@@ -1653,8 +1656,11 @@ KdbEnterDebuggerException(
     /* Decrement the entry count */
     InterlockedDecrement(&KdbEntryCount);
 
+    /* HACK: Raise back to old IRWL */
+    KeRaiseIrql(OldIrql, &OldIrql);
+
     /* Leave critical section */
-    Ke386RestoreFlags(OldEflags);
+    __writeeflags(OldEflags);
 
     /* Check if user requested a bugcheck */
     if (KdbpBugCheckRequested)
@@ -1677,22 +1683,16 @@ continue_execution:
         /* Clear dr6 status flags. */
         TrapFrame->Dr6 &= ~0x0000e00f;
 
-        /* Skip the current instruction */
-        Context->Eip++;
+        if (!KdbEnteredOnSingleStep && KdbSingleStepOver)
+        {
+            /* Skip the current instruction */
+            Context->Eip++;
+        }
     }
 
     return ContinueType;
 }
 
-VOID
-KdbDeleteProcessHook(
-    IN PEPROCESS Process)
-{
-    KdbSymFreeProcessSymbols(Process);
-
-    /* FIXME: Delete breakpoints for process */
-}
-
 VOID
 NTAPI
 KdbpGetCommandLineSettings(
@@ -1702,7 +1702,7 @@ KdbpGetCommandLineSettings(
 
     while (p1 && (p2 = strchr(p1, ' ')))
     {
-        p2++;
+        p2 += 2;
 
         if (!_strnicmp(p2, "KDSERIAL", 8))
         {
@@ -1715,6 +1715,11 @@ KdbpGetCommandLineSettings(
             p2 += 8;
             KdbDebugState |= KD_DEBUG_KDNOECHO;
         }
+        else if (!_strnicmp(p2, "FIRSTCHANCE", 11))
+        {
+            p2 += 11;
+            KdbpSetEnterCondition(-1, TRUE, KdbEnterAlways);
+        }
 
         p1 = p2;
     }