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