VirtualBox

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

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

Debugger: Use DBGFR3RegPrintf instead of accessing the CPUMCTX directly. Added rg64 and rg32.

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

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