Home | History | Annotate | Download | only in debug_info_test
      1 # Copyright 2018 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import os
      6 import subprocess
      7 
      8 import check_ngcc
      9 
     10 cu_checks = [check_ngcc.not_by_gcc]
     11 
     12 def check_compile_unit(dso_path, producer, comp_path):
     13     """check all compiler flags used to build the compile unit.
     14 
     15     Args:
     16         dso_path: path to the elf/dso
     17         producer: DW_AT_producer contains the compiler command line.
     18         comp_path: DW_AT_comp_dir + DW_AT_name
     19 
     20     Returns:
     21         A set of failed tests.
     22     """
     23     failed = set()
     24     for c in cu_checks:
     25         if not c(dso_path, producer, comp_path):
     26             failed.add(c.__module__)
     27 
     28     return failed
     29 
     30 def check_compile_units(dso_path):
     31     """check all compile units in the given dso.
     32 
     33     Args:
     34         dso_path: path to the dso
     35     Return:
     36         True if everything looks fine otherwise False.
     37     """
     38 
     39     failed = set()
     40     producer = ''
     41     comp_path = ''
     42 
     43     readelf = subprocess.Popen(['readelf', '--debug-dump=info',
     44                                 '--dwarf-depth=1', dso_path],
     45                                 stdout=subprocess.PIPE,
     46                                 stderr=open(os.devnull, 'w'))
     47     for l in readelf.stdout:
     48         if 'DW_TAG_compile_unit' in l:
     49             if producer:
     50                 failed = failed.union(check_compile_unit(dso_path, producer,
     51                                                          comp_path))
     52             producer = ''
     53             comp_path = ''
     54         elif 'DW_AT_producer' in l:
     55             producer = l
     56         elif 'DW_AT_name' in l:
     57             comp_path = os.path.join(comp_path, l.split(':')[-1].strip())
     58         elif 'DW_AT_comp_dir' in l:
     59             comp_path = os.path.join(l.split(':')[-1].strip(), comp_path)
     60     if producer:
     61         failed = failed.union(check_compile_unit(dso_path, producer, comp_path))
     62 
     63     if failed:
     64         print('%s failed check: %s' % (dso_path, ' '.join(failed)))
     65         return False
     66 
     67     return True
     68