diff --git a/src/Command/Inspector/GetCurrentFunctionNameCommand.php b/src/Command/Inspector/GetCurrentFunctionNameCommand.php deleted file mode 100644 index f66b414f..00000000 --- a/src/Command/Inspector/GetCurrentFunctionNameCommand.php +++ /dev/null @@ -1,107 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace PhpProfiler\Command\Inspector; - -use PhpProfiler\Inspector\RetryingLoopProvider; -use PhpProfiler\Inspector\Settings\InspectorSettingsException; -use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettingsFromConsoleInput; -use PhpProfiler\Inspector\Settings\TargetProcessSettings\TargetProcessSettingsFromConsoleInput; -use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettingsFromConsoleInput; -use PhpProfiler\Inspector\TargetProcess\TargetProcessResolver; -use PhpProfiler\Inspector\TraceLoopProvider; -use PhpProfiler\Lib\Elf\Parser\ElfParserException; -use PhpProfiler\Lib\Elf\Process\ProcessSymbolReaderException; -use PhpProfiler\Lib\Elf\Tls\TlsFinderException; -use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder; -use PhpProfiler\Lib\PhpProcessReader\PhpVersionDetector; -use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException; -use PhpProfiler\Lib\PhpProcessReader\PhpMemoryReader\CallTraceReader; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -final class GetCurrentFunctionNameCommand extends Command -{ - public function __construct( - private PhpGlobalsFinder $php_globals_finder, - private PhpVersionDetector $php_version_detector, - private CallTraceReader $executor_globals_reader, - private TraceLoopProvider $loop_provider, - private TargetPhpSettingsFromConsoleInput $target_php_settings_from_console_input, - private TargetProcessSettingsFromConsoleInput $target_process_settings_from_console_input, - private TraceLoopSettingsFromConsoleInput $trace_loop_settings_from_console_input, - private TargetProcessResolver $target_process_resolver, - private RetryingLoopProvider $retrying_loop_provider, - ) { - parent::__construct(); - } - - public function configure(): void - { - $this->setName('inspector:current_function') - ->setDescription('periodically get running function name from an outer process or thread') - ; - $this->target_process_settings_from_console_input->setOptions($this); - $this->trace_loop_settings_from_console_input->setOptions($this); - $this->target_php_settings_from_console_input->setOptions($this); - } - - /** - * @throws MemoryReaderException - * @throws ProcessSymbolReaderException - * @throws ElfParserException - * @throws TlsFinderException - * @throws InspectorSettingsException - */ - public function execute(InputInterface $input, OutputInterface $output): int - { - $target_php_settings = $this->target_php_settings_from_console_input->createSettings($input); - $target_process_settings = $this->target_process_settings_from_console_input->createSettings($input); - $loop_settings = $this->trace_loop_settings_from_console_input->createSettings($input); - - $process_specifier = $this->target_process_resolver->resolve($target_process_settings); - - $target_php_settings = $this->php_version_detector->decidePhpVersion( - $process_specifier, - $target_php_settings - ); - - // see the comment at GetTraceCommand::execute() - $eg_address = $this->retrying_loop_provider->do( - try: fn () => $this->php_globals_finder->findExecutorGlobals( - $process_specifier, - $target_php_settings - ), - retry_on: [\Throwable::class], - max_retry: 10, - interval_on_retry_ns: 1000 * 1000 * 10, - ); - - $this->loop_provider->getMainLoop( - function () use ($process_specifier, $target_php_settings, $eg_address, $output): bool { - $output->writeln( - $this->executor_globals_reader->readCurrentFunctionName( - $process_specifier->pid, - $target_php_settings->php_version, - $eg_address - ) - ); - return true; - }, - $loop_settings - )->invoke(); - - return 0; - } -} diff --git a/src/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReader.php b/src/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReader.php index 68cd914b..f738f1f1 100644 --- a/src/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReader.php +++ b/src/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReader.php @@ -84,25 +84,6 @@ private function getExecutorGlobals( return $dereferencer->deref($eg_pointer); } - /** - * @param value-of $php_version - * @throws MemoryReaderException - */ - public function readCurrentFunctionName(int $pid, string $php_version, int $executor_globals_address): string - { - $dereferencer = $this->getDereferencer($pid, $php_version); - $eg = $this->getExecutorGlobals($executor_globals_address, $php_version, $dereferencer); - if (is_null($eg->current_execute_data)) { - throw new \Exception('cannot read current execute data'); - } - /** - * @var ZendExecuteData $current_execute_data - * @psalm-ignore-var - */ - $current_execute_data = $dereferencer->deref($eg->current_execute_data); - return $current_execute_data->getFunctionName($dereferencer) ?? 'unknown'; - } - /** * @param value-of $php_version * @throws MemoryReaderException diff --git a/tests/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReaderTest.php b/tests/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReaderTest.php index cdbf4263..3d57c938 100644 --- a/tests/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReaderTest.php +++ b/tests/Lib/PhpProcessReader/PhpMemoryReader/CallTraceReaderTest.php @@ -54,64 +54,6 @@ protected function tearDown(): void } } - public function testReadCurrentFunctionName() - { - $memory_reader = new MemoryReader(); - $executor_globals_reader = new CallTraceReader( - $memory_reader, - new ZendTypeReaderCreator(), - new OpcodeFactory() - ); - $this->child = proc_open( - [ - PHP_BINARY, - '-r', - 'fputs(STDOUT, "a\n");fgets(STDIN);' - ], - [ - ['pipe', 'r'], - ['pipe', 'w'], - ['pipe', 'w'] - ], - $pipes - ); - - fgets($pipes[1]); - $child_status = proc_get_status($this->child); - $php_symbol_reader_creator = new PhpSymbolReaderCreator( - $memory_reader, - new ProcessModuleSymbolReaderCreator( - new Elf64SymbolResolverCreator( - new CatFileReader(), - new Elf64Parser( - new LittleEndianReader() - ) - ), - $memory_reader, - ), - ProcessMemoryMapCreator::create(), - new LittleEndianReader() - ); - $php_globals_finder = new PhpGlobalsFinder( - $php_symbol_reader_creator, - new LittleEndianReader(), - new MemoryReader() - ); - - /** @var int $child_status['pid'] */ - $executor_globals_address = $php_globals_finder->findExecutorGlobals( - new ProcessSpecifier($child_status['pid']), - new TargetPhpSettings() - ); - $name = $executor_globals_reader->readCurrentFunctionName( - $child_status['pid'], - ZendTypeReader::V74, - $executor_globals_address - ); - $this->assertSame('fgets', $name); - } - - public function testReadCallTrace() { $memory_reader = new MemoryReader();