/
dbgpclient.php
299 lines (257 loc) · 7.44 KB
/
dbgpclient.php
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
<?php
require_once __DIR__ . '/../../utils.inc';
class DebugClient
{
// free port will be selected automatically by the operating system
protected $port = 0;
private $tmpDir;
protected $socket;
protected $php;
protected $ppipes;
private $pid = null;
public function getPort()
{
return $this->port;
}
public function setPort($port)
{
$this->port = $port;
}
protected function getIPAddress()
{
return "127.0.0.1";
}
protected function getAddress()
{
return "tcp://" . $this->getIPAddress() . ":" . $this->getPort();
}
public function __construct()
{
$this->tmpDir = getTmpDir();
}
private function open( &$errno, &$errstr )
{
$socket = @stream_socket_server( $this->getAddress(), $errno, $errstr );
if ( $socket )
{
$name = stream_socket_get_name( $socket, false );
$name = explode( ":", $name );
$this->port = array_pop( $name );
}
return $socket;
}
private function launchPhp( &$pipes, $filename, array $ini_options = [], array $extra_options = [] )
{
@unlink( $this->tmpDir . 'error-output.txt' );
@unlink( $this->tmpDir . 'remote_log.txt' );
$descriptorspec = array(
0 => array( 'pipe', 'r' ),
1 => array( 'pipe', 'w' ),
2 => array( 'file', $this->tmpDir . 'error-output.txt', 'a' )
);
$default_options = array(
"xdebug.mode" => "debug",
"xdebug.start_with_request" => "'yes'",
"xdebug.client_host" => $this->getIPAddress(),
"xdebug.client_port" => $this->getPort(),
);
$env_vars = array_key_exists( 'env', $extra_options ) ? $extra_options['env'] : [];
$env_vars += $_ENV;
$options = (getenv('TEST_PHP_ARGS') ?: '');
$ini_options = array_merge( $default_options, $ini_options );
foreach ( $ini_options as $key => $value )
{
$options .= " -d{$key}=$value";
}
if ( array_key_exists( 'auto_prepend', $extra_options ) )
{
$prependFile = "{$this->tmpDir}auto-prepend.inc";
file_put_contents( $prependFile, $extra_options['auto_prepend'] );
$options .= " -dauto_prepend_file={$prependFile}";
}
$php = getenv( 'TEST_PHP_EXECUTABLE' );
$cmd = "{$php} $options {$filename} >{$this->tmpDir}php-stdout.txt 2>{$this->tmpDir}php-stderr.txt";
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$cmd = "exec {$cmd}";
}
$cwd = dirname( __FILE__ );
$process = proc_open( $cmd, $descriptorspec, $pipes, $cwd, $env_vars );
return $process;
}
function fixFilePath( $m )
{
preg_match( '@.*/(.*\.inc)@', $m[2], $fm );
if ( !isset( $fm[1] ) )
{
$fm[1] = '';
}
return " {$m[1]}=\"file://{$fm[1]}\"";
}
function doRead( $conn, ?string $transaction_id = null )
{
stream_set_timeout( $conn, 3 );
do {
$trans_id = null;
$length = 0;
while ( "\0" !== ( $char = fgetc($conn) ) ) {
if ( $char === false ) {
echo "read a false for $transaction_id" . PHP_EOL;
return null;
}
if ( !is_numeric($char) ) {
echo "read a non-number for $transaction_id" . PHP_EOL;
return null;
}
$length = $length * 10 + (int)$char;
}
$read = '';
while ( 0 < $length ) {
$data = fread( $conn, $length );
if ( $data === false )
{
echo "read a false for $transaction_id" . PHP_EOL;
return null;
}
$length -= strlen( $data );
$read .= $data;
}
$char = fgetc( $conn );
if ( $char !== "\0" )
{
echo 'must end with \0' . PHP_EOL;
}
// sanitize
$read = preg_replace( '@(?<!"Locals") id="\d+?(\d{4})"@', ' id="{{PID}}\\1"', $read );
$read = preg_replace( '@\s(appid)="\d+?"@', ' \\1=""', $read );
$read = preg_replace( '@\s(xdebug:language_version)="[^"]+?"@', ' \\1=""', $read );
$read = preg_replace( '@(engine\sversion)="[^"]+?"@', '\\1=""', $read );
$read = preg_replace( '@(2002-20[0-9]{2})@', '2002-2099', $read );
$read = preg_replace_callback( '@\s(fileuri|filename)="file:///(.+?)"@', self::class . '::fixFilePath', $read );
echo $read, "\n\n";
if ( preg_match( '@<stream xmlns="urn.debugger_protocol_v1" xmlns:xdebug@', $read ) )
{
continue;
}
if ( preg_match( '@<notify xmlns="urn.debugger_protocol_v1" xmlns:xdebug@', $read ) )
{
continue;
}
$matches = [];
if ( preg_match( '@transaction_id="(?P<transaction_id>\d+)"@', $read, $matches ) )
{
$trans_id = $matches['transaction_id'] ?? null;
}
} while ( $trans_id !== $transaction_id );
}
function start( $filename, array $ini_options = [], array $options = [])
{
$filename = realpath( $filename );
$this->socket = $this->open( $errno, $errstr );
if ( $this->socket === false )
{
echo "Could not create socket server - already in use?\n";
echo "Error: {$errstr}, errno: {$errno}\n";
echo "Address: {$this->getAddress()}\n";
return false;
}
$this->php = $this->launchPhp( $this->ppipes, $filename, $ini_options, $options );
$conn = @stream_socket_accept( $this->socket, isset( $options['timeout'] ) ? $options['timeout'] : 5 );
if ( $conn === false )
{
echo @file_get_contents( $this->tmpDir . 'php-stdout.txt' ), "\n";
echo @file_get_contents( $this->tmpDir . 'php-stderr.txt' ), "\n";
echo @file_get_contents( $this->tmpDir . 'error-output.txt' ), "\n";
echo @file_get_contents( $this->tmpDir . 'remote_log.txt' ), "\n";
proc_close( $this->php );
return false;
}
return $conn;
}
function stop( $conn, array $options = [] )
{
fclose( $conn );
fclose( $this->ppipes[0] );
fclose( $this->ppipes[1] );
fclose( $this->socket );
proc_close( $this->php );
if ( array_key_exists( 'show-stdout', $options ) && $options['show-stdout'] )
{
echo @file_get_contents( $this->tmpDir . 'php-stdout.txt' ), "\n";
}
// echo @file_get_contents( $this->tmpDir . 'php-stderr.txt' ), "\n";
// echo @file_get_contents( $this->tmpDir . 'error-output.txt' ), "\n";
}
function sendCommand( $conn, $command, $transaction_id )
{
// inject identifier
$parts = explode( ' ', $command, 2 );
if ( count($parts) == 1 )
{
$command = $parts[0] . " -i $transaction_id";
}
else
{
$command = $parts[0] . " -i $transaction_id " . $parts[1];
}
/* Replace PID macro */
$command = str_replace( "{{PID}}", $this->pid & 0x1ffff, $command );
$sanitised = $command;
$sanitised = preg_replace( '@\sfile://.*[/\\\\](.*\.inc)\s@', ' file://\\1 ', $sanitised );
echo "-> ", $sanitised, "\n";
fwrite( $conn, $command . "\0" );
}
function runTest( $filename, array $commands, array $ini_options = [], array $options = [] )
{
$conn = $this->start( $filename, $ini_options, $options );
if ( $conn === false )
{
return;
}
$i = 1;
$procInfo = proc_get_status( $this->php );
$this->pid = $procInfo['pid'];
// read header
$this->doRead( $conn );
foreach ( $commands as $command )
{
$this->sendCommand( $conn, $command, $i );
$this->doRead( $conn, (string)$i );
$i++;
}
$this->stop( $conn, $options );
}
}
class DebugClientIPv6 extends DebugClient
{
protected function getIPAddress()
{
return "::1";
}
protected function getAddress()
{
return "tcp://[" . $this->getIPAddress() . "]:" . $this->getPort();
}
public static function isSupported( &$errno, &$errstr )
{
$socket = @stream_socket_server( "tcp://[::1]:0", $errno, $errstr );
if ( $socket === false )
{
return false;
}
fclose( $socket );
return true;
}
}
function dbgpRunFile( $data, $commands, array $ini_options = [], array $options = [] )
{
if ( isset( $options['ipv'] ) && $options['ipv'] == 6 )
{
$t = new DebugClientIPv6();
}
else
{
$t = new DebugClient();
}
$t->runTest( $data, $commands, $ini_options, $options );
}
?>