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 #define MSG_PROBE_MEMORY 0x3001
24 #define MSG_PROBE_UICONTROL 0x3002
25 #define MSG_PROBE_UIEVENT 0x3003
26 #define MSG_PROBE_RESOUCE 0x3004
27 #define MSG_PROBE_LIFECYCLE 0x30015
28 #define MSG_PROBE_SCREENSHOT 0x3006
29 #define MSG_PROBE_SCENE 0x3007
30 #define MSG_PROBE_THREAD 0x3008
31 #define MSG_PROBE_CUSTOM 0x3009
32 #define MSG_PROBE_SYNC 0x3010
34 // TODO: remove this copy-paste
35 #define CALLER_ADDRESS \
36 ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
41 static inline char *pack_int32(char *to, uint32_t val)
43 *(uint32_t *)to = val;
44 return to + sizeof(uint32_t);
47 static inline char *pack_int64(char *to, uint64_t val)
49 *(uint64_t *)to = val;
50 return to + sizeof(uint64_t);
53 static inline char *pack_string(char *to, const char *str)
55 size_t len = strlen(str) + 1;
56 strncpy(to, str, len);
60 static inline char *pack_timestamp(char *to)
64 gettimeofday(&tv, NULL);
65 to = pack_int32(to, tv.tv_sec);
66 to = pack_int32(to, tv.tv_usec * 1000);
71 static inline char *pack_args(char *to, const char *fmt, ...)
74 uint32_t num = strlen(fmt);
77 memcpy(to, &num, sizeof(num));
91 for (t = fmt; *t != '\0'; t++) {
94 c = (uint8_t)va_arg(args, uint32_t);
96 memcpy(to, &c, sizeof(c));
100 d = va_arg(args, uint32_t);
102 memcpy(to, &d, sizeof(d));
106 x = (uint64_t)va_arg(args, uint64_t);
108 memcpy(to, &x, sizeof(x));
112 p = (uintptr_t)va_arg(args, uint64_t);
114 memcpy(to, &p, sizeof(p));
118 f = (float)va_arg(args, double);
120 memcpy(to, &f, sizeof(f));
124 w = va_arg(args, double);
126 memcpy(to, &w, sizeof(w));
130 s = va_arg(args, char *);
147 #define RET_PTR ret_p
148 #define PACK_INT32(val) \
149 BUF_PTR = pack_int32(BUF_PTR, val);
150 #define PACK_INT64(val) \
151 BUF_PTR = pack_int64(BUF_PTR, val);
152 #define PACK_STRING(str) \
153 BUF_PTR = pack_string(BUF_PTR, str);
155 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...) \
157 BUF_PTR = pack_int32(BUF_PTR, msg_id); \
158 BUF_PTR = pack_int32(BUF_PTR, 0); \
159 BUF_PTR = pack_timestamp(BUF_PTR); \
160 BUF_PTR = pack_int32(BUF_PTR, 0); \
161 BUF_PTR = pack_int32(BUF_PTR, api_id); \
162 BUF_PTR = pack_int32(BUF_PTR, getpid()); \
163 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid)); \
164 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
168 #define PACK_COMMON_END(ret, errn, intern_call) \
170 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret)); \
171 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn); \
172 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call); \
173 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
174 BUF_PTR = pack_int32(BUF_PTR, 0); \
175 BUF_PTR = pack_int32(BUF_PTR, 0); \
178 #define PACK_RETURN_END(ret) \
179 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
181 #define PACK_MEMORY(size, memory_api_type, addr) \
183 BUF_PTR = pack_int32(BUF_PTR, size); \
184 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
185 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
188 #define PACK_UICONTROL(control) \
190 if (unlikely(control == NULL)) { \
191 BUF_PTR = pack_string(BUF_PTR, ""); \
192 BUF_PTR = pack_string(BUF_PTR, ""); \
193 BUF_PTR = pack_int64(BUF_PTR, 0); \
195 char *type = NULL, *name = NULL; \
196 if (find_object_hash((void*)(control), \
197 &type, &name) == 1) { \
198 BUF_PTR = pack_string(BUF_PTR, type); \
199 BUF_PTR = pack_string(BUF_PTR, name); \
201 BUF_PTR = pack_string(BUF_PTR, ""); \
202 BUF_PTR = pack_string(BUF_PTR, ""); \
204 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
208 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2) \
210 BUF_PTR = pack_int32(BUF_PTR, event_type); \
211 BUF_PTR = pack_int32(BUF_PTR, detail_type); \
212 BUF_PTR = pack_int32(BUF_PTR, x); \
213 BUF_PTR = pack_int32(BUF_PTR, y); \
214 BUF_PTR = pack_string(BUF_PTR, info1); \
215 BUF_PTR = pack_int32(BUF_PTR, info2); \
218 /* #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size, \ */
221 /* BUF_PTR = pack_int32(BUF_PTR, size); \ */
222 /* BUF_PTR = pack_int32(BUF_PTR, fd_value); \ */
223 /* BUF_PTR = pack_int32(BUF_PTR, fd_type); \ */
224 /* BUF_PTR = pack_int32(BUF_PTR, fd_api_type); \ */
225 /* BUF_PTR = pack_int32(BUF_PTR, file_size); \ */
226 /* BUF_PTR = pack_string(BUF_PTR, file_path); \ */
229 /* #define PACK_SCREENSHOT(image_file_path, orienation) \ */
231 /* BUF_PTR = pack_string(BUF_PTR, image_file_path); \ */
232 /* BUF_PTR = pack_int32(BUF_PTR, orienation); \ */
235 /* #define PACK_SCENE(scene_name, form_name, form_pointer, \ */
236 /* panel_name, panel_pointer, transition_time, \ */
237 /* user_transition_time) \ */
239 /* BUF_PTR = pack_string(BUF_PTR, scene_name); \ */
240 /* BUF_PTR = pack_string(BUF_PTR, form_name); \ */
241 /* BUF_PTR = pack_int64(BUF_PTR, form_pointer); \ */
242 /* BUF_PTR = pack_string(BUF_PTR, panel_name); \ */
243 /* BUF_PTR = pack_int64(BUF_PTR, panel_pointer); \ */
244 /* BUF_PTR = pack_int32(BUF_PTR, transition_time); \ */
245 /* BUF_PTR = pack_int32(BUF_PTR, user_transition_time); \ */
248 /* #define PACK_THREAD(BUF_PTRthread_id, osp_thread_id, thread_type, api_type) \ */
249 /* BUF_PTR = pack_int32(BUF_PTR, pthread_id); \ */
250 /* BUF_PTR = pack_int32(BUF_PTR, osp_thread_id); \ */
251 /* BUF_PTR = pack_int32(BUF_PTR, thread_type); \ */
252 /* BUF_PTR = pack_int32(BUF_PTR, api_type); */
254 /* #define PACK_CUSTOM(handle, type, name, color, value) \ */
255 /* BUF_PTR = pack_int32(BUF_PTR, handle); \ */
256 /* BUF_PTR = pack_int32(BUF_PTR, type); \ */
257 /* BUF_PTR = pack_string(BUF_PTR, name); \ */
258 /* BUF_PTR = pack_int32(BUF_PTR, color); \ */
259 /* BUF_PTR = pack_int32(BUF_PTR, value); */
261 #define PACK_SYNC(sync_val, sync_type, api_type) \
263 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val); \
264 BUF_PTR = pack_int32(BUF_PTR, sync_type); \
265 BUF_PTR = pack_int32(BUF_PTR, api_type); \
268 #define LOG_PATH "/tmp/trace.bin"
271 log_fd = creat(LOG_PATH, 0644); \
272 if (log_fd == -1) { \
276 #define CLOSE_LOG() \
285 #define LOCAL_BUF_SIZE 1024
286 #define PREPARE_LOCAL_BUF() \
287 char buf[LOCAL_BUF_SIZE]; \
291 #define MSG_LEN_OFFSET 16
292 #define MSG_HDR_LEN 20
293 #define FLUSH_LOCAL_BUF() \
294 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
295 write(log_fd, buf, p - buf);
297 // =========================== post block macro ===========================
299 #define POST_PACK_PROBEBLOCK_BEGIN(LCTYPE, RETVALUE, INPUTFORMAT, ...) \
301 if(postBlockBegin(blockresult)) {
303 #define POST_PACK_PROBEBLOCK_END() \
306 errno = (newerrno != 0) ? newerrno : olderrno
311 /* int main(int argc, char **argv) */
313 /* char buf[1024]; */
316 /* p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
317 /* (uint64_t)0, 0.19, 0.33, "hello!"); */
318 /* p = PACK_COMMON_END(p, 0); */
320 /* int fd = creat("out.bin", 0644); */
321 /* if (fd == -1) { */
322 /* printf("error\n"); */
326 /* write(fd, buf, p - buf); */
332 #endif /* __BIN_PROTO_H__ */