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 #ifndef is_linux
44 /**
45 * @tc.name: UnwindCallStack
46 * @tc.desc:
47 * @tc.type: FUNC
48 */
49 HWTEST_F(CallStackTest, UnwindCallStack, TestSize.Level1)
50 {
51 std::vector<u64> regs;
52 std::vector<u8> data;
53 LoadFromFile(PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_USER_REGS_0, regs);
54 LoadFromFile(PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_USER_DATA_0, data);
55 if (regs.size() > 0 and data.size() > 0) {
56 #ifdef __arm__
57 ASSERT_EQ(regs.size(), 16u);
58 #endif
59 std::unordered_map<std::string, std::unique_ptr<SymbolsFile>> symbolsFiles;
60 std::string symbolFilePath = PATH_RESOURCE_TEST_DWARF_DATA + TEST_DWARF_ELF;
61 symbolsFiles[symbolFilePath] = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, symbolFilePath);
62 auto& symbolsFile = symbolsFiles[symbolFilePath];
63 ASSERT_EQ(symbolsFile->LoadSymbols(), true);
64 // fix the name
65 symbolsFile->filePath_ = TEST_DWARF_MMAP.front().fileName;
66 std::shared_ptr<VirtualRuntime> runtime = std::make_shared<VirtualRuntime>();
67 VirtualThread thread(getpid(), GetThreadId(), symbolsFiles, runtime.get(), false);
68 MakeMaps(thread);
69 std::vector<CallFrame> callFrames;
70 CallStack callStack;
71 callStack.UnwindCallStack(thread, regs.data(), regs.size(), data.data(), data.size(), callFrames);
72 #ifdef __arm__
73 bool ret = callStack.UnwindCallStack(thread, regs.data(), regs.size(), data.data(), data.size(), callFrames);
74 ASSERT_TRUE(ret);
75 #endif
76 }
77 }
78 #endif
79 } // namespace NativeDaemon
80 } // namespace Developtools
81 } // namespace OHOS
82