1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 "callstack_test.h"
17 #include "virtual_runtime.h"
18 #include "get_thread_id.h"
19 using namespace testing::ext;
20 using namespace std;
21 using namespace OHOS::HiviewDFX;
22 namespace OHOS {
23 namespace Developtools {
24 namespace NativeDaemon {
25 class CallStackTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 default_random_engine rnd_;
32 };
33
SetUpTestCase()34 void CallStackTest::SetUpTestCase() {}
35
TearDownTestCase()36 void CallStackTest::TearDownTestCase() {}
37
SetUp()38 void CallStackTest::SetUp() {}
39
TearDown()40 void CallStackTest::TearDown() {}
41
42
43 /**
44 * @tc.name: UnwindCallStack
45 * @tc.desc:
46 * @tc.type: FUNC
47 */
HWTEST_F(CallStackTest, UnwindCallStack, TestSize.Level1)48 HWTEST_F(CallStackTest, UnwindCallStack, TestSize.Level1)
49 {
50 #if is_linux
51 return;
52 #endif
53
54 std::vector<u64> regs;
55 std::vector<u8> data;
56 LoadFromFile(PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_USER_REGS_0, regs);
57 LoadFromFile(PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_USER_DATA_0, data);
58 if (regs.size() > 0 and data.size() > 0) {
59 #ifdef __arm__
60 ASSERT_EQ(regs.size(), 16u);
61 #endif
62 std::unordered_map<std::string, std::unique_ptr<SymbolsFile>> symbolsFiles;
63 std::string symbolFilePath = PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_ELF;
64 symbolsFiles[symbolFilePath] = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, symbolFilePath);
65 auto& symbolsFile = symbolsFiles[symbolFilePath];
66 ASSERT_EQ(symbolsFile->LoadSymbols(), true);
67 // fix the name
68 symbolsFile->filePath_ = TEST_DWARF_MMAP.front().fileName;
69 std::shared_ptr<VirtualRuntime> runtime = std::make_shared<VirtualRuntime>();
70 VirtualThread thread(getpid(), GetThreadId(), symbolsFiles, runtime.get(), false);
71 MakeMaps(thread);
72 std::vector<CallFrame> callFrames;
73 CallStack callStack;
74 callStack.UnwindCallStack(thread, regs.data(), regs.size(), data.data(), data.size(), callFrames);
75 #ifdef __arm__
76 bool ret = callStack.UnwindCallStack(thread, regs.data(), regs.size(), data.data(), data.size(), callFrames);
77 ASSERT_TRUE(ret);
78 #endif
79 }
80 }
81 } // namespace NativeDaemon
82 } // namespace Developtools
83 } // namespace OHOS
84