Skip to content

Commit

Permalink
Pre-empt function calls by checking for the condition first
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Oct 15, 2023
1 parent 7186325 commit 55a6191
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
58 changes: 19 additions & 39 deletions src/debugger/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,62 +205,34 @@ int xdebug_do_eval(char *eval_string, zval *ret_zval, zend_string **return_messa
return res;
}

int next_condition_met(void)
int next_condition_met(function_stack_entry *fse)
{
function_stack_entry *fse;
int level = 0;

if (!XG_DBG(context).do_next) {
return 0;
}

/* Get latest stack level and function number */
if (XG_BASE(stack) && XDEBUG_VECTOR_TAIL(XG_BASE(stack))) {
fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));
level = fse->level;
}

if (XG_DBG(context).next_stack != NULL) {
if ((XG_DBG(context).next_stack == XG_BASE(stack)) && (XG_DBG(context).next_level >= level)) {
if ((XG_DBG(context).next_stack == XG_BASE(stack)) && (XG_DBG(context).next_level >= fse->level)) {
return 1;
}
} else {
if (XG_DBG(context).next_level >= level) {
if (XG_DBG(context).next_level >= fse->level) {
return 1;
}
}

return 0;
}

int finish_condition_met(int break_at_return_scope)
int finish_condition_met(function_stack_entry *fse, int break_at_return_scope)
{
function_stack_entry *fse;
int level = 0;
int func_nr = 0;

if (!XG_DBG(context).do_finish) {
return 0;
}

/* Get latest stack level and function number */
if (XG_BASE(stack) && XDEBUG_VECTOR_TAIL(XG_BASE(stack))) {
fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));
level = fse->level;
func_nr = fse->function_nr;
}

if (!break_at_return_scope && level < XG_DBG(context).finish_level) {
if (!break_at_return_scope && fse->level < XG_DBG(context).finish_level) {
return 1;
}

if (break_at_return_scope && level <= XG_DBG(context).finish_level) {
if (break_at_return_scope && fse->level <= XG_DBG(context).finish_level) {
return 1;
}

if (
(level == XG_DBG(context).finish_level) &&
(func_nr > XG_DBG(context).finish_func_nr)
(fse->level == XG_DBG(context).finish_level) &&
(fse->function_nr > XG_DBG(context).finish_func_nr)
) {
return 1;
}
Expand All @@ -272,6 +244,7 @@ void xdebug_debugger_statement_call(zend_string *filename, int lineno)
{
xdebug_llist_element *le;
xdebug_brk_info *extra_brk_info;
function_stack_entry *fse;

if (XG_DBG(context).do_connect_to_client) {
XG_DBG(context).do_connect_to_client = 0;
Expand All @@ -285,6 +258,13 @@ void xdebug_debugger_statement_call(zend_string *filename, int lineno)
return;
}

if (!XG_BASE(stack) || !XDEBUG_VECTOR_TAIL(XG_BASE(stack))) {
return;
}

/* Fetch top level fse */
fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));

XG_DBG(suppress_return_value_step) = 0;

if (XG_DBG(context).do_break) {
Expand All @@ -301,7 +281,7 @@ void xdebug_debugger_statement_call(zend_string *filename, int lineno)
}

/* Check for "finish" */
if (finish_condition_met(0)) {
if (XG_DBG(context).do_finish && finish_condition_met(fse, 0)) {
XG_DBG(context).do_finish = 0;

if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), filename, lineno, XDEBUG_STEP, NULL, 0, NULL, NULL, NULL)) {
Expand All @@ -312,7 +292,7 @@ void xdebug_debugger_statement_call(zend_string *filename, int lineno)
}

/* Check for "next" */
if (next_condition_met()) {
if (XG_DBG(context).do_next && next_condition_met(fse)) {
XG_DBG(context).do_next = 0;

if (!XG_DBG(context).handler->remote_breakpoint(&(XG_DBG(context)), XG_BASE(stack), filename, lineno, XDEBUG_STEP, NULL, 0, NULL, NULL, NULL)) {
Expand Down Expand Up @@ -526,7 +506,7 @@ static int handle_breakpoints(function_stack_entry *fse, int breakpoint_type, zv
) {
if (XG_DBG(context).do_step) {
XG_DBG(context).do_step = 0;
} else if (finish_condition_met(1)) {
} else if (XG_DBG(context).do_finish && finish_condition_met(fse, 1)) {
XG_DBG(context).do_finish = 0;
} else {
return 1;
Expand Down
9 changes: 7 additions & 2 deletions xdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,13 @@ ZEND_DLEXPORT void xdebug_statement_call(zend_execute_data *frame)

lineno = EG(current_execute_data)->opline->lineno;

xdebug_coverage_count_line_if_active(op_array, op_array->filename, lineno);
xdebug_debugger_statement_call(op_array->filename, lineno);
if (XDEBUG_MODE_IS(XDEBUG_MODE_COVERAGE)) {
xdebug_coverage_count_line_if_active(op_array, op_array->filename, lineno);
}

if (XDEBUG_MODE_IS(XDEBUG_MODE_STEP_DEBUG)) {
xdebug_debugger_statement_call(op_array->filename, lineno);
}
}

ZEND_DLEXPORT int xdebug_zend_startup(zend_extension *extension)
Expand Down

0 comments on commit 55a6191

Please sign in to comment.