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