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