-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhandler_dbgp.c
More file actions
3207 lines (2627 loc) · 101 KB
/
handler_dbgp.c
File metadata and controls
3207 lines (2627 loc) · 101 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-2025 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 <errno.h>
#include <sys/types.h>
#ifndef PHP_WIN32
#include <unistd.h>
#endif
#include "lib/php-header.h"
#include "SAPI.h"
#include "ext/standard/php_string.h"
#include "ext/standard/url.h"
#include "main/php_version.h"
#include "main/php_network.h"
#include "ext/standard/base64.h"
#include "TSRM.h"
#include "php_globals.h"
#include "php_xdebug.h"
#include "com.h"
#include "handler_dbgp.h"
#include "debugger_private.h"
#include "lib/compat.h"
#include "lib/hash.h"
#include "lib/llist.h"
#include "lib/log.h"
#include "lib/mm.h"
#include "lib/var_export_xml.h"
#include "lib/vector.h"
#include "lib/xdebug_strndup.h"
#include "lib/xml.h"
#ifdef PHP_WIN32
#include "win32/time.h"
#include <process.h>
#endif
#include <fcntl.h>
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
xdebug_remote_handler xdebug_handler_dbgp = {
xdebug_dbgp_init,
xdebug_dbgp_deinit,
xdebug_dbgp_error,
xdebug_dbgp_break_on_line,
xdebug_dbgp_breakpoint,
xdebug_dbgp_resolve_breakpoints,
xdebug_dbgp_stream_output,
xdebug_dbgp_notification,
xdebug_dbgp_user_notify,
xdebug_dbgp_register_eval_id,
};
static char *create_eval_key_id(int id);
static void line_breakpoint_resolve_helper(xdebug_con *context, xdebug_lines_list *lines_list, xdebug_brk_info *brk_info);
/*****************************************************************************
** Constants and strings for statii and reasons
*/
/* Status structure */
#define DBGP_STATUS_STARTING 1
#define DBGP_STATUS_STOPPING 2
#define DBGP_STATUS_STOPPED 3
#define DBGP_STATUS_RUNNING 4
#define DBGP_STATUS_BREAK 5
#define DBGP_STATUS_DETACHED 6
const char *xdebug_dbgp_status_strings[7] =
{"", "starting", "stopping", "stopped", "running", "break", "detached"};
#define DBGP_REASON_OK 0
#define DBGP_REASON_ERROR 1
#define DBGP_REASON_ABORTED 2
#define DBGP_REASON_EXCEPTION 3
const char *xdebug_dbgp_reason_strings[4] =
{"ok", "error", "aborted", "exception"};
#define XDEBUG_ERROR_OK 0
#define XDEBUG_ERROR_PARSE 1
#define XDEBUG_ERROR_DUP_ARG 2
#define XDEBUG_ERROR_INVALID_ARGS 3
#define XDEBUG_ERROR_UNIMPLEMENTED 4
#define XDEBUG_ERROR_COMMAND_UNAVAILABLE 5
#define XDEBUG_ERROR_CANT_OPEN_FILE 100
#define XDEBUG_ERROR_STREAM_REDIRECT_FAILED 101 /* unused */
#define XDEBUG_ERROR_BREAKPOINT_NOT_SET 200
#define XDEBUG_ERROR_BREAKPOINT_TYPE_NOT_SUPPORTED 201
#define XDEBUG_ERROR_BREAKPOINT_INVALID 202
#define XDEBUG_ERROR_BREAKPOINT_NO_CODE 203
#define XDEBUG_ERROR_BREAKPOINT_INVALID_STATE 204
#define XDEBUG_ERROR_NO_SUCH_BREAKPOINT 205
#define XDEBUG_ERROR_EVALUATING_CODE 206
#define XDEBUG_ERROR_INVALID_EXPRESSION 207 /* unused */
#define XDEBUG_ERROR_PROPERTY_NON_EXISTENT 300
#define XDEBUG_ERROR_PROPERTY_NON_EXISTANT 300 /* compatibility typo */
#define XDEBUG_ERROR_STACK_DEPTH_INVALID 301
#define XDEBUG_ERROR_CONTEXT_INVALID 302 /* unused */
#define XDEBUG_ERROR_ENCODING_NOT_SUPPORTED 900
typedef struct {
int code;
const char *message;
} xdebug_error_entry;
static xdebug_error_entry xdebug_error_codes[24] = {
{ 0, "no error" },
{ 1, "parse error in command" },
{ 2, "duplicate arguments in command" },
{ 3, "invalid or missing options" },
{ 4, "unimplemented command" },
{ 5, "command is not available" },
{ 100, "can not open file" },
{ 101, "stream redirect failed" },
{ 200, "breakpoint could not be set" },
{ 201, "breakpoint type is not supported" },
{ 202, "invalid breakpoint line" },
{ 203, "no code on breakpoint line" },
{ 204, "invalid breakpoint state" },
{ 205, "no such breakpoint" },
{ 206, "error evaluating code" },
{ 207, "invalid expression" },
{ 300, "can not get property" },
{ 301, "stack depth invalid" },
{ 302, "context invalid" },
{ 900, "encoding not supported" },
{ 998, "an internal exception in the debugger" },
{ 999, "unknown error" },
{ -1, NULL }
};
static const char *error_message_from_code(int code)
{
xdebug_error_entry *error_entry = &xdebug_error_codes[0];
while (error_entry->message) {
if (code == error_entry->code) {
return error_entry->message;
}
error_entry++;
}
return NULL;
}
#define XDEBUG_STR_SWITCH_DECL char *__switch_variable
#define XDEBUG_STR_SWITCH(s) __switch_variable = (s);
#define XDEBUG_STR_CASE(s) if (strcmp(__switch_variable, s) == 0) {
#define XDEBUG_STR_CASE_END } else
#define XDEBUG_STR_CASE_DEFAULT {
#define XDEBUG_STR_CASE_DEFAULT_END }
#define XDEBUG_TYPES_COUNT 8
const char *xdebug_dbgp_typemap[XDEBUG_TYPES_COUNT][3] = {
/* common, lang, schema */
{"bool", "bool", "xsd:boolean"},
{"int", "int", "xsd:decimal"},
{"float", "float", "xsd:double"},
{"string", "string", "xsd:string"},
{"null", "null", NULL},
{"hash", "array", NULL},
{"object", "object", NULL},
{"resource", "resource", NULL}
};
typedef struct {
int value;
const char *name;
} xdebug_breakpoint_entry;
#define XDEBUG_BREAKPOINT_TYPES_COUNT 6
xdebug_breakpoint_entry xdebug_breakpoint_types[XDEBUG_BREAKPOINT_TYPES_COUNT] = {
{ XDEBUG_BREAKPOINT_TYPE_LINE, "line" },
{ XDEBUG_BREAKPOINT_TYPE_CONDITIONAL, "conditional" },
{ XDEBUG_BREAKPOINT_TYPE_CALL, "call" },
{ XDEBUG_BREAKPOINT_TYPE_RETURN, "return" },
{ XDEBUG_BREAKPOINT_TYPE_EXCEPTION, "exception" },
{ XDEBUG_BREAKPOINT_TYPE_WATCH, "watch" }
};
#define XDEBUG_DBGP_SCAN_RANGE 5
/*****************************************************************************
** Prototypes for debug command handlers
*/
/* DBGP_FUNC(break); */
DBGP_FUNC(breakpoint_get);
DBGP_FUNC(breakpoint_list);
DBGP_FUNC(breakpoint_remove);
DBGP_FUNC(breakpoint_set);
DBGP_FUNC(breakpoint_update);
DBGP_FUNC(context_get);
DBGP_FUNC(context_names);
DBGP_FUNC(eval);
DBGP_FUNC(feature_get);
DBGP_FUNC(feature_set);
DBGP_FUNC(typemap_get);
DBGP_FUNC(property_get);
DBGP_FUNC(property_set);
DBGP_FUNC(property_value);
DBGP_FUNC(source);
DBGP_FUNC(stack_depth);
DBGP_FUNC(stack_get);
DBGP_FUNC(status);
DBGP_FUNC(stderr);
DBGP_FUNC(stdout);
DBGP_FUNC(stop);
DBGP_FUNC(run);
DBGP_FUNC(step_into);
DBGP_FUNC(step_out);
DBGP_FUNC(step_over);
DBGP_FUNC(detach);
/* Non standard comments */
DBGP_FUNC(xcmd_get_executable_lines);
static xdebug_dbgp_cmd dbgp_commands[] = {
DBGP_FUNC_ENTRY(breakpoint_get, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(breakpoint_list, XDEBUG_DBGP_POST_MORTEM)
DBGP_FUNC_ENTRY(breakpoint_remove, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(breakpoint_set, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(breakpoint_update, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(context_get, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(context_names, XDEBUG_DBGP_POST_MORTEM)
DBGP_FUNC_ENTRY(eval, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(feature_get, XDEBUG_DBGP_POST_MORTEM)
DBGP_FUNC_ENTRY(feature_set, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(typemap_get, XDEBUG_DBGP_POST_MORTEM)
DBGP_FUNC_ENTRY(property_get, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(property_set, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(property_value, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(source, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(stack_depth, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(stack_get, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(status, XDEBUG_DBGP_POST_MORTEM)
DBGP_FUNC_ENTRY(stderr, XDEBUG_DBGP_NONE)
DBGP_FUNC_ENTRY(stdout, XDEBUG_DBGP_NONE)
DBGP_CONT_FUNC_ENTRY(run, XDEBUG_DBGP_NONE)
DBGP_CONT_FUNC_ENTRY(step_into, XDEBUG_DBGP_NONE)
DBGP_CONT_FUNC_ENTRY(step_out, XDEBUG_DBGP_NONE)
DBGP_CONT_FUNC_ENTRY(step_over, XDEBUG_DBGP_NONE)
DBGP_STOP_FUNC_ENTRY(stop, XDEBUG_DBGP_POST_MORTEM)
DBGP_STOP_FUNC_ENTRY(detach, XDEBUG_DBGP_POST_MORTEM)
/* Non standard functions */
DBGP_FUNC_ENTRY(xcmd_get_executable_lines, XDEBUG_DBGP_NONE)
{ NULL, NULL, 0 }
};
/*****************************************************************************
** Path Mapping Helpers
*/
static void add_path_map_action_facet(xdebug_xml_node *container, int action)
{
switch (action) {
case XDEBUG_PATH_MAP_RESULT_SKIP:
xdebug_xml_expand_attribute_value(container, "xdebug:facet", "skipped");
break;
case XDEBUG_PATH_MAP_RESULT_OK:
xdebug_xml_expand_attribute_value(container, "xdebug:facet", "mapped");
break;
}
}
static void map_local_to_remote_replace(xdebug_brk_info *brk_info)
{
xdebug_str *remote_path;
size_t remote_line;
if (!xdebug_lib_path_mapping_enabled()) {
return;
}
xdebug_log_ex(XLOG_CHAN_PATHMAP, XLOG_INFO, "ENABLED", "Mapping (to replace) local location %s:%d", ZSTR_VAL(brk_info->filename), brk_info->original_lineno);
if (xdebug_path_maps_local_to_remote(
ZSTR_VAL(brk_info->filename), brk_info->original_lineno,
&remote_path, &remote_line
) != XDEBUG_PATH_MAP_TYPE_UNKNOWN) {
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAPPED",
"Mapped location %s:%d to %s:%zd",
ZSTR_VAL(brk_info->filename), brk_info->original_lineno,
XDEBUG_STR_VAL(remote_path), remote_line
);
zend_string_release(brk_info->filename);
brk_info->filename = zend_string_init(XDEBUG_STR_VAL(remote_path), XDEBUG_STR_LEN(remote_path), false);
brk_info->original_lineno = remote_line;
brk_info->resolved_lineno = remote_line;
return;
}
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAP-FAIL",
"Couldn't map location %s:%d",
ZSTR_VAL(brk_info->filename), brk_info->original_lineno
);
}
static void map_remote_brkinfo_to_local(xdebug_brk_info *brk_info, xdebug_str **local_path, size_t *local_line, bool *must_free)
{
if (!xdebug_lib_path_mapping_enabled()) {
goto pass_through;
}
xdebug_log_ex(XLOG_CHAN_PATHMAP, XLOG_INFO, "ENABLED", "Mapping brkinfo local location %s:%d", ZSTR_VAL(brk_info->filename), brk_info->resolved_lineno);
if (xdebug_path_maps_remote_to_local(
ZSTR_VAL(brk_info->filename), brk_info->resolved_lineno,
local_path, local_line
) != XDEBUG_PATH_MAP_TYPE_UNKNOWN) {
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAPPED",
"Mapped location %s:%d to %s:%zd",
ZSTR_VAL(brk_info->filename), brk_info->resolved_lineno,
XDEBUG_STR_VAL((*local_path)), *local_line
);
*must_free = false;
return;
}
xdebug_log_ex(
XLOG_CHAN_PATHMAP, XLOG_INFO, "MAP-FAIL",
"Couldn't map location %s:%d",
ZSTR_VAL(brk_info->filename), brk_info->resolved_lineno
);
pass_through:
*local_path = xdebug_str_create(ZSTR_VAL(brk_info->filename), ZSTR_LEN(brk_info->filename));
*local_line = brk_info->resolved_lineno;
*must_free = true;
}
/*****************************************************************************
** Utility functions
*/
static xdebug_dbgp_cmd* lookup_cmd(char *cmd)
{
xdebug_dbgp_cmd *ptr = dbgp_commands;
while (ptr->name) {
if (strcmp(ptr->name, cmd) == 0) {
return ptr;
}
ptr++;
}
return NULL;
}
static xdebug_str *make_message(xdebug_con *context, xdebug_xml_node *message)
{
xdebug_str xml_message = XDEBUG_STR_INITIALIZER;
xdebug_str *ret = xdebug_str_new();
xdebug_xml_return_node(message, &xml_message);
xdebug_log(XLOG_CHAN_DEBUG, XLOG_COM, "-> %s\n", xml_message.d);
xdebug_str_add_fmt(ret, "%d", xml_message.l + sizeof("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n") - 1);
xdebug_str_addc(ret, '\0');
xdebug_str_add_literal(ret, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
xdebug_str_add(ret, xml_message.d, 0);
xdebug_str_addc(ret, '\0');
xdebug_str_destroy(&xml_message);
return ret;
}
static void send_message_ex(xdebug_con *context, xdebug_xml_node *message, int stage)
{
xdebug_str *tmp;
size_t bytes_written;
/* Sometimes we end up in 'send_message' although the debugging connection
* is already closed. In that case, we early return. */
if (XG_DBG(status) != DBGP_STATUS_STARTING && !xdebug_is_debug_connection_active()) {
return;
}
tmp = make_message(context, message);
bytes_written = SSENDL(context->socket, tmp->d, tmp->l);
/* Error */
if (bytes_written == -1) {
int current_errno = php_socket_errno();
char *sock_error = php_socket_strerror(current_errno, NULL, 0);
if (current_errno == EPIPE) {
xdebug_log_ex(XLOG_CHAN_DEBUG, XLOG_WARN, "REMCLOSE", "The debugging client closed the connection on socket %d: %s (error: %d).", context->socket, sock_error, current_errno);
xdebug_abort_debugger();
} else {
xdebug_log_ex(XLOG_CHAN_DEBUG, XLOG_WARN, "SENDERR", "There was a problem sending %zd bytes on socket %d: %s (error: %d).", tmp->l, context->socket, sock_error, current_errno);
}
efree(sock_error);
xdebug_str_free(tmp);
return;
}
/* Not enough written, we'll allow that for now */
if (bytes_written != tmp->l) {
char *sock_error = php_socket_strerror(php_socket_errno(), NULL, 0);
xdebug_log_ex(XLOG_CHAN_DEBUG, XLOG_WARN, "SENDERR", "There was a problem sending %zd bytes on socket %d: only %zd bytes were written: %s.", tmp->l, context->socket, bytes_written, sock_error);
efree(sock_error);
}
xdebug_str_free(tmp);
}
static void send_message(xdebug_con *context, xdebug_xml_node *message)
{
send_message_ex(context, message, 0);
}
static xdebug_xml_node* get_symbol(xdebug_str *name, xdebug_var_export_options *options)
{
zval retval;
xdebug_xml_node *tmp_node;
xdebug_get_php_symbol(&retval, name);
if (Z_TYPE(retval) == IS_UNDEF) {
return NULL;
}
if (strcmp(name->d, "this") == 0 && Z_TYPE(retval) == IS_NULL) {
return NULL;
}
tmp_node = xdebug_get_zval_value_xml_node(name, &retval, options);
zval_ptr_dtor_nogc(&retval);
return tmp_node;
}
static int get_symbol_contents(xdebug_str *name, xdebug_xml_node *node, xdebug_var_export_options *options)
{
zval retval;
zval *retval_ptr;
xdebug_get_php_symbol(&retval, name);
if (Z_TYPE(retval) == IS_UNDEF) {
return 0;
}
// TODO WTF???
retval_ptr = &retval;
xdebug_var_export_xml_node(&retval_ptr, name, node, options, 1);
zval_ptr_dtor_nogc(&retval);
return 1;
}
static xdebug_str* return_file_source(zend_string *filename, int begin, int end)
{
php_stream *stream;
int i = begin;
char *line = NULL;
xdebug_str *source = xdebug_str_new();
char *tmp_filename = NULL;
if (i < 0) {
begin = 0;
i = 0;
}
xdebug_str_add_literal(source, "");
tmp_filename = xdebug_path_from_url(filename);
stream = php_stream_open_wrapper(tmp_filename, "rb",
USE_PATH | REPORT_ERRORS,
NULL);
xdfree(tmp_filename);
/* Read until the "begin" line has been read */
if (!stream) {
return NULL;
}
/* skip to the first requested line */
while (i > 0 && !php_stream_eof(stream)) {
if (line) {
efree(line);
line = NULL;
}
line = php_stream_gets(stream, NULL, 1024);
i--;
}
/* Read until the "end" line has been read */
do {
if (line) {
xdebug_str_add(source, line, 0);
efree(line);
line = NULL;
if (php_stream_eof(stream)) break;
}
line = php_stream_gets(stream, NULL, 1024);
i++;
} while (i < end + 1 - begin);
/* Print last line */
if (line) {
efree(line);
line = NULL;
}
php_stream_close(stream);
return source;
}
static xdebug_str* return_eval_source(char *id, int begin, int end)
{
char *key;
xdebug_str *joined;
xdebug_eval_info *ei;
xdebug_arg *parts;
if (begin < 0) {
begin = 0;
}
key = create_eval_key_id(atoi(id));
if (!xdebug_hash_find(XG_DBG(context).eval_id_lookup, key, strlen(key), (void *) &ei)) {
return NULL;
}
parts = xdebug_arg_ctor();
xdebug_explode("\n", ZSTR_VAL(ei->contents), parts, end + 2);
joined = xdebug_join("\n", parts, begin, end);
xdebug_arg_dtor(parts);
return joined;
}
static int is_dbgp_url(char *filename)
{
return (strncmp(filename, "dbgp://", 7) == 0);
}
static xdebug_str* return_source(zend_string *filename, int begin, int end)
{
if (is_dbgp_url(ZSTR_VAL(filename))) {
return return_eval_source(ZSTR_VAL(filename) + 7, begin, end);
} else {
return return_file_source(filename, begin, end);
}
}
static xdebug_xml_node* return_stackframe(int nr)
{
function_stack_entry *fse, *fse_prev;
char *tmp_fname;
zend_string *tmp_filename;
xdebug_xml_node *tmp;
fse = xdebug_get_stack_frame(nr);
fse_prev = xdebug_get_stack_frame(nr - 1);
tmp_fname = xdebug_show_fname(fse->function, 0);
tmp = xdebug_xml_node_init("stack");
xdebug_xml_add_attribute_ex(tmp, "where", xdstrdup(tmp_fname), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "level", xdebug_sprintf("%ld", nr), 0, 1);
if (fse_prev) {
if (xdebug_debugger_check_evaled_code_zstr(fse_prev->filename, &tmp_filename)) {
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("eval"), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "filename", ZSTR_VAL(tmp_filename), 0, 0);
xdebug_xml_add_attribute_ex(tmp, "lineno", xdebug_sprintf("%lu", fse_prev->lineno), 0, 1);
zend_string_release(tmp_filename);
} else {
xdebug_str *local_path;
size_t local_line;
bool must_free_path = false;
add_path_map_action_facet(
tmp,
xdebug_debugger_map_remote_to_local(fse_prev->filename, fse_prev->lineno, &local_path, &local_line, &must_free_path)
);
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("file"), 0, 1);
if (local_path) {
xdebug_xml_add_attribute_ex(tmp, "filename", xdebug_xdebug_str_path_to_url(local_path), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "lineno", xdebug_sprintf("%lu", local_line), 0, 1);
}
if (must_free_path) {
xdebug_str_free(local_path);
}
}
} else {
zend_string *executed_filename = zend_get_executed_filename_ex();
int executed_lineno = zend_get_executed_lineno();
zend_string *tmp_filename;
if (xdebug_debugger_check_evaled_code_zstr(executed_filename, &tmp_filename)) {
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("eval"), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "filename", ZSTR_VAL(tmp_filename), 0, 0);
xdebug_xml_add_attribute_ex(tmp, "lineno", xdebug_sprintf("%lu", executed_lineno), 0, 1);
zend_string_release(tmp_filename);
} else if (executed_filename) {
xdebug_str *local_path;
size_t local_line;
bool must_free_path = false;
add_path_map_action_facet(
tmp,
xdebug_debugger_map_remote_to_local(executed_filename, executed_lineno, &local_path, &local_line, &must_free_path)
);
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("file"), 0, 1);
if (local_path) {
xdebug_xml_add_attribute_ex(tmp, "filename", xdebug_xdebug_str_path_to_url(local_path), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "lineno", xdebug_sprintf("%lu", local_line), 0, 1);
}
if (must_free_path) {
xdebug_str_free(local_path);
}
}
}
xdfree(tmp_fname);
return tmp;
}
/*****************************************************************************
** Client command handlers - Breakpoints
*/
/* Helper functions */
static void xdebug_hash_admin_dtor(xdebug_brk_admin *admin)
{
xdfree(admin->key);
xdfree(admin);
}
static int breakpoint_admin_add(xdebug_con *context, int type, char *key)
{
xdebug_brk_admin *admin = xdmalloc(sizeof(xdebug_brk_admin));
char *hkey;
XG_DBG(breakpoint_count)++;
admin->id = ((xdebug_get_pid() & 0x1ffff) * 10000) + XG_DBG(breakpoint_count);
admin->type = type;
admin->key = xdstrdup(key);
hkey = xdebug_sprintf("%lu", admin->id);
xdebug_hash_add(context->breakpoint_list, hkey, strlen(hkey), (void*) admin);
xdfree(hkey);
return admin->id;
}
static int breakpoint_admin_fetch(xdebug_con *context, char *hkey, int *type, char **key)
{
xdebug_brk_admin *admin;
if (!xdebug_hash_find(context->breakpoint_list, hkey, strlen(hkey), (void *) &admin)) {
return FAILURE;
}
*type = admin->type;
*key = admin->key;
return SUCCESS;
}
static int breakpoint_admin_remove(xdebug_con *context, char *hkey)
{
if (!xdebug_hash_delete(context->breakpoint_list, hkey, strlen(hkey))) {
return FAILURE;
}
return SUCCESS;
}
static void breakpoint_brk_info_add_resolved(xdebug_xml_node *xml, xdebug_brk_info *brk_info)
{
if (!XG_DBG(context).resolved_breakpoints) {
return;
}
if (brk_info->resolved == XDEBUG_BRK_RESOLVED) {
xdebug_xml_add_attribute(xml, "resolved", "resolved");
} else {
xdebug_xml_add_attribute(xml, "resolved", "unresolved");
}
}
static void breakpoint_brk_info_add(xdebug_xml_node *xml, xdebug_brk_info *brk_info)
{
xdebug_xml_add_attribute_ex(xml, "type", xdstrdup(XDEBUG_BREAKPOINT_TYPE_NAME(brk_info->brk_type)), 0, 1);
breakpoint_brk_info_add_resolved(xml, brk_info);
if (brk_info->filename && brk_info->resolved_lineno) {
if (is_dbgp_url(ZSTR_VAL(brk_info->filename))) {
xdebug_xml_add_attribute_ex(xml, "filename", ZSTR_VAL(brk_info->filename), 0, 0);
xdebug_xml_add_attribute_ex(xml, "lineno", xdebug_sprintf("%lu", brk_info->resolved_lineno), 0, 1);
} else {
xdebug_str *local_path;
size_t local_line;
bool must_free_path = false;
map_remote_brkinfo_to_local(brk_info, &local_path, &local_line, &must_free_path);
xdebug_xml_add_attribute_ex(xml, "filename", xdebug_xdebug_str_path_to_url(local_path), 0, 1);
xdebug_xml_add_attribute_ex(xml, "lineno", xdebug_sprintf("%lu", local_line), 0, 1);
if (must_free_path) {
xdebug_str_free(local_path);
}
}
}
if (brk_info->functionname) {
xdebug_xml_add_attribute_ex(xml, "function", xdstrdup(brk_info->functionname), 0, 1);
}
if (brk_info->classname) {
xdebug_xml_add_attribute_ex(xml, "class", xdstrdup(brk_info->classname), 0, 1);
}
if (brk_info->exceptionname) {
xdebug_xml_add_attribute_ex(xml, "exception", xdstrdup(brk_info->exceptionname), 0, 1);
}
if (brk_info->disabled) {
xdebug_xml_add_attribute(xml, "state", "disabled");
} else if (brk_info->temporary) {
xdebug_xml_add_attribute(xml, "state", "temporary");
} else {
xdebug_xml_add_attribute(xml, "state", "enabled");
}
xdebug_xml_add_attribute_ex(xml, "hit_count", xdebug_sprintf("%lu", brk_info->hit_count), 0, 1);
switch (brk_info->hit_condition) {
case XDEBUG_HIT_GREATER_EQUAL:
xdebug_xml_add_attribute(xml, "hit_condition", ">=");
break;
case XDEBUG_HIT_EQUAL:
xdebug_xml_add_attribute(xml, "hit_condition", "==");
break;
case XDEBUG_HIT_MOD:
xdebug_xml_add_attribute(xml, "hit_condition", "%");
break;
}
if (brk_info->condition) {
xdebug_xml_node *condition = xdebug_xml_node_init("expression");
xdebug_xml_add_text_ex(condition, brk_info->condition, strlen(brk_info->condition), 0, 1);
xdebug_xml_add_child(xml, condition);
}
xdebug_xml_add_attribute_ex(xml, "hit_value", xdebug_sprintf("%lu", brk_info->hit_value), 0, 1);
xdebug_xml_add_attribute_ex(xml, "id", xdebug_sprintf("%lu", brk_info->id), 0, 1);
}
static xdebug_brk_info* breakpoint_brk_info_fetch(int type, char *hkey)
{
xdebug_llist_element *le;
xdebug_brk_info *brk_info = NULL;
switch (type) {
case XDEBUG_BREAKPOINT_TYPE_LINE:
case XDEBUG_BREAKPOINT_TYPE_CONDITIONAL: {
xdebug_arg *parts;
/* First we split the key into filename and linenumber */
parts = xdebug_arg_ctor();
xdebug_explode("$", hkey, parts, -1);
/* Second we loop through the list of file/line breakpoints to
* look for our thingy */
for (le = XDEBUG_LLIST_HEAD(XG_DBG(context).line_breakpoints); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
brk_info = XDEBUG_LLIST_VALP(le);
if (atoi(parts->args[1]) == brk_info->original_lineno && memcmp(ZSTR_VAL(brk_info->filename), parts->args[0], ZSTR_LEN(brk_info->filename)) == 0) {
xdebug_arg_dtor(parts);
return brk_info;
}
}
/* Cleaning up */
xdebug_arg_dtor(parts);
break;
}
case XDEBUG_BREAKPOINT_TYPE_CALL:
case XDEBUG_BREAKPOINT_TYPE_RETURN:
if (xdebug_hash_find(XG_DBG(context).function_breakpoints, hkey, strlen(hkey), (void *) &brk_info)) {
return brk_info;
}
break;
case XDEBUG_BREAKPOINT_TYPE_EXCEPTION:
if (xdebug_hash_find(XG_DBG(context).exception_breakpoints, hkey, strlen(hkey), (void *) &brk_info)) {
return brk_info;
}
break;
}
return brk_info;
}
static int breakpoint_remove(int type, char *hkey)
{
xdebug_llist_element *le;
xdebug_brk_info *brk_info = NULL;
int retval = FAILURE;
switch (type) {
case XDEBUG_BREAKPOINT_TYPE_LINE:
case XDEBUG_BREAKPOINT_TYPE_CONDITIONAL: {
xdebug_arg *parts;
/* First we split the key into filename and linenumber */
parts = xdebug_arg_ctor();
xdebug_explode("$", hkey, parts, -1);
/* Second we loop through the list of file/line breakpoints to
* look for our thingy */
for (le = XDEBUG_LLIST_HEAD(XG_DBG(context).line_breakpoints); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
brk_info = XDEBUG_LLIST_VALP(le);
if (atoi(parts->args[1]) == brk_info->original_lineno && memcmp(ZSTR_VAL(brk_info->filename), parts->args[0], ZSTR_LEN(brk_info->filename)) == 0) {
xdebug_llist_remove(XG_DBG(context).line_breakpoints, le, NULL);
retval = SUCCESS;
break;
}
}
/* Cleaning up */
xdebug_arg_dtor(parts);
break;
}
case XDEBUG_BREAKPOINT_TYPE_CALL:
case XDEBUG_BREAKPOINT_TYPE_RETURN:
if (xdebug_hash_delete(XG_DBG(context).function_breakpoints, hkey, strlen(hkey))) {
retval = SUCCESS;
}
break;
case XDEBUG_BREAKPOINT_TYPE_EXCEPTION:
if (xdebug_hash_delete(XG_DBG(context).exception_breakpoints, hkey, strlen(hkey))) {
retval = SUCCESS;
}
break;
}
return retval;
}
#define BREAKPOINT_ACTION_GET 1
#define BREAKPOINT_ACTION_REMOVE 2
#define BREAKPOINT_ACTION_UPDATE 3
#define BREAKPOINT_CHANGE_STATE() \
XDEBUG_STR_SWITCH(CMD_OPTION_CHAR('s')) { \
XDEBUG_STR_CASE("enabled") \
brk_info->disabled = 0; \
XDEBUG_STR_CASE_END \
\
XDEBUG_STR_CASE("disabled") \
brk_info->disabled = 1; \
XDEBUG_STR_CASE_END \
\
XDEBUG_STR_CASE_DEFAULT \
RETURN_RESULT(XG_DBG(status), XG_DBG(reason), XDEBUG_ERROR_INVALID_ARGS); \
XDEBUG_STR_CASE_DEFAULT_END \
}
#define BREAKPOINT_CHANGE_OPERATOR() \
XDEBUG_STR_SWITCH(CMD_OPTION_CHAR('o')) { \
XDEBUG_STR_CASE(">=") \
brk_info->hit_condition = XDEBUG_HIT_GREATER_EQUAL; \
XDEBUG_STR_CASE_END \
\
XDEBUG_STR_CASE("==") \
brk_info->hit_condition = XDEBUG_HIT_EQUAL; \
XDEBUG_STR_CASE_END \
\
XDEBUG_STR_CASE("%") \
brk_info->hit_condition = XDEBUG_HIT_MOD; \
XDEBUG_STR_CASE_END \
\
XDEBUG_STR_CASE_DEFAULT \
RETURN_RESULT(XG_DBG(status), XG_DBG(reason), XDEBUG_ERROR_INVALID_ARGS); \
XDEBUG_STR_CASE_DEFAULT_END \
}
static void breakpoint_do_action(DBGP_FUNC_PARAMETERS, int action)
{
int type;
char *hkey;
xdebug_brk_info *brk_info;
xdebug_xml_node *breakpoint_node;
XDEBUG_STR_SWITCH_DECL;
if (!CMD_OPTION_SET('d')) {
RETURN_RESULT(XG_DBG(status), XG_DBG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
/* Lets check if it exists */
if (breakpoint_admin_fetch(context, CMD_OPTION_CHAR('d'), &type, (char**) &hkey) == FAILURE) {
RETURN_RESULT(XG_DBG(status), XG_DBG(reason), XDEBUG_ERROR_NO_SUCH_BREAKPOINT)
}
/* so it exists, now we're going to find it in the correct hash/list
* and return the info we have on it */
brk_info = breakpoint_brk_info_fetch(type, hkey);
if (action == BREAKPOINT_ACTION_UPDATE) {
if (CMD_OPTION_SET('s')) {
BREAKPOINT_CHANGE_STATE();
}
if (CMD_OPTION_SET('n')) {
brk_info->original_lineno = strtol(CMD_OPTION_CHAR('n'), NULL, 10);
brk_info->resolved_lineno = brk_info->original_lineno;
}
if (CMD_OPTION_SET('h')) {
brk_info->hit_value = strtol(CMD_OPTION_CHAR('h'), NULL, 10);
}
if (CMD_OPTION_SET('o')) {
BREAKPOINT_CHANGE_OPERATOR();
}
}
breakpoint_node = xdebug_xml_node_init("breakpoint");
breakpoint_brk_info_add(breakpoint_node, brk_info);
xdebug_xml_add_child(*retval, breakpoint_node);
if (action == BREAKPOINT_ACTION_REMOVE) {
/* Now we remove the crap */
breakpoint_remove(type, hkey);
breakpoint_admin_remove(context, CMD_OPTION_CHAR('d'));
}
}
DBGP_FUNC(breakpoint_get)
{
breakpoint_do_action(DBGP_FUNC_PASS_PARAMETERS, BREAKPOINT_ACTION_GET);
}
DBGP_FUNC(breakpoint_remove)
{
breakpoint_do_action(DBGP_FUNC_PASS_PARAMETERS, BREAKPOINT_ACTION_REMOVE);
}
DBGP_FUNC(breakpoint_update)
{
breakpoint_do_action(DBGP_FUNC_PASS_PARAMETERS, BREAKPOINT_ACTION_UPDATE);
}
static void breakpoint_exists_helper(void *retval, xdebug_hash_element *he, void *key_to_match)
{
int *line_found = (int *) retval;
xdebug_brk_admin *admin = (xdebug_brk_admin*) he->ptr;