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 #include "hisysevent_adapter.h"
16 
17 #include <string>
18 
19 #include "def.h"
20 
21 // hisysevent report 500 times per 5s
22 #define HISYSEVENT_PERIOD 5
23 #define HISYSEVENT_THRESHOLD 500
24 #include "hisysevent.h"
25 #include "sam_log.h"
26 
27 namespace OHOS {
28 using namespace OHOS::HiviewDFX;
29 namespace {
30 constexpr const char* ADD_SYSTEMABILITY_FAIL = "ADD_SYSTEMABILITY_FAIL";
31 constexpr const char* CALLER_UID = "CALLER_UID";
32 constexpr const char* SAID = "SAID";
33 constexpr const char* COUNT = "COUNT";
34 constexpr const char* FILE_NAME = "FILE_NAME";
35 constexpr const char* GETSA__TAG = "GETSA_FREQUENCY";
36 
37 constexpr const char* REASON = "REASON";
38 constexpr const char* ONDEMAND_SA_LOAD_FAIL = "ONDEMAND_SA_LOAD_FAIL";
39 constexpr const char* ONDEMAND_SA_LOAD = "ONDEMAND_SA_LOAD";
40 constexpr const char* EVENT = "EVENT";
41 constexpr const char* SA_CRASH = "SA_CRASH";
42 constexpr const char* ONDEMAND_SA_UNLOAD = "ONDEMAND_SA_UNLOAD";
43 constexpr const char* SA_UNLOAD_FAIL = "SA_UNLOAD_FAIL";
44 constexpr const char* SA_LOAD_DURATION = "SA_LOAD_DURATION";
45 constexpr const char* SA_UNLOAD_DURATION = "SA_UNLOAD_DURATION";
46 constexpr const char* SA_MAIN_EXIT = "SA_MAIN_EXIT";
47 constexpr const char* PROCESS_START_FAIL = "PROCESS_START_FAIL";
48 constexpr const char* PROCESS_STOP_FAIL = "PROCESS_STOP_FAIL";
49 constexpr const char* PROCESS_START_DURATION = "PROCESS_START_DURATION";
50 constexpr const char* PROCESS_STOP_DURATION = "PROCESS_STOP_DURATION";
51 constexpr const char* PROCESS_NAME = "PROCESS_NAME";
52 constexpr const char* PID = "PID";
53 constexpr const char* UID = "UID";
54 constexpr const char* DURATION = "DURATION";
55 constexpr const char* KEY_STAGE = "KEY_STAGE";
56 }
57 
ReportSaCrash(int32_t saId)58 void ReportSaCrash(int32_t saId)
59 {
60     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
61         SA_CRASH,
62         HiSysEvent::EventType::FAULT,
63         SAID, saId);
64     if (ret != 0) {
65         HILOGE("report sa crash failed! SA:%{public}d, ret:%{public}d.", saId, ret);
66     }
67 }
68 
ReportSaUnLoadFail(int32_t saId,int32_t pid,int32_t uid,const std::string & reason)69 void ReportSaUnLoadFail(int32_t saId, int32_t pid, int32_t uid, const std::string& reason)
70 {
71     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
72         SA_UNLOAD_FAIL,
73         HiSysEvent::EventType::FAULT,
74         SAID, saId,
75         PID, pid,
76         UID, uid,
77         REASON, reason);
78     if (ret != 0) {
79         HILOGE("report sa unload fail event failed! SA:%{public}d, ret %{public}d.", saId, ret);
80     }
81 }
82 
ReportSaDuration(const std::string & eventName,int32_t saId,int32_t keyStage,int64_t duration)83 static void ReportSaDuration(const std::string& eventName, int32_t saId, int32_t keyStage, int64_t duration)
84 {
85     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
86         eventName,
87         HiSysEvent::EventType::BEHAVIOR,
88         SAID, saId,
89         KEY_STAGE, keyStage,
90         DURATION, duration);
91     if (ret != 0) {
92         HILOGE("report event:%{public}s failed! SA:%{public}d, ret:%{public}d.",
93             eventName.c_str(), saId, ret);
94     }
95 }
96 
ReportSaMainExit(const std::string & reason)97 void ReportSaMainExit(const std::string& reason)
98 {
99     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
100         SA_MAIN_EXIT,
101         HiSysEvent::EventType::FAULT,
102         REASON, reason);
103     if (ret != 0) {
104         HILOGE("report sa main exit event failed! ret:%{public}d.", ret);
105     }
106 }
107 
ReportSaLoadDuration(int32_t saId,int32_t keyStage,int64_t duration)108 void ReportSaLoadDuration(int32_t saId, int32_t keyStage, int64_t duration)
109 {
110     ReportSaDuration(SA_LOAD_DURATION, saId, keyStage, duration);
111 }
112 
ReportSaUnLoadDuration(int32_t saId,int32_t keyStage,int64_t duration)113 void ReportSaUnLoadDuration(int32_t saId, int32_t keyStage, int64_t duration)
114 {
115     ReportSaDuration(SA_UNLOAD_DURATION, saId, keyStage, duration);
116 }
117 
ReportProcessDuration(const std::string & eventName,const std::string & processName,int32_t pid,int32_t uid,int64_t duration)118 static void ReportProcessDuration(const std::string& eventName, const std::string& processName,
119     int32_t pid, int32_t uid, int64_t duration)
120 {
121     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
122         eventName,
123         HiSysEvent::EventType::BEHAVIOR,
124         PROCESS_NAME, processName,
125         PID, pid,
126         UID, uid,
127         DURATION, duration);
128     if (ret != 0) {
129         HILOGE("report event:%{public}s failed! process:%{public}s, ret:%{public}d.",
130             eventName.c_str(), processName.c_str(), ret);
131     }
132 }
133 
ReportProcessStartDuration(const std::string & processName,int32_t pid,int32_t uid,int64_t duration)134 void ReportProcessStartDuration(const std::string& processName, int32_t pid, int32_t uid, int64_t duration)
135 {
136     ReportProcessDuration(PROCESS_START_DURATION, processName, pid, uid, duration);
137 }
138 
ReportProcessStopDuration(const std::string & processName,int32_t pid,int32_t uid,int64_t duration)139 void ReportProcessStopDuration(const std::string& processName, int32_t pid, int32_t uid, int64_t duration)
140 {
141     ReportProcessDuration(PROCESS_STOP_DURATION, processName, pid, uid, duration);
142 }
143 
ReportProcessFail(const std::string & eventName,const std::string & processName,int32_t pid,int32_t uid,const std::string & reason)144 static void ReportProcessFail(const std::string& eventName, const std::string& processName,
145     int32_t pid, int32_t uid, const std::string& reason)
146 {
147     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
148         eventName,
149         HiSysEvent::EventType::FAULT,
150         PROCESS_NAME, processName,
151         PID, pid,
152         UID, uid,
153         REASON, reason);
154     if (ret != 0) {
155         HILOGE("report event:%{public}s failed! process:%{public}s, ret:%{public}d.",
156             eventName.c_str(), processName.c_str(), ret);
157     }
158 }
159 
ReportProcessStartFail(const std::string & processName,int32_t pid,int32_t uid,const std::string & reason)160 void ReportProcessStartFail(const std::string& processName, int32_t pid, int32_t uid, const std::string& reason)
161 {
162     ReportProcessFail(PROCESS_START_FAIL, processName, pid, uid, reason);
163 }
164 
ReportProcessStopFail(const std::string & processName,int32_t pid,int32_t uid,const std::string & reason)165 void ReportProcessStopFail(const std::string& processName, int32_t pid, int32_t uid, const std::string& reason)
166 {
167     ReportProcessFail(PROCESS_STOP_FAIL, processName, pid, uid, reason);
168 }
169 
ReportSamgrSaLoadFail(int32_t said,int32_t pid,int32_t uid,const std::string & reason)170 void ReportSamgrSaLoadFail(int32_t said, int32_t pid, int32_t uid, const std::string& reason)
171 {
172     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
173         ONDEMAND_SA_LOAD_FAIL,
174         HiSysEvent::EventType::FAULT,
175         SAID, said,
176         PID, pid,
177         UID, uid,
178         REASON, reason);
179     if (ret != 0) {
180         HILOGE("hisysevent report samgr sa load fail event failed! ret %{public}d.", ret);
181     }
182 }
183 
ReportSamgrSaLoad(int32_t said,int32_t pid,int32_t uid,int32_t eventId)184 void ReportSamgrSaLoad(int32_t said, int32_t pid, int32_t uid, int32_t eventId)
185 {
186     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
187         ONDEMAND_SA_LOAD,
188         HiSysEvent::EventType::BEHAVIOR,
189         SAID, said,
190         PID, pid,
191         UID, uid,
192         EVENT, eventId);
193     if (ret != 0) {
194         HILOGE("hisysevent report samgr sa load event failed! ret %{public}d.", ret);
195     }
196 }
197 
ReportSamgrSaUnload(int32_t said,int32_t pid,int32_t uid,int32_t eventId)198 void ReportSamgrSaUnload(int32_t said, int32_t pid, int32_t uid, int32_t eventId)
199 {
200     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
201         ONDEMAND_SA_UNLOAD,
202         HiSysEvent::EventType::BEHAVIOR,
203         SAID, said,
204         PID, pid,
205         UID, uid,
206         EVENT, eventId);
207     if (ret != 0) {
208         HILOGE("hisysevent report samgr sa unload event failed! ret %{public}d.", ret);
209     }
210 }
211 
ReportAddSystemAbilityFailed(int32_t said,int32_t pid,int32_t uid,const std::string & filaName)212 void ReportAddSystemAbilityFailed(int32_t said, int32_t pid, int32_t uid, const std::string& filaName)
213 {
214     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
215         ADD_SYSTEMABILITY_FAIL,
216         HiSysEvent::EventType::FAULT,
217         SAID, said,
218         PID, pid,
219         UID, uid,
220         FILE_NAME, filaName);
221     if (ret != 0) {
222         HILOGE("hisysevent report add system ability event failed! ret %{public}d.", ret);
223     }
224 }
225 
ReportGetSAFrequency(uint32_t callerUid,uint32_t said,int32_t count)226 void ReportGetSAFrequency(uint32_t callerUid, uint32_t said, int32_t count)
227 {
228     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
229         GETSA__TAG,
230         HiSysEvent::EventType::STATISTIC,
231         CALLER_UID, callerUid,
232         SAID, said,
233         COUNT, count);
234     if (ret != 0) {
235         HILOGD("hisysevent report get sa frequency failed! ret %{public}d.", ret);
236     }
237 }
238 
WatchDogSendEvent(int32_t pid,uint32_t uid,const std::string & sendMsg,const std::string & eventName)239 void WatchDogSendEvent(int32_t pid, uint32_t uid, const std::string& sendMsg,
240     const std::string& eventName)
241 {
242     int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
243         eventName,
244         HiSysEvent::EventType::FAULT,
245         "PID", pid,
246         "UID", uid,
247         "MSG", sendMsg);
248     if (ret != 0) {
249         HILOGE("hisysevent report watchdog failed! ret %{public}d.", ret);
250     }
251 }
252 } // OHOS
253