• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "plugin.h"
17 
18 #include <fstream>
19 #include <regex>
20 
21 #include "file_util.h"
22 #include "hisysevent.h"
23 #include "logger.h"
24 #include "parameter_ex.h"
25 #include "plugin_factory.h"
26 #include "sys_event_dao.h"
27 #include "string_util.h"
28 #include "tbox.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 REGISTER(HiCollieCollector);
33 DEFINE_LOG_TAG("HiCollieCollector");
34 
GetListenerName()35 std::string HiCollieCollector::GetListenerName()
36 {
37     return name_;
38 }
39 
ReadyToLoad()40 bool HiCollieCollector::ReadyToLoad()
41 {
42     return true;
43 }
44 
CanProcessEvent(std::shared_ptr<Event> event)45 bool HiCollieCollector::CanProcessEvent(std::shared_ptr<Event> event)
46 {
47     return false;
48 }
49 
OnLoad()50 void HiCollieCollector::OnLoad()
51 {
52     SetName("HiCollieCollector");
53     SetVersion("HiCollieCollector 1.0");
54     HIVIEW_LOGI("OnLoad.");
55     AddListenerInfo(Event::MessageType::SYS_EVENT, STRINGID_SERVICE_TIMEOUT);
56     AddListenerInfo(Event::MessageType::SYS_EVENT, STRINGID_SERVICE_BLOCK);
57     GetHiviewContext()->RegisterUnorderedEventListener(
58         std::static_pointer_cast<HiCollieCollector>(shared_from_this()));
59 }
60 
OnUnload()61 void HiCollieCollector::OnUnload()
62 {
63     HIVIEW_LOGI("OnUnload.");
64 }
65 
OnEvent(std::shared_ptr<Event> & event)66 bool HiCollieCollector::OnEvent(std::shared_ptr<Event> &event)
67 {
68     return true;
69 }
70 
OnUnorderedEvent(const Event & event)71 void HiCollieCollector::OnUnorderedEvent(const Event &event)
72 {
73     HIVIEW_LOGI("received event domain=%{public}s, stringid=%{public}s.\n",
74         event.domain_.c_str(), event.eventName_.c_str());
75     if (GetHiviewContext() == nullptr) {
76         HIVIEW_LOGE("failed to get context.");
77         return;
78     }
79 
80     if (event.eventName_ != STRINGID_SERVICE_TIMEOUT && event.eventName_ != STRINGID_SERVICE_BLOCK) {
81         HIVIEW_LOGE("invalid stringid=%{public}s.\n", event.eventName_.c_str());
82         return;
83     }
84 
85     Event& eventRef = const_cast<Event&>(event);
86     SysEvent& sysEvent = static_cast<SysEvent&>(eventRef);
87     ProcessHiCollieEvent(sysEvent);
88 }
89 
SelectEventFromDB(int32_t pid,const std::string & processName,const std::string & moduleName) const90 std::string HiCollieCollector::SelectEventFromDB(
91     int32_t pid, const std::string& processName, const std::string& moduleName) const
92 {
93     std::string ret;
94 
95     auto eventQuery = EventStore::SysEventDao::BuildQuery(EventStore::StoreType::FAULT);
96     std::vector<std::string> selections { EventStore::EventCol::TS };
97     (*eventQuery).Select(selections)
98         .And(EventStore::EventCol::DOMAIN, EventStore::Op::EQ, "FRAMEWORK")
99         .And(EventStore::EventCol::NAME, EventStore::Op::EQ, "SERVICE_WARNING")
100         .And(EventStore::EventCol::PID, EventStore::Op::EQ, pid)
101         .And("PROCESS_NAME", EventStore::Op::EQ, processName)
102         .And("MODULE_NAME", EventStore::Op::EQ, moduleName);
103 
104     EventStore::ResultSet set = eventQuery->Execute();
105     if (set.GetErrCode() != 0) {
106         HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
107         return "";
108     }
109 
110     while (set.HasNext()) {
111         auto record = set.Next();
112 
113         std::string info = record->GetEventValue(EventStore::EventCol::INFO);
114         std::regex reg("logPath:([^,]+)");
115         std::smatch result;
116         if (std::regex_search(info, result, reg)) {
117             ret = result[1];
118             break;
119         }
120     }
121     return ret;
122 }
123 
GetTimeString(unsigned long long timestamp) const124 std::string HiCollieCollector::GetTimeString(unsigned long long timestamp) const
125 {
126     struct tm tm;
127     time_t ts;
128     constexpr int TIME_STRING_LEN = 16;
129     ts = timestamp / 1000; // 1000 ms to s
130     localtime_r(&ts, &tm);
131     char buf[TIME_STRING_LEN] = {0};
132 
133     auto ret = strftime(buf, TIME_STRING_LEN - 1, "%Y%m%d%H%M%S", &tm);
134     if (ret) {
135         HIVIEW_LOGE("strftime error");
136     }
137     return std::string(buf, strlen(buf));
138 }
139 
SaveToFile(SysEvent & sysEvent,int32_t pid,const std::string & processName,const std::string & path,const std::string & desPath) const140 void HiCollieCollector::SaveToFile(SysEvent &sysEvent, int32_t pid, const std::string& processName,
141     const std::string& path, const std::string& desPath) const
142 {
143     std::string moduleName = StringUtil::TrimStr(sysEvent.GetEventValue("MODULE_NAME"));
144     std::ostringstream desText;
145     desText << "DEVICE_INFO:" << Parameter::GetString("const.product.name", "Unknown") << std::endl;
146     desText << "BUILD_INFO:" << Parameter::GetString("const.product.software.version", "Unknown") << std::endl;
147     desText << "MODULE:" << processName << "-" << moduleName << std::endl;
148     desText << "SUMMARY:" << sysEvent.GetEventValue("MSG") << std::endl;
149     desText << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
150     std::ifstream ifs(path, std::ios::in);
151     if (!ifs.is_open()) {
152         return;
153     }
154     desText << ifs.rdbuf();
155     ifs.close();
156     if (sysEvent.eventName_ == STRINGID_SERVICE_BLOCK) {
157         std::string log = SelectEventFromDB(pid, processName, moduleName);
158         if (log != "" && FileUtil::FileExists(log)) {
159             std::ifstream fin(log, std::ios::in);
160             if (!fin.is_open()) {
161                 return;
162             }
163             desText << "\n*******************************************\n";
164             desText << fin.rdbuf();
165             fin.close();
166         }
167     }
168     if (!FileUtil::SaveStringToFile(desPath, desText.str())) {
169         HIVIEW_LOGE("failed to save log file %{public}s.", desPath.c_str());
170     }
171 }
172 
ProcessHiCollieEvent(SysEvent & sysEvent)173 void HiCollieCollector::ProcessHiCollieEvent(SysEvent &sysEvent)
174 {
175     std::string path = "";
176     int32_t pid = static_cast<int32_t>(sysEvent.GetEventIntValue("PID"));
177     pid = pid ? pid : sysEvent.GetPid();
178     int32_t uid = sysEvent.GetEventIntValue("UID");
179     uid = uid ? uid : sysEvent.GetUid();
180 
181     std::string timestamp = GetTimeString(sysEvent.happenTime_);
182     std::string processName = StringUtil::TrimStr(sysEvent.GetEventValue("PROCESS_NAME"));
183     if (processName == "") {
184         processName = std::to_string(pid);
185     }
186 
187     std::string desFile = "";
188     if (sysEvent.eventName_ == STRINGID_SERVICE_BLOCK) {
189         desFile = "watchdog-" + processName + "-" + std::to_string(uid) + "-" + timestamp;
190     } else if (sysEvent.eventName_ == STRINGID_SERVICE_TIMEOUT) {
191         desFile = "timeout-" + processName + "-" + std::to_string(uid) + "-" + timestamp;
192     }
193 
194     if (desFile == "") {
195         desFile = FileUtil::ExtractFileName(path);
196     }
197     std::string desPath = FAULT_LOG_PATH + desFile;
198 
199     std::string info = sysEvent.GetEventValue(EventStore::EventCol::INFO);
200     std::regex reg("logPath:([^,]+)");
201     std::smatch result;
202     if (!std::regex_search(info, result, reg)) {
203         return;
204     }
205     path = result[1].str();
206     SaveToFile(sysEvent, pid, processName, path, desPath);
207 
208     std::vector<std::string> paths = {desPath};
209     HiSysEvent::Write("RELIABILITY", sysEvent.eventName_ + "_REPORT", HiSysEvent::FAULT,
210         "HIVIEW_LOG_FILE_PATHS", paths,
211         "PID", sysEvent.GetEventValue(EVENT_PID),
212         "TGID", sysEvent.GetEventValue(EVENT_TGID),
213         "MSG", sysEvent.GetEventValue(EVENT_MSG));
214     ReportSysFreezeIfNeed(sysEvent, timestamp, processName, desPath);
215     HIVIEW_LOGI("send event [%{public}s, %{public}s] msg:%{public}s path:%{public}s",
216         "RELIABILITY", sysEvent.eventName_.c_str(), sysEvent.GetEventValue(EVENT_MSG).c_str(), desPath.c_str());
217 }
218 
ShouldReportSysFreeze(const std::string & processName)219 bool HiCollieCollector::ShouldReportSysFreeze(const std::string& processName)
220 {
221     if ((processName.find("foundation") == std::string::npos) &&
222         (processName.find("render_service") == std::string::npos)) {
223         return false;
224     }
225 
226     return true;
227 }
228 
ReportSysFreezeIfNeed(SysEvent & sysEvent,const std::string & timestamp,const std::string & processName,const std::string & path)229 void HiCollieCollector::ReportSysFreezeIfNeed(SysEvent &sysEvent, const std::string& timestamp, const std::string& processName,
230     const std::string& path)
231 {
232     if (!ShouldReportSysFreeze(processName)) {
233         return;
234     }
235 
236     int32_t pid = static_cast<int32_t>(sysEvent.GetEventIntValue("PID"));
237     pid = pid ? pid : sysEvent.GetPid();
238     int32_t uid = sysEvent.GetEventIntValue("UID");
239     uid = uid ? uid : sysEvent.GetUid();
240     std::string fingerPrint = Tbox::CalcFingerPrint(processName, 0, FP_BUFFER);
241     HiSysEvent::Write("RELIABILITY", "SYS_FREEZE", HiSysEvent::FAULT,
242         "LOG_PATH", path,
243         "PID", pid,
244         "UID", uid,
245         "MODULE", processName,
246         "REASON", "Watchdog",
247         "EVENT_TIME", timestamp,
248         "FINGERPRINT", fingerPrint,
249         "FIRST_FRAME", "/",
250         "SECOND_FRAME", "/",
251         "LAST_FRAME", "/",
252         "SUMMARY", sysEvent.GetEventValue(EVENT_MSG));
253 }
254 } // namespace HiviewDFX
255 } // namespace OHOS
256