VirtualBox

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

Last change on this file since 87162 was 86755, checked in by vboxsync, 4 years ago

VMM/DBGF,Debugger: Implement changes for the new breakpoint manager, bugref:9837

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