Home | History | Annotate | Download | only in command_script
      1 """
      2 Test lldb Python commands.
      3 """
      4 
      5 import os, time
      6 import unittest2
      7 import lldb
      8 from lldbtest import *
      9 
     10 class CmdPythonTestCase(TestBase):
     11 
     12     mydir = os.path.join("functionalities", "command_script")
     13 
     14     @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
     15     @dsym_test
     16     def test_with_dsym (self):
     17         self.buildDsym ()
     18         self.pycmd_tests ()
     19 
     20     @dwarf_test
     21     @skipIfLinux # causes buildbot failures, skip until we can investigate it
     22     def test_with_dwarf (self):
     23         self.buildDwarf ()
     24         self.pycmd_tests ()
     25 
     26     def pycmd_tests (self):
     27         exe = os.path.join (os.getcwd(), "a.out")
     28         self.expect("file " + exe,
     29                     patterns = [ "Current executable set to .*a.out" ])
     30 
     31         self.runCmd("command source py_import")
     32 
     33         # This is the function to remove the custom commands in order to have a
     34         # clean slate for the next test case.
     35         def cleanup():
     36             self.runCmd('command script delete welcome', check=False)
     37             self.runCmd('command script delete targetname', check=False)
     38             self.runCmd('command script delete longwait', check=False)
     39             self.runCmd('command script delete mysto', check=False)
     40             self.runCmd('command script delete tell_sync', check=False)
     41             self.runCmd('command script delete tell_async', check=False)
     42             self.runCmd('command script delete tell_curr', check=False)
     43             self.runCmd('command script delete bug11569', check=False)
     44 
     45         # Execute the cleanup function during test case tear down.
     46         self.addTearDownHook(cleanup)
     47 
     48         # Interact with debugger in synchronous mode
     49         self.setAsync(False)
     50 
     51         # We don't want to display the stdout if not in TraceOn() mode.
     52         if not self.TraceOn():
     53             self.HideStdout()
     54 
     55         self.expect('welcome Enrico',
     56             substrs = ['Hello Enrico, welcome to LLDB']);
     57                 
     58         self.expect("help welcome",
     59                     substrs = ['Just a docstring for welcome_impl',
     60                                'A command that says hello to LLDB users'])
     61 
     62         self.expect("help",
     63                     substrs = ['Run Python function welcome.welcome_impl',
     64                                'welcome'])
     65 
     66         self.expect("help -a",
     67                     substrs = ['Run Python function welcome.welcome_impl',
     68                                'welcome'])
     69 
     70         self.expect("help -u", matching=False,
     71                     substrs = ['Run Python function welcome.welcome_impl',
     72                                'welcome'])
     73 
     74         self.runCmd("command script delete welcome");
     75 
     76         self.expect('welcome Enrico', matching=False, error=True,
     77                 substrs = ['Hello Enrico, welcome to LLDB']);
     78 
     79         self.expect('targetname',
     80             substrs = ['a.out'])
     81 
     82         self.expect('targetname fail', error=True,
     83                     substrs = ['a test for error in command'])
     84 
     85         self.expect('command script list',
     86             substrs = ['targetname',
     87                        'Run Python function welcome.target_name_impl'])
     88 
     89         self.expect("help targetname",
     90                     substrs = ['Run Python function welcome.target_name_imp',
     91                                'This command takes','\'raw\' input',
     92                                'quote stuff'])
     93 
     94         self.expect("longwait",
     95                     substrs = ['Done; if you saw the delays I am doing OK'])
     96 
     97         self.runCmd("b main")
     98         self.runCmd("run")
     99         self.runCmd("mysto 3")
    100         self.expect("frame variable array",
    101                     substrs = ['[0] = 79630','[1] = 388785018','[2] = 0'])
    102         self.runCmd("mysto 3")
    103         self.expect("frame variable array",
    104                     substrs = ['[0] = 79630','[4] = 388785018','[5] = 0'])
    105 
    106 # we cannot use the stepover command to check for async execution mode since LLDB
    107 # seems to get confused when events start to queue up
    108         self.expect("tell_sync",
    109                     substrs = ['running sync'])
    110         self.expect("tell_async",
    111                     substrs = ['running async'])
    112         self.expect("tell_curr",
    113                     substrs = ['I am running sync'])
    114 
    115         # Test that a python command can redefine itself
    116         self.expect('command script add -f foobar welcome')
    117 
    118         self.runCmd("command script clear")
    119 
    120         # Test that re-defining an existing command works
    121         self.runCmd('command script add my_command --function welcome.welcome_impl')
    122         self.expect('my_command Blah', substrs = ['Hello Blah, welcome to LLDB'])
    123 
    124         self.runCmd('command script add my_command --function welcome.target_name_impl')
    125         self.expect('my_command', substrs = ['a.out'])
    126 
    127         self.runCmd("command script clear")
    128                 
    129         self.expect('command script list', matching=False,
    130                     substrs = ['targetname',
    131                                'longwait'])
    132 
    133         self.expect('command script add -f foobar frame', error=True,
    134                     substrs = ['cannot add command'])
    135 
    136         # http://llvm.org/bugs/show_bug.cgi?id=11569
    137         # LLDBSwigPythonCallCommand crashes when a command script returns an object 
    138         self.runCmd('command script add -f bug11569 bug11569')
    139         # This should not crash.
    140         self.runCmd('bug11569', check=False)
    141 
    142 if __name__ == '__main__':
    143     import atexit
    144     lldb.SBDebugger.Initialize()
    145     atexit.register(lambda: lldb.SBDebugger.Terminate())
    146     unittest2.main()
    147 
    148