blob: 9442585e0c6a19e3e861b1b653de74f7c30c0971 [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 *
Jim Ingham63dfc722012-09-22 00:05:11 +000015import lldbutil
Johnny Chen5b3a3572010-12-09 18:22:12 +000016
17class SourceManagerTestCase(TestBase):
18
Greg Clayton4570d3e2013-12-10 23:19:29 +000019 mydir = TestBase.compute_mydir(__file__)
Johnny Chen5b3a3572010-12-09 18:22:12 +000020
21 def setUp(self):
22 # Call super's setUp().
23 TestBase.setUp(self)
24 # Find the line number to break inside main().
25 self.line = line_number('main.c', '// Set break point at this line.')
Johnny Chen64bab482011-12-12 21:59:28 +000026 lldb.skip_build_and_cleanup = False
Johnny Chen5b3a3572010-12-09 18:22:12 +000027
Johnny Chenf6eaba82010-12-11 01:20:39 +000028 @python_api_test
29 def test_display_source_python(self):
30 """Test display of source using the SBSourceManager API."""
31 self.buildDefault()
32 self.display_source_python()
33
Johnny Chen64bab482011-12-12 21:59:28 +000034 def test_move_and_then_display_source(self):
35 """Test that target.source-map settings work by moving main.c to hidden/main.c."""
36 self.buildDefault()
37 self.move_and_then_display_source()
38
Johnny Chen5b3a3572010-12-09 18:22:12 +000039 def test_modify_source_file_while_debugging(self):
40 """Modify a source file while debugging the executable."""
41 self.buildDefault()
42 self.modify_source_file_while_debugging()
43
Johnny Chenf6eaba82010-12-11 01:20:39 +000044 def display_source_python(self):
45 """Display source using the SBSourceManager API."""
46 exe = os.path.join(os.getcwd(), "a.out")
47 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
48
49 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000050 self.assertTrue(target, VALID_TARGET)
Johnny Chenf6eaba82010-12-11 01:20:39 +000051
52 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000053 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenf6eaba82010-12-11 01:20:39 +000054
55 #
56 # Exercise Python APIs to display source lines.
57 #
58
59 # Create the filespec for 'main.c'.
60 filespec = lldb.SBFileSpec('main.c', False)
61 source_mgr = self.dbg.GetSourceManager()
62 # Use a string stream as the destination.
63 stream = lldb.SBStream()
64 source_mgr.DisplaySourceLinesWithLineNumbers(filespec,
65 self.line,
66 2, # context before
67 2, # context after
68 "=>", # prefix for current line
69 stream)
70
Johnny Chen10889e62011-03-30 22:28:50 +000071 # 2
72 # 3 int main(int argc, char const *argv[]) {
73 # => 4 printf("Hello world.\n"); // Set break point at this line.
74 # 5 return 0;
75 # 6 }
Johnny Chenf6eaba82010-12-11 01:20:39 +000076 self.expect(stream.GetData(), "Source code displayed correctly",
77 exe=False,
Johnny Chen4f8189b2011-12-20 00:41:28 +000078 patterns = ['=> %d.*Hello world' % self.line])
79
80 # Boundary condition testings for SBStream(). LLDB should not crash!
Jim Ingham874f43c2012-10-05 19:14:57 +000081 stream.Print(None)
Johnny Chen4f8189b2011-12-20 00:41:28 +000082 stream.RedirectToFile(None, True)
Johnny Chenf6eaba82010-12-11 01:20:39 +000083
Johnny Chen64bab482011-12-12 21:59:28 +000084 def move_and_then_display_source(self):
85 """Test that target.source-map settings work by moving main.c to hidden/main.c."""
86 exe = os.path.join(os.getcwd(), "a.out")
87 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
88
89 # Move main.c to hidden/main.c.
90 main_c = "main.c"
91 main_c_hidden = os.path.join("hidden", main_c)
92 os.rename(main_c, main_c_hidden)
93
94 if self.TraceOn():
Zachary Turner9ef307b2014-07-22 16:19:29 +000095 system([["ls"]])
96 system([["ls", "hidden"]])
Johnny Chen64bab482011-12-12 21:59:28 +000097
98 # Restore main.c after the test.
99 self.addTearDownHook(lambda: os.rename(main_c_hidden, main_c))
100
101 # Set target.source-map settings.
102 self.runCmd("settings set target.source-map %s %s" % (os.getcwd(), os.path.join(os.getcwd(), "hidden")))
103 # And verify that the settings work.
104 self.expect("settings show target.source-map",
105 substrs = [os.getcwd(), os.path.join(os.getcwd(), "hidden")])
106
107 # Display main() and verify that the source mapping has been kicked in.
Greg Clayton12ff1262013-02-06 00:35:33 +0000108 self.expect("source list -n main", SOURCE_DISPLAYED_CORRECTLY,
Johnny Chen64bab482011-12-12 21:59:28 +0000109 substrs = ['Hello world'])
110
Johnny Chen5b3a3572010-12-09 18:22:12 +0000111 def modify_source_file_while_debugging(self):
112 """Modify a source file while debugging the executable."""
113 exe = os.path.join(os.getcwd(), "a.out")
114 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
115
Jim Ingham63dfc722012-09-22 00:05:11 +0000116 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000117
Siva Chandra3154aa22015-05-27 22:27:41 +0000118 self.runCmd("run", RUN_FAILED)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000119
120 # The stop reason of the thread should be breakpoint.
121 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +0000122 substrs = ['stopped',
Johnny Chendbee2422011-04-20 20:35:59 +0000123 'main.c:%d' % self.line,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000124 'stop reason = breakpoint'])
125
126 # Display some source code.
Greg Clayton12ff1262013-02-06 00:35:33 +0000127 self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000128 substrs = ['Hello world'])
129
Johnny Chendbee2422011-04-20 20:35:59 +0000130 # The '-b' option shows the line table locations from the debug information
131 # that indicates valid places to set source level breakpoints.
132
133 # The file to display is implicit in this case.
Greg Clayton12ff1262013-02-06 00:35:33 +0000134 self.runCmd("source list -l %d -c 3 -b" % self.line)
Johnny Chendbee2422011-04-20 20:35:59 +0000135 output = self.res.GetOutput().splitlines()[0]
136
137 # If the breakpoint set command succeeded, we should expect a positive number
138 # of breakpoints for the current line, i.e., self.line.
139 import re
140 m = re.search('^\[(\d+)\].*// Set break point at this line.', output)
141 if not m:
142 self.fail("Fail to display source level breakpoints")
143 self.assertTrue(int(m.group(1)) > 0)
144
Johnny Chen5b3a3572010-12-09 18:22:12 +0000145 # Read the main.c file content.
146 with open('main.c', 'r') as f:
147 original_content = f.read()
Johnny Chen74266812011-04-19 22:11:23 +0000148 if self.TraceOn():
149 print "original content:", original_content
Johnny Chen5b3a3572010-12-09 18:22:12 +0000150
151 # Modify the in-memory copy of the original source code.
152 new_content = original_content.replace('Hello world', 'Hello lldb', 1)
153
154 # This is the function to restore the original content.
155 def restore_file():
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000156 #print "os.path.getmtime() before restore:", os.path.getmtime('main.c')
157 time.sleep(1)
Zachary Turner8f3f7bea2015-01-15 22:53:44 +0000158 with open('main.c', 'wb') as f:
Johnny Chen5b3a3572010-12-09 18:22:12 +0000159 f.write(original_content)
Johnny Chen74266812011-04-19 22:11:23 +0000160 if self.TraceOn():
161 with open('main.c', 'r') as f:
162 print "content restored to:", f.read()
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000163 # Touch the file just to be sure.
164 os.utime('main.c', None)
Johnny Chen74266812011-04-19 22:11:23 +0000165 if self.TraceOn():
166 print "os.path.getmtime() after restore:", os.path.getmtime('main.c')
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000167
168
Johnny Chen5b3a3572010-12-09 18:22:12 +0000169
170 # Modify the source code file.
Zachary Turner8f3f7bea2015-01-15 22:53:44 +0000171 with open('main.c', 'wb') as f:
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000172 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000173 f.write(new_content)
Johnny Chen74266812011-04-19 22:11:23 +0000174 if self.TraceOn():
175 print "new content:", new_content
176 print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c')
Johnny Chen5b3a3572010-12-09 18:22:12 +0000177 # Add teardown hook to restore the file to the original content.
178 self.addTearDownHook(restore_file)
179
180 # Display the source code again. We should see the updated line.
Greg Clayton12ff1262013-02-06 00:35:33 +0000181 self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000182 substrs = ['Hello lldb'])
183
184
185if __name__ == '__main__':
186 import atexit
187 lldb.SBDebugger.Initialize()
188 atexit.register(lambda: lldb.SBDebugger.Terminate())
189 unittest2.main()