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