Add probe to ecore_event_evas_mouse_move
[platform/core/system/swap-probe.git] / include / binproto.h
1 #ifndef __BIN_PROTO_H__
2 #define __BIN_PROTO_H__
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdint.h>
8 #include <stdarg.h>
9
10 #include <unistd.h>
11 #include <fcntl.h>
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15
16 // getpid/gettid
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <sys/syscall.h>
20
21 #include <sys/time.h>
22
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
33
34 // TODO: remove this copy-paste
35 #define CALLER_ADDRESS  \
36         ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
37
38
39
40 // COMMON
41 static  char *pack_int32(char *to, uint32_t val)
42 {
43         *(uint32_t *)to = val;
44         return to + sizeof(uint32_t);
45 }
46
47 static  char *pack_int64(char *to, uint64_t val)
48 {
49         *(uint64_t *)to = val;
50         return to + sizeof(uint64_t);
51 }
52
53 static  char *pack_addr(char *to, unsigned long addr)
54 {
55         return pack_int64(to, (intptr_t)addr);
56 }
57
58 static  char *pack_string(char *to, const char *str)
59 {
60         size_t len = strlen(str) + 1;
61         strncpy(to, str, len);
62         return to + len;
63 }
64
65
66 static  char *pack_msg_id(char *to, uint32_t msg_id)
67 {
68         return pack_int32(to, msg_id);
69 }
70
71 static  char *pack_seq_num(char *to)
72 {
73         // TODO: get seq num
74         return pack_int32(to, 0);
75 }
76
77 static  char *pack_timestamp(char *to)
78 {
79         struct timeval tv;
80
81         gettimeofday(&tv, NULL);
82         to = pack_int32(to, tv.tv_sec);
83         to = pack_int32(to, tv.tv_usec * 1000);
84
85         return to;
86 }
87
88 static  char *pack_api_id(char *to, uint32_t api_id)
89 {
90         return pack_int32(to, api_id);
91 }
92
93 static  char *pack_args(char *to, const char *fmt, ...)
94 {
95         va_list args;
96         uint32_t num = strlen(fmt);
97         const char *t = fmt;
98
99         memcpy(to, &num, sizeof(num));
100         to += sizeof(num);
101
102         va_start(args, fmt);
103
104         uint8_t c;
105         uint32_t d;
106         uint64_t x;
107         uint64_t p;
108         float f;
109         double w;
110         char *s;
111         int n;
112
113         for (t = fmt; *t != '\0'; t++) {
114                 switch (*t) {
115                 case 'c':
116                         c = (uint8_t)va_arg(args, uint32_t);
117                         *to++ = *t;
118                         memcpy(to, &c, sizeof(c));
119                         to += sizeof(c);
120                         break;
121                 case 'd':
122                         d = va_arg(args, uint32_t);
123                         *to++ = *t;
124                         memcpy(to, &d, sizeof(d));
125                         to += sizeof(d);
126                         break;
127                 case 'x':
128                         x = va_arg(args, uint64_t);
129                         *to++ = *t;
130                         memcpy(to, &x, sizeof(x));
131                         to += sizeof(x);
132                         break;
133                 case 'p':
134                         p = va_arg(args, uint64_t);
135                         *to++ = *t;
136                         memcpy(to, &p, sizeof(p));
137                         to += sizeof(p);
138                         break;
139                 case 'f':
140                         f = (float)va_arg(args, double);
141                         *to++ = *t;
142                         memcpy(to, &f, sizeof(f));
143                         to += sizeof(f);
144                         break;
145                 case 'w':
146                         w = va_arg(args, double);
147                         *to++ = *t;
148                         memcpy(to, &w, sizeof(w));
149                         to += sizeof(w);
150                         break;
151                 case 's':
152                         s = va_arg(args, char *);
153                         *to++ = *t;
154                         n = strlen(s) + 1;
155                         strncpy(to, s, n);
156                         to += n;
157                         break;
158                 default:
159                         break;
160                 }
161         }
162
163         va_end(args);
164
165         return to;
166 }
167
168 static  char *pack_pid(char *to)
169 {
170         return pack_int32(to, getpid());
171 }
172
173 static  char *pack_tid(char *to)
174 {
175         return pack_int32(to, syscall(__NR_gettid));
176 }
177
178 static  char *pack_return(char *to, uint64_t ret)
179 {
180         return pack_int64(to, ret);
181 }
182
183 static  char *pack_pc(char *to, uint64_t pc)
184 {
185         return pack_int64(to, pc);
186 }
187
188 static  char *pack_errno(char *to, uint32_t en)
189 {
190         return pack_int32(to, en);
191 }
192
193 static  char *pack_internal_call(char *to)
194 {
195         // TODO: where to get int call?
196         return pack_int32(to, 0);
197 }
198
199 static  char *pack_caller_pc(char *to)
200 {
201         return pack_int64(to, (uintptr_t)CALLER_ADDRESS);
202 }
203
204
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);                   \
211         to = pack_pid(to);                              \
212         to = pack_tid(to);                              \
213         to = pack_args(to, fmt, __VA_ARGS__)
214
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);
223
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);
228
229 #define PACK_UICONTROL(to, parent_name, parent_class_name,              \
230                        parent_pointer, child_name, child_class_name,    \
231                        child_pointer) \
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);
238
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);
246
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);
255
256 #define PACK_LIFECYCLE(to)
257
258 #define PACK_SCREENSHOT(to, image_file_path, orienation) \
259         to = pack_string(to, image_file_path); \
260         to = pack_int32(to, orienation);
261
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);
272
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);
278
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);
285
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);
290
291 #define LOG_PATH "/tmp/trace.bin"
292 #define OPEN_LOG()                              \
293         log_fd = creat(LOG_PATH, 0644);         \
294         if (log_fd == -1) {                     \
295                 exit(1);                        \
296         }
297
298 #define CLOSE_LOG()                             \
299         if (log_fd > 0) {                       \
300                 close(log_fd);                  \
301         }
302
303
304 #define LOCAL_BUF_SIZE 1024
305 #define PREPARE_LOCAL_BUF()                     \
306         char buf[LOCAL_BUF_SIZE];               \
307         char *p = buf;
308
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);
314
315
316 /* data */
317 extern int log_fd;
318
319 /* int main(int argc, char **argv) */
320 /* { */
321 /*      char buf[1024]; */
322 /*      char *p = buf; */
323
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); */
327
328 /*      int fd = creat("out.bin", 0644); */
329 /*      if (fd == -1) { */
330 /*              printf("error\n"); */
331 /*              exit(1); */
332 /*      } */
333
334 /*      write(fd, buf, p - buf); */
335 /*      close(fd); */
336
337 /*      return 0; */
338 /* } */
339
340 #endif /* __BIN_PROTO_H__ */