blob: 56b9dd9fa645a52c631ed63eed5744dbdc9d0f43 [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)
Johnny Chen4ebd0192011-05-24 18:22:45 +000043 self.assertTrue(target, VALID_TARGET)
Johnny Chenf6eaba82010-12-11 01:20:39 +000044
45 # Launch the process, and do not stop at the entry point.
Johnny Chen1d3e8802011-07-11 23:38:23 +000046 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chenf6eaba82010-12-11 01:20:39 +000047
48 #
49 # Exercise Python APIs to display source lines.
50 #
51
52 # Create the filespec for 'main.c'.
53 filespec = lldb.SBFileSpec('main.c', False)
54 source_mgr = self.dbg.GetSourceManager()
55 # Use a string stream as the destination.
56 stream = lldb.SBStream()
57 source_mgr.DisplaySourceLinesWithLineNumbers(filespec,
58 self.line,
59 2, # context before
60 2, # context after
61 "=>", # prefix for current line
62 stream)
63
Johnny Chen10889e62011-03-30 22:28:50 +000064 # 2
65 # 3 int main(int argc, char const *argv[]) {
66 # => 4 printf("Hello world.\n"); // Set break point at this line.
67 # 5 return 0;
68 # 6 }
Johnny Chenf6eaba82010-12-11 01:20:39 +000069 self.expect(stream.GetData(), "Source code displayed correctly",
70 exe=False,
Johnny Chen10889e62011-03-30 22:28:50 +000071 patterns = ['=> %d.*Hello world' % self.line])
Johnny Chenf6eaba82010-12-11 01:20:39 +000072
Johnny Chen5b3a3572010-12-09 18:22:12 +000073 def modify_source_file_while_debugging(self):
74 """Modify a source file while debugging the executable."""
75 exe = os.path.join(os.getcwd(), "a.out")
76 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
77
78 self.expect("breakpoint set -f main.c -l %d" % self.line,
79 BREAKPOINT_CREATED,
80 startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
81 self.line)
82
83 self.runCmd("run", RUN_SUCCEEDED)
84
85 # The stop reason of the thread should be breakpoint.
86 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000087 substrs = ['stopped',
Johnny Chendbee2422011-04-20 20:35:59 +000088 'main.c:%d' % self.line,
Johnny Chen5b3a3572010-12-09 18:22:12 +000089 'stop reason = breakpoint'])
90
91 # Display some source code.
92 self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
93 substrs = ['Hello world'])
94
Johnny Chendbee2422011-04-20 20:35:59 +000095 # The '-b' option shows the line table locations from the debug information
96 # that indicates valid places to set source level breakpoints.
97
98 # The file to display is implicit in this case.
99 self.runCmd("list -l %d -c 3 -b" % self.line)
100 output = self.res.GetOutput().splitlines()[0]
101
102 # If the breakpoint set command succeeded, we should expect a positive number
103 # of breakpoints for the current line, i.e., self.line.
104 import re
105 m = re.search('^\[(\d+)\].*// Set break point at this line.', output)
106 if not m:
107 self.fail("Fail to display source level breakpoints")
108 self.assertTrue(int(m.group(1)) > 0)
109
Johnny Chen5b3a3572010-12-09 18:22:12 +0000110 # Read the main.c file content.
111 with open('main.c', 'r') as f:
112 original_content = f.read()
Johnny Chen74266812011-04-19 22:11:23 +0000113 if self.TraceOn():
114 print "original content:", original_content
Johnny Chen5b3a3572010-12-09 18:22:12 +0000115
116 # Modify the in-memory copy of the original source code.
117 new_content = original_content.replace('Hello world', 'Hello lldb', 1)
118
119 # This is the function to restore the original content.
120 def restore_file():
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000121 #print "os.path.getmtime() before restore:", os.path.getmtime('main.c')
122 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000123 with open('main.c', 'w') as f:
124 f.write(original_content)
Johnny Chen74266812011-04-19 22:11:23 +0000125 if self.TraceOn():
126 with open('main.c', 'r') as f:
127 print "content restored to:", f.read()
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000128 # Touch the file just to be sure.
129 os.utime('main.c', None)
Johnny Chen74266812011-04-19 22:11:23 +0000130 if self.TraceOn():
131 print "os.path.getmtime() after restore:", os.path.getmtime('main.c')
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000132
133
Johnny Chen5b3a3572010-12-09 18:22:12 +0000134
135 # Modify the source code file.
136 with open('main.c', 'w') as f:
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000137 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000138 f.write(new_content)
Johnny Chen74266812011-04-19 22:11:23 +0000139 if self.TraceOn():
140 print "new content:", new_content
141 print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c')
Johnny Chen5b3a3572010-12-09 18:22:12 +0000142 # Add teardown hook to restore the file to the original content.
143 self.addTearDownHook(restore_file)
144
145 # Display the source code again. We should see the updated line.
146 self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
147 substrs = ['Hello lldb'])
148
149
150if __name__ == '__main__':
151 import atexit
152 lldb.SBDebugger.Initialize()
153 atexit.register(lambda: lldb.SBDebugger.Terminate())
154 unittest2.main()