VirtualBox

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

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

DISCPUSTATE -> DISSTATE (slow change).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/* $Id: tstDisasm-1.cpp 41790 2012-06-16 20:33:45Z 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.215389.xyz. 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
78static void testPerformance(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
79{
80 RTTestISubF("Performance - %s", pszSub);
81
82 size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
83 uint64_t cInstrs = 0;
84 uint64_t nsStart = RTTimeNanoTS();
85 for (uint32_t i = 0; i < _512K; i++) /* the samples are way to small. :-) */
86 {
87 for (size_t off = 0; off < cbInstrs; cInstrs++)
88 {
89 uint32_t cb = 1;
90 DISSTATE Dis;
91 DISInstr(&pabInstrs[off], enmDisCpuMode, &Dis, &cb);
92 off += cb;
93 }
94 }
95 uint64_t cNsElapsed = RTTimeNanoTS() - nsStart;
96
97 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "%s-Total", pszSub);
98 RTTestIValueF(cNsElapsed / cInstrs, RTTESTUNIT_NS_PER_CALL, "%s-per-instruction", pszSub);
99}
100
101
102int main(int argc, char **argv)
103{
104 RTTEST hTest;
105 RTEXITCODE rcExit = RTTestInitAndCreate("tstDisasm", &hTest);
106 if (rcExit)
107 return rcExit;
108 RTTestBanner(hTest);
109
110 static const struct
111 {
112 const char *pszDesc;
113 uint8_t const *pbStart;
114 uintptr_t uEndPtr;
115 DISCPUMODE enmCpuMode;
116 } aSnippets[] =
117 {
118 { "32-bit", (uint8_t const *)(uintptr_t)TestProc32, (uintptr_t)&TestProc32_EndProc, DISCPUMODE_32BIT },
119 { "64-bit", (uint8_t const *)(uintptr_t)TestProc64, (uintptr_t)&TestProc64_EndProc, DISCPUMODE_64BIT },
120 };
121
122 for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
123 testDisas(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
124
125 if (RTTestIErrorCount() == 0)
126 {
127 for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
128 testPerformance(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
129 }
130
131 return RTTestSummaryAndDestroy(hTest);
132}
133
Note: See TracBrowser for help on using the repository browser.

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