1 /*
2  * Copyright (c) 2023 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 "rustpanic_listener.h"
17 
18 #include <cstring>
19 #include <iostream>
20 
21 namespace OHOS {
22 namespace HiviewDFX {
SetKeyWord(const std::string & keyWord)23 void RustPanicListener::SetKeyWord(const std::string& keyWord)
24 {
25     std::cout << "Enter SetKeyWord:" << keyWord << std::endl;
26     this->keyWord = keyWord;
27     {
28         std::lock_guard<std::mutex> lock(setFlagMutex);
29         allFindFlag = false;
30     }
31 }
32 
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)33 void RustPanicListener::OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)
34 {
35     if (sysEvent == nullptr) {
36         return;
37     }
38     std::string reason;
39     sysEvent->GetParamValue("REASON", reason);
40 
41     std::cout << "recv event, reason:" << reason << std::endl;
42     if (reason.find(keyWord) == std::string::npos) {
43         std::cout << "not find keyWord in reason"  << std::endl;
44         return;
45     }
46 
47     // find all keywords, set allFindFlag to true
48     std::cout << "OnEvent get keyWord"  << std::endl;
49     {
50         std::lock_guard<std::mutex> lock(setFlagMutex);
51         allFindFlag = true;
52         keyWordCheckCondition.notify_all();
53     }
54 }
55 
OnServiceDied()56 void RustPanicListener::OnServiceDied()
57 {
58     std::cout << std::string("service disconnected, exit.") << std::endl;
59 }
60 
CheckKeywordInReasons()61 bool RustPanicListener::CheckKeywordInReasons()
62 {
63     std::cout << "Enter CheckKeywordInReasons"  << std::endl;
64     std::unique_lock<std::mutex> lock(setFlagMutex);
65     if (allFindFlag) {
66         std::cout << "allFindFlag is true, match ok, return true"  << std::endl;
67         return true;
68     }
69 
70     auto flagCheckFunc = [&]() {
71         return allFindFlag;
72     };
73 
74     // 6: wait allFindFlag set true for 6 seconds
75     if (keyWordCheckCondition.wait_for(lock, std::chrono::seconds(6), flagCheckFunc)) {
76         std::cout << "match ok, return true"  << std::endl;
77         return true;
78     } else {
79         std::cout << "match keywords timeout"  << std::endl;
80         return false;
81     }
82 }
83 } // namespace HiviewDFX
84 } // namespace OHOS