1879a47a5SGreg Clayton"""
2879a47a5SGreg ClaytonTest that LLDB can launch a linux executable and then execs into the dynamic
3879a47a5SGreg Claytonloader into this program again.
4879a47a5SGreg Clayton"""
5879a47a5SGreg Clayton
6879a47a5SGreg Claytonimport lldb
7879a47a5SGreg Claytonimport os
8879a47a5SGreg Clayton
9879a47a5SGreg Claytonfrom lldbsuite.test.decorators import *
10879a47a5SGreg Claytonfrom lldbsuite.test.lldbtest import *
11879a47a5SGreg Claytonfrom lldbsuite.test import lldbutil
12879a47a5SGreg Clayton
13879a47a5SGreg Claytonclass TestLinux64ExecViaDynamicLoader(TestBase):
14879a47a5SGreg Clayton    mydir = TestBase.compute_mydir(__file__)
15*cf2c8e41SPavel Labath    NO_DEBUG_INFO_TESTCASE = True
16*cf2c8e41SPavel Labath
17*cf2c8e41SPavel Labath    @skipIfXmlSupportMissing
18*cf2c8e41SPavel Labath    @skipIf(oslist=no_match(['linux']))
19*cf2c8e41SPavel Labath    def test_with_svr4(self):
20*cf2c8e41SPavel Labath        self.runCmd("settings set plugin.process.gdb-remote.use-libraries-svr4 true")
21*cf2c8e41SPavel Labath        self._test()
22879a47a5SGreg Clayton
23879a47a5SGreg Clayton    @skipIf(oslist=no_match(['linux']))
24*cf2c8e41SPavel Labath    def test_without_svr4(self):
25*cf2c8e41SPavel Labath        self.runCmd("settings set plugin.process.gdb-remote.use-libraries-svr4 false")
26*cf2c8e41SPavel Labath        self._test()
27*cf2c8e41SPavel Labath
28*cf2c8e41SPavel Labath    def _test(self):
29879a47a5SGreg Clayton        self.build()
30879a47a5SGreg Clayton
31879a47a5SGreg Clayton        # Extracts path of the interpreter.
32879a47a5SGreg Clayton        exe = self.getBuildArtifact("a.out")
33879a47a5SGreg Clayton
34879a47a5SGreg Clayton        spec = lldb.SBModuleSpec()
35879a47a5SGreg Clayton        spec.SetFileSpec(lldb.SBFileSpec(exe))
36879a47a5SGreg Clayton        interp_section = lldb.SBModule(spec).FindSection(".interp")
37879a47a5SGreg Clayton        if not interp_section:
38879a47a5SGreg Clayton          return
39879a47a5SGreg Clayton        section_data = interp_section.GetSectionData()
40879a47a5SGreg Clayton        error = lldb.SBError()
41879a47a5SGreg Clayton        dyld_path = section_data.GetString(error,0)
42879a47a5SGreg Clayton        if error.Fail():
43879a47a5SGreg Clayton          return
44879a47a5SGreg Clayton
45879a47a5SGreg Clayton        target = self.dbg.CreateTarget(exe)
46879a47a5SGreg Clayton        self.assertTrue(target, VALID_TARGET)
47879a47a5SGreg Clayton
48879a47a5SGreg Clayton        # Set a breakpoint in the main function that will get hit after the
49879a47a5SGreg Clayton        # program exec's via the dynamic loader. The breakpoint will only get
50879a47a5SGreg Clayton        # hit if we can successfully read the shared library lists in the
51879a47a5SGreg Clayton        # DynamicLoaderPOSIXDYLD.cpp when we exec into the dynamic loader.
52879a47a5SGreg Clayton        breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
53879a47a5SGreg Clayton        # Setup our launch info to supply the dynamic loader path to the
54879a47a5SGreg Clayton        # program so it gets two args:
55879a47a5SGreg Clayton        # - path to a.out
56879a47a5SGreg Clayton        # - path to dynamic loader
57879a47a5SGreg Clayton        launch_info = lldb.SBLaunchInfo([dyld_path])
58879a47a5SGreg Clayton        error = lldb.SBError()
59879a47a5SGreg Clayton        process = target.Launch(launch_info, error)
60879a47a5SGreg Clayton        self.assertSuccess(error)
61879a47a5SGreg Clayton
62879a47a5SGreg Clayton        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonExec)
63879a47a5SGreg Clayton        self.assertEqual(len(threads), 1, "We got a thread stopped for exec.")
64879a47a5SGreg Clayton
65879a47a5SGreg Clayton        process.Continue();
66879a47a5SGreg Clayton
67879a47a5SGreg Clayton        # Stopped on main here.
6847c4c6a7SDave Lee        self.assertState(process.GetState(), lldb.eStateStopped)
69879a47a5SGreg Clayton        thread = process.GetSelectedThread()
70879a47a5SGreg Clayton        self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
71