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