VirtualBox

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

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

Debugger console: more cleanup.

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

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