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