summaryrefslogtreecommitdiffstats
path: root/tools/find_runtime_symbols/reduce_debugline.py
blob: 75c8c8578d76c0d8e223f1409694ec7278b73b7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Reduces result of 'readelf -wL' to just a list of starting addresses.

It lists up all addresses where the corresponding source files change.  The
list is sorted in ascending order.  See tests/reduce_debugline_test.py for
examples.

This script assumes that the result of 'readelf -wL' ends with an empty line.

Note: the option '-wL' has the same meaning with '--debug-dump=decodedline'.
"""

import re
import sys


_FILENAME_PATTERN = re.compile('(CU: |)(.+)\:')


def reduce_decoded_debugline(input_file):
  filename = ''
  starting_dict = {}
  started = False

  for line in input_file:
    line = line.strip()
    unpacked = line.split(None, 2)

    if len(unpacked) == 3 and unpacked[2].startswith('0x'):
      if not started and filename:
        started = True
        starting_dict[int(unpacked[2], 16)] = filename
    else:
      started = False
      if line.endswith(':'):
        matched = _FILENAME_PATTERN.match(line)
        if matched:
          filename = matched.group(2)

  starting_list = []
  prev_filename = ''
  for address in sorted(starting_dict):
    curr_filename = starting_dict[address]
    if prev_filename != curr_filename:
      starting_list.append((address, starting_dict[address]))
    prev_filename = curr_filename
  return starting_list


def main():
  if len(sys.argv) != 1:
    print >> sys.stderr, 'Unsupported arguments'
    return 1

  starting_list = reduce_decoded_debugline(sys.stdin)
  bits64 = starting_list[-1][0] > 0xffffffff
  for address, filename in starting_list:
    if bits64:
      print '%016x %s' % (address, filename)
    else:
      print '%08x %s' % (address, filename)


if __name__ == '__main__':
  sys.exit(main())