Passed
Push — master ( d75285...c2a6de )
by Shinji
12:57
created

GetTraceCommand::execute()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 7
eloc 34
c 5
b 0
f 2
nc 12
nop 2
dl 0
loc 49
rs 8.4426
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 PhpProfiler\Lib\Elf\Parser\ElfParserException;
17
use PhpProfiler\Lib\Elf\Process\ProcessSymbolReaderException;
18
use PhpProfiler\Lib\Elf\Tls\TlsFinderException;
19
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
20
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
21
use PhpProfiler\Lib\PhpProcessReader\PhpMemoryReader\ExecutorGlobalsReader;
22
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Symfony\Component\Console\Output\ConsoleOutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...\ConsoleOutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
final class GetTraceCommand extends Command
29
{
30
    private const SLEEP_NANO_SECONDS_DEFAULT = 1000 * 1000 * 10;
31
32
    private PhpGlobalsFinder $php_globals_finder;
33
    private ExecutorGlobalsReader $executor_globals_reader;
34
    private TraceLoopProvider $loop_provider;
35
36
    /**
37
     * GetTraceCommand constructor.
38
     *
39
     * @param PhpGlobalsFinder $php_globals_finder
40
     * @param ExecutorGlobalsReader $executor_globals_reader
41
     * @param TraceLoopProvider $loop_provider
42
     * @param string|null $name
43
     */
44
    public function __construct(
45
        PhpGlobalsFinder $php_globals_finder,
46
        ExecutorGlobalsReader $executor_globals_reader,
47
        TraceLoopProvider $loop_provider,
48
        string $name = null
49
    ) {
50
        parent::__construct($name);
51
        $this->php_globals_finder = $php_globals_finder;
52
        $this->executor_globals_reader = $executor_globals_reader;
53
        $this->loop_provider = $loop_provider;
54
    }
55
56
    public function configure(): void
57
    {
58
        $this->setName('inspector:trace')
59
            ->setDescription('periodically get call trace from an outer process or thread')
60
            ->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id')
61
            ->addOption('depth', 'd', InputOption::VALUE_OPTIONAL, 'max depth')
62
            ->addOption(
63
                'sleep-ns',
64
                's',
65
                InputOption::VALUE_OPTIONAL,
66
                'nanoseconds between traces (default: 1000 * 1000 * 10)'
67
            );
68
    }
69
70
    /**
71
     * @param InputInterface $input
72
     * @param OutputInterface $output
73
     * @return int
74
     * @throws MemoryReaderException
75
     * @throws ProcessSymbolReaderException
76
     * @throws ElfParserException
77
     * @throws TlsFinderException
78
     */
79
    public function execute(InputInterface $input, OutputInterface $output): int
80
    {
81
        $pid = $input->getOption('pid');
82
        if (is_null($pid)) {
83
            $this->writeError('pid is not specified', $output);
84
            return 1;
85
        }
86
        $pid = filter_var($pid, FILTER_VALIDATE_INT);
87
        if ($pid === false) {
88
            $this->writeError('pid is not integer', $output);
89
            return 2;
90
        }
91
92
        $depth = $input->getOption('depth');
93
        if (is_null($depth)) {
94
            $depth = PHP_INT_MAX;
95
        }
96
        $depth = filter_var($depth, FILTER_VALIDATE_INT);
97
        if ($depth === false) {
98
            $this->writeError('depth is not integer', $output);
99
            return 2;
100
        }
101
102
        $sleep_nano_seconds = $input->getOption('sleep-ns');
103
        if (is_null($sleep_nano_seconds)) {
104
            $sleep_nano_seconds = self::SLEEP_NANO_SECONDS_DEFAULT;
105
        }
106
        $sleep_nano_seconds = filter_var($sleep_nano_seconds, FILTER_VALIDATE_INT);
107
        if ($sleep_nano_seconds === false) {
108
            $this->writeError('sleep-ns is not integer', $output);
109
            return 2;
110
        }
111
112
        $eg_address = $this->php_globals_finder->findExecutorGlobals($pid);
113
114
        $this->loop_provider->getMainLoop(
115
            function () use ($pid, $eg_address, $depth, $output): bool {
116
                $call_trace = $this->executor_globals_reader->readCallTrace(
117
                    $pid,
118
                    $eg_address,
119
                    $depth
120
                );
121
                $output->writeln(join(PHP_EOL, $call_trace) . PHP_EOL);
122
                return true;
123
            },
124
            $sleep_nano_seconds
125
        )->invoke();
126
127
        return 0;
128
    }
129
130
    public function writeError(string $message, OutputInterface $output): void
131
    {
132
        $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
133
        $error_output->writeln($message);
134
    }
135
}
136