VirtualBox

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

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

PGM,DBGC: working shadow page table dumper (sans EPT).

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

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