|
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\Lib\ByteStream; |
|
15
|
|
|
|
|
16
|
|
|
use OutOfBoundsException; |
|
17
|
|
|
use PhpProfiler\Lib\Process\MemoryMap\ProcessModuleMemoryMapInterface; |
|
18
|
|
|
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderInterface; |
|
19
|
|
|
|
|
20
|
|
|
use function chr; |
|
21
|
|
|
use function max; |
|
22
|
|
|
|
|
23
|
|
|
final class ProcessMemoryByteReader implements ByteReaderInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use ByteReaderDisableWriteAccessTrait; |
|
26
|
|
|
|
|
27
|
|
|
private const PAGE_SIZE = 8192; |
|
28
|
|
|
|
|
29
|
|
|
/** @var CDataByteReader[] */ |
|
30
|
|
|
private array $pages = []; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
private MemoryReaderInterface $memory_reader, |
|
34
|
|
|
private int $pid, |
|
35
|
|
|
private ProcessModuleMemoryMapInterface $memory_map |
|
36
|
|
|
) { |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function offsetExists($offset): bool |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->memory_map->isInRange($offset); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function offsetGet($offset): int |
|
45
|
|
|
{ |
|
46
|
|
|
if (!isset($this[$offset])) { |
|
47
|
|
|
throw new OutOfBoundsException(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$base_address = $this->memory_map->getBaseAddress(); |
|
51
|
|
|
|
|
52
|
|
|
$page = (int)($offset / self::PAGE_SIZE); |
|
53
|
|
|
$page_block = $this->locatePage($page, $base_address); |
|
54
|
|
|
|
|
55
|
|
|
$diff = 0; |
|
56
|
|
|
if ($page * self::PAGE_SIZE < $base_address) { |
|
57
|
|
|
$diff = $base_address - $page * self::PAGE_SIZE; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $page_block[($offset % self::PAGE_SIZE) - $diff]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function locatePage(int $page, int $base_address): CDataByteReader |
|
64
|
|
|
{ |
|
65
|
|
|
if (!isset($this->pages[$page])) { |
|
66
|
|
|
$this->pages[$page] = new CDataByteReader( |
|
67
|
|
|
$this->memory_reader->read( |
|
68
|
|
|
$this->pid, |
|
69
|
|
|
max($base_address, $page * self::PAGE_SIZE), |
|
70
|
|
|
self::PAGE_SIZE |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
return $this->pages[$page]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function createSliceAsString(int $offset, int $size): string |
|
78
|
|
|
{ |
|
79
|
|
|
$result = ''; |
|
80
|
|
|
for ($i = $offset; $i < ($offset + $size); $i++) { |
|
81
|
|
|
$result .= chr($this[$i]); |
|
82
|
|
|
} |
|
83
|
|
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|