Home | History | Annotate | Download | only in source-manager
      1 """
      2 Test lldb core component: SourceManager.
      3 
      4 Test cases:
      5 
      6 o test_display_source_python:
      7   Test display of source using the SBSourceManager API.
      8 o test_modify_source_file_while_debugging:
      9   Test the caching mechanism of the source manager.
     10 """
     11 
     12 import unittest2
     13 import lldb
     14 from lldbtest import *
     15 import lldbutil
     16 
     17 class SourceManagerTestCase(TestBase):
     18 
     19     mydir = "source-manager"
     20 
     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.')
     26         lldb.skip_build_and_cleanup = False
     27 
     28     @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 
     34     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 
     39     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 
     44     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)
     50         self.assertTrue(target, VALID_TARGET)
     51 
     52         # Launch the process, and do not stop at the entry point.
     53         process = target.LaunchSimple(None, None, os.getcwd())
     54 
     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 
     71         #    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    }
     76         self.expect(stream.GetData(), "Source code displayed correctly",
     77                     exe=False,
     78             patterns = ['=> %d.*Hello world' % self.line])
     79 
     80         # Boundary condition testings for SBStream().  LLDB should not crash!
     81         stream.Print(None)
     82         stream.RedirectToFile(None, True)
     83 
     84     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():
     95             system(["ls"])
     96             system(["ls", "hidden"])
     97 
     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.
    108         self.expect("source list -n main", SOURCE_DISPLAYED_CORRECTLY,
    109             substrs = ['Hello world'])
    110 
    111     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 
    116         lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
    117 
    118         self.runCmd("run", RUN_SUCCEEDED)
    119 
    120         # The stop reason of the thread should be breakpoint.
    121         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
    122             substrs = ['stopped',
    123                        'main.c:%d' % self.line,
    124                        'stop reason = breakpoint'])
    125 
    126         # Display some source code.
    127         self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
    128             substrs = ['Hello world'])
    129 
    130         # 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.
    134         self.runCmd("source list -l %d -c 3 -b" % self.line)
    135         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 
    145         # Read the main.c file content.
    146         with open('main.c', 'r') as f:
    147             original_content = f.read()
    148             if self.TraceOn():
    149                 print "original content:", original_content
    150 
    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():
    156             #print "os.path.getmtime() before restore:", os.path.getmtime('main.c')
    157             time.sleep(1)
    158             with open('main.c', 'w') as f:
    159                 f.write(original_content)
    160             if self.TraceOn():
    161                 with open('main.c', 'r') as f:
    162                     print "content restored to:", f.read()
    163             # Touch the file just to be sure.
    164             os.utime('main.c', None)
    165             if self.TraceOn():
    166                 print "os.path.getmtime() after restore:", os.path.getmtime('main.c')
    167 
    168 
    169 
    170         # Modify the source code file.
    171         with open('main.c', 'w') as f:
    172             time.sleep(1)
    173             f.write(new_content)
    174             if self.TraceOn():
    175                 print "new content:", new_content
    176                 print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c')
    177             # 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.
    181         self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
    182             substrs = ['Hello lldb'])
    183 
    184 
    185 if __name__ == '__main__':
    186     import atexit
    187     lldb.SBDebugger.Initialize()
    188     atexit.register(lambda: lldb.SBDebugger.Terminate())
    189     unittest2.main()
    190