2 Test that breakpoint by symbol name works correctly with dynamic libs.
5 from __future__ import print_function
12 from lldbsuite.test.decorators import *
13 from lldbsuite.test.lldbtest import *
14 from lldbsuite.test import lldbutil
17 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently
18 class LoadUnloadTestCase(TestBase):
20 mydir = TestBase.compute_mydir(__file__)
23 # Call super's setUp().
25 # Find the line number to break for main.cpp.
26 self.line = line_number(
28 '// Set break point at this line for test_lldb_process_load_and_unload_commands().')
29 self.line_d_function = line_number(
30 'd.cpp', '// Find this line number within d_dunction().')
31 if not self.platformIsDarwin():
32 if not lldb.remote_platform and "LD_LIBRARY_PATH" in os.environ:
34 "settings set target.env-vars " +
37 os.environ["LD_LIBRARY_PATH"] +
41 if lldb.remote_platform:
42 wd = lldb.remote_platform.GetWorkingDirectory()
46 "settings set target.env-vars " +
51 def copy_shlibs_to_remote(self, hidden_dir=False):
52 """ Copies the shared libs required by this test suite to remote.
53 Does nothing in case of non-remote platforms.
55 if lldb.remote_platform:
57 if self.platformIsDarwin():
60 shlibs = ['libloadunload_a.' + ext, 'libloadunload_b.' + ext,
61 'libloadunload_c.' + ext, 'libloadunload_d.' + ext]
62 wd = lldb.remote_platform.GetWorkingDirectory()
65 err = lldb.remote_platform.Put(
66 lldb.SBFileSpec(os.path.join(cwd, f)),
67 lldb.SBFileSpec(os.path.join(wd, f)))
70 "Unable copy '%s' to '%s'.\n>>> %s" %
71 (f, wd, err.GetCString()))
73 shlib = 'libloadunload_d.' + ext
74 hidden_dir = os.path.join(wd, 'hidden')
75 hidden_file = os.path.join(hidden_dir, shlib)
76 err = lldb.remote_platform.MakeDirectory(hidden_dir)
79 "Unable to create a directory '%s'." % hidden_dir)
80 err = lldb.remote_platform.Put(
81 lldb.SBFileSpec(os.path.join(cwd, 'hidden', shlib)),
82 lldb.SBFileSpec(hidden_file))
85 "Unable copy 'libloadunload_d.so' to '%s'.\n>>> %s" %
86 (wd, err.GetCString()))
88 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
89 @not_remote_testsuite_ready
90 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently
91 def test_modules_search_paths(self):
92 """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'."""
94 # Invoke the default build rule.
97 if self.platformIsDarwin():
98 dylibName = 'libloadunload_d.dylib'
100 dylibName = 'libloadunload_d.so'
102 # The directory with the dynamic library we did not link to.
103 new_dir = os.path.join(os.getcwd(), "hidden")
105 old_dylib = os.path.join(os.getcwd(), dylibName)
106 new_dylib = os.path.join(new_dir, dylibName)
108 exe = os.path.join(os.getcwd(), "a.out")
109 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
111 self.expect("target modules list",
113 # self.expect("target modules list -t 3",
114 # patterns = ["%s-[^-]*-[^-]*" % self.getArchitecture()])
115 # Add an image search path substitution pair.
117 "target modules search-paths add %s %s" %
118 (os.getcwd(), new_dir))
120 self.expect("target modules search-paths list",
121 substrs=[os.getcwd(), new_dir])
124 "target modules search-paths query %s" %
126 "Image search path successfully transformed",
129 # Obliterate traces of libd from the old location.
131 # Inform (DY)LD_LIBRARY_PATH of the new path, too.
132 env_cmd_string = "settings set target.env-vars " + self.dylibPath + "=" + new_dir
134 print("Set environment to: ", env_cmd_string)
135 self.runCmd(env_cmd_string)
136 self.runCmd("settings show target.env-vars")
138 remove_dyld_path_cmd = "settings remove target.env-vars " + self.dylibPath
139 self.addTearDownHook(
140 lambda: self.dbg.HandleCommand(remove_dyld_path_cmd))
145 "target modules list",
146 "LLDB successfully locates the relocated dynamic library",
149 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
150 @expectedFailureAndroid # wrong source file shows up for hidden library
151 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently
152 def test_dyld_library_path(self):
153 """Test (DY)LD_LIBRARY_PATH after moving libd.dylib, which defines d_function, somewhere else."""
155 # Invoke the default build rule.
157 self.copy_shlibs_to_remote(hidden_dir=True)
159 exe = os.path.join(os.getcwd(), "a.out")
160 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
162 # Shut off ANSI color usage so we don't get ANSI escape sequences
163 # mixed in with stop locations.
164 self.dbg.SetUseColor(False)
166 if self.platformIsDarwin():
167 dylibName = 'libloadunload_d.dylib'
168 dsymName = 'libloadunload_d.dylib.dSYM'
170 dylibName = 'libloadunload_d.so'
172 # The directory to relocate the dynamic library and its debugging info.
173 special_dir = "hidden"
174 if lldb.remote_platform:
175 wd = lldb.remote_platform.GetWorkingDirectory()
180 new_dir = os.path.join(wd, special_dir)
181 old_dylib = os.path.join(old_dir, dylibName)
183 remove_dyld_path_cmd = "settings remove target.env-vars " + self.dylibPath
184 self.addTearDownHook(
185 lambda: self.dbg.HandleCommand(remove_dyld_path_cmd))
187 # For now we don't track (DY)LD_LIBRARY_PATH, so the old library will be in
189 self.expect("target modules list",
190 substrs=[os.path.basename(old_dylib)],
193 lldbutil.run_break_set_by_file_and_line(
194 self, "d.cpp", self.line_d_function, num_expected_locations=1)
195 # After run, make sure the non-hidden library is picked up.
196 self.expect("run", substrs=["return", "700"])
198 self.runCmd("continue")
200 # Add the hidden directory first in the search path.
201 env_cmd_string = ("settings set target.env-vars %s=%s" %
202 (self.dylibPath, new_dir))
203 if not self.platformIsDarwin():
204 env_cmd_string += ":" + wd
205 self.runCmd(env_cmd_string)
207 # This time, the hidden library should be picked up.
208 self.expect("run", substrs=["return", "12345"])
211 bugnumber="llvm.org/pr25805",
212 hostoslist=["windows"],
216 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
217 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently
218 def test_lldb_process_load_and_unload_commands(self):
219 """Test that lldb process load/unload command work correctly."""
221 # Invoke the default build rule.
223 self.copy_shlibs_to_remote()
225 exe = os.path.join(os.getcwd(), "a.out")
226 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
228 # Break at main.cpp before the call to dlopen().
229 # Use lldb's process load command to load the dylib, instead.
231 lldbutil.run_break_set_by_file_and_line(
232 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
234 self.runCmd("run", RUN_SUCCEEDED)
236 if lldb.remote_platform:
237 shlib_dir = lldb.remote_platform.GetWorkingDirectory()
239 shlib_dir = self.mydir
241 if self.platformIsDarwin():
242 dylibName = 'libloadunload_a.dylib'
244 dylibName = 'libloadunload_a.so'
246 # Make sure that a_function does not exist at this point.
248 "image lookup -n a_function",
249 "a_function should not exist yet",
252 patterns=["1 match found"])
254 # Use lldb 'process load' to load the dylib.
256 "process load %s --install" %
258 "%s loaded correctly" %
263 'Image [0-9]+ loaded'])
265 # Search for and match the "Image ([0-9]+) loaded" pattern.
266 output = self.res.GetOutput()
267 pattern = re.compile("Image ([0-9]+) loaded")
268 for l in output.split(os.linesep):
270 match = pattern.search(l)
273 index = match.group(1)
275 # Now we should have an entry for a_function.
277 "image lookup -n a_function",
278 "a_function should now exist",
280 "1 match found .*%s" %
283 # Use lldb 'process unload' to unload the dylib.
285 "process unload %s" %
287 "%s unloaded correctly" %
290 "Unloading .* with index %s.*ok" %
293 self.runCmd("process continue")
295 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
296 def test_load_unload(self):
297 """Test breakpoint by name works correctly with dlopen'ing."""
299 # Invoke the default build rule.
301 self.copy_shlibs_to_remote()
303 exe = os.path.join(os.getcwd(), "a.out")
304 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
306 # Break by function name a_function (not yet loaded).
307 lldbutil.run_break_set_by_symbol(
308 self, "a_function", num_expected_locations=0)
310 self.runCmd("run", RUN_SUCCEEDED)
312 # The stop reason of the thread should be breakpoint and at a_function.
313 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
316 'stop reason = breakpoint'])
318 # The breakpoint should have a hit count of 1.
319 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
320 substrs=[' resolved, hit count = 1'])
322 # Issue the 'contnue' command. We should stop agaian at a_function.
323 # The stop reason of the thread should be breakpoint and at a_function.
324 self.runCmd("continue")
326 # rdar://problem/8508987
327 # The a_function breakpoint should be encountered twice.
328 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
331 'stop reason = breakpoint'])
333 # The breakpoint should have a hit count of 2.
334 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
335 substrs=[' resolved, hit count = 2'])
337 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
338 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently
339 def test_step_over_load(self):
340 """Test stepping over code that loads a shared library works correctly."""
342 # Invoke the default build rule.
344 self.copy_shlibs_to_remote()
346 exe = os.path.join(os.getcwd(), "a.out")
347 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
349 # Break by function name a_function (not yet loaded).
350 lldbutil.run_break_set_by_file_and_line(
351 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
353 self.runCmd("run", RUN_SUCCEEDED)
355 # The stop reason of the thread should be breakpoint and at a_function.
356 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
358 'stop reason = breakpoint'])
362 "Stepping over function that loads library")
364 # The stop reason should be step end.
365 self.expect("thread list", "step over succeeded.",
367 'stop reason = step over'])
369 @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
370 @skipIfWindows # Windows doesn't have dlopen and friends, dynamic libraries work differently
371 @unittest2.expectedFailure("llvm.org/pr25806")
372 def test_static_init_during_load(self):
373 """Test that we can set breakpoints correctly in static initializers"""
376 self.copy_shlibs_to_remote()
378 exe = os.path.join(os.getcwd(), "a.out")
379 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
381 a_init_bp_num = lldbutil.run_break_set_by_symbol(
382 self, "a_init", num_expected_locations=0)
383 b_init_bp_num = lldbutil.run_break_set_by_symbol(
384 self, "b_init", num_expected_locations=0)
385 d_init_bp_num = lldbutil.run_break_set_by_symbol(
386 self, "d_init", num_expected_locations=1)
388 self.runCmd("run", RUN_SUCCEEDED)
390 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
393 'stop reason = breakpoint %d' % d_init_bp_num])
395 self.runCmd("continue")
396 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
399 'stop reason = breakpoint %d' % a_init_bp_num])
400 self.expect("thread backtrace",
405 self.runCmd("continue")
406 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
409 'stop reason = breakpoint %d' % b_init_bp_num])
410 self.expect("thread backtrace",