VirtualBox

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

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

DBGF,CPUM,DBGC: Use DBGFReg in the debugger, stop accessing CPUMCTX structures directly when messing with registers.

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