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 /* printf("num = %d\n", num); */
101 memcpy(to, &num, sizeof(num));
104 memcpy(to, fmt, num);
118 for (t = fmt; *t != '\0'; t++) {
121 c = (uint8_t)va_arg(args, uint32_t);
122 /* printf("char = %c\n", c); */
123 memcpy(to, &c, sizeof(c));
127 d = va_arg(args, uint32_t);
128 /* printf("int = %d\n", d); */
129 memcpy(to, &d, sizeof(d));
133 x = va_arg(args, uint64_t);
134 /* printf("long = %lld\n", x); */
135 memcpy(to, &x, sizeof(x));
139 p = va_arg(args, uint64_t);
140 /* printf("pointer = %p\n", (void *)p); */
141 memcpy(to, &p, sizeof(p));
145 f = (float)va_arg(args, double);
146 /* printf("float = %f\n", f); */
147 memcpy(to, &f, sizeof(f));
151 w = va_arg(args, double);
152 /* printf("double = %e\n", w); */
153 memcpy(to, &w, sizeof(w));
157 s = va_arg(args, char *);
158 /* printf("string = %s\n", s); */
173 static char *pack_pid(char *to)
175 return pack_int32(to, getpid());
178 static char *pack_tid(char *to)
180 return pack_int32(to, syscall(__NR_gettid));
183 static char *pack_return(char *to, uint64_t ret)
185 return pack_int64(to, ret);
188 static char *pack_pc(char *to, uint64_t pc)
190 return pack_int64(to, pc);
193 static char *pack_errno(char *to, uint32_t en)
195 return pack_int32(to, en);
198 static char *pack_internal_call(char *to)
200 // TODO: where to get int call?
201 return pack_int32(to, 0);
204 static char *pack_caller_pc(char *to)
206 return pack_int64(to, (uintptr_t)CALLER_ADDRESS);
210 #define PACK_COMMON_BEGIN(to, msg_id, api_id, fmt, ...) \
211 to = pack_msg_id(to, msg_id); \
212 to = pack_seq_num(to); \
213 to = pack_timestamp(to); \
214 to = pack_int32(to, 0); \
215 to = pack_api_id(to, api_id); \
218 to = pack_args(to, fmt, __VA_ARGS__)
220 #define PACK_COMMON_END(to, ret, pc, errn) \
221 to = pack_return(to, (uintptr_t)ret); \
222 to = pack_pc(to, (uintptr_t)pc); \
223 to = pack_errno(to, (uint32_t)errn); \
224 to = pack_internal_call(to); \
225 to = pack_caller_pc(to); \
226 to = pack_int32(to, 0); \
227 to = pack_int32(to, 0);
229 #define PACK_MEMORY(to, size, memory_api_type, addr) \
230 to = pack_int32(to, size); \
231 to = pack_int32(to, memory_api_type); \
232 to = pack_int64(to, (uintptr_t)addr);
234 #define PACK_UICONTROL(to, parent_name, parent_class_name, \
235 parent_pointer, child_name, child_class_name, \
237 to = pack_string(to, parent_name); \
238 to = pack_string(to, parent_class_name); \
239 to = pack_int64(to, parent_pointer); \
240 to = pack_string(to, child_name); \
241 to = pack_string(to, child_class_name); \
242 to = pack_int64(to, child_pointer);
244 #define PACK_UIEVENT(to, event_type, detail_type, x, y, info1, info2) \
245 to = pack_int32(to, event_type); \
246 to = pack_int32(to, detail_type); \
247 to = pack_int32(to, x); \
248 to = pack_int32(to, y); \
249 to = pack_int32(to, info1); \
250 to = pack_int32(to, info2);
252 #define PACK_RESOURCE(to, size, fd_value, fd_type, fd_api_type, \
253 file_size, file_path) \
254 to = pack_int32(to, size); \
255 to = pack_int32(to, fd_value); \
256 to = pack_int32(to, fd_type); \
257 to = pack_int32(to, fd_api_type); \
258 to = pack_int32(to, file_size); \
259 to = pack_string(to, file_path);
261 #define PACK_LIFECYCLE(to)
263 #define PACK_SCREENSHOT(to, image_file_path, orienation) \
264 to = pack_string(to, image_file_path); \
265 to = pack_int32(to, orienation);
267 #define PACK_SCENE(to, scene_name, form_name, form_pointer, \
268 panel_name, panel_pointer, transition_time, \
269 user_transition_time) \
270 to = pack_string(to, scene_name); \
271 to = pack_string(to, form_name); \
272 to = pack_int64(to, form_pointer); \
273 to = pack_string(to, panel_name); \
274 to = pack_int64(to, panel_pointer); \
275 to = pack_int32(to, transition_time); \
276 to = pack_int32(to, user_transition_time);
278 #define PACK_THREAD(to, pthread_id, osp_thread_id, thread_type, api_type) \
279 to = pack_int32(to, pthread_id); \
280 to = pack_int32(to, osp_thread_id); \
281 to = pack_int32(to, thread_type); \
282 to = pack_int32(to, api_type);
284 #define PACK_CUSTOM(to, handle, type, name, color, value) \
285 to = pack_int32(to, handle); \
286 to = pack_int32(to, type); \
287 to = pack_string(to, name); \
288 to = pack_int32(to, color); \
289 to = pack_int32(to, value);
291 #define PACK_SYNC(to, sync_val, sync_type, api_type) \
292 to = pack_int32(to, sync_val); \
293 to = pack_int32(to, sync_type); \
294 to = pack_int32(to, api_type);
296 #define LOG_PATH "/tmp/trace.bin"
298 log_fd = creat(LOG_PATH, 0644); \
299 if (log_fd == -1) { \
303 #define CLOSE_LOG() \
309 #define LOCAL_BUF_SIZE 1024
310 #define PREPARE_LOCAL_BUF() \
311 char buf[LOCAL_BUF_SIZE]; \
314 #define MSG_LEN_OFFSET 16
315 #define MSG_HDR_LEN 20
316 #define FLUSH_LOCAL_BUF() \
317 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
318 write(log_fd, buf, p - buf);
324 /* int main(int argc, char **argv) */
326 /* char buf[1024]; */
329 /* p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
330 /* (uint64_t)0, 0.19, 0.33, "hello!"); */
331 /* p = PACK_COMMON_END(p, 0); */
333 /* int fd = creat("out.bin", 0644); */
334 /* if (fd == -1) { */
335 /* printf("error\n"); */
339 /* write(fd, buf, p - buf); */
345 #endif /* __BIN_PROTO_H__ */