xref: /llvm-project/lldb/test/API/python_api/target-arch-from-module/TestTargetArchFromModule.py (revision ebf249066ac5c7917064eb56a9e51c21000cdf93)
1"""
2An SBTarget with no arch, call AddModule, SBTarget's arch should be set.
3"""
4
5import os
6import subprocess
7import re
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class TargetArchFromModule(TestBase):
15    @skipIf(
16        debug_info=no_match(["dsym"]),
17        bugnumber="This test is looking explicitly for a dSYM",
18    )
19    @skipUnlessDarwin
20    @skipIfRemote
21    def test_target_arch_init(self):
22        self.build()
23        aout_exe = self.getBuildArtifact("a.out")
24        aout_dsym = self.getBuildArtifact("a.out.dSYM")
25        hidden_dir = self.getBuildArtifact("hide.noindex")
26        hidden_aout_exe = self.getBuildArtifact("hide.noindex/a.out")
27        hidden_aout_dsym = self.getBuildArtifact("hide.noindex/a.out.dSYM")
28        dsym_for_uuid = self.getBuildArtifact("dsym-for-uuid.sh")
29
30        # We can hook in our dsym-for-uuid shell script to lldb with
31        # this env var instead of requiring a defaults write.
32        os.environ["LLDB_APPLE_DSYMFORUUID_EXECUTABLE"] = dsym_for_uuid
33        self.addTearDownHook(
34            lambda: os.environ.pop("LLDB_APPLE_DSYMFORUUID_EXECUTABLE", None)
35        )
36
37        dwarfdump_uuid_regex = re.compile("UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*")
38        dwarfdump_cmd_output = subprocess.check_output(
39            ('/usr/bin/dwarfdump --uuid "%s"' % aout_exe), shell=True
40        ).decode("utf-8")
41        aout_uuid = None
42        for line in dwarfdump_cmd_output.splitlines():
43            match = dwarfdump_uuid_regex.search(line)
44            if match:
45                aout_uuid = match.group(1)
46        self.assertNotEqual(aout_uuid, None, "Could not get uuid of built a.out")
47
48        ###  Create our dsym-for-uuid shell script which returns self.hidden_aout_exe.
49        shell_cmds = [
50            "#! /bin/sh",
51            "# the last argument is the uuid",
52            "while [ $# -gt 1 ]",
53            "do",
54            "  shift",
55            "done",
56            "ret=0",
57            'echo "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>"',
58            'echo "<!DOCTYPE plist PUBLIC \\"-//Apple//DTD PLIST 1.0//EN\\" \\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\\">"',
59            'echo "<plist version=\\"1.0\\">"',
60            "",
61            'if [ "$1" = "%s" ]' % aout_uuid,
62            "then",
63            "  uuid=%s" % aout_uuid,
64            "  bin=%s" % hidden_aout_exe,
65            "  dsym=%s.dSYM/Contents/Resources/DWARF/%s"
66            % (hidden_aout_exe, os.path.basename(hidden_aout_exe)),
67            "fi",
68            'echo "  <dict>"',
69            'echo "    <key>$1</key>"',
70            'echo "    <dict>"',
71            'if [ -z "$uuid" -o -z "$bin" -o ! -f "$bin" ]',
72            "then",
73            '  echo "      <key>DBGError</key>"',
74            '  echo "      <string>not found by $0</string>"',
75            '  echo "    </dict>"',
76            '  echo "  </dict>"',
77            '  echo "</plist>"',
78            "  exit 0",
79            "fi",
80            "",
81            'echo "<key>DBGDSYMPath</key><string>$dsym</string>"',
82            'echo "<key>DBGSymbolRichExecutable</key><string>$bin</string>"',
83            'echo "</dict></dict></plist>"',
84            "exit $ret",
85        ]
86
87        with open(dsym_for_uuid, "w") as writer:
88            for l in shell_cmds:
89                writer.write(l + "\n")
90
91        os.chmod(dsym_for_uuid, 0o755)
92
93        # Move the main binary and its dSYM into the hide.noindex
94        # directory.  Now the only way lldb can find them is with
95        # the LLDB_APPLE_DSYMFORUUID_EXECUTABLE shell script -
96        # so we're testing that this dSYM discovery method works.
97        lldbutil.mkdir_p(hidden_dir)
98        os.rename(aout_exe, hidden_aout_exe)
99        os.rename(aout_dsym, hidden_aout_dsym)
100
101        target = self.dbg.CreateTarget("")
102        self.assertTrue(target.IsValid())
103        self.expect("target list", matching=False, substrs=["arch="])
104
105        m = target.AddModule(None, None, aout_uuid)
106        self.assertTrue(m.IsValid())
107        self.expect("target list", substrs=["arch="])
108