1 #ifndef __BIN_PROTO_H__
2 #define __BIN_PROTO_H__
13 #include <sys/types.h>
17 #include <sys/types.h>
19 #include <sys/syscall.h>
23 #include <sys/socket.h>
26 #define MSG_PROBE_MEMORY 0x3001
27 #define MSG_PROBE_UICONTROL 0x3002
28 #define MSG_PROBE_UIEVENT 0x3003
29 #define MSG_PROBE_RESOURCE 0x3004
30 #define MSG_PROBE_LIFECYCLE 0x3005
31 #define MSG_PROBE_SCREENSHOT 0x3006
32 #define MSG_PROBE_SCENE 0x3007
33 #define MSG_PROBE_THREAD 0x3008
34 #define MSG_PROBE_CUSTOM 0x3009
35 #define MSG_PROBE_SYNC 0x3010
37 // TODO: remove this copy-paste
38 #define CALLER_ADDRESS \
39 ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
44 static inline char *pack_int32(char *to, uint32_t val)
46 *(uint32_t *)to = val;
47 return to + sizeof(uint32_t);
50 static inline char *pack_int64(char *to, uint64_t val)
52 *(uint64_t *)to = val;
53 return to + sizeof(uint64_t);
56 static inline char *pack_string(char *to, const char *str)
58 size_t len = strlen(str) + 1;
59 strncpy(to, str, len);
63 static inline char *pack_double(char *to, double val)
66 return to + sizeof(double);
69 static inline char *pack_timestamp(char *to)
73 gettimeofday(&tv, NULL);
74 to = pack_int32(to, tv.tv_sec);
75 to = pack_int32(to, tv.tv_usec * 1000);
80 static inline char *pack_args(char *to, const char *fmt, ...)
83 uint32_t num = strlen(fmt);
90 memcpy(to, &num, sizeof(num));
104 for (t = fmt; *t != '\0'; t++) {
107 c = (uint8_t)va_arg(args, uint32_t);
109 memcpy(to, &c, sizeof(c));
113 d = va_arg(args, uint32_t);
115 memcpy(to, &d, sizeof(d));
119 x = (uint64_t)va_arg(args, uint64_t);
121 memcpy(to, &x, sizeof(x));
125 p = (uintptr_t)va_arg(args, uint64_t);
127 memcpy(to, &p, sizeof(p));
131 f = (float)va_arg(args, double);
133 memcpy(to, &f, sizeof(f));
137 w = va_arg(args, double);
139 memcpy(to, &w, sizeof(w));
143 s = va_arg(args, char *);
160 #define RET_PTR ret_p
161 #define PACK_INT32(val) \
162 BUF_PTR = pack_int32(BUF_PTR, val);
163 #define PACK_INT64(val) \
164 BUF_PTR = pack_int64(BUF_PTR, val);
165 #define PACK_STRING(str) \
166 BUF_PTR = pack_string(BUF_PTR, str);
168 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...) \
170 BUF_PTR = pack_int32(BUF_PTR, msg_id); \
171 BUF_PTR = pack_int32(BUF_PTR, 0); \
172 BUF_PTR = pack_timestamp(BUF_PTR); \
173 BUF_PTR = pack_int32(BUF_PTR, 0); \
174 BUF_PTR = pack_int32(BUF_PTR, api_id); \
175 BUF_PTR = pack_int32(BUF_PTR, getpid()); \
176 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid)); \
177 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
181 #define PACK_COMMON_END(ret, errn, intern_call) \
183 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret)); \
184 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn); \
185 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call); \
186 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
187 BUF_PTR = pack_int32(BUF_PTR, 0); \
188 BUF_PTR = pack_int32(BUF_PTR, 0); \
191 #define PACK_RETURN_END(ret) \
192 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
194 #define PACK_MEMORY(size, memory_api_type, addr) \
196 BUF_PTR = pack_int64(BUF_PTR, size); \
197 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
198 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
201 #define PACK_UICONTROL(control) \
203 if (unlikely(control == NULL)) { \
204 BUF_PTR = pack_string(BUF_PTR, ""); \
205 BUF_PTR = pack_string(BUF_PTR, ""); \
206 BUF_PTR = pack_int64(BUF_PTR, 0); \
208 char *type = NULL, *name = NULL; \
209 if (find_object_hash((void*)(control), \
210 &type, &name) == 1) { \
211 BUF_PTR = pack_string(BUF_PTR, type); \
212 BUF_PTR = pack_string(BUF_PTR, name); \
214 BUF_PTR = pack_string(BUF_PTR, ""); \
215 BUF_PTR = pack_string(BUF_PTR, ""); \
217 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
221 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2) \
223 BUF_PTR = pack_int32(BUF_PTR, event_type); \
224 BUF_PTR = pack_int32(BUF_PTR, detail_type); \
225 BUF_PTR = pack_int32(BUF_PTR, x); \
226 BUF_PTR = pack_int32(BUF_PTR, y); \
227 BUF_PTR = pack_string(BUF_PTR, info1); \
228 BUF_PTR = pack_int32(BUF_PTR, info2); \
231 #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size, \
234 BUF_PTR = pack_int64(BUF_PTR, size); \
235 BUF_PTR = pack_int32(BUF_PTR, fd_value); \
236 BUF_PTR = pack_int32(BUF_PTR, fd_type); \
237 BUF_PTR = pack_int32(BUF_PTR, fd_api_type); \
238 BUF_PTR = pack_int64(BUF_PTR, file_size); \
239 BUF_PTR = pack_string(BUF_PTR, file_path); \
242 #define PACK_SCREENSHOT(image_file_path, orientation) \
244 BUF_PTR = pack_string(BUF_PTR, image_file_path); \
245 BUF_PTR = pack_int32(BUF_PTR, orientation); \
248 #define PACK_SCENE(scene_name, formid, pform, panelid, ppanel, transition, user) \
250 BUF_PTR = pack_string(BUF_PTR, scene_name); \
251 if (unlikely(pform == NULL)) { \
252 BUF_PTR = pack_string(BUF_PTR, ""); \
253 BUF_PTR = pack_int64(BUF_PTR, 0); \
255 char *type = NULL, *name = NULL; \
256 if (find_object_hash((void*)(pform), &type, &name) == 1) { \
257 BUF_PTR = pack_string(BUF_PTR, name); \
259 BUF_PTR = pack_string(BUF_PTR, ""); \
261 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)pform); \
263 if (unlikely(ppanel == NULL)) { \
264 BUF_PTR = pack_string(BUF_PTR, ""); \
265 BUF_PTR = pack_int64(BUF_PTR, 0); \
267 char *type = NULL, *name = NULL; \
268 if (find_object_hash((void*)(ppanel), &type, &name) == 1) { \
269 BUF_PTR = pack_string(BUF_PTR, name); \
271 BUF_PTR = pack_string(BUF_PTR, ""); \
273 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ppanel)); \
275 BUF_PTR = pack_int64(BUF_PTR, transition); \
276 BUF_PTR = pack_int64(BUF_PTR, user); \
279 #define PACK_THREAD(thread_id, thread_type, api_type) \
281 BUF_PTR = pack_int64(BUF_PTR, thread_id); \
282 BUF_PTR = pack_int32(BUF_PTR, thread_type); \
283 BUF_PTR = pack_int32(BUF_PTR, api_type); \
286 #define PACK_CUSTOM(handle, type, name, color, value) \
288 BUF_PTR = pack_int32(BUF_PTR, handle); \
289 BUF_PTR = pack_int32(BUF_PTR, type); \
290 BUF_PTR = pack_string(BUF_PTR, name); \
291 BUF_PTR = pack_int32(BUF_PTR, color); \
292 BUF_PTR = pack_double(BUF_PTR, value); \
295 #define PACK_SYNC(sync_val, sync_type, api_type) \
297 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val); \
298 BUF_PTR = pack_int32(BUF_PTR, sync_type); \
299 BUF_PTR = pack_int32(BUF_PTR, api_type); \
302 #define LOCAL_BUF_SIZE 1024
303 #define PREPARE_LOCAL_BUF() \
304 char buf[LOCAL_BUF_SIZE]; \
308 #define MSG_LEN_OFFSET 16
309 #define MSG_HDR_LEN 20
310 #define FLUSH_LOCAL_BUF() \
311 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
312 send(gTraceInfo.socket.daemonSock, buf, (p - buf), 0);
314 // =========================== post block macro ===========================
316 #define POST_PACK_PROBEBLOCK_BEGIN() \
318 if(postBlockBegin(blockresult)) {
320 #define POST_PACK_PROBEBLOCK_END() \
323 errno = (newerrno != 0) ? newerrno : olderrno
325 /* int main(int argc, char **argv) */
327 /* char buf[1024]; */
330 /* p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
331 /* (uint64_t)0, 0.19, 0.33, "hello!"); */
332 /* p = PACK_COMMON_END(p, 0); */
334 /* int fd = creat("out.bin", 0644); */
335 /* if (fd == -1) { */
336 /* printf("error\n"); */
340 /* write(fd, buf, p - buf); */
346 #endif /* __BIN_PROTO_H__ */