VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFDisas.cpp@ 42062

Last change on this file since 42062 was 41965, checked in by vboxsync, 13 years ago

VMM: ran scm. Mostly svn:keywords changes (adding Revision).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 25.2 KB
Line 
1/* $Id: DBGFDisas.cpp 41965 2012-06-29 02:52:49Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Disassembler.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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_DBGF
22#include <VBox/vmm/dbgf.h>
23#include <VBox/vmm/selm.h>
24#include <VBox/vmm/mm.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/cpum.h>
27#include "DBGFInternal.h"
28#include <VBox/dis.h>
29#include <VBox/err.h>
30#include <VBox/param.h>
31#include <VBox/vmm/vm.h>
32#include "internal/pgm.h"
33
34#include <VBox/log.h>
35#include <iprt/assert.h>
36#include <iprt/string.h>
37#include <iprt/alloca.h>
38#include <iprt/ctype.h>
39
40
41/*******************************************************************************
42* Structures and Typedefs *
43*******************************************************************************/
44/**
45 * Structure used when disassembling and instructions in DBGF.
46 * This is used so the reader function can get the stuff it needs.
47 */
48typedef struct
49{
50 /** The core structure. */
51 DISCPUSTATE Cpu;
52 /** Pointer to the VM. */
53 PVM pVM;
54 /** Pointer to the VMCPU. */
55 PVMCPU pVCpu;
56 /** The address space for resolving symbol. */
57 RTDBGAS hAs;
58 /** Pointer to the first byte in the segment. */
59 RTGCUINTPTR GCPtrSegBase;
60 /** Pointer to the byte after the end of the segment. (might have wrapped!) */
61 RTGCUINTPTR GCPtrSegEnd;
62 /** The size of the segment minus 1. */
63 RTGCUINTPTR cbSegLimit;
64 /** The guest paging mode. */
65 PGMMODE enmMode;
66 /** Pointer to the current page - R3 Ptr. */
67 void const *pvPageR3;
68 /** Pointer to the current page - GC Ptr. */
69 RTGCPTR GCPtrPage;
70 /** Pointer to the next instruction (relative to GCPtrSegBase). */
71 RTGCUINTPTR GCPtrNext;
72 /** The lock information that PGMPhysReleasePageMappingLock needs. */
73 PGMPAGEMAPLOCK PageMapLock;
74 /** Whether the PageMapLock is valid or not. */
75 bool fLocked;
76 /** 64 bits mode or not. */
77 bool f64Bits;
78} DBGFDISASSTATE, *PDBGFDISASSTATE;
79
80
81/*******************************************************************************
82* Internal Functions *
83*******************************************************************************/
84static FNDISREADBYTES dbgfR3DisasInstrRead;
85
86
87
88/**
89 * Calls the disassembler with the proper reader functions and such for disa
90 *
91 * @returns VBox status code.
92 * @param pVM Pointer to the VM.
93 * @param pVCpu Pointer to the VMCPU.
94 * @param pSelInfo The selector info.
95 * @param enmMode The guest paging mode.
96 * @param fFlags DBGF_DISAS_FLAGS_XXX.
97 * @param GCPtr The GC pointer (selector offset).
98 * @param pState The disas CPU state.
99 */
100static int dbgfR3DisasInstrFirst(PVM pVM, PVMCPU pVCpu, PDBGFSELINFO pSelInfo, PGMMODE enmMode,
101 RTGCPTR GCPtr, uint32_t fFlags, PDBGFDISASSTATE pState)
102{
103 pState->GCPtrSegBase = pSelInfo->GCPtrBase;
104 pState->GCPtrSegEnd = pSelInfo->cbLimit + 1 + (RTGCUINTPTR)pSelInfo->GCPtrBase;
105 pState->cbSegLimit = pSelInfo->cbLimit;
106 pState->enmMode = enmMode;
107 pState->GCPtrPage = 0;
108 pState->pvPageR3 = NULL;
109 pState->hAs = pSelInfo->fFlags & DBGFSELINFO_FLAGS_HYPER /** @todo Deal more explicitly with RC in DBGFR3Disas*. */
110 ? DBGF_AS_RC_AND_GC_GLOBAL
111 : DBGF_AS_GLOBAL;
112 pState->pVM = pVM;
113 pState->pVCpu = pVCpu;
114 pState->fLocked = false;
115 pState->f64Bits = enmMode >= PGMMODE_AMD64 && pSelInfo->u.Raw.Gen.u1Long;
116
117 DISCPUMODE enmCpuMode;
118 switch (fFlags & DBGF_DISAS_FLAGS_MODE_MASK)
119 {
120 default:
121 AssertFailed();
122 case DBGF_DISAS_FLAGS_DEFAULT_MODE:
123 enmCpuMode = pState->f64Bits
124 ? DISCPUMODE_64BIT
125 : pSelInfo->u.Raw.Gen.u1DefBig
126 ? DISCPUMODE_32BIT
127 : DISCPUMODE_16BIT;
128 break;
129 case DBGF_DISAS_FLAGS_16BIT_MODE:
130 case DBGF_DISAS_FLAGS_16BIT_REAL_MODE:
131 enmCpuMode = DISCPUMODE_16BIT;
132 break;
133 case DBGF_DISAS_FLAGS_32BIT_MODE:
134 enmCpuMode = DISCPUMODE_32BIT;
135 break;
136 case DBGF_DISAS_FLAGS_64BIT_MODE:
137 enmCpuMode = DISCPUMODE_64BIT;
138 break;
139 }
140
141 uint32_t cbInstr;
142 int rc = DISInstrWithReader(GCPtr,
143 enmCpuMode,
144 dbgfR3DisasInstrRead,
145 &pState->Cpu,
146 &pState->Cpu,
147 &cbInstr);
148 if (RT_SUCCESS(rc))
149 {
150 pState->GCPtrNext = GCPtr + cbInstr;
151 return VINF_SUCCESS;
152 }
153
154 /* cleanup */
155 if (pState->fLocked)
156 {
157 PGMPhysReleasePageMappingLock(pVM, &pState->PageMapLock);
158 pState->fLocked = false;
159 }
160 return rc;
161}
162
163
164#if 0
165/**
166 * Calls the disassembler for disassembling the next instruction.
167 *
168 * @returns VBox status code.
169 * @param pState The disas CPU state.
170 */
171static int dbgfR3DisasInstrNext(PDBGFDISASSTATE pState)
172{
173 uint32_t cbInstr;
174 int rc = DISInstr(&pState->Cpu, (void *)pState->GCPtrNext, 0, &cbInstr, NULL);
175 if (RT_SUCCESS(rc))
176 {
177 pState->GCPtrNext = GCPtr + cbInstr;
178 return VINF_SUCCESS;
179 }
180 return rc;
181}
182#endif
183
184
185/**
186 * Done with the disassembler state, free associated resources.
187 *
188 * @param pState The disas CPU state ++.
189 */
190static void dbgfR3DisasInstrDone(PDBGFDISASSTATE pState)
191{
192 if (pState->fLocked)
193 {
194 PGMPhysReleasePageMappingLock(pState->pVM, &pState->PageMapLock);
195 pState->fLocked = false;
196 }
197}
198
199
200/**
201 * @callback_method_impl{FNDISREADBYTES}
202 *
203 * @remarks The source is relative to the base address indicated by
204 * DBGFDISASSTATE::GCPtrSegBase.
205 */
206static DECLCALLBACK(int) dbgfR3DisasInstrRead(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
207{
208 PDBGFDISASSTATE pState = (PDBGFDISASSTATE)pDis;
209 for (;;)
210 {
211 RTGCUINTPTR GCPtr = pDis->uInstrAddr + offInstr + pState->GCPtrSegBase;
212
213 /*
214 * Need to update the page translation?
215 */
216 if ( !pState->pvPageR3
217 || (GCPtr >> PAGE_SHIFT) != (pState->GCPtrPage >> PAGE_SHIFT))
218 {
219 int rc = VINF_SUCCESS;
220
221 /* translate the address */
222 pState->GCPtrPage = GCPtr & PAGE_BASE_GC_MASK;
223 if (MMHyperIsInsideArea(pState->pVM, pState->GCPtrPage))
224 {
225 pState->pvPageR3 = MMHyperRCToR3(pState->pVM, (RTRCPTR)pState->GCPtrPage);
226 if (!pState->pvPageR3)
227 rc = VERR_INVALID_POINTER;
228 }
229 else
230 {
231 if (pState->fLocked)
232 PGMPhysReleasePageMappingLock(pState->pVM, &pState->PageMapLock);
233
234 if (pState->enmMode <= PGMMODE_PROTECTED)
235 rc = PGMPhysGCPhys2CCPtrReadOnly(pState->pVM, pState->GCPtrPage, &pState->pvPageR3, &pState->PageMapLock);
236 else
237 rc = PGMPhysGCPtr2CCPtrReadOnly(pState->pVCpu, pState->GCPtrPage, &pState->pvPageR3, &pState->PageMapLock);
238 pState->fLocked = RT_SUCCESS_NP(rc);
239 }
240 if (RT_FAILURE(rc))
241 {
242 pState->pvPageR3 = NULL;
243 return rc;
244 }
245 }
246
247 /*
248 * Check the segment limit.
249 */
250 if (!pState->f64Bits && pDis->uInstrAddr + offInstr > pState->cbSegLimit)
251 return VERR_OUT_OF_SELECTOR_BOUNDS;
252
253 /*
254 * Calc how much we can read, maxing out the read.
255 */
256 uint32_t cb = PAGE_SIZE - (GCPtr & PAGE_OFFSET_MASK);
257 if (!pState->f64Bits)
258 {
259 RTGCUINTPTR cbSeg = pState->GCPtrSegEnd - GCPtr;
260 if (cb > cbSeg && cbSeg)
261 cb = cbSeg;
262 }
263 if (cb > cbMaxRead)
264 cb = cbMaxRead;
265
266 /*
267 * Read and advance,
268 */
269 memcpy(&pDis->abInstr[offInstr], (char *)pState->pvPageR3 + (GCPtr & PAGE_OFFSET_MASK), cb);
270 offInstr += (uint8_t)cb;
271 if (cb >= cbMinRead)
272 {
273 pDis->cbCachedInstr = offInstr;
274 return VINF_SUCCESS;
275 }
276 cbMaxRead -= (uint8_t)cb;
277 cbMinRead -= (uint8_t)cb;
278 }
279}
280
281
282/**
283 * @copydoc FNDISGETSYMBOL
284 */
285static DECLCALLBACK(int) dbgfR3DisasGetSymbol(PCDISCPUSTATE pCpu, uint32_t u32Sel, RTUINTPTR uAddress, char *pszBuf, size_t cchBuf, RTINTPTR *poff, void *pvUser)
286{
287 PDBGFDISASSTATE pState = (PDBGFDISASSTATE)pCpu;
288 PCDBGFSELINFO pSelInfo = (PCDBGFSELINFO)pvUser;
289 DBGFADDRESS Addr;
290 RTDBGSYMBOL Sym;
291 RTGCINTPTR off;
292 int rc;
293
294 if ( DIS_FMT_SEL_IS_REG(u32Sel)
295 ? DIS_FMT_SEL_GET_REG(u32Sel) == DISSELREG_CS
296 : pSelInfo->Sel == DIS_FMT_SEL_GET_VALUE(u32Sel))
297 {
298 rc = DBGFR3AddrFromSelInfoOff(pState->pVM, &Addr, pSelInfo, uAddress);
299 if (RT_SUCCESS(rc))
300 rc = DBGFR3AsSymbolByAddr(pState->pVM, pState->hAs, &Addr, &off, &Sym, NULL /*phMod*/);
301 }
302 else
303 rc = VERR_SYMBOL_NOT_FOUND; /** @todo implement this */
304 if (RT_SUCCESS(rc))
305 {
306 size_t cchName = strlen(Sym.szName);
307 if (cchName >= cchBuf)
308 cchName = cchBuf - 1;
309 memcpy(pszBuf, Sym.szName, cchName);
310 pszBuf[cchName] = '\0';
311
312 *poff = off;
313 }
314
315 return rc;
316}
317
318
319/**
320 * Disassembles the one instruction according to the specified flags and
321 * address, internal worker executing on the EMT of the specified virtual CPU.
322 *
323 * @returns VBox status code.
324 * @param pVM Pointer to the VM.
325 * @param pVCpu Pointer to the VMCPU.
326 * @param Sel The code selector. This used to determine the 32/16 bit ness and
327 * calculation of the actual instruction address.
328 * @param pGCPtr Pointer to the variable holding the code address
329 * relative to the base of Sel.
330 * @param fFlags Flags controlling where to start and how to format.
331 * A combination of the DBGF_DISAS_FLAGS_* \#defines.
332 * @param pszOutput Output buffer.
333 * @param cbOutput Size of the output buffer.
334 * @param pcbInstr Where to return the size of the instruction.
335 */
336static DECLCALLBACK(int)
337dbgfR3DisasInstrExOnVCpu(PVM pVM, PVMCPU pVCpu, RTSEL Sel, PRTGCPTR pGCPtr, uint32_t fFlags,
338 char *pszOutput, uint32_t cbOutput, uint32_t *pcbInstr)
339{
340 VMCPU_ASSERT_EMT(pVCpu);
341 RTGCPTR GCPtr = *pGCPtr;
342
343 /*
344 * Get the Sel and GCPtr if fFlags requests that.
345 */
346 PCCPUMCTXCORE pCtxCore = NULL;
347 PCPUMSELREGHID pHiddenSel = NULL;
348 int rc;
349 if (fFlags & (DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER))
350 {
351 if (fFlags & DBGF_DISAS_FLAGS_CURRENT_GUEST)
352 pCtxCore = CPUMGetGuestCtxCore(pVCpu);
353 else
354 pCtxCore = CPUMGetHyperCtxCore(pVCpu);
355 Sel = pCtxCore->cs.Sel;
356 pHiddenSel = (PCPUMSELREGHID)&pCtxCore->cs;
357 GCPtr = pCtxCore->rip;
358 }
359
360 /*
361 * Read the selector info - assume no stale selectors and nasty stuff like that.
362 * Since the selector flags in the CPUMCTX structures aren't up to date unless
363 * we recently visited REM, we'll not search for the selector there.
364 */
365 DBGFSELINFO SelInfo;
366 const PGMMODE enmMode = PGMGetGuestMode(pVCpu);
367 bool fRealModeAddress = false;
368
369 if ( pHiddenSel
370 && ( (fFlags & DBGF_DISAS_FLAGS_HID_SEL_REGS_VALID)
371 || CPUMAreHiddenSelRegsValid(pVCpu)))
372 {
373 SelInfo.Sel = Sel;
374 SelInfo.SelGate = 0;
375 SelInfo.GCPtrBase = pHiddenSel->u64Base;
376 SelInfo.cbLimit = pHiddenSel->u32Limit;
377 SelInfo.fFlags = PGMMODE_IS_LONG_MODE(enmMode)
378 ? DBGFSELINFO_FLAGS_LONG_MODE
379 : enmMode != PGMMODE_REAL && (!pCtxCore || !pCtxCore->eflags.Bits.u1VM)
380 ? DBGFSELINFO_FLAGS_PROT_MODE
381 : DBGFSELINFO_FLAGS_REAL_MODE;
382
383 SelInfo.u.Raw.au32[0] = 0;
384 SelInfo.u.Raw.au32[1] = 0;
385 SelInfo.u.Raw.Gen.u16LimitLow = 0xffff;
386 SelInfo.u.Raw.Gen.u4LimitHigh = 0xf;
387 SelInfo.u.Raw.Gen.u1Present = pHiddenSel->Attr.n.u1Present;
388 SelInfo.u.Raw.Gen.u1Granularity = pHiddenSel->Attr.n.u1Granularity;;
389 SelInfo.u.Raw.Gen.u1DefBig = pHiddenSel->Attr.n.u1DefBig;
390 SelInfo.u.Raw.Gen.u1Long = pHiddenSel->Attr.n.u1Long;
391 SelInfo.u.Raw.Gen.u1DescType = pHiddenSel->Attr.n.u1DescType;
392 SelInfo.u.Raw.Gen.u4Type = pHiddenSel->Attr.n.u4Type;
393 fRealModeAddress = !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE);
394 }
395 else if (Sel == DBGF_SEL_FLAT)
396 {
397 SelInfo.Sel = Sel;
398 SelInfo.SelGate = 0;
399 SelInfo.GCPtrBase = 0;
400 SelInfo.cbLimit = ~0;
401 SelInfo.fFlags = PGMMODE_IS_LONG_MODE(enmMode)
402 ? DBGFSELINFO_FLAGS_LONG_MODE
403 : enmMode != PGMMODE_REAL
404 ? DBGFSELINFO_FLAGS_PROT_MODE
405 : DBGFSELINFO_FLAGS_REAL_MODE;
406 SelInfo.u.Raw.au32[0] = 0;
407 SelInfo.u.Raw.au32[1] = 0;
408 SelInfo.u.Raw.Gen.u16LimitLow = 0xffff;
409 SelInfo.u.Raw.Gen.u4LimitHigh = 0xf;
410
411 if ( (fFlags & DBGF_DISAS_FLAGS_HID_SEL_REGS_VALID)
412 || CPUMAreHiddenSelRegsValid(pVCpu))
413 { /* Assume the current CS defines the execution mode. */
414 pCtxCore = CPUMGetGuestCtxCore(pVCpu);
415 pHiddenSel = (CPUMSELREGHID *)&pCtxCore->cs;
416
417 SelInfo.u.Raw.Gen.u1Present = pHiddenSel->Attr.n.u1Present;
418 SelInfo.u.Raw.Gen.u1Granularity = pHiddenSel->Attr.n.u1Granularity;;
419 SelInfo.u.Raw.Gen.u1DefBig = pHiddenSel->Attr.n.u1DefBig;
420 SelInfo.u.Raw.Gen.u1Long = pHiddenSel->Attr.n.u1Long;
421 SelInfo.u.Raw.Gen.u1DescType = pHiddenSel->Attr.n.u1DescType;
422 SelInfo.u.Raw.Gen.u4Type = pHiddenSel->Attr.n.u4Type;
423 }
424 else
425 {
426 SelInfo.u.Raw.Gen.u1Present = 1;
427 SelInfo.u.Raw.Gen.u1Granularity = 1;
428 SelInfo.u.Raw.Gen.u1DefBig = 1;
429 SelInfo.u.Raw.Gen.u1DescType = 1;
430 SelInfo.u.Raw.Gen.u4Type = X86_SEL_TYPE_EO;
431 }
432 }
433 else if ( !(fFlags & DBGF_DISAS_FLAGS_CURRENT_HYPER)
434 && ( (pCtxCore && pCtxCore->eflags.Bits.u1VM)
435 || enmMode == PGMMODE_REAL
436 || (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
437 )
438 )
439 { /* V86 mode or real mode - real mode addressing */
440 SelInfo.Sel = Sel;
441 SelInfo.SelGate = 0;
442 SelInfo.GCPtrBase = Sel * 16;
443 SelInfo.cbLimit = ~0;
444 SelInfo.fFlags = DBGFSELINFO_FLAGS_REAL_MODE;
445 SelInfo.u.Raw.au32[0] = 0;
446 SelInfo.u.Raw.au32[1] = 0;
447 SelInfo.u.Raw.Gen.u16LimitLow = 0xffff;
448 SelInfo.u.Raw.Gen.u4LimitHigh = 0xf;
449 SelInfo.u.Raw.Gen.u1Present = 1;
450 SelInfo.u.Raw.Gen.u1Granularity = 1;
451 SelInfo.u.Raw.Gen.u1DefBig = 0; /* 16 bits */
452 SelInfo.u.Raw.Gen.u1DescType = 1;
453 SelInfo.u.Raw.Gen.u4Type = X86_SEL_TYPE_EO;
454 fRealModeAddress = true;
455 }
456 else
457 {
458 rc = SELMR3GetSelectorInfo(pVM, pVCpu, Sel, &SelInfo);
459 if (RT_FAILURE(rc))
460 {
461 RTStrPrintf(pszOutput, cbOutput, "Sel=%04x -> %Rrc\n", Sel, rc);
462 return rc;
463 }
464 }
465
466 /*
467 * Disassemble it.
468 */
469 DBGFDISASSTATE State;
470 rc = dbgfR3DisasInstrFirst(pVM, pVCpu, &SelInfo, enmMode, GCPtr, fFlags, &State);
471 if (RT_FAILURE(rc))
472 {
473 RTStrPrintf(pszOutput, cbOutput, "Disas -> %Rrc\n", rc);
474 return rc;
475 }
476
477 /*
478 * Format it.
479 */
480 char szBuf[512];
481 DISFormatYasmEx(&State.Cpu, szBuf, sizeof(szBuf),
482 DIS_FMT_FLAGS_RELATIVE_BRANCH,
483 fFlags & DBGF_DISAS_FLAGS_NO_SYMBOLS ? NULL : dbgfR3DisasGetSymbol,
484 &SelInfo);
485
486 /*
487 * Print it to the user specified buffer.
488 */
489 if (fFlags & DBGF_DISAS_FLAGS_NO_BYTES)
490 {
491 if (fFlags & DBGF_DISAS_FLAGS_NO_ADDRESS)
492 RTStrPrintf(pszOutput, cbOutput, "%s", szBuf);
493 else if (fRealModeAddress)
494 RTStrPrintf(pszOutput, cbOutput, "%04x:%04x %s", Sel, (unsigned)GCPtr, szBuf);
495 else if (Sel == DBGF_SEL_FLAT)
496 {
497 if (enmMode >= PGMMODE_AMD64)
498 RTStrPrintf(pszOutput, cbOutput, "%RGv %s", GCPtr, szBuf);
499 else
500 RTStrPrintf(pszOutput, cbOutput, "%08RX32 %s", (uint32_t)GCPtr, szBuf);
501 }
502 else
503 {
504 if (enmMode >= PGMMODE_AMD64)
505 RTStrPrintf(pszOutput, cbOutput, "%04x:%RGv %s", Sel, GCPtr, szBuf);
506 else
507 RTStrPrintf(pszOutput, cbOutput, "%04x:%08RX32 %s", Sel, (uint32_t)GCPtr, szBuf);
508 }
509 }
510 else
511 {
512 uint32_t cbInstr = State.Cpu.cbInstr;
513 uint8_t const *pabInstr = State.Cpu.abInstr;
514 if (fFlags & DBGF_DISAS_FLAGS_NO_ADDRESS)
515 RTStrPrintf(pszOutput, cbOutput, "%.*Rhxs%*s %s",
516 cbInstr, pabInstr, cbInstr < 8 ? (8 - cbInstr) * 3 : 0, "",
517 szBuf);
518 else if (fRealModeAddress)
519 RTStrPrintf(pszOutput, cbOutput, "%04x:%04x %.*Rhxs%*s %s",
520 Sel, (unsigned)GCPtr,
521 cbInstr, pabInstr, cbInstr < 8 ? (8 - cbInstr) * 3 : 0, "",
522 szBuf);
523 else if (Sel == DBGF_SEL_FLAT)
524 {
525 if (enmMode >= PGMMODE_AMD64)
526 RTStrPrintf(pszOutput, cbOutput, "%RGv %.*Rhxs%*s %s",
527 GCPtr,
528 cbInstr, pabInstr, cbInstr < 8 ? (8 - cbInstr) * 3 : 0, "",
529 szBuf);
530 else
531 RTStrPrintf(pszOutput, cbOutput, "%08RX32 %.*Rhxs%*s %s",
532 (uint32_t)GCPtr,
533 cbInstr, pabInstr, cbInstr < 8 ? (8 - cbInstr) * 3 : 0, "",
534 szBuf);
535 }
536 else
537 {
538 if (enmMode >= PGMMODE_AMD64)
539 RTStrPrintf(pszOutput, cbOutput, "%04x:%RGv %.*Rhxs%*s %s",
540 Sel, GCPtr,
541 cbInstr, pabInstr, cbInstr < 8 ? (8 - cbInstr) * 3 : 0, "",
542 szBuf);
543 else
544 RTStrPrintf(pszOutput, cbOutput, "%04x:%08RX32 %.*Rhxs%*s %s",
545 Sel, (uint32_t)GCPtr,
546 cbInstr, pabInstr, cbInstr < 8 ? (8 - cbInstr) * 3 : 0, "",
547 szBuf);
548 }
549 }
550
551 if (pcbInstr)
552 *pcbInstr = State.Cpu.cbInstr;
553
554 dbgfR3DisasInstrDone(&State);
555 return VINF_SUCCESS;
556}
557
558
559/**
560 * Disassembles the one instruction according to the specified flags and address.
561 *
562 * @returns VBox status code.
563 * @param pVM Pointer to the VM.
564 * @param idCpu The ID of virtual CPU.
565 * @param Sel The code selector. This used to determine the 32/16 bit ness and
566 * calculation of the actual instruction address.
567 * @param GCPtr The code address relative to the base of Sel.
568 * @param fFlags Flags controlling where to start and how to format.
569 * A combination of the DBGF_DISAS_FLAGS_* \#defines.
570 * @param pszOutput Output buffer. This will always be properly
571 * terminated if @a cbOutput is greater than zero.
572 * @param cbOutput Size of the output buffer.
573 * @param pcbInstr Where to return the size of the instruction.
574 *
575 * @remarks May have to switch to the EMT of the virtual CPU in order to do
576 * address conversion.
577 */
578VMMR3DECL(int) DBGFR3DisasInstrEx(PVM pVM, VMCPUID idCpu, RTSEL Sel, RTGCPTR GCPtr, uint32_t fFlags,
579 char *pszOutput, uint32_t cbOutput, uint32_t *pcbInstr)
580{
581 AssertReturn(cbOutput > 0, VERR_INVALID_PARAMETER);
582 *pszOutput = '\0';
583 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
584 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
585 AssertReturn(!(fFlags & ~DBGF_DISAS_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
586 AssertReturn((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) <= DBGF_DISAS_FLAGS_64BIT_MODE, VERR_INVALID_PARAMETER);
587
588 /*
589 * Optimize the common case where we're called on the EMT of idCpu since
590 * we're using this all the time when logging.
591 */
592 int rc;
593 PVMCPU pVCpu = VMMGetCpu(pVM);
594 if ( pVCpu
595 && pVCpu->idCpu == idCpu)
596 rc = dbgfR3DisasInstrExOnVCpu(pVM, pVCpu, Sel, &GCPtr, fFlags, pszOutput, cbOutput, pcbInstr);
597 else
598 rc = VMR3ReqPriorityCallWait(pVM, idCpu, (PFNRT)dbgfR3DisasInstrExOnVCpu, 8,
599 pVM, VMMGetCpuById(pVM, idCpu), Sel, &GCPtr, fFlags, pszOutput, cbOutput, pcbInstr);
600 return rc;
601}
602
603
604/**
605 * Disassembles the current guest context instruction.
606 * All registers and data will be displayed. Addresses will be attempted resolved to symbols.
607 *
608 * @returns VBox status code.
609 * @param pVCpu Pointer to the VMCPU.
610 * @param pszOutput Output buffer. This will always be properly
611 * terminated if @a cbOutput is greater than zero.
612 * @param cbOutput Size of the output buffer.
613 */
614VMMR3DECL(int) DBGFR3DisasInstrCurrent(PVMCPU pVCpu, char *pszOutput, uint32_t cbOutput)
615{
616 AssertReturn(cbOutput > 0, VERR_INVALID_PARAMETER);
617 *pszOutput = '\0';
618 AssertReturn(pVCpu, VERR_INVALID_CONTEXT);
619 return DBGFR3DisasInstrEx(pVCpu->pVMR3, pVCpu->idCpu, 0, 0,
620 DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_DEFAULT_MODE,
621 pszOutput, cbOutput, NULL);
622}
623
624
625/**
626 * Disassembles the current guest context instruction and writes it to the log.
627 * All registers and data will be displayed. Addresses will be attempted resolved to symbols.
628 *
629 * @returns VBox status code.
630 * @param pVCpu Pointer to the VMCPU.
631 * @param pszPrefix Short prefix string to the disassembly string. (optional)
632 */
633VMMR3DECL(int) DBGFR3DisasInstrCurrentLogInternal(PVMCPU pVCpu, const char *pszPrefix)
634{
635 char szBuf[256];
636 szBuf[0] = '\0';
637 int rc = DBGFR3DisasInstrCurrent(pVCpu, &szBuf[0], sizeof(szBuf));
638 if (RT_FAILURE(rc))
639 RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrCurrentLog failed with rc=%Rrc\n", rc);
640 if (pszPrefix && *pszPrefix)
641 RTLogPrintf("%s-CPU%u: %s\n", pszPrefix, pVCpu->idCpu, szBuf);
642 else
643 RTLogPrintf("%s\n", szBuf);
644 return rc;
645}
646
647
648
649/**
650 * Disassembles the specified guest context instruction and writes it to the log.
651 * Addresses will be attempted resolved to symbols.
652 *
653 * @returns VBox status code.
654 * @param pVM Pointer to the VM.
655 * @param pVCpu Pointer to the VMCPU, defaults to CPU 0 if NULL.
656 * @param Sel The code selector. This used to determine the 32/16 bit-ness and
657 * calculation of the actual instruction address.
658 * @param GCPtr The code address relative to the base of Sel.
659 * @param pszPrefix Short prefix string to the disassembly string. (optional)
660 */
661VMMR3DECL(int) DBGFR3DisasInstrLogInternal(PVMCPU pVCpu, RTSEL Sel, RTGCPTR GCPtr, const char *pszPrefix)
662{
663 char szBuf[256];
664 int rc = DBGFR3DisasInstrEx(pVCpu->pVMR3, pVCpu->idCpu, Sel, GCPtr, DBGF_DISAS_FLAGS_DEFAULT_MODE,
665 &szBuf[0], sizeof(szBuf), NULL);
666 if (RT_FAILURE(rc))
667 RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrLog(, %RTsel, %RGv) failed with rc=%Rrc\n", Sel, GCPtr, rc);
668 if (pszPrefix && *pszPrefix)
669 RTLogPrintf("%s-CPU%u: %s\n", pszPrefix, pVCpu->idCpu, szBuf);
670 else
671 RTLogPrintf("%s\n", szBuf);
672 return rc;
673}
674
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