1 /* $Id: kdebug.c,v 1.32 2002/02/02 20:13:08 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>
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_COM1_IRQ 4 /* COM1 IRQ */
22 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
23 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
25 /* bochs debug output */
26 #define BOCHS_LOGGER_PORT (0xe9)
29 /* TYPEDEFS ****************************************************************/
40 /* VARIABLES ***************************************************************/
44 KdDebuggerEnabled
= FALSE
; /* EXPORTED */
48 KdDebuggerNotPresent
= TRUE
; /* EXPORTED */
51 static BOOLEAN KdpBreakPending
= FALSE
;
52 static BOOLEAN KdpLogOnly
= TRUE
;
53 static DEBUG_TYPE KdpDebugType
= NoDebug
;
56 /* PRIVATE FUNCTIONS ********************************************************/
59 PrintString(char* fmt
,...)
65 vsprintf(buffer
, fmt
, ap
);
68 HalDisplayString(buffer
);
73 KdInitSystem(ULONG Reserved
,
74 PLOADER_PARAMETER_BLOCK LoaderBlock
)
76 KD_PORT_INFORMATION PortInfo
;
81 /* Initialize runtime debugging if available */
85 /* set debug port default values */
86 PortInfo
.ComPort
= DEFAULT_DEBUG_PORT
;
87 PortInfo
.BaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
88 KdpPortIrq
= DEFAULT_DEBUG_COM2_IRQ
;
90 /* parse kernel command line */
92 /* check for 'DEBUGPORT' */
93 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
94 while (p1
&& (p2
= strchr(p1
, '/')))
97 if (!_strnicmp(p2
, "DEBUGPORT", 9))
103 if (!_strnicmp(p2
, "SCREEN", 6))
106 KdDebuggerEnabled
= TRUE
;
107 KdpDebugType
= ScreenDebug
;
109 else if (!_strnicmp(p2
, "BOCHS", 5))
112 KdDebuggerEnabled
= TRUE
;
113 KdpDebugType
= BochsDebug
;
115 else if (!_strnicmp(p2
, "COM", 3))
118 Value
= (ULONG
)atol(p2
);
119 if (Value
> 0 && Value
< 5)
121 KdDebuggerEnabled
= TRUE
;
122 KdpDebugType
= SerialDebug
;
123 PortInfo
.ComPort
= Value
;
126 else if (!_strnicmp(p2
, "FILE", 4))
129 KdDebuggerEnabled
= TRUE
;
130 KdpDebugType
= FileLogDebug
;
134 else if (!_strnicmp(p2
, "DEBUG", 5))
137 KdDebuggerEnabled
= TRUE
;
138 KdpDebugType
= SerialDebug
;
140 else if (!_strnicmp(p2
, "NODEBUG", 7))
143 KdDebuggerEnabled
= FALSE
;
144 KdpDebugType
= NoDebug
;
146 else if (!_strnicmp(p2
, "CRASHDEBUG", 10))
149 KdDebuggerEnabled
= FALSE
;
150 KdpDebugType
= NoDebug
;
152 else if (!_strnicmp(p2
, "BREAK", 5))
155 KdpBreakPending
= TRUE
;
157 else if (!_strnicmp(p2
, "BAUDRATE", 8))
163 Value
= (ULONG
)atol(p2
);
166 KdDebuggerEnabled
= TRUE
;
167 KdpDebugType
= SerialDebug
;
168 PortInfo
.BaudRate
= Value
;
171 else if (!_strnicmp(p2
, "IRQ", 3))
177 Value
= (ULONG
)atol(p2
);
180 KdDebuggerEnabled
= TRUE
;
181 KdpDebugType
= SerialDebug
;
187 else if (!_strnicmp(p2
, "GDB", 3))
195 /* print some information */
196 if (KdDebuggerEnabled
== TRUE
)
198 switch (KdpDebugType
)
204 PrintString("\n Screen debugging enabled\n\n");
208 PrintString("\n Bochs debugging enabled\n\n");
212 PrintString("\n Serial debugging enabled: COM%ld %ld Baud\n\n",
213 PortInfo
.ComPort
, PortInfo
.BaudRate
);
217 PrintString("\n File log debugging enabled\n\n");
222 /* initialize debug port */
223 if (KdDebuggerEnabled
== TRUE
)
225 switch (KdpDebugType
)
228 KdPortInitialize(&PortInfo
,
247 /* Initialize kernel debugger */
248 if (KdDebuggerEnabled
== TRUE
&&
249 KdpDebugType
== SerialDebug
&&
259 if (KdDebuggerEnabled
== TRUE
&&
260 KdpDebugType
== SerialDebug
&&
268 KdDebugPrint (LPSTR Message
)
270 PCHAR pch
= (PCHAR
) Message
;
276 KdPortPutByte ('\r');
278 KdPortPutByte (*pch
);
284 KdpPrintString(PANSI_STRING String
)
286 PCH pch
= String
->Buffer
;
288 switch (KdpDebugType
)
294 HalDisplayString(pch
);
298 if (KdpLogOnly
== TRUE
)
301 KdGdbDebugPrint(pch
);
309 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, '\r');
311 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, *pch
);
321 return((ULONG
)String
->Length
);
324 /* PUBLIC FUNCTIONS *********************************************************/
326 /* NTOSKRNL.KdPollBreakIn */
334 if (!KdDebuggerEnabled
|| KdpDebugType
!= SerialDebug
)
341 KeEnterKernelDebugger(VOID
)
343 HalDisplayString("\n\n *** Entered kernel debugger ***\n");
350 KdSystemDebugControl(ULONG Code
)
352 extern VOID
PsDumpThreads(BOOLEAN IncludeSystem
);
354 /* A - Dump the entire contents of the non-paged pool. */
357 MiDebugDumpNonPagedPool(FALSE
);
359 /* B - Bug check the system. */
365 * C - Dump statistics about the distribution of tagged blocks in
366 * the non-paged pool.
370 MiDebugDumpNonPagedPoolStats(FALSE
);
373 * D - Dump the blocks created in the non-paged pool since the last
374 * SysRq + D and SysRq + E command.
378 MiDebugDumpNonPagedPool(TRUE
);
380 /* E - Dump statistics about the tags of newly created blocks. */
383 MiDebugDumpNonPagedPoolStats(TRUE
);
393 PsDumpThreads(FALSE
);
407 /* K - Enter the system debugger. */
413 DbgPrint("No local kernel debugger\n");
414 #endif /* not KDBG */
419 /* Support routines for the GDB stubs */
422 KdPutChar(UCHAR Value
)
424 KdPortPutByte (Value
);
433 while (!KdPortGetByte (&Value
));