This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ytrace_var.c
472 lines (423 loc) · 14 KB
/
ytrace_var.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
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2016 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.0 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://xdebug.derickrethans.nl/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]> |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#include "php.h"
#include "php_ytrace.h"
#include "ytrace_compat.h"
ZEND_EXTERN_MODULE_GLOBALS(ytrace)
#if PHP_VERSION_ID >= 70000
static int ytrace_array_element_export(zval *zv_nptr, zend_ulong index_key, zend_string *hash_key, ytrace_str *str, int level)
{
zval **zv = &zv_nptr;
#else
static int ytrace_array_element_export(zval **zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
{
ytrace_str *str = va_arg(args, ytrace_str*);
int level = va_arg(args, int);
#endif
if (YTRACE_G(var_export_runtime)[level].current < YTRACE_G(display_max_children)) {
ytrace_str_appendl(str, ytrace_str_tab_pad(level), level, 1);
if (HASH_KEY_IS_NUMERIC(hash_key)) { /* numeric key */
#if PHP_VERSION_ID >= 70000
ytrace_str_append(str, ytrace_sprintf(YTRACE_INT_FMT " => ", index_key), 1);
#else
ytrace_str_append(str, ytrace_sprintf(YTRACE_INT_FMT " => ", HASH_APPLY_NUMERIC(hash_key)), 1);
#endif
} else { /* string key */
int tmp_len;
char *tmp_str;
tmp_str = ytrace_str_addcslashes((char*) HASH_APPLY_KEY_VAL(hash_key), HASH_APPLY_KEY_LEN(hash_key) - 1, &tmp_len);
ytrace_str_append(str, "'", 0);
ytrace_str_appendl(str, tmp_str, tmp_len, 0);
ytrace_str_append(str, "' => ", 0);
efree(tmp_str);
}
ytrace_var_export(zv, str, level + 1 TSRMLS_CC);
ytrace_str_append(str, ",\n", 0);
}
if (YTRACE_G(var_export_runtime)[level].current == YTRACE_G(display_max_children)) {
ytrace_str_appendl(str, ytrace_str_tab_pad(level), level, 1);
ytrace_str_append(str, "...\n", 0);
}
YTRACE_G(var_export_runtime)[level].current++;
return 0;
}
/* {{{ ytrace_get_property_info
*/
static char *ytrace_get_property_info(char *mangled_property, int mangled_len, char **property_name, char **class_name)
{
const char *prop_name, *cls_name;
#if PHP_VERSION_ID >= 70000
zend_string *i_mangled = zend_string_init(mangled_property, mangled_len - 1, 0);
zend_unmangle_property_name(i_mangled, &cls_name, &prop_name);
#else
zend_unmangle_property_name(mangled_property, mangled_len, &cls_name, &prop_name);
#endif
*property_name = (char *) strdup(prop_name);
*class_name = cls_name ? strdup(cls_name) : NULL;
#if PHP_VERSION_ID >= 70000
zend_string_release(i_mangled);
#endif
if (*class_name) {
if (*class_name[0] == '*') {
return "protected";
} else {
return "private";
}
} else {
return "public";
}
}
/* }}} */
#if PHP_VERSION_ID >= 70000
static int ytrace_object_element_export(zval *zv_nptr, zend_ulong index_key, zend_string *hash_key, ytrace_str *str, char *class_name, int level)
{
zval **zv = &zv_nptr;
#else
static int ytrace_object_element_export(zval **zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
{
ytrace_str *str = va_arg(args, ytrace_str*);
char *class_name = va_arg(args, char*);
int level = va_arg(args, int);
#endif
if (YTRACE_G(var_export_runtime)[level].current < YTRACE_G(display_max_children)) {
ytrace_str_appendl(str, ytrace_str_tab_pad(level), level, 1);
if (!HASH_KEY_IS_NUMERIC(hash_key)) {
char *prop_name, *modifier, *prop_class_name;
modifier = ytrace_get_property_info((char*) HASH_APPLY_KEY_VAL(hash_key), HASH_APPLY_KEY_LEN(hash_key), &prop_name, &prop_class_name);
if (strcmp(modifier, "private") != 0 || strcmp(class_name, prop_class_name) == 0) {
ytrace_str_append(str, ytrace_sprintf("%s $%s = ", modifier, prop_name), 1);
} else {
ytrace_str_append(str, ytrace_sprintf("%s ${%s}:%s = ", modifier, prop_class_name, prop_name), 1);
}
free(prop_name);
free(prop_class_name);
} else {
#if PHP_VERSION_ID >= 70000
ytrace_str_append(str, ytrace_sprintf("public $%d = ", index_key), 1);
#else
ytrace_str_append(str, ytrace_sprintf("public $%d = ", HASH_APPLY_NUMERIC(hash_key)), 1);
#endif
}
ytrace_var_export(zv, str, level + 1 TSRMLS_CC);
ytrace_str_append(str, ";\n", 0);
}
if (YTRACE_G(var_export_runtime)[level].current == YTRACE_G(display_max_children)) {
ytrace_str_appendl(str, ytrace_str_tab_pad(level), level, 1);
ytrace_str_append(str, "...\n", 0);
}
YTRACE_G(var_export_runtime)[level].current++;
return 0;
}
/* {{{ ytrace_get_property_info
*/
void ytrace_var_export(zval **struc, ytrace_str *str, int level TSRMLS_DC)
{
HashTable *myht;
char *tmp_str;
int is_temp = 0;
#if PHP_VERSION_ID >= 70000
zend_ulong num;
zend_string *key;
zval *val;
zval *tmpz;
#endif
if (!struc || !(*struc)) {
return;
}
#if PHP_VERSION_ID >= 70000
if (Z_TYPE_P(*struc) == IS_REFERENCE) {
tmpz = &((*struc)->value.ref->val);
struc = &tmpz;
}
#endif
switch (Z_TYPE_P(*struc)) {
#if PHP_VERSION_ID >= 70000
case IS_TRUE:
case IS_FALSE:
ytrace_str_append(str, ytrace_sprintf("%s", Z_TYPE_P(*struc) == IS_TRUE ? "TRUE" : "FALSE"), 1);
break;
#else
case IS_BOOL:
ytrace_str_append(str, ytrace_sprintf("%s", Z_LVAL_P(*struc) ? "TRUE" : "FALSE"), 1);
break;
#endif
case IS_NULL:
ytrace_str_append(str, "NULL", 0);
break;
case IS_LONG:
ytrace_str_append(str, ytrace_sprintf(YTRACE_INT_FMT, Z_LVAL_P(*struc)), 1);
break;
case IS_DOUBLE:
ytrace_str_append(str, ytrace_sprintf("%.*G", (int) EG(precision), Z_DVAL_P(*struc)), 1);
break;
case IS_STRING: {
int tmp_len;
tmp_str = ytrace_str_addcslashes(Z_STRVAL_P(*struc), Z_STRLEN_P(*struc), &tmp_len);
ytrace_str_append(str, "'", 0);
if (Z_STRLEN_P(*struc) < YTRACE_G(display_max_data)) {
ytrace_str_appendl(str, tmp_str, tmp_len, 0);
} else {
ytrace_str_appendl(str, tmp_str, YTRACE_G(display_max_data), 0);
ytrace_str_append(str, "...", 0);
}
ytrace_str_append(str, "'", 0);
efree(tmp_str);
} break;
case IS_ARRAY:
myht = Z_ARRVAL_P(*struc);
if (YTRACE_APPLY_COUNT(myht) < 1) {
if (myht->nNumOfElements == 0) {
ytrace_str_append(str, "array ()", 0);
break;
}
if (level > YTRACE_G(display_max_depth)) {
ytrace_str_append(str, "array (...)", 0);
break;
}
ytrace_str_append(str, "array (\n", 0);
YTRACE_G(var_export_runtime)[level + 1].current = 0;
#if PHP_VERSION_ID >= 70000
ZEND_HASH_INC_APPLY_COUNT(myht);
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
ytrace_array_element_export(val, num, key, str, level + 1);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_DEC_APPLY_COUNT(myht);
#else
zend_hash_apply_with_arguments(myht TSRMLS_CC, (apply_func_args_t) ytrace_array_element_export, 2, str, level + 1);
#endif
/* Remove the ", " at the end of the string */
ytrace_str_chop(str, 2);
if (level > 0) {
ytrace_str_append(str, "\n", 0);
ytrace_str_appendl(str, ytrace_str_tab_pad(level - 1), level - 1, 1);
ytrace_str_append(str, ")", 0);
} else {
ytrace_str_append(str, "\n)", 0);
}
} else {
ytrace_str_append(str, "...", 0);
}
break;
case IS_OBJECT:
if (Z_OBJ_HANDLER_P(*struc, get_debug_info)) {
myht = Z_OBJ_HANDLER_P(*struc, get_debug_info)(*struc, &is_temp TSRMLS_CC);
} else if (Z_OBJ_HANDLER_P(*struc, get_properties)) {
myht = Z_OBJPROP_P(*struc);
} else {
break;
}
if (YTRACE_APPLY_COUNT(myht) < 1) {
char *class_name = (char*) STR_NAME_VAL(Z_OBJCE_P(*struc)->name);
if (myht->nNumOfElements == 0) {
ytrace_str_append(str, ytrace_sprintf("class %s {}", class_name), 1);
break;
}
if (level > YTRACE_G(display_max_depth)) {
ytrace_str_append(str, ytrace_sprintf("class %s {...}", class_name), 1);
break;
}
ytrace_str_append(str, ytrace_sprintf("class %s {\n", class_name), 1);
YTRACE_G(var_export_runtime)[level + 1].current = 0;
#if PHP_VERSION_ID >= 70000
ZEND_HASH_INC_APPLY_COUNT(myht);
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
ytrace_object_element_export(val, num, key, str, class_name, level + 1);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_DEC_APPLY_COUNT(myht);
#else
zend_hash_apply_with_arguments(myht TSRMLS_CC, (apply_func_args_t) ytrace_object_element_export, 2, str, class_name, level + 1);
#endif
/* Remove the ", " at the end of the string */
ytrace_str_chop(str, 2);
if (level > 0) {
ytrace_str_append(str, "\n", 0);
ytrace_str_appendl(str, ytrace_str_tab_pad(level - 1), level - 1, 1);
ytrace_str_append(str, "}", 0);
} else {
ytrace_str_append(str, "\n}", 0);
}
} else {
ytrace_str_append(str, "...", 0);
}
if (is_temp) {
zend_hash_destroy(myht);
efree(myht);
}
break;
case IS_RESOURCE: {
char *type_name;
#if PHP_VERSION_ID >= 70000
type_name = (char *) zend_rsrc_list_get_rsrc_type(Z_RES_P(*struc) TSRMLS_CC);
ytrace_str_append(str, ytrace_sprintf("resource(%ld) of type (%s)", Z_RES_P(*struc)->handle, type_name ? type_name : "Unknown"), 1);
#else
type_name = (char *) zend_rsrc_list_get_rsrc_type(Z_LVAL_P(*struc) TSRMLS_CC);
ytrace_str_append(str, ytrace_sprintf("resource(%ld) of type (%s)", Z_LVAL_P(*struc), type_name ? type_name : "Unknown"), 1);
#endif
break;
}
#if PHP_VERSION_ID >= 70000
case IS_UNDEF:
ytrace_str_append(str, "*uninitialized*", 0);
break;
#endif
default:
ytrace_str_append(str, "NFC", 0);
break;
}
}
/* }}} */
#if PHP_VERSION_ID >= 70000
# define T(offset) (*(union _temp_variable *)((char*)zdata->current_execute_data->Ts + offset))
#elif PHP_VERSION_ID >= 50500
# define T(offset) (*EX_TMP_VAR(zdata, offset))
#else
# define T(offset) (*(temp_variable *)((char*)zdata->Ts + offset))
#endif
#if PHP_VERSION_ID >= 70000
zval *ytrace_get_zval(zend_execute_data *zdata, int node_type, const znode_op *node)
{
zend_free_op should_free;
return zend_get_zval_ptr(node_type, node, zdata, &should_free, BP_VAR_IS);
}
#else
zval *ytrace_get_zval(zend_execute_data *zdata, int node_type, const znode_op *node)
{
switch (node_type) {
case IS_CONST:
#if PHP_VERSION_ID >= 50300
return node->zv;
#else
return &node->u.constant;
#endif
break;
case IS_TMP_VAR:
return &T(node->var).tmp_var;
break;
case IS_VAR:
if (T(node->var).var.ptr) {
return T(node->var).var.ptr;
}
break;
case IS_CV: {
zval **tmp;
tmp = zend_get_compiled_variable_value(zdata, node->constant);
if (tmp) {
return *tmp;
}
break;
}
case IS_UNUSED:
//fprintf(stderr, "\nIS_UNUSED\n");
break;
default:
//fprintf(stderr, "\ndefault %d\n", node_type);
break;
}
return NULL;
}
#endif
void ytrace_get_zval_value(zval *val, ytrace_str *str)
{
int tmp_len;
char *tmp_str;
ytrace_str zval_str;
ytrace_str_new(&zval_str);
/* init variable display settings */
if (YTRACE_G(display_max_children) > 32) {
YTRACE_G(display_max_children) = 32;
} else if (YTRACE_G(display_max_children) < 1) {
YTRACE_G(display_max_children) = 0;
}
if (YTRACE_G(display_max_data) > 1024) {
YTRACE_G(display_max_data) = 1024;
} else if (YTRACE_G(display_max_data) < 1) {
YTRACE_G(display_max_data) = 0;
}
if (YTRACE_G(display_max_depth) > 16) {
YTRACE_G(display_max_depth) = 16;
} else if (YTRACE_G(display_max_depth) < 1) {
YTRACE_G(display_max_depth) = 0;
}
ytrace_var_export(&val, &zval_str, 0 TSRMLS_CC);
tmp_str = ytrace_str_addcslashes(zval_str.c, zval_str.len, &tmp_len);
ytrace_str_appendl(str, tmp_str, tmp_len, 0);
efree(tmp_str);
ytrace_str_destroy(&zval_str);
}
char* ytrace_get_val_from_server(char *var_name)
{
#if PHP_VERSION_ID >= 70000
zval *val = NULL;
if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY) {
val = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), var_name, strlen(var_name));
if (val) {
return strndup(Z_STRVAL_P(val), Z_STRLEN_P(val));
}
}
#else
int retval = FAILURE;
zval **val;
if (PG(http_globals)[TRACK_VARS_SERVER]) {
retval = zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), var_name, strlen(var_name) + 1, (void **) &val);
if (retval == SUCCESS) {
return strndup(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
}
}
#endif
return NULL;
}
char* ytrace_get_val_from_http_data(char *var_name)
{
#if PHP_VERSION_ID >= 70000
zval *val;
#else
zval **val;
#endif
if (
(
#if PHP_VERSION_ID >= 70000
(
(val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_GET]), var_name, strlen(var_name))) != NULL
) || (
(val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_POST]), var_name, strlen(var_name))) != NULL
) || (
(val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_COOKIE]), var_name, strlen(var_name))) != NULL
)
#else
(
PG(http_globals)[TRACK_VARS_GET] &&
zend_hash_find(PG(http_globals)[TRACK_VARS_GET]->value.ht, var_name, strlen(var_name) + 1, (void **) &val) == SUCCESS
) || (
PG(http_globals)[TRACK_VARS_POST] &&
zend_hash_find(PG(http_globals)[TRACK_VARS_POST]->value.ht, var_name, strlen(var_name) + 1, (void **) &val) == SUCCESS
) || (
PG(http_globals)[TRACK_VARS_COOKIE] &&
zend_hash_find(PG(http_globals)[TRACK_VARS_COOKIE]->value.ht, var_name, strlen(var_name) + 1, (void **) &val) == SUCCESS
)
#endif
)
) {
#if PHP_VERSION_ID >= 70000
return strndup(Z_STRVAL_P(val), Z_STRLEN_P(val));
#else
return strndup(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
#endif
}
return NULL;
}