Merge amd64 NDK from amd64 branch:
[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 Initialization
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 */
9
10 #include <ntoskrnl.h>
11 #define NDEBUG
12 #include <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 NTAPI 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 NTAPI
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 #if DBG
53 case ' soR': /* ROS-INTERNAL */
54 {
55 switch ((ULONG)Buffer1)
56 {
57 case ManualBugCheck:
58 KeBugCheck(MANUALLY_INITIATED_CRASH);
59 break;
60
61 case DumpAllThreads:
62 PspDumpThreads(TRUE);
63 break;
64
65 case DumpUserThreads:
66 PspDumpThreads(FALSE);
67 break;
68
69 case EnterDebugger:
70 DbgBreakPoint();
71 break;
72
73 case ThatsWhatSheSaid:
74 MmDumpPfnDatabase();
75 break;
76
77 default:
78 break;
79 }
80 break;
81 }
82
83 /* Special case for stack frame dumps */
84 case 'DsoR':
85 {
86 KeRosDumpStackFrames((PULONG)Buffer1, Buffer1Length);
87 break;
88 }
89 #endif
90 default:
91 HalDisplayString ("Invalid debug service call!\n");
92 break;
93 }
94
95 return Result;
96 }
97
98 BOOLEAN
99 NTAPI
100 KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
101 IN PKEXCEPTION_FRAME ExceptionFrame,
102 IN PEXCEPTION_RECORD ExceptionRecord,
103 IN PCONTEXT Context,
104 IN KPROCESSOR_MODE PreviousMode,
105 IN BOOLEAN SecondChance)
106 {
107 KD_CONTINUE_TYPE Return = kdHandleException;
108 ULONG ExceptionCommand = ExceptionRecord->ExceptionInformation[0];
109
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)))
117 {
118 /* Check if this is a debug print */
119 if (ExceptionCommand == BREAKPOINT_PRINT)
120 {
121 /* Print the string */
122 KdpServiceDispatcher(BREAKPOINT_PRINT,
123 (PVOID)ExceptionRecord->ExceptionInformation[1],
124 ExceptionRecord->ExceptionInformation[2]);
125
126 /* Return success */
127 KeSetContextReturnRegister(Context, STATUS_SUCCESS);
128 }
129 else if (ExceptionCommand == BREAKPOINT_LOAD_SYMBOLS)
130 {
131 #ifdef KDBG
132 PLDR_DATA_TABLE_ENTRY LdrEntry;
133
134 /* Load symbols. Currently implemented only for KDBG! */
135 if(KdbpSymFindModule(((PKD_SYMBOLS_INFO)ExceptionRecord->ExceptionInformation[2])->BaseOfDll, NULL, -1, &LdrEntry))
136 KdbSymProcessSymbols(LdrEntry);
137 #endif
138 }
139
140 /* This we can handle: simply bump the Program Counter */
141 KeSetContextPc(Context, KeGetContextPc(Context) + KD_BREAKPOINT_SIZE);
142 return TRUE;
143 }
144
145 /* Get out of here if the Debugger isn't connected */
146 if (KdDebuggerNotPresent) return FALSE;
147
148 #ifdef KDBG
149 /* Call KDBG if available */
150 Return = KdbEnterDebuggerException(ExceptionRecord,
151 PreviousMode,
152 Context,
153 TrapFrame,
154 !SecondChance);
155 #else /* not KDBG */
156 if (WrapperInitRoutine)
157 {
158 /* Call GDB */
159 Return = WrapperTable.KdpExceptionRoutine(ExceptionRecord,
160 Context,
161 TrapFrame);
162 }
163 #endif /* not KDBG */
164
165 /* Debugger didn't handle it, please handle! */
166 if (Return == kdHandleException) return FALSE;
167
168 /* Debugger handled it */
169 return TRUE;
170 }
171
172 BOOLEAN
173 NTAPI
174 KdpCallGdb(IN PKTRAP_FRAME TrapFrame,
175 IN PEXCEPTION_RECORD ExceptionRecord,
176 IN PCONTEXT Context)
177 {
178 KD_CONTINUE_TYPE Return = kdDoNotHandleException;
179
180 /* Get out of here if the Debugger isn't connected */
181 if (KdDebuggerNotPresent) return FALSE;
182
183 /* FIXME:
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
188 */
189 if (WrapperInitRoutine)
190 {
191 Return = WrapperTable.KdpExceptionRoutine(ExceptionRecord,
192 Context,
193 TrapFrame);
194 }
195
196 /* Debugger didn't handle it, please handle! */
197 if (Return == kdHandleException) return FALSE;
198
199 /* Debugger handled it */
200 return TRUE;
201 }
202
203 /* PUBLIC FUNCTIONS *********************************************************/
204
205 /*
206 * @implemented
207 */
208 BOOLEAN
209 NTAPI
210 KdRefreshDebuggerNotPresent(VOID)
211 {
212 UNIMPLEMENTED;
213
214 /* Just return whatever was set previously -- FIXME! */
215 return KdDebuggerNotPresent;
216 }
217
218 /*
219 * @implemented
220 */
221 NTSTATUS
222 NTAPI
223 KdDisableDebugger(VOID)
224 {
225 KIRQL OldIrql;
226
227 /* Raise IRQL */
228 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
229
230 /* TODO: Disable any breakpoints */
231
232 /* Disable the Debugger */
233 KdDebuggerEnabled = FALSE;
234
235 /* Lower the IRQL */
236 KeLowerIrql(OldIrql);
237
238 /* Return success */
239 return STATUS_SUCCESS;
240 }
241
242 /*
243 * @implemented
244 */
245 NTSTATUS
246 NTAPI
247 KdEnableDebugger(VOID)
248 {
249 KIRQL OldIrql;
250
251 /* Raise IRQL */
252 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
253
254 /* TODO: Re-enable any breakpoints */
255
256 /* Enable the Debugger */
257 KdDebuggerEnabled = TRUE;
258
259 /* Lower the IRQL */
260 KeLowerIrql(OldIrql);
261
262 /* Return success */
263 return STATUS_SUCCESS;
264 }
265
266 /*
267 * @implemented
268 */
269 BOOLEAN
270 NTAPI
271 KdPollBreakIn(VOID)
272 {
273 return KdpBreakPending;
274 }
275
276 /*
277 * @unimplemented
278 */
279 NTSTATUS
280 NTAPI
281 KdPowerTransition(ULONG PowerState)
282 {
283 UNIMPLEMENTED;
284 return STATUS_NOT_IMPLEMENTED;
285 }
286
287 /*
288 * @unimplemented
289 */
290 NTSTATUS
291 NTAPI
292 KdChangeOption(IN KD_OPTION Option,
293 IN ULONG InBufferLength OPTIONAL,
294 IN PVOID InBuffer,
295 IN ULONG OutBufferLength OPTIONAL,
296 OUT PVOID OutBuffer,
297 OUT PULONG OutBufferRequiredLength OPTIONAL)
298 {
299 UNIMPLEMENTED;
300 return STATUS_NOT_IMPLEMENTED;
301 }
302
303
304 NTSTATUS
305 NTAPI
306 NtQueryDebugFilterState(IN ULONG ComponentId,
307 IN ULONG Level)
308 {
309 ULONG i;
310
311 /* Convert Level to mask if it isn't already one */
312 if (Level < 32)
313 Level = 1 << Level;
314
315 /* Check if it is not the default component */
316 if (ComponentId != DPFLTR_DEFAULT_ID)
317 {
318 /* No, search for an existing entry in the table */
319 for (i = 0; i < KdComponentTableEntries; i++)
320 {
321 /* Check if it is the right component */
322 if (ComponentId == KdComponentTable[i].ComponentId)
323 {
324 /* Check if mask are matching */
325 return (Level & KdComponentTable[i].Level) != 0;
326 }
327 }
328 }
329
330 /* Entry not found in the table, use default mask */
331 return (Level & Kd_DEFAULT_MASK) != 0;
332 }
333
334 NTSTATUS
335 NTAPI
336 NtSetDebugFilterState(IN ULONG ComponentId,
337 IN ULONG Level,
338 IN BOOLEAN State)
339 {
340 ULONG i;
341
342 /* Convert Level to mask if it isn't already one */
343 if (Level < 32)
344 Level = 1 << Level;
345 Level &= ~DPFLTR_MASK;
346
347 /* Check if it is the default component */
348 if (ComponentId == DPFLTR_DEFAULT_ID)
349 {
350 /* Yes, modify the default mask */
351 if (State)
352 Kd_DEFAULT_MASK |= Level;
353 else
354 Kd_DEFAULT_MASK &= ~Level;
355
356 return STATUS_SUCCESS;
357 }
358
359 /* Search for an existing entry */
360 for (i = 0; i < KdComponentTableEntries; i++ )
361 {
362 if (ComponentId == KdComponentTable[i].ComponentId)
363 break;
364 }
365
366 /* Check if we have found an existing entry */
367 if (i == KdComponentTableEntries)
368 {
369 /* Check if we have enough space in the table */
370 if (i == MAX_KD_COMPONENT_TABLE_ENTRIES)
371 return STATUS_INVALID_PARAMETER_1;
372
373 /* Add a new entry */
374 ++KdComponentTableEntries;
375 KdComponentTable[i].ComponentId = ComponentId;
376 KdComponentTable[i].Level = Kd_DEFAULT_MASK;
377 }
378
379 /* Update entry table */
380 if (State)
381 KdComponentTable[i].Level |= Level;
382 else
383 KdComponentTable[i].Level &= ~Level;
384
385 return STATUS_SUCCESS;
386 }
387
388 /*
389 * @unimplemented
390 */
391 NTSTATUS
392 NTAPI
393 KdSystemDebugControl(IN SYSDBG_COMMAND Command,
394 IN PVOID InputBuffer,
395 IN ULONG InputBufferLength,
396 OUT PVOID OutputBuffer,
397 IN ULONG OutputBufferLength,
398 IN OUT PULONG ReturnLength,
399 IN KPROCESSOR_MODE PreviousMode)
400 {
401 /* HACK */
402 return KdpServiceDispatcher(Command, InputBuffer, InputBufferLength);
403 }
404
405 PKDEBUG_ROUTINE KiDebugRoutine = KdpEnterDebuggerException;
406
407 /* EOF */