1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci#include "fp_unwinder.h"
18800b99b8Sopenharmony_ci#include "unwinder.h"
19800b99b8Sopenharmony_ci
20800b99b8Sopenharmony_ciusing namespace testing;
21800b99b8Sopenharmony_ciusing namespace testing::ext;
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_cinamespace OHOS {
24800b99b8Sopenharmony_cinamespace HiviewDFX {
25800b99b8Sopenharmony_ci#undef LOG_DOMAIN
26800b99b8Sopenharmony_ci#undef LOG_TAG
27800b99b8Sopenharmony_ci#define LOG_TAG "DfxFpUnwinderTest"
28800b99b8Sopenharmony_ci#define LOG_DOMAIN 0xD002D11
29800b99b8Sopenharmony_ci
30800b99b8Sopenharmony_ciclass FpUnwinderTest : public testing::Test {
31800b99b8Sopenharmony_cipublic:
32800b99b8Sopenharmony_ci    static void SetUpTestCase() {}
33800b99b8Sopenharmony_ci    static void TearDownTestCase() {}
34800b99b8Sopenharmony_ci    void SetUp() {}
35800b99b8Sopenharmony_ci    void TearDown() {}
36800b99b8Sopenharmony_ci};
37800b99b8Sopenharmony_ci
38800b99b8Sopenharmony_ci/**
39800b99b8Sopenharmony_ci * @tc.name: FpUnwinderTest001
40800b99b8Sopenharmony_ci * @tc.desc: test fp unwinder Unwind interface
41800b99b8Sopenharmony_ci * @tc.type: FUNC
42800b99b8Sopenharmony_ci */
43800b99b8Sopenharmony_ciHWTEST_F(FpUnwinderTest, FpUnwinderTest001, TestSize.Level2)
44800b99b8Sopenharmony_ci{
45800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FpUnwinderTest001: start.";
46800b99b8Sopenharmony_ci#if defined(__aarch64__)
47800b99b8Sopenharmony_ci    uintptr_t regs[2]; // 2: pc and fp reg
48800b99b8Sopenharmony_ci    FpUnwinder::GetPcFpRegs(regs);
49800b99b8Sopenharmony_ci    uintptr_t pc = regs[0];
50800b99b8Sopenharmony_ci    uintptr_t fp = regs[1];
51800b99b8Sopenharmony_ci    const size_t maxSize = 32;
52800b99b8Sopenharmony_ci    uintptr_t pcs[maxSize] = {0};
53800b99b8Sopenharmony_ci    auto unwSz = FpUnwinder::GetPtr()->Unwind(pc, fp, pcs, maxSize);
54800b99b8Sopenharmony_ci    ASSERT_GT(unwSz, 1);
55800b99b8Sopenharmony_ci
56800b99b8Sopenharmony_ci    std::vector<DfxFrame> frames;
57800b99b8Sopenharmony_ci    for (auto i = 0; i < unwSz; ++i) {
58800b99b8Sopenharmony_ci        DfxFrame frame;
59800b99b8Sopenharmony_ci        frame.index = i;
60800b99b8Sopenharmony_ci        frame.pc = static_cast<uint64_t>(pcs[i]);
61800b99b8Sopenharmony_ci        frames.emplace_back(frame);
62800b99b8Sopenharmony_ci    }
63800b99b8Sopenharmony_ci    ASSERT_GT(frames.size(), 1);
64800b99b8Sopenharmony_ci    Unwinder::FillLocalFrames(frames);
65800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FpUnwinderTest001: frames:\n" << Unwinder::GetFramesStr(frames);
66800b99b8Sopenharmony_ci#endif
67800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FpUnwinderTest001: end.";
68800b99b8Sopenharmony_ci}
69800b99b8Sopenharmony_ci
70800b99b8Sopenharmony_ci/**
71800b99b8Sopenharmony_ci * @tc.name: FpUnwinderTest002
72800b99b8Sopenharmony_ci * @tc.desc: test fp unwinder UnwindSafe interface
73800b99b8Sopenharmony_ci * @tc.type: FUNC
74800b99b8Sopenharmony_ci */
75800b99b8Sopenharmony_ciHWTEST_F(FpUnwinderTest, FpUnwinderTest002, TestSize.Level2)
76800b99b8Sopenharmony_ci{
77800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FpUnwinderTest002: start.";
78800b99b8Sopenharmony_ci#if defined(__aarch64__)
79800b99b8Sopenharmony_ci    uintptr_t regs[2]; // 2: pc and fp reg
80800b99b8Sopenharmony_ci    FpUnwinder::GetPcFpRegs(regs);
81800b99b8Sopenharmony_ci    uintptr_t pc = regs[0];
82800b99b8Sopenharmony_ci    uintptr_t fp = regs[1];
83800b99b8Sopenharmony_ci    const size_t maxSize = 32;
84800b99b8Sopenharmony_ci    uintptr_t pcs[maxSize] = {0};
85800b99b8Sopenharmony_ci    auto unwSz = FpUnwinder::GetPtr()->UnwindSafe(pc, fp, pcs, maxSize);
86800b99b8Sopenharmony_ci    ASSERT_GT(unwSz, 1);
87800b99b8Sopenharmony_ci
88800b99b8Sopenharmony_ci    std::vector<DfxFrame> frames;
89800b99b8Sopenharmony_ci    for (auto i = 0; i < unwSz; ++i) {
90800b99b8Sopenharmony_ci        DfxFrame frame;
91800b99b8Sopenharmony_ci        frame.index = i;
92800b99b8Sopenharmony_ci        frame.pc = static_cast<uint64_t>(pcs[i]);
93800b99b8Sopenharmony_ci        frames.emplace_back(frame);
94800b99b8Sopenharmony_ci    }
95800b99b8Sopenharmony_ci    ASSERT_GT(frames.size(), 1);
96800b99b8Sopenharmony_ci    Unwinder::FillLocalFrames(frames);
97800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FpUnwinderTest002: frames:\n" << Unwinder::GetFramesStr(frames);
98800b99b8Sopenharmony_ci#endif
99800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FpUnwinderTest002: end.";
100800b99b8Sopenharmony_ci}
101800b99b8Sopenharmony_ci} // namespace HiviewDFX
102800b99b8Sopenharmony_ci} // namepsace OHOS