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_dump_request.h" 22 #include "dfx_thread.h" 23 #include "process_dumper.h" 24 #include "dfx_unwind_remote.h" 25 #include "dfx_util.h" 26 #include "dfx_test_util.h" 27 28 using namespace OHOS::HiviewDFX; 29 using namespace testing::ext; 30 using namespace std; 31 32 namespace OHOS { 33 namespace HiviewDFX { 34 class ProcessDumpTest : public testing::Test { 35 public: SetUpTestCase(void)36 static void SetUpTestCase(void) {} TearDownTestCase(void)37 static void TearDownTestCase(void) {} SetUp()38 void SetUp() {} TearDown()39 void TearDown() {} 40 }; 41 } // namespace HiviewDFX 42 } // namespace OHOS 43 44 namespace { 45 /** 46 * @tc.name: DfxProcessTest001 47 * @tc.desc: test DfxProcess Create 48 * @tc.type: FUNC 49 */ 50 HWTEST_F (ProcessDumpTest, DfxProcessTest001, TestSize.Level2) 51 { 52 GTEST_LOG_(INFO) << "DfxProcessTest001: start."; 53 std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid()); 54 EXPECT_EQ(false, process == nullptr) << "DfxProcessTest001 Failed"; 55 GTEST_LOG_(INFO) << "DfxProcessTest001: end."; 56 } 57 58 /** 59 * @tc.name: DfxProcessTest002 60 * @tc.desc: test init process threads 61 * @tc.type: FUNC 62 */ 63 HWTEST_F (ProcessDumpTest, DfxProcessTest002, TestSize.Level2) 64 { 65 GTEST_LOG_(INFO) << "DfxProcessTest002: start."; 66 pid_t accountmgrPid = GetProcessPid(ACCOUNTMGR_NAME); 67 if (accountmgrPid == 0) { 68 GTEST_LOG_(INFO) << "DfxProcessTest002: get pid failed."; 69 return; 70 } 71 pid_t pid = accountmgrPid; 72 pid_t tid = accountmgrPid; 73 auto keyThread = DfxThread::Create(pid, tid, tid); 74 auto process = DfxProcess::Create(pid, pid); 75 EXPECT_EQ(true, process != nullptr) << "DfxProcessTest002 Failed"; 76 GTEST_LOG_(INFO) << "DfxProcessTest002: end."; 77 } 78 79 /** 80 * @tc.name: DfxProcessTest003 81 * @tc.desc: test init other threads 82 * @tc.type: FUNC 83 */ 84 HWTEST_F (ProcessDumpTest, DfxProcessTest003, TestSize.Level2) 85 { 86 GTEST_LOG_(INFO) << "DfxProcessTest003: start."; 87 std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid()); 88 auto ret = process->InitOtherThreads(); 89 EXPECT_EQ(true, ret) << "DfxProcessTest003 Failed"; 90 auto threads = process->GetOtherThreads(); 91 EXPECT_GT(threads.size(), 0) << "DfxProcessTest003 Failed"; 92 process->ClearOtherThreads(); 93 threads = process->GetOtherThreads(); 94 EXPECT_EQ(threads.size(), 0) << "DfxProcessTest003 Failed"; 95 GTEST_LOG_(INFO) << "DfxProcessTest003: end."; 96 } 97 98 /** 99 * @tc.name: DfxProcessTest004 100 * @tc.desc: test Attach Detach 101 * @tc.type: FUNC 102 */ 103 HWTEST_F (ProcessDumpTest, DfxProcessTest004, TestSize.Level2) 104 { 105 GTEST_LOG_(INFO) << "DfxProcessTest004: start."; 106 std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid()); 107 auto ret = process->InitOtherThreads(); 108 EXPECT_EQ(true, ret) << "DfxProcessTest004 Failed"; 109 process->Attach(); 110 process->Detach(); 111 GTEST_LOG_(INFO) << "DfxProcessTest004: end."; 112 } 113 114 /** 115 * @tc.name: DfxProcessTest005 116 * @tc.desc: test get map 117 * @tc.type: FUNC 118 */ 119 HWTEST_F (ProcessDumpTest, DfxProcessTest005, TestSize.Level2) 120 { 121 GTEST_LOG_(INFO) << "DfxProcessTest005: start."; 122 std::shared_ptr<DfxProcess> process = DfxProcess::Create(getpid(), getpid()); 123 process->InitProcessMaps(); 124 auto output = process->GetMaps(); 125 EXPECT_EQ(true, output != nullptr) << "DfxProcessTest005 Failed"; 126 std::shared_ptr<DfxElfMaps> maps = std::make_shared<DfxElfMaps>(); 127 process->SetMaps(maps); 128 output = process->GetMaps(); 129 EXPECT_EQ(true, output == maps) << "DfxProcessTest005 Failed"; 130 GTEST_LOG_(INFO) << "DfxProcessTest005: end."; 131 } 132 133 /** 134 * @tc.name: DfxThreadTest001 135 * @tc.desc: test DfxThread Create 136 * @tc.type: FUNC 137 */ 138 HWTEST_F (ProcessDumpTest, DfxThreadTest001, TestSize.Level2) 139 { 140 GTEST_LOG_(INFO) << "DfxThreadTest001: start."; 141 int32_t pid = 1, tid = 1; 142 auto thread = DfxThread::Create(pid, tid, tid); 143 EXPECT_EQ(true, thread != nullptr) << "DfxThreadTest001 failed"; 144 GTEST_LOG_(INFO) << "DfxThreadTest001: end."; 145 } 146 147 /** 148 * @tc.name: DfxThreadTest002 149 * @tc.desc: test DfxThread GetThreadRegs 150 * @tc.type: FUNC 151 */ 152 HWTEST_F (ProcessDumpTest, DfxThreadTest002, TestSize.Level2) 153 { 154 GTEST_LOG_(INFO) << "DfxThreadTest002: start."; 155 int32_t pid = 243, tid = 243; 156 std::shared_ptr<DfxThread> thread = std::make_shared<DfxThread>(pid, tid, tid); 157 std::shared_ptr<DfxRegs> inputrefs; 158 thread->SetThreadRegs(inputrefs); 159 std::shared_ptr<DfxRegs> outputrefs = thread->GetThreadRegs(); 160 EXPECT_EQ(true, inputrefs == outputrefs) << "DfxThreadTest002 Failed"; 161 GTEST_LOG_(INFO) << "DfxThreadTest002: end."; 162 } 163 164 /** 165 * @tc.name: DfxUnwindRemoteTest001 166 * @tc.desc: test DfxUnwindRemote UnwindProcess 167 * @tc.type: FUNC 168 */ 169 HWTEST_F (ProcessDumpTest, DfxUnwindRemoteTest001, TestSize.Level2) 170 { 171 GTEST_LOG_(INFO) << "DfxUnwindRemoteTest001: start."; 172 pid_t pid = GetProcessPid(ACCOUNTMGR_NAME); 173 pid_t tid = pid; 174 std::shared_ptr<DfxThread> thread = DfxThread::Create(pid, tid, tid); 175 std::shared_ptr<DfxProcess> process = DfxProcess::Create(pid, pid); 176 process->keyThread_ = thread; 177 thread->Attach(); 178 bool ret = DfxUnwindRemote::GetInstance().UnwindProcess(process); 179 thread->Detach(); 180 EXPECT_EQ(true, ret) << "DfxUnwindRemoteTest001 Failed"; 181 GTEST_LOG_(INFO) << "DfxUnwindRemoteTest001: end."; 182 } 183 184 /** 185 * @tc.name: DfxUnwindRemoteTest002 186 * @tc.desc: test UnwindThread 187 * @tc.type: FUNC 188 */ 189 HWTEST_F (ProcessDumpTest, DfxUnwindRemoteTest002, TestSize.Level2) 190 { 191 GTEST_LOG_(INFO) << "DfxUnwindRemoteTest002: start."; 192 pid_t pid = GetProcessPid(FOUNDATION_NAME); 193 pid_t tid = pid; 194 std::shared_ptr<DfxThread> thread = DfxThread::Create(pid, tid, tid); 195 std::shared_ptr<DfxProcess> process = DfxProcess::Create(pid, pid); 196 process->keyThread_ = thread; 197 thread->Attach(); 198 bool ret = DfxUnwindRemote::GetInstance().UnwindThread(process, thread); 199 thread->Detach(); 200 EXPECT_EQ(true, ret) << "DfxUnwindRemoteTest002 Failed"; 201 GTEST_LOG_(INFO) << "DfxUnwindRemoteTest002: end."; 202 } 203 204 /** 205 * @tc.name: DfxUnwindRemoteTest003 206 * @tc.desc: test UnwindThreadFallback 207 * @tc.type: FUNC 208 */ 209 HWTEST_F (ProcessDumpTest, DfxUnwindRemoteTest003, TestSize.Level2) 210 { 211 GTEST_LOG_(INFO) << "DfxUnwindRemoteTest003: start."; 212 pid_t pid = GetProcessPid(FOUNDATION_NAME); 213 pid_t tid = pid; 214 std::shared_ptr<DfxThread> thread = DfxThread::Create(pid, tid, tid); 215 std::shared_ptr<DfxProcess> process = DfxProcess::Create(pid, pid); 216 auto dfxregs = DfxRegs::Create(); 217 uintptr_t regs[FP_MINI_REGS_SIZE] = {0}; 218 dfxregs->GetFramePointerMiniRegs(regs); 219 dfxregs->fp_ = regs[0]; // 0 : index of x29 or r11 register 220 dfxregs->pc_ = regs[3]; // 3 : index of x32 or r15 register 221 thread->SetThreadRegs(dfxregs); 222 process->keyThread_ = thread; 223 thread->Attach(); 224 DfxUnwindRemote::GetInstance().UnwindThreadFallback(process, thread); 225 thread->Detach(); 226 EXPECT_EQ(thread->GetFrames().size(), 2) << "DfxUnwindRemoteTest003 Failed"; 227 GTEST_LOG_(INFO) << "DfxUnwindRemoteTest003: end."; 228 } 229 } 230