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 KDBinit 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
;
878 PCHAR State
, pend
, str1
, str2
;
879 STATIC CONST PCHAR ThreadStateToString
[THREAD_STATE_MAX
] =
880 { "Initialized", "Ready", "Running",
881 "Suspended", "Frozen", "Terminated1",
882 "Terminated2", "Blocked" };
883 ASSERT(KdbCurrentProcess
!= NULL
);
885 if (Argc
>= 2 && _stricmp(Argv
[1], "list") == 0)
887 Process
= KdbCurrentProcess
;
891 ul
= strtoul(Argv
[2], &pend
, 0);
894 KdbpPrint("thread: '%s' is not a valid process id!\n", Argv
[2]);
897 if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID
)ul
, &Process
)))
899 KdbpPrint("thread: Invalid process id!\n");
904 Entry
= Process
->ThreadListHead
.Flink
;
905 if (Entry
== &Process
->ThreadListHead
)
908 KdbpPrint("No threads in process 0x%08x!\n", ul
);
910 KdbpPrint("No threads in current process!\n");
914 KdbpPrint(" TID State Prior. Affinity EBP EIP\n");
917 Thread
= CONTAINING_RECORD(Entry
, ETHREAD
, ThreadListEntry
);
919 if (Thread
== KdbCurrentThread
)
930 if (Thread
->Tcb
.TrapFrame
!= NULL
)
932 Esp
= (PULONG
)Thread
->Tcb
.TrapFrame
->Esp
;
933 Ebp
= (PULONG
)Thread
->Tcb
.TrapFrame
->Ebp
;
934 Eip
= Thread
->Tcb
.TrapFrame
->Eip
;
938 Esp
= (PULONG
)Thread
->Tcb
.KernelStack
;
939 Ebp
= (PULONG
)Esp
[4];
941 if (Ebp
!= NULL
) /* FIXME: Should we attach to the process to read Ebp[1]? */
942 KdbpSafeReadMemory(&Eip
, Ebp
+ 1, sizeof (Eip
));;
944 if (Thread
->Tcb
.State
< THREAD_STATE_MAX
)
945 State
= ThreadStateToString
[Thread
->Tcb
.State
];
949 KdbpPrint(" %s0x%08x %-11s %3d 0x%08x 0x%08x 0x%08x%s\n",
951 Thread
->Cid
.UniqueThread
,
953 Thread
->Tcb
.Priority
,
954 Thread
->Tcb
.Affinity
,
959 Entry
= Entry
->Flink
;
961 while (Entry
!= &Process
->ThreadListHead
);
963 else if (Argc
>= 2 && _stricmp(Argv
[1], "attach") == 0)
967 KdbpPrint("thread attach: thread id argument required!\n");
971 ul
= strtoul(Argv
[2], &pend
, 0);
974 KdbpPrint("thread attach: '%s' is not a valid thread id!\n", Argv
[2]);
977 if (!KdbpAttachToThread((PVOID
)ul
))
981 KdbpPrint("Attached to thread 0x%08x.\n", ul
);
985 Thread
= KdbCurrentThread
;
989 ul
= strtoul(Argv
[1], &pend
, 0);
992 KdbpPrint("thread: '%s' is not a valid thread id!\n", Argv
[1]);
995 if (!NT_SUCCESS(PsLookupThreadByThreadId((PVOID
)ul
, &Thread
)))
997 KdbpPrint("thread: Invalid thread id!\n");
1002 if (Thread
->Tcb
.State
< THREAD_STATE_MAX
)
1003 State
= ThreadStateToString
[Thread
->Tcb
.State
];
1008 " State: %s (0x%x)\n"
1010 " Affinity: 0x%08x\n"
1011 " Initial Stack: 0x%08x\n"
1012 " Stack Limit: 0x%08x\n"
1013 " Stack Base: 0x%08x\n"
1014 " Kernel Stack: 0x%08x\n"
1015 " Trap Frame: 0x%08x\n"
1016 " NPX State: %s (0x%x)\n",
1017 (Argc
< 2) ? "Current Thread:\n" : "",
1018 Thread
->Cid
.UniqueThread
,
1019 State
, Thread
->Tcb
.State
,
1020 Thread
->Tcb
.Priority
,
1021 Thread
->Tcb
.Affinity
,
1022 Thread
->Tcb
.InitialStack
,
1023 Thread
->Tcb
.StackLimit
,
1024 Thread
->Tcb
.StackBase
,
1025 Thread
->Tcb
.KernelStack
,
1026 Thread
->Tcb
.TrapFrame
,
1027 NPX_STATE_TO_STRING(Thread
->Tcb
.NpxState
), Thread
->Tcb
.NpxState
);
1034 /*!\brief Lists processes or switches to another process context.
1037 KdbpCmdProc(ULONG Argc
, PCHAR Argv
[])
1041 PCHAR State
, pend
, str1
, str2
;
1043 extern LIST_ENTRY PsActiveProcessHead
;
1045 if (Argc
>= 2 && _stricmp(Argv
[1], "list") == 0)
1047 Entry
= PsActiveProcessHead
.Flink
;
1048 if (Entry
== &PsActiveProcessHead
)
1050 KdbpPrint("No processes in the system!\n");
1054 KdbpPrint(" PID State Filename\n");
1057 Process
= CONTAINING_RECORD(Entry
, EPROCESS
, ProcessListEntry
);
1059 if (Process
== KdbCurrentProcess
)
1070 State
= ((Process
->Pcb
.State
== PROCESS_STATE_TERMINATED
) ? "Terminated" :
1071 ((Process
->Pcb
.State
== PROCESS_STATE_ACTIVE
) ? "Active" : "Unknown"));
1073 KdbpPrint(" %s0x%08x %-10s %s%s\n",
1075 Process
->UniqueProcessId
,
1077 Process
->ImageFileName
,
1080 Entry
= Entry
->Flink
;
1082 while(Entry
!= &PsActiveProcessHead
);
1084 else if (Argc
>= 2 && _stricmp(Argv
[1], "attach") == 0)
1088 KdbpPrint("process attach: process id argument required!\n");
1092 ul
= strtoul(Argv
[2], &pend
, 0);
1093 if (Argv
[2] == pend
)
1095 KdbpPrint("process attach: '%s' is not a valid process id!\n", Argv
[2]);
1098 if (!KdbpAttachToProcess((PVOID
)ul
))
1102 KdbpPrint("Attached to process 0x%08x, thread 0x%08x.\n", (UINT
)ul
,
1103 (UINT
)KdbCurrentThread
->Cid
.UniqueThread
);
1107 Process
= KdbCurrentProcess
;
1111 ul
= strtoul(Argv
[1], &pend
, 0);
1112 if (Argv
[1] == pend
)
1114 KdbpPrint("proc: '%s' is not a valid process id!\n", Argv
[1]);
1117 if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID
)ul
, &Process
)))
1119 KdbpPrint("proc: Invalid process id!\n");
1124 State
= ((Process
->Pcb
.State
== PROCESS_STATE_TERMINATED
) ? "Terminated" :
1125 ((Process
->Pcb
.State
== PROCESS_STATE_ACTIVE
) ? "Active" : "Unknown"));
1128 " State: %s (0x%x)\n"
1129 " Image Filename: %s\n",
1130 (Argc
< 2) ? "Current process:\n" : "",
1131 Process
->UniqueProcessId
,
1132 State
, Process
->Pcb
.State
,
1133 Process
->ImageFileName
);
1139 /*!\brief Lists loaded modules or the one containing the specified address.
1142 KdbpCmdMod(ULONG Argc
, PCHAR Argv
[])
1144 ULONGLONG Result
= 0;
1146 KDB_MODULE_INFO Info
;
1147 BOOLEAN DisplayOnlyOneModule
= FALSE
;
1152 /* Put the arguments back together */
1155 Argv
[Argc
][strlen(Argv
[Argc
])] = ' ';
1157 /* Evaluate the expression */
1158 if (!KdbpEvaluateExpression(Argv
[1], sizeof("kdb:> ")-1 + (Argv
[1]-Argv
[0]), &Result
))
1162 if (Result
> (ULONGLONG
)(~((ULONG_PTR
)0)))
1163 KdbpPrint("%s: Warning: Address %I64x is beeing truncated\n", Argv
[0]);
1164 Address
= (ULONG_PTR
)Result
;
1166 if (!KdbpSymFindModuleByAddress((PVOID
)Address
, &Info
))
1168 KdbpPrint("No module containing address 0x%x found!\n", Address
);
1171 DisplayOnlyOneModule
= TRUE
;
1175 if (!KdbpSymFindModuleByIndex(0, &Info
))
1177 KdbpPrint("No modules.\n");
1183 KdbpPrint(" Base Size Name\n");
1186 KdbpPrint(" %08x %08x %ws\n", Info
.Base
, Info
.Size
, Info
.Name
);
1188 if ((!DisplayOnlyOneModule
&& !KdbpSymFindModuleByIndex(i
++, &Info
)) ||
1189 DisplayOnlyOneModule
)
1198 /*!\brief Displays GDT, LDT or IDTd.
1201 KdbpCmdGdtLdtIdt(ULONG Argc
, PCHAR Argv
[])
1203 struct __attribute__((packed
)) {
1216 if (Argv
[0][0] == 'i')
1219 asm volatile("sidt %0" : : "m"(Reg
));
1223 KdbpPrint("Interrupt descriptor table is empty.\n");
1226 KdbpPrint("IDT Base: 0x%08x Limit: 0x%04x\n", Reg
.Base
, Reg
.Limit
);
1227 KdbpPrint(" Idx Type Seg. Sel. Offset DPL\n");
1228 for (i
= 0; (i
+ sizeof(SegDesc
) - 1) <= Reg
.Limit
; i
+= 8)
1230 if (!NT_SUCCESS(KdbpSafeReadMemory(SegDesc
, (PVOID
)(Reg
.Base
+ i
), sizeof(SegDesc
))))
1232 KdbpPrint("Couldn't access memory at 0x%08x!\n", Reg
.Base
+ i
);
1236 Dpl
= ((SegDesc
[1] >> 13) & 3);
1237 if ((SegDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
1238 SegType
= "TASKGATE";
1239 else if ((SegDesc
[1] & 0x1fe0) == 0x0e00) /* 32 bit Interrupt gate */
1240 SegType
= "INTGATE32";
1241 else if ((SegDesc
[1] & 0x1fe0) == 0x0600) /* 16 bit Interrupt gate */
1242 SegType
= "INTGATE16";
1243 else if ((SegDesc
[1] & 0x1fe0) == 0x0f00) /* 32 bit Trap gate */
1244 SegType
= "TRAPGATE32";
1245 else if ((SegDesc
[1] & 0x1fe0) == 0x0700) /* 16 bit Trap gate */
1246 SegType
= "TRAPGATE16";
1248 SegType
= "UNKNOWN";
1250 if ((SegDesc
[1] & (1 << 15)) == 0) /* not present */
1252 KdbpPrint(" %03d %-10s [NP] [NP] %02d\n",
1253 i
/ 8, SegType
, Dpl
);
1255 else if ((SegDesc
[1] & 0x1f00) == 0x0500) /* Task gate */
1257 SegSel
= SegDesc
[0] >> 16;
1258 KdbpPrint(" %03d %-10s 0x%04x %02d\n",
1259 i
/ 8, SegType
, SegSel
, Dpl
);
1263 SegSel
= SegDesc
[0] >> 16;
1264 SegBase
= (SegDesc
[1] & 0xffff0000) | (SegDesc
[0] & 0x0000ffff);
1265 KdbpPrint(" %03d %-10s 0x%04x 0x%08x %02d\n",
1266 i
/ 8, SegType
, SegSel
, SegBase
, Dpl
);
1273 if (Argv
[0][0] == 'g')
1276 asm volatile("sgdt %0" : : "m"(Reg
));
1281 ASSERT(Argv
[0][0] == 'l');
1283 asm volatile("sldt %0" : : "m"(Reg
));
1290 KdbpPrint("%s descriptor table is empty.\n",
1291 Argv
[0][0] == 'g' ? "Global" : "Local");
1294 KdbpPrint("%cDT Base: 0x%08x Limit: 0x%04x\n",
1295 Argv
[0][0] == 'g' ? 'G' : 'L', Reg
.Base
, Reg
.Limit
);
1296 KdbpPrint(" Idx Sel. Type Base Limit DPL Attribs\n");
1297 for ( ; (i
+ sizeof(SegDesc
) - 1) <= Reg
.Limit
; i
+= 8)
1299 if (!NT_SUCCESS(KdbpSafeReadMemory(SegDesc
, (PVOID
)(Reg
.Base
+ i
), sizeof(SegDesc
))))
1301 KdbpPrint("Couldn't access memory at 0x%08x!\n", Reg
.Base
+ i
);
1304 Dpl
= ((SegDesc
[1] >> 13) & 3);
1305 Type
= ((SegDesc
[1] >> 8) & 0xf);
1307 SegBase
= SegDesc
[0] >> 16;
1308 SegBase
|= (SegDesc
[1] & 0xff) << 16;
1309 SegBase
|= SegDesc
[1] & 0xff000000;
1310 SegLimit
= SegDesc
[0] & 0x0000ffff;
1311 SegLimit
|= (SegDesc
[1] >> 16) & 0xf;
1312 if ((SegDesc
[1] & (1 << 23)) != 0)
1322 if ((SegDesc
[1] & (1 << 12)) == 0) /* System segment */
1326 case 1: SegType
= "TSS16(Avl)"; break;
1327 case 2: SegType
= "LDT"; break;
1328 case 3: SegType
= "TSS16(Busy)"; break;
1329 case 4: SegType
= "CALLGATE16"; break;
1330 case 5: SegType
= "TASKGATE"; break;
1331 case 6: SegType
= "INTGATE16"; break;
1332 case 7: SegType
= "TRAPGATE16"; break;
1333 case 9: SegType
= "TSS32(Avl)"; break;
1334 case 11: SegType
= "TSS32(Busy)"; break;
1335 case 12: SegType
= "CALLGATE32"; break;
1336 case 14: SegType
= "INTGATE32"; break;
1337 case 15: SegType
= "INTGATE32"; break;
1338 default: SegType
= "UNKNOWN"; break;
1340 if (!(Type
>= 1 && Type
<= 3) &&
1341 Type
!= 9 && Type
!= 11)
1347 else if ((SegDesc
[1] & (1 << 11)) == 0) /* Data segment */
1349 if ((SegDesc
[1] & (1 << 22)) != 0)
1355 else /* Code segment */
1357 if ((SegDesc
[1] & (1 << 22)) != 0)
1363 if ((SegDesc
[1] & (1 << 15)) == 0) /* not present */
1365 KdbpPrint(" %03d 0x%04x %-11s [NP] [NP] %02d NP\n",
1366 i
/ 8, i
| Dpl
| ul
, SegType
, Dpl
);
1370 KdbpPrint(" %03d 0x%04x %-11s 0x%08x 0x%08x %02d ",
1371 i
/ 8, i
| Dpl
| ul
, SegType
, SegBase
, SegLimit
, Dpl
);
1372 if ((SegDesc
[1] & (1 << 12)) == 0) /* System segment */
1374 /* FIXME: Display system segment */
1376 else if ((SegDesc
[1] & (1 << 11)) == 0) /* Data segment */
1378 if ((SegDesc
[1] & (1 << 10)) != 0) /* Expand-down */
1380 KdbpPrint((SegDesc
[1] & (1 << 9)) ? " R/W" : " R");
1381 if ((SegDesc
[1] & (1 << 8)) != 0)
1384 else /* Code segment */
1386 if ((SegDesc
[1] & (1 << 10)) != 0) /* Conforming */
1388 KdbpPrint((SegDesc
[1] & (1 << 9)) ? " R/X" : " X");
1389 if ((SegDesc
[1] & (1 << 8)) != 0)
1392 if ((SegDesc
[1] & (1 << 20)) != 0)
1402 /*!\brief Displays the KPCR
1405 KdbpCmdPcr(ULONG Argc
, PCHAR Argv
[])
1407 PKPCR Pcr
= KeGetCurrentKPCR();
1409 KdbpPrint("Current PCR is at 0x%08x.\n", (INT
)Pcr
);
1410 KdbpPrint(" Tib.ExceptionList: 0x%08x\n"
1411 " Tib.StackBase: 0x%08x\n"
1412 " Tib.StackLimit: 0x%08x\n"
1413 " Tib.SubSystemTib: 0x%08x\n"
1414 " Tib.FiberData/Version: 0x%08x\n"
1415 " Tib.ArbitraryUserPointer: 0x%08x\n"
1416 " Tib.Self: 0x%08x\n"
1421 " IrrActive: 0x%08x\n"
1423 " KdVersionBlock: 0x%08x\n"
1427 " MajorVersion: 0x%04x\n"
1428 " MinorVersion: 0x%04x\n"
1429 " SetMember: 0x%08x\n"
1430 " StallScaleFactor: 0x%08x\n"
1431 " DebugActive: 0x%02x\n"
1432 " ProcessorNumber: 0x%02x\n"
1433 " L2CacheAssociativity: 0x%02x\n"
1434 " VdmAlert: 0x%08x\n"
1435 " L2CacheSize: 0x%08x\n"
1436 " InterruptMode: 0x%08x\n",
1437 Pcr
->Tib
.ExceptionList
, Pcr
->Tib
.StackBase
, Pcr
->Tib
.StackLimit
,
1438 Pcr
->Tib
.SubSystemTib
, Pcr
->Tib
.FiberData
, Pcr
->Tib
.ArbitraryUserPointer
,
1439 Pcr
->Tib
.Self
, Pcr
->Self
, Pcr
->PCRCB
, Pcr
->Irql
, Pcr
->IRR
, Pcr
->IrrActive
,
1440 Pcr
->IDR
, Pcr
->KdVersionBlock
, Pcr
->IDT
, Pcr
->GDT
, Pcr
->TSS
,
1441 Pcr
->MajorVersion
, Pcr
->MinorVersion
, Pcr
->SetMember
, Pcr
->StallScaleFactor
,
1442 Pcr
->DebugActive
, Pcr
->ProcessorNumber
, Pcr
->L2CacheAssociativity
,
1443 Pcr
->VdmAlert
, Pcr
->L2CacheSize
, Pcr
->InterruptMode
);
1448 /*!\brief Displays the TSS
1451 KdbpCmdTss(ULONG Argc
, PCHAR Argv
[])
1453 KTSS
*Tss
= KeGetCurrentKPCR()->TSS
;
1455 KdbpPrint("Current TSS is at 0x%08x.\n", (INT
)Tss
);
1456 KdbpPrint(" PreviousTask: 0x%08x\n"
1457 " Ss0:Esp0: 0x%04x:0x%08x\n"
1458 " Ss1:Esp1: 0x%04x:0x%08x\n"
1459 " Ss2:Esp2: 0x%04x:0x%08x\n"
1479 " IoMapBase: 0x%04x\n",
1480 Tss
->PreviousTask
, Tss
->Ss0
, Tss
->Esp0
, Tss
->Ss1
, Tss
->Esp1
,
1481 Tss
->Ss2
, Tss
->Esp2
, Tss
->Cr3
, Tss
->Eip
, Tss
->Eflags
, Tss
->Eax
,
1482 Tss
->Ecx
, Tss
->Edx
, Tss
->Ebx
, Tss
->Esp
, Tss
->Ebp
, Tss
->Esi
,
1483 Tss
->Edi
, Tss
->Es
, Tss
->Cs
, Tss
->Ss
, Tss
->Ds
, Tss
->Fs
, Tss
->Gs
,
1484 Tss
->Ldt
, Tss
->Trap
, Tss
->IoMapBase
);
1488 /*!\brief Bugchecks the system.
1491 KdbpCmdBugCheck(ULONG Argc
, PCHAR Argv
[])
1493 KEBUGCHECK(0xDEADDEAD);
1497 /*!\brief Sets or displays a config variables value.
1500 KdbpCmdSet(ULONG Argc
, PCHAR Argv
[])
1505 KDB_ENTER_CONDITION ConditionFirst
= KdbDoNotEnter
;
1506 KDB_ENTER_CONDITION ConditionLast
= KdbDoNotEnter
;
1507 STATIC CONST PCHAR ExceptionNames
[21] =
1508 { "ZERODEVIDE", "DEBUGTRAP", "NMI", "INT3", "OVERFLOW", "BOUND", "INVALIDOP",
1509 "NOMATHCOP", "DOUBLEFAULT", "RESERVED(9)", "INVALIDTSS", "SEGMENTNOTPRESENT",
1510 "STACKFAULT", "GPF", "PAGEFAULT", "RESERVED(15)", "MATHFAULT", "ALIGNMENTCHECK",
1511 "MACHINECHECK", "SIMDFAULT", "OTHERS" };
1515 KdbpPrint("Available settings:\n");
1516 KdbpPrint(" syntax [intel|at&t]\n");
1517 KdbpPrint(" condition [exception|*] [first|last] [never|always|kmode|umode]\n");
1519 else if (strcmp(Argv
[1], "syntax") == 0)
1522 KdbpPrint("syntax = %s\n", KdbUseIntelSyntax
? "intel" : "at&t");
1525 if (_stricmp(Argv
[2], "intel") == 0)
1526 KdbUseIntelSyntax
= TRUE
;
1527 else if (_stricmp(Argv
[2], "at&t") == 0)
1528 KdbUseIntelSyntax
= FALSE
;
1530 KdbpPrint("Unknown syntax '%s'.\n", Argv
[2]);
1533 else if (strcmp(Argv
[1], "condition") == 0)
1537 KdbpPrint("Conditions: (First) (Last)\n");
1538 for (l
= 0; l
< RTL_NUMBER_OF(ExceptionNames
) - 1; l
++)
1540 if (ExceptionNames
[l
] == NULL
)
1542 if (!KdbpGetEnterCondition(l
, TRUE
, &ConditionFirst
))
1544 if (!KdbpGetEnterCondition(l
, FALSE
, &ConditionLast
))
1546 KdbpPrint(" #%02d %-20s %-8s %-8s\n", l
, ExceptionNames
[l
],
1547 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1548 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1550 ASSERT(l
== (RTL_NUMBER_OF(ExceptionNames
) - 1));
1551 KdbpPrint(" %-20s %-8s %-8s\n", ExceptionNames
[l
],
1552 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1553 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1557 if (Argc
>= 5 && strcmp(Argv
[2], "*") == 0) /* Allow * only when setting condition */
1561 l
= (LONG
)strtoul(Argv
[2], &pend
, 0);
1562 if (Argv
[2] == pend
)
1564 for (l
= 0; l
< RTL_NUMBER_OF(ExceptionNames
); l
++)
1566 if (ExceptionNames
[l
] == NULL
)
1568 if (_stricmp(ExceptionNames
[l
], Argv
[2]) == 0)
1572 if (l
>= RTL_NUMBER_OF(ExceptionNames
))
1574 KdbpPrint("Unknown exception '%s'.\n", Argv
[2]);
1580 if (_stricmp(Argv
[3], "first") == 0)
1582 else if (_stricmp(Argv
[3], "last") == 0)
1586 KdbpPrint("set condition: second argument must be 'first' or 'last'\n");
1589 if (_stricmp(Argv
[4], "never") == 0)
1590 ConditionFirst
= KdbDoNotEnter
;
1591 else if (_stricmp(Argv
[4], "always") == 0)
1592 ConditionFirst
= KdbEnterAlways
;
1593 else if (_stricmp(Argv
[4], "umode") == 0)
1594 ConditionFirst
= KdbEnterFromUmode
;
1595 else if (_stricmp(Argv
[4], "kmode") == 0)
1596 ConditionFirst
= KdbEnterFromKmode
;
1599 KdbpPrint("set condition: third argument must be 'never', 'always', 'umode' or 'kmode'\n");
1602 if (!KdbpSetEnterCondition(l
, First
, ConditionFirst
))
1605 KdbpPrint("Couldn't change condition for exception #%02d\n", l
);
1607 KdbpPrint("Couldn't change condition for all exceptions\n", l
);
1610 else /* Argc >= 3 */
1612 if (!KdbpGetEnterCondition(l
, TRUE
, &ConditionFirst
))
1614 if (!KdbpGetEnterCondition(l
, FALSE
, &ConditionLast
))
1616 if (l
< (RTL_NUMBER_OF(ExceptionNames
) - 1))
1618 KdbpPrint("Condition for exception #%02d (%s): FirstChance %s LastChance %s\n",
1619 l
, ExceptionNames
[l
],
1620 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1621 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1625 KdbpPrint("Condition for all other exceptions: FirstChance %s LastChance %s\n",
1626 KDB_ENTER_CONDITION_TO_STRING(ConditionFirst
),
1627 KDB_ENTER_CONDITION_TO_STRING(ConditionLast
));
1633 KdbpPrint("Unknown setting '%s'.\n", Argv
[1]);
1638 /*!\brief Displays help screen.
1641 KdbpCmdHelp(ULONG Argc
, PCHAR Argv
[])
1645 KdbpPrint("Kernel debugger commands:\n");
1646 for (i
= 0; i
< RTL_NUMBER_OF(KdbDebuggerCommands
); i
++)
1648 if (KdbDebuggerCommands
[i
].Syntax
== NULL
) /* Command group */
1652 KdbpPrint("\x1b[7m* %s:\x1b[0m\n", KdbDebuggerCommands
[i
].Help
);
1656 KdbpPrint(" %-20s - %s\n",
1657 KdbDebuggerCommands
[i
].Syntax
,
1658 KdbDebuggerCommands
[i
].Help
);
1664 /*!\brief Prints the given string with printf-like formatting.
1666 * \param Format Format of the string/arguments.
1667 * \param ... Variable number of arguments matching the format specified in \a Format.
1669 * \note Doesn't correctly handle \\t and terminal escape sequences when calculating the
1670 * number of lines required to print a single line from the Buffer in the terminal.
1677 STATIC CHAR Buffer
[4096];
1678 STATIC BOOLEAN TerminalInitialized
= FALSE
;
1679 STATIC BOOLEAN TerminalReportsSize
= TRUE
;
1684 INT RowsPrintedByTerminal
;
1688 /* Check if the user has aborted output of the current command */
1689 if (KdbOutputAborted
)
1692 /* Initialize the terminal */
1693 if (!TerminalInitialized
)
1695 DbgPrint("\x1b[7h"); /* Enable linewrap */
1696 TerminalInitialized
= TRUE
;
1699 /* Get number of rows and columns in terminal */
1700 if ((KdbNumberOfRowsTerminal
< 0) || (KdbNumberOfColsTerminal
< 0) ||
1701 (KdbNumberOfRowsPrinted
) == 0) /* Refresh terminal size each time when number of rows printed is 0 */
1703 if ((KdDebugState
& KD_DEBUG_KDSERIAL
) && TerminalReportsSize
)
1705 /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
1706 TerminalReportsSize
= FALSE
;
1707 //DbgPrint("\x1b[18t");
1708 c
= KdbpTryGetCharSerial(10);
1711 c
= KdbpTryGetCharSerial(5);
1717 c
= KdbpTryGetCharSerial(5);
1720 Buffer
[Length
++] = c
;
1721 if (isalpha(c
) || Length
>= (sizeof (Buffer
) - 1))
1724 Buffer
[Length
] = '\0';
1725 if (Buffer
[0] == '8' && Buffer
[1] == ';')
1727 for (i
= 2; (i
< Length
) && (Buffer
[i
] != ';'); i
++);
1728 if (Buffer
[i
] == ';')
1731 /* Number of rows is now at Buffer + 2 and number of cols at Buffer + i */
1732 KdbNumberOfRowsTerminal
= strtoul(Buffer
+ 2, NULL
, 0);
1733 KdbNumberOfColsTerminal
= strtoul(Buffer
+ i
, NULL
, 0);
1734 TerminalReportsSize
= TRUE
;
1741 if (KdbNumberOfRowsTerminal
<= 0)
1743 /* Set number of rows to the default. */
1744 KdbNumberOfRowsTerminal
= 24;
1746 else if (KdbNumberOfColsTerminal
<= 0)
1748 /* Set number of cols to the default. */
1749 KdbNumberOfColsTerminal
= 80;
1753 /* Get the string */
1754 va_start(ap
, Format
);
1755 Length
= _vsnprintf(Buffer
, sizeof (Buffer
) - 1, Format
, ap
);
1756 Buffer
[Length
] = '\0';
1760 while (p
[0] != '\0')
1762 i
= strcspn(p
, "\n");
1764 /* Calculate the number of lines which will be printed in the terminal
1765 * when outputting the current line
1768 RowsPrintedByTerminal
= (i
+ KdbNumberOfColsPrinted
- 1) / KdbNumberOfColsTerminal
;
1770 RowsPrintedByTerminal
= 0;
1772 RowsPrintedByTerminal
++;
1774 /*DbgPrint("!%d!%d!%d!%d!", KdbNumberOfRowsPrinted, KdbNumberOfColsPrinted, i, RowsPrintedByTerminal);*/
1776 /* Display a prompt if we printed one screen full of text */
1777 if ((KdbNumberOfRowsPrinted
+ RowsPrintedByTerminal
) >= KdbNumberOfRowsTerminal
)
1779 if (KdbNumberOfColsPrinted
> 0)
1781 DbgPrint("--- Press q to abort, any other key to continue ---");
1782 if (KdDebugState
& KD_DEBUG_KDSERIAL
)
1783 c
= KdbpGetCharSerial();
1785 c
= KdbpGetCharKeyboard(&ScanCode
);
1788 /* Try to read '\n' which might follow '\r' - if \n is not received here
1789 * it will be interpreted as "return" when the next command should be read.
1791 if (KdDebugState
& KD_DEBUG_KDSERIAL
)
1792 c
= KdbpTryGetCharSerial(5);
1794 c
= KdbpTryGetCharKeyboard(&ScanCode
, 5);
1799 KdbOutputAborted
= TRUE
;
1802 KdbNumberOfRowsPrinted
= 0;
1803 KdbNumberOfColsPrinted
= 0;
1806 /* Insert a NUL after the line and print only the current line. */
1807 if (p
[i
] == '\n' && p
[i
+ 1] != '\0')
1822 /* Set p to the start of the next line and
1823 * remember the number of rows/cols printed
1829 KdbNumberOfColsPrinted
= 0;
1833 ASSERT(p
[0] == '\0');
1834 KdbNumberOfColsPrinted
+= i
;
1836 KdbNumberOfRowsPrinted
+= RowsPrintedByTerminal
;
1840 /*!\brief Appends a command to the command history
1842 * \param Command Pointer to the command to append to the history.
1845 KdbpCommandHistoryAppend(
1848 LONG Length1
= strlen(Command
) + 1;
1853 ASSERT(Length1
<= RTL_NUMBER_OF(KdbCommandHistoryBuffer
));
1856 (KdbCommandHistory
[KdbCommandHistoryIndex
] != NULL
&&
1857 strcmp(KdbCommandHistory
[KdbCommandHistoryIndex
], Command
) == 0))
1862 /* Calculate Length1 and Length2 */
1863 Buffer
= KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
;
1864 KdbCommandHistoryBufferIndex
+= Length1
;
1865 if (KdbCommandHistoryBufferIndex
>= RTL_NUMBER_OF(KdbCommandHistoryBuffer
))
1867 KdbCommandHistoryBufferIndex
-= RTL_NUMBER_OF(KdbCommandHistoryBuffer
);
1868 Length2
= KdbCommandHistoryBufferIndex
;
1872 /* Remove previous commands until there is enough space to append the new command */
1873 for (i
= KdbCommandHistoryIndex
; KdbCommandHistory
[i
] != NULL
;)
1876 (KdbCommandHistory
[i
] >= Buffer
||
1877 KdbCommandHistory
[i
] < (KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
))) ||
1879 (KdbCommandHistory
[i
] >= Buffer
&&
1880 KdbCommandHistory
[i
] < (KdbCommandHistoryBuffer
+ KdbCommandHistoryBufferIndex
))))
1882 KdbCommandHistory
[i
] = NULL
;
1886 i
= RTL_NUMBER_OF(KdbCommandHistory
) - 1;
1887 if (i
== KdbCommandHistoryIndex
)
1891 /* Make sure the new command history entry is free */
1892 KdbCommandHistoryIndex
++;
1893 KdbCommandHistoryIndex
%= RTL_NUMBER_OF(KdbCommandHistory
);
1894 if (KdbCommandHistory
[KdbCommandHistoryIndex
] != NULL
)
1896 KdbCommandHistory
[KdbCommandHistoryIndex
] = NULL
;
1899 /* Append command */
1900 KdbCommandHistory
[KdbCommandHistoryIndex
] = Buffer
;
1901 ASSERT((KdbCommandHistory
[KdbCommandHistoryIndex
] + Length1
) <= KdbCommandHistoryBuffer
+ RTL_NUMBER_OF(KdbCommandHistoryBuffer
));
1902 memcpy(KdbCommandHistory
[KdbCommandHistoryIndex
], Command
, Length1
);
1905 memcpy(KdbCommandHistoryBuffer
, Command
+ Length1
, Length2
);
1909 /*!\brief Reads a line of user-input.
1911 * \param Buffer Buffer to store the input into. Trailing newlines are removed.
1912 * \param Size Size of \a Buffer.
1914 * \note Accepts only \n newlines, \r is ignored.
1922 PCHAR Orig
= Buffer
;
1925 STATIC CHAR LastCommand
[1024] = "";
1926 STATIC CHAR LastKey
= '\0';
1927 INT CmdHistIndex
= -1;
1930 EchoOn
= !((KdDebugState
& KD_DEBUG_KDNOECHO
) != 0);
1934 if (KdDebugState
& KD_DEBUG_KDSERIAL
)
1936 Key
= KdbpGetCharSerial();
1938 if (Key
== KEY_ESC
) /* ESC */
1940 Key
= KdbpGetCharSerial();
1943 Key
= KdbpGetCharSerial();
1947 ScanCode
= KEY_SCAN_UP
;
1950 ScanCode
= KEY_SCAN_DOWN
;
1962 Key
= KdbpGetCharKeyboard(&ScanCode
);
1965 if ((Buffer
- Orig
) >= (Size
- 1))
1967 /* Buffer is full, accept only newlines */
1974 /* Read the next char - this is to throw away a \n which most clients should
1977 if (KdDebugState
& KD_DEBUG_KDSERIAL
)
1978 NextKey
= KdbpTryGetCharSerial(5);
1980 NextKey
= KdbpTryGetCharKeyboard(&ScanCode
, 5);
1983 * Repeat the last command if the user presses enter. Reduces the
1984 * risk of RSI when single-stepping.
1988 strncpy(Buffer
, LastCommand
, Size
);
1989 Buffer
[Size
- 1] = '\0';
1994 strncpy(LastCommand
, Orig
, sizeof (LastCommand
));
1995 LastCommand
[sizeof (LastCommand
) - 1] = '\0';
2000 else if (Key
== KEY_BS
|| Key
== KEY_DEL
)
2007 DbgPrint("%c %c", KEY_BS
, KEY_BS
);
2009 DbgPrint(" %c", KEY_BS
);
2012 else if (ScanCode
== KEY_SCAN_UP
)
2014 BOOLEAN Print
= TRUE
;
2015 if (CmdHistIndex
< 0)
2016 CmdHistIndex
= KdbCommandHistoryIndex
;
2019 i
= CmdHistIndex
- 1;
2021 CmdHistIndex
= RTL_NUMBER_OF(KdbCommandHistory
) - 1;
2022 if (KdbCommandHistory
[i
] != NULL
&& i
!= KdbCommandHistoryIndex
)
2027 if (Print
&& KdbCommandHistory
[CmdHistIndex
] != NULL
)
2029 while (Buffer
> Orig
)
2034 DbgPrint("%c %c", KEY_BS
, KEY_BS
);
2036 DbgPrint(" %c", KEY_BS
);
2038 i
= min(strlen(KdbCommandHistory
[CmdHistIndex
]), Size
- 1);
2039 memcpy(Orig
, KdbCommandHistory
[CmdHistIndex
], i
);
2042 DbgPrint("%s", Orig
);
2045 else if (ScanCode
== KEY_SCAN_DOWN
)
2047 if (CmdHistIndex
> 0 && CmdHistIndex
!= KdbCommandHistoryIndex
)
2049 i
= CmdHistIndex
+ 1;
2050 if (i
>= RTL_NUMBER_OF(KdbCommandHistory
))
2052 if (KdbCommandHistory
[i
] != NULL
)
2055 while (Buffer
> Orig
)
2060 DbgPrint("%c %c", KEY_BS
, KEY_BS
);
2062 DbgPrint(" %c", KEY_BS
);
2064 i
= min(strlen(KdbCommandHistory
[CmdHistIndex
]), Size
- 1);
2065 memcpy(Orig
, KdbCommandHistory
[CmdHistIndex
], i
);
2068 DbgPrint("%s", Orig
);
2075 DbgPrint("%c", Key
);
2084 /*!\brief Parses command line and executes command if found
2086 * \param Command Command line to parse and execute if possible.
2088 * \retval TRUE Don't continue execution.
2089 * \retval FALSE Continue execution (leave KDB)
2098 STATIC PCH Argv
[256];
2099 STATIC CHAR OrigCommand
[1024];
2101 strncpy(OrigCommand
, Command
, sizeof(OrigCommand
) - 1);
2102 OrigCommand
[sizeof(OrigCommand
) - 1] = '\0';
2108 while (*p
== '\t' || *p
== ' ')
2113 i
= strcspn(p
, "\t ");
2124 for (i
= 0; i
< RTL_NUMBER_OF(KdbDebuggerCommands
); i
++)
2126 if (KdbDebuggerCommands
[i
].Name
== NULL
)
2129 if (strcmp(KdbDebuggerCommands
[i
].Name
, Argv
[0]) == 0)
2131 return KdbDebuggerCommands
[i
].Fn(Argc
, Argv
);
2135 KdbpPrint("Command '%s' is unknown.\n", OrigCommand
);
2139 /*!\brief KDB Main Loop.
2141 * \param EnteredOnSingleStep TRUE if KDB was entered on single step.
2145 IN BOOLEAN EnteredOnSingleStep
)
2147 STATIC CHAR Command
[1024];
2150 if (EnteredOnSingleStep
)
2152 if (!KdbSymPrintAddress((PVOID
)KdbCurrentTrapFrame
->Tf
.Eip
))
2154 DbgPrint("<%x>", KdbCurrentTrapFrame
->Tf
.Eip
);
2157 if (KdbpDisassemble(KdbCurrentTrapFrame
->Tf
.Eip
, KdbUseIntelSyntax
) < 0)
2159 DbgPrint("<INVALID>");
2166 /* Print the prompt */
2169 /* Read a command and remember it */
2170 KdbpReadCommand(Command
, sizeof (Command
));
2171 KdbpCommandHistoryAppend(Command
);
2173 /* Reset the number of rows/cols printed and output aborted state */
2174 KdbNumberOfRowsPrinted
= KdbNumberOfColsPrinted
= 0;
2175 KdbOutputAborted
= FALSE
;
2177 /* Call the command */
2178 Continue
= KdbpDoCommand(Command
);
2182 /*!\brief Called when a module is loaded.
2184 * \param Name Filename of the module which was loaded.
2187 KdbpCliModuleLoaded(IN PUNICODE_STRING Name
)
2191 DbgPrint("Module %wZ loaded.\n", Name
);
2192 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
2195 /*!\brief This function is called by KdbEnterDebuggerException...
2197 * Used to interpret the init file in a context with a trapframe setup
2198 * (KdbpCliInit call KdbEnter which will call KdbEnterDebuggerException which will
2199 * call this function if KdbInitFileBuffer is not NULL.
2202 KdbpCliInterpretInitFile()
2208 /* Execute the commands in the init file */
2209 DbgPrint("KDB: Executing KDBinit file...\n");
2210 p1
= KdbInitFileBuffer
;
2211 while (p1
[0] != '\0')
2213 i
= strcspn(p1
, "\r\n");
2219 /* Look for "break" command and comments */
2221 while (isspace(p2
[0]))
2223 if (strncmp(p2
, "break", sizeof("break")-1) == 0 &&
2224 (p2
[sizeof("break")-1] == '\0' || isspace(p2
[sizeof("break")-1])))
2226 /* break into the debugger */
2227 KdbpCliMainLoop(FALSE
);
2229 else if (p2
[0] != '#' && p2
[0] != '\0') /* Ignore empty lines and comments */
2237 while (p1
[0] == '\r' || p1
[0] == '\n')
2240 DbgPrint("KDB: KDBinit executed\n");
2243 /*!\brief Called when KDB is initialized
2245 * Reads the KDBinit file from the SystemRoot\system32\drivers\etc directory and executes it.
2251 OBJECT_ATTRIBUTES ObjectAttributes
;
2252 UNICODE_STRING FileName
;
2253 IO_STATUS_BLOCK Iosb
;
2254 FILE_STANDARD_INFORMATION FileStdInfo
;
2255 HANDLE hFile
= NULL
;
2260 /* Initialize the object attributes */
2261 RtlInitUnicodeString(&FileName
, L
"\\SystemRoot\\system32\\drivers\\etc\\KDBinit");
2262 InitializeObjectAttributes(&ObjectAttributes
, &FileName
, 0, NULL
, NULL
);
2265 Status
= ZwOpenFile(&hFile
, FILE_READ_DATA
, &ObjectAttributes
, &Iosb
, 0,
2266 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
|
2267 FILE_NO_INTERMEDIATE_BUFFERING
);
2268 if (!NT_SUCCESS(Status
))
2270 DPRINT("Could not open \\SystemRoot\\system32\\drivers\\etc\\KDBinit (Status 0x%x)", Status
);
2274 /* Get the size of the file */
2275 Status
= ZwQueryInformationFile(hFile
, &Iosb
, &FileStdInfo
, sizeof (FileStdInfo
),
2276 FileStandardInformation
);
2277 if (!NT_SUCCESS(Status
))
2280 DPRINT("Could not query size of \\SystemRoot\\system32\\drivers\\etc\\KDBinit (Status 0x%x)", Status
);
2283 FileSize
= FileStdInfo
.EndOfFile
.u
.LowPart
;
2285 /* Allocate memory for the file */
2286 FileBuffer
= ExAllocatePool(PagedPool
, FileSize
+ 1); /* add 1 byte for terminating '\0' */
2287 if (FileBuffer
== NULL
)
2290 DPRINT("Could not allocate %d bytes for KDBinit file\n", FileSize
);
2294 /* Load file into memory */
2295 Status
= ZwReadFile(hFile
, 0, 0, 0, &Iosb
, FileBuffer
, FileSize
, 0, 0);
2297 if (!NT_SUCCESS(Status
) && Status
!= STATUS_END_OF_FILE
)
2299 ExFreePool(FileBuffer
);
2300 DPRINT("Could not read KDBinit file into memory (Status 0x%lx)\n", Status
);
2303 FileSize
= min(FileSize
, Iosb
.Information
);
2304 FileBuffer
[FileSize
] = '\0';
2306 /* Enter critical section */
2307 Ke386SaveFlags(OldEflags
);
2308 Ke386DisableInterrupts();
2310 /* Interpret the init file... */
2311 KdbInitFileBuffer
= FileBuffer
;
2313 KdbInitFileBuffer
= NULL
;
2315 /* Leave critical section */
2316 Ke386RestoreFlags(OldEflags
);
2318 ExFreePool(FileBuffer
);