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)
12 #include <internal/debug.h>
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 STDCALL
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 HalDisplayString ("Invalid debug service call!\n");
104 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
105 IN PKEXCEPTION_FRAME ExceptionFrame
,
106 IN PEXCEPTION_RECORD ExceptionRecord
,
108 IN KPROCESSOR_MODE PreviousMode
,
109 IN BOOLEAN SecondChance
)
111 KD_CONTINUE_TYPE Return
;
112 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
117 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
118 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
119 (ExceptionRecord
->NumberParameters
> 0) &&
120 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
121 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
122 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
123 (ExceptionCommand
== BREAKPOINT_PRINT
)))
125 /* Check if this is a debug print */
126 if (ExceptionCommand
== BREAKPOINT_PRINT
)
128 /* Print the string */
129 KdpServiceDispatcher(BREAKPOINT_PRINT
,
130 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
131 ExceptionRecord
->ExceptionInformation
[2]);
133 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
135 /* Load symbols. Currently implemented only for KDBG! */
136 KDB_SYMBOLFILE_HOOK((PANSI_STRING
)ExceptionRecord
->ExceptionInformation
[1],
137 (PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2]);
140 /* This we can handle: simply bump EIP */
147 /* Get out of here if the Debugger isn't connected */
148 if (KdDebuggerNotPresent
) return FALSE
;
150 /* Save old EIP value */
152 EipOld
= Context
->Eip
;
155 /* Call KDBG if available */
156 Return
= KdbEnterDebuggerException(ExceptionRecord
,
162 /* Bump EIP over int 3 if debugger did not already change it */
163 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
165 //DPRINT1("Address: %p. Return: %d\n", EipOld, Return);
168 /* Convert return to BOOLEAN */
169 if (Return
== kdDoNotHandleException
) return FALSE
;
175 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
176 IN PEXCEPTION_RECORD ExceptionRecord
,
179 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
181 /* Get out of here if the Debugger isn't connected */
182 if (KdDebuggerNotPresent
) return FALSE
;
185 * Right now, the GDB wrapper seems to handle exceptions differntly
186 * from KDGB and both are called at different times, while the GDB
187 * one is only called once and that's it. I don't really have the knowledge
188 * to fix the GDB stub, so until then, we'll be using this hack
190 if (WrapperInitRoutine
)
192 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
197 /* Convert return to BOOLEAN */
198 if (Return
== kdDoNotHandleException
) return FALSE
;
202 /* PUBLIC FUNCTIONS *********************************************************/
209 KdDisableDebugger(VOID
)
214 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
216 /* TODO: Disable any breakpoints */
218 /* Disable the Debugger */
219 KdDebuggerEnabled
= FALSE
;
222 KeLowerIrql(OldIrql
);
225 return STATUS_SUCCESS
;
233 KdEnableDebugger(VOID
)
238 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
240 /* TODO: Re-enable any breakpoints */
242 /* Enable the Debugger */
243 KdDebuggerEnabled
= TRUE
;
246 KeLowerIrql(OldIrql
);
249 return STATUS_SUCCESS
;
259 return KdpBreakPending
;
267 KdPowerTransition(ULONG PowerState
)
270 return STATUS_NOT_IMPLEMENTED
;
278 KdChangeOption(IN KD_OPTION Option
,
279 IN ULONG InBufferLength OPTIONAL
,
281 IN ULONG OutBufferLength OPTIONAL
,
283 OUT PULONG OutBufferRequiredLength OPTIONAL
)
286 return STATUS_NOT_IMPLEMENTED
;
292 NtQueryDebugFilterState(IN ULONG ComponentId
,
297 /* Convert Level to mask if it isn't already one */
301 /* Check if it is not the default component */
302 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
304 /* No, search for an existing entry in the table */
305 for (i
= 0; i
< KdComponentTableEntries
; i
++)
307 /* Check if it is the right component */
308 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
310 /* Check if mask are matching */
311 return (Level
& KdComponentTable
[i
].Level
) != 0;
316 /* Entry not found in the table, use default mask */
317 return (Level
& Kd_DEFAULT_MASK
) != 0;
322 NtSetDebugFilterState(IN ULONG ComponentId
,
328 /* Convert Level to mask if it isn't already one */
331 Level
&= ~DPFLTR_MASK
;
333 /* Check if it is the default component */
334 if (ComponentId
== DPFLTR_DEFAULT_ID
)
336 /* Yes, modify the default mask */
338 Kd_DEFAULT_MASK
|= Level
;
340 Kd_DEFAULT_MASK
&= ~Level
;
342 return STATUS_SUCCESS
;
345 /* Search for an existing entry */
346 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
348 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
352 /* Check if we have found an existing entry */
353 if (i
== KdComponentTableEntries
)
355 /* Check if we have enough space in the table */
356 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
357 return STATUS_INVALID_PARAMETER_1
;
359 /* Add a new entry */
360 ++KdComponentTableEntries
;
361 KdComponentTable
[i
].ComponentId
= ComponentId
;
362 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
365 /* Update entry table */
367 KdComponentTable
[i
].Level
|= Level
;
369 KdComponentTable
[i
].Level
&= ~Level
;
371 return STATUS_SUCCESS
;
379 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
380 IN PVOID InputBuffer
,
381 IN ULONG InputBufferLength
,
382 OUT PVOID OutputBuffer
,
383 IN ULONG OutputBufferLength
,
384 IN OUT PULONG ReturnLength
,
385 IN KPROCESSOR_MODE PreviousMode
)
388 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
391 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;