Add write to buffer draft
[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 // TODO: remove this copy-paste
22 #define CALLER_ADDRESS  \
23         ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
24
25
26
27 // COMMON
28 static  char *pack_int32(char *to, uint32_t val)
29 {
30         *(uint32_t *)to = val;
31         return to + sizeof(uint32_t);
32 }
33
34 static  char *pack_int64(char *to, uint64_t val)
35 {
36         *(uint64_t *)to = val;
37         return to + sizeof(uint64_t);
38 }
39
40 static  char *pack_addr(char *to, unsigned long addr)
41 {
42         return pack_int64(to, (intptr_t)addr);
43 }
44
45 static  char *pack_string(char *to, const char *str)
46 {
47         size_t len = strlen(str) + 1;
48         strncpy(to, str, len);
49         return to + len;
50 }
51
52
53 static  char *pack_api_id(char *to, uint32_t api_id)
54 {
55         return pack_int32(to, api_id);
56 }
57
58 static  char *pack_args(char *to, const char *fmt, ...)
59 {
60         va_list args;
61         uint32_t num = strlen(fmt);
62         const char *t = fmt;
63
64         /* printf("num = %d\n", num); */
65
66         memcpy(to, &num, sizeof(num));
67         to += sizeof(num);
68
69         memcpy(to, fmt, num);
70         to += num;
71
72         va_start(args, fmt);
73
74         uint8_t c;
75         uint32_t d;
76         uint64_t x;
77         uint64_t p;
78         float f;
79         double w;
80         char *s;
81         int n;
82
83         for (t = fmt; *t != '\0'; t++) {
84                 switch (*t) {
85                 case 'c':
86                         c = (uint8_t)va_arg(args, uint32_t);
87                         /* printf("char = %c\n", c); */
88                         memcpy(to, &c, sizeof(c));
89                         to += sizeof(c);
90                         break;
91                 case 'd':
92                         d = va_arg(args, uint32_t);
93                         /* printf("int = %d\n", d); */
94                         memcpy(to, &d, sizeof(d));
95                         to += sizeof(d);
96                         break;
97                 case 'x':
98                         x = va_arg(args, uint64_t);
99                         /* printf("long = %lld\n", x); */
100                         memcpy(to, &x, sizeof(x));
101                         to += sizeof(x);
102                         break;
103                 case 'p':
104                         p = va_arg(args, uint64_t);
105                         /* printf("pointer = %p\n", (void *)p); */
106                         memcpy(to, &p, sizeof(p));
107                         to += sizeof(p);
108                         break;
109                 case 'f':
110                         f = (float)va_arg(args, double);
111                         /* printf("float = %f\n", f); */
112                         memcpy(to, &f, sizeof(f));
113                         to += sizeof(f);
114                         break;
115                 case 'w':
116                         w = va_arg(args, double);
117                         /* printf("double = %e\n", w); */
118                         memcpy(to, &w, sizeof(w));
119                         to += sizeof(w);
120                         break;
121                 case 's':
122                         s = va_arg(args, char *);
123                         /* printf("string = %s\n", s); */
124                         n = strlen(s) + 1;
125                         strncpy(to, s, n);
126                         to += n;
127                         break;
128                 default:
129                         break;
130                 }
131         }
132
133         va_end(args);
134
135         return to;
136 }
137
138 static  char *pack_pid(char *to)
139 {
140         return pack_int32(to, getpid());
141 }
142
143 static  char *pack_tid(char *to)
144 {
145         return pack_int32(to, syscall(__NR_gettid));
146 }
147
148 static  char *pack_return(char *to, uint64_t ret)
149 {
150         return pack_int64(to, ret);
151 }
152
153 static  char *pack_pc(char *to, uint64_t pc)
154 {
155         return pack_int64(to, pc);
156 }
157
158 static  char *pack_errno(char *to, uint32_t en)
159 {
160         return pack_int32(to, en);
161 }
162
163 static  char *pack_internal_call(char *to)
164 {
165         // TODO: where to get int call?
166         return pack_int32(to, 0);
167 }
168
169 static  char *pack_caller_pc(char *to)
170 {
171         return pack_int64(to, (uintptr_t)CALLER_ADDRESS);
172 }
173
174
175 #define PACK_COMMON_BEGIN(to, api_id, fmt, ...) \
176         to = pack_api_id(to, api_id); \
177         to = pack_pid(to); \
178         to = pack_tid(to); \
179         to = pack_args(to, fmt, __VA_ARGS__)
180
181 #define PACK_COMMON_END(to, ret, pc)         \
182         to = pack_return(to, (uintptr_t)ret); \
183         to = pack_pc(to, (uintptr_t)pc);             \
184         to = pack_errno(to, (uint32_t)newerrno);     \
185         to = pack_internal_call(to); \
186         to = pack_caller_pc(to);
187
188 #define PACK_MEMORY(to, size, memory_api_type, addr) \
189         to = pack_int32(to, size); \
190         to = pack_int32(to, memory_api_type); \
191         to = pack_int64(to, (uintptr_t)addr);
192
193 #define PACK_UICONTROL(to, parent_name, parent_class_name,              \
194                        parent_pointer, child_name, child_class_name,    \
195                        child_pointer) \
196         to = pack_string(to, parent_name); \
197         to = pack_string(to, parent_class_name); \
198         to = pack_int64(to, parent_pointer); \
199         to = pack_string(to, child_name); \
200         to = pack_string(to, child_class_name); \
201         to = pack_int64(to, child_pointer);
202
203 #define PACK_UIEVENT(to, event_type, detail_type, x, y, info1, info2) \
204         to = pack_int32(to, event_type); \
205         to = pack_int32(to, detail_type); \
206         to = pack_int32(to, x); \
207         to = pack_int32(to, y); \
208         to = pack_int32(to, info1); \
209         to = pack_int32(to, info2);
210
211 #define PACK_RESOURCE(to, size, fd_value, fd_type, fd_api_type, \
212                       file_size, file_path) \
213         to = pack_int32(to, size); \
214         to = pack_int32(to, fd_value); \
215         to = pack_int32(to, fd_type); \
216         to = pack_int32(to, fd_api_type); \
217         to = pack_int32(to, file_size); \
218         to = pack_string(to, file_path);
219
220 #define PACK_LIFECYCLE(to)
221
222 #define PACK_SCREENSHOT(to, image_file_path, orienation) \
223         to = pack_string(to, image_file_path); \
224         to = pack_int32(to, orienation);
225
226 #define PACK_SCENE(to, scene_name, form_name, form_pointer,     \
227                    panel_name, panel_pointer, transition_time,  \
228                    user_transition_time)                        \
229         to = pack_string(to, scene_name); \
230         to = pack_string(to, form_name); \
231         to = pack_int64(to, form_pointer); \
232         to = pack_string(to, panel_name); \
233         to = pack_int64(to, panel_pointer); \
234         to = pack_int32(to, transition_time); \
235         to = pack_int32(to, user_transition_time);
236
237 #define PACK_THREAD(to, pthread_id, osp_thread_id, thread_type, api_type) \
238         to = pack_int32(to, pthread_id); \
239         to = pack_int32(to, osp_thread_id); \
240         to = pack_int32(to, thread_type); \
241         to = pack_int32(to, api_type);
242
243 #define PACK_CUSTOM(to, handle, type, name, color, value) \
244         to = pack_int32(to, handle); \
245         to = pack_int32(to, type); \
246         to = pack_string(to, name); \
247         to = pack_int32(to, color); \
248         to = pack_int32(to, value);
249
250 #define PACK_SYNC(to, sync_val, sync_type, api_type) \
251         to = pack_int32(to, sync_val);               \
252         to = pack_int32(to, sync_type);              \
253         to = pack_int32(to, api_type);
254
255 #define LOG_PATH "/tmp/trace.bin"
256 #define OPEN_LOG()                              \
257         log_fd = creat(LOG_PATH, 0644);         \
258         if (log_fd == -1) {                     \
259                 exit(1);                        \
260         }
261
262 #define CLOSE_LOG()                             \
263         if (log_fd > 0) {                       \
264                 close(log_fd);                  \
265         }
266
267
268 #define LOCAL_BUF_SIZE 1024
269 #define PREPARE_LOCAL_BUF()                     \
270         char buf[LOCAL_BUF_SIZE];               \
271         char *p = buf;
272
273 #define FLUSH_LOCAL_BUF()                       \
274         write(log_fd, buf, p - buf);
275
276
277 /* data */
278 extern int log_fd;
279
280 /* int main(int argc, char **argv) */
281 /* { */
282 /*      char buf[1024]; */
283 /*      char *p = buf; */
284
285 /*      p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
286 /*                            (uint64_t)0, 0.19, 0.33, "hello!"); */
287 /*      p = PACK_COMMON_END(p, 0); */
288
289 /*      int fd = creat("out.bin", 0644); */
290 /*      if (fd == -1) { */
291 /*              printf("error\n"); */
292 /*              exit(1); */
293 /*      } */
294
295 /*      write(fd, buf, p - buf); */
296 /*      close(fd); */
297
298 /*      return 0; */
299 /* } */
300
301 #endif /* __BIN_PROTO_H__ */