blob: 9bd3b82a4f5b9185b8bb70b74f50b9cc11ad3f9b [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
Johnny Chen49cb85d2011-11-28 21:39:07 +000013 def setUp(self):
14 # Call super's setUp().
15 TestBase.setUp(self)
16 # Get the full path to our executable to be debugged.
17 self.exe = os.path.join(os.getcwd(), "process_io")
Vince Harrondf3f00f2015-02-10 21:09:04 +000018 self.local_input_file = os.path.join(os.getcwd(), "input.txt")
19 self.local_output_file = os.path.join(os.getcwd(), "output.txt")
20 self.local_error_file = os.path.join(os.getcwd(), "error.txt")
21
22 self.input_file = os.path.join(self.get_process_working_directory(), "input.txt")
23 self.output_file = os.path.join(self.get_process_working_directory(), "output.txt")
24 self.error_file = os.path.join(self.get_process_working_directory(), "error.txt")
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000025 self.lines = ["Line 1", "Line 2", "Line 3"]
Vince Harron4a8abd32015-02-13 19:15:24 +000026
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000027 @skipIfWindows # stdio manipulation unsupported on Windows
28 @python_api_test
29 def test_stdin_by_api(self):
30 """Exercise SBProcess.PutSTDIN()."""
31 self.build()
32 self.create_target()
33 self.run_process(True)
34 output = self.process.GetSTDOUT(1000)
35 self.check_process_output(output, output)
36
37 @skipIfWindows # stdio manipulation unsupported on Windows
38 @python_api_test
39 def test_stdin_redirection(self):
40 """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
41 self.build()
42 self.create_target()
43 self.redirect_stdin()
44 self.run_process(False)
45 output = self.process.GetSTDOUT(1000)
46 self.check_process_output(output, output)
47
48 @skipIfWindows # stdio manipulation unsupported on Windows
49 @python_api_test
50 def test_stdout_redirection(self):
51 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
52 self.build()
53 self.create_target()
54 self.redirect_stdout()
55 self.run_process(True)
56 output = self.read_output_file_and_delete()
57 error = self.process.GetSTDOUT(1000)
58 self.check_process_output(output, error)
59
60 @skipIfWindows # stdio manipulation unsupported on Windows
61 @python_api_test
62 def test_stderr_redirection(self):
63 """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
64 self.build()
65 self.create_target()
66 self.redirect_stderr()
67 self.run_process(True)
68 output = self.process.GetSTDOUT(1000)
69 error = self.read_error_file_and_delete()
70 self.check_process_output(output, error)
71
72 @skipIfWindows # stdio manipulation unsupported on Windows
73 @python_api_test
74 def test_stdout_stderr_redirection(self):
75 """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
76 self.build()
77 self.create_target()
78 self.redirect_stdout()
79 self.redirect_stderr()
80 self.run_process(True)
81 output = self.read_output_file_and_delete()
82 error = self.read_error_file_and_delete()
83 self.check_process_output(output, error)
84
Vince Harron4a8abd32015-02-13 19:15:24 +000085 # target_file - path on local file system or remote file system if running remote
86 # local_file - path on local system
87 def read_file_and_delete(self, target_file, local_file):
88 if lldb.remote_platform:
89 self.runCmd('platform get-file "{remote}" "{local}"'.format(
90 remote=target_file, local=local_file))
91
92 self.assertTrue(os.path.exists(local_file), 'Make sure "{local}" file exists'.format(local=local_file))
93 f = open(local_file, 'r')
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000094 contents = f.read()
95 f.close()
Vince Harron4a8abd32015-02-13 19:15:24 +000096
97 #TODO: add 'platform delete-file' file command
98 #if lldb.remote_platform:
99 # self.runCmd('platform delete-file "{remote}"'.format(remote=target_file))
100 os.unlink(local_file)
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000101 return contents
Johnny Chen49cb85d2011-11-28 21:39:07 +0000102
Vince Harron4a8abd32015-02-13 19:15:24 +0000103 def read_output_file_and_delete(self):
104 return self.read_file_and_delete(self.output_file, self.local_output_file)
105
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000106 def read_error_file_and_delete(self):
Vince Harron4a8abd32015-02-13 19:15:24 +0000107 return self.read_file_and_delete(self.error_file, self.local_error_file)
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000108
109 def create_target(self):
110 '''Create the target and launch info that will be used by all tests'''
111 self.target = self.dbg.CreateTarget(self.exe)
112 self.launch_info = lldb.SBLaunchInfo([self.exe])
113 self.launch_info.SetWorkingDirectory(self.get_process_working_directory())
114
115 def redirect_stdin(self):
116 '''Redirect STDIN (file descriptor 0) to use our input.txt file
117
118 Make the input.txt file to use when redirecting STDIN, setup a cleanup action
119 to delete the input.txt at the end of the test in case exceptions are thrown,
120 and redirect STDIN in the launch info.'''
Vince Harrondf3f00f2015-02-10 21:09:04 +0000121 f = open(self.local_input_file, 'w')
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000122 for line in self.lines:
123 f.write(line + "\n")
124 f.close()
Vince Harrondf3f00f2015-02-10 21:09:04 +0000125
126 if lldb.remote_platform:
127 self.runCmd('platform put-file "{local}" "{remote}"'.format(
128 local=self.local_input_file, remote=self.input_file))
129
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000130 # This is the function to remove the custom formats in order to have a
131 # clean slate for the next test case.
132 def cleanup():
Vince Harrondf3f00f2015-02-10 21:09:04 +0000133 os.unlink(self.local_input_file)
Vince Harron4a8abd32015-02-13 19:15:24 +0000134 #TODO: add 'platform delete-file' file command
135 #if lldb.remote_platform:
136 # self.runCmd('platform delete-file "{remote}"'.format(remote=self.input_file))
Vince Harrondf3f00f2015-02-10 21:09:04 +0000137
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000138 # Execute the cleanup function during test case tear down.
139 self.addTearDownHook(cleanup)
140 self.launch_info.AddOpenFileAction(0, self.input_file, True, False);
141
142 def redirect_stdout(self):
143 '''Redirect STDOUT (file descriptor 1) to use our output.txt file'''
144 self.launch_info.AddOpenFileAction(1, self.output_file, False, True);
145
146 def redirect_stderr(self):
147 '''Redirect STDERR (file descriptor 2) to use our error.txt file'''
148 self.launch_info.AddOpenFileAction(2, self.error_file, False, True);
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000149
150 def run_process(self, put_stdin):
151 '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True'''
152 # Set the breakpoints
153 self.breakpoint = self.target.BreakpointCreateBySourceRegex('Set breakpoint here', lldb.SBFileSpec("main.c"))
154 self.assertTrue(self.breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
Johnny Chen49cb85d2011-11-28 21:39:07 +0000155
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000156 # Launch the process, and do not stop at the entry point.
157 error = lldb.SBError()
158 # This should launch the process and it should exit by the time we get back
159 # because we have synchronous mode enabled
160 self.process = self.target.Launch (self.launch_info, error)
Johnny Chen49cb85d2011-11-28 21:39:07 +0000161
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000162 self.assertTrue(error.Success(), "Make sure process launched successfully")
163 self.assertTrue(self.process, PROCESS_IS_VALID)
Daniel Malea249287a2013-02-19 16:08:57 +0000164
Johnny Chen49cb85d2011-11-28 21:39:07 +0000165 if self.TraceOn():
166 print "process launched."
167
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000168 # Frame #0 should be at our breakpoint.
169 threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, self.breakpoint)
170
171 self.assertTrue(len(threads) == 1)
172 self.thread = threads[0]
173 self.frame = self.thread.frames[0]
174 self.assertTrue(self.frame, "Frame 0 is valid.")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000175
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000176 if self.TraceOn():
177 print "process stopped at breakpoint, sending STDIN via LLDB API."
Johnny Chen49cb85d2011-11-28 21:39:07 +0000178
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000179 # Write data to stdin via the public API if we were asked to
180 if put_stdin:
181 for line in self.lines:
182 self.process.PutSTDIN(line + "\n")
183
184 # Let process continue so it will exit
185 self.process.Continue()
186 state = self.process.GetState()
187 self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID)
188
189 def check_process_output (self, output, error):
Johnny Chen5886fb52012-01-12 00:29:46 +0000190 # Since we launched the process without specifying stdin/out/err,
191 # a pseudo terminal is used for stdout/err, and we are satisfied
192 # once "input line=>1" appears in stdout.
Johnny Chen49cb85d2011-11-28 21:39:07 +0000193 # See also main.c.
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000194 if self.TraceOn():
195 print "output = '%s'" % output
196 print "error = '%s'" % error
197
198 for line in self.lines:
199 check_line = 'input line to stdout: %s' % (line)
200 self.assertTrue(check_line in output, "verify stdout line shows up in STDOUT")
201 for line in self.lines:
202 check_line = 'input line to stderr: %s' % (line)
203 self.assertTrue(check_line in error, "verify stderr line shows up in STDERR")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000204
205if __name__ == '__main__':
206 import atexit
207 lldb.SBDebugger.Initialize()
208 atexit.register(lambda: lldb.SBDebugger.Terminate())
209 unittest2.main()