Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 1 | """Test Python APIs for process IO.""" |
| 2 | |
| 3 | import os, sys, time |
| 4 | import unittest2 |
| 5 | import lldb |
| 6 | from lldbtest import * |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 7 | import lldbutil |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 8 | |
| 9 | class ProcessIOTestCase(TestBase): |
| 10 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 11 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 12 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 13 | @skipUnlessDarwin |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 14 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 15 | @dsym_test |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 16 | def test_stdin_by_api_with_dsym(self): |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 17 | """Exercise SBProcess.PutSTDIN().""" |
| 18 | self.buildDsym() |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 19 | self.do_stdin_by_api() |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 20 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 21 | @skipIfWindows # stdio manipulation unsupported on Windows |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 22 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 23 | @dwarf_test |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 24 | def test_stdin_by_api_with_dwarf(self): |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 25 | """Exercise SBProcess.PutSTDIN().""" |
| 26 | self.buildDwarf() |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 27 | self.do_stdin_by_api() |
| 28 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 29 | @skipUnlessDarwin |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 30 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 37 | @skipIfWindows # stdio manipulation unsupported on Windows |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 38 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 45 | @skipUnlessDarwin |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 46 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 53 | @skipIfWindows # stdio manipulation unsupported on Windows |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 54 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 61 | @skipUnlessDarwin |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 62 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 69 | @skipIfWindows # stdio manipulation unsupported on Windows |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 70 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 77 | @skipUnlessDarwin |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 78 | @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 Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame^] | 85 | @skipIfWindows # stdio manipulation unsupported on Windows |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 86 | @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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 92 | |
| 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 Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 98 | 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 Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 105 | self.lines = ["Line 1", "Line 2", "Line 3"] |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 106 | |
| 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 Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 116 | contents = f.read() |
| 117 | f.close() |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 118 | |
| 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 Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 123 | return contents |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 124 | |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 125 | def read_output_file_and_delete(self): |
| 126 | return self.read_file_and_delete(self.output_file, self.local_output_file) |
| 127 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 128 | def read_error_file_and_delete(self): |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 129 | return self.read_file_and_delete(self.error_file, self.local_error_file) |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 130 | |
| 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 Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 143 | f = open(self.local_input_file, 'w') |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 144 | for line in self.lines: |
| 145 | f.write(line + "\n") |
| 146 | f.close() |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 147 | |
| 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 Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 152 | # 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 Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 155 | os.unlink(self.local_input_file) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 156 | #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 Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 159 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 160 | # 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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 221 | """Launch a process and use SBProcess.PutSTDIN() to write data to it.""" |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 222 | 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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 232 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 233 | # 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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 238 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 239 | self.assertTrue(error.Success(), "Make sure process launched successfully") |
| 240 | self.assertTrue(self.process, PROCESS_IS_VALID) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 241 | |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 242 | if self.TraceOn(): |
| 243 | print "process launched." |
| 244 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 245 | # 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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 252 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 253 | if self.TraceOn(): |
| 254 | print "process stopped at breakpoint, sending STDIN via LLDB API." |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 255 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 256 | # 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 Chen | 5886fb5 | 2012-01-12 00:29:46 +0000 | [diff] [blame] | 267 | # 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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 270 | # See also main.c. |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 271 | 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 Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 281 | |
| 282 | if __name__ == '__main__': |
| 283 | import atexit |
| 284 | lldb.SBDebugger.Initialize() |
| 285 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 286 | unittest2.main() |