blob: f1a4cf21ca91e3891c90f74e88462468e16048c0 [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.')
Johnny Chen64bab482011-12-12 21:59:28 +000025 lldb.skip_build_and_cleanup = False
Johnny Chen5b3a3572010-12-09 18:22:12 +000026
Johnny Chenf6eaba82010-12-11 01:20:39 +000027 @python_api_test
28 def test_display_source_python(self):
29 """Test display of source using the SBSourceManager API."""
30 self.buildDefault()
31 self.display_source_python()
32
Johnny Chen64bab482011-12-12 21:59:28 +000033 def test_move_and_then_display_source(self):
34 """Test that target.source-map settings work by moving main.c to hidden/main.c."""
35 self.buildDefault()
36 self.move_and_then_display_source()
37
Johnny Chen5b3a3572010-12-09 18:22:12 +000038 def test_modify_source_file_while_debugging(self):
39 """Modify a source file while debugging the executable."""
40 self.buildDefault()
41 self.modify_source_file_while_debugging()
42
Johnny Chenf6eaba82010-12-11 01:20:39 +000043 def display_source_python(self):
44 """Display source using the SBSourceManager API."""
45 exe = os.path.join(os.getcwd(), "a.out")
46 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
47
48 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000049 self.assertTrue(target, VALID_TARGET)
Johnny Chenf6eaba82010-12-11 01:20:39 +000050
51 # Launch the process, and do not stop at the entry point.
Johnny Chen1d3e8802011-07-11 23:38:23 +000052 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chenf6eaba82010-12-11 01:20:39 +000053
54 #
55 # Exercise Python APIs to display source lines.
56 #
57
58 # Create the filespec for 'main.c'.
59 filespec = lldb.SBFileSpec('main.c', False)
60 source_mgr = self.dbg.GetSourceManager()
61 # Use a string stream as the destination.
62 stream = lldb.SBStream()
63 source_mgr.DisplaySourceLinesWithLineNumbers(filespec,
64 self.line,
65 2, # context before
66 2, # context after
67 "=>", # prefix for current line
68 stream)
69
Johnny Chen10889e62011-03-30 22:28:50 +000070 # 2
71 # 3 int main(int argc, char const *argv[]) {
72 # => 4 printf("Hello world.\n"); // Set break point at this line.
73 # 5 return 0;
74 # 6 }
Johnny Chenf6eaba82010-12-11 01:20:39 +000075 self.expect(stream.GetData(), "Source code displayed correctly",
76 exe=False,
Johnny Chen10889e62011-03-30 22:28:50 +000077 patterns = ['=> %d.*Hello world' % self.line])
Johnny Chenf6eaba82010-12-11 01:20:39 +000078
Johnny Chen64bab482011-12-12 21:59:28 +000079 def move_and_then_display_source(self):
80 """Test that target.source-map settings work by moving main.c to hidden/main.c."""
81 exe = os.path.join(os.getcwd(), "a.out")
82 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
83
84 # Move main.c to hidden/main.c.
85 main_c = "main.c"
86 main_c_hidden = os.path.join("hidden", main_c)
87 os.rename(main_c, main_c_hidden)
88
89 if self.TraceOn():
90 system(["ls"])
91 system(["ls", "hidden"])
92
93 # Restore main.c after the test.
94 self.addTearDownHook(lambda: os.rename(main_c_hidden, main_c))
95
96 # Set target.source-map settings.
97 self.runCmd("settings set target.source-map %s %s" % (os.getcwd(), os.path.join(os.getcwd(), "hidden")))
98 # And verify that the settings work.
99 self.expect("settings show target.source-map",
100 substrs = [os.getcwd(), os.path.join(os.getcwd(), "hidden")])
101
102 # Display main() and verify that the source mapping has been kicked in.
103 self.expect("list -n main", SOURCE_DISPLAYED_CORRECTLY,
104 substrs = ['Hello world'])
105
Johnny Chen5b3a3572010-12-09 18:22:12 +0000106 def modify_source_file_while_debugging(self):
107 """Modify a source file while debugging the executable."""
108 exe = os.path.join(os.getcwd(), "a.out")
109 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
110
111 self.expect("breakpoint set -f main.c -l %d" % self.line,
112 BREAKPOINT_CREATED,
113 startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
114 self.line)
115
116 self.runCmd("run", RUN_SUCCEEDED)
117
118 # The stop reason of the thread should be breakpoint.
119 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +0000120 substrs = ['stopped',
Johnny Chendbee2422011-04-20 20:35:59 +0000121 'main.c:%d' % self.line,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000122 'stop reason = breakpoint'])
123
124 # Display some source code.
125 self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
126 substrs = ['Hello world'])
127
Johnny Chendbee2422011-04-20 20:35:59 +0000128 # The '-b' option shows the line table locations from the debug information
129 # that indicates valid places to set source level breakpoints.
130
131 # The file to display is implicit in this case.
132 self.runCmd("list -l %d -c 3 -b" % self.line)
133 output = self.res.GetOutput().splitlines()[0]
134
135 # If the breakpoint set command succeeded, we should expect a positive number
136 # of breakpoints for the current line, i.e., self.line.
137 import re
138 m = re.search('^\[(\d+)\].*// Set break point at this line.', output)
139 if not m:
140 self.fail("Fail to display source level breakpoints")
141 self.assertTrue(int(m.group(1)) > 0)
142
Johnny Chen5b3a3572010-12-09 18:22:12 +0000143 # Read the main.c file content.
144 with open('main.c', 'r') as f:
145 original_content = f.read()
Johnny Chen74266812011-04-19 22:11:23 +0000146 if self.TraceOn():
147 print "original content:", original_content
Johnny Chen5b3a3572010-12-09 18:22:12 +0000148
149 # Modify the in-memory copy of the original source code.
150 new_content = original_content.replace('Hello world', 'Hello lldb', 1)
151
152 # This is the function to restore the original content.
153 def restore_file():
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000154 #print "os.path.getmtime() before restore:", os.path.getmtime('main.c')
155 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000156 with open('main.c', 'w') as f:
157 f.write(original_content)
Johnny Chen74266812011-04-19 22:11:23 +0000158 if self.TraceOn():
159 with open('main.c', 'r') as f:
160 print "content restored to:", f.read()
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000161 # Touch the file just to be sure.
162 os.utime('main.c', None)
Johnny Chen74266812011-04-19 22:11:23 +0000163 if self.TraceOn():
164 print "os.path.getmtime() after restore:", os.path.getmtime('main.c')
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000165
166
Johnny Chen5b3a3572010-12-09 18:22:12 +0000167
168 # Modify the source code file.
169 with open('main.c', 'w') as f:
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000170 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000171 f.write(new_content)
Johnny Chen74266812011-04-19 22:11:23 +0000172 if self.TraceOn():
173 print "new content:", new_content
174 print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c')
Johnny Chen5b3a3572010-12-09 18:22:12 +0000175 # Add teardown hook to restore the file to the original content.
176 self.addTearDownHook(restore_file)
177
178 # Display the source code again. We should see the updated line.
179 self.expect("list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
180 substrs = ['Hello lldb'])
181
182
183if __name__ == '__main__':
184 import atexit
185 lldb.SBDebugger.Initialize()
186 atexit.register(lambda: lldb.SBDebugger.Terminate())
187 unittest2.main()