1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2006-2016 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Georg Richter <georg@mysql.com> |
16 | Andrey Hristov <andrey@mysql.com> |
17 | Ulf Wendel <uwendel@mysql.com> |
18 +----------------------------------------------------------------------+
19 */
20
21 /* $Id$ */
22
23 #ifndef MYSQLND_DEBUG_H
24 #define MYSQLND_DEBUG_H
25
26 #include "mysqlnd_alloc.h"
27 #include "zend_stack.h"
28
29 struct st_mysqlnd_debug_methods
30 {
31 enum_func_status (*open)(MYSQLND_DEBUG * self, zend_bool reopen);
32 void (*set_mode)(MYSQLND_DEBUG * self, const char * const mode);
33 enum_func_status (*log)(MYSQLND_DEBUG * self, unsigned int line, const char * const file,
34 unsigned int level, const char * type, const char *message);
35 enum_func_status (*log_va)(MYSQLND_DEBUG * self, unsigned int line, const char * const file,
36 unsigned int level, const char * type, const char *format, ...);
37 zend_bool (*func_enter)(MYSQLND_DEBUG * self, unsigned int line, const char * const file,
38 const char * const func_name, unsigned int func_name_len);
39 enum_func_status (*func_leave)(MYSQLND_DEBUG * self, unsigned int line, const char * const file, uint64_t call_time);
40 enum_func_status (*close)(MYSQLND_DEBUG * self);
41 enum_func_status (*free_handle)(MYSQLND_DEBUG * self);
42 };
43
44
45 struct st_mysqlnd_debug
46 {
47 php_stream *stream;
48 #ifdef ZTS
49 TSRMLS_D;
50 #endif
51 unsigned int flags;
52 unsigned int nest_level_limit;
53 int pid;
54 char * file_name;
55 zend_stack call_stack;
56 zend_stack call_time_stack;
57 HashTable not_filtered_functions;
58 HashTable function_profiles;
59 struct st_mysqlnd_debug_methods *m;
60 const char ** skip_functions;
61 };
62
63 struct st_mysqlnd_plugin_trace_log
64 {
65 struct st_mysqlnd_plugin_header plugin_header;
66 struct
67 {
68 MYSQLND_DEBUG * (*trace_instance_init)(const char * skip_functions[] TSRMLS_DC);
69 char * (*get_backtrace)(uint max_levels, size_t * length TSRMLS_DC);
70 } methods;
71 };
72
73 void mysqlnd_debug_trace_plugin_register(TSRMLS_D);
74
75 PHPAPI MYSQLND_DEBUG * mysqlnd_debug_init(const char * skip_functions[] TSRMLS_DC);
76
77 PHPAPI char * mysqlnd_get_backtrace(uint max_levels, size_t * length TSRMLS_DC);
78
79 #if defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 1400))
80 #ifdef PHP_WIN32
81 #include "win32/time.h"
82 #elif defined(NETWARE)
83 #include <sys/timeval.h>
84 #include <sys/time.h>
85 #else
86 #include <sys/time.h>
87 #endif
88
89 #ifndef MYSQLND_PROFILING_DISABLED
90 #define DBG_PROFILE_TIMEVAL_TO_DOUBLE(tp) ((tp.tv_sec * 1000000LL)+ tp.tv_usec)
91 #define DBG_PROFILE_START_TIME() gettimeofday(&__dbg_prof_tp, NULL); __dbg_prof_start = DBG_PROFILE_TIMEVAL_TO_DOUBLE(__dbg_prof_tp);
92 #define DBG_PROFILE_END_TIME(duration) gettimeofday(&__dbg_prof_tp, NULL); (duration) = (DBG_PROFILE_TIMEVAL_TO_DOUBLE(__dbg_prof_tp) - __dbg_prof_start);
93 #else
94 #define DBG_PROFILE_TIMEVAL_TO_DOUBLE(tp)
95 #define DBG_PROFILE_START_TIME()
96 #define DBG_PROFILE_END_TIME(duration)
97 #endif
98
99 #define DBG_INF_EX(dbg_obj, msg) do { if (dbg_skip_trace == FALSE && (dbg_obj)) (dbg_obj)->m->log((dbg_obj), __LINE__, __FILE__, -1, "info : ", (msg)); } while (0)
100 #define DBG_ERR_EX(dbg_obj, msg) do { if (dbg_skip_trace == FALSE && (dbg_obj)) (dbg_obj)->m->log((dbg_obj), __LINE__, __FILE__, -1, "error: ", (msg)); } while (0)
101 #define DBG_INF_FMT_EX(dbg_obj, ...) do { if (dbg_skip_trace == FALSE && (dbg_obj)) (dbg_obj)->m->log_va((dbg_obj), __LINE__, __FILE__, -1, "info : ", __VA_ARGS__); } while (0)
102 #define DBG_ERR_FMT_EX(dbg_obj, ...) do { if (dbg_skip_trace == FALSE && (dbg_obj)) (dbg_obj)->m->log_va((dbg_obj), __LINE__, __FILE__, -1, "error: ", __VA_ARGS__); } while (0)
103
104 #define DBG_BLOCK_ENTER_EX(dbg_obj, block_name) DBG_BLOCK_ENTER_EX2((dbg_obj), (MYSQLND_DEBUG *) NULL, (block_name))
105 #define DBG_BLOCK_LEAVE_EX(dbg_obj) DBG_BLOCK_LEAVE_EX2((dbg_obj), (MYSQLND_DEBUG *) NULL)
106
107 #define DBG_BLOCK_ENTER_EX2(dbg_obj1, dbg_obj2, block_name) \
108 { \
109 DBG_ENTER_EX2((dbg_obj1), (dbg_obj2), (block_name));
110
111 #define DBG_BLOCK_LEAVE_EX2(dbg_obj1, dbg_obj2) \
112 DBG_LEAVE_EX2((dbg_obj1), (dbg_obj2), ;) \
113 } \
114
115
116 #define DBG_ENTER_EX(dbg_obj, func_name) DBG_ENTER_EX2((dbg_obj), (MYSQLND_DEBUG *) NULL, (func_name))
117 #define DBG_LEAVE_EX(dbg_obj, leave) DBG_LEAVE_EX2((dbg_obj), (MYSQLND_DEBUG *) NULL, leave)
118
119 #define DBG_ENTER_EX2(dbg_obj1, dbg_obj2, func_name) \
120 struct timeval __dbg_prof_tp = {0}; \
121 uint64_t __dbg_prof_start = 0; /* initialization is needed */ \
122 zend_bool dbg_skip_trace = TRUE; \
123 if ((dbg_obj1)) { \
124 dbg_skip_trace = !(dbg_obj1)->m->func_enter((dbg_obj1), __LINE__, __FILE__, func_name, strlen(func_name)); \
125 } \
126 if ((dbg_obj2)) { \
127 dbg_skip_trace |= !(dbg_obj2)->m->func_enter((dbg_obj2), __LINE__, __FILE__, func_name, strlen(func_name)); \
128 } \
129 if (dbg_skip_trace); /* shut compiler's mouth */\
130 do { \
131 if (((dbg_obj1) && (dbg_obj1)->flags & MYSQLND_DEBUG_PROFILE_CALLS) || \
132 ((dbg_obj2) && (dbg_obj2)->flags & MYSQLND_DEBUG_PROFILE_CALLS)) \
133 { \
134 DBG_PROFILE_START_TIME(); \
135 } \
136 } while (0);
137
138 #define DBG_LEAVE_EX2(dbg_obj1, dbg_obj2, leave) \
139 do {\
140 uint64_t this_call_duration = 0; \
141 if (((dbg_obj1) && (dbg_obj1)->flags & MYSQLND_DEBUG_PROFILE_CALLS) || \
142 ((dbg_obj2) && (dbg_obj2)->flags & MYSQLND_DEBUG_PROFILE_CALLS)) \
143 { \
144 DBG_PROFILE_END_TIME(this_call_duration); \
145 } \
146 if ((dbg_obj1)) { \
147 (dbg_obj1)->m->func_leave((dbg_obj1), __LINE__, __FILE__, this_call_duration); \
148 } \
149 if ((dbg_obj2)) { \
150 (dbg_obj2)->m->func_leave((dbg_obj2), __LINE__, __FILE__, this_call_duration); \
151 } \
152 leave \
153 } while (0);
154
155
156 #define DBG_RETURN_EX(dbg_obj, value) DBG_LEAVE_EX((dbg_obj), return (value);)
157 #define DBG_VOID_RETURN_EX(dbg_obj) DBG_LEAVE_EX((dbg_obj), return;)
158
159 #define DBG_RETURN_EX2(dbg_obj1, dbg_obj2, value) DBG_LEAVE_EX2((dbg_obj1), (dbg_obj2), return (value);)
160 #define DBG_VOID_RETURN_EX2(dbg_obj1, dbg_obj2) DBG_LEAVE_EX2((dbg_obj1), (dbg_obj2), return;)
161
162
163
164 #else /* defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 1400)) */
DBG_INF_EX(MYSQLND_DEBUG * dbg_obj,const char * const msg)165 static inline void DBG_INF_EX(MYSQLND_DEBUG * dbg_obj, const char * const msg) {}
DBG_ERR_EX(MYSQLND_DEBUG * dbg_obj,const char * const msg)166 static inline void DBG_ERR_EX(MYSQLND_DEBUG * dbg_obj, const char * const msg) {}
DBG_INF_FMT_EX(MYSQLND_DEBUG * dbg_obj,...)167 static inline void DBG_INF_FMT_EX(MYSQLND_DEBUG * dbg_obj, ...) {}
DBG_ERR_FMT_EX(MYSQLND_DEBUG * dbg_obj,...)168 static inline void DBG_ERR_FMT_EX(MYSQLND_DEBUG * dbg_obj, ...) {}
DBG_ENTER_EX(MYSQLND_DEBUG * dbg_obj,const char * const func_name)169 static inline void DBG_ENTER_EX(MYSQLND_DEBUG * dbg_obj, const char * const func_name) {}
170 #define DBG_BLOCK_ENTER(bname) {
171 #define DBG_RETURN_EX(dbg_obj, value) return (value)
172 #define DBG_VOID_RETURN_EX(dbg_obj) return
173 #define DBG_BLOCK_LEAVE_EX(dbg_obj) }
174
175 #endif
176
177 #if MYSQLND_DBG_ENABLED == 1
178
179 #define DBG_INF(msg) DBG_INF_EX(MYSQLND_G(dbg), (msg))
180 #define DBG_ERR(msg) DBG_ERR_EX(MYSQLND_G(dbg), (msg))
181 #define DBG_INF_FMT(...) DBG_INF_FMT_EX(MYSQLND_G(dbg), __VA_ARGS__)
182 #define DBG_ERR_FMT(...) DBG_ERR_FMT_EX(MYSQLND_G(dbg), __VA_ARGS__)
183
184 #define DBG_ENTER(func_name) DBG_ENTER_EX(MYSQLND_G(dbg), (func_name))
185 #define DBG_BLOCK_ENTER(bname) DBG_BLOCK_ENTER_EX(MYSQLND_G(dbg), (bname))
186 #define DBG_RETURN(value) DBG_RETURN_EX(MYSQLND_G(dbg), (value))
187 #define DBG_VOID_RETURN DBG_VOID_RETURN_EX(MYSQLND_G(dbg))
188 #define DBG_BLOCK_LEAVE DBG_BLOCK_LEAVE_EX(MYSQLND_G(dbg))
189
190
191 #define TRACE_ALLOC_INF(msg) DBG_INF_EX(MYSQLND_G(trace_alloc), (msg))
192 #define TRACE_ALLOC_ERR(msg) DBG_ERR_EX(MYSQLND_G(trace_alloc), (msg))
193 #define TRACE_ALLOC_INF_FMT(...) DBG_INF_FMT_EX(MYSQLND_G(trace_alloc), __VA_ARGS__)
194 #define TRACE_ALLOC_ERR_FMT(...) DBG_ERR_FMT_EX(MYSQLND_G(trace_alloc), __VA_ARGS__)
195
196 #define TRACE_ALLOC_ENTER(func_name) DBG_ENTER_EX2(MYSQLND_G(dbg), MYSQLND_G(trace_alloc), (func_name))
197 #define TRACE_ALLOC_BLOCK_ENTER(bname) DBG_BLOCK_ENTER_EX2(MYSQLND_G(dbg), MYSQLND_G(trace_alloc), (bname))
198 #define TRACE_ALLOC_RETURN(value) DBG_RETURN_EX2(MYSQLND_G(dbg), MYSQLND_G(trace_alloc), (value))
199 #define TRACE_ALLOC_VOID_RETURN DBG_VOID_RETURN_EX2(MYSQLND_G(dbg), MYSQLND_G(trace_alloc))
200 #define TRACE_ALLOC_BLOCK_LEAVE DBG_BLOCK_LEAVE_EX2(MYSQLND_G(dbg), MYSQLND_G(trace_alloc))
201
202 #elif MYSQLND_DBG_ENABLED == 0
203
DBG_INF(const char * const msg)204 static inline void DBG_INF(const char * const msg) {}
DBG_ERR(const char * const msg)205 static inline void DBG_ERR(const char * const msg) {}
DBG_INF_FMT(const char * const format,...)206 static inline void DBG_INF_FMT(const char * const format, ...) {}
DBG_ERR_FMT(const char * const format,...)207 static inline void DBG_ERR_FMT(const char * const format, ...) {}
DBG_ENTER(const char * const func_name)208 static inline void DBG_ENTER(const char * const func_name) {}
209 #define DBG_BLOCK_ENTER(bname) {
210 #define DBG_RETURN(value) return (value)
211 #define DBG_VOID_RETURN return
212 #define DBG_BLOCK_LEAVE }
213
214
TRACE_ALLOC_INF(const char * const msg)215 static inline void TRACE_ALLOC_INF(const char * const msg) {}
TRACE_ALLOC_ERR(const char * const msg)216 static inline void TRACE_ALLOC_ERR(const char * const msg) {}
TRACE_ALLOC_INF_FMT(const char * const format,...)217 static inline void TRACE_ALLOC_INF_FMT(const char * const format, ...) {}
TRACE_ALLOC_ERR_FMT(const char * const format,...)218 static inline void TRACE_ALLOC_ERR_FMT(const char * const format, ...) {}
TRACE_ALLOC_ENTER(const char * const func_name)219 static inline void TRACE_ALLOC_ENTER(const char * const func_name) {}
220 #define TRACE_ALLOC_BLOCK_ENTER(bname) {
221 #define TRACE_ALLOC_RETURN(value) return (value)
222 #define TRACE_ALLOC_VOID_RETURN return
223 #define TRACE_ALLOC_BLOCK_LEAVE }
224
225 #endif
226
227 #endif /* MYSQLND_DEBUG_H */
228
229 /*
230 * Local variables:
231 * tab-width: 4
232 * c-basic-offset: 4
233 * End:
234 * vim600: noet sw=4 ts=4 fdm=marker
235 * vim<600: noet sw=4 ts=4
236 */
237