2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/kd/kdinit.c
5 * PURPOSE: Kernel Debugger Initializtion
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]);
141 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
143 /* Load symbols. Currently implemented only for KDBG! */
144 KDB_SYMBOLFILE_HOOK((PANSI_STRING
)ExceptionRecord
->ExceptionInformation
[1],
145 (PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2]);
148 /* This we can handle: simply bump EIP */
152 Context
->Pc
+= sizeof(ULONG
);
157 /* Get out of here if the Debugger isn't connected */
158 if (KdDebuggerNotPresent
) return FALSE
;
160 /* Save old EIP value */
162 EipOld
= Context
->Eip
;
165 /* Call KDBG if available */
166 Return
= KdbEnterDebuggerException(ExceptionRecord
,
172 /* Bump EIP over int 3 if debugger did not already change it */
173 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
175 //DPRINT1("Address: %p. Return: %d\n", EipOld, Return);
178 /* Debugger didn't handle it, please handle! */
179 if (Return
== kdHandleException
) return FALSE
;
181 /* Debugger handled it */
187 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
188 IN PEXCEPTION_RECORD ExceptionRecord
,
191 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
193 /* Get out of here if the Debugger isn't connected */
194 if (KdDebuggerNotPresent
) return FALSE
;
197 * Right now, the GDB wrapper seems to handle exceptions differntly
198 * from KDGB and both are called at different times, while the GDB
199 * one is only called once and that's it. I don't really have the knowledge
200 * to fix the GDB stub, so until then, we'll be using this hack
202 if (WrapperInitRoutine
)
204 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
209 /* Debugger didn't handle it, please handle! */
210 if (Return
== kdHandleException
) return FALSE
;
212 /* Debugger handled it */
216 /* PUBLIC FUNCTIONS *********************************************************/
223 KdRefreshDebuggerNotPresent(VOID
)
227 /* Just return whatever was set previously -- FIXME! */
228 return KdDebuggerNotPresent
;
236 KdDisableDebugger(VOID
)
241 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
243 /* TODO: Disable any breakpoints */
245 /* Disable the Debugger */
246 KdDebuggerEnabled
= FALSE
;
249 KeLowerIrql(OldIrql
);
252 return STATUS_SUCCESS
;
260 KdEnableDebugger(VOID
)
265 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
267 /* TODO: Re-enable any breakpoints */
269 /* Enable the Debugger */
270 KdDebuggerEnabled
= TRUE
;
273 KeLowerIrql(OldIrql
);
276 return STATUS_SUCCESS
;
286 return KdpBreakPending
;
294 KdPowerTransition(ULONG PowerState
)
297 return STATUS_NOT_IMPLEMENTED
;
305 KdChangeOption(IN KD_OPTION Option
,
306 IN ULONG InBufferLength OPTIONAL
,
308 IN ULONG OutBufferLength OPTIONAL
,
310 OUT PULONG OutBufferRequiredLength OPTIONAL
)
313 return STATUS_NOT_IMPLEMENTED
;
319 NtQueryDebugFilterState(IN ULONG ComponentId
,
324 /* Convert Level to mask if it isn't already one */
328 /* Check if it is not the default component */
329 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
331 /* No, search for an existing entry in the table */
332 for (i
= 0; i
< KdComponentTableEntries
; i
++)
334 /* Check if it is the right component */
335 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
337 /* Check if mask are matching */
338 return (Level
& KdComponentTable
[i
].Level
) != 0;
343 /* Entry not found in the table, use default mask */
344 return (Level
& Kd_DEFAULT_MASK
) != 0;
349 NtSetDebugFilterState(IN ULONG ComponentId
,
355 /* Convert Level to mask if it isn't already one */
358 Level
&= ~DPFLTR_MASK
;
360 /* Check if it is the default component */
361 if (ComponentId
== DPFLTR_DEFAULT_ID
)
363 /* Yes, modify the default mask */
365 Kd_DEFAULT_MASK
|= Level
;
367 Kd_DEFAULT_MASK
&= ~Level
;
369 return STATUS_SUCCESS
;
372 /* Search for an existing entry */
373 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
375 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
379 /* Check if we have found an existing entry */
380 if (i
== KdComponentTableEntries
)
382 /* Check if we have enough space in the table */
383 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
384 return STATUS_INVALID_PARAMETER_1
;
386 /* Add a new entry */
387 ++KdComponentTableEntries
;
388 KdComponentTable
[i
].ComponentId
= ComponentId
;
389 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
392 /* Update entry table */
394 KdComponentTable
[i
].Level
|= Level
;
396 KdComponentTable
[i
].Level
&= ~Level
;
398 return STATUS_SUCCESS
;
406 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
407 IN PVOID InputBuffer
,
408 IN ULONG InputBufferLength
,
409 OUT PVOID OutputBuffer
,
410 IN ULONG OutputBufferLength
,
411 IN OUT PULONG ReturnLength
,
412 IN KPROCESSOR_MODE PreviousMode
)
415 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
418 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;