blob: 66ac0bdb0d86e870fef914057b98eca001d5a94f [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
Robert Flack13c7ad92015-03-30 14:12:17 +000013 @skipUnlessDarwin
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
Robert Flack13c7ad92015-03-30 14:12:17 +000021 @skipIfWindows # 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
Robert Flack13c7ad92015-03-30 14:12:17 +000029 @skipUnlessDarwin
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
Robert Flack13c7ad92015-03-30 14:12:17 +000037 @skipIfWindows # 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
Robert Flack13c7ad92015-03-30 14:12:17 +000045 @skipUnlessDarwin
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
Robert Flack13c7ad92015-03-30 14:12:17 +000053 @skipIfWindows # 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
Robert Flack13c7ad92015-03-30 14:12:17 +000061 @skipUnlessDarwin
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
Robert Flack13c7ad92015-03-30 14:12:17 +000069 @skipIfWindows # 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
Robert Flack13c7ad92015-03-30 14:12:17 +000077 @skipUnlessDarwin
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
Robert Flack13c7ad92015-03-30 14:12:17 +000085 @skipIfWindows # 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"]
Vince Harron4a8abd32015-02-13 19:15:24 +0000106
107 # target_file - path on local file system or remote file system if running remote
108 # local_file - path on local system
109 def read_file_and_delete(self, target_file, local_file):
110 if lldb.remote_platform:
111 self.runCmd('platform get-file "{remote}" "{local}"'.format(
112 remote=target_file, local=local_file))
113
114 self.assertTrue(os.path.exists(local_file), 'Make sure "{local}" file exists'.format(local=local_file))
115 f = open(local_file, 'r')
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000116 contents = f.read()
117 f.close()
Vince Harron4a8abd32015-02-13 19:15:24 +0000118
119 #TODO: add 'platform delete-file' file command
120 #if lldb.remote_platform:
121 # self.runCmd('platform delete-file "{remote}"'.format(remote=target_file))
122 os.unlink(local_file)
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000123 return contents
Johnny Chen49cb85d2011-11-28 21:39:07 +0000124
Vince Harron4a8abd32015-02-13 19:15:24 +0000125 def read_output_file_and_delete(self):
126 return self.read_file_and_delete(self.output_file, self.local_output_file)
127
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000128 def read_error_file_and_delete(self):
Vince Harron4a8abd32015-02-13 19:15:24 +0000129 return self.read_file_and_delete(self.error_file, self.local_error_file)
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000130
131 def create_target(self):
132 '''Create the target and launch info that will be used by all tests'''
133 self.target = self.dbg.CreateTarget(self.exe)
134 self.launch_info = lldb.SBLaunchInfo([self.exe])
135 self.launch_info.SetWorkingDirectory(self.get_process_working_directory())
136
137 def redirect_stdin(self):
138 '''Redirect STDIN (file descriptor 0) to use our input.txt file
139
140 Make the input.txt file to use when redirecting STDIN, setup a cleanup action
141 to delete the input.txt at the end of the test in case exceptions are thrown,
142 and redirect STDIN in the launch info.'''
Vince Harrondf3f00f2015-02-10 21:09:04 +0000143 f = open(self.local_input_file, 'w')
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000144 for line in self.lines:
145 f.write(line + "\n")
146 f.close()
Vince Harrondf3f00f2015-02-10 21:09:04 +0000147
148 if lldb.remote_platform:
149 self.runCmd('platform put-file "{local}" "{remote}"'.format(
150 local=self.local_input_file, remote=self.input_file))
151
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000152 # This is the function to remove the custom formats in order to have a
153 # clean slate for the next test case.
154 def cleanup():
Vince Harrondf3f00f2015-02-10 21:09:04 +0000155 os.unlink(self.local_input_file)
Vince Harron4a8abd32015-02-13 19:15:24 +0000156 #TODO: add 'platform delete-file' file command
157 #if lldb.remote_platform:
158 # self.runCmd('platform delete-file "{remote}"'.format(remote=self.input_file))
Vince Harrondf3f00f2015-02-10 21:09:04 +0000159
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000160 # Execute the cleanup function during test case tear down.
161 self.addTearDownHook(cleanup)
162 self.launch_info.AddOpenFileAction(0, self.input_file, True, False);
163
164 def redirect_stdout(self):
165 '''Redirect STDOUT (file descriptor 1) to use our output.txt file'''
166 self.launch_info.AddOpenFileAction(1, self.output_file, False, True);
167
168 def redirect_stderr(self):
169 '''Redirect STDERR (file descriptor 2) to use our error.txt file'''
170 self.launch_info.AddOpenFileAction(2, self.error_file, False, True);
171
172 def do_stdin_redirection(self):
173 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
174 self.create_target()
175 self.redirect_stdin()
176 self.run_process(False)
177 output = self.process.GetSTDOUT(1000)
178 self.check_process_output(output, output)
179
180 def do_stdout_redirection(self):
181 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
182 self.create_target()
183 self.redirect_stdout()
184 self.run_process(True)
185 output = self.read_output_file_and_delete()
186 error = self.process.GetSTDOUT(1000)
187 self.check_process_output(output, error)
188
189 def do_stderr_redirection(self):
190 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
191 self.create_target()
192 self.redirect_stderr()
193 self.run_process(True)
194 output = self.process.GetSTDOUT(1000)
195 error = self.read_error_file_and_delete()
196 self.check_process_output(output, error)
197
198 def do_stdout_stderr_redirection(self):
199 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
200 self.create_target()
201 self.redirect_stdout()
202 self.redirect_stderr()
203 self.run_process(True)
204 output = self.read_output_file_and_delete()
205 error = self.read_error_file_and_delete()
206 self.check_process_output(output, error)
207
208 def do_stdin_stdout_stderr_redirection(self):
209 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN, STDOUT and STDERR."""
210 # Make the input.txt file to use
211 self.create_target()
212 self.redirect_stdin()
213 self.redirect_stdout()
214 self.redirect_stderr()
215 self.run_process(True)
216 output = self.read_output_file_and_delete()
217 error = self.read_error_file_and_delete()
218 self.check_process_output(output, error)
219
220 def do_stdin_by_api(self):
Johnny Chen49cb85d2011-11-28 21:39:07 +0000221 """Launch a process and use SBProcess.PutSTDIN() to write data to it."""
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000222 self.create_target()
223 self.run_process(True)
224 output = self.process.GetSTDOUT(1000)
225 self.check_process_output(output, output)
226
227 def run_process(self, put_stdin):
228 '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True'''
229 # Set the breakpoints
230 self.breakpoint = self.target.BreakpointCreateBySourceRegex('Set breakpoint here', lldb.SBFileSpec("main.c"))
231 self.assertTrue(self.breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
Johnny Chen49cb85d2011-11-28 21:39:07 +0000232
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000233 # Launch the process, and do not stop at the entry point.
234 error = lldb.SBError()
235 # This should launch the process and it should exit by the time we get back
236 # because we have synchronous mode enabled
237 self.process = self.target.Launch (self.launch_info, error)
Johnny Chen49cb85d2011-11-28 21:39:07 +0000238
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000239 self.assertTrue(error.Success(), "Make sure process launched successfully")
240 self.assertTrue(self.process, PROCESS_IS_VALID)
Daniel Malea249287a2013-02-19 16:08:57 +0000241
Johnny Chen49cb85d2011-11-28 21:39:07 +0000242 if self.TraceOn():
243 print "process launched."
244
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000245 # Frame #0 should be at our breakpoint.
246 threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, self.breakpoint)
247
248 self.assertTrue(len(threads) == 1)
249 self.thread = threads[0]
250 self.frame = self.thread.frames[0]
251 self.assertTrue(self.frame, "Frame 0 is valid.")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000252
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000253 if self.TraceOn():
254 print "process stopped at breakpoint, sending STDIN via LLDB API."
Johnny Chen49cb85d2011-11-28 21:39:07 +0000255
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000256 # Write data to stdin via the public API if we were asked to
257 if put_stdin:
258 for line in self.lines:
259 self.process.PutSTDIN(line + "\n")
260
261 # Let process continue so it will exit
262 self.process.Continue()
263 state = self.process.GetState()
264 self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID)
265
266 def check_process_output (self, output, error):
Johnny Chen5886fb52012-01-12 00:29:46 +0000267 # Since we launched the process without specifying stdin/out/err,
268 # a pseudo terminal is used for stdout/err, and we are satisfied
269 # once "input line=>1" appears in stdout.
Johnny Chen49cb85d2011-11-28 21:39:07 +0000270 # See also main.c.
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000271 if self.TraceOn():
272 print "output = '%s'" % output
273 print "error = '%s'" % error
274
275 for line in self.lines:
276 check_line = 'input line to stdout: %s' % (line)
277 self.assertTrue(check_line in output, "verify stdout line shows up in STDOUT")
278 for line in self.lines:
279 check_line = 'input line to stderr: %s' % (line)
280 self.assertTrue(check_line in error, "verify stderr line shows up in STDERR")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000281
282if __name__ == '__main__':
283 import atexit
284 lldb.SBDebugger.Initialize()
285 atexit.register(lambda: lldb.SBDebugger.Terminate())
286 unittest2.main()