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 */
144 Context
->Pc
+= sizeof(ULONG
);
149 /* Get out of here if the Debugger isn't connected */
150 if (KdDebuggerNotPresent
) return FALSE
;
152 /* Save old EIP value */
154 EipOld
= Context
->Eip
;
157 /* Call KDBG if available */
158 Return
= KdbEnterDebuggerException(ExceptionRecord
,
164 /* Bump EIP over int 3 if debugger did not already change it */
165 if (ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
)
167 //DPRINT1("Address: %p. Return: %d\n", EipOld, Return);
170 /* Debugger didn't handle it, please handle! */
171 if (Return
== kdHandleException
) return FALSE
;
173 /* Debugger handled it */
179 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
180 IN PEXCEPTION_RECORD ExceptionRecord
,
183 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
185 /* Get out of here if the Debugger isn't connected */
186 if (KdDebuggerNotPresent
) return FALSE
;
189 * Right now, the GDB wrapper seems to handle exceptions differntly
190 * from KDGB and both are called at different times, while the GDB
191 * one is only called once and that's it. I don't really have the knowledge
192 * to fix the GDB stub, so until then, we'll be using this hack
194 if (WrapperInitRoutine
)
196 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
201 /* Debugger didn't handle it, please handle! */
202 if (Return
== kdHandleException
) return FALSE
;
204 /* Debugger handled it */
208 /* PUBLIC FUNCTIONS *********************************************************/
215 KdDisableDebugger(VOID
)
220 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
222 /* TODO: Disable any breakpoints */
224 /* Disable the Debugger */
225 KdDebuggerEnabled
= FALSE
;
228 KeLowerIrql(OldIrql
);
231 return STATUS_SUCCESS
;
239 KdEnableDebugger(VOID
)
244 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
246 /* TODO: Re-enable any breakpoints */
248 /* Enable the Debugger */
249 KdDebuggerEnabled
= TRUE
;
252 KeLowerIrql(OldIrql
);
255 return STATUS_SUCCESS
;
265 return KdpBreakPending
;
273 KdPowerTransition(ULONG PowerState
)
276 return STATUS_NOT_IMPLEMENTED
;
284 KdChangeOption(IN KD_OPTION Option
,
285 IN ULONG InBufferLength OPTIONAL
,
287 IN ULONG OutBufferLength OPTIONAL
,
289 OUT PULONG OutBufferRequiredLength OPTIONAL
)
292 return STATUS_NOT_IMPLEMENTED
;
298 NtQueryDebugFilterState(IN ULONG ComponentId
,
303 /* Convert Level to mask if it isn't already one */
307 /* Check if it is not the default component */
308 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
310 /* No, search for an existing entry in the table */
311 for (i
= 0; i
< KdComponentTableEntries
; i
++)
313 /* Check if it is the right component */
314 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
316 /* Check if mask are matching */
317 return (Level
& KdComponentTable
[i
].Level
) != 0;
322 /* Entry not found in the table, use default mask */
323 return (Level
& Kd_DEFAULT_MASK
) != 0;
328 NtSetDebugFilterState(IN ULONG ComponentId
,
334 /* Convert Level to mask if it isn't already one */
337 Level
&= ~DPFLTR_MASK
;
339 /* Check if it is the default component */
340 if (ComponentId
== DPFLTR_DEFAULT_ID
)
342 /* Yes, modify the default mask */
344 Kd_DEFAULT_MASK
|= Level
;
346 Kd_DEFAULT_MASK
&= ~Level
;
348 return STATUS_SUCCESS
;
351 /* Search for an existing entry */
352 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
354 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
358 /* Check if we have found an existing entry */
359 if (i
== KdComponentTableEntries
)
361 /* Check if we have enough space in the table */
362 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
363 return STATUS_INVALID_PARAMETER_1
;
365 /* Add a new entry */
366 ++KdComponentTableEntries
;
367 KdComponentTable
[i
].ComponentId
= ComponentId
;
368 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
371 /* Update entry table */
373 KdComponentTable
[i
].Level
|= Level
;
375 KdComponentTable
[i
].Level
&= ~Level
;
377 return STATUS_SUCCESS
;
385 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
386 IN PVOID InputBuffer
,
387 IN ULONG InputBufferLength
,
388 OUT PVOID OutputBuffer
,
389 IN ULONG OutputBufferLength
,
390 IN OUT PULONG ReturnLength
,
391 IN KPROCESSOR_MODE PreviousMode
)
394 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
397 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;