xref: /OK3568_Linux_fs/app/lvgl_demo/sys/trace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "trace.h"
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <inttypes.h>
10 #include <string.h>
11 
12 #define TRACE_ON
13 
14 #ifdef TRACE_ON
15 
16 #ifdef __cplusplus
17 #define CC_LIKELY( exp )    (__builtin_expect( !!(exp), true ))
18 #define CC_UNLIKELY( exp )  (__builtin_expect( !!(exp), false ))
19 #else
20 #define CC_LIKELY( exp )    (__builtin_expect( !!(exp), 1 ))
21 #define CC_UNLIKELY( exp )  (__builtin_expect( !!(exp), 0 ))
22 #endif
23 
24 #define ATRACE_MESSAGE_LENGTH 1024
25 
26 static int atrace_marker_fd     = -1;
27 static int init_ok = 0;
28 
trace_init_once(void)29 static int trace_init_once(void)
30 {
31     if (init_ok == 0)
32     {
33          atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
34         if (atrace_marker_fd == -1) {
35             printf("Error opening trace file: %s (%d)", strerror(errno), errno);
36             return -1;
37         }
38     }
39 
40     init_ok = 1;
41     return 0;
42 }
43 
44 #define WRITE_MSG(format_begin, format_end, name, value) { \
45     char buf[ATRACE_MESSAGE_LENGTH]; \
46     if (CC_UNLIKELY(!init_ok)) \
47         trace_init_once(); \
48     int pid = getpid(); \
49     int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
50         name, value); \
51     if (len >= (int) sizeof(buf)) { \
52         /* Given the sizeof(buf), and all of the current format buffers, \
53          * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \
54         int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
55         /* Truncate the name to make the message fit. */ \
56         printf("Truncated name in %s: %s\n", __FUNCTION__, name); \
57         len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \
58             name_len, name, value); \
59     } \
60     if (write(atrace_marker_fd, buf, len) != len) \
61         printf("Warning: write failed\n"); \
62 }
63 
atrace_begin_body(const char * name)64 void atrace_begin_body(const char* name)
65 {
66     WRITE_MSG("B|%d|", "%s", name, "");
67 }
68 
atrace_end_body()69 void atrace_end_body()
70 {
71     WRITE_MSG("E|%d", "%s", "", "");
72 }
73 
atrace_async_begin_body(const char * name,int32_t cookie)74 void atrace_async_begin_body(const char* name, int32_t cookie)
75 {
76     WRITE_MSG("S|%d|", "|%" PRId32, name, cookie);
77 }
78 
atrace_async_end_body(const char * name,int32_t cookie)79 void atrace_async_end_body(const char* name, int32_t cookie)
80 {
81     WRITE_MSG("F|%d|", "|%" PRId32, name, cookie);
82 }
83 
atrace_int_body(const char * name,int32_t value)84 void atrace_int_body(const char* name, int32_t value)
85 {
86     WRITE_MSG("C|%d|", "|%" PRId32, name, value);
87 }
88 
atrace_int64_body(const char * name,int64_t value)89 void atrace_int64_body(const char* name, int64_t value)
90 {
91     WRITE_MSG("C|%d|", "|%" PRId64, name, value);
92 }
93 
94 #else
95 
atrace_begin_body(const char * name)96 void atrace_begin_body(__attribute__((unused)) const char* name)
97 {
98 }
99 
atrace_end_body()100 void atrace_end_body()
101 {
102 }
103 
atrace_async_begin_body(const char * name,int32_t cookie)104 void atrace_async_begin_body(__attribute__((unused)) const char* name, __attribute__((unused)) int32_t cookie)
105 {
106 }
107 
atrace_async_end_body(const char * name,int32_t cookie)108 void atrace_async_end_body(__attribute__((unused)) const char* name, __attribute__((unused)) int32_t cookie)
109 {
110 }
111 
atrace_int_body(const char * name,int32_t value)112 void atrace_int_body(__attribute__((unused)) const char* name, __attribute__((unused)) int32_t value)
113 {
114 }
115 
atrace_int64_body(const char * name,int64_t value)116 void atrace_int64_body(__attribute__((unused)) const char* name, __attribute__((unused)) int64_t value)
117 {
118 }
119 
120 #endif
121