1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <bitset>
17 #include <gtest/gtest.h>
18
19 #include "dfx_types.h"
20 #include "register.h"
21
22 using namespace testing::ext;
23 using namespace std;
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 class RegisterTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void RegisterTest::SetUpTestCase() {}
36
TearDownTestCase()37 void RegisterTest::TearDownTestCase() {}
38
SetUp()39 void RegisterTest::SetUp() {}
40
TearDown()41 void RegisterTest::TearDown() {}
42
43 /**
44 * @tc.name: GetArchName
45 * @tc.desc:
46 * @tc.type: FUNC
47 */
48 HWTEST_F(RegisterTest, GetArchName, TestSize.Level1)
49 {
50 EXPECT_EQ(GetArchName(ArchType::X86_32).compare("X86_32"), 0);
51 EXPECT_EQ(GetArchName(ArchType::X86_64).compare("X86_64"), 0);
52 EXPECT_EQ(GetArchName(ArchType::ARM).compare("ARM"), 0);
53 EXPECT_EQ(GetArchName(ArchType::ARM64).compare("ARM64"), 0);
54 EXPECT_EQ(GetArchName(static_cast<ArchType>(100)).compare("Unsupport"), 0);
55 EXPECT_EQ(GetArchName(static_cast<ArchType>(-1)).compare("Unsupport"), 0);
56 }
57
58 /**
59 * @tc.name: GetSupportedRegMask
60 * @tc.desc:
61 * @tc.type: FUNC
62 */
63 HWTEST_F(RegisterTest, GetSupportedRegMask, TestSize.Level1)
64 {
65 EXPECT_NE(GetSupportedRegMask(ArchType::X86_32), GetSupportedRegMask(ArchType::X86_64));
66 EXPECT_NE(GetSupportedRegMask(ArchType::ARM), GetSupportedRegMask(ArchType::ARM64));
67 EXPECT_EQ(GetSupportedRegMask(static_cast<ArchType>(100)),
68 std::numeric_limits<uint64_t>::max());
69 EXPECT_EQ(GetSupportedRegMask(static_cast<ArchType>(-1)), std::numeric_limits<uint64_t>::max());
70
71 std::bitset<64> regMasker;
72 regMasker = GetSupportedRegMask(ArchType::X86_32);
73 EXPECT_EQ(regMasker.count(), PERF_REG_X86_32_MAX);
74
75 regMasker = GetSupportedRegMask(ArchType::X86_64);
76 // dont support PERF_REG_X86_DS,PERF_REG_X86_ES,PERF_REG_X86_FS,PERF_REG_X86_GS
77 EXPECT_EQ(regMasker.count(), PERF_REG_X86_64_MAX - 4u);
78
79 regMasker = GetSupportedRegMask(ArchType::ARM);
80 EXPECT_EQ(regMasker.count(), PERF_REG_ARM_MAX);
81
82 regMasker = GetSupportedRegMask(ArchType::ARM64);
83 EXPECT_EQ(regMasker.count(), PERF_REG_ARM64_MAX);
84 }
85
86 /**
87 * @tc.name: RegisterGetIP
88 * @tc.desc:
89 * @tc.type: FUNC
90 */
91 HWTEST_F(RegisterTest, RegisterGetIP, TestSize.Level1)
92 {
93 EXPECT_EQ(RegisterGetIP(ArchType::X86_64), PERF_REG_X86_IP);
94 EXPECT_EQ(RegisterGetIP(ArchType::ARM), PERF_REG_ARM_PC);
95 EXPECT_EQ(RegisterGetIP(ArchType::ARM64), PERF_REG_ARM64_PC);
96 EXPECT_EQ(RegisterGetSP(ArchType::UNSUPPORT), std::numeric_limits<size_t>::max());
97 }
98
99 /**
100 * @tc.name: RegisterGetSP
101 * @tc.desc:
102 * @tc.type: FUNC
103 */
104 HWTEST_F(RegisterTest, RegisterGetSP, TestSize.Level1)
105 {
106 EXPECT_EQ(RegisterGetSP(ArchType::X86_64), PERF_REG_X86_SP);
107 EXPECT_EQ(RegisterGetSP(ArchType::ARM), PERF_REG_ARM_SP);
108 EXPECT_EQ(RegisterGetSP(ArchType::ARM64), PERF_REG_ARM64_SP);
109 EXPECT_EQ(RegisterGetSP(ArchType::UNSUPPORT), std::numeric_limits<size_t>::max());
110 }
111
112 /**
113 * @tc.name: RegisterGetValue
114 * @tc.desc:
115 * @tc.type: FUNC
116 */
117 HWTEST_F(RegisterTest, RegisterGetValue, TestSize.Level1)
118 {
119 uint64_t value = 0;
120 const u64 registers[4] = {1, 2, 3, 4};
121
122 EXPECT_EQ(RegisterGetValue(value, registers, 0, sizeof(registers)), true);
123 EXPECT_EQ(RegisterGetValue(value, registers, sizeof(registers), sizeof(registers)), false);
124 EXPECT_EQ(RegisterGetValue(value, registers, -1, sizeof(registers)), false);
125
126 for (unsigned i = 0; i < sizeof(registers); i++) {
127 RegisterGetValue(value, registers, i, sizeof(registers));
128 EXPECT_EQ(value, registers[i]);
129 }
130 }
131
132 /**
133 * @tc.name: RegisterGetSPValue
134 * @tc.desc:
135 * @tc.type: FUNC
136 */
137 HWTEST_F(RegisterTest, RegisterGetSPValue, TestSize.Level1)
138 {
139 uint64_t value = 0;
140 uint64_t value2 = 0;
141 u64 registers[PERF_REG_ARM64_MAX] = {1, 2, 3, 4};
142 size_t sp = RegisterGetSP(buildArchType);
143 registers[sp] = 0x1234;
144
145 EXPECT_EQ(RegisterGetValue(value, registers, sp, sizeof(registers)),
146 RegisterGetSPValue(value2, buildArchType, registers, sizeof(registers)));
147
148 EXPECT_EQ(value, value2);
149 }
150
151 /**
152 * @tc.name: RegisterGetIPValue
153 * @tc.desc:
154 * @tc.type: FUNC
155 */
156 HWTEST_F(RegisterTest, RegisterGetIPValue, TestSize.Level1)
157 {
158 uint64_t value = 0;
159 uint64_t value2 = 0;
160 u64 registers[PERF_REG_ARM64_MAX] = {1, 2, 3, 4};
161 size_t ip = RegisterGetIP(buildArchType);
162 registers[ip] = 0x1234;
163
164 EXPECT_EQ(RegisterGetValue(value, registers, ip, sizeof(registers)),
165 RegisterGetIPValue(value2, buildArchType, registers, sizeof(registers)));
166
167 EXPECT_EQ(value, value2);
168 }
169
170 /**
171 * @tc.name: RegisterGetName
172 * @tc.desc:
173 * @tc.type: FUNC
174 */
175 HWTEST_F(RegisterTest, RegisterGetName, TestSize.Level1)
176 {
177 for (unsigned i = 0; i < PERF_REG_ARM64_MAX; i++) {
178 EXPECT_EQ(RegisterGetName(i).empty(), false);
179 }
180 }
181
182 /**
183 * @tc.name: LibunwindRegIdToPerfReg
184 * @tc.desc:
185 * @tc.type: FUNC
186 */
187 HWTEST_F(RegisterTest, LibunwindRegIdToPerfReg, TestSize.Level1)
188 {
189 #if defined(__x86_64__)
190 unsigned max = PERF_REG_X86_64_MAX;
191 #elif defined(__arm__)
192 unsigned max = PERF_REG_ARM_MAX;
193 #elif defined(__aarch64__)
194 unsigned max = PERF_REG_ARM64_MAX;
195 #endif
196 for (unsigned i = 0; i < max; i++) {
197 EXPECT_EQ(LibunwindRegIdToPerfReg(i) >= 0, true);
198 }
199 EXPECT_EQ(LibunwindRegIdToPerfReg(max + 1) < 0, true);
200 EXPECT_EQ(LibunwindRegIdToPerfReg(-1) < 0, true);
201 }
202
203 /**
204 * @tc.name: GetArchTypeFromABI
205 * @tc.desc: test GetArchTypeFromABI function
206 * @tc.type: FUNC
207 */
208 HWTEST_F(RegisterTest, GetArchTypeFromABI, TestSize.Level1)
209 {
210 #if defined(__x86_64__)
211 EXPECT_EQ(GetArchTypeFromABI(false), ArchType::X86_64);
212 EXPECT_EQ(GetArchTypeFromABI(true), ArchType::X86_32);
213 #elif defined(__aarch64__)
214 EXPECT_EQ(GetArchTypeFromABI(false), ArchType::ARM64);
215 EXPECT_EQ(GetArchTypeFromABI(true), ArchType::ARM);
216 #elif defined(__arm__)
217 EXPECT_EQ(GetArchTypeFromABI(false), ArchType::ARM64);
218 #else
219 EXPECT_EQ(GetArchTypeFromABI(false), ArchType::UNSUPPORT);
220 #endif
221 }
222
223 /**
224 * @tc.name: GetDeviceArch
225 * @tc.desc: test GetDeviceArch function
226 * @tc.type: FUNC
227 */
228 HWTEST_F(RegisterTest, GetDeviceArch, TestSize.Level1)
229 {
230 #if defined(__x86_64__)
231 EXPECT_EQ(GetDeviceArch(), ArchType::X86_64);
232 #elif defined(__aarch64__)
233 EXPECT_EQ(GetDeviceArch(), ArchType::ARM64);
234 #elif defined(__arm__)
235 EXPECT_EQ(GetDeviceArch(), ArchType::ARM64);
236 #else
237 EXPECT_EQ(GetDeviceArch(), ArchType::UNSUPPORT);
238 #endif
239 }
240
241 /**
242 * @tc.name: SetDeviceArch
243 * @tc.desc: test SetDeviceArch function
244 * @tc.type: FUNC
245 */
246 HWTEST_F(RegisterTest, SetDeviceArch, TestSize.Level1)
247 {
248 EXPECT_EQ(SetDeviceArch(ArchType::ARM64), ArchType::ARM64);
249 EXPECT_EQ(SetDeviceArch(ArchType::ARM), ArchType::ARM);
250 EXPECT_EQ(SetDeviceArch(ArchType::X86_64), ArchType::X86_64);
251 EXPECT_EQ(SetDeviceArch(ArchType::X86_32), ArchType::X86_32);
252 }
253 } // namespace HiviewDFX
254 } // namespace OHOS
255