xref: /llvm-project/lldb/test/API/python_api/process/io/TestProcessIO.py (revision f7c36d2f88e05a1747fa7916ad2fefdd9d459a55)
199451b44SJordan Rupprecht"""Test Python APIs for process IO."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprechtimport os
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass ProcessIOTestCase(TestBase):
1199451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
1299451b44SJordan Rupprecht
1399451b44SJordan Rupprecht    def setup_test(self):
1499451b44SJordan Rupprecht        # Get the full path to our executable to be debugged.
1599451b44SJordan Rupprecht        self.exe = self.getBuildArtifact("process_io")
1699451b44SJordan Rupprecht        self.local_input_file = self.getBuildArtifact("input.txt")
1799451b44SJordan Rupprecht        self.local_output_file = self.getBuildArtifact("output.txt")
1899451b44SJordan Rupprecht        self.local_error_file = self.getBuildArtifact("error.txt")
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht        self.input_file = os.path.join(
212238dcc3SJonas Devlieghere            self.get_process_working_directory(), "input.txt"
222238dcc3SJonas Devlieghere        )
2399451b44SJordan Rupprecht        self.output_file = os.path.join(
242238dcc3SJonas Devlieghere            self.get_process_working_directory(), "output.txt"
252238dcc3SJonas Devlieghere        )
2699451b44SJordan Rupprecht        self.error_file = os.path.join(
272238dcc3SJonas Devlieghere            self.get_process_working_directory(), "error.txt"
282238dcc3SJonas Devlieghere        )
2999451b44SJordan Rupprecht        self.lines = ["Line 1", "Line 2", "Line 3"]
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht    @skipIfWindows  # stdio manipulation unsupported on Windows
3299451b44SJordan Rupprecht    @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
3399451b44SJordan Rupprecht    @skipIfDarwinEmbedded  # I/O redirection like this is not supported on remote iOS devices yet <rdar://problem/54581135>
3499451b44SJordan Rupprecht    def test_stdin_by_api(self):
3599451b44SJordan Rupprecht        """Exercise SBProcess.PutSTDIN()."""
3699451b44SJordan Rupprecht        self.setup_test()
3799451b44SJordan Rupprecht        self.build()
3899451b44SJordan Rupprecht        self.create_target()
3999451b44SJordan Rupprecht        self.run_process(True)
4099451b44SJordan Rupprecht        output = self.process.GetSTDOUT(1000)
4199451b44SJordan Rupprecht        self.check_process_output(output, output)
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht    @skipIfWindows  # stdio manipulation unsupported on Windows
4499451b44SJordan Rupprecht    @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
4599451b44SJordan Rupprecht    def test_stdin_redirection(self):
4699451b44SJordan Rupprecht        """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR."""
4799451b44SJordan Rupprecht        self.setup_test()
4899451b44SJordan Rupprecht        self.build()
4999451b44SJordan Rupprecht        self.create_target()
5099451b44SJordan Rupprecht        self.redirect_stdin()
5199451b44SJordan Rupprecht        self.run_process(False)
5299451b44SJordan Rupprecht        output = self.process.GetSTDOUT(1000)
5399451b44SJordan Rupprecht        self.check_process_output(output, output)
5499451b44SJordan Rupprecht
5599451b44SJordan Rupprecht    @skipIfWindows  # stdio manipulation unsupported on Windows
5699451b44SJordan Rupprecht    @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
5799451b44SJordan Rupprecht    @skipIfDarwinEmbedded  # debugserver can't create/write files on the device
5899451b44SJordan Rupprecht    def test_stdout_redirection(self):
5999451b44SJordan Rupprecht        """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR."""
6099451b44SJordan Rupprecht        self.setup_test()
6199451b44SJordan Rupprecht        self.build()
6299451b44SJordan Rupprecht        self.create_target()
6399451b44SJordan Rupprecht        self.redirect_stdout()
6499451b44SJordan Rupprecht        self.run_process(True)
6599451b44SJordan Rupprecht        output = self.read_output_file_and_delete()
6699451b44SJordan Rupprecht        error = self.process.GetSTDOUT(1000)
6799451b44SJordan Rupprecht        self.check_process_output(output, error)
6899451b44SJordan Rupprecht
6999451b44SJordan Rupprecht    @skipIfWindows  # stdio manipulation unsupported on Windows
7099451b44SJordan Rupprecht    @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
7199451b44SJordan Rupprecht    @skipIfDarwinEmbedded  # debugserver can't create/write files on the device
7299451b44SJordan Rupprecht    def test_stderr_redirection(self):
7399451b44SJordan Rupprecht        """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT."""
7499451b44SJordan Rupprecht        self.setup_test()
7599451b44SJordan Rupprecht        self.build()
7699451b44SJordan Rupprecht        self.create_target()
7799451b44SJordan Rupprecht        self.redirect_stderr()
7899451b44SJordan Rupprecht        self.run_process(True)
7999451b44SJordan Rupprecht        output = self.process.GetSTDOUT(1000)
8099451b44SJordan Rupprecht        error = self.read_error_file_and_delete()
8199451b44SJordan Rupprecht        self.check_process_output(output, error)
8299451b44SJordan Rupprecht
8399451b44SJordan Rupprecht    @skipIfWindows  # stdio manipulation unsupported on Windows
8499451b44SJordan Rupprecht    @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
8599451b44SJordan Rupprecht    @skipIfDarwinEmbedded  # debugserver can't create/write files on the device
8699451b44SJordan Rupprecht    def test_stdout_stderr_redirection(self):
8799451b44SJordan Rupprecht        """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN."""
8899451b44SJordan Rupprecht        self.setup_test()
8999451b44SJordan Rupprecht        self.build()
9099451b44SJordan Rupprecht        self.create_target()
9199451b44SJordan Rupprecht        self.redirect_stdout()
9299451b44SJordan Rupprecht        self.redirect_stderr()
9399451b44SJordan Rupprecht        self.run_process(True)
9499451b44SJordan Rupprecht        output = self.read_output_file_and_delete()
9599451b44SJordan Rupprecht        error = self.read_error_file_and_delete()
9699451b44SJordan Rupprecht        self.check_process_output(output, error)
9799451b44SJordan Rupprecht
98efc6d33bSWanyi    @skipIfWindows  # stdio manipulation unsupported on Windows
99efc6d33bSWanyi    @expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
100efc6d33bSWanyi    @skipIfDarwinEmbedded  # debugserver can't create/write files on the device
101efc6d33bSWanyi    def test_stdout_stderr_redirection_to_existing_files(self):
102*f7c36d2fSWanyi        """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR redirect to output files already exist."""
103efc6d33bSWanyi        self.setup_test()
104efc6d33bSWanyi        self.build()
105efc6d33bSWanyi        self.create_target()
106*f7c36d2fSWanyi
107*f7c36d2fSWanyi        # Create the output and error files with placeholder
108*f7c36d2fSWanyi        placeholder = "This content should be overwritten."
109*f7c36d2fSWanyi        # Local file directory and working directory are the same for local debugging
110*f7c36d2fSWanyi        f = open(self.local_output_file, "w")
111*f7c36d2fSWanyi        f.write(placeholder)
112*f7c36d2fSWanyi        f.close()
113*f7c36d2fSWanyi        f = open(self.local_error_file, "w")
114*f7c36d2fSWanyi        f.write(placeholder)
115*f7c36d2fSWanyi        f.close()
116*f7c36d2fSWanyi        if lldb.remote_platform:
117*f7c36d2fSWanyi            self.runCmd(
118*f7c36d2fSWanyi                'platform put-file "{local}" "{remote}"'.format(
119*f7c36d2fSWanyi                    local=self.local_output_file, remote=self.output_file
120*f7c36d2fSWanyi                )
121*f7c36d2fSWanyi            )
122*f7c36d2fSWanyi            self.runCmd(
123*f7c36d2fSWanyi                'platform put-file "{local}" "{remote}"'.format(
124*f7c36d2fSWanyi                    local=self.local_error_file, remote=self.error_file
125*f7c36d2fSWanyi                )
126*f7c36d2fSWanyi            )
127*f7c36d2fSWanyi
128efc6d33bSWanyi        self.redirect_stdout()
129efc6d33bSWanyi        self.redirect_stderr()
130efc6d33bSWanyi        self.run_process(True)
131efc6d33bSWanyi        output = self.read_output_file_and_delete()
132efc6d33bSWanyi        error = self.read_error_file_and_delete()
133efc6d33bSWanyi        self.check_process_output(output, error)
134efc6d33bSWanyi
13599451b44SJordan Rupprecht    # target_file - path on local file system or remote file system if running remote
13699451b44SJordan Rupprecht    # local_file - path on local system
13799451b44SJordan Rupprecht    def read_file_and_delete(self, target_file, local_file):
13899451b44SJordan Rupprecht        if lldb.remote_platform:
1392238dcc3SJonas Devlieghere            self.runCmd(
1402238dcc3SJonas Devlieghere                'platform get-file "{remote}" "{local}"'.format(
1412238dcc3SJonas Devlieghere                    remote=target_file, local=local_file
1422238dcc3SJonas Devlieghere                )
1432238dcc3SJonas Devlieghere            )
14499451b44SJordan Rupprecht
14599451b44SJordan Rupprecht        self.assertTrue(
14699451b44SJordan Rupprecht            os.path.exists(local_file),
1472238dcc3SJonas Devlieghere            'Make sure "{local}" file exists'.format(local=local_file),
1482238dcc3SJonas Devlieghere        )
1492238dcc3SJonas Devlieghere        f = open(local_file, "r")
15099451b44SJordan Rupprecht        contents = f.read()
15199451b44SJordan Rupprecht        f.close()
15299451b44SJordan Rupprecht
15399451b44SJordan Rupprecht        # TODO: add 'platform delete-file' file command
15499451b44SJordan Rupprecht        # if lldb.remote_platform:
15599451b44SJordan Rupprecht        #    self.runCmd('platform delete-file "{remote}"'.format(remote=target_file))
15699451b44SJordan Rupprecht        os.unlink(local_file)
15799451b44SJordan Rupprecht        return contents
15899451b44SJordan Rupprecht
15999451b44SJordan Rupprecht    def read_output_file_and_delete(self):
1602238dcc3SJonas Devlieghere        return self.read_file_and_delete(self.output_file, self.local_output_file)
16199451b44SJordan Rupprecht
16299451b44SJordan Rupprecht    def read_error_file_and_delete(self):
1632238dcc3SJonas Devlieghere        return self.read_file_and_delete(self.error_file, self.local_error_file)
16499451b44SJordan Rupprecht
16599451b44SJordan Rupprecht    def create_target(self):
1662238dcc3SJonas Devlieghere        """Create the target and launch info that will be used by all tests"""
16799451b44SJordan Rupprecht        self.target = self.dbg.CreateTarget(self.exe)
16886aa8e63SJonas Devlieghere        self.launch_info = self.target.GetLaunchInfo()
1692238dcc3SJonas Devlieghere        self.launch_info.SetWorkingDirectory(self.get_process_working_directory())
17099451b44SJordan Rupprecht
17199451b44SJordan Rupprecht    def redirect_stdin(self):
1722238dcc3SJonas Devlieghere        """Redirect STDIN (file descriptor 0) to use our input.txt file
17399451b44SJordan Rupprecht
17499451b44SJordan Rupprecht        Make the input.txt file to use when redirecting STDIN, setup a cleanup action
17599451b44SJordan Rupprecht        to delete the input.txt at the end of the test in case exceptions are thrown,
1762238dcc3SJonas Devlieghere        and redirect STDIN in the launch info."""
1772238dcc3SJonas Devlieghere        f = open(self.local_input_file, "w")
17899451b44SJordan Rupprecht        for line in self.lines:
17999451b44SJordan Rupprecht            f.write(line + "\n")
18099451b44SJordan Rupprecht        f.close()
18199451b44SJordan Rupprecht
18299451b44SJordan Rupprecht        if lldb.remote_platform:
1832238dcc3SJonas Devlieghere            self.runCmd(
1842238dcc3SJonas Devlieghere                'platform put-file "{local}" "{remote}"'.format(
1852238dcc3SJonas Devlieghere                    local=self.local_input_file, remote=self.input_file
1862238dcc3SJonas Devlieghere                )
1872238dcc3SJonas Devlieghere            )
18899451b44SJordan Rupprecht
18999451b44SJordan Rupprecht        # This is the function to remove the custom formats in order to have a
19099451b44SJordan Rupprecht        # clean slate for the next test case.
19199451b44SJordan Rupprecht        def cleanup():
19299451b44SJordan Rupprecht            os.unlink(self.local_input_file)
19399451b44SJordan Rupprecht            # TODO: add 'platform delete-file' file command
19499451b44SJordan Rupprecht            # if lldb.remote_platform:
19599451b44SJordan Rupprecht            #    self.runCmd('platform delete-file "{remote}"'.format(remote=self.input_file))
19699451b44SJordan Rupprecht
19799451b44SJordan Rupprecht        # Execute the cleanup function during test case tear down.
19899451b44SJordan Rupprecht        self.addTearDownHook(cleanup)
19999451b44SJordan Rupprecht        self.launch_info.AddOpenFileAction(0, self.input_file, True, False)
20099451b44SJordan Rupprecht
20199451b44SJordan Rupprecht    def redirect_stdout(self):
2022238dcc3SJonas Devlieghere        """Redirect STDOUT (file descriptor 1) to use our output.txt file"""
20399451b44SJordan Rupprecht        self.launch_info.AddOpenFileAction(1, self.output_file, False, True)
20499451b44SJordan Rupprecht
20599451b44SJordan Rupprecht    def redirect_stderr(self):
2062238dcc3SJonas Devlieghere        """Redirect STDERR (file descriptor 2) to use our error.txt file"""
20799451b44SJordan Rupprecht        self.launch_info.AddOpenFileAction(2, self.error_file, False, True)
20899451b44SJordan Rupprecht
20999451b44SJordan Rupprecht    def run_process(self, put_stdin):
2102238dcc3SJonas Devlieghere        """Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True"""
21199451b44SJordan Rupprecht        # Set the breakpoints
21299451b44SJordan Rupprecht        self.breakpoint = self.target.BreakpointCreateBySourceRegex(
2132238dcc3SJonas Devlieghere            "Set breakpoint here", lldb.SBFileSpec("main.c")
2142238dcc3SJonas Devlieghere        )
2159c246882SJordan Rupprecht        self.assertGreater(self.breakpoint.GetNumLocations(), 0, VALID_BREAKPOINT)
21699451b44SJordan Rupprecht
21799451b44SJordan Rupprecht        # Launch the process, and do not stop at the entry point.
21899451b44SJordan Rupprecht        error = lldb.SBError()
21999451b44SJordan Rupprecht        # This should launch the process and it should exit by the time we get back
22099451b44SJordan Rupprecht        # because we have synchronous mode enabled
22199451b44SJordan Rupprecht        self.process = self.target.Launch(self.launch_info, error)
22299451b44SJordan Rupprecht
2232238dcc3SJonas Devlieghere        self.assertTrue(error.Success(), "Make sure process launched successfully")
22499451b44SJordan Rupprecht        self.assertTrue(self.process, PROCESS_IS_VALID)
22599451b44SJordan Rupprecht
22699451b44SJordan Rupprecht        if self.TraceOn():
22799451b44SJordan Rupprecht            print("process launched.")
22899451b44SJordan Rupprecht
22999451b44SJordan Rupprecht        # Frame #0 should be at our breakpoint.
23099451b44SJordan Rupprecht        threads = lldbutil.get_threads_stopped_at_breakpoint(
2312238dcc3SJonas Devlieghere            self.process, self.breakpoint
2322238dcc3SJonas Devlieghere        )
23399451b44SJordan Rupprecht
234619e2e09SDave Lee        self.assertEqual(len(threads), 1)
23599451b44SJordan Rupprecht        self.thread = threads[0]
23699451b44SJordan Rupprecht        self.frame = self.thread.frames[0]
23799451b44SJordan Rupprecht        self.assertTrue(self.frame, "Frame 0 is valid.")
23899451b44SJordan Rupprecht
23999451b44SJordan Rupprecht        if self.TraceOn():
24099451b44SJordan Rupprecht            print("process stopped at breakpoint, sending STDIN via LLDB API.")
24199451b44SJordan Rupprecht
24299451b44SJordan Rupprecht        # Write data to stdin via the public API if we were asked to
24399451b44SJordan Rupprecht        if put_stdin:
24499451b44SJordan Rupprecht            for line in self.lines:
24599451b44SJordan Rupprecht                self.process.PutSTDIN(line + "\n")
24699451b44SJordan Rupprecht
24799451b44SJordan Rupprecht        # Let process continue so it will exit
24899451b44SJordan Rupprecht        self.process.Continue()
24999451b44SJordan Rupprecht        state = self.process.GetState()
2501b8c7352SJonas Devlieghere        self.assertState(state, lldb.eStateExited, PROCESS_IS_VALID)
25199451b44SJordan Rupprecht
25299451b44SJordan Rupprecht    def check_process_output(self, output, error):
25399451b44SJordan Rupprecht        # Since we launched the process without specifying stdin/out/err,
25499451b44SJordan Rupprecht        # a pseudo terminal is used for stdout/err, and we are satisfied
25599451b44SJordan Rupprecht        # once "input line=>1" appears in stdout.
25699451b44SJordan Rupprecht        # See also main.c.
25799451b44SJordan Rupprecht        if self.TraceOn():
25899451b44SJordan Rupprecht            print("output = '%s'" % output)
25999451b44SJordan Rupprecht            print("error = '%s'" % error)
26099451b44SJordan Rupprecht
26199451b44SJordan Rupprecht        for line in self.lines:
2622238dcc3SJonas Devlieghere            check_line = "input line to stdout: %s" % (line)
2639c246882SJordan Rupprecht            self.assertIn(check_line, output, "verify stdout line shows up in STDOUT")
26499451b44SJordan Rupprecht        for line in self.lines:
2652238dcc3SJonas Devlieghere            check_line = "input line to stderr: %s" % (line)
2669c246882SJordan Rupprecht            self.assertIn(check_line, error, "verify stderr line shows up in STDERR")
267