xref: /PHP-8.0/sapi/phpdbg/phpdbg_prompt.c (revision 3ec37a74)
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 <stdio.h>
20 #include <string.h>
21 #include "zend.h"
22 #include "zend_compile.h"
23 #include "zend_exceptions.h"
24 #include "zend_vm.h"
25 #include "zend_generators.h"
26 #include "zend_interfaces.h"
27 #include "zend_smart_str.h"
28 #include "phpdbg.h"
29 #include "phpdbg_io.h"
30 
31 #include "phpdbg_help.h"
32 #include "phpdbg_print.h"
33 #include "phpdbg_info.h"
34 #include "phpdbg_break.h"
35 #include "phpdbg_opcode.h"
36 #include "phpdbg_list.h"
37 #include "phpdbg_utils.h"
38 #include "phpdbg_prompt.h"
39 #include "phpdbg_cmd.h"
40 #include "phpdbg_set.h"
41 #include "phpdbg_frame.h"
42 #include "phpdbg_lexer.h"
43 #include "phpdbg_parser.h"
44 #include "phpdbg_wait.h"
45 #include "phpdbg_eol.h"
46 
47 #if ZEND_VM_KIND != ZEND_VM_KIND_CALL && ZEND_VM_KIND != ZEND_VM_KIND_HYBRID
48 #error "phpdbg can only be built with CALL zend vm kind"
49 #endif
50 
51 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
52 extern int phpdbg_startup_run;
53 
54 #ifdef HAVE_LIBDL
55 #ifdef PHP_WIN32
56 #include "win32/param.h"
57 #include "win32/winutil.h"
58 #define GET_DL_ERROR()  php_win_err()
59 #else
60 #include <sys/param.h>
61 #define GET_DL_ERROR()  DL_ERROR()
62 #endif
63 #endif
64 
65 /* {{{ command declarations */
66 const phpdbg_command_t phpdbg_prompt_commands[] = {
67 	PHPDBG_COMMAND_D(exec,      "set execution context",                    'e', NULL, "s", 0),
68 	PHPDBG_COMMAND_D(stdin,     "read script from stdin",                    0 , NULL, "s", 0),
69 	PHPDBG_COMMAND_D(step,      "step through execution",                   's', NULL, 0, PHPDBG_ASYNC_SAFE),
70 	PHPDBG_COMMAND_D(continue,  "continue execution",                       'c', NULL, 0, PHPDBG_ASYNC_SAFE),
71 	PHPDBG_COMMAND_D(run,       "attempt execution",                        'r', NULL, "|s", 0),
72 	PHPDBG_COMMAND_D(ev,        "evaluate some code",                        0 , NULL, "i", PHPDBG_ASYNC_SAFE), /* restricted ASYNC_SAFE */
73 	PHPDBG_COMMAND_D(until,     "continue past the current line",           'u', NULL, 0, 0),
74 	PHPDBG_COMMAND_D(finish,    "continue past the end of the stack",       'F', NULL, 0, 0),
75 	PHPDBG_COMMAND_D(leave,     "continue until the end of the stack",      'L', NULL, 0, 0),
76 	PHPDBG_COMMAND_D(generator, "inspect or switch to a generator",         'g', NULL, "|n", 0),
77 	PHPDBG_COMMAND_D(print,     "print something",                          'p', phpdbg_print_commands, "|*c", 0),
78 	PHPDBG_COMMAND_D(break,     "set breakpoint",                           'b', phpdbg_break_commands, "|*c", 0),
79 	PHPDBG_COMMAND_D(back,      "show trace",                               't', NULL, "|n", PHPDBG_ASYNC_SAFE),
80 	PHPDBG_COMMAND_D(frame,     "switch to a frame",                        'f', NULL, "|n", PHPDBG_ASYNC_SAFE),
81 	PHPDBG_COMMAND_D(list,      "lists some code",                          'l', phpdbg_list_commands,  "*", PHPDBG_ASYNC_SAFE),
82 	PHPDBG_COMMAND_D(info,      "displays some information",               'i', phpdbg_info_commands, "|s", PHPDBG_ASYNC_SAFE),
83 	PHPDBG_COMMAND_D(clean,     "clean the execution environment",          'X', NULL, 0, 0),
84 	PHPDBG_COMMAND_D(clear,     "clear breakpoints",                        'C', NULL, 0, 0),
85 	PHPDBG_COMMAND_D(help,      "show help menu",                           'h', phpdbg_help_commands, "|s", PHPDBG_ASYNC_SAFE),
86 	PHPDBG_COMMAND_D(set,       "set phpdbg configuration",                 'S', phpdbg_set_commands,   "s", PHPDBG_ASYNC_SAFE),
87 	PHPDBG_COMMAND_D(register,  "register a function",                      'R', NULL, "s", 0),
88 	PHPDBG_COMMAND_D(source,    "execute a phpdbginit",                     '<', NULL, "s", 0),
89 	PHPDBG_COMMAND_D(export,    "export breaks to a .phpdbginit script",    '>', NULL, "s", PHPDBG_ASYNC_SAFE),
90 	PHPDBG_COMMAND_D(sh,   	    "shell a command",                           0 , NULL, "i", 0),
91 	PHPDBG_COMMAND_D(quit,      "exit phpdbg",                              'q', NULL, 0, PHPDBG_ASYNC_SAFE),
92 	PHPDBG_COMMAND_D(wait,      "wait for other process",                   'W', NULL, 0, 0),
93 	PHPDBG_COMMAND_D(watch,     "set watchpoint",                           'w', phpdbg_watch_commands, "|ss", 0),
94 	PHPDBG_COMMAND_D(next,      "step over next line",                      'n', NULL, 0, PHPDBG_ASYNC_SAFE),
95 	PHPDBG_COMMAND_D(eol,       "set EOL",                                  'E', NULL, "|s", 0),
96 	PHPDBG_END_COMMAND
97 }; /* }}} */
98 
phpdbg_call_register(phpdbg_param_t * stack)99 static inline int phpdbg_call_register(phpdbg_param_t *stack) /* {{{ */
100 {
101 	phpdbg_param_t *name = NULL;
102 
103 	if (stack->type == STACK_PARAM) {
104 		char *lc_name;
105 
106 		name = stack->next;
107 
108 		if (!name || name->type != STR_PARAM) {
109 			return FAILURE;
110 		}
111 
112 		lc_name = zend_str_tolower_dup(name->str, name->len);
113 
114 		if (zend_hash_str_exists(&PHPDBG_G(registered), lc_name, name->len)) {
115 			zval fretval;
116 			zend_fcall_info fci;
117 
118 			memset(&fci, 0, sizeof(zend_fcall_info));
119 
120 			ZVAL_STRINGL(&fci.function_name, lc_name, name->len);
121 			fci.size = sizeof(zend_fcall_info);
122 			//???fci.symbol_table = zend_rebuild_symbol_table();
123 			fci.object = NULL;
124 			fci.retval = &fretval;
125 
126 			if (name->next) {
127 				zval params;
128 				phpdbg_param_t *next = name->next;
129 
130 				array_init(&params);
131 
132 				while (next) {
133 					char *buffered = NULL;
134 
135 					switch (next->type) {
136 						case OP_PARAM:
137 						case COND_PARAM:
138 						case STR_PARAM:
139 							add_next_index_stringl(&params, next->str, next->len);
140 						break;
141 
142 						case NUMERIC_PARAM:
143 							add_next_index_long(&params, next->num);
144 						break;
145 
146 						case METHOD_PARAM:
147 							spprintf(&buffered, 0, "%s::%s", next->method.class, next->method.name);
148 							add_next_index_string(&params, buffered);
149 						break;
150 
151 						case NUMERIC_METHOD_PARAM:
152 							spprintf(&buffered, 0, "%s::%s#%ld", next->method.class, next->method.name, next->num);
153 							add_next_index_string(&params, buffered);
154 						break;
155 
156 						case NUMERIC_FUNCTION_PARAM:
157 							spprintf(&buffered, 0, "%s#%ld", next->str, next->num);
158 							add_next_index_string(&params, buffered);
159 						break;
160 
161 						case FILE_PARAM:
162 							spprintf(&buffered, 0, "%s:%ld", next->file.name, next->file.line);
163 							add_next_index_string(&params, buffered);
164 						break;
165 
166 						case NUMERIC_FILE_PARAM:
167 							spprintf(&buffered, 0, "%s:#%ld", next->file.name, next->file.line);
168 							add_next_index_string(&params, buffered);
169 						break;
170 
171 						default: {
172 							/* not yet */
173 						}
174 					}
175 
176 					next = next->next;
177 				}
178 
179 				zend_fcall_info_args(&fci, &params);
180 			} else {
181 				fci.params = NULL;
182 				fci.param_count = 0;
183 			}
184 
185 			phpdbg_activate_err_buf(0);
186 			phpdbg_free_err_buf();
187 
188 			phpdbg_debug("created %d params from arguments", fci.param_count);
189 
190 			if (zend_call_function(&fci, NULL) == SUCCESS) {
191 				zend_print_zval_r(&fretval, 0);
192 				phpdbg_out("\n");
193 				zval_ptr_dtor(&fretval);
194 			}
195 
196 			zval_ptr_dtor_str(&fci.function_name);
197 			efree(lc_name);
198 
199 			return SUCCESS;
200 		}
201 
202 		efree(lc_name);
203 	}
204 
205 	return FAILURE;
206 } /* }}} */
207 
208 struct phpdbg_init_state {
209 	int line;
210 	zend_bool in_code;
211 	char *code;
212 	size_t code_len;
213 	const char *init_file;
214 };
215 
phpdbg_line_init(char * cmd,struct phpdbg_init_state * state)216 static void phpdbg_line_init(char *cmd, struct phpdbg_init_state *state) {
217 	size_t cmd_len = strlen(cmd);
218 
219 	state->line++;
220 
221 	while (cmd_len > 0L && isspace(cmd[cmd_len-1])) {
222 		cmd_len--;
223 	}
224 
225 	cmd[cmd_len] = '\0';
226 
227 	if (*cmd && cmd_len > 0L && cmd[0] != '#') {
228 		if (cmd_len == 2) {
229 			if (memcmp(cmd, "<:", sizeof("<:")-1) == SUCCESS) {
230 				state->in_code = 1;
231 				return;
232 			} else {
233 				if (memcmp(cmd, ":>", sizeof(":>")-1) == SUCCESS) {
234 					state->in_code = 0;
235 					state->code[state->code_len] = '\0';
236 					zend_eval_stringl(state->code, state->code_len, NULL, "phpdbginit code");
237 					free(state->code);
238 					state->code = NULL;
239 					return;
240 				}
241 			}
242 		}
243 
244 		if (state->in_code) {
245 			if (state->code == NULL) {
246 				state->code = malloc(cmd_len + 1);
247 			} else {
248 				state->code = realloc(state->code, state->code_len + cmd_len + 1);
249 			}
250 
251 			if (state->code) {
252 				memcpy(&state->code[state->code_len], cmd, cmd_len);
253 				state->code_len += cmd_len;
254 			}
255 
256 			return;
257 		}
258 
259 		zend_try {
260 			char *input = phpdbg_read_input(cmd);
261 			phpdbg_param_t stack;
262 
263 			phpdbg_init_param(&stack, STACK_PARAM);
264 
265 			phpdbg_activate_err_buf(1);
266 
267 			if (phpdbg_do_parse(&stack, input) <= 0) {
268 				switch (phpdbg_stack_execute(&stack, 1 /* allow_async_unsafe == 1 */)) {
269 					case FAILURE:
270 						phpdbg_activate_err_buf(0);
271 						if (phpdbg_call_register(&stack) == FAILURE) {
272 							if (state->init_file) {
273 								phpdbg_output_err_buf("initfailure", "%b file=\"%s\" line=\"%d\" input=\"%s\"", "Unrecognized command in %s:%d: %s, %b!", state->init_file, state->line, input);
274 							} else {
275 								phpdbg_output_err_buf("initfailure", "%b line=\"%d\" input=\"%s\"", "Unrecognized command on line %d: %s, %b!", state->line, input);
276 							}
277 						}
278 					break;
279 				}
280 			}
281 
282 			phpdbg_activate_err_buf(0);
283 			phpdbg_free_err_buf();
284 
285 			phpdbg_stack_free(&stack);
286 			phpdbg_destroy_input(&input);
287 		} zend_catch {
288 			PHPDBG_G(flags) &= ~(PHPDBG_IS_RUNNING | PHPDBG_IS_CLEANING);
289 			if (PHPDBG_G(flags) & PHPDBG_IS_QUITTING) {
290 				zend_bailout();
291 			}
292 		} zend_end_try();
293 	}
294 
295 }
296 
phpdbg_string_init(char * buffer)297 void phpdbg_string_init(char *buffer) {
298 	struct phpdbg_init_state state = {0};
299 	char *str = strtok(buffer, "\n");
300 
301 	while (str) {
302 		phpdbg_line_init(str, &state);
303 
304 		str = strtok(NULL, "\n");
305 	}
306 
307 	if (state.code) {
308 		free(state.code);
309 	}
310 }
311 
phpdbg_try_file_init(char * init_file,size_t init_file_len,zend_bool free_init)312 void phpdbg_try_file_init(char *init_file, size_t init_file_len, zend_bool free_init) /* {{{ */
313 {
314 	zend_stat_t sb;
315 
316 	if (init_file && VCWD_STAT(init_file, &sb) != -1) {
317 		FILE *fp = fopen(init_file, "r");
318 		if (fp) {
319 			char cmd[PHPDBG_MAX_CMD];
320 			struct phpdbg_init_state state = {0};
321 
322 			state.init_file = init_file;
323 
324 			while (fgets(cmd, PHPDBG_MAX_CMD, fp) != NULL) {
325 				phpdbg_line_init(cmd, &state);
326 			}
327 
328 			if (state.code) {
329 				free(state.code);
330 			}
331 
332 			fclose(fp);
333 		} else {
334 			phpdbg_error("initfailure", "type=\"openfile\" file=\"%s\"", "Failed to open %s for initialization", init_file);
335 		}
336 
337 		if (free_init) {
338 			free(init_file);
339 		}
340 	}
341 } /* }}} */
342 
phpdbg_init(char * init_file,size_t init_file_len,zend_bool use_default)343 void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default) /* {{{ */
344 {
345 	if (init_file) {
346 		phpdbg_try_file_init(init_file, init_file_len, 1);
347 	} else if (use_default) {
348 		char *scan_dir = getenv("PHP_INI_SCAN_DIR");
349 		char *sys_ini;
350 		int i;
351 
352 		ZEND_IGNORE_VALUE(asprintf(&sys_ini, "%s/" PHPDBG_INIT_FILENAME, PHP_CONFIG_FILE_PATH));
353 		phpdbg_try_file_init(sys_ini, strlen(sys_ini), 0);
354 		free(sys_ini);
355 
356 		if (!scan_dir) {
357 			scan_dir = PHP_CONFIG_FILE_SCAN_DIR;
358 		}
359 		while (*scan_dir != 0) {
360 			i = 0;
361 			while (scan_dir[i] != ':') {
362 				if (scan_dir[i++] == 0) {
363 					i = -1;
364 					break;
365 				}
366 			}
367 			if (i != -1) {
368 				scan_dir[i] = 0;
369 			}
370 
371 			ZEND_IGNORE_VALUE(asprintf(&init_file, "%s/%s", scan_dir, PHPDBG_INIT_FILENAME));
372 			phpdbg_try_file_init(init_file, strlen(init_file), 1);
373 			free(init_file);
374 			if (i == -1) {
375 				break;
376 			}
377 			scan_dir += i + 1;
378 		}
379 
380 		phpdbg_try_file_init(PHPDBG_STRL(PHPDBG_INIT_FILENAME), 0);
381 	}
382 }
383 /* }}} */
384 
phpdbg_clean(zend_bool full,zend_bool resubmit)385 void phpdbg_clean(zend_bool full, zend_bool resubmit) /* {{{ */
386 {
387 	/* this is implicitly required */
388 	if (PHPDBG_G(ops)) {
389 		destroy_op_array(PHPDBG_G(ops));
390 		efree(PHPDBG_G(ops));
391 		PHPDBG_G(ops) = NULL;
392 	}
393 
394 	if (!resubmit && PHPDBG_G(cur_command)) {
395 		free(PHPDBG_G(cur_command));
396 		PHPDBG_G(cur_command) = NULL;
397 	}
398 
399 	if (full) {
400 		PHPDBG_G(flags) |= PHPDBG_IS_CLEANING;
401 	}
402 } /* }}} */
403 
PHPDBG_COMMAND(exec)404 PHPDBG_COMMAND(exec) /* {{{ */
405 {
406 	zend_stat_t sb;
407 
408 	if (VCWD_STAT(param->str, &sb) != FAILURE) {
409 		if (sb.st_mode & (S_IFREG|S_IFLNK)) {
410 			char *res = phpdbg_resolve_path(param->str);
411 			size_t res_len = strlen(res);
412 
413 			if ((res_len != PHPDBG_G(exec_len)) || (memcmp(res, PHPDBG_G(exec), res_len) != SUCCESS)) {
414 				if (PHPDBG_G(in_execution)) {
415 					if (phpdbg_ask_user_permission("Do you really want to stop execution to set a new execution context?") == FAILURE) {
416 						return FAILURE;
417 					}
418 				}
419 
420 				if (PHPDBG_G(exec)) {
421 					phpdbg_notice("exec", "type=\"unset\" context=\"%s\"", "Unsetting old execution context: %s", PHPDBG_G(exec));
422 					free(PHPDBG_G(exec));
423 					PHPDBG_G(exec) = NULL;
424 					PHPDBG_G(exec_len) = 0L;
425 				}
426 
427 				if (PHPDBG_G(ops)) {
428 					phpdbg_notice("exec", "type=\"unsetops\"", "Destroying compiled opcodes");
429 					phpdbg_clean(0, 0);
430 				}
431 
432 				PHPDBG_G(exec) = res;
433 				PHPDBG_G(exec_len) = res_len;
434 
435 				VCWD_CHDIR_FILE(res);
436 
437 				*SG(request_info).argv = estrndup(PHPDBG_G(exec), PHPDBG_G(exec_len));
438 				php_build_argv(NULL, &PG(http_globals)[TRACK_VARS_SERVER]);
439 
440 				phpdbg_notice("exec", "type=\"set\" context=\"%s\"", "Set execution context: %s", PHPDBG_G(exec));
441 
442 				if (PHPDBG_G(in_execution)) {
443 					phpdbg_clean(1, 0);
444 					return SUCCESS;
445 				}
446 
447 				phpdbg_compile();
448 			} else {
449 				phpdbg_notice("exec", "type=\"unchanged\"", "Execution context not changed");
450 			}
451 		} else {
452 			phpdbg_error("exec", "type=\"invalid\" context=\"%s\"", "Cannot use %s as execution context, not a valid file or symlink", param->str);
453 		}
454 	} else {
455 		phpdbg_error("exec", "type=\"notfound\" context=\"%s\"", "Cannot stat %s, ensure the file exists", param->str);
456 	}
457 	return SUCCESS;
458 } /* }}} */
459 
PHPDBG_COMMAND(stdin)460 PHPDBG_COMMAND(stdin)
461 {
462 	smart_str code = {0};
463 	char *buf;
464 	char *sep = param->str;
465 	int seplen = param->len;
466 	int bytes = 0;
467 
468 	smart_str_appends(&code, "?>");
469 
470 	do {
471 		PHPDBG_G(input_buflen) += bytes;
472 		if (PHPDBG_G(input_buflen) <= 0) {
473 			continue;
474 		}
475 
476 		if (sep && seplen) {
477 			char *nl = buf = PHPDBG_G(input_buffer);
478 			do {
479 				if (buf == nl + seplen) {
480 					if (!memcmp(sep, nl, seplen) && (*buf == '\n' || (*buf == '\r' && buf[1] == '\n'))) {
481 						smart_str_appendl(&code, PHPDBG_G(input_buffer), nl - PHPDBG_G(input_buffer));
482 						memmove(PHPDBG_G(input_buffer), ++buf, --PHPDBG_G(input_buflen));
483 						goto exec_code;
484 					}
485 				}
486 				if (*buf == '\n') {
487 					nl = buf + 1;
488 				}
489 				buf++;
490 			} while (--PHPDBG_G(input_buflen));
491 			if (buf != nl && buf <= nl + seplen) {
492 				smart_str_appendl(&code, PHPDBG_G(input_buffer), nl - PHPDBG_G(input_buffer));
493 				PHPDBG_G(input_buflen) = buf - nl;
494 				memmove(PHPDBG_G(input_buffer), nl, PHPDBG_G(input_buflen));
495 			} else {
496 				PHPDBG_G(input_buflen) = 0;
497 				smart_str_appendl(&code, PHPDBG_G(input_buffer), buf - PHPDBG_G(input_buffer));
498 			}
499 		} else {
500 			smart_str_appendl(&code, PHPDBG_G(input_buffer), PHPDBG_G(input_buflen));
501 			PHPDBG_G(input_buflen) = 0;
502 		}
503 	} while ((bytes = phpdbg_mixed_read(PHPDBG_G(io)[PHPDBG_STDIN].fd, PHPDBG_G(input_buffer) + PHPDBG_G(input_buflen), PHPDBG_MAX_CMD - PHPDBG_G(input_buflen), -1)) > 0);
504 
505 	if (bytes < 0) {
506 		PHPDBG_G(flags) |= PHPDBG_IS_QUITTING | PHPDBG_IS_DISCONNECTED;
507 		zend_bailout();
508 	}
509 
510 exec_code:
511 	smart_str_0(&code);
512 
513 	if (phpdbg_compile_stdin(code.s) == FAILURE) {
514 		zend_exception_error(EG(exception), E_ERROR);
515 		zend_bailout();
516 	}
517 
518 	return SUCCESS;
519 } /* }}} */
520 
phpdbg_compile_stdin(zend_string * code)521 int phpdbg_compile_stdin(zend_string *code) {
522 	PHPDBG_G(ops) = zend_compile_string(code, "Standard input code");
523 	zend_string_release(code);
524 
525 	if (EG(exception)) {
526 		return FAILURE;
527 	}
528 
529 	if (PHPDBG_G(exec)) {
530 		free(PHPDBG_G(exec));
531 	}
532 	PHPDBG_G(exec) = strdup("Standard input code");
533 	PHPDBG_G(exec_len) = sizeof("Standard input code") - 1;
534 	{ /* remove leading ?> from source */
535 		int i;
536 		/* remove trailing data after zero byte, used for avoiding conflicts in eval()'ed code snippets */
537 		zend_string *source_path = strpprintf(0, "Standard input code%c%p", 0, PHPDBG_G(ops)->opcodes);
538 		phpdbg_file_source *data = zend_hash_find_ptr(&PHPDBG_G(file_sources), source_path);
539 		dtor_func_t dtor = PHPDBG_G(file_sources).pDestructor;
540 		PHPDBG_G(file_sources).pDestructor = NULL;
541 		zend_hash_del(&PHPDBG_G(file_sources), source_path);
542 		PHPDBG_G(file_sources).pDestructor = dtor;
543 		zend_hash_str_update_ptr(&PHPDBG_G(file_sources), "Standard input code", sizeof("Standard input code")-1, data);
544 		zend_string_release(source_path);
545 
546 		for (i = 1; i <= data->lines; i++) {
547 			data->line[i] -= 2;
548 		}
549 		data->len -= 2;
550 		memmove(data->buf, data->buf + 2, data->len);
551 	}
552 
553 	phpdbg_notice("compile", "context=\"Standard input code\"", "Successful compilation of stdin input");
554 
555 	return SUCCESS;
556 }
557 
phpdbg_compile(void)558 int phpdbg_compile(void) /* {{{ */
559 {
560 	zend_file_handle fh;
561 	char *buf;
562 	size_t len;
563 
564 	if (!PHPDBG_G(exec)) {
565 		phpdbg_error("inactive", "type=\"nocontext\"", "No execution context");
566 		return FAILURE;
567 	}
568 
569 	if (php_stream_open_for_zend_ex(PHPDBG_G(exec), &fh, USE_PATH|STREAM_OPEN_FOR_INCLUDE) == SUCCESS && zend_stream_fixup(&fh, &buf, &len) == SUCCESS) {
570 		CG(skip_shebang) = 1;
571 		PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE);
572 		zend_destroy_file_handle(&fh);
573 		if (EG(exception)) {
574 			zend_exception_error(EG(exception), E_ERROR);
575 			zend_bailout();
576 		}
577 
578 		phpdbg_notice("compile", "context=\"%s\"", "Successful compilation of %s", PHPDBG_G(exec));
579 
580 		return SUCCESS;
581 	} else {
582 		phpdbg_error("compile", "type=\"openfailure\" context=\"%s\"", "Could not open file %s", PHPDBG_G(exec));
583 	}
584 
585 	return FAILURE;
586 } /* }}} */
587 
PHPDBG_COMMAND(step)588 PHPDBG_COMMAND(step) /* {{{ */
589 {
590 	if (PHPDBG_G(in_execution)) {
591 		PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
592 	}
593 
594 	return PHPDBG_NEXT;
595 } /* }}} */
596 
PHPDBG_COMMAND(continue)597 PHPDBG_COMMAND(continue) /* {{{ */
598 {
599 	return PHPDBG_NEXT;
600 } /* }}} */
601 
phpdbg_skip_line_helper()602 int phpdbg_skip_line_helper() /* {{{ */ {
603 	zend_execute_data *ex = phpdbg_user_execute_data(EG(current_execute_data));
604 	const zend_op_array *op_array = &ex->func->op_array;
605 	const zend_op *opline = op_array->opcodes;
606 
607 	PHPDBG_G(flags) |= PHPDBG_IN_UNTIL;
608 	PHPDBG_G(seek_ex) = ex;
609 	do {
610 		if (opline->lineno != ex->opline->lineno
611 		 || opline->opcode == ZEND_RETURN
612 		 || opline->opcode == ZEND_FAST_RET
613 		 || opline->opcode == ZEND_GENERATOR_RETURN
614 		 || opline->opcode == ZEND_EXIT
615 		 || opline->opcode == ZEND_YIELD
616 		 || opline->opcode == ZEND_YIELD_FROM
617 		) {
618 			zend_hash_index_update_ptr(&PHPDBG_G(seek), (zend_ulong) opline, (void *) opline);
619 		}
620 	} while (++opline < op_array->opcodes + op_array->last);
621 
622 	return PHPDBG_UNTIL;
623 }
624 /* }}} */
625 
PHPDBG_COMMAND(until)626 PHPDBG_COMMAND(until) /* {{{ */
627 {
628 	if (!PHPDBG_G(in_execution)) {
629 		phpdbg_error("inactive", "type=\"noexec\"", "Not executing");
630 		return SUCCESS;
631 	}
632 
633 	return phpdbg_skip_line_helper();
634 } /* }}} */
635 
PHPDBG_COMMAND(next)636 PHPDBG_COMMAND(next) /* {{{ */
637 {
638 	if (!PHPDBG_G(in_execution)) {
639 		phpdbg_error("inactive", "type=\"noexec\"", "Not executing");
640 		return SUCCESS;
641 	}
642 
643 	PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
644 	return phpdbg_skip_line_helper();
645 } /* }}} */
646 
phpdbg_seek_to_end(void)647 static void phpdbg_seek_to_end(void) /* {{{ */ {
648 	zend_execute_data *ex = phpdbg_user_execute_data(EG(current_execute_data));
649 	const zend_op_array *op_array = &ex->func->op_array;
650 	const zend_op *opline = op_array->opcodes;
651 
652 	PHPDBG_G(seek_ex) = ex;
653 	do {
654 		switch (opline->opcode) {
655 			case ZEND_RETURN:
656 			case ZEND_FAST_RET:
657 			case ZEND_GENERATOR_RETURN:
658 			case ZEND_EXIT:
659 			case ZEND_YIELD:
660 			case ZEND_YIELD_FROM:
661 				zend_hash_index_update_ptr(&PHPDBG_G(seek), (zend_ulong) opline, (void *) opline);
662 		}
663 	} while (++opline < op_array->opcodes + op_array->last);
664 }
665 /* }}} */
666 
PHPDBG_COMMAND(finish)667 PHPDBG_COMMAND(finish) /* {{{ */
668 {
669 	if (!PHPDBG_G(in_execution)) {
670 		phpdbg_error("inactive", "type=\"noexec\"", "Not executing");
671 		return SUCCESS;
672 	}
673 
674 	phpdbg_seek_to_end();
675 	if (zend_hash_index_exists(&PHPDBG_G(seek), (zend_ulong) phpdbg_user_execute_data(EG(current_execute_data))->opline)) {
676 		zend_hash_clean(&PHPDBG_G(seek));
677 	} else {
678 		PHPDBG_G(flags) |= PHPDBG_IN_FINISH;
679 	}
680 
681 	return PHPDBG_FINISH;
682 } /* }}} */
683 
PHPDBG_COMMAND(leave)684 PHPDBG_COMMAND(leave) /* {{{ */
685 {
686 	if (!PHPDBG_G(in_execution)) {
687 		phpdbg_error("inactive", "type=\"noexec\"", "Not executing");
688 		return SUCCESS;
689 	}
690 
691 	phpdbg_seek_to_end();
692 	if (zend_hash_index_exists(&PHPDBG_G(seek), (zend_ulong) phpdbg_user_execute_data(EG(current_execute_data))->opline)) {
693 		zend_hash_clean(&PHPDBG_G(seek));
694 		phpdbg_notice("leave", "type=\"end\"", "Already at the end of the function");
695 		return SUCCESS;
696 	} else {
697 		PHPDBG_G(flags) |= PHPDBG_IN_LEAVE;
698 		return PHPDBG_LEAVE;
699 	}
700 } /* }}} */
701 
PHPDBG_COMMAND(frame)702 PHPDBG_COMMAND(frame) /* {{{ */
703 {
704 	if (!param) {
705 		phpdbg_notice("frame", "id=\"%d\"", "Currently in frame #%d", PHPDBG_G(frame).num);
706 	} else {
707 		phpdbg_switch_frame(param->num);
708 	}
709 
710 	return SUCCESS;
711 } /* }}} */
712 
phpdbg_handle_exception(void)713 static inline void phpdbg_handle_exception(void) /* {{{ */
714 {
715 	zend_object *ex = EG(exception);
716 	zend_string *msg, *file;
717 	zend_long line;
718 	zval rv, tmp;
719 
720 	EG(exception) = NULL;
721 
722 	zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
723 	file = zval_get_string(zend_read_property(zend_get_exception_base(ex), ex, ZEND_STRL("file"), 1, &rv));
724 	line = zval_get_long(zend_read_property(zend_get_exception_base(ex), ex, ZEND_STRL("line"), 1, &rv));
725 
726 	if (EG(exception)) {
727 		EG(exception) = NULL;
728 		msg = ZSTR_EMPTY_ALLOC();
729 	} else {
730 		zend_update_property_string(zend_get_exception_base(ex), ex, ZEND_STRL("string"), Z_STRVAL(tmp));
731 		zval_ptr_dtor(&tmp);
732 		msg = zval_get_string(zend_read_property(zend_get_exception_base(ex), ex, ZEND_STRL("string"), 1, &rv));
733 	}
734 
735 	phpdbg_error("exception", "name=\"%s\" file=\"%s\" line=\"" ZEND_LONG_FMT "\"", "Uncaught %s in %s on line " ZEND_LONG_FMT, ZSTR_VAL(ex->ce->name), ZSTR_VAL(file), line);
736 	zend_string_release(file);
737 	phpdbg_writeln("exceptionmsg", "msg=\"%s\"", "%s", ZSTR_VAL(msg));
738 	zend_string_release(msg);
739 
740 	if (EG(prev_exception)) {
741 		OBJ_RELEASE(EG(prev_exception));
742 		EG(prev_exception) = 0;
743 	}
744 	OBJ_RELEASE(ex);
745 	EG(opline_before_exception) = NULL;
746 
747 	EG(exit_status) = 255;
748 } /* }}} */
749 
PHPDBG_COMMAND(run)750 PHPDBG_COMMAND(run) /* {{{ */
751 {
752 	if (PHPDBG_G(ops) || PHPDBG_G(exec)) {
753 		zend_execute_data *ex = EG(current_execute_data);
754 		zend_bool restore = 1;
755 
756 		if (PHPDBG_G(in_execution)) {
757 			if (phpdbg_ask_user_permission("Do you really want to restart execution?") == SUCCESS) {
758 				phpdbg_startup_run++;
759 				phpdbg_clean(1, 1);
760 			}
761 			return SUCCESS;
762 		}
763 
764 		if (!PHPDBG_G(ops)) {
765 			if (phpdbg_compile() == FAILURE) {
766 				phpdbg_error("compile", "type=\"compilefailure\" context=\"%s\"", "Failed to compile %s, cannot run", PHPDBG_G(exec));
767 				EG(exit_status) = FAILURE;
768 				goto out;
769 			}
770 		}
771 
772 		if (param && param->type != EMPTY_PARAM && param->len != 0) {
773 			char **argv = emalloc(5 * sizeof(char *));
774 			char *end = param->str + param->len, *p = param->str;
775 			char last_byte;
776 			int argc = 0;
777 			int i;
778 
779 			while (*end == '\r' || *end == '\n') *(end--) = 0;
780 			last_byte = end[1];
781 			end[1] = 0;
782 
783 			while (*p == ' ') p++;
784 			while (*p) {
785 				char sep = ' ';
786 				char *buf = emalloc(end - p + 2), *q = buf;
787 
788 				if (*p == '<') {
789 					/* use as STDIN */
790 					do p++; while (*p == ' ');
791 
792 					if (*p == '\'' || *p == '"') {
793 						sep = *(p++);
794 					}
795 					while (*p && *p != sep) {
796 						if (*p == '\\' && (p[1] == sep || p[1] == '\\')) {
797 							p++;
798 						}
799 						*(q++) = *(p++);
800 					}
801 					*(q++) = 0;
802 					if (*p) {
803 						do p++; while (*p == ' ');
804 					}
805 
806 					if (*p) {
807 						phpdbg_error("cmd", "", "Invalid run command, cannot put further arguments after stdin");
808 						goto free_cmd;
809 					}
810 
811 					PHPDBG_G(stdin_file) = fopen(buf, "r");
812 					if (PHPDBG_G(stdin_file) == NULL) {
813 						phpdbg_error("stdin", "path=\"%s\"", "Could not open '%s' for reading from stdin", buf);
814 						goto free_cmd;
815 					}
816 					efree(buf);
817 					phpdbg_register_file_handles();
818 					break;
819 				}
820 
821 				if (argc >= 4 && argc == (argc & -argc)) {
822 					argv = erealloc(argv, (argc * 2 + 1) * sizeof(char *));
823 				}
824 
825 				if (*p == '\'' || *p == '"') {
826 					sep = *(p++);
827 				}
828 				if (*p == '\\' && (p[1] == '<' || p[1] == '\'' || p[1] == '"')) {
829 					p++;
830 				}
831 				while (*p && *p != sep) {
832 					if (*p == '\\' && (p[1] == sep || p[1] == '\\' || (p[1] == '#' && sep == ' '))) {
833 						p++;
834 					}
835 					*(q++) = *(p++);
836 				}
837 				if (!*p && sep != ' ') {
838 					phpdbg_error("cmd", "", "Invalid run command, unterminated escape sequence");
839 free_cmd:
840 					efree(buf);
841 					for (i = 0; i < argc; i++) {
842 						efree(argv[i]);
843 					}
844 					efree(argv);
845 					end[1] = last_byte;
846 					return SUCCESS;
847 				}
848 
849 				*(q++) = 0;
850 				argv[++argc] = erealloc(buf, q - buf);
851 
852 				if (*p) {
853 					do p++; while (*p == ' ');
854 				}
855 			}
856 			end[1] = last_byte;
857 
858 			argv[0] = SG(request_info).argv[0];
859 			for (i = SG(request_info).argc; --i;) {
860 				efree(SG(request_info).argv[i]);
861 			}
862 			efree(SG(request_info).argv);
863 			SG(request_info).argv = erealloc(argv, ++argc * sizeof(char *));
864 			SG(request_info).argc = argc;
865 
866 			php_build_argv(NULL, &PG(http_globals)[TRACK_VARS_SERVER]);
867 		}
868 
869 		/* clean up from last execution */
870 		if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
871 			zend_hash_clean(ex->symbol_table);
872 		} else {
873 			zend_rebuild_symbol_table();
874 		}
875 		PHPDBG_G(handled_exception) = NULL;
876 
877 		/* clean seek state */
878 		PHPDBG_G(flags) &= ~PHPDBG_SEEK_MASK;
879 		zend_hash_clean(&PHPDBG_G(seek));
880 
881 		/* reset hit counters */
882 		phpdbg_reset_breakpoints();
883 
884 		zend_try {
885 			PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
886 			PHPDBG_G(flags) |= PHPDBG_IS_RUNNING;
887 			zend_execute(PHPDBG_G(ops), &PHPDBG_G(retval));
888 			PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
889 		} zend_catch {
890 			PHPDBG_G(in_execution) = 0;
891 
892 			if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
893 				restore = 0;
894 			} else {
895 				zend_bailout();
896 			}
897 		} zend_end_try();
898 
899 		if (PHPDBG_G(socket_fd) != -1) {
900 			close(PHPDBG_G(socket_fd));
901 			PHPDBG_G(socket_fd) = -1;
902 		}
903 
904 		if (restore) {
905 			zend_exception_restore();
906 			zend_try {
907 				zend_try_exception_handler();
908 				PHPDBG_G(in_execution) = 1;
909 			} zend_catch {
910 				PHPDBG_G(in_execution) = 0;
911 
912 				if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
913 					zend_bailout();
914 				}
915 			} zend_end_try();
916 
917 			if (EG(exception)) {
918 				phpdbg_handle_exception();
919 			}
920 		}
921 
922 		PHPDBG_G(flags) &= ~PHPDBG_IS_RUNNING;
923 
924 		phpdbg_clean(1, 0);
925 	} else {
926 		phpdbg_error("inactive", "type=\"nocontext\"", "Nothing to execute!");
927 	}
928 
929 out:
930 	PHPDBG_FRAME(num) = 0;
931 	return SUCCESS;
932 } /* }}} */
933 
phpdbg_output_ev_variable(char * name,size_t len,char * keyname,size_t keylen,HashTable * parent,zval * zv)934 int phpdbg_output_ev_variable(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv) /* {{{ */ {
935 	phpdbg_notice("eval", "variable=\"%.*s\"", "Printing variable %.*s", (int) len, name);
936 	phpdbg_xml("<eval %r>");
937 	zend_print_zval_r(zv, 0);
938 	phpdbg_xml("</eval>");
939 	phpdbg_out("\n");
940 
941 	efree(name);
942 	efree(keyname);
943 
944 	return SUCCESS;
945 }
946 /* }}} */
947 
PHPDBG_COMMAND(ev)948 PHPDBG_COMMAND(ev) /* {{{ */
949 {
950 	zend_bool stepping = ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING) == PHPDBG_IS_STEPPING);
951 	zval retval;
952 
953 	zend_execute_data *original_execute_data = EG(current_execute_data);
954 	zend_vm_stack original_stack = EG(vm_stack);
955 	zend_object *ex = NULL;
956 
957 	PHPDBG_OUTPUT_BACKUP();
958 
959 	original_stack->top = EG(vm_stack_top);
960 
961 	if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
962 		phpdbg_try_access {
963 			phpdbg_parse_variable(param->str, param->len, &EG(symbol_table), 0, phpdbg_output_ev_variable, 0);
964 		} phpdbg_catch_access {
965 			phpdbg_error("signalsegv", "", "Could not fetch data, invalid data source");
966 		} phpdbg_end_try_access();
967 
968 		PHPDBG_OUTPUT_BACKUP_RESTORE();
969 		return SUCCESS;
970 	}
971 
972 	if (!(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
973 		PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
974 	}
975 
976 	/* disable stepping while eval() in progress */
977 	PHPDBG_G(flags) |= PHPDBG_IN_EVAL;
978 	zend_try {
979 		if (zend_eval_stringl(param->str, param->len, &retval, "eval()'d code") == SUCCESS) {
980 			if (EG(exception)) {
981 				ex = EG(exception);
982 				zend_exception_error(EG(exception), E_ERROR);
983 			} else {
984 				phpdbg_xml("<eval %r>");
985 				if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
986 					zval *zvp = &retval;
987 					phpdbg_xml_var_dump(zvp);
988 				}
989 				zend_print_zval_r(&retval, 0);
990 				phpdbg_xml("</eval>");
991 				phpdbg_out("\n");
992 				zval_ptr_dtor(&retval);
993 			}
994 		}
995 	} zend_catch {
996 		PHPDBG_G(unclean_eval) = 1;
997 		if (ex) {
998 			OBJ_RELEASE(ex);
999 		}
1000 		EG(current_execute_data) = original_execute_data;
1001 		EG(vm_stack_top) = original_stack->top;
1002 		EG(vm_stack_end) = original_stack->end;
1003 		EG(vm_stack) = original_stack;
1004 		EG(exit_status) = 0;
1005 	} zend_end_try();
1006 
1007 	PHPDBG_G(flags) &= ~PHPDBG_IN_EVAL;
1008 
1009 	/* switch stepping back on */
1010 	if (stepping && !(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
1011 		PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
1012 	}
1013 
1014 	CG(unclean_shutdown) = 0;
1015 
1016 	PHPDBG_OUTPUT_BACKUP_RESTORE();
1017 
1018 	return SUCCESS;
1019 } /* }}} */
1020 
PHPDBG_COMMAND(back)1021 PHPDBG_COMMAND(back) /* {{{ */
1022 {
1023 	if (!PHPDBG_G(in_execution)) {
1024 		phpdbg_error("inactive", "type=\"noexec\"", "Not executing!");
1025 		return SUCCESS;
1026 	}
1027 
1028 	if (!param) {
1029 		phpdbg_dump_backtrace(0);
1030 	} else {
1031 		phpdbg_dump_backtrace(param->num);
1032 	}
1033 
1034 	return SUCCESS;
1035 } /* }}} */
1036 
PHPDBG_COMMAND(generator)1037 PHPDBG_COMMAND(generator) /* {{{ */
1038 {
1039 	int i;
1040 
1041 	if (!PHPDBG_G(in_execution)) {
1042 		phpdbg_error("inactive", "type=\"noexec\"", "Not executing!");
1043 		return SUCCESS;
1044 	}
1045 
1046 	if (param) {
1047 		i = param->num;
1048 		zend_object **obj = EG(objects_store).object_buckets + i;
1049 		if (i < EG(objects_store).top && *obj && IS_OBJ_VALID(*obj) && (*obj)->ce == zend_ce_generator) {
1050 			zend_generator *gen = (zend_generator *) *obj;
1051 			if (gen->execute_data) {
1052 				if (zend_generator_get_current(gen)->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
1053 					phpdbg_error("generator", "type=\"running\"", "Generator currently running");
1054 				} else {
1055 					phpdbg_open_generator_frame(gen);
1056 				}
1057 			} else {
1058 				phpdbg_error("generator", "type=\"closed\"", "Generator already closed");
1059 			}
1060 		} else {
1061 			phpdbg_error("invalidarg", "", "Invalid object handle");
1062 		}
1063 	} else {
1064 		for (i = 0; i < EG(objects_store).top; i++) {
1065 			zend_object *obj = EG(objects_store).object_buckets[i];
1066 			if (obj && IS_OBJ_VALID(obj) && obj->ce == zend_ce_generator) {
1067 				zend_generator *gen = (zend_generator *) obj, *current = zend_generator_get_current(gen);
1068 				if (gen->execute_data) {
1069 					zend_string *s = phpdbg_compile_stackframe(gen->execute_data);
1070 					phpdbg_out("#%d: %.*s", i, (int) ZSTR_LEN(s), ZSTR_VAL(s));
1071 					zend_string_release(s);
1072 					if (gen != current) {
1073 						if (gen->node.parent != current) {
1074 							phpdbg_out(" with direct parent #%d and", gen->node.parent->std.handle);
1075 						}
1076 						phpdbg_out(" executing #%d currently", current->std.handle);
1077 					}
1078 					phpdbg_out("\n");
1079 				}
1080 			}
1081 		}
1082 	}
1083 
1084 	return SUCCESS;
1085 } /* }}} */
1086 
PHPDBG_COMMAND(print)1087 PHPDBG_COMMAND(print) /* {{{ */
1088 {
1089 	if (!param || param->type == EMPTY_PARAM) {
1090 		return phpdbg_do_print_stack(param);
1091 	} else switch (param->type) {
1092 		case STR_PARAM:
1093 			return phpdbg_do_print_func(param);
1094 		case METHOD_PARAM:
1095 			return phpdbg_do_print_method(param);
1096 		default:
1097 			phpdbg_error("print", "type=\"invalidarg\"", "Invalid arguments to print, expected nothing, function name or method name");
1098 			return SUCCESS;
1099 	}
1100 } /* }}} */
1101 
PHPDBG_COMMAND(info)1102 PHPDBG_COMMAND(info) /* {{{ */
1103 {
1104 	phpdbg_out("Execution Context Information\n\n");
1105 	phpdbg_xml("<printinfo %r>");
1106 #ifdef HAVE_PHPDBG_READLINE
1107 # ifdef HAVE_LIBREADLINE
1108 	 phpdbg_writeln("info", "readline=\"yes\"", "Readline   yes");
1109 # else
1110 	 phpdbg_writeln("info", "readline=\"no\"", "Readline   no");
1111 # endif
1112 # ifdef HAVE_LIBEDIT
1113 	 phpdbg_writeln("info", "libedit=\"yes\"", "Libedit    yes");
1114 # else
1115 	 phpdbg_writeln("info", "libedit=\"no\"", "Libedit    no");
1116 # endif
1117 #else
1118      phpdbg_writeln("info", "readline=\"unavailable\"", "Readline   unavailable");
1119 #endif
1120 
1121 	phpdbg_writeln("info", "context=\"%s\"", "Exec       %s", PHPDBG_G(exec) ? PHPDBG_G(exec) : "none");
1122 	phpdbg_writeln("info", "compiled=\"%s\"", "Compiled   %s", PHPDBG_G(ops) ? "yes" : "no");
1123 	phpdbg_writeln("info", "stepping=\"%s\"", "Stepping   %s", (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ? "on" : "off");
1124 	phpdbg_writeln("info", "quiet=\"%s\"", "Quietness  %s", (PHPDBG_G(flags) & PHPDBG_IS_QUIET) ? "on" : "off");
1125 	phpdbg_writeln("info", "oplog=\"%s\"", "Oplog      %s", PHPDBG_G(oplog) ? "on" : "off");
1126 
1127 	if (PHPDBG_G(ops)) {
1128 		phpdbg_writeln("info", "ops=\"%d\"", "Opcodes    %d", PHPDBG_G(ops)->last);
1129 		phpdbg_writeln("info", "vars=\"%d\"", "Variables  %d", PHPDBG_G(ops)->last_var ? PHPDBG_G(ops)->last_var - 1 : 0);
1130 	}
1131 
1132 	phpdbg_writeln("info", "executing=\"%d\"", "Executing  %s", PHPDBG_G(in_execution) ? "yes" : "no");
1133 	if (PHPDBG_G(in_execution)) {
1134 		phpdbg_writeln("info", "vmret=\"%d\"", "VM Return  %d", PHPDBG_G(vmret));
1135 	}
1136 
1137 	phpdbg_writeln("info", "classes=\"%d\"", "Classes    %d", zend_hash_num_elements(EG(class_table)));
1138 	phpdbg_writeln("info", "functions=\"%d\"", "Functions  %d", zend_hash_num_elements(EG(function_table)));
1139 	phpdbg_writeln("info", "constants=\"%d\"", "Constants  %d", zend_hash_num_elements(EG(zend_constants)));
1140 	phpdbg_writeln("info", "includes=\"%d\"", "Included   %d", zend_hash_num_elements(&EG(included_files)));
1141 	phpdbg_xml("</printinfo>");
1142 
1143 	return SUCCESS;
1144 } /* }}} */
1145 
PHPDBG_COMMAND(set)1146 PHPDBG_COMMAND(set) /* {{{ */
1147 {
1148 	phpdbg_error("set", "type=\"toofewargs\" expected=\"1\"", "No set command selected!");
1149 
1150 	return SUCCESS;
1151 } /* }}} */
1152 
PHPDBG_COMMAND(break)1153 PHPDBG_COMMAND(break) /* {{{ */
1154 {
1155 	if (!param) {
1156 		if (PHPDBG_G(exec)) {
1157 			phpdbg_set_breakpoint_file(
1158 				zend_get_executed_filename(),
1159 				strlen(zend_get_executed_filename()),
1160 				zend_get_executed_lineno());
1161 		} else {
1162 			phpdbg_error("inactive", "type=\"noexec\"", "Execution context not set!");
1163 		}
1164 	} else switch (param->type) {
1165 		case ADDR_PARAM:
1166 			phpdbg_set_breakpoint_opline(param->addr);
1167 			break;
1168 		case NUMERIC_PARAM:
1169 			if (PHPDBG_G(exec)) {
1170 				phpdbg_set_breakpoint_file(phpdbg_current_file(), strlen(phpdbg_current_file()), param->num);
1171 			} else {
1172 				phpdbg_error("inactive", "type=\"noexec\"", "Execution context not set!");
1173 			}
1174 			break;
1175 		case METHOD_PARAM:
1176 			phpdbg_set_breakpoint_method(param->method.class, param->method.name);
1177 			break;
1178 		case NUMERIC_METHOD_PARAM:
1179 			phpdbg_set_breakpoint_method_opline(param->method.class, param->method.name, param->num);
1180 			break;
1181 		case NUMERIC_FUNCTION_PARAM:
1182 			phpdbg_set_breakpoint_function_opline(param->str, param->num);
1183 			break;
1184 		case FILE_PARAM:
1185 			phpdbg_set_breakpoint_file(param->file.name, 0, param->file.line);
1186 			break;
1187 		case NUMERIC_FILE_PARAM:
1188 			phpdbg_set_breakpoint_file_opline(param->file.name, param->file.line);
1189 			break;
1190 		case COND_PARAM:
1191 			phpdbg_set_breakpoint_expression(param->str, param->len);
1192 			break;
1193 		case STR_PARAM:
1194 			phpdbg_set_breakpoint_symbol(param->str, param->len);
1195 			break;
1196 		case OP_PARAM:
1197 			phpdbg_set_breakpoint_opcode(param->str, param->len);
1198 			break;
1199 
1200 		phpdbg_default_switch_case();
1201 	}
1202 
1203 	return SUCCESS;
1204 } /* }}} */
1205 
PHPDBG_COMMAND(sh)1206 PHPDBG_COMMAND(sh) /* {{{ */
1207 {
1208 	FILE *fd = NULL;
1209 	if ((fd=VCWD_POPEN((char*)param->str, "w"))) {
1210 		/* TODO: do something perhaps ?? do we want input ?? */
1211 		pclose(fd);
1212 	} else {
1213 		phpdbg_error("sh", "type=\"failure\" smd=\"%s\"", "Failed to execute %s", param->str);
1214 	}
1215 
1216 	return SUCCESS;
1217 } /* }}} */
1218 
add_module_info(zend_module_entry * module)1219 static int add_module_info(zend_module_entry *module) /* {{{ */ {
1220 	phpdbg_write("module", "name=\"%s\"", "%s\n", module->name);
1221 	return 0;
1222 }
1223 /* }}} */
1224 
add_zendext_info(zend_extension * ext)1225 static void add_zendext_info(zend_extension *ext) /* {{{ */ {
1226 	phpdbg_write("extension", "name=\"%s\"", "%s\n", ext->name);
1227 }
1228 /* }}} */
1229 
1230 #ifdef HAVE_LIBDL
phpdbg_load_module_or_extension(char ** path,char ** name)1231 PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, char **name) /* {{{ */ {
1232 	DL_HANDLE handle;
1233 	char *extension_dir;
1234 
1235 	extension_dir = INI_STR("extension_dir");
1236 
1237 	if (strchr(*path, '/') != NULL || strchr(*path, DEFAULT_SLASH) != NULL) {
1238 		/* path is fine */
1239 	} else if (extension_dir && extension_dir[0]) {
1240 		char *libpath;
1241 		int extension_dir_len = strlen(extension_dir);
1242 		if (IS_SLASH(extension_dir[extension_dir_len-1])) {
1243 			spprintf(&libpath, 0, "%s%s", extension_dir, *path); /* SAFE */
1244 		} else {
1245 			spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, *path); /* SAFE */
1246 		}
1247 		efree(*path);
1248 		*path = libpath;
1249 	} else {
1250 		phpdbg_error("dl", "type=\"relpath\"", "Not a full path given or extension_dir ini setting is not set");
1251 
1252 		return NULL;
1253 	}
1254 
1255 	handle = DL_LOAD(*path);
1256 
1257 	if (!handle) {
1258 #ifdef PHP_WIN32
1259 		char *err = GET_DL_ERROR();
1260 		if (err && err[0]) {
1261 			phpdbg_error("dl", "type=\"unknown\"", "%s", err);
1262 			php_win32_error_msg_free(err);
1263 		} else {
1264 			phpdbg_error("dl", "type=\"unknown\"", "Unknown reason");
1265 		}
1266 #else
1267 		phpdbg_error("dl", "type=\"unknown\"", "%s", GET_DL_ERROR());
1268 #endif
1269 		return NULL;
1270 	}
1271 
1272 #if ZEND_EXTENSIONS_SUPPORT
1273 	do {
1274 		zend_extension *new_extension;
1275 		zend_extension_version_info *extension_version_info;
1276 
1277 		extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
1278 		if (!extension_version_info) {
1279 			extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
1280 		}
1281 		new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
1282 		if (!new_extension) {
1283 			new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
1284 		}
1285 		if (!extension_version_info || !new_extension) {
1286 			break;
1287 		}
1288 		if (extension_version_info->zend_extension_api_no != ZEND_EXTENSION_API_NO &&(!new_extension->api_no_check || new_extension->api_no_check(ZEND_EXTENSION_API_NO) != SUCCESS)) {
1289 			phpdbg_error("dl", "type=\"wrongapi\" extension=\"%s\" apineeded=\"%d\" apiinstalled=\"%d\"", "%s requires Zend Engine API version %d, which does not match the installed Zend Engine API version %d", new_extension->name, extension_version_info->zend_extension_api_no, ZEND_EXTENSION_API_NO);
1290 
1291 			goto quit;
1292 		} else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) && (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) {
1293 			phpdbg_error("dl", "type=\"wrongbuild\" extension=\"%s\" buildneeded=\"%s\" buildinstalled=\"%s\"", "%s was built with configuration %s, whereas running engine is %s", new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
1294 
1295 			goto quit;
1296 		}
1297 
1298 		*name = new_extension->name;
1299 
1300 		zend_register_extension(new_extension, handle);
1301 
1302 		if (new_extension->startup) {
1303 			if (new_extension->startup(new_extension) != SUCCESS) {
1304 				phpdbg_error("dl", "type=\"startupfailure\" extension=\"%s\"", "Unable to startup Zend extension %s", new_extension->name);
1305 
1306 				goto quit;
1307 			}
1308 			zend_append_version_info(new_extension);
1309 		}
1310 
1311 		return "Zend extension";
1312 	} while (0);
1313 #endif
1314 
1315 	do {
1316 		zend_module_entry *module_entry;
1317 		zend_module_entry *(*get_module)(void);
1318 
1319 		get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
1320 		if (!get_module) {
1321 			get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
1322 		}
1323 
1324 		if (!get_module) {
1325 			break;
1326 		}
1327 
1328 		module_entry = get_module();
1329 		*name = (char *) module_entry->name;
1330 
1331 		if (strcmp(ZEND_EXTENSION_BUILD_ID, module_entry->build_id)) {
1332 			phpdbg_error("dl", "type=\"wrongbuild\" module=\"%s\" buildneeded=\"%s\" buildinstalled=\"%s\"",  "%s was built with configuration %s, whereas running engine is %s", module_entry->name, module_entry->build_id, ZEND_EXTENSION_BUILD_ID);
1333 
1334 			goto quit;
1335 		}
1336 
1337 		module_entry->type = MODULE_PERSISTENT;
1338 		module_entry->module_number = zend_next_free_module();
1339 		module_entry->handle = handle;
1340 
1341 		if ((module_entry = zend_register_module_ex(module_entry)) == NULL) {
1342 			phpdbg_error("dl", "type=\"registerfailure\" module=\"%s\"", "Unable to register module %s", module_entry->name);
1343 
1344 			goto quit;
1345 		}
1346 
1347 		if (zend_startup_module_ex(module_entry) == FAILURE) {
1348 			phpdbg_error("dl", "type=\"startupfailure\" module=\"%s\"", "Unable to startup module %s", module_entry->name);
1349 
1350 			goto quit;
1351 		}
1352 
1353 		if (module_entry->request_startup_func) {
1354 			if (module_entry->request_startup_func(MODULE_PERSISTENT, module_entry->module_number) == FAILURE) {
1355 				phpdbg_error("dl", "type=\"initfailure\" module=\"%s\"", "Unable to initialize module %s", module_entry->name);
1356 
1357 				goto quit;
1358 			}
1359 		}
1360 
1361 		return "module";
1362 	} while (0);
1363 
1364 	phpdbg_error("dl", "type=\"nophpso\"", "This shared object is nor a Zend extension nor a module");
1365 
1366 quit:
1367 	DL_UNLOAD(handle);
1368 	return NULL;
1369 }
1370 /* }}} */
1371 #endif
1372 
PHPDBG_COMMAND(dl)1373 PHPDBG_COMMAND(dl) /* {{{ */
1374 {
1375 	const char *type;
1376 	char *name, *path;
1377 
1378 	if (!param || param->type == EMPTY_PARAM) {
1379 		phpdbg_notice("dl", "extensiontype=\"Zend extension\"", "Zend extensions");
1380 		zend_llist_apply(&zend_extensions, (llist_apply_func_t) add_zendext_info);
1381 		phpdbg_out("\n");
1382 		phpdbg_notice("dl", "extensiontype=\"module\"", "Modules");
1383 		zend_hash_apply(&module_registry, (apply_func_t) add_module_info);
1384 	} else switch (param->type) {
1385 		case STR_PARAM:
1386 #ifdef HAVE_LIBDL
1387 			path = estrndup(param->str, param->len);
1388 
1389 			phpdbg_activate_err_buf(1);
1390 			if ((type = phpdbg_load_module_or_extension(&path, &name)) == NULL) {
1391 				phpdbg_error("dl", "path=\"%s\" %b", "Could not load %s, not found or invalid zend extension / module: %b", path);
1392 				efree(name);
1393 			} else {
1394 				phpdbg_notice("dl", "extensiontype=\"%s\" name=\"%s\" path=\"%s\"", "Successfully loaded the %s %s at path %s", type, name, path);
1395 			}
1396 			phpdbg_activate_err_buf(0);
1397 			phpdbg_free_err_buf();
1398 			efree(path);
1399 #else
1400 			phpdbg_error("dl", "type=\"unsupported\" path=\"%.*s\"", "Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str);
1401 #endif
1402 			break;
1403 
1404 		phpdbg_default_switch_case();
1405 	}
1406 
1407 	return SUCCESS;
1408 } /* }}} */
1409 
PHPDBG_COMMAND(source)1410 PHPDBG_COMMAND(source) /* {{{ */
1411 {
1412 	zend_stat_t sb;
1413 
1414 	if (VCWD_STAT(param->str, &sb) != -1) {
1415 		phpdbg_try_file_init(param->str, param->len, 0);
1416 	} else {
1417 		phpdbg_error("source", "type=\"notfound\" file=\"%s\"", "Failed to stat %s, file does not exist", param->str);
1418 	}
1419 
1420 	return SUCCESS;
1421 } /* }}} */
1422 
PHPDBG_COMMAND(export)1423 PHPDBG_COMMAND(export) /* {{{ */
1424 {
1425 	FILE *handle = VCWD_FOPEN(param->str, "w+");
1426 
1427 	if (handle) {
1428 		phpdbg_export_breakpoints(handle);
1429 		fclose(handle);
1430 	} else {
1431 		phpdbg_error("export", "type=\"openfailure\" file=\"%s\"", "Failed to open or create %s, check path and permissions", param->str);
1432 	}
1433 
1434 	return SUCCESS;
1435 } /* }}} */
1436 
PHPDBG_COMMAND(register)1437 PHPDBG_COMMAND(register) /* {{{ */
1438 {
1439 	zend_function *function;
1440 	char *lcname = zend_str_tolower_dup(param->str, param->len);
1441 	size_t lcname_len = strlen(lcname);
1442 
1443 	if (!zend_hash_str_exists(&PHPDBG_G(registered), lcname, lcname_len)) {
1444 		if ((function = zend_hash_str_find_ptr(EG(function_table), lcname, lcname_len))) {
1445 			zend_hash_str_update_ptr(&PHPDBG_G(registered), lcname, lcname_len, function);
1446 			function_add_ref(function);
1447 
1448 			phpdbg_notice("register", "function=\"%s\"", "Registered %s", lcname);
1449 		} else {
1450 			phpdbg_error("register", "type=\"notfound\" function=\"%s\"", "The requested function (%s) could not be found", param->str);
1451 		}
1452 	} else {
1453 		phpdbg_error("register", "type=\"inuse\" function=\"%s\"", "The requested name (%s) is already in use", lcname);
1454 	}
1455 
1456 	efree(lcname);
1457 	return SUCCESS;
1458 } /* }}} */
1459 
PHPDBG_COMMAND(quit)1460 PHPDBG_COMMAND(quit) /* {{{ */
1461 {
1462 	PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
1463 	PHPDBG_G(flags) &= ~PHPDBG_IS_CLEANING;
1464 
1465 	return SUCCESS;
1466 } /* }}} */
1467 
PHPDBG_COMMAND(clean)1468 PHPDBG_COMMAND(clean) /* {{{ */
1469 {
1470 	if (PHPDBG_G(in_execution)) {
1471 		if (phpdbg_ask_user_permission("Do you really want to clean your current environment?") == FAILURE) {
1472 			return SUCCESS;
1473 		}
1474 	}
1475 
1476 	phpdbg_out("Cleaning Execution Environment\n");
1477 	phpdbg_xml("<cleaninfo %r>");
1478 
1479 	phpdbg_writeln("clean", "classes=\"%d\"", "Classes    %d", zend_hash_num_elements(EG(class_table)));
1480 	phpdbg_writeln("clean", "functions=\"%d\"", "Functions  %d", zend_hash_num_elements(EG(function_table)));
1481 	phpdbg_writeln("clean", "constants=\"%d\"", "Constants  %d", zend_hash_num_elements(EG(zend_constants)));
1482 	phpdbg_writeln("clean", "includes=\"%d\"", "Includes   %d", zend_hash_num_elements(&EG(included_files)));
1483 
1484 	phpdbg_clean(1, 0);
1485 
1486 	phpdbg_xml("</cleaninfo>");
1487 
1488 	return SUCCESS;
1489 } /* }}} */
1490 
PHPDBG_COMMAND(clear)1491 PHPDBG_COMMAND(clear) /* {{{ */
1492 {
1493 	phpdbg_out("Clearing Breakpoints\n");
1494 	phpdbg_xml("<clearinfo %r>");
1495 
1496 	phpdbg_writeln("clear", "files=\"%d\"", "File              %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]));
1497 	phpdbg_writeln("clear", "functions=\"%d\"", "Functions         %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]));
1498 	phpdbg_writeln("clear", "methods=\"%d\"", "Methods           %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]));
1499 	phpdbg_writeln("clear", "oplines=\"%d\"", "Oplines           %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]));
1500 	phpdbg_writeln("clear", "fileoplines=\"%d\"", "File oplines      %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE]));
1501 	phpdbg_writeln("clear", "functionoplines=\"%d\"", "Function oplines  %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE]));
1502 	phpdbg_writeln("clear", "methodoplines=\"%d\"", "Method oplines    %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE]));
1503 	phpdbg_writeln("clear", "eval=\"%d\"", "Conditionals      %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_COND]));
1504 
1505 	phpdbg_clear_breakpoints();
1506 
1507 	phpdbg_xml("</clearinfo>");
1508 
1509 	return SUCCESS;
1510 } /* }}} */
1511 
PHPDBG_COMMAND(list)1512 PHPDBG_COMMAND(list) /* {{{ */
1513 {
1514 	if (!param) {
1515 		return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
1516 	} else switch (param->type) {
1517 		case NUMERIC_PARAM:
1518 			return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
1519 
1520 		case FILE_PARAM:
1521 			return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
1522 
1523 		case STR_PARAM:
1524 			phpdbg_list_function_byname(param->str, param->len);
1525 			break;
1526 
1527 		case METHOD_PARAM:
1528 			return PHPDBG_LIST_HANDLER(method)(PHPDBG_COMMAND_ARGS);
1529 
1530 		phpdbg_default_switch_case();
1531 	}
1532 
1533 	return SUCCESS;
1534 } /* }}} */
1535 
PHPDBG_COMMAND(watch)1536 PHPDBG_COMMAND(watch) /* {{{ */
1537 {
1538 	if (!param || param->type == EMPTY_PARAM) {
1539 		phpdbg_list_watchpoints();
1540 	} else switch (param->type) {
1541 		case STR_PARAM:
1542 			phpdbg_create_var_watchpoint(param->str, param->len);
1543 			break;
1544 
1545 		phpdbg_default_switch_case();
1546 	}
1547 
1548 	return SUCCESS;
1549 } /* }}} */
1550 
phpdbg_interactive(zend_bool allow_async_unsafe,char * input)1551 int phpdbg_interactive(zend_bool allow_async_unsafe, char *input) /* {{{ */
1552 {
1553 	int ret = SUCCESS;
1554 	phpdbg_param_t stack;
1555 
1556 	PHPDBG_G(flags) |= PHPDBG_IS_INTERACTIVE;
1557 
1558 	while (ret == SUCCESS || ret == FAILURE) {
1559 		if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1560 			zend_bailout();
1561 		}
1562 
1563 		if (!input && !(input = phpdbg_read_input(NULL))) {
1564 			break;
1565 		}
1566 
1567 
1568 		phpdbg_init_param(&stack, STACK_PARAM);
1569 
1570 		if (phpdbg_do_parse(&stack, input) <= 0) {
1571 			phpdbg_activate_err_buf(1);
1572 
1573 #ifdef PHP_WIN32
1574 #define PARA ((phpdbg_param_t *)stack.next)->type
1575 			if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE && (RUN_PARAM == PARA || EVAL_PARAM == PARA)) {
1576 				sigio_watcher_start();
1577 			}
1578 #endif
1579 			zend_try {
1580 				ret = phpdbg_stack_execute(&stack, allow_async_unsafe);
1581 			} zend_catch {
1582 				phpdbg_stack_free(&stack);
1583 				zend_bailout();
1584 			} zend_end_try();
1585 
1586 			switch (ret) {
1587 				case FAILURE:
1588 					if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
1589 						if (!allow_async_unsafe || phpdbg_call_register(&stack) == FAILURE) {
1590 							phpdbg_output_err_buf(NULL, "%b", "%b");
1591 						}
1592 					}
1593 				break;
1594 
1595 				case PHPDBG_LEAVE:
1596 				case PHPDBG_FINISH:
1597 				case PHPDBG_UNTIL:
1598 				case PHPDBG_NEXT: {
1599 					phpdbg_activate_err_buf(0);
1600 					phpdbg_free_err_buf();
1601 					if (!PHPDBG_G(in_execution) && !(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
1602 						phpdbg_error("command", "type=\"noexec\"", "Not running");
1603 					}
1604 					break;
1605 				}
1606 			}
1607 
1608 			phpdbg_activate_err_buf(0);
1609 			phpdbg_free_err_buf();
1610 #ifdef PHP_WIN32
1611 			if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE && (RUN_PARAM == PARA || EVAL_PARAM == PARA)) {
1612 				sigio_watcher_stop();
1613 			}
1614 #undef PARA
1615 #endif
1616 		}
1617 
1618 		phpdbg_stack_free(&stack);
1619 		phpdbg_destroy_input(&input);
1620 		PHPDBG_G(req_id) = 0;
1621 		input = NULL;
1622 	}
1623 
1624 	if (input) {
1625 		phpdbg_stack_free(&stack);
1626 		phpdbg_destroy_input(&input);
1627 		PHPDBG_G(req_id) = 0;
1628 	}
1629 
1630 	if (PHPDBG_G(in_execution)) {
1631 		phpdbg_restore_frame();
1632 	}
1633 
1634 	PHPDBG_G(flags) &= ~PHPDBG_IS_INTERACTIVE;
1635 
1636 	phpdbg_print_changed_zvals();
1637 
1638 	return ret;
1639 } /* }}} */
1640 
list_code()1641 static inline void list_code() {
1642 	if (!(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1643 		const char *file_char = zend_get_executed_filename();
1644 		zend_string *file = zend_string_init(file_char, strlen(file_char), 0);
1645 		phpdbg_list_file(file, 3, zend_get_executed_lineno()-1, zend_get_executed_lineno());
1646 		efree(file);
1647 	}
1648 }
1649 
1650 /* code may behave weirdly if EG(exception) is set; thus backup it */
1651 #define DO_INTERACTIVE(allow_async_unsafe) do { \
1652 	if (exception) { \
1653 		const zend_op *before_ex = EG(opline_before_exception); \
1654 		const zend_op *backup_opline = NULL; \
1655 		if (EG(current_execute_data) && EG(current_execute_data)->func && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) { \
1656 			backup_opline = EG(current_execute_data)->opline; \
1657 		} \
1658 		GC_ADDREF(exception); \
1659 		zend_clear_exception(); \
1660 		list_code(); \
1661 		switch (phpdbg_interactive(allow_async_unsafe, NULL)) { \
1662 			case PHPDBG_LEAVE: \
1663 			case PHPDBG_FINISH: \
1664 			case PHPDBG_UNTIL: \
1665 			case PHPDBG_NEXT: \
1666 				if (backup_opline \
1667 				 && (backup_opline->opcode == ZEND_HANDLE_EXCEPTION || backup_opline->opcode == ZEND_CATCH)) { \
1668 					EG(current_execute_data)->opline = backup_opline; \
1669 					EG(exception) = exception; \
1670 				} else { \
1671 					zend_throw_exception_internal(exception); \
1672 				} \
1673 				EG(opline_before_exception) = before_ex; \
1674 		} \
1675 	} else { \
1676 		list_code(); \
1677 		phpdbg_interactive(allow_async_unsafe, NULL); \
1678 	} \
1679 	goto next; \
1680 } while (0)
1681 
phpdbg_execute_ex(zend_execute_data * execute_data)1682 void phpdbg_execute_ex(zend_execute_data *execute_data) /* {{{ */
1683 {
1684 	zend_bool original_in_execution = PHPDBG_G(in_execution);
1685 
1686 	if ((PHPDBG_G(flags) & PHPDBG_IS_STOPPING) && !(PHPDBG_G(flags) & PHPDBG_IS_RUNNING)) {
1687 		zend_bailout();
1688 	}
1689 
1690 	PHPDBG_G(in_execution) = 1;
1691 
1692 	while (1) {
1693 		zend_object *exception = EG(exception);
1694 
1695 		if ((PHPDBG_G(flags) & PHPDBG_BP_RESOLVE_MASK)) {
1696 			/* resolve nth opline breakpoints */
1697 			phpdbg_resolve_op_array_breaks(&execute_data->func->op_array);
1698 		}
1699 
1700 #ifdef ZEND_WIN32
1701 		if (EG(timed_out)) {
1702 			zend_timeout();
1703 		}
1704 #endif
1705 
1706 		if (exception && zend_is_unwind_exit(exception)) {
1707 			/* Restore bailout based exit. */
1708 			zend_bailout();
1709 		}
1710 
1711 		if (PHPDBG_G(flags) & PHPDBG_PREVENT_INTERACTIVE) {
1712 			phpdbg_print_opline_ex(execute_data, 0);
1713 			goto next;
1714 		}
1715 
1716 		/* check for uncaught exceptions */
1717 		if (exception && PHPDBG_G(handled_exception) != exception && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1718 			zend_execute_data *prev_ex = execute_data;
1719 
1720 			do {
1721 				prev_ex = zend_generator_check_placeholder_frame(prev_ex);
1722 				/* assuming that no internal functions will silently swallow exceptions ... */
1723 				if (!prev_ex->func || !ZEND_USER_CODE(prev_ex->func->common.type)) {
1724 					continue;
1725 				}
1726 
1727 				if (phpdbg_check_caught_ex(prev_ex, exception)) {
1728 					goto ex_is_caught;
1729 				}
1730 			} while ((prev_ex = prev_ex->prev_execute_data));
1731 
1732 			PHPDBG_G(handled_exception) = exception;
1733 
1734 			zval rv;
1735 			zend_string *file = zval_get_string(zend_read_property(zend_get_exception_base(exception), exception, ZEND_STRL("file"), 1, &rv));
1736 			zend_long line = zval_get_long(zend_read_property(zend_get_exception_base(exception), exception, ZEND_STRL("line"), 1, &rv));
1737 			zend_string *msg = zval_get_string(zend_read_property(zend_get_exception_base(exception), exception, ZEND_STRL("message"), 1, &rv));
1738 
1739 			phpdbg_error("exception",
1740 				"name=\"%s\" file=\"%s\" line=\"" ZEND_LONG_FMT "\"",
1741 				"Uncaught %s in %s on line " ZEND_LONG_FMT ": %.*s",
1742 				ZSTR_VAL(exception->ce->name), ZSTR_VAL(file), line,
1743 				ZSTR_LEN(msg) < 80 ? (int) ZSTR_LEN(msg) : 80, ZSTR_VAL(msg));
1744 			zend_string_release(msg);
1745 			zend_string_release(file);
1746 
1747 			DO_INTERACTIVE(1);
1748 		}
1749 ex_is_caught:
1750 
1751 		/* allow conditional breakpoints and initialization to access the vm uninterrupted */
1752 		if (PHPDBG_G(flags) & (PHPDBG_IN_COND_BP | PHPDBG_IS_INITIALIZING)) {
1753 			/* skip possible breakpoints */
1754 			goto next;
1755 		}
1756 
1757 		/* not while in conditionals */
1758 		phpdbg_print_opline_ex(execute_data, 0);
1759 
1760 		/* perform seek operation */
1761 		if ((PHPDBG_G(flags) & PHPDBG_SEEK_MASK) && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1762 			/* current address */
1763 			zend_ulong address = (zend_ulong) execute_data->opline;
1764 
1765 			if (PHPDBG_G(seek_ex) != execute_data) {
1766 				if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) {
1767 					goto stepping;
1768 				}
1769 				goto next;
1770 			}
1771 
1772 #define INDEX_EXISTS_CHECK (zend_hash_index_exists(&PHPDBG_G(seek), address) || (exception && phpdbg_check_caught_ex(execute_data, exception) == 0))
1773 
1774 			/* run to next line */
1775 			if (PHPDBG_G(flags) & PHPDBG_IN_UNTIL) {
1776 				if (INDEX_EXISTS_CHECK) {
1777 					PHPDBG_G(flags) &= ~PHPDBG_IN_UNTIL;
1778 					zend_hash_clean(&PHPDBG_G(seek));
1779 				} else {
1780 					/* skip possible breakpoints */
1781 					goto next;
1782 				}
1783 			}
1784 
1785 			/* run to finish */
1786 			if (PHPDBG_G(flags) & PHPDBG_IN_FINISH) {
1787 				if (INDEX_EXISTS_CHECK) {
1788 					PHPDBG_G(flags) &= ~PHPDBG_IN_FINISH;
1789 					zend_hash_clean(&PHPDBG_G(seek));
1790 				}
1791 				/* skip possible breakpoints */
1792 				goto next;
1793 			}
1794 
1795 			/* break for leave */
1796 			if (PHPDBG_G(flags) & PHPDBG_IN_LEAVE) {
1797 				if (INDEX_EXISTS_CHECK) {
1798 					PHPDBG_G(flags) &= ~PHPDBG_IN_LEAVE;
1799 					zend_hash_clean(&PHPDBG_G(seek));
1800 					phpdbg_notice("breakpoint", "id=\"leave\" file=\"%s\" line=\"%u\"", "Breaking for leave at %s:%u",
1801 						zend_get_executed_filename(),
1802 						zend_get_executed_lineno()
1803 					);
1804 					DO_INTERACTIVE(1);
1805 				} else {
1806 					/* skip possible breakpoints */
1807 					goto next;
1808 				}
1809 			}
1810 		}
1811 
1812 		if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING && (PHPDBG_G(flags) & PHPDBG_STEP_OPCODE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1813 stepping:
1814 			PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
1815 			DO_INTERACTIVE(1);
1816 		}
1817 
1818 		/* check if some watchpoint was hit */
1819 		{
1820 			if (phpdbg_print_changed_zvals() == SUCCESS) {
1821 				DO_INTERACTIVE(1);
1822 			}
1823 		}
1824 
1825 		/* search for breakpoints */
1826 		{
1827 			phpdbg_breakbase_t *brake;
1828 
1829 			if ((PHPDBG_G(flags) & PHPDBG_BP_MASK)
1830 			    && (brake = phpdbg_find_breakpoint(execute_data))
1831 			    && (brake->type != PHPDBG_BREAK_FILE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1832 				phpdbg_hit_breakpoint(brake, 1);
1833 				DO_INTERACTIVE(1);
1834 			}
1835 		}
1836 
1837 		if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
1838 			PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
1839 
1840 			phpdbg_out("\n");
1841 			phpdbg_notice("signal", "type=\"SIGINT\"", "Program received signal SIGINT");
1842 			DO_INTERACTIVE(1);
1843 		}
1844 
1845 next:
1846 
1847 		PHPDBG_G(last_line) = execute_data->opline->lineno;
1848 
1849 		/* stupid hack to make zend_do_fcall_common_helper return ZEND_VM_ENTER() instead of recursively calling zend_execute() and eventually segfaulting */
1850 		if ((execute_data->opline->opcode == ZEND_DO_FCALL ||
1851 		     execute_data->opline->opcode == ZEND_DO_UCALL ||
1852 		     execute_data->opline->opcode == ZEND_DO_FCALL_BY_NAME) &&
1853 		     execute_data->call->func->type == ZEND_USER_FUNCTION) {
1854 			zend_execute_ex = execute_ex;
1855 		}
1856 		PHPDBG_G(vmret) = zend_vm_call_opcode_handler(execute_data);
1857 		zend_execute_ex = phpdbg_execute_ex;
1858 
1859 		if (PHPDBG_G(vmret) != 0) {
1860 			if (PHPDBG_G(vmret) < 0) {
1861 				PHPDBG_G(in_execution) = original_in_execution;
1862 				return;
1863 			} else {
1864 				execute_data = EG(current_execute_data);
1865 			}
1866 		}
1867 	}
1868 	zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
1869 } /* }}} */
1870 
1871 /* only if *not* interactive and while executing */
phpdbg_force_interruption(void)1872 void phpdbg_force_interruption(void) /* {{{ */ {
1873 	zend_object *exception = EG(exception);
1874 	zend_execute_data *data = EG(current_execute_data); /* should be always readable if not NULL */
1875 
1876 	PHPDBG_G(flags) |= PHPDBG_IN_SIGNAL_HANDLER;
1877 
1878 	if (data) {
1879 		if (data->func) {
1880 			if (ZEND_USER_CODE(data->func->type)) {
1881 				phpdbg_notice("hardinterrupt", "opline=\"%p\" num=\"%lu\" file=\"%s\" line=\"%u\"", "Current opline: %p (op #%lu) in %s:%u", data->opline, (data->opline - data->func->op_array.opcodes) / sizeof(data->opline), data->func->op_array.filename->val, data->opline->lineno);
1882 			} else if (data->func->internal_function.function_name) {
1883 				phpdbg_notice("hardinterrupt", "func=\"%s\"", "Current opline: in internal function %s", data->func->internal_function.function_name->val);
1884 			} else {
1885 				phpdbg_notice("hardinterrupt", "", "Current opline: executing internal code");
1886 			}
1887 		} else {
1888 			phpdbg_notice("hardinterrupt", "opline=\"%p\"", "Current opline: %p (op_array information unavailable)", data->opline);
1889 		}
1890 	} else {
1891 		phpdbg_notice("hardinterrupt", "", "No information available about executing context");
1892 	}
1893 
1894 	DO_INTERACTIVE(0);
1895 
1896 next:
1897 	PHPDBG_G(flags) &= ~PHPDBG_IN_SIGNAL_HANDLER;
1898 
1899 	if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1900 		zend_bailout();
1901 	}
1902 }
1903 /* }}} */
1904 
PHPDBG_COMMAND(eol)1905 PHPDBG_COMMAND(eol) /* {{{ */
1906 {
1907 	if (!param || param->type == EMPTY_PARAM) {
1908 		phpdbg_notice("eol", "argument required", "argument required");
1909 	} else switch (param->type) {
1910 		case STR_PARAM:
1911 			if (FAILURE == phpdbg_eol_global_update(param->str)) {
1912 				phpdbg_notice("eol", "unknown EOL name '%s', give crlf, lf, cr", "unknown EOL name '%s', give  crlf, lf, cr", param->str);
1913 			}
1914 			break;
1915 
1916 		phpdbg_default_switch_case();
1917 	}
1918 
1919 	return SUCCESS;
1920 } /* }}} */
1921