VirtualBox

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

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

VMM/DBGF,Debugger: Removed the !defined(VBOX_WITH_LOTS_OF_DBGF_BPS) code. bugref:9837

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