VirtualBox

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

Last change on this file since 82894 was 80014, checked in by vboxsync, 5 years ago

VMM: Kicking out raw-mode (work in progress). bugref:9517

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