1""" 2Test that breakpoint by symbol name works correctly with dynamic libs. 3""" 4 5from __future__ import print_function 6 7 8import os 9import re 10import lldb 11from lldbsuite.test.decorators import * 12from lldbsuite.test.lldbtest import * 13from lldbsuite.test import lldbutil 14 15 16class LoadUnloadTestCase(TestBase): 17 18 mydir = TestBase.compute_mydir(__file__) 19 20 NO_DEBUG_INFO_TESTCASE = True 21 22 def setUp(self): 23 # Call super's setUp(). 24 TestBase.setUp(self) 25 self.setup_test() 26 # Invoke the default build rule. 27 self.build() 28 # Find the line number to break for main.cpp. 29 self.line = line_number( 30 'main.cpp', 31 '// Set break point at this line for test_lldb_process_load_and_unload_commands().') 32 self.line_d_function = line_number( 33 'd.cpp', '// Find this line number within d_dunction().') 34 35 def setup_test(self): 36 lldbutil.mkdir_p(self.getBuildArtifact("hidden")) 37 if lldb.remote_platform: 38 path = lldb.remote_platform.GetWorkingDirectory() 39 else: 40 path = self.getBuildDir() 41 if self.dylibPath in os.environ: 42 sep = self.platformContext.shlib_path_separator 43 path = os.environ[self.dylibPath] + sep + path 44 self.runCmd("settings append target.env-vars '{}={}'".format(self.dylibPath, path)) 45 self.default_path = path 46 47 def copy_shlibs_to_remote(self, hidden_dir=False): 48 """ Copies the shared libs required by this test suite to remote. 49 Does nothing in case of non-remote platforms. 50 """ 51 if lldb.remote_platform: 52 ext = 'so' 53 if self.platformIsDarwin(): 54 ext = 'dylib' 55 56 shlibs = ['libloadunload_a.' + ext, 'libloadunload_b.' + ext, 57 'libloadunload_c.' + ext, 'libloadunload_d.' + ext] 58 wd = lldb.remote_platform.GetWorkingDirectory() 59 cwd = os.getcwd() 60 for f in shlibs: 61 err = lldb.remote_platform.Put( 62 lldb.SBFileSpec(self.getBuildArtifact(f)), 63 lldb.SBFileSpec(os.path.join(wd, f))) 64 if err.Fail(): 65 raise RuntimeError( 66 "Unable copy '%s' to '%s'.\n>>> %s" % 67 (f, wd, err.GetCString())) 68 if hidden_dir: 69 shlib = 'libloadunload_d.' + ext 70 hidden_dir = os.path.join(wd, 'hidden') 71 hidden_file = os.path.join(hidden_dir, shlib) 72 err = lldb.remote_platform.MakeDirectory(hidden_dir) 73 if err.Fail(): 74 raise RuntimeError( 75 "Unable to create a directory '%s'." % hidden_dir) 76 err = lldb.remote_platform.Put( 77 lldb.SBFileSpec(os.path.join('hidden', shlib)), 78 lldb.SBFileSpec(hidden_file)) 79 if err.Fail(): 80 raise RuntimeError( 81 "Unable copy 'libloadunload_d.so' to '%s'.\n>>> %s" % 82 (wd, err.GetCString())) 83 84 def setSvr4Support(self, enabled): 85 self.runCmd( 86 "settings set plugin.process.gdb-remote.use-libraries-svr4 {enabled}".format( 87 enabled="true" if enabled else "false" 88 ) 89 ) 90 91 # libloadunload_d.so does not appear in the image list because executable 92 # dependencies are resolved relative to the debuggers PWD. Bug? 93 @expectedFailureAll(oslist=["linux"]) 94 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 95 @not_remote_testsuite_ready 96 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently 97 @expectedFailureNetBSD 98 @skipIfReproducer # VFS is a snapshot. 99 def test_modules_search_paths(self): 100 """Test target modules list after loading a different copy of the library libd.dylib, and verifies that it works with 'target modules search-paths add'.""" 101 if self.platformIsDarwin(): 102 dylibName = 'libloadunload_d.dylib' 103 else: 104 dylibName = 'libloadunload_d.so' 105 106 # The directory with the dynamic library we did not link to. 107 new_dir = os.path.join(self.getBuildDir(), "hidden") 108 109 old_dylib = os.path.join(self.getBuildDir(), dylibName) 110 new_dylib = os.path.join(new_dir, dylibName) 111 exe = self.getBuildArtifact("a.out") 112 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 113 114 self.expect("target modules list", 115 substrs=[old_dylib]) 116 # self.expect("target modules list -t 3", 117 # patterns = ["%s-[^-]*-[^-]*" % self.getArchitecture()]) 118 # Add an image search path substitution pair. 119 self.runCmd( 120 "target modules search-paths add %s %s" % 121 (self.getBuildDir(), new_dir)) 122 123 self.expect("target modules search-paths list", 124 substrs=[self.getBuildDir(), new_dir]) 125 126 self.expect( 127 "target modules search-paths query %s" % 128 self.getBuildDir(), 129 "Image search path successfully transformed", 130 substrs=[new_dir]) 131 132 # Obliterate traces of libd from the old location. 133 os.remove(old_dylib) 134 # Inform (DY)LD_LIBRARY_PATH of the new path, too. 135 env_cmd_string = "settings replace target.env-vars " + self.dylibPath + "=" + new_dir 136 if self.TraceOn(): 137 print("Set environment to: ", env_cmd_string) 138 self.runCmd(env_cmd_string) 139 self.runCmd("settings show target.env-vars") 140 141 self.runCmd("run") 142 143 self.expect( 144 "target modules list", 145 "LLDB successfully locates the relocated dynamic library", 146 substrs=[new_dylib]) 147 148 # libloadunload_d.so does not appear in the image list because executable 149 # dependencies are resolved relative to the debuggers PWD. Bug? 150 @expectedFailureAll(oslist=["linux"]) 151 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 152 @expectedFailureAndroid # wrong source file shows up for hidden library 153 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently 154 @skipIfDarwinEmbedded 155 @expectedFailureNetBSD 156 def test_dyld_library_path(self): 157 """Test (DY)LD_LIBRARY_PATH after moving libd.dylib, which defines d_function, somewhere else.""" 158 self.copy_shlibs_to_remote(hidden_dir=True) 159 160 exe = self.getBuildArtifact("a.out") 161 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 162 163 # Shut off ANSI color usage so we don't get ANSI escape sequences 164 # mixed in with stop locations. 165 self.dbg.SetUseColor(False) 166 167 if self.platformIsDarwin(): 168 dylibName = 'libloadunload_d.dylib' 169 dsymName = 'libloadunload_d.dylib.dSYM' 170 else: 171 dylibName = 'libloadunload_d.so' 172 173 # The directory to relocate the dynamic library and its debugging info. 174 special_dir = "hidden" 175 if lldb.remote_platform: 176 wd = lldb.remote_platform.GetWorkingDirectory() 177 else: 178 wd = self.getBuildDir() 179 180 old_dir = wd 181 new_dir = os.path.join(wd, special_dir) 182 old_dylib = os.path.join(old_dir, dylibName) 183 184 # For now we don't track (DY)LD_LIBRARY_PATH, so the old 185 # library will be in the modules list. 186 self.expect("target modules list", 187 substrs=[os.path.basename(old_dylib)], 188 matching=True) 189 190 lldbutil.run_break_set_by_file_and_line( 191 self, "d.cpp", self.line_d_function, num_expected_locations=1) 192 # After run, make sure the non-hidden library is picked up. 193 self.expect("run", substrs=["return", "700"]) 194 195 self.runCmd("continue") 196 197 # Add the hidden directory first in the search path. 198 env_cmd_string = ("settings set target.env-vars %s=%s%s%s" % 199 (self.dylibPath, new_dir, 200 self.platformContext.shlib_path_separator, self.default_path)) 201 self.runCmd(env_cmd_string) 202 203 # This time, the hidden library should be picked up. 204 self.expect("run", substrs=["return", "12345"]) 205 206 @expectedFailureAll( 207 bugnumber="llvm.org/pr25805", 208 hostoslist=["windows"], 209 triple='.*-android') 210 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 211 @expectedFailureAll(oslist=["windows"]) # process load not implemented 212 def test_lldb_process_load_and_unload_commands(self): 213 self.setSvr4Support(False) 214 self.run_lldb_process_load_and_unload_commands() 215 216 @expectedFailureAll( 217 bugnumber="llvm.org/pr25805", 218 hostoslist=["windows"], 219 triple='.*-android') 220 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 221 @expectedFailureAll(oslist=["windows"]) # process load not implemented 222 def test_lldb_process_load_and_unload_commands_with_svr4(self): 223 self.setSvr4Support(True) 224 self.run_lldb_process_load_and_unload_commands() 225 226 def run_lldb_process_load_and_unload_commands(self): 227 """Test that lldb process load/unload command work correctly.""" 228 self.copy_shlibs_to_remote() 229 230 exe = self.getBuildArtifact("a.out") 231 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 232 233 # Break at main.cpp before the call to dlopen(). 234 # Use lldb's process load command to load the dylib, instead. 235 236 lldbutil.run_break_set_by_file_and_line( 237 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True) 238 239 self.runCmd("run", RUN_SUCCEEDED) 240 241 ctx = self.platformContext 242 dylibName = ctx.shlib_prefix + 'loadunload_a.' + ctx.shlib_extension 243 localDylibPath = self.getBuildArtifact(dylibName) 244 if lldb.remote_platform: 245 wd = lldb.remote_platform.GetWorkingDirectory() 246 remoteDylibPath = lldbutil.join_remote_paths(wd, dylibName) 247 else: 248 remoteDylibPath = localDylibPath 249 250 # Make sure that a_function does not exist at this point. 251 self.expect( 252 "image lookup -n a_function", 253 "a_function should not exist yet", 254 error=True, 255 matching=False, 256 patterns=["1 match found"]) 257 258 # Use lldb 'process load' to load the dylib. 259 self.expect( 260 "process load %s --install=%s" % (localDylibPath, remoteDylibPath), 261 "%s loaded correctly" % dylibName, 262 patterns=[ 263 'Loading "%s".*ok' % re.escape(localDylibPath), 264 'Image [0-9]+ loaded']) 265 266 # Search for and match the "Image ([0-9]+) loaded" pattern. 267 output = self.res.GetOutput() 268 pattern = re.compile("Image ([0-9]+) loaded") 269 for l in output.split(os.linesep): 270 #print("l:", l) 271 match = pattern.search(l) 272 if match: 273 break 274 index = match.group(1) 275 276 # Now we should have an entry for a_function. 277 self.expect( 278 "image lookup -n a_function", 279 "a_function should now exist", 280 patterns=[ 281 "1 match found .*%s" % 282 dylibName]) 283 284 # Use lldb 'process unload' to unload the dylib. 285 self.expect( 286 "process unload %s" % 287 index, 288 "%s unloaded correctly" % 289 dylibName, 290 patterns=[ 291 "Unloading .* with index %s.*ok" % 292 index]) 293 294 self.runCmd("process continue") 295 296 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 297 @expectedFailureAll(oslist=["windows"]) # breakpoint not hit 298 def test_load_unload(self): 299 self.setSvr4Support(False) 300 self.run_load_unload() 301 302 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 303 @expectedFailureAll(oslist=["windows"]) # breakpoint not hit 304 def test_load_unload_with_svr4(self): 305 self.setSvr4Support(True) 306 self.run_load_unload() 307 308 def run_load_unload(self): 309 """Test breakpoint by name works correctly with dlopen'ing.""" 310 self.copy_shlibs_to_remote() 311 312 exe = self.getBuildArtifact("a.out") 313 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 314 315 # Break by function name a_function (not yet loaded). 316 lldbutil.run_break_set_by_symbol( 317 self, "a_function", num_expected_locations=0) 318 319 self.runCmd("run", RUN_SUCCEEDED) 320 321 # The stop reason of the thread should be breakpoint and at a_function. 322 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 323 substrs=['stopped', 324 'a_function', 325 'stop reason = breakpoint']) 326 327 # The breakpoint should have a hit count of 1. 328 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE, 329 substrs=[' resolved, hit count = 1']) 330 331 # Issue the 'continue' command. We should stop agaian at a_function. 332 # The stop reason of the thread should be breakpoint and at a_function. 333 self.runCmd("continue") 334 335 # rdar://problem/8508987 336 # The a_function breakpoint should be encountered twice. 337 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 338 substrs=['stopped', 339 'a_function', 340 'stop reason = breakpoint']) 341 342 # The breakpoint should have a hit count of 2. 343 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE, 344 substrs=[' resolved, hit count = 2']) 345 346 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 347 def test_step_over_load(self): 348 self.setSvr4Support(False) 349 self.run_step_over_load() 350 351 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 352 def test_step_over_load_with_svr4(self): 353 self.setSvr4Support(True) 354 self.run_step_over_load() 355 356 def run_step_over_load(self): 357 """Test stepping over code that loads a shared library works correctly.""" 358 self.copy_shlibs_to_remote() 359 360 exe = self.getBuildArtifact("a.out") 361 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 362 363 # Break by function name a_function (not yet loaded). 364 lldbutil.run_break_set_by_file_and_line( 365 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True) 366 367 self.runCmd("run", RUN_SUCCEEDED) 368 369 # The stop reason of the thread should be breakpoint and at a_function. 370 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 371 substrs=['stopped', 372 'stop reason = breakpoint']) 373 374 self.runCmd( 375 "thread step-over", 376 "Stepping over function that loads library") 377 378 # The stop reason should be step end. 379 self.expect("thread list", "step over succeeded.", 380 substrs=['stopped', 381 'stop reason = step over']) 382 383 # We can't find a breakpoint location for d_init before launching because 384 # executable dependencies are resolved relative to the debuggers PWD. Bug? 385 @expectedFailureAll(oslist=["linux"], triple=no_match('aarch64-.*-android')) 386 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support 387 @expectedFailureNetBSD 388 def test_static_init_during_load(self): 389 """Test that we can set breakpoints correctly in static initializers""" 390 self.copy_shlibs_to_remote() 391 392 exe = self.getBuildArtifact("a.out") 393 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 394 395 a_init_bp_num = lldbutil.run_break_set_by_symbol( 396 self, "a_init", num_expected_locations=0) 397 b_init_bp_num = lldbutil.run_break_set_by_symbol( 398 self, "b_init", num_expected_locations=0) 399 d_init_bp_num = lldbutil.run_break_set_by_symbol( 400 self, "d_init", num_expected_locations=1) 401 402 self.runCmd("run", RUN_SUCCEEDED) 403 404 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 405 substrs=['stopped', 406 'd_init', 407 'stop reason = breakpoint %d' % d_init_bp_num]) 408 409 self.runCmd("continue") 410 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 411 substrs=['stopped', 412 'b_init', 413 'stop reason = breakpoint %d' % b_init_bp_num]) 414 self.expect("thread backtrace", 415 substrs=['b_init', 416 'dylib_open', 417 'main']) 418 419 self.runCmd("continue") 420 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 421 substrs=['stopped', 422 'a_init', 423 'stop reason = breakpoint %d' % a_init_bp_num]) 424 self.expect("thread backtrace", 425 substrs=['a_init', 426 'dylib_open', 427 'main']) 428