VirtualBox

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

Last change on this file since 9844 was 9844, checked in by vboxsync, 17 years ago

Correction

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 144.7 KB
Line 
1/** $Id: DBGCEmulateCodeView.cpp 9844 2008-06-20 09:50:13Z 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 PCPUMCTX pCtx;
912 int rc = CPUMQueryGuestCtxPtr(pVM, &pCtx);
913 AssertRC(rc);
914
915 if ( pDbgc->fRegCtxGuest
916 && CPUMIsGuestIn64BitCodeEx(pCtx))
917 {
918 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
919 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVM);
920 }
921 else
922 {
923 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
924 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVM) : CPUMGetHyperEIP(pVM);
925 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVM) : CPUMGetHyperCS(pVM);
926 }
927
928 if (pDbgc->fRegCtxGuest)
929 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
930 else
931 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER;
932 }
933 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
934 }
935 else
936 pDbgc->DisasmPos = paArgs[0];
937
938 /*
939 * Range.
940 */
941 switch (pDbgc->DisasmPos.enmRangeType)
942 {
943 case DBGCVAR_RANGE_NONE:
944 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
945 pDbgc->DisasmPos.u64Range = 10;
946 break;
947
948 case DBGCVAR_RANGE_ELEMENTS:
949 if (pDbgc->DisasmPos.u64Range > 2048)
950 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
951 break;
952
953 case DBGCVAR_RANGE_BYTES:
954 if (pDbgc->DisasmPos.u64Range > 65536)
955 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
956 break;
957
958 default:
959 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DisasmPos.enmRangeType);
960 }
961
962 /*
963 * Convert physical and host addresses to guest addresses.
964 */
965 int rc;
966 switch (pDbgc->DisasmPos.enmType)
967 {
968 case DBGCVAR_TYPE_GC_FLAT:
969 case DBGCVAR_TYPE_GC_FAR:
970 break;
971 case DBGCVAR_TYPE_GC_PHYS:
972 case DBGCVAR_TYPE_HC_FLAT:
973 case DBGCVAR_TYPE_HC_PHYS:
974 case DBGCVAR_TYPE_HC_FAR:
975 {
976 DBGCVAR VarTmp;
977 rc = pCmdHlp->pfnEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
978 if (VBOX_FAILURE(rc))
979 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: failed to evaluate '%%(%Dv)' -> %Vrc .\n", &pDbgc->DisasmPos, rc);
980 pDbgc->DisasmPos = VarTmp;
981 break;
982 }
983 default: AssertFailed(); break;
984 }
985
986 /*
987 * Print address.
988 * todo: Change to list near.
989 */
990#if 0
991 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DisasmPos);
992 if (VBOX_FAILURE(rc))
993 return rc;
994#endif
995
996 /*
997 * Do the disassembling.
998 */
999 unsigned cTries = 32;
1000 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1001 if (iRangeLeft == 0) /* klugde for 'r'. */
1002 iRangeLeft = -1;
1003 for (;;)
1004 {
1005 /*
1006 * Disassemble the instruction.
1007 */
1008 char szDis[256];
1009 uint32_t cbInstr = 1;
1010 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1011 rc = DBGFR3DisasInstrEx(pVM, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags, &szDis[0], sizeof(szDis), &cbInstr);
1012 else
1013 rc = DBGFR3DisasInstrEx(pVM, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags, &szDis[0], sizeof(szDis), &cbInstr);
1014 if (VBOX_SUCCESS(rc))
1015 {
1016 /* print it */
1017 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1018 if (VBOX_FAILURE(rc))
1019 return rc;
1020 }
1021 else
1022 {
1023 /* bitch. */
1024 int rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to disassemble instruction, skipping one byte.\n");
1025 if (VBOX_FAILURE(rc))
1026 return rc;
1027 if (cTries-- > 0)
1028 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Too many disassembly failures. Giving up.\n");
1029 cbInstr = 1;
1030 }
1031
1032 /* advance */
1033 if (iRangeLeft < 0) /* 'r' */
1034 break;
1035 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1036 iRangeLeft--;
1037 else
1038 iRangeLeft -= cbInstr;
1039 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1040 if (VBOX_FAILURE(rc))
1041 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DisasmPos, cbInstr);
1042 if (iRangeLeft <= 0)
1043 break;
1044 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1045 }
1046
1047 NOREF(pCmd); NOREF(pResult);
1048 return 0;
1049}
1050
1051
1052/**
1053 * The 'ls' command.
1054 *
1055 * @returns VBox status.
1056 * @param pCmd Pointer to the command descriptor (as registered).
1057 * @param pCmdHlp Pointer to command helper functions.
1058 * @param pVM Pointer to the current VM (if any).
1059 * @param paArgs Pointer to (readonly) array of arguments.
1060 * @param cArgs Number of arguments in the array.
1061 */
1062static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1063{
1064 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1065
1066 /*
1067 * Validate input.
1068 */
1069 if ( cArgs > 1
1070 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
1071 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
1072 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1073 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
1074 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1075 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
1076
1077 /*
1078 * Find address.
1079 */
1080 if (!cArgs)
1081 {
1082 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1083 {
1084 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
1085 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVM) : CPUMGetHyperEIP(pVM);
1086 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVM) : CPUMGetHyperCS(pVM);
1087 }
1088 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
1089 }
1090 else
1091 pDbgc->SourcePos = paArgs[0];
1092
1093 /*
1094 * Ensure the the source address is flat GC.
1095 */
1096 switch (pDbgc->SourcePos.enmType)
1097 {
1098 case DBGCVAR_TYPE_GC_FLAT:
1099 break;
1100 case DBGCVAR_TYPE_GC_PHYS:
1101 case DBGCVAR_TYPE_GC_FAR:
1102 case DBGCVAR_TYPE_HC_FLAT:
1103 case DBGCVAR_TYPE_HC_PHYS:
1104 case DBGCVAR_TYPE_HC_FAR:
1105 {
1106 int rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
1107 if (VBOX_FAILURE(rc))
1108 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid address or address type. (rc=%d)\n", rc);
1109 break;
1110 }
1111 default: AssertFailed(); break;
1112 }
1113
1114 /*
1115 * Range.
1116 */
1117 switch (pDbgc->SourcePos.enmRangeType)
1118 {
1119 case DBGCVAR_RANGE_NONE:
1120 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1121 pDbgc->SourcePos.u64Range = 10;
1122 break;
1123
1124 case DBGCVAR_RANGE_ELEMENTS:
1125 if (pDbgc->SourcePos.u64Range > 2048)
1126 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
1127 break;
1128
1129 case DBGCVAR_RANGE_BYTES:
1130 if (pDbgc->SourcePos.u64Range > 65536)
1131 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
1132 break;
1133
1134 default:
1135 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
1136 }
1137
1138 /*
1139 * Do the disassembling.
1140 */
1141 bool fFirst = 1;
1142 DBGFLINE LinePrev = { 0, 0, "" };
1143 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
1144 if (iRangeLeft == 0) /* klugde for 'r'. */
1145 iRangeLeft = -1;
1146 for (;;)
1147 {
1148 /*
1149 * Get line info.
1150 */
1151 DBGFLINE Line;
1152 RTGCINTPTR off;
1153 int rc = DBGFR3LineByAddr(pVM, pDbgc->SourcePos.u.GCFlat, &off, &Line);
1154 if (VBOX_FAILURE(rc))
1155 return VINF_SUCCESS;
1156
1157 unsigned cLines = 0;
1158 if (memcmp(&Line, &LinePrev, sizeof(Line)))
1159 {
1160 /*
1161 * Print filenamename
1162 */
1163 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
1164 fFirst = true;
1165 if (fFirst)
1166 {
1167 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
1168 if (VBOX_FAILURE(rc))
1169 return rc;
1170 }
1171
1172 /*
1173 * Try open the file and read the line.
1174 */
1175 FILE *phFile = fopen(Line.szFilename, "r");
1176 if (phFile)
1177 {
1178 /* Skip ahead to the desired line. */
1179 char szLine[4096];
1180 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
1181 if (cBefore > 7)
1182 cBefore = 0;
1183 unsigned cLeft = Line.uLineNo - cBefore;
1184 while (cLeft > 0)
1185 {
1186 szLine[0] = '\0';
1187 if (!fgets(szLine, sizeof(szLine), phFile))
1188 break;
1189 cLeft--;
1190 }
1191 if (!cLeft)
1192 {
1193 /* print the before lines */
1194 for (;;)
1195 {
1196 size_t cch = strlen(szLine);
1197 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || isspace(szLine[cch - 1])) )
1198 szLine[--cch] = '\0';
1199 if (cBefore-- <= 0)
1200 break;
1201
1202 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
1203 szLine[0] = '\0';
1204 fgets(szLine, sizeof(szLine), phFile);
1205 cLines++;
1206 }
1207 /* print the actual line */
1208 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
1209 }
1210 fclose(phFile);
1211 if (VBOX_FAILURE(rc))
1212 return rc;
1213 fFirst = false;
1214 }
1215 else
1216 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Warning: couldn't open source file '%s'\n", Line.szFilename);
1217
1218 LinePrev = Line;
1219 }
1220
1221
1222 /*
1223 * Advance
1224 */
1225 if (iRangeLeft < 0) /* 'r' */
1226 break;
1227 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1228 iRangeLeft -= cLines;
1229 else
1230 iRangeLeft -= 1;
1231 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
1232 if (VBOX_FAILURE(rc))
1233 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
1234 if (iRangeLeft <= 0)
1235 break;
1236 }
1237
1238 NOREF(pCmd); NOREF(pResult);
1239 return 0;
1240}
1241
1242
1243/**
1244 * The 'r' command.
1245 *
1246 * @returns VBox status.
1247 * @param pCmd Pointer to the command descriptor (as registered).
1248 * @param pCmdHlp Pointer to command helper functions.
1249 * @param pVM Pointer to the current VM (if any).
1250 * @param paArgs Pointer to (readonly) array of arguments.
1251 * @param cArgs Number of arguments in the array.
1252 */
1253static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1254{
1255 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1256
1257 if (pDbgc->fRegCtxGuest)
1258 return dbgcCmdRegGuest(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult);
1259 else
1260 return dbgcCmdRegHyper(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult);
1261}
1262
1263
1264/**
1265 * Common worker for the dbgcCmdReg*() commands.
1266 *
1267 * @returns VBox status.
1268 * @param pCmd Pointer to the command descriptor (as registered).
1269 * @param pCmdHlp Pointer to command helper functions.
1270 * @param pVM Pointer to the current VM (if any).
1271 * @param paArgs Pointer to (readonly) array of arguments.
1272 * @param cArgs Number of arguments in the array.
1273 * @param pszPrefix The symbol prefix.
1274 */
1275static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult, const char *pszPrefix)
1276{
1277 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1278
1279 /*
1280 * cArgs == 0: Show all
1281 */
1282 if (cArgs == 0)
1283 {
1284 /*
1285 * Get register context.
1286 */
1287 int rc;
1288 PCPUMCTX pCtx;
1289 PCCPUMCTXCORE pCtxCore;
1290 if (!*pszPrefix)
1291 {
1292 rc = CPUMQueryGuestCtxPtr(pVM, &pCtx);
1293 pCtxCore = CPUMGetGuestCtxCore(pVM);
1294 }
1295 else
1296 {
1297 rc = CPUMQueryHyperCtxPtr(pVM, &pCtx);
1298 pCtxCore = CPUMGetHyperCtxCore(pVM);
1299 }
1300 if (VBOX_FAILURE(rc))
1301 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Getting register context\n");
1302
1303 /*
1304 * Format the flags.
1305 */
1306 static struct
1307 {
1308 const char *pszSet; const char *pszClear; uint32_t fFlag;
1309 } aFlags[] =
1310 {
1311 { "vip",NULL, X86_EFL_VIP },
1312 { "vif",NULL, X86_EFL_VIF },
1313 { "ac", NULL, X86_EFL_AC },
1314 { "vm", NULL, X86_EFL_VM },
1315 { "rf", NULL, X86_EFL_RF },
1316 { "nt", NULL, X86_EFL_NT },
1317 { "ov", "nv", X86_EFL_OF },
1318 { "dn", "up", X86_EFL_DF },
1319 { "ei", "di", X86_EFL_IF },
1320 { "tf", NULL, X86_EFL_TF },
1321 { "ng", "pl", X86_EFL_SF },
1322 { "zr", "nz", X86_EFL_ZF },
1323 { "ac", "na", X86_EFL_AF },
1324 { "po", "pe", X86_EFL_PF },
1325 { "cy", "nc", X86_EFL_CF },
1326 };
1327 char szEFlags[80];
1328 char *psz = szEFlags;
1329 uint32_t efl = pCtxCore->eflags.u32;
1330 for (unsigned i = 0; i < ELEMENTS(aFlags); i++)
1331 {
1332 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
1333 if (pszAdd)
1334 {
1335 strcpy(psz, pszAdd);
1336 psz += strlen(pszAdd);
1337 *psz++ = ' ';
1338 }
1339 }
1340 psz[-1] = '\0';
1341
1342
1343 /*
1344 * Format the registers.
1345 */
1346 if (pDbgc->fRegTerse)
1347 {
1348 if (CPUMIsGuestIn64BitCodeEx(pCtx))
1349 {
1350 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1351 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
1352 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
1353 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
1354 "%sr14=%016RX64 %sr15=%016RX64\n"
1355 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
1356 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %sss=%04x %seflags=%08x\n",
1357 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
1358 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
1359 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
1360 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 34 : 31, szEFlags,
1361 pszPrefix, (RTSEL)pCtxCore->cs, pszPrefix, (RTSEL)pCtxCore->ds, pszPrefix, (RTSEL)pCtxCore->es,
1362 pszPrefix, (RTSEL)pCtxCore->fs, pszPrefix, (RTSEL)pCtxCore->gs, pszPrefix, (RTSEL)pCtxCore->ss, pszPrefix, efl);
1363 }
1364 else
1365 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1366 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
1367 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
1368 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %sss=%04x %seflags=%08x\n",
1369 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
1370 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 34 : 31, szEFlags,
1371 pszPrefix, (RTSEL)pCtxCore->cs, pszPrefix, (RTSEL)pCtxCore->ds, pszPrefix, (RTSEL)pCtxCore->es,
1372 pszPrefix, (RTSEL)pCtxCore->fs, pszPrefix, (RTSEL)pCtxCore->gs, pszPrefix, (RTSEL)pCtxCore->ss, pszPrefix, efl);
1373 }
1374 else
1375 {
1376 if (CPUMIsGuestIn64BitCodeEx(pCtx))
1377 {
1378 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1379 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
1380 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
1381 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
1382 "%sr14=%016RX64 %sr15=%016RX64\n"
1383 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
1384 "%scs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1385 "%sds={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1386 "%ses={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1387 "%sfs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1388 "%sgs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1389 "%sss={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1390 "%scr0=%016RX64 %scr2=%016RX64 %scr3=%016RX64 %scr4=%016RX64\n"
1391 "%sdr0=%016RX64 %sdr1=%016RX64 %sdr2=%016RX64 %sdr3=%016RX64\n"
1392 "%sdr4=%016RX64 %sdr5=%016RX64 %sdr6=%016RX64 %sdr7=%016RX64\n"
1393 "%sgdtr=%016RX64:%04x %sidtr=%016RX64:%04x %seflags=%08x\n"
1394 "%sldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1395 "%str ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1396 "%sSysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1397 ,
1398 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
1399 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
1400 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
1401 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
1402 pszPrefix, (RTSEL)pCtxCore->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u,
1403 pszPrefix, (RTSEL)pCtxCore->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u,
1404 pszPrefix, (RTSEL)pCtxCore->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u,
1405 pszPrefix, (RTSEL)pCtxCore->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u,
1406 pszPrefix, (RTSEL)pCtxCore->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u,
1407 pszPrefix, (RTSEL)pCtxCore->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u,
1408 pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
1409 pszPrefix, pCtx->dr0, pszPrefix, pCtx->dr1, pszPrefix, pCtx->dr2, pszPrefix, pCtx->dr3,
1410 pszPrefix, pCtx->dr4, pszPrefix, pCtx->dr5, pszPrefix, pCtx->dr6, pszPrefix, pCtx->dr7,
1411 pszPrefix, pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, efl,
1412 pszPrefix, (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u64Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
1413 pszPrefix, (RTSEL)pCtx->tr, pCtx->trHid.u64Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
1414 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp);
1415
1416 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1417 "MSR:\n"
1418 "%sEFER =%016RX64\n"
1419 "%sPAT =%016RX64\n"
1420 "%sSTAR =%016RX64\n"
1421 "%sCSTAR =%016RX64\n"
1422 "%sLSTAR =%016RX64\n"
1423 "%sSFMASK =%016RX64\n"
1424 "%sKERNELGSBASE =%016RX64\n",
1425 pszPrefix, pCtx->msrEFER,
1426 pszPrefix, pCtx->msrPAT,
1427 pszPrefix, pCtx->msrSTAR,
1428 pszPrefix, pCtx->msrCSTAR,
1429 pszPrefix, pCtx->msrLSTAR,
1430 pszPrefix, pCtx->msrSFMASK,
1431 pszPrefix, pCtx->msrKERNELGSBASE);
1432 }
1433 else
1434 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1435 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
1436 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
1437 "%scs={%04x base=%016RX64 limit=%08x flags=%08x} %sdr0=%016RX64 %sdr1=%016RX64\n"
1438 "%sds={%04x base=%016RX64 limit=%08x flags=%08x} %sdr2=%016RX64 %sdr3=%016RX64\n"
1439 "%ses={%04x base=%016RX64 limit=%08x flags=%08x} %sdr4=%016RX64 %sdr5=%016RX64\n"
1440 "%sfs={%04x base=%016RX64 limit=%08x flags=%08x} %sdr6=%016RX64 %sdr7=%016RX64\n"
1441 "%sgs={%04x base=%016RX64 limit=%08x flags=%08x} %scr0=%016RX64 %scr2=%016RX64\n"
1442 "%sss={%04x base=%016RX64 limit=%08x flags=%08x} %scr3=%016RX64 %scr4=%016RX64\n"
1443 "%sgdtr=%016RX64:%04x %sidtr=%016RX64:%04x %seflags=%08x\n"
1444 "%sldtr={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1445 "%str ={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1446 "%sSysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1447 "%sFCW=%04x %sFSW=%04x %sFTW=%04x\n"
1448 ,
1449 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
1450 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
1451 pszPrefix, (RTSEL)pCtxCore->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u, pszPrefix, pCtx->dr0, pszPrefix, pCtx->dr1,
1452 pszPrefix, (RTSEL)pCtxCore->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u, pszPrefix, pCtx->dr2, pszPrefix, pCtx->dr3,
1453 pszPrefix, (RTSEL)pCtxCore->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u, pszPrefix, pCtx->dr4, pszPrefix, pCtx->dr5,
1454 pszPrefix, (RTSEL)pCtxCore->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u, pszPrefix, pCtx->dr6, pszPrefix, pCtx->dr7,
1455 pszPrefix, (RTSEL)pCtxCore->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u, pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2,
1456 pszPrefix, (RTSEL)pCtxCore->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
1457 pszPrefix, pCtx->gdtr.pGdt,pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, pCtxCore->eflags,
1458 pszPrefix, (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u64Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
1459 pszPrefix, (RTSEL)pCtx->tr, pCtx->trHid.u64Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
1460 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp,
1461 pszPrefix, pCtx->fpu.FCW, pszPrefix, pCtx->fpu.FSW, pszPrefix, pCtx->fpu.FTW);
1462 }
1463 if (CPUMIsGuestIn64BitCodeEx(pCtx))
1464 {
1465 /*
1466 * Disassemble one instruction at cs:rip.
1467 */
1468 return pCmdHlp->pfnExec(pCmdHlp, "u %016x L 0", pCtx->rip);
1469 }
1470 else
1471 {
1472 /*
1473 * Disassemble one instruction at cs:eip.
1474 */
1475 return pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pCtx->cs, pCtx->eip);
1476 }
1477 }
1478
1479 /*
1480 * cArgs == 1: Show the register.
1481 * cArgs == 2: Modify the register.
1482 */
1483 if ( cArgs == 1
1484 || cArgs == 2)
1485 {
1486 /* locate the register symbol. */
1487 const char *pszReg = paArgs[0].u.pszString;
1488 if ( *pszPrefix
1489 && pszReg[0] != *pszPrefix)
1490 {
1491 /* prepend the prefix. */
1492 char *psz = (char *)alloca(strlen(pszReg) + 2);
1493 psz[0] = *pszPrefix;
1494 strcpy(psz + 1, paArgs[0].u.pszString);
1495 pszReg = psz;
1496 }
1497 PCDBGCSYM pSym = dbgcLookupRegisterSymbol(pDbgc, pszReg);
1498 if (!pSym)
1499 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER /* VERR_DBGC_INVALID_REGISTER */, "Invalid register name '%s'.\n", pszReg);
1500
1501 /* show the register */
1502 if (cArgs == 1)
1503 {
1504 DBGCVAR Var;
1505 memset(&Var, 0, sizeof(Var));
1506 int rc = pSym->pfnGet(pSym, pCmdHlp, DBGCVAR_TYPE_NUMBER, &Var);
1507 if (VBOX_FAILURE(rc))
1508 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Failed getting value for register '%s'.\n", pszReg);
1509 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s=%Dv\n", pszReg, &Var);
1510 }
1511
1512 /* change the register */
1513 int rc = pSym->pfnSet(pSym, pCmdHlp, &paArgs[1]);
1514 if (VBOX_FAILURE(rc))
1515 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Failed setting value for register '%s'.\n", pszReg);
1516 return VINF_SUCCESS;
1517 }
1518
1519
1520 NOREF(pCmd); NOREF(paArgs); NOREF(pResult);
1521 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Huh? cArgs=%d Expected 0, 1 or 2!\n", cArgs);
1522}
1523
1524
1525/**
1526 * The 'rg' command.
1527 *
1528 * @returns VBox status.
1529 * @param pCmd Pointer to the command descriptor (as registered).
1530 * @param pCmdHlp Pointer to command helper functions.
1531 * @param pVM Pointer to the current VM (if any).
1532 * @param paArgs Pointer to (readonly) array of arguments.
1533 * @param cArgs Number of arguments in the array.
1534 */
1535static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1536{
1537 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult, "");
1538}
1539
1540
1541/**
1542 * The 'rh' command.
1543 *
1544 * @returns VBox status.
1545 * @param pCmd Pointer to the command descriptor (as registered).
1546 * @param pCmdHlp Pointer to command helper functions.
1547 * @param pVM Pointer to the current VM (if any).
1548 * @param paArgs Pointer to (readonly) array of arguments.
1549 * @param cArgs Number of arguments in the array.
1550 */
1551static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1552{
1553 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult, ".");
1554}
1555
1556
1557/**
1558 * The 'rt' command.
1559 *
1560 * @returns VBox status.
1561 * @param pCmd Pointer to the command descriptor (as registered).
1562 * @param pCmdHlp Pointer to command helper functions.
1563 * @param pVM Pointer to the current VM (if any).
1564 * @param paArgs Pointer to (readonly) array of arguments.
1565 * @param cArgs Number of arguments in the array.
1566 */
1567static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1568{
1569 NOREF(pCmd); NOREF(pVM); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1570
1571 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1572 pDbgc->fRegTerse = !pDbgc->fRegTerse;
1573 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
1574}
1575
1576
1577/**
1578 * The 't' command.
1579 *
1580 * @returns VBox status.
1581 * @param pCmd Pointer to the command descriptor (as registered).
1582 * @param pCmdHlp Pointer to command helper functions.
1583 * @param pVM Pointer to the current VM (if any).
1584 * @param paArgs Pointer to (readonly) array of arguments.
1585 * @param cArgs Number of arguments in the array.
1586 */
1587static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1588{
1589 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1590
1591 int rc = DBGFR3Step(pVM);
1592 if (VBOX_SUCCESS(rc))
1593 pDbgc->fReady = false;
1594 else
1595 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
1596
1597 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1598 return rc;
1599}
1600
1601
1602/**
1603 * The 'k', 'kg' and 'kh' commands.
1604 *
1605 * @returns VBox status.
1606 * @param pCmd Pointer to the command descriptor (as registered).
1607 * @param pCmdHlp Pointer to command helper functions.
1608 * @param pVM Pointer to the current VM (if any).
1609 * @param paArgs Pointer to (readonly) array of arguments.
1610 * @param cArgs Number of arguments in the array.
1611 */
1612static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1613{
1614 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1615
1616 /*
1617 * Figure which context we're called for.
1618 */
1619 bool fGuest = pCmd->pszCmd[1] == 'g'
1620 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
1621
1622
1623 DBGFSTACKFRAME Frame;
1624 memset(&Frame, 0, sizeof(Frame));
1625 int rc;
1626 if (fGuest)
1627 rc = DBGFR3StackWalkBeginGuest(pVM, &Frame);
1628 else
1629 rc = DBGFR3StackWalkBeginHyper(pVM, &Frame);
1630 if (VBOX_FAILURE(rc))
1631 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to begin stack walk, rc=%Vrc\n", rc);
1632
1633 /*
1634 * Print header.
1635 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
1636 */
1637 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1638 if (VBOX_FAILURE(rc))
1639 return rc;
1640 do
1641 {
1642 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1643 (uint32_t)Frame.AddrFrame.off,
1644 (uint32_t)Frame.AddrReturnFrame.off,
1645 (uint32_t)Frame.AddrReturnPC.Sel,
1646 (uint32_t)Frame.AddrReturnPC.off,
1647 Frame.Args.au32[0],
1648 Frame.Args.au32[1],
1649 Frame.Args.au32[2],
1650 Frame.Args.au32[3]);
1651 if (VBOX_FAILURE(rc))
1652 return rc;
1653 if (!Frame.pSymPC)
1654 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %RTsel:%08RGv", Frame.AddrPC.Sel, Frame.AddrPC.off);
1655 else
1656 {
1657 RTGCINTPTR offDisp = Frame.AddrPC.FlatPtr - Frame.pSymPC->Value; /** @todo this isn't 100% correct for segemnted stuff. */
1658 if (offDisp > 0)
1659 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s+%llx", Frame.pSymPC->szName, (int64_t)offDisp);
1660 else if (offDisp < 0)
1661 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s-%llx", Frame.pSymPC->szName, -(int64_t)offDisp);
1662 else
1663 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s", Frame.pSymPC->szName);
1664 }
1665 if (VBOX_SUCCESS(rc) && Frame.pLinePC)
1666 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " [%s @ 0i%d]", Frame.pLinePC->szFilename, Frame.pLinePC->uLineNo);
1667 if (VBOX_SUCCESS(rc))
1668 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
1669 if (VBOX_FAILURE(rc))
1670 return rc;
1671
1672 /* next */
1673 rc = DBGFR3StackWalkNext(pVM, &Frame);
1674 } while (VBOX_SUCCESS(rc));
1675
1676 NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1677 return VINF_SUCCESS;
1678}
1679
1680
1681static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP /*pCmdHlp*/, PCX86DESC64 /*pDesc*/, unsigned /*iEntry*/, bool /* fHyper */, bool * /*fDblEntry*/)
1682{
1683 /* GUEST64 */
1684 return VINF_SUCCESS;
1685}
1686
1687
1688/**
1689 * Wroker function that displays one descriptor entry (GDT, LDT, IDT).
1690 *
1691 * @returns pfnPrintf status code.
1692 * @param pCmdHlp The DBGC command helpers.
1693 * @param pDesc The descriptor to display.
1694 * @param iEntry The descriptor entry number.
1695 * @param fHyper Whether the selector belongs to the hypervisor or not.
1696 */
1697static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
1698{
1699 int rc;
1700
1701 const char *pszHyper = fHyper ? " HYPER" : "";
1702 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
1703 if (pDesc->Gen.u1DescType)
1704 {
1705 static const char * const s_apszTypes[] =
1706 {
1707 "DataRO", /* 0 Read-Only */
1708 "DataRO", /* 1 Read-Only - Accessed */
1709 "DataRW", /* 2 Read/Write */
1710 "DataRW", /* 3 Read/Write - Accessed */
1711 "DownRO", /* 4 Expand-down, Read-Only */
1712 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
1713 "DownRW", /* 6 Expand-down, Read/Write */
1714 "DownRO", /* 7 Expand-down, Read/Write - Accessed */
1715 "CodeEO", /* 8 Execute-Only */
1716 "CodeEO", /* 9 Execute-Only - Accessed */
1717 "CodeER", /* A Execute/Readable */
1718 "CodeER", /* B Execute/Readable - Accessed */
1719 "ConfE0", /* C Conforming, Execute-Only */
1720 "ConfE0", /* D Conforming, Execute-Only - Accessed */
1721 "ConfER", /* E Conforming, Execute/Readable */
1722 "ConfER" /* F Conforming, Execute/Readable - Accessed */
1723 };
1724 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
1725 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1726 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1727 uint32_t u32Base = pDesc->Gen.u16BaseLow
1728 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1729 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1730 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1731 if (pDesc->Gen.u1Granularity)
1732 cbLimit <<= PAGE_SHIFT;
1733
1734 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
1735 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1736 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
1737 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
1738 }
1739 else
1740 {
1741 static const char * const s_apszTypes[] =
1742 {
1743 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
1744 "Tss16A", /* 1 0001 Available 16-bit TSS */
1745 "LDT ", /* 2 0010 LDT */
1746 "Tss16B", /* 3 0011 Busy 16-bit TSS */
1747 "Call16", /* 4 0100 16-bit Call Gate */
1748 "TaskG ", /* 5 0101 Task Gate */
1749 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
1750 "Trap16", /* 7 0111 16-bit Trap Gate */
1751 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
1752 "Tss32A", /* 9 1001 Available 32-bit TSS */
1753 "Ill-A ", /* A 1010 Reserved (Illegal) */
1754 "Tss32B", /* B 1011 Busy 32-bit TSS */
1755 "Call32", /* C 1100 32-bit Call Gate */
1756 "Ill-D ", /* D 1101 Reserved (Illegal) */
1757 "Int32 ", /* E 1110 32-bit Interrupt Gate */
1758 "Trap32" /* F 1111 32-bit Trap Gate */
1759 };
1760 switch (pDesc->Gen.u4Type)
1761 {
1762 /* raw */
1763 case X86_SEL_TYPE_SYS_UNDEFINED:
1764 case X86_SEL_TYPE_SYS_UNDEFINED2:
1765 case X86_SEL_TYPE_SYS_UNDEFINED4:
1766 case X86_SEL_TYPE_SYS_UNDEFINED3:
1767 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s %.8Rhxs DPL=%d %s%s\n",
1768 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
1769 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1770 break;
1771
1772 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1773 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1774 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1775 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1776 case X86_SEL_TYPE_SYS_LDT:
1777 {
1778 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1779 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
1780 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1781 uint32_t u32Base = pDesc->Gen.u16BaseLow
1782 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1783 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1784 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1785 if (pDesc->Gen.u1Granularity)
1786 cbLimit <<= PAGE_SHIFT;
1787
1788 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
1789 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1790 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
1791 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
1792 pszHyper);
1793 break;
1794 }
1795
1796 case X86_SEL_TYPE_SYS_TASK_GATE:
1797 {
1798 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s TSS=%04x DPL=%d %s%s\n",
1799 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
1800 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1801 break;
1802 }
1803
1804 case X86_SEL_TYPE_SYS_286_CALL_GATE:
1805 case X86_SEL_TYPE_SYS_386_CALL_GATE:
1806 {
1807 unsigned cParams = pDesc->au8[0] & 0x1f;
1808 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
1809 RTSEL sel = pDesc->au16[1];
1810 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
1811 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
1812 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1813 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
1814 break;
1815 }
1816
1817 case X86_SEL_TYPE_SYS_286_INT_GATE:
1818 case X86_SEL_TYPE_SYS_386_INT_GATE:
1819 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
1820 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
1821 {
1822 RTSEL sel = pDesc->au16[1];
1823 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
1824 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
1825 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1826 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1827 break;
1828 }
1829
1830 /* impossible, just it's necessary to keep gcc happy. */
1831 default:
1832 return VINF_SUCCESS;
1833 }
1834 }
1835 return rc;
1836}
1837
1838
1839/**
1840 * The 'dg', 'dga', 'dl' and 'dla' commands.
1841 *
1842 * @returns VBox status.
1843 * @param pCmd Pointer to the command descriptor (as registered).
1844 * @param pCmdHlp Pointer to command helper functions.
1845 * @param pVM Pointer to the current VM (if any).
1846 * @param paArgs Pointer to (readonly) array of arguments.
1847 * @param cArgs Number of arguments in the array.
1848 */
1849static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1850{
1851 /*
1852 * Validate input.
1853 */
1854 if (!pVM)
1855 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
1856
1857 /*
1858 * Get the CPU mode, check which command variation this is
1859 * and fix a default parameter if needed.
1860 */
1861 CPUMMODE enmMode = CPUMGetGuestMode(pVM);
1862 bool fGdt = pCmd->pszCmd[1] == 'g';
1863 bool fAll = pCmd->pszCmd[2] == 'a';
1864
1865 DBGCVAR Var;
1866 if (!cArgs)
1867 {
1868 cArgs = 1;
1869 paArgs = &Var;
1870 Var.enmType = DBGCVAR_TYPE_NUMBER;
1871 Var.u.u64Number = fGdt ? 0 : 4;
1872 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1873 Var.u64Range = 1024;
1874 }
1875
1876 /*
1877 * Process the arguments.
1878 */
1879 for (unsigned i = 0; i < cArgs; i++)
1880 {
1881 /*
1882 * Retrive the selector value from the argument.
1883 * The parser may confuse pointers and numbers if more than one
1884 * argument is given, that that into account.
1885 */
1886 /* check that what've got makes sense as we don't trust the parser yet. */
1887 if ( paArgs[i].enmType != DBGCVAR_TYPE_NUMBER
1888 && !DBGCVAR_ISPOINTER(paArgs[i].enmType))
1889 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: arg #%u isn't of number or pointer type but %d.\n", i, paArgs[i].enmType);
1890 uint64_t u64;
1891 unsigned cSels = 1;
1892 switch (paArgs[i].enmType)
1893 {
1894 case DBGCVAR_TYPE_NUMBER:
1895 u64 = paArgs[i].u.u64Number;
1896 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
1897 cSels = RT_MIN(paArgs[i].u64Range, 1024);
1898 break;
1899 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
1900 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
1901 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
1902 case DBGCVAR_TYPE_HC_FAR: u64 = paArgs[i].u.HCFar.sel; break;
1903 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
1904 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
1905 default: u64 = _64K; break;
1906 }
1907 if (u64 < _64K)
1908 {
1909 unsigned Sel = (RTSEL)u64;
1910
1911 /*
1912 * Dump the specified range.
1913 */
1914 bool fSingle = cSels == 1;
1915 while ( cSels-- > 0
1916 && Sel < _64K)
1917 {
1918 SELMSELINFO SelInfo;
1919 int rc = SELMR3GetSelectorInfo(pVM, Sel, &SelInfo);
1920 if (RT_SUCCESS(rc))
1921 {
1922 if (SelInfo.fRealMode)
1923 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x RealM Bas=%04x Lim=%04x\n",
1924 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
1925 else if (fAll || fSingle || SelInfo.Raw.Gen.u1Present)
1926 {
1927 if (enmMode == CPUMMODE_PROTECTED)
1928 rc = dbgcCmdDumpDTWorker32(pCmdHlp, (PX86DESC)&SelInfo.Raw, Sel, SelInfo.fHyper);
1929 else
1930 {
1931 bool fDblSkip = false;
1932 rc = dbgcCmdDumpDTWorker64(pCmdHlp, (PX86DESC64)&SelInfo.Raw, Sel, SelInfo.fHyper, &fDblSkip);
1933 if (fDblSkip)
1934 Sel += 4;
1935 }
1936 }
1937 }
1938 else
1939 {
1940 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %Vrc\n", Sel, rc);
1941 if (!fAll)
1942 return rc;
1943 }
1944 if (RT_FAILURE(rc))
1945 return rc;
1946
1947 /* next */
1948 Sel += 4;
1949 }
1950 }
1951 else
1952 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %llx is out of bounds\n", u64);
1953 }
1954
1955 NOREF(pResult);
1956 return VINF_SUCCESS;
1957}
1958
1959
1960/**
1961 * The 'di' and 'dia' commands.
1962 *
1963 * @returns VBox status.
1964 * @param pCmd Pointer to the command descriptor (as registered).
1965 * @param pCmdHlp Pointer to command helper functions.
1966 * @param pVM Pointer to the current VM (if any).
1967 * @param paArgs Pointer to (readonly) array of arguments.
1968 * @param cArgs Number of arguments in the array.
1969 */
1970static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1971{
1972 /*
1973 * Validate input.
1974 */
1975 if (!pVM)
1976 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
1977
1978 /*
1979 * Establish some stuff like the current IDTR and CPU mode,
1980 * and fix a default parameter.
1981 */
1982 uint16_t cbLimit;
1983 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVM, &cbLimit);
1984 CPUMMODE enmMode = CPUMGetGuestMode(pVM);
1985 unsigned cbEntry;
1986 switch (enmMode)
1987 {
1988 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
1989 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
1990 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
1991 default:
1992 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid CPU mode %d.\n", enmMode);
1993 }
1994
1995 bool fAll = pCmd->pszCmd[2] == 'a';
1996 DBGCVAR Var;
1997 if (!cArgs)
1998 {
1999 cArgs = 1;
2000 paArgs = &Var;
2001 Var.enmType = DBGCVAR_TYPE_NUMBER;
2002 Var.u.u64Number = 0;
2003 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2004 Var.u64Range = 256;
2005 }
2006
2007 /*
2008 * Process the arguments.
2009 */
2010 for (unsigned i = 0; i < cArgs; i++)
2011 {
2012 /* check that what've got makes sense as we don't trust the parser yet. */
2013 if (paArgs[i].enmType != DBGCVAR_TYPE_NUMBER)
2014 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: arg #%u isn't of number type but %d.\n", i, paArgs[i].enmType);
2015 if (paArgs[i].u.u64Number < 256)
2016 {
2017 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
2018 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
2019 ? paArgs[i].u64Range
2020 : 1;
2021 bool fSingle = cInts == 1;
2022 while ( cInts-- > 0
2023 && iInt < 256)
2024 {
2025 /*
2026 * Try read it.
2027 */
2028 union
2029 {
2030 RTFAR16 Real;
2031 X86DESC Prot;
2032 X86DESC64 Long;
2033 } u;
2034 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
2035 {
2036 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x not within the IDT\n", (unsigned)iInt);
2037 if (!fAll && !fSingle)
2038 return VINF_SUCCESS;
2039 }
2040 DBGCVAR AddrVar;
2041 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
2042 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
2043 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
2044 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &u, cbEntry, &AddrVar, NULL);
2045 if (VBOX_FAILURE(rc))
2046 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
2047
2048 /*
2049 * Display it.
2050 */
2051 switch (enmMode)
2052 {
2053 case CPUMMODE_REAL:
2054 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
2055 /** @todo resolve 16:16 IDTE to a symbol */
2056 break;
2057 case CPUMMODE_PROTECTED:
2058 if (fAll || fSingle || u.Prot.Gen.u1Present)
2059 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
2060 break;
2061 case CPUMMODE_LONG:
2062 if (fAll || fSingle || u.Long.Gen.u1Present)
2063 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
2064 break;
2065 default: break; /* to shut up gcc */
2066 }
2067 if (RT_FAILURE(rc))
2068 return rc;
2069
2070 /* next */
2071 iInt++;
2072 }
2073 }
2074 else
2075 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
2076 }
2077
2078 NOREF(pResult);
2079 return VINF_SUCCESS;
2080}
2081
2082
2083/**
2084 * The 'da', 'dq', 'dd', 'dw' and 'db' commands.
2085 *
2086 * @returns VBox status.
2087 * @param pCmd Pointer to the command descriptor (as registered).
2088 * @param pCmdHlp Pointer to command helper functions.
2089 * @param pVM Pointer to the current VM (if any).
2090 * @param paArgs Pointer to (readonly) array of arguments.
2091 * @param cArgs Number of arguments in the array.
2092 */
2093static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2094{
2095 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2096
2097 /*
2098 * Validate input.
2099 */
2100 if ( cArgs > 1
2101 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2102 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2103 if (!pVM)
2104 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2105
2106 /*
2107 * Figure out the element size.
2108 */
2109 unsigned cbElement;
2110 bool fAscii = false;
2111 switch (pCmd->pszCmd[1])
2112 {
2113 default:
2114 case 'b': cbElement = 1; break;
2115 case 'w': cbElement = 2; break;
2116 case 'd': cbElement = 4; break;
2117 case 'q': cbElement = 8; break;
2118 case 'a':
2119 cbElement = 1;
2120 fAscii = true;
2121 break;
2122 case '\0':
2123 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
2124 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
2125 if (!cbElement)
2126 cbElement = 1;
2127 break;
2128 }
2129
2130 /*
2131 * Find address.
2132 */
2133 if (!cArgs)
2134 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
2135 else
2136 pDbgc->DumpPos = paArgs[0];
2137
2138 /*
2139 * Range.
2140 */
2141 switch (pDbgc->DumpPos.enmRangeType)
2142 {
2143 case DBGCVAR_RANGE_NONE:
2144 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2145 pDbgc->DumpPos.u64Range = 0x60;
2146 break;
2147
2148 case DBGCVAR_RANGE_ELEMENTS:
2149 if (pDbgc->DumpPos.u64Range > 2048)
2150 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many elements requested. Max is 2048 elements.\n");
2151 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2152 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
2153 break;
2154
2155 case DBGCVAR_RANGE_BYTES:
2156 if (pDbgc->DumpPos.u64Range > 65536)
2157 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
2158 break;
2159
2160 default:
2161 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
2162 }
2163
2164 /*
2165 * Do the dumping.
2166 */
2167 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
2168 int cbLeft = (int)pDbgc->DumpPos.u64Range;
2169 uint8_t u8Prev = '\0';
2170 for (;;)
2171 {
2172 /*
2173 * Read memory.
2174 */
2175 char achBuffer[16];
2176 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
2177 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
2178 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
2179 if (VBOX_FAILURE(rc))
2180 {
2181 if (u8Prev && u8Prev != '\n')
2182 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2183 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
2184 }
2185
2186 /*
2187 * Display it.
2188 */
2189 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
2190 if (!fAscii)
2191 {
2192 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:", &pDbgc->DumpPos);
2193 unsigned i;
2194 for (i = 0; i < cb; i += cbElement)
2195 {
2196 const char *pszSpace = " ";
2197 if (cbElement <= 2 && i == 8 && !fAscii)
2198 pszSpace = "-";
2199 switch (cbElement)
2200 {
2201 case 1: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
2202 case 2: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
2203 case 4: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
2204 case 8: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
2205 }
2206 }
2207
2208 /* chars column */
2209 if (pDbgc->cbDumpElement == 1)
2210 {
2211 while (i++ < sizeof(achBuffer))
2212 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2213 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2214 for (i = 0; i < cb; i += cbElement)
2215 {
2216 uint8_t u8 = *(uint8_t *)&achBuffer[i];
2217 if (isprint(u8) && u8 < 127 && u8 >= 32)
2218 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2219 else
2220 pCmdHlp->pfnPrintf(pCmdHlp, NULL, ".");
2221 }
2222 }
2223 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2224 }
2225 else
2226 {
2227 /*
2228 * We print up to the first zero and stop there.
2229 * Only printables + '\t' and '\n' are printed.
2230 */
2231 if (!u8Prev)
2232 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DumpPos);
2233 uint8_t u8 = '\0';
2234 unsigned i;
2235 for (i = 0; i < cb; i++)
2236 {
2237 u8Prev = u8;
2238 u8 = *(uint8_t *)&achBuffer[i];
2239 if ( u8 < 127
2240 && ( (isprint(u8) && u8 >= 32)
2241 || u8 == '\t'
2242 || u8 == '\n'))
2243 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2244 else if (!u8)
2245 break;
2246 else
2247 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\\x%x", u8);
2248 }
2249 if (u8 == '\0')
2250 cb = cbLeft = i + 1;
2251 if (cbLeft - cb <= 0 && u8Prev != '\n')
2252 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2253 }
2254
2255 /*
2256 * Advance
2257 */
2258 cbLeft -= (int)cb;
2259 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
2260 if (VBOX_FAILURE(rc))
2261 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
2262 if (cbLeft <= 0)
2263 break;
2264 }
2265
2266 NOREF(pCmd); NOREF(pResult);
2267 return VINF_SUCCESS;
2268}
2269
2270
2271/**
2272 * Best guess at which paging mode currently applies to the guest
2273 * paging structures.
2274 *
2275 * This have to come up with a decent answer even when the guest
2276 * is in non-paged protected mode or real mode.
2277 *
2278 * @returns cr3.
2279 * @param pDbgc The DBGC instance.
2280 * @param pfPAE Where to store the page address extension indicator.
2281 * @param pfLME Where to store the long mode enabled indicator.
2282 * @param pfPSE Where to store the page size extension indicator.
2283 * @param pfPGE Where to store the page global enabled indicator.
2284 * @param pfNXE Where to store the no-execution enabled inidicator.
2285 */
2286static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2287{
2288 RTGCUINTREG cr4 = CPUMGetGuestCR4(pDbgc->pVM);
2289 *pfPSE = !!(cr4 & X86_CR4_PSE);
2290 *pfPGE = !!(cr4 & X86_CR4_PGE);
2291 *pfPAE = !!(cr4 & X86_CR4_PAE);
2292 *pfLME = CPUMGetGuestMode(pDbgc->pVM) == CPUMMODE_LONG;
2293 *pfNXE = false; /* GUEST64 GUESTNX */
2294 return CPUMGetGuestCR3(pDbgc->pVM);
2295}
2296
2297
2298/**
2299 * Determin the shadow paging mode.
2300 *
2301 * @returns cr3.
2302 * @param pDbgc The DBGC instance.
2303 * @param pfPAE Where to store the page address extension indicator.
2304 * @param pfLME Where to store the long mode enabled indicator.
2305 * @param pfPSE Where to store the page size extension indicator.
2306 * @param pfPGE Where to store the page global enabled indicator.
2307 * @param pfNXE Where to store the no-execution enabled inidicator.
2308 */
2309static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2310{
2311 *pfPSE = true;
2312 *pfPGE = false;
2313 switch (PGMGetShadowMode(pDbgc->pVM))
2314 {
2315 default:
2316 case PGMMODE_32_BIT:
2317 *pfPAE = *pfLME = *pfNXE = false;
2318 break;
2319 case PGMMODE_PAE:
2320 *pfLME = *pfNXE = false;
2321 *pfPAE = true;
2322 break;
2323 case PGMMODE_PAE_NX:
2324 *pfLME = false;
2325 *pfPAE = *pfNXE = true;
2326 break;
2327 case PGMMODE_AMD64:
2328 *pfNXE = false;
2329 *pfPAE = *pfLME = true;
2330 break;
2331 case PGMMODE_AMD64_NX:
2332 *pfPAE = *pfLME = *pfNXE = true;
2333 break;
2334 }
2335 return PGMGetHyperCR3(pDbgc->pVM);
2336}
2337
2338
2339/**
2340 * The 'dpd', 'dpda', 'dpdb', 'dpdg' and 'dpdh' commands.
2341 *
2342 * @returns VBox status.
2343 * @param pCmd Pointer to the command descriptor (as registered).
2344 * @param pCmdHlp Pointer to command helper functions.
2345 * @param pVM Pointer to the current VM (if any).
2346 * @param paArgs Pointer to (readonly) array of arguments.
2347 * @param cArgs Number of arguments in the array.
2348 */
2349static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2350{
2351 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2352
2353 /*
2354 * Validate input.
2355 */
2356 if ( cArgs > 1
2357 || (cArgs == 1 && pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2358 || (cArgs == 1 && pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2359 )
2360 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2361 if (!pVM)
2362 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2363
2364 /*
2365 * Guest or shadow page directories? Get the paging parameters.
2366 */
2367 bool fGuest = pCmd->pszCmd[3] != 'h';
2368 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2369 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2370 ? pDbgc->fRegCtxGuest
2371 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2372
2373 bool fPAE, fLME, fPSE, fPGE, fNXE;
2374 uint64_t cr3 = fGuest
2375 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2376 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2377 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2378
2379 /*
2380 * Setup default arugment if none was specified.
2381 * Fix address / index confusion.
2382 */
2383 DBGCVAR VarDefault;
2384 if (!cArgs)
2385 {
2386 if (pCmd->pszCmd[3] == 'a')
2387 {
2388 if (fLME || fPAE)
2389 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");
2390 if (fGuest)
2391 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
2392 else
2393 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
2394 }
2395 else
2396 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
2397 paArgs = &VarDefault;
2398 cArgs = 1;
2399 }
2400 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
2401 {
2402 Assert(pCmd->pszCmd[3] != 'a');
2403 VarDefault = paArgs[0];
2404 if (VarDefault.u.u64Number <= 1024)
2405 {
2406 if (fPAE)
2407 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
2408 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
2409 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
2410 VarDefault.u.u64Number <<= X86_PD_SHIFT;
2411 }
2412 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
2413 paArgs = &VarDefault;
2414 }
2415
2416 /*
2417 * Locate the PDE to start displaying at.
2418 *
2419 * The 'dpda' command takes the address of a PDE, while the others are guest
2420 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
2421 * while the others require us to do all the tedious walking thru the paging
2422 * hierarchy to find the intended PDE.
2423 */
2424 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
2425 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
2426 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
2427 unsigned cEntries; /* The number of entries to display. */
2428 unsigned cEntriesMax; /* The max number of entries to display. */
2429 int rc;
2430 if (pCmd->pszCmd[3] == 'a')
2431 {
2432 VarPDEAddr = paArgs[0];
2433 switch (VarPDEAddr.enmRangeType)
2434 {
2435 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
2436 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
2437 default: cEntries = 10; break;
2438 }
2439 cEntriesMax = PAGE_SIZE / cbEntry;
2440 }
2441 else
2442 {
2443 /*
2444 * Determin the range.
2445 */
2446 switch (paArgs[0].enmRangeType)
2447 {
2448 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2449 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2450 default: cEntries = 10; break;
2451 }
2452
2453 /*
2454 * Normalize the input address, it must be a flat GC address.
2455 */
2456 rc = pCmdHlp->pfnEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2457 if (VBOX_FAILURE(rc))
2458 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2459 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2460 {
2461 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2462 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2463 }
2464 if (fPAE)
2465 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
2466 else
2467 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
2468
2469 /*
2470 * Do the paging walk until we get to the page directory.
2471 */
2472 DBGCVAR VarCur;
2473 if (fGuest)
2474 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2475 else
2476 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2477 if (fLME)
2478 {
2479 /* Page Map Level 4 Lookup. */
2480 /* Check if it's a valid address first? */
2481 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2482 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2483 X86PML4E Pml4e;
2484 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2485 if (VBOX_FAILURE(rc))
2486 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2487 if (!Pml4e.n.u1Present)
2488 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2489
2490 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2491 Assert(fPAE);
2492 }
2493 if (fPAE)
2494 {
2495 /* Page directory pointer table. */
2496 X86PDPE Pdpe;
2497 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2498 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2499 if (VBOX_FAILURE(rc))
2500 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2501 if (!Pdpe.n.u1Present)
2502 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2503
2504 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2505 VarPDEAddr = VarCur;
2506 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2507 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
2508 }
2509 else
2510 {
2511 /* 32-bit legacy - CR3 == page directory. */
2512 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
2513 VarPDEAddr = VarCur;
2514 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
2515 }
2516 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2517 iEntry /= cbEntry;
2518 }
2519
2520 /* adjust cEntries */
2521 cEntries = RT_MAX(1, cEntries);
2522 cEntries = RT_MIN(cEntries, cEntriesMax);
2523
2524 /*
2525 * The display loop.
2526 */
2527 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
2528 &VarPDEAddr, iEntry);
2529 do
2530 {
2531 /*
2532 * Read.
2533 */
2534 X86PDEPAE Pde;
2535 Pde.u = 0;
2536 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, cbEntry, &VarPDEAddr, NULL);
2537 if (VBOX_FAILURE(rc))
2538 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
2539
2540 /*
2541 * Display.
2542 */
2543 if (iEntry != ~0U)
2544 {
2545 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2546 iEntry++;
2547 }
2548 if (fPSE && Pde.b.u1Size)
2549 DBGCCmdHlpPrintf(pCmdHlp,
2550 fPAE
2551 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2552 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2553 Pde.u,
2554 Pde.u & X86_PDE_PAE_PG_MASK,
2555 Pde.b.u1Present ? "p " : "np",
2556 Pde.b.u1Write ? "w" : "r",
2557 Pde.b.u1User ? "u" : "s",
2558 Pde.b.u1Accessed ? "a " : "na",
2559 Pde.b.u1Dirty ? "d " : "nd",
2560 Pde.b.u3Available,
2561 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
2562 Pde.b.u1WriteThru ? "pwt" : " ",
2563 Pde.b.u1CacheDisable ? "pcd" : " ",
2564 Pde.b.u1PAT ? "pat" : "",
2565 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2566 else
2567 DBGCCmdHlpPrintf(pCmdHlp,
2568 fPAE
2569 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
2570 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
2571 Pde.u,
2572 Pde.u & X86_PDE_PAE_PG_MASK,
2573 Pde.n.u1Present ? "p " : "np",
2574 Pde.n.u1Write ? "w" : "r",
2575 Pde.n.u1User ? "u" : "s",
2576 Pde.n.u1Accessed ? "a " : "na",
2577 Pde.u & RT_BIT(6) ? "6 " : " ",
2578 Pde.n.u3Available,
2579 Pde.u & RT_BIT(8) ? "8" : " ",
2580 Pde.n.u1WriteThru ? "pwt" : " ",
2581 Pde.n.u1CacheDisable ? "pcd" : " ",
2582 Pde.u & RT_BIT(7) ? "7" : "",
2583 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2584 if (Pde.u & UINT64_C(0x7fff000000000000))
2585 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
2586 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2587 if (VBOX_FAILURE(rc))
2588 return rc;
2589
2590 /*
2591 * Advance.
2592 */
2593 VarPDEAddr.u.u64Number += cbEntry;
2594 if (iEntry != ~0U)
2595 VarGCPtr.u.GCFlat += 1 << (fPAE ? X86_PD_PAE_SHIFT : X86_PD_SHIFT);
2596 } while (cEntries-- > 0);
2597
2598 NOREF(pResult);
2599 return VINF_SUCCESS;
2600}
2601
2602
2603/**
2604 * The 'dpdb' command.
2605 *
2606 * @returns VBox status.
2607 * @param pCmd Pointer to the command descriptor (as registered).
2608 * @param pCmdHlp Pointer to command helper functions.
2609 * @param pVM Pointer to the current VM (if any).
2610 * @param paArgs Pointer to (readonly) array of arguments.
2611 * @param cArgs Number of arguments in the array.
2612 */
2613static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2614{
2615 if (!pVM)
2616 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2617 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2618 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2619 if (VBOX_FAILURE(rc1))
2620 return rc1;
2621 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
2622 return rc2;
2623}
2624
2625
2626/**
2627 * The 'dpg*' commands.
2628 *
2629 * @returns VBox status.
2630 * @param pCmd Pointer to the command descriptor (as registered).
2631 * @param pCmdHlp Pointer to command helper functions.
2632 * @param pVM Pointer to the current VM (if any).
2633 * @param paArgs Pointer to (readonly) array of arguments.
2634 * @param cArgs Number of arguments in the array.
2635 */
2636static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2637{
2638 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2639
2640 /*
2641 * Validate input.
2642 */
2643 if ( cArgs != 1
2644 || (pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2645 || (pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2646 )
2647 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2648 if (!pVM)
2649 return DBGCCmdHlpPrintf(pCmdHlp, "error: No VM.\n");
2650
2651 /*
2652 * Guest or shadow page tables? Get the paging parameters.
2653 */
2654 bool fGuest = pCmd->pszCmd[3] != 'h';
2655 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2656 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2657 ? pDbgc->fRegCtxGuest
2658 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2659
2660 bool fPAE, fLME, fPSE, fPGE, fNXE;
2661 uint64_t cr3 = fGuest
2662 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2663 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2664 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2665
2666 /*
2667 * Locate the PTE to start displaying at.
2668 *
2669 * The 'dpta' command takes the address of a PTE, while the others are guest
2670 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
2671 * while the others require us to do all the tedious walking thru the paging
2672 * hierarchy to find the intended PTE.
2673 */
2674 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
2675 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
2676 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
2677 unsigned cEntries; /* The number of entries to display. */
2678 unsigned cEntriesMax; /* The max number of entries to display. */
2679 int rc;
2680 if (pCmd->pszCmd[3] == 'a')
2681 {
2682 VarPTEAddr = paArgs[0];
2683 switch (VarPTEAddr.enmRangeType)
2684 {
2685 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
2686 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
2687 default: cEntries = 10; break;
2688 }
2689 cEntriesMax = PAGE_SIZE / cbEntry;
2690 }
2691 else
2692 {
2693 /*
2694 * Determin the range.
2695 */
2696 switch (paArgs[0].enmRangeType)
2697 {
2698 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2699 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2700 default: cEntries = 10; break;
2701 }
2702
2703 /*
2704 * Normalize the input address, it must be a flat GC address.
2705 */
2706 rc = pCmdHlp->pfnEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2707 if (VBOX_FAILURE(rc))
2708 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2709 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2710 {
2711 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2712 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2713 }
2714 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
2715
2716 /*
2717 * Do the paging walk until we get to the page table.
2718 */
2719 DBGCVAR VarCur;
2720 if (fGuest)
2721 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2722 else
2723 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2724 if (fLME)
2725 {
2726 /* Page Map Level 4 Lookup. */
2727 /* Check if it's a valid address first? */
2728 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2729 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2730 X86PML4E Pml4e;
2731 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2732 if (VBOX_FAILURE(rc))
2733 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2734 if (!Pml4e.n.u1Present)
2735 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2736
2737 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2738 Assert(fPAE);
2739 }
2740 if (fPAE)
2741 {
2742 /* Page directory pointer table. */
2743 X86PDPE Pdpe;
2744 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2745 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2746 if (VBOX_FAILURE(rc))
2747 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2748 if (!Pdpe.n.u1Present)
2749 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2750
2751 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2752
2753 /* Page directory (PAE). */
2754 X86PDEPAE Pde;
2755 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
2756 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
2757 if (VBOX_FAILURE(rc))
2758 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
2759 if (!Pde.n.u1Present)
2760 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
2761 if (fPSE && Pde.n.u1Size)
2762 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
2763
2764 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
2765 VarPTEAddr = VarCur;
2766 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
2767 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
2768 }
2769 else
2770 {
2771 /* Page directory (legacy). */
2772 X86PDE Pde;
2773 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
2774 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
2775 if (VBOX_FAILURE(rc))
2776 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
2777 if (!Pde.n.u1Present)
2778 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
2779 if (fPSE && Pde.n.u1Size)
2780 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
2781
2782 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
2783 VarPTEAddr = VarCur;
2784 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
2785 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
2786 }
2787 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2788 iEntry /= cbEntry;
2789 }
2790
2791 /* adjust cEntries */
2792 cEntries = RT_MAX(1, cEntries);
2793 cEntries = RT_MIN(cEntries, cEntriesMax);
2794
2795 /*
2796 * The display loop.
2797 */
2798 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
2799 &VarPTEAddr, &VarGCPtr, iEntry);
2800 do
2801 {
2802 /*
2803 * Read.
2804 */
2805 X86PTEPAE Pte;
2806 Pte.u = 0;
2807 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pte, cbEntry, &VarPTEAddr, NULL);
2808 if (VBOX_FAILURE(rc))
2809 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
2810
2811 /*
2812 * Display.
2813 */
2814 if (iEntry != ~0U)
2815 {
2816 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2817 iEntry++;
2818 }
2819 DBGCCmdHlpPrintf(pCmdHlp,
2820 fPAE
2821 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2822 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2823 Pte.u,
2824 Pte.u & X86_PTE_PAE_PG_MASK,
2825 Pte.n.u1Present ? "p " : "np",
2826 Pte.n.u1Write ? "w" : "r",
2827 Pte.n.u1User ? "u" : "s",
2828 Pte.n.u1Accessed ? "a " : "na",
2829 Pte.n.u1Dirty ? "d " : "nd",
2830 Pte.n.u3Available,
2831 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
2832 Pte.n.u1WriteThru ? "pwt" : " ",
2833 Pte.n.u1CacheDisable ? "pcd" : " ",
2834 Pte.n.u1PAT ? "pat" : " ",
2835 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
2836 );
2837 if (Pte.u & UINT64_C(0x7fff000000000000))
2838 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
2839 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2840 if (VBOX_FAILURE(rc))
2841 return rc;
2842
2843 /*
2844 * Advance.
2845 */
2846 VarPTEAddr.u.u64Number += cbEntry;
2847 if (iEntry != ~0U)
2848 VarGCPtr.u.GCFlat += PAGE_SIZE;
2849 } while (cEntries-- > 0);
2850
2851 NOREF(pResult);
2852 return VINF_SUCCESS;
2853}
2854
2855
2856/**
2857 * The 'dptb' command.
2858 *
2859 * @returns VBox status.
2860 * @param pCmd Pointer to the command descriptor (as registered).
2861 * @param pCmdHlp Pointer to command helper functions.
2862 * @param pVM Pointer to the current VM (if any).
2863 * @param paArgs Pointer to (readonly) array of arguments.
2864 * @param cArgs Number of arguments in the array.
2865 */
2866static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2867{
2868 if (!pVM)
2869 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2870 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
2871 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
2872 if (VBOX_FAILURE(rc1))
2873 return rc1;
2874 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
2875 return rc2;
2876}
2877
2878
2879/**
2880 * The 'dt' command.
2881 *
2882 * @returns VBox status.
2883 * @param pCmd Pointer to the command descriptor (as registered).
2884 * @param pCmdHlp Pointer to command helper functions.
2885 * @param pVM Pointer to the current VM (if any).
2886 * @param paArgs Pointer to (readonly) array of arguments.
2887 * @param cArgs Number of arguments in the array.
2888 */
2889static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM /*pVM*/, PCDBGCVAR /*paArgs*/, unsigned /*cArgs*/, PDBGCVAR /*pResult*/)
2890{
2891 /*
2892 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
2893 */
2894
2895 /** @todo */
2896 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "dt is not implemented yet, feel free to do it. \n");
2897}
2898
2899
2900/**
2901 * The 'm' command.
2902 *
2903 * @returns VBox status.
2904 * @param pCmd Pointer to the command descriptor (as registered).
2905 * @param pCmdHlp Pointer to command helper functions.
2906 * @param pVM Pointer to the current VM (if any).
2907 * @param paArgs Pointer to (readonly) array of arguments.
2908 * @param cArgs Number of arguments in the array.
2909 */
2910static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2911{
2912 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Address: %DV\n", &paArgs[0]);
2913 if (!pVM)
2914 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2915 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2916 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2917 int rc3 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
2918 int rc4 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
2919 if (VBOX_FAILURE(rc1))
2920 return rc1;
2921 if (VBOX_FAILURE(rc2))
2922 return rc2;
2923 if (VBOX_FAILURE(rc3))
2924 return rc3;
2925 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
2926 return rc4;
2927}
2928
2929
2930/**
2931 * Converts one or more variables into a byte buffer for a
2932 * given unit size.
2933 *
2934 * @returns VBox status codes:
2935 * @retval VERR_TOO_MUCH_DATA if the buffer is too small, bitched.
2936 * @retval VERR_INTERNAL_ERROR on bad variable type, bitched.
2937 * @retval VINF_SUCCESS on success.
2938 *
2939 * @param pvBuf The buffer to convert into.
2940 * @param pcbBuf The buffer size on input. The size of the result on output.
2941 * @param cbUnit The unit size to apply when converting.
2942 * The high bit is used to indicate unicode string.
2943 * @param paVars The array of variables to convert.
2944 * @param cVars The number of variables.
2945 */
2946int dbgcVarsToBytes(PDBGCCMDHLP pCmdHlp, void *pvBuf, uint32_t *pcbBuf, size_t cbUnit, PCDBGCVAR paVars, unsigned cVars)
2947{
2948 union
2949 {
2950 uint8_t *pu8;
2951 uint16_t *pu16;
2952 uint32_t *pu32;
2953 uint64_t *pu64;
2954 } u, uEnd;
2955 u.pu8 = (uint8_t *)pvBuf;
2956 uEnd.pu8 = u.pu8 + *pcbBuf;
2957
2958 unsigned i;
2959 for (i = 0; i < cVars && u.pu8 < uEnd.pu8; i++)
2960 {
2961 switch (paVars[i].enmType)
2962 {
2963 case DBGCVAR_TYPE_GC_FAR:
2964 case DBGCVAR_TYPE_HC_FAR:
2965 case DBGCVAR_TYPE_GC_FLAT:
2966 case DBGCVAR_TYPE_GC_PHYS:
2967 case DBGCVAR_TYPE_HC_FLAT:
2968 case DBGCVAR_TYPE_HC_PHYS:
2969 case DBGCVAR_TYPE_NUMBER:
2970 {
2971 uint64_t u64 = paVars[i].u.u64Number;
2972 switch (cbUnit & 0x1f)
2973 {
2974 case 1:
2975 do
2976 {
2977 *u.pu8++ = u64;
2978 u64 >>= 8;
2979 } while (u64);
2980 break;
2981 case 2:
2982 do
2983 {
2984 *u.pu16++ = u64;
2985 u64 >>= 16;
2986 } while (u64);
2987 break;
2988 case 4:
2989 *u.pu32++ = u64;
2990 u64 >>= 32;
2991 if (u64)
2992 *u.pu32++ = u64;
2993 break;
2994 case 8:
2995 *u.pu64++ = u64;
2996 break;
2997 }
2998 break;
2999 }
3000
3001 case DBGCVAR_TYPE_STRING:
3002 case DBGCVAR_TYPE_SYMBOL:
3003 {
3004 const char *psz = paVars[i].u.pszString;
3005 size_t cbString = strlen(psz);
3006 if (cbUnit & RT_BIT_32(31))
3007 {
3008 /* Explode char to unit. */
3009 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8) * (cbUnit & 0x1f))
3010 {
3011 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3012 return VERR_TOO_MUCH_DATA;
3013 }
3014 while (*psz)
3015 {
3016 switch (cbUnit & 0x1f)
3017 {
3018 case 1: *u.pu8++ = *psz; break;
3019 case 2: *u.pu16++ = *psz; break;
3020 case 4: *u.pu32++ = *psz; break;
3021 case 8: *u.pu64++ = *psz; break;
3022 }
3023 psz++;
3024 }
3025 }
3026 else
3027 {
3028 /* Raw copy with zero padding if the size isn't aligned. */
3029 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8))
3030 {
3031 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3032 return VERR_TOO_MUCH_DATA;
3033 }
3034
3035 size_t cbCopy = cbString & ~(cbUnit - 1);
3036 memcpy(u.pu8, psz, cbCopy);
3037 u.pu8 += cbCopy;
3038 psz += cbCopy;
3039
3040 size_t cbReminder = cbString & (cbUnit - 1);
3041 if (cbReminder)
3042 {
3043 memcpy(u.pu8, psz, cbString & (cbUnit - 1));
3044 memset(u.pu8 + cbReminder, 0, cbUnit - cbReminder);
3045 u.pu8 += cbUnit;
3046 }
3047 }
3048 break;
3049 }
3050
3051 default:
3052 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3053 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INTERNAL_ERROR,
3054 "i=%d enmType=%d\n", i, paVars[i].enmType);
3055 return VERR_INTERNAL_ERROR;
3056 }
3057 }
3058 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3059 if (i != cVars)
3060 {
3061 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3062 return VERR_TOO_MUCH_DATA;
3063 }
3064 return VINF_SUCCESS;
3065}
3066
3067
3068/**
3069 * Executes the search.
3070 *
3071 * @returns VBox status code.
3072 * @param pCmdHlp The command helpers.
3073 * @param pVM The VM handle.
3074 * @param pAddress The address to start searching from. (undefined on output)
3075 * @param cbRange The address range to search. Must not wrap.
3076 * @param pabBytes The byte pattern to search for.
3077 * @param cbBytes The size of the pattern.
3078 * @param cbUnit The search unit.
3079 * @param cMaxHits The max number of hits.
3080 * @param pResult Where to store the result if it's a function invocation.
3081 */
3082static int dbgcCmdWorkerSearchMemDoIt(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGFADDRESS pAddress, RTGCUINTPTR cbRange,
3083 const uint8_t *pabBytes, uint32_t cbBytes,
3084 uint32_t cbUnit, uint64_t cMaxHits, PDBGCVAR pResult)
3085{
3086 /*
3087 * Do the search.
3088 */
3089 uint64_t cHits = 0;
3090 for (;;)
3091 {
3092 /* search */
3093 DBGFADDRESS HitAddress;
3094 int rc = DBGFR3MemScan(pVM, pAddress, cbRange, pabBytes, cbBytes, &HitAddress);
3095 if (RT_FAILURE(rc))
3096 {
3097 if (rc != VERR_DBGF_MEM_NOT_FOUND)
3098 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3MemScan\n");
3099
3100 /* update the current address so we can save it (later). */
3101 pAddress->off += cbRange;
3102 pAddress->FlatPtr += cbRange;
3103 cbRange = 0;
3104 break;
3105 }
3106
3107 /* report result */
3108 DBGCVAR VarCur;
3109 dbgcVarInit(&VarCur);
3110 dbgcVarSetDbgfAddr(&VarCur, &HitAddress);
3111 if (!pResult)
3112 pCmdHlp->pfnExec(pCmdHlp, "db %DV LB 10", &VarCur);
3113 else
3114 dbgcVarSetDbgfAddr(pResult, &HitAddress);
3115
3116 /* advance */
3117 cbRange -= HitAddress.FlatPtr - pAddress->FlatPtr;
3118 *pAddress = HitAddress;
3119 pAddress->FlatPtr += cbBytes;
3120 pAddress->off += cbBytes;
3121 if (cbRange <= cbBytes)
3122 {
3123 cbRange = 0;
3124 break;
3125 }
3126 cbRange -= cbBytes;
3127
3128 if (++cHits >= cMaxHits)
3129 {
3130 /// @todo save the search.
3131 break;
3132 }
3133 }
3134
3135 /*
3136 * Save the search so we can resume it...
3137 */
3138 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3139 if (pDbgc->abSearch != pabBytes)
3140 {
3141 memcpy(pDbgc->abSearch, pabBytes, cbBytes);
3142 pDbgc->cbSearch = cbBytes;
3143 pDbgc->cbSearchUnit = cbUnit;
3144 }
3145 pDbgc->cMaxSearchHits = cMaxHits;
3146 pDbgc->SearchAddr = *pAddress;
3147 pDbgc->cbSearchRange = cbRange;
3148
3149 return cHits ? VINF_SUCCESS : VERR_DBGC_COMMAND_FAILED;
3150}
3151
3152
3153/**
3154 * Resumes the previous search.
3155 *
3156 * @returns VBox status code.
3157 * @param pCmdHlp Pointer to the command helper functions.
3158 * @param pVM Pointer to the current VM (if any).
3159 * @param pResult Where to store the result of a function invocation.
3160 */
3161static int dbgcCmdWorkerSearchMemResume(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGCVAR pResult)
3162{
3163 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3164
3165 /*
3166 * Make sure there is a previous command.
3167 */
3168 if (!pDbgc->cbSearch)
3169 {
3170 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Error: No previous search\n");
3171 return VERR_DBGC_COMMAND_FAILED;
3172 }
3173
3174 /*
3175 * Make range and address adjustments.
3176 */
3177 DBGFADDRESS Address = pDbgc->SearchAddr;
3178 if (Address.FlatPtr == ~(RTGCUINTPTR)0)
3179 {
3180 Address.FlatPtr -= Address.off;
3181 Address.off = 0;
3182 }
3183
3184 RTGCUINTPTR cbRange = pDbgc->cbSearchRange;
3185 if (!cbRange)
3186 cbRange = ~(RTGCUINTPTR)0;
3187 if (Address.FlatPtr + cbRange < pDbgc->SearchAddr.FlatPtr)
3188 cbRange = ~(RTGCUINTPTR)0 - pDbgc->SearchAddr.FlatPtr + !!pDbgc->SearchAddr.FlatPtr;
3189
3190 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, pDbgc->abSearch, pDbgc->cbSearch,
3191 pDbgc->cbSearchUnit, pDbgc->cMaxSearchHits, pResult);
3192}
3193
3194
3195/**
3196 * Search memory, worker for the 's' and 's?' functions.
3197 *
3198 * @returns VBox status.
3199 * @param pCmdHlp Pointer to the command helper functions.
3200 * @param pVM Pointer to the current VM (if any).
3201 * @param pAddress Where to start searching. If no range, search till end of address space.
3202 * @param cMaxHits The maximum number of hits.
3203 * @param chType The search type.
3204 * @param paPatArgs The pattern variable array.
3205 * @param cPatArgs Number of pattern variables.
3206 * @param pResult Where to store the result of a function invocation.
3207 */
3208static int dbgcCmdWorkerSearchMem(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pAddress, uint64_t cMaxHits, char chType,
3209 PCDBGCVAR paPatArgs, unsigned cPatArgs, PDBGCVAR pResult)
3210{
3211 dbgcVarSetGCFlat(pResult, 0);
3212
3213 /*
3214 * Convert the search pattern into bytes and DBGFR3MemScan can deal with.
3215 */
3216 uint32_t cbUnit;
3217 switch (chType)
3218 {
3219 case 'a':
3220 case 'b': cbUnit = 1; break;
3221 case 'u': cbUnit = 2 | RT_BIT_32(31); break;
3222 case 'w': cbUnit = 2; break;
3223 case 'd': cbUnit = 4; break;
3224 case 'q': cbUnit = 8; break;
3225 default:
3226 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "chType=%c\n", chType);
3227 }
3228 uint8_t abBytes[RT_SIZEOFMEMB(DBGC, abSearch)];
3229 uint32_t cbBytes = sizeof(abBytes);
3230 int rc = dbgcVarsToBytes(pCmdHlp, abBytes, &cbBytes, cbUnit, paPatArgs, cPatArgs);
3231 if (RT_FAILURE(rc))
3232 return VERR_DBGC_COMMAND_FAILED;
3233
3234 /*
3235 * Make DBGF address and fix the range.
3236 */
3237 DBGFADDRESS Address;
3238 rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, pAddress, &Address);
3239 if (RT_FAILURE(rc))
3240 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", pAddress);
3241
3242 RTGCUINTPTR cbRange;
3243 switch (pAddress->enmRangeType)
3244 {
3245 case DBGCVAR_RANGE_BYTES:
3246 cbRange = pAddress->u64Range;
3247 if (cbRange != pAddress->u64Range)
3248 cbRange = ~(RTGCUINTPTR)0;
3249 break;
3250
3251 case DBGCVAR_RANGE_ELEMENTS:
3252 cbRange = (RTGCUINTPTR)(pAddress->u64Range * cbUnit);
3253 if ( cbRange != pAddress->u64Range * cbUnit
3254 || cbRange < pAddress->u64Range)
3255 cbRange = ~(RTGCUINTPTR)0;
3256 break;
3257
3258 default:
3259 cbRange = ~(RTGCUINTPTR)0;
3260 break;
3261 }
3262 if (Address.FlatPtr + cbRange < Address.FlatPtr)
3263 cbRange = ~(RTGCUINTPTR)0 - Address.FlatPtr + !!Address.FlatPtr;
3264
3265 /*
3266 * Ok, do it.
3267 */
3268 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, abBytes, cbBytes, cbUnit, cMaxHits, pResult);
3269}
3270
3271
3272/**
3273 * The 's' command.
3274 *
3275 * @returns VBox status.
3276 * @param pCmd Pointer to the command descriptor (as registered).
3277 * @param pCmdHlp Pointer to command helper functions.
3278 * @param pVM Pointer to the current VM (if any).
3279 * @param paArgs Pointer to (readonly) array of arguments.
3280 * @param cArgs Number of arguments in the array.
3281 */
3282static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3283{
3284 /* check that the parser did what it's supposed to do. */
3285 //if ( cArgs <= 2
3286 // && paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3287 // return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
3288
3289 /*
3290 * Repeate previous search?
3291 */
3292 if (cArgs == 0)
3293 return dbgcCmdWorkerSearchMemResume(pCmdHlp, pVM, pResult);
3294
3295 /*
3296 * Parse arguments.
3297 */
3298
3299 return -1;
3300}
3301
3302
3303/**
3304 * The 's?' command.
3305 *
3306 * @returns VBox status.
3307 * @param pCmd Pointer to the command descriptor (as registered).
3308 * @param pCmdHlp Pointer to command helper functions.
3309 * @param pVM Pointer to the current VM (if any).
3310 * @param paArgs Pointer to (readonly) array of arguments.
3311 * @param cArgs Number of arguments in the array.
3312 */
3313static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3314{
3315 /* check that the parser did what it's supposed to do. */
3316 if ( cArgs < 2
3317 || !DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
3318 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
3319 return dbgcCmdWorkerSearchMem(pCmdHlp, pVM, &paArgs[0], pResult ? 1 : 25, pCmd->pszCmd[1], paArgs + 1, cArgs - 1, pResult);
3320}
3321
3322
3323/**
3324 * List near symbol.
3325 *
3326 * @returns VBox status code.
3327 * @param pCmdHlp Pointer to command helper functions.
3328 * @param pVM Pointer to the current VM (if any).
3329 * @param pArg Pointer to the address or symbol to lookup.
3330 */
3331static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pArg, PDBGCVAR pResult)
3332{
3333 dbgcVarSetGCFlat(pResult, 0);
3334
3335 DBGFSYMBOL Symbol;
3336 int rc;
3337 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
3338 {
3339 /*
3340 * Lookup the symbol address.
3341 */
3342 rc = DBGFR3SymbolByName(pVM, pArg->u.pszString, &Symbol);
3343 if (VBOX_FAILURE(rc))
3344 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3SymbolByName(, %s,)\n", pArg->u.pszString);
3345
3346 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%VGv %s\n", (RTGCUINTPTR)Symbol.Value, Symbol.szName); /** @todo remove the RTUINGCPTR cast once DBGF got correct interfaces! */
3347 dbgcVarSetGCFlatByteRange(pResult, Symbol.Value, Symbol.cb);
3348 }
3349 else
3350 {
3351 /*
3352 * Convert it to a flat GC address and lookup that address.
3353 */
3354 DBGCVAR AddrVar;
3355 rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
3356 if (VBOX_FAILURE(rc))
3357 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
3358
3359 dbgcVarSetVar(pResult, &AddrVar);
3360
3361 RTGCINTPTR offDisp = 0;
3362 rc = DBGFR3SymbolByAddr(pVM, AddrVar.u.GCFlat, &offDisp, &Symbol);
3363 if (VBOX_FAILURE(rc))
3364 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3SymbolByAddr(, %VGv,,)\n", AddrVar.u.GCFlat);
3365
3366 if (!offDisp)
3367 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s", &AddrVar, Symbol.szName);
3368 else if (offDisp > 0)
3369 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
3370 else
3371 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
3372 if ((RTGCINTPTR)Symbol.cb > -offDisp)
3373 {
3374 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " LB %RGv\n", Symbol.cb + offDisp);
3375 dbgcVarSetByteRange(pResult, Symbol.cb + offDisp);
3376 }
3377 else
3378 {
3379 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
3380 dbgcVarSetNoRange(pResult);
3381 }
3382 }
3383
3384 return rc;
3385}
3386
3387
3388/**
3389 * The 'ln' (listnear) command.
3390 *
3391 * @returns VBox status.
3392 * @param pCmd Pointer to the command descriptor (as registered).
3393 * @param pCmdHlp Pointer to command helper functions.
3394 * @param pVM Pointer to the current VM (if any).
3395 * @param paArgs Pointer to (readonly) array of arguments.
3396 * @param cArgs Number of arguments in the array.
3397 */
3398static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3399{
3400 dbgcVarSetGCFlat(pResult, 0);
3401 if (!cArgs)
3402 {
3403 /*
3404 * Current cs:eip symbol.
3405 */
3406 DBGCVAR AddrVar;
3407 int rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(cs:eip)");
3408 if (VBOX_FAILURE(rc))
3409 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(cs:eip)\n");
3410 return dbgcDoListNear(pCmdHlp, pVM, &AddrVar, pResult);
3411 }
3412
3413 /*
3414 * Iterate arguments.
3415 */
3416 for (unsigned iArg = 0; iArg < cArgs; iArg++)
3417 {
3418 int rc = dbgcDoListNear(pCmdHlp, pVM, &paArgs[iArg], pResult);
3419 if (VBOX_FAILURE(rc))
3420 return rc;
3421 }
3422
3423 NOREF(pCmd); NOREF(pResult);
3424 return VINF_SUCCESS;
3425}
3426
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