TestSourceManager.py revision 931449ee91b1a3acf0cd8ab2e1dd1285d7c42ad4
1""" 2Test lldb core component: SourceManager. 3 4Test cases: 5 6o test_display_source_python: 7 Test display of source using the SBSourceManager API. 8o test_modify_source_file_while_debugging: 9 Test the caching mechanism of the source manager. 10""" 11 12import unittest2 13import lldb 14from 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 lldb.skip_build_and_cleanup = False 26 27 @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 33 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 38 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 43 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) 49 self.assertTrue(target, VALID_TARGET) 50 51 # Launch the process, and do not stop at the entry point. 52 process = target.LaunchSimple(None, None, os.getcwd()) 53 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 70 # 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 } 75 self.expect(stream.GetData(), "Source code displayed correctly", 76 exe=False, 77 patterns = ['=> %d.*Hello world' % self.line]) 78 79 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 106 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, 120 substrs = ['stopped', 121 'main.c:%d' % self.line, 122 '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 128 # 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 143 # Read the main.c file content. 144 with open('main.c', 'r') as f: 145 original_content = f.read() 146 if self.TraceOn(): 147 print "original content:", original_content 148 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(): 154 #print "os.path.getmtime() before restore:", os.path.getmtime('main.c') 155 time.sleep(1) 156 with open('main.c', 'w') as f: 157 f.write(original_content) 158 if self.TraceOn(): 159 with open('main.c', 'r') as f: 160 print "content restored to:", f.read() 161 # Touch the file just to be sure. 162 os.utime('main.c', None) 163 if self.TraceOn(): 164 print "os.path.getmtime() after restore:", os.path.getmtime('main.c') 165 166 167 168 # Modify the source code file. 169 with open('main.c', 'w') as f: 170 time.sleep(1) 171 f.write(new_content) 172 if self.TraceOn(): 173 print "new content:", new_content 174 print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c') 175 # 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() 188