aboutsummaryrefslogtreecommitdiff
path: root/test/functionalities/load_unload/TestLoadUnload.py
blob: 6fc1124a4f7e2e69e22454d0036c24771908d0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
Test that breakpoint by symbol name works correctly with dynamic libs.
"""

import os, time
import re
import unittest2
import lldb
from lldbtest import *
import lldbutil

class LoadUnloadTestCase(TestBase):

    def getCategories (self):
        return ['basic_process']

    mydir = os.path.join("functionalities", "load_unload")

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Find the line number to break for main.cpp.
        self.line = line_number('main.c',
                                '// Set break point at this line for test_lldb_process_load_and_unload_commands().')
        self.line_d_function = line_number('d.c',
                                           '// Find this line number within d_dunction().')

    @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
    @skipIfLinux # llvm.org/pr14424 - missing linux Makefiles/testcase support
    def test_modules_search_paths(self):
        """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'."""

        # Invoke the default build rule.
        self.buildDefault()

        if sys.platform.startswith("darwin"):
            dylibName = 'libd.dylib'

        # The directory with the dynamic library we did not link to.
        new_dir = os.path.join(os.getcwd(), "hidden")

        old_dylib = os.path.join(os.getcwd(), dylibName)
        new_dylib = os.path.join(new_dir, dylibName)

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        self.expect("target modules list",
            substrs = [old_dylib])
        #self.expect("target modules list -t 3",
        #    patterns = ["%s-[^-]*-[^-]*" % self.getArchitecture()])
        # Add an image search path substitution pair.
        self.runCmd("target modules search-paths add %s %s" % (os.getcwd(), new_dir))
        # Add teardown hook to clear image-search-paths after the test.
        # rdar://problem/10501020
        # Uncomment the following to reproduce 10501020.
        self.addTearDownHook(lambda: self.runCmd("target modules search-paths clear"))

        self.expect("target modules search-paths list",
            substrs = [os.getcwd(), new_dir])

        self.expect("target modules search-paths query %s" % os.getcwd(), "Image search path successfully transformed",
            substrs = [new_dir])

        # Obliterate traces of libd from the old location.
        os.remove(old_dylib)
        # Inform dyld of the new path, too.
        env_cmd_string = "settings set target.env-vars " + self.dylibPath + "=" + new_dir
        if self.TraceOn():
            print "Set environment to: ", env_cmd_string
        self.runCmd(env_cmd_string)
        self.runCmd("settings show target.env-vars")

        remove_dyld_path_cmd = "settings remove target.env-vars " + self.dylibPath
        self.addTearDownHook(lambda: self.runCmd(remove_dyld_path_cmd))

        self.runCmd("run")

        self.expect("target modules list", "LLDB successfully locates the relocated dynamic library",
            substrs = [new_dylib])

    @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
    @skipIfLinux # llvm.org/pr14424 - missing linux Makefiles/testcase support
    def test_dyld_library_path(self):
        """Test DYLD_LIBRARY_PATH after moving libd.dylib, which defines d_function, somewhere else."""

        # Invoke the default build rule.
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        if sys.platform.startswith("darwin"):
            dylibName = 'libd.dylib'
            dsymName = 'libd.dylib.dSYM'

        # The directory to relocate the dynamic library and its debugging info.
        special_dir = "hidden"
        new_dir = os.path.join(os.getcwd(), special_dir)

        old_dylib = os.path.join(os.getcwd(), dylibName)
        new_dylib = os.path.join(new_dir, dylibName)
        old_dSYM = os.path.join(os.getcwd(), dsymName)
        new_dSYM = os.path.join(new_dir, dsymName)

        #system(["ls", "-lR", "."])

        # Try running with the DYLD_LIBRARY_PATH environment variable set, make sure
        # we pick up the hidden dylib.

        env_cmd_string = "settings set target.env-vars " + self.dylibPath + "=" + new_dir
        if self.TraceOn():
            print "Set environment to: ", env_cmd_string
        self.runCmd(env_cmd_string)
        self.runCmd("settings show target.env-vars")

        remove_dyld_path_cmd = "settings remove target.env-vars " + self.dylibPath
        self.addTearDownHook(lambda: self.runCmd(remove_dyld_path_cmd))

        lldbutil.run_break_set_by_file_and_line (self, "d.c", self.line_d_function, num_expected_locations=1, loc_exact=True)

        # For now we don't track DYLD_LIBRARY_PATH, so the old library will be in
        # the modules list.
        self.expect("target modules list",
            substrs = [os.path.basename(old_dylib)],
            matching=True)

        self.runCmd("run")
        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
            patterns = ["frame #0.*d_function.*at d.c:%d" % self.line_d_function])

        # After run, make sure the hidden library is present, and the one we didn't 
        # load is not.
        self.expect("target modules list",
            substrs = [special_dir, os.path.basename(new_dylib)])

    @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
    @skipIfLinux # llvm.org/pr14424 - missing linux Makefiles/testcase support
    def test_lldb_process_load_and_unload_commands(self):
        """Test that lldb process load/unload command work correctly."""

        # Invoke the default build rule.
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Break at main.c before the call to dlopen().
        # Use lldb's process load command to load the dylib, instead.

        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)

        self.runCmd("run", RUN_SUCCEEDED)

        # Make sure that a_function does not exist at this point.
        self.expect("image lookup -n a_function", "a_function should not exist yet",
                    error=True, matching=False,
            patterns = ["1 match found .* %s" % self.mydir])

        # Use lldb 'process load' to load the dylib.
        self.expect("process load liba.dylib", "liba.dylib loaded correctly",
            patterns = ['Loading "liba.dylib".*ok',
                        'Image [0-9]+ loaded'])

        # Search for and match the "Image ([0-9]+) loaded" pattern.
        output = self.res.GetOutput()
        pattern = re.compile("Image ([0-9]+) loaded")
        for l in output.split(os.linesep):
            #print "l:", l
            match = pattern.search(l)
            if match:
                break
        index = match.group(1)

        # Now we should have an entry for a_function.
        self.expect("image lookup -n a_function", "a_function should now exist",
            patterns = ["1 match found .*%s" % self.mydir])

        # Use lldb 'process unload' to unload the dylib.
        self.expect("process unload %s" % index, "liba.dylib unloaded correctly",
            patterns = ["Unloading .* with index %s.*ok" % index])

        self.runCmd("process continue")

    @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
    @skipIfLinux # llvm.org/pr14424 - missing linux Makefiles/testcase support
    def test_load_unload(self):
        """Test breakpoint by name works correctly with dlopen'ing."""

        # Invoke the default build rule.
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Break by function name a_function (not yet loaded).
        lldbutil.run_break_set_by_symbol (self, "a_function", num_expected_locations=0)

        self.runCmd("run", RUN_SUCCEEDED)

        # The stop reason of the thread should be breakpoint and at a_function.
        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['stopped',
                       'a_function',
                       'stop reason = breakpoint'])

        # The breakpoint should have a hit count of 1.
        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
            substrs = [' resolved, hit count = 1'])

        # Issue the 'contnue' command.  We should stop agaian at a_function.
        # The stop reason of the thread should be breakpoint and at a_function.
        self.runCmd("continue")

        # rdar://problem/8508987
        # The a_function breakpoint should be encountered twice.
        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['stopped',
                       'a_function',
                       'stop reason = breakpoint'])

        # The breakpoint should have a hit count of 2.
        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
            substrs = [' resolved, hit count = 2'])

    @skipIfFreeBSD # llvm.org/pr14424 - missing FreeBSD Makefiles/testcase support
    @skipIfLinux # llvm.org/pr14424 - missing linux Makefiles/testcase support
    def test_step_over_load (self):
        """Test stepping over code that loads a shared library works correctly."""

        # Invoke the default build rule.
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Break by function name a_function (not yet loaded).
        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)

        self.runCmd("run", RUN_SUCCEEDED)

        # The stop reason of the thread should be breakpoint and at a_function.
        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['stopped',
                       'stop reason = breakpoint'])

        self.runCmd("thread step-over", "Stepping over function that loads library")
        
        # The stop reason should be step end.
        self.expect("thread list", "step over succeeded.", 
            substrs = ['stopped',
                      'stop reason = step over'])

if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()