change Copyright headers, change line endings to Unix format
[platform/core/system/swap-probe.git] / include / binproto.h
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2011 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
55 #define MSG_PROBE_MEMORY 0x3001
56 #define MSG_PROBE_UICONTROL 0x3002
57 #define MSG_PROBE_UIEVENT 0x3003
58 #define MSG_PROBE_RESOURCE 0x3004
59 #define MSG_PROBE_LIFECYCLE 0x3005
60 #define MSG_PROBE_SCREENSHOT 0x3006
61 #define MSG_PROBE_SCENE 0x3007
62 #define MSG_PROBE_THREAD 0x3008
63 #define MSG_PROBE_CUSTOM 0x3009
64 #define MSG_PROBE_SYNC 0x3010
65
66 // TODO: remove this copy-paste
67 #define CALLER_ADDRESS                                                  \
68         ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
69
70
71
72 // COMMON
73 static inline char *pack_int32(char *to, uint32_t val)
74 {
75         *(uint32_t *)to = val;
76         return to + sizeof(uint32_t);
77 }
78
79 static inline char *pack_int64(char *to, uint64_t val)
80 {
81         *(uint64_t *)to = val;
82         return to + sizeof(uint64_t);
83 }
84
85 static inline char *pack_string(char *to, const char *str)
86 {
87         size_t len = strlen(str) + 1;
88         strncpy(to, str, len);
89         return to + len;
90 }
91
92 static inline char *pack_double(char *to, double val)
93 {
94         *(double *)to = val;
95         return to + sizeof(double);
96 }
97
98 static inline char *pack_timestamp(char *to)
99 {
100         struct timeval tv;
101
102         gettimeofday(&tv, NULL);
103         to = pack_int32(to, tv.tv_sec);
104         to = pack_int32(to, tv.tv_usec * 1000);
105
106         return to;
107 }
108
109 static inline char *pack_args(char *to, const char *fmt, ...)
110 {
111         va_list args;
112         uint32_t num = strlen(fmt);
113         const char *t = fmt;
114
115         if(*t == '\0') {
116                 num = 0;
117         }
118
119         memcpy(to, &num, sizeof(num));
120         to += sizeof(num);
121
122         va_start(args, fmt);
123
124         uint8_t c;
125         uint32_t d;
126         uint64_t x;
127         uint64_t p;
128         float f;
129         double w;
130         char *s;
131         int n;
132
133         for (t = fmt; *t != '\0'; t++) {
134                 switch (*t) {
135                 case 'c':
136                         c = (uint8_t)va_arg(args, uint32_t);
137                         *to++ = *t;
138                         memcpy(to, &c, sizeof(c));
139                         to += sizeof(c);
140                         break;
141                 case 'd':
142                         d = va_arg(args, uint32_t);
143                         *to++ = *t;
144                         memcpy(to, &d, sizeof(d));
145                         to += sizeof(d);
146                         break;
147                 case 'x':
148                         x = (uint64_t)va_arg(args, uint64_t);
149                         *to++ = *t;
150                         memcpy(to, &x, sizeof(x));
151                         to += sizeof(x);
152                         break;
153                 case 'p':
154                         p = (uintptr_t)va_arg(args, uint64_t);
155                         *to++ = *t;
156                         memcpy(to, &p, sizeof(p));
157                         to += sizeof(p);
158                         break;
159                 case 'f':
160                         f = (float)va_arg(args, double);
161                         *to++ = *t;
162                         memcpy(to, &f, sizeof(f));
163                         to += sizeof(f);
164                         break;
165                 case 'w':
166                         w = va_arg(args, double);
167                         *to++ = *t;
168                         memcpy(to, &w, sizeof(w));
169                         to += sizeof(w);
170                         break;
171                 case 's':
172                         s = va_arg(args, char *);
173                         *to++ = *t;
174                         n = strlen(s) + 1;
175                         strncpy(to, s, n);
176                         to += n;
177                         break;
178                 default:
179                         break;
180                 }
181         }
182
183         va_end(args);
184
185         return to;
186 }
187
188 #define BUF_PTR p
189 #define RET_PTR ret_p
190 #define PACK_INT32(val)                         \
191         BUF_PTR = pack_int32(BUF_PTR, val);
192 #define PACK_INT64(val)                         \
193         BUF_PTR = pack_int64(BUF_PTR, val);
194 #define PACK_STRING(str)                        \
195         BUF_PTR = pack_string(BUF_PTR, str);
196
197 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...)             \
198         do {                                                    \
199                 BUF_PTR = pack_int32(BUF_PTR, msg_id);          \
200                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
201                 BUF_PTR = pack_timestamp(BUF_PTR);              \
202                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
203                 BUF_PTR = pack_int32(BUF_PTR, api_id);          \
204                 BUF_PTR = pack_int32(BUF_PTR, getpid());                \
205                 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid));    \
206                 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
207                 RET_PTR = BUF_PTR;              \
208         } while (0)
209
210 #define PACK_COMMON_END(ret, errn, intern_call)         \
211         do {                                                    \
212                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret));        \
213                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn);  \
214                 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call);   \
215                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
216                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
217                 BUF_PTR = pack_int32(BUF_PTR, 0);               \
218         } while (0)
219
220 #define PACK_RETURN_END(ret)                                                            \
221                 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
222
223 #define PACK_MEMORY(size, memory_api_type, addr)                \
224         do {                                                    \
225                 BUF_PTR = pack_int64(BUF_PTR, size);                    \
226                 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
227                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
228         } while (0)
229
230 #define PACK_UICONTROL(control)                                         \
231         do {                                                            \
232                 if (unlikely(control == NULL)) {                        \
233                         BUF_PTR = pack_string(BUF_PTR, "");             \
234                         BUF_PTR = pack_string(BUF_PTR, "");             \
235                         BUF_PTR = pack_int64(BUF_PTR, 0);               \
236                 } else {                                                \
237                         char *type = NULL, *name = NULL;                \
238                         if (find_object_hash((void*)(control),          \
239                                              &type, &name) == 1) {      \
240                                 BUF_PTR = pack_string(BUF_PTR, type);   \
241                                 BUF_PTR = pack_string(BUF_PTR, name);   \
242                         } else {                                        \
243                                 BUF_PTR = pack_string(BUF_PTR, "");     \
244                                 BUF_PTR = pack_string(BUF_PTR, "");     \
245                         }                                               \
246                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
247                 }                                                       \
248         } while(0)
249
250 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2)       \
251         do {                                                            \
252                 BUF_PTR = pack_int32(BUF_PTR, event_type);              \
253                 BUF_PTR = pack_int32(BUF_PTR, detail_type);             \
254                 BUF_PTR = pack_int32(BUF_PTR, x);                       \
255                 BUF_PTR = pack_int32(BUF_PTR, y);                       \
256                 BUF_PTR = pack_string(BUF_PTR, info1);                  \
257                 BUF_PTR = pack_int32(BUF_PTR, info2);                   \
258         } while (0)
259
260 #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size,  \
261                       file_path)                                        \
262         do {                                                            \
263                 BUF_PTR = pack_int64(BUF_PTR, size);                    \
264                 BUF_PTR = pack_int32(BUF_PTR, fd_value);                \
265                 BUF_PTR = pack_int32(BUF_PTR, fd_type);                 \
266                 BUF_PTR = pack_int32(BUF_PTR, fd_api_type);             \
267                 BUF_PTR = pack_int64(BUF_PTR, file_size);               \
268                 BUF_PTR = pack_string(BUF_PTR, file_path);              \
269         } while (0)
270
271 #define PACK_SCREENSHOT(image_file_path, orientation)                           \
272                 do {                                                            \
273                           BUF_PTR = pack_string(BUF_PTR, image_file_path);      \
274                           BUF_PTR = pack_int32(BUF_PTR, orientation);           \
275                 } while (0)
276
277 #define PACK_SCENE(scene_name, formid, pform, panelid, ppanel, transition, user)        \
278         do {                                                                            \
279                 BUF_PTR = pack_string(BUF_PTR,  scene_name);                            \
280                 if (unlikely(pform == NULL)) {                                          \
281                         BUF_PTR = pack_string(BUF_PTR, "");                             \
282                         BUF_PTR = pack_int64(BUF_PTR, 0);                               \
283                 } else {                                                                \
284                         char *type = NULL, *name = NULL;                                \
285                         if (find_object_hash((void*)(pform), &type, &name) == 1) {      \
286                                 BUF_PTR = pack_string(BUF_PTR, name);                   \
287                         } else {                                                        \
288                                 BUF_PTR = pack_string(BUF_PTR, "");                     \
289                         }                                                               \
290                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)pform);                \
291                 }                                                                       \
292                 if (unlikely(ppanel == NULL)) {                                         \
293                         BUF_PTR = pack_string(BUF_PTR, "");                             \
294                         BUF_PTR = pack_int64(BUF_PTR, 0);                               \
295                 } else {                                                                \
296                         char *type = NULL, *name = NULL;                                \
297                         if (find_object_hash((void*)(ppanel), &type, &name) == 1) {     \
298                                 BUF_PTR = pack_string(BUF_PTR, name);                   \
299                         } else {                                                        \
300                                 BUF_PTR = pack_string(BUF_PTR, "");                     \
301                         }                                                               \
302                         BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ppanel));             \
303                 }                                                                       \
304                 BUF_PTR = pack_int64(BUF_PTR, transition);                              \
305                 BUF_PTR = pack_int64(BUF_PTR, user);                                    \
306         } while(0)
307
308  #define PACK_THREAD(thread_id, thread_type, api_type)  \
309          do {                                                           \
310                 BUF_PTR = pack_int64(BUF_PTR, thread_id);               \
311                 BUF_PTR = pack_int32(BUF_PTR, thread_type);             \
312                 BUF_PTR = pack_int32(BUF_PTR, api_type);                \
313         } while (0)
314
315 #define PACK_CUSTOM(handle, type, name, color, value)           \
316         do {                                                    \
317                 BUF_PTR = pack_int32(BUF_PTR, handle);          \
318                 BUF_PTR = pack_int32(BUF_PTR, type);            \
319                 BUF_PTR = pack_string(BUF_PTR, name);           \
320                 BUF_PTR = pack_int32(BUF_PTR, color);           \
321                 BUF_PTR = pack_double(BUF_PTR, value);          \
322         } while (0)
323
324 #define PACK_SYNC(sync_val, sync_type, api_type)                     \
325         do {                                                         \
326                 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val);  \
327                 BUF_PTR = pack_int32(BUF_PTR, sync_type);            \
328                 BUF_PTR = pack_int32(BUF_PTR, api_type);             \
329         } while (0)
330
331 #define LOCAL_BUF_SIZE 1024
332 #define PREPARE_LOCAL_BUF()                     \
333                 char buf[LOCAL_BUF_SIZE];       \
334                 char *p = buf;                  \
335                 char *ret_p = NULL;
336
337 #define MSG_LEN_OFFSET 16
338 #define MSG_HDR_LEN 20
339 #define FLUSH_LOCAL_BUF()                                               \
340                 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
341                 send(gTraceInfo.socket.daemonSock, buf, (p - buf), 0);
342
343 // =========================== post block macro ===========================
344
345 #define POST_PACK_PROBEBLOCK_BEGIN()                                    \
346         newerrno = errno;                                               \
347         if(postBlockBegin(blockresult)) {
348
349 #define POST_PACK_PROBEBLOCK_END()                                      \
350                 postBlockEnd();                                         \
351         }                                                               \
352         errno = (newerrno != 0) ? newerrno : olderrno
353
354 /* int main(int argc, char **argv) */
355 /* { */
356 /*      char buf[1024]; */
357 /*      char *p = buf; */
358
359 /*      p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
360 /*                            (uint64_t)0, 0.19, 0.33, "hello!"); */
361 /*      p = PACK_COMMON_END(p, 0); */
362
363 /*      int fd = creat("out.bin", 0644); */
364 /*      if (fd == -1) { */
365 /*              printf("error\n"); */
366 /*              exit(1); */
367 /*      } */
368
369 /*      write(fd, buf, p - buf); */
370 /*      close(fd); */
371
372 /*      return 0; */
373 /* } */
374
375 #endif /* __BIN_PROTO_H__ */