php-src/phpdbg_prompt.c

729 lines
21 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"
#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"
static const phpdbg_command_t phpdbg_prompt_commands[];
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) /* {{{ */
{
if (PHPDBG_G(exec)) {
phpdbg_notice("Unsetting old execution context: %s", PHPDBG_G(exec));
2013-11-10 18:29:05 +00:00
efree(PHPDBG_G(exec));
PHPDBG_G(exec) = NULL;
}
2013-11-10 11:48:01 +00:00
2013-11-10 18:29:05 +00:00
if (PHPDBG_G(ops)) {
phpdbg_notice("Destroying compiled opcodes");
2013-11-12 09:52:59 +00:00
phpdbg_clean(0 TSRMLS_CC);
2013-11-10 18:29:05 +00:00
}
2013-11-10 11:36:57 +00:00
2013-11-10 18:29:05 +00:00
PHPDBG_G(exec) = estrndup(expr, PHPDBG_G(exec_len) = expr_len);
2013-11-10 11:36:57 +00:00
phpdbg_notice("Set execution context: %s", PHPDBG_G(exec));
2013-11-10 11:48:01 +00:00
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-10 21:07:29 +00:00
if (!EG(in_execution)) {
phpdbg_notice("Attempting compilation of %s", PHPDBG_G(exec));
2013-11-11 22:02:34 +00:00
2013-11-10 21:07:29 +00:00
if (php_stream_open_for_zend_ex(PHPDBG_G(exec), &fh,
USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC) == SUCCESS) {
PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE TSRMLS_CC);
2013-11-10 21:07:29 +00:00
zend_destroy_file_handle(&fh TSRMLS_CC);
phpdbg_notice("Success");
2013-11-10 21:07:29 +00:00
return SUCCESS;
} else {
phpdbg_error("Could not open file %s", PHPDBG_G(exec));
2013-11-10 21:07:29 +00:00
}
} else {
phpdbg_error("Cannot compile while in execution");
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) /* {{{ */
{
if (PHPDBG_G(exec)) {
2013-11-11 14:07:07 +00:00
if (!EG(in_execution)) {
if (PHPDBG_G(ops)) {
phpdbg_error("Destroying previously compiled opcodes");
2013-11-12 09:52:59 +00:00
phpdbg_clean(0 TSRMLS_CC);
2013-11-11 14:07:07 +00:00
}
2013-11-10 18:29:05 +00:00
}
return phpdbg_compile(TSRMLS_C);
} else {
phpdbg_error("No execution context");
2013-11-10 18:29:05 +00:00
return FAILURE;
}
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)) {
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);
zend_try {
zend_execute(EG(active_op_array) TSRMLS_CC);
} zend_catch {
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();
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) /* {{{ */
{
zval retval;
2013-11-12 02:19:43 +00:00
2013-11-11 17:09:08 +00:00
if (expr_len) {
zend_bool stepping = (PHPDBG_G(flags) & PHPDBG_IS_STEPPING);
2013-11-12 02:19:43 +00:00
/* disable stepping while eval() in progress */
PHPDBG_G(flags) &= ~ PHPDBG_IS_STEPPING;
2013-11-12 02:19:43 +00:00
2013-11-13 02:50:00 +00:00
if (zend_eval_stringl((char*)expr, expr_len,
2013-11-10 21:57:43 +00:00
&retval, "eval()'d code" TSRMLS_CC) == SUCCESS) {
zend_print_zval_r(&retval, 0 TSRMLS_CC);
2013-11-12 09:52:59 +00:00
zval_dtor(&retval);
2013-11-12 23:31:46 +00:00
phpdbg_writeln(EMPTY);
2013-11-10 18:29:05 +00:00
}
2013-11-12 02:19:43 +00:00
/* switch stepping back on */
if (stepping) {
PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
}
2013-11-10 18:29:05 +00:00
} else {
phpdbg_error("No expression provided!");
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
return SUCCESS;
2013-11-10 15:16:45 +00:00
} /* }}} */
2013-11-10 18:21:49 +00:00
static PHPDBG_COMMAND(back) /* {{{ */
{
if (!EG(in_execution)) {
phpdbg_error("Not executing!");
2013-11-10 18:21:49 +00:00
return FAILURE;
}
zval zbacktrace;
zval **tmp;
HashPosition position;
int i = 0, limit = (expr != NULL) ? atoi(expr) : 0;
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-12 08:26:11 +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-12 13:33:51 +00:00
char *line_pos;
2013-11-12 02:19:43 +00:00
2013-11-12 04:20:14 +00:00
if (expr_len <= 0L) {
phpdbg_error("No expression found");
return FAILURE;
}
2013-11-12 02:19:43 +00:00
2013-11-12 13:33:51 +00:00
line_pos = strchr(expr, ':');
2013-11-12 02:19:43 +00:00
2013-11-10 14:36:30 +00:00
if (line_pos) {
2013-11-12 13:33:51 +00:00
char *class;
char *func;
/* break class::method */
if (phpdbg_is_class_method(expr, expr_len, &class, &func)) {
phpdbg_set_breakpoint_method(class, func TSRMLS_CC);
} else {
/* break file:line */
char path[MAXPATHLEN], resolved_name[MAXPATHLEN];
long line_num = strtol(line_pos+1, NULL, 0);
if (line_num) {
memcpy(path, expr, line_pos - expr);
path[line_pos - expr] = 0;
if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) {
phpdbg_error("Failed to expand path %s", path);
return FAILURE;
}
phpdbg_set_breakpoint_file(resolved_name, line_num TSRMLS_CC);
} else {
phpdbg_error("No line specified in expression %s", expr);
return FAILURE;
2013-11-11 09:36:33 +00:00
}
2013-11-12 13:33:51 +00:00
}
} else {
2013-11-12 13:33:51 +00:00
/* break 0xc0ffee */
if (phpdbg_is_addr(expr)) {
zend_ulong opline = strtoul(expr, 0, 16);
phpdbg_set_breakpoint_opline(opline TSRMLS_CC);
} else if (phpdbg_is_numeric(expr)) {
2013-11-12 13:33:51 +00:00
/* break 1337 */
const char *filename = zend_get_executed_filename(TSRMLS_C);
long line_num = strtol(expr, NULL, 0);
if (!filename) {
phpdbg_error("No file context found");
return FAILURE;
}
phpdbg_set_breakpoint_file(filename, line_num TSRMLS_CC);
} else {
2013-11-12 13:33:51 +00:00
/* break symbol */
char name[200];
size_t name_len = strlen(expr);
if (name_len) {
name_len = MIN(name_len, 200);
memcpy(name, expr, name_len);
name[name_len] = 0;
phpdbg_set_breakpoint_symbol(name TSRMLS_CC);
} else {
phpdbg_error("Malformed break command found");
return FAILURE;
}
2013-11-11 11:54:41 +00:00
}
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) /* {{{ */
{
if (!EG(in_execution)) {
phpdbg_notice("Cleaning Execution Environment");
2013-11-12 23:31:46 +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-12 09:52:59 +00:00
phpdbg_clean(1 TSRMLS_CC);
phpdbg_notice("Clean Execution Environment");
2013-11-12 23:31:46 +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)));
} else {
phpdbg_error("Cannot clean environment while executing");
return FAILURE;
}
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-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-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;
} /* }}} */
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-12 23:31:46 +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-10 11:36:57 +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
} /* }}} */
2013-11-10 11:03:29 +00:00
static const phpdbg_command_t phpdbg_prompt_commands[] = {
2013-11-10 11:48:01 +00:00
PHPDBG_COMMAND_D(exec, "set execution context"),
PHPDBG_COMMAND_D(compile, "attempt to pre-compile execution context"),
2013-11-10 14:28:14 +00:00
PHPDBG_COMMAND_D(step, "step through execution"),
2013-11-10 21:09:35 +00:00
PHPDBG_COMMAND_D(next, "continue execution"),
2013-11-10 13:01:46 +00:00
PHPDBG_COMMAND_D(run, "attempt execution"),
2013-11-10 14:43:46 +00:00
PHPDBG_COMMAND_D(eval, "evaluate some code"),
2013-11-10 11:48:01 +00:00
PHPDBG_COMMAND_D(print, "print something"),
PHPDBG_COMMAND_D(break, "set breakpoint"),
2013-11-10 15:16:45 +00:00
PHPDBG_COMMAND_D(back, "show backtrace"),
2013-11-12 02:34:10 +00:00
PHPDBG_COMMAND_D(list, "list specified line or function"),
PHPDBG_COMMAND_D(clean, "clean the execution environment"),
2013-11-11 13:31:41 +00:00
PHPDBG_COMMAND_D(clear, "clear breakpoints"),
2013-11-10 11:35:59 +00:00
PHPDBG_COMMAND_D(help, "show help menu"),
2013-11-10 21:23:18 +00:00
PHPDBG_COMMAND_D(quiet, "silence some output"),
2013-11-10 11:35:59 +00:00
PHPDBG_COMMAND_D(quit, "exit phpdbg"),
{NULL, 0, 0}
};
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;
while (command && command->name) {
if (command->name_len == expr_len
&& memcmp(cmd, command->name, expr_len) == 0) {
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;
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];
2013-11-12 04:20:14 +00:00
phpdbg_interactive_enter:
2013-11-12 23:31:46 +00:00
phpdbg_write(PROMPT);
2013-11-13 01:35:39 +00:00
while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING) &&
2013-11-10 16:30:36 +00:00
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
phpdbg_interactive_enter:
while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-12 23:54:41 +00:00
cmd = readline(PROMPT);
2013-11-12 04:20:14 +00:00
cmd_len = strlen(cmd);
#endif
/* trim space from end of input */
while (isspace(cmd[cmd_len-1]))
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-12 04:20:14 +00:00
if (cmd && cmd_len > 0L) {
#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
}
} else if (PHPDBG_G(last)) {
PHPDBG_G(last)->handler(
PHPDBG_G(last_params), PHPDBG_G(last_params_len) TSRMLS_CC);
}
2013-11-10 11:36:57 +00:00
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-12 04:20:14 +00:00
goto phpdbg_interactive_enter;
2013-11-10 16:33:35 +00:00
}
}
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-10 18:15:00 +00:00
phpdbg_notice("OPLINE: %p:%s",
opline, phpdbg_decode_opcode(opline->opcode));
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
2013-11-11 17:06:30 +00:00
phpdbg_print_opline(
2013-11-12 09:26:26 +00:00
execute_data, 0 TSRMLS_CC);
2013-11-11 17:06:30 +00:00
if ((PHPDBG_G(flags) & PHPDBG_HAS_FILE_BP)
2013-11-11 17:12:31 +00:00
&& phpdbg_find_breakpoint_file(execute_data->op_array TSRMLS_CC) == SUCCESS) {
while (phpdbg_interactive(TSRMLS_C) != PHPDBG_NEXT) {
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-11 17:12:31 +00:00
continue;
}
}
}
if ((PHPDBG_G(flags) & (PHPDBG_HAS_METHOD_BP|PHPDBG_HAS_SYM_BP))) {
2013-11-10 20:59:35 +00:00
zend_execute_data *previous = execute_data->prev_execute_data;
2013-11-10 21:38:58 +00:00
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: {
2013-11-12 00:47:27 +00:00
if (phpdbg_find_breakpoint_symbol(previous->function_state.function TSRMLS_CC) == SUCCESS) {
while (phpdbg_interactive(TSRMLS_C) != PHPDBG_NEXT) {
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
continue;
}
}
2013-11-11 17:12:31 +00:00
}
} break;
2013-11-11 17:12:31 +00:00
}
}
}
2013-11-10 20:59:35 +00:00
}
2013-11-12 02:19:43 +00:00
if ((PHPDBG_G(flags) & PHPDBG_HAS_OPLINE_BP)
2013-11-11 17:12:31 +00:00
&& phpdbg_find_breakpoint_opline(execute_data->opline TSRMLS_CC) == SUCCESS) {
while (phpdbg_interactive(TSRMLS_C) != PHPDBG_NEXT) {
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-11 17:12:31 +00:00
continue;
}
}
}
2013-11-10 14:36:30 +00:00
2013-11-11 17:12:31 +00:00
PHPDBG_G(vmret) = execute_data->opline->handler(execute_data TSRMLS_CC);
if ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING)) {
2013-11-11 17:12:31 +00:00
while (phpdbg_interactive(TSRMLS_C) != PHPDBG_NEXT) {
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
2013-11-11 17:12:31 +00:00
continue;
}
}
}
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
} /* }}} */