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
;
55 /* Array of conditions when to enter KDB */
56 STATIC KDB_ENTER_CONDITION KdbEnterConditions
[][2] =
58 /* First chance Last chance */
59 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Zero devide */
60 { KdbEnterFromKmode
, KdbDoNotEnter
}, /* Debug trap */
61 { KdbDoNotEnter
, KdbEnterAlways
}, /* NMI */
62 { KdbEnterFromKmode
, KdbDoNotEnter
}, /* INT3 */
63 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Overflow */
64 { KdbDoNotEnter
, KdbEnterFromKmode
},
65 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Invalid opcode */
66 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* No math coprocessor fault */
67 { KdbEnterAlways
, KdbEnterAlways
},
68 { KdbEnterAlways
, KdbEnterAlways
},
69 { KdbDoNotEnter
, KdbEnterFromKmode
},
70 { KdbDoNotEnter
, KdbEnterFromKmode
},
71 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Stack fault */
72 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* General protection fault */
73 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* Page fault */
74 { KdbEnterAlways
, KdbEnterAlways
}, /* Reserved (15) */
75 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* FPU fault */
76 { KdbDoNotEnter
, KdbEnterFromKmode
},
77 { KdbDoNotEnter
, KdbEnterFromKmode
},
78 { KdbDoNotEnter
, KdbEnterFromKmode
}, /* SIMD fault */
79 { KdbDoNotEnter
, KdbEnterFromKmode
} /* Last entry: used for unknown exceptions */
82 /* Exception descriptions */
83 STATIC CONST CHAR
*ExceptionNrToString
[] =
90 "BOUND range exceeded",
92 "No Math Coprocessor",
96 "Segment Not Present",
97 "Stack Segment Fault",
109 KiSsFromTrapFrame(IN PKTRAP_FRAME TrapFrame
);
113 KiEspFromTrapFrame(IN PKTRAP_FRAME TrapFrame
);
117 KiSsToTrapFrame(IN PKTRAP_FRAME TrapFrame
,
122 KiEspToTrapFrame(IN PKTRAP_FRAME TrapFrame
,
125 /* ROS Internal. Please deprecate */
129 HalReleaseDisplayOwnership(
133 /* FUNCTIONS *****************************************************************/
136 KdbpTrapFrameToKdbTrapFrame(PKTRAP_FRAME TrapFrame
, PKDB_KTRAP_FRAME KdbTrapFrame
)
138 ULONG TrapCr0
, TrapCr2
, TrapCr3
, TrapCr4
;
140 /* Copy the TrapFrame only up to Eflags and zero the rest*/
141 RtlCopyMemory(&KdbTrapFrame
->Tf
, TrapFrame
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
142 RtlZeroMemory((PVOID
)((ULONG_PTR
)&KdbTrapFrame
->Tf
+ FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
)),
143 sizeof (KTRAP_FRAME
) - FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
147 "movl %%cr0, %0" "\n\t"
148 "movl %%cr2, %1" "\n\t"
149 "movl %%cr3, %2" "\n\t"
150 "movl %%cr4, %3" "\n\t"
151 : "=r"(TrapCr0
), "=r"(TrapCr2
),
152 "=r"(TrapCr3
), "=r"(TrapCr4
));
164 /* FIXME: What's the problem with cr4? */
170 KdbTrapFrame
->Cr0
= TrapCr0
;
171 KdbTrapFrame
->Cr2
= TrapCr2
;
172 KdbTrapFrame
->Cr3
= TrapCr3
;
173 KdbTrapFrame
->Cr4
= TrapCr4
;
175 KdbTrapFrame
->Tf
.HardwareEsp
= KiEspFromTrapFrame(TrapFrame
);
176 KdbTrapFrame
->Tf
.HardwareSegSs
= (USHORT
)(KiSsFromTrapFrame(TrapFrame
) & 0xFFFF);
179 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
183 KdbpKdbTrapFrameToTrapFrame(PKDB_KTRAP_FRAME KdbTrapFrame
, PKTRAP_FRAME TrapFrame
)
185 /* Copy the TrapFrame only up to Eflags and zero the rest*/
186 RtlCopyMemory(TrapFrame
, &KdbTrapFrame
->Tf
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
188 /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */
190 KiSsToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareSegSs
);
191 KiEspToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareEsp
);
193 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
197 KdbpKdbTrapFrameFromKernelStack(PVOID KernelStack
,
198 PKDB_KTRAP_FRAME KdbTrapFrame
)
202 RtlZeroMemory(KdbTrapFrame
, sizeof(KDB_KTRAP_FRAME
));
203 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
;
218 /* FIXME: what about the other registers??? */
221 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
222 * the old instruction in *OldInst.
224 * \param Process Process in which's context to overwrite the instruction.
225 * \param Address Address at which to overwrite the instruction.
226 * \param NewInst New instruction (written to \a Address)
227 * \param OldInst Old instruction (read from \a Address)
232 KdbpOverwriteInstruction(
233 IN PEPROCESS Process
,
234 IN ULONG_PTR Address
,
236 OUT PUCHAR OldInst OPTIONAL
)
240 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
243 /* Get the protection for the address. */
244 Protect
= MmGetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
246 /* Return if that page isn't present. */
247 if (Protect
& PAGE_NOACCESS
)
249 return STATUS_MEMORY_NOT_ALLOCATED
;
252 /* Attach to the process */
253 if (CurrentProcess
!= Process
)
255 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
258 /* Make the page writeable if it is read only. */
259 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
261 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
),
262 (Protect
& ~(PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
)) | PAGE_READWRITE
);
265 /* Copy the old instruction back to the caller. */
268 Status
= KdbpSafeReadMemory(OldInst
, (PUCHAR
)Address
, 1);
269 if (!NT_SUCCESS(Status
))
271 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
273 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
275 /* Detach from process */
276 if (CurrentProcess
!= Process
)
284 /* Copy the new instruction in its place. */
285 Status
= KdbpSafeWriteMemory((PUCHAR
)Address
, &NewInst
, 1);
287 /* Restore the page protection. */
288 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
290 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
293 /* Detach from process */
294 if (CurrentProcess
!= Process
)
296 KeUnstackDetachProcess(&ApcState
);
302 /*!\brief Checks whether the given instruction can be single stepped or has to be
303 * stepped over using a temporary breakpoint.
305 * \retval TRUE Instruction is a call.
306 * \retval FALSE Instruction is not a call.
309 KdbpShouldStepOverInstruction(ULONG_PTR Eip
)
314 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
316 KdbpPrint("Couldn't access memory at 0x%p\n", Eip
);
320 /* Check if the current instruction is a call. */
321 while ((i
< sizeof (Mem
)) && (Mem
[i
] == 0x66 || Mem
[i
] == 0x67))
323 if (i
== sizeof (Mem
))
325 if (Mem
[i
] == 0xE8 || Mem
[i
] == 0x9A || Mem
[i
] == 0xF2 || Mem
[i
] == 0xF3 ||
326 (((i
+ 1) < sizeof (Mem
)) && Mem
[i
] == 0xFF && (Mem
[i
+1] & 0x38) == 0x10))
333 /*!\brief Steps over an instruction
335 * If the given instruction should be stepped over, this function inserts a
336 * temporary breakpoint after the instruction and returns TRUE, otherwise it
339 * \retval TRUE Temporary breakpoint set after instruction.
340 * \retval FALSE No breakpoint was set.
343 KdbpStepOverInstruction(ULONG_PTR Eip
)
347 if (!KdbpShouldStepOverInstruction(Eip
))
350 InstLen
= KdbpGetInstLength(Eip
);
354 if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip
+ InstLen
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
360 /*!\brief Steps into an instruction (interrupts)
362 * If the given instruction should be stepped into, this function inserts a
363 * temporary breakpoint at the target instruction and returns TRUE, otherwise it
366 * \retval TRUE Temporary breakpoint set at target instruction.
367 * \retval FALSE No breakpoint was set.
370 KdbpStepIntoInstruction(ULONG_PTR Eip
)
372 KDESCRIPTOR Idtr
= {0};
379 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
381 /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
385 /* Check for INT instruction */
386 /* FIXME: Check for iret */
389 else if (Mem
[0] == 0xcd)
391 else if (Mem
[0] == 0xce && KdbCurrentTrapFrame
->Tf
.EFlags
& (1<<11)) /* 1 << 11 is the overflow flag */
396 if (IntVect
< 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
401 /* Read the interrupt descriptor table register */
402 Ke386GetInterruptDescriptorTable(*(PKDESCRIPTOR
)&Idtr
.Limit
);
403 if (IntVect
>= (Idtr
.Limit
+ 1) / 8)
405 /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
409 /* Get the interrupt descriptor */
410 if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc
, (PVOID
)(ULONG_PTR
)(Idtr
.Base
+ (IntVect
* 8)), sizeof (IntDesc
))))
412 /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
416 /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
417 if ((IntDesc
[1] & (1 << 15)) == 0) /* not present */
421 if ((IntDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
423 /* FIXME: Task gates not supported */
426 else if (((IntDesc
[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
427 ((IntDesc
[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
429 /* FIXME: Should the segment selector of the interrupt gate be checked? */
430 TargetEip
= (IntDesc
[1] & 0xffff0000) | (IntDesc
[0] & 0x0000ffff);
437 /* Insert breakpoint */
438 if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
444 /*!\brief Gets the number of the next breakpoint >= Start.
446 * \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
448 * \returns Breakpoint number (-1 if no more breakpoints are found)
451 KdbpGetNextBreakPointNr(
452 IN ULONG Start OPTIONAL
)
454 for (; Start
< RTL_NUMBER_OF(KdbBreakPoints
); Start
++)
456 if (KdbBreakPoints
[Start
].Type
!= KdbBreakPointNone
)
462 /*!\brief Returns information of the specified breakpoint.
464 * \param BreakPointNr Number of the breakpoint to return information of.
465 * \param Address Receives the address of the breakpoint.
466 * \param Type Receives the type of the breakpoint (hardware or software)
467 * \param Size Size - for memory breakpoints.
468 * \param AccessType Access type - for hardware breakpoints.
469 * \param DebugReg Debug register - for enabled hardware breakpoints.
470 * \param Enabled Whether the breakpoint is enabled or not.
471 * \param Process The owning process of the breakpoint.
472 * \param ConditionExpression The expression which was given as condition for the bp.
474 * \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
477 KdbpGetBreakPointInfo(
478 IN ULONG BreakPointNr
,
479 OUT ULONG_PTR
*Address OPTIONAL
,
480 OUT KDB_BREAKPOINT_TYPE
*Type OPTIONAL
,
481 OUT UCHAR
*Size OPTIONAL
,
482 OUT KDB_ACCESS_TYPE
*AccessType OPTIONAL
,
483 OUT UCHAR
*DebugReg OPTIONAL
,
484 OUT BOOLEAN
*Enabled OPTIONAL
,
485 OUT BOOLEAN
*Global OPTIONAL
,
486 OUT PEPROCESS
*Process OPTIONAL
,
487 OUT PCHAR
*ConditionExpression OPTIONAL
)
491 if (BreakPointNr
>= RTL_NUMBER_OF(KdbBreakPoints
) ||
492 KdbBreakPoints
[BreakPointNr
].Type
== KdbBreakPointNone
)
497 bp
= KdbBreakPoints
+ BreakPointNr
;
499 *Address
= bp
->Address
;
502 if (bp
->Type
== KdbBreakPointHardware
)
505 *Size
= bp
->Data
.Hw
.Size
;
506 if (AccessType
!= NULL
)
507 *AccessType
= bp
->Data
.Hw
.AccessType
;
508 if (DebugReg
!= NULL
&& bp
->Enabled
)
509 *DebugReg
= bp
->Data
.Hw
.DebugReg
;
512 *Enabled
= bp
->Enabled
;
514 *Global
= bp
->Global
;
516 *Process
= bp
->Process
;
517 if (ConditionExpression
!= NULL
)
518 *ConditionExpression
= bp
->ConditionExpression
;
523 /*!\brief Inserts a breakpoint into the breakpoint array.
525 * The \a Process of the breakpoint is set to \a KdbCurrentProcess
527 * \param Address Address at which to set the breakpoint.
528 * \param Type Type of breakpoint (hardware or software)
529 * \param Size Size of breakpoint (for hardware/memory breakpoints)
530 * \param AccessType Access type (for hardware breakpoins)
531 * \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
532 * \param Global Wether the breakpoint is global or local to a process.
533 * \param BreakPointNumber Receives the breakpoint number on success
538 KdbpInsertBreakPoint(
539 IN ULONG_PTR Address
,
540 IN KDB_BREAKPOINT_TYPE Type
,
541 IN UCHAR Size OPTIONAL
,
542 IN KDB_ACCESS_TYPE AccessType OPTIONAL
,
543 IN PCHAR ConditionExpression OPTIONAL
,
545 OUT PLONG BreakPointNr OPTIONAL
)
549 PCHAR ConditionExpressionDup
;
553 ASSERT(Type
!= KdbBreakPointNone
);
555 if (Type
== KdbBreakPointHardware
)
557 if ((Address
% Size
) != 0)
559 KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address
, Size
);
560 return STATUS_UNSUCCESSFUL
;
562 if (AccessType
== KdbAccessExec
&& Size
!= 1)
564 KdbpPrint("Size must be 1 for execution breakpoints.\n");
565 return STATUS_UNSUCCESSFUL
;
569 if (KdbBreakPointCount
== KDB_MAXIMUM_BREAKPOINT_COUNT
)
571 return STATUS_UNSUCCESSFUL
;
574 /* Parse conditon expression string and duplicate it */
575 if (ConditionExpression
!= NULL
)
577 Condition
= KdbpRpnParseExpression(ConditionExpression
, &ErrOffset
, ErrMsg
);
578 if (Condition
== NULL
)
581 KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg
, ErrOffset
);
583 KdbpPrint("Couldn't parse expression: %s", ErrMsg
);
584 return STATUS_UNSUCCESSFUL
;
587 i
= strlen(ConditionExpression
) + 1;
588 ConditionExpressionDup
= ExAllocatePoolWithTag(NonPagedPool
, i
, TAG_KDBG
);
589 RtlCopyMemory(ConditionExpressionDup
, ConditionExpression
, i
);
595 ConditionExpressionDup
= NULL
;
598 /* Find unused breakpoint */
599 if (Type
== KdbBreakPointTemporary
)
601 for (i
= RTL_NUMBER_OF(KdbBreakPoints
) - 1; i
>= 0; i
--)
603 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
609 for (i
= 0; i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
); i
++)
611 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
615 ASSERT(i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
));
617 /* Set the breakpoint */
618 ASSERT(KdbCurrentProcess
!= NULL
);
619 KdbBreakPoints
[i
].Type
= Type
;
620 KdbBreakPoints
[i
].Address
= Address
;
621 KdbBreakPoints
[i
].Enabled
= FALSE
;
622 KdbBreakPoints
[i
].Global
= Global
;
623 KdbBreakPoints
[i
].Process
= KdbCurrentProcess
;
624 KdbBreakPoints
[i
].ConditionExpression
= ConditionExpressionDup
;
625 KdbBreakPoints
[i
].Condition
= Condition
;
626 if (Type
== KdbBreakPointHardware
)
629 KdbBreakPoints
[i
].Data
.Hw
.Size
= Size
;
630 KdbBreakPoints
[i
].Data
.Hw
.AccessType
= AccessType
;
632 KdbBreakPointCount
++;
634 if (Type
!= KdbBreakPointTemporary
)
635 KdbpPrint("Breakpoint %d inserted.\n", i
);
637 /* Try to enable the breakpoint */
638 KdbpEnableBreakPoint(i
, NULL
);
640 /* Return the breakpoint number */
641 if (BreakPointNr
!= NULL
)
644 return STATUS_SUCCESS
;
647 /*!\brief Deletes a breakpoint
649 * \param BreakPointNr Number of the breakpoint to delete. Can be -1
650 * \param BreakPoint Breakpoint to delete. Can be NULL.
652 * \retval TRUE Success.
653 * \retval FALSE Failure (invalid breakpoint number)
656 KdbpDeleteBreakPoint(
657 IN LONG BreakPointNr OPTIONAL
,
658 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
660 if (BreakPointNr
< 0)
662 ASSERT(BreakPoint
!= NULL
);
663 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
665 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
667 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
670 if (BreakPoint
== NULL
)
672 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
674 if (BreakPoint
->Type
== KdbBreakPointNone
)
676 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
680 if (BreakPoint
->Enabled
&&
681 !KdbpDisableBreakPoint(-1, BreakPoint
))
684 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
685 KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr
);
686 BreakPoint
->Type
= KdbBreakPointNone
;
687 KdbBreakPointCount
--;
692 /*!\brief Checks if the breakpoint was set by the debugger
694 * Tries to find a breakpoint in the breakpoint array which caused
695 * the debug exception to happen.
697 * \param ExpNr Exception Number (1 or 3)
698 * \param TrapFrame Exception trapframe
700 * \returns Breakpoint number, -1 on error.
703 KdbpIsBreakPointOurs(
704 IN NTSTATUS ExceptionCode
,
705 IN PKTRAP_FRAME TrapFrame
)
708 ASSERT(ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
);
710 if (ExceptionCode
== STATUS_BREAKPOINT
) /* Software interrupt */
712 ULONG_PTR BpEip
= (ULONG_PTR
)TrapFrame
->Eip
- 1; /* Get EIP of INT3 instruction */
713 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
715 ASSERT((KdbSwBreakPoints
[i
]->Type
== KdbBreakPointSoftware
||
716 KdbSwBreakPoints
[i
]->Type
== KdbBreakPointTemporary
));
717 ASSERT(KdbSwBreakPoints
[i
]->Enabled
);
718 if (KdbSwBreakPoints
[i
]->Address
== BpEip
)
720 return KdbSwBreakPoints
[i
] - KdbBreakPoints
;
724 else if (ExceptionCode
== STATUS_SINGLE_STEP
) /* Hardware interrupt */
727 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
729 ASSERT(KdbHwBreakPoints
[i
]->Type
== KdbBreakPointHardware
&&
730 KdbHwBreakPoints
[i
]->Enabled
);
731 DebugReg
= KdbHwBreakPoints
[i
]->Data
.Hw
.DebugReg
;
732 if ((TrapFrame
->Dr6
& (1 << DebugReg
)) != 0)
734 return KdbHwBreakPoints
[i
] - KdbBreakPoints
;
742 /*!\brief Enables a breakpoint.
744 * \param BreakPointNr Number of the breakpoint to enable Can be -1.
745 * \param BreakPoint Breakpoint to enable. Can be NULL.
747 * \retval TRUE Success.
748 * \retval FALSE Failure.
750 * \sa KdbpDisableBreakPoint
753 KdbpEnableBreakPoint(
754 IN LONG BreakPointNr OPTIONAL
,
755 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
761 if (BreakPointNr
< 0)
763 ASSERT(BreakPoint
!= NULL
);
764 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
766 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
768 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
771 if (BreakPoint
== NULL
)
773 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
775 if (BreakPoint
->Type
== KdbBreakPointNone
)
777 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
781 if (BreakPoint
->Enabled
== TRUE
)
783 KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr
);
787 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
788 BreakPoint
->Type
== KdbBreakPointTemporary
)
790 if (KdbSwBreakPointCount
>= KDB_MAXIMUM_SW_BREAKPOINT_COUNT
)
792 KdbpPrint("Maximum number of SW breakpoints (%d) used. "
793 "Disable another breakpoint in order to enable this one.\n",
794 KDB_MAXIMUM_SW_BREAKPOINT_COUNT
);
797 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
798 0xCC, &BreakPoint
->Data
.SavedInstruction
);
799 if (!NT_SUCCESS(Status
))
801 KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint
->Address
);
804 KdbSwBreakPoints
[KdbSwBreakPointCount
++] = BreakPoint
;
808 if (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
)
809 ASSERT(BreakPoint
->Data
.Hw
.Size
== 1);
810 ASSERT((BreakPoint
->Address
% BreakPoint
->Data
.Hw
.Size
) == 0);
811 if (KdbHwBreakPointCount
>= KDB_MAXIMUM_HW_BREAKPOINT_COUNT
)
813 KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
814 "Disable another breakpoint in order to enable this one.\n",
815 KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
819 /* Find unused hw breakpoint */
820 ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT
== 4);
821 for (i
= 0; i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
; i
++)
823 if ((KdbTrapFrame
.Tf
.Dr7
& (0x3 << (i
* 2))) == 0)
826 ASSERT(i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
828 /* Set the breakpoint address. */
832 KdbTrapFrame
.Tf
.Dr0
= BreakPoint
->Address
;
835 KdbTrapFrame
.Tf
.Dr1
= BreakPoint
->Address
;
838 KdbTrapFrame
.Tf
.Dr2
= BreakPoint
->Address
;
841 KdbTrapFrame
.Tf
.Dr3
= BreakPoint
->Address
;
845 /* Enable the global breakpoint */
846 KdbTrapFrame
.Tf
.Dr7
|= (0x2 << (i
* 2));
848 /* Enable the exact match bits. */
849 KdbTrapFrame
.Tf
.Dr7
|= 0x00000300;
851 /* Clear existing state. */
852 KdbTrapFrame
.Tf
.Dr7
&= ~(0xF << (16 + (i
* 4)));
854 /* Set the breakpoint type. */
855 switch (BreakPoint
->Data
.Hw
.AccessType
)
864 case KdbAccessReadWrite
:
872 KdbTrapFrame
.Tf
.Dr7
|= (ul
<< (16 + (i
* 4)));
874 /* Set the breakpoint length. */
875 KdbTrapFrame
.Tf
.Dr7
|= ((BreakPoint
->Data
.Hw
.Size
- 1) << (18 + (i
* 4)));
877 /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
878 if (&KdbTrapFrame
!= KdbCurrentTrapFrame
)
880 KdbCurrentTrapFrame
->Tf
.Dr0
= KdbTrapFrame
.Tf
.Dr0
;
881 KdbCurrentTrapFrame
->Tf
.Dr1
= KdbTrapFrame
.Tf
.Dr1
;
882 KdbCurrentTrapFrame
->Tf
.Dr2
= KdbTrapFrame
.Tf
.Dr2
;
883 KdbCurrentTrapFrame
->Tf
.Dr3
= KdbTrapFrame
.Tf
.Dr3
;
884 KdbCurrentTrapFrame
->Tf
.Dr6
= KdbTrapFrame
.Tf
.Dr6
;
885 KdbCurrentTrapFrame
->Tf
.Dr7
= KdbTrapFrame
.Tf
.Dr7
;
888 BreakPoint
->Data
.Hw
.DebugReg
= i
;
889 KdbHwBreakPoints
[KdbHwBreakPointCount
++] = BreakPoint
;
892 BreakPoint
->Enabled
= TRUE
;
893 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
894 KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr
);
898 /*!\brief Disables a breakpoint.
900 * \param BreakPointNr Number of the breakpoint to disable. Can be -1
901 * \param BreakPoint Breakpoint to disable. Can be NULL.
903 * \retval TRUE Success.
904 * \retval FALSE Failure.
906 * \sa KdbpEnableBreakPoint
909 KdbpDisableBreakPoint(
910 IN LONG BreakPointNr OPTIONAL
,
911 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
916 if (BreakPointNr
< 0)
918 ASSERT(BreakPoint
!= NULL
);
919 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
921 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
923 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
926 if (BreakPoint
== NULL
)
928 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
930 if (BreakPoint
->Type
== KdbBreakPointNone
)
932 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
936 if (BreakPoint
->Enabled
== FALSE
)
938 KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr
);
942 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
943 BreakPoint
->Type
== KdbBreakPointTemporary
)
945 ASSERT(KdbSwBreakPointCount
> 0);
946 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
947 BreakPoint
->Data
.SavedInstruction
, NULL
);
948 if (!NT_SUCCESS(Status
))
950 KdbpPrint("Couldn't restore original instruction.\n");
954 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
956 if (KdbSwBreakPoints
[i
] == BreakPoint
)
958 KdbSwBreakPoints
[i
] = KdbSwBreakPoints
[--KdbSwBreakPointCount
];
959 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
963 if (i
!= (ULONG
)-1) /* not found */
968 ASSERT(BreakPoint
->Type
== KdbBreakPointHardware
);
970 /* Clear the breakpoint. */
971 KdbTrapFrame
.Tf
.Dr7
&= ~(0x3 << (BreakPoint
->Data
.Hw
.DebugReg
* 2));
972 if ((KdbTrapFrame
.Tf
.Dr7
& 0xFF) == 0)
975 * If no breakpoints are enabled then clear the exact match flags.
977 KdbTrapFrame
.Tf
.Dr7
&= 0xFFFFFCFF;
980 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
982 if (KdbHwBreakPoints
[i
] == BreakPoint
)
984 KdbHwBreakPoints
[i
] = KdbHwBreakPoints
[--KdbHwBreakPointCount
];
985 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
989 if (i
!= (ULONG
)-1) /* not found */
993 BreakPoint
->Enabled
= FALSE
;
994 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
995 KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr
);
999 /*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
1001 * \param ExceptionNr Number of the exception to get condition of.
1002 * \param FirstChance Whether to get first or last chance condition.
1003 * \param Condition Receives the condition setting.
1005 * \retval TRUE Success.
1006 * \retval FALSE Failure (invalid exception nr)
1009 KdbpGetEnterCondition(
1010 IN LONG ExceptionNr
,
1011 IN BOOLEAN FirstChance
,
1012 OUT KDB_ENTER_CONDITION
*Condition
)
1014 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
))
1017 *Condition
= KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1];
1021 /*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
1023 * \param ExceptionNr Number of the exception to set condition of (-1 for all)
1024 * \param FirstChance Whether to set first or last chance condition.
1025 * \param Condition The new condition setting.
1027 * \retval TRUE Success.
1028 * \retval FALSE Failure (invalid exception nr)
1031 KdbpSetEnterCondition(
1032 IN LONG ExceptionNr
,
1033 IN BOOLEAN FirstChance
,
1034 IN KDB_ENTER_CONDITION Condition
)
1036 if (ExceptionNr
< 0)
1038 for (ExceptionNr
= 0; ExceptionNr
< (LONG
)RTL_NUMBER_OF(KdbEnterConditions
); ExceptionNr
++)
1040 if (ExceptionNr
== 1 || ExceptionNr
== 8 ||
1041 ExceptionNr
== 9 || ExceptionNr
== 15) /* Reserved exceptions */
1045 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1050 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
) ||
1051 ExceptionNr
== 1 || ExceptionNr
== 8 || /* Do not allow changing of the debug */
1052 ExceptionNr
== 9 || ExceptionNr
== 15) /* trap or reserved exceptions */
1056 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1061 /*!\brief Switches to another thread context
1063 * \param ThreadId Id of the thread to switch to.
1065 * \retval TRUE Success.
1066 * \retval FALSE Failure (i.e. invalid thread id)
1072 PETHREAD Thread
= NULL
;
1075 /* Get a pointer to the thread */
1076 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &Thread
)))
1078 KdbpPrint("Invalid thread id: 0x%08x\n", (ULONG_PTR
)ThreadId
);
1081 Process
= Thread
->ThreadsProcess
;
1083 if (KeIsExecutingDpc() && Process
!= KdbCurrentProcess
)
1085 KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
1089 /* Save the current thread's context (if we previously attached to a thread) */
1090 if (KdbCurrentThread
!= KdbOriginalThread
)
1092 ASSERT(KdbCurrentTrapFrame
== &KdbThreadTrapFrame
);
1093 /* Actually, we can't save the context, there's no guarantee that there
1094 * was a trap frame */
1098 ASSERT(KdbCurrentTrapFrame
== &KdbTrapFrame
);
1101 /* Switch to the thread's context */
1102 if (Thread
!= KdbOriginalThread
)
1104 /* The thread we're attaching to isn't the thread on which we entered
1105 * kdb and so the thread we're attaching to is not running. There
1106 * is no guarantee that it actually has a trap frame. So we have to
1107 * peek directly at the registers which were saved on the stack when the
1108 * thread was preempted in the scheduler */
1109 KdbpKdbTrapFrameFromKernelStack(Thread
->Tcb
.KernelStack
,
1110 &KdbThreadTrapFrame
);
1111 KdbCurrentTrapFrame
= &KdbThreadTrapFrame
;
1113 else /* Switching back to original thread */
1115 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1117 KdbCurrentThread
= Thread
;
1119 /* Attach to the thread's process */
1120 ASSERT(KdbCurrentProcess
== PsGetCurrentProcess());
1121 if (KdbCurrentProcess
!= Process
)
1123 if (KdbCurrentProcess
!= KdbOriginalProcess
) /* detach from previously attached process */
1125 KeUnstackDetachProcess(&KdbApcState
);
1127 if (KdbOriginalProcess
!= Process
)
1129 KeStackAttachProcess(&Process
->Pcb
, &KdbApcState
);
1131 KdbCurrentProcess
= Process
;
1137 /*!\brief Switches to another process/thread context
1139 * This function switches to the first thread in the specified process.
1141 * \param ProcessId Id of the process to switch to.
1143 * \retval TRUE Success.
1144 * \retval FALSE Failure (i.e. invalid process id)
1147 KdbpAttachToProcess(
1150 PEPROCESS Process
= NULL
;
1154 /* Get a pointer to the process */
1155 if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId
, &Process
)))
1157 KdbpPrint("Invalid process id: 0x%08x\n", (ULONG_PTR
)ProcessId
);
1161 Entry
= Process
->ThreadListHead
.Flink
;
1162 if (Entry
== &KdbCurrentProcess
->ThreadListHead
)
1164 KdbpPrint("No threads in process 0x%p, cannot attach to process!\n", ProcessId
);
1168 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1170 return KdbpAttachToThread(Thread
->Cid
.UniqueThread
);
1173 /*!\brief Calls the main loop ...
1178 KdbpCliMainLoop(KdbEnteredOnSingleStep
);
1181 /*!\brief Internal function to enter KDB.
1183 * Disables interrupts, releases display ownership, ...
1189 PVOID SavedInitialStack
, SavedStackBase
, SavedKernelStack
;
1190 ULONG SavedStackLimit
;
1193 if (KdpDebugMode
.Screen
)
1195 InbvAcquireDisplayOwnership();
1198 /* Call the interface's main loop on a different stack */
1199 Thread
= PsGetCurrentThread();
1200 SavedInitialStack
= Thread
->Tcb
.InitialStack
;
1201 SavedStackBase
= Thread
->Tcb
.StackBase
;
1202 SavedStackLimit
= Thread
->Tcb
.StackLimit
;
1203 SavedKernelStack
= Thread
->Tcb
.KernelStack
;
1204 Thread
->Tcb
.InitialStack
= Thread
->Tcb
.StackBase
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1205 Thread
->Tcb
.StackLimit
= (ULONG_PTR
)KdbStack
;
1206 Thread
->Tcb
.KernelStack
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1208 /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);*/
1210 KdbpStackSwitchAndCall(KdbStack
+ KDB_STACK_SIZE
- sizeof(ULONG
), KdbpCallMainLoop
);
1212 Thread
->Tcb
.InitialStack
= SavedInitialStack
;
1213 Thread
->Tcb
.StackBase
= SavedStackBase
;
1214 Thread
->Tcb
.StackLimit
= SavedStackLimit
;
1215 Thread
->Tcb
.KernelStack
= SavedKernelStack
;
1220 KdbpGetExceptionNumberFromStatus(IN NTSTATUS ExceptionCode
)
1224 switch (ExceptionCode
)
1226 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1229 case STATUS_SINGLE_STEP
:
1232 case STATUS_BREAKPOINT
:
1235 case STATUS_INTEGER_OVERFLOW
:
1238 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
1241 case STATUS_ILLEGAL_INSTRUCTION
:
1244 case STATUS_FLOAT_INVALID_OPERATION
:
1247 case STATUS_STACK_OVERFLOW
:
1250 case STATUS_ACCESS_VIOLATION
:
1253 case STATUS_DATATYPE_MISALIGNMENT
:
1256 case STATUS_FLOAT_MULTIPLE_TRAPS
:
1261 Ret
= RTL_NUMBER_OF(KdbEnterConditions
) - 1;
1268 /*!\brief KDB Exception filter
1270 * Called by the exception dispatcher.
1272 * \param ExceptionRecord Unused.
1273 * \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
1274 * \param Context Context, IN/OUT parameter.
1275 * \param TrapFrame Exception TrapFrame.
1276 * \param FirstChance TRUE when called before exception frames were serached,
1277 * FALSE for the second call.
1279 * \returns KD_CONTINUE_TYPE
1282 KdbEnterDebuggerException(
1283 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
,
1284 IN KPROCESSOR_MODE PreviousMode
,
1285 IN PCONTEXT Context
,
1286 IN OUT PKTRAP_FRAME TrapFrame
,
1287 IN BOOLEAN FirstChance
)
1289 KDB_ENTER_CONDITION EnterCondition
;
1290 KD_CONTINUE_TYPE ContinueType
= kdHandleException
;
1291 PKDB_BREAKPOINT BreakPoint
;
1294 BOOLEAN Resume
= FALSE
;
1295 BOOLEAN EnterConditionMet
= TRUE
;
1297 NTSTATUS ExceptionCode
;
1299 ExceptionCode
= (ExceptionRecord
!= NULL
? ExceptionRecord
->ExceptionCode
: STATUS_BREAKPOINT
);
1301 KdbCurrentProcess
= PsGetCurrentProcess();
1303 /* Set continue type to kdContinue for single steps and breakpoints */
1304 if (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
)
1305 ContinueType
= kdContinue
;
1307 /* Check if we should handle the exception. */
1308 /* FIXME - won't get all exceptions here :( */
1309 ExpNr
= KdbpGetExceptionNumberFromStatus(ExceptionCode
);
1310 EnterCondition
= KdbEnterConditions
[ExpNr
][FirstChance
? 0 : 1];
1311 if (EnterCondition
== KdbDoNotEnter
||
1312 (EnterCondition
== KdbEnterFromUmode
&& PreviousMode
== KernelMode
) ||
1313 (EnterCondition
== KdbEnterFromKmode
&& PreviousMode
!= KernelMode
))
1315 EnterConditionMet
= FALSE
;
1318 /* If we stopped on one of our breakpoints then let the user know. */
1319 KdbLastBreakPointNr
= -1;
1320 KdbEnteredOnSingleStep
= FALSE
;
1322 if (FirstChance
&& (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
) &&
1323 (KdbLastBreakPointNr
= KdbpIsBreakPointOurs(ExceptionCode
, TrapFrame
)) >= 0)
1325 BreakPoint
= KdbBreakPoints
+ KdbLastBreakPointNr
;
1327 if (ExceptionCode
== STATUS_BREAKPOINT
)
1330 * ... and restore the original instruction.
1332 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
,
1333 BreakPoint
->Data
.SavedInstruction
, NULL
)))
1335 KdbpPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
1336 KeBugCheck(0); // FIXME: Proper bugcode!
1340 if ((BreakPoint
->Type
== KdbBreakPointHardware
) &&
1341 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
))
1343 Resume
= TRUE
; /* Set the resume flag when continuing execution */
1347 * When a temporary breakpoint is hit we have to make sure that we are
1348 * in the same context in which it was set, otherwise it could happen
1349 * that another process/thread hits it before and it gets deleted.
1351 else if (BreakPoint
->Type
== KdbBreakPointTemporary
&&
1352 BreakPoint
->Process
== KdbCurrentProcess
)
1354 ASSERT((TrapFrame
->EFlags
& EFLAGS_TF
) == 0);
1357 * Delete the temporary breakpoint which was used to step over or into the instruction.
1359 KdbpDeleteBreakPoint(-1, BreakPoint
);
1361 if (--KdbNumSingleSteps
> 0)
1363 if ((KdbSingleStepOver
&& !KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1364 (!KdbSingleStepOver
&& !KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1366 Context
->EFlags
|= EFLAGS_TF
;
1368 goto continue_execution
; /* return */
1371 KdbEnteredOnSingleStep
= TRUE
;
1375 * If we hit a breakpoint set by the debugger we set the single step flag,
1376 * ignore the next single step and reenable the breakpoint.
1378 else if (BreakPoint
->Type
== KdbBreakPointSoftware
||
1379 BreakPoint
->Type
== KdbBreakPointTemporary
)
1381 ASSERT(ExceptionCode
== STATUS_BREAKPOINT
);
1382 Context
->EFlags
|= EFLAGS_TF
;
1383 KdbBreakPointToReenable
= BreakPoint
;
1387 * Make sure that the breakpoint should be triggered in this context
1389 if (!BreakPoint
->Global
&& BreakPoint
->Process
!= KdbCurrentProcess
)
1391 goto continue_execution
; /* return */
1395 * Check if the condition for the breakpoint is met.
1397 if (BreakPoint
->Condition
!= NULL
)
1399 /* Setup the KDB trap frame */
1400 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1403 if (!KdbpRpnEvaluateParsedExpression(BreakPoint
->Condition
, &KdbTrapFrame
, &ull
, NULL
, NULL
))
1405 /* FIXME: Print warning? */
1407 else if (ull
== 0) /* condition is not met */
1409 goto continue_execution
; /* return */
1413 if (BreakPoint
->Type
== KdbBreakPointSoftware
)
1415 KdbpPrint("Entered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
1416 KdbLastBreakPointNr
, TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
);
1418 else if (BreakPoint
->Type
== KdbBreakPointHardware
)
1420 KdbpPrint("Entered debugger on breakpoint #%d: %s 0x%08x\n",
1421 KdbLastBreakPointNr
,
1422 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessRead
) ? "READ" :
1423 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessWrite
) ? "WRITE" :
1424 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessReadWrite
) ? "RDWR" : "EXEC")
1431 else if (ExceptionCode
== STATUS_SINGLE_STEP
)
1433 /* Silently ignore a debugger initiated single step. */
1434 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbBreakPointToReenable
!= NULL
)
1436 /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
1437 BreakPoint
= KdbBreakPointToReenable
;
1438 KdbBreakPointToReenable
= NULL
;
1439 ASSERT(BreakPoint
->Type
== KdbBreakPointSoftware
||
1440 BreakPoint
->Type
== KdbBreakPointTemporary
);
1443 * Reenable the breakpoint we disabled to execute the breakpointed
1446 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
, 0xCC,
1447 &BreakPoint
->Data
.SavedInstruction
)))
1449 KdbpPrint("Warning: Couldn't reenable breakpoint %d\n",
1450 BreakPoint
- KdbBreakPoints
);
1453 /* Unset TF if we are no longer single stepping. */
1454 if (KdbNumSingleSteps
== 0)
1455 Context
->EFlags
&= ~EFLAGS_TF
;
1456 goto continue_execution
; /* return */
1459 /* Check if we expect a single step */
1460 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbNumSingleSteps
> 0)
1462 /*ASSERT((Context->Eflags & EFLAGS_TF) != 0);*/
1463 if (--KdbNumSingleSteps
> 0)
1465 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1466 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1468 Context
->EFlags
&= ~EFLAGS_TF
;
1472 Context
->EFlags
|= EFLAGS_TF
;
1474 goto continue_execution
; /* return */
1478 Context
->EFlags
&= ~EFLAGS_TF
;
1479 KdbEnteredOnSingleStep
= TRUE
;
1484 if (!EnterConditionMet
)
1486 return kdHandleException
;
1488 KdbpPrint("Entered debugger on unexpected debug trap!\n");
1491 else if (ExceptionCode
== STATUS_BREAKPOINT
)
1493 if (KdbInitFileBuffer
!= NULL
)
1495 KdbpCliInterpretInitFile();
1496 EnterConditionMet
= FALSE
;
1498 if (!EnterConditionMet
)
1500 return kdHandleException
;
1503 KdbpPrint("Entered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
1504 TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
- 1);
1508 CONST CHAR
*ExceptionString
= (ExpNr
< RTL_NUMBER_OF(ExceptionNrToString
)) ?
1509 (ExceptionNrToString
[ExpNr
]) :
1510 ("Unknown/User defined exception");
1512 if (!EnterConditionMet
)
1514 return ContinueType
;
1517 KdbpPrint("Entered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
1518 FirstChance
? "first" : "last", ExceptionCode
, ExceptionString
);
1519 if (ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
1520 ExceptionRecord
!= NULL
&& ExceptionRecord
->NumberParameters
!= 0)
1522 /* FIXME: Add noexec memory stuff */
1526 asm volatile("movl %%cr2, %0" : "=r"(TrapCr2
));
1529 __asm mov TrapCr2
, eax
;
1531 #error Unknown compiler for inline assembler
1534 Err
= TrapFrame
->ErrCode
;
1535 KdbpPrint("Memory at 0x%p could not be %s: ", TrapCr2
, (Err
& (1 << 1)) ? "written" : "read");
1536 if ((Err
& (1 << 0)) == 0)
1537 KdbpPrint("Page not present.\n");
1540 if ((Err
& (1 << 3)) != 0)
1541 KdbpPrint("Reserved bits in page directory set.\n");
1543 KdbpPrint("Page protection violation.\n");
1548 /* Once we enter the debugger we do not expect any more single steps to happen */
1549 KdbNumSingleSteps
= 0;
1551 /* Update the current process pointer */
1552 KdbCurrentProcess
= KdbOriginalProcess
= PsGetCurrentProcess();
1553 KdbCurrentThread
= KdbOriginalThread
= PsGetCurrentThread();
1554 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1556 /* Setup the KDB trap frame */
1557 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1559 /* Enter critical section */
1560 Ke386SaveFlags(OldEflags
);
1563 /* Exception inside the debugger? Game over. */
1564 if (InterlockedIncrement(&KdbEntryCount
) > 1)
1566 Ke386RestoreFlags(OldEflags
);
1567 return kdHandleException
;
1570 /* Call the main loop. */
1571 KdbpInternalEnter();
1573 /* Check if we should single step */
1574 if (KdbNumSingleSteps
> 0)
1576 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)) ||
1577 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)))
1579 ASSERT((KdbCurrentTrapFrame
->Tf
.EFlags
& EFLAGS_TF
) == 0);
1580 /*KdbCurrentTrapFrame->Tf.EFlags &= ~EFLAGS_TF;*/
1584 Context
->EFlags
|= EFLAGS_TF
;
1588 /* We can't update the current thread's trapframe 'cause it might not
1591 /* Detach from attached process */
1592 if (KdbCurrentProcess
!= KdbOriginalProcess
)
1594 KeUnstackDetachProcess(&KdbApcState
);
1597 /* Update the exception TrapFrame */
1598 KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame
, TrapFrame
);
1600 /* Decrement the entry count */
1601 InterlockedDecrement(&KdbEntryCount
);
1603 /* Leave critical section */
1604 Ke386RestoreFlags(OldEflags
);
1607 /* Clear debug status */
1608 if (ExceptionCode
== STATUS_BREAKPOINT
) /* FIXME: Why clear DR6 on INT3? */
1610 /* Set the RF flag so we don't trigger the same breakpoint again. */
1613 TrapFrame
->EFlags
|= EFLAGS_RF
;
1616 /* Clear dr6 status flags. */
1617 TrapFrame
->Dr6
&= ~0x0000e00f;
1619 /* Skip the current instruction */
1623 return ContinueType
;
1627 KdbDeleteProcessHook(IN PEPROCESS Process
)
1629 KdbSymFreeProcessSymbols(Process
);
1631 /* FIXME: Delete breakpoints for process */
1636 KdbpGetCommandLineSettings(PCHAR p1
)
1640 while (p1
&& (p2
= strchr(p1
, ' ')))
1644 if (!_strnicmp(p2
, "KDSERIAL", 8))
1647 KdbDebugState
|= KD_DEBUG_KDSERIAL
;
1648 KdpDebugMode
.Serial
= TRUE
;
1650 else if (!_strnicmp(p2
, "KDNOECHO", 8))
1653 KdbDebugState
|= KD_DEBUG_KDNOECHO
;
1661 KdbpSafeReadMemory(OUT PVOID Dest
,
1665 BOOLEAN Result
= TRUE
;
1673 Result
= KdpSafeReadMemory((ULONG_PTR
)Src
, Bytes
, Dest
);
1677 ULONG_PTR Start
, End
, Write
;
1678 for (Start
= (ULONG_PTR
)Src
,
1679 End
= Start
+ Bytes
,
1680 Write
= (ULONG_PTR
)Dest
;
1681 Result
&& (Start
< End
);
1683 if (!KdpSafeReadMemory(Start
, 1, (PVOID
)Write
))
1689 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;
1693 KdbpSafeWriteMemory(OUT PVOID Dest
,
1697 BOOLEAN Result
= TRUE
;
1698 ULONG_PTR Start
, End
, Write
;
1700 for (Start
= (ULONG_PTR
)Src
,
1701 End
= Start
+ Bytes
,
1702 Write
= (ULONG_PTR
)Dest
;
1703 Result
&& (Start
< End
);
1705 if (!KdpSafeWriteMemory(Write
, 1, *((PCHAR
)Start
)))
1708 return Result
? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;