VirtualBox

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

Last change on this file since 35696 was 35696, checked in by vboxsync, 14 years ago

PCDBGCCMD & PFNDBGCCMD: Drop the return type & variable. Functions will be added separately from commands (superset of DBGCCMD).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 180.0 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 35696 2011-01-24 18:03:33Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, CodeView / WinDbg Emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
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
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_DBGC
22#include <VBox/dbg.h>
23#include <VBox/vmm/dbgf.h>
24#include <VBox/vmm/pgm.h>
25#include <VBox/vmm/cpum.h>
26#include <VBox/dis.h>
27#include <VBox/param.h>
28#include <VBox/err.h>
29#include <VBox/log.h>
30
31#include <iprt/asm.h>
32#include <iprt/mem.h>
33#include <iprt/string.h>
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36
37#include <stdlib.h>
38#include <stdio.h>
39
40#include "DBGCInternal.h"
41
42
43/*******************************************************************************
44* Internal Functions *
45*******************************************************************************/
46static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
47static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
48static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
49static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
50static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
51static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
52static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
53static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
54static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
55static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
56static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
57static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
58static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
59static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
60static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
61static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
62static DECLCALLBACK(int) dbgcCmdEditMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
63static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
64static DECLCALLBACK(int) dbgcCmdListModules(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
65static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
66static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
67static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
68static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
69static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
70static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
71static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
72static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
73static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
74static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
75static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
76static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
77
78
79/*******************************************************************************
80* Global Variables *
81*******************************************************************************/
82/** 'ba' arguments. */
83static const DBGCVARDESC g_aArgBrkAcc[] =
84{
85 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
86 { 1, 1, DBGCVAR_CAT_STRING, 0, "access", "The access type: x=execute, rw=read/write (alias r), w=write, i=not implemented." },
87 { 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." },
88 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
89 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
90 { 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)" },
91 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
92};
93
94
95/** 'bc', 'bd', 'be' arguments. */
96static const DBGCVARDESC g_aArgBrks[] =
97{
98 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
99 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "#bp", "Breakpoint number." },
100 { 0, 1, DBGCVAR_CAT_STRING, 0, "all", "All breakpoints." },
101};
102
103
104/** 'bp' arguments. */
105static const DBGCVARDESC g_aArgBrkSet[] =
106{
107 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
108 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
109 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
110 { 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)" },
111 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
112};
113
114
115/** 'br' arguments. */
116static const DBGCVARDESC g_aArgBrkREM[] =
117{
118 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
119 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
120 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
121 { 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)" },
122 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
123};
124
125
126/** 'd?' arguments. */
127static const DBGCVARDESC g_aArgDumpMem[] =
128{
129 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
130 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start dumping memory." },
131};
132
133
134/** 'dg', 'dga', 'dl', 'dla' arguments. */
135static const DBGCVARDESC g_aArgDumpDT[] =
136{
137 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
138 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "sel", "Selector or selector range." },
139 { 0, ~0, DBGCVAR_CAT_POINTER, 0, "address", "Far address which selector should be dumped." },
140};
141
142
143/** 'di', 'dia' arguments. */
144static const DBGCVARDESC g_aArgDumpIDT[] =
145{
146 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
147 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "int", "The interrupt vector or interrupt vector range." },
148};
149
150
151/** 'dpd*' arguments. */
152static const DBGCVARDESC g_aArgDumpPD[] =
153{
154 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
155 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "index", "Index into the page directory." },
156 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from. Range is applied to the page directory." },
157};
158
159
160/** 'dpda' arguments. */
161static const DBGCVARDESC g_aArgDumpPDAddr[] =
162{
163 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
164 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page directory entry to start dumping from." },
165};
166
167
168/** 'dph*' arguments. */
169static const DBGCVARDESC g_aArgDumpPH[] =
170{
171 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
172 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "Where in the address space to start dumping and for how long (range). The default address/range will be used if omitted." },
173 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "cr3", "The CR3 value to use. The current CR3 of the context will be used if omitted." },
174 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "mode", "The paging mode: legacy, pse, pae, long, ept. Append '-np' for nested paging and '-nx' for no-execute. The current mode will be used if omitted." },
175};
176
177
178/** 'dpt?' arguments. */
179static const DBGCVARDESC g_aArgDumpPT[] =
180{
181 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
182 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from." },
183};
184
185
186/** 'dpta' arguments. */
187static const DBGCVARDESC g_aArgDumpPTAddr[] =
188{
189 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
190 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page table entry to start dumping from." },
191};
192
193
194/** 'dt' arguments. */
195static const DBGCVARDESC g_aArgDumpTSS[] =
196{
197 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
198 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "tss", "TSS selector number." },
199 { 0, 1, DBGCVAR_CAT_POINTER, 0, "tss:ign|addr", "TSS address. If the selector is a TSS selector, the offset will be ignored." }
200};
201
202
203/** 'e?' arguments. */
204static const DBGCVARDESC g_aArgEditMem[] =
205{
206 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
207 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to write." },
208 { 1, ~0, DBGCVAR_CAT_NUMBER, 0, "value", "Value to write." },
209};
210
211
212/** 'lm' arguments. */
213static const DBGCVARDESC g_aArgListMods[] =
214{
215 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
216 { 0, ~0, DBGCVAR_CAT_STRING, 0, "module", "Module name." },
217};
218
219
220/** 'ln' arguments. */
221static const DBGCVARDESC g_aArgListNear[] =
222{
223 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
224 { 0, ~0, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
225 { 0, ~0, DBGCVAR_CAT_SYMBOL, 0, "symbol", "Symbol to lookup." },
226};
227
228
229/** 'ls' arguments. */
230static const DBGCVARDESC g_aArgListSource[] =
231{
232 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
233 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start looking for source lines." },
234};
235
236
237/** 'm' argument. */
238static const DBGCVARDESC g_aArgMemoryInfo[] =
239{
240 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
241 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Pointer to obtain info about." },
242};
243
244
245/** 'r' arguments. */
246static const DBGCVARDESC g_aArgReg[] =
247{
248 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
249 { 0, 1, DBGCVAR_CAT_SYMBOL, 0, "register", "Register to show or set." },
250 { 0, 1, DBGCVAR_CAT_NUMBER_NO_RANGE, DBGCVD_FLAGS_DEP_PREV, "value", "New register value." },
251};
252
253
254/** 's' arguments. */
255static const DBGCVARDESC g_aArgSearchMem[] =
256{
257 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
258 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-b", "Byte string." },
259 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-w", "Word string." },
260 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-d", "DWord string." },
261 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-q", "QWord string." },
262 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-a", "ASCII string." },
263 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-u", "Unicode string." },
264 { 0, 1, DBGCVAR_CAT_OPTION_NUMBER, 0, "-n <Hits>", "Maximum number of hits." },
265 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
266 { 0, ~0, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
267};
268
269
270/** 's?' arguments. */
271static const DBGCVARDESC g_aArgSearchMemType[] =
272{
273 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
274 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
275 { 1, ~0, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
276};
277
278
279/** 'u' arguments. */
280static const DBGCVARDESC g_aArgUnassemble[] =
281{
282 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
283 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
284};
285
286
287/** Command descriptors for the CodeView / WinDbg emulation.
288 * The emulation isn't attempting to be identical, only somewhat similar.
289 */
290const DBGCCMD g_aCmdsCodeView[] =
291{
292 /* pszCmd, cArgsMin, cArgsMax, paArgDescs, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
293 { "ba", 3, 6, &g_aArgBrkAcc[0], RT_ELEMENTS(g_aArgBrkAcc), 0, dbgcCmdBrkAccess, "<access> <size> <address> [passes [max passes]] [cmds]",
294 "Sets a data access breakpoint." },
295 { "bc", 1, ~0, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Enabled a set of breakpoints." },
296 { "bd", 1, ~0, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
297 { "be", 1, ~0, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enabled a set of breakpoints." },
298 { "bl", 0, 0, NULL, 0, 0, dbgcCmdBrkList, "", "Lists all the breakpoints." },
299 { "bp", 1, 4, &g_aArgBrkSet[0], RT_ELEMENTS(g_aArgBrkSet), 0, dbgcCmdBrkSet, "<address> [passes [max passes]] [cmds]",
300 "Sets a breakpoint (int 3)." },
301 { "br", 1, 4, &g_aArgBrkREM[0], RT_ELEMENTS(g_aArgBrkREM), 0, dbgcCmdBrkREM, "<address> [passes [max passes]] [cmds]",
302 "Sets a recompiler specific breakpoint." },
303 { "d", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory using last element size." },
304 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
305 { "db", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in bytes." },
306 { "dd", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in double words." },
307 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
308 { "dg", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT)." },
309 { "dga", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT) including not-present entries." },
310 { "di", 0, ~0, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT)." },
311 { "dia", 0, ~0, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT) including not-present entries." },
312 { "dl", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT)." },
313 { "dla", 0, ~0, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT) including not-present entries." },
314 { "dpd", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the default context." },
315 { "dpda", 0, 1, &g_aArgDumpPDAddr[0],RT_ELEMENTS(g_aArgDumpPDAddr), 0, dbgcCmdDumpPageDir, "[addr]", "Dumps specified page directory." },
316 { "dpdb", 1, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDirBoth, "[addr] [index]", "Dumps page directory entries of the guest and the hypervisor. " },
317 { "dpdg", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the guest." },
318 { "dpdh", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the hypervisor. " },
319 { "dph", 0, 3, &g_aArgDumpPH[0], RT_ELEMENTS(g_aArgDumpPH), 0, dbgcCmdDumpPageHierarchy, "[addr [cr3 [mode]]", "Dumps the paging hierarchy at for specfied address range. Default context." },
320 { "dphg", 0, 3, &g_aArgDumpPH[0], RT_ELEMENTS(g_aArgDumpPH), 0, dbgcCmdDumpPageHierarchy, "[addr [cr3 [mode]]", "Dumps the paging hierarchy at for specfied address range. Guest context." },
321 { "dphh", 0, 3, &g_aArgDumpPH[0], RT_ELEMENTS(g_aArgDumpPH), 0, dbgcCmdDumpPageHierarchy, "[addr [cr3 [mode]]", "Dumps the paging hierarchy at for specfied address range. Hypervisor context." },
322 { "dpt", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the default context." },
323 { "dpta", 1, 1, &g_aArgDumpPTAddr[0],RT_ELEMENTS(g_aArgDumpPTAddr), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps specified page table." },
324 { "dptb", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTableBoth,"<addr>", "Dumps page table entries of the guest and the hypervisor." },
325 { "dptg", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the guest." },
326 { "dpth", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the hypervisor." },
327 { "dq", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in quad words." },
328 { "dt", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the task state segment (TSS)." },
329 { "dt16", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 16-bit task state segment (TSS)." },
330 { "dt32", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 32-bit task state segment (TSS)." },
331 { "dt64", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 64-bit task state segment (TSS)." },
332 { "dw", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in words." },
333 /** @todo add 'e', 'ea str', 'eza str', 'eu str' and 'ezu str'. See also
334 * dbgcCmdSearchMem and its dbgcVarsToBytes usage. */
335 { "eb", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 1-byte value to memory." },
336 { "ew", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 2-byte value to memory." },
337 { "ed", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 4-byte value to memory." },
338 { "eq", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 8-byte value to memory." },
339 { "g", 0, 0, NULL, 0, 0, dbgcCmdGo, "", "Continue execution." },
340 { "k", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack." },
341 { "kg", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - guest." },
342 { "kh", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - hypervisor." },
343 { "lm", 0, ~0, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules." },
344 { "lmo", 0, ~0, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments." },
345 { "ln", 0, ~0, &g_aArgListNear[0], RT_ELEMENTS(g_aArgListNear), 0, dbgcCmdListNear, "[addr/sym [..]]", "List symbols near to the address. Default address is CS:EIP." },
346 { "ls", 0, 1, &g_aArgListSource[0],RT_ELEMENTS(g_aArgListSource), 0, dbgcCmdListSource, "[addr]", "Source." },
347 { "m", 1, 1, &g_aArgMemoryInfo[0],RT_ELEMENTS(g_aArgMemoryInfo), 0, dbgcCmdMemoryInfo, "<addr>", "Display information about that piece of memory." },
348 { "r", 0, 2, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdReg, "[reg [newval]]", "Show or set register(s) - active reg set." },
349 { "rg", 0, 2, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegGuest, "[reg [newval]]", "Show or set register(s) - guest reg set." },
350 { "rg32", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 32-bit guest registers." },
351 { "rg64", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 64-bit guest registers." },
352 { "rh", 0, 2, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegHyper, "[reg [newval]]", "Show or set register(s) - hypervisor reg set." },
353 { "rt", 0, 0, NULL, 0, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
354 { "s", 0, ~0, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
355 { "sa", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
356 { "sb", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
357 { "sd", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
358 { "sq", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
359 { "su", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
360 { "sw", 2, ~0, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more words." },
361 { "t", 0, 0, NULL, 0, 0, dbgcCmdTrace, "", "Instruction trace (step into)." },
362 { "u", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble." },
363 { "u64", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 64-bit code." },
364 { "u32", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 32-bit code." },
365 { "u16", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code." },
366 { "uv86", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code with v8086/real mode addressing." },
367};
368
369/** The number of commands in the CodeView/WinDbg emulation. */
370const unsigned g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
371
372
373
374/**
375 * The 'go' command.
376 *
377 * @returns VBox status.
378 * @param pCmd Pointer to the command descriptor (as registered).
379 * @param pCmdHlp Pointer to command helper functions.
380 * @param pVM Pointer to the current VM (if any).
381 * @param paArgs Pointer to (readonly) array of arguments.
382 * @param cArgs Number of arguments in the array.
383 */
384static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
385{
386 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
387
388 /*
389 * Check if the VM is halted or not before trying to resume it.
390 */
391 if (!DBGFR3IsHalted(pVM))
392 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The VM is already running");
393
394 int rc = DBGFR3Resume(pVM);
395 if (RT_FAILURE(rc))
396 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3Resume");
397
398 NOREF(paArgs); NOREF(cArgs);
399 return VINF_SUCCESS;
400}
401
402
403/**
404 * The 'ba' command.
405 *
406 * @returns VBox status.
407 * @param pCmd Pointer to the command descriptor (as registered).
408 * @param pCmdHlp Pointer to command helper functions.
409 * @param pVM Pointer to the current VM (if any).
410 * @param paArgs Pointer to (readonly) array of arguments.
411 * @param cArgs Number of arguments in the array.
412 */
413static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
414{
415 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
416
417 /*
418 * Interpret access type.
419 */
420 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
421 || paArgs[0].u.pszString[1])
422 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'",
423 paArgs[0].u.pszString, pCmd->pszCmd);
424 uint8_t fType = 0;
425 switch (paArgs[0].u.pszString[0])
426 {
427 case 'x': fType = X86_DR7_RW_EO; break;
428 case 'r': fType = X86_DR7_RW_RW; break;
429 case 'w': fType = X86_DR7_RW_WO; break;
430 case 'i': fType = X86_DR7_RW_IO; break;
431 }
432
433 /*
434 * Validate size.
435 */
436 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
437 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 'x' access type requires size 1!",
438 paArgs[1].u.u64Number, pCmd->pszCmd);
439 switch (paArgs[1].u.u64Number)
440 {
441 case 1:
442 case 2:
443 case 4:
444 break;
445 /*case 8: - later*/
446 default:
447 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 1, 2 or 4!",
448 paArgs[1].u.u64Number, pCmd->pszCmd);
449 }
450 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
451
452 /*
453 * Convert the pointer to a DBGF address.
454 */
455 DBGFADDRESS Address;
456 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
457 if (RT_FAILURE(rc))
458 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%DV,)", &paArgs[2]);
459
460 /*
461 * Pick out the optional arguments.
462 */
463 uint64_t iHitTrigger = 0;
464 uint64_t iHitDisable = ~0;
465 const char *pszCmds = NULL;
466 unsigned iArg = 3;
467 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
468 {
469 iHitTrigger = paArgs[iArg].u.u64Number;
470 iArg++;
471 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
472 {
473 iHitDisable = paArgs[iArg].u.u64Number;
474 iArg++;
475 }
476 }
477 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
478 {
479 pszCmds = paArgs[iArg].u.pszString;
480 iArg++;
481 }
482
483 /*
484 * Try set the breakpoint.
485 */
486 uint32_t iBp;
487 rc = DBGFR3BpSetReg(pVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
488 if (RT_SUCCESS(rc))
489 {
490 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
491 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
492 if (RT_SUCCESS(rc))
493 return DBGCCmdHlpPrintf(pCmdHlp, "Set access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
494 if (rc == VERR_DBGC_BP_EXISTS)
495 {
496 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
497 if (RT_SUCCESS(rc))
498 return DBGCCmdHlpPrintf(pCmdHlp, "Updated access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
499 }
500 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
501 AssertRC(rc2);
502 }
503 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set access breakpoint at %RGv", Address.FlatPtr);
504}
505
506
507/**
508 * The 'bc' command.
509 *
510 * @returns VBox status.
511 * @param pCmd Pointer to the command descriptor (as registered).
512 * @param pCmdHlp Pointer to command helper functions.
513 * @param pVM Pointer to the current VM (if any).
514 * @param paArgs Pointer to (readonly) array of arguments.
515 * @param cArgs Number of arguments in the array.
516 */
517static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
518{
519 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
520
521 /*
522 * Enumerate the arguments.
523 */
524 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
525 int rc = VINF_SUCCESS;
526 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
527 {
528 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
529 {
530 /* one */
531 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
532 if (iBp == paArgs[iArg].u.u64Number)
533 {
534 int rc2 = DBGFR3BpClear(pVM, iBp);
535 if (RT_FAILURE(rc2))
536 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
537 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
538 dbgcBpDelete(pDbgc, iBp);
539 }
540 else
541 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
542 }
543 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
544 {
545 /* all */
546 PDBGCBP pBp = pDbgc->pFirstBp;
547 while (pBp)
548 {
549 uint32_t iBp = pBp->iBp;
550 pBp = pBp->pNext;
551
552 int rc2 = DBGFR3BpClear(pVM, iBp);
553 if (RT_FAILURE(rc2))
554 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
555 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
556 dbgcBpDelete(pDbgc, iBp);
557 }
558 }
559 else
560 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
561 }
562 return rc;
563}
564
565
566/**
567 * The 'bd' command.
568 *
569 * @returns VBox status.
570 * @param pCmd Pointer to the command descriptor (as registered).
571 * @param pCmdHlp Pointer to command helper functions.
572 * @param pVM Pointer to the current VM (if any).
573 * @param paArgs Pointer to (readonly) array of arguments.
574 * @param cArgs Number of arguments in the array.
575 */
576static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
577{
578 /*
579 * Enumerate the arguments.
580 */
581 int rc = VINF_SUCCESS;
582 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
583 {
584 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
585 {
586 /* one */
587 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
588 if (iBp == paArgs[iArg].u.u64Number)
589 {
590 rc = DBGFR3BpDisable(pVM, iBp);
591 if (RT_FAILURE(rc))
592 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpDisable failed for breakpoint %#x", iBp);
593 }
594 else
595 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
596 }
597 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
598 {
599 /* all */
600 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
601 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
602 {
603 int rc2 = DBGFR3BpDisable(pVM, pBp->iBp);
604 if (RT_FAILURE(rc2))
605 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpDisable failed for breakpoint %#x", pBp->iBp);
606 }
607 }
608 else
609 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
610 }
611 return rc;
612}
613
614
615/**
616 * The 'be' command.
617 *
618 * @returns VBox status.
619 * @param pCmd Pointer to the command descriptor (as registered).
620 * @param pCmdHlp Pointer to command helper functions.
621 * @param pVM Pointer to the current VM (if any).
622 * @param paArgs Pointer to (readonly) array of arguments.
623 * @param cArgs Number of arguments in the array.
624 */
625static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
626{
627 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
628
629 /*
630 * Enumerate the arguments.
631 */
632 int rc = VINF_SUCCESS;
633 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
634 {
635 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
636 {
637 /* one */
638 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
639 if (iBp == paArgs[iArg].u.u64Number)
640 {
641 rc = DBGFR3BpEnable(pVM, iBp);
642 if (RT_FAILURE(rc))
643 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnable failed for breakpoint %#x", iBp);
644 }
645 else
646 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
647 }
648 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
649 {
650 /* all */
651 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
652 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
653 {
654 int rc2 = DBGFR3BpEnable(pVM, pBp->iBp);
655 if (RT_FAILURE(rc2))
656 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpEnable failed for breakpoint %#x", pBp->iBp);
657 }
658 }
659 else
660 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
661 }
662 return rc;
663}
664
665
666/**
667 * Breakpoint enumeration callback function.
668 *
669 * @returns VBox status code. Any failure will stop the enumeration.
670 * @param pVM The VM handle.
671 * @param pvUser The user argument.
672 * @param pBp Pointer to the breakpoint information. (readonly)
673 */
674static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PVM pVM, void *pvUser, PCDBGFBP pBp)
675{
676 PDBGC pDbgc = (PDBGC)pvUser;
677 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
678
679 /*
680 * BP type and size.
681 */
682 char chType;
683 char cb = 1;
684 switch (pBp->enmType)
685 {
686 case DBGFBPTYPE_INT3:
687 chType = 'p';
688 break;
689 case DBGFBPTYPE_REG:
690 switch (pBp->u.Reg.fType)
691 {
692 case X86_DR7_RW_EO: chType = 'x'; break;
693 case X86_DR7_RW_WO: chType = 'w'; break;
694 case X86_DR7_RW_IO: chType = 'i'; break;
695 case X86_DR7_RW_RW: chType = 'r'; break;
696 default: chType = '?'; break;
697
698 }
699 cb = pBp->u.Reg.cb;
700 break;
701 case DBGFBPTYPE_REM:
702 chType = 'r';
703 break;
704 default:
705 chType = '?';
706 break;
707 }
708
709 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%4#x %c %d %c %RGv %04RX64 (%04RX64 to ",
710 pBp->iBp, pBp->fEnabled ? 'e' : 'd', cb, chType,
711 pBp->GCPtr, pBp->cHits, pBp->iHitTrigger);
712 if (pBp->iHitDisable == ~(uint64_t)0)
713 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "~0) ");
714 else
715 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%04RX64)");
716
717 /*
718 * Try resolve the address.
719 */
720 RTDBGSYMBOL Sym;
721 RTINTPTR off;
722 DBGFADDRESS Addr;
723 int rc = DBGFR3AsSymbolByAddr(pVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pVM, &Addr, pBp->GCPtr), &off, &Sym, NULL);
724 if (RT_SUCCESS(rc))
725 {
726 if (!off)
727 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s", Sym.szName);
728 else if (off > 0)
729 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, off);
730 else
731 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, -off);
732 }
733
734 /*
735 * The commands.
736 */
737 if (pDbgcBp)
738 {
739 if (pDbgcBp->cchCmd)
740 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n cmds: '%s'\n", pDbgcBp->szCmd);
741 else
742 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n");
743 }
744 else
745 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " [unknown bp]\n");
746
747 return VINF_SUCCESS;
748}
749
750
751/**
752 * The 'bl' command.
753 *
754 * @returns VBox status.
755 * @param pCmd Pointer to the command descriptor (as registered).
756 * @param pCmdHlp Pointer to command helper functions.
757 * @param pVM Pointer to the current VM (if any).
758 * @param paArgs Pointer to (readonly) array of arguments.
759 * @param cArgs Number of arguments in the array.
760 */
761static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR /*paArgs*/, unsigned cArgs)
762{
763 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
764 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
765
766 /*
767 * Enumerate the breakpoints.
768 */
769 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
770 int rc = DBGFR3BpEnum(pVM, dbgcEnumBreakpointsCallback, pDbgc);
771 if (RT_FAILURE(rc))
772 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnum");
773 return rc;
774}
775
776
777/**
778 * The 'bp' command.
779 *
780 * @returns VBox status.
781 * @param pCmd Pointer to the command descriptor (as registered).
782 * @param pCmdHlp Pointer to command helper functions.
783 * @param pVM Pointer to the current VM (if any).
784 * @param paArgs Pointer to (readonly) array of arguments.
785 * @param cArgs Number of arguments in the array.
786 */
787static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
788{
789 /*
790 * Convert the pointer to a DBGF address.
791 */
792 DBGFADDRESS Address;
793 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
794 if (RT_FAILURE(rc))
795 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
796
797 /*
798 * Pick out the optional arguments.
799 */
800 uint64_t iHitTrigger = 0;
801 uint64_t iHitDisable = ~0;
802 const char *pszCmds = NULL;
803 unsigned iArg = 1;
804 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
805 {
806 iHitTrigger = paArgs[iArg].u.u64Number;
807 iArg++;
808 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
809 {
810 iHitDisable = paArgs[iArg].u.u64Number;
811 iArg++;
812 }
813 }
814 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
815 {
816 pszCmds = paArgs[iArg].u.pszString;
817 iArg++;
818 }
819
820 /*
821 * Try set the breakpoint.
822 */
823 uint32_t iBp;
824 rc = DBGFR3BpSet(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
825 if (RT_SUCCESS(rc))
826 {
827 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
828 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
829 if (RT_SUCCESS(rc))
830 return DBGCCmdHlpPrintf(pCmdHlp, "Set breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
831 if (rc == VERR_DBGC_BP_EXISTS)
832 {
833 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
834 if (RT_SUCCESS(rc))
835 return DBGCCmdHlpPrintf(pCmdHlp, "Updated breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
836 }
837 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
838 AssertRC(rc2);
839 }
840 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set breakpoint at %RGv", Address.FlatPtr);
841}
842
843
844/**
845 * The 'br' command.
846 *
847 * @returns VBox status.
848 * @param pCmd Pointer to the command descriptor (as registered).
849 * @param pCmdHlp Pointer to command helper functions.
850 * @param pVM Pointer to the current VM (if any).
851 * @param paArgs Pointer to (readonly) array of arguments.
852 * @param cArgs Number of arguments in the array.
853 */
854static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
855{
856 /*
857 * Convert the pointer to a DBGF address.
858 */
859 DBGFADDRESS Address;
860 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
861 if (RT_FAILURE(rc))
862 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
863
864 /*
865 * Pick out the optional arguments.
866 */
867 uint64_t iHitTrigger = 0;
868 uint64_t iHitDisable = ~0;
869 const char *pszCmds = NULL;
870 unsigned iArg = 1;
871 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
872 {
873 iHitTrigger = paArgs[iArg].u.u64Number;
874 iArg++;
875 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
876 {
877 iHitDisable = paArgs[iArg].u.u64Number;
878 iArg++;
879 }
880 }
881 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
882 {
883 pszCmds = paArgs[iArg].u.pszString;
884 iArg++;
885 }
886
887 /*
888 * Try set the breakpoint.
889 */
890 uint32_t iBp;
891 rc = DBGFR3BpSetREM(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
892 if (RT_SUCCESS(rc))
893 {
894 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
895 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
896 if (RT_SUCCESS(rc))
897 return DBGCCmdHlpPrintf(pCmdHlp, "Set REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
898 if (rc == VERR_DBGC_BP_EXISTS)
899 {
900 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
901 if (RT_SUCCESS(rc))
902 return DBGCCmdHlpPrintf(pCmdHlp, "Updated REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
903 }
904 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
905 AssertRC(rc2);
906 }
907 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set REM breakpoint at %RGv", Address.FlatPtr);
908}
909
910
911/**
912 * The 'u' command.
913 *
914 * @returns VBox status.
915 * @param pCmd Pointer to the command descriptor (as registered).
916 * @param pCmdHlp Pointer to command helper functions.
917 * @param pVM Pointer to the current VM (if any).
918 * @param paArgs Pointer to (readonly) array of arguments.
919 * @param cArgs Number of arguments in the array.
920 */
921static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
922{
923 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
924
925 /*
926 * Validate input.
927 */
928 if ( cArgs > 1
929 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
930 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
931 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
932 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
933 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
934 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
935
936 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS;
937
938 /*
939 * Check the desired mode.
940 */
941 switch (pCmd->pszCmd[1])
942 {
943 default: AssertFailed();
944 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
945 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
946 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
947 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
948 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
949 }
950
951 /*
952 * Find address.
953 */
954 if (!cArgs)
955 {
956 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
957 {
958 PVMCPU pVCpu = VMMGetCpuById(pVM, pDbgc->idCpu);
959 if ( pDbgc->fRegCtxGuest
960 && CPUMIsGuestIn64BitCodeEx(CPUMQueryGuestCtxPtr(pVCpu)))
961 {
962 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
963 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
964 }
965 else
966 {
967 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
968 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
969 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
970 }
971
972 if (pDbgc->fRegCtxGuest)
973 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
974 else
975 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER;
976 }
977 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
978 }
979 else
980 pDbgc->DisasmPos = paArgs[0];
981 pDbgc->pLastPos = &pDbgc->DisasmPos;
982
983 /*
984 * Range.
985 */
986 switch (pDbgc->DisasmPos.enmRangeType)
987 {
988 case DBGCVAR_RANGE_NONE:
989 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
990 pDbgc->DisasmPos.u64Range = 10;
991 break;
992
993 case DBGCVAR_RANGE_ELEMENTS:
994 if (pDbgc->DisasmPos.u64Range > 2048)
995 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
996 break;
997
998 case DBGCVAR_RANGE_BYTES:
999 if (pDbgc->DisasmPos.u64Range > 65536)
1000 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
1001 break;
1002
1003 default:
1004 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DisasmPos.enmRangeType);
1005 }
1006
1007 /*
1008 * Convert physical and host addresses to guest addresses.
1009 */
1010 int rc;
1011 switch (pDbgc->DisasmPos.enmType)
1012 {
1013 case DBGCVAR_TYPE_GC_FLAT:
1014 case DBGCVAR_TYPE_GC_FAR:
1015 break;
1016 case DBGCVAR_TYPE_GC_PHYS:
1017 case DBGCVAR_TYPE_HC_FLAT:
1018 case DBGCVAR_TYPE_HC_PHYS:
1019 {
1020 DBGCVAR VarTmp;
1021 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1022 if (RT_FAILURE(rc))
1023 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: failed to evaluate '%%(%Dv)' -> %Rrc .\n", &pDbgc->DisasmPos, rc);
1024 pDbgc->DisasmPos = VarTmp;
1025 break;
1026 }
1027 default: AssertFailed(); break;
1028 }
1029
1030 /*
1031 * Print address.
1032 * todo: Change to list near.
1033 */
1034#if 0
1035 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DisasmPos);
1036 if (RT_FAILURE(rc))
1037 return rc;
1038#endif
1039
1040 /*
1041 * Do the disassembling.
1042 */
1043 unsigned cTries = 32;
1044 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1045 if (iRangeLeft == 0) /* kludge for 'r'. */
1046 iRangeLeft = -1;
1047 for (;;)
1048 {
1049 /*
1050 * Disassemble the instruction.
1051 */
1052 char szDis[256];
1053 uint32_t cbInstr = 1;
1054 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1055 rc = DBGFR3DisasInstrEx(pVM, pDbgc->idCpu, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags,
1056 &szDis[0], sizeof(szDis), &cbInstr);
1057 else
1058 rc = DBGFR3DisasInstrEx(pVM, pDbgc->idCpu, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags,
1059 &szDis[0], sizeof(szDis), &cbInstr);
1060 if (RT_SUCCESS(rc))
1061 {
1062 /* print it */
1063 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1064 if (RT_FAILURE(rc))
1065 return rc;
1066 }
1067 else
1068 {
1069 /* bitch. */
1070 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to disassemble instruction, skipping one byte.\n");
1071 if (RT_FAILURE(rc))
1072 return rc;
1073 if (cTries-- > 0)
1074 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Too many disassembly failures. Giving up.\n");
1075 cbInstr = 1;
1076 }
1077
1078 /* advance */
1079 if (iRangeLeft < 0) /* 'r' */
1080 break;
1081 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1082 iRangeLeft--;
1083 else
1084 iRangeLeft -= cbInstr;
1085 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1086 if (RT_FAILURE(rc))
1087 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DisasmPos, cbInstr);
1088 if (iRangeLeft <= 0)
1089 break;
1090 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1091 }
1092
1093 NOREF(pCmd);
1094 return 0;
1095}
1096
1097
1098/**
1099 * The 'ls' command.
1100 *
1101 * @returns VBox status.
1102 * @param pCmd Pointer to the command descriptor (as registered).
1103 * @param pCmdHlp Pointer to command helper functions.
1104 * @param pVM Pointer to the current VM (if any).
1105 * @param paArgs Pointer to (readonly) array of arguments.
1106 * @param cArgs Number of arguments in the array.
1107 */
1108static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1109{
1110 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1111
1112 /*
1113 * Validate input.
1114 */
1115 if ( cArgs > 1
1116 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
1117 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
1118 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1119 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
1120 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1121 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
1122
1123 /*
1124 * Find address.
1125 */
1126 if (!cArgs)
1127 {
1128 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1129 {
1130 PVMCPU pVCpu = VMMGetCpuById(pVM, pDbgc->idCpu);
1131 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
1132 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1133 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1134 }
1135 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
1136 }
1137 else
1138 pDbgc->SourcePos = paArgs[0];
1139 pDbgc->pLastPos = &pDbgc->SourcePos;
1140
1141 /*
1142 * Ensure the source address is flat GC.
1143 */
1144 switch (pDbgc->SourcePos.enmType)
1145 {
1146 case DBGCVAR_TYPE_GC_FLAT:
1147 break;
1148 case DBGCVAR_TYPE_GC_PHYS:
1149 case DBGCVAR_TYPE_GC_FAR:
1150 case DBGCVAR_TYPE_HC_FLAT:
1151 case DBGCVAR_TYPE_HC_PHYS:
1152 {
1153 int rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
1154 if (RT_FAILURE(rc))
1155 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid address or address type. (rc=%d)\n", rc);
1156 break;
1157 }
1158 default: AssertFailed(); break;
1159 }
1160
1161 /*
1162 * Range.
1163 */
1164 switch (pDbgc->SourcePos.enmRangeType)
1165 {
1166 case DBGCVAR_RANGE_NONE:
1167 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1168 pDbgc->SourcePos.u64Range = 10;
1169 break;
1170
1171 case DBGCVAR_RANGE_ELEMENTS:
1172 if (pDbgc->SourcePos.u64Range > 2048)
1173 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
1174 break;
1175
1176 case DBGCVAR_RANGE_BYTES:
1177 if (pDbgc->SourcePos.u64Range > 65536)
1178 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
1179 break;
1180
1181 default:
1182 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
1183 }
1184
1185 /*
1186 * Do the disassembling.
1187 */
1188 bool fFirst = 1;
1189 DBGFLINE LinePrev = { 0, 0, "" };
1190 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
1191 if (iRangeLeft == 0) /* kludge for 'r'. */
1192 iRangeLeft = -1;
1193 for (;;)
1194 {
1195 /*
1196 * Get line info.
1197 */
1198 DBGFLINE Line;
1199 RTGCINTPTR off;
1200 int rc = DBGFR3LineByAddr(pVM, pDbgc->SourcePos.u.GCFlat, &off, &Line);
1201 if (RT_FAILURE(rc))
1202 return VINF_SUCCESS;
1203
1204 unsigned cLines = 0;
1205 if (memcmp(&Line, &LinePrev, sizeof(Line)))
1206 {
1207 /*
1208 * Print filenamename
1209 */
1210 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
1211 fFirst = true;
1212 if (fFirst)
1213 {
1214 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
1215 if (RT_FAILURE(rc))
1216 return rc;
1217 }
1218
1219 /*
1220 * Try open the file and read the line.
1221 */
1222 FILE *phFile = fopen(Line.szFilename, "r");
1223 if (phFile)
1224 {
1225 /* Skip ahead to the desired line. */
1226 char szLine[4096];
1227 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
1228 if (cBefore > 7)
1229 cBefore = 0;
1230 unsigned cLeft = Line.uLineNo - cBefore;
1231 while (cLeft > 0)
1232 {
1233 szLine[0] = '\0';
1234 if (!fgets(szLine, sizeof(szLine), phFile))
1235 break;
1236 cLeft--;
1237 }
1238 if (!cLeft)
1239 {
1240 /* print the before lines */
1241 for (;;)
1242 {
1243 size_t cch = strlen(szLine);
1244 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || RT_C_IS_SPACE(szLine[cch - 1])) )
1245 szLine[--cch] = '\0';
1246 if (cBefore-- <= 0)
1247 break;
1248
1249 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
1250 szLine[0] = '\0';
1251 fgets(szLine, sizeof(szLine), phFile);
1252 cLines++;
1253 }
1254 /* print the actual line */
1255 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
1256 }
1257 fclose(phFile);
1258 if (RT_FAILURE(rc))
1259 return rc;
1260 fFirst = false;
1261 }
1262 else
1263 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Warning: couldn't open source file '%s'\n", Line.szFilename);
1264
1265 LinePrev = Line;
1266 }
1267
1268
1269 /*
1270 * Advance
1271 */
1272 if (iRangeLeft < 0) /* 'r' */
1273 break;
1274 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1275 iRangeLeft -= cLines;
1276 else
1277 iRangeLeft -= 1;
1278 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
1279 if (RT_FAILURE(rc))
1280 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
1281 if (iRangeLeft <= 0)
1282 break;
1283 }
1284
1285 NOREF(pCmd);
1286 return 0;
1287}
1288
1289
1290/**
1291 * The 'r' command.
1292 *
1293 * @returns VBox status.
1294 * @param pCmd Pointer to the command descriptor (as registered).
1295 * @param pCmdHlp Pointer to command helper functions.
1296 * @param pVM Pointer to the current VM (if any).
1297 * @param paArgs Pointer to (readonly) array of arguments.
1298 * @param cArgs Number of arguments in the array.
1299 */
1300static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1301{
1302 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1303 if (!pDbgc->fRegCtxGuest)
1304 return dbgcCmdRegHyper(pCmd, pCmdHlp, pVM, paArgs, cArgs);
1305 return dbgcCmdRegGuest(pCmd, pCmdHlp, pVM, paArgs, cArgs);
1306}
1307
1308
1309/**
1310 * Common worker for the dbgcCmdReg*() commands.
1311 *
1312 * @returns VBox status.
1313 * @param pCmd Pointer to the command descriptor (as registered).
1314 * @param pCmdHlp Pointer to command helper functions.
1315 * @param pVM Pointer to the current VM (if any).
1316 * @param paArgs Pointer to (readonly) array of arguments.
1317 * @param cArgs Number of arguments in the array.
1318 * @param pszPrefix The symbol prefix.
1319 */
1320static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs,
1321 const char *pszPrefix)
1322{
1323 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1324 Assert(cArgs == 1 || cArgs == 2); /* cArgs == 0 is handled by the caller */
1325 if ( paArgs[0].enmType != DBGCVAR_TYPE_STRING
1326 && paArgs[0].enmType != DBGCVAR_TYPE_SYMBOL)
1327 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: The parser doesn't do its job properly yet.. Try drop the '@' or/and quote the register name\n");
1328
1329 /*
1330 * Parse the register name and kind.
1331 */
1332 const char *pszReg = paArgs[0].u.pszString;
1333 if (*pszReg == '@')
1334 pszReg++;
1335 VMCPUID idCpu = pDbgc->idCpu;
1336 if (*pszPrefix)
1337 idCpu |= DBGFREG_HYPER_VMCPUID;
1338 if (*pszReg == '.')
1339 {
1340 pszReg++;
1341 idCpu |= DBGFREG_HYPER_VMCPUID;
1342 }
1343 const char * const pszActualPrefix = idCpu & DBGFREG_HYPER_VMCPUID ? "." : "";
1344
1345 /*
1346 * Query the register type & value (the setter needs the type).
1347 */
1348 DBGFREGVALTYPE enmType;
1349 DBGFREGVAL Value;
1350 int rc = DBGFR3RegNmQuery(pVM, idCpu, pszReg, &Value, &enmType);
1351 if (RT_FAILURE(rc))
1352 {
1353 if (rc == VERR_DBGF_REGISTER_NOT_FOUND)
1354 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown register: '%s%s'.\n",
1355 pszActualPrefix, pszReg);
1356 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmQuery failed querying '%s%s': %Rrc.\n",
1357 pszActualPrefix, pszReg, rc);
1358 }
1359 if (cArgs == 1)
1360 {
1361 /*
1362 * Show the register.
1363 */
1364 char szValue[160];
1365 rc = DBGFR3RegFormatValue(szValue, sizeof(szValue), &Value, enmType, true /*fSpecial*/);
1366 if (RT_SUCCESS(rc))
1367 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s%s=%s\n", pszActualPrefix, pszReg, szValue);
1368 else
1369 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
1370 }
1371 else if (cArgs == 2)
1372 {
1373 /*
1374 * Modify the register.
1375 */
1376 if ( paArgs[1].enmType == DBGCVAR_TYPE_STRING
1377 || paArgs[1].enmType == DBGCVAR_TYPE_SYMBOL)
1378 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: The parser doesn't do its job properly on the 2nd argument yet...\n");
1379 if (enmType != DBGFREGVALTYPE_DTR)
1380 {
1381 enmType = DBGFREGVALTYPE_U64;
1382 rc = DBGCCmdHlpVarToNumber(pCmdHlp, &paArgs[1], &Value.u64);
1383 }
1384 else
1385 {
1386 enmType = DBGFREGVALTYPE_DTR;
1387 rc = DBGCCmdHlpVarToNumber(pCmdHlp, &paArgs[1], &Value.dtr.u64Base);
1388 if (RT_SUCCESS(rc) && paArgs[1].enmRangeType != DBGCVAR_RANGE_NONE)
1389 Value.dtr.u32Limit = (uint32_t)paArgs[1].u64Range;
1390 }
1391 if (RT_SUCCESS(rc))
1392 {
1393 rc = DBGFR3RegNmSet(pVM, idCpu, pszReg, &Value, enmType);
1394 if (RT_FAILURE(rc))
1395 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmSet failed settings '%s%s': %Rrc\n",
1396 pszActualPrefix, pszReg, rc);
1397 }
1398 else
1399 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
1400 }
1401 else
1402 {
1403 NOREF(pCmd); NOREF(paArgs);
1404 rc = DBGCCmdHlpPrintf(pCmdHlp, "Huh? cArgs=%d Expected 0, 1 or 2!\n", cArgs);
1405 }
1406 return rc;
1407}
1408
1409
1410/**
1411 * The 'rg', 'rg64' and 'rg32' commands.
1412 *
1413 * @returns VBox status.
1414 * @param pCmd Pointer to the command descriptor (as registered).
1415 * @param pCmdHlp Pointer to command helper functions.
1416 * @param pVM Pointer to the current VM (if any).
1417 * @param paArgs Pointer to (readonly) array of arguments.
1418 * @param cArgs Number of arguments in the array.
1419 */
1420static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1421{
1422 /*
1423 * Show all registers our selves.
1424 */
1425 if (cArgs == 0)
1426 {
1427 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1428 bool const f64BitMode = !strcmp(pCmd->pszCmd, "rg64")
1429 || ( !strcmp(pCmd->pszCmd, "rg32")
1430 && CPUMIsGuestIn64BitCodeEx(CPUMQueryGuestCtxPtr(VMMGetCpuById(pVM, pDbgc->idCpu))));
1431 char szDisAndRegs[8192];
1432 int rc;
1433
1434 if (pDbgc->fRegTerse)
1435 {
1436 if (f64BitMode)
1437 rc = DBGFR3RegPrintf(pVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
1438 "u %016VR{rip} L 0\n"
1439 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
1440 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
1441 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
1442 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
1443 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
1444 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} rflags=%08VR{rflags}\n");
1445 else
1446 rc = DBGFR3RegPrintf(pVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
1447 "u %04VR{cs}:%08VR{eip} L 0\n"
1448 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
1449 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
1450 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} eflags=%08VR{eflags}\n");
1451 }
1452 else
1453 {
1454 if (f64BitMode)
1455 rc = DBGFR3RegPrintf(pVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
1456 "u %016VR{rip} L 0\n"
1457 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
1458 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
1459 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
1460 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
1461 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
1462 "cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
1463 "ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
1464 "es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
1465 "fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
1466 "gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
1467 "ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
1468 "dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
1469 "dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
1470 "gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
1471 "ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
1472 "tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
1473 " sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
1474 " efer=%016VR{efer}\n"
1475 " pat=%016VR{pat}\n"
1476 " sf_mask=%016VR{sf_mask}\n"
1477 "krnl_gs_base=%016VR{krnl_gs_base}\n"
1478 " lstar=%016VR{lstar}\n"
1479 " star=%016VR{star} cstar=%016VR{cstar}\n"
1480 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
1481 );
1482 else
1483 rc = DBGFR3RegPrintf(pVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
1484 "u %04VR{cs}:%08VR{eip} L 0\n"
1485 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
1486 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
1487 "cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} dr0=%08VR{dr0} dr1=%08VR{dr1}\n"
1488 "ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} dr2=%08VR{dr2} dr3=%08VR{dr3}\n"
1489 "es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} dr6=%08VR{dr6} dr6=%08VR{dr6}\n"
1490 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr0}\n"
1491 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr0} cr4=%08VR{cr0}\n"
1492 "ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}} cr8=%08VR{cr8}\n"
1493 "gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} idtr=%08VR{idtr_base}:%04VR{idtr_lim} eflags=%08VR{eflags}\n"
1494 "ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
1495 "tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
1496 "sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
1497 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
1498 );
1499 }
1500 if (RT_FAILURE(rc))
1501 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
1502 char *pszRegs = strchr(szDisAndRegs, '\n');
1503 *pszRegs++ = '\0';
1504 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
1505
1506 /*
1507 * Disassemble one instruction at cs:[r|e]ip.
1508 */
1509 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
1510 }
1511 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, "");
1512}
1513
1514
1515/**
1516 * The 'rh' command.
1517 *
1518 * @returns VBox status.
1519 * @param pCmd Pointer to the command descriptor (as registered).
1520 * @param pCmdHlp Pointer to command helper functions.
1521 * @param pVM Pointer to the current VM (if any).
1522 * @param paArgs Pointer to (readonly) array of arguments.
1523 * @param cArgs Number of arguments in the array.
1524 */
1525static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1526{
1527 /*
1528 * Show all registers our selves.
1529 */
1530 if (cArgs == 0)
1531 {
1532 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1533 char szDisAndRegs[8192];
1534 int rc;
1535
1536 if (pDbgc->fRegTerse)
1537 rc = DBGFR3RegPrintf(pVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
1538 "u %VR{cs}:%VR{eip} L 0\n"
1539 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
1540 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
1541 ".cs=%04VR{cs} .ds=%04VR{ds} .es=%04VR{es} .fs=%04VR{fs} .gs=%04VR{gs} .ss=%04VR{ss} .eflags=%08VR{eflags}\n");
1542 else
1543 rc = DBGFR3RegPrintf(pVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
1544 "u %04VR{cs}:%08VR{eip} L 0\n"
1545 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
1546 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
1547 ".cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} .dr0=%08VR{dr0} .dr1=%08VR{dr1}\n"
1548 ".ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} .dr2=%08VR{dr2} .dr3=%08VR{dr3}\n"
1549 ".es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} .dr6=%08VR{dr6} .dr6=%08VR{dr6}\n"
1550 ".fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} .cr3=%016VR{cr3}\n"
1551 ".gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}}\n"
1552 ".ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
1553 ".gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} .idtr=%08VR{idtr_base}:%04VR{idtr_lim} .eflags=%08VR{eflags}\n"
1554 ".ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
1555 ".tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
1556 );
1557 if (RT_FAILURE(rc))
1558 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
1559 char *pszRegs = strchr(szDisAndRegs, '\n');
1560 *pszRegs++ = '\0';
1561 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
1562
1563 /*
1564 * Disassemble one instruction at cs:[r|e]ip.
1565 */
1566 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
1567 }
1568 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, ".");
1569}
1570
1571
1572/**
1573 * The 'rt' command.
1574 *
1575 * @returns VBox status.
1576 * @param pCmd Pointer to the command descriptor (as registered).
1577 * @param pCmdHlp Pointer to command helper functions.
1578 * @param pVM Pointer to the current VM (if any).
1579 * @param paArgs Pointer to (readonly) array of arguments.
1580 * @param cArgs Number of arguments in the array.
1581 */
1582static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1583{
1584 NOREF(pCmd); NOREF(pVM); NOREF(paArgs); NOREF(cArgs);
1585
1586 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1587 pDbgc->fRegTerse = !pDbgc->fRegTerse;
1588 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
1589}
1590
1591
1592/**
1593 * The 't' command.
1594 *
1595 * @returns VBox status.
1596 * @param pCmd Pointer to the command descriptor (as registered).
1597 * @param pCmdHlp Pointer to command helper functions.
1598 * @param pVM Pointer to the current VM (if any).
1599 * @param paArgs Pointer to (readonly) array of arguments.
1600 * @param cArgs Number of arguments in the array.
1601 */
1602static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1603{
1604 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1605
1606 int rc = DBGFR3Step(pVM, pDbgc->idCpu);
1607 if (RT_SUCCESS(rc))
1608 pDbgc->fReady = false;
1609 else
1610 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
1611
1612 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
1613 return rc;
1614}
1615
1616
1617/**
1618 * The 'k', 'kg' and 'kh' commands.
1619 *
1620 * @returns VBox status.
1621 * @param pCmd Pointer to the command descriptor (as registered).
1622 * @param pCmdHlp Pointer to command helper functions.
1623 * @param pVM Pointer to the current VM (if any).
1624 * @param paArgs Pointer to (readonly) array of arguments.
1625 * @param cArgs Number of arguments in the array.
1626 */
1627static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1628{
1629 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1630
1631 /*
1632 * Figure which context we're called for and start walking that stack.
1633 */
1634 int rc;
1635 PCDBGFSTACKFRAME pFirstFrame;
1636 bool const fGuest = pCmd->pszCmd[1] == 'g'
1637 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
1638 rc = DBGFR3StackWalkBegin(pVM, pDbgc->idCpu, fGuest ? DBGFCODETYPE_GUEST : DBGFCODETYPE_HYPER, &pFirstFrame);
1639 if (RT_FAILURE(rc))
1640 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to begin stack walk, rc=%Rrc\n", rc);
1641
1642 /*
1643 * Print header.
1644 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
1645 */
1646 uint32_t fBitFlags = 0;
1647 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
1648 pFrame;
1649 pFrame = DBGFR3StackWalkNext(pFrame))
1650 {
1651 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
1652 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
1653 {
1654 if (fCurBitFlags != fBitFlags)
1655 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1656 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1657 pFrame->AddrFrame.Sel,
1658 (uint16_t)pFrame->AddrFrame.off,
1659 pFrame->AddrReturnFrame.Sel,
1660 (uint16_t)pFrame->AddrReturnFrame.off,
1661 (uint32_t)pFrame->AddrReturnPC.Sel,
1662 (uint32_t)pFrame->AddrReturnPC.off,
1663 pFrame->Args.au32[0],
1664 pFrame->Args.au32[1],
1665 pFrame->Args.au32[2],
1666 pFrame->Args.au32[3]);
1667 }
1668 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
1669 {
1670 if (fCurBitFlags != fBitFlags)
1671 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1672 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1673 (uint32_t)pFrame->AddrFrame.off,
1674 (uint32_t)pFrame->AddrReturnFrame.off,
1675 (uint32_t)pFrame->AddrReturnPC.Sel,
1676 (uint32_t)pFrame->AddrReturnPC.off,
1677 pFrame->Args.au32[0],
1678 pFrame->Args.au32[1],
1679 pFrame->Args.au32[2],
1680 pFrame->Args.au32[3]);
1681 }
1682 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
1683 {
1684 if (fCurBitFlags != fBitFlags)
1685 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
1686 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%016RX64 %04RX16:%016RX64 %016RX64",
1687 (uint64_t)pFrame->AddrFrame.off,
1688 pFrame->AddrReturnFrame.Sel,
1689 (uint64_t)pFrame->AddrReturnFrame.off,
1690 (uint64_t)pFrame->AddrReturnPC.off);
1691 }
1692 if (RT_FAILURE(rc))
1693 break;
1694 if (!pFrame->pSymPC)
1695 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1696 fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
1697 ? " %RTsel:%016RGv"
1698 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
1699 ? " %RTsel:%08RGv"
1700 : " %RTsel:%04RGv"
1701 , pFrame->AddrPC.Sel, pFrame->AddrPC.off);
1702 else
1703 {
1704 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
1705 if (offDisp > 0)
1706 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
1707 else if (offDisp < 0)
1708 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
1709 else
1710 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s", pFrame->pSymPC->szName);
1711 }
1712 if (RT_SUCCESS(rc) && pFrame->pLinePC)
1713 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
1714 if (RT_SUCCESS(rc))
1715 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
1716 if (RT_FAILURE(rc))
1717 break;
1718
1719 fBitFlags = fCurBitFlags;
1720 }
1721
1722 DBGFR3StackWalkEnd(pFirstFrame);
1723
1724 NOREF(paArgs); NOREF(cArgs);
1725 return rc;
1726}
1727
1728
1729static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP pCmdHlp, PCX86DESC64 pDesc, unsigned iEntry, bool fHyper, bool *pfDblEntry)
1730{
1731 /* GUEST64 */
1732 int rc;
1733
1734 const char *pszHyper = fHyper ? " HYPER" : "";
1735 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
1736 if (pDesc->Gen.u1DescType)
1737 {
1738 static const char * const s_apszTypes[] =
1739 {
1740 "DataRO", /* 0 Read-Only */
1741 "DataRO", /* 1 Read-Only - Accessed */
1742 "DataRW", /* 2 Read/Write */
1743 "DataRW", /* 3 Read/Write - Accessed */
1744 "DownRO", /* 4 Expand-down, Read-Only */
1745 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
1746 "DownRW", /* 6 Expand-down, Read/Write */
1747 "DownRO", /* 7 Expand-down, Read/Write - Accessed */
1748 "CodeEO", /* 8 Execute-Only */
1749 "CodeEO", /* 9 Execute-Only - Accessed */
1750 "CodeER", /* A Execute/Readable */
1751 "CodeER", /* B Execute/Readable - Accessed */
1752 "ConfE0", /* C Conforming, Execute-Only */
1753 "ConfE0", /* D Conforming, Execute-Only - Accessed */
1754 "ConfER", /* E Conforming, Execute/Readable */
1755 "ConfER" /* F Conforming, Execute/Readable - Accessed */
1756 };
1757 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
1758 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1759 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1760 uint32_t u32Base = X86DESC_BASE(*pDesc);
1761 uint32_t cbLimit = X86DESC_LIMIT(*pDesc);
1762 if (pDesc->Gen.u1Granularity)
1763 cbLimit <<= PAGE_SHIFT;
1764
1765 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
1766 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1767 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
1768 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
1769 }
1770 else
1771 {
1772 static const char * const s_apszTypes[] =
1773 {
1774 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
1775 "Ill-1 ", /* 1 0001 Available 16-bit TSS */
1776 "LDT ", /* 2 0010 LDT */
1777 "Ill-3 ", /* 3 0011 Busy 16-bit TSS */
1778 "Ill-4 ", /* 4 0100 16-bit Call Gate */
1779 "Ill-5 ", /* 5 0101 Task Gate */
1780 "Ill-6 ", /* 6 0110 16-bit Interrupt Gate */
1781 "Ill-7 ", /* 7 0111 16-bit Trap Gate */
1782 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
1783 "Tss64A", /* 9 1001 Available 32-bit TSS */
1784 "Ill-A ", /* A 1010 Reserved (Illegal) */
1785 "Tss64B", /* B 1011 Busy 32-bit TSS */
1786 "Call64", /* C 1100 32-bit Call Gate */
1787 "Ill-D ", /* D 1101 Reserved (Illegal) */
1788 "Int64 ", /* E 1110 32-bit Interrupt Gate */
1789 "Trap64" /* F 1111 32-bit Trap Gate */
1790 };
1791 switch (pDesc->Gen.u4Type)
1792 {
1793 /* raw */
1794 case X86_SEL_TYPE_SYS_UNDEFINED:
1795 case X86_SEL_TYPE_SYS_UNDEFINED2:
1796 case X86_SEL_TYPE_SYS_UNDEFINED4:
1797 case X86_SEL_TYPE_SYS_UNDEFINED3:
1798 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1799 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1800 case X86_SEL_TYPE_SYS_286_CALL_GATE:
1801 case X86_SEL_TYPE_SYS_286_INT_GATE:
1802 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
1803 case X86_SEL_TYPE_SYS_TASK_GATE:
1804 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s %.8Rhxs DPL=%d %s%s\n",
1805 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
1806 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1807 break;
1808
1809 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1810 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1811 case X86_SEL_TYPE_SYS_LDT:
1812 {
1813 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
1814 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1815 const char *pszLong = pDesc->Gen.u1Long ? "LONG" : " ";
1816
1817 uint64_t u32Base = X86DESC64_BASE(*pDesc);
1818 uint32_t cbLimit = X86DESC_LIMIT(*pDesc);
1819
1820 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%016RX64 Lim=%08x DPL=%d %s %s %s %sAVL=%d R=%d%s\n",
1821 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1822 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszLong, pszBig,
1823 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
1824 pszHyper);
1825 if (pfDblEntry)
1826 *pfDblEntry = true;
1827 break;
1828 }
1829
1830 case X86_SEL_TYPE_SYS_386_CALL_GATE:
1831 {
1832 unsigned cParams = pDesc->au8[4] & 0x1f;
1833 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
1834 RTSEL sel = pDesc->au16[1];
1835 uint64_t off = pDesc->au16[0]
1836 | ((uint64_t)pDesc->au16[3] << 16)
1837 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
1838 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s %s=%d%s\n",
1839 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1840 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
1841 if (pfDblEntry)
1842 *pfDblEntry = true;
1843 break;
1844 }
1845
1846 case X86_SEL_TYPE_SYS_386_INT_GATE:
1847 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
1848 {
1849 RTSEL sel = pDesc->au16[1];
1850 uint64_t off = pDesc->au16[0]
1851 | ((uint64_t)pDesc->au16[3] << 16)
1852 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
1853 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s%s\n",
1854 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1855 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1856 if (pfDblEntry)
1857 *pfDblEntry = true;
1858 break;
1859 }
1860
1861 /* impossible, just it's necessary to keep gcc happy. */
1862 default:
1863 return VINF_SUCCESS;
1864 }
1865 }
1866 return VINF_SUCCESS;
1867}
1868
1869
1870/**
1871 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
1872 *
1873 * @returns pfnPrintf status code.
1874 * @param pCmdHlp The DBGC command helpers.
1875 * @param pDesc The descriptor to display.
1876 * @param iEntry The descriptor entry number.
1877 * @param fHyper Whether the selector belongs to the hypervisor or not.
1878 */
1879static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
1880{
1881 int rc;
1882
1883 const char *pszHyper = fHyper ? " HYPER" : "";
1884 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
1885 if (pDesc->Gen.u1DescType)
1886 {
1887 static const char * const s_apszTypes[] =
1888 {
1889 "DataRO", /* 0 Read-Only */
1890 "DataRO", /* 1 Read-Only - Accessed */
1891 "DataRW", /* 2 Read/Write */
1892 "DataRW", /* 3 Read/Write - Accessed */
1893 "DownRO", /* 4 Expand-down, Read-Only */
1894 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
1895 "DownRW", /* 6 Expand-down, Read/Write */
1896 "DownRO", /* 7 Expand-down, Read/Write - Accessed */
1897 "CodeEO", /* 8 Execute-Only */
1898 "CodeEO", /* 9 Execute-Only - Accessed */
1899 "CodeER", /* A Execute/Readable */
1900 "CodeER", /* B Execute/Readable - Accessed */
1901 "ConfE0", /* C Conforming, Execute-Only */
1902 "ConfE0", /* D Conforming, Execute-Only - Accessed */
1903 "ConfER", /* E Conforming, Execute/Readable */
1904 "ConfER" /* F Conforming, Execute/Readable - Accessed */
1905 };
1906 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
1907 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1908 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1909 uint32_t u32Base = pDesc->Gen.u16BaseLow
1910 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1911 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1912 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1913 if (pDesc->Gen.u1Granularity)
1914 cbLimit <<= PAGE_SHIFT;
1915
1916 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
1917 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1918 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
1919 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
1920 }
1921 else
1922 {
1923 static const char * const s_apszTypes[] =
1924 {
1925 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
1926 "Tss16A", /* 1 0001 Available 16-bit TSS */
1927 "LDT ", /* 2 0010 LDT */
1928 "Tss16B", /* 3 0011 Busy 16-bit TSS */
1929 "Call16", /* 4 0100 16-bit Call Gate */
1930 "TaskG ", /* 5 0101 Task Gate */
1931 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
1932 "Trap16", /* 7 0111 16-bit Trap Gate */
1933 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
1934 "Tss32A", /* 9 1001 Available 32-bit TSS */
1935 "Ill-A ", /* A 1010 Reserved (Illegal) */
1936 "Tss32B", /* B 1011 Busy 32-bit TSS */
1937 "Call32", /* C 1100 32-bit Call Gate */
1938 "Ill-D ", /* D 1101 Reserved (Illegal) */
1939 "Int32 ", /* E 1110 32-bit Interrupt Gate */
1940 "Trap32" /* F 1111 32-bit Trap Gate */
1941 };
1942 switch (pDesc->Gen.u4Type)
1943 {
1944 /* raw */
1945 case X86_SEL_TYPE_SYS_UNDEFINED:
1946 case X86_SEL_TYPE_SYS_UNDEFINED2:
1947 case X86_SEL_TYPE_SYS_UNDEFINED4:
1948 case X86_SEL_TYPE_SYS_UNDEFINED3:
1949 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s %.8Rhxs DPL=%d %s%s\n",
1950 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
1951 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1952 break;
1953
1954 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1955 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1956 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1957 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1958 case X86_SEL_TYPE_SYS_LDT:
1959 {
1960 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1961 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
1962 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1963 uint32_t u32Base = pDesc->Gen.u16BaseLow
1964 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1965 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1966 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1967 if (pDesc->Gen.u1Granularity)
1968 cbLimit <<= PAGE_SHIFT;
1969
1970 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
1971 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1972 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
1973 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
1974 pszHyper);
1975 break;
1976 }
1977
1978 case X86_SEL_TYPE_SYS_TASK_GATE:
1979 {
1980 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s TSS=%04x DPL=%d %s%s\n",
1981 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
1982 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1983 break;
1984 }
1985
1986 case X86_SEL_TYPE_SYS_286_CALL_GATE:
1987 case X86_SEL_TYPE_SYS_386_CALL_GATE:
1988 {
1989 unsigned cParams = pDesc->au8[4] & 0x1f;
1990 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
1991 RTSEL sel = pDesc->au16[1];
1992 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
1993 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
1994 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1995 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
1996 break;
1997 }
1998
1999 case X86_SEL_TYPE_SYS_286_INT_GATE:
2000 case X86_SEL_TYPE_SYS_386_INT_GATE:
2001 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2002 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2003 {
2004 RTSEL sel = pDesc->au16[1];
2005 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2006 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
2007 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2008 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2009 break;
2010 }
2011
2012 /* impossible, just it's necessary to keep gcc happy. */
2013 default:
2014 return VINF_SUCCESS;
2015 }
2016 }
2017 return rc;
2018}
2019
2020
2021/**
2022 * The 'dg', 'dga', 'dl' and 'dla' commands.
2023 *
2024 * @returns VBox status.
2025 * @param pCmd Pointer to the command descriptor (as registered).
2026 * @param pCmdHlp Pointer to command helper functions.
2027 * @param pVM Pointer to the current VM (if any).
2028 * @param paArgs Pointer to (readonly) array of arguments.
2029 * @param cArgs Number of arguments in the array.
2030 */
2031static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2032{
2033 /*
2034 * Validate input.
2035 */
2036 if (!pVM)
2037 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2038
2039 /*
2040 * Get the CPU mode, check which command variation this is
2041 * and fix a default parameter if needed.
2042 */
2043 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2044 PVMCPU pVCpu = VMMGetCpuById(pVM, pDbgc->idCpu);
2045 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2046 bool fGdt = pCmd->pszCmd[1] == 'g';
2047 bool fAll = pCmd->pszCmd[2] == 'a';
2048 RTSEL SelTable = fGdt ? 0 : X86_SEL_LDT;
2049
2050 DBGCVAR Var;
2051 if (!cArgs)
2052 {
2053 cArgs = 1;
2054 paArgs = &Var;
2055 Var.enmType = DBGCVAR_TYPE_NUMBER;
2056 Var.u.u64Number = 0;
2057 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2058 Var.u64Range = 1024;
2059 }
2060
2061 /*
2062 * Process the arguments.
2063 */
2064 for (unsigned i = 0; i < cArgs; i++)
2065 {
2066 /*
2067 * Retrieve the selector value from the argument.
2068 * The parser may confuse pointers and numbers if more than one
2069 * argument is given, that that into account.
2070 */
2071 /* check that what we got makes sense as we don't trust the parser yet. */
2072 if ( paArgs[i].enmType != DBGCVAR_TYPE_NUMBER
2073 && !DBGCVAR_ISPOINTER(paArgs[i].enmType))
2074 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: arg #%u isn't of number or pointer type but %d.\n", i, paArgs[i].enmType);
2075 uint64_t u64;
2076 unsigned cSels = 1;
2077 switch (paArgs[i].enmType)
2078 {
2079 case DBGCVAR_TYPE_NUMBER:
2080 u64 = paArgs[i].u.u64Number;
2081 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
2082 cSels = RT_MIN(paArgs[i].u64Range, 1024);
2083 break;
2084 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
2085 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
2086 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
2087 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
2088 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
2089 default: u64 = _64K; break;
2090 }
2091 if (u64 < _64K)
2092 {
2093 unsigned Sel = (RTSEL)u64;
2094
2095 /*
2096 * Dump the specified range.
2097 */
2098 bool fSingle = cSels == 1;
2099 while ( cSels-- > 0
2100 && Sel < _64K)
2101 {
2102 DBGFSELINFO SelInfo;
2103 int rc = DBGFR3SelQueryInfo(pVM, pDbgc->idCpu, Sel | SelTable, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
2104 if (RT_SUCCESS(rc))
2105 {
2106 if (SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE)
2107 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x RealM Bas=%04x Lim=%04x\n",
2108 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
2109 else if ( fAll
2110 || fSingle
2111 || SelInfo.u.Raw.Gen.u1Present)
2112 {
2113 if (enmMode == CPUMMODE_PROTECTED)
2114 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &SelInfo.u.Raw, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER));
2115 else
2116 {
2117 bool fDblSkip = false;
2118 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &SelInfo.u.Raw64, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), &fDblSkip);
2119 if (fDblSkip)
2120 Sel += 4;
2121 }
2122 }
2123 }
2124 else
2125 {
2126 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %Rrc\n", Sel, rc);
2127 if (!fAll)
2128 return rc;
2129 }
2130 if (RT_FAILURE(rc))
2131 return rc;
2132
2133 /* next */
2134 Sel += 8;
2135 }
2136 }
2137 else
2138 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %llx is out of bounds\n", u64);
2139 }
2140
2141 return VINF_SUCCESS;
2142}
2143
2144
2145/**
2146 * The 'di' and 'dia' commands.
2147 *
2148 * @returns VBox status.
2149 * @param pCmd Pointer to the command descriptor (as registered).
2150 * @param pCmdHlp Pointer to command helper functions.
2151 * @param pVM Pointer to the current VM (if any).
2152 * @param paArgs Pointer to (readonly) array of arguments.
2153 * @param cArgs Number of arguments in the array.
2154 */
2155static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2156{
2157 /*
2158 * Validate input.
2159 */
2160 if (!pVM)
2161 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2162
2163 /*
2164 * Establish some stuff like the current IDTR and CPU mode,
2165 * and fix a default parameter.
2166 */
2167 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2168 PVMCPU pVCpu = VMMGetCpuById(pVM, pDbgc->idCpu);
2169 uint16_t cbLimit;
2170 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVCpu, &cbLimit);
2171 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2172 unsigned cbEntry;
2173 switch (enmMode)
2174 {
2175 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
2176 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
2177 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
2178 default:
2179 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid CPU mode %d.\n", enmMode);
2180 }
2181
2182 bool fAll = pCmd->pszCmd[2] == 'a';
2183 DBGCVAR Var;
2184 if (!cArgs)
2185 {
2186 cArgs = 1;
2187 paArgs = &Var;
2188 Var.enmType = DBGCVAR_TYPE_NUMBER;
2189 Var.u.u64Number = 0;
2190 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2191 Var.u64Range = 256;
2192 }
2193
2194 /*
2195 * Process the arguments.
2196 */
2197 for (unsigned i = 0; i < cArgs; i++)
2198 {
2199 /* check that what we got makes sense as we don't trust the parser yet. */
2200 if (paArgs[i].enmType != DBGCVAR_TYPE_NUMBER)
2201 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: arg #%u isn't of number type but %d.\n", i, paArgs[i].enmType);
2202 if (paArgs[i].u.u64Number < 256)
2203 {
2204 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
2205 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
2206 ? paArgs[i].u64Range
2207 : 1;
2208 bool fSingle = cInts == 1;
2209 while ( cInts-- > 0
2210 && iInt < 256)
2211 {
2212 /*
2213 * Try read it.
2214 */
2215 union
2216 {
2217 RTFAR16 Real;
2218 X86DESC Prot;
2219 X86DESC64 Long;
2220 } u;
2221 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
2222 {
2223 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x not within the IDT\n", (unsigned)iInt);
2224 if (!fAll && !fSingle)
2225 return VINF_SUCCESS;
2226 }
2227 DBGCVAR AddrVar;
2228 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
2229 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
2230 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
2231 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &u, cbEntry, &AddrVar, NULL);
2232 if (RT_FAILURE(rc))
2233 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
2234
2235 /*
2236 * Display it.
2237 */
2238 switch (enmMode)
2239 {
2240 case CPUMMODE_REAL:
2241 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
2242 /** @todo resolve 16:16 IDTE to a symbol */
2243 break;
2244 case CPUMMODE_PROTECTED:
2245 if (fAll || fSingle || u.Prot.Gen.u1Present)
2246 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
2247 break;
2248 case CPUMMODE_LONG:
2249 if (fAll || fSingle || u.Long.Gen.u1Present)
2250 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
2251 break;
2252 default: break; /* to shut up gcc */
2253 }
2254 if (RT_FAILURE(rc))
2255 return rc;
2256
2257 /* next */
2258 iInt++;
2259 }
2260 }
2261 else
2262 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
2263 }
2264
2265 return VINF_SUCCESS;
2266}
2267
2268
2269/**
2270 * The 'da', 'dq', 'dd', 'dw' and 'db' commands.
2271 *
2272 * @returns VBox status.
2273 * @param pCmd Pointer to the command descriptor (as registered).
2274 * @param pCmdHlp Pointer to command helper functions.
2275 * @param pVM Pointer to the current VM (if any).
2276 * @param paArgs Pointer to (readonly) array of arguments.
2277 * @param cArgs Number of arguments in the array.
2278 */
2279static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2280{
2281 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2282
2283 /*
2284 * Validate input.
2285 */
2286 if ( cArgs > 1
2287 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2288 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2289 if (!pVM)
2290 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2291
2292 /*
2293 * Figure out the element size.
2294 */
2295 unsigned cbElement;
2296 bool fAscii = false;
2297 switch (pCmd->pszCmd[1])
2298 {
2299 default:
2300 case 'b': cbElement = 1; break;
2301 case 'w': cbElement = 2; break;
2302 case 'd': cbElement = 4; break;
2303 case 'q': cbElement = 8; break;
2304 case 'a':
2305 cbElement = 1;
2306 fAscii = true;
2307 break;
2308 case '\0':
2309 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
2310 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
2311 if (!cbElement)
2312 cbElement = 1;
2313 break;
2314 }
2315
2316 /*
2317 * Find address.
2318 */
2319 if (!cArgs)
2320 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
2321 else
2322 pDbgc->DumpPos = paArgs[0];
2323
2324 /*
2325 * Range.
2326 */
2327 switch (pDbgc->DumpPos.enmRangeType)
2328 {
2329 case DBGCVAR_RANGE_NONE:
2330 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2331 pDbgc->DumpPos.u64Range = 0x60;
2332 break;
2333
2334 case DBGCVAR_RANGE_ELEMENTS:
2335 if (pDbgc->DumpPos.u64Range > 2048)
2336 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many elements requested. Max is 2048 elements.\n");
2337 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2338 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
2339 break;
2340
2341 case DBGCVAR_RANGE_BYTES:
2342 if (pDbgc->DumpPos.u64Range > 65536)
2343 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
2344 break;
2345
2346 default:
2347 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
2348 }
2349
2350 pDbgc->pLastPos = &pDbgc->DumpPos;
2351
2352 /*
2353 * Do the dumping.
2354 */
2355 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
2356 int cbLeft = (int)pDbgc->DumpPos.u64Range;
2357 uint8_t u8Prev = '\0';
2358 for (;;)
2359 {
2360 /*
2361 * Read memory.
2362 */
2363 char achBuffer[16];
2364 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
2365 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
2366 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
2367 if (RT_FAILURE(rc))
2368 {
2369 if (u8Prev && u8Prev != '\n')
2370 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2371 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
2372 }
2373
2374 /*
2375 * Display it.
2376 */
2377 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
2378 if (!fAscii)
2379 {
2380 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:", &pDbgc->DumpPos);
2381 unsigned i;
2382 for (i = 0; i < cb; i += cbElement)
2383 {
2384 const char *pszSpace = " ";
2385 if (cbElement <= 2 && i == 8 && !fAscii)
2386 pszSpace = "-";
2387 switch (cbElement)
2388 {
2389 case 1: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
2390 case 2: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
2391 case 4: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
2392 case 8: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
2393 }
2394 }
2395
2396 /* chars column */
2397 if (pDbgc->cbDumpElement == 1)
2398 {
2399 while (i++ < sizeof(achBuffer))
2400 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2401 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2402 for (i = 0; i < cb; i += cbElement)
2403 {
2404 uint8_t u8 = *(uint8_t *)&achBuffer[i];
2405 if (RT_C_IS_PRINT(u8) && u8 < 127 && u8 >= 32)
2406 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2407 else
2408 pCmdHlp->pfnPrintf(pCmdHlp, NULL, ".");
2409 }
2410 }
2411 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2412 }
2413 else
2414 {
2415 /*
2416 * We print up to the first zero and stop there.
2417 * Only printables + '\t' and '\n' are printed.
2418 */
2419 if (!u8Prev)
2420 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DumpPos);
2421 uint8_t u8 = '\0';
2422 unsigned i;
2423 for (i = 0; i < cb; i++)
2424 {
2425 u8Prev = u8;
2426 u8 = *(uint8_t *)&achBuffer[i];
2427 if ( u8 < 127
2428 && ( (RT_C_IS_PRINT(u8) && u8 >= 32)
2429 || u8 == '\t'
2430 || u8 == '\n'))
2431 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2432 else if (!u8)
2433 break;
2434 else
2435 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\\x%x", u8);
2436 }
2437 if (u8 == '\0')
2438 cb = cbLeft = i + 1;
2439 if (cbLeft - cb <= 0 && u8Prev != '\n')
2440 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2441 }
2442
2443 /*
2444 * Advance
2445 */
2446 cbLeft -= (int)cb;
2447 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
2448 if (RT_FAILURE(rc))
2449 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
2450 if (cbLeft <= 0)
2451 break;
2452 }
2453
2454 NOREF(pCmd);
2455 return VINF_SUCCESS;
2456}
2457
2458
2459/**
2460 * Best guess at which paging mode currently applies to the guest
2461 * paging structures.
2462 *
2463 * This have to come up with a decent answer even when the guest
2464 * is in non-paged protected mode or real mode.
2465 *
2466 * @returns cr3.
2467 * @param pDbgc The DBGC instance.
2468 * @param pfPAE Where to store the page address extension indicator.
2469 * @param pfLME Where to store the long mode enabled indicator.
2470 * @param pfPSE Where to store the page size extension indicator.
2471 * @param pfPGE Where to store the page global enabled indicator.
2472 * @param pfNXE Where to store the no-execution enabled indicator.
2473 */
2474static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2475{
2476 PVMCPU pVCpu = VMMGetCpuById(pDbgc->pVM, pDbgc->idCpu);
2477 RTGCUINTREG cr4 = CPUMGetGuestCR4(pVCpu);
2478 *pfPSE = !!(cr4 & X86_CR4_PSE);
2479 *pfPGE = !!(cr4 & X86_CR4_PGE);
2480 if (cr4 & X86_CR4_PAE)
2481 {
2482 *pfPSE = true;
2483 *pfPAE = true;
2484 }
2485 else
2486 *pfPAE = false;
2487
2488 *pfLME = CPUMGetGuestMode(pVCpu) == CPUMMODE_LONG;
2489 *pfNXE = false; /* GUEST64 GUESTNX */
2490 return CPUMGetGuestCR3(pVCpu);
2491}
2492
2493
2494/**
2495 * Determine the shadow paging mode.
2496 *
2497 * @returns cr3.
2498 * @param pDbgc The DBGC instance.
2499 * @param pfPAE Where to store the page address extension indicator.
2500 * @param pfLME Where to store the long mode enabled indicator.
2501 * @param pfPSE Where to store the page size extension indicator.
2502 * @param pfPGE Where to store the page global enabled indicator.
2503 * @param pfNXE Where to store the no-execution enabled indicator.
2504 */
2505static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2506{
2507 PVMCPU pVCpu = VMMGetCpuById(pDbgc->pVM, pDbgc->idCpu);
2508
2509 *pfPSE = true;
2510 *pfPGE = false;
2511 switch (PGMGetShadowMode(pVCpu))
2512 {
2513 default:
2514 case PGMMODE_32_BIT:
2515 *pfPAE = *pfLME = *pfNXE = false;
2516 break;
2517 case PGMMODE_PAE:
2518 *pfLME = *pfNXE = false;
2519 *pfPAE = true;
2520 break;
2521 case PGMMODE_PAE_NX:
2522 *pfLME = false;
2523 *pfPAE = *pfNXE = true;
2524 break;
2525 case PGMMODE_AMD64:
2526 *pfNXE = false;
2527 *pfPAE = *pfLME = true;
2528 break;
2529 case PGMMODE_AMD64_NX:
2530 *pfPAE = *pfLME = *pfNXE = true;
2531 break;
2532 }
2533 return PGMGetHyperCR3(pVCpu);
2534}
2535
2536
2537/**
2538 * The 'dpd', 'dpda', 'dpdb', 'dpdg' and 'dpdh' commands.
2539 *
2540 * @returns VBox status.
2541 * @param pCmd Pointer to the command descriptor (as registered).
2542 * @param pCmdHlp Pointer to command helper functions.
2543 * @param pVM Pointer to the current VM (if any).
2544 * @param paArgs Pointer to (readonly) array of arguments.
2545 * @param cArgs Number of arguments in the array.
2546 */
2547static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2548{
2549 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2550
2551 /*
2552 * Validate input.
2553 */
2554 if ( cArgs > 1
2555 || (cArgs == 1 && pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2556 || (cArgs == 1 && pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2557 )
2558 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2559 if (!pVM)
2560 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2561
2562 /*
2563 * Guest or shadow page directories? Get the paging parameters.
2564 */
2565 bool fGuest = pCmd->pszCmd[3] != 'h';
2566 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2567 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2568 ? pDbgc->fRegCtxGuest
2569 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2570
2571 bool fPAE, fLME, fPSE, fPGE, fNXE;
2572 uint64_t cr3 = fGuest
2573 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2574 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2575 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2576
2577 /*
2578 * Setup default argument if none was specified.
2579 * Fix address / index confusion.
2580 */
2581 DBGCVAR VarDefault;
2582 if (!cArgs)
2583 {
2584 if (pCmd->pszCmd[3] == 'a')
2585 {
2586 if (fLME || fPAE)
2587 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");
2588 if (fGuest)
2589 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
2590 else
2591 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
2592 }
2593 else
2594 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
2595 paArgs = &VarDefault;
2596 cArgs = 1;
2597 }
2598 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
2599 {
2600 Assert(pCmd->pszCmd[3] != 'a');
2601 VarDefault = paArgs[0];
2602 if (VarDefault.u.u64Number <= 1024)
2603 {
2604 if (fPAE)
2605 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
2606 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
2607 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
2608 VarDefault.u.u64Number <<= X86_PD_SHIFT;
2609 }
2610 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
2611 paArgs = &VarDefault;
2612 }
2613
2614 /*
2615 * Locate the PDE to start displaying at.
2616 *
2617 * The 'dpda' command takes the address of a PDE, while the others are guest
2618 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
2619 * while the others require us to do all the tedious walking thru the paging
2620 * hierarchy to find the intended PDE.
2621 */
2622 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
2623 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
2624 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
2625 unsigned cEntries; /* The number of entries to display. */
2626 unsigned cEntriesMax; /* The max number of entries to display. */
2627 int rc;
2628 if (pCmd->pszCmd[3] == 'a')
2629 {
2630 VarPDEAddr = paArgs[0];
2631 switch (VarPDEAddr.enmRangeType)
2632 {
2633 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
2634 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
2635 default: cEntries = 10; break;
2636 }
2637 cEntriesMax = PAGE_SIZE / cbEntry;
2638 }
2639 else
2640 {
2641 /*
2642 * Determine the range.
2643 */
2644 switch (paArgs[0].enmRangeType)
2645 {
2646 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2647 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2648 default: cEntries = 10; break;
2649 }
2650
2651 /*
2652 * Normalize the input address, it must be a flat GC address.
2653 */
2654 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2655 if (RT_FAILURE(rc))
2656 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2657 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2658 {
2659 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2660 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2661 }
2662 if (fPAE)
2663 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
2664 else
2665 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
2666
2667 /*
2668 * Do the paging walk until we get to the page directory.
2669 */
2670 DBGCVAR VarCur;
2671 if (fGuest)
2672 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2673 else
2674 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2675 if (fLME)
2676 {
2677 /* Page Map Level 4 Lookup. */
2678 /* Check if it's a valid address first? */
2679 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2680 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2681 X86PML4E Pml4e;
2682 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2683 if (RT_FAILURE(rc))
2684 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2685 if (!Pml4e.n.u1Present)
2686 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2687
2688 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2689 Assert(fPAE);
2690 }
2691 if (fPAE)
2692 {
2693 /* Page directory pointer table. */
2694 X86PDPE Pdpe;
2695 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2696 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2697 if (RT_FAILURE(rc))
2698 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2699 if (!Pdpe.n.u1Present)
2700 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2701
2702 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2703 VarPDEAddr = VarCur;
2704 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2705 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
2706 }
2707 else
2708 {
2709 /* 32-bit legacy - CR3 == page directory. */
2710 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
2711 VarPDEAddr = VarCur;
2712 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
2713 }
2714 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2715 iEntry /= cbEntry;
2716 }
2717
2718 /* adjust cEntries */
2719 cEntries = RT_MAX(1, cEntries);
2720 cEntries = RT_MIN(cEntries, cEntriesMax);
2721
2722 /*
2723 * The display loop.
2724 */
2725 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
2726 &VarPDEAddr, iEntry);
2727 do
2728 {
2729 /*
2730 * Read.
2731 */
2732 X86PDEPAE Pde;
2733 Pde.u = 0;
2734 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, cbEntry, &VarPDEAddr, NULL);
2735 if (RT_FAILURE(rc))
2736 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
2737
2738 /*
2739 * Display.
2740 */
2741 if (iEntry != ~0U)
2742 {
2743 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2744 iEntry++;
2745 }
2746 if (fPSE && Pde.b.u1Size)
2747 DBGCCmdHlpPrintf(pCmdHlp,
2748 fPAE
2749 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2750 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2751 Pde.u,
2752 Pde.u & X86_PDE_PAE_PG_MASK,
2753 Pde.b.u1Present ? "p " : "np",
2754 Pde.b.u1Write ? "w" : "r",
2755 Pde.b.u1User ? "u" : "s",
2756 Pde.b.u1Accessed ? "a " : "na",
2757 Pde.b.u1Dirty ? "d " : "nd",
2758 Pde.b.u3Available,
2759 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
2760 Pde.b.u1WriteThru ? "pwt" : " ",
2761 Pde.b.u1CacheDisable ? "pcd" : " ",
2762 Pde.b.u1PAT ? "pat" : "",
2763 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2764 else
2765 DBGCCmdHlpPrintf(pCmdHlp,
2766 fPAE
2767 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
2768 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
2769 Pde.u,
2770 Pde.u & X86_PDE_PAE_PG_MASK,
2771 Pde.n.u1Present ? "p " : "np",
2772 Pde.n.u1Write ? "w" : "r",
2773 Pde.n.u1User ? "u" : "s",
2774 Pde.n.u1Accessed ? "a " : "na",
2775 Pde.u & RT_BIT(6) ? "6 " : " ",
2776 Pde.n.u3Available,
2777 Pde.u & RT_BIT(8) ? "8" : " ",
2778 Pde.n.u1WriteThru ? "pwt" : " ",
2779 Pde.n.u1CacheDisable ? "pcd" : " ",
2780 Pde.u & RT_BIT(7) ? "7" : "",
2781 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2782 if (Pde.u & UINT64_C(0x7fff000000000000))
2783 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
2784 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2785 if (RT_FAILURE(rc))
2786 return rc;
2787
2788 /*
2789 * Advance.
2790 */
2791 VarPDEAddr.u.u64Number += cbEntry;
2792 if (iEntry != ~0U)
2793 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
2794 } while (cEntries-- > 0);
2795
2796 return VINF_SUCCESS;
2797}
2798
2799
2800/**
2801 * The 'dpdb' command.
2802 *
2803 * @returns VBox status.
2804 * @param pCmd Pointer to the command descriptor (as registered).
2805 * @param pCmdHlp Pointer to command helper functions.
2806 * @param pVM Pointer to the current VM (if any).
2807 * @param paArgs Pointer to (readonly) array of arguments.
2808 * @param cArgs Number of arguments in the array.
2809 */
2810static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2811{
2812 if (!pVM)
2813 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2814 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2815 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2816 if (RT_FAILURE(rc1))
2817 return rc1;
2818 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2819 return rc2;
2820}
2821
2822
2823/**
2824 * The 'dph*' commands and main part of 'm'.
2825 *
2826 * @returns VBox status.
2827 * @param pCmd Pointer to the command descriptor (as registered).
2828 * @param pCmdHlp Pointer to command helper functions.
2829 * @param pVM Pointer to the current VM (if any).
2830 * @param paArgs Pointer to (readonly) array of arguments.
2831 * @param cArgs Number of arguments in the array.
2832 */
2833static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2834{
2835 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2836 if (!pVM)
2837 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No VM.\n");
2838
2839 /*
2840 * Figure the context and base flags.
2841 */
2842 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;
2843 if (pCmd->pszCmd[0] == 'm')
2844 fFlags |= DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW;
2845 else if (pCmd->pszCmd[3] == '\0')
2846 fFlags |= pDbgc->fRegCtxGuest ? DBGFPGDMP_FLAGS_GUEST : DBGFPGDMP_FLAGS_SHADOW;
2847 else if (pCmd->pszCmd[3] == 'g')
2848 fFlags |= DBGFPGDMP_FLAGS_GUEST;
2849 else if (pCmd->pszCmd[3] == 'h')
2850 fFlags |= DBGFPGDMP_FLAGS_SHADOW;
2851 else
2852 AssertFailed();
2853
2854 if (pDbgc->cPagingHierarchyDumps == 0)
2855 fFlags |= DBGFPGDMP_FLAGS_HEADER;
2856 pDbgc->cPagingHierarchyDumps = (pDbgc->cPagingHierarchyDumps + 1) % 42;
2857
2858 /*
2859 * Get the range.
2860 */
2861 PCDBGCVAR pRange = cArgs > 0 ? &paArgs[0] : pDbgc->pLastPos;
2862 RTGCPTR GCPtrFirst = NIL_RTGCPTR;
2863 int rc = DBGCCmdHlpVarToFlatAddr(pCmdHlp, pRange, &GCPtrFirst);
2864 if (RT_FAILURE(rc))
2865 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to convert %DV to a flat address: %Rrc", pRange, rc);
2866
2867 uint64_t cbRange;
2868 rc = DBGCCmdHlpVarGetRange(pCmdHlp, pRange, PAGE_SIZE, PAGE_SIZE * 8, &cbRange);
2869 if (RT_FAILURE(rc))
2870 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to obtain the range of %DV: %Rrc", pRange, rc);
2871
2872 RTGCPTR GCPtrLast = RTGCPTR_MAX - GCPtrFirst;
2873 if (cbRange >= GCPtrLast)
2874 GCPtrLast = RTGCPTR_MAX;
2875 else if (!cbRange)
2876 GCPtrLast = GCPtrFirst;
2877 else
2878 GCPtrLast = GCPtrFirst + cbRange - 1;
2879
2880 /*
2881 * Do we have a CR3?
2882 */
2883 uint64_t cr3 = 0;
2884 if (cArgs > 1)
2885 {
2886 if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
2887 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No CR3 or mode arguments when dumping both context, please.");
2888 if (paArgs[1].enmType != DBGCVAR_TYPE_NUMBER)
2889 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The CR3 argument is not a number: %DV", &paArgs[1]);
2890 cr3 = paArgs[1].u.u64Number;
2891 }
2892 else
2893 fFlags |= DBGFPGDMP_FLAGS_CURRENT_CR3;
2894
2895 /*
2896 * Do we have a mode?
2897 */
2898 if (cArgs > 2)
2899 {
2900 if (paArgs[2].enmType != DBGCVAR_TYPE_STRING)
2901 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The mode argument is not a string: %DV", &paArgs[2]);
2902 static const struct MODETOFLAGS
2903 {
2904 const char *pszName;
2905 uint32_t fFlags;
2906 } s_aModeToFlags[] =
2907 {
2908 { "ept", DBGFPGDMP_FLAGS_EPT },
2909 { "legacy", 0 },
2910 { "legacy-np", DBGFPGDMP_FLAGS_NP },
2911 { "pse", DBGFPGDMP_FLAGS_PSE },
2912 { "pse-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NP },
2913 { "pae", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE },
2914 { "pae-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NP },
2915 { "pae-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE },
2916 { "pae-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP },
2917 { "long", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME },
2918 { "long-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NP },
2919 { "long-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE },
2920 { "long-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP }
2921 };
2922 int i = RT_ELEMENTS(s_aModeToFlags);
2923 while (i-- > 0)
2924 if (!strcmp(s_aModeToFlags[i].pszName, paArgs[2].u.pszString))
2925 {
2926 fFlags |= s_aModeToFlags[i].fFlags;
2927 break;
2928 }
2929 if (i < 0)
2930 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown mode: \"%s\"", paArgs[2].u.pszString);
2931 }
2932 else
2933 fFlags |= DBGFPGDMP_FLAGS_CURRENT_MODE;
2934
2935 /*
2936 * Call the worker.
2937 */
2938 rc = DBGFR3PagingDumpEx(pVM, pDbgc->idCpu, fFlags, cr3, GCPtrFirst, GCPtrLast, 99 /*cMaxDepth*/,
2939 DBGCCmdHlpGetDbgfOutputHlp(pCmdHlp));
2940 if (RT_FAILURE(rc))
2941 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3PagingDumpEx: %Rrc\n", rc);
2942 return VINF_SUCCESS;
2943}
2944
2945
2946
2947/**
2948 * The 'dpg*' commands.
2949 *
2950 * @returns VBox status.
2951 * @param pCmd Pointer to the command descriptor (as registered).
2952 * @param pCmdHlp Pointer to command helper functions.
2953 * @param pVM Pointer to the current VM (if any).
2954 * @param paArgs Pointer to (readonly) array of arguments.
2955 * @param cArgs Number of arguments in the array.
2956 */
2957static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2958{
2959 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2960
2961 /*
2962 * Validate input.
2963 */
2964 if ( cArgs != 1
2965 || (pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2966 || (pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2967 )
2968 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2969 if (!pVM)
2970 return DBGCCmdHlpPrintf(pCmdHlp, "error: No VM.\n");
2971
2972 /*
2973 * Guest or shadow page tables? Get the paging parameters.
2974 */
2975 bool fGuest = pCmd->pszCmd[3] != 'h';
2976 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2977 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2978 ? pDbgc->fRegCtxGuest
2979 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2980
2981 bool fPAE, fLME, fPSE, fPGE, fNXE;
2982 uint64_t cr3 = fGuest
2983 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2984 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2985 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2986
2987 /*
2988 * Locate the PTE to start displaying at.
2989 *
2990 * The 'dpta' command takes the address of a PTE, while the others are guest
2991 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
2992 * while the others require us to do all the tedious walking thru the paging
2993 * hierarchy to find the intended PTE.
2994 */
2995 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
2996 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
2997 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
2998 unsigned cEntries; /* The number of entries to display. */
2999 unsigned cEntriesMax; /* The max number of entries to display. */
3000 int rc;
3001 if (pCmd->pszCmd[3] == 'a')
3002 {
3003 VarPTEAddr = paArgs[0];
3004 switch (VarPTEAddr.enmRangeType)
3005 {
3006 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
3007 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
3008 default: cEntries = 10; break;
3009 }
3010 cEntriesMax = PAGE_SIZE / cbEntry;
3011 }
3012 else
3013 {
3014 /*
3015 * Determine the range.
3016 */
3017 switch (paArgs[0].enmRangeType)
3018 {
3019 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3020 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3021 default: cEntries = 10; break;
3022 }
3023
3024 /*
3025 * Normalize the input address, it must be a flat GC address.
3026 */
3027 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3028 if (RT_FAILURE(rc))
3029 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3030 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3031 {
3032 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3033 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3034 }
3035 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
3036
3037 /*
3038 * Do the paging walk until we get to the page table.
3039 */
3040 DBGCVAR VarCur;
3041 if (fGuest)
3042 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3043 else
3044 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3045 if (fLME)
3046 {
3047 /* Page Map Level 4 Lookup. */
3048 /* Check if it's a valid address first? */
3049 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3050 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3051 X86PML4E Pml4e;
3052 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3053 if (RT_FAILURE(rc))
3054 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3055 if (!Pml4e.n.u1Present)
3056 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3057
3058 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3059 Assert(fPAE);
3060 }
3061 if (fPAE)
3062 {
3063 /* Page directory pointer table. */
3064 X86PDPE Pdpe;
3065 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3066 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3067 if (RT_FAILURE(rc))
3068 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3069 if (!Pdpe.n.u1Present)
3070 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3071
3072 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3073
3074 /* Page directory (PAE). */
3075 X86PDEPAE Pde;
3076 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
3077 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
3078 if (RT_FAILURE(rc))
3079 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3080 if (!Pde.n.u1Present)
3081 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3082 if (fPSE && Pde.n.u1Size)
3083 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3084
3085 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
3086 VarPTEAddr = VarCur;
3087 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
3088 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
3089 }
3090 else
3091 {
3092 /* Page directory (legacy). */
3093 X86PDE Pde;
3094 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
3095 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
3096 if (RT_FAILURE(rc))
3097 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3098 if (!Pde.n.u1Present)
3099 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3100 if (fPSE && Pde.n.u1Size)
3101 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3102
3103 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
3104 VarPTEAddr = VarCur;
3105 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
3106 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
3107 }
3108 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3109 iEntry /= cbEntry;
3110 }
3111
3112 /* adjust cEntries */
3113 cEntries = RT_MAX(1, cEntries);
3114 cEntries = RT_MIN(cEntries, cEntriesMax);
3115
3116 /*
3117 * The display loop.
3118 */
3119 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
3120 &VarPTEAddr, &VarGCPtr, iEntry);
3121 do
3122 {
3123 /*
3124 * Read.
3125 */
3126 X86PTEPAE Pte;
3127 Pte.u = 0;
3128 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pte, cbEntry, &VarPTEAddr, NULL);
3129 if (RT_FAILURE(rc))
3130 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
3131
3132 /*
3133 * Display.
3134 */
3135 if (iEntry != ~0U)
3136 {
3137 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3138 iEntry++;
3139 }
3140 DBGCCmdHlpPrintf(pCmdHlp,
3141 fPAE
3142 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3143 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3144 Pte.u,
3145 Pte.u & X86_PTE_PAE_PG_MASK,
3146 Pte.n.u1Present ? "p " : "np",
3147 Pte.n.u1Write ? "w" : "r",
3148 Pte.n.u1User ? "u" : "s",
3149 Pte.n.u1Accessed ? "a " : "na",
3150 Pte.n.u1Dirty ? "d " : "nd",
3151 Pte.n.u3Available,
3152 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
3153 Pte.n.u1WriteThru ? "pwt" : " ",
3154 Pte.n.u1CacheDisable ? "pcd" : " ",
3155 Pte.n.u1PAT ? "pat" : " ",
3156 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
3157 );
3158 if (Pte.u & UINT64_C(0x7fff000000000000))
3159 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
3160 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3161 if (RT_FAILURE(rc))
3162 return rc;
3163
3164 /*
3165 * Advance.
3166 */
3167 VarPTEAddr.u.u64Number += cbEntry;
3168 if (iEntry != ~0U)
3169 VarGCPtr.u.GCFlat += PAGE_SIZE;
3170 } while (cEntries-- > 0);
3171
3172 return VINF_SUCCESS;
3173}
3174
3175
3176/**
3177 * The 'dptb' command.
3178 *
3179 * @returns VBox status.
3180 * @param pCmd Pointer to the command descriptor (as registered).
3181 * @param pCmdHlp Pointer to command helper functions.
3182 * @param pVM Pointer to the current VM (if any).
3183 * @param paArgs Pointer to (readonly) array of arguments.
3184 * @param cArgs Number of arguments in the array.
3185 */
3186static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3187{
3188 if (!pVM)
3189 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
3190 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
3191 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
3192 if (RT_FAILURE(rc1))
3193 return rc1;
3194 NOREF(pCmd); NOREF(cArgs);
3195 return rc2;
3196}
3197
3198
3199/**
3200 * The 'dt' command.
3201 *
3202 * @returns VBox status.
3203 * @param pCmd Pointer to the command descriptor (as registered).
3204 * @param pCmdHlp Pointer to command helper functions.
3205 * @param pVM Pointer to the current VM (if any).
3206 * @param paArgs Pointer to (readonly) array of arguments.
3207 * @param cArgs Number of arguments in the array.
3208 */
3209static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3210{
3211 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3212 int rc;
3213
3214 if (!pVM)
3215 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No VM.\n");
3216 if ( cArgs > 1
3217 || (cArgs == 1 && paArgs[0].enmType == DBGCVAR_TYPE_STRING)
3218 || (cArgs == 1 && paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL))
3219 return DBGCCmdHlpFail(pCmdHlp, pCmd, "internal error: The parser doesn't do its job properly yet...\n");
3220
3221 /*
3222 * Check if the command indicates the type.
3223 */
3224 enum { kTss16, kTss32, kTss64, kTssToBeDetermined } enmTssType = kTssToBeDetermined;
3225 if (!strcmp(pCmd->pszCmd, "dt16"))
3226 enmTssType = kTss16;
3227 else if (!strcmp(pCmd->pszCmd, "dt32"))
3228 enmTssType = kTss32;
3229 else if (!strcmp(pCmd->pszCmd, "dt64"))
3230 enmTssType = kTss64;
3231
3232 /*
3233 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
3234 */
3235 uint32_t SelTss = UINT32_MAX;
3236 DBGCVAR VarTssAddr;
3237 if (cArgs == 0)
3238 {
3239 /** @todo consider querying the hidden bits instead (missing API). */
3240 uint16_t SelTR;
3241 rc = DBGFR3RegCpuQueryU16(pVM, pDbgc->idCpu, DBGFREG_TR, &SelTR);
3242 if (RT_FAILURE(rc))
3243 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to query TR, rc=%Rrc\n", rc);
3244 DBGCVAR_INIT_GC_FAR(&VarTssAddr, SelTR, 0);
3245 SelTss = SelTR;
3246 }
3247 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3248 {
3249 if (paArgs[0].u.u64Number < 0xffff)
3250 DBGCVAR_INIT_GC_FAR(&VarTssAddr, (RTSEL)paArgs[0].u.u64Number, 0);
3251 else
3252 {
3253 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
3254 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Element count doesn't combine with a TSS address.\n");
3255 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, paArgs[0].u.u64Number);
3256 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_BYTES)
3257 {
3258 VarTssAddr.enmRangeType = paArgs[0].enmRangeType;
3259 VarTssAddr.u64Range = paArgs[0].u64Range;
3260 }
3261 }
3262 }
3263 else
3264 VarTssAddr = paArgs[0];
3265
3266 /*
3267 * Deal with TSS:ign by means of the GDT.
3268 */
3269 if (VarTssAddr.enmType == DBGCVAR_TYPE_GC_FAR)
3270 {
3271 SelTss = VarTssAddr.u.GCFar.sel;
3272 DBGFSELINFO SelInfo;
3273 rc = DBGFR3SelQueryInfo(pVM, pDbgc->idCpu, VarTssAddr.u.GCFar.sel, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
3274 if (RT_FAILURE(rc))
3275 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3SelQueryInfo(,%u,%d,,) -> %Rrc.\n",
3276 pDbgc->idCpu, VarTssAddr.u.GCFar.sel, rc);
3277
3278 if (SelInfo.u.Raw.Gen.u1DescType)
3279 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (!sys)\n", VarTssAddr.u.GCFar.sel);
3280
3281 switch (SelInfo.u.Raw.Gen.u4Type)
3282 {
3283 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
3284 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
3285 if (enmTssType == kTssToBeDetermined)
3286 enmTssType = kTss16;
3287 break;
3288
3289 case X86_SEL_TYPE_SYS_386_TSS_BUSY: /* AMD64 too */
3290 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
3291 if (enmTssType == kTssToBeDetermined)
3292 enmTssType = SelInfo.fFlags & DBGFSELINFO_FLAGS_LONG_MODE ? kTss64 : kTss32;
3293 break;
3294
3295 default:
3296 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (type=%x)\n",
3297 VarTssAddr.u.GCFar.sel, SelInfo.u.Raw.Gen.u4Type);
3298 }
3299
3300 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, SelInfo.GCPtrBase);
3301 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, RT_MAX(SelInfo.cbLimit + 1, SelInfo.cbLimit));
3302 }
3303
3304 /*
3305 * Determine the TSS type if none is currently given.
3306 */
3307 if (enmTssType == kTssToBeDetermined)
3308 {
3309 if ( VarTssAddr.u64Range > 0
3310 && VarTssAddr.u64Range < sizeof(X86TSS32) - 4)
3311 enmTssType = kTss16;
3312 else
3313 {
3314 uint64_t uEfer;
3315 rc = DBGFR3RegCpuQueryU64(pVM, pDbgc->idCpu, DBGFREG_MSR_K6_EFER, &uEfer);
3316 if ( RT_FAILURE(rc)
3317 || !(uEfer & MSR_K6_EFER_LMA) )
3318 enmTssType = kTss32;
3319 else
3320 enmTssType = kTss64;
3321 }
3322 }
3323
3324 /*
3325 * Figure the min/max sizes.
3326 * ASSUMES max TSS size is 64 KB.
3327 */
3328 uint32_t cbTssMin;
3329 uint32_t cbTssMax;
3330 switch (enmTssType)
3331 {
3332 case kTss16:
3333 cbTssMin = cbTssMax = sizeof(X86TSS16);
3334 break;
3335 case kTss32:
3336 cbTssMin = RT_OFFSETOF(X86TSS32, IntRedirBitmap);
3337 cbTssMax = _64K;
3338 break;
3339 case kTss64:
3340 cbTssMin = RT_OFFSETOF(X86TSS64, IntRedirBitmap);
3341 cbTssMax = _64K;
3342 break;
3343 default:
3344 AssertFailedReturn(VERR_INTERNAL_ERROR);
3345 }
3346 uint32_t cbTss = VarTssAddr.enmRangeType == DBGCVAR_RANGE_BYTES ? (uint32_t)VarTssAddr.u64Range : 0;
3347 if (cbTss == 0)
3348 cbTss = cbTssMin;
3349 else if (cbTss < cbTssMin)
3350 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Minimum TSS size is %u bytes, you specified %llu (%llx) bytes.\n",
3351 cbTssMin, VarTssAddr.u64Range, VarTssAddr.u64Range);
3352 else if (cbTss > cbTssMax)
3353 cbTss = cbTssMax;
3354 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, cbTss);
3355
3356 /*
3357 * Read the TSS into a temporary buffer.
3358 */
3359 uint8_t abBuf[_64K];
3360 size_t cbTssRead;
3361 rc = DBGCCmdHlpMemRead(pCmdHlp, pVM, abBuf, cbTss, &VarTssAddr, &cbTssRead);
3362 if (RT_FAILURE(rc))
3363 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read TSS at %Dv: %Rrc\n", &VarTssAddr, rc);
3364 if (cbTssRead < cbTssMin)
3365 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read essential parts of the TSS (read %zu, min %zu).\n",
3366 cbTssRead, cbTssMin);
3367 if (cbTssRead < cbTss)
3368 memset(&abBuf[cbTssRead], 0xff, cbTss - cbTssRead);
3369
3370
3371 /*
3372 * Format the TSS.
3373 */
3374 uint16_t offIoBitmap;
3375 switch (enmTssType)
3376 {
3377 case kTss16:
3378 {
3379 PCX86TSS16 pTss = (PCX86TSS16)&abBuf[0];
3380 if (SelTss != UINT32_MAX)
3381 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS16 at %Dv\n", SelTss, &VarTssAddr);
3382 else
3383 DBGCCmdHlpPrintf(pCmdHlp, "TSS16 at %Dv\n", &VarTssAddr);
3384 DBGCCmdHlpPrintf(pCmdHlp,
3385 "ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x\n"
3386 "ip=%04x sp=%04x bp=%04x\n"
3387 "cs=%04x ss=%04x ds=%04x es=%04x flags=%04x\n"
3388 "ss:sp0=%04x:%04x ss:sp1=%04x:%04x ss:sp2=%04x:%04x\n"
3389 "prev=%04x ldtr=%04x\n"
3390 ,
3391 pTss->ax, pTss->bx, pTss->cx, pTss->dx, pTss->si, pTss->di,
3392 pTss->ip, pTss->sp, pTss->bp,
3393 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->flags,
3394 pTss->ss0, pTss->sp0, pTss->ss1, pTss->sp1, pTss->ss2, pTss->sp2,
3395 pTss->selPrev, pTss->selLdt);
3396 if (pTss->cs != 0)
3397 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%04x L 0", pTss->cs, pTss->ip);
3398 offIoBitmap = 0;
3399 break;
3400 }
3401
3402 case kTss32:
3403 {
3404 PCX86TSS32 pTss = (PCX86TSS32)&abBuf[0];
3405 if (SelTss != UINT32_MAX)
3406 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS32 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
3407 else
3408 DBGCCmdHlpPrintf(pCmdHlp, "TSS32 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
3409 DBGCCmdHlpPrintf(pCmdHlp,
3410 "eax=%08x bx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
3411 "eip=%08x esp=%08x ebp=%08x\n"
3412 "cs=%04x ss=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n"
3413 "ss:esp0=%04x:%08x ss:esp1=%04x:%08x ss:esp2=%04x:%08x\n"
3414 "prev=%04x ldtr=%04x cr3=%08x debug=%u iomap=%04x\n"
3415 ,
3416 pTss->eax, pTss->ebx, pTss->ecx, pTss->edx, pTss->esi, pTss->edi,
3417 pTss->eip, pTss->esp, pTss->ebp,
3418 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->fs, pTss->gs, pTss->eflags,
3419 pTss->ss0, pTss->esp0, pTss->ss1, pTss->esp1, pTss->ss2, pTss->esp2,
3420 pTss->selPrev, pTss->selLdt, pTss->cr3, pTss->fDebugTrap, pTss->offIoBitmap);
3421 if (pTss->cs != 0)
3422 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pTss->cs, pTss->eip);
3423 offIoBitmap = pTss->offIoBitmap;
3424 break;
3425 }
3426
3427 case kTss64:
3428 {
3429 PCX86TSS64 pTss = (PCX86TSS64)&abBuf[0];
3430 if (SelTss != UINT32_MAX)
3431 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS64 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
3432 else
3433 DBGCCmdHlpPrintf(pCmdHlp, "TSS64 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
3434 DBGCCmdHlpPrintf(pCmdHlp,
3435 "rsp0=%016RX16 rsp1=%016RX16 rsp2=%016RX16\n"
3436 "ist1=%016RX16 ist2=%016RX16\n"
3437 "ist3=%016RX16 ist4=%016RX16\n"
3438 "ist5=%016RX16 ist6=%016RX16\n"
3439 "ist7=%016RX16 iomap=%04x\n"
3440 ,
3441 pTss->rsp0, pTss->rsp1, pTss->rsp2,
3442 pTss->ist1, pTss->ist2,
3443 pTss->ist3, pTss->ist4,
3444 pTss->ist5, pTss->ist6,
3445 pTss->ist7, pTss->offIoBitmap);
3446 offIoBitmap = pTss->offIoBitmap;
3447 break;
3448 }
3449
3450 default:
3451 AssertFailedReturn(VERR_INTERNAL_ERROR);
3452 }
3453
3454 /*
3455 * Dump the interrupt redirection bitmap.
3456 */
3457 if (enmTssType != kTss16)
3458 {
3459 if ( offIoBitmap > cbTssMin
3460 && offIoBitmap < cbTss) /** @todo check exactly what the edge cases are here. */
3461 {
3462 if (offIoBitmap - cbTssMin >= 32)
3463 {
3464 DBGCCmdHlpPrintf(pCmdHlp, "Interrupt redirection:\n");
3465 uint8_t const *pbIntRedirBitmap = &abBuf[offIoBitmap - 32];
3466 uint32_t iStart = 0;
3467 bool fPrev = ASMBitTest(pbIntRedirBitmap, 0); /* LE/BE issue */
3468 for (uint32_t i = 0; i < 256; i++)
3469 {
3470 bool fThis = ASMBitTest(pbIntRedirBitmap, i);
3471 if (fThis != fPrev)
3472 {
3473 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, i - 1, fPrev ? "Protected mode" : "Redirected");
3474 fPrev = fThis;
3475 iStart = i;
3476 }
3477 }
3478 if (iStart != 255)
3479 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, 255, fPrev ? "Protected mode" : "Redirected");
3480 }
3481 else
3482 DBGCCmdHlpPrintf(pCmdHlp, "Invalid interrupt redirection bitmap size: %u (%#x), expected 32 bytes.\n",
3483 offIoBitmap - cbTssMin, offIoBitmap - cbTssMin);
3484 }
3485 else if (offIoBitmap > 0)
3486 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap (-%#x)\n", cbTssMin - offIoBitmap);
3487 else
3488 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap\n");
3489 }
3490
3491 /*
3492 * Dump the I/O bitmap if present.
3493 */
3494 if (enmTssType != kTss16)
3495 {
3496 if (offIoBitmap < cbTss)
3497 {
3498 uint32_t cPorts = RT_MIN((cbTss - offIoBitmap) * 8, _64K);
3499 DBGCVAR VarAddr;
3500 DBGCCmdHlpEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarTssAddr, offIoBitmap);
3501 DBGCCmdHlpPrintf(pCmdHlp, "I/O bitmap at %DV - %#x ports:\n", &VarAddr, cPorts);
3502
3503 uint8_t const *pbIoBitmap = &abBuf[offIoBitmap];
3504 uint32_t iStart = 0;
3505 bool fPrev = ASMBitTest(pbIoBitmap, 0);
3506 uint32_t cLine = 0;
3507 for (uint32_t i = 1; i < cPorts; i++)
3508 {
3509 bool fThis = ASMBitTest(pbIoBitmap, i);
3510 if (fThis != fPrev)
3511 {
3512 cLine++;
3513 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s%s", iStart, i-1,
3514 fPrev ? "GP" : "OK", (cLine % 6) == 0 ? "\n" : " ");
3515 fPrev = fThis;
3516 iStart = i;
3517 }
3518 }
3519 if (iStart != _64K-1)
3520 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s\n", iStart, _64K-1, fPrev ? "GP" : "OK");
3521 }
3522 else if (offIoBitmap > 0)
3523 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap (-%#x)\n", cbTssMin - offIoBitmap);
3524 else
3525 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap\n");
3526 }
3527
3528 return VINF_SUCCESS;
3529}
3530
3531
3532/**
3533 * The 'm' command.
3534 *
3535 * @returns VBox status.
3536 * @param pCmd Pointer to the command descriptor (as registered).
3537 * @param pCmdHlp Pointer to command helper functions.
3538 * @param pVM Pointer to the current VM (if any).
3539 * @param paArgs Pointer to (readonly) array of arguments.
3540 * @param cArgs Number of arguments in the array.
3541 */
3542static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3543{
3544 DBGCCmdHlpPrintf(pCmdHlp, "Address: %DV\n", &paArgs[0]);
3545 if (!pVM)
3546 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No VM.\n");
3547 return dbgcCmdDumpPageHierarchy(pCmd, pCmdHlp, pVM, paArgs, cArgs);
3548}
3549
3550
3551/**
3552 * Converts one or more variables into a byte buffer for a
3553 * given unit size.
3554 *
3555 * @returns VBox status codes:
3556 * @retval VERR_TOO_MUCH_DATA if the buffer is too small, bitched.
3557 * @retval VERR_INTERNAL_ERROR on bad variable type, bitched.
3558 * @retval VINF_SUCCESS on success.
3559 *
3560 * @param pvBuf The buffer to convert into.
3561 * @param pcbBuf The buffer size on input. The size of the result on output.
3562 * @param cbUnit The unit size to apply when converting.
3563 * The high bit is used to indicate unicode string.
3564 * @param paVars The array of variables to convert.
3565 * @param cVars The number of variables.
3566 */
3567int dbgcVarsToBytes(PDBGCCMDHLP pCmdHlp, void *pvBuf, uint32_t *pcbBuf, size_t cbUnit, PCDBGCVAR paVars, unsigned cVars)
3568{
3569 union
3570 {
3571 uint8_t *pu8;
3572 uint16_t *pu16;
3573 uint32_t *pu32;
3574 uint64_t *pu64;
3575 } u, uEnd;
3576 u.pu8 = (uint8_t *)pvBuf;
3577 uEnd.pu8 = u.pu8 + *pcbBuf;
3578
3579 unsigned i;
3580 for (i = 0; i < cVars && u.pu8 < uEnd.pu8; i++)
3581 {
3582 switch (paVars[i].enmType)
3583 {
3584 case DBGCVAR_TYPE_GC_FAR:
3585 case DBGCVAR_TYPE_GC_FLAT:
3586 case DBGCVAR_TYPE_GC_PHYS:
3587 case DBGCVAR_TYPE_HC_FLAT:
3588 case DBGCVAR_TYPE_HC_PHYS:
3589 case DBGCVAR_TYPE_NUMBER:
3590 {
3591 uint64_t u64 = paVars[i].u.u64Number;
3592 switch (cbUnit & 0x1f)
3593 {
3594 case 1:
3595 do
3596 {
3597 *u.pu8++ = u64;
3598 u64 >>= 8;
3599 } while (u64);
3600 break;
3601 case 2:
3602 do
3603 {
3604 *u.pu16++ = u64;
3605 u64 >>= 16;
3606 } while (u64);
3607 break;
3608 case 4:
3609 *u.pu32++ = u64;
3610 u64 >>= 32;
3611 if (u64)
3612 *u.pu32++ = u64;
3613 break;
3614 case 8:
3615 *u.pu64++ = u64;
3616 break;
3617 }
3618 break;
3619 }
3620
3621 case DBGCVAR_TYPE_STRING:
3622 case DBGCVAR_TYPE_SYMBOL:
3623 {
3624 const char *psz = paVars[i].u.pszString;
3625 size_t cbString = strlen(psz);
3626 if (cbUnit & RT_BIT_32(31))
3627 {
3628 /* Explode char to unit. */
3629 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8) * (cbUnit & 0x1f))
3630 {
3631 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3632 return VERR_TOO_MUCH_DATA;
3633 }
3634 while (*psz)
3635 {
3636 switch (cbUnit & 0x1f)
3637 {
3638 case 1: *u.pu8++ = *psz; break;
3639 case 2: *u.pu16++ = *psz; break;
3640 case 4: *u.pu32++ = *psz; break;
3641 case 8: *u.pu64++ = *psz; break;
3642 }
3643 psz++;
3644 }
3645 }
3646 else
3647 {
3648 /* Raw copy with zero padding if the size isn't aligned. */
3649 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8))
3650 {
3651 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3652 return VERR_TOO_MUCH_DATA;
3653 }
3654
3655 size_t cbCopy = cbString & ~(cbUnit - 1);
3656 memcpy(u.pu8, psz, cbCopy);
3657 u.pu8 += cbCopy;
3658 psz += cbCopy;
3659
3660 size_t cbReminder = cbString & (cbUnit - 1);
3661 if (cbReminder)
3662 {
3663 memcpy(u.pu8, psz, cbString & (cbUnit - 1));
3664 memset(u.pu8 + cbReminder, 0, cbUnit - cbReminder);
3665 u.pu8 += cbUnit;
3666 }
3667 }
3668 break;
3669 }
3670
3671 default:
3672 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3673 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INTERNAL_ERROR,
3674 "i=%d enmType=%d\n", i, paVars[i].enmType);
3675 return VERR_INTERNAL_ERROR;
3676 }
3677 }
3678 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3679 if (i != cVars)
3680 {
3681 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3682 return VERR_TOO_MUCH_DATA;
3683 }
3684 return VINF_SUCCESS;
3685}
3686
3687
3688/**
3689 * The 'eb', 'ew', 'ed' and 'eq' commands.
3690 *
3691 * @returns VBox status.
3692 * @param pCmd Pointer to the command descriptor (as registered).
3693 * @param pCmdHlp Pointer to command helper functions.
3694 * @param pVM Pointer to the current VM (if any).
3695 * @param paArgs Pointer to (readonly) array of arguments.
3696 * @param cArgs Number of arguments in the array.
3697 */
3698static DECLCALLBACK(int) dbgcCmdEditMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3699{
3700 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3701 unsigned iArg;
3702
3703 /*
3704 * Validate input.
3705 */
3706 if ( cArgs < 2
3707 || !DBGCVAR_ISPOINTER(paArgs[0].enmType))
3708 return DBGCCmdHlpFail(pCmdHlp, pCmd, "internal error: The parser doesn't do its job properly yet... It might help to use the '%%' operator.\n");
3709 for (iArg = 1; iArg < cArgs; iArg++)
3710 if (paArgs[iArg].enmType != DBGCVAR_TYPE_NUMBER)
3711 return DBGCCmdHlpFail(pCmdHlp, pCmd, "internal error: The parser doesn't do its job properly yet: Arg #%u is not a number.\n", iArg);
3712 if (!pVM)
3713 return DBGCCmdHlpFail(pCmdHlp, pCmd, "error: No VM.\n");
3714
3715 /*
3716 * Figure out the element size.
3717 */
3718 unsigned cbElement;
3719 switch (pCmd->pszCmd[1])
3720 {
3721 default:
3722 case 'b': cbElement = 1; break;
3723 case 'w': cbElement = 2; break;
3724 case 'd': cbElement = 4; break;
3725 case 'q': cbElement = 8; break;
3726 }
3727
3728 /*
3729 * Do setting.
3730 */
3731 DBGCVAR Addr = paArgs[0];
3732 for (iArg = 1;;)
3733 {
3734 size_t cbWritten;
3735 int rc = pCmdHlp->pfnMemWrite(pCmdHlp, pVM, &paArgs[iArg].u, cbElement, &Addr, &cbWritten);
3736 if (RT_FAILURE(rc))
3737 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Writing memory at %DV.\n", &Addr);
3738 if (cbWritten != cbElement)
3739 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Only wrote %u out of %u bytes!\n", cbWritten, cbElement);
3740
3741 /* advance. */
3742 iArg++;
3743 if (iArg >= cArgs)
3744 break;
3745 rc = DBGCCmdHlpEval(pCmdHlp, &Addr, "%Dv + %#x", &Addr, cbElement);
3746 if (RT_FAILURE(rc))
3747 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3748 }
3749
3750 return VINF_SUCCESS;
3751}
3752
3753
3754/**
3755 * Executes the search.
3756 *
3757 * @returns VBox status code.
3758 * @param pCmdHlp The command helpers.
3759 * @param pVM The VM handle.
3760 * @param pAddress The address to start searching from. (undefined on output)
3761 * @param cbRange The address range to search. Must not wrap.
3762 * @param pabBytes The byte pattern to search for.
3763 * @param cbBytes The size of the pattern.
3764 * @param cbUnit The search unit.
3765 * @param cMaxHits The max number of hits.
3766 * @param pResult Where to store the result if it's a function invocation.
3767 */
3768static int dbgcCmdWorkerSearchMemDoIt(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGFADDRESS pAddress, RTGCUINTPTR cbRange,
3769 const uint8_t *pabBytes, uint32_t cbBytes,
3770 uint32_t cbUnit, uint64_t cMaxHits, PDBGCVAR pResult)
3771{
3772 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3773
3774 /*
3775 * Do the search.
3776 */
3777 uint64_t cHits = 0;
3778 for (;;)
3779 {
3780 /* search */
3781 DBGFADDRESS HitAddress;
3782 int rc = DBGFR3MemScan(pVM, pDbgc->idCpu, pAddress, cbRange, 1, pabBytes, cbBytes, &HitAddress);
3783 if (RT_FAILURE(rc))
3784 {
3785 if (rc != VERR_DBGF_MEM_NOT_FOUND)
3786 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3MemScan\n");
3787
3788 /* update the current address so we can save it (later). */
3789 pAddress->off += cbRange;
3790 pAddress->FlatPtr += cbRange;
3791 cbRange = 0;
3792 break;
3793 }
3794
3795 /* report result */
3796 DBGCVAR VarCur;
3797 rc = DBGCCmdHlpVarFromDbgfAddr(pCmdHlp, &HitAddress, &VarCur);
3798 if (RT_FAILURE(rc))
3799 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGCCmdHlpVarFromDbgfAddr\n");
3800 if (!pResult)
3801 pCmdHlp->pfnExec(pCmdHlp, "db %DV LB 10", &VarCur);
3802 else
3803 DBGCVAR_ASSIGN(pResult, &VarCur);
3804
3805 /* advance */
3806 cbRange -= HitAddress.FlatPtr - pAddress->FlatPtr;
3807 *pAddress = HitAddress;
3808 pAddress->FlatPtr += cbBytes;
3809 pAddress->off += cbBytes;
3810 if (cbRange <= cbBytes)
3811 {
3812 cbRange = 0;
3813 break;
3814 }
3815 cbRange -= cbBytes;
3816
3817 if (++cHits >= cMaxHits)
3818 {
3819 /// @todo save the search.
3820 break;
3821 }
3822 }
3823
3824 /*
3825 * Save the search so we can resume it...
3826 */
3827 if (pDbgc->abSearch != pabBytes)
3828 {
3829 memcpy(pDbgc->abSearch, pabBytes, cbBytes);
3830 pDbgc->cbSearch = cbBytes;
3831 pDbgc->cbSearchUnit = cbUnit;
3832 }
3833 pDbgc->cMaxSearchHits = cMaxHits;
3834 pDbgc->SearchAddr = *pAddress;
3835 pDbgc->cbSearchRange = cbRange;
3836
3837 return cHits ? VINF_SUCCESS : VERR_DBGC_COMMAND_FAILED;
3838}
3839
3840
3841/**
3842 * Resumes the previous search.
3843 *
3844 * @returns VBox status code.
3845 * @param pCmdHlp Pointer to the command helper functions.
3846 * @param pVM Pointer to the current VM (if any).
3847 * @param pResult Where to store the result of a function invocation.
3848 */
3849static int dbgcCmdWorkerSearchMemResume(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGCVAR pResult)
3850{
3851 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3852
3853 /*
3854 * Make sure there is a previous command.
3855 */
3856 if (!pDbgc->cbSearch)
3857 {
3858 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Error: No previous search\n");
3859 return VERR_DBGC_COMMAND_FAILED;
3860 }
3861
3862 /*
3863 * Make range and address adjustments.
3864 */
3865 DBGFADDRESS Address = pDbgc->SearchAddr;
3866 if (Address.FlatPtr == ~(RTGCUINTPTR)0)
3867 {
3868 Address.FlatPtr -= Address.off;
3869 Address.off = 0;
3870 }
3871
3872 RTGCUINTPTR cbRange = pDbgc->cbSearchRange;
3873 if (!cbRange)
3874 cbRange = ~(RTGCUINTPTR)0;
3875 if (Address.FlatPtr + cbRange < pDbgc->SearchAddr.FlatPtr)
3876 cbRange = ~(RTGCUINTPTR)0 - pDbgc->SearchAddr.FlatPtr + !!pDbgc->SearchAddr.FlatPtr;
3877
3878 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, pDbgc->abSearch, pDbgc->cbSearch,
3879 pDbgc->cbSearchUnit, pDbgc->cMaxSearchHits, pResult);
3880}
3881
3882
3883/**
3884 * Search memory, worker for the 's' and 's?' functions.
3885 *
3886 * @returns VBox status.
3887 * @param pCmdHlp Pointer to the command helper functions.
3888 * @param pVM Pointer to the current VM (if any).
3889 * @param pAddress Where to start searching. If no range, search till end of address space.
3890 * @param cMaxHits The maximum number of hits.
3891 * @param chType The search type.
3892 * @param paPatArgs The pattern variable array.
3893 * @param cPatArgs Number of pattern variables.
3894 * @param pResult Where to store the result of a function invocation.
3895 */
3896static int dbgcCmdWorkerSearchMem(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pAddress, uint64_t cMaxHits, char chType,
3897 PCDBGCVAR paPatArgs, unsigned cPatArgs, PDBGCVAR pResult)
3898{
3899 DBGCVAR_INIT_GC_FLAT(pResult, 0);
3900
3901 /*
3902 * Convert the search pattern into bytes and DBGFR3MemScan can deal with.
3903 */
3904 uint32_t cbUnit;
3905 switch (chType)
3906 {
3907 case 'a':
3908 case 'b': cbUnit = 1; break;
3909 case 'u': cbUnit = 2 | RT_BIT_32(31); break;
3910 case 'w': cbUnit = 2; break;
3911 case 'd': cbUnit = 4; break;
3912 case 'q': cbUnit = 8; break;
3913 default:
3914 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "chType=%c\n", chType);
3915 }
3916 uint8_t abBytes[RT_SIZEOFMEMB(DBGC, abSearch)];
3917 uint32_t cbBytes = sizeof(abBytes);
3918 int rc = dbgcVarsToBytes(pCmdHlp, abBytes, &cbBytes, cbUnit, paPatArgs, cPatArgs);
3919 if (RT_FAILURE(rc))
3920 return VERR_DBGC_COMMAND_FAILED;
3921
3922 /*
3923 * Make DBGF address and fix the range.
3924 */
3925 DBGFADDRESS Address;
3926 rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, pAddress, &Address);
3927 if (RT_FAILURE(rc))
3928 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", pAddress);
3929
3930 RTGCUINTPTR cbRange;
3931 switch (pAddress->enmRangeType)
3932 {
3933 case DBGCVAR_RANGE_BYTES:
3934 cbRange = pAddress->u64Range;
3935 if (cbRange != pAddress->u64Range)
3936 cbRange = ~(RTGCUINTPTR)0;
3937 break;
3938
3939 case DBGCVAR_RANGE_ELEMENTS:
3940 cbRange = (RTGCUINTPTR)(pAddress->u64Range * cbUnit);
3941 if ( cbRange != pAddress->u64Range * cbUnit
3942 || cbRange < pAddress->u64Range)
3943 cbRange = ~(RTGCUINTPTR)0;
3944 break;
3945
3946 default:
3947 cbRange = ~(RTGCUINTPTR)0;
3948 break;
3949 }
3950 if (Address.FlatPtr + cbRange < Address.FlatPtr)
3951 cbRange = ~(RTGCUINTPTR)0 - Address.FlatPtr + !!Address.FlatPtr;
3952
3953 /*
3954 * Ok, do it.
3955 */
3956 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, abBytes, cbBytes, cbUnit, cMaxHits, pResult);
3957}
3958
3959
3960/**
3961 * The 's' command.
3962 *
3963 * @returns VBox status.
3964 * @param pCmd Pointer to the command descriptor (as registered).
3965 * @param pCmdHlp Pointer to command helper functions.
3966 * @param pVM Pointer to the current VM (if any).
3967 * @param paArgs Pointer to (readonly) array of arguments.
3968 * @param cArgs Number of arguments in the array.
3969 */
3970static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3971{
3972 /* check that the parser did what it's supposed to do. */
3973 //if ( cArgs <= 2
3974 // && paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3975 // return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
3976
3977 /*
3978 * Repeat previous search?
3979 */
3980 if (cArgs == 0)
3981 return dbgcCmdWorkerSearchMemResume(pCmdHlp, pVM, NULL);
3982
3983 /*
3984 * Parse arguments.
3985 */
3986
3987 return -1;
3988}
3989
3990
3991/**
3992 * The 's?' command.
3993 *
3994 * @returns VBox status.
3995 * @param pCmd Pointer to the command descriptor (as registered).
3996 * @param pCmdHlp Pointer to command helper functions.
3997 * @param pVM Pointer to the current VM (if any).
3998 * @param paArgs Pointer to (readonly) array of arguments.
3999 * @param cArgs Number of arguments in the array.
4000 */
4001static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
4002{
4003 /* check that the parser did what it's supposed to do. */
4004 if ( cArgs < 2
4005 || !DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
4006 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
4007 return dbgcCmdWorkerSearchMem(pCmdHlp, pVM, &paArgs[0], 25, pCmd->pszCmd[1], paArgs + 1, cArgs - 1, NULL);
4008}
4009
4010
4011/**
4012 * List near symbol.
4013 *
4014 * @returns VBox status code.
4015 * @param pCmdHlp Pointer to command helper functions.
4016 * @param pVM Pointer to the current VM (if any).
4017 * @param pArg Pointer to the address or symbol to lookup.
4018 */
4019static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pArg)
4020{
4021 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4022
4023 RTDBGSYMBOL Symbol;
4024 int rc;
4025 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
4026 {
4027 /*
4028 * Lookup the symbol address.
4029 */
4030 rc = DBGFR3AsSymbolByName(pVM, pDbgc->hDbgAs, pArg->u.pszString, &Symbol, NULL);
4031 if (RT_FAILURE(rc))
4032 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3AsSymbolByName(,,%s,)\n", pArg->u.pszString);
4033
4034 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%Rptr %s\n", Symbol.Value, Symbol.szName);
4035 }
4036 else
4037 {
4038 /*
4039 * Convert it to a flat GC address and lookup that address.
4040 */
4041 DBGCVAR AddrVar;
4042 rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
4043 if (RT_FAILURE(rc))
4044 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
4045
4046 RTINTPTR offDisp;
4047 DBGFADDRESS Addr;
4048 rc = DBGFR3AsSymbolByAddr(pVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pVM, &Addr, AddrVar.u.GCFlat), &offDisp, &Symbol, NULL);
4049 if (RT_FAILURE(rc))
4050 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3ASymbolByAddr(,,%RGv,,)\n", AddrVar.u.GCFlat);
4051
4052 if (!offDisp)
4053 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s", &AddrVar, Symbol.szName);
4054 else if (offDisp > 0)
4055 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
4056 else
4057 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
4058 if ((RTGCINTPTR)Symbol.cb > -offDisp)
4059 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " LB %RGv\n", Symbol.cb + offDisp);
4060 else
4061 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
4062 }
4063
4064 return rc;
4065}
4066
4067
4068/**
4069 * The 'ln' (listnear) command.
4070 *
4071 * @returns VBox status.
4072 * @param pCmd Pointer to the command descriptor (as registered).
4073 * @param pCmdHlp Pointer to command helper functions.
4074 * @param pVM Pointer to the current VM (if any).
4075 * @param paArgs Pointer to (readonly) array of arguments.
4076 * @param cArgs Number of arguments in the array.
4077 */
4078static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
4079{
4080 if (!cArgs)
4081 {
4082 /*
4083 * Current cs:eip symbol.
4084 */
4085 DBGCVAR AddrVar;
4086 int rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, "%%(cs:eip)");
4087 if (RT_FAILURE(rc))
4088 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(cs:eip)\n");
4089 return dbgcDoListNear(pCmdHlp, pVM, &AddrVar);
4090 }
4091
4092/** @todo Fix the darn parser, it's resolving symbols specified as arguments before we get in here. */
4093 /*
4094 * Iterate arguments.
4095 */
4096 for (unsigned iArg = 0; iArg < cArgs; iArg++)
4097 {
4098 int rc = dbgcDoListNear(pCmdHlp, pVM, &paArgs[iArg]);
4099 if (RT_FAILURE(rc))
4100 return rc;
4101 }
4102
4103 NOREF(pCmd);
4104 return VINF_SUCCESS;
4105}
4106
4107
4108/**
4109 * Matches the module patters against a module name.
4110 *
4111 * @returns true if matching, otherwise false.
4112 * @param pszName The module name.
4113 * @param paArgs The module pattern argument list.
4114 * @param cArgs Number of arguments.
4115 */
4116static bool dbgcCmdListModuleMatch(const char *pszName, PCDBGCVAR paArgs, unsigned cArgs)
4117{
4118 for (uint32_t i = 0; i < cArgs; i++)
4119 if (RTStrSimplePatternMatch(paArgs[i].u.pszString, pszName))
4120 return true;
4121 return false;
4122}
4123
4124
4125/**
4126 * The 'ln' (listnear) command.
4127 *
4128 * @returns VBox status.
4129 * @param pCmd Pointer to the command descriptor (as registered).
4130 * @param pCmdHlp Pointer to command helper functions.
4131 * @param pVM Pointer to the current VM (if any).
4132 * @param paArgs Pointer to (readonly) array of arguments.
4133 * @param cArgs Number of arguments in the array.
4134 */
4135static DECLCALLBACK(int) dbgcCmdListModules(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
4136{
4137 bool const fMappings = pCmd->pszCmd[2] == 'o';
4138 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4139
4140 /*
4141 * Iterate the modules in the current address space and print info about
4142 * those matching the input.
4143 */
4144 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pVM, pDbgc->hDbgAs);
4145 uint32_t cMods = RTDbgAsModuleCount(hAs);
4146 for (uint32_t iMod = 0; iMod < cMods; iMod++)
4147 {
4148 RTDBGMOD hMod = RTDbgAsModuleByIndex(hAs, iMod);
4149 if (hMod != NIL_RTDBGMOD)
4150 {
4151 uint32_t const cSegs = RTDbgModSegmentCount(hMod);
4152 const char * const pszName = RTDbgModName(hMod);
4153 if ( cArgs == 0
4154 || dbgcCmdListModuleMatch(pszName, paArgs, cArgs))
4155 {
4156 /*
4157 * Find the mapping with the lower address, preferring a full
4158 * image mapping, for the main line.
4159 */
4160 RTDBGASMAPINFO aMappings[128];
4161 uint32_t cMappings = RT_ELEMENTS(aMappings);
4162 int rc = RTDbgAsModuleQueryMapByIndex(hAs, iMod, &aMappings[0], &cMappings, 0 /*fFlags*/);
4163 if (RT_SUCCESS(rc))
4164 {
4165 bool fFull = false;
4166 RTUINTPTR uMin = RTUINTPTR_MAX;
4167 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
4168 if ( aMappings[iMap].Address < uMin
4169 && ( !fFull
4170 || aMappings[iMap].iSeg == NIL_RTDBGSEGIDX))
4171 uMin = aMappings[iMap].Address;
4172 DBGCCmdHlpPrintf(pCmdHlp, "%RGv %04x %s\n", (RTGCUINTPTR)uMin, cSegs, pszName);
4173
4174 if (fMappings)
4175 {
4176 /* sort by address first - not very efficient. */
4177 for (uint32_t i = 0; i + 1 < cMappings; i++)
4178 for (uint32_t j = i + 1; j < cMappings; j++)
4179 if (aMappings[j].Address < aMappings[i].Address)
4180 {
4181 RTDBGASMAPINFO Tmp = aMappings[j];
4182 aMappings[j] = aMappings[i];
4183 aMappings[i] = Tmp;
4184 }
4185
4186 /* print */
4187 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
4188 if (aMappings[iMap].iSeg != NIL_RTDBGSEGIDX)
4189 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv #%02x %s\n",
4190 (RTGCUINTPTR)aMappings[iMap].Address,
4191 (RTGCUINTPTR)RTDbgModSegmentSize(hMod, aMappings[iMap].iSeg),
4192 aMappings[iMap].iSeg,
4193 /** @todo RTDbgModSegmentName(hMod, aMappings[iMap].iSeg)*/ "noname");
4194 else
4195 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv <everything>\n",
4196 (RTGCUINTPTR)aMappings[iMap].Address,
4197 (RTGCUINTPTR)RTDbgModImageSize(hMod));
4198 }
4199 }
4200 else
4201 DBGCCmdHlpPrintf(pCmdHlp, "%.*s %04x %s (rc=%Rrc)\n",
4202 sizeof(RTGCPTR) * 2, "???????????", cSegs, pszName, rc);
4203 /** @todo missing address space API for enumerating the mappings. */
4204 }
4205 RTDbgModRelease(hMod);
4206 }
4207 }
4208 RTDbgAsRelease(hAs);
4209
4210 NOREF(pCmd);
4211 return VINF_SUCCESS;
4212}
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