1 /*
2  * Copyright (c) 2022-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 <fstream>
18 #include <map>
19 #include <csignal>
20 #include <dlfcn.h>
21 #include <string>
22 #include <syscall.h>
23 #include <unistd.h>
24 #include <vector>
25 
26 #include "dfx_config.h"
27 #include "dfx_define.h"
28 #include "dfx_logger.h"
29 #include "dfx_test_util.h"
30 #include "dfx_util.h"
31 #include "directory_ex.h"
32 #include "dfx_socket_request.h"
33 #include "multithread_constructor.h"
34 #include "process_dumper.h"
35 #include "faultlogger_client_msg.h"
36 
37 using namespace OHOS::HiviewDFX;
38 using namespace testing::ext;
39 using namespace std;
40 
41 using RecordAppExitReason = int (*)(int reason, const char *exitMsg);
42 
43 namespace OHOS {
44 namespace HiviewDFX {
45 class DfxProcessDumpTest : public testing::Test {
46 public:
47     static void SetUpTestCase(void);
48     static void TearDownTestCase(void);
49     void SetUp();
50     void TearDown();
51 };
52 } // namespace HiviewDFX
53 } // namespace OHOS
54 
SetUpTestCase(void)55 void DfxProcessDumpTest::SetUpTestCase(void)
56 {
57 }
58 
TearDownTestCase(void)59 void DfxProcessDumpTest::TearDownTestCase(void)
60 {
61 }
62 
SetUp(void)63 void DfxProcessDumpTest::SetUp(void)
64 {
65 }
66 
TearDown(void)67 void DfxProcessDumpTest::TearDown(void)
68 {
69 }
70 
CreateMultiThreadProcess(int threadNum)71 static pid_t CreateMultiThreadProcess(int threadNum)
72 {
73     pid_t pid = fork();
74     if (pid < 0) {
75         GTEST_LOG_(ERROR) << "Failed to fork new test process.";
76     } else if (pid == 0) {
77         (void)MultiThreadConstructor(threadNum);
78     }
79     return pid;
80 }
81 
CreateMultiThreadForThreadCrash(int threadNum)82 static pid_t CreateMultiThreadForThreadCrash(int threadNum)
83 {
84     pid_t pid = fork();
85     if (pid < 0) {
86         GTEST_LOG_(ERROR) << "Failed to fork new test process.";
87     } else if (pid == 0) {
88         (void)MultiThreadConstructorForThreadCrash(threadNum);
89     }
90     return pid;
91 }
92 
CreateMultiThreadForThreadCrashWithOpen(int threadNum, int openNum)93 static pid_t CreateMultiThreadForThreadCrashWithOpen(int threadNum, int openNum)
94 {
95     pid_t pid = fork();
96     if (pid < 0) {
97         GTEST_LOG_(ERROR) << "Failed to fork new test process.";
98     } else if (pid == 0) {
99         for (int i = 0; i < openNum; ++i) {
100             fopen("/dev/null", "r");
101         }
102         (void)MultiThreadConstructorForThreadCrash(threadNum);
103     }
104     return pid;
105 }
106 
CheckCppCrashKeyWords(const string& filePath, pid_t pid, int sig)107 static bool CheckCppCrashKeyWords(const string& filePath, pid_t pid, int sig)
108 {
109     if (filePath.empty() || pid <= 0) {
110         return false;
111     }
112     map<int, string> sigKey = {
113         { SIGILL, string("SIGILL") },
114         { SIGTRAP, string("SIGTRAP") },
115         { SIGABRT, string("SIGABRT") },
116         { SIGBUS, string("SIGBUS") },
117         { SIGFPE, string("SIGFPE") },
118         { SIGSEGV, string("SIGSEGV") },
119         { SIGSTKFLT, string("SIGSTKFLT") },
120         { SIGSYS, string("SIGSYS") },
121     };
122     string sigKeyword = "";
123     map<int, string>::iterator iter = sigKey.find(sig);
124     if (iter != sigKey.end()) {
125         sigKeyword = iter->second;
126     }
127     string keywords[] = {
128         "Pid:" + to_string(pid), "Uid:", "test_processdump", sigKeyword, "Tid:", "#00", "Registers:", REGISTERS,
129         "FaultStack:", "Maps:", "test_processdump"
130     };
131     int length = sizeof(keywords) / sizeof(keywords[0]);
132     int minRegIdx = 6; // 6 : index of REGISTERS
133     int count = CheckKeyWords(filePath, keywords, length, minRegIdx);
134     return count == length;
135 }
136 namespace {
CheckCppCrashExtraKeyWords(const string& filePath, std::string *keywords, int length, int minRegIdx)137 bool CheckCppCrashExtraKeyWords(const string& filePath, std::string *keywords, int length, int minRegIdx)
138 {
139     if (filePath.empty()) {
140         return false;
141     }
142     int count = CheckKeyWords(filePath, keywords, length, minRegIdx);
143     return count == length;
144 }
145 /**
146  * @tc.name: DfxProcessDumpTest001
147  * @tc.desc: test SIGILL crash
148  * @tc.type: FUNC
149  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest001, TestSize.Level2)150 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest001, TestSize.Level2)
151 {
152     GTEST_LOG_(INFO) << "DfxProcessDumpTest001: start.";
153     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
154     sleep(1);
155     auto curTime = GetTimeMilliSeconds();
156     kill(testProcess, SIGILL);
157     sleep(3); // 3 : wait 3s to generate cpp crash file
158     auto filename = GetCppCrashFileName(testProcess);
159     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
160     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGILL));
161     GTEST_LOG_(INFO) << "DfxProcessDumpTest001: end.";
162 }
163 
164 /**
165  * @tc.name: DfxProcessDumpTest002
166  * @tc.desc: test SIGTRAP crash
167  * @tc.type: FUNC
168  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest002, TestSize.Level2)169 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest002, TestSize.Level2)
170 {
171     GTEST_LOG_(INFO) << "DfxProcessDumpTest002: start.";
172     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
173     sleep(1);
174     auto curTime = GetTimeMilliSeconds();
175     kill(testProcess, SIGTRAP);
176     sleep(3); // 3 : wait 3s to generate cpp crash file
177     auto filename = GetCppCrashFileName(testProcess);
178     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
179     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGTRAP));
180     GTEST_LOG_(INFO) << "DfxProcessDumpTest002: end.";
181 }
182 
183 /**
184  * @tc.name: DfxProcessDumpTest003
185  * @tc.desc: test SIGABRT crash
186  * @tc.type: FUNC
187  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest003, TestSize.Level2)188 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest003, TestSize.Level2)
189 {
190     GTEST_LOG_(INFO) << "DfxProcessDumpTest003: start.";
191     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
192     sleep(1);
193     auto curTime = GetTimeMilliSeconds();
194     kill(testProcess, SIGABRT);
195     sleep(3); // 3 : wait 3s to generate cpp crash file
196     auto filename = GetCppCrashFileName(testProcess);
197     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
198     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGABRT));
199     GTEST_LOG_(INFO) << "DfxProcessDumpTest003: end.";
200 }
201 
202 /**
203  * @tc.name: DfxProcessDumpTest004
204  * @tc.desc: test SIGBUS crash
205  * @tc.type: FUNC
206  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest004, TestSize.Level2)207 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest004, TestSize.Level2)
208 {
209     GTEST_LOG_(INFO) << "DfxProcessDumpTest004: start.";
210     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
211     sleep(1);
212     auto curTime = GetTimeMilliSeconds();
213     kill(testProcess, SIGBUS);
214     sleep(3); // 3 : wait 3s to generate cpp crash file
215     auto filename = GetCppCrashFileName(testProcess);
216     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
217     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGBUS));
218     GTEST_LOG_(INFO) << "DfxProcessDumpTest004: end.";
219 }
220 
221 /**
222  * @tc.name: DfxProcessDumpTest005
223  * @tc.desc: test SIGFPE crash
224  * @tc.type: FUNC
225  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest005, TestSize.Level2)226 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest005, TestSize.Level2)
227 {
228     GTEST_LOG_(INFO) << "DfxProcessDumpTest005: start.";
229     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
230     sleep(1);
231     auto curTime = GetTimeMilliSeconds();
232     kill(testProcess, SIGFPE);
233     sleep(3); // 3 : wait 3s to generate cpp crash file
234     auto filename = GetCppCrashFileName(testProcess);
235     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
236     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGFPE));
237     GTEST_LOG_(INFO) << "DfxProcessDumpTest005: end.";
238 }
239 
240 /**
241  * @tc.name: DfxProcessDumpTest006
242  * @tc.desc: test SIGSEGV crash
243  * @tc.type: FUNC
244  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest006, TestSize.Level2)245 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest006, TestSize.Level2)
246 {
247     GTEST_LOG_(INFO) << "DfxProcessDumpTest006: start.";
248     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
249     GTEST_LOG_(INFO) << "process pid:" << testProcess;
250     sleep(1);
251     auto curTime = GetTimeMilliSeconds();
252     kill(testProcess, SIGSEGV);
253     sleep(3); // 3 : wait 3s to generate cpp crash file
254     auto filename = GetCppCrashFileName(testProcess);
255     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
256     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGSEGV));
257     GTEST_LOG_(INFO) << "DfxProcessDumpTest006: end.";
258 }
259 
260 /**
261  * @tc.name: DfxProcessDumpTest007
262  * @tc.desc: test SIGSTKFLT crash
263  * @tc.type: FUNC
264  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest007, TestSize.Level2)265 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest007, TestSize.Level2)
266 {
267     GTEST_LOG_(INFO) << "DfxProcessDumpTest007: start.";
268     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
269     sleep(1);
270     auto curTime = GetTimeMilliSeconds();
271     kill(testProcess, SIGSTKFLT);
272     sleep(3); // 3 : wait 3s to generate cpp crash file
273     auto filename = GetCppCrashFileName(testProcess);
274     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
275     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGSTKFLT));
276     GTEST_LOG_(INFO) << "DfxProcessDumpTest007: end.";
277 }
278 
279 /**
280  * @tc.name: DfxProcessDumpTest008
281  * @tc.desc: test SIGSYS crash
282  * @tc.type: FUNC
283  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest008, TestSize.Level2)284 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest008, TestSize.Level2)
285 {
286     GTEST_LOG_(INFO) << "DfxProcessDumpTest008: start.";
287     pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
288     sleep(1);
289     auto curTime = GetTimeMilliSeconds();
290     kill(testProcess, SIGSYS);
291     sleep(3); // 3 : wait 3s to generate cpp crash file
292     auto filename = GetCppCrashFileName(testProcess);
293     ASSERT_EQ(std::to_string(curTime).length(), filename.length() - filename.find_last_of('-') - 1);
294     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGSYS));
295     GTEST_LOG_(INFO) << "DfxProcessDumpTest008: end.";
296 }
297 
298 /**
299  * @tc.name: DfxProcessDumpTest009
300  * @tc.desc: test processdump command
301  * @tc.type: FUNC
302  * @tc.require:
303  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest009, TestSize.Level2)304 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest009, TestSize.Level2)
305 {
306     GTEST_LOG_(INFO) << "DfxProcessDumpTest009: start.";
307     string procCMD = "processdump";
308     string procDumpLog = ExecuteCommands(procCMD);
309     string log[] = {"please use dumpcatcher"};
310     int expectNum = sizeof(log) / sizeof(log[0]);
311     int count = GetKeywordsNum(procDumpLog, log, expectNum);
312     EXPECT_EQ(count, expectNum) << "DfxProcessDumpTest009 Failed";
313     GTEST_LOG_(INFO) << "DfxProcessDumpTest009: end.";
314 }
315 
316 /**
317  * @tc.name: DfxProcessDumpTest010
318  * @tc.desc: test processdump command: -p 1
319  * @tc.type: FUNC
320  * @tc.require:
321  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest010, TestSize.Level2)322 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest010, TestSize.Level2)
323 {
324     GTEST_LOG_(INFO) << "DfxProcessDumpTest010: start.";
325     string procCMD = "processdump -p 1";
326     string procDumpLog = ExecuteCommands(procCMD);
327     string log[] = {"please use dumpcatcher"};
328     int expectNum = sizeof(log) / sizeof(log[0]);
329     int count = GetKeywordsNum(procDumpLog, log, expectNum);
330     EXPECT_EQ(count, expectNum) << "DfxProcessDumpTest010 Failed";
331     GTEST_LOG_(INFO) << "DfxProcessDumpTest010: end.";
332 }
333 
334 /**
335  * @tc.name: DfxProcessDumpTest011
336  * @tc.desc: Testing the sub thread crash of multithreaded programs
337  * @tc.type: FUNC
338  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest011, TestSize.Level2)339 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest011, TestSize.Level2)
340 {
341     GTEST_LOG_(INFO) << "DfxProcessDumpTest011: start.";
342     pid_t testProcess = CreateMultiThreadForThreadCrash(10); // 10 : create a process with ten threads
343     GTEST_LOG_(INFO) << "process pid:" << testProcess;
344     sleep(3); // 3 : wait 3s to generate cpp crash file
345     auto filename = GetCppCrashFileName(testProcess);
346     ASSERT_TRUE(CheckCppCrashKeyWords(filename, testProcess, SIGSEGV));
347     GTEST_LOG_(INFO) << "DfxProcessDumpTest011: end.";
348 }
349 
350 
351 /**
352  * @tc.name: DfxProcessDumpTest012
353  * @tc.desc: Testing new add key word
354  * @tc.type: FUNC
355  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest012, TestSize.Level2)356 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest012, TestSize.Level2)
357 {
358     GTEST_LOG_(INFO) << "DfxProcessDumpTest012: start.";
359     pid_t testProcess = CreateMultiThreadForThreadCrash(10); // 10 : create a process with ten threads
360     GTEST_LOG_(INFO) << "process pid:" << testProcess;
361     sleep(3); // 3 : wait 3s to generate cpp crash file
362     auto filename = GetCppCrashFileName(testProcess);
363     string keywords[] = {
364         "time", "OpenFiles:"
365     };
366     int length = sizeof(keywords) / sizeof(keywords[0]);
367     int minRegIdx = -1; // -1 : no not check register value
368     ASSERT_TRUE(CheckCppCrashExtraKeyWords(filename, keywords, length, minRegIdx));
369     GTEST_LOG_(INFO) << "DfxProcessDumpTest012: end.";
370 }
371 
372 /**
373  * @tc.name: DfxProcessDumpTest013
374  * @tc.desc: Testing new add key word
375  * @tc.type: FUNC
376  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest013, TestSize.Level2)377 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest013, TestSize.Level2)
378 {
379     GTEST_LOG_(INFO) << "DfxProcessDumpTest013: start.";
380     int openNum = 128;
381     pid_t testProcess = CreateMultiThreadForThreadCrashWithOpen(10, openNum); // 10 : create a process with ten threads
382     GTEST_LOG_(INFO) << "process pid:" << testProcess;
383     sleep(3); // 3 : wait 3s to generate cpp crash file
384     auto filename = GetCppCrashFileName(testProcess);
385     string keywords[openNum];
386     string str = "FILE*";
387     for (int i = 0; i < openNum; ++i) {
388         keywords[i] = str;
389     }
390     int length = sizeof(keywords) / sizeof(keywords[0]);
391     int minRegIdx = -1; // -1 : no not check register value
392     ASSERT_TRUE(CheckCppCrashExtraKeyWords(filename, keywords, length, minRegIdx));
393     GTEST_LOG_(INFO) << "DfxProcessDumpTest013: end.";
394 }
395 
396 /**
397  * @tc.name: DfxProcessDumpTest014
398  * @tc.desc: Testing dlopen and dlsym interfaces
399  * @tc.type: FUNC
400  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest014, TestSize.Level2)401 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest014, TestSize.Level2)
402 {
403     GTEST_LOG_(INFO) << "DfxProcessDumpTest014: start.";
404     void* handle = dlopen("libfaultlogger.z.so", RTLD_LAZY | RTLD_NODELETE);
405     ASSERT_TRUE(handle) << "Failed to dlopen libfaultlogger";
406     auto addFaultLog = reinterpret_cast<void (*)(FaultDFXLOGIInner*)>(dlsym(handle, "AddFaultLog"));
407     ASSERT_TRUE(addFaultLog) << "Failed to dlsym addFaultLog";
408     FaultDFXLOGIInner info;
409     info.time = time(NULL);
410     info.id = 0;
411     info.pid = 1;
412     info.pipeFd = -1;
413     info.faultLogType = 2; // 2 : CPP_CRASH_TYPE
414     info.module = "";
415     info.reason = "";
416     info.summary = "";
417     info.registers = "";
418     addFaultLog(&info);
419     dlclose(handle);
420     GTEST_LOG_(INFO) << "DfxProcessDumpTest01: end.";
421 }
422 
423 /**
424  * @tc.name: DfxProcessDumpTest015
425  * @tc.desc: Testing dlopen and dlsym RecordAppExitReason
426  * @tc.type: FUNC
427  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest015, TestSize.Level2)428 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest015, TestSize.Level2)
429 {
430     GTEST_LOG_(INFO) << "DfxProcessDumpTest015: start.";
431     void* handle = dlopen("libability_manager_c.z.so", RTLD_LAZY | RTLD_NODELETE);
432     ASSERT_TRUE(handle) << "Failed to dlopen libability_manager_c";
433     RecordAppExitReason recordAppExitReason = (RecordAppExitReason)dlsym(handle, "RecordAppExitReason");
434     ASSERT_TRUE(recordAppExitReason) << "Failed to dlsym RecordAppExitReason";
435     string reason_ = "reason";
436     const int cppCrashExitReason = 2;
437     recordAppExitReason(cppCrashExitReason, reason_.c_str());
438     dlclose(handle);
439     GTEST_LOG_(INFO) << "DfxProcessDumpTest015: end.";
440 }
441 
442 /**
443  * @tc.name: DfxProcessDumpTest016
444  * @tc.desc: Testing DfxLogToSocket Function
445  * @tc.type: FUNC
446  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest016, TestSize.Level2)447 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest016, TestSize.Level2)
448 {
449     GTEST_LOG_(INFO) << "DfxProcessDumpTest016: start.";
450     pid_t pid = fork();
451     ASSERT_TRUE(pid >= 0);
452     if (pid == 0) {
453         sleep(3); // 3 : sleep 3 seconds
454     }
455     char msg[] = "test log";
456     DfxLogToSocket(msg);
457     kill(pid, SIGSEGV);
458     GTEST_LOG_(INFO) << "DfxProcessDumpTest016: end.";
459 }
460 
461 /**
462  * @tc.name: DfxProcessDumpTest017
463  * @tc.desc: Testing InitProcessInfo、InitKeyThread、InitRegs exception
464  * @tc.type: FUNC
465  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest017, TestSize.Level2)466 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest017, TestSize.Level2)
467 {
468     GTEST_LOG_(INFO) << "DfxProcessDumpTest017: start.";
469     ProcessDumper& ins = ProcessDumper::GetInstance();
470     std::shared_ptr<ProcessDumpRequest> request = std::make_shared<ProcessDumpRequest>();
471     int result = ins.InitProcessInfo(request);
472     ASSERT_EQ(result, -1);
473 
474     request->pid = 1;
475     request->nsPid = 1;
476     result = ins.InitProcessInfo(request);
477     ASSERT_EQ(result, -1);
478     ins.isCrash_ = true;
479     result = ins.InitProcessInfo(request);
480     ASSERT_EQ(result, 0);
481 
482     ins.process_ = nullptr;
483     bool ret = ins.InitKeyThread(nullptr);
484     ASSERT_FALSE(ret);
485     ins.InitKeyThread(request);
486     ASSERT_FALSE(ret);
487 
488     ins.process_ = DfxProcess::Create(request->pid, request->nsPid);
489     ret = ins.InitKeyThread(nullptr);
490     ASSERT_FALSE(ret);
491     ret = ins.InitKeyThread(request);
492     ASSERT_TRUE(ret);
493     ins.process_->keyThread_ = nullptr;
494     ret = ins.InitKeyThread(request);
495     ASSERT_TRUE(ret);
496 
497     ins.process_->keyThread_  = std::make_shared<DfxThread>();
498     request->dumpMode = FUSION_MODE;
499     ret = ins.InitKeyThread(request);
500     ASSERT_TRUE(ret);
501     request->dumpMode = SPLIT_MODE;
502     ret = ins.InitKeyThread(request);
503     ASSERT_TRUE(ret);
504     ins.process_->keyThread_ = nullptr;
505     request->dumpMode = FUSION_MODE;
506     ret = ins.InitKeyThread(request);
507     ASSERT_TRUE(ret);
508     request->dumpMode = SPLIT_MODE;
509     ret = ins.InitKeyThread(request);
510     ins.process_->keyThread_->threadInfo_.threadName = "";
511     ASSERT_TRUE(ret);
512     request->dumpMode  = FUSION_MODE;
513     int dumpRes = 1;
514     ins.InitRegs(request, dumpRes);
515     GTEST_LOG_(INFO) << "DfxProcessDumpTest017: end.";
516 }
517 
518 /**
519  * @tc.name: DfxProcessDumpTest018
520  * @tc.desc: Testing IsTargetProcessAlive exception
521  * @tc.type: FUNC
522  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest018, TestSize.Level2)523 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest018, TestSize.Level2)
524 {
525     GTEST_LOG_(INFO) << "DfxProcessDumpTest018: start.";
526     ProcessDumper& ins = ProcessDumper::GetInstance();
527     std::shared_ptr<ProcessDumpRequest> request = std::make_shared<ProcessDumpRequest>();
528     request->dumpMode = SPLIT_MODE;
529     ins.isCrash_ = true;
530     request->nsPid = syscall(SYS_getppid);
531     request->vmNsPid = 1;
532     bool ret = ins.IsTargetProcessAlive(request);
533     ASSERT_FALSE(ret);
534 
535     request->dumpMode = SPLIT_MODE;
536     ins.isCrash_ = true;
537     request->nsPid = 1;
538     request->vmNsPid = 1;
539     ret = ins.IsTargetProcessAlive(request);
540     ASSERT_FALSE(ret);
541 
542     request->dumpMode = SPLIT_MODE;
543     ins.isCrash_ = false;
544     request->nsPid = 1;
545     request->vmNsPid = syscall(SYS_getppid);
546     ret = ins.IsTargetProcessAlive(request);
547     ASSERT_FALSE(ret);
548 
549     request->dumpMode = SPLIT_MODE;
550     ins.isCrash_ = false;
551     request->nsPid = 1;
552     request->vmNsPid = 1;
553     ret = ins.IsTargetProcessAlive(request);
554     ASSERT_FALSE(ret);
555     GTEST_LOG_(INFO) << "DfxProcessDumpTest018: end.";
556 }
557 
558 /**
559  * @tc.name: DfxProcessDumpTest019
560  * @tc.desc: Testing InitVmThread exception
561  * @tc.type: FUNC
562  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest019, TestSize.Level2)563 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest019, TestSize.Level2)
564 {
565     GTEST_LOG_(INFO) << "DfxProcessDumpTest019: start.";
566     ProcessDumper& ins = ProcessDumper::GetInstance();
567     std::shared_ptr<ProcessDumpRequest> request = std::make_shared<ProcessDumpRequest>();
568     ins.process_ = nullptr;
569     bool ret = ins.InitVmThread(nullptr);
570     ASSERT_FALSE(ret);
571     ret = ins.InitVmThread(request);
572     ASSERT_FALSE(ret);
573 
574     ins.process_ = DfxProcess::Create(request->pid, request->nsPid);
575     ret = ins.InitVmThread(nullptr);
576     ASSERT_FALSE(ret);
577     ret = ins.InitVmThread(request);
578     ASSERT_TRUE(ret);
579     ins.isCrash_ = true;
580     request->vmPid = 1;
581     ret = ins.InitVmThread(request);
582     ASSERT_FALSE(ret);
583     request->vmNsPid = getppid();
584     ret = ins.InitVmThread(request);
585     ASSERT_TRUE(ret);
586     ins.process_->vmThread_ = nullptr;
587     ret = ins.InitVmThread(request);
588     ASSERT_FALSE(ret);
589 
590     ins.process_->vmThread_ = std::make_shared<DfxThread>();
591     ret = ins.InitVmThread(request);
592     ASSERT_FALSE(ret);
593     GTEST_LOG_(INFO) << "DfxProcessDumpTest019: end.";
594 }
595 
596 /**
597  * @tc.name: DfxProcessDumpTest020
598  * @tc.desc: Testing InitProcessInfo Function
599  * @tc.type: FUNC
600  */
HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest020, TestSize.Level2)601 HWTEST_F(DfxProcessDumpTest, DfxProcessDumpTest020, TestSize.Level2)
602 {
603     GTEST_LOG_(INFO) << "DfxProcessDumpTest020: start.";
604     ProcessDumper& ins = ProcessDumper::GetInstance();
605     std::shared_ptr<ProcessDumpRequest> request = std::make_shared<ProcessDumpRequest>();
606     ins.isCrash_ = true;
607     request->siginfo.si_signo = SIGLEAK_STACK;
608     int result = ins.InitPrintThread(request);
609     ASSERT_NE(result, -1);
610     ins.isCrash_ = true;
611     request->siginfo.si_signo = CPP_CRASH;
612     result = ins.InitPrintThread(request);
613     ASSERT_NE(result, -1);
614     ins.isCrash_ = false;
615     request->siginfo.si_signo = SIGLEAK_STACK;
616     result = ins.InitPrintThread(request);
617     ASSERT_NE(result, -1);
618     ins.isCrash_ = false;
619     request->siginfo.si_signo = CPP_CRASH;
620     result = ins.InitPrintThread(request);
621     ASSERT_EQ(result, -1);
622 
623     result = ins.WriteDumpBuf(1, nullptr, 1);
624     ASSERT_EQ(result, -1);
625     ins.resFd_ = -1;
626     ins.WriteDumpRes(1);
627     ASSERT_EQ(ins.resFd_, -1);
628     GTEST_LOG_(INFO) << "DfxProcessDumpTest020: end.";
629 }
630 }