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",
110 KiSsFromTrapFrame(IN PKTRAP_FRAME TrapFrame
);
114 KiEspFromTrapFrame(IN PKTRAP_FRAME TrapFrame
);
118 KiSsToTrapFrame(IN PKTRAP_FRAME TrapFrame
,
123 KiEspToTrapFrame(IN PKTRAP_FRAME TrapFrame
,
126 /* ROS Internal. Please deprecate */
130 HalReleaseDisplayOwnership(
134 /* FUNCTIONS *****************************************************************/
137 KdbpTrapFrameToKdbTrapFrame(PKTRAP_FRAME TrapFrame
, PKDB_KTRAP_FRAME KdbTrapFrame
)
139 ULONG TrapCr0
, TrapCr2
, TrapCr3
, TrapCr4
;
141 /* Copy the TrapFrame only up to Eflags and zero the rest*/
142 RtlCopyMemory(&KdbTrapFrame
->Tf
, TrapFrame
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
143 RtlZeroMemory((PVOID
)((ULONG_PTR
)&KdbTrapFrame
->Tf
+ FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
)),
144 sizeof (KTRAP_FRAME
) - FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
148 "movl %%cr0, %0" "\n\t"
149 "movl %%cr2, %1" "\n\t"
150 "movl %%cr3, %2" "\n\t"
151 "movl %%cr4, %3" "\n\t"
152 : "=r"(TrapCr0
), "=r"(TrapCr2
),
153 "=r"(TrapCr3
), "=r"(TrapCr4
));
165 /* FIXME: What's the problem with cr4? */
171 KdbTrapFrame
->Cr0
= TrapCr0
;
172 KdbTrapFrame
->Cr2
= TrapCr2
;
173 KdbTrapFrame
->Cr3
= TrapCr3
;
174 KdbTrapFrame
->Cr4
= TrapCr4
;
176 KdbTrapFrame
->Tf
.HardwareEsp
= KiEspFromTrapFrame(TrapFrame
);
177 KdbTrapFrame
->Tf
.HardwareSegSs
= (USHORT
)(KiSsFromTrapFrame(TrapFrame
) & 0xFFFF);
180 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
184 KdbpKdbTrapFrameToTrapFrame(PKDB_KTRAP_FRAME KdbTrapFrame
, 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(PVOID KernelStack
,
199 PKDB_KTRAP_FRAME KdbTrapFrame
)
203 RtlZeroMemory(KdbTrapFrame
, sizeof(KDB_KTRAP_FRAME
));
204 StackPtr
= (ULONG_PTR
*) KernelStack
;
205 KdbTrapFrame
->Tf
.Ebp
= StackPtr
[3];
206 KdbTrapFrame
->Tf
.Edi
= StackPtr
[4];
207 KdbTrapFrame
->Tf
.Esi
= StackPtr
[5];
208 KdbTrapFrame
->Tf
.Ebx
= StackPtr
[6];
209 KdbTrapFrame
->Tf
.Eip
= StackPtr
[7];
210 KdbTrapFrame
->Tf
.HardwareEsp
= (ULONG
) (StackPtr
+ 8);
211 KdbTrapFrame
->Tf
.HardwareSegSs
= KGDT_R0_DATA
;
212 KdbTrapFrame
->Tf
.SegCs
= KGDT_R0_CODE
;
213 KdbTrapFrame
->Tf
.SegDs
= KGDT_R0_DATA
;
214 KdbTrapFrame
->Tf
.SegEs
= KGDT_R0_DATA
;
215 KdbTrapFrame
->Tf
.SegGs
= KGDT_R0_DATA
;
217 /* FIXME: what about the other registers??? */
220 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
221 * the old instruction in *OldInst.
223 * \param Process Process in which's context to overwrite the instruction.
224 * \param Address Address at which to overwrite the instruction.
225 * \param NewInst New instruction (written to \a Address)
226 * \param OldInst Old instruction (read from \a Address)
231 KdbpOverwriteInstruction(
232 IN PEPROCESS Process
,
233 IN ULONG_PTR Address
,
235 OUT PUCHAR OldInst OPTIONAL
)
239 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
242 /* Get the protection for the address. */
243 Protect
= MmGetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
245 /* Return if that page isn't present. */
246 if (Protect
& PAGE_NOACCESS
)
248 return STATUS_MEMORY_NOT_ALLOCATED
;
251 /* Attach to the process */
252 if (CurrentProcess
!= Process
)
254 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
257 /* Make the page writeable if it is read only. */
258 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
260 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
),
261 (Protect
& ~(PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
)) | PAGE_READWRITE
);
264 /* Copy the old instruction back to the caller. */
267 Status
= KdbpSafeReadMemory(OldInst
, (PUCHAR
)Address
, 1);
268 if (!NT_SUCCESS(Status
))
270 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
272 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
274 /* Detach from process */
275 if (CurrentProcess
!= Process
)
283 /* Copy the new instruction in its place. */
284 Status
= KdbpSafeWriteMemory((PUCHAR
)Address
, &NewInst
, 1);
286 /* Restore the page protection. */
287 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
289 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
292 /* Detach from process */
293 if (CurrentProcess
!= Process
)
295 KeUnstackDetachProcess(&ApcState
);
301 /*!\brief Checks whether the given instruction can be single stepped or has to be
302 * stepped over using a temporary breakpoint.
304 * \retval TRUE Instruction is a call.
305 * \retval FALSE Instruction is not a call.
308 KdbpShouldStepOverInstruction(ULONG_PTR Eip
)
313 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
315 KdbpPrint("Couldn't access memory at 0x%p\n", Eip
);
319 /* Check if the current instruction is a call. */
320 while ((i
< sizeof (Mem
)) && (Mem
[i
] == 0x66 || Mem
[i
] == 0x67))
322 if (i
== sizeof (Mem
))
324 if (Mem
[i
] == 0xE8 || Mem
[i
] == 0x9A || Mem
[i
] == 0xF2 || Mem
[i
] == 0xF3 ||
325 (((i
+ 1) < sizeof (Mem
)) && Mem
[i
] == 0xFF && (Mem
[i
+1] & 0x38) == 0x10))
332 /*!\brief Steps over an instruction
334 * If the given instruction should be stepped over, this function inserts a
335 * temporary breakpoint after the instruction and returns TRUE, otherwise it
338 * \retval TRUE Temporary breakpoint set after instruction.
339 * \retval FALSE No breakpoint was set.
342 KdbpStepOverInstruction(ULONG_PTR Eip
)
346 if (!KdbpShouldStepOverInstruction(Eip
))
349 InstLen
= KdbpGetInstLength(Eip
);
353 if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip
+ InstLen
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
359 /*!\brief Steps into an instruction (interrupts)
361 * If the given instruction should be stepped into, this function inserts a
362 * temporary breakpoint at the target instruction and returns TRUE, otherwise it
365 * \retval TRUE Temporary breakpoint set at target instruction.
366 * \retval FALSE No breakpoint was set.
369 KdbpStepIntoInstruction(ULONG_PTR Eip
)
371 KDESCRIPTOR Idtr
= {0};
378 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
380 /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
384 /* Check for INT instruction */
385 /* FIXME: Check for iret */
388 else if (Mem
[0] == 0xcd)
390 else if (Mem
[0] == 0xce && KdbCurrentTrapFrame
->Tf
.EFlags
& (1<<11)) /* 1 << 11 is the overflow flag */
395 if (IntVect
< 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
400 /* Read the interrupt descriptor table register */
401 Ke386GetInterruptDescriptorTable(*(PKDESCRIPTOR
)&Idtr
.Limit
);
402 if (IntVect
>= (Idtr
.Limit
+ 1) / 8)
404 /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
408 /* Get the interrupt descriptor */
409 if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc
, (PVOID
)(Idtr
.Base
+ (IntVect
* 8)), sizeof (IntDesc
))))
411 /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
415 /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
416 if ((IntDesc
[1] & (1 << 15)) == 0) /* not present */
420 if ((IntDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
422 /* FIXME: Task gates not supported */
425 else if (((IntDesc
[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
426 ((IntDesc
[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
428 /* FIXME: Should the segment selector of the interrupt gate be checked? */
429 TargetEip
= (IntDesc
[1] & 0xffff0000) | (IntDesc
[0] & 0x0000ffff);
436 /* Insert breakpoint */
437 if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
443 /*!\brief Gets the number of the next breakpoint >= Start.
445 * \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
447 * \returns Breakpoint number (-1 if no more breakpoints are found)
450 KdbpGetNextBreakPointNr(
451 IN ULONG Start OPTIONAL
)
453 for (; Start
< RTL_NUMBER_OF(KdbBreakPoints
); Start
++)
455 if (KdbBreakPoints
[Start
].Type
!= KdbBreakPointNone
)
461 /*!\brief Returns information of the specified breakpoint.
463 * \param BreakPointNr Number of the breakpoint to return information of.
464 * \param Address Receives the address of the breakpoint.
465 * \param Type Receives the type of the breakpoint (hardware or software)
466 * \param Size Size - for memory breakpoints.
467 * \param AccessType Access type - for hardware breakpoints.
468 * \param DebugReg Debug register - for enabled hardware breakpoints.
469 * \param Enabled Whether the breakpoint is enabled or not.
470 * \param Process The owning process of the breakpoint.
471 * \param ConditionExpression The expression which was given as condition for the bp.
473 * \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
476 KdbpGetBreakPointInfo(
477 IN ULONG BreakPointNr
,
478 OUT ULONG_PTR
*Address OPTIONAL
,
479 OUT KDB_BREAKPOINT_TYPE
*Type OPTIONAL
,
480 OUT UCHAR
*Size OPTIONAL
,
481 OUT KDB_ACCESS_TYPE
*AccessType OPTIONAL
,
482 OUT UCHAR
*DebugReg OPTIONAL
,
483 OUT BOOLEAN
*Enabled OPTIONAL
,
484 OUT BOOLEAN
*Global OPTIONAL
,
485 OUT PEPROCESS
*Process OPTIONAL
,
486 OUT PCHAR
*ConditionExpression OPTIONAL
)
490 if (BreakPointNr
>= RTL_NUMBER_OF(KdbBreakPoints
) ||
491 KdbBreakPoints
[BreakPointNr
].Type
== KdbBreakPointNone
)
496 bp
= KdbBreakPoints
+ BreakPointNr
;
498 *Address
= bp
->Address
;
501 if (bp
->Type
== KdbBreakPointHardware
)
504 *Size
= bp
->Data
.Hw
.Size
;
505 if (AccessType
!= NULL
)
506 *AccessType
= bp
->Data
.Hw
.AccessType
;
507 if (DebugReg
!= NULL
&& bp
->Enabled
)
508 *DebugReg
= bp
->Data
.Hw
.DebugReg
;
511 *Enabled
= bp
->Enabled
;
513 *Global
= bp
->Global
;
515 *Process
= bp
->Process
;
516 if (ConditionExpression
!= NULL
)
517 *ConditionExpression
= bp
->ConditionExpression
;
522 /*!\brief Inserts a breakpoint into the breakpoint array.
524 * The \a Process of the breakpoint is set to \a KdbCurrentProcess
526 * \param Address Address at which to set the breakpoint.
527 * \param Type Type of breakpoint (hardware or software)
528 * \param Size Size of breakpoint (for hardware/memory breakpoints)
529 * \param AccessType Access type (for hardware breakpoins)
530 * \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
531 * \param Global Wether the breakpoint is global or local to a process.
532 * \param BreakPointNumber Receives the breakpoint number on success
537 KdbpInsertBreakPoint(
538 IN ULONG_PTR Address
,
539 IN KDB_BREAKPOINT_TYPE Type
,
540 IN UCHAR Size OPTIONAL
,
541 IN KDB_ACCESS_TYPE AccessType OPTIONAL
,
542 IN PCHAR ConditionExpression OPTIONAL
,
544 OUT PLONG BreakPointNr OPTIONAL
)
548 PCHAR ConditionExpressionDup
;
552 ASSERT(Type
!= KdbBreakPointNone
);
554 if (Type
== KdbBreakPointHardware
)
556 if ((Address
% Size
) != 0)
558 KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address
, Size
);
559 return STATUS_UNSUCCESSFUL
;
561 if (AccessType
== KdbAccessExec
&& Size
!= 1)
563 KdbpPrint("Size must be 1 for execution breakpoints.\n");
564 return STATUS_UNSUCCESSFUL
;
568 if (KdbBreakPointCount
== KDB_MAXIMUM_BREAKPOINT_COUNT
)
570 return STATUS_UNSUCCESSFUL
;
573 /* Parse conditon expression string and duplicate it */
574 if (ConditionExpression
!= NULL
)
576 Condition
= KdbpRpnParseExpression(ConditionExpression
, &ErrOffset
, ErrMsg
);
577 if (Condition
== NULL
)
580 KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg
, ErrOffset
);
582 KdbpPrint("Couldn't parse expression: %s", ErrMsg
);
583 return STATUS_UNSUCCESSFUL
;
586 i
= strlen(ConditionExpression
) + 1;
587 ConditionExpressionDup
= ExAllocatePoolWithTag(NonPagedPool
, i
, TAG_KDBG
);
588 RtlCopyMemory(ConditionExpressionDup
, ConditionExpression
, i
);
594 ConditionExpressionDup
= NULL
;
597 /* Find unused breakpoint */
598 if (Type
== KdbBreakPointTemporary
)
600 for (i
= RTL_NUMBER_OF(KdbBreakPoints
) - 1; i
>= 0; i
--)
602 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
608 for (i
= 0; i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
); i
++)
610 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
614 ASSERT(i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
));
616 /* Set the breakpoint */
617 ASSERT(KdbCurrentProcess
!= NULL
);
618 KdbBreakPoints
[i
].Type
= Type
;
619 KdbBreakPoints
[i
].Address
= Address
;
620 KdbBreakPoints
[i
].Enabled
= FALSE
;
621 KdbBreakPoints
[i
].Global
= Global
;
622 KdbBreakPoints
[i
].Process
= KdbCurrentProcess
;
623 KdbBreakPoints
[i
].ConditionExpression
= ConditionExpressionDup
;
624 KdbBreakPoints
[i
].Condition
= Condition
;
625 if (Type
== KdbBreakPointHardware
)
628 KdbBreakPoints
[i
].Data
.Hw
.Size
= Size
;
629 KdbBreakPoints
[i
].Data
.Hw
.AccessType
= AccessType
;
631 KdbBreakPointCount
++;
633 if (Type
!= KdbBreakPointTemporary
)
634 KdbpPrint("Breakpoint %d inserted.\n", i
);
636 /* Try to enable the breakpoint */
637 KdbpEnableBreakPoint(i
, NULL
);
639 /* Return the breakpoint number */
640 if (BreakPointNr
!= NULL
)
643 return STATUS_SUCCESS
;
646 /*!\brief Deletes a breakpoint
648 * \param BreakPointNr Number of the breakpoint to delete. Can be -1
649 * \param BreakPoint Breakpoint to delete. Can be NULL.
651 * \retval TRUE Success.
652 * \retval FALSE Failure (invalid breakpoint number)
655 KdbpDeleteBreakPoint(
656 IN LONG BreakPointNr OPTIONAL
,
657 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
659 if (BreakPointNr
< 0)
661 ASSERT(BreakPoint
!= NULL
);
662 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
664 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
666 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
669 if (BreakPoint
== NULL
)
671 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
673 if (BreakPoint
->Type
== KdbBreakPointNone
)
675 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
679 if (BreakPoint
->Enabled
&&
680 !KdbpDisableBreakPoint(-1, BreakPoint
))
683 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
684 KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr
);
685 BreakPoint
->Type
= KdbBreakPointNone
;
686 KdbBreakPointCount
--;
691 /*!\brief Checks if the breakpoint was set by the debugger
693 * Tries to find a breakpoint in the breakpoint array which caused
694 * the debug exception to happen.
696 * \param ExpNr Exception Number (1 or 3)
697 * \param TrapFrame Exception trapframe
699 * \returns Breakpoint number, -1 on error.
702 KdbpIsBreakPointOurs(
703 IN NTSTATUS ExceptionCode
,
704 IN PKTRAP_FRAME TrapFrame
)
707 ASSERT(ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
);
709 if (ExceptionCode
== STATUS_BREAKPOINT
) /* Software interrupt */
711 ULONG_PTR BpEip
= (ULONG_PTR
)TrapFrame
->Eip
- 1; /* Get EIP of INT3 instruction */
712 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
714 ASSERT((KdbSwBreakPoints
[i
]->Type
== KdbBreakPointSoftware
||
715 KdbSwBreakPoints
[i
]->Type
== KdbBreakPointTemporary
));
716 ASSERT(KdbSwBreakPoints
[i
]->Enabled
);
717 if (KdbSwBreakPoints
[i
]->Address
== BpEip
)
719 return KdbSwBreakPoints
[i
] - KdbBreakPoints
;
723 else if (ExceptionCode
== STATUS_SINGLE_STEP
) /* Hardware interrupt */
726 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
728 ASSERT(KdbHwBreakPoints
[i
]->Type
== KdbBreakPointHardware
&&
729 KdbHwBreakPoints
[i
]->Enabled
);
730 DebugReg
= KdbHwBreakPoints
[i
]->Data
.Hw
.DebugReg
;
731 if ((TrapFrame
->Dr6
& (1 << DebugReg
)) != 0)
733 return KdbHwBreakPoints
[i
] - KdbBreakPoints
;
741 /*!\brief Enables a breakpoint.
743 * \param BreakPointNr Number of the breakpoint to enable Can be -1.
744 * \param BreakPoint Breakpoint to enable. Can be NULL.
746 * \retval TRUE Success.
747 * \retval FALSE Failure.
749 * \sa KdbpDisableBreakPoint
752 KdbpEnableBreakPoint(
753 IN LONG BreakPointNr OPTIONAL
,
754 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
760 if (BreakPointNr
< 0)
762 ASSERT(BreakPoint
!= NULL
);
763 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
765 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
767 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
770 if (BreakPoint
== NULL
)
772 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
774 if (BreakPoint
->Type
== KdbBreakPointNone
)
776 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
780 if (BreakPoint
->Enabled
== TRUE
)
782 KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr
);
786 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
787 BreakPoint
->Type
== KdbBreakPointTemporary
)
789 if (KdbSwBreakPointCount
>= KDB_MAXIMUM_SW_BREAKPOINT_COUNT
)
791 KdbpPrint("Maximum number of SW breakpoints (%d) used. "
792 "Disable another breakpoint in order to enable this one.\n",
793 KDB_MAXIMUM_SW_BREAKPOINT_COUNT
);
796 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
797 0xCC, &BreakPoint
->Data
.SavedInstruction
);
798 if (!NT_SUCCESS(Status
))
800 KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint
->Address
);
803 KdbSwBreakPoints
[KdbSwBreakPointCount
++] = BreakPoint
;
807 if (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
)
808 ASSERT(BreakPoint
->Data
.Hw
.Size
== 1);
809 ASSERT((BreakPoint
->Address
% BreakPoint
->Data
.Hw
.Size
) == 0);
810 if (KdbHwBreakPointCount
>= KDB_MAXIMUM_HW_BREAKPOINT_COUNT
)
812 KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
813 "Disable another breakpoint in order to enable this one.\n",
814 KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
818 /* Find unused hw breakpoint */
819 ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT
== 4);
820 for (i
= 0; i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
; i
++)
822 if ((KdbTrapFrame
.Tf
.Dr7
& (0x3 << (i
* 2))) == 0)
825 ASSERT(i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
827 /* Set the breakpoint address. */
831 KdbTrapFrame
.Tf
.Dr0
= BreakPoint
->Address
;
834 KdbTrapFrame
.Tf
.Dr1
= BreakPoint
->Address
;
837 KdbTrapFrame
.Tf
.Dr2
= BreakPoint
->Address
;
840 KdbTrapFrame
.Tf
.Dr3
= BreakPoint
->Address
;
844 /* Enable the global breakpoint */
845 KdbTrapFrame
.Tf
.Dr7
|= (0x2 << (i
* 2));
847 /* Enable the exact match bits. */
848 KdbTrapFrame
.Tf
.Dr7
|= 0x00000300;
850 /* Clear existing state. */
851 KdbTrapFrame
.Tf
.Dr7
&= ~(0xF << (16 + (i
* 4)));
853 /* Set the breakpoint type. */
854 switch (BreakPoint
->Data
.Hw
.AccessType
)
863 case KdbAccessReadWrite
:
871 KdbTrapFrame
.Tf
.Dr7
|= (ul
<< (16 + (i
* 4)));
873 /* Set the breakpoint length. */
874 KdbTrapFrame
.Tf
.Dr7
|= ((BreakPoint
->Data
.Hw
.Size
- 1) << (18 + (i
* 4)));
876 /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
877 if (&KdbTrapFrame
!= KdbCurrentTrapFrame
)
879 KdbCurrentTrapFrame
->Tf
.Dr0
= KdbTrapFrame
.Tf
.Dr0
;
880 KdbCurrentTrapFrame
->Tf
.Dr1
= KdbTrapFrame
.Tf
.Dr1
;
881 KdbCurrentTrapFrame
->Tf
.Dr2
= KdbTrapFrame
.Tf
.Dr2
;
882 KdbCurrentTrapFrame
->Tf
.Dr3
= KdbTrapFrame
.Tf
.Dr3
;
883 KdbCurrentTrapFrame
->Tf
.Dr6
= KdbTrapFrame
.Tf
.Dr6
;
884 KdbCurrentTrapFrame
->Tf
.Dr7
= KdbTrapFrame
.Tf
.Dr7
;
887 BreakPoint
->Data
.Hw
.DebugReg
= i
;
888 KdbHwBreakPoints
[KdbHwBreakPointCount
++] = BreakPoint
;
891 BreakPoint
->Enabled
= TRUE
;
892 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
893 KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr
);
897 /*!\brief Disables a breakpoint.
899 * \param BreakPointNr Number of the breakpoint to disable. Can be -1
900 * \param BreakPoint Breakpoint to disable. Can be NULL.
902 * \retval TRUE Success.
903 * \retval FALSE Failure.
905 * \sa KdbpEnableBreakPoint
908 KdbpDisableBreakPoint(
909 IN LONG BreakPointNr OPTIONAL
,
910 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
915 if (BreakPointNr
< 0)
917 ASSERT(BreakPoint
!= NULL
);
918 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
920 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
922 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
925 if (BreakPoint
== NULL
)
927 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
929 if (BreakPoint
->Type
== KdbBreakPointNone
)
931 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
935 if (BreakPoint
->Enabled
== FALSE
)
937 KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr
);
941 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
942 BreakPoint
->Type
== KdbBreakPointTemporary
)
944 ASSERT(KdbSwBreakPointCount
> 0);
945 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
946 BreakPoint
->Data
.SavedInstruction
, NULL
);
947 if (!NT_SUCCESS(Status
))
949 KdbpPrint("Couldn't restore original instruction.\n");
953 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
955 if (KdbSwBreakPoints
[i
] == BreakPoint
)
957 KdbSwBreakPoints
[i
] = KdbSwBreakPoints
[--KdbSwBreakPointCount
];
958 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
962 if (i
!= (ULONG
)-1) /* not found */
967 ASSERT(BreakPoint
->Type
== KdbBreakPointHardware
);
969 /* Clear the breakpoint. */
970 KdbTrapFrame
.Tf
.Dr7
&= ~(0x3 << (BreakPoint
->Data
.Hw
.DebugReg
* 2));
971 if ((KdbTrapFrame
.Tf
.Dr7
& 0xFF) == 0)
974 * If no breakpoints are enabled then clear the exact match flags.
976 KdbTrapFrame
.Tf
.Dr7
&= 0xFFFFFCFF;
979 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
981 if (KdbHwBreakPoints
[i
] == BreakPoint
)
983 KdbHwBreakPoints
[i
] = KdbHwBreakPoints
[--KdbHwBreakPointCount
];
984 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
988 if (i
!= (ULONG
)-1) /* not found */
992 BreakPoint
->Enabled
= FALSE
;
993 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
994 KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr
);
998 /*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
1000 * \param ExceptionNr Number of the exception to get condition of.
1001 * \param FirstChance Whether to get first or last chance condition.
1002 * \param Condition Receives the condition setting.
1004 * \retval TRUE Success.
1005 * \retval FALSE Failure (invalid exception nr)
1008 KdbpGetEnterCondition(
1009 IN LONG ExceptionNr
,
1010 IN BOOLEAN FirstChance
,
1011 OUT KDB_ENTER_CONDITION
*Condition
)
1013 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
))
1016 *Condition
= KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1];
1020 /*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
1022 * \param ExceptionNr Number of the exception to set condition of (-1 for all)
1023 * \param FirstChance Whether to set first or last chance condition.
1024 * \param Condition The new condition setting.
1026 * \retval TRUE Success.
1027 * \retval FALSE Failure (invalid exception nr)
1030 KdbpSetEnterCondition(
1031 IN LONG ExceptionNr
,
1032 IN BOOLEAN FirstChance
,
1033 IN KDB_ENTER_CONDITION Condition
)
1035 if (ExceptionNr
< 0)
1037 for (ExceptionNr
= 0; ExceptionNr
< (LONG
)RTL_NUMBER_OF(KdbEnterConditions
); ExceptionNr
++)
1039 if (ExceptionNr
== 1 || ExceptionNr
== 8 ||
1040 ExceptionNr
== 9 || ExceptionNr
== 15) /* Reserved exceptions */
1044 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1049 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
) ||
1050 ExceptionNr
== 1 || ExceptionNr
== 8 || /* Do not allow changing of the debug */
1051 ExceptionNr
== 9 || ExceptionNr
== 15) /* trap or reserved exceptions */
1055 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1060 /*!\brief Switches to another thread context
1062 * \param ThreadId Id of the thread to switch to.
1064 * \retval TRUE Success.
1065 * \retval FALSE Failure (i.e. invalid thread id)
1071 PETHREAD Thread
= NULL
;
1074 /* Get a pointer to the thread */
1075 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &Thread
)))
1077 KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG
)ThreadId
);
1080 Process
= Thread
->ThreadsProcess
;
1082 if (KeIsExecutingDpc() && Process
!= KdbCurrentProcess
)
1084 KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
1088 /* Save the current thread's context (if we previously attached to a thread) */
1089 if (KdbCurrentThread
!= KdbOriginalThread
)
1091 ASSERT(KdbCurrentTrapFrame
== &KdbThreadTrapFrame
);
1092 /* Actually, we can't save the context, there's no guarantee that there
1093 * was a trap frame */
1097 ASSERT(KdbCurrentTrapFrame
== &KdbTrapFrame
);
1100 /* Switch to the thread's context */
1101 if (Thread
!= KdbOriginalThread
)
1103 /* The thread we're attaching to isn't the thread on which we entered
1104 * kdb and so the thread we're attaching to is not running. There
1105 * is no guarantee that it actually has a trap frame. So we have to
1106 * peek directly at the registers which were saved on the stack when the
1107 * thread was preempted in the scheduler */
1108 KdbpKdbTrapFrameFromKernelStack(Thread
->Tcb
.KernelStack
,
1109 &KdbThreadTrapFrame
);
1110 KdbCurrentTrapFrame
= &KdbThreadTrapFrame
;
1112 else /* Switching back to original thread */
1114 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1116 KdbCurrentThread
= Thread
;
1118 /* Attach to the thread's process */
1119 ASSERT(KdbCurrentProcess
== PsGetCurrentProcess());
1120 if (KdbCurrentProcess
!= Process
)
1122 if (KdbCurrentProcess
!= KdbOriginalProcess
) /* detach from previously attached process */
1124 KeUnstackDetachProcess(&KdbApcState
);
1126 if (KdbOriginalProcess
!= Process
)
1128 KeStackAttachProcess(&Process
->Pcb
, &KdbApcState
);
1130 KdbCurrentProcess
= Process
;
1136 /*!\brief Switches to another process/thread context
1138 * This function switches to the first thread in the specified process.
1140 * \param ProcessId Id of the process to switch to.
1142 * \retval TRUE Success.
1143 * \retval FALSE Failure (i.e. invalid process id)
1146 KdbpAttachToProcess(
1149 PEPROCESS Process
= NULL
;
1153 /* Get a pointer to the process */
1154 if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId
, &Process
)))
1156 KdbpPrint("Invalid process id: 0x%08x\n", (ULONG
)ProcessId
);
1160 Entry
= Process
->ThreadListHead
.Flink
;
1161 if (Entry
== &KdbCurrentProcess
->ThreadListHead
)
1163 KdbpPrint("No threads in process 0x%08x, cannot attach to process!\n", (ULONG
)ProcessId
);
1167 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1169 return KdbpAttachToThread(Thread
->Cid
.UniqueThread
);
1172 /*!\brief Calls the main loop ...
1177 KdbpCliMainLoop(KdbEnteredOnSingleStep
);
1180 /*!\brief Internal function to enter KDB.
1182 * Disables interrupts, releases display ownership, ...
1188 PVOID SavedInitialStack
, SavedStackBase
, SavedKernelStack
;
1189 ULONG SavedStackLimit
;
1192 if (KdpDebugMode
.Screen
)
1194 InbvAcquireDisplayOwnership();
1197 /* Call the interface's main loop on a different stack */
1198 Thread
= PsGetCurrentThread();
1199 SavedInitialStack
= Thread
->Tcb
.InitialStack
;
1200 SavedStackBase
= Thread
->Tcb
.StackBase
;
1201 SavedStackLimit
= Thread
->Tcb
.StackLimit
;
1202 SavedKernelStack
= Thread
->Tcb
.KernelStack
;
1203 Thread
->Tcb
.InitialStack
= Thread
->Tcb
.StackBase
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1204 Thread
->Tcb
.StackLimit
= (ULONG
)KdbStack
;
1205 Thread
->Tcb
.KernelStack
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1207 /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);*/
1209 KdbpStackSwitchAndCall(KdbStack
+ KDB_STACK_SIZE
- sizeof(ULONG
), KdbpCallMainLoop
);
1211 Thread
->Tcb
.InitialStack
= SavedInitialStack
;
1212 Thread
->Tcb
.StackBase
= SavedStackBase
;
1213 Thread
->Tcb
.StackLimit
= SavedStackLimit
;
1214 Thread
->Tcb
.KernelStack
= SavedKernelStack
;
1219 KdbpGetExceptionNumberFromStatus(IN NTSTATUS ExceptionCode
)
1223 switch (ExceptionCode
)
1225 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1228 case STATUS_SINGLE_STEP
:
1231 case STATUS_BREAKPOINT
:
1234 case STATUS_INTEGER_OVERFLOW
:
1237 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
1240 case STATUS_ILLEGAL_INSTRUCTION
:
1243 case STATUS_FLOAT_INVALID_OPERATION
:
1246 case STATUS_STACK_OVERFLOW
:
1249 case STATUS_ACCESS_VIOLATION
:
1252 case STATUS_DATATYPE_MISALIGNMENT
:
1255 case STATUS_FLOAT_MULTIPLE_TRAPS
:
1260 Ret
= RTL_NUMBER_OF(KdbEnterConditions
) - 1;
1267 /*!\brief KDB Exception filter
1269 * Called by the exception dispatcher.
1271 * \param ExceptionRecord Unused.
1272 * \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
1273 * \param Context Context, IN/OUT parameter.
1274 * \param TrapFrame Exception TrapFrame.
1275 * \param FirstChance TRUE when called before exception frames were serached,
1276 * FALSE for the second call.
1278 * \returns KD_CONTINUE_TYPE
1281 KdbEnterDebuggerException(
1282 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
,
1283 IN KPROCESSOR_MODE PreviousMode
,
1284 IN PCONTEXT Context
,
1285 IN OUT PKTRAP_FRAME TrapFrame
,
1286 IN BOOLEAN FirstChance
)
1288 KDB_ENTER_CONDITION EnterCondition
;
1289 KD_CONTINUE_TYPE ContinueType
= kdHandleException
;
1290 PKDB_BREAKPOINT BreakPoint
;
1293 BOOLEAN Resume
= FALSE
;
1294 BOOLEAN EnterConditionMet
= TRUE
;
1296 NTSTATUS ExceptionCode
;
1298 ExceptionCode
= (ExceptionRecord
!= NULL
? ExceptionRecord
->ExceptionCode
: STATUS_BREAKPOINT
);
1300 KdbCurrentProcess
= PsGetCurrentProcess();
1302 /* Set continue type to kdContinue for single steps and breakpoints */
1303 if (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
)
1304 ContinueType
= kdContinue
;
1306 /* Check if we should handle the exception. */
1307 /* FIXME - won't get all exceptions here :( */
1308 ExpNr
= KdbpGetExceptionNumberFromStatus(ExceptionCode
);
1309 EnterCondition
= KdbEnterConditions
[ExpNr
][FirstChance
? 0 : 1];
1310 if (EnterCondition
== KdbDoNotEnter
||
1311 (EnterCondition
== KdbEnterFromUmode
&& PreviousMode
== KernelMode
) ||
1312 (EnterCondition
== KdbEnterFromKmode
&& PreviousMode
!= KernelMode
))
1314 EnterConditionMet
= FALSE
;
1317 /* If we stopped on one of our breakpoints then let the user know. */
1318 KdbLastBreakPointNr
= -1;
1319 KdbEnteredOnSingleStep
= FALSE
;
1321 if (FirstChance
&& (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
) &&
1322 (KdbLastBreakPointNr
= KdbpIsBreakPointOurs(ExceptionCode
, TrapFrame
)) >= 0)
1324 BreakPoint
= KdbBreakPoints
+ KdbLastBreakPointNr
;
1326 if (ExceptionCode
== STATUS_BREAKPOINT
)
1329 * ... and restore the original instruction.
1331 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
,
1332 BreakPoint
->Data
.SavedInstruction
, NULL
)))
1334 KdbpPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
1335 KeBugCheck(0); // FIXME: Proper bugcode!
1339 if ((BreakPoint
->Type
== KdbBreakPointHardware
) &&
1340 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
))
1342 Resume
= TRUE
; /* Set the resume flag when continuing execution */
1346 * When a temporary breakpoint is hit we have to make sure that we are
1347 * in the same context in which it was set, otherwise it could happen
1348 * that another process/thread hits it before and it gets deleted.
1350 else if (BreakPoint
->Type
== KdbBreakPointTemporary
&&
1351 BreakPoint
->Process
== KdbCurrentProcess
)
1353 ASSERT((TrapFrame
->EFlags
& EFLAGS_TF
) == 0);
1356 * Delete the temporary breakpoint which was used to step over or into the instruction.
1358 KdbpDeleteBreakPoint(-1, BreakPoint
);
1360 if (--KdbNumSingleSteps
> 0)
1362 if ((KdbSingleStepOver
&& !KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1363 (!KdbSingleStepOver
&& !KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1365 Context
->EFlags
|= EFLAGS_TF
;
1367 goto continue_execution
; /* return */
1370 KdbEnteredOnSingleStep
= TRUE
;
1374 * If we hit a breakpoint set by the debugger we set the single step flag,
1375 * ignore the next single step and reenable the breakpoint.
1377 else if (BreakPoint
->Type
== KdbBreakPointSoftware
||
1378 BreakPoint
->Type
== KdbBreakPointTemporary
)
1380 ASSERT(ExceptionCode
== STATUS_BREAKPOINT
);
1381 Context
->EFlags
|= EFLAGS_TF
;
1382 KdbBreakPointToReenable
= BreakPoint
;
1386 * Make sure that the breakpoint should be triggered in this context
1388 if (!BreakPoint
->Global
&& BreakPoint
->Process
!= KdbCurrentProcess
)
1390 goto continue_execution
; /* return */
1394 * Check if the condition for the breakpoint is met.
1396 if (BreakPoint
->Condition
!= NULL
)
1398 /* Setup the KDB trap frame */
1399 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1402 if (!KdbpRpnEvaluateParsedExpression(BreakPoint
->Condition
, &KdbTrapFrame
, &ull
, NULL
, NULL
))
1404 /* FIXME: Print warning? */
1406 else if (ull
== 0) /* condition is not met */
1408 goto continue_execution
; /* return */
1412 if (BreakPoint
->Type
== KdbBreakPointSoftware
)
1414 KdbpPrint("Entered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
1415 KdbLastBreakPointNr
, TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
);
1417 else if (BreakPoint
->Type
== KdbBreakPointHardware
)
1419 KdbpPrint("Entered debugger on breakpoint #%d: %s 0x%08x\n",
1420 KdbLastBreakPointNr
,
1421 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessRead
) ? "READ" :
1422 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessWrite
) ? "WRITE" :
1423 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessReadWrite
) ? "RDWR" : "EXEC")
1430 else if (ExceptionCode
== STATUS_SINGLE_STEP
)
1432 /* Silently ignore a debugger initiated single step. */
1433 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbBreakPointToReenable
!= NULL
)
1435 /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
1436 BreakPoint
= KdbBreakPointToReenable
;
1437 KdbBreakPointToReenable
= NULL
;
1438 ASSERT(BreakPoint
->Type
== KdbBreakPointSoftware
||
1439 BreakPoint
->Type
== KdbBreakPointTemporary
);
1442 * Reenable the breakpoint we disabled to execute the breakpointed
1445 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
, 0xCC,
1446 &BreakPoint
->Data
.SavedInstruction
)))
1448 KdbpPrint("Warning: Couldn't reenable breakpoint %d\n",
1449 BreakPoint
- KdbBreakPoints
);
1452 /* Unset TF if we are no longer single stepping. */
1453 if (KdbNumSingleSteps
== 0)
1454 Context
->EFlags
&= ~EFLAGS_TF
;
1455 goto continue_execution
; /* return */
1458 /* Check if we expect a single step */
1459 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbNumSingleSteps
> 0)
1461 /*ASSERT((Context->Eflags & EFLAGS_TF) != 0);*/
1462 if (--KdbNumSingleSteps
> 0)
1464 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1465 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1467 Context
->EFlags
&= ~EFLAGS_TF
;
1471 Context
->EFlags
|= EFLAGS_TF
;
1473 goto continue_execution
; /* return */
1477 Context
->EFlags
&= ~EFLAGS_TF
;
1478 KdbEnteredOnSingleStep
= TRUE
;
1483 if (!EnterConditionMet
)
1485 return kdHandleException
;
1487 KdbpPrint("Entered debugger on unexpected debug trap!\n");
1490 else if (ExceptionCode
== STATUS_BREAKPOINT
)
1492 if (KdbInitFileBuffer
!= NULL
)
1494 KdbpCliInterpretInitFile();
1495 EnterConditionMet
= FALSE
;
1497 if (!EnterConditionMet
)
1499 return kdHandleException
;
1502 KdbpPrint("Entered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
1503 TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
- 1);
1507 CONST CHAR
*ExceptionString
= (ExpNr
< RTL_NUMBER_OF(ExceptionNrToString
)) ?
1508 (ExceptionNrToString
[ExpNr
]) :
1509 ("Unknown/User defined exception");
1511 if (!EnterConditionMet
)
1513 return ContinueType
;
1516 KdbpPrint("Entered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
1517 FirstChance
? "first" : "last", ExceptionCode
, ExceptionString
);
1518 if (ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
1519 ExceptionRecord
!= NULL
&& ExceptionRecord
->NumberParameters
!= 0)
1521 /* FIXME: Add noexec memory stuff */
1525 asm volatile("movl %%cr2, %0" : "=r"(TrapCr2
));
1528 __asm mov TrapCr2
, eax
;
1530 #error Unknown compiler for inline assembler
1533 Err
= TrapFrame
->ErrCode
;
1534 KdbpPrint("Memory at 0x%p could not be %s: ", TrapCr2
, (Err
& (1 << 1)) ? "written" : "read");
1535 if ((Err
& (1 << 0)) == 0)
1536 KdbpPrint("Page not present.\n");
1539 if ((Err
& (1 << 3)) != 0)
1540 KdbpPrint("Reserved bits in page directory set.\n");
1542 KdbpPrint("Page protection violation.\n");
1547 /* Once we enter the debugger we do not expect any more single steps to happen */
1548 KdbNumSingleSteps
= 0;
1550 /* Update the current process pointer */
1551 KdbCurrentProcess
= KdbOriginalProcess
= PsGetCurrentProcess();
1552 KdbCurrentThread
= KdbOriginalThread
= PsGetCurrentThread();
1553 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1555 /* Setup the KDB trap frame */
1556 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1558 /* Enter critical section */
1559 Ke386SaveFlags(OldEflags
);
1562 /* Exception inside the debugger? Game over. */
1563 if (InterlockedIncrement(&KdbEntryCount
) > 1)
1565 Ke386RestoreFlags(OldEflags
);
1566 return kdHandleException
;
1569 /* Call the main loop. */
1570 KdbpInternalEnter();
1572 /* Check if we should single step */
1573 if (KdbNumSingleSteps
> 0)
1575 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)) ||
1576 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)))
1578 ASSERT((KdbCurrentTrapFrame
->Tf
.EFlags
& EFLAGS_TF
) == 0);
1579 /*KdbCurrentTrapFrame->Tf.EFlags &= ~EFLAGS_TF;*/
1583 Context
->EFlags
|= EFLAGS_TF
;
1587 /* We can't update the current thread's trapframe 'cause it might not
1590 /* Detach from attached process */
1591 if (KdbCurrentProcess
!= KdbOriginalProcess
)
1593 KeUnstackDetachProcess(&KdbApcState
);
1596 /* Update the exception TrapFrame */
1597 KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame
, TrapFrame
);
1599 /* Decrement the entry count */
1600 InterlockedDecrement(&KdbEntryCount
);
1602 /* Leave critical section */
1603 Ke386RestoreFlags(OldEflags
);
1605 /* Check if user requested a bugcheck */
1606 if (KdbpBugCheckRequested
)
1608 /* Bugcheck the system */
1609 KeBugCheck(MANUALLY_INITIATED_CRASH
);
1613 /* Clear debug status */
1614 if (ExceptionCode
== STATUS_BREAKPOINT
) /* FIXME: Why clear DR6 on INT3? */
1616 /* Set the RF flag so we don't trigger the same breakpoint again. */
1619 TrapFrame
->EFlags
|= EFLAGS_RF
;
1622 /* Clear dr6 status flags. */
1623 TrapFrame
->Dr6
&= ~0x0000e00f;
1625 /* Skip the current instruction */
1629 return ContinueType
;
1633 KdbDeleteProcessHook(IN PEPROCESS Process
)
1635 KdbSymFreeProcessSymbols(Process
);
1637 /* FIXME: Delete breakpoints for process */
1642 KdbpGetCommandLineSettings(PCHAR p1
)
1646 while (p1
&& (p2
= strchr(p1
, ' ')))
1650 if (!_strnicmp(p2
, "KDSERIAL", 8))
1653 KdbDebugState
|= KD_DEBUG_KDSERIAL
;
1654 KdpDebugMode
.Serial
= TRUE
;
1656 else if (!_strnicmp(p2
, "KDNOECHO", 8))
1659 KdbDebugState
|= KD_DEBUG_KDNOECHO
;
1667 KdbpSafeReadMemory(OUT PVOID Dest
,
1671 BOOLEAN Result
= TRUE
;
1679 Result
= KdpSafeReadMemory((ULONG_PTR
)Src
, Bytes
, Dest
);
1683 ULONG_PTR Start
, End
, Write
;
1684 for (Start
= (ULONG_PTR
)Src
,
1685 End
= Start
+ Bytes
,
1686 Write
= (ULONG_PTR
)Dest
;
1687 Result
&& (Start
< End
);
1689 if (!KdpSafeReadMemory(Start
, 1, (PVOID
)Write
))
1695 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;
1699 KdbpSafeWriteMemory(OUT PVOID Dest
,
1703 BOOLEAN Result
= TRUE
;
1704 ULONG_PTR Start
, End
, Write
;
1706 for (Start
= (ULONG_PTR
)Src
,
1707 End
= Start
+ Bytes
,
1708 Write
= (ULONG_PTR
)Dest
;
1709 Result
&& (Start
< End
);
1711 if (!KdpSafeWriteMemory(Write
, 1, *((PCHAR
)Start
)))
1714 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;