2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/kdbg/kdb.c
5 * PURPOSE: Kernel Debugger
7 * PROGRAMMERS: Gregor Anich
10 /* INCLUDES ******************************************************************/
16 /* TYPES *********************************************************************/
18 /* DEFINES *******************************************************************/
20 #define KDB_STACK_SIZE (4096*3)
21 #define KDB_MAXIMUM_BREAKPOINT_COUNT 256
22 #define KDB_MAXIMUM_HW_BREAKPOINT_COUNT 4
23 #define KDB_MAXIMUM_SW_BREAKPOINT_COUNT 256
25 #define __STRING(x) #x
26 #define _STRING(x) __STRING(x)
28 /* GLOBALS *******************************************************************/
30 static LONG KdbEntryCount
= 0;
31 static CHAR KdbStack
[KDB_STACK_SIZE
];
33 static ULONG KdbBreakPointCount
= 0; /* Number of used breakpoints in the array */
34 static KDB_BREAKPOINT KdbBreakPoints
[KDB_MAXIMUM_BREAKPOINT_COUNT
] = {{0}}; /* Breakpoint array */
35 static ULONG KdbSwBreakPointCount
= 0; /* Number of enabled software breakpoints */
36 static ULONG KdbHwBreakPointCount
= 0; /* Number of enabled hardware breakpoints */
37 static PKDB_BREAKPOINT KdbSwBreakPoints
[KDB_MAXIMUM_SW_BREAKPOINT_COUNT
]; /* Enabled software breakpoints, orderless */
38 static PKDB_BREAKPOINT KdbHwBreakPoints
[KDB_MAXIMUM_HW_BREAKPOINT_COUNT
]; /* Enabled hardware breakpoints, orderless */
39 static PKDB_BREAKPOINT KdbBreakPointToReenable
= NULL
; /* Set to a breakpoint struct when single stepping after
40 a software breakpoint was hit, to reenable it */
41 LONG KdbLastBreakPointNr
= -1; /* Index of the breakpoint which cause KDB to be entered */
42 ULONG KdbNumSingleSteps
= 0; /* How many single steps to do */
43 BOOLEAN KdbSingleStepOver
= FALSE
; /* Whether to step over calls/reps. */
44 ULONG KdbDebugState
= 0; /* KDBG Settings (NOECHO, KDSERIAL) */
45 static BOOLEAN KdbEnteredOnSingleStep
= FALSE
; /* Set to true when KDB was entered because of single step */
46 PEPROCESS KdbCurrentProcess
= NULL
; /* The current process context in which KDB runs */
47 PEPROCESS KdbOriginalProcess
= NULL
; /* The process in whichs context KDB was intered */
48 PETHREAD KdbCurrentThread
= NULL
; /* The current thread context in which KDB runs */
49 PETHREAD KdbOriginalThread
= NULL
; /* The thread in whichs context KDB was entered */
50 PKDB_KTRAP_FRAME KdbCurrentTrapFrame
= NULL
; /* Pointer to the current trapframe */
51 static KDB_KTRAP_FRAME KdbTrapFrame
= { { 0 } }; /* The trapframe which was passed to KdbEnterDebuggerException */
52 static KDB_KTRAP_FRAME KdbThreadTrapFrame
= { { 0 } }; /* The trapframe of the current thread (KdbCurrentThread) */
53 static KAPC_STATE KdbApcState
;
54 extern BOOLEAN KdbpBugCheckRequested
;
56 /* Array of conditions when to enter KDB */
57 static KDB_ENTER_CONDITION KdbEnterConditions
[][2] =
59 /* First chance Last chance */
60 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Zero devide */
61 { KdbEnterFromKmode
, KdbDoNotEnter
}, /* Debug trap */
62 { KdbDoNotEnter
, KdbEnterAlways
}, /* NMI */
63 { KdbEnterFromKmode
, KdbDoNotEnter
}, /* INT3 */
64 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Overflow */
65 { KdbDoNotEnter
, KdbEnterFromKmode
},
66 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Invalid opcode */
67 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* No math coprocessor fault */
68 { KdbEnterAlways
, KdbEnterAlways
},
69 { KdbEnterAlways
, KdbEnterAlways
},
70 { KdbDoNotEnter
, KdbEnterFromKmode
},
71 { KdbDoNotEnter
, KdbEnterFromKmode
},
72 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Stack fault */
73 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* General protection fault */
74 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Page fault */
75 { KdbEnterAlways
, KdbEnterAlways
}, /* Reserved (15) */
76 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* FPU fault */
77 { KdbDoNotEnter
, KdbEnterFromKmode
},
78 { KdbDoNotEnter
, KdbEnterFromKmode
},
79 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* SIMD fault */
80 { KdbDoNotEnter
, KdbEnterFromKmode
} /* Last entry: used for unknown exceptions */
83 /* Exception descriptions */
84 static const CHAR
*ExceptionNrToString
[] =
91 "BOUND range exceeded",
93 "No Math Coprocessor",
97 "Segment Not Present",
98 "Stack Segment Fault",
111 IN PKTRAP_FRAME TrapFrame
);
116 IN PKTRAP_FRAME TrapFrame
);
121 IN PKTRAP_FRAME TrapFrame
,
127 IN PKTRAP_FRAME TrapFrame
,
130 /* ROS Internal. Please deprecate */
134 HalReleaseDisplayOwnership();
136 /* FUNCTIONS *****************************************************************/
139 KdbpTrapFrameToKdbTrapFrame(
140 PKTRAP_FRAME TrapFrame
,
141 PKDB_KTRAP_FRAME KdbTrapFrame
)
143 ULONG TrapCr0
, TrapCr2
, TrapCr3
, TrapCr4
;
145 /* Copy the TrapFrame only up to Eflags and zero the rest*/
146 RtlCopyMemory(&KdbTrapFrame
->Tf
, TrapFrame
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
147 RtlZeroMemory((PVOID
)((ULONG_PTR
)&KdbTrapFrame
->Tf
+ FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
)),
148 sizeof(KTRAP_FRAME
) - FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
152 "movl %%cr0, %0" "\n\t"
153 "movl %%cr2, %1" "\n\t"
154 "movl %%cr3, %2" "\n\t"
155 "movl %%cr4, %3" "\n\t"
156 : "=r"(TrapCr0
), "=r"(TrapCr2
),
157 "=r"(TrapCr3
), "=r"(TrapCr4
));
169 /* FIXME: What's the problem with cr4? */
175 KdbTrapFrame
->Cr0
= TrapCr0
;
176 KdbTrapFrame
->Cr2
= TrapCr2
;
177 KdbTrapFrame
->Cr3
= TrapCr3
;
178 KdbTrapFrame
->Cr4
= TrapCr4
;
180 KdbTrapFrame
->Tf
.HardwareEsp
= KiEspFromTrapFrame(TrapFrame
);
181 KdbTrapFrame
->Tf
.HardwareSegSs
= (USHORT
)(KiSsFromTrapFrame(TrapFrame
) & 0xFFFF);
184 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
188 KdbpKdbTrapFrameToTrapFrame(
189 PKDB_KTRAP_FRAME KdbTrapFrame
,
190 PKTRAP_FRAME TrapFrame
)
192 /* Copy the TrapFrame only up to Eflags and zero the rest*/
193 RtlCopyMemory(TrapFrame
, &KdbTrapFrame
->Tf
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
195 /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */
197 KiSsToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareSegSs
);
198 KiEspToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareEsp
);
200 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
204 KdbpKdbTrapFrameFromKernelStack(
206 PKDB_KTRAP_FRAME KdbTrapFrame
)
210 RtlZeroMemory(KdbTrapFrame
, sizeof(KDB_KTRAP_FRAME
));
211 StackPtr
= (ULONG_PTR
*) KernelStack
;
213 KdbTrapFrame
->Tf
.Ebp
= StackPtr
[3];
214 KdbTrapFrame
->Tf
.Edi
= StackPtr
[4];
215 KdbTrapFrame
->Tf
.Esi
= StackPtr
[5];
216 KdbTrapFrame
->Tf
.Ebx
= StackPtr
[6];
217 KdbTrapFrame
->Tf
.Eip
= StackPtr
[7];
218 KdbTrapFrame
->Tf
.HardwareEsp
= (ULONG
) (StackPtr
+ 8);
219 KdbTrapFrame
->Tf
.HardwareSegSs
= KGDT_R0_DATA
;
220 KdbTrapFrame
->Tf
.SegCs
= KGDT_R0_CODE
;
221 KdbTrapFrame
->Tf
.SegDs
= KGDT_R0_DATA
;
222 KdbTrapFrame
->Tf
.SegEs
= KGDT_R0_DATA
;
223 KdbTrapFrame
->Tf
.SegGs
= KGDT_R0_DATA
;
226 /* FIXME: what about the other registers??? */
229 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
230 * the old instruction in *OldInst.
232 * \param Process Process in which's context to overwrite the instruction.
233 * \param Address Address at which to overwrite the instruction.
234 * \param NewInst New instruction (written to \a Address)
235 * \param OldInst Old instruction (read from \a Address)
240 KdbpOverwriteInstruction(
241 IN PEPROCESS Process
,
242 IN ULONG_PTR Address
,
244 OUT PUCHAR OldInst OPTIONAL
)
248 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
251 /* Get the protection for the address. */
252 Protect
= MmGetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
254 /* Return if that page isn't present. */
255 if (Protect
& PAGE_NOACCESS
)
257 return STATUS_MEMORY_NOT_ALLOCATED
;
260 /* Attach to the process */
261 if (CurrentProcess
!= Process
)
263 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
266 /* Make the page writeable if it is read only. */
267 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
269 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
),
270 (Protect
& ~(PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
)) | PAGE_READWRITE
);
273 /* Copy the old instruction back to the caller. */
276 Status
= KdbpSafeReadMemory(OldInst
, (PUCHAR
)Address
, 1);
277 if (!NT_SUCCESS(Status
))
279 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
281 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
284 /* Detach from process */
285 if (CurrentProcess
!= Process
)
294 /* Copy the new instruction in its place. */
295 Status
= KdbpSafeWriteMemory((PUCHAR
)Address
, &NewInst
, 1);
297 /* Restore the page protection. */
298 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
300 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
303 /* Detach from process */
304 if (CurrentProcess
!= Process
)
306 KeUnstackDetachProcess(&ApcState
);
312 /*!\brief Checks whether the given instruction can be single stepped or has to be
313 * stepped over using a temporary breakpoint.
315 * \retval TRUE Instruction is a call.
316 * \retval FALSE Instruction is not a call.
319 KdbpShouldStepOverInstruction(
325 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
327 KdbpPrint("Couldn't access memory at 0x%p\n", Eip
);
331 /* Check if the current instruction is a call. */
332 while ((i
< sizeof (Mem
)) && (Mem
[i
] == 0x66 || Mem
[i
] == 0x67))
335 if (i
== sizeof (Mem
))
338 if (Mem
[i
] == 0xE8 || Mem
[i
] == 0x9A || Mem
[i
] == 0xF2 || Mem
[i
] == 0xF3 ||
339 (((i
+ 1) < sizeof (Mem
)) && Mem
[i
] == 0xFF && (Mem
[i
+1] & 0x38) == 0x10))
347 /*!\brief Steps over an instruction
349 * If the given instruction should be stepped over, this function inserts a
350 * temporary breakpoint after the instruction and returns TRUE, otherwise it
353 * \retval TRUE Temporary breakpoint set after instruction.
354 * \retval FALSE No breakpoint was set.
357 KdbpStepOverInstruction(
362 if (!KdbpShouldStepOverInstruction(Eip
))
365 InstLen
= KdbpGetInstLength(Eip
);
369 if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip
+ InstLen
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
375 /*!\brief Steps into an instruction (interrupts)
377 * If the given instruction should be stepped into, this function inserts a
378 * temporary breakpoint at the target instruction and returns TRUE, otherwise it
381 * \retval TRUE Temporary breakpoint set at target instruction.
382 * \retval FALSE No breakpoint was set.
385 KdbpStepIntoInstruction(
388 KDESCRIPTOR Idtr
= {0};
395 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
397 /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
401 /* Check for INT instruction */
402 /* FIXME: Check for iret */
405 else if (Mem
[0] == 0xcd)
407 else if (Mem
[0] == 0xce && KdbCurrentTrapFrame
->Tf
.EFlags
& (1<<11)) /* 1 << 11 is the overflow flag */
412 if (IntVect
< 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
417 /* Read the interrupt descriptor table register */
419 if (IntVect
>= (Idtr
.Limit
+ 1) / 8)
421 /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
425 /* Get the interrupt descriptor */
426 if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc
, (PVOID
)(ULONG_PTR
)(Idtr
.Base
+ (IntVect
* 8)), sizeof (IntDesc
))))
428 /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
432 /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
433 if ((IntDesc
[1] & (1 << 15)) == 0) /* not present */
437 if ((IntDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
439 /* FIXME: Task gates not supported */
442 else if (((IntDesc
[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
443 ((IntDesc
[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
445 /* FIXME: Should the segment selector of the interrupt gate be checked? */
446 TargetEip
= (IntDesc
[1] & 0xffff0000) | (IntDesc
[0] & 0x0000ffff);
453 /* Insert breakpoint */
454 if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
460 /*!\brief Gets the number of the next breakpoint >= Start.
462 * \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
464 * \returns Breakpoint number (-1 if no more breakpoints are found)
467 KdbpGetNextBreakPointNr(
468 IN ULONG Start OPTIONAL
)
470 for (; Start
< RTL_NUMBER_OF(KdbBreakPoints
); Start
++)
472 if (KdbBreakPoints
[Start
].Type
!= KdbBreakPointNone
)
479 /*!\brief Returns information of the specified breakpoint.
481 * \param BreakPointNr Number of the breakpoint to return information of.
482 * \param Address Receives the address of the breakpoint.
483 * \param Type Receives the type of the breakpoint (hardware or software)
484 * \param Size Size - for memory breakpoints.
485 * \param AccessType Access type - for hardware breakpoints.
486 * \param DebugReg Debug register - for enabled hardware breakpoints.
487 * \param Enabled Whether the breakpoint is enabled or not.
488 * \param Process The owning process of the breakpoint.
489 * \param ConditionExpression The expression which was given as condition for the bp.
491 * \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
494 KdbpGetBreakPointInfo(
495 IN ULONG BreakPointNr
,
496 OUT ULONG_PTR
*Address OPTIONAL
,
497 OUT KDB_BREAKPOINT_TYPE
*Type OPTIONAL
,
498 OUT UCHAR
*Size OPTIONAL
,
499 OUT KDB_ACCESS_TYPE
*AccessType OPTIONAL
,
500 OUT UCHAR
*DebugReg OPTIONAL
,
501 OUT BOOLEAN
*Enabled OPTIONAL
,
502 OUT BOOLEAN
*Global OPTIONAL
,
503 OUT PEPROCESS
*Process OPTIONAL
,
504 OUT PCHAR
*ConditionExpression OPTIONAL
)
508 if (BreakPointNr
>= RTL_NUMBER_OF(KdbBreakPoints
) ||
509 KdbBreakPoints
[BreakPointNr
].Type
== KdbBreakPointNone
)
514 bp
= KdbBreakPoints
+ BreakPointNr
;
516 *Address
= bp
->Address
;
521 if (bp
->Type
== KdbBreakPointHardware
)
524 *Size
= bp
->Data
.Hw
.Size
;
527 *AccessType
= bp
->Data
.Hw
.AccessType
;
529 if (DebugReg
&& bp
->Enabled
)
530 *DebugReg
= bp
->Data
.Hw
.DebugReg
;
534 *Enabled
= bp
->Enabled
;
537 *Global
= bp
->Global
;
540 *Process
= bp
->Process
;
542 if (ConditionExpression
)
543 *ConditionExpression
= bp
->ConditionExpression
;
548 /*!\brief Inserts a breakpoint into the breakpoint array.
550 * The \a Process of the breakpoint is set to \a KdbCurrentProcess
552 * \param Address Address at which to set the breakpoint.
553 * \param Type Type of breakpoint (hardware or software)
554 * \param Size Size of breakpoint (for hardware/memory breakpoints)
555 * \param AccessType Access type (for hardware breakpoins)
556 * \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
557 * \param Global Wether the breakpoint is global or local to a process.
558 * \param BreakPointNumber Receives the breakpoint number on success
563 KdbpInsertBreakPoint(
564 IN ULONG_PTR Address
,
565 IN KDB_BREAKPOINT_TYPE Type
,
566 IN UCHAR Size OPTIONAL
,
567 IN KDB_ACCESS_TYPE AccessType OPTIONAL
,
568 IN PCHAR ConditionExpression OPTIONAL
,
570 OUT PLONG BreakPointNr OPTIONAL
)
574 PCHAR ConditionExpressionDup
;
578 ASSERT(Type
!= KdbBreakPointNone
);
580 if (Type
== KdbBreakPointHardware
)
582 if ((Address
% Size
) != 0)
584 KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address
, Size
);
585 return STATUS_UNSUCCESSFUL
;
588 if (AccessType
== KdbAccessExec
&& Size
!= 1)
590 KdbpPrint("Size must be 1 for execution breakpoints.\n");
591 return STATUS_UNSUCCESSFUL
;
595 if (KdbBreakPointCount
== KDB_MAXIMUM_BREAKPOINT_COUNT
)
597 return STATUS_UNSUCCESSFUL
;
600 /* Parse conditon expression string and duplicate it */
601 if (ConditionExpression
)
603 Condition
= KdbpRpnParseExpression(ConditionExpression
, &ErrOffset
, ErrMsg
);
607 KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg
, ErrOffset
);
609 KdbpPrint("Couldn't parse expression: %s", ErrMsg
);
611 return STATUS_UNSUCCESSFUL
;
614 i
= strlen(ConditionExpression
) + 1;
615 ConditionExpressionDup
= ExAllocatePoolWithTag(NonPagedPool
, i
, TAG_KDBG
);
616 RtlCopyMemory(ConditionExpressionDup
, ConditionExpression
, i
);
621 ConditionExpressionDup
= NULL
;
624 /* Find unused breakpoint */
625 if (Type
== KdbBreakPointTemporary
)
627 for (i
= RTL_NUMBER_OF(KdbBreakPoints
) - 1; i
>= 0; i
--)
629 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
635 for (i
= 0; i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
); i
++)
637 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
642 ASSERT(i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
));
644 /* Set the breakpoint */
645 ASSERT(KdbCurrentProcess
);
646 KdbBreakPoints
[i
].Type
= Type
;
647 KdbBreakPoints
[i
].Address
= Address
;
648 KdbBreakPoints
[i
].Enabled
= FALSE
;
649 KdbBreakPoints
[i
].Global
= Global
;
650 KdbBreakPoints
[i
].Process
= KdbCurrentProcess
;
651 KdbBreakPoints
[i
].ConditionExpression
= ConditionExpressionDup
;
652 KdbBreakPoints
[i
].Condition
= Condition
;
654 if (Type
== KdbBreakPointHardware
)
656 KdbBreakPoints
[i
].Data
.Hw
.Size
= Size
;
657 KdbBreakPoints
[i
].Data
.Hw
.AccessType
= AccessType
;
660 KdbBreakPointCount
++;
662 if (Type
!= KdbBreakPointTemporary
)
663 KdbpPrint("Breakpoint %d inserted.\n", i
);
665 /* Try to enable the breakpoint */
666 KdbpEnableBreakPoint(i
, NULL
);
668 /* Return the breakpoint number */
672 return STATUS_SUCCESS
;
675 /*!\brief Deletes a breakpoint
677 * \param BreakPointNr Number of the breakpoint to delete. Can be -1
678 * \param BreakPoint Breakpoint to delete. Can be NULL.
680 * \retval TRUE Success.
681 * \retval FALSE Failure (invalid breakpoint number)
684 KdbpDeleteBreakPoint(
685 IN LONG BreakPointNr OPTIONAL
,
686 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
688 if (BreakPointNr
< 0)
691 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
694 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
696 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
702 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
705 if (BreakPoint
->Type
== KdbBreakPointNone
)
707 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
711 if (BreakPoint
->Enabled
&& !KdbpDisableBreakPoint(-1, BreakPoint
))
714 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
715 KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr
);
717 BreakPoint
->Type
= KdbBreakPointNone
;
718 KdbBreakPointCount
--;
723 /*!\brief Checks if the breakpoint was set by the debugger
725 * Tries to find a breakpoint in the breakpoint array which caused
726 * the debug exception to happen.
728 * \param ExpNr Exception Number (1 or 3)
729 * \param TrapFrame Exception trapframe
731 * \returns Breakpoint number, -1 on error.
734 KdbpIsBreakPointOurs(
735 IN NTSTATUS ExceptionCode
,
736 IN PKTRAP_FRAME TrapFrame
)
739 ASSERT(ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
);
741 if (ExceptionCode
== STATUS_BREAKPOINT
) /* Software interrupt */
743 ULONG_PTR BpEip
= (ULONG_PTR
)TrapFrame
->Eip
- 1; /* Get EIP of INT3 instruction */
744 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
746 ASSERT((KdbSwBreakPoints
[i
]->Type
== KdbBreakPointSoftware
||
747 KdbSwBreakPoints
[i
]->Type
== KdbBreakPointTemporary
));
748 ASSERT(KdbSwBreakPoints
[i
]->Enabled
);
750 if (KdbSwBreakPoints
[i
]->Address
== BpEip
)
752 return KdbSwBreakPoints
[i
] - KdbBreakPoints
;
756 else if (ExceptionCode
== STATUS_SINGLE_STEP
) /* Hardware interrupt */
760 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
762 ASSERT(KdbHwBreakPoints
[i
]->Type
== KdbBreakPointHardware
&&
763 KdbHwBreakPoints
[i
]->Enabled
);
764 DebugReg
= KdbHwBreakPoints
[i
]->Data
.Hw
.DebugReg
;
766 if ((TrapFrame
->Dr6
& (1 << DebugReg
)) != 0)
768 return KdbHwBreakPoints
[i
] - KdbBreakPoints
;
776 /*!\brief Enables a breakpoint.
778 * \param BreakPointNr Number of the breakpoint to enable Can be -1.
779 * \param BreakPoint Breakpoint to enable. Can be NULL.
781 * \retval TRUE Success.
782 * \retval FALSE Failure.
784 * \sa KdbpDisableBreakPoint
787 KdbpEnableBreakPoint(
788 IN LONG BreakPointNr OPTIONAL
,
789 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
795 if (BreakPointNr
< 0)
798 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
801 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
803 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
809 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
812 if (BreakPoint
->Type
== KdbBreakPointNone
)
814 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
818 if (BreakPoint
->Enabled
)
820 KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr
);
824 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
825 BreakPoint
->Type
== KdbBreakPointTemporary
)
827 if (KdbSwBreakPointCount
>= KDB_MAXIMUM_SW_BREAKPOINT_COUNT
)
829 KdbpPrint("Maximum number of SW breakpoints (%d) used. "
830 "Disable another breakpoint in order to enable this one.\n",
831 KDB_MAXIMUM_SW_BREAKPOINT_COUNT
);
835 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
836 0xCC, &BreakPoint
->Data
.SavedInstruction
);
837 if (!NT_SUCCESS(Status
))
839 KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint
->Address
);
843 KdbSwBreakPoints
[KdbSwBreakPointCount
++] = BreakPoint
;
847 if (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
)
848 ASSERT(BreakPoint
->Data
.Hw
.Size
== 1);
850 ASSERT((BreakPoint
->Address
% BreakPoint
->Data
.Hw
.Size
) == 0);
852 if (KdbHwBreakPointCount
>= KDB_MAXIMUM_HW_BREAKPOINT_COUNT
)
854 KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
855 "Disable another breakpoint in order to enable this one.\n",
856 KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
861 /* Find unused hw breakpoint */
862 ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT
== 4);
863 for (i
= 0; i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
; i
++)
865 if ((KdbTrapFrame
.Tf
.Dr7
& (0x3 << (i
* 2))) == 0)
869 ASSERT(i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
871 /* Set the breakpoint address. */
875 KdbTrapFrame
.Tf
.Dr0
= BreakPoint
->Address
;
878 KdbTrapFrame
.Tf
.Dr1
= BreakPoint
->Address
;
881 KdbTrapFrame
.Tf
.Dr2
= BreakPoint
->Address
;
884 KdbTrapFrame
.Tf
.Dr3
= BreakPoint
->Address
;
888 /* Enable the global breakpoint */
889 KdbTrapFrame
.Tf
.Dr7
|= (0x2 << (i
* 2));
891 /* Enable the exact match bits. */
892 KdbTrapFrame
.Tf
.Dr7
|= 0x00000300;
894 /* Clear existing state. */
895 KdbTrapFrame
.Tf
.Dr7
&= ~(0xF << (16 + (i
* 4)));
897 /* Set the breakpoint type. */
898 switch (BreakPoint
->Data
.Hw
.AccessType
)
907 case KdbAccessReadWrite
:
916 KdbTrapFrame
.Tf
.Dr7
|= (ul
<< (16 + (i
* 4)));
918 /* Set the breakpoint length. */
919 KdbTrapFrame
.Tf
.Dr7
|= ((BreakPoint
->Data
.Hw
.Size
- 1) << (18 + (i
* 4)));
921 /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
922 if (&KdbTrapFrame
!= KdbCurrentTrapFrame
)
924 KdbCurrentTrapFrame
->Tf
.Dr0
= KdbTrapFrame
.Tf
.Dr0
;
925 KdbCurrentTrapFrame
->Tf
.Dr1
= KdbTrapFrame
.Tf
.Dr1
;
926 KdbCurrentTrapFrame
->Tf
.Dr2
= KdbTrapFrame
.Tf
.Dr2
;
927 KdbCurrentTrapFrame
->Tf
.Dr3
= KdbTrapFrame
.Tf
.Dr3
;
928 KdbCurrentTrapFrame
->Tf
.Dr6
= KdbTrapFrame
.Tf
.Dr6
;
929 KdbCurrentTrapFrame
->Tf
.Dr7
= KdbTrapFrame
.Tf
.Dr7
;
932 BreakPoint
->Data
.Hw
.DebugReg
= i
;
933 KdbHwBreakPoints
[KdbHwBreakPointCount
++] = BreakPoint
;
936 BreakPoint
->Enabled
= TRUE
;
937 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
938 KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr
);
943 /*!\brief Disables a breakpoint.
945 * \param BreakPointNr Number of the breakpoint to disable. Can be -1
946 * \param BreakPoint Breakpoint to disable. Can be NULL.
948 * \retval TRUE Success.
949 * \retval FALSE Failure.
951 * \sa KdbpEnableBreakPoint
954 KdbpDisableBreakPoint(
955 IN LONG BreakPointNr OPTIONAL
,
956 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
961 if (BreakPointNr
< 0)
964 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
967 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
969 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
975 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
978 if (BreakPoint
->Type
== KdbBreakPointNone
)
980 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
984 if (BreakPoint
->Enabled
== FALSE
)
986 KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr
);
990 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
991 BreakPoint
->Type
== KdbBreakPointTemporary
)
993 ASSERT(KdbSwBreakPointCount
> 0);
994 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
995 BreakPoint
->Data
.SavedInstruction
, NULL
);
997 if (!NT_SUCCESS(Status
))
999 KdbpPrint("Couldn't restore original instruction.\n");
1003 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
1005 if (KdbSwBreakPoints
[i
] == BreakPoint
)
1007 KdbSwBreakPoints
[i
] = KdbSwBreakPoints
[--KdbSwBreakPointCount
];
1008 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
1013 if (i
!= MAXULONG
) /* not found */
1018 ASSERT(BreakPoint
->Type
== KdbBreakPointHardware
);
1020 /* Clear the breakpoint. */
1021 KdbTrapFrame
.Tf
.Dr7
&= ~(0x3 << (BreakPoint
->Data
.Hw
.DebugReg
* 2));
1022 if ((KdbTrapFrame
.Tf
.Dr7
& 0xFF) == 0)
1024 /* If no breakpoints are enabled then clear the exact match flags. */
1025 KdbTrapFrame
.Tf
.Dr7
&= 0xFFFFFCFF;
1028 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
1030 if (KdbHwBreakPoints
[i
] == BreakPoint
)
1032 KdbHwBreakPoints
[i
] = KdbHwBreakPoints
[--KdbHwBreakPointCount
];
1033 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
1038 if (i
!= MAXULONG
) /* not found */
1042 BreakPoint
->Enabled
= FALSE
;
1043 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
1044 KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr
);
1049 /*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
1051 * \param ExceptionNr Number of the exception to get condition of.
1052 * \param FirstChance Whether to get first or last chance condition.
1053 * \param Condition Receives the condition setting.
1055 * \retval TRUE Success.
1056 * \retval FALSE Failure (invalid exception nr)
1059 KdbpGetEnterCondition(
1060 IN LONG ExceptionNr
,
1061 IN BOOLEAN FirstChance
,
1062 OUT KDB_ENTER_CONDITION
*Condition
)
1064 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
))
1067 *Condition
= KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1];
1071 /*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
1073 * \param ExceptionNr Number of the exception to set condition of (-1 for all)
1074 * \param FirstChance Whether to set first or last chance condition.
1075 * \param Condition The new condition setting.
1077 * \retval TRUE Success.
1078 * \retval FALSE Failure (invalid exception nr)
1081 KdbpSetEnterCondition(
1082 IN LONG ExceptionNr
,
1083 IN BOOLEAN FirstChance
,
1084 IN KDB_ENTER_CONDITION Condition
)
1086 if (ExceptionNr
< 0)
1088 for (ExceptionNr
= 0; ExceptionNr
< (LONG
)RTL_NUMBER_OF(KdbEnterConditions
); ExceptionNr
++)
1090 if (ExceptionNr
== 1 || ExceptionNr
== 8 ||
1091 ExceptionNr
== 9 || ExceptionNr
== 15) /* Reserved exceptions */
1096 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1101 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
) ||
1102 ExceptionNr
== 1 || ExceptionNr
== 8 || /* Do not allow changing of the debug */
1103 ExceptionNr
== 9 || ExceptionNr
== 15) /* trap or reserved exceptions */
1108 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1114 /*!\brief Switches to another thread context
1116 * \param ThreadId Id of the thread to switch to.
1118 * \retval TRUE Success.
1119 * \retval FALSE Failure (i.e. invalid thread id)
1125 PETHREAD Thread
= NULL
;
1128 /* Get a pointer to the thread */
1129 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &Thread
)))
1131 KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG_PTR
)ThreadId
);
1134 Process
= Thread
->ThreadsProcess
;
1136 if (KeIsExecutingDpc() && Process
!= KdbCurrentProcess
)
1138 KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
1139 ObDereferenceObject(Thread
);
1143 /* Save the current thread's context (if we previously attached to a thread) */
1144 if (KdbCurrentThread
!= KdbOriginalThread
)
1146 ASSERT(KdbCurrentTrapFrame
== &KdbThreadTrapFrame
);
1147 /* Actually, we can't save the context, there's no guarantee that there was a trap frame */
1151 ASSERT(KdbCurrentTrapFrame
== &KdbTrapFrame
);
1154 /* Switch to the thread's context */
1155 if (Thread
!= KdbOriginalThread
)
1157 /* The thread we're attaching to isn't the thread on which we entered
1158 * kdb and so the thread we're attaching to is not running. There
1159 * is no guarantee that it actually has a trap frame. So we have to
1160 * peek directly at the registers which were saved on the stack when the
1161 * thread was preempted in the scheduler */
1162 KdbpKdbTrapFrameFromKernelStack(Thread
->Tcb
.KernelStack
,
1163 &KdbThreadTrapFrame
);
1164 KdbCurrentTrapFrame
= &KdbThreadTrapFrame
;
1166 else /* Switching back to original thread */
1168 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1170 KdbCurrentThread
= Thread
;
1172 /* Attach to the thread's process */
1173 ASSERT(KdbCurrentProcess
== PsGetCurrentProcess());
1174 if (KdbCurrentProcess
!= Process
)
1176 if (KdbCurrentProcess
!= KdbOriginalProcess
) /* detach from previously attached process */
1178 KeUnstackDetachProcess(&KdbApcState
);
1181 if (KdbOriginalProcess
!= Process
)
1183 KeStackAttachProcess(&Process
->Pcb
, &KdbApcState
);
1186 KdbCurrentProcess
= Process
;
1189 ObDereferenceObject(Thread
);
1193 /*!\brief Switches to another process/thread context
1195 * This function switches to the first thread in the specified process.
1197 * \param ProcessId Id of the process to switch to.
1199 * \retval TRUE Success.
1200 * \retval FALSE Failure (i.e. invalid process id)
1203 KdbpAttachToProcess(
1206 PEPROCESS Process
= NULL
;
1210 /* Get a pointer to the process */
1211 if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId
, &Process
)))
1213 KdbpPrint("Invalid process id: 0x%08x\n", (ULONG_PTR
)ProcessId
);
1217 Entry
= Process
->ThreadListHead
.Flink
;
1218 ObDereferenceObject(Process
);
1219 if (Entry
== &KdbCurrentProcess
->ThreadListHead
)
1221 KdbpPrint("No threads in process 0x%p, cannot attach to process!\n", ProcessId
);
1225 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1227 return KdbpAttachToThread(Thread
->Cid
.UniqueThread
);
1230 /*!\brief Calls the main loop ...
1233 KdbpCallMainLoop(VOID
)
1235 KdbpCliMainLoop(KdbEnteredOnSingleStep
);
1238 /*!\brief Internal function to enter KDB.
1240 * Disables interrupts, releases display ownership, ...
1246 PVOID SavedInitialStack
, SavedStackBase
, SavedKernelStack
;
1247 ULONG SavedStackLimit
;
1250 if (KdpDebugMode
.Screen
)
1252 InbvAcquireDisplayOwnership();
1255 /* Call the interface's main loop on a different stack */
1256 Thread
= PsGetCurrentThread();
1257 SavedInitialStack
= Thread
->Tcb
.InitialStack
;
1258 SavedStackBase
= Thread
->Tcb
.StackBase
;
1259 SavedStackLimit
= Thread
->Tcb
.StackLimit
;
1260 SavedKernelStack
= Thread
->Tcb
.KernelStack
;
1261 Thread
->Tcb
.InitialStack
= Thread
->Tcb
.StackBase
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1262 Thread
->Tcb
.StackLimit
= (ULONG_PTR
)KdbStack
;
1263 Thread
->Tcb
.KernelStack
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1265 /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);*/
1267 KdbpStackSwitchAndCall(KdbStack
+ KDB_STACK_SIZE
- sizeof(ULONG
), KdbpCallMainLoop
);
1269 Thread
->Tcb
.InitialStack
= SavedInitialStack
;
1270 Thread
->Tcb
.StackBase
= SavedStackBase
;
1271 Thread
->Tcb
.StackLimit
= SavedStackLimit
;
1272 Thread
->Tcb
.KernelStack
= SavedKernelStack
;
1277 KdbpGetExceptionNumberFromStatus(
1278 IN NTSTATUS ExceptionCode
)
1282 switch (ExceptionCode
)
1284 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1287 case STATUS_SINGLE_STEP
:
1290 case STATUS_BREAKPOINT
:
1293 case STATUS_INTEGER_OVERFLOW
:
1296 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
1299 case STATUS_ILLEGAL_INSTRUCTION
:
1302 case STATUS_FLOAT_INVALID_OPERATION
:
1305 case STATUS_STACK_OVERFLOW
:
1308 case STATUS_ACCESS_VIOLATION
:
1311 case STATUS_DATATYPE_MISALIGNMENT
:
1314 case STATUS_FLOAT_MULTIPLE_TRAPS
:
1319 Ret
= RTL_NUMBER_OF(KdbEnterConditions
) - 1;
1326 /*!\brief KDB Exception filter
1328 * Called by the exception dispatcher.
1330 * \param ExceptionRecord Unused.
1331 * \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
1332 * \param Context Context, IN/OUT parameter.
1333 * \param TrapFrame Exception TrapFrame.
1334 * \param FirstChance TRUE when called before exception frames were serached,
1335 * FALSE for the second call.
1337 * \returns KD_CONTINUE_TYPE
1340 KdbEnterDebuggerException(
1341 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
,
1342 IN KPROCESSOR_MODE PreviousMode
,
1343 IN PCONTEXT Context
,
1344 IN OUT PKTRAP_FRAME TrapFrame
,
1345 IN BOOLEAN FirstChance
)
1347 KDB_ENTER_CONDITION EnterCondition
;
1348 KD_CONTINUE_TYPE ContinueType
= kdHandleException
;
1349 PKDB_BREAKPOINT BreakPoint
;
1352 BOOLEAN Resume
= FALSE
;
1353 BOOLEAN EnterConditionMet
= TRUE
;
1356 NTSTATUS ExceptionCode
;
1358 ExceptionCode
= (ExceptionRecord
? ExceptionRecord
->ExceptionCode
: STATUS_BREAKPOINT
);
1360 KdbCurrentProcess
= PsGetCurrentProcess();
1362 /* Set continue type to kdContinue for single steps and breakpoints */
1363 if (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
)
1364 ContinueType
= kdContinue
;
1366 /* Check if we should handle the exception. */
1367 /* FIXME - won't get all exceptions here :( */
1368 ExpNr
= KdbpGetExceptionNumberFromStatus(ExceptionCode
);
1369 EnterCondition
= KdbEnterConditions
[ExpNr
][FirstChance
? 0 : 1];
1370 if (EnterCondition
== KdbDoNotEnter
||
1371 (EnterCondition
== KdbEnterFromUmode
&& PreviousMode
== KernelMode
) ||
1372 (EnterCondition
== KdbEnterFromKmode
&& PreviousMode
!= KernelMode
))
1374 EnterConditionMet
= FALSE
;
1377 /* If we stopped on one of our breakpoints then let the user know. */
1378 KdbLastBreakPointNr
= -1;
1379 KdbEnteredOnSingleStep
= FALSE
;
1381 if (FirstChance
&& (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
) &&
1382 (KdbLastBreakPointNr
= KdbpIsBreakPointOurs(ExceptionCode
, TrapFrame
)) >= 0)
1384 BreakPoint
= KdbBreakPoints
+ KdbLastBreakPointNr
;
1386 if (ExceptionCode
== STATUS_BREAKPOINT
)
1388 /* ... and restore the original instruction. */
1389 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
,
1390 BreakPoint
->Data
.SavedInstruction
, NULL
)))
1392 KdbpPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
1393 KeBugCheck(0); // FIXME: Proper bugcode!
1397 if ((BreakPoint
->Type
== KdbBreakPointHardware
) &&
1398 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
))
1400 Resume
= TRUE
; /* Set the resume flag when continuing execution */
1404 * When a temporary breakpoint is hit we have to make sure that we are
1405 * in the same context in which it was set, otherwise it could happen
1406 * that another process/thread hits it before and it gets deleted.
1408 else if (BreakPoint
->Type
== KdbBreakPointTemporary
&&
1409 BreakPoint
->Process
== KdbCurrentProcess
)
1411 ASSERT((TrapFrame
->EFlags
& EFLAGS_TF
) == 0);
1413 /* Delete the temporary breakpoint which was used to step over or into the instruction. */
1414 KdbpDeleteBreakPoint(-1, BreakPoint
);
1416 if (--KdbNumSingleSteps
> 0)
1418 if ((KdbSingleStepOver
&& !KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1419 (!KdbSingleStepOver
&& !KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1421 Context
->EFlags
|= EFLAGS_TF
;
1424 goto continue_execution
; /* return */
1427 KdbEnteredOnSingleStep
= TRUE
;
1431 * If we hit a breakpoint set by the debugger we set the single step flag,
1432 * ignore the next single step and reenable the breakpoint.
1434 else if (BreakPoint
->Type
== KdbBreakPointSoftware
||
1435 BreakPoint
->Type
== KdbBreakPointTemporary
)
1437 ASSERT(ExceptionCode
== STATUS_BREAKPOINT
);
1438 Context
->EFlags
|= EFLAGS_TF
;
1439 KdbBreakPointToReenable
= BreakPoint
;
1442 /* Make sure that the breakpoint should be triggered in this context */
1443 if (!BreakPoint
->Global
&& BreakPoint
->Process
!= KdbCurrentProcess
)
1445 goto continue_execution
; /* return */
1448 /* Check if the condition for the breakpoint is met. */
1449 if (BreakPoint
->Condition
)
1451 /* Setup the KDB trap frame */
1452 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1455 if (!KdbpRpnEvaluateParsedExpression(BreakPoint
->Condition
, &KdbTrapFrame
, &ull
, NULL
, NULL
))
1457 /* FIXME: Print warning? */
1459 else if (ull
== 0) /* condition is not met */
1461 goto continue_execution
; /* return */
1465 if (BreakPoint
->Type
== KdbBreakPointSoftware
)
1467 KdbpPrint("Entered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
1468 KdbLastBreakPointNr
, TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
);
1470 else if (BreakPoint
->Type
== KdbBreakPointHardware
)
1472 KdbpPrint("Entered debugger on breakpoint #%d: %s 0x%08x\n",
1473 KdbLastBreakPointNr
,
1474 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessRead
) ? "READ" :
1475 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessWrite
) ? "WRITE" :
1476 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessReadWrite
) ? "RDWR" : "EXEC")),
1477 BreakPoint
->Address
);
1480 else if (ExceptionCode
== STATUS_SINGLE_STEP
)
1482 /* Silently ignore a debugger initiated single step. */
1483 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbBreakPointToReenable
)
1485 /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
1486 BreakPoint
= KdbBreakPointToReenable
;
1487 KdbBreakPointToReenable
= NULL
;
1488 ASSERT(BreakPoint
->Type
== KdbBreakPointSoftware
||
1489 BreakPoint
->Type
== KdbBreakPointTemporary
);
1492 * Reenable the breakpoint we disabled to execute the breakpointed
1495 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
, 0xCC,
1496 &BreakPoint
->Data
.SavedInstruction
)))
1498 KdbpPrint("Warning: Couldn't reenable breakpoint %d\n",
1499 BreakPoint
- KdbBreakPoints
);
1502 /* Unset TF if we are no longer single stepping. */
1503 if (KdbNumSingleSteps
== 0)
1504 Context
->EFlags
&= ~EFLAGS_TF
;
1506 goto continue_execution
; /* return */
1509 /* Check if we expect a single step */
1510 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbNumSingleSteps
> 0)
1512 /*ASSERT((Context->Eflags & EFLAGS_TF) != 0);*/
1513 if (--KdbNumSingleSteps
> 0)
1515 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1516 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1518 Context
->EFlags
&= ~EFLAGS_TF
;
1522 Context
->EFlags
|= EFLAGS_TF
;
1525 goto continue_execution
; /* return */
1529 Context
->EFlags
&= ~EFLAGS_TF
;
1530 KdbEnteredOnSingleStep
= TRUE
;
1535 if (!EnterConditionMet
)
1537 return kdHandleException
;
1540 KdbpPrint("Entered debugger on unexpected debug trap!\n");
1543 else if (ExceptionCode
== STATUS_BREAKPOINT
)
1545 if (KdbInitFileBuffer
)
1547 KdbpCliInterpretInitFile();
1548 EnterConditionMet
= FALSE
;
1550 if (!EnterConditionMet
)
1552 return kdHandleException
;
1555 KdbpPrint("Entered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
1556 TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
- 1);
1560 const CHAR
*ExceptionString
= (ExpNr
< RTL_NUMBER_OF(ExceptionNrToString
)) ?
1561 (ExceptionNrToString
[ExpNr
]) :
1562 ("Unknown/User defined exception");
1564 if (!EnterConditionMet
)
1566 return ContinueType
;
1569 KdbpPrint("Entered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
1570 FirstChance
? "first" : "last", ExceptionCode
, ExceptionString
);
1572 if (ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
1573 ExceptionRecord
&& ExceptionRecord
->NumberParameters
!= 0)
1575 /* FIXME: Add noexec memory stuff */
1579 TrapCr2
= __readcr2();
1581 Err
= TrapFrame
->ErrCode
;
1582 KdbpPrint("Memory at 0x%p could not be %s: ", TrapCr2
, (Err
& (1 << 1)) ? "written" : "read");
1584 if ((Err
& (1 << 0)) == 0)
1586 KdbpPrint("Page not present.\n");
1590 if ((Err
& (1 << 3)) != 0)
1591 KdbpPrint("Reserved bits in page directory set.\n");
1593 KdbpPrint("Page protection violation.\n");
1598 /* Once we enter the debugger we do not expect any more single steps to happen */
1599 KdbNumSingleSteps
= 0;
1601 /* Update the current process pointer */
1602 KdbCurrentProcess
= KdbOriginalProcess
= PsGetCurrentProcess();
1603 KdbCurrentThread
= KdbOriginalThread
= PsGetCurrentThread();
1604 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1606 /* Setup the KDB trap frame */
1607 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1609 /* Enter critical section */
1610 OldEflags
= __readeflags();
1613 /* HACK: Save the current IRQL and pretend we are at passive level,
1614 * although interrupts are off. Needed because KDBG calls pageable code. */
1615 OldIrql
= KeGetCurrentIrql();
1616 KeLowerIrql(PASSIVE_LEVEL
);
1618 /* Exception inside the debugger? Game over. */
1619 if (InterlockedIncrement(&KdbEntryCount
) > 1)
1621 __writeeflags(OldEflags
);
1622 return kdHandleException
;
1625 /* Call the main loop. */
1626 KdbpInternalEnter();
1628 /* Check if we should single step */
1629 if (KdbNumSingleSteps
> 0)
1631 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)) ||
1632 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)))
1634 ASSERT((KdbCurrentTrapFrame
->Tf
.EFlags
& EFLAGS_TF
) == 0);
1635 /*KdbCurrentTrapFrame->Tf.EFlags &= ~EFLAGS_TF;*/
1639 Context
->EFlags
|= EFLAGS_TF
;
1643 /* We can't update the current thread's trapframe 'cause it might not have one */
1645 /* Detach from attached process */
1646 if (KdbCurrentProcess
!= KdbOriginalProcess
)
1648 KeUnstackDetachProcess(&KdbApcState
);
1651 /* Update the exception TrapFrame */
1652 KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame
, TrapFrame
);
1654 /* Decrement the entry count */
1655 InterlockedDecrement(&KdbEntryCount
);
1657 /* HACK: Raise back to old IRWL */
1658 KeRaiseIrql(OldIrql
, &OldIrql
);
1660 /* Leave critical section */
1661 __writeeflags(OldEflags
);
1663 /* Check if user requested a bugcheck */
1664 if (KdbpBugCheckRequested
)
1666 /* Clear the flag and bugcheck the system */
1667 KdbpBugCheckRequested
= FALSE
;
1668 KeBugCheck(MANUALLY_INITIATED_CRASH
);
1672 /* Clear debug status */
1673 if (ExceptionCode
== STATUS_BREAKPOINT
) /* FIXME: Why clear DR6 on INT3? */
1675 /* Set the RF flag so we don't trigger the same breakpoint again. */
1678 TrapFrame
->EFlags
|= EFLAGS_RF
;
1681 /* Clear dr6 status flags. */
1682 TrapFrame
->Dr6
&= ~0x0000e00f;
1684 /* Skip the current instruction */
1688 return ContinueType
;
1693 KdbpGetCommandLineSettings(
1698 while (p1
&& (p2
= strchr(p1
, ' ')))
1702 if (!_strnicmp(p2
, "KDSERIAL", 8))
1705 KdbDebugState
|= KD_DEBUG_KDSERIAL
;
1706 KdpDebugMode
.Serial
= TRUE
;
1708 else if (!_strnicmp(p2
, "KDNOECHO", 8))
1711 KdbDebugState
|= KD_DEBUG_KDNOECHO
;
1713 else if (!_strnicmp(p2
, "FIRSTCHANCE", 11))
1716 KdbpSetEnterCondition(-1, TRUE
, KdbEnterAlways
);
1729 BOOLEAN Result
= TRUE
;
1737 Result
= KdpSafeReadMemory((ULONG_PTR
)Src
, Bytes
, Dest
);
1742 ULONG_PTR Start
, End
, Write
;
1744 for (Start
= (ULONG_PTR
)Src
,
1745 End
= Start
+ Bytes
,
1746 Write
= (ULONG_PTR
)Dest
;
1747 Result
&& (Start
< End
);
1749 if (!KdpSafeReadMemory(Start
, 1, (PVOID
)Write
))
1756 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;
1760 KdbpSafeWriteMemory(
1765 BOOLEAN Result
= TRUE
;
1766 ULONG_PTR Start
, End
, Write
;
1768 for (Start
= (ULONG_PTR
)Src
,
1769 End
= Start
+ Bytes
,
1770 Write
= (ULONG_PTR
)Dest
;
1771 Result
&& (Start
< End
);
1773 if (!KdpSafeWriteMemory(Write
, 1, *((PCHAR
)Start
)))
1776 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;