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