2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/kd/kdinit.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
)
60 KeBugCheck(MANUALLY_INITIATED_CRASH
);
68 PspDumpThreads(FALSE
);
76 MmDumpArmPfnDatabase(FALSE
);
85 /* Register a debug callback */
88 switch (Buffer1Length
)
90 case ID_Win32PreServiceHook
:
91 KeWin32PreServiceHook
= Buffer1
;
94 case ID_Win32PostServiceHook
:
95 KeWin32PostServiceHook
= Buffer1
;
102 /* Special case for stack frame dumps */
105 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
110 HalDisplayString ("Invalid debug service call!\n");
119 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
120 IN PKEXCEPTION_FRAME ExceptionFrame
,
121 IN PEXCEPTION_RECORD ExceptionRecord
,
123 IN KPROCESSOR_MODE PreviousMode
,
124 IN BOOLEAN SecondChance
)
126 KD_CONTINUE_TYPE Return
= kdHandleException
;
127 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
129 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
130 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
131 (ExceptionRecord
->NumberParameters
> 0) &&
132 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
133 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
134 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
135 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
136 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
138 /* Check if this is a debug print */
139 if (ExceptionCommand
== BREAKPOINT_PRINT
)
141 /* Print the string */
142 KdpServiceDispatcher(BREAKPOINT_PRINT
,
143 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
144 ExceptionRecord
->ExceptionInformation
[2]);
147 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
150 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
152 PLDR_DATA_TABLE_ENTRY LdrEntry
;
154 /* Load symbols. Currently implemented only for KDBG! */
155 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
156 KdbSymProcessSymbols(LdrEntry
);
158 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
162 USHORT OutStringLength
;
164 /* Get the response string and length */
165 OutString
= (LPSTR
)Context
->Ebx
;
166 OutStringLength
= (USHORT
)Context
->Edi
;
169 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
170 ExceptionInformation
[1],
171 (USHORT
)ExceptionRecord
->
172 ExceptionInformation
[2],
176 /* Return the number of characters that we received */
177 Context
->Eax
= ReturnValue
;
181 /* This we can handle: simply bump the Program Counter */
182 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
187 /* Check if this is an assertion failure */
188 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
191 DbgPrint("\n!!! Assertion Failure at Address 0x%p !!!\n\n",
192 (PVOID
)Context
->Eip
);
194 /* Bump EIP to the instruction following the int 2C and return */
200 /* Get out of here if the Debugger isn't connected */
201 if (KdDebuggerNotPresent
) return FALSE
;
204 /* Call KDBG if available */
205 Return
= KdbEnterDebuggerException(ExceptionRecord
,
211 if (WrapperInitRoutine
)
214 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
218 #endif /* not KDBG */
220 /* Debugger didn't handle it, please handle! */
221 if (Return
== kdHandleException
) return FALSE
;
223 /* Debugger handled it */
229 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
230 IN PEXCEPTION_RECORD ExceptionRecord
,
233 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
235 /* Get out of here if the Debugger isn't connected */
236 if (KdDebuggerNotPresent
) return FALSE
;
239 * Right now, the GDB wrapper seems to handle exceptions differntly
240 * from KDGB and both are called at different times, while the GDB
241 * one is only called once and that's it. I don't really have the knowledge
242 * to fix the GDB stub, so until then, we'll be using this hack
244 if (WrapperInitRoutine
)
246 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
251 /* Debugger didn't handle it, please handle! */
252 if (Return
== kdHandleException
) return FALSE
;
254 /* Debugger handled it */
260 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
262 IN KPROCESSOR_MODE PreviousMode
)
264 /* KDBG has its own mechanism for ignoring user mode exceptions */
268 /* PUBLIC FUNCTIONS *********************************************************/
275 KdRefreshDebuggerNotPresent(VOID
)
279 /* Just return whatever was set previously -- FIXME! */
280 return KdDebuggerNotPresent
;
288 KdDisableDebugger(VOID
)
293 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
295 /* TODO: Disable any breakpoints */
297 /* Disable the Debugger */
298 KdDebuggerEnabled
= FALSE
;
301 KeLowerIrql(OldIrql
);
304 return STATUS_SUCCESS
;
312 KdEnableDebugger(VOID
)
317 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
319 /* TODO: Re-enable any breakpoints */
321 /* Enable the Debugger */
322 KdDebuggerEnabled
= TRUE
;
325 KeLowerIrql(OldIrql
);
328 return STATUS_SUCCESS
;
338 return KdpBreakPending
;
346 KdPowerTransition(ULONG PowerState
)
349 return STATUS_NOT_IMPLEMENTED
;
357 KdChangeOption(IN KD_OPTION Option
,
358 IN ULONG InBufferLength OPTIONAL
,
360 IN ULONG OutBufferLength OPTIONAL
,
362 OUT PULONG OutBufferRequiredLength OPTIONAL
)
365 return STATUS_NOT_IMPLEMENTED
;
371 NtQueryDebugFilterState(IN ULONG ComponentId
,
376 /* Convert Level to mask if it isn't already one */
380 /* Check if it is not the default component */
381 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
383 /* No, search for an existing entry in the table */
384 for (i
= 0; i
< KdComponentTableEntries
; i
++)
386 /* Check if it is the right component */
387 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
389 /* Check if mask are matching */
390 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
395 /* Entry not found in the table, use default mask */
396 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
401 NtSetDebugFilterState(IN ULONG ComponentId
,
407 /* Convert Level to mask if it isn't already one */
410 Level
&= ~DPFLTR_MASK
;
412 /* Check if it is the default component */
413 if (ComponentId
== DPFLTR_DEFAULT_ID
)
415 /* Yes, modify the default mask */
417 Kd_DEFAULT_MASK
|= Level
;
419 Kd_DEFAULT_MASK
&= ~Level
;
421 return STATUS_SUCCESS
;
424 /* Search for an existing entry */
425 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
427 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
431 /* Check if we have found an existing entry */
432 if (i
== KdComponentTableEntries
)
434 /* Check if we have enough space in the table */
435 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
436 return STATUS_INVALID_PARAMETER_1
;
438 /* Add a new entry */
439 ++KdComponentTableEntries
;
440 KdComponentTable
[i
].ComponentId
= ComponentId
;
441 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
444 /* Update entry table */
446 KdComponentTable
[i
].Level
|= Level
;
448 KdComponentTable
[i
].Level
&= ~Level
;
450 return STATUS_SUCCESS
;
458 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
459 IN PVOID InputBuffer
,
460 IN ULONG InputBufferLength
,
461 OUT PVOID OutputBuffer
,
462 IN ULONG OutputBufferLength
,
463 IN OUT PULONG ReturnLength
,
464 IN KPROCESSOR_MODE PreviousMode
)
467 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
470 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;