2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/kd/kdmain.c
5 * PURPOSE: Kernel Debugger Initialization
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
14 /* VARIABLES ***************************************************************/
16 BOOLEAN KdDebuggerEnabled
= FALSE
;
17 BOOLEAN KdEnteredDebugger
= FALSE
;
18 BOOLEAN KdDebuggerNotPresent
= TRUE
;
19 BOOLEAN KdBreakAfterSymbolLoad
= FALSE
;
20 BOOLEAN KdpBreakPending
= FALSE
;
21 BOOLEAN KdPitchDebugger
= TRUE
;
22 BOOLEAN KdIgnoreUmExceptions
= FALSE
;
23 KD_CONTEXT KdpContext
;
24 ULONG Kd_WIN2000_Mask
;
25 VOID NTAPI
PspDumpThreads(BOOLEAN SystemThreads
);
32 #define MAX_KD_COMPONENT_TABLE_ENTRIES 128
33 KD_COMPONENT_DATA KdComponentTable
[MAX_KD_COMPONENT_TABLE_ENTRIES
];
34 ULONG KdComponentTableEntries
= 0;
36 ULONG Kd_DEFAULT_MASK
= 1 << DPFLTR_ERROR_LEVEL
;
38 /* PRIVATE FUNCTIONS *********************************************************/
42 KdpServiceDispatcher(ULONG Service
,
50 case BREAKPOINT_PRINT
: /* DbgPrint */
51 Result
= KdpPrintString(Buffer1
, Buffer1Length
);
55 case ' soR': /* ROS-INTERNAL */
57 switch ((ULONG_PTR
)Buffer1
)
64 PspDumpThreads(FALSE
);
68 MmDumpArmPfnDatabase(FALSE
);
77 #if defined(_M_IX86) && !defined(_WINKD_) // See ke/i386/traphdlr.c
78 /* Register a debug callback */
81 switch (Buffer1Length
)
83 case ID_Win32PreServiceHook
:
84 KeWin32PreServiceHook
= Buffer1
;
87 case ID_Win32PostServiceHook
:
88 KeWin32PostServiceHook
= Buffer1
;
96 /* Special case for stack frame dumps */
99 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
104 /* Register KDBG CLI callback */
107 Result
= KdbRegisterCliCallback(Buffer1
, Buffer1Length
);
113 DPRINT1("Invalid debug service call!\n");
114 HalDisplayString("Invalid debug service call!\r\n");
123 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
124 IN PKEXCEPTION_FRAME ExceptionFrame
,
125 IN PEXCEPTION_RECORD ExceptionRecord
,
127 IN KPROCESSOR_MODE PreviousMode
,
128 IN BOOLEAN SecondChance
)
130 KD_CONTINUE_TYPE Return
= kdHandleException
;
131 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
133 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
134 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
135 (ExceptionRecord
->NumberParameters
> 0) &&
136 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
137 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
138 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
139 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
140 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
142 /* Check if this is a debug print */
143 if (ExceptionCommand
== BREAKPOINT_PRINT
)
145 /* Print the string */
146 KdpServiceDispatcher(BREAKPOINT_PRINT
,
147 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
148 ExceptionRecord
->ExceptionInformation
[2]);
151 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
154 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
156 PKD_SYMBOLS_INFO SymbolsInfo
;
157 KD_SYMBOLS_INFO CapturedSymbolsInfo
;
158 PLDR_DATA_TABLE_ENTRY LdrEntry
;
160 SymbolsInfo
= (PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2];
161 if (PreviousMode
!= KernelMode
)
165 ProbeForRead(SymbolsInfo
,
166 sizeof(*SymbolsInfo
),
168 RtlCopyMemory(&CapturedSymbolsInfo
,
170 sizeof(*SymbolsInfo
));
171 SymbolsInfo
= &CapturedSymbolsInfo
;
173 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
180 if (SymbolsInfo
!= NULL
)
182 /* Load symbols. Currently implemented only for KDBG! */
183 if (KdbpSymFindModule(SymbolsInfo
->BaseOfDll
, NULL
, -1, &LdrEntry
))
185 KdbSymProcessSymbols(LdrEntry
);
189 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
193 USHORT OutStringLength
;
195 /* Get the response string and length */
196 OutString
= (LPSTR
)Context
->Ebx
;
197 OutStringLength
= (USHORT
)Context
->Edi
;
200 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
201 ExceptionInformation
[1],
202 (USHORT
)ExceptionRecord
->
203 ExceptionInformation
[2],
208 /* Return the number of characters that we received */
209 Context
->Eax
= ReturnValue
;
213 /* This we can handle: simply bump the Program Counter */
214 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
219 /* Check if this is an assertion failure */
220 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
222 /* Bump EIP to the instruction following the int 2C */
227 /* Get out of here if the Debugger isn't connected */
228 if (KdDebuggerNotPresent
) return FALSE
;
231 /* Call KDBG if available */
232 Return
= KdbEnterDebuggerException(ExceptionRecord
,
238 if (WrapperInitRoutine
)
241 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
245 #endif /* not KDBG */
247 /* Debugger didn't handle it, please handle! */
248 if (Return
== kdHandleException
) return FALSE
;
250 /* Debugger handled it */
256 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
257 IN PEXCEPTION_RECORD ExceptionRecord
,
260 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
262 /* Get out of here if the Debugger isn't connected */
263 if (KdDebuggerNotPresent
) return FALSE
;
266 * Right now, the GDB wrapper seems to handle exceptions differntly
267 * from KDGB and both are called at different times, while the GDB
268 * one is only called once and that's it. I don't really have the knowledge
269 * to fix the GDB stub, so until then, we'll be using this hack
271 if (WrapperInitRoutine
)
273 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
278 /* Debugger didn't handle it, please handle! */
279 if (Return
== kdHandleException
) return FALSE
;
281 /* Debugger handled it */
287 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
289 IN KPROCESSOR_MODE PreviousMode
)
291 /* KDBG has its own mechanism for ignoring user mode exceptions */
295 /* PUBLIC FUNCTIONS *********************************************************/
302 KdRefreshDebuggerNotPresent(VOID
)
306 /* Just return whatever was set previously -- FIXME! */
307 return KdDebuggerNotPresent
;
315 KdDisableDebugger(VOID
)
320 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
322 /* TODO: Disable any breakpoints */
324 /* Disable the Debugger */
325 KdDebuggerEnabled
= FALSE
;
326 SharedUserData
->KdDebuggerEnabled
= FALSE
;
329 KeLowerIrql(OldIrql
);
332 return STATUS_SUCCESS
;
340 KdEnableDebugger(VOID
)
345 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
347 /* TODO: Re-enable any breakpoints */
349 /* Enable the Debugger */
350 KdDebuggerEnabled
= TRUE
;
351 SharedUserData
->KdDebuggerEnabled
= TRUE
;
354 KeLowerIrql(OldIrql
);
357 return STATUS_SUCCESS
;
367 return KdpBreakPending
;
375 KdPowerTransition(ULONG PowerState
)
378 return STATUS_NOT_IMPLEMENTED
;
386 KdChangeOption(IN KD_OPTION Option
,
387 IN ULONG InBufferLength OPTIONAL
,
389 IN ULONG OutBufferLength OPTIONAL
,
391 OUT PULONG OutBufferRequiredLength OPTIONAL
)
394 return STATUS_NOT_IMPLEMENTED
;
400 NtQueryDebugFilterState(IN ULONG ComponentId
,
405 /* Convert Level to mask if it isn't already one */
409 /* Check if it is not the default component */
410 if (ComponentId
!= MAXULONG
)
412 /* No, search for an existing entry in the table */
413 for (i
= 0; i
< KdComponentTableEntries
; i
++)
415 /* Check if it is the right component */
416 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
418 /* Check if mask are matching */
419 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
424 /* Entry not found in the table, use default mask */
425 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
430 NtSetDebugFilterState(IN ULONG ComponentId
,
436 /* Convert Level to mask if it isn't already one */
439 Level
&= ~DPFLTR_MASK
;
441 /* Check if it is the default component */
442 if (ComponentId
== MAXULONG
)
444 /* Yes, modify the default mask */
446 Kd_DEFAULT_MASK
|= Level
;
448 Kd_DEFAULT_MASK
&= ~Level
;
450 return STATUS_SUCCESS
;
453 /* Search for an existing entry */
454 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
456 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
460 /* Check if we have found an existing entry */
461 if (i
== KdComponentTableEntries
)
463 /* Check if we have enough space in the table */
464 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
465 return STATUS_INVALID_PARAMETER_1
;
467 /* Add a new entry */
468 ++KdComponentTableEntries
;
469 KdComponentTable
[i
].ComponentId
= ComponentId
;
470 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
473 /* Update entry table */
475 KdComponentTable
[i
].Level
|= Level
;
477 KdComponentTable
[i
].Level
&= ~Level
;
479 return STATUS_SUCCESS
;
487 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
488 IN PVOID InputBuffer
,
489 IN ULONG InputBufferLength
,
490 OUT PVOID OutputBuffer
,
491 IN ULONG OutputBufferLength
,
492 IN OUT PULONG ReturnLength
,
493 IN KPROCESSOR_MODE PreviousMode
)
496 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
499 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;