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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/dbg/kdb_cli.c
23 * PURPOSE: Kernel debugger command line interface
24 * PROGRAMMER: Gregor Anich (blight@blight.eu.org)
29 /* INCLUDES ******************************************************************/
34 #include <internal/debug.h>
36 /* DEFINES *******************************************************************/
42 #define KEY_SCAN_UP 72
43 #define KEY_SCAN_DOWN 80
45 #define KDB_ENTER_CONDITION_TO_STRING(cond) \
46 ((cond) == KdbDoNotEnter ? "never" : \
47 ((cond) == KdbEnterAlways ? "always" : \
48 ((cond) == KdbEnterFromKmode ? "kmode" : "umode")))
50 #define KDB_ACCESS_TYPE_TO_STRING(type) \
51 ((type) == KdbAccessRead ? "read" : \
52 ((type) == KdbAccessWrite ? "write" : \
53 ((type) == KdbAccessReadWrite ? "rdwr" : "exec")))
55 #define NPX_STATE_TO_STRING(state) \
56 ((state) == NPX_STATE_INVALID ? "Invalid" : \
57 ((state) == NPX_STATE_VALID ? "Valid" : \
58 ((state) == NPX_STATE_DIRTY ? "Dirty" : "Unknown")))
60 /* PROTOTYPES ****************************************************************/
62 STATIC BOOLEAN
KdbpCmdEvalExpression(ULONG Argc
, PCHAR Argv
[]);
63 STATIC BOOLEAN
KdbpCmdDisassembleX(ULONG Argc
, PCHAR Argv
[]);
64 STATIC BOOLEAN
KdbpCmdRegs(ULONG Argc
, PCHAR Argv
[]);
65 STATIC BOOLEAN
KdbpCmdBackTrace(ULONG Argc
, PCHAR Argv
[]);
67 STATIC BOOLEAN
KdbpCmdContinue(ULONG Argc
, PCHAR Argv
[]);
68 STATIC BOOLEAN
KdbpCmdStep(ULONG Argc
, PCHAR Argv
[]);
69 STATIC BOOLEAN
KdbpCmdBreakPointList(ULONG Argc
, PCHAR Argv
[]);
70 STATIC BOOLEAN
KdbpCmdEnableDisableClearBreakPoint(ULONG Argc
, PCHAR Argv
[]);
71 STATIC BOOLEAN
KdbpCmdBreakPoint(ULONG Argc
, PCHAR Argv
[]);
73 STATIC BOOLEAN
KdbpCmdThread(ULONG Argc
, PCHAR Argv
[]);
74 STATIC BOOLEAN
KdbpCmdProc(ULONG Argc
, PCHAR Argv
[]);
76 STATIC BOOLEAN
KdbpCmdMod(ULONG Argc
, PCHAR Argv
[]);
77 STATIC BOOLEAN
KdbpCmdGdtLdtIdt(ULONG Argc
, PCHAR Argv
[]);
78 STATIC BOOLEAN
KdbpCmdPcr(ULONG Argc
, PCHAR Argv
[]);
79 STATIC BOOLEAN
KdbpCmdTss(ULONG Argc
, PCHAR Argv
[]);
81 STATIC BOOLEAN
KdbpCmdBugCheck(ULONG Argc
, PCHAR Argv
[]);
82 STATIC BOOLEAN
KdbpCmdSet(ULONG Argc
, PCHAR Argv
[]);
83 STATIC BOOLEAN
KdbpCmdHelp(ULONG Argc
, PCHAR Argv
[]);
85 /* GLOBALS *******************************************************************/
87 STATIC BOOLEAN KdbUseIntelSyntax
= FALSE
; /* Set to TRUE for intel syntax */
89 STATIC CHAR KdbCommandHistoryBuffer
[2048]; /* Command history string ringbuffer */
90 STATIC PCHAR KdbCommandHistory
[sizeof(KdbCommandHistoryBuffer
) / 8] = { NULL
}; /* Command history ringbuffer */
91 STATIC LONG KdbCommandHistoryBufferIndex
= 0;
92 STATIC LONG KdbCommandHistoryIndex
= 0;
94 STATIC ULONG KdbNumberOfRowsPrinted
= 0;
95 STATIC ULONG KdbNumberOfColsPrinted
= 0;
96 STATIC BOOLEAN KdbOutputAborted
= FALSE
;
97 STATIC LONG KdbNumberOfRowsTerminal
= -1;
98 STATIC LONG KdbNumberOfColsTerminal
= -1;
100 PCHAR KdbInitFileBuffer
= NULL
; /* Buffer where KDB.init file is loaded into during initialization */
107 BOOLEAN (*Fn
)(ULONG Argc
, PCHAR Argv
[]);
108 } KdbDebuggerCommands
[] = {
110 { NULL
, NULL
, "Data", NULL
},
111 { "?", "? expression", "Evaluate expression.", KdbpCmdEvalExpression
},
112 { "disasm", "disasm [address] [L count]", "Disassemble count instructions at address.", KdbpCmdDisassembleX
},
113 { "x", "x [address] [L count]", "Display count dwords, starting at addr.", KdbpCmdDisassembleX
},
114 { "regs", "regs", "Display general purpose registers.", KdbpCmdRegs
},
115 { "cregs", "cregs", "Display control registers.", KdbpCmdRegs
},
116 { "sregs", "sregs", "Display status registers.", KdbpCmdRegs
},
117 { "dregs", "dregs", "Display debug registers.", KdbpCmdRegs
},
118 { "bt", "bt [*frameaddr|thread id]", "Prints current backtrace or from given frame addr", KdbpCmdBackTrace
},
121 { NULL
, NULL
, "Flow control", NULL
},
122 { "cont", "cont", "Continue execution (leave debugger)", KdbpCmdContinue
},
123 { "step", "step [count]", "Execute single instructions, stepping into interrupts.", KdbpCmdStep
},
124 { "next", "next [count]", "Execute single instructions, skipping calls and reps.", KdbpCmdStep
},
125 { "bl", "bl", "List breakpoints.", KdbpCmdBreakPointList
},
126 { "be", "be [breakpoint]", "Enable breakpoint.", KdbpCmdEnableDisableClearBreakPoint
},
127 { "bd", "bd [breakpoint]", "Disable breakpoint.", KdbpCmdEnableDisableClearBreakPoint
},
128 { "bc", "bc [breakpoint]", "Clear breakpoint.", KdbpCmdEnableDisableClearBreakPoint
},
129 { "bpx", "bpx [address] [IF condition]", "Set software execution breakpoint at address.", KdbpCmdBreakPoint
},
130 { "bpm", "bpm [r|w|rw|x] [byte|word|dword] [address] [IF condition]", "Set memory breakpoint at address.", KdbpCmdBreakPoint
},
133 { NULL
, NULL
, "Process/Thread", NULL
},
134 { "thread", "thread [list[ pid]|[attach ]tid]", "List threads in current or specified process, display thread with given id or attach to thread.", KdbpCmdThread
},
135 { "proc", "proc [list|[attach ]pid]", "List processes, display process with given id or attach to process.", KdbpCmdProc
},
137 /* System information */
138 { NULL
, NULL
, "System info", NULL
},
139 { "mod", "mod [address]", "List all modules or the one containing address.", KdbpCmdMod
},
140 { "gdt", "gdt", "Display global descriptor table.", KdbpCmdGdtLdtIdt
},
141 { "ldt", "ldt", "Display local descriptor table.", KdbpCmdGdtLdtIdt
},
142 { "idt", "idt", "Display interrupt descriptor table.", KdbpCmdGdtLdtIdt
},
143 { "pcr", "pcr", "Display processor control region.", KdbpCmdPcr
},
144 { "tss", "tss", "Display task state segment.", KdbpCmdTss
},
147 { NULL
, NULL
, "Others", NULL
},
148 { "bugcheck", "bugcheck", "Bugchecks the system.", KdbpCmdBugCheck
},
149 { "set", "set [var] [value]", "Sets var to value or displays value of var.", KdbpCmdSet
},
150 { "help", "help", "Display help screen.", KdbpCmdHelp
}
153 /* FUNCTIONS *****************************************************************/
155 /*!\brief Evaluates an expression...
157 * Much like KdbpRpnEvaluateExpression, but prints the error message (if any)
158 * at the given offset.
160 * \param Expression Expression to evaluate.
161 * \param ErrOffset Offset (in characters) to print the error message at.
162 * \param Result Receives the result on success.
164 * \retval TRUE Success.
165 * \retval FALSE Failure.
168 KdbpEvaluateExpression(
171 OUT PULONGLONG Result
)
173 STATIC CHAR ErrMsgBuffer
[130] = "^ ";
174 LONG ExpressionErrOffset
= -1;
175 PCHAR ErrMsg
= ErrMsgBuffer
;
178 Ok
= KdbpRpnEvaluateExpression(Expression
, KdbCurrentTrapFrame
, Result
,
179 &ExpressionErrOffset
, ErrMsgBuffer
+ 2);
182 if (ExpressionErrOffset
>= 0)
183 ExpressionErrOffset
+= ErrOffset
;
186 KdbpPrint("%*s%s\n", ExpressionErrOffset
, "", ErrMsg
);
192 /*!\brief Evaluates an expression and displays the result.
195 KdbpCmdEvalExpression(ULONG Argc
, PCHAR Argv
[])
198 ULONGLONG Result
= 0;
205 KdbpPrint("?: Argument required\n");
209 /* Put the arguments back together */
211 for (i
= 1; i
< Argc
; i
++)
213 len
= strlen(Argv
[i
]);
217 /* Evaluate the expression */
218 Ok
= KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
);
221 if (Result
> 0x00000000ffffffffLL
)
223 if (Result
& 0x8000000000000000LL
)
224 KdbpPrint("0x%016I64x %20I64u %20I64d\n", Result
, Result
, Result
);
226 KdbpPrint("0x%016I64x %20I64u\n", Result
, Result
);
231 if (ul
<= 0xff && ul
>= 0x80)
232 l
= (LONG
)((CHAR
)ul
);
233 else if (ul
<= 0xffff && ul
>= 0x8000)
234 l
= (LONG
)((SHORT
)ul
);
238 KdbpPrint("0x%08lx %10lu %10ld\n", ul
, ul
, l
);
240 KdbpPrint("0x%08lx %10lu\n", ul
, ul
);
247 /*!\brief Disassembles 10 instructions at eip or given address or
248 * displays 16 dwords from memory at given address.
251 KdbpCmdDisassembleX(ULONG Argc
, PCHAR Argv
[])
256 ULONGLONG Result
= 0;
257 ULONG_PTR Address
= KdbCurrentTrapFrame
->Tf
.Eip
;
260 if (Argv
[0][0] == 'x') /* display memory */
262 else /* disassemble */
267 /* Check for [L count] part */
269 if (strcmp(Argv
[Argc
-2], "L") == 0)
271 ul
= strtoul(Argv
[Argc
-1], NULL
, 0);
278 else if (Argv
[Argc
-1][0] == 'L')
280 ul
= strtoul(Argv
[Argc
-1] + 1, NULL
, 0);
288 /* Put the remaining arguments back together */
290 for (ul
= 1; ul
< Argc
; ul
++)
292 Argv
[ul
][strlen(Argv
[ul
])] = ' ';
297 /* Evaluate the expression */
300 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
302 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
303 KdbpPrint("Warning: Address %I64x is beeing truncated\n");
304 Address
= (ULONG_PTR
)Result
;
306 else if (Argv
[0][0] == 'x')
308 KdbpPrint("x: Address argument required.\n");
312 if (Argv
[0][0] == 'x')
318 if (!KdbSymPrintAddress((PVOID
)Address
))
319 KdbpPrint("<%x>:", Address
);
326 if (!NT_SUCCESS(KdbpSafeReadMemory(&ul
, (PVOID
)Address
, sizeof(ul
))))
327 KdbpPrint(" ????????");
329 KdbpPrint(" %08x", ul
);
330 Address
+= sizeof(ul
);
340 if (!KdbSymPrintAddress((PVOID
)Address
))
341 KdbpPrint("<%08x>: ", Address
);
344 InstLen
= KdbpDisassemble(Address
, KdbUseIntelSyntax
);
347 KdbpPrint("<INVALID>\n");
358 /*!\brief Displays CPU registers.
361 KdbpCmdRegs(ULONG Argc
, PCHAR Argv
[])
363 PKTRAP_FRAME Tf
= &KdbCurrentTrapFrame
->Tf
;
365 STATIC CONST PCHAR EflagsBits
[32] = { " CF", NULL
, " PF", " BIT3", " AF", " BIT5",
366 " ZF", " SF", " TF", " IF", " DF", " OF",
367 NULL
, NULL
, " NT", " BIT15", " RF", " VF",
368 " AC", " VIF", " VIP", " ID", " BIT22",
369 " BIT23", " BIT24", " BIT25", " BIT26",
370 " BIT27", " BIT28", " BIT29", " BIT30",
373 if (Argv
[0][0] == 'r') /* regs */
375 KdbpPrint("CS:EIP 0x%04x:0x%08x\n"
376 "SS:ESP 0x%04x:0x%08x\n"
377 " EAX 0x%08x EBX 0x%08x\n"
378 " ECX 0x%08x EDX 0x%08x\n"
379 " ESI 0x%08x EDI 0x%08x\n"
381 Tf
->Cs
& 0xFFFF, Tf
->Eip
,
387 KdbpPrint("EFLAGS 0x%08x ", Tf
->Eflags
);
388 for (i
= 0; i
< 32; i
++)
392 if ((Tf
->Eflags
& (1 << 1)) == 0)
397 KdbpPrint(" IOPL%d", (Tf
->Eflags
>> 12) & 3);
402 else if ((Tf
->Eflags
& (1 << i
)) != 0)
403 KdbpPrint(EflagsBits
[i
]);
407 else if (Argv
[0][0] == 'c') /* cregs */
409 ULONG Cr0
, Cr2
, Cr3
, Cr4
;
410 struct __attribute__((packed
)) {
415 STATIC CONST PCHAR Cr0Bits
[32] = { " PE", " MP", " EM", " TS", " ET", " NE", NULL
, NULL
,
416 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
417 " WP", NULL
, " AM", NULL
, NULL
, NULL
, NULL
, NULL
,
418 NULL
, NULL
, NULL
, NULL
, NULL
, " NW", " CD", " PG" };
419 STATIC CONST PCHAR Cr4Bits
[32] = { " VME", " PVI", " TSD", " DE", " PSE", " PAE", " MCE", " PGE",
420 " PCE", " OSFXSR", " OSXMMEXCPT", NULL
, NULL
, NULL
, NULL
, NULL
,
421 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
422 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
424 Cr0
= KdbCurrentTrapFrame
->Cr0
;
425 Cr2
= KdbCurrentTrapFrame
->Cr2
;
426 Cr3
= KdbCurrentTrapFrame
->Cr3
;
427 Cr4
= KdbCurrentTrapFrame
->Cr4
;
429 /* Get descriptor table regs */
430 asm volatile("sgdt %0" : : "m"(Gdtr
));
431 asm volatile("sldt %0" : : "m"(Ldtr
));
432 asm volatile("sidt %0" : : "m"(Idtr
));
434 /* Get the task register */
435 asm volatile("str %0" : "=g"(Tr
));
437 /* Display the control registers */
438 KdbpPrint("CR0 0x%08x ", Cr0
);
439 for (i
= 0; i
< 32; i
++)
441 if (Cr0Bits
[i
] == NULL
)
443 if ((Cr0
& (1 << i
)) != 0)
444 KdbpPrint(Cr0Bits
[i
]);
446 KdbpPrint("\nCR2 0x%08x\n", Cr2
);
447 KdbpPrint("CR3 0x%08x Pagedir-Base 0x%08x %s%s\n", Cr3
, (Cr3
& 0xfffff000),
448 (Cr3
& (1 << 3)) ? " PWT" : "", (Cr3
& (1 << 4)) ? " PCD" : "" );
449 KdbpPrint("CR4 0x%08x ", Cr4
);
450 for (i
= 0; i
< 32; i
++)
452 if (Cr4Bits
[i
] == NULL
)
454 if ((Cr4
& (1 << i
)) != 0)
455 KdbpPrint(Cr4Bits
[i
]);
458 /* Display the descriptor table regs */
459 KdbpPrint("\nGDTR Base 0x%08x Size 0x%04x\n", Gdtr
.Base
, Gdtr
.Limit
);
460 KdbpPrint("LDTR Base 0x%08x Size 0x%04x\n", Ldtr
.Base
, Ldtr
.Limit
);
461 KdbpPrint("IDTR Base 0x%08x Size 0x%04x\n", Idtr
.Base
, Idtr
.Limit
);
463 else if (Argv
[0][0] == 's') /* sregs */
465 KdbpPrint("CS 0x%04x Index 0x%04x %cDT RPL%d\n",
466 Tf
->Cs
& 0xffff, (Tf
->Cs
& 0xffff) >> 3,
467 (Tf
->Cs
& (1 << 2)) ? 'L' : 'G', Tf
->Cs
& 3);
468 KdbpPrint("DS 0x%04x Index 0x%04x %cDT RPL%d\n",
469 Tf
->Ds
, Tf
->Ds
>> 3, (Tf
->Ds
& (1 << 2)) ? 'L' : 'G', Tf
->Ds
& 3);
470 KdbpPrint("ES 0x%04x Index 0x%04x %cDT RPL%d\n",
471 Tf
->Es
, Tf
->Es
>> 3, (Tf
->Es
& (1 << 2)) ? 'L' : 'G', Tf
->Es
& 3);
472 KdbpPrint("FS 0x%04x Index 0x%04x %cDT RPL%d\n",
473 Tf
->Fs
, Tf
->Fs
>> 3, (Tf
->Fs
& (1 << 2)) ? 'L' : 'G', Tf
->Fs
& 3);
474 KdbpPrint("GS 0x%04x Index 0x%04x %cDT RPL%d\n",
475 Tf
->Gs
, Tf
->Gs
>> 3, (Tf
->Gs
& (1 << 2)) ? 'L' : 'G', Tf
->Gs
& 3);
476 KdbpPrint("SS 0x%04x Index 0x%04x %cDT RPL%d\n",
477 Tf
->Ss
, Tf
->Ss
>> 3, (Tf
->Ss
& (1 << 2)) ? 'L' : 'G', Tf
->Ss
& 3);
481 ASSERT(Argv
[0][0] == 'd');
482 KdbpPrint("DR0 0x%08x\n"
488 Tf
->Dr0
, Tf
->Dr1
, Tf
->Dr2
, Tf
->Dr3
,
494 /*!\brief Displays a backtrace.
497 KdbpCmdBackTrace(ULONG Argc
, PCHAR Argv
[])
501 ULONGLONG Result
= 0;
502 ULONG_PTR Frame
= KdbCurrentTrapFrame
->Tf
.Ebp
;
507 /* Check for [L count] part */
509 if (strcmp(Argv
[Argc
-2], "L") == 0)
511 ul
= strtoul(Argv
[Argc
-1], NULL
, 0);
518 else if (Argv
[Argc
-1][0] == 'L')
520 ul
= strtoul(Argv
[Argc
-1] + 1, NULL
, 0);
528 /* Put the remaining arguments back together */
530 for (ul
= 1; ul
< Argc
; ul
++)
532 Argv
[ul
][strlen(Argv
[ul
])] = ' ';
537 /* Check if frame addr or thread id is given. */
540 if (Argv
[1][0] == '*')
543 /* Evaluate the expression */
544 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
546 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
547 KdbpPrint("Warning: Address %I64x is beeing truncated\n");
548 Frame
= (ULONG_PTR
)Result
;
553 KdbpPrint("Thread backtrace not supported yet!\n");
558 KdbpPrint("Frames:\n");
561 if (!NT_SUCCESS(KdbpSafeReadMemory(&Address
, (PVOID
)(Frame
+ sizeof(ULONG_PTR
)), sizeof (ULONG_PTR
))))
563 KdbpPrint("Couldn't access memory at 0x%x!\n", Frame
+ sizeof(ULONG_PTR
));
566 if (!KdbSymPrintAddress((PVOID
)Address
))
567 KdbpPrint("<%08x>\n", Address
);
570 if (!NT_SUCCESS(KdbpSafeReadMemory(&Frame
, (PVOID
)Frame
, sizeof (ULONG_PTR
))))
572 KdbpPrint("Couldn't access memory at 0x%x!\n", Frame
);
580 /*!\brief Continues execution of the system/leaves KDB.
583 KdbpCmdContinue(ULONG Argc
, PCHAR Argv
[])
585 /* Exit the main loop */
589 /*!\brief Continues execution of the system/leaves KDB.
592 KdbpCmdStep(ULONG Argc
, PCHAR Argv
[])
598 Count
= strtoul(Argv
[1], NULL
, 0);
601 KdbpPrint("%s: Integer argument required\n", Argv
[0]);
606 if (Argv
[0][0] == 'n')
607 KdbSingleStepOver
= TRUE
;
609 KdbSingleStepOver
= FALSE
;
611 /* Set the number of single steps and return to the interrupted code. */
612 KdbNumSingleSteps
= Count
;
617 /*!\brief Lists breakpoints.
620 KdbpCmdBreakPointList(ULONG Argc
, PCHAR Argv
[])
623 ULONG_PTR Address
= 0;
624 KDB_BREAKPOINT_TYPE Type
= 0;
625 KDB_ACCESS_TYPE AccessType
= 0;
628 BOOLEAN Enabled
= FALSE
;
629 BOOLEAN Global
= FALSE
;
630 PEPROCESS Process
= NULL
;
631 PCHAR str1
, str2
, ConditionExpr
, GlobalOrLocal
;
634 l
= KdbpGetNextBreakPointNr(0);
637 KdbpPrint("No breakpoints.\n");
641 KdbpPrint("Breakpoints:\n");
644 if (!KdbpGetBreakPointInfo(l
, &Address
, &Type
, &Size
, &AccessType
, &DebugReg
,
645 &Enabled
, &Global
, &Process
, &ConditionExpr
))
650 if (l
== KdbLastBreakPointNr
)
662 GlobalOrLocal
= " global";
665 GlobalOrLocal
= Buffer
;
666 sprintf(Buffer
, " PID 0x%08lx",
667 (ULONG
)(Process
? Process
->UniqueProcessId
: INVALID_HANDLE_VALUE
));
670 if (Type
== KdbBreakPointSoftware
|| Type
== KdbBreakPointTemporary
)
672 KdbpPrint(" %s%03d BPX 0x%08x%s%s%s%s%s\n",
674 Enabled
? "" : " disabled",
676 ConditionExpr
? " IF " : "",
677 ConditionExpr
? ConditionExpr
: "",
680 else if (Type
== KdbBreakPointHardware
)
684 KdbpPrint(" %s%03d BPM 0x%08x %-5s %-5s disabled%s%s%s%s\n", str1
, l
, Address
,
685 KDB_ACCESS_TYPE_TO_STRING(AccessType
),
686 Size
== 1 ? "byte" : (Size
== 2 ? "word" : "dword"),
688 ConditionExpr
? " IF " : "",
689 ConditionExpr
? ConditionExpr
: "",
694 KdbpPrint(" %s%03d BPM 0x%08x %-5s %-5s DR%d%s%s%s%s\n", str1
, l
, Address
,
695 KDB_ACCESS_TYPE_TO_STRING(AccessType
),
696 Size
== 1 ? "byte" : (Size
== 2 ? "word" : "dword"),
699 ConditionExpr
? " IF " : "",
700 ConditionExpr
? ConditionExpr
: "",
705 while ((l
= KdbpGetNextBreakPointNr(l
+1)) >= 0);
710 /*!\brief Enables, disables or clears a breakpoint.
713 KdbpCmdEnableDisableClearBreakPoint(ULONG Argc
, PCHAR Argv
[])
720 KdbpPrint("%s: argument required\n", Argv
[0]);
725 BreakPointNr
= strtoul(Argv
[1], &pend
, 0);
726 if (pend
== Argv
[1] || *pend
!= '\0')
728 KdbpPrint("%s: integer argument required\n", Argv
[0]);
732 if (Argv
[0][1] == 'e') /* enable */
734 KdbpEnableBreakPoint(BreakPointNr
, NULL
);
736 else if (Argv
[0][1] == 'd') /* disable */
738 KdbpDisableBreakPoint(BreakPointNr
, NULL
);
742 ASSERT(Argv
[0][1] == 'c');
743 KdbpDeleteBreakPoint(BreakPointNr
, NULL
);
749 /*!\brief Sets a software or hardware (memory) breakpoint at the given address.
752 KdbpCmdBreakPoint(ULONG Argc
, PCHAR Argv
[])
754 ULONGLONG Result
= 0;
756 KDB_BREAKPOINT_TYPE Type
;
758 KDB_ACCESS_TYPE AccessType
= 0;
759 INT AddressArgIndex
, ConditionArgIndex
, i
;
760 BOOLEAN Global
= TRUE
;
762 if (Argv
[0][2] == 'x') /* software breakpoint */
766 KdbpPrint("bpx: Address argument required.\n");
771 Type
= KdbBreakPointSoftware
;
773 else /* memory breakpoint */
775 ASSERT(Argv
[0][2] == 'm');
779 KdbpPrint("bpm: Access type argument required (one of r, w, rw, x)\n");
783 if (_stricmp(Argv
[1], "x") == 0)
784 AccessType
= KdbAccessExec
;
785 else if (_stricmp(Argv
[1], "r") == 0)
786 AccessType
= KdbAccessRead
;
787 else if (_stricmp(Argv
[1], "w") == 0)
788 AccessType
= KdbAccessWrite
;
789 else if (_stricmp(Argv
[1], "rw") == 0)
790 AccessType
= KdbAccessReadWrite
;
793 KdbpPrint("bpm: Unknown access type '%s'\n", Argv
[1]);
799 KdbpPrint("bpm: %s argument required.\n", AccessType
== KdbAccessExec
? "Address" : "Memory size");
803 if (_stricmp(Argv
[2], "byte") == 0)
805 else if (_stricmp(Argv
[2], "word") == 0)
807 else if (_stricmp(Argv
[2], "dword") == 0)
809 else if (AccessType
== KdbAccessExec
)
816 KdbpPrint("bpm: Unknown memory size '%s'\n", Argv
[2]);
820 if (Argc
<= AddressArgIndex
)
822 KdbpPrint("bpm: Address argument required.\n");
826 Type
= KdbBreakPointHardware
;
829 /* Put the arguments back together */
830 ConditionArgIndex
= -1;
831 for (i
= AddressArgIndex
; i
< (Argc
-1); i
++)
833 if (strcmp(Argv
[i
+1], "IF") == 0) /* IF found */
835 ConditionArgIndex
= i
+ 2;
836 if (ConditionArgIndex
>= Argc
)
838 KdbpPrint("%s: IF requires condition expression.\n", Argv
[0]);
841 for (i
= ConditionArgIndex
; i
< (Argc
-1); i
++)
842 Argv
[i
][strlen(Argv
[i
])] = ' ';
845 Argv
[i
][strlen(Argv
[i
])] = ' ';
848 /* Evaluate the address expression */
849 if (!KdbpEvaluateExpression(Argv
[AddressArgIndex
],
850 sizeof("kdb:> ")-1 + (Argv
[AddressArgIndex
]-Argv
[0]),
855 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
856 KdbpPrint("%s: Warning: Address %I64x is beeing truncated\n", Argv
[0]);
857 Address
= (ULONG_PTR
)Result
;
859 KdbpInsertBreakPoint(Address
, Type
, Size
, AccessType
,
860 (ConditionArgIndex
< 0) ? NULL
: Argv
[ConditionArgIndex
],
866 /*!\brief Lists threads or switches to another thread context.
869 KdbpCmdThread(ULONG Argc
, PCHAR Argv
[])
872 PETHREAD Thread
= NULL
;
873 PEPROCESS Process
= NULL
;
877 PCHAR State
, pend
, str1
, str2
;
878 STATIC CONST PCHAR ThreadStateToString
[THREAD_STATE_MAX
] =
879 { "Initialized", "Ready", "Running",
880 "Suspended", "Frozen", "Terminated1",
881 "Terminated2", "Blocked" };
882 ASSERT(KdbCurrentProcess
!= NULL
);
884 if (Argc
>= 2 && _stricmp(Argv
[1], "list") == 0)
886 Process
= KdbCurrentProcess
;
890 ul
= strtoul(Argv
[2], &pend
, 0);
893 KdbpPrint("thread: '%s' is not a valid process id!\n", Argv
[2]);
896 if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID
)ul
, &Process
)))
898 KdbpPrint("thread: Invalid process id!\n");
903 Entry
= Process
->ThreadListHead
.Flink
;
904 if (Entry
== &Process
->ThreadListHead
)
907 KdbpPrint("No threads in process 0x%08x!\n", ul
);
909 KdbpPrint("No threads in current process!\n");
913 KdbpPrint(" TID State Prior. Affinity EBP EIP\n");
916 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
918 if (Thread
== KdbCurrentThread
)
929 if (Thread
->Tcb
.TrapFrame
!= NULL
)
931 Esp
= (PULONG
)Thread
->Tcb
.TrapFrame
->Esp
;
932 Ebp
= (PULONG
)Thread
->Tcb
.TrapFrame
->Ebp
;
933 Eip
= Thread
->Tcb
.TrapFrame
->Eip
;
937 Esp
= (PULONG
)Thread
->Tcb
.KernelStack
;
938 Ebp
= (PULONG
)Esp
[4];
940 if (Ebp
!= NULL
) /* FIXME: Should we attach to the process to read Ebp[1]? */
941 KdbpSafeReadMemory(&Eip
, Ebp
+ 1, sizeof (Eip
));;
943 if (Thread
->Tcb
.State
< THREAD_STATE_MAX
)
944 State
= ThreadStateToString
[Thread
->Tcb
.State
];
948 KdbpPrint(" %s0x%08x %-11s %3d 0x%08x 0x%08x 0x%08x%s\n",
950 Thread
->Cid
.UniqueThread
,
952 Thread
->Tcb
.Priority
,
953 Thread
->Tcb
.Affinity
,
958 Entry
= Entry
->Flink
;
960 while (Entry
!= &Process
->ThreadListHead
);
962 else if (Argc
>= 2 && _stricmp(Argv
[1], "attach") == 0)
966 KdbpPrint("thread attach: thread id argument required!\n");
970 ul
= strtoul(Argv
[2], &pend
, 0);
973 KdbpPrint("thread attach: '%s' is not a valid thread id!\n", Argv
[2]);
976 if (!KdbpAttachToThread((PVOID
)ul
))
980 KdbpPrint("Attached to thread 0x%08x.\n", ul
);
984 Thread
= KdbCurrentThread
;
988 ul
= strtoul(Argv
[1], &pend
, 0);
991 KdbpPrint("thread: '%s' is not a valid thread id!\n", Argv
[1]);
994 if (!NT_SUCCESS(PsLookupThreadByThreadId((PVOID
)ul
, &Thread
)))
996 KdbpPrint("thread: Invalid thread id!\n");
1001 if (Thread
->Tcb
.State
< THREAD_STATE_MAX
)
1002 State
= ThreadStateToString
[Thread
->Tcb
.State
];
1007 " State: %s (0x%x)\n"
1009 " Affinity: 0x%08x\n"
1010 " Initial Stack: 0x%08x\n"
1011 " Stack Limit: 0x%08x\n"
1012 " Stack Base: 0x%08x\n"
1013 " Kernel Stack: 0x%08x\n"
1014 " Trap Frame: 0x%08x\n"
1015 " NPX State: %s (0x%x)\n",
1016 (Argc
< 2) ? "Current Thread:\n" : "",
1017 Thread
->Cid
.UniqueThread
,
1018 State
, Thread
->Tcb
.State
,
1019 Thread
->Tcb
.Priority
,
1020 Thread
->Tcb
.Affinity
,
1021 Thread
->Tcb
.InitialStack
,
1022 Thread
->Tcb
.StackLimit
,
1023 Thread
->Tcb
.StackBase
,
1024 Thread
->Tcb
.KernelStack
,
1025 Thread
->Tcb
.TrapFrame
,
1026 NPX_STATE_TO_STRING(Thread
->Tcb
.NpxState
), Thread
->Tcb
.NpxState
);
1033 /*!\brief Lists processes or switches to another process context.
1036 KdbpCmdProc(ULONG Argc
, PCHAR Argv
[])
1040 PCHAR State
, pend
, str1
, str2
;
1042 extern LIST_ENTRY PsActiveProcessHead
;
1044 if (Argc
>= 2 && _stricmp(Argv
[1], "list") == 0)
1046 Entry
= PsActiveProcessHead
.Flink
;
1047 if (Entry
== &PsActiveProcessHead
)
1049 KdbpPrint("No processes in the system!\n");
1053 KdbpPrint(" PID State Filename\n");
1056 Process
= CONTAINING_RECORD(Entry
, EPROCESS
, ProcessListEntry
);
1058 if (Process
== KdbCurrentProcess
)
1069 State
= ((Process
->Pcb
.State
== PROCESS_STATE_TERMINATED
) ? "Terminated" :
1070 ((Process
->Pcb
.State
== PROCESS_STATE_ACTIVE
) ? "Active" : "Unknown"));
1072 KdbpPrint(" %s0x%08x %-10s %s%s\n",
1074 Process
->UniqueProcessId
,
1076 Process
->ImageFileName
,
1079 Entry
= Entry
->Flink
;
1081 while(Entry
!= &PsActiveProcessHead
);
1083 else if (Argc
>= 2 && _stricmp(Argv
[1], "attach") == 0)
1087 KdbpPrint("process attach: process id argument required!\n");
1091 ul
= strtoul(Argv
[2], &pend
, 0);
1092 if (Argv
[2] == pend
)
1094 KdbpPrint("process attach: '%s' is not a valid process id!\n", Argv
[2]);
1097 if (!KdbpAttachToProcess((PVOID
)ul
))
1101 KdbpPrint("Attached to process 0x%08x, thread 0x%08x.\n", (UINT
)ul
,
1102 (UINT
)KdbCurrentThread
->Cid
.UniqueThread
);
1106 Process
= KdbCurrentProcess
;
1110 ul
= strtoul(Argv
[1], &pend
, 0);
1111 if (Argv
[1] == pend
)
1113 KdbpPrint("proc: '%s' is not a valid process id!\n", Argv
[1]);
1116 if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID
)ul
, &Process
)))
1118 KdbpPrint("proc: Invalid process id!\n");
1123 State
= ((Process
->Pcb
.State
== PROCESS_STATE_TERMINATED
) ? "Terminated" :
1124 ((Process
->Pcb
.State
== PROCESS_STATE_ACTIVE
) ? "Active" : "Unknown"));
1127 " State: %s (0x%x)\n"
1128 " Image Filename: %s\n",
1129 (Argc
< 2) ? "Current process:\n" : "",
1130 Process
->UniqueProcessId
,
1131 State
, Process
->Pcb
.State
,
1132 Process
->ImageFileName
);
1138 /*!\brief Lists loaded modules or the one containing the specified address.
1141 KdbpCmdMod(ULONG Argc
, PCHAR Argv
[])
1143 ULONGLONG Result
= 0;
1145 KDB_MODULE_INFO Info
;
1146 BOOLEAN DisplayOnlyOneModule
= FALSE
;
1151 /* Put the arguments back together */
1154 Argv
[Argc
][strlen(Argv
[Argc
])] = ' ';
1156 /* Evaluate the expression */
1157 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
1161 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
1162 KdbpPrint("%s: Warning: Address %I64x is beeing truncated\n", Argv
[0]);
1163 Address
= (ULONG_PTR
)Result
;
1165 if (!KdbpSymFindModuleByAddress((PVOID
)Address
, &Info
))
1167 KdbpPrint("No module containing address 0x%x found!\n", Address
);
1170 DisplayOnlyOneModule
= TRUE
;
1174 if (!KdbpSymFindModuleByIndex(0, &Info
))
1176 KdbpPrint("No modules.\n");
1182 KdbpPrint(" Base Size Name\n");
1185 KdbpPrint(" %08x %08x %ws\n", Info
.Base
, Info
.Size
, Info
.Name
);
1187 if ((!DisplayOnlyOneModule
&& !KdbpSymFindModuleByIndex(i
++, &Info
)) ||
1188 DisplayOnlyOneModule
)
1197 /*!\brief Displays GDT, LDT or IDTd.
1200 KdbpCmdGdtLdtIdt(ULONG Argc
, PCHAR Argv
[])
1202 struct __attribute__((packed
)) {
1215 if (Argv
[0][0] == 'i')
1218 asm volatile("sidt %0" : : "m"(Reg
));
1222 KdbpPrint("Interrupt descriptor table is empty.\n");
1225 KdbpPrint("IDT Base: 0x%08x Limit: 0x%04x\n", Reg
.Base
, Reg
.Limit
);
1226 KdbpPrint(" Idx Type Seg. Sel. Offset DPL\n");
1227 for ( ; (i
+ sizeof(SegDesc
) - 1) <= Reg
.Limit
; i
+= 8)
1229 if (!NT_SUCCESS(KdbpSafeReadMemory(SegDesc
, (PVOID
)(Reg
.Base
+ i
), sizeof(SegDesc
))))
1231 KdbpPrint("Couldn't access memory at 0x%08x!\n", Reg
.Base
+ i
);
1235 if ((SegDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
1236 SegType
= "TASKGATE";
1237 else if ((SegDesc
[1] & 0x1fe0) == 0x0e00) /* 32 bit Interrupt gate */
1238 SegType
= "INTGATE32";
1239 else if ((SegDesc
[1] & 0x1fe0) == 0x0600) /* 16 bit Interrupt gate */
1240 SegType
= "INTGATE16";
1241 else if ((SegDesc
[1] & 0x1fe0) == 0x0f00) /* 32 bit Trap gate */
1242 SegType
= "TRAPGATE32";
1243 else if ((SegDesc
[1] & 0x1fe0) == 0x0700) /* 16 bit Trap gate */
1244 SegType
= "TRAPGATE16";
1246 if ((SegDesc
[1] & (1 << 15)) == 0) /* not present */
1248 KdbpPrint(" %03d %-10s [NP] [NP] %02d\n",
1249 i
/ 8, SegType
, Dpl
);
1251 else if ((SegDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
1253 SegSel
= SegDesc
[0] >> 16;
1254 KdbpPrint(" %03d %-10s 0x%04x %02d\n",
1255 i
/ 8, SegType
, SegSel
, Dpl
);
1259 SegSel
= SegDesc
[0] >> 16;
1260 SegBase
= (SegDesc
[1] & 0xffff0000) | (SegDesc
[0] & 0x0000ffff);
1261 KdbpPrint(" %03d %-10s 0x%04x 0x%08x %02d\n",
1262 i
/ 8, SegType
, SegSel
, SegBase
, Dpl
);
1269 if (Argv
[0][0] == 'g')
1272 asm volatile("sgdt %0" : : "m"(Reg
));
1277 ASSERT(Argv
[0][0] == 'l');
1279 asm volatile("sldt %0" : : "m"(Reg
));
1286 KdbpPrint("%s descriptor table is empty.\n",
1287 Argv
[0][0] == 'g' ? "Global" : "Local");
1290 KdbpPrint("%cDT Base: 0x%08x Limit: 0x%04x\n",
1291 Argv
[0][0] == 'g' ? 'G' : 'L', Reg
.Base
, Reg
.Limit
);
1292 KdbpPrint(" Idx Sel. Type Base Limit DPL Attribs\n");
1293 for ( ; (i
+ sizeof(SegDesc
) - 1) <= Reg
.Limit
; i
+= 8)
1295 if (!NT_SUCCESS(KdbpSafeReadMemory(SegDesc
, (PVOID
)(Reg
.Base
+ i
), sizeof(SegDesc
))))
1297 KdbpPrint("Couldn't access memory at 0x%08x!\n", Reg
.Base
+ i
);
1300 Dpl
= ((SegDesc
[1] >> 13) & 3);
1301 Type
= ((SegDesc
[1] >> 8) & 0xf);
1303 SegBase
= SegDesc
[0] >> 16;
1304 SegBase
|= (SegDesc
[1] & 0xff) << 16;
1305 SegBase
|= SegDesc
[1] & 0xff000000;
1306 SegLimit
= SegDesc
[0] & 0x0000ffff;
1307 SegLimit
|= (SegDesc
[1] >> 16) & 0xf;
1308 if ((SegDesc
[1] & (1 << 23)) != 0)
1318 if ((SegDesc
[1] & (1 << 12)) == 0) /* System segment */
1322 case 1: SegType
= "TSS16(Avl)"; break;
1323 case 2: SegType
= "LDT"; break;
1324 case 3: SegType
= "TSS16(Busy)"; break;
1325 case 4: SegType
= "CALLGATE16"; break;
1326 case 5: SegType
= "TASKGATE"; break;
1327 case 6: SegType
= "INTGATE16"; break;
1328 case 7: SegType
= "TRAPGATE16"; break;
1329 case 9: SegType
= "TSS32(Avl)"; break;
1330 case 11: SegType
= "TSS32(Busy)"; break;
1331 case 12: SegType
= "CALLGATE32"; break;
1332 case 14: SegType
= "INTGATE32"; break;
1333 case 15: SegType
= "INTGATE32"; break;
1334 default: SegType
= "UNKNOWN"; break;
1336 if (!(Type
>= 1 && Type
<= 3) &&
1337 Type
!= 9 && Type
!= 11)
1343 else if ((SegDesc
[1] & (1 << 11)) == 0) /* Data segment */
1345 if ((SegDesc
[1] & (1 << 22)) != 0)
1351 else /* Code segment */
1353 if ((SegDesc
[1] & (1 << 22)) != 0)
1359 if ((SegDesc
[1] & (1 << 15)) == 0) /* not present */
1361 KdbpPrint(" %03d 0x%04x %-11s [NP] [NP] %02d NP\n",
1362 i
/ 8, i
| Dpl
| ul
, SegType
, Dpl
);
1366 KdbpPrint(" %03d 0x%04x %-11s 0x%08x 0x%08x %02d ",
1367 i
/ 8, i
| Dpl
| ul
, SegType
, SegBase
, SegLimit
, Dpl
);
1368 if ((SegDesc
[1] & (1 << 12)) == 0) /* System segment */
1370 /* FIXME: Display system segment */
1372 else if ((SegDesc
[1] & (1 << 11)) == 0) /* Data segment */
1374 if ((SegDesc
[1] & (1 << 10)) != 0) /* Expand-down */
1376 KdbpPrint((SegDesc
[1] & (1 << 9)) ? " R/W" : " R");
1377 if ((SegDesc
[1] & (1 << 8)) != 0)
1380 else /* Code segment */
1382 if ((SegDesc
[1] & (1 << 10)) != 0) /* Conforming */
1384 KdbpPrint((SegDesc
[1] & (1 << 9)) ? " R/X" : " X");
1385 if ((SegDesc
[1] & (1 << 8)) != 0)
1388 if ((SegDesc
[1] & (1 << 20)) != 0)
1398 /*!\brief Displays the KPCR
1401 KdbpCmdPcr(ULONG Argc
, PCHAR Argv
[])
1403 PKPCR Pcr
= KeGetCurrentKPCR();
1405 KdbpPrint("Current PCR is at 0x%08x.\n", (INT
)Pcr
);
1406 KdbpPrint(" Tib.ExceptionList: 0x%08x\n"
1407 " Tib.StackBase: 0x%08x\n"
1408 " Tib.StackLimit: 0x%08x\n"
1409 " Tib.SubSystemTib: 0x%08x\n"
1410 " Tib.FiberData/Version: 0x%08x\n"
1411 " Tib.ArbitraryUserPointer: 0x%08x\n"
1412 " Tib.Self: 0x%08x\n"
1417 " IrrActive: 0x%08x\n"
1419 " KdVersionBlock: 0x%08x\n"
1423 " MajorVersion: 0x%04x\n"
1424 " MinorVersion: 0x%04x\n"
1425 " SetMember: 0x%08x\n"
1426 " StallScaleFactor: 0x%08x\n"
1427 " DebugActive: 0x%02x\n"
1428 " ProcessorNumber: 0x%02x\n"
1429 " L2CacheAssociativity: 0x%02x\n"
1430 " VdmAlert: 0x%08x\n"
1431 " L2CacheSize: 0x%08x\n"
1432 " InterruptMode: 0x%08x\n",
1433 Pcr
->Tib
.ExceptionList
, Pcr
->Tib
.StackBase
, Pcr
->Tib
.StackLimit
,
1434 Pcr
->Tib
.SubSystemTib
, Pcr
->Tib
.FiberData
, Pcr
->Tib
.ArbitraryUserPointer
,
1435 Pcr
->Tib
.Self
, Pcr
->Self
, Pcr
->PCRCB
, Pcr
->Irql
, Pcr
->IRR
, Pcr
->IrrActive
,
1436 Pcr
->IDR
, Pcr
->KdVersionBlock
, Pcr
->IDT
, Pcr
->GDT
, Pcr
->TSS
,
1437 Pcr
->MajorVersion
, Pcr
->MinorVersion
, Pcr
->SetMember
, Pcr
->StallScaleFactor
,
1438 Pcr
->DebugActive
, Pcr
->ProcessorNumber
, Pcr
->L2CacheAssociativity
,
1439 Pcr
->VdmAlert
, Pcr
->L2CacheSize
, Pcr
->InterruptMode
);
1444 /*!\brief Displays the TSS
1447 KdbpCmdTss(ULONG Argc
, PCHAR Argv
[])
1449 KTSS
*Tss
= KeGetCurrentKPCR()->TSS
;
1451 KdbpPrint("Current TSS is at 0x%08x.\n", (INT
)Tss
);
1452 KdbpPrint(" PreviousTask: 0x%08x\n"
1453 " Ss0:Esp0: 0x%04x:0x%08x\n"
1454 " Ss1:Esp1: 0x%04x:0x%08x\n"
1455 " Ss2:Esp2: 0x%04x:0x%08x\n"
1475 " IoMapBase: 0x%04x\n",
1476 Tss
->PreviousTask
, Tss
->Ss0
, Tss
->Esp0
, Tss
->Ss1
, Tss
->Esp1
,
1477 Tss
->Ss2
, Tss
->Esp2
, Tss
->Cr3
, Tss
->Eip
, Tss
->Eflags
, Tss
->Eax
,
1478 Tss
->Ecx
, Tss
->Edx
, Tss
->Ebx
, Tss
->Esp
, Tss
->Ebp
, Tss
->Esi
,
1479 Tss
->Edi
, Tss
->Es
, Tss
->Cs
, Tss
->Ss
, Tss
->Ds
, Tss
->Fs
, Tss
->Gs
,
1480 Tss
->Ldt
, Tss
->Trap
, Tss
->IoMapBase
);
1484 /*!\brief Bugchecks the system.
1487 KdbpCmdBugCheck(ULONG Argc
, PCHAR Argv
[])
1489 KEBUGCHECK(0xDEADDEAD);
1493 /*!\brief Sets or displays a config variables value.
1496 KdbpCmdSet(ULONG Argc
, PCHAR Argv
[])
1501 KDB_ENTER_CONDITION ConditionFirst
= KdbDoNotEnter
;
1502 KDB_ENTER_CONDITION ConditionLast
= KdbDoNotEnter
;
1503 STATIC CONST PCHAR ExceptionNames
[21] =
1504 { "ZERODEVIDE", "DEBUGTRAP", "NMI", "INT3", "OVERFLOW", "BOUND", "INVALIDOP",
1505 "NOMATHCOP", "DOUBLEFAULT", "RESERVED(9)", "INVALIDTSS", "SEGMENTNOTPRESENT",
1506 "STACKFAULT", "GPF", "PAGEFAULT", "RESERVED(15)", "MATHFAULT", "ALIGNMENTCHECK",
1507 "MACHINECHECK", "SIMDFAULT", "OTHERS" };
1511 KdbpPrint("Available settings:\n");
1512 KdbpPrint(" syntax [intel|at&t]\n");
1513 KdbpPrint(" condition [exception|*] [first|last] [never|always|kmode|umode]\n");
1515 else if (strcmp(Argv
[1], "syntax") == 0)
1518 KdbpPrint("syntax = %s\n", KdbUseIntelSyntax
? "intel" : "at&t");
1521 if (_stricmp(Argv
[2], "intel") == 0)
1522 KdbUseIntelSyntax
= TRUE
;
1523 else if (_stricmp(Argv
[2], "at&t") == 0)
1524 KdbUseIntelSyntax
= FALSE
;
1526 KdbpPrint("Unknown syntax '%s'.\n", Argv
[2]);
1529 else if (strcmp(Argv
[1], "condition") == 0)
1533 KdbpPrint("Conditions: (First) (Last)\n");
1534 for (l
= 0; l
< RTL_NUMBER_OF(ExceptionNames
) - 1; l
++)
1536 if (ExceptionNames
[l
] == NULL
)
1538 if (!KdbpGetEnterCondition(l
, TRUE
, &ConditionFirst
))
1540 if (!KdbpGetEnterCondition(l
, FALSE
, &ConditionLast
))
1542 KdbpPrint(" #%02d %-20s %-8s %-8s\n", l
, ExceptionNames
[l
],
1543 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1544 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1546 ASSERT(l
== (RTL_NUMBER_OF(ExceptionNames
) - 1));
1547 KdbpPrint(" %-20s %-8s %-8s\n", ExceptionNames
[l
],
1548 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1549 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1553 if (Argc
>= 5 && strcmp(Argv
[2], "*") == 0) /* Allow * only when setting condition */
1557 l
= (LONG
)strtoul(Argv
[2], &pend
, 0);
1558 if (Argv
[2] == pend
)
1560 for (l
= 0; l
< RTL_NUMBER_OF(ExceptionNames
); l
++)
1562 if (ExceptionNames
[l
] == NULL
)
1564 if (_stricmp(ExceptionNames
[l
], Argv
[2]) == 0)
1568 if (l
>= RTL_NUMBER_OF(ExceptionNames
))
1570 KdbpPrint("Unknown exception '%s'.\n", Argv
[2]);
1576 if (_stricmp(Argv
[3], "first") == 0)
1578 else if (_stricmp(Argv
[3], "last") == 0)
1582 KdbpPrint("set condition: second argument must be 'first' or 'last'\n");
1585 if (_stricmp(Argv
[4], "never") == 0)
1586 ConditionFirst
= KdbDoNotEnter
;
1587 else if (_stricmp(Argv
[4], "always") == 0)
1588 ConditionFirst
= KdbEnterAlways
;
1589 else if (_stricmp(Argv
[4], "umode") == 0)
1590 ConditionFirst
= KdbEnterFromUmode
;
1591 else if (_stricmp(Argv
[4], "kmode") == 0)
1592 ConditionFirst
= KdbEnterFromKmode
;
1595 KdbpPrint("set condition: third argument must be 'never', 'always', 'umode' or 'kmode'\n");
1598 if (!KdbpSetEnterCondition(l
, First
, ConditionFirst
))
1601 KdbpPrint("Couldn't change condition for exception #%02d\n", l
);
1603 KdbpPrint("Couldn't change condition for all exceptions\n", l
);
1606 else /* Argc >= 3 */
1608 if (!KdbpGetEnterCondition(l
, TRUE
, &ConditionFirst
))
1610 if (!KdbpGetEnterCondition(l
, FALSE
, &ConditionLast
))
1612 if (l
< (RTL_NUMBER_OF(ExceptionNames
) - 1))
1614 KdbpPrint("Condition for exception #%02d (%s): FirstChance %s LastChance %s\n",
1615 l
, ExceptionNames
[l
],
1616 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1617 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1621 KdbpPrint("Condition for all other exceptions: FirstChance %s LastChance %s\n",
1622 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1623 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1629 KdbpPrint("Unknown setting '%s'.\n", Argv
[1]);
1634 /*!\brief Displays help screen.
1637 KdbpCmdHelp(ULONG Argc
, PCHAR Argv
[])
1641 KdbpPrint("Kernel debugger commands:\n");
1642 for (i
= 0; i
< RTL_NUMBER_OF(KdbDebuggerCommands
); i
++)
1644 if (KdbDebuggerCommands
[i
].Syntax
== NULL
) /* Command group */
1648 KdbpPrint("\x1b[7m* %s:\x1b[0m\n", KdbDebuggerCommands
[i
].Help
);
1652 KdbpPrint(" %-20s - %s\n",
1653 KdbDebuggerCommands
[i
].Syntax
,
1654 KdbDebuggerCommands
[i
].Help
);
1660 /*!\brief Prints the given string with printf-like formatting.
1662 * \param Format Format of the string/arguments.
1663 * \param ... Variable number of arguments matching the format specified in \a Format.
1665 * \note Doesn't correctly handle \\t and terminal escape sequences when calculating the
1666 * number of lines required to print a single line from the Buffer in the terminal.
1673 STATIC CHAR Buffer
[4096];
1674 STATIC BOOLEAN TerminalInitialized
= FALSE
;
1675 STATIC BOOLEAN TerminalReportsSize
= TRUE
;
1680 INT RowsPrintedByTerminal
;
1683 /* Check if the user has aborted output of the current command */
1684 if (KdbOutputAborted
)
1687 /* Initialize the terminal */
1688 if (!TerminalInitialized
)
1690 DbgPrint("\x1b[7h"); /* Enable linewrap */
1691 TerminalInitialized
= TRUE
;
1694 /* Get number of rows and columns in terminal */
1695 if ((KdbNumberOfRowsTerminal
< 0) || (KdbNumberOfColsTerminal
< 0) ||
1696 (KdbNumberOfRowsPrinted
) == 0) /* Refresh terminal size each time when number of rows printed is 0 */
1698 if ((KdDebugState
& KD_DEBUG_KDSERIAL
) && TerminalReportsSize
)
1700 /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
1701 TerminalReportsSize
= FALSE
;
1702 DbgPrint("\x1b[18t");
1704 while ((i
-- > 0) && ((c
= KdbpTryGetCharSerial()) == -1));
1708 while ((i
-- > 0) && ((c
= KdbpTryGetCharSerial()) == -1));
1715 while ((i
-- > 0) && ((c
= KdbpTryGetCharSerial()) == -1));
1718 Buffer
[Length
++] = c
;
1719 if (isalpha(c
) || Length
>= (sizeof (Buffer
) - 1))
1722 Buffer
[Length
] = '\0';
1723 if (Buffer
[0] == '8' && Buffer
[1] == ';')
1725 for (i
= 2; (i
< Length
) && (Buffer
[i
] != ';'); i
++);
1726 if (Buffer
[i
] == ';')
1729 /* Number of rows is now at Buffer + 2 and number of cols at Buffer + i */
1730 KdbNumberOfRowsTerminal
= strtoul(Buffer
+ 2, NULL
, 0);
1731 KdbNumberOfColsTerminal
= strtoul(Buffer
+ i
, NULL
, 0);
1732 TerminalReportsSize
= TRUE
;
1739 if (KdbNumberOfRowsTerminal
<= 0)
1741 /* Set number of rows to the default. */
1742 KdbNumberOfRowsTerminal
= 24;
1744 else if (KdbNumberOfColsTerminal
<= 0)
1746 /* Set number of cols to the default. */
1747 KdbNumberOfColsTerminal
= 80;
1751 /* Get the string */
1752 va_start(ap
, Format
);
1753 Length
= _vsnprintf(Buffer
, sizeof (Buffer
) - 1, Format
, ap
);
1754 Buffer
[Length
] = '\0';
1758 while (p
[0] != '\0')
1760 i
= strcspn(p
, "\n");
1762 /* Calculate the number of lines which will be printed in the terminal
1763 * when outputting the current line
1766 RowsPrintedByTerminal
= (i
+ KdbNumberOfColsPrinted
- 1) / KdbNumberOfColsTerminal
;
1768 RowsPrintedByTerminal
= 0;
1770 RowsPrintedByTerminal
++;
1772 /*DbgPrint("!%d!%d!%d!%d!", KdbNumberOfRowsPrinted, KdbNumberOfColsPrinted, i, RowsPrintedByTerminal);*/
1774 /* Display a prompt if we printed one screen full of text */
1775 if ((KdbNumberOfRowsPrinted
+ RowsPrintedByTerminal
) >= KdbNumberOfRowsTerminal
)
1777 if (KdbNumberOfColsPrinted
> 0)
1779 DbgPrint("--- Press q to abort, any other key to continue ---");
1780 while ((c
= KdbpTryGetCharSerial()) == -1);
1783 /* Ignore \r and wait for \n or another \r - if \n is not received here
1784 * it will be interpreted as "return" when the next command should be read.
1786 while ((c
= KdbpTryGetCharSerial()) == -1);
1791 KdbOutputAborted
= TRUE
;
1794 KdbNumberOfRowsPrinted
= 0;
1795 KdbNumberOfColsPrinted
= 0;
1798 /* Insert a NUL after the line and print only the current line. */
1799 if (p
[i
] == '\n' && p
[i
+ 1] != '\0')
1814 /* Set p to the start of the next line and
1815 * remember the number of rows/cols printed
1821 KdbNumberOfColsPrinted
= 0;
1825 ASSERT(p
[0] == '\0');
1826 KdbNumberOfColsPrinted
+= i
;
1828 KdbNumberOfRowsPrinted
+= RowsPrintedByTerminal
;
1832 /*!\brief Appends a command to the command history
1834 * \param Command Pointer to the command to append to the history.
1837 KdbpCommandHistoryAppend(
1840 LONG Length1
= strlen(Command
) + 1;
1845 ASSERT(Length1
<= RTL_NUMBER_OF(KdbCommandHistoryBuffer
));
1848 (KdbCommandHistory
[KdbCommandHistoryIndex
] != NULL
&&
1849 strcmp(KdbCommandHistory
[KdbCommandHistoryIndex
], Command
) == 0))
1854 /* Calculate Length1 and Length2 */
1855 Buffer
= KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
;
1856 KdbCommandHistoryBufferIndex
+= Length1
;
1857 if (KdbCommandHistoryBufferIndex
>= RTL_NUMBER_OF(KdbCommandHistoryBuffer
))
1859 KdbCommandHistoryBufferIndex
-= RTL_NUMBER_OF(KdbCommandHistoryBuffer
);
1860 Length2
= KdbCommandHistoryBufferIndex
;
1864 /* Remove previous commands until there is enough space to append the new command */
1865 for (i
= KdbCommandHistoryIndex
; KdbCommandHistory
[i
] != NULL
;)
1868 (KdbCommandHistory
[i
] >= Buffer
||
1869 KdbCommandHistory
[i
] < (KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
))) ||
1871 (KdbCommandHistory
[i
] >= Buffer
&&
1872 KdbCommandHistory
[i
] < (KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
))))
1874 KdbCommandHistory
[i
] = NULL
;
1878 i
= RTL_NUMBER_OF(KdbCommandHistory
) - 1;
1879 if (i
== KdbCommandHistoryIndex
)
1883 /* Make sure the new command history entry is free */
1884 KdbCommandHistoryIndex
++;
1885 KdbCommandHistoryIndex
%= RTL_NUMBER_OF(KdbCommandHistory
);
1886 if (KdbCommandHistory
[KdbCommandHistoryIndex
] != NULL
)
1888 KdbCommandHistory
[KdbCommandHistoryIndex
] = NULL
;
1891 /* Append command */
1892 KdbCommandHistory
[KdbCommandHistoryIndex
] = Buffer
;
1893 ASSERT((KdbCommandHistory
[KdbCommandHistoryIndex
] + Length1
) <= KdbCommandHistoryBuffer
+ RTL_NUMBER_OF(KdbCommandHistoryBuffer
));
1894 memcpy(KdbCommandHistory
[KdbCommandHistoryIndex
], Command
, Length1
);
1897 memcpy(KdbCommandHistoryBuffer
, Command
+ Length1
, Length2
);
1901 /*!\brief Reads a line of user-input.
1903 * \param Buffer Buffer to store the input into. Trailing newlines are removed.
1904 * \param Size Size of \a Buffer.
1906 * \note Accepts only \n newlines, \r is ignored.
1914 PCHAR Orig
= Buffer
;
1917 STATIC CHAR LastCommand
[1024] = "";
1918 STATIC CHAR LastKey
= '\0';
1919 INT CmdHistIndex
= -1;
1922 EchoOn
= !((KdDebugState
& KD_DEBUG_KDNOECHO
) != 0);
1926 if (KdDebugState
& KD_DEBUG_KDSERIAL
)
1928 while ((Key
= KdbpTryGetCharSerial()) == -1);
1930 if (Key
== KEY_ESC
) /* ESC */
1932 while ((Key
= KdbpTryGetCharSerial()) == -1);
1935 while ((Key
= KdbpTryGetCharSerial()) == -1);
1939 ScanCode
= KEY_SCAN_UP
;
1942 ScanCode
= KEY_SCAN_DOWN
;
1953 while ((Key
= KdbpTryGetCharKeyboard(&ScanCode
)) == -1);
1955 if ((Buffer
- Orig
) >= (Size
- 1))
1957 /* Buffer is full, accept only newlines */
1964 /* Ignore this key... */
1966 else if (Key
== '\n')
1970 * Repeat the last command if the user presses enter. Reduces the
1971 * risk of RSI when single-stepping.
1975 strncpy(Buffer
, LastCommand
, Size
);
1976 Buffer
[Size
- 1] = '\0';
1981 strncpy(LastCommand
, Orig
, sizeof (LastCommand
));
1982 LastCommand
[sizeof (LastCommand
) - 1] = '\0';
1987 else if (Key
== KEY_BS
|| Key
== KEY_DEL
)
1994 DbgPrint("%c %c", KEY_BS
, KEY_BS
);
1996 DbgPrint(" %c", KEY_BS
);
1999 else if (ScanCode
== KEY_SCAN_UP
)
2001 BOOLEAN Print
= TRUE
;
2002 if (CmdHistIndex
< 0)
2003 CmdHistIndex
= KdbCommandHistoryIndex
;
2006 i
= CmdHistIndex
- 1;
2008 CmdHistIndex
= RTL_NUMBER_OF(KdbCommandHistory
) - 1;
2009 if (KdbCommandHistory
[i
] != NULL
&& i
!= KdbCommandHistoryIndex
)
2014 if (Print
&& KdbCommandHistory
[CmdHistIndex
] != NULL
)
2016 while (Buffer
> Orig
)
2021 DbgPrint("%c %c", KEY_BS
, KEY_BS
);
2023 DbgPrint(" %c", KEY_BS
);
2025 i
= min(strlen(KdbCommandHistory
[CmdHistIndex
]), Size
- 1);
2026 memcpy(Orig
, KdbCommandHistory
[CmdHistIndex
], i
);
2029 DbgPrint("%s", Orig
);
2032 else if (ScanCode
== KEY_SCAN_DOWN
)
2034 if (CmdHistIndex
> 0 && CmdHistIndex
!= KdbCommandHistoryIndex
)
2036 i
= CmdHistIndex
+ 1;
2037 if (i
>= RTL_NUMBER_OF(KdbCommandHistory
))
2039 if (KdbCommandHistory
[i
] != NULL
)
2042 while (Buffer
> Orig
)
2047 DbgPrint("%c %c", KEY_BS
, KEY_BS
);
2049 DbgPrint(" %c", KEY_BS
);
2051 i
= min(strlen(KdbCommandHistory
[CmdHistIndex
]), Size
- 1);
2052 memcpy(Orig
, KdbCommandHistory
[CmdHistIndex
], i
);
2055 DbgPrint("%s", Orig
);
2062 DbgPrint("%c", Key
);
2071 /*!\brief Parses command line and executes command if found
2073 * \param Command Command line to parse and execute if possible.
2075 * \retval TRUE Don't continue execution.
2076 * \retval FALSE Continue execution (leave KDB)
2085 STATIC PCH Argv
[256];
2086 STATIC CHAR OrigCommand
[1024];
2088 strncpy(OrigCommand
, Command
, sizeof(OrigCommand
) - 1);
2089 OrigCommand
[sizeof(OrigCommand
) - 1] = '\0';
2095 while (*p
== '\t' || *p
== ' ')
2100 i
= strcspn(p
, "\t ");
2111 for (i
= 0; i
< RTL_NUMBER_OF(KdbDebuggerCommands
); i
++)
2113 if (KdbDebuggerCommands
[i
].Name
== NULL
)
2116 if (strcmp(KdbDebuggerCommands
[i
].Name
, Argv
[0]) == 0)
2118 return KdbDebuggerCommands
[i
].Fn(Argc
, Argv
);
2122 KdbpPrint("Command '%s' is unknown.\n", OrigCommand
);
2126 /*!\brief KDB Main Loop.
2128 * \param EnteredOnSingleStep TRUE if KDB was entered on single step.
2132 IN BOOLEAN EnteredOnSingleStep
)
2134 STATIC CHAR Command
[1024];
2137 if (EnteredOnSingleStep
)
2139 if (!KdbSymPrintAddress((PVOID
)KdbCurrentTrapFrame
->Tf
.Eip
))
2141 DbgPrint("<%x>", KdbCurrentTrapFrame
->Tf
.Eip
);
2144 if (KdbpDisassemble(KdbCurrentTrapFrame
->Tf
.Eip
, KdbUseIntelSyntax
) < 0)
2146 DbgPrint("<INVALID>");
2153 /* Print the prompt */
2156 /* Read a command and remember it */
2157 KdbpReadCommand(Command
, sizeof (Command
));
2158 KdbpCommandHistoryAppend(Command
);
2160 /* Reset the number of rows/cols printed and output aborted state */
2161 KdbNumberOfRowsPrinted
= KdbNumberOfColsPrinted
= 0;
2162 KdbOutputAborted
= FALSE
;
2164 /* Call the command */
2165 Continue
= KdbpDoCommand(Command
);
2169 /*!\brief Called when a module is loaded.
2171 * \param Name Filename of the module which was loaded.
2174 KdbpCliModuleLoaded(IN PUNICODE_STRING Name
)
2178 DbgPrint("Module %wZ loaded.\n", Name
);
2179 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
2182 /*!\brief This function is called by KdbEnterDebuggerException...
2184 * Used to interpret the init file in a context with a trapframe setup
2185 * (KdbpCliInit call KdbEnter which will call KdbEnterDebuggerException which will
2186 * call this function if KdbInitFileBuffer is not NULL.
2189 KdbpCliInterpretInitFile()
2195 /* Execute the commands in the init file */
2196 DbgPrint("KDB: Executing KDB.init file...\n");
2197 p1
= KdbInitFileBuffer
;
2198 while (p1
[0] != '\0')
2200 i
= strcspn(p1
, "\r\n");
2206 /* Look for "break" command and comments */
2208 while (isspace(p2
[0]))
2210 if (strncmp(p2
, "break", sizeof("break")-1) == 0 &&
2211 (p2
[sizeof("break")-1] == '\0' || isspace(p2
[sizeof("break")-1])))
2213 /* break into the debugger */
2214 KdbpCliMainLoop(FALSE
);
2216 else if (p2
[0] != '#' && p2
[0] != '\0') /* Ignore empty lines and comments */
2224 while (p1
[0] == '\r' || p1
[0] == '\n')
2227 DbgPrint("KDB: KDB.init executed\n");
2230 /*!\brief Called when KDB is initialized
2232 * Reads the KDB.init file from the SystemRoot\system32\drivers\etc directory and executes it.
2238 OBJECT_ATTRIBUTES ObjectAttributes
;
2239 UNICODE_STRING FileName
;
2240 IO_STATUS_BLOCK Iosb
;
2241 FILE_STANDARD_INFORMATION FileStdInfo
;
2242 HANDLE hFile
= NULL
;
2247 /* Initialize the object attributes */
2248 RtlInitUnicodeString(&FileName
, L
"\\SystemRoot\\system32\\drivers\\etc\\KDB.init");
2249 InitializeObjectAttributes(&ObjectAttributes
, &FileName
, 0, NULL
, NULL
);
2252 Status
= ZwOpenFile(&hFile
, FILE_READ_DATA
, &ObjectAttributes
, &Iosb
, 0,
2253 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
|
2254 FILE_NO_INTERMEDIATE_BUFFERING
);
2255 if (!NT_SUCCESS(Status
))
2257 DPRINT("Could not open \\SystemRoot\\system32\\drivers\\etc\\KDB.init (Status 0x%x)", Status
);
2261 /* Get the size of the file */
2262 Status
= ZwQueryInformationFile(hFile
, &Iosb
, &FileStdInfo
, sizeof (FileStdInfo
),
2263 FileStandardInformation
);
2264 if (!NT_SUCCESS(Status
))
2267 DPRINT("Could not query size of \\SystemRoot\\system32\\drivers\\etc\\KDB.init (Status 0x%x)", Status
);
2270 FileSize
= FileStdInfo
.EndOfFile
.u
.LowPart
;
2272 /* Allocate memory for the file */
2273 FileBuffer
= ExAllocatePool(PagedPool
, FileSize
+ 1); /* add 1 byte for terminating '\0' */
2274 if (FileBuffer
== NULL
)
2277 DPRINT("Could not allocate %d bytes for KDB.init file\n", FileSize
);
2281 /* Load file into memory */
2282 Status
= ZwReadFile(hFile
, 0, 0, 0, &Iosb
, FileBuffer
, FileSize
, 0, 0);
2284 if (!NT_SUCCESS(Status
) && Status
!= STATUS_END_OF_FILE
)
2286 ExFreePool(FileBuffer
);
2287 DPRINT("Could not read KDB.init file into memory (Status 0x%lx)\n", Status
);
2290 FileSize
= min(FileSize
, Iosb
.Information
);
2291 FileBuffer
[FileSize
] = '\0';
2293 /* Enter critical section */
2294 Ke386SaveFlags(OldEflags
);
2295 Ke386DisableInterrupts();
2297 /* Interpret the init file... */
2298 KdbInitFileBuffer
= FileBuffer
;
2300 KdbInitFileBuffer
= NULL
;
2302 /* Leave critical section */
2303 Ke386RestoreFlags(OldEflags
);
2305 ExFreePool(FileBuffer
);