VirtualBox

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

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

Debugger/DBGCEmulateCodeView.cpp: Add x command to examine symbols

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 299.2 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 78017 2019-04-04 13:36:49Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, CodeView / WinDbg Emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGC
23#include <VBox/dbg.h>
24#include <VBox/vmm/dbgf.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/cpum.h>
27#include <VBox/dis.h>
28#include <VBox/param.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31
32#include <iprt/asm.h>
33#include <iprt/mem.h>
34#include <iprt/string.h>
35#include <iprt/assert.h>
36#include <iprt/ctype.h>
37
38#include <stdlib.h>
39#include <stdio.h>
40
41#include "DBGCInternal.h"
42
43
44/*********************************************************************************************************************************
45* Internal Functions *
46*********************************************************************************************************************************/
47static FNDBGCCMD dbgcCmdBrkAccess;
48static FNDBGCCMD dbgcCmdBrkClear;
49static FNDBGCCMD dbgcCmdBrkDisable;
50static FNDBGCCMD dbgcCmdBrkEnable;
51static FNDBGCCMD dbgcCmdBrkList;
52static FNDBGCCMD dbgcCmdBrkSet;
53static FNDBGCCMD dbgcCmdBrkREM;
54static FNDBGCCMD dbgcCmdDumpMem;
55static FNDBGCCMD dbgcCmdDumpDT;
56static FNDBGCCMD dbgcCmdDumpIDT;
57static FNDBGCCMD dbgcCmdDumpPageDir;
58static FNDBGCCMD dbgcCmdDumpPageDirBoth;
59static FNDBGCCMD dbgcCmdDumpPageHierarchy;
60static FNDBGCCMD dbgcCmdDumpPageTable;
61static FNDBGCCMD dbgcCmdDumpPageTableBoth;
62static FNDBGCCMD dbgcCmdDumpTSS;
63static FNDBGCCMD dbgcCmdDumpTypeInfo;
64static FNDBGCCMD dbgcCmdDumpTypedVal;
65static FNDBGCCMD dbgcCmdEditMem;
66static FNDBGCCMD dbgcCmdGo;
67static FNDBGCCMD dbgcCmdGoUp;
68static FNDBGCCMD dbgcCmdListModules;
69static FNDBGCCMD dbgcCmdListNear;
70static FNDBGCCMD dbgcCmdListSource;
71static FNDBGCCMD dbgcCmdListSymbols;
72static FNDBGCCMD dbgcCmdMemoryInfo;
73static FNDBGCCMD dbgcCmdReg;
74static FNDBGCCMD dbgcCmdRegGuest;
75static FNDBGCCMD dbgcCmdRegHyper;
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 { "rh", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegHyper, "[reg [[=] newval]]", "Show or set register(s) - hypervisor reg set." },
451 { "rt", 0, 0, NULL, 0, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
452 { "s", 0, ~0U, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
453 { "sa", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
454 { "sb", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
455 { "sd", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
456 { "sq", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
457 { "su", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
458 { "sw", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more words." },
459 { "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." },
460 { "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." },
461 { "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." },
462 { "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." },
463 { "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." },
464 { "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." },
465 { "t", 0, 2, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Trace ." },
466 { "tr", 0, 0, NULL, 0, 0, dbgcCmdStepTraceToggle, "", "Toggle displaying registers for tracing & stepping (no code executed)." },
467 { "ta", 1, 1, &g_aArgStepTraceTo[0], RT_ELEMENTS(g_aArgStepTraceTo), 0, dbgcCmdStepTraceTo, "<addr> [count] [cmds]","Trace to the given address." },
468 { "tc", 0, 0, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Trace to the next call instruction." },
469 { "tt", 0, 0, &g_aArgStepTrace[0], RT_ELEMENTS(g_aArgStepTrace), 0, dbgcCmdStepTrace, "[count] [cmds]", "Trace to the next return instruction." },
470 { "u", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble." },
471 { "u64", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 64-bit code." },
472 { "u32", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 32-bit code." },
473 { "u16", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code." },
474 { "uv86", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code with v8086/real mode addressing." },
475 { "ucfg", 0, 1, &g_aArgUnassembleCfg[0], RT_ELEMENTS(g_aArgUnassembleCfg), 0, dbgcCmdUnassembleCfg, "[addr]", "Unassemble creating a control flow graph." },
476 { "ucfgc", 0, 1, &g_aArgUnassembleCfg[0], RT_ELEMENTS(g_aArgUnassembleCfg), 0, dbgcCmdUnassembleCfg, "[addr]", "Unassemble creating a control flow graph with colors." },
477 { "x", 1, 1, &g_aArgListSyms[0], RT_ELEMENTS(g_aArgListSyms), 0, dbgcCmdListSymbols, "* | <Module!Symbol>", "Examine symbols." },
478};
479
480/** The number of commands in the CodeView/WinDbg emulation. */
481const uint32_t g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
482
483
484/**
485 * Selectable debug event descriptors.
486 *
487 * @remarks Sorted by DBGCSXEVT::enmType value.
488 */
489const DBGCSXEVT g_aDbgcSxEvents[] =
490{
491 { DBGFEVENT_INTERRUPT_HARDWARE, "hwint", NULL, kDbgcSxEventKind_Interrupt, kDbgcEvtState_Disabled, 0, "Hardware interrupt" },
492 { DBGFEVENT_INTERRUPT_SOFTWARE, "swint", NULL, kDbgcSxEventKind_Interrupt, kDbgcEvtState_Disabled, 0, "Software interrupt" },
493 { DBGFEVENT_TRIPLE_FAULT, "triplefault", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Enabled, 0, "Triple fault "},
494 { DBGFEVENT_XCPT_DE, "xcpt_de", "de", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DE (integer divide error)" },
495 { DBGFEVENT_XCPT_DB, "xcpt_db", "db", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DB (debug)" },
496 { DBGFEVENT_XCPT_02, "xcpt_02", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
497 { DBGFEVENT_XCPT_BP, "xcpt_bp", "bp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#BP (breakpoint)" },
498 { DBGFEVENT_XCPT_OF, "xcpt_of", "of", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#OF (overflow (INTO))" },
499 { DBGFEVENT_XCPT_BR, "xcpt_br", "br", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#BR (bound range exceeded)" },
500 { DBGFEVENT_XCPT_UD, "xcpt_ud", "ud", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#UD (undefined opcode)" },
501 { DBGFEVENT_XCPT_NM, "xcpt_nm", "nm", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#NM (FPU not available)" },
502 { DBGFEVENT_XCPT_DF, "xcpt_df", "df", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DF (double fault)" },
503 { DBGFEVENT_XCPT_09, "xcpt_09", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "Coprocessor segment overrun" },
504 { DBGFEVENT_XCPT_TS, "xcpt_ts", "ts", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#TS (task switch)" },
505 { DBGFEVENT_XCPT_NP, "xcpt_np", "np", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#NP (segment not present)" },
506 { DBGFEVENT_XCPT_SS, "xcpt_ss", "ss", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#SS (stack segment fault)" },
507 { DBGFEVENT_XCPT_GP, "xcpt_gp", "gp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#GP (general protection fault)" },
508 { DBGFEVENT_XCPT_PF, "xcpt_pf", "pf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#PF (page fault)" },
509 { DBGFEVENT_XCPT_0f, "xcpt_0f", "xcpt0f", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
510 { DBGFEVENT_XCPT_MF, "xcpt_mf", "mf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#MF (math fault)" },
511 { DBGFEVENT_XCPT_AC, "xcpt_ac", "ac", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#AC (alignment check)" },
512 { DBGFEVENT_XCPT_MC, "xcpt_mc", "mc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#MC (machine check)" },
513 { DBGFEVENT_XCPT_XF, "xcpt_xf", "xf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#XF (SIMD floating-point exception)" },
514 { DBGFEVENT_XCPT_VE, "xcpt_vd", "ve", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#VE (virtualization exception)" },
515 { DBGFEVENT_XCPT_15, "xcpt_15", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
516 { DBGFEVENT_XCPT_16, "xcpt_16", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
517 { DBGFEVENT_XCPT_17, "xcpt_17", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
518 { DBGFEVENT_XCPT_18, "xcpt_18", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
519 { DBGFEVENT_XCPT_19, "xcpt_19", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
520 { DBGFEVENT_XCPT_1a, "xcpt_1a", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
521 { DBGFEVENT_XCPT_1b, "xcpt_1b", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
522 { DBGFEVENT_XCPT_1c, "xcpt_1c", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
523 { DBGFEVENT_XCPT_1d, "xcpt_1d", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
524 { DBGFEVENT_XCPT_SX, "xcpt_sx", "sx", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#SX (security exception)" },
525 { DBGFEVENT_XCPT_1f, "xcpt_1f", "xcpt1f", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
526 { DBGFEVENT_INSTR_HALT, "instr_halt", "hlt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
527 { DBGFEVENT_INSTR_MWAIT, "instr_mwait", "mwait", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
528 { DBGFEVENT_INSTR_MONITOR, "instr_monitor", "monitor", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
529 { DBGFEVENT_INSTR_CPUID, "instr_cpuid", "cpuid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
530 { DBGFEVENT_INSTR_INVD, "instr_invd", "invd", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
531 { DBGFEVENT_INSTR_WBINVD, "instr_wbinvd", "wbinvd", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
532 { DBGFEVENT_INSTR_INVLPG, "instr_invlpg", "invlpg", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
533 { DBGFEVENT_INSTR_RDTSC, "instr_rdtsc", "rdtsc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
534 { DBGFEVENT_INSTR_RDTSCP, "instr_rdtscp", "rdtscp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
535 { DBGFEVENT_INSTR_RDPMC, "instr_rdpmc", "rdpmc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
536 { DBGFEVENT_INSTR_RDMSR, "instr_rdmsr", "rdmsr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
537 { DBGFEVENT_INSTR_WRMSR, "instr_wrmsr", "wrmsr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
538 { DBGFEVENT_INSTR_CRX_READ, "instr_crx_read", "crx_read", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
539 { DBGFEVENT_INSTR_CRX_WRITE, "instr_crx_write", "crx_write",kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
540 { DBGFEVENT_INSTR_DRX_READ, "instr_drx_read", "drx_read", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
541 { DBGFEVENT_INSTR_DRX_WRITE, "instr_drx_write", "drx_write",kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
542 { DBGFEVENT_INSTR_PAUSE, "instr_pause", "pause", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
543 { DBGFEVENT_INSTR_XSETBV, "instr_xsetbv", "xsetbv", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
544 { DBGFEVENT_INSTR_SIDT, "instr_sidt", "sidt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
545 { DBGFEVENT_INSTR_LIDT, "instr_lidt", "lidt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
546 { DBGFEVENT_INSTR_SGDT, "instr_sgdt", "sgdt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
547 { DBGFEVENT_INSTR_LGDT, "instr_lgdt", "lgdt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
548 { DBGFEVENT_INSTR_SLDT, "instr_sldt", "sldt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
549 { DBGFEVENT_INSTR_LLDT, "instr_lldt", "lldt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
550 { DBGFEVENT_INSTR_STR, "instr_str", "str", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
551 { DBGFEVENT_INSTR_LTR, "instr_ltr", "ltr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
552 { DBGFEVENT_INSTR_GETSEC, "instr_getsec", "getsec", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
553 { DBGFEVENT_INSTR_RSM, "instr_rsm", "rsm", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
554 { DBGFEVENT_INSTR_RDRAND, "instr_rdrand", "rdrand", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
555 { DBGFEVENT_INSTR_RDSEED, "instr_rdseed", "rdseed", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
556 { DBGFEVENT_INSTR_XSAVES, "instr_xsaves", "xsaves", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
557 { DBGFEVENT_INSTR_XRSTORS, "instr_xrstors", "xrstors", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
558 { DBGFEVENT_INSTR_VMM_CALL, "instr_vmm_call", "vmm_call", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
559 { DBGFEVENT_INSTR_VMX_VMCLEAR, "instr_vmx_vmclear", "vmclear", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
560 { DBGFEVENT_INSTR_VMX_VMLAUNCH, "instr_vmx_vmlaunch", "vmlaunch", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
561 { DBGFEVENT_INSTR_VMX_VMPTRLD, "instr_vmx_vmptrld", "vmptrld", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
562 { DBGFEVENT_INSTR_VMX_VMPTRST, "instr_vmx_vmptrst", "vmptrst", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
563 { DBGFEVENT_INSTR_VMX_VMREAD, "instr_vmx_vmread", "vmread", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
564 { DBGFEVENT_INSTR_VMX_VMRESUME, "instr_vmx_vmresume", "vmresume", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
565 { DBGFEVENT_INSTR_VMX_VMWRITE, "instr_vmx_vmwrite", "vmwrite", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
566 { DBGFEVENT_INSTR_VMX_VMXOFF, "instr_vmx_vmxoff", "vmxoff", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
567 { DBGFEVENT_INSTR_VMX_VMXON, "instr_vmx_vmxon", "vmxon", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
568 { DBGFEVENT_INSTR_VMX_VMFUNC, "instr_vmx_vmfunc", "vmfunc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
569 { DBGFEVENT_INSTR_VMX_INVEPT, "instr_vmx_invept", "invept", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
570 { DBGFEVENT_INSTR_VMX_INVVPID, "instr_vmx_invvpid", "invvpid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
571 { DBGFEVENT_INSTR_VMX_INVPCID, "instr_vmx_invpcid", "invpcid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
572 { DBGFEVENT_INSTR_SVM_VMRUN, "instr_svm_vmrun", "vmrun", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
573 { DBGFEVENT_INSTR_SVM_VMLOAD, "instr_svm_vmload", "vmload", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
574 { DBGFEVENT_INSTR_SVM_VMSAVE, "instr_svm_vmsave", "vmsave", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
575 { DBGFEVENT_INSTR_SVM_STGI, "instr_svm_stgi", "stgi", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
576 { DBGFEVENT_INSTR_SVM_CLGI, "instr_svm_clgi", "clgi", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
577 { DBGFEVENT_EXIT_TASK_SWITCH, "exit_task_switch", "task_switch", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
578 { DBGFEVENT_EXIT_HALT, "exit_halt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
579 { DBGFEVENT_EXIT_MWAIT, "exit_mwait", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
580 { DBGFEVENT_EXIT_MONITOR, "exit_monitor", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
581 { DBGFEVENT_EXIT_CPUID, "exit_cpuid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
582 { DBGFEVENT_EXIT_INVD, "exit_invd", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
583 { DBGFEVENT_EXIT_WBINVD, "exit_wbinvd", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
584 { DBGFEVENT_EXIT_INVLPG, "exit_invlpg", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
585 { DBGFEVENT_EXIT_RDTSC, "exit_rdtsc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
586 { DBGFEVENT_EXIT_RDTSCP, "exit_rdtscp", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
587 { DBGFEVENT_EXIT_RDPMC, "exit_rdpmc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
588 { DBGFEVENT_EXIT_RDMSR, "exit_rdmsr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
589 { DBGFEVENT_EXIT_WRMSR, "exit_wrmsr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
590 { DBGFEVENT_EXIT_CRX_READ, "exit_crx_read", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
591 { DBGFEVENT_EXIT_CRX_WRITE, "exit_crx_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
592 { DBGFEVENT_EXIT_DRX_READ, "exit_drx_read", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
593 { DBGFEVENT_EXIT_DRX_WRITE, "exit_drx_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
594 { DBGFEVENT_EXIT_PAUSE, "exit_pause", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
595 { DBGFEVENT_EXIT_XSETBV, "exit_xsetbv", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
596 { DBGFEVENT_EXIT_SIDT, "exit_sidt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
597 { DBGFEVENT_EXIT_LIDT, "exit_lidt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
598 { DBGFEVENT_EXIT_SGDT, "exit_sgdt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
599 { DBGFEVENT_EXIT_LGDT, "exit_lgdt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
600 { DBGFEVENT_EXIT_SLDT, "exit_sldt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
601 { DBGFEVENT_EXIT_LLDT, "exit_lldt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
602 { DBGFEVENT_EXIT_STR, "exit_str", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
603 { DBGFEVENT_EXIT_LTR, "exit_ltr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
604 { DBGFEVENT_EXIT_GETSEC, "exit_getsec", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
605 { DBGFEVENT_EXIT_RSM, "exit_rsm", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
606 { DBGFEVENT_EXIT_RDRAND, "exit_rdrand", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
607 { DBGFEVENT_EXIT_RDSEED, "exit_rdseed", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
608 { DBGFEVENT_EXIT_XSAVES, "exit_xsaves", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
609 { DBGFEVENT_EXIT_XRSTORS, "exit_xrstors", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
610 { DBGFEVENT_EXIT_VMM_CALL, "exit_vmm_call", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
611 { DBGFEVENT_EXIT_VMX_VMCLEAR, "exit_vmx_vmclear", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
612 { DBGFEVENT_EXIT_VMX_VMLAUNCH, "exit_vmx_vmlaunch", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
613 { DBGFEVENT_EXIT_VMX_VMPTRLD, "exit_vmx_vmptrld", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
614 { DBGFEVENT_EXIT_VMX_VMPTRST, "exit_vmx_vmptrst", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
615 { DBGFEVENT_EXIT_VMX_VMREAD, "exit_vmx_vmread", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
616 { DBGFEVENT_EXIT_VMX_VMRESUME, "exit_vmx_vmresume", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
617 { DBGFEVENT_EXIT_VMX_VMWRITE, "exit_vmx_vmwrite", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
618 { DBGFEVENT_EXIT_VMX_VMXOFF, "exit_vmx_vmxoff", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
619 { DBGFEVENT_EXIT_VMX_VMXON, "exit_vmx_vmxon", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
620 { DBGFEVENT_EXIT_VMX_VMFUNC, "exit_vmx_vmfunc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
621 { DBGFEVENT_EXIT_VMX_INVEPT, "exit_vmx_invept", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
622 { DBGFEVENT_EXIT_VMX_INVVPID, "exit_vmx_invvpid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
623 { DBGFEVENT_EXIT_VMX_INVPCID, "exit_vmx_invpcid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
624 { DBGFEVENT_EXIT_VMX_EPT_VIOLATION, "exit_vmx_ept_violation", "eptvio", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
625 { DBGFEVENT_EXIT_VMX_EPT_MISCONFIG, "exit_vmx_ept_misconfig", "eptmis", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
626 { DBGFEVENT_EXIT_VMX_VAPIC_ACCESS, "exit_vmx_vapic_access", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
627 { DBGFEVENT_EXIT_VMX_VAPIC_WRITE, "exit_vmx_vapic_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
628 { DBGFEVENT_EXIT_SVM_VMRUN, "exit_svm_vmrun", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
629 { DBGFEVENT_EXIT_SVM_VMLOAD, "exit_svm_vmload", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
630 { DBGFEVENT_EXIT_SVM_VMSAVE, "exit_svm_vmsave", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
631 { DBGFEVENT_EXIT_SVM_STGI, "exit_svm_stgi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
632 { DBGFEVENT_EXIT_SVM_CLGI, "exit_svm_clgi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
633 { DBGFEVENT_IOPORT_UNASSIGNED, "pio_unassigned", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
634 { DBGFEVENT_IOPORT_UNUSED, "pio_unused", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
635 { DBGFEVENT_MEMORY_UNASSIGNED, "mmio_unassigned", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
636 { DBGFEVENT_MEMORY_ROM_WRITE, "rom_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
637 { DBGFEVENT_BSOD_MSR, "bsod_msr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_BUGCHECK, NULL },
638 { DBGFEVENT_BSOD_EFI, "bsod_efi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_BUGCHECK, NULL },
639 { DBGFEVENT_BSOD_VMMDEV, "bsod_vmmdev", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_BUGCHECK, NULL },
640};
641/** Number of entries in g_aDbgcSxEvents. */
642const uint32_t g_cDbgcSxEvents = RT_ELEMENTS(g_aDbgcSxEvents);
643
644
645
646/**
647 * @callback_method_impl{FNDBGCCMD, The 'g' command.}
648 */
649static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
650{
651 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
652
653 /*
654 * Check if the VM is halted or not before trying to resume it.
655 */
656 if (!DBGFR3IsHalted(pUVM))
657 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The VM is already running");
658
659 int rc = DBGFR3Resume(pUVM);
660 if (RT_FAILURE(rc))
661 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3Resume");
662
663 NOREF(paArgs); NOREF(cArgs);
664 return VINF_SUCCESS;
665}
666
667
668/**
669 * @callback_method_impl{FNDBGCCMD, The 'gu' command.}
670 */
671static DECLCALLBACK(int) dbgcCmdGoUp(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
672{
673 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
674 RT_NOREF(pCmd, paArgs, cArgs);
675
676 /* The simple way out. */
677 PDBGFADDRESS pStackPop = NULL; /** @todo try set up some stack limitations */
678 RTGCPTR cbStackPop = 0;
679 int rc = DBGFR3StepEx(pUVM, pDbgc->idCpu, DBGF_STEP_F_OVER | DBGF_STEP_F_STOP_AFTER_RET, NULL, pStackPop, cbStackPop, _512K);
680 if (RT_SUCCESS(rc))
681 pDbgc->fReady = false;
682 else
683 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3StepEx(,,DBGF_STEP_F_OVER | DBGF_STEP_F_STOP_AFTER_RET,) failed");
684 return rc;
685}
686
687
688/**
689 * @callback_method_impl{FNDBGCCMD, The 'ba' command.}
690 */
691static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
692{
693 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
694
695 /*
696 * Interpret access type.
697 */
698 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
699 || paArgs[0].u.pszString[1])
700 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'",
701 paArgs[0].u.pszString, pCmd->pszCmd);
702 uint8_t fType = 0;
703 switch (paArgs[0].u.pszString[0])
704 {
705 case 'x': fType = X86_DR7_RW_EO; break;
706 case 'r': fType = X86_DR7_RW_RW; break;
707 case 'w': fType = X86_DR7_RW_WO; break;
708 case 'i': fType = X86_DR7_RW_IO; break;
709 }
710
711 /*
712 * Validate size.
713 */
714 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
715 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 'x' access type requires size 1!",
716 paArgs[1].u.u64Number, pCmd->pszCmd);
717 switch (paArgs[1].u.u64Number)
718 {
719 case 1:
720 case 2:
721 case 4:
722 break;
723 /*case 8: - later*/
724 default:
725 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 1, 2 or 4!",
726 paArgs[1].u.u64Number, pCmd->pszCmd);
727 }
728 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
729
730 /*
731 * Convert the pointer to a DBGF address.
732 */
733 DBGFADDRESS Address;
734 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
735 if (RT_FAILURE(rc))
736 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%DV,)", &paArgs[2]);
737
738 /*
739 * Pick out the optional arguments.
740 */
741 uint64_t iHitTrigger = 0;
742 uint64_t iHitDisable = UINT64_MAX;
743 const char *pszCmds = NULL;
744 unsigned iArg = 3;
745 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
746 {
747 iHitTrigger = paArgs[iArg].u.u64Number;
748 iArg++;
749 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
750 {
751 iHitDisable = paArgs[iArg].u.u64Number;
752 iArg++;
753 }
754 }
755 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
756 {
757 pszCmds = paArgs[iArg].u.pszString;
758 iArg++;
759 }
760
761 /*
762 * Try set the breakpoint.
763 */
764 uint32_t iBp;
765 rc = DBGFR3BpSetReg(pUVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
766 if (RT_SUCCESS(rc))
767 {
768 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
769 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
770 if (RT_SUCCESS(rc))
771 return DBGCCmdHlpPrintf(pCmdHlp, "Set access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
772 if (rc == VERR_DBGC_BP_EXISTS)
773 {
774 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
775 if (RT_SUCCESS(rc))
776 return DBGCCmdHlpPrintf(pCmdHlp, "Updated access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
777 }
778 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
779 AssertRC(rc2);
780 }
781 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set access breakpoint at %RGv", Address.FlatPtr);
782}
783
784
785/**
786 * @callback_method_impl{FNDBGCCMD, The 'bc' command.}
787 */
788static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
789{
790 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
791
792 /*
793 * Enumerate the arguments.
794 */
795 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
796 int rc = VINF_SUCCESS;
797 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
798 {
799 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
800 {
801 /* one */
802 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
803 if (iBp == paArgs[iArg].u.u64Number)
804 {
805 int rc2 = DBGFR3BpClear(pUVM, iBp);
806 if (RT_FAILURE(rc2))
807 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
808 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
809 dbgcBpDelete(pDbgc, iBp);
810 }
811 else
812 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
813 }
814 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
815 {
816 /* all */
817 PDBGCBP pBp = pDbgc->pFirstBp;
818 while (pBp)
819 {
820 uint32_t iBp = pBp->iBp;
821 pBp = pBp->pNext;
822
823 int rc2 = DBGFR3BpClear(pUVM, iBp);
824 if (RT_FAILURE(rc2))
825 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
826 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
827 dbgcBpDelete(pDbgc, iBp);
828 }
829 }
830 else
831 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
832 }
833 return rc;
834}
835
836
837/**
838 * @callback_method_impl{FNDBGCCMD, The 'bd' command.}
839 */
840static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
841{
842 /*
843 * Enumerate the arguments.
844 */
845 int rc = VINF_SUCCESS;
846 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
847 {
848 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
849 {
850 /* one */
851 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
852 if (iBp == paArgs[iArg].u.u64Number)
853 {
854 rc = DBGFR3BpDisable(pUVM, iBp);
855 if (RT_FAILURE(rc))
856 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpDisable failed for breakpoint %#x", iBp);
857 }
858 else
859 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
860 }
861 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
862 {
863 /* all */
864 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
865 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
866 {
867 int rc2 = DBGFR3BpDisable(pUVM, pBp->iBp);
868 if (RT_FAILURE(rc2))
869 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpDisable failed for breakpoint %#x", pBp->iBp);
870 }
871 }
872 else
873 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
874 }
875 return rc;
876}
877
878
879/**
880 * @callback_method_impl{FNDBGCCMD, The 'be' command.}
881 */
882static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
883{
884 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
885
886 /*
887 * Enumerate the arguments.
888 */
889 int rc = VINF_SUCCESS;
890 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
891 {
892 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
893 {
894 /* one */
895 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
896 if (iBp == paArgs[iArg].u.u64Number)
897 {
898 rc = DBGFR3BpEnable(pUVM, iBp);
899 if (RT_FAILURE(rc))
900 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnable failed for breakpoint %#x", iBp);
901 }
902 else
903 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
904 }
905 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
906 {
907 /* all */
908 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
909 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
910 {
911 int rc2 = DBGFR3BpEnable(pUVM, pBp->iBp);
912 if (RT_FAILURE(rc2))
913 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpEnable failed for breakpoint %#x", pBp->iBp);
914 }
915 }
916 else
917 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
918 }
919 return rc;
920}
921
922
923/**
924 * Breakpoint enumeration callback function.
925 *
926 * @returns VBox status code. Any failure will stop the enumeration.
927 * @param pUVM The user mode VM handle.
928 * @param pvUser The user argument.
929 * @param pBp Pointer to the breakpoint information. (readonly)
930 */
931static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PUVM pUVM, void *pvUser, PCDBGFBP pBp)
932{
933 PDBGC pDbgc = (PDBGC)pvUser;
934 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
935
936 /*
937 * BP type and size.
938 */
939 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%#4x %c ", pBp->iBp, pBp->fEnabled ? 'e' : 'd');
940 bool fHasAddress = false;
941 switch (pBp->enmType)
942 {
943 case DBGFBPTYPE_INT3:
944 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " p %RGv", pBp->u.Int3.GCPtr);
945 fHasAddress = true;
946 break;
947 case DBGFBPTYPE_REG:
948 {
949 char chType;
950 switch (pBp->u.Reg.fType)
951 {
952 case X86_DR7_RW_EO: chType = 'x'; break;
953 case X86_DR7_RW_WO: chType = 'w'; break;
954 case X86_DR7_RW_IO: chType = 'i'; break;
955 case X86_DR7_RW_RW: chType = 'r'; break;
956 default: chType = '?'; break;
957
958 }
959 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%d %c %RGv", pBp->u.Reg.cb, chType, pBp->u.Reg.GCPtr);
960 fHasAddress = true;
961 break;
962 }
963
964 case DBGFBPTYPE_REM:
965 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " r %RGv", pBp->u.Rem.GCPtr);
966 fHasAddress = true;
967 break;
968
969/** @todo realign the list when I/O and MMIO breakpoint command have been added and it's possible to test this code. */
970 case DBGFBPTYPE_PORT_IO:
971 case DBGFBPTYPE_MMIO:
972 {
973 uint32_t fAccess = pBp->enmType == DBGFBPTYPE_PORT_IO ? pBp->u.PortIo.fAccess : pBp->u.Mmio.fAccess;
974 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, pBp->enmType == DBGFBPTYPE_PORT_IO ? " i" : " m");
975 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
976 fAccess & DBGFBPIOACCESS_READ_MASK ? 'r' : '-',
977 fAccess & DBGFBPIOACCESS_READ_BYTE ? '1' : '-',
978 fAccess & DBGFBPIOACCESS_READ_WORD ? '2' : '-',
979 fAccess & DBGFBPIOACCESS_READ_DWORD ? '4' : '-',
980 fAccess & DBGFBPIOACCESS_READ_QWORD ? '8' : '-',
981 fAccess & DBGFBPIOACCESS_READ_OTHER ? '+' : '-');
982 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
983 fAccess & DBGFBPIOACCESS_WRITE_MASK ? 'w' : '-',
984 fAccess & DBGFBPIOACCESS_WRITE_BYTE ? '1' : '-',
985 fAccess & DBGFBPIOACCESS_WRITE_WORD ? '2' : '-',
986 fAccess & DBGFBPIOACCESS_WRITE_DWORD ? '4' : '-',
987 fAccess & DBGFBPIOACCESS_WRITE_QWORD ? '8' : '-',
988 fAccess & DBGFBPIOACCESS_WRITE_OTHER ? '+' : '-');
989 if (pBp->enmType == DBGFBPTYPE_PORT_IO)
990 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04x-%04x",
991 pBp->u.PortIo.uPort, pBp->u.PortIo.uPort + pBp->u.PortIo.cPorts - 1);
992 else
993 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%RGp LB %03x", pBp->u.Mmio.PhysAddr, pBp->u.Mmio.cb);
994 break;
995 }
996
997 default:
998 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " unknown type %d!!", pBp->enmType);
999 AssertFailed();
1000 break;
1001
1002 }
1003 if (pBp->iHitDisable == ~(uint64_t)0)
1004 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to ~0) ", pBp->cHits, pBp->iHitTrigger);
1005 else
1006 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to %04RX64)", pBp->cHits, pBp->iHitTrigger, pBp->iHitDisable);
1007
1008 /*
1009 * Try resolve the address if it has one.
1010 */
1011 if (fHasAddress)
1012 {
1013 RTDBGSYMBOL Sym;
1014 RTINTPTR off;
1015 DBGFADDRESS Addr;
1016 int rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, pBp->u.GCPtr),
1017 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
1018 &off, &Sym, NULL);
1019 if (RT_SUCCESS(rc))
1020 {
1021 if (!off)
1022 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s", Sym.szName);
1023 else if (off > 0)
1024 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, off);
1025 else
1026 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s-%RGv", Sym.szName, -off);
1027 }
1028 }
1029
1030 /*
1031 * The commands.
1032 */
1033 if (pDbgcBp)
1034 {
1035 if (pDbgcBp->cchCmd)
1036 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n cmds: '%s'\n", pDbgcBp->szCmd);
1037 else
1038 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n");
1039 }
1040 else
1041 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " [unknown bp]\n");
1042
1043 return VINF_SUCCESS;
1044}
1045
1046
1047/**
1048 * @callback_method_impl{FNDBGCCMD, The 'bl' command.}
1049 */
1050static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1051{
1052 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1053 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
1054 NOREF(paArgs);
1055
1056 /*
1057 * Enumerate the breakpoints.
1058 */
1059 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1060 int rc = DBGFR3BpEnum(pUVM, dbgcEnumBreakpointsCallback, pDbgc);
1061 if (RT_FAILURE(rc))
1062 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnum");
1063 return rc;
1064}
1065
1066
1067/**
1068 * @callback_method_impl{FNDBGCCMD, The 'bp' command.}
1069 */
1070static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1071{
1072 /*
1073 * Convert the pointer to a DBGF address.
1074 */
1075 DBGFADDRESS Address;
1076 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1077 if (RT_FAILURE(rc))
1078 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
1079
1080 /*
1081 * Pick out the optional arguments.
1082 */
1083 uint64_t iHitTrigger = 0;
1084 uint64_t iHitDisable = UINT64_MAX;
1085 const char *pszCmds = NULL;
1086 unsigned iArg = 1;
1087 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1088 {
1089 iHitTrigger = paArgs[iArg].u.u64Number;
1090 iArg++;
1091 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1092 {
1093 iHitDisable = paArgs[iArg].u.u64Number;
1094 iArg++;
1095 }
1096 }
1097 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1098 {
1099 pszCmds = paArgs[iArg].u.pszString;
1100 iArg++;
1101 }
1102
1103 /*
1104 * Try set the breakpoint.
1105 */
1106 uint32_t iBp;
1107 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1108 rc = DBGFR3BpSetInt3(pUVM, pDbgc->idCpu, &Address, iHitTrigger, iHitDisable, &iBp);
1109 if (RT_SUCCESS(rc))
1110 {
1111 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1112 if (RT_SUCCESS(rc))
1113 return DBGCCmdHlpPrintf(pCmdHlp, "Set breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1114 if (rc == VERR_DBGC_BP_EXISTS)
1115 {
1116 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1117 if (RT_SUCCESS(rc))
1118 return DBGCCmdHlpPrintf(pCmdHlp, "Updated breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1119 }
1120 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
1121 AssertRC(rc2);
1122 }
1123 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set breakpoint at %RGv", Address.FlatPtr);
1124}
1125
1126
1127/**
1128 * @callback_method_impl{FNDBGCCMD, The 'br' command.}
1129 */
1130static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1131{
1132 /*
1133 * Convert the pointer to a DBGF address.
1134 */
1135 DBGFADDRESS Address;
1136 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1137 if (RT_FAILURE(rc))
1138 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
1139
1140 /*
1141 * Pick out the optional arguments.
1142 */
1143 uint64_t iHitTrigger = 0;
1144 uint64_t iHitDisable = UINT64_MAX;
1145 const char *pszCmds = NULL;
1146 unsigned iArg = 1;
1147 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1148 {
1149 iHitTrigger = paArgs[iArg].u.u64Number;
1150 iArg++;
1151 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1152 {
1153 iHitDisable = paArgs[iArg].u.u64Number;
1154 iArg++;
1155 }
1156 }
1157 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1158 {
1159 pszCmds = paArgs[iArg].u.pszString;
1160 iArg++;
1161 }
1162
1163 /*
1164 * Try set the breakpoint.
1165 */
1166 uint32_t iBp;
1167 rc = DBGFR3BpSetREM(pUVM, &Address, iHitTrigger, iHitDisable, &iBp);
1168 if (RT_SUCCESS(rc))
1169 {
1170 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1171 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1172 if (RT_SUCCESS(rc))
1173 return DBGCCmdHlpPrintf(pCmdHlp, "Set REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1174 if (rc == VERR_DBGC_BP_EXISTS)
1175 {
1176 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1177 if (RT_SUCCESS(rc))
1178 return DBGCCmdHlpPrintf(pCmdHlp, "Updated REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1179 }
1180 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
1181 AssertRC(rc2);
1182 }
1183 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set REM breakpoint at %RGv", Address.FlatPtr);
1184}
1185
1186
1187/**
1188 * Helps the unassmble ('u') command display symbols it starts at and passes.
1189 *
1190 * @param pUVM The user mode VM handle.
1191 * @param pCmdHlp The command helpers for printing via.
1192 * @param hDbgAs The address space to look up addresses in.
1193 * @param pAddress The current address.
1194 * @param pcbCallAgain Where to return the distance to the next check (in
1195 * instruction bytes).
1196 */
1197static void dbgcCmdUnassambleHelpListNear(PUVM pUVM, PDBGCCMDHLP pCmdHlp, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
1198 PRTUINTPTR pcbCallAgain)
1199{
1200 RTDBGSYMBOL Symbol;
1201 RTGCINTPTR offDispSym;
1202 int rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress,
1203 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
1204 &offDispSym, &Symbol, NULL);
1205 if (RT_FAILURE(rc) || offDispSym > _1G)
1206 rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress,
1207 RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
1208 &offDispSym, &Symbol, NULL);
1209 if (RT_SUCCESS(rc) && offDispSym < _1G)
1210 {
1211 if (!offDispSym)
1212 {
1213 DBGCCmdHlpPrintf(pCmdHlp, "%s:\n", Symbol.szName);
1214 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb;
1215 }
1216 else if (offDispSym > 0)
1217 {
1218 DBGCCmdHlpPrintf(pCmdHlp, "%s+%#llx:\n", Symbol.szName, (uint64_t)offDispSym);
1219 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb > (RTGCUINTPTR)offDispSym ? Symbol.cb - (RTGCUINTPTR)offDispSym : 1;
1220 }
1221 else
1222 {
1223 DBGCCmdHlpPrintf(pCmdHlp, "%s-%#llx:\n", Symbol.szName, (uint64_t)-offDispSym);
1224 *pcbCallAgain = !Symbol.cb ? 64 : (RTGCUINTPTR)-offDispSym + Symbol.cb;
1225 }
1226 }
1227 else
1228 *pcbCallAgain = UINT32_MAX;
1229}
1230
1231
1232/**
1233 * @callback_method_impl{FNDBGCCMD, The 'u' command.}
1234 */
1235static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1236{
1237 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1238
1239 /*
1240 * Validate input.
1241 */
1242 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1243 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
1244 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
1245
1246 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1247 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
1248
1249 /*
1250 * Check the desired mode.
1251 */
1252 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS | DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
1253 switch (pCmd->pszCmd[1])
1254 {
1255 default: AssertFailed(); RT_FALL_THRU();
1256 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
1257 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
1258 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
1259 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
1260 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
1261 }
1262
1263 /** @todo should use DBGFADDRESS for everything */
1264
1265 /*
1266 * Find address.
1267 */
1268 if (!cArgs)
1269 {
1270 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1271 {
1272 /** @todo Batch query CS, RIP, CPU mode and flags. */
1273 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1274 if ( pDbgc->fRegCtxGuest
1275 && CPUMIsGuestIn64BitCode(pVCpu))
1276 {
1277 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
1278 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
1279 }
1280 else
1281 {
1282 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1283 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1284 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1285 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
1286 && pDbgc->fRegCtxGuest
1287 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
1288 {
1289 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1290 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
1291 }
1292 }
1293
1294 if (pDbgc->fRegCtxGuest)
1295 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1296 else
1297 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
1298 }
1299 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
1300 {
1301 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1302 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
1303 }
1304 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1305 }
1306 else
1307 pDbgc->DisasmPos = paArgs[0];
1308 pDbgc->pLastPos = &pDbgc->DisasmPos;
1309
1310 /*
1311 * Range.
1312 */
1313 switch (pDbgc->DisasmPos.enmRangeType)
1314 {
1315 case DBGCVAR_RANGE_NONE:
1316 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1317 pDbgc->DisasmPos.u64Range = 10;
1318 break;
1319
1320 case DBGCVAR_RANGE_ELEMENTS:
1321 if (pDbgc->DisasmPos.u64Range > 2048)
1322 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
1323 break;
1324
1325 case DBGCVAR_RANGE_BYTES:
1326 if (pDbgc->DisasmPos.u64Range > 65536)
1327 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
1328 break;
1329
1330 default:
1331 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
1332 }
1333
1334 /*
1335 * Convert physical and host addresses to guest addresses.
1336 */
1337 RTDBGAS hDbgAs = pDbgc->hDbgAs;
1338 int rc;
1339 switch (pDbgc->DisasmPos.enmType)
1340 {
1341 case DBGCVAR_TYPE_GC_FLAT:
1342 case DBGCVAR_TYPE_GC_FAR:
1343 break;
1344 case DBGCVAR_TYPE_GC_PHYS:
1345 hDbgAs = DBGF_AS_PHYS;
1346 RT_FALL_THRU();
1347 case DBGCVAR_TYPE_HC_FLAT:
1348 case DBGCVAR_TYPE_HC_PHYS:
1349 {
1350 DBGCVAR VarTmp;
1351 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1352 if (RT_FAILURE(rc))
1353 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
1354 pDbgc->DisasmPos = VarTmp;
1355 break;
1356 }
1357 default: AssertFailed(); break;
1358 }
1359
1360 DBGFADDRESS CurAddr;
1361 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1362 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1363 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1364 else
1365 {
1366 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1367 if (RT_FAILURE(rc))
1368 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
1369 }
1370
1371 if (CurAddr.fFlags & DBGFADDRESS_FLAGS_HMA)
1372 fFlags |= DBGF_DISAS_FLAGS_HYPER; /* This crap is due to not using DBGFADDRESS as DBGFR3Disas* input. */
1373 pDbgc->fDisasm = fFlags;
1374
1375 /*
1376 * Figure out where we are and display it. Also calculate when we need to
1377 * check for a new symbol if possible.
1378 */
1379 RTGCUINTPTR cbCheckSymbol;
1380 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1381
1382 /*
1383 * Do the disassembling.
1384 */
1385 unsigned cTries = 32;
1386 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1387 if (iRangeLeft == 0) /* kludge for 'r'. */
1388 iRangeLeft = -1;
1389 for (;;)
1390 {
1391 /*
1392 * Disassemble the instruction.
1393 */
1394 char szDis[256];
1395 uint32_t cbInstr = 1;
1396 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1397 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags,
1398 &szDis[0], sizeof(szDis), &cbInstr);
1399 else
1400 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags,
1401 &szDis[0], sizeof(szDis), &cbInstr);
1402 if (RT_SUCCESS(rc))
1403 {
1404 /* print it */
1405 rc = DBGCCmdHlpPrintf(pCmdHlp, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1406 if (RT_FAILURE(rc))
1407 return rc;
1408 }
1409 else
1410 {
1411 /* bitch. */
1412 int rc2 = DBGCCmdHlpPrintf(pCmdHlp, "Failed to disassemble instruction, skipping one byte.\n");
1413 if (RT_FAILURE(rc2))
1414 return rc2;
1415 if (cTries-- > 0)
1416 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Too many disassembly failures. Giving up");
1417 cbInstr = 1;
1418 }
1419
1420 /* advance */
1421 if (iRangeLeft < 0) /* 'r' */
1422 break;
1423 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1424 iRangeLeft--;
1425 else
1426 iRangeLeft -= cbInstr;
1427 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1428 if (RT_FAILURE(rc))
1429 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpEval(,,'(%Dv) + %x')", &pDbgc->DisasmPos, cbInstr);
1430 if (iRangeLeft <= 0)
1431 break;
1432 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1433
1434 /* Print next symbol? */
1435 if (cbCheckSymbol <= cbInstr)
1436 {
1437 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1438 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1439 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1440 else
1441 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1442 if (RT_SUCCESS(rc))
1443 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1444 else
1445 cbCheckSymbol = UINT32_MAX;
1446 }
1447 else
1448 cbCheckSymbol -= cbInstr;
1449 }
1450
1451 NOREF(pCmd);
1452 return VINF_SUCCESS;
1453}
1454
1455
1456/**
1457 * @callback_method_impl{FNDGCSCREENBLIT}
1458 */
1459static DECLCALLBACK(int) dbgcCmdUnassembleCfgBlit(const char *psz, void *pvUser)
1460{
1461 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pvUser;
1462 return DBGCCmdHlpPrintf(pCmdHlp, "%s", psz);
1463}
1464
1465
1466/**
1467 * Checks whether both addresses are equal.
1468 *
1469 * @returns true if both addresses point to the same location, false otherwise.
1470 * @param pAddr1 First address.
1471 * @param pAddr2 Second address.
1472 */
1473static bool dbgcCmdUnassembleCfgAddrEqual(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
1474{
1475 return pAddr1->Sel == pAddr2->Sel
1476 && pAddr1->off == pAddr2->off;
1477}
1478
1479
1480/**
1481 * Checks whether the first given address is lower than the second one.
1482 *
1483 * @returns true if both addresses point to the same location, false otherwise.
1484 * @param pAddr1 First address.
1485 * @param pAddr2 Second address.
1486 */
1487static bool dbgcCmdUnassembleCfgAddrLower(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
1488{
1489 return pAddr1->Sel == pAddr2->Sel
1490 && pAddr1->off < pAddr2->off;
1491}
1492
1493
1494/**
1495 * Calculates the size required for the given basic block including the
1496 * border and spacing on the edges.
1497 *
1498 * @returns nothing.
1499 * @param hFlowBb The basic block handle.
1500 * @param pDumpBb The dumper state to fill in for the basic block.
1501 */
1502static void dbgcCmdUnassembleCfgDumpCalcBbSize(DBGFFLOWBB hFlowBb, PDBGCFLOWBBDUMP pDumpBb)
1503{
1504 uint32_t fFlags = DBGFR3FlowBbGetFlags(hFlowBb);
1505 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(hFlowBb);
1506
1507 pDumpBb->hFlowBb = hFlowBb;
1508 pDumpBb->cchHeight = cInstr + 4; /* Include spacing and border top and bottom. */
1509 pDumpBb->cchWidth = 0;
1510 DBGFR3FlowBbGetStartAddress(hFlowBb, &pDumpBb->AddrStart);
1511
1512 DBGFFLOWBBENDTYPE enmType = DBGFR3FlowBbGetType(hFlowBb);
1513 if ( enmType == DBGFFLOWBBENDTYPE_COND
1514 || enmType == DBGFFLOWBBENDTYPE_UNCOND_JMP
1515 || enmType == DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP)
1516 DBGFR3FlowBbGetBranchAddress(hFlowBb, &pDumpBb->AddrTarget);
1517
1518 if (fFlags & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1519 {
1520 const char *pszErr = NULL;
1521 DBGFR3FlowBbQueryError(hFlowBb, &pszErr);
1522 if (pszErr)
1523 {
1524 pDumpBb->cchHeight++;
1525 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszErr));
1526 }
1527 }
1528 for (unsigned i = 0; i < cInstr; i++)
1529 {
1530 const char *pszInstr = NULL;
1531 int rc = DBGFR3FlowBbQueryInstr(hFlowBb, i, NULL, NULL, &pszInstr);
1532 AssertRC(rc);
1533 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszInstr));
1534 }
1535 pDumpBb->cchWidth += 4; /* Include spacing and border left and right. */
1536}
1537
1538
1539/**
1540 * Dumps a top or bottom boundary line.
1541 *
1542 * @returns nothing.
1543 * @param hScreen The screen to draw to.
1544 * @param uStartX Where to start drawing the boundary.
1545 * @param uStartY Y coordinate.
1546 * @param cchWidth Width of the boundary.
1547 * @param enmColor The color to use for drawing.
1548 */
1549static void dbgcCmdUnassembleCfgDumpBbBoundary(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1550 DBGCSCREENCOLOR enmColor)
1551{
1552 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '+', enmColor);
1553 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1554 uStartY, '-', enmColor);
1555 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '+', enmColor);
1556}
1557
1558
1559/**
1560 * Dumps a spacing line between the top or bottom boundary and the actual disassembly.
1561 *
1562 * @returns nothing.
1563 * @param hScreen The screen to draw to.
1564 * @param uStartX Where to start drawing the spacing.
1565 * @param uStartY Y coordinate.
1566 * @param cchWidth Width of the spacing.
1567 * @param enmColor The color to use for drawing.
1568 */
1569static void dbgcCmdUnassembleCfgDumpBbSpacing(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1570 DBGCSCREENCOLOR enmColor)
1571{
1572 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmColor);
1573 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1574 uStartY, ' ', enmColor);
1575 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmColor);
1576}
1577
1578
1579/**
1580 * Writes a given text to the screen.
1581 *
1582 * @returns nothing.
1583 * @param hScreen The screen to draw to.
1584 * @param uStartX Where to start drawing the line.
1585 * @param uStartY Y coordinate.
1586 * @param cchWidth Maximum width of the text.
1587 * @param pszText The text to write.
1588 * @param enmTextColor The color to use for drawing the text.
1589 * @param enmBorderColor The color to use for drawing the border.
1590 */
1591static void dbgcCmdUnassembleCfgDumpBbText(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY,
1592 uint32_t cchWidth, const char *pszText,
1593 DBGCSCREENCOLOR enmTextColor, DBGCSCREENCOLOR enmBorderColor)
1594{
1595 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmBorderColor);
1596 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + 1, uStartY, ' ', enmTextColor);
1597 dbgcScreenAsciiDrawString(hScreen, uStartX + 2, uStartY, pszText, enmTextColor);
1598 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmBorderColor);
1599}
1600
1601
1602/**
1603 * Dumps one basic block using the dumper callback.
1604 *
1605 * @returns nothing.
1606 * @param pDumpBb The basic block dump state to dump.
1607 * @param hScreen The screen to draw to.
1608 */
1609static void dbgcCmdUnassembleCfgDumpBb(PDBGCFLOWBBDUMP pDumpBb, DBGCSCREEN hScreen)
1610{
1611 uint32_t uStartY = pDumpBb->uStartY;
1612 bool fError = RT_BOOL(DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR);
1613 DBGCSCREENCOLOR enmColor = fError ? DBGCSCREENCOLOR_RED_BRIGHT : DBGCSCREENCOLOR_DEFAULT;
1614
1615 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1616 uStartY++;
1617 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1618 uStartY++;
1619
1620 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(pDumpBb->hFlowBb);
1621 for (unsigned i = 0; i < cInstr; i++)
1622 {
1623 const char *pszInstr = NULL;
1624 DBGFR3FlowBbQueryInstr(pDumpBb->hFlowBb, i, NULL, NULL, &pszInstr);
1625 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY + i,
1626 pDumpBb->cchWidth, pszInstr, DBGCSCREENCOLOR_DEFAULT,
1627 enmColor);
1628 }
1629 uStartY += cInstr;
1630
1631 if (fError)
1632 {
1633 const char *pszErr = NULL;
1634 DBGFR3FlowBbQueryError(pDumpBb->hFlowBb, &pszErr);
1635 if (pszErr)
1636 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY,
1637 pDumpBb->cchWidth, pszErr, enmColor,
1638 enmColor);
1639 uStartY++;
1640 }
1641
1642 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1643 uStartY++;
1644 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1645 uStartY++;
1646}
1647
1648
1649/**
1650 * Dumps one branch table using the dumper callback.
1651 *
1652 * @returns nothing.
1653 * @param pDumpBranchTbl The basic block dump state to dump.
1654 * @param hScreen The screen to draw to.
1655 */
1656static void dbgcCmdUnassembleCfgDumpBranchTbl(PDBGCFLOWBRANCHTBLDUMP pDumpBranchTbl, DBGCSCREEN hScreen)
1657{
1658 uint32_t uStartY = pDumpBranchTbl->uStartY;
1659 DBGCSCREENCOLOR enmColor = DBGCSCREENCOLOR_CYAN_BRIGHT;
1660
1661 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1662 uStartY++;
1663 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1664 uStartY++;
1665
1666 uint32_t cSlots = DBGFR3FlowBranchTblGetSlots(pDumpBranchTbl->hFlowBranchTbl);
1667 for (unsigned i = 0; i < cSlots; i++)
1668 {
1669 DBGFADDRESS Addr;
1670 char szAddr[128];
1671
1672 RT_ZERO(szAddr);
1673 DBGFR3FlowBranchTblGetAddrAtSlot(pDumpBranchTbl->hFlowBranchTbl, i, &Addr);
1674
1675 if (Addr.Sel == DBGF_SEL_FLAT)
1676 RTStrPrintf(&szAddr[0], sizeof(szAddr), "%RGv", Addr.FlatPtr);
1677 else
1678 RTStrPrintf(&szAddr[0], sizeof(szAddr), "%04x:%RGv", Addr.Sel, Addr.off);
1679
1680 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBranchTbl->uStartX, uStartY + i,
1681 pDumpBranchTbl->cchWidth, &szAddr[0], DBGCSCREENCOLOR_DEFAULT,
1682 enmColor);
1683 }
1684 uStartY += cSlots;
1685
1686 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1687 uStartY++;
1688 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1689 uStartY++;
1690}
1691
1692
1693/**
1694 * Fills in the dump states for the basic blocks and branch tables.
1695 *
1696 * @returns VBox status code.
1697 * @param hFlowIt The control flow graph iterator handle.
1698 * @param hFlowBranchTblIt The control flow graph branch table iterator handle.
1699 * @param paDumpBb The array of basic block dump states.
1700 * @param paDumpBranchTbl The array of branch table dump states.
1701 * @param cBbs Number of basic blocks.
1702 * @param cBranchTbls Number of branch tables.
1703 */
1704static int dbgcCmdUnassembleCfgDumpCalcDimensions(DBGFFLOWIT hFlowIt, DBGFFLOWBRANCHTBLIT hFlowBranchTblIt,
1705 PDBGCFLOWBBDUMP paDumpBb, PDBGCFLOWBRANCHTBLDUMP paDumpBranchTbl,
1706 uint32_t cBbs, uint32_t cBranchTbls)
1707{
1708 RT_NOREF2(cBbs, cBranchTbls);
1709
1710 /* Calculate the sizes of each basic block first. */
1711 DBGFFLOWBB hFlowBb = DBGFR3FlowItNext(hFlowIt);
1712 uint32_t idx = 0;
1713 while (hFlowBb)
1714 {
1715 dbgcCmdUnassembleCfgDumpCalcBbSize(hFlowBb, &paDumpBb[idx]);
1716 idx++;
1717 hFlowBb = DBGFR3FlowItNext(hFlowIt);
1718 }
1719
1720 if (paDumpBranchTbl)
1721 {
1722 idx = 0;
1723 DBGFFLOWBRANCHTBL hFlowBranchTbl = DBGFR3FlowBranchTblItNext(hFlowBranchTblIt);
1724 while (hFlowBranchTbl)
1725 {
1726 paDumpBranchTbl[idx].hFlowBranchTbl = hFlowBranchTbl;
1727 paDumpBranchTbl[idx].cchHeight = DBGFR3FlowBranchTblGetSlots(hFlowBranchTbl) + 4; /* Spacing and border. */
1728 paDumpBranchTbl[idx].cchWidth = 25 + 4; /* Spacing and border. */
1729 idx++;
1730 hFlowBranchTbl = DBGFR3FlowBranchTblItNext(hFlowBranchTblIt);
1731 }
1732 }
1733
1734 return VINF_SUCCESS;
1735}
1736
1737/**
1738 * Dumps the given control flow graph to the output.
1739 *
1740 * @returns VBox status code.
1741 * @param hCfg The control flow graph handle.
1742 * @param fUseColor Flag whether the output should be colorized.
1743 * @param pCmdHlp The command helper callback table.
1744 */
1745static int dbgcCmdUnassembleCfgDump(DBGFFLOW hCfg, bool fUseColor, PDBGCCMDHLP pCmdHlp)
1746{
1747 int rc = VINF_SUCCESS;
1748 DBGFFLOWIT hCfgIt = NULL;
1749 DBGFFLOWBRANCHTBLIT hFlowBranchTblIt = NULL;
1750 uint32_t cBbs = DBGFR3FlowGetBbCount(hCfg);
1751 uint32_t cBranchTbls = DBGFR3FlowGetBranchTblCount(hCfg);
1752 PDBGCFLOWBBDUMP paDumpBb = (PDBGCFLOWBBDUMP)RTMemTmpAllocZ(cBbs * sizeof(DBGCFLOWBBDUMP));
1753 PDBGCFLOWBRANCHTBLDUMP paDumpBranchTbl = NULL;
1754
1755 if (cBranchTbls)
1756 paDumpBranchTbl = (PDBGCFLOWBRANCHTBLDUMP)RTMemAllocZ(cBranchTbls * sizeof(DBGCFLOWBRANCHTBLDUMP));
1757
1758 if (RT_UNLIKELY(!paDumpBb || (!paDumpBranchTbl && cBranchTbls > 0)))
1759 rc = VERR_NO_MEMORY;
1760 if (RT_SUCCESS(rc))
1761 rc = DBGFR3FlowItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hCfgIt);
1762 if (RT_SUCCESS(rc) && cBranchTbls > 0)
1763 rc = DBGFR3FlowBranchTblItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hFlowBranchTblIt);
1764
1765 if (RT_SUCCESS(rc))
1766 {
1767 rc = dbgcCmdUnassembleCfgDumpCalcDimensions(hCfgIt, hFlowBranchTblIt, paDumpBb, paDumpBranchTbl,
1768 cBbs, cBranchTbls);
1769
1770 /* Calculate the ASCII screen dimensions and create one. */
1771 uint32_t cchWidth = 0;
1772 uint32_t cchLeftExtra = 5;
1773 uint32_t cchRightExtra = 5;
1774 uint32_t cchHeight = 0;
1775 for (unsigned i = 0; i < cBbs; i++)
1776 {
1777 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1778 cchWidth = RT_MAX(cchWidth, pDumpBb->cchWidth);
1779 cchHeight += pDumpBb->cchHeight;
1780
1781 /* Incomplete blocks don't have a successor. */
1782 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1783 continue;
1784
1785 switch (DBGFR3FlowBbGetType(pDumpBb->hFlowBb))
1786 {
1787 case DBGFFLOWBBENDTYPE_EXIT:
1788 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1789 break;
1790 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1791 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1792 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1793 cchLeftExtra++;
1794 else
1795 cchRightExtra++;
1796 break;
1797 case DBGFFLOWBBENDTYPE_UNCOND:
1798 cchHeight += 2; /* For the arrow down to the next basic block. */
1799 break;
1800 case DBGFFLOWBBENDTYPE_COND:
1801 cchHeight += 2; /* For the arrow down to the next basic block. */
1802 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1803 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1804 cchLeftExtra++;
1805 else
1806 cchRightExtra++;
1807 break;
1808 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1809 default:
1810 AssertFailed();
1811 }
1812 }
1813
1814 for (unsigned i = 0; i < cBranchTbls; i++)
1815 {
1816 PDBGCFLOWBRANCHTBLDUMP pDumpBranchTbl = &paDumpBranchTbl[i];
1817 cchWidth = RT_MAX(cchWidth, pDumpBranchTbl->cchWidth);
1818 cchHeight += pDumpBranchTbl->cchHeight;
1819 }
1820
1821 cchWidth += 2;
1822
1823 DBGCSCREEN hScreen = NULL;
1824 rc = dbgcScreenAsciiCreate(&hScreen, cchWidth + cchLeftExtra + cchRightExtra, cchHeight);
1825 if (RT_SUCCESS(rc))
1826 {
1827 uint32_t uY = 0;
1828
1829 /* Dump the branch tables first. */
1830 for (unsigned i = 0; i < cBranchTbls; i++)
1831 {
1832 paDumpBranchTbl[i].uStartX = cchLeftExtra + (cchWidth - paDumpBranchTbl[i].cchWidth) / 2;
1833 paDumpBranchTbl[i].uStartY = uY;
1834 dbgcCmdUnassembleCfgDumpBranchTbl(&paDumpBranchTbl[i], hScreen);
1835 uY += paDumpBranchTbl[i].cchHeight;
1836 }
1837
1838 /* Dump the basic blocks and connections to the immediate successor. */
1839 for (unsigned i = 0; i < cBbs; i++)
1840 {
1841 paDumpBb[i].uStartX = cchLeftExtra + (cchWidth - paDumpBb[i].cchWidth) / 2;
1842 paDumpBb[i].uStartY = uY;
1843 dbgcCmdUnassembleCfgDumpBb(&paDumpBb[i], hScreen);
1844 uY += paDumpBb[i].cchHeight;
1845
1846 /* Incomplete blocks don't have a successor. */
1847 if (DBGFR3FlowBbGetFlags(paDumpBb[i].hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1848 continue;
1849
1850 switch (DBGFR3FlowBbGetType(paDumpBb[i].hFlowBb))
1851 {
1852 case DBGFFLOWBBENDTYPE_EXIT:
1853 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1854 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1855 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1856 break;
1857 case DBGFFLOWBBENDTYPE_UNCOND:
1858 /* Draw the arrow down to the next block. */
1859 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1860 '|', DBGCSCREENCOLOR_BLUE_BRIGHT);
1861 uY++;
1862 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1863 'V', DBGCSCREENCOLOR_BLUE_BRIGHT);
1864 uY++;
1865 break;
1866 case DBGFFLOWBBENDTYPE_COND:
1867 /* Draw the arrow down to the next block. */
1868 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1869 '|', DBGCSCREENCOLOR_RED_BRIGHT);
1870 uY++;
1871 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1872 'V', DBGCSCREENCOLOR_RED_BRIGHT);
1873 uY++;
1874 break;
1875 default:
1876 AssertFailed();
1877 }
1878 }
1879
1880 /* Last pass, connect all remaining branches. */
1881 uint32_t uBackConns = 0;
1882 uint32_t uFwdConns = 0;
1883 for (unsigned i = 0; i < cBbs; i++)
1884 {
1885 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1886 DBGFFLOWBBENDTYPE enmEndType = DBGFR3FlowBbGetType(pDumpBb->hFlowBb);
1887
1888 /* Incomplete blocks don't have a successor. */
1889 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1890 continue;
1891
1892 switch (enmEndType)
1893 {
1894 case DBGFFLOWBBENDTYPE_EXIT:
1895 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1896 case DBGFFLOWBBENDTYPE_UNCOND:
1897 break;
1898 case DBGFFLOWBBENDTYPE_COND:
1899 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1900 {
1901 /* Find the target first to get the coordinates. */
1902 PDBGCFLOWBBDUMP pDumpBbTgt = NULL;
1903 for (unsigned idxDumpBb = 0; idxDumpBb < cBbs; idxDumpBb++)
1904 {
1905 pDumpBbTgt = &paDumpBb[idxDumpBb];
1906 if (dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBbTgt->AddrStart))
1907 break;
1908 }
1909
1910 DBGCSCREENCOLOR enmColor = enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
1911 ? DBGCSCREENCOLOR_YELLOW_BRIGHT
1912 : DBGCSCREENCOLOR_GREEN_BRIGHT;
1913
1914 /*
1915 * Use the right side for targets with higher addresses,
1916 * left when jumping backwards.
1917 */
1918 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1919 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1920 {
1921 /* Going backwards. */
1922 uint32_t uXVerLine = /*cchLeftExtra - 1 -*/ uBackConns + 1;
1923 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1924 uBackConns++;
1925
1926 /* Draw the arrow pointing to the target block. */
1927 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX - 1, pDumpBbTgt->uStartY,
1928 '>', enmColor);
1929 /* Draw the horizontal line. */
1930 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBbTgt->uStartX - 2,
1931 pDumpBbTgt->uStartY, '-', enmColor);
1932 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1933 enmColor);
1934 /* Draw the vertical line down to the source block. */
1935 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, pDumpBbTgt->uStartY + 1, uYHorLine - 1,
1936 '|', enmColor);
1937 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1938 /* Draw the horizontal connection between the source block and vertical part. */
1939 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBb->uStartX - 1,
1940 uYHorLine, '-', enmColor);
1941
1942 }
1943 else
1944 {
1945 /* Going forward. */
1946 uint32_t uXVerLine = cchWidth + cchLeftExtra + (cchRightExtra - uFwdConns) - 1;
1947 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1948 uFwdConns++;
1949
1950 /* Draw the horizontal line. */
1951 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBb->uStartX + pDumpBb->cchWidth,
1952 uXVerLine - 1, uYHorLine, '-', enmColor);
1953 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1954 /* Draw the vertical line down to the target block. */
1955 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, uYHorLine + 1, pDumpBbTgt->uStartY - 1,
1956 '|', enmColor);
1957 /* Draw the horizontal connection between the target block and vertical part. */
1958 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1959 uXVerLine, pDumpBbTgt->uStartY, '-', enmColor);
1960 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1961 enmColor);
1962 /* Draw the arrow pointing to the target block. */
1963 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1964 pDumpBbTgt->uStartY, '<', enmColor);
1965 }
1966 break;
1967 }
1968 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1969 default:
1970 AssertFailed();
1971 }
1972 }
1973
1974 rc = dbgcScreenAsciiBlit(hScreen, dbgcCmdUnassembleCfgBlit, pCmdHlp, fUseColor);
1975 dbgcScreenAsciiDestroy(hScreen);
1976 }
1977 }
1978
1979 if (paDumpBb)
1980 {
1981 for (unsigned i = 0; i < cBbs; i++)
1982 DBGFR3FlowBbRelease(paDumpBb[i].hFlowBb);
1983 RTMemTmpFree(paDumpBb);
1984 }
1985
1986 if (paDumpBranchTbl)
1987 {
1988 for (unsigned i = 0; i < cBranchTbls; i++)
1989 DBGFR3FlowBranchTblRelease(paDumpBranchTbl[i].hFlowBranchTbl);
1990 RTMemTmpFree(paDumpBranchTbl);
1991 }
1992
1993 if (hCfgIt)
1994 DBGFR3FlowItDestroy(hCfgIt);
1995 if (hFlowBranchTblIt)
1996 DBGFR3FlowBranchTblItDestroy(hFlowBranchTblIt);
1997
1998 return rc;
1999}
2000
2001
2002/**
2003 * @callback_method_impl{FNDBGCCMD, The 'ucfg' command.}
2004 */
2005static DECLCALLBACK(int) dbgcCmdUnassembleCfg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2006{
2007 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2008
2009 /*
2010 * Validate input.
2011 */
2012 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2013 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
2014 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
2015
2016 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
2017 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
2018
2019 /*
2020 * Check the desired mode.
2021 */
2022 unsigned fFlags = DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
2023 bool fUseColor = false;
2024 switch (pCmd->pszCmd[4])
2025 {
2026 default: AssertFailed(); RT_FALL_THRU();
2027 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
2028 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
2029 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
2030 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
2031 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
2032 case 'c': fUseColor = true; break;
2033 }
2034
2035 /** @todo should use DBGFADDRESS for everything */
2036
2037 /*
2038 * Find address.
2039 */
2040 if (!cArgs)
2041 {
2042 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
2043 {
2044 /** @todo Batch query CS, RIP, CPU mode and flags. */
2045 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2046 if ( pDbgc->fRegCtxGuest
2047 && CPUMIsGuestIn64BitCode(pVCpu))
2048 {
2049 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
2050 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
2051 }
2052 else
2053 {
2054 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
2055 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
2056 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
2057 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
2058 && pDbgc->fRegCtxGuest
2059 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
2060 {
2061 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
2062 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
2063 }
2064 }
2065
2066 if (pDbgc->fRegCtxGuest)
2067 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
2068 else
2069 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
2070 }
2071 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
2072 {
2073 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
2074 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
2075 }
2076 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
2077 }
2078 else
2079 pDbgc->DisasmPos = paArgs[0];
2080 pDbgc->pLastPos = &pDbgc->DisasmPos;
2081
2082 /*
2083 * Range.
2084 */
2085 switch (pDbgc->DisasmPos.enmRangeType)
2086 {
2087 case DBGCVAR_RANGE_NONE:
2088 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2089 pDbgc->DisasmPos.u64Range = 10;
2090 break;
2091
2092 case DBGCVAR_RANGE_ELEMENTS:
2093 if (pDbgc->DisasmPos.u64Range > 2048)
2094 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
2095 break;
2096
2097 case DBGCVAR_RANGE_BYTES:
2098 if (pDbgc->DisasmPos.u64Range > 65536)
2099 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
2100 break;
2101
2102 default:
2103 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
2104 }
2105
2106 /*
2107 * Convert physical and host addresses to guest addresses.
2108 */
2109 RTDBGAS hDbgAs = pDbgc->hDbgAs;
2110 int rc;
2111 switch (pDbgc->DisasmPos.enmType)
2112 {
2113 case DBGCVAR_TYPE_GC_FLAT:
2114 case DBGCVAR_TYPE_GC_FAR:
2115 break;
2116 case DBGCVAR_TYPE_GC_PHYS:
2117 hDbgAs = DBGF_AS_PHYS;
2118 RT_FALL_THRU();
2119 case DBGCVAR_TYPE_HC_FLAT:
2120 case DBGCVAR_TYPE_HC_PHYS:
2121 {
2122 DBGCVAR VarTmp;
2123 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
2124 if (RT_FAILURE(rc))
2125 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
2126 pDbgc->DisasmPos = VarTmp;
2127 break;
2128 }
2129 default: AssertFailed(); break;
2130 }
2131
2132 DBGFADDRESS CurAddr;
2133 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
2134 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
2135 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
2136 else
2137 {
2138 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
2139 if (RT_FAILURE(rc))
2140 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
2141 }
2142
2143 DBGFFLOW hCfg;
2144 rc = DBGFR3FlowCreate(pUVM, pDbgc->idCpu, &CurAddr, 0 /*cbDisasmMax*/,
2145 DBGF_FLOW_CREATE_F_TRY_RESOLVE_INDIRECT_BRANCHES, fFlags, &hCfg);
2146 if (RT_SUCCESS(rc))
2147 {
2148 /* Dump the graph. */
2149 rc = dbgcCmdUnassembleCfgDump(hCfg, fUseColor, pCmdHlp);
2150 DBGFR3FlowRelease(hCfg);
2151 }
2152 else
2153 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3FlowCreate failed on '%Dv'", &pDbgc->DisasmPos);
2154
2155 NOREF(pCmd);
2156 return rc;
2157}
2158
2159
2160/**
2161 * @callback_method_impl{FNDBGCCMD, The 'ls' command.}
2162 */
2163static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2164{
2165 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2166
2167 /*
2168 * Validate input.
2169 */
2170 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
2171 if (cArgs == 1)
2172 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
2173 if (!pUVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
2174 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start listing...");
2175 if (!pUVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
2176 return DBGCCmdHlpFail(pCmdHlp, pCmd, "GC address but no VM");
2177
2178 /*
2179 * Find address.
2180 */
2181 if (!cArgs)
2182 {
2183 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
2184 {
2185 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2186 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
2187 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
2188 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
2189 }
2190 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
2191 }
2192 else
2193 pDbgc->SourcePos = paArgs[0];
2194 pDbgc->pLastPos = &pDbgc->SourcePos;
2195
2196 /*
2197 * Ensure the source address is flat GC.
2198 */
2199 switch (pDbgc->SourcePos.enmType)
2200 {
2201 case DBGCVAR_TYPE_GC_FLAT:
2202 break;
2203 case DBGCVAR_TYPE_GC_PHYS:
2204 case DBGCVAR_TYPE_GC_FAR:
2205 case DBGCVAR_TYPE_HC_FLAT:
2206 case DBGCVAR_TYPE_HC_PHYS:
2207 {
2208 int rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
2209 if (RT_FAILURE(rc))
2210 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid address or address type. (rc=%d)\n", rc);
2211 break;
2212 }
2213 default: AssertFailed(); break;
2214 }
2215
2216 /*
2217 * Range.
2218 */
2219 switch (pDbgc->SourcePos.enmRangeType)
2220 {
2221 case DBGCVAR_RANGE_NONE:
2222 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2223 pDbgc->SourcePos.u64Range = 10;
2224 break;
2225
2226 case DBGCVAR_RANGE_ELEMENTS:
2227 if (pDbgc->SourcePos.u64Range > 2048)
2228 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many lines requested. Max is 2048 lines.\n");
2229 break;
2230
2231 case DBGCVAR_RANGE_BYTES:
2232 if (pDbgc->SourcePos.u64Range > 65536)
2233 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
2234 break;
2235
2236 default:
2237 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
2238 }
2239
2240 /*
2241 * Do the disassembling.
2242 */
2243 bool fFirst = 1;
2244 RTDBGLINE LinePrev = { 0, 0, 0, 0, 0, "" };
2245 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
2246 if (iRangeLeft == 0) /* kludge for 'r'. */
2247 iRangeLeft = -1;
2248 for (;;)
2249 {
2250 /*
2251 * Get line info.
2252 */
2253 RTDBGLINE Line;
2254 RTGCINTPTR off;
2255 DBGFADDRESS SourcePosAddr;
2256 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->SourcePos, &SourcePosAddr);
2257 if (RT_FAILURE(rc))
2258 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%Dv)", &pDbgc->SourcePos);
2259 rc = DBGFR3AsLineByAddr(pUVM, pDbgc->hDbgAs, &SourcePosAddr, &off, &Line, NULL);
2260 if (RT_FAILURE(rc))
2261 return VINF_SUCCESS;
2262
2263 unsigned cLines = 0;
2264 if (memcmp(&Line, &LinePrev, sizeof(Line)))
2265 {
2266 /*
2267 * Print filenamename
2268 */
2269 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
2270 fFirst = true;
2271 if (fFirst)
2272 {
2273 rc = DBGCCmdHlpPrintf(pCmdHlp, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
2274 if (RT_FAILURE(rc))
2275 return rc;
2276 }
2277
2278 /*
2279 * Try open the file and read the line.
2280 */
2281 FILE *phFile = fopen(Line.szFilename, "r");
2282 if (phFile)
2283 {
2284 /* Skip ahead to the desired line. */
2285 char szLine[4096];
2286 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
2287 if (cBefore > 7)
2288 cBefore = 0;
2289 unsigned cLeft = Line.uLineNo - cBefore;
2290 while (cLeft > 0)
2291 {
2292 szLine[0] = '\0';
2293 if (!fgets(szLine, sizeof(szLine), phFile))
2294 break;
2295 cLeft--;
2296 }
2297 if (!cLeft)
2298 {
2299 /* print the before lines */
2300 for (;;)
2301 {
2302 size_t cch = strlen(szLine);
2303 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || RT_C_IS_SPACE(szLine[cch - 1])) )
2304 szLine[--cch] = '\0';
2305 if (cBefore-- <= 0)
2306 break;
2307
2308 rc = DBGCCmdHlpPrintf(pCmdHlp, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
2309 szLine[0] = '\0';
2310 const char *pszShutUpGcc = fgets(szLine, sizeof(szLine), phFile); NOREF(pszShutUpGcc);
2311 cLines++;
2312 }
2313 /* print the actual line */
2314 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
2315 }
2316 fclose(phFile);
2317 if (RT_FAILURE(rc))
2318 return rc;
2319 fFirst = false;
2320 }
2321 else
2322 return DBGCCmdHlpPrintf(pCmdHlp, "Warning: couldn't open source file '%s'\n", Line.szFilename);
2323
2324 LinePrev = Line;
2325 }
2326
2327
2328 /*
2329 * Advance
2330 */
2331 if (iRangeLeft < 0) /* 'r' */
2332 break;
2333 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2334 iRangeLeft -= cLines;
2335 else
2336 iRangeLeft -= 1;
2337 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
2338 if (RT_FAILURE(rc))
2339 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
2340 if (iRangeLeft <= 0)
2341 break;
2342 }
2343
2344 NOREF(pCmd);
2345 return 0;
2346}
2347
2348
2349/**
2350 * @callback_method_impl{FNDBGCCMD, The 'r' command.}
2351 */
2352static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2353{
2354 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2355 if (!pDbgc->fRegCtxGuest)
2356 return dbgcCmdRegHyper(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2357 return dbgcCmdRegGuest(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2358}
2359
2360
2361/**
2362 * @callback_method_impl{FNDBGCCMD, Common worker for the dbgcCmdReg*()
2363 * commands.}
2364 */
2365static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs,
2366 const char *pszPrefix)
2367{
2368 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2369 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1 || cArgs == 2 || cArgs == 3);
2370 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING
2371 || paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL);
2372
2373 /*
2374 * Parse the register name and kind.
2375 */
2376 const char *pszReg = paArgs[0].u.pszString;
2377 if (*pszReg == '@')
2378 pszReg++;
2379 VMCPUID idCpu = pDbgc->idCpu;
2380 if (*pszPrefix)
2381 idCpu |= DBGFREG_HYPER_VMCPUID;
2382 if (*pszReg == '.')
2383 {
2384 pszReg++;
2385 idCpu |= DBGFREG_HYPER_VMCPUID;
2386 }
2387 const char * const pszActualPrefix = idCpu & DBGFREG_HYPER_VMCPUID ? "." : "";
2388
2389 /*
2390 * Query the register type & value (the setter needs the type).
2391 */
2392 DBGFREGVALTYPE enmType;
2393 DBGFREGVAL Value;
2394 int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Value, &enmType);
2395 if (RT_FAILURE(rc))
2396 {
2397 if (rc == VERR_DBGF_REGISTER_NOT_FOUND)
2398 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown register: '%s%s'.\n",
2399 pszActualPrefix, pszReg);
2400 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmQuery failed querying '%s%s': %Rrc.\n",
2401 pszActualPrefix, pszReg, rc);
2402 }
2403 if (cArgs == 1)
2404 {
2405 /*
2406 * Show the register.
2407 */
2408 char szValue[160];
2409 rc = DBGFR3RegFormatValue(szValue, sizeof(szValue), &Value, enmType, true /*fSpecial*/);
2410 if (RT_SUCCESS(rc))
2411 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s%s=%s\n", pszActualPrefix, pszReg, szValue);
2412 else
2413 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2414 }
2415 else
2416 {
2417 DBGCVAR NewValueTmp;
2418 PCDBGCVAR pNewValue;
2419 if (cArgs == 3)
2420 {
2421 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, paArgs[1].enmType == DBGCVAR_TYPE_STRING);
2422 if (strcmp(paArgs[1].u.pszString, "="))
2423 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Second argument must be '='.");
2424 pNewValue = &paArgs[2];
2425 }
2426 else
2427 {
2428 /* Not possible to convince the parser to support both codeview and
2429 windbg syntax and make the equal sign optional. Try help it. */
2430 /** @todo make DBGCCmdHlpConvert do more with strings. */
2431 rc = DBGCCmdHlpConvert(pCmdHlp, &paArgs[1], DBGCVAR_TYPE_NUMBER, true /*fConvSyms*/, &NewValueTmp);
2432 if (RT_FAILURE(rc))
2433 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "The last argument must be a value or valid symbol.");
2434 pNewValue = &NewValueTmp;
2435 }
2436
2437 /*
2438 * Modify the register.
2439 */
2440 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pNewValue->enmType == DBGCVAR_TYPE_NUMBER);
2441 if (enmType != DBGFREGVALTYPE_DTR)
2442 {
2443 enmType = DBGFREGVALTYPE_U64;
2444 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.u64);
2445 }
2446 else
2447 {
2448 enmType = DBGFREGVALTYPE_DTR;
2449 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.dtr.u64Base);
2450 if (RT_SUCCESS(rc) && pNewValue->enmRangeType != DBGCVAR_RANGE_NONE)
2451 Value.dtr.u32Limit = (uint32_t)pNewValue->u64Range;
2452 }
2453 if (RT_SUCCESS(rc))
2454 {
2455 rc = DBGFR3RegNmSet(pUVM, idCpu, pszReg, &Value, enmType);
2456 if (RT_FAILURE(rc))
2457 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmSet failed settings '%s%s': %Rrc\n",
2458 pszActualPrefix, pszReg, rc);
2459 if (rc != VINF_SUCCESS)
2460 DBGCCmdHlpPrintf(pCmdHlp, "%s: warning: %Rrc\n", pCmd->pszCmd, rc);
2461 }
2462 else
2463 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2464 }
2465 return rc;
2466}
2467
2468
2469/**
2470 * @callback_method_impl{FNDBGCCMD,
2471 * The 'rg'\, 'rg64' and 'rg32' commands\, worker for 'r'.}
2472 */
2473static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2474{
2475 /*
2476 * Show all registers our selves.
2477 */
2478 if (cArgs == 0)
2479 {
2480 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2481 bool const f64BitMode = !strcmp(pCmd->pszCmd, "rg64")
2482 || ( strcmp(pCmd->pszCmd, "rg32") != 0
2483 && DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu));
2484 char szDisAndRegs[8192];
2485 int rc;
2486
2487 if (pDbgc->fRegTerse)
2488 {
2489 if (f64BitMode)
2490 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2491 "u %016VR{rip} L 0\n"
2492 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2493 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2494 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2495 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2496 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2497 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} rflags=%08VR{rflags}\n");
2498 else
2499 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2500 "u %04VR{cs}:%08VR{eip} L 0\n"
2501 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2502 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2503 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} eflags=%08VR{eflags}\n");
2504 }
2505 else
2506 {
2507 if (f64BitMode)
2508 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2509 "u %016VR{rip} L 0\n"
2510 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2511 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2512 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2513 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2514 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2515 "cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
2516 "ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
2517 "es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
2518 "fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
2519 "gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
2520 "ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2521 "dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
2522 "dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
2523 "gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
2524 "ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
2525 "tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
2526 " sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2527 " efer=%016VR{efer}\n"
2528 " pat=%016VR{pat}\n"
2529 " sf_mask=%016VR{sf_mask}\n"
2530 "krnl_gs_base=%016VR{krnl_gs_base}\n"
2531 " lstar=%016VR{lstar}\n"
2532 " star=%016VR{star} cstar=%016VR{cstar}\n"
2533 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2534 );
2535 else
2536 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2537 "u %04VR{cs}:%08VR{eip} L 0\n"
2538 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2539 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2540 "cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} dr0=%08VR{dr0} dr1=%08VR{dr1}\n"
2541 "ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} dr2=%08VR{dr2} dr3=%08VR{dr3}\n"
2542 "es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} dr6=%08VR{dr6} dr7=%08VR{dr7}\n"
2543 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr2}\n"
2544 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr3} cr4=%08VR{cr4}\n"
2545 "ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}} cr8=%08VR{cr8}\n"
2546 "gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} idtr=%08VR{idtr_base}:%04VR{idtr_lim} eflags=%08VR{eflags}\n"
2547 "ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2548 "tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2549 "sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2550 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2551 );
2552 }
2553 if (RT_FAILURE(rc))
2554 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2555 char *pszRegs = strchr(szDisAndRegs, '\n');
2556 *pszRegs++ = '\0';
2557 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2558
2559 /*
2560 * Disassemble one instruction at cs:[r|e]ip.
2561 */
2562 if (!f64BitMode && strstr(pszRegs, " vm ")) /* a bit ugly... */
2563 return pCmdHlp->pfnExec(pCmdHlp, "uv86 %s", szDisAndRegs + 2);
2564 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2565 }
2566 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, "");
2567}
2568
2569
2570/**
2571 * @callback_method_impl{FNDBGCCMD, The 'rh' command.}
2572 */
2573static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2574{
2575 /*
2576 * Show all registers our selves.
2577 */
2578 if (cArgs == 0)
2579 {
2580 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2581 char szDisAndRegs[8192];
2582 int rc;
2583
2584 if (pDbgc->fRegTerse)
2585 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2586 "u %VR{cs}:%VR{eip} L 0\n"
2587 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2588 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2589 ".cs=%04VR{cs} .ds=%04VR{ds} .es=%04VR{es} .fs=%04VR{fs} .gs=%04VR{gs} .ss=%04VR{ss} .eflags=%08VR{eflags}\n");
2590 else
2591 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2592 "u %04VR{cs}:%08VR{eip} L 0\n"
2593 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2594 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2595 ".cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} .dr0=%08VR{dr0} .dr1=%08VR{dr1}\n"
2596 ".ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} .dr2=%08VR{dr2} .dr3=%08VR{dr3}\n"
2597 ".es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} .dr6=%08VR{dr6} .dr6=%08VR{dr6}\n"
2598 ".fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} .cr3=%016VR{cr3}\n"
2599 ".gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}}\n"
2600 ".ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2601 ".gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} .idtr=%08VR{idtr_base}:%04VR{idtr_lim} .eflags=%08VR{eflags}\n"
2602 ".ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2603 ".tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2604 );
2605 if (RT_FAILURE(rc))
2606 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2607 char *pszRegs = strchr(szDisAndRegs, '\n');
2608 *pszRegs++ = '\0';
2609 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2610
2611 /*
2612 * Disassemble one instruction at cs:[r|e]ip.
2613 */
2614 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2615 }
2616 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, ".");
2617}
2618
2619
2620/**
2621 * @callback_method_impl{FNDBGCCMD, The 'rt' command.}
2622 */
2623static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2624{
2625 NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
2626
2627 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2628 pDbgc->fRegTerse = !pDbgc->fRegTerse;
2629 return DBGCCmdHlpPrintf(pCmdHlp, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
2630}
2631
2632
2633/**
2634 * @callback_method_impl{FNDBGCCMD, The 'pr' and 'tr' commands.}
2635 */
2636static DECLCALLBACK(int) dbgcCmdStepTraceToggle(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2637{
2638 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2639 Assert(cArgs == 0); NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
2640
2641 /* Note! windbg accepts 'r' as a flag to 'p', 'pa', 'pc', 'pt', 't',
2642 'ta', 'tc' and 'tt'. We've simplified it. */
2643 pDbgc->fStepTraceRegs = !pDbgc->fStepTraceRegs;
2644 return VINF_SUCCESS;
2645}
2646
2647
2648/**
2649 * @callback_method_impl{FNDBGCCMD, The 'p'\, 'pc'\, 'pt'\, 't'\, 'tc'\, and 'tt' commands.}
2650 */
2651static DECLCALLBACK(int) dbgcCmdStepTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2652{
2653 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2654 if (cArgs != 0)
2655 return DBGCCmdHlpFail(pCmdHlp, pCmd,
2656 "Sorry, but the '%s' command does not currently implement any arguments.\n", pCmd->pszCmd);
2657
2658 /* The 'count' has to be implemented by DBGC, whereas the
2659 filtering is taken care of by DBGF. */
2660
2661 /*
2662 * Convert the command to DBGF_STEP_F_XXX and other API input.
2663 */
2664 //DBGFADDRESS StackPop;
2665 PDBGFADDRESS pStackPop = NULL;
2666 RTGCPTR cbStackPop = 0;
2667 uint32_t cMaxSteps = pCmd->pszCmd[0] == 'p' ? _512K : _64K;
2668 uint32_t fFlags = pCmd->pszCmd[0] == 'p' ? DBGF_STEP_F_OVER : DBGF_STEP_F_INTO;
2669 if (pCmd->pszCmd[1] == 'c')
2670 fFlags |= DBGF_STEP_F_STOP_ON_CALL;
2671 else if (pCmd->pszCmd[1] == 't')
2672 fFlags |= DBGF_STEP_F_STOP_ON_RET;
2673 else if (pCmd->pszCmd[0] != 'p')
2674 cMaxSteps = 1;
2675 else
2676 {
2677 /** @todo consider passing RSP + 1 in for 'p' and something else sensible for
2678 * the 'pt' command. */
2679 }
2680
2681 int rc = DBGFR3StepEx(pUVM, pDbgc->idCpu, fFlags, NULL, pStackPop, cbStackPop, cMaxSteps);
2682 if (RT_SUCCESS(rc))
2683 pDbgc->fReady = false;
2684 else
2685 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3StepEx(,,%#x,) failed", fFlags);
2686
2687 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2688 return rc;
2689}
2690
2691
2692/**
2693 * @callback_method_impl{FNDBGCCMD, The 'pa' and 'ta' commands.}
2694 */
2695static DECLCALLBACK(int) dbgcCmdStepTraceTo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2696{
2697 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2698 if (cArgs != 1)
2699 return DBGCCmdHlpFail(pCmdHlp, pCmd,
2700 "Sorry, but the '%s' command only implements a single argument at present.\n", pCmd->pszCmd);
2701 DBGFADDRESS Address;
2702 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
2703 if (RT_FAILURE(rc))
2704 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", &paArgs[0]);
2705
2706 uint32_t cMaxSteps = pCmd->pszCmd[0] == 'p' ? _512K : 1;
2707 uint32_t fFlags = pCmd->pszCmd[0] == 'p' ? DBGF_STEP_F_OVER : DBGF_STEP_F_INTO;
2708 rc = DBGFR3StepEx(pUVM, pDbgc->idCpu, fFlags, &Address, NULL, 0, cMaxSteps);
2709 if (RT_SUCCESS(rc))
2710 pDbgc->fReady = false;
2711 else
2712 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3StepEx(,,%#x,) failed", fFlags);
2713 return rc;
2714}
2715
2716
2717/**
2718 * Helper that tries to resolve a far address to a symbol and formats it.
2719 *
2720 * @returns Pointer to symbol string on success, NULL if not resolved.
2721 * Free using RTStrFree.
2722 * @param pCmdHlp The command helper structure.
2723 * @param hAs The address space to use. NIL_RTDBGAS means no symbol resolving.
2724 * @param sel The selector part of the address.
2725 * @param off The offset part of the address.
2726 * @param pszPrefix How to prefix the symbol string.
2727 * @param pszSuffix How to suffix the symbol string.
2728 */
2729static char *dbgcCmdHlpFarAddrToSymbol(PDBGCCMDHLP pCmdHlp, RTDBGAS hAs, RTSEL sel, uint64_t off,
2730 const char *pszPrefix, const char *pszSuffix)
2731{
2732 char *pszRet = NULL;
2733 if (hAs != NIL_RTDBGAS)
2734 {
2735 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2736 DBGFADDRESS Addr;
2737 int rc = DBGFR3AddrFromSelOff(pDbgc->pUVM, pDbgc->idCpu, &Addr, sel, off);
2738 if (RT_SUCCESS(rc))
2739 {
2740 RTGCINTPTR offDispSym = 0;
2741 PRTDBGSYMBOL pSymbol = DBGFR3AsSymbolByAddrA(pDbgc->pUVM, hAs, &Addr,
2742 RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL
2743 | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
2744 &offDispSym, NULL);
2745 if (pSymbol)
2746 {
2747 if (offDispSym == 0)
2748 pszRet = RTStrAPrintf2("%s%s%s", pszPrefix, pSymbol->szName, pszSuffix);
2749 else if (offDispSym > 0)
2750 pszRet = RTStrAPrintf2("%s%s+%llx%s", pszPrefix, pSymbol->szName, (int64_t)offDispSym, pszSuffix);
2751 else
2752 pszRet = RTStrAPrintf2("%s%s-%llx%s", pszPrefix, pSymbol->szName, -(int64_t)offDispSym, pszSuffix);
2753 RTDbgSymbolFree(pSymbol);
2754 }
2755 }
2756 }
2757 return pszRet;
2758}
2759
2760
2761/**
2762 * @callback_method_impl{FNDBGCCMD, The 'k'\, 'kg' and 'kh' commands.}
2763 */
2764static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2765{
2766 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2767
2768 /*
2769 * Figure which context we're called for and start walking that stack.
2770 */
2771 int rc;
2772 PCDBGFSTACKFRAME pFirstFrame;
2773 bool const fGuest = pCmd->pszCmd[1] == 'g'
2774 || (pCmd->pszCmd[1] != 'h' && pDbgc->fRegCtxGuest);
2775 bool const fVerbose = pCmd->pszCmd[1] == 'v'
2776 || (pCmd->pszCmd[1] != '\0' && pCmd->pszCmd[2] == 'v');
2777 rc = DBGFR3StackWalkBegin(pUVM, pDbgc->idCpu, fGuest ? DBGFCODETYPE_GUEST : DBGFCODETYPE_HYPER, &pFirstFrame);
2778 if (RT_FAILURE(rc))
2779 return DBGCCmdHlpPrintf(pCmdHlp, "Failed to begin stack walk, rc=%Rrc\n", rc);
2780
2781 /*
2782 * Print the frames.
2783 */
2784 char szTmp[1024];
2785 uint32_t fBitFlags = 0;
2786 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
2787 pFrame;
2788 pFrame = DBGFR3StackWalkNext(pFrame))
2789 {
2790 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
2791 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
2792 {
2793 if (fCurBitFlags != fBitFlags)
2794 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "# SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2795 rc = DBGCCmdHlpPrintf(pCmdHlp, "%02x %04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2796 pFrame->iFrame,
2797 pFrame->AddrFrame.Sel,
2798 (uint16_t)pFrame->AddrFrame.off,
2799 pFrame->AddrReturnFrame.Sel,
2800 (uint16_t)pFrame->AddrReturnFrame.off,
2801 (uint32_t)pFrame->AddrReturnPC.Sel,
2802 (uint32_t)pFrame->AddrReturnPC.off,
2803 pFrame->Args.au32[0],
2804 pFrame->Args.au32[1],
2805 pFrame->Args.au32[2],
2806 pFrame->Args.au32[3]);
2807 }
2808 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
2809 {
2810 if (fCurBitFlags != fBitFlags)
2811 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "# EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2812 rc = DBGCCmdHlpPrintf(pCmdHlp, "%02x %08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2813 pFrame->iFrame,
2814 (uint32_t)pFrame->AddrFrame.off,
2815 (uint32_t)pFrame->AddrReturnFrame.off,
2816 (uint32_t)pFrame->AddrReturnPC.Sel,
2817 (uint32_t)pFrame->AddrReturnPC.off,
2818 pFrame->Args.au32[0],
2819 pFrame->Args.au32[1],
2820 pFrame->Args.au32[2],
2821 pFrame->Args.au32[3]);
2822 }
2823 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
2824 {
2825 if (fCurBitFlags != fBitFlags)
2826 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "# RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
2827 rc = DBGCCmdHlpPrintf(pCmdHlp, "%02x %016RX64 %04RX16:%016RX64 %016RX64",
2828 pFrame->iFrame,
2829 (uint64_t)pFrame->AddrFrame.off,
2830 pFrame->AddrReturnFrame.Sel,
2831 (uint64_t)pFrame->AddrReturnFrame.off,
2832 (uint64_t)pFrame->AddrReturnPC.off);
2833 }
2834 if (RT_FAILURE(rc))
2835 break;
2836 if (!pFrame->pSymPC)
2837 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2838 fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
2839 ? " %RTsel:%016RGv"
2840 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT
2841 ? " %RTsel:%08RGv"
2842 : " %RTsel:%04RGv"
2843 , pFrame->AddrPC.Sel, pFrame->AddrPC.off);
2844 else
2845 {
2846 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
2847 if (offDisp > 0)
2848 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
2849 else if (offDisp < 0)
2850 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
2851 else
2852 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", pFrame->pSymPC->szName);
2853 }
2854 if (RT_SUCCESS(rc) && pFrame->pLinePC)
2855 rc = DBGCCmdHlpPrintf(pCmdHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
2856 if (RT_SUCCESS(rc))
2857 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2858
2859 if (fVerbose && RT_SUCCESS(rc))
2860 {
2861 /*
2862 * Display verbose frame info.
2863 */
2864 const char *pszRetType = "invalid";
2865 switch (pFrame->enmReturnType)
2866 {
2867 case RTDBGRETURNTYPE_NEAR16: pszRetType = "retn/16"; break;
2868 case RTDBGRETURNTYPE_NEAR32: pszRetType = "retn/32"; break;
2869 case RTDBGRETURNTYPE_NEAR64: pszRetType = "retn/64"; break;
2870 case RTDBGRETURNTYPE_FAR16: pszRetType = "retf/16"; break;
2871 case RTDBGRETURNTYPE_FAR32: pszRetType = "retf/32"; break;
2872 case RTDBGRETURNTYPE_FAR64: pszRetType = "retf/64"; break;
2873 case RTDBGRETURNTYPE_IRET16: pszRetType = "iret-16"; break;
2874 case RTDBGRETURNTYPE_IRET32: pszRetType = "iret/32s"; break;
2875 case RTDBGRETURNTYPE_IRET32_PRIV: pszRetType = "iret/32p"; break;
2876 case RTDBGRETURNTYPE_IRET32_V86: pszRetType = "iret/v86"; break;
2877 case RTDBGRETURNTYPE_IRET64: pszRetType = "iret/64"; break;
2878
2879 case RTDBGRETURNTYPE_END:
2880 case RTDBGRETURNTYPE_INVALID:
2881 case RTDBGRETURNTYPE_32BIT_HACK:
2882 break;
2883 }
2884 size_t cchLine = DBGCCmdHlpPrintfLen(pCmdHlp, " %s", pszRetType);
2885 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO)
2886 cchLine += DBGCCmdHlpPrintfLen(pCmdHlp, " used-unwind-info");
2887 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_ODD_EVEN)
2888 cchLine += DBGCCmdHlpPrintfLen(pCmdHlp, " used-odd-even");
2889 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_REAL_V86)
2890 cchLine += DBGCCmdHlpPrintfLen(pCmdHlp, " real-v86");
2891 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_MAX_DEPTH)
2892 cchLine += DBGCCmdHlpPrintfLen(pCmdHlp, " max-depth");
2893 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_TRAP_FRAME)
2894 cchLine += DBGCCmdHlpPrintfLen(pCmdHlp, " trap-frame");
2895
2896 if (pFrame->cSureRegs > 0)
2897 {
2898 cchLine = 1024; /* force new line */
2899 for (uint32_t i = 0; i < pFrame->cSureRegs; i++)
2900 {
2901 if (cchLine > 80)
2902 {
2903 DBGCCmdHlpPrintf(pCmdHlp, "\n ");
2904 cchLine = 2;
2905 }
2906
2907 szTmp[0] = '\0';
2908 DBGFR3RegFormatValue(szTmp, sizeof(szTmp), &pFrame->paSureRegs[i].Value,
2909 pFrame->paSureRegs[i].enmType, false);
2910 const char *pszName = pFrame->paSureRegs[i].enmReg != DBGFREG_END
2911 ? DBGFR3RegCpuName(pUVM, pFrame->paSureRegs[i].enmReg, pFrame->paSureRegs[i].enmType)
2912 : pFrame->paSureRegs[i].pszName;
2913 cchLine += DBGCCmdHlpPrintfLen(pCmdHlp, " %s=%s", pszName, szTmp);
2914 }
2915 }
2916
2917 if (RT_SUCCESS(rc))
2918 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2919 }
2920
2921 if (RT_FAILURE(rc))
2922 break;
2923
2924 fBitFlags = fCurBitFlags;
2925 }
2926
2927 DBGFR3StackWalkEnd(pFirstFrame);
2928
2929 NOREF(paArgs); NOREF(cArgs);
2930 return rc;
2931}
2932
2933
2934/**
2935 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
2936 *
2937 * @returns pfnPrintf status code.
2938 * @param pCmdHlp The DBGC command helpers.
2939 * @param pDesc The descriptor to display.
2940 * @param iEntry The descriptor entry number.
2941 * @param fHyper Whether the selector belongs to the hypervisor or not.
2942 * @param hAs Address space to use when resolving symbols.
2943 * @param pfDblEntry Where to indicate whether the entry is two entries wide.
2944 * Optional.
2945 */
2946static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP pCmdHlp, PCX86DESC64 pDesc, unsigned iEntry, bool fHyper, RTDBGAS hAs,
2947 bool *pfDblEntry)
2948{
2949 /* GUEST64 */
2950 int rc;
2951
2952 const char *pszHyper = fHyper ? " HYPER" : "";
2953 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2954 if (pDesc->Gen.u1DescType)
2955 {
2956 static const char * const s_apszTypes[] =
2957 {
2958 "DataRO", /* 0 Read-Only */
2959 "DataRO", /* 1 Read-Only - Accessed */
2960 "DataRW", /* 2 Read/Write */
2961 "DataRW", /* 3 Read/Write - Accessed */
2962 "DownRO", /* 4 Expand-down, Read-Only */
2963 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2964 "DownRW", /* 6 Expand-down, Read/Write */
2965 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2966 "CodeEO", /* 8 Execute-Only */
2967 "CodeEO", /* 9 Execute-Only - Accessed */
2968 "CodeER", /* A Execute/Readable */
2969 "CodeER", /* B Execute/Readable - Accessed */
2970 "ConfE0", /* C Conforming, Execute-Only */
2971 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2972 "ConfER", /* E Conforming, Execute/Readable */
2973 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2974 };
2975 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2976 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2977 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2978 uint32_t u32Base = X86DESC_BASE(pDesc);
2979 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2980
2981 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2982 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2983 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2984 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2985 }
2986 else
2987 {
2988 static const char * const s_apszTypes[] =
2989 {
2990 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
2991 "Ill-1 ", /* 1 0001 Available 16-bit TSS */
2992 "LDT ", /* 2 0010 LDT */
2993 "Ill-3 ", /* 3 0011 Busy 16-bit TSS */
2994 "Ill-4 ", /* 4 0100 16-bit Call Gate */
2995 "Ill-5 ", /* 5 0101 Task Gate */
2996 "Ill-6 ", /* 6 0110 16-bit Interrupt Gate */
2997 "Ill-7 ", /* 7 0111 16-bit Trap Gate */
2998 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
2999 "Tss64A", /* 9 1001 Available 32-bit TSS */
3000 "Ill-A ", /* A 1010 Reserved (Illegal) */
3001 "Tss64B", /* B 1011 Busy 32-bit TSS */
3002 "Call64", /* C 1100 32-bit Call Gate */
3003 "Ill-D ", /* D 1101 Reserved (Illegal) */
3004 "Int64 ", /* E 1110 32-bit Interrupt Gate */
3005 "Trap64" /* F 1111 32-bit Trap Gate */
3006 };
3007 switch (pDesc->Gen.u4Type)
3008 {
3009 /* raw */
3010 case X86_SEL_TYPE_SYS_UNDEFINED:
3011 case X86_SEL_TYPE_SYS_UNDEFINED2:
3012 case X86_SEL_TYPE_SYS_UNDEFINED4:
3013 case X86_SEL_TYPE_SYS_UNDEFINED3:
3014 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
3015 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
3016 case X86_SEL_TYPE_SYS_286_CALL_GATE:
3017 case X86_SEL_TYPE_SYS_286_INT_GATE:
3018 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
3019 case X86_SEL_TYPE_SYS_TASK_GATE:
3020 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
3021 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
3022 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
3023 break;
3024
3025 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
3026 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
3027 case X86_SEL_TYPE_SYS_LDT:
3028 {
3029 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
3030 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
3031 const char *pszLong = pDesc->Gen.u1Long ? "LONG" : " ";
3032
3033 uint64_t u64Base = X86DESC64_BASE(pDesc);
3034 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
3035
3036 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%016RX64 Lim=%08x DPL=%d %s %s %s %sAVL=%d R=%d%s\n",
3037 iEntry, s_apszTypes[pDesc->Gen.u4Type], u64Base, cbLimit,
3038 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszLong, pszBig,
3039 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
3040 pszHyper);
3041 if (pfDblEntry)
3042 *pfDblEntry = true;
3043 break;
3044 }
3045
3046 case X86_SEL_TYPE_SYS_386_CALL_GATE:
3047 {
3048 unsigned cParams = pDesc->au8[4] & 0x1f;
3049 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
3050 RTSEL sel = pDesc->au16[1];
3051 uint64_t off = pDesc->au16[0]
3052 | ((uint64_t)pDesc->au16[3] << 16)
3053 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
3054 char *pszSymbol = dbgcCmdHlpFarAddrToSymbol(pCmdHlp, hAs, sel, off, " (", ")");
3055 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s %s=%d%s%s\n",
3056 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
3057 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper, pszSymbol ? pszSymbol : "");
3058 RTStrFree(pszSymbol);
3059 if (pfDblEntry)
3060 *pfDblEntry = true;
3061 break;
3062 }
3063
3064 case X86_SEL_TYPE_SYS_386_INT_GATE:
3065 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
3066 {
3067 RTSEL sel = pDesc->Gate.u16Sel;
3068 uint64_t off = pDesc->Gate.u16OffsetLow
3069 | ((uint64_t)pDesc->Gate.u16OffsetHigh << 16)
3070 | ((uint64_t)pDesc->Gate.u32OffsetTop << 32);
3071 char *pszSymbol = dbgcCmdHlpFarAddrToSymbol(pCmdHlp, hAs, sel, off, " (", ")");
3072 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%u %s IST=%u%s%s\n",
3073 iEntry, s_apszTypes[pDesc->Gate.u4Type], sel, off,
3074 pDesc->Gate.u2Dpl, pszPresent, pDesc->Gate.u3IST, pszHyper, pszSymbol ? pszSymbol : "");
3075 RTStrFree(pszSymbol);
3076 if (pfDblEntry)
3077 *pfDblEntry = true;
3078 break;
3079 }
3080
3081 /* impossible, just it's necessary to keep gcc happy. */
3082 default:
3083 return VINF_SUCCESS;
3084 }
3085 }
3086 return VINF_SUCCESS;
3087}
3088
3089
3090/**
3091 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
3092 *
3093 * @returns pfnPrintf status code.
3094 * @param pCmdHlp The DBGC command helpers.
3095 * @param pDesc The descriptor to display.
3096 * @param iEntry The descriptor entry number.
3097 * @param fHyper Whether the selector belongs to the hypervisor or not.
3098 * @param hAs Address space to use when resolving symbols.
3099 */
3100static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper, RTDBGAS hAs)
3101{
3102 int rc;
3103
3104 const char *pszHyper = fHyper ? " HYPER" : "";
3105 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
3106 if (pDesc->Gen.u1DescType)
3107 {
3108 static const char * const s_apszTypes[] =
3109 {
3110 "DataRO", /* 0 Read-Only */
3111 "DataRO", /* 1 Read-Only - Accessed */
3112 "DataRW", /* 2 Read/Write */
3113 "DataRW", /* 3 Read/Write - Accessed */
3114 "DownRO", /* 4 Expand-down, Read-Only */
3115 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
3116 "DownRW", /* 6 Expand-down, Read/Write */
3117 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
3118 "CodeEO", /* 8 Execute-Only */
3119 "CodeEO", /* 9 Execute-Only - Accessed */
3120 "CodeER", /* A Execute/Readable */
3121 "CodeER", /* B Execute/Readable - Accessed */
3122 "ConfE0", /* C Conforming, Execute-Only */
3123 "ConfE0", /* D Conforming, Execute-Only - Accessed */
3124 "ConfER", /* E Conforming, Execute/Readable */
3125 "ConfER" /* F Conforming, Execute/Readable - Accessed */
3126 };
3127 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
3128 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
3129 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
3130 uint32_t u32Base = pDesc->Gen.u16BaseLow
3131 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
3132 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
3133 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
3134 if (pDesc->Gen.u1Granularity)
3135 cbLimit <<= PAGE_SHIFT;
3136
3137 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
3138 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
3139 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
3140 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
3141 }
3142 else
3143 {
3144 static const char * const s_apszTypes[] =
3145 {
3146 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
3147 "Tss16A", /* 1 0001 Available 16-bit TSS */
3148 "LDT ", /* 2 0010 LDT */
3149 "Tss16B", /* 3 0011 Busy 16-bit TSS */
3150 "Call16", /* 4 0100 16-bit Call Gate */
3151 "TaskG ", /* 5 0101 Task Gate */
3152 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
3153 "Trap16", /* 7 0111 16-bit Trap Gate */
3154 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
3155 "Tss32A", /* 9 1001 Available 32-bit TSS */
3156 "Ill-A ", /* A 1010 Reserved (Illegal) */
3157 "Tss32B", /* B 1011 Busy 32-bit TSS */
3158 "Call32", /* C 1100 32-bit Call Gate */
3159 "Ill-D ", /* D 1101 Reserved (Illegal) */
3160 "Int32 ", /* E 1110 32-bit Interrupt Gate */
3161 "Trap32" /* F 1111 32-bit Trap Gate */
3162 };
3163 switch (pDesc->Gen.u4Type)
3164 {
3165 /* raw */
3166 case X86_SEL_TYPE_SYS_UNDEFINED:
3167 case X86_SEL_TYPE_SYS_UNDEFINED2:
3168 case X86_SEL_TYPE_SYS_UNDEFINED4:
3169 case X86_SEL_TYPE_SYS_UNDEFINED3:
3170 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
3171 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
3172 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
3173 break;
3174
3175 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
3176 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
3177 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
3178 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
3179 case X86_SEL_TYPE_SYS_LDT:
3180 {
3181 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
3182 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
3183 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
3184 uint32_t u32Base = pDesc->Gen.u16BaseLow
3185 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
3186 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
3187 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
3188 if (pDesc->Gen.u1Granularity)
3189 cbLimit <<= PAGE_SHIFT;
3190
3191 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
3192 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
3193 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
3194 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
3195 pszHyper);
3196 break;
3197 }
3198
3199 case X86_SEL_TYPE_SYS_TASK_GATE:
3200 {
3201 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s TSS=%04x DPL=%d %s%s\n",
3202 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
3203 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
3204 break;
3205 }
3206
3207 case X86_SEL_TYPE_SYS_286_CALL_GATE:
3208 case X86_SEL_TYPE_SYS_386_CALL_GATE:
3209 {
3210 unsigned cParams = pDesc->au8[4] & 0x1f;
3211 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
3212 RTSEL sel = pDesc->au16[1];
3213 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
3214 char *pszSymbol = dbgcCmdHlpFarAddrToSymbol(pCmdHlp, hAs, sel, off, " (", ")");
3215 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s%s\n",
3216 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
3217 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper, pszSymbol ? pszSymbol : "");
3218 RTStrFree(pszSymbol);
3219 break;
3220 }
3221
3222 case X86_SEL_TYPE_SYS_286_INT_GATE:
3223 case X86_SEL_TYPE_SYS_386_INT_GATE:
3224 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
3225 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
3226 {
3227 RTSEL sel = pDesc->au16[1];
3228 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
3229 char *pszSymbol = dbgcCmdHlpFarAddrToSymbol(pCmdHlp, hAs, sel, off, " (", ")");
3230 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s%s\n",
3231 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
3232 pDesc->Gen.u2Dpl, pszPresent, pszHyper, pszSymbol ? pszSymbol : "");
3233 RTStrFree(pszSymbol);
3234 break;
3235 }
3236
3237 /* impossible, just it's necessary to keep gcc happy. */
3238 default:
3239 return VINF_SUCCESS;
3240 }
3241 }
3242 return rc;
3243}
3244
3245
3246/**
3247 * @callback_method_impl{FNDBGCCMD, The 'dg'\, 'dga'\, 'dl' and 'dla' commands.}
3248 */
3249static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3250{
3251 /*
3252 * Validate input.
3253 */
3254 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3255
3256 /*
3257 * Get the CPU mode, check which command variation this is
3258 * and fix a default parameter if needed.
3259 */
3260 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3261 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
3262 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
3263 bool fGdt = pCmd->pszCmd[1] == 'g';
3264 bool fAll = pCmd->pszCmd[2] == 'a';
3265 RTSEL SelTable = fGdt ? 0 : X86_SEL_LDT;
3266
3267 DBGCVAR Var;
3268 if (!cArgs)
3269 {
3270 cArgs = 1;
3271 paArgs = &Var;
3272 Var.enmType = DBGCVAR_TYPE_NUMBER;
3273 Var.u.u64Number = 0;
3274 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
3275 Var.u64Range = 1024;
3276 }
3277
3278 /*
3279 * Process the arguments.
3280 */
3281 for (unsigned i = 0; i < cArgs; i++)
3282 {
3283 /*
3284 * Retrieve the selector value from the argument.
3285 * The parser may confuse pointers and numbers if more than one
3286 * argument is given, that that into account.
3287 */
3288 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[i].enmType));
3289 uint64_t u64;
3290 unsigned cSels = 1;
3291 switch (paArgs[i].enmType)
3292 {
3293 case DBGCVAR_TYPE_NUMBER:
3294 u64 = paArgs[i].u.u64Number;
3295 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
3296 cSels = RT_MIN(paArgs[i].u64Range, 1024);
3297 break;
3298 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
3299 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
3300 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
3301 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
3302 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
3303 default: u64 = _64K; break;
3304 }
3305 if (u64 < _64K)
3306 {
3307 unsigned Sel = (RTSEL)u64;
3308
3309 /*
3310 * Dump the specified range.
3311 */
3312 bool fSingle = cSels == 1;
3313 while ( cSels-- > 0
3314 && Sel < _64K)
3315 {
3316 DBGFSELINFO SelInfo;
3317 int rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, Sel | SelTable, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
3318 if (RT_SUCCESS(rc))
3319 {
3320 if (SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE)
3321 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x RealM Bas=%04x Lim=%04x\n",
3322 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
3323 else if ( fAll
3324 || fSingle
3325 || SelInfo.u.Raw.Gen.u1Present)
3326 {
3327 if (enmMode == CPUMMODE_PROTECTED)
3328 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &SelInfo.u.Raw, Sel,
3329 !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), DBGF_AS_GLOBAL);
3330 else
3331 {
3332 bool fDblSkip = false;
3333 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &SelInfo.u.Raw64, Sel,
3334 !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), DBGF_AS_GLOBAL, &fDblSkip);
3335 if (fDblSkip)
3336 Sel += 4;
3337 }
3338 }
3339 }
3340 else
3341 {
3342 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %Rrc\n", Sel, rc);
3343 if (!fAll)
3344 return rc;
3345 }
3346 if (RT_FAILURE(rc))
3347 return rc;
3348
3349 /* next */
3350 Sel += 8;
3351 }
3352 }
3353 else
3354 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds\n", u64);
3355 }
3356
3357 return VINF_SUCCESS;
3358}
3359
3360
3361/**
3362 * @callback_method_impl{FNDBGCCMD, The 'di' and 'dia' commands.}
3363 */
3364static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3365{
3366 /*
3367 * Validate input.
3368 */
3369 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3370
3371 /*
3372 * Establish some stuff like the current IDTR and CPU mode,
3373 * and fix a default parameter.
3374 */
3375 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3376 CPUMMODE enmMode = DBGCCmdHlpGetCpuMode(pCmdHlp);
3377 uint16_t cbLimit = 0;
3378 uint64_t GCFlat = 0;
3379 int rc = DBGFR3RegCpuQueryXdtr(pDbgc->pUVM, pDbgc->idCpu, DBGFREG_IDTR, &GCFlat, &cbLimit);
3380 if (RT_FAILURE(rc))
3381 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3RegCpuQueryXdtr/DBGFREG_IDTR");
3382 unsigned cbEntry;
3383 switch (enmMode)
3384 {
3385 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
3386 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
3387 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
3388 default:
3389 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid CPU mode %d.\n", enmMode);
3390 }
3391
3392 bool fAll = pCmd->pszCmd[2] == 'a';
3393 DBGCVAR Var;
3394 if (!cArgs)
3395 {
3396 cArgs = 1;
3397 paArgs = &Var;
3398 Var.enmType = DBGCVAR_TYPE_NUMBER;
3399 Var.u.u64Number = 0;
3400 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
3401 Var.u64Range = 256;
3402 }
3403
3404 /*
3405 * Process the arguments.
3406 */
3407 for (unsigned i = 0; i < cArgs; i++)
3408 {
3409 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER);
3410 if (paArgs[i].u.u64Number < 256)
3411 {
3412 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
3413 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
3414 ? paArgs[i].u64Range
3415 : 1;
3416 bool fSingle = cInts == 1;
3417 while ( cInts-- > 0
3418 && iInt < 256)
3419 {
3420 /*
3421 * Try read it.
3422 */
3423 union
3424 {
3425 RTFAR16 Real;
3426 X86DESC Prot;
3427 X86DESC64 Long;
3428 } u;
3429 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
3430 {
3431 DBGCCmdHlpPrintf(pCmdHlp, "%04x not within the IDT\n", (unsigned)iInt);
3432 if (!fAll && !fSingle)
3433 return VINF_SUCCESS;
3434 }
3435 DBGCVAR AddrVar;
3436 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
3437 AddrVar.u.GCFlat = GCFlat + iInt * cbEntry;
3438 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
3439 rc = pCmdHlp->pfnMemRead(pCmdHlp, &u, cbEntry, &AddrVar, NULL);
3440 if (RT_FAILURE(rc))
3441 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
3442
3443 /*
3444 * Display it.
3445 */
3446 switch (enmMode)
3447 {
3448 case CPUMMODE_REAL:
3449 {
3450 char *pszSymbol = dbgcCmdHlpFarAddrToSymbol(pCmdHlp, DBGF_AS_GLOBAL, u.Real.sel, u.Real.off, " (", ")");
3451 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %RTfp16%s\n", (unsigned)iInt, u.Real, pszSymbol ? pszSymbol : "");
3452 RTStrFree(pszSymbol);
3453 break;
3454 }
3455 case CPUMMODE_PROTECTED:
3456 if (fAll || fSingle || u.Prot.Gen.u1Present)
3457 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false, DBGF_AS_GLOBAL);
3458 break;
3459 case CPUMMODE_LONG:
3460 if (fAll || fSingle || u.Long.Gen.u1Present)
3461 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, DBGF_AS_GLOBAL, NULL);
3462 break;
3463 default: break; /* to shut up gcc */
3464 }
3465 if (RT_FAILURE(rc))
3466 return rc;
3467
3468 /* next */
3469 iInt++;
3470 }
3471 }
3472 else
3473 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
3474 }
3475
3476 return VINF_SUCCESS;
3477}
3478
3479
3480/**
3481 * @callback_method_impl{FNDBGCCMD,
3482 * The 'da'\, 'dq'\, 'dqs'\, 'dd'\, 'dds'\, 'dw'\, 'db'\, 'dp'\, 'dps'\,
3483 * and 'du' commands.}
3484 */
3485static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3486{
3487 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3488
3489 /*
3490 * Validate input.
3491 */
3492 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3493 if (cArgs == 1)
3494 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3495 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3496
3497#define DBGC_DUMP_MEM_F_ASCII RT_BIT_32(31)
3498#define DBGC_DUMP_MEM_F_UNICODE RT_BIT_32(30)
3499#define DBGC_DUMP_MEM_F_FAR RT_BIT_32(29)
3500#define DBGC_DUMP_MEM_F_SYMBOLS RT_BIT_32(28)
3501#define DBGC_DUMP_MEM_F_SIZE UINT32_C(0x0000ffff)
3502
3503 /*
3504 * Figure out the element size.
3505 */
3506 unsigned cbElement;
3507 bool fAscii = false;
3508 bool fUnicode = false;
3509 bool fFar = false;
3510 bool fSymbols = pCmd->pszCmd[1] && pCmd->pszCmd[2] == 's';
3511 switch (pCmd->pszCmd[1])
3512 {
3513 default:
3514 case 'b': cbElement = 1; break;
3515 case 'w': cbElement = 2; break;
3516 case 'd': cbElement = 4; break;
3517 case 'q': cbElement = 8; break;
3518 case 'a':
3519 cbElement = 1;
3520 fAscii = true;
3521 break;
3522 case 'F':
3523 cbElement = 4;
3524 fFar = true;
3525 break;
3526 case 'p':
3527 cbElement = DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu) ? 8 : 4;
3528 break;
3529 case 'u':
3530 cbElement = 2;
3531 fUnicode = true;
3532 break;
3533 case '\0':
3534 fAscii = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_ASCII);
3535 fSymbols = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_SYMBOLS);
3536 fUnicode = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_UNICODE);
3537 fFar = RT_BOOL(pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_FAR);
3538 cbElement = pDbgc->cbDumpElement & DBGC_DUMP_MEM_F_SIZE;
3539 if (!cbElement)
3540 cbElement = 1;
3541 break;
3542 }
3543 uint32_t const cbDumpElement = cbElement
3544 | (fSymbols ? DBGC_DUMP_MEM_F_SYMBOLS : 0)
3545 | (fFar ? DBGC_DUMP_MEM_F_FAR : 0)
3546 | (fUnicode ? DBGC_DUMP_MEM_F_UNICODE : 0)
3547 | (fAscii ? DBGC_DUMP_MEM_F_ASCII : 0);
3548 pDbgc->cbDumpElement = cbDumpElement;
3549
3550 /*
3551 * Find address.
3552 */
3553 if (!cArgs)
3554 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
3555 else
3556 pDbgc->DumpPos = paArgs[0];
3557
3558 /*
3559 * Range.
3560 */
3561 switch (pDbgc->DumpPos.enmRangeType)
3562 {
3563 case DBGCVAR_RANGE_NONE:
3564 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3565 pDbgc->DumpPos.u64Range = 0x60;
3566 break;
3567
3568 case DBGCVAR_RANGE_ELEMENTS:
3569 if (pDbgc->DumpPos.u64Range > 2048)
3570 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many elements requested. Max is 2048 elements.\n");
3571 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3572 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
3573 break;
3574
3575 case DBGCVAR_RANGE_BYTES:
3576 if (pDbgc->DumpPos.u64Range > 65536)
3577 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
3578 break;
3579
3580 default:
3581 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
3582 }
3583
3584 pDbgc->pLastPos = &pDbgc->DumpPos;
3585
3586 /*
3587 * Do the dumping.
3588 */
3589 int cbLeft = (int)pDbgc->DumpPos.u64Range;
3590 uint8_t u16Prev = '\0';
3591 for (;;)
3592 {
3593 /*
3594 * Read memory.
3595 */
3596 char achBuffer[16];
3597 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
3598 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
3599 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
3600 if (RT_FAILURE(rc))
3601 {
3602 if (u16Prev && u16Prev != '\n')
3603 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3604 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
3605 }
3606
3607 /*
3608 * Display it.
3609 */
3610 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
3611 if (!fAscii && !fUnicode)
3612 {
3613 DBGCCmdHlpPrintf(pCmdHlp, "%DV:", &pDbgc->DumpPos);
3614 unsigned i;
3615 for (i = 0; i < cb; i += cbElement)
3616 {
3617 const char *pszSpace = " ";
3618 if (cbElement <= 2 && i == 8)
3619 pszSpace = "-";
3620 switch (cbElement)
3621 {
3622 case 1:
3623 DBGCCmdHlpPrintf(pCmdHlp, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]);
3624 break;
3625 case 2:
3626 DBGCCmdHlpPrintf(pCmdHlp, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]);
3627 break;
3628 case 4:
3629 if (!fFar)
3630 DBGCCmdHlpPrintf(pCmdHlp, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]);
3631 else
3632 DBGCCmdHlpPrintf(pCmdHlp, "%s%04x:%04x:",
3633 pszSpace, *(uint16_t *)&achBuffer[i + 2], *(uint16_t *)&achBuffer[i]);
3634 break;
3635 case 8:
3636 DBGCCmdHlpPrintf(pCmdHlp, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]);
3637 break;
3638 }
3639
3640 if (fSymbols)
3641 {
3642 /* Try lookup symbol for the above address. */
3643 DBGFADDRESS Addr;
3644 rc = VINF_SUCCESS;
3645 if (cbElement == 8)
3646 DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, *(uint64_t *)&achBuffer[i]);
3647 else if (!fFar)
3648 DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, *(uint32_t *)&achBuffer[i]);
3649 else
3650 rc = DBGFR3AddrFromSelOff(pDbgc->pUVM, pDbgc->idCpu, &Addr,
3651 *(uint16_t *)&achBuffer[i + 2], *(uint16_t *)&achBuffer[i]);
3652 if (RT_SUCCESS(rc))
3653 {
3654 RTINTPTR offDisp;
3655 RTDBGSYMBOL Symbol;
3656 rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, &Addr,
3657 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
3658 &offDisp, &Symbol, NULL);
3659 if (RT_SUCCESS(rc))
3660 {
3661 if (!offDisp)
3662 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", Symbol.szName);
3663 else if (offDisp > 0)
3664 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s + %RGv", Symbol.szName, offDisp);
3665 else
3666 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s - %RGv", Symbol.szName, -offDisp);
3667 if (Symbol.cb > 0)
3668 rc = DBGCCmdHlpPrintf(pCmdHlp, " (LB %RGv)", Symbol.cb);
3669 }
3670 }
3671
3672 /* Next line prefix. */
3673 unsigned iNext = i + cbElement;
3674 if (iNext < cb)
3675 {
3676 DBGCVAR TmpPos = pDbgc->DumpPos;
3677 DBGCCmdHlpEval(pCmdHlp, &TmpPos, "(%Dv) + %x", &pDbgc->DumpPos, iNext);
3678 DBGCCmdHlpPrintf(pCmdHlp, "\n%DV:", &pDbgc->DumpPos);
3679 }
3680 }
3681 }
3682
3683 /* Chars column. */
3684 if (cbElement == 1)
3685 {
3686 while (i++ < sizeof(achBuffer))
3687 DBGCCmdHlpPrintf(pCmdHlp, " ");
3688 DBGCCmdHlpPrintf(pCmdHlp, " ");
3689 for (i = 0; i < cb; i += cbElement)
3690 {
3691 uint8_t u8 = *(uint8_t *)&achBuffer[i];
3692 if (RT_C_IS_PRINT(u8) && u8 < 127 && u8 >= 32)
3693 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
3694 else
3695 DBGCCmdHlpPrintf(pCmdHlp, ".");
3696 }
3697 }
3698 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3699 }
3700 else
3701 {
3702 /*
3703 * We print up to the first zero and stop there.
3704 * Only printables + '\t' and '\n' are printed.
3705 */
3706 if (!u16Prev)
3707 DBGCCmdHlpPrintf(pCmdHlp, "%DV:\n", &pDbgc->DumpPos);
3708 uint16_t u16 = '\0';
3709 unsigned i;
3710 for (i = 0; i < cb; i += cbElement)
3711 {
3712 u16Prev = u16;
3713 if (cbElement == 1)
3714 u16 = *(uint8_t *)&achBuffer[i];
3715 else
3716 u16 = *(uint16_t *)&achBuffer[i];
3717 if ( u16 < 127
3718 && ( (RT_C_IS_PRINT(u16) && u16 >= 32)
3719 || u16 == '\t'
3720 || u16 == '\n'))
3721 DBGCCmdHlpPrintf(pCmdHlp, "%c", (int)u16);
3722 else if (!u16)
3723 break;
3724 else
3725 DBGCCmdHlpPrintf(pCmdHlp, "\\x%0*x", cbElement * 2, u16);
3726 }
3727 if (u16 == '\0')
3728 cb = cbLeft = i + 1;
3729 if (cbLeft - cb <= 0 && u16Prev != '\n')
3730 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3731 }
3732
3733 /*
3734 * Advance
3735 */
3736 cbLeft -= (int)cb;
3737 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
3738 if (RT_FAILURE(rc))
3739 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
3740 if (cbLeft <= 0)
3741 break;
3742 }
3743
3744 NOREF(pCmd);
3745 return VINF_SUCCESS;
3746}
3747
3748
3749/**
3750 * Best guess at which paging mode currently applies to the guest
3751 * paging structures.
3752 *
3753 * This have to come up with a decent answer even when the guest
3754 * is in non-paged protected mode or real mode.
3755 *
3756 * @returns cr3.
3757 * @param pDbgc The DBGC instance.
3758 * @param pfPAE Where to store the page address extension indicator.
3759 * @param pfLME Where to store the long mode enabled indicator.
3760 * @param pfPSE Where to store the page size extension indicator.
3761 * @param pfPGE Where to store the page global enabled indicator.
3762 * @param pfNXE Where to store the no-execution enabled indicator.
3763 */
3764static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3765{
3766 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3767 RTGCUINTREG cr4 = CPUMGetGuestCR4(pVCpu);
3768 *pfPSE = !!(cr4 & X86_CR4_PSE);
3769 *pfPGE = !!(cr4 & X86_CR4_PGE);
3770 if (cr4 & X86_CR4_PAE)
3771 {
3772 *pfPSE = true;
3773 *pfPAE = true;
3774 }
3775 else
3776 *pfPAE = false;
3777
3778 *pfLME = CPUMGetGuestMode(pVCpu) == CPUMMODE_LONG;
3779 *pfNXE = false; /* GUEST64 GUESTNX */
3780 return CPUMGetGuestCR3(pVCpu);
3781}
3782
3783
3784/**
3785 * Determine the shadow paging mode.
3786 *
3787 * @returns cr3.
3788 * @param pDbgc The DBGC instance.
3789 * @param pfPAE Where to store the page address extension indicator.
3790 * @param pfLME Where to store the long mode enabled indicator.
3791 * @param pfPSE Where to store the page size extension indicator.
3792 * @param pfPGE Where to store the page global enabled indicator.
3793 * @param pfNXE Where to store the no-execution enabled indicator.
3794 */
3795static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3796{
3797 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3798
3799 *pfPSE = true;
3800 *pfPGE = false;
3801 switch (PGMGetShadowMode(pVCpu))
3802 {
3803 default:
3804 case PGMMODE_32_BIT:
3805 *pfPAE = *pfLME = *pfNXE = false;
3806 break;
3807 case PGMMODE_PAE:
3808 *pfLME = *pfNXE = false;
3809 *pfPAE = true;
3810 break;
3811 case PGMMODE_PAE_NX:
3812 *pfLME = false;
3813 *pfPAE = *pfNXE = true;
3814 break;
3815 case PGMMODE_AMD64:
3816 *pfNXE = false;
3817 *pfPAE = *pfLME = true;
3818 break;
3819 case PGMMODE_AMD64_NX:
3820 *pfPAE = *pfLME = *pfNXE = true;
3821 break;
3822 }
3823 return PGMGetHyperCR3(pVCpu);
3824}
3825
3826
3827/**
3828 * @callback_method_impl{FNDBGCCMD,
3829 * The 'dpd'\, 'dpda'\, 'dpdb'\, 'dpdg' and 'dpdh' commands.}
3830 */
3831static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3832{
3833 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3834
3835 /*
3836 * Validate input.
3837 */
3838 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3839 if (cArgs == 1 && pCmd->pszCmd[3] == 'a')
3840 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3841 if (cArgs == 1 && pCmd->pszCmd[3] != 'a')
3842 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3843 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
3844 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3845
3846 /*
3847 * Guest or shadow page directories? Get the paging parameters.
3848 */
3849 bool fGuest = pCmd->pszCmd[3] != 'h';
3850 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
3851 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3852 ? pDbgc->fRegCtxGuest
3853 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
3854
3855 bool fPAE, fLME, fPSE, fPGE, fNXE;
3856 uint64_t cr3 = fGuest
3857 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
3858 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
3859 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
3860
3861 /*
3862 * Setup default argument if none was specified.
3863 * Fix address / index confusion.
3864 */
3865 DBGCVAR VarDefault;
3866 if (!cArgs)
3867 {
3868 if (pCmd->pszCmd[3] == 'a')
3869 {
3870 if (fLME || fPAE)
3871 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");
3872 if (fGuest)
3873 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
3874 else
3875 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
3876 }
3877 else
3878 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
3879 paArgs = &VarDefault;
3880 cArgs = 1;
3881 }
3882 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3883 {
3884 /* If it's a number (not an address), it's an index, so convert it to an address. */
3885 Assert(pCmd->pszCmd[3] != 'a');
3886 VarDefault = paArgs[0];
3887 if (fPAE)
3888 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
3889 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
3890 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
3891 VarDefault.u.u64Number <<= X86_PD_SHIFT;
3892 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
3893 paArgs = &VarDefault;
3894 }
3895
3896 /*
3897 * Locate the PDE to start displaying at.
3898 *
3899 * The 'dpda' command takes the address of a PDE, while the others are guest
3900 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
3901 * while the others require us to do all the tedious walking thru the paging
3902 * hierarchy to find the intended PDE.
3903 */
3904 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
3905 DBGCVAR VarGCPtr = { NULL, }; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
3906 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
3907 unsigned cEntries; /* The number of entries to display. */
3908 unsigned cEntriesMax; /* The max number of entries to display. */
3909 int rc;
3910 if (pCmd->pszCmd[3] == 'a')
3911 {
3912 VarPDEAddr = paArgs[0];
3913 switch (VarPDEAddr.enmRangeType)
3914 {
3915 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
3916 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
3917 default: cEntries = 10; break;
3918 }
3919 cEntriesMax = PAGE_SIZE / cbEntry;
3920 }
3921 else
3922 {
3923 /*
3924 * Determine the range.
3925 */
3926 switch (paArgs[0].enmRangeType)
3927 {
3928 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3929 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3930 default: cEntries = 10; break;
3931 }
3932
3933 /*
3934 * Normalize the input address, it must be a flat GC address.
3935 */
3936 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3937 if (RT_FAILURE(rc))
3938 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3939 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3940 {
3941 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3942 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3943 }
3944 if (fPAE)
3945 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
3946 else
3947 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
3948
3949 /*
3950 * Do the paging walk until we get to the page directory.
3951 */
3952 DBGCVAR VarCur;
3953 if (fGuest)
3954 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3955 else
3956 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3957 if (fLME)
3958 {
3959 /* Page Map Level 4 Lookup. */
3960 /* Check if it's a valid address first? */
3961 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3962 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3963 X86PML4E Pml4e;
3964 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3965 if (RT_FAILURE(rc))
3966 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3967 if (!Pml4e.n.u1Present)
3968 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3969
3970 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3971 Assert(fPAE);
3972 }
3973 if (fPAE)
3974 {
3975 /* Page directory pointer table. */
3976 X86PDPE Pdpe;
3977 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3978 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3979 if (RT_FAILURE(rc))
3980 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3981 if (!Pdpe.n.u1Present)
3982 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3983
3984 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3985 VarPDEAddr = VarCur;
3986 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3987 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
3988 }
3989 else
3990 {
3991 /* 32-bit legacy - CR3 == page directory. */
3992 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
3993 VarPDEAddr = VarCur;
3994 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
3995 }
3996 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3997 }
3998
3999 /* adjust cEntries */
4000 cEntries = RT_MAX(1, cEntries);
4001 cEntries = RT_MIN(cEntries, cEntriesMax);
4002
4003 /*
4004 * The display loop.
4005 */
4006 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
4007 &VarPDEAddr, iEntry);
4008 do
4009 {
4010 /*
4011 * Read.
4012 */
4013 X86PDEPAE Pde;
4014 Pde.u = 0;
4015 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, cbEntry, &VarPDEAddr, NULL);
4016 if (RT_FAILURE(rc))
4017 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
4018
4019 /*
4020 * Display.
4021 */
4022 if (iEntry != ~0U)
4023 {
4024 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
4025 iEntry++;
4026 }
4027 if (fPSE && Pde.b.u1Size)
4028 DBGCCmdHlpPrintf(pCmdHlp,
4029 fPAE
4030 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
4031 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
4032 Pde.u,
4033 Pde.u & X86_PDE_PAE_PG_MASK,
4034 Pde.b.u1Present ? "p " : "np",
4035 Pde.b.u1Write ? "w" : "r",
4036 Pde.b.u1User ? "u" : "s",
4037 Pde.b.u1Accessed ? "a " : "na",
4038 Pde.b.u1Dirty ? "d " : "nd",
4039 Pde.b.u3Available,
4040 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
4041 Pde.b.u1WriteThru ? "pwt" : " ",
4042 Pde.b.u1CacheDisable ? "pcd" : " ",
4043 Pde.b.u1PAT ? "pat" : "",
4044 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
4045 else
4046 DBGCCmdHlpPrintf(pCmdHlp,
4047 fPAE
4048 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
4049 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
4050 Pde.u,
4051 Pde.u & X86_PDE_PAE_PG_MASK,
4052 Pde.n.u1Present ? "p " : "np",
4053 Pde.n.u1Write ? "w" : "r",
4054 Pde.n.u1User ? "u" : "s",
4055 Pde.n.u1Accessed ? "a " : "na",
4056 Pde.u & RT_BIT(6) ? "6 " : " ",
4057 Pde.n.u3Available,
4058 Pde.u & RT_BIT(8) ? "8" : " ",
4059 Pde.n.u1WriteThru ? "pwt" : " ",
4060 Pde.n.u1CacheDisable ? "pcd" : " ",
4061 Pde.u & RT_BIT(7) ? "7" : "",
4062 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
4063 if (Pde.u & UINT64_C(0x7fff000000000000))
4064 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
4065 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
4066 if (RT_FAILURE(rc))
4067 return rc;
4068
4069 /*
4070 * Advance.
4071 */
4072 VarPDEAddr.u.u64Number += cbEntry;
4073 if (iEntry != ~0U)
4074 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
4075 } while (cEntries-- > 0);
4076
4077 return VINF_SUCCESS;
4078}
4079
4080
4081/**
4082 * @callback_method_impl{FNDBGCCMD, The 'dpdb' command.}
4083 */
4084static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4085{
4086 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4087 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
4088 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
4089 if (RT_FAILURE(rc1))
4090 return rc1;
4091 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
4092 return rc2;
4093}
4094
4095
4096/**
4097 * @callback_method_impl{FNDBGCCMD, The 'dph*' commands and main part of 'm'.}
4098 */
4099static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4100{
4101 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4102 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4103
4104 /*
4105 * Figure the context and base flags.
4106 */
4107 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;