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