Home | History | Annotate | Download | only in check_public_api_headers
      1 """Test the integrity of the lldb public api directory containing SB*.h headers.
      2 
      3 There should be nothing unwanted there and a simpe main.cpp which includes SB*.h
      4 should compile and link with the LLDB framework."""
      5 
      6 import os, re, StringIO
      7 import unittest2
      8 from lldbtest import *
      9 import lldbutil
     10 
     11 class SBDirCheckerCase(TestBase):
     12 
     13     mydir = os.path.join("api", "check_public_api_headers")
     14 
     15     def setUp(self):
     16         TestBase.setUp(self)
     17         self.lib_dir = os.environ["LLDB_LIB_DIR"]
     18         self.template = 'main.cpp.template'
     19         self.source = 'main.cpp'
     20         self.exe_name = 'a.out'
     21 
     22     def test_sb_api_directory(self):
     23         """Test the SB API directory and make sure there's no unwanted stuff."""
     24 
     25         if self.getArchitecture() == "i386":
     26             self.skipTest("LLDB is 64-bit and cannot be linked to 32-bit test program.")
     27 
     28         # Generate main.cpp, build it, and execute.
     29         self.generate_main_cpp()
     30         self.buildDriver(self.source, self.exe_name)
     31         self.sanity_check_executable(self.exe_name)
     32 
     33     def generate_main_cpp(self):
     34         """Generate main.cpp from main.cpp.template."""
     35         temp = os.path.join(os.getcwd(), self.template)
     36         with open(temp, 'r') as f:
     37             content = f.read()
     38 
     39         public_api_dir = os.path.join(os.environ["LLDB_SRC"], "include", "lldb", "API")
     40 
     41         # Look under the include/lldb/API directory and add #include statements
     42         # for all the SB API headers.
     43         public_headers = os.listdir(public_api_dir)
     44         # For different platforms, the include statement can vary.
     45         if sys.platform.startswith("darwin"):
     46             include_stmt = "'#include <%s>' % os.path.join('LLDB', header)"
     47         if sys.platform.startswith('freebsd') or sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile':
     48             include_stmt = "'#include <%s>' % os.path.join(public_api_dir, header)"
     49         list = [eval(include_stmt) for header in public_headers if (header.startswith("SB") and
     50                                                                     header.endswith(".h"))]
     51         includes = '\n'.join(list)
     52         new_content = content.replace('%include_SB_APIs%', includes)
     53         src = os.path.join(os.getcwd(), self.source)
     54         with open(src, 'w') as f:
     55             f.write(new_content)
     56 
     57         # The main.cpp has been generated, add a teardown hook to remove it.
     58         self.addTearDownHook(lambda: os.remove(src))
     59 
     60     def sanity_check_executable(self, exe_name):
     61         """Sanity check executable compiled from the auto-generated program."""
     62         exe = os.path.join(os.getcwd(), exe_name)
     63         self.runCmd("file %s" % exe, CURRENT_EXECUTABLE_SET)
     64 
     65         self.line_to_break = line_number(self.source, '// Set breakpoint here.')
     66 
     67         env_cmd = "settings set target.env-vars %s=%s" %(self.dylibPath, self.getLLDBLibraryEnvVal())
     68         if self.TraceOn():
     69             print "Set environment to: ", env_cmd
     70         self.runCmd(env_cmd)
     71         self.addTearDownHook(lambda: self.runCmd("settings remove target.env-vars %s" % self.dylibPath))
     72 
     73         lldbutil.run_break_set_by_file_and_line (self, self.source, self.line_to_break, num_expected_locations = -1)
     74 
     75         self.runCmd("run", RUN_SUCCEEDED)
     76 
     77         # The stop reason of the thread should be breakpoint.
     78         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
     79             substrs = ['stopped',
     80                        'stop reason = breakpoint'])
     81 
     82         self.runCmd('frame variable')
     83 
     84 if __name__ == '__main__':
     85     import atexit
     86     lldb.SBDebugger.Initialize()
     87     atexit.register(lambda: lldb.SBDebugger.Terminate())
     88     unittest2.main()
     89