-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathctrl_socket.c
More file actions
560 lines (470 loc) · 15.5 KB
/
ctrl_socket.c
File metadata and controls
560 lines (470 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2025 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| derick@xdebug.org so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifdef __linux__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#endif
#include "php_xdebug.h"
#if HAVE_XDEBUG_CONTROL_SOCKET_SUPPORT
#include "ctrl_socket.h"
#include "lib/cmd_parser.h"
#include "lib/log.h"
#include "lib/xml.h"
#include "base/base.h"
#if WIN32
#include <windows.h>
#endif
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
typedef struct {
int code;
const char *message;
} xdebug_error_entry;
static xdebug_error_entry xdebug_error_codes[24] = {
{ 0, "no error" },
{ 1, "parse error in command" },
{ 2, "duplicate arguments in command" },
{ 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 }
};
static const char *error_message_from_code(int code)
{
xdebug_error_entry *error_entry = &xdebug_error_codes[0];
while (error_entry->message) {
if (code == error_entry->code) {
return error_entry->message;
}
error_entry++;
}
return NULL;
}
#define ADD_REASON_MESSAGE(c) { \
xdebug_xml_node *message = xdebug_xml_node_init("message"); \
xdebug_xml_add_text(message, xdstrdup(error_message_from_code(c))); \
xdebug_xml_add_child(error, message); \
}
#define CTRL_FUNC_PARAMETERS xdebug_xml_node **retval, xdebug_dbgp_arg *args
#define CTRL_FUNC(name) static void xdebug_ctrl_handle_##name(CTRL_FUNC_PARAMETERS)
#define CTRL_FUNC_ENTRY(name) { #name, xdebug_ctrl_handle_##name },
typedef struct xdebug_ctrl_cmd {
const char *name;
void (*handler)(CTRL_FUNC_PARAMETERS);
int flags;
} 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 }
};
/* Command handler selection */
static xdebug_ctrl_cmd* lookup_cmd(char *cmd)
{
xdebug_ctrl_cmd *ptr = ctrl_commands;
while (ptr->name) {
if (strcmp(ptr->name, cmd) == 0) {
return ptr;
}
ptr++;
}
return NULL;
}
static xdebug_str *make_message(xdebug_xml_node *message)
{
xdebug_str xml_message = XDEBUG_STR_INITIALIZER;
xdebug_str *ret = xdebug_str_new();
xdebug_xml_return_node(message, &xml_message);
xdebug_str_add_literal(ret, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
xdebug_str_add(ret, xml_message.d, 0);
xdebug_str_addc(ret, '\0');
xdebug_str_destroy(&xml_message);
return ret;
}
#if __linux__
static void handle_command(int fd, const char *line)
#elif WIN32
static void handle_command(HANDLE h, const char *line)
#endif
{
char *cmd = NULL;
xdebug_dbgp_arg *args;
int res = 0;
xdebug_ctrl_cmd *command;
xdebug_str *message;
xdebug_xml_node *retval;
res = xdebug_cmd_parse(line, (char**) &cmd, (xdebug_dbgp_arg**) &args);
retval = xdebug_xml_node_init("ctrl-response");
xdebug_xml_add_attribute(retval, "xmlns:xdebug-ctrl", "https://xdebug.org/ctrl/xdebug");
command = lookup_cmd(cmd);
if (command) {
command->handler(&retval, args);
} else {
xdebug_xml_node *error;
error = xdebug_xml_node_init("error");
xdebug_xml_add_attribute_ex(error, "code", xdebug_sprintf("%lu", XDEBUG_ERROR_COMMAND_UNAVAILABLE), 0, 1);
ADD_REASON_MESSAGE(XDEBUG_ERROR_COMMAND_UNAVAILABLE);
xdebug_xml_add_child(retval, error);
}
message = make_message(retval);
#if __linux__
write(fd, message->d, message->l);
#elif WIN32
if (WriteFile(h, message->d, message->l, NULL, &XG_BASE(control_socket_ov))) {
SetEvent(XG_BASE(control_socket_ov).hEvent);
}
#endif
xdfree(cmd);
xdebug_cmd_arg_dtor(args);
}
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_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);
xdebug_xml_add_text(engine, xdstrdup(XDEBUG_NAME));
xdebug_xml_add_child(response, engine);
file = xdebug_xml_node_init("fileuri");
xdebug_xml_add_text(file, ZSTR_VAL(fse->filename));
xdebug_xml_add_child(response, file);
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);
time = xdebug_xml_node_init("time");
time_str = xdebug_sprintf("%f", time_elapsed);
xdebug_xml_add_text(time, time_str);
xdebug_xml_add_child(response, time);
memory = xdebug_xml_node_init("memory");
memory_str = xdebug_sprintf("%ld", zend_memory_usage(0) / 1024);
xdebug_xml_add_text(memory, memory_str);
xdebug_xml_add_child(response, memory);
xdebug_xml_add_child(*retval, response);
}
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_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;
}
if (!XG_BASE(observer_active)) {
xdebug_rebuild_stack();
}
XG_BASE(statement_handler_enabled) = true;
XG_BASE(observer_active) = true;
xdebug_xml_add_child(response, action);
xdebug_xml_add_child(*retval, response);
}
#if __linux__
static void xdebug_control_socket_handle(void)
{
char buffer[256];
int bytes_read;
int rc;
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);
/* non blocking */
timeout.tv_sec = 0;
timeout.tv_usec = 0;
memcpy(&working_set, &master_set, sizeof(master_set));
rc = select(XG_BASE(control_socket_fd) + 1, &working_set, NULL, NULL, &timeout);
if (rc < 0) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Select failed: %s", strerror(errno));
return;
}
if (rc == 0) {
return;
}
if (FD_ISSET(XG_BASE(control_socket_fd), &working_set)) {
int new_sd = accept(XG_BASE(control_socket_fd), NULL, NULL);
if (new_sd < 0) {
if (errno != EWOULDBLOCK) {
fprintf(stdout, " accept() failed: %d: %s", errno, strerror(errno));
}
return;
}
memset(buffer, 0, sizeof(buffer));
bytes_read = read(new_sd, buffer, sizeof(buffer));
if (bytes_read == -1) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't receive from socket: %s", strerror(errno));
} else {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_INFO, "CTRL-HANDLE", "Received: '%s'", buffer);
handle_command(new_sd, buffer);
}
close(new_sd);
}
}
#elif WIN32
static bool named_pipe_listen()
{
if (ConnectNamedPipe(XG_BASE(control_socket_h), &XG_BASE(control_socket_ov))) {
errno = GetLastError();
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't create control Named Pipe Connect 1 (0x%x)", errno);
return false;
}
switch (GetLastError()) {
case ERROR_IO_PENDING:
break;
case ERROR_PIPE_CONNECTED:
if (!SetEvent(XG_BASE(control_socket_ov).hEvent)) {
errno = GetLastError();
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't create control Named Pipe Connect SetEvent (0x%x)", errno);
return false;
}
default:
errno = GetLastError();
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't create control Named Pipe Connect 2 (0x%x)", errno);
return false;
}
return true;
}
static void xdebug_control_socket_handle(void)
{
DWORD result;
char buffer[256];
int bytes_read;
if (XG_BASE(control_socket_h) <= 0) {
/* No Named Pipe */
return;
}
if (WaitForSingleObject(XG_BASE(control_socket_ov).hEvent, 0) != WAIT_OBJECT_0) {
/* No connection yet */
return;
}
if (!GetOverlappedResult(XG_BASE(control_socket_h), &XG_BASE(control_socket_ov), &bytes_read, TRUE)) {
/* Error getting Overlapped result */
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't receive from Named Pipe GetOverlappedResult 1 (0x%x)", GetLastError());
return;
}
memset(buffer, 0, sizeof(buffer));
bytes_read = 0;
if (!ReadFile(
XG_BASE(control_socket_h),
buffer,
sizeof(buffer),
&bytes_read,
&XG_BASE(control_socket_ov)
)) {
errno = GetLastError();
if (errno == ERROR_IO_PENDING) {
WaitForSingleObject(XG_BASE(control_socket_ov).hEvent, INFINITY);
/* Error? */
if (!GetOverlappedResult(XG_BASE(control_socket_h), &XG_BASE(control_socket_ov), &bytes_read, TRUE)) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't receive from Named Pipe GetOverlappedResult 2 (0x%x)", GetLastError());
goto finish;
}
} else {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-HANDLE", "Can't receive from Named Pipe (0x%x)", GetLastError());
goto finish;
}
}
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_INFO, "CTRL-HANDLE", "Received: '%s'", buffer);
handle_command(XG_BASE(control_socket_h), buffer);
WaitForSingleObject(XG_BASE(control_socket_ov).hEvent, INFINITY);
GetOverlappedResult(XG_BASE(control_socket_h), &XG_BASE(control_socket_ov), &bytes_read, FALSE);
FlushFileBuffers(XG_BASE(control_socket_h));
finish:
DisconnectNamedPipe(XG_BASE(control_socket_h));
if (!named_pipe_listen()) {
xdebug_control_socket_teardown();
}
}
#endif
#if __linux__
static bool is_control_socket_active(void)
{
if (!XG_BASE(control_socket_path)) {
return false;
}
return true;
}
#elif WIN32
static bool is_control_socket_active(void)
{
if (XG_BASE(control_socket_h) <= 0) {
return false;
}
return true;
}
#endif
void xdebug_control_socket_dispatch(void)
{
if (!is_control_socket_active()) {
return;
}
switch (XINI_BASE(control_socket_granularity)) {
case XDEBUG_CONTROL_SOCKET_OFF:
return;
case XDEBUG_CONTROL_SOCKET_DEFAULT:
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();
}
#ifdef __linux__
void xdebug_control_socket_setup(void)
{
struct sockaddr_un *servaddr = NULL;
socklen_t addr_len = 0;
/* 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))) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-SOCKET", "Can't create control socket");
return;
}
XG_BASE(control_socket_path) = xdebug_sprintf("xdebug-ctrl." ZEND_ULONG_FMT, xdebug_get_pid());
/* Part 2b — Configure socket */
servaddr = (struct sockaddr_un *)xdmalloc(sizeof(struct sockaddr_un));
if (servaddr == NULL) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-ALLOC", "Can't allocate memory");
xdfree(XG_BASE(control_socket_path));
XG_BASE(control_socket_path) = NULL;
close(XG_BASE(control_socket_fd));
return;
}
servaddr->sun_family = AF_UNIX;
snprintf(servaddr->sun_path + 1, strlen(XG_BASE(control_socket_path)) + 1, "%s", XG_BASE(control_socket_path));
addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(XG_BASE(control_socket_path)) + 1;
servaddr->sun_path[0] = '\0';
if (0 != (bind(XG_BASE(control_socket_fd), (struct sockaddr *)servaddr, addr_len))) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-BIND", "Can't bind control socket");
xdfree(servaddr);
xdfree(XG_BASE(control_socket_path));
XG_BASE(control_socket_path) = NULL;
close(XG_BASE(control_socket_fd));
return;
}
/* Part 3 — Listen */
if (listen(XG_BASE(control_socket_fd), 32) < 0) {
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-LISTEN", "Listen failed: %s", strerror(errno));
xdfree(servaddr);
xdfree(XG_BASE(control_socket_path));
XG_BASE(control_socket_path) = NULL;
close(XG_BASE(control_socket_fd));
return;
}
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_INFO, "CTRL-OK", "Control socket set up successfully: '@%s'", XG_BASE(control_socket_path));
xdfree(servaddr);
}
void xdebug_control_socket_teardown(void)
{
if (XG_BASE(control_socket_path)) {
close(XG_BASE(control_socket_fd));
xdfree(XG_BASE(control_socket_path));
XG_BASE(control_socket_path) = NULL;
}
}
#elif WIN32
void xdebug_control_socket_setup(void)
{
char *name = NULL;
XG_BASE(control_socket_last_trigger) = xdebug_get_nanotime();
XG_BASE(control_socket_path) = xdebug_sprintf("xdebug-ctrl." ZEND_ULONG_FMT, xdebug_get_pid());
name = xdebug_sprintf("\\\\.\\pipe\\%s", XG_BASE(control_socket_path));
XG_BASE(control_socket_h) = CreateNamedPipeA(
name,
PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
1,
1024,
1024,
0,
NULL
);
if (XG_BASE(control_socket_h) == INVALID_HANDLE_VALUE) {
XG_BASE(control_socket_h) = 0;
errno = GetLastError();
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-SOCKET", "Can't create control Named Pipe (0x%x)", errno);
xdebug_control_socket_teardown();
xdfree(name);
return;
}
XG_BASE(control_socket_ov).Offset = 0;
XG_BASE(control_socket_ov).OffsetHigh = 0;
XG_BASE(control_socket_ov).hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (XG_BASE(control_socket_ov).hEvent == NULL) {
errno = GetLastError();
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_WARN, "CTRL-SOCKET", "Can't create control Named Pipe Event (0x%x)", errno);
xdebug_control_socket_teardown();
xdfree(name);
return;
}
if (!named_pipe_listen()) {
xdebug_control_socket_teardown();
xdfree(name);
return;
}
xdebug_log_ex(XLOG_CHAN_CONFIG, XLOG_INFO, "CTRL-OK", "Control socket set up successfully: '%s'", name);
xdfree(name);
}
void xdebug_control_socket_teardown(void)
{
if (XG_BASE(control_socket_path)) {
xdfree(XG_BASE(control_socket_path));
XG_BASE(control_socket_path) = NULL;
}
if (XG_BASE(control_socket_h)) {
DisconnectNamedPipe(XG_BASE(control_socket_h));
CloseHandle(XG_BASE(control_socket_h));
XG_BASE(control_socket_h) = 0;
}
if (XG_BASE(control_socket_ov).hEvent) {
CloseHandle(XG_BASE(control_socket_ov).hEvent);
XG_BASE(control_socket_ov).hEvent = 0;
}
}
#endif
#endif