Skip to content

Commit

Permalink
add options to manually specify the path to the php binary and libpth…
Browse files Browse the repository at this point in the history
…read.so
  • Loading branch information
sj-i committed May 28, 2020
1 parent 50166bd commit e300d23
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/Command/Inspector/GetCurrentFunctionNameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ public function configure(): void
InputOption::VALUE_OPTIONAL,
'regex to find the libpthread.so loaded in the target process'
)
->addOption(
'php-path',
null,
InputOption::VALUE_OPTIONAL,
'path to the php binary (only needed for chrooted ZTS target)'
)
->addOption(
'libpthread-path',
null,
InputOption::VALUE_OPTIONAL,
'path to the libpthread.so (only needed for chrooted ZTS target)'
)
;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Command/Inspector/GetEgAddressCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public function configure(): void
InputOption::VALUE_OPTIONAL,
'regex to find the libpthread.so loaded in the target process'
)
->addOption(
'php-path',
null,
InputOption::VALUE_OPTIONAL,
'path to the php binary (only needed for chrooted ZTS target)'
)
->addOption(
'libpthread-path',
null,
InputOption::VALUE_OPTIONAL,
'path to the libpthread.so (only needed for chrooted ZTS target)'
)
;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Command/Inspector/GetTraceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public function configure(): void
InputOption::VALUE_OPTIONAL,
'regex to find the libpthread.so loaded in the target process'
)
->addOption(
'php-path',
null,
InputOption::VALUE_OPTIONAL,
'path to the php binary (only needed in tracing chrooted ZTS target)'
)
->addOption(
'libpthread-path',
null,
InputOption::VALUE_OPTIONAL,
'path to the libpthread.so (only needed in tracing chrooted ZTS target)'
)
;
}

Expand Down
26 changes: 24 additions & 2 deletions src/Command/Inspector/Settings/TargetProcessSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ class TargetProcessSettings
public int $pid;
public string $php_regex;
public string $libpthread_regex;
public ?string $php_path;
public ?string $libpthread_path;

/**
* GetTraceSettings constructor.
* @param int $pid
* @param string $php_regex
* @param string $libpthread_regex
* @param string|null $php_path
* @param string|null $libpthread_path
*/
public function __construct(
int $pid,
string $php_regex = self::PHP_REGEX_DEFAULT,
string $libpthread_regex = self::LIBPTHREAD_REGEX_DEFAULT
string $libpthread_regex = self::LIBPTHREAD_REGEX_DEFAULT,
?string $php_path = null,
?string $libpthread_path = null
) {
$this->pid = $pid;
$this->php_regex = '{' . $php_regex . '}';
$this->libpthread_regex = '{' . $libpthread_regex . '}';
$this->php_path = $php_path;
$this->libpthread_path = $libpthread_path;
}

/**
Expand Down Expand Up @@ -75,6 +83,20 @@ public static function fromConsoleInput(InputInterface $input): self
);
}

return new self($pid, $php_regex, $libpthread_regex);
$php_path = $input->getOption('php-path');
if (!is_null($php_path) and !is_string($php_path)) {
throw TargetProcessSettingsException::create(
TargetProcessSettingsException::PHP_PATH_IS_NOT_STRING
);
}

$libpthread_path = $input->getOption('libpthread-path');
if (!is_null($libpthread_path) and !is_string($libpthread_path)) {
throw TargetProcessSettingsException::create(
TargetProcessSettingsException::LIBPTHREAD_PATH_IS_NOT_STRING
);
}

return new self($pid, $php_regex, $libpthread_regex, $php_path, $libpthread_path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ class TargetProcessSettingsException extends CommandSettingsException
public const PID_IS_NOT_INTEGER = 2;
public const PHP_REGEX_IS_NOT_STRING = 3;
public const LIBPTHREAD_REGEX_IS_NOT_STRING = 4;
public const PHP_PATH_IS_NOT_STRING = 5;
public const LIBPTHREAD_PATH_IS_NOT_STRING = 6;

protected const ERRORS = [
self::PID_NOT_SPECIFIED => 'pid is not specified',
self::PID_IS_NOT_INTEGER => 'pid is not integer',
self::PHP_REGEX_IS_NOT_STRING => 'php-regex must be a string',
self::LIBPTHREAD_REGEX_IS_NOT_STRING => 'libpthread-regex must be a string',
self::PHP_PATH_IS_NOT_STRING => 'php-path must be a string',
self::LIBPTHREAD_PATH_IS_NOT_STRING => 'libpthread-path must be a string',
];
}
9 changes: 5 additions & 4 deletions src/Lib/Elf/Process/ProcessModuleSymbolReaderCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ public function __construct(
* @param int $pid
* @param ProcessMemoryMap $process_memory_map
* @param string $regex
* @param string|null $binary_path
* @param int|null $tls_block_address
* @return ProcessModuleSymbolReader|null
* @throws ElfParserException
*/
public function createModuleReaderByNameRegex(
int $pid,
ProcessMemoryMap $process_memory_map,
string $regex,
?string $binary_path,
?int $tls_block_address = null
): ?ProcessModuleSymbolReader {
$memory_areas = $process_memory_map->findByNameRegex($regex);
Expand All @@ -63,9 +64,9 @@ public function createModuleReaderByNameRegex(
$module_memory_map = new ProcessModuleMemoryMap($memory_areas);

$module_name = current($memory_areas)->name;
$container_aware_path = $this->createContainerAwarePath($pid, $module_name);
$path = $binary_path ?? $this->createContainerAwarePath($pid, $module_name);
try {
$symbol_resolver = $this->symbol_resolver_creator->createLinearScanResolverFromPath($container_aware_path);
$symbol_resolver = $this->symbol_resolver_creator->createLinearScanResolverFromPath($path);
return new ProcessModuleSymbolReader(
$pid,
$symbol_resolver,
Expand All @@ -75,7 +76,7 @@ public function createModuleReaderByNameRegex(
);
} catch (ElfParserException $e) {
try {
$symbol_resolver = $this->symbol_resolver_creator->createDynamicResolverFromPath($container_aware_path);
$symbol_resolver = $this->symbol_resolver_creator->createDynamicResolverFromPath($path);
return new ProcessModuleSymbolReader(
$pid,
$symbol_resolver,
Expand Down
4 changes: 3 additions & 1 deletion src/Lib/PhpProcessReader/PhpGlobalsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public function getSymbolReader(TargetProcessSettings $target_process_settings):
$symbol_reader = $this->php_symbol_reader_creator->create(
$target_process_settings->pid,
$target_process_settings->php_regex,
$target_process_settings->libpthread_regex
$target_process_settings->libpthread_regex,
$target_process_settings->php_path,
$target_process_settings->libpthread_path
);
$this->php_symbol_reader_cache[$target_process_settings->pid] = $symbol_reader;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Lib/PhpProcessReader/PhpSymbolReaderCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,28 @@ public function __construct(
* @param int $pid
* @param string $php_finder_regex
* @param string $libpthread_finder_regex
* @param string|null $php_binar_path
* @param string|null $libpthread_binary_path
* @return ProcessModuleSymbolReader
* @throws ElfParserException
* @throws MemoryReaderException
* @throws ProcessSymbolReaderException
* @throws TlsFinderException
*/
public function create(
int $pid,
string $php_finder_regex,
string $libpthread_finder_regex
string $libpthread_finder_regex,
?string $php_binar_path,
?string $libpthread_binary_path
): ProcessModuleSymbolReader {
$process_memory_map = $this->process_memory_map_creator->getProcessMemoryMap($pid);

$tls_block_address = null;
$libpthread_symbol_reader = $this->process_module_symbol_reader_creator->createModuleReaderByNameRegex(
$pid,
$process_memory_map,
$libpthread_finder_regex
$libpthread_finder_regex,
$libpthread_binary_path
);
if (!is_null($libpthread_symbol_reader) and $libpthread_symbol_reader->isAllSymbolResolvable()) {
$tls_finder = new LibThreadDbTlsFinder(
Expand All @@ -93,6 +97,7 @@ public function create(
$pid,
$process_memory_map,
$php_finder_regex,
$php_binar_path,
$tls_block_address
);
if (is_null($php_symbol_reader)) {
Expand Down

0 comments on commit e300d23

Please sign in to comment.