1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """This parser turns the am memdump output into a |memory_map.Map| instance."""
11 from memory_inspector.core import memory_map
15 """Parses the output of memdump.
17 memdump (see chrome/src/tools/memdump) is a Linux/Android binary meant to be
18 executed on the target device which extracts memory map information about one
19 or more processes. In principle is can be seen as an alternative to cat-ing
20 /proc/PID/smaps, but with extra features (multiprocess accounting and resident
23 The expected memdump output looks like this:
24 ------------------------------------------------------------------------------
26 1000-2000 r-xp 0 private_unevictable=4096 private=8192 shared_app=[] \
27 shared_other_unevictable=4096 shared_other=4096 "/lib/foo.so" [v///fv0D]
28 ... other entries like the one above.
29 ------------------------------------------------------------------------------
30 The output is extremely similar to /proc/PID/smaps, with the following notes:
31 - unevictable has pretty much the same meaning of "dirty", in VM terms.
32 - private and shared_other are cumulative. This means the the "clean" part
33 must be calculated as difference of (private - private_unevictable).
34 - The final field [v///fv0D] is a base64 encoded bitmap which contains the
35 information about which pages inside the mapping are resident (present).
36 See tests/android_backend_test.py for a more complete example.
39 lines: array of strings containing memdump output.
42 An instance of |memory_map.Map|.
44 RE = (r'^([0-9a-f]+)-([0-9a-f]+)\s+'
45 r'([rwxps-]{4})\s*.*?'
46 r'private_unevictable=(\d+) private=(\d+) '
48 r'shared_other_unevictable=(\d+) shared_other=(\d+) '
50 r'\[([a-zA-Z0-9+/=-_:]*)\]$')
51 map_re = re.compile(RE)
52 skip_first_n_lines = 1
53 maps = memory_map.Map()
56 line = line.rstrip('\r\n')
58 if skip_first_n_lines > 0:
59 skip_first_n_lines -= 1
62 m = map_re.match(line)
64 logging.warning('Skipping unrecognized memdump line "%s"' % line)
67 # TODO(primiano): proper offset handling requires a change in memdump. In
68 # the meanwhile, it should pretty safe assuming zero-offset for libs (for
69 # symbolization). Also, offsets for other mappings don't really matter.
70 entry = memory_map.MapEntry(
71 start=int(m.group(1), 16),
72 end=int(m.group(2), 16) - 1, # end addr is inclusive in memdump output.
73 prot_flags=m.group(3),
74 mapped_file=m.group(9),
76 entry.priv_dirty_bytes = int(m.group(4))
77 entry.priv_clean_bytes = int(m.group(5)) - entry.priv_dirty_bytes
78 entry.shared_dirty_bytes = int(m.group(7))
79 entry.shared_clean_bytes = int(m.group(8)) - entry.shared_dirty_bytes
80 entry.resident_pages = [ord(c) for c in base64.b64decode(m.group(10))]