1 | /* $Id: Disasm.cpp 99220 2023-03-30 12:40:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox disassembler - Disassemble and optionally format.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_DIS
|
---|
33 | #include <VBox/dis.h>
|
---|
34 | #include <iprt/errcore.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include "DisasmInternal.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Disassembles one instruction
|
---|
42 | *
|
---|
43 | * @returns VBox error code
|
---|
44 | * @param pvInstr Pointer to the instruction to disassemble.
|
---|
45 | * @param enmCpuMode The CPU state.
|
---|
46 | * @param pDis The disassembler state (output).
|
---|
47 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
48 | * allowed.
|
---|
49 | * @param pszOutput Storage for disassembled instruction
|
---|
50 | * @param cbOutput Size of the output buffer.
|
---|
51 | *
|
---|
52 | * @todo Define output callback.
|
---|
53 | */
|
---|
54 | DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISSTATE pDis, uint32_t *pcbInstr,
|
---|
55 | char *pszOutput, size_t cbOutput)
|
---|
56 | {
|
---|
57 | return DISInstrToStrEx((uintptr_t)pvInstr, enmCpuMode, NULL, NULL, DISOPTYPE_ALL,
|
---|
58 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Disassembles one instruction with a byte fetcher caller.
|
---|
63 | *
|
---|
64 | * @returns VBox error code
|
---|
65 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
66 | * @param enmCpuMode The CPU mode.
|
---|
67 | * @param pfnCallback The byte fetcher callback.
|
---|
68 | * @param pvUser The user argument (found in
|
---|
69 | * DISSTATE::pvUser).
|
---|
70 | * @param pDis The disassembler state (output).
|
---|
71 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
72 | * allowed.
|
---|
73 | * @param pszOutput Storage for disassembled instruction.
|
---|
74 | * @param cbOutput Size of the output buffer.
|
---|
75 | *
|
---|
76 | * @todo Define output callback.
|
---|
77 | */
|
---|
78 | DISDECL(int) DISInstrToStrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
79 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
80 |
|
---|
81 | {
|
---|
82 | return DISInstrToStrEx(uInstrAddr, enmCpuMode, pfnReadBytes, pvUser, DISOPTYPE_ALL,
|
---|
83 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
|
---|
88 | *
|
---|
89 | * @returns VBox error code
|
---|
90 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
91 | * @param enmCpuMode The CPU mode.
|
---|
92 | * @param pfnCallback The byte fetcher callback.
|
---|
93 | * @param uFilter Instruction filter.
|
---|
94 | * @param pDis Where to return the disassembled instruction info.
|
---|
95 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
96 | * allowed.
|
---|
97 | * @param pszOutput Storage for disassembled instruction.
|
---|
98 | * @param cbOutput Size of the output buffer.
|
---|
99 | *
|
---|
100 | * @todo Define output callback.
|
---|
101 | */
|
---|
102 | DISDECL(int) DISInstrToStrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode,
|
---|
103 | PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
|
---|
104 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
105 | {
|
---|
106 | /* Don't filter if formatting is desired. */
|
---|
107 | if (uFilter != DISOPTYPE_ALL && pszOutput && cbOutput)
|
---|
108 | uFilter = DISOPTYPE_ALL;
|
---|
109 |
|
---|
110 | int rc = DISInstrEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pDis, pcbInstr);
|
---|
111 | if (RT_SUCCESS(rc) && pszOutput && cbOutput)
|
---|
112 | {
|
---|
113 | size_t cch = DISFormatYasmEx(pDis, pszOutput, cbOutput,
|
---|
114 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
115 | | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
|
---|
116 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
117 | if (cch + 2 <= cbOutput)
|
---|
118 | {
|
---|
119 | pszOutput[cch++] = '\n';
|
---|
120 | pszOutput[cch] = '\0';
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|