add post block macros without any APPEND_LOG calls
[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 inline char *pack_int32(char *to, uint32_t val)
42 {
43         *(uint32_t *)to = val;
44         return to + sizeof(uint32_t);
45 }
46
47 static inline char *pack_int64(char *to, uint64_t val)
48 {
49         *(uint64_t *)to = val;
50         return to + sizeof(uint64_t);
51 }
52
53 static inline 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 inline 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 inline 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 = (uint64_t)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 = (uintptr_t)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 RET_PTR ret_p
148 #define PACK_INT32(val)                         \
149         BUF_PTR = pack_int32(BUF_PTR, val);
150 #define PACK_INT64(val)                         \
151         BUF_PTR = pack_int64(BUF_PTR, val);
152 #define PACK_STRING(str)                        \
153         BUF_PTR = pack_string(BUF_PTR, str);
154
155 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...)             \
156         do {                                                    \
157                 BUF_PTR = pack_int32(BUF_PTR, msg_id);          \
158                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
159                 BUF_PTR = pack_timestamp(BUF_PTR);              \
160                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
161                 BUF_PTR = pack_int32(BUF_PTR, api_id);          \
162                 BUF_PTR = pack_int32(BUF_PTR, getpid());                \
163                 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid));    \
164                 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
165                 RET_PTR = BUF_PTR;              \
166         } while (0)
167
168 #define PACK_COMMON_END(ret, errn, intern_call)         \
169         do {                                                    \
170                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret));        \
171                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn);  \
172                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call);   \
173                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
174                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
175                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
176         } while (0)
177
178 #define PACK_RETURN_END(ret)                                                            \
179                 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
180
181 #define PACK_MEMORY(size, memory_api_type, addr)                \
182         do {                                                    \
183                 BUF_PTR = pack_int32(BUF_PTR, size);                    \
184                 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
185                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
186         } while (0)
187
188 #define PACK_UICONTROL(control)                                         \
189         do {                                                            \
190                 if (unlikely(control == NULL)) {                        \
191                         BUF_PTR = pack_string(BUF_PTR, "");             \
192                         BUF_PTR = pack_string(BUF_PTR, "");             \
193                         BUF_PTR = pack_int64(BUF_PTR, 0);               \
194                 } else {                                                \
195                         char *type = NULL, *name = NULL;                \
196                         if (find_object_hash((void*)(control),          \
197                                              &type, &name) == 1) {      \
198                                 BUF_PTR = pack_string(BUF_PTR, type);   \
199                                 BUF_PTR = pack_string(BUF_PTR, name);   \
200                         } else {                                        \
201                                 BUF_PTR = pack_string(BUF_PTR, "");     \
202                                 BUF_PTR = pack_string(BUF_PTR, "");     \
203                         }                                               \
204                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
205                 }                                                       \
206         } while(0)
207
208 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2)       \
209         do {                                                            \
210                 BUF_PTR = pack_int32(BUF_PTR, event_type);              \
211                 BUF_PTR = pack_int32(BUF_PTR, detail_type);             \
212                 BUF_PTR = pack_int32(BUF_PTR, x);                       \
213                 BUF_PTR = pack_int32(BUF_PTR, y);                       \
214                 BUF_PTR = pack_string(BUF_PTR, info1);                  \
215                 BUF_PTR = pack_int32(BUF_PTR, info2);                   \
216         } while (0)
217
218 /* #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size,       \ */
219 /*                    file_path)                                        \ */
220 /*      do {                                                            \ */
221 /*              BUF_PTR = pack_int32(BUF_PTR, size);                    \ */
222 /*              BUF_PTR = pack_int32(BUF_PTR, fd_value);                \ */
223 /*              BUF_PTR = pack_int32(BUF_PTR, fd_type);                 \ */
224 /*              BUF_PTR = pack_int32(BUF_PTR, fd_api_type);             \ */
225 /*              BUF_PTR = pack_int32(BUF_PTR, file_size);               \ */
226 /*              BUF_PTR = pack_string(BUF_PTR, file_path);              \ */
227 /*      } while (0) */
228
229 /* #define PACK_SCREENSHOT(image_file_path, orienation)                 \ */
230 /*              do {                                                    \ */
231 /*                        BUF_PTR = pack_string(BUF_PTR, image_file_path); \ */
232 /*                        BUF_PTR = pack_int32(BUF_PTR, orienation);    \ */
233 /*                } while (0) */
234
235 /* #define PACK_SCENE(scene_name, form_name, form_pointer,              \ */
236 /*                 panel_name, panel_pointer, transition_time,  \ */
237 /*                 user_transition_time)                        \ */
238 /*              do {                                                    \ */
239 /*      BUF_PTR = pack_string(BUF_PTR, scene_name);                             \ */
240 /*      BUF_PTR = pack_string(BUF_PTR, form_name);                              \ */
241 /*      BUF_PTR = pack_int64(BUF_PTR, form_pointer);                    \ */
242 /*      BUF_PTR = pack_string(BUF_PTR, panel_name);                             \ */
243 /*      BUF_PTR = pack_int64(BUF_PTR, panel_pointer);                   \ */
244 /*      BUF_PTR = pack_int32(BUF_PTR, transition_time);                 \ */
245 /*      BUF_PTR = pack_int32(BUF_PTR, user_transition_time);            \ */
246 /*              } while (0) */
247
248 /* #define PACK_THREAD(BUF_PTRthread_id, osp_thread_id, thread_type, api_type)  \ */
249 /*      BUF_PTR = pack_int32(BUF_PTR, pthread_id);                                      \ */
250 /*      BUF_PTR = pack_int32(BUF_PTR, osp_thread_id);                           \ */
251 /*      BUF_PTR = pack_int32(BUF_PTR, thread_type);                                     \ */
252 /*      BUF_PTR = pack_int32(BUF_PTR, api_type); */
253
254 /* #define PACK_CUSTOM(handle, type, name, color, value)        \ */
255 /*      BUF_PTR = pack_int32(BUF_PTR, handle);                  \ */
256 /*      BUF_PTR = pack_int32(BUF_PTR, type);                    \ */
257 /*      BUF_PTR = pack_string(BUF_PTR, name);                   \ */
258 /*      BUF_PTR = pack_int32(BUF_PTR, color);                   \ */
259 /*      BUF_PTR = pack_int32(BUF_PTR, value); */
260
261 #define PACK_SYNC(sync_val, sync_type, api_type)                     \
262         do {                                                         \
263                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val);  \
264                 BUF_PTR = pack_int32(BUF_PTR, sync_type);            \
265                 BUF_PTR = pack_int32(BUF_PTR, api_type);             \
266         } while (0)
267
268 #define LOG_PATH "/tmp/trace.bin"
269 #define OPEN_LOG()                                      \
270                 do {                                    \
271                         log_fd = creat(LOG_PATH, 0644); \
272                         if (log_fd == -1) {             \
273                                 exit(1);                \
274                         }                               \
275                 } while (0)
276 #define CLOSE_LOG()                             \
277                 do {                            \
278                         if (log_fd > 0) {       \
279                                 close(log_fd);  \
280                         }                       \
281                 } while (0)
282
283
284
285 #define LOCAL_BUF_SIZE 1024
286 #define PREPARE_LOCAL_BUF()                     \
287                 char buf[LOCAL_BUF_SIZE];       \
288                 char *p = buf;                  \
289                 char *ret_p = NULL;
290
291 #define MSG_LEN_OFFSET 16
292 #define MSG_HDR_LEN 20
293 #define FLUSH_LOCAL_BUF()                                               \
294                 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
295                 write(log_fd, buf, p - buf);
296
297 // =========================== post block macro ===========================
298
299 #define POST_PACK_PROBEBLOCK_BEGIN(LCTYPE, RETVALUE, INPUTFORMAT, ...)  \
300         newerrno = errno;                                               \
301         if(postBlockBegin(blockresult)) {
302
303 #define POST_PACK_PROBEBLOCK_END()                                      \
304                 postBlockEnd();                                         \
305         }                                                               \
306         errno = (newerrno != 0) ? newerrno : olderrno
307
308 /* data */
309 extern int log_fd;
310
311 /* int main(int argc, char **argv) */
312 /* { */
313 /*      char buf[1024]; */
314 /*      char *p = buf; */
315
316 /*      p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
317 /*                            (uint64_t)0, 0.19, 0.33, "hello!"); */
318 /*      p = PACK_COMMON_END(p, 0); */
319
320 /*      int fd = creat("out.bin", 0644); */
321 /*      if (fd == -1) { */
322 /*              printf("error\n"); */
323 /*              exit(1); */
324 /*      } */
325
326 /*      write(fd, buf, p - buf); */
327 /*      close(fd); */
328
329 /*      return 0; */
330 /* } */
331
332 #endif /* __BIN_PROTO_H__ */