-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdebugger.c
More file actions
1295 lines (1072 loc) · 37.6 KB
/
debugger.c
File metadata and controls
1295 lines (1072 loc) · 37.6 KB
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-2023 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 |
| derick@xdebug.org so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "php_xdebug.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#include "debugger_private.h"
#include "lib/log.h"
#include "lib/var.h"
extern ZEND_DECLARE_MODULE_GLOBALS(xdebug);
static size_t (*xdebug_orig_ub_write)(const char *string, size_t len);
static size_t xdebug_ub_write(const char *string, size_t length);
static void xdebug_line_list_dtor(xdebug_lines_list *line_list);
void xdebug_init_debugger_globals(xdebug_debugger_globals_t *xg)
{
xg->breakpoint_count = 0;
xg->ide_key = NULL;
xg->stdout_mode = 0;
xg->no_exec = 0;
xg->context.program_name = NULL;
xg->context.list.last_filename = NULL;
xg->context.list.last_line = 0;
xg->context.do_break = 0;
xg->context.pending_breakpoint = NULL;
xg->context.do_step = 0;
xg->context.do_next = 0;
xg->context.do_finish = 0;
xg->context.do_connect_to_client = 0;
xg->remote_connection_enabled = 0;
xg->remote_connection_pid = 0;
xg->breakpoints_allowed = 0;
xg->suppress_return_value_step = 0;
/* Capturing output */
if (sapi_module.ub_write != xdebug_ub_write) {
xdebug_orig_ub_write = sapi_module.ub_write;
sapi_module.ub_write = xdebug_ub_write;
}
/* Statistics and diagnostics */
xg->context.connected_hostname = NULL;
xg->context.connected_port = 0;
xg->context.detached_message = NULL;
}
static char *xdebug_debugger_get_ide_key(void)
{
char *ide_key;
ide_key = XINI_DBG(ide_key_setting);
if (ide_key && *ide_key) {
return ide_key;
}
ide_key = getenv("DBGP_IDEKEY");
if (ide_key && *ide_key) {
return ide_key;
}
return NULL;
}
void xdebug_debugger_reset_ide_key(char *envval)
{
if (XG_DBG(ide_key)) {
xdfree(XG_DBG(ide_key));
}
XG_DBG(ide_key) = xdstrdup(envval);
}
int xdebug_debugger_bailout_if_no_exec_requested(void)
{
/* We need to do this first before the executable clauses are called */
if (XG_DBG(no_exec) == 1) {
php_printf("DEBUG SESSION ENDED");
return 1;
}
return 0;
}
void xdebug_debugger_set_program_name(zend_string *filename)
{
if (!XG_DBG(context).program_name) {
XG_DBG(context).program_name = zend_string_copy(filename);
}
}
/* Remote debugger helper functions */
static int xdebug_handle_hit_value(xdebug_brk_info *brk_info)
{
/* If this is a temporary breakpoint, disable the breakpoint */
if (brk_info->temporary) {
brk_info->disabled = 1;
}
/* Increase hit counter */
brk_info->hit_count++;
/* If the hit_value is 0, the condition check is disabled */
if (!brk_info->hit_value) {
return 1;
}
switch (brk_info->hit_condition) {
case XDEBUG_HIT_GREATER_EQUAL:
if (brk_info->hit_count >= brk_info->hit_value) {
return 1;
}
break;
case XDEBUG_HIT_EQUAL:
if (brk_info->hit_count == brk_info->hit_value) {
return 1;
}
break;
case XDEBUG_HIT_MOD:
if (brk_info->hit_count % brk_info->hit_value == 0) {
return 1;
}
break;
case XDEBUG_HIT_DISABLED:
return 1;
break;
}
return 0;
}
int xdebug_do_eval(char *eval_string, zval *ret_zval, zend_string **return_message)
{
volatile int res = 1;
zend_execute_data *original_execute_data = EG(current_execute_data);
int original_no_extensions = EG(no_extensions);
zend_object *original_exception = EG(exception);
JMP_BUF *original_bailout = EG(bailout);
/* Remember error reporting level and track errors */
XG_BASE(error_reporting_override) = EG(error_reporting);
XG_BASE(error_reporting_overridden) = 1;
EG(error_reporting) = 0;
XG_DBG(context).inhibit_notifications = 1;
XG_DBG(breakpoints_allowed) = 0;
/* Reset exception in case we're triggered while being in xdebug_throw_exception_hook */
EG(exception) = NULL;
/* Do evaluation */
zend_first_try {
res = (zend_eval_string(eval_string, ret_zval, (char*) "xdebug://debug-eval") == SUCCESS);
} zend_catch {
res = 0;
} zend_end_try();
if (EG(exception)) {
if (return_message != NULL) {
zend_class_entry *base_ce;
zval *prop, rv;
*return_message = NULL;
base_ce = zend_get_exception_base(EG(exception));
if (base_ce) {
prop = zend_read_property_ex(base_ce, EG(exception), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv);
if (prop) {
*return_message = zval_get_string(prop);
}
}
}
if (!res) {
zend_clear_exception();
}
res = 0;
}
/* Clean up */
EG(error_reporting) = XG_BASE(error_reporting_override);
XG_BASE(error_reporting_overridden) = 0;
XG_DBG(breakpoints_allowed) = 1;
XG_DBG(suppress_return_value_step) = 0;
XG_DBG(context).inhibit_notifications = 0;
EG(current_execute_data) = original_execute_data;
EG(no_extensions) = original_no_extensions;
EG(exception) = original_exception;
EG(bailout) = original_bailout;
return res;
}
bool xdebug_debugger_check_evaled_code_zstr(zend_string *filename_in, zend_string **filename_out)
{
char *end_marker;
xdebug_eval_info *ei;
if (!filename_in) {
return false;
}
end_marker = ZSTR_VAL(filename_in) + ZSTR_LEN(filename_in) - strlen("eval()'d code");
if (end_marker >= ZSTR_VAL(filename_in) && strcmp("eval()'d code", end_marker) == 0) {
if (xdebug_hash_find(XG_DBG(context).eval_id_lookup, ZSTR_VAL(filename_in), ZSTR_LEN(filename_in), (void *) &ei)) {
*filename_out = zend_strpprintf(0, "dbgp://%u", ei->id);
return true;
}
}
return false;
}
bool xdebug_debugger_check_evaled_code_xdebug_str(xdebug_str *filename_in, zend_string **filename_out)
{
char *end_marker;
xdebug_eval_info *ei;
if (!filename_in) {
return false;
}
end_marker = XDEBUG_STR_VAL(filename_in) + XDEBUG_STR_LEN(filename_in) - strlen("eval()'d code");
if (end_marker >= XDEBUG_STR_VAL(filename_in) && strcmp("eval()'d code", end_marker) == 0) {
if (xdebug_hash_find(XG_DBG(context).eval_id_lookup, XDEBUG_STR_VAL(filename_in), XDEBUG_STR_LEN(filename_in), (void *) &ei)) {
*filename_out = zend_strpprintf(0, "dbgp://%u", ei->id);
return true;
}
}
return false;
}
int next_condition_met(function_stack_entry *fse)
{
if (XG_DBG(context).next_stack != NULL) {
if ((XG_DBG(context).next_stack == XG_BASE(stack)) && (XG_DBG(context).next_level >= fse->level)) {
return 1;
}
} else {
if (XG_DBG(context).next_level >= fse->level) {
return 1;
}
}
return 0;
}
int finish_condition_met(function_stack_entry *fse, int break_at_return_scope)
{
if (!break_at_return_scope && fse->level < XG_DBG(context).finish_level) {
return 1;
}
if (break_at_return_scope && fse->level <= XG_DBG(context).finish_level) {
return 1;
}
if (
(fse->level == XG_DBG(context).finish_level) &&
(fse->function_nr > XG_DBG(context).finish_func_nr)
) {
return 1;
}
return 0;
}
/*****************************************************************************
** Path Mapping Helpers
*/
static void log_map_result(int result, const char* remote_filename, int remote_lineno, xdebug_str* local_path, size_t local_line)
{
if (result == XDEBUG_PATH_MAP_TYPE_UNKNOWN) {
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAP-FAIL",
"Couldn't map location %s:%d", remote_filename, remote_lineno
);
return;
}
if (result & XDEBUG_PATH_MAP_FLAGS_SKIP) {
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAP-SKIP",
"Location %s:%d needs to be skipped", remote_filename, remote_lineno
);
return;
}
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAP-OK",
"Mapped location %s:%d to %s:%zd", remote_filename, remote_lineno, XDEBUG_STR_VAL(local_path), local_line
);
return;
}
int xdebug_debugger_map_remote_to_local(zend_string *remote_filename, int remote_lineno, xdebug_str **local_path, size_t *local_line, bool *must_free)
{
int result;
if (!xdebug_lib_path_mapping_enabled()) {
goto pass_through;
}
xdebug_log_ex(XLOG_CHAN_PATHMAP, XLOG_INFO, "ENABLED", "Mapping remote location %s:%d", ZSTR_VAL(remote_filename), remote_lineno);
result = xdebug_path_maps_remote_to_local(
ZSTR_VAL(remote_filename), remote_lineno,
local_path, local_line
);
log_map_result(result, ZSTR_VAL(remote_filename), remote_lineno, *local_path, *local_line);
if (result == XDEBUG_PATH_MAP_TYPE_UNKNOWN) {
pass_through:
*local_path = xdebug_str_create(ZSTR_VAL(remote_filename), ZSTR_LEN(remote_filename));
*local_line = remote_lineno;
*must_free = true;
return XDEBUG_PATH_MAP_RESULT_UNKNOWN;
}
if (result & XDEBUG_PATH_MAP_FLAGS_SKIP) {
*local_path = xdebug_str_create(ZSTR_VAL(remote_filename), ZSTR_LEN(remote_filename));
*local_line = remote_lineno;
*must_free = true;
return XDEBUG_PATH_MAP_RESULT_SKIP;
}
/* local_path and local_file have been overwritten by xdebug_path_maps_remote_to_local() */
*must_free = false;
return XDEBUG_PATH_MAP_RESULT_OK;
}
void xdebug_debugger_statement_call(zend_string *filename, int lineno)
{
xdebug_llist_element *le;
xdebug_brk_info *extra_brk_info;
function_stack_entry *fse;
xdebug_str *mapped_path;
size_t mapped_lineno;
bool must_free_mapped_path = false;
if (XG_DBG(context).do_connect_to_client) {
XG_DBG(context).do_connect_to_client = 0;
xdebug_debug_init_if_requested_on_connect_to_client();
xdebug_debug_init_if_requested_on_xdebug_break();
}
if (!xdebug_is_debug_connection_active()) {
return;
}
if (!XG_BASE(stack) || !XDEBUG_VECTOR_TAIL(XG_BASE(stack))) {
return;
}
/* Map paths */
if (xdebug_debugger_map_remote_to_local(filename, lineno, &mapped_path, &mapped_lineno, &must_free_mapped_path) == XDEBUG_PATH_MAP_RESULT_SKIP) {
return;
}
/* Fetch top level fse */
fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));
/* If we're in a user-defined main function, with a valid function, with two or more opcodes,
* with the current opcode to be the next-to-last one, and the last opcode being ZEND_RETURN,
* and the value of ZEND_RETURN is a constant, and the value of ZEND_RETURN is the default, then bail out */
if (
fse->user_defined && (fse->function.type & XFUNC_INCLUDES) &&
/*EG(current_execute_data)->opline && */EG(current_execute_data)->func && /*EG(current_execute_data)->func->op_array && */
EG(current_execute_data)->func->op_array.last >= 2 &&
((EG(current_execute_data)->opline - EG(current_execute_data)->func->op_array.opcodes) == EG(current_execute_data)->func->op_array.last - 2) &&
EG(current_execute_data)->func->op_array.opcodes[EG(current_execute_data)->func->op_array.last - 1].opcode == ZEND_RETURN &&
EG(current_execute_data)->func->op_array.opcodes[EG(current_execute_data)->func->op_array.last - 1].op1_type == IS_CONST
) {
return;
}
XG_DBG(suppress_return_value_step) = 0;
if (XG_DBG(context).do_break) {
xdebug_brk_info *brk_info = XG_DBG(context).pending_breakpoint;
XG_DBG(context).do_break = 0;
XG_DBG(context).pending_breakpoint = NULL;
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_BREAK, NULL, 0, NULL, brk_info, NULL)) {
xdebug_mark_debug_connection_not_active();
goto finish;
}
goto finish;
}
/* Check for "finish" (step_out) */
if (XG_DBG(context).do_finish && finish_condition_met(fse, 0)) {
XG_DBG(context).do_finish = 0;
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_STEP, NULL, 0, NULL, NULL, NULL)) {
xdebug_mark_debug_connection_not_active();
goto finish;
}
goto finish;
}
/* Check for "next" (step_over) */
if (XG_DBG(context).do_next && next_condition_met(fse)) {
XG_DBG(context).do_next = 0;
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_STEP, NULL, 0, NULL, NULL, NULL)) {
xdebug_mark_debug_connection_not_active();
goto finish;
}
goto finish;
}
/* Check for "step" (step_into) */
if (XG_DBG(context).do_step) {
XG_DBG(context).do_step = 0;
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_STEP, NULL, 0, NULL, NULL, NULL)) {
xdebug_mark_debug_connection_not_active();
goto finish;
}
goto finish;
}
if (fse->has_line_breakpoints && XG_DBG(context).line_breakpoints) {
int break_ok, res;
zval retval;
for (le = XDEBUG_LLIST_HEAD(XG_DBG(context).line_breakpoints); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
extra_brk_info = XDEBUG_LLIST_VALP(le);
if (XG_DBG(context).handler->break_on_line(&(XG_DBG(context)), extra_brk_info, filename, lineno)) {
break_ok = 1; /* Breaking is allowed by default */
/* Check if we have a condition set for it */
if (extra_brk_info->condition) {
/* If there is a condition, we disable breaking by
* default and only enabled it when the code evaluates
* to TRUE */
break_ok = 0;
/* Remember error reporting level */
res = xdebug_do_eval(extra_brk_info->condition, &retval, NULL);
if (res) {
break_ok = Z_TYPE(retval) == IS_TRUE;
zval_ptr_dtor_nogc(&retval);
}
}
if (break_ok && xdebug_handle_hit_value(extra_brk_info)) {
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_BREAK, NULL, 0, NULL, extra_brk_info, NULL)) {
xdebug_mark_debug_connection_not_active();
break;
}
goto finish;
}
}
}
}
finish:
/* Free mapped path */
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
}
void xdebug_debugger_throw_exception_hook(zend_object *exception, zval *file, zval *line, zval *code, char *code_str, zval *message)
{
zend_class_entry *exception_ce = exception->ce;
xdebug_brk_info *extra_brk_info;
xdebug_str *mapped_path;
size_t mapped_lineno;
bool must_free_mapped_path = false;
/* Start JIT if requested and not yet enabled */
xdebug_debug_init_if_requested_on_error();
if (!xdebug_is_debug_connection_active() || !XG_DBG(breakpoints_allowed)) {
return;
}
/* Map paths */
if (xdebug_debugger_map_remote_to_local(zend_get_executed_filename_ex(), zend_get_executed_lineno(), &mapped_path, &mapped_lineno, &must_free_mapped_path) == XDEBUG_PATH_MAP_RESULT_SKIP) {
return;
}
{
int exception_breakpoint_found = 0;
XG_DBG(suppress_return_value_step) = 1;
/* Check if we have a wild card exception breakpoint */
if (xdebug_hash_find(XG_DBG(context).exception_breakpoints, "*", 1, (void *) &extra_brk_info)) {
exception_breakpoint_found = 1;
} else {
/* Check if we have a breakpoint on this exception or its parent classes */
zend_class_entry *ce_ptr = exception_ce;
/* Check if we have a breakpoint on this exception or its parent classes */
do {
if (xdebug_hash_find(XG_DBG(context).exception_breakpoints, (char *) STR_NAME_VAL(ce_ptr->name), STR_NAME_LEN(ce_ptr->name), (void *) &extra_brk_info)) {
exception_breakpoint_found = 1;
}
ce_ptr = ce_ptr->parent;
} while (!exception_breakpoint_found && ce_ptr);
}
#if 0
if (XG_DBG(context).resolved_breakpoints && exception_breakpoint_found) {
XG_DBG(context).handler->resolve_breakpoints(&(XG_DBG(context)), extra_brk_info);
}
#endif
if (exception_breakpoint_found && xdebug_handle_hit_value(extra_brk_info)) {
if (
!XG_DBG(context).handler->remote_breakpoint(
&(XG_DBG(context)), XG_BASE(stack),
mapped_path, mapped_lineno,
XDEBUG_BREAK,
(char*) STR_NAME_VAL(exception_ce->name),
code_str ? code_str : ((code && Z_TYPE_P(code) == IS_STRING) ? Z_STRVAL_P(code) : NULL),
message ? Z_STRVAL_P(message) : "",
extra_brk_info,
NULL
)
) {
xdebug_mark_debug_connection_not_active();
}
}
}
/* Free mapped path */
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
}
void xdebug_debugger_error_cb(zend_string *error_filename, int error_lineno, int type, char *error_type_str, char *buffer)
{
xdebug_brk_info *extra_brk_info = NULL;
xdebug_str *mapped_path;
size_t mapped_lineno;
bool must_free_mapped_path = false;
/* Start JIT if requested and not yet enabled */
xdebug_debug_init_if_requested_on_error();
if (!xdebug_is_debug_connection_active() || !XG_DBG(breakpoints_allowed)) {
return;
}
/* Map paths */
if (xdebug_debugger_map_remote_to_local(error_filename, error_lineno, &mapped_path, &mapped_lineno, &must_free_mapped_path) == XDEBUG_PATH_MAP_RESULT_SKIP) {
return;
}
/* Send notification with warning/notice/error information */
if (XG_DBG(context).send_notifications && !XG_DBG(context).inhibit_notifications) {
if (!XG_DBG(context).handler->remote_notification(&(XG_DBG(context)), mapped_path, mapped_lineno, type, error_type_str, buffer)) {
xdebug_mark_debug_connection_not_active();
}
}
/* Check for the pseudo exceptions to allow breakpoints on PHP error statuses */
if (
xdebug_hash_find(XG_DBG(context).exception_breakpoints, error_type_str, strlen(error_type_str), (void *) &extra_brk_info) ||
xdebug_hash_find(XG_DBG(context).exception_breakpoints, "*", 1, (void *) &extra_brk_info)
) {
if (xdebug_handle_hit_value(extra_brk_info)) {
char *type_str = xdebug_sprintf("%ld", type);
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_BREAK, error_type_str, type_str, buffer, extra_brk_info, NULL)) {
xdebug_mark_debug_connection_not_active();
}
xdfree(type_str);
}
}
/* Free mapped path */
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
}
static bool handle_function_breakpoints(function_stack_entry *fse, int breakpoint_type, zval *return_value)
{
char *tmp_name = NULL;
size_t tmp_len = 0;
xdebug_brk_info *extra_brk_info = NULL;
xdebug_str *mapped_path;
size_t mapped_lineno;
bool must_free_mapped_path = false;
/* Short circuit if there are no function breakpoints */
if (!XG_DBG(context).function_breakpoints || XG_DBG(context).function_breakpoints->size == 0) {
return true;
}
/* Map paths */
if (xdebug_debugger_map_remote_to_local(fse->filename, fse->lineno, &mapped_path, &mapped_lineno, &must_free_mapped_path) == XDEBUG_PATH_MAP_RESULT_SKIP) {
return true;
}
/* Function breakpoints */
if (fse->function.type == XFUNC_NORMAL) {
tmp_len = 2 + ZSTR_LEN(fse->function.function) + 1;
tmp_name = xdmalloc(tmp_len);
/* We intentionally do not use xdebug_sprintf because it can create a bottleneck in large
* codebases due to setlocale calls. We don't care about the locale here. */
snprintf(
tmp_name, tmp_len,
"%c/%s",
(breakpoint_type & XDEBUG_BREAKPOINT_TYPE_CALL) ? 'C' : 'R',
ZSTR_VAL(fse->function.function)
);
}
/* class->function breakpoints */
else if (fse->function.type == XFUNC_MEMBER || fse->function.type == XFUNC_STATIC_MEMBER) {
/* Using strlen(ZSTR_VAL(...)) here to cut of the string at the first \0, which is needed
* for anonymous classes, in combination with the snprintf() below */
tmp_len = 2 + ZSTR_LEN(fse->function.object_class) + 2 + ZSTR_LEN(fse->function.function) + 1;
tmp_name = xdmalloc(tmp_len);
/* We intentionally do not use xdebug_sprintf because it can create a bottleneck in large
* codebases due to setlocale calls. We don't care about the locale here. */
snprintf(
tmp_name, tmp_len,
"%c/%s::%s",
(breakpoint_type & XDEBUG_BREAKPOINT_TYPE_CALL) ? 'C' : 'R',
ZSTR_VAL(fse->function.object_class), ZSTR_VAL(fse->function.function)
);
}
/* Unknown */
else {
goto finish;
}
if (xdebug_hash_find(XG_DBG(context).function_breakpoints, tmp_name, tmp_len - 1, (void *) &extra_brk_info)) {
/* Yup, breakpoint found, call handler if the breakpoint is not
* disabled AND handle_hit_value is happy */
if (!extra_brk_info->disabled && (extra_brk_info->function_break_type == (breakpoint_type & XDEBUG_BREAKPOINT_TYPES_MASK))) {
if (xdebug_handle_hit_value(extra_brk_info)) {
if (fse->user_defined == XDEBUG_BUILT_IN || (breakpoint_type & XDEBUG_BREAKPOINT_TYPE_RETURN)) {
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_BREAK, NULL, 0, NULL, extra_brk_info, return_value)) {
xdfree(tmp_name);
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
return false;
}
} else {
XG_DBG(context).do_break = 1;
XG_DBG(context).pending_breakpoint = extra_brk_info;
}
}
}
}
xdfree(tmp_name);
finish:
/* Free mapped path */
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
return true;
}
void xdebug_debugger_set_has_line_breakpoints(function_stack_entry *fse)
{
if (fse->has_line_breakpoints) {
return;
}
fse->has_line_breakpoints = true;
xdebug_log_ex(
XLOG_CHAN_DEBUG, XLOG_DEBUG, "HLB",
"Setting 'has_line_breakpoints on %s (%s:%d)",
fse->function.function ? ZSTR_VAL(fse->function.function) : "{no func}",
ZSTR_VAL(fse->filename), fse->lineno
);
}
static void mark_fse_as_having_line_breakpoints(function_stack_entry *fse)
{
xdebug_llist_element *le;
if (!XG_DBG(context).line_breakpoints || XG_DBG(context).line_breakpoints->size == 0) {
return;
}
/* loop over all line breakpoints until one hits, and if so, turn on 'has_line_breakpoints' */
for (le = XDEBUG_LLIST_HEAD(XG_DBG(context).line_breakpoints); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
xdebug_brk_info *extra_brk_info = XDEBUG_LLIST_VALP(le);
zend_string *executed_filename = zend_get_executed_filename_ex();
if (!executed_filename) {
continue;
}
if (fse->function.type == XFUNC_EVAL) {
zend_string *resolved_filename;
if (!xdebug_debugger_check_evaled_code_zstr(executed_filename, &resolved_filename)) {
continue;
}
if (!zend_string_equals(extra_brk_info->filename, resolved_filename)) {
zend_string_release(resolved_filename);
continue;
}
zend_string_release(resolved_filename);
} else if (!zend_string_equals(extra_brk_info->filename, executed_filename)) {
continue;
}
if (extra_brk_info->resolved_lineno >= fse->op_array->line_start && extra_brk_info->resolved_lineno <= fse->op_array->line_end) {
xdebug_debugger_set_has_line_breakpoints(fse);
return;
}
}
}
/* Returns false if something is wrong with the breakpoint */
static bool handle_breakpoints(function_stack_entry *fse, int breakpoint_type, zval *return_value)
{
xdebug_str *mapped_path;
size_t mapped_lineno;
bool must_free_mapped_path = false;
zend_string *executed_filename;
/* If 'has_line_breakpoints' hasn't been marked, either use the resolve
* list if it exists, or otherwise mark it as 'true' */
if (!fse->has_line_breakpoints) {
mark_fse_as_having_line_breakpoints(fse);
}
/* Handle function entry and exit breakpoints */
if (!handle_function_breakpoints(fse, breakpoint_type, return_value)) {
return false;
}
/* See if we need to do something with return value breakpoints */
if (
!(XG_DBG(context).breakpoint_include_return_value) ||
!(breakpoint_type & XDEBUG_BREAKPOINT_TYPE_RETURN) ||
(XG_DBG(suppress_return_value_step)) ||
!return_value
) {
return true;
}
/* Check whether we need to skip this break */
executed_filename = zend_get_executed_filename_ex();
if (executed_filename && xdebug_debugger_map_remote_to_local(executed_filename, zend_get_executed_lineno(), &mapped_path, &mapped_lineno, &must_free_mapped_path) == XDEBUG_PATH_MAP_RESULT_SKIP) {
return true;
}
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
/* Map paths for breakpoint location */
if (xdebug_debugger_map_remote_to_local(fse->filename, fse->lineno, &mapped_path, &mapped_lineno, &must_free_mapped_path) == XDEBUG_PATH_MAP_RESULT_SKIP) {
return true;
}
/* Do breaking logic */
if (XG_DBG(context).do_step) {
XG_DBG(context).do_step = 0;
} else if (XG_DBG(context).do_finish && finish_condition_met(fse, 1)) {
XG_DBG(context).do_finish = 0;
} else {
goto finish;
}
if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), mapped_path, mapped_lineno, XDEBUG_BREAK, NULL, 0, NULL, NULL, return_value)) {
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
return false;
}
finish:
/* Free mapped path */
if (must_free_mapped_path) {
xdebug_str_free(mapped_path);
}
return true;
}
void xdebug_debugger_handle_breakpoints(function_stack_entry *fse, int breakpoint_type, zval *return_value)
{
if (xdebug_is_debug_connection_active() && XG_DBG(breakpoints_allowed)) {
if (!handle_breakpoints(fse, breakpoint_type, return_value)) {
xdebug_mark_debug_connection_not_active();
}
}
}
static size_t xdebug_ub_write(const char *string, size_t length)
{
if (xdebug_is_debug_connection_active()) {
if (-1 == XG_DBG(context).handler->remote_stream_output(string, length)) {
return 0;
}
}
return xdebug_orig_ub_write(string, length);
}
static void xdebug_hook_output_handlers()
{
/* Override output handler for capturing output */
if (xdebug_orig_ub_write == NULL) {
xdebug_orig_ub_write = sapi_module.ub_write;
sapi_module.ub_write = xdebug_ub_write;
}
}
static void xdebug_unhook_output_handlers()
{
/* Restore original output handler */
sapi_module.ub_write = xdebug_orig_ub_write;
xdebug_orig_ub_write = NULL;
}
void xdebug_debugger_zend_startup(void)
{
/* Hook output handlers (header and output writer) */
xdebug_hook_output_handlers();
}
void xdebug_debugger_zend_shutdown(void)
{
/* Remove our hooks to output handlers (header and output writer) */
xdebug_unhook_output_handlers();
}
void xdebug_debugger_minit(void)
{
XG_DBG(breakpoint_count) = 0;
}
void xdebug_debugger_minfo(void)
{
php_info_print_table_start();
php_info_print_table_header(2, "Debugger", "enabled");
php_info_print_table_row(2, "IDE Key", XG_DBG(ide_key));
php_info_print_table_end();
}
void xdebug_debugger_rinit(void)
{
char *idekey;
/* Get the ide key for this session */
XG_DBG(ide_key) = NULL;
idekey = xdebug_debugger_get_ide_key();
if (idekey && *idekey) {
if (XG_DBG(ide_key)) {
xdfree(XG_DBG(ide_key));
}
XG_DBG(ide_key) = xdstrdup(idekey);
}
XG_DBG(no_exec) = 0;
xdebug_lib_set_active_symbol_table(NULL);
/* Check if we have this special get variable that stops a debugging
* request without executing any code */
{
zend_string *stop_no_exec = zend_string_init(ZEND_STRL("XDEBUG_SESSION_STOP_NO_EXEC"), 0);
if (
(
(
zend_hash_find(Z_ARR(PG(http_globals)[TRACK_VARS_GET]), stop_no_exec) != NULL
) || (
zend_hash_find(Z_ARR(PG(http_globals)[TRACK_VARS_POST]), stop_no_exec) != NULL
)
)
&& !SG(headers_sent)
) {
xdebug_setcookie("XDEBUG_SESSION", sizeof("XDEBUG_SESSION") - 1, (char*) "", 0, 0, "/", 1, NULL, 0, 0, 1, 0);
XG_DBG(no_exec) = 1;
}
zend_string_release(stop_no_exec);
}
xdebug_mark_debug_connection_not_active();
XG_DBG(context).socket = -1;
XG_DBG(breakpoints_allowed) = 1;
XG_DBG(suppress_return_value_step) = 0;
XG_DBG(detached) = 0;
XG_DBG(breakable_lines_map) = xdebug_hash_alloc(2048, (xdebug_hash_dtor_t) xdebug_line_list_dtor);
XG_DBG(function_count) = 0;
XG_DBG(class_count) = 0;
/* Initialize some debugger context properties */
XG_DBG(context).program_name = NULL;
XG_DBG(context).list.last_filename = NULL;
XG_DBG(context).list.last_line = 0;
XG_DBG(context).do_break = 0;
XG_DBG(context).pending_breakpoint = NULL;
XG_DBG(context).do_step = 0;
XG_DBG(context).do_next = 0;
XG_DBG(context).do_finish = 0;
XG_DBG(context).do_connect_to_client = 0;
/* Statistics and diagnostics */
XG_DBG(context).connected_hostname = NULL;
XG_DBG(context).connected_port = 0;
XG_DBG(context).detached_message = NULL;
}
void xdebug_debugger_post_deactivate(void)
{
if (XG_DBG(remote_connection_enabled)) {
XG_DBG(context).handler->remote_deinit(&(XG_DBG(context)));
xdebug_close_socket(XG_DBG(context).socket);
}
if (XG_DBG(context).program_name) {
zend_string_release(XG_DBG(context).program_name);
}
if (XG_DBG(ide_key)) {
xdfree(XG_DBG(ide_key));
XG_DBG(ide_key) = NULL;
}
if (XG_DBG(context.list.last_filename)) {
zend_string_release(XG_DBG(context).list.last_filename);
XG_DBG(context).list.last_filename = NULL;
}
xdebug_hash_destroy(XG_DBG(breakable_lines_map));
XG_DBG(breakable_lines_map) = NULL;
if (XG_DBG(context).connected_hostname) {
xdfree(XG_DBG(context).connected_hostname);
XG_DBG(context).connected_hostname = NULL;
}
if (XG_DBG(context).detached_message) {
xdfree(XG_DBG(context).detached_message);
XG_DBG(context).detached_message = NULL;
}
}
xdebug_set *xdebug_debugger_get_breakable_lines_from_oparray(zend_op_array *opa)
{
int i;
xdebug_set *tmp;
tmp = xdebug_set_create(opa->line_end);
for (i = 0; i < opa->last; i++ ) {
if (opa->opcodes[i].opcode == ZEND_EXT_STMT ) {
xdebug_set_add(tmp, opa->opcodes[i].lineno);
}
}
return tmp;
}
/* {{{ function/lines map collection helpers */
static void xdebug_function_lines_map_dtor(xdebug_function_lines_map_item *lines_map)
{
xdebug_set_free(lines_map->lines_breakable);
xdfree(lines_map);
}
static void xdebug_line_list_dtor(xdebug_lines_list *line_list)
{