File: test_dwarf_cu_and_die_cache.py

package info (click to toggle)
python-pyelftools 0.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,964 kB
  • sloc: python: 15,903; ansic: 298; asm: 86; makefile: 24; cpp: 18; sh: 4
file content (58 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download
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
#-------------------------------------------------------------------------------
# elftools tests
#
# Eli Bendersky (eliben@gmail.com), Milton Miller <miltonm@us.ibm.com>
# This code is in the public domain
#-------------------------------------------------------------------------------
import os
import unittest

from elftools.elf.elffile import ELFFile
from elftools.common.utils import bytes2str

class TestCacheLUTandDIEref(unittest.TestCase):
    def dprint(self, list):
        if False:
            self.oprint(list)

    def oprint(self, list):
        if False:
            print(list)

    def test_die_from_LUTentry(self):
        lines = ['']
        with open(os.path.join('test', 'testfiles_for_unittests',
                               'lambda.elf'), 'rb') as f:
            elffile = ELFFile(f)
            self.assertTrue(elffile.has_dwarf_info())

            dwarf = elffile.get_dwarf_info()
            pt = dwarf.get_pubnames()
            for (k, v) in pt.items():
                ndie = dwarf.get_DIE_from_lut_entry(v)
                self.dprint(ndie)
                if not 'DW_AT_type' in ndie.attributes:
                    continue
                if not 'DW_AT_name' in ndie.attributes:
                    continue
                name = bytes2str(ndie.attributes['DW_AT_name'].value)
                tlist = []
                tdie = ndie
                while True:
                    tdie = tdie.get_DIE_from_attribute('DW_AT_type')
                    self.dprint(ndie)
                    ttag = tdie.tag
                    if isinstance(ttag, int):
                        ttag = 'TAG(0x%x)' % ttag
                    tlist.append(ttag)
                    if 'DW_AT_name' in tdie.attributes:
                        break
                tlist.append(bytes2str(tdie.attributes['DW_AT_name'].value))
                tname = ' '.join(tlist)
                line = "%s DIE at %s is of type %s" % (
                        ndie.tag, ndie.offset, tname)
                lines.append(line)
                self.dprint(line)

        self.oprint('\n'.join(lines))
        self.assertGreater(len(lines), 1)