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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 13 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin") |
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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 21 | @unittest2.skipIf(sys.platform.startswith("win32"), "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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 29 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin") |
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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 37 | @unittest2.skipIf(sys.platform.startswith("win32"), "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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 45 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin") |
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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 53 | @unittest2.skipIf(sys.platform.startswith("win32"), "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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 61 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin") |
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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 69 | @unittest2.skipIf(sys.platform.startswith("win32"), "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 | |
Zachary Turner | 82da55f | 2014-11-25 19:03:08 +0000 | [diff] [blame] | 77 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "dsym requires Darwin") |
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 | |
Zachary Turner | 29278a6 | 2014-12-10 23:25:28 +0000 | [diff] [blame] | 85 | @unittest2.skipIf(sys.platform.startswith("win32"), "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"] |
| 106 | |
| 107 | def read_output_file_and_delete (self): |
| 108 | self.assertTrue(os.path.exists(self.output_file), "Make sure output.txt file exists") |
| 109 | f = open(self.output_file, 'r') |
| 110 | contents = f.read() |
| 111 | f.close() |
| 112 | os.unlink(self.output_file) |
| 113 | return contents |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 114 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 115 | def read_error_file_and_delete(self): |
| 116 | self.assertTrue(os.path.exists(self.error_file), "Make sure error.txt file exists") |
| 117 | f = open(self.error_file, 'r') |
| 118 | contents = f.read() |
| 119 | f.close() |
| 120 | os.unlink(self.error_file) |
| 121 | return contents |
| 122 | |
| 123 | def create_target(self): |
| 124 | '''Create the target and launch info that will be used by all tests''' |
| 125 | self.target = self.dbg.CreateTarget(self.exe) |
| 126 | self.launch_info = lldb.SBLaunchInfo([self.exe]) |
| 127 | self.launch_info.SetWorkingDirectory(self.get_process_working_directory()) |
| 128 | |
| 129 | def redirect_stdin(self): |
| 130 | '''Redirect STDIN (file descriptor 0) to use our input.txt file |
| 131 | |
| 132 | Make the input.txt file to use when redirecting STDIN, setup a cleanup action |
| 133 | to delete the input.txt at the end of the test in case exceptions are thrown, |
| 134 | and redirect STDIN in the launch info.''' |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame^] | 135 | f = open(self.local_input_file, 'w') |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 136 | for line in self.lines: |
| 137 | f.write(line + "\n") |
| 138 | f.close() |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame^] | 139 | |
| 140 | if lldb.remote_platform: |
| 141 | self.runCmd('platform put-file "{local}" "{remote}"'.format( |
| 142 | local=self.local_input_file, remote=self.input_file)) |
| 143 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 144 | # This is the function to remove the custom formats in order to have a |
| 145 | # clean slate for the next test case. |
| 146 | def cleanup(): |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame^] | 147 | os.unlink(self.local_input_file) |
| 148 | # if lldb.remote_platform: |
| 149 | # TODO: add delete file command |
| 150 | # self.runCmd('platform delete-file "{local}" "{remote}"'.format( |
| 151 | # local=self.local_input_file, remote=self.input_file))''' |
| 152 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 153 | # Execute the cleanup function during test case tear down. |
| 154 | self.addTearDownHook(cleanup) |
| 155 | self.launch_info.AddOpenFileAction(0, self.input_file, True, False); |
| 156 | |
| 157 | def redirect_stdout(self): |
| 158 | '''Redirect STDOUT (file descriptor 1) to use our output.txt file''' |
| 159 | self.launch_info.AddOpenFileAction(1, self.output_file, False, True); |
| 160 | |
| 161 | def redirect_stderr(self): |
| 162 | '''Redirect STDERR (file descriptor 2) to use our error.txt file''' |
| 163 | self.launch_info.AddOpenFileAction(2, self.error_file, False, True); |
| 164 | |
| 165 | def do_stdin_redirection(self): |
| 166 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR.""" |
| 167 | self.create_target() |
| 168 | self.redirect_stdin() |
| 169 | self.run_process(False) |
| 170 | output = self.process.GetSTDOUT(1000) |
| 171 | self.check_process_output(output, output) |
| 172 | |
| 173 | def do_stdout_redirection(self): |
| 174 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR.""" |
| 175 | self.create_target() |
| 176 | self.redirect_stdout() |
| 177 | self.run_process(True) |
| 178 | output = self.read_output_file_and_delete() |
| 179 | error = self.process.GetSTDOUT(1000) |
| 180 | self.check_process_output(output, error) |
| 181 | |
| 182 | def do_stderr_redirection(self): |
| 183 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT.""" |
| 184 | self.create_target() |
| 185 | self.redirect_stderr() |
| 186 | self.run_process(True) |
| 187 | output = self.process.GetSTDOUT(1000) |
| 188 | error = self.read_error_file_and_delete() |
| 189 | self.check_process_output(output, error) |
| 190 | |
| 191 | def do_stdout_stderr_redirection(self): |
| 192 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN.""" |
| 193 | self.create_target() |
| 194 | self.redirect_stdout() |
| 195 | self.redirect_stderr() |
| 196 | self.run_process(True) |
| 197 | output = self.read_output_file_and_delete() |
| 198 | error = self.read_error_file_and_delete() |
| 199 | self.check_process_output(output, error) |
| 200 | |
| 201 | def do_stdin_stdout_stderr_redirection(self): |
| 202 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN, STDOUT and STDERR.""" |
| 203 | # Make the input.txt file to use |
| 204 | self.create_target() |
| 205 | self.redirect_stdin() |
| 206 | self.redirect_stdout() |
| 207 | self.redirect_stderr() |
| 208 | self.run_process(True) |
| 209 | output = self.read_output_file_and_delete() |
| 210 | error = self.read_error_file_and_delete() |
| 211 | self.check_process_output(output, error) |
| 212 | |
| 213 | def do_stdin_by_api(self): |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 214 | """Launch a process and use SBProcess.PutSTDIN() to write data to it.""" |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 215 | self.create_target() |
| 216 | self.run_process(True) |
| 217 | output = self.process.GetSTDOUT(1000) |
| 218 | self.check_process_output(output, output) |
| 219 | |
| 220 | def run_process(self, put_stdin): |
| 221 | '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True''' |
| 222 | # Set the breakpoints |
| 223 | self.breakpoint = self.target.BreakpointCreateBySourceRegex('Set breakpoint here', lldb.SBFileSpec("main.c")) |
| 224 | self.assertTrue(self.breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 225 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 226 | # Launch the process, and do not stop at the entry point. |
| 227 | error = lldb.SBError() |
| 228 | # This should launch the process and it should exit by the time we get back |
| 229 | # because we have synchronous mode enabled |
| 230 | self.process = self.target.Launch (self.launch_info, error) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 231 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 232 | self.assertTrue(error.Success(), "Make sure process launched successfully") |
| 233 | self.assertTrue(self.process, PROCESS_IS_VALID) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 234 | |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 235 | if self.TraceOn(): |
| 236 | print "process launched." |
| 237 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 238 | # Frame #0 should be at our breakpoint. |
| 239 | threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, self.breakpoint) |
| 240 | |
| 241 | self.assertTrue(len(threads) == 1) |
| 242 | self.thread = threads[0] |
| 243 | self.frame = self.thread.frames[0] |
| 244 | self.assertTrue(self.frame, "Frame 0 is valid.") |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 245 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 246 | if self.TraceOn(): |
| 247 | print "process stopped at breakpoint, sending STDIN via LLDB API." |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 248 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 249 | # Write data to stdin via the public API if we were asked to |
| 250 | if put_stdin: |
| 251 | for line in self.lines: |
| 252 | self.process.PutSTDIN(line + "\n") |
| 253 | |
| 254 | # Let process continue so it will exit |
| 255 | self.process.Continue() |
| 256 | state = self.process.GetState() |
| 257 | self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID) |
| 258 | |
| 259 | def check_process_output (self, output, error): |
Johnny Chen | 5886fb5 | 2012-01-12 00:29:46 +0000 | [diff] [blame] | 260 | # Since we launched the process without specifying stdin/out/err, |
| 261 | # a pseudo terminal is used for stdout/err, and we are satisfied |
| 262 | # once "input line=>1" appears in stdout. |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 263 | # See also main.c. |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 264 | if self.TraceOn(): |
| 265 | print "output = '%s'" % output |
| 266 | print "error = '%s'" % error |
| 267 | |
| 268 | for line in self.lines: |
| 269 | check_line = 'input line to stdout: %s' % (line) |
| 270 | self.assertTrue(check_line in output, "verify stdout line shows up in STDOUT") |
| 271 | for line in self.lines: |
| 272 | check_line = 'input line to stderr: %s' % (line) |
| 273 | 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] | 274 | |
| 275 | if __name__ == '__main__': |
| 276 | import atexit |
| 277 | lldb.SBDebugger.Initialize() |
| 278 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 279 | unittest2.main() |