4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
8 * Nikita Kalyazin <n.kalyazin@samsung.com>
9 * Anastasia Lyupa <a.lyupa@samsung.com>
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)
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.
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
26 * - Samsung RnD Institute Russia
30 #ifndef __BIN_PROTO_H__
31 #define __BIN_PROTO_H__
42 #include <sys/types.h>
46 #include <sys/types.h>
48 #include <sys/syscall.h>
52 #include <sys/socket.h>
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
66 // TODO: remove this copy-paste
67 #define CALLER_ADDRESS \
68 ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
73 static inline char *pack_int32(char *to, uint32_t val)
75 *(uint32_t *)to = val;
76 return to + sizeof(uint32_t);
79 static inline char *pack_int64(char *to, uint64_t val)
81 *(uint64_t *)to = val;
82 return to + sizeof(uint64_t);
85 static inline char *pack_string(char *to, const char *str)
87 size_t len = strlen(str) + 1;
88 strncpy(to, str, len);
92 static inline char *pack_double(char *to, double val)
95 return to + sizeof(double);
98 static inline char *pack_timestamp(char *to)
102 gettimeofday(&tv, NULL);
103 to = pack_int32(to, tv.tv_sec);
104 to = pack_int32(to, tv.tv_usec * 1000);
109 static inline char *pack_args(char *to, const char *fmt, ...)
112 uint32_t num = strlen(fmt);
119 memcpy(to, &num, sizeof(num));
133 for (t = fmt; *t != '\0'; t++) {
136 c = (uint8_t)va_arg(args, uint32_t);
138 memcpy(to, &c, sizeof(c));
142 d = va_arg(args, uint32_t);
144 memcpy(to, &d, sizeof(d));
148 x = (uint64_t)va_arg(args, uint64_t);
150 memcpy(to, &x, sizeof(x));
154 p = (uintptr_t)va_arg(args, uint64_t);
156 memcpy(to, &p, sizeof(p));
160 f = (float)va_arg(args, double);
162 memcpy(to, &f, sizeof(f));
166 w = va_arg(args, double);
168 memcpy(to, &w, sizeof(w));
172 s = va_arg(args, char *);
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);
197 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...) \
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__); \
210 #define PACK_COMMON_END(ret, errn, intern_call) \
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); \
220 #define PACK_RETURN_END(ret) \
221 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
223 #define PACK_MEMORY(size, memory_api_type, addr) \
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); \
230 #define PACK_UICONTROL(control) \
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); \
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); \
243 BUF_PTR = pack_string(BUF_PTR, ""); \
244 BUF_PTR = pack_string(BUF_PTR, ""); \
246 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
250 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2) \
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); \
260 #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size, \
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); \
271 #define PACK_SCREENSHOT(image_file_path, orientation) \
273 BUF_PTR = pack_string(BUF_PTR, image_file_path); \
274 BUF_PTR = pack_int32(BUF_PTR, orientation); \
277 #define PACK_SCENE(scene_name, formid, pform, panelid, ppanel, transition, user) \
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); \
284 char *type = NULL, *name = NULL; \
285 if (find_object_hash((void*)(pform), &type, &name) == 1) { \
286 BUF_PTR = pack_string(BUF_PTR, name); \
288 BUF_PTR = pack_string(BUF_PTR, ""); \
290 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)pform); \
292 if (unlikely(ppanel == NULL)) { \
293 BUF_PTR = pack_string(BUF_PTR, ""); \
294 BUF_PTR = pack_int64(BUF_PTR, 0); \
296 char *type = NULL, *name = NULL; \
297 if (find_object_hash((void*)(ppanel), &type, &name) == 1) { \
298 BUF_PTR = pack_string(BUF_PTR, name); \
300 BUF_PTR = pack_string(BUF_PTR, ""); \
302 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ppanel)); \
304 BUF_PTR = pack_int64(BUF_PTR, transition); \
305 BUF_PTR = pack_int64(BUF_PTR, user); \
308 #define PACK_THREAD(thread_id, thread_type, api_type) \
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); \
315 #define PACK_CUSTOM(handle, type, name, color, value) \
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); \
324 #define PACK_SYNC(sync_val, sync_type, api_type) \
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); \
331 #define LOCAL_BUF_SIZE 1024
332 #define PREPARE_LOCAL_BUF() \
333 char buf[LOCAL_BUF_SIZE]; \
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);
343 // =========================== post block macro ===========================
345 #define POST_PACK_PROBEBLOCK_BEGIN() \
347 if(postBlockBegin(blockresult)) {
349 #define POST_PACK_PROBEBLOCK_END() \
352 errno = (newerrno != 0) ? newerrno : olderrno
354 /* int main(int argc, char **argv) */
356 /* char buf[1024]; */
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); */
363 /* int fd = creat("out.bin", 0644); */
364 /* if (fd == -1) { */
365 /* printf("error\n"); */
369 /* write(fd, buf, p - buf); */
375 #endif /* __BIN_PROTO_H__ */