1 | /* $Id: Disasm.cpp 41668 2012-06-12 13:15:51Z 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.215389.xyz. 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 uInstrAddr Pointer to the structure 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 | *
|
---|
43 | * @todo Define output callback.
|
---|
44 | */
|
---|
45 | DISDECL(int) DISInstr(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput)
|
---|
46 | {
|
---|
47 | return DISInstrEx(uInstrAddr, 0, enmCpuMode, NULL, NULL, OPTYPE_ALL,
|
---|
48 | pCpu, pcbInstr, pszOutput);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Disassembles one instruction
|
---|
53 | *
|
---|
54 | * @returns VBox error code
|
---|
55 | * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
|
---|
56 | * set correctly.
|
---|
57 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
58 | * @param offRealAddr Offset to add to instruction address to get the real
|
---|
59 | * virtual address.
|
---|
60 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
61 | * allowed.
|
---|
62 | * @param pszOutput Storage for disassembled instruction
|
---|
63 | *
|
---|
64 | * @todo Define output callback.
|
---|
65 | */
|
---|
66 | DISDECL(int) DISInstrWithOff(PDISCPUSTATE pCpu, RTUINTPTR uInstrAddr, RTUINTPTR offRealAddr,
|
---|
67 | uint32_t *pcbInstr, char *pszOutput)
|
---|
68 | {
|
---|
69 | return DISInstrEx(uInstrAddr, offRealAddr, pCpu->mode, pCpu->pfnReadBytes, pCpu->apvUserData[0], OPTYPE_ALL,
|
---|
70 | pCpu, pcbInstr, pszOutput);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Disassembles one instruction with a byte fetcher caller.
|
---|
75 | *
|
---|
76 | * @returns VBox error code
|
---|
77 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
78 | * @param enmCpuMode The CPU mode.
|
---|
79 | * @param pfnCallback The byte fetcher callback.
|
---|
80 | * @param pvUser The user argument (found in
|
---|
81 | * DISCPUSTATE::apvUserData[0]).
|
---|
82 | * @param pCpu Where to return the disassembled instruction.
|
---|
83 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
84 | * allowed.
|
---|
85 | * @param pszOutput Storage for disassembled instruction
|
---|
86 | *
|
---|
87 | * @todo Define output callback.
|
---|
88 | */
|
---|
89 | DISDECL(int) DISInstrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
90 | PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput)
|
---|
91 |
|
---|
92 | {
|
---|
93 | return DISInstrEx(uInstrAddr, 0, enmCpuMode, pfnReadBytes, pvUser, OPTYPE_ALL,
|
---|
94 | pCpu, pcbInstr, pszOutput);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
|
---|
99 | *
|
---|
100 | * @returns VBox error code
|
---|
101 | * @param pCpu Pointer to cpu structure which have DISCPUSTATE::mode
|
---|
102 | * set correctly.
|
---|
103 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
104 | * @param u32EipOffset Offset to add to instruction address to get the real virtual address
|
---|
105 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
106 | * allowed.
|
---|
107 | * @param pszOutput Storage for disassembled instruction
|
---|
108 | * @param uFilter Instruction type filter.
|
---|
109 | *
|
---|
110 | * @todo Define output callback.
|
---|
111 | */
|
---|
112 | DISDECL(int) DISInstrEx(RTUINTPTR uInstrAddr, RTUINTPTR offRealAddr, DISCPUMODE enmCpuMode,
|
---|
113 | PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
|
---|
114 | PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput)
|
---|
115 | {
|
---|
116 | int rc = DISCoreOneExEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pCpu, pcbInstr);
|
---|
117 | if (RT_SUCCESS(rc) && pszOutput)
|
---|
118 | {
|
---|
119 | size_t cbOutput = 128;
|
---|
120 | size_t cch;
|
---|
121 | #if 0
|
---|
122 | RTUINTPTR uRealAddr = uInstrAddr + offRealAddr;
|
---|
123 | if (pCpu->mode == CPUMODE_64BIT || uRealAddr > UINT32_MAX)
|
---|
124 | cch = RTStrPrintf(pszOutput, cbOutput, "%016RTptr: ", uRealAddr);
|
---|
125 | else
|
---|
126 | cch = RTStrPrintf(pszOutput, cbOutput, "%08RX32: ", (uint32_t)uRealAddr);
|
---|
127 | rc = DISFormatYasmEx(pCpu, pszOutput + cch, cbOutput - cch,
|
---|
128 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
129 | | DIS_FMT_FLAGS_RELATIVE_BRANCH,
|
---|
130 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
131 | #else
|
---|
132 | pCpu->uInstrAddr += offRealAddr;
|
---|
133 | rc = DISFormatYasmEx(pCpu, pszOutput, cbOutput,
|
---|
134 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
135 | | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
|
---|
136 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
137 | pCpu->uInstrAddr = uInstrAddr;
|
---|
138 | #endif
|
---|
139 | cch = strlen(pszOutput);
|
---|
140 | if (cch < cbOutput)
|
---|
141 | {
|
---|
142 | pszOutput[cch++] = '\n';
|
---|
143 | pszOutput[cch] = '\0';
|
---|
144 | }
|
---|
145 | }
|
---|
146 | return rc;
|
---|
147 | }
|
---|
148 |
|
---|