2 Test how many times newly loaded binaries are notified;
3 they should be delivered in batches instead of one-by-one.
6 from __future__ import print_function
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
14 class ModuleLoadedNotifysTestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
17 NO_DEBUG_INFO_TESTCASE = True
19 # DyanmicLoaderDarwin should batch up notifications about
20 # newly added/removed libraries. Other DynamicLoaders may
21 # not be written this way.
25 # Call super's setUp().
27 # Find the line number to break inside main().
28 self.line = line_number('main.cpp', '// breakpoint')
30 def test_launch_notifications(self):
31 """Test that lldb broadcasts newly loaded libraries in batches."""
33 exe = self.getBuildArtifact("a.out")
34 self.dbg.SetAsync(False)
36 listener = self.dbg.GetListener()
37 listener.StartListeningForEventClass(
39 lldb.SBTarget.GetBroadcasterClassName(),
40 lldb.SBTarget.eBroadcastBitModulesLoaded | lldb.SBTarget.eBroadcastBitModulesUnloaded)
42 # Create a target by the debugger.
43 target = self.dbg.CreateTarget(exe)
44 self.assertTrue(target, VALID_TARGET)
47 breakpoint = target.BreakpointCreateByName('main', 'a.out')
49 event = lldb.SBEvent()
50 # CreateTarget() generated modules-loaded events; consume them & toss
51 while listener.GetNextEvent(event):
54 error = lldb.SBError()
55 process = target.Launch(listener,
61 None, # working directory
63 False, # Stop at entry
67 process.GetState() == lldb.eStateStopped,
70 total_solibs_added = 0
71 total_solibs_removed = 0
72 total_modules_added_events = 0
73 total_modules_removed_events = 0
74 while listener.GetNextEvent(event):
75 if lldb.SBTarget.EventIsTargetEvent(event):
76 if event.GetType() == lldb.SBTarget.eBroadcastBitModulesLoaded:
77 solib_count = lldb.SBTarget.GetNumModulesFromEvent(event)
78 total_modules_added_events += 1
79 total_solibs_added += solib_count
81 # print all of the binaries that have been added
84 while i < solib_count:
85 module = lldb.SBTarget.GetModuleAtIndexFromEvent(i, event)
86 added_files.append(module.GetFileSpec().GetFilename())
88 print("Loaded files: %s" % (', '.join(added_files)))
90 if event.GetType() == lldb.SBTarget.eBroadcastBitModulesUnloaded:
91 solib_count = lldb.SBTarget.GetNumModulesFromEvent(event)
92 total_modules_removed_events += 1
93 total_solibs_removed += solib_count
95 # print all of the binaries that have been removed
98 while i < solib_count:
99 module = lldb.SBTarget.GetModuleAtIndexFromEvent(i, event)
100 removed_files.append(module.GetFileSpec().GetFilename())
102 print("Unloaded files: %s" % (', '.join(removed_files)))
105 # This is testing that we get back a small number of events with the loaded
106 # binaries in batches. Check that we got back more than 1 solib per event.
107 # In practice on Darwin today, we get back two events for a do-nothing c
108 # program: a.out and dyld, and then all the rest of the system libraries.
110 avg_solibs_added_per_event = int(float(total_solibs_added) / float(total_modules_added_events))
111 self.assertGreater(avg_solibs_added_per_event, 1)