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