add possibility to pack 0 args in pack_args
[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         if(*t == '\0') {
78                 num = 0;
79         }
80
81         memcpy(to, &num, sizeof(num));
82         to += sizeof(num);
83
84         va_start(args, fmt);
85
86         uint8_t c;
87         uint32_t d;
88         uint64_t x;
89         uint64_t p;
90         float f;
91         double w;
92         char *s;
93         int n;
94
95         for (t = fmt; *t != '\0'; t++) {
96                 switch (*t) {
97                 case 'c':
98                         c = (uint8_t)va_arg(args, uint32_t);
99                         *to++ = *t;
100                         memcpy(to, &c, sizeof(c));
101                         to += sizeof(c);
102                         break;
103                 case 'd':
104                         d = va_arg(args, uint32_t);
105                         *to++ = *t;
106                         memcpy(to, &d, sizeof(d));
107                         to += sizeof(d);
108                         break;
109                 case 'x':
110                         x = (uint64_t)va_arg(args, uint64_t);
111                         *to++ = *t;
112                         memcpy(to, &x, sizeof(x));
113                         to += sizeof(x);
114                         break;
115                 case 'p':
116                         p = (uintptr_t)va_arg(args, uint64_t);
117                         *to++ = *t;
118                         memcpy(to, &p, sizeof(p));
119                         to += sizeof(p);
120                         break;
121                 case 'f':
122                         f = (float)va_arg(args, double);
123                         *to++ = *t;
124                         memcpy(to, &f, sizeof(f));
125                         to += sizeof(f);
126                         break;
127                 case 'w':
128                         w = va_arg(args, double);
129                         *to++ = *t;
130                         memcpy(to, &w, sizeof(w));
131                         to += sizeof(w);
132                         break;
133                 case 's':
134                         s = va_arg(args, char *);
135                         *to++ = *t;
136                         n = strlen(s) + 1;
137                         strncpy(to, s, n);
138                         to += n;
139                         break;
140                 default:
141                         break;
142                 }
143         }
144
145         va_end(args);
146
147         return to;
148 }
149
150 #define BUF_PTR p
151 #define RET_PTR ret_p
152 #define PACK_INT32(val)                         \
153         BUF_PTR = pack_int32(BUF_PTR, val);
154 #define PACK_INT64(val)                         \
155         BUF_PTR = pack_int64(BUF_PTR, val);
156 #define PACK_STRING(str)                        \
157         BUF_PTR = pack_string(BUF_PTR, str);
158
159 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...)             \
160         do {                                                    \
161                 BUF_PTR = pack_int32(BUF_PTR, msg_id);          \
162                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
163                 BUF_PTR = pack_timestamp(BUF_PTR);              \
164                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
165                 BUF_PTR = pack_int32(BUF_PTR, api_id);          \
166                 BUF_PTR = pack_int32(BUF_PTR, getpid());                \
167                 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid));    \
168                 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
169                 RET_PTR = BUF_PTR;              \
170         } while (0)
171
172 #define PACK_COMMON_END(ret, errn, intern_call)         \
173         do {                                                    \
174                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret));        \
175                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn);  \
176                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call);   \
177                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
178                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
179                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
180         } while (0)
181
182 #define PACK_RETURN_END(ret)                                                            \
183                 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
184
185 #define PACK_MEMORY(size, memory_api_type, addr)                \
186         do {                                                    \
187                 BUF_PTR = pack_int32(BUF_PTR, size);                    \
188                 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
189                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
190         } while (0)
191
192 #define PACK_UICONTROL(control)                                         \
193         do {                                                            \
194                 if (unlikely(control == NULL)) {                        \
195                         BUF_PTR = pack_string(BUF_PTR, "");             \
196                         BUF_PTR = pack_string(BUF_PTR, "");             \
197                         BUF_PTR = pack_int64(BUF_PTR, 0);               \
198                 } else {                                                \
199                         char *type = NULL, *name = NULL;                \
200                         if (find_object_hash((void*)(control),          \
201                                              &type, &name) == 1) {      \
202                                 BUF_PTR = pack_string(BUF_PTR, type);   \
203                                 BUF_PTR = pack_string(BUF_PTR, name);   \
204                         } else {                                        \
205                                 BUF_PTR = pack_string(BUF_PTR, "");     \
206                                 BUF_PTR = pack_string(BUF_PTR, "");     \
207                         }                                               \
208                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
209                 }                                                       \
210         } while(0)
211
212 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2)       \
213         do {                                                            \
214                 BUF_PTR = pack_int32(BUF_PTR, event_type);              \
215                 BUF_PTR = pack_int32(BUF_PTR, detail_type);             \
216                 BUF_PTR = pack_int32(BUF_PTR, x);                       \
217                 BUF_PTR = pack_int32(BUF_PTR, y);                       \
218                 BUF_PTR = pack_string(BUF_PTR, info1);                  \
219                 BUF_PTR = pack_int32(BUF_PTR, info2);                   \
220         } while (0)
221
222 /* #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size,       \ */
223 /*                    file_path)                                        \ */
224 /*      do {                                                            \ */
225 /*              BUF_PTR = pack_int32(BUF_PTR, size);                    \ */
226 /*              BUF_PTR = pack_int32(BUF_PTR, fd_value);                \ */
227 /*              BUF_PTR = pack_int32(BUF_PTR, fd_type);                 \ */
228 /*              BUF_PTR = pack_int32(BUF_PTR, fd_api_type);             \ */
229 /*              BUF_PTR = pack_int32(BUF_PTR, file_size);               \ */
230 /*              BUF_PTR = pack_string(BUF_PTR, file_path);              \ */
231 /*      } while (0) */
232
233 /* #define PACK_SCREENSHOT(image_file_path, orienation)                 \ */
234 /*              do {                                                    \ */
235 /*                        BUF_PTR = pack_string(BUF_PTR, image_file_path); \ */
236 /*                        BUF_PTR = pack_int32(BUF_PTR, orienation);    \ */
237 /*                } while (0) */
238
239 /* #define PACK_SCENE(scene_name, form_name, form_pointer,              \ */
240 /*                 panel_name, panel_pointer, transition_time,  \ */
241 /*                 user_transition_time)                        \ */
242 /*              do {                                                    \ */
243 /*      BUF_PTR = pack_string(BUF_PTR, scene_name);                             \ */
244 /*      BUF_PTR = pack_string(BUF_PTR, form_name);                              \ */
245 /*      BUF_PTR = pack_int64(BUF_PTR, form_pointer);                    \ */
246 /*      BUF_PTR = pack_string(BUF_PTR, panel_name);                             \ */
247 /*      BUF_PTR = pack_int64(BUF_PTR, panel_pointer);                   \ */
248 /*      BUF_PTR = pack_int32(BUF_PTR, transition_time);                 \ */
249 /*      BUF_PTR = pack_int32(BUF_PTR, user_transition_time);            \ */
250 /*              } while (0) */
251
252  #define PACK_THREAD(thread_id, thread_type, api_type)  \
253          do {                                                           \
254                 BUF_PTR = pack_int64(BUF_PTR, thread_id);               \
255                 BUF_PTR = pack_int32(BUF_PTR, thread_type);             \
256                 BUF_PTR = pack_int32(BUF_PTR, api_type);                \
257         } while (0)
258
259 /* #define PACK_CUSTOM(handle, type, name, color, value)        \ */
260 /*      BUF_PTR = pack_int32(BUF_PTR, handle);                  \ */
261 /*      BUF_PTR = pack_int32(BUF_PTR, type);                    \ */
262 /*      BUF_PTR = pack_string(BUF_PTR, name);                   \ */
263 /*      BUF_PTR = pack_int32(BUF_PTR, color);                   \ */
264 /*      BUF_PTR = pack_int32(BUF_PTR, value); */
265
266 #define PACK_SYNC(sync_val, sync_type, api_type)                     \
267         do {                                                         \
268                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val);  \
269                 BUF_PTR = pack_int32(BUF_PTR, sync_type);            \
270                 BUF_PTR = pack_int32(BUF_PTR, api_type);             \
271         } while (0)
272
273 #define LOG_PATH "/tmp/trace.bin"
274 #define OPEN_LOG()                                      \
275                 do {                                    \
276                         log_fd = creat(LOG_PATH, 0644); \
277                         if (log_fd == -1) {             \
278                                 exit(1);                \
279                         }                               \
280                 } while (0)
281 #define CLOSE_LOG()                             \
282                 do {                            \
283                         if (log_fd > 0) {       \
284                                 close(log_fd);  \
285                         }                       \
286                 } while (0)
287
288
289
290 #define LOCAL_BUF_SIZE 1024
291 #define PREPARE_LOCAL_BUF()                     \
292                 char buf[LOCAL_BUF_SIZE];       \
293                 char *p = buf;                  \
294                 char *ret_p = NULL;
295
296 #define MSG_LEN_OFFSET 16
297 #define MSG_HDR_LEN 20
298 #define FLUSH_LOCAL_BUF()                                               \
299                 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
300                 write(log_fd, buf, p - buf);
301
302 // =========================== post block macro ===========================
303
304 #define POST_PACK_PROBEBLOCK_BEGIN(LCTYPE, RETVALUE, INPUTFORMAT, ...)  \
305         newerrno = errno;                                               \
306         if(postBlockBegin(blockresult)) {
307
308 #define POST_PACK_PROBEBLOCK_END()                                      \
309                 postBlockEnd();                                         \
310         }                                                               \
311         errno = (newerrno != 0) ? newerrno : olderrno
312
313 /* data */
314 extern int log_fd;
315
316 /* int main(int argc, char **argv) */
317 /* { */
318 /*      char buf[1024]; */
319 /*      char *p = buf; */
320
321 /*      p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
322 /*                            (uint64_t)0, 0.19, 0.33, "hello!"); */
323 /*      p = PACK_COMMON_END(p, 0); */
324
325 /*      int fd = creat("out.bin", 0644); */
326 /*      if (fd == -1) { */
327 /*              printf("error\n"); */
328 /*              exit(1); */
329 /*      } */
330
331 /*      write(fd, buf, p - buf); */
332 /*      close(fd); */
333
334 /*      return 0; */
335 /* } */
336
337 #endif /* __BIN_PROTO_H__ */