1 """Test Python APIs for process IO."""
3 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
13 class ProcessIOTestCase(TestBase):
15 mydir = TestBase.compute_mydir(__file__)
18 # Call super's setUp().
20 # Get the full path to our executable to be debugged.
21 self.exe = os.path.join(os.getcwd(), "process_io")
22 self.local_input_file = os.path.join(os.getcwd(), "input.txt")
23 self.local_output_file = os.path.join(os.getcwd(), "output.txt")
24 self.local_error_file = os.path.join(os.getcwd(), "error.txt")
26 self.input_file = os.path.join(self.get_process_working_directory(), "input.txt")
27 self.output_file = os.path.join(self.get_process_working_directory(), "output.txt")
28 self.error_file = os.path.join(self.get_process_working_directory(), "error.txt")
29 self.lines = ["Line 1", "Line 2", "Line 3"]
31 @skipIfWindows # stdio manipulation unsupported on Windows
32 @add_test_categories(['pyapi'])
33 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
34 def test_stdin_by_api(self):
35 """Exercise SBProcess.PutSTDIN()."""
38 self.run_process(True)
39 output = self.process.GetSTDOUT(1000)
40 self.check_process_output(output, output)
42 @skipIfWindows # stdio manipulation unsupported on Windows
43 @add_test_categories(['pyapi'])
44 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
45 def test_stdin_redirection(self):
46 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
50 self.run_process(False)
51 output = self.process.GetSTDOUT(1000)
52 self.check_process_output(output, output)
54 @skipIfWindows # stdio manipulation unsupported on Windows
55 @add_test_categories(['pyapi'])
56 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
57 def test_stdout_redirection(self):
58 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
61 self.redirect_stdout()
62 self.run_process(True)
63 output = self.read_output_file_and_delete()
64 error = self.process.GetSTDOUT(1000)
65 self.check_process_output(output, error)
67 @skipIfWindows # stdio manipulation unsupported on Windows
68 @add_test_categories(['pyapi'])
69 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
70 def test_stderr_redirection(self):
71 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
74 self.redirect_stderr()
75 self.run_process(True)
76 output = self.process.GetSTDOUT(1000)
77 error = self.read_error_file_and_delete()
78 self.check_process_output(output, error)
80 @skipIfWindows # stdio manipulation unsupported on Windows
81 @add_test_categories(['pyapi'])
82 @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
83 def test_stdout_stderr_redirection(self):
84 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
87 self.redirect_stdout()
88 self.redirect_stderr()
89 self.run_process(True)
90 output = self.read_output_file_and_delete()
91 error = self.read_error_file_and_delete()
92 self.check_process_output(output, error)
94 # target_file - path on local file system or remote file system if running remote
95 # local_file - path on local system
96 def read_file_and_delete(self, target_file, local_file):
97 if lldb.remote_platform:
98 self.runCmd('platform get-file "{remote}" "{local}"'.format(
99 remote=target_file, local=local_file))
101 self.assertTrue(os.path.exists(local_file), 'Make sure "{local}" file exists'.format(local=local_file))
102 f = open(local_file, 'r')
106 #TODO: add 'platform delete-file' file command
107 #if lldb.remote_platform:
108 # self.runCmd('platform delete-file "{remote}"'.format(remote=target_file))
109 os.unlink(local_file)
112 def read_output_file_and_delete(self):
113 return self.read_file_and_delete(self.output_file, self.local_output_file)
115 def read_error_file_and_delete(self):
116 return self.read_file_and_delete(self.error_file, self.local_error_file)
118 def create_target(self):
119 '''Create the target and launch info that will be used by all tests'''
120 self.target = self.dbg.CreateTarget(self.exe)
121 self.launch_info = lldb.SBLaunchInfo([self.exe])
122 self.launch_info.SetWorkingDirectory(self.get_process_working_directory())
124 def redirect_stdin(self):
125 '''Redirect STDIN (file descriptor 0) to use our input.txt file
127 Make the input.txt file to use when redirecting STDIN, setup a cleanup action
128 to delete the input.txt at the end of the test in case exceptions are thrown,
129 and redirect STDIN in the launch info.'''
130 f = open(self.local_input_file, 'w')
131 for line in self.lines:
135 if lldb.remote_platform:
136 self.runCmd('platform put-file "{local}" "{remote}"'.format(
137 local=self.local_input_file, remote=self.input_file))
139 # This is the function to remove the custom formats in order to have a
140 # clean slate for the next test case.
142 os.unlink(self.local_input_file)
143 #TODO: add 'platform delete-file' file command
144 #if lldb.remote_platform:
145 # self.runCmd('platform delete-file "{remote}"'.format(remote=self.input_file))
147 # Execute the cleanup function during test case tear down.
148 self.addTearDownHook(cleanup)
149 self.launch_info.AddOpenFileAction(0, self.input_file, True, False);
151 def redirect_stdout(self):
152 '''Redirect STDOUT (file descriptor 1) to use our output.txt file'''
153 self.launch_info.AddOpenFileAction(1, self.output_file, False, True);
155 def redirect_stderr(self):
156 '''Redirect STDERR (file descriptor 2) to use our error.txt file'''
157 self.launch_info.AddOpenFileAction(2, self.error_file, False, True);
159 def run_process(self, put_stdin):
160 '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True'''
161 # Set the breakpoints
162 self.breakpoint = self.target.BreakpointCreateBySourceRegex('Set breakpoint here', lldb.SBFileSpec("main.c"))
163 self.assertTrue(self.breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
165 # Launch the process, and do not stop at the entry point.
166 error = lldb.SBError()
167 # This should launch the process and it should exit by the time we get back
168 # because we have synchronous mode enabled
169 self.process = self.target.Launch (self.launch_info, error)
171 self.assertTrue(error.Success(), "Make sure process launched successfully")
172 self.assertTrue(self.process, PROCESS_IS_VALID)
175 print("process launched.")
177 # Frame #0 should be at our breakpoint.
178 threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, self.breakpoint)
180 self.assertTrue(len(threads) == 1)
181 self.thread = threads[0]
182 self.frame = self.thread.frames[0]
183 self.assertTrue(self.frame, "Frame 0 is valid.")
186 print("process stopped at breakpoint, sending STDIN via LLDB API.")
188 # Write data to stdin via the public API if we were asked to
190 for line in self.lines:
191 self.process.PutSTDIN(line + "\n")
193 # Let process continue so it will exit
194 self.process.Continue()
195 state = self.process.GetState()
196 self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID)
198 def check_process_output (self, output, error):
199 # Since we launched the process without specifying stdin/out/err,
200 # a pseudo terminal is used for stdout/err, and we are satisfied
201 # once "input line=>1" appears in stdout.
204 print("output = '%s'" % output)
205 print("error = '%s'" % error)
207 for line in self.lines:
208 check_line = 'input line to stdout: %s' % (line)
209 self.assertTrue(check_line in output, "verify stdout line shows up in STDOUT")
210 for line in self.lines:
211 check_line = 'input line to stderr: %s' % (line)
212 self.assertTrue(check_line in error, "verify stderr line shows up in STDERR")