1"""
2Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
3"""
4
5import lldb
6import os
7
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11class TestLinux64LaunchingViaDynamicLoader(TestBase):
12    mydir = TestBase.compute_mydir(__file__)
13
14    @skipIf(oslist=no_match(['linux']))
15    @no_debug_info_test
16    @skipIf(oslist=["linux"], archs=["arm"])
17    def test(self):
18        self.build()
19
20        # Extracts path of the interpreter.
21        spec = lldb.SBModuleSpec()
22        spec.SetFileSpec(lldb.SBFileSpec(self.getBuildArtifact("a.out")))
23        interp_section = lldb.SBModule(spec).FindSection(".interp")
24        if not interp_section:
25          return
26        section_data = interp_section.GetSectionData()
27        error = lldb.SBError()
28        exe = section_data.GetString(error,0)
29        if error.Fail():
30          return
31
32        target = self.dbg.CreateTarget(exe)
33        self.assertTrue(target, VALID_TARGET)
34
35        # Set breakpoints both on shared library function as well as on
36        # main. Both of them will be pending breakpoints.
37        breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
38        breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
39        launch_info = lldb.SBLaunchInfo([ "--library-path", self.get_process_working_directory(), self.getBuildArtifact("a.out")])
40        launch_info.SetWorkingDirectory(self.get_process_working_directory())
41        error = lldb.SBError()
42        process = target.Launch(launch_info, error)
43        self.assertTrue(error.Success())
44
45        # Stopped on main here.
46        self.assertEqual(process.GetState(), lldb.eStateStopped)
47        thread = process.GetSelectedThread()
48        self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
49        process.Continue()
50
51        # Stopped on get_signal_crash function here.
52        self.assertEqual(process.GetState(), lldb.eStateStopped)
53        self.assertIn("get_signal_crash", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
54        process.Continue()
55
56        # Stopped because of generated signal.
57        self.assertEqual(process.GetState(), lldb.eStateStopped)
58        self.assertIn("raise", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
59        self.assertIn("get_signal_crash", thread.GetFrameAtIndex(1).GetDisplayFunctionName())
60