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 DPRINT1("Invalid debug service call!\n");
112 HalDisplayString("Invalid debug service call!\r\n");
121 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
122 IN PKEXCEPTION_FRAME ExceptionFrame
,
123 IN PEXCEPTION_RECORD ExceptionRecord
,
125 IN KPROCESSOR_MODE PreviousMode
,
126 IN BOOLEAN SecondChance
)
128 KD_CONTINUE_TYPE Return
= kdHandleException
;
129 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
131 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
132 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
133 (ExceptionRecord
->NumberParameters
> 0) &&
134 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
135 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
136 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
137 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
138 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
140 /* Check if this is a debug print */
141 if (ExceptionCommand
== BREAKPOINT_PRINT
)
143 /* Print the string */
144 KdpServiceDispatcher(BREAKPOINT_PRINT
,
145 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
146 ExceptionRecord
->ExceptionInformation
[2]);
149 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
152 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
154 PLDR_DATA_TABLE_ENTRY LdrEntry
;
156 /* Load symbols. Currently implemented only for KDBG! */
157 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
158 KdbSymProcessSymbols(LdrEntry
);
160 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
164 USHORT OutStringLength
;
166 /* Get the response string and length */
167 OutString
= (LPSTR
)Context
->Ebx
;
168 OutStringLength
= (USHORT
)Context
->Edi
;
171 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
172 ExceptionInformation
[1],
173 (USHORT
)ExceptionRecord
->
174 ExceptionInformation
[2],
178 /* Return the number of characters that we received */
179 Context
->Eax
= ReturnValue
;
183 /* This we can handle: simply bump the Program Counter */
184 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
189 /* Check if this is an assertion failure */
190 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
192 /* Bump EIP to the instruction following the int 2C */
197 /* Get out of here if the Debugger isn't connected */
198 if (KdDebuggerNotPresent
) return FALSE
;
201 /* Call KDBG if available */
202 Return
= KdbEnterDebuggerException(ExceptionRecord
,
208 if (WrapperInitRoutine
)
211 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
215 #endif /* not KDBG */
217 /* Debugger didn't handle it, please handle! */
218 if (Return
== kdHandleException
) return FALSE
;
220 /* Debugger handled it */
226 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
227 IN PEXCEPTION_RECORD ExceptionRecord
,
230 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
232 /* Get out of here if the Debugger isn't connected */
233 if (KdDebuggerNotPresent
) return FALSE
;
236 * Right now, the GDB wrapper seems to handle exceptions differntly
237 * from KDGB and both are called at different times, while the GDB
238 * one is only called once and that's it. I don't really have the knowledge
239 * to fix the GDB stub, so until then, we'll be using this hack
241 if (WrapperInitRoutine
)
243 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
248 /* Debugger didn't handle it, please handle! */
249 if (Return
== kdHandleException
) return FALSE
;
251 /* Debugger handled it */
257 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
259 IN KPROCESSOR_MODE PreviousMode
)
261 /* KDBG has its own mechanism for ignoring user mode exceptions */
265 /* PUBLIC FUNCTIONS *********************************************************/
272 KdRefreshDebuggerNotPresent(VOID
)
276 /* Just return whatever was set previously -- FIXME! */
277 return KdDebuggerNotPresent
;
285 KdDisableDebugger(VOID
)
290 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
292 /* TODO: Disable any breakpoints */
294 /* Disable the Debugger */
295 KdDebuggerEnabled
= FALSE
;
298 KeLowerIrql(OldIrql
);
301 return STATUS_SUCCESS
;
309 KdEnableDebugger(VOID
)
314 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
316 /* TODO: Re-enable any breakpoints */
318 /* Enable the Debugger */
319 KdDebuggerEnabled
= TRUE
;
322 KeLowerIrql(OldIrql
);
325 return STATUS_SUCCESS
;
335 return KdpBreakPending
;
343 KdPowerTransition(ULONG PowerState
)
346 return STATUS_NOT_IMPLEMENTED
;
354 KdChangeOption(IN KD_OPTION Option
,
355 IN ULONG InBufferLength OPTIONAL
,
357 IN ULONG OutBufferLength OPTIONAL
,
359 OUT PULONG OutBufferRequiredLength OPTIONAL
)
362 return STATUS_NOT_IMPLEMENTED
;
368 NtQueryDebugFilterState(IN ULONG ComponentId
,
373 /* Convert Level to mask if it isn't already one */
377 /* Check if it is not the default component */
378 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
380 /* No, search for an existing entry in the table */
381 for (i
= 0; i
< KdComponentTableEntries
; i
++)
383 /* Check if it is the right component */
384 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
386 /* Check if mask are matching */
387 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
392 /* Entry not found in the table, use default mask */
393 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
398 NtSetDebugFilterState(IN ULONG ComponentId
,
404 /* Convert Level to mask if it isn't already one */
407 Level
&= ~DPFLTR_MASK
;
409 /* Check if it is the default component */
410 if (ComponentId
== DPFLTR_DEFAULT_ID
)
412 /* Yes, modify the default mask */
414 Kd_DEFAULT_MASK
|= Level
;
416 Kd_DEFAULT_MASK
&= ~Level
;
418 return STATUS_SUCCESS
;
421 /* Search for an existing entry */
422 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
424 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
428 /* Check if we have found an existing entry */
429 if (i
== KdComponentTableEntries
)
431 /* Check if we have enough space in the table */
432 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
433 return STATUS_INVALID_PARAMETER_1
;
435 /* Add a new entry */
436 ++KdComponentTableEntries
;
437 KdComponentTable
[i
].ComponentId
= ComponentId
;
438 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
441 /* Update entry table */
443 KdComponentTable
[i
].Level
|= Level
;
445 KdComponentTable
[i
].Level
&= ~Level
;
447 return STATUS_SUCCESS
;
455 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
456 IN PVOID InputBuffer
,
457 IN ULONG InputBufferLength
,
458 OUT PVOID OutputBuffer
,
459 IN ULONG OutputBufferLength
,
460 IN OUT PULONG ReturnLength
,
461 IN KPROCESSOR_MODE PreviousMode
)
464 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
467 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;