VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGCEmulateCodeView.cpp@ 9834

Last change on this file since 9834 was 9834, checked in by vboxsync, 16 years ago

Initial support for 64 bits register dumps.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 144.1 KB
Line 
1/** $Id: DBGCEmulateCodeView.cpp 9834 2008-06-20 08:24:49Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, CodeView / WinDbg Emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_DBGC
26#include <VBox/dbg.h>
27#include <VBox/dbgf.h>
28#include <VBox/pgm.h>
29#include <VBox/selm.h>
30#include <VBox/cpum.h>
31#include <VBox/dis.h>
32#include <VBox/param.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35
36#include <iprt/alloc.h>
37#include <iprt/alloca.h>
38#include <iprt/string.h>
39#include <iprt/assert.h>
40#include <iprt/ctype.h>
41
42#include <stdlib.h>
43#include <stdio.h>
44
45#include "DBGCInternal.h"
46
47
48/*******************************************************************************
49* Internal Functions *
50*******************************************************************************/
51static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
52static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
53static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
54static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
55static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
56static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
57static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
58static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
59static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
60static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
61static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
62static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
63static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
64static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
65static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
66static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
67static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
68static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
69static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
70static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
71static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
72static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
73static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
74static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
75static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
76static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
77static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
78static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
79
80
81/*******************************************************************************
82* Global Variables *
83*******************************************************************************/
84/** 'ba' arguments. */
85static const DBGCVARDESC g_aArgBrkAcc[] =
86{
87 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
88 { 1, 1, DBGCVAR_CAT_STRING, 0, "access", "The access type: x=execute, rw=read/write (alias r), w=write, i=not implemented." },
89 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "size", "The access size: 1, 2, 4, or 8. 'x' access requires 1, and 8 requires amd64 long mode." },
90 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
91 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
92 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
93 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
94};
95
96
97/** 'bc', 'bd', 'be' arguments. */
98static const DBGCVARDESC g_aArgBrks[] =
99{
100 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
101 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "#bp", "Breakpoint number." },
102 { 0, 1, DBGCVAR_CAT_STRING, 0, "all", "All breakpoints." },
103};
104
105
106/** 'bp' arguments. */
107static const DBGCVARDESC g_aArgBrkSet[] =
108{
109 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
110 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
111 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
112 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
113 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
114};
115
116
117/** 'br' arguments. */
118static const DBGCVARDESC g_aArgBrkREM[] =
119{
120 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
121 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
122 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
123 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
124 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
125};
126
127
128/** 'd?' arguments. */
129static const DBGCVARDESC g_aArgDumpMem[] =
130{
131 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
132 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start dumping memory." },
133};
134
135
136/** 'dg', 'dga', 'dl', 'dla' arguments. */
137static const DBGCVARDESC g_aArgDumpDT[] =
138{
139 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
140 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "sel", "Selector or selector range." },
141 { 0, ~0, DBGCVAR_CAT_POINTER, 0, "address", "Far address which selector should be dumped." },
142};
143
144
145/** 'di', 'dia' arguments. */
146static const DBGCVARDESC g_aArgDumpIDT[] =
147{
148 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
149 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "int", "The interrupt vector or interrupt vector range." },
150};
151
152
153/** 'dpd*' arguments. */
154static const DBGCVARDESC g_aArgDumpPD[] =
155{
156 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
157 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "index", "Index into the page directory." },
158 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from. Range is applied to the page directory." },
159};
160
161
162/** 'dpda' arguments. */
163static const DBGCVARDESC g_aArgDumpPDAddr[] =
164{
165 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
166 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page directory entry to start dumping from." },
167};
168
169
170/** 'dpt?' arguments. */
171static const DBGCVARDESC g_aArgDumpPT[] =
172{
173 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
174 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from." },
175};
176
177
178/** 'dpta' arguments. */
179static const DBGCVARDESC g_aArgDumpPTAddr[] =
180{
181 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
182 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page table entry to start dumping from." },
183};
184
185
186/** 'dt' arguments. */
187static const DBGCVARDESC g_aArgDumpTSS[] =
188{
189 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
190 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "tss", "TSS selector number." },
191 { 0, 1, DBGCVAR_CAT_POINTER, 0, "tss:ign|addr", "TSS address. If the selector is a TSS selector, the offset will be ignored." }
192};
193
194
195/** 'ln' arguments. */
196static const DBGCVARDESC g_aArgListNear[] =
197{
198 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
199 { 0, ~0, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
200 { 0, ~0, DBGCVAR_CAT_SYMBOL, 0, "symbol", "Symbol to lookup." },
201};
202
203/** 'ln' return. */
204static const DBGCVARDESC g_RetListNear =
205{
206 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "The last resolved symbol/address with adjusted range."
207};
208
209
210/** 'ls' arguments. */
211static const DBGCVARDESC g_aArgListSource[] =
212{
213 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
214 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start looking for source lines." },
215};
216
217
218/** 'm' argument. */
219static const DBGCVARDESC g_aArgMemoryInfo[] =
220{
221 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
222 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Pointer to obtain info about." },
223};
224
225
226/** 'r' arguments. */
227static const DBGCVARDESC g_aArgReg[] =
228{
229 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
230 { 0, 1, DBGCVAR_CAT_SYMBOL, 0, "register", "Register to show or set." },
231 { 0, 1, DBGCVAR_CAT_NUMBER_NO_RANGE, DBGCVD_FLAGS_DEP_PREV, "value", "New register value." },
232};
233
234
235/** 's' arguments. */
236static const DBGCVARDESC g_aArgSearchMem[] =
237{
238 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
239 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-b", "Byte string." },
240 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-w", "Word string." },
241 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-d", "DWord string." },
242 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-q", "QWord string." },
243 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-a", "ASCII string." },
244 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-u", "Unicode string." },
245 { 0, 1, DBGCVAR_CAT_OPTION_NUMBER, 0, "-n <Hits>", "Maximum number of hits." },
246 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
247 { 0, ~0, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
248};
249
250
251/** 's?' arguments. */
252static const DBGCVARDESC g_aArgSearchMemType[] =
253{
254 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
255 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
256 { 1, ~0, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
257};
258
259
260/** 'u' arguments. */
261static const DBGCVARDESC g_aArgUnassemble[] =
262{
263 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
264 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
265};
266
267
268/** Command descriptors for the CodeView / WinDbg emulation.
269 * The emulation isn't attempting to be identical, only somewhat similar.
270 */
271const DBGCCMD g_aCmdsCodeView[] =
272{
273 /* pszCmd, cArgsMin, cArgsMax, paArgDescs, cArgDescs, pResultDesc, fFlags, pfnHandler pszSyntax, ....pszDescription */
274 { "ba", 3, 6, &g_aArgBrkAcc[0], RT_ELEMENTS(g_aArgBrkAcc), NULL, 0, dbgcCmdBrkAccess, "<access> <size> <address> [passes [max passes]] [cmds]",
275 "Sets a data access breakpoint." },
276 { "bc", 1, ~0, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), NULL, 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Enabled a set of breakpoints." },
277 { "bd", 1, ~0, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), NULL, 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
278 { "be", 1, ~0, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), NULL, 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enabled a set of breakpoints." },
279 { "bl", 0, 0, NULL, 0, NULL, 0, dbgcCmdBrkList, "", "Lists all the breakpoints." },
280 { "bp", 1, 4, &g_aArgBrkSet[0], RT_ELEMENTS(g_aArgBrkSet), NULL, 0, dbgcCmdBrkSet, "<address> [passes [max passes]] [cmds]",
281 "Sets a breakpoint (int 3)." },
282 { "br", 1, 4, &g_aArgBrkREM[0], RT_ELEMENTS(g_aArgBrkREM), NULL, 0, dbgcCmdBrkREM, "<address> [passes [max passes]] [cmds]",
283 "Sets a recompiler specific breakpoint." },
284 { "d", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory using last element size." },
285 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
286 { "db", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in bytes." },
287 { "dd", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in double words." },
288 { "dg", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), NULL, 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT)." },
289 { "dga", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), NULL, 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT) including not-present entries." },
290 { "di", 0, ~0, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), NULL, 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT)." },
291 { "dia", 0, ~0, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), NULL, 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT) including not-present entries." },
292 { "dl", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), NULL, 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT)." },
293 { "dla", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), NULL, 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT) including not-present entries." },
294 { "dpd", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the default context." },
295 { "dpda", 0, 1, &g_aArgDumpPDAddr[0],RT_ELEMENTS(g_aArgDumpPDAddr),NULL, 0, dbgcCmdDumpPageDir, "[addr]", "Dumps specified page directory." },
296 { "dpdb", 1, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDirBoth, "[addr] [index]", "Dumps page directory entries of the guest and the hypervisor. " },
297 { "dpdg", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the guest." },
298 { "dpdh", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the hypervisor. " },
299 { "dpt", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the default context." },
300 { "dpta", 1, 1, &g_aArgDumpPTAddr[0],RT_ELEMENTS(g_aArgDumpPTAddr), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps specified page table." },
301 { "dptb", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTableBoth,"<addr>", "Dumps page table entries of the guest and the hypervisor." },
302 { "dptg", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the guest." },
303 { "dpth", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the hypervisor." },
304 { "dq", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in quad words." },
305 { "dt", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), NULL, 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the task state segment (TSS)." },
306 { "dw", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in words." },
307 { "g", 0, 0, NULL, 0, NULL, 0, dbgcCmdGo, "", "Continue execution." },
308 { "k", 0, 0, NULL, 0, NULL, 0, dbgcCmdStack, "", "Callstack." },
309 { "kg", 0, 0, NULL, 0, NULL, 0, dbgcCmdStack, "", "Callstack - guest." },
310 { "kh", 0, 0, NULL, 0, NULL, 0, dbgcCmdStack, "", "Callstack - hypervisor." },
311 { "ln", 0, ~0, &g_aArgListNear[0], RT_ELEMENTS(g_aArgListNear), &g_RetListNear, 0, dbgcCmdListNear, "[addr/sym [..]]", "List symbols near to the address. Default address is CS:EIP." },
312 { "ls", 0, 1, &g_aArgListSource[0],RT_ELEMENTS(g_aArgListSource),NULL, 0, dbgcCmdListSource, "[addr]", "Source." },
313 { "m", 1, 1, &g_aArgMemoryInfo[0],RT_ELEMENTS(g_aArgMemoryInfo),NULL, 0, dbgcCmdMemoryInfo, "<addr>", "Display information about that piece of memory." },
314 { "r", 0, 2, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), NULL, 0, dbgcCmdReg, "[reg [newval]]", "Show or set register(s) - active reg set." },
315 { "rg", 0, 2, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), NULL, 0, dbgcCmdRegGuest, "[reg [newval]]", "Show or set register(s) - guest reg set." },
316 { "rh", 0, 2, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), NULL, 0, dbgcCmdRegHyper, "[reg [newval]]", "Show or set register(s) - hypervisor reg set." },
317 { "rt", 0, 0, NULL, 0, NULL, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
318 { "s", 0, ~0, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), NULL, 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
319 { "sa", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType), NULL, 0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
320 { "sb", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType), NULL, 0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
321 { "sd", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType), NULL, 0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
322 { "sq", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType), NULL, 0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
323 { "su", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType), NULL, 0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
324 { "sw", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType), NULL, 0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more words." },
325 { "t", 0, 0, NULL, 0, NULL, 0, dbgcCmdTrace, "", "Instruction trace (step into)." },
326 { "u", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble),NULL, 0, dbgcCmdUnassemble, "[addr]", "Unassemble." },
327};
328
329/** The number of commands in the CodeView/WinDbg emulation. */
330const unsigned g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
331
332
333
334/**
335 * The 'go' command.
336 *
337 * @returns VBox status.
338 * @param pCmd Pointer to the command descriptor (as registered).
339 * @param pCmdHlp Pointer to command helper functions.
340 * @param pVM Pointer to the current VM (if any).
341 * @param paArgs Pointer to (readonly) array of arguments.
342 * @param cArgs Number of arguments in the array.
343 */
344static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
345{
346 /*
347 * Check if the VM is halted or not before trying to resume it.
348 */
349 if (!DBGFR3IsHalted(pVM))
350 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "warning: The VM is already running...\n");
351 else
352 {
353 int rc = DBGFR3Resume(pVM);
354 if (VBOX_FAILURE(rc))
355 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Executing DBGFR3Resume().");
356 }
357
358 NOREF(pCmd);
359 NOREF(paArgs);
360 NOREF(cArgs);
361 NOREF(pResult);
362 return 0;
363}
364
365
366/**
367 * The 'ba' command.
368 *
369 * @returns VBox status.
370 * @param pCmd Pointer to the command descriptor (as registered).
371 * @param pCmdHlp Pointer to command helper functions.
372 * @param pVM Pointer to the current VM (if any).
373 * @param paArgs Pointer to (readonly) array of arguments.
374 * @param cArgs Number of arguments in the array.
375 */
376static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
377{
378 /*
379 * Interpret access type.
380 */
381 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
382 || paArgs[0].u.pszString[1])
383 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'.\n",
384 paArgs[0].u.pszString, pCmd->pszCmd);
385 uint8_t fType = 0;
386 switch (paArgs[0].u.pszString[0])
387 {
388 case 'x': fType = X86_DR7_RW_EO; break;
389 case 'r': fType = X86_DR7_RW_RW; break;
390 case 'w': fType = X86_DR7_RW_WO; break;
391 case 'i': fType = X86_DR7_RW_IO; break;
392 }
393
394 /*
395 * Validate size.
396 */
397 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
398 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid access size %RX64 for '%s'. 'x' access type requires size 1!\n",
399 paArgs[1].u.u64Number, pCmd->pszCmd);
400 switch (paArgs[1].u.u64Number)
401 {
402 case 1:
403 case 2:
404 case 4:
405 break;
406 /*case 8: - later*/
407 default:
408 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid access size %RX64 for '%s'. 1, 2 or 4!\n",
409 paArgs[1].u.u64Number, pCmd->pszCmd);
410 }
411 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
412
413 /*
414 * Convert the pointer to a DBGF address.
415 */
416 DBGFADDRESS Address;
417 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
418 if (VBOX_FAILURE(rc))
419 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Couldn't convert '%DV' to a DBGF address, rc=%Vrc.\n", &paArgs[2], rc);
420
421 /*
422 * Pick out the optional arguments.
423 */
424 uint64_t iHitTrigger = 0;
425 uint64_t iHitDisable = ~0;
426 const char *pszCmds = NULL;
427 unsigned iArg = 3;
428 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
429 {
430 iHitTrigger = paArgs[iArg].u.u64Number;
431 iArg++;
432 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
433 {
434 iHitDisable = paArgs[iArg].u.u64Number;
435 iArg++;
436 }
437 }
438 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
439 {
440 pszCmds = paArgs[iArg].u.pszString;
441 iArg++;
442 }
443
444 /*
445 * Try set the breakpoint.
446 */
447 RTUINT iBp;
448 rc = DBGFR3BpSetReg(pVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
449 if (VBOX_SUCCESS(rc))
450 {
451 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
452 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
453 if (VBOX_SUCCESS(rc))
454 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Set access breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
455 if (rc == VERR_DBGC_BP_EXISTS)
456 {
457 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
458 if (VBOX_SUCCESS(rc))
459 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Updated access breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
460 }
461 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
462 AssertRC(rc2);
463 }
464 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Failed to set access breakpoint at %VGv, rc=%Vrc.\n", Address.FlatPtr, rc);
465}
466
467
468/**
469 * The 'bc' command.
470 *
471 * @returns VBox status.
472 * @param pCmd Pointer to the command descriptor (as registered).
473 * @param pCmdHlp Pointer to command helper functions.
474 * @param pVM Pointer to the current VM (if any).
475 * @param paArgs Pointer to (readonly) array of arguments.
476 * @param cArgs Number of arguments in the array.
477 */
478static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
479{
480 /*
481 * Enumerate the arguments.
482 */
483 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
484 int rc = VINF_SUCCESS;
485 for (unsigned iArg = 0; iArg < cArgs && VBOX_SUCCESS(rc); iArg++)
486 {
487 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
488 {
489 /* one */
490 RTUINT iBp = (RTUINT)paArgs[iArg].u.u64Number;
491 if (iBp != paArgs[iArg].u.u64Number)
492 {
493 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Breakpoint id %RX64 is too large!\n", paArgs[iArg].u.u64Number);
494 break;
495 }
496 int rc2 = DBGFR3BpClear(pVM, iBp);
497 if (VBOX_FAILURE(rc2))
498 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc2, "DBGFR3BpClear failed for breakpoint %u!\n", iBp);
499 if (VBOX_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
500 dbgcBpDelete(pDbgc, iBp);
501 }
502 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
503 {
504 /* all */
505 PDBGCBP pBp = pDbgc->pFirstBp;
506 while (pBp)
507 {
508 RTUINT iBp = pBp->iBp;
509 pBp = pBp->pNext;
510
511 int rc2 = DBGFR3BpClear(pVM, iBp);
512 if (VBOX_FAILURE(rc2))
513 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc2, "DBGFR3BpClear failed for breakpoint %u!\n", iBp);
514 if (VBOX_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
515 dbgcBpDelete(pDbgc, iBp);
516 }
517 }
518 else
519 {
520 /* invalid parameter */
521 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid argument '%s' to '%s'!\n", paArgs[iArg].u.pszString, pCmd->pszCmd);
522 break;
523 }
524 }
525 return rc;
526}
527
528
529/**
530 * The 'bd' command.
531 *
532 * @returns VBox status.
533 * @param pCmd Pointer to the command descriptor (as registered).
534 * @param pCmdHlp Pointer to command helper functions.
535 * @param pVM Pointer to the current VM (if any).
536 * @param paArgs Pointer to (readonly) array of arguments.
537 * @param cArgs Number of arguments in the array.
538 */
539static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
540{
541 /*
542 * Enumerate the arguments.
543 */
544 int rc = VINF_SUCCESS;
545 for (unsigned iArg = 0; iArg < cArgs && VBOX_SUCCESS(rc); iArg++)
546 {
547 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
548 {
549 /* one */
550 RTUINT iBp = (RTUINT)paArgs[iArg].u.u64Number;
551 if (iBp != paArgs[iArg].u.u64Number)
552 {
553 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Breakpoint id %RX64 is too large!\n", paArgs[iArg].u.u64Number);
554 break;
555 }
556 rc = DBGFR3BpDisable(pVM, iBp);
557 if (VBOX_FAILURE(rc))
558 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpDisable failed for breakpoint %u!\n", iBp);
559 }
560 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
561 {
562 /* all */
563 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
564 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
565 {
566 rc = DBGFR3BpDisable(pVM, pBp->iBp);
567 if (VBOX_FAILURE(rc))
568 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpDisable failed for breakpoint %u!\n", pBp->iBp);
569 }
570 }
571 else
572 {
573 /* invalid parameter */
574 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid argument '%s' to '%s'!\n", paArgs[iArg].u.pszString, pCmd->pszCmd);
575 break;
576 }
577 }
578 return rc;
579}
580
581
582/**
583 * The 'be' command.
584 *
585 * @returns VBox status.
586 * @param pCmd Pointer to the command descriptor (as registered).
587 * @param pCmdHlp Pointer to command helper functions.
588 * @param pVM Pointer to the current VM (if any).
589 * @param paArgs Pointer to (readonly) array of arguments.
590 * @param cArgs Number of arguments in the array.
591 */
592static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
593{
594 /*
595 * Enumerate the arguments.
596 */
597 int rc = VINF_SUCCESS;
598 for (unsigned iArg = 0; iArg < cArgs && VBOX_SUCCESS(rc); iArg++)
599 {
600 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
601 {
602 /* one */
603 RTUINT iBp = (RTUINT)paArgs[iArg].u.u64Number;
604 if (iBp != paArgs[iArg].u.u64Number)
605 {
606 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Breakpoint id %RX64 is too large!\n", paArgs[iArg].u.u64Number);
607 break;
608 }
609 rc = DBGFR3BpEnable(pVM, iBp);
610 if (VBOX_FAILURE(rc))
611 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpEnable failed for breakpoint %u!\n", iBp);
612 }
613 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
614 {
615 /* all */
616 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
617 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
618 {
619 rc = DBGFR3BpEnable(pVM, pBp->iBp);
620 if (VBOX_FAILURE(rc))
621 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpEnable failed for breakpoint %u!\n", pBp->iBp);
622 }
623 }
624 else
625 {
626 /* invalid parameter */
627 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid argument '%s' to '%s'!\n", paArgs[iArg].u.pszString, pCmd->pszCmd);
628 break;
629 }
630 }
631 return rc;
632}
633
634
635/**
636 * Breakpoint enumeration callback function.
637 *
638 * @returns VBox status code. Any failure will stop the enumeration.
639 * @param pVM The VM handle.
640 * @param pvUser The user argument.
641 * @param pBp Pointer to the breakpoint information. (readonly)
642 */
643static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PVM pVM, void *pvUser, PCDBGFBP pBp)
644{
645 PDBGC pDbgc = (PDBGC)pvUser;
646 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
647
648 /*
649 * BP type and size.
650 */
651 char chType;
652 char cb = 1;
653 switch (pBp->enmType)
654 {
655 case DBGFBPTYPE_INT3:
656 chType = 'p';
657 break;
658 case DBGFBPTYPE_REG:
659 switch (pBp->u.Reg.fType)
660 {
661 case X86_DR7_RW_EO: chType = 'x'; break;
662 case X86_DR7_RW_WO: chType = 'w'; break;
663 case X86_DR7_RW_IO: chType = 'i'; break;
664 case X86_DR7_RW_RW: chType = 'r'; break;
665 default: chType = '?'; break;
666
667 }
668 cb = pBp->u.Reg.cb;
669 break;
670 case DBGFBPTYPE_REM:
671 chType = 'r';
672 break;
673 default:
674 chType = '?';
675 break;
676 }
677
678 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%2u %c %d %c %VGv %04RX64 (%04RX64 to ",
679 pBp->iBp, pBp->fEnabled ? 'e' : 'd', cb, chType,
680 pBp->GCPtr, pBp->cHits, pBp->iHitTrigger);
681 if (pBp->iHitDisable == ~(uint64_t)0)
682 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "~0) ");
683 else
684 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%04RX64)");
685
686 /*
687 * Try resolve the address.
688 */
689 DBGFSYMBOL Sym;
690 RTGCINTPTR off;
691 int rc = DBGFR3SymbolByAddr(pVM, pBp->GCPtr, &off, &Sym);
692 if (VBOX_SUCCESS(rc))
693 {
694 if (!off)
695 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s", Sym.szName);
696 else if (off > 0)
697 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s+%VGv", Sym.szName, off);
698 else
699 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s+%VGv", Sym.szName, -off);
700 }
701
702 /*
703 * The commands.
704 */
705 if (pDbgcBp)
706 {
707 if (pDbgcBp->cchCmd)
708 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n cmds: '%s'\n",
709 pDbgcBp->szCmd);
710 else
711 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n");
712 }
713 else
714 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " [unknown bp]\n");
715
716 return VINF_SUCCESS;
717}
718
719
720/**
721 * The 'bl' command.
722 *
723 * @returns VBox status.
724 * @param pCmd Pointer to the command descriptor (as registered).
725 * @param pCmdHlp Pointer to command helper functions.
726 * @param pVM Pointer to the current VM (if any).
727 * @param paArgs Pointer to (readonly) array of arguments.
728 * @param cArgs Number of arguments in the array.
729 */
730static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR /*paArgs*/, unsigned /*cArgs*/, PDBGCVAR /*pResult*/)
731{
732 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
733
734 /*
735 * Enumerate the breakpoints.
736 */
737 int rc = DBGFR3BpEnum(pVM, dbgcEnumBreakpointsCallback, pDbgc);
738 if (VBOX_FAILURE(rc))
739 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpEnum failed.\n");
740 return rc;
741}
742
743
744/**
745 * The 'bp' command.
746 *
747 * @returns VBox status.
748 * @param pCmd Pointer to the command descriptor (as registered).
749 * @param pCmdHlp Pointer to command helper functions.
750 * @param pVM Pointer to the current VM (if any).
751 * @param paArgs Pointer to (readonly) array of arguments.
752 * @param cArgs Number of arguments in the array.
753 */
754static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
755{
756 /*
757 * Convert the pointer to a DBGF address.
758 */
759 DBGFADDRESS Address;
760 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
761 if (VBOX_FAILURE(rc))
762 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Couldn't convert '%DV' to a DBGF address, rc=%Vrc.\n", &paArgs[0], rc);
763
764 /*
765 * Pick out the optional arguments.
766 */
767 uint64_t iHitTrigger = 0;
768 uint64_t iHitDisable = ~0;
769 const char *pszCmds = NULL;
770 unsigned iArg = 1;
771 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
772 {
773 iHitTrigger = paArgs[iArg].u.u64Number;
774 iArg++;
775 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
776 {
777 iHitDisable = paArgs[iArg].u.u64Number;
778 iArg++;
779 }
780 }
781 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
782 {
783 pszCmds = paArgs[iArg].u.pszString;
784 iArg++;
785 }
786
787 /*
788 * Try set the breakpoint.
789 */
790 RTUINT iBp;
791 rc = DBGFR3BpSet(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
792 if (VBOX_SUCCESS(rc))
793 {
794 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
795 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
796 if (VBOX_SUCCESS(rc))
797 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Set breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
798 if (rc == VERR_DBGC_BP_EXISTS)
799 {
800 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
801 if (VBOX_SUCCESS(rc))
802 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Updated breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
803 }
804 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
805 AssertRC(rc2);
806 }
807 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Failed to set breakpoint at %VGv, rc=%Vrc.\n", Address.FlatPtr, rc);
808}
809
810
811/**
812 * The 'br' command.
813 *
814 * @returns VBox status.
815 * @param pCmd Pointer to the command descriptor (as registered).
816 * @param pCmdHlp Pointer to command helper functions.
817 * @param pVM Pointer to the current VM (if any).
818 * @param paArgs Pointer to (readonly) array of arguments.
819 * @param cArgs Number of arguments in the array.
820 */
821static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
822{
823 /*
824 * Convert the pointer to a DBGF address.
825 */
826 DBGFADDRESS Address;
827 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
828 if (VBOX_FAILURE(rc))
829 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Couldn't convert '%DV' to a DBGF address, rc=%Vrc.\n", &paArgs[0], rc);
830
831 /*
832 * Pick out the optional arguments.
833 */
834 uint64_t iHitTrigger = 0;
835 uint64_t iHitDisable = ~0;
836 const char *pszCmds = NULL;
837 unsigned iArg = 1;
838 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
839 {
840 iHitTrigger = paArgs[iArg].u.u64Number;
841 iArg++;
842 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
843 {
844 iHitDisable = paArgs[iArg].u.u64Number;
845 iArg++;
846 }
847 }
848 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
849 {
850 pszCmds = paArgs[iArg].u.pszString;
851 iArg++;
852 }
853
854 /*
855 * Try set the breakpoint.
856 */
857 RTUINT iBp;
858 rc = DBGFR3BpSetREM(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
859 if (VBOX_SUCCESS(rc))
860 {
861 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
862 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
863 if (VBOX_SUCCESS(rc))
864 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Set REM breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
865 if (rc == VERR_DBGC_BP_EXISTS)
866 {
867 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
868 if (VBOX_SUCCESS(rc))
869 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Updated REM breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
870 }
871 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
872 AssertRC(rc2);
873 }
874 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Failed to set REM breakpoint at %VGv, rc=%Vrc.\n", Address.FlatPtr, rc);
875}
876
877
878/**
879 * The 'u' command.
880 *
881 * @returns VBox status.
882 * @param pCmd Pointer to the command descriptor (as registered).
883 * @param pCmdHlp Pointer to command helper functions.
884 * @param pVM Pointer to the current VM (if any).
885 * @param paArgs Pointer to (readonly) array of arguments.
886 * @param cArgs Number of arguments in the array.
887 */
888static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
889{
890 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
891
892 /*
893 * Validate input.
894 */
895 if ( cArgs > 1
896 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
897 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
898 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
899 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
900 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
901 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
902
903 /*
904 * Find address.
905 */
906 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS;
907 if (!cArgs)
908 {
909 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
910 {
911 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
912 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVM) : CPUMGetHyperEIP(pVM);
913 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVM) : CPUMGetHyperCS(pVM);
914 if (pDbgc->fRegCtxGuest)
915 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
916 else
917 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER;
918 }
919 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
920 }
921 else
922 pDbgc->DisasmPos = paArgs[0];
923
924 /*
925 * Range.
926 */
927 switch (pDbgc->DisasmPos.enmRangeType)
928 {
929 case DBGCVAR_RANGE_NONE:
930 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
931 pDbgc->DisasmPos.u64Range = 10;
932 break;
933
934 case DBGCVAR_RANGE_ELEMENTS:
935 if (pDbgc->DisasmPos.u64Range > 2048)
936 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
937 break;
938
939 case DBGCVAR_RANGE_BYTES:
940 if (pDbgc->DisasmPos.u64Range > 65536)
941 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
942 break;
943
944 default:
945 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DisasmPos.enmRangeType);
946 }
947
948 /*
949 * Convert physical and host addresses to guest addresses.
950 */
951 int rc;
952 switch (pDbgc->DisasmPos.enmType)
953 {
954 case DBGCVAR_TYPE_GC_FLAT:
955 case DBGCVAR_TYPE_GC_FAR:
956 break;
957 case DBGCVAR_TYPE_GC_PHYS:
958 case DBGCVAR_TYPE_HC_FLAT:
959 case DBGCVAR_TYPE_HC_PHYS:
960 case DBGCVAR_TYPE_HC_FAR:
961 {
962 DBGCVAR VarTmp;
963 rc = pCmdHlp->pfnEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
964 if (VBOX_FAILURE(rc))
965 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: failed to evaluate '%%(%Dv)' -> %Vrc .\n", &pDbgc->DisasmPos, rc);
966 pDbgc->DisasmPos = VarTmp;
967 break;
968 }
969 default: AssertFailed(); break;
970 }
971
972 /*
973 * Print address.
974 * todo: Change to list near.
975 */
976#if 0
977 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DisasmPos);
978 if (VBOX_FAILURE(rc))
979 return rc;
980#endif
981
982 /*
983 * Do the disassembling.
984 */
985 unsigned cTries = 32;
986 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
987 if (iRangeLeft == 0) /* klugde for 'r'. */
988 iRangeLeft = -1;
989 for (;;)
990 {
991 /*
992 * Disassemble the instruction.
993 */
994 char szDis[256];
995 uint32_t cbInstr = 1;
996 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
997 rc = DBGFR3DisasInstrEx(pVM, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags, &szDis[0], sizeof(szDis), &cbInstr);
998 else
999 rc = DBGFR3DisasInstrEx(pVM, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags, &szDis[0], sizeof(szDis), &cbInstr);
1000 if (VBOX_SUCCESS(rc))
1001 {
1002 /* print it */
1003 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1004 if (VBOX_FAILURE(rc))
1005 return rc;
1006 }
1007 else
1008 {
1009 /* bitch. */
1010 int rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to disassemble instruction, skipping one byte.\n");
1011 if (VBOX_FAILURE(rc))
1012 return rc;
1013 if (cTries-- > 0)
1014 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Too many disassembly failures. Giving up.\n");
1015 cbInstr = 1;
1016 }
1017
1018 /* advance */
1019 if (iRangeLeft < 0) /* 'r' */
1020 break;
1021 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1022 iRangeLeft--;
1023 else
1024 iRangeLeft -= cbInstr;
1025 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1026 if (VBOX_FAILURE(rc))
1027 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DisasmPos, cbInstr);
1028 if (iRangeLeft <= 0)
1029 break;
1030 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1031 }
1032
1033 NOREF(pCmd); NOREF(pResult);
1034 return 0;
1035}
1036
1037
1038/**
1039 * The 'ls' command.
1040 *
1041 * @returns VBox status.
1042 * @param pCmd Pointer to the command descriptor (as registered).
1043 * @param pCmdHlp Pointer to command helper functions.
1044 * @param pVM Pointer to the current VM (if any).
1045 * @param paArgs Pointer to (readonly) array of arguments.
1046 * @param cArgs Number of arguments in the array.
1047 */
1048static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1049{
1050 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1051
1052 /*
1053 * Validate input.
1054 */
1055 if ( cArgs > 1
1056 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
1057 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
1058 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1059 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
1060 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1061 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
1062
1063 /*
1064 * Find address.
1065 */
1066 if (!cArgs)
1067 {
1068 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1069 {
1070 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
1071 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVM) : CPUMGetHyperEIP(pVM);
1072 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVM) : CPUMGetHyperCS(pVM);
1073 }
1074 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
1075 }
1076 else
1077 pDbgc->SourcePos = paArgs[0];
1078
1079 /*
1080 * Ensure the the source address is flat GC.
1081 */
1082 switch (pDbgc->SourcePos.enmType)
1083 {
1084 case DBGCVAR_TYPE_GC_FLAT:
1085 break;
1086 case DBGCVAR_TYPE_GC_PHYS:
1087 case DBGCVAR_TYPE_GC_FAR:
1088 case DBGCVAR_TYPE_HC_FLAT:
1089 case DBGCVAR_TYPE_HC_PHYS:
1090 case DBGCVAR_TYPE_HC_FAR:
1091 {
1092 int rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
1093 if (VBOX_FAILURE(rc))
1094 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid address or address type. (rc=%d)\n", rc);
1095 break;
1096 }
1097 default: AssertFailed(); break;
1098 }
1099
1100 /*
1101 * Range.
1102 */
1103 switch (pDbgc->SourcePos.enmRangeType)
1104 {
1105 case DBGCVAR_RANGE_NONE:
1106 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1107 pDbgc->SourcePos.u64Range = 10;
1108 break;
1109
1110 case DBGCVAR_RANGE_ELEMENTS:
1111 if (pDbgc->SourcePos.u64Range > 2048)
1112 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
1113 break;
1114
1115 case DBGCVAR_RANGE_BYTES:
1116 if (pDbgc->SourcePos.u64Range > 65536)
1117 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
1118 break;
1119
1120 default:
1121 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
1122 }
1123
1124 /*
1125 * Do the disassembling.
1126 */
1127 bool fFirst = 1;
1128 DBGFLINE LinePrev = { 0, 0, "" };
1129 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
1130 if (iRangeLeft == 0) /* klugde for 'r'. */
1131 iRangeLeft = -1;
1132 for (;;)
1133 {
1134 /*
1135 * Get line info.
1136 */
1137 DBGFLINE Line;
1138 RTGCINTPTR off;
1139 int rc = DBGFR3LineByAddr(pVM, pDbgc->SourcePos.u.GCFlat, &off, &Line);
1140 if (VBOX_FAILURE(rc))
1141 return VINF_SUCCESS;
1142
1143 unsigned cLines = 0;
1144 if (memcmp(&Line, &LinePrev, sizeof(Line)))
1145 {
1146 /*
1147 * Print filenamename
1148 */
1149 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
1150 fFirst = true;
1151 if (fFirst)
1152 {
1153 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
1154 if (VBOX_FAILURE(rc))
1155 return rc;
1156 }
1157
1158 /*
1159 * Try open the file and read the line.
1160 */
1161 FILE *phFile = fopen(Line.szFilename, "r");
1162 if (phFile)
1163 {
1164 /* Skip ahead to the desired line. */
1165 char szLine[4096];
1166 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
1167 if (cBefore > 7)
1168 cBefore = 0;
1169 unsigned cLeft = Line.uLineNo - cBefore;
1170 while (cLeft > 0)
1171 {
1172 szLine[0] = '\0';
1173 if (!fgets(szLine, sizeof(szLine), phFile))
1174 break;
1175 cLeft--;
1176 }
1177 if (!cLeft)
1178 {
1179 /* print the before lines */
1180 for (;;)
1181 {
1182 size_t cch = strlen(szLine);
1183 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || isspace(szLine[cch - 1])) )
1184 szLine[--cch] = '\0';
1185 if (cBefore-- <= 0)
1186 break;
1187
1188 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
1189 szLine[0] = '\0';
1190 fgets(szLine, sizeof(szLine), phFile);
1191 cLines++;
1192 }
1193 /* print the actual line */
1194 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
1195 }
1196 fclose(phFile);
1197 if (VBOX_FAILURE(rc))
1198 return rc;
1199 fFirst = false;
1200 }
1201 else
1202 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Warning: couldn't open source file '%s'\n", Line.szFilename);
1203
1204 LinePrev = Line;
1205 }
1206
1207
1208 /*
1209 * Advance
1210 */
1211 if (iRangeLeft < 0) /* 'r' */
1212 break;
1213 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1214 iRangeLeft -= cLines;
1215 else
1216 iRangeLeft -= 1;
1217 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
1218 if (VBOX_FAILURE(rc))
1219 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
1220 if (iRangeLeft <= 0)
1221 break;
1222 }
1223
1224 NOREF(pCmd); NOREF(pResult);
1225 return 0;
1226}
1227
1228
1229/**
1230 * The 'r' command.
1231 *
1232 * @returns VBox status.
1233 * @param pCmd Pointer to the command descriptor (as registered).
1234 * @param pCmdHlp Pointer to command helper functions.
1235 * @param pVM Pointer to the current VM (if any).
1236 * @param paArgs Pointer to (readonly) array of arguments.
1237 * @param cArgs Number of arguments in the array.
1238 */
1239static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1240{
1241 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1242
1243 if (pDbgc->fRegCtxGuest)
1244 return dbgcCmdRegGuest(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult);
1245 else
1246 return dbgcCmdRegHyper(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult);
1247}
1248
1249
1250/**
1251 * Common worker for the dbgcCmdReg*() commands.
1252 *
1253 * @returns VBox status.
1254 * @param pCmd Pointer to the command descriptor (as registered).
1255 * @param pCmdHlp Pointer to command helper functions.
1256 * @param pVM Pointer to the current VM (if any).
1257 * @param paArgs Pointer to (readonly) array of arguments.
1258 * @param cArgs Number of arguments in the array.
1259 * @param pszPrefix The symbol prefix.
1260 */
1261static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult, const char *pszPrefix)
1262{
1263 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1264
1265 /*
1266 * cArgs == 0: Show all
1267 */
1268 if (cArgs == 0)
1269 {
1270 /*
1271 * Get register context.
1272 */
1273 int rc;
1274 PCPUMCTX pCtx;
1275 PCCPUMCTXCORE pCtxCore;
1276 if (!*pszPrefix)
1277 {
1278 rc = CPUMQueryGuestCtxPtr(pVM, &pCtx);
1279 pCtxCore = CPUMGetGuestCtxCore(pVM);
1280 }
1281 else
1282 {
1283 rc = CPUMQueryHyperCtxPtr(pVM, &pCtx);
1284 pCtxCore = CPUMGetHyperCtxCore(pVM);
1285 }
1286 if (VBOX_FAILURE(rc))
1287 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Getting register context\n");
1288
1289 /*
1290 * Format the flags.
1291 */
1292 static struct
1293 {
1294 const char *pszSet; const char *pszClear; uint32_t fFlag;
1295 } aFlags[] =
1296 {
1297 { "vip",NULL, X86_EFL_VIP },
1298 { "vif",NULL, X86_EFL_VIF },
1299 { "ac", NULL, X86_EFL_AC },
1300 { "vm", NULL, X86_EFL_VM },
1301 { "rf", NULL, X86_EFL_RF },
1302 { "nt", NULL, X86_EFL_NT },
1303 { "ov", "nv", X86_EFL_OF },
1304 { "dn", "up", X86_EFL_DF },
1305 { "ei", "di", X86_EFL_IF },
1306 { "tf", NULL, X86_EFL_TF },
1307 { "ng", "pl", X86_EFL_SF },
1308 { "zr", "nz", X86_EFL_ZF },
1309 { "ac", "na", X86_EFL_AF },
1310 { "po", "pe", X86_EFL_PF },
1311 { "cy", "nc", X86_EFL_CF },
1312 };
1313 char szEFlags[80];
1314 char *psz = szEFlags;
1315 uint32_t efl = pCtxCore->eflags.u32;
1316 for (unsigned i = 0; i < ELEMENTS(aFlags); i++)
1317 {
1318 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
1319 if (pszAdd)
1320 {
1321 strcpy(psz, pszAdd);
1322 psz += strlen(pszAdd);
1323 *psz++ = ' ';
1324 }
1325 }
1326 psz[-1] = '\0';
1327
1328
1329 /*
1330 * Format the registers.
1331 */
1332 if (pDbgc->fRegTerse)
1333 {
1334 if (CPUMIsGuestIn64BitCodeEx(pCtx))
1335 {
1336 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1337 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
1338 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
1339 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
1340 "%sr14=%016RX64 %sr15=%016RX64\n"
1341 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
1342 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %sss=%04x %seflags=%08x\n",
1343 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
1344 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
1345 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
1346 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 34 : 31, szEFlags,
1347 pszPrefix, (RTSEL)pCtxCore->cs, pszPrefix, (RTSEL)pCtxCore->ds, pszPrefix, (RTSEL)pCtxCore->es,
1348 pszPrefix, (RTSEL)pCtxCore->fs, pszPrefix, (RTSEL)pCtxCore->gs, pszPrefix, (RTSEL)pCtxCore->ss, pszPrefix, efl);
1349 }
1350 else
1351 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1352 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
1353 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
1354 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %sss=%04x %seflags=%08x\n",
1355 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
1356 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 34 : 31, szEFlags,
1357 pszPrefix, (RTSEL)pCtxCore->cs, pszPrefix, (RTSEL)pCtxCore->ds, pszPrefix, (RTSEL)pCtxCore->es,
1358 pszPrefix, (RTSEL)pCtxCore->fs, pszPrefix, (RTSEL)pCtxCore->gs, pszPrefix, (RTSEL)pCtxCore->ss, pszPrefix, efl);
1359 }
1360 else
1361 {
1362 if (CPUMIsGuestIn64BitCodeEx(pCtx))
1363 {
1364 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1365 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
1366 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
1367 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
1368 "%sr14=%016RX64 %sr15=%016RX64\n"
1369 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
1370 "%scs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1371 "%sds={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1372 "%ses={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1373 "%sfs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1374 "%sgs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1375 "%sss={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1376 "%scr0=%016RX64 %scr2=%016RX64 %scr3=%016RX64 %scr4=%016RX64\n"
1377 "%sdr0=%016RX64 %sdr1=%016RX64 %sdr2=%016RX64 %sdr3=%016RX64\n"
1378 "%sdr4=%016RX64 %sdr5=%016RX64 %sdr6=%016RX64 %sdr7=%016RX64\n"
1379 "%sgdtr=%016RX64:%04x %sidtr=%016RX64:%04x %seflags=%08x\n"
1380 "%sldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1381 "%str ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1382 "%sSysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1383 ,
1384 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
1385 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
1386 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
1387 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
1388 pszPrefix, (RTSEL)pCtxCore->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u,
1389 pszPrefix, (RTSEL)pCtxCore->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u,
1390 pszPrefix, (RTSEL)pCtxCore->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u,
1391 pszPrefix, (RTSEL)pCtxCore->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u,
1392 pszPrefix, (RTSEL)pCtxCore->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u,
1393 pszPrefix, (RTSEL)pCtxCore->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u,
1394 pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
1395 pszPrefix, pCtx->dr0, pszPrefix, pCtx->dr1, pszPrefix, pCtx->dr2, pszPrefix, pCtx->dr3,
1396 pszPrefix, pCtx->dr4, pszPrefix, pCtx->dr5, pszPrefix, pCtx->dr6, pszPrefix, pCtx->dr7,
1397 pszPrefix, pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, efl,
1398 pszPrefix, (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u64Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
1399 pszPrefix, (RTSEL)pCtx->tr, pCtx->trHid.u64Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
1400 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp);
1401
1402 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1403 "MSR:\n"
1404 "%sEFER =%016RX64\n"
1405 "%sPAT =%016RX64\n"
1406 "%sSTAR =%016RX64\n"
1407 "%sCSTAR =%016RX64\n"
1408 "%sLSTAR =%016RX64\n"
1409 "%sSFMASK =%016RX64\n"
1410 "%sKERNELGSBASE =%016RX64\n",
1411 pszPrefix, pCtx->msrEFER,
1412 pszPrefix, pCtx->msrPAT,
1413 pszPrefix, pCtx->msrSTAR,
1414 pszPrefix, pCtx->msrCSTAR,
1415 pszPrefix, pCtx->msrLSTAR,
1416 pszPrefix, pCtx->msrSFMASK,
1417 pszPrefix, pCtx->msrKERNELGSBASE);
1418
1419 }
1420 else
1421 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1422 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
1423 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
1424 "%scs={%04x base=%016RX64 limit=%08x flags=%08x} %sdr0=%016RX64 %sdr1=%016RX64\n"
1425 "%sds={%04x base=%016RX64 limit=%08x flags=%08x} %sdr2=%016RX64 %sdr3=%016RX64\n"
1426 "%ses={%04x base=%016RX64 limit=%08x flags=%08x} %sdr4=%016RX64 %sdr5=%016RX64\n"
1427 "%sfs={%04x base=%016RX64 limit=%08x flags=%08x} %sdr6=%016RX64 %sdr7=%016RX64\n"
1428 "%sgs={%04x base=%016RX64 limit=%08x flags=%08x} %scr0=%016RX64 %scr2=%016RX64\n"
1429 "%sss={%04x base=%016RX64 limit=%08x flags=%08x} %scr3=%016RX64 %scr4=%016RX64\n"
1430 "%sgdtr=%016RX64:%04x %sidtr=%016RX64:%04x %seflags=%08x\n"
1431 "%sldtr={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1432 "%str ={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1433 "%sSysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1434 "%sFCW=%04x %sFSW=%04x %sFTW=%04x\n"
1435 ,
1436 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
1437 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
1438 pszPrefix, (RTSEL)pCtxCore->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u, pszPrefix, pCtx->dr0, pszPrefix, pCtx->dr1,
1439 pszPrefix, (RTSEL)pCtxCore->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u, pszPrefix, pCtx->dr2, pszPrefix, pCtx->dr3,
1440 pszPrefix, (RTSEL)pCtxCore->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u, pszPrefix, pCtx->dr4, pszPrefix, pCtx->dr5,
1441 pszPrefix, (RTSEL)pCtxCore->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u, pszPrefix, pCtx->dr6, pszPrefix, pCtx->dr7,
1442 pszPrefix, (RTSEL)pCtxCore->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u, pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2,
1443 pszPrefix, (RTSEL)pCtxCore->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
1444 pszPrefix, pCtx->gdtr.pGdt,pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, pCtxCore->eflags,
1445 pszPrefix, (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u64Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
1446 pszPrefix, (RTSEL)pCtx->tr, pCtx->trHid.u64Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
1447 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp,
1448 pszPrefix, pCtx->fpu.FCW, pszPrefix, pCtx->fpu.FSW, pszPrefix, pCtx->fpu.FTW);
1449 }
1450
1451 /*
1452 * Disassemble one instruction at cs:eip.
1453 */
1454 return pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pCtx->cs, pCtx->eip);
1455 }
1456
1457 /*
1458 * cArgs == 1: Show the register.
1459 * cArgs == 2: Modify the register.
1460 */
1461 if ( cArgs == 1
1462 || cArgs == 2)
1463 {
1464 /* locate the register symbol. */
1465 const char *pszReg = paArgs[0].u.pszString;
1466 if ( *pszPrefix
1467 && pszReg[0] != *pszPrefix)
1468 {
1469 /* prepend the prefix. */
1470 char *psz = (char *)alloca(strlen(pszReg) + 2);
1471 psz[0] = *pszPrefix;
1472 strcpy(psz + 1, paArgs[0].u.pszString);
1473 pszReg = psz;
1474 }
1475 PCDBGCSYM pSym = dbgcLookupRegisterSymbol(pDbgc, pszReg);
1476 if (!pSym)
1477 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER /* VERR_DBGC_INVALID_REGISTER */, "Invalid register name '%s'.\n", pszReg);
1478
1479 /* show the register */
1480 if (cArgs == 1)
1481 {
1482 DBGCVAR Var;
1483 memset(&Var, 0, sizeof(Var));
1484 int rc = pSym->pfnGet(pSym, pCmdHlp, DBGCVAR_TYPE_NUMBER, &Var);
1485 if (VBOX_FAILURE(rc))
1486 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Failed getting value for register '%s'.\n", pszReg);
1487 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s=%Dv\n", pszReg, &Var);
1488 }
1489
1490 /* change the register */
1491 int rc = pSym->pfnSet(pSym, pCmdHlp, &paArgs[1]);
1492 if (VBOX_FAILURE(rc))
1493 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Failed setting value for register '%s'.\n", pszReg);
1494 return VINF_SUCCESS;
1495 }
1496
1497
1498 NOREF(pCmd); NOREF(paArgs); NOREF(pResult);
1499 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Huh? cArgs=%d Expected 0, 1 or 2!\n", cArgs);
1500}
1501
1502
1503/**
1504 * The 'rg' command.
1505 *
1506 * @returns VBox status.
1507 * @param pCmd Pointer to the command descriptor (as registered).
1508 * @param pCmdHlp Pointer to command helper functions.
1509 * @param pVM Pointer to the current VM (if any).
1510 * @param paArgs Pointer to (readonly) array of arguments.
1511 * @param cArgs Number of arguments in the array.
1512 */
1513static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1514{
1515 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult, "");
1516}
1517
1518
1519/**
1520 * The 'rh' command.
1521 *
1522 * @returns VBox status.
1523 * @param pCmd Pointer to the command descriptor (as registered).
1524 * @param pCmdHlp Pointer to command helper functions.
1525 * @param pVM Pointer to the current VM (if any).
1526 * @param paArgs Pointer to (readonly) array of arguments.
1527 * @param cArgs Number of arguments in the array.
1528 */
1529static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1530{
1531 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult, ".");
1532}
1533
1534
1535/**
1536 * The 'rt' command.
1537 *
1538 * @returns VBox status.
1539 * @param pCmd Pointer to the command descriptor (as registered).
1540 * @param pCmdHlp Pointer to command helper functions.
1541 * @param pVM Pointer to the current VM (if any).
1542 * @param paArgs Pointer to (readonly) array of arguments.
1543 * @param cArgs Number of arguments in the array.
1544 */
1545static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1546{
1547 NOREF(pCmd); NOREF(pVM); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1548
1549 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1550 pDbgc->fRegTerse = !pDbgc->fRegTerse;
1551 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
1552}
1553
1554
1555/**
1556 * The 't' command.
1557 *
1558 * @returns VBox status.
1559 * @param pCmd Pointer to the command descriptor (as registered).
1560 * @param pCmdHlp Pointer to command helper functions.
1561 * @param pVM Pointer to the current VM (if any).
1562 * @param paArgs Pointer to (readonly) array of arguments.
1563 * @param cArgs Number of arguments in the array.
1564 */
1565static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1566{
1567 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1568
1569 int rc = DBGFR3Step(pVM);
1570 if (VBOX_SUCCESS(rc))
1571 pDbgc->fReady = false;
1572 else
1573 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
1574
1575 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1576 return rc;
1577}
1578
1579
1580/**
1581 * The 'k', 'kg' and 'kh' commands.
1582 *
1583 * @returns VBox status.
1584 * @param pCmd Pointer to the command descriptor (as registered).
1585 * @param pCmdHlp Pointer to command helper functions.
1586 * @param pVM Pointer to the current VM (if any).
1587 * @param paArgs Pointer to (readonly) array of arguments.
1588 * @param cArgs Number of arguments in the array.
1589 */
1590static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1591{
1592 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1593
1594 /*
1595 * Figure which context we're called for.
1596 */
1597 bool fGuest = pCmd->pszCmd[1] == 'g'
1598 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
1599
1600
1601 DBGFSTACKFRAME Frame;
1602 memset(&Frame, 0, sizeof(Frame));
1603 int rc;
1604 if (fGuest)
1605 rc = DBGFR3StackWalkBeginGuest(pVM, &Frame);
1606 else
1607 rc = DBGFR3StackWalkBeginHyper(pVM, &Frame);
1608 if (VBOX_FAILURE(rc))
1609 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to begin stack walk, rc=%Vrc\n", rc);
1610
1611 /*
1612 * Print header.
1613 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
1614 */
1615 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1616 if (VBOX_FAILURE(rc))
1617 return rc;
1618 do
1619 {
1620 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1621 (uint32_t)Frame.AddrFrame.off,
1622 (uint32_t)Frame.AddrReturnFrame.off,
1623 (uint32_t)Frame.AddrReturnPC.Sel,
1624 (uint32_t)Frame.AddrReturnPC.off,
1625 Frame.Args.au32[0],
1626 Frame.Args.au32[1],
1627 Frame.Args.au32[2],
1628 Frame.Args.au32[3]);
1629 if (VBOX_FAILURE(rc))
1630 return rc;
1631 if (!Frame.pSymPC)
1632 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %RTsel:%08RGv", Frame.AddrPC.Sel, Frame.AddrPC.off);
1633 else
1634 {
1635 RTGCINTPTR offDisp = Frame.AddrPC.FlatPtr - Frame.pSymPC->Value; /** @todo this isn't 100% correct for segemnted stuff. */
1636 if (offDisp > 0)
1637 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s+%llx", Frame.pSymPC->szName, (int64_t)offDisp);
1638 else if (offDisp < 0)
1639 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s-%llx", Frame.pSymPC->szName, -(int64_t)offDisp);
1640 else
1641 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s", Frame.pSymPC->szName);
1642 }
1643 if (VBOX_SUCCESS(rc) && Frame.pLinePC)
1644 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " [%s @ 0i%d]", Frame.pLinePC->szFilename, Frame.pLinePC->uLineNo);
1645 if (VBOX_SUCCESS(rc))
1646 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
1647 if (VBOX_FAILURE(rc))
1648 return rc;
1649
1650 /* next */
1651 rc = DBGFR3StackWalkNext(pVM, &Frame);
1652 } while (VBOX_SUCCESS(rc));
1653
1654 NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1655 return VINF_SUCCESS;
1656}
1657
1658
1659static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP /*pCmdHlp*/, PCX86DESC64 /*pDesc*/, unsigned /*iEntry*/, bool /* fHyper */, bool * /*fDblEntry*/)
1660{
1661 /* GUEST64 */
1662 return VINF_SUCCESS;
1663}
1664
1665
1666/**
1667 * Wroker function that displays one descriptor entry (GDT, LDT, IDT).
1668 *
1669 * @returns pfnPrintf status code.
1670 * @param pCmdHlp The DBGC command helpers.
1671 * @param pDesc The descriptor to display.
1672 * @param iEntry The descriptor entry number.
1673 * @param fHyper Whether the selector belongs to the hypervisor or not.
1674 */
1675static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
1676{
1677 int rc;
1678
1679 const char *pszHyper = fHyper ? " HYPER" : "";
1680 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
1681 if (pDesc->Gen.u1DescType)
1682 {
1683 static const char * const s_apszTypes[] =
1684 {
1685 "DataRO", /* 0 Read-Only */
1686 "DataRO", /* 1 Read-Only - Accessed */
1687 "DataRW", /* 2 Read/Write */
1688 "DataRW", /* 3 Read/Write - Accessed */
1689 "DownRO", /* 4 Expand-down, Read-Only */
1690 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
1691 "DownRW", /* 6 Expand-down, Read/Write */
1692 "DownRO", /* 7 Expand-down, Read/Write - Accessed */
1693 "CodeEO", /* 8 Execute-Only */
1694 "CodeEO", /* 9 Execute-Only - Accessed */
1695 "CodeER", /* A Execute/Readable */
1696 "CodeER", /* B Execute/Readable - Accessed */
1697 "ConfE0", /* C Conforming, Execute-Only */
1698 "ConfE0", /* D Conforming, Execute-Only - Accessed */
1699 "ConfER", /* E Conforming, Execute/Readable */
1700 "ConfER" /* F Conforming, Execute/Readable - Accessed */
1701 };
1702 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
1703 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1704 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1705 uint32_t u32Base = pDesc->Gen.u16BaseLow
1706 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1707 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1708 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1709 if (pDesc->Gen.u1Granularity)
1710 cbLimit <<= PAGE_SHIFT;
1711
1712 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
1713 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1714 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
1715 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
1716 }
1717 else
1718 {
1719 static const char * const s_apszTypes[] =
1720 {
1721 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
1722 "Tss16A", /* 1 0001 Available 16-bit TSS */
1723 "LDT ", /* 2 0010 LDT */
1724 "Tss16B", /* 3 0011 Busy 16-bit TSS */
1725 "Call16", /* 4 0100 16-bit Call Gate */
1726 "TaskG ", /* 5 0101 Task Gate */
1727 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
1728 "Trap16", /* 7 0111 16-bit Trap Gate */
1729 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
1730 "Tss32A", /* 9 1001 Available 32-bit TSS */
1731 "Ill-A ", /* A 1010 Reserved (Illegal) */
1732 "Tss32B", /* B 1011 Busy 32-bit TSS */
1733 "Call32", /* C 1100 32-bit Call Gate */
1734 "Ill-D ", /* D 1101 Reserved (Illegal) */
1735 "Int32 ", /* E 1110 32-bit Interrupt Gate */
1736 "Trap32" /* F 1111 32-bit Trap Gate */
1737 };
1738 switch (pDesc->Gen.u4Type)
1739 {
1740 /* raw */
1741 case X86_SEL_TYPE_SYS_UNDEFINED:
1742 case X86_SEL_TYPE_SYS_UNDEFINED2:
1743 case X86_SEL_TYPE_SYS_UNDEFINED4:
1744 case X86_SEL_TYPE_SYS_UNDEFINED3:
1745 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s %.8Rhxs DPL=%d %s%s\n",
1746 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
1747 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1748 break;
1749
1750 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1751 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1752 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1753 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1754 case X86_SEL_TYPE_SYS_LDT:
1755 {
1756 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1757 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
1758 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1759 uint32_t u32Base = pDesc->Gen.u16BaseLow
1760 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1761 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1762 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1763 if (pDesc->Gen.u1Granularity)
1764 cbLimit <<= PAGE_SHIFT;
1765
1766 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
1767 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1768 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
1769 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
1770 pszHyper);
1771 break;
1772 }
1773
1774 case X86_SEL_TYPE_SYS_TASK_GATE:
1775 {
1776 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s TSS=%04x DPL=%d %s%s\n",
1777 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
1778 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1779 break;
1780 }
1781
1782 case X86_SEL_TYPE_SYS_286_CALL_GATE:
1783 case X86_SEL_TYPE_SYS_386_CALL_GATE:
1784 {
1785 unsigned cParams = pDesc->au8[0] & 0x1f;
1786 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
1787 RTSEL sel = pDesc->au16[1];
1788 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
1789 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
1790 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1791 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
1792 break;
1793 }
1794
1795 case X86_SEL_TYPE_SYS_286_INT_GATE:
1796 case X86_SEL_TYPE_SYS_386_INT_GATE:
1797 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
1798 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
1799 {
1800 RTSEL sel = pDesc->au16[1];
1801 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
1802 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
1803 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1804 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1805 break;
1806 }
1807
1808 /* impossible, just it's necessary to keep gcc happy. */
1809 default:
1810 return VINF_SUCCESS;
1811 }
1812 }
1813 return rc;
1814}
1815
1816
1817/**
1818 * The 'dg', 'dga', 'dl' and 'dla' commands.
1819 *
1820 * @returns VBox status.
1821 * @param pCmd Pointer to the command descriptor (as registered).
1822 * @param pCmdHlp Pointer to command helper functions.
1823 * @param pVM Pointer to the current VM (if any).
1824 * @param paArgs Pointer to (readonly) array of arguments.
1825 * @param cArgs Number of arguments in the array.
1826 */
1827static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1828{
1829 /*
1830 * Validate input.
1831 */
1832 if (!pVM)
1833 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
1834
1835 /*
1836 * Get the CPU mode, check which command variation this is
1837 * and fix a default parameter if needed.
1838 */
1839 CPUMMODE enmMode = CPUMGetGuestMode(pVM);
1840 bool fGdt = pCmd->pszCmd[1] == 'g';
1841 bool fAll = pCmd->pszCmd[2] == 'a';
1842
1843 DBGCVAR Var;
1844 if (!cArgs)
1845 {
1846 cArgs = 1;
1847 paArgs = &Var;
1848 Var.enmType = DBGCVAR_TYPE_NUMBER;
1849 Var.u.u64Number = fGdt ? 0 : 4;
1850 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1851 Var.u64Range = 1024;
1852 }
1853
1854 /*
1855 * Process the arguments.
1856 */
1857 for (unsigned i = 0; i < cArgs; i++)
1858 {
1859 /*
1860 * Retrive the selector value from the argument.
1861 * The parser may confuse pointers and numbers if more than one
1862 * argument is given, that that into account.
1863 */
1864 /* check that what've got makes sense as we don't trust the parser yet. */
1865 if ( paArgs[i].enmType != DBGCVAR_TYPE_NUMBER
1866 && !DBGCVAR_ISPOINTER(paArgs[i].enmType))
1867 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: arg #%u isn't of number or pointer type but %d.\n", i, paArgs[i].enmType);
1868 uint64_t u64;
1869 unsigned cSels = 1;
1870 switch (paArgs[i].enmType)
1871 {
1872 case DBGCVAR_TYPE_NUMBER:
1873 u64 = paArgs[i].u.u64Number;
1874 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
1875 cSels = RT_MIN(paArgs[i].u64Range, 1024);
1876 break;
1877 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
1878 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
1879 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
1880 case DBGCVAR_TYPE_HC_FAR: u64 = paArgs[i].u.HCFar.sel; break;
1881 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
1882 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
1883 default: u64 = _64K; break;
1884 }
1885 if (u64 < _64K)
1886 {
1887 unsigned Sel = (RTSEL)u64;
1888
1889 /*
1890 * Dump the specified range.
1891 */
1892 bool fSingle = cSels == 1;
1893 while ( cSels-- > 0
1894 && Sel < _64K)
1895 {
1896 SELMSELINFO SelInfo;
1897 int rc = SELMR3GetSelectorInfo(pVM, Sel, &SelInfo);
1898 if (RT_SUCCESS(rc))
1899 {
1900 if (SelInfo.fRealMode)
1901 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x RealM Bas=%04x Lim=%04x\n",
1902 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
1903 else if (fAll || fSingle || SelInfo.Raw.Gen.u1Present)
1904 {
1905 if (enmMode == CPUMMODE_PROTECTED)
1906 rc = dbgcCmdDumpDTWorker32(pCmdHlp, (PX86DESC)&SelInfo.Raw, Sel, SelInfo.fHyper);
1907 else
1908 {
1909 bool fDblSkip = false;
1910 rc = dbgcCmdDumpDTWorker64(pCmdHlp, (PX86DESC64)&SelInfo.Raw, Sel, SelInfo.fHyper, &fDblSkip);
1911 if (fDblSkip)
1912 Sel += 4;
1913 }
1914 }
1915 }
1916 else
1917 {
1918 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %Vrc\n", Sel, rc);
1919 if (!fAll)
1920 return rc;
1921 }
1922 if (RT_FAILURE(rc))
1923 return rc;
1924
1925 /* next */
1926 Sel += 4;
1927 }
1928 }
1929 else
1930 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %llx is out of bounds\n", u64);
1931 }
1932
1933 NOREF(pResult);
1934 return VINF_SUCCESS;
1935}
1936
1937
1938/**
1939 * The 'di' and 'dia' commands.
1940 *
1941 * @returns VBox status.
1942 * @param pCmd Pointer to the command descriptor (as registered).
1943 * @param pCmdHlp Pointer to command helper functions.
1944 * @param pVM Pointer to the current VM (if any).
1945 * @param paArgs Pointer to (readonly) array of arguments.
1946 * @param cArgs Number of arguments in the array.
1947 */
1948static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1949{
1950 /*
1951 * Validate input.
1952 */
1953 if (!pVM)
1954 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
1955
1956 /*
1957 * Establish some stuff like the current IDTR and CPU mode,
1958 * and fix a default parameter.
1959 */
1960 uint16_t cbLimit;
1961 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVM, &cbLimit);
1962 CPUMMODE enmMode = CPUMGetGuestMode(pVM);
1963 unsigned cbEntry;
1964 switch (enmMode)
1965 {
1966 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
1967 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
1968 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
1969 default:
1970 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid CPU mode %d.\n", enmMode);
1971 }
1972
1973 bool fAll = pCmd->pszCmd[2] == 'a';
1974 DBGCVAR Var;
1975 if (!cArgs)
1976 {
1977 cArgs = 1;
1978 paArgs = &Var;
1979 Var.enmType = DBGCVAR_TYPE_NUMBER;
1980 Var.u.u64Number = 0;
1981 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1982 Var.u64Range = 256;
1983 }
1984
1985 /*
1986 * Process the arguments.
1987 */
1988 for (unsigned i = 0; i < cArgs; i++)
1989 {
1990 /* check that what've got makes sense as we don't trust the parser yet. */
1991 if (paArgs[i].enmType != DBGCVAR_TYPE_NUMBER)
1992 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: arg #%u isn't of number type but %d.\n", i, paArgs[i].enmType);
1993 if (paArgs[i].u.u64Number < 256)
1994 {
1995 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
1996 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
1997 ? paArgs[i].u64Range
1998 : 1;
1999 bool fSingle = cInts == 1;
2000 while ( cInts-- > 0
2001 && iInt < 256)
2002 {
2003 /*
2004 * Try read it.
2005 */
2006 union
2007 {
2008 RTFAR16 Real;
2009 X86DESC Prot;
2010 X86DESC64 Long;
2011 } u;
2012 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
2013 {
2014 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x not within the IDT\n", (unsigned)iInt);
2015 if (!fAll && !fSingle)
2016 return VINF_SUCCESS;
2017 }
2018 DBGCVAR AddrVar;
2019 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
2020 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
2021 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
2022 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &u, cbEntry, &AddrVar, NULL);
2023 if (VBOX_FAILURE(rc))
2024 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
2025
2026 /*
2027 * Display it.
2028 */
2029 switch (enmMode)
2030 {
2031 case CPUMMODE_REAL:
2032 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
2033 /** @todo resolve 16:16 IDTE to a symbol */
2034 break;
2035 case CPUMMODE_PROTECTED:
2036 if (fAll || fSingle || u.Prot.Gen.u1Present)
2037 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
2038 break;
2039 case CPUMMODE_LONG:
2040 if (fAll || fSingle || u.Long.Gen.u1Present)
2041 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
2042 break;
2043 default: break; /* to shut up gcc */
2044 }
2045 if (RT_FAILURE(rc))
2046 return rc;
2047
2048 /* next */
2049 iInt++;
2050 }
2051 }
2052 else
2053 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
2054 }
2055
2056 NOREF(pResult);
2057 return VINF_SUCCESS;
2058}
2059
2060
2061/**
2062 * The 'da', 'dq', 'dd', 'dw' and 'db' commands.
2063 *
2064 * @returns VBox status.
2065 * @param pCmd Pointer to the command descriptor (as registered).
2066 * @param pCmdHlp Pointer to command helper functions.
2067 * @param pVM Pointer to the current VM (if any).
2068 * @param paArgs Pointer to (readonly) array of arguments.
2069 * @param cArgs Number of arguments in the array.
2070 */
2071static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2072{
2073 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2074
2075 /*
2076 * Validate input.
2077 */
2078 if ( cArgs > 1
2079 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2080 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2081 if (!pVM)
2082 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2083
2084 /*
2085 * Figure out the element size.
2086 */
2087 unsigned cbElement;
2088 bool fAscii = false;
2089 switch (pCmd->pszCmd[1])
2090 {
2091 default:
2092 case 'b': cbElement = 1; break;
2093 case 'w': cbElement = 2; break;
2094 case 'd': cbElement = 4; break;
2095 case 'q': cbElement = 8; break;
2096 case 'a':
2097 cbElement = 1;
2098 fAscii = true;
2099 break;
2100 case '\0':
2101 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
2102 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
2103 if (!cbElement)
2104 cbElement = 1;
2105 break;
2106 }
2107
2108 /*
2109 * Find address.
2110 */
2111 if (!cArgs)
2112 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
2113 else
2114 pDbgc->DumpPos = paArgs[0];
2115
2116 /*
2117 * Range.
2118 */
2119 switch (pDbgc->DumpPos.enmRangeType)
2120 {
2121 case DBGCVAR_RANGE_NONE:
2122 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2123 pDbgc->DumpPos.u64Range = 0x60;
2124 break;
2125
2126 case DBGCVAR_RANGE_ELEMENTS:
2127 if (pDbgc->DumpPos.u64Range > 2048)
2128 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many elements requested. Max is 2048 elements.\n");
2129 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2130 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
2131 break;
2132
2133 case DBGCVAR_RANGE_BYTES:
2134 if (pDbgc->DumpPos.u64Range > 65536)
2135 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
2136 break;
2137
2138 default:
2139 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
2140 }
2141
2142 /*
2143 * Do the dumping.
2144 */
2145 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
2146 int cbLeft = (int)pDbgc->DumpPos.u64Range;
2147 uint8_t u8Prev = '\0';
2148 for (;;)
2149 {
2150 /*
2151 * Read memory.
2152 */
2153 char achBuffer[16];
2154 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
2155 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
2156 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
2157 if (VBOX_FAILURE(rc))
2158 {
2159 if (u8Prev && u8Prev != '\n')
2160 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2161 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
2162 }
2163
2164 /*
2165 * Display it.
2166 */
2167 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
2168 if (!fAscii)
2169 {
2170 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:", &pDbgc->DumpPos);
2171 unsigned i;
2172 for (i = 0; i < cb; i += cbElement)
2173 {
2174 const char *pszSpace = " ";
2175 if (cbElement <= 2 && i == 8 && !fAscii)
2176 pszSpace = "-";
2177 switch (cbElement)
2178 {
2179 case 1: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
2180 case 2: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
2181 case 4: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
2182 case 8: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
2183 }
2184 }
2185
2186 /* chars column */
2187 if (pDbgc->cbDumpElement == 1)
2188 {
2189 while (i++ < sizeof(achBuffer))
2190 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2191 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2192 for (i = 0; i < cb; i += cbElement)
2193 {
2194 uint8_t u8 = *(uint8_t *)&achBuffer[i];
2195 if (isprint(u8) && u8 < 127 && u8 >= 32)
2196 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2197 else
2198 pCmdHlp->pfnPrintf(pCmdHlp, NULL, ".");
2199 }
2200 }
2201 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2202 }
2203 else
2204 {
2205 /*
2206 * We print up to the first zero and stop there.
2207 * Only printables + '\t' and '\n' are printed.
2208 */
2209 if (!u8Prev)
2210 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DumpPos);
2211 uint8_t u8 = '\0';
2212 unsigned i;
2213 for (i = 0; i < cb; i++)
2214 {
2215 u8Prev = u8;
2216 u8 = *(uint8_t *)&achBuffer[i];
2217 if ( u8 < 127
2218 && ( (isprint(u8) && u8 >= 32)
2219 || u8 == '\t'
2220 || u8 == '\n'))
2221 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2222 else if (!u8)
2223 break;
2224 else
2225 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\\x%x", u8);
2226 }
2227 if (u8 == '\0')
2228 cb = cbLeft = i + 1;
2229 if (cbLeft - cb <= 0 && u8Prev != '\n')
2230 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2231 }
2232
2233 /*
2234 * Advance
2235 */
2236 cbLeft -= (int)cb;
2237 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
2238 if (VBOX_FAILURE(rc))
2239 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
2240 if (cbLeft <= 0)
2241 break;
2242 }
2243
2244 NOREF(pCmd); NOREF(pResult);
2245 return VINF_SUCCESS;
2246}
2247
2248
2249/**
2250 * Best guess at which paging mode currently applies to the guest
2251 * paging structures.
2252 *
2253 * This have to come up with a decent answer even when the guest
2254 * is in non-paged protected mode or real mode.
2255 *
2256 * @returns cr3.
2257 * @param pDbgc The DBGC instance.
2258 * @param pfPAE Where to store the page address extension indicator.
2259 * @param pfLME Where to store the long mode enabled indicator.
2260 * @param pfPSE Where to store the page size extension indicator.
2261 * @param pfPGE Where to store the page global enabled indicator.
2262 * @param pfNXE Where to store the no-execution enabled inidicator.
2263 */
2264static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2265{
2266 RTGCUINTREG cr4 = CPUMGetGuestCR4(pDbgc->pVM);
2267 *pfPSE = !!(cr4 & X86_CR4_PSE);
2268 *pfPGE = !!(cr4 & X86_CR4_PGE);
2269 *pfPAE = !!(cr4 & X86_CR4_PAE);
2270 *pfLME = CPUMGetGuestMode(pDbgc->pVM) == CPUMMODE_LONG;
2271 *pfNXE = false; /* GUEST64 GUESTNX */
2272 return CPUMGetGuestCR3(pDbgc->pVM);
2273}
2274
2275
2276/**
2277 * Determin the shadow paging mode.
2278 *
2279 * @returns cr3.
2280 * @param pDbgc The DBGC instance.
2281 * @param pfPAE Where to store the page address extension indicator.
2282 * @param pfLME Where to store the long mode enabled indicator.
2283 * @param pfPSE Where to store the page size extension indicator.
2284 * @param pfPGE Where to store the page global enabled indicator.
2285 * @param pfNXE Where to store the no-execution enabled inidicator.
2286 */
2287static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2288{
2289 *pfPSE = true;
2290 *pfPGE = false;
2291 switch (PGMGetShadowMode(pDbgc->pVM))
2292 {
2293 default:
2294 case PGMMODE_32_BIT:
2295 *pfPAE = *pfLME = *pfNXE = false;
2296 break;
2297 case PGMMODE_PAE:
2298 *pfLME = *pfNXE = false;
2299 *pfPAE = true;
2300 break;
2301 case PGMMODE_PAE_NX:
2302 *pfLME = false;
2303 *pfPAE = *pfNXE = true;
2304 break;
2305 case PGMMODE_AMD64:
2306 *pfNXE = false;
2307 *pfPAE = *pfLME = true;
2308 break;
2309 case PGMMODE_AMD64_NX:
2310 *pfPAE = *pfLME = *pfNXE = true;
2311 break;
2312 }
2313 return PGMGetHyperCR3(pDbgc->pVM);
2314}
2315
2316
2317/**
2318 * The 'dpd', 'dpda', 'dpdb', 'dpdg' and 'dpdh' commands.
2319 *
2320 * @returns VBox status.
2321 * @param pCmd Pointer to the command descriptor (as registered).
2322 * @param pCmdHlp Pointer to command helper functions.
2323 * @param pVM Pointer to the current VM (if any).
2324 * @param paArgs Pointer to (readonly) array of arguments.
2325 * @param cArgs Number of arguments in the array.
2326 */
2327static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2328{
2329 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2330
2331 /*
2332 * Validate input.
2333 */
2334 if ( cArgs > 1
2335 || (cArgs == 1 && pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2336 || (cArgs == 1 && pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2337 )
2338 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2339 if (!pVM)
2340 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2341
2342 /*
2343 * Guest or shadow page directories? Get the paging parameters.
2344 */
2345 bool fGuest = pCmd->pszCmd[3] != 'h';
2346 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2347 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2348 ? pDbgc->fRegCtxGuest
2349 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2350
2351 bool fPAE, fLME, fPSE, fPGE, fNXE;
2352 uint64_t cr3 = fGuest
2353 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2354 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2355 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2356
2357 /*
2358 * Setup default arugment if none was specified.
2359 * Fix address / index confusion.
2360 */
2361 DBGCVAR VarDefault;
2362 if (!cArgs)
2363 {
2364 if (pCmd->pszCmd[3] == 'a')
2365 {
2366 if (fLME || fPAE)
2367 return DBGCCmdHlpPrintf(pCmdHlp, "Default argument for 'dpda' hasn't been fully implemented yet. Try with an address or use one of the other commands.\n");
2368 if (fGuest)
2369 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
2370 else
2371 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
2372 }
2373 else
2374 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
2375 paArgs = &VarDefault;
2376 cArgs = 1;
2377 }
2378 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
2379 {
2380 Assert(pCmd->pszCmd[3] != 'a');
2381 VarDefault = paArgs[0];
2382 if (VarDefault.u.u64Number <= 1024)
2383 {
2384 if (fPAE)
2385 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
2386 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
2387 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
2388 VarDefault.u.u64Number <<= X86_PD_SHIFT;
2389 }
2390 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
2391 paArgs = &VarDefault;
2392 }
2393
2394 /*
2395 * Locate the PDE to start displaying at.
2396 *
2397 * The 'dpda' command takes the address of a PDE, while the others are guest
2398 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
2399 * while the others require us to do all the tedious walking thru the paging
2400 * hierarchy to find the intended PDE.
2401 */
2402 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
2403 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
2404 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
2405 unsigned cEntries; /* The number of entries to display. */
2406 unsigned cEntriesMax; /* The max number of entries to display. */
2407 int rc;
2408 if (pCmd->pszCmd[3] == 'a')
2409 {
2410 VarPDEAddr = paArgs[0];
2411 switch (VarPDEAddr.enmRangeType)
2412 {
2413 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
2414 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
2415 default: cEntries = 10; break;
2416 }
2417 cEntriesMax = PAGE_SIZE / cbEntry;
2418 }
2419 else
2420 {
2421 /*
2422 * Determin the range.
2423 */
2424 switch (paArgs[0].enmRangeType)
2425 {
2426 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2427 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2428 default: cEntries = 10; break;
2429 }
2430
2431 /*
2432 * Normalize the input address, it must be a flat GC address.
2433 */
2434 rc = pCmdHlp->pfnEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2435 if (VBOX_FAILURE(rc))
2436 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2437 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2438 {
2439 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2440 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2441 }
2442 if (fPAE)
2443 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
2444 else
2445 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
2446
2447 /*
2448 * Do the paging walk until we get to the page directory.
2449 */
2450 DBGCVAR VarCur;
2451 if (fGuest)
2452 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2453 else
2454 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2455 if (fLME)
2456 {
2457 /* Page Map Level 4 Lookup. */
2458 /* Check if it's a valid address first? */
2459 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2460 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2461 X86PML4E Pml4e;
2462 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2463 if (VBOX_FAILURE(rc))
2464 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2465 if (!Pml4e.n.u1Present)
2466 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2467
2468 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2469 Assert(fPAE);
2470 }
2471 if (fPAE)
2472 {
2473 /* Page directory pointer table. */
2474 X86PDPE Pdpe;
2475 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2476 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2477 if (VBOX_FAILURE(rc))
2478 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2479 if (!Pdpe.n.u1Present)
2480 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2481
2482 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2483 VarPDEAddr = VarCur;
2484 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2485 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
2486 }
2487 else
2488 {
2489 /* 32-bit legacy - CR3 == page directory. */
2490 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
2491 VarPDEAddr = VarCur;
2492 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
2493 }
2494 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2495 iEntry /= cbEntry;
2496 }
2497
2498 /* adjust cEntries */
2499 cEntries = RT_MAX(1, cEntries);
2500 cEntries = RT_MIN(cEntries, cEntriesMax);
2501
2502 /*
2503 * The display loop.
2504 */
2505 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
2506 &VarPDEAddr, iEntry);
2507 do
2508 {
2509 /*
2510 * Read.
2511 */
2512 X86PDEPAE Pde;
2513 Pde.u = 0;
2514 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, cbEntry, &VarPDEAddr, NULL);
2515 if (VBOX_FAILURE(rc))
2516 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
2517
2518 /*
2519 * Display.
2520 */
2521 if (iEntry != ~0U)
2522 {
2523 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2524 iEntry++;
2525 }
2526 if (fPSE && Pde.b.u1Size)
2527 DBGCCmdHlpPrintf(pCmdHlp,
2528 fPAE
2529 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2530 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2531 Pde.u,
2532 Pde.u & X86_PDE_PAE_PG_MASK,
2533 Pde.b.u1Present ? "p " : "np",
2534 Pde.b.u1Write ? "w" : "r",
2535 Pde.b.u1User ? "u" : "s",
2536 Pde.b.u1Accessed ? "a " : "na",
2537 Pde.b.u1Dirty ? "d " : "nd",
2538 Pde.b.u3Available,
2539 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
2540 Pde.b.u1WriteThru ? "pwt" : " ",
2541 Pde.b.u1CacheDisable ? "pcd" : " ",
2542 Pde.b.u1PAT ? "pat" : "",
2543 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2544 else
2545 DBGCCmdHlpPrintf(pCmdHlp,
2546 fPAE
2547 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
2548 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
2549 Pde.u,
2550 Pde.u & X86_PDE_PAE_PG_MASK,
2551 Pde.n.u1Present ? "p " : "np",
2552 Pde.n.u1Write ? "w" : "r",
2553 Pde.n.u1User ? "u" : "s",
2554 Pde.n.u1Accessed ? "a " : "na",
2555 Pde.u & RT_BIT(6) ? "6 " : " ",
2556 Pde.n.u3Available,
2557 Pde.u & RT_BIT(8) ? "8" : " ",
2558 Pde.n.u1WriteThru ? "pwt" : " ",
2559 Pde.n.u1CacheDisable ? "pcd" : " ",
2560 Pde.u & RT_BIT(7) ? "7" : "",
2561 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2562 if (Pde.u & UINT64_C(0x7fff000000000000))
2563 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
2564 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2565 if (VBOX_FAILURE(rc))
2566 return rc;
2567
2568 /*
2569 * Advance.
2570 */
2571 VarPDEAddr.u.u64Number += cbEntry;
2572 if (iEntry != ~0U)
2573 VarGCPtr.u.GCFlat += 1 << (fPAE ? X86_PD_PAE_SHIFT : X86_PD_SHIFT);
2574 } while (cEntries-- > 0);
2575
2576 NOREF(pResult);
2577 return VINF_SUCCESS;
2578}
2579
2580
2581/**
2582 * The 'dpdb' command.
2583 *
2584 * @returns VBox status.
2585 * @param pCmd Pointer to the command descriptor (as registered).
2586 * @param pCmdHlp Pointer to command helper functions.
2587 * @param pVM Pointer to the current VM (if any).
2588 * @param paArgs Pointer to (readonly) array of arguments.
2589 * @param cArgs Number of arguments in the array.
2590 */
2591static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2592{
2593 if (!pVM)
2594 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2595 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2596 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2597 if (VBOX_FAILURE(rc1))
2598 return rc1;
2599 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
2600 return rc2;
2601}
2602
2603
2604/**
2605 * The 'dpg*' commands.
2606 *
2607 * @returns VBox status.
2608 * @param pCmd Pointer to the command descriptor (as registered).
2609 * @param pCmdHlp Pointer to command helper functions.
2610 * @param pVM Pointer to the current VM (if any).
2611 * @param paArgs Pointer to (readonly) array of arguments.
2612 * @param cArgs Number of arguments in the array.
2613 */
2614static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2615{
2616 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2617
2618 /*
2619 * Validate input.
2620 */
2621 if ( cArgs != 1
2622 || (pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2623 || (pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2624 )
2625 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2626 if (!pVM)
2627 return DBGCCmdHlpPrintf(pCmdHlp, "error: No VM.\n");
2628
2629 /*
2630 * Guest or shadow page tables? Get the paging parameters.
2631 */
2632 bool fGuest = pCmd->pszCmd[3] != 'h';
2633 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2634 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2635 ? pDbgc->fRegCtxGuest
2636 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2637
2638 bool fPAE, fLME, fPSE, fPGE, fNXE;
2639 uint64_t cr3 = fGuest
2640 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2641 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2642 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2643
2644 /*
2645 * Locate the PTE to start displaying at.
2646 *
2647 * The 'dpta' command takes the address of a PTE, while the others are guest
2648 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
2649 * while the others require us to do all the tedious walking thru the paging
2650 * hierarchy to find the intended PTE.
2651 */
2652 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
2653 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
2654 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
2655 unsigned cEntries; /* The number of entries to display. */
2656 unsigned cEntriesMax; /* The max number of entries to display. */
2657 int rc;
2658 if (pCmd->pszCmd[3] == 'a')
2659 {
2660 VarPTEAddr = paArgs[0];
2661 switch (VarPTEAddr.enmRangeType)
2662 {
2663 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
2664 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
2665 default: cEntries = 10; break;
2666 }
2667 cEntriesMax = PAGE_SIZE / cbEntry;
2668 }
2669 else
2670 {
2671 /*
2672 * Determin the range.
2673 */
2674 switch (paArgs[0].enmRangeType)
2675 {
2676 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2677 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2678 default: cEntries = 10; break;
2679 }
2680
2681 /*
2682 * Normalize the input address, it must be a flat GC address.
2683 */
2684 rc = pCmdHlp->pfnEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2685 if (VBOX_FAILURE(rc))
2686 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2687 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2688 {
2689 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2690 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2691 }
2692 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
2693
2694 /*
2695 * Do the paging walk until we get to the page table.
2696 */
2697 DBGCVAR VarCur;
2698 if (fGuest)
2699 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2700 else
2701 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2702 if (fLME)
2703 {
2704 /* Page Map Level 4 Lookup. */
2705 /* Check if it's a valid address first? */
2706 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2707 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2708 X86PML4E Pml4e;
2709 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2710 if (VBOX_FAILURE(rc))
2711 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2712 if (!Pml4e.n.u1Present)
2713 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2714
2715 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2716 Assert(fPAE);
2717 }
2718 if (fPAE)
2719 {
2720 /* Page directory pointer table. */
2721 X86PDPE Pdpe;
2722 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2723 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2724 if (VBOX_FAILURE(rc))
2725 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2726 if (!Pdpe.n.u1Present)
2727 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2728
2729 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2730
2731 /* Page directory (PAE). */
2732 X86PDEPAE Pde;
2733 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
2734 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
2735 if (VBOX_FAILURE(rc))
2736 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
2737 if (!Pde.n.u1Present)
2738 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
2739 if (fPSE && Pde.n.u1Size)
2740 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
2741
2742 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
2743 VarPTEAddr = VarCur;
2744 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
2745 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
2746 }
2747 else
2748 {
2749 /* Page directory (legacy). */
2750 X86PDE Pde;
2751 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
2752 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
2753 if (VBOX_FAILURE(rc))
2754 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
2755 if (!Pde.n.u1Present)
2756 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
2757 if (fPSE && Pde.n.u1Size)
2758 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
2759
2760 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
2761 VarPTEAddr = VarCur;
2762 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
2763 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
2764 }
2765 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2766 iEntry /= cbEntry;
2767 }
2768
2769 /* adjust cEntries */
2770 cEntries = RT_MAX(1, cEntries);
2771 cEntries = RT_MIN(cEntries, cEntriesMax);
2772
2773 /*
2774 * The display loop.
2775 */
2776 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
2777 &VarPTEAddr, &VarGCPtr, iEntry);
2778 do
2779 {
2780 /*
2781 * Read.
2782 */
2783 X86PTEPAE Pte;
2784 Pte.u = 0;
2785 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pte, cbEntry, &VarPTEAddr, NULL);
2786 if (VBOX_FAILURE(rc))
2787 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
2788
2789 /*
2790 * Display.
2791 */
2792 if (iEntry != ~0U)
2793 {
2794 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2795 iEntry++;
2796 }
2797 DBGCCmdHlpPrintf(pCmdHlp,
2798 fPAE
2799 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2800 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2801 Pte.u,
2802 Pte.u & X86_PTE_PAE_PG_MASK,
2803 Pte.n.u1Present ? "p " : "np",
2804 Pte.n.u1Write ? "w" : "r",
2805 Pte.n.u1User ? "u" : "s",
2806 Pte.n.u1Accessed ? "a " : "na",
2807 Pte.n.u1Dirty ? "d " : "nd",
2808 Pte.n.u3Available,
2809 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
2810 Pte.n.u1WriteThru ? "pwt" : " ",
2811 Pte.n.u1CacheDisable ? "pcd" : " ",
2812 Pte.n.u1PAT ? "pat" : " ",
2813 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
2814 );
2815 if (Pte.u & UINT64_C(0x7fff000000000000))
2816 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
2817 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2818 if (VBOX_FAILURE(rc))
2819 return rc;
2820
2821 /*
2822 * Advance.
2823 */
2824 VarPTEAddr.u.u64Number += cbEntry;
2825 if (iEntry != ~0U)
2826 VarGCPtr.u.GCFlat += PAGE_SIZE;
2827 } while (cEntries-- > 0);
2828
2829 NOREF(pResult);
2830 return VINF_SUCCESS;
2831}
2832
2833
2834/**
2835 * The 'dptb' command.
2836 *
2837 * @returns VBox status.
2838 * @param pCmd Pointer to the command descriptor (as registered).
2839 * @param pCmdHlp Pointer to command helper functions.
2840 * @param pVM Pointer to the current VM (if any).
2841 * @param paArgs Pointer to (readonly) array of arguments.
2842 * @param cArgs Number of arguments in the array.
2843 */
2844static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2845{
2846 if (!pVM)
2847 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2848 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
2849 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
2850 if (VBOX_FAILURE(rc1))
2851 return rc1;
2852 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
2853 return rc2;
2854}
2855
2856
2857/**
2858 * The 'dt' command.
2859 *
2860 * @returns VBox status.
2861 * @param pCmd Pointer to the command descriptor (as registered).
2862 * @param pCmdHlp Pointer to command helper functions.
2863 * @param pVM Pointer to the current VM (if any).
2864 * @param paArgs Pointer to (readonly) array of arguments.
2865 * @param cArgs Number of arguments in the array.
2866 */
2867static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM /*pVM*/, PCDBGCVAR /*paArgs*/, unsigned /*cArgs*/, PDBGCVAR /*pResult*/)
2868{
2869 /*
2870 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
2871 */
2872
2873 /** @todo */
2874 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "dt is not implemented yet, feel free to do it. \n");
2875}
2876
2877
2878/**
2879 * The 'm' command.
2880 *
2881 * @returns VBox status.
2882 * @param pCmd Pointer to the command descriptor (as registered).
2883 * @param pCmdHlp Pointer to command helper functions.
2884 * @param pVM Pointer to the current VM (if any).
2885 * @param paArgs Pointer to (readonly) array of arguments.
2886 * @param cArgs Number of arguments in the array.
2887 */
2888static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2889{
2890 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Address: %DV\n", &paArgs[0]);
2891 if (!pVM)
2892 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2893 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2894 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2895 int rc3 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
2896 int rc4 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
2897 if (VBOX_FAILURE(rc1))
2898 return rc1;
2899 if (VBOX_FAILURE(rc2))
2900 return rc2;
2901 if (VBOX_FAILURE(rc3))
2902 return rc3;
2903 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
2904 return rc4;
2905}
2906
2907
2908/**
2909 * Converts one or more variables into a byte buffer for a
2910 * given unit size.
2911 *
2912 * @returns VBox status codes:
2913 * @retval VERR_TOO_MUCH_DATA if the buffer is too small, bitched.
2914 * @retval VERR_INTERNAL_ERROR on bad variable type, bitched.
2915 * @retval VINF_SUCCESS on success.
2916 *
2917 * @param pvBuf The buffer to convert into.
2918 * @param pcbBuf The buffer size on input. The size of the result on output.
2919 * @param cbUnit The unit size to apply when converting.
2920 * The high bit is used to indicate unicode string.
2921 * @param paVars The array of variables to convert.
2922 * @param cVars The number of variables.
2923 */
2924int dbgcVarsToBytes(PDBGCCMDHLP pCmdHlp, void *pvBuf, uint32_t *pcbBuf, size_t cbUnit, PCDBGCVAR paVars, unsigned cVars)
2925{
2926 union
2927 {
2928 uint8_t *pu8;
2929 uint16_t *pu16;
2930 uint32_t *pu32;
2931 uint64_t *pu64;
2932 } u, uEnd;
2933 u.pu8 = (uint8_t *)pvBuf;
2934 uEnd.pu8 = u.pu8 + *pcbBuf;
2935
2936 unsigned i;
2937 for (i = 0; i < cVars && u.pu8 < uEnd.pu8; i++)
2938 {
2939 switch (paVars[i].enmType)
2940 {
2941 case DBGCVAR_TYPE_GC_FAR:
2942 case DBGCVAR_TYPE_HC_FAR:
2943 case DBGCVAR_TYPE_GC_FLAT:
2944 case DBGCVAR_TYPE_GC_PHYS:
2945 case DBGCVAR_TYPE_HC_FLAT:
2946 case DBGCVAR_TYPE_HC_PHYS:
2947 case DBGCVAR_TYPE_NUMBER:
2948 {
2949 uint64_t u64 = paVars[i].u.u64Number;
2950 switch (cbUnit & 0x1f)
2951 {
2952 case 1:
2953 do
2954 {
2955 *u.pu8++ = u64;
2956 u64 >>= 8;
2957 } while (u64);
2958 break;
2959 case 2:
2960 do
2961 {
2962 *u.pu16++ = u64;
2963 u64 >>= 16;
2964 } while (u64);
2965 break;
2966 case 4:
2967 *u.pu32++ = u64;
2968 u64 >>= 32;
2969 if (u64)
2970 *u.pu32++ = u64;
2971 break;
2972 case 8:
2973 *u.pu64++ = u64;
2974 break;
2975 }
2976 break;
2977 }
2978
2979 case DBGCVAR_TYPE_STRING:
2980 case DBGCVAR_TYPE_SYMBOL:
2981 {
2982 const char *psz = paVars[i].u.pszString;
2983 size_t cbString = strlen(psz);
2984 if (cbUnit & RT_BIT_32(31))
2985 {
2986 /* Explode char to unit. */
2987 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8) * (cbUnit & 0x1f))
2988 {
2989 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
2990 return VERR_TOO_MUCH_DATA;
2991 }
2992 while (*psz)
2993 {
2994 switch (cbUnit & 0x1f)
2995 {
2996 case 1: *u.pu8++ = *psz; break;
2997 case 2: *u.pu16++ = *psz; break;
2998 case 4: *u.pu32++ = *psz; break;
2999 case 8: *u.pu64++ = *psz; break;
3000 }
3001 psz++;
3002 }
3003 }
3004 else
3005 {
3006 /* Raw copy with zero padding if the size isn't aligned. */
3007 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8))
3008 {
3009 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3010 return VERR_TOO_MUCH_DATA;
3011 }
3012
3013 size_t cbCopy = cbString & ~(cbUnit - 1);
3014 memcpy(u.pu8, psz, cbCopy);
3015 u.pu8 += cbCopy;
3016 psz += cbCopy;
3017
3018 size_t cbReminder = cbString & (cbUnit - 1);
3019 if (cbReminder)
3020 {
3021 memcpy(u.pu8, psz, cbString & (cbUnit - 1));
3022 memset(u.pu8 + cbReminder, 0, cbUnit - cbReminder);
3023 u.pu8 += cbUnit;
3024 }
3025 }
3026 break;
3027 }
3028
3029 default:
3030 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3031 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INTERNAL_ERROR,
3032 "i=%d enmType=%d\n", i, paVars[i].enmType);
3033 return VERR_INTERNAL_ERROR;
3034 }
3035 }
3036 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3037 if (i != cVars)
3038 {
3039 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3040 return VERR_TOO_MUCH_DATA;
3041 }
3042 return VINF_SUCCESS;
3043}
3044
3045
3046/**
3047 * Executes the search.
3048 *
3049 * @returns VBox status code.
3050 * @param pCmdHlp The command helpers.
3051 * @param pVM The VM handle.
3052 * @param pAddress The address to start searching from. (undefined on output)
3053 * @param cbRange The address range to search. Must not wrap.
3054 * @param pabBytes The byte pattern to search for.
3055 * @param cbBytes The size of the pattern.
3056 * @param cbUnit The search unit.
3057 * @param cMaxHits The max number of hits.
3058 * @param pResult Where to store the result if it's a function invocation.
3059 */
3060static int dbgcCmdWorkerSearchMemDoIt(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGFADDRESS pAddress, RTGCUINTPTR cbRange,
3061 const uint8_t *pabBytes, uint32_t cbBytes,
3062 uint32_t cbUnit, uint64_t cMaxHits, PDBGCVAR pResult)
3063{
3064 /*
3065 * Do the search.
3066 */
3067 uint64_t cHits = 0;
3068 for (;;)
3069 {
3070 /* search */
3071 DBGFADDRESS HitAddress;
3072 int rc = DBGFR3MemScan(pVM, pAddress, cbRange, pabBytes, cbBytes, &HitAddress);
3073 if (RT_FAILURE(rc))
3074 {
3075 if (rc != VERR_DBGF_MEM_NOT_FOUND)
3076 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3MemScan\n");
3077
3078 /* update the current address so we can save it (later). */
3079 pAddress->off += cbRange;
3080 pAddress->FlatPtr += cbRange;
3081 cbRange = 0;
3082 break;
3083 }
3084
3085 /* report result */
3086 DBGCVAR VarCur;
3087 dbgcVarInit(&VarCur);
3088 dbgcVarSetDbgfAddr(&VarCur, &HitAddress);
3089 if (!pResult)
3090 pCmdHlp->pfnExec(pCmdHlp, "db %DV LB 10", &VarCur);
3091 else
3092 dbgcVarSetDbgfAddr(pResult, &HitAddress);
3093
3094 /* advance */
3095 cbRange -= HitAddress.FlatPtr - pAddress->FlatPtr;
3096 *pAddress = HitAddress;
3097 pAddress->FlatPtr += cbBytes;
3098 pAddress->off += cbBytes;
3099 if (cbRange <= cbBytes)
3100 {
3101 cbRange = 0;
3102 break;
3103 }
3104 cbRange -= cbBytes;
3105
3106 if (++cHits >= cMaxHits)
3107 {
3108 /// @todo save the search.
3109 break;
3110 }
3111 }
3112
3113 /*
3114 * Save the search so we can resume it...
3115 */
3116 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3117 if (pDbgc->abSearch != pabBytes)
3118 {
3119 memcpy(pDbgc->abSearch, pabBytes, cbBytes);
3120 pDbgc->cbSearch = cbBytes;
3121 pDbgc->cbSearchUnit = cbUnit;
3122 }
3123 pDbgc->cMaxSearchHits = cMaxHits;
3124 pDbgc->SearchAddr = *pAddress;
3125 pDbgc->cbSearchRange = cbRange;
3126
3127 return cHits ? VINF_SUCCESS : VERR_DBGC_COMMAND_FAILED;
3128}
3129
3130
3131/**
3132 * Resumes the previous search.
3133 *
3134 * @returns VBox status code.
3135 * @param pCmdHlp Pointer to the command helper functions.
3136 * @param pVM Pointer to the current VM (if any).
3137 * @param pResult Where to store the result of a function invocation.
3138 */
3139static int dbgcCmdWorkerSearchMemResume(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGCVAR pResult)
3140{
3141 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3142
3143 /*
3144 * Make sure there is a previous command.
3145 */
3146 if (!pDbgc->cbSearch)
3147 {
3148 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Error: No previous search\n");
3149 return VERR_DBGC_COMMAND_FAILED;
3150 }
3151
3152 /*
3153 * Make range and address adjustments.
3154 */
3155 DBGFADDRESS Address = pDbgc->SearchAddr;
3156 if (Address.FlatPtr == ~(RTGCUINTPTR)0)
3157 {
3158 Address.FlatPtr -= Address.off;
3159 Address.off = 0;
3160 }
3161
3162 RTGCUINTPTR cbRange = pDbgc->cbSearchRange;
3163 if (!cbRange)
3164 cbRange = ~(RTGCUINTPTR)0;
3165 if (Address.FlatPtr + cbRange < pDbgc->SearchAddr.FlatPtr)
3166 cbRange = ~(RTGCUINTPTR)0 - pDbgc->SearchAddr.FlatPtr + !!pDbgc->SearchAddr.FlatPtr;
3167
3168 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, pDbgc->abSearch, pDbgc->cbSearch,
3169 pDbgc->cbSearchUnit, pDbgc->cMaxSearchHits, pResult);
3170}
3171
3172
3173/**
3174 * Search memory, worker for the 's' and 's?' functions.
3175 *
3176 * @returns VBox status.
3177 * @param pCmdHlp Pointer to the command helper functions.
3178 * @param pVM Pointer to the current VM (if any).
3179 * @param pAddress Where to start searching. If no range, search till end of address space.
3180 * @param cMaxHits The maximum number of hits.
3181 * @param chType The search type.
3182 * @param paPatArgs The pattern variable array.
3183 * @param cPatArgs Number of pattern variables.
3184 * @param pResult Where to store the result of a function invocation.
3185 */
3186static int dbgcCmdWorkerSearchMem(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pAddress, uint64_t cMaxHits, char chType,
3187 PCDBGCVAR paPatArgs, unsigned cPatArgs, PDBGCVAR pResult)
3188{
3189 dbgcVarSetGCFlat(pResult, 0);
3190
3191 /*
3192 * Convert the search pattern into bytes and DBGFR3MemScan can deal with.
3193 */
3194 uint32_t cbUnit;
3195 switch (chType)
3196 {
3197 case 'a':
3198 case 'b': cbUnit = 1; break;
3199 case 'u': cbUnit = 2 | RT_BIT_32(31); break;
3200 case 'w': cbUnit = 2; break;
3201 case 'd': cbUnit = 4; break;
3202 case 'q': cbUnit = 8; break;
3203 default:
3204 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "chType=%c\n", chType);
3205 }
3206 uint8_t abBytes[RT_SIZEOFMEMB(DBGC, abSearch)];
3207 uint32_t cbBytes = sizeof(abBytes);
3208 int rc = dbgcVarsToBytes(pCmdHlp, abBytes, &cbBytes, cbUnit, paPatArgs, cPatArgs);
3209 if (RT_FAILURE(rc))
3210 return VERR_DBGC_COMMAND_FAILED;
3211
3212 /*
3213 * Make DBGF address and fix the range.
3214 */
3215 DBGFADDRESS Address;
3216 rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, pAddress, &Address);
3217 if (RT_FAILURE(rc))
3218 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", pAddress);
3219
3220 RTGCUINTPTR cbRange;
3221 switch (pAddress->enmRangeType)
3222 {
3223 case DBGCVAR_RANGE_BYTES:
3224 cbRange = pAddress->u64Range;
3225 if (cbRange != pAddress->u64Range)
3226 cbRange = ~(RTGCUINTPTR)0;
3227 break;
3228
3229 case DBGCVAR_RANGE_ELEMENTS:
3230 cbRange = (RTGCUINTPTR)(pAddress->u64Range * cbUnit);
3231 if ( cbRange != pAddress->u64Range * cbUnit
3232 || cbRange < pAddress->u64Range)
3233 cbRange = ~(RTGCUINTPTR)0;
3234 break;
3235
3236 default:
3237 cbRange = ~(RTGCUINTPTR)0;
3238 break;
3239 }
3240 if (Address.FlatPtr + cbRange < Address.FlatPtr)
3241 cbRange = ~(RTGCUINTPTR)0 - Address.FlatPtr + !!Address.FlatPtr;
3242
3243 /*
3244 * Ok, do it.
3245 */
3246 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, abBytes, cbBytes, cbUnit, cMaxHits, pResult);
3247}
3248
3249
3250/**
3251 * The 's' command.
3252 *
3253 * @returns VBox status.
3254 * @param pCmd Pointer to the command descriptor (as registered).
3255 * @param pCmdHlp Pointer to command helper functions.
3256 * @param pVM Pointer to the current VM (if any).
3257 * @param paArgs Pointer to (readonly) array of arguments.
3258 * @param cArgs Number of arguments in the array.
3259 */
3260static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3261{
3262 /* check that the parser did what it's supposed to do. */
3263 //if ( cArgs <= 2
3264 // && paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3265 // return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
3266
3267 /*
3268 * Repeate previous search?
3269 */
3270 if (cArgs == 0)
3271 return dbgcCmdWorkerSearchMemResume(pCmdHlp, pVM, pResult);
3272
3273 /*
3274 * Parse arguments.
3275 */
3276
3277 return -1;
3278}
3279
3280
3281/**
3282 * The 's?' command.
3283 *
3284 * @returns VBox status.
3285 * @param pCmd Pointer to the command descriptor (as registered).
3286 * @param pCmdHlp Pointer to command helper functions.
3287 * @param pVM Pointer to the current VM (if any).
3288 * @param paArgs Pointer to (readonly) array of arguments.
3289 * @param cArgs Number of arguments in the array.
3290 */
3291static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3292{
3293 /* check that the parser did what it's supposed to do. */
3294 if ( cArgs < 2
3295 || !DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
3296 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
3297 return dbgcCmdWorkerSearchMem(pCmdHlp, pVM, &paArgs[0], pResult ? 1 : 25, pCmd->pszCmd[1], paArgs + 1, cArgs - 1, pResult);
3298}
3299
3300
3301/**
3302 * List near symbol.
3303 *
3304 * @returns VBox status code.
3305 * @param pCmdHlp Pointer to command helper functions.
3306 * @param pVM Pointer to the current VM (if any).
3307 * @param pArg Pointer to the address or symbol to lookup.
3308 */
3309static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pArg, PDBGCVAR pResult)
3310{
3311 dbgcVarSetGCFlat(pResult, 0);
3312
3313 DBGFSYMBOL Symbol;
3314 int rc;
3315 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
3316 {
3317 /*
3318 * Lookup the symbol address.
3319 */
3320 rc = DBGFR3SymbolByName(pVM, pArg->u.pszString, &Symbol);
3321 if (VBOX_FAILURE(rc))
3322 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3SymbolByName(, %s,)\n", pArg->u.pszString);
3323
3324 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%VGv %s\n", (RTGCUINTPTR)Symbol.Value, Symbol.szName); /** @todo remove the RTUINGCPTR cast once DBGF got correct interfaces! */
3325 dbgcVarSetGCFlatByteRange(pResult, Symbol.Value, Symbol.cb);
3326 }
3327 else
3328 {
3329 /*
3330 * Convert it to a flat GC address and lookup that address.
3331 */
3332 DBGCVAR AddrVar;
3333 rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
3334 if (VBOX_FAILURE(rc))
3335 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
3336
3337 dbgcVarSetVar(pResult, &AddrVar);
3338
3339 RTGCINTPTR offDisp = 0;
3340 rc = DBGFR3SymbolByAddr(pVM, AddrVar.u.GCFlat, &offDisp, &Symbol);
3341 if (VBOX_FAILURE(rc))
3342 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3SymbolByAddr(, %VGv,,)\n", AddrVar.u.GCFlat);
3343
3344 if (!offDisp)
3345 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s", &AddrVar, Symbol.szName);
3346 else if (offDisp > 0)
3347 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
3348 else
3349 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
3350 if ((RTGCINTPTR)Symbol.cb > -offDisp)
3351 {
3352 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " LB %RGv\n", Symbol.cb + offDisp);
3353 dbgcVarSetByteRange(pResult, Symbol.cb + offDisp);
3354 }
3355 else
3356 {
3357 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
3358 dbgcVarSetNoRange(pResult);
3359 }
3360 }
3361
3362 return rc;
3363}
3364
3365
3366/**
3367 * The 'ln' (listnear) command.
3368 *
3369 * @returns VBox status.
3370 * @param pCmd Pointer to the command descriptor (as registered).
3371 * @param pCmdHlp Pointer to command helper functions.
3372 * @param pVM Pointer to the current VM (if any).
3373 * @param paArgs Pointer to (readonly) array of arguments.
3374 * @param cArgs Number of arguments in the array.
3375 */
3376static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3377{
3378 dbgcVarSetGCFlat(pResult, 0);
3379 if (!cArgs)
3380 {
3381 /*
3382 * Current cs:eip symbol.
3383 */
3384 DBGCVAR AddrVar;
3385 int rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(cs:eip)");
3386 if (VBOX_FAILURE(rc))
3387 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(cs:eip)\n");
3388 return dbgcDoListNear(pCmdHlp, pVM, &AddrVar, pResult);
3389 }
3390
3391 /*
3392 * Iterate arguments.
3393 */
3394 for (unsigned iArg = 0; iArg < cArgs; iArg++)
3395 {
3396 int rc = dbgcDoListNear(pCmdHlp, pVM, &paArgs[iArg], pResult);
3397 if (VBOX_FAILURE(rc))
3398 return rc;
3399 }
3400
3401 NOREF(pCmd); NOREF(pResult);
3402 return VINF_SUCCESS;
3403}
3404
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette