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 char *pack_int32(char *to, uint32_t val)
43 *(uint32_t *)to = val;
44 return to + sizeof(uint32_t);
47 static char *pack_int64(char *to, uint64_t val)
49 *(uint64_t *)to = val;
50 return to + sizeof(uint64_t);
53 static char *pack_addr(char *to, unsigned long addr)
55 return pack_int64(to, (intptr_t)addr);
58 static char *pack_string(char *to, const char *str)
60 size_t len = strlen(str) + 1;
61 strncpy(to, str, len);
66 static char *pack_msg_id(char *to, uint32_t msg_id)
68 return pack_int32(to, msg_id);
71 static char *pack_seq_num(char *to)
74 return pack_int32(to, 0);
77 static char *pack_timestamp(char *to)
81 gettimeofday(&tv, NULL);
82 to = pack_int32(to, tv.tv_sec);
83 to = pack_int32(to, tv.tv_usec * 1000);
88 static char *pack_api_id(char *to, uint32_t api_id)
90 return pack_int32(to, api_id);
93 static char *pack_args(char *to, const char *fmt, ...)
96 uint32_t num = strlen(fmt);
99 memcpy(to, &num, sizeof(num));
113 for (t = fmt; *t != '\0'; t++) {
116 c = (uint8_t)va_arg(args, uint32_t);
118 memcpy(to, &c, sizeof(c));
122 d = va_arg(args, uint32_t);
124 memcpy(to, &d, sizeof(d));
128 x = va_arg(args, uint64_t);
130 memcpy(to, &x, sizeof(x));
134 p = va_arg(args, uint64_t);
136 memcpy(to, &p, sizeof(p));
140 f = (float)va_arg(args, double);
142 memcpy(to, &f, sizeof(f));
146 w = va_arg(args, double);
148 memcpy(to, &w, sizeof(w));
152 s = va_arg(args, char *);
168 static char *pack_pid(char *to)
170 return pack_int32(to, getpid());
173 static char *pack_tid(char *to)
175 return pack_int32(to, syscall(__NR_gettid));
178 static char *pack_return(char *to, uint64_t ret)
180 return pack_int64(to, ret);
183 static char *pack_pc(char *to, uint64_t pc)
185 return pack_int64(to, pc);
188 static char *pack_errno(char *to, uint32_t en)
190 return pack_int32(to, en);
193 static char *pack_internal_call(char *to)
195 // TODO: where to get int call?
196 return pack_int32(to, 0);
199 static char *pack_caller_pc(char *to)
201 return pack_int64(to, (uintptr_t)CALLER_ADDRESS);
205 #define PACK_COMMON_BEGIN(to, msg_id, api_id, fmt, ...) \
206 to = pack_msg_id(to, msg_id); \
207 to = pack_seq_num(to); \
208 to = pack_timestamp(to); \
209 to = pack_int32(to, 0); \
210 to = pack_api_id(to, api_id); \
213 to = pack_args(to, fmt, __VA_ARGS__)
215 #define PACK_COMMON_END(to, ret, pc, errn) \
216 to = pack_return(to, (uintptr_t)ret); \
217 to = pack_pc(to, (uintptr_t)pc); \
218 to = pack_errno(to, (uint32_t)errn); \
219 to = pack_internal_call(to); \
220 to = pack_caller_pc(to); \
221 to = pack_int32(to, 0); \
222 to = pack_int32(to, 0);
224 #define PACK_MEMORY(to, size, memory_api_type, addr) \
225 to = pack_int32(to, size); \
226 to = pack_int32(to, memory_api_type); \
227 to = pack_int64(to, (uintptr_t)addr);
229 #define PACK_UICONTROL(to, parent_name, parent_class_name, \
230 parent_pointer, child_name, child_class_name, \
232 to = pack_string(to, parent_name); \
233 to = pack_string(to, parent_class_name); \
234 to = pack_int64(to, parent_pointer); \
235 to = pack_string(to, child_name); \
236 to = pack_string(to, child_class_name); \
237 to = pack_int64(to, child_pointer);
239 #define PACK_UIEVENT(to, event_type, detail_type, x, y, info1, info2) \
240 to = pack_int32(to, event_type); \
241 to = pack_int32(to, detail_type); \
242 to = pack_int32(to, x); \
243 to = pack_int32(to, y); \
244 to = pack_string(to, info1); \
245 to = pack_int32(to, info2);
247 #define PACK_RESOURCE(to, size, fd_value, fd_type, fd_api_type, \
248 file_size, file_path) \
249 to = pack_int32(to, size); \
250 to = pack_int32(to, fd_value); \
251 to = pack_int32(to, fd_type); \
252 to = pack_int32(to, fd_api_type); \
253 to = pack_int32(to, file_size); \
254 to = pack_string(to, file_path);
256 #define PACK_LIFECYCLE(to)
258 #define PACK_SCREENSHOT(to, image_file_path, orienation) \
259 to = pack_string(to, image_file_path); \
260 to = pack_int32(to, orienation);
262 #define PACK_SCENE(to, scene_name, form_name, form_pointer, \
263 panel_name, panel_pointer, transition_time, \
264 user_transition_time) \
265 to = pack_string(to, scene_name); \
266 to = pack_string(to, form_name); \
267 to = pack_int64(to, form_pointer); \
268 to = pack_string(to, panel_name); \
269 to = pack_int64(to, panel_pointer); \
270 to = pack_int32(to, transition_time); \
271 to = pack_int32(to, user_transition_time);
273 #define PACK_THREAD(to, pthread_id, osp_thread_id, thread_type, api_type) \
274 to = pack_int32(to, pthread_id); \
275 to = pack_int32(to, osp_thread_id); \
276 to = pack_int32(to, thread_type); \
277 to = pack_int32(to, api_type);
279 #define PACK_CUSTOM(to, handle, type, name, color, value) \
280 to = pack_int32(to, handle); \
281 to = pack_int32(to, type); \
282 to = pack_string(to, name); \
283 to = pack_int32(to, color); \
284 to = pack_int32(to, value);
286 #define PACK_SYNC(to, sync_val, sync_type, api_type) \
287 to = pack_int32(to, sync_val); \
288 to = pack_int32(to, sync_type); \
289 to = pack_int32(to, api_type);
291 #define LOG_PATH "/tmp/trace.bin"
293 log_fd = creat(LOG_PATH, 0644); \
294 if (log_fd == -1) { \
298 #define CLOSE_LOG() \
304 #define LOCAL_BUF_SIZE 1024
305 #define PREPARE_LOCAL_BUF() \
306 char buf[LOCAL_BUF_SIZE]; \
309 #define MSG_LEN_OFFSET 16
310 #define MSG_HDR_LEN 20
311 #define FLUSH_LOCAL_BUF() \
312 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
313 write(log_fd, buf, p - buf);
319 /* int main(int argc, char **argv) */
321 /* char buf[1024]; */
324 /* p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
325 /* (uint64_t)0, 0.19, 0.33, "hello!"); */
326 /* p = PACK_COMMON_END(p, 0); */
328 /* int fd = creat("out.bin", 0644); */
329 /* if (fd == -1) { */
330 /* printf("error\n"); */
334 /* write(fd, buf, p - buf); */
340 #endif /* __BIN_PROTO_H__ */