1 | /* $Id: Disasm.cpp 41676 2012-06-12 20:53:40Z 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 | */
|
---|
46 | DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISCPUSTATE pCpu, uint32_t *pcbInstr,
|
---|
47 | char *pszOutput, size_t cbOutput)
|
---|
48 | {
|
---|
49 | return DISInstrToStrEx((uintptr_t)pvInstr, enmCpuMode, NULL, NULL, DISOPTYPE_ALL,
|
---|
50 | pCpu, pcbInstr, pszOutput, cbOutput);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Disassembles one instruction with a byte fetcher caller.
|
---|
55 | *
|
---|
56 | * @returns VBox error code
|
---|
57 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
58 | * @param enmCpuMode The CPU mode.
|
---|
59 | * @param pfnCallback The byte fetcher callback.
|
---|
60 | * @param pvUser The user argument (found in
|
---|
61 | * DISCPUSTATE::apvUserData[0]).
|
---|
62 | * @param pCpu Where to return the disassembled instruction.
|
---|
63 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
64 | * allowed.
|
---|
65 | * @param pszOutput Storage for disassembled instruction.
|
---|
66 | * @param cbOutput Size of the output buffer.
|
---|
67 | *
|
---|
68 | * @todo Define output callback.
|
---|
69 | */
|
---|
70 | DISDECL(int) DISInstrToStrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
71 | PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
72 |
|
---|
73 | {
|
---|
74 | return DISInstrToStrEx(uInstrAddr, enmCpuMode, pfnReadBytes, pvUser, DISOPTYPE_ALL,
|
---|
75 | pCpu, pcbInstr, pszOutput, cbOutput);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
|
---|
80 | *
|
---|
81 | * @returns VBox error code
|
---|
82 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
83 | * @param enmCpuMode The CPU mode.
|
---|
84 | * @param pfnCallback The byte fetcher callback.
|
---|
85 | * @param uFilter Instruction filter.
|
---|
86 | * @param pCpu Where to return the disassembled instruction.
|
---|
87 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
88 | * allowed.
|
---|
89 | * @param pszOutput Storage for disassembled instruction.
|
---|
90 | * @param cbOutput Size of the output buffer.
|
---|
91 | *
|
---|
92 | * @todo Define output callback.
|
---|
93 | */
|
---|
94 | DISDECL(int) DISInstrToStrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode,
|
---|
95 | PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
|
---|
96 | PDISCPUSTATE pCpu, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
97 | {
|
---|
98 | int rc = DISInstEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pCpu, pcbInstr);
|
---|
99 | if (RT_SUCCESS(rc) && pszOutput && cbOutput)
|
---|
100 | {
|
---|
101 | rc = DISFormatYasmEx(pCpu, pszOutput, cbOutput,
|
---|
102 | DIS_FMT_FLAGS_BYTES_LEFT | DIS_FMT_FLAGS_BYTES_BRACKETS | DIS_FMT_FLAGS_BYTES_SPACED
|
---|
103 | | DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_ADDR_LEFT,
|
---|
104 | NULL /*pfnGetSymbol*/, NULL /*pvUser*/);
|
---|
105 | size_t cch = strlen(pszOutput);
|
---|
106 | if (cch < cbOutput)
|
---|
107 | {
|
---|
108 | pszOutput[cch++] = '\n';
|
---|
109 | pszOutput[cch] = '\0';
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 |
|
---|