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