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