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+'
47 r'private_unevictable=(\d+) private=(\d+) '
49 r'shared_other_unevictable=(\d+) shared_other=(\d+) '
51 r'\[([a-zA-Z0-9+/=-_:]*)\]$')
52 map_re = re.compile(RE)
53 skip_first_n_lines = 1
54 maps = memory_map.Map()
57 line = line.rstrip('\r\n')
59 if skip_first_n_lines > 0:
60 skip_first_n_lines -= 1
63 m = map_re.match(line)
65 logging.warning('Skipping unrecognized memdump line "%s"' % line)
68 start = int(m.group(1), 16)
69 end = int(m.group(2), 16) - 1 # end addr is inclusive in memdump output.
71 # Sadly, this actually happened. Probably a kernel bug, see b/17402069.
72 logging.warning('Skipping unfeasible mmap "%s"' % line)
74 entry = memory_map.MapEntry(
77 prot_flags=m.group(3),
78 mapped_file=m.group(10),
79 mapped_offset=int(m.group(4), 16))
80 entry.priv_dirty_bytes = int(m.group(5))
81 entry.priv_clean_bytes = int(m.group(6)) - entry.priv_dirty_bytes
82 entry.shared_dirty_bytes = int(m.group(8))
83 entry.shared_clean_bytes = int(m.group(9)) - entry.shared_dirty_bytes
84 entry.resident_pages = [ord(c) for c in base64.b64decode(m.group(11))]