VirtualBox

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

Last change on this file since 48898 was 48898, checked in by vboxsync, 11 years ago

VMM,DBGC: Implemented DBGFR3RegNmSet and made the debugger side work.

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