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