VirtualBox

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

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

Debugger Console: Drop the HC_FAR type.

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

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