]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/python_api/process/io/TestProcessIO.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / python_api / process / io / TestProcessIO.py
1 """Test Python APIs for process IO."""
2
3 from __future__ import print_function
4
5
6 import os
7 import sys
8 import time
9 import lldb
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
13
14
15 class ProcessIOTestCase(TestBase):
16
17     mydir = TestBase.compute_mydir(__file__)
18
19     def setUp(self):
20         # Call super's setUp().
21         TestBase.setUp(self)
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")
27
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"]
35
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()."""
41         self.build()
42         self.create_target()
43         self.run_process(True)
44         output = self.process.GetSTDOUT(1000)
45         self.check_process_output(output, output)
46
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."""
52         self.build()
53         self.create_target()
54         self.redirect_stdin()
55         self.run_process(False)
56         output = self.process.GetSTDOUT(1000)
57         self.check_process_output(output, output)
58
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."""
64         self.build()
65         self.create_target()
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)
71
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."""
77         self.build()
78         self.create_target()
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)
84
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."""
90         self.build()
91         self.create_target()
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)
98
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))
105
106         self.assertTrue(
107             os.path.exists(local_file),
108             'Make sure "{local}" file exists'.format(
109                 local=local_file))
110         f = open(local_file, 'r')
111         contents = f.read()
112         f.close()
113
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)
118         return contents
119
120     def read_output_file_and_delete(self):
121         return self.read_file_and_delete(
122             self.output_file, self.local_output_file)
123
124     def read_error_file_and_delete(self):
125         return self.read_file_and_delete(
126             self.error_file, self.local_error_file)
127
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())
134
135     def redirect_stdin(self):
136         '''Redirect STDIN (file descriptor 0) to use our input.txt file
137
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:
143             f.write(line + "\n")
144         f.close()
145
146         if lldb.remote_platform:
147             self.runCmd('platform put-file "{local}" "{remote}"'.format(
148                 local=self.local_input_file, remote=self.input_file))
149
150         # This is the function to remove the custom formats in order to have a
151         # clean slate for the next test case.
152         def cleanup():
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))
157
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)
161
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)
165
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)
169
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"))
175         self.assertTrue(
176             self.breakpoint.GetNumLocations() > 0,
177             VALID_BREAKPOINT)
178
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)
184
185         self.assertTrue(
186             error.Success(),
187             "Make sure process launched successfully")
188         self.assertTrue(self.process, PROCESS_IS_VALID)
189
190         if self.TraceOn():
191             print("process launched.")
192
193         # Frame #0 should be at our breakpoint.
194         threads = lldbutil.get_threads_stopped_at_breakpoint(
195             self.process, self.breakpoint)
196
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.")
201
202         if self.TraceOn():
203             print("process stopped at breakpoint, sending STDIN via LLDB API.")
204
205         # Write data to stdin via the public API if we were asked to
206         if put_stdin:
207             for line in self.lines:
208                 self.process.PutSTDIN(line + "\n")
209
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)
214
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.
219             # See also main.c.
220         if self.TraceOn():
221             print("output = '%s'" % output)
222             print("error = '%s'" % error)
223
224         for line in self.lines:
225             check_line = 'input line to stdout: %s' % (line)
226             self.assertTrue(
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)
231             self.assertTrue(
232                 check_line in error,
233                 "verify stderr line shows up in STDERR")