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