VirtualBox

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

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

DBGC: LDT entry numbering fix.

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