1""" Test that stop-on-sharedlibrary-events works and cooperates with breakpoints. """
2import lldb
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test import lldbutil
6
7class TestStopOnSharedlibraryEvents(TestBase):
8
9    @skipIfRemote
10    @skipIfWindows
11    @no_debug_info_test
12    def test_stopping_breakpoints(self):
13        self.do_test()
14
15    @skipIfRemote
16    @skipIfWindows
17    @no_debug_info_test
18    def test_auto_continue(self):
19        def auto_continue(bkpt):
20            bkpt.SetAutoContinue(True)
21        self.do_test(auto_continue)
22
23    @skipIfRemote
24    @skipIfWindows
25    @no_debug_info_test
26    def test_failing_condition(self):
27        def condition(bkpt):
28            bkpt.SetCondition("1 == 2")
29        self.do_test(condition)
30
31    @skipIfRemote
32    @skipIfWindows
33    @no_debug_info_test
34    def test_continue_callback(self):
35        def bkpt_callback(bkpt):
36            bkpt.SetScriptCallbackBody("return False")
37        self.do_test(bkpt_callback)
38
39    def do_test(self, bkpt_modifier = None):
40        self.build()
41        main_spec = lldb.SBFileSpec("main.cpp")
42        # Launch and stop before the dlopen call.
43        target, process, thread, _ = lldbutil.run_to_source_breakpoint(self,
44                "// Set a breakpoint here", main_spec, extra_images=["load_a",
45                    "load_b"])
46
47        # Now turn on shared library events, continue and make sure we stop for the event.
48        self.runCmd("settings set target.process.stop-on-sharedlibrary-events 1")
49        self.addTearDownHook(lambda: self.runCmd(
50            "settings set target.process.stop-on-sharedlibrary-events 0"))
51
52        # Since I don't know how to check that we are at the "right place" to stop for
53        # shared library events, make an breakpoint after the load is done and
54        # make sure we don't stop there:
55        backstop_bkpt_1 = target.BreakpointCreateBySourceRegex("Set another here - we should not hit this one", main_spec)
56        self.assertGreater(backstop_bkpt_1.GetNumLocations(), 0, "Set our second breakpoint")
57
58        process.Continue()
59        self.assertState(process.GetState(), lldb.eStateStopped, "We didn't stop for the load")
60        self.assertEqual(backstop_bkpt_1.GetHitCount(), 0, "Hit our backstop breakpoint")
61
62        # We should be stopped after the library is loaded, check that:
63        found_it = False
64        for module in target.modules:
65            if module.file.basename.find("load_a") > -1:
66                found_it = True
67                break
68        self.assertTrue(found_it, "Found the loaded module.")
69
70        # Now capture the place where we stopped so we can set a breakpoint and make
71        # sure the breakpoint there works correctly:
72        load_address = process.GetSelectedThread().frames[0].addr
73        load_bkpt = target.BreakpointCreateBySBAddress(load_address)
74        self.assertGreater(load_bkpt.GetNumLocations(), 0, "Set the load breakpoint")
75
76        backstop_bkpt_1.SetEnabled(False)
77
78        backstop_bkpt_2 = target.BreakpointCreateBySourceRegex("Set a third here - we should not hit this one", main_spec)
79        self.assertGreater(backstop_bkpt_2.GetNumLocations(), 0, "Set our third breakpoint")
80
81        if bkpt_modifier == None:
82            process.Continue()
83            self.assertState(process.GetState(), lldb.eStateStopped, "We didn't stop for the load")
84            self.assertEqual(backstop_bkpt_2.GetHitCount(), 0, "Hit our backstop breakpoint")
85            self.assertEqual(thread.stop_reason, lldb.eStopReasonBreakpoint, "We attributed the stop to the breakpoint")
86            self.assertEqual(load_bkpt.GetHitCount(), 1, "We hit our breakpoint at the load address")
87        else:
88            bkpt_modifier(load_bkpt)
89            process.Continue()
90            self.assertState(process.GetState(), lldb.eStateStopped, "We didn't stop")
91            self.assertTrue(thread.IsValid(), "Our thread was no longer valid.")
92            self.assertEqual(thread.stop_reason, lldb.eStopReasonBreakpoint, "We didn't hit some breakpoint")
93            self.assertEqual(backstop_bkpt_2.GetHitCount(), 1, "We continued to the right breakpoint")
94
95
96
97
98
99