Add pthread_mutex_lock (SYNC)
[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_string(char *to, const char *str)
54 {
55         size_t len = strlen(str) + 1;
56         strncpy(to, str, len);
57         return to + len;
58 }
59
60 static  char *pack_timestamp(char *to)
61 {
62         struct timeval tv;
63
64         gettimeofday(&tv, NULL);
65         to = pack_int32(to, tv.tv_sec);
66         to = pack_int32(to, tv.tv_usec * 1000);
67
68         return to;
69 }
70
71 static  char *pack_args(char *to, const char *fmt, ...)
72 {
73         va_list args;
74         uint32_t num = strlen(fmt);
75         const char *t = fmt;
76
77         memcpy(to, &num, sizeof(num));
78         to += sizeof(num);
79
80         va_start(args, fmt);
81
82         uint8_t c;
83         uint32_t d;
84         uint64_t x;
85         uint64_t p;
86         float f;
87         double w;
88         char *s;
89         int n;
90
91         for (t = fmt; *t != '\0'; t++) {
92                 switch (*t) {
93                 case 'c':
94                         c = (uint8_t)va_arg(args, uint32_t);
95                         *to++ = *t;
96                         memcpy(to, &c, sizeof(c));
97                         to += sizeof(c);
98                         break;
99                 case 'd':
100                         d = va_arg(args, uint32_t);
101                         *to++ = *t;
102                         memcpy(to, &d, sizeof(d));
103                         to += sizeof(d);
104                         break;
105                 case 'x':
106                         x = va_arg(args, uint64_t);
107                         *to++ = *t;
108                         memcpy(to, &x, sizeof(x));
109                         to += sizeof(x);
110                         break;
111                 case 'p':
112                         p = va_arg(args, uint64_t);
113                         *to++ = *t;
114                         memcpy(to, &p, sizeof(p));
115                         to += sizeof(p);
116                         break;
117                 case 'f':
118                         f = (float)va_arg(args, double);
119                         *to++ = *t;
120                         memcpy(to, &f, sizeof(f));
121                         to += sizeof(f);
122                         break;
123                 case 'w':
124                         w = va_arg(args, double);
125                         *to++ = *t;
126                         memcpy(to, &w, sizeof(w));
127                         to += sizeof(w);
128                         break;
129                 case 's':
130                         s = va_arg(args, char *);
131                         *to++ = *t;
132                         n = strlen(s) + 1;
133                         strncpy(to, s, n);
134                         to += n;
135                         break;
136                 default:
137                         break;
138                 }
139         }
140
141         va_end(args);
142
143         return to;
144 }
145
146 #define BUF_PTR p
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);
153
154 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...)             \
155         do {                                                    \
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__); \
164         } while (0)
165
166 #define PACK_COMMON_END(ret, pc, errn)                          \
167         do {                                                    \
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);               \
175         } while (0)
176
177 #define PACK_MEMORY(size, memory_api_type, addr)                \
178         do {                                                    \
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);       \
182         } while (0)
183
184 #define PACK_UICONTROL(control)                                         \
185         do {                                                            \
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);               \
190                 } else {                                                \
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);   \
196                         } else {                                        \
197                                 BUF_PTR = pack_string(BUF_PTR, "");     \
198                                 BUF_PTR = pack_string(BUF_PTR, "");     \
199                         }                                               \
200                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
201                 }                                                       \
202         } while(0)
203
204 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2)       \
205         do {                                                            \
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);                   \
212         } while (0)
213
214 /* #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size,       \ */
215 /*                    file_path)                                        \ */
216 /*      do {                                                            \ */
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);              \ */
223 /*      } while (0) */
224
225 /* #define PACK_SCREENSHOT(image_file_path, orienation)                 \ */
226 /*              do {                                                    \ */
227 /*                        BUF_PTR = pack_string(BUF_PTR, image_file_path); \ */
228 /*                        BUF_PTR = pack_int32(BUF_PTR, orienation);    \ */
229 /*                } while (0) */
230
231 /* #define PACK_SCENE(scene_name, form_name, form_pointer,              \ */
232 /*                 panel_name, panel_pointer, transition_time,  \ */
233 /*                 user_transition_time)                        \ */
234 /*              do {                                                    \ */
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);            \ */
242 /*              } while (0) */
243
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); */
249
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); */
256
257 #define PACK_SYNC(sync_val, sync_type, api_type)                     \
258         do {                                                 \
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);             \
262         } while (0)
263
264 #define LOG_PATH "/tmp/trace.bin"
265 #define OPEN_LOG()                                      \
266                 do {                                    \
267                         log_fd = creat(LOG_PATH, 0644); \
268                         if (log_fd == -1) {             \
269                                 exit(1);                \
270                         }                               \
271                 } while (0)
272 #define CLOSE_LOG()                             \
273                 do {                            \
274                         if (log_fd > 0) {       \
275                                 close(log_fd);  \
276                         }                       \
277                 } while (0)
278
279
280
281 #define LOCAL_BUF_SIZE 1024
282 #define PREPARE_LOCAL_BUF()                     \
283                 char buf[LOCAL_BUF_SIZE];       \
284                 char *p = buf;
285
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);
291
292
293 /* data */
294 extern int log_fd;
295
296 /* int main(int argc, char **argv) */
297 /* { */
298 /*      char buf[1024]; */
299 /*      char *p = buf; */
300
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); */
304
305 /*      int fd = creat("out.bin", 0644); */
306 /*      if (fd == -1) { */
307 /*              printf("error\n"); */
308 /*              exit(1); */
309 /*      } */
310
311 /*      write(fd, buf, p - buf); */
312 /*      close(fd); */
313
314 /*      return 0; */
315 /* } */
316
317 #endif /* __BIN_PROTO_H__ */