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 #ifndef HIVIEWDFX_HITRACECHAIN_C_H
17 #define HIVIEWDFX_HITRACECHAIN_C_H
18 
19 #include <endian.h>
20 #include <stdarg.h>
21 #include <stdint.h>
22 
23 #include "securec.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 typedef enum HiTraceIdValid {
30     HITRACE_ID_INVALID = 0,
31     HITRACE_ID_VALID = 1,
32 } HiTraceIdValid;
33 
34 typedef enum HiTraceVersion {
35     HITRACE_VER_1 = 0,
36 } HiTraceVersion;
37 
38 typedef enum HiTraceFlag {
39     // MIN: valid.
40     HITRACE_FLAG_MIN = 0,
41     // DEFAULT: default value.
42     HITRACE_FLAG_DEFAULT = 0,
43     // trace sync and async call. default: trace sync call only.
44     HITRACE_FLAG_INCLUDE_ASYNC = 1 << 0,
45     // do not create child span. default: create child span.
46     HITRACE_FLAG_DONOT_CREATE_SPAN = 1 << 1,
47     // output tracepoint info in span. default: do not output tracepoint info.
48     HITRACE_FLAG_TP_INFO = 1 << 2,
49     // do not output begin and end info. default: output begin and end info.
50     HITRACE_FLAG_NO_BE_INFO = 1 << 3,
51     // do not add id to log. default: add id to log.
52     HITRACE_FLAG_DONOT_ENABLE_LOG = 1 << 4,
53     // the trace is triggered by fault.
54     HITRACE_FLAG_FAULT_TRIGGER = 1 << 5,
55     // output device-to-device tracepoint info in span only. default: do not output device-to-device tracepoint info.
56     HITRACE_FLAG_D2D_TP_INFO = 1 << 6,
57     // MAX: valid.
58     HITRACE_FLAG_MAX = (1 << 7) - 1,
59 } HiTraceFlag;
60 
61 // HiTrace tracepoint type
62 typedef enum HiTraceTracepointType {
63     HITRACE_TP_MIN = 0,    // MIN: valid
64     HITRACE_TP_CS = 0,      // client send
65     HITRACE_TP_CR = 1,      // client receive
66     HITRACE_TP_SS = 2,      // server send
67     HITRACE_TP_SR = 3,      // server receive
68     HITRACE_TP_GENERAL = 4, // general info
69     HITRACE_TP_MAX = 4,     // MAX: valid
70 } HiTraceTracepointType;
71 
72 // HiTrace communication mode
73 typedef enum HiTraceCommunicationMode {
74     HITRACE_CM_MIN = 0,      // MIN: valid
75     HITRACE_CM_DEFAULT = 0, // unspecified communication mode
76     HITRACE_CM_THREAD = 1,  // thread-to-thread communication mode
77     HITRACE_CM_PROCESS = 2, // process-to-process communication mode
78     HITRACE_CM_DEVICE = 3,  // device-to-device communication mode
79     HITRACE_CM_MAX = 3,     // MAX: valid
80 } HiTraceCommunicationMode;
81 
82 typedef struct HiTraceIdStruct {
83 #if __BYTE_ORDER == __LITTLE_ENDIAN
84     uint64_t valid : 1;
85     uint64_t ver : 3;
86     uint64_t chainId : 60;
87 
88     uint64_t flags : 12;
89     uint64_t spanId : 26;
90     uint64_t parentSpanId : 26;
91 #elif __BYTE_ORDER == __BIG_ENDIAN
92     uint64_t chainId : 60;
93     uint64_t ver : 3;
94     uint64_t valid : 1;
95 
96     uint64_t parentSpanId : 26;
97     uint64_t spanId : 26;
98     uint64_t flags : 12;
99 #else
100 #error "ERROR: No BIG_LITTLE_ENDIAN defines."
101 #endif
102 } HiTraceIdStruct;
103 
104 #define HITRACE_ID_LEN sizeof(HiTraceIdStruct)
105 
106 HiTraceIdStruct HiTraceChainBegin(const char* name, int flags);
107 void HiTraceChainEnd(const HiTraceIdStruct* pId);
108 HiTraceIdStruct HiTraceChainGetId(void);
109 void HiTraceChainSetId(const HiTraceIdStruct* pId);
110 void HiTraceChainClearId(void);
111 HiTraceIdStruct HiTraceChainCreateSpan(void);
112 HiTraceIdStruct HiTraceChainSaveAndSetId(const HiTraceIdStruct* pId);
113 void HiTraceChainRestoreId(const HiTraceIdStruct* oldId);
114 void HiTraceChainTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...)
115     __attribute__((__format__(os_log, 3, 4)));
116 void HiTraceChainTracepointWithArgs(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt,
117     va_list args);
118 void HiTraceChainTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
119     const char* fmt, ...) __attribute__((__format__(os_log, 4, 5)));
120 void HiTraceChainTracepointExWithArgs(HiTraceCommunicationMode mode, HiTraceTracepointType type,
121     const HiTraceIdStruct* pId, const char* fmt, va_list args);
122 
HiTraceChainInitId(HiTraceIdStruct * pId)123 static inline void HiTraceChainInitId(HiTraceIdStruct* pId)
124 {
125     pId->valid = HITRACE_ID_INVALID;
126     pId->ver = 0;
127     pId->chainId = 0;
128     pId->flags = 0;
129     pId->spanId = 0;
130     pId->parentSpanId = 0;
131 }
132 
HiTraceChainIsValid(const HiTraceIdStruct * pId)133 static inline int HiTraceChainIsValid(const HiTraceIdStruct* pId)
134 {
135     return (pId) && (pId->valid == HITRACE_ID_VALID);
136 }
137 
HiTraceChainIsFlagEnabled(const HiTraceIdStruct * pId,HiTraceFlag flag)138 static inline int HiTraceChainIsFlagEnabled(const HiTraceIdStruct* pId, HiTraceFlag flag)
139 {
140     return HiTraceChainIsValid(pId) && ((pId->flags & (uint64_t)flag) != 0);
141 }
142 
HiTraceChainEnableFlag(HiTraceIdStruct * pId,HiTraceFlag flag)143 static inline void HiTraceChainEnableFlag(HiTraceIdStruct* pId, HiTraceFlag flag)
144 {
145     if (HiTraceChainIsValid(pId)) {
146         pId->flags |= (uint64_t)flag;
147     }
148     return;
149 }
150 
HiTraceChainGetFlags(const HiTraceIdStruct * pId)151 static inline int HiTraceChainGetFlags(const HiTraceIdStruct* pId)
152 {
153     if (!HiTraceChainIsValid(pId)) {
154         return 0;
155     }
156     return pId->flags;
157 }
158 
HiTraceChainSetFlags(HiTraceIdStruct * pId,int flags)159 static inline void HiTraceChainSetFlags(HiTraceIdStruct* pId, int flags)
160 {
161     if (HiTraceChainIsValid(pId) && (flags >= HITRACE_FLAG_MIN) && (flags < HITRACE_FLAG_MAX)) {
162         pId->flags = (uint64_t)flags;
163     }
164     return;
165 }
166 
HiTraceChainGetChainId(const HiTraceIdStruct * pId)167 static inline uint64_t HiTraceChainGetChainId(const HiTraceIdStruct* pId)
168 {
169     if (!HiTraceChainIsValid(pId)) {
170         return 0;
171     }
172     return pId->chainId;
173 }
174 
HiTraceChainSetChainId(HiTraceIdStruct * pId,uint64_t chainId)175 static inline void HiTraceChainSetChainId(HiTraceIdStruct* pId, uint64_t chainId)
176 {
177     if (!pId || chainId == 0) {
178         return;
179     }
180 
181     if (!HiTraceChainIsValid(pId)) {
182         pId->valid = HITRACE_ID_VALID;
183         pId->ver = HITRACE_VER_1;
184         pId->flags = pId->spanId = pId->parentSpanId = 0;
185     }
186     pId->chainId = chainId;
187 }
188 
HiTraceChainGetSpanId(const HiTraceIdStruct * pId)189 static inline uint64_t HiTraceChainGetSpanId(const HiTraceIdStruct* pId)
190 {
191     if (!HiTraceChainIsValid(pId)) {
192         return 0;
193     }
194     return pId->spanId;
195 }
196 
HiTraceChainSetSpanId(HiTraceIdStruct * pId,uint64_t spanId)197 static inline void HiTraceChainSetSpanId(HiTraceIdStruct* pId, uint64_t spanId)
198 {
199     if (HiTraceChainIsValid(pId)) {
200         pId->spanId = spanId;
201     }
202     return;
203 }
204 
HiTraceChainGetParentSpanId(const HiTraceIdStruct * pId)205 static inline uint64_t HiTraceChainGetParentSpanId(const HiTraceIdStruct* pId)
206 {
207     if (!HiTraceChainIsValid(pId)) {
208         return 0;
209     }
210     return pId->parentSpanId;
211 }
212 
HiTraceChainSetParentSpanId(HiTraceIdStruct * pId,uint64_t parentSpanId)213 static inline void HiTraceChainSetParentSpanId(HiTraceIdStruct* pId, uint64_t parentSpanId)
214 {
215     if (HiTraceChainIsValid(pId)) {
216         pId->parentSpanId = parentSpanId;
217     }
218     return;
219 }
220 
HiTraceChainIdToBytes(const HiTraceIdStruct * pId,uint8_t * pIdArray,int len)221 static inline int HiTraceChainIdToBytes(const HiTraceIdStruct* pId, uint8_t* pIdArray, int len)
222 {
223     if (!HiTraceChainIsValid(pId) || (len < (int)HITRACE_ID_LEN)) {
224         return 0;
225     }
226 
227     *((uint64_t*)pIdArray) = htobe64(*((uint64_t*)pId));
228     *((uint64_t*)pIdArray + 1) = htobe64(*((uint64_t*)pId + 1));
229     return sizeof(HiTraceIdStruct);
230 }
231 
HiTraceChainBytesToId(const uint8_t * pIdArray,int len)232 static inline HiTraceIdStruct HiTraceChainBytesToId(const uint8_t* pIdArray, int len)
233 {
234     HiTraceIdStruct id = {0, 0, 0, 0, 0, 0};
235     HiTraceChainInitId(&id);
236 
237     if ((!pIdArray) || (len != (int)HITRACE_ID_LEN)) {
238         return id;
239     }
240 
241     uint64_t tmp1 = 0;
242     uint64_t tmp2 = 0;
243     if (memcpy_s(&tmp1, sizeof(uint64_t), pIdArray, sizeof(uint64_t)) != EOK ||
244         memcpy_s(&tmp2, sizeof(uint64_t), pIdArray + sizeof(uint64_t), sizeof(uint64_t)) != EOK) {
245         return id;
246     }
247     *((uint64_t*)(&id)) = be64toh(tmp1);
248     *((uint64_t*)(&id) + 1) = be64toh(tmp2);
249     return id;
250 }
251 
252 #ifdef __cplusplus
253 }
254 #endif
255 
256 #endif // HIVIEWDFX_HITRACECHAIN_C_H
257