blob: f4db75c961f266de7ca61ed4556e10f247a8058f [file] [log] [blame]
Johnny Chen843f6892010-07-07 21:10:55 +00001"""
Johnny Chenb2017362010-12-14 22:26:34 +00002Test that breakpoint by symbol name works correctly with dynamic libs.
Johnny Chen843f6892010-07-07 21:10:55 +00003"""
4
5import os, time
Johnny Chen55e1bdf2010-12-06 21:08:51 +00006import re
Johnny Chen75e28f92010-08-05 23:42:46 +00007import unittest2
Johnny Chen843f6892010-07-07 21:10:55 +00008import lldb
Johnny Chend85dae52010-08-09 23:44:24 +00009from lldbtest import *
Johnny Chen843f6892010-07-07 21:10:55 +000010
Johnny Chen1c42e862010-09-01 19:59:58 +000011class LoadUnloadTestCase(TestBase):
Johnny Chen843f6892010-07-07 21:10:55 +000012
13 mydir = "load_unload"
14
Johnny Chen55e1bdf2010-12-06 21:08:51 +000015 def setUp(self):
16 # Call super's setUp().
17 TestBase.setUp(self)
18 # Find the line number to break for main.cpp.
19 self.line = line_number('main.c',
20 '// Set break point at this line for test_lldb_process_load_and_unload_commands().')
Johnny Chenb2017362010-12-14 22:26:34 +000021 self.line_d_function = line_number('d.c',
22 '// Find this line number within d_dunction().')
23
Johnny Chen4d661352011-02-03 00:30:19 +000024 def test_image_search_paths(self):
25 """Test image list after moving libd.dylib, and verifies that it works with 'target image-search-paths add'."""
26
27 # Invoke the default build rule.
28 self.buildDefault()
29
30 if sys.platform.startswith("darwin"):
31 dylibName = 'libd.dylib'
32
33 # Now let's move the dynamic library to a different directory than $CWD.
34
35 # The directory to relocate the dynamic library to.
36 new_dir = os.path.join(os.getcwd(), "dyld_path")
37
38 # This is the function to remove the dyld_path directory after the test.
39 def remove_dyld_dir():
40 import shutil
41 shutil.rmtree(new_dir)
42
43 old_dylib = os.path.join(os.getcwd(), dylibName)
44 new_dylib = os.path.join(new_dir, dylibName)
45
46 os.mkdir(new_dir)
47 os.rename(old_dylib, new_dylib)
48 self.addTearDownHook(remove_dyld_dir)
49
50 exe = os.path.join(os.getcwd(), "a.out")
51 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
52 self.expect("image list",
53 substrs = [old_dylib])
54 self.runCmd("target image-search-paths add %s %s" % (os.getcwd(), new_dir))
55 self.expect("image list", "LLDB successfully locates the relocated dynamic library",
56 substrs = [new_dylib])
57
58
Johnny Chenb2017362010-12-14 22:26:34 +000059 def test_dyld_library_path(self):
60 """Test DYLD_LIBRARY_PATH after moving libd.dylib, which defines d_function, somewhere else."""
61
62 # Invoke the default build rule.
63 self.buildDefault()
64
65 if sys.platform.startswith("darwin"):
66 dylibName = 'libd.dylib'
67 dsymName = 'libd.dylib.dSYM'
68 dylibPath = 'DYLD_LIBRARY_PATH'
69
70 # Now let's move the dynamic library to a different directory than $CWD.
71
72 # The directory to relocate the dynamic library and its debugging info.
73 new_dir = os.path.join(os.getcwd(), "dyld_path")
74
75 # This is the function to remove the dyld_path directory after the test.
Johnny Chenec882742010-12-14 23:13:03 +000076 def remove_dyld_dir():
Johnny Chenb2017362010-12-14 22:26:34 +000077 import shutil
78 shutil.rmtree(new_dir)
79
80 old_dylib = os.path.join(os.getcwd(), dylibName)
81 new_dylib = os.path.join(new_dir, dylibName)
82 old_dSYM = os.path.join(os.getcwd(), dsymName)
83 new_dSYM = os.path.join(new_dir, dsymName)
84 #system(["ls", "-lR", "."])
85 os.mkdir(new_dir)
86 os.rename(old_dylib, new_dylib)
87 if dsymName:
88 os.rename(old_dSYM, new_dSYM)
Johnny Chenec882742010-12-14 23:13:03 +000089 self.addTearDownHook(remove_dyld_dir)
Johnny Chenb2017362010-12-14 22:26:34 +000090 #system(["ls", "-lR", "."])
91
92 # With libd.dylib moved, a.out run should fail.
93 exe = os.path.join(os.getcwd(), "a.out")
94 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
95 # Set breakpoint by function name d_function.
96 self.expect("breakpoint set -n d_function", BREAKPOINT_CREATED,
97 substrs = ["Breakpoint created",
98 "name = 'd_function'",
99 "locations = 0 (pending)"])
100 self.runCmd("run")
101 self.expect("process status", "Not expected to hit the d_function breakpoint",
102 matching=False,
103 substrs = ["stop reason = breakpoint"])
104 # Kill the inferior process.
105 self.runCmd("process kill")
106
107 # Try again with the DYLD_LIBRARY_PATH environment variable properly set.
108 os.environ[dylibPath] = new_dir
Johnny Chenec882742010-12-14 23:13:03 +0000109 self.addTearDownHook(lambda: os.environ.pop(dylibPath))
Johnny Chenb2017362010-12-14 22:26:34 +0000110 self.runCmd("run")
111 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
112 patterns = ["frame #0.*d_function.*at d.c:%d" % self.line_d_function])
Johnny Chen55e1bdf2010-12-06 21:08:51 +0000113
114 def test_lldb_process_load_and_unload_commands(self):
115 """Test that lldb process load/unload command work correctly."""
116
117 # Invoke the default build rule.
118 self.buildDefault()
119
120 exe = os.path.join(os.getcwd(), "a.out")
121 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
122
123 # Break at main.c before the call to dlopen().
124 # Use lldb's process load command to load the dylib, instead.
125
126 self.expect("breakpoint set -f main.c -l %d" % self.line,
127 BREAKPOINT_CREATED,
128 startstr = "Breakpoint created: 1: file ='main.c', line = %d" %
129 self.line)
130
131 self.runCmd("run", RUN_SUCCEEDED)
132
133 # Make sure that a_function does not exist at this point.
134 self.expect("image lookup -n a_function", "a_function should not exist yet",
135 error=True, matching=False,
136 patterns = ["1 match found .* %s" % self.mydir])
137
138 # Use lldb 'process load' to load the dylib.
139 self.expect("process load liba.dylib", "liba.dylib loaded correctly",
140 patterns = ['Loading "liba.dylib".*ok',
141 'Image [0-9]+ loaded'])
142
143 # Search for and match the "Image ([0-9]+) loaded" pattern.
144 output = self.res.GetOutput()
145 pattern = re.compile("Image ([0-9]+) loaded")
146 for l in output.split(os.linesep):
147 #print "l:", l
148 match = pattern.search(l)
149 if match:
150 break
151 index = match.group(1)
152
153 # Now we should have an entry for a_function.
154 self.expect("image lookup -n a_function", "a_function should now exist",
155 patterns = ["1 match found .*%s" % self.mydir])
156
157 # Use lldb 'process unload' to unload the dylib.
158 self.expect("process unload %s" % index, "liba.dylib unloaded correctly",
159 patterns = ["Unloading .* with index %s.*ok" % index])
160
161 self.runCmd("process continue")
162
Johnny Chen30974392010-07-27 20:59:06 +0000163 def test_load_unload(self):
Johnny Chen843f6892010-07-07 21:10:55 +0000164 """Test breakpoint by name works correctly with dlopen'ing."""
Johnny Chen821a8c42010-09-03 23:52:15 +0000165
166 # Invoke the default build rule.
167 self.buildDefault()
168
Johnny Chen843f6892010-07-07 21:10:55 +0000169 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen029acae2010-08-20 21:03:09 +0000170 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
Johnny Chen843f6892010-07-07 21:10:55 +0000171
172 # Break by function name a_function (not yet loaded).
Johnny Chen029acae2010-08-20 21:03:09 +0000173 self.expect("breakpoint set -n a_function", BREAKPOINT_CREATED,
174 startstr = "Breakpoint created: 1: name = 'a_function', locations = 0 (pending)")
Johnny Chen843f6892010-07-07 21:10:55 +0000175
Johnny Chen1bb9f9a2010-08-27 23:47:36 +0000176 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen843f6892010-07-07 21:10:55 +0000177
178 # The stop reason of the thread should be breakpoint and at a_function.
Johnny Chen029acae2010-08-20 21:03:09 +0000179 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen8a87d522010-10-18 15:44:42 +0000180 substrs = ['state is stopped',
Johnny Chen029acae2010-08-20 21:03:09 +0000181 'a_function',
Johnny Chen029acae2010-08-20 21:03:09 +0000182 'stop reason = breakpoint'])
Johnny Chen843f6892010-07-07 21:10:55 +0000183
184 # The breakpoint should have a hit count of 1.
Johnny Chen029acae2010-08-20 21:03:09 +0000185 self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
186 substrs = [' resolved, hit count = 1'])
Johnny Chen843f6892010-07-07 21:10:55 +0000187
Johnny Chen14df3d42010-10-04 16:58:16 +0000188 # Issue the 'contnue' command. We should stop agaian at a_function.
189 # The stop reason of the thread should be breakpoint and at a_function.
190 self.runCmd("continue")
Johnny Chenc958be42010-10-20 21:56:26 +0000191
192 # rdar://problem/8508987
193 # The a_function breakpoint should be encountered twice.
Johnny Chen14df3d42010-10-04 16:58:16 +0000194 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen8a87d522010-10-18 15:44:42 +0000195 substrs = ['state is stopped',
Johnny Chen14df3d42010-10-04 16:58:16 +0000196 'a_function',
197 'stop reason = breakpoint'])
198
199 # The breakpoint should have a hit count of 2.
200 self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
201 substrs = [' resolved, hit count = 2'])
Johnny Chen843f6892010-07-07 21:10:55 +0000202
203
204if __name__ == '__main__':
Johnny Chen88f83042010-08-05 21:23:45 +0000205 import atexit
Johnny Chen843f6892010-07-07 21:10:55 +0000206 lldb.SBDebugger.Initialize()
Johnny Chen88f83042010-08-05 21:23:45 +0000207 atexit.register(lambda: lldb.SBDebugger.Terminate())
Johnny Chen75e28f92010-08-05 23:42:46 +0000208 unittest2.main()