1 /* $Id: kdebug.c,v 1.16 2000/10/22 16:36:50 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/kd/kdebug.c
6 * PURPOSE: Kernel debugger
7 * PROGRAMMER: Eric Kohl (ekohl@abo.rhein-zeitung.de)
12 #include <ddk/ntddk.h>
13 #include <internal/ntoskrnl.h>
14 #include <internal/kd.h>
17 /* serial debug connection */
18 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
19 #define DEFAULT_DEBUG_BAUD_RATE 19200 /* 19200 Baud */
21 /* bochs debug output */
22 #define BOCHS_LOGGER_PORT (0xe9)
25 /* TYPEDEFS ****************************************************************/
27 #define ScreenDebug (0x1)
28 #define SerialDebug (0x2)
29 #define BochsDebug (0x4)
31 /* VARIABLES ***************************************************************/
35 KdDebuggerEnabled
= FALSE
; /* EXPORTED */
39 KdDebuggerNotPresent
= TRUE
; /* EXPORTED */
42 static BOOLEAN KdpBreakPending
= FALSE
;
43 static BOOLEAN KdpBreakRecieved
= FALSE
;
44 static ULONG KdpDebugType
= ScreenDebug
| BochsDebug
;
47 /* PRIVATE FUNCTIONS ********************************************************/
50 PrintString (char* fmt
,...)
56 vsprintf(buffer
, fmt
, ap
);
59 HalDisplayString (buffer
);
66 PLOADER_PARAMETER_BLOCK LoaderBlock
69 KD_PORT_INFORMATION PortInfo
;
73 /* set debug port default values */
74 PortInfo
.ComPort
= DEFAULT_DEBUG_PORT
;
75 PortInfo
.BaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
78 * parse kernel command line
81 /* check for 'DEBUGPORT' */
82 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
83 while (p1
&& (p2
= strchr (p1
, '/')))
86 if (!_strnicmp (p2
, "DEBUGPORT", 9))
92 if (!_strnicmp (p2
, "SCREEN", 6))
95 KdDebuggerEnabled
= TRUE
;
96 KdpDebugType
|= ScreenDebug
;
98 else if (!_strnicmp (p2
, "BOCHS", 5))
101 KdDebuggerEnabled
= TRUE
;
102 KdpDebugType
|= BochsDebug
;
104 else if (!_strnicmp (p2
, "COM", 3))
107 Value
= (ULONG
)atol (p2
);
108 if (Value
> 0 && Value
< 5)
110 KdDebuggerEnabled
= TRUE
;
111 KdpDebugType
|= SerialDebug
;
112 PortInfo
.ComPort
= Value
;
120 /* check for 'BAUDRATE' */
121 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
122 while (p1
&& (p2
= strchr (p1
, '/')))
125 if (!_strnicmp (p2
, "BAUDRATE", 8))
131 Value
= (ULONG
)atol (p2
);
134 KdDebuggerEnabled
= TRUE
;
135 KdpDebugType
= KdpDebugType
| SerialDebug
;
136 PortInfo
.BaudRate
= Value
;
143 /* Check for 'DEBUG'. Dont' accept 'DEBUGPORT'!*/
144 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
145 while (p1
&& (p2
= strchr (p1
, '/')))
148 if (!_strnicmp (p2
, "DEBUG", 5) &&
149 _strnicmp (p2
, "DEBUGPORT", 9))
152 KdDebuggerEnabled
= TRUE
;
153 KdpDebugType
= KdpDebugType
| SerialDebug
;
159 /* Check for 'NODEBUG' */
160 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
161 while (p1
&& (p2
= strchr (p1
, '/')))
164 if (!_strnicmp (p2
, "NODEBUG", 7))
167 KdDebuggerEnabled
= FALSE
;
173 /* Check for 'CRASHDEBUG' */
174 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
175 while (p1
&& (p2
= strchr (p1
, '/')))
178 if (!_strnicmp (p2
, "CRASHDEBUG", 10))
181 KdDebuggerEnabled
= FALSE
;
187 /* Check for 'BREAK' */
188 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
189 while (p1
&& (p2
= strchr (p1
, '/')))
192 if (!_strnicmp (p2
, "BREAK", 5))
195 KdpBreakPending
= TRUE
;
202 /* print some information */
203 if (KdDebuggerEnabled
== TRUE
)
205 if (KdpDebugType
& ScreenDebug
)
207 PrintString ("\n Screen debugging enabled\n\n");
209 if (KdpDebugType
& BochsDebug
)
211 PrintString ("\n Bochs debugging enabled\n\n");
213 if (KdpDebugType
& SerialDebug
)
215 PrintString ("\n Serial debugging enabled: COM%ld %ld Baud\n\n",
216 PortInfo
.ComPort
, PortInfo
.BaudRate
);
220 PrintString ("\n Debugging disabled\n\n");
223 /* initialize debug port */
224 if (KdDebuggerEnabled
&& KdpDebugType
== SerialDebug
)
226 KdPortInitialize (&PortInfo
,
233 ULONG
KdpPrintString (PANSI_STRING String
)
235 PCH pch
= String
->Buffer
;
237 if (KdpDebugType
& ScreenDebug
)
239 HalDisplayString (String
->Buffer
);
241 if (KdpDebugType
& SerialDebug
)
247 KdPortPutByte ('\r');
249 KdPortPutByte (*pch
);
253 if (KdpDebugType
& BochsDebug
)
259 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, '\r');
261 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, *pch
);
266 return (ULONG
)String
->Length
;
269 /* PUBLIC FUNCTIONS *********************************************************/
271 /* NTOSKRNL.KdPollBreakIn */
279 BOOLEAN Result
= FALSE
;
282 if (KdDebuggerEnabled
== FALSE
|| KdpDebugType
!= SerialDebug
)
285 // Flags = KiDisableInterrupts();
287 HalDisplayString ("Waiting for kernel debugger connection...\n");
289 if (KdPortPollByte (&ByteRead
))
291 if (ByteRead
== 0x62)
293 if (KdpBreakPending
== TRUE
)
295 KdpBreakPending
= FALSE
;
296 KdpBreakRecieved
= TRUE
;
299 HalDisplayString (" Kernel debugger connected\n");
303 HalDisplayString (" Kernel debugger connection failed\n");
307 // KiRestoreInterrupts (Flags);
313 KeEnterKernelDebugger (VOID
)
315 HalDisplayString ("\n\n *** Entered kernel debugger ***\n");