blob: b51cd18bb92e7523a2caf9d1ab0790797341ab9b [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
Johnny Chenf6eaba82010-12-11 01:20:39 +00006o test_display_source_python:
7 Test display of source using the SBSourceManager API.
Johnny Chende2c8bd2010-12-09 22:06:05 +00008o test_modify_source_file_while_debugging:
9 Test the caching mechanism of the source manager.
Johnny Chen5b3a3572010-12-09 18:22:12 +000010"""
11
Johnny Chen5b3a3572010-12-09 18:22:12 +000012import unittest2
Johnny Chen67f73ac2010-12-09 18:38:52 +000013import lldb
Johnny Chen5b3a3572010-12-09 18:22:12 +000014from lldbtest import *
15
16class 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 Chenf6eaba82010-12-11 01:20:39 +000026 @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 Chen5b3a3572010-12-09 18:22:12 +000032 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 Chenf6eaba82010-12-11 01:20:39 +000037 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 Clayton6f907e62011-01-23 17:46:22 +000046 error = lldb.SBError()
Johnny Chend762ff12011-02-03 23:15:53 +000047 process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
Johnny Chenf6eaba82010-12-11 01:20:39 +000048
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
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 }
70 self.expect(stream.GetData(), "Source code displayed correctly",
71 exe=False,
Johnny Chen68300da2010-12-13 18:59:27 +000072 patterns = ['%d =>.*Hello world' % self.line])
Johnny Chenf6eaba82010-12-11 01:20:39 +000073
Johnny Chen5b3a3572010-12-09 18:22:12 +000074 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,
88 substrs = ['state is stopped',
89 'main.c',
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
96 # Read the main.c file content.
97 with open('main.c', 'r') as f:
98 original_content = f.read()
99 print "original content:", original_content
100
101 # Modify the in-memory copy of the original source code.
102 new_content = original_content.replace('Hello world', 'Hello lldb', 1)
103
104 # This is the function to restore the original content.
105 def restore_file():
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000106 #print "os.path.getmtime() before restore:", os.path.getmtime('main.c')
107 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000108 with open('main.c', 'w') as f:
109 f.write(original_content)
110 with open('main.c', 'r') as f:
111 print "content restored to:", f.read()
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000112 # Touch the file just to be sure.
113 os.utime('main.c', None)
114 print "os.path.getmtime() after restore:", os.path.getmtime('main.c')
115
116
Johnny Chen5b3a3572010-12-09 18:22:12 +0000117
118 # Modify the source code file.
119 with open('main.c', 'w') as f:
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000120 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000121 f.write(new_content)
122 print "new content:", new_content
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000123 print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c')
Johnny Chen5b3a3572010-12-09 18:22:12 +0000124 # Add teardown hook to restore the file to the original content.
125 self.addTearDownHook(restore_file)
126
127 # Display the source code again. We should see the updated line.
128 self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
129 substrs = ['Hello lldb'])
130
131
132if __name__ == '__main__':
133 import atexit
134 lldb.SBDebugger.Initialize()
135 atexit.register(lambda: lldb.SBDebugger.Terminate())
136 unittest2.main()