|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the reliforp/reli-prof 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 Reli\Lib\PhpProcessReader; |
|
15
|
|
|
|
|
16
|
|
|
use Reli\Lib\ByteStream\IntegerByteSequence\IntegerByteSequenceReader; |
|
17
|
|
|
use Reli\Lib\Elf\Parser\ElfParserException; |
|
18
|
|
|
use Reli\Lib\Elf\Process\ProcessModuleSymbolReader; |
|
19
|
|
|
use Reli\Lib\Elf\Process\ProcessModuleSymbolReaderCreator; |
|
20
|
|
|
use Reli\Lib\Elf\Process\ProcessSymbolReaderException; |
|
21
|
|
|
use Reli\Lib\Elf\Tls\LibThreadDbTlsFinder; |
|
22
|
|
|
use Reli\Lib\Elf\Tls\TlsFinderException; |
|
23
|
|
|
use Reli\Lib\Elf\Tls\X64LinuxThreadPointerRetriever; |
|
24
|
|
|
use Reli\Lib\Process\MemoryMap\ProcessMemoryMapCreator; |
|
25
|
|
|
use Reli\Lib\Process\MemoryReader\MemoryReaderException; |
|
26
|
|
|
use Reli\Lib\Process\MemoryReader\MemoryReaderInterface; |
|
27
|
|
|
|
|
28
|
|
|
final class PhpSymbolReaderCreator |
|
29
|
|
|
{ |
|
30
|
|
|
public function __construct( |
|
31
|
|
|
private MemoryReaderInterface $memory_reader, |
|
32
|
|
|
private ProcessModuleSymbolReaderCreator $process_module_symbol_reader_creator, |
|
33
|
|
|
private ProcessMemoryMapCreator $process_memory_map_creator, |
|
34
|
|
|
private IntegerByteSequenceReader $integer_reader, |
|
35
|
|
|
) { |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws MemoryReaderException |
|
40
|
|
|
* @throws ProcessSymbolReaderException |
|
41
|
|
|
* @throws TlsFinderException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function create( |
|
44
|
|
|
int $pid, |
|
45
|
|
|
string $php_finder_regex, |
|
46
|
|
|
string $libpthread_finder_regex, |
|
47
|
|
|
?string $php_binar_path, |
|
48
|
|
|
?string $libpthread_binary_path |
|
49
|
|
|
): ProcessModuleSymbolReader { |
|
50
|
|
|
$process_memory_map = $this->process_memory_map_creator->getProcessMemoryMap($pid); |
|
51
|
|
|
|
|
52
|
|
|
$tls_block_address = null; |
|
53
|
|
|
$libpthread_symbol_reader = $this->process_module_symbol_reader_creator->createModuleReaderByNameRegex( |
|
54
|
|
|
$pid, |
|
55
|
|
|
$process_memory_map, |
|
56
|
|
|
$libpthread_finder_regex, |
|
57
|
|
|
$libpthread_binary_path |
|
58
|
|
|
); |
|
59
|
|
|
if (!is_null($libpthread_symbol_reader)) { |
|
60
|
|
|
try { |
|
61
|
|
|
$tls_finder = new LibThreadDbTlsFinder( |
|
62
|
|
|
$libpthread_symbol_reader, |
|
63
|
|
|
X64LinuxThreadPointerRetriever::createDefault(), |
|
64
|
|
|
$this->memory_reader, |
|
65
|
|
|
$this->integer_reader |
|
66
|
|
|
); |
|
67
|
|
|
$tls_block_address = $tls_finder->findTlsBlock($pid, 1); |
|
68
|
|
|
} catch (TlsFinderException $e) { |
|
69
|
|
|
$tls_block_address = null; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$php_symbol_reader = $this->process_module_symbol_reader_creator->createModuleReaderByNameRegex( |
|
74
|
|
|
$pid, |
|
75
|
|
|
$process_memory_map, |
|
76
|
|
|
$php_finder_regex, |
|
77
|
|
|
$php_binar_path, |
|
78
|
|
|
$tls_block_address |
|
79
|
|
|
); |
|
80
|
|
|
if (is_null($php_symbol_reader)) { |
|
81
|
|
|
throw new \RuntimeException('php module not found'); |
|
82
|
|
|
} |
|
83
|
|
|
return $php_symbol_reader; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|