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 TAG('R', 'o', 's', ' '): /* ROS-INTERNAL */
55 switch ((ULONG
)Buffer1
)
57 case DumpNonPagedPool
:
58 MiDebugDumpNonPagedPool(FALSE
);
62 KeBugCheck(MANUALLY_INITIATED_CRASH
);
65 case DumpNonPagedPoolStats
:
66 MiDebugDumpNonPagedPoolStats(FALSE
);
69 case DumpNewNonPagedPool
:
70 MiDebugDumpNonPagedPool(TRUE
);
73 case DumpNewNonPagedPoolStats
:
74 MiDebugDumpNonPagedPoolStats(TRUE
);
82 PspDumpThreads(FALSE
);
95 /* Special case for stack frame dumps */
96 case TAG('R', 'o', 's', 'D'):
98 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
103 HalDisplayString ("Invalid debug service call!\n");
112 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
113 IN PKEXCEPTION_FRAME ExceptionFrame
,
114 IN PEXCEPTION_RECORD ExceptionRecord
,
116 IN KPROCESSOR_MODE PreviousMode
,
117 IN BOOLEAN SecondChance
)
119 KD_CONTINUE_TYPE Return
;
120 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
125 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
126 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
127 (ExceptionRecord
->NumberParameters
> 0) &&
128 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
129 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
130 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
131 (ExceptionCommand
== BREAKPOINT_PRINT
)))
133 /* Check if this is a debug print */
134 if (ExceptionCommand
== BREAKPOINT_PRINT
)
136 /* Print the string */
137 KdpServiceDispatcher(BREAKPOINT_PRINT
,
138 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
139 ExceptionRecord
->ExceptionInformation
[2]);
140 Context
->Eax
= STATUS_SUCCESS
;
142 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
144 /* Load symbols. Currently implemented only for KDBG! */
145 KDB_SYMBOLFILE_HOOK((PANSI_STRING
)ExceptionRecord
->ExceptionInformation
[1],
146 (PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2]);
149 /* This we can handle: simply bump EIP */
153 Context
->Pc
+= sizeof(ULONG
);
158 /* Get out of here if the Debugger isn't connected */
159 if (KdDebuggerNotPresent
) return FALSE
;
161 /* Save old EIP value */
163 EipOld
= Context
->Eip
;
167 /* Call KDBG if available */
168 Return
= KdbEnterDebuggerException(ExceptionRecord
,
174 if (WrapperInitRoutine
)
177 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
181 #endif /* not KDBG */
183 /* Bump EIP over int 3 if debugger did not already change it */
184 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
186 //DPRINT1("Address: %p. Return: %d\n", EipOld, Return);
189 /* Debugger didn't handle it, please handle! */
190 if (Return
== kdHandleException
) return FALSE
;
192 /* Debugger handled it */
198 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
199 IN PEXCEPTION_RECORD ExceptionRecord
,
202 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
204 /* Get out of here if the Debugger isn't connected */
205 if (KdDebuggerNotPresent
) return FALSE
;
208 * Right now, the GDB wrapper seems to handle exceptions differntly
209 * from KDGB and both are called at different times, while the GDB
210 * one is only called once and that's it. I don't really have the knowledge
211 * to fix the GDB stub, so until then, we'll be using this hack
213 if (WrapperInitRoutine
)
215 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
220 /* Debugger didn't handle it, please handle! */
221 if (Return
== kdHandleException
) return FALSE
;
223 /* Debugger handled it */
227 /* PUBLIC FUNCTIONS *********************************************************/
234 KdRefreshDebuggerNotPresent(VOID
)
238 /* Just return whatever was set previously -- FIXME! */
239 return KdDebuggerNotPresent
;
247 KdDisableDebugger(VOID
)
252 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
254 /* TODO: Disable any breakpoints */
256 /* Disable the Debugger */
257 KdDebuggerEnabled
= FALSE
;
260 KeLowerIrql(OldIrql
);
263 return STATUS_SUCCESS
;
271 KdEnableDebugger(VOID
)
276 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
278 /* TODO: Re-enable any breakpoints */
280 /* Enable the Debugger */
281 KdDebuggerEnabled
= TRUE
;
284 KeLowerIrql(OldIrql
);
287 return STATUS_SUCCESS
;
297 return KdpBreakPending
;
305 KdPowerTransition(ULONG PowerState
)
308 return STATUS_NOT_IMPLEMENTED
;
316 KdChangeOption(IN KD_OPTION Option
,
317 IN ULONG InBufferLength OPTIONAL
,
319 IN ULONG OutBufferLength OPTIONAL
,
321 OUT PULONG OutBufferRequiredLength OPTIONAL
)
324 return STATUS_NOT_IMPLEMENTED
;
330 NtQueryDebugFilterState(IN ULONG ComponentId
,
335 /* Convert Level to mask if it isn't already one */
339 /* Check if it is not the default component */
340 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
342 /* No, search for an existing entry in the table */
343 for (i
= 0; i
< KdComponentTableEntries
; i
++)
345 /* Check if it is the right component */
346 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
348 /* Check if mask are matching */
349 return (Level
& KdComponentTable
[i
].Level
) != 0;
354 /* Entry not found in the table, use default mask */
355 return (Level
& Kd_DEFAULT_MASK
) != 0;
360 NtSetDebugFilterState(IN ULONG ComponentId
,
366 /* Convert Level to mask if it isn't already one */
369 Level
&= ~DPFLTR_MASK
;
371 /* Check if it is the default component */
372 if (ComponentId
== DPFLTR_DEFAULT_ID
)
374 /* Yes, modify the default mask */
376 Kd_DEFAULT_MASK
|= Level
;
378 Kd_DEFAULT_MASK
&= ~Level
;
380 return STATUS_SUCCESS
;
383 /* Search for an existing entry */
384 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
386 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
390 /* Check if we have found an existing entry */
391 if (i
== KdComponentTableEntries
)
393 /* Check if we have enough space in the table */
394 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
395 return STATUS_INVALID_PARAMETER_1
;
397 /* Add a new entry */
398 ++KdComponentTableEntries
;
399 KdComponentTable
[i
].ComponentId
= ComponentId
;
400 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
403 /* Update entry table */
405 KdComponentTable
[i
].Level
|= Level
;
407 KdComponentTable
[i
].Level
&= ~Level
;
409 return STATUS_SUCCESS
;
417 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
418 IN PVOID InputBuffer
,
419 IN ULONG InputBufferLength
,
420 OUT PVOID OutputBuffer
,
421 IN ULONG OutputBufferLength
,
422 IN OUT PULONG ReturnLength
,
423 IN KPROCESSOR_MODE PreviousMode
)
426 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
429 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;