aboutsummaryrefslogtreecommitdiff
path: root/lldb/test/API/macosx/lc-note/multiple-binary-corefile/TestMultipleBinaryCorefile.py
blob: 0a0bc68646e626ebf72ff0ef151490df2bed4a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Test corefiles with "main bin spec"/"load binary" with only vmaddrs works."""


import os
import re
import subprocess

import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class TestMultipleBinaryCorefile(TestBase):
    def initial_setup(self):
        self.build()
        self.aout_exe_basename = "a.out"
        self.libone_exe_basename = "libone.dylib"
        self.libtwo_exe_basename = "libtwo.dylib"
        self.aout_exe = self.getBuildArtifact(self.aout_exe_basename)
        self.aout_slide = 0x5000
        self.libone_exe = self.getBuildArtifact(self.libone_exe_basename)
        self.libone_slide = 0x100840000
        self.libtwo_exe = self.getBuildArtifact(self.libtwo_exe_basename)
        self.libtwo_slide = 0
        self.corefile = self.getBuildArtifact("multiple-binaries.core")
        self.create_corefile = self.getBuildArtifact("create-multibin-corefile")
        cmd = "%s %s %s@%x %s@%x %s@%x" % (
            self.create_corefile,
            self.corefile,
            self.aout_exe,
            self.aout_slide,
            self.libone_exe,
            self.libone_slide,
            self.libtwo_exe,
            self.libtwo_slide,
        )
        if self.TraceOn():
            print("Creating corefile with command %s")
        call(cmd, shell=True)

    def load_corefile_and_test(self):
        target = self.dbg.CreateTarget("")
        err = lldb.SBError()
        if self.TraceOn():
            print("loading corefile %s" % self.corefile)
        process = target.LoadCore(self.corefile)
        self.assertEqual(process.IsValid(), True)
        if self.TraceOn():
            print("image list after loading corefile:")
            self.runCmd("image list")

        ## We don't have libone.dylib in the global module cache or from
        ## dsymForUUID, and lldb will not read the binary out of memory.
        self.assertEqual(target.GetNumModules(), 2)
        fspec = target.GetModuleAtIndex(0).GetFileSpec()
        self.assertEqual(fspec.GetFilename(), self.aout_exe_basename)

        fspec = target.GetModuleAtIndex(1).GetFileSpec()
        self.assertEqual(fspec.GetFilename(), self.libtwo_exe_basename)

        # Executables "always" have this base address
        aout_load = (
            target.GetModuleAtIndex(0)
            .GetObjectFileHeaderAddress()
            .GetLoadAddress(target)
        )
        self.assertEqual(aout_load, 0x100000000 + self.aout_slide)

        # Value from Makefile
        libtwo_load = (
            target.GetModuleAtIndex(1)
            .GetObjectFileHeaderAddress()
            .GetLoadAddress(target)
        )
        self.assertEqual(libtwo_load, self.libtwo_slide)

        self.dbg.DeleteTarget(target)
        self.dbg.Clear()

    NO_DEBUG_INFO_TESTCASE = True

    @skipIf(archs=no_match(["x86_64", "arm64", "arm64e", "aarch64"]))
    @skipIfRemote
    @skipUnlessDarwin
    def test_corefile_binaries_dsymforuuid(self):
        self.initial_setup()

        if self.TraceOn():
            self.runCmd("log enable lldb dyld host")
            self.addTearDownHook(lambda: self.runCmd("log disable lldb dyld host"))

        ## We can hook in our dsym-for-uuid shell script to lldb with this env
        ## var instead of requiring a defaults write.
        dsym_for_uuid = self.getBuildArtifact("dsym-for-uuid.sh")
        os.environ["LLDB_APPLE_DSYMFORUUID_EXECUTABLE"] = dsym_for_uuid
        if self.TraceOn():
            print("Setting env var LLDB_APPLE_DSYMFORUUID_EXECUTABLE=" + dsym_for_uuid)
        self.addTearDownHook(
            lambda: os.environ.pop("LLDB_APPLE_DSYMFORUUID_EXECUTABLE", None)
        )

        self.runCmd("settings set target.load-script-from-symbol-file true")
        self.addTearDownHook(
            lambda: self.runCmd(
                "settings set target.load-script-from-symbol-file false"
            )
        )

        dwarfdump_uuid_regex = re.compile("UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*")
        dwarfdump_cmd_output = subprocess.check_output(
            ('/usr/bin/dwarfdump --uuid "%s"' % self.libtwo_exe), shell=True
        ).decode("utf-8")
        libtwo_uuid = None
        for line in dwarfdump_cmd_output.splitlines():
            match = dwarfdump_uuid_regex.search(line)
            if match:
                libtwo_uuid = match.group(1)
        self.assertNotEqual(
            libtwo_uuid, None, "Could not get uuid of built libtwo.dylib"
        )
        dwarfdump_cmd_output = subprocess.check_output(
            ('/usr/bin/dwarfdump --uuid "%s"' % self.aout_exe), shell=True
        ).decode("utf-8")
        aout_uuid = None
        for line in dwarfdump_cmd_output.splitlines():
            match = dwarfdump_uuid_regex.search(line)
            if match:
                aout_uuid = match.group(1)
        self.assertNotEqual(aout_uuid, None, "Could not get uuid of built a.out")

        ###  Create our dsym-for-uuid shell script which returns aout_exe
        shell_cmds = [
            "#! /bin/sh",
            "# the last argument is the uuid",
            "while [ $# -gt 1 ]",
            "do",
            "  shift",
            "done",
            'echo "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>"',
            'echo "<!DOCTYPE plist PUBLIC \\"-//Apple//DTD PLIST 1.0//EN\\" \\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\\">"',
            'echo "<plist version=\\"1.0\\">"',
            'echo "  <dict>"',
            'echo "    <key>$1</key>"',
            'echo "    <dict>"',
            "",
            'if [ "$1" != "%s" -a "$1" != "%s" ]' % (libtwo_uuid, aout_uuid),
            "then",
            '  echo "      <key>DBGError</key>"',
            '  echo "      <string>not found by $0</string>"',
            '  echo "    </dict>"',
            '  echo "  </dict>"',
            '  echo "</plist>"',
            "  exit 0",
            "fi",
            #  UUID matches libtwo.dylib
            'if [ "$1" = "%s" ]' % (libtwo_uuid),
            "then",
            "  uuid=%s" % libtwo_uuid,
            "  bin=%s" % self.libtwo_exe,
            "  dsym=%s.dSYM/Contents/Resources/DWARF/%s"
            % (self.libtwo_exe, os.path.basename(self.libtwo_exe)),
            "fi",
            #  UUID matches a.out
            'if [ "$1" = "%s" ]' % (aout_uuid),
            "then",
            "  uuid=%s" % aout_uuid,
            "  bin=%s" % self.aout_exe,
            "  dsym=%s.dSYM/Contents/Resources/DWARF/%s"
            % (self.aout_exe, os.path.basename(self.aout_exe)),
            "fi",
            "",
            'echo "      <key>DBGDSYMPath</key>"',
            'echo "      <string>$dsym</string>"',
            'echo "      <key>DBGSymbolRichExecutable</key>"',
            'echo "      <string>$bin</string>"',
            'echo "    </dict>"',
            'echo "  </dict>"',
            'echo "</plist>"',
            "exit 0",
        ]

        with open(dsym_for_uuid, "w") as writer:
            for l in shell_cmds:
                writer.write(l + "\n")

        os.chmod(dsym_for_uuid, 0o755)

        # Register TWO of our binaries, but require dsymForUUID to find the third.
        target = self.dbg.CreateTarget(self.aout_exe, "", "", False, lldb.SBError())
        self.dbg.DeleteTarget(target)

        if self.TraceOn():
            print("Global image list, before loading corefile:")
            self.runCmd("image list -g")

        self.load_corefile_and_test()

    @skipIf(archs=no_match(["x86_64", "arm64", "arm64e", "aarch64"]))
    @skipIfRemote
    @skipUnlessDarwin
    def test_corefile_binaries_preloaded(self):
        self.initial_setup()

        if self.TraceOn():
            self.runCmd("log enable lldb dyld host")
            self.addTearDownHook(lambda: self.runCmd("log disable lldb dyld host"))

        # Register all three binaries in lldb's global module
        # cache, then throw the Targets away.
        target = self.dbg.CreateTarget(self.aout_exe, "", "", False, lldb.SBError())
        self.dbg.DeleteTarget(target)
        target = self.dbg.CreateTarget(self.libtwo_exe, "", "", False, lldb.SBError())
        self.dbg.DeleteTarget(target)

        if self.TraceOn():
            print("Global image list, before loading corefile:")
            self.runCmd("image list -g")

        self.load_corefile_and_test()