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 HalDisplayString ("Invalid debug service call!\n");
111 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
112 IN PKEXCEPTION_FRAME ExceptionFrame
,
113 IN PEXCEPTION_RECORD ExceptionRecord
,
115 IN KPROCESSOR_MODE PreviousMode
,
116 IN BOOLEAN SecondChance
)
118 KD_CONTINUE_TYPE Return
= kdHandleException
;
119 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
121 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
122 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
123 (ExceptionRecord
->NumberParameters
> 0) &&
124 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
125 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
126 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
127 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
128 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
130 /* Check if this is a debug print */
131 if (ExceptionCommand
== BREAKPOINT_PRINT
)
133 /* Print the string */
134 KdpServiceDispatcher(BREAKPOINT_PRINT
,
135 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
136 ExceptionRecord
->ExceptionInformation
[2]);
139 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
142 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
144 PLDR_DATA_TABLE_ENTRY LdrEntry
;
146 /* Load symbols. Currently implemented only for KDBG! */
147 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
148 KdbSymProcessSymbols(LdrEntry
);
150 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
154 USHORT OutStringLength
;
156 /* Get the response string and length */
157 OutString
= (LPSTR
)Context
->Ebx
;
158 OutStringLength
= (USHORT
)Context
->Edi
;
161 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
162 ExceptionInformation
[1],
163 (USHORT
)ExceptionRecord
->
164 ExceptionInformation
[2],
168 /* Return the number of characters that we received */
169 Context
->Eax
= ReturnValue
;
173 /* This we can handle: simply bump the Program Counter */
174 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
179 /* Check if this is an assertion failure */
180 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
183 DbgPrint("\n!!! Assertion Failure at Address 0x%p !!!\n\n",
184 (PVOID
)Context
->Eip
);
186 /* Bump EIP to the instruction following the int 2C and return */
192 /* Get out of here if the Debugger isn't connected */
193 if (KdDebuggerNotPresent
) return FALSE
;
196 /* Call KDBG if available */
197 Return
= KdbEnterDebuggerException(ExceptionRecord
,
203 if (WrapperInitRoutine
)
206 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
210 #endif /* not KDBG */
212 /* Debugger didn't handle it, please handle! */
213 if (Return
== kdHandleException
) return FALSE
;
215 /* Debugger handled it */
221 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
222 IN PEXCEPTION_RECORD ExceptionRecord
,
225 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
227 /* Get out of here if the Debugger isn't connected */
228 if (KdDebuggerNotPresent
) return FALSE
;
231 * Right now, the GDB wrapper seems to handle exceptions differntly
232 * from KDGB and both are called at different times, while the GDB
233 * one is only called once and that's it. I don't really have the knowledge
234 * to fix the GDB stub, so until then, we'll be using this hack
236 if (WrapperInitRoutine
)
238 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
243 /* Debugger didn't handle it, please handle! */
244 if (Return
== kdHandleException
) return FALSE
;
246 /* Debugger handled it */
252 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
254 IN KPROCESSOR_MODE PreviousMode
)
256 /* KDBG has its own mechanism for ignoring user mode exceptions */
260 /* PUBLIC FUNCTIONS *********************************************************/
267 KdRefreshDebuggerNotPresent(VOID
)
271 /* Just return whatever was set previously -- FIXME! */
272 return KdDebuggerNotPresent
;
280 KdDisableDebugger(VOID
)
285 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
287 /* TODO: Disable any breakpoints */
289 /* Disable the Debugger */
290 KdDebuggerEnabled
= FALSE
;
293 KeLowerIrql(OldIrql
);
296 return STATUS_SUCCESS
;
304 KdEnableDebugger(VOID
)
309 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
311 /* TODO: Re-enable any breakpoints */
313 /* Enable the Debugger */
314 KdDebuggerEnabled
= TRUE
;
317 KeLowerIrql(OldIrql
);
320 return STATUS_SUCCESS
;
330 return KdpBreakPending
;
338 KdPowerTransition(ULONG PowerState
)
341 return STATUS_NOT_IMPLEMENTED
;
349 KdChangeOption(IN KD_OPTION Option
,
350 IN ULONG InBufferLength OPTIONAL
,
352 IN ULONG OutBufferLength OPTIONAL
,
354 OUT PULONG OutBufferRequiredLength OPTIONAL
)
357 return STATUS_NOT_IMPLEMENTED
;
363 NtQueryDebugFilterState(IN ULONG ComponentId
,
368 /* Convert Level to mask if it isn't already one */
372 /* Check if it is not the default component */
373 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
375 /* No, search for an existing entry in the table */
376 for (i
= 0; i
< KdComponentTableEntries
; i
++)
378 /* Check if it is the right component */
379 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
381 /* Check if mask are matching */
382 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
387 /* Entry not found in the table, use default mask */
388 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
393 NtSetDebugFilterState(IN ULONG ComponentId
,
399 /* Convert Level to mask if it isn't already one */
402 Level
&= ~DPFLTR_MASK
;
404 /* Check if it is the default component */
405 if (ComponentId
== DPFLTR_DEFAULT_ID
)
407 /* Yes, modify the default mask */
409 Kd_DEFAULT_MASK
|= Level
;
411 Kd_DEFAULT_MASK
&= ~Level
;
413 return STATUS_SUCCESS
;
416 /* Search for an existing entry */
417 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
419 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
423 /* Check if we have found an existing entry */
424 if (i
== KdComponentTableEntries
)
426 /* Check if we have enough space in the table */
427 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
428 return STATUS_INVALID_PARAMETER_1
;
430 /* Add a new entry */
431 ++KdComponentTableEntries
;
432 KdComponentTable
[i
].ComponentId
= ComponentId
;
433 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
436 /* Update entry table */
438 KdComponentTable
[i
].Level
|= Level
;
440 KdComponentTable
[i
].Level
&= ~Level
;
442 return STATUS_SUCCESS
;
450 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
451 IN PVOID InputBuffer
,
452 IN ULONG InputBufferLength
,
453 OUT PVOID OutputBuffer
,
454 IN ULONG OutputBufferLength
,
455 IN OUT PULONG ReturnLength
,
456 IN KPROCESSOR_MODE PreviousMode
)
459 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
462 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;