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 | |
| 6 | o test_modify_source_file_while_debugging: |
| 7 | Test the caching mechanism of the source manager. |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 8 | """ |
| 9 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 10 | import unittest2 |
Johnny Chen | 67f73ac | 2010-12-09 18:38:52 +0000 | [diff] [blame] | 11 | import lldb |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 12 | from lldbtest import * |
| 13 | |
| 14 | class SourceManagerTestCase(TestBase): |
| 15 | |
| 16 | mydir = "source-manager" |
| 17 | |
| 18 | def setUp(self): |
| 19 | # Call super's setUp(). |
| 20 | TestBase.setUp(self) |
| 21 | # Find the line number to break inside main(). |
| 22 | self.line = line_number('main.c', '// Set break point at this line.') |
| 23 | |
| 24 | def test_modify_source_file_while_debugging(self): |
| 25 | """Modify a source file while debugging the executable.""" |
| 26 | self.buildDefault() |
| 27 | self.modify_source_file_while_debugging() |
| 28 | |
| 29 | def modify_source_file_while_debugging(self): |
| 30 | """Modify a source file while debugging the executable.""" |
| 31 | exe = os.path.join(os.getcwd(), "a.out") |
| 32 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 33 | |
| 34 | self.expect("breakpoint set -f main.c -l %d" % self.line, |
| 35 | BREAKPOINT_CREATED, |
| 36 | startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" % |
| 37 | self.line) |
| 38 | |
| 39 | self.runCmd("run", RUN_SUCCEEDED) |
| 40 | |
| 41 | # The stop reason of the thread should be breakpoint. |
| 42 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| 43 | substrs = ['state is stopped', |
| 44 | 'main.c', |
| 45 | 'stop reason = breakpoint']) |
| 46 | |
| 47 | # Display some source code. |
| 48 | self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY, |
| 49 | substrs = ['Hello world']) |
| 50 | |
| 51 | # Read the main.c file content. |
| 52 | with open('main.c', 'r') as f: |
| 53 | original_content = f.read() |
| 54 | print "original content:", original_content |
| 55 | |
| 56 | # Modify the in-memory copy of the original source code. |
| 57 | new_content = original_content.replace('Hello world', 'Hello lldb', 1) |
| 58 | |
| 59 | # This is the function to restore the original content. |
| 60 | def restore_file(): |
| 61 | with open('main.c', 'w') as f: |
| 62 | f.write(original_content) |
| 63 | with open('main.c', 'r') as f: |
| 64 | print "content restored to:", f.read() |
| 65 | |
| 66 | # Modify the source code file. |
| 67 | with open('main.c', 'w') as f: |
| 68 | f.write(new_content) |
| 69 | print "new content:", new_content |
| 70 | # Add teardown hook to restore the file to the original content. |
| 71 | self.addTearDownHook(restore_file) |
| 72 | |
| 73 | # Display the source code again. We should see the updated line. |
| 74 | self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY, |
| 75 | substrs = ['Hello lldb']) |
| 76 | |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | import atexit |
| 80 | lldb.SBDebugger.Initialize() |
| 81 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 82 | unittest2.main() |