• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "report.h"
17 
18 #include <algorithm>
19 #include <dirent.h>
20 #include <fstream>
21 #include <iostream>
22 #include <sstream>
23 #include <sys/inotify.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 
27 #include "ability_manager_client.h"
28 #include "element_name.h"
29 #include "exception_manager.h"
30 #include "filter_category.h"
31 #include "format_csv.h"
32 #include "format_json.h"
33 #include "statistics_ability.h"
34 #include "statistics_componment.h"
35 #include "statistics_event.h"
36 #include "statistics_exception.h"
37 #include "string_ex.h"
38 #include "wukong_define.h"
39 #include "wukong_util.h"
40 
41 namespace OHOS {
42 namespace WuKong {
43 namespace {
44 const uint32_t SEGMENT_STATISTICS_LENGTH = 10;
45 std::string crashDir = "/data/log/faultlog/faultlogger/";
ListenCrashDir()46 void ListenCrashDir()
47 {
48     int fd;
49     int wd;
50     ssize_t readLenght;
51     char buf[BUFSIZ];
52     char* bufPtr = nullptr;
53     struct inotify_event *event;
54     fd = inotify_init();
55     INFO_LOG("init notify");
56     if (fd < 0) {
57         return;
58     }
59     wd = inotify_add_watch(fd, "/data/log/faultlog/faultlogger/", IN_CLOSE_WRITE);
60     INFO_LOG("add_watch");
61     if (wd < 0) {
62         ERROR_LOG("inotify_add_watch /data/log/faultlog/faultlogger/ failed");
63         return;
64     }
65     buf[sizeof(buf) - 1] = 0;
66     std::string destDir = Report::GetInstance()->GetReportExceptionDir();
67     while ((readLenght = read(fd, buf, sizeof(buf) - 1)) > 0) {
68         uint32_t len = static_cast<uint32_t>(readLenght);
69         uint32_t nread = 0;
70         while (len > 0) {
71             bufPtr = &buf[nread];
72             void* middleType =  static_cast<void *>(bufPtr);
73             event = static_cast<struct inotify_event *>(middleType);
74             if ((event->mask & IN_CLOSE_WRITE) && (event->len > 0)) {
75                 DEBUG_LOG_STR("event->mask{%x}", event->mask);
76                 std::string targetFile(event->name);
77                 WuKongUtil::GetInstance()->CopyFile(targetFile, crashDir, destDir);
78                 DEBUG_LOG_STR("%s --- IN_CLOSE_WRITE\n", event->name);
79                 Report::GetInstance()->ExceptionRecord(targetFile);
80             }
81             nread = nread + sizeof(struct inotify_event) + event->len;
82             len = len - sizeof(struct inotify_event) - event->len;
83         }
84     }
85     INFO_LOG("exit thread");
86     return;
87 }
88 
StartCrashDirListen()89 void StartCrashDirListen()
90 {
91     std::thread listenerThread(&ListenCrashDir);
92     INFO_LOG("create listener thread");
93     listenerThread.detach();
94     INFO_LOG("thread detach");
95 }
96 }  // namespace
97 using namespace OHOS::AAFwk;
Report()98 Report::Report()
99 {
100     EnvInit();
101     DataSetInit();
102 }
103 
EnvInit()104 void Report::EnvInit()
105 {
106     const std::string DEFAULT_DIR = "/data/local/tmp/wukong/report/";
107     startRunTime_ = WuKongUtil::GetInstance()->GetStartRunTime();
108     // Get a screenshot within the previous timestamp of the current timestamp
109     DIR *dirp = nullptr;
110     dirp = opendir(DEFAULT_DIR.c_str());
111     std::string maxValue = "";
112     std::string targetTimeDir;
113     // setting filename
114     currentTestDir_ = WuKongUtil::GetInstance()->GetCurrentTestDir();
115     INFO_LOG_STR("Report currentTestDir: (%s)", currentTestDir_.c_str());
116     // setting filename
117     reportCsvFileName_ = currentTestDir_ + "wukong_report.csv";
118     reportJsonFileName_ = currentTestDir_ + "data.js";
119 
120     INFO_LOG_STR("Report CSV: (%s)", reportCsvFileName_.c_str());
121     INFO_LOG_STR("Report JSON: (%s)", reportJsonFileName_.c_str());
122 
123     reportExceptionDir_ = currentTestDir_ + "exception/";
124     INFO_LOG_STR("Report exception dir: (%s)", reportExceptionDir_.c_str());
125     int dirExist = access(reportExceptionDir_.c_str(), F_OK);
126     if (dirExist != 0) {
127         int dirStatus = mkdir((reportExceptionDir_).c_str(), 0777);
128         if (dirStatus == -1) {
129             ERROR_LOG("exception dir create fail");
130         }
131     }
132     StartCrashDirListen();
133     // register crash catcher
134     ExceptionManager::GetInstance()->StartCatching();
135     if (dirp == nullptr) {
136         ERROR_LOG_STR("dir{%s} opendir error", DEFAULT_DIR.c_str());
137         return;
138     }
139     while (dirp != nullptr) {
140         struct dirent *dp;
141         if ((dp = readdir(dirp)) == NULL) {
142             break;
143         }
144         std::string currentStringName(dp->d_name);
145         if (currentStringName != startRunTime_) {
146             if (currentStringName > maxValue) {
147                 maxValue = currentStringName;
148                 targetTimeDir = currentStringName;
149             }
150         }
151     }
152     (void)closedir(dirp);
153     // Delete the screenshot under the timestamp
154     std::string targetDir_ = DEFAULT_DIR + targetTimeDir +"/screenshot/";
155     WuKongUtil::GetInstance()->DeleteFile(targetDir_);
156 }
157 
DataSetInit()158 void Report::DataSetInit()
159 {
160     std::shared_ptr<Filter> categoryFilter = std::make_shared<FilterCategory>();
161     eventDataSet_->SetFilterStragety(categoryFilter);
162     eventDataSet_->SetFilterType("event");
163     std::shared_ptr<Statistics> eventSatistics = std::make_shared<StatisticsEvent>();
164     eventDataSet_->SetStatisticsStragety(eventSatistics);
165 
166     // set componment filter,statistics,format
167     componmentDataSet_->SetFilterStragety(categoryFilter);
168     componmentDataSet_->SetFilterType("componment");
169     std::shared_ptr<Statistics> componmentSatistics = std::make_shared<StatisticsComponment>();
170     componmentDataSet_->SetStatisticsStragety(componmentSatistics);
171 
172     // set ability filter,statistics,format
173     abilityDataSet_->SetFilterStragety(categoryFilter);
174     abilityDataSet_->SetFilterType("abilityName");
175     std::shared_ptr<Statistics> abilitySatistics = std::make_shared<StatisticsAbility>();
176     abilityDataSet_->SetStatisticsStragety(abilitySatistics);
177 
178     // set exception filter,statistics,format
179     exceptionDataSet_->SetFilterStragety(categoryFilter);
180     exceptionDataSet_->SetFilterType("exception");
181     std::shared_ptr<Statistics> exceptionSatistics = std::make_shared<StatisticsException>();
182     exceptionDataSet_->SetStatisticsStragety(exceptionSatistics);
183 }
184 
SyncInputInfo(std::shared_ptr<InputedMsgObject> inputedMsgObject)185 void Report::SyncInputInfo(std::shared_ptr<InputedMsgObject> inputedMsgObject)
186 {
187     TRACK_LOG_STD();
188     std::shared_ptr<AbilityManagerClient> abilityManagerClient = AbilityManagerClient::GetInstance();
189     OHOS::AppExecFwk::ElementName elementName = abilityManagerClient->GetTopAbility();
190     std::map<std::string, std::string> data;
191     data["bundleName"] = elementName.GetBundleName();
192     data["abilityName"] = elementName.GetAbilityName();
193     DEBUG_LOG_STR("bundleName{%s} abilityName{%s} ", data["bundleName"].c_str(), data["abilityName"].c_str());
194     inputedMode inputMode = inputedMsgObject->GetInputedMode();
195     switch (inputMode) {
196         case multimodeInput: {
197             auto inputMutlMsgPtr = std::static_pointer_cast<MultimodeInputMsg>(inputedMsgObject);
198             data["event"] = inputMutlMsgPtr->GetInputType();
199             DEBUG_LOG_STR("eventType{%s}", data["event"].c_str());
200             break;
201         }
202 
203         case componmentInput: {
204             auto inputCompMsgPtr = std::static_pointer_cast<ComponmentInputMsg>(inputedMsgObject);
205             ComponmentInfoArrange(data["bundleName"], inputCompMsgPtr, data);
206             DEBUG_LOG("componmentType map");
207             break;
208         }
209         default:
210             break;
211     }
212 
213     // first appswitch abandon
214     std::map<std::string, std::string>::iterator it = data.find("event");
215     if (it != data.end() && (data["event"] == "appswitch") && (isFirstAppSwitch_ == false)) {
216         DEBUG_LOG("first appswitch abandon");
217         isFirstAppSwitch_ = true;
218         return;
219     }
220     // record app used to control data display
221     std::vector<std::string>::iterator bundleIter = std::find(bundles_.begin(), bundles_.end(), data["bundleName"]);
222     if (bundleIter == bundles_.end()) {
223         DEBUG_LOG_STR("push apps item{%s}", data["bundleName"].c_str());
224         bundles_.push_back(data["bundleName"]);
225     }
226     // send `k => v` to filter
227     eventDataSet_->FilterData(data);
228     componmentDataSet_->FilterData(data);
229     abilityDataSet_->FilterData(data);
230     taskCount_++;
231     DEBUG_LOG_STR("taskCount{%d}", taskCount_);
232     // statistics and storage every 10 data
233     if ((taskCount_ % SEGMENT_STATISTICS_LENGTH) == 0) {
234         HilogFileRecord();
235         SegmentedWriteCSV();
236         SegmentedWriteJson();
237     }
238     TRACK_LOG_END();
239 }
240 
~Report()241 Report::~Report()
242 {
243 }
244 
SegmentedWriteCSV()245 void Report::SegmentedWriteCSV()
246 {
247     TRACK_LOG_STD();
248     // csv report format
249     if (reportCsvFileName_.empty()) {
250         return;
251     }
252     std::shared_ptr<FormatCSV> formatCSV = std::make_shared<FormatCSV>();
253     eventDataSet_->SetFormatStragety(formatCSV);
254     componmentDataSet_->SetFormatStragety(formatCSV);
255     abilityDataSet_->SetFormatStragety(formatCSV);
256     exceptionDataSet_->SetFormatStragety(formatCSV);
257     std::stringstream modules;
258     modules << "module, Base Info" << std::endl;
259     modules << "name, base" << std::endl;
260     modules << "detail, info" << std::endl;
261     modules << "name, base, detail, info" << std::endl;
262     modules << "task status, success" << std::endl;
263     modules << "task time  , " << time(0) - startTime_ << std::endl;
264     if (!seed_.empty()) {
265         modules << "seed , " << seed_ << std::endl;
266     }
267     modules << "task count , " << taskCount_ << std::endl;
268     DEBUG_LOG("start event statistics");
269     eventDataSet_->StatisticsData();
270     DEBUG_LOG("end event statistics");
271     DEBUG_LOG("start componment statistics");
272     componmentDataSet_->StatisticsData();
273     DEBUG_LOG("end componment statistics");
274     std::string moduleInput;
275     modules << "module, Input Message Statistics" << std::endl;
276     modules << "name, all";
277     // show all app and detail
278     for (auto bundleIter : bundles_) {
279         modules << ", " << bundleIter;
280     }
281     modules << std::endl;
282     modules << "detail, event, componment" << std::endl;
283     eventDataSet_->FormatData("all", moduleInput);
284     componmentDataSet_->FormatData("all", moduleInput);
285     // loop app show name-type statistics content
286     for (auto bundleIter : bundles_) {
287         eventDataSet_->FormatData(bundleIter, moduleInput);
288         componmentDataSet_->FormatData(bundleIter, moduleInput);
289     }
290     modules << moduleInput;
291     modules << "module, ability Statistics" << std::endl;
292     modules << "name, all" << std::endl;
293     modules << "detail, ability" << std::endl;
294     moduleInput = "";
295     abilityDataSet_->StatisticsData();
296     abilityDataSet_->FormatData("all", moduleInput);
297     modules << moduleInput;
298 
299     std::unique_lock<std::mutex> locker(crashMtx_);
300     modules << "module, Exception Message Statistics" << std::endl;
301     modules << "name, exception" << std::endl;
302     modules << "detail, statistics" << std::endl;
303     moduleInput = "";
304     exceptionDataSet_->StatisticsData();
305     exceptionDataSet_->FormatData("exception", moduleInput);
306     modules << moduleInput;
307     locker.unlock();
308     std::string csvContent = modules.str();
309     std::fstream csvFileStream(reportCsvFileName_, std::ios::out | std::ios::trunc);
310     csvFileStream << csvContent << std::endl;
311     csvFileStream.close();
312     TRACK_LOG_END();
313 }
314 
SegmentedWriteJson()315 void Report::SegmentedWriteJson()
316 {
317     TRACK_LOG_STD();
318     DEBUG_LOG("SegmentedWriteJson start");
319     // csv report format
320     if (reportCsvFileName_.empty()) {
321         return;
322     }
323     std::shared_ptr<FormatJSON> formatJSON = std::make_shared<FormatJSON>();
324     eventDataSet_->SetFormatStragety(formatJSON);
325     componmentDataSet_->SetFormatStragety(formatJSON);
326     abilityDataSet_->SetFormatStragety(formatJSON);
327     exceptionDataSet_->SetFormatStragety(formatJSON);
328     std::stringstream modules;
329     std::string moduleInput;
330     modules << "var reportJson = {" << std::endl;
331     modules << "base: [" << std::endl;
332     modules << "{ item: \"task status\", detail: \" success \"}," << std::endl;
333     modules << "{ item: \"task time\", detail: \" " << time(0) - startTime_ << "s\"}," << std::endl;
334     modules << "{ item: \"task count\", detail: \" " << taskCount_ << "\"}," << std::endl;
335     if (!seed_.empty()) {
336         modules << "{ item: \"seed\", detail: \" " << seed_ << "\"}," << std::endl;
337     }
338     modules << "]," << std::endl;
339     modules << "detailApps:{" << std::endl;
340     modules << "names:[ \"all\"";
341     // show all app and detail
342     for (auto bundleIter : bundles_) {
343         modules << ", \"" << bundleIter << " \"";
344     }
345     modules << "]," << std::endl;
346     modules << "details: [" << std::endl;
347     modules << "{" << std::endl;
348     modules << "eventStatistics:" << std::endl;
349     eventDataSet_->FormatData("all", moduleInput);
350     modules << moduleInput;
351     modules << "controlStatistics:";
352     componmentDataSet_->FormatData("all", moduleInput);
353     modules << moduleInput;
354     modules << "},";
355     // loop app show name-type statistics content
356     for (auto bundleIter : bundles_) {
357         modules << "{" << std::endl;
358         modules << "eventStatistics:";
359         eventDataSet_->FormatData(bundleIter, moduleInput);
360         modules << moduleInput;
361         modules << "controlStatistics:";
362         componmentDataSet_->FormatData(bundleIter, moduleInput);
363         modules << moduleInput;
364         modules << "},";
365     }
366     modules << "]" << std::endl;
367     modules << "}," << std::endl;
368     modules << "abilityStatistics:";
369     abilityDataSet_->FormatData("all", moduleInput);
370     modules << moduleInput;
371     modules << "detailException: {" << std::endl;
372     modules << "names: [\"exception statistics\", \"cpp crash statistics\", \"js crash statistics\"]," << std::endl;
373     modules << "details: [" << std::endl;
374     modules << "{" << std::endl;
375     modules << "exception_statistics: {" << std::endl;
376     modules << "header: [\"Type\", \"Times\", \"Proportion\"]," << std::endl;
377     modules << "content: " << std::endl;
378     exceptionDataSet_->FormatData("exception", moduleInput);
379     modules << moduleInput;
380     modules << "}," << std::endl;
381     modules << "}," << std::endl;
382     modules << "]" << std::endl;
383     modules << "}," << std::endl;
384     unsigned int index = 0;
385     modules << "screens:[" << std::endl;
386     for (auto srceen : screenPaths_) {
387         modules << "{index:\"" << index << "\","
388                 << "path:\"" << srceen << "\"}," << std::endl;
389         index++;
390     }
391     modules << "]," << std::endl;
392     modules << "};" << std::endl;
393     std::string jsonContent = modules.str();
394     std::fstream jsonFileStream(reportJsonFileName_, std::ios::out | std::ios::trunc);
395     jsonFileStream << jsonContent << std::endl;
396     jsonFileStream.close();
397     DEBUG_LOG("SegmentedWriteJson end");
398     TRACK_LOG_END();
399 }
400 
HilogFileRecord()401 void Report::HilogFileRecord()
402 {
403     struct dirent *dp;
404     DIR *dirpHilog = nullptr;
405     std::shared_ptr<WuKongUtil> utilPtr = WuKongUtil::GetInstance();
406     dirpHilog = opendir(hilogDirs_.c_str());
407     if (dirpHilog == nullptr) {
408         ERROR_LOG_STR("dir{%s} opendir error", hilogDirs_.c_str());
409         return;
410     }
411     while ((dp = readdir(dirpHilog)) != NULL) {
412         std::string targetFile(dp->d_name);
413         if ((strcmp(dp->d_name, ".") != 0) && (strcmp(dp->d_name, "..") != 0)) {
414             std::vector<std::string>::iterator iterDir = find(hilogFiles_.begin(), hilogFiles_.end(), targetFile);
415             if (iterDir == hilogFiles_.end()) {
416                 DEBUG_LOG("hilog copy action");
417                 utilPtr->CopyFile(targetFile, hilogDirs_, reportExceptionDir_);
418                 hilogFiles_.push_back(targetFile);
419             }
420         }
421     }
422     if (dirpHilog != nullptr) {
423         (void)closedir(dirpHilog);
424     }
425 }
426 
ExceptionRecord(const std::string & exceptionFilename)427 void Report::ExceptionRecord(const std::string &exceptionFilename)
428 {
429     std::unique_lock<std::mutex> locker(crashMtx_);
430     std::map<std::string, std::string> data;
431     std::string exceptionType;
432     if (exceptionFilename.find("cppcrash") != std::string::npos) {
433         exceptionType = "cppcrash";
434     }
435 
436     if (exceptionFilename.find("appfreeze") != std::string::npos) {
437         exceptionType = "appfreeze";
438     }
439 
440     if (exceptionFilename.find("jscrash") != std::string::npos) {
441         exceptionType = "jscrash";
442     }
443 
444     if (exceptionFilename.find("serviceblock") != std::string::npos) {
445         exceptionType = "serviceblock";
446     }
447 
448     data["exception"] = exceptionType;
449     exceptionDataSet_->FilterData(data);
450 }
451 
Finish()452 void Report::Finish()
453 {
454     SegmentedWriteCSV();
455     SegmentedWriteJson();
456     ExceptionManager::GetInstance()->StopCatching();
457 }
458 
SetSeed(std::string seed)459 void Report::SetSeed(std::string seed)
460 {
461     seed_ = seed;
462 }
463 
ComponmentInfoArrange(const std::string & bundle,std::shared_ptr<ComponmentInputMsg> inputCompMsgPtr,std::map<std::string,std::string> & data)464 void Report::ComponmentInfoArrange(const std::string &bundle, std::shared_ptr<ComponmentInputMsg> inputCompMsgPtr,
465                                    std::map<std::string, std::string> &data)
466 {
467     std::map<std::string, componmentRecord>::iterator bundleComponmentRecordIter;
468     componmentRecord componmentRecord;
469     bundleComponmentRecordIter = bundleComponmentRecord_.find(bundle);
470     if (bundleComponmentRecordIter != bundleComponmentRecord_.end()) {
471         componmentRecord = bundleComponmentRecordIter->second;
472     }
473     componmentRecord.pageIdComponments[inputCompMsgPtr->pageId_] = inputCompMsgPtr->pageComponments;
474     std::map<std::string, uint32_t>::iterator componmentTypeCountIter;
475     uint32_t componmentTypeInputedCount = 0, componmentTypeTotal = 0;
476     componmentTypeCountIter = componmentRecord.componmentTypeCount.find(inputCompMsgPtr->componmentType_);
477     if (componmentTypeCountIter != componmentRecord.componmentTypeCount.end()) {
478         componmentTypeInputedCount = componmentTypeCountIter->second;
479     }
480     componmentTypeInputedCount++;
481 
482     for (auto pageIdComponmentsIter : componmentRecord.pageIdComponments) {
483         for (auto componmentVectorIter : pageIdComponmentsIter.second) {
484             if (componmentVectorIter.compare(inputCompMsgPtr->componmentType_) == 0) {
485                 componmentTypeTotal++;
486             }
487         }
488     }
489     if (componmentTypeInputedCount > componmentTypeTotal) {
490         componmentTypeInputedCount = componmentTypeTotal;
491     }
492 
493     componmentRecord.componmentTypeCount[inputCompMsgPtr->componmentType_] = componmentTypeInputedCount;
494     data["componment"] = inputCompMsgPtr->componmentType_;
495     data["inputedTimes"] = std::to_string(componmentTypeInputedCount);
496     data["componmentTotals"] = std::to_string(componmentTypeTotal);
497     DEBUG_LOG_STR("componmentType{%s} inputedTimes{%s} componmentTotals{%s}", data["componment"].c_str(),
498                   data["inputedTimes"].c_str(), data["componmentTotals"].c_str());
499     bundleComponmentRecord_[bundle] = componmentRecord;
500 }
501 
RecordScreenPath(const std::string & screenPath)502 void Report::RecordScreenPath(const std::string &screenPath)
503 {
504     TRACK_LOG_STD();
505     screenPaths_.push_back(screenPath);
506     TRACK_LOG_END();
507 }
508 
GetReportExceptionDir()509 std::string Report::GetReportExceptionDir()
510 {
511     return reportExceptionDir_;
512 }
513 }  // namespace WuKong
514 }  // namespace OHOS