forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug_var.c
2700 lines (2255 loc) · 82.8 KB
/
xdebug_var.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
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2018 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <[email protected]> |
| Nikita Popov <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "ext/standard/php_string.h"
#include "ext/standard/url.h"
#include "zend.h"
#include "zend_extensions.h"
#include "ext/standard/php_smart_string.h"
#include "zend_smart_str.h"
#include "php_xdebug.h"
#include "xdebug_compat.h"
#include "xdebug_private.h"
#include "xdebug_mm.h"
#include "xdebug_var.h"
#include "xdebug_xml.h"
/* Set correct int format to use */
#include "Zend/zend_long.h"
#if SIZEOF_ZEND_LONG == 4
# define XDEBUG_INT_FMT "%ld"
#else
# define XDEBUG_INT_FMT "%lld"
#endif
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
HashTable *xdebug_objdebug_pp(zval **zval_pp, int *is_tmp TSRMLS_DC)
{
zval dzval = **zval_pp;
HashTable *tmp;
if (!XG(in_debug_info) && Z_OBJ_HANDLER(dzval, get_debug_info)) {
zend_bool old_trace = XG(do_trace);
zend_object *orig_exception;
XG(do_trace) = 0;
XG(in_debug_info) = 1;
orig_exception = EG(exception);
EG(exception) = NULL;
tmp = Z_OBJ_HANDLER(dzval, get_debug_info)(&dzval, is_tmp TSRMLS_CC);
XG(in_debug_info) = 0;
XG(do_trace) = old_trace;
EG(exception) = orig_exception;
return tmp;
} else {
*is_tmp = 0;
if (Z_OBJ_HANDLER(dzval, get_properties)) {
return Z_OBJPROP(dzval);
}
}
return NULL;
}
char* xdebug_error_type_simple(int type)
{
switch (type) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
return xdstrdup("fatal-error");
break;
case E_RECOVERABLE_ERROR:
return xdstrdup("catchable-fatal-error");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
return xdstrdup("warning");
break;
case E_PARSE:
return xdstrdup("parse-error");
break;
case E_NOTICE:
case E_USER_NOTICE:
return xdstrdup("notice");
break;
case E_STRICT:
return xdstrdup("strict-standards");
break;
case E_DEPRECATED:
case E_USER_DEPRECATED:
return xdstrdup("deprecated");
break;
case 0:
return xdstrdup("xdebug");
break;
default:
return xdstrdup("unknown-error");
break;
}
}
char* xdebug_error_type(int type)
{
switch (type) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
return xdstrdup("Fatal error");
break;
case E_RECOVERABLE_ERROR:
return xdstrdup("Catchable fatal error");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
return xdstrdup("Warning");
break;
case E_PARSE:
return xdstrdup("Parse error");
break;
case E_NOTICE:
case E_USER_NOTICE:
return xdstrdup("Notice");
break;
case E_STRICT:
return xdstrdup("Strict standards");
break;
case E_DEPRECATED:
case E_USER_DEPRECATED:
return xdstrdup("Deprecated");
break;
case 0:
return xdstrdup("Xdebug");
break;
default:
return xdstrdup("Unknown error");
break;
}
}
/*************************************************************************************************************************************/
#define T(offset) (*(union _temp_variable *)((char*)zdata->current_execute_data->Ts + offset))
zval *xdebug_get_zval_with_opline(zend_execute_data *zdata, const zend_op *opline, int node_type, const znode_op *node, int *is_var)
{
zend_free_op should_free;
#if PHP_VERSION_ID >= 70300
return zend_get_zval_ptr(opline, node_type, node, zdata, &should_free, BP_VAR_IS);
#else
return zend_get_zval_ptr(node_type, node, zdata, &should_free, BP_VAR_IS);
#endif
}
zval *xdebug_get_zval(zend_execute_data *zdata, int node_type, const znode_op *node, int *is_var)
{
return xdebug_get_zval_with_opline(zdata, zdata->opline, node_type, node, is_var);
}
/*****************************************************************************
** PHP Variable related utility functions
*/
/*****************************************************************************
** Data returning functions
*/
#define XF_ST_ROOT 0
#define XF_ST_ARRAY_INDEX_NUM 1
#define XF_ST_ARRAY_INDEX_ASSOC 2
#define XF_ST_OBJ_PROPERTY 3
#define XF_ST_STATIC_ROOT 4
#define XF_ST_STATIC_PROPERTY 5
inline static HashTable *fetch_ht_from_zval(zval *z TSRMLS_DC)
{
switch (Z_TYPE_P(z)) {
case IS_ARRAY:
return Z_ARRVAL_P(z);
break;
case IS_OBJECT:
return Z_OBJPROP_P(z);
break;
}
return NULL;
}
inline static char *fetch_classname_from_zval(zval *z, int *length, zend_class_entry **ce TSRMLS_DC)
{
zend_string *class_name;
if (Z_TYPE_P(z) == IS_INDIRECT) {
z = z->value.zv;
}
if (Z_TYPE_P(z) == IS_REFERENCE) {
z = &z->value.ref->val;
}
if (Z_TYPE_P(z) != IS_OBJECT) {
return NULL;
}
class_name = Z_OBJ_HANDLER_P(z, get_class_name)(Z_OBJ_P(z));
*ce = Z_OBJCE_P(z);
*length = class_name->len;
return estrdup(class_name->val);
}
static char* prepare_search_key(char *name, unsigned int *name_length, const char *prefix, int prefix_length)
{
char *element;
int extra_length = 0;
if (prefix_length) {
if (prefix[0] == '*') {
extra_length = 3;
} else {
extra_length = 2 + prefix_length;
}
}
element = malloc(*name_length + 1 + extra_length);
memset(element, 0, *name_length + 1 + extra_length);
if (extra_length) {
memcpy(element + 1, prefix, extra_length - 2);
}
memcpy(element + extra_length, name, *name_length);
*name_length += extra_length;
return element;
}
static zval *get_arrayobject_storage(zval *parent, HashTable **properties, int *is_temp TSRMLS_DC)
{
*properties = Z_OBJDEBUG_P(parent, *is_temp);
return zend_hash_str_find(*properties, "\0ArrayObject\0storage", sizeof("*ArrayObject*storage") - 1);
}
static zval *get_splobjectstorage_storage(zval *parent, HashTable **properties, int *is_temp TSRMLS_DC)
{
*properties = Z_OBJDEBUG_P(parent, *is_temp);
return zend_hash_str_find(*properties, "\0SplObjectStorage\0storage", sizeof("*SplObjectStorage*storage") - 1);
}
static zval *get_arrayiterator_storage(zval *parent, HashTable **properties, int *is_temp TSRMLS_DC)
{
*properties = Z_OBJDEBUG_P(parent, *is_temp);
return zend_hash_str_find(*properties, "\0ArrayIterator\0storage", sizeof("*ArrayIterator*storage") - 1);
}
static inline void maybe_destroy_ht(HashTable *ht, int is_temp)
{
if (ht && is_temp) {
zend_hash_destroy(ht);
efree(ht);
}
}
static void fetch_zval_from_symbol_table(
zval *value_in, char *name, unsigned int name_length,
int type, char* ccn, int ccnl, zend_class_entry *cce TSRMLS_DC)
{
HashTable *ht = NULL;
char *element = NULL;
unsigned int element_length = name_length;
zend_property_info *zpp;
int is_temp = 0;
HashTable *myht = NULL;
zval *orig_value_in = value_in;
zval tmp_retval;
ZVAL_UNDEF(&tmp_retval);
if (Z_TYPE_P(value_in) == IS_INDIRECT) {
value_in = Z_INDIRECT_P(value_in);
}
ZVAL_DEREF(value_in);
ht = fetch_ht_from_zval(value_in TSRMLS_CC);
switch (type) {
case XF_ST_STATIC_ROOT:
case XF_ST_STATIC_PROPERTY:
/* First we try a public,private,protected property */
element = prepare_search_key(name, &element_length, "", 0);
if (cce && ((zpp = zend_hash_str_find_ptr(&cce->properties_info, element, element_length)) != NULL) && cce->static_members_table) {
ZVAL_COPY(&tmp_retval, &cce->static_members_table[zpp->offset]);
goto cleanup;
}
element_length = name_length;
/* Then we try to see whether the first char is * and use the part between * and * as class name for the private property */
if (name[0] == '*') {
char *secondStar;
secondStar = strstr(name + 1, "*");
if (secondStar) {
free(element);
element_length = name_length - (secondStar + 1 - name);
element = prepare_search_key(secondStar + 1, &element_length, "", 0);
if (cce && ((zpp = zend_hash_str_find_ptr(&cce->properties_info, element, element_length)) != NULL)) {
ZVAL_COPY(&tmp_retval, &cce->static_members_table[zpp->offset]);
goto cleanup;
}
}
}
break;
case XF_ST_ROOT:
/* Check for compiled vars */
element = prepare_search_key(name, &element_length, "", 0);
if (XG(active_execute_data) && XG(active_execute_data)->func) {
int i = 0;
zend_ulong hash_value = zend_inline_hash_func(element, element_length);
zend_op_array *opa = &XG(active_execute_data)->func->op_array;
zval **CV;
while (i < opa->last_var) {
if (ZSTR_H(opa->vars[i]) == hash_value &&
ZSTR_LEN(opa->vars[i]) == element_length &&
strncmp(STR_NAME_VAL(opa->vars[i]), element, element_length) == 0)
{
zval *CV_z = ZEND_CALL_VAR_NUM(XG(active_execute_data), i);
CV = &CV_z;
if (CV) {
ZVAL_COPY(&tmp_retval, *CV);
goto cleanup;
}
}
i++;
}
}
free(element);
ht = XG(active_symbol_table);
XDEBUG_BREAK_INTENTIONALLY_MISSING
case XF_ST_ARRAY_INDEX_ASSOC:
element = prepare_search_key(name, &name_length, "", 0);
xdebug_stripcslashes(element, (int *) &name_length);
/* Handle "this" in a different way */
if (type == XF_ST_ROOT && strcmp("this", element) == 0) {
if (XG(This)) {
ZVAL_COPY(&tmp_retval, XG(This));
} else {
ZVAL_NULL(&tmp_retval);
}
goto cleanup;
}
if (ht) {
zval *tmp = zend_hash_str_find(ht, element, name_length);
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
goto cleanup;
}
}
break;
case XF_ST_ARRAY_INDEX_NUM:
element = prepare_search_key(name, &name_length, "", 0);
if (ht) {
zval *tmp = zend_hash_index_find(ht, strtoull(element, NULL, 10));
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
goto cleanup;
}
}
break;
case XF_ST_OBJ_PROPERTY:
/* Let's see if there is a debug handler */
if (value_in && Z_TYPE_P(value_in) == IS_OBJECT) {
myht = xdebug_objdebug_pp(&value_in, &is_temp TSRMLS_CC);
if (myht) {
zval *tmp = zend_symtable_str_find(myht, name, name_length);
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
maybe_destroy_ht(myht, is_temp);
goto cleanup;
}
maybe_destroy_ht(myht, is_temp);
}
}
/* First we try an object handler */
if (cce) {
zval *tmp_val;
tmp_val = zend_read_property(cce, value_in, name, name_length, 1, &tmp_retval);
if (tmp_val != &tmp_retval && tmp_val != &EG(uninitialized_zval)) {
ZVAL_COPY(&tmp_retval, tmp_val);
goto cleanup;
}
if (EG(exception)) {
zend_clear_exception(TSRMLS_C);
}
}
/* Then we try a public property */
element = prepare_search_key(name, &element_length, "", 0);
if (ht) {
zval *tmp = zend_symtable_str_find(ht, element, element_length);
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
goto cleanup;
}
}
element_length = name_length;
/* Then we try it again as protected property */
free(element);
element = prepare_search_key(name, &element_length, "*", 1);
if (ht) {
zval *tmp = zend_hash_str_find(ht, element, element_length);
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
goto cleanup;
}
}
element_length = name_length;
/* Then we try it again as private property */
free(element);
element = prepare_search_key(name, &element_length, ccn, ccnl);
if (ht) {
zval *tmp = zend_hash_str_find(ht, element, element_length);
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
goto cleanup;
}
}
element_length = name_length;
/* All right, time for a mega hack. It's SplObjectStorage access time! */
if (strncmp(ccn, "SplObjectStorage", ccnl) == 0 && strncmp(name, "storage", name_length) == 0) {
zval *tmp = get_splobjectstorage_storage(value_in, &myht, &is_temp TSRMLS_CC);
element = NULL;
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
maybe_destroy_ht(myht, is_temp);
goto cleanup;
}
maybe_destroy_ht(myht, is_temp);
}
/* Then we try to see whether the first char is * and use the part between * and * as class name for the private property */
if (name[0] == '*') {
char *secondStar;
secondStar = strstr(name + 1, "*");
if (secondStar) {
free(element);
element_length = name_length - (secondStar + 1 - name);
/* All right, time for a mega hack. It's ArrayObject access time! */
if (strncmp(name + 1, "ArrayObject", secondStar - name - 1) == 0 && strncmp(secondStar + 1, "storage", element_length) == 0) {
zval *tmp = get_arrayobject_storage(value_in, &myht, &is_temp TSRMLS_CC);
element = NULL;
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
maybe_destroy_ht(myht, is_temp);
goto cleanup;
}
maybe_destroy_ht(myht, is_temp);
}
/* All right, time for a mega hack. It's ArrayIterator access time! */
if (strncmp(name + 1, "ArrayIterator", secondStar - name - 1) == 0 && strncmp(secondStar + 1, "storage", element_length) == 0) {
zval *tmp = get_arrayiterator_storage(value_in, &myht, &is_temp TSRMLS_CC);
element = NULL;
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
maybe_destroy_ht(myht, is_temp);
goto cleanup;
}
maybe_destroy_ht(myht, is_temp);
}
/* The normal one */
element = prepare_search_key(secondStar + 1, &element_length, name + 1, secondStar - name - 1);
if (ht) {
zval *tmp = zend_hash_str_find(ht, element, element_length);
if (tmp != NULL) {
ZVAL_COPY(&tmp_retval, tmp);
goto cleanup;
}
}
}
}
break;
}
cleanup:
if (element) {
free(element);
}
zval_ptr_dtor_nogc(orig_value_in);
ZVAL_COPY_VALUE(orig_value_in, &tmp_retval);
}
inline static int is_objectish(zval *value)
{
switch (Z_TYPE_P(value)) {
case IS_OBJECT:
return 1;
case IS_INDIRECT:
if (Z_TYPE_P(Z_INDIRECT_P(value)) == IS_OBJECT) {
return 1;
}
break;
case IS_REFERENCE:
if (Z_TYPE_P(Z_REFVAL_P(value)) == IS_OBJECT) {
return 1;
}
break;
}
return 0;
}
void xdebug_get_php_symbol(zval *retval, xdebug_str* name)
{
int found = -1;
int state = 0;
char *ptr = name->d;
int ctr = 0;
char *keyword = NULL, *keyword_end = NULL;
int type = XF_ST_ROOT;
char *current_classname = NULL;
zend_class_entry *current_ce = NULL;
int cc_length = 0;
char quotechar = 0;
ZVAL_UNDEF(retval);
do {
if (ctr == name->l) {
found = 0;
} else {
switch (state) {
case 0:
if (ptr[ctr] == '$') {
keyword = &ptr[ctr] + 1;
break;
}
if (ptr[ctr] == ':') { /* special tricks */
keyword = &ptr[ctr];
state = 7;
break;
}
keyword = &ptr[ctr];
state = 1;
XDEBUG_BREAK_INTENTIONALLY_MISSING
case 1:
if (ptr[ctr] == '[') {
keyword_end = &ptr[ctr];
if (keyword) {
fetch_zval_from_symbol_table(retval, keyword, keyword_end - keyword, type, current_classname, cc_length, current_ce TSRMLS_CC);
if (current_classname) {
efree(current_classname);
}
current_classname = NULL;
cc_length = 0;
current_ce = NULL;
keyword = NULL;
}
state = 3;
} else if (ptr[ctr] == '-') {
keyword_end = &ptr[ctr];
if (keyword) {
fetch_zval_from_symbol_table(retval, keyword, keyword_end - keyword, type, current_classname, cc_length, current_ce TSRMLS_CC);
if (current_classname) {
efree(current_classname);
}
current_classname = NULL;
cc_length = 0;
current_ce = NULL;
if (is_objectish(retval)) {
current_classname = fetch_classname_from_zval(retval, &cc_length, ¤t_ce TSRMLS_CC);
}
keyword = NULL;
}
state = 2;
type = XF_ST_OBJ_PROPERTY;
} else if (ptr[ctr] == ':') {
keyword_end = &ptr[ctr];
if (keyword) {
fetch_zval_from_symbol_table(retval, keyword, keyword_end - keyword, type, current_classname, cc_length, current_ce TSRMLS_CC);
if (current_classname) {
efree(current_classname);
}
current_classname = NULL;
cc_length = 0;
if (is_objectish(retval)) {
current_classname = fetch_classname_from_zval(retval, &cc_length, ¤t_ce TSRMLS_CC);
}
keyword = NULL;
}
state = 8;
type = XF_ST_STATIC_PROPERTY;
}
break;
case 2:
if (ptr[ctr] != '>') {
keyword = &ptr[ctr];
state = 1;
}
break;
case 8:
if (ptr[ctr] != ':') {
keyword = &ptr[ctr];
state = 1;
}
break;
case 3: /* Parsing in [...] */
/* Associative arrays */
if (ptr[ctr] == '\'' || ptr[ctr] == '"') {
state = 4;
keyword = &ptr[ctr] + 1;
quotechar = ptr[ctr];
type = XF_ST_ARRAY_INDEX_ASSOC;
}
/* Numerical index */
if (ptr[ctr] >= '0' && ptr[ctr] <= '9') {
cc_length = 0;
state = 6;
keyword = &ptr[ctr];
type = XF_ST_ARRAY_INDEX_NUM;
}
/* Numerical index starting with a - */
if (ptr[ctr] == '-') {
state = 9;
keyword = &ptr[ctr];
}
break;
case 9:
/* Numerical index starting with a - */
if (ptr[ctr] >= '0' && ptr[ctr] <= '9') {
state = 6;
type = XF_ST_ARRAY_INDEX_NUM;
}
break;
case 4:
if (ptr[ctr] == '\\') {
state = 10; /* Escaped character */
} else if (ptr[ctr] == quotechar) {
quotechar = 0;
state = 5;
keyword_end = &ptr[ctr];
fetch_zval_from_symbol_table(retval, keyword, keyword_end - keyword, type, current_classname, cc_length, current_ce TSRMLS_CC);
if (current_classname) {
efree(current_classname);
}
current_classname = NULL;
cc_length = 0;
if (is_objectish(retval)) {
current_classname = fetch_classname_from_zval(retval, &cc_length, ¤t_ce TSRMLS_CC);
}
keyword = NULL;
}
break;
case 10: /* Escaped character */
state = 4;
break;
case 5:
if (ptr[ctr] == ']') {
state = 1;
}
break;
case 6:
if (ptr[ctr] == ']') {
state = 1;
keyword_end = &ptr[ctr];
fetch_zval_from_symbol_table(retval, keyword, keyword_end - keyword, type, current_classname, cc_length, current_ce TSRMLS_CC);
if (current_classname) {
efree(current_classname);
}
current_classname = NULL;
cc_length = 0;
if (is_objectish(retval)) {
current_classname = fetch_classname_from_zval(retval, &cc_length, ¤t_ce TSRMLS_CC);
}
keyword = NULL;
}
break;
case 7: /* special cases, started with a ":" */
if (ptr[ctr] == ':') {
state = 1;
keyword_end = &ptr[ctr];
if (strncmp(keyword, "::", 2) == 0 && XG(active_fse)->function.class) { /* static class properties */
zend_class_entry *ce = xdebug_fetch_class(XG(active_fse)->function.class, strlen(XG(active_fse)->function.class), ZEND_FETCH_CLASS_SELF TSRMLS_CC);
current_classname = estrdup(STR_NAME_VAL(ce->name));
cc_length = strlen(STR_NAME_VAL(ce->name));
current_ce = ce;
keyword = &ptr[ctr] + 1;
type = XF_ST_STATIC_ROOT;
} else {
keyword = NULL;
}
}
break;
}
ctr++;
}
} while (found < 0);
if (keyword != NULL) {
fetch_zval_from_symbol_table(retval, keyword, &ptr[ctr] - keyword, type, current_classname, cc_length, current_ce TSRMLS_CC);
}
if (current_classname) {
efree(current_classname);
}
}
static xdebug_str* xdebug_get_property_info(char *mangled_property, int mangled_len, const char **modifier, char **class_name)
{
const char *cls_name, *tmp_prop_name;
size_t tmp_prop_name_len;
xdebug_str *property_name;
zend_string *i_mangled = zend_string_init(mangled_property, mangled_len - 1, 0);
zend_unmangle_property_name_ex(i_mangled, &cls_name, &tmp_prop_name, &tmp_prop_name_len);
property_name = xdebug_str_create((char*) tmp_prop_name, tmp_prop_name_len);
*class_name = cls_name ? xdstrdup(cls_name) : NULL;
zend_string_release(i_mangled);
if (*class_name) {
if (*class_name[0] == '*') {
*modifier = "protected";
} else {
*modifier = "private";
}
} else {
*modifier = "public";
}
return property_name;
}
#define XDEBUG_MAX_INT 2147483647
xdebug_var_export_options* xdebug_var_export_options_from_ini(TSRMLS_D)
{
xdebug_var_export_options *options;
options = xdmalloc(sizeof(xdebug_var_export_options));
options->max_children = XG(display_max_children);
options->max_data = XG(display_max_data);
options->max_depth = XG(display_max_depth);
options->show_hidden = 0;
options->show_location = XG(overload_var_dump) > 1;
options->extended_properties = 0;
options->force_extended = 0;
if (options->max_children == -1 || options->max_children > XDEBUG_MAX_INT) {
options->max_children = XDEBUG_MAX_INT;
} else if (options->max_children < 1) {
options->max_children = 0;
}
if (options->max_data == -1 || options->max_data > XDEBUG_MAX_INT) {
options->max_data = XDEBUG_MAX_INT;
} else if (options->max_data < 1) {
options->max_data = 0;
}
if (options->max_depth == -1 || options->max_depth > 1023) {
options->max_depth = 1023;
} else if (options->max_depth < 1) {
options->max_depth = 0;
}
options->runtime = (xdebug_var_runtime_page*) xdmalloc((options->max_depth + 1) * sizeof(xdebug_var_runtime_page));
options->no_decoration = 0;
return options;
}
xdebug_var_export_options xdebug_var_nolimit_options = { XDEBUG_MAX_INT, XDEBUG_MAX_INT, 1023, 1, 0, 0, 0, NULL, 0 };
xdebug_var_export_options* xdebug_var_get_nolimit_options(TSRMLS_D)
{
return &xdebug_var_nolimit_options;
}
/*****************************************************************************
** Normal variable printing routines
*/
static int xdebug_array_element_export(zval *zv_nptr, zend_ulong index_key, zend_string *hash_key, int level, xdebug_str *str, int debug_zval, xdebug_var_export_options *options)
{
zval **zv = &zv_nptr;
if (options->runtime[level].current_element_nr >= options->runtime[level].start_element_nr &&
options->runtime[level].current_element_nr < options->runtime[level].end_element_nr)
{
if (HASH_KEY_IS_NUMERIC(hash_key)) { /* numeric key */
xdebug_str_add(str, xdebug_sprintf(XDEBUG_INT_FMT " => ", index_key), 1);
} else { /* string key */
size_t newlen = 0;
char *tmp, *tmp2;
tmp = xdebug_str_to_str((char*) HASH_APPLY_KEY_VAL(hash_key), HASH_APPLY_KEY_LEN(hash_key), "'", 1, "\\'", 2, &newlen);
tmp2 = xdebug_str_to_str(tmp, newlen - 1, "\0", 1, "\\0", 2, &newlen);
if (tmp) {
efree(tmp);
}
xdebug_str_addl(str, "'", 1, 0);
if (tmp2) {
xdebug_str_addl(str, tmp2, newlen, 0);
efree(tmp2);
}
xdebug_str_add(str, "' => ", 0);
}
xdebug_var_export(zv, str, level + 2, debug_zval, options TSRMLS_CC);
xdebug_str_addl(str, ", ", 2, 0);
}
if (options->runtime[level].current_element_nr == options->runtime[level].end_element_nr) {
xdebug_str_addl(str, "..., ", 5, 0);
}
options->runtime[level].current_element_nr++;
return 0;
}
static int xdebug_object_element_export(zval *zv_nptr, zend_ulong index_key, zend_string *hash_key, int level, xdebug_str *str, int debug_zval, xdebug_var_export_options *options, char *class_name)
{
zval **zv = &zv_nptr;
if (options->runtime[level].current_element_nr >= options->runtime[level].start_element_nr &&
options->runtime[level].current_element_nr < options->runtime[level].end_element_nr)
{
if (!HASH_KEY_IS_NUMERIC(hash_key)) {
xdebug_str *property_name;
char *prop_class_name;
const char *modifier;
property_name = xdebug_get_property_info((char*) HASH_APPLY_KEY_VAL(hash_key), HASH_APPLY_KEY_LEN(hash_key), &modifier, &prop_class_name);
xdebug_str_add(str, modifier, 0);
xdebug_str_addl(str, " $", 2, 0);
if (strcmp(modifier, "private") != 0 || strcmp(class_name, prop_class_name) == 0) {
xdebug_str_add_str(str, property_name);
xdebug_str_addl(str, " = ", 3, 0);
} else {
xdebug_str_addc(str, '{');
xdebug_str_add(str, prop_class_name, 0);
xdebug_str_addc(str, '}');
xdebug_str_add_str(str, property_name);
xdebug_str_addl(str, " = ", 3, 0);
}
xdebug_str_free(property_name);
xdfree(prop_class_name);
} else {
xdebug_str_add(str, xdebug_sprintf("public $%d = ", index_key), 1);
}
xdebug_var_export(zv, str, level + 2, debug_zval, options TSRMLS_CC);
xdebug_str_addl(str, "; ", 2, 0);
}
if (options->runtime[level].current_element_nr == options->runtime[level].end_element_nr) {
xdebug_str_addl(str, "...; ", 5, 0);
}
options->runtime[level].current_element_nr++;
return 0;
}
void xdebug_var_export(zval **struc, xdebug_str *str, int level, int debug_zval, xdebug_var_export_options *options TSRMLS_DC)
{
HashTable *myht;
char* tmp_str;
int is_temp;
zend_ulong num;
zend_string *key;
zval *val;
zval *tmpz;
if (!struc || !(*struc)) {
return;
}
if (debug_zval) {
if (Z_TYPE_P(*struc) >= IS_STRING && Z_TYPE_P(*struc) != IS_INDIRECT) {
xdebug_str_add(str, xdebug_sprintf("(refcount=%d, is_ref=%d)=", (*struc)->value.counted->gc.refcount, Z_TYPE_P(*struc) == IS_REFERENCE), 1);
} else {
xdebug_str_add(str, "(refcount=0, is_ref=0)=", 0);
}
}
if (Z_TYPE_P(*struc) == IS_REFERENCE) {
tmpz = &((*struc)->value.ref->val);
struc = &tmpz;
}
switch (Z_TYPE_P(*struc)) {
case IS_TRUE:
case IS_FALSE:
xdebug_str_add(str, xdebug_sprintf("%s", Z_TYPE_P(*struc) == IS_TRUE ? "TRUE" : "FALSE"), 1);
break;
case IS_NULL:
xdebug_str_addl(str, "NULL", 4, 0);
break;
case IS_LONG:
xdebug_str_add(str, xdebug_sprintf(XDEBUG_INT_FMT, Z_LVAL_P(*struc)), 1);
break;
case IS_DOUBLE:
xdebug_str_add(str, xdebug_sprintf("%.*G", (int) EG(precision), Z_DVAL_P(*struc)), 1);
break;
case IS_STRING: {
zend_string *i_string = zend_string_init(Z_STRVAL_P(*struc), Z_STRLEN_P(*struc), 0);
zend_string *tmp_zstr;
#if PHP_VERSION_ID >= 70300
tmp_zstr = php_addcslashes(i_string, (char*) "'\\\0..\37", 7);
#else
tmp_zstr = php_addcslashes(i_string, 0, (char*) "'\\\0..\37", 7);
#endif
tmp_str = estrndup(tmp_zstr->val, tmp_zstr->len);
zend_string_release(tmp_zstr);
zend_string_release(i_string);
if (options->no_decoration) {
xdebug_str_add(str, tmp_str, 0);
} else if ((size_t) Z_STRLEN_P(*struc) <= (size_t) options->max_data) {
xdebug_str_add(str, xdebug_sprintf("'%s'", tmp_str), 1);
} else {
xdebug_str_addl(str, "'", 1, 0);
xdebug_str_addl(str, xdebug_sprintf("%s", tmp_str), options->max_data, 1);
xdebug_str_addl(str, "...'", 4, 0);
}
efree(tmp_str);
} break;
case IS_ARRAY:
myht = Z_ARRVAL_P(*struc);
if (!xdebug_zend_hash_is_recursive(myht)) {
xdebug_str_addl(str, "array (", 7, 0);
if (level <= options->max_depth) {
options->runtime[level].current_element_nr = 0;
options->runtime[level].start_element_nr = 0;
options->runtime[level].end_element_nr = options->max_children;
xdebug_zend_hash_apply_protection_begin(myht);
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
xdebug_array_element_export(val, num, key, level, str, debug_zval, options);
} ZEND_HASH_FOREACH_END();
xdebug_zend_hash_apply_protection_end(myht);
/* Remove the ", " at the end of the string */
if (myht->nNumOfElements > 0) {
xdebug_str_chop(str, 2);
}
} else {
xdebug_str_addl(str, "...", 3, 0);
}
xdebug_str_addl(str, ")", 1, 0);
} else {
xdebug_str_addl(str, "...", 3, 0);
}
break;
case IS_OBJECT:
myht = xdebug_objdebug_pp(struc, &is_temp TSRMLS_CC);
if (!xdebug_zend_hash_is_recursive(myht)) {
char *class_name = (char*) STR_NAME_VAL(Z_OBJCE_P(*struc)->name);
xdebug_str_add(str, xdebug_sprintf("class %s { ", class_name), 1);
if (level <= options->max_depth) {