1 /* $Id: kdebug.c,v 1.12 2000/07/01 18:25:00 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 ****************************************************************/
29 #define ScreenDebug (0x1)
30 #define SerialDebug (0x2)
31 #define BochsDebug (0x4)
33 /* VARIABLES ***************************************************************/
37 KdDebuggerEnabled
= FALSE
; /* EXPORTED */
41 KdDebuggerNotPresent
= TRUE
; /* EXPORTED */
44 static BOOLEAN KdpBreakPending
= FALSE
;
45 static BOOLEAN KdpBreakRecieved
= FALSE
;
46 static ULONG KdpDebugType
= BochsDebug
;
49 /* PRIVATE FUNCTIONS ********************************************************/
52 PrintString (char* fmt
,...)
58 vsprintf(buffer
, fmt
, ap
);
61 HalDisplayString (buffer
);
68 PLOADER_PARAMETER_BLOCK LoaderBlock
71 KD_PORT_INFORMATION PortInfo
;
75 /* set debug port default values */
76 PortInfo
.ComPort
= DEFAULT_DEBUG_PORT
;
77 PortInfo
.BaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
80 * parse kernel command line
83 /* check for 'DEBUGPORT' */
84 p1
= LoaderBlock
->kernel_parameters
;
85 while (p1
&& (p2
= strchr (p1
, '/')))
88 if (!_strnicmp (p2
, "DEBUGPORT", 9))
94 if (!_strnicmp (p2
, "SCREEN", 6))
97 KdDebuggerEnabled
= TRUE
;
98 KdpDebugType
|= ScreenDebug
;
100 else if (!_strnicmp (p2
, "BOCHS", 5))
103 KdDebuggerEnabled
= TRUE
;
104 KdpDebugType
|= BochsDebug
;
106 else if (!_strnicmp (p2
, "COM", 3))
109 Value
= (ULONG
)atol (p2
);
110 if (Value
> 0 && Value
< 5)
112 KdDebuggerEnabled
= TRUE
;
113 KdpDebugType
|= SerialDebug
;
114 PortInfo
.ComPort
= Value
;
122 /* check for 'BAUDRATE' */
123 p1
= LoaderBlock
->kernel_parameters
;
124 while (p1
&& (p2
= strchr (p1
, '/')))
127 if (!_strnicmp (p2
, "BAUDRATE", 8))
133 Value
= (ULONG
)atol (p2
);
136 KdDebuggerEnabled
= TRUE
;
137 KdpDebugType
= KdpDebugType
| SerialDebug
;
138 PortInfo
.BaudRate
= Value
;
145 /* Check for 'DEBUG'. Dont' accept 'DEBUGPORT'!*/
146 p1
= LoaderBlock
->kernel_parameters
;
147 while (p1
&& (p2
= strchr (p1
, '/')))
150 if (!_strnicmp (p2
, "DEBUG", 5) &&
151 _strnicmp (p2
, "DEBUGPORT", 9))
154 KdDebuggerEnabled
= TRUE
;
155 KdpDebugType
= KdpDebugType
| SerialDebug
;
161 /* Check for 'NODEBUG' */
162 p1
= LoaderBlock
->kernel_parameters
;
163 while (p1
&& (p2
= strchr (p1
, '/')))
166 if (!_strnicmp (p2
, "NODEBUG", 7))
169 KdDebuggerEnabled
= FALSE
;
175 /* Check for 'CRASHDEBUG' */
176 p1
= LoaderBlock
->kernel_parameters
;
177 while (p1
&& (p2
= strchr (p1
, '/')))
180 if (!_strnicmp (p2
, "CRASHDEBUG", 10))
183 KdDebuggerEnabled
= FALSE
;
189 /* Check for 'BREAK' */
190 p1
= LoaderBlock
->kernel_parameters
;
191 while (p1
&& (p2
= strchr (p1
, '/')))
194 if (!_strnicmp (p2
, "BREAK", 5))
197 KdpBreakPending
= TRUE
;
204 /* print some information */
205 if (KdDebuggerEnabled
== TRUE
)
207 if (KdpDebugType
& ScreenDebug
)
209 PrintString ("\n Screen debugging enabled\n\n");
211 if (KdpDebugType
& BochsDebug
)
213 PrintString ("\n Bochs debugging enabled\n\n");
215 if (KdpDebugType
& SerialDebug
)
217 PrintString ("\n Serial debugging enabled: COM%ld %ld Baud\n\n",
218 PortInfo
.ComPort
, PortInfo
.BaudRate
);
222 PrintString ("\n Debugging disabled\n\n");
225 /* initialize debug port */
226 if (KdDebuggerEnabled
&& KdpDebugType
== SerialDebug
)
228 KdPortInitialize (&PortInfo
,
235 ULONG
KdpPrintString (PANSI_STRING String
)
237 PCH pch
= String
->Buffer
;
239 if (KdpDebugType
& ScreenDebug
)
241 HalDisplayString (String
->Buffer
);
243 if (KdpDebugType
& SerialDebug
)
249 KdPortPutByte ('\r');
251 KdPortPutByte (*pch
);
255 if (KdpDebugType
& BochsDebug
)
261 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, '\r');
263 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, *pch
);
268 return (ULONG
)String
->Length
;
271 /* PUBLIC FUNCTIONS *********************************************************/
273 /* NTOSKRNL.KdPollBreakIn */
281 BOOLEAN Result
= FALSE
;
284 if (KdDebuggerEnabled
== FALSE
|| KdpDebugType
!= SerialDebug
)
287 // Flags = KiDisableInterrupts();
289 HalDisplayString ("Waiting for kernel debugger connection...\n");
291 if (KdPortPollByte (&ByteRead
))
293 if (ByteRead
== 0x62)
295 if (KdpBreakPending
== TRUE
)
297 KdpBreakPending
= FALSE
;
298 KdpBreakRecieved
= TRUE
;
301 HalDisplayString (" Kernel debugger connected\n");
305 HalDisplayString (" Kernel debugger connection failed\n");
309 // KiRestoreInterrupts (Flags);
316 KeEnterKernelDebugger (
320 HalDisplayString ("\n\n *** Entered kernel debugger ***\n");