Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test lldb core component: SourceManager. |
| 3 | |
| 4 | Test cases: |
Johnny Chen | de2c8bd | 2010-12-09 22:06:05 +0000 | [diff] [blame] | 5 | |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 6 | o test_display_source_python: |
| 7 | Test display of source using the SBSourceManager API. |
Johnny Chen | de2c8bd | 2010-12-09 22:06:05 +0000 | [diff] [blame] | 8 | o test_modify_source_file_while_debugging: |
| 9 | Test the caching mechanism of the source manager. |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 12 | import unittest2 |
Johnny Chen | 67f73ac | 2010-12-09 18:38:52 +0000 | [diff] [blame] | 13 | import lldb |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 14 | from lldbtest import * |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 15 | import lldbutil |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 16 | |
| 17 | class SourceManagerTestCase(TestBase): |
| 18 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame^] | 19 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 20 | |
| 21 | def setUp(self): |
| 22 | # Call super's setUp(). |
| 23 | TestBase.setUp(self) |
| 24 | # Find the line number to break inside main(). |
| 25 | self.line = line_number('main.c', '// Set break point at this line.') |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 26 | lldb.skip_build_and_cleanup = False |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 27 | |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 28 | @python_api_test |
| 29 | def test_display_source_python(self): |
| 30 | """Test display of source using the SBSourceManager API.""" |
| 31 | self.buildDefault() |
| 32 | self.display_source_python() |
| 33 | |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 34 | def test_move_and_then_display_source(self): |
| 35 | """Test that target.source-map settings work by moving main.c to hidden/main.c.""" |
| 36 | self.buildDefault() |
| 37 | self.move_and_then_display_source() |
| 38 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 39 | def test_modify_source_file_while_debugging(self): |
| 40 | """Modify a source file while debugging the executable.""" |
| 41 | self.buildDefault() |
| 42 | self.modify_source_file_while_debugging() |
| 43 | |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 44 | def display_source_python(self): |
| 45 | """Display source using the SBSourceManager API.""" |
| 46 | exe = os.path.join(os.getcwd(), "a.out") |
| 47 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 48 | |
| 49 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 50 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 51 | |
| 52 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 53 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 54 | |
| 55 | # |
| 56 | # Exercise Python APIs to display source lines. |
| 57 | # |
| 58 | |
| 59 | # Create the filespec for 'main.c'. |
| 60 | filespec = lldb.SBFileSpec('main.c', False) |
| 61 | source_mgr = self.dbg.GetSourceManager() |
| 62 | # Use a string stream as the destination. |
| 63 | stream = lldb.SBStream() |
| 64 | source_mgr.DisplaySourceLinesWithLineNumbers(filespec, |
| 65 | self.line, |
| 66 | 2, # context before |
| 67 | 2, # context after |
| 68 | "=>", # prefix for current line |
| 69 | stream) |
| 70 | |
Johnny Chen | 10889e6 | 2011-03-30 22:28:50 +0000 | [diff] [blame] | 71 | # 2 |
| 72 | # 3 int main(int argc, char const *argv[]) { |
| 73 | # => 4 printf("Hello world.\n"); // Set break point at this line. |
| 74 | # 5 return 0; |
| 75 | # 6 } |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 76 | self.expect(stream.GetData(), "Source code displayed correctly", |
| 77 | exe=False, |
Johnny Chen | 4f8189b | 2011-12-20 00:41:28 +0000 | [diff] [blame] | 78 | patterns = ['=> %d.*Hello world' % self.line]) |
| 79 | |
| 80 | # Boundary condition testings for SBStream(). LLDB should not crash! |
Jim Ingham | 874f43c | 2012-10-05 19:14:57 +0000 | [diff] [blame] | 81 | stream.Print(None) |
Johnny Chen | 4f8189b | 2011-12-20 00:41:28 +0000 | [diff] [blame] | 82 | stream.RedirectToFile(None, True) |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 83 | |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 84 | def move_and_then_display_source(self): |
| 85 | """Test that target.source-map settings work by moving main.c to hidden/main.c.""" |
| 86 | exe = os.path.join(os.getcwd(), "a.out") |
| 87 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 88 | |
| 89 | # Move main.c to hidden/main.c. |
| 90 | main_c = "main.c" |
| 91 | main_c_hidden = os.path.join("hidden", main_c) |
| 92 | os.rename(main_c, main_c_hidden) |
| 93 | |
| 94 | if self.TraceOn(): |
| 95 | system(["ls"]) |
| 96 | system(["ls", "hidden"]) |
| 97 | |
| 98 | # Restore main.c after the test. |
| 99 | self.addTearDownHook(lambda: os.rename(main_c_hidden, main_c)) |
| 100 | |
| 101 | # Set target.source-map settings. |
| 102 | self.runCmd("settings set target.source-map %s %s" % (os.getcwd(), os.path.join(os.getcwd(), "hidden"))) |
| 103 | # And verify that the settings work. |
| 104 | self.expect("settings show target.source-map", |
| 105 | substrs = [os.getcwd(), os.path.join(os.getcwd(), "hidden")]) |
| 106 | |
| 107 | # Display main() and verify that the source mapping has been kicked in. |
Greg Clayton | 12ff126 | 2013-02-06 00:35:33 +0000 | [diff] [blame] | 108 | self.expect("source list -n main", SOURCE_DISPLAYED_CORRECTLY, |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 109 | substrs = ['Hello world']) |
| 110 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 111 | def modify_source_file_while_debugging(self): |
| 112 | """Modify a source file while debugging the executable.""" |
| 113 | exe = os.path.join(os.getcwd(), "a.out") |
| 114 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 115 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 116 | lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True) |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 117 | |
| 118 | self.runCmd("run", RUN_SUCCEEDED) |
| 119 | |
| 120 | # The stop reason of the thread should be breakpoint. |
| 121 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 122 | substrs = ['stopped', |
Johnny Chen | dbee242 | 2011-04-20 20:35:59 +0000 | [diff] [blame] | 123 | 'main.c:%d' % self.line, |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 124 | 'stop reason = breakpoint']) |
| 125 | |
| 126 | # Display some source code. |
Greg Clayton | 12ff126 | 2013-02-06 00:35:33 +0000 | [diff] [blame] | 127 | self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY, |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 128 | substrs = ['Hello world']) |
| 129 | |
Johnny Chen | dbee242 | 2011-04-20 20:35:59 +0000 | [diff] [blame] | 130 | # The '-b' option shows the line table locations from the debug information |
| 131 | # that indicates valid places to set source level breakpoints. |
| 132 | |
| 133 | # The file to display is implicit in this case. |
Greg Clayton | 12ff126 | 2013-02-06 00:35:33 +0000 | [diff] [blame] | 134 | self.runCmd("source list -l %d -c 3 -b" % self.line) |
Johnny Chen | dbee242 | 2011-04-20 20:35:59 +0000 | [diff] [blame] | 135 | output = self.res.GetOutput().splitlines()[0] |
| 136 | |
| 137 | # If the breakpoint set command succeeded, we should expect a positive number |
| 138 | # of breakpoints for the current line, i.e., self.line. |
| 139 | import re |
| 140 | m = re.search('^\[(\d+)\].*// Set break point at this line.', output) |
| 141 | if not m: |
| 142 | self.fail("Fail to display source level breakpoints") |
| 143 | self.assertTrue(int(m.group(1)) > 0) |
| 144 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 145 | # Read the main.c file content. |
| 146 | with open('main.c', 'r') as f: |
| 147 | original_content = f.read() |
Johnny Chen | 7426681 | 2011-04-19 22:11:23 +0000 | [diff] [blame] | 148 | if self.TraceOn(): |
| 149 | print "original content:", original_content |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 150 | |
| 151 | # Modify the in-memory copy of the original source code. |
| 152 | new_content = original_content.replace('Hello world', 'Hello lldb', 1) |
| 153 | |
| 154 | # This is the function to restore the original content. |
| 155 | def restore_file(): |
Johnny Chen | e0ec9ea | 2011-03-04 01:35:22 +0000 | [diff] [blame] | 156 | #print "os.path.getmtime() before restore:", os.path.getmtime('main.c') |
| 157 | time.sleep(1) |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 158 | with open('main.c', 'w') as f: |
| 159 | f.write(original_content) |
Johnny Chen | 7426681 | 2011-04-19 22:11:23 +0000 | [diff] [blame] | 160 | if self.TraceOn(): |
| 161 | with open('main.c', 'r') as f: |
| 162 | print "content restored to:", f.read() |
Johnny Chen | e0ec9ea | 2011-03-04 01:35:22 +0000 | [diff] [blame] | 163 | # Touch the file just to be sure. |
| 164 | os.utime('main.c', None) |
Johnny Chen | 7426681 | 2011-04-19 22:11:23 +0000 | [diff] [blame] | 165 | if self.TraceOn(): |
| 166 | print "os.path.getmtime() after restore:", os.path.getmtime('main.c') |
Johnny Chen | e0ec9ea | 2011-03-04 01:35:22 +0000 | [diff] [blame] | 167 | |
| 168 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 169 | |
| 170 | # Modify the source code file. |
| 171 | with open('main.c', 'w') as f: |
Johnny Chen | e0ec9ea | 2011-03-04 01:35:22 +0000 | [diff] [blame] | 172 | time.sleep(1) |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 173 | f.write(new_content) |
Johnny Chen | 7426681 | 2011-04-19 22:11:23 +0000 | [diff] [blame] | 174 | if self.TraceOn(): |
| 175 | print "new content:", new_content |
| 176 | print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c') |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 177 | # Add teardown hook to restore the file to the original content. |
| 178 | self.addTearDownHook(restore_file) |
| 179 | |
| 180 | # Display the source code again. We should see the updated line. |
Greg Clayton | 12ff126 | 2013-02-06 00:35:33 +0000 | [diff] [blame] | 181 | self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY, |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 182 | substrs = ['Hello lldb']) |
| 183 | |
| 184 | |
| 185 | if __name__ == '__main__': |
| 186 | import atexit |
| 187 | lldb.SBDebugger.Initialize() |
| 188 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 189 | unittest2.main() |