forked from krakjoe/phpdbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpdbg_bp.c
1637 lines (1354 loc) · 52.4 KB
/
phpdbg_bp.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP 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: Felipe Pena <[email protected]> |
| Authors: Joe Watkins <[email protected]> |
| Authors: Bob Weinand <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "zend.h"
#include "zend_hash.h"
#include "phpdbg.h"
#include "phpdbg_bp.h"
#include "phpdbg_utils.h"
#include "phpdbg_opcode.h"
#include "zend_globals.h"
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
/* {{{ private api functions */
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_file(zend_op_array* TSRMLS_DC);
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_symbol(zend_function* TSRMLS_DC);
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_method(zend_op_array* TSRMLS_DC);
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opline(phpdbg_opline_ptr_t TSRMLS_DC);
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opcode(zend_uchar TSRMLS_DC);
static inline phpdbg_breakbase_t *phpdbg_find_conditional_breakpoint(zend_execute_data *execute_data TSRMLS_DC); /* }}} */
/*
* Note:
* A break point must always set the correct id and type
* A set breakpoint function must always map new points
*/
static inline void _phpdbg_break_mapping(int id, HashTable *table TSRMLS_DC)
{
zend_hash_index_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], (id), (void**) &table, sizeof(void*), NULL);
}
#define PHPDBG_BREAK_MAPPING(id, table) _phpdbg_break_mapping(id, table TSRMLS_CC)
#define PHPDBG_BREAK_UNMAPPING(id) \
zend_hash_index_del(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], (id))
#define PHPDBG_BREAK_INIT(b, t) do {\
b.id = PHPDBG_G(bp_count)++; \
b.type = t; \
b.disabled = 0;\
b.hits = 0; \
} while(0)
static void phpdbg_file_breaks_dtor(void *data) /* {{{ */
{
phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) data;
efree((char*)bp->filename);
} /* }}} */
static void phpdbg_class_breaks_dtor(void *data) /* {{{ */
{
phpdbg_breakmethod_t *bp = (phpdbg_breakmethod_t*) data;
efree((char*)bp->class_name);
efree((char*)bp->func_name);
} /* }}} */
static void phpdbg_opline_class_breaks_dtor(void *data) /* {{{ */
{
zend_hash_destroy((HashTable *)data);
} /* }}} */
static void phpdbg_opline_breaks_dtor(void *data) /* {{{ */
{
phpdbg_breakopline_t *bp = (phpdbg_breakopline_t *) data;
if (bp->class_name) {
efree((char*)bp->class_name);
}
if (bp->func_name) {
efree((char*)bp->func_name);
}
} /* }}} */
PHPDBG_API void phpdbg_reset_breakpoints(TSRMLS_D) /* {{{ */
{
if (zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP])) {
HashPosition position[2];
HashTable **table = NULL;
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], &position[0]);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], (void**)&table, &position[0]) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], &position[0])) {
phpdbg_breakbase_t *brake;
for (zend_hash_internal_pointer_reset_ex((*table), &position[1]);
zend_hash_get_current_data_ex((*table), (void**)&brake, &position[1]) == SUCCESS;
zend_hash_move_forward_ex((*table), &position[1])) {
brake->hits = 0;
}
}
}
} /* }}} */
PHPDBG_API void phpdbg_export_breakpoints(FILE *handle TSRMLS_DC) /* {{{ */
{
HashPosition position[2];
HashTable **table = NULL;
zend_ulong id = 0L;
if (zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP])) {
phpdbg_notice(
"Exporting %d breakpoints",
zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP]));
/* this only looks like magic, it isn't */
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], &position[0]);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], (void**)&table, &position[0]) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], &position[0])) {
phpdbg_breakbase_t *brake;
zend_hash_get_current_key_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], NULL, NULL, &id, 0, &position[0]);
for (zend_hash_internal_pointer_reset_ex((*table), &position[1]);
zend_hash_get_current_data_ex((*table), (void**)&brake, &position[1]) == SUCCESS;
zend_hash_move_forward_ex((*table), &position[1])) {
if (brake->id == id) {
switch (brake->type) {
case PHPDBG_BREAK_FILE: {
fprintf(handle,
"break %s:%lu\n",
((phpdbg_breakfile_t*)brake)->filename,
((phpdbg_breakfile_t*)brake)->line);
} break;
case PHPDBG_BREAK_SYM: {
fprintf(handle,
"break %s\n",
((phpdbg_breaksymbol_t*)brake)->symbol);
} break;
case PHPDBG_BREAK_METHOD: {
fprintf(handle,
"break %s::%s\n",
((phpdbg_breakmethod_t*)brake)->class_name,
((phpdbg_breakmethod_t*)brake)->func_name);
} break;
case PHPDBG_BREAK_METHOD_OPLINE: {
fprintf(handle,
"break %s::%s#%ld\n",
((phpdbg_breakopline_t*)brake)->class_name,
((phpdbg_breakopline_t*)brake)->func_name,
((phpdbg_breakopline_t*)brake)->opline_num);
} break;
case PHPDBG_BREAK_FUNCTION_OPLINE: {
fprintf(handle,
"break %s#%ld\n",
((phpdbg_breakopline_t*)brake)->func_name,
((phpdbg_breakopline_t*)brake)->opline_num);
} break;
case PHPDBG_BREAK_FILE_OPLINE: {
fprintf(handle,
"break %s:#%ld\n",
((phpdbg_breakopline_t*)brake)->class_name,
((phpdbg_breakopline_t*)brake)->opline_num);
} break;
case PHPDBG_BREAK_OPCODE: {
fprintf(handle,
"break %s\n",
((phpdbg_breakop_t*)brake)->name);
} break;
case PHPDBG_BREAK_COND: {
phpdbg_breakcond_t *conditional = (phpdbg_breakcond_t*) brake;
if (conditional->paramed) {
switch (conditional->param.type) {
case STR_PARAM:
fprintf(handle,
"break at %s if %s\n", conditional->param.str, conditional->code);
break;
case METHOD_PARAM:
fprintf(handle,
"break at %s::%s if %s\n",
conditional->param.method.class, conditional->param.method.name,
conditional->code);
break;
case FILE_PARAM:
fprintf(handle,
"break at %s:%lu if %s\n",
conditional->param.file.name, conditional->param.file.line,
conditional->code);
break;
default: { /* do nothing */ } break;
}
} else {
fprintf(
handle, "break if %s\n", conditional->code);
}
} break;
}
}
}
}
}
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_file(const char *path, long line_num TSRMLS_DC) /* {{{ */
{
php_stream_statbuf ssb;
char realpath[MAXPATHLEN];
if (php_stream_stat_path(path, &ssb) != FAILURE) {
if (ssb.sb.st_mode & (S_IFREG|S_IFLNK)) {
HashTable *broken;
phpdbg_breakfile_t new_break;
size_t path_len = 0L;
if (VCWD_REALPATH(path, realpath)) {
path = realpath;
}
path_len = strlen(path);
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
path, path_len, (void**)&broken) == FAILURE) {
HashTable breaks;
zend_hash_init(&breaks, 8, NULL, phpdbg_file_breaks_dtor, 0);
zend_hash_update(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
path, path_len, &breaks, sizeof(HashTable),
(void**)&broken);
}
if (!zend_hash_index_exists(broken, line_num)) {
PHPDBG_G(flags) |= PHPDBG_HAS_FILE_BP;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_FILE);
new_break.filename = estrndup(path, path_len);
new_break.line = line_num;
zend_hash_index_update(
broken, line_num, (void**)&new_break, sizeof(phpdbg_breakfile_t), NULL);
phpdbg_notice("Breakpoint #%d added at %s:%ld",
new_break.id, new_break.filename, new_break.line);
PHPDBG_BREAK_MAPPING(new_break.id, broken);
} else {
phpdbg_error("Breakpoint at %s:%ld exists", path, line_num);
}
} else {
phpdbg_error("Cannot set breakpoint in %s, it is not a regular file", path);
}
} else {
phpdbg_error("Cannot stat %s, it does not exist", path);
}
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_symbol(const char *name, size_t name_len TSRMLS_DC) /* {{{ */
{
if (!zend_hash_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], name, name_len)) {
phpdbg_breaksymbol_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_SYM_BP;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_SYM);
new_break.symbol = estrndup(name, name_len);
zend_hash_update(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], new_break.symbol,
name_len, &new_break, sizeof(phpdbg_breaksymbol_t), NULL);
phpdbg_notice("Breakpoint #%d added at %s",
new_break.id, new_break.symbol);
PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_SYM]);
} else {
phpdbg_notice("Breakpoint exists at %s", name);
}
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_method(const char *class_name, const char *func_name TSRMLS_DC) /* {{{ */
{
HashTable class_breaks, *class_table;
size_t class_len = strlen(class_name);
size_t func_len = strlen(func_name);
char *lcname = zend_str_tolower_dup(func_name, func_len);
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], class_name,
class_len, (void**)&class_table) != SUCCESS) {
zend_hash_init(&class_breaks, 8, NULL, phpdbg_class_breaks_dtor, 0);
zend_hash_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD],
class_name, class_len,
(void**)&class_breaks, sizeof(HashTable), (void**)&class_table);
}
if (!zend_hash_exists(class_table, lcname, func_len)) {
phpdbg_breakmethod_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_METHOD_BP;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_METHOD);
new_break.class_name = estrndup(class_name, class_len);
new_break.class_len = class_len;
new_break.func_name = estrndup(func_name, func_len);
new_break.func_len = func_len;
zend_hash_update(class_table, lcname, func_len,
&new_break, sizeof(phpdbg_breakmethod_t), NULL);
phpdbg_notice("Breakpoint #%d added at %s::%s",
new_break.id, class_name, func_name);
PHPDBG_BREAK_MAPPING(new_break.id, class_table);
} else {
phpdbg_notice("Breakpoint exists at %s::%s", class_name, func_name);
}
efree(lcname);
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_opline(zend_ulong opline TSRMLS_DC) /* {{{ */
{
if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline)) {
phpdbg_breakline_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_OPLINE);
new_break.name = NULL;
new_break.opline = opline;
new_break.base = NULL;
zend_hash_index_update(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline,
&new_break, sizeof(phpdbg_breakline_t), NULL);
phpdbg_notice("Breakpoint #%d added at %#lx",
new_break.id, new_break.opline);
PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
} else {
phpdbg_notice("Breakpoint exists at %#lx", opline);
}
} /* }}} */
PHPDBG_API int phpdbg_resolve_op_array_break(phpdbg_breakopline_t *brake, zend_op_array *op_array TSRMLS_DC) /* {{{ */
{
phpdbg_breakline_t opline_break;
if (op_array->last <= brake->opline_num) {
if (brake->class_name == NULL) {
phpdbg_error("There are only %d oplines in function %s (breaking at opline %ld impossible)", op_array->last, brake->func_name, brake->opline_num);
} else if (brake->func_name == NULL) {
phpdbg_error("There are only %d oplines in file %s (breaking at opline %ld impossible)", op_array->last, brake->class_name, brake->opline_num);
} else {
phpdbg_error("There are only %d oplines in method %s::%s (breaking at opline %ld impossible)", op_array->last, brake->class_name, brake->func_name, brake->opline_num);
}
return FAILURE;
}
opline_break.disabled = 0;
opline_break.hits = 0;
opline_break.id = brake->id;
opline_break.opline = brake->opline = (zend_ulong)(op_array->opcodes + brake->opline_num);
opline_break.name = NULL;
opline_break.base = brake;
if (op_array->scope) {
opline_break.type = PHPDBG_BREAK_METHOD_OPLINE;
} else if (op_array->function_name) {
opline_break.type = PHPDBG_BREAK_FUNCTION_OPLINE;
} else {
opline_break.type = PHPDBG_BREAK_FILE_OPLINE;
}
PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
zend_hash_index_update(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline_break.opline, &opline_break, sizeof(phpdbg_breakline_t), NULL);
return SUCCESS;
} /* }}} */
PHPDBG_API void phpdbg_resolve_op_array_breaks(zend_op_array *op_array TSRMLS_DC) /* {{{ */
{
HashTable *func_table = &PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE];
HashTable *oplines_table;
HashPosition position;
phpdbg_breakopline_t *brake;
if (op_array->scope != NULL &&
zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], op_array->scope->name, op_array->scope->name_length, (void **)&func_table) == FAILURE) {
return;
}
if (op_array->function_name == NULL) {
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE], op_array->filename, strlen(op_array->filename), (void **)&oplines_table) == FAILURE) {
return;
}
} else if (zend_hash_find(func_table, op_array->function_name?op_array->function_name:"", op_array->function_name?strlen(op_array->function_name):0, (void **)&oplines_table) == FAILURE) {
return;
}
for (zend_hash_internal_pointer_reset_ex(oplines_table, &position);
zend_hash_get_current_data_ex(oplines_table, (void**) &brake, &position) == SUCCESS;
zend_hash_move_forward_ex(oplines_table, &position)) {
if (phpdbg_resolve_op_array_break(brake, op_array TSRMLS_CC) == SUCCESS) {
phpdbg_breakline_t *opline_break;
zend_hash_internal_pointer_end(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
zend_hash_get_current_data(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (void **)&opline_break);
phpdbg_notice("Breakpoint #%d resolved at %s%s%s#%ld (opline %#lx)",
brake->id,
brake->class_name?brake->class_name:"",
brake->class_name&&brake->func_name?"::":"",
brake->func_name?brake->func_name:"",
brake->opline_num,
brake->opline);
}
}
} /* }}} */
PHPDBG_API int phpdbg_resolve_opline_break(phpdbg_breakopline_t *new_break TSRMLS_DC) /* {{{ */
{
HashTable *func_table = EG(function_table);
zend_function *func;
if (new_break->func_name == NULL) {
if (EG(current_execute_data) == NULL) {
if (PHPDBG_G(ops) != NULL && !memcmp(PHPDBG_G(ops)->filename, new_break->class_name, new_break->class_len)) {
if (phpdbg_resolve_op_array_break(new_break, PHPDBG_G(ops) TSRMLS_CC) == SUCCESS) {
return SUCCESS;
} else {
return 2;
}
}
return FAILURE;
} else {
zend_execute_data *execute_data = EG(current_execute_data);
do {
if (execute_data->op_array->function_name == NULL && execute_data->op_array->scope == NULL && !memcmp(execute_data->op_array->filename, new_break->class_name, new_break->class_len)) {
if (phpdbg_resolve_op_array_break(new_break, execute_data->op_array TSRMLS_CC) == SUCCESS) {
return SUCCESS;
} else {
return 2;
}
}
} while ((execute_data = execute_data->prev_execute_data) != NULL);
return FAILURE;
}
}
if (new_break->class_name != NULL) {
zend_class_entry **ce;
if (zend_hash_find(EG(class_table), zend_str_tolower_dup(new_break->class_name, new_break->class_len), new_break->class_len + 1, (void **)&ce) == FAILURE) {
return FAILURE;
}
func_table = &(*ce)->function_table;
}
if (zend_hash_find(func_table, zend_str_tolower_dup(new_break->func_name, new_break->func_len), new_break->func_len + 1, (void **)&func) == FAILURE) {
if (new_break->class_name != NULL && new_break->func_name != NULL) {
phpdbg_error("Method %s doesn't exist in class %s", new_break->func_name, new_break->class_name);
return 2;
}
return FAILURE;
}
if (func->type != ZEND_USER_FUNCTION) {
if (new_break->class_name == NULL) {
phpdbg_error("%s is not an user defined function, no oplines exist", new_break->func_name);
} else {
phpdbg_error("%s::%s is not an user defined method, no oplines exist", new_break->class_name, new_break->func_name);
}
return 2;
}
if (phpdbg_resolve_op_array_break(new_break, &func->op_array TSRMLS_CC) == FAILURE) {
return 2;
}
return SUCCESS;
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_method_opline(const char *class, const char *method, zend_ulong opline TSRMLS_DC) /* {{{ */
{
phpdbg_breakopline_t new_break;
HashTable class_breaks, *class_table;
HashTable method_breaks, *method_table;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_METHOD_OPLINE);
new_break.func_len = strlen(method);
new_break.func_name = estrndup(method, new_break.func_len);
new_break.class_len = strlen(class);
new_break.class_name = estrndup(class, new_break.class_len);
new_break.opline_num = opline;
new_break.opline = 0;
switch (phpdbg_resolve_opline_break(&new_break TSRMLS_CC)) {
case FAILURE:
phpdbg_notice("Pending breakpoint #%d at %s::%s#%ld", new_break.id, new_break.class_name, new_break.func_name, opline);
break;
case SUCCESS:
phpdbg_notice("Breakpoint #%d added at %s::%s#%ld", new_break.id, new_break.class_name, new_break.func_name, opline);
break;
case 2:
return;
}
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], new_break.class_name, new_break.class_len, (void **)&class_table) == FAILURE) {
zend_hash_init(&class_breaks, 8, NULL, phpdbg_opline_class_breaks_dtor, 0);
zend_hash_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE],
new_break.class_name,
new_break.class_len,
(void **)&class_breaks, sizeof(HashTable), (void **)&class_table);
}
if (zend_hash_find(class_table, new_break.func_name, new_break.func_len, (void **)&method_table) == FAILURE) {
zend_hash_init(&method_breaks, 8, NULL, phpdbg_opline_breaks_dtor, 0);
zend_hash_update(
class_table,
new_break.func_name,
new_break.func_len,
(void **)&method_breaks, sizeof(HashTable), (void **)&method_table);
}
if (zend_hash_index_exists(method_table, opline)) {
phpdbg_notice("Breakpoint already exists for %s::%s#%ld", new_break.class_name, new_break.func_name, opline);
efree((char*)new_break.func_name);
efree((char*)new_break.class_name);
PHPDBG_G(bp_count)--;
return;
}
PHPDBG_G(flags) |= PHPDBG_HAS_METHOD_OPLINE_BP;
PHPDBG_BREAK_MAPPING(new_break.id, method_table);
zend_hash_index_update(method_table, opline, &new_break, sizeof(phpdbg_breakopline_t), NULL);
}
PHPDBG_API void phpdbg_set_breakpoint_function_opline(const char *function, zend_ulong opline TSRMLS_DC) /* {{{ */
{
phpdbg_breakopline_t new_break;
HashTable func_breaks, *func_table;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_FUNCTION_OPLINE);
new_break.func_len = strlen(function);
new_break.func_name = estrndup(function, new_break.func_len);
new_break.class_len = 0;
new_break.class_name = NULL;
new_break.opline_num = opline;
new_break.opline = 0;
switch (phpdbg_resolve_opline_break(&new_break TSRMLS_CC)) {
case FAILURE:
phpdbg_notice("Pending breakpoint #%d at %s#%ld", new_break.id, new_break.func_name, opline);
break;
case SUCCESS:
phpdbg_notice("Breakpoint #%d added at %s#%ld", new_break.id, new_break.func_name, opline);
break;
case 2:
return;
}
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE], new_break.func_name, new_break.func_len, (void **)&func_table) == FAILURE) {
zend_hash_init(&func_breaks, 8, NULL, phpdbg_opline_breaks_dtor, 0);
zend_hash_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE],
new_break.func_name,
new_break.func_len,
(void **)&func_breaks, sizeof(HashTable), (void **)&func_table);
}
if (zend_hash_index_exists(func_table, opline)) {
phpdbg_notice("Breakpoint already exists for %s#%ld", new_break.func_name, opline);
efree((char*)new_break.func_name);
PHPDBG_G(bp_count)--;
return;
}
PHPDBG_BREAK_MAPPING(new_break.id, func_table);
PHPDBG_G(flags) |= PHPDBG_HAS_FUNCTION_OPLINE_BP;
zend_hash_index_update(func_table, opline, &new_break, sizeof(phpdbg_breakopline_t), NULL);
}
PHPDBG_API void phpdbg_set_breakpoint_file_opline(const char *file, zend_ulong opline TSRMLS_DC) /* {{{ */
{
phpdbg_breakopline_t new_break;
HashTable file_breaks, *file_table;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_FILE_OPLINE);
new_break.func_len = 0;
new_break.func_name = NULL;
new_break.class_len = strlen(file);
new_break.class_name = estrndup(file, new_break.class_len);
new_break.opline_num = opline;
new_break.opline = 0;
switch (phpdbg_resolve_opline_break(&new_break TSRMLS_CC)) {
case FAILURE:
phpdbg_notice("Pending breakpoint #%d at %s:%ld", new_break.id, new_break.class_name, opline);
break;
case SUCCESS:
phpdbg_notice("Breakpoint #%d added at %s:%ld", new_break.id, new_break.class_name, opline);
break;
case 2:
return;
}
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE], new_break.class_name, new_break.class_len, (void **)&file_table) == FAILURE) {
zend_hash_init(&file_breaks, 8, NULL, phpdbg_opline_breaks_dtor, 0);
zend_hash_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE],
new_break.class_name,
new_break.class_len,
(void **)&file_breaks, sizeof(HashTable), (void **)&file_table);
}
if (zend_hash_index_exists(file_table, opline)) {
phpdbg_notice("Breakpoint already exists for %s:%ld", new_break.class_name, opline);
efree((char*)new_break.class_name);
PHPDBG_G(bp_count)--;
return;
}
PHPDBG_BREAK_MAPPING(new_break.id, file_table);
PHPDBG_G(flags) |= PHPDBG_HAS_FILE_OPLINE_BP;
zend_hash_index_update(file_table, opline, &new_break, sizeof(phpdbg_breakopline_t), NULL);
}
PHPDBG_API void phpdbg_set_breakpoint_opcode(const char *name, size_t name_len TSRMLS_DC) /* {{{ */
{
phpdbg_breakop_t new_break;
zend_ulong hash = zend_hash_func(name, name_len);
if (zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE], hash)) {
phpdbg_notice(
"Breakpoint exists for %s", name);
return;
}
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_OPCODE);
new_break.hash = hash;
new_break.name = estrndup(name, name_len);
zend_hash_index_update(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE], hash,
&new_break, sizeof(phpdbg_breakop_t), NULL);
PHPDBG_G(flags) |= PHPDBG_HAS_OPCODE_BP;
phpdbg_notice("Breakpoint #%d added at %s", new_break.id, name);
PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE]);
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_opline_ex(phpdbg_opline_ptr_t opline TSRMLS_DC) /* {{{ */
{
if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline)) {
phpdbg_breakline_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_OPLINE);
new_break.opline = (zend_ulong) opline;
zend_hash_index_update(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE],
(zend_ulong) opline, &new_break, sizeof(phpdbg_breakline_t), NULL);
phpdbg_notice("Breakpoint #%d added at %#lx",
new_break.id, new_break.opline);
PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
}
} /* }}} */
static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, const phpdbg_param_t *param, const char *expr, size_t expr_len, zend_ulong hash TSRMLS_DC) /* {{{ */
{
phpdbg_breakcond_t new_break;
zend_uint cops = CG(compiler_options);
zval pv;
PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_COND);
new_break.hash = hash;
if (param) {
new_break.paramed = 1;
phpdbg_copy_param(
param, &new_break.param TSRMLS_CC);
} else {
new_break.paramed = 0;
}
cops = CG(compiler_options);
CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
new_break.code = estrndup(expr, expr_len);
new_break.code_len = expr_len;
Z_STRLEN(pv) = expr_len + sizeof("return ;") - 1;
Z_STRVAL(pv) = emalloc(Z_STRLEN(pv) + 1);
memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, expr, expr_len);
Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
Z_TYPE(pv) = IS_STRING;
new_break.ops = zend_compile_string(
&pv, "Conditional Breakpoint Code" TSRMLS_CC);
zval_dtor(&pv);
if (new_break.ops) {
zend_hash_index_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash, &new_break,
sizeof(phpdbg_breakcond_t), (void**)&brake);
phpdbg_notice("Conditional breakpoint #%d added %s/%p",
brake->id, brake->code, brake->ops);
PHPDBG_G(flags) |= PHPDBG_HAS_COND_BP;
PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_COND]);
} else {
phpdbg_error(
"Failed to compile code for expression %s", expr);
efree((char*)new_break.code);
PHPDBG_G(bp_count)--;
}
CG(compiler_options) = cops;
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_expression(const char *expr, size_t expr_len TSRMLS_DC) /* {{{ */
{
zend_ulong expr_hash = zend_inline_hash_func(expr, expr_len);
phpdbg_breakcond_t new_break;
if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], expr_hash)) {
phpdbg_create_conditional_break(
&new_break, NULL, expr, expr_len, expr_hash TSRMLS_CC);
} else {
phpdbg_notice("Conditional break %s exists", expr);
}
} /* }}} */
PHPDBG_API void phpdbg_set_breakpoint_at(const phpdbg_param_t *param TSRMLS_DC) /* {{{ */
{
phpdbg_breakcond_t new_break;
phpdbg_param_t *condition;
zend_ulong hash = 0L;
if (param->next) {
condition = param->next;
hash = zend_inline_hash_func(condition->str, condition->len);
if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash)) {
phpdbg_create_conditional_break(
&new_break, param,
condition->str, condition->len, hash TSRMLS_CC);
} else {
phpdbg_notice(
"Conditional break %s exists at the specified location", condition->str);
}
}
} /* }}} */
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_file(zend_op_array *op_array TSRMLS_DC) /* {{{ */
{
HashTable *breaks;
phpdbg_breakbase_t *brake;
size_t name_len = strlen(op_array->filename);
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], op_array->filename,
name_len, (void**)&breaks) == FAILURE) {
return NULL;
}
if (zend_hash_index_find(breaks, (*EG(opline_ptr))->lineno, (void**)&brake) == SUCCESS) {
return brake;
}
return NULL;
} /* }}} */
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_symbol(zend_function *fbc TSRMLS_DC) /* {{{ */
{
const char *fname;
zend_op_array *ops;
phpdbg_breakbase_t *brake;
if (fbc->type != ZEND_USER_FUNCTION) {
return NULL;
}
ops = (zend_op_array*)fbc;
if (ops->scope) {
/* find method breaks here */
return phpdbg_find_breakpoint_method(ops TSRMLS_CC);
}
fname = ops->function_name;
if (!fname) {
fname = "main";
}
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], fname, strlen(fname), (void**)&brake) == SUCCESS) {
return brake;
}
return NULL;
} /* }}} */
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_method(zend_op_array *ops TSRMLS_DC) /* {{{ */
{
HashTable *class_table;
phpdbg_breakbase_t *brake;
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], ops->scope->name,
ops->scope->name_length, (void**)&class_table) == SUCCESS) {
char *lcname = zend_str_tolower_dup(ops->function_name, strlen(ops->function_name));
size_t lcname_len = strlen(lcname);
if (zend_hash_find(
class_table,
lcname,
lcname_len, (void**)&brake) == SUCCESS) {
efree(lcname);
return brake;
}
efree(lcname);
}
return NULL;
} /* }}} */
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opline(phpdbg_opline_ptr_t opline TSRMLS_DC) /* {{{ */
{
phpdbg_breakline_t *brake;
if (zend_hash_index_find(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE],
(zend_ulong) opline, (void**)&brake) == SUCCESS) {
return (brake->base?(phpdbg_breakbase_t *)brake->base:(phpdbg_breakbase_t *)brake);
}
return NULL;
} /* }}} */
static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opcode(zend_uchar opcode TSRMLS_DC) /* {{{ */
{
phpdbg_breakbase_t *brake;
const char *opname = phpdbg_decode_opcode(opcode);
if (memcmp(opname, PHPDBG_STRL("UNKNOWN")) == 0) {
return NULL;
}
if (zend_hash_index_find(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE],
zend_hash_func(opname, strlen(opname)), (void**)&brake) == SUCCESS) {
return brake;
}
return NULL;
} /* }}} */
static inline zend_bool phpdbg_find_breakpoint_param(phpdbg_param_t *param, zend_execute_data *execute_data TSRMLS_DC) /* {{{ */
{
zend_function *function = (zend_function*) execute_data->function_state.function;
switch (param->type) {
case NUMERIC_FUNCTION_PARAM:
case STR_PARAM: {
/* function breakpoint */
if (function->type != ZEND_USER_FUNCTION) {
return 0;
}
{
const char *str = NULL;
size_t len = 0L;
zend_op_array *ops = (zend_op_array*)function;
str = ops->function_name ? ops->function_name : "main";
len = strlen(str);
if (len == param->len && memcmp(param->str, str, len) == SUCCESS) {
return param->type == STR_PARAM || execute_data->opline - ops->opcodes == param->num;
}
}
} break;
case FILE_PARAM: {
if (param->file.line == zend_get_executed_lineno(TSRMLS_C)) {
const char *str = zend_get_executed_filename(TSRMLS_C);
size_t lengths[2] = {strlen(param->file.name), strlen(str)};
if (lengths[0] == lengths[1]) {
return (memcmp(
param->file.name, str, lengths[0]) == SUCCESS);
}
}
} break;
case NUMERIC_METHOD_PARAM:
case METHOD_PARAM: {
if (function->type != ZEND_USER_FUNCTION) {
return 0;
}
{
zend_op_array *ops = (zend_op_array*) function;
if (ops->scope) {
size_t lengths[2] = {strlen(param->method.class), ops->scope->name_length};
if (lengths[0] == lengths[1] && memcmp(param->method.class, ops->scope->name, lengths[0]) == SUCCESS) {
lengths[0] = strlen(param->method.name);
lengths[1] = strlen(ops->function_name);
if (lengths[0] == lengths[1] && memcmp(param->method.name, ops->function_name, lengths[0]) == SUCCESS) {
return param->type == METHOD_PARAM || (execute_data->opline - ops->opcodes) == param->num;
}
}
}
}
} break;
case ADDR_PARAM: {
return ((zend_ulong)(phpdbg_opline_ptr_t)execute_data->opline == param->addr);
} break;
default: {
/* do nothing */
} break;
}
return 0;
} /* }}} */
static inline phpdbg_breakbase_t *phpdbg_find_conditional_breakpoint(zend_execute_data *execute_data TSRMLS_DC) /* {{{ */
{
phpdbg_breakcond_t *bp;
HashPosition position;
int breakpoint = FAILURE;
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], &position);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], (void*)&bp, &position) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], &position)) {
zval *retval = NULL;
int orig_interactive = CG(interactive);
zval **orig_retval = EG(return_value_ptr_ptr);
zend_op_array *orig_ops = EG(active_op_array);
zend_op **orig_opline = EG(opline_ptr);
if (((phpdbg_breakbase_t*)bp)->disabled) {
continue;
}
if (bp->paramed) {
if (!phpdbg_find_breakpoint_param(&bp->param, execute_data TSRMLS_CC)) {
continue;
}
}
ALLOC_INIT_ZVAL(retval);
EG(return_value_ptr_ptr) = &retval;
EG(active_op_array) = bp->ops;
EG(no_extensions) = 1;
if (!EG(active_symbol_table)) {
zend_rebuild_symbol_table(TSRMLS_C);
}
CG(interactive) = 0;
zend_try {