VirtualBox

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

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

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette