|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sj-i/php-profiler package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace PhpProfiler\Command\Inspector; |
|
15
|
|
|
|
|
16
|
|
|
use Amp\Loop; |
|
|
|
|
|
|
17
|
|
|
use Amp\Promise; |
|
|
|
|
|
|
18
|
|
|
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContext; |
|
19
|
|
|
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreator; |
|
20
|
|
|
use PhpProfiler\Inspector\Daemon\Searcher\Context\PhpSearcherContextCreator; |
|
21
|
|
|
use PhpProfiler\Inspector\Settings\DaemonSettings; |
|
22
|
|
|
use PhpProfiler\Inspector\Settings\GetTraceSettings; |
|
23
|
|
|
use PhpProfiler\Inspector\Settings\TargetPhpSettings; |
|
24
|
|
|
use PhpProfiler\Inspector\Settings\TraceLoopSettings; |
|
25
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
|
|
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
|
|
27
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
|
|
|
|
|
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
final class DaemonCommand extends Command |
|
31
|
|
|
{ |
|
32
|
|
|
private PhpSearcherContextCreator $php_searcher_context_creator; |
|
33
|
|
|
private PhpReaderContextCreator $php_reader_context_creator; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
PhpSearcherContextCreator $php_searcher_context_creator, |
|
37
|
|
|
PhpReaderContextCreator $php_reader_context_creator |
|
38
|
|
|
) { |
|
39
|
|
|
parent::__construct(); |
|
40
|
|
|
$this->php_reader_context_creator = $php_reader_context_creator; |
|
41
|
|
|
$this->php_searcher_context_creator = $php_searcher_context_creator; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function configure(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->setName('inspector:daemon') |
|
47
|
|
|
->setDescription('periodically get running function name from an outer process or thread') |
|
48
|
|
|
->addOption( |
|
49
|
|
|
'target-regex', |
|
50
|
|
|
'P', |
|
51
|
|
|
InputOption::VALUE_OPTIONAL, |
|
52
|
|
|
'regex to find the php binary loaded in the target process' |
|
53
|
|
|
) |
|
54
|
|
|
->addOption('depth', 'd', InputOption::VALUE_OPTIONAL, 'max depth') |
|
55
|
|
|
->addOption( |
|
56
|
|
|
'sleep-ns', |
|
57
|
|
|
's', |
|
58
|
|
|
InputOption::VALUE_OPTIONAL, |
|
59
|
|
|
'nanoseconds between traces (default: 1000 * 1000 * 10)' |
|
60
|
|
|
) |
|
61
|
|
|
->addOption( |
|
62
|
|
|
'max-retries', |
|
63
|
|
|
'r', |
|
64
|
|
|
InputOption::VALUE_OPTIONAL, |
|
65
|
|
|
'max retries on contiguous errors of read (default: 10)' |
|
66
|
|
|
) |
|
67
|
|
|
->addOption( |
|
68
|
|
|
'threads', |
|
69
|
|
|
'T', |
|
70
|
|
|
InputOption::VALUE_OPTIONAL, |
|
71
|
|
|
'number of workers (default: 8)' |
|
72
|
|
|
) |
|
73
|
|
|
->addOption( |
|
74
|
|
|
'php-regex', |
|
75
|
|
|
null, |
|
76
|
|
|
InputOption::VALUE_OPTIONAL, |
|
77
|
|
|
'regex to find the php binary loaded in the target process' |
|
78
|
|
|
) |
|
79
|
|
|
->addOption( |
|
80
|
|
|
'libpthread-regex', |
|
81
|
|
|
null, |
|
82
|
|
|
InputOption::VALUE_OPTIONAL, |
|
83
|
|
|
'regex to find the libpthread.so loaded in the target process' |
|
84
|
|
|
) |
|
85
|
|
|
->addOption( |
|
86
|
|
|
'php-version', |
|
87
|
|
|
null, |
|
88
|
|
|
InputOption::VALUE_OPTIONAL, |
|
89
|
|
|
'php version of the target' |
|
90
|
|
|
) |
|
91
|
|
|
->addOption( |
|
92
|
|
|
'php-path', |
|
93
|
|
|
null, |
|
94
|
|
|
InputOption::VALUE_OPTIONAL, |
|
95
|
|
|
'path to the php binary (only needed in tracing chrooted ZTS target)' |
|
96
|
|
|
) |
|
97
|
|
|
->addOption( |
|
98
|
|
|
'libpthread-path', |
|
99
|
|
|
null, |
|
100
|
|
|
InputOption::VALUE_OPTIONAL, |
|
101
|
|
|
'path to the libpthread.so (only needed in tracing chrooted ZTS target)' |
|
102
|
|
|
) |
|
103
|
|
|
; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param InputInterface $input |
|
108
|
|
|
* @param OutputInterface $output |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
|
112
|
|
|
{ |
|
113
|
|
|
$target_php_settings = TargetPhpSettings::fromConsoleInput($input); |
|
114
|
|
|
$loop_settings = TraceLoopSettings::fromConsoleInput($input); |
|
115
|
|
|
$get_trace_settings = GetTraceSettings::fromConsoleInput($input); |
|
116
|
|
|
$daemon_settings = DaemonSettings::fromConsoleInput($input); |
|
117
|
|
|
|
|
118
|
|
|
$searcher_context = $this->php_searcher_context_creator->create(); |
|
119
|
|
|
Promise\wait($searcher_context->start()); |
|
|
|
|
|
|
120
|
|
|
Promise\wait($searcher_context->sendTargetRegex($daemon_settings->target_regex)); |
|
121
|
|
|
$pid_list = Promise\wait($searcher_context->receivePidList()); |
|
122
|
|
|
|
|
123
|
|
|
$readers = []; |
|
124
|
|
|
foreach ($pid_list as $target_pid) { |
|
125
|
|
|
$reader_context = $this->php_reader_context_creator->create(); |
|
126
|
|
|
Promise\wait($reader_context->start()); |
|
127
|
|
|
Promise\wait( |
|
128
|
|
|
$reader_context->sendSettings( |
|
129
|
|
|
[ |
|
130
|
|
|
$target_pid, |
|
131
|
|
|
$target_php_settings, |
|
132
|
|
|
$loop_settings, |
|
133
|
|
|
$get_trace_settings |
|
134
|
|
|
] |
|
135
|
|
|
) |
|
136
|
|
|
); |
|
137
|
|
|
$readers[$target_pid] = $reader_context; |
|
138
|
|
|
} |
|
139
|
|
|
exec('stty -icanon -echo'); |
|
140
|
|
|
|
|
141
|
|
|
Loop::run(function () use (&$readers, $output) { |
|
142
|
|
|
Loop::onReadable( |
|
143
|
|
|
STDIN, |
|
144
|
|
|
/** @param resource $stream */ |
|
145
|
|
|
function (string $watcher_id, $stream) { |
|
146
|
|
|
$key = fread($stream, 1); |
|
147
|
|
|
if ($key === 'q') { |
|
148
|
|
|
Loop::cancel($watcher_id); |
|
149
|
|
|
Loop::stop(); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
); |
|
153
|
|
|
Loop::repeat(10, function () use (&$readers, $output) { |
|
154
|
|
|
/** @var array<int, PhpReaderContext> $readers */ |
|
155
|
|
|
|
|
156
|
|
|
$promises = []; |
|
157
|
|
|
foreach ($readers as $pid => $reader) { |
|
158
|
|
|
if (!$reader->isRunning()) { |
|
159
|
|
|
/** @psalm-suppress MixedArrayAccess*/ |
|
160
|
|
|
unset($readers[$pid]); |
|
161
|
|
|
continue; |
|
162
|
|
|
} |
|
163
|
|
|
$promises[] = \Amp\call( |
|
|
|
|
|
|
164
|
|
|
function () use ($reader, &$readers, $pid, $output) { |
|
165
|
|
|
/** @psalm-suppress MixedArrayAccess*/ |
|
166
|
|
|
unset($readers[$pid]); |
|
167
|
|
|
/** @var string $result */ |
|
168
|
|
|
$result = yield $reader->receiveTrace(); |
|
169
|
|
|
$output->write($result); |
|
170
|
|
|
$readers[$pid] = $reader; |
|
171
|
|
|
} |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
yield $promises; |
|
175
|
|
|
}); |
|
176
|
|
|
}); |
|
177
|
|
|
|
|
178
|
|
|
return 0; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths