VirtualBox

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

Last change on this file since 73353 was 73351, checked in by vboxsync, 6 years ago

VBoxGuest,VMMDev,DBGF,VM: Added bug check report to VBoxGuest/VMMDev and hooked it up to DBGF. Made DBGF remember the last reported bug check, adding an info handler for displaying it. Added VM reset counters w/ getters for use in bug check reporting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 287.1 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 73351 2018-07-25 13:02:11Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, CodeView / WinDbg Emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 dbgcCmdDumpTypeInfo;
64static FNDBGCCMD dbgcCmdDumpTypedVal;
65static FNDBGCCMD dbgcCmdEditMem;
66static FNDBGCCMD dbgcCmdGo;
67static FNDBGCCMD dbgcCmdGoUp;
68static FNDBGCCMD dbgcCmdListModules;
69static FNDBGCCMD dbgcCmdListNear;
70static FNDBGCCMD dbgcCmdListSource;
71static FNDBGCCMD dbgcCmdMemoryInfo;
72static FNDBGCCMD dbgcCmdReg;
73static FNDBGCCMD dbgcCmdRegGuest;
74static FNDBGCCMD dbgcCmdRegHyper;
75static FNDBGCCMD dbgcCmdRegTerse;
76static FNDBGCCMD dbgcCmdSearchMem;
77static FNDBGCCMD dbgcCmdSearchMemType;
78static FNDBGCCMD dbgcCmdStepTrace;
79static FNDBGCCMD dbgcCmdStepTraceTo;
80static FNDBGCCMD dbgcCmdStepTraceToggle;
81static FNDBGCCMD dbgcCmdEventCtrl;
82static FNDBGCCMD dbgcCmdEventCtrlList;
83static FNDBGCCMD dbgcCmdEventCtrlReset;
84static FNDBGCCMD dbgcCmdStack;
85static FNDBGCCMD dbgcCmdUnassemble;
86static FNDBGCCMD dbgcCmdUnassembleCfg;
87
88
89/*********************************************************************************************************************************
90* Global Variables *
91*********************************************************************************************************************************/
92/** 'ba' arguments. */
93static const DBGCVARDESC g_aArgBrkAcc[] =
94{
95 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
96 { 1, 1, DBGCVAR_CAT_STRING, 0, "access", "The access type: x=execute, rw=read/write (alias r), w=write, i=not implemented." },
97 { 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." },
98 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
99 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
100 { 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)" },
101 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
102};
103
104
105/** 'bc', 'bd', 'be' arguments. */
106static const DBGCVARDESC g_aArgBrks[] =
107{
108 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
109 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "#bp", "Breakpoint number." },
110 { 0, 1, DBGCVAR_CAT_STRING, 0, "all", "All breakpoints." },
111};
112
113
114/** 'bp' arguments. */
115static const DBGCVARDESC g_aArgBrkSet[] =
116{
117 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
118 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
119 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
120 { 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)" },
121 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
122};
123
124
125/** 'br' arguments. */
126static const DBGCVARDESC g_aArgBrkREM[] =
127{
128 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
129 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
130 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
131 { 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)" },
132 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
133};
134
135
136/** 'd?' arguments. */
137static const DBGCVARDESC g_aArgDumpMem[] =
138{
139 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
140 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start dumping memory." },
141};
142
143
144/** 'dg', 'dga', 'dl', 'dla' arguments. */
145static const DBGCVARDESC g_aArgDumpDT[] =
146{
147 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
148 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "sel", "Selector or selector range." },
149 { 0, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Far address which selector should be dumped." },
150};
151
152
153/** 'di', 'dia' arguments. */
154static const DBGCVARDESC g_aArgDumpIDT[] =
155{
156 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
157 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "int", "The interrupt vector or interrupt vector range." },
158};
159
160
161/** 'dpd*' arguments. */
162static const DBGCVARDESC g_aArgDumpPD[] =
163{
164 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
165 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "index", "Index into the page directory." },
166 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from. Range is applied to the page directory." },
167};
168
169
170/** 'dpda' arguments. */
171static const DBGCVARDESC g_aArgDumpPDAddr[] =
172{
173 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
174 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page directory entry to start dumping from." },
175};
176
177
178/** 'dph*' arguments. */
179static const DBGCVARDESC g_aArgDumpPH[] =
180{
181 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
182 { 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." },
183 { 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." },
184 { 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." },
185};
186
187
188/** 'dpt?' arguments. */
189static const DBGCVARDESC g_aArgDumpPT[] =
190{
191 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
192 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from." },
193};
194
195
196/** 'dpta' arguments. */
197static const DBGCVARDESC g_aArgDumpPTAddr[] =
198{
199 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
200 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page table entry to start dumping from." },
201};
202
203
204/** 'dt' arguments. */
205static const DBGCVARDESC g_aArgDumpTSS[] =
206{
207 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
208 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "tss", "TSS selector number." },
209 { 0, 1, DBGCVAR_CAT_POINTER, 0, "tss:ign|addr", "TSS address. If the selector is a TSS selector, the offset will be ignored." }
210};
211
212
213/** 'dti' arguments. */
214static const DBGCVARDESC g_aArgDumpTypeInfo[] =
215{
216 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
217 { 1, 1, DBGCVAR_CAT_STRING, 0, "type", "The type to dump" },
218 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "levels", "How many levels to dump the type information" }
219};
220
221
222/** 'dtv' arguments. */
223static const DBGCVARDESC g_aArgDumpTypedVal[] =
224{
225 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
226 { 1, 1, DBGCVAR_CAT_STRING, 0, "type", "The type to use" },
227 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address to start dumping from." },
228 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "levels", "How many levels to dump" }
229};
230
231
232/** 'e?' arguments. */
233static const DBGCVARDESC g_aArgEditMem[] =
234{
235 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
236 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to write." },
237 { 1, ~0U, DBGCVAR_CAT_NUMBER, 0, "value", "Value to write." },
238};
239
240
241/** 'lm' arguments. */
242static const DBGCVARDESC g_aArgListMods[] =
243{
244 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
245 { 0, ~0U, DBGCVAR_CAT_STRING, 0, "module", "Module name." },
246};
247
248
249/** 'ln' arguments. */
250static const DBGCVARDESC g_aArgListNear[] =
251{
252 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
253 { 0, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
254 { 0, ~0U, DBGCVAR_CAT_SYMBOL, 0, "symbol", "Symbol to lookup." },
255};
256
257
258/** 'ls' arguments. */
259static const DBGCVARDESC g_aArgListSource[] =
260{
261 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
262 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start looking for source lines." },
263};
264
265
266/** 'm' argument. */
267static const DBGCVARDESC g_aArgMemoryInfo[] =
268{
269 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
270 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Pointer to obtain info about." },
271};
272
273
274/** 'p', 'pc', 'pt', 't', 'tc' and 'tt' arguments. */
275static const DBGCVARDESC g_aArgStepTrace[] =
276{
277 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
278 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "count", "Number of instructions or source lines to step." },
279 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed afterwards. Quote it!" },
280};
281
282
283/** 'pa' and 'ta' arguments. */
284static const DBGCVARDESC g_aArgStepTraceTo[] =
285{
286 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
287 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Where to stop" },
288 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed afterwards. Quote it!" },
289};
290
291
292/** 'r' arguments. */
293static const DBGCVARDESC g_aArgReg[] =
294{
295 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
296 { 0, 1, DBGCVAR_CAT_SYMBOL, 0, "register", "Register to show or set." },
297 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "=", "Equal sign." },
298 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "value", "New register value." },
299};
300
301
302/** 's' arguments. */
303static const DBGCVARDESC g_aArgSearchMem[] =
304{
305 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
306 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-b", "Byte string." },
307 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-w", "Word string." },
308 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-d", "DWord string." },
309 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-q", "QWord string." },
310 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-a", "ASCII string." },
311 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-u", "Unicode string." },
312 { 0, 1, DBGCVAR_CAT_OPTION_NUMBER, 0, "-n <Hits>", "Maximum number of hits." },
313 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
314 { 0, ~0U, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
315};
316
317
318/** 's?' arguments. */
319static const DBGCVARDESC g_aArgSearchMemType[] =
320{
321 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
322 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
323 { 1, ~0U, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
324};
325
326
327/** 'sxe', 'sxn', 'sxi', 'sx-' arguments. */
328static const DBGCVARDESC g_aArgEventCtrl[] =
329{
330 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
331 { 0, 1, DBGCVAR_CAT_STRING, 0, "-c", "The -c option, requires <cmds>." },
332 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "cmds", "Command to execute on this event." },
333 { 0 /*weird*/, ~0U, DBGCVAR_CAT_STRING, 0, "event", "One or more events, 'all' refering to all events." },
334};
335
336/** 'sx' and 'sr' arguments. */
337static const DBGCVARDESC g_aArgEventCtrlOpt[] =
338{
339 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
340 { 0, ~0U, DBGCVAR_CAT_STRING, 0, "event", "Zero or more events, 'all' refering to all events and being the default." },
341};
342
343/** 'u' arguments. */
344static const DBGCVARDESC g_aArgUnassemble[] =
345{
346 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
347 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
348};
349
350/** 'ucfg' arguments. */
351static const DBGCVARDESC g_aArgUnassembleCfg[] =
352{
353 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
354 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
355};
356
357
358/** Command descriptors for the CodeView / WinDbg emulation.
359 * The emulation isn't attempting to be identical, only somewhat similar.
360 */
361const DBGCCMD g_aCmdsCodeView[] =
362{
363 /* pszCmd, cArgsMin, cArgsMax, paArgDescs, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
364 { "ba", 3, 6, &g_aArgBrkAcc[0], RT_ELEMENTS(g_aArgBrkAcc), 0, dbgcCmdBrkAccess, "<access> <size> <address> [passes [max passes]] [cmds]",
365 "Sets a data access breakpoint." },
366 { "bc", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Deletes a set of breakpoints." },
367 { "bd", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
368 { "be", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enables a set of breakpoints." },
369 { "bl", 0, 0, NULL, 0, 0, dbgcCmdBrkList, "", "Lists all the breakpoints." },
370 { "bp", 1, 4, &g_aArgBrkSet[0], RT_ELEMENTS(g_aArgBrkSet), 0, dbgcCmdBrkSet, "<address> [passes [max passes]] [cmds]",
371 "Sets a breakpoint (int 3)." },
372 { "br", 1, 4, &g_aArgBrkREM[0], RT_ELEMENTS(g_aArgBrkREM), 0, dbgcCmdBrkREM, "<address> [passes [max passes]] [cmds]",
373 "Sets a recompiler specific breakpoint." },
374 { "d", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory using last element size and type." },
375 { "dF", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as far 16:16." },
376 { "dFs", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as far 16:16 with near symbols." },
377 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
378 { "db", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in bytes." },
379 { "dd", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in double words." },
380 { "dds", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as double words with near symbols." },
381 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
382 { "dg", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT)." },
383 { "dga", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT) including not-present entries." },
384 { "di", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT)." },
385 { "dia", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT) including not-present entries." },
386 { "dl", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT)." },
387 { "dla", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT) including not-present entries." },
388 { "dpd", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the default context." },
389 { "dpda", 0, 1, &g_aArgDumpPDAddr[0],RT_ELEMENTS(g_aArgDumpPDAddr), 0, dbgcCmdDumpPageDir, "[addr]", "Dumps memory at given address as a page directory." },
390 { "dpdb", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDirBoth, "[addr|index]", "Dumps page directory entries of the guest and the hypervisor. " },
391 { "dpdg", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the guest." },
392 { "dpdh", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the hypervisor. " },
393 { "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." },
394 { "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." },
395 { "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." },
396 { "dp", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in mode sized words." },
397 { "dps", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in mode sized words with near symbols." },
398 { "dpt", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the default context." },
399 { "dpta", 1, 1, &g_aArgDumpPTAddr[0],RT_ELEMENTS(g_aArgDumpPTAddr), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps memory at given address as a page table." },
400 { "dptb", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTableBoth,"<addr>", "Dumps page table entries of the guest and the hypervisor." },
401 { "dptg", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the guest." },
402 { "dpth", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the hypervisor." },
403 { "dq", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in quad words." },
404 { "dqs", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as quad words with near symbols." },
405 { "dt", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the task state segment (TSS)." },
406 { "dt16", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 16-bit task state segment (TSS)." },
407 { "dt32", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 32-bit task state segment (TSS)." },
408 { "dt64", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 64-bit task state segment (TSS)." },
409 { "dti", 1, 2, &g_aArgDumpTypeInfo[0],RT_ELEMENTS(g_aArgDumpTypeInfo), 0, dbgcCmdDumpTypeInfo,"<type> [levels]", "Dump type information." },
410 { "dtv", 2, 3, &g_aArgDumpTypedVal[0],RT_ELEMENTS(g_aArgDumpTypedVal), 0, dbgcCmdDumpTypedVal,"<type> <addr> [levels]", "Dump a memory buffer using the information in the given type." },
411 { "du", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as unicode string (little endian)." },
412 { "dw", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in words." },
413 /** @todo add 'e', 'ea str', 'eza str', 'eu str' and 'ezu str'. See also
414 * dbgcCmdSearchMem and its dbgcVarsToBytes usage. */
415 { "eb", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 1-byte value to memory." },
416 { "ew", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 2-byte value to memory." },
417 { "ed", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 4-byte value to memory." },
418 { "eq", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 8-byte value to memory." },
419 { "g", 0, 0, NULL, 0, 0, dbgcCmdGo, "", "Continue execution." },
420 { "gu", 0, 0, NULL, 0, 0, dbgcCmdGoUp, "", "Go up - continue execution till after return." },
421 { "k", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack." },
422 { "kg", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - guest." },
423 { "kh", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - hypervisor." },
424 { "lm", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules." },
425 { "lmv", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules, verbose." },
426 { "lmo", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments." },
427 { "lmov", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments, verbose." },
428 { "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." },
429 { "ls", 0, 1, &g_aArgListSource[0],RT_ELEMENTS(g_aArgListSource), 0, dbgcCmdListSource, "[addr]", "Source." },
430 { "m", 1, 1, &g_aArgMemoryInfo[0],RT_ELEMENTS(g_aArgMemoryInfo), 0, dbgcCmdMemoryInfo, "<addr>", "Display information about that piece of memory." },
431 { "p", 0, 2, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Step over." },
432 { "pr", 0, 0, NULL, 0, 0, dbgcCmdStepTraceToggle, "", "Toggle displaying registers for tracing & stepping (no code executed)." },
433 { "pa", 1, 1, &g_aArgStepTraceTo[0], RT_ELEMENTS(g_aArgStepTraceTo), 0, dbgcCmdStepTraceTo, "<addr> [count] [cmds]","Step to the given address." },
434 { "pc", 0, 0, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Step to the next call instruction." },
435 { "pt", 0, 0, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Step to the next return instruction." },
436 { "r", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdReg, "[reg [[=] newval]]", "Show or set register(s) - active reg set." },
437 { "rg", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegGuest, "[reg [[=] newval]]", "Show or set register(s) - guest reg set." },
438 { "rg32", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 32-bit guest registers." },
439 { "rg64", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 64-bit guest registers." },
440 { "rh", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegHyper, "[reg [[=] newval]]", "Show or set register(s) - hypervisor reg set." },
441 { "rt", 0, 0, NULL, 0, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
442 { "s", 0, ~0U, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
443 { "sa", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
444 { "sb", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
445 { "sd", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
446 { "sq", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
447 { "su", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
448 { "sw", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more words." },
449 { "sx", 0, ~0U, &g_aArgEventCtrlOpt[0], RT_ELEMENTS(g_aArgEventCtrlOpt), 0, dbgcCmdEventCtrlList, "[<event> [..]]", "Lists settings for exceptions, exits and other events. All if no filter is specified." },
450 { "sx-", 3, ~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." },
451 { "sxe", 1, ~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." },
452 { "sxn", 1, ~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." },
453 { "sxi", 1, ~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." },
454 { "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." },
455 { "t", 0, 2, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Trace ." },
456 { "tr", 0, 0, NULL, 0, 0, dbgcCmdStepTraceToggle, "", "Toggle displaying registers for tracing & stepping (no code executed)." },
457 { "ta", 1, 1, &g_aArgStepTraceTo[0], RT_ELEMENTS(g_aArgStepTraceTo), 0, dbgcCmdStepTraceTo, "<addr> [count] [cmds]","Trace to the given address." },
458 { "tc", 0, 0, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Trace to the next call instruction." },
459 { "tt", 0, 0, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Trace to the next return instruction." },
460 { "u", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble." },
461 { "u64", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 64-bit code." },
462 { "u32", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 32-bit code." },
463 { "u16", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code." },
464 { "uv86", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code with v8086/real mode addressing." },
465 { "ucfg", 0, 1, &g_aArgUnassembleCfg[0], RT_ELEMENTS(g_aArgUnassembleCfg), 0, dbgcCmdUnassembleCfg, "[addr]", "Unassemble creating a control flow graph." },
466 { "ucfgc", 0, 1, &g_aArgUnassembleCfg[0], RT_ELEMENTS(g_aArgUnassembleCfg), 0, dbgcCmdUnassembleCfg, "[addr]", "Unassemble creating a control flow graph with colors." },
467};
468
469/** The number of commands in the CodeView/WinDbg emulation. */
470const uint32_t g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
471
472
473/**
474 * Selectable debug event descriptors.
475 *
476 * @remarks Sorted by DBGCSXEVT::enmType value.
477 */
478const DBGCSXEVT g_aDbgcSxEvents[] =
479{
480 { DBGFEVENT_INTERRUPT_HARDWARE, "hwint", NULL, kDbgcSxEventKind_Interrupt, kDbgcEvtState_Disabled, 0, "Hardware interrupt" },
481 { DBGFEVENT_INTERRUPT_SOFTWARE, "swint", NULL, kDbgcSxEventKind_Interrupt, kDbgcEvtState_Disabled, 0, "Software interrupt" },
482 { DBGFEVENT_TRIPLE_FAULT, "triplefault", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Enabled, 0, "Triple fault "},
483 { DBGFEVENT_XCPT_DE, "xcpt_de", "de", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DE (integer divide error)" },
484 { DBGFEVENT_XCPT_DB, "xcpt_db", "db", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DB (debug)" },
485 { DBGFEVENT_XCPT_02, "xcpt_02", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
486 { DBGFEVENT_XCPT_BP, "xcpt_bp", "bp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#BP (breakpoint)" },
487 { DBGFEVENT_XCPT_OF, "xcpt_of", "of", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#OF (overflow (INTO))" },
488 { DBGFEVENT_XCPT_BR, "xcpt_br", "br", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#BR (bound range exceeded)" },
489 { DBGFEVENT_XCPT_UD, "xcpt_ud", "ud", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#UD (undefined opcode)" },
490 { DBGFEVENT_XCPT_NM, "xcpt_nm", "nm", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#NM (FPU not available)" },
491 { DBGFEVENT_XCPT_DF, "xcpt_df", "df", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DF (double fault)" },
492 { DBGFEVENT_XCPT_09, "xcpt_09", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "Coprocessor segment overrun" },
493 { DBGFEVENT_XCPT_TS, "xcpt_ts", "ts", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#TS (task switch)" },
494 { DBGFEVENT_XCPT_NP, "xcpt_np", "np", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#NP (segment not present)" },
495 { DBGFEVENT_XCPT_SS, "xcpt_ss", "ss", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#SS (stack segment fault)" },
496 { DBGFEVENT_XCPT_GP, "xcpt_gp", "gp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#GP (general protection fault)" },
497 { DBGFEVENT_XCPT_PF, "xcpt_pf", "pf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#PF (page fault)" },
498 { DBGFEVENT_XCPT_0f, "xcpt_0f", "xcpt0f", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
499 { DBGFEVENT_XCPT_MF, "xcpt_mf", "mf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#MF (math fault)" },
500 { DBGFEVENT_XCPT_AC, "xcpt_ac", "ac", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#AC (alignment check)" },
501 { DBGFEVENT_XCPT_MC, "xcpt_mc", "mc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#MC (machine check)" },
502 { DBGFEVENT_XCPT_XF, "xcpt_xf", "xf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#XF (SIMD floating-point exception)" },
503 { DBGFEVENT_XCPT_VE, "xcpt_vd", "ve", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#VE (virtualization exception)" },
504 { DBGFEVENT_XCPT_15, "xcpt_15", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
505 { DBGFEVENT_XCPT_16, "xcpt_16", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
506 { DBGFEVENT_XCPT_17, "xcpt_17", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
507 { DBGFEVENT_XCPT_18, "xcpt_18", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
508 { DBGFEVENT_XCPT_19, "xcpt_19", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
509 { DBGFEVENT_XCPT_1a, "xcpt_1a", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
510 { DBGFEVENT_XCPT_1b, "xcpt_1b", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
511 { DBGFEVENT_XCPT_1c, "xcpt_1c", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
512 { DBGFEVENT_XCPT_1d, "xcpt_1d", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
513 { DBGFEVENT_XCPT_SX, "xcpt_sx", "sx", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#SX (security exception)" },
514 { DBGFEVENT_XCPT_1f, "xcpt_1f", "xcpt1f", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
515 { DBGFEVENT_INSTR_HALT, "instr_halt", "hlt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
516 { DBGFEVENT_INSTR_MWAIT, "instr_mwait", "mwait", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
517 { DBGFEVENT_INSTR_MONITOR, "instr_monitor", "monitor", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
518 { DBGFEVENT_INSTR_CPUID, "instr_cpuid", "cpuid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
519 { DBGFEVENT_INSTR_INVD, "instr_invd", "invd", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
520 { DBGFEVENT_INSTR_WBINVD, "instr_wbinvd", "wbinvd", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
521 { DBGFEVENT_INSTR_INVLPG, "instr_invlpg", "invlpg", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
522 { DBGFEVENT_INSTR_RDTSC, "instr_rdtsc", "rdtsc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
523 { DBGFEVENT_INSTR_RDTSCP, "instr_rdtscp", "rdtscp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
524 { DBGFEVENT_INSTR_RDPMC, "instr_rdpmc", "rdpmc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
525 { DBGFEVENT_INSTR_RDMSR, "instr_rdmsr", "rdmsr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
526 { DBGFEVENT_INSTR_WRMSR, "instr_wrmsr", "wrmsr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
527 { DBGFEVENT_INSTR_CRX_READ, "instr_crx_read", "crx_read", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
528 { DBGFEVENT_INSTR_CRX_WRITE, "instr_crx_write", "crx_write",kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
529 { DBGFEVENT_INSTR_DRX_READ, "instr_drx_read", "drx_read", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
530 { DBGFEVENT_INSTR_DRX_WRITE, "instr_drx_write", "drx_write",kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
531 { DBGFEVENT_INSTR_PAUSE, "instr_pause", "pause", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
532 { DBGFEVENT_INSTR_XSETBV, "instr_xsetbv", "xsetbv", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
533 { DBGFEVENT_INSTR_SIDT, "instr_sidt", "sidt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
534 { DBGFEVENT_INSTR_LIDT, "instr_lidt", "lidt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
535 { DBGFEVENT_INSTR_SGDT, "instr_sgdt", "sgdt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
536 { DBGFEVENT_INSTR_LGDT, "instr_lgdt", "lgdt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
537 { DBGFEVENT_INSTR_SLDT, "instr_sldt", "sldt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
538 { DBGFEVENT_INSTR_LLDT, "instr_lldt", "lldt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
539 { DBGFEVENT_INSTR_STR, "instr_str", "str", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
540 { DBGFEVENT_INSTR_LTR, "instr_ltr", "ltr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
541 { DBGFEVENT_INSTR_GETSEC, "instr_getsec", "getsec", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
542 { DBGFEVENT_INSTR_RSM, "instr_rsm", "rsm", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
543 { DBGFEVENT_INSTR_RDRAND, "instr_rdrand", "rdrand", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
544 { DBGFEVENT_INSTR_RDSEED, "instr_rdseed", "rdseed", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
545 { DBGFEVENT_INSTR_XSAVES, "instr_xsaves", "xsaves", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
546 { DBGFEVENT_INSTR_XRSTORS, "instr_xrstors", "xrstors", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
547 { DBGFEVENT_INSTR_VMM_CALL, "instr_vmm_call", "vmm_call", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
548 { DBGFEVENT_INSTR_VMX_VMCLEAR, "instr_vmx_vmclear", "vmclear", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
549 { DBGFEVENT_INSTR_VMX_VMLAUNCH, "instr_vmx_vmlaunch", "vmlaunch", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
550 { DBGFEVENT_INSTR_VMX_VMPTRLD, "instr_vmx_vmptrld", "vmptrld", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
551 { DBGFEVENT_INSTR_VMX_VMPTRST, "instr_vmx_vmptrst", "vmptrst", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
552 { DBGFEVENT_INSTR_VMX_VMREAD, "instr_vmx_vmread", "vmread", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
553 { DBGFEVENT_INSTR_VMX_VMRESUME, "instr_vmx_vmresume", "vmresume", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
554 { DBGFEVENT_INSTR_VMX_VMWRITE, "instr_vmx_vmwrite", "vmwrite", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
555 { DBGFEVENT_INSTR_VMX_VMXOFF, "instr_vmx_vmxoff", "vmxoff", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
556 { DBGFEVENT_INSTR_VMX_VMXON, "instr_vmx_vmxon", "vmxon", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
557 { DBGFEVENT_INSTR_VMX_VMFUNC, "instr_vmx_vmfunc", "vmfunc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
558 { DBGFEVENT_INSTR_VMX_INVEPT, "instr_vmx_invept", "invept", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
559 { DBGFEVENT_INSTR_VMX_INVVPID, "instr_vmx_invvpid", "invvpid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
560 { DBGFEVENT_INSTR_VMX_INVPCID, "instr_vmx_invpcid", "invpcid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
561 { DBGFEVENT_INSTR_SVM_VMRUN, "instr_svm_vmrun", "vmrun", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
562 { DBGFEVENT_INSTR_SVM_VMLOAD, "instr_svm_vmload", "vmload", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
563 { DBGFEVENT_INSTR_SVM_VMSAVE, "instr_svm_vmsave", "vmsave", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
564 { DBGFEVENT_INSTR_SVM_STGI, "instr_svm_stgi", "stgi", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
565 { DBGFEVENT_INSTR_SVM_CLGI, "instr_svm_clgi", "clgi", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
566 { DBGFEVENT_EXIT_TASK_SWITCH, "exit_task_switch", "task_switch", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
567 { DBGFEVENT_EXIT_HALT, "exit_halt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
568 { DBGFEVENT_EXIT_MWAIT, "exit_mwait", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
569 { DBGFEVENT_EXIT_MONITOR, "exit_monitor", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
570 { DBGFEVENT_EXIT_CPUID, "exit_cpuid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
571 { DBGFEVENT_EXIT_INVD, "exit_invd", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
572 { DBGFEVENT_EXIT_WBINVD, "exit_wbinvd", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
573 { DBGFEVENT_EXIT_INVLPG, "exit_invlpg", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
574 { DBGFEVENT_EXIT_RDTSC, "exit_rdtsc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
575 { DBGFEVENT_EXIT_RDTSCP, "exit_rdtscp", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
576 { DBGFEVENT_EXIT_RDPMC, "exit_rdpmc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
577 { DBGFEVENT_EXIT_RDMSR, "exit_rdmsr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
578 { DBGFEVENT_EXIT_WRMSR, "exit_wrmsr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
579 { DBGFEVENT_EXIT_CRX_READ, "exit_crx_read", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
580 { DBGFEVENT_EXIT_CRX_WRITE, "exit_crx_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
581 { DBGFEVENT_EXIT_DRX_READ, "exit_drx_read", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
582 { DBGFEVENT_EXIT_DRX_WRITE, "exit_drx_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
583 { DBGFEVENT_EXIT_PAUSE, "exit_pause", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
584 { DBGFEVENT_EXIT_XSETBV, "exit_xsetbv", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
585 { DBGFEVENT_EXIT_SIDT, "exit_sidt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
586 { DBGFEVENT_EXIT_LIDT, "exit_lidt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
587 { DBGFEVENT_EXIT_SGDT, "exit_sgdt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
588 { DBGFEVENT_EXIT_LGDT, "exit_lgdt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
589 { DBGFEVENT_EXIT_SLDT, "exit_sldt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
590 { DBGFEVENT_EXIT_LLDT, "exit_lldt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
591 { DBGFEVENT_EXIT_STR, "exit_str", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
592 { DBGFEVENT_EXIT_LTR, "exit_ltr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
593 { DBGFEVENT_EXIT_GETSEC, "exit_getsec", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
594 { DBGFEVENT_EXIT_RSM, "exit_rsm", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
595 { DBGFEVENT_EXIT_RDRAND, "exit_rdrand", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
596 { DBGFEVENT_EXIT_RDSEED, "exit_rdseed", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
597 { DBGFEVENT_EXIT_XSAVES, "exit_xsaves", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
598 { DBGFEVENT_EXIT_XRSTORS, "exit_xrstors", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
599 { DBGFEVENT_EXIT_VMM_CALL, "exit_vmm_call", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
600 { DBGFEVENT_EXIT_VMX_VMCLEAR, "exit_vmx_vmclear", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
601 { DBGFEVENT_EXIT_VMX_VMLAUNCH, "exit_vmx_vmlaunch", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
602 { DBGFEVENT_EXIT_VMX_VMPTRLD, "exit_vmx_vmptrld", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
603 { DBGFEVENT_EXIT_VMX_VMPTRST, "exit_vmx_vmptrst", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
604 { DBGFEVENT_EXIT_VMX_VMREAD, "exit_vmx_vmread", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
605 { DBGFEVENT_EXIT_VMX_VMRESUME, "exit_vmx_vmresume", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
606 { DBGFEVENT_EXIT_VMX_VMWRITE, "exit_vmx_vmwrite", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
607 { DBGFEVENT_EXIT_VMX_VMXOFF, "exit_vmx_vmxoff", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
608 { DBGFEVENT_EXIT_VMX_VMXON, "exit_vmx_vmxon", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
609 { DBGFEVENT_EXIT_VMX_VMFUNC, "exit_vmx_vmfunc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
610 { DBGFEVENT_EXIT_VMX_INVEPT, "exit_vmx_invept", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
611 { DBGFEVENT_EXIT_VMX_INVVPID, "exit_vmx_invvpid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
612 { DBGFEVENT_EXIT_VMX_INVPCID, "exit_vmx_invpcid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
613 { DBGFEVENT_EXIT_VMX_EPT_VIOLATION, "exit_vmx_ept_violation", "eptvio", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
614 { DBGFEVENT_EXIT_VMX_EPT_MISCONFIG, "exit_vmx_ept_misconfig", "eptmis", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
615 { DBGFEVENT_EXIT_VMX_VAPIC_ACCESS, "exit_vmx_vapic_access", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
616 { DBGFEVENT_EXIT_VMX_VAPIC_WRITE, "exit_vmx_vapic_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
617 { DBGFEVENT_EXIT_SVM_VMRUN, "exit_svm_vmrun", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
618 { DBGFEVENT_EXIT_SVM_VMLOAD, "exit_svm_vmload", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
619 { DBGFEVENT_EXIT_SVM_VMSAVE, "exit_svm_vmsave", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
620 { DBGFEVENT_EXIT_SVM_STGI, "exit_svm_stgi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
621 { DBGFEVENT_EXIT_SVM_CLGI, "exit_svm_clgi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
622 { DBGFEVENT_IOPORT_UNASSIGNED, "pio_unassigned", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
623 { DBGFEVENT_IOPORT_UNUSED, "pio_unused", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
624 { DBGFEVENT_MEMORY_UNASSIGNED, "mmio_unassigned", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
625 { DBGFEVENT_MEMORY_ROM_WRITE, "rom_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
626 { DBGFEVENT_BSOD_MSR, "bsod_msr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_BUGCHECK, NULL },
627 { DBGFEVENT_BSOD_EFI, "bsod_efi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_BUGCHECK, NULL },
628 { DBGFEVENT_BSOD_VMMDEV, "bsod_vmmdev", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_BUGCHECK, NULL },
629};
630/** Number of entries in g_aDbgcSxEvents. */
631const uint32_t g_cDbgcSxEvents = RT_ELEMENTS(g_aDbgcSxEvents);
632
633
634
635/**
636 * @callback_method_impl{FNDBGCCMD, The 'g' command.}
637 */
638static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
639{
640 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
641
642 /*
643 * Check if the VM is halted or not before trying to resume it.
644 */
645 if (!DBGFR3IsHalted(pUVM))
646 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The VM is already running");
647
648 int rc = DBGFR3Resume(pUVM);
649 if (RT_FAILURE(rc))
650 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3Resume");
651
652 NOREF(paArgs); NOREF(cArgs);
653 return VINF_SUCCESS;
654}
655
656
657/**
658 * @callback_method_impl{FNDBGCCMD, The 'gu' command.}
659 */
660static DECLCALLBACK(int) dbgcCmdGoUp(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
661{
662 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
663 RT_NOREF(pCmd, paArgs, cArgs);
664
665 /* The simple way out. */
666 PDBGFADDRESS pStackPop = NULL; /** @todo try set up some stack limitations */
667 RTGCPTR cbStackPop = 0;
668 int rc = DBGFR3StepEx(pUVM, pDbgc->idCpu, DBGF_STEP_F_OVER | DBGF_STEP_F_STOP_AFTER_RET, NULL, pStackPop, cbStackPop, _512K);
669 if (RT_SUCCESS(rc))
670 pDbgc->fReady = false;
671 else
672 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3StepEx(,,DBGF_STEP_F_OVER | DBGF_STEP_F_STOP_AFTER_RET,) failed");
673 return rc;
674}
675
676
677/**
678 * @callback_method_impl{FNDBGCCMD, The 'ba' command.}
679 */
680static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
681{
682 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
683
684 /*
685 * Interpret access type.
686 */
687 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
688 || paArgs[0].u.pszString[1])
689 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'",
690 paArgs[0].u.pszString, pCmd->pszCmd);
691 uint8_t fType = 0;
692 switch (paArgs[0].u.pszString[0])
693 {
694 case 'x': fType = X86_DR7_RW_EO; break;
695 case 'r': fType = X86_DR7_RW_RW; break;
696 case 'w': fType = X86_DR7_RW_WO; break;
697 case 'i': fType = X86_DR7_RW_IO; break;
698 }
699
700 /*
701 * Validate size.
702 */
703 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
704 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 'x' access type requires size 1!",
705 paArgs[1].u.u64Number, pCmd->pszCmd);
706 switch (paArgs[1].u.u64Number)
707 {
708 case 1:
709 case 2:
710 case 4:
711 break;
712 /*case 8: - later*/
713 default:
714 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 1, 2 or 4!",
715 paArgs[1].u.u64Number, pCmd->pszCmd);
716 }
717 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
718
719 /*
720 * Convert the pointer to a DBGF address.
721 */
722 DBGFADDRESS Address;
723 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
724 if (RT_FAILURE(rc))
725 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%DV,)", &paArgs[2]);
726
727 /*
728 * Pick out the optional arguments.
729 */
730 uint64_t iHitTrigger = 0;
731 uint64_t iHitDisable = UINT64_MAX;
732 const char *pszCmds = NULL;
733 unsigned iArg = 3;
734 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
735 {
736 iHitTrigger = paArgs[iArg].u.u64Number;
737 iArg++;
738 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
739 {
740 iHitDisable = paArgs[iArg].u.u64Number;
741 iArg++;
742 }
743 }
744 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
745 {
746 pszCmds = paArgs[iArg].u.pszString;
747 iArg++;
748 }
749
750 /*
751 * Try set the breakpoint.
752 */
753 uint32_t iBp;
754 rc = DBGFR3BpSetReg(pUVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
755 if (RT_SUCCESS(rc))
756 {
757 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
758 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
759 if (RT_SUCCESS(rc))
760 return DBGCCmdHlpPrintf(pCmdHlp, "Set access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
761 if (rc == VERR_DBGC_BP_EXISTS)
762 {
763 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
764 if (RT_SUCCESS(rc))
765 return DBGCCmdHlpPrintf(pCmdHlp, "Updated access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
766 }
767 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
768 AssertRC(rc2);
769 }
770 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set access breakpoint at %RGv", Address.FlatPtr);
771}
772
773
774/**
775 * @callback_method_impl{FNDBGCCMD, The 'bc' command.}
776 */
777static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
778{
779 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
780
781 /*
782 * Enumerate the arguments.
783 */
784 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
785 int rc = VINF_SUCCESS;
786 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
787 {
788 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
789 {
790 /* one */
791 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
792 if (iBp == paArgs[iArg].u.u64Number)
793 {
794 int rc2 = DBGFR3BpClear(pUVM, iBp);
795 if (RT_FAILURE(rc2))
796 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
797 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
798 dbgcBpDelete(pDbgc, iBp);
799 }
800 else
801 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
802 }
803 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
804 {
805 /* all */
806 PDBGCBP pBp = pDbgc->pFirstBp;
807 while (pBp)
808 {
809 uint32_t iBp = pBp->iBp;
810 pBp = pBp->pNext;
811
812 int rc2 = DBGFR3BpClear(pUVM, iBp);
813 if (RT_FAILURE(rc2))
814 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
815 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
816 dbgcBpDelete(pDbgc, iBp);
817 }
818 }
819 else
820 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
821 }
822 return rc;
823}
824
825
826/**
827 * @callback_method_impl{FNDBGCCMD, The 'bd' command.}
828 */
829static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
830{
831 /*
832 * Enumerate the arguments.
833 */
834 int rc = VINF_SUCCESS;
835 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
836 {
837 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
838 {
839 /* one */
840 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
841 if (iBp == paArgs[iArg].u.u64Number)
842 {
843 rc = DBGFR3BpDisable(pUVM, iBp);
844 if (RT_FAILURE(rc))
845 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpDisable failed for breakpoint %#x", iBp);
846 }
847 else
848 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
849 }
850 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
851 {
852 /* all */
853 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
854 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
855 {
856 int rc2 = DBGFR3BpDisable(pUVM, pBp->iBp);
857 if (RT_FAILURE(rc2))
858 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpDisable failed for breakpoint %#x", pBp->iBp);
859 }
860 }
861 else
862 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
863 }
864 return rc;
865}
866
867
868/**
869 * @callback_method_impl{FNDBGCCMD, The 'be' command.}
870 */
871static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
872{
873 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
874
875 /*
876 * Enumerate the arguments.
877 */
878 int rc = VINF_SUCCESS;
879 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
880 {
881 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
882 {
883 /* one */
884 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
885 if (iBp == paArgs[iArg].u.u64Number)
886 {
887 rc = DBGFR3BpEnable(pUVM, iBp);
888 if (RT_FAILURE(rc))
889 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnable failed for breakpoint %#x", iBp);
890 }
891 else
892 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
893 }
894 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
895 {
896 /* all */
897 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
898 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
899 {
900 int rc2 = DBGFR3BpEnable(pUVM, pBp->iBp);
901 if (RT_FAILURE(rc2))
902 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpEnable failed for breakpoint %#x", pBp->iBp);
903 }
904 }
905 else
906 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
907 }
908 return rc;
909}
910
911
912/**
913 * Breakpoint enumeration callback function.
914 *
915 * @returns VBox status code. Any failure will stop the enumeration.
916 * @param pUVM The user mode VM handle.
917 * @param pvUser The user argument.
918 * @param pBp Pointer to the breakpoint information. (readonly)
919 */
920static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PUVM pUVM, void *pvUser, PCDBGFBP pBp)
921{
922 PDBGC pDbgc = (PDBGC)pvUser;
923 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
924
925 /*
926 * BP type and size.
927 */
928 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%#4x %c ", pBp->iBp, pBp->fEnabled ? 'e' : 'd');
929 bool fHasAddress = false;
930 switch (pBp->enmType)
931 {
932 case DBGFBPTYPE_INT3:
933 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " p %RGv", pBp->u.Int3.GCPtr);
934 fHasAddress = true;
935 break;
936 case DBGFBPTYPE_REG:
937 {
938 char chType;
939 switch (pBp->u.Reg.fType)
940 {
941 case X86_DR7_RW_EO: chType = 'x'; break;
942 case X86_DR7_RW_WO: chType = 'w'; break;
943 case X86_DR7_RW_IO: chType = 'i'; break;
944 case X86_DR7_RW_RW: chType = 'r'; break;
945 default: chType = '?'; break;
946
947 }
948 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%d %c %RGv", pBp->u.Reg.cb, chType, pBp->u.Reg.GCPtr);
949 fHasAddress = true;
950 break;
951 }
952
953 case DBGFBPTYPE_REM:
954 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " r %RGv", pBp->u.Rem.GCPtr);
955 fHasAddress = true;
956 break;
957
958/** @todo realign the list when I/O and MMIO breakpoint command have been added and it's possible to test this code. */
959 case DBGFBPTYPE_PORT_IO:
960 case DBGFBPTYPE_MMIO:
961 {
962 uint32_t fAccess = pBp->enmType == DBGFBPTYPE_PORT_IO ? pBp->u.PortIo.fAccess : pBp->u.Mmio.fAccess;
963 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, pBp->enmType == DBGFBPTYPE_PORT_IO ? " i" : " m");
964 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
965 fAccess & DBGFBPIOACCESS_READ_MASK ? 'r' : '-',
966 fAccess & DBGFBPIOACCESS_READ_BYTE ? '1' : '-',
967 fAccess & DBGFBPIOACCESS_READ_WORD ? '2' : '-',
968 fAccess & DBGFBPIOACCESS_READ_DWORD ? '4' : '-',
969 fAccess & DBGFBPIOACCESS_READ_QWORD ? '8' : '-',
970 fAccess & DBGFBPIOACCESS_READ_OTHER ? '+' : '-');
971 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
972 fAccess & DBGFBPIOACCESS_WRITE_MASK ? 'w' : '-',
973 fAccess & DBGFBPIOACCESS_WRITE_BYTE ? '1' : '-',
974 fAccess & DBGFBPIOACCESS_WRITE_WORD ? '2' : '-',
975 fAccess & DBGFBPIOACCESS_WRITE_DWORD ? '4' : '-',
976 fAccess & DBGFBPIOACCESS_WRITE_QWORD ? '8' : '-',
977 fAccess & DBGFBPIOACCESS_WRITE_OTHER ? '+' : '-');
978 if (pBp->enmType == DBGFBPTYPE_PORT_IO)
979 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04x-%04x",
980 pBp->u.PortIo.uPort, pBp->u.PortIo.uPort + pBp->u.PortIo.cPorts - 1);
981 else
982 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%RGp LB %03x", pBp->u.Mmio.PhysAddr, pBp->u.Mmio.cb);
983 break;
984 }
985
986 default:
987 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " unknown type %d!!", pBp->enmType);
988 AssertFailed();
989 break;
990
991 }
992 if (pBp->iHitDisable == ~(uint64_t)0)
993 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to ~0) ", pBp->cHits, pBp->iHitTrigger);
994 else
995 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to %04RX64)", pBp->cHits, pBp->iHitTrigger, pBp->iHitDisable);
996
997 /*
998 * Try resolve the address if it has one.
999 */
1000 if (fHasAddress)
1001 {
1002 RTDBGSYMBOL Sym;
1003 RTINTPTR off;
1004 DBGFADDRESS Addr;
1005 int rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, pBp->u.GCPtr),
1006 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &off, &Sym, NULL);
1007 if (RT_SUCCESS(rc))
1008 {
1009 if (!off)
1010 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s", Sym.szName);
1011 else if (off > 0)
1012 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, off);
1013 else
1014 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s-%RGv", Sym.szName, -off);
1015 }
1016 }
1017
1018 /*
1019 * The commands.
1020 */
1021 if (pDbgcBp)
1022 {
1023 if (pDbgcBp->cchCmd)
1024 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n cmds: '%s'\n", pDbgcBp->szCmd);
1025 else
1026 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n");
1027 }
1028 else
1029 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " [unknown bp]\n");
1030
1031 return VINF_SUCCESS;
1032}
1033
1034
1035/**
1036 * @callback_method_impl{FNDBGCCMD, The 'bl' command.}
1037 */
1038static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1039{
1040 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1041 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
1042 NOREF(paArgs);
1043
1044 /*
1045 * Enumerate the breakpoints.
1046 */
1047 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1048 int rc = DBGFR3BpEnum(pUVM, dbgcEnumBreakpointsCallback, pDbgc);
1049 if (RT_FAILURE(rc))
1050 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnum");
1051 return rc;
1052}
1053
1054
1055/**
1056 * @callback_method_impl{FNDBGCCMD, The 'bp' command.}
1057 */
1058static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1059{
1060 /*
1061 * Convert the pointer to a DBGF address.
1062 */
1063 DBGFADDRESS Address;
1064 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1065 if (RT_FAILURE(rc))
1066 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
1067
1068 /*
1069 * Pick out the optional arguments.
1070 */
1071 uint64_t iHitTrigger = 0;
1072 uint64_t iHitDisable = UINT64_MAX;
1073 const char *pszCmds = NULL;
1074 unsigned iArg = 1;
1075 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1076 {
1077 iHitTrigger = paArgs[iArg].u.u64Number;
1078 iArg++;
1079 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1080 {
1081 iHitDisable = paArgs[iArg].u.u64Number;
1082 iArg++;
1083 }
1084 }
1085 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1086 {
1087 pszCmds = paArgs[iArg].u.pszString;
1088 iArg++;
1089 }
1090
1091 /*
1092 * Try set the breakpoint.
1093 */
1094 uint32_t iBp;
1095 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1096 rc = DBGFR3BpSetInt3(pUVM, pDbgc->idCpu, &Address, iHitTrigger, iHitDisable, &iBp);
1097 if (RT_SUCCESS(rc))
1098 {
1099 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1100 if (RT_SUCCESS(rc))
1101 return DBGCCmdHlpPrintf(pCmdHlp, "Set breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1102 if (rc == VERR_DBGC_BP_EXISTS)
1103 {
1104 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1105 if (RT_SUCCESS(rc))
1106 return DBGCCmdHlpPrintf(pCmdHlp, "Updated breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1107 }
1108 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
1109 AssertRC(rc2);
1110 }
1111 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set breakpoint at %RGv", Address.FlatPtr);
1112}
1113
1114
1115/**
1116 * @callback_method_impl{FNDBGCCMD, The 'br' command.}
1117 */
1118static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1119{
1120 /*
1121 * Convert the pointer to a DBGF address.
1122 */
1123 DBGFADDRESS Address;
1124 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1125 if (RT_FAILURE(rc))
1126 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
1127
1128 /*
1129 * Pick out the optional arguments.
1130 */
1131 uint64_t iHitTrigger = 0;
1132 uint64_t iHitDisable = UINT64_MAX;
1133 const char *pszCmds = NULL;
1134 unsigned iArg = 1;
1135 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1136 {
1137 iHitTrigger = paArgs[iArg].u.u64Number;
1138 iArg++;
1139 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1140 {
1141 iHitDisable = paArgs[iArg].u.u64Number;
1142 iArg++;
1143 }
1144 }
1145 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1146 {
1147 pszCmds = paArgs[iArg].u.pszString;
1148 iArg++;
1149 }
1150
1151 /*
1152 * Try set the breakpoint.
1153 */
1154 uint32_t iBp;
1155 rc = DBGFR3BpSetREM(pUVM, &Address, iHitTrigger, iHitDisable, &iBp);
1156 if (RT_SUCCESS(rc))
1157 {
1158 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1159 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1160 if (RT_SUCCESS(rc))
1161 return DBGCCmdHlpPrintf(pCmdHlp, "Set REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1162 if (rc == VERR_DBGC_BP_EXISTS)
1163 {
1164 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1165 if (RT_SUCCESS(rc))
1166 return DBGCCmdHlpPrintf(pCmdHlp, "Updated REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1167 }
1168 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
1169 AssertRC(rc2);
1170 }
1171 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set REM breakpoint at %RGv", Address.FlatPtr);
1172}
1173
1174
1175/**
1176 * Helps the unassmble ('u') command display symbols it starts at and passes.
1177 *
1178 * @param pUVM The user mode VM handle.
1179 * @param pCmdHlp The command helpers for printing via.
1180 * @param hDbgAs The address space to look up addresses in.
1181 * @param pAddress The current address.
1182 * @param pcbCallAgain Where to return the distance to the next check (in
1183 * instruction bytes).
1184 */
1185static void dbgcCmdUnassambleHelpListNear(PUVM pUVM, PDBGCCMDHLP pCmdHlp, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
1186 PRTUINTPTR pcbCallAgain)
1187{
1188 RTDBGSYMBOL Symbol;
1189 RTGCINTPTR offDispSym;
1190 int rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDispSym, &Symbol, NULL);
1191 if (RT_FAILURE(rc) || offDispSym > _1G)
1192 rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL, &offDispSym, &Symbol, NULL);
1193 if (RT_SUCCESS(rc) && offDispSym < _1G)
1194 {
1195 if (!offDispSym)
1196 {
1197 DBGCCmdHlpPrintf(pCmdHlp, "%s:\n", Symbol.szName);
1198 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb;
1199 }
1200 else if (offDispSym > 0)
1201 {
1202 DBGCCmdHlpPrintf(pCmdHlp, "%s+%#llx:\n", Symbol.szName, (uint64_t)offDispSym);
1203 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb > (RTGCUINTPTR)offDispSym ? Symbol.cb - (RTGCUINTPTR)offDispSym : 1;
1204 }
1205 else
1206 {
1207 DBGCCmdHlpPrintf(pCmdHlp, "%s-%#llx:\n", Symbol.szName, (uint64_t)-offDispSym);
1208 *pcbCallAgain = !Symbol.cb ? 64 : (RTGCUINTPTR)-offDispSym + Symbol.cb;
1209 }
1210 }
1211 else
1212 *pcbCallAgain = UINT32_MAX;
1213}
1214
1215
1216/**
1217 * @callback_method_impl{FNDBGCCMD, The 'u' command.}
1218 */
1219static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1220{
1221 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1222
1223 /*
1224 * Validate input.
1225 */
1226 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1227 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
1228 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
1229
1230 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1231 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
1232
1233 /*
1234 * Check the desired mode.
1235 */
1236 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS | DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
1237 switch (pCmd->pszCmd[1])
1238 {
1239 default: AssertFailed(); RT_FALL_THRU();
1240 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
1241 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
1242 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
1243 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
1244 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
1245 }
1246
1247 /** @todo should use DBGFADDRESS for everything */
1248
1249 /*
1250 * Find address.
1251 */
1252 if (!cArgs)
1253 {
1254 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1255 {
1256 /** @todo Batch query CS, RIP, CPU mode and flags. */
1257 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1258 if ( pDbgc->fRegCtxGuest
1259 && CPUMIsGuestIn64BitCode(pVCpu))
1260 {
1261 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
1262 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
1263 }
1264 else
1265 {
1266 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1267 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1268 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1269 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
1270 && pDbgc->fRegCtxGuest
1271 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
1272 {
1273 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1274 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
1275 }
1276 }
1277
1278 if (pDbgc->fRegCtxGuest)
1279 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1280 else
1281 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
1282 }
1283 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
1284 {
1285 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1286 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
1287 }
1288 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1289 }
1290 else
1291 pDbgc->DisasmPos = paArgs[0];
1292 pDbgc->pLastPos = &pDbgc->DisasmPos;
1293
1294 /*
1295 * Range.
1296 */
1297 switch (pDbgc->DisasmPos.enmRangeType)
1298 {
1299 case DBGCVAR_RANGE_NONE:
1300 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1301 pDbgc->DisasmPos.u64Range = 10;
1302 break;
1303
1304 case DBGCVAR_RANGE_ELEMENTS:
1305 if (pDbgc->DisasmPos.u64Range > 2048)
1306 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
1307 break;
1308
1309 case DBGCVAR_RANGE_BYTES:
1310 if (pDbgc->DisasmPos.u64Range > 65536)
1311 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
1312 break;
1313
1314 default:
1315 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
1316 }
1317
1318 /*
1319 * Convert physical and host addresses to guest addresses.
1320 */
1321 RTDBGAS hDbgAs = pDbgc->hDbgAs;
1322 int rc;
1323 switch (pDbgc->DisasmPos.enmType)
1324 {
1325 case DBGCVAR_TYPE_GC_FLAT:
1326 case DBGCVAR_TYPE_GC_FAR:
1327 break;
1328 case DBGCVAR_TYPE_GC_PHYS:
1329 hDbgAs = DBGF_AS_PHYS;
1330 RT_FALL_THRU();
1331 case DBGCVAR_TYPE_HC_FLAT:
1332 case DBGCVAR_TYPE_HC_PHYS:
1333 {
1334 DBGCVAR VarTmp;
1335 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1336 if (RT_FAILURE(rc))
1337 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
1338 pDbgc->DisasmPos = VarTmp;
1339 break;
1340 }
1341 default: AssertFailed(); break;
1342 }
1343
1344 DBGFADDRESS CurAddr;
1345 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1346 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1347 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1348 else
1349 {
1350 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1351 if (RT_FAILURE(rc))
1352 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
1353 }
1354
1355 if (CurAddr.fFlags & DBGFADDRESS_FLAGS_HMA)
1356 fFlags |= DBGF_DISAS_FLAGS_HYPER; /* This crap is due to not using DBGFADDRESS as DBGFR3Disas* input. */
1357 pDbgc->fDisasm = fFlags;
1358
1359 /*
1360 * Figure out where we are and display it. Also calculate when we need to
1361 * check for a new symbol if possible.
1362 */
1363 RTGCUINTPTR cbCheckSymbol;
1364 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1365
1366 /*
1367 * Do the disassembling.
1368 */
1369 unsigned cTries = 32;
1370 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1371 if (iRangeLeft == 0) /* kludge for 'r'. */
1372 iRangeLeft = -1;
1373 for (;;)
1374 {
1375 /*
1376 * Disassemble the instruction.
1377 */
1378 char szDis[256];
1379 uint32_t cbInstr = 1;
1380 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1381 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags,
1382 &szDis[0], sizeof(szDis), &cbInstr);
1383 else
1384 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags,
1385 &szDis[0], sizeof(szDis), &cbInstr);
1386 if (RT_SUCCESS(rc))
1387 {
1388 /* print it */
1389 rc = DBGCCmdHlpPrintf(pCmdHlp, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1390 if (RT_FAILURE(rc))
1391 return rc;
1392 }
1393 else
1394 {
1395 /* bitch. */
1396 int rc2 = DBGCCmdHlpPrintf(pCmdHlp, "Failed to disassemble instruction, skipping one byte.\n");
1397 if (RT_FAILURE(rc2))
1398 return rc2;
1399 if (cTries-- > 0)
1400 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Too many disassembly failures. Giving up");
1401 cbInstr = 1;
1402 }
1403
1404 /* advance */
1405 if (iRangeLeft < 0) /* 'r' */
1406 break;
1407 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1408 iRangeLeft--;
1409 else
1410 iRangeLeft -= cbInstr;
1411 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1412 if (RT_FAILURE(rc))
1413 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpEval(,,'(%Dv) + %x')", &pDbgc->DisasmPos, cbInstr);
1414 if (iRangeLeft <= 0)
1415 break;
1416 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1417
1418 /* Print next symbol? */
1419 if (cbCheckSymbol <= cbInstr)
1420 {
1421 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1422 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1423 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1424 else
1425 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1426 if (RT_SUCCESS(rc))
1427 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1428 else
1429 cbCheckSymbol = UINT32_MAX;
1430 }
1431 else
1432 cbCheckSymbol -= cbInstr;
1433 }
1434
1435 NOREF(pCmd);
1436 return VINF_SUCCESS;
1437}
1438
1439
1440/**
1441 * @callback_method_impl{FNDGCSCREENBLIT}
1442 */
1443static DECLCALLBACK(int) dbgcCmdUnassembleCfgBlit(const char *psz, void *pvUser)
1444{
1445 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pvUser;
1446 return DBGCCmdHlpPrintf(pCmdHlp, "%s", psz);
1447}
1448
1449
1450/**
1451 * Checks whether both addresses are equal.
1452 *
1453 * @returns true if both addresses point to the same location, false otherwise.
1454 * @param pAddr1 First address.
1455 * @param pAddr2 Second address.
1456 */
1457static bool dbgcCmdUnassembleCfgAddrEqual(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
1458{
1459 return pAddr1->Sel == pAddr2->Sel
1460 && pAddr1->off == pAddr2->off;
1461}
1462
1463
1464/**
1465 * Checks whether the first given address is lower than the second one.
1466 *
1467 * @returns true if both addresses point to the same location, false otherwise.
1468 * @param pAddr1 First address.
1469 * @param pAddr2 Second address.
1470 */
1471static bool dbgcCmdUnassembleCfgAddrLower(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
1472{
1473 return pAddr1->Sel == pAddr2->Sel
1474 && pAddr1->off < pAddr2->off;
1475}
1476
1477
1478/**
1479 * Calculates the size required for the given basic block including the
1480 * border and spacing on the edges.
1481 *
1482 * @returns nothing.
1483 * @param hFlowBb The basic block handle.
1484 * @param pDumpBb The dumper state to fill in for the basic block.
1485 */
1486static void dbgcCmdUnassembleCfgDumpCalcBbSize(DBGFFLOWBB hFlowBb, PDBGCFLOWBBDUMP pDumpBb)
1487{
1488 uint32_t fFlags = DBGFR3FlowBbGetFlags(hFlowBb);
1489 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(hFlowBb);
1490
1491 pDumpBb->hFlowBb = hFlowBb;
1492 pDumpBb->cchHeight = cInstr + 4; /* Include spacing and border top and bottom. */
1493 pDumpBb->cchWidth = 0;
1494 DBGFR3FlowBbGetStartAddress(hFlowBb, &pDumpBb->AddrStart);
1495
1496 DBGFFLOWBBENDTYPE enmType = DBGFR3FlowBbGetType(hFlowBb);
1497 if ( enmType == DBGFFLOWBBENDTYPE_COND
1498 || enmType == DBGFFLOWBBENDTYPE_UNCOND_JMP
1499 || enmType == DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP)
1500 DBGFR3FlowBbGetBranchAddress(hFlowBb, &pDumpBb->AddrTarget);
1501
1502 if (fFlags & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1503 {
1504 const char *pszErr = NULL;
1505 DBGFR3FlowBbQueryError(hFlowBb, &pszErr);
1506 if (pszErr)
1507 {
1508 pDumpBb->cchHeight++;
1509 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszErr));
1510 }
1511 }
1512 for (unsigned i = 0; i < cInstr; i++)
1513 {
1514 const char *pszInstr = NULL;
1515 int rc = DBGFR3FlowBbQueryInstr(hFlowBb, i, NULL, NULL, &pszInstr);
1516 AssertRC(rc);
1517 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszInstr));
1518 }
1519 pDumpBb->cchWidth += 4; /* Include spacing and border left and right. */
1520}
1521
1522
1523/**
1524 * Dumps a top or bottom boundary line.
1525 *
1526 * @returns nothing.
1527 * @param hScreen The screen to draw to.
1528 * @param uStartX Where to start drawing the boundary.
1529 * @param uStartY Y coordinate.
1530 * @param cchWidth Width of the boundary.
1531 * @param enmColor The color to use for drawing.
1532 */
1533static void dbgcCmdUnassembleCfgDumpBbBoundary(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1534 DBGCSCREENCOLOR enmColor)
1535{
1536 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '+', enmColor);
1537 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1538 uStartY, '-', enmColor);
1539 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '+', enmColor);
1540}
1541
1542
1543/**
1544 * Dumps a spacing line between the top or bottom boundary and the actual disassembly.
1545 *
1546 * @returns nothing.
1547 * @param hScreen The screen to draw to.
1548 * @param uStartX Where to start drawing the spacing.
1549 * @param uStartY Y coordinate.
1550 * @param cchWidth Width of the spacing.
1551 * @param enmColor The color to use for drawing.
1552 */
1553static void dbgcCmdUnassembleCfgDumpBbSpacing(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1554 DBGCSCREENCOLOR enmColor)
1555{
1556 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmColor);
1557 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1558 uStartY, ' ', enmColor);
1559 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmColor);
1560}
1561
1562
1563/**
1564 * Writes a given text to the screen.
1565 *
1566 * @returns nothing.
1567 * @param hScreen The screen to draw to.
1568 * @param uStartX Where to start drawing the line.
1569 * @param uStartY Y coordinate.
1570 * @param cchWidth Maximum width of the text.
1571 * @param pszText The text to write.
1572 * @param enmTextColor The color to use for drawing the text.
1573 * @param enmBorderColor The color to use for drawing the border.
1574 */
1575static void dbgcCmdUnassembleCfgDumpBbText(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY,
1576 uint32_t cchWidth, const char *pszText,
1577 DBGCSCREENCOLOR enmTextColor, DBGCSCREENCOLOR enmBorderColor)
1578{
1579 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmBorderColor);
1580 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + 1, uStartY, ' ', enmTextColor);
1581 dbgcScreenAsciiDrawString(hScreen, uStartX + 2, uStartY, pszText, enmTextColor);
1582 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmBorderColor);
1583}
1584
1585
1586/**
1587 * Dumps one basic block using the dumper callback.
1588 *
1589 * @returns nothing.
1590 * @param pDumpBb The basic block dump state to dump.
1591 * @param hScreen The screen to draw to.
1592 */
1593static void dbgcCmdUnassembleCfgDumpBb(PDBGCFLOWBBDUMP pDumpBb, DBGCSCREEN hScreen)
1594{
1595 uint32_t uStartY = pDumpBb->uStartY;
1596 bool fError = RT_BOOL(DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR);
1597 DBGCSCREENCOLOR enmColor = fError ? DBGCSCREENCOLOR_RED_BRIGHT : DBGCSCREENCOLOR_DEFAULT;
1598
1599 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1600 uStartY++;
1601 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1602 uStartY++;
1603
1604 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(pDumpBb->hFlowBb);
1605 for (unsigned i = 0; i < cInstr; i++)
1606 {
1607 const char *pszInstr = NULL;
1608 DBGFR3FlowBbQueryInstr(pDumpBb->hFlowBb, i, NULL, NULL, &pszInstr);
1609 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY + i,
1610 pDumpBb->cchWidth, pszInstr, DBGCSCREENCOLOR_DEFAULT,
1611 enmColor);
1612 }
1613 uStartY += cInstr;
1614
1615 if (fError)
1616 {
1617 const char *pszErr = NULL;
1618 DBGFR3FlowBbQueryError(pDumpBb->hFlowBb, &pszErr);
1619 if (pszErr)
1620 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY,
1621 pDumpBb->cchWidth, pszErr, enmColor,
1622 enmColor);
1623 uStartY++;
1624 }
1625
1626 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1627 uStartY++;
1628 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1629 uStartY++;
1630}
1631
1632
1633/**
1634 * Dumps one branch table using the dumper callback.
1635 *
1636 * @returns nothing.
1637 * @param pDumpBranchTbl The basic block dump state to dump.
1638 * @param hScreen The screen to draw to.
1639 */
1640static void dbgcCmdUnassembleCfgDumpBranchTbl(PDBGCFLOWBRANCHTBLDUMP pDumpBranchTbl, DBGCSCREEN hScreen)
1641{
1642 uint32_t uStartY = pDumpBranchTbl->uStartY;
1643 DBGCSCREENCOLOR enmColor = DBGCSCREENCOLOR_CYAN_BRIGHT;
1644
1645 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1646 uStartY++;
1647 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1648 uStartY++;
1649
1650 uint32_t cSlots = DBGFR3FlowBranchTblGetSlots(pDumpBranchTbl->hFlowBranchTbl);
1651 for (unsigned i = 0; i < cSlots; i++)
1652 {
1653 DBGFADDRESS Addr;
1654 char szAddr[128];
1655
1656 RT_ZERO(szAddr);
1657 DBGFR3FlowBranchTblGetAddrAtSlot(pDumpBranchTbl->hFlowBranchTbl, i, &Addr);
1658
1659 if (Addr.Sel == DBGF_SEL_FLAT)
1660 RTStrPrintf(&szAddr[0], sizeof(szAddr), "%RGv", Addr.FlatPtr);
1661 else
1662 RTStrPrintf(&szAddr[0], sizeof(szAddr), "%04x:%RGv", Addr.Sel, Addr.off);
1663
1664 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBranchTbl->uStartX, uStartY + i,
1665 pDumpBranchTbl->cchWidth, &szAddr[0], DBGCSCREENCOLOR_DEFAULT,
1666 enmColor);
1667 }
1668 uStartY += cSlots;
1669
1670 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1671 uStartY++;
1672 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1673 uStartY++;
1674}
1675
1676
1677/**
1678 * Fills in the dump states for the basic blocks and branch tables.
1679 *
1680 * @returns VBox status code.
1681 * @param hFlowIt The control flow graph iterator handle.
1682 * @param hFlowBranchTblIt The control flow graph branch table iterator handle.
1683 * @param paDumpBb The array of basic block dump states.
1684 * @param paDumpBranchTbl The array of branch table dump states.
1685 * @param cBbs Number of basic blocks.
1686 * @param cBranchTbls Number of branch tables.
1687 */
1688static int dbgcCmdUnassembleCfgDumpCalcDimensions(DBGFFLOWIT hFlowIt, DBGFFLOWBRANCHTBLIT hFlowBranchTblIt,
1689 PDBGCFLOWBBDUMP paDumpBb, PDBGCFLOWBRANCHTBLDUMP paDumpBranchTbl,
1690 uint32_t cBbs, uint32_t cBranchTbls)
1691{
1692 RT_NOREF2(cBbs, cBranchTbls);
1693
1694 /* Calculate the sizes of each basic block first. */
1695 DBGFFLOWBB hFlowBb = DBGFR3FlowItNext(hFlowIt);
1696 uint32_t idx = 0;
1697 while (hFlowBb)
1698 {
1699 dbgcCmdUnassembleCfgDumpCalcBbSize(hFlowBb, &paDumpBb[idx]);
1700 idx++;
1701 hFlowBb = DBGFR3FlowItNext(hFlowIt);
1702 }
1703
1704 if (paDumpBranchTbl)
1705 {
1706 idx = 0;
1707 DBGFFLOWBRANCHTBL hFlowBranchTbl = DBGFR3FlowBranchTblItNext(hFlowBranchTblIt);
1708 while (hFlowBranchTbl)
1709 {
1710 paDumpBranchTbl[idx].hFlowBranchTbl = hFlowBranchTbl;
1711 paDumpBranchTbl[idx].cchHeight = DBGFR3FlowBranchTblGetSlots(hFlowBranchTbl) + 4; /* Spacing and border. */
1712 paDumpBranchTbl[idx].cchWidth = 25 + 4; /* Spacing and border. */
1713 idx++;
1714 hFlowBranchTbl = DBGFR3FlowBranchTblItNext(hFlowBranchTblIt);
1715 }
1716 }
1717
1718 return VINF_SUCCESS;
1719}
1720
1721/**
1722 * Dumps the given control flow graph to the output.
1723 *
1724 * @returns VBox status code.
1725 * @param hCfg The control flow graph handle.
1726 * @param fUseColor Flag whether the output should be colorized.
1727 * @param pCmdHlp The command helper callback table.
1728 */
1729static int dbgcCmdUnassembleCfgDump(DBGFFLOW hCfg, bool fUseColor, PDBGCCMDHLP pCmdHlp)
1730{
1731 int rc = VINF_SUCCESS;
1732 DBGFFLOWIT hCfgIt = NULL;
1733 DBGFFLOWBRANCHTBLIT hFlowBranchTblIt = NULL;
1734 uint32_t cBbs = DBGFR3FlowGetBbCount(hCfg);
1735 uint32_t cBranchTbls = DBGFR3FlowGetBranchTblCount(hCfg);
1736 PDBGCFLOWBBDUMP paDumpBb = (PDBGCFLOWBBDUMP)RTMemTmpAllocZ(cBbs * sizeof(DBGCFLOWBBDUMP));
1737 PDBGCFLOWBRANCHTBLDUMP paDumpBranchTbl = NULL;
1738
1739 if (cBranchTbls)
1740 paDumpBranchTbl = (PDBGCFLOWBRANCHTBLDUMP)RTMemAllocZ(cBranchTbls * sizeof(DBGCFLOWBRANCHTBLDUMP));
1741
1742 if (RT_UNLIKELY(!paDumpBb || (!paDumpBranchTbl && cBranchTbls > 0)))
1743 rc = VERR_NO_MEMORY;
1744 if (RT_SUCCESS(rc))
1745 rc = DBGFR3FlowItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hCfgIt);
1746 if (RT_SUCCESS(rc) && cBranchTbls > 0)
1747 rc = DBGFR3FlowBranchTblItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hFlowBranchTblIt);
1748
1749 if (RT_SUCCESS(rc))
1750 {
1751 rc = dbgcCmdUnassembleCfgDumpCalcDimensions(hCfgIt, hFlowBranchTblIt, paDumpBb, paDumpBranchTbl,
1752 cBbs, cBranchTbls);
1753
1754 /* Calculate the ASCII screen dimensions and create one. */
1755 uint32_t cchWidth = 0;
1756 uint32_t cchLeftExtra = 5;
1757 uint32_t cchRightExtra = 5;
1758 uint32_t cchHeight = 0;
1759 for (unsigned i = 0; i < cBbs; i++)
1760 {
1761 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1762 cchWidth = RT_MAX(cchWidth, pDumpBb->cchWidth);
1763 cchHeight += pDumpBb->cchHeight;
1764
1765 /* Incomplete blocks don't have a successor. */
1766 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1767 continue;
1768
1769 switch (DBGFR3FlowBbGetType(pDumpBb->hFlowBb))
1770 {
1771 case DBGFFLOWBBENDTYPE_EXIT:
1772 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1773 break;
1774 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1775 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1776 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1777 cchLeftExtra++;
1778 else
1779 cchRightExtra++;
1780 break;
1781 case DBGFFLOWBBENDTYPE_UNCOND:
1782 cchHeight += 2; /* For the arrow down to the next basic block. */
1783 break;
1784 case DBGFFLOWBBENDTYPE_COND:
1785 cchHeight += 2; /* For the arrow down to the next basic block. */
1786 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1787 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1788 cchLeftExtra++;
1789 else
1790 cchRightExtra++;
1791 break;
1792 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1793 default:
1794 AssertFailed();
1795 }
1796 }
1797
1798 for (unsigned i = 0; i < cBranchTbls; i++)
1799 {
1800 PDBGCFLOWBRANCHTBLDUMP pDumpBranchTbl = &paDumpBranchTbl[i];
1801 cchWidth = RT_MAX(cchWidth, pDumpBranchTbl->cchWidth);
1802 cchHeight += pDumpBranchTbl->cchHeight;
1803 }
1804
1805 cchWidth += 2;
1806
1807 DBGCSCREEN hScreen = NULL;
1808 rc = dbgcScreenAsciiCreate(&hScreen, cchWidth + cchLeftExtra + cchRightExtra, cchHeight);
1809 if (RT_SUCCESS(rc))
1810 {
1811 uint32_t uY = 0;
1812
1813 /* Dump the branch tables first. */
1814 for (unsigned i = 0; i < cBranchTbls; i++)
1815 {
1816 paDumpBranchTbl[i].uStartX = cchLeftExtra + (cchWidth - paDumpBranchTbl[i].cchWidth) / 2;
1817 paDumpBranchTbl[i].uStartY = uY;
1818 dbgcCmdUnassembleCfgDumpBranchTbl(&paDumpBranchTbl[i], hScreen);
1819 uY += paDumpBranchTbl[i].cchHeight;
1820 }
1821
1822 /* Dump the basic blocks and connections to the immediate successor. */
1823 for (unsigned i = 0; i < cBbs; i++)
1824 {
1825 paDumpBb[i].uStartX = cchLeftExtra + (cchWidth - paDumpBb[i].cchWidth) / 2;
1826 paDumpBb[i].uStartY = uY;
1827 dbgcCmdUnassembleCfgDumpBb(&paDumpBb[i], hScreen);
1828 uY += paDumpBb[i].cchHeight;
1829
1830 /* Incomplete blocks don't have a successor. */
1831 if (DBGFR3FlowBbGetFlags(paDumpBb[i].hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1832 continue;
1833
1834 switch (DBGFR3FlowBbGetType(paDumpBb[i].hFlowBb))
1835 {
1836 case DBGFFLOWBBENDTYPE_EXIT:
1837 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1838 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1839 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1840 break;
1841 case DBGFFLOWBBENDTYPE_UNCOND:
1842 /* Draw the arrow down to the next block. */
1843 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1844 '|', DBGCSCREENCOLOR_BLUE_BRIGHT);
1845 uY++;
1846 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1847 'V', DBGCSCREENCOLOR_BLUE_BRIGHT);
1848 uY++;
1849 break;
1850 case DBGFFLOWBBENDTYPE_COND:
1851 /* Draw the arrow down to the next block. */
1852 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1853 '|', DBGCSCREENCOLOR_RED_BRIGHT);
1854 uY++;
1855 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1856 'V', DBGCSCREENCOLOR_RED_BRIGHT);
1857 uY++;
1858 break;
1859 default:
1860 AssertFailed();
1861 }
1862 }
1863
1864 /* Last pass, connect all remaining branches. */
1865 uint32_t uBackConns = 0;
1866 uint32_t uFwdConns = 0;
1867 for (unsigned i = 0; i < cBbs; i++)
1868 {
1869 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1870 DBGFFLOWBBENDTYPE enmEndType = DBGFR3FlowBbGetType(pDumpBb->hFlowBb);
1871
1872 /* Incomplete blocks don't have a successor. */
1873 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1874 continue;
1875
1876 switch (enmEndType)
1877 {
1878 case DBGFFLOWBBENDTYPE_EXIT:
1879 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1880 case DBGFFLOWBBENDTYPE_UNCOND:
1881 break;
1882 case DBGFFLOWBBENDTYPE_COND:
1883 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1884 {
1885 /* Find the target first to get the coordinates. */
1886 PDBGCFLOWBBDUMP pDumpBbTgt = NULL;
1887 for (unsigned idxDumpBb = 0; idxDumpBb < cBbs; idxDumpBb++)
1888 {
1889 pDumpBbTgt = &paDumpBb[idxDumpBb];
1890 if (dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBbTgt->AddrStart))
1891 break;
1892 }
1893
1894 DBGCSCREENCOLOR enmColor = enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
1895 ? DBGCSCREENCOLOR_YELLOW_BRIGHT
1896 : DBGCSCREENCOLOR_GREEN_BRIGHT;
1897
1898 /*
1899 * Use the right side for targets with higher addresses,
1900 * left when jumping backwards.
1901 */
1902 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1903 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1904 {
1905 /* Going backwards. */
1906 uint32_t uXVerLine = /*cchLeftExtra - 1 -*/ uBackConns + 1;
1907 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1908 uBackConns++;
1909
1910 /* Draw the arrow pointing to the target block. */
1911 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX - 1, pDumpBbTgt->uStartY,
1912 '>', enmColor);
1913 /* Draw the horizontal line. */
1914 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBbTgt->uStartX - 2,
1915 pDumpBbTgt->uStartY, '-', enmColor);
1916 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1917 enmColor);
1918 /* Draw the vertical line down to the source block. */
1919 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, pDumpBbTgt->uStartY + 1, uYHorLine - 1,
1920 '|', enmColor);
1921 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1922 /* Draw the horizontal connection between the source block and vertical part. */
1923 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBb->uStartX - 1,
1924 uYHorLine, '-', enmColor);
1925
1926 }
1927 else
1928 {
1929 /* Going forward. */
1930 uint32_t uXVerLine = cchWidth + cchLeftExtra + (cchRightExtra - uFwdConns) - 1;
1931 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1932 uFwdConns++;
1933
1934 /* Draw the horizontal line. */
1935 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBb->uStartX + pDumpBb->cchWidth,
1936 uXVerLine - 1, uYHorLine, '-', enmColor);
1937 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1938 /* Draw the vertical line down to the target block. */
1939 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, uYHorLine + 1, pDumpBbTgt->uStartY - 1,
1940 '|', enmColor);
1941 /* Draw the horizontal connection between the target block and vertical part. */
1942 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1943 uXVerLine, pDumpBbTgt->uStartY, '-', enmColor);
1944 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1945 enmColor);
1946 /* Draw the arrow pointing to the target block. */
1947 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1948 pDumpBbTgt->uStartY, '<', enmColor);
1949 }
1950 break;
1951 }
1952 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1953 default:
1954 AssertFailed();
1955 }
1956 }
1957
1958 rc = dbgcScreenAsciiBlit(hScreen, dbgcCmdUnassembleCfgBlit, pCmdHlp, fUseColor);
1959 dbgcScreenAsciiDestroy(hScreen);
1960 }
1961 }
1962
1963 if (paDumpBb)
1964 {
1965 for (unsigned i = 0; i < cBbs; i++)
1966 DBGFR3FlowBbRelease(paDumpBb[i].hFlowBb);
1967 RTMemTmpFree(paDumpBb);
1968 }
1969
1970 if (paDumpBranchTbl)
1971 {
1972 for (unsigned i = 0; i < cBranchTbls; i++)
1973 DBGFR3FlowBranchTblRelease(paDumpBranchTbl[i].hFlowBranchTbl);
1974 RTMemTmpFree(paDumpBranchTbl);
1975 }
1976
1977 if (hCfgIt)
1978 DBGFR3FlowItDestroy(hCfgIt);
1979 if (hFlowBranchTblIt)
1980 DBGFR3FlowBranchTblItDestroy(hFlowBranchTblIt);
1981
1982 return rc;
1983}
1984
1985
1986/**
1987 * @callback_method_impl{FNDBGCCMD, The 'ucfg' command.}
1988 */
1989static DECLCALLBACK(int) dbgcCmdUnassembleCfg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1990{
1991 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1992
1993 /*
1994 * Validate input.
1995 */
1996 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1997 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
1998 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
1999
2000 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
2001 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
2002
2003 /*
2004 * Check the desired mode.
2005 */
2006 unsigned fFlags = DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
2007 bool fUseColor = false;
2008 switch (pCmd->pszCmd[4])
2009 {
2010 default: AssertFailed(); RT_FALL_THRU();
2011 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
2012 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
2013 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
2014 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
2015 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
2016 case 'c': fUseColor = true; break;
2017 }
2018
2019 /** @todo should use DBGFADDRESS for everything */
2020
2021 /*
2022 * Find address.
2023 */
2024 if (!cArgs)
2025 {
2026 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
2027 {
2028 /** @todo Batch query CS, RIP, CPU mode and flags. */
2029 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2030 if ( pDbgc->fRegCtxGuest
2031 && CPUMIsGuestIn64BitCode(pVCpu))
2032 {
2033 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
2034 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
2035 }
2036 else
2037 {
2038 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
2039 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
2040 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
2041 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
2042 && pDbgc->fRegCtxGuest
2043 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
2044 {
2045 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
2046 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
2047 }
2048 }
2049
2050 if (pDbgc->fRegCtxGuest)
2051 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
2052 else
2053 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
2054 }
2055 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
2056 {
2057 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
2058 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
2059 }
2060 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
2061 }
2062 else
2063 pDbgc->DisasmPos = paArgs[0];
2064 pDbgc->pLastPos = &pDbgc->DisasmPos;
2065
2066 /*
2067 * Range.
2068 */
2069 switch (pDbgc->DisasmPos.enmRangeType)
2070 {
2071 case DBGCVAR_RANGE_NONE:
2072 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2073 pDbgc->DisasmPos.u64Range = 10;
2074 break;
2075
2076 case DBGCVAR_RANGE_ELEMENTS:
2077 if (pDbgc->DisasmPos.u64Range > 2048)
2078 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
2079 break;
2080
2081 case DBGCVAR_RANGE_BYTES:
2082 if (pDbgc->DisasmPos.u64Range > 65536)
2083 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
2084 break;
2085
2086 default:
2087 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
2088 }
2089
2090 /*
2091 * Convert physical and host addresses to guest addresses.
2092 */
2093 RTDBGAS hDbgAs = pDbgc->hDbgAs;
2094 int rc;
2095 switch (pDbgc->DisasmPos.enmType)
2096 {
2097 case DBGCVAR_TYPE_GC_FLAT:
2098 case DBGCVAR_TYPE_GC_FAR:
2099 break;
2100 case DBGCVAR_TYPE_GC_PHYS:
2101 hDbgAs = DBGF_AS_PHYS;
2102 RT_FALL_THRU();
2103 case DBGCVAR_TYPE_HC_FLAT:
2104 case DBGCVAR_TYPE_HC_PHYS:
2105 {
2106 DBGCVAR VarTmp;
2107 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
2108 if (RT_FAILURE(rc))
2109 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
2110 pDbgc->DisasmPos = VarTmp;
2111 break;
2112 }
2113 default: AssertFailed(); break;
2114 }
2115
2116 DBGFADDRESS CurAddr;
2117 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
2118 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
2119 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
2120 else
2121 {
2122 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
2123 if (RT_FAILURE(rc))
2124 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
2125 }
2126
2127 DBGFFLOW hCfg;
2128 rc = DBGFR3FlowCreate(pUVM, pDbgc->idCpu, &CurAddr, 0 /*cbDisasmMax*/,
2129 DBGF_FLOW_CREATE_F_TRY_RESOLVE_INDIRECT_BRANCHES, fFlags, &hCfg);
2130 if (RT_SUCCESS(rc))
2131 {
2132 /* Dump the graph. */
2133 rc = dbgcCmdUnassembleCfgDump(hCfg, fUseColor, pCmdHlp);
2134 DBGFR3FlowRelease(hCfg);
2135 }
2136 else
2137 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3FlowCreate failed on '%Dv'", &pDbgc->DisasmPos);
2138
2139 NOREF(pCmd);
2140 return rc;
2141}
2142
2143
2144/**
2145 * @callback_method_impl{FNDBGCCMD, The 'ls' command.}
2146 */
2147static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2148{
2149 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2150
2151 /*
2152 * Validate input.
2153 */
2154 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
2155 if (cArgs == 1)
2156 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
2157 if (!pUVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
2158 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start listing...");
2159 if (!pUVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
2160 return DBGCCmdHlpFail(pCmdHlp, pCmd, "GC address but no VM");
2161
2162 /*
2163 * Find address.
2164 */
2165 if (!cArgs)
2166 {
2167 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
2168 {
2169 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2170 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
2171 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
2172 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
2173 }
2174 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
2175 }
2176 else
2177 pDbgc->SourcePos = paArgs[0];
2178 pDbgc->pLastPos = &pDbgc->SourcePos;
2179
2180 /*
2181 * Ensure the source address is flat GC.
2182 */
2183 switch (pDbgc->SourcePos.enmType)
2184 {
2185 case DBGCVAR_TYPE_GC_FLAT:
2186 break;
2187 case DBGCVAR_TYPE_GC_PHYS:
2188 case DBGCVAR_TYPE_GC_FAR:
2189 case DBGCVAR_TYPE_HC_FLAT:
2190 case DBGCVAR_TYPE_HC_PHYS:
2191 {
2192 int rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
2193 if (RT_FAILURE(rc))
2194 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid address or address type. (rc=%d)\n", rc);
2195 break;
2196 }
2197 default: AssertFailed(); break;
2198 }
2199
2200 /*
2201 * Range.
2202 */
2203 switch (pDbgc->SourcePos.enmRangeType)
2204 {
2205 case DBGCVAR_RANGE_NONE:
2206 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2207 pDbgc->SourcePos.u64Range = 10;
2208 break;
2209
2210 case DBGCVAR_RANGE_ELEMENTS:
2211 if (pDbgc->SourcePos.u64Range > 2048)
2212 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many lines requested. Max is 2048 lines.\n");
2213 break;
2214
2215 case DBGCVAR_RANGE_BYTES:
2216 if (pDbgc->SourcePos.u64Range > 65536)
2217 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
2218 break;
2219
2220 default:
2221 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
2222 }
2223
2224 /*
2225 * Do the disassembling.
2226 */
2227 bool fFirst = 1;
2228 RTDBGLINE LinePrev = { 0, 0, 0, 0, 0, "" };
2229 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
2230 if (iRangeLeft == 0) /* kludge for 'r'. */
2231 iRangeLeft = -1;
2232 for (;;)
2233 {
2234 /*
2235 * Get line info.
2236 */
2237 RTDBGLINE Line;
2238 RTGCINTPTR off;
2239 DBGFADDRESS SourcePosAddr;
2240 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->SourcePos, &SourcePosAddr);
2241 if (RT_FAILURE(rc))
2242 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%Dv)", &pDbgc->SourcePos);
2243 rc = DBGFR3AsLineByAddr(pUVM, pDbgc->hDbgAs, &SourcePosAddr, &off, &Line, NULL);
2244 if (RT_FAILURE(rc))
2245 return VINF_SUCCESS;
2246
2247 unsigned cLines = 0;
2248 if (memcmp(&Line, &LinePrev, sizeof(Line)))
2249 {
2250 /*
2251 * Print filenamename
2252 */
2253 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
2254 fFirst = true;
2255 if (fFirst)
2256 {
2257 rc = DBGCCmdHlpPrintf(pCmdHlp, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
2258 if (RT_FAILURE(rc))
2259 return rc;
2260 }
2261
2262 /*
2263 * Try open the file and read the line.
2264 */
2265 FILE *phFile = fopen(Line.szFilename, "r");
2266 if (phFile)
2267 {
2268 /* Skip ahead to the desired line. */
2269 char szLine[4096];
2270 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
2271 if (cBefore > 7)
2272 cBefore = 0;
2273 unsigned cLeft = Line.uLineNo - cBefore;
2274 while (cLeft > 0)
2275 {
2276 szLine[0] = '\0';
2277 if (!fgets(szLine, sizeof(szLine), phFile))
2278 break;
2279 cLeft--;
2280 }
2281 if (!cLeft)
2282 {
2283 /* print the before lines */
2284 for (;;)
2285 {
2286 size_t cch = strlen(szLine);
2287 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || RT_C_IS_SPACE(szLine[cch - 1])) )
2288 szLine[--cch] = '\0';
2289 if (cBefore-- <= 0)
2290 break;
2291
2292 rc = DBGCCmdHlpPrintf(pCmdHlp, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
2293 szLine[0] = '\0';
2294 const char *pszShutUpGcc = fgets(szLine, sizeof(szLine), phFile); NOREF(pszShutUpGcc);
2295 cLines++;
2296 }
2297 /* print the actual line */
2298 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
2299 }
2300 fclose(phFile);
2301 if (RT_FAILURE(rc))
2302 return rc;
2303 fFirst = false;
2304 }
2305 else
2306 return DBGCCmdHlpPrintf(pCmdHlp, "Warning: couldn't open source file '%s'\n", Line.szFilename);
2307
2308 LinePrev = Line;
2309 }
2310
2311
2312 /*
2313 * Advance
2314 */
2315 if (iRangeLeft < 0) /* 'r' */
2316 break;
2317 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2318 iRangeLeft -= cLines;
2319 else
2320 iRangeLeft -= 1;
2321 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
2322 if (RT_FAILURE(rc))
2323 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
2324 if (iRangeLeft <= 0)
2325 break;
2326 }
2327
2328 NOREF(pCmd);
2329 return 0;
2330}
2331
2332
2333/**
2334 * @callback_method_impl{FNDBGCCMD, The 'r' command.}
2335 */
2336static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2337{
2338 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2339 if (!pDbgc->fRegCtxGuest)
2340 return dbgcCmdRegHyper(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2341 return dbgcCmdRegGuest(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2342}
2343
2344
2345/**
2346 * @callback_method_impl{FNDBGCCMD, Common worker for the dbgcCmdReg*()
2347 * commands.}
2348 */
2349static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs,
2350 const char *pszPrefix)
2351{
2352 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2353 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1 || cArgs == 2 || cArgs == 3);
2354 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING
2355 || paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL);
2356
2357 /*
2358 * Parse the register name and kind.
2359 */
2360 const char *pszReg = paArgs[0].u.pszString;
2361 if (*pszReg == '@')
2362 pszReg++;
2363 VMCPUID idCpu = pDbgc->idCpu;
2364 if (*pszPrefix)
2365 idCpu |= DBGFREG_HYPER_VMCPUID;
2366 if (*pszReg == '.')
2367 {
2368 pszReg++;
2369 idCpu |= DBGFREG_HYPER_VMCPUID;
2370 }
2371 const char * const pszActualPrefix = idCpu & DBGFREG_HYPER_VMCPUID ? "." : "";
2372
2373 /*
2374 * Query the register type & value (the setter needs the type).
2375 */
2376 DBGFREGVALTYPE enmType;
2377 DBGFREGVAL Value;
2378 int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Value, &enmType);
2379 if (RT_FAILURE(rc))
2380 {
2381 if (rc == VERR_DBGF_REGISTER_NOT_FOUND)
2382 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown register: '%s%s'.\n",
2383 pszActualPrefix, pszReg);
2384 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmQuery failed querying '%s%s': %Rrc.\n",
2385 pszActualPrefix, pszReg, rc);
2386 }
2387 if (cArgs == 1)
2388 {
2389 /*
2390 * Show the register.
2391 */
2392 char szValue[160];
2393 rc = DBGFR3RegFormatValue(szValue, sizeof(szValue), &Value, enmType, true /*fSpecial*/);
2394 if (RT_SUCCESS(rc))
2395 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s%s=%s\n", pszActualPrefix, pszReg, szValue);
2396 else
2397 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2398 }
2399 else
2400 {
2401 DBGCVAR NewValueTmp;
2402 PCDBGCVAR pNewValue;
2403 if (cArgs == 3)
2404 {
2405 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, paArgs[1].enmType == DBGCVAR_TYPE_STRING);
2406 if (strcmp(paArgs[1].u.pszString, "="))
2407 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Second argument must be '='.");
2408 pNewValue = &paArgs[2];
2409 }
2410 else
2411 {
2412 /* Not possible to convince the parser to support both codeview and
2413 windbg syntax and make the equal sign optional. Try help it. */
2414 /** @todo make DBGCCmdHlpConvert do more with strings. */
2415 rc = DBGCCmdHlpConvert(pCmdHlp, &paArgs[1], DBGCVAR_TYPE_NUMBER, true /*fConvSyms*/, &NewValueTmp);
2416 if (RT_FAILURE(rc))
2417 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "The last argument must be a value or valid symbol.");
2418 pNewValue = &NewValueTmp;
2419 }
2420
2421 /*
2422 * Modify the register.
2423 */
2424 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pNewValue->enmType == DBGCVAR_TYPE_NUMBER);
2425 if (enmType != DBGFREGVALTYPE_DTR)
2426 {
2427 enmType = DBGFREGVALTYPE_U64;
2428 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.u64);
2429 }
2430 else
2431 {
2432 enmType = DBGFREGVALTYPE_DTR;
2433 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.dtr.u64Base);
2434 if (RT_SUCCESS(rc) && pNewValue->enmRangeType != DBGCVAR_RANGE_NONE)
2435 Value.dtr.u32Limit = (uint32_t)pNewValue->u64Range;
2436 }
2437 if (RT_SUCCESS(rc))
2438 {
2439 rc = DBGFR3RegNmSet(pUVM, idCpu, pszReg, &Value, enmType);
2440 if (RT_FAILURE(rc))
2441 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmSet failed settings '%s%s': %Rrc\n",
2442 pszActualPrefix, pszReg, rc);
2443 if (rc != VINF_SUCCESS)
2444 DBGCCmdHlpPrintf(pCmdHlp, "%s: warning: %Rrc\n", pCmd->pszCmd, rc);
2445 }
2446 else
2447 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2448 }
2449 return rc;
2450}
2451
2452
2453/**
2454 * @callback_method_impl{FNDBGCCMD,
2455 * The 'rg'\, 'rg64' and 'rg32' commands\, worker for 'r'.}
2456 */
2457static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2458{
2459 /*
2460 * Show all registers our selves.
2461 */
2462 if (cArgs == 0)
2463 {
2464 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2465 bool const f64BitMode = !strcmp(pCmd->pszCmd, "rg64")
2466 || ( strcmp(pCmd->pszCmd, "rg32") != 0
2467 && DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu));
2468 char szDisAndRegs[8192];
2469 int rc;
2470
2471 if (pDbgc->fRegTerse)
2472 {
2473 if (f64BitMode)
2474 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2475 "u %016VR{rip} L 0\n"
2476 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2477 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2478 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2479 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2480 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2481 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} rflags=%08VR{rflags}\n");
2482 else
2483 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2484 "u %04VR{cs}:%08VR{eip} L 0\n"
2485 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2486 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2487 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} eflags=%08VR{eflags}\n");
2488 }
2489 else
2490 {
2491 if (f64BitMode)
2492 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2493 "u %016VR{rip} L 0\n"
2494 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2495 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2496 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2497 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2498 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2499 "cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
2500 "ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
2501 "es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
2502 "fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
2503 "gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
2504 "ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2505 "dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
2506 "dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
2507 "gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
2508 "ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
2509 "tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
2510 " sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2511 " efer=%016VR{efer}\n"
2512 " pat=%016VR{pat}\n"
2513 " sf_mask=%016VR{sf_mask}\n"
2514 "krnl_gs_base=%016VR{krnl_gs_base}\n"
2515 " lstar=%016VR{lstar}\n"
2516 " star=%016VR{star} cstar=%016VR{cstar}\n"
2517 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2518 );
2519 else
2520 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2521 "u %04VR{cs}:%08VR{eip} L 0\n"
2522 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2523 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2524 "cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} dr0=%08VR{dr0} dr1=%08VR{dr1}\n"
2525 "ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} dr2=%08VR{dr2} dr3=%08VR{dr3}\n"
2526 "es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} dr6=%08VR{dr6} dr7=%08VR{dr7}\n"
2527 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr2}\n"
2528 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr3} cr4=%08VR{cr4}\n"
2529 "ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}} cr8=%08VR{cr8}\n"
2530 "gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} idtr=%08VR{idtr_base}:%04VR{idtr_lim} eflags=%08VR{eflags}\n"
2531 "ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2532 "tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2533 "sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2534 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2535 );
2536 }
2537 if (RT_FAILURE(rc))
2538 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2539 char *pszRegs = strchr(szDisAndRegs, '\n');
2540 *pszRegs++ = '\0';
2541 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2542
2543 /*
2544 * Disassemble one instruction at cs:[r|e]ip.
2545 */
2546 if (!f64BitMode && strstr(pszRegs, " vm ")) /* a bit ugly... */
2547 return pCmdHlp->pfnExec(pCmdHlp, "uv86 %s", szDisAndRegs + 2);
2548 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2549 }
2550 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, "");
2551}
2552
2553
2554/**
2555 * @callback_method_impl{FNDBGCCMD, The 'rh' command.}
2556 */
2557static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2558{
2559 /*
2560 * Show all registers our selves.
2561 */
2562 if (cArgs == 0)
2563 {
2564 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2565 char szDisAndRegs[8192];
2566 int rc;
2567
2568 if (pDbgc->fRegTerse)
2569 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2570 "u %VR{cs}:%VR{eip} L 0\n"
2571 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2572 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2573 ".cs=%04VR{cs} .ds=%04VR{ds} .es=%04VR{es} .fs=%04VR{fs} .gs=%04VR{gs} .ss=%04VR{ss} .eflags=%08VR{eflags}\n");
2574 else
2575 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2576 "u %04VR{cs}:%08VR{eip} L 0\n"
2577 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2578 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2579 ".cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} .dr0=%08VR{dr0} .dr1=%08VR{dr1}\n"
2580 ".ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} .dr2=%08VR{dr2} .dr3=%08VR{dr3}\n"
2581 ".es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} .dr6=%08VR{dr6} .dr6=%08VR{dr6}\n"
2582 ".fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} .cr3=%016VR{cr3}\n"
2583 ".gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}}\n"
2584 ".ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2585 ".gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} .idtr=%08VR{idtr_base}:%04VR{idtr_lim} .eflags=%08VR{eflags}\n"
2586 ".ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2587 ".tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2588 );
2589 if (RT_FAILURE(rc))
2590 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2591 char *pszRegs = strchr(szDisAndRegs, '\n');
2592 *pszRegs++ = '\0';
2593 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2594
2595 /*
2596 * Disassemble one instruction at cs:[r|e]ip.
2597 */
2598 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2599 }
2600 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, ".");
2601}
2602
2603
2604/**
2605 * @callback_method_impl{FNDBGCCMD, The 'rt' command.}
2606 */
2607static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2608{
2609 NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
2610
2611 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2612 pDbgc->fRegTerse = !pDbgc->fRegTerse;
2613 return DBGCCmdHlpPrintf(pCmdHlp, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
2614}
2615
2616
2617/**
2618 * @callback_method_impl{FNDBGCCMD, The 'pr' and 'tr' commands.}
2619 */
2620static DECLCALLBACK(int) dbgcCmdStepTraceToggle(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2621{
2622 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2623 Assert(cArgs == 0); NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
2624
2625 /* Note! windbg accepts 'r' as a flag to 'p', 'pa', 'pc', 'pt', 't',
2626 'ta', 'tc' and 'tt'. We've simplified it. */
2627 pDbgc->fStepTraceRegs = !pDbgc->fStepTraceRegs;
2628 return VINF_SUCCESS;
2629}
2630
2631
2632/**
2633 * @callback_method_impl{FNDBGCCMD, The 'p'\, 'pc'\, 'pt'\, 't'\, 'tc'\, and 'tt' commands.}
2634 */
2635static DECLCALLBACK(int) dbgcCmdStepTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2636{
2637 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2638 if (cArgs != 0)
2639 return DBGCCmdHlpFail(pCmdHlp, pCmd,
2640 "Sorry, but the '%s' command does not currently implement any arguments.\n", pCmd->pszCmd);
2641
2642 /* The 'count' has to be implemented by DBGC, whereas the
2643 filtering is taken care of by DBGF. */
2644
2645 /*
2646 * Convert the command to DBGF_STEP_F_XXX and other API input.
2647 */
2648 //DBGFADDRESS StackPop;
2649 PDBGFADDRESS pStackPop = NULL;
2650 RTGCPTR cbStackPop = 0;
2651 uint32_t cMaxSteps = pCmd->pszCmd[0] == 'p' ? _512K : _64K;
2652 uint32_t fFlags = pCmd->pszCmd[0] == 'p' ? DBGF_STEP_F_OVER : DBGF_STEP_F_INTO;
2653 if (pCmd->pszCmd[1] == 'c')
2654 fFlags |= DBGF_STEP_F_STOP_ON_CALL;
2655 else if (pCmd->pszCmd[1] == 't')
2656 fFlags |= DBGF_STEP_F_STOP_ON_RET;
2657 else if (pCmd->pszCmd[0] != 'p')
2658 cMaxSteps = 1;
2659 else
2660 {
2661 /** @todo consider passing RSP + 1 in for 'p' and something else sensible for
2662 * the 'pt' command. */
2663 }
2664
2665 int rc = DBGFR3StepEx(pUVM, pDbgc->idCpu, fFlags, NULL, pStackPop, cbStackPop, cMaxSteps);
2666 if (RT_SUCCESS(rc))
2667 pDbgc->fReady = false;
2668 else
2669 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3StepEx(,,%#x,) failed", fFlags);
2670
2671 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2672 return rc;
2673}
2674
2675
2676/**
2677 * @callback_method_impl{FNDBGCCMD, The 'pa' and 'ta' commands.}
2678 */
2679static DECLCALLBACK(int) dbgcCmdStepTraceTo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2680{
2681 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2682 if (cArgs != 1)
2683 return DBGCCmdHlpFail(pCmdHlp, pCmd,
2684 "Sorry, but the '%s' command only implements a single argument at present.\n", pCmd->pszCmd);
2685 DBGFADDRESS Address;
2686 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
2687 if (RT_FAILURE(rc))
2688 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", &paArgs[0]);
2689
2690 uint32_t cMaxSteps = pCmd->pszCmd[0] == 'p' ? _512K : 1;
2691 uint32_t fFlags = pCmd->pszCmd[0] == 'p' ? DBGF_STEP_F_OVER : DBGF_STEP_F_INTO;
2692 rc = DBGFR3StepEx(pUVM, pDbgc->idCpu, fFlags, &Address, NULL, 0, cMaxSteps);
2693 if (RT_SUCCESS(rc))
2694 pDbgc->fReady = false;
2695 else
2696 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3StepEx(,,%#x,) failed", fFlags);
2697 return rc;
2698}
2699
2700
2701/**
2702 * @callback_method_impl{FNDBGCCMD, The 'k'\, 'kg' and 'kh' commands.}
2703 */
2704static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2705{
2706 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2707
2708 /*
2709 * Figure which context we're called for and start walking that stack.
2710 */
2711 int rc;
2712 PCDBGFSTACKFRAME pFirstFrame;
2713 bool const fGuest = pCmd->pszCmd[1] == 'g'
2714 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
2715 rc = DBGFR3StackWalkBegin(pUVM, pDbgc->idCpu, fGuest ? DBGFCODETYPE_GUEST : DBGFCODETYPE_HYPER, &pFirstFrame);
2716 if (RT_FAILURE(rc))
2717 return DBGCCmdHlpPrintf(pCmdHlp, "Failed to begin stack walk, rc=%Rrc\n", rc);
2718
2719 /*
2720 * Print header.
2721 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
2722 */
2723 uint32_t fBitFlags = 0;
2724 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
2725 pFrame;
2726 pFrame = DBGFR3StackWalkNext(pFrame))
2727 {
2728 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
2729 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
2730 {
2731 if (fCurBitFlags != fBitFlags)
2732 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2733 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2734 pFrame->AddrFrame.Sel,
2735 (uint16_t)pFrame->AddrFrame.off,
2736 pFrame->AddrReturnFrame.Sel,
2737 (uint16_t)pFrame->AddrReturnFrame.off,
2738 (uint32_t)pFrame->AddrReturnPC.Sel,
2739 (uint32_t)pFrame->AddrReturnPC.off,
2740 pFrame->Args.au32[0],
2741 pFrame->Args.au32[1],
2742 pFrame->Args.au32[2],
2743 pFrame->Args.au32[3]);
2744 }
2745 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
2746 {
2747 if (fCurBitFlags != fBitFlags)
2748 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2749 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2750 (uint32_t)pFrame->AddrFrame.off,
2751 (uint32_t)pFrame->AddrReturnFrame.off,
2752 (uint32_t)pFrame->AddrReturnPC.Sel,
2753 (uint32_t)pFrame->AddrReturnPC.off,
2754 pFrame->Args.au32[0],
2755 pFrame->Args.au32[1],
2756 pFrame->Args.au32[2],
2757 pFrame->Args.au32[3]);
2758 }
2759 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
2760 {
2761 if (fCurBitFlags != fBitFlags)
2762 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
2763 rc = DBGCCmdHlpPrintf(pCmdHlp, "%016RX64 %04RX16:%016RX64 %016RX64",
2764 (uint64_t)pFrame->AddrFrame.off,
2765 pFrame->AddrReturnFrame.Sel,
2766 (uint64_t)pFrame->AddrReturnFrame.off,
2767 (uint64_t)pFrame->AddrReturnPC.off);
2768 }
2769 if (RT_FAILURE(rc))
2770 break;
2771 if (!pFrame->pSymPC)
2772 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2773 fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
2774 ? " %RTsel:%016RGv"
2775 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT
2776 ? " %RTsel:%08RGv"
2777 : " %RTsel:%04RGv"
2778 , pFrame->AddrPC.Sel, pFrame->AddrPC.off);
2779 else
2780 {
2781 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
2782 if (offDisp > 0)
2783 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
2784 else if (offDisp < 0)
2785 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
2786 else
2787 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", pFrame->pSymPC->szName);
2788 }
2789 if (RT_SUCCESS(rc) && pFrame->pLinePC)
2790 rc = DBGCCmdHlpPrintf(pCmdHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
2791 if (RT_SUCCESS(rc))
2792 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2793 if (RT_FAILURE(rc))
2794 break;
2795
2796 fBitFlags = fCurBitFlags;
2797 }
2798
2799 DBGFR3StackWalkEnd(pFirstFrame);
2800
2801 NOREF(paArgs); NOREF(cArgs);
2802 return rc;
2803}
2804
2805
2806static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP pCmdHlp, PCX86DESC64 pDesc, unsigned iEntry, bool fHyper, bool *pfDblEntry)
2807{
2808 /* GUEST64 */
2809 int rc;
2810
2811 const char *pszHyper = fHyper ? " HYPER" : "";
2812 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2813 if (pDesc->Gen.u1DescType)
2814 {
2815 static const char * const s_apszTypes[] =
2816 {
2817 "DataRO", /* 0 Read-Only */
2818 "DataRO", /* 1 Read-Only - Accessed */
2819 "DataRW", /* 2 Read/Write */
2820 "DataRW", /* 3 Read/Write - Accessed */
2821 "DownRO", /* 4 Expand-down, Read-Only */
2822 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2823 "DownRW", /* 6 Expand-down, Read/Write */
2824 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2825 "CodeEO", /* 8 Execute-Only */
2826 "CodeEO", /* 9 Execute-Only - Accessed */
2827 "CodeER", /* A Execute/Readable */
2828 "CodeER", /* B Execute/Readable - Accessed */
2829 "ConfE0", /* C Conforming, Execute-Only */
2830 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2831 "ConfER", /* E Conforming, Execute/Readable */
2832 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2833 };
2834 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2835 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2836 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2837 uint32_t u32Base = X86DESC_BASE(pDesc);
2838 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2839
2840 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2841 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2842 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2843 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2844 }
2845 else
2846 {
2847 static const char * const s_apszTypes[] =
2848 {
2849 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
2850 "Ill-1 ", /* 1 0001 Available 16-bit TSS */
2851 "LDT ", /* 2 0010 LDT */
2852 "Ill-3 ", /* 3 0011 Busy 16-bit TSS */
2853 "Ill-4 ", /* 4 0100 16-bit Call Gate */
2854 "Ill-5 ", /* 5 0101 Task Gate */
2855 "Ill-6 ", /* 6 0110 16-bit Interrupt Gate */
2856 "Ill-7 ", /* 7 0111 16-bit Trap Gate */
2857 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
2858 "Tss64A", /* 9 1001 Available 32-bit TSS */
2859 "Ill-A ", /* A 1010 Reserved (Illegal) */
2860 "Tss64B", /* B 1011 Busy 32-bit TSS */
2861 "Call64", /* C 1100 32-bit Call Gate */
2862 "Ill-D ", /* D 1101 Reserved (Illegal) */
2863 "Int64 ", /* E 1110 32-bit Interrupt Gate */
2864 "Trap64" /* F 1111 32-bit Trap Gate */
2865 };
2866 switch (pDesc->Gen.u4Type)
2867 {
2868 /* raw */
2869 case X86_SEL_TYPE_SYS_UNDEFINED:
2870 case X86_SEL_TYPE_SYS_UNDEFINED2:
2871 case X86_SEL_TYPE_SYS_UNDEFINED4:
2872 case X86_SEL_TYPE_SYS_UNDEFINED3:
2873 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2874 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2875 case X86_SEL_TYPE_SYS_286_CALL_GATE:
2876 case X86_SEL_TYPE_SYS_286_INT_GATE:
2877 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2878 case X86_SEL_TYPE_SYS_TASK_GATE:
2879 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
2880 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
2881 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2882 break;
2883
2884 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2885 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2886 case X86_SEL_TYPE_SYS_LDT:
2887 {
2888 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
2889 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2890 const char *pszLong = pDesc->Gen.u1Long ? "LONG" : " ";
2891
2892 uint64_t u64Base = X86DESC64_BASE(pDesc);
2893 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2894
2895 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%016RX64 Lim=%08x DPL=%d %s %s %s %sAVL=%d R=%d%s\n",
2896 iEntry, s_apszTypes[pDesc->Gen.u4Type], u64Base, cbLimit,
2897 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszLong, pszBig,
2898 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
2899 pszHyper);
2900 if (pfDblEntry)
2901 *pfDblEntry = true;
2902 break;
2903 }
2904
2905 case X86_SEL_TYPE_SYS_386_CALL_GATE:
2906 {
2907 unsigned cParams = pDesc->au8[4] & 0x1f;
2908 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
2909 RTSEL sel = pDesc->au16[1];
2910 uint64_t off = pDesc->au16[0]
2911 | ((uint64_t)pDesc->au16[3] << 16)
2912 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
2913 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s %s=%d%s\n",
2914 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2915 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
2916 if (pfDblEntry)
2917 *pfDblEntry = true;
2918 break;
2919 }
2920
2921 case X86_SEL_TYPE_SYS_386_INT_GATE:
2922 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2923 {
2924 RTSEL sel = pDesc->Gate.u16Sel;
2925 uint64_t off = pDesc->Gate.u16OffsetLow
2926 | ((uint64_t)pDesc->Gate.u16OffsetHigh << 16)
2927 | ((uint64_t)pDesc->Gate.u32OffsetTop << 32);
2928 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%u %s IST=%u%s\n",
2929 iEntry, s_apszTypes[pDesc->Gate.u4Type], sel, off,
2930 pDesc->Gate.u2Dpl, pszPresent, pDesc->Gate.u3IST, pszHyper);
2931 if (pfDblEntry)
2932 *pfDblEntry = true;
2933 break;
2934 }
2935
2936 /* impossible, just it's necessary to keep gcc happy. */
2937 default:
2938 return VINF_SUCCESS;
2939 }
2940 }
2941 return VINF_SUCCESS;
2942}
2943
2944
2945/**
2946 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
2947 *
2948 * @returns pfnPrintf status code.
2949 * @param pCmdHlp The DBGC command helpers.
2950 * @param pDesc The descriptor to display.
2951 * @param iEntry The descriptor entry number.
2952 * @param fHyper Whether the selector belongs to the hypervisor or not.
2953 */
2954static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
2955{
2956 int rc;
2957
2958 const char *pszHyper = fHyper ? " HYPER" : "";
2959 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2960 if (pDesc->Gen.u1DescType)
2961 {
2962 static const char * const s_apszTypes[] =
2963 {
2964 "DataRO", /* 0 Read-Only */
2965 "DataRO", /* 1 Read-Only - Accessed */
2966 "DataRW", /* 2 Read/Write */
2967 "DataRW", /* 3 Read/Write - Accessed */
2968 "DownRO", /* 4 Expand-down, Read-Only */
2969 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2970 "DownRW", /* 6 Expand-down, Read/Write */
2971 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2972 "CodeEO", /* 8 Execute-Only */
2973 "CodeEO", /* 9 Execute-Only - Accessed */
2974 "CodeER", /* A Execute/Readable */
2975 "CodeER", /* B Execute/Readable - Accessed */
2976 "ConfE0", /* C Conforming, Execute-Only */
2977 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2978 "ConfER", /* E Conforming, Execute/Readable */
2979 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2980 };
2981 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2982 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2983 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2984 uint32_t u32Base = pDesc->Gen.u16BaseLow
2985 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
2986 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
2987 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
2988 if (pDesc->Gen.u1Granularity)
2989 cbLimit <<= PAGE_SHIFT;
2990
2991 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2992 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2993 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2994 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2995 }
2996 else
2997 {
2998 static const char * const s_apszTypes[] =
2999 {
3000 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
3001 "Tss16A", /* 1 0001 Available 16-bit TSS */
3002 "LDT ", /* 2 0010 LDT */
3003 "Tss16B", /* 3 0011 Busy 16-bit TSS */
3004 "Call16", /* 4 0100 16-bit Call Gate */
3005 "TaskG ", /* 5 0101 Task Gate */
3006 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
3007 "Trap16", /* 7 0111 16-bit Trap Gate */
3008 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
3009 "Tss32A", /* 9 1001 Available 32-bit TSS */
3010 "Ill-A ", /* A 1010 Reserved (Illegal) */
3011 "Tss32B", /* B 1011 Busy 32-bit TSS */
3012 "Call32", /* C 1100 32-bit Call Gate */
3013 "Ill-D ", /* D 1101 Reserved (Illegal) */
3014 "Int32 ", /* E 1110 32-bit Interrupt Gate */
3015 "Trap32" /* F 1111 32-bit Trap Gate */
3016 };
3017 switch (pDesc->Gen.u4Type)
3018 {
3019 /* raw */
3020 case X86_SEL_TYPE_SYS_UNDEFINED:
3021 case X86_SEL_TYPE_SYS_UNDEFINED2:
3022 case X86_SEL_TYPE_SYS_UNDEFINED4:
3023 case X86_SEL_TYPE_SYS_UNDEFINED3:
3024 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
3025 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
3026 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
3027 break;
3028
3029 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
3030 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
3031 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
3032 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
3033 case X86_SEL_TYPE_SYS_LDT:
3034 {
3035 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
3036 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
3037 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
3038 uint32_t u32Base = pDesc->Gen.u16BaseLow
3039 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
3040 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
3041 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
3042 if (pDesc->Gen.u1Granularity)
3043 cbLimit <<= PAGE_SHIFT;
3044
3045 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
3046 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
3047 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
3048 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
3049 pszHyper);
3050 break;
3051 }
3052
3053 case X86_SEL_TYPE_SYS_TASK_GATE:
3054 {
3055 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s TSS=%04x DPL=%d %s%s\n",
3056 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
3057 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
3058 break;
3059 }
3060
3061 case X86_SEL_TYPE_SYS_286_CALL_GATE:
3062 case X86_SEL_TYPE_SYS_386_CALL_GATE:
3063 {
3064 unsigned cParams = pDesc->au8[4] & 0x1f;
3065 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
3066 RTSEL sel = pDesc->au16[1];
3067 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
3068 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
3069 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
3070 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
3071 break;
3072 }
3073
3074 case X86_SEL_TYPE_SYS_286_INT_GATE:
3075 case X86_SEL_TYPE_SYS_386_INT_GATE:
3076 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
3077 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
3078 {
3079 RTSEL sel = pDesc->au16[1];
3080 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
3081 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
3082 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
3083 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
3084 break;
3085 }
3086
3087 /* impossible, just it's necessary to keep gcc happy. */
3088 default:
3089 return VINF_SUCCESS;
3090 }
3091 }
3092 return rc;
3093}
3094
3095
3096/**
3097 * @callback_method_impl{FNDBGCCMD, The 'dg'\, 'dga'\, 'dl' and 'dla' commands.}
3098 */
3099static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3100{
3101 /*
3102 * Validate input.
3103 */
3104 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3105
3106 /*
3107 * Get the CPU mode, check which command variation this is
3108 * and fix a default parameter if needed.
3109 */
3110 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3111 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
3112 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
3113 bool fGdt = pCmd->pszCmd[1] == 'g';
3114 bool fAll = pCmd->pszCmd[2] == 'a';
3115 RTSEL SelTable = fGdt ? 0 : X86_SEL_LDT;
3116
3117 DBGCVAR Var;
3118 if (!cArgs)
3119 {
3120 cArgs = 1;
3121 paArgs = &Var;
3122 Var.enmType = DBGCVAR_TYPE_NUMBER;
3123 Var.u.u64Number = 0;
3124 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
3125 Var.u64Range = 1024;
3126 }
3127
3128 /*
3129 * Process the arguments.
3130 */
3131 for (unsigned i = 0; i < cArgs; i++)
3132 {
3133 /*
3134 * Retrieve the selector value from the argument.
3135 * The parser may confuse pointers and numbers if more than one
3136 * argument is given, that that into account.
3137 */
3138 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[i].enmType));
3139 uint64_t u64;
3140 unsigned cSels = 1;
3141 switch (paArgs[i].enmType)
3142 {
3143 case DBGCVAR_TYPE_NUMBER:
3144 u64 = paArgs[i].u.u64Number;
3145 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
3146 cSels = RT_MIN(paArgs[i].u64Range, 1024);
3147 break;
3148 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
3149 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
3150 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
3151 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
3152 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
3153 default: u64 = _64K; break;
3154 }
3155 if (u64 < _64K)
3156 {
3157 unsigned Sel = (RTSEL)u64;
3158
3159 /*
3160 * Dump the specified range.
3161 */
3162 bool fSingle = cSels == 1;
3163 while ( cSels-- > 0
3164 && Sel < _64K)
3165 {
3166 DBGFSELINFO SelInfo;
3167 int rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, Sel | SelTable, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
3168 if (RT_SUCCESS(rc))
3169 {
3170 if (SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE)
3171 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x RealM Bas=%04x Lim=%04x\n",
3172 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
3173 else if ( fAll
3174 || fSingle
3175 || SelInfo.u.Raw.Gen.u1Present)
3176 {
3177 if (enmMode == CPUMMODE_PROTECTED)
3178 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &SelInfo.u.Raw, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER));
3179 else
3180 {
3181 bool fDblSkip = false;
3182 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &SelInfo.u.Raw64, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), &fDblSkip);
3183 if (fDblSkip)
3184 Sel += 4;
3185 }
3186 }
3187 }
3188 else
3189 {
3190 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %Rrc\n", Sel, rc);
3191 if (!fAll)
3192 return rc;
3193 }
3194 if (RT_FAILURE(rc))
3195 return rc;
3196
3197 /* next */
3198 Sel += 8;
3199 }
3200 }
3201 else
3202 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds\n", u64);
3203 }
3204
3205 return VINF_SUCCESS;
3206}
3207
3208
3209/**
3210 * @callback_method_impl{FNDBGCCMD, The 'di' and 'dia' commands.}
3211 */
3212static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3213{
3214 /*
3215 * Validate input.
3216 */
3217 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3218
3219 /*
3220 * Establish some stuff like the current IDTR and CPU mode,
3221 * and fix a default parameter.
3222 */
3223 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3224 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
3225 uint16_t cbLimit;
3226 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVCpu, &cbLimit);
3227 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
3228 unsigned cbEntry;
3229 switch (enmMode)
3230 {
3231 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
3232 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
3233 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
3234 default:
3235 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid CPU mode %d.\n", enmMode);
3236 }
3237
3238 bool fAll = pCmd->pszCmd[2] == 'a';
3239 DBGCVAR Var;
3240 if (!cArgs)
3241 {
3242 cArgs = 1;
3243 paArgs = &Var;
3244 Var.enmType = DBGCVAR_TYPE_NUMBER;
3245 Var.u.u64Number = 0;
3246 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
3247 Var.u64Range = 256;
3248 }
3249
3250 /*
3251 * Process the arguments.
3252 */
3253 for (unsigned i = 0; i < cArgs; i++)
3254 {
3255 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER);
3256 if (paArgs[i].u.u64Number < 256)
3257 {
3258 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
3259 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
3260 ? paArgs[i].u64Range
3261 : 1;
3262 bool fSingle = cInts == 1;
3263 while ( cInts-- > 0
3264 && iInt < 256)
3265 {
3266 /*
3267 * Try read it.
3268 */
3269 union
3270 {
3271 RTFAR16 Real;
3272 X86DESC Prot;
3273 X86DESC64 Long;
3274 } u;
3275 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
3276 {
3277 DBGCCmdHlpPrintf(pCmdHlp, "%04x not within the IDT\n", (unsigned)iInt);
3278 if (!fAll && !fSingle)
3279 return VINF_SUCCESS;
3280 }
3281 DBGCVAR AddrVar;
3282 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
3283 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
3284 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
3285 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &u, cbEntry, &AddrVar, NULL);
3286 if (RT_FAILURE(rc))
3287 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
3288
3289 /*
3290 * Display it.
3291 */
3292 switch (enmMode)
3293 {
3294 case CPUMMODE_REAL:
3295 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
3296 /** @todo resolve 16:16 IDTE to a symbol */
3297 break;
3298 case CPUMMODE_PROTECTED:
3299 if (fAll || fSingle || u.Prot.Gen.u1Present)
3300 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
3301 break;
3302 case CPUMMODE_LONG:
3303 if (fAll || fSingle || u.Long.Gen.u1Present)
3304 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
3305 break;
3306 default: break; /* to shut up gcc */
3307 }
3308 if (RT_FAILURE(rc))
3309 return rc;
3310
3311 /* next */
3312 iInt++;
3313 }
3314 }
3315 else
3316 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
3317 }
3318
3319 return VINF_SUCCESS;
3320}
3321
3322
3323/**
3324 * @callback_method_impl{FNDBGCCMD,
3325 * The 'da'\, 'dq'\, 'dqs'\, 'dd'\, 'dds'\, 'dw'\, 'db'\, 'dp'\, 'dps'\,
3326 * and 'du' commands.}
3327 */
3328static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3329{
3330 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3331
3332 /*
3333 * Validate input.
3334 */
3335 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3336 if (cArgs == 1)
3337 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3338 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3339
3340#define DBGC_DUMP_MEM_F_ASCII RT_BIT_32(31)
3341#define DBGC_DUMP_MEM_F_UNICODE RT_BIT_32(30)
3342#define DBGC_DUMP_MEM_F_FAR RT_BIT_32(29)
3343#define DBGC_DUMP_MEM_F_SYMBOLS RT_BIT_32(28)
3344#define DBGC_DUMP_MEM_F_SIZE UINT32_C(0x0000ffff)
3345
3346 /*
3347 * Figure out the element size.
3348 */
3349 unsigned cbElement;
3350 bool fAscii = false;
3351 bool fUnicode = false;
3352 bool fFar = false;
3353 bool fSymbols = pCmd->pszCmd[1] && pCmd->pszCmd[2] == 's';
3354 switch (pCmd->pszCmd[1])
3355 {
3356 default:
3357 case 'b': cbElement = 1; break;
3358 case 'w': cbElement = 2; break;
3359 case 'd': cbElement = 4; break;
3360 case 'q': cbElement = 8; break;
3361 case 'a':
3362 cbElement = 1;
3363 fAscii = true;
3364 break;
3365 case 'F':
3366 cbElement = 4;
3367 fFar = true;
3368 break;
3369 case 'p':
3370 cbElement = DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu) ? 8 : 4;
3371 break;
3372 case 'u':
3373 cbElement = 2;
3374 fUnicode = true;
3375 break;
3376 case '\0':
3377 fAscii = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_ASCII);
3378 fSymbols = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_SYMBOLS);
3379 fUnicode = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_UNICODE);
3380 fFar = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_FAR);
3381 cbElement = pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_SIZE;
3382 if (!cbElement)
3383 cbElement = 1;
3384 break;
3385 }
3386 uint32_t const cbDumpElement = cbElement
3387 | (fSymbols ? DBGC_DUMP_MEM_F_SYMBOLS : 0)
3388 | (fFar ? DBGC_DUMP_MEM_F_FAR : 0)
3389 | (fUnicode ? DBGC_DUMP_MEM_F_UNICODE : 0)
3390 | (fAscii ? DBGC_DUMP_MEM_F_ASCII : 0);
3391 pDbgc->cbDumpElement = cbDumpElement;
3392
3393 /*
3394 * Find address.
3395 */
3396 if (!cArgs)
3397 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
3398 else
3399 pDbgc->DumpPos = paArgs[0];
3400
3401 /*
3402 * Range.
3403 */
3404 switch (pDbgc->DumpPos.enmRangeType)
3405 {
3406 case DBGCVAR_RANGE_NONE:
3407 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3408 pDbgc->DumpPos.u64Range = 0x60;
3409 break;
3410
3411 case DBGCVAR_RANGE_ELEMENTS:
3412 if (pDbgc->DumpPos.u64Range > 2048)
3413 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many elements requested. Max is 2048 elements.\n");
3414 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3415 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
3416 break;
3417
3418 case DBGCVAR_RANGE_BYTES:
3419 if (pDbgc->DumpPos.u64Range > 65536)
3420 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
3421 break;
3422
3423 default:
3424 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
3425 }
3426
3427 pDbgc->pLastPos = &pDbgc->DumpPos;
3428
3429 /*
3430 * Do the dumping.
3431 */
3432 int cbLeft = (int)pDbgc->DumpPos.u64Range;
3433 uint8_t u16Prev = '\0';
3434 for (;;)
3435 {
3436 /*
3437 * Read memory.
3438 */
3439 char achBuffer[16];
3440 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
3441 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
3442 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
3443 if (RT_FAILURE(rc))
3444 {
3445 if (u16Prev && u16Prev != '\n')
3446 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3447 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
3448 }
3449
3450 /*
3451 * Display it.
3452 */
3453 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
3454 if (!fAscii && !fUnicode)
3455 {
3456 DBGCCmdHlpPrintf(pCmdHlp, "%DV:", &pDbgc->DumpPos);
3457 unsigned i;
3458 for (i = 0; i < cb; i += cbElement)
3459 {
3460 const char *pszSpace = " ";
3461 if (cbElement <= 2 && i == 8)
3462 pszSpace = "-";
3463 switch (cbElement)
3464 {
3465 case 1:
3466 DBGCCmdHlpPrintf(pCmdHlp, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]);
3467 break;
3468 case 2:
3469 DBGCCmdHlpPrintf(pCmdHlp, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]);
3470 break;
3471 case 4:
3472 if (!fFar)
3473 DBGCCmdHlpPrintf(pCmdHlp, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]);
3474 else
3475 DBGCCmdHlpPrintf(pCmdHlp, "%s%04x:%04x:",
3476 pszSpace, *(uint16_t *)&achBuffer[i + 2], *(uint16_t *)&achBuffer[i]);
3477 break;
3478 case 8:
3479 DBGCCmdHlpPrintf(pCmdHlp, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]);
3480 break;
3481 }
3482
3483 if (fSymbols)
3484 {
3485 /* Try lookup symbol for the above address. */
3486 DBGFADDRESS Addr;
3487 rc = VINF_SUCCESS;
3488 if (cbElement == 8)
3489 DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, *(uint64_t *)&achBuffer[i]);
3490 else if (!fFar)
3491 DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, *(uint32_t *)&achBuffer[i]);
3492 else
3493 rc = DBGFR3AddrFromSelOff(pDbgc->pUVM, pDbgc->idCpu, &Addr,
3494 *(uint16_t *)&achBuffer[i + 2], *(uint16_t *)&achBuffer[i]);
3495 if (RT_SUCCESS(rc))
3496 {
3497 RTINTPTR offDisp;
3498 RTDBGSYMBOL Symbol;
3499 rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, &Addr, RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL,
3500 &offDisp, &Symbol, NULL);
3501 if (RT_SUCCESS(rc))
3502 {
3503 if (!offDisp)
3504 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", Symbol.szName);
3505 else if (offDisp > 0)
3506 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s + %RGv", Symbol.szName, offDisp);
3507 else
3508 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s - %RGv", Symbol.szName, -offDisp);
3509 if (Symbol.cb > 0)
3510 rc = DBGCCmdHlpPrintf(pCmdHlp, " (LB %RGv)", Symbol.cb);
3511 }
3512 }
3513
3514 /* Next line prefix. */
3515 unsigned iNext = i + cbElement;
3516 if (iNext < cb)
3517 {
3518 DBGCVAR TmpPos = pDbgc->DumpPos;
3519 DBGCCmdHlpEval(pCmdHlp, &TmpPos, "(%Dv) + %x", &pDbgc->DumpPos, iNext);
3520 DBGCCmdHlpPrintf(pCmdHlp, "\n%DV:", &pDbgc->DumpPos);
3521 }
3522 }
3523 }
3524
3525 /* Chars column. */
3526 if (cbElement == 1)
3527 {
3528 while (i++ < sizeof(achBuffer))
3529 DBGCCmdHlpPrintf(pCmdHlp, " ");
3530 DBGCCmdHlpPrintf(pCmdHlp, " ");
3531 for (i = 0; i < cb; i += cbElement)
3532 {
3533 uint8_t u8 = *(uint8_t *)&achBuffer[i];
3534 if (RT_C_IS_PRINT(u8) && u8 < 127 && u8 >= 32)
3535 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
3536 else
3537 DBGCCmdHlpPrintf(pCmdHlp, ".");
3538 }
3539 }
3540 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3541 }
3542 else
3543 {
3544 /*
3545 * We print up to the first zero and stop there.
3546 * Only printables + '\t' and '\n' are printed.
3547 */
3548 if (!u16Prev)
3549 DBGCCmdHlpPrintf(pCmdHlp, "%DV:\n", &pDbgc->DumpPos);
3550 uint16_t u16 = '\0';
3551 unsigned i;
3552 for (i = 0; i < cb; i += cbElement)
3553 {
3554 u16Prev = u16;
3555 if (cbElement == 1)
3556 u16 = *(uint8_t *)&achBuffer[i];
3557 else
3558 u16 = *(uint16_t *)&achBuffer[i];
3559 if ( u16 < 127
3560 && ( (RT_C_IS_PRINT(u16) && u16 >= 32)
3561 || u16 == '\t'
3562 || u16 == '\n'))
3563 DBGCCmdHlpPrintf(pCmdHlp, "%c", (int)u16);
3564 else if (!u16)
3565 break;
3566 else
3567 DBGCCmdHlpPrintf(pCmdHlp, "\\x%0*x", cbElement * 2, u16);
3568 }
3569 if (u16 == '\0')
3570 cb = cbLeft = i + 1;
3571 if (cbLeft - cb <= 0 && u16Prev != '\n')
3572 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3573 }
3574
3575 /*
3576 * Advance
3577 */
3578 cbLeft -= (int)cb;
3579 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
3580 if (RT_FAILURE(rc))
3581 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
3582 if (cbLeft <= 0)
3583 break;
3584 }
3585
3586 NOREF(pCmd);
3587 return VINF_SUCCESS;
3588}
3589
3590
3591/**
3592 * Best guess at which paging mode currently applies to the guest
3593 * paging structures.
3594 *
3595 * This have to come up with a decent answer even when the guest
3596 * is in non-paged protected mode or real mode.
3597 *
3598 * @returns cr3.
3599 * @param pDbgc The DBGC instance.
3600 * @param pfPAE Where to store the page address extension indicator.
3601 * @param pfLME Where to store the long mode enabled indicator.
3602 * @param pfPSE Where to store the page size extension indicator.
3603 * @param pfPGE Where to store the page global enabled indicator.
3604 * @param pfNXE Where to store the no-execution enabled indicator.
3605 */
3606static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3607{
3608 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3609 RTGCUINTREG cr4 = CPUMGetGuestCR4(pVCpu);
3610 *pfPSE = !!(cr4 & X86_CR4_PSE);
3611 *pfPGE = !!(cr4 & X86_CR4_PGE);
3612 if (cr4 & X86_CR4_PAE)
3613 {
3614 *pfPSE = true;
3615 *pfPAE = true;
3616 }
3617 else
3618 *pfPAE = false;
3619
3620 *pfLME = CPUMGetGuestMode(pVCpu) == CPUMMODE_LONG;
3621 *pfNXE = false; /* GUEST64 GUESTNX */
3622 return CPUMGetGuestCR3(pVCpu);
3623}
3624
3625
3626/**
3627 * Determine the shadow paging mode.
3628 *
3629 * @returns cr3.
3630 * @param pDbgc The DBGC instance.
3631 * @param pfPAE Where to store the page address extension indicator.
3632 * @param pfLME Where to store the long mode enabled indicator.
3633 * @param pfPSE Where to store the page size extension indicator.
3634 * @param pfPGE Where to store the page global enabled indicator.
3635 * @param pfNXE Where to store the no-execution enabled indicator.
3636 */
3637static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3638{
3639 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3640
3641 *pfPSE = true;
3642 *pfPGE = false;
3643 switch (PGMGetShadowMode(pVCpu))
3644 {
3645 default:
3646 case PGMMODE_32_BIT:
3647 *pfPAE = *pfLME = *pfNXE = false;
3648 break;
3649 case PGMMODE_PAE:
3650 *pfLME = *pfNXE = false;
3651 *pfPAE = true;
3652 break;
3653 case PGMMODE_PAE_NX:
3654 *pfLME = false;
3655 *pfPAE = *pfNXE = true;
3656 break;
3657 case PGMMODE_AMD64:
3658 *pfNXE = false;
3659 *pfPAE = *pfLME = true;
3660 break;
3661 case PGMMODE_AMD64_NX:
3662 *pfPAE = *pfLME = *pfNXE = true;
3663 break;
3664 }
3665 return PGMGetHyperCR3(pVCpu);
3666}
3667
3668
3669/**
3670 * @callback_method_impl{FNDBGCCMD,
3671 * The 'dpd'\, 'dpda'\, 'dpdb'\, 'dpdg' and 'dpdh' commands.}
3672 */
3673static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3674{
3675 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3676
3677 /*
3678 * Validate input.
3679 */
3680 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3681 if (cArgs == 1 && pCmd->pszCmd[3] == 'a')
3682 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3683 if (cArgs == 1 && pCmd->pszCmd[3] != 'a')
3684 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3685 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
3686 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3687
3688 /*
3689 * Guest or shadow page directories? Get the paging parameters.
3690 */
3691 bool fGuest = pCmd->pszCmd[3] != 'h';
3692 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
3693 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3694 ? pDbgc->fRegCtxGuest
3695 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
3696
3697 bool fPAE, fLME, fPSE, fPGE, fNXE;
3698 uint64_t cr3 = fGuest
3699 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
3700 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
3701 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
3702
3703 /*
3704 * Setup default argument if none was specified.
3705 * Fix address / index confusion.
3706 */
3707 DBGCVAR VarDefault;
3708 if (!cArgs)
3709 {
3710 if (pCmd->pszCmd[3] == 'a')
3711 {
3712 if (fLME || fPAE)
3713 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");
3714 if (fGuest)
3715 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
3716 else
3717 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
3718 }
3719 else
3720 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
3721 paArgs = &VarDefault;
3722 cArgs = 1;
3723 }
3724 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3725 {
3726 /* If it's a number (not an address), it's an index, so convert it to an address. */
3727 Assert(pCmd->pszCmd[3] != 'a');
3728 VarDefault = paArgs[0];
3729 if (fPAE)
3730 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
3731 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
3732 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
3733 VarDefault.u.u64Number <<= X86_PD_SHIFT;
3734 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
3735 paArgs = &VarDefault;
3736 }
3737
3738 /*
3739 * Locate the PDE to start displaying at.
3740 *
3741 * The 'dpda' command takes the address of a PDE, while the others are guest
3742 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
3743 * while the others require us to do all the tedious walking thru the paging
3744 * hierarchy to find the intended PDE.
3745 */
3746 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
3747 DBGCVAR VarGCPtr = { NULL, }; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
3748 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
3749 unsigned cEntries; /* The number of entries to display. */
3750 unsigned cEntriesMax; /* The max number of entries to display. */
3751 int rc;
3752 if (pCmd->pszCmd[3] == 'a')
3753 {
3754 VarPDEAddr = paArgs[0];
3755 switch (VarPDEAddr.enmRangeType)
3756 {
3757 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
3758 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
3759 default: cEntries = 10; break;
3760 }
3761 cEntriesMax = PAGE_SIZE / cbEntry;
3762 }
3763 else
3764 {
3765 /*
3766 * Determine the range.
3767 */
3768 switch (paArgs[0].enmRangeType)
3769 {
3770 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3771 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3772 default: cEntries = 10; break;
3773 }
3774
3775 /*
3776 * Normalize the input address, it must be a flat GC address.
3777 */
3778 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3779 if (RT_FAILURE(rc))
3780 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3781 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3782 {
3783 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3784 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3785 }
3786 if (fPAE)
3787 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
3788 else
3789 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
3790
3791 /*
3792 * Do the paging walk until we get to the page directory.
3793 */
3794 DBGCVAR VarCur;
3795 if (fGuest)
3796 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3797 else
3798 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3799 if (fLME)
3800 {
3801 /* Page Map Level 4 Lookup. */
3802 /* Check if it's a valid address first? */
3803 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3804 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3805 X86PML4E Pml4e;
3806 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3807 if (RT_FAILURE(rc))
3808 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3809 if (!Pml4e.n.u1Present)
3810 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3811
3812 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3813 Assert(fPAE);
3814 }
3815 if (fPAE)
3816 {
3817 /* Page directory pointer table. */
3818 X86PDPE Pdpe;
3819 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3820 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3821 if (RT_FAILURE(rc))
3822 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3823 if (!Pdpe.n.u1Present)
3824 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3825
3826 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3827 VarPDEAddr = VarCur;
3828 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3829 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
3830 }
3831 else
3832 {
3833 /* 32-bit legacy - CR3 == page directory. */
3834 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
3835 VarPDEAddr = VarCur;
3836 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
3837 }
3838 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3839 }
3840
3841 /* adjust cEntries */
3842 cEntries = RT_MAX(1, cEntries);
3843 cEntries = RT_MIN(cEntries, cEntriesMax);
3844
3845 /*
3846 * The display loop.
3847 */
3848 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
3849 &VarPDEAddr, iEntry);
3850 do
3851 {
3852 /*
3853 * Read.
3854 */
3855 X86PDEPAE Pde;
3856 Pde.u = 0;
3857 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, cbEntry, &VarPDEAddr, NULL);
3858 if (RT_FAILURE(rc))
3859 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
3860
3861 /*
3862 * Display.
3863 */
3864 if (iEntry != ~0U)
3865 {
3866 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3867 iEntry++;
3868 }
3869 if (fPSE && Pde.b.u1Size)
3870 DBGCCmdHlpPrintf(pCmdHlp,
3871 fPAE
3872 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3873 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3874 Pde.u,
3875 Pde.u & X86_PDE_PAE_PG_MASK,
3876 Pde.b.u1Present ? "p " : "np",
3877 Pde.b.u1Write ? "w" : "r",
3878 Pde.b.u1User ? "u" : "s",
3879 Pde.b.u1Accessed ? "a " : "na",
3880 Pde.b.u1Dirty ? "d " : "nd",
3881 Pde.b.u3Available,
3882 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
3883 Pde.b.u1WriteThru ? "pwt" : " ",
3884 Pde.b.u1CacheDisable ? "pcd" : " ",
3885 Pde.b.u1PAT ? "pat" : "",
3886 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
3887 else
3888 DBGCCmdHlpPrintf(pCmdHlp,
3889 fPAE
3890 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
3891 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
3892 Pde.u,
3893 Pde.u & X86_PDE_PAE_PG_MASK,
3894 Pde.n.u1Present ? "p " : "np",
3895 Pde.n.u1Write ? "w" : "r",
3896 Pde.n.u1User ? "u" : "s",
3897 Pde.n.u1Accessed ? "a " : "na",
3898 Pde.u & RT_BIT(6) ? "6 " : " ",
3899 Pde.n.u3Available,
3900 Pde.u & RT_BIT(8) ? "8" : " ",
3901 Pde.n.u1WriteThru ? "pwt" : " ",
3902 Pde.n.u1CacheDisable ? "pcd" : " ",
3903 Pde.u & RT_BIT(7) ? "7" : "",
3904 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
3905 if (Pde.u & UINT64_C(0x7fff000000000000))
3906 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
3907 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3908 if (RT_FAILURE(rc))
3909 return rc;
3910
3911 /*
3912 * Advance.
3913 */
3914 VarPDEAddr.u.u64Number += cbEntry;
3915 if (iEntry != ~0U)
3916 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
3917 } while (cEntries-- > 0);
3918
3919 return VINF_SUCCESS;
3920}
3921
3922
3923/**
3924 * @callback_method_impl{FNDBGCCMD, The 'dpdb' command.}
3925 */
3926static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3927{
3928 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3929 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
3930 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
3931 if (RT_FAILURE(rc1))
3932 return rc1;
3933 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
3934 return rc2;
3935}
3936
3937
3938/**
3939 * @callback_method_impl{FNDBGCCMD, The 'dph*' commands and main part of 'm'.}
3940 */
3941static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3942{
3943 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3944 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3945
3946 /*
3947 * Figure the context and base flags.
3948 */
3949 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;
3950 if (pCmd->pszCmd[0] == 'm')
3951 fFlags |= DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW;
3952 else if (pCmd->pszCmd[3] == '\0')
3953 fFlags |= pDbgc->fRegCtxGuest ? DBGFPGDMP_FLAGS_GUEST : DBGFPGDMP_FLAGS_SHADOW;
3954 else if (pCmd->pszCmd[3] == 'g')
3955 fFlags |= DBGFPGDMP_FLAGS_GUEST;
3956 else if (pCmd->pszCmd[3] == 'h')
3957 fFlags |= DBGFPGDMP_FLAGS_SHADOW;
3958 else
3959 AssertFailed();
3960
3961 if (pDbgc->cPagingHierarchyDumps == 0)
3962 fFlags |= DBGFPGDMP_FLAGS_HEADER;
3963 pDbgc->cPagingHierarchyDumps = (pDbgc->cPagingHierarchyDumps + 1) % 42;
3964
3965 /*
3966 * Get the range.
3967 */
3968 PCDBGCVAR pRange = cArgs > 0 ? &paArgs[0] : pDbgc->pLastPos;
3969 RTGCPTR GCPtrFirst = NIL_RTGCPTR;
3970 int rc = DBGCCmdHlpVarToFlatAddr(pCmdHlp, pRange, &GCPtrFirst);
3971 if (RT_FAILURE(rc))
3972 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to convert %DV to a flat address: %Rrc", pRange, rc);
3973
3974 uint64_t cbRange;
3975 rc = DBGCCmdHlpVarGetRange(pCmdHlp, pRange, PAGE_SIZE, PAGE_SIZE * 8, &cbRange);
3976 if (RT_FAILURE(rc))
3977 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to obtain the range of %DV: %Rrc", pRange, rc);
3978
3979 RTGCPTR GCPtrLast = RTGCPTR_MAX - GCPtrFirst;
3980 if (cbRange >= GCPtrLast)
3981 GCPtrLast = RTGCPTR_MAX;
3982 else if (!cbRange)
3983 GCPtrLast = GCPtrFirst;
3984 else
3985 GCPtrLast = GCPtrFirst + cbRange - 1;
3986
3987 /*
3988 * Do we have a CR3?
3989 */
3990 uint64_t cr3 = 0;
3991 if (cArgs > 1)
3992 {
3993 if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
3994 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No CR3 or mode arguments when dumping both context, please.");
3995 if (paArgs[1].enmType != DBGCVAR_TYPE_NUMBER)
3996 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The CR3 argument is not a number: %DV", &paArgs[1]);
3997 cr3 = paArgs[1].u.u64Number;
3998 }
3999 else
4000 fFlags |= DBGFPGDMP_FLAGS_CURRENT_CR3;
4001
4002 /*
4003 * Do we have a mode?
4004 */
4005 if (cArgs > 2)
4006 {
4007 if (paArgs[2].enmType != DBGCVAR_TYPE_STRING)
4008 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The mode argument is not a string: %DV", &paArgs[2]);
4009 static const struct MODETOFLAGS
4010 {
4011 const char *pszName;
4012 uint32_t fFlags;
4013 } s_aModeToFlags[] =
4014 {
4015 { "ept", DBGFPGDMP_FLAGS_EPT },
4016 { "legacy", 0 },
4017 { "legacy-np", DBGFPGDMP_FLAGS_NP },
4018 { "pse", DBGFPGDMP_FLAGS_PSE },
4019 { "pse-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NP },
4020 { "pae", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE },
4021 { "pae-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NP },
4022 { "pae-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE },
4023 { "pae-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP },
4024 { "long", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME },
4025 { "long-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NP },
4026 { "long-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE },
4027 { "long-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP }
4028 };
4029 int i = RT_ELEMENTS(s_aModeToFlags);
4030 while (i-- > 0)
4031 if (!strcmp(s_aModeToFlags[i].pszName, paArgs[2].u.pszString))
4032 {
4033 fFlags |= s_aModeToFlags[i].fFlags;
4034 break;
4035 }
4036 if (i < 0)
4037 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown mode: \"%s\"", paArgs[2].u.pszString);
4038 }
4039 else
4040 fFlags |= DBGFPGDMP_FLAGS_CURRENT_MODE;
4041
4042 /*
4043 * Call the worker.
4044 */
4045 rc = DBGFR3PagingDumpEx(pUVM, pDbgc->idCpu, fFlags, cr3, GCPtrFirst, GCPtrLast, 99 /*cMaxDepth*/,
4046 DBGCCmdHlpGetDbgfOutputHlp(pCmdHlp));
4047 if (RT_FAILURE(rc))
4048 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3PagingDumpEx: %Rrc\n", rc);
4049 return VINF_SUCCESS;
4050}
4051
4052
4053
4054/**
4055 * @callback_method_impl{FNDBGCCMD, The 'dpg*' commands.}
4056 */
4057static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4058{
4059 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4060
4061 /*
4062 * Validate input.
4063 */
4064 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1);
4065 if (pCmd->pszCmd[3] == 'a')
4066 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
4067 else
4068 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
4069 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
4070 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4071
4072 /*
4073 * Guest or shadow page tables? Get the paging parameters.
4074 */
4075 bool fGuest = pCmd->pszCmd[3] != 'h';
4076 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
4077 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
4078 ? pDbgc->fRegCtxGuest
4079 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
4080
4081 bool fPAE, fLME, fPSE, fPGE, fNXE;
4082 uint64_t cr3 = fGuest
4083 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
4084 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
4085 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
4086
4087 /*
4088 * Locate the PTE to start displaying at.
4089 *
4090 * The 'dpta' command takes the address of a PTE, while the others are guest
4091 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
4092 * while the others require us to do all the tedious walking thru the paging
4093 * hierarchy to find the intended PTE.
4094 */
4095 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
4096 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
4097 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
4098 unsigned cEntries; /* The number of entries to display. */
4099 unsigned cEntriesMax; /* The max number of entries to display. */
4100 int rc;
4101 if (pCmd->pszCmd[3] == 'a')
4102 {
4103 VarPTEAddr = paArgs[0];
4104 switch (VarPTEAddr.enmRangeType)
4105 {
4106 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
4107 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
4108 default: cEntries = 10; break;
4109 }
4110 cEntriesMax = PAGE_SIZE / cbEntry;
4111 }
4112 else
4113 {
4114 /*
4115 * Determine the range.
4116 */
4117 switch (paArgs[0].enmRangeType)
4118 {
4119 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
4120 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
4121 default: cEntries = 10; break;
4122 }
4123
4124 /*
4125 * Normalize the input address, it must be a flat GC address.
4126 */
4127 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
4128 if (RT_FAILURE(rc))
4129 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
4130 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
4131 {
4132 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
4133 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
4134 }
4135 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
4136
4137 /*
4138 * Do the paging walk until we get to the page table.
4139 */
4140 DBGCVAR VarCur;
4141 if (fGuest)
4142 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
4143 else
4144 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
4145 if (fLME)
4146 {
4147 /* Page Map Level 4 Lookup. */
4148 /* Check if it's a valid address first? */
4149 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
4150 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
4151 X86PML4E Pml4e;
4152 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
4153 if (RT_FAILURE(rc))
4154 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
4155 if (!Pml4e.n.u1Present)
4156 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
4157
4158 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
4159 Assert(fPAE);
4160 }
4161 if (fPAE)
4162 {
4163 /* Page directory pointer table. */
4164 X86PDPE Pdpe;
4165 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
4166 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
4167 if (RT_FAILURE(rc))
4168 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
4169 if (!Pdpe.n.u1Present)
4170 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
4171
4172 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
4173
4174 /* Page directory (PAE). */
4175 X86PDEPAE Pde;
4176 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
4177 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
4178 if (RT_FAILURE(rc))
4179 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
4180 if (!Pde.n.u1Present)
4181 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
4182 if (fPSE && Pde.n.u1Size)
4183 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
4184
4185 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
4186 VarPTEAddr = VarCur;
4187 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
4188 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
4189 }
4190 else
4191 {
4192 /* Page directory (legacy). */
4193 X86PDE Pde;
4194 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
4195 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
4196 if (RT_FAILURE(rc))
4197 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
4198 if (!Pde.n.u1Present)
4199 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
4200 if (fPSE && Pde.n.u1Size)
4201 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
4202
4203 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
4204 VarPTEAddr = VarCur;
4205 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
4206 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
4207 }
4208 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
4209 }
4210
4211 /* adjust cEntries */
4212 cEntries = RT_MAX(1, cEntries);
4213 cEntries = RT_MIN(cEntries, cEntriesMax);
4214
4215 /*
4216 * The display loop.
4217 */
4218 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
4219 &VarPTEAddr, &VarGCPtr, iEntry);
4220 do
4221 {
4222 /*
4223 * Read.
4224 */
4225 X86PTEPAE Pte;
4226 Pte.u = 0;
4227 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pte, cbEntry, &VarPTEAddr, NULL);
4228 if (RT_FAILURE(rc))
4229 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
4230
4231 /*
4232 * Display.
4233 */
4234 if (iEntry != ~0U)
4235 {
4236 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
4237 iEntry++;
4238 }
4239 DBGCCmdHlpPrintf(pCmdHlp,
4240 fPAE
4241 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
4242 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
4243 Pte.u,
4244 Pte.u & X86_PTE_PAE_PG_MASK,
4245 Pte.n.u1Present ? "p " : "np",
4246 Pte.n.u1Write ? "w" : "r",
4247 Pte.n.u1User ? "u" : "s",
4248 Pte.n.u1Accessed ? "a " : "na",
4249 Pte.n.u1Dirty ? "d " : "nd",
4250 Pte.n.u3Available,
4251 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
4252 Pte.n.u1WriteThru ? "pwt" : " ",
4253 Pte.n.u1CacheDisable ? "pcd" : " ",
4254 Pte.n.u1PAT ? "pat" : " ",
4255 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
4256 );
4257 if (Pte.u & UINT64_C(0x7fff000000000000))
4258 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
4259 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
4260 if (RT_FAILURE(rc))
4261 return rc;
4262
4263 /*
4264 * Advance.
4265 */
4266 VarPTEAddr.u.u64Number += cbEntry;
4267 if (iEntry != ~0U)
4268 VarGCPtr.u.GCFlat += PAGE_SIZE;
4269 } while (cEntries-- > 0);
4270
4271 return VINF_SUCCESS;
4272}
4273
4274
4275/**
4276 * @callback_method_impl{FNDBGCCMD, The 'dptb' command.}
4277 */
4278static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4279{
4280 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4281 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
4282 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
4283 if (RT_FAILURE(rc1))
4284 return rc1;
4285 NOREF(pCmd); NOREF(cArgs);
4286 return rc2;
4287}
4288
4289
4290/**
4291 * @callback_method_impl{FNDBGCCMD, The 'dt' command.}
4292 */
4293static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4294{
4295 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4296 int rc;
4297
4298 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4299 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
4300 if (cArgs == 1)
4301 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType != DBGCVAR_TYPE_STRING
4302 && paArgs[0].enmType != DBGCVAR_TYPE_SYMBOL);
4303
4304 /*
4305 * Check if the command indicates the type.
4306 */
4307 enum { kTss16, kTss32, kTss64, kTssToBeDetermined } enmTssType = kTssToBeDetermined;
4308 if (!strcmp(pCmd->pszCmd, "dt16"))
4309 enmTssType = kTss16;
4310 else if (!strcmp(pCmd->pszCmd, "dt32"))
4311 enmTssType = kTss32;
4312 else if (!strcmp(pCmd->pszCmd, "dt64"))
4313 enmTssType = kTss64;
4314
4315 /*
4316 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
4317 */
4318 uint32_t SelTss = UINT32_MAX;
4319 DBGCVAR VarTssAddr;
4320 if (cArgs == 0)
4321 {
4322 /** @todo consider querying the hidden bits instead (missing API). */
4323 uint16_t SelTR;
4324 rc = DBGFR3RegCpuQueryU16(pUVM, pDbgc->idCpu, DBGFREG_TR, &SelTR);
4325 if (RT_FAILURE(rc))
4326 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to query TR, rc=%Rrc\n", rc);
4327 DBGCVAR_INIT_GC_FAR(&VarTssAddr, SelTR, 0);
4328 SelTss = SelTR;
4329 }
4330 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
4331 {
4332 if (paArgs[0].u.u64Number < 0xffff)
4333 DBGCVAR_INIT_GC_FAR(&VarTssAddr, (RTSEL)paArgs[0].u.u64Number, 0);
4334 else
4335 {
4336 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
4337 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Element count doesn't combine with a TSS address.\n");
4338 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, paArgs[0].u.u64Number);
4339 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_BYTES)
4340 {
4341 VarTssAddr.enmRangeType = paArgs[0].enmRangeType;
4342 VarTssAddr.u64Range = paArgs[0].u64Range;
4343 }
4344 }
4345 }
4346 else
4347 VarTssAddr = paArgs[0];
4348
4349 /*
4350 * Deal with TSS:ign by means of the GDT.
4351 */
4352 if (VarTssAddr.enmType == DBGCVAR_TYPE_GC_FAR)
4353 {
4354 SelTss = VarTssAddr.u.GCFar.sel;
4355 DBGFSELINFO SelInfo;
4356 rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, VarTssAddr.u.GCFar.sel, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
4357 if (RT_FAILURE(rc))
4358 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3SelQueryInfo(,%u,%d,,) -> %Rrc.\n",
4359 pDbgc->idCpu, VarTssAddr.u.GCFar.sel, rc);
4360
4361 if (SelInfo.u.Raw.Gen.u1DescType)
4362 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (!sys)\n", VarTssAddr.u.GCFar.sel);
4363
4364 switch (SelInfo.u.Raw.Gen.u4Type)
4365 {
4366 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
4367 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
4368 if (enmTssType == kTssToBeDetermined)
4369 enmTssType = kTss16;
4370 break;
4371
4372 case X86_SEL_TYPE_SYS_386_TSS_BUSY: /* AMD64 too */
4373 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
4374 if (enmTssType == kTssToBeDetermined)
4375 enmTssType = SelInfo.fFlags & DBGFSELINFO_FLAGS_LONG_MODE ? kTss64 : kTss32;
4376 break;
4377
4378 default:
4379 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (type=%x)\n",
4380 VarTssAddr.u.GCFar.sel, SelInfo.u.Raw.Gen.u4Type);
4381 }
4382
4383 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, SelInfo.GCPtrBase);
4384 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, RT_MAX(SelInfo.cbLimit + 1, SelInfo.cbLimit));
4385 }
4386
4387 /*
4388 * Determine the TSS type if none is currently given.
4389 */
4390 if (enmTssType == kTssToBeDetermined)
4391 {
4392 if ( VarTssAddr.u64Range > 0
4393 && VarTssAddr.u64Range < sizeof(X86TSS32) - 4)
4394 enmTssType = kTss16;
4395 else
4396 {
4397 uint64_t uEfer;
4398 rc = DBGFR3RegCpuQueryU64(pUVM, pDbgc->idCpu, DBGFREG_MSR_K6_EFER, &uEfer);
4399 if ( RT_FAILURE(rc)
4400 || !(uEfer & MSR_K6_EFER_LMA) )
4401 enmTssType = kTss32;
4402 else
4403 enmTssType = kTss64;
4404 }
4405 }
4406
4407 /*
4408 * Figure the min/max sizes.
4409 * ASSUMES max TSS size is 64 KB.
4410 */
4411 uint32_t cbTssMin;
4412 uint32_t cbTssMax;
4413 switch (enmTssType)
4414 {
4415 case kTss16:
4416 cbTssMin = cbTssMax = X86_SEL_TYPE_SYS_286_TSS_LIMIT_MIN + 1;
4417 break;
4418 case kTss32:
4419 cbTssMin = X86_SEL_TYPE_SYS_386_TSS_LIMIT_MIN + 1;
4420 cbTssMax = _64K;
4421 break;
4422 case kTss64:
4423 cbTssMin = X86_SEL_TYPE_SYS_386_TSS_LIMIT_MIN + 1;
4424 cbTssMax = _64K;
4425 break;
4426 default:
4427 AssertFailedReturn(VERR_INTERNAL_ERROR);
4428 }
4429 uint32_t cbTss = VarTssAddr.enmRangeType == DBGCVAR_RANGE_BYTES ? (uint32_t)VarTssAddr.u64Range : 0;
4430 if (cbTss == 0)
4431 cbTss = cbTssMin;
4432 else if (cbTss < cbTssMin)
4433 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Minimum TSS size is %u bytes, you specified %llu (%llx) bytes.\n",
4434 cbTssMin, VarTssAddr.u64Range, VarTssAddr.u64Range);
4435 else if (cbTss > cbTssMax)
4436 cbTss = cbTssMax;
4437 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, cbTss);
4438
4439 /*
4440 * Read the TSS into a temporary buffer.
4441 */
4442 uint8_t abBuf[_64K];
4443 size_t cbTssRead;
4444 rc = DBGCCmdHlpMemRead(pCmdHlp, abBuf, cbTss, &VarTssAddr, &cbTssRead);
4445 if (RT_FAILURE(rc))
4446 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read TSS at %Dv: %Rrc\n", &VarTssAddr, rc);
4447 if (cbTssRead < cbTssMin)
4448 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read essential parts of the TSS (read %zu, min %zu).\n",
4449 cbTssRead, cbTssMin);
4450 if (cbTssRead < cbTss)
4451 memset(&abBuf[cbTssRead], 0xff, cbTss - cbTssRead);
4452
4453
4454 /*
4455 * Format the TSS.
4456 */
4457 uint16_t offIoBitmap;
4458 switch (enmTssType)
4459 {
4460 case kTss16:
4461 {
4462 PCX86TSS16 pTss = (PCX86TSS16)&abBuf[0];
4463 if (SelTss != UINT32_MAX)
4464 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS16 at %Dv\n", SelTss, &VarTssAddr);
4465 else
4466 DBGCCmdHlpPrintf(pCmdHlp, "TSS16 at %Dv\n", &VarTssAddr);
4467 DBGCCmdHlpPrintf(pCmdHlp,
4468 "ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x\n"
4469 "ip=%04x sp=%04x bp=%04x\n"
4470 "cs=%04x ss=%04x ds=%04x es=%04x flags=%04x\n"
4471 "ss:sp0=%04x:%04x ss:sp1=%04x:%04x ss:sp2=%04x:%04x\n"
4472 "prev=%04x ldtr=%04x\n"
4473 ,
4474 pTss->ax, pTss->bx, pTss->cx, pTss->dx, pTss->si, pTss->di,
4475 pTss->ip, pTss->sp, pTss->bp,
4476 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->flags,
4477 pTss->ss0, pTss->sp0, pTss->ss1, pTss->sp1, pTss->ss2, pTss->sp2,
4478 pTss->selPrev, pTss->selLdt);
4479 if (pTss->cs != 0)
4480 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%04x L 0", pTss->cs, pTss->ip);
4481 offIoBitmap = 0;
4482 break;
4483 }
4484
4485 case kTss32:
4486 {
4487 PCX86TSS32 pTss = (PCX86TSS32)&abBuf[0];
4488 if (SelTss != UINT32_MAX)
4489 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS32 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
4490 else
4491 DBGCCmdHlpPrintf(pCmdHlp, "TSS32 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
4492 DBGCCmdHlpPrintf(pCmdHlp,
4493 "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
4494 "eip=%08x esp=%08x ebp=%08x\n"
4495 "cs=%04x ss=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n"
4496 "ss:esp0=%04x:%08x ss:esp1=%04x:%08x ss:esp2=%04x:%08x\n"
4497 "prev=%04x ldtr=%04x cr3=%08x debug=%u iomap=%04x\n"
4498 ,
4499 pTss->eax, pTss->ebx, pTss->ecx, pTss->edx, pTss->esi, pTss->edi,
4500 pTss->eip, pTss->esp, pTss->ebp,
4501 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->fs, pTss->gs, pTss->eflags,
4502 pTss->ss0, pTss->esp0, pTss->ss1, pTss->esp1, pTss->ss2, pTss->esp2,
4503 pTss->selPrev, pTss->selLdt, pTss->cr3, pTss->fDebugTrap, pTss->offIoBitmap);
4504 if (pTss->cs != 0)
4505 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pTss->cs, pTss->eip);
4506 offIoBitmap = pTss->offIoBitmap;
4507 break;
4508 }
4509
4510 case kTss64:
4511 {
4512 PCX86TSS64 pTss = (PCX86TSS64)&abBuf[0];
4513 if (SelTss != UINT32_MAX)
4514 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS64 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
4515 else
4516 DBGCCmdHlpPrintf(pCmdHlp, "TSS64 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
4517 DBGCCmdHlpPrintf(pCmdHlp,
4518 "rsp0=%016RX64 rsp1=%016RX64 rsp2=%016RX64\n"
4519 "ist1=%016RX64 ist2=%016RX64\n"
4520 "ist3=%016RX64 ist4=%016RX64\n"
4521 "ist5=%016RX64 ist6=%016RX64\n"
4522 "ist7=%016RX64 iomap=%04x\n"
4523 ,
4524 pTss->rsp0, pTss->rsp1, pTss->rsp2,
4525 pTss->ist1, pTss->ist2,
4526 pTss->ist3, pTss->ist4,
4527 pTss->ist5, pTss->ist6,
4528 pTss->ist7, pTss->offIoBitmap);
4529 offIoBitmap = pTss->offIoBitmap;
4530 break;
4531 }
4532
4533 default:
4534 AssertFailedReturn(VERR_INTERNAL_ERROR);
4535 }
4536
4537 /*
4538 * Dump the interrupt redirection bitmap.
4539 */
4540 if (enmTssType != kTss16)
4541 {
4542 if ( offIoBitmap > cbTssMin
4543 && offIoBitmap < cbTss) /** @todo check exactly what the edge cases are here. */
4544 {
4545 if (offIoBitmap - cbTssMin >= 32)
4546 {
4547 DBGCCmdHlpPrintf(pCmdHlp, "Interrupt redirection:\n");
4548 uint8_t const *pbIntRedirBitmap = &abBuf[offIoBitmap - 32];
4549 uint32_t iStart = 0;
4550 bool fPrev = ASMBitTest(pbIntRedirBitmap, 0); /* LE/BE issue */
4551 for (uint32_t i = 0; i < 256; i++)
4552 {
4553 bool fThis = ASMBitTest(pbIntRedirBitmap, i);
4554 if (fThis != fPrev)
4555 {
4556 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, i - 1, fPrev ? "Protected mode" : "Redirected");
4557 fPrev = fThis;
4558 iStart = i;
4559 }
4560 }
4561 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, 255, fPrev ? "Protected mode" : "Redirected");
4562 }
4563 else
4564 DBGCCmdHlpPrintf(pCmdHlp, "Invalid interrupt redirection bitmap size: %u (%#x), expected 32 bytes.\n",
4565 offIoBitmap - cbTssMin, offIoBitmap - cbTssMin);
4566 }
4567 else if (offIoBitmap > 0)
4568 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap (-%#x)\n", cbTssMin - offIoBitmap);
4569 else
4570 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap\n");
4571 }
4572
4573 /*
4574 * Dump the I/O permission bitmap if present. The IOPM cannot start below offset 0x68
4575 * (that applies to both 32-bit and 64-bit TSSs since their size is the same).
4576 * Note that there is always one padding byte that is not technically part of the bitmap
4577 * and "must have all bits set". It's not clear what happens when it doesn't. All ports
4578 * not covered by the bitmap are considered to be not accessible.
4579 */
4580 if (enmTssType != kTss16)
4581 {
4582 if (offIoBitmap < cbTss && offIoBitmap >= 0x68)
4583 {
4584 uint32_t cPorts = RT_MIN((cbTss - offIoBitmap) * 8, _64K);
4585 DBGCVAR VarAddr;
4586 DBGCCmdHlpEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarTssAddr, offIoBitmap);
4587 DBGCCmdHlpPrintf(pCmdHlp, "I/O bitmap at %DV - %#x ports:\n", &VarAddr, cPorts);
4588
4589 uint8_t const *pbIoBitmap = &abBuf[offIoBitmap];
4590 uint32_t iStart = 0;
4591 bool fPrev = ASMBitTest(pbIoBitmap, 0);
4592 uint32_t cLine = 0;
4593 for (uint32_t i = 1; i < _64K; i++)
4594 {
4595 bool fThis = i < cPorts ? ASMBitTest(pbIoBitmap, i) : true;
4596 if (fThis != fPrev)
4597 {
4598 cLine++;
4599 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s%s", iStart, i-1,
4600 fPrev ? "GP" : "OK", (cLine % 6) == 0 ? "\n" : " ");
4601 fPrev = fThis;
4602 iStart = i;
4603 }
4604 }
4605 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s\n", iStart, _64K-1, fPrev ? "GP" : "OK");
4606 }
4607 else if (offIoBitmap > 0)
4608 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap (-%#x)\n", cbTssMin - offIoBitmap);
4609 else
4610 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap\n");
4611 }
4612
4613 return VINF_SUCCESS;
4614}
4615
4616
4617/**
4618 * @callback_method_impl{FNDBGFR3TYPEDUMP, The 'dti' command dumper callback.}
4619 */
4620static DECLCALLBACK(int) dbgcCmdDumpTypeInfoCallback(uint32_t off, const char *pszField, uint32_t iLvl,
4621 const char *pszType, uint32_t fTypeFlags,
4622 uint32_t cElements, void *pvUser)
4623{
4624 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pvUser;
4625
4626 /* Pad with spaces to match the level. */
4627 for (uint32_t i = 0; i < iLvl; i++)
4628 DBGCCmdHlpPrintf(pCmdHlp, " ");
4629
4630 size_t cbWritten = 0;
4631 DBGCCmdHlpPrintfEx(pCmdHlp, &cbWritten, "+0x%04x %s", off, pszField);
4632 while (cbWritten < 32)
4633 {
4634 /* Fill with spaces to get proper aligning. */
4635 DBGCCmdHlpPrintf(pCmdHlp, " ");
4636 cbWritten++;
4637 }
4638
4639 DBGCCmdHlpPrintf(pCmdHlp, ": ");
4640 if (fTypeFlags & DBGFTYPEREGMEMBER_F_ARRAY)
4641 DBGCCmdHlpPrintf(pCmdHlp, "[%u] ", cElements);
4642 if (fTypeFlags & DBGFTYPEREGMEMBER_F_POINTER)
4643 DBGCCmdHlpPrintf(pCmdHlp, "Ptr ");
4644 DBGCCmdHlpPrintf(pCmdHlp, "%s\n", pszType);
4645
4646 return VINF_SUCCESS;
4647}
4648
4649
4650/**
4651 * @callback_method_impl{FNDBGCCMD, The 'dti' command.}
4652 */
4653static DECLCALLBACK(int) dbgcCmdDumpTypeInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4654{
4655 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4656 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1 || cArgs == 2);
4657 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING);
4658 if (cArgs == 2)
4659 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[1].enmType == DBGCVAR_TYPE_NUMBER);
4660
4661 uint32_t cLvlMax = cArgs == 2 ? (uint32_t)paArgs[1].u.u64Number : UINT32_MAX;
4662 return DBGFR3TypeDumpEx(pUVM, paArgs[0].u.pszString, 0 /* fFlags */, cLvlMax,
4663 dbgcCmdDumpTypeInfoCallback, pCmdHlp);
4664}
4665
4666
4667static void dbgcCmdDumpTypedValCallbackBuiltin(PDBGCCMDHLP pCmdHlp, DBGFTYPEBUILTIN enmType, size_t cbType,
4668 PDBGFTYPEVALBUF pValBuf)
4669{
4670 switch (enmType)
4671 {
4672 case DBGFTYPEBUILTIN_UINT8:
4673 DBGCCmdHlpPrintf(pCmdHlp, "%RU8", pValBuf->u8);
4674 break;
4675 case DBGFTYPEBUILTIN_INT8:
4676 DBGCCmdHlpPrintf(pCmdHlp, "%RI8", pValBuf->i8);
4677 break;
4678 case DBGFTYPEBUILTIN_UINT16:
4679 DBGCCmdHlpPrintf(pCmdHlp, "%RU16", pValBuf->u16);
4680 break;
4681 case DBGFTYPEBUILTIN_INT16:
4682 DBGCCmdHlpPrintf(pCmdHlp, "%RI16", pValBuf->i16);
4683 break;
4684 case DBGFTYPEBUILTIN_UINT32:
4685 DBGCCmdHlpPrintf(pCmdHlp, "%RU32", pValBuf->u32);
4686 break;
4687 case DBGFTYPEBUILTIN_INT32:
4688 DBGCCmdHlpPrintf(pCmdHlp, "%RI32", pValBuf->i32);
4689 break;
4690 case DBGFTYPEBUILTIN_UINT64:
4691 DBGCCmdHlpPrintf(pCmdHlp, "%RU64", pValBuf->u64);
4692 break;
4693 case DBGFTYPEBUILTIN_INT64:
4694 DBGCCmdHlpPrintf(pCmdHlp, "%RI64", pValBuf->i64);
4695 break;
4696 case DBGFTYPEBUILTIN_PTR32:
4697 DBGCCmdHlpPrintf(pCmdHlp, "%RX32", pValBuf->GCPtr);
4698 break;
4699 case DBGFTYPEBUILTIN_PTR64:
4700 DBGCCmdHlpPrintf(pCmdHlp, "%RX64", pValBuf->GCPtr);
4701 break;
4702 case DBGFTYPEBUILTIN_PTR:
4703 if (cbType == sizeof(uint32_t))
4704 DBGCCmdHlpPrintf(pCmdHlp, "%RX32", pValBuf->GCPtr);
4705 else if (cbType == sizeof(uint64_t))
4706 DBGCCmdHlpPrintf(pCmdHlp, "%RX64", pValBuf->GCPtr);
4707 else
4708 DBGCCmdHlpPrintf(pCmdHlp, "<Unsupported pointer width %u>", cbType);
4709 break;
4710 case DBGFTYPEBUILTIN_SIZE:
4711 if (cbType == sizeof(uint32_t))
4712 DBGCCmdHlpPrintf(pCmdHlp, "%RU32", pValBuf->size);
4713 else if (cbType == sizeof(uint64_t))
4714 DBGCCmdHlpPrintf(pCmdHlp, "%RU64", pValBuf->size);
4715 else
4716 DBGCCmdHlpPrintf(pCmdHlp, "<Unsupported size width %u>", cbType);
4717 break;
4718 case DBGFTYPEBUILTIN_FLOAT32:
4719 case DBGFTYPEBUILTIN_FLOAT64:
4720 case DBGFTYPEBUILTIN_COMPOUND:
4721 default:
4722 AssertMsgFailed(("Invalid built-in type: %d\n", enmType));
4723 }
4724}
4725
4726/**
4727 * @callback_method_impl{FNDBGFR3TYPEDUMP, The 'dtv' command dumper callback.}
4728 */
4729static DECLCALLBACK(int) dbgcCmdDumpTypedValCallback(uint32_t off, const char *pszField, uint32_t iLvl,
4730 DBGFTYPEBUILTIN enmType, size_t cbType,
4731 PDBGFTYPEVALBUF pValBuf, uint32_t cValBufs,
4732 void *pvUser)
4733{
4734 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pvUser;
4735
4736 /* Pad with spaces to match the level. */
4737 for (uint32_t i = 0; i < iLvl; i++)
4738 DBGCCmdHlpPrintf(pCmdHlp, " ");
4739
4740 size_t cbWritten = 0;
4741 DBGCCmdHlpPrintfEx(pCmdHlp, &cbWritten, "+0x%04x %s", off, pszField);
4742 while (cbWritten < 32)
4743 {
4744 /* Fill with spaces to get proper aligning. */
4745 DBGCCmdHlpPrintf(pCmdHlp, " ");
4746 cbWritten++;
4747 }
4748
4749 DBGCCmdHlpPrintf(pCmdHlp, ": ");
4750 if (cValBufs > 1)
4751 DBGCCmdHlpPrintf(pCmdHlp, "[%u] [ ", cValBufs);
4752
4753 for (uint32_t i = 0; i < cValBufs; i++)
4754 {
4755 dbgcCmdDumpTypedValCallbackBuiltin(pCmdHlp, enmType, cbType, pValBuf);
4756 if (i < cValBufs - 1)
4757 DBGCCmdHlpPrintf(pCmdHlp, " , ");
4758 pValBuf++;
4759 }
4760
4761 if (cValBufs > 1)
4762 DBGCCmdHlpPrintf(pCmdHlp, " ]");
4763 DBGCCmdHlpPrintf(pCmdHlp, "\n");
4764
4765 return VINF_SUCCESS;
4766}
4767
4768
4769/**
4770 * @callback_method_impl{FNDBGCCMD, The 'dtv' command.}
4771 */
4772static DECLCALLBACK(int) dbgcCmdDumpTypedVal(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4773{
4774 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4775 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 2 || cArgs == 3);
4776 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING);
4777 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISGCPOINTER(paArgs[1].enmType));
4778 if (cArgs == 3)
4779 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[2].enmType == DBGCVAR_TYPE_NUMBER);
4780
4781 /*
4782 * Make DBGF address and fix the range.
4783 */
4784 DBGFADDRESS Address;
4785 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[1], &Address);
4786 if (RT_FAILURE(rc))
4787 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", &paArgs[1]);
4788
4789 uint32_t cLvlMax = cArgs == 3 ? (uint32_t)paArgs[2].u.u64Number : UINT32_MAX;
4790 return DBGFR3TypeValDumpEx(pUVM, &Address, paArgs[0].u.pszString, 0 /* fFlags */, cLvlMax,
4791 dbgcCmdDumpTypedValCallback, pCmdHlp);
4792}
4793
4794/**
4795 * @callback_method_impl{FNDBGCCMD, The 'm' command.}
4796 */
4797static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4798{
4799 DBGCCmdHlpPrintf(pCmdHlp, "Address: %DV\n", &paArgs[0]);
4800 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4801 return dbgcCmdDumpPageHierarchy(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
4802}
4803
4804
4805/**
4806 * Converts one or more variables into a byte buffer for a
4807 * given unit size.
4808 *
4809 * @returns VBox status codes:
4810 * @retval VERR_TOO_MUCH_DATA if the buffer is too small, bitched.
4811 * @retval VERR_INTERNAL_ERROR on bad variable type, bitched.
4812 * @retval VINF_SUCCESS on success.
4813 *
4814 * @param pCmdHlp The command helper callback table.
4815 * @param pvBuf The buffer to convert into.
4816 * @param pcbBuf The buffer size on input. The size of the result on output.
4817 * @param cbUnit The unit size to apply when converting.
4818 * The high bit is used to indicate unicode string.
4819 * @param paVars The array of variables to convert.
4820 * @param cVars The number of variables.
4821 */
4822int dbgcVarsToBytes(PDBGCCMDHLP pCmdHlp, void *pvBuf, uint32_t *pcbBuf, size_t cbUnit, PCDBGCVAR paVars, unsigned cVars)
4823{
4824 union
4825 {
4826 uint8_t *pu8;
4827 uint16_t *pu16;
4828 uint32_t *pu32;
4829 uint64_t *pu64;
4830 } u, uEnd;
4831 u.pu8 = (uint8_t *)pvBuf;
4832 uEnd.pu8 = u.pu8 + *pcbBuf;
4833
4834 unsigned i;
4835 for (i = 0; i < cVars && u.pu8 < uEnd.pu8; i++)
4836 {
4837 switch (paVars[i].enmType)
4838 {
4839 case DBGCVAR_TYPE_GC_FAR:
4840 case DBGCVAR_TYPE_GC_FLAT:
4841 case DBGCVAR_TYPE_GC_PHYS:
4842 case DBGCVAR_TYPE_HC_FLAT:
4843 case DBGCVAR_TYPE_HC_PHYS:
4844 case DBGCVAR_TYPE_NUMBER:
4845 {
4846 uint64_t u64 = paVars[i].u.u64Number;
4847 switch (cbUnit & 0x1f)
4848 {
4849 case 1:
4850 do
4851 {
4852 *u.pu8++ = u64;
4853 u64 >>= 8;
4854 } while (u64);
4855 break;
4856 case 2:
4857 do
4858 {
4859 *u.pu16++ = u64;
4860 u64 >>= 16;
4861 } while (u64);
4862 break;
4863 case 4:
4864 *u.pu32++ = u64;
4865 u64 >>= 32;
4866 if (u64)
4867 *u.pu32++ = u64;
4868 break;
4869 case 8:
4870 *u.pu64++ = u64;
4871 break;
4872 }
4873 break;
4874 }
4875
4876 case DBGCVAR_TYPE_STRING:
4877 case DBGCVAR_TYPE_SYMBOL:
4878 {
4879 const char *psz = paVars[i].u.pszString;
4880 size_t cbString = strlen(psz);
4881 if (cbUnit & RT_BIT_32(31))
4882 {
4883 /* Explode char to unit. */
4884 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8) * (cbUnit & 0x1f))
4885 {
4886 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
4887 return VERR_TOO_MUCH_DATA;
4888 }
4889 while (*psz)
4890 {
4891 switch (cbUnit & 0x1f)
4892 {
4893 case 1: *u.pu8++ = *psz; break;
4894 case 2: *u.pu16++ = *psz; break;
4895 case 4: *u.pu32++ = *psz; break;
4896 case 8: *u.pu64++ = *psz; break;
4897 }
4898 psz++;
4899 }
4900 }
4901 else
4902 {
4903 /* Raw copy with zero padding if the size isn't aligned. */
4904 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8))
4905 {
4906 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
4907 return VERR_TOO_MUCH_DATA;
4908 }
4909
4910 size_t cbCopy = cbString & ~(cbUnit - 1);
4911 memcpy(u.pu8, psz, cbCopy);
4912 u.pu8 += cbCopy;
4913 psz += cbCopy;
4914
4915 size_t cbReminder = cbString & (cbUnit - 1);
4916 if (cbReminder)
4917 {
4918 memcpy(u.pu8, psz, cbString & (cbUnit - 1));
4919 memset(u.pu8 + cbReminder, 0, cbUnit - cbReminder);
4920 u.pu8 += cbUnit;
4921 }
4922 }
4923 break;
4924 }
4925
4926 default:
4927 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
4928 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INTERNAL_ERROR,
4929 "i=%d enmType=%d\n", i, paVars[i].enmType);
4930 return VERR_INTERNAL_ERROR;
4931 }
4932 }
4933 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
4934 if (i != cVars)
4935 {
4936 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
4937 return VERR_TOO_MUCH_DATA;
4938 }
4939 return VINF_SUCCESS;
4940}
4941
4942
4943/**
4944 * @callback_method_impl{FNDBGCCMD, The 'eb'\, 'ew'\, 'ed' and 'eq' commands.}
4945 */
4946static DECLCALLBACK(int) dbgcCmdEditMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4947{
4948 /*
4949 * Validate input.
4950 */
4951 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs >= 2);
4952 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
4953 for (unsigned iArg = 1; iArg < cArgs; iArg++)
4954 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER);
4955 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4956
4957 /*
4958 * Figure out the element size.
4959 */
4960 unsigned cbElement;
4961 switch (pCmd->pszCmd[1])
4962 {
4963 default:
4964 case 'b': cbElement = 1; break;
4965 case 'w': cbElement = 2; break;
4966 case 'd': cbElement = 4; break;
4967 case 'q': cbElement = 8; break;
4968 }
4969
4970 /*
4971 * Do setting.
4972 */
4973 DBGCVAR Addr = paArgs[0];
4974 for (unsigned iArg = 1;;)
4975 {
4976 size_t cbWritten;
4977 int rc = pCmdHlp->pfnMemWrite(pCmdHlp, &paArgs[iArg].u, cbElement, &Addr, &cbWritten);
4978 if (RT_FAILURE(rc))
4979 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Writing memory at %DV.\n", &Addr);
4980 if (cbWritten != cbElement)
4981 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Only wrote %u out of %u bytes!\n", cbWritten, cbElement);
4982
4983 /* advance. */
4984 iArg++;
4985 if (iArg >= cArgs)
4986 break;
4987 rc = DBGCCmdHlpEval(pCmdHlp, &Addr, "%Dv + %#x", &Addr, cbElement);
4988 if (RT_FAILURE(rc))
4989 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
4990 }
4991
4992 return VINF_SUCCESS;
4993}
4994
4995
4996/**
4997 * Executes the search.
4998 *
4999 * @returns VBox status code.
5000 * @param pCmdHlp The command helpers.
5001 * @param pUVM The user mode VM handle.
5002 * @param pAddress The address to start searching from. (undefined on output)
5003 * @param cbRange The address range to search. Must not wrap.
5004 * @param pabBytes The byte pattern to search for.
5005 * @param cbBytes The size of the pattern.
5006 * @param cbUnit The search unit.
5007 * @param cMaxHits The max number of hits.
5008 * @param pResult Where to store the result if it's a function invocation.
5009 */
5010static int dbgcCmdWorkerSearchMemDoIt(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PDBGFADDRESS pAddress, RTGCUINTPTR cbRange,
5011 const uint8_t *pabBytes, uint32_t cbBytes,
5012 uint32_t cbUnit, uint64_t cMaxHits, PDBGCVAR pResult)
5013{
5014 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5015
5016 /*
5017 * Do the search.
5018 */
5019 uint64_t cHits = 0;
5020 for (;;)
5021 {
5022 /* search */
5023 DBGFADDRESS HitAddress;
5024 int rc = DBGFR3MemScan(pUVM, pDbgc->idCpu, pAddress, cbRange, 1, pabBytes, cbBytes, &HitAddress);
5025 if (RT_FAILURE(rc))
5026 {
5027 if (rc != VERR_DBGF_MEM_NOT_FOUND)
5028 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3MemScan\n");
5029
5030 /* update the current address so we can save it (later). */
5031 pAddress->off += cbRange;
5032 pAddress->FlatPtr += cbRange;
5033 cbRange = 0;
5034 break;
5035 }
5036
5037 /* report result */
5038 DBGCVAR VarCur;
5039 rc = DBGCCmdHlpVarFromDbgfAddr(pCmdHlp, &HitAddress, &VarCur);
5040 if (RT_FAILURE(rc))
5041 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGCCmdHlpVarFromDbgfAddr\n");
5042 if (!pResult)
5043 pCmdHlp->pfnExec(pCmdHlp, "db %DV LB 10", &VarCur);
5044 else
5045 DBGCVAR_ASSIGN(pResult, &VarCur);
5046
5047 /* advance */
5048 cbRange -= HitAddress.FlatPtr - pAddress->FlatPtr;
5049 *pAddress = HitAddress;
5050 pAddress->FlatPtr += cbBytes;
5051 pAddress->off += cbBytes;
5052 if (cbRange <= cbBytes)
5053 {
5054 cbRange = 0;
5055 break;
5056 }
5057 cbRange -= cbBytes;
5058
5059 if (++cHits >= cMaxHits)
5060 {
5061 /// @todo save the search.
5062 break;
5063 }
5064 }
5065
5066 /*
5067 * Save the search so we can resume it...
5068 */
5069 if (pDbgc->abSearch != pabBytes)
5070 {
5071 memcpy(pDbgc->abSearch, pabBytes, cbBytes);
5072 pDbgc->cbSearch = cbBytes;
5073 pDbgc->cbSearchUnit = cbUnit;
5074 }
5075 pDbgc->cMaxSearchHits = cMaxHits;
5076 pDbgc->SearchAddr = *pAddress;
5077 pDbgc->cbSearchRange = cbRange;
5078
5079 return cHits ? VINF_SUCCESS : VERR_DBGC_COMMAND_FAILED;
5080}
5081
5082
5083/**
5084 * Resumes the previous search.
5085 *
5086 * @returns VBox status code.
5087 * @param pCmdHlp Pointer to the command helper functions.
5088 * @param pUVM The user mode VM handle.
5089 * @param pResult Where to store the result of a function invocation.
5090 */
5091static int dbgcCmdWorkerSearchMemResume(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PDBGCVAR pResult)
5092{
5093 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5094
5095 /*
5096 * Make sure there is a previous command.
5097 */
5098 if (!pDbgc->cbSearch)
5099 {
5100 DBGCCmdHlpPrintf(pCmdHlp, "Error: No previous search\n");
5101 return VERR_DBGC_COMMAND_FAILED;
5102 }
5103
5104 /*
5105 * Make range and address adjustments.
5106 */
5107 DBGFADDRESS Address = pDbgc->SearchAddr;
5108 if (Address.FlatPtr == ~(RTGCUINTPTR)0)
5109 {
5110 Address.FlatPtr -= Address.off;
5111 Address.off = 0;
5112 }
5113
5114 RTGCUINTPTR cbRange = pDbgc->cbSearchRange;
5115 if (!cbRange)
5116 cbRange = ~(RTGCUINTPTR)0;
5117 if (Address.FlatPtr + cbRange < pDbgc->SearchAddr.FlatPtr)
5118 cbRange = ~(RTGCUINTPTR)0 - pDbgc->SearchAddr.FlatPtr + !!pDbgc->SearchAddr.FlatPtr;
5119
5120 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pUVM, &Address, cbRange, pDbgc->abSearch, pDbgc->cbSearch,
5121 pDbgc->cbSearchUnit, pDbgc->cMaxSearchHits, pResult);
5122}
5123
5124
5125/**
5126 * Search memory, worker for the 's' and 's?' functions.
5127 *
5128 * @returns VBox status code.
5129 * @param pCmdHlp Pointer to the command helper functions.
5130 * @param pUVM The user mode VM handle.
5131 * @param pAddress Where to start searching. If no range, search till end of address space.
5132 * @param cMaxHits The maximum number of hits.
5133 * @param chType The search type.
5134 * @param paPatArgs The pattern variable array.
5135 * @param cPatArgs Number of pattern variables.
5136 * @param pResult Where to store the result of a function invocation.
5137 */
5138static int dbgcCmdWorkerSearchMem(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pAddress, uint64_t cMaxHits, char chType,
5139 PCDBGCVAR paPatArgs, unsigned cPatArgs, PDBGCVAR pResult)
5140{
5141 if (pResult)
5142 DBGCVAR_INIT_GC_FLAT(pResult, 0);
5143
5144 /*
5145 * Convert the search pattern into bytes and DBGFR3MemScan can deal with.
5146 */
5147 uint32_t cbUnit;
5148 switch (chType)
5149 {
5150 case 'a':
5151 case 'b': cbUnit = 1; break;
5152 case 'u': cbUnit = 2 | RT_BIT_32(31); break;
5153 case 'w': cbUnit = 2; break;
5154 case 'd': cbUnit = 4; break;
5155 case 'q': cbUnit = 8; break;
5156 default:
5157 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "chType=%c\n", chType);
5158 }
5159 uint8_t abBytes[RT_SIZEOFMEMB(DBGC, abSearch)];
5160 uint32_t cbBytes = sizeof(abBytes);
5161 int rc = dbgcVarsToBytes(pCmdHlp, abBytes, &cbBytes, cbUnit, paPatArgs, cPatArgs);
5162 if (RT_FAILURE(rc))
5163 return VERR_DBGC_COMMAND_FAILED;
5164
5165 /*
5166 * Make DBGF address and fix the range.
5167 */
5168 DBGFADDRESS Address;
5169 rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, pAddress, &Address);
5170 if (RT_FAILURE(rc))
5171 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", pAddress);
5172
5173 RTGCUINTPTR cbRange;
5174 switch (pAddress->enmRangeType)
5175 {
5176 case DBGCVAR_RANGE_BYTES:
5177 cbRange = pAddress->u64Range;
5178 if (cbRange != pAddress->u64Range)
5179 cbRange = ~(RTGCUINTPTR)0;
5180 break;
5181
5182 case DBGCVAR_RANGE_ELEMENTS:
5183 cbRange = (RTGCUINTPTR)(pAddress->u64Range * cbUnit);
5184 if ( cbRange != pAddress->u64Range * cbUnit
5185 || cbRange < pAddress->u64Range)
5186 cbRange = ~(RTGCUINTPTR)0;
5187 break;
5188
5189 default:
5190 cbRange = ~(RTGCUINTPTR)0;
5191 break;
5192 }
5193 if (Address.FlatPtr + cbRange < Address.FlatPtr)
5194 cbRange = ~(RTGCUINTPTR)0 - Address.FlatPtr + !!Address.FlatPtr;
5195
5196 /*
5197 * Ok, do it.
5198 */
5199 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pUVM, &Address, cbRange, abBytes, cbBytes, cbUnit, cMaxHits, pResult);
5200}
5201
5202
5203/**
5204 * @callback_method_impl{FNDBGCCMD, The 's' command.}
5205 */
5206static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5207{
5208 RT_NOREF2(pCmd, paArgs);
5209
5210 /* check that the parser did what it's supposed to do. */
5211 //if ( cArgs <= 2
5212 // && paArgs[0].enmType != DBGCVAR_TYPE_STRING)
5213 // return DBGCCmdHlpPrintf(pCmdHlp, "parser error\n");
5214
5215 /*
5216 * Repeat previous search?
5217 */
5218 if (cArgs == 0)
5219 return dbgcCmdWorkerSearchMemResume(pCmdHlp, pUVM, NULL);
5220
5221 /*
5222 * Parse arguments.
5223 */
5224
5225 return -1;
5226}
5227
5228
5229/**
5230 * @callback_method_impl{FNDBGCCMD, The 's?' command.}
5231 */
5232static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5233{
5234 /* check that the parser did what it's supposed to do. */
5235 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs >= 2 && DBGCVAR_ISGCPOINTER(paArgs[0].enmType));
5236 return dbgcCmdWorkerSearchMem(pCmdHlp, pUVM, &paArgs[0], 25, pCmd->pszCmd[1], paArgs + 1, cArgs - 1, NULL);
5237}
5238
5239
5240/**
5241 * Matching function for interrupts event names.
5242 *
5243 * This parses the interrupt number and length.
5244 *
5245 * @returns True if match, false if not.
5246 * @param pPattern The user specified pattern to match.
5247 * @param pszEvtName The event name.
5248 * @param pCmdHlp Command helpers for warning about malformed stuff.
5249 * @param piFirst Where to return start interrupt number on success.
5250 * @param pcInts Where to return the number of interrupts on success.
5251 */
5252static bool dbgcEventIsMatchingInt(PCDBGCVAR pPattern, const char *pszEvtName, PDBGCCMDHLP pCmdHlp,
5253 uint8_t *piFirst, uint16_t *pcInts)
5254{
5255 /*
5256 * Ignore trailing hex digits when comparing with the event base name.
5257 */
5258 const char *pszPattern = pPattern->u.pszString;
5259 const char *pszEnd = RTStrEnd(pszPattern, RTSTR_MAX);
5260 while ( (uintptr_t)pszEnd > (uintptr_t)pszPattern
5261 && RT_C_IS_XDIGIT(pszEnd[-1]))
5262 pszEnd -= 1;
5263 if (RTStrSimplePatternNMatch(pszPattern, pszEnd - pszPattern, pszEvtName, RTSTR_MAX))
5264 {
5265 /*
5266 * Parse the index and length.
5267 */
5268 if (!*pszEnd)
5269 *piFirst = 0;
5270 else
5271 {
5272 int rc = RTStrToUInt8Full(pszEnd, 16, piFirst);
5273 if (rc != VINF_SUCCESS)
5274 {
5275 if (RT_FAILURE(rc))
5276 *piFirst = 0;
5277 DBGCCmdHlpPrintf(pCmdHlp, "Warning: %Rrc parsing '%s' - interpreting it as %#x\n", rc, pszEnd, *piFirst);
5278 }
5279 }
5280
5281 if (pPattern->enmRangeType == DBGCVAR_RANGE_NONE)
5282 *pcInts = 1;
5283 else
5284 *pcInts = RT_MAX(RT_MIN((uint16_t)pPattern->u64Range, 256 - *piFirst), 1);
5285 return true;
5286 }
5287 return false;
5288}
5289
5290
5291/**
5292 * Updates a DBGC event config.
5293 *
5294 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
5295 * @param ppEvtCfg The event configuration entry to update.
5296 * @param pszCmd The new command. Leave command alone if NULL.
5297 * @param enmEvtState The new event state.
5298 * @param fChangeCmdOnly Whether to only update the command.
5299 */
5300static int dbgcEventUpdate(PDBGCEVTCFG *ppEvtCfg, const char *pszCmd, DBGCEVTSTATE enmEvtState, bool fChangeCmdOnly)
5301{
5302 PDBGCEVTCFG pEvtCfg = *ppEvtCfg;
5303
5304 /*
5305 * If we've got a command string, update the command too.
5306 */
5307 if (pszCmd)
5308 {
5309 size_t cchCmd = strlen(pszCmd);
5310 if ( !cchCmd
5311 && ( !fChangeCmdOnly
5312 ? enmEvtState == kDbgcEvtState_Disabled
5313 : !pEvtCfg || pEvtCfg->enmState == kDbgcEvtState_Disabled))
5314 {
5315 /* NULL entry is fine if no command and disabled. */
5316 RTMemFree(pEvtCfg);
5317 *ppEvtCfg = NULL;
5318 }
5319 else
5320 {
5321 if (!pEvtCfg || pEvtCfg->cchCmd < cchCmd)
5322 {
5323 RTMemFree(pEvtCfg);
5324 *ppEvtCfg = pEvtCfg = (PDBGCEVTCFG)RTMemAlloc(RT_UOFFSETOF_DYN(DBGCEVTCFG, szCmd[cchCmd + 1]));
5325 if (!pEvtCfg)
5326 return VERR_NO_MEMORY;
5327 }
5328 pEvtCfg->enmState = enmEvtState;
5329 pEvtCfg->cchCmd = cchCmd;
5330 memcpy(pEvtCfg->szCmd, pszCmd, cchCmd + 1);
5331 }
5332 }
5333 /*
5334 * Update existing or enable new. If NULL and not enabled, we can keep it that way.
5335 */
5336 else if (pEvtCfg || enmEvtState != kDbgcEvtState_Disabled)
5337 {
5338 if (!pEvtCfg)
5339 {
5340 *ppEvtCfg = pEvtCfg = (PDBGCEVTCFG)RTMemAlloc(sizeof(DBGCEVTCFG));
5341 if (!pEvtCfg)
5342 return VERR_NO_MEMORY;
5343 pEvtCfg->cchCmd = 0;
5344 pEvtCfg->szCmd[0] = '\0';
5345 }
5346 pEvtCfg->enmState = enmEvtState;
5347 }
5348
5349 return VINF_SUCCESS;
5350}
5351
5352
5353/**
5354 * Record one settings change for a plain event.
5355 *
5356 * @returns The new @a cIntCfgs value.
5357 * @param paEventCfgs The event setttings array. Must have DBGFEVENT_END
5358 * entries.
5359 * @param cEventCfgs The current number of entries in @a paEventCfgs.
5360 * @param enmType The event to change the settings for.
5361 * @param enmEvtState The new event state.
5362 * @param iSxEvt Index into the g_aDbgcSxEvents array.
5363 *
5364 * @remarks We use abUnused[0] for the enmEvtState, while abUnused[1] and
5365 * abUnused[2] are used for iSxEvt.
5366 */
5367static uint32_t dbgcEventAddPlainConfig(PDBGFEVENTCONFIG paEventCfgs, uint32_t cEventCfgs, DBGFEVENTTYPE enmType,
5368 DBGCEVTSTATE enmEvtState, uint16_t iSxEvt)
5369{
5370 uint32_t iCfg;
5371 for (iCfg = 0; iCfg < cEventCfgs; iCfg++)
5372 if (paEventCfgs[iCfg].enmType == enmType)
5373 break;
5374 if (iCfg == cEventCfgs)
5375 {
5376 Assert(cEventCfgs < DBGFEVENT_END);
5377 paEventCfgs[iCfg].enmType = enmType;
5378 cEventCfgs++;
5379 }
5380 paEventCfgs[iCfg].fEnabled = enmEvtState > kDbgcEvtState_Disabled;
5381 paEventCfgs[iCfg].abUnused[0] = enmEvtState;
5382 paEventCfgs[iCfg].abUnused[1] = (uint8_t)iSxEvt;
5383 paEventCfgs[iCfg].abUnused[2] = (uint8_t)(iSxEvt >> 8);
5384 return cEventCfgs;
5385}
5386
5387
5388/**
5389 * Record one or more interrupt event config changes.
5390 *
5391 * @returns The new @a cIntCfgs value.
5392 * @param paIntCfgs Interrupt confiruation array. Must have 256 entries.
5393 * @param cIntCfgs The current number of entries in @a paIntCfgs.
5394 * @param iInt The interrupt number to start with.
5395 * @param cInts The number of interrupts to change.
5396 * @param pszName The settings name (hwint/swint).
5397 * @param enmEvtState The new event state.
5398 * @param bIntOp The new DBGF interrupt state.
5399 */
5400static uint32_t dbgcEventAddIntConfig(PDBGFINTERRUPTCONFIG paIntCfgs, uint32_t cIntCfgs, uint8_t iInt, uint16_t cInts,
5401 const char *pszName, DBGCEVTSTATE enmEvtState, uint8_t bIntOp)
5402{
5403 bool const fHwInt = *pszName == 'h';
5404
5405 bIntOp |= (uint8_t)enmEvtState << 4;
5406 uint8_t const bSoftState = !fHwInt ? bIntOp : DBGFINTERRUPTSTATE_DONT_TOUCH;
5407 uint8_t const bHardState = fHwInt ? bIntOp : DBGFINTERRUPTSTATE_DONT_TOUCH;
5408
5409 while (cInts > 0)
5410 {
5411 uint32_t iCfg;
5412 for (iCfg = 0; iCfg < cIntCfgs; iCfg++)
5413 if (paIntCfgs[iCfg].iInterrupt == iInt)
5414 break;
5415 if (iCfg == cIntCfgs)
5416 break;
5417 if (fHwInt)
5418 paIntCfgs[iCfg].enmHardState = bHardState;
5419 else
5420 paIntCfgs[iCfg].enmSoftState = bSoftState;
5421 iInt++;
5422 cInts--;
5423 }
5424
5425 while (cInts > 0)
5426 {
5427 Assert(cIntCfgs < 256);
5428 paIntCfgs[cIntCfgs].iInterrupt = iInt;
5429 paIntCfgs[cIntCfgs].enmHardState = bHardState;
5430 paIntCfgs[cIntCfgs].enmSoftState = bSoftState;
5431 cIntCfgs++;
5432 iInt++;
5433 cInts--;
5434 }
5435
5436 return cIntCfgs;
5437}
5438
5439
5440/**
5441 * Applies event settings changes to DBGC and DBGF.
5442 *
5443 * @returns VBox status code (fully bitched)
5444 * @param pCmdHlp The command helpers.
5445 * @param pUVM The user mode VM handle.
5446 * @param paIntCfgs Interrupt configuration array. We use the upper 4
5447 * bits of the settings for the DBGCEVTSTATE. This
5448 * will be cleared.
5449 * @param cIntCfgs Number of interrupt configuration changes.
5450 * @param paEventCfgs The generic event configuration array. We use the
5451 * abUnused[0] member for the DBGCEVTSTATE, and
5452 * abUnused[2:1] for the g_aDbgcSxEvents index.
5453 * @param cEventCfgs The number of generic event settings changes.
5454 * @param pszCmd The commands to associate with the changed events.
5455 * If this is NULL, don't touch the command.
5456 * @param fChangeCmdOnly Whether to only change the commands (sx-).
5457 */
5458static int dbgcEventApplyChanges(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PDBGFINTERRUPTCONFIG paIntCfgs, uint32_t cIntCfgs,
5459 PCDBGFEVENTCONFIG paEventCfgs, uint32_t cEventCfgs, const char *pszCmd, bool fChangeCmdOnly)
5460{
5461 int rc;
5462
5463 /*
5464 * Apply changes to DBGC. This can only fail with out of memory error.
5465 */
5466 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5467 if (cIntCfgs)
5468 for (uint32_t iCfg = 0; iCfg < cIntCfgs; iCfg++)
5469 {
5470 DBGCEVTSTATE enmEvtState = (DBGCEVTSTATE)(paIntCfgs[iCfg].enmHardState >> 4);
5471 paIntCfgs[iCfg].enmHardState &= 0xf;
5472 if (paIntCfgs[iCfg].enmHardState != DBGFINTERRUPTSTATE_DONT_TOUCH)
5473 {
5474 rc = dbgcEventUpdate(&pDbgc->apHardInts[paIntCfgs[iCfg].iInterrupt], pszCmd, enmEvtState, fChangeCmdOnly);
5475 if (RT_FAILURE(rc))
5476 return rc;
5477 }
5478
5479 enmEvtState = (DBGCEVTSTATE)(paIntCfgs[iCfg].enmSoftState >> 4);
5480 paIntCfgs[iCfg].enmSoftState &= 0xf;
5481 if (paIntCfgs[iCfg].enmSoftState != DBGFINTERRUPTSTATE_DONT_TOUCH)
5482 {
5483 rc = dbgcEventUpdate(&pDbgc->apSoftInts[paIntCfgs[iCfg].iInterrupt], pszCmd, enmEvtState, fChangeCmdOnly);
5484 if (RT_FAILURE(rc))
5485 return rc;
5486 }
5487 }
5488
5489 if (cEventCfgs)
5490 {
5491 for (uint32_t iCfg = 0; iCfg < cEventCfgs; iCfg++)
5492 {
5493 Assert((unsigned)paEventCfgs[iCfg].enmType < RT_ELEMENTS(pDbgc->apEventCfgs));
5494 uint16_t iSxEvt = RT_MAKE_U16(paEventCfgs[iCfg].abUnused[1], paEventCfgs[iCfg].abUnused[2]);
5495 Assert(iSxEvt < RT_ELEMENTS(g_aDbgcSxEvents));
5496 rc = dbgcEventUpdate(&pDbgc->apEventCfgs[iSxEvt], pszCmd, (DBGCEVTSTATE)paEventCfgs[iCfg].abUnused[0], fChangeCmdOnly);
5497 if (RT_FAILURE(rc))
5498 return rc;
5499 }
5500 }
5501
5502 /*
5503 * Apply changes to DBGF.
5504 */
5505 if (!fChangeCmdOnly)
5506 {
5507 if (cIntCfgs)
5508 {
5509 rc = DBGFR3InterruptConfigEx(pUVM, paIntCfgs, cIntCfgs);
5510 if (RT_FAILURE(rc))
5511 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3InterruptConfigEx: %Rrc\n", rc);
5512 }
5513 if (cEventCfgs)
5514 {
5515 rc = DBGFR3EventConfigEx(pUVM, paEventCfgs, cEventCfgs);
5516 if (RT_FAILURE(rc))
5517 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3EventConfigEx: %Rrc\n", rc);
5518 }
5519 }
5520
5521 return VINF_SUCCESS;
5522}
5523
5524
5525/**
5526 * @callback_method_impl{FNDBGCCMD, The 'sx[eni-]' commands.}
5527 */
5528static DECLCALLBACK(int) dbgcCmdEventCtrl(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5529{
5530 /*
5531 * Figure out which command this is.
5532 */
5533 uint8_t bIntOp;
5534 DBGCEVTSTATE enmEvtState;
5535 bool fChangeCmdOnly;
5536 switch (pCmd->pszCmd[2])
5537 {
5538 case 'e': bIntOp = DBGFINTERRUPTSTATE_ENABLED; enmEvtState = kDbgcEvtState_Enabled; fChangeCmdOnly = false; break;
5539 case 'n': bIntOp = DBGFINTERRUPTSTATE_ENABLED; enmEvtState = kDbgcEvtState_Notify; fChangeCmdOnly = false; break;
5540 case '-': bIntOp = DBGFINTERRUPTSTATE_ENABLED; enmEvtState = kDbgcEvtState_Invalid; fChangeCmdOnly = true; break;
5541 case 'i': bIntOp = DBGFINTERRUPTSTATE_DISABLED; enmEvtState = kDbgcEvtState_Disabled; fChangeCmdOnly = false; break;
5542 default:
5543 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "pszCmd=%s\n", pCmd->pszCmd);
5544 }
5545
5546 /*
5547 * Command option.
5548 */
5549 unsigned iArg = 0;
5550 const char *pszCmd = NULL;
5551 if ( cArgs >= iArg + 2
5552 && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING
5553 && paArgs[iArg + 1].enmType == DBGCVAR_TYPE_STRING
5554 && strcmp(paArgs[iArg].u.pszString, "-c") == 0)
5555 {
5556 pszCmd = paArgs[iArg + 1].u.pszString;
5557 iArg += 2;
5558 }
5559 if (fChangeCmdOnly && !pszCmd)
5560 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "The 'sx-' requires the '-c cmd' arguments.\n");
5561
5562 /*
5563 * The remaining arguments are event specifiers to which the operation should be applied.
5564 */
5565 uint32_t cIntCfgs = 0;
5566 DBGFINTERRUPTCONFIG aIntCfgs[256];
5567 uint32_t cEventCfgs = 0;
5568 DBGFEVENTCONFIG aEventCfgs[DBGFEVENT_END];
5569
5570 for (; iArg < cArgs; iArg++)
5571 {
5572 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, iArg, paArgs[iArg].enmType == DBGCVAR_TYPE_STRING
5573 || paArgs[iArg].enmType == DBGCVAR_TYPE_SYMBOL);
5574 uint32_t cHits = 0;
5575 for (uint32_t iEvt = 0; iEvt < RT_ELEMENTS(g_aDbgcSxEvents); iEvt++)
5576 if (g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Plain)
5577 {
5578 if ( RTStrSimplePatternMatch(paArgs[iArg].u.pszString, g_aDbgcSxEvents[iEvt].pszName)
5579 || ( g_aDbgcSxEvents[iEvt].pszAltNm
5580 && RTStrSimplePatternMatch(paArgs[iArg].u.pszString, g_aDbgcSxEvents[iEvt].pszAltNm)) )
5581 {
5582 cEventCfgs = dbgcEventAddPlainConfig(aEventCfgs, cEventCfgs, g_aDbgcSxEvents[iEvt].enmType,
5583 enmEvtState, iEvt);
5584 cHits++;
5585 }
5586 }
5587 else
5588 {
5589 Assert(g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Interrupt);
5590 uint8_t iInt;
5591 uint16_t cInts;
5592 if (dbgcEventIsMatchingInt(&paArgs[iArg], g_aDbgcSxEvents[iEvt].pszName, pCmdHlp, &iInt, &cInts))
5593 {
5594 cIntCfgs = dbgcEventAddIntConfig(aIntCfgs, cIntCfgs, iInt, cInts, g_aDbgcSxEvents[iEvt].pszName,
5595 enmEvtState, bIntOp);
5596 cHits++;
5597 }
5598 }
5599 if (!cHits)
5600 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown event: '%s'\n", paArgs[iArg].u.pszString);
5601 }
5602
5603 /*
5604 * Apply the changes.
5605 */
5606 return dbgcEventApplyChanges(pCmdHlp, pUVM, aIntCfgs, cIntCfgs, aEventCfgs, cEventCfgs, pszCmd, fChangeCmdOnly);
5607}
5608
5609
5610/**
5611 * @callback_method_impl{FNDBGCCMD, The 'sxr' commands.}
5612 */
5613static DECLCALLBACK(int) dbgcCmdEventCtrlReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5614{
5615 RT_NOREF1(pCmd);
5616 uint32_t cEventCfgs = 0;
5617 DBGFEVENTCONFIG aEventCfgs[DBGFEVENT_END];
5618 uint32_t cIntCfgs = 0;
5619 DBGFINTERRUPTCONFIG aIntCfgs[256];
5620
5621 if (cArgs == 0)
5622 {
5623 /*
5624 * All events.
5625 */
5626 for (uint32_t iInt = 0; iInt < 256; iInt++)
5627 {
5628 aIntCfgs[iInt].iInterrupt = iInt;
5629 aIntCfgs[iInt].enmHardState = DBGFINTERRUPTSTATE_DONT_TOUCH;
5630 aIntCfgs[iInt].enmSoftState = DBGFINTERRUPTSTATE_DONT_TOUCH;
5631 }
5632 cIntCfgs = 256;
5633
5634 for (uint32_t iEvt = 0; iEvt < RT_ELEMENTS(g_aDbgcSxEvents); iEvt++)
5635 if (g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Plain)
5636 {
5637 aEventCfgs[cEventCfgs].enmType = g_aDbgcSxEvents[iEvt].enmType;
5638 aEventCfgs[cEventCfgs].fEnabled = g_aDbgcSxEvents[iEvt].enmDefault > kDbgcEvtState_Disabled;
5639 aEventCfgs[cEventCfgs].abUnused[0] = g_aDbgcSxEvents[iEvt].enmDefault;
5640 aEventCfgs[cEventCfgs].abUnused[1] = (uint8_t)iEvt;
5641 aEventCfgs[cEventCfgs].abUnused[2] = (uint8_t)(iEvt >> 8);
5642 cEventCfgs++;
5643 }
5644 else
5645 {
5646 uint8_t const bState = ( g_aDbgcSxEvents[iEvt].enmDefault > kDbgcEvtState_Disabled
5647 ? DBGFINTERRUPTSTATE_ENABLED : DBGFINTERRUPTSTATE_DISABLED)
5648 | ((uint8_t)g_aDbgcSxEvents[iEvt].enmDefault << 4);
5649 if (strcmp(g_aDbgcSxEvents[iEvt].pszName, "hwint") == 0)
5650 for (uint32_t iInt = 0; iInt < 256; iInt++)
5651 aIntCfgs[iInt].enmHardState = bState;
5652 else
5653 for (uint32_t iInt = 0; iInt < 256; iInt++)
5654 aIntCfgs[iInt].enmSoftState = bState;
5655 }
5656 }
5657 else
5658 {
5659 /*
5660 * Selected events.
5661 */
5662 for (uint32_t iArg = 0; iArg < cArgs; iArg++)
5663 {
5664 unsigned cHits = 0;
5665 for (uint32_t iEvt = 0; iEvt < RT_ELEMENTS(g_aDbgcSxEvents); iEvt++)
5666 if (g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Plain)
5667 {
5668 if ( RTStrSimplePatternMatch(paArgs[iArg].u.pszString, g_aDbgcSxEvents[iEvt].pszName)
5669 || ( g_aDbgcSxEvents[iEvt].pszAltNm
5670 && RTStrSimplePatternMatch(paArgs[iArg].u.pszString, g_aDbgcSxEvents[iEvt].pszAltNm)) )
5671 {
5672 cEventCfgs = dbgcEventAddPlainConfig(aEventCfgs, cEventCfgs, g_aDbgcSxEvents[iEvt].enmType,
5673 g_aDbgcSxEvents[iEvt].enmDefault, iEvt);
5674 cHits++;
5675 }
5676 }
5677 else
5678 {
5679 Assert(g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Interrupt);
5680 uint8_t iInt;
5681 uint16_t cInts;
5682 if (dbgcEventIsMatchingInt(&paArgs[iArg], g_aDbgcSxEvents[iEvt].pszName, pCmdHlp, &iInt, &cInts))
5683 {
5684 cIntCfgs = dbgcEventAddIntConfig(aIntCfgs, cIntCfgs, iInt, cInts, g_aDbgcSxEvents[iEvt].pszName,
5685 g_aDbgcSxEvents[iEvt].enmDefault,
5686 g_aDbgcSxEvents[iEvt].enmDefault > kDbgcEvtState_Disabled
5687 ? DBGFINTERRUPTSTATE_ENABLED : DBGFINTERRUPTSTATE_DISABLED);
5688 cHits++;
5689 }
5690 }
5691 if (!cHits)
5692 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown event: '%s'\n", paArgs[iArg].u.pszString);
5693 }
5694 }
5695
5696 /*
5697 * Apply the reset changes.
5698 */
5699 return dbgcEventApplyChanges(pCmdHlp, pUVM, aIntCfgs, cIntCfgs, aEventCfgs, cEventCfgs, "", false);
5700}
5701
5702
5703/**
5704 * Used during DBGC initialization to configure events with defaults.
5705 *
5706 * @returns VBox status code.
5707 * @param pDbgc The DBGC instance.
5708 */
5709void dbgcEventInit(PDBGC pDbgc)
5710{
5711 if (pDbgc->pUVM)
5712 dbgcCmdEventCtrlReset(NULL, &pDbgc->CmdHlp, pDbgc->pUVM, NULL, 0);
5713}
5714
5715
5716/**
5717 * Used during DBGC termination to disable all events.
5718 *
5719 * @param pDbgc The DBGC instance.
5720 */
5721void dbgcEventTerm(PDBGC pDbgc)
5722{
5723/** @todo need to do more than just reset later. */
5724 if (pDbgc->pUVM && VMR3GetStateU(pDbgc->pUVM) < VMSTATE_DESTROYING)
5725 dbgcCmdEventCtrlReset(NULL, &pDbgc->CmdHlp, pDbgc->pUVM, NULL, 0);
5726}
5727
5728
5729static void dbgcEventDisplay(PDBGCCMDHLP pCmdHlp, const char *pszName, DBGCEVTSTATE enmDefault, PDBGCEVTCFG const *ppEvtCfg)
5730{
5731 RT_NOREF1(enmDefault);
5732 PDBGCEVTCFG pEvtCfg = *ppEvtCfg;
5733
5734 const char *pszState;
5735 switch (pEvtCfg ? pEvtCfg->enmState : kDbgcEvtState_Disabled)
5736 {
5737 case kDbgcEvtState_Disabled: pszState = "ignore"; break;
5738 case kDbgcEvtState_Enabled: pszState = "enabled"; break;
5739 case kDbgcEvtState_Notify: pszState = "notify"; break;
5740 default:
5741 AssertFailed();
5742 pszState = "invalid";
5743 break;
5744 }
5745
5746 if (pEvtCfg && pEvtCfg->cchCmd > 0)
5747 DBGCCmdHlpPrintf(pCmdHlp, "%-22s %-7s \"%s\"\n", pszName, pszState, pEvtCfg->szCmd);
5748 else
5749 DBGCCmdHlpPrintf(pCmdHlp, "%-22s %s\n", pszName, pszState);
5750}
5751
5752
5753static void dbgcEventDisplayRange(PDBGCCMDHLP pCmdHlp, const char *pszBaseNm, DBGCEVTSTATE enmDefault,
5754 PDBGCEVTCFG const *papEvtCfgs, unsigned iCfg, unsigned cCfgs)
5755{
5756 do
5757 {
5758 PCDBGCEVTCFG pFirstCfg = papEvtCfgs[iCfg];
5759 if (pFirstCfg && pFirstCfg->enmState == kDbgcEvtState_Disabled && pFirstCfg->cchCmd == 0)
5760 pFirstCfg = NULL;
5761
5762 unsigned const iFirstCfg = iCfg;
5763 iCfg++;
5764 while (iCfg < cCfgs)
5765 {
5766 PCDBGCEVTCFG pCurCfg = papEvtCfgs[iCfg];
5767 if (pCurCfg && pCurCfg->enmState == kDbgcEvtState_Disabled && pCurCfg->cchCmd == 0)
5768 pCurCfg = NULL;
5769 if (pCurCfg != pFirstCfg)
5770 {
5771 if (!pCurCfg || !pFirstCfg)
5772 break;
5773 if (pCurCfg->enmState != pFirstCfg->enmState)
5774 break;
5775 if (pCurCfg->cchCmd != pFirstCfg->cchCmd)
5776 break;
5777 if (memcmp(pCurCfg->szCmd, pFirstCfg->szCmd, pFirstCfg->cchCmd) != 0)
5778 break;
5779 }
5780 iCfg++;
5781 }
5782
5783 char szName[16];
5784 unsigned cEntries = iCfg - iFirstCfg;
5785 if (cEntries == 1)
5786 RTStrPrintf(szName, sizeof(szName), "%s%02x", pszBaseNm, iFirstCfg);
5787 else
5788 RTStrPrintf(szName, sizeof(szName), "%s%02x L %#x", pszBaseNm, iFirstCfg, cEntries);
5789 dbgcEventDisplay(pCmdHlp, szName, enmDefault, &papEvtCfgs[iFirstCfg]);
5790
5791 cCfgs -= cEntries;
5792 } while (cCfgs > 0);
5793}
5794
5795
5796/**
5797 * @callback_method_impl{FNDBGCCMD, The 'sx' commands.}
5798 */
5799static DECLCALLBACK(int) dbgcCmdEventCtrlList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5800{
5801 RT_NOREF2(pCmd, pUVM);
5802 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5803
5804 if (cArgs == 0)
5805 {
5806 /*
5807 * All events.
5808 */
5809 for (uint32_t iEvt = 0; iEvt < RT_ELEMENTS(g_aDbgcSxEvents); iEvt++)
5810 if (g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Plain)
5811 dbgcEventDisplay(pCmdHlp, g_aDbgcSxEvents[iEvt].pszName, g_aDbgcSxEvents[iEvt].enmDefault,
5812 &pDbgc->apEventCfgs[iEvt]);
5813 else if (strcmp(g_aDbgcSxEvents[iEvt].pszName, "hwint") == 0)
5814 dbgcEventDisplayRange(pCmdHlp, g_aDbgcSxEvents[iEvt].pszName, g_aDbgcSxEvents[iEvt].enmDefault,
5815 pDbgc->apHardInts, 0, 256);
5816 else
5817 dbgcEventDisplayRange(pCmdHlp, g_aDbgcSxEvents[iEvt].pszName, g_aDbgcSxEvents[iEvt].enmDefault,
5818 pDbgc->apSoftInts, 0, 256);
5819 }
5820 else
5821 {
5822 /*
5823 * Selected events.
5824 */
5825 for (uint32_t iArg = 0; iArg < cArgs; iArg++)
5826 {
5827 unsigned cHits = 0;
5828 for (uint32_t iEvt = 0; iEvt < RT_ELEMENTS(g_aDbgcSxEvents); iEvt++)
5829 if (g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Plain)
5830 {
5831 if ( RTStrSimplePatternMatch(paArgs[iArg].u.pszString, g_aDbgcSxEvents[iEvt].pszName)
5832 || ( g_aDbgcSxEvents[iEvt].pszAltNm
5833 && RTStrSimplePatternMatch(paArgs[iArg].u.pszString, g_aDbgcSxEvents[iEvt].pszAltNm)) )
5834 {
5835 dbgcEventDisplay(pCmdHlp, g_aDbgcSxEvents[iEvt].pszName, g_aDbgcSxEvents[iEvt].enmDefault,
5836 &pDbgc->apEventCfgs[iEvt]);
5837 cHits++;
5838 }
5839 }
5840 else
5841 {
5842 Assert(g_aDbgcSxEvents[iEvt].enmKind == kDbgcSxEventKind_Interrupt);
5843 uint8_t iInt;
5844 uint16_t cInts;
5845 if (dbgcEventIsMatchingInt(&paArgs[iArg], g_aDbgcSxEvents[iEvt].pszName, pCmdHlp, &iInt, &cInts))
5846 {
5847 if (strcmp(g_aDbgcSxEvents[iEvt].pszName, "hwint") == 0)
5848 dbgcEventDisplayRange(pCmdHlp, g_aDbgcSxEvents[iEvt].pszName, g_aDbgcSxEvents[iEvt].enmDefault,
5849 pDbgc->apHardInts, iInt, cInts);
5850 else
5851 dbgcEventDisplayRange(pCmdHlp, g_aDbgcSxEvents[iEvt].pszName, g_aDbgcSxEvents[iEvt].enmDefault,
5852 pDbgc->apSoftInts, iInt, cInts);
5853 cHits++;
5854 }
5855 }
5856 if (cHits == 0)
5857 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown event: '%s'\n", paArgs[iArg].u.pszString);
5858 }
5859 }
5860
5861 return VINF_SUCCESS;
5862}
5863
5864
5865
5866/**
5867 * List near symbol.
5868 *
5869 * @returns VBox status code.
5870 * @param pCmdHlp Pointer to command helper functions.
5871 * @param pUVM The user mode VM handle.
5872 * @param pArg Pointer to the address or symbol to lookup.
5873 */
5874static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pArg)
5875{
5876 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5877
5878 RTDBGSYMBOL Symbol;
5879 int rc;
5880 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
5881 {
5882 /*
5883 * Lookup the symbol address.
5884 */
5885 rc = DBGFR3AsSymbolByName(pUVM, pDbgc->hDbgAs, pArg->u.pszString, &Symbol, NULL);
5886 if (RT_FAILURE(rc))
5887 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3AsSymbolByName(,,%s,)\n", pArg->u.pszString);
5888
5889 rc = DBGCCmdHlpPrintf(pCmdHlp, "%RTptr %s\n", Symbol.Value, Symbol.szName);
5890 }
5891 else
5892 {
5893 /*
5894 * Convert it to a flat GC address and lookup that address.
5895 */
5896 DBGCVAR AddrVar;
5897 rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
5898 if (RT_FAILURE(rc))
5899 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
5900
5901 RTINTPTR offDisp;
5902 DBGFADDRESS Addr;
5903 rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, AddrVar.u.GCFlat),
5904 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDisp, &Symbol, NULL);
5905 if (RT_FAILURE(rc))
5906 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3ASymbolByAddr(,,%RGv,,)\n", AddrVar.u.GCFlat);
5907
5908 if (!offDisp)
5909 rc = DBGCCmdHlpPrintf(pCmdHlp, "%DV %s", &AddrVar, Symbol.szName);
5910 else if (offDisp > 0)
5911 rc = DBGCCmdHlpPrintf(pCmdHlp, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
5912 else
5913 rc = DBGCCmdHlpPrintf(pCmdHlp, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
5914 if (Symbol.cb > 0)
5915 rc = DBGCCmdHlpPrintf(pCmdHlp, " (LB %RGv)\n", Symbol.cb);
5916 else
5917 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
5918 }
5919
5920 return rc;
5921}
5922
5923
5924/**
5925 * @callback_method_impl{FNDBGCCMD, The 'ln' (listnear) command.}
5926 */
5927static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5928{
5929 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5930 if (!cArgs)
5931 {
5932 /*
5933 * Current cs:eip symbol.
5934 */
5935 DBGCVAR AddrVar;
5936 const char *pszFmtExpr = pDbgc->fRegCtxGuest ? "%%(cs:eip)" : "%%(.cs:.eip)";
5937 int rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, pszFmtExpr);
5938 if (RT_FAILURE(rc))
5939 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%s\n", pszFmtExpr + 1);
5940 return dbgcDoListNear(pCmdHlp, pUVM, &AddrVar);
5941 }
5942
5943/** @todo Fix the darn parser, it's resolving symbols specified as arguments before we get in here. */
5944 /*
5945 * Iterate arguments.
5946 */
5947 for (unsigned iArg = 0; iArg < cArgs; iArg++)
5948 {
5949 int rc = dbgcDoListNear(pCmdHlp, pUVM, &paArgs[iArg]);
5950 if (RT_FAILURE(rc))
5951 return rc;
5952 }
5953
5954 NOREF(pCmd);
5955 return VINF_SUCCESS;
5956}
5957
5958
5959/**
5960 * Matches the module patters against a module name.
5961 *
5962 * @returns true if matching, otherwise false.
5963 * @param pszName The module name.
5964 * @param paArgs The module pattern argument list.
5965 * @param cArgs Number of arguments.
5966 */
5967static bool dbgcCmdListModuleMatch(const char *pszName, PCDBGCVAR paArgs, unsigned cArgs)
5968{
5969 for (uint32_t i = 0; i < cArgs; i++)
5970 if (RTStrSimplePatternMatch(paArgs[i].u.pszString, pszName))
5971 return true;
5972 return false;
5973}
5974
5975
5976/**
5977 * @callback_method_impl{FNDBGCCMD, The 'ln' (list near) command.}
5978 */
5979static DECLCALLBACK(int) dbgcCmdListModules(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
5980{
5981 bool const fMappings = pCmd->pszCmd[2] == 'o';
5982 bool const fVerbose = pCmd->pszCmd[strlen(pCmd->pszCmd) - 1] == 'v';
5983 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5984
5985 /*
5986 * Iterate the modules in the current address space and print info about
5987 * those matching the input.
5988 */
5989 RTDBGAS hAsCurAlias = pDbgc->hDbgAs;
5990 for (uint32_t iAs = 0;; iAs++)
5991 {
5992 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, hAsCurAlias);
5993 uint32_t cMods = RTDbgAsModuleCount(hAs);
5994 for (uint32_t iMod = 0; iMod < cMods; iMod++)
5995 {
5996 RTDBGMOD hMod = RTDbgAsModuleByIndex(hAs, iMod);
5997 if (hMod != NIL_RTDBGMOD)
5998 {
5999 bool const fDeferred = RTDbgModIsDeferred(hMod);
6000 bool const fExports = RTDbgModIsExports(hMod);
6001 uint32_t const cSegs = fDeferred ? 1 : RTDbgModSegmentCount(hMod);
6002 const char * const pszName = RTDbgModName(hMod);
6003 const char * const pszImgFile = RTDbgModImageFile(hMod);
6004 const char * const pszImgFileUsed = RTDbgModImageFileUsed(hMod);
6005 const char * const pszDbgFile = RTDbgModDebugFile(hMod);
6006 if ( cArgs == 0
6007 || dbgcCmdListModuleMatch(pszName, paArgs, cArgs))
6008 {
6009 /*
6010 * Find the mapping with the lower address, preferring a full
6011 * image mapping, for the main line.
6012 */
6013 RTDBGASMAPINFO aMappings[128];
6014 uint32_t cMappings = RT_ELEMENTS(aMappings);
6015 int rc = RTDbgAsModuleQueryMapByIndex(hAs, iMod, &aMappings[0], &cMappings, 0 /*fFlags*/);
6016 if (RT_SUCCESS(rc))
6017 {
6018 bool fFull = false;
6019 RTUINTPTR uMin = RTUINTPTR_MAX;
6020 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
6021 if ( aMappings[iMap].Address < uMin
6022 && ( !fFull
6023 || aMappings[iMap].iSeg == NIL_RTDBGSEGIDX))
6024 uMin = aMappings[iMap].Address;
6025 if (!fVerbose || !pszImgFile)
6026 DBGCCmdHlpPrintf(pCmdHlp, "%RGv %04x %s%s\n", (RTGCUINTPTR)uMin, cSegs, pszName,
6027 fExports ? " (exports)" : fDeferred ? " (deferred)" : "");
6028 else
6029 DBGCCmdHlpPrintf(pCmdHlp, "%RGv %04x %-12s %s%s\n", (RTGCUINTPTR)uMin, cSegs, pszName, pszImgFile,
6030 fExports ? " (exports)" : fDeferred ? " (deferred)" : "");
6031 if (fVerbose && pszImgFileUsed)
6032 DBGCCmdHlpPrintf(pCmdHlp, " Local image: %s\n", pszImgFileUsed);
6033 if (fVerbose && pszDbgFile)
6034 DBGCCmdHlpPrintf(pCmdHlp, " Debug file: %s\n", pszDbgFile);
6035
6036 if (fMappings)
6037 {
6038 /* sort by address first - not very efficient. */
6039 for (uint32_t i = 0; i + 1 < cMappings; i++)
6040 for (uint32_t j = i + 1; j < cMappings; j++)
6041 if (aMappings[j].Address < aMappings[i].Address)
6042 {
6043 RTDBGASMAPINFO Tmp = aMappings[j];
6044 aMappings[j] = aMappings[i];
6045 aMappings[i] = Tmp;
6046 }
6047
6048 /* print */
6049 if ( cMappings == 1
6050 && aMappings[0].iSeg == NIL_RTDBGSEGIDX
6051 && !fDeferred)
6052 {
6053 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
6054 {
6055 RTDBGSEGMENT SegInfo;
6056 rc = RTDbgModSegmentByIndex(hMod, iSeg, &SegInfo);
6057 if (RT_SUCCESS(rc))
6058 {
6059 if (SegInfo.uRva != RTUINTPTR_MAX)
6060 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv #%02x %s\n",
6061 (RTGCUINTPTR)(aMappings[0].Address + SegInfo.uRva),
6062 (RTGCUINTPTR)SegInfo.cb, iSeg, SegInfo.szName);
6063 else
6064 DBGCCmdHlpPrintf(pCmdHlp, " %*s %RGv #%02x %s\n",
6065 sizeof(RTGCUINTPTR)*2, "noload",
6066 (RTGCUINTPTR)SegInfo.cb, iSeg, SegInfo.szName);
6067 }
6068 else
6069 DBGCCmdHlpPrintf(pCmdHlp, " Error query segment #%u: %Rrc\n", iSeg, rc);
6070 }
6071 }
6072 else
6073 {
6074 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
6075 if (aMappings[iMap].iSeg == NIL_RTDBGSEGIDX)
6076 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv <everything>\n",
6077 (RTGCUINTPTR)aMappings[iMap].Address,
6078 (RTGCUINTPTR)RTDbgModImageSize(hMod));
6079 else if (!fDeferred)
6080 {
6081 RTDBGSEGMENT SegInfo;
6082 rc = RTDbgModSegmentByIndex(hMod, aMappings[iMap].iSeg, &SegInfo);
6083 if (RT_FAILURE(rc))
6084 {
6085 RT_ZERO(SegInfo);
6086 strcpy(SegInfo.szName, "error");
6087 }
6088 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv #%02x %s\n",
6089 (RTGCUINTPTR)aMappings[iMap].Address,
6090 (RTGCUINTPTR)SegInfo.cb,
6091 aMappings[iMap].iSeg, SegInfo.szName);
6092 }
6093 else
6094 DBGCCmdHlpPrintf(pCmdHlp, " %RGv #%02x\n",
6095 (RTGCUINTPTR)aMappings[iMap].Address, aMappings[iMap].iSeg);
6096 }
6097 }
6098 }
6099 else
6100 DBGCCmdHlpPrintf(pCmdHlp, "%.*s %04x %s (rc=%Rrc)\n",
6101 sizeof(RTGCPTR) * 2, "???????????", cSegs, pszName, rc);
6102 /** @todo missing address space API for enumerating the mappings. */
6103 }
6104 RTDbgModRelease(hMod);
6105 }
6106 }
6107 RTDbgAsRelease(hAs);
6108
6109 /* For DBGF_AS_RC_AND_GC_GLOBAL we're required to do more work. */
6110 if (hAsCurAlias != DBGF_AS_RC_AND_GC_GLOBAL)
6111 break;
6112 AssertBreak(iAs == 0);
6113 hAsCurAlias = DBGF_AS_GLOBAL;
6114 }
6115
6116 NOREF(pCmd);
6117 return VINF_SUCCESS;
6118}
6119
6120
6121
6122/**
6123 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 8-bit value.}
6124 */
6125static DECLCALLBACK(int) dbgcFuncReadU8(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6126 PDBGCVAR pResult)
6127{
6128 RT_NOREF1(pUVM);
6129 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6130 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
6131 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
6132
6133 uint8_t b;
6134 int rc = DBGCCmdHlpMemRead(pCmdHlp, &b, sizeof(b), &paArgs[0], NULL);
6135 if (RT_FAILURE(rc))
6136 return rc;
6137 DBGCVAR_INIT_NUMBER(pResult, b);
6138
6139 NOREF(pFunc);
6140 return VINF_SUCCESS;
6141}
6142
6143
6144/**
6145 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 16-bit value.}
6146 */
6147static DECLCALLBACK(int) dbgcFuncReadU16(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6148 PDBGCVAR pResult)
6149{
6150 RT_NOREF1(pUVM);
6151 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6152 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
6153 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
6154
6155 uint16_t u16;
6156 int rc = DBGCCmdHlpMemRead(pCmdHlp, &u16, sizeof(u16), &paArgs[0], NULL);
6157 if (RT_FAILURE(rc))
6158 return rc;
6159 DBGCVAR_INIT_NUMBER(pResult, u16);
6160
6161 NOREF(pFunc);
6162 return VINF_SUCCESS;
6163}
6164
6165
6166/**
6167 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 32-bit value.}
6168 */
6169static DECLCALLBACK(int) dbgcFuncReadU32(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6170 PDBGCVAR pResult)
6171{
6172 RT_NOREF1(pUVM);
6173 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6174 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
6175 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
6176
6177 uint32_t u32;
6178 int rc = DBGCCmdHlpMemRead(pCmdHlp, &u32, sizeof(u32), &paArgs[0], NULL);
6179 if (RT_FAILURE(rc))
6180 return rc;
6181 DBGCVAR_INIT_NUMBER(pResult, u32);
6182
6183 NOREF(pFunc);
6184 return VINF_SUCCESS;
6185}
6186
6187
6188/**
6189 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 64-bit value.}
6190 */
6191static DECLCALLBACK(int) dbgcFuncReadU64(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6192 PDBGCVAR pResult)
6193{
6194 RT_NOREF1(pUVM);
6195 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6196 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
6197 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
6198
6199 uint64_t u64;
6200 int rc = DBGCCmdHlpMemRead(pCmdHlp, &u64, sizeof(u64), &paArgs[0], NULL);
6201 if (RT_FAILURE(rc))
6202 return rc;
6203 DBGCVAR_INIT_NUMBER(pResult, u64);
6204
6205 NOREF(pFunc);
6206 return VINF_SUCCESS;
6207}
6208
6209
6210/**
6211 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned pointer-sized value.}
6212 */
6213static DECLCALLBACK(int) dbgcFuncReadPtr(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6214 PDBGCVAR pResult)
6215{
6216 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6217 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
6218 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
6219
6220 CPUMMODE enmMode = DBGCCmdHlpGetCpuMode(pCmdHlp);
6221 if (enmMode == CPUMMODE_LONG)
6222 return dbgcFuncReadU64(pFunc, pCmdHlp, pUVM, paArgs, cArgs, pResult);
6223 return dbgcFuncReadU32(pFunc, pCmdHlp, pUVM, paArgs, cArgs, pResult);
6224}
6225
6226
6227/**
6228 * @callback_method_impl{FNDBGCFUNC, The hi(value) function implementation.}
6229 */
6230static DECLCALLBACK(int) dbgcFuncHi(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6231 PDBGCVAR pResult)
6232{
6233 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6234
6235 uint16_t uHi;
6236 switch (paArgs[0].enmType)
6237 {
6238 case DBGCVAR_TYPE_GC_FLAT: uHi = (uint16_t)(paArgs[0].u.GCFlat >> 16); break;
6239 case DBGCVAR_TYPE_GC_FAR: uHi = (uint16_t)paArgs[0].u.GCFar.sel; break;
6240 case DBGCVAR_TYPE_GC_PHYS: uHi = (uint16_t)(paArgs[0].u.GCPhys >> 16); break;
6241 case DBGCVAR_TYPE_HC_FLAT: uHi = (uint16_t)((uintptr_t)paArgs[0].u.pvHCFlat >> 16); break;
6242 case DBGCVAR_TYPE_HC_PHYS: uHi = (uint16_t)(paArgs[0].u.HCPhys >> 16); break;
6243 case DBGCVAR_TYPE_NUMBER: uHi = (uint16_t)(paArgs[0].u.u64Number >> 16); break;
6244 default:
6245 AssertFailedReturn(VERR_DBGC_PARSE_BUG);
6246 }
6247 DBGCVAR_INIT_NUMBER(pResult, uHi);
6248 DBGCVAR_SET_RANGE(pResult, paArgs[0].enmRangeType, paArgs[0].u64Range);
6249
6250 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pUVM);
6251 return VINF_SUCCESS;
6252}
6253
6254
6255/**
6256 * @callback_method_impl{FNDBGCFUNC, The low(value) function implementation.}
6257 */
6258static DECLCALLBACK(int) dbgcFuncLow(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6259 PDBGCVAR pResult)
6260{
6261 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6262
6263 uint16_t uLow;
6264 switch (paArgs[0].enmType)
6265 {
6266 case DBGCVAR_TYPE_GC_FLAT: uLow = (uint16_t)paArgs[0].u.GCFlat; break;
6267 case DBGCVAR_TYPE_GC_FAR: uLow = (uint16_t)paArgs[0].u.GCFar.off; break;
6268 case DBGCVAR_TYPE_GC_PHYS: uLow = (uint16_t)paArgs[0].u.GCPhys; break;
6269 case DBGCVAR_TYPE_HC_FLAT: uLow = (uint16_t)(uintptr_t)paArgs[0].u.pvHCFlat; break;
6270 case DBGCVAR_TYPE_HC_PHYS: uLow = (uint16_t)paArgs[0].u.HCPhys; break;
6271 case DBGCVAR_TYPE_NUMBER: uLow = (uint16_t)paArgs[0].u.u64Number; break;
6272 default:
6273 AssertFailedReturn(VERR_DBGC_PARSE_BUG);
6274 }
6275 DBGCVAR_INIT_NUMBER(pResult, uLow);
6276 DBGCVAR_SET_RANGE(pResult, paArgs[0].enmRangeType, paArgs[0].u64Range);
6277
6278 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pUVM);
6279 return VINF_SUCCESS;
6280}
6281
6282
6283/**
6284 * @callback_method_impl{FNDBGCFUNC,The low(value) function implementation.}
6285 */
6286static DECLCALLBACK(int) dbgcFuncNot(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
6287 PDBGCVAR pResult)
6288{
6289 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
6290 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pUVM);
6291 return DBGCCmdHlpEval(pCmdHlp, pResult, "!(%Dv)", &paArgs[0]);
6292}
6293
6294
6295/** Generic pointer argument wo/ range. */
6296static const DBGCVARDESC g_aArgPointerWoRange[] =
6297{
6298 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
6299 { 1, 1, DBGCVAR_CAT_POINTER_NO_RANGE, 0, "value", "Address or number." },
6300};
6301
6302/** Generic pointer or number argument. */
6303static const DBGCVARDESC g_aArgPointerNumber[] =
6304{
6305 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
6306 { 1, 1, DBGCVAR_CAT_POINTER_NUMBER, 0, "value", "Address or number." },
6307};
6308
6309
6310
6311/** Function descriptors for the CodeView / WinDbg emulation.
6312 * The emulation isn't attempting to be identical, only somewhat similar.
6313 */
6314const DBGCFUNC g_aFuncsCodeView[] =
6315{
6316 { "by", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU8, "address", "Reads a byte at the given address." },
6317 { "dwo", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU32, "address", "Reads a 32-bit value at the given address." },
6318 { "hi", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncHi, "value", "Returns the high 16-bit bits of a value." },
6319 { "low", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncLow, "value", "Returns the low 16-bit bits of a value." },
6320 { "not", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncNot, "address", "Boolean NOT." },
6321 { "poi", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadPtr, "address", "Reads a pointer sized (CS) value at the given address." },
6322 { "qwo", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU64, "address", "Reads a 32-bit value at the given address." },
6323 { "wo", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU16, "address", "Reads a 16-bit value at the given address." },
6324};
6325
6326/** The number of functions in the CodeView/WinDbg emulation. */
6327const uint32_t g_cFuncsCodeView = RT_ELEMENTS(g_aFuncsCodeView);
6328
Note: See TracBrowser for help on using the repository browser.

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