| 1 |
from ptrace.os_tools import HAS_PROC |
| 2 |
from ptrace.debugger.process_error import ProcessError |
| 3 |
from ptrace.ctypes_tools import formatAddress |
| 4 |
import re |
| 5 |
from weakref import ref |
| 6 |
|
| 7 |
if HAS_PROC: |
| 8 |
from ptrace.linux_proc import openProc, ProcError |
| 9 |
|
| 10 |
PROC_MAP_REGEX = re.compile( |
| 11 |
# Address range: '08048000-080b0000 ' |
| 12 |
r'([0-9a-f]+)-([0-9a-f]+) ' |
| 13 |
# Permission: 'r-xp ' |
| 14 |
r'(.{4}) ' |
| 15 |
# Offset: '0804d000' |
| 16 |
r'([0-9a-f]+) ' |
| 17 |
# Device (major:minor): 'fe:01 ' |
| 18 |
r'([0-9a-f]{2,3}):([0-9a-f]{2,3}) ' |
| 19 |
# Inode: '3334030' |
| 20 |
r'([0-9]+)' |
| 21 |
# Filename: ' /usr/bin/synergyc' |
| 22 |
r'(?: +(.*))?') |
| 23 |
|
| 24 |
|
| 25 |
class MemoryMapping(object): |
| 26 |
""" |
| 27 |
Process memory mapping (metadata about the mapping). |
| 28 |
|
| 29 |
Attributes: |
| 30 |
- start (int): first byte address |
| 31 |
- end (int): last byte address + 1 |
| 32 |
- permissions (str) |
| 33 |
- offset (int): for file, offset in bytes from the file start |
| 34 |
- major_device / minor_device (int): major / minor device number |
| 35 |
- inode (int) |
| 36 |
- pathname (str) |
| 37 |
- _process: weak reference to the process |
| 38 |
|
| 39 |
Operations: |
| 40 |
- "address in mapping" checks the address is in the mapping. |
| 41 |
- "search(somestring)" returns the offsets of "somestring" in the mapping |
| 42 |
- "str(mapping)" create one string describing the mapping |
| 43 |
- "repr(mapping)" create a string representation of the mapping, |
| 44 |
useful in list contexts |
| 45 |
""" |
| 46 |
|
| 47 |
def __init__(self, process, start, end, permissions, offset, major_device, minor_device, inode, pathname): |
| 48 |
self._process = ref(process) |
| 49 |
self.start = start |
| 50 |
self.end = end |
| 51 |
self.permissions = permissions |
| 52 |
self.offset = offset |
| 53 |
self.major_device = major_device |
| 54 |
self.minor_device = minor_device |
| 55 |
self.inode = inode |
| 56 |
self.pathname = pathname |
| 57 |
|
| 58 |
def __contains__(self, address): |
| 59 |
return self.start <= address < self.end |
| 60 |
|
| 61 |
def __str__(self): |
| 62 |
text = "%s-%s" % (formatAddress(self.start), formatAddress(self.end)) |
| 63 |
if self.pathname: |
| 64 |
text += " => %s" % self.pathname |
| 65 |
text += " (%s)" % self.permissions |
| 66 |
return text |
| 67 |
__repr__ = __str__ |
| 68 |
|
| 69 |
def search(self, bytestr): |
| 70 |
process = self._process() |
| 71 |
|
| 72 |
bytestr_len = len(bytestr) |
| 73 |
buf_len = 64 * 1024 |
| 74 |
|
| 75 |
if buf_len < bytestr_len: |
| 76 |
buf_len = bytestr_len |
| 77 |
|
| 78 |
remaining = self.end - self.start |
| 79 |
covered = self.start |
| 80 |
|
| 81 |
while remaining >= bytestr_len: |
| 82 |
if remaining > buf_len: |
| 83 |
requested = buf_len |
| 84 |
else: |
| 85 |
requested = remaining |
| 86 |
|
| 87 |
data = process.readBytes(covered, requested) |
| 88 |
|
| 89 |
if data == "": |
| 90 |
break |
| 91 |
|
| 92 |
offset = data.find(bytestr) |
| 93 |
if (offset == -1): |
| 94 |
skip = requested - bytestr_len + 1 |
| 95 |
else: |
| 96 |
yield (covered + offset) |
| 97 |
skip = offset + bytestr_len |
| 98 |
|
| 99 |
covered += skip |
| 100 |
remaining -= skip |
| 101 |
|
| 102 |
|
| 103 |
def readProcessMappings(process): |
| 104 |
""" |
| 105 |
Read all memory mappings of the specified process. |
| 106 |
|
| 107 |
Return a list of MemoryMapping objects, or empty list if it's not possible |
| 108 |
to read the mappings. |
| 109 |
|
| 110 |
May raise a ProcessError. |
| 111 |
""" |
| 112 |
maps = [] |
| 113 |
if not HAS_PROC: |
| 114 |
return maps |
| 115 |
try: |
| 116 |
mapsfile = openProc("%s/maps" % process.pid) |
| 117 |
except ProcError as err: |
| 118 |
raise ProcessError(process, "Unable to read process maps: %s" % err) |
| 119 |
try: |
| 120 |
for line in mapsfile: |
| 121 |
line = line.rstrip() |
| 122 |
match = PROC_MAP_REGEX.match(line) |
| 123 |
if not match: |
| 124 |
raise ProcessError( |
| 125 |
process, "Unable to parse memory mapping: %r" % line) |
| 126 |
map = MemoryMapping( |
| 127 |
process, |
| 128 |
int(match.group(1), 16), |
| 129 |
int(match.group(2), 16), |
| 130 |
match.group(3), |
| 131 |
int(match.group(4), 16), |
| 132 |
int(match.group(5), 16), |
| 133 |
int(match.group(6), 16), |
| 134 |
int(match.group(7)), |
| 135 |
match.group(8)) |
| 136 |
maps.append(map) |
| 137 |
finally: |
| 138 |
mapsfile.close() |
| 139 |
return maps |