change Copyright headers, change line endings to Unix format
[platform/core/system/swap-probe.git] / include / daprobe.h
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * Anastasia Lyupa <a.lyupa@samsung.com>
12  * 
13  * This library is free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License as published by the
15  * Free Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  * 
18  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21  * License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this library; if not, write to the Free Software Foundation, Inc., 51
25  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  *
27  * Contributors:
28  * - S-Core Co., Ltd
29  * - Samsung RnD Institute Russia
30  * 
31  */
32
33 #ifndef __DAPROBE_H__
34 #define __DAPROBE_H__
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdbool.h>
39 #include <dlfcn.h>
40 #include "probeinfo.h"
41
42 #ifdef __cplusplus
43 extern "C"{
44 #endif
45
46 #ifndef likely
47 #define likely(x)       __builtin_expect((x),1)
48 #define unlikely(x)     __builtin_expect((x),0)
49 #endif
50
51 #define NUM_OF_MONITOR          3
52 #define DA_LOG_MAX                      4096
53 #define PERROR_MSG_MAX          128
54
55 #define DEFAULT_TOKEN   "`,"
56
57 #define CALLER_ADDRESS  \
58         ((void*) __builtin_extract_return_addr(__builtin_return_address(0)))
59
60 #define BACKTRACE_SYMBOLS cached_backtrace_symbols
61
62 typedef enum
63 {
64         MT_MEMORY,
65         MT_FILE,
66         MT_SOCKET,
67 } MonitorType;
68
69 enum BlockLocation
70 {
71         BL_PRE,
72         BL_POST,
73 };
74
75 typedef struct
76 {
77         int type;
78         int length;
79         char data[DA_LOG_MAX];
80 } log_t;
81
82 typedef struct
83 {
84         int                             eventIndex;
85         int                             pID;
86         int                             tID;
87         unsigned long   currentTime;
88         unsigned int    callDepth;
89 } probeInfo_t;
90
91 /***************************************************************************
92  * helper apis
93  ***************************************************************************/
94
95 int preBlockBegin(void* caller, bool bFiltering, enum DaOptions);
96 void preBlockEnd();
97
98 int postBlockBegin(int preresult);
99 void postBlockEnd();
100
101 void probeBlockStart();
102 void probeBlockEnd();
103
104 unsigned int getCurrentEventIndex();
105 unsigned int getCallDepth();
106 unsigned long getTraceState();
107 unsigned long getCurrentTime();
108 bool setProbePoint(probeInfo_t * iProbe);
109 int update_heap_memory_size(bool isAdd, size_t size);
110
111 bool printLogStr(const char* str, int msgType);
112 //bool printLog(log_t* log, int msgType);
113 int __appendTypeLog(log_t* log, int nInput, char* token, ...);
114 int getBacktraceString(log_t* log, int bufsize);
115
116 //char* captureScreenShotX(int* width, int* height);
117 //void releaseScreenShotX();
118
119 //int printSamplingLog(void);
120
121 //bool captureScreen(unsigned int logNum);
122 //void captureProbe();
123 //int registeScreenChange(int renderID);
124 //void detectTouchEvent(int touchID);
125 //int getTouchState();
126 //int isPossibleCapture();
127
128
129 /***************************************************************************
130  * helper macros
131  ***************************************************************************/
132
133 // ========================= print log =====================================
134 #define PRINTMSG(msg)   printLogStr(msg, MSG_MSG)
135
136 // =========================== declare variables ===========================
137 // local variable is faster than heap allocated variable
138 // array variable initialization with declare is expensive than memset
139
140 // declare variable for standard api (not tizen related api)
141 #define DECLARE_VARIABLE_STANDARD               \
142                 probeInfo_t     probeInfo;                      \
143                 int blockresult;                                \
144                 bool bfiltering = true;                 \
145                 int olderrno, newerrno;                 \
146                 int __attribute__((unused)) ret
147
148 // ========================== get function pointer =========================
149
150 #define GET_REAL_FUNCP(FUNCNAME, SONAME, FUNCTIONPOINTER)                       \
151                 do {                                                                                                            \
152                         if(!FUNCTIONPOINTER) {                                                                  \
153                                 probeBlockStart();                                                                      \
154                                 if(lib_handle[SONAME] == NULL) {                                        \
155                                         lib_handle[SONAME] = dlopen(lib_string[SONAME], RTLD_LAZY);                     \
156                                         if(lib_handle[SONAME] == NULL) {                                \
157                                                 char perror_msg[PERROR_MSG_MAX];                        \
158                                                 sprintf(perror_msg, "dlopen failed : %s", lib_string[SONAME]);  \
159                                                 perror(perror_msg);                                                     \
160                                                 exit(0);                                                                        \
161                                         }                                                                                               \
162                                 }                                                                                                       \
163                                 FUNCTIONPOINTER = dlsym(lib_handle[SONAME], #FUNCNAME);         \
164                                 if(FUNCTIONPOINTER == NULL || dlerror() != NULL) {      \
165                                         perror("dlsym failed : " #FUNCNAME);                    \
166                                         exit(0);                                                                                \
167                                 }                                                                                                       \
168                                 probeBlockEnd();                                                                        \
169                         }                                                                                                               \
170                 } while(0)
171
172 #define GET_REAL_FUNC(FUNCNAME, SONAME)         \
173                 GET_REAL_FUNCP(FUNCNAME, SONAME, FUNCNAME##p)
174
175 #define GET_REAL_FUNCP_RTLD_NEXT(FUNCNAME, FUNCTIONPOINTER)                     \
176                 do {                                                                                                            \
177                         if(!FUNCTIONPOINTER) {                                                                  \
178                                 probeBlockStart();                                                                      \
179                                 FUNCTIONPOINTER = dlsym(RTLD_NEXT, #FUNCNAME);          \
180                                 if(FUNCTIONPOINTER == NULL || dlerror() != NULL) {      \
181                                         perror("dlsym failed");                                                 \
182                                         exit(0);                                                                                \
183                                 }                                                                                                       \
184                                 probeBlockEnd();                                                                        \
185                         }                                                                                                               \
186                 } while(0)
187
188 #define GET_REAL_FUNC_RTLD_NEXT(FUNCNAME)       \
189                 GET_REAL_FUNCP_RTLD_NEXT(FUNCNAME, FUNCNAME##p)
190
191 #define GET_REAL_FUNCP_RTLD_NEXT_CPP(FUNCNAME, FUNCTIONPOINTER)         \
192                 do {                                                                                                            \
193                         if(!FUNCTIONPOINTER) {                                                                  \
194                                 probeBlockStart();                                                                      \
195                                 void* funcp = dlsym(RTLD_NEXT, #FUNCNAME);                      \
196                                 if(funcp == NULL || dlerror() != NULL) {                        \
197                                         perror("dlsym failed : " #FUNCNAME);                    \
198                                         exit(0);                                                                                \
199                                 }                                                                                                       \
200                                 memcpy(&FUNCTIONPOINTER, &funcp, sizeof(void*));        \
201                                 probeBlockEnd();                                                                        \
202                         }                                                                                                               \
203                 } while(0)
204
205 #define GET_REAL_FUNC_RTLD_NEXT_CPP(FUNCNAME)   \
206                 GET_REAL_FUNCP_RTLD_NEXT_CPP(FUNCNAME, FUNCNAME##p)
207
208 // ======================= pre block macro ================================
209
210 #define PRE_PROBEBLOCK_BEGIN()                                                                                                  \
211         if((blockresult = preBlockBegin(CALLER_ADDRESS, bfiltering, _sopt)) != 0) {     \
212                 setProbePoint(&probeInfo)
213
214 #define PRE_PROBEBLOCK_END()    \
215                 preBlockEnd();                  \
216         }                                                       \
217         olderrno = errno;                       \
218         errno = 0
219
220 #define PRE_PROBEBLOCK() \
221                 PRE_PROBEBLOCK_BEGIN(); \
222                 PRE_PROBEBLOCK_END()
223
224 // ===================== unconditional probe block ========================
225
226 #define PRE_UNCONDITIONAL_BLOCK_BEGIN() \
227         do {                                                            \
228                 probeBlockStart();                              \
229                 setProbePoint(&probeInfo)
230
231 #define PRE_UNCONDITIONAL_BLOCK_END()   \
232                 probeBlockEnd();                                \
233         } while(0)
234
235 #define POST_UNCONDITIONAL_BLOCK_BEGIN(LCTYPE)  \
236         do {                                                                            \
237                 probeBlockStart();                                              \
238                 INIT_LOG;                                                               \
239                 APPEND_LOG_BASIC(LCTYPE)
240                 
241 #define POST_UNCONDITIONAL_BLOCK_END()                  \
242                 printLog(&log, MSG_LOG);                                \
243                 probeBlockEnd();                                                \
244         } while(0)
245
246 // ========================= simplify macro ================================
247
248 #define BEFORE_ORIGINAL(FUNCNAME, LIBNAME)      \
249         DECLARE_VARIABLE_STANDARD;                              \
250         GET_REAL_FUNC(FUNCNAME, LIBNAME);               \
251         PRE_PROBEBLOCK()
252
253 #define BEFORE_ORIGINAL_NOFILTER(FUNCNAME, LIBNAME)     \
254         DECLARE_VARIABLE_STANDARD;                                              \
255         GET_REAL_FUNC(FUNCNAME, LIBNAME);                               \
256         bfiltering = false;                                                             \
257         PRE_PROBEBLOCK()
258
259 #ifdef __cplusplus
260 }
261 #endif
262
263 #endif