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>
23 #define MSG_PROBE_MEMORY 0x3001
24 #define MSG_PROBE_UICONTROL 0x3002
25 #define MSG_PROBE_UIEVENT 0x3003
26 #define MSG_PROBE_RESOUCE 0x3004
27 #define MSG_PROBE_LIFECYCLE 0x30015
28 #define MSG_PROBE_SCREENSHOT 0x3006
29 #define MSG_PROBE_SCENE 0x3007
30 #define MSG_PROBE_THREAD 0x3008
31 #define MSG_PROBE_CUSTOM 0x3009
32 #define MSG_PROBE_SYNC 0x3010
34 // TODO: remove this copy-paste
35 #define CALLER_ADDRESS \
36 ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
41 static inline char *pack_int32(char *to, uint32_t val)
43 *(uint32_t *)to = val;
44 return to + sizeof(uint32_t);
47 static inline char *pack_int64(char *to, uint64_t val)
49 *(uint64_t *)to = val;
50 return to + sizeof(uint64_t);
53 static inline char *pack_string(char *to, const char *str)
55 size_t len = strlen(str) + 1;
56 strncpy(to, str, len);
60 static inline char *pack_timestamp(char *to)
64 gettimeofday(&tv, NULL);
65 to = pack_int32(to, tv.tv_sec);
66 to = pack_int32(to, tv.tv_usec * 1000);
71 static inline char *pack_args(char *to, const char *fmt, ...)
74 uint32_t num = strlen(fmt);
81 memcpy(to, &num, sizeof(num));
95 for (t = fmt; *t != '\0'; t++) {
98 c = (uint8_t)va_arg(args, uint32_t);
100 memcpy(to, &c, sizeof(c));
104 d = va_arg(args, uint32_t);
106 memcpy(to, &d, sizeof(d));
110 x = (uint64_t)va_arg(args, uint64_t);
112 memcpy(to, &x, sizeof(x));
116 p = (uintptr_t)va_arg(args, uint64_t);
118 memcpy(to, &p, sizeof(p));
122 f = (float)va_arg(args, double);
124 memcpy(to, &f, sizeof(f));
128 w = va_arg(args, double);
130 memcpy(to, &w, sizeof(w));
134 s = va_arg(args, char *);
151 #define RET_PTR ret_p
152 #define PACK_INT32(val) \
153 BUF_PTR = pack_int32(BUF_PTR, val);
154 #define PACK_INT64(val) \
155 BUF_PTR = pack_int64(BUF_PTR, val);
156 #define PACK_STRING(str) \
157 BUF_PTR = pack_string(BUF_PTR, str);
159 #define PACK_COMMON_BEGIN(msg_id, api_id, fmt, ...) \
161 BUF_PTR = pack_int32(BUF_PTR, msg_id); \
162 BUF_PTR = pack_int32(BUF_PTR, 0); \
163 BUF_PTR = pack_timestamp(BUF_PTR); \
164 BUF_PTR = pack_int32(BUF_PTR, 0); \
165 BUF_PTR = pack_int32(BUF_PTR, api_id); \
166 BUF_PTR = pack_int32(BUF_PTR, getpid()); \
167 BUF_PTR = pack_int32(BUF_PTR, syscall(__NR_gettid)); \
168 BUF_PTR = pack_args(BUF_PTR, fmt, __VA_ARGS__); \
172 #define PACK_COMMON_END(ret, errn, intern_call) \
174 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(ret)); \
175 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)errn); \
176 BUF_PTR = pack_int32(BUF_PTR, (uint32_t)intern_call); \
177 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)CALLER_ADDRESS); \
178 BUF_PTR = pack_int32(BUF_PTR, 0); \
179 BUF_PTR = pack_int32(BUF_PTR, 0); \
182 #define PACK_RETURN_END(ret) \
183 RET_PTR = pack_int64(RET_PTR, (uintptr_t)(ret));
185 #define PACK_MEMORY(size, memory_api_type, addr) \
187 BUF_PTR = pack_int32(BUF_PTR, size); \
188 BUF_PTR = pack_int32(BUF_PTR, memory_api_type); \
189 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)addr); \
192 #define PACK_UICONTROL(control) \
194 if (unlikely(control == NULL)) { \
195 BUF_PTR = pack_string(BUF_PTR, ""); \
196 BUF_PTR = pack_string(BUF_PTR, ""); \
197 BUF_PTR = pack_int64(BUF_PTR, 0); \
199 char *type = NULL, *name = NULL; \
200 if (find_object_hash((void*)(control), \
201 &type, &name) == 1) { \
202 BUF_PTR = pack_string(BUF_PTR, type); \
203 BUF_PTR = pack_string(BUF_PTR, name); \
205 BUF_PTR = pack_string(BUF_PTR, ""); \
206 BUF_PTR = pack_string(BUF_PTR, ""); \
208 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)(control)); \
212 #define PACK_UIEVENT(event_type, detail_type, x, y, info1, info2) \
214 BUF_PTR = pack_int32(BUF_PTR, event_type); \
215 BUF_PTR = pack_int32(BUF_PTR, detail_type); \
216 BUF_PTR = pack_int32(BUF_PTR, x); \
217 BUF_PTR = pack_int32(BUF_PTR, y); \
218 BUF_PTR = pack_string(BUF_PTR, info1); \
219 BUF_PTR = pack_int32(BUF_PTR, info2); \
222 /* #define PACK_RESOURCE(size, fd_value, fd_type, fd_api_type, file_size, \ */
225 /* BUF_PTR = pack_int32(BUF_PTR, size); \ */
226 /* BUF_PTR = pack_int32(BUF_PTR, fd_value); \ */
227 /* BUF_PTR = pack_int32(BUF_PTR, fd_type); \ */
228 /* BUF_PTR = pack_int32(BUF_PTR, fd_api_type); \ */
229 /* BUF_PTR = pack_int32(BUF_PTR, file_size); \ */
230 /* BUF_PTR = pack_string(BUF_PTR, file_path); \ */
233 /* #define PACK_SCREENSHOT(image_file_path, orienation) \ */
235 /* BUF_PTR = pack_string(BUF_PTR, image_file_path); \ */
236 /* BUF_PTR = pack_int32(BUF_PTR, orienation); \ */
239 /* #define PACK_SCENE(scene_name, form_name, form_pointer, \ */
240 /* panel_name, panel_pointer, transition_time, \ */
241 /* user_transition_time) \ */
243 /* BUF_PTR = pack_string(BUF_PTR, scene_name); \ */
244 /* BUF_PTR = pack_string(BUF_PTR, form_name); \ */
245 /* BUF_PTR = pack_int64(BUF_PTR, form_pointer); \ */
246 /* BUF_PTR = pack_string(BUF_PTR, panel_name); \ */
247 /* BUF_PTR = pack_int64(BUF_PTR, panel_pointer); \ */
248 /* BUF_PTR = pack_int32(BUF_PTR, transition_time); \ */
249 /* BUF_PTR = pack_int32(BUF_PTR, user_transition_time); \ */
252 #define PACK_THREAD(thread_id, thread_type, api_type) \
254 BUF_PTR = pack_int64(BUF_PTR, thread_id); \
255 BUF_PTR = pack_int32(BUF_PTR, thread_type); \
256 BUF_PTR = pack_int32(BUF_PTR, api_type); \
259 /* #define PACK_CUSTOM(handle, type, name, color, value) \ */
260 /* BUF_PTR = pack_int32(BUF_PTR, handle); \ */
261 /* BUF_PTR = pack_int32(BUF_PTR, type); \ */
262 /* BUF_PTR = pack_string(BUF_PTR, name); \ */
263 /* BUF_PTR = pack_int32(BUF_PTR, color); \ */
264 /* BUF_PTR = pack_int32(BUF_PTR, value); */
266 #define PACK_SYNC(sync_val, sync_type, api_type) \
268 BUF_PTR = pack_int64(BUF_PTR, (uintptr_t)sync_val); \
269 BUF_PTR = pack_int32(BUF_PTR, sync_type); \
270 BUF_PTR = pack_int32(BUF_PTR, api_type); \
273 #define LOG_PATH "/tmp/trace.bin"
276 log_fd = creat(LOG_PATH, 0644); \
277 if (log_fd == -1) { \
281 #define CLOSE_LOG() \
290 #define LOCAL_BUF_SIZE 1024
291 #define PREPARE_LOCAL_BUF() \
292 char buf[LOCAL_BUF_SIZE]; \
296 #define MSG_LEN_OFFSET 16
297 #define MSG_HDR_LEN 20
298 #define FLUSH_LOCAL_BUF() \
299 *(uint32_t *)(buf + MSG_LEN_OFFSET) = (p - buf) - MSG_HDR_LEN; \
300 write(log_fd, buf, p - buf);
302 // =========================== post block macro ===========================
304 #define POST_PACK_PROBEBLOCK_BEGIN(LCTYPE, RETVALUE, INPUTFORMAT, ...) \
306 if(postBlockBegin(blockresult)) {
308 #define POST_PACK_PROBEBLOCK_END() \
311 errno = (newerrno != 0) ? newerrno : olderrno
316 /* int main(int argc, char **argv) */
318 /* char buf[1024]; */
321 /* p = PACK_COMMON_BEGIN(p, 42, "cdxpfws", 'a', 10, (uint64_t)80, */
322 /* (uint64_t)0, 0.19, 0.33, "hello!"); */
323 /* p = PACK_COMMON_END(p, 0); */
325 /* int fd = creat("out.bin", 0644); */
326 /* if (fd == -1) { */
327 /* printf("error\n"); */
331 /* write(fd, buf, p - buf); */
337 #endif /* __BIN_PROTO_H__ */