1#!/usr/bin/env python
2# Copyright (c) 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Reduces result of 'readelf -wL' to just a list of starting addresses.
6
7It lists up all addresses where the corresponding source files change.  The
8list is sorted in ascending order.  See tests/reduce_debugline_test.py for
9examples.
10
11This script assumes that the result of 'readelf -wL' ends with an empty line.
12
13Note: the option '-wL' has the same meaning with '--debug-dump=decodedline'.
14"""
15
16import re
17import sys
18
19
20_FILENAME_PATTERN = re.compile('(CU: |)(.+)\:')
21
22
23def reduce_decoded_debugline(input_file):
24  filename = ''
25  starting_dict = {}
26  started = False
27
28  for line in input_file:
29    line = line.strip()
30    unpacked = line.split(None, 2)
31
32    if len(unpacked) == 3 and unpacked[2].startswith('0x'):
33      if not started and filename:
34        started = True
35        starting_dict[int(unpacked[2], 16)] = filename
36    else:
37      started = False
38      if line.endswith(':'):
39        matched = _FILENAME_PATTERN.match(line)
40        if matched:
41          filename = matched.group(2)
42
43  starting_list = []
44  prev_filename = ''
45  for address in sorted(starting_dict):
46    curr_filename = starting_dict[address]
47    if prev_filename != curr_filename:
48      starting_list.append((address, starting_dict[address]))
49    prev_filename = curr_filename
50  return starting_list
51
52
53def main():
54  if len(sys.argv) != 1:
55    print >> sys.stderr, 'Unsupported arguments'
56    return 1
57
58  starting_list = reduce_decoded_debugline(sys.stdin)
59  bits64 = starting_list[-1][0] > 0xffffffff
60  for address, filename in starting_list:
61    if bits64:
62      print '%016x %s' % (address, filename)
63    else:
64      print '%08x %s' % (address, filename)
65
66
67if __name__ == '__main__':
68  sys.exit(main())
69