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