1 """Test Python APIs for process IO."""
3 from __future__ import print_function
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
15 class ProcessIOTestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
20 # Call super's setUp().
22 # Get the full path to our executable to be debugged.
23 self.exe = os.path.join(os.getcwd(), "process_io")
24 self.local_input_file = os.path.join(os.getcwd(), "input.txt")
25 self.local_output_file = os.path.join(os.getcwd(), "output.txt")
26 self.local_error_file = os.path.join(os.getcwd(), "error.txt")
28 self.input_file = os.path.join(
29 self.get_process_working_directory(), "input.txt")
30 self.output_file = os.path.join(
31 self.get_process_working_directory(), "output.txt")
32 self.error_file = os.path.join(
33 self.get_process_working_directory(), "error.txt")
34 self.lines = ["Line 1", "Line 2", "Line 3"]
36 @skipIfWindows # stdio manipulation unsupported on Windows
37 @add_test_categories(['pyapi'])
38 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
39 def test_stdin_by_api(self):
40 """Exercise SBProcess.PutSTDIN()."""
43 self.run_process(True)
44 output = self.process.GetSTDOUT(1000)
45 self.check_process_output(output, output)
47 @skipIfWindows # stdio manipulation unsupported on Windows
48 @add_test_categories(['pyapi'])
49 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
50 def test_stdin_redirection(self):
51 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
55 self.run_process(False)
56 output = self.process.GetSTDOUT(1000)
57 self.check_process_output(output, output)
59 @skipIfWindows # stdio manipulation unsupported on Windows
60 @add_test_categories(['pyapi'])
61 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
62 def test_stdout_redirection(self):
63 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
66 self.redirect_stdout()
67 self.run_process(True)
68 output = self.read_output_file_and_delete()
69 error = self.process.GetSTDOUT(1000)
70 self.check_process_output(output, error)
72 @skipIfWindows # stdio manipulation unsupported on Windows
73 @add_test_categories(['pyapi'])
74 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
75 def test_stderr_redirection(self):
76 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
79 self.redirect_stderr()
80 self.run_process(True)
81 output = self.process.GetSTDOUT(1000)
82 error = self.read_error_file_and_delete()
83 self.check_process_output(output, error)
85 @skipIfWindows # stdio manipulation unsupported on Windows
86 @add_test_categories(['pyapi'])
87 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
88 def test_stdout_stderr_redirection(self):
89 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
92 self.redirect_stdout()
93 self.redirect_stderr()
94 self.run_process(True)
95 output = self.read_output_file_and_delete()
96 error = self.read_error_file_and_delete()
97 self.check_process_output(output, error)
99 # target_file - path on local file system or remote file system if running remote
100 # local_file - path on local system
101 def read_file_and_delete(self, target_file, local_file):
102 if lldb.remote_platform:
103 self.runCmd('platform get-file "{remote}" "{local}"'.format(
104 remote=target_file, local=local_file))
107 os.path.exists(local_file),
108 'Make sure "{local}" file exists'.format(
110 f = open(local_file, 'r')
114 # TODO: add 'platform delete-file' file command
115 # if lldb.remote_platform:
116 # self.runCmd('platform delete-file "{remote}"'.format(remote=target_file))
117 os.unlink(local_file)
120 def read_output_file_and_delete(self):
121 return self.read_file_and_delete(
122 self.output_file, self.local_output_file)
124 def read_error_file_and_delete(self):
125 return self.read_file_and_delete(
126 self.error_file, self.local_error_file)
128 def create_target(self):
129 '''Create the target and launch info that will be used by all tests'''
130 self.target = self.dbg.CreateTarget(self.exe)
131 self.launch_info = lldb.SBLaunchInfo([self.exe])
132 self.launch_info.SetWorkingDirectory(
133 self.get_process_working_directory())
135 def redirect_stdin(self):
136 '''Redirect STDIN (file descriptor 0) to use our input.txt file
138 Make the input.txt file to use when redirecting STDIN, setup a cleanup action
139 to delete the input.txt at the end of the test in case exceptions are thrown,
140 and redirect STDIN in the launch info.'''
141 f = open(self.local_input_file, 'w')
142 for line in self.lines:
146 if lldb.remote_platform:
147 self.runCmd('platform put-file "{local}" "{remote}"'.format(
148 local=self.local_input_file, remote=self.input_file))
150 # This is the function to remove the custom formats in order to have a
151 # clean slate for the next test case.
153 os.unlink(self.local_input_file)
154 # TODO: add 'platform delete-file' file command
155 # if lldb.remote_platform:
156 # self.runCmd('platform delete-file "{remote}"'.format(remote=self.input_file))
158 # Execute the cleanup function during test case tear down.
159 self.addTearDownHook(cleanup)
160 self.launch_info.AddOpenFileAction(0, self.input_file, True, False)
162 def redirect_stdout(self):
163 '''Redirect STDOUT (file descriptor 1) to use our output.txt file'''
164 self.launch_info.AddOpenFileAction(1, self.output_file, False, True)
166 def redirect_stderr(self):
167 '''Redirect STDERR (file descriptor 2) to use our error.txt file'''
168 self.launch_info.AddOpenFileAction(2, self.error_file, False, True)
170 def run_process(self, put_stdin):
171 '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True'''
172 # Set the breakpoints
173 self.breakpoint = self.target.BreakpointCreateBySourceRegex(
174 'Set breakpoint here', lldb.SBFileSpec("main.c"))
176 self.breakpoint.GetNumLocations() > 0,
179 # Launch the process, and do not stop at the entry point.
180 error = lldb.SBError()
181 # This should launch the process and it should exit by the time we get back
182 # because we have synchronous mode enabled
183 self.process = self.target.Launch(self.launch_info, error)
187 "Make sure process launched successfully")
188 self.assertTrue(self.process, PROCESS_IS_VALID)
191 print("process launched.")
193 # Frame #0 should be at our breakpoint.
194 threads = lldbutil.get_threads_stopped_at_breakpoint(
195 self.process, self.breakpoint)
197 self.assertTrue(len(threads) == 1)
198 self.thread = threads[0]
199 self.frame = self.thread.frames[0]
200 self.assertTrue(self.frame, "Frame 0 is valid.")
203 print("process stopped at breakpoint, sending STDIN via LLDB API.")
205 # Write data to stdin via the public API if we were asked to
207 for line in self.lines:
208 self.process.PutSTDIN(line + "\n")
210 # Let process continue so it will exit
211 self.process.Continue()
212 state = self.process.GetState()
213 self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID)
215 def check_process_output(self, output, error):
216 # Since we launched the process without specifying stdin/out/err,
217 # a pseudo terminal is used for stdout/err, and we are satisfied
218 # once "input line=>1" appears in stdout.
221 print("output = '%s'" % output)
222 print("error = '%s'" % error)
224 for line in self.lines:
225 check_line = 'input line to stdout: %s' % (line)
227 check_line in output,
228 "verify stdout line shows up in STDOUT")
229 for line in self.lines:
230 check_line = 'input line to stderr: %s' % (line)
233 "verify stderr line shows up in STDERR")