xref: /PHP-5.6/sapi/phpdbg/phpdbg_prompt.c (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Felipe Pena <felipe@php.net>                                |
16    | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
17    | Authors: Bob Weinand <bwoebi@php.net>                                |
18    +----------------------------------------------------------------------+
19 */
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include "zend.h"
24 #include "zend_compile.h"
25 #include "phpdbg.h"
26 #include "phpdbg_help.h"
27 #include "phpdbg_print.h"
28 #include "phpdbg_info.h"
29 #include "phpdbg_break.h"
30 #include "phpdbg_bp.h"
31 #include "phpdbg_opcode.h"
32 #include "phpdbg_list.h"
33 #include "phpdbg_utils.h"
34 #include "phpdbg_prompt.h"
35 #include "phpdbg_cmd.h"
36 #include "phpdbg_set.h"
37 #include "phpdbg_frame.h"
38 #include "phpdbg_lexer.h"
39 #include "phpdbg_parser.h"
40 
41 /* {{{ command declarations */
42 const phpdbg_command_t phpdbg_prompt_commands[] = {
43 	PHPDBG_COMMAND_D(exec,    "set execution context",                    'e', NULL, "s"),
44 	PHPDBG_COMMAND_D(step,    "step through execution",                   's', NULL, 0),
45 	PHPDBG_COMMAND_D(continue,"continue execution",                       'c', NULL, 0),
46 	PHPDBG_COMMAND_D(run,     "attempt execution",                        'r', NULL, "|s"),
47 	PHPDBG_COMMAND_D(ev,      "evaluate some code",                        0, NULL, "i"),
48 	PHPDBG_COMMAND_D(until,   "continue past the current line",           'u', NULL, 0),
49 	PHPDBG_COMMAND_D(finish,  "continue past the end of the stack",       'F', NULL, 0),
50 	PHPDBG_COMMAND_D(leave,   "continue until the end of the stack",      'L', NULL, 0),
51 	PHPDBG_COMMAND_D(print,   "print something",                          'p', phpdbg_print_commands, 0),
52 	PHPDBG_COMMAND_D(break,   "set breakpoint",                           'b', phpdbg_break_commands, "|*c"),
53 	PHPDBG_COMMAND_D(back,    "show trace",                               't', NULL, "|n"),
54 	PHPDBG_COMMAND_D(frame,   "switch to a frame",                        'f', NULL, "|n"),
55 	PHPDBG_COMMAND_D(list,    "lists some code",                          'l', phpdbg_list_commands, "*"),
56 	PHPDBG_COMMAND_D(info,    "displays some informations",               'i', phpdbg_info_commands, "s"),
57 	PHPDBG_COMMAND_D(clean,   "clean the execution environment",          'X', NULL, 0),
58 	PHPDBG_COMMAND_D(clear,   "clear breakpoints",                        'C', NULL, 0),
59 	PHPDBG_COMMAND_D(help,    "show help menu",                           'h', phpdbg_help_commands, "|s"),
60 	PHPDBG_COMMAND_D(set,     "set phpdbg configuration",                 'S', phpdbg_set_commands,   "s"),
61 	PHPDBG_COMMAND_D(register,"register a function",                      'R', NULL, "s"),
62 	PHPDBG_COMMAND_D(source,  "execute a phpdbginit",                     '<', NULL, "s"),
63 	PHPDBG_COMMAND_D(export,  "export breaks to a .phpdbginit script",    '>', NULL, "s"),
64 	PHPDBG_COMMAND_D(sh,   	  "shell a command",                           0, NULL, "i"),
65 	PHPDBG_COMMAND_D(quit,    "exit phpdbg",                              'q', NULL, 0),
66 	PHPDBG_COMMAND_D(watch,   "set watchpoint",                           'w', phpdbg_watch_commands, "|ss"),
67 	PHPDBG_END_COMMAND
68 }; /* }}} */
69 
70 ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
71 
phpdbg_call_register(phpdbg_param_t * stack TSRMLS_DC)72 static inline int phpdbg_call_register(phpdbg_param_t *stack TSRMLS_DC) /* {{{ */
73 {
74 	phpdbg_param_t *name = NULL;
75 
76 	if (stack->type == STACK_PARAM) {
77 		name = stack->next;
78 
79 		if (!name || name->type != STR_PARAM) {
80 			return FAILURE;
81 		}
82 
83 		if (zend_hash_exists(
84 				&PHPDBG_G(registered), name->str, name->len+1)) {
85 
86 			zval fname, *fretval;
87 			zend_fcall_info fci;
88 
89 			ZVAL_STRINGL(&fname, name->str, name->len, 1);
90 
91 			memset(&fci, 0, sizeof(zend_fcall_info));
92 
93 			fci.size = sizeof(zend_fcall_info);
94 			fci.function_table = &PHPDBG_G(registered);
95 			fci.function_name = &fname;
96 			fci.symbol_table = EG(active_symbol_table);
97 			fci.object_ptr = NULL;
98 			fci.retval_ptr_ptr = &fretval;
99 			fci.no_separation = 1;
100 
101 			if (name->next) {
102 				zval params;
103 				phpdbg_param_t *next = name->next;
104 
105 				array_init(&params);
106 
107 				while (next) {
108 					char *buffered = NULL;
109 
110 					switch (next->type) {
111 						case OP_PARAM:
112 						case COND_PARAM:
113 						case STR_PARAM:
114 							add_next_index_stringl(
115 								&params,
116 								next->str,
117 								next->len, 1);
118 						break;
119 
120 						case NUMERIC_PARAM:
121 							add_next_index_long(&params, next->num);
122 						break;
123 
124 						case METHOD_PARAM:
125 							spprintf(&buffered, 0, "%s::%s",
126 								next->method.class, next->method.name);
127 							add_next_index_string(&params, buffered, 0);
128 						break;
129 
130 						case NUMERIC_METHOD_PARAM:
131 							spprintf(&buffered, 0, "%s::%s#%ld",
132 								next->method.class, next->method.name, next->num);
133 							add_next_index_string(&params, buffered, 0);
134 						break;
135 
136 						case NUMERIC_FUNCTION_PARAM:
137 							spprintf(&buffered, 0, "%s#%ld",
138 								next->str, next->num);
139 							add_next_index_string(&params, buffered, 0);
140 						break;
141 
142 						case FILE_PARAM:
143 							spprintf(&buffered, 0, "%s:%ld",
144 								next->file.name, next->file.line);
145 							add_next_index_string(&params, buffered, 0);
146 						break;
147 
148 						case NUMERIC_FILE_PARAM:
149 							spprintf(&buffered, 0, "%s:#%ld",
150 								next->file.name, next->file.line);
151 							add_next_index_string(&params, buffered, 0);
152 						break;
153 
154 						default: {
155 							/* not yet */
156 						}
157 					}
158 
159 					next = next->next;
160 				}
161 
162 				zend_fcall_info_args(&fci, &params TSRMLS_CC);
163 			} else {
164 				fci.params = NULL;
165 				fci.param_count = 0;
166 			}
167 
168 			phpdbg_debug(
169 				"created %d params from arguments",
170 				fci.param_count);
171 
172 			zend_call_function(&fci, NULL TSRMLS_CC);
173 
174 			if (fretval) {
175 				zend_print_zval_r(
176 					fretval, 0 TSRMLS_CC);
177 				phpdbg_writeln(EMPTY);
178 			}
179 
180 			zval_dtor(&fname);
181 
182 			return SUCCESS;
183 		}
184 	}
185 
186 	return FAILURE;
187 } /* }}} */
188 
phpdbg_try_file_init(char * init_file,size_t init_file_len,zend_bool free_init TSRMLS_DC)189 void phpdbg_try_file_init(char *init_file, size_t init_file_len, zend_bool free_init TSRMLS_DC) /* {{{ */
190 {
191 	struct stat sb;
192 
193 	if (init_file && VCWD_STAT(init_file, &sb) != -1) {
194 		FILE *fp = fopen(init_file, "r");
195 		if (fp) {
196 			int line = 1;
197 
198 			char cmd[PHPDBG_MAX_CMD];
199 			size_t cmd_len = 0L;
200 			char *code = NULL;
201 			size_t code_len = 0L;
202 			zend_bool in_code = 0;
203 
204 			while (fgets(cmd, PHPDBG_MAX_CMD, fp) != NULL) {
205 				cmd_len = strlen(cmd)-1;
206 
207 				while (cmd_len > 0L && isspace(cmd[cmd_len-1]))
208 					cmd_len--;
209 
210 				cmd[cmd_len] = '\0';
211 
212 				if (*cmd && cmd_len > 0L && cmd[0] != '#') {
213 					if (cmd_len == 2) {
214 						if (memcmp(cmd, "<:", sizeof("<:")-1) == SUCCESS) {
215 							in_code = 1;
216 							goto next_line;
217 						} else {
218 							if (memcmp(cmd, ":>", sizeof(":>")-1) == SUCCESS) {
219 								in_code = 0;
220 								code[code_len] = '\0';
221 								{
222 									zend_eval_stringl(
223 										code, code_len, NULL, "phpdbginit code" TSRMLS_CC);
224 								}
225 								free(code);
226 								code = NULL;
227 								goto next_line;
228 							}
229 						}
230 					}
231 
232 					if (in_code) {
233 						if (code == NULL) {
234 							code = malloc(cmd_len + 1);
235 						} else code = realloc(code, code_len + cmd_len + 1);
236 
237 						if (code) {
238 							memcpy(
239 								&code[code_len], cmd, cmd_len);
240 							code_len += cmd_len;
241 						}
242 						goto next_line;
243 					}
244 
245 					{
246 						char *why = NULL;
247 						char *input = phpdbg_read_input(cmd TSRMLS_CC);
248 						phpdbg_param_t stack;
249 
250 						phpdbg_init_param(&stack, STACK_PARAM);
251 
252 						if (phpdbg_do_parse(&stack, input TSRMLS_CC) <= 0) {
253 							switch (phpdbg_stack_execute(&stack, &why TSRMLS_CC)) {
254 								case FAILURE:
255 //									if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
256 										if (phpdbg_call_register(&stack TSRMLS_CC) == FAILURE) {
257 											phpdbg_error(
258 												"Unrecognized command in %s:%d: %s, %s!",
259 												init_file, line, input, why);
260 										}
261 //									}
262 								break;
263 							}
264 						}
265 
266 						if (why) {
267 							free(why);
268 							why = NULL;
269 						}
270 
271 						phpdbg_stack_free(&stack);
272 						phpdbg_destroy_input(&input TSRMLS_CC);
273 					}
274 				}
275 next_line:
276 				line++;
277 			}
278 
279 			if (code) {
280 				free(code);
281 			}
282 
283 			fclose(fp);
284 		} else {
285 			phpdbg_error(
286 				"Failed to open %s for initialization", init_file);
287 		}
288 
289 		if (free_init) {
290 			free(init_file);
291 		}
292 	}
293 } /* }}} */
294 
phpdbg_init(char * init_file,size_t init_file_len,zend_bool use_default TSRMLS_DC)295 void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TSRMLS_DC) /* {{{ */
296 {
297 	if (!init_file && use_default) {
298 		char *scan_dir = getenv("PHP_INI_SCAN_DIR");
299 		int i;
300 
301 		phpdbg_try_file_init(PHPDBG_STRL(PHP_CONFIG_FILE_PATH "/" PHPDBG_INIT_FILENAME), 0 TSRMLS_CC);
302 
303 		if (!scan_dir) {
304 			scan_dir = PHP_CONFIG_FILE_SCAN_DIR;
305 		}
306 		while (*scan_dir != 0) {
307 			i = 0;
308 			while (scan_dir[i] != ':') {
309 				if (scan_dir[i++] == 0) {
310 					i = -1;
311 					break;
312 				}
313 			}
314 			if (i != -1) {
315 				scan_dir[i] = 0;
316 			}
317 
318 			asprintf(
319 				&init_file, "%s/%s", scan_dir, PHPDBG_INIT_FILENAME);
320 			phpdbg_try_file_init(init_file, strlen(init_file), 1 TSRMLS_CC);
321 			if (i == -1) {
322 				break;
323 			}
324 			scan_dir += i + 1;
325 		}
326 
327 		phpdbg_try_file_init(PHPDBG_STRL(PHPDBG_INIT_FILENAME), 0 TSRMLS_CC);
328 	} else {
329 		phpdbg_try_file_init(init_file, init_file_len, 1 TSRMLS_CC);
330 	}
331 }
332 
PHPDBG_COMMAND(exec)333 PHPDBG_COMMAND(exec) /* {{{ */
334 {
335 	struct stat sb;
336 
337 	if (VCWD_STAT(param->str, &sb) != FAILURE) {
338 		if (sb.st_mode & (S_IFREG|S_IFLNK)) {
339 			char *res = phpdbg_resolve_path(param->str TSRMLS_CC);
340 			size_t res_len = strlen(res);
341 
342 			if ((res_len != PHPDBG_G(exec_len)) || (memcmp(res, PHPDBG_G(exec), res_len) != SUCCESS)) {
343 
344 				if (PHPDBG_G(exec)) {
345 					phpdbg_notice("Unsetting old execution context: %s", PHPDBG_G(exec));
346 					efree(PHPDBG_G(exec));
347 					PHPDBG_G(exec) = NULL;
348 					PHPDBG_G(exec_len) = 0L;
349 				}
350 
351 				if (PHPDBG_G(ops)) {
352 					phpdbg_notice("Destroying compiled opcodes");
353 					phpdbg_clean(0 TSRMLS_CC);
354 				}
355 
356 				PHPDBG_G(exec) = res;
357 				PHPDBG_G(exec_len) = res_len;
358 
359 				*SG(request_info).argv = PHPDBG_G(exec);
360 				php_hash_environment(TSRMLS_C);
361 
362 				phpdbg_notice("Set execution context: %s", PHPDBG_G(exec));
363 
364 				if (phpdbg_compile(TSRMLS_C) == FAILURE) {
365 					phpdbg_error("Failed to compile %s", PHPDBG_G(exec));
366 				}
367 			} else {
368 				phpdbg_notice("Execution context not changed");
369 			}
370 		} else {
371 			phpdbg_error("Cannot use %s as execution context, not a valid file or symlink", param->str);
372 		}
373 	} else {
374 		phpdbg_error("Cannot stat %s, ensure the file exists", param->str);
375 	}
376 	return SUCCESS;
377 } /* }}} */
378 
phpdbg_compile(TSRMLS_D)379 int phpdbg_compile(TSRMLS_D) /* {{{ */
380 {
381 	zend_file_handle fh;
382 
383 	if (!PHPDBG_G(exec)) {
384 		phpdbg_error("No execution context");
385 		return SUCCESS;
386 	}
387 
388 	if (EG(in_execution)) {
389 		phpdbg_error("Cannot compile while in execution");
390 		return FAILURE;
391 	}
392 
393 	phpdbg_notice("Attempting compilation of %s", PHPDBG_G(exec));
394 
395 	if (php_stream_open_for_zend_ex(PHPDBG_G(exec), &fh,
396 		USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC) == SUCCESS) {
397 
398 		PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE TSRMLS_CC);
399 		zend_destroy_file_handle(&fh TSRMLS_CC);
400 
401 		phpdbg_notice("Success");
402 		return SUCCESS;
403 	} else {
404 		phpdbg_error("Could not open file %s", PHPDBG_G(exec));
405 	}
406 
407 	return FAILURE;
408 } /* }}} */
409 
PHPDBG_COMMAND(step)410 PHPDBG_COMMAND(step) /* {{{ */
411 {
412 	if (EG(in_execution)) {
413 		PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
414 	}
415 
416 	return PHPDBG_NEXT;
417 } /* }}} */
418 
PHPDBG_COMMAND(continue)419 PHPDBG_COMMAND(continue) /* {{{ */
420 {
421 	return PHPDBG_NEXT;
422 } /* }}} */
423 
PHPDBG_COMMAND(until)424 PHPDBG_COMMAND(until) /* {{{ */
425 {
426 	if (!EG(in_execution)) {
427 		phpdbg_error("Not executing");
428 		return SUCCESS;
429 	}
430 
431 	PHPDBG_G(flags) |= PHPDBG_IN_UNTIL;
432 	{
433 		zend_uint next = 0,
434 				  self = (EG(current_execute_data)->opline - EG(active_op_array)->opcodes);
435 		zend_op  *opline = &EG(active_op_array)->opcodes[self];
436 
437 		for (next = self; next < EG(active_op_array)->last; next++) {
438 			if (EG(active_op_array)->opcodes[next].lineno != opline->lineno) {
439 				zend_hash_index_update(
440 					&PHPDBG_G(seek),
441 					(zend_ulong) &EG(active_op_array)->opcodes[next],
442 					&EG(active_op_array)->opcodes[next],
443 					sizeof(zend_op), NULL);
444 				break;
445 			}
446 		}
447 	}
448 
449 	return PHPDBG_UNTIL;
450 } /* }}} */
451 
PHPDBG_COMMAND(finish)452 PHPDBG_COMMAND(finish) /* {{{ */
453 {
454 	if (!EG(in_execution)) {
455 		phpdbg_error("Not executing");
456 		return SUCCESS;
457 	}
458 
459 	PHPDBG_G(flags) |= PHPDBG_IN_FINISH;
460 	{
461 		zend_uint next = 0,
462 				  self = (EG(current_execute_data)->opline - EG(active_op_array)->opcodes);
463 
464 		for (next = self; next < EG(active_op_array)->last; next++) {
465 			switch (EG(active_op_array)->opcodes[next].opcode) {
466 				case ZEND_RETURN:
467 				case ZEND_THROW:
468 				case ZEND_EXIT:
469 #ifdef ZEND_YIELD
470 				case ZEND_YIELD:
471 #endif
472 					zend_hash_index_update(
473 						&PHPDBG_G(seek),
474 						(zend_ulong) &EG(active_op_array)->opcodes[next],
475 						&EG(active_op_array)->opcodes[next],
476 						sizeof(zend_op), NULL);
477 				break;
478 			}
479 		}
480 	}
481 
482 	return PHPDBG_FINISH;
483 } /* }}} */
484 
PHPDBG_COMMAND(leave)485 PHPDBG_COMMAND(leave) /* {{{ */
486 {
487 	if (!EG(in_execution)) {
488 		phpdbg_error("Not executing");
489 		return SUCCESS;
490 	}
491 
492 	PHPDBG_G(flags) |= PHPDBG_IN_LEAVE;
493 	{
494 		zend_uint next = 0,
495 				  self = (EG(current_execute_data)->opline - EG(active_op_array)->opcodes);
496 
497 		for (next = self; next < EG(active_op_array)->last; next++) {
498 			switch (EG(active_op_array)->opcodes[next].opcode) {
499 				case ZEND_RETURN:
500 				case ZEND_THROW:
501 				case ZEND_EXIT:
502 #ifdef ZEND_YIELD
503 				case ZEND_YIELD:
504 #endif
505 					zend_hash_index_update(
506 						&PHPDBG_G(seek),
507 						(zend_ulong) &EG(active_op_array)->opcodes[next],
508 						&EG(active_op_array)->opcodes[next],
509 						sizeof(zend_op), NULL);
510 				break;
511 			}
512 		}
513 	}
514 
515 	return PHPDBG_LEAVE;
516 } /* }}} */
517 
PHPDBG_COMMAND(frame)518 PHPDBG_COMMAND(frame) /* {{{ */
519 {
520 	if (!param) {
521 		phpdbg_notice("Currently in frame #%d", PHPDBG_G(frame).num);
522 	} else phpdbg_switch_frame(param->num TSRMLS_CC);
523 
524 	return SUCCESS;
525 } /* }}} */
526 
phpdbg_handle_exception(TSRMLS_D)527 static inline void phpdbg_handle_exception(TSRMLS_D) /* }}} */
528 {
529 	zend_fcall_info fci;
530 
531 	zval fname,
532 		 *trace,
533 		 exception;
534 
535 	/* get filename and linenumber before unsetting exception */
536 	const char *filename = zend_get_executed_filename(TSRMLS_C);
537 	zend_uint lineno = zend_get_executed_lineno(TSRMLS_C);
538 
539 	/* copy exception */
540 	exception = *EG(exception);
541 	zval_copy_ctor(&exception);
542 	EG(exception) = NULL;
543 
544 	phpdbg_error(
545 		"Uncaught %s!",
546 		Z_OBJCE(exception)->name);
547 
548 	/* call __toString */
549 	ZVAL_STRINGL(&fname, "__tostring", sizeof("__tostring")-1, 1);
550 	fci.size = sizeof(fci);
551 	fci.function_table = &Z_OBJCE(exception)->function_table;
552 	fci.function_name = &fname;
553 	fci.symbol_table = NULL;
554 	fci.object_ptr = &exception;
555 	fci.retval_ptr_ptr = &trace;
556 	fci.param_count = 0;
557 	fci.params = NULL;
558 	fci.no_separation = 1;
559 	zend_call_function(&fci, NULL TSRMLS_CC);
560 
561 	if (trace) {
562 		phpdbg_writeln(
563 			"Uncaught %s", Z_STRVAL_P(trace));
564 		/* remember to dtor trace */
565 		zval_ptr_dtor(&trace);
566 	}
567 
568 	/* output useful information about address */
569 	phpdbg_writeln(
570 		"Stacked entered at %p in %s on line %u",
571 		EG(active_op_array)->opcodes, filename, lineno);
572 
573 	zval_dtor(&fname);
574 	zval_dtor(&exception);
575 } /* }}} */
576 
PHPDBG_COMMAND(run)577 PHPDBG_COMMAND(run) /* {{{ */
578 {
579 	if (EG(in_execution)) {
580 		phpdbg_error("Cannot start another execution while one is in progress");
581 		return SUCCESS;
582 	}
583 
584 	if (PHPDBG_G(ops) || PHPDBG_G(exec)) {
585 		zend_op **orig_opline = EG(opline_ptr);
586 		zend_op_array *orig_op_array = EG(active_op_array);
587 		zval **orig_retval_ptr = EG(return_value_ptr_ptr);
588 		zend_bool restore = 1;
589 		zend_execute_data *ex = EG(current_execute_data);
590 
591 		if (!PHPDBG_G(ops)) {
592 			if (phpdbg_compile(TSRMLS_C) == FAILURE) {
593 				phpdbg_error("Failed to compile %s, cannot run", PHPDBG_G(exec));
594 				goto out;
595 			}
596 		}
597 
598 		EG(active_op_array) = PHPDBG_G(ops);
599 		EG(return_value_ptr_ptr) = &PHPDBG_G(retval);
600 		if (!EG(active_symbol_table)) {
601 			zend_rebuild_symbol_table(TSRMLS_C);
602 		}
603 
604 		/* clean up from last execution */
605 		if (ex && ex->symbol_table) {
606 			zend_hash_clean(ex->symbol_table);
607 		}
608 
609 		/* clean seek state */
610 		PHPDBG_G(flags) &= ~PHPDBG_SEEK_MASK;
611 		zend_hash_clean(
612 			&PHPDBG_G(seek));
613 
614 		/* reset hit counters */
615 		phpdbg_reset_breakpoints(TSRMLS_C);
616 
617 		if (param && param->type != EMPTY_PARAM && param->len != 0) {
618 			char **argv = emalloc(5 * sizeof(char *));
619 			int argc = 0;
620 			int i;
621 			char *argv_str = strtok(param->str, " ");
622 
623 			while (argv_str) {
624 				if (argc >= 4 && argc == (argc & -argc)) {
625 					argv = erealloc(argv, (argc * 2 + 1) * sizeof(char *));
626 				}
627 				argv[++argc] = argv_str;
628 				argv_str = strtok(0, " ");
629 				argv[argc] = estrdup(argv[argc]);
630 			}
631 			argv[0] = SG(request_info).argv[0];
632 			for (i = SG(request_info).argc; --i;) {
633 				efree(SG(request_info).argv[i]);
634 			}
635 			efree(SG(request_info).argv);
636 			SG(request_info).argv = erealloc(argv, ++argc * sizeof(char *));
637 			SG(request_info).argc = argc;
638 
639 			php_hash_environment(TSRMLS_C);
640 		}
641 
642 		zend_try {
643 			php_output_activate(TSRMLS_C);
644 			PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
645 			zend_execute(EG(active_op_array) TSRMLS_CC);
646 			PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
647 			php_output_deactivate(TSRMLS_C);
648 		} zend_catch {
649 			EG(active_op_array) = orig_op_array;
650 			EG(opline_ptr) = orig_opline;
651 			EG(return_value_ptr_ptr) = orig_retval_ptr;
652 
653 			if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
654 				phpdbg_error("Caught exit/error from VM");
655 				restore = 0;
656 			}
657 		} zend_end_try();
658 
659 		if (restore) {
660 			if (EG(exception)) {
661 				phpdbg_handle_exception(TSRMLS_C);
662 			}
663 
664 			EG(active_op_array) = orig_op_array;
665 			EG(opline_ptr) = orig_opline;
666 			EG(return_value_ptr_ptr) = orig_retval_ptr;
667 		}
668 	} else {
669 		phpdbg_error("Nothing to execute!");
670 	}
671 
672 out:
673 	PHPDBG_FRAME(num) = 0;
674 	return SUCCESS;
675 } /* }}} */
676 
PHPDBG_COMMAND(ev)677 PHPDBG_COMMAND(ev) /* {{{ */
678 {
679 	zend_bool stepping = ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING)==PHPDBG_IS_STEPPING);
680 	zval retval;
681 
682 	if (!(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
683 		PHPDBG_G(flags) &= ~ PHPDBG_IS_STEPPING;
684 	}
685 
686 	/* disable stepping while eval() in progress */
687 	PHPDBG_G(flags) |= PHPDBG_IN_EVAL;
688 	zend_try {
689 		if (zend_eval_stringl(param->str, param->len,
690 			&retval, "eval()'d code" TSRMLS_CC) == SUCCESS) {
691 			zend_print_zval_r(
692 				&retval, 0 TSRMLS_CC);
693 			phpdbg_writeln(EMPTY);
694 			zval_dtor(&retval);
695 		}
696 	} zend_end_try();
697 	PHPDBG_G(flags) &= ~PHPDBG_IN_EVAL;
698 
699 	/* switch stepping back on */
700 	if (stepping &&
701 		!(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
702 		PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
703 	}
704 
705 	CG(unclean_shutdown) = 0;
706 
707 	return SUCCESS;
708 } /* }}} */
709 
PHPDBG_COMMAND(back)710 PHPDBG_COMMAND(back) /* {{{ */
711 {
712 	if (!EG(in_execution)) {
713 		phpdbg_error("Not executing!");
714 		return SUCCESS;
715 	}
716 
717 	if (!param) {
718 		phpdbg_dump_backtrace(0 TSRMLS_CC);
719 	} else {
720 		phpdbg_dump_backtrace(param->num TSRMLS_CC);
721 	}
722 
723 	return SUCCESS;
724 } /* }}} */
725 
PHPDBG_COMMAND(print)726 PHPDBG_COMMAND(print) /* {{{ */
727 {
728 	phpdbg_writeln(SEPARATE);
729 	phpdbg_notice("Execution Context Information");
730 #ifdef HAVE_LIBREADLINE
731 	phpdbg_writeln("Readline\tyes");
732 #else
733 	phpdbg_writeln("Readline\tno");
734 #endif
735 #ifdef HAVE_LIBEDIT
736 	phpdbg_writeln("Libedit\t\tyes");
737 #else
738 	phpdbg_writeln("Libedit\t\tno");
739 #endif
740 
741 	phpdbg_writeln("Exec\t\t%s", PHPDBG_G(exec) ? PHPDBG_G(exec) : "none");
742 	phpdbg_writeln("Compiled\t%s", PHPDBG_G(ops) ? "yes" : "no");
743 	phpdbg_writeln("Stepping\t%s", (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ? "on" : "off");
744 	phpdbg_writeln("Quietness\t%s", (PHPDBG_G(flags) & PHPDBG_IS_QUIET) ? "on" : "off");
745 	phpdbg_writeln("Oplog\t\t%s", PHPDBG_G(oplog) ? "on" : "off");
746 
747 	if (PHPDBG_G(ops)) {
748 		phpdbg_writeln("Opcodes\t\t%d", PHPDBG_G(ops)->last);
749 
750 		if (PHPDBG_G(ops)->last_var) {
751 			phpdbg_writeln("Variables\t%d", PHPDBG_G(ops)->last_var-1);
752 		} else {
753 			phpdbg_writeln("Variables\tNone");
754 		}
755 	}
756 
757 	phpdbg_writeln("Executing\t%s", EG(in_execution) ? "yes" : "no");
758 	if (EG(in_execution)) {
759 		phpdbg_writeln("VM Return\t%d", PHPDBG_G(vmret));
760 	}
761 
762 	phpdbg_writeln("Classes\t\t%d", zend_hash_num_elements(EG(class_table)));
763 	phpdbg_writeln("Functions\t%d", zend_hash_num_elements(EG(function_table)));
764 	phpdbg_writeln("Constants\t%d", zend_hash_num_elements(EG(zend_constants)));
765 	phpdbg_writeln("Included\t%d", zend_hash_num_elements(&EG(included_files)));
766 
767 	phpdbg_writeln(SEPARATE);
768 
769 	return SUCCESS;
770 } /* }}} */
771 
PHPDBG_COMMAND(info)772 PHPDBG_COMMAND(info) /* {{{ */
773 {
774 	phpdbg_error(
775 		"No information command selected!");
776 
777 	return SUCCESS;
778 } /* }}} */
779 
PHPDBG_COMMAND(set)780 PHPDBG_COMMAND(set) /* {{{ */
781 {
782 	phpdbg_error(
783 		"No set command selected!");
784 
785 	return SUCCESS;
786 } /* }}} */
787 
PHPDBG_COMMAND(break)788 PHPDBG_COMMAND(break) /* {{{ */
789 {
790 	if (!param) {
791 		phpdbg_set_breakpoint_file(
792 				zend_get_executed_filename(TSRMLS_C),
793 				zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
794 	} else switch (param->type) {
795 		case ADDR_PARAM:
796 			phpdbg_set_breakpoint_opline(param->addr TSRMLS_CC);
797 			break;
798 		case NUMERIC_PARAM:
799 			if (PHPDBG_G(exec)) {
800 				phpdbg_set_breakpoint_file(phpdbg_current_file(TSRMLS_C), param->num TSRMLS_CC);
801 			} else {
802 				phpdbg_error("Execution context not set!");
803 			}
804 			break;
805 		case METHOD_PARAM:
806 			phpdbg_set_breakpoint_method(param->method.class, param->method.name TSRMLS_CC);
807 			break;
808 		case NUMERIC_METHOD_PARAM:
809 			phpdbg_set_breakpoint_method_opline(param->method.class, param->method.name, param->num TSRMLS_CC);
810 			break;
811 		case NUMERIC_FUNCTION_PARAM:
812 			phpdbg_set_breakpoint_function_opline(param->str, param->num TSRMLS_CC);
813 			break;
814 		case FILE_PARAM:
815 			phpdbg_set_breakpoint_file(param->file.name, param->file.line TSRMLS_CC);
816 			break;
817 		case NUMERIC_FILE_PARAM:
818 			phpdbg_set_breakpoint_file_opline(param->file.name, param->file.line TSRMLS_CC);
819 			break;
820 		case COND_PARAM:
821 			phpdbg_set_breakpoint_expression(param->str, param->len TSRMLS_CC);
822 			break;
823 		case STR_PARAM:
824 			phpdbg_set_breakpoint_symbol(param->str, param->len TSRMLS_CC);
825 			break;
826 		case OP_PARAM:
827 			phpdbg_set_breakpoint_opcode(param->str, param->len TSRMLS_CC);
828 			break;
829 
830 		phpdbg_default_switch_case();
831 	}
832 
833 	return SUCCESS;
834 } /* }}} */
835 
PHPDBG_COMMAND(sh)836 PHPDBG_COMMAND(sh) /* {{{ */
837 {
838 	FILE *fd = NULL;
839 	if ((fd=VCWD_POPEN((char*)param->str, "w"))) {
840 		/* do something perhaps ?? do we want input ?? */
841 		fclose(fd);
842 	} else {
843 		phpdbg_error(
844 			"Failed to execute %s", param->str);
845 	}
846 
847 	return SUCCESS;
848 } /* }}} */
849 
PHPDBG_COMMAND(source)850 PHPDBG_COMMAND(source) /* {{{ */
851 {
852 	struct stat sb;
853 
854 	if (VCWD_STAT(param->str, &sb) != -1) {
855 		phpdbg_try_file_init(param->str, param->len, 0 TSRMLS_CC);
856 	} else {
857 		phpdbg_error(
858 			"Failed to stat %s, file does not exist", param->str);
859 	}
860 
861 	return SUCCESS;
862 } /* }}} */
863 
PHPDBG_COMMAND(export)864 PHPDBG_COMMAND(export) /* {{{ */
865 {
866 	FILE *handle = VCWD_FOPEN(param->str, "w+");
867 
868 	if (handle) {
869 		phpdbg_export_breakpoints(handle TSRMLS_CC);
870 		fclose(handle);
871 	} else {
872 		phpdbg_error(
873 			"Failed to open or create %s, check path and permissions", param->str);
874 	}
875 
876 	return SUCCESS;
877 } /* }}} */
878 
PHPDBG_COMMAND(register)879 PHPDBG_COMMAND(register) /* {{{ */
880 {
881 	zend_function *function;
882 	char *lcname = zend_str_tolower_dup(param->str, param->len);
883 	size_t lcname_len = strlen(lcname);
884 
885 	if (!zend_hash_exists(&PHPDBG_G(registered), lcname, lcname_len+1)) {
886 		if (zend_hash_find(EG(function_table), lcname, lcname_len+1, (void**) &function) == SUCCESS) {
887 			zend_hash_update(
888 				&PHPDBG_G(registered), lcname, lcname_len+1, (void*)&function, sizeof(zend_function), NULL);
889 			function_add_ref(function);
890 
891 			phpdbg_notice(
892 				"Registered %s", lcname);
893 		} else {
894 			phpdbg_error("The requested function (%s) could not be found", param->str);
895 		}
896 	} else {
897 		phpdbg_error(
898 			"The requested name (%s) is already in use", lcname);
899 	}
900 
901 	efree(lcname);
902 	return SUCCESS;
903 } /* }}} */
904 
PHPDBG_COMMAND(quit)905 PHPDBG_COMMAND(quit) /* {{{ */
906 {
907 	/* don't allow this to loop, ever ... */
908 	if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
909 		PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
910 		zend_bailout();
911 	}
912 
913 	return PHPDBG_NEXT;
914 } /* }}} */
915 
PHPDBG_COMMAND(clean)916 PHPDBG_COMMAND(clean) /* {{{ */
917 {
918 	if (EG(in_execution)) {
919 		phpdbg_error("Cannot clean environment while executing");
920 		return SUCCESS;
921 	}
922 
923 	phpdbg_notice("Cleaning Execution Environment");
924 
925 	phpdbg_writeln("Classes\t\t\t%d", zend_hash_num_elements(EG(class_table)));
926 	phpdbg_writeln("Functions\t\t%d", zend_hash_num_elements(EG(function_table)));
927 	phpdbg_writeln("Constants\t\t%d", zend_hash_num_elements(EG(zend_constants)));
928 	phpdbg_writeln("Includes\t\t%d", zend_hash_num_elements(&EG(included_files)));
929 
930 	phpdbg_clean(1 TSRMLS_CC);
931 
932 	return SUCCESS;
933 } /* }}} */
934 
PHPDBG_COMMAND(clear)935 PHPDBG_COMMAND(clear) /* {{{ */
936 {
937 	phpdbg_notice("Clearing Breakpoints");
938 
939 	phpdbg_writeln("File\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]));
940 	phpdbg_writeln("Functions\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]));
941 	phpdbg_writeln("Methods\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]));
942 	phpdbg_writeln("Oplines\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]));
943 	phpdbg_writeln("File oplines\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE]));
944 	phpdbg_writeln("Function oplines\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE]));
945 	phpdbg_writeln("Method oplines\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE]));
946 	phpdbg_writeln("Conditionals\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_COND]));
947 
948 	phpdbg_clear_breakpoints(TSRMLS_C);
949 
950 	return SUCCESS;
951 } /* }}} */
952 
PHPDBG_COMMAND(list)953 PHPDBG_COMMAND(list) /* {{{ */
954 {
955 	if (!param) {
956 		return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
957 	} else switch (param->type) {
958 		case NUMERIC_PARAM:
959 			return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
960 
961 		case FILE_PARAM:
962 			return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
963 
964 		case STR_PARAM:
965 			phpdbg_list_function_byname(param->str, param->len TSRMLS_CC);
966 			break;
967 
968 		case METHOD_PARAM:
969 			return PHPDBG_LIST_HANDLER(method)(PHPDBG_COMMAND_ARGS);
970 
971 		phpdbg_default_switch_case();
972 	}
973 
974 	return SUCCESS;
975 } /* }}} */
976 
PHPDBG_COMMAND(watch)977 PHPDBG_COMMAND(watch) /* {{{ */
978 {
979 	if (!param || param->type == EMPTY_PARAM) {
980 		phpdbg_list_watchpoints(TSRMLS_C);
981 	} else switch (param->type) {
982 		case STR_PARAM:
983 			if (phpdbg_create_var_watchpoint(param->str, param->len TSRMLS_CC) != FAILURE) {
984 				phpdbg_notice("Set watchpoint on %.*s", (int)param->len, param->str);
985 			}
986 			break;
987 
988 		phpdbg_default_switch_case();
989 	}
990 
991 	return SUCCESS;
992 } /* }}} */
993 
phpdbg_interactive(TSRMLS_D)994 int phpdbg_interactive(TSRMLS_D) /* {{{ */
995 {
996 	int ret = SUCCESS;
997 	char *why = NULL;
998 	char *input = NULL;
999 	phpdbg_param_t stack;
1000 
1001 	PHPDBG_G(flags) |= PHPDBG_IS_INTERACTIVE;
1002 
1003 	input = phpdbg_read_input(NULL TSRMLS_CC);
1004 
1005 	if (input) {
1006 		do {
1007 			phpdbg_init_param(&stack, STACK_PARAM);
1008 
1009 			if (phpdbg_do_parse(&stack, input TSRMLS_CC) <= 0) {
1010 				switch (ret = phpdbg_stack_execute(&stack, &why TSRMLS_CC)) {
1011 					case FAILURE:
1012 						if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
1013 							if (phpdbg_call_register(&stack TSRMLS_CC) == FAILURE) {
1014 								if (why) {
1015 									phpdbg_error("%s", why);
1016 								}
1017 							}
1018 						}
1019 
1020 						if (why) {
1021 							free(why);
1022 							why = NULL;
1023 						}
1024 					break;
1025 
1026 					case PHPDBG_LEAVE:
1027 					case PHPDBG_FINISH:
1028 					case PHPDBG_UNTIL:
1029 					case PHPDBG_NEXT: {
1030 						if (!EG(in_execution) && !(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
1031 							phpdbg_error("Not running");
1032 						}
1033 						goto out;
1034 					}
1035 				}
1036 			}
1037 
1038 			if (why) {
1039 				free(why);
1040 				why = NULL;
1041 			}
1042 
1043 			phpdbg_stack_free(&stack);
1044 			phpdbg_destroy_input(&input TSRMLS_CC);
1045 
1046 		} while ((input = phpdbg_read_input(NULL TSRMLS_CC)));
1047 	}
1048 
1049 out:
1050 	if (input) {
1051 		phpdbg_stack_free(&stack);
1052 		phpdbg_destroy_input(&input TSRMLS_CC);
1053 	}
1054 
1055 	if (why) {
1056 		free(why);
1057 	}
1058 
1059 	if (EG(in_execution)) {
1060 		phpdbg_restore_frame(TSRMLS_C);
1061 	}
1062 
1063 	PHPDBG_G(flags) &= ~PHPDBG_IS_INTERACTIVE;
1064 
1065 	phpdbg_print_changed_zvals(TSRMLS_C);
1066 
1067 	return ret;
1068 } /* }}} */
1069 
phpdbg_clean(zend_bool full TSRMLS_DC)1070 void phpdbg_clean(zend_bool full TSRMLS_DC) /* {{{ */
1071 {
1072 	/* this is implicitly required */
1073 	if (PHPDBG_G(ops)) {
1074 		destroy_op_array(PHPDBG_G(ops) TSRMLS_CC);
1075 		efree(PHPDBG_G(ops));
1076 		PHPDBG_G(ops) = NULL;
1077 	}
1078 
1079 	if (full) {
1080 		PHPDBG_G(flags) |= PHPDBG_IS_CLEANING;
1081 
1082 		zend_bailout();
1083 	}
1084 } /* }}} */
1085 
phpdbg_create_execute_data(zend_op_array * op_array,zend_bool nested TSRMLS_DC)1086 static inline zend_execute_data *phpdbg_create_execute_data(zend_op_array *op_array, zend_bool nested TSRMLS_DC) /* {{{ */
1087 {
1088 #if PHP_VERSION_ID >= 50500
1089 	return zend_create_execute_data_from_op_array(op_array, nested TSRMLS_CC);
1090 #else
1091 
1092 #undef EX
1093 #define EX(element) execute_data->element
1094 #undef EX_CV
1095 #define EX_CV(var) EX(CVs)[var]
1096 #undef EX_CVs
1097 #define EX_CVs() EX(CVs)
1098 #undef EX_T
1099 #define EX_T(offset) (*(temp_variable *)((char *) EX(Ts) + offset))
1100 #undef EX_Ts
1101 #define EX_Ts() EX(Ts)
1102 
1103 	zend_execute_data *execute_data = (zend_execute_data *)zend_vm_stack_alloc(
1104 		ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) +
1105 		ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)) +
1106 		ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T TSRMLS_CC);
1107 
1108 	EX(CVs) = (zval***)((char*)execute_data + ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)));
1109 	memset(EX(CVs), 0, sizeof(zval**) * op_array->last_var);
1110 	EX(Ts) = (temp_variable *)(((char*)EX(CVs)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)));
1111 	EX(fbc) = NULL;
1112 	EX(called_scope) = NULL;
1113 	EX(object) = NULL;
1114 	EX(old_error_reporting) = NULL;
1115 	EX(op_array) = op_array;
1116 	EX(symbol_table) = EG(active_symbol_table);
1117 	EX(prev_execute_data) = EG(current_execute_data);
1118 	EG(current_execute_data) = execute_data;
1119 	EX(nested) = nested;
1120 
1121 	if (!op_array->run_time_cache && op_array->last_cache_slot) {
1122 		op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
1123 	}
1124 
1125 	if (op_array->this_var != -1 && EG(This)) {
1126  		Z_ADDREF_P(EG(This)); /* For $this pointer */
1127 		if (!EG(active_symbol_table)) {
1128 			EX_CV(op_array->this_var) = (zval**)EX_CVs() + (op_array->last_var + op_array->this_var);
1129 			*EX_CV(op_array->this_var) = EG(This);
1130 		} else {
1131 			if (zend_hash_add(EG(active_symbol_table), "this", sizeof("this"), &EG(This), sizeof(zval *), (void**)&EX_CV(op_array->this_var))==FAILURE) {
1132 				Z_DELREF_P(EG(This));
1133 			}
1134 		}
1135 	}
1136 
1137 	EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes;
1138 	EG(opline_ptr) = &EX(opline);
1139 
1140 	EX(function_state).function = (zend_function *) op_array;
1141 	EX(function_state).arguments = NULL;
1142 
1143 	return execute_data;
1144 #endif
1145 } /* }}} */
1146 
1147 #if PHP_VERSION_ID >= 50500
phpdbg_execute_ex(zend_execute_data * execute_data TSRMLS_DC)1148 void phpdbg_execute_ex(zend_execute_data *execute_data TSRMLS_DC) /* {{{ */
1149 {
1150 #else
1151 void phpdbg_execute_ex(zend_op_array *op_array TSRMLS_DC) /* {{{ */
1152 {
1153 	long long flags = 0;
1154 	zend_ulong address = 0L;
1155 	zend_execute_data *execute_data;
1156 	zend_bool nested = 0;
1157 #endif
1158 	zend_bool original_in_execution = EG(in_execution);
1159 	HashTable vars;
1160 
1161 #if PHP_VERSION_ID < 50500
1162 	if (EG(exception)) {
1163 		return;
1164 	}
1165 #endif
1166 
1167 	EG(in_execution) = 1;
1168 
1169 #if PHP_VERSION_ID >= 50500
1170 	if (0) {
1171 zend_vm_enter:
1172 		execute_data = phpdbg_create_execute_data(EG(active_op_array), 1 TSRMLS_CC);
1173 	}
1174 	zend_hash_init(&vars, EG(active_op_array)->last, NULL, NULL, 0);
1175 #else
1176 zend_vm_enter:
1177 	execute_data = phpdbg_create_execute_data(op_array, nested TSRMLS_CC);
1178 	nested = 1;
1179 	zend_hash_init(&vars, EG(active_op_array)->last, NULL, NULL, 0);
1180 #endif
1181 
1182 	while (1) {
1183 
1184 		if ((PHPDBG_G(flags) & PHPDBG_BP_RESOLVE_MASK)) {
1185 			/* resolve nth opline breakpoints */
1186 			phpdbg_resolve_op_array_breaks(EG(active_op_array) TSRMLS_CC);
1187 		}
1188 
1189 #ifdef ZEND_WIN32
1190 		if (EG(timed_out)) {
1191 			zend_timeout(0);
1192 		}
1193 #endif
1194 
1195 #define DO_INTERACTIVE() do { \
1196 	if (!(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) { \
1197 		phpdbg_list_file( \
1198 			zend_get_executed_filename(TSRMLS_C), \
1199 			3, \
1200 			zend_get_executed_lineno(TSRMLS_C)-1, \
1201 			zend_get_executed_lineno(TSRMLS_C) \
1202 			TSRMLS_CC \
1203 		); \
1204 	} \
1205 	\
1206 /*	do { */\
1207 		switch (phpdbg_interactive(TSRMLS_C)) { \
1208 			case PHPDBG_LEAVE: \
1209 			case PHPDBG_FINISH: \
1210 			case PHPDBG_UNTIL: \
1211 			case PHPDBG_NEXT:{ \
1212 				goto next; \
1213 			} \
1214 		} \
1215 /*	} while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)); */\
1216 } while (0)
1217 
1218 		/* allow conditional breakpoints and
1219 			initialization to access the vm uninterrupted */
1220 		if ((PHPDBG_G(flags) & PHPDBG_IN_COND_BP) ||
1221 			(PHPDBG_G(flags) & PHPDBG_IS_INITIALIZING)) {
1222 			/* skip possible breakpoints */
1223 			goto next;
1224 		}
1225 
1226 		/* perform seek operation */
1227 		if (PHPDBG_G(flags) & PHPDBG_SEEK_MASK) {
1228 			/* current address */
1229 			zend_ulong address = (zend_ulong) execute_data->opline;
1230 
1231 			/* run to next line */
1232 			if (PHPDBG_G(flags) & PHPDBG_IN_UNTIL) {
1233 				if (zend_hash_index_exists(&PHPDBG_G(seek), address)) {
1234 					PHPDBG_G(flags) &= ~PHPDBG_IN_UNTIL;
1235 					zend_hash_clean(
1236 						&PHPDBG_G(seek));
1237 				} else {
1238 					/* skip possible breakpoints */
1239 					goto next;
1240 				}
1241 			}
1242 
1243 			/* run to finish */
1244 			if (PHPDBG_G(flags) & PHPDBG_IN_FINISH) {
1245 				if (zend_hash_index_exists(&PHPDBG_G(seek), address)) {
1246 					PHPDBG_G(flags) &= ~PHPDBG_IN_FINISH;
1247 					zend_hash_clean(
1248 						&PHPDBG_G(seek));
1249 				}
1250 				/* skip possible breakpoints */
1251 				goto next;
1252 			}
1253 
1254 			/* break for leave */
1255 			if (PHPDBG_G(flags) & PHPDBG_IN_LEAVE) {
1256 				if (zend_hash_index_exists(&PHPDBG_G(seek), address)) {
1257 					PHPDBG_G(flags) &= ~PHPDBG_IN_LEAVE;
1258 					zend_hash_clean(
1259 						&PHPDBG_G(seek));
1260 					phpdbg_notice(
1261 						"Breaking for leave at %s:%u",
1262 						zend_get_executed_filename(TSRMLS_C),
1263 						zend_get_executed_lineno(TSRMLS_C)
1264 					);
1265 					DO_INTERACTIVE();
1266 				} else {
1267 					/* skip possible breakpoints */
1268 					goto next;
1269 				}
1270 			}
1271 		}
1272 
1273 		/* not while in conditionals */
1274 		phpdbg_print_opline_ex(
1275 			execute_data, &vars, 0 TSRMLS_CC);
1276 
1277 		if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING && (PHPDBG_G(flags) & PHPDBG_STEP_OPCODE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1278 			PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
1279 			DO_INTERACTIVE();
1280 		}
1281 
1282 		/* check if some watchpoint was hit */
1283 		{
1284 			if (phpdbg_print_changed_zvals(TSRMLS_C) == SUCCESS) {
1285 				DO_INTERACTIVE();
1286 			}
1287 		}
1288 
1289 		/* search for breakpoints */
1290 		{
1291 			phpdbg_breakbase_t *brake;
1292 
1293 			if ((PHPDBG_G(flags) & PHPDBG_BP_MASK)
1294 			    && (brake = phpdbg_find_breakpoint(execute_data TSRMLS_CC))
1295 			    && (brake->type != PHPDBG_BREAK_FILE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
1296 				phpdbg_hit_breakpoint(brake, 1 TSRMLS_CC);
1297 				DO_INTERACTIVE();
1298 			}
1299 		}
1300 
1301 next:
1302 		if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
1303 			phpdbg_writeln(EMPTY);
1304 			phpdbg_notice("Program received signal SIGINT");
1305 			PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
1306 			DO_INTERACTIVE();
1307 		}
1308 
1309 		PHPDBG_G(last_line) = execute_data->opline->lineno;
1310 
1311 		PHPDBG_G(vmret) = execute_data->opline->handler(execute_data TSRMLS_CC);
1312 
1313 		if (PHPDBG_G(vmret) > 0) {
1314 			switch (PHPDBG_G(vmret)) {
1315 				case 1:
1316 					EG(in_execution) = original_in_execution;
1317 					zend_hash_destroy(&vars);
1318 					return;
1319 				case 2:
1320 #if PHP_VERSION_ID < 50500
1321 					op_array = EG(active_op_array);
1322 #endif
1323 					zend_hash_destroy(&vars);
1324 					goto zend_vm_enter;
1325 					break;
1326 				case 3:
1327 					execute_data = EG(current_execute_data);
1328 					break;
1329 				default:
1330 					break;
1331 			}
1332 		}
1333 	}
1334 	zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
1335 } /* }}} */
1336