- Also bump PC for ARM after a KD exception.
[reactos.git] / reactos / ntoskrnl / kd / kdmain.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/kd/kdinit.c
5 * PURPOSE: Kernel Debugger Initializtion
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 */
9
10 #include <ntoskrnl.h>
11 #define NDEBUG
12 #include <internal/debug.h>
13
14 /* VARIABLES ***************************************************************/
15
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);
24
25 typedef struct
26 {
27 ULONG ComponentId;
28 ULONG Level;
29 } KD_COMPONENT_DATA;
30 #define MAX_KD_COMPONENT_TABLE_ENTRIES 128
31 KD_COMPONENT_DATA KdComponentTable[MAX_KD_COMPONENT_TABLE_ENTRIES];
32 ULONG KdComponentTableEntries = 0;
33
34 ULONG Kd_DEFAULT_MASK = 1 << DPFLTR_ERROR_LEVEL;
35
36 /* PRIVATE FUNCTIONS *********************************************************/
37
38 ULONG
39 STDCALL
40 KdpServiceDispatcher(ULONG Service,
41 PVOID Buffer1,
42 ULONG Buffer1Length)
43 {
44 ULONG Result = 0;
45
46 switch (Service)
47 {
48 case BREAKPOINT_PRINT: /* DbgPrint */
49 Result = KdpPrintString(Buffer1, Buffer1Length);
50 break;
51
52 #ifdef DBG
53 case TAG('R', 'o', 's', ' '): /* ROS-INTERNAL */
54 {
55 switch ((ULONG)Buffer1)
56 {
57 case DumpNonPagedPool:
58 MiDebugDumpNonPagedPool(FALSE);
59 break;
60
61 case ManualBugCheck:
62 KEBUGCHECK(MANUALLY_INITIATED_CRASH);
63 break;
64
65 case DumpNonPagedPoolStats:
66 MiDebugDumpNonPagedPoolStats(FALSE);
67 break;
68
69 case DumpNewNonPagedPool:
70 MiDebugDumpNonPagedPool(TRUE);
71 break;
72
73 case DumpNewNonPagedPoolStats:
74 MiDebugDumpNonPagedPoolStats(TRUE);
75 break;
76
77 case DumpAllThreads:
78 PspDumpThreads(TRUE);
79 break;
80
81 case DumpUserThreads:
82 PspDumpThreads(FALSE);
83 break;
84
85 case EnterDebugger:
86 DbgBreakPoint();
87 break;
88
89 default:
90 break;
91 }
92 }
93 #endif
94 default:
95 HalDisplayString ("Invalid debug service call!\n");
96 break;
97 }
98
99 return Result;
100 }
101
102 BOOLEAN
103 NTAPI
104 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
105 IN PKEXCEPTION_FRAME ExceptionFrame,
106 IN PEXCEPTION_RECORD ExceptionRecord,
107 IN PCONTEXT Context,
108 IN KPROCESSOR_MODE PreviousMode,
109 IN BOOLEAN SecondChance)
110 {
111 KD_CONTINUE_TYPE Return;
112 ULONG ExceptionCommand = ExceptionRecord->ExceptionInformation[0];
113 #ifdef _M_IX86
114 ULONG EipOld;
115 #endif
116
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)))
124 {
125 /* Check if this is a debug print */
126 if (ExceptionCommand == BREAKPOINT_PRINT)
127 {
128 /* Print the string */
129 KdpServiceDispatcher(BREAKPOINT_PRINT,
130 (PVOID)ExceptionRecord->ExceptionInformation[1],
131 ExceptionRecord->ExceptionInformation[2]);
132 }
133 else if (ExceptionCommand == BREAKPOINT_LOAD_SYMBOLS)
134 {
135 /* Load symbols. Currently implemented only for KDBG! */
136 KDB_SYMBOLFILE_HOOK((PANSI_STRING)ExceptionRecord->ExceptionInformation[1],
137 (PKD_SYMBOLS_INFO)ExceptionRecord->ExceptionInformation[2]);
138 }
139
140 /* This we can handle: simply bump EIP */
141 #ifdef _M_IX86
142 Context->Eip++;
143 #elif _M_ARM
144 Context->Pc += sizeof(ULONG);
145 #endif
146 return TRUE;
147 }
148
149 /* Get out of here if the Debugger isn't connected */
150 if (KdDebuggerNotPresent) return FALSE;
151
152 /* Save old EIP value */
153 #ifdef _M_IX86
154 EipOld = Context->Eip;
155 #endif
156
157 /* Call KDBG if available */
158 Return = KdbEnterDebuggerException(ExceptionRecord,
159 PreviousMode,
160 Context,
161 TrapFrame,
162 !SecondChance);
163
164 /* Bump EIP over int 3 if debugger did not already change it */
165 if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT)
166 {
167 //DPRINT1("Address: %p. Return: %d\n", EipOld, Return);
168 }
169
170 /* Debugger didn't handle it, please handle! */
171 if (Return == kdHandleException) return FALSE;
172
173 /* Debugger handled it */
174 return TRUE;
175 }
176
177 BOOLEAN
178 NTAPI
179 KdpCallGdb(IN PKTRAP_FRAME TrapFrame,
180 IN PEXCEPTION_RECORD ExceptionRecord,
181 IN PCONTEXT Context)
182 {
183 KD_CONTINUE_TYPE Return = kdDoNotHandleException;
184
185 /* Get out of here if the Debugger isn't connected */
186 if (KdDebuggerNotPresent) return FALSE;
187
188 /* FIXME:
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
193 */
194 if (WrapperInitRoutine)
195 {
196 Return = WrapperTable.KdpExceptionRoutine(ExceptionRecord,
197 Context,
198 TrapFrame);
199 }
200
201 /* Debugger didn't handle it, please handle! */
202 if (Return == kdHandleException) return FALSE;
203
204 /* Debugger handled it */
205 return TRUE;
206 }
207
208 /* PUBLIC FUNCTIONS *********************************************************/
209
210 /*
211 * @implemented
212 */
213 NTSTATUS
214 STDCALL
215 KdDisableDebugger(VOID)
216 {
217 KIRQL OldIrql;
218
219 /* Raise IRQL */
220 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
221
222 /* TODO: Disable any breakpoints */
223
224 /* Disable the Debugger */
225 KdDebuggerEnabled = FALSE;
226
227 /* Lower the IRQL */
228 KeLowerIrql(OldIrql);
229
230 /* Return success */
231 return STATUS_SUCCESS;
232 }
233
234 /*
235 * @implemented
236 */
237 NTSTATUS
238 STDCALL
239 KdEnableDebugger(VOID)
240 {
241 KIRQL OldIrql;
242
243 /* Raise IRQL */
244 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
245
246 /* TODO: Re-enable any breakpoints */
247
248 /* Enable the Debugger */
249 KdDebuggerEnabled = TRUE;
250
251 /* Lower the IRQL */
252 KeLowerIrql(OldIrql);
253
254 /* Return success */
255 return STATUS_SUCCESS;
256 }
257
258 /*
259 * @implemented
260 */
261 BOOLEAN
262 STDCALL
263 KdPollBreakIn(VOID)
264 {
265 return KdpBreakPending;
266 }
267
268 /*
269 * @unimplemented
270 */
271 NTSTATUS
272 STDCALL
273 KdPowerTransition(ULONG PowerState)
274 {
275 UNIMPLEMENTED;
276 return STATUS_NOT_IMPLEMENTED;
277 }
278
279 /*
280 * @unimplemented
281 */
282 NTSTATUS
283 NTAPI
284 KdChangeOption(IN KD_OPTION Option,
285 IN ULONG InBufferLength OPTIONAL,
286 IN PVOID InBuffer,
287 IN ULONG OutBufferLength OPTIONAL,
288 OUT PVOID OutBuffer,
289 OUT PULONG OutBufferRequiredLength OPTIONAL)
290 {
291 UNIMPLEMENTED;
292 return STATUS_NOT_IMPLEMENTED;
293 }
294
295
296 NTSTATUS
297 NTAPI
298 NtQueryDebugFilterState(IN ULONG ComponentId,
299 IN ULONG Level)
300 {
301 ULONG i;
302
303 /* Convert Level to mask if it isn't already one */
304 if (Level < 32)
305 Level = 1 << Level;
306
307 /* Check if it is not the default component */
308 if (ComponentId != DPFLTR_DEFAULT_ID)
309 {
310 /* No, search for an existing entry in the table */
311 for (i = 0; i < KdComponentTableEntries; i++)
312 {
313 /* Check if it is the right component */
314 if (ComponentId == KdComponentTable[i].ComponentId)
315 {
316 /* Check if mask are matching */
317 return (Level & KdComponentTable[i].Level) != 0;
318 }
319 }
320 }
321
322 /* Entry not found in the table, use default mask */
323 return (Level & Kd_DEFAULT_MASK) != 0;
324 }
325
326 NTSTATUS
327 NTAPI
328 NtSetDebugFilterState(IN ULONG ComponentId,
329 IN ULONG Level,
330 IN BOOLEAN State)
331 {
332 ULONG i;
333
334 /* Convert Level to mask if it isn't already one */
335 if (Level < 32)
336 Level = 1 << Level;
337 Level &= ~DPFLTR_MASK;
338
339 /* Check if it is the default component */
340 if (ComponentId == DPFLTR_DEFAULT_ID)
341 {
342 /* Yes, modify the default mask */
343 if (State)
344 Kd_DEFAULT_MASK |= Level;
345 else
346 Kd_DEFAULT_MASK &= ~Level;
347
348 return STATUS_SUCCESS;
349 }
350
351 /* Search for an existing entry */
352 for (i = 0; i < KdComponentTableEntries; i++ )
353 {
354 if (ComponentId == KdComponentTable[i].ComponentId)
355 break;
356 }
357
358 /* Check if we have found an existing entry */
359 if (i == KdComponentTableEntries)
360 {
361 /* Check if we have enough space in the table */
362 if (i == MAX_KD_COMPONENT_TABLE_ENTRIES)
363 return STATUS_INVALID_PARAMETER_1;
364
365 /* Add a new entry */
366 ++KdComponentTableEntries;
367 KdComponentTable[i].ComponentId = ComponentId;
368 KdComponentTable[i].Level = Kd_DEFAULT_MASK;
369 }
370
371 /* Update entry table */
372 if (State)
373 KdComponentTable[i].Level |= Level;
374 else
375 KdComponentTable[i].Level &= ~Level;
376
377 return STATUS_SUCCESS;
378 }
379
380 /*
381 * @unimplemented
382 */
383 NTSTATUS
384 NTAPI
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)
392 {
393 /* HACK */
394 return KdpServiceDispatcher(Command, InputBuffer, InputBufferLength);
395 }
396
397 PKDEBUG_ROUTINE KiDebugRoutine = KdpEnterDebuggerException;
398
399 /* EOF */