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 KiEnableTimerWatchdog
= FALSE
;
20 BOOLEAN KdBreakAfterSymbolLoad
= FALSE
;
21 BOOLEAN KdpBreakPending
;
22 BOOLEAN KdPitchDebugger
= TRUE
;
23 VOID NTAPI
PspDumpThreads(BOOLEAN SystemThreads
);
30 #define MAX_KD_COMPONENT_TABLE_ENTRIES 128
31 KD_COMPONENT_DATA KdComponentTable
[MAX_KD_COMPONENT_TABLE_ENTRIES
];
32 ULONG KdComponentTableEntries
= 0;
34 ULONG Kd_DEFAULT_MASK
= 1 << DPFLTR_ERROR_LEVEL
;
36 /* PRIVATE FUNCTIONS *********************************************************/
40 KdpServiceDispatcher(ULONG Service
,
48 case BREAKPOINT_PRINT
: /* DbgPrint */
49 Result
= KdpPrintString(Buffer1
, Buffer1Length
);
53 case ' soR': /* ROS-INTERNAL */
55 switch ((ULONG_PTR
)Buffer1
)
58 KeBugCheck(MANUALLY_INITIATED_CRASH
);
66 PspDumpThreads(FALSE
);
73 case ThatsWhatSheSaid
:
83 /* Special case for stack frame dumps */
86 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
91 HalDisplayString ("Invalid debug service call!\n");
100 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
101 IN PKEXCEPTION_FRAME ExceptionFrame
,
102 IN PEXCEPTION_RECORD ExceptionRecord
,
104 IN KPROCESSOR_MODE PreviousMode
,
105 IN BOOLEAN SecondChance
)
107 KD_CONTINUE_TYPE Return
= kdHandleException
;
108 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
110 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
111 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
112 (ExceptionRecord
->NumberParameters
> 0) &&
113 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
114 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
115 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
116 (ExceptionCommand
== BREAKPOINT_PRINT
)))
118 /* Check if this is a debug print */
119 if (ExceptionCommand
== BREAKPOINT_PRINT
)
121 /* Print the string */
122 KdpServiceDispatcher(BREAKPOINT_PRINT
,
123 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
124 ExceptionRecord
->ExceptionInformation
[2]);
127 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
129 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
132 PLDR_DATA_TABLE_ENTRY LdrEntry
;
134 /* Load symbols. Currently implemented only for KDBG! */
135 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
136 KdbSymProcessSymbols(LdrEntry
);
140 /* This we can handle: simply bump the Program Counter */
141 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
145 /* Get out of here if the Debugger isn't connected */
146 if (KdDebuggerNotPresent
) return FALSE
;
149 /* Call KDBG if available */
150 Return
= KdbEnterDebuggerException(ExceptionRecord
,
156 if (WrapperInitRoutine
)
159 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
163 #endif /* not KDBG */
165 /* Debugger didn't handle it, please handle! */
166 if (Return
== kdHandleException
) return FALSE
;
168 /* Debugger handled it */
174 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
175 IN PEXCEPTION_RECORD ExceptionRecord
,
178 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
180 /* Get out of here if the Debugger isn't connected */
181 if (KdDebuggerNotPresent
) return FALSE
;
184 * Right now, the GDB wrapper seems to handle exceptions differntly
185 * from KDGB and both are called at different times, while the GDB
186 * one is only called once and that's it. I don't really have the knowledge
187 * to fix the GDB stub, so until then, we'll be using this hack
189 if (WrapperInitRoutine
)
191 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
196 /* Debugger didn't handle it, please handle! */
197 if (Return
== kdHandleException
) return FALSE
;
199 /* Debugger handled it */
203 /* PUBLIC FUNCTIONS *********************************************************/
210 KdRefreshDebuggerNotPresent(VOID
)
214 /* Just return whatever was set previously -- FIXME! */
215 return KdDebuggerNotPresent
;
223 KdDisableDebugger(VOID
)
228 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
230 /* TODO: Disable any breakpoints */
232 /* Disable the Debugger */
233 KdDebuggerEnabled
= FALSE
;
236 KeLowerIrql(OldIrql
);
239 return STATUS_SUCCESS
;
247 KdEnableDebugger(VOID
)
252 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
254 /* TODO: Re-enable any breakpoints */
256 /* Enable the Debugger */
257 KdDebuggerEnabled
= TRUE
;
260 KeLowerIrql(OldIrql
);
263 return STATUS_SUCCESS
;
273 return KdpBreakPending
;
281 KdPowerTransition(ULONG PowerState
)
284 return STATUS_NOT_IMPLEMENTED
;
292 KdChangeOption(IN KD_OPTION Option
,
293 IN ULONG InBufferLength OPTIONAL
,
295 IN ULONG OutBufferLength OPTIONAL
,
297 OUT PULONG OutBufferRequiredLength OPTIONAL
)
300 return STATUS_NOT_IMPLEMENTED
;
306 NtQueryDebugFilterState(IN ULONG ComponentId
,
311 /* Convert Level to mask if it isn't already one */
315 /* Check if it is not the default component */
316 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
318 /* No, search for an existing entry in the table */
319 for (i
= 0; i
< KdComponentTableEntries
; i
++)
321 /* Check if it is the right component */
322 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
324 /* Check if mask are matching */
325 return (Level
& KdComponentTable
[i
].Level
) != 0;
330 /* Entry not found in the table, use default mask */
331 return (Level
& Kd_DEFAULT_MASK
) != 0;
336 NtSetDebugFilterState(IN ULONG ComponentId
,
342 /* Convert Level to mask if it isn't already one */
345 Level
&= ~DPFLTR_MASK
;
347 /* Check if it is the default component */
348 if (ComponentId
== DPFLTR_DEFAULT_ID
)
350 /* Yes, modify the default mask */
352 Kd_DEFAULT_MASK
|= Level
;
354 Kd_DEFAULT_MASK
&= ~Level
;
356 return STATUS_SUCCESS
;
359 /* Search for an existing entry */
360 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
362 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
366 /* Check if we have found an existing entry */
367 if (i
== KdComponentTableEntries
)
369 /* Check if we have enough space in the table */
370 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
371 return STATUS_INVALID_PARAMETER_1
;
373 /* Add a new entry */
374 ++KdComponentTableEntries
;
375 KdComponentTable
[i
].ComponentId
= ComponentId
;
376 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
379 /* Update entry table */
381 KdComponentTable
[i
].Level
|= Level
;
383 KdComponentTable
[i
].Level
&= ~Level
;
385 return STATUS_SUCCESS
;
393 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
394 IN PVOID InputBuffer
,
395 IN ULONG InputBufferLength
,
396 OUT PVOID OutputBuffer
,
397 IN ULONG OutputBufferLength
,
398 IN OUT PULONG ReturnLength
,
399 IN KPROCESSOR_MODE PreviousMode
)
402 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
405 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;