File: dwarfdump_test.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (182 lines) | stat: -rwxr-xr-x 6,263 bytes parent folder | download | duplicates (10)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

import dwarfdump


class DwarfDumpTest(unittest.TestCase):
  def _MakeRangeInfoList(self, flat_list):
    out = []
    for item in flat_list:
      assert len(item) == 3
      out.append((dwarfdump._AddressRange(item[0], item[1]), item[2]))
    return out

  def testParseNonContiguousAddressRange(self):
    """Test parsing DW_TAG_compile_unit with non-contiguous address range."""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("solution.cc")',
        'DW_AT_low_pc (0x0)',
        'DW_AT_ranges (0x1',
        '[0x10, 0x21)',
        '[0x31, 0x41))',
    ]
    expected_info_list = [(0x10, 0x21, 'solution.cc'),
                          (0x31, 0x41, 'solution.cc')]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testParseNonContiguousAddressRangeOtherBrackets(self):
    """Test parsing DW_AT_ranges when non-standard brackets are used."""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("solution.cc")',
        'DW_AT_low_pc (0x0)',
        'DW_AT_ranges [0x1',
        '(0x10, 0x21)',
        '[0x31, 0x41]]',
    ]
    expected_info_list = [(0x10, 0x21, 'solution.cc'),
                          (0x31, 0x41, 'solution.cc')]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testParseNonContiguousIgnoreEmptyRanges(self):
    """Test that empty ranges are ignored when parsing DW_AT_ranges."""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("solution.cc")',
        'DW_AT_ranges (0x1',
        '[0x1, 0x1)',
        '[0x10, 0x21)',
        '[0x22, 0x22)',
        '[0x31, 0x41)',
        '[0x42, 0x42))',
    ]
    expected_info_list = [(0x10, 0x21, 'solution.cc'),
                          (0x31, 0x41, 'solution.cc')]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testParseContiguousAddressRange(self):
    """Test parsing DW_TAG_compile_unit with contiguous address range."""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("solution.cc")',
        'DW_AT_low_pc (0x1)',
        'DW_AT_high_pc (0x10)',
    ]
    expected_info_list = [
        (0x1, 0x10, 'solution.cc'),
    ]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testParseSingleAddress(self):
    """Test parsing DW_TAG_compile_unit with single address."""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("solution.cc")',
        'DW_AT_low_pc (0x10)',
    ]
    expected_info_list = [
        (0x10, 0x11, 'solution.cc'),
    ]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testParseEmptyCompileUnit(self):
    """Test parsing empty DW_TAG_compile_unit."""
    lines = ['DW_TAG_compile_unit']
    self.assertEqual([], dwarfdump.ParseDumpOutputForTest(lines))

  def testConsecutiveCompileUnits(self):
    """Test parsing consecutive DW_TAG_compile_units."""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("foo.cc")',
        'DW_AT_low_pc (0x1)',
        'DW_AT_high_pc (0x10)',
        'DW_TAG_compile_unit',
        'DW_AT_name ("bar.cc")',
        'DW_AT_low_pc (0x12)',
        'DW_AT_high_pc (0x20)',
    ]
    expected_info_list = [(0x1, 0x10, 'foo.cc'), (0x12, 0x20, 'bar.cc')]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testTagTerminatedCompileUnit(self):
    """Test parsing DW_TAG_compile_unit where compile unit is followed by a
    non-DW_TAG_compile_unit entry.
    """
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name ("foo.cc")',
        'DW_AT_low_pc (0x1)',
        'DW_AT_high_pc (0x10)',
        'DW_TAG_subprogram',
        'DW_AT_name ("bar.cc")',
        'DW_AT_low_pc (0x12)',
        'DW_AT_high_pc (0x20)',
    ]
    expected_info_list = [
        (0x1, 0x10, 'foo.cc'),
    ]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testHandlePrefixes(self):
    """Test parsing DW_TAG_compile_unit where 'DW_' does not start line in
    DW_TAG_compile_unit entry.
    """
    lines = [
        '0x1 DW_TAG_compile_unit',
        '    DW_AT_language (DW_LANG_C_plus_plus_14)',
        '    DW_AT_name     ("solution.cc")',
        '    DW_AT_stmt_list (0x5)',
        '    DW_AT_low_pc   (0x1)',
        '    DW_AT_high_pc  (0x10)',
    ]
    expected_info_list = [
        (0x1, 0x10, 'solution.cc'),
    ]
    self.assertEqual(self._MakeRangeInfoList(expected_info_list),
                     dwarfdump.ParseDumpOutputForTest(lines))

  def testFindAddress(self):
    """Tests for _SourceMapper.FindSourceForTextAddress()"""
    lines = [
        'DW_TAG_compile_unit',
        'DW_AT_name     ("foo.cc")',
        'DW_AT_low_pc   (0x1)',
        'DW_AT_high_pc  (0x10)',
        'DW_TAG_compile_unit',
        'DW_AT_name     ("bar.cc")',
        'DW_AT_low_pc   (0x21)',
        'DW_AT_high_pc  (0x30)',
        'DW_TAG_compile_unit',
        'DW_AT_name     ("baz.cc")',
        'DW_AT_low_pc   (0x41)',
        'DW_AT_high_pc  (0x50)',
    ]
    source_mapper = dwarfdump.CreateAddressSourceMapperForTest(lines)
    # Address is before first range.
    self.assertEqual('', source_mapper.FindSourceForTextAddress(0x0))
    # Address matches start of first range.
    self.assertEqual('foo.cc', source_mapper.FindSourceForTextAddress(0x1))
    # Address is in the middle of middle range.
    self.assertEqual('bar.cc', source_mapper.FindSourceForTextAddress(0x2a))
    # Address matches end of last range.
    self.assertEqual('baz.cc', source_mapper.FindSourceForTextAddress(0x4f))
    # Address is after lange range.
    self.assertEqual('', source_mapper.FindSourceForTextAddress(0x50))


if __name__ == '__main__':
  unittest.main()