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