forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug_stack.c
1271 lines (1122 loc) · 38.8 KB
/
xdebug_stack.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-2011 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.0 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://xdebug.derickrethans.nl/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_code_coverage.h"
#include "xdebug_compat.h"
#include "xdebug_profiler.h"
#include "xdebug_stack.h"
#include "xdebug_str.h"
#include "xdebug_superglobals.h"
#include "xdebug_var.h"
#include "ext/standard/html.h"
#include "main/php_ini.h"
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
static char* text_formats[10] = {
"\n",
"%s: %s in %s on line %d\n",
"\nCall Stack:\n",
#if HAVE_PHP_MEMORY_USAGE
"%10.4f %10ld %3d. %s(",
#else
"%10.4f %3d. %s(",
#endif
"'%s'",
") %s:%d\n",
"\n\nVariables in local scope (#%d):\n",
"\n",
" $%s = %s\n",
" $%s = *uninitialized*\n"
};
#ifndef PHP_WIN32
static char* ansi_formats[10] = {
"\n",
"\e[1m\e[31m%s\e[0m: %s\e[22m in \e[31m%s\e[0m on line \e[32m%d\e[0m\e[22m\n",
"\n\e[1mCall Stack:\e[22m\n",
#if HAVE_PHP_MEMORY_USAGE
"%10.4f %10ld %3d. %s(",
#else
"%10.4f %3d. %s(",
#endif
"'%s'",
") %s:%d\n",
"\n\nVariables in local scope (#%d):\n",
"\n",
" $%s = %s\n",
" $%s = *uninitialized*\n"
};
#endif
static char* html_formats[12] = {
"<br />\n<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>\n",
"<tr><th align='left' bgcolor='#f57900' colspan=\"5\"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> %s: %s in %s on line <i>%d</i></th></tr>\n",
#if HAVE_PHP_MEMORY_USAGE
"<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>\n<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>\n",
"<tr><td bgcolor='#eeeeec' align='center'>%d</td><td bgcolor='#eeeeec' align='center'>%.4f</td><td bgcolor='#eeeeec' align='right'>%ld</td><td bgcolor='#eeeeec'>%s( ",
#else
"<tr><th align='left' bgcolor='#e9b96e' colspan='4'>Call Stack</th></tr>\n<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>\n",
"<tr><td bgcolor='#eeeeec' align='center'>%d</td><td bgcolor='#eeeeec' align='center'>%.4f</td><td bgcolor='#eeeeec'>%s( ",
#endif
"<font color='#00bb00'>'%s'</font>",
" )</td><td title='%s' bgcolor='#eeeeec'>..%s<b>:</b>%d</td></tr>\n",
#if HAVE_PHP_MEMORY_USAGE
"<tr><th align='left' colspan='5' bgcolor='#e9b96e'>Variables in local scope (#%d)</th></tr>\n",
#else
"<tr><th align='left' colspan='4' bgcolor='#e9b96e'>Variables in local scope (#%d)</th></tr>\n",
#endif
"</table></font>\n",
"<tr><td colspan='2' align='right' bgcolor='#eeeeec' valign='top'><pre>$%s =</pre></td><td colspan='3' bgcolor='#eeeeec'>%s</td></tr>\n",
"<tr><td colspan='2' align='right' bgcolor='#eeeeec' valign='top'><pre>$%s =</pre></td><td colspan='3' bgcolor='#eeeeec' valign='top'><i>Undefined</i></td></tr>\n",
" )</td><td title='%s' bgcolor='#eeeeec'><a style='color: black' href='%s'>..%s<b>:</b>%d</a></td></tr>\n",
"<tr><th align='left' bgcolor='#f57900' colspan=\"5\"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> %s: %s in <a style='color: black' href='%s'>%s</a> on line <i>%d</i></th></tr>\n"
};
static char** select_formats(int html TSRMLS_DC) {
if (html) {
return html_formats;
}
#ifndef PHP_WIN32
else if (XG(cli_color) == 1 && xdebug_is_output_tty(TSRMLS_C)) {
return ansi_formats;
}
#endif
else {
return text_formats;
}
}
static void dump_used_var_with_contents(void *htmlq, xdebug_hash_element* he, void *argument)
{
int html = *(int *)htmlq;
int len;
zval *zvar;
char *contents;
char *name = (char*) he->ptr;
HashTable *tmp_ht;
char **formats;
xdebug_str *str = (xdebug_str *) argument;
TSRMLS_FETCH();
if (!he->ptr) {
return;
}
/* Bail out on $this and $GLOBALS */
if (strcmp(name, "this") == 0 || strcmp(name, "GLOBALS") == 0) {
return;
}
#if PHP_VERSION_ID >= 50300
if (!EG(active_symbol_table)) {
zend_rebuild_symbol_table(TSRMLS_C);
}
#endif
tmp_ht = XG(active_symbol_table);
XG(active_symbol_table) = EG(active_symbol_table);
zvar = xdebug_get_php_symbol(name, strlen(name) + 1);
XG(active_symbol_table) = tmp_ht;
formats = select_formats(PG(html_errors) TSRMLS_CC);
if (!zvar) {
xdebug_str_add(str, xdebug_sprintf(formats[9], name), 1);
return;
}
if (html) {
contents = xdebug_get_zval_value_fancy(NULL, zvar, &len, 0, NULL TSRMLS_CC);
} else {
contents = xdebug_get_zval_value(zvar, 0, NULL);
}
if (contents) {
xdebug_str_add(str, xdebug_sprintf(formats[8], name, contents), 1);
} else {
xdebug_str_add(str, xdebug_sprintf(formats[9], name), 1);
}
xdfree(contents);
}
void xdebug_log_stack(const char *error_type_str, char *buffer, const char *error_filename, const int error_lineno TSRMLS_DC)
{
xdebug_llist_element *le;
function_stack_entry *i;
char *tmp_log_message;
tmp_log_message = xdebug_sprintf( "PHP %s: %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno);
php_log_err(tmp_log_message TSRMLS_CC);
xdfree(tmp_log_message);
if (XG(stack) && XG(stack)->size) {
php_log_err("PHP Stack trace:" TSRMLS_CC);
for (le = XDEBUG_LLIST_HEAD(XG(stack)); le != NULL; le = XDEBUG_LLIST_NEXT(le))
{
int c = 0; /* Comma flag */
int j = 0; /* Counter */
char *tmp_name;
xdebug_str log_buffer = {0, 0, NULL};
i = XDEBUG_LLIST_VALP(le);
tmp_name = xdebug_show_fname(i->function, 0, 0 TSRMLS_CC);
xdebug_str_add(&log_buffer, xdebug_sprintf("PHP %3d. %s(", i->level, tmp_name), 1);
xdfree(tmp_name);
/* Printing vars */
for (j = 0; j < i->varc; j++) {
char *tmp_varname, *tmp_value;
if (c) {
xdebug_str_addl(&log_buffer, ", ", 2, 0);
} else {
c = 1;
}
tmp_varname = i->var[j].name ? xdebug_sprintf("$%s = ", i->var[j].name) : xdstrdup("");
xdebug_str_add(&log_buffer, tmp_varname, 0);
xdfree(tmp_varname);
if (i->var[j].addr) {
tmp_value = xdebug_get_zval_value(i->var[j].addr, 0, NULL);
xdebug_str_add(&log_buffer, tmp_value, 0);
xdfree(tmp_value);
} else {
xdebug_str_addl(&log_buffer, "*uninitialized*", 15, 0);
}
}
xdebug_str_add(&log_buffer, xdebug_sprintf(") %s:%d", i->filename, i->lineno), 1);
php_log_err(log_buffer.d TSRMLS_CC);
xdebug_str_free(&log_buffer);
}
}
}
static int create_file_link(char **filename, const char *error_filename, int error_lineno TSRMLS_DC)
{
xdebug_str fname = {0, 0, NULL};
char *format = XG(file_link_format);
while (*format)
{
if (*format != '%') {
xdebug_str_addl(&fname, (char *) format, 1, 0);
} else {
format++;
switch (*format)
{
case 'f': /* filename */
xdebug_str_add(&fname, xdebug_sprintf("%s", error_filename), 1);
break;
case 'l': /* line number */
xdebug_str_add(&fname, xdebug_sprintf("%d", error_lineno), 1);
break;
case '%': /* literal % */
xdebug_str_addl(&fname, "%", 1, 0);
break;
}
}
format++;
}
*filename = fname.d;
return fname.l;
}
void xdebug_append_error_head(xdebug_str *str, int html TSRMLS_DC)
{
char **formats = select_formats(html TSRMLS_CC);
xdebug_str_add(str, formats[0], 0);
}
void xdebug_append_error_description(xdebug_str *str, int html, const char *error_type_str, char *buffer, const char *error_filename, const int error_lineno TSRMLS_DC)
{
char **formats = select_formats(html TSRMLS_CC);
char *escaped;
#if PHP_VERSION_ID >= 50400
size_t newlen;
#else
int newlen;
#endif
if (html) {
escaped = php_escape_html_entities_ex(buffer, strlen(buffer), &newlen, 0, 0, NULL, 1 TSRMLS_CC);
} else {
escaped = estrdup(buffer);
}
if (strlen(XG(file_link_format)) > 0 && html) {
char *file_link;
create_file_link(&file_link, error_filename, error_lineno TSRMLS_CC);
xdebug_str_add(str, xdebug_sprintf(formats[11], error_type_str, escaped, file_link, error_filename, error_lineno), 1);
xdfree(file_link);
} else {
xdebug_str_add(str, xdebug_sprintf(formats[1], error_type_str, escaped, error_filename, error_lineno), 1);
}
efree(escaped);
}
void xdebug_append_printable_stack(xdebug_str *str, int html TSRMLS_DC)
{
xdebug_llist_element *le;
function_stack_entry *i;
int len;
char **formats = select_formats(html TSRMLS_CC);
if (XG(stack) && XG(stack)->size) {
i = XDEBUG_LLIST_VALP(XDEBUG_LLIST_HEAD(XG(stack)));
xdebug_str_add(str, formats[2], 0);
for (le = XDEBUG_LLIST_HEAD(XG(stack)); le != NULL; le = XDEBUG_LLIST_NEXT(le))
{
int c = 0; /* Comma flag */
int j = 0; /* Counter */
char *tmp_name;
i = XDEBUG_LLIST_VALP(le);
tmp_name = xdebug_show_fname(i->function, html, 0 TSRMLS_CC);
if (html) {
#if HAVE_PHP_MEMORY_USAGE
xdebug_str_add(str, xdebug_sprintf(formats[3], i->level, i->time - XG(start_time), i->memory, tmp_name), 1);
#else
xdebug_str_add(str, xdebug_sprintf(formats[3], i->level, i->time - XG(start_time), tmp_name), 1);
#endif
} else {
#if HAVE_PHP_MEMORY_USAGE
xdebug_str_add(str, xdebug_sprintf(formats[3], i->time - XG(start_time), i->memory, i->level, tmp_name), 1);
#else
xdebug_str_add(str, xdebug_sprintf(formats[3], i->time - XG(start_time), i->level, tmp_name), 1);
#endif
}
xdfree(tmp_name);
/* Printing vars */
for (j = 0; j < i->varc; j++) {
char *tmp_value, *tmp_fancy_value, *tmp_fancy_synop_value;
int newlen;
if (c) {
xdebug_str_addl(str, ", ", 2, 0);
} else {
c = 1;
}
if (i->var[j].name && XG(collect_params) >= 4) {
if (html) {
xdebug_str_add(str, xdebug_sprintf("<span>$%s = </span>", i->var[j].name), 1);
} else {
xdebug_str_add(str, xdebug_sprintf("$%s = ", i->var[j].name), 1);
}
}
if (i->var[j].addr) {
if (html) {
tmp_value = xdebug_get_zval_value(i->var[j].addr, 0, NULL);
tmp_fancy_value = xdebug_xmlize(tmp_value, strlen(tmp_value), &newlen);
tmp_fancy_synop_value = xdebug_get_zval_synopsis_fancy("", i->var[j].addr, &len, 0, NULL TSRMLS_CC);
switch (XG(collect_params)) {
case 1: // synopsis
xdebug_str_add(str, xdebug_sprintf("<span>%s</span>", tmp_fancy_synop_value), 1);
break;
case 2: // synopsis + full in tooltip
xdebug_str_add(str, xdebug_sprintf("<span title='%s'>%s</span>", tmp_fancy_value, tmp_fancy_synop_value), 1);
break;
case 3: // full
default:
xdebug_str_add(str, xdebug_sprintf("<span>%s</span>", tmp_fancy_value), 1);
break;
}
xdfree(tmp_value);
efree(tmp_fancy_value);
xdfree(tmp_fancy_synop_value);
} else {
switch (XG(collect_params)) {
case 1: // synopsis
case 2:
tmp_value = xdebug_get_zval_synopsis(i->var[j].addr, 0, NULL);
break;
case 3:
default:
tmp_value = xdebug_get_zval_value(i->var[j].addr, 0, NULL);
break;
}
if (tmp_value) {
xdebug_str_add(str, xdebug_sprintf("%s", tmp_value), 1);
xdfree(tmp_value);
} else {
xdebug_str_addl(str, "???", 3, 0);
}
}
} else {
xdebug_str_addl(str, "???", 3, 0);
}
}
if (i->include_filename) {
xdebug_str_add(str, xdebug_sprintf(formats[4], i->include_filename), 1);
}
if (html) {
if (strlen(XG(file_link_format)) > 0) {
char *just_filename = strrchr(i->filename, DEFAULT_SLASH);
char *file_link;
create_file_link(&file_link, i->filename, i->lineno TSRMLS_CC);
xdebug_str_add(str, xdebug_sprintf(formats[10], i->filename, file_link, just_filename, i->lineno), 1);
xdfree(file_link);
} else {
char *just_filename = strrchr(i->filename, DEFAULT_SLASH);
xdebug_str_add(str, xdebug_sprintf(formats[5], i->filename, just_filename, i->lineno), 1);
}
} else {
xdebug_str_add(str, xdebug_sprintf(formats[5], i->filename, i->lineno), 1);
}
}
if (XG(dump_globals) && !(XG(dump_once) && XG(dumped))) {
char *tmp = xdebug_get_printable_superglobals(html TSRMLS_CC);
if (tmp) {
xdebug_str_add(str, tmp, 1);
}
XG(dumped) = 1;
}
if (XG(show_local_vars) && XG(stack) && XDEBUG_LLIST_TAIL(XG(stack))) {
int scope_nr = XG(stack)->size;
i = XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack)));
if (i->user_defined == XDEBUG_INTERNAL && XDEBUG_LLIST_PREV(XDEBUG_LLIST_TAIL(XG(stack))) && XDEBUG_LLIST_VALP(XDEBUG_LLIST_PREV(XDEBUG_LLIST_TAIL(XG(stack))))) {
i = XDEBUG_LLIST_VALP(XDEBUG_LLIST_PREV(XDEBUG_LLIST_TAIL(XG(stack))));
scope_nr--;
}
if (i->used_vars && i->used_vars->size) {
xdebug_hash *tmp_hash;
xdebug_str_add(str, xdebug_sprintf(formats[6], scope_nr), 1);
tmp_hash = xdebug_used_var_hash_from_llist(i->used_vars);
xdebug_hash_apply_with_argument(tmp_hash, (void*) &html, dump_used_var_with_contents, (void *) str);
xdebug_hash_destroy(tmp_hash);
}
}
}
}
void xdebug_append_error_footer(xdebug_str *str, int html TSRMLS_DC)
{
char **formats = select_formats(html TSRMLS_CC);
xdebug_str_add(str, formats[7], 0);
}
static char *get_printable_stack(int html, const char *error_type_str, char *buffer, const char *error_filename, const int error_lineno TSRMLS_DC)
{
char *prepend_string;
char *append_string;
xdebug_str str = {0, 0, NULL};
prepend_string = INI_STR("error_prepend_string");
append_string = INI_STR("error_append_string");
xdebug_str_add(&str, prepend_string ? prepend_string : "", 0);
xdebug_append_error_head(&str, html TSRMLS_CC);
xdebug_append_error_description(&str, html, error_type_str, buffer, error_filename, error_lineno TSRMLS_CC);
xdebug_append_printable_stack(&str, html TSRMLS_CC);
xdebug_append_error_footer(&str, html TSRMLS_CC);
xdebug_str_add(&str, append_string ? append_string : "", 0);
return str.d;
}
#define XDEBUG_LOG_PRINT(fs, string, ...) if (fs) { fprintf(fs, string, ## __VA_ARGS__); }
void xdebug_init_debugger(TSRMLS_D)
{
xdebug_open_log(TSRMLS_C);
if (XG(remote_connect_back)) {
zval **remote_addr = NULL;
XDEBUG_LOG_PRINT(XG(remote_log_file), "I: Checking remote connect back address.\n");
if (zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "X_HTTP_FORWARDED_FOR", 21, (void**)&remote_addr) == FAILURE) {
zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "REMOTE_ADDR", 12, (void**)&remote_addr);
}
if (remote_addr) {
XDEBUG_LOG_PRINT(XG(remote_log_file), "I: Remote address found, connecting to %s:%d.\n", Z_STRVAL_PP(remote_addr), XG(remote_port));
XG(context).socket = xdebug_create_socket(Z_STRVAL_PP(remote_addr), XG(remote_port));
} else {
XDEBUG_LOG_PRINT(XG(remote_log_file), "W: Remote address not found, connecting to configured address/port: %s:%d. :-|\n", XG(remote_host), XG(remote_port));
XG(context).socket = xdebug_create_socket(XG(remote_host), XG(remote_port));
}
} else {
XDEBUG_LOG_PRINT(XG(remote_log_file), "I: Connecting to configured address/port: %s:%d.\n", XG(remote_host), XG(remote_port));
XG(context).socket = xdebug_create_socket(XG(remote_host), XG(remote_port));
}
if (XG(context).socket >= 0) {
XDEBUG_LOG_PRINT(XG(remote_log_file), "I: Connected to client. :-)\n");
XG(remote_enabled) = 0;
/* Get handler from mode */
XG(context).handler = xdebug_handler_get(XG(remote_handler));
if (!XG(context).handler) {
zend_error(E_WARNING, "The remote debug handler '%s' is not supported.", XG(remote_handler));
XDEBUG_LOG_PRINT(XG(remote_log_file), "E: The remote debug handler '%s' is not supported. :-(\n", XG(remote_handler));
} else if (!XG(context).handler->remote_init(&(XG(context)), XDEBUG_REQ)) {
/* The request could not be started, ignore it then */
XDEBUG_LOG_PRINT(XG(remote_log_file), "E: The debug session could not be started. :-(\n");
} else {
/* All is well, turn off script time outs */
XG(remote_enabled) = 1;
}
} else {
XDEBUG_LOG_PRINT(XG(remote_log_file), "E: Could not connect to client. :-(\n");
}
if (!XG(remote_enabled)) {
xdebug_close_log(TSRMLS_C);
}
}
void xdebug_do_jit(TSRMLS_D)
{
if (!XG(remote_enabled) && XG(remote_enable) && (XG(remote_mode) == XDEBUG_JIT)) {
xdebug_init_debugger(TSRMLS_C);
}
}
static void php_output_error(const char *error TSRMLS_DC)
{
#ifdef PHP_DISPLAY_ERRORS_STDERR
if (PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR) {
fputs(error, stderr);
fflush(stderr);
return;
}
#endif
php_printf("%s", error);
}
/* Error callback for formatting stack traces */
void xdebug_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
{
char *buffer, *error_type_str;
int buffer_len;
xdebug_brk_info *extra_brk_info = NULL;
error_handling_t error_handling;
zend_class_entry *exception_class;
TSRMLS_FETCH();
buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args);
error_type_str = xdebug_error_type(type);
/* Store last error message for error_get_last() */
if (PG(last_error_message)) {
free(PG(last_error_message));
}
if (PG(last_error_file)) {
free(PG(last_error_file));
}
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 2) || PHP_MAJOR_VERSION >= 6
PG(last_error_type) = type;
#endif
PG(last_error_message) = strdup(buffer);
PG(last_error_file) = strdup(error_filename);
PG(last_error_lineno) = error_lineno;
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) || PHP_MAJOR_VERSION >= 6
error_handling = EG(error_handling);
exception_class = EG(exception_class);
#else
error_handling = PG(error_handling);
exception_class = PG(exception_class);
#endif
/* according to error handling mode, suppress error, throw exception or show it */
if (error_handling != EH_NORMAL && EG(in_execution)) {
switch (type) {
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
/* fatal errors are real errors and cannot be made exceptions */
break;
case E_STRICT:
/* for the sake of BC to old damaged code */
break;
case E_NOTICE:
case E_USER_NOTICE:
/* notices are no errors and are not treated as such like E_WARNINGS */
break;
default:
/* throw an exception if we are in EH_THROW mode
* but DO NOT overwrite a pending exception
*/
if (error_handling == EH_THROW && !EG(exception)) {
zend_throw_error_exception(exception_class, buffer, 0, type TSRMLS_CC);
}
efree(buffer);
xdfree(error_type_str);
return;
}
}
if (EG(error_reporting) & type) {
/* Log to logger */
if (PG(log_errors)) {
#ifdef PHP_WIN32
if (type==E_CORE_ERROR || type==E_CORE_WARNING) {
MessageBox(NULL, buffer, error_type_str, MB_OK|ZEND_SERVICE_MB_STYLE);
}
#endif
xdebug_log_stack(error_type_str, buffer, error_filename, error_lineno TSRMLS_CC);
}
/* Display errors */
if (PG(display_errors) && !PG(during_request_startup)) {
char *printable_stack;
/* We need to see if we have an uncaught exception fatal error now */
if (type == E_ERROR && strncmp(buffer, "Uncaught exception", 18) == 0) {
xdebug_str str = {0, 0, NULL};
char *tmp_buf, *p;
/* find first new line */
p = strchr(buffer, '\n');
/* find last quote */
p = ((char *) zend_memrchr(buffer, '\'', p - buffer)) + 1;
/* Create new buffer */
tmp_buf = calloc(p - buffer + 1, 1);
strncpy(tmp_buf, buffer, p - buffer );
/* Append error */
xdebug_append_error_head(&str, PG(html_errors) TSRMLS_CC);
xdebug_append_error_description(&str, PG(html_errors), error_type_str, tmp_buf, error_filename, error_lineno TSRMLS_CC);
xdebug_append_printable_stack(&str, PG(html_errors) TSRMLS_CC);
xdebug_str_add(&str, XG(last_exception_trace), 0);
xdebug_append_error_footer(&str, PG(html_errors) TSRMLS_CC);
php_output_error(str.d TSRMLS_CC);
xdfree(str.d);
free(tmp_buf);
} else {
printable_stack = get_printable_stack(PG(html_errors), error_type_str, buffer, error_filename, error_lineno TSRMLS_CC);
php_output_error(printable_stack TSRMLS_CC);
xdfree(printable_stack);
}
}
if (XG(do_collect_errors)) {
char *printable_stack;
printable_stack = get_printable_stack(PG(html_errors), error_type_str, buffer, error_filename, error_lineno TSRMLS_CC);
xdebug_llist_insert_next(XG(collected_errors), XDEBUG_LLIST_TAIL(XG(collected_errors)), printable_stack);
}
}
/* Start JIT if requested and not yet enabled */
xdebug_do_jit(TSRMLS_C);
/* Check for the pseudo exceptions to allow breakpoints on PHP error statuses */
if (XG(remote_enabled) && XG(breakpoints_allowed)) {
if (xdebug_hash_find(XG(context).exception_breakpoints, error_type_str, strlen(error_type_str), (void *) &extra_brk_info)) {
if (xdebug_handle_hit_value(extra_brk_info)) {
if (!XG(context).handler->remote_breakpoint(&(XG(context)), XG(stack), error_filename, error_lineno, XDEBUG_BREAK, error_type_str, buffer)) {
XG(remote_enabled) = 0;
}
}
}
}
xdfree(error_type_str);
#if PHP_VERSION_ID < 50400
/* Bail out if we can't recover */
switch (type) {
case E_CORE_ERROR:
/* no break - intentionally */
case E_ERROR:
#if PHP_VERSION_ID >= 50200
case E_RECOVERABLE_ERROR:
#endif
/*case E_PARSE: the parser would return 1 (failure), we can bail out nicely */
case E_COMPILE_ERROR:
case E_USER_ERROR:
EG(exit_status) = 255;
#if HAVE_PHP_MEMORY_USAGE
/* restore memory limit */
# if PHP_VERSION_ID >= 50200
zend_set_memory_limit(PG(memory_limit));
# else
AG(memory_limit) = PG(memory_limit);
# endif
#endif
zend_objects_store_mark_destructed(&EG(objects_store) TSRMLS_CC);
zend_bailout();
return;
}
#else
/* Bail out if we can't recover */
switch (type) {
case E_CORE_ERROR:
if (!php_get_module_initialized()) {
/* bad error in module startup - no way we can live with this */
exit(-2);
}
case E_ERROR:
case E_RECOVERABLE_ERROR:
case E_PARSE:
case E_COMPILE_ERROR:
case E_USER_ERROR:
EG(exit_status) = 255;
if (php_get_module_initialized()) {
if (!PG(display_errors) &&
!SG(headers_sent) &&
SG(sapi_headers).http_response_code == 200
) {
sapi_header_line ctr = {0};
ctr.line = "HTTP/1.0 500 Internal Server Error";
ctr.line_len = sizeof("HTTP/1.0 500 Internal Server Error") - 1;
sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
}
/* the parser would return 1 (failure), we can bail out nicely */
if (type != E_PARSE) {
/* restore memory limit */
zend_set_memory_limit(PG(memory_limit));
zend_objects_store_mark_destructed(&EG(objects_store) TSRMLS_CC);
zend_bailout();
return;
}
}
break;
}
#endif
if (PG(track_errors) && EG(active_symbol_table)) {
zval *tmp;
ALLOC_ZVAL(tmp);
INIT_PZVAL(tmp);
Z_STRVAL_P(tmp) = (char *) estrndup(buffer, buffer_len);
Z_STRLEN_P(tmp) = buffer_len;
Z_TYPE_P(tmp) = IS_STRING;
zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) & tmp, sizeof(zval *), NULL);
}
efree(buffer);
}
/* {{{ proto array xdebug_print_function_stack([string message])
Displays a stack trace */
PHP_FUNCTION(xdebug_print_function_stack)
{
char *message = NULL;
int message_len;
function_stack_entry *i;
char *tmp;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &message_len) == FAILURE) {
return;
}
i = xdebug_get_stack_frame(0 TSRMLS_CC);
if (message) {
tmp = get_printable_stack(PG(html_errors), "Xdebug", message, i->filename, i->lineno TSRMLS_CC);
} else {
tmp = get_printable_stack(PG(html_errors), "Xdebug", "user triggered", i->filename, i->lineno TSRMLS_CC);
}
php_printf("%s", tmp);
xdfree(tmp);
}
/* }}} */
/* {{{ proto array xdebug_get_formatted_function_stack()
Displays a stack trace */
PHP_FUNCTION(xdebug_get_formatted_function_stack)
{
function_stack_entry *i;
char *tmp;
i = xdebug_get_stack_frame(0 TSRMLS_CC);
tmp = get_printable_stack(PG(html_errors), "Xdebug", "user triggered", i->filename, i->lineno TSRMLS_CC);
RETVAL_STRING(tmp, 1);
xdfree(tmp);
}
/* }}} */
/* {{{ proto string xdebug_call_class()
Returns the name of the calling class */
PHP_FUNCTION(xdebug_call_class)
{
function_stack_entry *i;
long depth = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &depth) == FAILURE) {
return;
}
i = xdebug_get_stack_frame(2 + depth TSRMLS_CC);
if (i) {
RETURN_STRING(i->function.class ? i->function.class : "", 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string xdebug_call_function()
Returns the function name from which the current function was called from. */
PHP_FUNCTION(xdebug_call_function)
{
function_stack_entry *i;
long depth = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &depth) == FAILURE) {
return;
}
i = xdebug_get_stack_frame(2 + depth TSRMLS_CC);
if (i) {
RETURN_STRING(i->function.function ? i->function.function : "{}", 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string xdebug_call_line()
Returns the line number where the current function was called from. */
PHP_FUNCTION(xdebug_call_line)
{
function_stack_entry *i;
long depth = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &depth) == FAILURE) {
return;
}
i = xdebug_get_stack_frame(1 + depth TSRMLS_CC);
if (i) {
RETURN_LONG(i->lineno);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int xdebug_call_file()
Returns the filename where the current function was called from. */
PHP_FUNCTION(xdebug_call_file)
{
function_stack_entry *i;
long depth = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &depth) == FAILURE) {
return;
}
i = xdebug_get_stack_frame(1 + depth TSRMLS_CC);
if (i) {
RETURN_STRING(i->filename, 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
static void xdebug_build_fname(xdebug_func *tmp, zend_execute_data *edata TSRMLS_DC)
{
memset(tmp, 0, sizeof(xdebug_func));
if (edata) {
if (edata->function_state.function->common.function_name) {
if (edata->object) {
tmp->type = XFUNC_MEMBER;
if (edata->function_state.function->common.scope) { /* __autoload has no scope */
tmp->class = xdstrdup(edata->function_state.function->common.scope->name);
}
} else if (EG(scope) && edata->function_state.function->common.scope && edata->function_state.function->common.scope->name) {
tmp->type = XFUNC_STATIC_MEMBER;
tmp->class = xdstrdup(edata->function_state.function->common.scope->name);
} else {
tmp->type = XFUNC_NORMAL;
}
if (strcmp(edata->function_state.function->common.function_name, "{closure}") == 0) {
tmp->function = xdebug_sprintf(
"{closure:%s:%d-%d}",
edata->function_state.function->op_array.filename,
edata->function_state.function->op_array.line_start,
edata->function_state.function->op_array.line_end
);
} else {
tmp->function = xdstrdup(edata->function_state.function->common.function_name);
}
} else {
#if PHP_VERSION_ID >= 50399
switch (edata->opline->extended_value) {
#else
switch (edata->opline->op2.u.constant.value.lval) {
#endif
case ZEND_EVAL:
tmp->type = XFUNC_EVAL;
break;
case ZEND_INCLUDE:
tmp->type = XFUNC_INCLUDE;
break;
case ZEND_REQUIRE:
tmp->type = XFUNC_REQUIRE;
break;
case ZEND_INCLUDE_ONCE:
tmp->type = XFUNC_INCLUDE_ONCE;
break;
case ZEND_REQUIRE_ONCE:
tmp->type = XFUNC_REQUIRE_ONCE;
break;
default:
tmp->type = XFUNC_UNKNOWN;
break;
}
}
}
}
function_stack_entry *xdebug_add_stack_frame(zend_execute_data *zdata, zend_op_array *op_array, int type TSRMLS_DC)
{
zend_execute_data *edata = EG(current_execute_data);
function_stack_entry *tmp;
zend_op *cur_opcode;
zval **param;
int i = 0;
char *aggr_key;
int aggr_key_len;
tmp = xdmalloc (sizeof (function_stack_entry));
tmp->var = NULL;
tmp->varc = 0;
tmp->refcount = 1;
tmp->level = XG(level);
tmp->arg_done = 0;
tmp->used_vars = NULL;
tmp->user_defined = type;
tmp->filename = NULL;
tmp->include_filename = NULL;
tmp->profile.call_list = NULL;
tmp->op_array = op_array;
tmp->symbol_table = NULL;
tmp->execute_data = NULL;
XG(function_count)++;
if (edata && edata->op_array) {
/* Normal function calls */
tmp->filename = xdstrdup(edata->op_array->filename);
} else if (edata && edata->prev_execute_data && XDEBUG_LLIST_TAIL(XG(stack))
) {
tmp->filename = xdstrdup(((function_stack_entry*) XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack))))->filename);
}
if (!tmp->filename) {
/* Includes/main script etc */
tmp->filename = (op_array && op_array->filename) ? xdstrdup(op_array->filename): NULL;
}
/* Call user function locations */
if (!tmp->filename && XDEBUG_LLIST_TAIL(XG(stack)) && XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack))) ) {
tmp->filename = xdstrdup(((function_stack_entry*) XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack))))->filename);
}
#if HAVE_PHP_MEMORY_USAGE
tmp->prev_memory = XG(prev_memory);
tmp->memory = XG_MEMORY_USAGE();
XG(prev_memory) = tmp->memory;
#else
tmp->memory = 0;
tmp->prev_memory = 0;
#endif
tmp->time = xdebug_get_utime();
tmp->lineno = 0;
xdebug_build_fname(&(tmp->function), zdata TSRMLS_CC);
if (!tmp->function.type) {
tmp->function.function = xdstrdup("{main}");
tmp->function.class = NULL;
tmp->function.type = XFUNC_NORMAL;
} else if (tmp->function.type & XFUNC_INCLUDES) {
if (EG(opline_ptr)) {
cur_opcode = *EG(opline_ptr);
tmp->lineno = cur_opcode->lineno;
} else {
tmp->lineno = 0;
}
if (tmp->function.type == XFUNC_EVAL) {
tmp->include_filename = xdebug_sprintf("%s", XG(last_eval_statement));
} else if (XG(collect_includes)) {
tmp->include_filename = xdstrdup(zend_get_executed_filename(TSRMLS_C));
}
} else {
if (edata->opline) {
cur_opcode = edata->opline;
if (cur_opcode) {
tmp->lineno = cur_opcode->lineno;
}
} else if (edata->prev_execute_data && edata->prev_execute_data->opline) {
cur_opcode = edata->prev_execute_data->opline;
if (cur_opcode) {
tmp->lineno = cur_opcode->lineno;
}
}
if (XG(remote_enabled) || XG(collect_params) || XG(collect_vars)) {
void **p;
int arguments_sent = 0, arguments_wanted = 0, arguments_storage = 0;
/* This calculates how many arguments where sent to a function. It
* works for both internal and user defined functions.
* op_array->num_args works only for user defined functions so
* we're not using that here. */
#if PHP_VERSION_ID >= 50300
void **curpos = NULL;