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 12from __future__ import print_function 13 14import lldb 15from lldbsuite.test.decorators import * 16from lldbsuite.test.lldbtest import * 17from lldbsuite.test import lldbutil 18 19 20def ansi_underline_surround_regex(inner_regex_text): 21 # return re.compile(r"\[4m%s\[0m" % inner_regex_text) 22 return "4.+\033\\[4m%s\033\\[0m" % inner_regex_text 23 24def ansi_color_surround_regex(inner_regex_text): 25 return "\033\\[3[0-7]m%s\033\\[0m" % inner_regex_text 26 27class SourceManagerTestCase(TestBase): 28 29 mydir = TestBase.compute_mydir(__file__) 30 31 NO_DEBUG_INFO_TESTCASE = True 32 33 def setUp(self): 34 # Call super's setUp(). 35 TestBase.setUp(self) 36 # Find the line number to break inside main(). 37 self.file = self.getBuildArtifact("main-copy.c") 38 self.line = line_number("main.c", '// Set break point at this line.') 39 40 def get_expected_stop_column_number(self): 41 """Return the 1-based column number of the first non-whitespace 42 character in the breakpoint source line.""" 43 stop_line = get_line(self.file, self.line) 44 # The number of spaces that must be skipped to get to the first non- 45 # whitespace character --- where we expect the debugger breakpoint 46 # column to be --- is equal to the number of characters that get 47 # stripped off the front when we lstrip it, plus one to specify 48 # the character column after the initial whitespace. 49 return len(stop_line) - len(stop_line.lstrip()) + 1 50 51 def do_display_source_python_api(self, use_color, needle_regex, highlight_source=False): 52 self.build() 53 exe = self.getBuildArtifact("a.out") 54 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 55 56 target = self.dbg.CreateTarget(exe) 57 self.assertTrue(target, VALID_TARGET) 58 59 # Launch the process, and do not stop at the entry point. 60 args = None 61 envp = None 62 process = target.LaunchSimple( 63 args, envp, self.get_process_working_directory()) 64 self.assertIsNotNone(process) 65 66 # 67 # Exercise Python APIs to display source lines. 68 # 69 70 # Setup whether we should use ansi escape sequences, including color 71 # and styles such as underline. 72 self.dbg.SetUseColor(use_color) 73 # Disable syntax highlighting if needed. 74 75 self.runCmd("settings set highlight-source " + str(highlight_source).lower()) 76 77 filespec = lldb.SBFileSpec(self.file, False) 78 source_mgr = self.dbg.GetSourceManager() 79 # Use a string stream as the destination. 80 stream = lldb.SBStream() 81 column = self.get_expected_stop_column_number() 82 context_before = 2 83 context_after = 2 84 current_line_prefix = "=>" 85 source_mgr.DisplaySourceLinesWithLineNumbersAndColumn( 86 filespec, self.line, column, context_before, context_after, 87 current_line_prefix, stream) 88 89 # 2 90 # 3 int main(int argc, char const *argv[]) { 91 # => 4 printf("Hello world.\n"); // Set break point at this line. 92 # 5 return 0; 93 # 6 } 94 self.expect(stream.GetData(), "Source code displayed correctly:\n" + stream.GetData(), 95 exe=False, 96 patterns=['=>', '%d.*Hello world' % self.line, 97 needle_regex]) 98 99 # Boundary condition testings for SBStream(). LLDB should not crash! 100 stream.Print(None) 101 stream.RedirectToFile(None, True) 102 103 @add_test_categories(['pyapi']) 104 def test_display_source_python_dumb_terminal(self): 105 """Test display of source using the SBSourceManager API, using a 106 dumb terminal and thus no color support (the default).""" 107 use_color = False 108 self.do_display_source_python_api(use_color, r"\s+\^") 109 110 @add_test_categories(['pyapi']) 111 def test_display_source_python_ansi_terminal(self): 112 """Test display of source using the SBSourceManager API, using a 113 dumb terminal and thus no color support (the default).""" 114 use_color = True 115 underline_regex = ansi_underline_surround_regex(r"printf") 116 self.do_display_source_python_api(use_color, underline_regex) 117 118 @add_test_categories(['pyapi']) 119 def test_display_source_python_ansi_terminal_syntax_highlighting(self): 120 """Test display of source using the SBSourceManager API and check for 121 the syntax highlighted output""" 122 use_color = True 123 syntax_highlighting = True; 124 125 # Just pick 'int' as something that should be colored. 126 color_regex = ansi_color_surround_regex("int") 127 self.do_display_source_python_api(use_color, color_regex, syntax_highlighting) 128 129 # Same for 'char'. 130 color_regex = ansi_color_surround_regex("char") 131 self.do_display_source_python_api(use_color, color_regex, syntax_highlighting) 132 133 # Test that we didn't color unrelated identifiers. 134 self.do_display_source_python_api(use_color, r" main\(", syntax_highlighting) 135 self.do_display_source_python_api(use_color, r"\);", syntax_highlighting) 136 137 def test_move_and_then_display_source(self): 138 """Test that target.source-map settings work by moving main.c to hidden/main.c.""" 139 self.build() 140 exe = self.getBuildArtifact("a.out") 141 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 142 143 # Move main.c to hidden/main.c. 144 hidden = self.getBuildArtifact("hidden") 145 lldbutil.mkdir_p(hidden) 146 main_c_hidden = os.path.join(hidden, "main-copy.c") 147 os.rename(self.file, main_c_hidden) 148 149 # Set source remapping with invalid replace path and verify we get an 150 # error 151 self.expect( 152 "settings set target.source-map /a/b/c/d/e /q/r/s/t/u", 153 error=True, 154 substrs=['''error: the replacement path doesn't exist: "/q/r/s/t/u"''']) 155 156 # 'make -C' has resolved current directory to its realpath form. 157 builddir_real = os.path.realpath(self.getBuildDir()) 158 hidden_real = os.path.realpath(hidden) 159 # Set target.source-map settings. 160 self.runCmd("settings set target.source-map %s %s" % 161 (builddir_real, hidden_real)) 162 # And verify that the settings work. 163 self.expect("settings show target.source-map", 164 substrs=[builddir_real, hidden_real]) 165 166 # Display main() and verify that the source mapping has been kicked in. 167 self.expect("source list -n main", SOURCE_DISPLAYED_CORRECTLY, 168 substrs=['Hello world']) 169 170 @skipIf(oslist=["windows"], bugnumber="llvm.org/pr44431") 171 def test_modify_source_file_while_debugging(self): 172 """Modify a source file while debugging the executable.""" 173 self.build() 174 exe = self.getBuildArtifact("a.out") 175 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 176 177 lldbutil.run_break_set_by_file_and_line( 178 self, "main-copy.c", self.line, num_expected_locations=1, loc_exact=True) 179 180 self.runCmd("run", RUN_SUCCEEDED) 181 182 # The stop reason of the thread should be breakpoint. 183 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 184 substrs=['stopped', 185 'main-copy.c:%d' % self.line, 186 'stop reason = breakpoint']) 187 188 # Display some source code. 189 self.expect( 190 "source list -f main-copy.c -l %d" % 191 self.line, 192 SOURCE_DISPLAYED_CORRECTLY, 193 substrs=['Hello world']) 194 195 # Do the same thing with a file & line spec: 196 self.expect( 197 "source list -y main-copy.c:%d" % 198 self.line, 199 SOURCE_DISPLAYED_CORRECTLY, 200 substrs=['Hello world']) 201 202 # The '-b' option shows the line table locations from the debug information 203 # that indicates valid places to set source level breakpoints. 204 205 # The file to display is implicit in this case. 206 self.runCmd("source list -l %d -c 3 -b" % self.line) 207 output = self.res.GetOutput().splitlines()[0] 208 209 # If the breakpoint set command succeeded, we should expect a positive number 210 # of breakpoints for the current line, i.e., self.line. 211 import re 212 m = re.search('^\[(\d+)\].*// Set break point at this line.', output) 213 if not m: 214 self.fail("Fail to display source level breakpoints") 215 self.assertTrue(int(m.group(1)) > 0) 216 217 # Read the main.c file content. 218 with io.open(self.file, 'r', newline='\n') as f: 219 original_content = f.read() 220 if self.TraceOn(): 221 print("original content:", original_content) 222 223 # Modify the in-memory copy of the original source code. 224 new_content = original_content.replace('Hello world', 'Hello lldb', 1) 225 226 # Modify the source code file. 227 with io.open(self.file, 'w', newline='\n') as f: 228 time.sleep(1) 229 f.write(new_content) 230 if self.TraceOn(): 231 print("new content:", new_content) 232 print( 233 "os.path.getmtime() after writing new content:", 234 os.path.getmtime(self.file)) 235 236 # Display the source code again. We should see the updated line. 237 self.expect( 238 "source list -f main-copy.c -l %d" % 239 self.line, 240 SOURCE_DISPLAYED_CORRECTLY, 241 substrs=['Hello lldb']) 242 243 def test_set_breakpoint_with_absolute_path(self): 244 self.build() 245 hidden = self.getBuildArtifact("hidden") 246 lldbutil.mkdir_p(hidden) 247 # 'make -C' has resolved current directory to its realpath form. 248 builddir_real = os.path.realpath(self.getBuildDir()) 249 hidden_real = os.path.realpath(hidden) 250 self.runCmd("settings set target.source-map %s %s" % 251 (builddir_real, hidden_real)) 252 253 exe = self.getBuildArtifact("a.out") 254 main = os.path.join(builddir_real, "hidden", "main-copy.c") 255 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 256 257 lldbutil.run_break_set_by_file_and_line( 258 self, main, self.line, num_expected_locations=1, loc_exact=False) 259 260 self.runCmd("run", RUN_SUCCEEDED) 261 262 # The stop reason of the thread should be breakpoint. 263 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 264 substrs=['stopped', 265 'main-copy.c:%d' % self.line, 266 'stop reason = breakpoint']) 267 268 def test_artificial_source_location(self): 269 src_file = 'artificial_location.c' 270 d = {'C_SOURCES': src_file } 271 self.build(dictionary=d) 272 273 lldbutil.run_to_source_breakpoint( 274 self, 'main', 275 lldb.SBFileSpec(src_file, False)) 276 277 self.expect("run", RUN_SUCCEEDED, 278 substrs=['stop reason = breakpoint', '%s:%d' % (src_file,0), 279 'Note: this address is compiler-generated code in ' 280 'function', 'that has no source code associated ' 281 'with it.']) 282 283