1"""
2Test that clang produces the __apple accelerator tables, for example, __apple_types, correctly.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9from lldbutil import symbol_type_to_str
10
11class AppleTypesTestCase(TestBase):
12
13    mydir = os.path.join("macosx", "debug-info", "apple_types")
14
15    #rdar://problem/11166975
16    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
17    def test_debug_info_for_apple_types(self):
18        """Test that __apple_types section does get produced by clang."""
19
20        if not self.getCompiler().endswith('clang'):
21            self.skipTest("clang compiler only test")
22
23        self.buildDefault()
24        self.apple_types(dot_o=True)
25
26    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
27    def test_debug_info_for_apple_types_dsym(self):
28        """Test that __apple_types section does get produced by dsymutil.
29           This is supposed to succeed even with rdar://problem/11166975."""
30
31        if not self.getCompiler().endswith('clang'):
32            self.skipTest("clang compiler only test")
33
34        self.buildDsym()
35        self.apple_types(dot_o=False)
36
37    def apple_types(self, dot_o):
38        """Test that __apple_types section does get produced by clang."""
39        if dot_o:
40            exe = os.path.join(os.getcwd(), "main.o")
41        else:
42            exe = os.path.join(os.getcwd(), "a.out.dSYM/Contents/Resources/DWARF/a.out")
43
44        target = self.dbg.CreateTarget(exe)
45        self.assertTrue(target, VALID_TARGET)
46        self.assertTrue(target.GetNumModules() > 0)
47
48        # Hide stdout if not running with '-t' option.
49        if not self.TraceOn():
50            self.HideStdout()
51
52        print "Number of modules for the target: %d" % target.GetNumModules()
53        for module in target.module_iter():
54            print module
55
56        # Get the executable module at index 0.
57        exe_module = target.GetModuleAtIndex(0)
58
59        dwarf_section = exe_module.FindSection("__DWARF")
60        self.assertTrue(dwarf_section)
61        print "__DWARF section:", dwarf_section
62        print "Number of sub-sections: %d" % dwarf_section.GetNumSubSections()
63        INDENT = ' ' * 4
64        for subsec in dwarf_section:
65            print INDENT + str(subsec)
66
67        debug_str_sub_section = dwarf_section.FindSubSection("__debug_str")
68        self.assertTrue(debug_str_sub_section)
69        print "__debug_str sub-section:", debug_str_sub_section
70
71        # Find our __apple_types section by name.
72        apple_types_sub_section = dwarf_section.FindSubSection("__apple_types")
73        self.assertTrue(apple_types_sub_section)
74        print "__apple_types sub-section:", apple_types_sub_section
75
76        # These other three all important subsections should also be present.
77        self.assertTrue(dwarf_section.FindSubSection("__apple_names") and
78                        dwarf_section.FindSubSection("__apple_namespac") and
79                        dwarf_section.FindSubSection("__apple_objc"))
80
81
82if __name__ == '__main__':
83    import atexit
84    lldb.SBDebugger.Initialize()
85    atexit.register(lambda: lldb.SBDebugger.Terminate())
86    unittest2.main()
87