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