forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug_code_coverage.c
1185 lines (1014 loc) · 37.6 KB
/
xdebug_code_coverage.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]> |
+----------------------------------------------------------------------+
*/
#include "php_xdebug.h"
#include "xdebug_private.h"
#include "xdebug_filter.h"
#include "xdebug_set.h"
#include "xdebug_stack.h"
#include "xdebug_var.h"
#include "xdebug_branch_info.h"
#include "xdebug_code_coverage.h"
#include "xdebug_compat.h"
#include "xdebug_tracing.h"
extern ZEND_DECLARE_MODULE_GLOBALS(xdebug);
void xdebug_coverage_line_dtor(void *data)
{
xdebug_coverage_line *line = (xdebug_coverage_line *) data;
xdfree(line);
}
xdebug_coverage_file *xdebug_coverage_file_ctor(char *filename)
{
xdebug_coverage_file *file;
file = xdmalloc(sizeof(xdebug_coverage_file));
file->name = xdstrdup(filename);
file->lines = xdebug_hash_alloc(128, xdebug_coverage_line_dtor);
file->functions = xdebug_hash_alloc(128, xdebug_coverage_function_dtor);
file->has_branch_info = 0;
return file;
}
void xdebug_coverage_file_dtor(void *data)
{
xdebug_coverage_file *file = (xdebug_coverage_file *) data;
xdebug_hash_destroy(file->lines);
xdebug_hash_destroy(file->functions);
xdfree(file->name);
xdfree(file);
}
xdebug_coverage_function *xdebug_coverage_function_ctor(char *function_name)
{
xdebug_coverage_function *function;
function = xdmalloc(sizeof(xdebug_coverage_function));
function->name = xdstrdup(function_name);
function->branch_info = NULL;
return function;
}
void xdebug_coverage_function_dtor(void *data)
{
xdebug_coverage_function *function = (xdebug_coverage_function *) data;
if (function->branch_info) {
xdebug_branch_info_free(function->branch_info);
}
xdfree(function->name);
xdfree(function);
}
void xdebug_print_opcode_info(char type, zend_execute_data *execute_data, const zend_op *cur_opcode TSRMLS_DC)
{
zend_op_array *op_array = &execute_data->func->op_array;
char *file = (char*) STR_NAME_VAL(op_array->filename);
xdebug_func func_info;
char *function_name;
long opnr = execute_data->opline - execute_data->func->op_array.opcodes;
xdebug_build_fname_from_oparray(&func_info, op_array TSRMLS_CC);
function_name = xdebug_func_format(&func_info TSRMLS_CC);
if (func_info.class) {
xdfree(func_info.class);
}
if (func_info.function) {
xdfree(func_info.function);
}
xdebug_branch_info_mark_reached(file, function_name, op_array, opnr TSRMLS_CC);
xdfree(function_name);
}
int xdebug_check_branch_entry_handler(zend_execute_data *execute_data)
{
zend_op_array *op_array = &execute_data->func->op_array;
if (!op_array->reserved[XG(code_coverage_filter_offset)] && XG(do_code_coverage)) {
const zend_op *cur_opcode;
cur_opcode = execute_data->opline;
xdebug_print_opcode_info('G', execute_data, cur_opcode TSRMLS_CC);
}
return ZEND_USER_OPCODE_DISPATCH;
}
#define XDEBUG_OPCODE_OVERRIDE(f) \
int xdebug_##f##_handler(zend_execute_data *execute_data) \
{ \
return xdebug_common_override_handler(execute_data); \
}
int xdebug_common_override_handler(zend_execute_data *execute_data)
{
zend_op_array *op_array = &execute_data->func->op_array;
if (!op_array->reserved[XG(code_coverage_filter_offset)] && XG(do_code_coverage)) {
const zend_op *cur_opcode;
int lineno;
char *file;
cur_opcode = execute_data->opline;
lineno = cur_opcode->lineno;
file = (char*) STR_NAME_VAL(op_array->filename);
xdebug_print_opcode_info('C', execute_data, cur_opcode TSRMLS_CC);
xdebug_count_line(file, lineno, 0, 0 TSRMLS_CC);
}
return ZEND_USER_OPCODE_DISPATCH;
}
static int xdebug_is_static_call(const zend_op *cur_opcode, const zend_op *prev_opcode, const zend_op **found_opcode)
{
const zend_op *opcode_ptr;
opcode_ptr = cur_opcode;
# if PHP_VERSION_ID >= 70100
while (!(opcode_ptr->opcode == ZEND_EXT_STMT) && !((opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_W) || (opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_RW))) {
opcode_ptr = opcode_ptr - 1;
}
if ((opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_W) || (opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_RW)) {
# else
while (!(opcode_ptr->opcode == ZEND_EXT_STMT) && !((opcode_ptr->opcode == ZEND_FETCH_W) || (opcode_ptr->opcode == ZEND_FETCH_RW))) {
opcode_ptr = opcode_ptr - 1;
}
if (((opcode_ptr->opcode == ZEND_FETCH_W) || (opcode_ptr->opcode == ZEND_FETCH_RW)) && opcode_ptr->extended_value == ZEND_FETCH_STATIC_MEMBER) {
# endif
*found_opcode = opcode_ptr;
return 1;
}
return 0;
}
static const zend_op *xdebug_find_referenced_opline(zend_execute_data *execute_data, const zend_op *cur_opcode, int op1_or_op2)
{
int op_type = (op1_or_op2 == 1) ? cur_opcode->op1_type : cur_opcode->op2_type;
if (op_type == IS_VAR) {
size_t variable_number = (op1_or_op2 == 1) ? cur_opcode->op1.var : cur_opcode->op2.var;
const zend_op *scan_opcode = cur_opcode;
int found = 0;
/* Scroll up until we find a RES of IS_VAR with the right value */
do {
scan_opcode--;
if (scan_opcode->result_type == IS_VAR && scan_opcode->result.var == variable_number) {
found = 1;
}
} while (!found);
return scan_opcode;
}
return NULL;
}
static char *xdebug_find_var_name(zend_execute_data *execute_data, const zend_op *cur_opcode, const zend_op *lower_bound TSRMLS_DC)
{
const zend_op *next_opcode, *prev_opcode = NULL, *opcode_ptr;
zval *dimval;
int is_var;
zend_op_array *op_array = &execute_data->func->op_array;
xdebug_str name = XDEBUG_STR_INITIALIZER;
int gohungfound = 0, is_static = 0;
xdebug_str *zval_value = NULL;
xdebug_var_export_options *options;
const zend_op *static_opcode_ptr = NULL;
next_opcode = cur_opcode + 1;
prev_opcode = cur_opcode - 1;
if (cur_opcode->opcode == ZEND_QM_ASSIGN) {
#if PHP_VERSION_ID >= 70000
xdebug_str_add(&name, xdebug_sprintf("$%s", zend_get_compiled_variable_name(op_array, cur_opcode->result.var)->val), 1);
#else
xdebug_str_add(&name, xdebug_sprintf("$%s", zend_get_compiled_variable_name(op_array, cur_opcode->result.var, &cv_len)), 1);
#endif
}
is_static = xdebug_is_static_call(cur_opcode, prev_opcode, &static_opcode_ptr);
options = xdebug_var_export_options_from_ini(TSRMLS_C);
options->no_decoration = 1;
if (cur_opcode->op1_type == IS_CV) {
if (!lower_bound) {
xdebug_str_add(&name, xdebug_sprintf("$%s", zend_get_compiled_variable_name(op_array, cur_opcode->op1.var)->val), 1);
}
} else if (cur_opcode->op1_type == IS_VAR && cur_opcode->opcode == ZEND_ASSIGN && (prev_opcode->opcode == ZEND_FETCH_W || prev_opcode->opcode == ZEND_FETCH_RW)) {
if (is_static) {
xdebug_str_add(&name, xdebug_sprintf("self::"), 1);
} else {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, prev_opcode->op1_type, &prev_opcode->op1, &is_var), 0, options);
xdebug_str_addc(&name, '$');
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
}
} else if (is_static) { /* todo : see if you can change this and the previous cases around */
xdebug_str_add(&name, xdebug_sprintf("self::"), 1 );
}
if ((cur_opcode->opcode >= ZEND_ASSIGN_ADD && cur_opcode->opcode <= ZEND_ASSIGN_BW_XOR)
|| cur_opcode->opcode == ZEND_ASSIGN_POW
) {
if (cur_opcode->extended_value == ZEND_ASSIGN_OBJ) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, cur_opcode->op2_type, &cur_opcode->op2, &is_var), 0, options);
if (cur_opcode->op1_type == IS_UNUSED) {
xdebug_str_addl(&name, "$this->", 7, 0);
} else {
xdebug_str_addl(&name, "->", 2, 0);
}
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
} else if (cur_opcode->extended_value == ZEND_ASSIGN_DIM) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, cur_opcode->op2_type, &cur_opcode->op2, &is_var), 0, NULL);
xdebug_str_addc(&name, '[');
xdebug_str_add_str(&name, zval_value);
xdebug_str_addc(&name, ']');
xdebug_str_free(zval_value);
}
}
if (cur_opcode->opcode >= ZEND_PRE_INC_OBJ && cur_opcode->opcode <= ZEND_POST_DEC_OBJ) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, cur_opcode->op2_type, &cur_opcode->op2, &is_var), 0, options);
xdebug_str_addl(&name, "$this->", 7, 0);
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
}
/* Scroll back to start of FETCHES */
/* FIXME: See whether we can do this unroll looping only once - in is_static() */
gohungfound = 0;
if (!is_static) {
if (cur_opcode == lower_bound) {
gohungfound = 1;
}
opcode_ptr = prev_opcode;
while ((opcode_ptr >= lower_bound) && (opcode_ptr->opcode == ZEND_FETCH_DIM_W || opcode_ptr->opcode == ZEND_FETCH_OBJ_W || opcode_ptr->opcode == ZEND_FETCH_W || opcode_ptr->opcode == ZEND_FETCH_RW)) {
opcode_ptr = opcode_ptr - 1;
gohungfound = 1;
}
opcode_ptr = opcode_ptr + 1;
} else { /* if we have a static method, we should already have found the first fetch */
opcode_ptr = static_opcode_ptr;
gohungfound = 1;
}
if (gohungfound) {
int cv_found = 0;
do
{
if (opcode_ptr->op1_type == IS_UNUSED && opcode_ptr->opcode == ZEND_FETCH_OBJ_W) {
xdebug_str_add(&name, "$this", 0);
}
if (opcode_ptr->op1_type == IS_CV) {
xdebug_str_add(&name, xdebug_sprintf("$%s", zend_get_compiled_variable_name(op_array, opcode_ptr->op1.var)->val), 1);
}
#if PHP_VERSION_ID >= 70100
if (opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_W || opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_R || opcode_ptr->opcode == ZEND_FETCH_STATIC_PROP_RW) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, opcode_ptr->op1_type, &opcode_ptr->op1, &is_var), 0, options);
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
}
#endif
if (opcode_ptr->opcode == ZEND_FETCH_W) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, opcode_ptr->op1_type, &opcode_ptr->op1, &is_var), 0, options);
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
}
if (is_static && opcode_ptr->opcode == ZEND_FETCH_RW) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, opcode_ptr->op1_type, &opcode_ptr->op1, &is_var), 0, options);
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
}
if (opcode_ptr->opcode == ZEND_FETCH_DIM_W) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, opcode_ptr->op2_type, &opcode_ptr->op2, &is_var), 0, NULL);
xdebug_str_addc(&name, '[');
if (zval_value) {
xdebug_str_add_str(&name, zval_value);
}
xdebug_str_addc(&name, ']');
xdebug_str_free(zval_value);
} else if (opcode_ptr->opcode == ZEND_FETCH_OBJ_W) {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, opcode_ptr->op2_type, &opcode_ptr->op2, &is_var), 0, options);
xdebug_str_addl(&name, "->", 2, 0);
xdebug_str_add_str(&name, zval_value);
xdebug_str_free(zval_value);
}
opcode_ptr = opcode_ptr + 1;
if (opcode_ptr->op1_type == IS_CV) {
cv_found = 1;
}
} while (!cv_found && (opcode_ptr->opcode == ZEND_FETCH_DIM_W || opcode_ptr->opcode == ZEND_FETCH_OBJ_W || opcode_ptr->opcode == ZEND_FETCH_W || opcode_ptr->opcode == ZEND_FETCH_RW));
}
if (cur_opcode->opcode == ZEND_ASSIGN_OBJ) {
if (cur_opcode->op1_type == IS_UNUSED) {
xdebug_str_add(&name, "$this", 0);
}
dimval = xdebug_get_zval(execute_data, cur_opcode->op2_type, &cur_opcode->op2, &is_var);
xdebug_str_add(&name, xdebug_sprintf("->%s", Z_STRVAL_P(dimval)), 1);
}
if (cur_opcode->opcode == ZEND_ASSIGN_DIM) {
if (next_opcode->opcode == ZEND_OP_DATA && cur_opcode->op2_type == IS_UNUSED) {
xdebug_str_add(&name, "[]", 0);
} else {
zval_value = xdebug_get_zval_value(xdebug_get_zval(execute_data, opcode_ptr->op2_type, &opcode_ptr->op2, &is_var), 0, NULL);
xdebug_str_addc(&name, '[');
xdebug_str_add_str(&name, zval_value);
xdebug_str_addc(&name, ']');
xdfree(zval_value);
}
}
xdfree(options->runtime);
xdfree(options);
return name.d;
}
static int xdebug_common_assign_dim_handler(const char *op, int do_cc, zend_execute_data *execute_data)
{
char *file;
zend_op_array *op_array = &execute_data->func->op_array;
int lineno;
const zend_op *cur_opcode, *next_opcode;
zval *val = NULL;
char *right_full_varname = NULL;
int is_var;
function_stack_entry *fse;
cur_opcode = execute_data->opline;
next_opcode = cur_opcode + 1;
file = (char*) STR_NAME_VAL(op_array->filename);
lineno = cur_opcode->lineno;
/* TODO TEST FOR ASSIGNMENTS IN FILTERING */
// if (xdebug_is_top_stack_frame_filtered(XDEBUG_FILTER_CODE_COVERAGE)) {
// return ZEND_USER_OPCODE_DISPATCH;
// }
if (!op_array->reserved[XG(code_coverage_filter_offset)] && XG(do_code_coverage)) {
xdebug_print_opcode_info('=', execute_data, cur_opcode TSRMLS_CC);
if (do_cc) {
xdebug_count_line(file, lineno, 0, 0 TSRMLS_CC);
}
}
if (XG(do_trace) && XG(trace_context) && XG(collect_assignments)) {
char *full_varname;
if (cur_opcode->opcode == ZEND_QM_ASSIGN && cur_opcode->result_type != IS_CV) {
return ZEND_USER_OPCODE_DISPATCH;
}
full_varname = xdebug_find_var_name(execute_data, execute_data->opline, NULL TSRMLS_CC);
if (cur_opcode->opcode >= ZEND_PRE_INC && cur_opcode->opcode <= ZEND_POST_DEC) {
char *tmp_varname;
switch (cur_opcode->opcode) {
case ZEND_PRE_INC: tmp_varname = xdebug_sprintf("++%s", full_varname); break;
case ZEND_POST_INC: tmp_varname = xdebug_sprintf("%s++", full_varname); break;
case ZEND_PRE_DEC: tmp_varname = xdebug_sprintf("--%s", full_varname); break;
case ZEND_POST_DEC: tmp_varname = xdebug_sprintf("%s--", full_varname); break;
}
xdfree(full_varname);
full_varname = tmp_varname;
val = xdebug_get_zval(execute_data, cur_opcode->op1_type, &cur_opcode->op1, &is_var);
} else if (cur_opcode->opcode >= ZEND_PRE_INC_OBJ && cur_opcode->opcode <= ZEND_POST_DEC_OBJ) {
char *tmp_varname;
switch (cur_opcode->opcode) {
case ZEND_PRE_INC_OBJ: tmp_varname = xdebug_sprintf("++%s", full_varname); break;
case ZEND_POST_INC_OBJ: tmp_varname = xdebug_sprintf("%s++", full_varname); break;
case ZEND_PRE_DEC_OBJ: tmp_varname = xdebug_sprintf("--%s", full_varname); break;
case ZEND_POST_DEC_OBJ: tmp_varname = xdebug_sprintf("%s--", full_varname); break;
}
xdfree(full_varname);
full_varname = tmp_varname;
val = xdebug_get_zval(execute_data, cur_opcode->op2_type, &cur_opcode->op2, &is_var);
} else if (next_opcode->opcode == ZEND_OP_DATA) {
val = xdebug_get_zval(execute_data, next_opcode->op1_type, &next_opcode->op1, &is_var);
} else if (cur_opcode->opcode == ZEND_QM_ASSIGN) {
val = xdebug_get_zval(execute_data, cur_opcode->op1_type, &cur_opcode->op1, &is_var);
} else if (cur_opcode->opcode == ZEND_ASSIGN_REF) {
if (cur_opcode->op2_type == IS_CV) {
right_full_varname = xdebug_sprintf("$%s", zend_get_compiled_variable_name(op_array, cur_opcode->op2.var)->val);
} else {
const zend_op *referenced_opline = xdebug_find_referenced_opline(execute_data, cur_opcode, 2);
#if PHP_VERSION_ID <= 70100
const zend_op *previous_opline = xdebug_find_referenced_opline(execute_data, cur_opcode, 1);
right_full_varname = xdebug_find_var_name(execute_data, referenced_opline, previous_opline + 1);
#else
right_full_varname = xdebug_find_var_name(execute_data, referenced_opline, NULL);
#endif
}
} else {
val = xdebug_get_zval(execute_data, cur_opcode->op2_type, &cur_opcode->op2, &is_var);
}
fse = XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack)));
if (XG(do_trace) && XG(trace_context) && XG(collect_assignments) && XG(trace_handler)->assignment) {
XG(trace_handler)->assignment(XG(trace_context), fse, full_varname, val, right_full_varname, op, file, lineno TSRMLS_CC);
}
xdfree(full_varname);
}
return ZEND_USER_OPCODE_DISPATCH;
}
#define XDEBUG_OPCODE_OVERRIDE_ASSIGN(f,o,cc) \
int xdebug_##f##_handler(zend_execute_data *execute_data) \
{ \
return xdebug_common_assign_dim_handler((o), (cc), execute_data); \
}
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign,"=",1)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(qm_assign,"=",1)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_add,"+=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_sub,"-=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_mul,"*=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_div,"/=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_mod,"%=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_pow,"**=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_sl,"<<=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_sr,">>=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(pre_inc,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(post_inc,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(pre_dec,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(post_dec,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(pre_inc_obj,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(post_inc_obj,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(pre_dec_obj,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(post_dec_obj,"",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_concat,".=",1)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_bw_or,"|=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_bw_and,"&=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_bw_xor,"^=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_dim,"=",1)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_obj,"=",1)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_ref,"=&",1)
void xdebug_count_line(char *filename, int lineno, int executable, int deadcode TSRMLS_DC)
{
xdebug_coverage_file *file;
xdebug_coverage_line *line;
if (XG(previous_filename) && strcmp(XG(previous_filename), filename) == 0) {
file = XG(previous_file);
} else {
/* Check if the file already exists in the hash */
if (!xdebug_hash_find(XG(code_coverage), filename, strlen(filename), (void *) &file)) {
/* The file does not exist, so we add it to the hash */
file = xdebug_coverage_file_ctor(filename);
xdebug_hash_add(XG(code_coverage), filename, strlen(filename), file);
}
XG(previous_filename) = file->name;
XG(previous_file) = file;
}
/* Check if the line already exists in the hash */
if (!xdebug_hash_index_find(file->lines, lineno, (void *) &line)) {
line = xdmalloc(sizeof(xdebug_coverage_line));
line->lineno = lineno;
line->count = 0;
line->executable = 0;
xdebug_hash_index_add(file->lines, lineno, line);
}
if (executable) {
if (line->executable != 1 && deadcode) {
line->executable = 2;
} else {
line->executable = 1;
}
} else {
line->count++;
}
}
static void prefill_from_opcode(char *fn, zend_op opcode, int deadcode TSRMLS_DC)
{
if (
opcode.opcode != ZEND_NOP &&
opcode.opcode != ZEND_EXT_NOP &&
opcode.opcode != ZEND_RECV &&
opcode.opcode != ZEND_RECV_INIT
&& opcode.opcode != ZEND_VERIFY_ABSTRACT_CLASS
&& opcode.opcode != ZEND_OP_DATA
&& opcode.opcode != ZEND_ADD_INTERFACE
&& opcode.opcode != ZEND_TICKS
&& opcode.opcode != ZEND_FAST_CALL
&& opcode.opcode != ZEND_RECV_VARIADIC
) {
xdebug_count_line(fn, opcode.lineno, 1, deadcode TSRMLS_CC);
}
}
#define XDEBUG_ZNODE_ELEM(node,var) node.var
#if ZEND_USE_ABS_JMP_ADDR
# define XDEBUG_ZNODE_JMP_LINE(node, opline, base) (int32_t)(((long)((node).jmp_addr) - (long)(base_address)) / sizeof(zend_op))
#else
# define XDEBUG_ZNODE_JMP_LINE(node, opline, base) (int32_t)(((int32_t)((node).jmp_offset) / sizeof(zend_op)) + (opline))
#endif
static int xdebug_find_jumps(zend_op_array *opa, unsigned int position, size_t *jump_count, int *jumps)
{
#if ZEND_USE_ABS_JMP_ADDR
zend_op *base_address = &(opa->opcodes[0]);
#endif
zend_op opcode = opa->opcodes[position];
if (opcode.opcode == ZEND_JMP) {
jumps[0] = XDEBUG_ZNODE_JMP_LINE(opcode.op1, position, base_address);
*jump_count = 1;
return 1;
} else if (
opcode.opcode == ZEND_JMPZ ||
opcode.opcode == ZEND_JMPNZ ||
opcode.opcode == ZEND_JMPZ_EX ||
opcode.opcode == ZEND_JMPNZ_EX
) {
jumps[0] = position + 1;
jumps[1] = XDEBUG_ZNODE_JMP_LINE(opcode.op2, position, base_address);
*jump_count = 2;
return 1;
} else if (opcode.opcode == ZEND_JMPZNZ) {
jumps[0] = XDEBUG_ZNODE_JMP_LINE(opcode.op2, position, base_address);
jumps[1] = position + ((int32_t) opcode.extended_value / (int32_t) sizeof(zend_op));
*jump_count = 2;
return 1;
} else if (opcode.opcode == ZEND_FE_FETCH_R || opcode.opcode == ZEND_FE_FETCH_RW) {
jumps[0] = position + 1;
jumps[1] = position + (opcode.extended_value / sizeof(zend_op));
*jump_count = 2;
return 1;
} else if (opcode.opcode == ZEND_FE_RESET_R || opcode.opcode == ZEND_FE_RESET_RW) {
jumps[0] = position + 1;
jumps[1] = XDEBUG_ZNODE_JMP_LINE(opcode.op2, position, base_address);
*jump_count = 2;
return 1;
} else if (opcode.opcode == ZEND_CATCH) {
*jump_count = 2;
jumps[0] = position + 1;
if (!opcode.result.num) {
#if PHP_VERSION_ID >= 70100
jumps[1] = position + (opcode.extended_value / sizeof(zend_op));
#else
jumps[1] = opcode.extended_value;
#endif
if (jumps[1] == jumps[0]) {
jumps[1] = XDEBUG_JMP_NOT_SET;
*jump_count = 1;
}
} else {
jumps[1] = XDEBUG_JMP_EXIT;
}
return 1;
} else if (opcode.opcode == ZEND_GOTO) {
jumps[0] = XDEBUG_ZNODE_JMP_LINE(opcode.op1, position, base_address);
*jump_count = 1;
return 1;
} else if (opcode.opcode == ZEND_FAST_CALL) {
jumps[0] = XDEBUG_ZNODE_JMP_LINE(opcode.op1, position, base_address);
jumps[1] = position + 1;
*jump_count = 2;
return 1;
} else if (opcode.opcode == ZEND_FAST_RET) {
jumps[0] = XDEBUG_JMP_EXIT;
*jump_count = 1;
return 1;
} else if (
opcode.opcode == ZEND_GENERATOR_RETURN ||
opcode.opcode == ZEND_EXIT ||
opcode.opcode == ZEND_THROW ||
opcode.opcode == ZEND_RETURN
) {
jumps[0] = XDEBUG_JMP_EXIT;
*jump_count = 1;
return 1;
#if PHP_VERSION_ID >= 70200
} else if (
opcode.opcode == ZEND_SWITCH_LONG ||
opcode.opcode == ZEND_SWITCH_STRING
) {
zval *array_value;
HashTable *myht;
zval *val;
array_value = RT_CONSTANT_EX(opa->literals, opcode.op2);
myht = Z_ARRVAL_P(array_value);
/* All 'case' statements */
ZEND_HASH_FOREACH_VAL_IND(myht, val) {
if (*jump_count < XDEBUG_BRANCH_MAX_OUTS - 2) {
jumps[*jump_count] = position + (val->value.lval / sizeof(zend_op));
(*jump_count)++;
}
} ZEND_HASH_FOREACH_END();
/* The 'default' case */
jumps[*jump_count] = position + (opcode.extended_value / sizeof(zend_op));
(*jump_count)++;
/* The 'next' opcode */
jumps[*jump_count] = position + 1;
(*jump_count)++;
return 1;
#endif
}
return 0;
}
static void xdebug_analyse_branch(zend_op_array *opa, unsigned int position, xdebug_set *set, xdebug_branch_info *branch_info TSRMLS_DC)
{
/* fprintf(stderr, "Branch analysis from position: %d\n", position); */
if (branch_info) {
xdebug_set_add(branch_info->starts, position);
branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
}
/* First we see if the branch has been visited, if so we bail out. */
if (xdebug_set_in(set, position)) {
return;
}
/* fprintf(stderr, "XDEBUG Adding %d\n", position); */
/* Loop over the opcodes until the end of the array, or until a jump point has been found */
xdebug_set_add(set, position);
while (position < opa->last) {
size_t jump_count = 0;
int jumps[XDEBUG_BRANCH_MAX_OUTS];
size_t i;
/* See if we have a jump instruction */
if (xdebug_find_jumps(opa, position, &jump_count, jumps)) {
for (i = 0; i < jump_count; i++) {
if (jumps[i] == XDEBUG_JMP_EXIT || jumps[i] != XDEBUG_JMP_NOT_SET) {
if (branch_info) {
xdebug_branch_info_update(branch_info, position, opa->opcodes[position].lineno, i, jumps[i]);
}
if (jumps[i] != XDEBUG_JMP_EXIT) {
xdebug_analyse_branch(opa, jumps[i], set, branch_info TSRMLS_CC);
}
}
}
break;
}
/* See if we have a throw instruction */
if (opa->opcodes[position].opcode == ZEND_THROW) {
/* fprintf(stderr, "Throw found at %d\n", position); */
if (branch_info) {
xdebug_set_add(branch_info->ends, position);
branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
}
break;
}
/* See if we have an exit instruction */
if (opa->opcodes[position].opcode == ZEND_EXIT) {
/* fprintf(stderr, "X* Return found\n"); */
if (branch_info) {
xdebug_set_add(branch_info->ends, position);
branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
}
break;
}
/* See if we have a return instruction */
if (
opa->opcodes[position].opcode == ZEND_RETURN
|| opa->opcodes[position].opcode == ZEND_RETURN_BY_REF
) {
/*(fprintf(stderr, "XDEBUG Return found\n");)*/
if (branch_info) {
xdebug_set_add(branch_info->ends, position);
branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
}
break;
}
position++;
/*(fprintf(stderr, "XDEBUG Adding %d\n", position);)*/
xdebug_set_add(set, position);
}
}
static void xdebug_analyse_oparray(zend_op_array *opa, xdebug_set *set, xdebug_branch_info *branch_info TSRMLS_DC)
{
unsigned int position = 0;
while (position < opa->last) {
if (position == 0) {
xdebug_analyse_branch(opa, position, set, branch_info TSRMLS_CC);
if (branch_info) {
xdebug_set_add(branch_info->entry_points, position);
}
} else if (opa->opcodes[position].opcode == ZEND_CATCH) {
xdebug_analyse_branch(opa, position, set, branch_info TSRMLS_CC);
if (branch_info) {
xdebug_set_add(branch_info->entry_points, position);
}
}
position++;
}
if (branch_info) {
xdebug_set_add(branch_info->ends, opa->last-1);
branch_info->branches[opa->last-1].start_lineno = opa->opcodes[opa->last-1].lineno;
}
}
void xdebug_build_fname_from_oparray(xdebug_func *tmp, zend_op_array *opa TSRMLS_DC)
{
int closure = 0;
memset(tmp, 0, sizeof(xdebug_func));
if (opa->function_name) {
if (strcmp(STR_NAME_VAL(opa->function_name), "{closure}") == 0) {
tmp->function = xdebug_sprintf(
"{closure:%s:%d-%d}",
STR_NAME_VAL(opa->filename),
opa->line_start,
opa->line_end
);
closure = 1;
} else {
tmp->function = xdstrdup(STR_NAME_VAL(opa->function_name));
}
} else {
tmp->function = xdstrdup("{main}");
}
if (opa->scope && !closure) {
tmp->type = XFUNC_MEMBER;
tmp->class = xdstrdup(STR_NAME_VAL(opa->scope->name));
} else {
tmp->type = XFUNC_NORMAL;
}
}
char* xdebug_func_format(xdebug_func *func TSRMLS_DC)
{
switch (func->type) {
case XFUNC_NORMAL:
return xdstrdup(func->function);
case XFUNC_MEMBER:
return xdebug_sprintf("%s->%s", func->class, func->function);
default:
return xdstrdup("???");
}
}
static void prefill_from_oparray(char *filename, zend_op_array *op_array TSRMLS_DC)
{
unsigned int i;
xdebug_set *set = NULL;
xdebug_branch_info *branch_info = NULL;
op_array->reserved[XG(dead_code_analysis_tracker_offset)] = (void*) XG(dead_code_last_start_id);
/* Check for abstract methods and simply return from this function in those
* cases. */
if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
return;
}
/* Check whether this function should be filtered out */
{
/*
function_stack_entry tmp_fse;
tmp_fse.filename = STR_NAME_VAL(op_array->filename);
xdebug_build_fname_from_oparray(&tmp_fse.function, op_array TSRMLS_CC);
printf(" - PREFIL FILTERED FOR %s (%s::%s): %s\n",
tmp_fse.filename, tmp_fse.function.class, tmp_fse.function.function,
op_array->reserved[XG(code_coverage_filter_offset)] ? "YES" : "NO");
*/
if (op_array->reserved[XG(code_coverage_filter_offset)]) {
return;
}
}
/* Run dead code analysis if requested */
if (XG(code_coverage_dead_code_analysis) && (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) {
set = xdebug_set_create(op_array->last);
if (XG(code_coverage_branch_check)) {
branch_info = xdebug_branch_info_create(op_array->last);
}
xdebug_analyse_oparray(op_array, set, branch_info TSRMLS_CC);
}
/* The normal loop then finally */
for (i = 0; i < op_array->last; i++) {
zend_op opcode = op_array->opcodes[i];
prefill_from_opcode(filename, opcode, set ? !xdebug_set_in(set, i) : 0 TSRMLS_CC);
}
if (set) {
xdebug_set_free(set);
}
if (branch_info) {
char *function_name;
xdebug_func func_info;
xdebug_build_fname_from_oparray(&func_info, op_array TSRMLS_CC);
function_name = xdebug_func_format(&func_info TSRMLS_CC);
if (func_info.class) {
xdfree(func_info.class);
}
if (func_info.function) {
xdfree(func_info.function);
}
xdebug_branch_post_process(op_array, branch_info);
xdebug_branch_find_paths(branch_info);
xdebug_branch_info_add_branches_and_paths(filename, (char*) function_name, branch_info TSRMLS_CC);
xdfree(function_name);
}
}
static int prefill_from_function_table(zend_op_array *opa)
{
if (opa->type == ZEND_USER_FUNCTION) {
if ((long) opa->reserved[XG(dead_code_analysis_tracker_offset)] < XG(dead_code_last_start_id)) {
prefill_from_oparray((char*) STR_NAME_VAL(opa->filename), opa TSRMLS_CC);
}
}
return ZEND_HASH_APPLY_KEEP;
}
static int prefill_from_class_table(zend_class_entry *class_entry)
{
zend_class_entry *ce;
ce = class_entry;
if (ce->type == ZEND_USER_CLASS) {
if (!(ce->ce_flags & ZEND_XDEBUG_VISITED)) {
zend_op_array *val;
ce->ce_flags |= ZEND_XDEBUG_VISITED;
ZEND_HASH_INC_APPLY_COUNT(&ce->function_table);
ZEND_HASH_FOREACH_PTR(&ce->function_table, val) {
prefill_from_function_table(val);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_DEC_APPLY_COUNT(&ce->function_table);
}
}
return ZEND_HASH_APPLY_KEEP;
}
void xdebug_prefill_code_coverage(zend_op_array *op_array TSRMLS_DC)
{
zend_op_array *function_op_array;
zend_class_entry *class_entry;
if ((long) op_array->reserved[XG(dead_code_analysis_tracker_offset)] < XG(dead_code_last_start_id)) {
prefill_from_oparray((char*) STR_NAME_VAL(op_array->filename), op_array TSRMLS_CC);
}
ZEND_HASH_INC_APPLY_COUNT(CG(function_table));
ZEND_HASH_FOREACH_PTR(CG(function_table), function_op_array) {
prefill_from_function_table(function_op_array);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_DEC_APPLY_COUNT(CG(function_table));
ZEND_HASH_INC_APPLY_COUNT(CG(class_table));
ZEND_HASH_FOREACH_PTR(CG(class_table), class_entry) {
prefill_from_class_table(class_entry);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_DEC_APPLY_COUNT(CG(class_table));
}
void xdebug_code_coverage_start_of_function(zend_op_array *op_array, char *function_name TSRMLS_DC)
{
xdebug_path *path = xdebug_path_new(NULL);
xdebug_prefill_code_coverage(op_array TSRMLS_CC);
xdebug_path_info_add_path_for_level(XG(paths_stack), path, XG(level) TSRMLS_CC);
if (XG(branches).size == 0 || XG(level) >= XG(branches).size) {
XG(branches).size = XG(level) + 32;
XG(branches).last_branch_nr = realloc(XG(branches).last_branch_nr, sizeof(int) * XG(branches.size));
}
XG(branches).last_branch_nr[XG(level)] = -1;
}
void xdebug_code_coverage_end_of_function(zend_op_array *op_array, char *file_name, char *function_name TSRMLS_DC)
{
xdebug_str str = XDEBUG_STR_INITIALIZER;
xdebug_path *path = xdebug_path_info_get_path_for_level(XG(paths_stack), XG(level) TSRMLS_CC);
if (!path) {
return;
}
xdebug_create_key_for_path(path, &str);
xdebug_branch_info_mark_end_of_function_reached(file_name, function_name, str.d, str.l TSRMLS_CC);
xdfree(str.d);
if (path) {
xdebug_path_free(path);
}
}
PHP_FUNCTION(xdebug_start_code_coverage)
{
zend_long options = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &options) == FAILURE) {
return;
}
XG(code_coverage_unused) = (options & XDEBUG_CC_OPTION_UNUSED);
XG(code_coverage_dead_code_analysis) = (options & XDEBUG_CC_OPTION_DEAD_CODE);
XG(code_coverage_branch_check) = (options & XDEBUG_CC_OPTION_BRANCH_CHECK);
if (!XG(extended_info)) {
php_error(E_WARNING, "You can only use code coverage when you leave the setting of 'xdebug.extended_info' to the default '1'.");
RETURN_FALSE;
} else if (!XG(code_coverage)) {
php_error(E_WARNING, "Code coverage needs to be enabled in php.ini by setting 'xdebug.coverage_enable' to '1'.");
RETURN_FALSE;
} else {
XG(do_code_coverage) = 1;
RETURN_TRUE;
}
}
PHP_FUNCTION(xdebug_stop_code_coverage)
{
zend_long cleanup = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &cleanup) == FAILURE) {
return;
}
if (XG(do_code_coverage)) {
if (cleanup) {
XG(previous_filename) = NULL;
XG(previous_file) = NULL;
XG(previous_mark_filename) = NULL;
XG(previous_mark_file) = NULL;
xdebug_hash_destroy(XG(code_coverage));
XG(code_coverage) = xdebug_hash_alloc(32, xdebug_coverage_file_dtor);
XG(dead_code_last_start_id)++;
xdebug_path_info_dtor(XG(paths_stack));
XG(paths_stack) = xdebug_path_info_ctor();
}
XG(do_code_coverage) = 0;
RETURN_TRUE;
}
RETURN_FALSE;
}
static int xdebug_lineno_cmp(const void *a, const void *b TSRMLS_DC)
{