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 PCHAR 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 /* FUNCTIONS *****************************************************************/
128 KdbpTrapFrameToKdbTrapFrame(PKTRAP_FRAME TrapFrame
, PKDB_KTRAP_FRAME KdbTrapFrame
)
130 /* Copy the TrapFrame only up to Eflags and zero the rest*/
131 RtlCopyMemory(&KdbTrapFrame
->Tf
, TrapFrame
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
132 RtlZeroMemory((PVOID
)((ULONG_PTR
)&KdbTrapFrame
->Tf
+ FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
)),
133 sizeof (KTRAP_FRAME
) - FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
135 "movl %%cr0, %0" "\n\t"
136 "movl %%cr2, %1" "\n\t"
137 "movl %%cr3, %2" "\n\t"
138 "movl %%cr4, %3" "\n\t"
139 : "=r"(KdbTrapFrame
->Cr0
), "=r"(KdbTrapFrame
->Cr2
),
140 "=r"(KdbTrapFrame
->Cr3
), "=r"(KdbTrapFrame
->Cr4
));
142 KdbTrapFrame
->Tf
.HardwareEsp
= KiEspFromTrapFrame(TrapFrame
);
143 KdbTrapFrame
->Tf
.HardwareSegSs
= (USHORT
)(KiSsFromTrapFrame(TrapFrame
) & 0xFFFF);
146 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
150 KdbpKdbTrapFrameToTrapFrame(PKDB_KTRAP_FRAME KdbTrapFrame
, PKTRAP_FRAME TrapFrame
)
152 /* Copy the TrapFrame only up to Eflags and zero the rest*/
153 RtlCopyMemory(TrapFrame
, &KdbTrapFrame
->Tf
, FIELD_OFFSET(KTRAP_FRAME
, HardwareEsp
));
155 /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */
157 KiSsToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareSegSs
);
158 KiEspToTrapFrame(TrapFrame
, KdbTrapFrame
->Tf
.HardwareEsp
);
160 /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
163 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
164 * the old instruction in *OldInst.
166 * \param Process Process in which's context to overwrite the instruction.
167 * \param Address Address at which to overwrite the instruction.
168 * \param NewInst New instruction (written to \a Address)
169 * \param OldInst Old instruction (read from \a Address)
174 KdbpOverwriteInstruction(
175 IN PEPROCESS Process
,
176 IN ULONG_PTR Address
,
178 OUT PUCHAR OldInst OPTIONAL
)
182 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
185 /* Get the protection for the address. */
186 Protect
= MmGetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
188 /* Return if that page isn't present. */
189 if (Protect
& PAGE_NOACCESS
)
191 return STATUS_MEMORY_NOT_ALLOCATED
;
194 /* Attach to the process */
195 if (CurrentProcess
!= Process
)
197 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
200 /* Make the page writeable if it is read only. */
201 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
203 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
),
204 (Protect
& ~(PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
)) | PAGE_READWRITE
);
207 /* Copy the old instruction back to the caller. */
210 Status
= KdbpSafeReadMemory(OldInst
, (PUCHAR
)Address
, 1);
211 if (!NT_SUCCESS(Status
))
213 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
215 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
217 /* Detach from process */
218 if (CurrentProcess
!= Process
)
226 /* Copy the new instruction in its place. */
227 Status
= KdbpSafeWriteMemory((PUCHAR
)Address
, &NewInst
, 1);
229 /* Restore the page protection. */
230 if (Protect
& (PAGE_READONLY
|PAGE_EXECUTE
|PAGE_EXECUTE_READ
))
232 MmSetPageProtect(Process
, (PVOID
)PAGE_ROUND_DOWN(Address
), Protect
);
235 /* Detach from process */
236 if (CurrentProcess
!= Process
)
238 KeUnstackDetachProcess(&ApcState
);
244 /*!\brief Checks whether the given instruction can be single stepped or has to be
245 * stepped over using a temporary breakpoint.
247 * \retval TRUE Instruction is a call.
248 * \retval FALSE Instruction is not a call.
251 KdbpShouldStepOverInstruction(ULONG_PTR Eip
)
256 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
258 KdbpPrint("Couldn't access memory at 0x%p\n", Eip
);
262 /* Check if the current instruction is a call. */
263 while ((i
< sizeof (Mem
)) && (Mem
[i
] == 0x66 || Mem
[i
] == 0x67))
265 if (i
== sizeof (Mem
))
267 if (Mem
[i
] == 0xE8 || Mem
[i
] == 0x9A || Mem
[i
] == 0xF2 || Mem
[i
] == 0xF3 ||
268 (((i
+ 1) < sizeof (Mem
)) && Mem
[i
] == 0xFF && (Mem
[i
+1] & 0x38) == 0x10))
275 /*!\brief Steps over an instruction
277 * If the given instruction should be stepped over, this function inserts a
278 * temporary breakpoint after the instruction and returns TRUE, otherwise it
281 * \retval TRUE Temporary breakpoint set after instruction.
282 * \retval FALSE No breakpoint was set.
285 KdbpStepOverInstruction(ULONG_PTR Eip
)
289 if (!KdbpShouldStepOverInstruction(Eip
))
292 InstLen
= KdbpGetInstLength(Eip
);
296 if (!NT_SUCCESS(KdbpInsertBreakPoint(Eip
+ InstLen
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
302 /*!\brief Steps into an instruction (interrupts)
304 * If the given instruction should be stepped into, this function inserts a
305 * temporary breakpoint at the target instruction and returns TRUE, otherwise it
308 * \retval TRUE Temporary breakpoint set at target instruction.
309 * \retval FALSE No breakpoint was set.
312 KdbpStepIntoInstruction(ULONG_PTR Eip
)
321 if (!NT_SUCCESS(KdbpSafeReadMemory(Mem
, (PVOID
)Eip
, sizeof (Mem
))))
323 /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
327 /* Check for INT instruction */
328 /* FIXME: Check for iret */
331 else if (Mem
[0] == 0xcd)
333 else if (Mem
[0] == 0xce && KdbCurrentTrapFrame
->Tf
.EFlags
& (1<<11)) /* 1 << 11 is the overflow flag */
338 if (IntVect
< 32) /* We should be informed about interrupts < 32 by the kernel, no need to breakpoint them */
343 /* Read the interrupt descriptor table register */
344 asm volatile("sidt %0" : : "m"(Idtr
.Limit
));
345 if (IntVect
>= (Idtr
.Limit
+ 1) / 8)
347 /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
351 /* Get the interrupt descriptor */
352 if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc
, (PVOID
)(Idtr
.Base
+ (IntVect
* 8)), sizeof (IntDesc
))))
354 /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
358 /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
359 if ((IntDesc
[1] & (1 << 15)) == 0) /* not present */
363 if ((IntDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
365 /* FIXME: Task gates not supported */
368 else if (((IntDesc
[1] & 0x1fe0) == 0x0e00) || /* 32 bit Interrupt gate */
369 ((IntDesc
[1] & 0x1fe0) == 0x0f00)) /* 32 bit Trap gate */
371 /* FIXME: Should the segment selector of the interrupt gate be checked? */
372 TargetEip
= (IntDesc
[1] & 0xffff0000) | (IntDesc
[0] & 0x0000ffff);
379 /* Insert breakpoint */
380 if (!NT_SUCCESS(KdbpInsertBreakPoint(TargetEip
, KdbBreakPointTemporary
, 0, 0, NULL
, FALSE
, NULL
)))
386 /*!\brief Gets the number of the next breakpoint >= Start.
388 * \param Start Breakpoint number to start searching at. -1 if no more breakpoints are found.
390 * \returns Breakpoint number (-1 if no more breakpoints are found)
393 KdbpGetNextBreakPointNr(
394 IN ULONG Start OPTIONAL
)
396 for (; Start
< RTL_NUMBER_OF(KdbBreakPoints
); Start
++)
398 if (KdbBreakPoints
[Start
].Type
!= KdbBreakPointNone
)
404 /*!\brief Returns information of the specified breakpoint.
406 * \param BreakPointNr Number of the breakpoint to return information of.
407 * \param Address Receives the address of the breakpoint.
408 * \param Type Receives the type of the breakpoint (hardware or software)
409 * \param Size Size - for memory breakpoints.
410 * \param AccessType Access type - for hardware breakpoints.
411 * \param DebugReg Debug register - for enabled hardware breakpoints.
412 * \param Enabled Whether the breakpoint is enabled or not.
413 * \param Process The owning process of the breakpoint.
414 * \param ConditionExpression The expression which was given as condition for the bp.
416 * \returns NULL on failure, pointer to a KDB_BREAKPOINT struct on success.
419 KdbpGetBreakPointInfo(
420 IN ULONG BreakPointNr
,
421 OUT ULONG_PTR
*Address OPTIONAL
,
422 OUT KDB_BREAKPOINT_TYPE
*Type OPTIONAL
,
423 OUT UCHAR
*Size OPTIONAL
,
424 OUT KDB_ACCESS_TYPE
*AccessType OPTIONAL
,
425 OUT UCHAR
*DebugReg OPTIONAL
,
426 OUT BOOLEAN
*Enabled OPTIONAL
,
427 OUT BOOLEAN
*Global OPTIONAL
,
428 OUT PEPROCESS
*Process OPTIONAL
,
429 OUT PCHAR
*ConditionExpression OPTIONAL
)
433 if (BreakPointNr
>= RTL_NUMBER_OF(KdbBreakPoints
) ||
434 KdbBreakPoints
[BreakPointNr
].Type
== KdbBreakPointNone
)
439 bp
= KdbBreakPoints
+ BreakPointNr
;
441 *Address
= bp
->Address
;
444 if (bp
->Type
== KdbBreakPointHardware
)
447 *Size
= bp
->Data
.Hw
.Size
;
448 if (AccessType
!= NULL
)
449 *AccessType
= bp
->Data
.Hw
.AccessType
;
450 if (DebugReg
!= NULL
&& bp
->Enabled
)
451 *DebugReg
= bp
->Data
.Hw
.DebugReg
;
454 *Enabled
= bp
->Enabled
;
456 *Global
= bp
->Global
;
458 *Process
= bp
->Process
;
459 if (ConditionExpression
!= NULL
)
460 *ConditionExpression
= bp
->ConditionExpression
;
465 /*!\brief Inserts a breakpoint into the breakpoint array.
467 * The \a Process of the breakpoint is set to \a KdbCurrentProcess
469 * \param Address Address at which to set the breakpoint.
470 * \param Type Type of breakpoint (hardware or software)
471 * \param Size Size of breakpoint (for hardware/memory breakpoints)
472 * \param AccessType Access type (for hardware breakpoins)
473 * \param ConditionExpression Expression which must evaluate to true for conditional breakpoints.
474 * \param Global Wether the breakpoint is global or local to a process.
475 * \param BreakPointNumber Receives the breakpoint number on success
480 KdbpInsertBreakPoint(
481 IN ULONG_PTR Address
,
482 IN KDB_BREAKPOINT_TYPE Type
,
483 IN UCHAR Size OPTIONAL
,
484 IN KDB_ACCESS_TYPE AccessType OPTIONAL
,
485 IN PCHAR ConditionExpression OPTIONAL
,
487 OUT PULONG BreakPointNumber OPTIONAL
)
491 PCHAR ConditionExpressionDup
;
495 ASSERT(Type
!= KdbBreakPointNone
);
497 if (Type
== KdbBreakPointHardware
)
499 if ((Address
% Size
) != 0)
501 KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address
, Size
);
502 return STATUS_UNSUCCESSFUL
;
504 if (AccessType
== KdbAccessExec
&& Size
!= 1)
506 KdbpPrint("Size must be 1 for execution breakpoints.\n");
507 return STATUS_UNSUCCESSFUL
;
511 if (KdbBreakPointCount
== KDB_MAXIMUM_BREAKPOINT_COUNT
)
513 return STATUS_UNSUCCESSFUL
;
516 /* Parse conditon expression string and duplicate it */
517 if (ConditionExpression
!= NULL
)
519 Condition
= KdbpRpnParseExpression(ConditionExpression
, &ErrOffset
, ErrMsg
);
520 if (Condition
== NULL
)
523 KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg
, ErrOffset
);
525 KdbpPrint("Couldn't parse expression: %s", ErrMsg
);
526 return STATUS_UNSUCCESSFUL
;
529 i
= strlen(ConditionExpression
) + 1;
530 ConditionExpressionDup
= ExAllocatePoolWithTag(NonPagedPool
, i
, TAG_KDBG
);
531 RtlCopyMemory(ConditionExpressionDup
, ConditionExpression
, i
);
537 ConditionExpressionDup
= NULL
;
540 /* Find unused breakpoint */
541 if (Type
== KdbBreakPointTemporary
)
543 for (i
= RTL_NUMBER_OF(KdbBreakPoints
) - 1; i
>= 0; i
--)
545 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
551 for (i
= 0; i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
); i
++)
553 if (KdbBreakPoints
[i
].Type
== KdbBreakPointNone
)
557 ASSERT(i
< (LONG
)RTL_NUMBER_OF(KdbBreakPoints
));
559 /* Set the breakpoint */
560 ASSERT(KdbCurrentProcess
!= NULL
);
561 KdbBreakPoints
[i
].Type
= Type
;
562 KdbBreakPoints
[i
].Address
= Address
;
563 KdbBreakPoints
[i
].Enabled
= FALSE
;
564 KdbBreakPoints
[i
].Global
= Global
;
565 KdbBreakPoints
[i
].Process
= KdbCurrentProcess
;
566 KdbBreakPoints
[i
].ConditionExpression
= ConditionExpressionDup
;
567 KdbBreakPoints
[i
].Condition
= Condition
;
568 if (Type
== KdbBreakPointHardware
)
571 KdbBreakPoints
[i
].Data
.Hw
.Size
= Size
;
572 KdbBreakPoints
[i
].Data
.Hw
.AccessType
= AccessType
;
574 KdbBreakPointCount
++;
576 if (Type
!= KdbBreakPointTemporary
)
577 KdbpPrint("Breakpoint %d inserted.\n", i
);
579 /* Try to enable the breakpoint */
580 KdbpEnableBreakPoint(i
, NULL
);
582 /* Return the breakpoint number */
583 if (BreakPointNumber
!= NULL
)
584 *BreakPointNumber
= i
;
586 return STATUS_SUCCESS
;
589 /*!\brief Deletes a breakpoint
591 * \param BreakPointNr Number of the breakpoint to delete. Can be -1
592 * \param BreakPoint Breakpoint to delete. Can be NULL.
594 * \retval TRUE Success.
595 * \retval FALSE Failure (invalid breakpoint number)
598 KdbpDeleteBreakPoint(
599 IN LONG BreakPointNr OPTIONAL
,
600 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
602 if (BreakPointNr
< 0)
604 ASSERT(BreakPoint
!= NULL
);
605 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
607 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
609 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
612 if (BreakPoint
== NULL
)
614 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
616 if (BreakPoint
->Type
== KdbBreakPointNone
)
618 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
622 if (BreakPoint
->Enabled
&&
623 !KdbpDisableBreakPoint(-1, BreakPoint
))
626 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
627 KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr
);
628 BreakPoint
->Type
= KdbBreakPointNone
;
629 KdbBreakPointCount
--;
634 /*!\brief Checks if the breakpoint was set by the debugger
636 * Tries to find a breakpoint in the breakpoint array which caused
637 * the debug exception to happen.
639 * \param ExpNr Exception Number (1 or 3)
640 * \param TrapFrame Exception trapframe
642 * \returns Breakpoint number, -1 on error.
645 KdbpIsBreakPointOurs(
647 IN PKTRAP_FRAME TrapFrame
)
650 ASSERT(ExpNr
== 1 || ExpNr
== 3);
652 if (ExpNr
== 3) /* Software interrupt */
654 ULONG_PTR BpEip
= (ULONG_PTR
)TrapFrame
->Eip
- 1; /* Get EIP of INT3 instruction */
655 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
657 ASSERT((KdbSwBreakPoints
[i
]->Type
== KdbBreakPointSoftware
||
658 KdbSwBreakPoints
[i
]->Type
== KdbBreakPointTemporary
));
659 ASSERT(KdbSwBreakPoints
[i
]->Enabled
);
660 if (KdbSwBreakPoints
[i
]->Address
== BpEip
)
662 return KdbSwBreakPoints
[i
] - KdbBreakPoints
;
666 else if (ExpNr
== 1) /* Hardware interrupt */
669 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
671 ASSERT(KdbHwBreakPoints
[i
]->Type
== KdbBreakPointHardware
&&
672 KdbHwBreakPoints
[i
]->Enabled
);
673 DebugReg
= KdbHwBreakPoints
[i
]->Data
.Hw
.DebugReg
;
674 if ((TrapFrame
->Dr6
& (1 << DebugReg
)) != 0)
676 return KdbHwBreakPoints
[i
] - KdbBreakPoints
;
684 /*!\brief Enables a breakpoint.
686 * \param BreakPointNr Number of the breakpoint to enable Can be -1.
687 * \param BreakPoint Breakpoint to enable. Can be NULL.
689 * \retval TRUE Success.
690 * \retval FALSE Failure.
692 * \sa KdbpDisableBreakPoint
695 KdbpEnableBreakPoint(
696 IN LONG BreakPointNr OPTIONAL
,
697 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
703 if (BreakPointNr
< 0)
705 ASSERT(BreakPoint
!= NULL
);
706 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
708 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
710 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
713 if (BreakPoint
== NULL
)
715 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
717 if (BreakPoint
->Type
== KdbBreakPointNone
)
719 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
723 if (BreakPoint
->Enabled
== TRUE
)
725 KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr
);
729 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
730 BreakPoint
->Type
== KdbBreakPointTemporary
)
732 if (KdbSwBreakPointCount
>= KDB_MAXIMUM_SW_BREAKPOINT_COUNT
)
734 KdbpPrint("Maximum number of SW breakpoints (%d) used. "
735 "Disable another breakpoint in order to enable this one.\n",
736 KDB_MAXIMUM_SW_BREAKPOINT_COUNT
);
739 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
740 0xCC, &BreakPoint
->Data
.SavedInstruction
);
741 if (!NT_SUCCESS(Status
))
743 KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint
->Address
);
746 KdbSwBreakPoints
[KdbSwBreakPointCount
++] = BreakPoint
;
750 if (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
)
751 ASSERT(BreakPoint
->Data
.Hw
.Size
== 1);
752 ASSERT((BreakPoint
->Address
% BreakPoint
->Data
.Hw
.Size
) == 0);
753 if (KdbHwBreakPointCount
>= KDB_MAXIMUM_HW_BREAKPOINT_COUNT
)
755 KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
756 "Disable another breakpoint in order to enable this one.\n",
757 KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
761 /* Find unused hw breakpoint */
762 ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT
== 4);
763 for (i
= 0; i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
; i
++)
765 if ((KdbTrapFrame
.Tf
.Dr7
& (0x3 << (i
* 2))) == 0)
768 ASSERT(i
< KDB_MAXIMUM_HW_BREAKPOINT_COUNT
);
770 /* Set the breakpoint address. */
774 KdbTrapFrame
.Tf
.Dr0
= BreakPoint
->Address
;
777 KdbTrapFrame
.Tf
.Dr1
= BreakPoint
->Address
;
780 KdbTrapFrame
.Tf
.Dr2
= BreakPoint
->Address
;
783 KdbTrapFrame
.Tf
.Dr3
= BreakPoint
->Address
;
787 /* Enable the global breakpoint */
788 KdbTrapFrame
.Tf
.Dr7
|= (0x2 << (i
* 2));
790 /* Enable the exact match bits. */
791 KdbTrapFrame
.Tf
.Dr7
|= 0x00000300;
793 /* Clear existing state. */
794 KdbTrapFrame
.Tf
.Dr7
&= ~(0xF << (16 + (i
* 4)));
796 /* Set the breakpoint type. */
797 switch (BreakPoint
->Data
.Hw
.AccessType
)
806 case KdbAccessReadWrite
:
814 KdbTrapFrame
.Tf
.Dr7
|= (ul
<< (16 + (i
* 4)));
816 /* Set the breakpoint length. */
817 KdbTrapFrame
.Tf
.Dr7
|= ((BreakPoint
->Data
.Hw
.Size
- 1) << (18 + (i
* 4)));
819 /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
820 if (&KdbTrapFrame
!= KdbCurrentTrapFrame
)
822 KdbCurrentTrapFrame
->Tf
.Dr0
= KdbTrapFrame
.Tf
.Dr0
;
823 KdbCurrentTrapFrame
->Tf
.Dr1
= KdbTrapFrame
.Tf
.Dr1
;
824 KdbCurrentTrapFrame
->Tf
.Dr2
= KdbTrapFrame
.Tf
.Dr2
;
825 KdbCurrentTrapFrame
->Tf
.Dr3
= KdbTrapFrame
.Tf
.Dr3
;
826 KdbCurrentTrapFrame
->Tf
.Dr6
= KdbTrapFrame
.Tf
.Dr6
;
827 KdbCurrentTrapFrame
->Tf
.Dr7
= KdbTrapFrame
.Tf
.Dr7
;
830 BreakPoint
->Data
.Hw
.DebugReg
= i
;
831 KdbHwBreakPoints
[KdbHwBreakPointCount
++] = BreakPoint
;
834 BreakPoint
->Enabled
= TRUE
;
835 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
836 KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr
);
840 /*!\brief Disables a breakpoint.
842 * \param BreakPointNr Number of the breakpoint to disable. Can be -1
843 * \param BreakPoint Breakpoint to disable. Can be NULL.
845 * \retval TRUE Success.
846 * \retval FALSE Failure.
848 * \sa KdbpEnableBreakPoint
851 KdbpDisableBreakPoint(
852 IN LONG BreakPointNr OPTIONAL
,
853 IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL
)
858 if (BreakPointNr
< 0)
860 ASSERT(BreakPoint
!= NULL
);
861 BreakPointNr
= BreakPoint
- KdbBreakPoints
;
863 if (BreakPointNr
< 0 || BreakPointNr
>= KDB_MAXIMUM_BREAKPOINT_COUNT
)
865 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
868 if (BreakPoint
== NULL
)
870 BreakPoint
= KdbBreakPoints
+ BreakPointNr
;
872 if (BreakPoint
->Type
== KdbBreakPointNone
)
874 KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr
);
878 if (BreakPoint
->Enabled
== FALSE
)
880 KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr
);
884 if (BreakPoint
->Type
== KdbBreakPointSoftware
||
885 BreakPoint
->Type
== KdbBreakPointTemporary
)
887 ASSERT(KdbSwBreakPointCount
> 0);
888 Status
= KdbpOverwriteInstruction(BreakPoint
->Process
, BreakPoint
->Address
,
889 BreakPoint
->Data
.SavedInstruction
, NULL
);
890 if (!NT_SUCCESS(Status
))
892 KdbpPrint("Couldn't restore original instruction.\n");
896 for (i
= 0; i
< KdbSwBreakPointCount
; i
++)
898 if (KdbSwBreakPoints
[i
] == BreakPoint
)
900 KdbSwBreakPoints
[i
] = KdbSwBreakPoints
[--KdbSwBreakPointCount
];
901 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbSwBreakPointCount */
905 if (i
!= (UINT
)-1) /* not found */
910 ASSERT(BreakPoint
->Type
== KdbBreakPointHardware
);
912 /* Clear the breakpoint. */
913 KdbTrapFrame
.Tf
.Dr7
&= ~(0x3 << (BreakPoint
->Data
.Hw
.DebugReg
* 2));
914 if ((KdbTrapFrame
.Tf
.Dr7
& 0xFF) == 0)
917 * If no breakpoints are enabled then clear the exact match flags.
919 KdbTrapFrame
.Tf
.Dr7
&= 0xFFFFFCFF;
922 for (i
= 0; i
< KdbHwBreakPointCount
; i
++)
924 if (KdbHwBreakPoints
[i
] == BreakPoint
)
926 KdbHwBreakPoints
[i
] = KdbHwBreakPoints
[--KdbHwBreakPointCount
];
927 i
= -1; /* if the last breakpoint is disabled dont break with i >= KdbHwBreakPointCount */
931 if (i
!= (UINT
)-1) /* not found */
935 BreakPoint
->Enabled
= FALSE
;
936 if (BreakPoint
->Type
!= KdbBreakPointTemporary
)
937 KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr
);
941 /*!\brief Gets the first or last chance enter-condition for exception nr. \a ExceptionNr
943 * \param ExceptionNr Number of the exception to get condition of.
944 * \param FirstChance Whether to get first or last chance condition.
945 * \param Condition Receives the condition setting.
947 * \retval TRUE Success.
948 * \retval FALSE Failure (invalid exception nr)
951 KdbpGetEnterCondition(
953 IN BOOLEAN FirstChance
,
954 OUT KDB_ENTER_CONDITION
*Condition
)
956 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
))
959 *Condition
= KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1];
963 /*!\brief Sets the first or last chance enter-condition for exception nr. \a ExceptionNr
965 * \param ExceptionNr Number of the exception to set condition of (-1 for all)
966 * \param FirstChance Whether to set first or last chance condition.
967 * \param Condition The new condition setting.
969 * \retval TRUE Success.
970 * \retval FALSE Failure (invalid exception nr)
973 KdbpSetEnterCondition(
975 IN BOOLEAN FirstChance
,
976 IN KDB_ENTER_CONDITION Condition
)
980 for (ExceptionNr
= 0; ExceptionNr
< (LONG
)RTL_NUMBER_OF(KdbEnterConditions
); ExceptionNr
++)
982 if (ExceptionNr
== 1 || ExceptionNr
== 8 ||
983 ExceptionNr
== 9 || ExceptionNr
== 15) /* Reserved exceptions */
987 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
992 if (ExceptionNr
>= (LONG
)RTL_NUMBER_OF(KdbEnterConditions
) ||
993 ExceptionNr
== 1 || ExceptionNr
== 8 || /* Do not allow changing of the debug */
994 ExceptionNr
== 9 || ExceptionNr
== 15) /* trap or reserved exceptions */
998 KdbEnterConditions
[ExceptionNr
][FirstChance
? 0 : 1] = Condition
;
1003 /*!\brief Switches to another thread context
1005 * \param ThreadId Id of the thread to switch to.
1007 * \retval TRUE Success.
1008 * \retval FALSE Failure (i.e. invalid thread id)
1014 PETHREAD Thread
= NULL
;
1017 /* Get a pointer to the thread */
1018 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &Thread
)))
1020 KdbpPrint("Invalid thread id: 0x%08x\n", (UINT
)ThreadId
);
1023 Process
= Thread
->ThreadsProcess
;
1025 if (KeIsExecutingDpc() && Process
!= KdbCurrentProcess
)
1027 KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
1031 /* Save the current thread's context (if we previously attached to a thread) */
1032 if (KdbCurrentThread
!= KdbOriginalThread
)
1034 ASSERT(KdbCurrentTrapFrame
== &KdbThreadTrapFrame
);
1035 KdbpKdbTrapFrameToTrapFrame(KdbCurrentTrapFrame
, KdbCurrentThread
->Tcb
.TrapFrame
);
1039 ASSERT(KdbCurrentTrapFrame
== &KdbTrapFrame
);
1042 /* Switch to the thread's context */
1043 if (Thread
!= KdbOriginalThread
)
1045 if (Thread
->Tcb
.TrapFrame
== NULL
)
1047 KdbpPrint("Threads TrapFrame is NULL! Cannot attach.\n");
1050 KdbpTrapFrameToKdbTrapFrame(Thread
->Tcb
.TrapFrame
, &KdbThreadTrapFrame
);
1051 KdbCurrentTrapFrame
= &KdbThreadTrapFrame
;
1053 else /* Switching back to original thread */
1055 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1057 KdbCurrentThread
= Thread
;
1059 /* Attach to the thread's process */
1060 ASSERT(KdbCurrentProcess
== PsGetCurrentProcess());
1061 if (KdbCurrentProcess
!= Process
)
1063 if (KdbCurrentProcess
!= KdbOriginalProcess
) /* detach from previously attached process */
1065 KeUnstackDetachProcess(&KdbApcState
);
1067 if (KdbOriginalProcess
!= Process
)
1069 KeStackAttachProcess(&Process
->Pcb
, &KdbApcState
);
1071 KdbCurrentProcess
= Process
;
1077 /*!\brief Switches to another process/thread context
1079 * This function switches to the first thread in the specified process.
1081 * \param ProcessId Id of the process to switch to.
1083 * \retval TRUE Success.
1084 * \retval FALSE Failure (i.e. invalid process id)
1087 KdbpAttachToProcess(
1090 PEPROCESS Process
= NULL
;
1094 /* Get a pointer to the process */
1095 if (!NT_SUCCESS(PsLookupProcessByProcessId(ProcessId
, &Process
)))
1097 KdbpPrint("Invalid process id: 0x%08x\n", (UINT
)ProcessId
);
1101 Entry
= Process
->ThreadListHead
.Flink
;
1102 if (Entry
== &KdbCurrentProcess
->ThreadListHead
)
1104 KdbpPrint("No threads in process 0x%08x, cannot attach to process!\n", (UINT
)ProcessId
);
1108 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1110 return KdbpAttachToThread(Thread
->Cid
.UniqueThread
);
1113 /*!\brief Calls the main loop ...
1118 KdbpCliMainLoop(KdbEnteredOnSingleStep
);
1121 /*!\brief Internal function to enter KDB.
1123 * Disables interrupts, releases display ownership, ...
1129 PVOID SavedInitialStack
, SavedStackBase
, SavedKernelStack
;
1130 ULONG SavedStackLimit
;
1133 if (KdpDebugMode
.Screen
)
1135 HalReleaseDisplayOwnership();
1138 /* Call the interface's main loop on a different stack */
1139 Thread
= PsGetCurrentThread();
1140 SavedInitialStack
= Thread
->Tcb
.InitialStack
;
1141 SavedStackBase
= Thread
->Tcb
.StackBase
;
1142 SavedStackLimit
= Thread
->Tcb
.StackLimit
;
1143 SavedKernelStack
= Thread
->Tcb
.KernelStack
;
1144 Thread
->Tcb
.InitialStack
= Thread
->Tcb
.StackBase
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1145 Thread
->Tcb
.StackLimit
= (ULONG
)KdbStack
;
1146 Thread
->Tcb
.KernelStack
= (char*)KdbStack
+ KDB_STACK_SIZE
;
1148 /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase);*/
1150 KdbpStackSwitchAndCall(Thread
->Tcb
.KernelStack
, KdbpCallMainLoop
);
1152 Thread
->Tcb
.InitialStack
= SavedInitialStack
;
1153 Thread
->Tcb
.StackBase
= SavedStackBase
;
1154 Thread
->Tcb
.StackLimit
= SavedStackLimit
;
1155 Thread
->Tcb
.KernelStack
= SavedKernelStack
;
1159 /*!\brief KDB Exception filter
1161 * Called by the exception dispatcher.
1163 * \param ExceptionRecord Unused.
1164 * \param PreviousMode UserMode if the exception was raised from umode, otherwise KernelMode.
1165 * \param Context Unused.
1166 * \param TrapFrame Exception TrapFrame.
1167 * \param FirstChance TRUE when called before exception frames were serached,
1168 * FALSE for the second call.
1170 * \returns KD_CONTINUE_TYPE
1173 KdbEnterDebuggerException(
1174 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
,
1175 IN KPROCESSOR_MODE PreviousMode
,
1176 IN PCONTEXT Context OPTIONAL
,
1177 IN OUT PKTRAP_FRAME TrapFrame
,
1178 IN BOOLEAN FirstChance
)
1180 ULONG ExpNr
= (ULONG
)TrapFrame
->DbgArgMark
;
1181 KDB_ENTER_CONDITION EnterCondition
;
1182 KD_CONTINUE_TYPE ContinueType
= kdHandleException
;
1183 PKDB_BREAKPOINT BreakPoint
;
1186 BOOLEAN Resume
= FALSE
;
1187 BOOLEAN EnterConditionMet
= TRUE
;
1190 KdbCurrentProcess
= PsGetCurrentProcess();
1192 /* Set continue type to kdContinue for single steps and breakpoints */
1193 if (ExpNr
== 1 || ExpNr
== 3)
1194 ContinueType
= kdContinue
;
1196 /* Check if we should handle the exception. */
1197 ul
= min(ExpNr
, RTL_NUMBER_OF(KdbEnterConditions
) - 1);
1198 EnterCondition
= KdbEnterConditions
[ul
][FirstChance
? 0 : 1];
1199 if (EnterCondition
== KdbDoNotEnter
||
1200 (EnterCondition
== KdbEnterFromUmode
&& PreviousMode
== KernelMode
) ||
1201 (EnterCondition
== KdbEnterFromKmode
&& PreviousMode
!= KernelMode
))
1203 EnterConditionMet
= FALSE
;
1206 /* If we stopped on one of our breakpoints then let the user know. */
1207 KdbLastBreakPointNr
= -1;
1208 KdbEnteredOnSingleStep
= FALSE
;
1210 if (FirstChance
&& (ExpNr
== 1 || ExpNr
== 3) &&
1211 (KdbLastBreakPointNr
= KdbpIsBreakPointOurs(ExpNr
, TrapFrame
)) >= 0)
1213 BreakPoint
= KdbBreakPoints
+ KdbLastBreakPointNr
;
1218 * The breakpoint will point to the next instruction by default so
1219 * point it back to the start of original instruction.
1224 * ... and restore the original instruction.
1226 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
,
1227 BreakPoint
->Data
.SavedInstruction
, NULL
)))
1229 DbgPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
1234 if ((BreakPoint
->Type
== KdbBreakPointHardware
) &&
1235 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessExec
))
1237 Resume
= TRUE
; /* Set the resume flag when continuing execution */
1241 * When a temporary breakpoint is hit we have to make sure that we are
1242 * in the same context in which it was set, otherwise it could happen
1243 * that another process/thread hits it before and it gets deleted.
1245 else if (BreakPoint
->Type
== KdbBreakPointTemporary
&&
1246 BreakPoint
->Process
== KdbCurrentProcess
)
1248 ASSERT((TrapFrame
->EFlags
& X86_EFLAGS_TF
) == 0);
1251 * Delete the temporary breakpoint which was used to step over or into the instruction.
1253 KdbpDeleteBreakPoint(-1, BreakPoint
);
1255 if (--KdbNumSingleSteps
> 0)
1257 if ((KdbSingleStepOver
&& !KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1258 (!KdbSingleStepOver
&& !KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1260 TrapFrame
->EFlags
|= X86_EFLAGS_TF
;
1262 goto continue_execution
; /* return */
1265 KdbEnteredOnSingleStep
= TRUE
;
1269 * If we hit a breakpoint set by the debugger we set the single step flag,
1270 * ignore the next single step and reenable the breakpoint.
1272 else if (BreakPoint
->Type
== KdbBreakPointSoftware
||
1273 BreakPoint
->Type
== KdbBreakPointTemporary
)
1276 TrapFrame
->EFlags
|= X86_EFLAGS_TF
;
1277 KdbBreakPointToReenable
= BreakPoint
;
1281 * Make sure that the breakpoint should be triggered in this context
1283 if (!BreakPoint
->Global
&& BreakPoint
->Process
!= KdbCurrentProcess
)
1285 goto continue_execution
; /* return */
1289 * Check if the condition for the breakpoint is met.
1291 if (BreakPoint
->Condition
!= NULL
)
1293 /* Setup the KDB trap frame */
1294 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1297 if (!KdbpRpnEvaluateParsedExpression(BreakPoint
->Condition
, &KdbTrapFrame
, &ull
, NULL
, NULL
))
1299 /* FIXME: Print warning? */
1301 else if (ull
== 0) /* condition is not met */
1303 goto continue_execution
; /* return */
1307 if (BreakPoint
->Type
== KdbBreakPointSoftware
)
1309 DbgPrint("Entered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
1310 KdbLastBreakPointNr
, TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
);
1312 else if (BreakPoint
->Type
== KdbBreakPointHardware
)
1314 DbgPrint("Entered debugger on breakpoint #%d: %s 0x%08x\n",
1315 KdbLastBreakPointNr
,
1316 (BreakPoint
->Data
.Hw
.AccessType
== KdbAccessRead
) ? "READ" :
1317 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessWrite
) ? "WRITE" :
1318 ((BreakPoint
->Data
.Hw
.AccessType
== KdbAccessReadWrite
) ? "RDWR" : "EXEC")
1325 else if (ExpNr
== 1)
1327 /* Silently ignore a debugger initiated single step. */
1328 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbBreakPointToReenable
!= NULL
)
1330 /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
1331 BreakPoint
= KdbBreakPointToReenable
;
1332 KdbBreakPointToReenable
= NULL
;
1333 ASSERT(BreakPoint
->Type
== KdbBreakPointSoftware
||
1334 BreakPoint
->Type
== KdbBreakPointTemporary
);
1337 * Reenable the breakpoint we disabled to execute the breakpointed
1340 if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess
, BreakPoint
->Address
, 0xCC,
1341 &BreakPoint
->Data
.SavedInstruction
)))
1343 DbgPrint("Warning: Couldn't reenable breakpoint %d\n",
1344 BreakPoint
- KdbBreakPoints
);
1347 /* Unset TF if we are no longer single stepping. */
1348 if (KdbNumSingleSteps
== 0)
1349 TrapFrame
->EFlags
&= ~X86_EFLAGS_TF
;
1350 goto continue_execution
; /* return */
1353 /* Check if we expect a single step */
1354 if ((TrapFrame
->Dr6
& 0xf) == 0 && KdbNumSingleSteps
> 0)
1356 /*ASSERT((TrapFrame->Eflags & X86_EFLAGS_TF) != 0);*/
1357 if (--KdbNumSingleSteps
> 0)
1359 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(TrapFrame
->Eip
)) ||
1360 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(TrapFrame
->Eip
)))
1362 TrapFrame
->EFlags
&= ~X86_EFLAGS_TF
;
1366 TrapFrame
->EFlags
|= X86_EFLAGS_TF
;
1368 goto continue_execution
; /* return */
1371 TrapFrame
->EFlags
&= ~X86_EFLAGS_TF
;
1372 KdbEnteredOnSingleStep
= TRUE
;
1376 if (!EnterConditionMet
)
1378 return ContinueType
;
1380 DbgPrint("Entered debugger on unexpected debug trap!\n");
1383 else if (ExpNr
== 3)
1385 if (KdbInitFileBuffer
!= NULL
)
1387 KdbpCliInterpretInitFile();
1388 EnterConditionMet
= FALSE
;
1390 if (!EnterConditionMet
)
1392 return ContinueType
;
1395 DbgPrint("Entered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
1396 TrapFrame
->SegCs
& 0xffff, TrapFrame
->Eip
- 1);
1400 CONST PCHAR ExceptionString
= (ExpNr
< RTL_NUMBER_OF(ExceptionNrToString
)) ?
1401 (ExceptionNrToString
[ExpNr
]) :
1402 ("Unknown/User defined exception");
1404 if (!EnterConditionMet
)
1406 return ContinueType
;
1409 DbgPrint("Entered debugger on %s-chance exception number %d (%s)\n",
1410 FirstChance
? "first" : "last", ExpNr
, ExceptionString
);
1413 /* FIXME: Add noexec memory stuff */
1416 asm volatile("movl %%cr2, %0" : "=r"(Cr2
));
1417 Err
= TrapFrame
->ErrCode
;
1418 DbgPrint("Memory at 0x%p could not be %s: ", Cr2
, (Err
& (1 << 1)) ? "written" : "read");
1419 if ((Err
& (1 << 0)) == 0)
1420 DbgPrint("Page not present.\n");
1423 if ((Err
& (1 << 3)) != 0)
1424 DbgPrint("Reserved bits in page directory set.\n");
1426 DbgPrint("Page protection violation.\n");
1431 /* Once we enter the debugger we do not expect any more single steps to happen */
1432 KdbNumSingleSteps
= 0;
1434 /* Update the current process pointer */
1435 KdbCurrentProcess
= KdbOriginalProcess
= PsGetCurrentProcess();
1436 KdbCurrentThread
= KdbOriginalThread
= PsGetCurrentThread();
1437 KdbCurrentTrapFrame
= &KdbTrapFrame
;
1439 /* Setup the KDB trap frame */
1440 KdbpTrapFrameToKdbTrapFrame(TrapFrame
, &KdbTrapFrame
);
1442 /* Enter critical section */
1443 Ke386SaveFlags(OldEflags
);
1444 Ke386DisableInterrupts();
1446 /* Exception inside the debugger? Game over. */
1447 if (InterlockedIncrement(&KdbEntryCount
) > 1)
1449 Ke386RestoreFlags(OldEflags
);
1450 return kdHandleException
;
1453 /* Call the main loop. */
1454 KdbpInternalEnter();
1456 /* Check if we should single step */
1457 if (KdbNumSingleSteps
> 0)
1459 if ((KdbSingleStepOver
&& KdbpStepOverInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)) ||
1460 (!KdbSingleStepOver
&& KdbpStepIntoInstruction(KdbCurrentTrapFrame
->Tf
.Eip
)))
1462 ASSERT((KdbCurrentTrapFrame
->Tf
.EFlags
& X86_EFLAGS_TF
) == 0);
1463 /*KdbCurrentTrapFrame->Tf.EFlags &= ~X86_EFLAGS_TF;*/
1467 KdbCurrentTrapFrame
->Tf
.EFlags
|= X86_EFLAGS_TF
;
1471 /* Save the current thread's trapframe */
1472 if (KdbCurrentTrapFrame
== &KdbThreadTrapFrame
)
1474 KdbpKdbTrapFrameToTrapFrame(KdbCurrentTrapFrame
, KdbCurrentThread
->Tcb
.TrapFrame
);
1477 /* Detach from attached process */
1478 if (KdbCurrentProcess
!= KdbOriginalProcess
)
1480 KeUnstackDetachProcess(&KdbApcState
);
1483 /* Update the exception TrapFrame */
1484 KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame
, TrapFrame
);
1486 /* Decrement the entry count */
1487 InterlockedDecrement(&KdbEntryCount
);
1489 /* Leave critical section */
1490 Ke386RestoreFlags(OldEflags
);
1493 /* Clear debug status */
1494 if (ExpNr
== 1 || ExpNr
== 3) /* FIXME: Why clear DR6 on INT3? */
1496 /* Set the RF flag so we don't trigger the same breakpoint again. */
1499 TrapFrame
->EFlags
|= X86_EFLAGS_RF
;
1502 /* Clear dr6 status flags. */
1503 TrapFrame
->Dr6
&= ~0x0000e00f;
1507 return ContinueType
;
1511 KdbDeleteProcessHook(IN PEPROCESS Process
)
1513 KdbSymFreeProcessSymbols(Process
);
1515 /* FIXME: Delete breakpoints for process */
1520 KdbpGetCommandLineSettings(PCHAR p1
)
1524 while (p1
&& (p2
= strchr(p1
, '/')))
1528 if (!_strnicmp(p2
, "KDSERIAL", 8))
1531 KdbDebugState
|= KD_DEBUG_KDSERIAL
;
1532 KdpDebugMode
.Serial
= TRUE
;
1534 else if (!_strnicmp(p2
, "KDNOECHO", 8))
1537 KdbDebugState
|= KD_DEBUG_KDNOECHO
;
1545 KdbpSafeReadMemory(OUT PVOID Dest
,
1549 NTSTATUS Status
= STATUS_SUCCESS
;
1559 Status
= _SEH_GetExceptionCode();
1567 KdbpSafeWriteMemory(OUT PVOID Dest
,
1571 NTSTATUS Status
= STATUS_SUCCESS
;
1581 Status
= _SEH_GetExceptionCode();