• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 <gtest/gtest.h>
17 
18 #include <cstdio>
19 #include <thread>
20 #include <unistd.h>
21 #include <malloc.h>
22 #include <securec.h>
23 #include "dfx_ptrace.h"
24 #include "dfx_regs_get.h"
25 #include "unwinder.h"
26 #include "dfx_test_util.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 #undef LOG_DOMAIN
34 #undef LOG_TAG
35 #define LOG_TAG "DfxUnwinderPacTest"
36 #define LOG_DOMAIN 0xD002D11
37 
38 class UnwinderPacTest : public testing::Test {
39 public:
SetUpTestCase()40     static void SetUpTestCase() {}
TearDownTestCase()41     static void TearDownTestCase() {}
SetUp()42     void SetUp() {}
TearDown()43     void TearDown() {}
44 };
45 
46 /**
47  * @tc.name: UnwinderPacTest001
48  * @tc.desc: test unwinder unwind interface with pac
49  * @tc.type: FUNC
50  */
51 HWTEST_F(UnwinderPacTest, UnwinderPacTest001, TestSize.Level0)
52 {
53     GTEST_LOG_(INFO) << "UnwinderPacTest001: start.";
54     static pid_t pid = getpid();
55     pid_t child = fork();
56     if (child == 0) {
57         GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
58         auto unwinder = std::make_shared<Unwinder>(pid);
59         bool unwRet = DfxPtrace::Attach(pid);
60         EXPECT_EQ(true, unwRet) << "UnwinderPacTest001: Attach:" << unwRet;
61         auto regs = DfxRegs::CreateRemoteRegs(pid);
62         auto maps = DfxMaps::Create(pid);
63         unwinder->SetRegs(regs);
64         UnwindContext context;
65         context.pid = pid;
66         context.regs = regs;
67         context.maps = maps;
68         unwRet = unwinder->Unwind(&context);
69         EXPECT_EQ(true, unwRet) << "UnwinderPacTest001: Unwind:" << unwRet;
70         auto frames = unwinder->GetFrames();
71         EXPECT_GT(frames.size(), 1);
72         GTEST_LOG_(INFO) << "frames:\n" << Unwinder::GetFramesStr(frames);
73         DfxPtrace::Detach(pid);
74         CheckAndExit(HasFailure());
75     }
76     int status;
77     wait(&status);
78     ASSERT_EQ(status, 0);
79     GTEST_LOG_(INFO) << "UnwinderPacTest001: end.";
80 }
81 
82 /**
83  * @tc.name: UnwinderPacTest002
84  * @tc.desc: test unwinder FpStep interface with pac
85  * @tc.type: FUNC
86  */
87 HWTEST_F(UnwinderPacTest, UnwinderPacTest002, TestSize.Level2)
88 {
89     GTEST_LOG_(INFO) << "UnwinderPacTest002: start.";
90 #if defined(__aarch64__)
91     static pid_t pid = getpid();
92     pid_t child = fork();
93     if (child == 0) {
94         GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
95         auto unwinder = std::make_shared<Unwinder>(pid);
96         bool unwRet = DfxPtrace::Attach(pid);
97         EXPECT_EQ(true, unwRet) << "UnwinderPacTest002: Attach:" << unwRet;
98         auto regs = DfxRegs::CreateRemoteRegs(pid);
99         unwinder->SetRegs(regs);
100         UnwindContext context;
101         context.pid = pid;
102         context.regs = regs;
103         unwRet = unwinder->UnwindByFp(&context);
104         EXPECT_TRUE(unwRet) << "UnwinderPacTest002: unwRet:" << unwRet;
105         DfxPtrace::Detach(pid);
106         CheckAndExit(HasFailure());
107     }
108     int status;
109     wait(&status);
110     ASSERT_EQ(status, 0);
111 #endif
112     GTEST_LOG_(INFO) << "UnwinderPacTest002: end.";
113 }
114 } // namespace HiviewDFX
115 } // namepsace OHOS