def get_source_file_path_references(location):
    """
    Yield unique references to source file paths extracted from DWARF debug symbols
    from the Elf file at `location`.

    If there are errors when processing Elfs, these are returned as well as paths
    prefixed with 'ERROR: '.
    """
    if not os.path.exists(location):
        return
    T = contenttype.get_type(location)
    if not T.is_elf:
        return
    unique_files = set()
    unique_paths = set()
    errors = []
    try:
        with_libdwarf = dwarf.Dwarf(location)
        for path in with_libdwarf.included_source_files:
            if '/' not in path:
                # bare file name
                unique_files.add(path)
            else:
                unique_paths.add(path)

        for path in with_libdwarf.original_source_files:
            if '/' not in path:
                # bare file name
                unique_files.add(path)
            else:
                unique_paths.add(path)

    except Exception as lde:
        msg = str(lde)
        _, m1, m2 = msg.partition('dwarfdump')
        errors.append(''.join([m1, m2]))

    try:
        with_binutils_nm = dwarf2.get_dwarfs(location)
        for entry in with_binutils_nm:
            path = entry.path
            if '/' not in path:
                # bare file name
                unique_files.add(path)
            else:
                unique_paths.add(path)
    except Exception as lde:
        msg = str(lde)
        errors.append(str)

    seen_file_names = set(file_name(p) for p in unique_paths)
    for fn in unique_files:
        if fn not in seen_file_names and fn not in ignores:
            unique_paths.add(fn)

    for error in errors:
        yield 'ERROR: ' + error

    for path in sorted(unique_paths):
        yield path
 def test_dwarf2_error_shash_x86_64(self):
     test_file = 'dwarf/shash.x86_64'
     test_loc = self.get_test_loc(test_file)
     emsg1 = 'File format is ambiguous'
     try:
         list(dwarf2.get_dwarfs(test_loc))
     except Exception, e:
         assert emsg1 in str(e)
 def test_dwarf2_error_amd64_exec(self):
     test_file = 'dwarf/amd64_exec'
     test_loc = self.get_test_loc(test_file)
     emsg1 = 'File format is ambiguous'
     try:
         list(dwarf2.get_dwarfs(test_loc))
     except Exception, e:
         assert emsg1 in str(e)
 def test_dwarf2_error_misc_elfs_cpp_test_o(self):
     test_file = 'misc_elfs/cpp-test.o'
     test_loc = self.get_test_loc(test_file)
     emsg1 = 'File format is ambiguous'
     try:
         list(dwarf2.get_dwarfs(test_loc))
     except Exception, e:
         assert emsg1 in str(e)
    def check_dwarf(self, test_file, expected_file, regen=False):
        test_loc = self.get_test_loc(test_file)
        result = [list(r) for r in dwarf2.get_dwarfs(test_loc)]

        expected_loc = FIXED_testcase.get_test_loc(expected_file,
                                                   self.test_data_dir)

        if regen:
            with open(expected_loc, 'wb') as exc:
                json.dump(result, exc, indent=2, encoding='utf-8')

        with open(expected_loc, 'rb') as exc:
            expected = json.load(exc,
                                 encoding='utf-8',
                                 object_pairs_hook=OrderedDict)

        assert sorted(expected) == sorted(result)
 def test_dwarf2_empty_on_non_existing_file(self):
     test_file = 'dwarf/32.fsize.chgg_DOES_NOT_EXIST'
     result = dwarf2.get_dwarfs(test_file)
     self.assertRaises(IOError, list, result)