1"""
2Test that LLDB can launch a linux executable and then execs into the dynamic
3loader into this program again.
4"""
5
6import lldb
7import os
8
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13class TestLinux64ExecViaDynamicLoader(TestBase):
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @skipIfXmlSupportMissing
18    @skipIf(oslist=no_match(['linux']))
19    def test_with_svr4(self):
20        self.runCmd("settings set plugin.process.gdb-remote.use-libraries-svr4 true")
21        self._test()
22
23    @skipIf(oslist=no_match(['linux']))
24    def test_without_svr4(self):
25        self.runCmd("settings set plugin.process.gdb-remote.use-libraries-svr4 false")
26        self._test()
27
28    def _test(self):
29        self.build()
30
31        # Extracts path of the interpreter.
32        exe = self.getBuildArtifact("a.out")
33
34        spec = lldb.SBModuleSpec()
35        spec.SetFileSpec(lldb.SBFileSpec(exe))
36        interp_section = lldb.SBModule(spec).FindSection(".interp")
37        if not interp_section:
38          return
39        section_data = interp_section.GetSectionData()
40        error = lldb.SBError()
41        dyld_path = section_data.GetString(error,0)
42        if error.Fail():
43          return
44
45        target = self.dbg.CreateTarget(exe)
46        self.assertTrue(target, VALID_TARGET)
47
48        # Set a breakpoint in the main function that will get hit after the
49        # program exec's via the dynamic loader. The breakpoint will only get
50        # hit if we can successfully read the shared library lists in the
51        # DynamicLoaderPOSIXDYLD.cpp when we exec into the dynamic loader.
52        breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
53        # Setup our launch info to supply the dynamic loader path to the
54        # program so it gets two args:
55        # - path to a.out
56        # - path to dynamic loader
57        launch_info = lldb.SBLaunchInfo([dyld_path])
58        error = lldb.SBError()
59        process = target.Launch(launch_info, error)
60        self.assertSuccess(error)
61
62        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonExec)
63        self.assertEqual(len(threads), 1, "We got a thread stopped for exec.")
64
65        process.Continue();
66
67        # Stopped on main here.
68        self.assertState(process.GetState(), lldb.eStateStopped)
69        thread = process.GetSelectedThread()
70        self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
71