1 | /* $Id: tstDisasm-1.cpp 41886 2012-06-22 13:24:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox disassembler - Test application
|
---|
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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <VBox/dis.h>
|
---|
22 | #include <iprt/test.h>
|
---|
23 | #include <iprt/ctype.h>
|
---|
24 | #include <iprt/string.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/time.h>
|
---|
27 |
|
---|
28 |
|
---|
29 | DECLASM(int) TestProc32(void);
|
---|
30 | DECLASM(int) TestProc32_EndProc(void);
|
---|
31 | #ifndef RT_OS_OS2
|
---|
32 | DECLASM(int) TestProc64(void);
|
---|
33 | DECLASM(int) TestProc64_EndProc(void);
|
---|
34 | #endif
|
---|
35 | //uint8_t aCode16[] = { 0x66, 0x67, 0x89, 0x07 };
|
---|
36 |
|
---|
37 | static void testDisas(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
|
---|
38 | {
|
---|
39 | RTTestISub(pszSub);
|
---|
40 | size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
|
---|
41 | for (size_t off = 0; off < cbInstrs;)
|
---|
42 | {
|
---|
43 | uint32_t const cErrBefore = RTTestIErrorCount();
|
---|
44 | uint32_t cb = 1;
|
---|
45 | DISSTATE Dis;
|
---|
46 | char szOutput[256] = {0};
|
---|
47 | int rc = DISInstrToStr(&pabInstrs[off], enmDisCpuMode, &Dis, &cb, szOutput, sizeof(szOutput));
|
---|
48 |
|
---|
49 | RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
|
---|
50 | RTTESTI_CHECK(cb == Dis.cbInstr);
|
---|
51 | RTTESTI_CHECK(cb > 0);
|
---|
52 | RTTESTI_CHECK(cb <= 16);
|
---|
53 | RTStrStripR(szOutput);
|
---|
54 | RTTESTI_CHECK(szOutput[0]);
|
---|
55 | if (szOutput[0])
|
---|
56 | {
|
---|
57 | char *pszBytes = strchr(szOutput, '[');
|
---|
58 | RTTESTI_CHECK(pszBytes);
|
---|
59 | if (pszBytes)
|
---|
60 | {
|
---|
61 | RTTESTI_CHECK(pszBytes[-1] == ' ');
|
---|
62 | RTTESTI_CHECK(RT_C_IS_XDIGIT(pszBytes[1]));
|
---|
63 | RTTESTI_CHECK(pszBytes[cb * 3] == ']');
|
---|
64 | RTTESTI_CHECK(pszBytes[cb * 3 + 1] == ' ');
|
---|
65 |
|
---|
66 | size_t cch = strlen(szOutput);
|
---|
67 | RTTESTI_CHECK(szOutput[cch - 1] != ',');
|
---|
68 | }
|
---|
69 | }
|
---|
70 | if (cErrBefore != RTTestIErrorCount())
|
---|
71 | RTTestIFailureDetails("rc=%Rrc, off=%#x (%u) cbInstr=%u enmDisCpuMode=%d\n",
|
---|
72 | rc, off, Dis.cbInstr, enmDisCpuMode);
|
---|
73 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s\n", szOutput);
|
---|
74 |
|
---|
75 | /* Check with size-only. */
|
---|
76 | uint32_t cbOnly = 1;
|
---|
77 | DISSTATE DisOnly;
|
---|
78 | rc = DISInstrWithPrefetchedBytes((uintptr_t)&pabInstrs[off], enmDisCpuMode, 0 /*fFilter - none */,
|
---|
79 | Dis.abInstr, Dis.cbCachedInstr, NULL, NULL, &DisOnly, &cbOnly);
|
---|
80 |
|
---|
81 | RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
|
---|
82 | RTTESTI_CHECK(cbOnly == DisOnly.cbInstr);
|
---|
83 | RTTESTI_CHECK_MSG(cbOnly == cb, ("%#x vs %#x\n", cbOnly, cb));
|
---|
84 |
|
---|
85 | off += cb;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | static DECLCALLBACK(int) testReadBytes(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
91 | {
|
---|
92 | memcpy(&pDis->abInstr[offInstr], (void *)((uintptr_t)pDis->uInstrAddr + offInstr), cbMaxRead);
|
---|
93 | pDis->cbCachedInstr = offInstr + cbMaxRead;
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | static void testPerformance(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
|
---|
99 | {
|
---|
100 | RTTestISubF("Performance - %s", pszSub);
|
---|
101 |
|
---|
102 | size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
|
---|
103 | uint64_t cInstrs = 0;
|
---|
104 | uint64_t nsStart = RTTimeNanoTS();
|
---|
105 | for (uint32_t i = 0; i < _512K; i++) /* the samples are way to small. :-) */
|
---|
106 | {
|
---|
107 | for (size_t off = 0; off < cbInstrs; cInstrs++)
|
---|
108 | {
|
---|
109 | uint32_t cb = 1;
|
---|
110 | DISSTATE Dis;
|
---|
111 | DISInstrWithReader((uintptr_t)&pabInstrs[off], enmDisCpuMode, testReadBytes, NULL, &Dis, &cb);
|
---|
112 | off += cb;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | uint64_t cNsElapsed = RTTimeNanoTS() - nsStart;
|
---|
116 |
|
---|
117 | RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "%s-Total", pszSub);
|
---|
118 | RTTestIValueF(cNsElapsed / cInstrs, RTTESTUNIT_NS_PER_CALL, "%s-per-instruction", pszSub);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | int main(int argc, char **argv)
|
---|
123 | {
|
---|
124 | RTTEST hTest;
|
---|
125 | RTEXITCODE rcExit = RTTestInitAndCreate("tstDisasm", &hTest);
|
---|
126 | if (rcExit)
|
---|
127 | return rcExit;
|
---|
128 | RTTestBanner(hTest);
|
---|
129 |
|
---|
130 | static const struct
|
---|
131 | {
|
---|
132 | const char *pszDesc;
|
---|
133 | uint8_t const *pbStart;
|
---|
134 | uintptr_t uEndPtr;
|
---|
135 | DISCPUMODE enmCpuMode;
|
---|
136 | } aSnippets[] =
|
---|
137 | {
|
---|
138 | { "32-bit", (uint8_t const *)(uintptr_t)TestProc32, (uintptr_t)&TestProc32_EndProc, DISCPUMODE_32BIT },
|
---|
139 | { "64-bit", (uint8_t const *)(uintptr_t)TestProc64, (uintptr_t)&TestProc64_EndProc, DISCPUMODE_64BIT },
|
---|
140 | };
|
---|
141 |
|
---|
142 | for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
|
---|
143 | testDisas(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
|
---|
144 |
|
---|
145 | if (RTTestIErrorCount() == 0)
|
---|
146 | {
|
---|
147 | for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
|
---|
148 | testPerformance(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
|
---|
149 | }
|
---|
150 |
|
---|
151 | return RTTestSummaryAndDestroy(hTest);
|
---|
152 | }
|
---|
153 |
|
---|