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