1 /* $Id: kdebug.c,v 1.8 2000/03/04 22:02:13 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>
19 /* serial debug connection */
20 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
21 #define DEFAULT_DEBUG_BAUD_RATE 19200 /* 19200 Baud */
23 /* bochs debug output */
24 #define BOCHS_LOGGER_PORT (0xe9)
27 /* TYPEDEFS ****************************************************************/
37 /* VARIABLES ***************************************************************/
41 KdDebuggerEnabled
= FALSE
; /* EXPORTED */
45 KdDebuggerNotPresent
= TRUE
; /* EXPORTED */
48 static BOOLEAN KdpBreakPending
= FALSE
;
49 static BOOLEAN KdpBreakRecieved
= FALSE
;
50 static DEBUGTYPE KdpDebugType
= ScreenDebug
;
53 /* PRIVATE FUNCTIONS ********************************************************/
56 PrintString (char* fmt
,...)
62 vsprintf(buffer
, fmt
, ap
);
65 HalDisplayString (buffer
);
75 KD_PORT_INFORMATION PortInfo
;
79 /* set debug port default values */
80 PortInfo
.ComPort
= DEFAULT_DEBUG_PORT
;
81 PortInfo
.BaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
84 * parse kernel command line
87 /* check for 'DEBUGPORT' */
88 p1
= BootParam
->kernel_parameters
;
89 while (p1
&& (p2
= strchr (p1
, '/')))
92 if (!_strnicmp (p2
, "DEBUGPORT", 9))
98 if (!_strnicmp (p2
, "SCREEN", 6))
101 KdDebuggerEnabled
= TRUE
;
102 KdpDebugType
= ScreenDebug
;
104 else if (!_strnicmp (p2
, "BOCHS", 5))
107 KdDebuggerEnabled
= TRUE
;
108 KdpDebugType
= BochsDebug
;
110 else if (!_strnicmp (p2
, "COM", 3))
113 Value
= (ULONG
)atol (p2
);
114 if (Value
> 0 && Value
< 5)
116 KdDebuggerEnabled
= TRUE
;
117 KdpDebugType
= SerialDebug
;
118 PortInfo
.ComPort
= Value
;
126 /* check for 'BAUDRATE' */
127 p1
= BootParam
->kernel_parameters
;
128 while (p1
&& (p2
= strchr (p1
, '/')))
131 if (!_strnicmp (p2
, "BAUDRATE", 8))
137 Value
= (ULONG
)atol (p2
);
140 KdDebuggerEnabled
= TRUE
;
141 KdpDebugType
= SerialDebug
;
142 PortInfo
.BaudRate
= Value
;
149 /* Check for 'DEBUG'. Dont' accept 'DEBUGPORT'!*/
150 p1
= BootParam
->kernel_parameters
;
151 while (p1
&& (p2
= strchr (p1
, '/')))
154 if (!_strnicmp (p2
, "DEBUG", 5) &&
155 _strnicmp (p2
, "DEBUGPORT", 9))
158 KdDebuggerEnabled
= TRUE
;
159 KdpDebugType
= SerialDebug
;
165 /* Check for 'NODEBUG' */
166 p1
= BootParam
->kernel_parameters
;
167 while (p1
&& (p2
= strchr (p1
, '/')))
170 if (!_strnicmp (p2
, "NODEBUG", 7))
173 KdDebuggerEnabled
= FALSE
;
179 /* Check for 'CRASHDEBUG' */
180 p1
= BootParam
->kernel_parameters
;
181 while (p1
&& (p2
= strchr (p1
, '/')))
184 if (!_strnicmp (p2
, "CRASHDEBUG", 10))
187 KdDebuggerEnabled
= FALSE
;
193 /* Check for 'BREAK' */
194 p1
= BootParam
->kernel_parameters
;
195 while (p1
&& (p2
= strchr (p1
, '/')))
198 if (!_strnicmp (p2
, "BREAK", 5))
201 KdpBreakPending
= TRUE
;
208 /* print some information */
209 if (KdDebuggerEnabled
== TRUE
)
211 if (KdpDebugType
== ScreenDebug
)
213 PrintString ("\n Screen debugging enabled\n\n");
215 else if (KdpDebugType
== BochsDebug
)
217 PrintString ("\n Bochs debugging enabled\n\n");
219 else if (KdpDebugType
== SerialDebug
)
221 PrintString ("\n Serial debugging enabled: COM%ld %ld Baud\n\n",
222 PortInfo
.ComPort
, PortInfo
.BaudRate
);
226 PrintString ("\n Debugging disabled\n\n");
229 /* initialize debug port */
230 if (KdDebuggerEnabled
&& KdpDebugType
== SerialDebug
)
232 KdPortInitialize (&PortInfo
,
240 KdpPrintString (PANSI_STRING String
)
242 PCH pch
= String
->Buffer
;
244 if (KdpDebugType
== ScreenDebug
)
246 HalDisplayString (String
->Buffer
);
248 else if (KdpDebugType
== SerialDebug
)
254 KdPortPutByte ('\r');
256 KdPortPutByte (*pch
);
260 else if (KdpDebugType
== BochsDebug
)
266 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, '\r');
268 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, *pch
);
273 return (ULONG
)String
->Length
;
276 /* PUBLIC FUNCTIONS *********************************************************/
278 /* NTOSKRNL.KdPollBreakIn */
286 BOOLEAN Result
= FALSE
;
289 if (KdDebuggerEnabled
== FALSE
|| KdpDebugType
!= SerialDebug
)
292 // Flags = KiDisableInterrupts();
294 HalDisplayString ("Waiting for kernel debugger connection...\n");
296 if (KdPortPollByte (&ByteRead
))
298 if (ByteRead
== 0x62)
300 if (KdpBreakPending
== TRUE
)
302 KdpBreakPending
= FALSE
;
303 KdpBreakRecieved
= TRUE
;
306 HalDisplayString (" Kernel debugger connected\n");
310 HalDisplayString (" Kernel debugger connection failed\n");
314 // KiRestoreInterrupts (Flags);
321 KeEnterKernelDebugger (
325 HalDisplayString ("\n\n *** Entered kernel debugger ***\n");