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];
113 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
114 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
115 (ExceptionRecord
->NumberParameters
> 0) &&
116 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
117 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
118 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
119 (ExceptionCommand
== BREAKPOINT_PRINT
)))
121 /* Check if this is a debug print */
122 if (ExceptionCommand
== BREAKPOINT_PRINT
)
124 /* Print the string */
125 KdpServiceDispatcher(BREAKPOINT_PRINT
,
126 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
127 ExceptionRecord
->ExceptionInformation
[2]);
129 Context
->Eax
= STATUS_SUCCESS
;
131 Context
->R0
= STATUS_SUCCESS
;
133 #error Please be portable when modifying code
136 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
139 PLDR_DATA_TABLE_ENTRY LdrEntry
;
141 /* Load symbols. Currently implemented only for KDBG! */
142 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
143 KdbSymProcessSymbols(LdrEntry
);
147 /* This we can handle: simply bump EIP */
151 Context
->Pc
+= sizeof(ULONG
);
156 /* Get out of here if the Debugger isn't connected */
157 if (KdDebuggerNotPresent
) return FALSE
;
159 /* Save old EIP value */
161 EipOld
= Context
->Eip
;
165 /* Call KDBG if available */
166 Return
= KdbEnterDebuggerException(ExceptionRecord
,
172 if (WrapperInitRoutine
)
175 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
179 #endif /* not KDBG */
181 /* Bump EIP over int 3 if debugger did not already change it */
182 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
184 //DPRINT1("Address: %p. Return: %d\n", EipOld, Return);
187 /* Debugger didn't handle it, please handle! */
188 if (Return
== kdHandleException
) return FALSE
;
190 /* Debugger handled it */
196 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
197 IN PEXCEPTION_RECORD ExceptionRecord
,
200 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
202 /* Get out of here if the Debugger isn't connected */
203 if (KdDebuggerNotPresent
) return FALSE
;
206 * Right now, the GDB wrapper seems to handle exceptions differntly
207 * from KDGB and both are called at different times, while the GDB
208 * one is only called once and that's it. I don't really have the knowledge
209 * to fix the GDB stub, so until then, we'll be using this hack
211 if (WrapperInitRoutine
)
213 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
218 /* Debugger didn't handle it, please handle! */
219 if (Return
== kdHandleException
) return FALSE
;
221 /* Debugger handled it */
225 /* PUBLIC FUNCTIONS *********************************************************/
232 KdRefreshDebuggerNotPresent(VOID
)
236 /* Just return whatever was set previously -- FIXME! */
237 return KdDebuggerNotPresent
;
245 KdDisableDebugger(VOID
)
250 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
252 /* TODO: Disable any breakpoints */
254 /* Disable the Debugger */
255 KdDebuggerEnabled
= FALSE
;
258 KeLowerIrql(OldIrql
);
261 return STATUS_SUCCESS
;
269 KdEnableDebugger(VOID
)
274 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
276 /* TODO: Re-enable any breakpoints */
278 /* Enable the Debugger */
279 KdDebuggerEnabled
= TRUE
;
282 KeLowerIrql(OldIrql
);
285 return STATUS_SUCCESS
;
295 return KdpBreakPending
;
303 KdPowerTransition(ULONG PowerState
)
306 return STATUS_NOT_IMPLEMENTED
;
314 KdChangeOption(IN KD_OPTION Option
,
315 IN ULONG InBufferLength OPTIONAL
,
317 IN ULONG OutBufferLength OPTIONAL
,
319 OUT PULONG OutBufferRequiredLength OPTIONAL
)
322 return STATUS_NOT_IMPLEMENTED
;
328 NtQueryDebugFilterState(IN ULONG ComponentId
,
333 /* Convert Level to mask if it isn't already one */
337 /* Check if it is not the default component */
338 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
340 /* No, search for an existing entry in the table */
341 for (i
= 0; i
< KdComponentTableEntries
; i
++)
343 /* Check if it is the right component */
344 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
346 /* Check if mask are matching */
347 return (Level
& KdComponentTable
[i
].Level
) != 0;
352 /* Entry not found in the table, use default mask */
353 return (Level
& Kd_DEFAULT_MASK
) != 0;
358 NtSetDebugFilterState(IN ULONG ComponentId
,
364 /* Convert Level to mask if it isn't already one */
367 Level
&= ~DPFLTR_MASK
;
369 /* Check if it is the default component */
370 if (ComponentId
== DPFLTR_DEFAULT_ID
)
372 /* Yes, modify the default mask */
374 Kd_DEFAULT_MASK
|= Level
;
376 Kd_DEFAULT_MASK
&= ~Level
;
378 return STATUS_SUCCESS
;
381 /* Search for an existing entry */
382 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
384 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
388 /* Check if we have found an existing entry */
389 if (i
== KdComponentTableEntries
)
391 /* Check if we have enough space in the table */
392 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
393 return STATUS_INVALID_PARAMETER_1
;
395 /* Add a new entry */
396 ++KdComponentTableEntries
;
397 KdComponentTable
[i
].ComponentId
= ComponentId
;
398 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
401 /* Update entry table */
403 KdComponentTable
[i
].Level
|= Level
;
405 KdComponentTable
[i
].Level
&= ~Level
;
407 return STATUS_SUCCESS
;
415 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
416 IN PVOID InputBuffer
,
417 IN ULONG InputBufferLength
,
418 OUT PVOID OutputBuffer
,
419 IN ULONG OutputBufferLength
,
420 IN OUT PULONG ReturnLength
,
421 IN KPROCESSOR_MODE PreviousMode
)
424 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
427 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;