1 | /* $Id: DisasmFormatBytes.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Disassembler - Helper for formatting the opcode bytes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 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 | #include "DisasmInternal.h"
|
---|
23 | #include <iprt/string.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Helper function for formatting the opcode bytes.
|
---|
30 | *
|
---|
31 | * @returns The number of output bytes.
|
---|
32 | *
|
---|
33 | * @param pCpu Pointer to the disassembler cpu state.
|
---|
34 | * @param pszDst The output buffer.
|
---|
35 | * @param cchDst The size of the output buffer.
|
---|
36 | * @param fFlags The flags passed to the formatter.
|
---|
37 | */
|
---|
38 | size_t disFormatBytes(PCDISCPUSTATE pCpu, char *pszDst, size_t cchDst, uint32_t fFlags)
|
---|
39 | {
|
---|
40 | /*
|
---|
41 | * Read the bytes first.
|
---|
42 | */
|
---|
43 | uint8_t ab[16];
|
---|
44 | uint32_t cb = pCpu->opsize;
|
---|
45 | Assert(cb <= 16);
|
---|
46 | if (cb > 16)
|
---|
47 | cb = 16;
|
---|
48 |
|
---|
49 | if (pCpu->pfnReadBytes)
|
---|
50 | {
|
---|
51 | int rc = pCpu->pfnReadBytes(pCpu->opaddr, &ab[0], cb, (void *)pCpu);
|
---|
52 | if (RT_FAILURE(rc))
|
---|
53 | {
|
---|
54 | for (uint32_t i = 0; i < cb; i++)
|
---|
55 | {
|
---|
56 | int rc2 = pCpu->pfnReadBytes(pCpu->opaddr + i, &ab[i], 1, (void *)pCpu);
|
---|
57 | if (RT_FAILURE(rc2))
|
---|
58 | ab[i] = 0xcc;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | uint8_t const *pabSrc = (uint8_t const *)(uintptr_t)pCpu->opaddr;
|
---|
65 | for (uint32_t i = 0; i < cb; i++)
|
---|
66 | ab[i] = pabSrc[i];
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Now for the output.
|
---|
71 | */
|
---|
72 | size_t cchOutput = 0;
|
---|
73 | #define PUT_C(ch) \
|
---|
74 | do { \
|
---|
75 | cchOutput++; \
|
---|
76 | if (cchDst > 1) \
|
---|
77 | { \
|
---|
78 | cchDst--; \
|
---|
79 | *pszDst++ = (ch); \
|
---|
80 | } \
|
---|
81 | } while (0)
|
---|
82 | #define PUT_NUM(cch, fmt, num) \
|
---|
83 | do { \
|
---|
84 | cchOutput += (cch); \
|
---|
85 | if (cchDst > 1) \
|
---|
86 | { \
|
---|
87 | const size_t cchTmp = RTStrPrintf(pszDst, cchDst, fmt, (num)); \
|
---|
88 | pszDst += cchTmp; \
|
---|
89 | cchDst -= cchTmp; \
|
---|
90 | } \
|
---|
91 | } while (0)
|
---|
92 |
|
---|
93 |
|
---|
94 | if (fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS)
|
---|
95 | PUT_C('[');
|
---|
96 |
|
---|
97 | for (uint32_t i = 0; i < cb; i++)
|
---|
98 | {
|
---|
99 | if (i != 0 && (fFlags & DIS_FMT_FLAGS_BYTES_SPACED))
|
---|
100 | PUT_NUM(3, " %02x", ab[i]);
|
---|
101 | else
|
---|
102 | PUT_NUM(2, "%02x", ab[i]);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS)
|
---|
106 | PUT_C(']');
|
---|
107 |
|
---|
108 | /* Terminate it just in case. */
|
---|
109 | if (cchDst >= 1)
|
---|
110 | *pszDst = '\0';
|
---|
111 |
|
---|
112 | #undef PUT_C
|
---|
113 | #undef PUT_NUM
|
---|
114 | return cchOutput;
|
---|
115 | }
|
---|
116 |
|
---|