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_string(char *to, const char *str)
55 size_t len = strlen(str) + 1;
56 strncpy(to, str, len);
60 static 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 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 = va_arg(args, uint64_t);
108 memcpy(to, &x, sizeof(x));
112 p = 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 PACK_INT32(val) \
148 BUF_PTR = pack_int32(BUF_PTR, val);
149 #define PACK_INT64(val) \
150 BUF_PTR = pack_int64(BUF_PTR, val);
151 #define PACK_STRING(str) \
152 BUF_PTR = pack_string(BUF_PTR, str);
154 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...) \
156 BUF_PTR = pack_int32(BUF_PTR, msg_id); \
157 BUF_PTR = pack_int32(BUF_PTR, 0); \
158 BUF_PTR = pack_timestamp(BUF_PTR); \
159 BUF_PTR = pack_int32(BUF_PTR, 0); \
160 BUF_PTR = pack_int32(BUF_PTR, api_id); \
161 BUF_PTR = pack_int32(BUF_PTR, getpid()); \
162 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid)); \
163 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
166 #define PACK_COMMON_END(ret, pc, errn) \
168 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret)); \
169 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(pc)); \
170 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn); \
171 BUF_PTR = pack_int32(BUF_PTR, 0); \
172 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
173 BUF_PTR = pack_int32(BUF_PTR, 0); \
174 BUF_PTR = pack_int32(BUF_PTR, 0); \
177 #define PACK_MEMORY(size, memory_api_type, addr) \
179 BUF_PTR = pack_int32(p, size); \
180 BUF_PTR = pack_int32(p, memory_api_type); \
181 BUF_PTR = pack_int64(p, (uintptr_t)addr); \
184 #define PACK_UICONTROL(control) \
186 if (unlikely(control == NULL)) { \
187 BUF_PTR = pack_string(BUF_PTR, ""); \
188 BUF_PTR = pack_string(BUF_PTR, ""); \
189 BUF_PTR = pack_int64(BUF_PTR, 0); \
191 char *type = NULL, *name = NULL; \
192 if (find_object_hash((void*)(control), \
193 &type, &name) == 1) { \
194 BUF_PTR = pack_string(BUF_PTR, type); \
195 BUF_PTR = pack_string(BUF_PTR, name); \
197 BUF_PTR = pack_string(BUF_PTR, ""); \
198 BUF_PTR = pack_string(BUF_PTR, ""); \
200 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
204 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2) \
206 BUF_PTR = pack_int32(BUF_PTR, event_type); \
207 BUF_PTR = pack_int32(BUF_PTR, detail_type); \
208 BUF_PTR = pack_int32(BUF_PTR, x); \
209 BUF_PTR = pack_int32(BUF_PTR, y); \
210 BUF_PTR = pack_string(BUF_PTR, info1); \
211 BUF_PTR = pack_int32(BUF_PTR, info2); \
214 /* #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size, \ */
217 /* BUF_PTR = pack_int32(BUF_PTR, size); \ */
218 /* BUF_PTR = pack_int32(BUF_PTR, fd_value); \ */
219 /* BUF_PTR = pack_int32(BUF_PTR, fd_type); \ */
220 /* BUF_PTR = pack_int32(BUF_PTR, fd_api_type); \ */
221 /* BUF_PTR = pack_int32(BUF_PTR, file_size); \ */
222 /* BUF_PTR = pack_string(BUF_PTR, file_path); \ */
225 /* #define PACK_SCREENSHOT(image_file_path, orienation) \ */
227 /* BUF_PTR = pack_string(BUF_PTR, image_file_path); \ */
228 /* BUF_PTR = pack_int32(BUF_PTR, orienation); \ */
231 /* #define PACK_SCENE(scene_name, form_name, form_pointer, \ */
232 /* panel_name, panel_pointer, transition_time, \ */
233 /* user_transition_time) \ */
235 /* BUF_PTR = pack_string(BUF_PTR, scene_name); \ */
236 /* BUF_PTR = pack_string(BUF_PTR, form_name); \ */
237 /* BUF_PTR = pack_int64(BUF_PTR, form_pointer); \ */
238 /* BUF_PTR = pack_string(BUF_PTR, panel_name); \ */
239 /* BUF_PTR = pack_int64(BUF_PTR, panel_pointer); \ */
240 /* BUF_PTR = pack_int32(BUF_PTR, transition_time); \ */
241 /* BUF_PTR = pack_int32(BUF_PTR, user_transition_time); \ */
244 /* #define PACK_THREAD(BUF_PTRthread_id, osp_thread_id, thread_type, api_type) \ */
245 /* BUF_PTR = pack_int32(BUF_PTR, pthread_id); \ */
246 /* BUF_PTR = pack_int32(BUF_PTR, osp_thread_id); \ */
247 /* BUF_PTR = pack_int32(BUF_PTR, thread_type); \ */
248 /* BUF_PTR = pack_int32(BUF_PTR, api_type); */
250 /* #define PACK_CUSTOM(handle, type, name, color, value) \ */
251 /* BUF_PTR = pack_int32(BUF_PTR, handle); \ */
252 /* BUF_PTR = pack_int32(BUF_PTR, type); \ */
253 /* BUF_PTR = pack_string(BUF_PTR, name); \ */
254 /* BUF_PTR = pack_int32(BUF_PTR, color); \ */
255 /* BUF_PTR = pack_int32(BUF_PTR, value); */
257 #define PACK_SYNC(sync_val, sync_type, api_type) \
259 BUF_PTR = pack_int32(BUF_PTR, sync_val); \
260 BUF_PTR = pack_int32(BUF_PTR, sync_type); \
261 BUF_PTR = pack_int32(BUF_PTR, api_type); \
264 #define LOG_PATH "/tmp/trace.bin"
267 log_fd = creat(LOG_PATH, 0644); \
268 if (log_fd == -1) { \
272 #define CLOSE_LOG() \
281 #define LOCAL_BUF_SIZE 1024
282 #define PREPARE_LOCAL_BUF() \
283 char buf[LOCAL_BUF_SIZE]; \
286 #define MSG_LEN_OFFSET 16
287 #define MSG_HDR_LEN 20
288 #define FLUSH_LOCAL_BUF() \
289 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
290 write(log_fd, buf, p - buf);
296 /* int main(int argc, char **argv) */
298 /* char buf[1024]; */
301 /* p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
302 /* (uint64_t)0, 0.19, 0.33, "hello!"); */
303 /* p = PACK_COMMON_END(p, 0); */
305 /* int fd = creat("out.bin", 0644); */
306 /* if (fd == -1) { */
307 /* printf("error\n"); */
311 /* write(fd, buf, p - buf); */
317 #endif /* __BIN_PROTO_H__ */