• 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 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 #undef LOG_DOMAIN
33 #undef LOG_TAG
34 #define LOG_TAG "DfxUnwinderPacTest"
35 #define LOG_DOMAIN 0xD002D11
36 
37 class UnwinderPacTest : public testing::Test {
38 public:
SetUpTestCase()39     static void SetUpTestCase() {}
TearDownTestCase()40     static void TearDownTestCase() {}
SetUp()41     void SetUp() {}
TearDown()42     void TearDown() {}
43 };
44 
45 /**
46  * @tc.name: UnwinderPacTest001
47  * @tc.desc: test unwinder unwind interface with pac
48  * @tc.type: FUNC
49  */
50 HWTEST_F(UnwinderPacTest, UnwinderPacTest001, TestSize.Level2)
51 {
52     GTEST_LOG_(INFO) << "UnwinderPacTest001: start.";
53     static pid_t pid = getpid();
54     pid_t child = fork();
55     if (child == 0) {
56         GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
57         auto unwinder = std::make_shared<Unwinder>(pid);
58         bool unwRet = DfxPtrace::Attach(pid);
59         EXPECT_EQ(true, unwRet) << "UnwinderPacTest001: Attach:" << unwRet;
60         auto regs = DfxRegs::CreateRemoteRegs(pid);
61         unwinder->SetRegs(regs);
62         UnwindContext context;
63         context.pid = pid;
64         context.regs = regs;
65         unwRet = unwinder->Unwind(&context);
66         EXPECT_EQ(true, unwRet) << "UnwinderPacTest001: Unwind:" << unwRet;
67         auto frames = unwinder->GetFrames();
68         ASSERT_GT(frames.size(), 1);
69         GTEST_LOG_(INFO) << "frames:\n" << Unwinder::GetFramesStr(frames);
70         DfxPtrace::Detach(pid);
71         _exit(0);
72     }
73 
74     int status;
75     int ret = wait(&status);
76     ASSERT_EQ(status, 0);
77     GTEST_LOG_(INFO) << "Status:" << status << " Result:" << ret;
78     GTEST_LOG_(INFO) << "UnwinderPacTest001: end.";
79 }
80 
81 /**
82  * @tc.name: UnwinderPacTest002
83  * @tc.desc: test unwinder FpStep interface with pac
84  * @tc.type: FUNC
85  */
86 HWTEST_F(UnwinderPacTest, UnwinderPacTest002, TestSize.Level2)
87 {
88     GTEST_LOG_(INFO) << "UnwinderPacTest002: start.";
89 #if defined(__aarch64__)
90     static pid_t pid = getpid();
91     pid_t child = fork();
92     if (child == 0) {
93         GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
94         auto unwinder = std::make_shared<Unwinder>(pid);
95         bool unwRet = DfxPtrace::Attach(pid);
96         EXPECT_EQ(true, unwRet) << "UnwinderPacTest002: Attach:" << unwRet;
97         auto regs = DfxRegs::CreateRemoteRegs(pid);
98         unwinder->SetRegs(regs);
99         UnwindContext context;
100         context.pid = pid;
101         context.regs = regs;
102         unwRet = unwinder->UnwindByFp(&context);
103         ASSERT_TRUE(unwRet) << "UnwinderPacTest002: unwRet:" << unwRet;
104         DfxPtrace::Detach(pid);
105         _exit(0);
106     }
107 
108     int status;
109     int ret = wait(&status);
110     ASSERT_EQ(status, 0);
111     GTEST_LOG_(INFO) << "Status:" << status << " Result:" << ret;
112 #endif
113     GTEST_LOG_(INFO) << "UnwinderPacTest002: end.";
114 }
115 } // namespace HiviewDFX
116 } // namepsace OHOS