VirtualBox

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

Last change on this file since 7954 was 7484, checked in by vboxsync, 17 years ago

make gcc happy

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 25.3 KB
Line 
1/* $Id: DBGFDisas.cpp 7484 2008-03-18 10:36:12Z vboxsync $ */
2/** @file
3 * VMM DBGF - Debugger Facility, Disassembler.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGF
23#include <VBox/dbgf.h>
24#include <VBox/selm.h>
25#include <VBox/mm.h>
26#include <VBox/pgm.h>
27#include <VBox/cpum.h>
28#include "DBGFInternal.h"
29#include <VBox/dis.h>
30#include <VBox/err.h>
31#include <VBox/param.h>
32
33#include <VBox/log.h>
34#include <iprt/assert.h>
35#include <iprt/string.h>
36#include <iprt/alloca.h>
37#include <iprt/ctype.h>
38
39
40/*******************************************************************************
41* Internal Functions *
42*******************************************************************************/
43static DECLCALLBACK(int) dbgfR3DisasInstrRead(RTHCUINTPTR pSrc, uint8_t *pDest, uint32_t size, void *pvUserdata);
44
45
46/**
47 * Structure used when disassembling and instructions in DBGF.
48 * This is used so the reader function can get the stuff it needs.
49 */
50typedef struct
51{
52 /** The core structure. */
53 DISCPUSTATE Cpu;
54 /** The VM handle. */
55 PVM pVM;
56 /** Pointer to the first byte in the segemnt. */
57 RTGCUINTPTR GCPtrSegBase;
58 /** Pointer to the byte after the end of the segment. (might have wrapped!) */
59 RTGCUINTPTR GCPtrSegEnd;
60 /** The size of the segment minus 1. */
61 RTGCUINTPTR cbSegLimit;
62 /** The guest paging mode. */
63 PGMMODE enmMode;
64 /** Pointer to the current page - HC Ptr. */
65 void const *pvPageHC;
66 /** Pointer to the current page - GC Ptr. */
67 RTGCPTR pvPageGC;
68 /** Pointer to the next instruction (relative to GCPtrSegBase). */
69 RTGCUINTPTR GCPtrNext;
70 /** The lock information that PGMPhysReleasePageMappingLock needs. */
71 PGMPAGEMAPLOCK PageMapLock;
72 /** Whether the PageMapLock is valid or not. */
73 bool fLocked;
74} DBGFDISASSTATE, *PDBGFDISASSTATE;
75
76
77
78/**
79 * Calls the dissassembler with the proper reader functions and such for disa
80 *
81 * @returns VBox status code.
82 * @param pVM VM handle
83 * @param pSelInfo The selector info.
84 * @param enmMode The guest paging mode.
85 * @param GCPtr The GC pointer (selector offset).
86 * @param pState The disas CPU state.
87 */
88static int dbgfR3DisasInstrFirst(PVM pVM, PSELMSELINFO pSelInfo, PGMMODE enmMode, RTGCPTR GCPtr, PDBGFDISASSTATE pState)
89{
90 pState->Cpu.mode = pSelInfo->Raw.Gen.u1DefBig ? CPUMODE_32BIT : CPUMODE_16BIT;
91 pState->Cpu.pfnReadBytes = dbgfR3DisasInstrRead;
92 pState->GCPtrSegBase = pSelInfo->GCPtrBase;
93 pState->GCPtrSegEnd = pSelInfo->cbLimit + 1 + (RTGCUINTPTR)pSelInfo->GCPtrBase;
94 pState->cbSegLimit = pSelInfo->cbLimit;
95 pState->enmMode = enmMode;
96 pState->pvPageGC = 0;
97 pState->pvPageHC = NULL;
98 pState->pVM = pVM;
99 pState->fLocked = false;
100 Assert((uintptr_t)GCPtr == GCPtr);
101 uint32_t cbInstr;
102 int rc = DISInstr(&pState->Cpu, GCPtr, 0, &cbInstr, NULL);
103 if (VBOX_SUCCESS(rc))
104 {
105 pState->GCPtrNext = GCPtr + cbInstr;
106 return VINF_SUCCESS;
107 }
108
109 /* cleanup */
110 if (pState->fLocked)
111 {
112 PGMPhysReleasePageMappingLock(pVM, &pState->PageMapLock);
113 pState->fLocked = false;
114 }
115 return rc;
116}
117
118
119#if 0
120/**
121 * Calls the dissassembler for disassembling the next instruction.
122 *
123 * @returns VBox status code.
124 * @param pState The disas CPU state.
125 */
126static int dbgfR3DisasInstrNext(PDBGFDISASSTATE pState)
127{
128 uint32_t cbInstr;
129 int rc = DISInstr(&pState->Cpu, (void *)pState->GCPtrNext, 0, &cbInstr, NULL);
130 if (VBOX_SUCCESS(rc))
131 {
132 pState->GCPtrNext = GCPtr + cbInstr;
133 return VINF_SUCCESS;
134 }
135 return rc;
136}
137#endif
138
139
140/**
141 * Done with the dissassembler state, free associated resources.
142 *
143 * @param pState The disas CPU state ++.
144 */
145static void dbgfR3DisasInstrDone(PDBGFDISASSTATE pState)
146{
147 if (pState->fLocked)
148 {
149 PGMPhysReleasePageMappingLock(pState->pVM, &pState->PageMapLock);
150 pState->fLocked = false;
151 }
152}
153
154
155/**
156 * Instruction reader.
157 *
158 * @returns VBox status code. (Why this is a int32_t and not just an int is also beyond me.)
159 * @param PtrSrc Address to read from.
160 * In our case this is relative to the selector pointed to by the 2nd user argument of uDisCpu.
161 * @param pu8Dst Where to store the bytes.
162 * @param cbRead Number of bytes to read.
163 * @param uDisCpu Pointer to the disassembler cpu state. (Why this is a VBOXHUINTPTR is beyond me...)
164 * In this context it's always pointer to the Core of a DBGFDISASSTATE.
165 */
166static DECLCALLBACK(int) dbgfR3DisasInstrRead(RTHCUINTPTR PtrSrc, uint8_t *pu8Dst, uint32_t cbRead, void *pvDisCpu)
167{
168 PDBGFDISASSTATE pState = (PDBGFDISASSTATE)pvDisCpu;
169 Assert(cbRead > 0);
170 for (;;)
171 {
172 RTGCUINTPTR GCPtr = PtrSrc + pState->GCPtrSegBase;
173
174 /* Need to update the page translation? */
175 if ( !pState->pvPageHC
176 || (GCPtr >> PAGE_SHIFT) != (pState->pvPageGC >> PAGE_SHIFT))
177 {
178 int rc = VINF_SUCCESS;
179
180 /* translate the address */
181 pState->pvPageGC = GCPtr & PAGE_BASE_GC_MASK;
182 if (MMHyperIsInsideArea(pState->pVM, pState->pvPageGC))
183 {
184 pState->pvPageHC = MMHyperGC2HC(pState->pVM, pState->pvPageGC);
185 if (!pState->pvPageHC)
186 rc = VERR_INVALID_POINTER;
187 }
188 else
189 {
190 if (pState->fLocked)
191 PGMPhysReleasePageMappingLock(pState->pVM, &pState->PageMapLock);
192
193 if (pState->enmMode <= PGMMODE_PROTECTED)
194 rc = PGMPhysGCPhys2CCPtrReadOnly(pState->pVM, pState->pvPageGC, &pState->pvPageHC, &pState->PageMapLock);
195 else
196 rc = PGMPhysGCPtr2CCPtrReadOnly(pState->pVM, pState->pvPageGC, &pState->pvPageHC, &pState->PageMapLock);
197 pState->fLocked = RT_SUCCESS_NP(rc);
198 }
199 if (VBOX_FAILURE(rc))
200 {
201 pState->pvPageHC = NULL;
202 return rc;
203 }
204 }
205
206 /* check the segemnt limit */
207 if (PtrSrc > pState->cbSegLimit)
208 return VERR_OUT_OF_SELECTOR_BOUNDS;
209
210 /* calc how much we can read */
211 uint32_t cb = PAGE_SIZE - (GCPtr & PAGE_OFFSET_MASK);
212 RTGCUINTPTR cbSeg = pState->GCPtrSegEnd - GCPtr;
213 if (cb > cbSeg && cbSeg)
214 cb = cbSeg;
215 if (cb > cbRead)
216 cb = cbRead;
217
218 /* read and advance */
219 memcpy(pu8Dst, (char *)pState->pvPageHC + (GCPtr & PAGE_OFFSET_MASK), cb);
220 cbRead -= cb;
221 if (!cbRead)
222 return VINF_SUCCESS;
223 pu8Dst += cb;
224 PtrSrc += cb;
225 }
226}
227
228
229/**
230 * Copy a string and return pointer to the terminator char in the copy.
231 */
232inline char *mystrpcpy(char *pszDst, const char *pszSrc)
233{
234 size_t cch = strlen(pszSrc);
235 memcpy(pszDst, pszSrc, cch + 1);
236 return pszDst + cch;
237}
238
239
240/**
241 * Disassembles the one instruction according to the specified flags and address.
242 *
243 * @returns VBox status code.
244 * @param pVM VM handle.
245 * @param Sel The code selector. This used to determin the 32/16 bit ness and
246 * calculation of the actual instruction address.
247 * @param GCPtr The code address relative to the base of Sel.
248 * @param fFlags Flags controlling where to start and how to format.
249 * A combination of the DBGF_DISAS_FLAGS_* \#defines.
250 * @param pszOutput Output buffer.
251 * @param cchOutput Size of the output buffer.
252 * @param pcbInstr Where to return the size of the instruction.
253 */
254DBGFR3DECL(int) DBGFR3DisasInstrEx(PVM pVM, RTSEL Sel, RTGCPTR GCPtr, unsigned fFlags, char *pszOutput, uint32_t cchOutput, uint32_t *pcbInstr)
255{
256 /*
257 * Get the Sel and GCPtr if fFlags requests that.
258 */
259 PCCPUMCTXCORE pCtxCore = NULL;
260 CPUMSELREGHID *pHiddenSel = NULL;
261 int rc;
262 if (fFlags & (DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER))
263 {
264 if (fFlags & DBGF_DISAS_FLAGS_CURRENT_GUEST)
265 pCtxCore = CPUMGetGuestCtxCore(pVM);
266 else
267 pCtxCore = CPUMGetHyperCtxCore(pVM);
268 Sel = pCtxCore->cs;
269 pHiddenSel = (CPUMSELREGHID *)&pCtxCore->csHid;
270 GCPtr = pCtxCore->eip;
271 }
272
273 /*
274 * Read the selector info - assume no stale selectors and nasty stuff like that.
275 * Since the selector flags in the CPUMCTX structures aren't up to date unless
276 * we recently visited REM, we'll not search for the selector there.
277 */
278 SELMSELINFO SelInfo;
279 const PGMMODE enmMode = PGMGetGuestMode(pVM);
280 bool fRealModeAddress = false;
281
282 if ( pHiddenSel
283 && CPUMAreHiddenSelRegsValid(pVM))
284 {
285 SelInfo.GCPtrBase = pHiddenSel->u32Base;
286 SelInfo.cbLimit = pHiddenSel->u32Limit;
287 SelInfo.fHyper = false;
288 SelInfo.fRealMode = !!((pCtxCore && pCtxCore->eflags.Bits.u1VM) || enmMode == PGMMODE_REAL);
289 SelInfo.Raw.au32[0] = 0;
290 SelInfo.Raw.au32[1] = 0;
291 SelInfo.Raw.Gen.u16LimitLow = ~0;
292 SelInfo.Raw.Gen.u4LimitHigh = ~0;
293 SelInfo.Raw.Gen.u1Present = pHiddenSel->Attr.n.u1Present;
294 SelInfo.Raw.Gen.u1Granularity = pHiddenSel->Attr.n.u1Granularity;;
295 SelInfo.Raw.Gen.u1DefBig = pHiddenSel->Attr.n.u1DefBig;
296 SelInfo.Raw.Gen.u1DescType = pHiddenSel->Attr.n.u1DescType;
297 SelInfo.Raw.Gen.u4Type = pHiddenSel->Attr.n.u4Type;
298 fRealModeAddress = SelInfo.fRealMode;
299 }
300 else if (Sel == DBGF_SEL_FLAT)
301 {
302 SelInfo.GCPtrBase = 0;
303 SelInfo.cbLimit = ~0;
304 SelInfo.fHyper = false;
305 SelInfo.fRealMode = false;
306 SelInfo.Raw.au32[0] = 0;
307 SelInfo.Raw.au32[1] = 0;
308 SelInfo.Raw.Gen.u16LimitLow = ~0;
309 SelInfo.Raw.Gen.u4LimitHigh = ~0;
310 SelInfo.Raw.Gen.u1Present = 1;
311 SelInfo.Raw.Gen.u1Granularity = 1;
312 SelInfo.Raw.Gen.u1DefBig = 1;
313 SelInfo.Raw.Gen.u1DescType = 1;
314 SelInfo.Raw.Gen.u4Type = X86_SEL_TYPE_EO;
315 }
316 else if ( !(fFlags & DBGF_DISAS_FLAGS_CURRENT_HYPER)
317 && ( (pCtxCore && pCtxCore->eflags.Bits.u1VM)
318 || enmMode == PGMMODE_REAL) )
319 { /* V86 mode or real mode - real mode addressing */
320 SelInfo.GCPtrBase = Sel * 16;
321 SelInfo.cbLimit = ~0;
322 SelInfo.fHyper = false;
323 SelInfo.fRealMode = true;
324 SelInfo.Raw.au32[0] = 0;
325 SelInfo.Raw.au32[1] = 0;
326 SelInfo.Raw.Gen.u16LimitLow = ~0;
327 SelInfo.Raw.Gen.u4LimitHigh = ~0;
328 SelInfo.Raw.Gen.u1Present = 1;
329 SelInfo.Raw.Gen.u1Granularity = 1;
330 SelInfo.Raw.Gen.u1DefBig = 0; /* 16 bits */
331 SelInfo.Raw.Gen.u1DescType = 1;
332 SelInfo.Raw.Gen.u4Type = X86_SEL_TYPE_EO;
333 fRealModeAddress = true;
334 }
335 else
336 {
337 rc = SELMR3GetSelectorInfo(pVM, Sel, &SelInfo);
338 if (VBOX_FAILURE(rc))
339 {
340 RTStrPrintf(pszOutput, cchOutput, "Sel=%04x -> %Vrc\n", Sel, rc);
341 return rc;
342 }
343 }
344
345 /*
346 * Disassemble it.
347 */
348 DBGFDISASSTATE State;
349 rc = dbgfR3DisasInstrFirst(pVM, &SelInfo, enmMode, GCPtr, &State);
350 if (VBOX_FAILURE(rc))
351 {
352 RTStrPrintf(pszOutput, cchOutput, "Disas -> %Vrc\n", rc);
353 return rc;
354 }
355
356 /*
357 * Format it.
358 */
359 char szBuf[512];
360 char *psz = &szBuf[0];
361
362 /* prefix */
363 if (State.Cpu.prefix & PREFIX_LOCK)
364 psz = (char *)memcpy(psz, "lock ", sizeof("lock ")) + sizeof("lock ") - 1;
365 if (State.Cpu.prefix & PREFIX_REP)
366 psz = (char *)memcpy(psz, "rep(e) ", sizeof("rep(e) ")) + sizeof("rep(e) ") - 1;
367 else if(State.Cpu.prefix & PREFIX_REPNE)
368 psz = (char *)memcpy(psz, "repne ", sizeof("repne ")) + sizeof("repne ") - 1;
369
370 /* the instruction */
371 const char *pszFormat = State.Cpu.pszOpcode;
372 char ch;
373 while ((ch = *pszFormat) && !isspace(ch) && ch != '%')
374 {
375 *psz++ = ch;
376 pszFormat++;
377 }
378 if (isspace(ch))
379 {
380 do *psz++ = ' ';
381#ifdef DEBUG_bird /* Not sure if Sander want's this because of log size */
382 while (psz - szBuf < 8);
383#else
384 while (0);
385#endif
386 while (isspace(*pszFormat))
387 pszFormat++;
388 }
389
390 if (fFlags & DBGF_DISAS_FLAGS_NO_ANNOTATION)
391 pCtxCore = NULL;
392
393 /** @todo implement annotation and symbol lookup! */
394 int iParam = 1;
395 for (;;)
396 {
397 ch = *pszFormat;
398 if (ch == '%')
399 {
400 ch = pszFormat[1];
401 switch (ch)
402 {
403 /*
404 * Relative jump offset.
405 */
406 case 'J':
407 {
408 AssertMsg(iParam == 1, ("Invalid branch parameter nr %d\n", iParam));
409 int32_t i32Disp;
410 if (State.Cpu.param1.flags & USE_IMMEDIATE8_REL)
411 i32Disp = (int32_t)(int8_t)State.Cpu.param1.parval;
412 else if (State.Cpu.param1.flags & USE_IMMEDIATE16_REL)
413 i32Disp = (int32_t)(int16_t)State.Cpu.param1.parval;
414 else if (State.Cpu.param1.flags & USE_IMMEDIATE32_REL)
415 i32Disp = (int32_t)State.Cpu.param1.parval;
416 else
417 {
418 AssertMsgFailed(("Oops!\n"));
419 dbgfR3DisasInstrDone(&State);
420 return VERR_GENERAL_FAILURE;
421 }
422 RTGCUINTPTR GCPtrTarget = (RTGCUINTPTR)GCPtr + State.Cpu.opsize + i32Disp;
423 switch (State.Cpu.opmode)
424 {
425 case CPUMODE_16BIT: GCPtrTarget &= UINT16_MAX; break;
426 case CPUMODE_32BIT: GCPtrTarget &= UINT32_MAX; break;
427 default: break;
428 }
429#ifdef DEBUG_bird /* an experiment. */
430 DBGFSYMBOL Sym;
431 RTGCINTPTR off;
432 int rc = DBGFR3SymbolByAddr(pVM, GCPtrTarget + SelInfo.GCPtrBase, &off, &Sym);
433 if ( VBOX_SUCCESS(rc)
434 && Sym.Value - SelInfo.GCPtrBase <= SelInfo.cbLimit
435 && off < _1M * 16 && off > -_1M * 16)
436 {
437 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz, "%s", Sym.szName);
438 if (off > 0)
439 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz, "+%#x", (int)off);
440 else if (off > 0)
441 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz, "-%#x", -(int)off);
442 switch (State.Cpu.opmode)
443 {
444 case CPUMODE_16BIT:
445 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz,
446 i32Disp >= 0 ? " (%04VGv/+%x)" : " (%04VGv/-%x)",
447 GCPtrTarget, i32Disp >= 0 ? i32Disp : -i32Disp);
448 break;
449 case CPUMODE_32BIT:
450 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz,
451 i32Disp >= 0 ? " (%08VGv/+%x)" : " (%08VGv/-%x)",
452 GCPtrTarget, i32Disp >= 0 ? i32Disp : -i32Disp);
453 break;
454 default:
455 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz,
456 i32Disp >= 0 ? " (%VGv/+%x)" : " (%VGv/-%x)",
457 GCPtrTarget, i32Disp >= 0 ? i32Disp : -i32Disp);
458 break;
459 }
460 }
461 else
462#endif /* DEBUG_bird */
463 {
464 switch (State.Cpu.opmode)
465 {
466 case CPUMODE_16BIT:
467 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz,
468 i32Disp >= 0 ? "%04VGv (+%x)" : "%04VGv (-%x)",
469 GCPtrTarget, i32Disp >= 0 ? i32Disp : -i32Disp);
470 break;
471 case CPUMODE_32BIT:
472 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz,
473 i32Disp >= 0 ? "%08VGv (+%x)" : "%08VGv (-%x)",
474 GCPtrTarget, i32Disp >= 0 ? i32Disp : -i32Disp);
475 break;
476 default:
477 psz += RTStrPrintf(psz, &szBuf[sizeof(szBuf)] - psz,
478 i32Disp >= 0 ? "%VGv (+%x)" : "%VGv (-%x)",
479 GCPtrTarget, i32Disp >= 0 ? i32Disp : -i32Disp);
480 break;
481 }
482 }
483 break;
484 }
485
486 case 'A': //direct address
487 case 'C': //control register
488 case 'D': //debug register
489 case 'E': //ModRM specifies parameter
490 case 'F': //Eflags register
491 case 'G': //ModRM selects general register
492 case 'I': //Immediate data
493 case 'M': //ModRM may only refer to memory
494 case 'O': //No ModRM byte
495 case 'P': //ModRM byte selects MMX register
496 case 'Q': //ModRM byte selects MMX register or memory address
497 case 'R': //ModRM byte may only refer to a general register
498 case 'S': //ModRM byte selects a segment register
499 case 'T': //ModRM byte selects a test register
500 case 'V': //ModRM byte selects an XMM/SSE register
501 case 'W': //ModRM byte selects an XMM/SSE register or a memory address
502 case 'X': //DS:SI
503 case 'Y': //ES:DI
504 switch (iParam)
505 {
506 case 1: psz = mystrpcpy(psz, State.Cpu.param1.szParam); break;
507 case 2: psz = mystrpcpy(psz, State.Cpu.param2.szParam); break;
508 case 3: psz = mystrpcpy(psz, State.Cpu.param3.szParam); break;
509 }
510 pszFormat += 2;
511 break;
512
513 case 'e': //register based on operand size (e.g. %eAX)
514 if (State.Cpu.opmode == CPUMODE_32BIT)
515 *psz++ = 'E';
516 *psz++ = pszFormat[2];
517 *psz++ = pszFormat[3];
518 pszFormat += 4;
519 break;
520
521 default:
522 AssertMsgFailed(("Oops! ch=%c\n", ch));
523 break;
524 }
525
526 /* Skip to the next parameter in the format string. */
527 pszFormat = strchr(pszFormat, ',');
528 if (!pszFormat)
529 break;
530 pszFormat++;
531 *psz++ = ch = ',';
532 iParam++;
533 }
534 else
535 {
536 /* output char, but check for parameter separator first. */
537 if (ch == ',')
538 iParam++;
539 *psz++ = ch;
540 if (!ch)
541 break;
542 pszFormat++;
543 }
544
545#ifdef DEBUG_bird /* Not sure if Sander want's this because of log size */
546 /* space after commas */
547 if (ch == ',')
548 {
549 while (isspace(*pszFormat))
550 pszFormat++;
551 *psz++ = ' ';
552 }
553#endif
554 } /* foreach char in pszFormat */
555 *psz = '\0';
556
557 /*
558 * Print it to the user specified buffer.
559 */
560 if (fFlags & DBGF_DISAS_FLAGS_NO_BYTES)
561 {
562 if (fFlags & DBGF_DISAS_FLAGS_NO_ADDRESS)
563 RTStrPrintf(pszOutput, cchOutput, "%s", szBuf);
564 else if (fRealModeAddress)
565 RTStrPrintf(pszOutput, cchOutput, "%04x:%04x %s", Sel, (unsigned)GCPtr, szBuf);
566 else if (Sel == DBGF_SEL_FLAT)
567 RTStrPrintf(pszOutput, cchOutput, "%VGv %s", GCPtr, szBuf);
568 else
569 RTStrPrintf(pszOutput, cchOutput, "%04x:%VGv %s", Sel, GCPtr, szBuf);
570 }
571 else
572 {
573 uint32_t cbBits = State.Cpu.opsize;
574 uint8_t *pau8Bits = (uint8_t *)alloca(cbBits);
575 rc = dbgfR3DisasInstrRead(GCPtr, pau8Bits, cbBits, &State);
576 AssertRC(rc);
577 if (fFlags & DBGF_DISAS_FLAGS_NO_ADDRESS)
578 RTStrPrintf(pszOutput, cchOutput, "%.*Vhxs%*s %s",
579 cbBits, pau8Bits, cbBits < 8 ? (8 - cbBits) * 3 : 0, "",
580 szBuf);
581 else if (fRealModeAddress)
582 RTStrPrintf(pszOutput, cchOutput, "%04x:%04x %.*Vhxs%*s %s",
583 Sel, (unsigned)GCPtr,
584 cbBits, pau8Bits, cbBits < 8 ? (8 - cbBits) * 3 : 0, "",
585 szBuf);
586 else if (Sel == DBGF_SEL_FLAT)
587 RTStrPrintf(pszOutput, cchOutput, "%VGv %.*Vhxs%*s %s",
588 GCPtr,
589 cbBits, pau8Bits, cbBits < 8 ? (8 - cbBits) * 3 : 0, "",
590 szBuf);
591 else
592 RTStrPrintf(pszOutput, cchOutput, "%04x:%VGv %.*Vhxs%*s %s",
593 Sel, GCPtr,
594 cbBits, pau8Bits, cbBits < 8 ? (8 - cbBits) * 3 : 0, "",
595 szBuf);
596
597 }
598
599 if (pcbInstr)
600 *pcbInstr = State.Cpu.opsize;
601
602 dbgfR3DisasInstrDone(&State);
603 return VINF_SUCCESS;
604}
605
606
607/**
608 * Disassembles an instruction.
609 * Addresses will be tried resolved to symbols
610 *
611 * @returns VBox status code.
612 * @param pVM VM handle.
613 * @param Sel The code selector. This used to determin the 32/16 bit ness and
614 * calculation of the actual instruction address.
615 * @param GCPtr The code address relative to the base of Sel.
616 * @param pszOutput Output buffer.
617 * @param cchOutput Size of the output buffer.
618 */
619DBGFR3DECL(int) DBGFR3DisasInstr(PVM pVM, RTSEL Sel, RTGCPTR GCPtr, char *pszOutput, uint32_t cchOutput)
620{
621 return DBGFR3DisasInstrEx(pVM, Sel, GCPtr, 0, pszOutput, cchOutput, NULL);
622}
623
624
625/**
626 * Disassembles the current guest context instruction.
627 * All registers and data will be displayed. Addresses will be attempted resolved to symbols.
628 *
629 * @returns VBox status code.
630 * @param pVM VM handle.
631 * @param pszOutput Output buffer.
632 * @param cchOutput Size of the output buffer.
633 */
634DBGFR3DECL(int) DBGFR3DisasInstrCurrent(PVM pVM, char *pszOutput, uint32_t cchOutput)
635{
636 return DBGFR3DisasInstrEx(pVM, 0, 0, DBGF_DISAS_FLAGS_CURRENT_GUEST, pszOutput, cchOutput, NULL);
637}
638
639
640/**
641 * Disassembles the current guest context instruction and writes it to the log.
642 * All registers and data will be displayed. Addresses will be attempted resolved to symbols.
643 *
644 * @returns VBox status code.
645 * @param pVM VM handle.
646 * @param pszPrefix Short prefix string to the dissassembly string. (optional)
647 */
648DBGFR3DECL(int) DBGFR3DisasInstrCurrentLogInternal(PVM pVM, const char *pszPrefix)
649{
650 char szBuf[256];
651 szBuf[0] = '\0';
652 int rc = DBGFR3DisasInstrCurrent(pVM, &szBuf[0], sizeof(szBuf));
653 if (VBOX_FAILURE(rc))
654 RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrCurrentLog failed with rc=%Vrc\n", rc);
655 if (pszPrefix && *pszPrefix)
656 RTLogPrintf("%s: %s\n", pszPrefix, szBuf);
657 else
658 RTLogPrintf("%s\n", szBuf);
659 return rc;
660}
661
662
663
664/**
665 * Disassembles the specified guest context instruction and writes it to the log.
666 * Addresses will be attempted resolved to symbols.
667 *
668 * @returns VBox status code.
669 * @param pVM VM handle.
670 * @param Sel The code selector. This used to determin the 32/16 bit-ness and
671 * calculation of the actual instruction address.
672 * @param GCPtr The code address relative to the base of Sel.
673 */
674DBGFR3DECL(int) DBGFR3DisasInstrLogInternal(PVM pVM, RTSEL Sel, RTGCPTR GCPtr)
675{
676 char szBuf[256];
677 szBuf[0] = '\0';
678 int rc = DBGFR3DisasInstr(pVM, Sel, GCPtr, &szBuf[0], sizeof(szBuf));
679 if (VBOX_FAILURE(rc))
680 RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrLog(, %RTsel, %RGv) failed with rc=%Vrc\n", Sel, GCPtr, rc);
681 RTLogPrintf("%s\n", szBuf);
682 return rc;
683}
684
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