1 | /* $Id: Disasm.cpp 98103 2023-01-17 14:15: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 <VBox/disopcode.h>
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include "DisasmInternal.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Disassembles one instruction
|
---|
43 | *
|
---|
44 | * @returns VBox error code
|
---|
45 | * @param pvInstr Pointer to the instruction to disassemble.
|
---|
46 | * @param enmCpuMode The CPU state.
|
---|
47 | * @param pDis The disassembler state (output).
|
---|
48 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
49 | * allowed.
|
---|
50 | * @param pszOutput Storage for disassembled instruction
|
---|
51 | * @param cbOutput Size of the output buffer.
|
---|
52 | *
|
---|
53 | * @todo Define output callback.
|
---|
54 | */
|
---|
55 | DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISSTATE pDis, uint32_t *pcbInstr,
|
---|
56 | char *pszOutput, size_t cbOutput)
|
---|
57 | {
|
---|
58 | return DISInstrToStrEx((uintptr_t)pvInstr, enmCpuMode, NULL, NULL, DISOPTYPE_ALL,
|
---|
59 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Disassembles one instruction with a byte fetcher caller.
|
---|
64 | *
|
---|
65 | * @returns VBox error code
|
---|
66 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
67 | * @param enmCpuMode The CPU mode.
|
---|
68 | * @param pfnCallback The byte fetcher callback.
|
---|
69 | * @param pvUser The user argument (found in
|
---|
70 | * DISSTATE::pvUser).
|
---|
71 | * @param pDis The disassembler state (output).
|
---|
72 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
73 | * allowed.
|
---|
74 | * @param pszOutput Storage for disassembled instruction.
|
---|
75 | * @param cbOutput Size of the output buffer.
|
---|
76 | *
|
---|
77 | * @todo Define output callback.
|
---|
78 | */
|
---|
79 | DISDECL(int) DISInstrToStrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
80 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
81 |
|
---|
82 | {
|
---|
83 | return DISInstrToStrEx(uInstrAddr, enmCpuMode, pfnReadBytes, pvUser, DISOPTYPE_ALL,
|
---|
84 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
|
---|
89 | *
|
---|
90 | * @returns VBox error code
|
---|
91 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
92 | * @param enmCpuMode The CPU mode.
|
---|
93 | * @param pfnCallback The byte fetcher callback.
|
---|
94 | * @param uFilter Instruction filter.
|
---|
95 | * @param pDis Where to return the disassembled instruction info.
|
---|
96 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
97 | * allowed.
|
---|
98 | * @param pszOutput Storage for disassembled instruction.
|
---|
99 | * @param cbOutput Size of the output buffer.
|
---|
100 | *
|
---|
101 | * @todo Define output callback.
|
---|
102 | */
|
---|
103 | DISDECL(int) DISInstrToStrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode,
|
---|
104 | PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
|
---|
105 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
106 | {
|
---|
107 | /* Don't filter if formatting is desired. */
|
---|
108 | if (uFilter != DISOPTYPE_ALL && pszOutput && cbOutput)
|
---|
109 | uFilter = DISOPTYPE_ALL;
|
---|
110 |
|
---|
111 | int rc = DISInstrEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pDis, pcbInstr);
|
---|
112 | if (RT_SUCCESS(rc) && pszOutput && cbOutput)
|
---|
113 | {
|
---|
114 | size_t cch = DISFormatYasmEx(pDis, pszOutput, cbOutput,
|
---|
115 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
116 | | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
|
---|
117 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
118 | if (cch + 2 <= cbOutput)
|
---|
119 | {
|
---|
120 | pszOutput[cch++] = '\n';
|
---|
121 | pszOutput[cch] = '\0';
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|