148f512ceSopenharmony_ci/* 248f512ceSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 448f512ceSopenharmony_ci * you may not use this file except in compliance with the License. 548f512ceSopenharmony_ci * You may obtain a copy of the License at 648f512ceSopenharmony_ci * 748f512ceSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 848f512ceSopenharmony_ci * 948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and 1348f512ceSopenharmony_ci * limitations under the License. 1448f512ceSopenharmony_ci */ 1548f512ceSopenharmony_ci 1648f512ceSopenharmony_ci#include "callstack_test.h" 1748f512ceSopenharmony_ci 1848f512ceSopenharmony_ciusing namespace testing::ext; 1948f512ceSopenharmony_ciusing namespace testing; 2048f512ceSopenharmony_ciusing namespace std; 2148f512ceSopenharmony_ciusing namespace OHOS::HiviewDFX; 2248f512ceSopenharmony_cinamespace OHOS { 2348f512ceSopenharmony_cinamespace Developtools { 2448f512ceSopenharmony_cinamespace HiPerf { 2548f512ceSopenharmony_ciclass CallStackTest : public testing::Test { 2648f512ceSopenharmony_cipublic: 2748f512ceSopenharmony_ci static void SetUpTestCase(void); 2848f512ceSopenharmony_ci static void TearDownTestCase(void); 2948f512ceSopenharmony_ci void SetUp(); 3048f512ceSopenharmony_ci void TearDown(); 3148f512ceSopenharmony_ci default_random_engine rnd_; 3248f512ceSopenharmony_ci}; 3348f512ceSopenharmony_ci 3448f512ceSopenharmony_civoid CallStackTest::SetUpTestCase() 3548f512ceSopenharmony_ci{ 3648f512ceSopenharmony_ci DebugLogger::GetInstance()->Reset(); 3748f512ceSopenharmony_ci DebugLogger::GetInstance()->OpenLog(DEFAULT_UT_LOG_DIR + "CallStackTest.txt"); 3848f512ceSopenharmony_ci} 3948f512ceSopenharmony_ci 4048f512ceSopenharmony_civoid CallStackTest::TearDownTestCase() 4148f512ceSopenharmony_ci{ 4248f512ceSopenharmony_ci DebugLogger::GetInstance()->RestoreLog(); 4348f512ceSopenharmony_ci} 4448f512ceSopenharmony_ci 4548f512ceSopenharmony_civoid CallStackTest::SetUp() {} 4648f512ceSopenharmony_ci 4748f512ceSopenharmony_civoid CallStackTest::TearDown() {} 4848f512ceSopenharmony_ci 4948f512ceSopenharmony_ci/** 5048f512ceSopenharmony_ci * @tc.name: ExpandCallStack 5148f512ceSopenharmony_ci * @tc.desc: 5248f512ceSopenharmony_ci * @tc.type: FUNC 5348f512ceSopenharmony_ci */ 5448f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackEmpty, TestSize.Level1) 5548f512ceSopenharmony_ci{ 5648f512ceSopenharmony_ci /* 5748f512ceSopenharmony_ci 3 2 1 5848f512ceSopenharmony_ci cache A -> B -> C 5948f512ceSopenharmony_ci new C 6048f512ceSopenharmony_ci expand A -> B -> C 6148f512ceSopenharmony_ci */ 6248f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 6348f512ceSopenharmony_ci CallStack callStack; 6448f512ceSopenharmony_ci 6548f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 6648f512ceSopenharmony_ci {0x1u, 0x1u}, 6748f512ceSopenharmony_ci {0x2u, 0x2u}, 6848f512ceSopenharmony_ci {0x3u, 0x3u}, 6948f512ceSopenharmony_ci }; 7048f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = {}; 7148f512ceSopenharmony_ci 7248f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 7348f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 0u); 7448f512ceSopenharmony_ci ASSERT_NE(stack1, stack2); 7548f512ceSopenharmony_ci} 7648f512ceSopenharmony_ci 7748f512ceSopenharmony_ci/** 7848f512ceSopenharmony_ci * @tc.name: ExpandCallStack 7948f512ceSopenharmony_ci * @tc.desc: 8048f512ceSopenharmony_ci * @tc.type: FUNC 8148f512ceSopenharmony_ci */ 8248f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackC, TestSize.Level1) 8348f512ceSopenharmony_ci{ 8448f512ceSopenharmony_ci /* 8548f512ceSopenharmony_ci 3 2 1 8648f512ceSopenharmony_ci cache A -> B -> C 8748f512ceSopenharmony_ci new C 8848f512ceSopenharmony_ci expand A -> B -> C 8948f512ceSopenharmony_ci */ 9048f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 9148f512ceSopenharmony_ci CallStack callStack; 9248f512ceSopenharmony_ci 9348f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 9448f512ceSopenharmony_ci {0x1u, 0x1u}, 9548f512ceSopenharmony_ci {0x2u, 0x2u}, 9648f512ceSopenharmony_ci {0x3u, 0x3u}, 9748f512ceSopenharmony_ci }; 9848f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 9948f512ceSopenharmony_ci {0x1u, 0x1u}, 10048f512ceSopenharmony_ci }; 10148f512ceSopenharmony_ci 10248f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 10348f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 2u); 10448f512ceSopenharmony_ci ASSERT_EQ(stack1, stack2); 10548f512ceSopenharmony_ci} 10648f512ceSopenharmony_ci 10748f512ceSopenharmony_ci/** 10848f512ceSopenharmony_ci * @tc.name: ExpandCallStack 10948f512ceSopenharmony_ci * @tc.desc: 11048f512ceSopenharmony_ci * @tc.type: FUNC 11148f512ceSopenharmony_ci */ 11248f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackBC, TestSize.Level1) 11348f512ceSopenharmony_ci{ 11448f512ceSopenharmony_ci /* 11548f512ceSopenharmony_ci 3 2 1 11648f512ceSopenharmony_ci cache A -> B -> C 11748f512ceSopenharmony_ci new B -> C 11848f512ceSopenharmony_ci expand A -> B -> C 11948f512ceSopenharmony_ci */ 12048f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 12148f512ceSopenharmony_ci CallStack callStack; 12248f512ceSopenharmony_ci 12348f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 12448f512ceSopenharmony_ci {0x1u, 0x1u}, 12548f512ceSopenharmony_ci {0x2u, 0x2u}, 12648f512ceSopenharmony_ci {0x3u, 0x3u}, 12748f512ceSopenharmony_ci }; 12848f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 12948f512ceSopenharmony_ci {0x1u, 0x1u}, 13048f512ceSopenharmony_ci {0x2u, 0x2u}, 13148f512ceSopenharmony_ci }; 13248f512ceSopenharmony_ci 13348f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 13448f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 1u); 13548f512ceSopenharmony_ci ASSERT_EQ(stack1, stack2); 13648f512ceSopenharmony_ci} 13748f512ceSopenharmony_ci 13848f512ceSopenharmony_ci/** 13948f512ceSopenharmony_ci * @tc.name: ExpandCallStack 14048f512ceSopenharmony_ci * @tc.desc: 14148f512ceSopenharmony_ci * @tc.type: FUNC 14248f512ceSopenharmony_ci */ 14348f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackABC, TestSize.Level1) 14448f512ceSopenharmony_ci{ 14548f512ceSopenharmony_ci /* 14648f512ceSopenharmony_ci 3 2 1 14748f512ceSopenharmony_ci cache A -> B -> C 14848f512ceSopenharmony_ci new A -> B -> C 14948f512ceSopenharmony_ci expand A -> B -> C 15048f512ceSopenharmony_ci */ 15148f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 15248f512ceSopenharmony_ci CallStack callStack; 15348f512ceSopenharmony_ci 15448f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 15548f512ceSopenharmony_ci {0x1u, 0x1u}, 15648f512ceSopenharmony_ci {0x2u, 0x2u}, 15748f512ceSopenharmony_ci {0x3u, 0x3u}, 15848f512ceSopenharmony_ci }; 15948f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 16048f512ceSopenharmony_ci {0x1u, 0x1u}, 16148f512ceSopenharmony_ci {0x2u, 0x2u}, 16248f512ceSopenharmony_ci {0x3u, 0x3u}, 16348f512ceSopenharmony_ci }; 16448f512ceSopenharmony_ci 16548f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 16648f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 0u); 16748f512ceSopenharmony_ci ASSERT_EQ(stack1, stack2); 16848f512ceSopenharmony_ci} 16948f512ceSopenharmony_ci 17048f512ceSopenharmony_ci/** 17148f512ceSopenharmony_ci * @tc.name: ExpandCallStack 17248f512ceSopenharmony_ci * @tc.desc: 17348f512ceSopenharmony_ci * @tc.type: FUNC 17448f512ceSopenharmony_ci */ 17548f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackAB, TestSize.Level1) 17648f512ceSopenharmony_ci{ 17748f512ceSopenharmony_ci /* 17848f512ceSopenharmony_ci 3 2 1 17948f512ceSopenharmony_ci cache A -> B -> C 18048f512ceSopenharmony_ci new A -> B 18148f512ceSopenharmony_ci expand A -> B 18248f512ceSopenharmony_ci */ 18348f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 18448f512ceSopenharmony_ci CallStack callStack; 18548f512ceSopenharmony_ci 18648f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 18748f512ceSopenharmony_ci {0x1u, 0x1u}, 18848f512ceSopenharmony_ci {0x2u, 0x2u}, 18948f512ceSopenharmony_ci {0x3u, 0x3u}, 19048f512ceSopenharmony_ci }; 19148f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 19248f512ceSopenharmony_ci {0x2u, 0x2u}, 19348f512ceSopenharmony_ci {0x3u, 0x3u}, 19448f512ceSopenharmony_ci }; 19548f512ceSopenharmony_ci 19648f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 19748f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 0u); 19848f512ceSopenharmony_ci ASSERT_NE(stack1, stack2); 19948f512ceSopenharmony_ci} 20048f512ceSopenharmony_ci 20148f512ceSopenharmony_ci/** 20248f512ceSopenharmony_ci * @tc.name: ExpandCallStack 20348f512ceSopenharmony_ci * @tc.desc: 20448f512ceSopenharmony_ci * @tc.type: FUNC 20548f512ceSopenharmony_ci */ 20648f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackA, TestSize.Level1) 20748f512ceSopenharmony_ci{ 20848f512ceSopenharmony_ci /* 20948f512ceSopenharmony_ci 3 2 1 21048f512ceSopenharmony_ci cache A -> B -> C 21148f512ceSopenharmony_ci new A 21248f512ceSopenharmony_ci expand A 21348f512ceSopenharmony_ci */ 21448f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 21548f512ceSopenharmony_ci CallStack callStack; 21648f512ceSopenharmony_ci 21748f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 21848f512ceSopenharmony_ci {0x1u, 0x1u}, 21948f512ceSopenharmony_ci {0x2u, 0x2u}, 22048f512ceSopenharmony_ci {0x3u, 0x3u}, 22148f512ceSopenharmony_ci }; 22248f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 22348f512ceSopenharmony_ci {0x3u, 0x3u}, 22448f512ceSopenharmony_ci }; 22548f512ceSopenharmony_ci 22648f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 22748f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 0u); 22848f512ceSopenharmony_ci ASSERT_NE(stack1, stack2); 22948f512ceSopenharmony_ci} 23048f512ceSopenharmony_ci 23148f512ceSopenharmony_ci/** 23248f512ceSopenharmony_ci * @tc.name: ExpandCallStack 23348f512ceSopenharmony_ci * @tc.desc: 23448f512ceSopenharmony_ci * @tc.type: FUNC 23548f512ceSopenharmony_ci */ 23648f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackB, TestSize.Level1) 23748f512ceSopenharmony_ci{ 23848f512ceSopenharmony_ci /* 23948f512ceSopenharmony_ci 3 2 1 24048f512ceSopenharmony_ci cache A -> B -> C 24148f512ceSopenharmony_ci new B 24248f512ceSopenharmony_ci expand A -> B 24348f512ceSopenharmony_ci */ 24448f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 24548f512ceSopenharmony_ci CallStack callStack; 24648f512ceSopenharmony_ci 24748f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 24848f512ceSopenharmony_ci {0x1u, 0x1u}, 24948f512ceSopenharmony_ci {0x2u, 0x2u}, 25048f512ceSopenharmony_ci {0x3u, 0x3u}, 25148f512ceSopenharmony_ci }; 25248f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 25348f512ceSopenharmony_ci {0x2u, 0x2u}, 25448f512ceSopenharmony_ci }; 25548f512ceSopenharmony_ci std::vector<DfxFrame> stack3 = { 25648f512ceSopenharmony_ci {0x2u, 0x2u}, 25748f512ceSopenharmony_ci {0x3u, 0x3u}, 25848f512ceSopenharmony_ci }; 25948f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 26048f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 1u); 26148f512ceSopenharmony_ci ASSERT_NE(stack1, stack2); 26248f512ceSopenharmony_ci ASSERT_EQ(stack3, stack2); 26348f512ceSopenharmony_ci} 26448f512ceSopenharmony_ci 26548f512ceSopenharmony_ci/** 26648f512ceSopenharmony_ci * @tc.name: ExpandCallStack 26748f512ceSopenharmony_ci * @tc.desc: 26848f512ceSopenharmony_ci * @tc.type: FUNC 26948f512ceSopenharmony_ci */ 27048f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackB2, TestSize.Level1) 27148f512ceSopenharmony_ci{ 27248f512ceSopenharmony_ci /* 27348f512ceSopenharmony_ci 3 2 1 27448f512ceSopenharmony_ci cache A -> B -> C 27548f512ceSopenharmony_ci new B 27648f512ceSopenharmony_ci expand A -> B 27748f512ceSopenharmony_ci */ 27848f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 27948f512ceSopenharmony_ci CallStack callStack; 28048f512ceSopenharmony_ci 28148f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 28248f512ceSopenharmony_ci {0x1u, 0x1u}, 28348f512ceSopenharmony_ci {0x2u, 0x2u}, 28448f512ceSopenharmony_ci {0x3u, 0x3u}, 28548f512ceSopenharmony_ci }; 28648f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 28748f512ceSopenharmony_ci {0x2u, 0x2u}, 28848f512ceSopenharmony_ci }; 28948f512ceSopenharmony_ci std::vector<DfxFrame> stack3 = { 29048f512ceSopenharmony_ci {0x2u, 0x2u}, 29148f512ceSopenharmony_ci {0x3u, 0x3u}, 29248f512ceSopenharmony_ci }; 29348f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1, 2), 0u); 29448f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2, 2), 0u); 29548f512ceSopenharmony_ci ASSERT_NE(stack1, stack2); 29648f512ceSopenharmony_ci ASSERT_NE(stack3, stack2); 29748f512ceSopenharmony_ci} 29848f512ceSopenharmony_ci 29948f512ceSopenharmony_ci/** 30048f512ceSopenharmony_ci * @tc.name: ExpandCallStack 30148f512ceSopenharmony_ci * @tc.desc: 30248f512ceSopenharmony_ci * @tc.type: FUNC 30348f512ceSopenharmony_ci */ 30448f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackB0, TestSize.Level1) 30548f512ceSopenharmony_ci{ 30648f512ceSopenharmony_ci /* 30748f512ceSopenharmony_ci 3 2 1 30848f512ceSopenharmony_ci cache A -> B -> C 30948f512ceSopenharmony_ci new B 31048f512ceSopenharmony_ci expand A -> B 31148f512ceSopenharmony_ci */ 31248f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 31348f512ceSopenharmony_ci CallStack callStack; 31448f512ceSopenharmony_ci 31548f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 31648f512ceSopenharmony_ci {0x1u, 0x1u}, 31748f512ceSopenharmony_ci {0x2u, 0x2u}, 31848f512ceSopenharmony_ci {0x3u, 0x3u}, 31948f512ceSopenharmony_ci }; 32048f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 32148f512ceSopenharmony_ci {0x2u, 0x2u}, 32248f512ceSopenharmony_ci }; 32348f512ceSopenharmony_ci std::vector<DfxFrame> stack3 = { 32448f512ceSopenharmony_ci {0x2u, 0x2u}, 32548f512ceSopenharmony_ci {0x3u, 0x3u}, 32648f512ceSopenharmony_ci }; 32748f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1, 0), 0u); 32848f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2, 0), 0u); 32948f512ceSopenharmony_ci ASSERT_NE(stack1, stack2); 33048f512ceSopenharmony_ci ASSERT_NE(stack3, stack2); 33148f512ceSopenharmony_ci} 33248f512ceSopenharmony_ci 33348f512ceSopenharmony_ci/** 33448f512ceSopenharmony_ci * @tc.name: ExpandCallStack 33548f512ceSopenharmony_ci * @tc.desc: 33648f512ceSopenharmony_ci * @tc.type: FUNC 33748f512ceSopenharmony_ci */ 33848f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackBC2, TestSize.Level1) 33948f512ceSopenharmony_ci{ 34048f512ceSopenharmony_ci /* 34148f512ceSopenharmony_ci 3 2 1 34248f512ceSopenharmony_ci cache A -> B -> C 34348f512ceSopenharmony_ci new B -> C 34448f512ceSopenharmony_ci expand A -> B -> C 34548f512ceSopenharmony_ci */ 34648f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true); 34748f512ceSopenharmony_ci CallStack callStack; 34848f512ceSopenharmony_ci 34948f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 35048f512ceSopenharmony_ci {0x1u, 0x1u}, 35148f512ceSopenharmony_ci {0x2u, 0x2u}, 35248f512ceSopenharmony_ci {0x3u, 0x3u}, 35348f512ceSopenharmony_ci }; 35448f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 35548f512ceSopenharmony_ci {0x1u, 0x1u}, 35648f512ceSopenharmony_ci {0x2u, 0x2u}, 35748f512ceSopenharmony_ci }; 35848f512ceSopenharmony_ci 35948f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1, 2), 0u); 36048f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2, 2), 1u); 36148f512ceSopenharmony_ci ASSERT_EQ(stack1, stack2); 36248f512ceSopenharmony_ci} 36348f512ceSopenharmony_ci 36448f512ceSopenharmony_ci/** 36548f512ceSopenharmony_ci * @tc.name: ExpandCallStack 36648f512ceSopenharmony_ci * @tc.desc: 36748f512ceSopenharmony_ci * @tc.type: FUNC 36848f512ceSopenharmony_ci */ 36948f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackABCDE, TestSize.Level1) 37048f512ceSopenharmony_ci{ 37148f512ceSopenharmony_ci /* 37248f512ceSopenharmony_ci 0. A -> B -> C -> E -> F 37348f512ceSopenharmony_ci 1. C -> E -> F 37448f512ceSopenharmony_ci 2. B -> C 37548f512ceSopenharmony_ci 3. A -> B -> C 37648f512ceSopenharmony_ci 4. B -> F -> F 37748f512ceSopenharmony_ci */ 37848f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 37948f512ceSopenharmony_ci CallStack callStack; 38048f512ceSopenharmony_ci 38148f512ceSopenharmony_ci std::vector<DfxFrame> stackFull = { 38248f512ceSopenharmony_ci {0xE, 0xE}, {0xD, 0xD}, {0xC, 0xC}, {0xB, 0xB}, {0xA, 0xA}, 38348f512ceSopenharmony_ci }; 38448f512ceSopenharmony_ci std::vector<DfxFrame> stackBC = { 38548f512ceSopenharmony_ci {0xC, 0xC}, 38648f512ceSopenharmony_ci {0xB, 0xB}, 38748f512ceSopenharmony_ci }; 38848f512ceSopenharmony_ci std::vector<DfxFrame> stackABC = { 38948f512ceSopenharmony_ci {0xC, 0xC}, 39048f512ceSopenharmony_ci {0xB, 0xB}, 39148f512ceSopenharmony_ci {0xA, 0xA}, 39248f512ceSopenharmony_ci }; 39348f512ceSopenharmony_ci std::vector<DfxFrame> stackBFF = { 39448f512ceSopenharmony_ci {0xF, 0xF}, 39548f512ceSopenharmony_ci {0xF, 0xF}, 39648f512ceSopenharmony_ci {0xB, 0xB}, 39748f512ceSopenharmony_ci }; 39848f512ceSopenharmony_ci std::vector<DfxFrame> stackBFF2 = { 39948f512ceSopenharmony_ci {0xF, 0xF}, 40048f512ceSopenharmony_ci {0xF, 0xF}, 40148f512ceSopenharmony_ci {0xB, 0xB}, 40248f512ceSopenharmony_ci }; 40348f512ceSopenharmony_ci 40448f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackFull), 0u); 40548f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackBC), 1u); 40648f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackABC), 0u); 40748f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackBFF), 1u); 40848f512ceSopenharmony_ci 40948f512ceSopenharmony_ci // use stackBFF 41048f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackBFF2, 2), 1u); 41148f512ceSopenharmony_ci} 41248f512ceSopenharmony_ci 41348f512ceSopenharmony_ci/** 41448f512ceSopenharmony_ci * @tc.name: ExpandCallStack 41548f512ceSopenharmony_ci * @tc.desc: 41648f512ceSopenharmony_ci * @tc.type: FUNC 41748f512ceSopenharmony_ci */ 41848f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackFailure, TestSize.Level1) 41948f512ceSopenharmony_ci{ 42048f512ceSopenharmony_ci /* 42148f512ceSopenharmony_ci 0. A -> B -> C -> E -> F 42248f512ceSopenharmony_ci 1. C -> E -> F 42348f512ceSopenharmony_ci 2. B -> C 42448f512ceSopenharmony_ci 3. A -> B -> C 42548f512ceSopenharmony_ci 4. B -> F -> F 42648f512ceSopenharmony_ci */ 42748f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 42848f512ceSopenharmony_ci CallStack callStack; 42948f512ceSopenharmony_ci 43048f512ceSopenharmony_ci std::vector<DfxFrame> stackFull = { 43148f512ceSopenharmony_ci {0xC, 0xC}, 43248f512ceSopenharmony_ci {0xB, 0xB}, 43348f512ceSopenharmony_ci {0xA, 0xA}, 43448f512ceSopenharmony_ci }; 43548f512ceSopenharmony_ci std::vector<DfxFrame> stackDE = { 43648f512ceSopenharmony_ci {0xE, 0xE}, 43748f512ceSopenharmony_ci {0xD, 0xD}, 43848f512ceSopenharmony_ci }; 43948f512ceSopenharmony_ci 44048f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackFull), 0u); 44148f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackDE), 0u); 44248f512ceSopenharmony_ci} 44348f512ceSopenharmony_ci 44448f512ceSopenharmony_ci/** 44548f512ceSopenharmony_ci * @tc.name: ExpandCallStack 44648f512ceSopenharmony_ci * @tc.desc: 44748f512ceSopenharmony_ci * @tc.type: FUNC 44848f512ceSopenharmony_ci */ 44948f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackTwoChance, TestSize.Level1) 45048f512ceSopenharmony_ci{ 45148f512ceSopenharmony_ci /* 45248f512ceSopenharmony_ci 0. A -> B -> C -> E -> F 45348f512ceSopenharmony_ci 1. 2 -> C -> E -> F 45448f512ceSopenharmony_ci 2. C 45548f512ceSopenharmony_ci */ 45648f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH); 45748f512ceSopenharmony_ci CallStack callStack; 45848f512ceSopenharmony_ci 45948f512ceSopenharmony_ci std::vector<DfxFrame> stack0 = { 46048f512ceSopenharmony_ci {0xE, 0xE}, {0xD, 0xD}, {0xC, 0xC}, {0xB, 0xB}, {0xA, 0xA}, 46148f512ceSopenharmony_ci }; 46248f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 46348f512ceSopenharmony_ci {0xE, 0xE}, 46448f512ceSopenharmony_ci {0xD, 0xD}, 46548f512ceSopenharmony_ci {0xC, 0xC}, 46648f512ceSopenharmony_ci {0x2, 0x2}, 46748f512ceSopenharmony_ci }; 46848f512ceSopenharmony_ci std::vector<DfxFrame> stackC = { 46948f512ceSopenharmony_ci {0xC, 0xC}, 47048f512ceSopenharmony_ci }; 47148f512ceSopenharmony_ci std::vector<DfxFrame> stackC2 = { 47248f512ceSopenharmony_ci {0xC, 0xC}, 47348f512ceSopenharmony_ci {0x2, 0x2}, 47448f512ceSopenharmony_ci }; 47548f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack0), 0u); 47648f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 47748f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stackC), 1u); 47848f512ceSopenharmony_ci} 47948f512ceSopenharmony_ci 48048f512ceSopenharmony_ci/** 48148f512ceSopenharmony_ci * @tc.name: ExpandCallStack 48248f512ceSopenharmony_ci * @tc.desc: 48348f512ceSopenharmony_ci * @tc.type: FUNC 48448f512ceSopenharmony_ci */ 48548f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackFullCache, TestSize.Level1) 48648f512ceSopenharmony_ci{ 48748f512ceSopenharmony_ci CallStack callStack; 48848f512ceSopenharmony_ci for (size_t i = 0; i < MAX_CALL_FRAME_EXPAND_CACHE_SIZE; i++) { 48948f512ceSopenharmony_ci std::vector<DfxFrame> stack = {{rnd_(), rnd_()}}; 49048f512ceSopenharmony_ci callStack.ExpandCallStack(0, stack); 49148f512ceSopenharmony_ci } 49248f512ceSopenharmony_ci for (size_t i = 0; i < MAX_CALL_FRAME_EXPAND_CACHE_SIZE; i++) { 49348f512ceSopenharmony_ci std::vector<DfxFrame> stack = {{rnd_(), rnd_()}}; 49448f512ceSopenharmony_ci callStack.ExpandCallStack(0, stack); 49548f512ceSopenharmony_ci } 49648f512ceSopenharmony_ci EXPECT_EQ(callStack.cachedCallFramesMap_[0].size(), MAX_CALL_FRAME_EXPAND_CACHE_SIZE); 49748f512ceSopenharmony_ci} 49848f512ceSopenharmony_ci 49948f512ceSopenharmony_ci/** 50048f512ceSopenharmony_ci * @tc.name: ExpandCallStack 50148f512ceSopenharmony_ci * @tc.desc: 50248f512ceSopenharmony_ci * @tc.type: FUNC 50348f512ceSopenharmony_ci */ 50448f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackSmall, TestSize.Level1) 50548f512ceSopenharmony_ci{ 50648f512ceSopenharmony_ci CallStack callStack; 50748f512ceSopenharmony_ci std::vector<DfxFrame> stack0 = {}; 50848f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = {{0x1, 0x1}}; 50948f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = {{0x1, 0x1}, {0x2, 0x2}}; 51048f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack0), 0u); 51148f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 51248f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 51348f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2, 2), 0u); 51448f512ceSopenharmony_ci} 51548f512ceSopenharmony_ci 51648f512ceSopenharmony_ci/** 51748f512ceSopenharmony_ci * @tc.name: ExpandCallStack 51848f512ceSopenharmony_ci * @tc.desc: 51948f512ceSopenharmony_ci * @tc.type: FUNC 52048f512ceSopenharmony_ci */ 52148f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackLimit, TestSize.Level1) 52248f512ceSopenharmony_ci{ 52348f512ceSopenharmony_ci /* 52448f512ceSopenharmony_ci 3 2 1 0 52548f512ceSopenharmony_ci cache A -> B -> C 52648f512ceSopenharmony_ci stack2 C 52748f512ceSopenharmony_ci expand C 52848f512ceSopenharmony_ci 52948f512ceSopenharmony_ci stack3 B -> C 53048f512ceSopenharmony_ci expand A -> B -> C 53148f512ceSopenharmony_ci 53248f512ceSopenharmony_ci stack4 C -> D 53348f512ceSopenharmony_ci expand C 53448f512ceSopenharmony_ci */ 53548f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true); 53648f512ceSopenharmony_ci CallStack callStack; 53748f512ceSopenharmony_ci 53848f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 53948f512ceSopenharmony_ci {0x1u, 0x1u}, 54048f512ceSopenharmony_ci {0x2u, 0x2u}, 54148f512ceSopenharmony_ci {0x3u, 0x3u}, 54248f512ceSopenharmony_ci }; 54348f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 54448f512ceSopenharmony_ci {0x1u, 0x1u}, 54548f512ceSopenharmony_ci }; 54648f512ceSopenharmony_ci std::vector<DfxFrame> stack3 = { 54748f512ceSopenharmony_ci {0x1u, 0x1u}, 54848f512ceSopenharmony_ci {0x2u, 0x2u}, 54948f512ceSopenharmony_ci }; 55048f512ceSopenharmony_ci std::vector<DfxFrame> stack4 = { 55148f512ceSopenharmony_ci {0x0u, 0x0u}, 55248f512ceSopenharmony_ci {0x1u, 0x1u}, 55348f512ceSopenharmony_ci }; 55448f512ceSopenharmony_ci 55548f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1, 2u), 0u); 55648f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2, 2u), 0u); 55748f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack3, 2u), 1u); 55848f512ceSopenharmony_ci EXPECT_THAT(stack1, ContainerEq(stack3)); 55948f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack4, 2u), 0u); 56048f512ceSopenharmony_ci} 56148f512ceSopenharmony_ci 56248f512ceSopenharmony_ci/** 56348f512ceSopenharmony_ci * @tc.name: ExpandCallStack 56448f512ceSopenharmony_ci * @tc.desc: 56548f512ceSopenharmony_ci * @tc.type: FUNC 56648f512ceSopenharmony_ci */ 56748f512ceSopenharmony_ciHWTEST_F(CallStackTest, ExpendCallStackABABAB, TestSize.Level1) 56848f512ceSopenharmony_ci{ 56948f512ceSopenharmony_ci /* 57048f512ceSopenharmony_ci Caller Called 57148f512ceSopenharmony_ci cache A -> B -> C -> A -> B -> C -> A -> B 57248f512ceSopenharmony_ci stack2 C 57348f512ceSopenharmony_ci expand A -> B -> C -> A -> B -> C 57448f512ceSopenharmony_ci 57548f512ceSopenharmony_ci stack3 B -> C 57648f512ceSopenharmony_ci expand A -> B -> C -> A -> B -> C 57748f512ceSopenharmony_ci 57848f512ceSopenharmony_ci stack4 C -> D 57948f512ceSopenharmony_ci expand A -> B -> C -> A -> B -> C -> D 58048f512ceSopenharmony_ci */ 58148f512ceSopenharmony_ci ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true); 58248f512ceSopenharmony_ci CallStack callStack; 58348f512ceSopenharmony_ci 58448f512ceSopenharmony_ci std::vector<DfxFrame> stack1 = { 58548f512ceSopenharmony_ci {0xb, 0xb}, {0xa, 0xa}, {0xc, 0xc}, {0xb, 0xb}, 58648f512ceSopenharmony_ci {0xa, 0xa}, {0xc, 0xc}, {0xb, 0xb}, {0xa, 0xa}, 58748f512ceSopenharmony_ci }; 58848f512ceSopenharmony_ci std::vector<DfxFrame> stack2 = { 58948f512ceSopenharmony_ci {0xc, 0xc}, 59048f512ceSopenharmony_ci }; 59148f512ceSopenharmony_ci std::vector<DfxFrame> stack3 = { 59248f512ceSopenharmony_ci {0xc, 0xc}, 59348f512ceSopenharmony_ci {0xb, 0xb}, 59448f512ceSopenharmony_ci }; 59548f512ceSopenharmony_ci std::vector<DfxFrame> stack4 = { 59648f512ceSopenharmony_ci {0xd, 0xd}, 59748f512ceSopenharmony_ci {0xc, 0xc}, 59848f512ceSopenharmony_ci }; 59948f512ceSopenharmony_ci 60048f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); 60148f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 5u); 60248f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack3), 4u); 60348f512ceSopenharmony_ci ASSERT_EQ(callStack.ExpandCallStack(0, stack4), 5u); 60448f512ceSopenharmony_ci} 60548f512ceSopenharmony_ci 60648f512ceSopenharmony_ci/** 60748f512ceSopenharmony_ci * @tc.name: UnwindCallStack 60848f512ceSopenharmony_ci * @tc.desc: 60948f512ceSopenharmony_ci * @tc.type: FUNC 61048f512ceSopenharmony_ci */ 61148f512ceSopenharmony_ciHWTEST_F(CallStackTest, UnwindCallStack, TestSize.Level1) 61248f512ceSopenharmony_ci{ 61348f512ceSopenharmony_ci#if is_linux 61448f512ceSopenharmony_ci return; 61548f512ceSopenharmony_ci#endif 61648f512ceSopenharmony_ci 61748f512ceSopenharmony_ci std::vector<u64> regs; 61848f512ceSopenharmony_ci std::vector<u8> data; 61948f512ceSopenharmony_ci LoadFromFile(PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_USER_REGS_0, regs); 62048f512ceSopenharmony_ci LoadFromFile(PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_USER_DATA_0, data); 62148f512ceSopenharmony_ci if (regs.size() > 0 and data.size() > 0) { 62248f512ceSopenharmony_ci#ifdef __arm__ 62348f512ceSopenharmony_ci ASSERT_EQ(regs.size(), 16u); 62448f512ceSopenharmony_ci#endif 62548f512ceSopenharmony_ci std::vector<std::unique_ptr<SymbolsFile>> symbolsFiles; 62648f512ceSopenharmony_ci auto &symbolsFile = symbolsFiles.emplace_back(SymbolsFile::CreateSymbolsFile( 62748f512ceSopenharmony_ci SYMBOL_ELF_FILE, TEST_DWARF_ELF)); 62848f512ceSopenharmony_ci ASSERT_EQ(symbolsFile->setSymbolsFilePath(PATH_RESOURCE_TEST_DWARF_DATA), true); 62948f512ceSopenharmony_ci ASSERT_EQ(symbolsFile->LoadSymbols(), true); 63048f512ceSopenharmony_ci // fix the name 63148f512ceSopenharmony_ci symbolsFile->filePath_ = TEST_DWARF_MMAP.front().fileName; 63248f512ceSopenharmony_ci 63348f512ceSopenharmony_ci VirtualThread thread(getpid(), symbolsFiles); 63448f512ceSopenharmony_ci MakeMaps(thread); 63548f512ceSopenharmony_ci std::vector<DfxFrame> callFrames; 63648f512ceSopenharmony_ci CallStack callStack; 63748f512ceSopenharmony_ci 63848f512ceSopenharmony_ci bool ret = callStack.UnwindCallStack(thread, false, regs.data(), regs.size(), data.data(), data.size(), 63948f512ceSopenharmony_ci callFrames); 64048f512ceSopenharmony_ci ASSERT_TRUE(ret); 64148f512ceSopenharmony_ci ASSERT_LE(TEST_DWARF_FRAMES.size(), callFrames.size()); 64248f512ceSopenharmony_ci } 64348f512ceSopenharmony_ci} 64448f512ceSopenharmony_ci} // namespace HiPerf 64548f512ceSopenharmony_ci} // namespace Developtools 64648f512ceSopenharmony_ci} // namespace OHOS 647