MSG_PROBE_RESOURCE: change fd type from int32 to int64
[platform/core/system/swap-probe.git] / include / binproto.h
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  *
8  * Nikita Kalyazin <n.kalyazin@samsung.com>
9  * Anastasia Lyupa <a.lyupa@samsung.com>
10  * 
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the
13  * Free Software Foundation; either version 2.1 of the License, or (at your option)
14  * any later version.
15  * 
16  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19  * License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this library; if not, write to the Free Software Foundation, Inc., 51
23  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  *
25  * Contributors:
26  * - Samsung RnD Institute Russia
27  * 
28  */
29
30 #ifndef __BIN_PROTO_H__
31 #define __BIN_PROTO_H__
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38
39 #include <unistd.h>
40 #include <fcntl.h>
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44
45 // getpid/gettid
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <sys/syscall.h>
49
50 #include <sys/time.h>
51
52 #include <sys/socket.h>
53 #include "dahelper.h"
54 #include "api_id_mapping.h"
55
56 #define MSG_PROBE_MEMORY 0x3001
57 #define MSG_PROBE_UICONTROL 0x3002
58 #define MSG_PROBE_UIEVENT 0x3003
59 #define MSG_PROBE_RESOURCE 0x3004
60 #define MSG_PROBE_LIFECYCLE 0x3005
61 #define MSG_PROBE_SCREENSHOT 0x3006
62 #define MSG_PROBE_SCENE 0x3007
63 #define MSG_PROBE_THREAD 0x3008
64 #define MSG_PROBE_CUSTOM 0x3009
65 #define MSG_PROBE_SYNC 0x3010
66
67 // TODO: remove this copy-paste
68 #define CALLER_ADDRESS                                                  \
69         ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
70
71
72
73 // COMMON
74 static inline char *pack_int32(char *to, uint32_t val)
75 {
76         *(uint32_t *)to = val;
77         return to + sizeof(uint32_t);
78 }
79
80 static inline char *pack_int64(char *to, uint64_t val)
81 {
82         *(uint64_t *)to = val;
83         return to + sizeof(uint64_t);
84 }
85
86 static inline char *pack_string(char *to, const char *str)
87 {
88         size_t len = strlen(str) + 1;
89         strncpy(to, str, len);
90         return to + len;
91 }
92
93 static inline char *pack_double(char *to, double val)
94 {
95         *(double *)to = val;
96         return to + sizeof(double);
97 }
98
99 static inline char *pack_timestamp(char *to)
100 {
101         struct timeval tv;
102
103         gettimeofday(&tv, NULL);
104         to = pack_int32(to, tv.tv_sec);
105         to = pack_int32(to, tv.tv_usec * 1000);
106
107         return to;
108 }
109
110 static inline char *pack_args(char *to, const char *fmt, ...)
111 {
112         va_list args;
113         uint32_t num = strlen(fmt);
114         const char *t = fmt;
115
116         if(*t == '\0') {
117                 num = 0;
118         }
119
120         memcpy(to, &num, sizeof(num));
121         to += sizeof(num);
122
123         va_start(args, fmt);
124
125         uint8_t c;
126         uint32_t d;
127         uint64_t x;
128         uint64_t p;
129         float f;
130         double w;
131         char *s;
132         int n;
133
134         for (t = fmt; *t != '\0'; t++) {
135                 switch (*t) {
136                 case 'c':
137                         c = (uint8_t)va_arg(args, uint32_t);
138                         *to++ = *t;
139                         memcpy(to, &c, sizeof(c));
140                         to += sizeof(c);
141                         break;
142                 case 'd':
143                         d = va_arg(args, uint32_t);
144                         *to++ = *t;
145                         memcpy(to, &d, sizeof(d));
146                         to += sizeof(d);
147                         break;
148                 case 'x':
149                         x = (uint64_t)va_arg(args, uint64_t);
150                         *to++ = *t;
151                         memcpy(to, &x, sizeof(x));
152                         to += sizeof(x);
153                         break;
154                 case 'p':
155                         p = (uintptr_t)va_arg(args, uint64_t);
156                         *to++ = *t;
157                         memcpy(to, &p, sizeof(p));
158                         to += sizeof(p);
159                         break;
160                 case 'f':
161                         f = (float)va_arg(args, double);
162                         *to++ = *t;
163                         memcpy(to, &f, sizeof(f));
164                         to += sizeof(f);
165                         break;
166                 case 'w':
167                         w = va_arg(args, double);
168                         *to++ = *t;
169                         memcpy(to, &w, sizeof(w));
170                         to += sizeof(w);
171                         break;
172                 case 's':
173                         s = va_arg(args, char *);
174                         *to++ = *t;
175                         n = strlen(s) + 1;
176                         strncpy(to, s, n);
177                         to += n;
178                         break;
179                 default:
180                         break;
181                 }
182         }
183
184         va_end(args);
185
186         return to;
187 }
188
189 #define BUF_PTR p
190 #define RET_PTR ret_p
191 #define PACK_INT32(val)                         \
192         BUF_PTR = pack_int32(BUF_PTR, val);
193 #define PACK_INT64(val)                         \
194         BUF_PTR = pack_int64(BUF_PTR, val);
195 #define PACK_STRING(str)                        \
196         BUF_PTR = pack_string(BUF_PTR, str);
197
198 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...)             \
199         do {                                                    \
200                 BUF_PTR = pack_int32(BUF_PTR, msg_id);          \
201                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
202                 BUF_PTR = pack_timestamp(BUF_PTR);              \
203                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
204                 BUF_PTR = pack_int32(BUF_PTR, api_id);          \
205                 BUF_PTR = pack_int32(BUF_PTR, getpid());                \
206                 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid));    \
207                 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
208                 RET_PTR = BUF_PTR;              \
209         } while (0)
210
211 #define PACK_COMMON_END(ret, errn, intern_call)         \
212         do {                                                    \
213                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret));        \
214                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn);  \
215                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call);   \
216                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
217                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
218                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
219         } while (0)
220
221 #define PACK_RETURN_END(ret)                                                            \
222                 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
223
224 #define PACK_MEMORY(size, memory_api_type, addr)                \
225         do {                                                    \
226                 BUF_PTR = pack_int64(BUF_PTR, size);                    \
227                 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
228                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
229         } while (0)
230
231 #define PACK_UICONTROL(control)                                         \
232         do {                                                            \
233                 if (unlikely(control == NULL)) {                        \
234                         BUF_PTR = pack_string(BUF_PTR, "");             \
235                         BUF_PTR = pack_string(BUF_PTR, "");             \
236                         BUF_PTR = pack_int64(BUF_PTR, 0);               \
237                 } else {                                                \
238                         char *type = NULL, *name = NULL;                \
239                         if (find_object_hash((void*)(control),          \
240                                              &type, &name) == 1) {      \
241                                 BUF_PTR = pack_string(BUF_PTR, type);   \
242                                 BUF_PTR = pack_string(BUF_PTR, name);   \
243                         } else {                                        \
244                                 BUF_PTR = pack_string(BUF_PTR, "");     \
245                                 BUF_PTR = pack_string(BUF_PTR, "");     \
246                         }                                               \
247                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
248                 }                                                       \
249         } while(0)
250
251 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2)       \
252         do {                                                            \
253                 BUF_PTR = pack_int32(BUF_PTR, event_type);              \
254                 BUF_PTR = pack_int32(BUF_PTR, detail_type);             \
255                 BUF_PTR = pack_int32(BUF_PTR, x);                       \
256                 BUF_PTR = pack_int32(BUF_PTR, y);                       \
257                 BUF_PTR = pack_string(BUF_PTR, info1);                  \
258                 BUF_PTR = pack_int32(BUF_PTR, info2);                   \
259         } while (0)
260
261 #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size,  \
262                       file_path)                                        \
263         do {                                                            \
264                 BUF_PTR = pack_int64(BUF_PTR, size);                    \
265                 BUF_PTR = pack_int64(BUF_PTR, fd_value);                \
266                 BUF_PTR = pack_int32(BUF_PTR, fd_type);                 \
267                 BUF_PTR = pack_int32(BUF_PTR, fd_api_type);             \
268                 BUF_PTR = pack_int64(BUF_PTR, file_size);               \
269                 BUF_PTR = pack_string(BUF_PTR, file_path);              \
270         } while (0)
271
272 #define PACK_SCREENSHOT(image_file_path, orientation)                           \
273                 do {                                                            \
274                           BUF_PTR = pack_string(BUF_PTR, image_file_path);      \
275                           BUF_PTR = pack_int32(BUF_PTR, orientation);           \
276                 } while (0)
277
278 #define PACK_SCENE(scene_name, formid, pform, panelid, ppanel, transition, user)        \
279         do {                                                                            \
280                 BUF_PTR = pack_string(BUF_PTR,  scene_name);                            \
281                 if (unlikely(pform == NULL)) {                                          \
282                         BUF_PTR = pack_string(BUF_PTR, "");                             \
283                         BUF_PTR = pack_int64(BUF_PTR, 0);                               \
284                 } else {                                                                \
285                         char *type = NULL, *name = NULL;                                \
286                         if (find_object_hash((void*)(pform), &type, &name) == 1) {      \
287                                 BUF_PTR = pack_string(BUF_PTR, name);                   \
288                         } else {                                                        \
289                                 BUF_PTR = pack_string(BUF_PTR, "");                     \
290                         }                                                               \
291                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)pform);                \
292                 }                                                                       \
293                 if (unlikely(ppanel == NULL)) {                                         \
294                         BUF_PTR = pack_string(BUF_PTR, "");                             \
295                         BUF_PTR = pack_int64(BUF_PTR, 0);                               \
296                 } else {                                                                \
297                         char *type = NULL, *name = NULL;                                \
298                         if (find_object_hash((void*)(ppanel), &type, &name) == 1) {     \
299                                 BUF_PTR = pack_string(BUF_PTR, name);                   \
300                         } else {                                                        \
301                                 BUF_PTR = pack_string(BUF_PTR, "");                     \
302                         }                                                               \
303                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ppanel));             \
304                 }                                                                       \
305                 BUF_PTR = pack_int64(BUF_PTR, transition);                              \
306                 BUF_PTR = pack_int64(BUF_PTR, user);                                    \
307         } while(0)
308
309  #define PACK_THREAD(thread_id, thread_type, api_type)  \
310          do {                                                           \
311                 BUF_PTR = pack_int64(BUF_PTR, thread_id);               \
312                 BUF_PTR = pack_int32(BUF_PTR, thread_type);             \
313                 BUF_PTR = pack_int32(BUF_PTR, api_type);                \
314         } while (0)
315
316 #define PACK_CUSTOM(handle, type, name, color, value)           \
317         do {                                                    \
318                 BUF_PTR = pack_int32(BUF_PTR, handle);          \
319                 BUF_PTR = pack_int32(BUF_PTR, type);            \
320                 BUF_PTR = pack_string(BUF_PTR, name);           \
321                 BUF_PTR = pack_int32(BUF_PTR, color);           \
322                 BUF_PTR = pack_double(BUF_PTR, value);          \
323         } while (0)
324
325 #define PACK_SYNC(sync_val, sync_type, api_type)                     \
326         do {                                                         \
327                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val);  \
328                 BUF_PTR = pack_int32(BUF_PTR, sync_type);            \
329                 BUF_PTR = pack_int32(BUF_PTR, api_type);             \
330         } while (0)
331
332 #define LOCAL_BUF_SIZE 1024
333 #define PREPARE_LOCAL_BUF()                     \
334                 char buf[LOCAL_BUF_SIZE];       \
335                 char *p = buf;                  \
336                 char *ret_p = NULL;
337
338 #define MSG_LEN_OFFSET 16
339 #define MSG_HDR_LEN 20
340 #define FLUSH_LOCAL_BUF()                                               \
341                 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
342                 send(gTraceInfo.socket.daemonSock, buf, (p - buf), 0);
343
344 // =========================== post block macro ===========================
345
346 #define POST_PACK_PROBEBLOCK_BEGIN()                                    \
347         newerrno = errno;                                               \
348         if(postBlockBegin(blockresult)) {
349
350 #define POST_PACK_PROBEBLOCK_END()                                      \
351                 postBlockEnd();                                         \
352         }                                                               \
353         errno = (newerrno != 0) ? newerrno : olderrno
354
355 /* int main(int argc, char **argv) */
356 /* { */
357 /*      char buf[1024]; */
358 /*      char *p = buf; */
359
360 /*      p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
361 /*                            (uint64_t)0, 0.19, 0.33, "hello!"); */
362 /*      p = PACK_COMMON_END(p, 0); */
363
364 /*      int fd = creat("out.bin", 0644); */
365 /*      if (fd == -1) { */
366 /*              printf("error\n"); */
367 /*              exit(1); */
368 /*      } */
369
370 /*      write(fd, buf, p - buf); */
371 /*      close(fd); */
372
373 /*      return 0; */
374 /* } */
375
376 #endif /* __BIN_PROTO_H__ */