xref: /llvm-project/lldb/test/API/commands/command/script/TestCommandScript.py (revision 7406d236d873d74b01c3cef285e5d9012dcef151)
1"""
2Test lldb Python commands.
3"""
4
5
6import sys
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11
12class CmdPythonTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def test(self):
18        self.build()
19        self.pycmd_tests()
20
21    def pycmd_tests(self):
22        self.runCmd("command source py_import")
23
24        # Test a bunch of different kinds of python callables with
25        # both 4 and 5 positional arguments.
26        self.expect("foobar", substrs=["All good"])
27        self.expect("foobar4", substrs=["All good"])
28        self.expect("vfoobar", substrs=["All good"])
29        self.expect("v5foobar", substrs=["All good"])
30        self.expect("sfoobar", substrs=["All good"])
31        self.expect("cfoobar", substrs=["All good"])
32        self.expect("ifoobar", substrs=["All good"])
33        self.expect("sfoobar4", substrs=["All good"])
34        self.expect("cfoobar4", substrs=["All good"])
35        self.expect("ifoobar4", substrs=["All good"])
36        self.expect("ofoobar", substrs=["All good"])
37        self.expect("ofoobar4", substrs=["All good"])
38
39        # Verify command that specifies eCommandRequiresTarget returns failure
40        # without a target.
41        self.expect('targetname',
42                    substrs=['a.out'], matching=False, error=True)
43
44        exe = self.getBuildArtifact("a.out")
45        self.expect("file " + exe,
46                    patterns=["Current executable set to .*a.out"])
47
48        self.expect('targetname',
49                    substrs=['a.out'], matching=True, error=False)
50
51        # This is the function to remove the custom commands in order to have a
52        # clean slate for the next test case.
53        def cleanup():
54            self.runCmd('command script delete welcome', check=False)
55            self.runCmd('command script delete targetname', check=False)
56            self.runCmd('command script delete longwait', check=False)
57            self.runCmd('command script delete mysto', check=False)
58            self.runCmd('command script delete tell_sync', check=False)
59            self.runCmd('command script delete tell_async', check=False)
60            self.runCmd('command script delete tell_curr', check=False)
61            self.runCmd('command script delete bug11569', check=False)
62            self.runCmd('command script delete takes_exe_ctx', check=False)
63            self.runCmd('command script delete decorated', check=False)
64
65        # Execute the cleanup function during test case tear down.
66        self.addTearDownHook(cleanup)
67
68        # Interact with debugger in synchronous mode
69        self.setAsync(False)
70
71        # We don't want to display the stdout if not in TraceOn() mode.
72        if not self.TraceOn():
73            self.HideStdout()
74
75        self.expect('welcome Enrico',
76                    substrs=['Hello Enrico, welcome to LLDB'])
77
78        self.expect("help welcome",
79                    substrs=['Just a docstring for welcome_impl',
80                             'A command that says hello to LLDB users'])
81
82        decorated_commands = ["decorated" + str(n) for n in range(1, 5)]
83        for name in decorated_commands:
84            self.expect(name, substrs=["hello from " + name])
85            self.expect("help " + name,
86                        substrs=["Python command defined by @lldb.command"])
87
88        self.expect("help",
89                    substrs=['For more information run']
90                             + decorated_commands + ['welcome'])
91
92        self.expect("help -a",
93                    substrs=['For more information run']
94                             + decorated_commands + ['welcome'])
95
96        self.expect("help -u", matching=False,
97                    substrs=['For more information run'])
98
99        self.runCmd("command script delete welcome")
100
101        self.expect('welcome Enrico', matching=False, error=True,
102                    substrs=['Hello Enrico, welcome to LLDB'])
103
104        self.expect('targetname fail', error=True,
105                    substrs=['a test for error in command'])
106
107        self.expect('command script list',
108                    substrs=['targetname',
109                             'For more information run'])
110
111        self.expect("help targetname",
112                    substrs=['Expects', '\'raw\'', 'input',
113                             'help', 'raw-input'])
114
115        self.expect("longwait",
116                    substrs=['Done; if you saw the delays I am doing OK'])
117
118        self.runCmd("b main")
119        self.runCmd("run")
120        self.runCmd("mysto 3")
121        self.expect("frame variable array",
122                    substrs=['[0] = 79630', '[1] = 388785018', '[2] = 0'])
123        self.runCmd("mysto 3")
124        self.expect("frame variable array",
125                    substrs=['[0] = 79630', '[4] = 388785018', '[5] = 0'])
126
127# we cannot use the stepover command to check for async execution mode since LLDB
128# seems to get confused when events start to queue up
129        self.expect("tell_sync",
130                    substrs=['running sync'])
131        self.expect("tell_async",
132                    substrs=['running async'])
133        self.expect("tell_curr",
134                    substrs=['I am running sync'])
135
136# check that the execution context is passed in to commands that ask for it
137        self.expect("takes_exe_ctx", substrs=["a.out"])
138
139        # Test that a python command can redefine itself
140        self.expect('command script add -f foobar welcome -h "just some help"')
141
142        self.runCmd("command script clear")
143
144        # Test that re-defining an existing command works
145        self.runCmd(
146            'command script add my_command --class welcome.WelcomeCommand')
147        self.expect('my_command Blah', substrs=['Hello Blah, welcome to LLDB'])
148
149        self.runCmd(
150            'command script add my_command -o --class welcome.TargetnameCommand')
151        self.expect('my_command', substrs=['a.out'])
152
153        self.runCmd("command script clear")
154
155        self.expect('command script list', matching=False,
156                    substrs=['targetname',
157                             'longwait'])
158
159        self.expect('command script add -f foobar frame', error=True,
160                    substrs=['cannot add command'])
161
162        # http://llvm.org/bugs/show_bug.cgi?id=11569
163        # LLDBSwigPythonCallCommand crashes when a command script returns an
164        # object
165        self.runCmd('command script add -f bug11569 bug11569')
166        # This should not crash.
167        self.runCmd('bug11569', check=False)
168
169    def test_persistence(self):
170        self.runCmd("command script import persistence.py")
171        self.runCmd("command script add -f persistence.save_debugger save_debugger")
172        self.expect("save_debugger", substrs=[str(self.dbg)])
173        self.expect("script str(persistence.debugger_copy)", substrs=[str(self.dbg)])
174