VirtualBox

source: vbox/trunk/src/VBox/Disassembler/testcase/tstDisasm-1.cpp@ 41797

Last change on this file since 41797 was 41797, checked in by vboxsync, 13 years ago

tstDisasm-1: use out own callback.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* $Id: tstDisasm-1.cpp 41797 2012-06-17 02:52:09Z 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
29DECLASM(int) TestProc32(void);
30DECLASM(int) TestProc32_EndProc(void);
31#ifndef RT_OS_OS2
32DECLASM(int) TestProc64(void);
33DECLASM(int) TestProc64_EndProc(void);
34#endif
35//uint8_t aCode16[] = { 0x66, 0x67, 0x89, 0x07 };
36
37static 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 off += cb;
75 }
76}
77
78
79static DECLCALLBACK(int) testReadBytes(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
80{
81 memcpy(&pDis->abInstr[offInstr], (void *)((uintptr_t)pDis->uInstrAddr + offInstr), cbMaxRead);
82 pDis->cbCachedInstr = offInstr + cbMaxRead;
83 return VINF_SUCCESS;
84}
85
86
87static void testPerformance(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
88{
89 RTTestISubF("Performance - %s", pszSub);
90
91 size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
92 uint64_t cInstrs = 0;
93 uint64_t nsStart = RTTimeNanoTS();
94 for (uint32_t i = 0; i < _512K; i++) /* the samples are way to small. :-) */
95 {
96 for (size_t off = 0; off < cbInstrs; cInstrs++)
97 {
98 uint32_t cb = 1;
99 DISSTATE Dis;
100 DISInstrWithReader((uintptr_t)&pabInstrs[off], enmDisCpuMode, testReadBytes, NULL, &Dis, &cb);
101 off += cb;
102 }
103 }
104 uint64_t cNsElapsed = RTTimeNanoTS() - nsStart;
105
106 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "%s-Total", pszSub);
107 RTTestIValueF(cNsElapsed / cInstrs, RTTESTUNIT_NS_PER_CALL, "%s-per-instruction", pszSub);
108}
109
110
111int main(int argc, char **argv)
112{
113 RTTEST hTest;
114 RTEXITCODE rcExit = RTTestInitAndCreate("tstDisasm", &hTest);
115 if (rcExit)
116 return rcExit;
117 RTTestBanner(hTest);
118
119 static const struct
120 {
121 const char *pszDesc;
122 uint8_t const *pbStart;
123 uintptr_t uEndPtr;
124 DISCPUMODE enmCpuMode;
125 } aSnippets[] =
126 {
127 { "32-bit", (uint8_t const *)(uintptr_t)TestProc32, (uintptr_t)&TestProc32_EndProc, DISCPUMODE_32BIT },
128 { "64-bit", (uint8_t const *)(uintptr_t)TestProc64, (uintptr_t)&TestProc64_EndProc, DISCPUMODE_64BIT },
129 };
130
131 for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
132 testDisas(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
133
134 if (RTTestIErrorCount() == 0)
135 {
136 for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
137 testPerformance(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
138 }
139
140 return RTTestSummaryAndDestroy(hTest);
141}
142
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette