2005-04-25 14:44:48 +00:00
|
|
|
/*
|
2005-01-26 13:58:37 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
2001-04-10 17:48:17 +00:00
|
|
|
* PROJECT: ReactOS kernel
|
2005-04-25 14:44:48 +00:00
|
|
|
* FILE: ntoskrnl/kdbg/kdb.c
|
|
|
|
* PURPOSE: Kernel Debugger
|
2005-05-09 01:38:29 +00:00
|
|
|
*
|
2005-04-25 14:44:48 +00:00
|
|
|
* PROGRAMMERS: Gregor Anich
|
2001-04-10 17:48:17 +00:00
|
|
|
*/
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2001-04-10 17:48:17 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2023-04-04 21:38:32 +00:00
|
|
|
#include "kdb.h"
|
|
|
|
|
2001-04-12 00:56:04 +00:00
|
|
|
/* TYPES *********************************************************************/
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/* DEFINES *******************************************************************/
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
#define KDB_STACK_SIZE (4096*3)
|
2020-01-26 17:59:05 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
#define KDB_STACK_ALIGN 16
|
|
|
|
#define KDB_STACK_RESERVE (5 * sizeof(PVOID)) /* Home space + return address */
|
|
|
|
#else
|
|
|
|
#define KDB_STACK_ALIGN 4
|
|
|
|
#define KDB_STACK_RESERVE sizeof(ULONG) /* Return address */
|
|
|
|
#endif
|
2005-03-05 23:35:08 +00:00
|
|
|
#define KDB_MAXIMUM_BREAKPOINT_COUNT 256
|
|
|
|
#define KDB_MAXIMUM_HW_BREAKPOINT_COUNT 4
|
|
|
|
#define KDB_MAXIMUM_SW_BREAKPOINT_COUNT 256
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
#define __STRING(x) #x
|
|
|
|
#define _STRING(x) __STRING(x)
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/* GLOBALS *******************************************************************/
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-06-13 15:28:33 +00:00
|
|
|
static LONG KdbEntryCount = 0;
|
2020-01-26 17:59:05 +00:00
|
|
|
static DECLSPEC_ALIGN(KDB_STACK_ALIGN) CHAR KdbStack[KDB_STACK_SIZE];
|
2009-06-13 15:28:33 +00:00
|
|
|
|
|
|
|
static ULONG KdbBreakPointCount = 0; /* Number of used breakpoints in the array */
|
|
|
|
static KDB_BREAKPOINT KdbBreakPoints[KDB_MAXIMUM_BREAKPOINT_COUNT] = {{0}}; /* Breakpoint array */
|
|
|
|
static ULONG KdbSwBreakPointCount = 0; /* Number of enabled software breakpoints */
|
|
|
|
static ULONG KdbHwBreakPointCount = 0; /* Number of enabled hardware breakpoints */
|
|
|
|
static PKDB_BREAKPOINT KdbSwBreakPoints[KDB_MAXIMUM_SW_BREAKPOINT_COUNT]; /* Enabled software breakpoints, orderless */
|
|
|
|
static PKDB_BREAKPOINT KdbHwBreakPoints[KDB_MAXIMUM_HW_BREAKPOINT_COUNT]; /* Enabled hardware breakpoints, orderless */
|
|
|
|
static PKDB_BREAKPOINT KdbBreakPointToReenable = NULL; /* Set to a breakpoint struct when single stepping after
|
2005-03-05 23:35:08 +00:00
|
|
|
a software breakpoint was hit, to reenable it */
|
2015-12-23 22:38:46 +00:00
|
|
|
static BOOLEAN KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep;
|
2005-03-05 23:35:08 +00:00
|
|
|
LONG KdbLastBreakPointNr = -1; /* Index of the breakpoint which cause KDB to be entered */
|
|
|
|
ULONG KdbNumSingleSteps = 0; /* How many single steps to do */
|
|
|
|
BOOLEAN KdbSingleStepOver = FALSE; /* Whether to step over calls/reps. */
|
2009-06-13 15:28:33 +00:00
|
|
|
static BOOLEAN KdbEnteredOnSingleStep = FALSE; /* Set to true when KDB was entered because of single step */
|
2005-03-05 23:35:08 +00:00
|
|
|
PEPROCESS KdbCurrentProcess = NULL; /* The current process context in which KDB runs */
|
|
|
|
PEPROCESS KdbOriginalProcess = NULL; /* The process in whichs context KDB was intered */
|
|
|
|
PETHREAD KdbCurrentThread = NULL; /* The current thread context in which KDB runs */
|
|
|
|
PETHREAD KdbOriginalThread = NULL; /* The thread in whichs context KDB was entered */
|
|
|
|
PKDB_KTRAP_FRAME KdbCurrentTrapFrame = NULL; /* Pointer to the current trapframe */
|
2020-04-08 21:08:57 +00:00
|
|
|
static KDB_KTRAP_FRAME KdbTrapFrame = { 0 }; /* The trapframe which was passed to KdbEnterDebuggerException */
|
|
|
|
static KDB_KTRAP_FRAME KdbThreadTrapFrame = { 0 }; /* The trapframe of the current thread (KdbCurrentThread) */
|
2009-06-13 15:28:33 +00:00
|
|
|
static KAPC_STATE KdbApcState;
|
2008-11-06 18:33:56 +00:00
|
|
|
extern BOOLEAN KdbpBugCheckRequested;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
|
|
|
/* Array of conditions when to enter KDB */
|
2009-06-13 15:28:33 +00:00
|
|
|
static KDB_ENTER_CONDITION KdbEnterConditions[][2] =
|
2001-04-12 00:56:04 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
/* First chance Last chance */
|
2014-10-16 21:33:32 +00:00
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 0: Zero divide */
|
|
|
|
{ KdbEnterFromKmode, KdbDoNotEnter }, /* 1: Debug trap */
|
|
|
|
{ KdbDoNotEnter, KdbEnterAlways }, /* 2: NMI */
|
|
|
|
{ KdbEnterFromKmode, KdbDoNotEnter }, /* 3: INT3 */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 4: Overflow */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 5: BOUND range exceeded */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 6: Invalid opcode */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 7: No math coprocessor fault */
|
|
|
|
{ KdbEnterAlways, KdbEnterAlways }, /* 8: Double Fault */
|
|
|
|
{ KdbEnterAlways, KdbEnterAlways }, /* 9: Unknown(9) */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 10: Invalid TSS */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 11: Segment Not Present */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 12: Stack fault */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 13: General protection fault */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 14: Page fault */
|
|
|
|
{ KdbEnterAlways, KdbEnterAlways }, /* 15: Reserved (15) */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 16: FPU fault */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 17: Alignment Check */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 18: Machine Check */
|
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode }, /* 19: SIMD fault */
|
2015-09-12 21:44:48 +00:00
|
|
|
{ KdbEnterFromKmode, KdbDoNotEnter }, /* 20: Assertion failure */
|
2009-09-09 13:49:04 +00:00
|
|
|
{ KdbDoNotEnter, KdbEnterFromKmode } /* Last entry: used for unknown exceptions */
|
2001-04-12 00:56:04 +00:00
|
|
|
};
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/* Exception descriptions */
|
2009-06-13 15:28:33 +00:00
|
|
|
static const CHAR *ExceptionNrToString[] =
|
2005-03-05 23:35:08 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
"Divide Error",
|
|
|
|
"Debug Trap",
|
|
|
|
"NMI",
|
|
|
|
"Breakpoint",
|
|
|
|
"Overflow",
|
|
|
|
"BOUND range exceeded",
|
|
|
|
"Invalid Opcode",
|
|
|
|
"No Math Coprocessor",
|
|
|
|
"Double Fault",
|
|
|
|
"Unknown(9)",
|
|
|
|
"Invalid TSS",
|
|
|
|
"Segment Not Present",
|
|
|
|
"Stack Segment Fault",
|
|
|
|
"General Protection",
|
|
|
|
"Page Fault",
|
|
|
|
"Reserved(15)",
|
|
|
|
"Math Fault",
|
|
|
|
"Alignment Check",
|
|
|
|
"Machine Check",
|
2014-10-16 21:33:32 +00:00
|
|
|
"SIMD Fault",
|
|
|
|
"Assertion Failure"
|
2005-03-05 23:35:08 +00:00
|
|
|
};
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2001-04-10 17:48:17 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2009-06-13 15:28:33 +00:00
|
|
|
static VOID
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpKdbTrapFrameFromKernelStack(
|
|
|
|
PVOID KernelStack,
|
|
|
|
PKDB_KTRAP_FRAME KdbTrapFrame)
|
2007-06-30 18:58:09 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONG_PTR *StackPtr;
|
|
|
|
|
|
|
|
RtlZeroMemory(KdbTrapFrame, sizeof(KDB_KTRAP_FRAME));
|
|
|
|
StackPtr = (ULONG_PTR *) KernelStack;
|
2010-03-02 19:04:15 +00:00
|
|
|
#ifdef _M_IX86
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame->Ebp = StackPtr[3];
|
|
|
|
KdbTrapFrame->Edi = StackPtr[4];
|
|
|
|
KdbTrapFrame->Esi = StackPtr[5];
|
|
|
|
KdbTrapFrame->Ebx = StackPtr[6];
|
|
|
|
KdbTrapFrame->Eip = StackPtr[7];
|
|
|
|
KdbTrapFrame->Esp = (ULONG) (StackPtr + 8);
|
|
|
|
KdbTrapFrame->SegSs = KGDT_R0_DATA;
|
|
|
|
KdbTrapFrame->SegCs = KGDT_R0_CODE;
|
|
|
|
KdbTrapFrame->SegDs = KGDT_R0_DATA;
|
|
|
|
KdbTrapFrame->SegEs = KGDT_R0_DATA;
|
|
|
|
KdbTrapFrame->SegGs = KGDT_R0_DATA;
|
2010-01-13 22:35:43 +00:00
|
|
|
#endif
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* FIXME: what about the other registers??? */
|
2007-06-30 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
|
|
|
|
* the old instruction in *OldInst.
|
|
|
|
*
|
|
|
|
* \param Process Process in which's context to overwrite the instruction.
|
|
|
|
* \param Address Address at which to overwrite the instruction.
|
|
|
|
* \param NewInst New instruction (written to \a Address)
|
|
|
|
* \param OldInst Old instruction (read from \a Address)
|
2001-04-16 02:02:07 +00:00
|
|
|
*
|
2005-03-05 23:35:08 +00:00
|
|
|
* \returns NTSTATUS
|
2001-04-16 02:02:07 +00:00
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static NTSTATUS
|
2005-03-05 23:35:08 +00:00
|
|
|
KdbpOverwriteInstruction(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN ULONG_PTR Address,
|
|
|
|
IN UCHAR NewInst,
|
|
|
|
OUT PUCHAR OldInst OPTIONAL)
|
2001-04-16 02:02:07 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG Protect;
|
|
|
|
PEPROCESS CurrentProcess = PsGetCurrentProcess();
|
|
|
|
KAPC_STATE ApcState;
|
2001-04-16 02:02:07 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Get the protection for the address. */
|
|
|
|
Protect = MmGetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address));
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Return if that page isn't present. */
|
|
|
|
if (Protect & PAGE_NOACCESS)
|
|
|
|
{
|
|
|
|
return STATUS_MEMORY_NOT_ALLOCATED;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Attach to the process */
|
|
|
|
if (CurrentProcess != Process)
|
|
|
|
{
|
|
|
|
KeStackAttachProcess(&Process->Pcb, &ApcState);
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Make the page writeable if it is read only. */
|
|
|
|
if (Protect & (PAGE_READONLY|PAGE_EXECUTE|PAGE_EXECUTE_READ))
|
|
|
|
{
|
|
|
|
MmSetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address),
|
|
|
|
(Protect & ~(PAGE_READONLY|PAGE_EXECUTE|PAGE_EXECUTE_READ)) | PAGE_READWRITE);
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Copy the old instruction back to the caller. */
|
|
|
|
if (OldInst)
|
|
|
|
{
|
|
|
|
Status = KdbpSafeReadMemory(OldInst, (PUCHAR)Address, 1);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
if (Protect & (PAGE_READONLY|PAGE_EXECUTE|PAGE_EXECUTE_READ))
|
|
|
|
{
|
|
|
|
MmSetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address), Protect);
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Detach from process */
|
|
|
|
if (CurrentProcess != Process)
|
|
|
|
{
|
2023-04-06 01:55:20 +00:00
|
|
|
KeUnstackDetachProcess(&ApcState);
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Copy the new instruction in its place. */
|
|
|
|
Status = KdbpSafeWriteMemory((PUCHAR)Address, &NewInst, 1);
|
2001-04-16 02:02:07 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Restore the page protection. */
|
|
|
|
if (Protect & (PAGE_READONLY|PAGE_EXECUTE|PAGE_EXECUTE_READ))
|
|
|
|
{
|
|
|
|
MmSetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address), Protect);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach from process */
|
|
|
|
if (CurrentProcess != Process)
|
|
|
|
{
|
|
|
|
KeUnstackDetachProcess(&ApcState);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
2001-04-16 02:02:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Checks whether the given instruction can be single stepped or has to be
|
|
|
|
* stepped over using a temporary breakpoint.
|
|
|
|
*
|
|
|
|
* \retval TRUE Instruction is a call.
|
|
|
|
* \retval FALSE Instruction is not a call.
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpShouldStepOverInstruction(
|
|
|
|
ULONG_PTR Eip)
|
2004-10-27 13:33:59 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
UCHAR Mem[3];
|
|
|
|
ULONG i = 0;
|
2004-10-27 13:33:59 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!NT_SUCCESS(KdbpSafeReadMemory(Mem, (PVOID)Eip, sizeof (Mem))))
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Couldn't access memory at 0x%p\n", Eip);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-10-27 13:33:59 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Check if the current instruction is a call. */
|
|
|
|
while ((i < sizeof (Mem)) && (Mem[i] == 0x66 || Mem[i] == 0x67))
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i == sizeof (Mem))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Mem[i] == 0xE8 || Mem[i] == 0x9A || Mem[i] == 0xF2 || Mem[i] == 0xF3 ||
|
|
|
|
(((i + 1) < sizeof (Mem)) && Mem[i] == 0xFF && (Mem[i+1] & 0x38) == 0x10))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2005-03-05 23:35:08 +00:00
|
|
|
}
|
2004-10-27 13:33:59 +00:00
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Steps over an instruction
|
|
|
|
*
|
|
|
|
* If the given instruction should be stepped over, this function inserts a
|
|
|
|
* temporary breakpoint after the instruction and returns TRUE, otherwise it
|
|
|
|
* returns FALSE.
|
|
|
|
*
|
|
|
|
* \retval TRUE Temporary breakpoint set after instruction.
|
|
|
|
* \retval FALSE No breakpoint was set.
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpStepOverInstruction(
|
|
|
|
ULONG_PTR Eip)
|
2004-10-27 13:33:59 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
LONG InstLen;
|
2004-10-27 13:33:59 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!KdbpShouldStepOverInstruction(Eip))
|
|
|
|
return FALSE;
|
2004-10-27 13:33:59 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
InstLen = KdbpGetInstLength(Eip);
|
|
|
|
if (InstLen < 1)
|
|
|
|
return FALSE;
|
2004-10-27 13:33:59 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip + InstLen, KdbBreakPointTemporary, 0, 0, NULL, FALSE, NULL)))
|
|
|
|
return FALSE;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
2001-04-10 17:48:17 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Steps into an instruction (interrupts)
|
|
|
|
*
|
|
|
|
* If the given instruction should be stepped into, this function inserts a
|
|
|
|
* temporary breakpoint at the target instruction and returns TRUE, otherwise it
|
|
|
|
* returns FALSE.
|
|
|
|
*
|
|
|
|
* \retval TRUE Temporary breakpoint set at target instruction.
|
|
|
|
* \retval FALSE No breakpoint was set.
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpStepIntoInstruction(
|
|
|
|
ULONG_PTR Eip)
|
2004-03-13 18:21:57 +00:00
|
|
|
{
|
2007-11-30 21:04:12 +00:00
|
|
|
KDESCRIPTOR Idtr = {0};
|
2009-09-09 13:49:04 +00:00
|
|
|
UCHAR Mem[2];
|
|
|
|
INT IntVect;
|
|
|
|
ULONG IntDesc[2];
|
|
|
|
ULONG_PTR TargetEip;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Read memory */
|
|
|
|
if (!NT_SUCCESS(KdbpSafeReadMemory(Mem, (PVOID)Eip, sizeof (Mem))))
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
// KdbPrintf("Couldn't access memory at 0x%p\n", Eip);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Check for INT instruction */
|
|
|
|
/* FIXME: Check for iret */
|
|
|
|
if (Mem[0] == 0xcc)
|
|
|
|
IntVect = 3;
|
|
|
|
else if (Mem[0] == 0xcd)
|
|
|
|
IntVect = Mem[1];
|
2020-04-08 21:08:57 +00:00
|
|
|
else if (Mem[0] == 0xce && KdbCurrentTrapFrame->EFlags & (1<<11)) /* 1 << 11 is the overflow flag */
|
2009-09-09 13:49:04 +00:00
|
|
|
IntVect = 4;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (IntVect < 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Read the interrupt descriptor table register */
|
2009-09-21 15:20:18 +00:00
|
|
|
__sidt(&Idtr.Limit);
|
2009-09-09 13:49:04 +00:00
|
|
|
if (IntVect >= (Idtr.Limit + 1) / 8)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
// KdbPrintf("IDT does not contain interrupt vector %d.\n", IntVect);
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Get the interrupt descriptor */
|
2020-01-26 17:59:05 +00:00
|
|
|
if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc, (PVOID)((ULONG_PTR)Idtr.Base + (IntVect * 8)), sizeof(IntDesc))))
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
// KdbPrintf("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
|
|
|
|
if ((IntDesc[1] & (1 << 15)) == 0) /* not present */
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((IntDesc[1] & 0x1f00) == 0x0500) /* Task gate */
|
|
|
|
{
|
|
|
|
/* FIXME: Task gates not supported */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (((IntDesc[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
|
|
|
|
((IntDesc[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
|
|
|
|
{
|
|
|
|
/* FIXME: Should the segment selector of the interrupt gate be checked? */
|
|
|
|
TargetEip = (IntDesc[1] & 0xffff0000) | (IntDesc[0] & 0x0000ffff);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Insert breakpoint */
|
|
|
|
if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip, KdbBreakPointTemporary, 0, 0, NULL, FALSE, NULL)))
|
|
|
|
return FALSE;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
2004-03-13 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Gets the number of the next breakpoint >= Start.
|
|
|
|
*
|
|
|
|
* \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
|
|
|
|
*
|
|
|
|
* \returns Breakpoint number (-1 if no more breakpoints are found)
|
|
|
|
*/
|
|
|
|
LONG
|
|
|
|
KdbpGetNextBreakPointNr(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN ULONG Start OPTIONAL)
|
2004-03-13 18:21:57 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
for (; Start < RTL_NUMBER_OF(KdbBreakPoints); Start++)
|
|
|
|
{
|
|
|
|
if (KdbBreakPoints[Start].Type != KdbBreakPointNone)
|
|
|
|
return Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2004-03-13 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Returns information of the specified breakpoint.
|
|
|
|
*
|
|
|
|
* \param BreakPointNr Number of the breakpoint to return information of.
|
|
|
|
* \param Address Receives the address of the breakpoint.
|
|
|
|
* \param Type Receives the type of the breakpoint (hardware or software)
|
|
|
|
* \param Size Size - for memory breakpoints.
|
|
|
|
* \param AccessType Access type - for hardware breakpoints.
|
|
|
|
* \param DebugReg Debug register - for enabled hardware breakpoints.
|
|
|
|
* \param Enabled Whether the breakpoint is enabled or not.
|
|
|
|
* \param Process The owning process of the breakpoint.
|
|
|
|
* \param ConditionExpression The expression which was given as condition for the bp.
|
|
|
|
*
|
|
|
|
* \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpGetBreakPointInfo(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN ULONG BreakPointNr,
|
|
|
|
OUT ULONG_PTR *Address OPTIONAL,
|
|
|
|
OUT KDB_BREAKPOINT_TYPE *Type OPTIONAL,
|
|
|
|
OUT UCHAR *Size OPTIONAL,
|
|
|
|
OUT KDB_ACCESS_TYPE *AccessType OPTIONAL,
|
|
|
|
OUT UCHAR *DebugReg OPTIONAL,
|
|
|
|
OUT BOOLEAN *Enabled OPTIONAL,
|
|
|
|
OUT BOOLEAN *Global OPTIONAL,
|
|
|
|
OUT PEPROCESS *Process OPTIONAL,
|
|
|
|
OUT PCHAR *ConditionExpression OPTIONAL)
|
2004-03-13 18:21:57 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PKDB_BREAKPOINT bp;
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPointNr >= RTL_NUMBER_OF(KdbBreakPoints) ||
|
|
|
|
KdbBreakPoints[BreakPointNr].Type == KdbBreakPointNone)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
bp = KdbBreakPoints + BreakPointNr;
|
|
|
|
if (Address)
|
|
|
|
*Address = bp->Address;
|
|
|
|
|
|
|
|
if (Type)
|
|
|
|
*Type = bp->Type;
|
|
|
|
|
|
|
|
if (bp->Type == KdbBreakPointHardware)
|
|
|
|
{
|
|
|
|
if (Size)
|
|
|
|
*Size = bp->Data.Hw.Size;
|
|
|
|
|
|
|
|
if (AccessType)
|
|
|
|
*AccessType = bp->Data.Hw.AccessType;
|
|
|
|
|
|
|
|
if (DebugReg && bp->Enabled)
|
|
|
|
*DebugReg = bp->Data.Hw.DebugReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Enabled)
|
|
|
|
*Enabled = bp->Enabled;
|
|
|
|
|
|
|
|
if (Global)
|
|
|
|
*Global = bp->Global;
|
|
|
|
|
|
|
|
if (Process)
|
|
|
|
*Process = bp->Process;
|
|
|
|
|
|
|
|
if (ConditionExpression)
|
|
|
|
*ConditionExpression = bp->ConditionExpression;
|
|
|
|
|
|
|
|
return TRUE;
|
2004-03-13 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Inserts a breakpoint into the breakpoint array.
|
|
|
|
*
|
|
|
|
* The \a Process of the breakpoint is set to \a KdbCurrentProcess
|
|
|
|
*
|
|
|
|
* \param Address Address at which to set the breakpoint.
|
|
|
|
* \param Type Type of breakpoint (hardware or software)
|
|
|
|
* \param Size Size of breakpoint (for hardware/memory breakpoints)
|
|
|
|
* \param AccessType Access type (for hardware breakpoins)
|
|
|
|
* \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
|
|
|
|
* \param Global Wether the breakpoint is global or local to a process.
|
|
|
|
* \param BreakPointNumber Receives the breakpoint number on success
|
|
|
|
*
|
|
|
|
* \returns NTSTATUS
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
KdbpInsertBreakPoint(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN ULONG_PTR Address,
|
|
|
|
IN KDB_BREAKPOINT_TYPE Type,
|
|
|
|
IN UCHAR Size OPTIONAL,
|
|
|
|
IN KDB_ACCESS_TYPE AccessType OPTIONAL,
|
|
|
|
IN PCHAR ConditionExpression OPTIONAL,
|
|
|
|
IN BOOLEAN Global,
|
|
|
|
OUT PLONG BreakPointNr OPTIONAL)
|
2004-03-13 18:21:57 +00:00
|
|
|
{
|
2020-01-26 17:59:05 +00:00
|
|
|
LONG_PTR i;
|
2009-09-09 13:49:04 +00:00
|
|
|
PVOID Condition;
|
|
|
|
PCHAR ConditionExpressionDup;
|
|
|
|
LONG ErrOffset;
|
|
|
|
CHAR ErrMsg[128];
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Type != KdbBreakPointNone);
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (Type == KdbBreakPointHardware)
|
|
|
|
{
|
|
|
|
if ((Address % Size) != 0)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address, Size);
|
2009-09-09 13:49:04 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (AccessType == KdbAccessExec && Size != 1)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPuts("Size must be 1 for execution breakpoints.\n");
|
2009-09-09 13:49:04 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (KdbBreakPointCount == KDB_MAXIMUM_BREAKPOINT_COUNT)
|
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2022-11-28 01:48:55 +00:00
|
|
|
/* Parse condition expression string and duplicate it */
|
2009-09-09 13:49:04 +00:00
|
|
|
if (ConditionExpression)
|
|
|
|
{
|
|
|
|
Condition = KdbpRpnParseExpression(ConditionExpression, &ErrOffset, ErrMsg);
|
|
|
|
if (!Condition)
|
|
|
|
{
|
|
|
|
if (ErrOffset >= 0)
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Couldn't parse expression: %s at character %d\n", ErrMsg, ErrOffset);
|
2009-09-09 13:49:04 +00:00
|
|
|
else
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Couldn't parse expression: %s", ErrMsg);
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
i = strlen(ConditionExpression) + 1;
|
|
|
|
ConditionExpressionDup = ExAllocatePoolWithTag(NonPagedPool, i, TAG_KDBG);
|
|
|
|
RtlCopyMemory(ConditionExpressionDup, ConditionExpression, i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Condition = NULL;
|
|
|
|
ConditionExpressionDup = NULL;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Find unused breakpoint */
|
|
|
|
if (Type == KdbBreakPointTemporary)
|
|
|
|
{
|
|
|
|
for (i = RTL_NUMBER_OF(KdbBreakPoints) - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (KdbBreakPoints[i].Type == KdbBreakPointNone)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < (LONG)RTL_NUMBER_OF(KdbBreakPoints); i++)
|
|
|
|
{
|
|
|
|
if (KdbBreakPoints[i].Type == KdbBreakPointNone)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(i < (LONG)RTL_NUMBER_OF(KdbBreakPoints));
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Set the breakpoint */
|
|
|
|
ASSERT(KdbCurrentProcess);
|
|
|
|
KdbBreakPoints[i].Type = Type;
|
|
|
|
KdbBreakPoints[i].Address = Address;
|
|
|
|
KdbBreakPoints[i].Enabled = FALSE;
|
|
|
|
KdbBreakPoints[i].Global = Global;
|
|
|
|
KdbBreakPoints[i].Process = KdbCurrentProcess;
|
|
|
|
KdbBreakPoints[i].ConditionExpression = ConditionExpressionDup;
|
|
|
|
KdbBreakPoints[i].Condition = Condition;
|
2004-03-13 18:21:57 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (Type == KdbBreakPointHardware)
|
|
|
|
{
|
|
|
|
KdbBreakPoints[i].Data.Hw.Size = Size;
|
|
|
|
KdbBreakPoints[i].Data.Hw.AccessType = AccessType;
|
|
|
|
}
|
|
|
|
|
|
|
|
KdbBreakPointCount++;
|
|
|
|
|
|
|
|
if (Type != KdbBreakPointTemporary)
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Breakpoint %d inserted.\n", i);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Try to enable the breakpoint */
|
|
|
|
KdbpEnableBreakPoint(i, NULL);
|
|
|
|
|
|
|
|
/* Return the breakpoint number */
|
|
|
|
if (BreakPointNr)
|
|
|
|
*BreakPointNr = i;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2004-03-13 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Deletes a breakpoint
|
|
|
|
*
|
|
|
|
* \param BreakPointNr Number of the breakpoint to delete. Can be -1
|
|
|
|
* \param BreakPoint Breakpoint to delete. Can be NULL.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (invalid breakpoint number)
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpDeleteBreakPoint(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN LONG BreakPointNr OPTIONAL,
|
|
|
|
IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL)
|
2004-03-13 18:21:57 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPointNr < 0)
|
|
|
|
{
|
|
|
|
ASSERT(BreakPoint);
|
|
|
|
BreakPointNr = BreakPoint - KdbBreakPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPointNr < 0 || BreakPointNr >= KDB_MAXIMUM_BREAKPOINT_COUNT)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!BreakPoint)
|
|
|
|
{
|
|
|
|
BreakPoint = KdbBreakPoints + BreakPointNr;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPoint->Type == KdbBreakPointNone)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPoint->Enabled && !KdbpDisableBreakPoint(-1, BreakPoint))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (BreakPoint->Type != KdbBreakPointTemporary)
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Breakpoint %d deleted.\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
BreakPoint->Type = KdbBreakPointNone;
|
|
|
|
KdbBreakPointCount--;
|
|
|
|
|
|
|
|
return TRUE;
|
2004-03-13 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Checks if the breakpoint was set by the debugger
|
|
|
|
*
|
|
|
|
* Tries to find a breakpoint in the breakpoint array which caused
|
|
|
|
* the debug exception to happen.
|
|
|
|
*
|
|
|
|
* \param ExpNr Exception Number (1 or 3)
|
|
|
|
* \param TrapFrame Exception trapframe
|
|
|
|
*
|
|
|
|
* \returns Breakpoint number, -1 on error.
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static LONG
|
2005-03-05 23:35:08 +00:00
|
|
|
KdbpIsBreakPointOurs(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN NTSTATUS ExceptionCode,
|
2020-04-08 21:08:57 +00:00
|
|
|
IN PCONTEXT Context)
|
2001-04-22 14:47:00 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONG i;
|
|
|
|
ASSERT(ExceptionCode == STATUS_SINGLE_STEP || ExceptionCode == STATUS_BREAKPOINT);
|
2001-04-22 14:47:00 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (ExceptionCode == STATUS_BREAKPOINT) /* Software interrupt */
|
|
|
|
{
|
2021-04-27 08:23:37 +00:00
|
|
|
ULONG_PTR BpPc = KeGetContextPc(Context) - 1; /* Get EIP of INT3 instruction */
|
2009-09-09 13:49:04 +00:00
|
|
|
for (i = 0; i < KdbSwBreakPointCount; i++)
|
|
|
|
{
|
|
|
|
ASSERT((KdbSwBreakPoints[i]->Type == KdbBreakPointSoftware ||
|
|
|
|
KdbSwBreakPoints[i]->Type == KdbBreakPointTemporary));
|
|
|
|
ASSERT(KdbSwBreakPoints[i]->Enabled);
|
|
|
|
|
2021-04-27 08:23:37 +00:00
|
|
|
if (KdbSwBreakPoints[i]->Address == BpPc)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
return KdbSwBreakPoints[i] - KdbBreakPoints;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ExceptionCode == STATUS_SINGLE_STEP) /* Hardware interrupt */
|
|
|
|
{
|
|
|
|
UCHAR DebugReg;
|
|
|
|
|
|
|
|
for (i = 0; i < KdbHwBreakPointCount; i++)
|
|
|
|
{
|
|
|
|
ASSERT(KdbHwBreakPoints[i]->Type == KdbBreakPointHardware &&
|
|
|
|
KdbHwBreakPoints[i]->Enabled);
|
|
|
|
DebugReg = KdbHwBreakPoints[i]->Data.Hw.DebugReg;
|
|
|
|
|
2020-01-26 17:59:05 +00:00
|
|
|
if ((Context->Dr6 & ((ULONG_PTR)1 << DebugReg)) != 0)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
return KdbHwBreakPoints[i] - KdbBreakPoints;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return -1;
|
2003-12-23 05:04:59 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Enables a breakpoint.
|
|
|
|
*
|
|
|
|
* \param BreakPointNr Number of the breakpoint to enable Can be -1.
|
|
|
|
* \param BreakPoint Breakpoint to enable. Can be NULL.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure.
|
|
|
|
*
|
|
|
|
* \sa KdbpDisableBreakPoint
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpEnableBreakPoint(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN LONG BreakPointNr OPTIONAL,
|
|
|
|
IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL)
|
2001-04-16 02:02:07 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
INT i;
|
|
|
|
ULONG ul;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPointNr < 0)
|
|
|
|
{
|
|
|
|
ASSERT(BreakPoint);
|
|
|
|
BreakPointNr = BreakPoint - KdbBreakPoints;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPointNr < 0 || BreakPointNr >= KDB_MAXIMUM_BREAKPOINT_COUNT)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!BreakPoint)
|
|
|
|
{
|
|
|
|
BreakPoint = KdbBreakPoints + BreakPointNr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPoint->Type == KdbBreakPointNone)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPoint->Enabled)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Breakpoint %d is already enabled.\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPoint->Type == KdbBreakPointSoftware ||
|
|
|
|
BreakPoint->Type == KdbBreakPointTemporary)
|
|
|
|
{
|
|
|
|
if (KdbSwBreakPointCount >= KDB_MAXIMUM_SW_BREAKPOINT_COUNT)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Maximum number of SW breakpoints (%d) used. "
|
2009-09-09 13:49:04 +00:00
|
|
|
"Disable another breakpoint in order to enable this one.\n",
|
|
|
|
KDB_MAXIMUM_SW_BREAKPOINT_COUNT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = KdbpOverwriteInstruction(BreakPoint->Process, BreakPoint->Address,
|
|
|
|
0xCC, &BreakPoint->Data.SavedInstruction);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Couldn't access memory at 0x%p\n", BreakPoint->Address);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
KdbSwBreakPoints[KdbSwBreakPointCount++] = BreakPoint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (BreakPoint->Data.Hw.AccessType == KdbAccessExec)
|
|
|
|
ASSERT(BreakPoint->Data.Hw.Size == 1);
|
|
|
|
|
|
|
|
ASSERT((BreakPoint->Address % BreakPoint->Data.Hw.Size) == 0);
|
|
|
|
|
|
|
|
if (KdbHwBreakPointCount >= KDB_MAXIMUM_HW_BREAKPOINT_COUNT)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Maximum number of HW breakpoints (%d) already used. "
|
2009-09-09 13:49:04 +00:00
|
|
|
"Disable another breakpoint in order to enable this one.\n",
|
|
|
|
KDB_MAXIMUM_HW_BREAKPOINT_COUNT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find unused hw breakpoint */
|
|
|
|
ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT == 4);
|
|
|
|
for (i = 0; i < KDB_MAXIMUM_HW_BREAKPOINT_COUNT; i++)
|
|
|
|
{
|
2020-04-08 21:08:57 +00:00
|
|
|
if ((KdbTrapFrame.Dr7 & (0x3 << (i * 2))) == 0)
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(i < KDB_MAXIMUM_HW_BREAKPOINT_COUNT);
|
|
|
|
|
|
|
|
/* Set the breakpoint address. */
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case 0:
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr0 = BreakPoint->Address;
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr1 = BreakPoint->Address;
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr2 = BreakPoint->Address;
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr3 = BreakPoint->Address;
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable the global breakpoint */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 |= (0x2 << (i * 2));
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Enable the exact match bits. */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 |= 0x00000300;
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Clear existing state. */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 &= ~(0xF << (16 + (i * 4)));
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Set the breakpoint type. */
|
|
|
|
switch (BreakPoint->Data.Hw.AccessType)
|
|
|
|
{
|
|
|
|
case KdbAccessExec:
|
|
|
|
ul = 0;
|
|
|
|
break;
|
|
|
|
case KdbAccessWrite:
|
|
|
|
ul = 1;
|
|
|
|
break;
|
|
|
|
case KdbAccessRead:
|
|
|
|
case KdbAccessReadWrite:
|
|
|
|
ul = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 |= (ul << (16 + (i * 4)));
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Set the breakpoint length. */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 |= ((BreakPoint->Data.Hw.Size - 1) << (18 + (i * 4)));
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
|
|
|
|
if (&KdbTrapFrame != KdbCurrentTrapFrame)
|
|
|
|
{
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbCurrentTrapFrame->Dr0 = KdbTrapFrame.Dr0;
|
|
|
|
KdbCurrentTrapFrame->Dr1 = KdbTrapFrame.Dr1;
|
|
|
|
KdbCurrentTrapFrame->Dr2 = KdbTrapFrame.Dr2;
|
|
|
|
KdbCurrentTrapFrame->Dr3 = KdbTrapFrame.Dr3;
|
|
|
|
KdbCurrentTrapFrame->Dr6 = KdbTrapFrame.Dr6;
|
|
|
|
KdbCurrentTrapFrame->Dr7 = KdbTrapFrame.Dr7;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2004-01-10 21:06:38 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
BreakPoint->Data.Hw.DebugReg = i;
|
|
|
|
KdbHwBreakPoints[KdbHwBreakPointCount++] = BreakPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakPoint->Enabled = TRUE;
|
|
|
|
if (BreakPoint->Type != KdbBreakPointTemporary)
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Breakpoint %d enabled.\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2004-01-10 21:06:38 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Disables a breakpoint.
|
|
|
|
*
|
|
|
|
* \param BreakPointNr Number of the breakpoint to disable. Can be -1
|
|
|
|
* \param BreakPoint Breakpoint to disable. Can be NULL.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure.
|
|
|
|
*
|
|
|
|
* \sa KdbpEnableBreakPoint
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpDisableBreakPoint(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN LONG BreakPointNr OPTIONAL,
|
|
|
|
IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL)
|
2004-01-10 21:06:38 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONG i;
|
|
|
|
NTSTATUS Status;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPointNr < 0)
|
|
|
|
{
|
|
|
|
ASSERT(BreakPoint);
|
|
|
|
BreakPointNr = BreakPoint - KdbBreakPoints;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPointNr < 0 || BreakPointNr >= KDB_MAXIMUM_BREAKPOINT_COUNT)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!BreakPoint)
|
|
|
|
{
|
|
|
|
BreakPoint = KdbBreakPoints + BreakPointNr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPoint->Type == KdbBreakPointNone)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPoint->Enabled == FALSE)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Breakpoint %d is not enabled.\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (BreakPoint->Type == KdbBreakPointSoftware ||
|
|
|
|
BreakPoint->Type == KdbBreakPointTemporary)
|
|
|
|
{
|
|
|
|
ASSERT(KdbSwBreakPointCount > 0);
|
|
|
|
Status = KdbpOverwriteInstruction(BreakPoint->Process, BreakPoint->Address,
|
|
|
|
BreakPoint->Data.SavedInstruction, NULL);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPuts("Couldn't restore original instruction.\n");
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < KdbSwBreakPointCount; i++)
|
|
|
|
{
|
|
|
|
if (KdbSwBreakPoints[i] == BreakPoint)
|
|
|
|
{
|
|
|
|
KdbSwBreakPoints[i] = KdbSwBreakPoints[--KdbSwBreakPointCount];
|
|
|
|
i = -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-27 20:07:43 +00:00
|
|
|
if (i != MAXULONG) /* not found */
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(BreakPoint->Type == KdbBreakPointHardware);
|
|
|
|
|
|
|
|
/* Clear the breakpoint. */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 &= ~(0x3 << (BreakPoint->Data.Hw.DebugReg * 2));
|
|
|
|
if ((KdbTrapFrame.Dr7 & 0xFF) == 0)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
/* If no breakpoints are enabled then clear the exact match flags. */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.Dr7 &= 0xFFFFFCFF;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < KdbHwBreakPointCount; i++)
|
|
|
|
{
|
|
|
|
if (KdbHwBreakPoints[i] == BreakPoint)
|
|
|
|
{
|
|
|
|
KdbHwBreakPoints[i] = KdbHwBreakPoints[--KdbHwBreakPointCount];
|
|
|
|
i = -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-27 20:07:43 +00:00
|
|
|
if (i != MAXULONG) /* not found */
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakPoint->Enabled = FALSE;
|
|
|
|
if (BreakPoint->Type != KdbBreakPointTemporary)
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Breakpoint %d disabled.\n", BreakPointNr);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2004-01-10 21:06:38 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
|
|
|
|
*
|
|
|
|
* \param ExceptionNr Number of the exception to get condition of.
|
|
|
|
* \param FirstChance Whether to get first or last chance condition.
|
|
|
|
* \param Condition Receives the condition setting.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (invalid exception nr)
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpGetEnterCondition(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN LONG ExceptionNr,
|
|
|
|
IN BOOLEAN FirstChance,
|
|
|
|
OUT KDB_ENTER_CONDITION *Condition)
|
2001-04-16 02:02:07 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (ExceptionNr >= (LONG)RTL_NUMBER_OF(KdbEnterConditions))
|
|
|
|
return FALSE;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
*Condition = KdbEnterConditions[ExceptionNr][FirstChance ? 0 : 1];
|
|
|
|
return TRUE;
|
2001-04-16 02:02:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
|
|
|
|
*
|
|
|
|
* \param ExceptionNr Number of the exception to set condition of (-1 for all)
|
|
|
|
* \param FirstChance Whether to set first or last chance condition.
|
|
|
|
* \param Condition The new condition setting.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (invalid exception nr)
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpSetEnterCondition(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN LONG ExceptionNr,
|
|
|
|
IN BOOLEAN FirstChance,
|
|
|
|
IN KDB_ENTER_CONDITION Condition)
|
2001-04-16 02:02:07 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (ExceptionNr < 0)
|
|
|
|
{
|
|
|
|
for (ExceptionNr = 0; ExceptionNr < (LONG)RTL_NUMBER_OF(KdbEnterConditions); ExceptionNr++)
|
|
|
|
{
|
|
|
|
if (ExceptionNr == 1 || ExceptionNr == 8 ||
|
|
|
|
ExceptionNr == 9 || ExceptionNr == 15) /* Reserved exceptions */
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
KdbEnterConditions[ExceptionNr][FirstChance ? 0 : 1] = Condition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ExceptionNr >= (LONG)RTL_NUMBER_OF(KdbEnterConditions) ||
|
|
|
|
ExceptionNr == 1 || ExceptionNr == 8 || /* Do not allow changing of the debug */
|
|
|
|
ExceptionNr == 9 || ExceptionNr == 15) /* trap or reserved exceptions */
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
KdbEnterConditions[ExceptionNr][FirstChance ? 0 : 1] = Condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2001-04-16 02:02:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Switches to another thread context
|
|
|
|
*
|
|
|
|
* \param ThreadId Id of the thread to switch to.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (i.e. invalid thread id)
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpAttachToThread(
|
2009-09-09 13:49:04 +00:00
|
|
|
PVOID ThreadId)
|
2001-04-16 02:02:07 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PETHREAD Thread = NULL;
|
|
|
|
PEPROCESS Process;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Get a pointer to the thread */
|
|
|
|
if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId, &Thread)))
|
|
|
|
{
|
2010-01-13 22:35:43 +00:00
|
|
|
KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG_PTR)ThreadId);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
Process = Thread->ThreadsProcess;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (KeIsExecutingDpc() && Process != KdbCurrentProcess)
|
|
|
|
{
|
|
|
|
KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
|
|
|
|
ObDereferenceObject(Thread);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Save the current thread's context (if we previously attached to a thread) */
|
|
|
|
if (KdbCurrentThread != KdbOriginalThread)
|
|
|
|
{
|
|
|
|
ASSERT(KdbCurrentTrapFrame == &KdbThreadTrapFrame);
|
|
|
|
/* Actually, we can't save the context, there's no guarantee that there was a trap frame */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(KdbCurrentTrapFrame == &KdbTrapFrame);
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Switch to the thread's context */
|
|
|
|
if (Thread != KdbOriginalThread)
|
|
|
|
{
|
|
|
|
/* The thread we're attaching to isn't the thread on which we entered
|
|
|
|
* kdb and so the thread we're attaching to is not running. There
|
|
|
|
* is no guarantee that it actually has a trap frame. So we have to
|
|
|
|
* peek directly at the registers which were saved on the stack when the
|
|
|
|
* thread was preempted in the scheduler */
|
|
|
|
KdbpKdbTrapFrameFromKernelStack(Thread->Tcb.KernelStack,
|
|
|
|
&KdbThreadTrapFrame);
|
|
|
|
KdbCurrentTrapFrame = &KdbThreadTrapFrame;
|
|
|
|
}
|
|
|
|
else /* Switching back to original thread */
|
|
|
|
{
|
|
|
|
KdbCurrentTrapFrame = &KdbTrapFrame;
|
|
|
|
}
|
|
|
|
KdbCurrentThread = Thread;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Attach to the thread's process */
|
|
|
|
ASSERT(KdbCurrentProcess == PsGetCurrentProcess());
|
|
|
|
if (KdbCurrentProcess != Process)
|
|
|
|
{
|
|
|
|
if (KdbCurrentProcess != KdbOriginalProcess) /* detach from previously attached process */
|
|
|
|
{
|
|
|
|
KeUnstackDetachProcess(&KdbApcState);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KdbOriginalProcess != Process)
|
|
|
|
{
|
|
|
|
KeStackAttachProcess(&Process->Pcb, &KdbApcState);
|
|
|
|
}
|
|
|
|
|
|
|
|
KdbCurrentProcess = Process;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
ObDereferenceObject(Thread);
|
|
|
|
return TRUE;
|
2001-04-16 02:02:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief Switches to another process/thread context
|
|
|
|
*
|
|
|
|
* This function switches to the first thread in the specified process.
|
|
|
|
*
|
|
|
|
* \param ProcessId Id of the process to switch to.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (i.e. invalid process id)
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpAttachToProcess(
|
2009-09-09 13:49:04 +00:00
|
|
|
PVOID ProcessId)
|
2001-04-16 02:02:07 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PEPROCESS Process = NULL;
|
|
|
|
PETHREAD Thread;
|
|
|
|
PLIST_ENTRY Entry;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Get a pointer to the process */
|
|
|
|
if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId, &Process)))
|
|
|
|
{
|
2010-01-13 22:35:43 +00:00
|
|
|
KdbpPrint("Invalid process id: 0x%08x\n", (ULONG_PTR)ProcessId);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
Entry = Process->ThreadListHead.Flink;
|
|
|
|
ObDereferenceObject(Process);
|
|
|
|
if (Entry == &KdbCurrentProcess->ThreadListHead)
|
|
|
|
{
|
2010-01-13 22:35:43 +00:00
|
|
|
KdbpPrint("No threads in process 0x%p, cannot attach to process!\n", ProcessId);
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
Thread = CONTAINING_RECORD(Entry, ETHREAD, ThreadListEntry);
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return KdbpAttachToThread(Thread->Cid.UniqueThread);
|
2001-04-16 02:02:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 02:13:31 +00:00
|
|
|
/**
|
|
|
|
* @brief Calls the main interactive debugger loop.
|
|
|
|
**/
|
2009-06-13 15:28:33 +00:00
|
|
|
static VOID
|
2009-09-17 18:28:26 +00:00
|
|
|
KdbpCallMainLoop(VOID)
|
2001-04-12 00:56:04 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpCliMainLoop(KdbEnteredOnSingleStep);
|
2001-04-12 00:56:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 02:13:31 +00:00
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Internal function to enter KDBG and run the specified procedure.
|
2005-03-05 23:35:08 +00:00
|
|
|
*
|
|
|
|
* Disables interrupts, releases display ownership, ...
|
2022-11-28 02:13:31 +00:00
|
|
|
*
|
|
|
|
* @param[in] Procedure
|
|
|
|
* The procedure to execute under the KDBG environment.
|
|
|
|
* Either execute the main interactive debugger loop (KdbpCallMainLoop)
|
|
|
|
* or run the KDBinit file (KdbpCliInterpretInitFile).
|
|
|
|
**/
|
2009-06-13 15:28:33 +00:00
|
|
|
static VOID
|
2022-11-28 02:13:31 +00:00
|
|
|
KdbpInternalEnter(
|
|
|
|
_In_ VOID (*Procedure)(VOID))
|
2005-05-09 01:38:29 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PETHREAD Thread;
|
|
|
|
PVOID SavedInitialStack, SavedStackBase, SavedKernelStack;
|
|
|
|
ULONG SavedStackLimit;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2019-12-26 16:19:11 +00:00
|
|
|
KbdDisableMouse();
|
2011-11-21 05:28:08 +00:00
|
|
|
|
2019-12-26 16:19:11 +00:00
|
|
|
/* Take control of the display */
|
|
|
|
if (KdpDebugMode.Screen)
|
|
|
|
KdpScreenAcquire();
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2022-11-28 02:13:31 +00:00
|
|
|
/* Call the specified debugger procedure on a different stack */
|
2009-09-09 13:49:04 +00:00
|
|
|
Thread = PsGetCurrentThread();
|
|
|
|
SavedInitialStack = Thread->Tcb.InitialStack;
|
|
|
|
SavedStackBase = Thread->Tcb.StackBase;
|
|
|
|
SavedStackLimit = Thread->Tcb.StackLimit;
|
|
|
|
SavedKernelStack = Thread->Tcb.KernelStack;
|
|
|
|
Thread->Tcb.InitialStack = Thread->Tcb.StackBase = (char*)KdbStack + KDB_STACK_SIZE;
|
2010-01-13 22:35:43 +00:00
|
|
|
Thread->Tcb.StackLimit = (ULONG_PTR)KdbStack;
|
2009-09-09 13:49:04 +00:00
|
|
|
Thread->Tcb.KernelStack = (char*)KdbStack + KDB_STACK_SIZE;
|
|
|
|
|
2024-10-14 18:27:38 +00:00
|
|
|
// KdbPrintf("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n",
|
|
|
|
// Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
2022-11-28 02:13:31 +00:00
|
|
|
KdbpStackSwitchAndCall(KdbStack + KDB_STACK_SIZE - KDB_STACK_RESERVE, Procedure);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
Thread->Tcb.InitialStack = SavedInitialStack;
|
|
|
|
Thread->Tcb.StackBase = SavedStackBase;
|
|
|
|
Thread->Tcb.StackLimit = SavedStackLimit;
|
|
|
|
Thread->Tcb.KernelStack = SavedKernelStack;
|
2019-11-17 15:44:22 +00:00
|
|
|
|
2019-12-26 16:19:11 +00:00
|
|
|
/* Release the display */
|
|
|
|
if (KdpDebugMode.Screen)
|
|
|
|
KdpScreenRelease();
|
2019-11-17 15:44:22 +00:00
|
|
|
|
2019-12-26 16:19:11 +00:00
|
|
|
KbdEnableMouse();
|
2004-11-18 02:10:28 +00:00
|
|
|
}
|
|
|
|
|
2009-06-13 15:28:33 +00:00
|
|
|
static ULONG
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpGetExceptionNumberFromStatus(
|
|
|
|
IN NTSTATUS ExceptionCode)
|
2006-05-10 17:47:44 +00:00
|
|
|
{
|
|
|
|
ULONG Ret;
|
|
|
|
|
|
|
|
switch (ExceptionCode)
|
|
|
|
{
|
|
|
|
case STATUS_INTEGER_DIVIDE_BY_ZERO:
|
|
|
|
Ret = 0;
|
|
|
|
break;
|
|
|
|
case STATUS_SINGLE_STEP:
|
|
|
|
Ret = 1;
|
|
|
|
break;
|
|
|
|
case STATUS_BREAKPOINT:
|
|
|
|
Ret = 3;
|
|
|
|
break;
|
|
|
|
case STATUS_INTEGER_OVERFLOW:
|
|
|
|
Ret = 4;
|
|
|
|
break;
|
|
|
|
case STATUS_ARRAY_BOUNDS_EXCEEDED:
|
|
|
|
Ret = 5;
|
|
|
|
break;
|
|
|
|
case STATUS_ILLEGAL_INSTRUCTION:
|
|
|
|
Ret = 6;
|
|
|
|
break;
|
|
|
|
case STATUS_FLOAT_INVALID_OPERATION:
|
|
|
|
Ret = 7;
|
|
|
|
break;
|
|
|
|
case STATUS_STACK_OVERFLOW:
|
|
|
|
Ret = 12;
|
|
|
|
break;
|
|
|
|
case STATUS_ACCESS_VIOLATION:
|
|
|
|
Ret = 14;
|
|
|
|
break;
|
|
|
|
case STATUS_DATATYPE_MISALIGNMENT:
|
|
|
|
Ret = 17;
|
|
|
|
break;
|
|
|
|
case STATUS_FLOAT_MULTIPLE_TRAPS:
|
|
|
|
Ret = 18;
|
|
|
|
break;
|
2014-10-16 21:33:32 +00:00
|
|
|
case STATUS_ASSERTION_FAILURE:
|
|
|
|
Ret = 20;
|
|
|
|
break;
|
2006-05-10 17:47:44 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
Ret = RTL_NUMBER_OF(KdbEnterConditions) - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
/*!\brief KDB Exception filter
|
|
|
|
*
|
|
|
|
* Called by the exception dispatcher.
|
|
|
|
*
|
|
|
|
* \param ExceptionRecord Unused.
|
|
|
|
* \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
|
2008-06-11 11:34:04 +00:00
|
|
|
* \param Context Context, IN/OUT parameter.
|
2005-03-05 23:35:08 +00:00
|
|
|
* \param TrapFrame Exception TrapFrame.
|
|
|
|
* \param FirstChance TRUE when called before exception frames were serached,
|
|
|
|
* FALSE for the second call.
|
|
|
|
*
|
|
|
|
* \returns KD_CONTINUE_TYPE
|
|
|
|
*/
|
|
|
|
KD_CONTINUE_TYPE
|
|
|
|
KdbEnterDebuggerException(
|
2020-04-08 21:08:57 +00:00
|
|
|
IN PEXCEPTION_RECORD64 ExceptionRecord,
|
2009-09-09 13:49:04 +00:00
|
|
|
IN KPROCESSOR_MODE PreviousMode,
|
|
|
|
IN PCONTEXT Context,
|
|
|
|
IN BOOLEAN FirstChance)
|
2005-01-12 19:04:06 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
KDB_ENTER_CONDITION EnterCondition;
|
|
|
|
KD_CONTINUE_TYPE ContinueType = kdHandleException;
|
|
|
|
PKDB_BREAKPOINT BreakPoint;
|
|
|
|
ULONG ExpNr;
|
|
|
|
ULONGLONG ull;
|
|
|
|
BOOLEAN Resume = FALSE;
|
|
|
|
BOOLEAN EnterConditionMet = TRUE;
|
|
|
|
ULONG OldEflags;
|
2009-12-26 19:36:24 +00:00
|
|
|
KIRQL OldIrql;
|
2009-09-09 13:49:04 +00:00
|
|
|
NTSTATUS ExceptionCode;
|
2022-11-28 02:13:31 +00:00
|
|
|
VOID (*EntryPoint)(VOID) = KdbpCallMainLoop;
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
ExceptionCode = (ExceptionRecord ? ExceptionRecord->ExceptionCode : STATUS_BREAKPOINT);
|
|
|
|
|
|
|
|
KdbCurrentProcess = PsGetCurrentProcess();
|
|
|
|
|
|
|
|
/* Set continue type to kdContinue for single steps and breakpoints */
|
2015-09-12 22:01:10 +00:00
|
|
|
if (ExceptionCode == STATUS_SINGLE_STEP ||
|
|
|
|
ExceptionCode == STATUS_BREAKPOINT ||
|
|
|
|
ExceptionCode == STATUS_ASSERTION_FAILURE)
|
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ContinueType = kdContinue;
|
2015-09-12 22:01:10 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Check if we should handle the exception. */
|
|
|
|
/* FIXME - won't get all exceptions here :( */
|
|
|
|
ExpNr = KdbpGetExceptionNumberFromStatus(ExceptionCode);
|
|
|
|
EnterCondition = KdbEnterConditions[ExpNr][FirstChance ? 0 : 1];
|
|
|
|
if (EnterCondition == KdbDoNotEnter ||
|
|
|
|
(EnterCondition == KdbEnterFromUmode && PreviousMode == KernelMode) ||
|
|
|
|
(EnterCondition == KdbEnterFromKmode && PreviousMode != KernelMode))
|
|
|
|
{
|
|
|
|
EnterConditionMet = FALSE;
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2019-11-17 15:44:22 +00:00
|
|
|
/* If we stopped on one of our breakpoints then let the user know */
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbLastBreakPointNr = -1;
|
|
|
|
KdbEnteredOnSingleStep = FALSE;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (FirstChance && (ExceptionCode == STATUS_SINGLE_STEP || ExceptionCode == STATUS_BREAKPOINT) &&
|
2020-04-08 21:08:57 +00:00
|
|
|
(KdbLastBreakPointNr = KdbpIsBreakPointOurs(ExceptionCode, Context)) >= 0)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
BreakPoint = KdbBreakPoints + KdbLastBreakPointNr;
|
|
|
|
|
|
|
|
if (ExceptionCode == STATUS_BREAKPOINT)
|
|
|
|
{
|
2019-11-17 15:44:22 +00:00
|
|
|
/* ... and restore the original instruction */
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess, BreakPoint->Address,
|
|
|
|
BreakPoint->Data.SavedInstruction, NULL)))
|
2005-03-05 23:35:08 +00:00
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPuts("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
|
2009-09-09 13:49:04 +00:00
|
|
|
KeBugCheck(0); // FIXME: Proper bugcode!
|
2005-03-05 23:35:08 +00:00
|
|
|
}
|
2015-12-23 22:38:46 +00:00
|
|
|
|
|
|
|
/* Also since we are past the int3 now, decrement EIP in the
|
|
|
|
TrapFrame. This is only needed because KDBG insists on working
|
|
|
|
with the TrapFrame instead of with the Context, as it is supposed
|
|
|
|
to do. The context has already EIP point to the int3, since
|
|
|
|
KiDispatchException accounts for that. Whatever we do here with
|
|
|
|
the TrapFrame does not matter anyway, since KiDispatchException
|
|
|
|
will overwrite it with the values from the Context! */
|
2021-04-27 08:23:37 +00:00
|
|
|
KeSetContextPc(Context, KeGetContextPc(Context) - 1);
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((BreakPoint->Type == KdbBreakPointHardware) &&
|
|
|
|
(BreakPoint->Data.Hw.AccessType == KdbAccessExec))
|
|
|
|
{
|
|
|
|
Resume = TRUE; /* Set the resume flag when continuing execution */
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* When a temporary breakpoint is hit we have to make sure that we are
|
|
|
|
* in the same context in which it was set, otherwise it could happen
|
|
|
|
* that another process/thread hits it before and it gets deleted.
|
|
|
|
*/
|
|
|
|
else if (BreakPoint->Type == KdbBreakPointTemporary &&
|
|
|
|
BreakPoint->Process == KdbCurrentProcess)
|
|
|
|
{
|
2020-04-08 21:08:57 +00:00
|
|
|
ASSERT((Context->EFlags & EFLAGS_TF) == 0);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
2019-11-17 15:44:22 +00:00
|
|
|
/* Delete the temporary breakpoint which was used to step over or into the instruction */
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpDeleteBreakPoint(-1, BreakPoint);
|
|
|
|
|
|
|
|
if (--KdbNumSingleSteps > 0)
|
|
|
|
{
|
2021-04-27 08:23:37 +00:00
|
|
|
if ((KdbSingleStepOver && !KdbpStepOverInstruction(KeGetContextPc(Context))) ||
|
|
|
|
(!KdbSingleStepOver && !KdbpStepIntoInstruction(KeGetContextPc(Context))))
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
Context->EFlags |= EFLAGS_TF;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto continue_execution; /* return */
|
|
|
|
}
|
|
|
|
|
|
|
|
KdbEnteredOnSingleStep = TRUE;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we hit a breakpoint set by the debugger we set the single step flag,
|
|
|
|
* ignore the next single step and reenable the breakpoint.
|
|
|
|
*/
|
|
|
|
else if (BreakPoint->Type == KdbBreakPointSoftware ||
|
|
|
|
BreakPoint->Type == KdbBreakPointTemporary)
|
|
|
|
{
|
|
|
|
ASSERT(ExceptionCode == STATUS_BREAKPOINT);
|
|
|
|
Context->EFlags |= EFLAGS_TF;
|
|
|
|
KdbBreakPointToReenable = BreakPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the breakpoint should be triggered in this context */
|
|
|
|
if (!BreakPoint->Global && BreakPoint->Process != KdbCurrentProcess)
|
|
|
|
{
|
2005-03-05 23:35:08 +00:00
|
|
|
goto continue_execution; /* return */
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the condition for the breakpoint is met. */
|
|
|
|
if (BreakPoint->Condition)
|
|
|
|
{
|
|
|
|
/* Setup the KDB trap frame */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame = *Context;
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
ull = 0;
|
|
|
|
if (!KdbpRpnEvaluateParsedExpression(BreakPoint->Condition, &KdbTrapFrame, &ull, NULL, NULL))
|
|
|
|
{
|
|
|
|
/* FIXME: Print warning? */
|
|
|
|
}
|
|
|
|
else if (ull == 0) /* condition is not met */
|
|
|
|
{
|
|
|
|
goto continue_execution; /* return */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BreakPoint->Type == KdbBreakPointSoftware)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("\nEntered debugger on breakpoint #%d: EXEC 0x%04x:0x%p\n",
|
2021-04-27 08:23:37 +00:00
|
|
|
KdbLastBreakPointNr, Context->SegCs & 0xffff, KeGetContextPc(Context));
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
else if (BreakPoint->Type == KdbBreakPointHardware)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("\nEntered debugger on breakpoint #%d: %s 0x%08x\n",
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbLastBreakPointNr,
|
2022-11-28 01:48:55 +00:00
|
|
|
(BreakPoint->Data.Hw.AccessType == KdbAccessRead) ? "READ" :
|
|
|
|
((BreakPoint->Data.Hw.AccessType == KdbAccessWrite) ? "WRITE" :
|
|
|
|
((BreakPoint->Data.Hw.AccessType == KdbAccessReadWrite) ? "RDWR" : "EXEC")),
|
|
|
|
BreakPoint->Address);
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ExceptionCode == STATUS_SINGLE_STEP)
|
|
|
|
{
|
|
|
|
/* Silently ignore a debugger initiated single step. */
|
2020-04-08 21:08:57 +00:00
|
|
|
if ((Context->Dr6 & 0xf) == 0 && KdbBreakPointToReenable)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
/* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
|
|
|
|
BreakPoint = KdbBreakPointToReenable;
|
|
|
|
KdbBreakPointToReenable = NULL;
|
|
|
|
ASSERT(BreakPoint->Type == KdbBreakPointSoftware ||
|
|
|
|
BreakPoint->Type == KdbBreakPointTemporary);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reenable the breakpoint we disabled to execute the breakpointed
|
|
|
|
* instruction.
|
|
|
|
*/
|
|
|
|
if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess, BreakPoint->Address, 0xCC,
|
|
|
|
&BreakPoint->Data.SavedInstruction)))
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("Warning: Couldn't reenable breakpoint %d\n",
|
2009-09-09 13:49:04 +00:00
|
|
|
BreakPoint - KdbBreakPoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unset TF if we are no longer single stepping. */
|
|
|
|
if (KdbNumSingleSteps == 0)
|
|
|
|
Context->EFlags &= ~EFLAGS_TF;
|
|
|
|
|
2015-12-23 22:38:46 +00:00
|
|
|
if (!KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep)
|
|
|
|
{
|
|
|
|
goto continue_execution; /* return */
|
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 22:38:46 +00:00
|
|
|
/* Quoth the raven, 'Nevermore!' */
|
|
|
|
KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep = FALSE;
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Check if we expect a single step */
|
2020-04-08 21:08:57 +00:00
|
|
|
if ((Context->Dr6 & 0xf) == 0 && KdbNumSingleSteps > 0)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
/*ASSERT((Context->Eflags & EFLAGS_TF) != 0);*/
|
|
|
|
if (--KdbNumSingleSteps > 0)
|
2005-03-05 23:35:08 +00:00
|
|
|
{
|
2021-04-27 08:23:37 +00:00
|
|
|
if ((KdbSingleStepOver && KdbpStepOverInstruction(KeGetContextPc(Context))) ||
|
|
|
|
(!KdbSingleStepOver && KdbpStepIntoInstruction(KeGetContextPc(Context))))
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
|
|
|
Context->EFlags &= ~EFLAGS_TF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Context->EFlags |= EFLAGS_TF;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto continue_execution; /* return */
|
2005-03-05 23:35:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
Context->EFlags &= ~EFLAGS_TF;
|
|
|
|
KdbEnteredOnSingleStep = TRUE;
|
2005-03-05 23:35:08 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!EnterConditionMet)
|
|
|
|
{
|
2019-12-26 16:19:11 +00:00
|
|
|
return kdHandleException;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPuts("\nEntered debugger on unexpected debug trap!\n");
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ExceptionCode == STATUS_BREAKPOINT)
|
|
|
|
{
|
2022-11-28 02:13:31 +00:00
|
|
|
/* Do the condition check and banner display only if we enter
|
|
|
|
* from a true code breakpoint. We skip those when running the
|
|
|
|
* KDBinit file, because it is done via an artificial breakpoint. */
|
2009-09-09 13:49:04 +00:00
|
|
|
if (KdbInitFileBuffer)
|
|
|
|
{
|
2022-11-28 02:13:31 +00:00
|
|
|
EntryPoint = KdbpCliInterpretInitFile;
|
|
|
|
goto EnterKdbg;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2022-11-28 02:13:31 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!EnterConditionMet)
|
|
|
|
{
|
2019-12-26 16:19:11 +00:00
|
|
|
return kdHandleException;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("\nEntered debugger on embedded INT3 at 0x%04x:0x%p.\n",
|
2020-01-26 17:59:05 +00:00
|
|
|
Context->SegCs & 0xffff, KeGetContextPc(Context));
|
2022-11-28 02:13:31 +00:00
|
|
|
EnterKdbg:;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const CHAR *ExceptionString = (ExpNr < RTL_NUMBER_OF(ExceptionNrToString)) ?
|
2022-11-28 01:48:55 +00:00
|
|
|
ExceptionNrToString[ExpNr] :
|
|
|
|
"Unknown/User defined exception";
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
if (!EnterConditionMet)
|
|
|
|
{
|
2019-12-26 16:19:11 +00:00
|
|
|
return ContinueType;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 01:48:55 +00:00
|
|
|
KdbPrintf("\nEntered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
|
2009-09-09 13:49:04 +00:00
|
|
|
FirstChance ? "first" : "last", ExceptionCode, ExceptionString);
|
|
|
|
|
|
|
|
if (ExceptionCode == STATUS_ACCESS_VIOLATION &&
|
|
|
|
ExceptionRecord && ExceptionRecord->NumberParameters != 0)
|
|
|
|
{
|
2022-11-28 01:48:55 +00:00
|
|
|
ULONG_PTR TrapCr2 = __readcr2();
|
|
|
|
KdbPrintf("Memory at 0x%p could not be accessed\n", TrapCr2);
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Once we enter the debugger we do not expect any more single steps to happen */
|
|
|
|
KdbNumSingleSteps = 0;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Update the current process pointer */
|
|
|
|
KdbCurrentProcess = KdbOriginalProcess = PsGetCurrentProcess();
|
|
|
|
KdbCurrentThread = KdbOriginalThread = PsGetCurrentThread();
|
|
|
|
KdbCurrentTrapFrame = &KdbTrapFrame;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Setup the KDB trap frame */
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame = *Context;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Enter critical section */
|
2009-09-21 15:20:18 +00:00
|
|
|
OldEflags = __readeflags();
|
2009-09-09 13:49:04 +00:00
|
|
|
_disable();
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2021-06-22 17:46:27 +00:00
|
|
|
/* HACK: Save the current IRQL and pretend we are at dispatch level */
|
2009-12-26 19:36:24 +00:00
|
|
|
OldIrql = KeGetCurrentIrql();
|
2021-06-22 17:46:27 +00:00
|
|
|
if (OldIrql > DISPATCH_LEVEL)
|
|
|
|
KeLowerIrql(DISPATCH_LEVEL);
|
2009-12-26 19:36:24 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Exception inside the debugger? Game over. */
|
|
|
|
if (InterlockedIncrement(&KdbEntryCount) > 1)
|
|
|
|
{
|
2009-09-21 15:20:18 +00:00
|
|
|
__writeeflags(OldEflags);
|
2019-12-26 16:19:11 +00:00
|
|
|
return kdHandleException;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2005-03-31 16:08:09 +00:00
|
|
|
|
2022-11-28 02:13:31 +00:00
|
|
|
/* Enter KDBG proper and run either the main loop or the KDBinit file */
|
|
|
|
KdbpInternalEnter(EntryPoint);
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Check if we should single step */
|
|
|
|
if (KdbNumSingleSteps > 0)
|
|
|
|
{
|
2015-12-23 22:38:46 +00:00
|
|
|
/* Variable explains itself! */
|
|
|
|
KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep = TRUE;
|
|
|
|
|
2021-04-27 08:23:37 +00:00
|
|
|
if ((KdbSingleStepOver && KdbpStepOverInstruction(KeGetContextPc(KdbCurrentTrapFrame))) ||
|
|
|
|
(!KdbSingleStepOver && KdbpStepIntoInstruction(KeGetContextPc(KdbCurrentTrapFrame))))
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
2020-04-08 21:08:57 +00:00
|
|
|
ASSERT((KdbCurrentTrapFrame->EFlags & EFLAGS_TF) == 0);
|
|
|
|
/*KdbCurrentTrapFrame->EFlags &= ~EFLAGS_TF;*/
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-08 21:08:57 +00:00
|
|
|
KdbTrapFrame.EFlags |= EFLAGS_TF;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* We can't update the current thread's trapframe 'cause it might not have one */
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Detach from attached process */
|
|
|
|
if (KdbCurrentProcess != KdbOriginalProcess)
|
|
|
|
{
|
|
|
|
KeUnstackDetachProcess(&KdbApcState);
|
|
|
|
}
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2020-04-08 21:08:57 +00:00
|
|
|
/* Update the exception Context */
|
|
|
|
*Context = KdbTrapFrame;
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Decrement the entry count */
|
|
|
|
InterlockedDecrement(&KdbEntryCount);
|
2005-03-31 16:08:09 +00:00
|
|
|
|
2014-09-13 20:18:02 +00:00
|
|
|
/* HACK: Raise back to old IRQL */
|
2021-06-22 17:46:27 +00:00
|
|
|
if (OldIrql > DISPATCH_LEVEL)
|
|
|
|
KeRaiseIrql(OldIrql, &OldIrql);
|
2009-12-26 19:36:24 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Leave critical section */
|
2009-09-21 15:20:18 +00:00
|
|
|
__writeeflags(OldEflags);
|
2005-03-05 23:35:08 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Check if user requested a bugcheck */
|
|
|
|
if (KdbpBugCheckRequested)
|
|
|
|
{
|
|
|
|
/* Clear the flag and bugcheck the system */
|
|
|
|
KdbpBugCheckRequested = FALSE;
|
|
|
|
KeBugCheck(MANUALLY_INITIATED_CRASH);
|
|
|
|
}
|
2008-11-06 18:33:56 +00:00
|
|
|
|
2005-03-05 23:35:08 +00:00
|
|
|
continue_execution:
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Clear debug status */
|
|
|
|
if (ExceptionCode == STATUS_BREAKPOINT) /* FIXME: Why clear DR6 on INT3? */
|
|
|
|
{
|
|
|
|
/* Set the RF flag so we don't trigger the same breakpoint again. */
|
|
|
|
if (Resume)
|
|
|
|
{
|
2020-04-08 21:08:57 +00:00
|
|
|
Context->EFlags |= EFLAGS_RF;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Clear dr6 status flags. */
|
2020-04-08 21:08:57 +00:00
|
|
|
Context->Dr6 &= ~0x0000e00f;
|
2001-05-05 19:13:10 +00:00
|
|
|
|
2010-08-19 10:03:03 +00:00
|
|
|
if (!(KdbEnteredOnSingleStep && KdbSingleStepOver))
|
2010-08-19 09:03:36 +00:00
|
|
|
{
|
|
|
|
/* Skip the current instruction */
|
2021-04-27 08:23:37 +00:00
|
|
|
KeSetContextPc(Context, KeGetContextPc(Context) + KD_BREAKPOINT_SIZE);
|
2010-08-19 09:03:36 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2004-01-10 21:06:38 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return ContinueType;
|
2001-04-12 00:56:04 +00:00
|
|
|
}
|
|
|
|
|
2005-01-12 19:04:06 +00:00
|
|
|
VOID
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpGetCommandLineSettings(
|
2023-03-29 00:06:35 +00:00
|
|
|
_In_ PCSTR p1)
|
2005-01-12 19:04:06 +00:00
|
|
|
{
|
2012-12-02 15:36:18 +00:00
|
|
|
#define CONST_STR_LEN(x) (sizeof(x)/sizeof(x[0]) - 1)
|
2005-04-25 14:44:48 +00:00
|
|
|
|
2023-03-29 00:06:35 +00:00
|
|
|
while (p1 && *p1)
|
2005-04-25 14:44:48 +00:00
|
|
|
{
|
2023-03-29 00:06:35 +00:00
|
|
|
/* Skip leading whitespace */
|
2012-12-02 15:30:59 +00:00
|
|
|
while (*p1 == ' ') ++p1;
|
2005-04-25 14:44:48 +00:00
|
|
|
|
2023-03-29 00:06:35 +00:00
|
|
|
if (!_strnicmp(p1, "FIRSTCHANCE", CONST_STR_LEN("FIRSTCHANCE")))
|
2010-06-06 07:35:21 +00:00
|
|
|
{
|
2012-12-02 15:30:59 +00:00
|
|
|
p1 += CONST_STR_LEN("FIRSTCHANCE");
|
2010-06-06 07:35:21 +00:00
|
|
|
KdbpSetEnterCondition(-1, TRUE, KdbEnterAlways);
|
|
|
|
}
|
2023-03-29 00:06:35 +00:00
|
|
|
|
|
|
|
/* Move on to the next option */
|
|
|
|
p1 = strchr(p1, ' ');
|
2005-04-25 14:44:48 +00:00
|
|
|
}
|
2005-01-12 19:04:06 +00:00
|
|
|
}
|
2005-07-22 20:52:31 +00:00
|
|
|
|
2019-11-03 22:36:40 +00:00
|
|
|
NTSTATUS
|
|
|
|
KdbpSafeReadMemory(
|
|
|
|
OUT PVOID Dest,
|
|
|
|
IN PVOID Src,
|
|
|
|
IN ULONG Bytes)
|
|
|
|
{
|
|
|
|
return KdpCopyMemoryChunks((ULONG64)(ULONG_PTR)Src,
|
|
|
|
Dest,
|
|
|
|
Bytes,
|
|
|
|
0,
|
|
|
|
MMDBG_COPY_UNSAFE,
|
|
|
|
NULL);
|
2005-07-22 20:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
2009-09-09 13:49:04 +00:00
|
|
|
KdbpSafeWriteMemory(
|
|
|
|
OUT PVOID Dest,
|
|
|
|
IN PVOID Src,
|
|
|
|
IN ULONG Bytes)
|
2005-07-22 20:52:31 +00:00
|
|
|
{
|
2019-11-03 22:36:40 +00:00
|
|
|
return KdpCopyMemoryChunks((ULONG64)(ULONG_PTR)Dest,
|
|
|
|
Src,
|
|
|
|
Bytes,
|
|
|
|
0,
|
|
|
|
MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
|
|
|
|
NULL);
|
2005-07-22 20:52:31 +00:00
|
|
|
}
|