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):
14*cf2c8e41SPavel Labath    NO_DEBUG_INFO_TESTCASE = True
15*cf2c8e41SPavel Labath
16*cf2c8e41SPavel Labath    @skipIfXmlSupportMissing
17*cf2c8e41SPavel Labath    @skipIf(oslist=no_match(['linux']))
18*cf2c8e41SPavel Labath    def test_with_svr4(self):
19*cf2c8e41SPavel Labath        self.runCmd("settings set plugin.process.gdb-remote.use-libraries-svr4 true")
20*cf2c8e41SPavel Labath        self._test()
21879a47a5SGreg Clayton
22879a47a5SGreg Clayton    @skipIf(oslist=no_match(['linux']))
23*cf2c8e41SPavel Labath    def test_without_svr4(self):
24*cf2c8e41SPavel Labath        self.runCmd("settings set plugin.process.gdb-remote.use-libraries-svr4 false")
25*cf2c8e41SPavel Labath        self._test()
26*cf2c8e41SPavel Labath
27*cf2c8e41SPavel Labath    def _test(self):
28879a47a5SGreg Clayton        self.build()
29879a47a5SGreg Clayton
30879a47a5SGreg Clayton        # Extracts path of the interpreter.
31879a47a5SGreg Clayton        exe = self.getBuildArtifact("a.out")
32879a47a5SGreg Clayton
33879a47a5SGreg Clayton        spec = lldb.SBModuleSpec()
34879a47a5SGreg Clayton        spec.SetFileSpec(lldb.SBFileSpec(exe))
35879a47a5SGreg Clayton        interp_section = lldb.SBModule(spec).FindSection(".interp")
36879a47a5SGreg Clayton        if not interp_section:
37879a47a5SGreg Clayton          return
38879a47a5SGreg Clayton        section_data = interp_section.GetSectionData()
39879a47a5SGreg Clayton        error = lldb.SBError()
40879a47a5SGreg Clayton        dyld_path = section_data.GetString(error,0)
41879a47a5SGreg Clayton        if error.Fail():
42879a47a5SGreg Clayton          return
43879a47a5SGreg Clayton
44879a47a5SGreg Clayton        target = self.dbg.CreateTarget(exe)
45879a47a5SGreg Clayton        self.assertTrue(target, VALID_TARGET)
46879a47a5SGreg Clayton
47879a47a5SGreg Clayton        # Set a breakpoint in the main function that will get hit after the
48879a47a5SGreg Clayton        # program exec's via the dynamic loader. The breakpoint will only get
49879a47a5SGreg Clayton        # hit if we can successfully read the shared library lists in the
50879a47a5SGreg Clayton        # DynamicLoaderPOSIXDYLD.cpp when we exec into the dynamic loader.
51879a47a5SGreg Clayton        breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
52879a47a5SGreg Clayton        # Setup our launch info to supply the dynamic loader path to the
53879a47a5SGreg Clayton        # program so it gets two args:
54879a47a5SGreg Clayton        # - path to a.out
55879a47a5SGreg Clayton        # - path to dynamic loader
56879a47a5SGreg Clayton        launch_info = lldb.SBLaunchInfo([dyld_path])
57879a47a5SGreg Clayton        error = lldb.SBError()
58879a47a5SGreg Clayton        process = target.Launch(launch_info, error)
59879a47a5SGreg Clayton        self.assertSuccess(error)
60879a47a5SGreg Clayton
61879a47a5SGreg Clayton        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonExec)
62879a47a5SGreg Clayton        self.assertEqual(len(threads), 1, "We got a thread stopped for exec.")
63879a47a5SGreg Clayton
64879a47a5SGreg Clayton        process.Continue();
65879a47a5SGreg Clayton
66879a47a5SGreg Clayton        # Stopped on main here.
6747c4c6a7SDave Lee        self.assertState(process.GetState(), lldb.eStateStopped)
68879a47a5SGreg Clayton        thread = process.GetSelectedThread()
69879a47a5SGreg Clayton        self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
70