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 <securec.h>
17 #include "comm_log.h"
18 #include "fillpinc.h"
19 #include "nstackx.h"
20 #include "nstackx_dfile.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24 #include "softbus_adapter_hisysevent.h"
25 #include "softbus_hisysevt_nstack.h"
26
27 #ifdef FILLP_ENHANCED
28 /* below define must keep the same with DStream DFinder DMsg DFile */
29 #define NSTACK_DFX_EVENT_NAME_LEN 33
30
31 typedef enum {
32 NSTACK_DFX_EVT_TYPE_FAULT,
33 NSTACK_DFX_EVT_TYPE_STATISTIC,
34 NSTACK_DFX_EVT_TYPE_SECURITY,
35 NSTACK_DFX_EVT_TYPE_BEHAVIOR,
36 NSTACK_DFX_EVT_TYPE_BUTT
37 } NstackDfxEvtType;
38
39 typedef enum {
40 NSTACK_DFX_EVT_LEVEL_CRITICAL,
41 NSTACK_DFX_EVT_LEVEL_MINOR,
42 } NstackDfxEvtLevel;
43
44 typedef enum {
45 NSTACK_DFX_PARAMTYPE_BOOL,
46 NSTACK_DFX_PARAMTYPE_UINT8,
47 NSTACK_DFX_PARAMTYPE_UINT16,
48 NSTACK_DFX_PARAMTYPE_INT32,
49 NSTACK_DFX_PARAMTYPE_UINT32,
50 NSTACK_DFX_PARAMTYPE_UINT64,
51 NSTACK_DFX_PARAMTYPE_FLOAT,
52 NSTACK_DFX_PARAMTYPE_DOUBLE,
53 NSTACK_DFX_PARAMTYPE_STRING,
54 } NstackDfxEvtParamType;
55
56 typedef struct {
57 NstackDfxEvtParamType type;
58 char paramName[NSTACK_DFX_EVENT_NAME_LEN];
59 union {
60 uint8_t u8v;
61 uint16_t u16v;
62 int32_t i32v;
63 uint32_t u32v;
64 uint64_t u64v;
65 float f;
66 double d;
67 char str[NSTACK_DFX_EVENT_NAME_LEN];
68 } val;
69 } NstackDfxEvtParam;
70
71 typedef struct {
72 char eventName[NSTACK_DFX_EVENT_NAME_LEN];
73 NstackDfxEvtType type;
74 NstackDfxEvtLevel level;
75 uint32_t paramNum;
76 NstackDfxEvtParam *paramArray;
77 } NstackDfxEvent;
78 /* up define must keep the same with DStream DFinder DMsg DFile */
79
CopyEventParamVal(SoftBusEvtParamType type,void * dst,const void * src)80 static int32_t CopyEventParamVal(SoftBusEvtParamType type, void *dst, const void *src)
81 {
82 switch (type) {
83 case SOFTBUS_EVT_PARAMTYPE_BOOL:
84 case SOFTBUS_EVT_PARAMTYPE_UINT8:
85 *(uint8_t *)dst = *(uint8_t *)src;
86 break;
87 case SOFTBUS_EVT_PARAMTYPE_UINT16:
88 *(uint16_t *)dst = *(uint16_t *)src;
89 break;
90 case SOFTBUS_EVT_PARAMTYPE_INT32:
91 case SOFTBUS_EVT_PARAMTYPE_UINT32:
92 case SOFTBUS_EVT_PARAMTYPE_FLOAT:
93 *(uint32_t *)dst = *(uint32_t *)src;
94 break;
95 case SOFTBUS_EVT_PARAMTYPE_UINT64:
96 case SOFTBUS_EVT_PARAMTYPE_DOUBLE:
97 *(uint64_t *)dst = *(uint64_t *)src;
98 break;
99 case SOFTBUS_EVT_PARAMTYPE_STRING:
100 if (strcpy_s(dst, SOFTBUS_HISYSEVT_PARAM_LEN, src) != EOK) {
101 COMM_LOGE(COMM_DFX, "softbus param string max=%{public}d, nstackParamString=%{public}s",
102 SOFTBUS_HISYSEVT_PARAM_LEN, (char *)src);
103 return SOFTBUS_STRCPY_ERR;
104 }
105 break;
106 default:
107 COMM_LOGE(COMM_DFX, "unknow param type");
108 return SOFTBUS_INVALID_PARAM;
109 }
110 return SOFTBUS_OK;
111 }
112
NstackEventParaToSoftBusEventPara(SoftBusEvtParam * dst,const NstackDfxEvtParam * src)113 static int32_t NstackEventParaToSoftBusEventPara(SoftBusEvtParam *dst, const NstackDfxEvtParam *src)
114 {
115 if (src->type >= SOFTBUS_EVT_PARAMTYPE_BUTT) {
116 COMM_LOGE(COMM_DFX, "softbus paramType max=%{public}d, nstackParamType=%{public}d",
117 SOFTBUS_EVT_PARAMTYPE_BUTT, src->type);
118 return SOFTBUS_INVALID_PARAM;
119 }
120 dst->paramType = (SoftBusEvtParamType)src->type;
121 if (strcpy_s(dst->paramName, SOFTBUS_HISYSEVT_NAME_LEN, src->paramName) != EOK) {
122 COMM_LOGE(COMM_DFX, "softbus paramName maxSize=%{public}d, nstackParamName=%{public}s",
123 SOFTBUS_HISYSEVT_NAME_LEN, src->paramName);
124 return SOFTBUS_STRCPY_ERR;
125 }
126 int32_t ret = CopyEventParamVal(dst->paramType, &dst->paramValue, &src->val);
127 if (ret != SOFTBUS_OK) {
128 return ret;
129 }
130 return SOFTBUS_OK;
131 }
132
NstackDfxEvtToSoftBusReportMsg(SoftBusEvtReportMsg * msg,const NstackDfxEvent * info)133 static int32_t NstackDfxEvtToSoftBusReportMsg(SoftBusEvtReportMsg *msg, const NstackDfxEvent *info)
134 {
135 if (strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, info->eventName) != EOK) {
136 COMM_LOGE(COMM_DFX, "eventName mismatch, nstackEventName=%{public}s", info->eventName);
137 return SOFTBUS_STRCPY_ERR;
138 }
139 if (info->type >= SOFTBUS_EVT_TYPE_BUTT) {
140 COMM_LOGE(COMM_DFX, "eventType mismatch, nstackEventType=%{public}d", info->type);
141 return SOFTBUS_INVALID_PARAM;
142 }
143 msg->evtType = (SoftBusEvtType)(info->type);
144 if (info->paramNum != 0 && info->paramArray == NULL) {
145 COMM_LOGE(COMM_DFX, "param mismatch, paramArray=NULL, nstackParamNum=%{public}u", info->paramNum);
146 return SOFTBUS_INVALID_PARAM;
147 }
148 msg->paramNum = info->paramNum;
149 if (msg->paramNum == 0) {
150 return SOFTBUS_OK;
151 }
152 msg->paramArray = (SoftBusEvtParam *)SoftBusCalloc(msg->paramNum * sizeof(SoftBusEvtParam));
153 if (msg->paramArray == NULL) {
154 COMM_LOGE(COMM_DFX, "SoftBusCalloc paramArray failed! paramNum=%{public}u", info->paramNum);
155 return SOFTBUS_MALLOC_ERR;
156 }
157 int32_t ret = SOFTBUS_OK;
158 for (uint8_t i = 0; i < info->paramNum; i++) {
159 ret = NstackEventParaToSoftBusEventPara(&msg->paramArray[i], &info->paramArray[i]);
160 if (ret != SOFTBUS_OK) {
161 return ret;
162 }
163 }
164 return SOFTBUS_OK;
165 }
166
NstackHiEventCb(void * softObj,const NstackDfxEvent * info)167 static void NstackHiEventCb(void *softObj, const NstackDfxEvent *info)
168 {
169 (void)softObj;
170 if (info == NULL) {
171 COMM_LOGE(COMM_DFX, "info is NULL");
172 return;
173 }
174 SoftBusEvtReportMsg msg;
175 (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg));
176 if (NstackDfxEvtToSoftBusReportMsg(&msg, info) != SOFTBUS_OK) {
177 if (msg.paramArray != NULL) {
178 SoftBusFree(msg.paramArray);
179 }
180 COMM_LOGE(COMM_DFX, "change NstackDfxEvent to SoftBusEvtReportMsg failed!");
181 return;
182 }
183 (void)SoftbusWriteHisEvt(&msg);
184 if (msg.paramArray != NULL) {
185 SoftBusFree(msg.paramArray);
186 }
187 }
188
DstreamHiEventCb(void * softObj,const FillpDfxEvent * info)189 void DstreamHiEventCb(void *softObj, const FillpDfxEvent *info)
190 {
191 if (softObj == NULL || info == NULL) {
192 COMM_LOGE(COMM_DFX, "param is NULL");
193 return;
194 }
195 NstackDfxEvent nstackInfo;
196 if (memcpy_s(&nstackInfo, sizeof(NstackDfxEvent), info, sizeof(FillpDfxEvent)) != EOK) {
197 COMM_LOGE(COMM_DFX, "change FillpDfxEvent to NstackDfxEvent failed!");
198 return;
199 }
200 NstackHiEventCb(softObj, &nstackInfo);
201 }
202
DFileHiEventCb(void * softObj,const DFileEvent * info)203 static void DFileHiEventCb(void *softObj, const DFileEvent *info)
204 {
205 NstackDfxEvent nstackInfo;
206 if (memcpy_s(&nstackInfo, sizeof(NstackDfxEvent), info, sizeof(DFileEvent)) != EOK) {
207 COMM_LOGE(COMM_DFX, "change DFileEvent to NstackDfxEvent failed!");
208 return;
209 }
210 NstackHiEventCb(softObj, &nstackInfo);
211 }
212
DFinderHiEventCb(void * softObj,const DFinderEvent * info)213 static void DFinderHiEventCb(void *softObj, const DFinderEvent *info)
214 {
215 NstackDfxEvent nstackInfo;
216 if (memcpy_s(nstackInfo.eventName, sizeof(nstackInfo.eventName),
217 info->eventName, sizeof(info->eventName)) != EOK) {
218 COMM_LOGE(COMM_DFX, "change DFinderEvent to NstackDfxEvent failed!");
219 return;
220 }
221
222 nstackInfo.type = (NstackDfxEvtType)info->type;
223 nstackInfo.level = (NstackDfxEvtLevel)info->level;
224 nstackInfo.paramNum = info->paramNum;
225 nstackInfo.paramArray = (NstackDfxEvtParam *)info->params;
226 NstackHiEventCb(softObj, &nstackInfo);
227 }
228
NstackInitHiEvent(void)229 void NstackInitHiEvent(void)
230 {
231 NSTACKX_DFileSetEventFunc(NULL, DFileHiEventCb);
232 if (NSTACKX_DFinderSetEventFunc(NULL, DFinderHiEventCb) != 0) {
233 COMM_LOGE(COMM_DFX, "NSTACKX_DFinderSetEventFunc failed!");
234 }
235 }
236 #endif /* FILLP_ENHANCED */
237