blob: d4210b44f90269f62f86f1909623ad32cfa436f3 [file] [log] [blame]
Enrico Granatacb2921d2011-08-24 17:45:40 +00001"""
2Test lldb Python commands.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9
10class CmdPythonTestCase(TestBase):
11
Greg Clayton4570d3e2013-12-10 23:19:29 +000012 mydir = TestBase.compute_mydir(__file__)
Enrico Granatacb2921d2011-08-24 17:45:40 +000013
Robert Flack13c7ad92015-03-30 14:12:17 +000014 @skipUnlessDarwin
Johnny Chenf1548d42012-04-06 00:56:05 +000015 @dsym_test
Enrico Granatacb2921d2011-08-24 17:45:40 +000016 def test_with_dsym (self):
17 self.buildDsym ()
18 self.pycmd_tests ()
19
Johnny Chenf1548d42012-04-06 00:56:05 +000020 @dwarf_test
Enrico Granatacb2921d2011-08-24 17:45:40 +000021 def test_with_dwarf (self):
22 self.buildDwarf ()
23 self.pycmd_tests ()
24
25 def pycmd_tests (self):
Enrico Granatae87764f2015-05-27 05:04:35 +000026 self.runCmd("command source py_import")
27
28 self.expect('targetname',
29 substrs = ['a.out'], matching=False, error=True)
30
Enrico Granatacb2921d2011-08-24 17:45:40 +000031 exe = os.path.join (os.getcwd(), "a.out")
32 self.expect("file " + exe,
33 patterns = [ "Current executable set to .*a.out" ])
34
Enrico Granatae87764f2015-05-27 05:04:35 +000035 self.expect('targetname',
36 substrs = ['a.out'], matching=True, error=False)
Enrico Granatacb2921d2011-08-24 17:45:40 +000037
Johnny Chen04a101d2011-10-12 17:50:41 +000038 # This is the function to remove the custom commands in order to have a
39 # clean slate for the next test case.
40 def cleanup():
41 self.runCmd('command script delete welcome', check=False)
42 self.runCmd('command script delete targetname', check=False)
43 self.runCmd('command script delete longwait', check=False)
Enrico Granata0a305db2011-11-07 22:57:04 +000044 self.runCmd('command script delete mysto', check=False)
45 self.runCmd('command script delete tell_sync', check=False)
46 self.runCmd('command script delete tell_async', check=False)
47 self.runCmd('command script delete tell_curr', check=False)
Johnny Chen1d9cb8a2011-12-14 20:40:27 +000048 self.runCmd('command script delete bug11569', check=False)
Enrico Granata06be0592014-10-01 21:47:29 +000049 self.runCmd('command script delete takes_exe_ctx', check=False)
Johnny Chen04a101d2011-10-12 17:50:41 +000050
51 # Execute the cleanup function during test case tear down.
52 self.addTearDownHook(cleanup)
53
Daniel Malea249287a2013-02-19 16:08:57 +000054 # Interact with debugger in synchronous mode
55 self.setAsync(False)
56
Johnny Chen3e15c4d2011-08-24 18:19:50 +000057 # We don't want to display the stdout if not in TraceOn() mode.
58 if not self.TraceOn():
59 self.HideStdout()
60
Enrico Granatacb2921d2011-08-24 17:45:40 +000061 self.expect('welcome Enrico',
62 substrs = ['Hello Enrico, welcome to LLDB']);
63
64 self.expect("help welcome",
65 substrs = ['Just a docstring for welcome_impl',
66 'A command that says hello to LLDB users'])
67
Enrico Granata08633ee2011-09-09 17:49:36 +000068 self.expect("help",
Enrico Granata735152e2014-09-15 17:52:44 +000069 substrs = ['For more information run',
Enrico Granata08633ee2011-09-09 17:49:36 +000070 'welcome'])
71
72 self.expect("help -a",
Enrico Granata735152e2014-09-15 17:52:44 +000073 substrs = ['For more information run',
Enrico Granata08633ee2011-09-09 17:49:36 +000074 'welcome'])
75
76 self.expect("help -u", matching=False,
Enrico Granata735152e2014-09-15 17:52:44 +000077 substrs = ['For more information run'])
Enrico Granata08633ee2011-09-09 17:49:36 +000078
Enrico Granatacb2921d2011-08-24 17:45:40 +000079 self.runCmd("command script delete welcome");
80
81 self.expect('welcome Enrico', matching=False, error=True,
82 substrs = ['Hello Enrico, welcome to LLDB']);
83
Enrico Granatacb2921d2011-08-24 17:45:40 +000084 self.expect('targetname fail', error=True,
85 substrs = ['a test for error in command'])
86
87 self.expect('command script list',
88 substrs = ['targetname',
Enrico Granata735152e2014-09-15 17:52:44 +000089 'For more information run'])
Enrico Granatacb2921d2011-08-24 17:45:40 +000090
91 self.expect("help targetname",
Sean Callanane6553d82014-10-06 17:58:15 +000092 substrs = ['This', 'command', 'takes', '\'raw\'', 'input',
93 'quote', 'stuff'])
Enrico Granatacb2921d2011-08-24 17:45:40 +000094
95 self.expect("longwait",
96 substrs = ['Done; if you saw the delays I am doing OK'])
97
Enrico Granata0a305db2011-11-07 22:57:04 +000098 self.runCmd("b main")
99 self.runCmd("run")
100 self.runCmd("mysto 3")
101 self.expect("frame variable array",
102 substrs = ['[0] = 79630','[1] = 388785018','[2] = 0'])
103 self.runCmd("mysto 3")
104 self.expect("frame variable array",
105 substrs = ['[0] = 79630','[4] = 388785018','[5] = 0'])
106
107# we cannot use the stepover command to check for async execution mode since LLDB
108# seems to get confused when events start to queue up
109 self.expect("tell_sync",
110 substrs = ['running sync'])
111 self.expect("tell_async",
112 substrs = ['running async'])
113 self.expect("tell_curr",
Daniel Malea249287a2013-02-19 16:08:57 +0000114 substrs = ['I am running sync'])
Enrico Granata06be0592014-10-01 21:47:29 +0000115
116# check that the execution context is passed in to commands that ask for it
117 self.expect("takes_exe_ctx", substrs = ["a.out"])
Enrico Granata0a305db2011-11-07 22:57:04 +0000118
Daniel Malead82ac9e2013-02-21 21:18:07 +0000119 # Test that a python command can redefine itself
Enrico Granata735152e2014-09-15 17:52:44 +0000120 self.expect('command script add -f foobar welcome -h "just some help"')
121
Enrico Granatacb2921d2011-08-24 17:45:40 +0000122 self.runCmd("command script clear")
123
Daniel Malead82ac9e2013-02-21 21:18:07 +0000124 # Test that re-defining an existing command works
Enrico Granata6f79bb22015-03-13 22:22:28 +0000125 self.runCmd('command script add my_command --class welcome.WelcomeCommand')
Daniel Malead82ac9e2013-02-21 21:18:07 +0000126 self.expect('my_command Blah', substrs = ['Hello Blah, welcome to LLDB'])
127
Enrico Granatae87764f2015-05-27 05:04:35 +0000128 self.runCmd('command script add my_command --class welcome.TargetnameCommand')
Daniel Malead82ac9e2013-02-21 21:18:07 +0000129 self.expect('my_command', substrs = ['a.out'])
130
131 self.runCmd("command script clear")
132
Enrico Granatacb2921d2011-08-24 17:45:40 +0000133 self.expect('command script list', matching=False,
134 substrs = ['targetname',
135 'longwait'])
136
Enrico Granata0a305db2011-11-07 22:57:04 +0000137 self.expect('command script add -f foobar frame', error=True,
138 substrs = ['cannot add command'])
139
Johnny Chen1d9cb8a2011-12-14 20:40:27 +0000140 # http://llvm.org/bugs/show_bug.cgi?id=11569
141 # LLDBSwigPythonCallCommand crashes when a command script returns an object
142 self.runCmd('command script add -f bug11569 bug11569')
143 # This should not crash.
144 self.runCmd('bug11569', check=False)
145
Enrico Granatacb2921d2011-08-24 17:45:40 +0000146if __name__ == '__main__':
147 import atexit
148 lldb.SBDebugger.Initialize()
149 atexit.register(lambda: lldb.SBDebugger.Terminate())
150 unittest2.main()
151