1 /*
2 * Copyright (c) 2021-2025 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 #include "event_logger_catcher_test.h"
16
17 #include <ctime>
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 #include <regex>
22
23 #include <fcntl.h>
24 #include <sys/prctl.h>
25 #include <unistd.h>
26 #include <string>
27
28 #include "securec.h"
29 #include "common_utils.h"
30 #include "file_util.h"
31 #define private public
32 #ifdef DMESG_CATCHER_ENABLE
33 #include "dmesg_catcher.h"
34 #include "time_util.h"
35 #endif // DMESG_CATCHER_ENABLE
36 #include "event_log_task.h"
37 #ifdef STACKTRACE_CATCHER_ENABLE
38 #include "open_stacktrace_catcher.h"
39 #endif // STACKTRACE_CATCHER_ENABLE
40 #include "shell_catcher.h"
41 #ifdef BINDER_CATCHER_ENABLE
42 #include "peer_binder_catcher.h"
43 #include "parameter_ex.h"
44 #endif // BINDER_CATCHER_ENABLE
45 #ifdef USAGE_CATCHER_ENABLE
46 #include "cpu_core_info_catcher.h"
47 #include "memory_catcher.h"
48 #endif // USAGE_CATCHER_ENABLE
49 #include "summary_log_info_catcher.h"
50 #undef private
51 #ifdef BINDER_CATCHER_ENABLE
52 #include "binder_catcher.h"
53 #endif // BINDER_CATCHER_ENABLE
54 #ifdef OTHER_CATCHER_ENABLE
55 #include "ffrt_catcher.h"
56 #endif // OTHER_CATCHER_ENABLE
57 #include "event_logger.h"
58 #include "event_log_catcher.h"
59 #include "sys_event.h"
60 #include "hisysevent.h"
61 #include "eventlogger_util_test.h"
62 #include "log_catcher_utils.h"
63 #include "thermal_info_catcher.h"
64
65 using namespace testing::ext;
66 using namespace OHOS::HiviewDFX;
67
68 namespace OHOS {
69 namespace HiviewDFX {
SetUp()70 void EventloggerCatcherTest::SetUp()
71 {
72 /**
73 * @tc.setup: create an event loop and multiple event handlers
74 */
75 printf("SetUp.\n");
76 InitSeLinuxEnabled();
77 }
78
TearDown()79 void EventloggerCatcherTest::TearDown()
80 {
81 /**
82 * @tc.teardown: destroy the event loop we have created
83 */
84 CancelSeLinuxEnabled();
85 printf("TearDown.\n");
86 }
87
88 /**
89 * @tc.name: EventLogCatcher
90 * @tc.desc: test EventLogCatcher
91 * @tc.type: FUNC
92 */
93 HWTEST_F(EventloggerCatcherTest, EventLogCatcherTest_001, TestSize.Level3)
94 {
95 auto eventLogCatcher = std::make_shared<EventLogCatcher>();
96 EXPECT_TRUE(eventLogCatcher->GetLogSize() == -1);
97 eventLogCatcher->SetLogSize(1);
98 EXPECT_TRUE(eventLogCatcher->GetLogSize() == 1);
99 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
100 if (fd < 0) {
101 printf("Fail to create catcherFile. errno: %d\n", errno);
102 FAIL();
103 }
104 int res = eventLogCatcher->Catch(fd, 1);
105 EXPECT_TRUE(res == 0);
106 close(fd);
107 }
108
109 /**
110 * @tc.name: EventlogTask
111 * @tc.desc: test EventLogTask
112 * @tc.type: FUNC
113 */
114 HWTEST_F(EventloggerCatcherTest, EventlogTask_001, TestSize.Level0)
115 {
116 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
117 if (fd < 0) {
118 printf("Fail to create testFile. errno: %d\n", errno);
119 FAIL();
120 }
121 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
122 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
123 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
124 logTask->AddStopReason(fd, nullptr, "Test");
125 auto eventLogCatcher = std::make_shared<EventLogCatcher>();
126 logTask->AddStopReason(fd, eventLogCatcher, "Test");
127 logTask->AddSeparator(fd, eventLogCatcher);
128 bool ret = logTask->ShouldStopLogTask(fd, 1, -1, eventLogCatcher);
129 EXPECT_EQ(ret, false);
130 ret = logTask->ShouldStopLogTask(fd, 1, 20000, eventLogCatcher);
131 EXPECT_EQ(ret, false);
132 logTask->status_ = EventLogTask::Status::TASK_TIMEOUT;
133 ret = logTask->ShouldStopLogTask(fd, 0, 1, eventLogCatcher);
134 EXPECT_EQ(ret, true);
135 close(fd);
136 }
137
138 /**
139 * @tc.name: EventlogTask
140 * @tc.desc: test EventlogTask
141 * @tc.type: FUNC
142 */
143 HWTEST_F(EventloggerCatcherTest, EventlogTask_002, TestSize.Level3)
144 {
145 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
146 if (fd < 0) {
147 printf("Fail to create testFile. errno: %d\n", errno);
148 FAIL();
149 }
150 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
151 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
152 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
153 EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNABLE);
154 auto ret = logTask->StartCompose();
155 EXPECT_EQ(ret, 2);
156 EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNING);
157 ret = logTask->StartCompose();
158 EXPECT_EQ(ret, 1);
159 EXPECT_EQ(logTask->GetLogSize(), 0);
160 close(fd);
161 }
162
163 /**
164 * @tc.name: EventlogTask
165 * @tc.desc: test EventlogTask
166 * @tc.type: FUNC
167 */
168 HWTEST_F(EventloggerCatcherTest, EventlogTask_003, TestSize.Level3)
169 {
170 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
171 if (fd < 0) {
172 printf("Fail to create testFile. errno: %d\n", errno);
173 FAIL();
174 }
175 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
176 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
177 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
178 logTask->AddLog("cmd:scbCS");
179 #ifdef STACKTRACE_CATCHER_ENABLE
180 logTask->AppStackCapture();
181 logTask->SystemStackCapture();
182 logTask->RemoteStackCapture();
183 logTask->GetGPUProcessStack();
184 #endif // STACKTRACE_CATCHER_ENABLE
185
186 #ifdef BINDER_CATCHER_ENABLE
187 logTask->BinderLogCapture();
188 EXPECT_EQ(logTask->PeerBinderCapture("Test"), false);
189 EXPECT_EQ(logTask->PeerBinderCapture("pb"), false);
190 EXPECT_EQ(logTask->PeerBinderCapture("pb:1:a"), true);
191 #endif // BINDER_CATCHER_ENABLE
192
193 #ifdef HILOG_CATCHER_ENABLE
194 logTask->HilogCapture();
195 logTask->LightHilogCapture();
196 logTask->InputHilogCapture();
197 #endif // HILOG_CATCHER_ENABLE
198
199 #ifdef SCB_CATCHER_ENABLE
200 logTask->SCBSessionCapture();
201 logTask->SCBViewParamCapture();
202 logTask->SCBWMSCapture();
203 logTask->SCBWMSEVTCapture();
204 #endif // SCB_CATCHER_ENABLE
205
206 #ifdef USAGE_CATCHER_ENABLE
207 logTask->DumpAppMapCapture();
208 logTask->WMSUsageCapture();
209 logTask->AMSUsageCapture();
210 logTask->PMSUsageCapture();
211 logTask->DPMSUsageCapture();
212 logTask->RSUsageCapture();
213 logTask->MemoryUsageCapture();
214 logTask->CpuUsageCapture();
215 logTask->CpuCoreInfoCapture();
216 #endif // USAGE_CATCHER_ENABLE
217
218 #ifdef DMESG_CATCHER_ENABLE
219 logTask->DmesgCapture(0, 0);
220 logTask->DmesgCapture(0, 1);
221 logTask->DmesgCapture(1, 1);
222 logTask->DmesgCapture(0, 2);
223 logTask->DmesgCapture(1, 2);
224 #endif // DMESG_CATCHER_ENABLE
225
226 #ifdef OTHER_CATCHER_ENABLE
227 logTask->Screenshot();
228 logTask->FfrtCapture();
229 logTask->DMSUsageCapture();
230 logTask->MMIUsageCapture();
231 logTask->EECStateCapture();
232 logTask->GECStateCapture();
233 logTask->UIStateCapture();
234 #endif // OTHER_CATCHER_ENABLE
235
236 #ifdef HITRACE_CATCHER_ENABLE
237 logTask->HitraceCapture(false);
238 sysEvent->eventName_ = "THREAD_BLOCK_6S";
239 sysEvent->SetValue("PROCESS_NAME", "EventloggerCatcherTest");
240 logTask->HitraceCapture(true);
241 #endif // HITRACE_CATCHER_ENABLE
242 logTask->GetThermalInfoCapture();
243 logTask->AddLog("Test");
244 logTask->AddLog("cmd:w");
245 logTask->status_ = EventLogTask::Status::TASK_RUNNING;
246 auto ret = logTask->StartCompose();
247 printf("task size: %d\n", static_cast<int>(logTask->tasks_.size()));
248
249 close(fd);
250 }
251
252 #ifdef STACKTRACE_CATCHER_ENABLE
253 /**
254 * @tc.name: EventlogTask
255 * @tc.desc: test EventlogTask
256 * @tc.type: FUNC
257 */
258 HWTEST_F(EventloggerCatcherTest, EventlogTask_004, TestSize.Level3)
259 {
260 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
261 if (fd < 0) {
262 printf("Fail to create testFile. errno: %d\n", errno);
263 FAIL();
264 }
265 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
266 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
267 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
268 logTask->GetStackByProcessName();
269 sysEvent->SetEventValue("PROCESS_NAME", "EventloggerCatcherTest");
270 logTask->GetStackByProcessName();
271 EXPECT_TRUE(logTask != nullptr);
272 }
273 #endif // STACKTRACE_CATCHER_ENABLE
274
275 /**
276 * @tc.name: EventlogTask
277 * @tc.desc: test EventlogTask
278 * @tc.type: FUNC
279 */
280 HWTEST_F(EventloggerCatcherTest, EventlogTask_005, TestSize.Level3)
281 {
282 auto fd = open("/data/test/vreFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
283 if (fd < 0) {
284 printf("Fail to create vreFile. errno: %d\n", errno);
285 FAIL();
286 }
287 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
288 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
289 sysEvent->SetEventValue("PID", getpid());
290 sysEvent->SetEventValue("APPNODEID", 2025);
291 sysEvent->SetEventValue("APPNODENAME", "test appNodeName");
292 sysEvent->SetEventValue("LEASHWINDOWID", 319);
293 sysEvent->SetEventValue("LEASHWINDOWNAME", "test leashWindowName");
294 sysEvent->SetEventValue("EXT_INFO", "test ext_info");
295
296 sysEvent->domain_ = "AAFWK";
297 sysEvent->eventName_ = "APP_INPUT_BLOCK";
298 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
299 logTask->SaveRsVulKanError();
300 sysEvent->domain_ = "GRAPHIC";
301 sysEvent->eventName_ = "RS_VULKAN_ERROR";
302 logTask->SaveRsVulKanError();
303 close(fd);
304
305 std::string line;
306 std::ifstream ifs("/data/test/vreFile", std::ios::in);
307 if (ifs.is_open()) {
308 while (std::getline(ifs, line)) {
309 if (line.find("APPNODEID") != std::string::npos) {
310 printf("%s", line.c_str());
311 EXPECT_EQ(line, "APPNODEID=2025");
312 }
313 if (line.find("EXT_INFO") != std::string::npos) {
314 printf("%s", line.c_str());
315 EXPECT_EQ(line, "EXT_INFO=test ext_info");
316 }
317 }
318 }
319 EXPECT_EQ(sysEvent->GetEventValue("PROCESS_NAME"), "EventloggerCatcherTest");
320 }
321
322 #if defined(KERNELSTACK_CATCHER_ENABLE) && defined(DMESG_CATCHER_ENABLE)
323 /**
324 * @tc.name: EventlogTask
325 * @tc.desc: test EventlogTask
326 * @tc.type: FUNC
327 */
328 HWTEST_F(EventloggerCatcherTest, EventlogTask_006, TestSize.Level3)
329 {
330 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
331 if (fd < 0) {
332 printf("Fail to create testFile. errno: %d\n", errno);
333 FAIL();
334 }
335 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
336 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
337 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
338 logTask->SysrqCapture(true);
339 sysEvent->SetEventValue("SPECIFICSTACK_NAME", "foundation");
340 logTask->SysrqCapture(true);
341 EXPECT_TRUE(logTask != nullptr);
342 }
343 #endif
344
345 #ifdef BINDER_CATCHER_ENABLE
346 /**
347 * @tc.name: BinderCatcherTest_001
348 * @tc.desc: add testcase code coverage
349 * @tc.type: FUNC
350 */
351 HWTEST_F(EventloggerCatcherTest, BinderCatcherTest_001, TestSize.Level1)
352 {
353 auto binderCatcher = std::make_shared<BinderCatcher>();
354 bool ret = binderCatcher->Initialize("test", 1, 2);
355 EXPECT_EQ(ret, true);
356 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
357 if (fd < 0) {
358 printf("Fail to create catcherFile. errno: %d\n", errno);
359 FAIL();
360 }
361 int res = binderCatcher->Catch(fd, 1);
362 EXPECT_TRUE(res > 0);
363 close(fd);
364 }
365 #endif // BINDER_CATCHER_ENABLE
366
367 #ifdef USAGE_CATCHER_ENABLE
368 /**
369 * @tc.name: MemoryCatcherTest_001
370 * @tc.desc: add testcase code coverage
371 * @tc.type: FUNC
372 */
373 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_001, TestSize.Level0)
374 {
375 auto memoryCatcher = std::make_shared<MemoryCatcher>();
376 bool ret = memoryCatcher->Initialize("test", 1, 2);
377 EXPECT_EQ(ret, true);
378 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
379 if (fd < 0) {
380 printf("Fail to create catcherFile. errno: %d\n", errno);
381 FAIL();
382 }
383 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
384 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
385 sysEvent->SetEventValue("FREEZE_MEMORY", "Get freeze memory end time:\\n12300 45600 78900 36001 96300");
386 memoryCatcher->SetEvent(sysEvent);
387 int res = memoryCatcher->Catch(fd, 1);
388 EXPECT_TRUE(res > 0);
389 res = memoryCatcher->Catch(0, 1);
390 EXPECT_EQ(res, 0);
391 printf("memoryCatcher result: %d\n", res);
392 close(fd);
393 }
394
395 /**
396 * @tc.name: MemoryCatcherTest_002
397 * @tc.desc: EventloggerCatcherTest
398 * @tc.type: FUNC
399 */
400 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_002, TestSize.Level3)
401 {
402 auto memoryCatcher = std::make_shared<MemoryCatcher>();
403 EXPECT_EQ(memoryCatcher->GetStringFromFile("/data/log/test"), "");
404 }
405
406 /**
407 * @tc.name: MemoryCatcherTest_003
408 * @tc.desc: EventloggerCatcherTest
409 * @tc.type: FUNC
410 */
411 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_003, TestSize.Level3)
412 {
413 auto memoryCatcher = std::make_shared<MemoryCatcher>();
414 int ret = memoryCatcher->GetNumFromString("abc");
415 EXPECT_EQ(ret, 0);
416 ret = memoryCatcher->GetNumFromString("100");
417 EXPECT_EQ(ret, 100);
418 }
419
420 /**
421 * @tc.name: MemoryCatcherTest_004
422 * @tc.desc: EventloggerCatcherTest
423 * @tc.type: FUNC
424 */
425 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_004, TestSize.Level3)
426 {
427 auto memoryCatcher = std::make_shared<MemoryCatcher>();
428 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
429 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
430 memoryCatcher->SetEvent(sysEvent);
431 std::string data;
432 memoryCatcher->CheckString("abc: 100", data, "abcd", "/data/log/test");
433 EXPECT_TRUE(data.empty());
434 memoryCatcher->CheckString("abc: 100", data, "abc", "");
435 EXPECT_EQ(sysEvent->GetEventValue("HOST_RESOURCE_WARNING"), "Yes");
436 memoryCatcher->CheckString("abc: 3000000", data, "abc", "/proc/ashmem_process_info");
437 EXPECT_TRUE(data.empty());
438 }
439
440 /**
441 * @tc.name: MemoryCatcherTest_004
442 * @tc.desc: EventloggerCatcherTest
443 * @tc.type: FUNC
444 */
445 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_005, TestSize.Level3)
446 {
447 auto memoryCatcher = std::make_shared<MemoryCatcher>();
448 std::string memInfo = memoryCatcher->CollectFreezeSysMemory();
449 EXPECT_TRUE(!memInfo.empty());
450 }
451
452 /**
453 * @tc.name: MemoryCatcherTest_006
454 * @tc.desc: EventloggerCatcherTest
455 * @tc.type: FUNC
456 */
457 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_006, TestSize.Level3)
458 {
459 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
460 if (fd < 0) {
461 printf("Fail to create testFile. errno: %d\n", errno);
462 FAIL();
463 }
464 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
465 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
466 sysEvent->SetEventValue("FREEZE_MEMORY", "test\\ntest");
467 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
468 logTask->MemoryUsageCapture();
469 EXPECT_TRUE(logTask != nullptr);
470
471 sysEvent->SetEventValue("FREEZE_MEMORY", "freeze Get freeze memory end time:\\n123 456 789 100 200");
472 logTask->MemoryUsageCapture();
473 EXPECT_TRUE(logTask != nullptr);
474 close(fd);
475 }
476
477 /**
478 * @tc.name: MemoryCatcherTest_007
479 * @tc.desc: EventloggerCatcherTest
480 * @tc.type: FUNC
481 */
482 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_007, TestSize.Level3)
483 {
484 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
485 if (fd < 0) {
486 printf("Fail to create testFile. errno: %d\n", errno);
487 FAIL();
488 }
489
490 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
491 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
492 sysEvent->SetEventValue("FREEZE_MEMORY", "AshmemUsed 3200000 kB\\n"
493 "DMAHEAP 3200000 kB");
494 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
495 logTask->MemoryUsageCapture();
496 EXPECT_TRUE(logTask != nullptr);
497 close(fd);
498 }
499
500 /**
501 * @tc.name: CpuCoreInfoCatcherTest_001
502 * @tc.desc: add testcase code coverage
503 * @tc.type: FUNC
504 */
505 HWTEST_F(EventloggerCatcherTest, CpuCoreInfoCatcherTest_001, TestSize.Level1)
506 {
507 auto cpuCoreInfoCatcher = std::make_shared<CpuCoreInfoCatcher>();
508 bool ret = cpuCoreInfoCatcher->Initialize("test", 1, 2);
509 EXPECT_EQ(ret, true);
510 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
511 if (fd < 0) {
512 printf("Fail to create catcherFile. errno: %d\n", errno);
513 FAIL();
514 }
515 auto eventLogCatcher = std::make_shared<EventLogCatcher>();
516 int originSize = eventLogCatcher->GetFdSize(fd);
517 printf("Get testFile originSize: %d\n", originSize);
518
519 int res = cpuCoreInfoCatcher->Catch(fd, 1);
520 EXPECT_TRUE(res > 0);
521 int currentSize = eventLogCatcher->GetFdSize(fd) - originSize;
522 printf("Get testFile size: %d\n", currentSize);
523 close(fd);
524 }
525 #endif // USAGE_CATCHER_ENABLE
526
527 #ifdef OTHER_CATCHER_ENABLE
528 /**
529 * @tc.name: FfrtCatcherTest_001
530 * @tc.desc: add testcase code coverage
531 * @tc.type: FUNC
532 */
533 HWTEST_F(EventloggerCatcherTest, FfrtCatcherTest_001, TestSize.Level0)
534 {
535 auto fd = open("/data/test/FfrtCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
536 if (fd < 0) {
537 printf("Fail to create FfrtCatcherFile. errno: %d\n", errno);
538 FAIL();
539 }
540
541 auto ffrtCatcher = std::make_shared<FfrtCatcher>();
542 int pid = CommonUtils::GetPidByName("foundation");
543 if (pid > 0) {
544 bool res = ffrtCatcher->Initialize("", pid, 0);
545 EXPECT_TRUE(res);
546
547 int jsonFd = 1;
548 EXPECT_TRUE(ffrtCatcher->Catch(fd, jsonFd) > 0);
549 }
550 EXPECT_TRUE(true);
551 close(fd);
552 }
553
554 /**
555 * @tc.name: FfrtCatcherTest_002
556 * @tc.desc: add testcase code coverage
557 * @tc.type: FUNC
558 */
559 HWTEST_F(EventloggerCatcherTest, FfrtCatcherTest_002, TestSize.Level1)
560 {
561 auto ffrtCatcher = std::make_shared<FfrtCatcher>();
562 bool ret = ffrtCatcher->Initialize("test", 1, 2);
563 EXPECT_EQ(ret, true);
564 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
565 if (fd < 0) {
566 printf("Fail to create catcherFile. errno: %d\n", errno);
567 FAIL();
568 }
569 int res = ffrtCatcher->Catch(fd, 1);
570 EXPECT_TRUE(res > 0);
571 res = ffrtCatcher->Catch(0, 1);
572 EXPECT_EQ(res, 0);
573 printf("ffrtCatcher result: %d\n", res);
574 close(fd);
575 }
576 #endif // OTHER_CATCHER_ENABLE
577
578 #ifdef DMESG_CATCHER_ENABLE
579 /**
580 * @tc.name: DmesgCatcherTest_001
581 * @tc.desc: add testcase code coverage
582 * @tc.type: FUNC
583 */
584 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_001, TestSize.Level0)
585 {
586 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
587 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
588 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("DmesgCatcherTest_001",
589 nullptr, jsonStr);
590 event->eventId_ = 0;
591 event->domain_ = "KERNEL_VENDOR";
592 event->eventName_ = "HUNGTASK";
593 event->SetEventValue("PID", 0);
594 EXPECT_TRUE(dmesgCatcher->Init(event));
595 }
596
597 /**
598 * @tc.name: DmesgCatcherTest_002
599 * @tc.desc: add testcase code coverage
600 * @tc.type: FUNC
601 */
602 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_002, TestSize.Level1)
603 {
604 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
605 auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
606 if (fd < 0) {
607 printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
608 FAIL();
609 }
610
611 dmesgCatcher->Initialize("", 0, 0);
612 int jsonFd = 1;
613 EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
614
615 dmesgCatcher->Initialize("", 0, 1);
616 EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
617
618 dmesgCatcher->Initialize("", 1, 1);
619 printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
620
621 dmesgCatcher->Initialize("", 0, 2);
622 printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
623
624 dmesgCatcher->Initialize("", 1, 2);
625 printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
626
627 close(fd);
628 }
629
630 /**
631 * @tc.name: DmesgCatcherTest_003
632 * @tc.desc: add testcase code coverage
633 * @tc.type: FUNC
634 */
635 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_003, TestSize.Level1)
636 {
637 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
638 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("DmesgCatcherTest_003",
639 nullptr, jsonStr);
640 event->SetEventValue("SYSRQ_TIME", "20250124");
641 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
642 dmesgCatcher->Init(event);
643
644 bool ret = dmesgCatcher->DumpDmesgLog(-1);
645 EXPECT_EQ(ret, false);
646 ret = dmesgCatcher->WriteSysrqTrigger();
647 EXPECT_EQ(ret, true);
648
649 auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
650 if (fd < 0) {
651 printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
652 FAIL();
653 }
654 dmesgCatcher->Initialize("", true, 1);
655 ret = dmesgCatcher->DumpDmesgLog(fd);
656 close(fd);
657 EXPECT_EQ(ret, false);
658
659 auto fd1 = open("/data/test/dmesgCatcherFile2", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
660 if (fd1 < 0) {
661 printf("Fail to create dmesgCatcherFile2. errno: %d\n", errno);
662 FAIL();
663 }
664 dmesgCatcher->Initialize("", true, 2);
665 ret = dmesgCatcher->DumpDmesgLog(fd1);
666 close(fd1);
667 EXPECT_EQ(ret, true);
668 }
669
670 #ifdef KERNELSTACK_CATCHER_ENABLE
671 /**
672 * @tc.name: DmesgCatcherTest_004
673 * @tc.desc: add test
674 * @tc.type: FUNC
675 */
676 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_004, TestSize.Level1)
677 {
678 int pid = getpid();
679 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
680 int ret = dmesgCatcher->DumpKernelStacktrace(-1, pid);
681 EXPECT_EQ(ret, -1);
682 auto fd = open("/data/test/logCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
683 if (fd < 0) {
684 printf("Fail to create logCatcherFile. errno: %d\n", errno);
685 FAIL();
686 }
687 ret = dmesgCatcher->DumpKernelStacktrace(fd, pid);
688 EXPECT_EQ(ret, 0);
689 close(fd);
690 }
691
692 /**
693 * @tc.name: DmesgCatcherTest_005
694 * @tc.desc: add test
695 * @tc.type: FUNC
696 */
697 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_005, TestSize.Level1)
698 {
699 std::vector<pid_t> tids;
700 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
701 dmesgCatcher->GetTidsByPid(-1, tids);
702 EXPECT_TRUE(tids.size() == 0);
703 dmesgCatcher->GetTidsByPid(getpid(), tids);
704 EXPECT_TRUE(tids.size() > 0);
705 }
706 #endif // KERNELSTACK_CATCHER_ENABLE
707
708 #endif // DMESG_CATCHER_ENABLE
709
710 #ifdef STACKTRACE_CATCHER_ENABLE
711 /**
712 * @tc.name: OpenStacktraceCatcherTest_001
713 * @tc.desc: add testcase code coverage
714 * @tc.type: FUNC
715 */
716 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_001, TestSize.Level0)
717 {
718 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
719 if (fd < 0) {
720 printf("Fail to create catcherFile. errno: %d\n", errno);
721 FAIL();
722 }
723
724 auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
725 ASSERT_EQ(openStackCatcher->Initialize("", 0, 0), false);
726
727 int jsonFd = 1;
728 bool ret = openStackCatcher->Catch(fd, jsonFd);
729 EXPECT_TRUE(ret == 0);
730
731 EXPECT_EQ(openStackCatcher->Initialize("test", 0, 0), false);
732 ret = openStackCatcher->Catch(fd, jsonFd);
733 EXPECT_TRUE(ret == 0);
734
735 EXPECT_EQ(openStackCatcher->Initialize("", 1, 0), true);
736 EXPECT_EQ(openStackCatcher->Initialize("test", 1, 0), true);
737 ret = openStackCatcher->Catch(fd, jsonFd);
738 EXPECT_TRUE(ret > 0);
739 close(fd);
740 }
741
742 /**
743 * @tc.name: OpenStacktraceCatcherTest_002
744 * @tc.desc: add testcase code coverage
745 * @tc.type: FUNC
746 */
747 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_002, TestSize.Level1)
748 {
749 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
750 if (fd < 0) {
751 printf("Fail to create catcherFile. errno: %d\n", errno);
752 FAIL();
753 }
754
755 auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
756 bool ret = openStackCatcher->Catch(fd, 1);
757 EXPECT_TRUE(ret == 0);
758 EXPECT_EQ(openStackCatcher->ForkAndDumpStackTrace(fd), 0);
759 close(fd);
760 }
761 #endif // STACKTRACE_CATCHER_ENABLE
762
763 #ifdef BINDER_CATCHER_ENABLE
764 /**
765 * @tc.name: PeerBinderCatcherTest_001
766 * @tc.desc: add testcase code coverage
767 * @tc.type: FUNC
768 */
769 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_001, TestSize.Level0)
770 {
771 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
772 if (fd < 0) {
773 printf("Fail to create catcherFile. errno: %d\n", errno);
774 FAIL();
775 }
776 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
777 peerBinderCatcher->Initialize("PeerBinderCatcherTest", 0, 0);
778 int jsonFd = 1;
779 int res = peerBinderCatcher->Catch(fd, jsonFd);
780 EXPECT_TRUE(res < 0);
781
782 peerBinderCatcher->Initialize("a", 0, 0);
783 peerBinderCatcher->Initialize("a", 1, 0);
784 res = peerBinderCatcher->Catch(fd, jsonFd);
785 EXPECT_TRUE(res < 0);
786
787 peerBinderCatcher->Initialize("a", 1, 1);
788 res = peerBinderCatcher->Catch(fd, jsonFd);
789 if (Parameter::IsOversea()) {
790 EXPECT_TRUE(res == 0);
791 } else {
792 EXPECT_TRUE(res > 0);
793 }
794
795 int pid = CommonUtils::GetPidByName("foundation");
796 #ifdef HAS_HIPERF
797 std::set<int> pids;
798 pids.insert(pid);
799 peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids, pid, "r");
800 #endif
801 peerBinderCatcher->Initialize("", 0, pid);
802 peerBinderCatcher->Initialize("foundation", 0, pid);
803 peerBinderCatcher->Initialize("foundation", 1, pid);
804 peerBinderCatcher->CatcherFfrtStack(fd, pid);
805 peerBinderCatcher->CatcherStacktrace(fd, pid);
806 close(fd);
807 }
808
809 /**
810 * @tc.name: PeerBinderCatcherTest_002
811 * @tc.desc: add testcase code coverage
812 * @tc.type: FUNC
813 */
814 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_002, TestSize.Level1)
815 {
816 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
817 if (fd < 0) {
818 printf("Fail to create catcherFile. errno: %d\n", errno);
819 FAIL();
820 }
821 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
822 peerBinderCatcher->Initialize("a", 1, 0);
823 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
824 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("PeerBinderCatcherTest_002",
825 nullptr, jsonStr);
826 event->eventId_ = 0;
827 event->domain_ = "KERNEL_VENDOR";
828 event->eventName_ = "HUNGTASK";
829 event->SetEventValue("PID", 0);
830 std::string filePath = "/data/test/catcherFile";
831 std::set<int> catchedPids;
832 catchedPids.insert(0);
833 catchedPids.insert(1);
834 int pid = CommonUtils::GetPidByName("foundation");
835 catchedPids.insert(pid);
836 peerBinderCatcher->Init(event, filePath, catchedPids);
837 peerBinderCatcher->Initialize("foundation", 1, pid);
838 int res = peerBinderCatcher->Catch(fd, 1);
839 if (Parameter::IsOversea()) {
840 EXPECT_EQ(res, 0);
841 } else {
842 EXPECT_GT(res, 0);
843 }
844 close(fd);
845 }
846
847 /**
848 * @tc.name: PeerBinderCatcherTest_003
849 * @tc.desc: add testcase code coverage
850 * @tc.type: FUNC
851 */
852 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_003, TestSize.Level1)
853 {
854 std::map<int, std::list<OHOS::HiviewDFX::PeerBinderCatcher::BinderInfo>> manager;
855 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
856 std::set<int> pids;
857 int eventPid = 1;
858 int eventTid = 3;
859 PeerBinderCatcher::ParseBinderParam params = {eventPid, eventTid};
860
861 PeerBinderCatcher::BinderInfo info = {2, 0, 4, 0, 1};
862 manager[info.clientPid].push_back(info);
863 peerBinderCatcher->ParseBinderCallChain(manager, pids, eventPid, params, true);
864 EXPECT_TRUE(pids.empty());
865
866 PeerBinderCatcher::BinderInfo info1 = {1, 3, 5, 7, 3};
867 manager[info1.clientPid].push_back(info1);
868 PeerBinderCatcher::BinderInfo info2 = {5, 7, 9, 13, 3};
869 manager[info2.clientPid].push_back(info2);
870 PeerBinderCatcher::BinderInfo info3 = {5, 6, 10, 120, 3};
871 manager[info3.clientPid].push_back(info3);
872 PeerBinderCatcher::BinderInfo info4 = {9, 10, 11, 120, 3};
873 manager[info4.clientPid].push_back(info4);
874 PeerBinderCatcher::BinderInfo info5 = {9, 13, 19, 12666, 3};
875 manager[info5.clientPid].push_back(info5);
876 peerBinderCatcher->ParseBinderCallChain(manager, pids, eventPid, params, true);
877 EXPECT_EQ(pids.size(), 5);
878 EXPECT_EQ(peerBinderCatcher->terminalBinder_.pid, info5.serverPid);
879 EXPECT_EQ(peerBinderCatcher->terminalBinder_.tid, info5.serverTid);
880 #ifdef HAS_HIPERF
881 pids.insert(3);
882 pids.insert(4);
883 int processId = getpid();
884 std::string perfCmd = "r";
885 peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids, processId, perfCmd);
886 peerBinderCatcher->DumpHiperf(pids, processId, perfCmd);
887 #endif
888 }
889
890 /**
891 * @tc.name: PeerBinderCatcherTest_004
892 * @tc.desc: add testcase code coverage
893 * @tc.type: FUNC
894 */
895 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_004, TestSize.Level1)
896 {
897 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
898 if (fd < 0) {
899 printf("Fail to create catcherFile. errno: %d\n", errno);
900 FAIL();
901 }
902 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
903 std::set<int> asyncPids;
904 std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1, asyncPids);
905 EXPECT_TRUE(pids.empty());
906 close(fd);
907 }
908
909 /**
910 * @tc.name: PeerBinderCatcherTest_005
911 * @tc.desc: add testcase code coverage
912 * @tc.type: FUNC
913 */
914 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_005, TestSize.Level1)
915 {
916 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
917 std::list<PeerBinderCatcher::OutputBinderInfo> infoList;
918 peerBinderCatcher->AddBinderJsonInfo(infoList, -1);
919 PeerBinderCatcher::OutputBinderInfo info = {
920 .info = "Test",
921 .pid = 0
922 };
923 infoList.push_back(info);
924 peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
925 PeerBinderCatcher::OutputBinderInfo info1 = {
926 .info = "Test",
927 .pid = getpid()
928 };
929 infoList.push_back(info1);
930 peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
931 std::string str = "/proc/" + std::to_string(getpid()) + "/cmdline";
932 printf("%s\n", str.c_str());
933 EXPECT_TRUE(!str.empty());
934 }
935
936 /**
937 * @tc.name: PeerBinderCatcherTest_006
938 * @tc.desc: add testcase code coverage
939 * @tc.type: FUNC
940 */
941 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_006, TestSize.Level1)
942 {
943 auto fd = open("/data/test/peerFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
944 if (fd < 0) {
945 printf("Fail to create peerFile. errno: %d\n", errno);
946 FAIL();
947 }
948 std::ofstream testFile;
949 std::string path = "/data/test/peerFile";
950 testFile.open(path);
951 testFile << "0:pid\tcontext:binder\t0:request\t3:started\t"
952 "16:max\t4:ready\t521092:free_space\n";
953 testFile.close();
954 close(fd);
955
956 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
957 std::ifstream fin;
958 fin.open(path.c_str());
959 if (!fin.is_open()) {
960 printf("open binder file failed, %s\n.", path.c_str());
961 FAIL();
962 }
963 auto fd1 = open("/data/test/peerTestFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
964 if (fd1 < 0) {
965 printf("Fail to create peerTestFile. errno: %d\n", errno);
966 FAIL();
967 }
968 std::set<int> asyncPids;
969 peerBinderCatcher->BinderInfoParser(fin, fd1, 1, asyncPids);
970 std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1, asyncPids);
971 EXPECT_TRUE(pids.empty());
972 pids = peerBinderCatcher->GetBinderPeerPids(-1, 1, asyncPids);
973 EXPECT_TRUE(pids.empty());
974 fin.close();
975 close(fd1);
976 }
977
978 /**
979 * @tc.name: PeerBinderCatcherTest_007
980 * @tc.desc: add testcase code coverage
981 * @tc.type: FUNC
982 */
983 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_007, TestSize.Level1)
984 {
985 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
986 bool ret = peerBinderCatcher->IsAncoProc(getpid());
987 EXPECT_TRUE(!ret);
988 }
989
990 /**
991 * @tc.name: PeerBinderCatcherTest_008
992 * @tc.desc: add testcase code coverage
993 * @tc.type: FUNC
994 */
995 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_008, TestSize.Level1)
996 {
997 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
998 std::string str = "123";
999 uint16_t index = 1;
1000 std::string ret = peerBinderCatcher->StrSplit(str, index);
1001 EXPECT_EQ(ret, "");
1002 str = "123:456";
1003 ret = peerBinderCatcher->StrSplit(str, index);
1004 EXPECT_EQ(ret, "456");
1005
1006 bool isBinderMatchup = false;
1007 std::string line = " async_space";
1008
1009 auto fd = open("/data/test/peerBinderTestFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1010 if (fd < 0) {
1011 printf("Fail to create peerBinderTestFile. errno: %d\n", errno);
1012 FAIL();
1013 }
1014 peerBinderCatcher->SaveBinderLineToFd(fd, line, isBinderMatchup);
1015 EXPECT_FALSE(isBinderMatchup);
1016
1017 line = " free_async_space";
1018 peerBinderCatcher->SaveBinderLineToFd(fd, line, isBinderMatchup);
1019 EXPECT_TRUE(isBinderMatchup);
1020
1021 peerBinderCatcher->SaveBinderLineToFd(fd, line, isBinderMatchup);
1022 EXPECT_TRUE(isBinderMatchup);
1023
1024 fsync(fd);
1025 close(fd);
1026
1027 std::ifstream testFile("/data/test/peerBinderTestFile");
1028 if (testFile.is_open()) {
1029 std::string line;
1030 while (std::getline(testFile, line)) {
1031 printf("%s\n", line.c_str());
1032 EXPECT_TRUE(line.find("async_space") != std::string::npos);
1033 }
1034 testFile.close();
1035 }
1036 }
1037 #endif // BINDER_CATCHER_ENABLE
1038
1039 /**
1040 * @tc.name: ShellCatcherTest
1041 * @tc.desc: add testcase code coverage
1042 * @tc.type: FUNC
1043 */
1044 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_001, TestSize.Level0)
1045 {
1046 auto fd = open("/data/test/shellCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1047 if (fd < 0) {
1048 printf("Fail to create shellCatcherFile. errno: %d\n", errno);
1049 FAIL();
1050 }
1051
1052 auto shellCatcher = std::make_shared<ShellCatcher>();
1053 int pid = CommonUtils::GetPidByName("foundation");
1054
1055 #ifdef USAGE_CATCHER_ENABLE
1056 bool res = shellCatcher->Initialize("", ShellCatcher::CATCHER_WMS, pid);
1057 EXPECT_TRUE(res);
1058 #endif // USAGE_CATCHER_ENABLE
1059
1060 int jsonFd = 1;
1061 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) < 0);
1062
1063 std::string cmd = "ShellCatcherTest_001";
1064 #ifdef USAGE_CATCHER_ENABLE
1065 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_WMS, pid);
1066 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1067
1068 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_AMS, pid);
1069 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1070
1071 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_CPU, pid);
1072 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1073
1074 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_PMS, pid);
1075 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1076 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_DPMS, pid);
1077 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1078
1079 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_RS, pid);
1080 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1081 #endif // USAGE_CATCHER_ENABLE
1082
1083 #ifdef HILOG_CATCHER_ENABLE
1084 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_HILOG, 0);
1085 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1086
1087 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_LIGHT_HILOG, 0);
1088 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1089 #endif // HILOG_CATCHER_ENABLE
1090
1091 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SNAPSHOT, 0);
1092 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1093 close(fd);
1094 }
1095
1096 /**
1097 * @tc.name: ShellCatcherTest
1098 * @tc.desc: GET_DISPLAY_SNAPSHOT test
1099 * @tc.type: FUNC
1100 */
1101 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_002, TestSize.Level1)
1102 {
1103 auto fd = open("/data/test/shellCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1104 if (fd < 0) {
1105 printf("Fail to create shellCatcherFile. errno: %d\n", errno);
1106 FAIL();
1107 }
1108 auto shellCatcher = std::make_shared<ShellCatcher>();
1109 int jsonFd = 1;
1110 std::string cmd = "ShellCatcherTest_002";
1111 #ifdef HILOG_CATCHER_ENABLE
1112 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_INPUT_HILOG, 0);
1113 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1114 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_INPUT_EVENT_HILOG, 0);
1115 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1116 #endif // HILOG_CATCHER_ENABLE
1117
1118 #ifdef OTHER_CATCHER_ENABLE
1119 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_EEC, 0);
1120 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1121 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_GEC, 0);
1122 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1123 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_UI, 0);
1124 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1125 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SNAPSHOT, 0);
1126 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
1127 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_MMI, 0);
1128 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1129 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_DMS, 0);
1130 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1131 #endif // OTHER_CATCHER_ENABLE
1132 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
1133 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("ShellCatcherTest", nullptr, jsonStr);
1134 event->SetValue("FOCUS_WINDOW", 4); // 4 test value
1135 shellCatcher->SetEvent(event);
1136 #ifdef SCB_CATCHER_ENABLE
1137 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SCBWMSEVT, 0);
1138 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1139 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SCBVIEWPARAM, 0);
1140 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1141 shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SCBWMS, 0);
1142 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
1143 #endif // SCB_CATCHER_ENABLE
1144 close(fd);
1145 }
1146
1147 /**
1148 * @tc.name: ShellCatcherTest
1149 * @tc.desc: add test
1150 * @tc.type: FUNC
1151 */
1152 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_003, TestSize.Level1)
1153 {
1154 auto shellCatcher = std::make_shared<ShellCatcher>();
1155 shellCatcher->SetFocusWindowId("ShellCatcherTest_003");
1156 EXPECT_TRUE(!shellCatcher->focusWindowId_.empty());
1157 }
1158
1159 /**
1160 * @tc.name: LogCatcherUtilsTest_001
1161 * @tc.desc: add test
1162 * @tc.type: FUNC
1163 */
1164 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_001, TestSize.Level0)
1165 {
1166 auto fd = open("/data/test/dumpstacktrace_file", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1167 if (fd < 0) {
1168 printf("Fail to create dumpstacktrace_file. errno: %d\n", errno);
1169 FAIL();
1170 }
1171 int pid = getpid();
1172 std::string threadStack;
1173 int ret = LogCatcherUtils::DumpStacktrace(-1, pid, threadStack);
1174 EXPECT_EQ(ret, -1);
__anonbd564d190102null1175 std::thread thread1([pid]{
1176 auto fd1 = open("/data/test/dumpstacktrace_file1", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1177 if (fd1 < 0) {
1178 printf("Fail to create dumpstacktrace_file1. errno: %d\n", errno);
1179 FAIL();
1180 }
1181 std::string threadStack1;
1182 LogCatcherUtils::DumpStacktrace(fd1, pid, threadStack1);
1183 close(fd1);
1184 });
1185 if (thread1.joinable()) {
1186 thread1.detach();
1187 }
1188 ret = LogCatcherUtils::DumpStacktrace(fd, pid, threadStack);
1189 close(fd);
1190 EXPECT_TRUE(threadStack.empty());
1191 EXPECT_EQ(ret, 0);
1192 ret = LogCatcherUtils::WriteKernelStackToFd(200, "Test 01\n", getprocpid());
1193 EXPECT_EQ(ret, 0);
1194 ret = LogCatcherUtils::WriteKernelStackToFd(200, "Test 02\n", getprocpid());
1195 EXPECT_EQ(ret, 0);
1196 ret = LogCatcherUtils::WriteKernelStackToFd(2, "Test", -1);
1197 EXPECT_EQ(ret, -1);
1198 }
1199
1200 /**
1201 * @tc.name: LogCatcherUtilsTest_002
1202 * @tc.desc: add test
1203 * @tc.type: FUNC
1204 */
1205 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_002, TestSize.Level1)
1206 {
1207 std::string processStack = "LogCatcherUtilsTest_002";
1208 std::string stack = "";
1209 LogCatcherUtils::GetThreadStack(processStack, stack, 0);
1210 EXPECT_TRUE(stack.empty());
1211 int tid = gettid();
1212 processStack = "Tid:1234, Name: TestThread\n#00 pc 0017888c /system/lib/libark_jsruntime.so\n"
1213 "#01 pc 00025779 /system/lib/platformsdk/libipc_core.z.so";
1214 stack.clear();
1215 LogCatcherUtils::GetThreadStack(processStack, stack, tid);
1216 EXPECT_TRUE(stack.empty());
1217
1218 constexpr int testTid = 1234;
1219 stack.clear();
1220 processStack = "Tid:1234, Name: TestThread\n"
1221 "#00 pc 0017888c /system/lib/libark_jsruntime.so\n"
1222 "#01 pc 00025779 /system/lib/platformsdk/libipc_core.z.so";
1223 LogCatcherUtils::GetThreadStack(processStack, stack, testTid);
1224 EXPECT_FALSE(stack.empty());
1225
1226 stack.clear();
1227 processStack = "Tid:1234, Name: TestThread\n"
1228 "ThreadInfo:state=SLEEP, utime=1, stime=2, cutime=0, schedstat={254042, 0, 3}\n"
1229 "#00 pc 0017888c /system/lib/libark_jsruntime.so\n"
1230 "#01 pc 00025779 /system/lib/platformsdk/libipc_core.z.so";
1231 LogCatcherUtils::GetThreadStack(processStack, stack, testTid);
1232 const string result = "#00 pc 0017888c /system/lib/libark_jsruntime.so\n"
1233 "#01 pc 00025779 /system/lib/platformsdk/libipc_core.z.so\n";
1234 EXPECT_EQ(stack, result);
1235
1236 stack.clear();
1237 processStack = "Tid:1234, Name: TestThread\n"
1238 "ThreadInfo:state=SLEEP, utime=1, stime=2, cutime=0, schedstat={254042, 0, 3}\n"
1239 "#00 pc 0017888c /system/lib/libark_jsruntime.so\n"
1240 "#01 pc 00025779 /system/lib/platformsdk/libipc_core.z.so\n"
1241 "Tid:1235, Name: TestThread\n"
1242 "ThreadInfo:state=SLEEP, utime=1, stime=2, cutime=0, schedstat={254042, 0, 3}\n"
1243 "#00 pc 0028888c /system/lib/libark_jsruntime.so\n"
1244 "#01 pc 00036881 /system/lib/platformsdk/libipc_core.z.so";
1245 LogCatcherUtils::GetThreadStack(processStack, stack, testTid);
1246 EXPECT_EQ(stack, result);
1247 }
1248
1249 /**
1250 * @tc.name: LogCatcherUtilsTest_003
1251 * @tc.desc: add test
1252 * @tc.type: FUNC
1253 */
1254 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_003, TestSize.Level1)
1255 {
1256 auto fd = open("/data/test/logCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1257 if (fd < 0) {
1258 printf("Fail to create logCatcherFile. errno: %d\n", errno);
1259 FAIL();
1260 }
1261 int ret = LogCatcherUtils::DumpStackFfrt(fd, "");
1262 EXPECT_EQ(ret, 0);
1263 close(fd);
1264 }
1265
1266 /**
1267 * @tc.name: LogCatcherUtilsTest_004
1268 * @tc.desc: add test
1269 * @tc.type: FUNC
1270 */
1271 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_004, TestSize.Level1)
1272 {
1273 auto fd = open("/data/test/logCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1274 if (fd < 0) {
1275 printf("Fail to create logCatcherFile. errno: %d\n", errno);
1276 FAIL();
1277 }
1278 std::string serviceName = "ApplicationManagerService";
1279 std::string cmd = "Test";
1280 int count = 0;
1281 LogCatcherUtils::ReadShellToFile(fd, serviceName, cmd, count);
1282 EXPECT_EQ(count, 0);
1283 close(fd);
1284 }
1285
1286 #ifdef HITRACE_CATCHER_ENABLE
1287 /**
1288 * @tc.name: LogCatcherUtilsTest_005
1289 * @tc.desc: add test
1290 * @tc.type: FUNC
1291 */
1292 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_005, TestSize.Level1)
1293 {
1294 std::map<std::string, std::string> valuePairs;
1295 LogCatcherUtils::HandleTelemetryMsg(valuePairs);
1296 std::pair<std::string, std::string> telemetryInfo = LogCatcherUtils::GetTelemetryInfo();
1297
1298 EXPECT_EQ(telemetryInfo.first, "");
1299 EXPECT_EQ(telemetryInfo.second, "");
1300
1301 valuePairs["telemetryId"] = "testId2025";
1302 LogCatcherUtils::HandleTelemetryMsg(valuePairs);
1303 telemetryInfo = LogCatcherUtils::GetTelemetryInfo();
1304 EXPECT_EQ(telemetryInfo.first, "");
1305 EXPECT_EQ(telemetryInfo.second, "");
1306
1307 valuePairs["fault"] = "1";
1308 LogCatcherUtils::HandleTelemetryMsg(valuePairs);
1309 telemetryInfo = LogCatcherUtils::GetTelemetryInfo();
1310 EXPECT_EQ(telemetryInfo.first, "");
1311 EXPECT_EQ(telemetryInfo.second, "");
1312
1313 valuePairs["fault"] = "32";
1314 valuePairs["telemetryStatus"] = "on";
1315 valuePairs["traceAppFilter"] = "testPackageName2025";
1316 LogCatcherUtils::HandleTelemetryMsg(valuePairs);
1317 telemetryInfo = LogCatcherUtils::GetTelemetryInfo();
1318 EXPECT_EQ(telemetryInfo.first, "testId2025");
1319 EXPECT_EQ(telemetryInfo.second, "testPackageName2025");
1320
1321 LogCatcherUtils::FreezeFilterTraceOn("testPackageName2025");
1322 uint64_t faultTime = TimeUtil::GetMilliseconds() / 1000;
1323 auto dumpResult = LogCatcherUtils::FreezeDumpTrace(faultTime, true, "testPackageName2025");
1324 EXPECT_TRUE(!dumpResult.first.empty());
1325 EXPECT_TRUE(dumpResult.second.empty());
1326
1327 valuePairs["telemetryStatus"] = "off";;
1328 LogCatcherUtils::HandleTelemetryMsg(valuePairs);
1329 telemetryInfo = LogCatcherUtils::GetTelemetryInfo();
1330 EXPECT_EQ(telemetryInfo.first, "");
1331 EXPECT_EQ(telemetryInfo.second, "");
1332 }
1333 #endif
1334
1335 /**
1336 * @tc.name: ThermalInfoCatcherTest_001
1337 * @tc.desc: add testcase code coverage
1338 * @tc.type: FUNC
1339 */
1340 HWTEST_F(EventloggerCatcherTest, ThermalInfoCatcherTest_001, TestSize.Level1)
1341 {
1342 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1343 if (fd < 0) {
1344 printf("Fail to create catcherFile. errno: %d\n", errno);
1345 FAIL();
1346 }
1347
1348 auto thermalInfoCatcher = std::make_shared<ThermalInfoCatcher>();
1349 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
1350 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
1351 thermalInfoCatcher->SetEvent(sysEvent);
1352 int ret = thermalInfoCatcher->Catch(fd, 1);
1353 EXPECT_TRUE(ret > 0);
1354 close(fd);
1355 }
1356
1357 /**
1358 * @tc.name: SummaryLogInfoCatcherTest_001
1359 * @tc.desc: add testcase code coverage
1360 * @tc.type: FUNC
1361 */
1362 HWTEST_F(EventloggerCatcherTest, SummaryLogInfoCatcherCatcherTest_001, TestSize.Level1)
1363 {
1364 auto fd = open("/data/test/summaryLogInfoFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
1365 if (fd < 0) {
1366 printf("Fail to create summaryLogInfoFile. errno: %d\n", errno);
1367 FAIL();
1368 }
1369 uint64_t faultTime = TimeUtil::GetMilliseconds() / 1000;
1370 std::string formatTime = TimeUtil::TimestampFormatToDate(faultTime, "%Y/%m/%d-%H:%M:%S");
1371
1372 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
1373 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
1374 sysEvent->SetEventValue("MSG", "Fault time:" + formatTime + "\n");
1375 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(0, 0, sysEvent);
1376 logTask->SaveSummaryLogInfo();
1377
1378 auto summaryLogInfoCatcher = std::make_shared<SummaryLogInfoCatcher>();
1379 summaryLogInfoCatcher->SetFaultTime(static_cast<int64_t>(logTask->GetFaultTime()));
1380 bool ret = summaryLogInfoCatcher->Catch(fd, 1);
1381 close(fd);
1382 EXPECT_TRUE(!ret);
1383 EXPECT_EQ(logTask->GetFaultTime(), faultTime);
1384
1385 EXPECT_EQ(summaryLogInfoCatcher->CharArrayStr(nullptr, 8), "");
1386 char chars[8] = {'s', 'u', 'm', 'm', 'a', 'r', 'y', '\0'};
1387 EXPECT_EQ(summaryLogInfoCatcher->CharArrayStr(chars, 8), "summary");
1388
1389 std::shared_ptr<SysEvent> sysEvent1 = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
1390 sysEvent1->SetEventValue("MSG", "Fault time:xx:");
1391 sysEvent1->happenTime_ = TimeUtil::GetMilliseconds();
1392 logTask->faultTime_ = 0;
1393 logTask = std::make_unique<EventLogTask>(0, 0, sysEvent1);
1394 EXPECT_EQ(logTask->GetFaultTime(), sysEvent1->happenTime_ / 1000);
1395
1396 std::shared_ptr<SysEvent> sysEvent2 = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
1397 sysEvent2->SetEventValue("MSG", "Fault time:2025/07/14-18:28/59\\n");
1398 sysEvent2->happenTime_ = TimeUtil::GetMilliseconds();
1399 logTask->faultTime_ = 0;
1400 logTask = std::make_unique<EventLogTask>(0, 0, sysEvent2);
1401 EXPECT_EQ(logTask->GetFaultTime(), sysEvent2->happenTime_ / 1000);
1402 }
1403 } // namespace HiviewDFX
1404 } // namespace OHOS
1405