Skip to content

Commit

Permalink
Add setting, and implement 'pause' command
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Nov 28, 2023
1 parent f6472a0 commit efe0bd0
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 15 deletions.
12 changes: 9 additions & 3 deletions src/base/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ static void xdebug_execute_user_code_begin(zend_execute_data *execute_data)
}

#ifdef __linux__
xdebug_ctrl_socket_dispatch();
xdebug_control_socket_dispatch();
#endif

if (XDEBUG_MODE_IS(XDEBUG_MODE_DEVELOP)) {
Expand Down Expand Up @@ -1344,6 +1344,10 @@ void xdebug_base_minit(INIT_FUNC_ARGS)
XG_BASE(private_tmp) = NULL;
#ifdef __linux__
read_systemd_private_tmp_directory(&XG_BASE(private_tmp));

XG_BASE(control_socket_path) = NULL;
XG_BASE(control_socket_fd) = 0;
XG_BASE(control_socket_last_trigger) = 0;
#endif
}

Expand Down Expand Up @@ -1401,7 +1405,9 @@ void xdebug_base_rinit()

#ifdef __linux__
/* Set-up Control Socket */
xdebug_ctrl_socket_setup();
if (XINI_BASE(control_socket_granularity) != XDEBUG_CONTROL_SOCKET_OFF) {
xdebug_control_socket_setup();
}
#endif

/* Signal that we're in a request now */
Expand Down Expand Up @@ -1454,7 +1460,7 @@ void xdebug_base_post_deactivate()

#ifdef __linux__
/* Close Down Control Socket */
xdebug_ctrl_socket_teardown();
xdebug_control_socket_teardown();
#endif
}

Expand Down
9 changes: 7 additions & 2 deletions src/base/base_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ typedef struct _xdebug_base_globals_t {

#ifdef __linux__
/* Control Socket */
char *control_socket_path;
int control_socket_fd;
char *control_socket_path;
int control_socket_fd;
zend_long control_socket_last_trigger;
#endif

/* filters */
Expand All @@ -88,6 +89,10 @@ typedef struct _xdebug_base_globals_t {
} xdebug_base_globals_t;

typedef struct _xdebug_base_settings_t {
#ifdef __linux__
int control_socket_granularity;
zend_long control_socket_threshold_ms;
#endif
zend_long max_nesting_level;
} xdebug_base_settings_t;

Expand Down
82 changes: 78 additions & 4 deletions src/base/ctrl_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
+----------------------------------------------------------------------+
*/

#ifdef __linux__

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down Expand Up @@ -45,6 +47,8 @@ static xdebug_error_entry xdebug_error_codes[24] = {
{ 3, "invalid or missing options" },
{ 4, "unimplemented command" },
{ 5, "command is not available" },
{ 400, "step debugger is not enabled" },
{ 401, "step debugger did not activate" },
{ -1, NULL }
};

Expand Down Expand Up @@ -80,9 +84,11 @@ typedef struct xdebug_ctrl_cmd {

/* Command definition list */
CTRL_FUNC(ps);
CTRL_FUNC(pause);

static xdebug_ctrl_cmd ctrl_commands[] = {
CTRL_FUNC_ENTRY(ps)
CTRL_FUNC_ENTRY(pause)
{ NULL, NULL }
};

Expand Down Expand Up @@ -151,10 +157,11 @@ CTRL_FUNC(ps)
{
xdebug_xml_node *response, *engine, *file, *pid, *time, *memory;
char *pid_str, *time_str, *memory_str;
function_stack_entry *fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));
function_stack_entry *fse = XDEBUG_VECTOR_HEAD(XG_BASE(stack));
double time_elapsed = XDEBUG_SECONDS_SINCE_START(xdebug_get_nanotime());

response = xdebug_xml_node_init("ps");
xdebug_xml_add_attribute(response, "success", "1");

engine = xdebug_xml_node_init("engine");
xdebug_xml_add_attribute(engine, "version", XDEBUG_VERSION);
Expand Down Expand Up @@ -183,7 +190,47 @@ CTRL_FUNC(ps)
xdebug_xml_add_child(*retval, response);
}

void xdebug_ctrl_socket_dispatch(void)
CTRL_FUNC(pause)
{
xdebug_xml_node *response, *pid, *action;
char *pid_str;

response = xdebug_xml_node_init("pause");
xdebug_xml_add_attribute(response, "success", "1");

pid = xdebug_xml_node_init("pid");
pid_str = xdebug_sprintf("%lu", xdebug_get_pid());
xdebug_xml_add_text(pid, pid_str);
xdebug_xml_add_child(response, pid);

if (!XDEBUG_MODE_IS(XDEBUG_MODE_STEP_DEBUG)) {
xdebug_xml_node *error;

error = xdebug_xml_node_init("error");
xdebug_xml_add_attribute_ex(error, "code", xdebug_sprintf("%lu", XDEBUG_ERROR_STEP_DEBUG_MODE_NOT_ENABLED), 0, 1);
ADD_REASON_MESSAGE(XDEBUG_ERROR_STEP_DEBUG_MODE_NOT_ENABLED);
xdebug_xml_add_child(*retval, error);

xdebug_xml_add_child(*retval, response);
return;
}

if (!xdebug_is_debug_connection_active()) {
action = xdebug_xml_node_init("action");
xdebug_xml_add_text(action, xdstrdup("IDE Connection Signalled"));

XG_DBG(context).do_connect_to_client = 1;
} else {
action = xdebug_xml_node_init("action");
xdebug_xml_add_text(action, xdstrdup("Breakpoint Signalled"));

XG_DBG(context).do_break = 1;
}
xdebug_xml_add_child(response, action);
xdebug_xml_add_child(*retval, response);
}

static void xdebug_control_socket_handle(void)
{
char buffer[256];
int bytes_read;
Expand All @@ -192,6 +239,9 @@ void xdebug_ctrl_socket_dispatch(void)
struct timeval timeout;
fd_set master_set, working_set;

/* Update last trigger */
XG_BASE(control_socket_last_trigger) = xdebug_get_nanotime();

/* Initialize the master fd_set */
FD_ZERO(&master_set);
FD_SET(XG_BASE(control_socket_fd), &master_set);
Expand Down Expand Up @@ -233,13 +283,35 @@ void xdebug_ctrl_socket_dispatch(void)
close(new_sd);
}

void xdebug_ctrl_socket_setup(void)
void xdebug_control_socket_dispatch(void)
{
if (!XG_BASE(control_socket_path)) {
return;
}

switch (XINI_BASE(control_socket_granularity)) {
case XDEBUG_CONTROL_SOCKET_OFF:
return;

case XDEBUG_CONTROL_SOCKET_TIME:
if (xdebug_get_nanotime() < (XG_BASE(control_socket_last_trigger) + (XINI_BASE(control_socket_threshold_ms) * 1000000))) {
return;
}

break;
}

xdebug_control_socket_handle();
}

void xdebug_control_socket_setup(void)
{
struct sockaddr_un *servaddr = NULL;

/* Initialise control socket globals */
XG_BASE(control_socket_fd) = -1;
XG_BASE(control_socket_path) = NULL;
XG_BASE(control_socket_last_trigger) = xdebug_get_nanotime();

/* Part 1 – create the socket */
if (0 > (XG_BASE(control_socket_fd) = socket(AF_UNIX, SOCK_STREAM, 0))) {
Expand Down Expand Up @@ -289,10 +361,12 @@ void xdebug_ctrl_socket_setup(void)
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_INFO, "CTRL-OK", "Control socket set up succesfully: '@%s'", XG_BASE(control_socket_path));
}

void xdebug_ctrl_socket_teardown(void)
void xdebug_control_socket_teardown(void)
{
if (XG_BASE(control_socket_path)) {
close(XG_BASE(control_socket_fd));
xdfree(XG_BASE(control_socket_path));
}
}

#endif
6 changes: 3 additions & 3 deletions src/base/ctrl_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#ifndef __XDEBUG_CTRL_SOCKET__
#define __XDEBUG_CTRL_SOCKET__

void xdebug_ctrl_socket_setup(void);
void xdebug_ctrl_socket_teardown(void);
void xdebug_control_socket_setup(void);
void xdebug_control_socket_teardown(void);

void xdebug_ctrl_socket_dispatch(void);
void xdebug_control_socket_dispatch(void);

#endif // __XDEBUG_CTRL_SOCKET__
2 changes: 2 additions & 0 deletions src/lib/cmd_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ int xdebug_cmd_parse(const char *line, char **cmd, xdebug_dbgp_arg **ret_args);
#define XDEBUG_ERROR_UNIMPLEMENTED 4
#define XDEBUG_ERROR_COMMAND_UNAVAILABLE 5

#define XDEBUG_ERROR_STEP_DEBUG_MODE_NOT_ENABLED 400
#define XDEBUG_ERROR_STEP_DEBUG_NOT_STARTED 401

#endif
15 changes: 15 additions & 0 deletions src/lib/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ int xdebug_lib_set_mode(const char *mode)
return result;
}

#if __linux__
int xdebug_lib_set_control_socket_granularity(char *value)
{
if (strcmp(value, "no") == 0 || value[0] == '\0') {
XINI_BASE(control_socket_granularity) = XDEBUG_CONTROL_SOCKET_OFF;
return 1;
}

XINI_BASE(control_socket_granularity) = XDEBUG_CONTROL_SOCKET_TIME;
XINI_BASE(control_socket_threshold_ms) = 25;

return 0;
}
#endif

int xdebug_lib_get_start_with_request(void)
{
return XG_LIB(start_with_request);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ int xdebug_lib_set_start_upon_error(char *value);
int xdebug_lib_start_upon_error(void);
int xdebug_lib_get_start_upon_error(void);

#if __linux__
# define XDEBUG_CONTROL_SOCKET_OFF 1
# define XDEBUG_CONTROL_SOCKET_TIME 4
int xdebug_lib_set_control_socket_granularity(char *value);
#endif

const char *xdebug_lib_mode_from_value(int mode);

void xdebug_lib_set_active_data(zend_execute_data *execute_data);
Expand Down
2 changes: 1 addition & 1 deletion tests/debugger/bug01978.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $commands = array(
$xdebugLogFileName = sys_get_temp_dir() . '/' . getenv('UNIQ_RUN_ID') . getenv('TEST_PHP_WORKER') . 'remote-log-1978.txt';
@unlink( $xdebugLogFileName );

dbgpRunFile( $filename, $commands, [ 'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 7 ] );
dbgpRunFile( $filename, $commands, [ 'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 7, 'xdebug.control_socket' => 'off' ] );

echo file_get_contents( $xdebugLogFileName );
@unlink( $xdebugLogFileName );
Expand Down
1 change: 1 addition & 0 deletions tests/debugger/remote_log-unix-2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ xdebug.discover_client_host=1
xdebug.client_host=unix:///tmp/xdbg.sock
xdebug.client_port=0
xdebug.client_discovery_header=I_LIKE_COOKIES
xdebug.control_socket=off
--FILE--
<?php
echo strlen("foo"), "\n";
Expand Down
1 change: 1 addition & 0 deletions tests/debugger/remote_log-unix.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ xdebug.start_with_request=yes
xdebug.log={TMP}/{RUNID}{TEST_PHP_WORKER}remote-log4.txt
xdebug.client_host=unix:///tmp/xdbg.sock
xdebug.client_port=0
xdebug.control_socket=off
--FILE--
<?php
echo strlen("foo"), "\n";
Expand Down
1 change: 1 addition & 0 deletions tests/debugger/remote_log1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ xdebug.log_level=20
xdebug.discover_client_host=0
xdebug.client_host=doesnotexist
xdebug.client_port=9002
xdebug.control_socket=off
--FILE--
<?php
echo strlen("foo"), "\n";
Expand Down
1 change: 1 addition & 0 deletions tests/debugger/remote_log2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ xdebug.log={TMP}/{RUNID}{TEST_PHP_WORKER}remote-log2.txt
xdebug.discover_client_host=1
xdebug.client_host=doesnotexist2
xdebug.client_port=9003
xdebug.control_socket=off
--FILE--
<?php
echo strlen("foo"), "\n";
Expand Down
1 change: 1 addition & 0 deletions tests/debugger/remote_log3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ xdebug.discover_client_host=1
xdebug.client_host=doesnotexist2
xdebug.client_port=9003
xdebug.client_discovery_header=I_LIKE_COOKIES,HTTP_X_FORWARDED_FOR,REMOTE_ADDR
xdebug.control_socket=off
--FILE--
<?php
echo strlen("foo"), "\n";
Expand Down
1 change: 1 addition & 0 deletions tests/debugger/remote_log4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ xdebug.discover_client_host=1
xdebug.client_host=doesnotexist2
xdebug.client_port=9003
xdebug.client_discovery_header=I_LIKE_COOKIES
xdebug.control_socket=off
--FILE--
<?php
echo strlen("foo"), "\n";
Expand Down
3 changes: 2 additions & 1 deletion tests/debugger/start_with_request_trigger_match-009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ dbgpRunFile(
[
'xdebug.mode' => 'debug', 'xdebug.start_with_request' => 'trigger',
'xdebug.trigger_value' => 'value1,value2', 'variables_order' => 'PGCS',
'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 10
'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 10,
'xdebug.control_socket' => 'off',
],
['timeout' => 1]
);
Expand Down
3 changes: 2 additions & 1 deletion tests/debugger/start_with_request_trigger_match-010.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ dbgpRunFile(
[
'xdebug.mode' => 'debug', 'xdebug.start_with_request' => 'trigger',
'xdebug.trigger_value' => 'value1,value2', 'variables_order' => 'PGCS',
'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 10
'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 10,
'xdebug.control_socket' => 'off',
],
['timeout' => 1]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dbgpRunFile(
'xdebug.start_with_request' => 'trigger', 'xdebug.trigger_value' => 'not-foobar',
'variables_order' => 'PGCS',
'xdebug.log' => $xdebugLogFileName, 'xdebug.log_level' => 10,
'xdebug.control_socket' => 'off',
],
['timeout' => 1]
);
Expand Down
1 change: 1 addition & 0 deletions tests/filter/bug01919-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ check_reqs('!win');
--INI--
xdebug.mode=develop
xdebug.log={TMP}/{RUNID}{TEST_PHP_WORKER}issue1919-001.txt
xdebug.control_socket=off
--FILE--
<?php
xdebug_set_filter(XDEBUG_FILTER_CODE_COVERAGE, XDEBUG_PATH_INCLUDE, []);
Expand Down
1 change: 1 addition & 0 deletions tests/filter/bug01919-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ check_reqs('!win');
--INI--
xdebug.mode=develop
xdebug.log={TMP}/{RUNID}{TEST_PHP_WORKER}issue1919-002.txt
xdebug.control_socket=off
--FILE--
<?php
xdebug_set_filter(XDEBUG_FILTER_TRACING, XDEBUG_PATH_INCLUDE, []);
Expand Down
1 change: 1 addition & 0 deletions tests/profiler/bug02037.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ check_reqs('!win');
xdebug.mode=profile
xdebug.log={TMP}/{RUNID}{TEST_PHP_WORKER}issue2037.txt
xdebug.output_dir=/tmp/un-writable
xdebug.control_socket=off
--FILE--
<?php
echo "==DONE==\n";
Expand Down
1 change: 1 addition & 0 deletions tests/profiler/bug02069-zlib-compression-append.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ xdebug.use_compression=1
xdebug.profiler_append=1
xdebug.profiler_output_name=cachegrind.out.%R.end
xdebug.log={TMP}/{RUNID}{TEST_PHP_WORKER}issue2069-001.txt
xdebug.control_socket=off
--FILE--
<?php
require_once 'capture-profile.inc';
Expand Down
Loading

0 comments on commit efe0bd0

Please sign in to comment.