bf469eecc1db1838fbc23d5b85f857158d1f53ad
[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         /* printf("num = %d\n", num); */
100
101         memcpy(to, &num, sizeof(num));
102         to += sizeof(num);
103
104         memcpy(to, fmt, num);
105         to += num;
106
107         va_start(args, fmt);
108
109         uint8_t c;
110         uint32_t d;
111         uint64_t x;
112         uint64_t p;
113         float f;
114         double w;
115         char *s;
116         int n;
117
118         for (t = fmt; *t != '\0'; t++) {
119                 switch (*t) {
120                 case 'c':
121                         c = (uint8_t)va_arg(args, uint32_t);
122                         /* printf("char = %c\n", c); */
123                         memcpy(to, &c, sizeof(c));
124                         to += sizeof(c);
125                         break;
126                 case 'd':
127                         d = va_arg(args, uint32_t);
128                         /* printf("int = %d\n", d); */
129                         memcpy(to, &d, sizeof(d));
130                         to += sizeof(d);
131                         break;
132                 case 'x':
133                         x = va_arg(args, uint64_t);
134                         /* printf("long = %lld\n", x); */
135                         memcpy(to, &x, sizeof(x));
136                         to += sizeof(x);
137                         break;
138                 case 'p':
139                         p = va_arg(args, uint64_t);
140                         /* printf("pointer = %p\n", (void *)p); */
141                         memcpy(to, &p, sizeof(p));
142                         to += sizeof(p);
143                         break;
144                 case 'f':
145                         f = (float)va_arg(args, double);
146                         /* printf("float = %f\n", f); */
147                         memcpy(to, &f, sizeof(f));
148                         to += sizeof(f);
149                         break;
150                 case 'w':
151                         w = va_arg(args, double);
152                         /* printf("double = %e\n", w); */
153                         memcpy(to, &w, sizeof(w));
154                         to += sizeof(w);
155                         break;
156                 case 's':
157                         s = va_arg(args, char *);
158                         /* printf("string = %s\n", s); */
159                         n = strlen(s) + 1;
160                         strncpy(to, s, n);
161                         to += n;
162                         break;
163                 default:
164                         break;
165                 }
166         }
167
168         va_end(args);
169
170         return to;
171 }
172
173 static  char *pack_pid(char *to)
174 {
175         return pack_int32(to, getpid());
176 }
177
178 static  char *pack_tid(char *to)
179 {
180         return pack_int32(to, syscall(__NR_gettid));
181 }
182
183 static  char *pack_return(char *to, uint64_t ret)
184 {
185         return pack_int64(to, ret);
186 }
187
188 static  char *pack_pc(char *to, uint64_t pc)
189 {
190         return pack_int64(to, pc);
191 }
192
193 static  char *pack_errno(char *to, uint32_t en)
194 {
195         return pack_int32(to, en);
196 }
197
198 static  char *pack_internal_call(char *to)
199 {
200         // TODO: where to get int call?
201         return pack_int32(to, 0);
202 }
203
204 static  char *pack_caller_pc(char *to)
205 {
206         return pack_int64(to, (uintptr_t)CALLER_ADDRESS);
207 }
208
209
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);                   \
216         to = pack_pid(to);                              \
217         to = pack_tid(to);                              \
218         to = pack_args(to, fmt, __VA_ARGS__)
219
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);
228
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);
233
234 #define PACK_UICONTROL(to, parent_name, parent_class_name,              \
235                        parent_pointer, child_name, child_class_name,    \
236                        child_pointer) \
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);
243
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);
251
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);
260
261 #define PACK_LIFECYCLE(to)
262
263 #define PACK_SCREENSHOT(to, image_file_path, orienation) \
264         to = pack_string(to, image_file_path); \
265         to = pack_int32(to, orienation);
266
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);
277
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);
283
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);
290
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);
295
296 #define LOG_PATH "/tmp/trace.bin"
297 #define OPEN_LOG()                              \
298         log_fd = creat(LOG_PATH, 0644);         \
299         if (log_fd == -1) {                     \
300                 exit(1);                        \
301         }
302
303 #define CLOSE_LOG()                             \
304         if (log_fd > 0) {                       \
305                 close(log_fd);                  \
306         }
307
308
309 #define LOCAL_BUF_SIZE 1024
310 #define PREPARE_LOCAL_BUF()                     \
311         char buf[LOCAL_BUF_SIZE];               \
312         char *p = buf;
313
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);
319
320
321 /* data */
322 extern int log_fd;
323
324 /* int main(int argc, char **argv) */
325 /* { */
326 /*      char buf[1024]; */
327 /*      char *p = buf; */
328
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); */
332
333 /*      int fd = creat("out.bin", 0644); */
334 /*      if (fd == -1) { */
335 /*              printf("error\n"); */
336 /*              exit(1); */
337 /*      } */
338
339 /*      write(fd, buf, p - buf); */
340 /*      close(fd); */
341
342 /*      return 0; */
343 /* } */
344
345 #endif /* __BIN_PROTO_H__ */