1 #ifndef __BIN_PROTO_H__
2 #define __BIN_PROTO_H__
13 #include <sys/types.h>
17 #include <sys/types.h>
19 #include <sys/syscall.h>
21 // TODO: remove this copy-paste
22 #define CALLER_ADDRESS \
23 ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
28 static char *pack_int32(char *to, uint32_t val)
30 *(uint32_t *)to = val;
31 return to + sizeof(uint32_t);
34 static char *pack_int64(char *to, uint64_t val)
36 *(uint64_t *)to = val;
37 return to + sizeof(uint64_t);
40 static char *pack_addr(char *to, unsigned long addr)
42 return pack_int64(to, (intptr_t)addr);
45 static char *pack_string(char *to, const char *str)
47 size_t len = strlen(str) + 1;
48 strncpy(to, str, len);
53 static char *pack_api_id(char *to, uint32_t api_id)
55 return pack_int32(to, api_id);
58 static char *pack_args(char *to, const char *fmt, ...)
61 uint32_t num = strlen(fmt);
64 /* printf("num = %d\n", num); */
66 memcpy(to, &num, sizeof(num));
83 for (t = fmt; *t != '\0'; t++) {
86 c = (uint8_t)va_arg(args, uint32_t);
87 /* printf("char = %c\n", c); */
88 memcpy(to, &c, sizeof(c));
92 d = va_arg(args, uint32_t);
93 /* printf("int = %d\n", d); */
94 memcpy(to, &d, sizeof(d));
98 x = va_arg(args, uint64_t);
99 /* printf("long = %lld\n", x); */
100 memcpy(to, &x, sizeof(x));
104 p = va_arg(args, uint64_t);
105 /* printf("pointer = %p\n", (void *)p); */
106 memcpy(to, &p, sizeof(p));
110 f = (float)va_arg(args, double);
111 /* printf("float = %f\n", f); */
112 memcpy(to, &f, sizeof(f));
116 w = va_arg(args, double);
117 /* printf("double = %e\n", w); */
118 memcpy(to, &w, sizeof(w));
122 s = va_arg(args, char *);
123 /* printf("string = %s\n", s); */
138 static char *pack_pid(char *to)
140 return pack_int32(to, getpid());
143 static char *pack_tid(char *to)
145 return pack_int32(to, syscall(__NR_gettid));
148 static char *pack_return(char *to, uint64_t ret)
150 return pack_int64(to, ret);
153 static char *pack_pc(char *to, uint64_t pc)
155 return pack_int64(to, pc);
158 static char *pack_errno(char *to, uint32_t en)
160 return pack_int32(to, en);
163 static char *pack_internal_call(char *to)
165 // TODO: where to get int call?
166 return pack_int32(to, 0);
169 static char *pack_caller_pc(char *to)
171 return pack_int64(to, (uintptr_t)CALLER_ADDRESS);
175 #define PACK_COMMON_BEGIN(to, api_id, fmt, ...) \
176 to = pack_api_id(to, api_id); \
179 to = pack_args(to, fmt, __VA_ARGS__)
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);
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);
193 #define PACK_UICONTROL(to, parent_name, parent_class_name, \
194 parent_pointer, child_name, child_class_name, \
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);
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);
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);
220 #define PACK_LIFECYCLE(to)
222 #define PACK_SCREENSHOT(to, image_file_path, orienation) \
223 to = pack_string(to, image_file_path); \
224 to = pack_int32(to, orienation);
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);
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);
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);
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);
255 #define LOG_PATH "/tmp/trace.bin"
257 log_fd = creat(LOG_PATH, 0644); \
258 if (log_fd == -1) { \
262 #define CLOSE_LOG() \
268 #define LOCAL_BUF_SIZE 1024
269 #define PREPARE_LOCAL_BUF() \
270 char buf[LOCAL_BUF_SIZE]; \
273 #define FLUSH_LOCAL_BUF() \
274 write(log_fd, buf, p - buf);
280 /* int main(int argc, char **argv) */
282 /* char buf[1024]; */
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); */
289 /* int fd = creat("out.bin", 0644); */
290 /* if (fd == -1) { */
291 /* printf("error\n"); */
295 /* write(fd, buf, p - buf); */
301 #endif /* __BIN_PROTO_H__ */