VirtualBox

source: vbox/trunk/src/VBox/Disassembler/Disasm.cpp@ 41669

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

DISInstr -> DISInstrToStr.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: Disasm.cpp 41669 2012-06-12 13:34:07Z vboxsync $ */
2/** @file
3 * VBox disassembler - Disassemble and optionally format.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DIS
23#include <VBox/dis.h>
24#include <VBox/disopcode.h>
25#include <VBox/err.h>
26#include <iprt/assert.h>
27#include <iprt/string.h>
28#include "DisasmInternal.h"
29#include "DisasmTables.h"
30
31
32/**
33 * Disassembles one instruction
34 *
35 * @returns VBox error code
36 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
37 * set correctly.
38 * @param pvInstr Pointer to the instruction to disassemble.
39 * @param pcbInstr Where to store the size of the instruction. NULL is
40 * allowed.
41 * @param pszOutput Storage for disassembled instruction
42 * @param cbOutput Size of the output buffer.
43 *
44 * @todo Define output callback.
45 */
46DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISCPUSTATE pCpu, uint32_t *pcbInstr,
47 char *pszOutput, size_t cbOutput)
48{
49 return DISInstrEx((uintptr_t)pvInstr, 0, enmCpuMode, NULL, NULL, OPTYPE_ALL,
50 pCpu, pcbInstr, pszOutput);
51}
52
53/**
54 * Disassembles one instruction
55 *
56 * @returns VBox error code
57 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
58 * set correctly.
59 * @param uInstrAddr Pointer to the instruction to disassemble.
60 * @param offRealAddr Offset to add to instruction address to get the real
61 * virtual address.
62 * @param pcbInstr Where to store the size of the instruction. NULL is
63 * allowed.
64 * @param pszOutput Storage for disassembled instruction
65 *
66 * @todo Define output callback.
67 */
68DISDECL(int) DISInstrWithOff(PDISCPUSTATE pCpu, RTUINTPTR uInstrAddr, RTUINTPTR offRealAddr,
69 uint32_t *pcbInstr, char *pszOutput)
70{
71 return DISInstrEx(uInstrAddr, offRealAddr, pCpu->mode, pCpu->pfnReadBytes, pCpu->apvUserData[0], OPTYPE_ALL,
72 pCpu, pcbInstr, pszOutput);
73}
74
75/**
76 * Disassembles one instruction with a byte fetcher caller.
77 *
78 * @returns VBox error code
79 * @param uInstrAddr Pointer to the structure to disassemble.
80 * @param enmCpuMode The CPU mode.
81 * @param pfnCallback The byte fetcher callback.
82 * @param pvUser The user argument (found in
83 * DISCPUSTATE::apvUserData[0]).
84 * @param pCpu Where to return the disassembled instruction.
85 * @param pcbInstr Where to store the size of the instruction. NULL is
86 * allowed.
87 * @param pszOutput Storage for disassembled instruction
88 *
89 * @todo Define output callback.
90 */
91DISDECL(int) DISInstrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
92 PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput)
93
94{
95 return DISInstrEx(uInstrAddr, 0, enmCpuMode, pfnReadBytes, pvUser, OPTYPE_ALL,
96 pCpu, pcbInstr, pszOutput);
97}
98
99/**
100 * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
101 *
102 * @returns VBox error code
103 * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
104 * set correctly.
105 * @param uInstrAddr Pointer to the structure to disassemble.
106 * @param u32EipOffset Offset to add to instruction address to get the real virtual address
107 * @param pcbInstr Where to store the size of the instruction. NULL is
108 * allowed.
109 * @param pszOutput Storage for disassembled instruction
110 * @param uFilter Instruction type filter.
111 *
112 * @todo Define output callback.
113 */
114DISDECL(int) DISInstrEx(RTUINTPTR uInstrAddr, RTUINTPTR offRealAddr, DISCPUMODE enmCpuMode,
115 PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
116 PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput)
117{
118 int rc = DISCoreOneExEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pCpu, pcbInstr);
119 if (RT_SUCCESS(rc) && pszOutput)
120 {
121 size_t cbOutput = 128;
122 size_t cch;
123#if 0
124 RTUINTPTR uRealAddr = uInstrAddr + offRealAddr;
125 if (pCpu->mode == CPUMODE_64BIT || uRealAddr > UINT32_MAX)
126 cch = RTStrPrintf(pszOutput, cbOutput, "%016RTptr: ", uRealAddr);
127 else
128 cch = RTStrPrintf(pszOutput, cbOutput, "%08RX32: ", (uint32_t)uRealAddr);
129 rc = DISFormatYasmEx(pCpu, pszOutput + cch, cbOutput - cch,
130 DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
131 | DIS_FMT_FLAGS_RELATIVE_BRANCH,
132 NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
133#else
134 pCpu->uInstrAddr += offRealAddr;
135 rc = DISFormatYasmEx(pCpu, pszOutput, cbOutput,
136 DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
137 | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
138 NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
139 pCpu->uInstrAddr = uInstrAddr;
140#endif
141 cch = strlen(pszOutput);
142 if (cch < cbOutput)
143 {
144 pszOutput[cch++] = '\n';
145 pszOutput[cch] = '\0';
146 }
147 }
148 return rc;
149}
150
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