Merge 15329:15546 from trunk
[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 EXPORTED KdDebuggerEnabled = FALSE;
17 BOOLEAN EXPORTED KdEnteredDebugger = FALSE;
18 BOOLEAN EXPORTED KdDebuggerNotPresent = TRUE;
19 BOOLEAN EXPORTED KiEnableTimerWatchdog = FALSE;
20 ULONG EXPORTED KiBugCheckData;
21 BOOLEAN KdpBreakPending;
22 VOID STDCALL PspDumpThreads(BOOLEAN SystemThreads);
23
24 /* PRIVATE FUNCTIONS *********************************************************/
25
26 ULONG
27 STDCALL
28 KdpServiceDispatcher(ULONG Service,
29 PVOID Context1,
30 PVOID Context2)
31 {
32 ULONG Result = 0;
33
34 switch (Service)
35 {
36 case 1: /* DbgPrint */
37 Result = KdpPrintString ((PANSI_STRING)Context1);
38 break;
39
40 #ifdef DBG
41 case TAG('R', 'o', 's', ' '): /* ROS-INTERNAL */
42 {
43 switch ((ULONG)Context1)
44 {
45 case DumpNonPagedPool:
46 MiDebugDumpNonPagedPool(FALSE);
47 break;
48
49 case ManualBugCheck:
50 KEBUGCHECK(MANUALLY_INITIATED_CRASH);
51 break;
52
53 case DumpNonPagedPoolStats:
54 MiDebugDumpNonPagedPoolStats(FALSE);
55 break;
56
57 case DumpNewNonPagedPool:
58 MiDebugDumpNonPagedPool(TRUE);
59 break;
60
61 case DumpNewNonPagedPoolStats:
62 MiDebugDumpNonPagedPoolStats(TRUE);
63 break;
64
65 case DumpAllThreads:
66 PspDumpThreads(TRUE);
67 break;
68
69 case DumpUserThreads:
70 PspDumpThreads(FALSE);
71 break;
72
73 case EnterDebugger:
74 DbgBreakPoint();
75 break;
76
77 default:
78 break;
79 }
80 }
81 #endif
82 default:
83 HalDisplayString ("Invalid debug service call!\n");
84 break;
85 }
86
87 return Result;
88 }
89
90 KD_CONTINUE_TYPE
91 STDCALL
92 KdpEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord,
93 KPROCESSOR_MODE PreviousMode,
94 PCONTEXT Context,
95 PKTRAP_FRAME TrapFrame,
96 BOOLEAN FirstChance,
97 BOOLEAN Gdb)
98 {
99 /* Get out of here if the Debugger isn't enabled */
100 if (!KdDebuggerEnabled) return kdHandleException;
101
102 /* FIXME:
103 * Right now, the GDB wrapper seems to handle exceptions differntly
104 * from KDGB and both are called at different times, while the GDB
105 * one is only called once and that's it. I don't really have the knowledge
106 * to fix the GDB stub, so until then, we'll be using this hack
107 */
108 if (Gdb)
109 {
110 /* Call the registered wrapper */
111 if (WrapperInitRoutine) return WrapperTable.
112 KdpExceptionRoutine(ExceptionRecord,
113 Context,
114 TrapFrame);
115 }
116
117 /* Call KDBG if available */
118 return KdbEnterDebuggerException(ExceptionRecord,
119 PreviousMode,
120 Context,
121 TrapFrame,
122 FirstChance);
123 }
124
125 /* PUBLIC FUNCTIONS *********************************************************/
126
127 /*
128 * @implemented
129 */
130 VOID
131 STDCALL
132 KdDisableDebugger(VOID)
133 {
134 KIRQL OldIrql;
135
136 /* Raise IRQL */
137 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
138
139 /* TODO: Disable any breakpoints */
140
141 /* Disable the Debugger */
142 KdDebuggerEnabled = FALSE;
143
144 /* Lower the IRQL */
145 KeLowerIrql(OldIrql);
146 }
147
148 /*
149 * @implemented
150 */
151 VOID
152 STDCALL
153 KdEnableDebugger(VOID)
154 {
155 KIRQL OldIrql;
156
157 /* Raise IRQL */
158 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
159
160 /* TODO: Re-enable any breakpoints */
161
162 /* Enable the Debugger */
163 KdDebuggerEnabled = TRUE;
164
165 /* Lower the IRQL */
166 KeLowerIrql(OldIrql);
167 }
168
169 /*
170 * @implemented
171 */
172 BOOLEAN
173 STDCALL
174 KdPollBreakIn(VOID)
175 {
176 return KdpBreakPending;
177 }
178
179 /*
180 * @implemented
181 */
182 VOID
183 STDCALL
184 KeEnterKernelDebugger(VOID)
185 {
186 HalDisplayString("\n\n *** Entered kernel debugger ***\n");
187
188 /* Set the Variable */
189 KdEnteredDebugger = TRUE;
190
191 /* Halt the CPU */
192 for (;;) Ke386HaltProcessor();
193 }
194
195 /*
196 * @unimplemented
197 */
198 NTSTATUS
199 STDCALL
200 KdPowerTransition(ULONG PowerState)
201 {
202 UNIMPLEMENTED;
203 return STATUS_NOT_IMPLEMENTED;
204 }
205
206 /* EOF */