xref: /PHP-8.4/sapi/phpdbg/phpdbg_prompt.c (revision 443aa29d)
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_YIELD
612 		 || opline->opcode == ZEND_YIELD_FROM
613 		) {
614 			zend_hash_index_update_ptr(&PHPDBG_G(seek), (zend_ulong) opline, (void *) opline);
615 		}
616 	} while (++opline < op_array->opcodes + op_array->last);
617 
618 	return PHPDBG_UNTIL;
619 }
620 /* }}} */
621 
PHPDBG_COMMAND(until)622 PHPDBG_COMMAND(until) /* {{{ */
623 {
624 	if (!PHPDBG_G(in_execution)) {
625 		phpdbg_error("Not executing");
626 		return SUCCESS;
627 	}
628 
629 	return phpdbg_skip_line_helper();
630 } /* }}} */
631 
PHPDBG_COMMAND(next)632 PHPDBG_COMMAND(next) /* {{{ */
633 {
634 	if (!PHPDBG_G(in_execution)) {
635 		phpdbg_error("Not executing");
636 		return SUCCESS;
637 	}
638 
639 	PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
640 	return phpdbg_skip_line_helper();
641 } /* }}} */
642 
phpdbg_seek_to_end(void)643 static void phpdbg_seek_to_end(void) /* {{{ */ {
644 	zend_execute_data *ex = phpdbg_user_execute_data(EG(current_execute_data));
645 	const zend_op_array *op_array = &ex->func->op_array;
646 	const zend_op *opline = op_array->opcodes;
647 
648 	PHPDBG_G(seek_ex) = ex;
649 	do {
650 		switch (opline->opcode) {
651 			case ZEND_RETURN:
652 			case ZEND_FAST_RET:
653 			case ZEND_GENERATOR_RETURN:
654 			case ZEND_YIELD:
655 			case ZEND_YIELD_FROM:
656 				zend_hash_index_update_ptr(&PHPDBG_G(seek), (zend_ulong) opline, (void *) opline);
657 		}
658 	} while (++opline < op_array->opcodes + op_array->last);
659 }
660 /* }}} */
661 
PHPDBG_COMMAND(finish)662 PHPDBG_COMMAND(finish) /* {{{ */
663 {
664 	if (!PHPDBG_G(in_execution)) {
665 		phpdbg_error("Not executing");
666 		return SUCCESS;
667 	}
668 
669 	phpdbg_seek_to_end();
670 	if (zend_hash_index_exists(&PHPDBG_G(seek), (zend_ulong) phpdbg_user_execute_data(EG(current_execute_data))->opline)) {
671 		zend_hash_clean(&PHPDBG_G(seek));
672 	} else {
673 		PHPDBG_G(flags) |= PHPDBG_IN_FINISH;
674 	}
675 
676 	return PHPDBG_FINISH;
677 } /* }}} */
678 
PHPDBG_COMMAND(leave)679 PHPDBG_COMMAND(leave) /* {{{ */
680 {
681 	if (!PHPDBG_G(in_execution)) {
682 		phpdbg_error("Not executing");
683 		return SUCCESS;
684 	}
685 
686 	phpdbg_seek_to_end();
687 	if (zend_hash_index_exists(&PHPDBG_G(seek), (zend_ulong) phpdbg_user_execute_data(EG(current_execute_data))->opline)) {
688 		zend_hash_clean(&PHPDBG_G(seek));
689 		phpdbg_notice("Already at the end of the function");
690 		return SUCCESS;
691 	} else {
692 		PHPDBG_G(flags) |= PHPDBG_IN_LEAVE;
693 		return PHPDBG_LEAVE;
694 	}
695 } /* }}} */
696 
PHPDBG_COMMAND(frame)697 PHPDBG_COMMAND(frame) /* {{{ */
698 {
699 	if (!param) {
700 		phpdbg_notice("Currently in frame #%d", PHPDBG_G(frame).num);
701 	} else {
702 		phpdbg_switch_frame(param->num);
703 	}
704 
705 	return SUCCESS;
706 } /* }}} */
707 
phpdbg_handle_exception(void)708 static inline void phpdbg_handle_exception(void) /* {{{ */
709 {
710 	zend_object *ex = EG(exception);
711 	zend_string *msg, *file;
712 	zend_long line;
713 	zval rv, tmp;
714 
715 	EG(exception) = NULL;
716 
717 	zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
718 	file = zval_get_string(zend_read_property_ex(zend_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_FILE), /* silent */ true, &rv));
719 	line = zval_get_long(zend_read_property_ex(zend_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_LINE), /* silent */ true, &rv));
720 
721 	if (EG(exception)) {
722 		EG(exception) = NULL;
723 		msg = ZSTR_EMPTY_ALLOC();
724 	} else {
725 		zend_update_property_string(zend_get_exception_base(ex), ex, ZEND_STRL("string"), Z_STRVAL(tmp));
726 		zval_ptr_dtor(&tmp);
727 		msg = zval_get_string(zend_read_property_ex(zend_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), /* silent */ true, &rv));
728 	}
729 
730 	phpdbg_error("Uncaught %s in %s on line " ZEND_LONG_FMT, ZSTR_VAL(ex->ce->name), ZSTR_VAL(file), line);
731 	zend_string_release(file);
732 	phpdbg_writeln("%s", ZSTR_VAL(msg));
733 	zend_string_release(msg);
734 
735 	if (EG(prev_exception)) {
736 		OBJ_RELEASE(EG(prev_exception));
737 		EG(prev_exception) = 0;
738 	}
739 	OBJ_RELEASE(ex);
740 	EG(opline_before_exception) = NULL;
741 
742 	EG(exit_status) = 255;
743 } /* }}} */
744 
PHPDBG_COMMAND(run)745 PHPDBG_COMMAND(run) /* {{{ */
746 {
747 	if (PHPDBG_G(ops) || PHPDBG_G(exec)) {
748 		zend_execute_data *ex = EG(current_execute_data);
749 		bool restore = 1;
750 
751 		if (PHPDBG_G(in_execution)) {
752 			if (phpdbg_ask_user_permission("Do you really want to restart execution?") == SUCCESS) {
753 				phpdbg_startup_run++;
754 				phpdbg_clean(1, 1);
755 			}
756 			return SUCCESS;
757 		}
758 
759 		if (!PHPDBG_G(ops)) {
760 			if (phpdbg_compile() == FAILURE) {
761 				phpdbg_error("Failed to compile %s, cannot run", PHPDBG_G(exec));
762 				EG(exit_status) = FAILURE;
763 				goto out;
764 			}
765 		}
766 
767 		if (param && param->type != EMPTY_PARAM && param->len != 0) {
768 			char **argv = emalloc(5 * sizeof(char *));
769 			char *end = param->str + param->len, *p = param->str;
770 			char last_byte;
771 			int argc = 0;
772 			int i;
773 
774 			while (*end == '\r' || *end == '\n') *(end--) = 0;
775 			last_byte = end[1];
776 			end[1] = 0;
777 
778 			while (*p == ' ') p++;
779 			while (*p) {
780 				char sep = ' ';
781 				char *buf = emalloc(end - p + 2), *q = buf;
782 
783 				if (*p == '<') {
784 					/* use as STDIN */
785 					do p++; while (*p == ' ');
786 
787 					if (*p == '\'' || *p == '"') {
788 						sep = *(p++);
789 					}
790 					while (*p && *p != sep) {
791 						if (*p == '\\' && (p[1] == sep || p[1] == '\\')) {
792 							p++;
793 						}
794 						*(q++) = *(p++);
795 					}
796 					*(q++) = 0;
797 					if (*p) {
798 						do p++; while (*p == ' ');
799 					}
800 
801 					if (*p) {
802 						phpdbg_error("Invalid run command, cannot put further arguments after stdin");
803 						goto free_cmd;
804 					}
805 
806 					PHPDBG_G(stdin_file) = fopen(buf, "r");
807 					if (PHPDBG_G(stdin_file) == NULL) {
808 						phpdbg_error("Could not open '%s' for reading from stdin", buf);
809 						goto free_cmd;
810 					}
811 					efree(buf);
812 					phpdbg_register_file_handles();
813 					break;
814 				}
815 
816 				if (argc >= 4 && argc == (argc & -argc)) {
817 					argv = erealloc(argv, (argc * 2 + 1) * sizeof(char *));
818 				}
819 
820 				if (*p == '\'' || *p == '"') {
821 					sep = *(p++);
822 				}
823 				if (*p == '\\' && (p[1] == '<' || p[1] == '\'' || p[1] == '"')) {
824 					p++;
825 				}
826 				while (*p && *p != sep) {
827 					if (*p == '\\' && (p[1] == sep || p[1] == '\\' || (p[1] == '#' && sep == ' '))) {
828 						p++;
829 					}
830 					*(q++) = *(p++);
831 				}
832 				if (!*p && sep != ' ') {
833 					phpdbg_error("Invalid run command, unterminated escape sequence");
834 free_cmd:
835 					efree(buf);
836 					for (i = 0; i < argc; i++) {
837 						efree(argv[i]);
838 					}
839 					efree(argv);
840 					end[1] = last_byte;
841 					return SUCCESS;
842 				}
843 
844 				*(q++) = 0;
845 				argv[++argc] = erealloc(buf, q - buf);
846 
847 				if (*p) {
848 					do p++; while (*p == ' ');
849 				}
850 			}
851 			end[1] = last_byte;
852 
853 			argv[0] = SG(request_info).argv[0];
854 			for (i = SG(request_info).argc; --i;) {
855 				efree(SG(request_info).argv[i]);
856 			}
857 			efree(SG(request_info).argv);
858 			SG(request_info).argv = erealloc(argv, ++argc * sizeof(char *));
859 			SG(request_info).argc = argc;
860 
861 			php_build_argv(NULL, &PG(http_globals)[TRACK_VARS_SERVER]);
862 		}
863 
864 		/* clean up from last execution */
865 		if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
866 			zend_hash_clean(ex->symbol_table);
867 		} else {
868 			zend_rebuild_symbol_table();
869 		}
870 		PHPDBG_G(handled_exception) = NULL;
871 
872 		/* clean seek state */
873 		PHPDBG_G(flags) &= ~PHPDBG_SEEK_MASK;
874 		zend_hash_clean(&PHPDBG_G(seek));
875 
876 		/* reset hit counters */
877 		phpdbg_reset_breakpoints();
878 
879 		zend_try {
880 			PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
881 			PHPDBG_G(flags) |= PHPDBG_IS_RUNNING;
882 			zend_execute(PHPDBG_G(ops), &PHPDBG_G(retval));
883 			PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
884 		} zend_catch {
885 			PHPDBG_G(in_execution) = 0;
886 
887 			if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
888 				restore = 0;
889 			} else {
890 				zend_bailout();
891 			}
892 		} zend_end_try();
893 
894 		if (restore) {
895 			zend_exception_restore();
896 			zend_try {
897 				zend_try_exception_handler();
898 				PHPDBG_G(in_execution) = 1;
899 			} zend_catch {
900 				PHPDBG_G(in_execution) = 0;
901 
902 				if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
903 					zend_bailout();
904 				}
905 			} zend_end_try();
906 
907 			if (EG(exception) && !zend_is_unwind_exit(EG(exception))) {
908 				phpdbg_handle_exception();
909 			}
910 		}
911 
912 		PHPDBG_G(flags) &= ~PHPDBG_IS_RUNNING;
913 
914 		phpdbg_clean(1, 0);
915 	} else {
916 		phpdbg_error("Nothing to execute!");
917 	}
918 
919 out:
920 	PHPDBG_FRAME(num) = 0;
921 	return SUCCESS;
922 } /* }}} */
923 
phpdbg_output_ev_variable(char * name,size_t len,char * keyname,size_t keylen,HashTable * parent,zval * zv)924 int phpdbg_output_ev_variable(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv) /* {{{ */ {
925 	phpdbg_notice("Printing variable %.*s", (int) len, name);
926 
927 	zend_print_zval_r(zv, 0);
928 
929 	phpdbg_out("\n");
930 
931 	efree(name);
932 	efree(keyname);
933 
934 	return SUCCESS;
935 }
936 /* }}} */
937 
PHPDBG_COMMAND(ev)938 PHPDBG_COMMAND(ev) /* {{{ */
939 {
940 	bool stepping = ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING) == PHPDBG_IS_STEPPING);
941 	zval retval;
942 
943 	zend_execute_data *original_execute_data = EG(current_execute_data);
944 	zend_vm_stack original_stack = EG(vm_stack);
945 	zend_object *ex = NULL;
946 
947 	PHPDBG_OUTPUT_BACKUP();
948 
949 	original_stack->top = EG(vm_stack_top);
950 
951 	if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
952 		phpdbg_try_access {
953 			phpdbg_parse_variable(param->str, param->len, &EG(symbol_table), 0, phpdbg_output_ev_variable, 0);
954 		} phpdbg_catch_access {
955 			phpdbg_error("Could not fetch data, invalid data source");
956 		} phpdbg_end_try_access();
957 
958 		PHPDBG_OUTPUT_BACKUP_RESTORE();
959 		return SUCCESS;
960 	}
961 
962 	if (!(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
963 		PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
964 	}
965 
966 	/* disable stepping while eval() in progress */
967 	PHPDBG_G(flags) |= PHPDBG_IN_EVAL;
968 	zend_try {
969 		if (zend_eval_stringl(param->str, param->len, &retval, "eval()'d code") == SUCCESS) {
970 			if (EG(exception)) {
971 				ex = EG(exception);
972 				zend_exception_error(EG(exception), E_ERROR);
973 			} else {
974 				zend_print_zval_r(&retval, 0);
975 				phpdbg_out("\n");
976 				zval_ptr_dtor(&retval);
977 			}
978 		}
979 	} zend_catch {
980 		PHPDBG_G(unclean_eval) = 1;
981 		if (ex) {
982 			OBJ_RELEASE(ex);
983 		}
984 		EG(current_execute_data) = original_execute_data;
985 		EG(vm_stack_top) = original_stack->top;
986 		EG(vm_stack_end) = original_stack->end;
987 		EG(vm_stack) = original_stack;
988 		EG(exit_status) = 0;
989 	} zend_end_try();
990 
991 	PHPDBG_G(flags) &= ~PHPDBG_IN_EVAL;
992 
993 	/* switch stepping back on */
994 	if (stepping && !(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
995 		PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
996 	}
997 
998 	CG(unclean_shutdown) = 0;
999 
1000 	PHPDBG_OUTPUT_BACKUP_RESTORE();
1001 
1002 	return SUCCESS;
1003 } /* }}} */
1004 
PHPDBG_COMMAND(back)1005 PHPDBG_COMMAND(back) /* {{{ */
1006 {
1007 	if (!PHPDBG_G(in_execution)) {
1008 		phpdbg_error("Not executing!");
1009 		return SUCCESS;
1010 	}
1011 
1012 	if (!param) {
1013 		phpdbg_dump_backtrace(0);
1014 	} else {
1015 		phpdbg_dump_backtrace(param->num);
1016 	}
1017 
1018 	return SUCCESS;
1019 } /* }}} */
1020 
PHPDBG_COMMAND(generator)1021 PHPDBG_COMMAND(generator) /* {{{ */
1022 {
1023 	int i;
1024 
1025 	if (!PHPDBG_G(in_execution)) {
1026 		phpdbg_error("Not executing!");
1027 		return SUCCESS;
1028 	}
1029 
1030 	if (param) {
1031 		i = param->num;
1032 		zend_object **obj = EG(objects_store).object_buckets + i;
1033 		if (i < EG(objects_store).top && *obj && IS_OBJ_VALID(*obj) && (*obj)->ce == zend_ce_generator) {
1034 			zend_generator *gen = (zend_generator *) *obj;
1035 			if (gen->execute_data) {
1036 				if (zend_generator_get_current(gen)->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
1037 					phpdbg_error("Generator currently running");
1038 				} else {
1039 					phpdbg_open_generator_frame(gen);
1040 				}
1041 			} else {
1042 				phpdbg_error("Generator already closed");
1043 			}
1044 		} else {
1045 			phpdbg_error("Invalid object handle");
1046 		}
1047 	} else {
1048 		for (i = 0; i < EG(objects_store).top; i++) {
1049 			zend_object *obj = EG(objects_store).object_buckets[i];
1050 			if (obj && IS_OBJ_VALID(obj) && obj->ce == zend_ce_generator) {
1051 				zend_generator *gen = (zend_generator *) obj, *current = zend_generator_get_current(gen);
1052 				if (gen->execute_data) {
1053 					zend_string *s = phpdbg_compile_stackframe(gen->execute_data);
1054 					phpdbg_out("#%d: %.*s", i, (int) ZSTR_LEN(s), ZSTR_VAL(s));
1055 					zend_string_release(s);
1056 					if (gen != current) {
1057 						if (gen->node.parent != current) {
1058 							phpdbg_out(" with direct parent #%d and", gen->node.parent->std.handle);
1059 						}
1060 						phpdbg_out(" executing #%d currently", current->std.handle);
1061 					}
1062 					phpdbg_out("\n");
1063 				}
1064 			}
1065 		}
1066 	}
1067 
1068 	return SUCCESS;
1069 } /* }}} */
1070 
PHPDBG_COMMAND(print)1071 PHPDBG_COMMAND(print) /* {{{ */
1072 {
1073 	if (!param || param->type == EMPTY_PARAM) {
1074 		return phpdbg_do_print_stack(param);
1075 	} else switch (param->type) {
1076 		case STR_PARAM:
1077 			return phpdbg_do_print_func(param);
1078 		case METHOD_PARAM:
1079 			return phpdbg_do_print_method(param);
1080 		default:
1081 			phpdbg_error("Invalid arguments to print, expected nothing, function name or method name");
1082 			return SUCCESS;
1083 	}
1084 } /* }}} */
1085 
PHPDBG_COMMAND(info)1086 PHPDBG_COMMAND(info) /* {{{ */
1087 {
1088 	phpdbg_out("Execution Context Information\n\n");
1089 #ifdef HAVE_PHPDBG_READLINE
1090 # ifdef HAVE_LIBREADLINE
1091 	 phpdbg_writeln( "Readline   yes");
1092 # else
1093 	 phpdbg_writeln("Readline   no");
1094 # endif
1095 # ifdef HAVE_LIBEDIT
1096 	 phpdbg_writeln("Libedit    yes");
1097 # else
1098 	 phpdbg_writeln("Libedit    no");
1099 # endif
1100 #else
1101 	phpdbg_writeln("Readline   unavailable");
1102 #endif
1103 
1104 	phpdbg_writeln("Exec       %s", PHPDBG_G(exec) ? PHPDBG_G(exec) : "none");
1105 	phpdbg_writeln("Compiled   %s", PHPDBG_G(ops) ? "yes" : "no");
1106 	phpdbg_writeln("Stepping   %s", (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ? "on" : "off");
1107 	phpdbg_writeln("Quietness  %s", (PHPDBG_G(flags) & PHPDBG_IS_QUIET) ? "on" : "off");
1108 
1109 	if (PHPDBG_G(ops)) {
1110 		phpdbg_writeln("Opcodes    %d", PHPDBG_G(ops)->last);
1111 		phpdbg_writeln("Variables  %d", PHPDBG_G(ops)->last_var ? PHPDBG_G(ops)->last_var - 1 : 0);
1112 	}
1113 
1114 	phpdbg_writeln("Executing  %s", PHPDBG_G(in_execution) ? "yes" : "no");
1115 	if (PHPDBG_G(in_execution)) {
1116 		phpdbg_writeln("VM Return  %d", PHPDBG_G(vmret));
1117 	}
1118 
1119 	phpdbg_writeln("Classes    %d", zend_hash_num_elements(EG(class_table)));
1120 	phpdbg_writeln("Functions  %d", zend_hash_num_elements(EG(function_table)));
1121 	phpdbg_writeln("Constants  %d", zend_hash_num_elements(EG(zend_constants)));
1122 	phpdbg_writeln("Included   %d", zend_hash_num_elements(&EG(included_files)));
1123 
1124 	return SUCCESS;
1125 } /* }}} */
1126 
PHPDBG_COMMAND(set)1127 PHPDBG_COMMAND(set) /* {{{ */
1128 {
1129 	phpdbg_error("No set command selected!");
1130 
1131 	return SUCCESS;
1132 } /* }}} */
1133 
PHPDBG_COMMAND(break)1134 PHPDBG_COMMAND(break) /* {{{ */
1135 {
1136 	if (!param) {
1137 		if (PHPDBG_G(exec)) {
1138 			phpdbg_set_breakpoint_file(
1139 				zend_get_executed_filename(),
1140 				strlen(zend_get_executed_filename()),
1141 				zend_get_executed_lineno());
1142 		} else {
1143 			phpdbg_error("Execution context not set!");
1144 		}
1145 	} else switch (param->type) {
1146 		case ADDR_PARAM:
1147 			phpdbg_set_breakpoint_opline(param->addr);
1148 			break;
1149 		case NUMERIC_PARAM:
1150 			if (PHPDBG_G(exec)) {
1151 				phpdbg_set_breakpoint_file(phpdbg_current_file(), strlen(phpdbg_current_file()), param->num);
1152 			} else {
1153 				phpdbg_error("Execution context not set!");
1154 			}
1155 			break;
1156 		case METHOD_PARAM:
1157 			phpdbg_set_breakpoint_method(param->method.class, param->method.name);
1158 			break;
1159 		case NUMERIC_METHOD_PARAM:
1160 			phpdbg_set_breakpoint_method_opline(param->method.class, param->method.name, param->num);
1161 			break;
1162 		case NUMERIC_FUNCTION_PARAM:
1163 			phpdbg_set_breakpoint_function_opline(param->str, param->num);
1164 			break;
1165 		case FILE_PARAM:
1166 			phpdbg_set_breakpoint_file(param->file.name, 0, param->file.line);
1167 			break;
1168 		case NUMERIC_FILE_PARAM:
1169 			phpdbg_set_breakpoint_file_opline(param->file.name, param->file.line);
1170 			break;
1171 		case COND_PARAM:
1172 			phpdbg_set_breakpoint_expression(param->str, param->len);
1173 			break;
1174 		case STR_PARAM:
1175 			phpdbg_set_breakpoint_symbol(param->str, param->len);
1176 			break;
1177 		case OP_PARAM:
1178 			phpdbg_set_breakpoint_opcode(param->str, param->len);
1179 			break;
1180 
1181 		phpdbg_default_switch_case();
1182 	}
1183 
1184 	return SUCCESS;
1185 } /* }}} */
1186 
PHPDBG_COMMAND(sh)1187 PHPDBG_COMMAND(sh) /* {{{ */
1188 {
1189 	FILE *fd = NULL;
1190 	if ((fd=VCWD_POPEN((char*)param->str, "w"))) {
1191 		/* TODO: do something perhaps ?? do we want input ?? */
1192 		pclose(fd);
1193 	} else {
1194 		phpdbg_error("Failed to execute %s", param->str);
1195 	}
1196 
1197 	return SUCCESS;
1198 } /* }}} */
1199 
add_module_info(zend_module_entry * module)1200 static int add_module_info(zend_module_entry *module) /* {{{ */ {
1201 	phpdbg_write("%s\n", module->name);
1202 	return 0;
1203 }
1204 /* }}} */
1205 
add_zendext_info(zend_extension * ext)1206 static void add_zendext_info(zend_extension *ext) /* {{{ */ {
1207 	phpdbg_write("%s\n", ext->name);
1208 }
1209 /* }}} */
1210 
1211 #ifdef HAVE_LIBDL
phpdbg_load_module_or_extension(char ** path,const char ** name)1212 PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, const char **name) /* {{{ */ {
1213 	DL_HANDLE handle;
1214 	char *extension_dir;
1215 
1216 	extension_dir = INI_STR("extension_dir");
1217 
1218 	if (strchr(*path, '/') != NULL || strchr(*path, DEFAULT_SLASH) != NULL) {
1219 		/* path is fine */
1220 	} else if (extension_dir && extension_dir[0]) {
1221 		char *libpath;
1222 		int extension_dir_len = strlen(extension_dir);
1223 		if (IS_SLASH(extension_dir[extension_dir_len-1])) {
1224 			spprintf(&libpath, 0, "%s%s", extension_dir, *path); /* SAFE */
1225 		} else {
1226 			spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, *path); /* SAFE */
1227 		}
1228 		efree(*path);
1229 		*path = libpath;
1230 	} else {
1231 		phpdbg_error("Not a full path given or extension_dir ini setting is not set");
1232 
1233 		return NULL;
1234 	}
1235 
1236 	handle = DL_LOAD(*path);
1237 
1238 	if (!handle) {
1239 #ifdef PHP_WIN32
1240 		char *err = GET_DL_ERROR();
1241 		if (err && err[0]) {
1242 			phpdbg_error("%s", err);
1243 			php_win32_error_msg_free(err);
1244 		} else {
1245 			phpdbg_error("Unknown reason");
1246 		}
1247 #else
1248 		phpdbg_error("%s", GET_DL_ERROR());
1249 #endif
1250 		return NULL;
1251 	}
1252 
1253 #if ZEND_EXTENSIONS_SUPPORT
1254 	do {
1255 		zend_extension *new_extension;
1256 
1257 		const zend_extension_version_info *extension_version_info = (const zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
1258 		if (!extension_version_info) {
1259 			extension_version_info = (const zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
1260 		}
1261 		new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
1262 		if (!new_extension) {
1263 			new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
1264 		}
1265 		if (!extension_version_info || !new_extension) {
1266 			break;
1267 		}
1268 		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)) {
1269 			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);
1270 
1271 			goto quit;
1272 		} 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)) {
1273 			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);
1274 
1275 			goto quit;
1276 		}
1277 
1278 		*name = new_extension->name;
1279 
1280 		zend_register_extension(new_extension, handle);
1281 
1282 		if (new_extension->startup) {
1283 			if (new_extension->startup(new_extension) != SUCCESS) {
1284 				phpdbg_error("Unable to startup Zend extension %s", new_extension->name);
1285 
1286 				goto quit;
1287 			}
1288 			zend_append_version_info(new_extension);
1289 		}
1290 
1291 		return "Zend extension";
1292 	} while (0);
1293 #endif
1294 
1295 	do {
1296 		zend_module_entry *module_entry;
1297 		zend_module_entry *(*get_module)(void);
1298 
1299 		get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
1300 		if (!get_module) {
1301 			get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
1302 		}
1303 
1304 		if (!get_module) {
1305 			break;
1306 		}
1307 
1308 		module_entry = get_module();
1309 		*name = module_entry->name;
1310 
1311 		if (strcmp(ZEND_EXTENSION_BUILD_ID, module_entry->build_id)) {
1312 			phpdbg_error("%s was built with configuration %s, whereas running engine is %s", module_entry->name, module_entry->build_id, ZEND_EXTENSION_BUILD_ID);
1313 
1314 			goto quit;
1315 		}
1316 
1317 		if ((module_entry = zend_register_module_ex(module_entry, MODULE_PERSISTENT)) == NULL) {
1318 			phpdbg_error("Unable to register module %s", *name);
1319 
1320 			goto quit;
1321 		}
1322 
1323 		module_entry->handle = handle;
1324 
1325 		if (zend_startup_module_ex(module_entry) == FAILURE) {
1326 			phpdbg_error("Unable to startup module %s", module_entry->name);
1327 
1328 			goto quit;
1329 		}
1330 
1331 		if (module_entry->request_startup_func) {
1332 			if (module_entry->request_startup_func(MODULE_PERSISTENT, module_entry->module_number) == FAILURE) {
1333 				phpdbg_error("Unable to initialize module %s", module_entry->name);
1334 
1335 				goto quit;
1336 			}
1337 		}
1338 
1339 		return "module";
1340 	} while (0);
1341 
1342 	phpdbg_error("This shared object is nor a Zend extension nor a module");
1343 
1344 quit:
1345 	DL_UNLOAD(handle);
1346 	return NULL;
1347 }
1348 /* }}} */
1349 #endif
1350 
PHPDBG_COMMAND(dl)1351 PHPDBG_COMMAND(dl) /* {{{ */
1352 {
1353 	const char *type, *name;
1354 	char *path;
1355 
1356 	if (!param || param->type == EMPTY_PARAM) {
1357 		phpdbg_notice("Zend extensions");
1358 		zend_llist_apply(&zend_extensions, (llist_apply_func_t) add_zendext_info);
1359 		phpdbg_out("\n");
1360 		phpdbg_notice("Modules");
1361 		zend_hash_apply(&module_registry, (apply_func_t) add_module_info);
1362 	} else switch (param->type) {
1363 		case STR_PARAM:
1364 #ifdef HAVE_LIBDL
1365 			path = estrndup(param->str, param->len);
1366 
1367 			phpdbg_activate_err_buf(1);
1368 			if ((type = phpdbg_load_module_or_extension(&path, &name)) == NULL) {
1369 				phpdbg_error("Could not load %s, not found or invalid zend extension / module: %s", path, PHPDBG_G(err_buf).msg);
1370 			} else {
1371 				phpdbg_notice("Successfully loaded the %s %s at path %s", type, name, path);
1372 			}
1373 			phpdbg_activate_err_buf(0);
1374 			phpdbg_free_err_buf();
1375 			efree(path);
1376 #else
1377 			phpdbg_error("Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str);
1378 #endif
1379 			break;
1380 
1381 		phpdbg_default_switch_case();
1382 	}
1383 
1384 	return SUCCESS;
1385 } /* }}} */
1386 
PHPDBG_COMMAND(source)1387 PHPDBG_COMMAND(source) /* {{{ */
1388 {
1389 	zend_stat_t sb = {0};
1390 
1391 	if (VCWD_STAT(param->str, &sb) != -1) {
1392 		phpdbg_try_file_init(param->str, param->len, 0);
1393 	} else {
1394 		phpdbg_error("Failed to stat %s, file does not exist", param->str);
1395 	}
1396 
1397 	return SUCCESS;
1398 } /* }}} */
1399 
PHPDBG_COMMAND(export)1400 PHPDBG_COMMAND(export) /* {{{ */
1401 {
1402 	FILE *handle = VCWD_FOPEN(param->str, "w+");
1403 
1404 	if (handle) {
1405 		phpdbg_export_breakpoints(handle);
1406 		fclose(handle);
1407 	} else {
1408 		phpdbg_error("Failed to open or create %s, check path and permissions", param->str);
1409 	}
1410 
1411 	return SUCCESS;
1412 } /* }}} */
1413 
PHPDBG_COMMAND(register)1414 PHPDBG_COMMAND(register) /* {{{ */
1415 {
1416 	zend_function *function;
1417 	char *lcname = zend_str_tolower_dup(param->str, param->len);
1418 	size_t lcname_len = param->len;
1419 
1420 	if (!zend_hash_str_exists(&PHPDBG_G(registered), lcname, lcname_len)) {
1421 		if ((function = zend_hash_str_find_ptr(EG(function_table), lcname, lcname_len))) {
1422 			zend_hash_str_update_ptr(&PHPDBG_G(registered), lcname, lcname_len, function);
1423 			function_add_ref(function);
1424 
1425 			phpdbg_notice("Registered %s", lcname);
1426 		} else {
1427 			phpdbg_error("The requested function (%s) could not be found", param->str);
1428 		}
1429 	} else {
1430 		phpdbg_error("The requested name (%s) is already in use", lcname);
1431 	}
1432 
1433 	efree(lcname);
1434 	return SUCCESS;
1435 } /* }}} */
1436 
PHPDBG_COMMAND(quit)1437 PHPDBG_COMMAND(quit) /* {{{ */
1438 {
1439 	PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
1440 	PHPDBG_G(flags) &= ~PHPDBG_IS_CLEANING;
1441 
1442 	return SUCCESS;
1443 } /* }}} */
1444 
PHPDBG_COMMAND(clean)1445 PHPDBG_COMMAND(clean) /* {{{ */
1446 {
1447 	if (PHPDBG_G(in_execution)) {
1448 		if (phpdbg_ask_user_permission("Do you really want to clean your current environment?") == FAILURE) {
1449 			return SUCCESS;
1450 		}
1451 	}
1452 
1453 	phpdbg_out("Cleaning Execution Environment\n");
1454 
1455 	phpdbg_writeln("Classes    %d", zend_hash_num_elements(EG(class_table)));
1456 	phpdbg_writeln("Functions  %d", zend_hash_num_elements(EG(function_table)));
1457 	phpdbg_writeln("Constants  %d", zend_hash_num_elements(EG(zend_constants)));
1458 	phpdbg_writeln("Includes   %d", zend_hash_num_elements(&EG(included_files)));
1459 
1460 	phpdbg_clean(1, 0);
1461 
1462 	return SUCCESS;
1463 } /* }}} */
1464 
PHPDBG_COMMAND(clear)1465 PHPDBG_COMMAND(clear) /* {{{ */
1466 {
1467 	phpdbg_out("Clearing Breakpoints\n");
1468 
1469 	phpdbg_writeln("File              %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]));
1470 	phpdbg_writeln("Functions         %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]));
1471 	phpdbg_writeln("Methods           %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]));
1472 	phpdbg_writeln("Oplines           %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]));
1473 	phpdbg_writeln("File oplines      %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE]));
1474 	phpdbg_writeln("Function oplines  %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE]));
1475 	phpdbg_writeln("Method oplines    %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE]));
1476 	phpdbg_writeln("Conditionals      %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_COND]));
1477 
1478 	phpdbg_clear_breakpoints();
1479 
1480 	return SUCCESS;
1481 } /* }}} */
1482 
PHPDBG_COMMAND(list)1483 PHPDBG_COMMAND(list) /* {{{ */
1484 {
1485 	if (!param) {
1486 		return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
1487 	} else switch (param->type) {
1488 		case NUMERIC_PARAM:
1489 			return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
1490 
1491 		case FILE_PARAM:
1492 			return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
1493 
1494 		case STR_PARAM:
1495 			phpdbg_list_function_byname(param->str, param->len);
1496 			break;
1497 
1498 		case METHOD_PARAM:
1499 			return PHPDBG_LIST_HANDLER(method)(PHPDBG_COMMAND_ARGS);
1500 
1501 		phpdbg_default_switch_case();
1502 	}
1503 
1504 	return SUCCESS;
1505 } /* }}} */
1506 
PHPDBG_COMMAND(watch)1507 PHPDBG_COMMAND(watch) /* {{{ */
1508 {
1509 	if (!param || param->type == EMPTY_PARAM) {
1510 		phpdbg_list_watchpoints();
1511 	} else switch (param->type) {
1512 		case STR_PARAM:
1513 			phpdbg_create_var_watchpoint(param->str, param->len);
1514 			break;
1515 
1516 		phpdbg_default_switch_case();
1517 	}
1518 
1519 	return SUCCESS;
1520 } /* }}} */
1521 
phpdbg_interactive(bool allow_async_unsafe,char * input)1522 int phpdbg_interactive(bool allow_async_unsafe, char *input) /* {{{ */
1523 {
1524 	int ret = SUCCESS;
1525 	phpdbg_param_t stack;
1526 
1527 	PHPDBG_G(flags) |= PHPDBG_IS_INTERACTIVE;
1528 
1529 	while (ret == SUCCESS || ret == FAILURE) {
1530 		if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1531 			zend_bailout();
1532 		}
1533 
1534 		if (!input && !(input = phpdbg_read_input(NULL))) {
1535 			break;
1536 		}
1537 
1538 
1539 		phpdbg_init_param(&stack, STACK_PARAM);
1540 
1541 		if (phpdbg_do_parse(&stack, input) <= 0) {
1542 			phpdbg_activate_err_buf(1);
1543 
1544 			zend_try {
1545 				ret = phpdbg_stack_execute(&stack, allow_async_unsafe);
1546 			} zend_catch {
1547 				phpdbg_stack_free(&stack);
1548 				phpdbg_destroy_input(&input);
1549 				/* TODO: should use proper unwinding instead of bailing out */
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 #ifdef ZEND_CHECK_STACK_LIMIT
1656 	if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
1657 		zend_call_stack_size_error();
1658 		/* No opline was executed before exception */
1659 		EG(opline_before_exception) = NULL;
1660 		/* Fall through to handle exception below. */
1661 	}
1662 #endif /* ZEND_CHECK_STACK_LIMIT */
1663 
1664 	while (1) {
1665 		zend_object *exception = EG(exception);
1666 
1667 		if ((PHPDBG_G(flags) & PHPDBG_BP_RESOLVE_MASK)) {
1668 			/* resolve nth opline breakpoints */
1669 			phpdbg_resolve_op_array_breaks(&execute_data->func->op_array);
1670 		}
1671 
1672 #ifdef ZEND_WIN32
1673 		if (zend_atomic_bool_load_ex(&EG(timed_out))) {
1674 			zend_timeout();
1675 		}
1676 #endif
1677 
1678 		if (exception && zend_is_unwind_exit(exception)) {
1679 			/* Restore bailout based exit. */
1680 			zend_bailout();
1681 		}
1682 
1683 		if (PHPDBG_G(flags) & PHPDBG_PREVENT_INTERACTIVE) {
1684 			phpdbg_print_opline(execute_data, 0);
1685 			goto next;
1686 		}
1687 
1688 		/* check for uncaught exceptions */
1689 		if (exception && PHPDBG_G(handled_exception) != exception && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1690 			zend_execute_data *prev_ex = execute_data;
1691 
1692 			do {
1693 				prev_ex = zend_generator_check_placeholder_frame(prev_ex);
1694 				/* assuming that no internal functions will silently swallow exceptions ... */
1695 				if (!prev_ex->func || !ZEND_USER_CODE(prev_ex->func->common.type)) {
1696 					continue;
1697 				}
1698 
1699 				if (phpdbg_check_caught_ex(prev_ex, exception)) {
1700 					goto ex_is_caught;
1701 				}
1702 			} while ((prev_ex = prev_ex->prev_execute_data));
1703 
1704 			PHPDBG_G(handled_exception) = exception;
1705 
1706 			zval rv;
1707 			zend_string *file = zval_get_string(zend_read_property_ex(zend_get_exception_base(exception), exception, ZSTR_KNOWN(ZEND_STR_FILE), /* silent */ true, &rv));
1708 			zend_long line = zval_get_long(zend_read_property_ex(zend_get_exception_base(exception), exception, ZSTR_KNOWN(ZEND_STR_LINE), /* silent */ true, &rv));
1709 			zend_string *msg = zval_get_string(zend_read_property_ex(zend_get_exception_base(exception), exception, ZSTR_KNOWN(ZEND_STR_MESSAGE), /* silent */ true, &rv));
1710 
1711 			phpdbg_error("Uncaught %s in %s on line " ZEND_LONG_FMT ": %.*s",
1712 				ZSTR_VAL(exception->ce->name), ZSTR_VAL(file), line,
1713 				ZSTR_LEN(msg) < 80 ? (int) ZSTR_LEN(msg) : 80, ZSTR_VAL(msg));
1714 			zend_string_release(msg);
1715 			zend_string_release(file);
1716 
1717 			DO_INTERACTIVE(1);
1718 		}
1719 ex_is_caught:
1720 
1721 		/* allow conditional breakpoints and initialization to access the vm uninterrupted */
1722 		if (PHPDBG_G(flags) & (PHPDBG_IN_COND_BP | PHPDBG_IS_INITIALIZING)) {
1723 			/* skip possible breakpoints */
1724 			goto next;
1725 		}
1726 
1727 		/* not while in conditionals */
1728 		phpdbg_print_opline(execute_data, 0);
1729 
1730 		/* perform seek operation */
1731 		if ((PHPDBG_G(flags) & PHPDBG_SEEK_MASK) && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
1732 			/* current address */
1733 			zend_ulong address = (zend_ulong) execute_data->opline;
1734 
1735 			if (PHPDBG_G(seek_ex) != execute_data) {
1736 				if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) {
1737 					goto stepping;
1738 				}
1739 				goto next;
1740 			}
1741 
1742 #define INDEX_EXISTS_CHECK (zend_hash_index_exists(&PHPDBG_G(seek), address) || (exception && phpdbg_check_caught_ex(execute_data, exception) == 0))
1743 
1744 			/* run to next line */
1745 			if (PHPDBG_G(flags) & PHPDBG_IN_UNTIL) {
1746 				if (INDEX_EXISTS_CHECK) {
1747 					PHPDBG_G(flags) &= ~PHPDBG_IN_UNTIL;
1748 					zend_hash_clean(&PHPDBG_G(seek));
1749 				} else {
1750 					/* skip possible breakpoints */
1751 					goto next;
1752 				}
1753 			}
1754 
1755 			/* run to finish */
1756 			if (PHPDBG_G(flags) & PHPDBG_IN_FINISH) {
1757 				if (INDEX_EXISTS_CHECK) {
1758 					PHPDBG_G(flags) &= ~PHPDBG_IN_FINISH;
1759 					zend_hash_clean(&PHPDBG_G(seek));
1760 				}
1761 				/* skip possible breakpoints */
1762 				goto next;
1763 			}
1764 
1765 			/* break for leave */
1766 			if (PHPDBG_G(flags) & PHPDBG_IN_LEAVE) {
1767 				if (INDEX_EXISTS_CHECK) {
1768 					PHPDBG_G(flags) &= ~PHPDBG_IN_LEAVE;
1769 					zend_hash_clean(&PHPDBG_G(seek));
1770 					phpdbg_notice("Breaking for leave at %s:%u",
1771 						zend_get_executed_filename(),
1772 						zend_get_executed_lineno()
1773 					);
1774 					DO_INTERACTIVE(1);
1775 				} else {
1776 					/* skip possible breakpoints */
1777 					goto next;
1778 				}
1779 			}
1780 		}
1781 
1782 		if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING && (PHPDBG_G(flags) & PHPDBG_STEP_OPCODE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1783 stepping:
1784 			PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
1785 			DO_INTERACTIVE(1);
1786 		}
1787 
1788 		/* check if some watchpoint was hit */
1789 		{
1790 			if (phpdbg_print_changed_zvals() == SUCCESS) {
1791 				DO_INTERACTIVE(1);
1792 			}
1793 		}
1794 
1795 		/* search for breakpoints */
1796 		{
1797 			phpdbg_breakbase_t *brake;
1798 
1799 			if ((PHPDBG_G(flags) & PHPDBG_BP_MASK)
1800 			    && (brake = phpdbg_find_breakpoint(execute_data))
1801 			    && (brake->type != PHPDBG_BREAK_FILE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1802 				phpdbg_hit_breakpoint(brake, 1);
1803 				DO_INTERACTIVE(1);
1804 			}
1805 		}
1806 
1807 		if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
1808 			PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
1809 
1810 			phpdbg_out("\n");
1811 			phpdbg_notice("Program received signal SIGINT");
1812 			DO_INTERACTIVE(1);
1813 		}
1814 
1815 next:
1816 
1817 		PHPDBG_G(last_line) = execute_data->opline->lineno;
1818 
1819 		/* stupid hack to make zend_do_fcall_common_helper return ZEND_VM_ENTER() instead of recursively calling zend_execute() and eventually segfaulting */
1820 		if ((execute_data->opline->opcode == ZEND_DO_FCALL ||
1821 		     execute_data->opline->opcode == ZEND_DO_UCALL ||
1822 		     execute_data->opline->opcode == ZEND_DO_FCALL_BY_NAME) &&
1823 		     execute_data->call->func->type == ZEND_USER_FUNCTION) {
1824 			zend_execute_ex = execute_ex;
1825 		}
1826 		PHPDBG_G(vmret) = zend_vm_call_opcode_handler(execute_data);
1827 		zend_execute_ex = phpdbg_execute_ex;
1828 
1829 		if (PHPDBG_G(vmret) != 0) {
1830 			if (PHPDBG_G(vmret) < 0) {
1831 				PHPDBG_G(in_execution) = original_in_execution;
1832 				return;
1833 			} else {
1834 				execute_data = EG(current_execute_data);
1835 			}
1836 		}
1837 	}
1838 	zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
1839 } /* }}} */
1840 
1841 /* only if *not* interactive and while executing */
phpdbg_force_interruption(void)1842 void phpdbg_force_interruption(void) /* {{{ */ {
1843 	zend_object *exception = EG(exception);
1844 	zend_execute_data *data = EG(current_execute_data); /* should be always readable if not NULL */
1845 
1846 	PHPDBG_G(flags) |= PHPDBG_IN_SIGNAL_HANDLER;
1847 
1848 	if (data) {
1849 		if (data->func) {
1850 			if (ZEND_USER_CODE(data->func->type)) {
1851 				phpdbg_notice("Current opline: %p (op #%u) in %s:%u",
1852 				    data->opline,
1853 				    (uint32_t) (data->opline - data->func->op_array.opcodes),
1854 				    data->func->op_array.filename->val,
1855 				    data->opline->lineno);
1856 			} else if (data->func->internal_function.function_name) {
1857 				phpdbg_notice("Current opline: in internal function %s",
1858 				    data->func->internal_function.function_name->val);
1859 			} else {
1860 				phpdbg_notice("Current opline: executing internal code");
1861 			}
1862 		} else {
1863 			phpdbg_notice("Current opline: %p (op_array information unavailable)",
1864 			    data->opline);
1865 		}
1866 	} else {
1867 		phpdbg_notice("No information available about executing context");
1868 	}
1869 
1870 	DO_INTERACTIVE(0);
1871 
1872 next:
1873 	PHPDBG_G(flags) &= ~PHPDBG_IN_SIGNAL_HANDLER;
1874 
1875 	if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1876 		zend_bailout();
1877 	}
1878 }
1879 /* }}} */
1880