Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 1 | """ |
Johnny Chen | b201736 | 2010-12-14 22:26:34 +0000 | [diff] [blame] | 2 | Test that breakpoint by symbol name works correctly with dynamic libs. |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 3 | """ |
| 4 | |
| 5 | import os, time |
Johnny Chen | 55e1bdf | 2010-12-06 21:08:51 +0000 | [diff] [blame] | 6 | import re |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 7 | import unittest2 |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 8 | import lldb |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 9 | from lldbtest import * |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 10 | |
Johnny Chen | 1c42e86 | 2010-09-01 19:59:58 +0000 | [diff] [blame] | 11 | class LoadUnloadTestCase(TestBase): |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 12 | |
| 13 | mydir = "load_unload" |
| 14 | |
Johnny Chen | 55e1bdf | 2010-12-06 21:08:51 +0000 | [diff] [blame] | 15 | 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 Chen | b201736 | 2010-12-14 22:26:34 +0000 | [diff] [blame] | 21 | self.line_d_function = line_number('d.c', |
| 22 | '// Find this line number within d_dunction().') |
| 23 | |
Johnny Chen | 4d66135 | 2011-02-03 00:30:19 +0000 | [diff] [blame] | 24 | 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)) |
Johnny Chen | 7acdcec | 2011-02-03 18:03:54 +0000 | [diff] [blame^] | 55 | # Add teardown hook to clear image-search-paths after the test. |
| 56 | self.addTearDownHook(lambda: self.runCmd("target image-search-paths clear")) |
Johnny Chen | 4d66135 | 2011-02-03 00:30:19 +0000 | [diff] [blame] | 57 | self.expect("image list", "LLDB successfully locates the relocated dynamic library", |
| 58 | substrs = [new_dylib]) |
| 59 | |
| 60 | |
Johnny Chen | b201736 | 2010-12-14 22:26:34 +0000 | [diff] [blame] | 61 | def test_dyld_library_path(self): |
| 62 | """Test DYLD_LIBRARY_PATH after moving libd.dylib, which defines d_function, somewhere else.""" |
| 63 | |
| 64 | # Invoke the default build rule. |
| 65 | self.buildDefault() |
| 66 | |
| 67 | if sys.platform.startswith("darwin"): |
| 68 | dylibName = 'libd.dylib' |
| 69 | dsymName = 'libd.dylib.dSYM' |
| 70 | dylibPath = 'DYLD_LIBRARY_PATH' |
| 71 | |
| 72 | # Now let's move the dynamic library to a different directory than $CWD. |
| 73 | |
| 74 | # The directory to relocate the dynamic library and its debugging info. |
| 75 | new_dir = os.path.join(os.getcwd(), "dyld_path") |
| 76 | |
| 77 | # This is the function to remove the dyld_path directory after the test. |
Johnny Chen | ec88274 | 2010-12-14 23:13:03 +0000 | [diff] [blame] | 78 | def remove_dyld_dir(): |
Johnny Chen | b201736 | 2010-12-14 22:26:34 +0000 | [diff] [blame] | 79 | import shutil |
| 80 | shutil.rmtree(new_dir) |
| 81 | |
| 82 | old_dylib = os.path.join(os.getcwd(), dylibName) |
| 83 | new_dylib = os.path.join(new_dir, dylibName) |
| 84 | old_dSYM = os.path.join(os.getcwd(), dsymName) |
| 85 | new_dSYM = os.path.join(new_dir, dsymName) |
| 86 | #system(["ls", "-lR", "."]) |
| 87 | os.mkdir(new_dir) |
| 88 | os.rename(old_dylib, new_dylib) |
| 89 | if dsymName: |
| 90 | os.rename(old_dSYM, new_dSYM) |
Johnny Chen | ec88274 | 2010-12-14 23:13:03 +0000 | [diff] [blame] | 91 | self.addTearDownHook(remove_dyld_dir) |
Johnny Chen | b201736 | 2010-12-14 22:26:34 +0000 | [diff] [blame] | 92 | #system(["ls", "-lR", "."]) |
| 93 | |
| 94 | # With libd.dylib moved, a.out run should fail. |
| 95 | exe = os.path.join(os.getcwd(), "a.out") |
| 96 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 97 | # Set breakpoint by function name d_function. |
| 98 | self.expect("breakpoint set -n d_function", BREAKPOINT_CREATED, |
| 99 | substrs = ["Breakpoint created", |
| 100 | "name = 'd_function'", |
| 101 | "locations = 0 (pending)"]) |
| 102 | self.runCmd("run") |
| 103 | self.expect("process status", "Not expected to hit the d_function breakpoint", |
| 104 | matching=False, |
| 105 | substrs = ["stop reason = breakpoint"]) |
| 106 | # Kill the inferior process. |
| 107 | self.runCmd("process kill") |
| 108 | |
| 109 | # Try again with the DYLD_LIBRARY_PATH environment variable properly set. |
| 110 | os.environ[dylibPath] = new_dir |
Johnny Chen | ec88274 | 2010-12-14 23:13:03 +0000 | [diff] [blame] | 111 | self.addTearDownHook(lambda: os.environ.pop(dylibPath)) |
Johnny Chen | b201736 | 2010-12-14 22:26:34 +0000 | [diff] [blame] | 112 | self.runCmd("run") |
| 113 | self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT, |
| 114 | patterns = ["frame #0.*d_function.*at d.c:%d" % self.line_d_function]) |
Johnny Chen | 55e1bdf | 2010-12-06 21:08:51 +0000 | [diff] [blame] | 115 | |
| 116 | def test_lldb_process_load_and_unload_commands(self): |
| 117 | """Test that lldb process load/unload command work correctly.""" |
| 118 | |
| 119 | # Invoke the default build rule. |
| 120 | self.buildDefault() |
| 121 | |
| 122 | exe = os.path.join(os.getcwd(), "a.out") |
| 123 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 124 | |
| 125 | # Break at main.c before the call to dlopen(). |
| 126 | # Use lldb's process load command to load the dylib, instead. |
| 127 | |
| 128 | self.expect("breakpoint set -f main.c -l %d" % self.line, |
| 129 | BREAKPOINT_CREATED, |
| 130 | startstr = "Breakpoint created: 1: file ='main.c', line = %d" % |
| 131 | self.line) |
| 132 | |
| 133 | self.runCmd("run", RUN_SUCCEEDED) |
| 134 | |
| 135 | # Make sure that a_function does not exist at this point. |
| 136 | self.expect("image lookup -n a_function", "a_function should not exist yet", |
| 137 | error=True, matching=False, |
| 138 | patterns = ["1 match found .* %s" % self.mydir]) |
| 139 | |
| 140 | # Use lldb 'process load' to load the dylib. |
| 141 | self.expect("process load liba.dylib", "liba.dylib loaded correctly", |
| 142 | patterns = ['Loading "liba.dylib".*ok', |
| 143 | 'Image [0-9]+ loaded']) |
| 144 | |
| 145 | # Search for and match the "Image ([0-9]+) loaded" pattern. |
| 146 | output = self.res.GetOutput() |
| 147 | pattern = re.compile("Image ([0-9]+) loaded") |
| 148 | for l in output.split(os.linesep): |
| 149 | #print "l:", l |
| 150 | match = pattern.search(l) |
| 151 | if match: |
| 152 | break |
| 153 | index = match.group(1) |
| 154 | |
| 155 | # Now we should have an entry for a_function. |
| 156 | self.expect("image lookup -n a_function", "a_function should now exist", |
| 157 | patterns = ["1 match found .*%s" % self.mydir]) |
| 158 | |
| 159 | # Use lldb 'process unload' to unload the dylib. |
| 160 | self.expect("process unload %s" % index, "liba.dylib unloaded correctly", |
| 161 | patterns = ["Unloading .* with index %s.*ok" % index]) |
| 162 | |
| 163 | self.runCmd("process continue") |
| 164 | |
Johnny Chen | 3097439 | 2010-07-27 20:59:06 +0000 | [diff] [blame] | 165 | def test_load_unload(self): |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 166 | """Test breakpoint by name works correctly with dlopen'ing.""" |
Johnny Chen | 821a8c4 | 2010-09-03 23:52:15 +0000 | [diff] [blame] | 167 | |
| 168 | # Invoke the default build rule. |
| 169 | self.buildDefault() |
| 170 | |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 171 | exe = os.path.join(os.getcwd(), "a.out") |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 172 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 173 | |
| 174 | # Break by function name a_function (not yet loaded). |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 175 | self.expect("breakpoint set -n a_function", BREAKPOINT_CREATED, |
| 176 | startstr = "Breakpoint created: 1: name = 'a_function', locations = 0 (pending)") |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 177 | |
Johnny Chen | 1bb9f9a | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 178 | self.runCmd("run", RUN_SUCCEEDED) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 179 | |
| 180 | # The stop reason of the thread should be breakpoint and at a_function. |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 181 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Johnny Chen | 8a87d52 | 2010-10-18 15:44:42 +0000 | [diff] [blame] | 182 | substrs = ['state is stopped', |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 183 | 'a_function', |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 184 | 'stop reason = breakpoint']) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 185 | |
| 186 | # The breakpoint should have a hit count of 1. |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 187 | self.expect("breakpoint list", BREAKPOINT_HIT_ONCE, |
| 188 | substrs = [' resolved, hit count = 1']) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 189 | |
Johnny Chen | 14df3d4 | 2010-10-04 16:58:16 +0000 | [diff] [blame] | 190 | # Issue the 'contnue' command. We should stop agaian at a_function. |
| 191 | # The stop reason of the thread should be breakpoint and at a_function. |
| 192 | self.runCmd("continue") |
Johnny Chen | c958be4 | 2010-10-20 21:56:26 +0000 | [diff] [blame] | 193 | |
| 194 | # rdar://problem/8508987 |
| 195 | # The a_function breakpoint should be encountered twice. |
Johnny Chen | 14df3d4 | 2010-10-04 16:58:16 +0000 | [diff] [blame] | 196 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Johnny Chen | 8a87d52 | 2010-10-18 15:44:42 +0000 | [diff] [blame] | 197 | substrs = ['state is stopped', |
Johnny Chen | 14df3d4 | 2010-10-04 16:58:16 +0000 | [diff] [blame] | 198 | 'a_function', |
| 199 | 'stop reason = breakpoint']) |
| 200 | |
| 201 | # The breakpoint should have a hit count of 2. |
| 202 | self.expect("breakpoint list", BREAKPOINT_HIT_ONCE, |
| 203 | substrs = [' resolved, hit count = 2']) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | if __name__ == '__main__': |
Johnny Chen | 88f8304 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 207 | import atexit |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 208 | lldb.SBDebugger.Initialize() |
Johnny Chen | 88f8304 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 209 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 210 | unittest2.main() |