-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathmysqlnd_debug.c
1706 lines (1490 loc) · 46.8 KB
/
mysqlnd_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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Georg Richter <georg@mysql.com> |
| Andrey Hristov <andrey@mysql.com> |
| Ulf Wendel <uwendel@mysql.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_debug.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_statistics.h"
#include "zend_builtin_functions.h"
static const char * const mysqlnd_debug_default_trace_file = "/tmp/mysqlnd.trace";
#ifdef ZTS
#define MYSQLND_ZTS(self) TSRMLS_D = (self)->TSRMLS_C
#else
#define MYSQLND_ZTS(self)
#endif
#define MYSQLND_DEBUG_DUMP_TIME 1
#define MYSQLND_DEBUG_DUMP_TRACE 2
#define MYSQLND_DEBUG_DUMP_PID 4
#define MYSQLND_DEBUG_DUMP_LINE 8
#define MYSQLND_DEBUG_DUMP_FILE 16
#define MYSQLND_DEBUG_DUMP_LEVEL 32
#define MYSQLND_DEBUG_APPEND 64
#define MYSQLND_DEBUG_FLUSH 128
#define MYSQLND_DEBUG_TRACE_MEMORY_CALLS 256
static const char mysqlnd_emalloc_name[] = "_mysqlnd_emalloc";
static const char mysqlnd_pemalloc_name[] = "_mysqlnd_pemalloc";
static const char mysqlnd_ecalloc_name[] = "_mysqlnd_ecalloc";
static const char mysqlnd_pecalloc_name[] = "_mysqlnd_pecalloc";
static const char mysqlnd_erealloc_name[] = "_mysqlnd_erealloc";
static const char mysqlnd_perealloc_name[] = "_mysqlnd_perealloc";
static const char mysqlnd_efree_name[] = "_mysqlnd_efree";
static const char mysqlnd_pefree_name[] = "_mysqlnd_pefree";
static const char mysqlnd_malloc_name[] = "_mysqlnd_malloc";
static const char mysqlnd_calloc_name[] = "_mysqlnd_calloc";
static const char mysqlnd_realloc_name[] = "_mysqlnd_realloc";
static const char mysqlnd_free_name[] = "_mysqlnd_free";
static const char mysqlnd_pestrndup_name[] = "_mysqlnd_pestrndup";
static const char mysqlnd_pestrdup_name[] = "_mysqlnd_pestrdup";
const char * mysqlnd_debug_std_no_trace_funcs[] =
{
mysqlnd_emalloc_name,
mysqlnd_ecalloc_name,
mysqlnd_efree_name,
mysqlnd_erealloc_name,
mysqlnd_pemalloc_name,
mysqlnd_pecalloc_name,
mysqlnd_pefree_name,
mysqlnd_perealloc_name,
mysqlnd_malloc_name,
mysqlnd_calloc_name,
mysqlnd_realloc_name,
mysqlnd_free_name,
mysqlnd_pestrndup_name,
mysqlnd_read_header_name,
mysqlnd_read_body_name,
NULL /* must be always last */
};
/* {{{ mysqlnd_debug::open */
static enum_func_status
MYSQLND_METHOD(mysqlnd_debug, open)(MYSQLND_DEBUG * self, zend_bool reopen)
{
MYSQLND_ZTS(self);
if (!self->file_name) {
return FAIL;
}
self->stream = php_stream_open_wrapper(self->file_name,
reopen == TRUE || self->flags & MYSQLND_DEBUG_APPEND? "ab":"wb",
REPORT_ERRORS, NULL);
return self->stream? PASS:FAIL;
}
/* }}} */
/* {{{ mysqlnd_debug::log */
static enum_func_status
MYSQLND_METHOD(mysqlnd_debug, log)(MYSQLND_DEBUG * self,
unsigned int line, const char * const file,
unsigned int level, const char * type, const char * message)
{
char pipe_buffer[512];
enum_func_status ret;
int i;
char * message_line;
unsigned int message_line_len;
unsigned int flags = self->flags;
char pid_buffer[10], time_buffer[30], file_buffer[200],
line_buffer[6], level_buffer[7];
MYSQLND_ZTS(self);
if (!self->stream && FAIL == self->m->open(self, FALSE)) {
return FAIL;
}
if (level == -1) {
level = zend_stack_count(&self->call_stack);
}
i = MIN(level, sizeof(pipe_buffer) / 2 - 1);
pipe_buffer[i*2] = '\0';
for (;i > 0;i--) {
pipe_buffer[i*2 - 1] = ' ';
pipe_buffer[i*2 - 2] = '|';
}
if (flags & MYSQLND_DEBUG_DUMP_PID) {
snprintf(pid_buffer, sizeof(pid_buffer) - 1, "%5u: ", self->pid);
pid_buffer[sizeof(pid_buffer) - 1 ] = '\0';
}
if (flags & MYSQLND_DEBUG_DUMP_TIME) {
/* The following from FF's DBUG library, which is in the public domain */
#if defined(PHP_WIN32)
/* FIXME This doesn't give microseconds as in Unix case, and the resolution is
in system ticks, 10 ms intervals. See my_getsystime.c for high res */
SYSTEMTIME loc_t;
GetLocalTime(&loc_t);
snprintf(time_buffer, sizeof(time_buffer) - 1,
/* "%04d-%02d-%02d " */
"%02d:%02d:%02d.%06d ",
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
#else
struct timeval tv;
struct tm *tm_p;
if (gettimeofday(&tv, NULL) != -1) {
if ((tm_p= localtime((const time_t *)&tv.tv_sec))) {
snprintf(time_buffer, sizeof(time_buffer) - 1,
/* "%04d-%02d-%02d " */
"%02d:%02d:%02d.%06d ",
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
(int) (tv.tv_usec));
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
}
}
#endif
}
if (flags & MYSQLND_DEBUG_DUMP_FILE) {
snprintf(file_buffer, sizeof(file_buffer) - 1, "%14s: ", file);
file_buffer[sizeof(file_buffer) - 1 ] = '\0';
}
if (flags & MYSQLND_DEBUG_DUMP_LINE) {
snprintf(line_buffer, sizeof(line_buffer) - 1, "%5u: ", line);
line_buffer[sizeof(line_buffer) - 1 ] = '\0';
}
if (flags & MYSQLND_DEBUG_DUMP_LEVEL) {
snprintf(level_buffer, sizeof(level_buffer) - 1, "%4u: ", level);
level_buffer[sizeof(level_buffer) - 1 ] = '\0';
}
message_line_len = spprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
flags & MYSQLND_DEBUG_DUMP_LINE? line_buffer:"",
flags & MYSQLND_DEBUG_DUMP_LEVEL? level_buffer:"",
pipe_buffer, type? type:"", message);
ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
efree(message_line); /* allocated by spprintf */
if (flags & MYSQLND_DEBUG_FLUSH) {
self->m->close(self);
self->m->open(self, TRUE);
}
return ret;
}
/* }}} */
/* {{{ mysqlnd_debug::log_va */
static enum_func_status
MYSQLND_METHOD(mysqlnd_debug, log_va)(MYSQLND_DEBUG *self,
unsigned int line, const char * const file,
unsigned int level, const char * type,
const char *format, ...)
{
char pipe_buffer[512];
int i;
enum_func_status ret;
char * message_line, *buffer;
unsigned int message_line_len;
va_list args;
unsigned int flags = self->flags;
char pid_buffer[10], time_buffer[30], file_buffer[200],
line_buffer[6], level_buffer[7];
MYSQLND_ZTS(self);
if (!self->stream && FAIL == self->m->open(self, FALSE)) {
return FAIL;
}
if (level == -1) {
level = zend_stack_count(&self->call_stack);
}
i = MIN(level, sizeof(pipe_buffer) / 2 - 1);
pipe_buffer[i*2] = '\0';
for (;i > 0;i--) {
pipe_buffer[i*2 - 1] = ' ';
pipe_buffer[i*2 - 2] = '|';
}
if (flags & MYSQLND_DEBUG_DUMP_PID) {
snprintf(pid_buffer, sizeof(pid_buffer) - 1, "%5u: ", self->pid);
pid_buffer[sizeof(pid_buffer) - 1 ] = '\0';
}
if (flags & MYSQLND_DEBUG_DUMP_TIME) {
/* The following from FF's DBUG library, which is in the public domain */
#if defined(PHP_WIN32)
/* FIXME This doesn't give microseconds as in Unix case, and the resolution is
in system ticks, 10 ms intervals. See my_getsystime.c for high res */
SYSTEMTIME loc_t;
GetLocalTime(&loc_t);
snprintf(time_buffer, sizeof(time_buffer) - 1,
/* "%04d-%02d-%02d " */
"%02d:%02d:%02d.%06d ",
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
#else
struct timeval tv;
struct tm *tm_p;
if (gettimeofday(&tv, NULL) != -1) {
if ((tm_p= localtime((const time_t *)&tv.tv_sec))) {
snprintf(time_buffer, sizeof(time_buffer) - 1,
/* "%04d-%02d-%02d " */
"%02d:%02d:%02d.%06d ",
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
(int) (tv.tv_usec));
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
}
}
#endif
}
if (flags & MYSQLND_DEBUG_DUMP_FILE) {
snprintf(file_buffer, sizeof(file_buffer) - 1, "%14s: ", file);
file_buffer[sizeof(file_buffer) - 1 ] = '\0';
}
if (flags & MYSQLND_DEBUG_DUMP_LINE) {
snprintf(line_buffer, sizeof(line_buffer) - 1, "%5u: ", line);
line_buffer[sizeof(line_buffer) - 1 ] = '\0';
}
if (flags & MYSQLND_DEBUG_DUMP_LEVEL) {
snprintf(level_buffer, sizeof(level_buffer) - 1, "%4u: ", level);
level_buffer[sizeof(level_buffer) - 1 ] = '\0';
}
va_start(args, format);
vspprintf(&buffer, 0, format, args);
va_end(args);
message_line_len = spprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
flags & MYSQLND_DEBUG_DUMP_LINE? line_buffer:"",
flags & MYSQLND_DEBUG_DUMP_LEVEL? level_buffer:"",
pipe_buffer, type? type:"", buffer);
efree(buffer);
ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
efree(message_line); /* allocated by spprintf */
if (flags & MYSQLND_DEBUG_FLUSH) {
self->m->close(self);
self->m->open(self, TRUE);
}
return ret;
}
/* }}} */
/* FALSE - The DBG_ calls won't be traced, TRUE - will be traced */
/* {{{ mysqlnd_res_meta::func_enter */
static zend_bool
MYSQLND_METHOD(mysqlnd_debug, func_enter)(MYSQLND_DEBUG * self,
unsigned int line, const char * const file,
const char * const func_name, unsigned int func_name_len)
{
if ((self->flags & MYSQLND_DEBUG_DUMP_TRACE) == 0 || self->file_name == NULL) {
return FALSE;
}
if ((uint) zend_stack_count(&self->call_stack) >= self->nest_level_limit) {
return FALSE;
}
if ((self->flags & MYSQLND_DEBUG_TRACE_MEMORY_CALLS) == 0 && self->skip_functions) {
const char ** p = self->skip_functions;
while (*p) {
if (*p == func_name) {
zend_stack_push(&self->call_stack, "", sizeof(""));
return FALSE;
}
p++;
}
}
zend_stack_push(&self->call_stack, func_name, func_name_len + 1);
if (zend_hash_num_elements(&self->not_filtered_functions) &&
0 == zend_hash_exists(&self->not_filtered_functions, func_name, strlen(func_name) + 1))
{
return FALSE;
}
self->m->log_va(self, line, file, zend_stack_count(&self->call_stack) - 1, NULL, ">%s", func_name);
return TRUE;
}
/* }}} */
/* {{{ mysqlnd_res_meta::func_leave */
static enum_func_status
MYSQLND_METHOD(mysqlnd_debug, func_leave)(MYSQLND_DEBUG * self, unsigned int line, const char * const file)
{
char *func_name;
if ((self->flags & MYSQLND_DEBUG_DUMP_TRACE) == 0 || self->file_name == NULL) {
return PASS;
}
if ((uint) zend_stack_count(&self->call_stack) >= self->nest_level_limit) {
return PASS;
}
zend_stack_top(&self->call_stack, (void **)&func_name);
if (func_name[0] == '\0') {
; /* don't log that function */
} else if (!zend_hash_num_elements(&self->not_filtered_functions) ||
1 == zend_hash_exists(&self->not_filtered_functions, func_name, strlen(func_name) + 1))
{
self->m->log_va(self, line, file, zend_stack_count(&self->call_stack) - 1, NULL, "<%s", func_name);
}
return zend_stack_del_top(&self->call_stack) == SUCCESS? PASS:FAIL;
}
/* }}} */
/* {{{ mysqlnd_res_meta::close */
static enum_func_status
MYSQLND_METHOD(mysqlnd_debug, close)(MYSQLND_DEBUG * self)
{
MYSQLND_ZTS(self);
if (self->stream) {
php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE);
self->stream = NULL;
}
return PASS;
}
/* }}} */
/* {{{ mysqlnd_res_meta::free */
static enum_func_status
MYSQLND_METHOD(mysqlnd_debug, free)(MYSQLND_DEBUG * self)
{
if (self->file_name && self->file_name != mysqlnd_debug_default_trace_file) {
efree(self->file_name);
self->file_name = NULL;
}
zend_stack_destroy(&self->call_stack);
zend_hash_destroy(&self->not_filtered_functions);
efree(self);
return PASS;
}
/* }}} */
enum mysqlnd_debug_parser_state
{
PARSER_WAIT_MODIFIER,
PARSER_WAIT_COLON,
PARSER_WAIT_VALUE
};
/* {{{ mysqlnd_res_meta::set_mode */
static void
MYSQLND_METHOD(mysqlnd_debug, set_mode)(MYSQLND_DEBUG * self, const char * const mode)
{
unsigned int mode_len = strlen(mode), i;
enum mysqlnd_debug_parser_state state = PARSER_WAIT_MODIFIER;
self->flags = 0;
self->nest_level_limit = 0;
if (self->file_name && self->file_name != mysqlnd_debug_default_trace_file) {
efree(self->file_name);
self->file_name = NULL;
}
if (zend_hash_num_elements(&self->not_filtered_functions)) {
zend_hash_destroy(&self->not_filtered_functions);
zend_hash_init(&self->not_filtered_functions, 0, NULL, NULL, 0);
}
for (i = 0; i < mode_len; i++) {
switch (mode[i]) {
case 'O':
case 'A':
self->flags |= MYSQLND_DEBUG_FLUSH;
case 'a':
case 'o':
if (mode[i] == 'a' || mode[i] == 'A') {
self->flags |= MYSQLND_DEBUG_APPEND;
}
if (i + 1 < mode_len && mode[i+1] == ',') {
unsigned int j = i + 2;
while (j < mode_len) {
if (mode[j] == ':') {
break;
}
j++;
}
if (j > i + 2) {
self->file_name = estrndup(mode + i + 2, j - i - 2);
}
i = j;
} else {
if (!self->file_name)
self->file_name = (char *) mysqlnd_debug_default_trace_file;
}
state = PARSER_WAIT_COLON;
break;
case ':':
#if 0
if (state != PARSER_WAIT_COLON) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Consecutive semicolons at position %u", i);
}
#endif
state = PARSER_WAIT_MODIFIER;
break;
case 'f': /* limit output to these functions */
if (i + 1 < mode_len && mode[i+1] == ',') {
unsigned int j = i + 2;
i++;
while (j < mode_len) {
if (mode[j] == ':') {
/* function names with :: */
if ((j + 1 < mode_len) && mode[j+1] == ':') {
j += 2;
continue;
}
}
if (mode[j] == ',' || mode[j] == ':') {
if (j > i + 2) {
char func_name[1024];
unsigned int func_name_len = MIN(sizeof(func_name) - 1, j - i - 1);
memcpy(func_name, mode + i + 1, func_name_len);
func_name[func_name_len] = '\0';
zend_hash_add_empty_element(&self->not_filtered_functions,
func_name, func_name_len + 1);
i = j;
}
if (mode[j] == ':') {
break;
}
}
j++;
}
i = j;
} else {
#if 0
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Expected list of functions for '%c' found none", mode[i]);
#endif
}
state = PARSER_WAIT_COLON;
break;
case 'D':
case 'd':
case 'g':
case 'p':
/* unsupported */
if ((i + 1) < mode_len && mode[i+1] == ',') {
i+= 2;
while (i < mode_len) {
if (mode[i] == ':') {
break;
}
i++;
}
}
state = PARSER_WAIT_COLON;
break;
case 'F':
self->flags |= MYSQLND_DEBUG_DUMP_FILE;
state = PARSER_WAIT_COLON;
break;
case 'i':
self->flags |= MYSQLND_DEBUG_DUMP_PID;
state = PARSER_WAIT_COLON;
break;
case 'L':
self->flags |= MYSQLND_DEBUG_DUMP_LINE;
state = PARSER_WAIT_COLON;
break;
case 'n':
self->flags |= MYSQLND_DEBUG_DUMP_LEVEL;
state = PARSER_WAIT_COLON;
break;
case 't':
if (mode[i+1] == ',') {
unsigned int j = i + 2;
while (j < mode_len) {
if (mode[j] == ':') {
break;
}
j++;
}
if (j > i + 2) {
char *value_str = estrndup(mode + i + 2, j - i - 2);
self->nest_level_limit = atoi(value_str);
efree(value_str);
}
i = j;
} else {
self->nest_level_limit = 200; /* default value for FF DBUG */
}
self->flags |= MYSQLND_DEBUG_DUMP_TRACE;
state = PARSER_WAIT_COLON;
break;
case 'T':
self->flags |= MYSQLND_DEBUG_DUMP_TIME;
state = PARSER_WAIT_COLON;
break;
case 'N':
case 'P':
case 'r':
case 'S':
state = PARSER_WAIT_COLON;
break;
case 'm': /* mysqlnd extension - trace memory functions */
self->flags |= MYSQLND_DEBUG_TRACE_MEMORY_CALLS;
state = PARSER_WAIT_COLON;
break;
default:
if (state == PARSER_WAIT_MODIFIER) {
#if 0
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecognized format '%c'", mode[i]);
#endif
if (i+1 < mode_len && mode[i+1] == ',') {
i+= 2;
while (i < mode_len) {
if (mode[i] == ':') {
break;
}
i++;
}
}
state = PARSER_WAIT_COLON;
} else if (state == PARSER_WAIT_COLON) {
#if 0
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Colon expected, '%c' found", mode[i]);
#endif
}
break;
}
}
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_debug)
MYSQLND_METHOD(mysqlnd_debug, open),
MYSQLND_METHOD(mysqlnd_debug, set_mode),
MYSQLND_METHOD(mysqlnd_debug, log),
MYSQLND_METHOD(mysqlnd_debug, log_va),
MYSQLND_METHOD(mysqlnd_debug, func_enter),
MYSQLND_METHOD(mysqlnd_debug, func_leave),
MYSQLND_METHOD(mysqlnd_debug, close),
MYSQLND_METHOD(mysqlnd_debug, free),
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_debug_init */
PHPAPI MYSQLND_DEBUG *
mysqlnd_debug_init(const char * skip_functions[] TSRMLS_DC)
{
MYSQLND_DEBUG *ret = ecalloc(1, sizeof(MYSQLND_DEBUG));
#ifdef ZTS
ret->TSRMLS_C = TSRMLS_C;
#endif
ret->nest_level_limit = 0;
ret->pid = getpid();
zend_stack_init(&ret->call_stack);
zend_hash_init(&ret->not_filtered_functions, 0, NULL, NULL, 0);
ret->m = & mysqlnd_mysqlnd_debug_methods;
ret->skip_functions = skip_functions;
return ret;
}
/* }}} */
/* {{{ _mysqlnd_debug */
PHPAPI void _mysqlnd_debug(const char * mode TSRMLS_DC)
{
#if PHP_DEBUG
MYSQLND_DEBUG *dbg = MYSQLND_G(dbg);
if (!dbg) {
MYSQLND_G(dbg) = dbg = mysqlnd_debug_init(mysqlnd_debug_std_no_trace_funcs TSRMLS_CC);
if (!dbg) {
return;
}
}
dbg->m->close(dbg);
dbg->m->set_mode(dbg, mode);
while (zend_stack_count(&dbg->call_stack)) {
zend_stack_del_top(&dbg->call_stack);
}
#endif
}
/* }}} */
#if ZEND_DEBUG
#else
#define __zend_filename "/unknown/unknown"
#define __zend_lineno 0
#endif
#define REAL_SIZE(s) (collect_memory_statistics? (s) + sizeof(size_t) : (s))
#define REAL_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) - sizeof(size_t)) : (p))
#define FAKE_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) + sizeof(size_t)) : (p))
/* {{{ _mysqlnd_emalloc */
void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = &MYSQLND_G(debug_emalloc_fail_threshold);
DBG_ENTER(mysqlnd_emalloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = emalloc(REAL_SIZE(size));
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_EMALLOC_COUNT, 1, STAT_MEM_EMALLOC_AMOUNT, size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_pemalloc */
void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = persistent? &MYSQLND_G(debug_malloc_fail_threshold):&MYSQLND_G(debug_emalloc_fail_threshold);
DBG_ENTER(mysqlnd_pemalloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = pemalloc(REAL_SIZE(size), persistent);
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("size=%lu ptr=%p persistent=%u", size, ret, persistent);
if (ret && collect_memory_statistics) {
enum mysqlnd_collected_stats s1 = persistent? STAT_MEM_MALLOC_COUNT:STAT_MEM_EMALLOC_COUNT;
enum mysqlnd_collected_stats s2 = persistent? STAT_MEM_MALLOC_AMOUNT:STAT_MEM_EMALLOC_AMOUNT;
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_ecalloc */
void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = &MYSQLND_G(debug_ecalloc_fail_threshold);
DBG_ENTER(mysqlnd_ecalloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
DBG_INF_FMT("before: %lu", zend_memory_usage(FALSE TSRMLS_CC));
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = ecalloc(nmemb, REAL_SIZE(size));
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("after : %lu", zend_memory_usage(FALSE TSRMLS_CC));
DBG_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_ECALLOC_COUNT, 1, STAT_MEM_ECALLOC_AMOUNT, size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_pecalloc */
void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = persistent? &MYSQLND_G(debug_calloc_fail_threshold):&MYSQLND_G(debug_ecalloc_fail_threshold);
DBG_ENTER(mysqlnd_pecalloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = pecalloc(nmemb, REAL_SIZE(size), persistent);
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
enum mysqlnd_collected_stats s1 = persistent? STAT_MEM_CALLOC_COUNT:STAT_MEM_ECALLOC_COUNT;
enum mysqlnd_collected_stats s2 = persistent? STAT_MEM_CALLOC_AMOUNT:STAT_MEM_ECALLOC_AMOUNT;
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_erealloc */
void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0;
long * threshold = &MYSQLND_G(debug_erealloc_fail_threshold);
DBG_ENTER(mysqlnd_erealloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
DBG_INF_FMT("ptr=%p old_size=%lu, new_size=%lu", ptr, old_size, new_size);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = erealloc(REAL_PTR(ptr), REAL_SIZE(new_size));
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("new_ptr=%p", (char*)ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = new_size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_EREALLOC_COUNT, 1, STAT_MEM_EREALLOC_AMOUNT, new_size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_perealloc */
void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0;
long * threshold = persistent? &MYSQLND_G(debug_realloc_fail_threshold):&MYSQLND_G(debug_erealloc_fail_threshold);
DBG_ENTER(mysqlnd_perealloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
DBG_INF_FMT("ptr=%p old_size=%lu new_size=%lu persistent=%u", ptr, old_size, new_size, persistent);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = perealloc(REAL_PTR(ptr), REAL_SIZE(new_size), persistent);
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("new_ptr=%p", (char*)ret);
if (ret && collect_memory_statistics) {
enum mysqlnd_collected_stats s1 = persistent? STAT_MEM_REALLOC_COUNT:STAT_MEM_EREALLOC_COUNT;
enum mysqlnd_collected_stats s2 = persistent? STAT_MEM_REALLOC_AMOUNT:STAT_MEM_EREALLOC_AMOUNT;
*(size_t *) ret = new_size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, new_size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_efree */
void _mysqlnd_efree(void *ptr MYSQLND_MEM_D)
{
size_t free_amount = 0;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
DBG_ENTER(mysqlnd_efree_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
DBG_INF_FMT("ptr=%p", ptr);
if (ptr) {
if (collect_memory_statistics) {
free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
DBG_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
}
efree(REAL_PTR(ptr));
}
if (collect_memory_statistics) {
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_EFREE_COUNT, 1, STAT_MEM_EFREE_AMOUNT, free_amount);
}
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ _mysqlnd_pefree */
void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
{
size_t free_amount = 0;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
DBG_ENTER(mysqlnd_pefree_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
DBG_INF_FMT("ptr=%p persistent=%u", ptr, persistent);
if (ptr) {
if (collect_memory_statistics) {
free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
DBG_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
}
pefree(REAL_PTR(ptr), persistent);
}
if (collect_memory_statistics) {
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(persistent? STAT_MEM_FREE_COUNT:STAT_MEM_EFREE_COUNT, 1,
persistent? STAT_MEM_FREE_AMOUNT:STAT_MEM_EFREE_AMOUNT, free_amount);
}
DBG_VOID_RETURN;
}
/* {{{ _mysqlnd_malloc */
void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = &MYSQLND_G(debug_malloc_fail_threshold);
DBG_ENTER(mysqlnd_malloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = malloc(REAL_SIZE(size));
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_MALLOC_COUNT, 1, STAT_MEM_MALLOC_AMOUNT, size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_calloc */
void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = &MYSQLND_G(debug_calloc_fail_threshold);
DBG_ENTER(mysqlnd_calloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = calloc(nmemb, REAL_SIZE(size));
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_CALLOC_COUNT, 1, STAT_MEM_CALLOC_AMOUNT, size);
}
DBG_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_realloc */
void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
long * threshold = &MYSQLND_G(debug_realloc_fail_threshold);
DBG_ENTER(mysqlnd_realloc_name);
DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
DBG_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr);
DBG_INF_FMT("before: %lu", zend_memory_usage(TRUE TSRMLS_CC));
#if PHP_DEBUG
/* -1 is also "true" */
if (*threshold) {
#endif
ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size));
#if PHP_DEBUG
--*threshold;
} else if (*threshold == 0) {
ret = NULL;
}
#endif
DBG_INF_FMT("new_ptr=%p", (char*)ret);
if (ret && collect_memory_statistics) {