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 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
)
60 KeBugCheck(MANUALLY_INITIATED_CRASH
);
68 PspDumpThreads(FALSE
);
75 case ThatsWhatSheSaid
:
85 /* Special case for stack frame dumps */
88 KeRosDumpStackFrames((PULONG
)Buffer1
, Buffer1Length
);
93 HalDisplayString ("Invalid debug service call!\n");
102 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame
,
103 IN PKEXCEPTION_FRAME ExceptionFrame
,
104 IN PEXCEPTION_RECORD ExceptionRecord
,
106 IN KPROCESSOR_MODE PreviousMode
,
107 IN BOOLEAN SecondChance
)
109 KD_CONTINUE_TYPE Return
= kdHandleException
;
110 ULONG ExceptionCommand
= ExceptionRecord
->ExceptionInformation
[0];
112 /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
113 if ((ExceptionRecord
->ExceptionCode
== STATUS_BREAKPOINT
) &&
114 (ExceptionRecord
->NumberParameters
> 0) &&
115 ((ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
) ||
116 (ExceptionCommand
== BREAKPOINT_UNLOAD_SYMBOLS
) ||
117 (ExceptionCommand
== BREAKPOINT_COMMAND_STRING
) ||
118 (ExceptionCommand
== BREAKPOINT_PRINT
) ||
119 (ExceptionCommand
== BREAKPOINT_PROMPT
)))
121 /* Check if this is a debug print */
122 if (ExceptionCommand
== BREAKPOINT_PRINT
)
124 /* Print the string */
125 KdpServiceDispatcher(BREAKPOINT_PRINT
,
126 (PVOID
)ExceptionRecord
->ExceptionInformation
[1],
127 ExceptionRecord
->ExceptionInformation
[2]);
130 KeSetContextReturnRegister(Context
, STATUS_SUCCESS
);
133 else if (ExceptionCommand
== BREAKPOINT_LOAD_SYMBOLS
)
135 PLDR_DATA_TABLE_ENTRY LdrEntry
;
137 /* Load symbols. Currently implemented only for KDBG! */
138 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO
)ExceptionRecord
->ExceptionInformation
[2])->BaseOfDll
, NULL
, -1, &LdrEntry
))
139 KdbSymProcessSymbols(LdrEntry
);
141 else if (ExceptionCommand
== BREAKPOINT_PROMPT
)
145 USHORT OutStringLength
;
147 /* Get the response string and length */
148 OutString
= (LPSTR
)Context
->Ebx
;
149 OutStringLength
= (USHORT
)Context
->Edi
;
152 ReturnValue
= KdpPrompt((LPSTR
)ExceptionRecord
->
153 ExceptionInformation
[1],
154 (USHORT
)ExceptionRecord
->
155 ExceptionInformation
[2],
159 /* Return the number of characters that we received */
160 Context
->Eax
= ReturnValue
;
164 /* This we can handle: simply bump the Program Counter */
165 KeSetContextPc(Context
, KeGetContextPc(Context
) + KD_BREAKPOINT_SIZE
);
170 /* Check if this is an assertion failure */
171 if (ExceptionRecord
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
174 DbgPrint("\n!!! Assertion Failure at Address 0x%p !!!\n\n",
175 (PVOID
)Context
->Eip
);
177 /* Bump EIP to the instruction following the int 2C and return */
183 /* Get out of here if the Debugger isn't connected */
184 if (KdDebuggerNotPresent
) return FALSE
;
187 /* Call KDBG if available */
188 Return
= KdbEnterDebuggerException(ExceptionRecord
,
194 if (WrapperInitRoutine
)
197 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
201 #endif /* not KDBG */
203 /* Debugger didn't handle it, please handle! */
204 if (Return
== kdHandleException
) return FALSE
;
206 /* Debugger handled it */
212 KdpCallGdb(IN PKTRAP_FRAME TrapFrame
,
213 IN PEXCEPTION_RECORD ExceptionRecord
,
216 KD_CONTINUE_TYPE Return
= kdDoNotHandleException
;
218 /* Get out of here if the Debugger isn't connected */
219 if (KdDebuggerNotPresent
) return FALSE
;
222 * Right now, the GDB wrapper seems to handle exceptions differntly
223 * from KDGB and both are called at different times, while the GDB
224 * one is only called once and that's it. I don't really have the knowledge
225 * to fix the GDB stub, so until then, we'll be using this hack
227 if (WrapperInitRoutine
)
229 Return
= WrapperTable
.KdpExceptionRoutine(ExceptionRecord
,
234 /* Debugger didn't handle it, please handle! */
235 if (Return
== kdHandleException
) return FALSE
;
237 /* Debugger handled it */
243 KdIsThisAKdTrap(IN PEXCEPTION_RECORD ExceptionRecord
,
245 IN KPROCESSOR_MODE PreviousMode
)
247 /* KDBG has its own mechanism for ignoring user mode exceptions */
251 /* PUBLIC FUNCTIONS *********************************************************/
258 KdRefreshDebuggerNotPresent(VOID
)
262 /* Just return whatever was set previously -- FIXME! */
263 return KdDebuggerNotPresent
;
271 KdDisableDebugger(VOID
)
276 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
278 /* TODO: Disable any breakpoints */
280 /* Disable the Debugger */
281 KdDebuggerEnabled
= FALSE
;
284 KeLowerIrql(OldIrql
);
287 return STATUS_SUCCESS
;
295 KdEnableDebugger(VOID
)
300 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
302 /* TODO: Re-enable any breakpoints */
304 /* Enable the Debugger */
305 KdDebuggerEnabled
= TRUE
;
308 KeLowerIrql(OldIrql
);
311 return STATUS_SUCCESS
;
321 return KdpBreakPending
;
329 KdPowerTransition(ULONG PowerState
)
332 return STATUS_NOT_IMPLEMENTED
;
340 KdChangeOption(IN KD_OPTION Option
,
341 IN ULONG InBufferLength OPTIONAL
,
343 IN ULONG OutBufferLength OPTIONAL
,
345 OUT PULONG OutBufferRequiredLength OPTIONAL
)
348 return STATUS_NOT_IMPLEMENTED
;
354 NtQueryDebugFilterState(IN ULONG ComponentId
,
359 /* Convert Level to mask if it isn't already one */
363 /* Check if it is not the default component */
364 if (ComponentId
!= DPFLTR_DEFAULT_ID
)
366 /* No, search for an existing entry in the table */
367 for (i
= 0; i
< KdComponentTableEntries
; i
++)
369 /* Check if it is the right component */
370 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
372 /* Check if mask are matching */
373 return (Level
& KdComponentTable
[i
].Level
) ? TRUE
: FALSE
;
378 /* Entry not found in the table, use default mask */
379 return (Level
& Kd_DEFAULT_MASK
) ? TRUE
: FALSE
;
384 NtSetDebugFilterState(IN ULONG ComponentId
,
390 /* Convert Level to mask if it isn't already one */
393 Level
&= ~DPFLTR_MASK
;
395 /* Check if it is the default component */
396 if (ComponentId
== DPFLTR_DEFAULT_ID
)
398 /* Yes, modify the default mask */
400 Kd_DEFAULT_MASK
|= Level
;
402 Kd_DEFAULT_MASK
&= ~Level
;
404 return STATUS_SUCCESS
;
407 /* Search for an existing entry */
408 for (i
= 0; i
< KdComponentTableEntries
; i
++ )
410 if (ComponentId
== KdComponentTable
[i
].ComponentId
)
414 /* Check if we have found an existing entry */
415 if (i
== KdComponentTableEntries
)
417 /* Check if we have enough space in the table */
418 if (i
== MAX_KD_COMPONENT_TABLE_ENTRIES
)
419 return STATUS_INVALID_PARAMETER_1
;
421 /* Add a new entry */
422 ++KdComponentTableEntries
;
423 KdComponentTable
[i
].ComponentId
= ComponentId
;
424 KdComponentTable
[i
].Level
= Kd_DEFAULT_MASK
;
427 /* Update entry table */
429 KdComponentTable
[i
].Level
|= Level
;
431 KdComponentTable
[i
].Level
&= ~Level
;
433 return STATUS_SUCCESS
;
441 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
442 IN PVOID InputBuffer
,
443 IN ULONG InputBufferLength
,
444 OUT PVOID OutputBuffer
,
445 IN ULONG OutputBufferLength
,
446 IN OUT PULONG ReturnLength
,
447 IN KPROCESSOR_MODE PreviousMode
)
450 return KdpServiceDispatcher(Command
, InputBuffer
, InputBufferLength
);
453 PKDEBUG_ROUTINE KiDebugRoutine
= KdpEnterDebuggerException
;