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
)
193 DbgPrint("\n!!! Assertion Failure at Address 0x%p !!!\n\n",
194 (PVOID
)Context
->Eip
);
196 /* Bump EIP to the instruction following the int 2C and return */
202 /* Get out of here if the Debugger isn't connected */
203 if (KdDebuggerNotPresent
) return FALSE
;
206 /* Call KDBG if available */
207 Return
= KdbEnterDebuggerException(ExceptionRecord
,
213 if (WrapperInitRoutine
)
216 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
220 #endif /* not KDBG */
222 /* Debugger didn't handle it, please handle! */
223 if (Return
== kdHandleException
) return FALSE
;
225 /* Debugger handled it */
231 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
232 IN PEXCEPTION_RECORD ExceptionRecord
,
235 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
237 /* Get out of here if the Debugger isn't connected */
238 if (KdDebuggerNotPresent
) return FALSE
;
241 * Right now, the GDB wrapper seems to handle exceptions differntly
242 * from KDGB and both are called at different times, while the GDB
243 * one is only called once and that's it. I don't really have the knowledge
244 * to fix the GDB stub, so until then, we'll be using this hack
246 if (WrapperInitRoutine
)
248 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
253 /* Debugger didn't handle it, please handle! */
254 if (Return
== kdHandleException
) return FALSE
;
256 /* Debugger handled it */
262 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
264 IN KPROCESSOR_MODE PreviousMode
)
266 /* KDBG has its own mechanism for ignoring user mode exceptions */
270 /* PUBLIC FUNCTIONS *********************************************************/
277 KdRefreshDebuggerNotPresent(VOID
)
281 /* Just return whatever was set previously -- FIXME! */
282 return KdDebuggerNotPresent
;
290 KdDisableDebugger(VOID
)
295 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
297 /* TODO: Disable any breakpoints */
299 /* Disable the Debugger */
300 KdDebuggerEnabled
= FALSE
;
303 KeLowerIrql(OldIrql
);
306 return STATUS_SUCCESS
;
314 KdEnableDebugger(VOID
)
319 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
321 /* TODO: Re-enable any breakpoints */
323 /* Enable the Debugger */
324 KdDebuggerEnabled
= TRUE
;
327 KeLowerIrql(OldIrql
);
330 return STATUS_SUCCESS
;
340 return KdpBreakPending
;
348 KdPowerTransition(ULONG PowerState
)
351 return STATUS_NOT_IMPLEMENTED
;
359 KdChangeOption(IN KD_OPTION Option
,
360 IN ULONG InBufferLength OPTIONAL
,
362 IN ULONG OutBufferLength OPTIONAL
,
364 OUT PULONG OutBufferRequiredLength OPTIONAL
)
367 return STATUS_NOT_IMPLEMENTED
;
373 NtQueryDebugFilterState(IN ULONG ComponentId
,
378 /* Convert Level to mask if it isn't already one */
382 /* Check if it is not the default component */
383 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
385 /* No, search for an existing entry in the table */
386 for (i
= 0; i
< KdComponentTableEntries
; i
++)
388 /* Check if it is the right component */
389 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
391 /* Check if mask are matching */
392 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
397 /* Entry not found in the table, use default mask */
398 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
403 NtSetDebugFilterState(IN ULONG ComponentId
,
409 /* Convert Level to mask if it isn't already one */
412 Level
&= ~DPFLTR_MASK
;
414 /* Check if it is the default component */
415 if (ComponentId
== DPFLTR_DEFAULT_ID
)
417 /* Yes, modify the default mask */
419 Kd_DEFAULT_MASK
|= Level
;
421 Kd_DEFAULT_MASK
&= ~Level
;
423 return STATUS_SUCCESS
;
426 /* Search for an existing entry */
427 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
429 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
433 /* Check if we have found an existing entry */
434 if (i
== KdComponentTableEntries
)
436 /* Check if we have enough space in the table */
437 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
438 return STATUS_INVALID_PARAMETER_1
;
440 /* Add a new entry */
441 ++KdComponentTableEntries
;
442 KdComponentTable
[i
].ComponentId
= ComponentId
;
443 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
446 /* Update entry table */
448 KdComponentTable
[i
].Level
|= Level
;
450 KdComponentTable
[i
].Level
&= ~Level
;
452 return STATUS_SUCCESS
;
460 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
461 IN PVOID InputBuffer
,
462 IN ULONG InputBufferLength
,
463 OUT PVOID OutputBuffer
,
464 IN ULONG OutputBufferLength
,
465 IN OUT PULONG ReturnLength
,
466 IN KPROCESSOR_MODE PreviousMode
)
469 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
472 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;