1""" 2Test the use of the global module cache in lldb 3""" 4 5import lldb 6 7from lldbsuite.test.decorators import * 8from lldbsuite.test.lldbtest import * 9from lldbsuite.test import lldbutil 10import os 11import shutil 12from pathlib import Path 13import time 14 15class GlobalModuleCacheTestCase(TestBase): 16 # NO_DEBUG_INFO_TESTCASE = True 17 18 def check_counter_var(self, thread, value): 19 frame = thread.frames[0] 20 var = frame.FindVariable("counter") 21 self.assertTrue(var.GetError().Success(), "Got counter variable") 22 self.assertEqual(var.GetValueAsUnsigned(), value, "This was one-print") 23 24 def copy_to_main(self, src, dst): 25 # We are relying on the source file being newer than the .o file from 26 # a previous build, so sleep a bit here to ensure that the touch is later. 27 time.sleep(2) 28 try: 29 shutil.copy(src, dst) 30 except: 31 self.fail(f"Could not copy {src} to {dst}") 32 Path(dst).touch() 33 34 # The rerun tests indicate rerunning on Windows doesn't really work, so 35 # this one won't either. 36 @skipIfWindows 37 # On Arm and AArch64 Linux, this test attempts to pop a thread plan when 38 # we only have the base plan remaining. Skip it until we can figure out 39 # the bug this is exposing. 40 @skipIf(oslist=["linux"], archs=["arm", "aarch64"]) 41 def test_OneTargetOneDebugger(self): 42 self.do_test(True, True) 43 44 # This behaves as implemented but that behavior is not desirable. 45 # This test tests for the desired behavior as an expected fail. 46 @skipIfWindows 47 @expectedFailureAll 48 def test_TwoTargetsOneDebugger(self): 49 self.do_test(False, True) 50 51 @skipIfWindows 52 @expectedFailureAll 53 def test_OneTargetTwoDebuggers(self): 54 self.do_test(True, False) 55 56 def do_test(self, one_target, one_debugger): 57 # Here to debug flakiness on Arm, remove later! 58 log_cmd_result = lldb.SBCommandReturnObject() 59 interp = self.dbg.GetCommandInterpreter() 60 interp.HandleCommand("log enable lldb step", log_cmd_result) 61 62 # Make sure that if we have one target, and we run, then 63 # change the binary and rerun, the binary (and any .o files 64 # if using dwarf in .o file debugging) get removed from the 65 # shared module cache. They are no longer reachable. 66 debug_style = self.getDebugInfo() 67 68 # Before we do anything, clear the global module cache so we don't 69 # see objects from other runs: 70 lldb.SBDebugger.MemoryPressureDetected() 71 72 # Set up the paths for our two versions of main.c: 73 main_c_path = os.path.join(self.getBuildDir(), "main.c") 74 one_print_path = os.path.join(self.getSourceDir(), "one-print.c") 75 two_print_path = os.path.join(self.getSourceDir(), "two-print.c") 76 main_filespec = lldb.SBFileSpec(main_c_path) 77 78 # First copy the one-print.c to main.c in the build folder and 79 # build our a.out from there: 80 self.copy_to_main(one_print_path, main_c_path) 81 self.build(dictionary={"C_SOURCES": main_c_path, "EXE": "a.out"}) 82 83 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint( 84 self, "return counter;", main_filespec 85 ) 86 87 # Make sure we ran the version we intended here: 88 self.check_counter_var(thread, 1) 89 process.Kill() 90 91 # Now copy two-print.c over main.c, rebuild, and rerun: 92 # os.unlink(target.GetExecutable().fullpath) 93 self.copy_to_main(two_print_path, main_c_path) 94 95 self.build(dictionary={"C_SOURCES": main_c_path, "EXE": "a.out"}) 96 error = lldb.SBError() 97 if one_debugger: 98 if one_target: 99 (_, process, thread, _) = lldbutil.run_to_breakpoint_do_run( 100 self, target, bkpt 101 ) 102 else: 103 (target2, process2, thread, bkpt) = lldbutil.run_to_source_breakpoint( 104 self, "return counter;", main_filespec 105 ) 106 else: 107 if one_target: 108 new_debugger = lldb.SBDebugger().Create() 109 self.old_debugger = self.dbg 110 self.dbg = new_debugger 111 def cleanupDebugger(self): 112 lldb.SBDebugger.Destroy(self.dbg) 113 self.dbg = self.old_debugger 114 self.old_debugger = None 115 116 self.addTearDownHook(cleanupDebugger) 117 (target2, process2, thread, bkpt) = lldbutil.run_to_source_breakpoint( 118 self, "return counter;", main_filespec 119 ) 120 121 # In two-print.c counter will be 2: 122 self.check_counter_var(thread, 2) 123 124 # If we made two targets, destroy the first one, that should free up the 125 # unreachable Modules: 126 if not one_target: 127 target.Clear() 128 129 num_a_dot_out_entries = 1 130 # For dSYM's there will be two lines of output, one for the a.out and one 131 # for the dSYM. 132 if debug_style == "dsym": 133 num_a_dot_out_entries += 1 134 135 error = self.check_image_list_result(num_a_dot_out_entries, 1) 136 # Even if this fails, MemoryPressureDetected should fix this. 137 lldb.SBDebugger.MemoryPressureDetected() 138 error_after_mpd = self.check_image_list_result(num_a_dot_out_entries, 1) 139 fail_msg = "" 140 if error != "": 141 fail_msg = "Error before MPD: " + error 142 143 if error_after_mpd != "": 144 fail_msg = fail_msg + "\nError after MPD: " + error_after_mpd 145 if fail_msg != "": 146 self.fail(fail_msg) 147 148 def check_image_list_result(self, num_a_dot_out, num_main_dot_o): 149 # Check the global module list, there should only be one a.out, and if we are 150 # doing dwarf in .o file, there should only be one .o file. This returns 151 # an error string on error - rather than asserting, so you can stage this 152 # failing. 153 image_cmd_result = lldb.SBCommandReturnObject() 154 interp = self.dbg.GetCommandInterpreter() 155 interp.HandleCommand("image list -g", image_cmd_result) 156 if self.TraceOn(): 157 print(f"Expected: a.out: {num_a_dot_out} main.o: {num_main_dot_o}") 158 print(image_cmd_result) 159 160 image_list_str = image_cmd_result.GetOutput() 161 image_list = image_list_str.splitlines() 162 found_a_dot_out = 0 163 found_main_dot_o = 0 164 165 for line in image_list: 166 # FIXME: force this to be at the end of the string: 167 if "a.out" in line: 168 found_a_dot_out += 1 169 if "main.o" in line: 170 found_main_dot_o += 1 171 172 if num_a_dot_out != found_a_dot_out: 173 return f"Got {found_a_dot_out} number of a.out's, expected {num_a_dot_out}" 174 175 if found_main_dot_o > 0 and num_main_dot_o != found_main_dot_o: 176 return f"Got {found_main_dot_o} number of main.o's, expected {num_main_dot_o}" 177 178 return "" 179