blob: 6efda1c46aa8b30b8d2312c645ce5e0c6d94f031 [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
14 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
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):
26 exe = os.path.join (os.getcwd(), "a.out")
27 self.expect("file " + exe,
28 patterns = [ "Current executable set to .*a.out" ])
29
30 self.runCmd("command source py_import")
31
Johnny Chen04a101d2011-10-12 17:50:41 +000032 # This is the function to remove the custom commands in order to have a
33 # clean slate for the next test case.
34 def cleanup():
35 self.runCmd('command script delete welcome', check=False)
36 self.runCmd('command script delete targetname', check=False)
37 self.runCmd('command script delete longwait', check=False)
Enrico Granata0a305db2011-11-07 22:57:04 +000038 self.runCmd('command script delete mysto', check=False)
39 self.runCmd('command script delete tell_sync', check=False)
40 self.runCmd('command script delete tell_async', check=False)
41 self.runCmd('command script delete tell_curr', check=False)
Johnny Chen1d9cb8a2011-12-14 20:40:27 +000042 self.runCmd('command script delete bug11569', check=False)
Johnny Chen04a101d2011-10-12 17:50:41 +000043
44 # Execute the cleanup function during test case tear down.
45 self.addTearDownHook(cleanup)
46
Daniel Malea249287a2013-02-19 16:08:57 +000047 # Interact with debugger in synchronous mode
48 self.setAsync(False)
49
Johnny Chen3e15c4d2011-08-24 18:19:50 +000050 # We don't want to display the stdout if not in TraceOn() mode.
51 if not self.TraceOn():
52 self.HideStdout()
53
Enrico Granatacb2921d2011-08-24 17:45:40 +000054 self.expect('welcome Enrico',
55 substrs = ['Hello Enrico, welcome to LLDB']);
56
57 self.expect("help welcome",
58 substrs = ['Just a docstring for welcome_impl',
59 'A command that says hello to LLDB users'])
60
Enrico Granata08633ee2011-09-09 17:49:36 +000061 self.expect("help",
Enrico Granata735152e2014-09-15 17:52:44 +000062 substrs = ['For more information run',
Enrico Granata08633ee2011-09-09 17:49:36 +000063 'welcome'])
64
65 self.expect("help -a",
Enrico Granata735152e2014-09-15 17:52:44 +000066 substrs = ['For more information run',
Enrico Granata08633ee2011-09-09 17:49:36 +000067 'welcome'])
68
69 self.expect("help -u", matching=False,
Enrico Granata735152e2014-09-15 17:52:44 +000070 substrs = ['For more information run'])
Enrico Granata08633ee2011-09-09 17:49:36 +000071
Enrico Granatacb2921d2011-08-24 17:45:40 +000072 self.runCmd("command script delete welcome");
73
74 self.expect('welcome Enrico', matching=False, error=True,
75 substrs = ['Hello Enrico, welcome to LLDB']);
76
77 self.expect('targetname',
78 substrs = ['a.out'])
79
80 self.expect('targetname fail', error=True,
81 substrs = ['a test for error in command'])
82
83 self.expect('command script list',
84 substrs = ['targetname',
Enrico Granata735152e2014-09-15 17:52:44 +000085 'For more information run'])
Enrico Granatacb2921d2011-08-24 17:45:40 +000086
87 self.expect("help targetname",
Enrico Granata735152e2014-09-15 17:52:44 +000088 substrs = ['This command takes','\'raw\' input',
Enrico Granatacb2921d2011-08-24 17:45:40 +000089 'quote stuff'])
90
91 self.expect("longwait",
92 substrs = ['Done; if you saw the delays I am doing OK'])
93
Enrico Granata0a305db2011-11-07 22:57:04 +000094 self.runCmd("b main")
95 self.runCmd("run")
96 self.runCmd("mysto 3")
97 self.expect("frame variable array",
98 substrs = ['[0] = 79630','[1] = 388785018','[2] = 0'])
99 self.runCmd("mysto 3")
100 self.expect("frame variable array",
101 substrs = ['[0] = 79630','[4] = 388785018','[5] = 0'])
102
103# we cannot use the stepover command to check for async execution mode since LLDB
104# seems to get confused when events start to queue up
105 self.expect("tell_sync",
106 substrs = ['running sync'])
107 self.expect("tell_async",
108 substrs = ['running async'])
109 self.expect("tell_curr",
Daniel Malea249287a2013-02-19 16:08:57 +0000110 substrs = ['I am running sync'])
Enrico Granata0a305db2011-11-07 22:57:04 +0000111
Daniel Malead82ac9e2013-02-21 21:18:07 +0000112 # Test that a python command can redefine itself
Enrico Granata735152e2014-09-15 17:52:44 +0000113 self.expect('command script add -f foobar welcome -h "just some help"')
114
Enrico Granatacb2921d2011-08-24 17:45:40 +0000115 self.runCmd("command script clear")
116
Daniel Malead82ac9e2013-02-21 21:18:07 +0000117 # Test that re-defining an existing command works
118 self.runCmd('command script add my_command --function welcome.welcome_impl')
119 self.expect('my_command Blah', substrs = ['Hello Blah, welcome to LLDB'])
120
121 self.runCmd('command script add my_command --function welcome.target_name_impl')
122 self.expect('my_command', substrs = ['a.out'])
123
124 self.runCmd("command script clear")
125
Enrico Granatacb2921d2011-08-24 17:45:40 +0000126 self.expect('command script list', matching=False,
127 substrs = ['targetname',
128 'longwait'])
129
Enrico Granata0a305db2011-11-07 22:57:04 +0000130 self.expect('command script add -f foobar frame', error=True,
131 substrs = ['cannot add command'])
132
Johnny Chen1d9cb8a2011-12-14 20:40:27 +0000133 # http://llvm.org/bugs/show_bug.cgi?id=11569
134 # LLDBSwigPythonCallCommand crashes when a command script returns an object
135 self.runCmd('command script add -f bug11569 bug11569')
136 # This should not crash.
137 self.runCmd('bug11569', check=False)
138
Enrico Granatacb2921d2011-08-24 17:45:40 +0000139if __name__ == '__main__':
140 import atexit
141 lldb.SBDebugger.Initialize()
142 atexit.register(lambda: lldb.SBDebugger.Terminate())
143 unittest2.main()
144