VirtualBox

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

Last change on this file since 56874 was 56296, checked in by vboxsync, 10 years ago

Debugger: Updated (C) year.

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