forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug_handler_dbgp.c
2531 lines (2135 loc) · 76.4 KB
/
xdebug_handler_dbgp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2018 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <[email protected]> |
| Shane Caraveo <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <sys/types.h>
#ifndef PHP_WIN32
#include <unistd.h>
#endif
#include "php.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 "xdebug_private.h"
#include "xdebug_code_coverage.h"
#include "xdebug_com.h"
#include "xdebug_compat.h"
#include "xdebug_handler_dbgp.h"
#include "xdebug_hash.h"
#include "xdebug_llist.h"
#include "xdebug_mm.h"
#include "xdebug_stack.h"
#include "xdebug_var.h"
#include "xdebug_xml.h"
#include "xdebug_compat.h"
#ifdef PHP_WIN32
#include "win32/time.h"
#include <process.h>
#endif
#include <fcntl.h>
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
static char *create_eval_key_id(int id);
/*****************************************************************************
** 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[6] =
{"", "starting", "stopping", "stopped", "running", "break"};
#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"};
typedef struct {
int code;
const char *message;
} xdebug_error_entry;
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" },
{ 800, "profiler not started" },
{ 900, "encoding not supported" },
{ 998, "an internal exception in the debugger" },
{ 999, "unknown error" },
{ -1, 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}
};
/*****************************************************************************
** 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_profiler_name_get);
DBGP_FUNC(xcmd_get_executable_lines);
/*****************************************************************************
** Dispatcher tables for supported debug commands
*/
static xdebug_dbgp_cmd dbgp_commands[] = {
/* DBGP_FUNC_ENTRY(break) */
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_NONE)
/* Non standard functions */
DBGP_FUNC_ENTRY(xcmd_profiler_name_get, XDEBUG_DBGP_POST_MORTEM)
DBGP_FUNC_ENTRY(xcmd_get_executable_lines, XDEBUG_DBGP_NONE)
{ NULL, NULL, 0, 0 }
};
/*****************************************************************************
** 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 TSRMLS_DC)
{
xdebug_str xml_message = XDEBUG_STR_INITIALIZER;
xdebug_str *ret = xdebug_str_new();
xdebug_xml_return_node(message, &xml_message);
if (XG(remote_log_file)) {
long pid = getpid();
fprintf(XG(remote_log_file), "[%ld] -> %s\n[%ld]\n", pid, xml_message.d, pid);
fflush(XG(remote_log_file));
}
xdebug_str_add(ret, xdebug_sprintf("%d", xml_message.l + sizeof("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n") - 1), 1);
xdebug_str_addl(ret, "\0", 1, 0);
xdebug_str_add(ret, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n", 0);
xdebug_str_add(ret, xml_message.d, 0);
xdebug_str_addl(ret, "\0", 1, 0);
xdebug_str_destroy(&xml_message);
return ret;
}
static void send_message(xdebug_con *context, xdebug_xml_node *message TSRMLS_DC)
{
xdebug_str *tmp;
tmp = make_message(context, message TSRMLS_CC);
if ((size_t) SSENDL(context->socket, tmp->d, tmp->l) != tmp->l) {
char *sock_error = php_socket_strerror(php_socket_errno(), NULL, 0);
fprintf(stderr, "There was a problem sending %zd bytes on socket %d: %s", tmp->l, context->socket, sock_error);
efree(sock_error);
}
xdebug_str_free(tmp);
}
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 TSRMLS_CC);
if (Z_TYPE(retval) != IS_UNDEF) {
if (strcmp(name->d, "this") == 0 && Z_TYPE(retval) == IS_NULL) {
return NULL;
}
tmp_node = xdebug_get_zval_value_xml_node(name, &retval, options TSRMLS_CC);
zval_ptr_dtor_nogc(&retval);
return tmp_node;
}
return NULL;
}
static int get_symbol_contents(xdebug_str *name, xdebug_xml_node *node, xdebug_var_export_options *options)
{
zval retval;
xdebug_get_php_symbol(&retval, name TSRMLS_CC);
if (Z_TYPE(retval) != IS_UNDEF) {
// TODO WTF???
zval *retval_ptr = &retval;
xdebug_var_export_xml_node(&retval_ptr, name, node, options, 1 TSRMLS_CC);
zval_ptr_dtor_nogc(&retval);
return 1;
}
return 0;
}
static xdebug_str* return_file_source(char *filename, int begin, int end TSRMLS_DC)
{
php_stream *stream;
int i = begin;
char *line = NULL;
xdebug_str *source = xdebug_str_new();
if (i < 0) {
begin = 0;
i = 0;
}
filename = xdebug_path_from_url(filename TSRMLS_CC);
stream = php_stream_open_wrapper(filename, "rb",
USE_PATH | REPORT_ERRORS,
NULL);
xdfree(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 TSRMLS_DC)
{
char *key;
xdebug_str *joined;
xdebug_eval_info *ei;
xdebug_arg *parts = (xdebug_arg*) xdmalloc(sizeof(xdebug_arg));
if (begin < 0) {
begin = 0;
}
key = create_eval_key_id(atoi(id));
if (xdebug_hash_find(XG(context).eval_id_lookup, key, strlen(key), (void *) &ei)) {
xdebug_arg_init(parts);
xdebug_explode("\n", ei->contents, parts, end + 2);
joined = xdebug_join("\n", parts, begin, end);
xdebug_arg_dtor(parts);
return joined;
}
return NULL;
}
static xdebug_str* return_source(char *filename, int begin, int end TSRMLS_DC)
{
if (strncmp(filename, "dbgp://", 7) == 0) {
return return_eval_source(filename + 7, begin, end TSRMLS_CC);
} else {
return return_file_source(filename, begin, end TSRMLS_CC);
}
}
static int check_evaled_code(function_stack_entry *fse, char **filename, int *lineno, int use_fse TSRMLS_DC)
{
char *end_marker;
xdebug_eval_info *ei;
char *filename_to_use;
filename_to_use = use_fse ? fse->filename : *filename;
end_marker = filename_to_use + strlen(filename_to_use) - strlen("eval()'d code");
if (end_marker >= filename_to_use && strcmp("eval()'d code", end_marker) == 0) {
if (xdebug_hash_find(XG(context).eval_id_lookup, filename_to_use, strlen(filename_to_use), (void *) &ei)) {
*filename = xdebug_sprintf("dbgp://%lu", ei->id);
}
return 1;
}
return 0;
}
static xdebug_xml_node* return_stackframe(int nr TSRMLS_DC)
{
function_stack_entry *fse, *fse_prev;
char *tmp_fname;
char *tmp_filename;
int tmp_lineno;
xdebug_xml_node *tmp;
fse = xdebug_get_stack_frame(nr TSRMLS_CC);
fse_prev = xdebug_get_stack_frame(nr - 1 TSRMLS_CC);
tmp_fname = xdebug_show_fname(fse->function, 0, 0 TSRMLS_CC);
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 (check_evaled_code(fse_prev, &tmp_filename, &tmp_lineno, 1 TSRMLS_CC)) {
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("eval"), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "filename", tmp_filename, 0, 0);
} else {
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("file"), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "filename", xdebug_path_to_url(fse_prev->filename TSRMLS_CC), 0, 1);
}
xdebug_xml_add_attribute_ex(tmp, "lineno", xdebug_sprintf("%lu", fse_prev->lineno TSRMLS_CC), 0, 1);
} else {
tmp_filename = (char *) zend_get_executed_filename(TSRMLS_C);
tmp_lineno = zend_get_executed_lineno(TSRMLS_C);
if (check_evaled_code(fse, &tmp_filename, &tmp_lineno, 0 TSRMLS_CC)) {
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("eval"), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "filename", tmp_filename, 0, 0);
} else {
xdebug_xml_add_attribute_ex(tmp, "type", xdstrdup("file"), 0, 1);
xdebug_xml_add_attribute_ex(tmp, "filename", xdebug_path_to_url(tmp_filename TSRMLS_CC), 0, 1);
}
xdebug_xml_add_attribute_ex(tmp, "lineno", xdebug_sprintf("%lu", tmp_lineno), 0, 1);
}
xdfree(tmp_fname);
return tmp;
}
/*****************************************************************************
** Client command handlers - Breakpoints
*/
/* Helper functions */
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;
TSRMLS_FETCH();
XG(breakpoint_count)++;
admin->id = ((getpid() & 0x1ffff) * 10000) + XG(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)) {
*type = admin->type;
*key = admin->key;
return SUCCESS;
} else {
return FAILURE;
}
}
static int breakpoint_admin_remove(xdebug_con *context, char *hkey)
{
if (xdebug_hash_delete(context->breakpoint_list, hkey, strlen(hkey))) {
return SUCCESS;
} else {
return FAILURE;
}
}
static void breakpoint_brk_info_add(xdebug_xml_node *xml, xdebug_brk_info *brk_info)
{
TSRMLS_FETCH();
if (brk_info->type) {
xdebug_xml_add_attribute_ex(xml, "type", xdstrdup(brk_info->type), 0, 1);
}
if (brk_info->file) {
xdebug_xml_add_attribute_ex(xml, "filename", xdebug_path_to_url(brk_info->file TSRMLS_CC), 0, 1);
}
if (brk_info->lineno) {
xdebug_xml_add_attribute_ex(xml, "lineno", xdebug_sprintf("%lu", brk_info->lineno), 0, 1);
}
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->temporary) {
xdebug_xml_add_attribute(xml, "state", "temporary");
} else if (brk_info->disabled) {
xdebug_xml_add_attribute(xml, "state", "disabled");
} 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);
}
static xdebug_brk_info* breakpoint_brk_info_fetch(int type, char *hkey)
{
xdebug_llist_element *le;
xdebug_brk_info *brk_info = NULL;
xdebug_arg *parts = (xdebug_arg*) xdmalloc(sizeof(xdebug_arg));
TSRMLS_FETCH();
switch (type) {
case BREAKPOINT_TYPE_LINE:
/* First we split the key into filename and linenumber */
xdebug_arg_init(parts);
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(context).line_breakpoints); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
brk_info = XDEBUG_LLIST_VALP(le);
if (atoi(parts->args[1]) == brk_info->lineno && memcmp(brk_info->file, parts->args[0], brk_info->file_len) == 0) {
xdebug_arg_dtor(parts);
return brk_info;
}
}
/* Cleaning up */
xdebug_arg_dtor(parts);
break;
case BREAKPOINT_TYPE_FUNCTION:
if (xdebug_hash_find(XG(context).function_breakpoints, hkey, strlen(hkey), (void *) &brk_info)) {
return brk_info;
}
break;
case BREAKPOINT_TYPE_EXCEPTION:
if (xdebug_hash_find(XG(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;
xdebug_arg *parts = (xdebug_arg*) xdmalloc(sizeof(xdebug_arg));
int retval = FAILURE;
TSRMLS_FETCH();
switch (type) {
case BREAKPOINT_TYPE_LINE:
/* First we split the key into filename and linenumber */
xdebug_arg_init(parts);
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(context).line_breakpoints); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
brk_info = XDEBUG_LLIST_VALP(le);
if (atoi(parts->args[1]) == brk_info->lineno && memcmp(brk_info->file, parts->args[0], brk_info->file_len) == 0) {
xdebug_llist_remove(XG(context).line_breakpoints, le, NULL);
retval = SUCCESS;
break;
}
}
/* Cleaning up */
xdebug_arg_dtor(parts);
break;
case BREAKPOINT_TYPE_FUNCTION:
if (xdebug_hash_delete(XG(context).function_breakpoints, hkey, strlen(hkey))) {
retval = SUCCESS;
}
break;
case BREAKPOINT_TYPE_EXCEPTION:
if (xdebug_hash_delete(XG(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(status), XG(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(status), XG(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(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
/* Lets check if it exists */
if (breakpoint_admin_fetch(context, CMD_OPTION_CHAR('d'), &type, (char**) &hkey) == SUCCESS) {
/* 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->lineno = strtol(CMD_OPTION_CHAR('n'), NULL, 10);
}
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_attribute_ex(breakpoint_node, "id", xdstrdup(CMD_OPTION_CHAR('d')), 0, 1);
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'));
}
} else {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_NO_SUCH_BREAKPOINT)
}
}
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_list_helper(void *xml, xdebug_hash_element *he)
{
xdebug_xml_node *xml_node = (xdebug_xml_node*) xml;
xdebug_xml_node *child;
xdebug_brk_admin *admin = (xdebug_brk_admin*) he->ptr;
xdebug_brk_info *brk_info;
child = xdebug_xml_node_init("breakpoint");
brk_info = breakpoint_brk_info_fetch(admin->type, admin->key);
breakpoint_brk_info_add(child, brk_info);
xdebug_xml_add_attribute_ex(child, "id", xdebug_sprintf("%lu", admin->id), 0, 1);
xdebug_xml_add_child(xml_node, child);
}
DBGP_FUNC(breakpoint_list)
{
xdebug_hash_apply(context->breakpoint_list, (void *) *retval, breakpoint_list_helper);
}
DBGP_FUNC(breakpoint_set)
{
xdebug_brk_info *brk_info;
char *tmp_name;
int brk_id = 0;
size_t new_length = 0;
function_stack_entry *fse;
XDEBUG_STR_SWITCH_DECL;
brk_info = xdmalloc(sizeof(xdebug_brk_info));
brk_info->type = NULL;
brk_info->file = NULL;
brk_info->file_len = 0;
brk_info->lineno = 0;
brk_info->classname = NULL;
brk_info->functionname = NULL;
brk_info->function_break_type = 0;
brk_info->exceptionname = NULL;
brk_info->condition = NULL;
brk_info->disabled = 0;
brk_info->temporary = 0;
brk_info->hit_count = 0;
brk_info->hit_value = 0;
brk_info->hit_condition = XDEBUG_HIT_DISABLED;
if (!CMD_OPTION_SET('t')) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
} else {
if (
(strcmp(CMD_OPTION_CHAR('t'), "line") != 0) &&
(strcmp(CMD_OPTION_CHAR('t'), "conditional") != 0) &&
(strcmp(CMD_OPTION_CHAR('t'), "call") != 0) &&
(strcmp(CMD_OPTION_CHAR('t'), "return") != 0) &&
(strcmp(CMD_OPTION_CHAR('t'), "exception") != 0) &&
(strcmp(CMD_OPTION_CHAR('t'), "watch") != 0)
) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
brk_info->type = xdstrdup(CMD_OPTION_CHAR('t'));
}
if (CMD_OPTION_SET('s')) {
BREAKPOINT_CHANGE_STATE();
xdebug_xml_add_attribute_ex(*retval, "state", xdstrdup(CMD_OPTION_CHAR('s')), 0, 1);
}
if (CMD_OPTION_SET('o') && CMD_OPTION_SET('h')) {
BREAKPOINT_CHANGE_OPERATOR();
brk_info->hit_value = strtol(CMD_OPTION_CHAR('h'), NULL, 10);
}
if (CMD_OPTION_SET('r')) {
brk_info->temporary = strtol(CMD_OPTION_CHAR('r'), NULL, 10);
}
if ((strcmp(CMD_OPTION_CHAR('t'), "line") == 0) || (strcmp(CMD_OPTION_CHAR('t'), "conditional") == 0)) {
if (!CMD_OPTION_SET('n')) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
brk_info->lineno = strtol(CMD_OPTION_CHAR('n'), NULL, 10);
/* If no filename is given, we use the current one */
if (!CMD_OPTION_SET('f')) {
fse = xdebug_get_stack_tail(TSRMLS_C);
if (!fse) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_STACK_DEPTH_INVALID);
} else {
brk_info->file = xdebug_path_from_url(fse->filename TSRMLS_CC);
brk_info->file_len = strlen(brk_info->file);
}
} else {
char realpath_file[MAXPATHLEN];
brk_info->file = xdebug_path_from_url(CMD_OPTION_CHAR('f') TSRMLS_CC);
/* Now we do some real path checks to resolve symlinks. */
if (VCWD_REALPATH(brk_info->file, realpath_file)) {
xdfree(brk_info->file);
brk_info->file = xdstrdup(realpath_file);
}
brk_info->file_len = strlen(brk_info->file);
}
/* Perhaps we have a break condition */
if (CMD_OPTION_SET('-')) {
brk_info->condition = (char*) xdebug_base64_decode((unsigned char*) CMD_OPTION_CHAR('-'), CMD_OPTION_LEN('-'), &new_length);
}
tmp_name = xdebug_sprintf("%s$%lu", brk_info->file, brk_info->lineno);
brk_id = breakpoint_admin_add(context, BREAKPOINT_TYPE_LINE, tmp_name);
xdfree(tmp_name);
xdebug_llist_insert_next(context->line_breakpoints, XDEBUG_LLIST_TAIL(context->line_breakpoints), (void*) brk_info);
} else
if ((strcmp(CMD_OPTION_CHAR('t'), "call") == 0) || (strcmp(CMD_OPTION_CHAR('t'), "return") == 0)) {
if (strcmp(CMD_OPTION_CHAR('t'), "call") == 0) {
brk_info->function_break_type = XDEBUG_BRK_FUNC_CALL;
} else {
brk_info->function_break_type = XDEBUG_BRK_FUNC_RETURN;
}
if (!CMD_OPTION_SET('m')) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
brk_info->functionname = xdstrdup(CMD_OPTION_CHAR('m'));
if (CMD_OPTION_SET('a')) {
int res;
brk_info->classname = xdstrdup(CMD_OPTION_CHAR('a'));
tmp_name = xdebug_sprintf("%s::%s", CMD_OPTION_CHAR('a'), CMD_OPTION_CHAR('m'));
res = xdebug_hash_add(context->function_breakpoints, tmp_name, strlen(tmp_name), (void*) brk_info);
brk_id = breakpoint_admin_add(context, BREAKPOINT_TYPE_FUNCTION, tmp_name);
xdfree(tmp_name);
if (!res) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
} else {
if (!xdebug_hash_add(context->function_breakpoints, CMD_OPTION_CHAR('m'), CMD_OPTION_LEN('m'), (void*) brk_info)) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_BREAKPOINT_NOT_SET);
} else {
brk_id = breakpoint_admin_add(context, BREAKPOINT_TYPE_FUNCTION, CMD_OPTION_CHAR('m'));
}
}
} else
if (strcmp(CMD_OPTION_CHAR('t'), "exception") == 0) {
if (!CMD_OPTION_SET('x')) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
brk_info->exceptionname = xdstrdup(CMD_OPTION_CHAR('x'));
if (!xdebug_hash_add(context->exception_breakpoints, CMD_OPTION_CHAR('x'), CMD_OPTION_LEN('x'), (void*) brk_info)) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_BREAKPOINT_NOT_SET);
} else {
brk_id = breakpoint_admin_add(context, BREAKPOINT_TYPE_EXCEPTION, CMD_OPTION_CHAR('x'));
}
} else
if (strcmp(CMD_OPTION_CHAR('t'), "watch") == 0) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_BREAKPOINT_TYPE_NOT_SUPPORTED);
}
xdebug_xml_add_attribute_ex(*retval, "id", xdebug_sprintf("%lu", brk_id), 0, 1);
}
static int xdebug_do_eval(char *eval_string, zval *ret_zval TSRMLS_DC)
{
int old_error_reporting;
int old_track_errors;
int res = FAILURE;
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 */
old_error_reporting = EG(error_reporting);
old_track_errors = PG(track_errors);
EG(error_reporting) = 0;
PG(track_errors) = 0;
/* Do evaluation */
XG(breakpoints_allowed) = 0;
/* Reset exception in case we're triggered while being in xdebug_throw_exception_hook */
EG(exception) = NULL;
zend_first_try {
res = zend_eval_string(eval_string, ret_zval, (char*) "xdebug://debug-eval" TSRMLS_CC);
} zend_end_try();
/* FIXME: Bubble up exception message to DBGp return packet */
if (EG(exception)) {
res = FAILURE;
}
/* Clean up */
EG(error_reporting) = old_error_reporting;
PG(track_errors) = old_track_errors;
XG(breakpoints_allowed) = 1;
EG(current_execute_data) = original_execute_data;
EG(no_extensions) = original_no_extensions;
EG(exception) = original_exception;
EG(bailout) = original_bailout;
return res;
}
DBGP_FUNC(eval)
{
char *eval_string;
xdebug_xml_node *ret_xml;
zval ret_zval;
size_t new_length = 0;
int res;
xdebug_var_export_options *options;
if (!CMD_OPTION_SET('-')) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_INVALID_ARGS);
}
options = (xdebug_var_export_options*) context->options;
if (CMD_OPTION_SET('p')) {
options->runtime[0].page = strtol(CMD_OPTION_CHAR('p'), NULL, 10);
} else {
options->runtime[0].page = 0;
}
/* base64 decode eval string */
eval_string = (char*) xdebug_base64_decode((unsigned char*) CMD_OPTION_CHAR('-'), CMD_OPTION_LEN('-'), &new_length);
res = xdebug_do_eval(eval_string, &ret_zval TSRMLS_CC);
xdfree(eval_string);
/* Handle result */
if (res == FAILURE) {
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_EVALUATING_CODE);
} else {
ret_xml = xdebug_get_zval_value_xml_node(NULL, &ret_zval, options TSRMLS_CC);
xdebug_xml_add_child(*retval, ret_xml);
zval_ptr_dtor(&ret_zval);
}
}
/* these functions interupt PHP's output functions, so we can
redirect to our remote debugger! */
static int xdebug_send_stream(const char *name, const char *str, uint str_length TSRMLS_DC)
{
/* create an xml document to send as the stream */
xdebug_xml_node *message;
message = xdebug_xml_node_init("stream");
xdebug_xml_add_attribute(message, "xmlns", "urn:debugger_protocol_v1");
xdebug_xml_add_attribute(message, "xmlns:xdebug", "https://xdebug.org/dbgp/xdebug");
xdebug_xml_add_attribute_ex(message, "type", (char *)name, 0, 0);
xdebug_xml_add_text_encodel(message, xdstrndup(str, str_length), str_length);
send_message(&XG(context), message TSRMLS_CC);