php-src/phpdbg_bp.c

364 lines
12 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 "zend.h"
#include "zend_hash.h"
2013-11-10 20:10:54 +00:00
#include "zend_llist.h"
#include "phpdbg.h"
#include "phpdbg_bp.h"
#include "phpdbg_utils.h"
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
static void phpdbg_llist_breakfile_dtor(void *data) /* {{{ */
{
phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) data;
efree((char*)bp->filename);
} /* }}} */
static void phpdbg_class_breaks_dtor(void *data) /* {{{ */
{
phpdbg_breakmethod_t *bp = (phpdbg_breakmethod_t*) data;
2013-11-12 01:41:14 +00:00
efree((char*)bp->class_name);
efree((char*)bp->func_name);
} /* }}} */
void phpdbg_set_breakpoint_file(const char *path, long line_num TSRMLS_DC) /* {{{ */
{
phpdbg_breakfile_t new_break;
zend_llist *break_files_ptr;
size_t path_len = strlen(path);
new_break.filename = estrndup(path, path_len);
new_break.line = line_num;
2013-11-11 22:06:36 +00:00
PHPDBG_G(flags) |= PHPDBG_HAS_FILE_BP;
2013-11-12 01:51:50 +00:00
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
new_break.filename, path_len, (void**)&break_files_ptr) == FAILURE) {
zend_llist break_files;
zend_llist_init(&break_files, sizeof(phpdbg_breakfile_t),
phpdbg_llist_breakfile_dtor, 0);
2013-11-12 01:51:50 +00:00
zend_hash_update(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],
new_break.filename, path_len, &break_files, sizeof(zend_llist),
(void**)&break_files_ptr);
}
2013-11-10 19:15:32 +00:00
new_break.id = PHPDBG_G(bp_count)++;
zend_llist_add_element(break_files_ptr, &new_break);
2013-11-11 22:06:36 +00:00
phpdbg_notice("Breakpoint #%d added at %s:%ld",
new_break.id, new_break.filename, new_break.line);
} /* }}} */
2013-11-10 21:38:58 +00:00
void phpdbg_set_breakpoint_symbol(const char *name TSRMLS_DC) /* {{{ */
{
size_t name_len = strlen(name);
2013-11-12 01:51:50 +00:00
if (!zend_hash_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], name, name_len)) {
2013-11-10 21:38:58 +00:00
phpdbg_breaksymbol_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_SYM_BP;
2013-11-10 21:38:58 +00:00
new_break.symbol = estrndup(name, name_len + 1);
new_break.id = PHPDBG_G(bp_count)++;
2013-11-12 01:51:50 +00:00
zend_hash_update(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], new_break.symbol,
2013-11-10 21:38:58 +00:00
name_len, &new_break, sizeof(phpdbg_breaksymbol_t), NULL);
2013-11-11 22:06:36 +00:00
phpdbg_notice("Breakpoint #%d added at %s",
new_break.id, new_break.symbol);
} else {
phpdbg_notice("Breakpoint exists at %s", name);
}
} /* }}} */
2013-11-12 13:33:51 +00:00
void phpdbg_set_breakpoint_method(const char* class_name, const char* func_name TSRMLS_DC) /* {{{ */
{
HashTable class_breaks, *class_table;
2013-11-12 13:33:51 +00:00
size_t class_len = strlen(class_name);
size_t func_len = strlen(func_name);
2013-11-12 01:41:14 +00:00
2013-11-12 13:33:51 +00:00
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], class_name,
class_len, (void**)&class_table) != SUCCESS) {
zend_hash_init(&class_breaks, 8, NULL, phpdbg_class_breaks_dtor, 0);
zend_hash_update(
&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD],
class_name, class_len,
(void**)&class_breaks, sizeof(HashTable), (void**)&class_table);
}
if (!zend_hash_exists(class_table, func_name, func_len)) {
phpdbg_breakmethod_t new_break;
2013-11-12 01:41:14 +00:00
PHPDBG_G(flags) |= PHPDBG_HAS_METHOD_BP;
2013-11-12 01:41:14 +00:00
new_break.class_name = class_name;
new_break.class_len = class_len;
new_break.func_name = func_name;
new_break.func_len = func_len;
new_break.id = PHPDBG_G(bp_count)++;
2013-11-12 01:41:14 +00:00
2013-11-12 13:33:51 +00:00
zend_hash_update(class_table, func_name, func_len,
&new_break, sizeof(phpdbg_breakmethod_t), NULL);
phpdbg_notice("Breakpoint #%d added at %s::%s",
new_break.id, class_name, func_name);
} else {
phpdbg_notice("Breakpoint exists at %s::%s", class_name, func_name);
}
} /* }}} */
void phpdbg_set_breakpoint_opline(zend_ulong opline TSRMLS_DC) /* {{{ */
2013-11-11 11:54:41 +00:00
{
2013-11-12 01:51:50 +00:00
if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline)) {
2013-11-11 11:54:41 +00:00
phpdbg_breakline_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
2013-11-11 22:06:36 +00:00
new_break.name = NULL;
2013-11-11 13:04:21 +00:00
new_break.opline = opline;
2013-11-11 11:54:41 +00:00
new_break.id = PHPDBG_G(bp_count)++;
2013-11-11 22:06:36 +00:00
2013-11-12 01:51:50 +00:00
zend_hash_index_update(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline, &new_break, sizeof(phpdbg_breakline_t), NULL);
2013-11-11 22:06:36 +00:00
phpdbg_notice("Breakpoint #%d added at %#lx",
new_break.id, new_break.opline);
2013-11-11 11:54:41 +00:00
} else {
phpdbg_notice("Breakpoint exists at %#lx", opline);
2013-11-11 11:54:41 +00:00
}
} /* }}} */
2013-11-11 14:33:53 +00:00
void phpdbg_set_breakpoint_opline_ex(phpdbg_opline_ptr_t opline TSRMLS_DC) /* {{{ */
{
2013-11-12 01:51:50 +00:00
if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline)) {
2013-11-11 14:33:53 +00:00
phpdbg_breakline_t new_break;
PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
2013-11-11 22:06:36 +00:00
new_break.opline = (zend_ulong) opline;
2013-11-11 14:33:53 +00:00
new_break.id = PHPDBG_G(bp_count)++;
2013-11-11 22:06:36 +00:00
2013-11-12 01:51:50 +00:00
zend_hash_index_update(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline, &new_break, sizeof(phpdbg_breakline_t), NULL);
2013-11-11 22:06:36 +00:00
phpdbg_notice("Breakpoint #%d added at %#lx",
new_break.id, new_break.opline);
2013-11-11 14:33:53 +00:00
}
} /* }}} */
2013-11-10 20:06:19 +00:00
int phpdbg_find_breakpoint_file(zend_op_array *op_array TSRMLS_DC) /* {{{ */
{
size_t name_len = strlen(op_array->filename);
zend_llist *break_list;
2013-11-10 19:15:32 +00:00
zend_llist_element *le;
2013-11-12 01:51:50 +00:00
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], op_array->filename, name_len,
2013-11-10 19:15:32 +00:00
(void**)&break_list) == FAILURE) {
return FAILURE;
}
2013-11-10 19:15:32 +00:00
for (le = break_list->head; le; le = le->next) {
const phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*)le->data;
2013-11-10 19:15:32 +00:00
if (bp->line == (*EG(opline_ptr))->lineno) {
phpdbg_notice("Breakpoint #%d at %s:%ld",
bp->id, bp->filename, bp->line);
2013-11-10 19:15:32 +00:00
return SUCCESS;
}
}
return FAILURE;
} /* }}} */
2013-11-10 20:06:19 +00:00
int phpdbg_find_breakpoint_symbol(zend_function *fbc TSRMLS_DC) /* {{{ */
{
const char *fname;
2013-11-12 00:40:40 +00:00
zend_op_array *ops;
2013-11-10 21:38:58 +00:00
phpdbg_breaksymbol_t *bp;
if (fbc->type != ZEND_USER_FUNCTION) {
return FAILURE;
}
2013-11-12 01:41:14 +00:00
2013-11-12 00:40:40 +00:00
ops = (zend_op_array*)fbc;
2013-11-12 01:41:14 +00:00
2013-11-12 00:40:40 +00:00
if (ops->scope) {
2013-11-12 00:47:27 +00:00
/* find method breaks here */
return phpdbg_find_breakpoint_method(
ops TSRMLS_CC);
2013-11-12 00:40:40 +00:00
}
2013-11-12 00:47:27 +00:00
fname = ops->function_name;
if (!fname) {
fname = "main";
}
2013-11-10 21:38:58 +00:00
2013-11-12 01:51:50 +00:00
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], fname, strlen(fname),
2013-11-10 21:38:58 +00:00
(void**)&bp) == SUCCESS) {
phpdbg_notice("Breakpoint #%d in %s() at %s:%u",
2013-11-12 06:03:37 +00:00
bp->id, bp->symbol,
2013-11-11 22:06:36 +00:00
zend_get_executed_filename(TSRMLS_C),
zend_get_executed_lineno(TSRMLS_C));
return SUCCESS;
}
return FAILURE;
2013-11-10 21:57:43 +00:00
} /* }}} */
2013-11-11 11:54:41 +00:00
2013-11-12 00:47:27 +00:00
int phpdbg_find_breakpoint_method(zend_op_array *ops TSRMLS_DC) /* {{{ */
{
HashTable *class_table;
phpdbg_breakmethod_t *bp;
if (zend_hash_find(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], ops->scope->name, ops->scope->name_length,
(void**)&class_table) == SUCCESS) {
if (zend_hash_find(
class_table,
2013-11-12 01:41:14 +00:00
ops->function_name,
strlen(ops->function_name), (void**)&bp) == SUCCESS) {
2013-11-12 01:41:14 +00:00
phpdbg_notice("Breakpoint #%d in %s::%s() at %s:%u",
2013-11-12 06:03:37 +00:00
bp->id, bp->class_name, bp->func_name,
zend_get_executed_filename(TSRMLS_C),
zend_get_executed_lineno(TSRMLS_C));
return SUCCESS;
}
}
return FAILURE;
} /* }}} */
2013-11-11 13:07:02 +00:00
int phpdbg_find_breakpoint_opline(phpdbg_opline_ptr_t opline TSRMLS_DC) /* {{{ */
2013-11-11 11:54:41 +00:00
{
phpdbg_breakline_t *bp;
2013-11-12 01:51:50 +00:00
if (zend_hash_index_find(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline,
2013-11-11 13:04:21 +00:00
(void**)&bp) == SUCCESS) {
phpdbg_notice("Breakpoint #%d in %#lx at %s:%u",
bp->id, bp->opline,
2013-11-11 22:06:36 +00:00
zend_get_executed_filename(TSRMLS_C),
zend_get_executed_lineno(TSRMLS_C));
2013-11-11 22:06:36 +00:00
2013-11-11 11:54:41 +00:00
return SUCCESS;
}
2013-11-11 22:06:36 +00:00
2013-11-11 11:54:41 +00:00
return FAILURE;
} /* }}} */
2013-11-11 14:33:53 +00:00
2013-11-11 17:19:05 +00:00
void phpdbg_clear_breakpoints(TSRMLS_D) /* {{{ */
{
2013-11-12 01:51:50 +00:00
zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]);
zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]);
zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]);
2013-11-12 01:41:14 +00:00
PHPDBG_G(flags) &= ~PHPDBG_BP_MASK;
2013-11-11 17:19:05 +00:00
PHPDBG_G(bp_count) = 0;
} /* }}} */
2013-11-12 16:04:41 +00:00
void phpdbg_print_breakpoints(zend_ulong type TSRMLS_DC) /* {{{ */
{
switch (type) {
case PHPDBG_BREAK_SYM: if ((PHPDBG_G(flags) & PHPDBG_HAS_SYM_BP)) {
HashPosition position;
phpdbg_breaksymbol_t *brake;
2013-11-12 23:54:41 +00:00
phpdbg_writeln(SEPARATE);
2013-11-12 23:43:49 +00:00
phpdbg_writeln("Function Breakpoints:");
2013-11-12 16:04:41 +00:00
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], &position);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], (void**) &brake, &position) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], &position)) {
2013-11-12 23:43:49 +00:00
phpdbg_writeln(
"#%d\t\t%s", brake->id, brake->symbol);
2013-11-12 16:04:41 +00:00
}
} break;
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
case PHPDBG_BREAK_METHOD: if ((PHPDBG_G(flags) & PHPDBG_HAS_METHOD_BP)) {
HashPosition position[2];
HashTable *class_table;
char *class_name = NULL;
zend_uint class_len = 0;
zend_ulong class_idx = 0L;
2013-11-12 18:53:48 +00:00
2013-11-12 23:54:41 +00:00
phpdbg_writeln(SEPARATE);
2013-11-12 23:43:49 +00:00
phpdbg_writeln("Method Breakpoints:");
2013-11-12 16:04:41 +00:00
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], &position[0]);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], (void**) &class_table, &position[0]) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], &position[0])) {
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
if (zend_hash_get_current_key_ex(
2013-11-12 18:53:48 +00:00
&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD],
2013-11-12 16:04:41 +00:00
&class_name, &class_len, &class_idx, 0, &position[0]) == HASH_KEY_IS_STRING) {
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
phpdbg_breakmethod_t *brake;
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
for (zend_hash_internal_pointer_reset_ex(class_table, &position[1]);
zend_hash_get_current_data_ex(class_table, (void**)&brake, &position[1]) == SUCCESS;
zend_hash_move_forward_ex(class_table, &position[1])) {
2013-11-12 23:43:49 +00:00
phpdbg_writeln(
"#%d\t\t%s::%s", brake->id, brake->class_name, brake->func_name);
2013-11-12 16:04:41 +00:00
}
}
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
}
} break;
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
case PHPDBG_BREAK_FILE: if ((PHPDBG_G(flags) & PHPDBG_HAS_FILE_BP)) {
HashPosition position;
zend_llist *points;
2013-11-12 23:54:41 +00:00
phpdbg_writeln(SEPARATE);
2013-11-12 23:43:49 +00:00
phpdbg_writeln("File Breakpoints:");
2013-11-12 16:04:41 +00:00
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], &position);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], (void**) &points, &position) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], &position)) {
zend_llist_position lposition;
phpdbg_breakfile_t *brake;
if ((brake = zend_llist_get_first_ex(points, &lposition))) {
do {
2013-11-12 23:43:49 +00:00
phpdbg_writeln("#%d\t\t%s:%lu", brake->id, brake->filename, brake->line);
2013-11-12 16:04:41 +00:00
} while ((brake = zend_llist_get_next_ex(points, &lposition)));
}
}
} break;
2013-11-12 18:53:48 +00:00
2013-11-12 16:04:41 +00:00
case PHPDBG_BREAK_OPLINE: if ((PHPDBG_G(flags) & PHPDBG_HAS_OPLINE_BP)) {
HashPosition position;
phpdbg_breakline_t *brake;
2013-11-12 23:54:41 +00:00
phpdbg_writeln(SEPARATE);
2013-11-12 23:43:49 +00:00
phpdbg_writeln("Opline Breakpoints:");
2013-11-12 16:04:41 +00:00
for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], &position);
zend_hash_get_current_data_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (void**) &brake, &position) == SUCCESS;
zend_hash_move_forward_ex(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], &position)) {
2013-11-12 23:43:49 +00:00
phpdbg_writeln("#%d\t\t%#lx", brake->id, brake->opline);
2013-11-12 16:04:41 +00:00
}
} break;
}
} /* }}} */