php-src/phpdbg_prompt.c

771 lines
22 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <string.h>
#include "zend.h"
2013-11-10 13:01:46 +00:00
#include "zend_compile.h"
#include "phpdbg.h"
#include "phpdbg_help.h"
2013-11-12 08:26:11 +00:00
#include "phpdbg_print.h"
2013-11-13 14:22:01 +00:00
#include "phpdbg_break.h"
#include "phpdbg_bp.h"
#include "phpdbg_opcode.h"
2013-11-12 00:24:24 +00:00
#include "phpdbg_list.h"
2013-11-12 02:19:43 +00:00
#include "phpdbg_utils.h"
2013-11-13 21:15:45 +00:00
/* {{{ forward declarations */
static PHPDBG_COMMAND(exec);
static PHPDBG_COMMAND(compile);
static PHPDBG_COMMAND(step);
static PHPDBG_COMMAND(next);
static PHPDBG_COMMAND(run);
static PHPDBG_COMMAND(eval);
static PHPDBG_COMMAND(print);
static PHPDBG_COMMAND(break);
static PHPDBG_COMMAND(back);
static PHPDBG_COMMAND(list);
static PHPDBG_COMMAND(clean);
static PHPDBG_COMMAND(clear);
static PHPDBG_COMMAND(help);
static PHPDBG_COMMAND(quiet);
static PHPDBG_COMMAND(aliases);
static PHPDBG_COMMAND(quit); /* }}} */
/* {{{ command declarations */
static const phpdbg_command_t phpdbg_prompt_commands[] = {
PHPDBG_COMMAND_EX_D(exec, "set execution context", 'e'),
PHPDBG_COMMAND_EX_D(compile, "attempt to pre-compile execution context", 'c'),
PHPDBG_COMMAND_EX_D(step, "step through execution", 's'),
PHPDBG_COMMAND_EX_D(next, "continue execution", 'n'),
PHPDBG_COMMAND_EX_D(run, "attempt execution", 'r'),
PHPDBG_COMMAND_EX_D(eval, "evaluate some code", 'E'),
PHPDBG_COMMAND_EX_D(print, "print something", 'p'),
PHPDBG_COMMAND_EX_D(break, "set breakpoint", 'b'),
PHPDBG_COMMAND_EX_D(back, "show trace", 't'),
PHPDBG_COMMAND_EX_D(list, "list specified line or function", 'l'),
PHPDBG_COMMAND_EX_D(clean, "clean the execution environment", 'X'),
PHPDBG_COMMAND_EX_D(clear, "clear breakpoints", 'C'),
PHPDBG_COMMAND_EX_D(help, "show help menu", 'h'),
PHPDBG_COMMAND_EX_D(quiet, "silence some output", 'Q'),
PHPDBG_COMMAND_EX_D(aliases, "show alias list", 'a'),
PHPDBG_COMMAND_EX_D(quit, "exit phpdbg", 'q'),
{NULL, 0, 0}
}; /* }}} */
2013-11-10 11:03:29 +00:00
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
2013-11-10 18:29:05 +00:00
static PHPDBG_COMMAND(exec) /* {{{ */
{
2013-11-13 21:43:54 +00:00
if (expr_len == 0) {
phpdbg_error("No expression provided");
return SUCCESS;
}
if (PHPDBG_G(exec)) {
phpdbg_notice("Unsetting old execution context: %s", PHPDBG_G(exec));
efree(PHPDBG_G(exec));
PHPDBG_G(exec) = NULL;
2013-11-13 17:35:51 +00:00
}
2013-11-13 21:43:54 +00:00
if (PHPDBG_G(ops)) {
phpdbg_notice("Destroying compiled opcodes");
phpdbg_clean(0 TSRMLS_CC);
}
PHPDBG_G(exec) = estrndup(expr, PHPDBG_G(exec_len) = expr_len);
2013-11-10 11:48:01 +00:00
2013-11-13 21:43:54 +00:00
phpdbg_notice("Set execution context: %s", PHPDBG_G(exec));
2013-11-10 18:29:05 +00:00
return SUCCESS;
2013-11-10 11:35:59 +00:00
} /* }}} */
2013-11-10 18:29:05 +00:00
static inline int phpdbg_compile(TSRMLS_D) /* {{{ */
{
zend_file_handle fh;
2013-11-10 13:01:46 +00:00
2013-11-13 21:43:54 +00:00
if (EG(in_execution)) {
phpdbg_error("Cannot compile while in execution");
return FAILURE;
}
2013-11-11 22:02:34 +00:00
2013-11-13 21:43:54 +00:00
phpdbg_notice("Attempting compilation of %s", PHPDBG_G(exec));
2013-11-13 21:43:54 +00:00
if (php_stream_open_for_zend_ex(PHPDBG_G(exec), &fh,
USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC) == SUCCESS) {
2013-11-13 21:43:54 +00:00
PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE TSRMLS_CC);
zend_destroy_file_handle(&fh TSRMLS_CC);
phpdbg_notice("Success");
return SUCCESS;
2013-11-10 21:07:29 +00:00
} else {
2013-11-13 21:43:54 +00:00
phpdbg_error("Could not open file %s", PHPDBG_G(exec));
2013-11-10 21:07:29 +00:00
}
2013-11-10 21:38:58 +00:00
2013-11-10 18:29:05 +00:00
return FAILURE;
2013-11-10 11:03:29 +00:00
} /* }}} */
2013-11-10 18:29:05 +00:00
static PHPDBG_COMMAND(compile) /* {{{ */
{
2013-11-13 21:43:54 +00:00
if (!PHPDBG_G(exec)) {
phpdbg_error("No execution context");
2013-11-10 18:29:05 +00:00
return FAILURE;
}
2013-11-13 21:43:54 +00:00
if (!EG(in_execution)) {
if (PHPDBG_G(ops)) {
phpdbg_error("Destroying previously compiled opcodes");
phpdbg_clean(0 TSRMLS_CC);
}
}
return phpdbg_compile(TSRMLS_C);
2013-11-10 14:28:14 +00:00
} /* }}} */
2013-11-10 18:29:05 +00:00
static PHPDBG_COMMAND(step) /* {{{ */
{
if (atoi(expr)) {
PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
} else {
PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
}
2013-11-12 02:19:43 +00:00
phpdbg_notice("Stepping %s",
(PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ? "on" : "off");
2013-11-10 18:29:05 +00:00
return SUCCESS;
2013-11-10 14:28:14 +00:00
} /* }}} */
2013-11-10 18:29:05 +00:00
static PHPDBG_COMMAND(next) /* {{{ */
{
return PHPDBG_NEXT;
2013-11-10 15:32:24 +00:00
} /* }}} */
2013-11-10 18:29:05 +00:00
static PHPDBG_COMMAND(run) /* {{{ */
{
if (EG(in_execution)) {
phpdbg_error("Cannot start another execution while one is in progress");
return FAILURE;
}
2013-11-11 22:02:34 +00:00
2013-11-10 18:29:05 +00:00
if (PHPDBG_G(ops) || PHPDBG_G(exec)) {
2013-11-13 14:22:01 +00:00
zend_op **orig_opline = EG(opline_ptr);
zend_op_array *orig_op_array = EG(active_op_array);
zval **orig_retval_ptr = EG(return_value_ptr_ptr);
2013-11-13 21:31:33 +00:00
2013-11-10 18:29:05 +00:00
if (!PHPDBG_G(ops)) {
if (phpdbg_compile(TSRMLS_C) == FAILURE) {
phpdbg_error("Failed to compile %s, cannot run", PHPDBG_G(exec));
2013-11-10 18:29:05 +00:00
return FAILURE;
}
}
2013-11-10 15:21:20 +00:00
2013-11-10 18:29:05 +00:00
EG(active_op_array) = PHPDBG_G(ops);
EG(return_value_ptr_ptr) = &PHPDBG_G(retval);
2013-11-13 14:22:01 +00:00
if (!EG(active_symbol_table)) {
zend_rebuild_symbol_table(TSRMLS_C);
}
2013-11-13 21:31:33 +00:00
2013-11-10 18:29:05 +00:00
zend_try {
2013-11-13 14:22:01 +00:00
zend_execute(
EG(active_op_array) TSRMLS_CC);
2013-11-10 18:29:05 +00:00
} zend_catch {
2013-11-13 14:22:01 +00:00
EG(active_op_array) = orig_op_array;
EG(opline_ptr) = orig_opline;
EG(return_value_ptr_ptr) = orig_retval_ptr;
2013-11-13 21:31:33 +00:00
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
phpdbg_error("Caught excetion in VM");
2013-11-10 18:29:05 +00:00
return FAILURE;
} else return SUCCESS;
} zend_end_try();
2013-11-13 14:22:01 +00:00
EG(active_op_array) = orig_op_array;
EG(opline_ptr) = orig_opline;
EG(return_value_ptr_ptr) = orig_retval_ptr;
2013-11-13 21:31:33 +00:00
2013-11-10 18:29:05 +00:00
return SUCCESS;
} else {
phpdbg_error("Nothing to execute!");
2013-11-10 18:29:05 +00:00
return FAILURE;
}
2013-11-10 14:43:46 +00:00
} /* }}} */
2013-11-10 18:29:05 +00:00
static PHPDBG_COMMAND(eval) /* {{{ */
{
2013-11-13 21:43:54 +00:00
zend_bool stepping = (PHPDBG_G(flags) & PHPDBG_IS_STEPPING);
2013-11-10 18:29:05 +00:00
zval retval;
2013-11-12 02:19:43 +00:00
2013-11-13 21:43:54 +00:00
if (expr_len == 0) {
phpdbg_error("No expression provided!");
return FAILURE;
}
2013-11-12 02:19:43 +00:00
2013-11-13 21:43:54 +00:00
PHPDBG_G(flags) &= ~ PHPDBG_IS_STEPPING;
2013-11-13 21:31:33 +00:00
2013-11-13 21:43:54 +00:00
/* disable stepping while eval() in progress */
PHPDBG_G(flags) |= PHPDBG_IN_EVAL;
if (zend_eval_stringl((char*)expr, expr_len,
&retval, "eval()'d code" TSRMLS_CC) == SUCCESS) {
zend_print_zval_r(&retval, 0 TSRMLS_CC);
zval_dtor(&retval);
phpdbg_writeln(EMPTY);
}
PHPDBG_G(flags) &= ~PHPDBG_IN_EVAL;
2013-11-13 21:31:33 +00:00
2013-11-13 21:43:54 +00:00
/* switch stepping back on */
if (stepping) {
PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
2013-11-10 18:29:05 +00:00
}
2013-11-10 15:21:20 +00:00
2013-11-10 18:29:05 +00:00
return SUCCESS;
2013-11-10 15:16:45 +00:00
} /* }}} */
2013-11-10 18:21:49 +00:00
static PHPDBG_COMMAND(back) /* {{{ */
{
2013-11-13 21:15:45 +00:00
zval zbacktrace;
zval **tmp;
HashPosition position;
int i = 0, limit = 0;
2013-11-13 21:31:33 +00:00
2013-11-10 18:21:49 +00:00
if (!EG(in_execution)) {
phpdbg_error("Not executing!");
2013-11-10 18:21:49 +00:00
return FAILURE;
}
2013-11-13 21:31:33 +00:00
2013-11-13 21:15:45 +00:00
limit = (expr != NULL) ? atoi(expr) : 0;
2013-11-10 18:21:49 +00:00
zend_fetch_debug_backtrace(&zbacktrace, 0, 0, limit TSRMLS_CC);
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position);
zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), (void**)&tmp, &position) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position)) {
if (i++) {
2013-11-12 23:31:46 +00:00
phpdbg_writeln(",");
2013-11-10 18:21:49 +00:00
}
zend_print_flat_zval_r(*tmp TSRMLS_CC);
}
2013-11-12 23:31:46 +00:00
phpdbg_writeln(EMPTY);
2013-11-10 18:21:49 +00:00
zval_dtor(&zbacktrace);
2013-11-10 18:21:49 +00:00
return SUCCESS;
2013-11-10 15:16:45 +00:00
} /* }}} */
2013-11-10 18:15:00 +00:00
static PHPDBG_COMMAND(print) /* {{{ */
{
2013-11-13 02:39:02 +00:00
if (expr && expr_len > 0L) {
2013-11-13 08:05:26 +00:00
if (phpdbg_do_cmd(phpdbg_print_commands, (char*)expr, expr_len TSRMLS_CC) == FAILURE) {
2013-11-13 02:39:02 +00:00
phpdbg_error("Failed to find print command %s", expr);
2013-11-12 08:26:11 +00:00
}
2013-11-10 18:15:00 +00:00
return SUCCESS;
}
2013-11-11 11:54:41 +00:00
2013-11-12 23:54:41 +00:00
phpdbg_writeln(SEPARATE);
2013-11-13 02:48:22 +00:00
phpdbg_notice("Execution Context Information");
2013-11-12 08:26:11 +00:00
#ifdef HAVE_LIBREADLINE
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Readline\tyes");
2013-11-12 08:26:11 +00:00
#else
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Readline\tno");
2013-11-12 08:26:11 +00:00
#endif
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Exec\t\t%s", PHPDBG_G(exec) ? PHPDBG_G(exec) : "none");
phpdbg_writeln("Compiled\t%s", PHPDBG_G(ops) ? "yes" : "no");
phpdbg_writeln("Stepping\t%s", (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ? "on" : "off");
phpdbg_writeln("Quietness\t%s", (PHPDBG_G(flags) & PHPDBG_IS_QUIET) ? "on" : "off");
2013-11-12 02:19:43 +00:00
2013-11-10 18:15:00 +00:00
if (PHPDBG_G(ops)) {
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Opcodes\t\t%d", PHPDBG_G(ops)->last);
2013-11-10 18:15:00 +00:00
if (PHPDBG_G(ops)->last_var) {
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Variables\t%d", PHPDBG_G(ops)->last_var-1);
2013-11-10 18:15:00 +00:00
} else {
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Variables\tNone");
2013-11-10 18:15:00 +00:00
}
}
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Executing\t%s", EG(in_execution) ? "yes" : "no");
2013-11-10 18:15:00 +00:00
if (EG(in_execution)) {
2013-11-12 23:31:46 +00:00
phpdbg_writeln("VM Return\t%d", PHPDBG_G(vmret));
2013-11-10 18:15:00 +00:00
}
2013-11-12 23:31:46 +00:00
phpdbg_writeln("Classes\t\t%d", zend_hash_num_elements(EG(class_table)));
phpdbg_writeln("Functions\t%d", zend_hash_num_elements(EG(function_table)));
phpdbg_writeln("Constants\t%d", zend_hash_num_elements(EG(zend_constants)));
phpdbg_writeln("Included\t%d", zend_hash_num_elements(&EG(included_files)));
2013-11-11 22:02:34 +00:00
2013-11-12 16:04:41 +00:00
phpdbg_print_breakpoints(PHPDBG_BREAK_FILE TSRMLS_CC);
phpdbg_print_breakpoints(PHPDBG_BREAK_SYM TSRMLS_CC);
phpdbg_print_breakpoints(PHPDBG_BREAK_METHOD TSRMLS_CC);
phpdbg_print_breakpoints(PHPDBG_BREAK_OPLINE TSRMLS_CC);
2013-11-11 22:02:34 +00:00
2013-11-12 23:54:41 +00:00
phpdbg_writeln(SEPARATE);
2013-11-10 18:15:00 +00:00
return SUCCESS;
} /* }}} */
2013-11-10 14:36:30 +00:00
static PHPDBG_COMMAND(break) /* {{{ */
{
2013-11-13 23:13:41 +00:00
phpdbg_param_t param;
2013-11-12 13:33:51 +00:00
char *line_pos;
2013-11-13 21:31:33 +00:00
if (expr_len == 0) {
phpdbg_error("No expression found");
return FAILURE;
2013-11-13 14:22:01 +00:00
}
2013-11-12 02:19:43 +00:00
2013-11-13 21:31:33 +00:00
/* allow advanced breakers to run */
if (phpdbg_do_cmd(phpdbg_break_commands, (char*)expr, expr_len TSRMLS_CC) == SUCCESS) {
return SUCCESS;
}
2013-11-12 02:19:43 +00:00
2013-11-13 23:13:41 +00:00
switch (phpdbg_parse_param(expr, expr_len, &param)) {
case ADDR_PARAM:
phpdbg_set_breakpoint_opline(param.addr TSRMLS_CC);
break;
case NUMERIC_PARAM:
phpdbg_set_breakpoint_file(phpdbg_current_file(TSRMLS_C), param.num TSRMLS_CC);
break;
case METHOD_PARAM:
phpdbg_set_breakpoint_method(param.method.class, param.method.name TSRMLS_CC);
break;
case FILE_PARAM:
phpdbg_set_breakpoint_file(param.file.name, param.file.line TSRMLS_CC);
case STR_PARAM:
phpdbg_set_breakpoint_symbol(param.str TSRMLS_CC);
break;
default:
break;
2013-11-10 14:36:30 +00:00
}
2013-11-13 23:13:41 +00:00
phpdbg_clear_param(&param);
2013-11-10 14:36:30 +00:00
return SUCCESS;
} /* }}} */
static PHPDBG_COMMAND(quit) /* {{{ */
{
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
zend_bailout();
2013-11-10 11:36:57 +00:00
return SUCCESS;
} /* }}} */
static int clean_non_persistent_constant_full(const zend_constant *c TSRMLS_DC) /* {{{ */
{
return (c->flags & CONST_PERSISTENT) ? 0 : 1;
} /* }}} */
static int clean_non_persistent_class_full(zend_class_entry **ce TSRMLS_DC) /* {{{ */
{
return ((*ce)->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
} /* }}} */
static int clean_non_persistent_function_full(zend_function *function TSRMLS_DC) /* {{{ */
{
return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
} /* }}} */
static PHPDBG_COMMAND(clean) /* {{{ */
{
2013-11-13 21:43:54 +00:00
if (EG(in_execution)) {
phpdbg_error("Cannot clean environment while executing");
return FAILURE;
}
2013-11-13 21:31:33 +00:00
2013-11-13 21:43:54 +00:00
phpdbg_notice("Cleaning Execution Environment");
2013-11-13 21:43:54 +00:00
phpdbg_writeln("Classes\t\t\t%d", zend_hash_num_elements(EG(class_table)));
phpdbg_writeln("Functions\t\t%d", zend_hash_num_elements(EG(function_table)));
phpdbg_writeln("Constants\t\t%d", zend_hash_num_elements(EG(zend_constants)));
phpdbg_writeln("Includes\t\t%d", zend_hash_num_elements(&EG(included_files)));
2013-11-13 21:43:54 +00:00
phpdbg_clean(1 TSRMLS_CC);
2013-11-13 21:31:33 +00:00
2013-11-13 21:43:54 +00:00
phpdbg_notice("Clean Execution Environment");
phpdbg_writeln("Classes\t\t\t%d", zend_hash_num_elements(EG(class_table)));
phpdbg_writeln("Functions\t\t%d", zend_hash_num_elements(EG(function_table)));
phpdbg_writeln("Constants\t\t%d", zend_hash_num_elements(EG(zend_constants)));
phpdbg_writeln("Includes\t\t%d", zend_hash_num_elements(&EG(included_files)));
2013-11-11 22:02:34 +00:00
return SUCCESS;
} /* }}} */
2013-11-11 13:31:41 +00:00
static PHPDBG_COMMAND(clear) /* {{{ */
{
phpdbg_notice("Clearing Breakpoints");
2013-11-13 21:31:33 +00:00
2013-11-12 23:31:46 +00:00
phpdbg_writeln("File\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]));
phpdbg_writeln("Functions\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]));
phpdbg_writeln("Methods\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]));
phpdbg_writeln("Oplines\t\t\t%d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]));
2013-11-13 21:31:33 +00:00
2013-11-11 17:19:05 +00:00
phpdbg_clear_breakpoints(TSRMLS_C);
2013-11-11 22:02:34 +00:00
2013-11-11 13:31:41 +00:00
return SUCCESS;
} /* }}} */
2013-11-13 17:35:51 +00:00
static PHPDBG_COMMAND(aliases) /* {{{ */
{
2013-11-13 21:43:54 +00:00
const phpdbg_command_t *prompt_command = phpdbg_prompt_commands;
2013-11-13 17:35:51 +00:00
phpdbg_notice("Welcome to phpdbg, the interactive PHP debugger, v%s",
PHPDBG_VERSION);
phpdbg_notice("Aliases");
2013-11-13 21:43:54 +00:00
while (prompt_command && prompt_command->name) {
if (prompt_command->alias) {
phpdbg_writeln("\t%c -> %s\t%s", prompt_command->alias,
prompt_command->name, prompt_command->tip);
}
++prompt_command;
2013-11-13 17:35:51 +00:00
}
2013-11-13 21:31:33 +00:00
2013-11-13 21:43:54 +00:00
phpdbg_notice("Please report bugs to <%s>", PHPDBG_ISSUES);
2013-11-13 17:35:51 +00:00
return SUCCESS;
} /* }}} */
static PHPDBG_COMMAND(help) /* {{{ */
{
phpdbg_notice("Welcome to phpdbg, the interactive PHP debugger, v%s",
PHPDBG_VERSION);
2013-11-10 11:36:57 +00:00
2013-11-12 04:20:14 +00:00
if (expr_len > 0L) {
if (phpdbg_do_cmd(phpdbg_help_commands, (char*)expr, expr_len TSRMLS_CC) == FAILURE) {
2013-11-13 02:48:22 +00:00
phpdbg_error("Failed to find help command: %s", expr);
2013-11-12 04:20:14 +00:00
}
} else {
2013-11-10 18:21:49 +00:00
const phpdbg_command_t *prompt_command = phpdbg_prompt_commands;
const phpdbg_command_t *help_command = phpdbg_help_commands;
2013-11-12 23:31:46 +00:00
phpdbg_writeln("To get help regarding a specific command type \"help command\"");
2013-11-10 18:21:49 +00:00
phpdbg_notice("Commands");
2013-11-10 18:21:49 +00:00
while (prompt_command && prompt_command->name) {
2013-11-13 17:35:51 +00:00
phpdbg_writeln(
"\t%s\t%s", prompt_command->name, prompt_command->tip);
2013-11-10 18:21:49 +00:00
++prompt_command;
}
2013-11-13 21:31:33 +00:00
phpdbg_notice("Helpers Loaded");
2013-11-10 18:21:49 +00:00
while (help_command && help_command->name) {
2013-11-12 23:31:46 +00:00
phpdbg_writeln("\t%s\t%s", help_command->name, help_command->tip);
2013-11-10 18:21:49 +00:00
++help_command;
}
}
phpdbg_notice("Please report bugs to <%s>", PHPDBG_ISSUES);
2013-11-10 18:21:49 +00:00
return SUCCESS;
} /* }}} */
2013-11-10 21:23:18 +00:00
static PHPDBG_COMMAND(quiet) { /* {{{ */
if (atoi(expr)) {
PHPDBG_G(flags) |= PHPDBG_IS_QUIET;
} else {
PHPDBG_G(flags) &= ~PHPDBG_IS_QUIET;
}
2013-11-11 22:02:34 +00:00
phpdbg_notice("Quietness %s",
(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ? "enabled" : "disabled");
2013-11-11 22:02:34 +00:00
2013-11-10 21:23:18 +00:00
return SUCCESS;
} /* }}} */
2013-11-11 23:40:03 +00:00
static PHPDBG_COMMAND(list) /* {{{ */
{
2013-11-12 02:50:15 +00:00
if (phpdbg_is_empty(expr) || phpdbg_is_numeric(expr)) {
2013-11-12 02:19:43 +00:00
long offset = 0, count = strtol(expr, NULL, 0);
const char *filename = PHPDBG_G(exec);
if (zend_is_executing(TSRMLS_C)) {
filename = zend_get_executed_filename(TSRMLS_C);
offset = zend_get_executed_lineno(TSRMLS_C);
} else if (!filename) {
phpdbg_error("No file to list");
2013-11-12 02:19:43 +00:00
return SUCCESS;
}
2013-11-11 23:40:03 +00:00
phpdbg_list_file(filename, count, offset TSRMLS_CC);
2013-11-12 02:19:43 +00:00
} else {
2013-11-12 06:43:23 +00:00
HashTable *func_table = EG(function_table);
2013-11-12 02:19:43 +00:00
zend_function* fbc;
2013-11-12 06:43:23 +00:00
const char *func_name = expr;
size_t func_name_len = expr_len;
2013-11-12 06:43:23 +00:00
/* search active scope if begins with period */
if (func_name[0] == '.') {
if (EG(scope)) {
func_name++;
func_name_len--;
2013-11-12 06:43:23 +00:00
func_table = &EG(scope)->function_table;
} else {
phpdbg_error("No active class");
2013-11-12 06:43:23 +00:00
return FAILURE;
}
} else if (!EG(function_table)) {
phpdbg_error("No function table loaded");
2013-11-12 02:19:43 +00:00
return SUCCESS;
2013-11-12 06:43:23 +00:00
} else {
func_table = EG(function_table);
2013-11-12 02:19:43 +00:00
}
2013-11-13 02:43:09 +00:00
if (zend_hash_find(func_table, func_name, func_name_len+1,
2013-11-12 02:19:43 +00:00
(void**)&fbc) == SUCCESS) {
phpdbg_list_function(fbc TSRMLS_CC);
2013-11-12 02:59:10 +00:00
} else {
phpdbg_error("Function %s not found", func_name);
2013-11-12 02:19:43 +00:00
}
}
2013-11-12 00:24:24 +00:00
return SUCCESS;
2013-11-11 23:40:03 +00:00
} /* }}} */
int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC) /* {{{ */
{
char *expr = NULL;
#ifndef _WIN32
const char *cmd = strtok_r(cmd_line, " ", &expr);
2013-11-12 13:21:55 +00:00
#else
const char *cmd = strtok_s(cmd_line, " ", &expr);
2013-11-12 13:21:55 +00:00
#endif
size_t expr_len = (cmd != NULL) ? strlen(cmd) : 0;
2013-11-13 21:31:33 +00:00
2013-11-13 05:36:01 +00:00
while (command && command->name && command->handler) {
2013-11-13 17:35:51 +00:00
if ((command->name_len == expr_len
&& memcmp(cmd, command->name, expr_len) == 0)
2013-11-13 18:17:06 +00:00
|| ((expr_len == 1) && (command->alias && command->alias == cmd_line[0]))) {
2013-11-13 21:31:33 +00:00
2013-11-11 11:54:41 +00:00
PHPDBG_G(last) = (phpdbg_command_t*) command;
PHPDBG_G(last_params) = expr;
2013-11-13 02:39:02 +00:00
PHPDBG_G(last_params_len) = (cmd_len - expr_len) ?
(((cmd_len - expr_len) - sizeof(" "))+1) : 0;
2013-11-13 21:31:33 +00:00
return command->handler(
PHPDBG_G(last_params), PHPDBG_G(last_params_len) TSRMLS_CC);
}
++command;
}
return FAILURE;
} /* }}} */
int phpdbg_interactive(TSRMLS_D) /* {{{ */
{
2013-11-12 04:20:14 +00:00
size_t cmd_len;
2013-11-12 04:20:14 +00:00
#ifndef HAVE_LIBREADLINE
char cmd[PHPDBG_MAX_CMD];
while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING) &&
2013-11-13 05:36:01 +00:00
phpdbg_write(PROMPT) &&
(fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL)) {
2013-11-12 04:20:14 +00:00
cmd_len = strlen(cmd) - 1;
#else
char *cmd = NULL;
2013-11-12 04:20:14 +00:00
while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-12 23:54:41 +00:00
cmd = readline(PROMPT);
2013-11-13 05:36:01 +00:00
if (cmd) {
cmd_len = strlen(cmd);
} else cmd_len = 0L;
2013-11-12 04:20:14 +00:00
#endif
/* trim space from end of input */
2013-11-13 04:34:02 +00:00
while (*cmd && isspace(cmd[cmd_len-1]))
2013-11-12 04:20:14 +00:00
cmd_len--;
2013-11-12 04:20:14 +00:00
/* ensure string is null terminated */
cmd[cmd_len] = '\0';
2013-11-10 11:36:57 +00:00
2013-11-13 08:05:26 +00:00
if (*cmd && cmd_len > 0L) {
2013-11-12 04:20:14 +00:00
#ifdef HAVE_LIBREADLINE
add_history(cmd);
#endif
2013-11-12 04:53:04 +00:00
2013-11-10 14:28:14 +00:00
switch (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
case FAILURE:
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
phpdbg_error("Failed to execute %s!", cmd);
2013-11-10 16:34:12 +00:00
}
2013-11-10 14:28:14 +00:00
break;
2013-11-10 15:11:56 +00:00
2013-11-12 01:31:51 +00:00
case PHPDBG_NEXT: {
if (!EG(in_execution)) {
phpdbg_error("Not running");
2013-11-12 01:31:51 +00:00
}
2013-11-10 14:28:14 +00:00
return PHPDBG_NEXT;
2013-11-12 01:31:51 +00:00
}
2013-11-10 14:28:14 +00:00
}
2013-11-13 21:31:33 +00:00
2013-11-13 04:20:39 +00:00
#ifdef HAVE_LIBREADLINE
if (cmd) {
free(cmd);
cmd = NULL;
}
#endif
2013-11-13 04:34:02 +00:00
} else if (PHPDBG_G(last)) {
PHPDBG_G(last)->handler(
PHPDBG_G(last_params), PHPDBG_G(last_params_len) TSRMLS_CC);
}
}
2013-11-13 21:31:33 +00:00
2013-11-13 04:34:02 +00:00
#ifdef HAVE_LIBREADLINE
if (cmd) {
free(cmd);
}
#endif
2013-11-10 15:11:56 +00:00
2013-11-10 14:28:14 +00:00
return SUCCESS;
} /* }}} */
2013-11-10 13:01:46 +00:00
2013-11-12 09:26:26 +00:00
void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags TSRMLS_DC) /* {{{ */
2013-11-10 20:06:19 +00:00
{
/* force out a line while stepping so the user knows what is happening */
if (ignore_flags ||
(!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ||
2013-11-12 09:52:59 +00:00
(PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
2013-11-10 21:23:18 +00:00
zend_op *opline = execute_data->opline;
2013-11-13 04:47:07 +00:00
/* output line info */
phpdbg_notice("#%lu %p %s %s",
opline->lineno,
2013-11-13 21:31:33 +00:00
opline, phpdbg_decode_opcode(opline->opcode),
2013-11-13 04:47:07 +00:00
execute_data->op_array->filename ? execute_data->op_array->filename : "unknown");
2013-11-12 09:52:59 +00:00
}
} /* }}} */
void phpdbg_clean(zend_bool full TSRMLS_DC) /* {{{ */
{
zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC);
2013-11-12 09:52:59 +00:00
/* this is implicitly required */
if (PHPDBG_G(ops)) {
destroy_op_array(PHPDBG_G(ops) TSRMLS_CC);
2013-11-12 09:52:59 +00:00
efree(PHPDBG_G(ops));
PHPDBG_G(ops) = NULL;
}
if (full) {
zend_hash_reverse_apply(EG(function_table),
(apply_func_t) clean_non_persistent_function_full TSRMLS_CC);
zend_hash_reverse_apply(EG(class_table),
(apply_func_t) clean_non_persistent_class_full TSRMLS_CC);
zend_hash_reverse_apply(EG(zend_constants),
(apply_func_t) clean_non_persistent_constant_full TSRMLS_CC);
2013-11-12 09:52:59 +00:00
zend_hash_clean(&EG(included_files));
2013-11-10 21:23:18 +00:00
}
2013-11-10 18:05:11 +00:00
} /* }}} */
2013-11-10 18:21:49 +00:00
void phpdbg_execute_ex(zend_execute_data *execute_data TSRMLS_DC) /* {{{ */
2013-11-10 13:01:46 +00:00
{
2013-11-10 15:16:45 +00:00
zend_bool original_in_execution = EG(in_execution);
2013-11-10 13:01:46 +00:00
EG(in_execution) = 1;
if (0) {
zend_vm_enter:
execute_data = zend_create_execute_data_from_op_array(EG(active_op_array), 1 TSRMLS_CC);
}
while (1) {
#ifdef ZEND_WIN32
if (EG(timed_out)) {
zend_timeout(0);
}
#endif
#define DO_INTERACTIVE() do {\
switch (phpdbg_interactive(TSRMLS_C)) {\
case PHPDBG_NEXT:\
goto next;\
}\
} while(!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING))
2013-11-13 14:22:01 +00:00
/* allow conditional breakpoints to access the vm uninterrupted */
if (!(PHPDBG_G(flags) & PHPDBG_IN_COND_BP)) {
2013-11-13 21:31:33 +00:00
2013-11-13 14:50:13 +00:00
/* not while in conditionals */
phpdbg_print_opline(
execute_data, 0 TSRMLS_CC);
2013-11-13 21:31:33 +00:00
/* conditions cannot be executed by eval()'d code */
2013-11-13 21:31:33 +00:00
if (!(PHPDBG_G(flags) & PHPDBG_IN_EVAL)
&& (PHPDBG_G(flags) & PHPDBG_HAS_COND_BP)
2013-11-13 14:22:01 +00:00
&& phpdbg_find_conditional_breakpoint(TSRMLS_C) == SUCCESS) {
DO_INTERACTIVE();
2013-11-13 14:22:01 +00:00
}
if ((PHPDBG_G(flags) & PHPDBG_HAS_FILE_BP)
&& phpdbg_find_breakpoint_file(execute_data->op_array TSRMLS_CC) == SUCCESS) {
DO_INTERACTIVE();
2013-11-11 17:12:31 +00:00
}
2013-11-13 14:22:01 +00:00
if ((PHPDBG_G(flags) & (PHPDBG_HAS_METHOD_BP|PHPDBG_HAS_SYM_BP))) {
zend_execute_data *previous = execute_data->prev_execute_data;
if (previous && previous != execute_data && previous->opline) {
/* check we are the beginning of a function entry */
if (execute_data->opline == EG(active_op_array)->opcodes) {
switch (previous->opline->opcode) {
case ZEND_DO_FCALL:
case ZEND_DO_FCALL_BY_NAME:
case ZEND_INIT_STATIC_METHOD_CALL: {
if (phpdbg_find_breakpoint_symbol(previous->function_state.function TSRMLS_CC) == SUCCESS) {
DO_INTERACTIVE();
}
2013-11-13 14:22:01 +00:00
} break;
}
2013-11-11 17:12:31 +00:00
}
}
}
2013-11-12 02:19:43 +00:00
2013-11-13 14:22:01 +00:00
if ((PHPDBG_G(flags) & PHPDBG_HAS_OPLINE_BP)
&& phpdbg_find_breakpoint_opline(execute_data->opline TSRMLS_CC) == SUCCESS) {
DO_INTERACTIVE();
2013-11-11 17:12:31 +00:00
}
}
2013-11-10 14:36:30 +00:00
2013-11-13 14:22:01 +00:00
next:
2013-11-11 17:12:31 +00:00
PHPDBG_G(vmret) = execute_data->opline->handler(execute_data TSRMLS_CC);
2013-11-13 14:22:01 +00:00
if (!(PHPDBG_G(flags) & PHPDBG_IN_COND_BP)) {
if ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING)) {
DO_INTERACTIVE();
2013-11-11 17:12:31 +00:00
}
}
if (PHPDBG_G(vmret) > 0) {
switch (PHPDBG_G(vmret)) {
case 1:
EG(in_execution) = original_in_execution;
return;
case 2:
goto zend_vm_enter;
break;
case 3:
execute_data = EG(current_execute_data);
break;
default:
2013-11-10 13:01:46 +00:00
break;
}
}
}
zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
2013-11-10 18:21:49 +00:00
} /* }}} */