1
# ***** BEGIN LICENSE BLOCK *****
2
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
# The contents of this file are subject to the Mozilla Public License Version
5
# 1.1 (the "License"); you may not use this file except in compliance with
6
# the License. You may obtain a copy of the License at
7
# http://www.mozilla.org/MPL/
9
# Software distributed under the License is distributed on an "AS IS" basis,
10
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
# for the specific language governing rights and limitations under the
14
# The Original Code is mozilla.org
16
# The Initial Developer of the Original Code is
17
# the Mozilla Foundation
18
# Portions created by the Initial Developer are Copyright (C) 2011
19
# the Initial Developer. All Rights Reserved.
22
# Mike Hommey <mh@glandium.org>
24
# Alternatively, the contents of this file may be used under the terms of
25
# either the GNU General Public License Version 2 or later (the "GPL"), or
26
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
# in which case the provisions of the GPL or the LGPL are applicable instead
28
# of those above. If you wish to allow use of your version of this file only
29
# under the terms of either the GPL or the LGPL, and not to allow others to
30
# use your version of this file under the terms of the MPL, indicate your
31
# decision by deleting the provisions above and replace them with the notice
32
# and other provisions required by the GPL or the LGPL. If you do not delete
33
# the provisions above, a recipient may use your version of this file under
34
# the terms of any one of the MPL, the GPL or the LGPL.
36
# ***** END LICENSE BLOCK *****
38
# This script returns the number of items for the DW_AT_ranges corresponding
39
# to a given compilation unit. This is used as a helper to find a bug in some
46
def get_range_for(compilation_unit, debug_info):
47
'''Returns the range offset for a given compilation unit
48
in a given debug_info.'''
51
for nfo in debug_info.splitlines():
52
if 'DW_TAG_compile_unit' in nfo:
54
elif 'DW_TAG_' in nfo or not nfo.strip():
55
if name == compilation_unit:
56
return int(ranges, 16)
60
if 'DW_AT_name' in nfo:
61
name = nfo.rsplit(None, 1)[1]
62
elif 'DW_AT_ranges' in nfo:
63
ranges = nfo.rsplit(None, 1)[1]
66
def get_range_length(range, debug_ranges):
67
'''Returns the number of items in the range starting at the
70
for line in debug_ranges.splitlines():
71
m = re.match('\s*([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)', line)
72
if m and int(m.group(1), 16) == range:
76
def main(bin, compilation_unit):
77
p = subprocess.Popen(['objdump', '-W', bin], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
78
(out, err) = p.communicate()
79
sections = re.split('\n(Contents of the|The section) ', out)
80
debug_info = [s for s in sections if s.startswith('.debug_info')]
81
debug_ranges = [s for s in sections if s.startswith('.debug_ranges')]
82
if not debug_ranges or not debug_info:
85
range = get_range_for(compilation_unit, debug_info[0])
87
return get_range_length(range, debug_ranges[0])
92
if __name__ == '__main__':
93
print main(*sys.argv[1:])