VirtualBox

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

Last change on this file since 73370 was 73360, checked in by vboxsync, 7 years ago

VMM,REM,DBGC: Use RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED.

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