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 /* FUNCTIONS *****************************************************************/
133 KdbpTrapFrameToKdbTrapFrame(
134 PKTRAP_FRAME TrapFrame
,
135 PKDB_KTRAP_FRAME KdbTrapFrame
)
137 ULONG TrapCr0
, TrapCr2
, TrapCr3
, TrapCr4
;
139 /* Copy the TrapFrame only up to Eflags and zero the rest*/
140 RtlCopyMemory(&KdbTrapFrame
->Tf
, TrapFrame
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
141 RtlZeroMemory((PVOID
)((ULONG_PTR
)&KdbTrapFrame
->Tf
+ FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
)),
142 sizeof(KTRAP_FRAME
) - FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
146 "movl %%cr0, %0" "\n\t"
147 "movl %%cr2, %1" "\n\t"
148 "movl %%cr3, %2" "\n\t"
149 "movl %%cr4, %3" "\n\t"
150 : "=r"(TrapCr0
), "=r"(TrapCr2
),
151 "=r"(TrapCr3
), "=r"(TrapCr4
));
163 /* FIXME: What's the problem with cr4? */
169 KdbTrapFrame
->Cr0
= TrapCr0
;
170 KdbTrapFrame
->Cr2
= TrapCr2
;
171 KdbTrapFrame
->Cr3
= TrapCr3
;
172 KdbTrapFrame
->Cr4
= TrapCr4
;
174 KdbTrapFrame
->Tf
.HardwareEsp
= KiEspFromTrapFrame(TrapFrame
);
175 KdbTrapFrame
->Tf
.HardwareSegSs
= (USHORT
)(KiSsFromTrapFrame(TrapFrame
) & 0xFFFF);
178 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
182 KdbpKdbTrapFrameToTrapFrame(
183 PKDB_KTRAP_FRAME KdbTrapFrame
,
184 PKTRAP_FRAME TrapFrame
)
186 /* Copy the TrapFrame only up to Eflags and zero the rest*/
187 RtlCopyMemory(TrapFrame
, &KdbTrapFrame
->Tf
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
189 /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */
191 KiSsToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareSegSs
);
192 KiEspToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareEsp
);
194 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
198 KdbpKdbTrapFrameFromKernelStack(
200 PKDB_KTRAP_FRAME KdbTrapFrame
)
204 RtlZeroMemory(KdbTrapFrame
, sizeof(KDB_KTRAP_FRAME
));
205 StackPtr
= (ULONG_PTR
*) KernelStack
;
207 KdbTrapFrame
->Tf
.Ebp
= StackPtr
[3];
208 KdbTrapFrame
->Tf
.Edi
= StackPtr
[4];
209 KdbTrapFrame
->Tf
.Esi
= StackPtr
[5];
210 KdbTrapFrame
->Tf
.Ebx
= StackPtr
[6];
211 KdbTrapFrame
->Tf
.Eip
= StackPtr
[7];
212 KdbTrapFrame
->Tf
.HardwareEsp
= (ULONG
) (StackPtr
+ 8);
213 KdbTrapFrame
->Tf
.HardwareSegSs
= KGDT_R0_DATA
;
214 KdbTrapFrame
->Tf
.SegCs
= KGDT_R0_CODE
;
215 KdbTrapFrame
->Tf
.SegDs
= KGDT_R0_DATA
;
216 KdbTrapFrame
->Tf
.SegEs
= KGDT_R0_DATA
;
217 KdbTrapFrame
->Tf
.SegGs
= KGDT_R0_DATA
;
220 /* FIXME: what about the other registers??? */
223 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
224 * the old instruction in *OldInst.
226 * \param Process Process in which's context to overwrite the instruction.
227 * \param Address Address at which to overwrite the instruction.
228 * \param NewInst New instruction (written to \a Address)
229 * \param OldInst Old instruction (read from \a Address)
234 KdbpOverwriteInstruction(
235 IN PEPROCESS Process
,
236 IN ULONG_PTR Address
,
238 OUT PUCHAR OldInst OPTIONAL
)
242 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
245 /* Get the protection for the address. */
246 Protect
= MmGetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
248 /* Return if that page isn't present. */
249 if (Protect
& PAGE_NOACCESS
)
251 return STATUS_MEMORY_NOT_ALLOCATED
;
254 /* Attach to the process */
255 if (CurrentProcess
!= Process
)
257 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
260 /* Make the page writeable if it is read only. */
261 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
263 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
),
264 (Protect
& ~(PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
)) | PAGE_READWRITE
);
267 /* Copy the old instruction back to the caller. */
270 Status
= KdbpSafeReadMemory(OldInst
, (PUCHAR
)Address
, 1);
271 if (!NT_SUCCESS(Status
))
273 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
275 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
278 /* Detach from process */
279 if (CurrentProcess
!= Process
)
288 /* Copy the new instruction in its place. */
289 Status
= KdbpSafeWriteMemory((PUCHAR
)Address
, &NewInst
, 1);
291 /* Restore the page protection. */
292 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
294 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
297 /* Detach from process */
298 if (CurrentProcess
!= Process
)
300 KeUnstackDetachProcess(&ApcState
);
306 /*!\brief Checks whether the given instruction can be single stepped or has to be
307 * stepped over using a temporary breakpoint.
309 * \retval TRUE Instruction is a call.
310 * \retval FALSE Instruction is not a call.
313 KdbpShouldStepOverInstruction(
319 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
321 KdbpPrint("Couldn't access memory at 0x%p\n", Eip
);
325 /* Check if the current instruction is a call. */
326 while ((i
< sizeof (Mem
)) && (Mem
[i
] == 0x66 || Mem
[i
] == 0x67))
329 if (i
== sizeof (Mem
))
332 if (Mem
[i
] == 0xE8 || Mem
[i
] == 0x9A || Mem
[i
] == 0xF2 || Mem
[i
] == 0xF3 ||
333 (((i
+ 1) < sizeof (Mem
)) && Mem
[i
] == 0xFF && (Mem
[i
+1] & 0x38) == 0x10))
341 /*!\brief Steps over an instruction
343 * If the given instruction should be stepped over, this function inserts a
344 * temporary breakpoint after the instruction and returns TRUE, otherwise it
347 * \retval TRUE Temporary breakpoint set after instruction.
348 * \retval FALSE No breakpoint was set.
351 KdbpStepOverInstruction(
356 if (!KdbpShouldStepOverInstruction(Eip
))
359 InstLen
= KdbpGetInstLength(Eip
);
363 if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip
+ InstLen
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
369 /*!\brief Steps into an instruction (interrupts)
371 * If the given instruction should be stepped into, this function inserts a
372 * temporary breakpoint at the target instruction and returns TRUE, otherwise it
375 * \retval TRUE Temporary breakpoint set at target instruction.
376 * \retval FALSE No breakpoint was set.
379 KdbpStepIntoInstruction(
382 KDESCRIPTOR Idtr
= {0};
389 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
391 /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
395 /* Check for INT instruction */
396 /* FIXME: Check for iret */
399 else if (Mem
[0] == 0xcd)
401 else if (Mem
[0] == 0xce && KdbCurrentTrapFrame
->Tf
.EFlags
& (1<<11)) /* 1 << 11 is the overflow flag */
406 if (IntVect
< 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
411 /* Read the interrupt descriptor table register */
413 if (IntVect
>= (Idtr
.Limit
+ 1) / 8)
415 /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
419 /* Get the interrupt descriptor */
420 if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc
, (PVOID
)(ULONG_PTR
)(Idtr
.Base
+ (IntVect
* 8)), sizeof (IntDesc
))))
422 /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
426 /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
427 if ((IntDesc
[1] & (1 << 15)) == 0) /* not present */
431 if ((IntDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
433 /* FIXME: Task gates not supported */
436 else if (((IntDesc
[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
437 ((IntDesc
[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
439 /* FIXME: Should the segment selector of the interrupt gate be checked? */
440 TargetEip
= (IntDesc
[1] & 0xffff0000) | (IntDesc
[0] & 0x0000ffff);
447 /* Insert breakpoint */
448 if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
454 /*!\brief Gets the number of the next breakpoint >= Start.
456 * \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
458 * \returns Breakpoint number (-1 if no more breakpoints are found)
461 KdbpGetNextBreakPointNr(
462 IN ULONG Start OPTIONAL
)
464 for (; Start
< RTL_NUMBER_OF(KdbBreakPoints
); Start
++)
466 if (KdbBreakPoints
[Start
].Type
!= KdbBreakPointNone
)
473 /*!\brief Returns information of the specified breakpoint.
475 * \param BreakPointNr Number of the breakpoint to return information of.
476 * \param Address Receives the address of the breakpoint.
477 * \param Type Receives the type of the breakpoint (hardware or software)
478 * \param Size Size - for memory breakpoints.
479 * \param AccessType Access type - for hardware breakpoints.
480 * \param DebugReg Debug register - for enabled hardware breakpoints.
481 * \param Enabled Whether the breakpoint is enabled or not.
482 * \param Process The owning process of the breakpoint.
483 * \param ConditionExpression The expression which was given as condition for the bp.
485 * \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
488 KdbpGetBreakPointInfo(
489 IN ULONG BreakPointNr
,
490 OUT ULONG_PTR
*Address OPTIONAL
,
491 OUT KDB_BREAKPOINT_TYPE
*Type OPTIONAL
,
492 OUT UCHAR
*Size OPTIONAL
,
493 OUT KDB_ACCESS_TYPE
*AccessType OPTIONAL
,
494 OUT UCHAR
*DebugReg OPTIONAL
,
495 OUT BOOLEAN
*Enabled OPTIONAL
,
496 OUT BOOLEAN
*Global OPTIONAL
,
497 OUT PEPROCESS
*Process OPTIONAL
,
498 OUT PCHAR
*ConditionExpression OPTIONAL
)
502 if (BreakPointNr
>= RTL_NUMBER_OF(KdbBreakPoints
) ||
503 KdbBreakPoints
[BreakPointNr
].Type
== KdbBreakPointNone
)
508 bp
= KdbBreakPoints
+ BreakPointNr
;
510 *Address
= bp
->Address
;
515 if (bp
->Type
== KdbBreakPointHardware
)
518 *Size
= bp
->Data
.Hw
.Size
;
521 *AccessType
= bp
->Data
.Hw
.AccessType
;
523 if (DebugReg
&& bp
->Enabled
)
524 *DebugReg
= bp
->Data
.Hw
.DebugReg
;
528 *Enabled
= bp
->Enabled
;
531 *Global
= bp
->Global
;
534 *Process
= bp
->Process
;
536 if (ConditionExpression
)
537 *ConditionExpression
= bp
->ConditionExpression
;
542 /*!\brief Inserts a breakpoint into the breakpoint array.
544 * The \a Process of the breakpoint is set to \a KdbCurrentProcess
546 * \param Address Address at which to set the breakpoint.
547 * \param Type Type of breakpoint (hardware or software)
548 * \param Size Size of breakpoint (for hardware/memory breakpoints)
549 * \param AccessType Access type (for hardware breakpoins)
550 * \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
551 * \param Global Wether the breakpoint is global or local to a process.
552 * \param BreakPointNumber Receives the breakpoint number on success
557 KdbpInsertBreakPoint(
558 IN ULONG_PTR Address
,
559 IN KDB_BREAKPOINT_TYPE Type
,
560 IN UCHAR Size OPTIONAL
,
561 IN KDB_ACCESS_TYPE AccessType OPTIONAL
,
562 IN PCHAR ConditionExpression OPTIONAL
,
564 OUT PLONG BreakPointNr OPTIONAL
)
568 PCHAR ConditionExpressionDup
;
572 ASSERT(Type
!= KdbBreakPointNone
);
574 if (Type
== KdbBreakPointHardware
)
576 if ((Address
% Size
) != 0)
578 KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address
, Size
);
579 return STATUS_UNSUCCESSFUL
;
582 if (AccessType
== KdbAccessExec
&& Size
!= 1)
584 KdbpPrint("Size must be 1 for execution breakpoints.\n");
585 return STATUS_UNSUCCESSFUL
;
589 if (KdbBreakPointCount
== KDB_MAXIMUM_BREAKPOINT_COUNT
)
591 return STATUS_UNSUCCESSFUL
;
594 /* Parse conditon expression string and duplicate it */
595 if (ConditionExpression
)
597 Condition
= KdbpRpnParseExpression(ConditionExpression
, &ErrOffset
, ErrMsg
);
601 KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg
, ErrOffset
);
603 KdbpPrint("Couldn't parse expression: %s", ErrMsg
);
605 return STATUS_UNSUCCESSFUL
;
608 i
= strlen(ConditionExpression
) + 1;
609 ConditionExpressionDup
= ExAllocatePoolWithTag(NonPagedPool
, i
, TAG_KDBG
);
610 RtlCopyMemory(ConditionExpressionDup
, ConditionExpression
, i
);
615 ConditionExpressionDup
= NULL
;
618 /* Find unused breakpoint */
619 if (Type
== KdbBreakPointTemporary
)
621 for (i
= RTL_NUMBER_OF(KdbBreakPoints
) - 1; i
>= 0; i
--)
623 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
629 for (i
= 0; i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
); i
++)
631 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
636 ASSERT(i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
));
638 /* Set the breakpoint */
639 ASSERT(KdbCurrentProcess
);
640 KdbBreakPoints
[i
].Type
= Type
;
641 KdbBreakPoints
[i
].Address
= Address
;
642 KdbBreakPoints
[i
].Enabled
= FALSE
;
643 KdbBreakPoints
[i
].Global
= Global
;
644 KdbBreakPoints
[i
].Process
= KdbCurrentProcess
;
645 KdbBreakPoints
[i
].ConditionExpression
= ConditionExpressionDup
;
646 KdbBreakPoints
[i
].Condition
= Condition
;
648 if (Type
== KdbBreakPointHardware
)
650 KdbBreakPoints
[i
].Data
.Hw
.Size
= Size
;
651 KdbBreakPoints
[i
].Data
.Hw
.AccessType
= AccessType
;
654 KdbBreakPointCount
++;
656 if (Type
!= KdbBreakPointTemporary
)
657 KdbpPrint("Breakpoint %d inserted.\n", i
);
659 /* Try to enable the breakpoint */
660 KdbpEnableBreakPoint(i
, NULL
);
662 /* Return the breakpoint number */
666 return STATUS_SUCCESS
;
669 /*!\brief Deletes a breakpoint
671 * \param BreakPointNr Number of the breakpoint to delete. Can be -1
672 * \param BreakPoint Breakpoint to delete. Can be NULL.
674 * \retval TRUE Success.
675 * \retval FALSE Failure (invalid breakpoint number)
678 KdbpDeleteBreakPoint(
679 IN LONG BreakPointNr OPTIONAL
,
680 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
682 if (BreakPointNr
< 0)
685 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
688 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
690 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
696 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
699 if (BreakPoint
->Type
== KdbBreakPointNone
)
701 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
705 if (BreakPoint
->Enabled
&& !KdbpDisableBreakPoint(-1, BreakPoint
))
708 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
709 KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr
);
711 BreakPoint
->Type
= KdbBreakPointNone
;
712 KdbBreakPointCount
--;
717 /*!\brief Checks if the breakpoint was set by the debugger
719 * Tries to find a breakpoint in the breakpoint array which caused
720 * the debug exception to happen.
722 * \param ExpNr Exception Number (1 or 3)
723 * \param TrapFrame Exception trapframe
725 * \returns Breakpoint number, -1 on error.
728 KdbpIsBreakPointOurs(
729 IN NTSTATUS ExceptionCode
,
730 IN PKTRAP_FRAME TrapFrame
)
733 ASSERT(ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
);
735 if (ExceptionCode
== STATUS_BREAKPOINT
) /* Software interrupt */
737 ULONG_PTR BpEip
= (ULONG_PTR
)TrapFrame
->Eip
- 1; /* Get EIP of INT3 instruction */
738 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
740 ASSERT((KdbSwBreakPoints
[i
]->Type
== KdbBreakPointSoftware
||
741 KdbSwBreakPoints
[i
]->Type
== KdbBreakPointTemporary
));
742 ASSERT(KdbSwBreakPoints
[i
]->Enabled
);
744 if (KdbSwBreakPoints
[i
]->Address
== BpEip
)
746 return KdbSwBreakPoints
[i
] - KdbBreakPoints
;
750 else if (ExceptionCode
== STATUS_SINGLE_STEP
) /* Hardware interrupt */
754 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
756 ASSERT(KdbHwBreakPoints
[i
]->Type
== KdbBreakPointHardware
&&
757 KdbHwBreakPoints
[i
]->Enabled
);
758 DebugReg
= KdbHwBreakPoints
[i
]->Data
.Hw
.DebugReg
;
760 if ((TrapFrame
->Dr6
& (1 << DebugReg
)) != 0)
762 return KdbHwBreakPoints
[i
] - KdbBreakPoints
;
770 /*!\brief Enables a breakpoint.
772 * \param BreakPointNr Number of the breakpoint to enable Can be -1.
773 * \param BreakPoint Breakpoint to enable. Can be NULL.
775 * \retval TRUE Success.
776 * \retval FALSE Failure.
778 * \sa KdbpDisableBreakPoint
781 KdbpEnableBreakPoint(
782 IN LONG BreakPointNr OPTIONAL
,
783 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
789 if (BreakPointNr
< 0)
792 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
795 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
797 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
803 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
806 if (BreakPoint
->Type
== KdbBreakPointNone
)
808 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
812 if (BreakPoint
->Enabled
)
814 KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr
);
818 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
819 BreakPoint
->Type
== KdbBreakPointTemporary
)
821 if (KdbSwBreakPointCount
>= KDB_MAXIMUM_SW_BREAKPOINT_COUNT
)
823 KdbpPrint("Maximum number of SW breakpoints (%d) used. "
824 "Disable another breakpoint in order to enable this one.\n",
825 KDB_MAXIMUM_SW_BREAKPOINT_COUNT
);
829 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
830 0xCC, &BreakPoint
->Data
.SavedInstruction
);
831 if (!NT_SUCCESS(Status
))
833 KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint
->Address
);
837 KdbSwBreakPoints
[KdbSwBreakPointCount
++] = BreakPoint
;
841 if (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
)
842 ASSERT(BreakPoint
->Data
.Hw
.Size
== 1);
844 ASSERT((BreakPoint
->Address
% BreakPoint
->Data
.Hw
.Size
) == 0);
846 if (KdbHwBreakPointCount
>= KDB_MAXIMUM_HW_BREAKPOINT_COUNT
)
848 KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
849 "Disable another breakpoint in order to enable this one.\n",
850 KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
855 /* Find unused hw breakpoint */
856 ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT
== 4);
857 for (i
= 0; i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
; i
++)
859 if ((KdbTrapFrame
.Tf
.Dr7
& (0x3 << (i
* 2))) == 0)
863 ASSERT(i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
865 /* Set the breakpoint address. */
869 KdbTrapFrame
.Tf
.Dr0
= BreakPoint
->Address
;
872 KdbTrapFrame
.Tf
.Dr1
= BreakPoint
->Address
;
875 KdbTrapFrame
.Tf
.Dr2
= BreakPoint
->Address
;
878 KdbTrapFrame
.Tf
.Dr3
= BreakPoint
->Address
;
882 /* Enable the global breakpoint */
883 KdbTrapFrame
.Tf
.Dr7
|= (0x2 << (i
* 2));
885 /* Enable the exact match bits. */
886 KdbTrapFrame
.Tf
.Dr7
|= 0x00000300;
888 /* Clear existing state. */
889 KdbTrapFrame
.Tf
.Dr7
&= ~(0xF << (16 + (i
* 4)));
891 /* Set the breakpoint type. */
892 switch (BreakPoint
->Data
.Hw
.AccessType
)
901 case KdbAccessReadWrite
:
910 KdbTrapFrame
.Tf
.Dr7
|= (ul
<< (16 + (i
* 4)));
912 /* Set the breakpoint length. */
913 KdbTrapFrame
.Tf
.Dr7
|= ((BreakPoint
->Data
.Hw
.Size
- 1) << (18 + (i
* 4)));
915 /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
916 if (&KdbTrapFrame
!= KdbCurrentTrapFrame
)
918 KdbCurrentTrapFrame
->Tf
.Dr0
= KdbTrapFrame
.Tf
.Dr0
;
919 KdbCurrentTrapFrame
->Tf
.Dr1
= KdbTrapFrame
.Tf
.Dr1
;
920 KdbCurrentTrapFrame
->Tf
.Dr2
= KdbTrapFrame
.Tf
.Dr2
;
921 KdbCurrentTrapFrame
->Tf
.Dr3
= KdbTrapFrame
.Tf
.Dr3
;
922 KdbCurrentTrapFrame
->Tf
.Dr6
= KdbTrapFrame
.Tf
.Dr6
;
923 KdbCurrentTrapFrame
->Tf
.Dr7
= KdbTrapFrame
.Tf
.Dr7
;
926 BreakPoint
->Data
.Hw
.DebugReg
= i
;
927 KdbHwBreakPoints
[KdbHwBreakPointCount
++] = BreakPoint
;
930 BreakPoint
->Enabled
= TRUE
;
931 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
932 KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr
);
937 /*!\brief Disables a breakpoint.
939 * \param BreakPointNr Number of the breakpoint to disable. Can be -1
940 * \param BreakPoint Breakpoint to disable. Can be NULL.
942 * \retval TRUE Success.
943 * \retval FALSE Failure.
945 * \sa KdbpEnableBreakPoint
948 KdbpDisableBreakPoint(
949 IN LONG BreakPointNr OPTIONAL
,
950 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
955 if (BreakPointNr
< 0)
958 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
961 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
963 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
969 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
972 if (BreakPoint
->Type
== KdbBreakPointNone
)
974 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
978 if (BreakPoint
->Enabled
== FALSE
)
980 KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr
);
984 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
985 BreakPoint
->Type
== KdbBreakPointTemporary
)
987 ASSERT(KdbSwBreakPointCount
> 0);
988 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
989 BreakPoint
->Data
.SavedInstruction
, NULL
);
991 if (!NT_SUCCESS(Status
))
993 KdbpPrint("Couldn't restore original instruction.\n");
997 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
999 if (KdbSwBreakPoints
[i
] == BreakPoint
)
1001 KdbSwBreakPoints
[i
] = KdbSwBreakPoints
[--KdbSwBreakPointCount
];
1002 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
1007 if (i
!= MAXULONG
) /* not found */
1012 ASSERT(BreakPoint
->Type
== KdbBreakPointHardware
);
1014 /* Clear the breakpoint. */
1015 KdbTrapFrame
.Tf
.Dr7
&= ~(0x3 << (BreakPoint
->Data
.Hw
.DebugReg
* 2));
1016 if ((KdbTrapFrame
.Tf
.Dr7
& 0xFF) == 0)
1018 /* If no breakpoints are enabled then clear the exact match flags. */
1019 KdbTrapFrame
.Tf
.Dr7
&= 0xFFFFFCFF;
1022 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
1024 if (KdbHwBreakPoints
[i
] == BreakPoint
)
1026 KdbHwBreakPoints
[i
] = KdbHwBreakPoints
[--KdbHwBreakPointCount
];
1027 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
1032 if (i
!= MAXULONG
) /* not found */
1036 BreakPoint
->Enabled
= FALSE
;
1037 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
1038 KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr
);
1043 /*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
1045 * \param ExceptionNr Number of the exception to get condition of.
1046 * \param FirstChance Whether to get first or last chance condition.
1047 * \param Condition Receives the condition setting.
1049 * \retval TRUE Success.
1050 * \retval FALSE Failure (invalid exception nr)
1053 KdbpGetEnterCondition(
1054 IN LONG ExceptionNr
,
1055 IN BOOLEAN FirstChance
,
1056 OUT KDB_ENTER_CONDITION
*Condition
)
1058 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
))
1061 *Condition
= KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1];
1065 /*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
1067 * \param ExceptionNr Number of the exception to set condition of (-1 for all)
1068 * \param FirstChance Whether to set first or last chance condition.
1069 * \param Condition The new condition setting.
1071 * \retval TRUE Success.
1072 * \retval FALSE Failure (invalid exception nr)
1075 KdbpSetEnterCondition(
1076 IN LONG ExceptionNr
,
1077 IN BOOLEAN FirstChance
,
1078 IN KDB_ENTER_CONDITION Condition
)
1080 if (ExceptionNr
< 0)
1082 for (ExceptionNr
= 0; ExceptionNr
< (LONG
)RTL_NUMBER_OF(KdbEnterConditions
); ExceptionNr
++)
1084 if (ExceptionNr
== 1 || ExceptionNr
== 8 ||
1085 ExceptionNr
== 9 || ExceptionNr
== 15) /* Reserved exceptions */
1090 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1095 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
) ||
1096 ExceptionNr
== 1 || ExceptionNr
== 8 || /* Do not allow changing of the debug */
1097 ExceptionNr
== 9 || ExceptionNr
== 15) /* trap or reserved exceptions */
1102 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1108 /*!\brief Switches to another thread context
1110 * \param ThreadId Id of the thread to switch to.
1112 * \retval TRUE Success.
1113 * \retval FALSE Failure (i.e. invalid thread id)
1119 PETHREAD Thread
= NULL
;
1122 /* Get a pointer to the thread */
1123 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &Thread
)))
1125 KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG_PTR
)ThreadId
);
1128 Process
= Thread
->ThreadsProcess
;
1130 if (KeIsExecutingDpc() && Process
!= KdbCurrentProcess
)
1132 KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
1133 ObDereferenceObject(Thread
);
1137 /* Save the current thread's context (if we previously attached to a thread) */
1138 if (KdbCurrentThread
!= KdbOriginalThread
)
1140 ASSERT(KdbCurrentTrapFrame
== &KdbThreadTrapFrame
);
1141 /* Actually, we can't save the context, there's no guarantee that there was a trap frame */
1145 ASSERT(KdbCurrentTrapFrame
== &KdbTrapFrame
);
1148 /* Switch to the thread's context */
1149 if (Thread
!= KdbOriginalThread
)
1151 /* The thread we're attaching to isn't the thread on which we entered
1152 * kdb and so the thread we're attaching to is not running. There
1153 * is no guarantee that it actually has a trap frame. So we have to
1154 * peek directly at the registers which were saved on the stack when the
1155 * thread was preempted in the scheduler */
1156 KdbpKdbTrapFrameFromKernelStack(Thread
->Tcb
.KernelStack
,
1157 &KdbThreadTrapFrame
);
1158 KdbCurrentTrapFrame
= &KdbThreadTrapFrame
;
1160 else /* Switching back to original thread */
1162 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1164 KdbCurrentThread
= Thread
;
1166 /* Attach to the thread's process */
1167 ASSERT(KdbCurrentProcess
== PsGetCurrentProcess());
1168 if (KdbCurrentProcess
!= Process
)
1170 if (KdbCurrentProcess
!= KdbOriginalProcess
) /* detach from previously attached process */
1172 KeUnstackDetachProcess(&KdbApcState
);
1175 if (KdbOriginalProcess
!= Process
)
1177 KeStackAttachProcess(&Process
->Pcb
, &KdbApcState
);
1180 KdbCurrentProcess
= Process
;
1183 ObDereferenceObject(Thread
);
1187 /*!\brief Switches to another process/thread context
1189 * This function switches to the first thread in the specified process.
1191 * \param ProcessId Id of the process to switch to.
1193 * \retval TRUE Success.
1194 * \retval FALSE Failure (i.e. invalid process id)
1197 KdbpAttachToProcess(
1200 PEPROCESS Process
= NULL
;
1204 /* Get a pointer to the process */
1205 if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId
, &Process
)))
1207 KdbpPrint("Invalid process id: 0x%08x\n", (ULONG_PTR
)ProcessId
);
1211 Entry
= Process
->ThreadListHead
.Flink
;
1212 ObDereferenceObject(Process
);
1213 if (Entry
== &KdbCurrentProcess
->ThreadListHead
)
1215 KdbpPrint("No threads in process 0x%p, cannot attach to process!\n", ProcessId
);
1219 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1221 return KdbpAttachToThread(Thread
->Cid
.UniqueThread
);
1224 /*!\brief Calls the main loop ...
1227 KdbpCallMainLoop(VOID
)
1229 KdbpCliMainLoop(KdbEnteredOnSingleStep
);
1232 /*!\brief Internal function to enter KDB.
1234 * Disables interrupts, releases display ownership, ...
1240 PVOID SavedInitialStack
, SavedStackBase
, SavedKernelStack
;
1241 ULONG SavedStackLimit
;
1245 if (KdpDebugMode
.Screen
&&
1246 InbvIsBootDriverInstalled() &&
1247 !InbvCheckDisplayOwnership())
1249 /* Acquire ownership and reset the display */
1250 InbvAcquireDisplayOwnership();
1253 /* Display debugger prompt */
1254 InbvSolidColorFill(0, 0, 639, 479, 0);
1255 InbvSetTextColor(15);
1256 InbvInstallDisplayStringFilter(NULL
);
1257 InbvEnableDisplayString(TRUE
);
1258 InbvSetScrollRegion(0, 0, 639, 479);
1261 /* Call the interface's main loop on a different stack */
1262 Thread
= PsGetCurrentThread();
1263 SavedInitialStack
= Thread
->Tcb
.InitialStack
;
1264 SavedStackBase
= Thread
->Tcb
.StackBase
;
1265 SavedStackLimit
= Thread
->Tcb
.StackLimit
;
1266 SavedKernelStack
= Thread
->Tcb
.KernelStack
;
1267 Thread
->Tcb
.InitialStack
= Thread
->Tcb
.StackBase
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1268 Thread
->Tcb
.StackLimit
= (ULONG_PTR
)KdbStack
;
1269 Thread
->Tcb
.KernelStack
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1271 /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);*/
1273 KdbpStackSwitchAndCall(KdbStack
+ KDB_STACK_SIZE
- sizeof(ULONG
), KdbpCallMainLoop
);
1275 Thread
->Tcb
.InitialStack
= SavedInitialStack
;
1276 Thread
->Tcb
.StackBase
= SavedStackBase
;
1277 Thread
->Tcb
.StackLimit
= SavedStackLimit
;
1278 Thread
->Tcb
.KernelStack
= SavedKernelStack
;
1283 KdbpGetExceptionNumberFromStatus(
1284 IN NTSTATUS ExceptionCode
)
1288 switch (ExceptionCode
)
1290 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1293 case STATUS_SINGLE_STEP
:
1296 case STATUS_BREAKPOINT
:
1299 case STATUS_INTEGER_OVERFLOW
:
1302 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
1305 case STATUS_ILLEGAL_INSTRUCTION
:
1308 case STATUS_FLOAT_INVALID_OPERATION
:
1311 case STATUS_STACK_OVERFLOW
:
1314 case STATUS_ACCESS_VIOLATION
:
1317 case STATUS_DATATYPE_MISALIGNMENT
:
1320 case STATUS_FLOAT_MULTIPLE_TRAPS
:
1325 Ret
= RTL_NUMBER_OF(KdbEnterConditions
) - 1;
1332 /*!\brief KDB Exception filter
1334 * Called by the exception dispatcher.
1336 * \param ExceptionRecord Unused.
1337 * \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
1338 * \param Context Context, IN/OUT parameter.
1339 * \param TrapFrame Exception TrapFrame.
1340 * \param FirstChance TRUE when called before exception frames were serached,
1341 * FALSE for the second call.
1343 * \returns KD_CONTINUE_TYPE
1346 KdbEnterDebuggerException(
1347 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
,
1348 IN KPROCESSOR_MODE PreviousMode
,
1349 IN PCONTEXT Context
,
1350 IN OUT PKTRAP_FRAME TrapFrame
,
1351 IN BOOLEAN FirstChance
)
1353 KDB_ENTER_CONDITION EnterCondition
;
1354 KD_CONTINUE_TYPE ContinueType
= kdHandleException
;
1355 PKDB_BREAKPOINT BreakPoint
;
1358 BOOLEAN Resume
= FALSE
;
1359 BOOLEAN EnterConditionMet
= TRUE
;
1362 NTSTATUS ExceptionCode
;
1364 ExceptionCode
= (ExceptionRecord
? ExceptionRecord
->ExceptionCode
: STATUS_BREAKPOINT
);
1366 KdbCurrentProcess
= PsGetCurrentProcess();
1368 /* Set continue type to kdContinue for single steps and breakpoints */
1369 if (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
)
1370 ContinueType
= kdContinue
;
1372 /* Check if we should handle the exception. */
1373 /* FIXME - won't get all exceptions here :( */
1374 ExpNr
= KdbpGetExceptionNumberFromStatus(ExceptionCode
);
1375 EnterCondition
= KdbEnterConditions
[ExpNr
][FirstChance
? 0 : 1];
1376 if (EnterCondition
== KdbDoNotEnter
||
1377 (EnterCondition
== KdbEnterFromUmode
&& PreviousMode
== KernelMode
) ||
1378 (EnterCondition
== KdbEnterFromKmode
&& PreviousMode
!= KernelMode
))
1380 EnterConditionMet
= FALSE
;
1383 /* If we stopped on one of our breakpoints then let the user know. */
1384 KdbLastBreakPointNr
= -1;
1385 KdbEnteredOnSingleStep
= FALSE
;
1387 if (FirstChance
&& (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
) &&
1388 (KdbLastBreakPointNr
= KdbpIsBreakPointOurs(ExceptionCode
, TrapFrame
)) >= 0)
1390 BreakPoint
= KdbBreakPoints
+ KdbLastBreakPointNr
;
1392 if (ExceptionCode
== STATUS_BREAKPOINT
)
1394 /* ... and restore the original instruction. */
1395 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
,
1396 BreakPoint
->Data
.SavedInstruction
, NULL
)))
1398 KdbpPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
1399 KeBugCheck(0); // FIXME: Proper bugcode!
1403 if ((BreakPoint
->Type
== KdbBreakPointHardware
) &&
1404 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
))
1406 Resume
= TRUE
; /* Set the resume flag when continuing execution */
1410 * When a temporary breakpoint is hit we have to make sure that we are
1411 * in the same context in which it was set, otherwise it could happen
1412 * that another process/thread hits it before and it gets deleted.
1414 else if (BreakPoint
->Type
== KdbBreakPointTemporary
&&
1415 BreakPoint
->Process
== KdbCurrentProcess
)
1417 ASSERT((TrapFrame
->EFlags
& EFLAGS_TF
) == 0);
1419 /* Delete the temporary breakpoint which was used to step over or into the instruction. */
1420 KdbpDeleteBreakPoint(-1, BreakPoint
);
1424 if (--KdbNumSingleSteps
> 0)
1426 if ((KdbSingleStepOver
&& !KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1427 (!KdbSingleStepOver
&& !KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1429 Context
->EFlags
|= EFLAGS_TF
;
1432 goto continue_execution
; /* return */
1435 KdbEnteredOnSingleStep
= TRUE
;
1439 * If we hit a breakpoint set by the debugger we set the single step flag,
1440 * ignore the next single step and reenable the breakpoint.
1442 else if (BreakPoint
->Type
== KdbBreakPointSoftware
||
1443 BreakPoint
->Type
== KdbBreakPointTemporary
)
1445 ASSERT(ExceptionCode
== STATUS_BREAKPOINT
);
1446 Context
->EFlags
|= EFLAGS_TF
;
1447 KdbBreakPointToReenable
= BreakPoint
;
1450 /* Make sure that the breakpoint should be triggered in this context */
1451 if (!BreakPoint
->Global
&& BreakPoint
->Process
!= KdbCurrentProcess
)
1453 goto continue_execution
; /* return */
1456 /* Check if the condition for the breakpoint is met. */
1457 if (BreakPoint
->Condition
)
1459 /* Setup the KDB trap frame */
1460 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1463 if (!KdbpRpnEvaluateParsedExpression(BreakPoint
->Condition
, &KdbTrapFrame
, &ull
, NULL
, NULL
))
1465 /* FIXME: Print warning? */
1467 else if (ull
== 0) /* condition is not met */
1469 goto continue_execution
; /* return */
1473 if (BreakPoint
->Type
== KdbBreakPointSoftware
)
1475 KdbpPrint("\nEntered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
1476 KdbLastBreakPointNr
, TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
);
1478 else if (BreakPoint
->Type
== KdbBreakPointHardware
)
1480 KdbpPrint("\nEntered debugger on breakpoint #%d: %s 0x%08x\n",
1481 KdbLastBreakPointNr
,
1482 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessRead
) ? "READ" :
1483 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessWrite
) ? "WRITE" :
1484 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessReadWrite
) ? "RDWR" : "EXEC")),
1485 BreakPoint
->Address
);
1488 else if (ExceptionCode
== STATUS_SINGLE_STEP
)
1490 /* Silently ignore a debugger initiated single step. */
1491 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbBreakPointToReenable
)
1493 /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
1494 BreakPoint
= KdbBreakPointToReenable
;
1495 KdbBreakPointToReenable
= NULL
;
1496 ASSERT(BreakPoint
->Type
== KdbBreakPointSoftware
||
1497 BreakPoint
->Type
== KdbBreakPointTemporary
);
1500 * Reenable the breakpoint we disabled to execute the breakpointed
1503 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
, 0xCC,
1504 &BreakPoint
->Data
.SavedInstruction
)))
1506 KdbpPrint("Warning: Couldn't reenable breakpoint %d\n",
1507 BreakPoint
- KdbBreakPoints
);
1510 /* Unset TF if we are no longer single stepping. */
1511 if (KdbNumSingleSteps
== 0)
1512 Context
->EFlags
&= ~EFLAGS_TF
;
1514 goto continue_execution
; /* return */
1517 /* Check if we expect a single step */
1518 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbNumSingleSteps
> 0)
1520 /*ASSERT((Context->Eflags & EFLAGS_TF) != 0);*/
1521 if (--KdbNumSingleSteps
> 0)
1523 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1524 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1526 Context
->EFlags
&= ~EFLAGS_TF
;
1530 Context
->EFlags
|= EFLAGS_TF
;
1533 goto continue_execution
; /* return */
1537 Context
->EFlags
&= ~EFLAGS_TF
;
1538 KdbEnteredOnSingleStep
= TRUE
;
1543 if (!EnterConditionMet
)
1545 return kdHandleException
;
1548 KdbpPrint("\nEntered debugger on unexpected debug trap!\n");
1551 else if (ExceptionCode
== STATUS_BREAKPOINT
)
1553 if (KdbInitFileBuffer
)
1555 KdbpCliInterpretInitFile();
1556 EnterConditionMet
= FALSE
;
1558 if (!EnterConditionMet
)
1560 return kdHandleException
;
1563 KdbpPrint("\nEntered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
1564 TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
- 1);
1568 const CHAR
*ExceptionString
= (ExpNr
< RTL_NUMBER_OF(ExceptionNrToString
)) ?
1569 (ExceptionNrToString
[ExpNr
]) :
1570 ("Unknown/User defined exception");
1572 if (!EnterConditionMet
)
1574 return ContinueType
;
1577 KdbpPrint("\nEntered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
1578 FirstChance
? "first" : "last", ExceptionCode
, ExceptionString
);
1580 if (ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
1581 ExceptionRecord
&& ExceptionRecord
->NumberParameters
!= 0)
1583 /* FIXME: Add noexec memory stuff */
1587 TrapCr2
= __readcr2();
1589 Err
= TrapFrame
->ErrCode
;
1590 KdbpPrint("Memory at 0x%p could not be %s: ", TrapCr2
, (Err
& (1 << 1)) ? "written" : "read");
1592 if ((Err
& (1 << 0)) == 0)
1594 KdbpPrint("Page not present.\n");
1598 if ((Err
& (1 << 3)) != 0)
1599 KdbpPrint("Reserved bits in page directory set.\n");
1601 KdbpPrint("Page protection violation.\n");
1606 /* Once we enter the debugger we do not expect any more single steps to happen */
1607 KdbNumSingleSteps
= 0;
1609 /* Update the current process pointer */
1610 KdbCurrentProcess
= KdbOriginalProcess
= PsGetCurrentProcess();
1611 KdbCurrentThread
= KdbOriginalThread
= PsGetCurrentThread();
1612 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1614 /* Setup the KDB trap frame */
1615 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1617 /* Enter critical section */
1618 OldEflags
= __readeflags();
1621 /* HACK: Save the current IRQL and pretend we are at passive level,
1622 * although interrupts are off. Needed because KDBG calls pageable code. */
1623 OldIrql
= KeGetCurrentIrql();
1624 KeLowerIrql(PASSIVE_LEVEL
);
1626 /* Exception inside the debugger? Game over. */
1627 if (InterlockedIncrement(&KdbEntryCount
) > 1)
1629 __writeeflags(OldEflags
);
1630 return kdHandleException
;
1633 /* Call the main loop. */
1634 KdbpInternalEnter();
1636 /* Check if we should single step */
1637 if (KdbNumSingleSteps
> 0)
1639 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)) ||
1640 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)))
1642 ASSERT((KdbCurrentTrapFrame
->Tf
.EFlags
& EFLAGS_TF
) == 0);
1643 /*KdbCurrentTrapFrame->Tf.EFlags &= ~EFLAGS_TF;*/
1647 Context
->EFlags
|= EFLAGS_TF
;
1651 /* We can't update the current thread's trapframe 'cause it might not have one */
1653 /* Detach from attached process */
1654 if (KdbCurrentProcess
!= KdbOriginalProcess
)
1656 KeUnstackDetachProcess(&KdbApcState
);
1659 /* Update the exception TrapFrame */
1660 KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame
, TrapFrame
);
1662 /* Decrement the entry count */
1663 InterlockedDecrement(&KdbEntryCount
);
1665 /* HACK: Raise back to old IRWL */
1666 KeRaiseIrql(OldIrql
, &OldIrql
);
1668 /* Leave critical section */
1669 __writeeflags(OldEflags
);
1671 /* Check if user requested a bugcheck */
1672 if (KdbpBugCheckRequested
)
1674 /* Clear the flag and bugcheck the system */
1675 KdbpBugCheckRequested
= FALSE
;
1676 KeBugCheck(MANUALLY_INITIATED_CRASH
);
1680 /* Clear debug status */
1681 if (ExceptionCode
== STATUS_BREAKPOINT
) /* FIXME: Why clear DR6 on INT3? */
1683 /* Set the RF flag so we don't trigger the same breakpoint again. */
1686 TrapFrame
->EFlags
|= EFLAGS_RF
;
1689 /* Clear dr6 status flags. */
1690 TrapFrame
->Dr6
&= ~0x0000e00f;
1692 if (!(KdbEnteredOnSingleStep
&& KdbSingleStepOver
))
1694 /* Skip the current instruction */
1699 return ContinueType
;
1705 KdbpGetCommandLineSettings(
1708 #define CONST_STR_LEN(x) (sizeof(x)/sizeof(x[0]) - 1)
1710 while (p1
&& (p1
= strchr(p1
, ' ')))
1712 /* Skip other spaces */
1713 while (*p1
== ' ') ++p1
;
1715 if (!_strnicmp(p1
, "KDSERIAL", CONST_STR_LEN("KDSERIAL")))
1717 p1
+= CONST_STR_LEN("KDSERIAL");
1718 KdbDebugState
|= KD_DEBUG_KDSERIAL
;
1719 KdpDebugMode
.Serial
= TRUE
;
1721 else if (!_strnicmp(p1
, "KDNOECHO", CONST_STR_LEN("KDNOECHO")))
1723 p1
+= CONST_STR_LEN("KDNOECHO");
1724 KdbDebugState
|= KD_DEBUG_KDNOECHO
;
1726 else if (!_strnicmp(p1
, "FIRSTCHANCE", CONST_STR_LEN("FIRSTCHANCE")))
1728 p1
+= CONST_STR_LEN("FIRSTCHANCE");
1729 KdbpSetEnterCondition(-1, TRUE
, KdbEnterAlways
);
1740 BOOLEAN Result
= TRUE
;
1748 Result
= KdpSafeReadMemory((ULONG_PTR
)Src
, Bytes
, Dest
);
1753 ULONG_PTR Start
, End
, Write
;
1755 for (Start
= (ULONG_PTR
)Src
,
1756 End
= Start
+ Bytes
,
1757 Write
= (ULONG_PTR
)Dest
;
1758 Result
&& (Start
< End
);
1760 if (!KdpSafeReadMemory(Start
, 1, (PVOID
)Write
))
1767 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;
1771 KdbpSafeWriteMemory(
1776 BOOLEAN Result
= TRUE
;
1777 ULONG_PTR Start
, End
, Write
;
1779 for (Start
= (ULONG_PTR
)Src
,
1780 End
= Start
+ Bytes
,
1781 Write
= (ULONG_PTR
)Dest
;
1782 Result
&& (Start
< End
);
1784 if (!KdpSafeWriteMemory(Write
, 1, *((PCHAR
)Start
)))
1787 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;