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