| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 """Common utilities for tools that deal with binary size information. | |
| 6 """ | |
| 7 | |
| 8 import logging | |
| 9 import re | |
| 10 | |
| 11 | |
| 12 def ParseNm(nm_lines): | |
| 13 """Parse nm output, returning data for all relevant (to binary size) | |
| 14 symbols and ignoring the rest. | |
| 15 | |
| 16 Args: | |
| 17 nm_lines: an iterable over lines of nm output. | |
| 18 | |
| 19 Yields: | |
| 20 (symbol name, symbol type, symbol size, source file path). | |
| 21 | |
| 22 Path may be None if nm couldn't figure out the source file. | |
| 23 """ | |
| 24 | |
| 25 # Match lines with size, symbol, optional location, optional discriminator | |
| 26 sym_re = re.compile(r'^([0-9a-f]{8,}) ' # address (8+ hex digits) | |
| 27 '([0-9a-f]{8,}) ' # size (8+ hex digits) | |
| 28 '(.) ' # symbol type, one character | |
| 29 '([^\t]+)' # symbol name, separated from next by tab | |
| 30 '(?:\t(.*):[\d\?]+)?.*$') # location | |
| 31 # Match lines with addr but no size. | |
| 32 addr_re = re.compile(r'^[0-9a-f]{8,} (.) ([^\t]+)(?:\t.*)?$') | |
| 33 # Match lines that don't have an address at all -- typically external symbols. | |
| 34 noaddr_re = re.compile(r'^ {8,} (.) (.*)$') | |
| 35 # Match lines with no symbol name, only addr and type | |
| 36 addr_only_re = re.compile(r'^[0-9a-f]{8,} (.)$') | |
| 37 | |
| 38 seen_lines = set() | |
| 39 for line in nm_lines: | |
| 40 line = line.rstrip() | |
| 41 if line in seen_lines: | |
| 42 # nm outputs identical lines at times. We don't want to treat | |
| 43 # those as distinct symbols because that would make no sense. | |
| 44 continue | |
| 45 seen_lines.add(line) | |
| 46 match = sym_re.match(line) | |
| 47 if match: | |
| 48 address, size, sym_type, sym = match.groups()[0:4] | |
| 49 size = int(size, 16) | |
| 50 if sym_type in ('B', 'b'): | |
| 51 continue # skip all BSS for now. | |
| 52 path = match.group(5) | |
| 53 yield sym, sym_type, size, path, address | |
| 54 continue | |
| 55 match = addr_re.match(line) | |
| 56 if match: | |
| 57 # sym_type, sym = match.groups()[0:2] | |
| 58 continue # No size == we don't care. | |
| 59 match = noaddr_re.match(line) | |
| 60 if match: | |
| 61 sym_type, sym = match.groups() | |
| 62 if sym_type in ('U', 'w'): | |
| 63 continue # external or weak symbol | |
| 64 match = addr_only_re.match(line) | |
| 65 if match: | |
| 66 continue # Nothing to do. | |
| 67 | |
| 68 | |
| 69 # If we reach this part of the loop, there was something in the | |
| 70 # line that we didn't expect or recognize. | |
| 71 logging.warning('nm output parser failed to parse: %s', repr(line)) | |
| OLD | NEW |