VirtualBox

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

Last change on this file since 59036 was 58903, checked in by vboxsync, 9 years ago

DBGF: I/O and MMIO breakpoint API changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 189.7 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 58903 2015-11-27 15:07:07Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, CodeView / WinDbg Emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGC
23#include <VBox/dbg.h>
24#include <VBox/vmm/dbgf.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/cpum.h>
27#include <VBox/dis.h>
28#include <VBox/param.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31
32#include <iprt/asm.h>
33#include <iprt/mem.h>
34#include <iprt/string.h>
35#include <iprt/assert.h>
36#include <iprt/ctype.h>
37
38#include <stdlib.h>
39#include <stdio.h>
40
41#include "DBGCInternal.h"
42
43
44/*********************************************************************************************************************************
45* Internal Functions *
46*********************************************************************************************************************************/
47static FNDBGCCMD dbgcCmdBrkAccess;
48static FNDBGCCMD dbgcCmdBrkClear;
49static FNDBGCCMD dbgcCmdBrkDisable;
50static FNDBGCCMD dbgcCmdBrkEnable;
51static FNDBGCCMD dbgcCmdBrkList;
52static FNDBGCCMD dbgcCmdBrkSet;
53static FNDBGCCMD dbgcCmdBrkREM;
54static FNDBGCCMD dbgcCmdDumpMem;
55static FNDBGCCMD dbgcCmdDumpDT;
56static FNDBGCCMD dbgcCmdDumpIDT;
57static FNDBGCCMD dbgcCmdDumpPageDir;
58static FNDBGCCMD dbgcCmdDumpPageDirBoth;
59static FNDBGCCMD dbgcCmdDumpPageHierarchy;
60static FNDBGCCMD dbgcCmdDumpPageTable;
61static FNDBGCCMD dbgcCmdDumpPageTableBoth;
62static FNDBGCCMD dbgcCmdDumpTSS;
63static FNDBGCCMD dbgcCmdEditMem;
64static FNDBGCCMD dbgcCmdGo;
65static FNDBGCCMD dbgcCmdListModules;
66static FNDBGCCMD dbgcCmdListNear;
67static FNDBGCCMD dbgcCmdListSource;
68static FNDBGCCMD dbgcCmdMemoryInfo;
69static FNDBGCCMD dbgcCmdReg;
70static FNDBGCCMD dbgcCmdRegGuest;
71static FNDBGCCMD dbgcCmdRegHyper;
72static FNDBGCCMD dbgcCmdRegTerse;
73static FNDBGCCMD dbgcCmdSearchMem;
74static FNDBGCCMD dbgcCmdSearchMemType;
75static FNDBGCCMD dbgcCmdEventCtrl;
76static FNDBGCCMD dbgcCmdEventCtrlList;
77static FNDBGCCMD dbgcCmdEventCtrlReset;
78static FNDBGCCMD dbgcCmdStack;
79static FNDBGCCMD dbgcCmdTrace;
80static FNDBGCCMD dbgcCmdUnassemble;
81
82
83/*********************************************************************************************************************************
84* Global Variables *
85*********************************************************************************************************************************/
86/** 'ba' arguments. */
87static const DBGCVARDESC g_aArgBrkAcc[] =
88{
89 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
90 { 1, 1, DBGCVAR_CAT_STRING, 0, "access", "The access type: x=execute, rw=read/write (alias r), w=write, i=not implemented." },
91 { 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." },
92 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
93 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
94 { 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)" },
95 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
96};
97
98
99/** 'bc', 'bd', 'be' arguments. */
100static const DBGCVARDESC g_aArgBrks[] =
101{
102 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
103 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "#bp", "Breakpoint number." },
104 { 0, 1, DBGCVAR_CAT_STRING, 0, "all", "All breakpoints." },
105};
106
107
108/** 'bp' arguments. */
109static const DBGCVARDESC g_aArgBrkSet[] =
110{
111 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
112 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
113 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
114 { 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)" },
115 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
116};
117
118
119/** 'br' arguments. */
120static const DBGCVARDESC g_aArgBrkREM[] =
121{
122 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
123 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
124 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
125 { 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)" },
126 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
127};
128
129
130/** 'd?' arguments. */
131static const DBGCVARDESC g_aArgDumpMem[] =
132{
133 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
134 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start dumping memory." },
135};
136
137
138/** 'dg', 'dga', 'dl', 'dla' arguments. */
139static const DBGCVARDESC g_aArgDumpDT[] =
140{
141 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
142 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "sel", "Selector or selector range." },
143 { 0, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Far address which selector should be dumped." },
144};
145
146
147/** 'di', 'dia' arguments. */
148static const DBGCVARDESC g_aArgDumpIDT[] =
149{
150 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
151 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "int", "The interrupt vector or interrupt vector range." },
152};
153
154
155/** 'dpd*' arguments. */
156static const DBGCVARDESC g_aArgDumpPD[] =
157{
158 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
159 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "index", "Index into the page directory." },
160 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from. Range is applied to the page directory." },
161};
162
163
164/** 'dpda' arguments. */
165static const DBGCVARDESC g_aArgDumpPDAddr[] =
166{
167 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
168 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page directory entry to start dumping from." },
169};
170
171
172/** 'dph*' arguments. */
173static const DBGCVARDESC g_aArgDumpPH[] =
174{
175 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
176 { 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." },
177 { 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." },
178 { 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." },
179};
180
181
182/** 'dpt?' arguments. */
183static const DBGCVARDESC g_aArgDumpPT[] =
184{
185 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
186 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from." },
187};
188
189
190/** 'dpta' arguments. */
191static const DBGCVARDESC g_aArgDumpPTAddr[] =
192{
193 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
194 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page table entry to start dumping from." },
195};
196
197
198/** 'dt' arguments. */
199static const DBGCVARDESC g_aArgDumpTSS[] =
200{
201 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
202 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "tss", "TSS selector number." },
203 { 0, 1, DBGCVAR_CAT_POINTER, 0, "tss:ign|addr", "TSS address. If the selector is a TSS selector, the offset will be ignored." }
204};
205
206
207/** 'e?' arguments. */
208static const DBGCVARDESC g_aArgEditMem[] =
209{
210 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
211 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to write." },
212 { 1, ~0U, DBGCVAR_CAT_NUMBER, 0, "value", "Value to write." },
213};
214
215
216/** 'lm' arguments. */
217static const DBGCVARDESC g_aArgListMods[] =
218{
219 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
220 { 0, ~0U, DBGCVAR_CAT_STRING, 0, "module", "Module name." },
221};
222
223
224/** 'ln' arguments. */
225static const DBGCVARDESC g_aArgListNear[] =
226{
227 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
228 { 0, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
229 { 0, ~0U, DBGCVAR_CAT_SYMBOL, 0, "symbol", "Symbol to lookup." },
230};
231
232
233/** 'ls' arguments. */
234static const DBGCVARDESC g_aArgListSource[] =
235{
236 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
237 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start looking for source lines." },
238};
239
240
241/** 'm' argument. */
242static const DBGCVARDESC g_aArgMemoryInfo[] =
243{
244 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
245 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Pointer to obtain info about." },
246};
247
248
249/** 'r' arguments. */
250static const DBGCVARDESC g_aArgReg[] =
251{
252 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
253 { 0, 1, DBGCVAR_CAT_SYMBOL, 0, "register", "Register to show or set." },
254 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "=", "Equal sign." },
255 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "value", "New register value." },
256};
257
258
259/** 's' arguments. */
260static const DBGCVARDESC g_aArgSearchMem[] =
261{
262 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
263 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-b", "Byte string." },
264 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-w", "Word string." },
265 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-d", "DWord string." },
266 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-q", "QWord string." },
267 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-a", "ASCII string." },
268 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-u", "Unicode string." },
269 { 0, 1, DBGCVAR_CAT_OPTION_NUMBER, 0, "-n <Hits>", "Maximum number of hits." },
270 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
271 { 0, ~0U, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
272};
273
274
275/** 's?' arguments. */
276static const DBGCVARDESC g_aArgSearchMemType[] =
277{
278 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
279 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
280 { 1, ~0U, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
281};
282
283
284/** 'sxe', 'sxn', 'sxi', 'sx-' arguments. */
285static const DBGCVARDESC g_aArgEventCtrl[] =
286{
287 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
288 { 0, 1, DBGCVAR_CAT_STRING, 0, "-c", "The -c option, requires <cmds>." },
289 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "cmds", "Command to execute on this event." },
290 { 1, ~0U, DBGCVAR_CAT_STRING, 0, "event", "One or more events, 'all' refering to all events." },
291};
292
293/** 'sx' and 'sr' arguments. */
294static const DBGCVARDESC g_aArgEventCtrlOpt[] =
295{
296 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
297 { 0, ~0U, DBGCVAR_CAT_STRING, 0, "event", "Zero or more events, 'all' refering to all events and being the default." },
298};
299
300/** 'u' arguments. */
301static const DBGCVARDESC g_aArgUnassemble[] =
302{
303 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
304 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
305};
306
307
308/** Command descriptors for the CodeView / WinDbg emulation.
309 * The emulation isn't attempting to be identical, only somewhat similar.
310 */
311const DBGCCMD g_aCmdsCodeView[] =
312{
313 /* pszCmd, cArgsMin, cArgsMax, paArgDescs, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
314 { "ba", 3, 6, &g_aArgBrkAcc[0], RT_ELEMENTS(g_aArgBrkAcc), 0, dbgcCmdBrkAccess, "<access> <size> <address> [passes [max passes]] [cmds]",
315 "Sets a data access breakpoint." },
316 { "bc", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Deletes a set of breakpoints." },
317 { "bd", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
318 { "be", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enables a set of breakpoints." },
319 { "bl", 0, 0, NULL, 0, 0, dbgcCmdBrkList, "", "Lists all the breakpoints." },
320 { "bp", 1, 4, &g_aArgBrkSet[0], RT_ELEMENTS(g_aArgBrkSet), 0, dbgcCmdBrkSet, "<address> [passes [max passes]] [cmds]",
321 "Sets a breakpoint (int 3)." },
322 { "br", 1, 4, &g_aArgBrkREM[0], RT_ELEMENTS(g_aArgBrkREM), 0, dbgcCmdBrkREM, "<address> [passes [max passes]] [cmds]",
323 "Sets a recompiler specific breakpoint." },
324 { "d", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory using last element size." },
325 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
326 { "db", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in bytes." },
327 { "dd", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in double words." },
328 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
329 { "dg", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT)." },
330 { "dga", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT) including not-present entries." },
331 { "di", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT)." },
332 { "dia", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT) including not-present entries." },
333 { "dl", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT)." },
334 { "dla", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT) including not-present entries." },
335 { "dpd", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the default context." },
336 { "dpda", 0, 1, &g_aArgDumpPDAddr[0],RT_ELEMENTS(g_aArgDumpPDAddr), 0, dbgcCmdDumpPageDir, "[addr]", "Dumps memory at given address as a page directory." },
337 { "dpdb", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDirBoth, "[addr|index]", "Dumps page directory entries of the guest and the hypervisor. " },
338 { "dpdg", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the guest." },
339 { "dpdh", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the hypervisor. " },
340 { "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." },
341 { "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." },
342 { "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." },
343 { "dpt", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the default context." },
344 { "dpta", 1, 1, &g_aArgDumpPTAddr[0],RT_ELEMENTS(g_aArgDumpPTAddr), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps memory at given address as a page table." },
345 { "dptb", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTableBoth,"<addr>", "Dumps page table entries of the guest and the hypervisor." },
346 { "dptg", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the guest." },
347 { "dpth", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the hypervisor." },
348 { "dq", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in quad words." },
349 { "dt", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the task state segment (TSS)." },
350 { "dt16", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 16-bit task state segment (TSS)." },
351 { "dt32", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 32-bit task state segment (TSS)." },
352 { "dt64", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 64-bit task state segment (TSS)." },
353 { "dw", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in words." },
354 /** @todo add 'e', 'ea str', 'eza str', 'eu str' and 'ezu str'. See also
355 * dbgcCmdSearchMem and its dbgcVarsToBytes usage. */
356 { "eb", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 1-byte value to memory." },
357 { "ew", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 2-byte value to memory." },
358 { "ed", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 4-byte value to memory." },
359 { "eq", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 8-byte value to memory." },
360 { "g", 0, 0, NULL, 0, 0, dbgcCmdGo, "", "Continue execution." },
361 { "k", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack." },
362 { "kg", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - guest." },
363 { "kh", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - hypervisor." },
364 { "lm", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules." },
365 { "lmv", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules, verbose." },
366 { "lmo", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments." },
367 { "lmov", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments, verbose." },
368 { "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." },
369 { "ls", 0, 1, &g_aArgListSource[0],RT_ELEMENTS(g_aArgListSource), 0, dbgcCmdListSource, "[addr]", "Source." },
370 { "m", 1, 1, &g_aArgMemoryInfo[0],RT_ELEMENTS(g_aArgMemoryInfo), 0, dbgcCmdMemoryInfo, "<addr>", "Display information about that piece of memory." },
371 { "r", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdReg, "[reg [[=] newval]]", "Show or set register(s) - active reg set." },
372 { "rg", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegGuest, "[reg [[=] newval]]", "Show or set register(s) - guest reg set." },
373 { "rg32", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 32-bit guest registers." },
374 { "rg64", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 64-bit guest registers." },
375 { "rh", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegHyper, "[reg [[=] newval]]", "Show or set register(s) - hypervisor reg set." },
376 { "rt", 0, 0, NULL, 0, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
377 { "s", 0, ~0U, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
378 { "sa", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
379 { "sb", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
380 { "sd", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
381 { "sq", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
382 { "su", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
383 { "sw", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more words." },
384 { "sx", 0, 0, &g_aArgEventCtrlOpt[0], RT_ELEMENTS(g_aArgEventCtrlOpt), 0, dbgcCmdEventCtrlList, "[<event> [..]]", "Lists settings for exceptions, exits and other events. All if no filter is specified." },
385 { "sx-", 2, ~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." },
386 { "sxe", 2, ~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." },
387 { "sxn", 2, ~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." },
388 { "sxi", 2, ~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." },
389 { "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." },
390 { "t", 0, 0, NULL, 0, 0, dbgcCmdTrace, "", "Instruction trace (step into)." },
391 { "u", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble." },
392 { "u64", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 64-bit code." },
393 { "u32", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 32-bit code." },
394 { "u16", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code." },
395 { "uv86", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code with v8086/real mode addressing." },
396};
397
398/** The number of commands in the CodeView/WinDbg emulation. */
399const uint32_t g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
400
401
402
403
404/**
405 * @callback_method_impl{FNDBGCCMD, The 'go' command.}
406 */
407static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
408{
409 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
410
411 /*
412 * Check if the VM is halted or not before trying to resume it.
413 */
414 if (!DBGFR3IsHalted(pUVM))
415 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The VM is already running");
416
417 int rc = DBGFR3Resume(pUVM);
418 if (RT_FAILURE(rc))
419 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3Resume");
420
421 NOREF(paArgs); NOREF(cArgs);
422 return VINF_SUCCESS;
423}
424
425
426/**
427 * @callback_method_impl{FNDBGCCMD, The 'ba' command.}
428 */
429static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
430{
431 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
432
433 /*
434 * Interpret access type.
435 */
436 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
437 || paArgs[0].u.pszString[1])
438 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'",
439 paArgs[0].u.pszString, pCmd->pszCmd);
440 uint8_t fType = 0;
441 switch (paArgs[0].u.pszString[0])
442 {
443 case 'x': fType = X86_DR7_RW_EO; break;
444 case 'r': fType = X86_DR7_RW_RW; break;
445 case 'w': fType = X86_DR7_RW_WO; break;
446 case 'i': fType = X86_DR7_RW_IO; break;
447 }
448
449 /*
450 * Validate size.
451 */
452 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
453 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 'x' access type requires size 1!",
454 paArgs[1].u.u64Number, pCmd->pszCmd);
455 switch (paArgs[1].u.u64Number)
456 {
457 case 1:
458 case 2:
459 case 4:
460 break;
461 /*case 8: - later*/
462 default:
463 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 1, 2 or 4!",
464 paArgs[1].u.u64Number, pCmd->pszCmd);
465 }
466 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
467
468 /*
469 * Convert the pointer to a DBGF address.
470 */
471 DBGFADDRESS Address;
472 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
473 if (RT_FAILURE(rc))
474 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%DV,)", &paArgs[2]);
475
476 /*
477 * Pick out the optional arguments.
478 */
479 uint64_t iHitTrigger = 0;
480 uint64_t iHitDisable = ~0;
481 const char *pszCmds = NULL;
482 unsigned iArg = 3;
483 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
484 {
485 iHitTrigger = paArgs[iArg].u.u64Number;
486 iArg++;
487 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
488 {
489 iHitDisable = paArgs[iArg].u.u64Number;
490 iArg++;
491 }
492 }
493 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
494 {
495 pszCmds = paArgs[iArg].u.pszString;
496 iArg++;
497 }
498
499 /*
500 * Try set the breakpoint.
501 */
502 uint32_t iBp;
503 rc = DBGFR3BpSetReg(pUVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
504 if (RT_SUCCESS(rc))
505 {
506 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
507 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
508 if (RT_SUCCESS(rc))
509 return DBGCCmdHlpPrintf(pCmdHlp, "Set access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
510 if (rc == VERR_DBGC_BP_EXISTS)
511 {
512 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
513 if (RT_SUCCESS(rc))
514 return DBGCCmdHlpPrintf(pCmdHlp, "Updated access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
515 }
516 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
517 AssertRC(rc2);
518 }
519 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set access breakpoint at %RGv", Address.FlatPtr);
520}
521
522
523/**
524 * @callback_method_impl{FNDBGCCMD, The 'bc' command.}
525 */
526static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
527{
528 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
529
530 /*
531 * Enumerate the arguments.
532 */
533 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
534 int rc = VINF_SUCCESS;
535 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
536 {
537 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
538 {
539 /* one */
540 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
541 if (iBp == paArgs[iArg].u.u64Number)
542 {
543 int rc2 = DBGFR3BpClear(pUVM, iBp);
544 if (RT_FAILURE(rc2))
545 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
546 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
547 dbgcBpDelete(pDbgc, iBp);
548 }
549 else
550 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
551 }
552 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
553 {
554 /* all */
555 PDBGCBP pBp = pDbgc->pFirstBp;
556 while (pBp)
557 {
558 uint32_t iBp = pBp->iBp;
559 pBp = pBp->pNext;
560
561 int rc2 = DBGFR3BpClear(pUVM, iBp);
562 if (RT_FAILURE(rc2))
563 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
564 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
565 dbgcBpDelete(pDbgc, iBp);
566 }
567 }
568 else
569 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
570 }
571 return rc;
572}
573
574
575/**
576 * @callback_method_impl{FNDBGCCMD, The 'bd' command.}
577 */
578static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
579{
580 /*
581 * Enumerate the arguments.
582 */
583 int rc = VINF_SUCCESS;
584 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
585 {
586 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
587 {
588 /* one */
589 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
590 if (iBp == paArgs[iArg].u.u64Number)
591 {
592 rc = DBGFR3BpDisable(pUVM, iBp);
593 if (RT_FAILURE(rc))
594 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpDisable failed for breakpoint %#x", iBp);
595 }
596 else
597 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
598 }
599 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
600 {
601 /* all */
602 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
603 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
604 {
605 int rc2 = DBGFR3BpDisable(pUVM, pBp->iBp);
606 if (RT_FAILURE(rc2))
607 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpDisable failed for breakpoint %#x", pBp->iBp);
608 }
609 }
610 else
611 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
612 }
613 return rc;
614}
615
616
617/**
618 * @callback_method_impl{FNDBGCCMD, The 'be' command.}
619 */
620static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
621{
622 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
623
624 /*
625 * Enumerate the arguments.
626 */
627 int rc = VINF_SUCCESS;
628 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
629 {
630 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
631 {
632 /* one */
633 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
634 if (iBp == paArgs[iArg].u.u64Number)
635 {
636 rc = DBGFR3BpEnable(pUVM, iBp);
637 if (RT_FAILURE(rc))
638 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnable failed for breakpoint %#x", iBp);
639 }
640 else
641 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
642 }
643 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
644 {
645 /* all */
646 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
647 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
648 {
649 int rc2 = DBGFR3BpEnable(pUVM, pBp->iBp);
650 if (RT_FAILURE(rc2))
651 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpEnable failed for breakpoint %#x", pBp->iBp);
652 }
653 }
654 else
655 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
656 }
657 return rc;
658}
659
660
661/**
662 * Breakpoint enumeration callback function.
663 *
664 * @returns VBox status code. Any failure will stop the enumeration.
665 * @param pUVM The user mode VM handle.
666 * @param pvUser The user argument.
667 * @param pBp Pointer to the breakpoint information. (readonly)
668 */
669static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PUVM pUVM, void *pvUser, PCDBGFBP pBp)
670{
671 PDBGC pDbgc = (PDBGC)pvUser;
672 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
673
674 /*
675 * BP type and size.
676 */
677 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%#4x %c ", pBp->iBp, pBp->fEnabled ? 'e' : 'd');
678 bool fHasAddress = false;
679 switch (pBp->enmType)
680 {
681 case DBGFBPTYPE_INT3:
682 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " p %RGv", pBp->u.Int3.GCPtr);
683 fHasAddress = true;
684 break;
685 case DBGFBPTYPE_REG:
686 {
687 char chType;
688 switch (pBp->u.Reg.fType)
689 {
690 case X86_DR7_RW_EO: chType = 'x'; break;
691 case X86_DR7_RW_WO: chType = 'w'; break;
692 case X86_DR7_RW_IO: chType = 'i'; break;
693 case X86_DR7_RW_RW: chType = 'r'; break;
694 default: chType = '?'; break;
695
696 }
697 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%d %c %RGv", pBp->u.Reg.cb, chType, pBp->u.Reg.GCPtr);
698 fHasAddress = true;
699 break;
700 }
701
702 case DBGFBPTYPE_REM:
703 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " r %RGv", pBp->u.Rem.GCPtr);
704 fHasAddress = true;
705 break;
706
707/** @todo realign the list when I/O and MMIO breakpoint command have been added and it's possible to test this code. */
708 case DBGFBPTYPE_PORT_IO:
709 case DBGFBPTYPE_MMIO:
710 {
711 uint32_t fAccess = pBp->enmType == DBGFBPTYPE_PORT_IO ? pBp->u.PortIo.fAccess : pBp->u.Mmio.fAccess;
712 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, pBp->enmType == DBGFBPTYPE_PORT_IO ? " i" : " m");
713 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
714 fAccess & DBGFBPIOACCESS_READ_MASK ? 'r' : '-',
715 fAccess & DBGFBPIOACCESS_READ_BYTE ? '1' : '-',
716 fAccess & DBGFBPIOACCESS_READ_WORD ? '2' : '-',
717 fAccess & DBGFBPIOACCESS_READ_DWORD ? '4' : '-',
718 fAccess & DBGFBPIOACCESS_READ_QWORD ? '8' : '-',
719 fAccess & DBGFBPIOACCESS_READ_OTHER ? '+' : '-');
720 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
721 fAccess & DBGFBPIOACCESS_WRITE_MASK ? 'w' : '-',
722 fAccess & DBGFBPIOACCESS_WRITE_BYTE ? '1' : '-',
723 fAccess & DBGFBPIOACCESS_WRITE_WORD ? '2' : '-',
724 fAccess & DBGFBPIOACCESS_WRITE_DWORD ? '4' : '-',
725 fAccess & DBGFBPIOACCESS_WRITE_QWORD ? '8' : '-',
726 fAccess & DBGFBPIOACCESS_WRITE_OTHER ? '+' : '-');
727 if (pBp->enmType == DBGFBPTYPE_PORT_IO)
728 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04x-%04x",
729 pBp->u.PortIo.uPort, pBp->u.PortIo.uPort + pBp->u.PortIo.cPorts - 1);
730 else
731 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%RGp LB %03x", pBp->u.Mmio.PhysAddr, pBp->u.Mmio.cb);
732 break;
733 }
734
735 default:
736 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " unknown type %d!!", pBp->enmType);
737 AssertFailed();
738 break;
739
740 }
741 if (pBp->iHitDisable == ~(uint64_t)0)
742 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to ~0) ", pBp->cHits, pBp->iHitTrigger);
743 else
744 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to %04RX64)", pBp->cHits, pBp->iHitTrigger, pBp->iHitDisable);
745
746 /*
747 * Try resolve the address if it has one.
748 */
749 if (fHasAddress)
750 {
751 RTDBGSYMBOL Sym;
752 RTINTPTR off;
753 DBGFADDRESS Addr;
754 int rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, pBp->u.GCPtr),
755 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &off, &Sym, NULL);
756 if (RT_SUCCESS(rc))
757 {
758 if (!off)
759 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s", Sym.szName);
760 else if (off > 0)
761 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, off);
762 else
763 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s-%RGv", Sym.szName, -off);
764 }
765 }
766
767 /*
768 * The commands.
769 */
770 if (pDbgcBp)
771 {
772 if (pDbgcBp->cchCmd)
773 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n cmds: '%s'\n", pDbgcBp->szCmd);
774 else
775 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n");
776 }
777 else
778 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " [unknown bp]\n");
779
780 return VINF_SUCCESS;
781}
782
783
784/**
785 * @callback_method_impl{FNDBGCCMD, The 'bl' command.}
786 */
787static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
788{
789 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
790 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
791 NOREF(paArgs);
792
793 /*
794 * Enumerate the breakpoints.
795 */
796 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
797 int rc = DBGFR3BpEnum(pUVM, dbgcEnumBreakpointsCallback, pDbgc);
798 if (RT_FAILURE(rc))
799 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnum");
800 return rc;
801}
802
803
804/**
805 * @callback_method_impl{FNDBGCCMD, The 'bp' command.}
806 */
807static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
808{
809 /*
810 * Convert the pointer to a DBGF address.
811 */
812 DBGFADDRESS Address;
813 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
814 if (RT_FAILURE(rc))
815 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
816
817 /*
818 * Pick out the optional arguments.
819 */
820 uint64_t iHitTrigger = 0;
821 uint64_t iHitDisable = ~0;
822 const char *pszCmds = NULL;
823 unsigned iArg = 1;
824 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
825 {
826 iHitTrigger = paArgs[iArg].u.u64Number;
827 iArg++;
828 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
829 {
830 iHitDisable = paArgs[iArg].u.u64Number;
831 iArg++;
832 }
833 }
834 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
835 {
836 pszCmds = paArgs[iArg].u.pszString;
837 iArg++;
838 }
839
840 /*
841 * Try set the breakpoint.
842 */
843 uint32_t iBp;
844 rc = DBGFR3BpSet(pUVM, &Address, iHitTrigger, iHitDisable, &iBp);
845 if (RT_SUCCESS(rc))
846 {
847 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
848 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
849 if (RT_SUCCESS(rc))
850 return DBGCCmdHlpPrintf(pCmdHlp, "Set breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
851 if (rc == VERR_DBGC_BP_EXISTS)
852 {
853 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
854 if (RT_SUCCESS(rc))
855 return DBGCCmdHlpPrintf(pCmdHlp, "Updated breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
856 }
857 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
858 AssertRC(rc2);
859 }
860 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set breakpoint at %RGv", Address.FlatPtr);
861}
862
863
864/**
865 * @callback_method_impl{FNDBGCCMD, The 'br' command.}
866 */
867static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
868{
869 /*
870 * Convert the pointer to a DBGF address.
871 */
872 DBGFADDRESS Address;
873 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
874 if (RT_FAILURE(rc))
875 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
876
877 /*
878 * Pick out the optional arguments.
879 */
880 uint64_t iHitTrigger = 0;
881 uint64_t iHitDisable = ~0;
882 const char *pszCmds = NULL;
883 unsigned iArg = 1;
884 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
885 {
886 iHitTrigger = paArgs[iArg].u.u64Number;
887 iArg++;
888 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
889 {
890 iHitDisable = paArgs[iArg].u.u64Number;
891 iArg++;
892 }
893 }
894 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
895 {
896 pszCmds = paArgs[iArg].u.pszString;
897 iArg++;
898 }
899
900 /*
901 * Try set the breakpoint.
902 */
903 uint32_t iBp;
904 rc = DBGFR3BpSetREM(pUVM, &Address, iHitTrigger, iHitDisable, &iBp);
905 if (RT_SUCCESS(rc))
906 {
907 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
908 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
909 if (RT_SUCCESS(rc))
910 return DBGCCmdHlpPrintf(pCmdHlp, "Set REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
911 if (rc == VERR_DBGC_BP_EXISTS)
912 {
913 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
914 if (RT_SUCCESS(rc))
915 return DBGCCmdHlpPrintf(pCmdHlp, "Updated REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
916 }
917 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
918 AssertRC(rc2);
919 }
920 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set REM breakpoint at %RGv", Address.FlatPtr);
921}
922
923
924/**
925 * Helps the unassmble ('u') command display symbols it starts at and passes.
926 *
927 * @param pUVM The user mode VM handle.
928 * @param pCmdHlp The command helpers for printing via.
929 * @param hDbgAs The address space to look up addresses in.
930 * @param pAddress The current address.
931 * @param pcbCallAgain Where to return the distance to the next check (in
932 * instruction bytes).
933 */
934static void dbgcCmdUnassambleHelpListNear(PUVM pUVM, PDBGCCMDHLP pCmdHlp, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
935 PRTUINTPTR pcbCallAgain)
936{
937 RTDBGSYMBOL Symbol;
938 RTGCINTPTR offDispSym;
939 int rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDispSym, &Symbol, NULL);
940 if (RT_FAILURE(rc) || offDispSym > _1G)
941 rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL, &offDispSym, &Symbol, NULL);
942 if (RT_SUCCESS(rc) && offDispSym < _1G)
943 {
944 if (!offDispSym)
945 {
946 DBGCCmdHlpPrintf(pCmdHlp, "%s:\n", Symbol.szName);
947 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb;
948 }
949 else if (offDispSym > 0)
950 {
951 DBGCCmdHlpPrintf(pCmdHlp, "%s+%#llx:\n", Symbol.szName, (uint64_t)offDispSym);
952 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb > (RTGCUINTPTR)offDispSym ? Symbol.cb - (RTGCUINTPTR)offDispSym : 1;
953 }
954 else
955 {
956 DBGCCmdHlpPrintf(pCmdHlp, "%s-%#llx:\n", Symbol.szName, (uint64_t)-offDispSym);
957 *pcbCallAgain = !Symbol.cb ? 64 : (RTGCUINTPTR)-offDispSym + Symbol.cb;
958 }
959 }
960 else
961 *pcbCallAgain = UINT32_MAX;
962}
963
964
965/**
966 * @callback_method_impl{FNDBGCCMD, The 'u' command.}
967 */
968static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
969{
970 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
971
972 /*
973 * Validate input.
974 */
975 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
976 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
977 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
978
979 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
980 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
981
982 /*
983 * Check the desired mode.
984 */
985 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS | DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
986 switch (pCmd->pszCmd[1])
987 {
988 default: AssertFailed();
989 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
990 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
991 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
992 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
993 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
994 }
995
996 /** @todo should use DBGFADDRESS for everything */
997
998 /*
999 * Find address.
1000 */
1001 if (!cArgs)
1002 {
1003 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1004 {
1005 /** @todo Batch query CS, RIP, CPU mode and flags. */
1006 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1007 if ( pDbgc->fRegCtxGuest
1008 && CPUMIsGuestIn64BitCode(pVCpu))
1009 {
1010 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
1011 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
1012 }
1013 else
1014 {
1015 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1016 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1017 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1018 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
1019 && pDbgc->fRegCtxGuest
1020 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
1021 {
1022 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1023 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
1024 }
1025 }
1026
1027 if (pDbgc->fRegCtxGuest)
1028 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1029 else
1030 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
1031 }
1032 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
1033 {
1034 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1035 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
1036 }
1037 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1038 }
1039 else
1040 pDbgc->DisasmPos = paArgs[0];
1041 pDbgc->pLastPos = &pDbgc->DisasmPos;
1042
1043 /*
1044 * Range.
1045 */
1046 switch (pDbgc->DisasmPos.enmRangeType)
1047 {
1048 case DBGCVAR_RANGE_NONE:
1049 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1050 pDbgc->DisasmPos.u64Range = 10;
1051 break;
1052
1053 case DBGCVAR_RANGE_ELEMENTS:
1054 if (pDbgc->DisasmPos.u64Range > 2048)
1055 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
1056 break;
1057
1058 case DBGCVAR_RANGE_BYTES:
1059 if (pDbgc->DisasmPos.u64Range > 65536)
1060 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
1061 break;
1062
1063 default:
1064 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
1065 }
1066
1067 /*
1068 * Convert physical and host addresses to guest addresses.
1069 */
1070 RTDBGAS hDbgAs = pDbgc->hDbgAs;
1071 int rc;
1072 switch (pDbgc->DisasmPos.enmType)
1073 {
1074 case DBGCVAR_TYPE_GC_FLAT:
1075 case DBGCVAR_TYPE_GC_FAR:
1076 break;
1077 case DBGCVAR_TYPE_GC_PHYS:
1078 hDbgAs = DBGF_AS_PHYS;
1079 case DBGCVAR_TYPE_HC_FLAT:
1080 case DBGCVAR_TYPE_HC_PHYS:
1081 {
1082 DBGCVAR VarTmp;
1083 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1084 if (RT_FAILURE(rc))
1085 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
1086 pDbgc->DisasmPos = VarTmp;
1087 break;
1088 }
1089 default: AssertFailed(); break;
1090 }
1091
1092 DBGFADDRESS CurAddr;
1093 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1094 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1095 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1096 else
1097 {
1098 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1099 if (RT_FAILURE(rc))
1100 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
1101 }
1102
1103 if (CurAddr.fFlags & DBGFADDRESS_FLAGS_HMA)
1104 fFlags |= DBGF_DISAS_FLAGS_HYPER; /* This crap is due to not using DBGFADDRESS as DBGFR3Disas* input. */
1105 pDbgc->fDisasm = fFlags;
1106
1107 /*
1108 * Figure out where we are and display it. Also calculate when we need to
1109 * check for a new symbol if possible.
1110 */
1111 RTGCUINTPTR cbCheckSymbol;
1112 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1113
1114 /*
1115 * Do the disassembling.
1116 */
1117 unsigned cTries = 32;
1118 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1119 if (iRangeLeft == 0) /* kludge for 'r'. */
1120 iRangeLeft = -1;
1121 for (;;)
1122 {
1123 /*
1124 * Disassemble the instruction.
1125 */
1126 char szDis[256];
1127 uint32_t cbInstr = 1;
1128 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1129 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags,
1130 &szDis[0], sizeof(szDis), &cbInstr);
1131 else
1132 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags,
1133 &szDis[0], sizeof(szDis), &cbInstr);
1134 if (RT_SUCCESS(rc))
1135 {
1136 /* print it */
1137 rc = DBGCCmdHlpPrintf(pCmdHlp, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1138 if (RT_FAILURE(rc))
1139 return rc;
1140 }
1141 else
1142 {
1143 /* bitch. */
1144 int rc2 = DBGCCmdHlpPrintf(pCmdHlp, "Failed to disassemble instruction, skipping one byte.\n");
1145 if (RT_FAILURE(rc2))
1146 return rc2;
1147 if (cTries-- > 0)
1148 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Too many disassembly failures. Giving up");
1149 cbInstr = 1;
1150 }
1151
1152 /* advance */
1153 if (iRangeLeft < 0) /* 'r' */
1154 break;
1155 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1156 iRangeLeft--;
1157 else
1158 iRangeLeft -= cbInstr;
1159 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1160 if (RT_FAILURE(rc))
1161 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpEval(,,'(%Dv) + %x')", &pDbgc->DisasmPos, cbInstr);
1162 if (iRangeLeft <= 0)
1163 break;
1164 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1165
1166 /* Print next symbol? */
1167 if (cbCheckSymbol <= cbInstr)
1168 {
1169 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1170 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1171 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1172 else
1173 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1174 if (RT_SUCCESS(rc))
1175 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1176 else
1177 cbCheckSymbol = UINT32_MAX;
1178 }
1179 else
1180 cbCheckSymbol -= cbInstr;
1181 }
1182
1183 NOREF(pCmd);
1184 return VINF_SUCCESS;
1185}
1186
1187
1188/**
1189 * @callback_method_impl{FNDBGCCMD, The 'ls' command.}
1190 */
1191static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1192{
1193 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1194
1195 /*
1196 * Validate input.
1197 */
1198 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
1199 if (cArgs == 1)
1200 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
1201 if (!pUVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1202 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start listing...");
1203 if (!pUVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1204 return DBGCCmdHlpFail(pCmdHlp, pCmd, "GC address but no VM");
1205
1206 /*
1207 * Find address.
1208 */
1209 if (!cArgs)
1210 {
1211 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1212 {
1213 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1214 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
1215 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1216 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1217 }
1218 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
1219 }
1220 else
1221 pDbgc->SourcePos = paArgs[0];
1222 pDbgc->pLastPos = &pDbgc->SourcePos;
1223
1224 /*
1225 * Ensure the source address is flat GC.
1226 */
1227 switch (pDbgc->SourcePos.enmType)
1228 {
1229 case DBGCVAR_TYPE_GC_FLAT:
1230 break;
1231 case DBGCVAR_TYPE_GC_PHYS:
1232 case DBGCVAR_TYPE_GC_FAR:
1233 case DBGCVAR_TYPE_HC_FLAT:
1234 case DBGCVAR_TYPE_HC_PHYS:
1235 {
1236 int rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
1237 if (RT_FAILURE(rc))
1238 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid address or address type. (rc=%d)\n", rc);
1239 break;
1240 }
1241 default: AssertFailed(); break;
1242 }
1243
1244 /*
1245 * Range.
1246 */
1247 switch (pDbgc->SourcePos.enmRangeType)
1248 {
1249 case DBGCVAR_RANGE_NONE:
1250 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1251 pDbgc->SourcePos.u64Range = 10;
1252 break;
1253
1254 case DBGCVAR_RANGE_ELEMENTS:
1255 if (pDbgc->SourcePos.u64Range > 2048)
1256 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many lines requested. Max is 2048 lines.\n");
1257 break;
1258
1259 case DBGCVAR_RANGE_BYTES:
1260 if (pDbgc->SourcePos.u64Range > 65536)
1261 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
1262 break;
1263
1264 default:
1265 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
1266 }
1267
1268 /*
1269 * Do the disassembling.
1270 */
1271 bool fFirst = 1;
1272 RTDBGLINE LinePrev = { 0, 0, 0, 0, 0, "" };
1273 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
1274 if (iRangeLeft == 0) /* kludge for 'r'. */
1275 iRangeLeft = -1;
1276 for (;;)
1277 {
1278 /*
1279 * Get line info.
1280 */
1281 RTDBGLINE Line;
1282 RTGCINTPTR off;
1283 DBGFADDRESS SourcePosAddr;
1284 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->SourcePos, &SourcePosAddr);
1285 if (RT_FAILURE(rc))
1286 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%Dv)", &pDbgc->SourcePos);
1287 rc = DBGFR3AsLineByAddr(pUVM, pDbgc->hDbgAs, &SourcePosAddr, &off, &Line, NULL);
1288 if (RT_FAILURE(rc))
1289 return VINF_SUCCESS;
1290
1291 unsigned cLines = 0;
1292 if (memcmp(&Line, &LinePrev, sizeof(Line)))
1293 {
1294 /*
1295 * Print filenamename
1296 */
1297 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
1298 fFirst = true;
1299 if (fFirst)
1300 {
1301 rc = DBGCCmdHlpPrintf(pCmdHlp, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
1302 if (RT_FAILURE(rc))
1303 return rc;
1304 }
1305
1306 /*
1307 * Try open the file and read the line.
1308 */
1309 FILE *phFile = fopen(Line.szFilename, "r");
1310 if (phFile)
1311 {
1312 /* Skip ahead to the desired line. */
1313 char szLine[4096];
1314 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
1315 if (cBefore > 7)
1316 cBefore = 0;
1317 unsigned cLeft = Line.uLineNo - cBefore;
1318 while (cLeft > 0)
1319 {
1320 szLine[0] = '\0';
1321 if (!fgets(szLine, sizeof(szLine), phFile))
1322 break;
1323 cLeft--;
1324 }
1325 if (!cLeft)
1326 {
1327 /* print the before lines */
1328 for (;;)
1329 {
1330 size_t cch = strlen(szLine);
1331 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || RT_C_IS_SPACE(szLine[cch - 1])) )
1332 szLine[--cch] = '\0';
1333 if (cBefore-- <= 0)
1334 break;
1335
1336 rc = DBGCCmdHlpPrintf(pCmdHlp, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
1337 szLine[0] = '\0';
1338 (void)fgets(szLine, sizeof(szLine), phFile);
1339 cLines++;
1340 }
1341 /* print the actual line */
1342 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
1343 }
1344 fclose(phFile);
1345 if (RT_FAILURE(rc))
1346 return rc;
1347 fFirst = false;
1348 }
1349 else
1350 return DBGCCmdHlpPrintf(pCmdHlp, "Warning: couldn't open source file '%s'\n", Line.szFilename);
1351
1352 LinePrev = Line;
1353 }
1354
1355
1356 /*
1357 * Advance
1358 */
1359 if (iRangeLeft < 0) /* 'r' */
1360 break;
1361 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1362 iRangeLeft -= cLines;
1363 else
1364 iRangeLeft -= 1;
1365 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
1366 if (RT_FAILURE(rc))
1367 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
1368 if (iRangeLeft <= 0)
1369 break;
1370 }
1371
1372 NOREF(pCmd);
1373 return 0;
1374}
1375
1376
1377/**
1378 * @callback_method_impl{FNDBGCCMD, The 'r' command.}
1379 */
1380static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1381{
1382 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1383 if (!pDbgc->fRegCtxGuest)
1384 return dbgcCmdRegHyper(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
1385 return dbgcCmdRegGuest(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
1386}
1387
1388
1389/**
1390 * @callback_method_impl{FNDBGCCMD, Common worker for the dbgcCmdReg*()
1391 * commands.}
1392 */
1393static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs,
1394 const char *pszPrefix)
1395{
1396 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1397 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1 || cArgs == 2 || cArgs == 3);
1398 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING
1399 || paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL);
1400
1401 /*
1402 * Parse the register name and kind.
1403 */
1404 const char *pszReg = paArgs[0].u.pszString;
1405 if (*pszReg == '@')
1406 pszReg++;
1407 VMCPUID idCpu = pDbgc->idCpu;
1408 if (*pszPrefix)
1409 idCpu |= DBGFREG_HYPER_VMCPUID;
1410 if (*pszReg == '.')
1411 {
1412 pszReg++;
1413 idCpu |= DBGFREG_HYPER_VMCPUID;
1414 }
1415 const char * const pszActualPrefix = idCpu & DBGFREG_HYPER_VMCPUID ? "." : "";
1416
1417 /*
1418 * Query the register type & value (the setter needs the type).
1419 */
1420 DBGFREGVALTYPE enmType;
1421 DBGFREGVAL Value;
1422 int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Value, &enmType);
1423 if (RT_FAILURE(rc))
1424 {
1425 if (rc == VERR_DBGF_REGISTER_NOT_FOUND)
1426 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown register: '%s%s'.\n",
1427 pszActualPrefix, pszReg);
1428 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmQuery failed querying '%s%s': %Rrc.\n",
1429 pszActualPrefix, pszReg, rc);
1430 }
1431 if (cArgs == 1)
1432 {
1433 /*
1434 * Show the register.
1435 */
1436 char szValue[160];
1437 rc = DBGFR3RegFormatValue(szValue, sizeof(szValue), &Value, enmType, true /*fSpecial*/);
1438 if (RT_SUCCESS(rc))
1439 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s%s=%s\n", pszActualPrefix, pszReg, szValue);
1440 else
1441 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
1442 }
1443 else
1444 {
1445 DBGCVAR NewValueTmp;
1446 PCDBGCVAR pNewValue;
1447 if (cArgs == 3)
1448 {
1449 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, paArgs[1].enmType == DBGCVAR_TYPE_STRING);
1450 if (strcmp(paArgs[1].u.pszString, "="))
1451 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Second argument must be '='.");
1452 pNewValue = &paArgs[2];
1453 }
1454 else
1455 {
1456 /* Not possible to convince the parser to support both codeview and
1457 windbg syntax and make the equal sign optional. Try help it. */
1458 /** @todo make DBGCCmdHlpConvert do more with strings. */
1459 rc = DBGCCmdHlpConvert(pCmdHlp, &paArgs[1], DBGCVAR_TYPE_NUMBER, true /*fConvSyms*/, &NewValueTmp);
1460 if (RT_FAILURE(rc))
1461 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "The last argument must be a value or valid symbol.");
1462 pNewValue = &NewValueTmp;
1463 }
1464
1465 /*
1466 * Modify the register.
1467 */
1468 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pNewValue->enmType == DBGCVAR_TYPE_NUMBER);
1469 if (enmType != DBGFREGVALTYPE_DTR)
1470 {
1471 enmType = DBGFREGVALTYPE_U64;
1472 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.u64);
1473 }
1474 else
1475 {
1476 enmType = DBGFREGVALTYPE_DTR;
1477 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.dtr.u64Base);
1478 if (RT_SUCCESS(rc) && pNewValue->enmRangeType != DBGCVAR_RANGE_NONE)
1479 Value.dtr.u32Limit = (uint32_t)pNewValue->u64Range;
1480 }
1481 if (RT_SUCCESS(rc))
1482 {
1483 rc = DBGFR3RegNmSet(pUVM, idCpu, pszReg, &Value, enmType);
1484 if (RT_FAILURE(rc))
1485 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmSet failed settings '%s%s': %Rrc\n",
1486 pszActualPrefix, pszReg, rc);
1487 if (rc != VINF_SUCCESS)
1488 DBGCCmdHlpPrintf(pCmdHlp, "%s: warning: %Rrc\n", pCmd->pszCmd, rc);
1489 }
1490 else
1491 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
1492 }
1493 return rc;
1494}
1495
1496
1497/**
1498 * @callback_method_impl{FNDBGCCMD,
1499 * The 'rg'\, 'rg64' and 'rg32' commands\, worker for 'r'.}
1500 */
1501static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1502{
1503 /*
1504 * Show all registers our selves.
1505 */
1506 if (cArgs == 0)
1507 {
1508 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1509 bool const f64BitMode = !strcmp(pCmd->pszCmd, "rg64")
1510 || ( strcmp(pCmd->pszCmd, "rg32") != 0
1511 && DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu));
1512 char szDisAndRegs[8192];
1513 int rc;
1514
1515 if (pDbgc->fRegTerse)
1516 {
1517 if (f64BitMode)
1518 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
1519 "u %016VR{rip} L 0\n"
1520 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
1521 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
1522 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
1523 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
1524 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
1525 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} rflags=%08VR{rflags}\n");
1526 else
1527 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
1528 "u %04VR{cs}:%08VR{eip} L 0\n"
1529 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
1530 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
1531 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} eflags=%08VR{eflags}\n");
1532 }
1533 else
1534 {
1535 if (f64BitMode)
1536 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
1537 "u %016VR{rip} L 0\n"
1538 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
1539 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
1540 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
1541 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
1542 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
1543 "cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
1544 "ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
1545 "es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
1546 "fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
1547 "gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
1548 "ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
1549 "dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
1550 "dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
1551 "gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
1552 "ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
1553 "tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
1554 " sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
1555 " efer=%016VR{efer}\n"
1556 " pat=%016VR{pat}\n"
1557 " sf_mask=%016VR{sf_mask}\n"
1558 "krnl_gs_base=%016VR{krnl_gs_base}\n"
1559 " lstar=%016VR{lstar}\n"
1560 " star=%016VR{star} cstar=%016VR{cstar}\n"
1561 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
1562 );
1563 else
1564 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
1565 "u %04VR{cs}:%08VR{eip} L 0\n"
1566 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
1567 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
1568 "cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} dr0=%08VR{dr0} dr1=%08VR{dr1}\n"
1569 "ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} dr2=%08VR{dr2} dr3=%08VR{dr3}\n"
1570 "es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} dr6=%08VR{dr6} dr7=%08VR{dr7}\n"
1571 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr2}\n"
1572 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr3} cr4=%08VR{cr4}\n"
1573 "ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}} cr8=%08VR{cr8}\n"
1574 "gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} idtr=%08VR{idtr_base}:%04VR{idtr_lim} eflags=%08VR{eflags}\n"
1575 "ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
1576 "tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
1577 "sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
1578 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
1579 );
1580 }
1581 if (RT_FAILURE(rc))
1582 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
1583 char *pszRegs = strchr(szDisAndRegs, '\n');
1584 *pszRegs++ = '\0';
1585 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
1586
1587 /*
1588 * Disassemble one instruction at cs:[r|e]ip.
1589 */
1590 if (!f64BitMode && strstr(pszRegs, " vm ")) /* a big ugly... */
1591 return pCmdHlp->pfnExec(pCmdHlp, "uv86 %s", szDisAndRegs + 2);
1592 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
1593 }
1594 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, "");
1595}
1596
1597
1598/**
1599 * @callback_method_impl{FNDBGCCMD, The 'rh' command.}
1600 */
1601static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1602{
1603 /*
1604 * Show all registers our selves.
1605 */
1606 if (cArgs == 0)
1607 {
1608 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1609 char szDisAndRegs[8192];
1610 int rc;
1611
1612 if (pDbgc->fRegTerse)
1613 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
1614 "u %VR{cs}:%VR{eip} L 0\n"
1615 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
1616 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
1617 ".cs=%04VR{cs} .ds=%04VR{ds} .es=%04VR{es} .fs=%04VR{fs} .gs=%04VR{gs} .ss=%04VR{ss} .eflags=%08VR{eflags}\n");
1618 else
1619 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
1620 "u %04VR{cs}:%08VR{eip} L 0\n"
1621 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
1622 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
1623 ".cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} .dr0=%08VR{dr0} .dr1=%08VR{dr1}\n"
1624 ".ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} .dr2=%08VR{dr2} .dr3=%08VR{dr3}\n"
1625 ".es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} .dr6=%08VR{dr6} .dr6=%08VR{dr6}\n"
1626 ".fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} .cr3=%016VR{cr3}\n"
1627 ".gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}}\n"
1628 ".ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
1629 ".gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} .idtr=%08VR{idtr_base}:%04VR{idtr_lim} .eflags=%08VR{eflags}\n"
1630 ".ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
1631 ".tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
1632 );
1633 if (RT_FAILURE(rc))
1634 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
1635 char *pszRegs = strchr(szDisAndRegs, '\n');
1636 *pszRegs++ = '\0';
1637 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
1638
1639 /*
1640 * Disassemble one instruction at cs:[r|e]ip.
1641 */
1642 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
1643 }
1644 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, ".");
1645}
1646
1647
1648/**
1649 * @callback_method_impl{FNDBGCCMD, The 'rt' command.}
1650 */
1651static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1652{
1653 NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
1654
1655 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1656 pDbgc->fRegTerse = !pDbgc->fRegTerse;
1657 return DBGCCmdHlpPrintf(pCmdHlp, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
1658}
1659
1660
1661/**
1662 * @callback_method_impl{FNDBGCCMD, The 't' command.}
1663 */
1664static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1665{
1666 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1667
1668 int rc = DBGFR3Step(pUVM, pDbgc->idCpu);
1669 if (RT_SUCCESS(rc))
1670 pDbgc->fReady = false;
1671 else
1672 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
1673
1674 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
1675 return rc;
1676}
1677
1678
1679/**
1680 * @callback_method_impl{FNDBGCCMD, The 'k'\, 'kg' and 'kh' commands.}
1681 */
1682static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1683{
1684 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1685
1686 /*
1687 * Figure which context we're called for and start walking that stack.
1688 */
1689 int rc;
1690 PCDBGFSTACKFRAME pFirstFrame;
1691 bool const fGuest = pCmd->pszCmd[1] == 'g'
1692 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
1693 rc = DBGFR3StackWalkBegin(pUVM, pDbgc->idCpu, fGuest ? DBGFCODETYPE_GUEST : DBGFCODETYPE_HYPER, &pFirstFrame);
1694 if (RT_FAILURE(rc))
1695 return DBGCCmdHlpPrintf(pCmdHlp, "Failed to begin stack walk, rc=%Rrc\n", rc);
1696
1697 /*
1698 * Print header.
1699 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
1700 */
1701 uint32_t fBitFlags = 0;
1702 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
1703 pFrame;
1704 pFrame = DBGFR3StackWalkNext(pFrame))
1705 {
1706 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
1707 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
1708 {
1709 if (fCurBitFlags != fBitFlags)
1710 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1711 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1712 pFrame->AddrFrame.Sel,
1713 (uint16_t)pFrame->AddrFrame.off,
1714 pFrame->AddrReturnFrame.Sel,
1715 (uint16_t)pFrame->AddrReturnFrame.off,
1716 (uint32_t)pFrame->AddrReturnPC.Sel,
1717 (uint32_t)pFrame->AddrReturnPC.off,
1718 pFrame->Args.au32[0],
1719 pFrame->Args.au32[1],
1720 pFrame->Args.au32[2],
1721 pFrame->Args.au32[3]);
1722 }
1723 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
1724 {
1725 if (fCurBitFlags != fBitFlags)
1726 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
1727 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
1728 (uint32_t)pFrame->AddrFrame.off,
1729 (uint32_t)pFrame->AddrReturnFrame.off,
1730 (uint32_t)pFrame->AddrReturnPC.Sel,
1731 (uint32_t)pFrame->AddrReturnPC.off,
1732 pFrame->Args.au32[0],
1733 pFrame->Args.au32[1],
1734 pFrame->Args.au32[2],
1735 pFrame->Args.au32[3]);
1736 }
1737 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
1738 {
1739 if (fCurBitFlags != fBitFlags)
1740 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
1741 rc = DBGCCmdHlpPrintf(pCmdHlp, "%016RX64 %04RX16:%016RX64 %016RX64",
1742 (uint64_t)pFrame->AddrFrame.off,
1743 pFrame->AddrReturnFrame.Sel,
1744 (uint64_t)pFrame->AddrReturnFrame.off,
1745 (uint64_t)pFrame->AddrReturnPC.off);
1746 }
1747 if (RT_FAILURE(rc))
1748 break;
1749 if (!pFrame->pSymPC)
1750 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1751 fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
1752 ? " %RTsel:%016RGv"
1753 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT
1754 ? " %RTsel:%08RGv"
1755 : " %RTsel:%04RGv"
1756 , pFrame->AddrPC.Sel, pFrame->AddrPC.off);
1757 else
1758 {
1759 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
1760 if (offDisp > 0)
1761 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
1762 else if (offDisp < 0)
1763 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
1764 else
1765 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", pFrame->pSymPC->szName);
1766 }
1767 if (RT_SUCCESS(rc) && pFrame->pLinePC)
1768 rc = DBGCCmdHlpPrintf(pCmdHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
1769 if (RT_SUCCESS(rc))
1770 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
1771 if (RT_FAILURE(rc))
1772 break;
1773
1774 fBitFlags = fCurBitFlags;
1775 }
1776
1777 DBGFR3StackWalkEnd(pFirstFrame);
1778
1779 NOREF(paArgs); NOREF(cArgs);
1780 return rc;
1781}
1782
1783
1784static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP pCmdHlp, PCX86DESC64 pDesc, unsigned iEntry, bool fHyper, bool *pfDblEntry)
1785{
1786 /* GUEST64 */
1787 int rc;
1788
1789 const char *pszHyper = fHyper ? " HYPER" : "";
1790 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
1791 if (pDesc->Gen.u1DescType)
1792 {
1793 static const char * const s_apszTypes[] =
1794 {
1795 "DataRO", /* 0 Read-Only */
1796 "DataRO", /* 1 Read-Only - Accessed */
1797 "DataRW", /* 2 Read/Write */
1798 "DataRW", /* 3 Read/Write - Accessed */
1799 "DownRO", /* 4 Expand-down, Read-Only */
1800 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
1801 "DownRW", /* 6 Expand-down, Read/Write */
1802 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
1803 "CodeEO", /* 8 Execute-Only */
1804 "CodeEO", /* 9 Execute-Only - Accessed */
1805 "CodeER", /* A Execute/Readable */
1806 "CodeER", /* B Execute/Readable - Accessed */
1807 "ConfE0", /* C Conforming, Execute-Only */
1808 "ConfE0", /* D Conforming, Execute-Only - Accessed */
1809 "ConfER", /* E Conforming, Execute/Readable */
1810 "ConfER" /* F Conforming, Execute/Readable - Accessed */
1811 };
1812 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
1813 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1814 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1815 uint32_t u32Base = X86DESC_BASE(pDesc);
1816 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
1817
1818 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
1819 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1820 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
1821 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
1822 }
1823 else
1824 {
1825 static const char * const s_apszTypes[] =
1826 {
1827 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
1828 "Ill-1 ", /* 1 0001 Available 16-bit TSS */
1829 "LDT ", /* 2 0010 LDT */
1830 "Ill-3 ", /* 3 0011 Busy 16-bit TSS */
1831 "Ill-4 ", /* 4 0100 16-bit Call Gate */
1832 "Ill-5 ", /* 5 0101 Task Gate */
1833 "Ill-6 ", /* 6 0110 16-bit Interrupt Gate */
1834 "Ill-7 ", /* 7 0111 16-bit Trap Gate */
1835 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
1836 "Tss64A", /* 9 1001 Available 32-bit TSS */
1837 "Ill-A ", /* A 1010 Reserved (Illegal) */
1838 "Tss64B", /* B 1011 Busy 32-bit TSS */
1839 "Call64", /* C 1100 32-bit Call Gate */
1840 "Ill-D ", /* D 1101 Reserved (Illegal) */
1841 "Int64 ", /* E 1110 32-bit Interrupt Gate */
1842 "Trap64" /* F 1111 32-bit Trap Gate */
1843 };
1844 switch (pDesc->Gen.u4Type)
1845 {
1846 /* raw */
1847 case X86_SEL_TYPE_SYS_UNDEFINED:
1848 case X86_SEL_TYPE_SYS_UNDEFINED2:
1849 case X86_SEL_TYPE_SYS_UNDEFINED4:
1850 case X86_SEL_TYPE_SYS_UNDEFINED3:
1851 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1852 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1853 case X86_SEL_TYPE_SYS_286_CALL_GATE:
1854 case X86_SEL_TYPE_SYS_286_INT_GATE:
1855 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
1856 case X86_SEL_TYPE_SYS_TASK_GATE:
1857 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
1858 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
1859 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
1860 break;
1861
1862 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1863 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1864 case X86_SEL_TYPE_SYS_LDT:
1865 {
1866 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
1867 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1868 const char *pszLong = pDesc->Gen.u1Long ? "LONG" : " ";
1869
1870 uint64_t u64Base = X86DESC64_BASE(pDesc);
1871 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
1872
1873 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%016RX64 Lim=%08x DPL=%d %s %s %s %sAVL=%d R=%d%s\n",
1874 iEntry, s_apszTypes[pDesc->Gen.u4Type], u64Base, cbLimit,
1875 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszLong, pszBig,
1876 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
1877 pszHyper);
1878 if (pfDblEntry)
1879 *pfDblEntry = true;
1880 break;
1881 }
1882
1883 case X86_SEL_TYPE_SYS_386_CALL_GATE:
1884 {
1885 unsigned cParams = pDesc->au8[4] & 0x1f;
1886 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
1887 RTSEL sel = pDesc->au16[1];
1888 uint64_t off = pDesc->au16[0]
1889 | ((uint64_t)pDesc->au16[3] << 16)
1890 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
1891 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s %s=%d%s\n",
1892 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
1893 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
1894 if (pfDblEntry)
1895 *pfDblEntry = true;
1896 break;
1897 }
1898
1899 case X86_SEL_TYPE_SYS_386_INT_GATE:
1900 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
1901 {
1902 RTSEL sel = pDesc->Gate.u16Sel;
1903 uint64_t off = pDesc->Gate.u16OffsetLow
1904 | ((uint64_t)pDesc->Gate.u16OffsetHigh << 16)
1905 | ((uint64_t)pDesc->Gate.u32OffsetTop << 32);
1906 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%u %s IST=%u%s\n",
1907 iEntry, s_apszTypes[pDesc->Gate.u4Type], sel, off,
1908 pDesc->Gate.u2Dpl, pszPresent, pDesc->Gate.u3IST, pszHyper);
1909 if (pfDblEntry)
1910 *pfDblEntry = true;
1911 break;
1912 }
1913
1914 /* impossible, just it's necessary to keep gcc happy. */
1915 default:
1916 return VINF_SUCCESS;
1917 }
1918 }
1919 return VINF_SUCCESS;
1920}
1921
1922
1923/**
1924 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
1925 *
1926 * @returns pfnPrintf status code.
1927 * @param pCmdHlp The DBGC command helpers.
1928 * @param pDesc The descriptor to display.
1929 * @param iEntry The descriptor entry number.
1930 * @param fHyper Whether the selector belongs to the hypervisor or not.
1931 */
1932static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
1933{
1934 int rc;
1935
1936 const char *pszHyper = fHyper ? " HYPER" : "";
1937 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
1938 if (pDesc->Gen.u1DescType)
1939 {
1940 static const char * const s_apszTypes[] =
1941 {
1942 "DataRO", /* 0 Read-Only */
1943 "DataRO", /* 1 Read-Only - Accessed */
1944 "DataRW", /* 2 Read/Write */
1945 "DataRW", /* 3 Read/Write - Accessed */
1946 "DownRO", /* 4 Expand-down, Read-Only */
1947 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
1948 "DownRW", /* 6 Expand-down, Read/Write */
1949 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
1950 "CodeEO", /* 8 Execute-Only */
1951 "CodeEO", /* 9 Execute-Only - Accessed */
1952 "CodeER", /* A Execute/Readable */
1953 "CodeER", /* B Execute/Readable - Accessed */
1954 "ConfE0", /* C Conforming, Execute-Only */
1955 "ConfE0", /* D Conforming, Execute-Only - Accessed */
1956 "ConfER", /* E Conforming, Execute/Readable */
1957 "ConfER" /* F Conforming, Execute/Readable - Accessed */
1958 };
1959 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
1960 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
1961 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
1962 uint32_t u32Base = pDesc->Gen.u16BaseLow
1963 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
1964 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
1965 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
1966 if (pDesc->Gen.u1Granularity)
1967 cbLimit <<= PAGE_SHIFT;
1968
1969 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
1970 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
1971 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
1972 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
1973 }
1974 else
1975 {
1976 static const char * const s_apszTypes[] =
1977 {
1978 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
1979 "Tss16A", /* 1 0001 Available 16-bit TSS */
1980 "LDT ", /* 2 0010 LDT */
1981 "Tss16B", /* 3 0011 Busy 16-bit TSS */
1982 "Call16", /* 4 0100 16-bit Call Gate */
1983 "TaskG ", /* 5 0101 Task Gate */
1984 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
1985 "Trap16", /* 7 0111 16-bit Trap Gate */
1986 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
1987 "Tss32A", /* 9 1001 Available 32-bit TSS */
1988 "Ill-A ", /* A 1010 Reserved (Illegal) */
1989 "Tss32B", /* B 1011 Busy 32-bit TSS */
1990 "Call32", /* C 1100 32-bit Call Gate */
1991 "Ill-D ", /* D 1101 Reserved (Illegal) */
1992 "Int32 ", /* E 1110 32-bit Interrupt Gate */
1993 "Trap32" /* F 1111 32-bit Trap Gate */
1994 };
1995 switch (pDesc->Gen.u4Type)
1996 {
1997 /* raw */
1998 case X86_SEL_TYPE_SYS_UNDEFINED:
1999 case X86_SEL_TYPE_SYS_UNDEFINED2:
2000 case X86_SEL_TYPE_SYS_UNDEFINED4:
2001 case X86_SEL_TYPE_SYS_UNDEFINED3:
2002 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
2003 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
2004 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2005 break;
2006
2007 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2008 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2009 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2010 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2011 case X86_SEL_TYPE_SYS_LDT:
2012 {
2013 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2014 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
2015 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2016 uint32_t u32Base = pDesc->Gen.u16BaseLow
2017 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
2018 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
2019 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
2020 if (pDesc->Gen.u1Granularity)
2021 cbLimit <<= PAGE_SHIFT;
2022
2023 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
2024 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2025 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
2026 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
2027 pszHyper);
2028 break;
2029 }
2030
2031 case X86_SEL_TYPE_SYS_TASK_GATE:
2032 {
2033 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s TSS=%04x DPL=%d %s%s\n",
2034 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
2035 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2036 break;
2037 }
2038
2039 case X86_SEL_TYPE_SYS_286_CALL_GATE:
2040 case X86_SEL_TYPE_SYS_386_CALL_GATE:
2041 {
2042 unsigned cParams = pDesc->au8[4] & 0x1f;
2043 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
2044 RTSEL sel = pDesc->au16[1];
2045 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2046 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
2047 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2048 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
2049 break;
2050 }
2051
2052 case X86_SEL_TYPE_SYS_286_INT_GATE:
2053 case X86_SEL_TYPE_SYS_386_INT_GATE:
2054 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2055 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2056 {
2057 RTSEL sel = pDesc->au16[1];
2058 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2059 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
2060 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2061 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2062 break;
2063 }
2064
2065 /* impossible, just it's necessary to keep gcc happy. */
2066 default:
2067 return VINF_SUCCESS;
2068 }
2069 }
2070 return rc;
2071}
2072
2073
2074/**
2075 * @callback_method_impl{FNDBGCCMD, The 'dg'\, 'dga'\, 'dl' and 'dla' commands.}
2076 */
2077static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2078{
2079 /*
2080 * Validate input.
2081 */
2082 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2083
2084 /*
2085 * Get the CPU mode, check which command variation this is
2086 * and fix a default parameter if needed.
2087 */
2088 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2089 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2090 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2091 bool fGdt = pCmd->pszCmd[1] == 'g';
2092 bool fAll = pCmd->pszCmd[2] == 'a';
2093 RTSEL SelTable = fGdt ? 0 : X86_SEL_LDT;
2094
2095 DBGCVAR Var;
2096 if (!cArgs)
2097 {
2098 cArgs = 1;
2099 paArgs = &Var;
2100 Var.enmType = DBGCVAR_TYPE_NUMBER;
2101 Var.u.u64Number = 0;
2102 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2103 Var.u64Range = 1024;
2104 }
2105
2106 /*
2107 * Process the arguments.
2108 */
2109 for (unsigned i = 0; i < cArgs; i++)
2110 {
2111 /*
2112 * Retrieve the selector value from the argument.
2113 * The parser may confuse pointers and numbers if more than one
2114 * argument is given, that that into account.
2115 */
2116 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[i].enmType));
2117 uint64_t u64;
2118 unsigned cSels = 1;
2119 switch (paArgs[i].enmType)
2120 {
2121 case DBGCVAR_TYPE_NUMBER:
2122 u64 = paArgs[i].u.u64Number;
2123 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
2124 cSels = RT_MIN(paArgs[i].u64Range, 1024);
2125 break;
2126 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
2127 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
2128 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
2129 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
2130 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
2131 default: u64 = _64K; break;
2132 }
2133 if (u64 < _64K)
2134 {
2135 unsigned Sel = (RTSEL)u64;
2136
2137 /*
2138 * Dump the specified range.
2139 */
2140 bool fSingle = cSels == 1;
2141 while ( cSels-- > 0
2142 && Sel < _64K)
2143 {
2144 DBGFSELINFO SelInfo;
2145 int rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, Sel | SelTable, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
2146 if (RT_SUCCESS(rc))
2147 {
2148 if (SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE)
2149 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x RealM Bas=%04x Lim=%04x\n",
2150 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
2151 else if ( fAll
2152 || fSingle
2153 || SelInfo.u.Raw.Gen.u1Present)
2154 {
2155 if (enmMode == CPUMMODE_PROTECTED)
2156 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &SelInfo.u.Raw, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER));
2157 else
2158 {
2159 bool fDblSkip = false;
2160 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &SelInfo.u.Raw64, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), &fDblSkip);
2161 if (fDblSkip)
2162 Sel += 4;
2163 }
2164 }
2165 }
2166 else
2167 {
2168 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %Rrc\n", Sel, rc);
2169 if (!fAll)
2170 return rc;
2171 }
2172 if (RT_FAILURE(rc))
2173 return rc;
2174
2175 /* next */
2176 Sel += 8;
2177 }
2178 }
2179 else
2180 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds\n", u64);
2181 }
2182
2183 return VINF_SUCCESS;
2184}
2185
2186
2187/**
2188 * @callback_method_impl{FNDBGCCMD, The 'di' and 'dia' commands.}
2189 */
2190static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2191{
2192 /*
2193 * Validate input.
2194 */
2195 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2196
2197 /*
2198 * Establish some stuff like the current IDTR and CPU mode,
2199 * and fix a default parameter.
2200 */
2201 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2202 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2203 uint16_t cbLimit;
2204 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVCpu, &cbLimit);
2205 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2206 unsigned cbEntry;
2207 switch (enmMode)
2208 {
2209 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
2210 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
2211 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
2212 default:
2213 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid CPU mode %d.\n", enmMode);
2214 }
2215
2216 bool fAll = pCmd->pszCmd[2] == 'a';
2217 DBGCVAR Var;
2218 if (!cArgs)
2219 {
2220 cArgs = 1;
2221 paArgs = &Var;
2222 Var.enmType = DBGCVAR_TYPE_NUMBER;
2223 Var.u.u64Number = 0;
2224 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2225 Var.u64Range = 256;
2226 }
2227
2228 /*
2229 * Process the arguments.
2230 */
2231 for (unsigned i = 0; i < cArgs; i++)
2232 {
2233 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER);
2234 if (paArgs[i].u.u64Number < 256)
2235 {
2236 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
2237 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
2238 ? paArgs[i].u64Range
2239 : 1;
2240 bool fSingle = cInts == 1;
2241 while ( cInts-- > 0
2242 && iInt < 256)
2243 {
2244 /*
2245 * Try read it.
2246 */
2247 union
2248 {
2249 RTFAR16 Real;
2250 X86DESC Prot;
2251 X86DESC64 Long;
2252 } u;
2253 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
2254 {
2255 DBGCCmdHlpPrintf(pCmdHlp, "%04x not within the IDT\n", (unsigned)iInt);
2256 if (!fAll && !fSingle)
2257 return VINF_SUCCESS;
2258 }
2259 DBGCVAR AddrVar;
2260 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
2261 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
2262 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
2263 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &u, cbEntry, &AddrVar, NULL);
2264 if (RT_FAILURE(rc))
2265 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
2266
2267 /*
2268 * Display it.
2269 */
2270 switch (enmMode)
2271 {
2272 case CPUMMODE_REAL:
2273 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
2274 /** @todo resolve 16:16 IDTE to a symbol */
2275 break;
2276 case CPUMMODE_PROTECTED:
2277 if (fAll || fSingle || u.Prot.Gen.u1Present)
2278 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
2279 break;
2280 case CPUMMODE_LONG:
2281 if (fAll || fSingle || u.Long.Gen.u1Present)
2282 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
2283 break;
2284 default: break; /* to shut up gcc */
2285 }
2286 if (RT_FAILURE(rc))
2287 return rc;
2288
2289 /* next */
2290 iInt++;
2291 }
2292 }
2293 else
2294 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
2295 }
2296
2297 return VINF_SUCCESS;
2298}
2299
2300
2301/**
2302 * @callback_method_impl{FNDBGCCMD,
2303 * The 'da'\, 'dq'\, 'dd'\, 'dw' and 'db' commands.}
2304 */
2305static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2306{
2307 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2308
2309 /*
2310 * Validate input.
2311 */
2312 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
2313 if (cArgs == 1)
2314 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
2315 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2316
2317 /*
2318 * Figure out the element size.
2319 */
2320 unsigned cbElement;
2321 bool fAscii = false;
2322 switch (pCmd->pszCmd[1])
2323 {
2324 default:
2325 case 'b': cbElement = 1; break;
2326 case 'w': cbElement = 2; break;
2327 case 'd': cbElement = 4; break;
2328 case 'q': cbElement = 8; break;
2329 case 'a':
2330 cbElement = 1;
2331 fAscii = true;
2332 break;
2333 case '\0':
2334 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
2335 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
2336 if (!cbElement)
2337 cbElement = 1;
2338 break;
2339 }
2340
2341 /*
2342 * Find address.
2343 */
2344 if (!cArgs)
2345 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
2346 else
2347 pDbgc->DumpPos = paArgs[0];
2348
2349 /*
2350 * Range.
2351 */
2352 switch (pDbgc->DumpPos.enmRangeType)
2353 {
2354 case DBGCVAR_RANGE_NONE:
2355 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2356 pDbgc->DumpPos.u64Range = 0x60;
2357 break;
2358
2359 case DBGCVAR_RANGE_ELEMENTS:
2360 if (pDbgc->DumpPos.u64Range > 2048)
2361 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many elements requested. Max is 2048 elements.\n");
2362 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2363 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
2364 break;
2365
2366 case DBGCVAR_RANGE_BYTES:
2367 if (pDbgc->DumpPos.u64Range > 65536)
2368 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
2369 break;
2370
2371 default:
2372 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
2373 }
2374
2375 pDbgc->pLastPos = &pDbgc->DumpPos;
2376
2377 /*
2378 * Do the dumping.
2379 */
2380 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
2381 int cbLeft = (int)pDbgc->DumpPos.u64Range;
2382 uint8_t u8Prev = '\0';
2383 for (;;)
2384 {
2385 /*
2386 * Read memory.
2387 */
2388 char achBuffer[16];
2389 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
2390 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
2391 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
2392 if (RT_FAILURE(rc))
2393 {
2394 if (u8Prev && u8Prev != '\n')
2395 DBGCCmdHlpPrintf(pCmdHlp, "\n");
2396 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
2397 }
2398
2399 /*
2400 * Display it.
2401 */
2402 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
2403 if (!fAscii)
2404 {
2405 DBGCCmdHlpPrintf(pCmdHlp, "%DV:", &pDbgc->DumpPos);
2406 unsigned i;
2407 for (i = 0; i < cb; i += cbElement)
2408 {
2409 const char *pszSpace = " ";
2410 if (cbElement <= 2 && i == 8 && !fAscii)
2411 pszSpace = "-";
2412 switch (cbElement)
2413 {
2414 case 1: DBGCCmdHlpPrintf(pCmdHlp, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
2415 case 2: DBGCCmdHlpPrintf(pCmdHlp, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
2416 case 4: DBGCCmdHlpPrintf(pCmdHlp, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
2417 case 8: DBGCCmdHlpPrintf(pCmdHlp, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
2418 }
2419 }
2420
2421 /* chars column */
2422 if (pDbgc->cbDumpElement == 1)
2423 {
2424 while (i++ < sizeof(achBuffer))
2425 DBGCCmdHlpPrintf(pCmdHlp, " ");
2426 DBGCCmdHlpPrintf(pCmdHlp, " ");
2427 for (i = 0; i < cb; i += cbElement)
2428 {
2429 uint8_t u8 = *(uint8_t *)&achBuffer[i];
2430 if (RT_C_IS_PRINT(u8) && u8 < 127 && u8 >= 32)
2431 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
2432 else
2433 DBGCCmdHlpPrintf(pCmdHlp, ".");
2434 }
2435 }
2436 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2437 }
2438 else
2439 {
2440 /*
2441 * We print up to the first zero and stop there.
2442 * Only printables + '\t' and '\n' are printed.
2443 */
2444 if (!u8Prev)
2445 DBGCCmdHlpPrintf(pCmdHlp, "%DV:\n", &pDbgc->DumpPos);
2446 uint8_t u8 = '\0';
2447 unsigned i;
2448 for (i = 0; i < cb; i++)
2449 {
2450 u8Prev = u8;
2451 u8 = *(uint8_t *)&achBuffer[i];
2452 if ( u8 < 127
2453 && ( (RT_C_IS_PRINT(u8) && u8 >= 32)
2454 || u8 == '\t'
2455 || u8 == '\n'))
2456 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
2457 else if (!u8)
2458 break;
2459 else
2460 DBGCCmdHlpPrintf(pCmdHlp, "\\x%x", u8);
2461 }
2462 if (u8 == '\0')
2463 cb = cbLeft = i + 1;
2464 if (cbLeft - cb <= 0 && u8Prev != '\n')
2465 DBGCCmdHlpPrintf(pCmdHlp, "\n");
2466 }
2467
2468 /*
2469 * Advance
2470 */
2471 cbLeft -= (int)cb;
2472 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
2473 if (RT_FAILURE(rc))
2474 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
2475 if (cbLeft <= 0)
2476 break;
2477 }
2478
2479 NOREF(pCmd);
2480 return VINF_SUCCESS;
2481}
2482
2483
2484/**
2485 * Best guess at which paging mode currently applies to the guest
2486 * paging structures.
2487 *
2488 * This have to come up with a decent answer even when the guest
2489 * is in non-paged protected mode or real mode.
2490 *
2491 * @returns cr3.
2492 * @param pDbgc The DBGC instance.
2493 * @param pfPAE Where to store the page address extension indicator.
2494 * @param pfLME Where to store the long mode enabled indicator.
2495 * @param pfPSE Where to store the page size extension indicator.
2496 * @param pfPGE Where to store the page global enabled indicator.
2497 * @param pfNXE Where to store the no-execution enabled indicator.
2498 */
2499static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2500{
2501 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
2502 RTGCUINTREG cr4 = CPUMGetGuestCR4(pVCpu);
2503 *pfPSE = !!(cr4 & X86_CR4_PSE);
2504 *pfPGE = !!(cr4 & X86_CR4_PGE);
2505 if (cr4 & X86_CR4_PAE)
2506 {
2507 *pfPSE = true;
2508 *pfPAE = true;
2509 }
2510 else
2511 *pfPAE = false;
2512
2513 *pfLME = CPUMGetGuestMode(pVCpu) == CPUMMODE_LONG;
2514 *pfNXE = false; /* GUEST64 GUESTNX */
2515 return CPUMGetGuestCR3(pVCpu);
2516}
2517
2518
2519/**
2520 * Determine the shadow paging mode.
2521 *
2522 * @returns cr3.
2523 * @param pDbgc The DBGC instance.
2524 * @param pfPAE Where to store the page address extension indicator.
2525 * @param pfLME Where to store the long mode enabled indicator.
2526 * @param pfPSE Where to store the page size extension indicator.
2527 * @param pfPGE Where to store the page global enabled indicator.
2528 * @param pfNXE Where to store the no-execution enabled indicator.
2529 */
2530static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
2531{
2532 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
2533
2534 *pfPSE = true;
2535 *pfPGE = false;
2536 switch (PGMGetShadowMode(pVCpu))
2537 {
2538 default:
2539 case PGMMODE_32_BIT:
2540 *pfPAE = *pfLME = *pfNXE = false;
2541 break;
2542 case PGMMODE_PAE:
2543 *pfLME = *pfNXE = false;
2544 *pfPAE = true;
2545 break;
2546 case PGMMODE_PAE_NX:
2547 *pfLME = false;
2548 *pfPAE = *pfNXE = true;
2549 break;
2550 case PGMMODE_AMD64:
2551 *pfNXE = false;
2552 *pfPAE = *pfLME = true;
2553 break;
2554 case PGMMODE_AMD64_NX:
2555 *pfPAE = *pfLME = *pfNXE = true;
2556 break;
2557 }
2558 return PGMGetHyperCR3(pVCpu);
2559}
2560
2561
2562/**
2563 * @callback_method_impl{FNDBGCCMD,
2564 * The 'dpd'\, 'dpda'\, 'dpdb'\, 'dpdg' and 'dpdh' commands.}
2565 */
2566static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2567{
2568 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2569
2570 /*
2571 * Validate input.
2572 */
2573 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
2574 if (cArgs == 1 && pCmd->pszCmd[3] == 'a')
2575 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
2576 if (cArgs == 1 && pCmd->pszCmd[3] != 'a')
2577 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2578 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
2579 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2580
2581 /*
2582 * Guest or shadow page directories? Get the paging parameters.
2583 */
2584 bool fGuest = pCmd->pszCmd[3] != 'h';
2585 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2586 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2587 ? pDbgc->fRegCtxGuest
2588 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2589
2590 bool fPAE, fLME, fPSE, fPGE, fNXE;
2591 uint64_t cr3 = fGuest
2592 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2593 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2594 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2595
2596 /*
2597 * Setup default argument if none was specified.
2598 * Fix address / index confusion.
2599 */
2600 DBGCVAR VarDefault;
2601 if (!cArgs)
2602 {
2603 if (pCmd->pszCmd[3] == 'a')
2604 {
2605 if (fLME || fPAE)
2606 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");
2607 if (fGuest)
2608 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
2609 else
2610 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
2611 }
2612 else
2613 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
2614 paArgs = &VarDefault;
2615 cArgs = 1;
2616 }
2617 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
2618 {
2619 /* If it's a number (not an address), it's an index, so convert it to an address. */
2620 Assert(pCmd->pszCmd[3] != 'a');
2621 VarDefault = paArgs[0];
2622 if (fPAE)
2623 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
2624 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
2625 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
2626 VarDefault.u.u64Number <<= X86_PD_SHIFT;
2627 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
2628 paArgs = &VarDefault;
2629 }
2630
2631 /*
2632 * Locate the PDE to start displaying at.
2633 *
2634 * The 'dpda' command takes the address of a PDE, while the others are guest
2635 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
2636 * while the others require us to do all the tedious walking thru the paging
2637 * hierarchy to find the intended PDE.
2638 */
2639 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
2640 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
2641 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
2642 unsigned cEntries; /* The number of entries to display. */
2643 unsigned cEntriesMax; /* The max number of entries to display. */
2644 int rc;
2645 if (pCmd->pszCmd[3] == 'a')
2646 {
2647 VarPDEAddr = paArgs[0];
2648 switch (VarPDEAddr.enmRangeType)
2649 {
2650 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
2651 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
2652 default: cEntries = 10; break;
2653 }
2654 cEntriesMax = PAGE_SIZE / cbEntry;
2655 }
2656 else
2657 {
2658 /*
2659 * Determine the range.
2660 */
2661 switch (paArgs[0].enmRangeType)
2662 {
2663 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
2664 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
2665 default: cEntries = 10; break;
2666 }
2667
2668 /*
2669 * Normalize the input address, it must be a flat GC address.
2670 */
2671 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2672 if (RT_FAILURE(rc))
2673 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2674 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
2675 {
2676 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
2677 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
2678 }
2679 if (fPAE)
2680 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
2681 else
2682 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
2683
2684 /*
2685 * Do the paging walk until we get to the page directory.
2686 */
2687 DBGCVAR VarCur;
2688 if (fGuest)
2689 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
2690 else
2691 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
2692 if (fLME)
2693 {
2694 /* Page Map Level 4 Lookup. */
2695 /* Check if it's a valid address first? */
2696 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
2697 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
2698 X86PML4E Pml4e;
2699 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
2700 if (RT_FAILURE(rc))
2701 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
2702 if (!Pml4e.n.u1Present)
2703 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
2704
2705 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
2706 Assert(fPAE);
2707 }
2708 if (fPAE)
2709 {
2710 /* Page directory pointer table. */
2711 X86PDPE Pdpe;
2712 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
2713 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
2714 if (RT_FAILURE(rc))
2715 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
2716 if (!Pdpe.n.u1Present)
2717 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
2718
2719 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2720 VarPDEAddr = VarCur;
2721 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
2722 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
2723 }
2724 else
2725 {
2726 /* 32-bit legacy - CR3 == page directory. */
2727 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
2728 VarPDEAddr = VarCur;
2729 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
2730 }
2731 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
2732 }
2733
2734 /* adjust cEntries */
2735 cEntries = RT_MAX(1, cEntries);
2736 cEntries = RT_MIN(cEntries, cEntriesMax);
2737
2738 /*
2739 * The display loop.
2740 */
2741 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
2742 &VarPDEAddr, iEntry);
2743 do
2744 {
2745 /*
2746 * Read.
2747 */
2748 X86PDEPAE Pde;
2749 Pde.u = 0;
2750 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, cbEntry, &VarPDEAddr, NULL);
2751 if (RT_FAILURE(rc))
2752 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
2753
2754 /*
2755 * Display.
2756 */
2757 if (iEntry != ~0U)
2758 {
2759 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
2760 iEntry++;
2761 }
2762 if (fPSE && Pde.b.u1Size)
2763 DBGCCmdHlpPrintf(pCmdHlp,
2764 fPAE
2765 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
2766 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
2767 Pde.u,
2768 Pde.u & X86_PDE_PAE_PG_MASK,
2769 Pde.b.u1Present ? "p " : "np",
2770 Pde.b.u1Write ? "w" : "r",
2771 Pde.b.u1User ? "u" : "s",
2772 Pde.b.u1Accessed ? "a " : "na",
2773 Pde.b.u1Dirty ? "d " : "nd",
2774 Pde.b.u3Available,
2775 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
2776 Pde.b.u1WriteThru ? "pwt" : " ",
2777 Pde.b.u1CacheDisable ? "pcd" : " ",
2778 Pde.b.u1PAT ? "pat" : "",
2779 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2780 else
2781 DBGCCmdHlpPrintf(pCmdHlp,
2782 fPAE
2783 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
2784 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
2785 Pde.u,
2786 Pde.u & X86_PDE_PAE_PG_MASK,
2787 Pde.n.u1Present ? "p " : "np",
2788 Pde.n.u1Write ? "w" : "r",
2789 Pde.n.u1User ? "u" : "s",
2790 Pde.n.u1Accessed ? "a " : "na",
2791 Pde.u & RT_BIT(6) ? "6 " : " ",
2792 Pde.n.u3Available,
2793 Pde.u & RT_BIT(8) ? "8" : " ",
2794 Pde.n.u1WriteThru ? "pwt" : " ",
2795 Pde.n.u1CacheDisable ? "pcd" : " ",
2796 Pde.u & RT_BIT(7) ? "7" : "",
2797 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
2798 if (Pde.u & UINT64_C(0x7fff000000000000))
2799 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
2800 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2801 if (RT_FAILURE(rc))
2802 return rc;
2803
2804 /*
2805 * Advance.
2806 */
2807 VarPDEAddr.u.u64Number += cbEntry;
2808 if (iEntry != ~0U)
2809 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
2810 } while (cEntries-- > 0);
2811
2812 return VINF_SUCCESS;
2813}
2814
2815
2816/**
2817 * @callback_method_impl{FNDBGCCMD, The 'dpdb' command.}
2818 */
2819static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2820{
2821 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2822 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2823 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2824 if (RT_FAILURE(rc1))
2825 return rc1;
2826 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2827 return rc2;
2828}
2829
2830
2831/**
2832 * @callback_method_impl{FNDBGCCMD, The 'dph*' commands and main part of 'm'.}
2833 */
2834static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2835{
2836 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2837 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2838
2839 /*
2840 * Figure the context and base flags.
2841 */
2842 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;
2843 if (pCmd->pszCmd[0] == 'm')
2844 fFlags |= DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW;
2845 else if (pCmd->pszCmd[3] == '\0')
2846 fFlags |= pDbgc->fRegCtxGuest ? DBGFPGDMP_FLAGS_GUEST : DBGFPGDMP_FLAGS_SHADOW;
2847 else if (pCmd->pszCmd[3] == 'g')
2848 fFlags |= DBGFPGDMP_FLAGS_GUEST;
2849 else if (pCmd->pszCmd[3] == 'h')
2850 fFlags |= DBGFPGDMP_FLAGS_SHADOW;
2851 else
2852 AssertFailed();
2853
2854 if (pDbgc->cPagingHierarchyDumps == 0)
2855 fFlags |= DBGFPGDMP_FLAGS_HEADER;
2856 pDbgc->cPagingHierarchyDumps = (pDbgc->cPagingHierarchyDumps + 1) % 42;
2857
2858 /*
2859 * Get the range.
2860 */
2861 PCDBGCVAR pRange = cArgs > 0 ? &paArgs[0] : pDbgc->pLastPos;
2862 RTGCPTR GCPtrFirst = NIL_RTGCPTR;
2863 int rc = DBGCCmdHlpVarToFlatAddr(pCmdHlp, pRange, &GCPtrFirst);
2864 if (RT_FAILURE(rc))
2865 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to convert %DV to a flat address: %Rrc", pRange, rc);
2866
2867 uint64_t cbRange;
2868 rc = DBGCCmdHlpVarGetRange(pCmdHlp, pRange, PAGE_SIZE, PAGE_SIZE * 8, &cbRange);
2869 if (RT_FAILURE(rc))
2870 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to obtain the range of %DV: %Rrc", pRange, rc);
2871
2872 RTGCPTR GCPtrLast = RTGCPTR_MAX - GCPtrFirst;
2873 if (cbRange >= GCPtrLast)
2874 GCPtrLast = RTGCPTR_MAX;
2875 else if (!cbRange)
2876 GCPtrLast = GCPtrFirst;
2877 else
2878 GCPtrLast = GCPtrFirst + cbRange - 1;
2879
2880 /*
2881 * Do we have a CR3?
2882 */
2883 uint64_t cr3 = 0;
2884 if (cArgs > 1)
2885 {
2886 if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
2887 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No CR3 or mode arguments when dumping both context, please.");
2888 if (paArgs[1].enmType != DBGCVAR_TYPE_NUMBER)
2889 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The CR3 argument is not a number: %DV", &paArgs[1]);
2890 cr3 = paArgs[1].u.u64Number;
2891 }
2892 else
2893 fFlags |= DBGFPGDMP_FLAGS_CURRENT_CR3;
2894
2895 /*
2896 * Do we have a mode?
2897 */
2898 if (cArgs > 2)
2899 {
2900 if (paArgs[2].enmType != DBGCVAR_TYPE_STRING)
2901 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The mode argument is not a string: %DV", &paArgs[2]);
2902 static const struct MODETOFLAGS
2903 {
2904 const char *pszName;
2905 uint32_t fFlags;
2906 } s_aModeToFlags[] =
2907 {
2908 { "ept", DBGFPGDMP_FLAGS_EPT },
2909 { "legacy", 0 },
2910 { "legacy-np", DBGFPGDMP_FLAGS_NP },
2911 { "pse", DBGFPGDMP_FLAGS_PSE },
2912 { "pse-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NP },
2913 { "pae", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE },
2914 { "pae-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NP },
2915 { "pae-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE },
2916 { "pae-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP },
2917 { "long", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME },
2918 { "long-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NP },
2919 { "long-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE },
2920 { "long-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP }
2921 };
2922 int i = RT_ELEMENTS(s_aModeToFlags);
2923 while (i-- > 0)
2924 if (!strcmp(s_aModeToFlags[i].pszName, paArgs[2].u.pszString))
2925 {
2926 fFlags |= s_aModeToFlags[i].fFlags;
2927 break;
2928 }
2929 if (i < 0)
2930 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown mode: \"%s\"", paArgs[2].u.pszString);
2931 }
2932 else
2933 fFlags |= DBGFPGDMP_FLAGS_CURRENT_MODE;
2934
2935 /*
2936 * Call the worker.
2937 */
2938 rc = DBGFR3PagingDumpEx(pUVM, pDbgc->idCpu, fFlags, cr3, GCPtrFirst, GCPtrLast, 99 /*cMaxDepth*/,
2939 DBGCCmdHlpGetDbgfOutputHlp(pCmdHlp));
2940 if (RT_FAILURE(rc))
2941 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3PagingDumpEx: %Rrc\n", rc);
2942 return VINF_SUCCESS;
2943}
2944
2945
2946
2947/**
2948 * @callback_method_impl{FNDBGCCMD, The 'dpg*' commands.}
2949 */
2950static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2951{
2952 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2953
2954 /*
2955 * Validate input.
2956 */
2957 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1);
2958 if (pCmd->pszCmd[3] == 'a')
2959 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
2960 else
2961 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2962 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
2963 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2964
2965 /*
2966 * Guest or shadow page tables? Get the paging parameters.
2967 */
2968 bool fGuest = pCmd->pszCmd[3] != 'h';
2969 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
2970 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
2971 ? pDbgc->fRegCtxGuest
2972 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
2973
2974 bool fPAE, fLME, fPSE, fPGE, fNXE;
2975 uint64_t cr3 = fGuest
2976 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
2977 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
2978 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
2979
2980 /*
2981 * Locate the PTE to start displaying at.
2982 *
2983 * The 'dpta' command takes the address of a PTE, while the others are guest
2984 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
2985 * while the others require us to do all the tedious walking thru the paging
2986 * hierarchy to find the intended PTE.
2987 */
2988 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
2989 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
2990 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
2991 unsigned cEntries; /* The number of entries to display. */
2992 unsigned cEntriesMax; /* The max number of entries to display. */
2993 int rc;
2994 if (pCmd->pszCmd[3] == 'a')
2995 {
2996 VarPTEAddr = paArgs[0];
2997 switch (VarPTEAddr.enmRangeType)
2998 {
2999 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
3000 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
3001 default: cEntries = 10; break;
3002 }
3003 cEntriesMax = PAGE_SIZE / cbEntry;
3004 }
3005 else
3006 {
3007 /*
3008 * Determine the range.
3009 */
3010 switch (paArgs[0].enmRangeType)
3011 {
3012 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3013 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3014 default: cEntries = 10; break;
3015 }
3016
3017 /*
3018 * Normalize the input address, it must be a flat GC address.
3019 */
3020 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3021 if (RT_FAILURE(rc))
3022 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3023 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3024 {
3025 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3026 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3027 }
3028 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
3029
3030 /*
3031 * Do the paging walk until we get to the page table.
3032 */
3033 DBGCVAR VarCur;
3034 if (fGuest)
3035 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3036 else
3037 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3038 if (fLME)
3039 {
3040 /* Page Map Level 4 Lookup. */
3041 /* Check if it's a valid address first? */
3042 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3043 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3044 X86PML4E Pml4e;
3045 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3046 if (RT_FAILURE(rc))
3047 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3048 if (!Pml4e.n.u1Present)
3049 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3050
3051 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3052 Assert(fPAE);
3053 }
3054 if (fPAE)
3055 {
3056 /* Page directory pointer table. */
3057 X86PDPE Pdpe;
3058 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3059 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3060 if (RT_FAILURE(rc))
3061 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3062 if (!Pdpe.n.u1Present)
3063 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3064
3065 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3066
3067 /* Page directory (PAE). */
3068 X86PDEPAE Pde;
3069 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
3070 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
3071 if (RT_FAILURE(rc))
3072 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3073 if (!Pde.n.u1Present)
3074 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3075 if (fPSE && Pde.n.u1Size)
3076 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3077
3078 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
3079 VarPTEAddr = VarCur;
3080 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
3081 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
3082 }
3083 else
3084 {
3085 /* Page directory (legacy). */
3086 X86PDE Pde;
3087 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
3088 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
3089 if (RT_FAILURE(rc))
3090 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3091 if (!Pde.n.u1Present)
3092 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3093 if (fPSE && Pde.n.u1Size)
3094 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3095
3096 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
3097 VarPTEAddr = VarCur;
3098 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
3099 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
3100 }
3101 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3102 }
3103
3104 /* adjust cEntries */
3105 cEntries = RT_MAX(1, cEntries);
3106 cEntries = RT_MIN(cEntries, cEntriesMax);
3107
3108 /*
3109 * The display loop.
3110 */
3111 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
3112 &VarPTEAddr, &VarGCPtr, iEntry);
3113 do
3114 {
3115 /*
3116 * Read.
3117 */
3118 X86PTEPAE Pte;
3119 Pte.u = 0;
3120 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pte, cbEntry, &VarPTEAddr, NULL);
3121 if (RT_FAILURE(rc))
3122 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
3123
3124 /*
3125 * Display.
3126 */
3127 if (iEntry != ~0U)
3128 {
3129 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3130 iEntry++;
3131 }
3132 DBGCCmdHlpPrintf(pCmdHlp,
3133 fPAE
3134 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3135 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3136 Pte.u,
3137 Pte.u & X86_PTE_PAE_PG_MASK,
3138 Pte.n.u1Present ? "p " : "np",
3139 Pte.n.u1Write ? "w" : "r",
3140 Pte.n.u1User ? "u" : "s",
3141 Pte.n.u1Accessed ? "a " : "na",
3142 Pte.n.u1Dirty ? "d " : "nd",
3143 Pte.n.u3Available,
3144 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
3145 Pte.n.u1WriteThru ? "pwt" : " ",
3146 Pte.n.u1CacheDisable ? "pcd" : " ",
3147 Pte.n.u1PAT ? "pat" : " ",
3148 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
3149 );
3150 if (Pte.u & UINT64_C(0x7fff000000000000))
3151 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
3152 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3153 if (RT_FAILURE(rc))
3154 return rc;
3155
3156 /*
3157 * Advance.
3158 */
3159 VarPTEAddr.u.u64Number += cbEntry;
3160 if (iEntry != ~0U)
3161 VarGCPtr.u.GCFlat += PAGE_SIZE;
3162 } while (cEntries-- > 0);
3163
3164 return VINF_SUCCESS;
3165}
3166
3167
3168/**
3169 * @callback_method_impl{FNDBGCCMD, The 'dptb' command.}
3170 */
3171static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3172{
3173 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3174 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
3175 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
3176 if (RT_FAILURE(rc1))
3177 return rc1;
3178 NOREF(pCmd); NOREF(cArgs);
3179 return rc2;
3180}
3181
3182
3183/**
3184 * @callback_method_impl{FNDBGCCMD, The 'dt' command.}
3185 */
3186static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3187{
3188 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3189 int rc;
3190
3191 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3192 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3193 if (cArgs == 1)
3194 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType != DBGCVAR_TYPE_STRING
3195 && paArgs[0].enmType != DBGCVAR_TYPE_SYMBOL);
3196
3197 /*
3198 * Check if the command indicates the type.
3199 */
3200 enum { kTss16, kTss32, kTss64, kTssToBeDetermined } enmTssType = kTssToBeDetermined;
3201 if (!strcmp(pCmd->pszCmd, "dt16"))
3202 enmTssType = kTss16;
3203 else if (!strcmp(pCmd->pszCmd, "dt32"))
3204 enmTssType = kTss32;
3205 else if (!strcmp(pCmd->pszCmd, "dt64"))
3206 enmTssType = kTss64;
3207
3208 /*
3209 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
3210 */
3211 uint32_t SelTss = UINT32_MAX;
3212 DBGCVAR VarTssAddr;
3213 if (cArgs == 0)
3214 {
3215 /** @todo consider querying the hidden bits instead (missing API). */
3216 uint16_t SelTR;
3217 rc = DBGFR3RegCpuQueryU16(pUVM, pDbgc->idCpu, DBGFREG_TR, &SelTR);
3218 if (RT_FAILURE(rc))
3219 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to query TR, rc=%Rrc\n", rc);
3220 DBGCVAR_INIT_GC_FAR(&VarTssAddr, SelTR, 0);
3221 SelTss = SelTR;
3222 }
3223 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3224 {
3225 if (paArgs[0].u.u64Number < 0xffff)
3226 DBGCVAR_INIT_GC_FAR(&VarTssAddr, (RTSEL)paArgs[0].u.u64Number, 0);
3227 else
3228 {
3229 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
3230 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Element count doesn't combine with a TSS address.\n");
3231 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, paArgs[0].u.u64Number);
3232 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_BYTES)
3233 {
3234 VarTssAddr.enmRangeType = paArgs[0].enmRangeType;
3235 VarTssAddr.u64Range = paArgs[0].u64Range;
3236 }
3237 }
3238 }
3239 else
3240 VarTssAddr = paArgs[0];
3241
3242 /*
3243 * Deal with TSS:ign by means of the GDT.
3244 */
3245 if (VarTssAddr.enmType == DBGCVAR_TYPE_GC_FAR)
3246 {
3247 SelTss = VarTssAddr.u.GCFar.sel;
3248 DBGFSELINFO SelInfo;
3249 rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, VarTssAddr.u.GCFar.sel, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
3250 if (RT_FAILURE(rc))
3251 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3SelQueryInfo(,%u,%d,,) -> %Rrc.\n",
3252 pDbgc->idCpu, VarTssAddr.u.GCFar.sel, rc);
3253
3254 if (SelInfo.u.Raw.Gen.u1DescType)
3255 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (!sys)\n", VarTssAddr.u.GCFar.sel);
3256
3257 switch (SelInfo.u.Raw.Gen.u4Type)
3258 {
3259 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
3260 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
3261 if (enmTssType == kTssToBeDetermined)
3262 enmTssType = kTss16;
3263 break;
3264
3265 case X86_SEL_TYPE_SYS_386_TSS_BUSY: /* AMD64 too */
3266 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
3267 if (enmTssType == kTssToBeDetermined)
3268 enmTssType = SelInfo.fFlags & DBGFSELINFO_FLAGS_LONG_MODE ? kTss64 : kTss32;
3269 break;
3270
3271 default:
3272 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (type=%x)\n",
3273 VarTssAddr.u.GCFar.sel, SelInfo.u.Raw.Gen.u4Type);
3274 }
3275
3276 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, SelInfo.GCPtrBase);
3277 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, RT_MAX(SelInfo.cbLimit + 1, SelInfo.cbLimit));
3278 }
3279
3280 /*
3281 * Determine the TSS type if none is currently given.
3282 */
3283 if (enmTssType == kTssToBeDetermined)
3284 {
3285 if ( VarTssAddr.u64Range > 0
3286 && VarTssAddr.u64Range < sizeof(X86TSS32) - 4)
3287 enmTssType = kTss16;
3288 else
3289 {
3290 uint64_t uEfer;
3291 rc = DBGFR3RegCpuQueryU64(pUVM, pDbgc->idCpu, DBGFREG_MSR_K6_EFER, &uEfer);
3292 if ( RT_FAILURE(rc)
3293 || !(uEfer & MSR_K6_EFER_LMA) )
3294 enmTssType = kTss32;
3295 else
3296 enmTssType = kTss64;
3297 }
3298 }
3299
3300 /*
3301 * Figure the min/max sizes.
3302 * ASSUMES max TSS size is 64 KB.
3303 */
3304 uint32_t cbTssMin;
3305 uint32_t cbTssMax;
3306 switch (enmTssType)
3307 {
3308 case kTss16:
3309 cbTssMin = cbTssMax = sizeof(X86TSS16);
3310 break;
3311 case kTss32:
3312 cbTssMin = RT_OFFSETOF(X86TSS32, IntRedirBitmap);
3313 cbTssMax = _64K;
3314 break;
3315 case kTss64:
3316 cbTssMin = RT_OFFSETOF(X86TSS64, IntRedirBitmap);
3317 cbTssMax = _64K;
3318 break;
3319 default:
3320 AssertFailedReturn(VERR_INTERNAL_ERROR);
3321 }
3322 uint32_t cbTss = VarTssAddr.enmRangeType == DBGCVAR_RANGE_BYTES ? (uint32_t)VarTssAddr.u64Range : 0;
3323 if (cbTss == 0)
3324 cbTss = cbTssMin;
3325 else if (cbTss < cbTssMin)
3326 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Minimum TSS size is %u bytes, you specified %llu (%llx) bytes.\n",
3327 cbTssMin, VarTssAddr.u64Range, VarTssAddr.u64Range);
3328 else if (cbTss > cbTssMax)
3329 cbTss = cbTssMax;
3330 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, cbTss);
3331
3332 /*
3333 * Read the TSS into a temporary buffer.
3334 */
3335 uint8_t abBuf[_64K];
3336 size_t cbTssRead;
3337 rc = DBGCCmdHlpMemRead(pCmdHlp, abBuf, cbTss, &VarTssAddr, &cbTssRead);
3338 if (RT_FAILURE(rc))
3339 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read TSS at %Dv: %Rrc\n", &VarTssAddr, rc);
3340 if (cbTssRead < cbTssMin)
3341 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read essential parts of the TSS (read %zu, min %zu).\n",
3342 cbTssRead, cbTssMin);
3343 if (cbTssRead < cbTss)
3344 memset(&abBuf[cbTssRead], 0xff, cbTss - cbTssRead);
3345
3346
3347 /*
3348 * Format the TSS.
3349 */
3350 uint16_t offIoBitmap;
3351 switch (enmTssType)
3352 {
3353 case kTss16:
3354 {
3355 PCX86TSS16 pTss = (PCX86TSS16)&abBuf[0];
3356 if (SelTss != UINT32_MAX)
3357 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS16 at %Dv\n", SelTss, &VarTssAddr);
3358 else
3359 DBGCCmdHlpPrintf(pCmdHlp, "TSS16 at %Dv\n", &VarTssAddr);
3360 DBGCCmdHlpPrintf(pCmdHlp,
3361 "ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x\n"
3362 "ip=%04x sp=%04x bp=%04x\n"
3363 "cs=%04x ss=%04x ds=%04x es=%04x flags=%04x\n"
3364 "ss:sp0=%04x:%04x ss:sp1=%04x:%04x ss:sp2=%04x:%04x\n"
3365 "prev=%04x ldtr=%04x\n"
3366 ,
3367 pTss->ax, pTss->bx, pTss->cx, pTss->dx, pTss->si, pTss->di,
3368 pTss->ip, pTss->sp, pTss->bp,
3369 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->flags,
3370 pTss->ss0, pTss->sp0, pTss->ss1, pTss->sp1, pTss->ss2, pTss->sp2,
3371 pTss->selPrev, pTss->selLdt);
3372 if (pTss->cs != 0)
3373 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%04x L 0", pTss->cs, pTss->ip);
3374 offIoBitmap = 0;
3375 break;
3376 }
3377
3378 case kTss32:
3379 {
3380 PCX86TSS32 pTss = (PCX86TSS32)&abBuf[0];
3381 if (SelTss != UINT32_MAX)
3382 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS32 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
3383 else
3384 DBGCCmdHlpPrintf(pCmdHlp, "TSS32 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
3385 DBGCCmdHlpPrintf(pCmdHlp,
3386 "eax=%08x bx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
3387 "eip=%08x esp=%08x ebp=%08x\n"
3388 "cs=%04x ss=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n"
3389 "ss:esp0=%04x:%08x ss:esp1=%04x:%08x ss:esp2=%04x:%08x\n"
3390 "prev=%04x ldtr=%04x cr3=%08x debug=%u iomap=%04x\n"
3391 ,
3392 pTss->eax, pTss->ebx, pTss->ecx, pTss->edx, pTss->esi, pTss->edi,
3393 pTss->eip, pTss->esp, pTss->ebp,
3394 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->fs, pTss->gs, pTss->eflags,
3395 pTss->ss0, pTss->esp0, pTss->ss1, pTss->esp1, pTss->ss2, pTss->esp2,
3396 pTss->selPrev, pTss->selLdt, pTss->cr3, pTss->fDebugTrap, pTss->offIoBitmap);
3397 if (pTss->cs != 0)
3398 pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pTss->cs, pTss->eip);
3399 offIoBitmap = pTss->offIoBitmap;
3400 break;
3401 }
3402
3403 case kTss64:
3404 {
3405 PCX86TSS64 pTss = (PCX86TSS64)&abBuf[0];
3406 if (SelTss != UINT32_MAX)
3407 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS64 at %Dv (min=%04x)\n", SelTss, &VarTssAddr, cbTssMin);
3408 else
3409 DBGCCmdHlpPrintf(pCmdHlp, "TSS64 at %Dv (min=%04x)\n", &VarTssAddr, cbTssMin);
3410 DBGCCmdHlpPrintf(pCmdHlp,
3411 "rsp0=%016RX16 rsp1=%016RX16 rsp2=%016RX16\n"
3412 "ist1=%016RX16 ist2=%016RX16\n"
3413 "ist3=%016RX16 ist4=%016RX16\n"
3414 "ist5=%016RX16 ist6=%016RX16\n"
3415 "ist7=%016RX16 iomap=%04x\n"
3416 ,
3417 pTss->rsp0, pTss->rsp1, pTss->rsp2,
3418 pTss->ist1, pTss->ist2,
3419 pTss->ist3, pTss->ist4,
3420 pTss->ist5, pTss->ist6,
3421 pTss->ist7, pTss->offIoBitmap);
3422 offIoBitmap = pTss->offIoBitmap;
3423 break;
3424 }
3425
3426 default:
3427 AssertFailedReturn(VERR_INTERNAL_ERROR);
3428 }
3429
3430 /*
3431 * Dump the interrupt redirection bitmap.
3432 */
3433 if (enmTssType != kTss16)
3434 {
3435 if ( offIoBitmap > cbTssMin
3436 && offIoBitmap < cbTss) /** @todo check exactly what the edge cases are here. */
3437 {
3438 if (offIoBitmap - cbTssMin >= 32)
3439 {
3440 DBGCCmdHlpPrintf(pCmdHlp, "Interrupt redirection:\n");
3441 uint8_t const *pbIntRedirBitmap = &abBuf[offIoBitmap - 32];
3442 uint32_t iStart = 0;
3443 bool fPrev = ASMBitTest(pbIntRedirBitmap, 0); /* LE/BE issue */
3444 for (uint32_t i = 0; i < 256; i++)
3445 {
3446 bool fThis = ASMBitTest(pbIntRedirBitmap, i);
3447 if (fThis != fPrev)
3448 {
3449 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, i - 1, fPrev ? "Protected mode" : "Redirected");
3450 fPrev = fThis;
3451 iStart = i;
3452 }
3453 }
3454 if (iStart != 255)
3455 DBGCCmdHlpPrintf(pCmdHlp, "%02x-%02x %s\n", iStart, 255, fPrev ? "Protected mode" : "Redirected");
3456 }
3457 else
3458 DBGCCmdHlpPrintf(pCmdHlp, "Invalid interrupt redirection bitmap size: %u (%#x), expected 32 bytes.\n",
3459 offIoBitmap - cbTssMin, offIoBitmap - cbTssMin);
3460 }
3461 else if (offIoBitmap > 0)
3462 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap (-%#x)\n", cbTssMin - offIoBitmap);
3463 else
3464 DBGCCmdHlpPrintf(pCmdHlp, "No interrupt redirection bitmap\n");
3465 }
3466
3467 /*
3468 * Dump the I/O permission bitmap if present. The IOPM cannot start below offset 0x64
3469 * (that applies to both 32-bit and 64-bit TSSs since their size is the same).
3470 */
3471 if (enmTssType != kTss16)
3472 {
3473 if (offIoBitmap < cbTss && offIoBitmap >= 0x64)
3474 {
3475 uint32_t cPorts = RT_MIN((cbTss - offIoBitmap) * 8, _64K);
3476 DBGCVAR VarAddr;
3477 DBGCCmdHlpEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarTssAddr, offIoBitmap);
3478 DBGCCmdHlpPrintf(pCmdHlp, "I/O bitmap at %DV - %#x ports:\n", &VarAddr, cPorts);
3479
3480 uint8_t const *pbIoBitmap = &abBuf[offIoBitmap];
3481 uint32_t iStart = 0;
3482 bool fPrev = ASMBitTest(pbIoBitmap, 0);
3483 uint32_t cLine = 0;
3484 for (uint32_t i = 1; i < cPorts; i++)
3485 {
3486 bool fThis = ASMBitTest(pbIoBitmap, i);
3487 if (fThis != fPrev)
3488 {
3489 cLine++;
3490 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s%s", iStart, i-1,
3491 fPrev ? "GP" : "OK", (cLine % 6) == 0 ? "\n" : " ");
3492 fPrev = fThis;
3493 iStart = i;
3494 }
3495 }
3496 if (iStart != _64K-1)
3497 DBGCCmdHlpPrintf(pCmdHlp, "%04x-%04x %s\n", iStart, _64K-1, fPrev ? "GP" : "OK");
3498 }
3499 else if (offIoBitmap > 0)
3500 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap (-%#x)\n", cbTssMin - offIoBitmap);
3501 else
3502 DBGCCmdHlpPrintf(pCmdHlp, "No I/O bitmap\n");
3503 }
3504
3505 return VINF_SUCCESS;
3506}
3507
3508
3509/**
3510 * @callback_method_impl{FNDBGCCMD, The 'm' command.}
3511 */
3512static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3513{
3514 DBGCCmdHlpPrintf(pCmdHlp, "Address: %DV\n", &paArgs[0]);
3515 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3516 return dbgcCmdDumpPageHierarchy(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
3517}
3518
3519
3520/**
3521 * Converts one or more variables into a byte buffer for a
3522 * given unit size.
3523 *
3524 * @returns VBox status codes:
3525 * @retval VERR_TOO_MUCH_DATA if the buffer is too small, bitched.
3526 * @retval VERR_INTERNAL_ERROR on bad variable type, bitched.
3527 * @retval VINF_SUCCESS on success.
3528 *
3529 * @param pvBuf The buffer to convert into.
3530 * @param pcbBuf The buffer size on input. The size of the result on output.
3531 * @param cbUnit The unit size to apply when converting.
3532 * The high bit is used to indicate unicode string.
3533 * @param paVars The array of variables to convert.
3534 * @param cVars The number of variables.
3535 */
3536int dbgcVarsToBytes(PDBGCCMDHLP pCmdHlp, void *pvBuf, uint32_t *pcbBuf, size_t cbUnit, PCDBGCVAR paVars, unsigned cVars)
3537{
3538 union
3539 {
3540 uint8_t *pu8;
3541 uint16_t *pu16;
3542 uint32_t *pu32;
3543 uint64_t *pu64;
3544 } u, uEnd;
3545 u.pu8 = (uint8_t *)pvBuf;
3546 uEnd.pu8 = u.pu8 + *pcbBuf;
3547
3548 unsigned i;
3549 for (i = 0; i < cVars && u.pu8 < uEnd.pu8; i++)
3550 {
3551 switch (paVars[i].enmType)
3552 {
3553 case DBGCVAR_TYPE_GC_FAR:
3554 case DBGCVAR_TYPE_GC_FLAT:
3555 case DBGCVAR_TYPE_GC_PHYS:
3556 case DBGCVAR_TYPE_HC_FLAT:
3557 case DBGCVAR_TYPE_HC_PHYS:
3558 case DBGCVAR_TYPE_NUMBER:
3559 {
3560 uint64_t u64 = paVars[i].u.u64Number;
3561 switch (cbUnit & 0x1f)
3562 {
3563 case 1:
3564 do
3565 {
3566 *u.pu8++ = u64;
3567 u64 >>= 8;
3568 } while (u64);
3569 break;
3570 case 2:
3571 do
3572 {
3573 *u.pu16++ = u64;
3574 u64 >>= 16;
3575 } while (u64);
3576 break;
3577 case 4:
3578 *u.pu32++ = u64;
3579 u64 >>= 32;
3580 if (u64)
3581 *u.pu32++ = u64;
3582 break;
3583 case 8:
3584 *u.pu64++ = u64;
3585 break;
3586 }
3587 break;
3588 }
3589
3590 case DBGCVAR_TYPE_STRING:
3591 case DBGCVAR_TYPE_SYMBOL:
3592 {
3593 const char *psz = paVars[i].u.pszString;
3594 size_t cbString = strlen(psz);
3595 if (cbUnit & RT_BIT_32(31))
3596 {
3597 /* Explode char to unit. */
3598 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8) * (cbUnit & 0x1f))
3599 {
3600 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3601 return VERR_TOO_MUCH_DATA;
3602 }
3603 while (*psz)
3604 {
3605 switch (cbUnit & 0x1f)
3606 {
3607 case 1: *u.pu8++ = *psz; break;
3608 case 2: *u.pu16++ = *psz; break;
3609 case 4: *u.pu32++ = *psz; break;
3610 case 8: *u.pu64++ = *psz; break;
3611 }
3612 psz++;
3613 }
3614 }
3615 else
3616 {
3617 /* Raw copy with zero padding if the size isn't aligned. */
3618 if (cbString > (uintptr_t)(uEnd.pu8 - u.pu8))
3619 {
3620 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3621 return VERR_TOO_MUCH_DATA;
3622 }
3623
3624 size_t cbCopy = cbString & ~(cbUnit - 1);
3625 memcpy(u.pu8, psz, cbCopy);
3626 u.pu8 += cbCopy;
3627 psz += cbCopy;
3628
3629 size_t cbReminder = cbString & (cbUnit - 1);
3630 if (cbReminder)
3631 {
3632 memcpy(u.pu8, psz, cbString & (cbUnit - 1));
3633 memset(u.pu8 + cbReminder, 0, cbUnit - cbReminder);
3634 u.pu8 += cbUnit;
3635 }
3636 }
3637 break;
3638 }
3639
3640 default:
3641 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3642 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INTERNAL_ERROR,
3643 "i=%d enmType=%d\n", i, paVars[i].enmType);
3644 return VERR_INTERNAL_ERROR;
3645 }
3646 }
3647 *pcbBuf = u.pu8 - (uint8_t *)pvBuf;
3648 if (i != cVars)
3649 {
3650 pCmdHlp->pfnVBoxError(pCmdHlp, VERR_TOO_MUCH_DATA, "Max %d bytes.\n", uEnd.pu8 - (uint8_t *)pvBuf);
3651 return VERR_TOO_MUCH_DATA;
3652 }
3653 return VINF_SUCCESS;
3654}
3655
3656
3657/**
3658 * @callback_method_impl{FNDBGCCMD, The 'eb'\, 'ew'\, 'ed' and 'eq' commands.}
3659 */
3660static DECLCALLBACK(int) dbgcCmdEditMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3661{
3662 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3663 unsigned iArg;
3664
3665 /*
3666 * Validate input.
3667 */
3668 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs >= 2);
3669 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3670 for (iArg = 1; iArg < cArgs; iArg++)
3671 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER);
3672 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3673
3674 /*
3675 * Figure out the element size.
3676 */
3677 unsigned cbElement;
3678 switch (pCmd->pszCmd[1])
3679 {
3680 default:
3681 case 'b': cbElement = 1; break;
3682 case 'w': cbElement = 2; break;
3683 case 'd': cbElement = 4; break;
3684 case 'q': cbElement = 8; break;
3685 }
3686
3687 /*
3688 * Do setting.
3689 */
3690 DBGCVAR Addr = paArgs[0];
3691 for (iArg = 1;;)
3692 {
3693 size_t cbWritten;
3694 int rc = pCmdHlp->pfnMemWrite(pCmdHlp, &paArgs[iArg].u, cbElement, &Addr, &cbWritten);
3695 if (RT_FAILURE(rc))
3696 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Writing memory at %DV.\n", &Addr);
3697 if (cbWritten != cbElement)
3698 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Only wrote %u out of %u bytes!\n", cbWritten, cbElement);
3699
3700 /* advance. */
3701 iArg++;
3702 if (iArg >= cArgs)
3703 break;
3704 rc = DBGCCmdHlpEval(pCmdHlp, &Addr, "%Dv + %#x", &Addr, cbElement);
3705 if (RT_FAILURE(rc))
3706 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3707 }
3708
3709 return VINF_SUCCESS;
3710}
3711
3712
3713/**
3714 * Executes the search.
3715 *
3716 * @returns VBox status code.
3717 * @param pCmdHlp The command helpers.
3718 * @param pUVM The user mode VM handle.
3719 * @param pAddress The address to start searching from. (undefined on output)
3720 * @param cbRange The address range to search. Must not wrap.
3721 * @param pabBytes The byte pattern to search for.
3722 * @param cbBytes The size of the pattern.
3723 * @param cbUnit The search unit.
3724 * @param cMaxHits The max number of hits.
3725 * @param pResult Where to store the result if it's a function invocation.
3726 */
3727static int dbgcCmdWorkerSearchMemDoIt(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PDBGFADDRESS pAddress, RTGCUINTPTR cbRange,
3728 const uint8_t *pabBytes, uint32_t cbBytes,
3729 uint32_t cbUnit, uint64_t cMaxHits, PDBGCVAR pResult)
3730{
3731 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3732
3733 /*
3734 * Do the search.
3735 */
3736 uint64_t cHits = 0;
3737 for (;;)
3738 {
3739 /* search */
3740 DBGFADDRESS HitAddress;
3741 int rc = DBGFR3MemScan(pUVM, pDbgc->idCpu, pAddress, cbRange, 1, pabBytes, cbBytes, &HitAddress);
3742 if (RT_FAILURE(rc))
3743 {
3744 if (rc != VERR_DBGF_MEM_NOT_FOUND)
3745 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3MemScan\n");
3746
3747 /* update the current address so we can save it (later). */
3748 pAddress->off += cbRange;
3749 pAddress->FlatPtr += cbRange;
3750 cbRange = 0;
3751 break;
3752 }
3753
3754 /* report result */
3755 DBGCVAR VarCur;
3756 rc = DBGCCmdHlpVarFromDbgfAddr(pCmdHlp, &HitAddress, &VarCur);
3757 if (RT_FAILURE(rc))
3758 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGCCmdHlpVarFromDbgfAddr\n");
3759 if (!pResult)
3760 pCmdHlp->pfnExec(pCmdHlp, "db %DV LB 10", &VarCur);
3761 else
3762 DBGCVAR_ASSIGN(pResult, &VarCur);
3763
3764 /* advance */
3765 cbRange -= HitAddress.FlatPtr - pAddress->FlatPtr;
3766 *pAddress = HitAddress;
3767 pAddress->FlatPtr += cbBytes;
3768 pAddress->off += cbBytes;
3769 if (cbRange <= cbBytes)
3770 {
3771 cbRange = 0;
3772 break;
3773 }
3774 cbRange -= cbBytes;
3775
3776 if (++cHits >= cMaxHits)
3777 {
3778 /// @todo save the search.
3779 break;
3780 }
3781 }
3782
3783 /*
3784 * Save the search so we can resume it...
3785 */
3786 if (pDbgc->abSearch != pabBytes)
3787 {
3788 memcpy(pDbgc->abSearch, pabBytes, cbBytes);
3789 pDbgc->cbSearch = cbBytes;
3790 pDbgc->cbSearchUnit = cbUnit;
3791 }
3792 pDbgc->cMaxSearchHits = cMaxHits;
3793 pDbgc->SearchAddr = *pAddress;
3794 pDbgc->cbSearchRange = cbRange;
3795
3796 return cHits ? VINF_SUCCESS : VERR_DBGC_COMMAND_FAILED;
3797}
3798
3799
3800/**
3801 * Resumes the previous search.
3802 *
3803 * @returns VBox status code.
3804 * @param pCmdHlp Pointer to the command helper functions.
3805 * @param pUVM The user mode VM handle.
3806 * @param pResult Where to store the result of a function invocation.
3807 */
3808static int dbgcCmdWorkerSearchMemResume(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PDBGCVAR pResult)
3809{
3810 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3811
3812 /*
3813 * Make sure there is a previous command.
3814 */
3815 if (!pDbgc->cbSearch)
3816 {
3817 DBGCCmdHlpPrintf(pCmdHlp, "Error: No previous search\n");
3818 return VERR_DBGC_COMMAND_FAILED;
3819 }
3820
3821 /*
3822 * Make range and address adjustments.
3823 */
3824 DBGFADDRESS Address = pDbgc->SearchAddr;
3825 if (Address.FlatPtr == ~(RTGCUINTPTR)0)
3826 {
3827 Address.FlatPtr -= Address.off;
3828 Address.off = 0;
3829 }
3830
3831 RTGCUINTPTR cbRange = pDbgc->cbSearchRange;
3832 if (!cbRange)
3833 cbRange = ~(RTGCUINTPTR)0;
3834 if (Address.FlatPtr + cbRange < pDbgc->SearchAddr.FlatPtr)
3835 cbRange = ~(RTGCUINTPTR)0 - pDbgc->SearchAddr.FlatPtr + !!pDbgc->SearchAddr.FlatPtr;
3836
3837 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pUVM, &Address, cbRange, pDbgc->abSearch, pDbgc->cbSearch,
3838 pDbgc->cbSearchUnit, pDbgc->cMaxSearchHits, pResult);
3839}
3840
3841
3842/**
3843 * Search memory, worker for the 's' and 's?' functions.
3844 *
3845 * @returns VBox status code.
3846 * @param pCmdHlp Pointer to the command helper functions.
3847 * @param pUVM The user mode VM handle.
3848 * @param pAddress Where to start searching. If no range, search till end of address space.
3849 * @param cMaxHits The maximum number of hits.
3850 * @param chType The search type.
3851 * @param paPatArgs The pattern variable array.
3852 * @param cPatArgs Number of pattern variables.
3853 * @param pResult Where to store the result of a function invocation.
3854 */
3855static int dbgcCmdWorkerSearchMem(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pAddress, uint64_t cMaxHits, char chType,
3856 PCDBGCVAR paPatArgs, unsigned cPatArgs, PDBGCVAR pResult)
3857{
3858 if (pResult)
3859 DBGCVAR_INIT_GC_FLAT(pResult, 0);
3860
3861 /*
3862 * Convert the search pattern into bytes and DBGFR3MemScan can deal with.
3863 */
3864 uint32_t cbUnit;
3865 switch (chType)
3866 {
3867 case 'a':
3868 case 'b': cbUnit = 1; break;
3869 case 'u': cbUnit = 2 | RT_BIT_32(31); break;
3870 case 'w': cbUnit = 2; break;
3871 case 'd': cbUnit = 4; break;
3872 case 'q': cbUnit = 8; break;
3873 default:
3874 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "chType=%c\n", chType);
3875 }
3876 uint8_t abBytes[RT_SIZEOFMEMB(DBGC, abSearch)];
3877 uint32_t cbBytes = sizeof(abBytes);
3878 int rc = dbgcVarsToBytes(pCmdHlp, abBytes, &cbBytes, cbUnit, paPatArgs, cPatArgs);
3879 if (RT_FAILURE(rc))
3880 return VERR_DBGC_COMMAND_FAILED;
3881
3882 /*
3883 * Make DBGF address and fix the range.
3884 */
3885 DBGFADDRESS Address;
3886 rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, pAddress, &Address);
3887 if (RT_FAILURE(rc))
3888 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "VarToDbgfAddr(,%Dv,)\n", pAddress);
3889
3890 RTGCUINTPTR cbRange;
3891 switch (pAddress->enmRangeType)
3892 {
3893 case DBGCVAR_RANGE_BYTES:
3894 cbRange = pAddress->u64Range;
3895 if (cbRange != pAddress->u64Range)
3896 cbRange = ~(RTGCUINTPTR)0;
3897 break;
3898
3899 case DBGCVAR_RANGE_ELEMENTS:
3900 cbRange = (RTGCUINTPTR)(pAddress->u64Range * cbUnit);
3901 if ( cbRange != pAddress->u64Range * cbUnit
3902 || cbRange < pAddress->u64Range)
3903 cbRange = ~(RTGCUINTPTR)0;
3904 break;
3905
3906 default:
3907 cbRange = ~(RTGCUINTPTR)0;
3908 break;
3909 }
3910 if (Address.FlatPtr + cbRange < Address.FlatPtr)
3911 cbRange = ~(RTGCUINTPTR)0 - Address.FlatPtr + !!Address.FlatPtr;
3912
3913 /*
3914 * Ok, do it.
3915 */
3916 return dbgcCmdWorkerSearchMemDoIt(pCmdHlp, pUVM, &Address, cbRange, abBytes, cbBytes, cbUnit, cMaxHits, pResult);
3917}
3918
3919
3920/**
3921 * @callback_method_impl{FNDBGCCMD, The 's' command.}
3922 */
3923static DECLCALLBACK(int) dbgcCmdSearchMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3924{
3925 /* check that the parser did what it's supposed to do. */
3926 //if ( cArgs <= 2
3927 // && paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3928 // return DBGCCmdHlpPrintf(pCmdHlp, "parser error\n");
3929
3930 /*
3931 * Repeat previous search?
3932 */
3933 if (cArgs == 0)
3934 return dbgcCmdWorkerSearchMemResume(pCmdHlp, pUVM, NULL);
3935
3936 /*
3937 * Parse arguments.
3938 */
3939
3940 return -1;
3941}
3942
3943
3944/**
3945 * @callback_method_impl{FNDBGCCMD, The 's?' command.}
3946 */
3947static DECLCALLBACK(int) dbgcCmdSearchMemType(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3948{
3949 /* check that the parser did what it's supposed to do. */
3950 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs >= 2 && DBGCVAR_ISGCPOINTER(paArgs[0].enmType));
3951 return dbgcCmdWorkerSearchMem(pCmdHlp, pUVM, &paArgs[0], 25, pCmd->pszCmd[1], paArgs + 1, cArgs - 1, NULL);
3952}
3953
3954
3955/**
3956 * @callback_method_impl{FNDBGCCMD, The 'sx[eni-]' commands.}
3957 */
3958static DECLCALLBACK(int) dbgcCmdEventCtrl(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3959{
3960 return VERR_NOT_IMPLEMENTED;
3961}
3962
3963
3964/**
3965 * @callback_method_impl{FNDBGCCMD, The 'sx' commands.}
3966 */
3967static DECLCALLBACK(int) dbgcCmdEventCtrlList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3968{
3969 return VERR_NOT_IMPLEMENTED;
3970}
3971
3972
3973/**
3974 * @callback_method_impl{FNDBGCCMD, The 'sxr' commands.}
3975 */
3976static DECLCALLBACK(int) dbgcCmdEventCtrlReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3977{
3978 return VERR_NOT_IMPLEMENTED;
3979}
3980
3981
3982
3983/**
3984 * List near symbol.
3985 *
3986 * @returns VBox status code.
3987 * @param pCmdHlp Pointer to command helper functions.
3988 * @param pUVM The user mode VM handle.
3989 * @param pArg Pointer to the address or symbol to lookup.
3990 */
3991static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pArg)
3992{
3993 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3994
3995 RTDBGSYMBOL Symbol;
3996 int rc;
3997 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
3998 {
3999 /*
4000 * Lookup the symbol address.
4001 */
4002 rc = DBGFR3AsSymbolByName(pUVM, pDbgc->hDbgAs, pArg->u.pszString, &Symbol, NULL);
4003 if (RT_FAILURE(rc))
4004 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3AsSymbolByName(,,%s,)\n", pArg->u.pszString);
4005
4006 rc = DBGCCmdHlpPrintf(pCmdHlp, "%RTptr %s\n", Symbol.Value, Symbol.szName);
4007 }
4008 else
4009 {
4010 /*
4011 * Convert it to a flat GC address and lookup that address.
4012 */
4013 DBGCVAR AddrVar;
4014 rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
4015 if (RT_FAILURE(rc))
4016 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
4017
4018 RTINTPTR offDisp;
4019 DBGFADDRESS Addr;
4020 rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, AddrVar.u.GCFlat),
4021 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDisp, &Symbol, NULL);
4022 if (RT_FAILURE(rc))
4023 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3ASymbolByAddr(,,%RGv,,)\n", AddrVar.u.GCFlat);
4024
4025 if (!offDisp)
4026 rc = DBGCCmdHlpPrintf(pCmdHlp, "%DV %s", &AddrVar, Symbol.szName);
4027 else if (offDisp > 0)
4028 rc = DBGCCmdHlpPrintf(pCmdHlp, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
4029 else
4030 rc = DBGCCmdHlpPrintf(pCmdHlp, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
4031 if (Symbol.cb > 0)
4032 rc = DBGCCmdHlpPrintf(pCmdHlp, " (LB %RGv)\n", Symbol.cb);
4033 else
4034 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
4035 }
4036
4037 return rc;
4038}
4039
4040
4041/**
4042 * @callback_method_impl{FNDBGCCMD, The 'ln' (listnear) command.}
4043 */
4044static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4045{
4046 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4047 if (!cArgs)
4048 {
4049 /*
4050 * Current cs:eip symbol.
4051 */
4052 DBGCVAR AddrVar;
4053 const char *pszFmtExpr = pDbgc->fRegCtxGuest ? "%%(cs:eip)" : "%%(.cs:.eip)";
4054 int rc = DBGCCmdHlpEval(pCmdHlp, &AddrVar, pszFmtExpr);
4055 if (RT_FAILURE(rc))
4056 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%s\n", pszFmtExpr + 1);
4057 return dbgcDoListNear(pCmdHlp, pUVM, &AddrVar);
4058 }
4059
4060/** @todo Fix the darn parser, it's resolving symbols specified as arguments before we get in here. */
4061 /*
4062 * Iterate arguments.
4063 */
4064 for (unsigned iArg = 0; iArg < cArgs; iArg++)
4065 {
4066 int rc = dbgcDoListNear(pCmdHlp, pUVM, &paArgs[iArg]);
4067 if (RT_FAILURE(rc))
4068 return rc;
4069 }
4070
4071 NOREF(pCmd);
4072 return VINF_SUCCESS;
4073}
4074
4075
4076/**
4077 * Matches the module patters against a module name.
4078 *
4079 * @returns true if matching, otherwise false.
4080 * @param pszName The module name.
4081 * @param paArgs The module pattern argument list.
4082 * @param cArgs Number of arguments.
4083 */
4084static bool dbgcCmdListModuleMatch(const char *pszName, PCDBGCVAR paArgs, unsigned cArgs)
4085{
4086 for (uint32_t i = 0; i < cArgs; i++)
4087 if (RTStrSimplePatternMatch(paArgs[i].u.pszString, pszName))
4088 return true;
4089 return false;
4090}
4091
4092
4093/**
4094 * @callback_method_impl{FNDBGCCMD, The 'ln' (list near) command.}
4095 */
4096static DECLCALLBACK(int) dbgcCmdListModules(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4097{
4098 bool const fMappings = pCmd->pszCmd[2] == 'o';
4099 bool const fVerbose = pCmd->pszCmd[strlen(pCmd->pszCmd) - 1] == 'v';
4100 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4101
4102 /*
4103 * Iterate the modules in the current address space and print info about
4104 * those matching the input.
4105 */
4106 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, pDbgc->hDbgAs);
4107 uint32_t cMods = RTDbgAsModuleCount(hAs);
4108 for (uint32_t iMod = 0; iMod < cMods; iMod++)
4109 {
4110 RTDBGMOD hMod = RTDbgAsModuleByIndex(hAs, iMod);
4111 if (hMod != NIL_RTDBGMOD)
4112 {
4113 bool const fDeferred = RTDbgModIsDeferred(hMod);
4114 bool const fExports = RTDbgModIsExports(hMod);
4115 uint32_t const cSegs = fDeferred ? 1 : RTDbgModSegmentCount(hMod);
4116 const char * const pszName = RTDbgModName(hMod);
4117 const char * const pszImgFile = RTDbgModImageFile(hMod);
4118 const char * const pszImgFileUsed = RTDbgModImageFileUsed(hMod);
4119 const char * const pszDbgFile = RTDbgModDebugFile(hMod);
4120 if ( cArgs == 0
4121 || dbgcCmdListModuleMatch(pszName, paArgs, cArgs))
4122 {
4123 /*
4124 * Find the mapping with the lower address, preferring a full
4125 * image mapping, for the main line.
4126 */
4127 RTDBGASMAPINFO aMappings[128];
4128 uint32_t cMappings = RT_ELEMENTS(aMappings);
4129 int rc = RTDbgAsModuleQueryMapByIndex(hAs, iMod, &aMappings[0], &cMappings, 0 /*fFlags*/);
4130 if (RT_SUCCESS(rc))
4131 {
4132 bool fFull = false;
4133 RTUINTPTR uMin = RTUINTPTR_MAX;
4134 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
4135 if ( aMappings[iMap].Address < uMin
4136 && ( !fFull
4137 || aMappings[iMap].iSeg == NIL_RTDBGSEGIDX))
4138 uMin = aMappings[iMap].Address;
4139 if (!fVerbose || !pszImgFile)
4140 DBGCCmdHlpPrintf(pCmdHlp, "%RGv %04x %s%s\n", (RTGCUINTPTR)uMin, cSegs, pszName,
4141 fExports ? " (exports)" : fDeferred ? " (deferred)" : "");
4142 else
4143 DBGCCmdHlpPrintf(pCmdHlp, "%RGv %04x %-12s %s%s\n", (RTGCUINTPTR)uMin, cSegs, pszName, pszImgFile,
4144 fExports ? " (exports)" : fDeferred ? " (deferred)" : "");
4145 if (fVerbose && pszImgFileUsed)
4146 DBGCCmdHlpPrintf(pCmdHlp, " Local image: %s\n", pszImgFileUsed);
4147 if (fVerbose && pszDbgFile)
4148 DBGCCmdHlpPrintf(pCmdHlp, " Debug file: %s\n", pszDbgFile);
4149
4150 if (fMappings)
4151 {
4152 /* sort by address first - not very efficient. */
4153 for (uint32_t i = 0; i + 1 < cMappings; i++)
4154 for (uint32_t j = i + 1; j < cMappings; j++)
4155 if (aMappings[j].Address < aMappings[i].Address)
4156 {
4157 RTDBGASMAPINFO Tmp = aMappings[j];
4158 aMappings[j] = aMappings[i];
4159 aMappings[i] = Tmp;
4160 }
4161
4162 /* print */
4163 if ( cMappings == 1
4164 && aMappings[0].iSeg == NIL_RTDBGSEGIDX
4165 && !fDeferred)
4166 {
4167 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4168 {
4169 RTDBGSEGMENT SegInfo;
4170 rc = RTDbgModSegmentByIndex(hMod, iSeg, &SegInfo);
4171 if (RT_SUCCESS(rc))
4172 {
4173 if (SegInfo.uRva != RTUINTPTR_MAX)
4174 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv #%02x %s\n",
4175 (RTGCUINTPTR)(aMappings[0].Address + SegInfo.uRva),
4176 (RTGCUINTPTR)SegInfo.cb, iSeg, SegInfo.szName);
4177 else
4178 DBGCCmdHlpPrintf(pCmdHlp, " %*s %RGv #%02x %s\n",
4179 sizeof(RTGCUINTPTR)*2, "noload",
4180 (RTGCUINTPTR)SegInfo.cb, iSeg, SegInfo.szName);
4181 }
4182 else
4183 DBGCCmdHlpPrintf(pCmdHlp, " Error query segment #%u: %Rrc\n", iSeg, rc);
4184 }
4185 }
4186 else
4187 {
4188 for (uint32_t iMap = 0; iMap < cMappings; iMap++)
4189 if (aMappings[iMap].iSeg == NIL_RTDBGSEGIDX)
4190 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv <everything>\n",
4191 (RTGCUINTPTR)aMappings[iMap].Address,
4192 (RTGCUINTPTR)RTDbgModImageSize(hMod));
4193 else if (!fDeferred)
4194 {
4195 RTDBGSEGMENT SegInfo;
4196 rc = RTDbgModSegmentByIndex(hMod, aMappings[iMap].iSeg, &SegInfo);
4197 if (RT_FAILURE(rc))
4198 {
4199 RT_ZERO(SegInfo);
4200 strcpy(SegInfo.szName, "error");
4201 }
4202 DBGCCmdHlpPrintf(pCmdHlp, " %RGv %RGv #%02x %s\n",
4203 (RTGCUINTPTR)aMappings[iMap].Address,
4204 (RTGCUINTPTR)SegInfo.cb,
4205 aMappings[iMap].iSeg, SegInfo.szName);
4206 }
4207 else
4208 DBGCCmdHlpPrintf(pCmdHlp, " %RGv #%02x\n",
4209 (RTGCUINTPTR)aMappings[iMap].Address, aMappings[iMap].iSeg);
4210 }
4211 }
4212 }
4213 else
4214 DBGCCmdHlpPrintf(pCmdHlp, "%.*s %04x %s (rc=%Rrc)\n",
4215 sizeof(RTGCPTR) * 2, "???????????", cSegs, pszName, rc);
4216 /** @todo missing address space API for enumerating the mappings. */
4217 }
4218 RTDbgModRelease(hMod);
4219 }
4220 }
4221 RTDbgAsRelease(hAs);
4222
4223 NOREF(pCmd);
4224 return VINF_SUCCESS;
4225}
4226
4227
4228
4229/**
4230 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 8-bit value.}
4231 */
4232static DECLCALLBACK(int) dbgcFuncReadU8(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4233 PDBGCVAR pResult)
4234{
4235 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4236 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
4237 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
4238
4239 uint8_t b;
4240 int rc = DBGCCmdHlpMemRead(pCmdHlp, &b, sizeof(b), &paArgs[0], NULL);
4241 if (RT_FAILURE(rc))
4242 return rc;
4243 DBGCVAR_INIT_NUMBER(pResult, b);
4244
4245 NOREF(pFunc);
4246 return VINF_SUCCESS;
4247}
4248
4249
4250/**
4251 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 16-bit value.}
4252 */
4253static DECLCALLBACK(int) dbgcFuncReadU16(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4254 PDBGCVAR pResult)
4255{
4256 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4257 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
4258 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
4259
4260 uint16_t u16;
4261 int rc = DBGCCmdHlpMemRead(pCmdHlp, &u16, sizeof(u16), &paArgs[0], NULL);
4262 if (RT_FAILURE(rc))
4263 return rc;
4264 DBGCVAR_INIT_NUMBER(pResult, u16);
4265
4266 NOREF(pFunc);
4267 return VINF_SUCCESS;
4268}
4269
4270
4271/**
4272 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 32-bit value.}
4273 */
4274static DECLCALLBACK(int) dbgcFuncReadU32(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4275 PDBGCVAR pResult)
4276{
4277 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4278 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
4279 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
4280
4281 uint32_t u32;
4282 int rc = DBGCCmdHlpMemRead(pCmdHlp, &u32, sizeof(u32), &paArgs[0], NULL);
4283 if (RT_FAILURE(rc))
4284 return rc;
4285 DBGCVAR_INIT_NUMBER(pResult, u32);
4286
4287 NOREF(pFunc);
4288 return VINF_SUCCESS;
4289}
4290
4291
4292/**
4293 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned 64-bit value.}
4294 */
4295static DECLCALLBACK(int) dbgcFuncReadU64(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4296 PDBGCVAR pResult)
4297{
4298 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4299 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
4300 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
4301
4302 uint64_t u64;
4303 int rc = DBGCCmdHlpMemRead(pCmdHlp, &u64, sizeof(u64), &paArgs[0], NULL);
4304 if (RT_FAILURE(rc))
4305 return rc;
4306 DBGCVAR_INIT_NUMBER(pResult, u64);
4307
4308 NOREF(pFunc);
4309 return VINF_SUCCESS;
4310}
4311
4312
4313/**
4314 * @callback_method_impl{FNDBGCFUNC, Reads a unsigned pointer-sized value.}
4315 */
4316static DECLCALLBACK(int) dbgcFuncReadPtr(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4317 PDBGCVAR pResult)
4318{
4319 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4320 AssertReturn(DBGCVAR_ISPOINTER(paArgs[0].enmType), VERR_DBGC_PARSE_BUG);
4321 AssertReturn(paArgs[0].enmRangeType == DBGCVAR_RANGE_NONE, VERR_DBGC_PARSE_BUG);
4322
4323 CPUMMODE enmMode = DBGCCmdHlpGetCpuMode(pCmdHlp);
4324 if (enmMode == CPUMMODE_LONG)
4325 return dbgcFuncReadU64(pFunc, pCmdHlp, pUVM, paArgs, cArgs, pResult);
4326 return dbgcFuncReadU32(pFunc, pCmdHlp, pUVM, paArgs, cArgs, pResult);
4327}
4328
4329
4330/**
4331 * @callback_method_impl{FNDBGCFUNC, The hi(value) function implementation.}
4332 */
4333static DECLCALLBACK(int) dbgcFuncHi(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4334 PDBGCVAR pResult)
4335{
4336 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4337
4338 uint16_t uHi;
4339 switch (paArgs[0].enmType)
4340 {
4341 case DBGCVAR_TYPE_GC_FLAT: uHi = (uint16_t)(paArgs[0].u.GCFlat >> 16); break;
4342 case DBGCVAR_TYPE_GC_FAR: uHi = (uint16_t)paArgs[0].u.GCFar.sel; break;
4343 case DBGCVAR_TYPE_GC_PHYS: uHi = (uint16_t)(paArgs[0].u.GCPhys >> 16); break;
4344 case DBGCVAR_TYPE_HC_FLAT: uHi = (uint16_t)((uintptr_t)paArgs[0].u.pvHCFlat >> 16); break;
4345 case DBGCVAR_TYPE_HC_PHYS: uHi = (uint16_t)(paArgs[0].u.HCPhys >> 16); break;
4346 case DBGCVAR_TYPE_NUMBER: uHi = (uint16_t)(paArgs[0].u.u64Number >> 16); break;
4347 default:
4348 AssertFailedReturn(VERR_DBGC_PARSE_BUG);
4349 }
4350 DBGCVAR_INIT_NUMBER(pResult, uHi);
4351 DBGCVAR_SET_RANGE(pResult, paArgs[0].enmRangeType, paArgs[0].u64Range);
4352
4353 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pUVM);
4354 return VINF_SUCCESS;
4355}
4356
4357
4358/**
4359 * @callback_method_impl{FNDBGCFUNC, The low(value) function implementation.}
4360 */
4361static DECLCALLBACK(int) dbgcFuncLow(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4362 PDBGCVAR pResult)
4363{
4364 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4365
4366 uint16_t uLow;
4367 switch (paArgs[0].enmType)
4368 {
4369 case DBGCVAR_TYPE_GC_FLAT: uLow = (uint16_t)paArgs[0].u.GCFlat; break;
4370 case DBGCVAR_TYPE_GC_FAR: uLow = (uint16_t)paArgs[0].u.GCFar.off; break;
4371 case DBGCVAR_TYPE_GC_PHYS: uLow = (uint16_t)paArgs[0].u.GCPhys; break;
4372 case DBGCVAR_TYPE_HC_FLAT: uLow = (uint16_t)(uintptr_t)paArgs[0].u.pvHCFlat; break;
4373 case DBGCVAR_TYPE_HC_PHYS: uLow = (uint16_t)paArgs[0].u.HCPhys; break;
4374 case DBGCVAR_TYPE_NUMBER: uLow = (uint16_t)paArgs[0].u.u64Number; break;
4375 default:
4376 AssertFailedReturn(VERR_DBGC_PARSE_BUG);
4377 }
4378 DBGCVAR_INIT_NUMBER(pResult, uLow);
4379 DBGCVAR_SET_RANGE(pResult, paArgs[0].enmRangeType, paArgs[0].u64Range);
4380
4381 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pUVM);
4382 return VINF_SUCCESS;
4383}
4384
4385
4386/**
4387 * @callback_method_impl{FNDBGCFUNC,The low(value) function implementation.}
4388 */
4389static DECLCALLBACK(int) dbgcFuncNot(PCDBGCFUNC pFunc, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, uint32_t cArgs,
4390 PDBGCVAR pResult)
4391{
4392 AssertReturn(cArgs == 1, VERR_DBGC_PARSE_BUG);
4393 NOREF(pFunc); NOREF(pCmdHlp); NOREF(pUVM);
4394 return DBGCCmdHlpEval(pCmdHlp, pResult, "!(%Dv)", &paArgs[0]);
4395}
4396
4397
4398/** Generic pointer argument wo/ range. */
4399static const DBGCVARDESC g_aArgPointerWoRange[] =
4400{
4401 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
4402 { 1, 1, DBGCVAR_CAT_POINTER_NO_RANGE, 0, "value", "Address or number." },
4403};
4404
4405/** Generic pointer or number argument. */
4406static const DBGCVARDESC g_aArgPointerNumber[] =
4407{
4408 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
4409 { 1, 1, DBGCVAR_CAT_POINTER_NUMBER, 0, "value", "Address or number." },
4410};
4411
4412
4413
4414/** Function descriptors for the CodeView / WinDbg emulation.
4415 * The emulation isn't attempting to be identical, only somewhat similar.
4416 */
4417const DBGCFUNC g_aFuncsCodeView[] =
4418{
4419 { "by", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU8, "address", "Reads a byte at the given address." },
4420 { "dwo", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU32, "address", "Reads a 32-bit value at the given address." },
4421 { "hi", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncHi, "value", "Returns the high 16-bit bits of a value." },
4422 { "low", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncLow, "value", "Returns the low 16-bit bits of a value." },
4423 { "not", 1, 1, &g_aArgPointerNumber[0], RT_ELEMENTS(g_aArgPointerNumber), 0, dbgcFuncNot, "address", "Boolean NOT." },
4424 { "poi", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadPtr, "address", "Reads a pointer sized (CS) value at the given address." },
4425 { "qwo", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU64, "address", "Reads a 32-bit value at the given address." },
4426 { "wo", 1, 1, &g_aArgPointerWoRange[0], RT_ELEMENTS(g_aArgPointerWoRange), 0, dbgcFuncReadU16, "address", "Reads a 16-bit value at the given address." },
4427};
4428
4429/** The number of functions in the CodeView/WinDbg emulation. */
4430const uint32_t g_cFuncsCodeView = RT_ELEMENTS(g_aFuncsCodeView);
4431
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