VirtualBox

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

Last change on this file since 58890 was 58890, checked in by vboxsync, 9 years ago

DBGCEmulateCodeView.cpp: Sketched out the sx[-enir] commands which we'll use to control various debug events; guest CPU exceptions are up first.

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