VirtualBox

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

Last change on this file since 45983 was 44399, checked in by vboxsync, 12 years ago

DBGF,DBGC,++: PVM -> PUVM. Some refactoring and cleanup as well.

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