1import lldb
2from lldbsuite.test.decorators import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5
6class TestCase(TestBase):
7    NO_DEBUG_INFO_TESTCASE = True
8
9    @skipIfRemote
10    def test_load_after_attach(self):
11        self.build()
12
13        sync_file_path = lldbutil.append_to_process_working_directory(self, "process_ready")
14
15        ctx = self.platformContext
16        lib_name = ctx.shlib_prefix + 'lib_b.' + ctx.shlib_extension
17
18        exe = self.getBuildArtifact("a.out")
19        lib = self.getBuildArtifact(lib_name)
20
21        target = self.dbg.CreateTarget(exe)
22        environment = self.registerSharedLibrariesWithTarget(target, ["lib_b"])
23
24        # Spawn a new process.
25        # use realpath to workaround llvm.org/pr48376
26        # Pass path to solib for dlopen to properly locate the library.
27        popen = self.spawnSubprocess(os.path.realpath(exe), [sync_file_path],
28                extra_env=environment)
29        lldbutil.wait_for_file_on_target(self, sync_file_path)
30
31        # Attach to the spawned process.
32        error = lldb.SBError()
33        process = target.AttachToProcessWithID(self.dbg.GetListener(),
34                popen.pid, error)
35        self.assertSuccess(error)
36
37        # Continue until first breakpoint.
38        breakpoint1 = self.target().BreakpointCreateBySourceRegex(
39            "// break here", lldb.SBFileSpec("main.cpp"))
40        self.assertEqual(breakpoint1.GetNumResolvedLocations(), 1)
41        stopped_threads = lldbutil.continue_to_breakpoint(self.process(), breakpoint1)
42        self.assertEqual(len(stopped_threads), 1)
43
44        # Change a variable to escape the loop
45        self.runCmd("expression main_thread_continue = 1")
46
47        # Continue so that dlopen is called.
48        breakpoint2 = self.target().BreakpointCreateBySourceRegex(
49            "// break after dlopen", lldb.SBFileSpec("main.cpp"))
50        self.assertEqual(breakpoint2.GetNumResolvedLocations(), 1)
51        stopped_threads = lldbutil.continue_to_breakpoint(self.process(), breakpoint2)
52        self.assertEqual(len(stopped_threads), 1)
53
54        # Check that image list contains liblib_b after dlopen.
55        self.match(
56                "image list",
57                patterns = [lib_name],
58                matching = True,
59                msg = lib_name + " missing in image list")
60
61