xref: /PHP-8.3/sapi/phpdbg/phpdbg_prompt.c (revision 95c97c81)
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 		ZEND_IGNORE_VALUE(asprintf(&sys_ini, "%s/" PHPDBG_INIT_FILENAME, PHP_CONFIG_FILE_PATH));
347 		phpdbg_try_file_init(sys_ini, strlen(sys_ini), 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, strlen(init_file), 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) && !zend_is_unwind_exit(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 		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", *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 = {0};
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 				phpdbg_destroy_input(&input);
1553 				/* TODO: should use proper unwinding instead of bailing out */
1554 				zend_bailout();
1555 			} zend_end_try();
1556 
1557 			switch (ret) {
1558 				case FAILURE:
1559 					if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
1560 						if (!allow_async_unsafe || phpdbg_call_register(&stack) == FAILURE) {
1561 							if (PHPDBG_G(err_buf).active) {
1562 							    phpdbg_output_err_buf("%s", PHPDBG_G(err_buf).msg);
1563 							}
1564 						}
1565 					}
1566 				break;
1567 
1568 				case PHPDBG_LEAVE:
1569 				case PHPDBG_FINISH:
1570 				case PHPDBG_UNTIL:
1571 				case PHPDBG_NEXT: {
1572 					phpdbg_activate_err_buf(0);
1573 					phpdbg_free_err_buf();
1574 					if (!PHPDBG_G(in_execution) && !(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
1575 						phpdbg_error("Not running");
1576 					}
1577 					break;
1578 				}
1579 			}
1580 
1581 			phpdbg_activate_err_buf(0);
1582 			phpdbg_free_err_buf();
1583 		}
1584 
1585 		phpdbg_stack_free(&stack);
1586 		phpdbg_destroy_input(&input);
1587 		PHPDBG_G(req_id) = 0;
1588 		input = NULL;
1589 	}
1590 
1591 	if (input) {
1592 		phpdbg_stack_free(&stack);
1593 		phpdbg_destroy_input(&input);
1594 		PHPDBG_G(req_id) = 0;
1595 	}
1596 
1597 	if (PHPDBG_G(in_execution)) {
1598 		phpdbg_restore_frame();
1599 	}
1600 
1601 	PHPDBG_G(flags) &= ~PHPDBG_IS_INTERACTIVE;
1602 
1603 	phpdbg_print_changed_zvals();
1604 
1605 	return ret;
1606 } /* }}} */
1607 
list_code(void)1608 static inline void list_code(void) {
1609 	if (!(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1610 		const char *file_char = zend_get_executed_filename();
1611 		zend_string *file = zend_string_init(file_char, strlen(file_char), 0);
1612 		phpdbg_list_file(file, 3, zend_get_executed_lineno()-1, zend_get_executed_lineno());
1613 		efree(file);
1614 	}
1615 }
1616 
1617 /* code may behave weirdly if EG(exception) is set; thus backup it */
1618 #define DO_INTERACTIVE(allow_async_unsafe) do { \
1619 	if (exception) { \
1620 		const zend_op *before_ex = EG(opline_before_exception); \
1621 		const zend_op *backup_opline = NULL; \
1622 		if (EG(current_execute_data) && EG(current_execute_data)->func && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) { \
1623 			backup_opline = EG(current_execute_data)->opline; \
1624 		} \
1625 		GC_ADDREF(exception); \
1626 		zend_clear_exception(); \
1627 		list_code(); \
1628 		switch (phpdbg_interactive(allow_async_unsafe, NULL)) { \
1629 			case PHPDBG_LEAVE: \
1630 			case PHPDBG_FINISH: \
1631 			case PHPDBG_UNTIL: \
1632 			case PHPDBG_NEXT: \
1633 				if (backup_opline \
1634 				 && (backup_opline->opcode == ZEND_HANDLE_EXCEPTION || backup_opline->opcode == ZEND_CATCH)) { \
1635 					EG(current_execute_data)->opline = backup_opline; \
1636 					EG(exception) = exception; \
1637 				} else { \
1638 					zend_throw_exception_internal(exception); \
1639 				} \
1640 				EG(opline_before_exception) = before_ex; \
1641 		} \
1642 	} else { \
1643 		list_code(); \
1644 		phpdbg_interactive(allow_async_unsafe, NULL); \
1645 	} \
1646 	goto next; \
1647 } while (0)
1648 
phpdbg_execute_ex(zend_execute_data * execute_data)1649 void phpdbg_execute_ex(zend_execute_data *execute_data) /* {{{ */
1650 {
1651 	bool original_in_execution = PHPDBG_G(in_execution);
1652 
1653 	if ((PHPDBG_G(flags) & PHPDBG_IS_STOPPING) && !(PHPDBG_G(flags) & PHPDBG_IS_RUNNING)) {
1654 		zend_bailout();
1655 	}
1656 
1657 	PHPDBG_G(in_execution) = 1;
1658 
1659 	while (1) {
1660 		zend_object *exception = EG(exception);
1661 
1662 		if ((PHPDBG_G(flags) & PHPDBG_BP_RESOLVE_MASK)) {
1663 			/* resolve nth opline breakpoints */
1664 			phpdbg_resolve_op_array_breaks(&execute_data->func->op_array);
1665 		}
1666 
1667 #ifdef ZEND_WIN32
1668 		if (zend_atomic_bool_load_ex(&EG(timed_out))) {
1669 			zend_timeout();
1670 		}
1671 #endif
1672 
1673 		if (exception && zend_is_unwind_exit(exception)) {
1674 			/* Restore bailout based exit. */
1675 			zend_bailout();
1676 		}
1677 
1678 		if (PHPDBG_G(flags) & PHPDBG_PREVENT_INTERACTIVE) {
1679 			phpdbg_print_opline(execute_data, 0);
1680 			goto next;
1681 		}
1682 
1683 		/* check for uncaught exceptions */
1684 		if (exception && PHPDBG_G(handled_exception) != exception && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1685 			zend_execute_data *prev_ex = execute_data;
1686 
1687 			do {
1688 				prev_ex = zend_generator_check_placeholder_frame(prev_ex);
1689 				/* assuming that no internal functions will silently swallow exceptions ... */
1690 				if (!prev_ex->func || !ZEND_USER_CODE(prev_ex->func->common.type)) {
1691 					continue;
1692 				}
1693 
1694 				if (phpdbg_check_caught_ex(prev_ex, exception)) {
1695 					goto ex_is_caught;
1696 				}
1697 			} while ((prev_ex = prev_ex->prev_execute_data));
1698 
1699 			PHPDBG_G(handled_exception) = exception;
1700 
1701 			zval rv;
1702 			zend_string *file = zval_get_string(zend_read_property_ex(zend_get_exception_base(exception), exception, ZSTR_KNOWN(ZEND_STR_FILE), /* silent */ true, &rv));
1703 			zend_long line = zval_get_long(zend_read_property_ex(zend_get_exception_base(exception), exception, ZSTR_KNOWN(ZEND_STR_LINE), /* silent */ true, &rv));
1704 			zend_string *msg = zval_get_string(zend_read_property_ex(zend_get_exception_base(exception), exception, ZSTR_KNOWN(ZEND_STR_MESSAGE), /* silent */ true, &rv));
1705 
1706 			phpdbg_error("Uncaught %s in %s on line " ZEND_LONG_FMT ": %.*s",
1707 				ZSTR_VAL(exception->ce->name), ZSTR_VAL(file), line,
1708 				ZSTR_LEN(msg) < 80 ? (int) ZSTR_LEN(msg) : 80, ZSTR_VAL(msg));
1709 			zend_string_release(msg);
1710 			zend_string_release(file);
1711 
1712 			DO_INTERACTIVE(1);
1713 		}
1714 ex_is_caught:
1715 
1716 		/* allow conditional breakpoints and initialization to access the vm uninterrupted */
1717 		if (PHPDBG_G(flags) & (PHPDBG_IN_COND_BP | PHPDBG_IS_INITIALIZING)) {
1718 			/* skip possible breakpoints */
1719 			goto next;
1720 		}
1721 
1722 		/* not while in conditionals */
1723 		phpdbg_print_opline(execute_data, 0);
1724 
1725 		/* perform seek operation */
1726 		if ((PHPDBG_G(flags) & PHPDBG_SEEK_MASK) && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1727 			/* current address */
1728 			zend_ulong address = (zend_ulong) execute_data->opline;
1729 
1730 			if (PHPDBG_G(seek_ex) != execute_data) {
1731 				if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) {
1732 					goto stepping;
1733 				}
1734 				goto next;
1735 			}
1736 
1737 #define INDEX_EXISTS_CHECK (zend_hash_index_exists(&PHPDBG_G(seek), address) || (exception && phpdbg_check_caught_ex(execute_data, exception) == 0))
1738 
1739 			/* run to next line */
1740 			if (PHPDBG_G(flags) & PHPDBG_IN_UNTIL) {
1741 				if (INDEX_EXISTS_CHECK) {
1742 					PHPDBG_G(flags) &= ~PHPDBG_IN_UNTIL;
1743 					zend_hash_clean(&PHPDBG_G(seek));
1744 				} else {
1745 					/* skip possible breakpoints */
1746 					goto next;
1747 				}
1748 			}
1749 
1750 			/* run to finish */
1751 			if (PHPDBG_G(flags) & PHPDBG_IN_FINISH) {
1752 				if (INDEX_EXISTS_CHECK) {
1753 					PHPDBG_G(flags) &= ~PHPDBG_IN_FINISH;
1754 					zend_hash_clean(&PHPDBG_G(seek));
1755 				}
1756 				/* skip possible breakpoints */
1757 				goto next;
1758 			}
1759 
1760 			/* break for leave */
1761 			if (PHPDBG_G(flags) & PHPDBG_IN_LEAVE) {
1762 				if (INDEX_EXISTS_CHECK) {
1763 					PHPDBG_G(flags) &= ~PHPDBG_IN_LEAVE;
1764 					zend_hash_clean(&PHPDBG_G(seek));
1765 					phpdbg_notice("Breaking for leave at %s:%u",
1766 						zend_get_executed_filename(),
1767 						zend_get_executed_lineno()
1768 					);
1769 					DO_INTERACTIVE(1);
1770 				} else {
1771 					/* skip possible breakpoints */
1772 					goto next;
1773 				}
1774 			}
1775 		}
1776 
1777 		if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING && (PHPDBG_G(flags) & PHPDBG_STEP_OPCODE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1778 stepping:
1779 			PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
1780 			DO_INTERACTIVE(1);
1781 		}
1782 
1783 		/* check if some watchpoint was hit */
1784 		{
1785 			if (phpdbg_print_changed_zvals() == SUCCESS) {
1786 				DO_INTERACTIVE(1);
1787 			}
1788 		}
1789 
1790 		/* search for breakpoints */
1791 		{
1792 			phpdbg_breakbase_t *brake;
1793 
1794 			if ((PHPDBG_G(flags) & PHPDBG_BP_MASK)
1795 			    && (brake = phpdbg_find_breakpoint(execute_data))
1796 			    && (brake->type != PHPDBG_BREAK_FILE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1797 				phpdbg_hit_breakpoint(brake, 1);
1798 				DO_INTERACTIVE(1);
1799 			}
1800 		}
1801 
1802 		if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
1803 			PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
1804 
1805 			phpdbg_out("\n");
1806 			phpdbg_notice("Program received signal SIGINT");
1807 			DO_INTERACTIVE(1);
1808 		}
1809 
1810 next:
1811 
1812 		PHPDBG_G(last_line) = execute_data->opline->lineno;
1813 
1814 		/* stupid hack to make zend_do_fcall_common_helper return ZEND_VM_ENTER() instead of recursively calling zend_execute() and eventually segfaulting */
1815 		if ((execute_data->opline->opcode == ZEND_DO_FCALL ||
1816 		     execute_data->opline->opcode == ZEND_DO_UCALL ||
1817 		     execute_data->opline->opcode == ZEND_DO_FCALL_BY_NAME) &&
1818 		     execute_data->call->func->type == ZEND_USER_FUNCTION) {
1819 			zend_execute_ex = execute_ex;
1820 		}
1821 		PHPDBG_G(vmret) = zend_vm_call_opcode_handler(execute_data);
1822 		zend_execute_ex = phpdbg_execute_ex;
1823 
1824 		if (PHPDBG_G(vmret) != 0) {
1825 			if (PHPDBG_G(vmret) < 0) {
1826 				PHPDBG_G(in_execution) = original_in_execution;
1827 				return;
1828 			} else {
1829 				execute_data = EG(current_execute_data);
1830 			}
1831 		}
1832 	}
1833 	zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
1834 } /* }}} */
1835 
1836 /* only if *not* interactive and while executing */
phpdbg_force_interruption(void)1837 void phpdbg_force_interruption(void) /* {{{ */ {
1838 	zend_object *exception = EG(exception);
1839 	zend_execute_data *data = EG(current_execute_data); /* should be always readable if not NULL */
1840 
1841 	PHPDBG_G(flags) |= PHPDBG_IN_SIGNAL_HANDLER;
1842 
1843 	if (data) {
1844 		if (data->func) {
1845 			if (ZEND_USER_CODE(data->func->type)) {
1846 				phpdbg_notice("Current opline: %p (op #%u) in %s:%u",
1847 				    data->opline,
1848 				    (uint32_t) (data->opline - data->func->op_array.opcodes),
1849 				    data->func->op_array.filename->val,
1850 				    data->opline->lineno);
1851 			} else if (data->func->internal_function.function_name) {
1852 				phpdbg_notice("Current opline: in internal function %s",
1853 				    data->func->internal_function.function_name->val);
1854 			} else {
1855 				phpdbg_notice("Current opline: executing internal code");
1856 			}
1857 		} else {
1858 			phpdbg_notice("Current opline: %p (op_array information unavailable)",
1859 			    data->opline);
1860 		}
1861 	} else {
1862 		phpdbg_notice("No information available about executing context");
1863 	}
1864 
1865 	DO_INTERACTIVE(0);
1866 
1867 next:
1868 	PHPDBG_G(flags) &= ~PHPDBG_IN_SIGNAL_HANDLER;
1869 
1870 	if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1871 		zend_bailout();
1872 	}
1873 }
1874 /* }}} */
1875