3 * Copyright (C) 2005 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/kdbg/kdb_cli.c
22 * PURPOSE: Kernel debugger command line interface
23 * PROGRAMMER: Gregor Anich (blight@blight.eu.org)
29 /* INCLUDES ******************************************************************/
36 /* DEFINES *******************************************************************/
42 #define KEY_SCAN_UP 72
43 #define KEY_SCAN_DOWN 80
45 /* Scan codes of keyboard keys: */
46 #define KEYSC_END 0x004f
47 #define KEYSC_PAGEUP 0x0049
48 #define KEYSC_PAGEDOWN 0x0051
49 #define KEYSC_HOME 0x0047
50 #define KEYSC_ARROWUP 0x0048
52 #define KDB_ENTER_CONDITION_TO_STRING(cond) \
53 ((cond) == KdbDoNotEnter ? "never" : \
54 ((cond) == KdbEnterAlways ? "always" : \
55 ((cond) == KdbEnterFromKmode ? "kmode" : "umode")))
57 #define KDB_ACCESS_TYPE_TO_STRING(type) \
58 ((type) == KdbAccessRead ? "read" : \
59 ((type) == KdbAccessWrite ? "write" : \
60 ((type) == KdbAccessReadWrite ? "rdwr" : "exec")))
62 #define NPX_STATE_TO_STRING(state) \
63 ((state) == NPX_STATE_LOADED ? "Loaded" : \
64 ((state) == NPX_STATE_NOT_LOADED ? "Not loaded" : "Unknown"))
66 /* PROTOTYPES ****************************************************************/
68 static BOOLEAN
KdbpCmdEvalExpression(ULONG Argc
, PCHAR Argv
[]);
69 static BOOLEAN
KdbpCmdDisassembleX(ULONG Argc
, PCHAR Argv
[]);
70 static BOOLEAN
KdbpCmdRegs(ULONG Argc
, PCHAR Argv
[]);
71 static BOOLEAN
KdbpCmdBackTrace(ULONG Argc
, PCHAR Argv
[]);
73 static BOOLEAN
KdbpCmdContinue(ULONG Argc
, PCHAR Argv
[]);
74 static BOOLEAN
KdbpCmdStep(ULONG Argc
, PCHAR Argv
[]);
75 static BOOLEAN
KdbpCmdBreakPointList(ULONG Argc
, PCHAR Argv
[]);
76 static BOOLEAN
KdbpCmdEnableDisableClearBreakPoint(ULONG Argc
, PCHAR Argv
[]);
77 static BOOLEAN
KdbpCmdBreakPoint(ULONG Argc
, PCHAR Argv
[]);
79 static BOOLEAN
KdbpCmdThread(ULONG Argc
, PCHAR Argv
[]);
80 static BOOLEAN
KdbpCmdProc(ULONG Argc
, PCHAR Argv
[]);
82 static BOOLEAN
KdbpCmdMod(ULONG Argc
, PCHAR Argv
[]);
83 static BOOLEAN
KdbpCmdGdtLdtIdt(ULONG Argc
, PCHAR Argv
[]);
84 static BOOLEAN
KdbpCmdPcr(ULONG Argc
, PCHAR Argv
[]);
85 static BOOLEAN
KdbpCmdTss(ULONG Argc
, PCHAR Argv
[]);
87 static BOOLEAN
KdbpCmdBugCheck(ULONG Argc
, PCHAR Argv
[]);
88 static BOOLEAN
KdbpCmdReboot(ULONG Argc
, PCHAR Argv
[]);
89 static BOOLEAN
KdbpCmdFilter(ULONG Argc
, PCHAR Argv
[]);
90 static BOOLEAN
KdbpCmdSet(ULONG Argc
, PCHAR Argv
[]);
91 static BOOLEAN
KdbpCmdHelp(ULONG Argc
, PCHAR Argv
[]);
92 static BOOLEAN
KdbpCmdDmesg(ULONG Argc
, PCHAR Argv
[]);
94 BOOLEAN
ExpKdbgExtPool(ULONG Argc
, PCHAR Argv
[]);
97 static BOOLEAN
KdbpCmdPrintStruct(ULONG Argc
, PCHAR Argv
[]);
100 /* GLOBALS *******************************************************************/
102 static PKDBG_CLI_ROUTINE KdbCliCallbacks
[10];
103 static BOOLEAN KdbUseIntelSyntax
= FALSE
; /* Set to TRUE for intel syntax */
104 static BOOLEAN KdbBreakOnModuleLoad
= FALSE
; /* Set to TRUE to break into KDB when a module is loaded */
106 static CHAR KdbCommandHistoryBuffer
[2048]; /* Command history string ringbuffer */
107 static PCHAR KdbCommandHistory
[sizeof(KdbCommandHistoryBuffer
) / 8] = { NULL
}; /* Command history ringbuffer */
108 static LONG KdbCommandHistoryBufferIndex
= 0;
109 static LONG KdbCommandHistoryIndex
= 0;
111 static ULONG KdbNumberOfRowsPrinted
= 0;
112 static ULONG KdbNumberOfColsPrinted
= 0;
113 static BOOLEAN KdbOutputAborted
= FALSE
;
114 static BOOLEAN KdbRepeatLastCommand
= FALSE
;
115 static LONG KdbNumberOfRowsTerminal
= -1;
116 static LONG KdbNumberOfColsTerminal
= -1;
118 PCHAR KdbInitFileBuffer
= NULL
; /* Buffer where KDBinit file is loaded into during initialization */
119 BOOLEAN KdbpBugCheckRequested
= FALSE
;
122 /* defined in ../kd/kdio.c, declare here: */
123 extern volatile BOOLEAN KdbpIsInDmesgMode
;
124 extern const ULONG KdpDmesgBufferSize
;
125 extern PCHAR KdpDmesgBuffer
;
126 extern volatile ULONG KdpDmesgCurrentPosition
;
127 extern volatile ULONG KdpDmesgFreeBytes
;
128 extern volatile ULONG KdbDmesgTotalWritten
;
135 BOOLEAN (*Fn
)(ULONG Argc
, PCHAR Argv
[]);
136 } KdbDebuggerCommands
[] = {
138 { NULL
, NULL
, "Data", NULL
},
139 { "?", "? expression", "Evaluate expression.", KdbpCmdEvalExpression
},
140 { "disasm", "disasm [address] [L count]", "Disassemble count instructions at address.", KdbpCmdDisassembleX
},
141 { "x", "x [address] [L count]", "Display count dwords, starting at addr.", KdbpCmdDisassembleX
},
142 { "regs", "regs", "Display general purpose registers.", KdbpCmdRegs
},
143 { "cregs", "cregs", "Display control registers.", KdbpCmdRegs
},
144 { "sregs", "sregs", "Display status registers.", KdbpCmdRegs
},
145 { "dregs", "dregs", "Display debug registers.", KdbpCmdRegs
},
146 { "bt", "bt [*frameaddr|thread id]", "Prints current backtrace or from given frame addr", KdbpCmdBackTrace
},
148 { "dt", "dt [mod] [type] [addr]", "Print a struct. Addr is optional.", KdbpCmdPrintStruct
},
152 { NULL
, NULL
, "Flow control", NULL
},
153 { "cont", "cont", "Continue execution (leave debugger)", KdbpCmdContinue
},
154 { "step", "step [count]", "Execute single instructions, stepping into interrupts.", KdbpCmdStep
},
155 { "next", "next [count]", "Execute single instructions, skipping calls and reps.", KdbpCmdStep
},
156 { "bl", "bl", "List breakpoints.", KdbpCmdBreakPointList
},
157 { "be", "be [breakpoint]", "Enable breakpoint.", KdbpCmdEnableDisableClearBreakPoint
},
158 { "bd", "bd [breakpoint]", "Disable breakpoint.", KdbpCmdEnableDisableClearBreakPoint
},
159 { "bc", "bc [breakpoint]", "Clear breakpoint.", KdbpCmdEnableDisableClearBreakPoint
},
160 { "bpx", "bpx [address] [IF condition]", "Set software execution breakpoint at address.", KdbpCmdBreakPoint
},
161 { "bpm", "bpm [r|w|rw|x] [byte|word|dword] [address] [IF condition]", "Set memory breakpoint at address.", KdbpCmdBreakPoint
},
164 { NULL
, NULL
, "Process/Thread", NULL
},
165 { "thread", "thread [list[ pid]|[attach ]tid]", "List threads in current or specified process, display thread with given id or attach to thread.", KdbpCmdThread
},
166 { "proc", "proc [list|[attach ]pid]", "List processes, display process with given id or attach to process.", KdbpCmdProc
},
168 /* System information */
169 { NULL
, NULL
, "System info", NULL
},
170 { "mod", "mod [address]", "List all modules or the one containing address.", KdbpCmdMod
},
171 { "gdt", "gdt", "Display global descriptor table.", KdbpCmdGdtLdtIdt
},
172 { "ldt", "ldt", "Display local descriptor table.", KdbpCmdGdtLdtIdt
},
173 { "idt", "idt", "Display interrupt descriptor table.", KdbpCmdGdtLdtIdt
},
174 { "pcr", "pcr", "Display processor control region.", KdbpCmdPcr
},
175 { "tss", "tss", "Display task state segment.", KdbpCmdTss
},
178 { NULL
, NULL
, "Others", NULL
},
179 { "bugcheck", "bugcheck", "Bugchecks the system.", KdbpCmdBugCheck
},
180 { "reboot", "reboot", "Reboots the system.", KdbpCmdReboot
},
181 { "filter", "filter [error|warning|trace|info|level]+|-[componentname|default]", "Enable/disable debug channels", KdbpCmdFilter
},
182 { "set", "set [var] [value]", "Sets var to value or displays value of var.", KdbpCmdSet
},
183 { "dmesg", "dmesg", "Display debug messages on screen, with navigation on pages.", KdbpCmdDmesg
},
184 { "kmsg", "kmsg", "Kernel dmesg. Alias for dmesg.", KdbpCmdDmesg
},
185 { "help", "help", "Display help screen.", KdbpCmdHelp
},
186 { "!pool", "!pool [Address [Flags]]", "Display information about pool allocations.", ExpKdbgExtPool
}
189 /* FUNCTIONS *****************************************************************/
191 /*!\brief Transform a component name to an integer
193 * \param ComponentName The name of the component.
194 * \param ComponentId Receives the component id on success.
196 * \retval TRUE Success.
197 * \retval FALSE Failure.
201 IN PCCH ComponentName
,
202 OUT PULONG ComponentId
)
213 { "DEFAULT", MAXULONG
},
214 { "SYSTEM", DPFLTR_SYSTEM_ID
},
215 { "SMSS", DPFLTR_SMSS_ID
},
216 { "SETUP", DPFLTR_SETUP_ID
},
217 { "NTFS", DPFLTR_NTFS_ID
},
218 { "FSTUB", DPFLTR_FSTUB_ID
},
219 { "CRASHDUMP", DPFLTR_CRASHDUMP_ID
},
220 { "CDAUDIO", DPFLTR_CDAUDIO_ID
},
221 { "CDROM", DPFLTR_CDROM_ID
},
222 { "CLASSPNP", DPFLTR_CLASSPNP_ID
},
223 { "DISK", DPFLTR_DISK_ID
},
224 { "REDBOOK", DPFLTR_REDBOOK_ID
},
225 { "STORPROP", DPFLTR_STORPROP_ID
},
226 { "SCSIPORT", DPFLTR_SCSIPORT_ID
},
227 { "SCSIMINIPORT", DPFLTR_SCSIMINIPORT_ID
},
228 { "CONFIG", DPFLTR_CONFIG_ID
},
229 { "I8042PRT", DPFLTR_I8042PRT_ID
},
230 { "SERMOUSE", DPFLTR_SERMOUSE_ID
},
231 { "LSERMOUS", DPFLTR_LSERMOUS_ID
},
232 { "KBDHID", DPFLTR_KBDHID_ID
},
233 { "MOUHID", DPFLTR_MOUHID_ID
},
234 { "KBDCLASS", DPFLTR_KBDCLASS_ID
},
235 { "MOUCLASS", DPFLTR_MOUCLASS_ID
},
236 { "TWOTRACK", DPFLTR_TWOTRACK_ID
},
237 { "WMILIB", DPFLTR_WMILIB_ID
},
238 { "ACPI", DPFLTR_ACPI_ID
},
239 { "AMLI", DPFLTR_AMLI_ID
},
240 { "HALIA64", DPFLTR_HALIA64_ID
},
241 { "VIDEO", DPFLTR_VIDEO_ID
},
242 { "SVCHOST", DPFLTR_SVCHOST_ID
},
243 { "VIDEOPRT", DPFLTR_VIDEOPRT_ID
},
244 { "TCPIP", DPFLTR_TCPIP_ID
},
245 { "DMSYNTH", DPFLTR_DMSYNTH_ID
},
246 { "NTOSPNP", DPFLTR_NTOSPNP_ID
},
247 { "FASTFAT", DPFLTR_FASTFAT_ID
},
248 { "SAMSS", DPFLTR_SAMSS_ID
},
249 { "PNPMGR", DPFLTR_PNPMGR_ID
},
250 { "NETAPI", DPFLTR_NETAPI_ID
},
251 { "SCSERVER", DPFLTR_SCSERVER_ID
},
252 { "SCCLIENT", DPFLTR_SCCLIENT_ID
},
253 { "SERIAL", DPFLTR_SERIAL_ID
},
254 { "SERENUM", DPFLTR_SERENUM_ID
},
255 { "UHCD", DPFLTR_UHCD_ID
},
256 { "RPCPROXY", DPFLTR_RPCPROXY_ID
},
257 { "AUTOCHK", DPFLTR_AUTOCHK_ID
},
258 { "DCOMSS", DPFLTR_DCOMSS_ID
},
259 { "UNIMODEM", DPFLTR_UNIMODEM_ID
},
260 { "SIS", DPFLTR_SIS_ID
},
261 { "FLTMGR", DPFLTR_FLTMGR_ID
},
262 { "WMICORE", DPFLTR_WMICORE_ID
},
263 { "BURNENG", DPFLTR_BURNENG_ID
},
264 { "IMAPI", DPFLTR_IMAPI_ID
},
265 { "SXS", DPFLTR_SXS_ID
},
266 { "FUSION", DPFLTR_FUSION_ID
},
267 { "IDLETASK", DPFLTR_IDLETASK_ID
},
268 { "SOFTPCI", DPFLTR_SOFTPCI_ID
},
269 { "TAPE", DPFLTR_TAPE_ID
},
270 { "MCHGR", DPFLTR_MCHGR_ID
},
271 { "IDEP", DPFLTR_IDEP_ID
},
272 { "PCIIDE", DPFLTR_PCIIDE_ID
},
273 { "FLOPPY", DPFLTR_FLOPPY_ID
},
274 { "FDC", DPFLTR_FDC_ID
},
275 { "TERMSRV", DPFLTR_TERMSRV_ID
},
276 { "W32TIME", DPFLTR_W32TIME_ID
},
277 { "PREFETCHER", DPFLTR_PREFETCHER_ID
},
278 { "RSFILTER", DPFLTR_RSFILTER_ID
},
279 { "FCPORT", DPFLTR_FCPORT_ID
},
280 { "PCI", DPFLTR_PCI_ID
},
281 { "DMIO", DPFLTR_DMIO_ID
},
282 { "DMCONFIG", DPFLTR_DMCONFIG_ID
},
283 { "DMADMIN", DPFLTR_DMADMIN_ID
},
284 { "WSOCKTRANSPORT", DPFLTR_WSOCKTRANSPORT_ID
},
285 { "VSS", DPFLTR_VSS_ID
},
286 { "PNPMEM", DPFLTR_PNPMEM_ID
},
287 { "PROCESSOR", DPFLTR_PROCESSOR_ID
},
288 { "DMSERVER", DPFLTR_DMSERVER_ID
},
289 { "SR", DPFLTR_SR_ID
},
290 { "INFINIBAND", DPFLTR_INFINIBAND_ID
},
291 { "IHVDRIVER", DPFLTR_IHVDRIVER_ID
},
292 { "IHVVIDEO", DPFLTR_IHVVIDEO_ID
},
293 { "IHVAUDIO", DPFLTR_IHVAUDIO_ID
},
294 { "IHVNETWORK", DPFLTR_IHVNETWORK_ID
},
295 { "IHVSTREAMING", DPFLTR_IHVSTREAMING_ID
},
296 { "IHVBUS", DPFLTR_IHVBUS_ID
},
297 { "HPS", DPFLTR_HPS_ID
},
298 { "RTLTHREADPOOL", DPFLTR_RTLTHREADPOOL_ID
},
299 { "LDR", DPFLTR_LDR_ID
},
300 { "TCPIP6", DPFLTR_TCPIP6_ID
},
301 { "ISAPNP", DPFLTR_ISAPNP_ID
},
302 { "SHPC", DPFLTR_SHPC_ID
},
303 { "STORPORT", DPFLTR_STORPORT_ID
},
304 { "STORMINIPORT", DPFLTR_STORMINIPORT_ID
},
305 { "PRINTSPOOLER", DPFLTR_PRINTSPOOLER_ID
},
306 { "VSSDYNDISK", DPFLTR_VSSDYNDISK_ID
},
307 { "VERIFIER", DPFLTR_VERIFIER_ID
},
308 { "VDS", DPFLTR_VDS_ID
},
309 { "VDSBAS", DPFLTR_VDSBAS_ID
},
310 { "VDSDYN", DPFLTR_VDSDYN_ID
},
311 { "VDSDYNDR", DPFLTR_VDSDYNDR_ID
},
312 { "VDSLDR", DPFLTR_VDSLDR_ID
},
313 { "VDSUTIL", DPFLTR_VDSUTIL_ID
},
314 { "DFRGIFC", DPFLTR_DFRGIFC_ID
},
315 { "MM", DPFLTR_MM_ID
},
316 { "DFSC", DPFLTR_DFSC_ID
},
317 { "WOW64", DPFLTR_WOW64_ID
},
318 { "ALPC", DPFLTR_ALPC_ID
},
319 { "WDI", DPFLTR_WDI_ID
},
320 { "PERFLIB", DPFLTR_PERFLIB_ID
},
321 { "KTM", DPFLTR_KTM_ID
},
322 { "IOSTRESS", DPFLTR_IOSTRESS_ID
},
323 { "HEAP", DPFLTR_HEAP_ID
},
324 { "WHEA", DPFLTR_WHEA_ID
},
325 { "USERGDI", DPFLTR_USERGDI_ID
},
326 { "MMCSS", DPFLTR_MMCSS_ID
},
327 { "TPM", DPFLTR_TPM_ID
},
328 { "THREADORDER", DPFLTR_THREADORDER_ID
},
329 { "ENVIRON", DPFLTR_ENVIRON_ID
},
330 { "EMS", DPFLTR_EMS_ID
},
331 { "WDT", DPFLTR_WDT_ID
},
332 { "FVEVOL", DPFLTR_FVEVOL_ID
},
333 { "NDIS", DPFLTR_NDIS_ID
},
334 { "NVCTRACE", DPFLTR_NVCTRACE_ID
},
335 { "LUAFV", DPFLTR_LUAFV_ID
},
336 { "APPCOMPAT", DPFLTR_APPCOMPAT_ID
},
337 { "USBSTOR", DPFLTR_USBSTOR_ID
},
338 { "SBP2PORT", DPFLTR_SBP2PORT_ID
},
339 { "COVERAGE", DPFLTR_COVERAGE_ID
},
340 { "CACHEMGR", DPFLTR_CACHEMGR_ID
},
341 { "MOUNTMGR", DPFLTR_MOUNTMGR_ID
},
342 { "CFR", DPFLTR_CFR_ID
},
343 { "TXF", DPFLTR_TXF_ID
},
344 { "KSECDD", DPFLTR_KSECDD_ID
},
345 { "FLTREGRESS", DPFLTR_FLTREGRESS_ID
},
346 { "MPIO", DPFLTR_MPIO_ID
},
347 { "MSDSM", DPFLTR_MSDSM_ID
},
348 { "UDFS", DPFLTR_UDFS_ID
},
349 { "PSHED", DPFLTR_PSHED_ID
},
350 { "STORVSP", DPFLTR_STORVSP_ID
},
351 { "LSASS", DPFLTR_LSASS_ID
},
352 { "SSPICLI", DPFLTR_SSPICLI_ID
},
353 { "CNG", DPFLTR_CNG_ID
},
354 { "EXFAT", DPFLTR_EXFAT_ID
},
355 { "FILETRACE", DPFLTR_FILETRACE_ID
},
356 { "XSAVE", DPFLTR_XSAVE_ID
},
357 { "SE", DPFLTR_SE_ID
},
358 { "DRIVEEXTENDER", DPFLTR_DRIVEEXTENDER_ID
},
361 for (i
= 0; i
< sizeof(ComponentTable
) / sizeof(ComponentTable
[0]); i
++)
363 if (_stricmp(ComponentName
, ComponentTable
[i
].Name
) == 0)
365 *ComponentId
= ComponentTable
[i
].Id
;
373 /*!\brief Evaluates an expression...
375 * Much like KdbpRpnEvaluateExpression, but prints the error message (if any)
376 * at the given offset.
378 * \param Expression Expression to evaluate.
379 * \param ErrOffset Offset (in characters) to print the error message at.
380 * \param Result Receives the result on success.
382 * \retval TRUE Success.
383 * \retval FALSE Failure.
386 KdbpEvaluateExpression(
389 OUT PULONGLONG Result
)
391 static CHAR ErrMsgBuffer
[130] = "^ ";
392 LONG ExpressionErrOffset
= -1;
393 PCHAR ErrMsg
= ErrMsgBuffer
;
396 Ok
= KdbpRpnEvaluateExpression(Expression
, KdbCurrentTrapFrame
, Result
,
397 &ExpressionErrOffset
, ErrMsgBuffer
+ 2);
400 if (ExpressionErrOffset
>= 0)
401 ExpressionErrOffset
+= ErrOffset
;
405 KdbpPrint("%*s%s\n", ExpressionErrOffset
, "", ErrMsg
);
415 OUT ULONG_PTR
*pulValue
)
419 /* Skip optional '0x' prefix */
420 if ((pszNum
[0] == '0') && ((pszNum
[1] == 'x') || (pszNum
[1] == 'X')))
423 /* Make a number from the string (hex) */
424 *pulValue
= strtoul(pszNum
, &endptr
, 16);
426 return (*endptr
== '\0');
429 /*!\brief Evaluates an expression and displays the result.
432 KdbpCmdEvalExpression(
437 ULONGLONG Result
= 0;
444 KdbpPrint("?: Argument required\n");
448 /* Put the arguments back together */
450 for (i
= 1; i
< Argc
; i
++)
452 len
= strlen(Argv
[i
]);
456 /* Evaluate the expression */
457 Ok
= KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
);
460 if (Result
> 0x00000000ffffffffLL
)
462 if (Result
& 0x8000000000000000LL
)
463 KdbpPrint("0x%016I64x %20I64u %20I64d\n", Result
, Result
, Result
);
465 KdbpPrint("0x%016I64x %20I64u\n", Result
, Result
);
471 if (ul
<= 0xff && ul
>= 0x80)
472 l
= (LONG
)((CHAR
)ul
);
473 else if (ul
<= 0xffff && ul
>= 0x8000)
474 l
= (LONG
)((SHORT
)ul
);
479 KdbpPrint("0x%08lx %10lu %10ld\n", ul
, ul
, l
);
481 KdbpPrint("0x%08lx %10lu\n", ul
, ul
);
490 /*!\brief Print a struct
493 KdbpPrintStructInternal
498 PROSSYM_AGGREGATE Aggregate
)
502 PROSSYM_AGGREGATE_MEMBER Member
;
503 ULONG IndentLen
= strlen(Indent
);
504 ROSSYM_AGGREGATE MemberAggregate
= {0 };
506 for (i
= 0; i
< Aggregate
->NumElements
; i
++) {
507 Member
= &Aggregate
->Elements
[i
];
508 KdbpPrint("%s%p+%x: %s", Indent
, ((PCHAR
)BaseAddress
) + Member
->BaseOffset
, Member
->Size
, Member
->Name
? Member
->Name
: "<anoymous>");
510 if (!strcmp(Member
->Type
, "_UNICODE_STRING")) {
511 KdbpPrint("\"%wZ\"\n", ((PCHAR
)BaseAddress
) + Member
->BaseOffset
);
513 } else if (!strcmp(Member
->Type
, "PUNICODE_STRING")) {
514 KdbpPrint("\"%wZ\"\n", *(((PUNICODE_STRING
*)((PCHAR
)BaseAddress
) + Member
->BaseOffset
)));
517 switch (Member
->Size
) {
523 if (NT_SUCCESS(KdbpSafeReadMemory(&Result
, ((PCHAR
)BaseAddress
) + Member
->BaseOffset
, Member
->Size
))) {
525 Result
>>= Member
->FirstBit
;
526 Result
&= ((1 << Member
->Bits
) - 1);
528 KdbpPrint(" %lx\n", Result
);
534 if (Member
->Size
< 8) {
535 if (NT_SUCCESS(KdbpSafeReadMemory(&Result
, ((PCHAR
)BaseAddress
) + Member
->BaseOffset
, Member
->Size
))) {
537 for (j
= 0; j
< Member
->Size
; j
++) {
538 KdbpPrint(" %02x", (int)(Result
& 0xff));
541 } else goto readfail
;
543 KdbpPrint(" %s @ %p {\n", Member
->Type
, ((PCHAR
)BaseAddress
) + Member
->BaseOffset
);
544 Indent
[IndentLen
] = ' ';
545 if (RosSymAggregate(Info
, Member
->Type
, &MemberAggregate
)) {
546 KdbpPrintStructInternal(Info
, Indent
, DoRead
, ((PCHAR
)BaseAddress
) + Member
->BaseOffset
, &MemberAggregate
);
547 RosSymFreeAggregate(&MemberAggregate
);
549 Indent
[IndentLen
] = 0;
550 KdbpPrint("%s}\n", Indent
);
556 if (Member
->Size
<= 8) {
559 KdbpPrint(" %s @ %x {\n", Member
->Type
, Member
->BaseOffset
);
560 Indent
[IndentLen
] = ' ';
561 if (RosSymAggregate(Info
, Member
->Type
, &MemberAggregate
)) {
562 KdbpPrintStructInternal(Info
, Indent
, DoRead
, BaseAddress
, &MemberAggregate
);
563 RosSymFreeAggregate(&MemberAggregate
);
565 Indent
[IndentLen
] = 0;
566 KdbpPrint("%s}\n", Indent
);
572 PROSSYM_INFO
KdbpSymFindCachedFile(PUNICODE_STRING ModName
);
580 ULONGLONG Result
= 0;
581 PVOID BaseAddress
= 0;
582 ROSSYM_AGGREGATE Aggregate
= {0};
583 UNICODE_STRING ModName
= {0};
584 ANSI_STRING AnsiName
= {0};
585 CHAR Indent
[100] = {0};
588 if (Argc
< 3) goto end
;
589 AnsiName
.Length
= AnsiName
.MaximumLength
= strlen(Argv
[1]);
590 AnsiName
.Buffer
= Argv
[1];
591 RtlAnsiStringToUnicodeString(&ModName
, &AnsiName
, TRUE
);
592 Info
= KdbpSymFindCachedFile(&ModName
);
594 if (!Info
|| !RosSymAggregate(Info
, Argv
[2], &Aggregate
)) {
595 DPRINT1("Could not get aggregate\n");
599 // Get an argument for location if it was given
602 PCHAR ArgStart
= Argv
[3];
603 DPRINT1("Trying to get expression\n");
604 for (i
= 3; i
< Argc
- 1; i
++)
606 len
= strlen(Argv
[i
]);
610 /* Evaluate the expression */
611 DPRINT1("Arg: %s\n", ArgStart
);
612 if (KdbpEvaluateExpression(ArgStart
, strlen(ArgStart
), &Result
)) {
613 BaseAddress
= (PVOID
)(ULONG_PTR
)Result
;
614 DPRINT1("BaseAddress: %p\n", BaseAddress
);
617 DPRINT1("BaseAddress %p\n", BaseAddress
);
618 KdbpPrintStructInternal(Info
, Indent
, !!BaseAddress
, BaseAddress
, &Aggregate
);
620 RosSymFreeAggregate(&Aggregate
);
621 RtlFreeUnicodeString(&ModName
);
626 /*!\brief Display list of active debug channels
633 ULONG i
, j
, ComponentId
, Level
;
634 ULONG set
= DPFLTR_MASK
, clear
= DPFLTR_MASK
;
645 { "error", 1 << DPFLTR_ERROR_LEVEL
},
646 { "warning", 1 << DPFLTR_WARNING_LEVEL
},
647 { "trace", 1 << DPFLTR_TRACE_LEVEL
},
648 { "info", 1 << DPFLTR_INFO_LEVEL
},
651 for (i
= 1; i
< Argc
; i
++)
654 p
= opt
+ strcspn(opt
, "+-");
655 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
659 for (j
= 0; j
< sizeof(debug_classes
) / sizeof(debug_classes
[0]); j
++)
661 SIZE_T len
= strlen(debug_classes
[j
].Name
);
662 if (len
!= (p
- opt
))
664 if (_strnicmp(opt
, debug_classes
[j
].Name
, len
) == 0) /* found it */
667 set
|= debug_classes
[j
].Level
;
669 clear
|= debug_classes
[j
].Level
;
673 if (j
== sizeof(debug_classes
) / sizeof(debug_classes
[0]))
675 Level
= strtoul(opt
, &pend
, 0);
678 KdbpPrint("filter: bad class name '%.*s'\n", p
- opt
, opt
);
694 if (*p
== '+' || *p
== '-')
697 if (!KdbpGetComponentId(p
, &ComponentId
))
699 KdbpPrint("filter: '%s' is not a valid component name!\n", p
);
703 /* Get current mask value */
704 NtSetDebugFilterState(ComponentId
, set
, TRUE
);
705 NtSetDebugFilterState(ComponentId
, clear
, FALSE
);
711 /*!\brief Disassembles 10 instructions at eip or given address or
712 * displays 16 dwords from memory at given address.
722 ULONGLONG Result
= 0;
723 ULONG_PTR Address
= KdbCurrentTrapFrame
->Tf
.Eip
;
726 if (Argv
[0][0] == 'x') /* display memory */
728 else /* disassemble */
733 /* Check for [L count] part */
735 if (strcmp(Argv
[Argc
-2], "L") == 0)
737 ul
= strtoul(Argv
[Argc
-1], NULL
, 0);
744 else if (Argv
[Argc
-1][0] == 'L')
746 ul
= strtoul(Argv
[Argc
-1] + 1, NULL
, 0);
754 /* Put the remaining arguments back together */
756 for (ul
= 1; ul
< Argc
; ul
++)
758 Argv
[ul
][strlen(Argv
[ul
])] = ' ';
763 /* Evaluate the expression */
766 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
769 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
770 KdbpPrint("Warning: Address %I64x is beeing truncated\n",Result
);
772 Address
= (ULONG_PTR
)Result
;
774 else if (Argv
[0][0] == 'x')
776 KdbpPrint("x: Address argument required.\n");
780 if (Argv
[0][0] == 'x')
787 if (!KdbSymPrintAddress((PVOID
)Address
, NULL
))
788 KdbpPrint("<%x>:", Address
);
797 if (!NT_SUCCESS(KdbpSafeReadMemory(&ul
, (PVOID
)Address
, sizeof(ul
))))
798 KdbpPrint(" ????????");
800 KdbpPrint(" %08x", ul
);
802 Address
+= sizeof(ul
);
813 if (!KdbSymPrintAddress((PVOID
)Address
, NULL
))
814 KdbpPrint("<%08x>: ", Address
);
818 InstLen
= KdbpDisassemble(Address
, KdbUseIntelSyntax
);
821 KdbpPrint("<INVALID>\n");
833 /*!\brief Displays CPU registers.
840 PKTRAP_FRAME Tf
= &KdbCurrentTrapFrame
->Tf
;
842 static const PCHAR EflagsBits
[32] = { " CF", NULL
, " PF", " BIT3", " AF", " BIT5",
843 " ZF", " SF", " TF", " IF", " DF", " OF",
844 NULL
, NULL
, " NT", " BIT15", " RF", " VF",
845 " AC", " VIF", " VIP", " ID", " BIT22",
846 " BIT23", " BIT24", " BIT25", " BIT26",
847 " BIT27", " BIT28", " BIT29", " BIT30",
850 if (Argv
[0][0] == 'r') /* regs */
852 KdbpPrint("CS:EIP 0x%04x:0x%08x\n"
853 "SS:ESP 0x%04x:0x%08x\n"
854 " EAX 0x%08x EBX 0x%08x\n"
855 " ECX 0x%08x EDX 0x%08x\n"
856 " ESI 0x%08x EDI 0x%08x\n"
858 Tf
->SegCs
& 0xFFFF, Tf
->Eip
,
859 Tf
->HardwareSegSs
, Tf
->HardwareEsp
,
864 KdbpPrint("EFLAGS 0x%08x ", Tf
->EFlags
);
866 for (i
= 0; i
< 32; i
++)
870 if ((Tf
->EFlags
& (1 << 1)) == 0)
875 KdbpPrint(" IOPL%d", (Tf
->EFlags
>> 12) & 3);
880 else if ((Tf
->EFlags
& (1 << i
)) != 0)
882 KdbpPrint(EflagsBits
[i
]);
888 else if (Argv
[0][0] == 'c') /* cregs */
890 ULONG Cr0
, Cr2
, Cr3
, Cr4
;
891 KDESCRIPTOR Gdtr
= {0, 0, 0}, Idtr
= {0, 0, 0};
893 static const PCHAR Cr0Bits
[32] = { " PE", " MP", " EM", " TS", " ET", " NE", NULL
, NULL
,
894 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
895 " WP", NULL
, " AM", NULL
, NULL
, NULL
, NULL
, NULL
,
896 NULL
, NULL
, NULL
, NULL
, NULL
, " NW", " CD", " PG" };
897 static const PCHAR Cr4Bits
[32] = { " VME", " PVI", " TSD", " DE", " PSE", " PAE", " MCE", " PGE",
898 " PCE", " OSFXSR", " OSXMMEXCPT", NULL
, NULL
, NULL
, NULL
, NULL
,
899 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
900 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
902 Cr0
= KdbCurrentTrapFrame
->Cr0
;
903 Cr2
= KdbCurrentTrapFrame
->Cr2
;
904 Cr3
= KdbCurrentTrapFrame
->Cr3
;
905 Cr4
= KdbCurrentTrapFrame
->Cr4
;
907 /* Get descriptor table regs */
908 Ke386GetGlobalDescriptorTable(&Gdtr
.Limit
);
909 Ldtr
= Ke386GetLocalDescriptorTable();
912 /* Display the control registers */
913 KdbpPrint("CR0 0x%08x ", Cr0
);
915 for (i
= 0; i
< 32; i
++)
920 if ((Cr0
& (1 << i
)) != 0)
921 KdbpPrint(Cr0Bits
[i
]);
924 KdbpPrint("\nCR2 0x%08x\n", Cr2
);
925 KdbpPrint("CR3 0x%08x Pagedir-Base 0x%08x %s%s\n", Cr3
, (Cr3
& 0xfffff000),
926 (Cr3
& (1 << 3)) ? " PWT" : "", (Cr3
& (1 << 4)) ? " PCD" : "" );
927 KdbpPrint("CR4 0x%08x ", Cr4
);
929 for (i
= 0; i
< 32; i
++)
934 if ((Cr4
& (1 << i
)) != 0)
935 KdbpPrint(Cr4Bits
[i
]);
938 /* Display the descriptor table regs */
939 KdbpPrint("\nGDTR Base 0x%08x Size 0x%04x\n", Gdtr
.Base
, Gdtr
.Limit
);
940 KdbpPrint("LDTR 0x%04x\n", Ldtr
);
941 KdbpPrint("IDTR Base 0x%08x Size 0x%04x\n", Idtr
.Base
, Idtr
.Limit
);
943 else if (Argv
[0][0] == 's') /* sregs */
945 KdbpPrint("CS 0x%04x Index 0x%04x %cDT RPL%d\n",
946 Tf
->SegCs
& 0xffff, (Tf
->SegCs
& 0xffff) >> 3,
947 (Tf
->SegCs
& (1 << 2)) ? 'L' : 'G', Tf
->SegCs
& 3);
948 KdbpPrint("DS 0x%04x Index 0x%04x %cDT RPL%d\n",
949 Tf
->SegDs
, Tf
->SegDs
>> 3, (Tf
->SegDs
& (1 << 2)) ? 'L' : 'G', Tf
->SegDs
& 3);
950 KdbpPrint("ES 0x%04x Index 0x%04x %cDT RPL%d\n",
951 Tf
->SegEs
, Tf
->SegEs
>> 3, (Tf
->SegEs
& (1 << 2)) ? 'L' : 'G', Tf
->SegEs
& 3);
952 KdbpPrint("FS 0x%04x Index 0x%04x %cDT RPL%d\n",
953 Tf
->SegFs
, Tf
->SegFs
>> 3, (Tf
->SegFs
& (1 << 2)) ? 'L' : 'G', Tf
->SegFs
& 3);
954 KdbpPrint("GS 0x%04x Index 0x%04x %cDT RPL%d\n",
955 Tf
->SegGs
, Tf
->SegGs
>> 3, (Tf
->SegGs
& (1 << 2)) ? 'L' : 'G', Tf
->SegGs
& 3);
956 KdbpPrint("SS 0x%04x Index 0x%04x %cDT RPL%d\n",
957 Tf
->HardwareSegSs
, Tf
->HardwareSegSs
>> 3, (Tf
->HardwareSegSs
& (1 << 2)) ? 'L' : 'G', Tf
->HardwareSegSs
& 3);
961 ASSERT(Argv
[0][0] == 'd');
962 KdbpPrint("DR0 0x%08x\n"
968 Tf
->Dr0
, Tf
->Dr1
, Tf
->Dr2
, Tf
->Dr3
,
976 KdbpTrapFrameFromPrevTss(
977 PKTRAP_FRAME TrapFrame
)
985 Ke386GetGlobalDescriptorTable(&Gdtr
.Limit
);
988 if ((Sel
& (sizeof(KGDTENTRY
) - 1)) ||
989 (Sel
< sizeof(KGDTENTRY
)) ||
990 (Sel
+ sizeof(KGDTENTRY
) - 1 > Gdtr
.Limit
))
993 if (!NT_SUCCESS(KdbpSafeReadMemory(&Desc
,
994 (PVOID
)(Gdtr
.Base
+ Sel
),
998 if (Desc
.HighWord
.Bits
.Type
!= 0xB)
1001 Tss
= (PKTSS
)(ULONG_PTR
)(Desc
.BaseLow
|
1002 Desc
.HighWord
.Bytes
.BaseMid
<< 16 |
1003 Desc
.HighWord
.Bytes
.BaseHi
<< 24);
1005 if (!NT_SUCCESS(KdbpSafeReadMemory(&Sel
,
1006 (PVOID
)&Tss
->Backlink
,
1010 if ((Sel
& (sizeof(KGDTENTRY
) - 1)) ||
1011 (Sel
< sizeof(KGDTENTRY
)) ||
1012 (Sel
+ sizeof(KGDTENTRY
) - 1 > Gdtr
.Limit
))
1015 if (!NT_SUCCESS(KdbpSafeReadMemory(&Desc
,
1016 (PVOID
)(Gdtr
.Base
+ Sel
),
1017 sizeof(KGDTENTRY
))))
1020 if (Desc
.HighWord
.Bits
.Type
!= 0xB)
1023 Tss
= (PKTSS
)(ULONG_PTR
)(Desc
.BaseLow
|
1024 Desc
.HighWord
.Bytes
.BaseMid
<< 16 |
1025 Desc
.HighWord
.Bytes
.BaseHi
<< 24);
1027 if (!NT_SUCCESS(KdbpSafeReadMemory(&Eip
,
1029 sizeof(ULONG_PTR
))))
1032 if (!NT_SUCCESS(KdbpSafeReadMemory(&Ebp
,
1034 sizeof(ULONG_PTR
))))
1037 TrapFrame
->Eip
= Eip
;
1038 TrapFrame
->Ebp
= Ebp
;
1042 VOID __cdecl
KiTrap02(VOID
);
1043 VOID FASTCALL
KiTrap03Handler(IN PKTRAP_FRAME
);
1044 VOID __cdecl
KiTrap08(VOID
);
1045 VOID __cdecl
KiTrap09(VOID
);
1048 KdbpInNmiOrDoubleFaultHandler(
1051 return (Address
> (ULONG_PTR
)KiTrap02
&& Address
< (ULONG_PTR
)KiTrap03Handler
) ||
1052 (Address
> (ULONG_PTR
)KiTrap08
&& Address
< (ULONG_PTR
)KiTrap09
);
1055 /*!\brief Displays a backtrace.
1063 ULONGLONG Result
= 0;
1064 ULONG_PTR Frame
= KdbCurrentTrapFrame
->Tf
.Ebp
;
1066 KTRAP_FRAME TrapFrame
;
1070 /* Check for [L count] part */
1073 if (strcmp(Argv
[Argc
-2], "L") == 0)
1075 ul
= strtoul(Argv
[Argc
-1], NULL
, 0);
1081 else if (Argv
[Argc
-1][0] == 'L')
1083 ul
= strtoul(Argv
[Argc
-1] + 1, NULL
, 0);
1090 /* Put the remaining arguments back together */
1092 for (ul
= 1; ul
< Argc
; ul
++)
1094 Argv
[ul
][strlen(Argv
[ul
])] = ' ';
1099 /* Check if frame addr or thread id is given. */
1102 if (Argv
[1][0] == '*')
1106 /* Evaluate the expression */
1107 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
1110 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
1111 KdbpPrint("Warning: Address %I64x is beeing truncated\n",Result
);
1113 Frame
= (ULONG_PTR
)Result
;
1117 KdbpPrint("Thread backtrace not supported yet!\n");
1123 KdbpPrint("Eip:\n");
1125 /* Try printing the function at EIP */
1126 if (!KdbSymPrintAddress((PVOID
)KdbCurrentTrapFrame
->Tf
.Eip
, &KdbCurrentTrapFrame
->Tf
))
1127 KdbpPrint("<%08x>\n", KdbCurrentTrapFrame
->Tf
.Eip
);
1132 TrapFrame
= KdbCurrentTrapFrame
->Tf
;
1133 KdbpPrint("Frames:\n");
1137 BOOLEAN GotNextFrame
;
1142 if (!NT_SUCCESS(KdbpSafeReadMemory(&Address
, (PVOID
)(Frame
+ sizeof(ULONG_PTR
)), sizeof (ULONG_PTR
))))
1144 KdbpPrint("Couldn't access memory at 0x%p!\n", Frame
+ sizeof(ULONG_PTR
));
1148 if ((GotNextFrame
= NT_SUCCESS(KdbpSafeReadMemory(&Frame
, (PVOID
)Frame
, sizeof (ULONG_PTR
)))))
1149 TrapFrame
.Ebp
= Frame
;
1151 /* Print the location of the call instruction */
1152 if (!KdbSymPrintAddress((PVOID
)(Address
- 5), &TrapFrame
))
1153 KdbpPrint("<%08x>\n", Address
);
1157 if (KdbOutputAborted
) break;
1162 if (KdbpInNmiOrDoubleFaultHandler(Address
))
1164 if ((GotNextFrame
= KdbpTrapFrameFromPrevTss(&TrapFrame
)))
1166 Address
= TrapFrame
.Eip
;
1167 Frame
= TrapFrame
.Ebp
;
1169 if (!KdbSymPrintAddress((PVOID
)Address
, &TrapFrame
))
1170 KdbpPrint("<%08x>\n", Address
);
1178 KdbpPrint("Couldn't access memory at 0x%p!\n", Frame
);
1186 /*!\brief Continues execution of the system/leaves KDB.
1193 /* Exit the main loop */
1197 /*!\brief Continues execution of the system/leaves KDB.
1208 Count
= strtoul(Argv
[1], NULL
, 0);
1211 KdbpPrint("%s: Integer argument required\n", Argv
[0]);
1216 if (Argv
[0][0] == 'n')
1217 KdbSingleStepOver
= TRUE
;
1219 KdbSingleStepOver
= FALSE
;
1221 /* Set the number of single steps and return to the interrupted code. */
1222 KdbNumSingleSteps
= Count
;
1227 /*!\brief Lists breakpoints.
1230 KdbpCmdBreakPointList(
1235 ULONG_PTR Address
= 0;
1236 KDB_BREAKPOINT_TYPE Type
= 0;
1237 KDB_ACCESS_TYPE AccessType
= 0;
1240 BOOLEAN Enabled
= FALSE
;
1241 BOOLEAN Global
= FALSE
;
1242 PEPROCESS Process
= NULL
;
1243 PCHAR str1
, str2
, ConditionExpr
, GlobalOrLocal
;
1246 l
= KdbpGetNextBreakPointNr(0);
1249 KdbpPrint("No breakpoints.\n");
1253 KdbpPrint("Breakpoints:\n");
1256 if (!KdbpGetBreakPointInfo(l
, &Address
, &Type
, &Size
, &AccessType
, &DebugReg
,
1257 &Enabled
, &Global
, &Process
, &ConditionExpr
))
1262 if (l
== KdbLastBreakPointNr
)
1275 GlobalOrLocal
= " global";
1279 GlobalOrLocal
= Buffer
;
1280 sprintf(Buffer
, " PID 0x%08lx",
1281 (ULONG
)(Process
? Process
->UniqueProcessId
: INVALID_HANDLE_VALUE
));
1284 if (Type
== KdbBreakPointSoftware
|| Type
== KdbBreakPointTemporary
)
1286 KdbpPrint(" %s%03d BPX 0x%08x%s%s%s%s%s\n",
1288 Enabled
? "" : " disabled",
1290 ConditionExpr
? " IF " : "",
1291 ConditionExpr
? ConditionExpr
: "",
1294 else if (Type
== KdbBreakPointHardware
)
1298 KdbpPrint(" %s%03d BPM 0x%08x %-5s %-5s disabled%s%s%s%s\n", str1
, l
, Address
,
1299 KDB_ACCESS_TYPE_TO_STRING(AccessType
),
1300 Size
== 1 ? "byte" : (Size
== 2 ? "word" : "dword"),
1302 ConditionExpr
? " IF " : "",
1303 ConditionExpr
? ConditionExpr
: "",
1308 KdbpPrint(" %s%03d BPM 0x%08x %-5s %-5s DR%d%s%s%s%s\n", str1
, l
, Address
,
1309 KDB_ACCESS_TYPE_TO_STRING(AccessType
),
1310 Size
== 1 ? "byte" : (Size
== 2 ? "word" : "dword"),
1313 ConditionExpr
? " IF " : "",
1314 ConditionExpr
? ConditionExpr
: "",
1319 while ((l
= KdbpGetNextBreakPointNr(l
+1)) >= 0);
1324 /*!\brief Enables, disables or clears a breakpoint.
1327 KdbpCmdEnableDisableClearBreakPoint(
1336 KdbpPrint("%s: argument required\n", Argv
[0]);
1341 BreakPointNr
= strtoul(Argv
[1], &pend
, 0);
1342 if (pend
== Argv
[1] || *pend
!= '\0')
1344 KdbpPrint("%s: integer argument required\n", Argv
[0]);
1348 if (Argv
[0][1] == 'e') /* enable */
1350 KdbpEnableBreakPoint(BreakPointNr
, NULL
);
1352 else if (Argv
[0][1] == 'd') /* disable */
1354 KdbpDisableBreakPoint(BreakPointNr
, NULL
);
1358 ASSERT(Argv
[0][1] == 'c');
1359 KdbpDeleteBreakPoint(BreakPointNr
, NULL
);
1365 /*!\brief Sets a software or hardware (memory) breakpoint at the given address.
1368 KdbpCmdBreakPoint(ULONG Argc
, PCHAR Argv
[])
1370 ULONGLONG Result
= 0;
1372 KDB_BREAKPOINT_TYPE Type
;
1374 KDB_ACCESS_TYPE AccessType
= 0;
1375 ULONG AddressArgIndex
, i
;
1376 LONG ConditionArgIndex
;
1377 BOOLEAN Global
= TRUE
;
1379 if (Argv
[0][2] == 'x') /* software breakpoint */
1383 KdbpPrint("bpx: Address argument required.\n");
1387 AddressArgIndex
= 1;
1388 Type
= KdbBreakPointSoftware
;
1390 else /* memory breakpoint */
1392 ASSERT(Argv
[0][2] == 'm');
1396 KdbpPrint("bpm: Access type argument required (one of r, w, rw, x)\n");
1400 if (_stricmp(Argv
[1], "x") == 0)
1401 AccessType
= KdbAccessExec
;
1402 else if (_stricmp(Argv
[1], "r") == 0)
1403 AccessType
= KdbAccessRead
;
1404 else if (_stricmp(Argv
[1], "w") == 0)
1405 AccessType
= KdbAccessWrite
;
1406 else if (_stricmp(Argv
[1], "rw") == 0)
1407 AccessType
= KdbAccessReadWrite
;
1410 KdbpPrint("bpm: Unknown access type '%s'\n", Argv
[1]);
1416 KdbpPrint("bpm: %s argument required.\n", AccessType
== KdbAccessExec
? "Address" : "Memory size");
1420 AddressArgIndex
= 3;
1421 if (_stricmp(Argv
[2], "byte") == 0)
1423 else if (_stricmp(Argv
[2], "word") == 0)
1425 else if (_stricmp(Argv
[2], "dword") == 0)
1427 else if (AccessType
== KdbAccessExec
)
1434 KdbpPrint("bpm: Unknown memory size '%s'\n", Argv
[2]);
1438 if (Argc
<= AddressArgIndex
)
1440 KdbpPrint("bpm: Address argument required.\n");
1444 Type
= KdbBreakPointHardware
;
1447 /* Put the arguments back together */
1448 ConditionArgIndex
= -1;
1449 for (i
= AddressArgIndex
; i
< (Argc
-1); i
++)
1451 if (strcmp(Argv
[i
+1], "IF") == 0) /* IF found */
1453 ConditionArgIndex
= i
+ 2;
1454 if ((ULONG
)ConditionArgIndex
>= Argc
)
1456 KdbpPrint("%s: IF requires condition expression.\n", Argv
[0]);
1460 for (i
= ConditionArgIndex
; i
< (Argc
-1); i
++)
1461 Argv
[i
][strlen(Argv
[i
])] = ' ';
1466 Argv
[i
][strlen(Argv
[i
])] = ' ';
1469 /* Evaluate the address expression */
1470 if (!KdbpEvaluateExpression(Argv
[AddressArgIndex
],
1471 sizeof("kdb:> ")-1 + (Argv
[AddressArgIndex
]-Argv
[0]),
1477 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
1478 KdbpPrint("%s: Warning: Address %I64x is beeing truncated\n", Argv
[0],Result
);
1480 Address
= (ULONG_PTR
)Result
;
1482 KdbpInsertBreakPoint(Address
, Type
, Size
, AccessType
,
1483 (ConditionArgIndex
< 0) ? NULL
: Argv
[ConditionArgIndex
],
1489 /*!\brief Lists threads or switches to another thread context.
1497 PETHREAD Thread
= NULL
;
1498 PEPROCESS Process
= NULL
;
1499 BOOLEAN ReferencedThread
= FALSE
, ReferencedProcess
= FALSE
;
1504 PCHAR State
, pend
, str1
, str2
;
1505 static const PCHAR ThreadStateToString
[DeferredReady
+1] =
1507 "Initialized", "Ready", "Running",
1508 "Standby", "Terminated", "Waiting",
1509 "Transition", "DeferredReady"
1512 ASSERT(KdbCurrentProcess
);
1514 if (Argc
>= 2 && _stricmp(Argv
[1], "list") == 0)
1516 Process
= KdbCurrentProcess
;
1520 ul
= strtoul(Argv
[2], &pend
, 0);
1521 if (Argv
[2] == pend
)
1523 KdbpPrint("thread: '%s' is not a valid process id!\n", Argv
[2]);
1527 if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID
)ul
, &Process
)))
1529 KdbpPrint("thread: Invalid process id!\n");
1533 /* Remember our reference */
1534 ReferencedProcess
= TRUE
;
1537 Entry
= Process
->ThreadListHead
.Flink
;
1538 if (Entry
== &Process
->ThreadListHead
)
1541 KdbpPrint("No threads in process 0x%08x!\n", ul
);
1543 KdbpPrint("No threads in current process!\n");
1545 if (ReferencedProcess
)
1546 ObDereferenceObject(Process
);
1551 KdbpPrint(" TID State Prior. Affinity EBP EIP\n");
1554 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
1556 if (Thread
== KdbCurrentThread
)
1567 if (!Thread
->Tcb
.InitialStack
)
1569 /* Thread has no kernel stack (probably terminated) */
1573 else if (Thread
->Tcb
.TrapFrame
)
1575 if (Thread
->Tcb
.TrapFrame
->PreviousPreviousMode
== KernelMode
)
1576 Esp
= (PULONG
)Thread
->Tcb
.TrapFrame
->TempEsp
;
1578 Esp
= (PULONG
)Thread
->Tcb
.TrapFrame
->HardwareEsp
;
1580 Ebp
= (PULONG
)Thread
->Tcb
.TrapFrame
->Ebp
;
1581 Eip
= Thread
->Tcb
.TrapFrame
->Eip
;
1585 Esp
= (PULONG
)Thread
->Tcb
.KernelStack
;
1586 Ebp
= (PULONG
)Esp
[4];
1589 if (Ebp
) /* FIXME: Should we attach to the process to read Ebp[1]? */
1590 KdbpSafeReadMemory(&Eip
, Ebp
+ 1, sizeof (Eip
));
1593 if (Thread
->Tcb
.State
< (DeferredReady
+ 1))
1594 State
= ThreadStateToString
[Thread
->Tcb
.State
];
1598 KdbpPrint(" %s0x%08x %-11s %3d 0x%08x 0x%08x 0x%08x%s\n",
1600 Thread
->Cid
.UniqueThread
,
1602 Thread
->Tcb
.Priority
,
1603 Thread
->Tcb
.Affinity
,
1608 Entry
= Entry
->Flink
;
1610 while (Entry
!= &Process
->ThreadListHead
);
1612 /* Release our reference, if any */
1613 if (ReferencedProcess
)
1614 ObDereferenceObject(Process
);
1616 else if (Argc
>= 2 && _stricmp(Argv
[1], "attach") == 0)
1620 KdbpPrint("thread attach: thread id argument required!\n");
1624 ul
= strtoul(Argv
[2], &pend
, 0);
1625 if (Argv
[2] == pend
)
1627 KdbpPrint("thread attach: '%s' is not a valid thread id!\n", Argv
[2]);
1631 if (!KdbpAttachToThread((PVOID
)ul
))
1636 KdbpPrint("Attached to thread 0x%08x.\n", ul
);
1640 Thread
= KdbCurrentThread
;
1644 ul
= strtoul(Argv
[1], &pend
, 0);
1645 if (Argv
[1] == pend
)
1647 KdbpPrint("thread: '%s' is not a valid thread id!\n", Argv
[1]);
1651 if (!NT_SUCCESS(PsLookupThreadByThreadId((PVOID
)ul
, &Thread
)))
1653 KdbpPrint("thread: Invalid thread id!\n");
1657 /* Remember our reference */
1658 ReferencedThread
= TRUE
;
1661 if (Thread
->Tcb
.State
< (DeferredReady
+ 1))
1662 State
= ThreadStateToString
[Thread
->Tcb
.State
];
1668 " State: %s (0x%x)\n"
1670 " Affinity: 0x%08x\n"
1671 " Initial Stack: 0x%08x\n"
1672 " Stack Limit: 0x%08x\n"
1673 " Stack Base: 0x%08x\n"
1674 " Kernel Stack: 0x%08x\n"
1675 " Trap Frame: 0x%08x\n"
1676 " NPX State: %s (0x%x)\n",
1677 (Argc
< 2) ? "Current Thread:\n" : "",
1678 Thread
->Cid
.UniqueThread
,
1679 State
, Thread
->Tcb
.State
,
1680 Thread
->Tcb
.Priority
,
1681 Thread
->Tcb
.Affinity
,
1682 Thread
->Tcb
.InitialStack
,
1683 Thread
->Tcb
.StackLimit
,
1684 Thread
->Tcb
.StackBase
,
1685 Thread
->Tcb
.KernelStack
,
1686 Thread
->Tcb
.TrapFrame
,
1687 NPX_STATE_TO_STRING(Thread
->Tcb
.NpxState
), Thread
->Tcb
.NpxState
);
1689 /* Release our reference if we had one */
1690 if (ReferencedThread
)
1691 ObDereferenceObject(Thread
);
1697 /*!\brief Lists processes or switches to another process context.
1706 BOOLEAN ReferencedProcess
= FALSE
;
1707 PCHAR State
, pend
, str1
, str2
;
1709 extern LIST_ENTRY PsActiveProcessHead
;
1711 if (Argc
>= 2 && _stricmp(Argv
[1], "list") == 0)
1713 Entry
= PsActiveProcessHead
.Flink
;
1714 if (!Entry
|| Entry
== &PsActiveProcessHead
)
1716 KdbpPrint("No processes in the system!\n");
1720 KdbpPrint(" PID State Filename\n");
1723 Process
= CONTAINING_RECORD(Entry
, EPROCESS
, ActiveProcessLinks
);
1725 if (Process
== KdbCurrentProcess
)
1736 State
= ((Process
->Pcb
.State
== ProcessInMemory
) ? "In Memory" :
1737 ((Process
->Pcb
.State
== ProcessOutOfMemory
) ? "Out of Memory" : "In Transition"));
1739 KdbpPrint(" %s0x%08x %-10s %s%s\n",
1741 Process
->UniqueProcessId
,
1743 Process
->ImageFileName
,
1746 Entry
= Entry
->Flink
;
1748 while(Entry
!= &PsActiveProcessHead
);
1750 else if (Argc
>= 2 && _stricmp(Argv
[1], "attach") == 0)
1754 KdbpPrint("process attach: process id argument required!\n");
1758 ul
= strtoul(Argv
[2], &pend
, 0);
1759 if (Argv
[2] == pend
)
1761 KdbpPrint("process attach: '%s' is not a valid process id!\n", Argv
[2]);
1765 if (!KdbpAttachToProcess((PVOID
)ul
))
1770 KdbpPrint("Attached to process 0x%08x, thread 0x%08x.\n", (ULONG
)ul
,
1771 (ULONG
)KdbCurrentThread
->Cid
.UniqueThread
);
1775 Process
= KdbCurrentProcess
;
1779 ul
= strtoul(Argv
[1], &pend
, 0);
1780 if (Argv
[1] == pend
)
1782 KdbpPrint("proc: '%s' is not a valid process id!\n", Argv
[1]);
1786 if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID
)ul
, &Process
)))
1788 KdbpPrint("proc: Invalid process id!\n");
1792 /* Remember our reference */
1793 ReferencedProcess
= TRUE
;
1796 State
= ((Process
->Pcb
.State
== ProcessInMemory
) ? "In Memory" :
1797 ((Process
->Pcb
.State
== ProcessOutOfMemory
) ? "Out of Memory" : "In Transition"));
1800 " State: %s (0x%x)\n"
1801 " Image Filename: %s\n",
1802 (Argc
< 2) ? "Current process:\n" : "",
1803 Process
->UniqueProcessId
,
1804 State
, Process
->Pcb
.State
,
1805 Process
->ImageFileName
);
1807 /* Release our reference, if any */
1808 if (ReferencedProcess
)
1809 ObDereferenceObject(Process
);
1815 /*!\brief Lists loaded modules or the one containing the specified address.
1822 ULONGLONG Result
= 0;
1824 PLDR_DATA_TABLE_ENTRY LdrEntry
;
1825 BOOLEAN DisplayOnlyOneModule
= FALSE
;
1830 /* Put the arguments back together */
1833 Argv
[Argc
][strlen(Argv
[Argc
])] = ' ';
1835 /* Evaluate the expression */
1836 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
1841 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
1842 KdbpPrint("%s: Warning: Address %I64x is beeing truncated\n", Argv
[0],Result
);
1844 Address
= (ULONG_PTR
)Result
;
1846 if (!KdbpSymFindModule((PVOID
)Address
, NULL
, -1, &LdrEntry
))
1848 KdbpPrint("No module containing address 0x%p found!\n", Address
);
1852 DisplayOnlyOneModule
= TRUE
;
1856 if (!KdbpSymFindModule(NULL
, NULL
, 0, &LdrEntry
))
1858 ULONG_PTR ntoskrnlBase
= ((ULONG_PTR
)KdbpCmdMod
) & 0xfff00000;
1859 KdbpPrint(" Base Size Name\n");
1860 KdbpPrint(" %08x %08x %s\n", ntoskrnlBase
, 0, "ntoskrnl.exe");
1867 KdbpPrint(" Base Size Name\n");
1870 KdbpPrint(" %08x %08x %wZ\n", LdrEntry
->DllBase
, LdrEntry
->SizeOfImage
, &LdrEntry
->BaseDllName
);
1872 if(DisplayOnlyOneModule
|| !KdbpSymFindModule(NULL
, NULL
, i
++, &LdrEntry
))
1879 /*!\brief Displays GDT, LDT or IDTd.
1896 if (Argv
[0][0] == 'i')
1903 KdbpPrint("Interrupt descriptor table is empty.\n");
1907 KdbpPrint("IDT Base: 0x%08x Limit: 0x%04x\n", Reg
.Base
, Reg
.Limit
);
1908 KdbpPrint(" Idx Type Seg. Sel. Offset DPL\n");
1910 for (i
= 0; (i
+ sizeof(SegDesc
) - 1) <= Reg
.Limit
; i
+= 8)
1912 if (!NT_SUCCESS(KdbpSafeReadMemory(SegDesc
, (PVOID
)(Reg
.Base
+ i
), sizeof(SegDesc
))))
1914 KdbpPrint("Couldn't access memory at 0x%08x!\n", Reg
.Base
+ i
);
1918 Dpl
= ((SegDesc
[1] >> 13) & 3);
1919 if ((SegDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
1920 SegType
= "TASKGATE";
1921 else if ((SegDesc
[1] & 0x1fe0) == 0x0e00) /* 32 bit Interrupt gate */
1922 SegType
= "INTGATE32";
1923 else if ((SegDesc
[1] & 0x1fe0) == 0x0600) /* 16 bit Interrupt gate */
1924 SegType
= "INTGATE16";
1925 else if ((SegDesc
[1] & 0x1fe0) == 0x0f00) /* 32 bit Trap gate */
1926 SegType
= "TRAPGATE32";
1927 else if ((SegDesc
[1] & 0x1fe0) == 0x0700) /* 16 bit Trap gate */
1928 SegType
= "TRAPGATE16";
1930 SegType
= "UNKNOWN";
1932 if ((SegDesc
[1] & (1 << 15)) == 0) /* not present */
1934 KdbpPrint(" %03d %-10s [NP] [NP] %02d\n",
1935 i
/ 8, SegType
, Dpl
);
1937 else if ((SegDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
1939 SegSel
= SegDesc
[0] >> 16;
1940 KdbpPrint(" %03d %-10s 0x%04x %02d\n",
1941 i
/ 8, SegType
, SegSel
, Dpl
);
1945 SegSel
= SegDesc
[0] >> 16;
1946 SegBase
= (SegDesc
[1] & 0xffff0000) | (SegDesc
[0] & 0x0000ffff);
1947 KdbpPrint(" %03d %-10s 0x%04x 0x%08x %02d\n",
1948 i
/ 8, SegType
, SegSel
, SegBase
, Dpl
);
1956 if (Argv
[0][0] == 'g')
1959 Ke386GetGlobalDescriptorTable(&Reg
.Limit
);
1964 ASSERT(Argv
[0][0] == 'l');
1967 Reg
.Limit
= Ke386GetLocalDescriptorTable();
1975 KdbpPrint("%s descriptor table is empty.\n",
1976 Argv
[0][0] == 'g' ? "Global" : "Local");
1980 KdbpPrint("%cDT Base: 0x%08x Limit: 0x%04x\n",
1981 Argv
[0][0] == 'g' ? 'G' : 'L', Reg
.Base
, Reg
.Limit
);
1982 KdbpPrint(" Idx Sel. Type Base Limit DPL Attribs\n");
1984 for (; (i
+ sizeof(SegDesc
) - 1) <= Reg
.Limit
; i
+= 8)
1986 if (!NT_SUCCESS(KdbpSafeReadMemory(SegDesc
, (PVOID
)(Reg
.Base
+ i
), sizeof(SegDesc
))))
1988 KdbpPrint("Couldn't access memory at 0x%08x!\n", Reg
.Base
+ i
);
1992 Dpl
= ((SegDesc
[1] >> 13) & 3);
1993 Type
= ((SegDesc
[1] >> 8) & 0xf);
1995 SegBase
= SegDesc
[0] >> 16;
1996 SegBase
|= (SegDesc
[1] & 0xff) << 16;
1997 SegBase
|= SegDesc
[1] & 0xff000000;
1998 SegLimit
= SegDesc
[0] & 0x0000ffff;
1999 SegLimit
|= (SegDesc
[1] >> 16) & 0xf;
2001 if ((SegDesc
[1] & (1 << 23)) != 0)
2011 if ((SegDesc
[1] & (1 << 12)) == 0) /* System segment */
2015 case 1: SegType
= "TSS16(Avl)"; break;
2016 case 2: SegType
= "LDT"; break;
2017 case 3: SegType
= "TSS16(Busy)"; break;
2018 case 4: SegType
= "CALLGATE16"; break;
2019 case 5: SegType
= "TASKGATE"; break;
2020 case 6: SegType
= "INTGATE16"; break;
2021 case 7: SegType
= "TRAPGATE16"; break;
2022 case 9: SegType
= "TSS32(Avl)"; break;
2023 case 11: SegType
= "TSS32(Busy)"; break;
2024 case 12: SegType
= "CALLGATE32"; break;
2025 case 14: SegType
= "INTGATE32"; break;
2026 case 15: SegType
= "INTGATE32"; break;
2027 default: SegType
= "UNKNOWN"; break;
2030 if (!(Type
>= 1 && Type
<= 3) &&
2031 Type
!= 9 && Type
!= 11)
2037 else if ((SegDesc
[1] & (1 << 11)) == 0) /* Data segment */
2039 if ((SegDesc
[1] & (1 << 22)) != 0)
2044 else /* Code segment */
2046 if ((SegDesc
[1] & (1 << 22)) != 0)
2052 if ((SegDesc
[1] & (1 << 15)) == 0) /* not present */
2054 KdbpPrint(" %03d 0x%04x %-11s [NP] [NP] %02d NP\n",
2055 i
/ 8, i
| Dpl
| ul
, SegType
, Dpl
);
2059 KdbpPrint(" %03d 0x%04x %-11s 0x%08x 0x%08x %02d ",
2060 i
/ 8, i
| Dpl
| ul
, SegType
, SegBase
, SegLimit
, Dpl
);
2062 if ((SegDesc
[1] & (1 << 12)) == 0) /* System segment */
2064 /* FIXME: Display system segment */
2066 else if ((SegDesc
[1] & (1 << 11)) == 0) /* Data segment */
2068 if ((SegDesc
[1] & (1 << 10)) != 0) /* Expand-down */
2071 KdbpPrint((SegDesc
[1] & (1 << 9)) ? " R/W" : " R");
2073 if ((SegDesc
[1] & (1 << 8)) != 0)
2076 else /* Code segment */
2078 if ((SegDesc
[1] & (1 << 10)) != 0) /* Conforming */
2081 KdbpPrint((SegDesc
[1] & (1 << 9)) ? " R/X" : " X");
2083 if ((SegDesc
[1] & (1 << 8)) != 0)
2087 if ((SegDesc
[1] & (1 << 20)) != 0)
2098 /*!\brief Displays the KPCR
2105 PKIPCR Pcr
= (PKIPCR
)KeGetPcr();
2107 KdbpPrint("Current PCR is at 0x%08x.\n", (INT
)Pcr
);
2108 KdbpPrint(" Tib.ExceptionList: 0x%08x\n"
2109 " Tib.StackBase: 0x%08x\n"
2110 " Tib.StackLimit: 0x%08x\n"
2111 " Tib.SubSystemTib: 0x%08x\n"
2112 " Tib.FiberData/Version: 0x%08x\n"
2113 " Tib.ArbitraryUserPointer: 0x%08x\n"
2114 " Tib.Self: 0x%08x\n"
2119 " IrrActive: 0x%08x\n"
2121 " KdVersionBlock: 0x%08x\n"
2125 " MajorVersion: 0x%04x\n"
2126 " MinorVersion: 0x%04x\n"
2127 " SetMember: 0x%08x\n"
2128 " StallScaleFactor: 0x%08x\n"
2130 " L2CacheAssociativity: 0x%02x\n"
2131 " VdmAlert: 0x%08x\n"
2132 " L2CacheSize: 0x%08x\n"
2133 " InterruptMode: 0x%08x\n",
2134 Pcr
->NtTib
.ExceptionList
, Pcr
->NtTib
.StackBase
, Pcr
->NtTib
.StackLimit
,
2135 Pcr
->NtTib
.SubSystemTib
, Pcr
->NtTib
.FiberData
, Pcr
->NtTib
.ArbitraryUserPointer
,
2136 Pcr
->NtTib
.Self
, Pcr
->Self
, Pcr
->Prcb
, Pcr
->Irql
, Pcr
->IRR
, Pcr
->IrrActive
,
2137 Pcr
->IDR
, Pcr
->KdVersionBlock
, Pcr
->IDT
, Pcr
->GDT
, Pcr
->TSS
,
2138 Pcr
->MajorVersion
, Pcr
->MinorVersion
, Pcr
->SetMember
, Pcr
->StallScaleFactor
,
2139 Pcr
->Number
, Pcr
->SecondLevelCacheAssociativity
,
2140 Pcr
->VdmAlert
, Pcr
->SecondLevelCacheSize
, Pcr
->InterruptMode
);
2145 /*!\brief Displays the TSS
2152 KTSS
*Tss
= KeGetPcr()->TSS
;
2154 KdbpPrint("Current TSS is at 0x%08x.\n", (INT
)Tss
);
2155 KdbpPrint(" Eip: 0x%08x\n"
2162 " IoMapBase: 0x%04x\n",
2163 Tss
->Eip
, Tss
->Es
, Tss
->Cs
, Tss
->Ds
, Tss
->Fs
, Tss
->Gs
, Tss
->IoMapBase
);
2168 /*!\brief Bugchecks the system.
2175 /* Set the flag and quit looping */
2176 KdbpBugCheckRequested
= TRUE
;
2186 /* Reboot immediately (we do not return) */
2187 HalReturnToFirmware(HalRebootRoutine
);
2195 IN ULONG BufLength
);
2197 /*!\brief Display debug messages on screen, with paging.
2199 * Keys for per-page view: Home, End, PageUp, Arrow Up, PageDown,
2200 * all others are as PageDown.
2209 KdbpIsInDmesgMode
= TRUE
; /* Toggle logging flag */
2210 if (!KdpDmesgBuffer
)
2212 KdbpPrint("Dmesg: error, buffer is not allocated! /DEBUGPORT=SCREEN kernel param required for dmesg.\n");
2216 KdbpPrint("*** Dmesg *** TotalWritten=%lu, BufferSize=%lu, CurrentPosition=%lu\n",
2217 KdbDmesgTotalWritten
, KdpDmesgBufferSize
, KdpDmesgCurrentPosition
);
2219 // Pass data to the pager:
2220 end
= KdpDmesgCurrentPosition
;
2221 beg
= (end
+ KdpDmesgFreeBytes
) % KdpDmesgBufferSize
;
2223 // no roll-overs, and overwritten=lost bytes
2224 if (KdbDmesgTotalWritten
<= KdpDmesgBufferSize
)
2226 // show buffer (KdpDmesgBuffer + beg, num)
2227 KdbpPager(KdpDmesgBuffer
, KdpDmesgCurrentPosition
);
2231 // show 2 buffers: (KdpDmesgBuffer + beg, KdpDmesgBufferSize - beg)
2232 // and: (KdpDmesgBuffer, end)
2233 KdbpPager(KdpDmesgBuffer
+ beg
, KdpDmesgBufferSize
- beg
);
2234 KdbpPrint("*** Dmesg: buffer rollup ***\n");
2235 KdbpPager(KdpDmesgBuffer
, end
);
2237 KdbpPrint("*** Dmesg: end of output ***\n");
2239 KdbpIsInDmesgMode
= FALSE
; /* Toggle logging flag */
2244 /*!\brief Sets or displays a config variables value.
2254 KDB_ENTER_CONDITION ConditionFirst
= KdbDoNotEnter
;
2255 KDB_ENTER_CONDITION ConditionLast
= KdbDoNotEnter
;
2257 static const PCHAR ExceptionNames
[21] =
2259 "ZERODEVIDE", "DEBUGTRAP", "NMI", "INT3", "OVERFLOW", "BOUND", "INVALIDOP",
2260 "NOMATHCOP", "DOUBLEFAULT", "RESERVED(9)", "INVALIDTSS", "SEGMENTNOTPRESENT",
2261 "STACKFAULT", "GPF", "PAGEFAULT", "RESERVED(15)", "MATHFAULT", "ALIGNMENTCHECK",
2262 "MACHINECHECK", "SIMDFAULT", "OTHERS"
2267 KdbpPrint("Available settings:\n");
2268 KdbpPrint(" syntax [intel|at&t]\n");
2269 KdbpPrint(" condition [exception|*] [first|last] [never|always|kmode|umode]\n");
2270 KdbpPrint(" break_on_module_load [true|false]\n");
2272 else if (strcmp(Argv
[1], "syntax") == 0)
2276 KdbpPrint("syntax = %s\n", KdbUseIntelSyntax
? "intel" : "at&t");
2280 if (_stricmp(Argv
[2], "intel") == 0)
2281 KdbUseIntelSyntax
= TRUE
;
2282 else if (_stricmp(Argv
[2], "at&t") == 0)
2283 KdbUseIntelSyntax
= FALSE
;
2285 KdbpPrint("Unknown syntax '%s'.\n", Argv
[2]);
2288 else if (strcmp(Argv
[1], "condition") == 0)
2292 KdbpPrint("Conditions: (First) (Last)\n");
2293 for (l
= 0; l
< RTL_NUMBER_OF(ExceptionNames
) - 1; l
++)
2295 if (!ExceptionNames
[l
])
2298 if (!KdbpGetEnterCondition(l
, TRUE
, &ConditionFirst
))
2301 if (!KdbpGetEnterCondition(l
, FALSE
, &ConditionLast
))
2304 KdbpPrint(" #%02d %-20s %-8s %-8s\n", l
, ExceptionNames
[l
],
2305 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
2306 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
2309 ASSERT(l
== (RTL_NUMBER_OF(ExceptionNames
) - 1));
2310 KdbpPrint(" %-20s %-8s %-8s\n", ExceptionNames
[l
],
2311 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
2312 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
2316 if (Argc
>= 5 && strcmp(Argv
[2], "*") == 0) /* Allow * only when setting condition */
2322 l
= strtoul(Argv
[2], &pend
, 0);
2324 if (Argv
[2] == pend
)
2326 for (l
= 0; l
< RTL_NUMBER_OF(ExceptionNames
); l
++)
2328 if (!ExceptionNames
[l
])
2331 if (_stricmp(ExceptionNames
[l
], Argv
[2]) == 0)
2336 if (l
>= RTL_NUMBER_OF(ExceptionNames
))
2338 KdbpPrint("Unknown exception '%s'.\n", Argv
[2]);
2345 if (_stricmp(Argv
[3], "first") == 0)
2347 else if (_stricmp(Argv
[3], "last") == 0)
2351 KdbpPrint("set condition: second argument must be 'first' or 'last'\n");
2355 if (_stricmp(Argv
[4], "never") == 0)
2356 ConditionFirst
= KdbDoNotEnter
;
2357 else if (_stricmp(Argv
[4], "always") == 0)
2358 ConditionFirst
= KdbEnterAlways
;
2359 else if (_stricmp(Argv
[4], "umode") == 0)
2360 ConditionFirst
= KdbEnterFromUmode
;
2361 else if (_stricmp(Argv
[4], "kmode") == 0)
2362 ConditionFirst
= KdbEnterFromKmode
;
2365 KdbpPrint("set condition: third argument must be 'never', 'always', 'umode' or 'kmode'\n");
2369 if (!KdbpSetEnterCondition(l
, First
, ConditionFirst
))
2372 KdbpPrint("Couldn't change condition for exception #%02d\n", l
);
2374 KdbpPrint("Couldn't change condition for all exceptions\n", l
);
2377 else /* Argc >= 3 */
2379 if (!KdbpGetEnterCondition(l
, TRUE
, &ConditionFirst
))
2382 if (!KdbpGetEnterCondition(l
, FALSE
, &ConditionLast
))
2385 if (l
< (RTL_NUMBER_OF(ExceptionNames
) - 1))
2387 KdbpPrint("Condition for exception #%02d (%s): FirstChance %s LastChance %s\n",
2388 l
, ExceptionNames
[l
],
2389 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
2390 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
2394 KdbpPrint("Condition for all other exceptions: FirstChance %s LastChance %s\n",
2395 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
2396 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
2401 else if (strcmp(Argv
[1], "break_on_module_load") == 0)
2404 KdbpPrint("break_on_module_load = %s\n", KdbBreakOnModuleLoad
? "enabled" : "disabled");
2407 if (_stricmp(Argv
[2], "enable") == 0 || _stricmp(Argv
[2], "enabled") == 0 || _stricmp(Argv
[2], "true") == 0)
2408 KdbBreakOnModuleLoad
= TRUE
;
2409 else if (_stricmp(Argv
[2], "disable") == 0 || _stricmp(Argv
[2], "disabled") == 0 || _stricmp(Argv
[2], "false") == 0)
2410 KdbBreakOnModuleLoad
= FALSE
;
2412 KdbpPrint("Unknown setting '%s'.\n", Argv
[2]);
2417 KdbpPrint("Unknown setting '%s'.\n", Argv
[1]);
2423 /*!\brief Displays help screen.
2432 KdbpPrint("Kernel debugger commands:\n");
2433 for (i
= 0; i
< RTL_NUMBER_OF(KdbDebuggerCommands
); i
++)
2435 if (!KdbDebuggerCommands
[i
].Syntax
) /* Command group */
2440 KdbpPrint("\x1b[7m* %s:\x1b[0m\n", KdbDebuggerCommands
[i
].Help
);
2444 KdbpPrint(" %-20s - %s\n",
2445 KdbDebuggerCommands
[i
].Syntax
,
2446 KdbDebuggerCommands
[i
].Help
);
2452 /*!\brief Prints the given string with printf-like formatting.
2454 * \param Format Format of the string/arguments.
2455 * \param ... Variable number of arguments matching the format specified in \a Format.
2457 * \note Doesn't correctly handle \\t and terminal escape sequences when calculating the
2458 * number of lines required to print a single line from the Buffer in the terminal.
2459 * Prints maximum 4096 chars, because of its buffer size.
2466 static CHAR Buffer
[4096];
2467 static BOOLEAN TerminalInitialized
= FALSE
;
2468 static BOOLEAN TerminalConnected
= FALSE
;
2469 static BOOLEAN TerminalReportsSize
= TRUE
;
2474 LONG RowsPrintedByTerminal
;
2478 /* Check if the user has aborted output of the current command */
2479 if (KdbOutputAborted
)
2482 /* Initialize the terminal */
2483 if (!TerminalInitialized
)
2485 DbgPrint("\x1b[7h"); /* Enable linewrap */
2487 /* Query terminal type */
2488 /*DbgPrint("\x1b[Z");*/
2491 TerminalInitialized
= TRUE
;
2493 KeStallExecutionProcessor(100000);
2497 c
= KdbpTryGetCharSerial(5000);
2501 Buffer
[Length
++] = c
;
2502 if (Length
>= (sizeof (Buffer
) - 1))
2506 Buffer
[Length
] = '\0';
2508 TerminalConnected
= TRUE
;
2511 /* Get number of rows and columns in terminal */
2512 if ((KdbNumberOfRowsTerminal
< 0) || (KdbNumberOfColsTerminal
< 0) ||
2513 (KdbNumberOfRowsPrinted
) == 0) /* Refresh terminal size each time when number of rows printed is 0 */
2515 if ((KdbDebugState
& KD_DEBUG_KDSERIAL
) && TerminalConnected
&& TerminalReportsSize
)
2517 /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
2518 TerminalReportsSize
= FALSE
;
2519 KeStallExecutionProcessor(100000);
2520 DbgPrint("\x1b[18t");
2521 c
= KdbpTryGetCharSerial(5000);
2525 c
= KdbpTryGetCharSerial(5000);
2532 c
= KdbpTryGetCharSerial(5000);
2536 Buffer
[Length
++] = c
;
2537 if (isalpha(c
) || Length
>= (sizeof (Buffer
) - 1))
2541 Buffer
[Length
] = '\0';
2542 if (Buffer
[0] == '8' && Buffer
[1] == ';')
2544 for (i
= 2; (i
< Length
) && (Buffer
[i
] != ';'); i
++);
2546 if (Buffer
[i
] == ';')
2550 /* Number of rows is now at Buffer + 2 and number of cols at Buffer + i */
2551 KdbNumberOfRowsTerminal
= strtoul(Buffer
+ 2, NULL
, 0);
2552 KdbNumberOfColsTerminal
= strtoul(Buffer
+ i
, NULL
, 0);
2553 TerminalReportsSize
= TRUE
;
2557 /* Clear further characters */
2558 while ((c
= KdbpTryGetCharSerial(5000)) != -1);
2562 if (KdbNumberOfRowsTerminal
<= 0)
2564 /* Set number of rows to the default. */
2565 KdbNumberOfRowsTerminal
= 23; //24; //Mna.: 23 for SCREEN debugport
2567 else if (KdbNumberOfColsTerminal
<= 0)
2569 /* Set number of cols to the default. */
2570 KdbNumberOfColsTerminal
= 75; //80; //Mna.: 75 for SCREEN debugport
2574 /* Get the string */
2575 va_start(ap
, Format
);
2576 Length
= _vsnprintf(Buffer
, sizeof (Buffer
) - 1, Format
, ap
);
2577 Buffer
[Length
] = '\0';
2581 while (p
[0] != '\0')
2583 i
= strcspn(p
, "\n");
2585 /* Calculate the number of lines which will be printed in the terminal
2586 * when outputting the current line
2589 RowsPrintedByTerminal
= (i
+ KdbNumberOfColsPrinted
- 1) / KdbNumberOfColsTerminal
;
2591 RowsPrintedByTerminal
= 0;
2594 RowsPrintedByTerminal
++;
2596 /*DbgPrint("!%d!%d!%d!%d!", KdbNumberOfRowsPrinted, KdbNumberOfColsPrinted, i, RowsPrintedByTerminal);*/
2598 /* Display a prompt if we printed one screen full of text */
2599 if (KdbNumberOfRowsTerminal
> 0 &&
2600 (LONG
)(KdbNumberOfRowsPrinted
+ RowsPrintedByTerminal
) >= KdbNumberOfRowsTerminal
)
2602 KdbRepeatLastCommand
= FALSE
;
2604 if (KdbNumberOfColsPrinted
> 0)
2607 DbgPrint("--- Press q to abort, any other key to continue ---");
2608 RowsPrintedByTerminal
++; /* added by Mna. */
2610 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
2611 c
= KdbpGetCharSerial();
2613 c
= KdbpGetCharKeyboard(&ScanCode
);
2617 /* Try to read '\n' which might follow '\r' - if \n is not received here
2618 * it will be interpreted as "return" when the next command should be read.
2620 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
2621 c
= KdbpTryGetCharSerial(5);
2623 c
= KdbpTryGetCharKeyboard(&ScanCode
, 5);
2629 KdbOutputAborted
= TRUE
;
2633 KdbNumberOfRowsPrinted
= 0;
2634 KdbNumberOfColsPrinted
= 0;
2637 /* Insert a NUL after the line and print only the current line. */
2638 if (p
[i
] == '\n' && p
[i
+ 1] != '\0')
2648 /* Remove escape sequences from the line if there's no terminal connected */
2649 if (!TerminalConnected
)
2651 while ((p2
= strrchr(p
, '\x1b'))) /* Look for escape character */
2656 while (!isalpha(p2
[j
++]));
2671 /* Set p to the start of the next line and
2672 * remember the number of rows/cols printed
2678 KdbNumberOfColsPrinted
= 0;
2682 ASSERT(p
[0] == '\0');
2683 KdbNumberOfColsPrinted
+= i
;
2686 KdbNumberOfRowsPrinted
+= RowsPrintedByTerminal
;
2690 /** memrchr(), explicitly defined, since was absent in MinGW of RosBE. */
2693 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
2696 memrchr(const void *s
, int c
, size_t n
)
2698 const unsigned char *cp
;
2702 cp
= (unsigned char *)s
+ n
;
2705 if (*(--cp
) == (unsigned char)c
)
2712 /*!\brief Calculate pointer position for N lines upper of current position.
2714 * \param Buffer Characters buffer to operate on.
2715 * \param BufLength Buffer size.
2717 * \note Calculate pointer position for N lines upper of current displaying
2718 * position within the given buffer.
2720 * Used by KdbpPager().
2721 * Now N lines count is hardcoded to KdbNumberOfRowsTerminal.
2724 CountOnePageUp(PCHAR Buffer
, ULONG BufLength
, PCHAR pCurPos
)
2727 // p0 is initial guess of Page Start
2728 ULONG p0len
= KdbNumberOfRowsTerminal
* KdbNumberOfColsTerminal
;
2729 PCHAR p0
= pCurPos
- p0len
;
2730 PCHAR prev_p
= p0
, p1
;
2733 if (pCurPos
< Buffer
)
2735 ASSERT(pCurPos
<= Buffer
+ BufLength
);
2737 p
= memrchr(p0
, '\n', p0len
);
2740 for (j
= KdbNumberOfRowsTerminal
; j
--; )
2743 p1
= memrchr(p0
, '\n', p
-p0
);
2753 linesCnt
= (KdbNumberOfColsTerminal
+prev_p
-p
-2) / KdbNumberOfColsTerminal
;
2763 /*!\brief Prints the given string with, page by page.
2765 * \param Buffer Characters buffer to print.
2766 * \param BufferLen Buffer size.
2768 * \note Doesn't correctly handle \\t and terminal escape sequences when calculating the
2769 * number of lines required to print a single line from the Buffer in the terminal.
2770 * Maximum length of buffer is limited only by memory size.
2772 * Note: BufLength should be greater then (KdbNumberOfRowsTerminal * KdbNumberOfColsTerminal).
2780 static CHAR InBuffer
[4096];
2781 static BOOLEAN TerminalInitialized
= FALSE
;
2782 static BOOLEAN TerminalConnected
= FALSE
;
2783 static BOOLEAN TerminalReportsSize
= TRUE
;
2788 LONG RowsPrintedByTerminal
;
2794 /* Check if the user has aborted output of the current command */
2795 if (KdbOutputAborted
)
2798 /* Initialize the terminal */
2799 if (!TerminalInitialized
)
2801 DbgPrint("\x1b[7h"); /* Enable linewrap */
2803 /* Query terminal type */
2804 /*DbgPrint("\x1b[Z");*/
2807 TerminalInitialized
= TRUE
;
2809 KeStallExecutionProcessor(100000);
2813 c
= KdbpTryGetCharSerial(5000);
2817 InBuffer
[Length
++] = c
;
2818 if (Length
>= (sizeof (InBuffer
) - 1))
2822 InBuffer
[Length
] = '\0';
2824 TerminalConnected
= TRUE
;
2827 /* Get number of rows and columns in terminal */
2828 if ((KdbNumberOfRowsTerminal
< 0) || (KdbNumberOfColsTerminal
< 0) ||
2829 (KdbNumberOfRowsPrinted
) == 0) /* Refresh terminal size each time when number of rows printed is 0 */
2831 if ((KdbDebugState
& KD_DEBUG_KDSERIAL
) && TerminalConnected
&& TerminalReportsSize
)
2833 /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
2834 TerminalReportsSize
= FALSE
;
2835 KeStallExecutionProcessor(100000);
2836 DbgPrint("\x1b[18t");
2837 c
= KdbpTryGetCharSerial(5000);
2841 c
= KdbpTryGetCharSerial(5000);
2848 c
= KdbpTryGetCharSerial(5000);
2852 InBuffer
[Length
++] = c
;
2853 if (isalpha(c
) || Length
>= (sizeof (InBuffer
) - 1))
2857 InBuffer
[Length
] = '\0';
2858 if (InBuffer
[0] == '8' && InBuffer
[1] == ';')
2860 for (i
= 2; (i
< Length
) && (InBuffer
[i
] != ';'); i
++);
2862 if (Buffer
[i
] == ';')
2866 /* Number of rows is now at Buffer + 2 and number of cols at Buffer + i */
2867 KdbNumberOfRowsTerminal
= strtoul(InBuffer
+ 2, NULL
, 0);
2868 KdbNumberOfColsTerminal
= strtoul(InBuffer
+ i
, NULL
, 0);
2869 TerminalReportsSize
= TRUE
;
2873 /* Clear further characters */
2874 while ((c
= KdbpTryGetCharSerial(5000)) != -1);
2878 if (KdbNumberOfRowsTerminal
<= 0)
2880 /* Set number of rows to the default. */
2881 KdbNumberOfRowsTerminal
= 24;
2883 else if (KdbNumberOfColsTerminal
<= 0)
2885 /* Set number of cols to the default. */
2886 KdbNumberOfColsTerminal
= 80;
2890 /* Get the string */
2893 while (p
[0] != '\0')
2895 if ( p
> Buffer
+BufLength
)
2897 DbgPrint("Dmesg: error, p > Buffer+BufLength,d=%d", p
- (Buffer
+BufLength
));
2900 i
= strcspn(p
, "\n");
2902 // Are we out of buffer?
2903 if (p
+ i
> Buffer
+ BufLength
)
2904 // Leaving pager function:
2907 /* Calculate the number of lines which will be printed in the terminal
2908 * when outputting the current line
2911 RowsPrintedByTerminal
= (i
+ KdbNumberOfColsPrinted
- 1) / KdbNumberOfColsTerminal
;
2913 RowsPrintedByTerminal
= 0;
2916 RowsPrintedByTerminal
++;
2918 /*DbgPrint("!%d!%d!%d!%d!", KdbNumberOfRowsPrinted, KdbNumberOfColsPrinted, i, RowsPrintedByTerminal);*/
2920 /* Display a prompt if we printed one screen full of text */
2921 if (KdbNumberOfRowsTerminal
> 0 &&
2922 (LONG
)(KdbNumberOfRowsPrinted
+ RowsPrintedByTerminal
) >= KdbNumberOfRowsTerminal
)
2924 KdbRepeatLastCommand
= FALSE
;
2926 if (KdbNumberOfColsPrinted
> 0)
2929 DbgPrint("--- Press q to abort, e/End,h/Home,u/PgUp, other key/PgDn ---");
2930 RowsPrintedByTerminal
++;
2932 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
2933 c
= KdbpGetCharSerial();
2935 c
= KdbpGetCharKeyboard(&ScanCode
);
2939 /* Try to read '\n' which might follow '\r' - if \n is not received here
2940 * it will be interpreted as "return" when the next command should be read.
2942 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
2943 c
= KdbpTryGetCharSerial(5);
2945 c
= KdbpTryGetCharKeyboard(&ScanCode
, 5);
2948 //DbgPrint("\n"); //Consize version: don't show pressed key
2949 DbgPrint(" '%c'/scan=%04x\n", c
, ScanCode
); // Shows pressed key
2953 KdbOutputAborted
= TRUE
;
2956 if ( ScanCode
== KEYSC_END
|| c
=='e')
2958 PCHAR pBufEnd
= Buffer
+ BufLength
;
2959 p
= CountOnePageUp(Buffer
, BufLength
, pBufEnd
);
2960 i
= strcspn(p
, "\n");
2962 else if (ScanCode
== KEYSC_PAGEUP
|| c
=='u')
2964 p
= CountOnePageUp(Buffer
, BufLength
, p
);
2965 i
= strcspn(p
, "\n");
2967 else if (ScanCode
== KEYSC_HOME
|| c
=='h')
2970 i
= strcspn(p
, "\n");
2972 else if (ScanCode
== KEYSC_ARROWUP
)
2974 p
= CountOnePageUp(Buffer
, BufLength
, p
);
2975 i
= strcspn(p
, "\n");
2978 KdbNumberOfRowsPrinted
= 0;
2979 KdbNumberOfColsPrinted
= 0;
2982 /* Insert a NUL after the line and print only the current line. */
2983 if (p
[i
] == '\n' && p
[i
+ 1] != '\0')
2993 /* Remove escape sequences from the line if there's no terminal connected */
2994 if (!TerminalConnected
)
2996 while ((p2
= strrchr(p
, '\x1b'))) /* Look for escape character */
3001 while (!isalpha(p2
[j
++]));
3011 // The main printing of the current line:
3014 // restore not null char with saved:
3018 /* Set p to the start of the next line and
3019 * remember the number of rows/cols printed
3025 KdbNumberOfColsPrinted
= 0;
3029 ASSERT(p
[0] == '\0');
3030 KdbNumberOfColsPrinted
+= i
;
3033 KdbNumberOfRowsPrinted
+= RowsPrintedByTerminal
;
3037 /*!\brief Appends a command to the command history
3039 * \param Command Pointer to the command to append to the history.
3042 KdbpCommandHistoryAppend(
3045 ULONG Length1
= strlen(Command
) + 1;
3050 ASSERT(Length1
<= RTL_NUMBER_OF(KdbCommandHistoryBuffer
));
3053 (KdbCommandHistory
[KdbCommandHistoryIndex
] &&
3054 strcmp(KdbCommandHistory
[KdbCommandHistoryIndex
], Command
) == 0))
3059 /* Calculate Length1 and Length2 */
3060 Buffer
= KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
;
3061 KdbCommandHistoryBufferIndex
+= Length1
;
3062 if (KdbCommandHistoryBufferIndex
>= (LONG
)RTL_NUMBER_OF(KdbCommandHistoryBuffer
))
3064 KdbCommandHistoryBufferIndex
-= RTL_NUMBER_OF(KdbCommandHistoryBuffer
);
3065 Length2
= KdbCommandHistoryBufferIndex
;
3069 /* Remove previous commands until there is enough space to append the new command */
3070 for (i
= KdbCommandHistoryIndex
; KdbCommandHistory
[i
];)
3073 (KdbCommandHistory
[i
] >= Buffer
||
3074 KdbCommandHistory
[i
] < (KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
))) ||
3076 (KdbCommandHistory
[i
] >= Buffer
&&
3077 KdbCommandHistory
[i
] < (KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
))))
3079 KdbCommandHistory
[i
] = NULL
;
3084 i
= RTL_NUMBER_OF(KdbCommandHistory
) - 1;
3086 if (i
== KdbCommandHistoryIndex
)
3090 /* Make sure the new command history entry is free */
3091 KdbCommandHistoryIndex
++;
3092 KdbCommandHistoryIndex
%= RTL_NUMBER_OF(KdbCommandHistory
);
3093 if (KdbCommandHistory
[KdbCommandHistoryIndex
])
3095 KdbCommandHistory
[KdbCommandHistoryIndex
] = NULL
;
3098 /* Append command */
3099 KdbCommandHistory
[KdbCommandHistoryIndex
] = Buffer
;
3100 ASSERT((KdbCommandHistory
[KdbCommandHistoryIndex
] + Length1
) <= KdbCommandHistoryBuffer
+ RTL_NUMBER_OF(KdbCommandHistoryBuffer
));
3101 memcpy(KdbCommandHistory
[KdbCommandHistoryIndex
], Command
, Length1
);
3104 memcpy(KdbCommandHistoryBuffer
, Command
+ Length1
, Length2
);
3108 /*!\brief Reads a line of user-input.
3110 * \param Buffer Buffer to store the input into. Trailing newlines are removed.
3111 * \param Size Size of \a Buffer.
3113 * \note Accepts only \n newlines, \r is ignored.
3121 PCHAR Orig
= Buffer
;
3124 static CHAR LastCommand
[1024];
3125 static CHAR NextKey
= '\0';
3126 INT CmdHistIndex
= -1;
3129 EchoOn
= !((KdbDebugState
& KD_DEBUG_KDNOECHO
) != 0);
3133 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
3135 Key
= (NextKey
== '\0') ? KdbpGetCharSerial() : NextKey
;
3138 if (Key
== KEY_ESC
) /* ESC */
3140 Key
= KdbpGetCharSerial();
3143 Key
= KdbpGetCharSerial();
3148 ScanCode
= KEY_SCAN_UP
;
3151 ScanCode
= KEY_SCAN_DOWN
;
3164 Key
= (NextKey
== '\0') ? KdbpGetCharKeyboard(&ScanCode
) : NextKey
;
3168 if ((ULONG
)(Buffer
- Orig
) >= (Size
- 1))
3170 /* Buffer is full, accept only newlines */
3177 /* Read the next char - this is to throw away a \n which most clients should
3180 KeStallExecutionProcessor(100000);
3182 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
3183 NextKey
= KdbpTryGetCharSerial(5);
3185 NextKey
= KdbpTryGetCharKeyboard(&ScanCode
, 5);
3187 if (NextKey
== '\n' || NextKey
== -1) /* \n or no response at all */
3193 * Repeat the last command if the user presses enter. Reduces the
3194 * risk of RSI when single-stepping.
3198 KdbRepeatLastCommand
= TRUE
;
3200 RtlStringCbCopyA(LastCommand
, sizeof(LastCommand
), Orig
);
3202 else if (KdbRepeatLastCommand
)
3203 RtlStringCbCopyA(Buffer
, Size
, LastCommand
);
3209 else if (Key
== KEY_BS
|| Key
== KEY_DEL
)
3217 KdbpPrint("%c %c", KEY_BS
, KEY_BS
);
3219 KdbpPrint(" %c", KEY_BS
);
3222 else if (ScanCode
== KEY_SCAN_UP
)
3224 BOOLEAN Print
= TRUE
;
3226 if (CmdHistIndex
< 0)
3228 CmdHistIndex
= KdbCommandHistoryIndex
;
3232 i
= CmdHistIndex
- 1;
3235 CmdHistIndex
= RTL_NUMBER_OF(KdbCommandHistory
) - 1;
3237 if (KdbCommandHistory
[i
] && i
!= KdbCommandHistoryIndex
)
3243 if (Print
&& KdbCommandHistory
[CmdHistIndex
])
3245 while (Buffer
> Orig
)
3251 KdbpPrint("%c %c", KEY_BS
, KEY_BS
);
3253 KdbpPrint(" %c", KEY_BS
);
3256 i
= min(strlen(KdbCommandHistory
[CmdHistIndex
]), Size
- 1);
3257 memcpy(Orig
, KdbCommandHistory
[CmdHistIndex
], i
);
3260 KdbpPrint("%s", Orig
);
3263 else if (ScanCode
== KEY_SCAN_DOWN
)
3265 if (CmdHistIndex
> 0 && CmdHistIndex
!= KdbCommandHistoryIndex
)
3267 i
= CmdHistIndex
+ 1;
3268 if (i
>= (INT
)RTL_NUMBER_OF(KdbCommandHistory
))
3271 if (KdbCommandHistory
[i
])
3274 while (Buffer
> Orig
)
3280 KdbpPrint("%c %c", KEY_BS
, KEY_BS
);
3282 KdbpPrint(" %c", KEY_BS
);
3285 i
= min(strlen(KdbCommandHistory
[CmdHistIndex
]), Size
- 1);
3286 memcpy(Orig
, KdbCommandHistory
[CmdHistIndex
], i
);
3289 KdbpPrint("%s", Orig
);
3296 KdbpPrint("%c", Key
);
3307 KdbRegisterCliCallback(
3313 /* Loop all entries */
3314 for (i
= 0; i
< _countof(KdbCliCallbacks
); i
++)
3316 /* Check if deregistering was requested */
3319 /* Check if this entry is the one that was registered */
3320 if (KdbCliCallbacks
[i
] == Callback
)
3322 /* Delete it and report success */
3323 KdbCliCallbacks
[i
] = NULL
;
3329 /* Check if this entry is free */
3330 if (KdbCliCallbacks
[i
] == NULL
)
3332 /* Set it and and report success */
3333 KdbCliCallbacks
[i
] = Callback
;
3343 /*! \brief Invokes registered CLI callbacks until one of them handled the
3346 * \param Command - Command line to parse and execute if possible.
3347 * \param Argc - Number of arguments in Argv
3348 * \param Argv - Array of strings, each of them containing one argument.
3350 * \return TRUE, if the command was handled, FALSE if it was not handled.
3354 KdbpInvokeCliCallbacks(
3361 /* Loop all entries */
3362 for (i
= 0; i
< _countof(KdbCliCallbacks
); i
++)
3364 /* Check if this entry is registered */
3365 if (KdbCliCallbacks
[i
])
3367 /* Invoke the callback and check if it handled the command */
3368 if (KdbCliCallbacks
[i
](Command
, Argc
, Argv
))
3375 /* None of the callbacks handled the command */
3380 /*!\brief Parses command line and executes command if found
3382 * \param Command Command line to parse and execute if possible.
3384 * \retval TRUE Don't continue execution.
3385 * \retval FALSE Continue execution (leave KDB)
3394 // FIXME: for what do we need a 1024 characters command line and 256 tokens?
3395 static PCH Argv
[256];
3396 static CHAR OrigCommand
[1024];
3398 RtlStringCbCopyA(OrigCommand
, sizeof(OrigCommand
), Command
);
3405 while (*p
== '\t' || *p
== ' ')
3411 i
= strcspn(p
, "\t ");
3424 for (i
= 0; i
< RTL_NUMBER_OF(KdbDebuggerCommands
); i
++)
3426 if (!KdbDebuggerCommands
[i
].Name
)
3429 if (strcmp(KdbDebuggerCommands
[i
].Name
, Argv
[0]) == 0)
3431 return KdbDebuggerCommands
[i
].Fn(Argc
, Argv
);
3435 /* Now invoke the registered callbacks */
3436 if (KdbpInvokeCliCallbacks(Command
, Argc
, Argv
))
3441 KdbpPrint("Command '%s' is unknown.\n", OrigCommand
);
3445 /*!\brief KDB Main Loop.
3447 * \param EnteredOnSingleStep TRUE if KDB was entered on single step.
3451 IN BOOLEAN EnteredOnSingleStep
)
3453 static CHAR Command
[1024];
3456 if (EnteredOnSingleStep
)
3458 if (!KdbSymPrintAddress((PVOID
)KdbCurrentTrapFrame
->Tf
.Eip
, &KdbCurrentTrapFrame
->Tf
))
3460 KdbpPrint("<%x>", KdbCurrentTrapFrame
->Tf
.Eip
);
3464 if (KdbpDisassemble(KdbCurrentTrapFrame
->Tf
.Eip
, KdbUseIntelSyntax
) < 0)
3466 KdbpPrint("<INVALID>");
3471 /* Flush the input buffer */
3472 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
3474 while (KdbpTryGetCharSerial(1) != -1);
3479 while (KdbpTryGetCharKeyboard(&ScanCode
, 1) != -1);
3485 /* Reset the number of rows/cols printed */
3486 KdbNumberOfRowsPrinted
= KdbNumberOfColsPrinted
= 0;
3488 /* Print the prompt */
3489 KdbpPrint("kdb:> ");
3491 /* Read a command and remember it */
3492 KdbpReadCommand(Command
, sizeof (Command
));
3493 KdbpCommandHistoryAppend(Command
);
3495 /* Reset the number of rows/cols printed and output aborted state */
3496 KdbNumberOfRowsPrinted
= KdbNumberOfColsPrinted
= 0;
3497 KdbOutputAborted
= FALSE
;
3499 /* Call the command */
3500 Continue
= KdbpDoCommand(Command
);
3501 KdbOutputAborted
= FALSE
;
3506 /*!\brief Called when a module is loaded.
3508 * \param Name Filename of the module which was loaded.
3511 KdbpCliModuleLoaded(
3512 IN PUNICODE_STRING Name
)
3514 if (!KdbBreakOnModuleLoad
)
3517 KdbpPrint("Module %wZ loaded.\n", Name
);
3518 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
3521 /*!\brief This function is called by KdbEnterDebuggerException...
3523 * Used to interpret the init file in a context with a trapframe setup
3524 * (KdbpCliInit call KdbEnter which will call KdbEnterDebuggerException which will
3525 * call this function if KdbInitFileBuffer is not NULL.
3528 KdbpCliInterpretInitFile(VOID
)
3534 /* Execute the commands in the init file */
3535 DPRINT("KDB: Executing KDBinit file...\n");
3536 p1
= KdbInitFileBuffer
;
3537 while (p1
[0] != '\0')
3539 i
= strcspn(p1
, "\r\n");
3545 /* Look for "break" command and comments */
3548 while (isspace(p2
[0]))
3551 if (strncmp(p2
, "break", sizeof("break")-1) == 0 &&
3552 (p2
[sizeof("break")-1] == '\0' || isspace(p2
[sizeof("break")-1])))
3554 /* break into the debugger */
3555 KdbpCliMainLoop(FALSE
);
3557 else if (p2
[0] != '#' && p2
[0] != '\0') /* Ignore empty lines and comments */
3566 while (p1
[0] == '\r' || p1
[0] == '\n')
3569 DPRINT("KDB: KDBinit executed\n");
3572 /*!\brief Called when KDB is initialized
3574 * Reads the KDBinit file from the SystemRoot\System32\drivers\etc directory and executes it.
3580 OBJECT_ATTRIBUTES ObjectAttributes
;
3581 UNICODE_STRING FileName
;
3582 IO_STATUS_BLOCK Iosb
;
3583 FILE_STANDARD_INFORMATION FileStdInfo
;
3584 HANDLE hFile
= NULL
;
3589 /* Initialize the object attributes */
3590 RtlInitUnicodeString(&FileName
, L
"\\SystemRoot\\System32\\drivers\\etc\\KDBinit");
3591 InitializeObjectAttributes(&ObjectAttributes
, &FileName
, 0, NULL
, NULL
);
3594 Status
= ZwOpenFile(&hFile
, FILE_READ_DATA
| SYNCHRONIZE
,
3595 &ObjectAttributes
, &Iosb
, 0,
3596 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
|
3597 FILE_NO_INTERMEDIATE_BUFFERING
);
3598 if (!NT_SUCCESS(Status
))
3600 DPRINT("Could not open \\SystemRoot\\System32\\drivers\\etc\\KDBinit (Status 0x%x)", Status
);
3604 /* Get the size of the file */
3605 Status
= ZwQueryInformationFile(hFile
, &Iosb
, &FileStdInfo
, sizeof (FileStdInfo
),
3606 FileStandardInformation
);
3607 if (!NT_SUCCESS(Status
))
3610 DPRINT("Could not query size of \\SystemRoot\\System32\\drivers\\etc\\KDBinit (Status 0x%x)", Status
);
3613 FileSize
= FileStdInfo
.EndOfFile
.u
.LowPart
;
3615 /* Allocate memory for the file */
3616 FileBuffer
= ExAllocatePool(PagedPool
, FileSize
+ 1); /* add 1 byte for terminating '\0' */
3620 DPRINT("Could not allocate %d bytes for KDBinit file\n", FileSize
);
3624 /* Load file into memory */
3625 Status
= ZwReadFile(hFile
, 0, 0, 0, &Iosb
, FileBuffer
, FileSize
, 0, 0);
3628 if (!NT_SUCCESS(Status
) && Status
!= STATUS_END_OF_FILE
)
3630 ExFreePool(FileBuffer
);
3631 DPRINT("Could not read KDBinit file into memory (Status 0x%lx)\n", Status
);
3635 FileSize
= min(FileSize
, (INT
)Iosb
.Information
);
3636 FileBuffer
[FileSize
] = '\0';
3638 /* Enter critical section */
3639 OldEflags
= __readeflags();
3642 /* Interpret the init file... */
3643 KdbInitFileBuffer
= FileBuffer
;
3645 KdbInitFileBuffer
= NULL
;
3647 /* Leave critical section */
3648 __writeeflags(OldEflags
);
3650 ExFreePool(FileBuffer
);
3655 KdpSerialDebugPrint(
3660 STRING KdpPromptString
= RTL_CONSTANT_STRING("kdb:> ");
3661 extern KSPIN_LOCK KdpSerialSpinLock
;
3665 KdpPrompt(IN LPSTR InString
,
3666 IN USHORT InStringLength
,
3667 OUT LPSTR OutString
,
3668 IN USHORT OutStringLength
)
3672 ULONG DummyScanCode
;
3675 /* Acquire the printing spinlock without waiting at raised IRQL */
3678 /* Wait when the spinlock becomes available */
3679 while (!KeTestSpinLock(&KdpSerialSpinLock
));
3681 /* Spinlock was free, raise IRQL */
3682 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
3684 /* Try to get the spinlock */
3685 if (KeTryToAcquireSpinLockAtDpcLevel(&KdpSerialSpinLock
))
3688 /* Someone else got the spinlock, lower IRQL back */
3689 KeLowerIrql(OldIrql
);
3692 /* Loop the string to send */
3693 for (i
= 0; i
< InStringLength
; i
++)
3695 /* Print it to serial */
3696 KdPortPutByteEx(&SerialPortInfo
, *(PCHAR
)(InString
+ i
));
3699 /* Print a new line for log neatness */
3700 KdPortPutByteEx(&SerialPortInfo
, '\r');
3701 KdPortPutByteEx(&SerialPortInfo
, '\n');
3703 /* Print the kdb prompt */
3704 for (i
= 0; i
< KdpPromptString
.Length
; i
++)
3706 /* Print it to serial */
3707 KdPortPutByteEx(&SerialPortInfo
,
3708 *(KdpPromptString
.Buffer
+ i
));
3711 if (!(KdbDebugState
& KD_DEBUG_KDSERIAL
))
3714 /* Loop the whole string */
3715 for (i
= 0; i
< OutStringLength
; i
++)
3717 /* Check if this is serial debugging mode */
3718 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
3720 /* Get the character from serial */
3723 Response
= KdbpTryGetCharSerial(MAXULONG
);
3724 } while (Response
== -1);
3728 /* Get the response from the keyboard */
3731 Response
= KdbpTryGetCharKeyboard(&DummyScanCode
, MAXULONG
);
3732 } while (Response
== -1);
3735 /* Check for return */
3736 if (Response
== '\r')
3739 * We might need to discard the next '\n'.
3740 * Wait a bit to make sure we receive it.
3742 KeStallExecutionProcessor(100000);
3744 /* Check the mode */
3745 if (KdbDebugState
& KD_DEBUG_KDSERIAL
)
3747 /* Read and discard the next character, if any */
3748 KdbpTryGetCharSerial(5);
3752 /* Read and discard the next character, if any */
3753 KdbpTryGetCharKeyboard(&DummyScanCode
, 5);
3757 * Null terminate the output string -- documentation states that
3758 * DbgPrompt does not null terminate, but it does
3760 *(PCHAR
)(OutString
+ i
) = 0;
3764 /* Write it back and print it to the log */
3765 *(PCHAR
)(OutString
+ i
) = Response
;
3766 KdPortPutByteEx(&SerialPortInfo
, Response
);
3769 if (!(KdbDebugState
& KD_DEBUG_KDSERIAL
))
3772 /* Print a new line */
3773 KdPortPutByteEx(&SerialPortInfo
, '\r');
3774 KdPortPutByteEx(&SerialPortInfo
, '\n');
3776 /* Release spinlock */
3777 KiReleaseSpinLock(&KdpSerialSpinLock
);
3779 /* Lower IRQL back */
3780 KeLowerIrql(OldIrql
);
3782 /* Return the length */