Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import re | |
| 4 import sys | |
| 5 | |
| 6 class ProcMapsEntry(object): | |
|
M-A Ruel
2012/07/19 15:21:15
A oneliner docstring would help here and in a few
Dai Mikurube (NOT FULLTIME)
2012/07/20 04:32:39
Done.
| |
| 7 def __init__( | |
| 8 self, begin, end, readable, writable, executable, private, offset, | |
| 9 major, minor, inode, name): | |
| 10 self.begin = begin | |
| 11 self.end = end | |
| 12 self.readable = readable | |
| 13 self.writable = writable | |
| 14 self.executable = executable | |
| 15 self.private = private | |
| 16 self.offset = offset | |
| 17 self.major = major | |
| 18 self.minor = minor | |
| 19 self.inode = inode | |
| 20 self.name = name | |
| 21 | |
| 22 | |
| 23 class ProcMaps(object): | |
| 24 | |
| 25 def __init__(self): | |
| 26 self._sorted_indexes = [] | |
| 27 self._dictionary = {} | |
| 28 self._sorted = True | |
| 29 | |
| 30 def append(self, entry): | |
| 31 if self._sorted_indexes and self._sorted_indexes[-1] > entry.begin: | |
| 32 self._sorted = False | |
| 33 self._sorted_indexes.append(entry.begin) | |
| 34 self._dictionary[entry.begin] = entry | |
| 35 | |
| 36 def iter(self, condition): | |
| 37 if not self._sorted: | |
| 38 self._sorted_indexes.sort() | |
| 39 self._sorted = True | |
| 40 for index in self._sorted_indexes: | |
| 41 if not condition or condition(self._dictionary[index]): | |
| 42 yield self._dictionary[index] | |
| 43 | |
| 44 def __iter__(self): | |
| 45 if not self._sorted: | |
| 46 self._sorted_indexes.sort() | |
| 47 self._sorted = True | |
| 48 for index in self._sorted_indexes: | |
| 49 yield self._dictionary[index] | |
| 50 | |
| 51 | |
| 52 _MAPS_PATTERN = re.compile( | |
|
M-A Ruel
2012/07/19 15:21:15
All constants in every files at top of the file
Dai Mikurube (NOT FULLTIME)
2012/07/20 04:32:39
Done.
| |
| 53 '^([a-f0-9]+)-([a-f0-9]+)\s+(.)(.)(.)(.)\s+([a-f0-9]+)\s+(\S+):(\S+)\s+' | |
| 54 '(\d+)\s+(\S+)$', re.IGNORECASE) | |
| 55 | |
| 56 def parse_proc_maps(FILE): | |
| 57 table = ProcMaps() | |
| 58 for line in FILE: | |
| 59 matched = _MAPS_PATTERN.match(line) | |
| 60 if matched: | |
| 61 table.append(ProcMapsEntry( | |
| 62 int(matched.group(1), 16), # begin | |
| 63 int(matched.group(2), 16), # end | |
| 64 matched.group(3), # readable | |
| 65 matched.group(4), # writable | |
| 66 matched.group(5), # executable | |
| 67 matched.group(6), # private | |
| 68 int(matched.group(7), 16), # offset | |
| 69 matched.group(8), # major | |
| 70 matched.group(9), # minor | |
| 71 int(matched.group(10), 10), # inode | |
| 72 matched.group(11) # name | |
| 73 )) | |
| 74 | |
| 75 return table | |
| 76 | |
| 77 | |
| 78 if __name__ == '__main__': | |
| 79 if len(sys.argv) < 2: | |
| 80 sys.stderr.write("""Usage: | |
| 81 %s /path/to/maps | |
| 82 """ % sys.argv[0]) | |
| 83 sys.exit(1) | |
| 84 | |
| 85 with open(sys.argv[1], mode='r') as FILE: | |
| 86 maps = parse_proc_maps(FILE) | |
| 87 | |
| 88 for entry in maps: | |
| 89 print "%016x-%016x +%06x %s" % ( | |
| 90 entry.begin, entry.end, entry.offset, entry.name) | |
| OLD | NEW |