2013-11-12 16:26:11 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-20 00:33:14 +08:00
|
|
|
| PHP Version 7 |
|
2013-11-12 16:26:11 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2014-01-18 06:09:07 +08:00
|
|
|
| Copyright (c) 1997-2014 The PHP Group |
|
2013-11-12 16:26:11 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| 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> |
|
2013-12-18 18:02:50 +08:00
|
|
|
| Authors: Bob Weinand <bwoebi@php.net> |
|
2013-11-12 16:26:11 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "phpdbg.h"
|
|
|
|
#include "phpdbg_print.h"
|
2013-11-13 06:39:39 +08:00
|
|
|
#include "phpdbg_utils.h"
|
2013-11-13 09:35:39 +08:00
|
|
|
#include "phpdbg_opcode.h"
|
2013-11-18 05:01:41 +08:00
|
|
|
#include "phpdbg_prompt.h"
|
2013-11-12 16:26:11 +08:00
|
|
|
|
2013-11-12 17:26:26 +08:00
|
|
|
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
|
|
|
|
|
2014-02-20 04:18:49 +08:00
|
|
|
#define PHPDBG_PRINT_COMMAND_D(f, h, a, m, l, s) \
|
|
|
|
PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[9])
|
|
|
|
|
2014-02-03 00:35:28 +08:00
|
|
|
const phpdbg_command_t phpdbg_print_commands[] = {
|
2014-02-20 04:18:49 +08:00
|
|
|
PHPDBG_PRINT_COMMAND_D(exec, "print out the instructions in the execution context", 'e', print_exec, NULL, 0),
|
|
|
|
PHPDBG_PRINT_COMMAND_D(opline, "print out the instruction in the current opline", 'o', print_opline, NULL, 0),
|
|
|
|
PHPDBG_PRINT_COMMAND_D(class, "print out the instructions in the specified class", 'c', print_class, NULL, "s"),
|
|
|
|
PHPDBG_PRINT_COMMAND_D(method, "print out the instructions in the specified method", 'm', print_method, NULL, "m"),
|
|
|
|
PHPDBG_PRINT_COMMAND_D(func, "print out the instructions in the specified function", 'f', print_func, NULL, "s"),
|
|
|
|
PHPDBG_PRINT_COMMAND_D(stack, "print out the instructions in the current stack", 's', print_stack, NULL, 0),
|
2014-02-03 00:35:28 +08:00
|
|
|
PHPDBG_END_COMMAND
|
|
|
|
};
|
|
|
|
|
2013-11-12 17:26:26 +08:00
|
|
|
PHPDBG_PRINT(opline) /* {{{ */
|
2013-11-12 16:26:11 +08:00
|
|
|
{
|
2013-11-12 17:26:26 +08:00
|
|
|
if (EG(in_execution) && EG(current_execute_data)) {
|
2013-11-26 18:02:58 +08:00
|
|
|
phpdbg_print_opline(EG(current_execute_data), 1 TSRMLS_CC);
|
2013-11-12 17:26:26 +08:00
|
|
|
} else {
|
2013-11-13 06:39:39 +08:00
|
|
|
phpdbg_error("Not Executing!");
|
2013-11-12 17:26:26 +08:00
|
|
|
}
|
2013-11-13 06:39:39 +08:00
|
|
|
|
2013-11-12 16:26:11 +08:00
|
|
|
return SUCCESS;
|
|
|
|
} /* }}} */
|
2013-11-13 08:36:47 +08:00
|
|
|
|
2013-11-19 05:55:29 +08:00
|
|
|
static inline void phpdbg_print_function_helper(zend_function *method TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2013-11-26 18:02:58 +08:00
|
|
|
switch (method->type) {
|
|
|
|
case ZEND_USER_FUNCTION: {
|
|
|
|
zend_op_array* op_array = &(method->op_array);
|
2013-11-21 21:52:20 +08:00
|
|
|
HashTable vars;
|
|
|
|
|
2013-11-26 18:02:58 +08:00
|
|
|
if (op_array) {
|
|
|
|
zend_op *opline = &(op_array->opcodes[0]);
|
2014-08-26 01:28:33 +08:00
|
|
|
uint32_t opcode = 0,
|
2013-11-26 18:02:58 +08:00
|
|
|
end = op_array->last-1;
|
|
|
|
|
|
|
|
if (method->common.scope) {
|
|
|
|
phpdbg_writeln("\tL%d-%d %s::%s() %s",
|
|
|
|
op_array->line_start, op_array->line_end,
|
|
|
|
method->common.scope->name,
|
|
|
|
method->common.function_name,
|
|
|
|
op_array->filename ? op_array->filename : "unknown");
|
|
|
|
} else {
|
|
|
|
phpdbg_writeln("\tL%d-%d %s() %s",
|
|
|
|
method->common.function_name ? op_array->line_start : 0,
|
|
|
|
method->common.function_name ? op_array->line_end : 0,
|
|
|
|
method->common.function_name ? method->common.function_name : "{main}",
|
|
|
|
op_array->filename ? op_array->filename : "unknown");
|
|
|
|
}
|
2013-11-18 05:01:41 +08:00
|
|
|
|
2013-11-21 21:52:20 +08:00
|
|
|
zend_hash_init(&vars, op_array->last, NULL, NULL, 0);
|
2013-11-26 18:02:58 +08:00
|
|
|
do {
|
|
|
|
char *decode = phpdbg_decode_opline(op_array, opline, &vars TSRMLS_CC);
|
|
|
|
if (decode != NULL) {
|
|
|
|
phpdbg_writeln("\t\tL%u\t%p %-30s %s",
|
|
|
|
opline->lineno,
|
|
|
|
opline,
|
|
|
|
phpdbg_decode_opcode(opline->opcode),
|
|
|
|
decode);
|
|
|
|
free(decode);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("\tFailed to decode opline %16p", opline);
|
|
|
|
}
|
|
|
|
opline++;
|
2014-02-18 17:13:42 +08:00
|
|
|
} while (opcode++ < end);
|
2013-11-26 18:02:58 +08:00
|
|
|
zend_hash_destroy(&vars);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
if (method->common.scope) {
|
|
|
|
phpdbg_writeln("\tInternal %s::%s()", method->common.scope->name, method->common.function_name);
|
|
|
|
} else {
|
|
|
|
phpdbg_writeln("\tInternal %s()", method->common.function_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 05:55:29 +08:00
|
|
|
} /* }}} */
|
2013-11-13 10:26:37 +08:00
|
|
|
|
2013-11-17 17:30:44 +08:00
|
|
|
PHPDBG_PRINT(exec) /* {{{ */
|
|
|
|
{
|
2013-11-26 18:02:58 +08:00
|
|
|
if (PHPDBG_G(exec)) {
|
|
|
|
if (!PHPDBG_G(ops)) {
|
|
|
|
phpdbg_compile(TSRMLS_C);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PHPDBG_G(ops)) {
|
|
|
|
phpdbg_notice("Context %s", PHPDBG_G(exec));
|
|
|
|
|
|
|
|
phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops) TSRMLS_CC);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
phpdbg_error("No execution context set");
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
2013-11-17 17:30:44 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2013-11-18 22:24:58 +08:00
|
|
|
PHPDBG_PRINT(stack) /* {{{ */
|
|
|
|
{
|
2013-11-25 18:17:57 +08:00
|
|
|
zend_op_array *ops = EG(active_op_array);
|
|
|
|
|
2013-11-26 18:02:58 +08:00
|
|
|
if (EG(in_execution) && ops) {
|
|
|
|
if (ops->function_name) {
|
2013-11-25 18:17:57 +08:00
|
|
|
if (ops->scope) {
|
2013-11-26 18:02:58 +08:00
|
|
|
phpdbg_notice("Stack in %s::%s()", ops->scope->name, ops->function_name);
|
2013-11-25 18:17:57 +08:00
|
|
|
} else {
|
2013-11-26 18:02:58 +08:00
|
|
|
phpdbg_notice("Stack in %s()", ops->function_name);
|
2013-11-25 18:17:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ops->filename) {
|
2013-11-26 18:02:58 +08:00
|
|
|
phpdbg_notice("Stack in %s", ops->filename);
|
2013-11-25 18:17:57 +08:00
|
|
|
} else {
|
2013-11-26 18:02:58 +08:00
|
|
|
phpdbg_notice("Stack @ %p", ops);
|
2013-11-25 18:17:57 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 18:02:58 +08:00
|
|
|
phpdbg_print_function_helper((zend_function*) ops TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("Not Executing!");
|
|
|
|
}
|
2013-11-18 22:24:58 +08:00
|
|
|
|
2013-11-26 18:02:58 +08:00
|
|
|
return SUCCESS;
|
2013-11-18 22:24:58 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2013-11-13 08:36:47 +08:00
|
|
|
PHPDBG_PRINT(class) /* {{{ */
|
|
|
|
{
|
2013-11-26 18:02:58 +08:00
|
|
|
zend_class_entry **ce;
|
2013-11-18 05:01:41 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
|
|
|
|
phpdbg_notice("%s %s: %s",
|
|
|
|
((*ce)->type == ZEND_USER_CLASS) ?
|
|
|
|
"User" : "Internal",
|
|
|
|
((*ce)->ce_flags & ZEND_ACC_INTERFACE) ?
|
|
|
|
"Interface" :
|
|
|
|
((*ce)->ce_flags & ZEND_ACC_ABSTRACT) ?
|
|
|
|
"Abstract Class" :
|
|
|
|
"Class",
|
|
|
|
(*ce)->name);
|
|
|
|
|
|
|
|
phpdbg_writeln("Methods (%d):", zend_hash_num_elements(&(*ce)->function_table));
|
|
|
|
if (zend_hash_num_elements(&(*ce)->function_table)) {
|
|
|
|
HashPosition position;
|
|
|
|
zend_function *method;
|
|
|
|
|
|
|
|
for (zend_hash_internal_pointer_reset_ex(&(*ce)->function_table, &position);
|
|
|
|
zend_hash_get_current_data_ex(&(*ce)->function_table, (void**) &method, &position) == SUCCESS;
|
|
|
|
zend_hash_move_forward_ex(&(*ce)->function_table, &position)) {
|
|
|
|
phpdbg_print_function_helper(method TSRMLS_CC);
|
2013-11-26 18:02:58 +08:00
|
|
|
}
|
2014-02-22 04:46:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The class %s could not be found", param->str);
|
2013-11-13 08:36:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
} /* }}} */
|
2013-11-13 10:26:37 +08:00
|
|
|
|
2013-11-13 13:36:01 +08:00
|
|
|
PHPDBG_PRINT(method) /* {{{ */
|
|
|
|
{
|
2014-02-22 04:46:56 +08:00
|
|
|
zend_class_entry **ce;
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
|
|
|
|
zend_function *fbc;
|
|
|
|
char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) {
|
|
|
|
phpdbg_notice("%s Method %s",
|
|
|
|
(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
|
|
|
|
fbc->common.function_name);
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
phpdbg_print_function_helper(fbc TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The method %s could not be found", param->method.name);
|
|
|
|
}
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
efree(lcname);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The class %s could not be found", param->method.class);
|
2013-11-26 18:02:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
2013-11-13 13:36:01 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2013-11-13 10:26:37 +08:00
|
|
|
PHPDBG_PRINT(func) /* {{{ */
|
|
|
|
{
|
2014-02-22 04:46:56 +08:00
|
|
|
HashTable *func_table = EG(function_table);
|
|
|
|
zend_function* fbc;
|
|
|
|
const char *func_name = param->str;
|
|
|
|
size_t func_name_len = param->len;
|
|
|
|
char *lcname;
|
|
|
|
/* search active scope if begins with period */
|
|
|
|
if (func_name[0] == '.') {
|
|
|
|
if (EG(scope)) {
|
|
|
|
func_name++;
|
|
|
|
func_name_len--;
|
|
|
|
|
|
|
|
func_table = &EG(scope)->function_table;
|
|
|
|
} else {
|
|
|
|
phpdbg_error("No active class");
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
} else if (!EG(function_table)) {
|
|
|
|
phpdbg_error("No function table loaded");
|
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
|
|
|
func_table = EG(function_table);
|
|
|
|
}
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
lcname = zend_str_tolower_dup(func_name, func_name_len);
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
if (zend_hash_find(func_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) {
|
|
|
|
phpdbg_notice("%s %s %s",
|
|
|
|
(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
|
|
|
|
(fbc->common.scope) ? "Method" : "Function",
|
|
|
|
fbc->common.function_name);
|
2013-11-26 18:02:58 +08:00
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
phpdbg_print_function_helper(fbc TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The function %s could not be found", func_name);
|
2013-11-26 18:02:58 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 04:46:56 +08:00
|
|
|
efree(lcname);
|
|
|
|
|
2013-11-26 18:02:58 +08:00
|
|
|
return SUCCESS;
|
2013-11-13 10:26:37 +08:00
|
|
|
} /* }}} */
|