1 /* $Id: kdebug.c,v 1.30 2001/12/31 19:06:47 dwelch 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>
15 #include <internal/mm.h>
17 #include "../dbg/kdb.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)
32 #define FileLogDebug (0x8)
34 /* VARIABLES ***************************************************************/
38 KdDebuggerEnabled
= FALSE
; /* EXPORTED */
42 KdDebuggerNotPresent
= TRUE
; /* EXPORTED */
45 static BOOLEAN KdpBreakPending
= FALSE
;
46 static BOOLEAN KdpBreakRecieved
= FALSE
;
47 static ULONG KdpDebugType
= ScreenDebug
| 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
;
76 /* Initialize runtime debugging if available */
81 /* set debug port default values */
82 PortInfo
.ComPort
= DEFAULT_DEBUG_PORT
;
83 PortInfo
.BaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
86 * parse kernel command line
89 /* check for 'DEBUGPORT' */
90 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
91 while (p1
&& (p2
= strchr (p1
, '/')))
94 if (!_strnicmp (p2
, "DEBUGPORT", 9))
100 if (!_strnicmp (p2
, "SCREEN", 6))
103 KdDebuggerEnabled
= TRUE
;
104 KdpDebugType
|= ScreenDebug
;
106 else if (!_strnicmp (p2
, "BOCHS", 5))
109 KdDebuggerEnabled
= TRUE
;
110 KdpDebugType
|= BochsDebug
;
112 else if (!_strnicmp (p2
, "COM", 3))
115 Value
= (ULONG
)atol (p2
);
116 if (Value
> 0 && Value
< 5)
118 KdDebuggerEnabled
= TRUE
;
119 KdpDebugType
|= SerialDebug
;
120 PortInfo
.ComPort
= Value
;
125 else if (!_strnicmp (p2
, "DEBUG", 5))
128 KdDebuggerEnabled
= TRUE
;
129 KdpDebugType
= KdpDebugType
| SerialDebug
;
131 else if (!_strnicmp (p2
, "NODEBUG", 7))
134 KdDebuggerEnabled
= FALSE
;
136 else if (!_strnicmp (p2
, "CRASHDEBUG", 10))
139 KdDebuggerEnabled
= FALSE
;
141 else if (!_strnicmp (p2
, "BREAK", 5))
144 KdpBreakPending
= TRUE
;
146 else if (!_strnicmp (p2
, "BAUDRATE", 8))
152 Value
= (ULONG
)atol (p2
);
155 KdDebuggerEnabled
= TRUE
;
156 KdpDebugType
= KdpDebugType
| SerialDebug
;
157 PortInfo
.BaudRate
= Value
;
164 #ifdef DBGPRINT_FILE_LOG
165 KdpDebugType
|= FileLogDebug
;
167 #endif /* DBGPRINT_FILE_LOG */
169 /* print some information */
170 if (KdDebuggerEnabled
== TRUE
)
172 if (KdpDebugType
& ScreenDebug
)
174 PrintString ("\n Screen debugging enabled\n\n");
176 if (KdpDebugType
& BochsDebug
)
178 PrintString ("\n Bochs debugging enabled\n\n");
180 if (KdpDebugType
& SerialDebug
)
182 PrintString ("\n Serial debugging enabled: COM%ld %ld Baud\n\n",
183 PortInfo
.ComPort
, PortInfo
.BaudRate
);
185 if (KdpDebugType
& FileLogDebug
)
187 PrintString("\n File log debugging enabled\n\n");
191 PrintString ("\n Debugging disabled\n\n");
194 /* initialize debug port */
195 if (KdDebuggerEnabled
&& (KdpDebugType
& SerialDebug
))
197 KdPortInitialize (&PortInfo
,
204 ULONG
KdpPrintString (PANSI_STRING String
)
206 PCH pch
= String
->Buffer
;
208 if (KdpDebugType
& ScreenDebug
)
210 HalDisplayString (String
->Buffer
);
212 if (KdpDebugType
& SerialDebug
)
218 KdPortPutByte ('\r');
220 KdPortPutByte (*pch
);
224 if (KdpDebugType
& BochsDebug
)
226 pch
= String
->Buffer
;
231 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, '\r');
233 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, *pch
);
237 #ifdef DBGPRINT_FILE_LOG
238 if (KdpDebugType
& FileLogDebug
)
240 DebugLogWrite(String
->Buffer
);
242 #endif /* DBGPRINT_FILE_LOG */
243 return (ULONG
)String
->Length
;
246 /* PUBLIC FUNCTIONS *********************************************************/
248 /* NTOSKRNL.KdPollBreakIn */
256 BOOLEAN Result
= FALSE
;
259 if (KdDebuggerEnabled
== FALSE
|| KdpDebugType
!= SerialDebug
)
262 // Flags = KiDisableInterrupts();
264 HalDisplayString ("Waiting for kernel debugger connection...\n");
266 if (KdPortPollByte (&ByteRead
))
268 if (ByteRead
== 0x62)
270 if (KdpBreakPending
== TRUE
)
272 KdpBreakPending
= FALSE
;
273 KdpBreakRecieved
= TRUE
;
276 HalDisplayString (" Kernel debugger connected\n");
280 HalDisplayString (" Kernel debugger connection failed\n");
284 // KiRestoreInterrupts (Flags);
290 KeEnterKernelDebugger (VOID
)
292 HalDisplayString ("\n\n *** Entered kernel debugger ***\n");
303 KdSystemDebugControl(ULONG Code
)
305 extern VOID
PsDumpThreads(BOOLEAN IncludeSystem
);
307 /* A - Dump the entire contents of the non-paged pool. */
310 MiDebugDumpNonPagedPool(FALSE
);
312 /* B - Bug check the system. */
318 * C - Dump statistics about the distribution of tagged blocks in
319 * the non-paged pool.
323 MiDebugDumpNonPagedPoolStats(FALSE
);
326 * D - Dump the blocks created in the non-paged pool since the last
327 * SysRq + D and SysRq + E command.
331 MiDebugDumpNonPagedPool(TRUE
);
333 /* E - Dump statistics about the tags of newly created blocks. */
336 MiDebugDumpNonPagedPoolStats(TRUE
);
346 PsDumpThreads(FALSE
);
360 /* K - Enter the system debugger. */
366 DbgPrint("No local kernel debugger\n");
367 #endif /* not KDBG */