reactos/ntoskrnl/kd64/kdbreak.c

421 lines
14 KiB
C
Raw Normal View History

/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL - See COPYING in the top level directory
* FILE: ntoskrnl/kd64/kdbreak.c
* PURPOSE: KD64 Breakpoint Support
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
- Replace RtlpGetExceptionAddress by the _ReturnAddress intrinsic and add it to ARM intrin.h as it was missing. - Simplify RtlpCheckForActiveDebugger: Remove the BOOLEAN parameter as we would always pass it FALSE. Always return FALSE false from kernel mode for simplicity. - Fix a critical flaw in our exception support: RtlRaiseException and RtlRaiseStatus were implemented in C on x86. This lead to unpredictable register corruption because the compiler could not know that it had to preserve non-volatile registers before calling RtlCaptureContext as the saved context is later used to restore the caller in case the exception is handled and execution is continued. This made the functions unsafe to return from as any non-volatile register could be corrupted. Implement them in assembly for x86 to safely capture the context using only EBP and ESP. The C versions of those routines are still used and shared for the other architectures we support -- needs to be determined if this is safe and correct for those architectures. - The ntdll exception Wine exposed this issue, and all tests now pass. The remaining failures on the build server are caused by missing or incomplete debug register support in KVM/QEMU. Run the test in another VM or on real hardware and all the tests will pass. - Implement Debug Prompt (DbgPrompt) support for KD and KDBG. The KDBG implementation reads the prompt from keyboard or serial depending on the mode so that sysreg and rosdbg can support it too. - Properly implement RtlAssert using DbgPrompt to prompt for the action to take instead of always doing a breakpoint. The new implementation is disabled until sysreg can support this. Also move RtlAssert to its own file as it has nothing to do with the error routines (nor does it belong in exception.c). - Note that DbgPrompt was already used in PspCatchCriticalBreak, and this would have resulted in a silent hang as BREAKPOINT_PROMPT wasn't handled at all by KDBG. - Implement KiRaiseAssertion (10 lines of code with the trap macros) and thus support NT_ASSERT. Add partial support for it to KDBG to print out a warning and the address of the failure, but don't do anything else. Also add NT_ASSERT to the DDK headers so that we can use it, but don't use it yet as the ARM method of performing this has not been decided nor implemented. - KiTrap3 doesn't set STATUS_SUCCESS but BREAKPOINT_BREAK. They have the same numerical value but very different meaning -- BREAKPOINT_BREAK means that the exception is a software breakpoint and not a debug service call. Fix some comments to document that this is what is checked for. - Fix inverted and broken logic in KdpReport. It would never pass second chance exceptions to the debugger, didn't respect the stop-on-exception flag properly and would always fail to handle some special exceptions in both first and second chance instead of just failing to handle it in first chance. Clean up, reformat and document what is going on. - The DebugPrint and DebugPrompt support routines only perform a 2D interrupt on x86; use more portable comments. - Add Alex to the programmer section of x86's kdsup.c -- he wrote KdpGetStateChange, KdpSetContextState and the code that was previously in KdpRead/WriteControlSpace. - Add my name to the parts of KD where I have made significant work on getting KD/WinDbg support up and running. - KD debugging is now quite functional and stable. Some bugs and stubs remain to be flushed out, but overall KD is now much better and easier to use than KDBG. svn path=/trunk/; revision=43705
2009-10-23 22:51:39 +00:00
* Stefan Ginsberg (stefan.ginsberg@reactos.org)
*/
/* INCLUDES ******************************************************************/
#include <ntoskrnl.h>
#define NDEBUG
#include <debug.h>
/* FUNCTIONS *****************************************************************/
ULONG
NTAPI
KdpAddBreakpoint(IN PVOID Address)
{
KD_BREAKPOINT_TYPE Content;
ULONG i;
NTSTATUS Status;
/* Check whether we are not setting a breakpoint twice */
for (i = 0; i < KD_BREAKPOINT_MAX; i++)
{
/* Check if the breakpoint is valid */
if ((KdpBreakpointTable[i].Flags & KD_BREAKPOINT_ACTIVE) &&
(KdpBreakpointTable[i].Address == Address))
{
/* Were we not able to remove it earlier? */
if (KdpBreakpointTable[i].Flags & KD_BREAKPOINT_EXPIRED)
{
/* Just re-use it! */
KdpBreakpointTable[i].Flags &= ~KD_BREAKPOINT_EXPIRED;
return i + 1;
}
else
{
/* Fail */
return 0;
}
}
}
/* Find a free entry */
for (i = 0; i < KD_BREAKPOINT_MAX; i++)
{
if (KdpBreakpointTable[i].Flags == 0)
break;
}
/* Fail if no free entry was found */
if (i == KD_BREAKPOINT_MAX) return 0;
/* Save the breakpoint */
KdpBreakpointTable[i].Address = Address;
/* If we are setting the breakpoint in user space, save the active process context */
if (Address < KD_HIGHEST_USER_BREAKPOINT_ADDRESS)
KdpBreakpointTable[i].DirectoryTableBase = KeGetCurrentThread()->ApcState.Process->DirectoryTableBase[0];
/* Try to save the old instruction */
Status = KdpCopyMemoryChunks((ULONG_PTR)Address,
&Content,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE,
NULL);
if (NT_SUCCESS(Status))
{
/* Memory accessible, set the breakpoint */
KdpBreakpointTable[i].Content = Content;
KdpBreakpointTable[i].Flags = KD_BREAKPOINT_ACTIVE;
/* Write the breakpoint */
Status = KdpCopyMemoryChunks((ULONG_PTR)Address,
&KdpBreakpointInstruction,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
NULL);
if (!NT_SUCCESS(Status))
{
/* This should never happen */
KdpDprintf("Unable to write breakpoint to address 0x%p\n", Address);
}
}
else
{
/* Memory is inaccessible now, setting breakpoint is deferred */
KdpDprintf("Failed to set breakpoint at address 0x%p, adding deferred breakpoint.\n", Address);
KdpBreakpointTable[i].Flags = KD_BREAKPOINT_ACTIVE | KD_BREAKPOINT_PENDING;
KdpOweBreakpoint = TRUE;
}
/* Return the breakpoint handle */
return i + 1;
}
VOID
NTAPI
KdSetOwedBreakpoints(VOID)
{
BOOLEAN Enable;
KD_BREAKPOINT_TYPE Content;
ULONG i;
NTSTATUS Status;
/* If we don't owe any breakpoints, just return */
if (!KdpOweBreakpoint) return;
/* Enter the debugger */
Enable = KdEnterDebugger(NULL, NULL);
/*
* Suppose we succeed in setting all the breakpoints.
* If we fail to do so, the flag will be set again.
*/
KdpOweBreakpoint = FALSE;
/* Loop through current breakpoints and try to set or delete the pending ones */
for (i = 0; i < KD_BREAKPOINT_MAX; i++)
{
if (KdpBreakpointTable[i].Flags & (KD_BREAKPOINT_PENDING | KD_BREAKPOINT_EXPIRED))
{
/*
* Set the breakpoint only if it is in kernel space, or if it is
* in user space and the active process context matches.
*/
if (KdpBreakpointTable[i].Address < KD_HIGHEST_USER_BREAKPOINT_ADDRESS &&
KdpBreakpointTable[i].DirectoryTableBase != KeGetCurrentThread()->ApcState.Process->DirectoryTableBase[0])
{
KdpOweBreakpoint = TRUE;
continue;
}
/* Try to save the old instruction */
Status = KdpCopyMemoryChunks((ULONG_PTR)KdpBreakpointTable[i].Address,
&Content,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE,
NULL);
if (!NT_SUCCESS(Status))
{
/* Memory is still inaccessible, breakpoint setting will be deferred again */
// KdpDprintf("Failed to set deferred breakpoint at address 0x%p\n",
// KdpBreakpointTable[i].Address);
KdpOweBreakpoint = TRUE;
continue;
}
/* Check if we need to write the breakpoint */
if (KdpBreakpointTable[i].Flags & KD_BREAKPOINT_PENDING)
{
/* Memory accessible, set the breakpoint */
KdpBreakpointTable[i].Content = Content;
/* Write the breakpoint */
Status = KdpCopyMemoryChunks((ULONG_PTR)KdpBreakpointTable[i].Address,
&KdpBreakpointInstruction,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
NULL);
if (!NT_SUCCESS(Status))
{
/* This should never happen */
KdpDprintf("Unable to write deferred breakpoint to address 0x%p\n",
KdpBreakpointTable[i].Address);
KdpOweBreakpoint = TRUE;
}
else
{
KdpBreakpointTable[i].Flags = KD_BREAKPOINT_ACTIVE;
}
continue;
}
/* Check if we need to restore the original instruction */
if (KdpBreakpointTable[i].Flags & KD_BREAKPOINT_EXPIRED)
{
/* Write it back */
Status = KdpCopyMemoryChunks((ULONG_PTR)KdpBreakpointTable[i].Address,
&KdpBreakpointTable[i].Content,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
NULL);
if (!NT_SUCCESS(Status))
{
/* This should never happen */
KdpDprintf("Unable to delete deferred breakpoint at address 0x%p\n",
KdpBreakpointTable[i].Address);
KdpOweBreakpoint = TRUE;
}
else
{
/* Check if the breakpoint is suspended */
if (KdpBreakpointTable[i].Flags & KD_BREAKPOINT_SUSPENDED)
{
KdpBreakpointTable[i].Flags = KD_BREAKPOINT_SUSPENDED | KD_BREAKPOINT_ACTIVE;
}
else
{
/* Invalidate it */
KdpBreakpointTable[i].Flags = 0;
}
}
continue;
}
}
}
/* Exit the debugger */
KdExitDebugger(Enable);
}
BOOLEAN
NTAPI
KdpLowWriteContent(IN ULONG BpIndex)
{
NTSTATUS Status;
/* Make sure that the breakpoint is actually active */
if (KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_PENDING)
{
/* So we have a valid breakpoint, but it hasn't been used yet... */
KdpBreakpointTable[BpIndex].Flags &= ~KD_BREAKPOINT_PENDING;
return TRUE;
}
/* Is the original instruction a breakpoint anyway? */
if (KdpBreakpointTable[BpIndex].Content == KdpBreakpointInstruction)
{
/* Then leave it that way... */
return TRUE;
}
/* We have an active breakpoint with an instruction to bring back. Do it. */
Status = KdpCopyMemoryChunks((ULONG_PTR)KdpBreakpointTable[BpIndex].Address,
&KdpBreakpointTable[BpIndex].Content,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
NULL);
if (!NT_SUCCESS(Status))
{
/* Memory is inaccessible now, restoring original instruction is deferred */
// KdpDprintf("Failed to delete breakpoint at address 0x%p\n",
// KdpBreakpointTable[BpIndex].Address);
KdpBreakpointTable[BpIndex].Flags |= KD_BREAKPOINT_EXPIRED;
KdpOweBreakpoint = TRUE;
return FALSE;
}
/* Everything went fine, return */
return TRUE;
}
BOOLEAN
NTAPI
KdpLowRestoreBreakpoint(IN ULONG BpIndex)
{
NTSTATUS Status;
/* Were we not able to remove it earlier? */
if (KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_EXPIRED)
{
/* Just re-use it! */
KdpBreakpointTable[BpIndex].Flags &= ~KD_BREAKPOINT_EXPIRED;
return TRUE;
}
/* Are we merely writing a breakpoint on top of another breakpoint? */
if (KdpBreakpointTable[BpIndex].Content == KdpBreakpointInstruction)
{
/* Nothing to do */
return TRUE;
}
/* Ok, we actually have to overwrite the instruction now */
Status = KdpCopyMemoryChunks((ULONG_PTR)KdpBreakpointTable[BpIndex].Address,
&KdpBreakpointInstruction,
KD_BREAKPOINT_SIZE,
0,
MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
NULL);
if (!NT_SUCCESS(Status))
{
/* Memory is inaccessible now, restoring breakpoint is deferred */
// KdpDprintf("Failed to restore breakpoint at address 0x%p\n",
// KdpBreakpointTable[BpIndex].Address);
KdpBreakpointTable[BpIndex].Flags |= KD_BREAKPOINT_PENDING;
KdpOweBreakpoint = TRUE;
return FALSE;
}
/* Clear any possible previous pending flag and return success */
KdpBreakpointTable[BpIndex].Flags &= ~KD_BREAKPOINT_PENDING;
return TRUE;
}
BOOLEAN
NTAPI
KdpDeleteBreakpoint(IN ULONG BpEntry)
{
ULONG BpIndex = BpEntry - 1;
/* Check for invalid breakpoint entry */
if (!BpEntry || (BpEntry > KD_BREAKPOINT_MAX)) return FALSE;
/* If the specified breakpoint table entry is not valid, then return FALSE. */
if (!KdpBreakpointTable[BpIndex].Flags) return FALSE;
/* Check if the breakpoint is suspended */
if (KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_SUSPENDED)
{
/* Check if breakpoint is not being deleted */
if (!(KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_EXPIRED))
{
/* Invalidate it and return success */
KdpBreakpointTable[BpIndex].Flags = 0;
return TRUE;
}
}
/* Restore original data, then invalidate it and return success */
if (KdpLowWriteContent(BpIndex)) KdpBreakpointTable[BpIndex].Flags = 0;
return TRUE;
}
BOOLEAN
NTAPI
KdpDeleteBreakpointRange(IN PVOID Base,
IN PVOID Limit)
{
ULONG BpIndex;
BOOLEAN DeletedBreakpoints;
/* Assume no breakpoints will be deleted */
DeletedBreakpoints = FALSE;
/* Loop the breakpoint table */
for (BpIndex = 0; BpIndex < KD_BREAKPOINT_MAX; BpIndex++)
{
/* Make sure that the breakpoint is active and matches the range. */
if ((KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_ACTIVE) &&
((KdpBreakpointTable[BpIndex].Address >= Base) &&
(KdpBreakpointTable[BpIndex].Address <= Limit)))
{
/* Delete it, and remember if we succeeded at least once */
if (KdpDeleteBreakpoint(BpIndex + 1)) DeletedBreakpoints = TRUE;
}
}
/* Return whether we deleted anything */
return DeletedBreakpoints;
}
VOID
NTAPI
KdpRestoreAllBreakpoints(VOID)
{
ULONG BpIndex;
/* No more suspended Breakpoints */
BreakpointsSuspended = FALSE;
/* Loop the breakpoints */
for (BpIndex = 0; BpIndex < KD_BREAKPOINT_MAX; BpIndex++)
{
/* Check if they are valid, suspended breakpoints */
if ((KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_ACTIVE) &&
(KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_SUSPENDED))
{
/* Unsuspend them */
KdpBreakpointTable[BpIndex].Flags &= ~KD_BREAKPOINT_SUSPENDED;
KdpLowRestoreBreakpoint(BpIndex);
}
}
}
VOID
NTAPI
KdpSuspendBreakPoint(IN ULONG BpEntry)
{
ULONG BpIndex = BpEntry - 1;
/* Check if this is a valid, unsuspended breakpoint */
if ((KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_ACTIVE) &&
!(KdpBreakpointTable[BpIndex].Flags & KD_BREAKPOINT_SUSPENDED))
{
/* Suspend it */
KdpBreakpointTable[BpIndex].Flags |= KD_BREAKPOINT_SUSPENDED;
KdpLowWriteContent(BpIndex);
}
}
VOID
NTAPI
KdpSuspendAllBreakPoints(VOID)
{
ULONG BpEntry;
/* Breakpoints are suspended */
BreakpointsSuspended = TRUE;
/* Loop every breakpoint */
for (BpEntry = 1; BpEntry <= KD_BREAKPOINT_MAX; BpEntry++)
{
/* Suspend it */
KdpSuspendBreakPoint(BpEntry);
}
}