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