VirtualBox

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

Last change on this file since 41562 was 41562, checked in by vboxsync, 13 years ago

build fix / adding hi() and low()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 183.0 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 41562 2012-06-04 12:54:57Z 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, ~0U, 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, ~0U, DBGCVAR_CAT_NUMBER, 0, "sel", "Selector or selector range." },
139 { 0, ~0U, 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, ~0U, 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, ~0U, 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, ~0U, 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, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
225 { 0, ~0U, 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, ~0U, 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, ~0U, 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, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Deletes a set of breakpoints." },
296 { "bd", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
297 { "be", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enables 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, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT)." },
309 { "dga", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT) including not-present entries." },
310 { "di", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT)." },
311 { "dia", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT) including not-present entries." },
312 { "dl", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT)." },
313 { "dla", 0, ~0U, &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 memory at given address as a page directory." },
316 { "dpdb", 0, 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 memory at given address as a 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, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules." },
344 { "lmo", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments." },
345 { "ln", 0, ~0U, &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, ~0U, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
355 { "sa", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
356 { "sb", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
357 { "sd", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
358 { "sq", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
359 { "su", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
360 { "sw", 2, ~0U, &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 uint32_t g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
371
372
373
374
375/**
376 * The 'go' command.
377 *
378 * @returns VBox status.
379 * @param pCmd Pointer to the command descriptor (as registered).
380 * @param pCmdHlp Pointer to command helper functions.
381 * @param pVM Pointer to the current VM (if any).
382 * @param paArgs Pointer to (readonly) array of arguments.
383 * @param cArgs Number of arguments in the array.
384 */
385static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
386{
387 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
388
389 /*
390 * Check if the VM is halted or not before trying to resume it.
391 */
392 if (!DBGFR3IsHalted(pVM))
393 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The VM is already running");
394
395 int rc = DBGFR3Resume(pVM);
396 if (RT_FAILURE(rc))
397 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3Resume");
398
399 NOREF(paArgs); NOREF(cArgs);
400 return VINF_SUCCESS;
401}
402
403
404/**
405 * The 'ba' command.
406 *
407 * @returns VBox status.
408 * @param pCmd Pointer to the command descriptor (as registered).
409 * @param pCmdHlp Pointer to command helper functions.
410 * @param pVM Pointer to the current VM (if any).
411 * @param paArgs Pointer to (readonly) array of arguments.
412 * @param cArgs Number of arguments in the array.
413 */
414static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
415{
416 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
417
418 /*
419 * Interpret access type.
420 */
421 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
422 || paArgs[0].u.pszString[1])
423 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'",
424 paArgs[0].u.pszString, pCmd->pszCmd);
425 uint8_t fType = 0;
426 switch (paArgs[0].u.pszString[0])
427 {
428 case 'x': fType = X86_DR7_RW_EO; break;
429 case 'r': fType = X86_DR7_RW_RW; break;
430 case 'w': fType = X86_DR7_RW_WO; break;
431 case 'i': fType = X86_DR7_RW_IO; break;
432 }
433
434 /*
435 * Validate size.
436 */
437 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
438 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 'x' access type requires size 1!",
439 paArgs[1].u.u64Number, pCmd->pszCmd);
440 switch (paArgs[1].u.u64Number)
441 {
442 case 1:
443 case 2:
444 case 4:
445 break;
446 /*case 8: - later*/
447 default:
448 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 1, 2 or 4!",
449 paArgs[1].u.u64Number, pCmd->pszCmd);
450 }
451 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
452
453 /*
454 * Convert the pointer to a DBGF address.
455 */
456 DBGFADDRESS Address;
457 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
458 if (RT_FAILURE(rc))
459 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%DV,)", &paArgs[2]);
460
461 /*
462 * Pick out the optional arguments.
463 */
464 uint64_t iHitTrigger = 0;
465 uint64_t iHitDisable = ~0;
466 const char *pszCmds = NULL;
467 unsigned iArg = 3;
468 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
469 {
470 iHitTrigger = paArgs[iArg].u.u64Number;
471 iArg++;
472 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
473 {
474 iHitDisable = paArgs[iArg].u.u64Number;
475 iArg++;
476 }
477 }
478 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
479 {
480 pszCmds = paArgs[iArg].u.pszString;
481 iArg++;
482 }
483
484 /*
485 * Try set the breakpoint.
486 */
487 uint32_t iBp;
488 rc = DBGFR3BpSetReg(pVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
489 if (RT_SUCCESS(rc))
490 {
491 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
492 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
493 if (RT_SUCCESS(rc))
494 return DBGCCmdHlpPrintf(pCmdHlp, "Set access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
495 if (rc == VERR_DBGC_BP_EXISTS)
496 {
497 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
498 if (RT_SUCCESS(rc))
499 return DBGCCmdHlpPrintf(pCmdHlp, "Updated access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
500 }
501 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
502 AssertRC(rc2);
503 }
504 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set access breakpoint at %RGv", Address.FlatPtr);
505}
506
507
508/**
509 * The 'bc' command.
510 *
511 * @returns VBox status.
512 * @param pCmd Pointer to the command descriptor (as registered).
513 * @param pCmdHlp Pointer to command helper functions.
514 * @param pVM Pointer to the current VM (if any).
515 * @param paArgs Pointer to (readonly) array of arguments.
516 * @param cArgs Number of arguments in the array.
517 */
518static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
519{
520 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
521
522 /*
523 * Enumerate the arguments.
524 */
525 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
526 int rc = VINF_SUCCESS;
527 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
528 {
529 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
530 {
531 /* one */
532 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
533 if (iBp == paArgs[iArg].u.u64Number)
534 {
535 int rc2 = DBGFR3BpClear(pVM, iBp);
536 if (RT_FAILURE(rc2))
537 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
538 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
539 dbgcBpDelete(pDbgc, iBp);
540 }
541 else
542 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
543 }
544 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
545 {
546 /* all */
547 PDBGCBP pBp = pDbgc->pFirstBp;
548 while (pBp)
549 {
550 uint32_t iBp = pBp->iBp;
551 pBp = pBp->pNext;
552
553 int rc2 = DBGFR3BpClear(pVM, iBp);
554 if (RT_FAILURE(rc2))
555 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
556 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
557 dbgcBpDelete(pDbgc, iBp);
558 }
559 }
560 else
561 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
562 }
563 return rc;
564}
565
566
567/**
568 * The 'bd' command.
569 *
570 * @returns VBox status.
571 * @param pCmd Pointer to the command descriptor (as registered).
572 * @param pCmdHlp Pointer to command helper functions.
573 * @param pVM Pointer to the current VM (if any).
574 * @param paArgs Pointer to (readonly) array of arguments.
575 * @param cArgs Number of arguments in the array.
576 */
577static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
578{
579 /*
580 * Enumerate the arguments.
581 */
582 int rc = VINF_SUCCESS;
583 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
584 {
585 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
586 {
587 /* one */
588 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
589 if (iBp == paArgs[iArg].u.u64Number)
590 {
591 rc = DBGFR3BpDisable(pVM, iBp);
592 if (RT_FAILURE(rc))
593 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpDisable failed for breakpoint %#x", iBp);
594 }
595 else
596 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
597 }
598 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
599 {
600 /* all */
601 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
602 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
603 {
604 int rc2 = DBGFR3BpDisable(pVM, pBp->iBp);
605 if (RT_FAILURE(rc2))
606 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpDisable failed for breakpoint %#x", pBp->iBp);
607 }
608 }
609 else
610 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
611 }
612 return rc;
613}
614
615
616/**
617 * The 'be' command.
618 *
619 * @returns VBox status.
620 * @param pCmd Pointer to the command descriptor (as registered).
621 * @param pCmdHlp Pointer to command helper functions.
622 * @param pVM Pointer to the current VM (if any).
623 * @param paArgs Pointer to (readonly) array of arguments.
624 * @param cArgs Number of arguments in the array.
625 */
626static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
627{
628 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
629
630 /*
631 * Enumerate the arguments.
632 */
633 int rc = VINF_SUCCESS;
634 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
635 {
636 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
637 {
638 /* one */
639 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
640 if (iBp == paArgs[iArg].u.u64Number)
641 {
642 rc = DBGFR3BpEnable(pVM, iBp);
643 if (RT_FAILURE(rc))
644 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnable failed for breakpoint %#x", iBp);
645 }
646 else
647 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
648 }
649 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
650 {
651 /* all */
652 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
653 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
654 {
655 int rc2 = DBGFR3BpEnable(pVM, pBp->iBp);
656 if (RT_FAILURE(rc2))
657 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpEnable failed for breakpoint %#x", pBp->iBp);
658 }
659 }
660 else
661 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
662 }
663 return rc;
664}
665
666
667/**
668 * Breakpoint enumeration callback function.
669 *
670 * @returns VBox status code. Any failure will stop the enumeration.
671 * @param pVM The VM handle.
672 * @param pvUser The user argument.
673 * @param pBp Pointer to the breakpoint information. (readonly)
674 */
675static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PVM pVM, void *pvUser, PCDBGFBP pBp)
676{
677 PDBGC pDbgc = (PDBGC)pvUser;
678 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
679
680 /*
681 * BP type and size.
682 */
683 char chType;
684 char cb = 1;
685 switch (pBp->enmType)
686 {
687 case DBGFBPTYPE_INT3:
688 chType = 'p';
689 break;
690 case DBGFBPTYPE_REG:
691 switch (pBp->u.Reg.fType)
692 {
693 case X86_DR7_RW_EO: chType = 'x'; break;
694 case X86_DR7_RW_WO: chType = 'w'; break;
695 case X86_DR7_RW_IO: chType = 'i'; break;
696 case X86_DR7_RW_RW: chType = 'r'; break;
697 default: chType = '?'; break;
698
699 }
700 cb = pBp->u.Reg.cb;
701 break;
702 case DBGFBPTYPE_REM:
703 chType = 'r';
704 break;
705 default:
706 chType = '?';
707 break;
708 }
709
710 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%#4x %c %d %c %RGv %04RX64 (%04RX64 to ",
711 pBp->iBp, pBp->fEnabled ? 'e' : 'd', (int)cb, chType,
712 pBp->GCPtr, pBp->cHits, pBp->iHitTrigger);
713 if (pBp->iHitDisable == ~(uint64_t)0)
714 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "~0) ");
715 else
716 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%04RX64)", pBp->iHitDisable);
717
718 /*
719 * Try resolve the address.
720 */
721 RTDBGSYMBOL Sym;
722 RTINTPTR off;
723 DBGFADDRESS Addr;
724 int rc = DBGFR3AsSymbolByAddr(pVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pVM, &Addr, pBp->GCPtr), &off, &Sym, NULL);
725 if (RT_SUCCESS(rc))
726 {
727 if (!off)
728 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s", Sym.szName);
729 else if (off > 0)
730 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, off);
731 else
732 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, -off);
733 }
734
735 /*
736 * The commands.
737 */
738 if (pDbgcBp)
739 {
740 if (pDbgcBp->cchCmd)
741 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n cmds: '%s'\n", pDbgcBp->szCmd);
742 else
743 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n");
744 }
745 else
746 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " [unknown bp]\n");
747
748 return VINF_SUCCESS;
749}
750
751
752/**
753 * The 'bl' command.
754 *
755 * @returns VBox status.
756 * @param pCmd Pointer to the command descriptor (as registered).
757 * @param pCmdHlp Pointer to command helper functions.
758 * @param pVM Pointer to the current VM (if any).
759 * @param paArgs Pointer to (readonly) array of arguments.
760 * @param cArgs Number of arguments in the array.
761 */
762static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR /*paArgs*/, unsigned cArgs)
763{
764 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
765 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
766
767 /*
768 * Enumerate the breakpoints.
769 */
770 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
771 int rc = DBGFR3BpEnum(pVM, dbgcEnumBreakpointsCallback, pDbgc);
772 if (RT_FAILURE(rc))
773 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnum");
774 return rc;
775}
776
777
778/**
779 * The 'bp' command.
780 *
781 * @returns VBox status.
782 * @param pCmd Pointer to the command descriptor (as registered).
783 * @param pCmdHlp Pointer to command helper functions.
784 * @param pVM Pointer to the current VM (if any).
785 * @param paArgs Pointer to (readonly) array of arguments.
786 * @param cArgs Number of arguments in the array.
787 */
788static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
789{
790 /*
791 * Convert the pointer to a DBGF address.
792 */
793 DBGFADDRESS Address;
794 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
795 if (RT_FAILURE(rc))
796 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
797
798 /*
799 * Pick out the optional arguments.
800 */
801 uint64_t iHitTrigger = 0;
802 uint64_t iHitDisable = ~0;
803 const char *pszCmds = NULL;
804 unsigned iArg = 1;
805 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
806 {
807 iHitTrigger = paArgs[iArg].u.u64Number;
808 iArg++;
809 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
810 {
811 iHitDisable = paArgs[iArg].u.u64Number;
812 iArg++;
813 }
814 }
815 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
816 {
817 pszCmds = paArgs[iArg].u.pszString;
818 iArg++;
819 }
820
821 /*
822 * Try set the breakpoint.
823 */
824 uint32_t iBp;
825 rc = DBGFR3BpSet(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
826 if (RT_SUCCESS(rc))
827 {
828 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
829 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
830 if (RT_SUCCESS(rc))
831 return DBGCCmdHlpPrintf(pCmdHlp, "Set breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
832 if (rc == VERR_DBGC_BP_EXISTS)
833 {
834 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
835 if (RT_SUCCESS(rc))
836 return DBGCCmdHlpPrintf(pCmdHlp, "Updated breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
837 }
838 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
839 AssertRC(rc2);
840 }
841 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set breakpoint at %RGv", Address.FlatPtr);
842}
843
844
845/**
846 * The 'br' command.
847 *
848 * @returns VBox status.
849 * @param pCmd Pointer to the command descriptor (as registered).
850 * @param pCmdHlp Pointer to command helper functions.
851 * @param pVM Pointer to the current VM (if any).
852 * @param paArgs Pointer to (readonly) array of arguments.
853 * @param cArgs Number of arguments in the array.
854 */
855static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
856{
857 /*
858 * Convert the pointer to a DBGF address.
859 */
860 DBGFADDRESS Address;
861 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
862 if (RT_FAILURE(rc))
863 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
864
865 /*
866 * Pick out the optional arguments.
867 */
868 uint64_t iHitTrigger = 0;
869 uint64_t iHitDisable = ~0;
870 const char *pszCmds = NULL;
871 unsigned iArg = 1;
872 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
873 {
874 iHitTrigger = paArgs[iArg].u.u64Number;
875 iArg++;
876 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
877 {
878 iHitDisable = paArgs[iArg].u.u64Number;
879 iArg++;
880 }
881 }
882 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
883 {
884 pszCmds = paArgs[iArg].u.pszString;
885 iArg++;
886 }
887
888 /*
889 * Try set the breakpoint.
890 */
891 uint32_t iBp;
892 rc = DBGFR3BpSetREM(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
893 if (RT_SUCCESS(rc))
894 {
895 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
896 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
897 if (RT_SUCCESS(rc))
898 return DBGCCmdHlpPrintf(pCmdHlp, "Set REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
899 if (rc == VERR_DBGC_BP_EXISTS)
900 {
901 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
902 if (RT_SUCCESS(rc))
903 return DBGCCmdHlpPrintf(pCmdHlp, "Updated REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
904 }
905 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
906 AssertRC(rc2);
907 }
908 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set REM breakpoint at %RGv", Address.FlatPtr);
909}
910
911
912/**
913 * The 'u' command.
914 *
915 * @returns VBox status.
916 * @param pCmd Pointer to the command descriptor (as registered).
917 * @param pCmdHlp Pointer to command helper functions.
918 * @param pVM Pointer to the current VM (if any).
919 * @param paArgs Pointer to (readonly) array of arguments.
920 * @param cArgs Number of arguments in the array.
921 */
922static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
923{
924 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
925
926 /*
927 * Validate input.
928 */
929 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
930 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
931 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
932
933 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
934 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
935
936 /*
937 * Check the desired mode.
938 */
939 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS;
940 switch (pCmd->pszCmd[1])
941 {
942 default: AssertFailed();
943 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
944 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
945 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
946 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
947 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
948 }
949
950 /*
951 * Find address.
952 */
953 if (!cArgs)
954 {
955 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
956 {
957 /** @todo Batch query CS, RIP & CPU mode. */
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 DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
996 break;
997
998 case DBGCVAR_RANGE_BYTES:
999 if (pDbgc->DisasmPos.u64Range > 65536)
1000 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
1001 break;
1002
1003 default:
1004 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", 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 DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
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 = DBGCCmdHlpPrintf(pCmdHlp, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1064 if (RT_FAILURE(rc))
1065 return rc;
1066 }
1067 else
1068 {
1069 /* bitch. */
1070 rc = DBGCCmdHlpPrintf(pCmdHlp, "Failed to disassemble instruction, skipping one byte.\n");
1071 if (RT_FAILURE(rc))
1072 return rc;
1073 if (cTries-- > 0)
1074 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Too many disassembly failures. Giving up");
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 DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpEval(,,'(%Dv) + %x')", &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 VINF_SUCCESS;
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} dr7=%08VR{dr7}\n"
1490 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr2}\n"
1491 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr3} cr4=%08VR{cr4}\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 "DownRW", /* 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 "DownRW", /* 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 /* If it's a number (not an address), it's an index, so convert it to an address. */
2601 Assert(pCmd->pszCmd[3] != 'a');
2602 VarDefault = paArgs[0];
2603 if (fPAE)
2604 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
2605 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
2606 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
2607 VarDefault.u.u64Number <<= X86_PD_SHIFT;
2608 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
2609 paArgs = &VarDefault;
2610 }
2611
2612 /*
2613 * Locate the PDE to start displaying at.
2614 *
2615 * The 'dpda' command takes the address of a PDE, while the others are guest
2616 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
2617 * while the others require us to do all the tedious walking thru the paging
2618 * hierarchy to find the intended PDE.
2619 */
2620 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
2621 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
2622 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
2623 unsigned cEntries; /* The number of entries to display. */
2624 unsigned cEntriesMax; /* The max number of entries to display. */
2625 int rc;
2626 if (pCmd->pszCmd[3] == 'a')
2627 {
2628 VarPDEAddr = paArgs[0];
2629 switch (VarPDEAddr.enmRangeType)
2630 {
2631 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
2632 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
2633 default: cEntries = 10; break;
2634 }
2635 cEntriesMax = PAGE_SIZE / cbEntry;
2636 }
2637 else
2638 {
2639 /*
2640 * Determine the range.
2641 */
2642 switch (paArgs[0].enmRangeType)
2643 {
2644 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2645 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2646 default: cEntries = 10; break;
2647 }
2648
2649 /*
2650 * Normalize the input address, it must be a flat GC address.
2651 */
2652 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2653 if (RT_FAILURE(rc))
2654 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2655 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2656 {
2657 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2658 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2659 }
2660 if (fPAE)
2661 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
2662 else
2663 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
2664
2665 /*
2666 * Do the paging walk until we get to the page directory.
2667 */
2668 DBGCVAR VarCur;
2669 if (fGuest)
2670 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2671 else
2672 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2673 if (fLME)
2674 {
2675 /* Page Map Level 4 Lookup. */
2676 /* Check if it's a valid address first? */
2677 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2678 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2679 X86PML4E Pml4e;
2680 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2681 if (RT_FAILURE(rc))
2682 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2683 if (!Pml4e.n.u1Present)
2684 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2685
2686 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2687 Assert(fPAE);
2688 }
2689 if (fPAE)
2690 {
2691 /* Page directory pointer table. */
2692 X86PDPE Pdpe;
2693 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2694 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2695 if (RT_FAILURE(rc))
2696 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2697 if (!Pdpe.n.u1Present)
2698 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2699
2700 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2701 VarPDEAddr = VarCur;
2702 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2703 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
2704 }
2705 else
2706 {
2707 /* 32-bit legacy - CR3 == page directory. */
2708 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
2709 VarPDEAddr = VarCur;
2710 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
2711 }
2712 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2713 }
2714
2715 /* adjust cEntries */
2716 cEntries = RT_MAX(1, cEntries);
2717 cEntries = RT_MIN(cEntries, cEntriesMax);
2718
2719 /*
2720 * The display loop.
2721 */
2722 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
2723 &VarPDEAddr, iEntry);
2724 do
2725 {
2726 /*
2727 * Read.
2728 */
2729 X86PDEPAE Pde;
2730 Pde.u = 0;
2731 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, cbEntry, &VarPDEAddr, NULL);
2732 if (RT_FAILURE(rc))
2733 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
2734
2735 /*
2736 * Display.
2737 */
2738 if (iEntry != ~0U)
2739 {
2740 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2741 iEntry++;
2742 }
2743 if (fPSE && Pde.b.u1Size)
2744 DBGCCmdHlpPrintf(pCmdHlp,
2745 fPAE
2746 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2747 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2748 Pde.u,
2749 Pde.u & X86_PDE_PAE_PG_MASK,
2750 Pde.b.u1Present ? "p " : "np",
2751 Pde.b.u1Write ? "w" : "r",
2752 Pde.b.u1User ? "u" : "s",
2753 Pde.b.u1Accessed ? "a " : "na",
2754 Pde.b.u1Dirty ? "d " : "nd",
2755 Pde.b.u3Available,
2756 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
2757 Pde.b.u1WriteThru ? "pwt" : " ",
2758 Pde.b.u1CacheDisable ? "pcd" : " ",
2759 Pde.b.u1PAT ? "pat" : "",
2760 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2761 else
2762 DBGCCmdHlpPrintf(pCmdHlp,
2763 fPAE
2764 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
2765 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
2766 Pde.u,
2767 Pde.u & X86_PDE_PAE_PG_MASK,
2768 Pde.n.u1Present ? "p " : "np",
2769 Pde.n.u1Write ? "w" : "r",
2770 Pde.n.u1User ? "u" : "s",
2771 Pde.n.u1Accessed ? "a " : "na",
2772 Pde.u & RT_BIT(6) ? "6 " : " ",
2773 Pde.n.u3Available,
2774 Pde.u & RT_BIT(8) ? "8" : " ",
2775 Pde.n.u1WriteThru ? "pwt" : " ",
2776 Pde.n.u1CacheDisable ? "pcd" : " ",
2777 Pde.u & RT_BIT(7) ? "7" : "",
2778 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2779 if (Pde.u & UINT64_C(0x7fff000000000000))
2780 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
2781 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2782 if (RT_FAILURE(rc))
2783 return rc;
2784
2785 /*
2786 * Advance.
2787 */
2788 VarPDEAddr.u.u64Number += cbEntry;
2789 if (iEntry != ~0U)
2790 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
2791 } while (cEntries-- > 0);
2792
2793 return VINF_SUCCESS;
2794}
2795
2796
2797/**
2798 * The 'dpdb' command.
2799 *
2800 * @returns VBox status.
2801 * @param pCmd Pointer to the command descriptor (as registered).
2802 * @param pCmdHlp Pointer to command helper functions.
2803 * @param pVM Pointer to the current VM (if any).
2804 * @param paArgs Pointer to (readonly) array of arguments.
2805 * @param cArgs Number of arguments in the array.
2806 */
2807static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2808{
2809 if (!pVM)
2810 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2811 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2812 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2813 if (RT_FAILURE(rc1))
2814 return rc1;
2815 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2816 return rc2;
2817}
2818
2819
2820/**
2821 * The 'dph*' commands and main part of 'm'.
2822 *
2823 * @returns VBox status.
2824 * @param pCmd Pointer to the command descriptor (as registered).
2825 * @param pCmdHlp Pointer to command helper functions.
2826 * @param pVM Pointer to the current VM (if any).
2827 * @param paArgs Pointer to (readonly) array of arguments.
2828 * @param cArgs Number of arguments in the array.
2829 */
2830static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2831{
2832 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2833 if (!pVM)
2834 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No VM.\n");
2835
2836 /*
2837 * Figure the context and base flags.
2838 */
2839 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;
2840 if (pCmd->pszCmd[0] == 'm')
2841 fFlags |= DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW;
2842 else if (pCmd->pszCmd[3] == '\0')
2843 fFlags |= pDbgc->fRegCtxGuest ? DBGFPGDMP_FLAGS_GUEST : DBGFPGDMP_FLAGS_SHADOW;
2844 else if (pCmd->pszCmd[3] == 'g')
2845 fFlags |= DBGFPGDMP_FLAGS_GUEST;
2846 else if (pCmd->pszCmd[3] == 'h')
2847 fFlags |= DBGFPGDMP_FLAGS_SHADOW;
2848 else
2849 AssertFailed();
2850
2851 if (pDbgc->cPagingHierarchyDumps == 0)
2852 fFlags |= DBGFPGDMP_FLAGS_HEADER;
2853 pDbgc->cPagingHierarchyDumps = (pDbgc->cPagingHierarchyDumps + 1) % 42;
2854
2855 /*
2856 * Get the range.
2857 */
2858 PCDBGCVAR pRange = cArgs > 0 ? &paArgs[0] : pDbgc->pLastPos;
2859 RTGCPTR GCPtrFirst = NIL_RTGCPTR;
2860 int rc = DBGCCmdHlpVarToFlatAddr(pCmdHlp, pRange, &GCPtrFirst);
2861 if (RT_FAILURE(rc))
2862 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to convert %DV to a flat address: %Rrc", pRange, rc);
2863
2864 uint64_t cbRange;
2865 rc = DBGCCmdHlpVarGetRange(pCmdHlp, pRange, PAGE_SIZE, PAGE_SIZE * 8, &cbRange);
2866 if (RT_FAILURE(rc))
2867 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to obtain the range of %DV: %Rrc", pRange, rc);
2868
2869 RTGCPTR GCPtrLast = RTGCPTR_MAX - GCPtrFirst;
2870 if (cbRange >= GCPtrLast)
2871 GCPtrLast = RTGCPTR_MAX;
2872 else if (!cbRange)
2873 GCPtrLast = GCPtrFirst;
2874 else
2875 GCPtrLast = GCPtrFirst + cbRange - 1;
2876
2877 /*
2878 * Do we have a CR3?
2879 */
2880 uint64_t cr3 = 0;
2881 if (cArgs > 1)
2882 {
2883 if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
2884 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No CR3 or mode arguments when dumping both context, please.");
2885 if (paArgs[1].enmType != DBGCVAR_TYPE_NUMBER)
2886 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The CR3 argument is not a number: %DV", &paArgs[1]);
2887 cr3 = paArgs[1].u.u64Number;
2888 }
2889 else
2890 fFlags |= DBGFPGDMP_FLAGS_CURRENT_CR3;
2891
2892 /*
2893 * Do we have a mode?
2894 */
2895 if (cArgs > 2)
2896 {
2897 if (paArgs[2].enmType != DBGCVAR_TYPE_STRING)
2898 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The mode argument is not a string: %DV", &paArgs[2]);
2899 static const struct MODETOFLAGS
2900 {
2901 const char *pszName;
2902 uint32_t fFlags;
2903 } s_aModeToFlags[] =
2904 {
2905 { "ept", DBGFPGDMP_FLAGS_EPT },
2906 { "legacy", 0 },
2907 { "legacy-np", DBGFPGDMP_FLAGS_NP },
2908 { "pse", DBGFPGDMP_FLAGS_PSE },
2909 { "pse-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NP },
2910 { "pae", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE },
2911 { "pae-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NP },
2912 { "pae-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE },
2913 { "pae-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP },
2914 { "long", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME },
2915 { "long-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NP },
2916 { "long-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE },
2917 { "long-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP }
2918 };
2919 int i = RT_ELEMENTS(s_aModeToFlags);
2920 while (i-- > 0)
2921 if (!strcmp(s_aModeToFlags[i].pszName, paArgs[2].u.pszString))
2922 {
2923 fFlags |= s_aModeToFlags[i].fFlags;
2924 break;
2925 }
2926 if (i < 0)
2927 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown mode: \"%s\"", paArgs[2].u.pszString);
2928 }
2929 else
2930 fFlags |= DBGFPGDMP_FLAGS_CURRENT_MODE;
2931
2932 /*
2933 * Call the worker.
2934 */
2935 rc = DBGFR3PagingDumpEx(pVM, pDbgc->idCpu, fFlags, cr3, GCPtrFirst, GCPtrLast, 99 /*cMaxDepth*/,
2936 DBGCCmdHlpGetDbgfOutputHlp(pCmdHlp));
2937 if (RT_FAILURE(rc))
2938 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3PagingDumpEx: %Rrc\n", rc);
2939 return VINF_SUCCESS;
2940}
2941
2942
2943
2944/**
2945 * The 'dpg*' commands.
2946 *
2947 * @returns VBox status.
2948 * @param pCmd Pointer to the command descriptor (as registered).
2949 * @param pCmdHlp Pointer to command helper functions.
2950 * @param pVM Pointer to the current VM (if any).
2951 * @param paArgs Pointer to (readonly) array of arguments.
2952 * @param cArgs Number of arguments in the array.
2953 */
2954static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
2955{
2956 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2957
2958 /*
2959 * Validate input.
2960 */
2961 if ( cArgs != 1
2962 || (pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2963 || (pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2964 )
2965 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2966 if (!pVM)
2967 return DBGCCmdHlpPrintf(pCmdHlp, "error: No VM.\n");
2968
2969 /*
2970 * Guest or shadow page tables? Get the paging parameters.
2971 */
2972 bool fGuest = pCmd->pszCmd[3] != 'h';
2973 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2974 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2975 ? pDbgc->fRegCtxGuest
2976 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2977
2978 bool fPAE, fLME, fPSE, fPGE, fNXE;
2979 uint64_t cr3 = fGuest
2980 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2981 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2982 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2983
2984 /*
2985 * Locate the PTE to start displaying at.
2986 *
2987 * The 'dpta' command takes the address of a PTE, while the others are guest
2988 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
2989 * while the others require us to do all the tedious walking thru the paging
2990 * hierarchy to find the intended PTE.
2991 */
2992 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
2993 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
2994 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
2995 unsigned cEntries; /* The number of entries to display. */
2996 unsigned cEntriesMax; /* The max number of entries to display. */
2997 int rc;
2998 if (pCmd->pszCmd[3] == 'a')
2999 {
3000 VarPTEAddr = paArgs[0];
3001 switch (VarPTEAddr.enmRangeType)
3002 {
3003 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
3004 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
3005 default: cEntries = 10; break;
3006 }
3007 cEntriesMax = PAGE_SIZE / cbEntry;
3008 }
3009 else
3010 {
3011 /*
3012 * Determine the range.
3013 */
3014 switch (paArgs[0].enmRangeType)
3015 {
3016 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3017 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3018 default: cEntries = 10; break;
3019 }
3020
3021 /*
3022 * Normalize the input address, it must be a flat GC address.
3023 */
3024 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3025 if (RT_FAILURE(rc))
3026 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3027 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3028 {
3029 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3030 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3031 }
3032 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
3033
3034 /*
3035 * Do the paging walk until we get to the page table.
3036 */
3037 DBGCVAR VarCur;
3038 if (fGuest)
3039 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3040 else
3041 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3042 if (fLME)
3043 {
3044 /* Page Map Level 4 Lookup. */
3045 /* Check if it's a valid address first? */
3046 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3047 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3048 X86PML4E Pml4e;
3049 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3050 if (RT_FAILURE(rc))
3051 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3052 if (!Pml4e.n.u1Present)
3053 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3054
3055 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3056 Assert(fPAE);
3057 }
3058 if (fPAE)
3059 {
3060 /* Page directory pointer table. */
3061 X86PDPE Pdpe;
3062 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3063 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3064 if (RT_FAILURE(rc))
3065 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3066 if (!Pdpe.n.u1Present)
3067 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3068
3069 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3070
3071 /* Page directory (PAE). */
3072 X86PDEPAE Pde;
3073 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
3074 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
3075 if (RT_FAILURE(rc))
3076 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3077 if (!Pde.n.u1Present)
3078 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3079 if (fPSE && Pde.n.u1Size)
3080 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3081
3082 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
3083 VarPTEAddr = VarCur;
3084 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
3085 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
3086 }
3087 else
3088 {
3089 /* Page directory (legacy). */
3090 X86PDE Pde;
3091 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
3092 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarCur, NULL);
3093 if (RT_FAILURE(rc))
3094 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3095 if (!Pde.n.u1Present)
3096 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3097 if (fPSE && Pde.n.u1Size)
3098 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3099
3100 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
3101 VarPTEAddr = VarCur;
3102 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
3103 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
3104 }
3105 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3106 }
3107
3108 /* adjust cEntries */
3109 cEntries = RT_MAX(1, cEntries);
3110 cEntries = RT_MIN(cEntries, cEntriesMax);
3111
3112 /*
3113 * The display loop.
3114 */
3115 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
3116 &VarPTEAddr, &VarGCPtr, iEntry);
3117 do
3118 {
3119 /*
3120 * Read.
3121 */
3122 X86PTEPAE Pte;
3123 Pte.u = 0;
3124 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pte, cbEntry, &VarPTEAddr, NULL);
3125 if (RT_FAILURE(rc))
3126 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
3127
3128 /*
3129 * Display.
3130 */
3131 if (iEntry != ~0U)
3132 {
3133 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3134 iEntry++;
3135 }
3136 DBGCCmdHlpPrintf(pCmdHlp,
3137 fPAE
3138 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3139 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3140 Pte.u,
3141 Pte.u & X86_PTE_PAE_PG_MASK,
3142 Pte.n.u1Present ? "p " : "np",
3143 Pte.n.u1Write ? "w" : "r",
3144 Pte.n.u1User ? "u" : "s",
3145 Pte.n.u1Accessed ? "a " : "na",
3146 Pte.n.u1Dirty ? "d " : "nd",
3147 Pte.n.u3Available,
3148 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
3149 Pte.n.u1WriteThru ? "pwt" : " ",
3150 Pte.n.u1CacheDisable ? "pcd" : " ",
3151 Pte.n.u1PAT ? "pat" : " ",
3152 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
3153 );
3154 if (Pte.u & UINT64_C(0x7fff000000000000))
3155 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
3156 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3157 if (RT_FAILURE(rc))
3158 return rc;
3159
3160 /*
3161 * Advance.
3162 */
3163 VarPTEAddr.u.u64Number += cbEntry;
3164 if (iEntry != ~0U)
3165 VarGCPtr.u.GCFlat += PAGE_SIZE;
3166 } while (cEntries-- > 0);
3167
3168 return VINF_SUCCESS;
3169}
3170
3171
3172/**
3173 * The 'dptb' command.
3174 *
3175 * @returns VBox status.
3176 * @param pCmd Pointer to the command descriptor (as registered).
3177 * @param pCmdHlp Pointer to command helper functions.
3178 * @param pVM Pointer to the current VM (if any).
3179 * @param paArgs Pointer to (readonly) array of arguments.
3180 * @param cArgs Number of arguments in the array.
3181 */
3182static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3183{
3184 if (!pVM)
3185 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
3186 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
3187 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
3188 if (RT_FAILURE(rc1))
3189 return rc1;
3190 NOREF(pCmd); NOREF(cArgs);
3191 return rc2;
3192}
3193
3194
3195/**
3196 * The 'dt' command.
3197 *
3198 * @returns VBox status.
3199 * @param pCmd Pointer to the command descriptor (as registered).
3200 * @param pCmdHlp Pointer to command helper functions.
3201 * @param pVM Pointer to the current VM (if any).
3202 * @param paArgs Pointer to (readonly) array of arguments.
3203 * @param cArgs Number of arguments in the array.
3204 */
3205static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3206{
3207 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3208 int rc;
3209
3210 if (!pVM)
3211 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No VM.\n");
3212 if ( cArgs > 1
3213 || (cArgs == 1 && paArgs[0].enmType == DBGCVAR_TYPE_STRING)
3214 || (cArgs == 1 && paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL))
3215 return DBGCCmdHlpFail(pCmdHlp, pCmd, "internal error: The parser doesn't do its job properly yet...\n");
3216
3217 /*
3218 * Check if the command indicates the type.
3219 */
3220 enum { kTss16, kTss32, kTss64, kTssToBeDetermined } enmTssType = kTssToBeDetermined;
3221 if (!strcmp(pCmd->pszCmd, "dt16"))
3222 enmTssType = kTss16;
3223 else if (!strcmp(pCmd->pszCmd, "dt32"))
3224 enmTssType = kTss32;
3225 else if (!strcmp(pCmd->pszCmd, "dt64"))
3226 enmTssType = kTss64;
3227
3228 /*
3229 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
3230 */
3231 uint32_t SelTss = UINT32_MAX;
3232 DBGCVAR VarTssAddr;
3233 if (cArgs == 0)
3234 {
3235 /** @todo consider querying the hidden bits instead (missing API). */
3236 uint16_t SelTR;
3237 rc = DBGFR3RegCpuQueryU16(pVM, pDbgc->idCpu, DBGFREG_TR, &SelTR);
3238 if (RT_FAILURE(rc))
3239 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to query TR, rc=%Rrc\n", rc);
3240 DBGCVAR_INIT_GC_FAR(&VarTssAddr, SelTR, 0);
3241 SelTss = SelTR;
3242 }
3243 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3244 {
3245 if (paArgs[0].u.u64Number < 0xffff)
3246 DBGCVAR_INIT_GC_FAR(&VarTssAddr, (RTSEL)paArgs[0].u.u64Number, 0);
3247 else
3248 {
3249 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
3250 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Element count doesn't combine with a TSS address.\n");
3251 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, paArgs[0].u.u64Number);
3252 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_BYTES)
3253 {
3254 VarTssAddr.enmRangeType = paArgs[0].enmRangeType;
3255 VarTssAddr.u64Range = paArgs[0].u64Range;
3256 }
3257 }
3258 }
3259 else
3260 VarTssAddr = paArgs[0];
3261
3262 /*
3263 * Deal with TSS:ign by means of the GDT.
3264 */
3265 if (VarTssAddr.enmType == DBGCVAR_TYPE_GC_FAR)
3266 {
3267 SelTss = VarTssAddr.u.GCFar.sel;
3268 DBGFSELINFO SelInfo;
3269 rc = DBGFR3SelQueryInfo(pVM, pDbgc->idCpu, VarTssAddr.u.GCFar.sel, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
3270 if (RT_FAILURE(rc))
3271 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3SelQueryInfo(,%u,%d,,) -> %Rrc.\n",
3272 pDbgc->idCpu, VarTssAddr.u.GCFar.sel, rc);
3273
3274 if (SelInfo.u.Raw.Gen.u1DescType)
3275 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (!sys)\n", VarTssAddr.u.GCFar.sel);
3276
3277 switch (SelInfo.u.Raw.Gen.u4Type)
3278 {
3279 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
3280 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
3281 if (enmTssType == kTssToBeDetermined)
3282 enmTssType = kTss16;
3283 break;
3284
3285 case X86_SEL_TYPE_SYS_386_TSS_BUSY: /* AMD64 too */
3286 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
3287 if (enmTssType == kTssToBeDetermined)
3288 enmTssType = SelInfo.fFlags & DBGFSELINFO_FLAGS_LONG_MODE ? kTss64 : kTss32;
3289 break;
3290
3291 default:
3292 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (type=%x)\n",
3293 VarTssAddr.u.GCFar.sel, SelInfo.u.Raw.Gen.u4Type);
3294 }
3295
3296 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, SelInfo.GCPtrBase);
3297 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, RT_MAX(SelInfo.cbLimit + 1, SelInfo.cbLimit));
3298 }
3299
3300 /*
3301 * Determine the TSS type if none is currently given.
3302 */
3303 if (enmTssType == kTssToBeDetermined)
3304 {
3305 if ( VarTssAddr.u64Range > 0
3306 && VarTssAddr.u64Range < sizeof(X86TSS32) - 4)
3307 enmTssType = kTss16;
3308 else
3309 {
3310 uint64_t uEfer;
3311 rc = DBGFR3RegCpuQueryU64(pVM, pDbgc->idCpu, DBGFREG_MSR_K6_EFER, &uEfer);
3312 if ( RT_FAILURE(rc)
3313 || !(uEfer & MSR_K6_EFER_LMA) )
3314 enmTssType = kTss32;
3315 else
3316 enmTssType = kTss64;
3317 }
3318 }
3319
3320 /*
3321 * Figure the min/max sizes.
3322 * ASSUMES max TSS size is 64 KB.
3323 */
3324 uint32_t cbTssMin;
3325 uint32_t cbTssMax;
3326 switch (enmTssType)
3327 {
3328 case kTss16:
3329 cbTssMin = cbTssMax = sizeof(X86TSS16);
3330 break;
3331 case kTss32:
3332 cbTssMin = RT_OFFSETOF(X86TSS32, IntRedirBitmap);
3333 cbTssMax = _64K;
3334 break;
3335 case kTss64:
3336 cbTssMin = RT_OFFSETOF(X86TSS64, IntRedirBitmap);
3337 cbTssMax = _64K;
3338 break;
3339 default:
3340 AssertFailedReturn(VERR_INTERNAL_ERROR);
3341 }
3342 uint32_t cbTss = VarTssAddr.enmRangeType == DBGCVAR_RANGE_BYTES ? (uint32_t)VarTssAddr.u64Range : 0;
3343 if (cbTss == 0)
3344 cbTss = cbTssMin;
3345 else if (cbTss < cbTssMin)
3346 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Minimum TSS size is %u bytes, you specified %llu (%llx) bytes.\n",
3347 cbTssMin, VarTssAddr.u64Range, VarTssAddr.u64Range);
3348 else if (cbTss > cbTssMax)
3349 cbTss = cbTssMax;
3350 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, cbTss);
3351
3352 /*
3353 * Read the TSS into a temporary buffer.
3354 */
3355 uint8_t abBuf[_64K];
3356 size_t cbTssRead;
3357 rc = DBGCCmdHlpMemRead(pCmdHlp, pVM, abBuf, cbTss, &VarTssAddr, &cbTssRead);
3358 if (RT_FAILURE(rc))
3359 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read TSS at %Dv: %Rrc\n", &VarTssAddr, rc);
3360 if (cbTssRead < cbTssMin)
3361 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read essential parts of the TSS (read %zu, min %zu).\n",
3362 cbTssRead, cbTssMin);
3363 if (cbTssRead < cbTss)
3364 memset(&abBuf[cbTssRead], 0xff, cbTss - cbTssRead);
3365
3366
3367 /*
3368 * Format the TSS.
3369 */
3370 uint16_t offIoBitmap;
3371 switch (enmTssType)
3372 {
3373 case kTss16:
3374 {
3375 PCX86TSS16 pTss = (PCX86TSS16)&abBuf[0];
3376 if (SelTss != UINT32_MAX)
3377 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS16 at %Dv\n", SelTss, &VarTssAddr);
3378 else
3379 DBGCCmdHlpPrintf(pCmdHlp, "TSS16 at %Dv\n", &VarTssAddr);
3380 DBGCCmdHlpPrintf(pCmdHlp,
3381 "ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x\n"
3382 "ip=%04x sp=%04x bp=%04x\n"
3383 "cs=%04x ss=%04x ds=%04x es=%04x flags=%04x\n"
3384 "ss:sp0=%04x:%04x ss:sp1=%04x:%04x ss:sp2=%04x:%04x\n"
3385 "prev=%04x ldtr=%04x\n"
3386 ,
3387 pTss->ax, pTss->bx, pTss->cx, pTss->dx, pTss->si, pTss->di,
3388 pTss->ip, pTss->sp, pTss->bp,
3389 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->flags,
3390 pTss->ss0, pTss->sp0, pTss->ss1, pTss->sp1, pTss->ss2, pTss->sp2,
3391 pTss->selPrev, pTss->selLdt);
3392 if (pTss->cs != 0)
3393 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%04x L 0", pTss->cs, pTss->ip);
3394 offIoBitmap = 0;
3395 break;
3396 }
3397
3398 case kTss32:
3399 {
3400 PCX86TSS32 pTss = (PCX86TSS32)&abBuf[0];
3401 if (SelTss != UINT32_MAX)
3402 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS32 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
3403 else
3404 DBGCCmdHlpPrintf(pCmdHlp, "TSS32 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
3405 DBGCCmdHlpPrintf(pCmdHlp,
3406 "eax=%08x bx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
3407 "eip=%08x esp=%08x ebp=%08x\n"
3408 "cs=%04x ss=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n"
3409 "ss:esp0=%04x:%08x ss:esp1=%04x:%08x ss:esp2=%04x:%08x\n"
3410 "prev=%04x ldtr=%04x cr3=%08x debug=%u iomap=%04x\n"
3411 ,
3412 pTss->eax, pTss->ebx, pTss->ecx, pTss->edx, pTss->esi, pTss->edi,
3413 pTss->eip, pTss->esp, pTss->ebp,
3414 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->fs, pTss->gs, pTss->eflags,
3415 pTss->ss0, pTss->esp0, pTss->ss1, pTss->esp1, pTss->ss2, pTss->esp2,
3416 pTss->selPrev, pTss->selLdt, pTss->cr3, pTss->fDebugTrap, pTss->offIoBitmap);
3417 if (pTss->cs != 0)
3418 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pTss->cs, pTss->eip);
3419 offIoBitmap = pTss->offIoBitmap;
3420 break;
3421 }
3422
3423 case kTss64:
3424 {
3425 PCX86TSS64 pTss = (PCX86TSS64)&abBuf[0];
3426 if (SelTss != UINT32_MAX)
3427 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS64 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
3428 else
3429 DBGCCmdHlpPrintf(pCmdHlp, "TSS64 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
3430 DBGCCmdHlpPrintf(pCmdHlp,
3431 "rsp0=%016RX16 rsp1=%016RX16 rsp2=%016RX16\n"
3432 "ist1=%016RX16 ist2=%016RX16\n"
3433 "ist3=%016RX16 ist4=%016RX16\n"
3434 "ist5=%016RX16 ist6=%016RX16\n"
3435 "ist7=%016RX16 iomap=%04x\n"
3436 ,
3437 pTss->rsp0, pTss->rsp1, pTss->rsp2,
3438 pTss->ist1, pTss->ist2,
3439 pTss->ist3, pTss->ist4,
3440 pTss->ist5, pTss->ist6,
3441 pTss->ist7, pTss->offIoBitmap);
3442 offIoBitmap = pTss->offIoBitmap;
3443 break;
3444 }
3445
3446 default:
3447 AssertFailedReturn(VERR_INTERNAL_ERROR);
3448 }
3449
3450 /*
3451 * Dump the interrupt redirection bitmap.
3452 */
3453 if (enmTssType != kTss16)
3454 {
3455 if ( offIoBitmap > cbTssMin
3456 && offIoBitmap < cbTss) /** @todo check exactly what the edge cases are here. */
3457 {
3458 if (offIoBitmap - cbTssMin >= 32)
3459 {
3460 DBGCCmdHlpPrintf(pCmdHlp, "Interrupt redirection:\n");
3461 uint8_t const *pbIntRedirBitmap = &abBuf[offIoBitmap - 32];
3462 uint32_t iStart = 0;
3463 bool fPrev = ASMBitTest(pbIntRedirBitmap, 0); /* LE/BE issue */
3464 for (uint32_t i = 0; i < 256; i++)
3465 {
3466 bool fThis = ASMBitTest(pbIntRedirBitmap, i);
3467 if (fThis != fPrev)
3468 {
3469 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, i - 1, fPrev ? "Protected mode" : "Redirected");
3470 fPrev = fThis;
3471 iStart = i;
3472 }
3473 }
3474 if (iStart != 255)
3475 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, 255, fPrev ? "Protected mode" : "Redirected");
3476 }
3477 else
3478 DBGCCmdHlpPrintf(pCmdHlp, "Invalid interrupt redirection bitmap size: %u (%#x), expected 32 bytes.\n",
3479 offIoBitmap - cbTssMin, offIoBitmap - cbTssMin);
3480 }
3481 else if (offIoBitmap > 0)
3482 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap (-%#x)\n", cbTssMin - offIoBitmap);
3483 else
3484 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap\n");
3485 }
3486
3487 /*
3488 * Dump the I/O permission bitmap if present. The IOPM cannot start below offset 0x64
3489 * (that applies to both 32-bit and 64-bit TSSs since their size is the same).
3490 */
3491 if (enmTssType != kTss16)
3492 {
3493 if (offIoBitmap < cbTss && offIoBitmap >= 0x64)
3494 {
3495 uint32_t cPorts = RT_MIN((cbTss - offIoBitmap) * 8, _64K);
3496 DBGCVAR VarAddr;
3497 DBGCCmdHlpEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarTssAddr, offIoBitmap);
3498 DBGCCmdHlpPrintf(pCmdHlp, "I/O bitmap at %DV - %#x ports:\n", &VarAddr, cPorts);
3499
3500 uint8_t const *pbIoBitmap = &abBuf[offIoBitmap];
3501 uint32_t iStart = 0;
3502 bool fPrev = ASMBitTest(pbIoBitmap, 0);
3503 uint32_t cLine = 0;
3504 for (uint32_t i = 1; i < cPorts; i++)
3505 {
3506 bool fThis = ASMBitTest(pbIoBitmap, i);
3507 if (fThis != fPrev)
3508 {
3509 cLine++;
3510 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s%s", iStart, i-1,
3511 fPrev ? "GP" : "OK", (cLine % 6) == 0 ? "\n" : " ");
3512 fPrev = fThis;
3513 iStart = i;
3514 }
3515 }
3516 if (iStart != _64K-1)
3517 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s\n", iStart, _64K-1, fPrev ? "GP" : "OK");
3518 }
3519 else if (offIoBitmap > 0)
3520 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap (-%#x)\n", cbTssMin - offIoBitmap);
3521 else
3522 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap\n");
3523 }
3524
3525 return VINF_SUCCESS;
3526}
3527
3528
3529/**
3530 * The 'm' command.
3531 *
3532 * @returns VBox status.
3533 * @param pCmd Pointer to the command descriptor (as registered).
3534 * @param pCmdHlp Pointer to command helper functions.
3535 * @param pVM Pointer to the current VM (if any).
3536 * @param paArgs Pointer to (readonly) array of arguments.
3537 * @param cArgs Number of arguments in the array.
3538 */
3539static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3540{
3541 DBGCCmdHlpPrintf(pCmdHlp, "Address: %DV\n", &paArgs[0]);
3542 if (!pVM)
3543 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No VM.\n");
3544 return dbgcCmdDumpPageHierarchy(pCmd, pCmdHlp, pVM, paArgs, cArgs);
3545}
3546
3547
3548/**
3549 * Converts one or more variables into a byte buffer for a
3550 * given unit size.
3551 *
3552 * @returns VBox status codes:
3553 * @retval VERR_TOO_MUCH_DATA if the buffer is too small, bitched.
3554 * @retval VERR_INTERNAL_ERROR on bad variable type, bitched.
3555 * @retval VINF_SUCCESS on success.
3556 *
3557 * @param pvBuf The buffer to convert into.
3558 * @param pcbBuf The buffer size on input. The size of the result on output.
3559 * @param cbUnit The unit size to apply when converting.
3560 * The high bit is used to indicate unicode string.
3561 * @param paVars The array of variables to convert.
3562 * @param cVars The number of variables.
3563 */
3564int dbgcVarsToBytes(PDBGCCMDHLP pCmdHlp, void *pvBuf, uint32_t *pcbBuf, size_t cbUnit, PCDBGCVAR paVars, unsigned cVars)
3565{
3566 union
3567 {
3568 uint8_t *pu8;
3569 uint16_t *pu16;
3570 uint32_t *pu32;
3571 uint64_t *pu64;
3572 } u, uEnd;
3573 u.pu8 = (uint8_t *)pvBuf;
3574 uEnd.pu8 = u.pu8 + *pcbBuf;
3575
3576 unsigned i;
3577 for (i = 0; i < cVars && u.pu8 < uEnd.pu8; i++)
3578 {
3579 switch (paVars[i].enmType)
3580 {
3581 case DBGCVAR_TYPE_GC_FAR:
3582 case DBGCVAR_TYPE_GC_FLAT:
3583 case DBGCVAR_TYPE_GC_PHYS:
3584 case DBGCVAR_TYPE_HC_FLAT:
3585 case DBGCVAR_TYPE_HC_PHYS:
3586 case DBGCVAR_TYPE_NUMBER:
3587 {
3588 uint64_t u64 = paVars[i].u.u64Number;
3589 switch (cbUnit & 0x1f)
3590 {
3591 case 1:
3592 do
3593 {
3594 *u.pu8++ = u64;
3595 u64 >>= 8;
3596 } while (u64);
3597 break;
3598 case 2:
3599 do
3600 {
3601 *u.pu16++ = u64;
3602 u64 >>= 16;
3603 } while (u64);
3604 break;
3605 case 4:
3606 *u.pu32++ = u64;
3607 u64 >>= 32;
3608 if (u64)
3609 *u.pu32++ = u64;
3610 break;
3611 case 8:
3612 *u.pu64++ = u64;
3613 break;
3614 }
3615 break;
3616 }
3617
3618 case DBGCVAR_TYPE_STRING:
3619 case DBGCVAR_TYPE_SYMBOL:
3620 {
3621 const char *psz = paVars[i].u.pszString;
3622 size_t cbString = strlen(psz);
3623 if (cbUnit & RT_BIT_32(31))
3624 {
3625 /* Explode char to unit. */
3626 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8) * (cbUnit & 0x1f))
3627 {
3628 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3629 return VERR_TOO_MUCH_DATA;
3630 }
3631 while (*psz)
3632 {
3633 switch (cbUnit & 0x1f)
3634 {
3635 case 1: *u.pu8++ = *psz; break;
3636 case 2: *u.pu16++ = *psz; break;
3637 case 4: *u.pu32++ = *psz; break;
3638 case 8: *u.pu64++ = *psz; break;
3639 }
3640 psz++;
3641 }
3642 }
3643 else
3644 {
3645 /* Raw copy with zero padding if the size isn't aligned. */
3646 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8))
3647 {
3648 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3649 return VERR_TOO_MUCH_DATA;
3650 }
3651
3652 size_t cbCopy = cbString & ~(cbUnit - 1);
3653 memcpy(u.pu8, psz, cbCopy);
3654 u.pu8 += cbCopy;
3655 psz += cbCopy;
3656
3657 size_t cbReminder = cbString & (cbUnit - 1);
3658 if (cbReminder)
3659 {
3660 memcpy(u.pu8, psz, cbString & (cbUnit - 1));
3661 memset(u.pu8 + cbReminder, 0, cbUnit - cbReminder);
3662 u.pu8 += cbUnit;
3663 }
3664 }
3665 break;
3666 }
3667
3668 default:
3669 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3670 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INTERNAL_ERROR,
3671 "i=%d enmType=%d\n", i, paVars[i].enmType);
3672 return VERR_INTERNAL_ERROR;
3673 }
3674 }
3675 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3676 if (i != cVars)
3677 {
3678 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3679 return VERR_TOO_MUCH_DATA;
3680 }
3681 return VINF_SUCCESS;
3682}
3683
3684
3685/**
3686 * The 'eb', 'ew', 'ed' and 'eq' commands.
3687 *
3688 * @returns VBox status.
3689 * @param pCmd Pointer to the command descriptor (as registered).
3690 * @param pCmdHlp Pointer to command helper functions.
3691 * @param pVM Pointer to the current VM (if any).
3692 * @param paArgs Pointer to (readonly) array of arguments.
3693 * @param cArgs Number of arguments in the array.
3694 */
3695static DECLCALLBACK(int) dbgcCmdEditMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3696{
3697 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3698 unsigned iArg;
3699
3700 /*
3701 * Validate input.
3702 */
3703 if ( cArgs < 2
3704 || !DBGCVAR_ISPOINTER(paArgs[0].enmType))
3705 return DBGCCmdHlpFail(pCmdHlp, pCmd, "internal error: The parser doesn't do its job properly yet... It might help to use the '%%' operator.\n");
3706 for (iArg = 1; iArg < cArgs; iArg++)
3707 if (paArgs[iArg].enmType != DBGCVAR_TYPE_NUMBER)
3708 return DBGCCmdHlpFail(pCmdHlp, pCmd, "internal error: The parser doesn't do its job properly yet: Arg #%u is not a number.\n", iArg);
3709 if (!pVM)
3710 return DBGCCmdHlpFail(pCmdHlp, pCmd, "error: No VM.\n");
3711
3712 /*
3713 * Figure out the element size.
3714 */
3715 unsigned cbElement;
3716 switch (pCmd->pszCmd[1])
3717 {
3718 default:
3719 case 'b': cbElement = 1; break;
3720 case 'w': cbElement = 2; break;
3721 case 'd': cbElement = 4; break;
3722 case 'q': cbElement = 8; break;
3723 }
3724
3725 /*
3726 * Do setting.
3727 */
3728 DBGCVAR Addr = paArgs[0];
3729 for (iArg = 1;;)
3730 {
3731 size_t cbWritten;
3732 int rc = pCmdHlp->pfnMemWrite(pCmdHlp, pVM, &paArgs[iArg].u, cbElement, &Addr, &cbWritten);
3733 if (RT_FAILURE(rc))
3734 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Writing memory at %DV.\n", &Addr);
3735 if (cbWritten != cbElement)
3736 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Only wrote %u out of %u bytes!\n", cbWritten, cbElement);
3737
3738 /* advance. */
3739 iArg++;
3740 if (iArg >= cArgs)
3741 break;
3742 rc = DBGCCmdHlpEval(pCmdHlp, &Addr, "%Dv + %#x", &Addr, cbElement);
3743 if (RT_FAILURE(rc))
3744 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3745 }
3746
3747 return VINF_SUCCESS;
3748}
3749
3750
3751/**
3752 * Executes the search.
3753 *
3754 * @returns VBox status code.
3755 * @param pCmdHlp The command helpers.
3756 * @param pVM The VM handle.
3757 * @param pAddress The address to start searching from. (undefined on output)
3758 * @param cbRange The address range to search. Must not wrap.
3759 * @param pabBytes The byte pattern to search for.
3760 * @param cbBytes The size of the pattern.
3761 * @param cbUnit The search unit.
3762 * @param cMaxHits The max number of hits.
3763 * @param pResult Where to store the result if it's a function invocation.
3764 */
3765static int dbgcCmdWorkerSearchMemDoIt(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGFADDRESS pAddress, RTGCUINTPTR cbRange,
3766 const uint8_t *pabBytes, uint32_t cbBytes,
3767 uint32_t cbUnit, uint64_t cMaxHits, PDBGCVAR pResult)
3768{
3769 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3770
3771 /*
3772 * Do the search.
3773 */
3774 uint64_t cHits = 0;
3775 for (;;)
3776 {
3777 /* search */
3778 DBGFADDRESS HitAddress;
3779 int rc = DBGFR3MemScan(pVM, pDbgc->idCpu, pAddress, cbRange, 1, pabBytes, cbBytes, &HitAddress);
3780 if (RT_FAILURE(rc))
3781 {
3782 if (rc != VERR_DBGF_MEM_NOT_FOUND)
3783 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3MemScan\n");
3784
3785 /* update the current address so we can save it (later). */
3786 pAddress->off += cbRange;
3787 pAddress->FlatPtr += cbRange;
3788 cbRange = 0;
3789 break;
3790 }
3791
3792 /* report result */
3793 DBGCVAR VarCur;
3794 rc = DBGCCmdHlpVarFromDbgfAddr(pCmdHlp, &HitAddress, &VarCur);
3795 if (RT_FAILURE(rc))
3796 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGCCmdHlpVarFromDbgfAddr\n");
3797 if (!pResult)
3798 pCmdHlp->pfnExec(pCmdHlp, "db %DV LB 10", &VarCur);
3799 else
3800 DBGCVAR_ASSIGN(pResult, &VarCur);
3801
3802 /* advance */
3803 cbRange -= HitAddress.FlatPtr - pAddress->FlatPtr;
3804 *pAddress = HitAddress;
3805 pAddress->FlatPtr += cbBytes;
3806 pAddress->off += cbBytes;
3807 if (cbRange <= cbBytes)
3808 {
3809 cbRange = 0;
3810 break;
3811 }
3812 cbRange -= cbBytes;
3813
3814 if (++cHits >= cMaxHits)
3815 {
3816 /// @todo save the search.
3817 break;
3818 }
3819 }
3820
3821 /*
3822 * Save the search so we can resume it...
3823 */
3824 if (pDbgc->abSearch != pabBytes)
3825 {
3826 memcpy(pDbgc->abSearch, pabBytes, cbBytes);
3827 pDbgc->cbSearch = cbBytes;
3828 pDbgc->cbSearchUnit = cbUnit;
3829 }
3830 pDbgc->cMaxSearchHits = cMaxHits;
3831 pDbgc->SearchAddr = *pAddress;
3832 pDbgc->cbSearchRange = cbRange;
3833
3834 return cHits ? VINF_SUCCESS : VERR_DBGC_COMMAND_FAILED;
3835}
3836
3837
3838/**
3839 * Resumes the previous search.
3840 *
3841 * @returns VBox status code.
3842 * @param pCmdHlp Pointer to the command helper functions.
3843 * @param pVM Pointer to the current VM (if any).
3844 * @param pResult Where to store the result of a function invocation.
3845 */
3846static int dbgcCmdWorkerSearchMemResume(PDBGCCMDHLP pCmdHlp, PVM pVM, PDBGCVAR pResult)
3847{
3848 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3849
3850 /*
3851 * Make sure there is a previous command.
3852 */
3853 if (!pDbgc->cbSearch)
3854 {
3855 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Error: No previous search\n");
3856 return VERR_DBGC_COMMAND_FAILED;
3857 }
3858
3859 /*
3860 * Make range and address adjustments.
3861 */
3862 DBGFADDRESS Address = pDbgc->SearchAddr;
3863 if (Address.FlatPtr == ~(RTGCUINTPTR)0)
3864 {
3865 Address.FlatPtr -= Address.off;
3866 Address.off = 0;
3867 }
3868
3869 RTGCUINTPTR cbRange = pDbgc->cbSearchRange;
3870 if (!cbRange)
3871 cbRange = ~(RTGCUINTPTR)0;
3872 if (Address.FlatPtr + cbRange < pDbgc->SearchAddr.FlatPtr)
3873 cbRange = ~(RTGCUINTPTR)0 - pDbgc->SearchAddr.FlatPtr + !!pDbgc->SearchAddr.FlatPtr;
3874
3875 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, pDbgc->abSearch, pDbgc->cbSearch,
3876 pDbgc->cbSearchUnit, pDbgc->cMaxSearchHits, pResult);
3877}
3878
3879
3880/**
3881 * Search memory, worker for the 's' and 's?' functions.
3882 *
3883 * @returns VBox status.
3884 * @param pCmdHlp Pointer to the command helper functions.
3885 * @param pVM Pointer to the current VM (if any).
3886 * @param pAddress Where to start searching. If no range, search till end of address space.
3887 * @param cMaxHits The maximum number of hits.
3888 * @param chType The search type.
3889 * @param paPatArgs The pattern variable array.
3890 * @param cPatArgs Number of pattern variables.
3891 * @param pResult Where to store the result of a function invocation.
3892 */
3893static int dbgcCmdWorkerSearchMem(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pAddress, uint64_t cMaxHits, char chType,
3894 PCDBGCVAR paPatArgs, unsigned cPatArgs, PDBGCVAR pResult)
3895{
3896 if (pResult)
3897 DBGCVAR_INIT_GC_FLAT(pResult, 0);
3898
3899 /*
3900 * Convert the search pattern into bytes and DBGFR3MemScan can deal with.
3901 */
3902 uint32_t cbUnit;
3903 switch (chType)
3904 {
3905 case 'a':
3906 case 'b': cbUnit = 1; break;
3907 case 'u': cbUnit = 2 | RT_BIT_32(31); break;
3908 case 'w': cbUnit = 2; break;
3909 case 'd': cbUnit = 4; break;
3910 case 'q': cbUnit = 8; break;
3911 default:
3912 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "chType=%c\n", chType);
3913 }
3914 uint8_t abBytes[RT_SIZEOFMEMB(DBGC, abSearch)];
3915 uint32_t cbBytes = sizeof(abBytes);
3916 int rc = dbgcVarsToBytes(pCmdHlp, abBytes, &cbBytes, cbUnit, paPatArgs, cPatArgs);
3917 if (RT_FAILURE(rc))
3918 return VERR_DBGC_COMMAND_FAILED;
3919
3920 /*
3921 * Make DBGF address and fix the range.
3922 */
3923 DBGFADDRESS Address;
3924 rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, pAddress, &Address);
3925 if (RT_FAILURE(rc))
3926 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", pAddress);
3927
3928 RTGCUINTPTR cbRange;
3929 switch (pAddress->enmRangeType)
3930 {
3931 case DBGCVAR_RANGE_BYTES:
3932 cbRange = pAddress->u64Range;
3933 if (cbRange != pAddress->u64Range)
3934 cbRange = ~(RTGCUINTPTR)0;
3935 break;
3936
3937 case DBGCVAR_RANGE_ELEMENTS:
3938 cbRange = (RTGCUINTPTR)(pAddress->u64Range * cbUnit);
3939 if ( cbRange != pAddress->u64Range * cbUnit
3940 || cbRange < pAddress->u64Range)
3941 cbRange = ~(RTGCUINTPTR)0;
3942 break;
3943
3944 default:
3945 cbRange = ~(RTGCUINTPTR)0;
3946 break;
3947 }
3948 if (Address.FlatPtr + cbRange < Address.FlatPtr)
3949 cbRange = ~(RTGCUINTPTR)0 - Address.FlatPtr + !!Address.FlatPtr;
3950
3951 /*
3952 * Ok, do it.
3953 */
3954 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pVM, &Address, cbRange, abBytes, cbBytes, cbUnit, cMaxHits, pResult);
3955}
3956
3957
3958/**
3959 * The 's' command.
3960 *
3961 * @returns VBox status.
3962 * @param pCmd Pointer to the command descriptor (as registered).
3963 * @param pCmdHlp Pointer to command helper functions.
3964 * @param pVM Pointer to the current VM (if any).
3965 * @param paArgs Pointer to (readonly) array of arguments.
3966 * @param cArgs Number of arguments in the array.
3967 */
3968static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
3969{
3970 /* check that the parser did what it's supposed to do. */
3971 //if ( cArgs <= 2
3972 // && paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3973 // return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
3974
3975 /*
3976 * Repeat previous search?
3977 */
3978 if (cArgs == 0)
3979 return dbgcCmdWorkerSearchMemResume(pCmdHlp, pVM, NULL);
3980
3981 /*
3982 * Parse arguments.
3983 */
3984
3985 return -1;
3986}
3987
3988
3989/**
3990 * The 's?' command.
3991 *
3992 * @returns VBox status.
3993 * @param pCmd Pointer to the command descriptor (as registered).
3994 * @param pCmdHlp Pointer to command helper functions.
3995 * @param pVM Pointer to the current VM (if any).
3996 * @param paArgs Pointer to (readonly) array of arguments.
3997 * @param cArgs Number of arguments in the array.
3998 */
3999static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
4000{
4001 /* check that the parser did what it's supposed to do. */
4002 if ( cArgs < 2
4003 || !DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
4004 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "parser error\n");
4005 return dbgcCmdWorkerSearchMem(pCmdHlp, pVM, &paArgs[0], 25, pCmd->pszCmd[1], paArgs + 1, cArgs - 1, NULL);
4006}
4007
4008
4009/**
4010 * List near symbol.
4011 *
4012 * @returns VBox status code.
4013 * @param pCmdHlp Pointer to command helper functions.
4014 * @param pVM Pointer to the current VM (if any).
4015 * @param pArg Pointer to the address or symbol to lookup.
4016 */
4017static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pArg)
4018{
4019 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4020
4021 RTDBGSYMBOL Symbol;
4022 int rc;
4023 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
4024 {
4025 /*
4026 * Lookup the symbol address.
4027 */
4028 rc = DBGFR3AsSymbolByName(pVM, pDbgc->hDbgAs, pArg->u.pszString, &Symbol, NULL);
4029 if (RT_FAILURE(rc))
4030 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3AsSymbolByName(,,%s,)\n", pArg->u.pszString);
4031
4032 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%Rptr %s\n", Symbol.Value, Symbol.szName);
4033 }
4034 else
4035 {
4036 /*
4037 * Convert it to a flat GC address and lookup that address.
4038 */
4039 DBGCVAR AddrVar;
4040 rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
4041 if (RT_FAILURE(rc))
4042 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
4043
4044 RTINTPTR offDisp;
4045 DBGFADDRESS Addr;
4046 rc = DBGFR3AsSymbolByAddr(pVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pVM, &Addr, AddrVar.u.GCFlat), &offDisp, &Symbol, NULL);
4047 if (RT_FAILURE(rc))
4048 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3ASymbolByAddr(,,%RGv,,)\n", AddrVar.u.GCFlat);
4049
4050 if (!offDisp)
4051 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s", &AddrVar, Symbol.szName);
4052 else if (offDisp > 0)
4053 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
4054 else
4055 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
4056 if ((RTGCINTPTR)Symbol.cb > -offDisp)
4057 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " LB %RGv\n", Symbol.cb + offDisp);
4058 else
4059 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
4060 }
4061
4062 return rc;
4063}
4064
4065
4066/**
4067 * The 'ln' (listnear) command.
4068 *
4069 * @returns VBox status.
4070 * @param pCmd Pointer to the command descriptor (as registered).
4071 * @param pCmdHlp Pointer to command helper functions.
4072 * @param pVM Pointer to the current VM (if any).
4073 * @param paArgs Pointer to (readonly) array of arguments.
4074 * @param cArgs Number of arguments in the array.
4075 */
4076static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
4077{
4078 if (!cArgs)
4079 {
4080 /*
4081 * Current cs:eip symbol.
4082 */
4083 DBGCVAR AddrVar;
4084 int rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, "%%(cs:eip)");
4085 if (RT_FAILURE(rc))
4086 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(cs:eip)\n");
4087 return dbgcDoListNear(pCmdHlp, pVM, &AddrVar);
4088 }
4089
4090/** @todo Fix the darn parser, it's resolving symbols specified as arguments before we get in here. */
4091 /*
4092 * Iterate arguments.
4093 */
4094 for (unsigned iArg = 0; iArg < cArgs; iArg++)
4095 {
4096 int rc = dbgcDoListNear(pCmdHlp, pVM, &paArgs[iArg]);
4097 if (RT_FAILURE(rc))
4098 return rc;
4099 }
4100
4101 NOREF(pCmd);
4102 return VINF_SUCCESS;
4103}
4104
4105
4106/**
4107 * Matches the module patters against a module name.
4108 *
4109 * @returns true if matching, otherwise false.
4110 * @param pszName The module name.
4111 * @param paArgs The module pattern argument list.
4112 * @param cArgs Number of arguments.
4113 */
4114static bool dbgcCmdListModuleMatch(const char *pszName, PCDBGCVAR paArgs, unsigned cArgs)
4115{
4116 for (uint32_t i = 0; i < cArgs; i++)
4117 if (RTStrSimplePatternMatch(paArgs[i].u.pszString, pszName))
4118 return true;
4119 return false;
4120}
4121
4122
4123/**
4124 * The 'ln' (listnear) command.
4125 *
4126 * @returns VBox status.
4127 * @param pCmd Pointer to the command descriptor (as registered).
4128 * @param pCmdHlp Pointer to command helper functions.
4129 * @param pVM Pointer to the current VM (if any).
4130 * @param paArgs Pointer to (readonly) array of arguments.
4131 * @param cArgs Number of arguments in the array.
4132 */
4133static DECLCALLBACK(int) dbgcCmdListModules(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
4134{
4135 bool const fMappings = pCmd->pszCmd[2] == 'o';
4136 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4137
4138 /*
4139 * Iterate the modules in the current address space and print info about
4140 * those matching the input.
4141 */
4142 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pVM, pDbgc->hDbgAs);
4143 uint32_t cMods = RTDbgAsModuleCount(hAs);
4144 for (uint32_t iMod = 0; iMod < cMods; iMod++)
4145 {
4146 RTDBGMOD hMod = RTDbgAsModuleByIndex(hAs, iMod);
4147 if (hMod != NIL_RTDBGMOD)
4148 {
4149 uint32_t const cSegs = RTDbgModSegmentCount(hMod);
4150 const char * const pszName = RTDbgModName(hMod);
4151 if ( cArgs == 0
4152 || dbgcCmdListModuleMatch(pszName, paArgs, cArgs))
4153 {
4154 /*
4155 * Find the mapping with the lower address, preferring a full
4156 * image mapping, for the main line.
4157 */
4158 RTDBGASMAPINFO aMappings[128];
4159 uint32_t cMappings = RT_ELEMENTS(aMappings);
4160 int rc = RTDbgAsModuleQueryMapByIndex(hAs, iMod, &aMappings[0], &cMappings, 0 /*fFlags*/);
4161 if (RT_SUCCESS(rc))
4162 {
4163 bool fFull = false;
4164 RTUINTPTR uMin = RTUINTPTR_MAX;
4165 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
4166 if ( aMappings[iMap].Address < uMin
4167 && ( !fFull
4168 || aMappings[iMap].iSeg == NIL_RTDBGSEGIDX))
4169 uMin = aMappings[iMap].Address;
4170 DBGCCmdHlpPrintf(pCmdHlp, "%RGv %04x %s\n", (RTGCUINTPTR)uMin, cSegs, pszName);
4171
4172 if (fMappings)
4173 {
4174 /* sort by address first - not very efficient. */
4175 for (uint32_t i = 0; i + 1 < cMappings; i++)
4176 for (uint32_t j = i + 1; j < cMappings; j++)
4177 if (aMappings[j].Address < aMappings[i].Address)
4178 {
4179 RTDBGASMAPINFO Tmp = aMappings[j];
4180 aMappings[j] = aMappings[i];
4181 aMappings[i] = Tmp;
4182 }
4183
4184 /* print */
4185 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
4186 if (aMappings[iMap].iSeg != NIL_RTDBGSEGIDX)
4187 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv #%02x %s\n",
4188 (RTGCUINTPTR)aMappings[iMap].Address,
4189 (RTGCUINTPTR)RTDbgModSegmentSize(hMod, aMappings[iMap].iSeg),
4190 aMappings[iMap].iSeg,
4191 /** @todo RTDbgModSegmentName(hMod, aMappings[iMap].iSeg)*/ "noname");
4192 else
4193 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv <everything>\n",
4194 (RTGCUINTPTR)aMappings[iMap].Address,
4195 (RTGCUINTPTR)RTDbgModImageSize(hMod));
4196 }
4197 }
4198 else
4199 DBGCCmdHlpPrintf(pCmdHlp, "%.*s %04x %s (rc=%Rrc)\n",
4200 sizeof(RTGCPTR) * 2, "???????????", cSegs, pszName, rc);
4201 /** @todo missing address space API for enumerating the mappings. */
4202 }
4203 RTDbgModRelease(hMod);
4204 }
4205 }
4206 RTDbgAsRelease(hAs);
4207
4208 NOREF(pCmd);
4209 return VINF_SUCCESS;
4210}
4211
4212
4213
4214
4215/**
4216 * @callback_method_impl{The hi(value) function implementation.}
4217 */
4218static DECLCALLBACK(int) dbgcFuncHi(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, uint32_t cArgs,
4219 PDBGCVAR pResult)
4220{
4221 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4222 uint16_t uHi;
4223 switch (paArgs[0].enmType)
4224 {
4225 case DBGCVAR_TYPE_GC_FLAT: uHi = (uint16_t)(paArgs[0].u.GCFlat >> 16); break;
4226 case DBGCVAR_TYPE_GC_FAR: uHi = (uint16_t)paArgs[0].u.GCFar.sel; break;
4227 case DBGCVAR_TYPE_GC_PHYS: uHi = (uint16_t)(paArgs[0].u.GCPhys >> 16); break;
4228 case DBGCVAR_TYPE_HC_FLAT: uHi = (uint16_t)((uintptr_t)paArgs[0].u.pvHCFlat >> 16); break;
4229 case DBGCVAR_TYPE_HC_PHYS: uHi = (uint16_t)(paArgs[0].u.HCPhys >> 16); break;
4230 case DBGCVAR_TYPE_NUMBER: uHi = (uint16_t)(paArgs[0].u.u64Number >> 16); break;
4231 default:
4232 AssertFailedReturn(VERR_DBGC_PARSE_BUG);
4233 }
4234
4235 DBGCVAR_INIT_NUMBER(pResult, uHi);
4236 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pVM);
4237 return VINF_SUCCESS;
4238}
4239
4240
4241/**
4242 * @callback_method_impl{The low(value) function implementation.}
4243 */
4244static DECLCALLBACK(int) dbgcFuncLow(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, uint32_t cArgs,
4245 PDBGCVAR pResult)
4246{
4247 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4248 uint16_t uLow;
4249 switch (paArgs[0].enmType)
4250 {
4251 case DBGCVAR_TYPE_GC_FLAT: uLow = (uint16_t)paArgs[0].u.GCFlat; break;
4252 case DBGCVAR_TYPE_GC_FAR: uLow = (uint16_t)paArgs[0].u.GCFar.off; break;
4253 case DBGCVAR_TYPE_GC_PHYS: uLow = (uint16_t)paArgs[0].u.GCPhys; break;
4254 case DBGCVAR_TYPE_HC_FLAT: uLow = (uint16_t)(uintptr_t)paArgs[0].u.pvHCFlat; break;
4255 case DBGCVAR_TYPE_HC_PHYS: uLow = (uint16_t)paArgs[0].u.HCPhys; break;
4256 case DBGCVAR_TYPE_NUMBER: uLow = (uint16_t)paArgs[0].u.u64Number; break;
4257 default:
4258 AssertFailedReturn(VERR_DBGC_PARSE_BUG);
4259 }
4260
4261 DBGCVAR_INIT_NUMBER(pResult, uLow);
4262 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pVM);
4263 return VINF_SUCCESS;
4264}
4265
4266
4267/** Generic pointer or number argument. */
4268static const DBGCVARDESC g_aArgPointerNumber[] =
4269{
4270 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
4271 { 1, 1, DBGCVAR_CAT_POINTER_NUMBER, 0, "value", "Address or number." },
4272};
4273
4274
4275
4276/** Function descriptors for the CodeView / WinDbg emulation.
4277 * The emulation isn't attempting to be identical, only somewhat similar.
4278 */
4279const DBGCFUNC g_aFuncsCodeView[] =
4280{
4281 { "hi", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncHi, "value", "Returns the high 16-bit bits of a value." },
4282 { "low", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncLow, "value", "Returns the low 16-bit bits of a value." },
4283};
4284
4285/** The number of functions in the CodeView/WinDbg emulation. */
4286const uint32_t g_cFuncsCodeView = RT_ELEMENTS(g_aFuncsCodeView);
4287
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