forked from eaccelerator/eaccelerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
351 lines (317 loc) · 10.6 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
+----------------------------------------------------------------------+
| eAccelerator project |
+----------------------------------------------------------------------+
| Copyright (c) 2004 - 2012 eAccelerator |
| http://eaccelerator.net |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| MA 02111-1307, USA. |
| |
| A copy is availble at http://www.gnu.org/copyleft/gpl.txt |
+----------------------------------------------------------------------+
$Id: debug.c 375 2010-01-19 15:49:13Z bart $
*/
#include "eaccelerator.h"
#ifdef HAVE_EACCELERATOR
#include "debug.h"
#include <ctype.h>
#include <stdio.h>
static FILE *F_fp = NULL;
static int file_no = 0;
long ea_debug = 0;
/**
* Init the debug system. This must be called before any debug
* functions are used.
*/
void ea_debug_init (TSRMLS_D)
{
F_fp = fopen (EAG(ea_log_file), "a");
if (!F_fp) {
F_fp = stderr;
}
file_no = fileno(F_fp);
}
/**
* Close the debug system.
*/
void ea_debug_shutdown ()
{
fflush (F_fp);
if (F_fp != stderr) {
fclose (F_fp);
}
F_fp = NULL;
}
/**
* Print a log message that will be print when the debug level is
* equal to EA_LOG. This function is always called even if ea isn't
* compiled with DEBUG and the log level is not equal to EA_LOG.
*/
void ea_debug_log (char *format, ...)
{
if (ea_debug & EA_LOG) {
char output_buf[512];
va_list args;
va_start (args, format);
vsnprintf (output_buf, sizeof (output_buf), format, args);
va_end (args);
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_EX);
}
fputs (output_buf, F_fp);
fflush (F_fp);
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_UN);
}
}
}
/**
* Output an error message to stderr. This message are always printed
* no matter what log level is used.
*/
void ea_debug_error (char *format, ...)
{
char output_buf[512];
va_list args;
va_start (args, format);
vsnprintf (output_buf, sizeof (output_buf), format, args);
va_end (args);
fputs (output_buf, stderr);
fflush (stderr);
}
/*
* All these functions aren't compiled when eA isn't compiled with DEBUG. They
* are replaced with function with no body, so it's optimized away by the compiler.
* Even if the debug level is ok.
*/
/**
* Print a debug message
*/
void ea_debug_printf (long debug_level, char *format, ...)
{
if (ea_debug & debug_level) {
char output_buf[512];
va_list args;
va_start (args, format);
vsnprintf (output_buf, sizeof (output_buf), format, args);
va_end (args);
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_EX);
}
fputs (output_buf, F_fp);
fflush (F_fp);
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_UN);
}
}
}
/**
* Put a debug message
*/
void ea_debug_put (long debug_level, char *message)
{
if (debug_level & ea_debug) {
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_EX);
}
fputs (message, F_fp);
fflush (F_fp);
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_UN);
}
}
}
/**
* Print a binary message
*/
void ea_debug_binary_print (long debug_level, const char *p, int len)
{
if (ea_debug & debug_level) {
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_EX);
}
while (len--) {
if (*p == 0) {
fputs ("\\0", F_fp);
} else {
fputc (*p, F_fp);
}
p++;
}
fputc ('\n', F_fp);
fflush (F_fp);
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_UN);
}
}
}
/**
* Log a hashkey
*/
void ea_debug_log_hashkeys (char *p, HashTable * ht)
{
if (ea_debug & EA_LOG_HASHKEYS) {
Bucket *b;
int i = 0;
b = ht->pListHead;
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_EX);
}
fputs(p, F_fp);
fflush(F_fp);
while (b) {
fprintf (F_fp, "[%d] ", i);
ea_debug_binary_print (EA_LOG_HASHKEYS, b->arKey, b->nKeyLength);
b = b->pListNext;
i++;
}
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_UN);
}
}
}
/**
* Pad the message with the current pad level.
*/
void ea_debug_pad (long debug_level TSRMLS_DC)
{
#ifdef DEBUG /* This ifdef is still req'd because xpad is N/A in a non-debug compile */
if (ea_debug & debug_level) {
int i;
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_EX);
}
i = EAG (xpad);
while (i-- > 0) {
fputc ('\t', F_fp);
}
if (F_fp != stderr) {
EACCELERATOR_FLOCK(file_no, LOCK_UN);
}
}
#endif
}
void ea_debug_start_time (struct timeval *tvstart)
{
gettimeofday (tvstart, NULL);
}
long ea_debug_elapsed_time (struct timeval *tvstart)
{
struct timeval tvend;
int sec, usec;
gettimeofday (&tvend, NULL);
sec = tvend.tv_sec - tvstart->tv_sec;
usec = tvend.tv_usec - tvstart->tv_usec;
return sec * 1000000 + usec;
}
/*
* This dumps a HashTable to debug output. Taken from zend_hash.c and slightly adapted.
*/
void ea_debug_hash_display(HashTable * ht)
{
Bucket *p;
uint i;
fprintf(F_fp, "ht->nTableSize: %d\n", ht->nTableSize);
fprintf(F_fp, "ht->nNumOfElements: %d\n", ht->nNumOfElements);
for (i = 0; i < ht->nTableSize; i++) {
p = ht->arBuckets[i];
while (p != NULL) {
fprintf(F_fp, "\t%s <==> 0x%lX\n", p->arKey, p->h);
p = p->pNext;
}
}
fflush(F_fp);
}
/*
* Dump an eaccelerator class entry structure
*/
void ea_debug_dump_ea_class_entry(ea_class_entry *ce)
{
fprintf(F_fp, "ea class entry: '%s' (len = %u)\n", ce->name, ce->name_length);
fprintf(F_fp, "\tparent: '%s'\n", ce->parent);
fprintf(F_fp, "\ttype: %d\n", ce->type);
fprintf(F_fp, "\tfunction_table: %u entries\n", ce->function_table.nNumOfElements);
fprintf(F_fp, "\tproperties_info: %u entries\n", ce->properties_info.nNumOfElements);
#ifdef ZEND_ENGINE_2_4
fprintf(F_fp, "\tdefault_properties: %u entries\n", ce->default_properties_count);
fprintf(F_fp, "\tdefault_static_members: %u entries\n", ce->default_static_members_count);
#else
fprintf(F_fp, "\tdefault_properties: %u entries\n", ce->default_properties.nNumOfElements);
fprintf(F_fp, "\tdefault_static_members: %u entries\n", ce->default_static_members.nNumOfElements);
fprintf(F_fp, "\tstatic_members: %u entries\n", ce->static_members->nNumOfElements);
#endif
fprintf(F_fp, "\tconstants_Table: %u entries\n", ce->constants_table.nNumOfElements);
fprintf(F_fp, "\tce_flags: %u\n", ce->ce_flags);
fprintf(F_fp, "\tnum_interfaces: %u\n", ce->num_interfaces);
fprintf(F_fp, "\tfilename: %s\n", ce->filename);
fprintf(F_fp, "\tline_start: %u\n", ce->line_start);
fprintf(F_fp, "\tline_end: %u\n", ce->line_end);
# ifdef INCLUDE_DOC_COMMENTS
fprintf(F_fp, "\tdoc_comment: %s\n", ce->doc_comment);
fprintf(F_fp, "\tdoc_comment_len: %u\n", ce->doc_comment_len);
# endif
fflush(F_fp);
}
/*
* Dump a zend class entry structure
*/
void ea_debug_dump_zend_class_entry(zend_class_entry *ce)
{
fprintf(F_fp, "zend class entry: '%s' (len = %u)\n", ce->name, ce->name_length);
fprintf(F_fp, "\tparent: '%s'\n", (ce->parent == NULL) ? "none" : ce->parent->name);
fprintf(F_fp, "\ttype: %d\n", ce->type);
fprintf(F_fp, "\tfunction_table: %u entries\n", ce->function_table.nNumOfElements);
fprintf(F_fp, "\tproperties_info: %u entries\n", ce->properties_info.nNumOfElements);
# ifdef ZEND_ENGINE_2_4
fprintf(F_fp, "\tdefault_properties: %u entries\n", ce->default_properties_count);
fprintf(F_fp, "\tdefault_static_members: %u entries\n", ce->default_static_members_count);
# else
fprintf(F_fp, "\tdefault_properties: %u entries\n", ce->default_properties.nNumOfElements);
fprintf(F_fp, "\tdefault_static_members: %u entries\n", ce->default_static_members.nNumOfElements);
fprintf(F_fp, "\tstatic_members: %u entries\n", ce->static_members->nNumOfElements);
# endif
fprintf(F_fp, "\tconstants_Table: %u entries\n", ce->constants_table.nNumOfElements);
fprintf(F_fp, "\tce_flags: %u\n", ce->ce_flags);
fprintf(F_fp, "\tnum_interfaces: %u\n", ce->num_interfaces);
# ifdef ZEND_ENGINE_2_4
fprintf(F_fp, "\tfilename: %s\n", ce->info.user.filename);
fprintf(F_fp, "\tline_start: %u\n", ce->info.user.line_start);
fprintf(F_fp, "\tline_end: %u\n", ce->info.user.line_end);
# else
fprintf(F_fp, "\tfilename: %s\n", ce->filename);
fprintf(F_fp, "\tline_start: %u\n", ce->line_start);
fprintf(F_fp, "\tline_end: %u\n", ce->line_end);
# endif
# ifdef INCLUDE_DOC_COMMENTS
# ifdef ZEND_ENGINE_2_4
fprintf(F_fp, "\tdoc_comment: %s\n", ce->info.user.doc_comment);
fprintf(F_fp, "\tdoc_comment_len: %u\n", ce->info.user.doc_comment_len);
# else
fprintf(F_fp, "\tdoc_comment: %s\n", ce->doc_comment);
fprintf(F_fp, "\tdoc_comment_len: %u\n", ce->doc_comment_len);
# endif
# endif
fflush(F_fp);
}
#endif /* #ifdef HAVE_EACCELERATOR */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim>600: expandtab sw=4 ts=4 sts=4 fdm=marker
* vim<600: expandtab sw=4 ts=4 sts=4
*/