1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 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
18800b99b8Sopenharmony_ci#include <cstdio>
19800b99b8Sopenharmony_ci#include <thread>
20800b99b8Sopenharmony_ci#include <unistd.h>
21800b99b8Sopenharmony_ci#include <malloc.h>
22800b99b8Sopenharmony_ci#include <securec.h>
23800b99b8Sopenharmony_ci#include "dfx_ptrace.h"
24800b99b8Sopenharmony_ci#include "dfx_regs_get.h"
25800b99b8Sopenharmony_ci#include "unwinder.h"
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_ciusing namespace testing;
28800b99b8Sopenharmony_ciusing namespace testing::ext;
29800b99b8Sopenharmony_ci
30800b99b8Sopenharmony_cinamespace OHOS {
31800b99b8Sopenharmony_cinamespace HiviewDFX {
32800b99b8Sopenharmony_ci#undef LOG_DOMAIN
33800b99b8Sopenharmony_ci#undef LOG_TAG
34800b99b8Sopenharmony_ci#define LOG_TAG "DfxUnwinderPacTest"
35800b99b8Sopenharmony_ci#define LOG_DOMAIN 0xD002D11
36800b99b8Sopenharmony_ci
37800b99b8Sopenharmony_ciclass UnwinderPacTest : public testing::Test {
38800b99b8Sopenharmony_cipublic:
39800b99b8Sopenharmony_ci    static void SetUpTestCase() {}
40800b99b8Sopenharmony_ci    static void TearDownTestCase() {}
41800b99b8Sopenharmony_ci    void SetUp() {}
42800b99b8Sopenharmony_ci    void TearDown() {}
43800b99b8Sopenharmony_ci};
44800b99b8Sopenharmony_ci
45800b99b8Sopenharmony_ci/**
46800b99b8Sopenharmony_ci * @tc.name: UnwinderPacTest001
47800b99b8Sopenharmony_ci * @tc.desc: test unwinder unwind interface with pac
48800b99b8Sopenharmony_ci * @tc.type: FUNC
49800b99b8Sopenharmony_ci */
50800b99b8Sopenharmony_ciHWTEST_F(UnwinderPacTest, UnwinderPacTest001, TestSize.Level2)
51800b99b8Sopenharmony_ci{
52800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "UnwinderPacTest001: start.";
53800b99b8Sopenharmony_ci    static pid_t pid = getpid();
54800b99b8Sopenharmony_ci    pid_t child = fork();
55800b99b8Sopenharmony_ci    if (child == 0) {
56800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
57800b99b8Sopenharmony_ci        auto unwinder = std::make_shared<Unwinder>(pid);
58800b99b8Sopenharmony_ci        bool unwRet = DfxPtrace::Attach(pid);
59800b99b8Sopenharmony_ci        EXPECT_EQ(true, unwRet) << "UnwinderPacTest001: Attach:" << unwRet;
60800b99b8Sopenharmony_ci        auto regs = DfxRegs::CreateRemoteRegs(pid);
61800b99b8Sopenharmony_ci        auto maps = DfxMaps::Create(pid);
62800b99b8Sopenharmony_ci        unwinder->SetRegs(regs);
63800b99b8Sopenharmony_ci        UnwindContext context;
64800b99b8Sopenharmony_ci        context.pid = pid;
65800b99b8Sopenharmony_ci        context.regs = regs;
66800b99b8Sopenharmony_ci        context.maps = maps;
67800b99b8Sopenharmony_ci        unwRet = unwinder->Unwind(&context);
68800b99b8Sopenharmony_ci        EXPECT_EQ(true, unwRet) << "UnwinderPacTest001: Unwind:" << unwRet;
69800b99b8Sopenharmony_ci        auto frames = unwinder->GetFrames();
70800b99b8Sopenharmony_ci        ASSERT_GT(frames.size(), 1);
71800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "frames:\n" << Unwinder::GetFramesStr(frames);
72800b99b8Sopenharmony_ci        DfxPtrace::Detach(pid);
73800b99b8Sopenharmony_ci        _exit(0);
74800b99b8Sopenharmony_ci    }
75800b99b8Sopenharmony_ci
76800b99b8Sopenharmony_ci    int status;
77800b99b8Sopenharmony_ci    int ret = wait(&status);
78800b99b8Sopenharmony_ci    ASSERT_EQ(status, 0);
79800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "Status:" << status << " Result:" << ret;
80800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "UnwinderPacTest001: end.";
81800b99b8Sopenharmony_ci}
82800b99b8Sopenharmony_ci
83800b99b8Sopenharmony_ci/**
84800b99b8Sopenharmony_ci * @tc.name: UnwinderPacTest002
85800b99b8Sopenharmony_ci * @tc.desc: test unwinder FpStep interface with pac
86800b99b8Sopenharmony_ci * @tc.type: FUNC
87800b99b8Sopenharmony_ci */
88800b99b8Sopenharmony_ciHWTEST_F(UnwinderPacTest, UnwinderPacTest002, TestSize.Level2)
89800b99b8Sopenharmony_ci{
90800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "UnwinderPacTest002: start.";
91800b99b8Sopenharmony_ci#if defined(__aarch64__)
92800b99b8Sopenharmony_ci    static pid_t pid = getpid();
93800b99b8Sopenharmony_ci    pid_t child = fork();
94800b99b8Sopenharmony_ci    if (child == 0) {
95800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
96800b99b8Sopenharmony_ci        auto unwinder = std::make_shared<Unwinder>(pid);
97800b99b8Sopenharmony_ci        bool unwRet = DfxPtrace::Attach(pid);
98800b99b8Sopenharmony_ci        EXPECT_EQ(true, unwRet) << "UnwinderPacTest002: Attach:" << unwRet;
99800b99b8Sopenharmony_ci        auto regs = DfxRegs::CreateRemoteRegs(pid);
100800b99b8Sopenharmony_ci        unwinder->SetRegs(regs);
101800b99b8Sopenharmony_ci        UnwindContext context;
102800b99b8Sopenharmony_ci        context.pid = pid;
103800b99b8Sopenharmony_ci        context.regs = regs;
104800b99b8Sopenharmony_ci        unwRet = unwinder->UnwindByFp(&context);
105800b99b8Sopenharmony_ci        ASSERT_TRUE(unwRet) << "UnwinderPacTest002: unwRet:" << unwRet;
106800b99b8Sopenharmony_ci        DfxPtrace::Detach(pid);
107800b99b8Sopenharmony_ci        _exit(0);
108800b99b8Sopenharmony_ci    }
109800b99b8Sopenharmony_ci
110800b99b8Sopenharmony_ci    int status;
111800b99b8Sopenharmony_ci    int ret = wait(&status);
112800b99b8Sopenharmony_ci    ASSERT_EQ(status, 0);
113800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "Status:" << status << " Result:" << ret;
114800b99b8Sopenharmony_ci#endif
115800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "UnwinderPacTest002: end.";
116800b99b8Sopenharmony_ci}
117800b99b8Sopenharmony_ci} // namespace HiviewDFX
118800b99b8Sopenharmony_ci} // namepsace OHOS