VirtualBox

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

Last change on this file since 46165 was 46165, checked in by vboxsync, 12 years ago

Made dSYM-bundle loading work as well as line numbers in the stack traces (when possible).

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