1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Felipe Pena <felipe@php.net> |
16 | Authors: Joe Watkins <joe.watkins@live.co.uk> |
17 | Authors: Bob Weinand <bwoebi@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #include "zend.h"
22
23 #include "php.h"
24 #include "phpdbg.h"
25 #include "phpdbg_opcode.h"
26 #include "phpdbg_utils.h"
27 #include "ext/standard/php_string.h"
28
29 /* FASYNC under Solaris */
30 #ifdef HAVE_SYS_FILE_H
31 # include <sys/file.h>
32 #endif
33
34 #ifdef HAVE_SYS_IOCTL_H
35 # include "sys/ioctl.h"
36 # ifndef GWINSZ_IN_SYS_IOCTL
37 # include <termios.h>
38 # endif
39 #endif
40
41 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
42
43 /* {{{ color structures */
44 const static phpdbg_color_t colors[] = {
45 PHPDBG_COLOR_D("none", "0;0"),
46
47 PHPDBG_COLOR_D("white", "0;64"),
48 PHPDBG_COLOR_D("white-bold", "1;64"),
49 PHPDBG_COLOR_D("white-underline", "4;64"),
50 PHPDBG_COLOR_D("red", "0;31"),
51 PHPDBG_COLOR_D("red-bold", "1;31"),
52 PHPDBG_COLOR_D("red-underline", "4;31"),
53 PHPDBG_COLOR_D("green", "0;32"),
54 PHPDBG_COLOR_D("green-bold", "1;32"),
55 PHPDBG_COLOR_D("green-underline", "4;32"),
56 PHPDBG_COLOR_D("yellow", "0;33"),
57 PHPDBG_COLOR_D("yellow-bold", "1;33"),
58 PHPDBG_COLOR_D("yellow-underline", "4;33"),
59 PHPDBG_COLOR_D("blue", "0;34"),
60 PHPDBG_COLOR_D("blue-bold", "1;34"),
61 PHPDBG_COLOR_D("blue-underline", "4;34"),
62 PHPDBG_COLOR_D("purple", "0;35"),
63 PHPDBG_COLOR_D("purple-bold", "1;35"),
64 PHPDBG_COLOR_D("purple-underline", "4;35"),
65 PHPDBG_COLOR_D("cyan", "0;36"),
66 PHPDBG_COLOR_D("cyan-bold", "1;36"),
67 PHPDBG_COLOR_D("cyan-underline", "4;36"),
68 PHPDBG_COLOR_D("black", "0;30"),
69 PHPDBG_COLOR_D("black-bold", "1;30"),
70 PHPDBG_COLOR_D("black-underline", "4;30"),
71 PHPDBG_COLOR_END
72 }; /* }}} */
73
74 /* {{{ */
75 const static phpdbg_element_t elements[] = {
76 PHPDBG_ELEMENT_D("prompt", PHPDBG_COLOR_PROMPT),
77 PHPDBG_ELEMENT_D("error", PHPDBG_COLOR_ERROR),
78 PHPDBG_ELEMENT_D("notice", PHPDBG_COLOR_NOTICE),
79 PHPDBG_ELEMENT_END
80 }; /* }}} */
81
phpdbg_is_numeric(const char * str)82 PHPDBG_API int phpdbg_is_numeric(const char *str) /* {{{ */
83 {
84 if (!str)
85 return 0;
86
87 for (; *str; str++) {
88 if (isspace(*str) || *str == '-') {
89 continue;
90 }
91 return isdigit(*str);
92 }
93 return 0;
94 } /* }}} */
95
phpdbg_is_empty(const char * str)96 PHPDBG_API int phpdbg_is_empty(const char *str) /* {{{ */
97 {
98 if (!str)
99 return 1;
100
101 for (; *str; str++) {
102 if (isspace(*str)) {
103 continue;
104 }
105 return 0;
106 }
107 return 1;
108 } /* }}} */
109
phpdbg_is_addr(const char * str)110 PHPDBG_API int phpdbg_is_addr(const char *str) /* {{{ */
111 {
112 return str[0] && str[1] && memcmp(str, "0x", 2) == 0;
113 } /* }}} */
114
phpdbg_is_class_method(const char * str,size_t len,char ** class,char ** method)115 PHPDBG_API int phpdbg_is_class_method(const char *str, size_t len, char **class, char **method) /* {{{ */
116 {
117 char *sep = NULL;
118
119 if (strstr(str, "#") != NULL)
120 return 0;
121
122 if (strstr(str, " ") != NULL)
123 return 0;
124
125 sep = strstr(str, "::");
126
127 if (!sep || sep == str || sep+2 == str+len-1) {
128 return 0;
129 }
130
131 if (class != NULL) {
132
133 if (str[0] == '\\') {
134 str++;
135 len--;
136 }
137
138 *class = estrndup(str, sep - str);
139 (*class)[sep - str] = 0;
140 }
141
142 if (method != NULL) {
143 *method = estrndup(sep+2, str + len - (sep + 2));
144 }
145
146 return 1;
147 } /* }}} */
148
phpdbg_resolve_path(const char * path)149 PHPDBG_API char *phpdbg_resolve_path(const char *path) /* {{{ */
150 {
151 char resolved_name[MAXPATHLEN];
152
153 if (expand_filepath(path, resolved_name) == NULL) {
154 return NULL;
155 }
156
157 return strdup(resolved_name);
158 } /* }}} */
159
phpdbg_current_file(void)160 PHPDBG_API const char *phpdbg_current_file(void) /* {{{ */
161 {
162 const char *file = zend_get_executed_filename();
163
164 if (memcmp(file, "[no active file]", sizeof("[no active file]")) == 0) {
165 return PHPDBG_G(exec);
166 }
167
168 return file;
169 } /* }}} */
170
phpdbg_get_function(const char * fname,const char * cname)171 PHPDBG_API const zend_function *phpdbg_get_function(const char *fname, const char *cname) /* {{{ */
172 {
173 zend_function *func = NULL;
174 zend_string *lfname = zend_string_init(fname, strlen(fname), 0);
175 zend_string *tmp = zend_string_tolower(lfname);
176 zend_string_release(lfname);
177 lfname = tmp;
178
179 if (cname) {
180 zend_class_entry *ce;
181 zend_string *lcname = zend_string_init(cname, strlen(cname), 0);
182 tmp = zend_string_tolower(lcname);
183 zend_string_release(lcname);
184 lcname = tmp;
185 ce = zend_lookup_class(lcname);
186
187 zend_string_release(lcname);
188
189 if (ce) {
190 func = zend_hash_find_ptr(&ce->function_table, lfname);
191 }
192 } else {
193 func = zend_hash_find_ptr(EG(function_table), lfname);
194 }
195
196 zend_string_release(lfname);
197 return func;
198 } /* }}} */
199
phpdbg_trim(const char * str,size_t len,size_t * new_len)200 PHPDBG_API char *phpdbg_trim(const char *str, size_t len, size_t *new_len) /* {{{ */
201 {
202 const char *p = str;
203 char *new = NULL;
204
205 while (p && isspace(*p)) {
206 ++p;
207 --len;
208 }
209
210 while (*p && isspace(*(p + len -1))) {
211 --len;
212 }
213
214 if (len == 0) {
215 new = estrndup("", sizeof(""));
216 *new_len = 0;
217 } else {
218 new = estrndup(p, len);
219 *(new + len) = '\0';
220
221 if (new_len) {
222 *new_len = len;
223 }
224 }
225
226 return new;
227
228 } /* }}} */
229
phpdbg_get_color(const char * name,size_t name_length)230 PHPDBG_API const phpdbg_color_t *phpdbg_get_color(const char *name, size_t name_length) /* {{{ */
231 {
232 const phpdbg_color_t *color = colors;
233
234 while (color && color->name) {
235 if (name_length == color->name_length &&
236 memcmp(name, color->name, name_length) == SUCCESS) {
237 phpdbg_debug("phpdbg_get_color(%s, %lu): %s", name, name_length, color->code);
238 return color;
239 }
240 ++color;
241 }
242
243 phpdbg_debug("phpdbg_get_color(%s, %lu): failed", name, name_length);
244
245 return NULL;
246 } /* }}} */
247
phpdbg_set_color(int element,const phpdbg_color_t * color)248 PHPDBG_API void phpdbg_set_color(int element, const phpdbg_color_t *color) /* {{{ */
249 {
250 PHPDBG_G(colors)[element] = color;
251 } /* }}} */
252
phpdbg_set_color_ex(int element,const char * name,size_t name_length)253 PHPDBG_API void phpdbg_set_color_ex(int element, const char *name, size_t name_length) /* {{{ */
254 {
255 const phpdbg_color_t *color = phpdbg_get_color(name, name_length);
256
257 if (color) {
258 phpdbg_set_color(element, color);
259 } else PHPDBG_G(colors)[element] = colors;
260 } /* }}} */
261
phpdbg_get_colors(void)262 PHPDBG_API const phpdbg_color_t* phpdbg_get_colors(void) /* {{{ */
263 {
264 return colors;
265 } /* }}} */
266
phpdbg_get_element(const char * name,size_t len)267 PHPDBG_API int phpdbg_get_element(const char *name, size_t len) {
268 const phpdbg_element_t *element = elements;
269
270 while (element && element->name) {
271 if (len == element->name_length) {
272 if (strncasecmp(name, element->name, len) == SUCCESS) {
273 return element->id;
274 }
275 }
276 element++;
277 }
278
279 return PHPDBG_COLOR_INVALID;
280 }
281
phpdbg_set_prompt(const char * prompt)282 PHPDBG_API void phpdbg_set_prompt(const char *prompt) /* {{{ */
283 {
284 /* free formatted prompt */
285 if (PHPDBG_G(prompt)[1]) {
286 free(PHPDBG_G(prompt)[1]);
287 PHPDBG_G(prompt)[1] = NULL;
288 }
289 /* free old prompt */
290 if (PHPDBG_G(prompt)[0]) {
291 free(PHPDBG_G(prompt)[0]);
292 PHPDBG_G(prompt)[0] = NULL;
293 }
294
295 /* copy new prompt */
296 PHPDBG_G(prompt)[0] = strdup(prompt);
297 } /* }}} */
298
phpdbg_get_prompt(void)299 PHPDBG_API const char *phpdbg_get_prompt(void) /* {{{ */
300 {
301 /* find cached prompt */
302 if (PHPDBG_G(prompt)[1]) {
303 return PHPDBG_G(prompt)[1];
304 }
305
306 /* create cached prompt */
307 #ifndef HAVE_LIBEDIT
308 /* TODO: libedit doesn't seems to support coloured prompt */
309 if ((PHPDBG_G(flags) & PHPDBG_IS_COLOURED)) {
310 ZEND_IGNORE_VALUE(asprintf(&PHPDBG_G(prompt)[1], "\033[%sm%s\033[0m ",
311 PHPDBG_G(colors)[PHPDBG_COLOR_PROMPT]->code,
312 PHPDBG_G(prompt)[0]));
313 } else
314 #endif
315 {
316 ZEND_IGNORE_VALUE(asprintf(&PHPDBG_G(prompt)[1], "%s ", PHPDBG_G(prompt)[0]));
317 }
318
319 return PHPDBG_G(prompt)[1];
320 } /* }}} */
321
phpdbg_rebuild_symtable(void)322 int phpdbg_rebuild_symtable(void) {
323 if (!EG(current_execute_data) || !EG(current_execute_data)->func) {
324 phpdbg_error("inactive", "type=\"op_array\"", "No active op array!");
325 return FAILURE;
326 }
327
328 if (!zend_rebuild_symbol_table()) {
329 phpdbg_error("inactive", "type=\"symbol_table\"", "No active symbol table!");
330 return FAILURE;
331 }
332
333 return SUCCESS;
334 }
335
phpdbg_get_terminal_width(void)336 PHPDBG_API int phpdbg_get_terminal_width(void) /* {{{ */
337 {
338 int columns;
339 #ifdef _WIN32
340 CONSOLE_SCREEN_BUFFER_INFO csbi;
341
342 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
343 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
344 #elif defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
345 struct winsize w;
346
347 columns = ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0 ? w.ws_col : 80;
348 #else
349 columns = 80;
350 #endif
351 return columns;
352 } /* }}} */
353
phpdbg_get_terminal_height(void)354 PHPDBG_API int phpdbg_get_terminal_height(void) /* {{{ */
355 {
356 int lines;
357 #ifdef _WIN32
358 CONSOLE_SCREEN_BUFFER_INFO csbi;
359
360 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
361 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
362 } else {
363 lines = 40;
364 }
365 #elif defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
366 struct winsize w;
367
368 lines = ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0 ? w.ws_row : 40;
369 #else
370 lines = 40;
371 #endif
372 return lines;
373 } /* }}} */
374
phpdbg_set_async_io(int fd)375 PHPDBG_API void phpdbg_set_async_io(int fd) {
376 #if !defined(_WIN32) && defined(FASYNC)
377 int flags;
378 fcntl(STDIN_FILENO, F_SETOWN, getpid());
379 flags = fcntl(STDIN_FILENO, F_GETFL);
380 fcntl(STDIN_FILENO, F_SETFL, flags | FASYNC);
381 #endif
382 }
383
phpdbg_safe_class_lookup(const char * name,int name_length,zend_class_entry ** ce)384 int phpdbg_safe_class_lookup(const char *name, int name_length, zend_class_entry **ce) {
385 if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
386 char *lc_name, *lc_free;
387 int lc_length;
388
389 if (name == NULL || !name_length) {
390 return FAILURE;
391 }
392
393 lc_free = lc_name = emalloc(name_length + 1);
394 zend_str_tolower_copy(lc_name, name, name_length);
395 lc_length = name_length + 1;
396
397 if (lc_name[0] == '\\') {
398 lc_name += 1;
399 lc_length -= 1;
400 }
401
402 phpdbg_try_access {
403 *ce = zend_hash_str_find_ptr(EG(class_table), lc_name, lc_length);
404 } phpdbg_catch_access {
405 phpdbg_error("signalsegv", "class=\"%.*s\"", "Could not fetch class %.*s, invalid data source", name_length, name);
406 } phpdbg_end_try_access();
407
408 efree(lc_free);
409 } else {
410 zend_string *str_name = zend_string_init(name, name_length, 0);
411 *ce = zend_lookup_class(str_name);
412 efree(str_name);
413 }
414
415 return *ce ? SUCCESS : FAILURE;
416 }
417
phpdbg_get_property_key(char * key)418 char *phpdbg_get_property_key(char *key) {
419 if (*key != 0) {
420 return key;
421 }
422 return strchr(key + 1, 0) + 1;
423 }
424
phpdbg_parse_variable_arg_wrapper(char * name,size_t len,char * keyname,size_t keylen,HashTable * parent,zval * zv,phpdbg_parse_var_func callback)425 static int phpdbg_parse_variable_arg_wrapper(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv, phpdbg_parse_var_func callback) {
426 return callback(name, len, keyname, keylen, parent, zv);
427 }
428
phpdbg_parse_variable(char * input,size_t len,HashTable * parent,size_t i,phpdbg_parse_var_func callback,zend_bool silent)429 PHPDBG_API int phpdbg_parse_variable(char *input, size_t len, HashTable *parent, size_t i, phpdbg_parse_var_func callback, zend_bool silent) {
430 return phpdbg_parse_variable_with_arg(input, len, parent, i, (phpdbg_parse_var_with_arg_func) phpdbg_parse_variable_arg_wrapper, NULL, silent, callback);
431 }
432
phpdbg_parse_variable_with_arg(char * input,size_t len,HashTable * parent,size_t i,phpdbg_parse_var_with_arg_func callback,phpdbg_parse_var_with_arg_func step_cb,zend_bool silent,void * arg)433 PHPDBG_API int phpdbg_parse_variable_with_arg(char *input, size_t len, HashTable *parent, size_t i, phpdbg_parse_var_with_arg_func callback, phpdbg_parse_var_with_arg_func step_cb, zend_bool silent, void *arg) {
434 int ret = FAILURE;
435 zend_bool new_index = 1;
436 char *last_index = NULL;
437 size_t index_len = 0;
438 zval *zv;
439
440 if (len < 2 || *input != '$') {
441 goto error;
442 }
443
444 while (i++ < len) {
445 if (i == len) {
446 new_index = 1;
447 } else {
448 switch (input[i]) {
449 case '[':
450 new_index = 1;
451 break;
452 case ']':
453 break;
454 case '>':
455 if (last_index[index_len - 1] == '-') {
456 new_index = 1;
457 index_len--;
458 }
459 break;
460
461 default:
462 if (new_index) {
463 last_index = input + i;
464 new_index = 0;
465 }
466 if (input[i - 1] == ']') {
467 goto error;
468 }
469 index_len++;
470 }
471 }
472
473 if (new_index && index_len == 0) {
474 zend_ulong numkey;
475 zend_string *strkey;
476 ZEND_HASH_FOREACH_KEY_VAL_IND(parent, numkey, strkey, zv) {
477 if (i == len || (i == len - 1 && input[len - 1] == ']')) {
478 char *key, *propkey;
479 size_t namelen, keylen;
480 char *name;
481 char *keyname = estrndup(last_index, index_len);
482 if (strkey) {
483 key = ZSTR_VAL(strkey);
484 keylen = ZSTR_LEN(strkey);
485 } else {
486 keylen = spprintf(&key, 0, ZEND_ULONG_FMT, numkey);
487 }
488 propkey = phpdbg_get_property_key(key);
489 name = emalloc(i + keylen + 2);
490 namelen = sprintf(name, "%.*s%.*s%s", (int) i, input, (int) (keylen - (propkey - key)), propkey, input[len - 1] == ']'?"]":"");
491 if (!strkey) {
492 efree(key);
493 }
494
495 ret = callback(name, namelen, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
496 } else retry_ref: if (Z_TYPE_P(zv) == IS_OBJECT) {
497 if (step_cb) {
498 char *name = estrndup(input, i);
499 char *keyname = estrndup(last_index, index_len);
500
501 ret = step_cb(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
502 }
503
504 phpdbg_parse_variable_with_arg(input, len, Z_OBJPROP_P(zv), i, callback, step_cb, silent, arg);
505 } else if (Z_TYPE_P(zv) == IS_ARRAY) {
506 if (step_cb) {
507 char *name = estrndup(input, i);
508 char *keyname = estrndup(last_index, index_len);
509
510 ret = step_cb(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
511 }
512
513 phpdbg_parse_variable_with_arg(input, len, Z_ARRVAL_P(zv), i, callback, step_cb, silent, arg);
514 } else if (Z_ISREF_P(zv)) {
515 if (step_cb) {
516 char *name = estrndup(input, i);
517 char *keyname = estrndup(last_index, index_len);
518
519 ret = step_cb(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
520 }
521
522 ZVAL_DEREF(zv);
523 goto retry_ref;
524 } else {
525 /* Ignore silently */
526 }
527 } ZEND_HASH_FOREACH_END();
528 return ret;
529 } else if (new_index) {
530 char last_chr = last_index[index_len];
531 last_index[index_len] = 0;
532 if (!(zv = zend_symtable_str_find(parent, last_index, index_len))) {
533 if (!silent) {
534 phpdbg_error("variable", "type=\"undefined\" variable=\"%.*s\"", "%.*s is undefined", (int) input[i] == ']' ? i + 1 : i, input);
535 }
536 return FAILURE;
537 }
538 while (Z_TYPE_P(zv) == IS_INDIRECT) {
539 zv = Z_INDIRECT_P(zv);
540 }
541
542 last_index[index_len] = last_chr;
543 if (i == len) {
544 char *name = estrndup(input, i);
545 char *keyname = estrndup(last_index, index_len);
546
547 ret = callback(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
548 } else retry_ref_end: if (Z_TYPE_P(zv) == IS_OBJECT) {
549 if (step_cb) {
550 char *name = estrndup(input, i);
551 char *keyname = estrndup(last_index, index_len);
552
553 ret = step_cb(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
554 }
555
556 parent = Z_OBJPROP_P(zv);
557 } else if (Z_TYPE_P(zv) == IS_ARRAY) {
558 if (step_cb) {
559 char *name = estrndup(input, i);
560 char *keyname = estrndup(last_index, index_len);
561
562 ret = step_cb(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
563 }
564
565 parent = Z_ARRVAL_P(zv);
566 } else if (Z_ISREF_P(zv)) {
567 if (step_cb) {
568 char *name = estrndup(input, i);
569 char *keyname = estrndup(last_index, index_len);
570
571 ret = step_cb(name, i, keyname, index_len, parent, zv, arg) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
572 }
573
574 ZVAL_DEREF(zv);
575 goto retry_ref_end;
576 } else {
577 phpdbg_error("variable", "type=\"notiterable\" variable=\"%.*s\"", "%.*s is nor an array nor an object", (int) (input[i] == '>' ? i - 1 : i), input);
578 return FAILURE;
579 }
580 index_len = 0;
581 }
582 }
583
584 return ret;
585 error:
586 phpdbg_error("variable", "type=\"invalidinput\"", "Malformed input");
587 return FAILURE;
588 }
589
phpdbg_is_auto_global(char * name,int len)590 int phpdbg_is_auto_global(char *name, int len) {
591 return zend_is_auto_global_str(name, len);
592 }
593
phpdbg_xml_array_element_dump(zval * zv,zend_string * key,zend_ulong num)594 static int phpdbg_xml_array_element_dump(zval *zv, zend_string *key, zend_ulong num) {
595 phpdbg_xml("<element");
596
597 phpdbg_try_access {
598 if (key) { /* string key */
599 phpdbg_xml(" name=\"%.*s\"", (int) ZSTR_LEN(key), ZSTR_VAL(key));
600 } else { /* numeric key */
601 phpdbg_xml(" name=\"%ld\"", num);
602 }
603 } phpdbg_catch_access {
604 phpdbg_xml(" severity=\"error\" ></element>");
605 return 0;
606 } phpdbg_end_try_access();
607
608 phpdbg_xml(">");
609
610 phpdbg_xml_var_dump(zv);
611
612 phpdbg_xml("</element>");
613
614 return 0;
615 }
616
phpdbg_xml_object_property_dump(zval * zv,zend_string * key,zend_ulong num)617 static int phpdbg_xml_object_property_dump(zval *zv, zend_string *key, zend_ulong num) {
618 phpdbg_xml("<property");
619
620 phpdbg_try_access {
621 if (key) { /* string key */
622 const char *prop_name, *class_name;
623 int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
624
625 if (class_name && unmangle == SUCCESS) {
626 phpdbg_xml(" name=\"%s\"", prop_name);
627 if (class_name[0] == '*') {
628 phpdbg_xml(" protection=\"protected\"");
629 } else {
630 phpdbg_xml(" class=\"%s\" protection=\"private\"", class_name);
631 }
632 } else {
633 phpdbg_xml(" name=\"%.*s\" protection=\"public\"", (int) ZSTR_LEN(key), ZSTR_VAL(key));
634 }
635 } else { /* numeric key */
636 phpdbg_xml(" name=\"%ld\" protection=\"public\"", num);
637 }
638 } phpdbg_catch_access {
639 phpdbg_xml(" severity=\"error\" ></property>");
640 return 0;
641 } phpdbg_end_try_access();
642
643 phpdbg_xml(">");
644
645 phpdbg_xml_var_dump(zv);
646
647 phpdbg_xml("</property>");
648
649 return 0;
650 }
651
652 #define COMMON (is_ref ? "&" : "")
653
phpdbg_xml_var_dump(zval * zv)654 PHPDBG_API void phpdbg_xml_var_dump(zval *zv) {
655 HashTable *myht;
656 zend_string *class_name, *key;
657 zend_ulong num;
658 zval *val;
659 int (*element_dump_func)(zval *zv, zend_string *key, zend_ulong num);
660 zend_bool is_ref = 0;
661
662 phpdbg_try_access {
663 is_ref = Z_ISREF_P(zv) && GC_REFCOUNT(Z_COUNTED_P(zv)) > 1;
664 ZVAL_DEREF(zv);
665
666 switch (Z_TYPE_P(zv)) {
667 case IS_TRUE:
668 phpdbg_xml("<bool refstatus=\"%s\" value=\"true\" />", COMMON);
669 break;
670 case IS_FALSE:
671 phpdbg_xml("<bool refstatus=\"%s\" value=\"false\" />", COMMON);
672 break;
673 case IS_NULL:
674 phpdbg_xml("<null refstatus=\"%s\" />", COMMON);
675 break;
676 case IS_LONG:
677 phpdbg_xml("<int refstatus=\"%s\" value=\"" ZEND_LONG_FMT "\" />", COMMON, Z_LVAL_P(zv));
678 break;
679 case IS_DOUBLE:
680 phpdbg_xml("<float refstatus=\"%s\" value=\"%.*G\" />", COMMON, (int) EG(precision), Z_DVAL_P(zv));
681 break;
682 case IS_STRING:
683 phpdbg_xml("<string refstatus=\"%s\" length=\"%zd\" value=\"%.*s\" />", COMMON, Z_STRLEN_P(zv), (int) Z_STRLEN_P(zv), Z_STRVAL_P(zv));
684 break;
685 case IS_ARRAY:
686 myht = Z_ARRVAL_P(zv);
687 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
688 if (GC_IS_RECURSIVE(myht)) {
689 phpdbg_xml("<recursion />");
690 break;
691 }
692 GC_PROTECT_RECURSION(myht);
693 }
694 phpdbg_xml("<array refstatus=\"%s\" num=\"%d\">", COMMON, zend_hash_num_elements(myht));
695 element_dump_func = phpdbg_xml_array_element_dump;
696 goto head_done;
697 case IS_OBJECT:
698 myht = zend_get_properties_for(zv, ZEND_PROP_PURPOSE_DEBUG);
699 if (myht && GC_IS_RECURSIVE(myht)) {
700 phpdbg_xml("<recursion />");
701 break;
702 }
703
704 class_name = Z_OBJ_HANDLER_P(zv, get_class_name)(Z_OBJ_P(zv));
705 phpdbg_xml("<object refstatus=\"%s\" class=\"%.*s\" id=\"%d\" num=\"%d\">", COMMON, (int) ZSTR_LEN(class_name), ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(zv), myht ? zend_hash_num_elements(myht) : 0);
706 zend_string_release(class_name);
707
708 element_dump_func = phpdbg_xml_object_property_dump;
709 head_done:
710 if (myht) {
711 ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
712 element_dump_func(val, key, num);
713 } ZEND_HASH_FOREACH_END();
714 zend_hash_apply_with_arguments(myht, (apply_func_args_t) element_dump_func, 0);
715 GC_UNPROTECT_RECURSION(myht);
716 if (Z_TYPE_P(zv) == IS_OBJECT) {
717 zend_release_properties(myht);
718 }
719 }
720 if (Z_TYPE_P(zv) == IS_ARRAY) {
721 phpdbg_xml("</array>");
722 } else {
723 phpdbg_xml("</object>");
724 }
725 break;
726 case IS_RESOURCE: {
727 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(zv));
728 phpdbg_xml("<resource refstatus=\"%s\" id=\"%pd\" type=\"%s\" />", COMMON, Z_RES_P(zv)->handle, type_name ? type_name : "unknown");
729 break;
730 }
731 default:
732 break;
733 }
734 } phpdbg_end_try_access();
735 }
736
phpdbg_check_caught_ex(zend_execute_data * execute_data,zend_object * exception)737 PHPDBG_API zend_bool phpdbg_check_caught_ex(zend_execute_data *execute_data, zend_object *exception) {
738 const zend_op *op;
739 zend_op *cur;
740 uint32_t op_num, i;
741 zend_op_array *op_array = &execute_data->func->op_array;
742
743 if (execute_data->opline >= EG(exception_op) && execute_data->opline < EG(exception_op) + 3) {
744 op = EG(opline_before_exception);
745 } else {
746 op = execute_data->opline;
747 }
748
749 op_num = op - op_array->opcodes;
750
751 for (i = 0; i < op_array->last_try_catch && op_array->try_catch_array[i].try_op <= op_num; i++) {
752 uint32_t catch = op_array->try_catch_array[i].catch_op, finally = op_array->try_catch_array[i].finally_op;
753 if (op_num <= catch || op_num <= finally) {
754 if (finally) {
755 return 1;
756 }
757
758 cur = &op_array->opcodes[catch];
759 while (1) {
760 zend_class_entry *ce;
761
762 if (!(ce = CACHED_PTR(cur->extended_value & ~ZEND_LAST_CATCH))) {
763 ce = zend_fetch_class_by_name(Z_STR_P(RT_CONSTANT(cur, cur->op1)), Z_STR_P(RT_CONSTANT(cur, cur->op1) + 1), ZEND_FETCH_CLASS_NO_AUTOLOAD);
764 CACHE_PTR(cur->extended_value & ~ZEND_LAST_CATCH, ce);
765 }
766
767 if (ce == exception->ce || (ce && instanceof_function(exception->ce, ce))) {
768 return 1;
769 }
770
771 if (cur->extended_value & ZEND_LAST_CATCH) {
772 return 0;
773 }
774
775 cur = OP_JMP_ADDR(cur, cur->op2);
776 }
777
778 return 0;
779 }
780 }
781
782 return op->opcode == ZEND_CATCH;
783 }
784
phpdbg_short_zval_print(zval * zv,int maxlen)785 char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
786 {
787 char *decode = NULL;
788
789 switch (Z_TYPE_P(zv)) {
790 case IS_UNDEF:
791 decode = estrdup("");
792 break;
793 case IS_NULL:
794 decode = estrdup("null");
795 break;
796 case IS_FALSE:
797 decode = estrdup("false");
798 break;
799 case IS_TRUE:
800 decode = estrdup("true");
801 break;
802 case IS_LONG:
803 spprintf(&decode, 0, ZEND_LONG_FMT, Z_LVAL_P(zv));
804 break;
805 case IS_DOUBLE:
806 spprintf(&decode, 0, "%.*G", 14, Z_DVAL_P(zv));
807
808 /* Make sure it looks like a float */
809 if (zend_finite(Z_DVAL_P(zv)) && !strchr(decode, '.')) {
810 size_t len = strlen(decode);
811 char *decode2 = emalloc(len + strlen(".0") + 1);
812 memcpy(decode2, decode, len);
813 decode2[len] = '.';
814 decode2[len+1] = '0';
815 decode2[len+2] = '\0';
816 efree(decode);
817 decode = decode2;
818 }
819 break;
820 case IS_STRING: {
821 int i;
822 zend_string *str = php_addcslashes(Z_STR_P(zv), "\\\"\n\t\0", 5);
823 for (i = 0; i < ZSTR_LEN(str); i++) {
824 if (ZSTR_VAL(str)[i] < 32) {
825 ZSTR_VAL(str)[i] = ' ';
826 }
827 }
828 spprintf(&decode, 0, "\"%.*s\"%c",
829 ZSTR_LEN(str) <= maxlen - 2 ? (int) ZSTR_LEN(str) : (maxlen - 3),
830 ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen - 2 ? 0 : '+');
831 zend_string_release(str);
832 } break;
833 case IS_RESOURCE:
834 spprintf(&decode, 0, "Rsrc #%d", Z_RES_HANDLE_P(zv));
835 break;
836 case IS_ARRAY:
837 spprintf(&decode, 0, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));
838 break;
839 case IS_OBJECT: {
840 zend_string *str = Z_OBJCE_P(zv)->name;
841 spprintf(&decode, 0, "%.*s%c",
842 ZSTR_LEN(str) <= maxlen ? (int) ZSTR_LEN(str) : maxlen - 1,
843 ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen ? 0 : '+');
844 break;
845 }
846 case IS_CONSTANT_AST: {
847 zend_ast *ast = Z_ASTVAL_P(zv);
848
849 if (ast->kind == ZEND_AST_CONSTANT
850 || ast->kind == ZEND_AST_CONSTANT_CLASS) {
851 decode = estrdup("<constant>");
852 } else {
853 decode = estrdup("<ast>");
854 }
855 break;
856 }
857 default:
858 spprintf(&decode, 0, "unknown type: %d", Z_TYPE_P(zv));
859 break;
860 }
861
862 return decode;
863 } /* }}} */
864