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
17 #include "hisysevent_wrapper.h"
18 #include "hisysevent.h"
19
20 #include "hks_cfi.h"
21 #include "hks_log.h"
22 #include "hks_template.h"
23
24 using namespace OHOS::HiviewDFX;
25
26 static constexpr const char g_eventName[] = "FAULT";
27 static constexpr const char g_tagFunction[] = "FUNCTION";
28 static constexpr const char g_tagUserId[] = "USER_ID";
29 static constexpr const char g_tagProcessUID[] = "PROCESS_UID";
30 static constexpr const char g_tagKeyType[] = "KEY_TYPE";
31 static constexpr const char g_tagErrorCode[] = "ERROR_CODE";
32 static constexpr const char g_tagExtra[] = "EXTRA";
33 static constexpr const char g_stats[] = "PERFORMANCE";
34
ConvertToHiSysEventType(enum EventType inEventType,int32_t * outEventTypeInt)35 static int32_t ConvertToHiSysEventType(enum EventType inEventType,
36 int32_t *outEventTypeInt)
37 {
38 switch (inEventType) {
39 case FAULT:
40 *outEventTypeInt = HiSysEvent::EventType::FAULT;
41 break;
42 case STATISTIC:
43 *outEventTypeInt = HiSysEvent::EventType::STATISTIC;
44 break;
45 case SECURITY:
46 *outEventTypeInt = HiSysEvent::EventType::SECURITY;
47 break;
48 case BEHAVIOR:
49 *outEventTypeInt = HiSysEvent::EventType::BEHAVIOR;
50 break;
51 default:
52 HKS_LOG_E("Invalid inEventType!");
53 return HKS_ERROR_INVALID_ARGUMENT;
54 }
55 return HKS_SUCCESS;
56 }
57
WriteEvent(enum EventType eventType,const char * functionName,const struct EventValues * eventValues,const char * extra)58 int WriteEvent(enum EventType eventType, const char *functionName, const struct EventValues *eventValues,
59 const char *extra)
60 {
61 int32_t outEventTypeInt = 0;
62 int32_t ret = ConvertToHiSysEventType(eventType, &outEventTypeInt);
63 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "convert to hiSysEvent event type failed!")
64
65 enum HiSysEvent::EventType outEventType = static_cast<enum HiSysEvent::EventType>(outEventTypeInt);
66
67 ret = HiSysEventWrite(HiSysEvent::Domain::HUKS, g_eventName,
68 outEventType, g_tagFunction, functionName, g_tagUserId, eventValues->userId, g_tagProcessUID,
69 eventValues->processName, g_tagKeyType, eventValues->keyType, g_tagErrorCode, eventValues->errorCode,
70 g_tagExtra, extra);
71 return ret;
72 }
73
ENABLE_CFI(int WritePerformanceEvent (enum EventPerformanceID performanceId))74 ENABLE_CFI(int WritePerformanceEvent(enum EventPerformanceID performanceId))
75 {
76 int id = performanceId;
77 std::chrono::milliseconds ms = std::chrono::duration_cast< std::chrono::milliseconds >(
78 std::chrono::system_clock::now().time_since_epoch());
79
80 return HiSysEventWrite(g_stats, "CPU_SCENE_ENTRY",
81 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
82 "PACKAGE_NAME", "huks_service",
83 "SCENE_ID", std::to_string(id).c_str(),
84 "HAPPEN_TIME", ms.count());
85 }