• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include <memory>
18 #include <string>
19 
20 #include "dfx_regs.h"
21 #include "dfx_regs_get.h"
22 #include "dfx_dump_request.h"
23 #include "dfx_thread.h"
24 #include "process_dumper.h"
25 #include "dfx_unwind_remote.h"
26 #include "dfx_util.h"
27 #include "dfx_test_util.h"
28 
29 using namespace OHOS::HiviewDFX;
30 using namespace testing::ext;
31 using namespace std;
32 
33 namespace OHOS {
34 namespace HiviewDFX {
35 class ProcessDumpTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void) {}
TearDownTestCase(void)38     static void TearDownTestCase(void) {}
SetUp()39     void SetUp() {}
TearDown()40     void TearDown() {}
41 };
42 } // namespace HiviewDFX
43 } // namespace OHOS
44 
45 namespace {
46 /**
47  * @tc.name: DfxProcessTest001
48  * @tc.desc: test DfxProcess Create
49  * @tc.type: FUNC
50  */
51 HWTEST_F (ProcessDumpTest, DfxProcessTest001, TestSize.Level2)
52 {
53     GTEST_LOG_(INFO) << "DfxProcessTest001: start.";
54     std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid());
55     EXPECT_EQ(false, process == nullptr) << "DfxProcessTest001 Failed";
56     GTEST_LOG_(INFO) << "DfxProcessTest001: end.";
57 }
58 
59 /**
60  * @tc.name: DfxProcessTest002
61  * @tc.desc: test init process threads
62  * @tc.type: FUNC
63  */
64 HWTEST_F (ProcessDumpTest, DfxProcessTest002, TestSize.Level2)
65 {
66     GTEST_LOG_(INFO) << "DfxProcessTest002: start.";
67     pid_t accountmgrPid = GetProcessPid(ACCOUNTMGR_NAME);
68     if (accountmgrPid == 0) {
69         GTEST_LOG_(INFO) << "DfxProcessTest002: get pid failed.";
70         return;
71     }
72     pid_t pid = accountmgrPid;
73     pid_t tid = accountmgrPid;
74     auto keyThread = DfxThread::Create(pid, tid, tid);
75     auto process = DfxProcess::Create(pid, pid);
76     EXPECT_EQ(true, process != nullptr) << "DfxProcessTest002 Failed";
77     GTEST_LOG_(INFO) << "DfxProcessTest002: end.";
78 }
79 
80 /**
81  * @tc.name: DfxProcessTest003
82  * @tc.desc: test init other threads
83  * @tc.type: FUNC
84  */
85 HWTEST_F (ProcessDumpTest, DfxProcessTest003, TestSize.Level2)
86 {
87     GTEST_LOG_(INFO) << "DfxProcessTest003: start.";
88     std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid());
89     auto ret = process->InitOtherThreads();
90     EXPECT_EQ(true, ret) << "DfxProcessTest003 Failed";
91     auto threads = process->GetOtherThreads();
92     EXPECT_GT(threads.size(), 0) << "DfxProcessTest003 Failed";
93     process->ClearOtherThreads();
94     threads = process->GetOtherThreads();
95     EXPECT_EQ(threads.size(), 0) << "DfxProcessTest003 Failed";
96     GTEST_LOG_(INFO) << "DfxProcessTest003: end.";
97 }
98 
99 /**
100  * @tc.name: DfxProcessTest004
101  * @tc.desc: test Attach Detach
102  * @tc.type: FUNC
103  */
104 HWTEST_F (ProcessDumpTest, DfxProcessTest004, TestSize.Level2)
105 {
106     GTEST_LOG_(INFO) << "DfxProcessTest004: start.";
107     std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid());
108     auto ret = process->InitOtherThreads();
109     EXPECT_EQ(true, ret) << "DfxProcessTest004 Failed";
110     process->Attach();
111     process->Detach();
112     GTEST_LOG_(INFO) << "DfxProcessTest004: end.";
113 }
114 
115 /**
116  * @tc.name: DfxThreadTest001
117  * @tc.desc: test DfxThread Create
118  * @tc.type: FUNC
119  */
120 HWTEST_F (ProcessDumpTest, DfxThreadTest001, TestSize.Level2)
121 {
122     GTEST_LOG_(INFO) << "DfxThreadTest001: start.";
123     int32_t pid = 1, tid = 1;
124     auto thread = DfxThread::Create(pid, tid, tid);
125     EXPECT_EQ(true, thread != nullptr) << "DfxThreadTest001 failed";
126     GTEST_LOG_(INFO) << "DfxThreadTest001: end.";
127 }
128 
129 /**
130  * @tc.name: DfxThreadTest002
131  * @tc.desc: test DfxThread GetThreadRegs
132  * @tc.type: FUNC
133  */
134 HWTEST_F (ProcessDumpTest, DfxThreadTest002, TestSize.Level2)
135 {
136     GTEST_LOG_(INFO) << "DfxThreadTest002: start.";
137     int32_t pid = 243, tid = 243;
138     std::shared_ptr<DfxThread> thread = std::make_shared<DfxThread>(pid, tid, tid);
139     std::shared_ptr<DfxRegs> inputrefs;
140     thread->SetThreadRegs(inputrefs);
141     std::shared_ptr<DfxRegs> outputrefs = thread->GetThreadRegs();
142     EXPECT_EQ(true, inputrefs == outputrefs) << "DfxThreadTest002 Failed";
143     GTEST_LOG_(INFO) << "DfxThreadTest002: end.";
144 }
145 
146 /**
147  * @tc.name: DfxUnwindRemoteTest001
148  * @tc.desc: test DfxUnwindRemote UnwindProcess
149  * @tc.type: FUNC
150  */
151 HWTEST_F (ProcessDumpTest, DfxUnwindRemoteTest001, TestSize.Level2)
152 {
153     GTEST_LOG_(INFO) << "DfxUnwindRemoteTest001: start.";
154     pid_t pid = GetProcessPid(ACCOUNTMGR_NAME);
155     pid_t tid = pid;
156     std::shared_ptr<DfxThread> thread = DfxThread::Create(pid, tid, tid);
157     std::shared_ptr<DfxProcess> process = DfxProcess::Create(pid, pid);
158     auto unwinder = std::make_shared<Unwinder>(pid);
159     process->keyThread_ = thread;
160     thread->Attach();
161     thread->SetThreadRegs(DfxRegs::CreateRemoteRegs(pid));
162     std::shared_ptr<ProcessDumpRequest> request = std::make_shared<ProcessDumpRequest>();
163     bool ret = DfxUnwindRemote::GetInstance().UnwindProcess(request, process, unwinder);
164     thread->Detach();
165     EXPECT_EQ(true, ret) << "DfxUnwindRemoteTest001 Failed";
166     GTEST_LOG_(INFO) << "DfxUnwindRemoteTest001: end.";
167 }
168 }
169