2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/kd/kdmain.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 KD_CONTEXT KdpContext
;
24 ULONG Kd_WIN2000_Mask
;
25 VOID NTAPI
PspDumpThreads(BOOLEAN SystemThreads
);
32 #define MAX_KD_COMPONENT_TABLE_ENTRIES 128
33 KD_COMPONENT_DATA KdComponentTable
[MAX_KD_COMPONENT_TABLE_ENTRIES
];
34 ULONG KdComponentTableEntries
= 0;
36 ULONG Kd_DEFAULT_MASK
= 1 << DPFLTR_ERROR_LEVEL
;
38 /* PRIVATE FUNCTIONS *********************************************************/
42 KdpServiceDispatcher(ULONG Service
,
50 case BREAKPOINT_PRINT
: /* DbgPrint */
51 Result
= KdpPrintString(Buffer1
, Buffer1Length
);
55 case ' soR': /* ROS-INTERNAL */
57 switch ((ULONG_PTR
)Buffer1
)
64 PspDumpThreads(FALSE
);
68 MmDumpArmPfnDatabase(FALSE
);
77 #if defined(_M_IX86) && !defined(_WINKD_) // See ke/i386/traphdlr.c
78 /* Register a debug callback */
81 switch (Buffer1Length
)
83 case ID_Win32PreServiceHook
:
84 KeWin32PreServiceHook
= Buffer1
;
87 case ID_Win32PostServiceHook
:
88 KeWin32PostServiceHook
= Buffer1
;
96 /* Special case for stack frame dumps */
99 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
104 /* Register KDBG CLI callback */
107 Result
= KdbRegisterCliCallback(Buffer1
, Buffer1Length
);
113 DPRINT1("Invalid debug service call!\n");
114 HalDisplayString("Invalid debug service call!\r\n");
123 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
124 IN PKEXCEPTION_FRAME ExceptionFrame
,
125 IN PEXCEPTION_RECORD ExceptionRecord
,
127 IN KPROCESSOR_MODE PreviousMode
,
128 IN BOOLEAN SecondChance
)
130 KD_CONTINUE_TYPE Return
= kdHandleException
;
131 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
133 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
134 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
135 (ExceptionRecord
->NumberParameters
> 0) &&
136 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
137 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
138 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
139 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
140 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
142 /* Check if this is a debug print */
143 if (ExceptionCommand
== BREAKPOINT_PRINT
)
145 /* Print the string */
146 KdpServiceDispatcher(BREAKPOINT_PRINT
,
147 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
148 ExceptionRecord
->ExceptionInformation
[2]);
151 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
154 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
156 PLDR_DATA_TABLE_ENTRY LdrEntry
;
158 /* Load symbols. Currently implemented only for KDBG! */
159 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
160 KdbSymProcessSymbols(LdrEntry
);
162 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
166 USHORT OutStringLength
;
168 /* Get the response string and length */
169 OutString
= (LPSTR
)Context
->Ebx
;
170 OutStringLength
= (USHORT
)Context
->Edi
;
173 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
174 ExceptionInformation
[1],
175 (USHORT
)ExceptionRecord
->
176 ExceptionInformation
[2],
181 /* Return the number of characters that we received */
182 Context
->Eax
= ReturnValue
;
186 /* This we can handle: simply bump the Program Counter */
187 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
192 /* Check if this is an assertion failure */
193 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
195 /* Bump EIP to the instruction following the int 2C */
200 /* Get out of here if the Debugger isn't connected */
201 if (KdDebuggerNotPresent
) return FALSE
;
204 /* Call KDBG if available */
205 Return
= KdbEnterDebuggerException(ExceptionRecord
,
211 if (WrapperInitRoutine
)
214 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
218 #endif /* not KDBG */
220 /* Debugger didn't handle it, please handle! */
221 if (Return
== kdHandleException
) return FALSE
;
223 /* Debugger handled it */
229 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
230 IN PEXCEPTION_RECORD ExceptionRecord
,
233 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
235 /* Get out of here if the Debugger isn't connected */
236 if (KdDebuggerNotPresent
) return FALSE
;
239 * Right now, the GDB wrapper seems to handle exceptions differntly
240 * from KDGB and both are called at different times, while the GDB
241 * one is only called once and that's it. I don't really have the knowledge
242 * to fix the GDB stub, so until then, we'll be using this hack
244 if (WrapperInitRoutine
)
246 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
251 /* Debugger didn't handle it, please handle! */
252 if (Return
== kdHandleException
) return FALSE
;
254 /* Debugger handled it */
260 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
262 IN KPROCESSOR_MODE PreviousMode
)
264 /* KDBG has its own mechanism for ignoring user mode exceptions */
268 /* PUBLIC FUNCTIONS *********************************************************/
275 KdRefreshDebuggerNotPresent(VOID
)
279 /* Just return whatever was set previously -- FIXME! */
280 return KdDebuggerNotPresent
;
288 KdDisableDebugger(VOID
)
293 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
295 /* TODO: Disable any breakpoints */
297 /* Disable the Debugger */
298 KdDebuggerEnabled
= FALSE
;
299 SharedUserData
->KdDebuggerEnabled
= FALSE
;
302 KeLowerIrql(OldIrql
);
305 return STATUS_SUCCESS
;
313 KdEnableDebugger(VOID
)
318 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
320 /* TODO: Re-enable any breakpoints */
322 /* Enable the Debugger */
323 KdDebuggerEnabled
= TRUE
;
324 SharedUserData
->KdDebuggerEnabled
= TRUE
;
327 KeLowerIrql(OldIrql
);
330 return STATUS_SUCCESS
;
340 return KdpBreakPending
;
348 KdPowerTransition(ULONG PowerState
)
351 return STATUS_NOT_IMPLEMENTED
;
359 KdChangeOption(IN KD_OPTION Option
,
360 IN ULONG InBufferLength OPTIONAL
,
362 IN ULONG OutBufferLength OPTIONAL
,
364 OUT PULONG OutBufferRequiredLength OPTIONAL
)
367 return STATUS_NOT_IMPLEMENTED
;
373 NtQueryDebugFilterState(IN ULONG ComponentId
,
378 /* Convert Level to mask if it isn't already one */
382 /* Check if it is not the default component */
383 if (ComponentId
!= MAXULONG
)
385 /* No, search for an existing entry in the table */
386 for (i
= 0; i
< KdComponentTableEntries
; i
++)
388 /* Check if it is the right component */
389 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
391 /* Check if mask are matching */
392 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
397 /* Entry not found in the table, use default mask */
398 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
403 NtSetDebugFilterState(IN ULONG ComponentId
,
409 /* Convert Level to mask if it isn't already one */
412 Level
&= ~DPFLTR_MASK
;
414 /* Check if it is the default component */
415 if (ComponentId
== MAXULONG
)
417 /* Yes, modify the default mask */
419 Kd_DEFAULT_MASK
|= Level
;
421 Kd_DEFAULT_MASK
&= ~Level
;
423 return STATUS_SUCCESS
;
426 /* Search for an existing entry */
427 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
429 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
433 /* Check if we have found an existing entry */
434 if (i
== KdComponentTableEntries
)
436 /* Check if we have enough space in the table */
437 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
438 return STATUS_INVALID_PARAMETER_1
;
440 /* Add a new entry */
441 ++KdComponentTableEntries
;
442 KdComponentTable
[i
].ComponentId
= ComponentId
;
443 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
446 /* Update entry table */
448 KdComponentTable
[i
].Level
|= Level
;
450 KdComponentTable
[i
].Level
&= ~Level
;
452 return STATUS_SUCCESS
;
460 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
461 IN PVOID InputBuffer
,
462 IN ULONG InputBufferLength
,
463 OUT PVOID OutputBuffer
,
464 IN ULONG OutputBufferLength
,
465 IN OUT PULONG ReturnLength
,
466 IN KPROCESSOR_MODE PreviousMode
)
469 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
472 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;