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 ******************************************************************/
14 #include <internal/debug.h>
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 { KdbEnterAlways
, 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 /* Copy the TrapFrame only up to Eflags and zero the rest*/
139 RtlCopyMemory(&KdbTrapFrame
->Tf
, TrapFrame
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
140 RtlZeroMemory((PVOID
)((ULONG_PTR
)&KdbTrapFrame
->Tf
+ FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
)),
141 sizeof (KTRAP_FRAME
) - FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
143 "movl %%cr0, %0" "\n\t"
144 "movl %%cr2, %1" "\n\t"
145 "movl %%cr3, %2" "\n\t"
146 "movl %%cr4, %3" "\n\t"
147 : "=r"(KdbTrapFrame
->Cr0
), "=r"(KdbTrapFrame
->Cr2
),
148 "=r"(KdbTrapFrame
->Cr3
), "=r"(KdbTrapFrame
->Cr4
));
150 KdbTrapFrame
->Tf
.HardwareEsp
= KiEspFromTrapFrame(TrapFrame
);
151 KdbTrapFrame
->Tf
.HardwareSegSs
= (USHORT
)(KiSsFromTrapFrame(TrapFrame
) & 0xFFFF);
154 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
158 KdbpKdbTrapFrameToTrapFrame(PKDB_KTRAP_FRAME KdbTrapFrame
, PKTRAP_FRAME TrapFrame
)
160 /* Copy the TrapFrame only up to Eflags and zero the rest*/
161 RtlCopyMemory(TrapFrame
, &KdbTrapFrame
->Tf
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
163 /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */
165 KiSsToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareSegSs
);
166 KiEspToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareEsp
);
168 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
171 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
172 * the old instruction in *OldInst.
174 * \param Process Process in which's context to overwrite the instruction.
175 * \param Address Address at which to overwrite the instruction.
176 * \param NewInst New instruction (written to \a Address)
177 * \param OldInst Old instruction (read from \a Address)
182 KdbpOverwriteInstruction(
183 IN PEPROCESS Process
,
184 IN ULONG_PTR Address
,
186 OUT PUCHAR OldInst OPTIONAL
)
190 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
193 /* Get the protection for the address. */
194 Protect
= MmGetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
196 /* Return if that page isn't present. */
197 if (Protect
& PAGE_NOACCESS
)
199 return STATUS_MEMORY_NOT_ALLOCATED
;
202 /* Attach to the process */
203 if (CurrentProcess
!= Process
)
205 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
208 /* Make the page writeable if it is read only. */
209 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
211 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
),
212 (Protect
& ~(PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
)) | PAGE_READWRITE
);
215 /* Copy the old instruction back to the caller. */
218 Status
= KdbpSafeReadMemory(OldInst
, (PUCHAR
)Address
, 1);
219 if (!NT_SUCCESS(Status
))
221 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
223 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
225 /* Detach from process */
226 if (CurrentProcess
!= Process
)
234 /* Copy the new instruction in its place. */
235 Status
= KdbpSafeWriteMemory((PUCHAR
)Address
, &NewInst
, 1);
237 /* Restore the page protection. */
238 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
240 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
243 /* Detach from process */
244 if (CurrentProcess
!= Process
)
246 KeUnstackDetachProcess(&ApcState
);
252 /*!\brief Checks whether the given instruction can be single stepped or has to be
253 * stepped over using a temporary breakpoint.
255 * \retval TRUE Instruction is a call.
256 * \retval FALSE Instruction is not a call.
259 KdbpShouldStepOverInstruction(ULONG_PTR Eip
)
264 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
266 KdbpPrint("Couldn't access memory at 0x%p\n", Eip
);
270 /* Check if the current instruction is a call. */
271 while ((i
< sizeof (Mem
)) && (Mem
[i
] == 0x66 || Mem
[i
] == 0x67))
273 if (i
== sizeof (Mem
))
275 if (Mem
[i
] == 0xE8 || Mem
[i
] == 0x9A || Mem
[i
] == 0xF2 || Mem
[i
] == 0xF3 ||
276 (((i
+ 1) < sizeof (Mem
)) && Mem
[i
] == 0xFF && (Mem
[i
+1] & 0x38) == 0x10))
283 /*!\brief Steps over an instruction
285 * If the given instruction should be stepped over, this function inserts a
286 * temporary breakpoint after the instruction and returns TRUE, otherwise it
289 * \retval TRUE Temporary breakpoint set after instruction.
290 * \retval FALSE No breakpoint was set.
293 KdbpStepOverInstruction(ULONG_PTR Eip
)
297 if (!KdbpShouldStepOverInstruction(Eip
))
300 InstLen
= KdbpGetInstLength(Eip
);
304 if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip
+ InstLen
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
310 /*!\brief Steps into an instruction (interrupts)
312 * If the given instruction should be stepped into, this function inserts a
313 * temporary breakpoint at the target instruction and returns TRUE, otherwise it
316 * \retval TRUE Temporary breakpoint set at target instruction.
317 * \retval FALSE No breakpoint was set.
320 KdbpStepIntoInstruction(ULONG_PTR Eip
)
329 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
331 /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
335 /* Check for INT instruction */
336 /* FIXME: Check for iret */
339 else if (Mem
[0] == 0xcd)
341 else if (Mem
[0] == 0xce && KdbCurrentTrapFrame
->Tf
.EFlags
& (1<<11)) /* 1 << 11 is the overflow flag */
346 if (IntVect
< 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
351 /* Read the interrupt descriptor table register */
352 asm volatile("sidt %0" : : "m"(Idtr
.Limit
));
353 if (IntVect
>= (Idtr
.Limit
+ 1) / 8)
355 /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
359 /* Get the interrupt descriptor */
360 if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc
, (PVOID
)(Idtr
.Base
+ (IntVect
* 8)), sizeof (IntDesc
))))
362 /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
366 /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
367 if ((IntDesc
[1] & (1 << 15)) == 0) /* not present */
371 if ((IntDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
373 /* FIXME: Task gates not supported */
376 else if (((IntDesc
[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
377 ((IntDesc
[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
379 /* FIXME: Should the segment selector of the interrupt gate be checked? */
380 TargetEip
= (IntDesc
[1] & 0xffff0000) | (IntDesc
[0] & 0x0000ffff);
387 /* Insert breakpoint */
388 if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
394 /*!\brief Gets the number of the next breakpoint >= Start.
396 * \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
398 * \returns Breakpoint number (-1 if no more breakpoints are found)
401 KdbpGetNextBreakPointNr(
402 IN ULONG Start OPTIONAL
)
404 for (; Start
< RTL_NUMBER_OF(KdbBreakPoints
); Start
++)
406 if (KdbBreakPoints
[Start
].Type
!= KdbBreakPointNone
)
412 /*!\brief Returns information of the specified breakpoint.
414 * \param BreakPointNr Number of the breakpoint to return information of.
415 * \param Address Receives the address of the breakpoint.
416 * \param Type Receives the type of the breakpoint (hardware or software)
417 * \param Size Size - for memory breakpoints.
418 * \param AccessType Access type - for hardware breakpoints.
419 * \param DebugReg Debug register - for enabled hardware breakpoints.
420 * \param Enabled Whether the breakpoint is enabled or not.
421 * \param Process The owning process of the breakpoint.
422 * \param ConditionExpression The expression which was given as condition for the bp.
424 * \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
427 KdbpGetBreakPointInfo(
428 IN ULONG BreakPointNr
,
429 OUT ULONG_PTR
*Address OPTIONAL
,
430 OUT KDB_BREAKPOINT_TYPE
*Type OPTIONAL
,
431 OUT UCHAR
*Size OPTIONAL
,
432 OUT KDB_ACCESS_TYPE
*AccessType OPTIONAL
,
433 OUT UCHAR
*DebugReg OPTIONAL
,
434 OUT BOOLEAN
*Enabled OPTIONAL
,
435 OUT BOOLEAN
*Global OPTIONAL
,
436 OUT PEPROCESS
*Process OPTIONAL
,
437 OUT PCHAR
*ConditionExpression OPTIONAL
)
441 if (BreakPointNr
>= RTL_NUMBER_OF(KdbBreakPoints
) ||
442 KdbBreakPoints
[BreakPointNr
].Type
== KdbBreakPointNone
)
447 bp
= KdbBreakPoints
+ BreakPointNr
;
449 *Address
= bp
->Address
;
452 if (bp
->Type
== KdbBreakPointHardware
)
455 *Size
= bp
->Data
.Hw
.Size
;
456 if (AccessType
!= NULL
)
457 *AccessType
= bp
->Data
.Hw
.AccessType
;
458 if (DebugReg
!= NULL
&& bp
->Enabled
)
459 *DebugReg
= bp
->Data
.Hw
.DebugReg
;
462 *Enabled
= bp
->Enabled
;
464 *Global
= bp
->Global
;
466 *Process
= bp
->Process
;
467 if (ConditionExpression
!= NULL
)
468 *ConditionExpression
= bp
->ConditionExpression
;
473 /*!\brief Inserts a breakpoint into the breakpoint array.
475 * The \a Process of the breakpoint is set to \a KdbCurrentProcess
477 * \param Address Address at which to set the breakpoint.
478 * \param Type Type of breakpoint (hardware or software)
479 * \param Size Size of breakpoint (for hardware/memory breakpoints)
480 * \param AccessType Access type (for hardware breakpoins)
481 * \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
482 * \param Global Wether the breakpoint is global or local to a process.
483 * \param BreakPointNumber Receives the breakpoint number on success
488 KdbpInsertBreakPoint(
489 IN ULONG_PTR Address
,
490 IN KDB_BREAKPOINT_TYPE Type
,
491 IN UCHAR Size OPTIONAL
,
492 IN KDB_ACCESS_TYPE AccessType OPTIONAL
,
493 IN PCHAR ConditionExpression OPTIONAL
,
495 OUT PULONG BreakPointNumber OPTIONAL
)
499 PCHAR ConditionExpressionDup
;
503 ASSERT(Type
!= KdbBreakPointNone
);
505 if (Type
== KdbBreakPointHardware
)
507 if ((Address
% Size
) != 0)
509 KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address
, Size
);
510 return STATUS_UNSUCCESSFUL
;
512 if (AccessType
== KdbAccessExec
&& Size
!= 1)
514 KdbpPrint("Size must be 1 for execution breakpoints.\n");
515 return STATUS_UNSUCCESSFUL
;
519 if (KdbBreakPointCount
== KDB_MAXIMUM_BREAKPOINT_COUNT
)
521 return STATUS_UNSUCCESSFUL
;
524 /* Parse conditon expression string and duplicate it */
525 if (ConditionExpression
!= NULL
)
527 Condition
= KdbpRpnParseExpression(ConditionExpression
, &ErrOffset
, ErrMsg
);
528 if (Condition
== NULL
)
531 KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg
, ErrOffset
);
533 KdbpPrint("Couldn't parse expression: %s", ErrMsg
);
534 return STATUS_UNSUCCESSFUL
;
537 i
= strlen(ConditionExpression
) + 1;
538 ConditionExpressionDup
= ExAllocatePoolWithTag(NonPagedPool
, i
, TAG_KDBG
);
539 RtlCopyMemory(ConditionExpressionDup
, ConditionExpression
, i
);
545 ConditionExpressionDup
= NULL
;
548 /* Find unused breakpoint */
549 if (Type
== KdbBreakPointTemporary
)
551 for (i
= RTL_NUMBER_OF(KdbBreakPoints
) - 1; i
>= 0; i
--)
553 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
559 for (i
= 0; i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
); i
++)
561 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
565 ASSERT(i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
));
567 /* Set the breakpoint */
568 ASSERT(KdbCurrentProcess
!= NULL
);
569 KdbBreakPoints
[i
].Type
= Type
;
570 KdbBreakPoints
[i
].Address
= Address
;
571 KdbBreakPoints
[i
].Enabled
= FALSE
;
572 KdbBreakPoints
[i
].Global
= Global
;
573 KdbBreakPoints
[i
].Process
= KdbCurrentProcess
;
574 KdbBreakPoints
[i
].ConditionExpression
= ConditionExpressionDup
;
575 KdbBreakPoints
[i
].Condition
= Condition
;
576 if (Type
== KdbBreakPointHardware
)
579 KdbBreakPoints
[i
].Data
.Hw
.Size
= Size
;
580 KdbBreakPoints
[i
].Data
.Hw
.AccessType
= AccessType
;
582 KdbBreakPointCount
++;
584 if (Type
!= KdbBreakPointTemporary
)
585 KdbpPrint("Breakpoint %d inserted.\n", i
);
587 /* Try to enable the breakpoint */
588 KdbpEnableBreakPoint(i
, NULL
);
590 /* Return the breakpoint number */
591 if (BreakPointNumber
!= NULL
)
592 *BreakPointNumber
= i
;
594 return STATUS_SUCCESS
;
597 /*!\brief Deletes a breakpoint
599 * \param BreakPointNr Number of the breakpoint to delete. Can be -1
600 * \param BreakPoint Breakpoint to delete. Can be NULL.
602 * \retval TRUE Success.
603 * \retval FALSE Failure (invalid breakpoint number)
606 KdbpDeleteBreakPoint(
607 IN LONG BreakPointNr OPTIONAL
,
608 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
610 if (BreakPointNr
< 0)
612 ASSERT(BreakPoint
!= NULL
);
613 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
615 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
617 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
620 if (BreakPoint
== NULL
)
622 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
624 if (BreakPoint
->Type
== KdbBreakPointNone
)
626 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
630 if (BreakPoint
->Enabled
&&
631 !KdbpDisableBreakPoint(-1, BreakPoint
))
634 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
635 KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr
);
636 BreakPoint
->Type
= KdbBreakPointNone
;
637 KdbBreakPointCount
--;
642 /*!\brief Checks if the breakpoint was set by the debugger
644 * Tries to find a breakpoint in the breakpoint array which caused
645 * the debug exception to happen.
647 * \param ExpNr Exception Number (1 or 3)
648 * \param TrapFrame Exception trapframe
650 * \returns Breakpoint number, -1 on error.
653 KdbpIsBreakPointOurs(
654 IN NTSTATUS ExceptionCode
,
655 IN PKTRAP_FRAME TrapFrame
)
658 ASSERT(ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
);
660 if (ExceptionCode
== STATUS_BREAKPOINT
) /* Software interrupt */
662 ULONG_PTR BpEip
= (ULONG_PTR
)TrapFrame
->Eip
- 1; /* Get EIP of INT3 instruction */
663 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
665 ASSERT((KdbSwBreakPoints
[i
]->Type
== KdbBreakPointSoftware
||
666 KdbSwBreakPoints
[i
]->Type
== KdbBreakPointTemporary
));
667 ASSERT(KdbSwBreakPoints
[i
]->Enabled
);
668 if (KdbSwBreakPoints
[i
]->Address
== BpEip
)
670 return KdbSwBreakPoints
[i
] - KdbBreakPoints
;
674 else if (ExceptionCode
== STATUS_SINGLE_STEP
) /* Hardware interrupt */
677 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
679 ASSERT(KdbHwBreakPoints
[i
]->Type
== KdbBreakPointHardware
&&
680 KdbHwBreakPoints
[i
]->Enabled
);
681 DebugReg
= KdbHwBreakPoints
[i
]->Data
.Hw
.DebugReg
;
682 if ((TrapFrame
->Dr6
& (1 << DebugReg
)) != 0)
684 return KdbHwBreakPoints
[i
] - KdbBreakPoints
;
692 /*!\brief Enables a breakpoint.
694 * \param BreakPointNr Number of the breakpoint to enable Can be -1.
695 * \param BreakPoint Breakpoint to enable. Can be NULL.
697 * \retval TRUE Success.
698 * \retval FALSE Failure.
700 * \sa KdbpDisableBreakPoint
703 KdbpEnableBreakPoint(
704 IN LONG BreakPointNr OPTIONAL
,
705 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
711 if (BreakPointNr
< 0)
713 ASSERT(BreakPoint
!= NULL
);
714 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
716 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
718 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
721 if (BreakPoint
== NULL
)
723 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
725 if (BreakPoint
->Type
== KdbBreakPointNone
)
727 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
731 if (BreakPoint
->Enabled
== TRUE
)
733 KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr
);
737 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
738 BreakPoint
->Type
== KdbBreakPointTemporary
)
740 if (KdbSwBreakPointCount
>= KDB_MAXIMUM_SW_BREAKPOINT_COUNT
)
742 KdbpPrint("Maximum number of SW breakpoints (%d) used. "
743 "Disable another breakpoint in order to enable this one.\n",
744 KDB_MAXIMUM_SW_BREAKPOINT_COUNT
);
747 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
748 0xCC, &BreakPoint
->Data
.SavedInstruction
);
749 if (!NT_SUCCESS(Status
))
751 KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint
->Address
);
754 KdbSwBreakPoints
[KdbSwBreakPointCount
++] = BreakPoint
;
758 if (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
)
759 ASSERT(BreakPoint
->Data
.Hw
.Size
== 1);
760 ASSERT((BreakPoint
->Address
% BreakPoint
->Data
.Hw
.Size
) == 0);
761 if (KdbHwBreakPointCount
>= KDB_MAXIMUM_HW_BREAKPOINT_COUNT
)
763 KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
764 "Disable another breakpoint in order to enable this one.\n",
765 KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
769 /* Find unused hw breakpoint */
770 ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT
== 4);
771 for (i
= 0; i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
; i
++)
773 if ((KdbTrapFrame
.Tf
.Dr7
& (0x3 << (i
* 2))) == 0)
776 ASSERT(i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
778 /* Set the breakpoint address. */
782 KdbTrapFrame
.Tf
.Dr0
= BreakPoint
->Address
;
785 KdbTrapFrame
.Tf
.Dr1
= BreakPoint
->Address
;
788 KdbTrapFrame
.Tf
.Dr2
= BreakPoint
->Address
;
791 KdbTrapFrame
.Tf
.Dr3
= BreakPoint
->Address
;
795 /* Enable the global breakpoint */
796 KdbTrapFrame
.Tf
.Dr7
|= (0x2 << (i
* 2));
798 /* Enable the exact match bits. */
799 KdbTrapFrame
.Tf
.Dr7
|= 0x00000300;
801 /* Clear existing state. */
802 KdbTrapFrame
.Tf
.Dr7
&= ~(0xF << (16 + (i
* 4)));
804 /* Set the breakpoint type. */
805 switch (BreakPoint
->Data
.Hw
.AccessType
)
814 case KdbAccessReadWrite
:
822 KdbTrapFrame
.Tf
.Dr7
|= (ul
<< (16 + (i
* 4)));
824 /* Set the breakpoint length. */
825 KdbTrapFrame
.Tf
.Dr7
|= ((BreakPoint
->Data
.Hw
.Size
- 1) << (18 + (i
* 4)));
827 /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
828 if (&KdbTrapFrame
!= KdbCurrentTrapFrame
)
830 KdbCurrentTrapFrame
->Tf
.Dr0
= KdbTrapFrame
.Tf
.Dr0
;
831 KdbCurrentTrapFrame
->Tf
.Dr1
= KdbTrapFrame
.Tf
.Dr1
;
832 KdbCurrentTrapFrame
->Tf
.Dr2
= KdbTrapFrame
.Tf
.Dr2
;
833 KdbCurrentTrapFrame
->Tf
.Dr3
= KdbTrapFrame
.Tf
.Dr3
;
834 KdbCurrentTrapFrame
->Tf
.Dr6
= KdbTrapFrame
.Tf
.Dr6
;
835 KdbCurrentTrapFrame
->Tf
.Dr7
= KdbTrapFrame
.Tf
.Dr7
;
838 BreakPoint
->Data
.Hw
.DebugReg
= i
;
839 KdbHwBreakPoints
[KdbHwBreakPointCount
++] = BreakPoint
;
842 BreakPoint
->Enabled
= TRUE
;
843 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
844 KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr
);
848 /*!\brief Disables a breakpoint.
850 * \param BreakPointNr Number of the breakpoint to disable. Can be -1
851 * \param BreakPoint Breakpoint to disable. Can be NULL.
853 * \retval TRUE Success.
854 * \retval FALSE Failure.
856 * \sa KdbpEnableBreakPoint
859 KdbpDisableBreakPoint(
860 IN LONG BreakPointNr OPTIONAL
,
861 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
866 if (BreakPointNr
< 0)
868 ASSERT(BreakPoint
!= NULL
);
869 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
871 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
873 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
876 if (BreakPoint
== NULL
)
878 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
880 if (BreakPoint
->Type
== KdbBreakPointNone
)
882 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
886 if (BreakPoint
->Enabled
== FALSE
)
888 KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr
);
892 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
893 BreakPoint
->Type
== KdbBreakPointTemporary
)
895 ASSERT(KdbSwBreakPointCount
> 0);
896 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
897 BreakPoint
->Data
.SavedInstruction
, NULL
);
898 if (!NT_SUCCESS(Status
))
900 KdbpPrint("Couldn't restore original instruction.\n");
904 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
906 if (KdbSwBreakPoints
[i
] == BreakPoint
)
908 KdbSwBreakPoints
[i
] = KdbSwBreakPoints
[--KdbSwBreakPointCount
];
909 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
913 if (i
!= (UINT
)-1) /* not found */
918 ASSERT(BreakPoint
->Type
== KdbBreakPointHardware
);
920 /* Clear the breakpoint. */
921 KdbTrapFrame
.Tf
.Dr7
&= ~(0x3 << (BreakPoint
->Data
.Hw
.DebugReg
* 2));
922 if ((KdbTrapFrame
.Tf
.Dr7
& 0xFF) == 0)
925 * If no breakpoints are enabled then clear the exact match flags.
927 KdbTrapFrame
.Tf
.Dr7
&= 0xFFFFFCFF;
930 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
932 if (KdbHwBreakPoints
[i
] == BreakPoint
)
934 KdbHwBreakPoints
[i
] = KdbHwBreakPoints
[--KdbHwBreakPointCount
];
935 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
939 if (i
!= (UINT
)-1) /* not found */
943 BreakPoint
->Enabled
= FALSE
;
944 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
945 KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr
);
949 /*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
951 * \param ExceptionNr Number of the exception to get condition of.
952 * \param FirstChance Whether to get first or last chance condition.
953 * \param Condition Receives the condition setting.
955 * \retval TRUE Success.
956 * \retval FALSE Failure (invalid exception nr)
959 KdbpGetEnterCondition(
961 IN BOOLEAN FirstChance
,
962 OUT KDB_ENTER_CONDITION
*Condition
)
964 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
))
967 *Condition
= KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1];
971 /*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
973 * \param ExceptionNr Number of the exception to set condition of (-1 for all)
974 * \param FirstChance Whether to set first or last chance condition.
975 * \param Condition The new condition setting.
977 * \retval TRUE Success.
978 * \retval FALSE Failure (invalid exception nr)
981 KdbpSetEnterCondition(
983 IN BOOLEAN FirstChance
,
984 IN KDB_ENTER_CONDITION Condition
)
988 for (ExceptionNr
= 0; ExceptionNr
< (LONG
)RTL_NUMBER_OF(KdbEnterConditions
); ExceptionNr
++)
990 if (ExceptionNr
== 1 || ExceptionNr
== 8 ||
991 ExceptionNr
== 9 || ExceptionNr
== 15) /* Reserved exceptions */
995 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1000 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
) ||
1001 ExceptionNr
== 1 || ExceptionNr
== 8 || /* Do not allow changing of the debug */
1002 ExceptionNr
== 9 || ExceptionNr
== 15) /* trap or reserved exceptions */
1006 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1011 /*!\brief Switches to another thread context
1013 * \param ThreadId Id of the thread to switch to.
1015 * \retval TRUE Success.
1016 * \retval FALSE Failure (i.e. invalid thread id)
1022 PETHREAD Thread
= NULL
;
1025 /* Get a pointer to the thread */
1026 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &Thread
)))
1028 KdbpPrint("Invalid thread id: 0x%08x\n", (UINT
)ThreadId
);
1031 Process
= Thread
->ThreadsProcess
;
1033 if (KeIsExecutingDpc() && Process
!= KdbCurrentProcess
)
1035 KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
1039 /* Save the current thread's context (if we previously attached to a thread) */
1040 if (KdbCurrentThread
!= KdbOriginalThread
)
1042 ASSERT(KdbCurrentTrapFrame
== &KdbThreadTrapFrame
);
1043 KdbpKdbTrapFrameToTrapFrame(KdbCurrentTrapFrame
, KdbCurrentThread
->Tcb
.TrapFrame
);
1047 ASSERT(KdbCurrentTrapFrame
== &KdbTrapFrame
);
1050 /* Switch to the thread's context */
1051 if (Thread
!= KdbOriginalThread
)
1053 if (Thread
->Tcb
.TrapFrame
== NULL
)
1055 KdbpPrint("Threads TrapFrame is NULL! Cannot attach.\n");
1058 KdbpTrapFrameToKdbTrapFrame(Thread
->Tcb
.TrapFrame
, &KdbThreadTrapFrame
);
1059 KdbCurrentTrapFrame
= &KdbThreadTrapFrame
;
1061 else /* Switching back to original thread */
1063 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1065 KdbCurrentThread
= Thread
;
1067 /* Attach to the thread's process */
1068 ASSERT(KdbCurrentProcess
== PsGetCurrentProcess());
1069 if (KdbCurrentProcess
!= Process
)
1071 if (KdbCurrentProcess
!= KdbOriginalProcess
) /* detach from previously attached process */
1073 KeUnstackDetachProcess(&KdbApcState
);
1075 if (KdbOriginalProcess
!= Process
)
1077 KeStackAttachProcess(&Process
->Pcb
, &KdbApcState
);
1079 KdbCurrentProcess
= Process
;
1085 /*!\brief Switches to another process/thread context
1087 * This function switches to the first thread in the specified process.
1089 * \param ProcessId Id of the process to switch to.
1091 * \retval TRUE Success.
1092 * \retval FALSE Failure (i.e. invalid process id)
1095 KdbpAttachToProcess(
1098 PEPROCESS Process
= NULL
;
1102 /* Get a pointer to the process */
1103 if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId
, &Process
)))
1105 KdbpPrint("Invalid process id: 0x%08x\n", (UINT
)ProcessId
);
1109 Entry
= Process
->ThreadListHead
.Flink
;
1110 if (Entry
== &KdbCurrentProcess
->ThreadListHead
)
1112 KdbpPrint("No threads in process 0x%08x, cannot attach to process!\n", (UINT
)ProcessId
);
1116 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1118 return KdbpAttachToThread(Thread
->Cid
.UniqueThread
);
1121 /*!\brief Calls the main loop ...
1126 KdbpCliMainLoop(KdbEnteredOnSingleStep
);
1129 /*!\brief Internal function to enter KDB.
1131 * Disables interrupts, releases display ownership, ...
1137 PVOID SavedInitialStack
, SavedStackBase
, SavedKernelStack
;
1138 ULONG SavedStackLimit
;
1141 if (KdpDebugMode
.Screen
)
1143 InbvAcquireDisplayOwnership();
1146 /* Call the interface's main loop on a different stack */
1147 Thread
= PsGetCurrentThread();
1148 SavedInitialStack
= Thread
->Tcb
.InitialStack
;
1149 SavedStackBase
= Thread
->Tcb
.StackBase
;
1150 SavedStackLimit
= Thread
->Tcb
.StackLimit
;
1151 SavedKernelStack
= Thread
->Tcb
.KernelStack
;
1152 Thread
->Tcb
.InitialStack
= Thread
->Tcb
.StackBase
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1153 Thread
->Tcb
.StackLimit
= (ULONG
)KdbStack
;
1154 Thread
->Tcb
.KernelStack
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1156 /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase);*/
1158 KdbpStackSwitchAndCall(Thread
->Tcb
.KernelStack
, KdbpCallMainLoop
);
1160 Thread
->Tcb
.InitialStack
= SavedInitialStack
;
1161 Thread
->Tcb
.StackBase
= SavedStackBase
;
1162 Thread
->Tcb
.StackLimit
= SavedStackLimit
;
1163 Thread
->Tcb
.KernelStack
= SavedKernelStack
;
1168 KdbpGetExceptionNumberFromStatus(IN NTSTATUS ExceptionCode
)
1172 switch (ExceptionCode
)
1174 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1177 case STATUS_SINGLE_STEP
:
1180 case STATUS_BREAKPOINT
:
1183 case STATUS_INTEGER_OVERFLOW
:
1186 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
1189 case STATUS_ILLEGAL_INSTRUCTION
:
1192 case STATUS_FLOAT_INVALID_OPERATION
:
1195 case STATUS_STACK_OVERFLOW
:
1198 case STATUS_ACCESS_VIOLATION
:
1201 case STATUS_DATATYPE_MISALIGNMENT
:
1204 case STATUS_FLOAT_MULTIPLE_TRAPS
:
1209 Ret
= RTL_NUMBER_OF(KdbEnterConditions
) - 1;
1216 /*!\brief KDB Exception filter
1218 * Called by the exception dispatcher.
1220 * \param ExceptionRecord Unused.
1221 * \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
1222 * \param Context Unused.
1223 * \param TrapFrame Exception TrapFrame.
1224 * \param FirstChance TRUE when called before exception frames were serached,
1225 * FALSE for the second call.
1227 * \returns KD_CONTINUE_TYPE
1230 KdbEnterDebuggerException(
1231 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
,
1232 IN KPROCESSOR_MODE PreviousMode
,
1233 IN PCONTEXT Context OPTIONAL
,
1234 IN OUT PKTRAP_FRAME TrapFrame
,
1235 IN BOOLEAN FirstChance
)
1237 KDB_ENTER_CONDITION EnterCondition
;
1238 KD_CONTINUE_TYPE ContinueType
= kdHandleException
;
1239 PKDB_BREAKPOINT BreakPoint
;
1242 BOOLEAN Resume
= FALSE
;
1243 BOOLEAN EnterConditionMet
= TRUE
;
1245 NTSTATUS ExceptionCode
;
1247 ExceptionCode
= (ExceptionRecord
!= NULL
? ExceptionRecord
->ExceptionCode
: STATUS_BREAKPOINT
);
1249 KdbCurrentProcess
= PsGetCurrentProcess();
1251 /* Set continue type to kdContinue for single steps and breakpoints */
1252 if (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
)
1253 ContinueType
= kdContinue
;
1255 /* Check if we should handle the exception. */
1256 /* FIXME - won't get all exceptions here :( */
1257 ExpNr
= KdbpGetExceptionNumberFromStatus(ExceptionCode
);
1258 EnterCondition
= KdbEnterConditions
[ExpNr
][FirstChance
? 0 : 1];
1259 if (EnterCondition
== KdbDoNotEnter
||
1260 (EnterCondition
== KdbEnterFromUmode
&& PreviousMode
== KernelMode
) ||
1261 (EnterCondition
== KdbEnterFromKmode
&& PreviousMode
!= KernelMode
))
1263 EnterConditionMet
= FALSE
;
1266 /* If we stopped on one of our breakpoints then let the user know. */
1267 KdbLastBreakPointNr
= -1;
1268 KdbEnteredOnSingleStep
= FALSE
;
1270 if (FirstChance
&& (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
) &&
1271 (KdbLastBreakPointNr
= KdbpIsBreakPointOurs(ExceptionCode
, TrapFrame
)) >= 0)
1273 BreakPoint
= KdbBreakPoints
+ KdbLastBreakPointNr
;
1275 if (ExceptionCode
== STATUS_BREAKPOINT
)
1278 * The breakpoint will point to the next instruction by default so
1279 * point it back to the start of original instruction.
1284 * ... and restore the original instruction.
1286 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
,
1287 BreakPoint
->Data
.SavedInstruction
, NULL
)))
1289 DbgPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
1294 if ((BreakPoint
->Type
== KdbBreakPointHardware
) &&
1295 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
))
1297 Resume
= TRUE
; /* Set the resume flag when continuing execution */
1301 * When a temporary breakpoint is hit we have to make sure that we are
1302 * in the same context in which it was set, otherwise it could happen
1303 * that another process/thread hits it before and it gets deleted.
1305 else if (BreakPoint
->Type
== KdbBreakPointTemporary
&&
1306 BreakPoint
->Process
== KdbCurrentProcess
)
1308 ASSERT((TrapFrame
->EFlags
& X86_EFLAGS_TF
) == 0);
1311 * Delete the temporary breakpoint which was used to step over or into the instruction.
1313 KdbpDeleteBreakPoint(-1, BreakPoint
);
1315 if (--KdbNumSingleSteps
> 0)
1317 if ((KdbSingleStepOver
&& !KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1318 (!KdbSingleStepOver
&& !KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1320 TrapFrame
->EFlags
|= X86_EFLAGS_TF
;
1322 goto continue_execution
; /* return */
1325 KdbEnteredOnSingleStep
= TRUE
;
1329 * If we hit a breakpoint set by the debugger we set the single step flag,
1330 * ignore the next single step and reenable the breakpoint.
1332 else if (BreakPoint
->Type
== KdbBreakPointSoftware
||
1333 BreakPoint
->Type
== KdbBreakPointTemporary
)
1335 ASSERT(ExceptionCode
== STATUS_BREAKPOINT
);
1336 TrapFrame
->EFlags
|= X86_EFLAGS_TF
;
1337 KdbBreakPointToReenable
= BreakPoint
;
1341 * Make sure that the breakpoint should be triggered in this context
1343 if (!BreakPoint
->Global
&& BreakPoint
->Process
!= KdbCurrentProcess
)
1345 goto continue_execution
; /* return */
1349 * Check if the condition for the breakpoint is met.
1351 if (BreakPoint
->Condition
!= NULL
)
1353 /* Setup the KDB trap frame */
1354 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1357 if (!KdbpRpnEvaluateParsedExpression(BreakPoint
->Condition
, &KdbTrapFrame
, &ull
, NULL
, NULL
))
1359 /* FIXME: Print warning? */
1361 else if (ull
== 0) /* condition is not met */
1363 goto continue_execution
; /* return */
1367 if (BreakPoint
->Type
== KdbBreakPointSoftware
)
1369 DbgPrint("Entered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
1370 KdbLastBreakPointNr
, TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
);
1372 else if (BreakPoint
->Type
== KdbBreakPointHardware
)
1374 DbgPrint("Entered debugger on breakpoint #%d: %s 0x%08x\n",
1375 KdbLastBreakPointNr
,
1376 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessRead
) ? "READ" :
1377 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessWrite
) ? "WRITE" :
1378 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessReadWrite
) ? "RDWR" : "EXEC")
1385 else if (ExceptionCode
== STATUS_SINGLE_STEP
)
1387 /* Silently ignore a debugger initiated single step. */
1388 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbBreakPointToReenable
!= NULL
)
1390 /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
1391 BreakPoint
= KdbBreakPointToReenable
;
1392 KdbBreakPointToReenable
= NULL
;
1393 ASSERT(BreakPoint
->Type
== KdbBreakPointSoftware
||
1394 BreakPoint
->Type
== KdbBreakPointTemporary
);
1397 * Reenable the breakpoint we disabled to execute the breakpointed
1400 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
, 0xCC,
1401 &BreakPoint
->Data
.SavedInstruction
)))
1403 DbgPrint("Warning: Couldn't reenable breakpoint %d\n",
1404 BreakPoint
- KdbBreakPoints
);
1407 /* Unset TF if we are no longer single stepping. */
1408 if (KdbNumSingleSteps
== 0)
1409 TrapFrame
->EFlags
&= ~X86_EFLAGS_TF
;
1410 goto continue_execution
; /* return */
1413 /* Check if we expect a single step */
1414 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbNumSingleSteps
> 0)
1416 /*ASSERT((TrapFrame->Eflags & X86_EFLAGS_TF) != 0);*/
1417 if (--KdbNumSingleSteps
> 0)
1419 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1420 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1422 TrapFrame
->EFlags
&= ~X86_EFLAGS_TF
;
1426 TrapFrame
->EFlags
|= X86_EFLAGS_TF
;
1428 goto continue_execution
; /* return */
1431 TrapFrame
->EFlags
&= ~X86_EFLAGS_TF
;
1432 KdbEnteredOnSingleStep
= TRUE
;
1436 if (!EnterConditionMet
)
1438 return ContinueType
;
1440 DbgPrint("Entered debugger on unexpected debug trap!\n");
1443 else if (ExceptionCode
== STATUS_BREAKPOINT
)
1445 if (KdbInitFileBuffer
!= NULL
)
1447 KdbpCliInterpretInitFile();
1448 EnterConditionMet
= FALSE
;
1450 if (!EnterConditionMet
)
1452 return ContinueType
;
1455 DbgPrint("Entered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
1456 TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
- 1);
1460 CONST CHAR
*ExceptionString
= (ExpNr
< RTL_NUMBER_OF(ExceptionNrToString
)) ?
1461 (ExceptionNrToString
[ExpNr
]) :
1462 ("Unknown/User defined exception");
1464 if (!EnterConditionMet
)
1466 return ContinueType
;
1469 DbgPrint("Entered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
1470 FirstChance
? "first" : "last", ExceptionCode
, ExceptionString
);
1471 if (ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
1472 ExceptionRecord
!= NULL
&& ExceptionRecord
->NumberParameters
!= 0)
1474 /* FIXME: Add noexec memory stuff */
1477 asm volatile("movl %%cr2, %0" : "=r"(Cr2
));
1478 Err
= TrapFrame
->ErrCode
;
1479 DbgPrint("Memory at 0x%p could not be %s: ", Cr2
, (Err
& (1 << 1)) ? "written" : "read");
1480 if ((Err
& (1 << 0)) == 0)
1481 DbgPrint("Page not present.\n");
1484 if ((Err
& (1 << 3)) != 0)
1485 DbgPrint("Reserved bits in page directory set.\n");
1487 DbgPrint("Page protection violation.\n");
1492 /* Once we enter the debugger we do not expect any more single steps to happen */
1493 KdbNumSingleSteps
= 0;
1495 /* Update the current process pointer */
1496 KdbCurrentProcess
= KdbOriginalProcess
= PsGetCurrentProcess();
1497 KdbCurrentThread
= KdbOriginalThread
= PsGetCurrentThread();
1498 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1500 /* Setup the KDB trap frame */
1501 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1503 /* Enter critical section */
1504 Ke386SaveFlags(OldEflags
);
1507 /* Exception inside the debugger? Game over. */
1508 if (InterlockedIncrement(&KdbEntryCount
) > 1)
1510 Ke386RestoreFlags(OldEflags
);
1511 return kdHandleException
;
1514 /* Call the main loop. */
1515 KdbpInternalEnter();
1517 /* Check if we should single step */
1518 if (KdbNumSingleSteps
> 0)
1520 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)) ||
1521 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)))
1523 ASSERT((KdbCurrentTrapFrame
->Tf
.EFlags
& X86_EFLAGS_TF
) == 0);
1524 /*KdbCurrentTrapFrame->Tf.EFlags &= ~X86_EFLAGS_TF;*/
1528 KdbCurrentTrapFrame
->Tf
.EFlags
|= X86_EFLAGS_TF
;
1532 /* Save the current thread's trapframe */
1533 if (KdbCurrentTrapFrame
== &KdbThreadTrapFrame
)
1535 KdbpKdbTrapFrameToTrapFrame(KdbCurrentTrapFrame
, KdbCurrentThread
->Tcb
.TrapFrame
);
1538 /* Detach from attached process */
1539 if (KdbCurrentProcess
!= KdbOriginalProcess
)
1541 KeUnstackDetachProcess(&KdbApcState
);
1544 /* Update the exception TrapFrame */
1545 KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame
, TrapFrame
);
1547 /* Decrement the entry count */
1548 InterlockedDecrement(&KdbEntryCount
);
1550 /* Leave critical section */
1551 Ke386RestoreFlags(OldEflags
);
1554 /* Clear debug status */
1555 if (ExceptionCode
== STATUS_SINGLE_STEP
|| ExceptionCode
== STATUS_BREAKPOINT
) /* FIXME: Why clear DR6 on INT3? */
1557 /* Set the RF flag so we don't trigger the same breakpoint again. */
1560 TrapFrame
->EFlags
|= X86_EFLAGS_RF
;
1563 /* Clear dr6 status flags. */
1564 TrapFrame
->Dr6
&= ~0x0000e00f;
1568 return ContinueType
;
1572 KdbDeleteProcessHook(IN PEPROCESS Process
)
1574 KdbSymFreeProcessSymbols(Process
);
1576 /* FIXME: Delete breakpoints for process */
1581 KdbpGetCommandLineSettings(PCHAR p1
)
1585 while (p1
&& (p2
= strchr(p1
, '/')))
1589 if (!_strnicmp(p2
, "KDSERIAL", 8))
1592 KdbDebugState
|= KD_DEBUG_KDSERIAL
;
1593 KdpDebugMode
.Serial
= TRUE
;
1595 else if (!_strnicmp(p2
, "KDNOECHO", 8))
1598 KdbDebugState
|= KD_DEBUG_KDNOECHO
;
1606 KdbpSafeReadMemory(OUT PVOID Dest
,
1610 NTSTATUS Status
= STATUS_SUCCESS
;
1620 Status
= _SEH_GetExceptionCode();
1628 KdbpSafeWriteMemory(OUT PVOID Dest
,
1632 NTSTATUS Status
= STATUS_SUCCESS
;
1642 Status
= _SEH_GetExceptionCode();