• 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 #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 HiTraceIdStruct HiTraceChainBeginWithDomain(const char* name, int flags, unsigned int domain);
108 void HiTraceChainEnd(const HiTraceIdStruct* pId);
109 void HiTraceChainEndWithDomain(const HiTraceIdStruct* pId, unsigned int domain);
110 HiTraceIdStruct HiTraceChainGetId(void);
111 HiTraceIdStruct* HiTraceChainGetIdAddress(void);
112 void HiTraceChainSetId(const HiTraceIdStruct* pId);
113 void HiTraceChainClearId(void);
114 HiTraceIdStruct HiTraceChainCreateSpan(void);
115 HiTraceIdStruct HiTraceChainSaveAndSetId(const HiTraceIdStruct* pId);
116 void HiTraceChainRestoreId(const HiTraceIdStruct* oldId);
117 void HiTraceChainTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...)
118     __attribute__((__format__(os_log, 3, 4)));
119 void HiTraceChainTracepointWithArgs(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt,
120     va_list args);
121 void HiTraceChainTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
122     const char* fmt, ...) __attribute__((__format__(os_log, 4, 5)));
123 void HiTraceChainTracepointExWithDomain(HiTraceCommunicationMode mode, HiTraceTracepointType type,
124     const HiTraceIdStruct* pId, unsigned int domain, const char* fmt, ...) __attribute__((__format__(os_log, 5, 6)));
125 void HiTraceChainTracepointExWithArgs(HiTraceCommunicationMode mode, HiTraceTracepointType type,
126     const HiTraceIdStruct* pId, const char* fmt, va_list args);
127 void HiTraceChainTracepointExWithArgsDomain(HiTraceCommunicationMode mode, HiTraceTracepointType type,
128     const HiTraceIdStruct* pId, unsigned int domain, const char* fmt, va_list args);
129 
HiTraceChainInitId(HiTraceIdStruct * pId)130 static inline void HiTraceChainInitId(HiTraceIdStruct* pId)
131 {
132     pId->valid = HITRACE_ID_INVALID;
133     pId->ver = 0;
134     pId->chainId = 0;
135     pId->flags = 0;
136     pId->spanId = 0;
137     pId->parentSpanId = 0;
138 }
139 
HiTraceChainIsValid(const HiTraceIdStruct * pId)140 static inline int HiTraceChainIsValid(const HiTraceIdStruct* pId)
141 {
142     return (pId) && (pId->valid == HITRACE_ID_VALID);
143 }
144 
HiTraceChainIsFlagEnabled(const HiTraceIdStruct * pId,HiTraceFlag flag)145 static inline int HiTraceChainIsFlagEnabled(const HiTraceIdStruct* pId, HiTraceFlag flag)
146 {
147     return HiTraceChainIsValid(pId) && ((pId->flags & (uint64_t)flag) != 0);
148 }
149 
HiTraceChainEnableFlag(HiTraceIdStruct * pId,HiTraceFlag flag)150 static inline void HiTraceChainEnableFlag(HiTraceIdStruct* pId, HiTraceFlag flag)
151 {
152     if (HiTraceChainIsValid(pId)) {
153         pId->flags |= (uint64_t)flag;
154     }
155     return;
156 }
157 
HiTraceChainGetFlags(const HiTraceIdStruct * pId)158 static inline int HiTraceChainGetFlags(const HiTraceIdStruct* pId)
159 {
160     if (!HiTraceChainIsValid(pId)) {
161         return 0;
162     }
163     return pId->flags;
164 }
165 
HiTraceChainSetFlags(HiTraceIdStruct * pId,int flags)166 static inline void HiTraceChainSetFlags(HiTraceIdStruct* pId, int flags)
167 {
168     if (HiTraceChainIsValid(pId) && (flags >= HITRACE_FLAG_MIN) && (flags < HITRACE_FLAG_MAX)) {
169         pId->flags = (uint64_t)flags;
170     }
171     return;
172 }
173 
HiTraceChainGetChainId(const HiTraceIdStruct * pId)174 static inline uint64_t HiTraceChainGetChainId(const HiTraceIdStruct* pId)
175 {
176     if (!HiTraceChainIsValid(pId)) {
177         return 0;
178     }
179     return pId->chainId;
180 }
181 
HiTraceChainSetChainId(HiTraceIdStruct * pId,uint64_t chainId)182 static inline void HiTraceChainSetChainId(HiTraceIdStruct* pId, uint64_t chainId)
183 {
184     if (!pId || chainId == 0) {
185         return;
186     }
187 
188     if (!HiTraceChainIsValid(pId)) {
189         pId->valid = HITRACE_ID_VALID;
190         pId->ver = HITRACE_VER_1;
191         pId->flags = pId->spanId = pId->parentSpanId = 0;
192     }
193     pId->chainId = chainId;
194 }
195 
HiTraceChainGetSpanId(const HiTraceIdStruct * pId)196 static inline uint64_t HiTraceChainGetSpanId(const HiTraceIdStruct* pId)
197 {
198     if (!HiTraceChainIsValid(pId)) {
199         return 0;
200     }
201     return pId->spanId;
202 }
203 
HiTraceChainSetSpanId(HiTraceIdStruct * pId,uint64_t spanId)204 static inline void HiTraceChainSetSpanId(HiTraceIdStruct* pId, uint64_t spanId)
205 {
206     if (HiTraceChainIsValid(pId)) {
207         pId->spanId = spanId;
208     }
209     return;
210 }
211 
HiTraceChainGetParentSpanId(const HiTraceIdStruct * pId)212 static inline uint64_t HiTraceChainGetParentSpanId(const HiTraceIdStruct* pId)
213 {
214     if (!HiTraceChainIsValid(pId)) {
215         return 0;
216     }
217     return pId->parentSpanId;
218 }
219 
HiTraceChainSetParentSpanId(HiTraceIdStruct * pId,uint64_t parentSpanId)220 static inline void HiTraceChainSetParentSpanId(HiTraceIdStruct* pId, uint64_t parentSpanId)
221 {
222     if (HiTraceChainIsValid(pId)) {
223         pId->parentSpanId = parentSpanId;
224     }
225     return;
226 }
227 
HiTraceChainIdToBytes(const HiTraceIdStruct * pId,uint8_t * pIdArray,int len)228 static inline int HiTraceChainIdToBytes(const HiTraceIdStruct* pId, uint8_t* pIdArray, int len)
229 {
230     if (!HiTraceChainIsValid(pId) || (len < (int)HITRACE_ID_LEN)) {
231         return 0;
232     }
233 
234     *((uint64_t*)pIdArray) = htobe64(*((uint64_t*)pId));
235     *((uint64_t*)pIdArray + 1) = htobe64(*((uint64_t*)pId + 1));
236     return sizeof(HiTraceIdStruct);
237 }
238 
HiTraceChainBytesToId(const uint8_t * pIdArray,int len)239 static inline HiTraceIdStruct HiTraceChainBytesToId(const uint8_t* pIdArray, int len)
240 {
241     HiTraceIdStruct id = {0, 0, 0, 0, 0, 0};
242     HiTraceChainInitId(&id);
243 
244     if ((!pIdArray) || (len != (int)HITRACE_ID_LEN)) {
245         return id;
246     }
247 
248     uint64_t tmp1 = 0;
249     uint64_t tmp2 = 0;
250     if (memcpy_s(&tmp1, sizeof(tmp1), pIdArray, sizeof(uint64_t)) != EOK ||
251         memcpy_s(&tmp2, sizeof(tmp2), pIdArray + sizeof(uint64_t), sizeof(uint64_t)) != EOK) {
252         return id;
253     }
254     *((uint64_t*)(&id)) = be64toh(tmp1);
255     *((uint64_t*)(&id) + 1) = be64toh(tmp2);
256     return id;
257 }
258 
259 #ifdef __cplusplus
260 }
261 #endif
262 
263 #endif // HIVIEWDFX_HITRACECHAIN_C_H
264