1 /*
2 * Copyright (C) 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 "summary_log_info_catcher.h"
16
17 #include <fcntl.h>
18 #include <sys/ioctl.h>
19 #include <unistd.h>
20
21 #include "file_util.h"
22 #include "hiview_logger.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 #define SUMMARY_LOG_BASE 'S'
28 #define GET_SUMMARY_LOG _IOWR(SUMMARY_LOG_BASE, 0x01, int32_t)
29
30 static constexpr int LINE_BASE_SIZE = 108;
31 static constexpr int SUMMARY_LOG_INFO_MAX_SIZE = 10;
32 struct summary_log_line_info {
33 int64_t sec_timestamp;
34 char buffer[LINE_BASE_SIZE];
35 };
36 struct ringbuff_log_info {
37 int64_t needed_sec_timestamp;
38 unsigned int size;
39 struct summary_log_line_info line[SUMMARY_LOG_INFO_MAX_SIZE];
40 };
41 }
42
43 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-SummaryLogInfoCatcher");
SummaryLogInfoCatcher()44 SummaryLogInfoCatcher::SummaryLogInfoCatcher() : EventLogCatcher()
45 {
46 name_ = "SummaryLogInfoCatcher";
47 }
48
Initialize(const std::string & strParam1,int intParam1,int intParam2)49 bool SummaryLogInfoCatcher::Initialize(const std::string& strParam1, int intParam1, int intParam2)
50 {
51 // this catcher do not need parameters, just return true
52 description_ = "SummaryLogInfoCatcher --\n";
53 return true;
54 }
55
SetFaultTime(int64_t faultTime)56 void SummaryLogInfoCatcher::SetFaultTime(int64_t faultTime)
57 {
58 faultTime_ = faultTime;
59 }
60
Catch(int fd,int jsonFd)61 int SummaryLogInfoCatcher::Catch(int fd, int jsonFd)
62 {
63 int originSize = GetFdSize(fd);
64
65 int sysLoadFd = open("/dev/sysload", O_RDWR);
66 if (sysLoadFd < 0) {
67 HIVIEW_LOGE("open /dev/sysload failed!");
68 return 0;
69 }
70 ringbuff_log_info info = { faultTime_, sizeof(info.line) / sizeof(info.line[0]) };
71 int res = ioctl(sysLoadFd, GET_SUMMARY_LOG, &info);
72 if (res < 0) {
73 HIVIEW_LOGE("ioctl failed, errno:%{public}d", errno);
74 close(sysLoadFd);
75 return 0;
76 }
77 close(sysLoadFd);
78 HIVIEW_LOGI("ioctl res:%{public}d, info size:%{public}d", res, info.size);
79
80 std::string summaryLogInfoStr;
81 std::string lineStr;
82 for (const summary_log_line_info &lineInfo: info.line) {
83 if (lineInfo.sec_timestamp == 0) {
84 continue;
85 }
86 lineStr = "timestamp=" + std::to_string(lineInfo.sec_timestamp) +
87 ", data=" + CharArrayStr(lineInfo.buffer, static_cast<size_t>(LINE_BASE_SIZE)) + "\n";
88 summaryLogInfoStr += lineStr;
89 }
90 FileUtil::SaveStringToFd(fd, summaryLogInfoStr);
91
92 logSize_ = GetFdSize(fd) - originSize;
93 if (logSize_ <= 0) {
94 FileUtil::SaveStringToFd(fd, "summary log info is empty or saving it failed!\n");
95 }
96 return logSize_;
97 }
98
CharArrayStr(const char * chars,size_t maxSize)99 std::string SummaryLogInfoCatcher::CharArrayStr(const char* chars, size_t maxSize)
100 {
101 if (!chars) {
102 return "";
103 }
104
105 size_t length = 0;
106 while (length < maxSize && chars[length] != '\0') {
107 length++;
108 }
109 return std::string(chars, length);
110 }
111 } // namespace HiviewDFX
112 } // namespace OHOS