blob: 9a1ceeae70a67617dee98cc360ec4f79ffb195fa [file] [log] [blame]
Johnny Chen5b3a3572010-12-09 18:22:12 +00001"""
2Test lldb core component: SourceManager.
3
4Test cases:
Johnny Chende2c8bd2010-12-09 22:06:05 +00005
6o test_modify_source_file_while_debugging:
7 Test the caching mechanism of the source manager.
Johnny Chen5b3a3572010-12-09 18:22:12 +00008"""
9
Johnny Chen5b3a3572010-12-09 18:22:12 +000010import unittest2
Johnny Chen67f73ac2010-12-09 18:38:52 +000011import lldb
Johnny Chen5b3a3572010-12-09 18:22:12 +000012from lldbtest import *
13
14class 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
78if __name__ == '__main__':
79 import atexit
80 lldb.SBDebugger.Initialize()
81 atexit.register(lambda: lldb.SBDebugger.Terminate())
82 unittest2.main()