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