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