• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "crash_exception_listener.h"
17 
18 #include <cstring>
19 #include <iostream>
20 
21 namespace OHOS {
22 namespace HiviewDFX {
SetKeyWord(const std::string & keyWord)23 void CrashExceptionListener::SetKeyWord(const std::string& keyWord)
24 {
25     this->keyWord = keyWord;
26     std::lock_guard<std::mutex> lock(setFlagMutex);
27     allFindFlag = false;
28 }
29 
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)30 void CrashExceptionListener::OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)
31 {
32     if (sysEvent == nullptr) {
33         return;
34     }
35     std::string reason;
36     sysEvent->GetParamValue("PROCESS_NAME", reason);
37 
38     if (reason.find(keyWord) == std::string::npos) {
39         return;
40     }
41 
42     // find all keywords, set allFindFlag to true
43     std::lock_guard<std::mutex> lock(setFlagMutex);
44     allFindFlag = true;
45     keyWordCheckCondition.notify_all();
46 }
47 
OnServiceDied()48 void CrashExceptionListener::OnServiceDied()
49 {
50 }
51 
CheckKeywordInReasons()52 bool CrashExceptionListener::CheckKeywordInReasons()
53 {
54     std::unique_lock<std::mutex> lock(setFlagMutex);
55     if (allFindFlag) {
56         return true;
57     }
58 
59     auto flagCheckFunc = [this]() {
60         return this->allFindFlag;
61     };
62 
63     // wait allFindFlag set true for 6 seconds
64     if (keyWordCheckCondition.wait_for(lock, std::chrono::seconds(6), flagCheckFunc)) {
65         return true;
66     } else {
67         return false;
68     }
69 }
70 } // namespace HiviewDFX
71 } // namespace OHOS