blob: 9beaacefa6287aa520916bbe328537e88d2c2086 [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
Zachary Turner35d017f2015-10-23 17:04:29 +000012from __future__ import print_function
13
Zachary Turner77db4a82015-10-22 20:06:20 +000014import lldb_shared
15
Johnny Chen67f73ac2010-12-09 18:38:52 +000016import lldb
Johnny Chen5b3a3572010-12-09 18:22:12 +000017from lldbtest import *
Jim Ingham63dfc722012-09-22 00:05:11 +000018import lldbutil
Johnny Chen5b3a3572010-12-09 18:22:12 +000019
20class SourceManagerTestCase(TestBase):
21
Greg Clayton4570d3e2013-12-10 23:19:29 +000022 mydir = TestBase.compute_mydir(__file__)
Johnny Chen5b3a3572010-12-09 18:22:12 +000023
24 def setUp(self):
25 # Call super's setUp().
26 TestBase.setUp(self)
27 # Find the line number to break inside main().
28 self.line = line_number('main.c', '// Set break point at this line.')
Johnny Chen64bab482011-12-12 21:59:28 +000029 lldb.skip_build_and_cleanup = False
Johnny Chen5b3a3572010-12-09 18:22:12 +000030
Johnny Chenf6eaba82010-12-11 01:20:39 +000031 @python_api_test
32 def test_display_source_python(self):
33 """Test display of source using the SBSourceManager API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000034 self.build()
Johnny Chenf6eaba82010-12-11 01:20:39 +000035 exe = os.path.join(os.getcwd(), "a.out")
36 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
37
38 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000039 self.assertTrue(target, VALID_TARGET)
Johnny Chenf6eaba82010-12-11 01:20:39 +000040
41 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000042 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenf6eaba82010-12-11 01:20:39 +000043
44 #
45 # Exercise Python APIs to display source lines.
46 #
47
48 # Create the filespec for 'main.c'.
49 filespec = lldb.SBFileSpec('main.c', False)
50 source_mgr = self.dbg.GetSourceManager()
51 # Use a string stream as the destination.
52 stream = lldb.SBStream()
53 source_mgr.DisplaySourceLinesWithLineNumbers(filespec,
54 self.line,
55 2, # context before
56 2, # context after
57 "=>", # prefix for current line
58 stream)
59
Johnny Chen10889e62011-03-30 22:28:50 +000060 # 2
61 # 3 int main(int argc, char const *argv[]) {
62 # => 4 printf("Hello world.\n"); // Set break point at this line.
63 # 5 return 0;
64 # 6 }
Johnny Chenf6eaba82010-12-11 01:20:39 +000065 self.expect(stream.GetData(), "Source code displayed correctly",
66 exe=False,
Johnny Chen4f8189b2011-12-20 00:41:28 +000067 patterns = ['=> %d.*Hello world' % self.line])
68
69 # Boundary condition testings for SBStream(). LLDB should not crash!
Jim Ingham874f43c2012-10-05 19:14:57 +000070 stream.Print(None)
Johnny Chen4f8189b2011-12-20 00:41:28 +000071 stream.RedirectToFile(None, True)
Johnny Chenf6eaba82010-12-11 01:20:39 +000072
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000073 def test_move_and_then_display_source(self):
Johnny Chen64bab482011-12-12 21:59:28 +000074 """Test that target.source-map settings work by moving main.c to hidden/main.c."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000075 self.build()
Johnny Chen64bab482011-12-12 21:59:28 +000076 exe = os.path.join(os.getcwd(), "a.out")
77 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
78
79 # Move main.c to hidden/main.c.
80 main_c = "main.c"
81 main_c_hidden = os.path.join("hidden", main_c)
82 os.rename(main_c, main_c_hidden)
83
84 if self.TraceOn():
Zachary Turner9ef307b2014-07-22 16:19:29 +000085 system([["ls"]])
86 system([["ls", "hidden"]])
Johnny Chen64bab482011-12-12 21:59:28 +000087
88 # Restore main.c after the test.
89 self.addTearDownHook(lambda: os.rename(main_c_hidden, main_c))
90
91 # Set target.source-map settings.
92 self.runCmd("settings set target.source-map %s %s" % (os.getcwd(), os.path.join(os.getcwd(), "hidden")))
93 # And verify that the settings work.
94 self.expect("settings show target.source-map",
95 substrs = [os.getcwd(), os.path.join(os.getcwd(), "hidden")])
96
97 # Display main() and verify that the source mapping has been kicked in.
Greg Clayton12ff1262013-02-06 00:35:33 +000098 self.expect("source list -n main", SOURCE_DISPLAYED_CORRECTLY,
Johnny Chen64bab482011-12-12 21:59:28 +000099 substrs = ['Hello world'])
100
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000101 def test_modify_source_file_while_debugging(self):
Johnny Chen5b3a3572010-12-09 18:22:12 +0000102 """Modify a source file while debugging the executable."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000103 self.build()
Johnny Chen5b3a3572010-12-09 18:22:12 +0000104 exe = os.path.join(os.getcwd(), "a.out")
105 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
106
Jim Ingham63dfc722012-09-22 00:05:11 +0000107 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 +0000108
Sean Callanan05834cd2015-07-01 23:56:30 +0000109 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000110
111 # The stop reason of the thread should be breakpoint.
112 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +0000113 substrs = ['stopped',
Johnny Chendbee2422011-04-20 20:35:59 +0000114 'main.c:%d' % self.line,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000115 'stop reason = breakpoint'])
116
117 # Display some source code.
Greg Clayton12ff1262013-02-06 00:35:33 +0000118 self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000119 substrs = ['Hello world'])
120
Johnny Chendbee2422011-04-20 20:35:59 +0000121 # The '-b' option shows the line table locations from the debug information
122 # that indicates valid places to set source level breakpoints.
123
124 # The file to display is implicit in this case.
Greg Clayton12ff1262013-02-06 00:35:33 +0000125 self.runCmd("source list -l %d -c 3 -b" % self.line)
Johnny Chendbee2422011-04-20 20:35:59 +0000126 output = self.res.GetOutput().splitlines()[0]
127
128 # If the breakpoint set command succeeded, we should expect a positive number
129 # of breakpoints for the current line, i.e., self.line.
130 import re
131 m = re.search('^\[(\d+)\].*// Set break point at this line.', output)
132 if not m:
133 self.fail("Fail to display source level breakpoints")
134 self.assertTrue(int(m.group(1)) > 0)
135
Johnny Chen5b3a3572010-12-09 18:22:12 +0000136 # Read the main.c file content.
137 with open('main.c', 'r') as f:
138 original_content = f.read()
Johnny Chen74266812011-04-19 22:11:23 +0000139 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000140 print("original content:", original_content)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000141
142 # Modify the in-memory copy of the original source code.
143 new_content = original_content.replace('Hello world', 'Hello lldb', 1)
144
145 # This is the function to restore the original content.
146 def restore_file():
Zachary Turner35d017f2015-10-23 17:04:29 +0000147 #print("os.path.getmtime() before restore:", os.path.getmtime('main.c'))
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000148 time.sleep(1)
Zachary Turner8f3f7bea2015-01-15 22:53:44 +0000149 with open('main.c', 'wb') as f:
Johnny Chen5b3a3572010-12-09 18:22:12 +0000150 f.write(original_content)
Johnny Chen74266812011-04-19 22:11:23 +0000151 if self.TraceOn():
152 with open('main.c', 'r') as f:
Zachary Turner35d017f2015-10-23 17:04:29 +0000153 print("content restored to:", f.read())
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000154 # Touch the file just to be sure.
155 os.utime('main.c', None)
Johnny Chen74266812011-04-19 22:11:23 +0000156 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000157 print("os.path.getmtime() after restore:", os.path.getmtime('main.c'))
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000158
159
Johnny Chen5b3a3572010-12-09 18:22:12 +0000160
161 # Modify the source code file.
Zachary Turner8f3f7bea2015-01-15 22:53:44 +0000162 with open('main.c', 'wb') as f:
Johnny Chene0ec9ea2011-03-04 01:35:22 +0000163 time.sleep(1)
Johnny Chen5b3a3572010-12-09 18:22:12 +0000164 f.write(new_content)
Johnny Chen74266812011-04-19 22:11:23 +0000165 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000166 print("new content:", new_content)
167 print("os.path.getmtime() after writing new content:", os.path.getmtime('main.c'))
Johnny Chen5b3a3572010-12-09 18:22:12 +0000168 # Add teardown hook to restore the file to the original content.
169 self.addTearDownHook(restore_file)
170
171 # Display the source code again. We should see the updated line.
Greg Clayton12ff1262013-02-06 00:35:33 +0000172 self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
Johnny Chen5b3a3572010-12-09 18:22:12 +0000173 substrs = ['Hello lldb'])