Passed
Push — master ( c2a6de...b42a4e )
by Shinji
01:35
created

GetTraceCommand::writeError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
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\Command\CommandSettingsException;
17
use PhpProfiler\Command\Inspector\Settings\GetTraceSettings;
18
use PhpProfiler\Command\Inspector\Settings\LoopSettings;
19
use PhpProfiler\Command\Inspector\Settings\TargetProcessSettings;
20
use PhpProfiler\Lib\Elf\Parser\ElfParserException;
21
use PhpProfiler\Lib\Elf\Process\ProcessSymbolReaderException;
22
use PhpProfiler\Lib\Elf\Tls\TlsFinderException;
23
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
24
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
25
use PhpProfiler\Lib\PhpProcessReader\PhpMemoryReader\ExecutorGlobalsReader;
26
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...
27
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...
28
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...
29
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...
30
31
final class GetTraceCommand extends Command
32
{
33
    private PhpGlobalsFinder $php_globals_finder;
34
    private ExecutorGlobalsReader $executor_globals_reader;
35
    private TraceLoopProvider $loop_provider;
36
37
    /**
38
     * GetTraceCommand constructor.
39
     *
40
     * @param PhpGlobalsFinder $php_globals_finder
41
     * @param ExecutorGlobalsReader $executor_globals_reader
42
     * @param TraceLoopProvider $loop_provider
43
     */
44
    public function __construct(
45
        PhpGlobalsFinder $php_globals_finder,
46
        ExecutorGlobalsReader $executor_globals_reader,
47
        TraceLoopProvider $loop_provider
48
    ) {
49
        parent::__construct();
50
        $this->php_globals_finder = $php_globals_finder;
51
        $this->executor_globals_reader = $executor_globals_reader;
52
        $this->loop_provider = $loop_provider;
53
    }
54
55
    public function configure(): void
56
    {
57
        $this->setName('inspector:trace')
58
            ->setDescription('periodically get call trace from an outer process or thread')
59
            ->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id')
60
            ->addOption('depth', 'd', InputOption::VALUE_OPTIONAL, 'max depth')
61
            ->addOption(
62
                'sleep-ns',
63
                's',
64
                InputOption::VALUE_OPTIONAL,
65
                'nanoseconds between traces (default: 1000 * 1000 * 10)'
66
            );
67
    }
68
69
    /**
70
     * @param InputInterface $input
71
     * @param OutputInterface $output
72
     * @return int
73
     * @throws MemoryReaderException
74
     * @throws ProcessSymbolReaderException
75
     * @throws ElfParserException
76
     * @throws TlsFinderException
77
     * @throws CommandSettingsException
78
     */
79
    public function execute(InputInterface $input, OutputInterface $output): int
80
    {
81
        $target_process_settings = TargetProcessSettings::fromConsoleInput($input);
82
        $loop_settings = LoopSettings::fromConsoleInput($input);
83
        $get_trace_settings = GetTraceSettings::fromConsoleInput($input);
84
85
        $eg_address = $this->php_globals_finder->findExecutorGlobals($target_process_settings->pid);
86
87
        $this->loop_provider->getMainLoop(
88
            function () use ($get_trace_settings, $target_process_settings, $eg_address, $output): bool {
89
                $call_trace = $this->executor_globals_reader->readCallTrace(
90
                    $target_process_settings->pid,
91
                    $eg_address,
92
                    $get_trace_settings->depth
93
                );
94
                $output->writeln(join(PHP_EOL, $call_trace) . PHP_EOL);
95
                return true;
96
            },
97
            $loop_settings
98
        )->invoke();
99
100
        return 0;
101
    }
102
}
103