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
|
|
|
namespace PhpProfiler\Lib\Binary; |
13
|
|
|
|
14
|
|
|
use PhpProfiler\Lib\Process\MemoryMap\ProcessMemoryArea; |
15
|
|
|
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderInterface; |
16
|
|
|
|
17
|
|
|
class ProcessMemoryByteReader implements ByteReaderInterface |
18
|
|
|
{ |
19
|
|
|
use ByteReaderDisableWriteAccessTrait; |
20
|
|
|
|
21
|
|
|
private const PAGE_SIZE = 8192; |
22
|
|
|
|
23
|
|
|
private MemoryReaderInterface $memory_reader; |
24
|
|
|
|
25
|
|
|
/** @var CDataByteReader[] */ |
26
|
|
|
private array $pages = []; |
27
|
|
|
private int $pid; |
28
|
|
|
/** @var ProcessMemoryArea[] */ |
29
|
|
|
private array $memory_area; |
30
|
|
|
private bool $is_address = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ProcessMemoryByteReader constructor. |
34
|
|
|
* @param MemoryReaderInterface $memory_reader |
35
|
|
|
* @param int $pid |
36
|
|
|
* @param ProcessMemoryArea[] $memory_area |
37
|
|
|
*/ |
38
|
|
|
public function __construct(MemoryReaderInterface $memory_reader, int $pid, array $memory_area) |
39
|
|
|
{ |
40
|
|
|
$this->memory_reader = $memory_reader; |
41
|
|
|
$this->pid = $pid; |
42
|
|
|
$this->memory_area = $memory_area; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function offsetExists($offset): bool |
46
|
|
|
{ |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function useMemoryAddress(bool $is_address): void |
51
|
|
|
{ |
52
|
|
|
$this->is_address = $is_address; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getBegin(): int |
56
|
|
|
{ |
57
|
|
|
$begin = PHP_INT_MAX; |
58
|
|
|
foreach ($this->memory_area as $memory_area) { |
59
|
|
|
$begin = min($begin, hexdec($memory_area->begin)); |
60
|
|
|
} |
61
|
|
|
return $begin; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function offsetGet($offset): int |
65
|
|
|
{ |
66
|
|
|
if (!$this->is_address) { |
67
|
|
|
$offset = $this->getMemoryAddressFromOffset($offset); |
68
|
|
|
} |
69
|
|
|
$page = (int)floor($offset / self::PAGE_SIZE); |
70
|
|
|
if (!isset($this->pages[$page])) { |
71
|
|
|
$this->pages[$page] = new CDataByteReader( |
72
|
|
|
$this->memory_reader->read( |
73
|
|
|
$this->pid, |
74
|
|
|
max($this->getBegin(), $page * self::PAGE_SIZE), |
75
|
|
|
self::PAGE_SIZE |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
$diff = 0; |
80
|
|
|
if ($page * self::PAGE_SIZE < $this->getBegin()) { |
81
|
|
|
$diff = $this->getBegin() - $page * self::PAGE_SIZE; |
82
|
|
|
} |
83
|
|
|
return $this->pages[$page][($offset % self::PAGE_SIZE) - $diff]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getMemoryAddressFromOffset(int $offset): int |
87
|
|
|
{ |
88
|
|
|
$ranges = []; |
89
|
|
|
foreach ($this->memory_area as $memory_area) { |
90
|
|
|
$ranges[hexdec($memory_area->file_offset)] = hexdec($memory_area->begin); |
91
|
|
|
} |
92
|
|
|
ksort($ranges); |
93
|
|
|
$file_offset_decided = 0; |
94
|
|
|
foreach ($ranges as $file_offset => $memory_begin) { |
95
|
|
|
if ($file_offset <= $offset) { |
96
|
|
|
$file_offset_decided = $file_offset; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $ranges[$file_offset_decided] + ($offset - $file_offset_decided); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function createSliceAsString(int $offset, int $size): string |
103
|
|
|
{ |
104
|
|
|
$result = ''; |
105
|
|
|
for ($i = $offset; $i < ($offset + $size); $i++) { |
106
|
|
|
$result .= chr($this[$i]); |
107
|
|
|
} |
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|