VirtualBox

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

Last change on this file since 85416 was 83082, checked in by vboxsync, 5 years ago

DBGC/lm: Dump the UUID and timestamp of images in verbose mode.

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