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