1 /* $Id: kdebug.c,v 1.34 2002/02/09 18:41:24 chorns 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)
28 /* VARIABLES ***************************************************************/
32 KdDebuggerEnabled
= FALSE
; /* EXPORTED */
36 KdDebuggerNotPresent
= TRUE
; /* EXPORTED */
39 static BOOLEAN KdpBreakPending
= FALSE
;
40 DEBUG_TYPE KdDebugType
= NoDebug
;
43 /* PRIVATE FUNCTIONS ********************************************************/
46 PrintString(char* fmt
,...)
52 vsprintf(buffer
, fmt
, ap
);
55 HalDisplayString(buffer
);
60 KdInitSystem(ULONG Reserved
,
61 PLOADER_PARAMETER_BLOCK LoaderBlock
)
63 KD_PORT_INFORMATION PortInfo
;
68 /* Initialize runtime debugging if available */
72 /* set debug port default values */
73 PortInfo
.ComPort
= DEFAULT_DEBUG_PORT
;
74 PortInfo
.BaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
75 KdpPortIrq
= DEFAULT_DEBUG_COM2_IRQ
;
77 /* parse kernel command line */
79 /* check for 'DEBUGPORT' */
80 p1
= (PCHAR
)LoaderBlock
->CommandLine
;
81 while (p1
&& (p2
= strchr(p1
, '/')))
84 if (!_strnicmp(p2
, "DEBUGPORT", 9))
90 if (!_strnicmp(p2
, "SCREEN", 6))
93 KdDebuggerEnabled
= TRUE
;
94 KdDebugType
= ScreenDebug
;
96 else if (!_strnicmp(p2
, "BOCHS", 5))
99 KdDebuggerEnabled
= TRUE
;
100 KdDebugType
= BochsDebug
;
102 else if (!_strnicmp(p2
, "GDB", 3))
105 KdDebuggerEnabled
= TRUE
;
106 KdDebugType
= GdbDebug
;
108 else if (!_strnicmp(p2
, "PICE", 4))
111 KdDebuggerEnabled
= TRUE
;
112 KdDebugType
= PiceDebug
;
114 else if (!_strnicmp(p2
, "COM", 3))
117 Value
= (ULONG
)atol(p2
);
118 if (Value
> 0 && Value
< 5)
120 PrintString("\n COM2 found\n\n");
121 KdDebuggerEnabled
= TRUE
;
122 KdDebugType
= SerialDebug
;
123 PortInfo
.ComPort
= Value
;
126 else if (!_strnicmp(p2
, "FILE", 4))
129 KdDebuggerEnabled
= TRUE
;
130 KdDebugType
= FileLogDebug
;
134 else if (!_strnicmp(p2
, "DEBUG", 5))
137 /* Check that KdDebugType equals NoDebug so we don't override any set KdDebugType */
138 if (KdDebugType
== NoDebug
)
140 KdDebuggerEnabled
= TRUE
;
141 KdDebugType
= SerialDebug
;
144 else if (!_strnicmp(p2
, "NODEBUG", 7))
147 KdDebuggerEnabled
= FALSE
;
148 KdDebugType
= NoDebug
;
150 else if (!_strnicmp(p2
, "CRASHDEBUG", 10))
153 KdDebuggerEnabled
= FALSE
;
154 KdDebugType
= NoDebug
;
156 else if (!_strnicmp(p2
, "BREAK", 5))
159 KdpBreakPending
= TRUE
;
161 else if (!_strnicmp(p2
, "BAUDRATE", 8))
167 Value
= (ULONG
)atol(p2
);
170 KdDebuggerEnabled
= TRUE
;
171 KdDebugType
= SerialDebug
;
172 PortInfo
.BaudRate
= Value
;
175 else if (!_strnicmp(p2
, "IRQ", 3))
181 Value
= (ULONG
)atol(p2
);
184 KdDebuggerEnabled
= TRUE
;
185 KdDebugType
= SerialDebug
;
194 /* print some information */
195 if (KdDebuggerEnabled
== TRUE
)
203 PrintString("\n GDB debugging enabled\n\n");
207 PrintString("\n Private ICE debugger enabled\n\n");
211 //PrintString("\n Screen debugging enabled\n\n");
215 PrintString("\n Bochs debugging enabled\n\n");
219 PrintString("\n Serial debugging enabled: COM%ld %ld Baud\n\n",
220 PortInfo
.ComPort
, PortInfo
.BaudRate
);
224 PrintString("\n File log debugging enabled\n\n");
229 /* initialize debug port */
230 if (KdDebuggerEnabled
== TRUE
)
236 KdPortInitialize(&PortInfo
,
255 /* Initialize kernel debugger */
256 if (KdDebuggerEnabled
== TRUE
&&
257 KdDebugType
== GdbDebug
)
266 if (KdDebuggerEnabled
== TRUE
&&
267 KdDebugType
== GdbDebug
)
274 KdDebugPrint (LPSTR Message
)
276 PCHAR pch
= (PCHAR
) Message
;
282 KdPortPutByte ('\r');
284 KdPortPutByte (*pch
);
290 KdpPrintString(PANSI_STRING String
)
292 PCH pch
= String
->Buffer
;
300 KdGdbDebugPrint(pch
);
307 HalDisplayString(pch
);
319 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, '\r');
321 WRITE_PORT_UCHAR((PUCHAR
)BOCHS_LOGGER_PORT
, *pch
);
331 return((ULONG
)String
->Length
);
334 /* PUBLIC FUNCTIONS *********************************************************/
336 /* NTOSKRNL.KdPollBreakIn */
341 if ((!KdDebuggerEnabled
) || (KdDebugType
!= SerialDebug
))
343 return KdpBreakPending
;
347 KeEnterKernelDebugger(VOID
)
349 HalDisplayString("\n\n *** Entered kernel debugger ***\n");
356 KdSystemDebugControl(ULONG Code
)
358 extern VOID
PsDumpThreads(BOOLEAN IncludeSystem
);
360 /* A - Dump the entire contents of the non-paged pool. */
363 MiDebugDumpNonPagedPool(FALSE
);
365 /* B - Bug check the system. */
371 * C - Dump statistics about the distribution of tagged blocks in
372 * the non-paged pool.
376 MiDebugDumpNonPagedPoolStats(FALSE
);
379 * D - Dump the blocks created in the non-paged pool since the last
380 * SysRq + D and SysRq + E command.
384 MiDebugDumpNonPagedPool(TRUE
);
386 /* E - Dump statistics about the tags of newly created blocks. */
389 MiDebugDumpNonPagedPoolStats(TRUE
);
399 PsDumpThreads(FALSE
);
413 /* K - Enter the system debugger. */
419 DbgPrint("No local kernel debugger\n");
420 #endif /* not KDBG */
425 /* Support routines for the GDB stubs */
428 KdPutChar(UCHAR Value
)
430 KdPortPutByte (Value
);
439 while (!KdPortGetByte (&Value
));