forked from mabu233/sdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxdebug_filter.c
More file actions
255 lines (214 loc) · 8.26 KB
/
xdebug_filter.c
File metadata and controls
255 lines (214 loc) · 8.26 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
/*
+----------------------------------------------------------------------+
| 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 |
| derick@xdebug.org so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <derick@xdebug.org> |
+----------------------------------------------------------------------+
*/
#include "php_xdebug.h"
#include "xdebug_private.h"
#include "xdebug_filter.h"
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
int xdebug_filter_is_valid(void)
{
return 0;
}
int xdebug_is_stack_frame_filtered(int filter_type, function_stack_entry *fse)
{
switch (filter_type) {
case XDEBUG_FILTER_TRACING:
return fse->filtered_tracing;
case XDEBUG_FILTER_CODE_COVERAGE:
return fse->filtered_code_coverage;
}
return 0;
}
int xdebug_is_top_stack_frame_filtered(int filter_type)
{
function_stack_entry *fse;
fse = XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack)));
return xdebug_is_stack_frame_filtered(filter_type, fse);
}
void xdebug_filter_register_constants(INIT_FUNC_ARGS)
{
REGISTER_LONG_CONSTANT("XDEBUG_FILTER_TRACING", XDEBUG_FILTER_TRACING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XDEBUG_FILTER_CODE_COVERAGE", XDEBUG_FILTER_CODE_COVERAGE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XDEBUG_FILTER_NONE", XDEBUG_FILTER_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XDEBUG_PATH_WHITELIST", XDEBUG_PATH_WHITELIST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XDEBUG_PATH_BLACKLIST", XDEBUG_PATH_BLACKLIST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XDEBUG_NAMESPACE_WHITELIST", XDEBUG_NAMESPACE_WHITELIST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XDEBUG_NAMESPACE_BLACKLIST", XDEBUG_NAMESPACE_BLACKLIST, CONST_CS | CONST_PERSISTENT);
}
int xdebug_filter_match_path_whitelist(function_stack_entry *fse, long *filtered_flag, char *filter)
{
if (strncasecmp(filter, fse->filename, strlen(filter)) == 0) {
*filtered_flag = 0;
return 1;
}
return 0;
}
int xdebug_filter_match_path_blacklist(function_stack_entry *fse, long *filtered_flag, char *filter)
{
if (strncasecmp(filter, fse->filename, strlen(filter)) == 0) {
*filtered_flag = 1;
return 1;
}
return 0;
}
int xdebug_filter_match_namespace_whitelist(function_stack_entry *fse, long *filtered_flag, char *filter)
{
if (!fse->function.class && strlen(filter) == 0) {
*filtered_flag = 0;
return 1;
}
if (fse->function.class && strlen(filter) > 0 && strncasecmp(filter, fse->function.class, strlen(filter)) == 0) {
*filtered_flag = 0;
return 1;
}
return 0;
}
int xdebug_filter_match_namespace_blacklist(function_stack_entry *fse, long *filtered_flag, char *filter)
{
if (!fse->function.class && strlen(filter) == 0) {
*filtered_flag = 1;
return 1;
}
if (fse->function.class && strlen(filter) > 0 && strncasecmp(filter, fse->function.class, strlen(filter)) == 0) {
*filtered_flag = 1;
return 1;
}
return 0;
}
static void xdebug_filter_run_internal(function_stack_entry *fse, int group, long *filtered_flag, int type, xdebug_llist *filters)
{
xdebug_llist_element *le;
unsigned int k;
function_stack_entry tmp_fse;
int (*filter_to_run)(function_stack_entry *fse, long *filtered_flag, char *filter);
le = XDEBUG_LLIST_HEAD(filters);
switch (type) {
case XDEBUG_PATH_WHITELIST:
*filtered_flag = 1;
if (group == XDEBUG_FILTER_CODE_COVERAGE && fse->function.type & XFUNC_INCLUDES) {
tmp_fse.filename = fse->include_filename;
fse = &tmp_fse;
}
filter_to_run = xdebug_filter_match_path_whitelist;
break;
case XDEBUG_PATH_BLACKLIST:
*filtered_flag = 0;
if (group == XDEBUG_FILTER_CODE_COVERAGE && fse->function.type & XFUNC_INCLUDES) {
tmp_fse.filename = fse->include_filename;
fse = &tmp_fse;
}
filter_to_run = xdebug_filter_match_path_blacklist;
break;
case XDEBUG_NAMESPACE_WHITELIST:
*filtered_flag = 1;
filter_to_run = xdebug_filter_match_namespace_whitelist;
break;
case XDEBUG_NAMESPACE_BLACKLIST:
*filtered_flag = 0;
filter_to_run = xdebug_filter_match_namespace_blacklist;
break;
default:
/* Logically can't happen, but compilers can't detect that */
return;
}
for (k = 0; k < filters->size; k++, le = XDEBUG_LLIST_NEXT(le)) {
char *filter = XDEBUG_LLIST_VALP(le);
/* If the filter matched once, we're done */
if (filter_to_run(fse, filtered_flag, filter)) {
break;
}
}
}
void xdebug_filter_run_tracing(function_stack_entry *fse)
{
fse->filtered_tracing = 0;
if (XG(filter_type_tracing) != XDEBUG_FILTER_NONE) {
xdebug_filter_run_internal(fse, XDEBUG_FILTER_TRACING, &fse->filtered_tracing, XG(filter_type_tracing), XG(filters_tracing));
}
}
void xdebug_filter_run_code_coverage(zend_op_array *op_array)
{
op_array->reserved[XG(dead_code_analysis_tracker_offset)] = 0;
if (XG(filter_type_code_coverage) != XDEBUG_FILTER_NONE) {
function_stack_entry tmp_fse;
tmp_fse.filename = STR_NAME_VAL(op_array->filename);
xdebug_build_fname_from_oparray(&tmp_fse.function, op_array TSRMLS_CC);
xdebug_filter_run_internal(&tmp_fse, XDEBUG_FILTER_CODE_COVERAGE, &tmp_fse.filtered_code_coverage, XG(filter_type_code_coverage), XG(filters_code_coverage));
op_array->reserved[XG(code_coverage_filter_offset)] = (void*) tmp_fse.filtered_code_coverage;
}
}
/* {{{ proto void xdebug_set_filter(int group, int type, array filters)
This function configures filters for tracing and code coverage */
PHP_FUNCTION(xdebug_set_filter)
{
zend_long filter_group;
zend_long filter_type;
xdebug_llist **filter_list;
zval *filters, *item;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lla", &filter_group, &filter_type, &filters) == FAILURE) {
return;
}
switch (filter_group) {
case XDEBUG_FILTER_TRACING:
filter_list = &XG(filters_tracing);
XG(filter_type_tracing) = XDEBUG_FILTER_NONE;
break;
case XDEBUG_FILTER_CODE_COVERAGE:
filter_list = &XG(filters_code_coverage);
XG(filter_type_code_coverage) = XDEBUG_FILTER_NONE;
if (filter_type == XDEBUG_NAMESPACE_WHITELIST || filter_type == XDEBUG_NAMESPACE_BLACKLIST) {
php_error(E_WARNING, "The code coverage filter (XDEBUG_FILTER_CODE_COVERAGE) only supports the XDEBUG_PATH_WHITELIST, XDEBUG_PATH_BLACKLIST, and XDEBUG_FILTER_NONE filter types");
return;
}
break;
default:
php_error(E_WARNING, "Filter group needs to be one of XDEBUG_FILTER_TRACING or XDEBUG_FILTER_CODE_COVERAGE");
return;
}
if (
filter_type == XDEBUG_PATH_WHITELIST ||
filter_type == XDEBUG_PATH_BLACKLIST ||
filter_type == XDEBUG_NAMESPACE_WHITELIST ||
filter_type == XDEBUG_NAMESPACE_BLACKLIST ||
filter_type == XDEBUG_FILTER_NONE
) {
switch (filter_group) {
case XDEBUG_FILTER_TRACING:
XG(filter_type_tracing) = filter_type;
break;
case XDEBUG_FILTER_CODE_COVERAGE:
XG(filter_type_code_coverage) = filter_type;
break;
}
} else {
php_error(E_WARNING, "Filter type needs to be one of XDEBUG_PATH_WHITELIST, XDEBUG_PATH_BLACKLIST, XDEBUG_NAMESPACE_WHITELIST, XDEBUG_NAMESPACE_BLACKLIST, or XDEBUG_FILTER_NONE");
return;
}
xdebug_llist_empty(*filter_list, NULL);
if (filter_type == XDEBUG_FILTER_NONE) {
return;
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(filters), item) {
zend_string *str = zval_get_string(item);
char *filter = ZSTR_VAL(str);
/* If we are a namespace filter, and the filter name starts with \, we
* need to strip the \ from the matcher */
xdebug_llist_insert_next(*filter_list, XDEBUG_LLIST_TAIL(*filter_list), xdstrdup(filter[0] == '\\' ? &filter[1] : filter));
zend_string_release(str);
} ZEND_HASH_FOREACH_END();
}
/* }}} */