[41668] | 1 | /* $Id: Disasm.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[41668] | 3 | * VBox disassembler - Disassemble and optionally format.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[76553] | 7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
[57358] | 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[41668] | 22 | #define LOG_GROUP LOG_GROUP_DIS
|
---|
[1] | 23 | #include <VBox/dis.h>
|
---|
| 24 | #include <VBox/disopcode.h>
|
---|
[76455] | 25 | #include <iprt/errcore.h>
|
---|
[1] | 26 | #include <iprt/assert.h>
|
---|
| 27 | #include <iprt/string.h>
|
---|
| 28 | #include "DisasmInternal.h"
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
[41658] | 32 | * Disassembles one instruction
|
---|
[1] | 33 | *
|
---|
[4953] | 34 | * @returns VBox error code
|
---|
[41669] | 35 | * @param pvInstr Pointer to the instruction to disassemble.
|
---|
[41789] | 36 | * @param enmCpuMode The CPU state.
|
---|
| 37 | * @param pDis The disassembler state (output).
|
---|
[41668] | 38 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
| 39 | * allowed.
|
---|
[41658] | 40 | * @param pszOutput Storage for disassembled instruction
|
---|
[41669] | 41 | * @param cbOutput Size of the output buffer.
|
---|
[1] | 42 | *
|
---|
| 43 | * @todo Define output callback.
|
---|
| 44 | */
|
---|
[41790] | 45 | DISDECL(int) DISInstrToStr(void const *pvInstr, DISCPUMODE enmCpuMode, PDISSTATE pDis, uint32_t *pcbInstr,
|
---|
[41669] | 46 | char *pszOutput, size_t cbOutput)
|
---|
[1] | 47 | {
|
---|
[41676] | 48 | return DISInstrToStrEx((uintptr_t)pvInstr, enmCpuMode, NULL, NULL, DISOPTYPE_ALL,
|
---|
[41789] | 49 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
[1] | 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
[41658] | 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
|
---|
[41790] | 60 | * DISSTATE::pvUser).
|
---|
[41789] | 61 | * @param pDis The disassembler state (output).
|
---|
[41668] | 62 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
| 63 | * allowed.
|
---|
[41671] | 64 | * @param pszOutput Storage for disassembled instruction.
|
---|
| 65 | * @param cbOutput Size of the output buffer.
|
---|
[41658] | 66 | *
|
---|
| 67 | * @todo Define output callback.
|
---|
| 68 | */
|
---|
[41671] | 69 | DISDECL(int) DISInstrToStrWithReader(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode, PFNDISREADBYTES pfnReadBytes, void *pvUser,
|
---|
[41790] | 70 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
[41658] | 71 |
|
---|
| 72 | {
|
---|
[41676] | 73 | return DISInstrToStrEx(uInstrAddr, enmCpuMode, pfnReadBytes, pvUser, DISOPTYPE_ALL,
|
---|
[41789] | 74 | pDis, pcbInstr, pszOutput, cbOutput);
|
---|
[41658] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
[1] | 78 | * Disassembles one instruction; only fully disassembly an instruction if it matches the filter criteria
|
---|
| 79 | *
|
---|
[4953] | 80 | * @returns VBox error code
|
---|
[41658] | 81 | * @param uInstrAddr Pointer to the structure to disassemble.
|
---|
[41671] | 82 | * @param enmCpuMode The CPU mode.
|
---|
| 83 | * @param pfnCallback The byte fetcher callback.
|
---|
| 84 | * @param uFilter Instruction filter.
|
---|
[41789] | 85 | * @param pDis Where to return the disassembled instruction info.
|
---|
[41668] | 86 | * @param pcbInstr Where to store the size of the instruction. NULL is
|
---|
| 87 | * allowed.
|
---|
[41671] | 88 | * @param pszOutput Storage for disassembled instruction.
|
---|
| 89 | * @param cbOutput Size of the output buffer.
|
---|
[1] | 90 | *
|
---|
| 91 | * @todo Define output callback.
|
---|
| 92 | */
|
---|
[41671] | 93 | DISDECL(int) DISInstrToStrEx(RTUINTPTR uInstrAddr, DISCPUMODE enmCpuMode,
|
---|
| 94 | PFNDISREADBYTES pfnReadBytes, void *pvUser, uint32_t uFilter,
|
---|
[41790] | 95 | PDISSTATE pDis, uint32_t *pcbInstr, char *pszOutput, size_t cbOutput)
|
---|
[1] | 96 | {
|
---|
[41886] | 97 | /* Don't filter if formatting is desired. */
|
---|
| 98 | if (uFilter != DISOPTYPE_ALL && pszOutput && cbOutput)
|
---|
[42284] | 99 | uFilter = DISOPTYPE_ALL;
|
---|
[41886] | 100 |
|
---|
| 101 | int rc = DISInstrEx(uInstrAddr, enmCpuMode, uFilter, pfnReadBytes, pvUser, pDis, pcbInstr);
|
---|
[41671] | 102 | if (RT_SUCCESS(rc) && pszOutput && cbOutput)
|
---|
[8361] | 103 | {
|
---|
[41789] | 104 | size_t cch = DISFormatYasmEx(pDis, pszOutput, cbOutput,
|
---|
[41694] | 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*/);
|
---|
[41871] | 108 | if (cch + 2 <= cbOutput)
|
---|
[1] | 109 | {
|
---|
[41668] | 110 | pszOutput[cch++] = '\n';
|
---|
| 111 | pszOutput[cch] = '\0';
|
---|
[41658] | 112 | }
|
---|
[1] | 113 | }
|
---|
[41668] | 114 | return rc;
|
---|
[1] | 115 | }
|
---|
| 116 |
|
---|