blob: 2b188af5935c5994c1daaae16ef62ea5ec299d3e [file] [log] [blame]
Johnny Chen49cb85d2011-11-28 21:39:07 +00001"""Test Python APIs for process IO."""
2
3import os, sys, time
4import unittest2
5import lldb
6from lldbtest import *
Greg Claytonb8e9b8b2014-10-14 20:18:05 +00007import lldbutil
Johnny Chen49cb85d2011-11-28 21:39:07 +00008
9class ProcessIOTestCase(TestBase):
10
Greg Clayton4570d3e2013-12-10 23:19:29 +000011 mydir = TestBase.compute_mydir(__file__)
Johnny Chen49cb85d2011-11-28 21:39:07 +000012
Zachary Turner82da55f2014-11-25 19:03:08 +000013 @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin")
Johnny Chen49cb85d2011-11-28 21:39:07 +000014 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000015 @dsym_test
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000016 def test_stdin_by_api_with_dsym(self):
Johnny Chen49cb85d2011-11-28 21:39:07 +000017 """Exercise SBProcess.PutSTDIN()."""
18 self.buildDsym()
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000019 self.do_stdin_by_api()
Johnny Chen49cb85d2011-11-28 21:39:07 +000020
Zachary Turner82da55f2014-11-25 19:03:08 +000021 @unittest2.skipIf(sys.platform.startswith("win32"), "stdio manipulation unsupported on Windows")
Johnny Chen49cb85d2011-11-28 21:39:07 +000022 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000023 @dwarf_test
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000024 def test_stdin_by_api_with_dwarf(self):
Johnny Chen49cb85d2011-11-28 21:39:07 +000025 """Exercise SBProcess.PutSTDIN()."""
26 self.buildDwarf()
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000027 self.do_stdin_by_api()
28
Zachary Turner82da55f2014-11-25 19:03:08 +000029 @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000030 @python_api_test
31 @dsym_test
32 def test_stdin_redirection_with_dsym(self):
33 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
34 self.buildDsym()
35 self.do_stdin_redirection()
36
Zachary Turner82da55f2014-11-25 19:03:08 +000037 @unittest2.skipIf(sys.platform.startswith("win32"), "stdio manipulation unsupported on Windows")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000038 @python_api_test
39 @dwarf_test
40 def test_stdin_redirection_with_dwarf(self):
41 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
42 self.buildDwarf()
43 self.do_stdin_redirection()
44
Zachary Turner82da55f2014-11-25 19:03:08 +000045 @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000046 @python_api_test
47 @dsym_test
48 def test_stdout_redirection_with_dsym(self):
49 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
50 self.buildDsym()
51 self.do_stdout_redirection()
52
Zachary Turner82da55f2014-11-25 19:03:08 +000053 @unittest2.skipIf(sys.platform.startswith("win32"), "stdio manipulation unsupported on Windows")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000054 @python_api_test
55 @dwarf_test
56 def test_stdout_redirection_with_dwarf(self):
57 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
58 self.buildDwarf()
59 self.do_stdout_redirection()
60
Zachary Turner82da55f2014-11-25 19:03:08 +000061 @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000062 @python_api_test
63 @dsym_test
64 def test_stderr_redirection_with_dsym(self):
65 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
66 self.buildDsym()
67 self.do_stderr_redirection()
68
Zachary Turner82da55f2014-11-25 19:03:08 +000069 @unittest2.skipIf(sys.platform.startswith("win32"), "stdio manipulation unsupported on Windows")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000070 @python_api_test
71 @dwarf_test
72 def test_stderr_redirection_with_dwarf(self):
73 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
74 self.buildDwarf()
75 self.do_stderr_redirection()
76
Zachary Turner82da55f2014-11-25 19:03:08 +000077 @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000078 @python_api_test
79 @dsym_test
80 def test_stdout_stderr_redirection_with_dsym(self):
81 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
82 self.buildDsym()
83 self.do_stdout_stderr_redirection()
84
Zachary Turner29278a62014-12-10 23:25:28 +000085 @unittest2.skipIf(sys.platform.startswith("win32"), "stdio manipulation unsupported on Windows")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000086 @python_api_test
87 @dwarf_test
88 def test_stdout_stderr_redirection_with_dwarf(self):
89 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
90 self.buildDwarf()
91 self.do_stdout_stderr_redirection()
Johnny Chen49cb85d2011-11-28 21:39:07 +000092
93 def setUp(self):
94 # Call super's setUp().
95 TestBase.setUp(self)
96 # Get the full path to our executable to be debugged.
97 self.exe = os.path.join(os.getcwd(), "process_io")
Vince Harrondf3f00f2015-02-10 21:09:04 +000098 self.local_input_file = os.path.join(os.getcwd(), "input.txt")
99 self.local_output_file = os.path.join(os.getcwd(), "output.txt")
100 self.local_error_file = os.path.join(os.getcwd(), "error.txt")
101
102 self.input_file = os.path.join(self.get_process_working_directory(), "input.txt")
103 self.output_file = os.path.join(self.get_process_working_directory(), "output.txt")
104 self.error_file = os.path.join(self.get_process_working_directory(), "error.txt")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000105 self.lines = ["Line 1", "Line 2", "Line 3"]
106
107 def read_output_file_and_delete (self):
108 self.assertTrue(os.path.exists(self.output_file), "Make sure output.txt file exists")
109 f = open(self.output_file, 'r')
110 contents = f.read()
111 f.close()
112 os.unlink(self.output_file)
113 return contents
Johnny Chen49cb85d2011-11-28 21:39:07 +0000114
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000115 def read_error_file_and_delete(self):
116 self.assertTrue(os.path.exists(self.error_file), "Make sure error.txt file exists")
117 f = open(self.error_file, 'r')
118 contents = f.read()
119 f.close()
120 os.unlink(self.error_file)
121 return contents
122
123 def create_target(self):
124 '''Create the target and launch info that will be used by all tests'''
125 self.target = self.dbg.CreateTarget(self.exe)
126 self.launch_info = lldb.SBLaunchInfo([self.exe])
127 self.launch_info.SetWorkingDirectory(self.get_process_working_directory())
128
129 def redirect_stdin(self):
130 '''Redirect STDIN (file descriptor 0) to use our input.txt file
131
132 Make the input.txt file to use when redirecting STDIN, setup a cleanup action
133 to delete the input.txt at the end of the test in case exceptions are thrown,
134 and redirect STDIN in the launch info.'''
Vince Harrondf3f00f2015-02-10 21:09:04 +0000135 f = open(self.local_input_file, 'w')
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000136 for line in self.lines:
137 f.write(line + "\n")
138 f.close()
Vince Harrondf3f00f2015-02-10 21:09:04 +0000139
140 if lldb.remote_platform:
141 self.runCmd('platform put-file "{local}" "{remote}"'.format(
142 local=self.local_input_file, remote=self.input_file))
143
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000144 # This is the function to remove the custom formats in order to have a
145 # clean slate for the next test case.
146 def cleanup():
Vince Harrondf3f00f2015-02-10 21:09:04 +0000147 os.unlink(self.local_input_file)
148 # if lldb.remote_platform:
149 # TODO: add delete file command
150 # self.runCmd('platform delete-file "{local}" "{remote}"'.format(
151 # local=self.local_input_file, remote=self.input_file))'''
152
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000153 # Execute the cleanup function during test case tear down.
154 self.addTearDownHook(cleanup)
155 self.launch_info.AddOpenFileAction(0, self.input_file, True, False);
156
157 def redirect_stdout(self):
158 '''Redirect STDOUT (file descriptor 1) to use our output.txt file'''
159 self.launch_info.AddOpenFileAction(1, self.output_file, False, True);
160
161 def redirect_stderr(self):
162 '''Redirect STDERR (file descriptor 2) to use our error.txt file'''
163 self.launch_info.AddOpenFileAction(2, self.error_file, False, True);
164
165 def do_stdin_redirection(self):
166 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
167 self.create_target()
168 self.redirect_stdin()
169 self.run_process(False)
170 output = self.process.GetSTDOUT(1000)
171 self.check_process_output(output, output)
172
173 def do_stdout_redirection(self):
174 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
175 self.create_target()
176 self.redirect_stdout()
177 self.run_process(True)
178 output = self.read_output_file_and_delete()
179 error = self.process.GetSTDOUT(1000)
180 self.check_process_output(output, error)
181
182 def do_stderr_redirection(self):
183 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
184 self.create_target()
185 self.redirect_stderr()
186 self.run_process(True)
187 output = self.process.GetSTDOUT(1000)
188 error = self.read_error_file_and_delete()
189 self.check_process_output(output, error)
190
191 def do_stdout_stderr_redirection(self):
192 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
193 self.create_target()
194 self.redirect_stdout()
195 self.redirect_stderr()
196 self.run_process(True)
197 output = self.read_output_file_and_delete()
198 error = self.read_error_file_and_delete()
199 self.check_process_output(output, error)
200
201 def do_stdin_stdout_stderr_redirection(self):
202 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN, STDOUT and STDERR."""
203 # Make the input.txt file to use
204 self.create_target()
205 self.redirect_stdin()
206 self.redirect_stdout()
207 self.redirect_stderr()
208 self.run_process(True)
209 output = self.read_output_file_and_delete()
210 error = self.read_error_file_and_delete()
211 self.check_process_output(output, error)
212
213 def do_stdin_by_api(self):
Johnny Chen49cb85d2011-11-28 21:39:07 +0000214 """Launch a process and use SBProcess.PutSTDIN() to write data to it."""
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000215 self.create_target()
216 self.run_process(True)
217 output = self.process.GetSTDOUT(1000)
218 self.check_process_output(output, output)
219
220 def run_process(self, put_stdin):
221 '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True'''
222 # Set the breakpoints
223 self.breakpoint = self.target.BreakpointCreateBySourceRegex('Set breakpoint here', lldb.SBFileSpec("main.c"))
224 self.assertTrue(self.breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
Johnny Chen49cb85d2011-11-28 21:39:07 +0000225
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000226 # Launch the process, and do not stop at the entry point.
227 error = lldb.SBError()
228 # This should launch the process and it should exit by the time we get back
229 # because we have synchronous mode enabled
230 self.process = self.target.Launch (self.launch_info, error)
Johnny Chen49cb85d2011-11-28 21:39:07 +0000231
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000232 self.assertTrue(error.Success(), "Make sure process launched successfully")
233 self.assertTrue(self.process, PROCESS_IS_VALID)
Daniel Malea249287a2013-02-19 16:08:57 +0000234
Johnny Chen49cb85d2011-11-28 21:39:07 +0000235 if self.TraceOn():
236 print "process launched."
237
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000238 # Frame #0 should be at our breakpoint.
239 threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, self.breakpoint)
240
241 self.assertTrue(len(threads) == 1)
242 self.thread = threads[0]
243 self.frame = self.thread.frames[0]
244 self.assertTrue(self.frame, "Frame 0 is valid.")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000245
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000246 if self.TraceOn():
247 print "process stopped at breakpoint, sending STDIN via LLDB API."
Johnny Chen49cb85d2011-11-28 21:39:07 +0000248
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000249 # Write data to stdin via the public API if we were asked to
250 if put_stdin:
251 for line in self.lines:
252 self.process.PutSTDIN(line + "\n")
253
254 # Let process continue so it will exit
255 self.process.Continue()
256 state = self.process.GetState()
257 self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID)
258
259 def check_process_output (self, output, error):
Johnny Chen5886fb52012-01-12 00:29:46 +0000260 # Since we launched the process without specifying stdin/out/err,
261 # a pseudo terminal is used for stdout/err, and we are satisfied
262 # once "input line=>1" appears in stdout.
Johnny Chen49cb85d2011-11-28 21:39:07 +0000263 # See also main.c.
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000264 if self.TraceOn():
265 print "output = '%s'" % output
266 print "error = '%s'" % error
267
268 for line in self.lines:
269 check_line = 'input line to stdout: %s' % (line)
270 self.assertTrue(check_line in output, "verify stdout line shows up in STDOUT")
271 for line in self.lines:
272 check_line = 'input line to stderr: %s' % (line)
273 self.assertTrue(check_line in error, "verify stderr line shows up in STDERR")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000274
275if __name__ == '__main__':
276 import atexit
277 lldb.SBDebugger.Initialize()
278 atexit.register(lambda: lldb.SBDebugger.Terminate())
279 unittest2.main()