1import lldb
2from lldbsuite.test.decorators import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5
6
7class TestBreakInLoadedDylib(TestBase):
8    """Test that we can set a source regex breakpoint that will take in
9    a dlopened library that hasn't loaded when we set the breakpoint."""
10
11    NO_DEBUG_INFO_TESTCASE = True
12
13    @skipIfRemote
14    def common_setup(self):
15        self.build()
16        ctx = self.platformContext
17        self.main_spec = lldb.SBFileSpec("main.cpp")
18        self.b_spec = lldb.SBFileSpec("b.cpp")
19        self.lib_shortname = "lib_b"
20        self.lib_fullname = (
21            ctx.shlib_prefix + self.lib_shortname + "." + ctx.shlib_extension
22        )
23        self.lib_spec = lldb.SBFileSpec(self.lib_fullname)
24
25    def test_break_in_dlopen_dylib_using_lldbutils(self):
26        self.common_setup()
27        lldbutil.run_to_source_breakpoint(
28            self,
29            "Break here in dylib",
30            self.b_spec,
31            bkpt_module=self.lib_fullname,
32            extra_images=[self.lib_shortname],
33            has_locations_before_run=False,
34        )
35
36    @skipIfRemote
37    def test_break_in_dlopen_dylib_using_target(self):
38        self.common_setup()
39
40        target, process, _, _ = lldbutil.run_to_source_breakpoint(
41            self,
42            "Break here before we dlopen",
43            self.main_spec,
44            extra_images=[self.lib_shortname],
45        )
46
47        # Now set some breakpoints that won't take till the library is loaded:
48        # This one is currently how lldbutils does it but test here in case that changes:
49        bkpt1 = target.BreakpointCreateBySourceRegex(
50            "Break here in dylib", self.b_spec, self.lib_fullname
51        )
52
53        # Try the file list API as well.  Put in some bogus entries too, to make sure those
54        # don't trip us up:
55
56        files_list = lldb.SBFileSpecList()
57        files_list.Append(self.b_spec)
58        files_list.Append(self.main_spec)
59        files_list.Append(lldb.SBFileSpec("I_bet_nobody_has_this_file.cpp"))
60
61        modules_list = lldb.SBFileSpecList()
62        modules_list.Append(self.lib_spec)
63        modules_list.Append(lldb.SBFileSpec("libI_bet_not_this_one_either.dylib"))
64
65        bkpt2 = target.BreakpointCreateBySourceRegex(
66            "Break here in dylib", modules_list, files_list
67        )
68
69        lldbutil.continue_to_breakpoint(process, bkpt1)
70        self.assertEqual(bkpt1.GetHitCount(), 1, "Hit breakpoint 1")
71        self.assertEqual(bkpt2.GetHitCount(), 1, "Hit breakpoint 2")
72