1import lldb
2from lldbsuite.test.decorators import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5
6
7@skipUnlessDarwin
8class AddDsymDownload(TestBase):
9
10    mydir = TestBase.compute_mydir(__file__)
11    dwarfdump_uuid_regex = re.compile('UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*')
12
13    def get_uuid(self):
14        dwarfdump_cmd_output = subprocess.check_output(
15            ('/usr/bin/dwarfdump --uuid "%s"' % self.exe),
16            shell=True).decode("utf-8")
17        for line in dwarfdump_cmd_output.splitlines():
18            match = self.dwarfdump_uuid_regex.search(line)
19            if match:
20                return match.group(1)
21        return None
22
23    def create_dsym_for_uuid(self):
24        shell_cmds = [
25            '#! /bin/sh', '# the last argument is the uuid',
26            'while [ $# -gt 1 ]', 'do', '  shift', 'done', 'ret=0',
27            'echo "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>"',
28            'echo "<!DOCTYPE plist PUBLIC \\"-//Apple//DTD PLIST 1.0//EN\\" \\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\\">"',
29            'echo "<plist version=\\"1.0\\">"', '',
30            'if [ "$1" != "%s" ]' % (self.uuid), 'then',
31            '  echo "<key>DBGError</key><string>not found</string>"',
32            '  echo "</plist>"', '  exit 1', 'fi',
33            '  uuid=%s' % self.uuid,
34            '  bin=%s' % self.exe,
35            '  dsym=%s' % self.dsym, 'echo "<dict><key>$uuid</key><dict>"', '',
36            'echo "<key>DBGDSYMPath</key><string>$dsym</string>"',
37            'echo "<key>DBGSymbolRichExecutable</key><string>$bin</string>"',
38            'echo "</dict></dict></plist>"', 'exit $ret'
39        ]
40
41        with open(self.dsym_for_uuid, "w") as writer:
42            for l in shell_cmds:
43                writer.write(l + '\n')
44
45        os.chmod(self.dsym_for_uuid, 0o755)
46
47    def setUp(self):
48        TestBase.setUp(self)
49        self.source = 'main.c'
50        self.exe = self.getBuildArtifact("a.out")
51        self.dsym = os.path.join(
52            self.getBuildDir(),
53            "hide.app/Contents/a.out.dSYM/Contents/Resources/DWARF/",
54            os.path.basename(self.exe))
55        self.dsym_for_uuid = self.getBuildArtifact("dsym-for-uuid.sh")
56
57        self.build(debug_info="dsym")
58        self.assertTrue(os.path.exists(self.exe))
59        self.assertTrue(os.path.exists(self.dsym))
60
61        self.uuid = self.get_uuid()
62        self.assertNotEqual(self.uuid, None, "Could not get uuid for a.out")
63
64        self.create_dsym_for_uuid()
65
66        os.environ['LLDB_APPLE_DSYMFORUUID_EXECUTABLE'] = self.dsym_for_uuid
67        self.addTearDownHook(
68            lambda: os.environ.pop('LLDB_APPLE_DSYMFORUUID_EXECUTABLE', None))
69
70    def do_test(self, command):
71        self.target = self.dbg.CreateTarget(self.exe)
72        self.assertTrue(self.target, VALID_TARGET)
73
74        main_bp = self.target.BreakpointCreateByName("main", "a.out")
75        self.assertTrue(main_bp, VALID_BREAKPOINT)
76
77        self.process = self.target.LaunchSimple(
78            None, None, self.get_process_working_directory())
79        self.assertTrue(self.process, PROCESS_IS_VALID)
80
81        # The stop reason of the thread should be breakpoint.
82        self.assertEquals(self.process.GetState(), lldb.eStateStopped,
83                          STOPPED_DUE_TO_BREAKPOINT)
84
85        self.runCmd(command)
86        self.expect("frame select", substrs=['a.out`main at main.c'])
87
88    @no_debug_info_test
89    def test_frame(self):
90        self.do_test("add-dsym --frame")
91
92    @no_debug_info_test
93    def test_uuid(self):
94        self.do_test("add-dsym --uuid {}".format(self.uuid))
95
96    @no_debug_info_test
97    def test_stack(self):
98        self.do_test("add-dsym --stack")
99