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
)
64 PspDumpThreads(FALSE
);
68 MmDumpArmPfnDatabase(FALSE
);
77 /* Register a debug callback */
80 switch (Buffer1Length
)
82 case ID_Win32PreServiceHook
:
83 KeWin32PreServiceHook
= Buffer1
;
86 case ID_Win32PostServiceHook
:
87 KeWin32PostServiceHook
= Buffer1
;
94 /* Special case for stack frame dumps */
97 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
102 /* Register KDBG CLI callback */
105 Result
= KdbRegisterCliCallback(Buffer1
, Buffer1Length
);
111 HalDisplayString ("Invalid debug service call!\n");
120 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
121 IN PKEXCEPTION_FRAME ExceptionFrame
,
122 IN PEXCEPTION_RECORD ExceptionRecord
,
124 IN KPROCESSOR_MODE PreviousMode
,
125 IN BOOLEAN SecondChance
)
127 KD_CONTINUE_TYPE Return
= kdHandleException
;
128 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
130 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
131 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
132 (ExceptionRecord
->NumberParameters
> 0) &&
133 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
134 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
135 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
136 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
137 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
139 /* Check if this is a debug print */
140 if (ExceptionCommand
== BREAKPOINT_PRINT
)
142 /* Print the string */
143 KdpServiceDispatcher(BREAKPOINT_PRINT
,
144 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
145 ExceptionRecord
->ExceptionInformation
[2]);
148 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
151 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
153 PLDR_DATA_TABLE_ENTRY LdrEntry
;
155 /* Load symbols. Currently implemented only for KDBG! */
156 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
157 KdbSymProcessSymbols(LdrEntry
);
159 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
163 USHORT OutStringLength
;
165 /* Get the response string and length */
166 OutString
= (LPSTR
)Context
->Ebx
;
167 OutStringLength
= (USHORT
)Context
->Edi
;
170 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
171 ExceptionInformation
[1],
172 (USHORT
)ExceptionRecord
->
173 ExceptionInformation
[2],
177 /* Return the number of characters that we received */
178 Context
->Eax
= ReturnValue
;
182 /* This we can handle: simply bump the Program Counter */
183 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
188 /* Check if this is an assertion failure */
189 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
192 DbgPrint("\n!!! Assertion Failure at Address 0x%p !!!\n\n",
193 (PVOID
)Context
->Eip
);
195 /* Bump EIP to the instruction following the int 2C and return */
201 /* Get out of here if the Debugger isn't connected */
202 if (KdDebuggerNotPresent
) return FALSE
;
205 /* Call KDBG if available */
206 Return
= KdbEnterDebuggerException(ExceptionRecord
,
212 if (WrapperInitRoutine
)
215 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
219 #endif /* not KDBG */
221 /* Debugger didn't handle it, please handle! */
222 if (Return
== kdHandleException
) return FALSE
;
224 /* Debugger handled it */
230 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
231 IN PEXCEPTION_RECORD ExceptionRecord
,
234 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
236 /* Get out of here if the Debugger isn't connected */
237 if (KdDebuggerNotPresent
) return FALSE
;
240 * Right now, the GDB wrapper seems to handle exceptions differntly
241 * from KDGB and both are called at different times, while the GDB
242 * one is only called once and that's it. I don't really have the knowledge
243 * to fix the GDB stub, so until then, we'll be using this hack
245 if (WrapperInitRoutine
)
247 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
252 /* Debugger didn't handle it, please handle! */
253 if (Return
== kdHandleException
) return FALSE
;
255 /* Debugger handled it */
261 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
263 IN KPROCESSOR_MODE PreviousMode
)
265 /* KDBG has its own mechanism for ignoring user mode exceptions */
269 /* PUBLIC FUNCTIONS *********************************************************/
276 KdRefreshDebuggerNotPresent(VOID
)
280 /* Just return whatever was set previously -- FIXME! */
281 return KdDebuggerNotPresent
;
289 KdDisableDebugger(VOID
)
294 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
296 /* TODO: Disable any breakpoints */
298 /* Disable the Debugger */
299 KdDebuggerEnabled
= FALSE
;
302 KeLowerIrql(OldIrql
);
305 return STATUS_SUCCESS
;
313 KdEnableDebugger(VOID
)
318 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
320 /* TODO: Re-enable any breakpoints */
322 /* Enable the Debugger */
323 KdDebuggerEnabled
= TRUE
;
326 KeLowerIrql(OldIrql
);
329 return STATUS_SUCCESS
;
339 return KdpBreakPending
;
347 KdPowerTransition(ULONG PowerState
)
350 return STATUS_NOT_IMPLEMENTED
;
358 KdChangeOption(IN KD_OPTION Option
,
359 IN ULONG InBufferLength OPTIONAL
,
361 IN ULONG OutBufferLength OPTIONAL
,
363 OUT PULONG OutBufferRequiredLength OPTIONAL
)
366 return STATUS_NOT_IMPLEMENTED
;
372 NtQueryDebugFilterState(IN ULONG ComponentId
,
377 /* Convert Level to mask if it isn't already one */
381 /* Check if it is not the default component */
382 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
384 /* No, search for an existing entry in the table */
385 for (i
= 0; i
< KdComponentTableEntries
; i
++)
387 /* Check if it is the right component */
388 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
390 /* Check if mask are matching */
391 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
396 /* Entry not found in the table, use default mask */
397 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
402 NtSetDebugFilterState(IN ULONG ComponentId
,
408 /* Convert Level to mask if it isn't already one */
411 Level
&= ~DPFLTR_MASK
;
413 /* Check if it is the default component */
414 if (ComponentId
== DPFLTR_DEFAULT_ID
)
416 /* Yes, modify the default mask */
418 Kd_DEFAULT_MASK
|= Level
;
420 Kd_DEFAULT_MASK
&= ~Level
;
422 return STATUS_SUCCESS
;
425 /* Search for an existing entry */
426 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
428 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
432 /* Check if we have found an existing entry */
433 if (i
== KdComponentTableEntries
)
435 /* Check if we have enough space in the table */
436 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
437 return STATUS_INVALID_PARAMETER_1
;
439 /* Add a new entry */
440 ++KdComponentTableEntries
;
441 KdComponentTable
[i
].ComponentId
= ComponentId
;
442 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
445 /* Update entry table */
447 KdComponentTable
[i
].Level
|= Level
;
449 KdComponentTable
[i
].Level
&= ~Level
;
451 return STATUS_SUCCESS
;
459 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
460 IN PVOID InputBuffer
,
461 IN ULONG InputBufferLength
,
462 OUT PVOID OutputBuffer
,
463 IN ULONG OutputBufferLength
,
464 IN OUT PULONG ReturnLength
,
465 IN KPROCESSOR_MODE PreviousMode
)
468 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
471 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;