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 KdBreakAfterSymbolLoad
= FALSE
;
20 BOOLEAN KdpBreakPending
= FALSE
;
21 BOOLEAN KdPitchDebugger
= TRUE
;
22 BOOLEAN KdIgnoreUmExceptions
= FALSE
;
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
)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];
110 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
111 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
112 (ExceptionRecord
->NumberParameters
> 0) &&
113 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
114 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
115 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
116 (ExceptionCommand
== BREAKPOINT_PRINT
)))
118 /* Check if this is a debug print */
119 if (ExceptionCommand
== BREAKPOINT_PRINT
)
121 /* Print the string */
122 KdpServiceDispatcher(BREAKPOINT_PRINT
,
123 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
124 ExceptionRecord
->ExceptionInformation
[2]);
127 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
129 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
132 PLDR_DATA_TABLE_ENTRY LdrEntry
;
134 /* Load symbols. Currently implemented only for KDBG! */
135 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
136 KdbSymProcessSymbols(LdrEntry
);
140 /* This we can handle: simply bump the Program Counter */
141 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
145 /* Get out of here if the Debugger isn't connected */
146 if (KdDebuggerNotPresent
) return FALSE
;
149 /* Call KDBG if available */
150 Return
= KdbEnterDebuggerException(ExceptionRecord
,
156 if (WrapperInitRoutine
)
159 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
163 #endif /* not KDBG */
165 /* Debugger didn't handle it, please handle! */
166 if (Return
== kdHandleException
) return FALSE
;
168 /* Debugger handled it */
174 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
175 IN PEXCEPTION_RECORD ExceptionRecord
,
178 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
180 /* Get out of here if the Debugger isn't connected */
181 if (KdDebuggerNotPresent
) return FALSE
;
184 * Right now, the GDB wrapper seems to handle exceptions differntly
185 * from KDGB and both are called at different times, while the GDB
186 * one is only called once and that's it. I don't really have the knowledge
187 * to fix the GDB stub, so until then, we'll be using this hack
189 if (WrapperInitRoutine
)
191 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
196 /* Debugger didn't handle it, please handle! */
197 if (Return
== kdHandleException
) return FALSE
;
199 /* Debugger handled it */
205 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
207 IN KPROCESSOR_MODE PreviousMode
)
209 /* KDBG has its own mechanism for ignoring user mode exceptions */
213 /* PUBLIC FUNCTIONS *********************************************************/
220 KdRefreshDebuggerNotPresent(VOID
)
224 /* Just return whatever was set previously -- FIXME! */
225 return KdDebuggerNotPresent
;
233 KdDisableDebugger(VOID
)
238 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
240 /* TODO: Disable any breakpoints */
242 /* Disable the Debugger */
243 KdDebuggerEnabled
= FALSE
;
246 KeLowerIrql(OldIrql
);
249 return STATUS_SUCCESS
;
257 KdEnableDebugger(VOID
)
262 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
264 /* TODO: Re-enable any breakpoints */
266 /* Enable the Debugger */
267 KdDebuggerEnabled
= TRUE
;
270 KeLowerIrql(OldIrql
);
273 return STATUS_SUCCESS
;
283 return KdpBreakPending
;
291 KdPowerTransition(ULONG PowerState
)
294 return STATUS_NOT_IMPLEMENTED
;
302 KdChangeOption(IN KD_OPTION Option
,
303 IN ULONG InBufferLength OPTIONAL
,
305 IN ULONG OutBufferLength OPTIONAL
,
307 OUT PULONG OutBufferRequiredLength OPTIONAL
)
310 return STATUS_NOT_IMPLEMENTED
;
316 NtQueryDebugFilterState(IN ULONG ComponentId
,
321 /* Convert Level to mask if it isn't already one */
325 /* Check if it is not the default component */
326 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
328 /* No, search for an existing entry in the table */
329 for (i
= 0; i
< KdComponentTableEntries
; i
++)
331 /* Check if it is the right component */
332 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
334 /* Check if mask are matching */
335 return (Level
& KdComponentTable
[i
].Level
) != 0;
340 /* Entry not found in the table, use default mask */
341 return (Level
& Kd_DEFAULT_MASK
) != 0;
346 NtSetDebugFilterState(IN ULONG ComponentId
,
352 /* Convert Level to mask if it isn't already one */
355 Level
&= ~DPFLTR_MASK
;
357 /* Check if it is the default component */
358 if (ComponentId
== DPFLTR_DEFAULT_ID
)
360 /* Yes, modify the default mask */
362 Kd_DEFAULT_MASK
|= Level
;
364 Kd_DEFAULT_MASK
&= ~Level
;
366 return STATUS_SUCCESS
;
369 /* Search for an existing entry */
370 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
372 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
376 /* Check if we have found an existing entry */
377 if (i
== KdComponentTableEntries
)
379 /* Check if we have enough space in the table */
380 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
381 return STATUS_INVALID_PARAMETER_1
;
383 /* Add a new entry */
384 ++KdComponentTableEntries
;
385 KdComponentTable
[i
].ComponentId
= ComponentId
;
386 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
389 /* Update entry table */
391 KdComponentTable
[i
].Level
|= Level
;
393 KdComponentTable
[i
].Level
&= ~Level
;
395 return STATUS_SUCCESS
;
403 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
404 IN PVOID InputBuffer
,
405 IN ULONG InputBufferLength
,
406 OUT PVOID OutputBuffer
,
407 IN ULONG OutputBufferLength
,
408 IN OUT PULONG ReturnLength
,
409 IN KPROCESSOR_MODE PreviousMode
)
412 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
415 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;