TestProcessLaunch.py revision e2ac6dec08024429fcc8ef1646a435a732e5e94d
1ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice"""
2b94250fd6ca03a311353b592b6bd1b2b2f1c32f0Johnny ChenTest lldb process launch flags.
3ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice"""
4ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
5ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Ticeimport os, time
6ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Ticeimport unittest2
7ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Ticeimport lldb
8ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Ticefrom lldbtest import *
9ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
10803c97980675a9bcbb33802c3be65afff65cc301Johnny Chenclass ProcessLaunchTestCase(TestBase):
11ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
12e2ac6dec08024429fcc8ef1646a435a732e5e94dJohnny Chen    mydir = os.path.join("functionalities", "process_launch")
13ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
14ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
15803c97980675a9bcbb33802c3be65afff65cc301Johnny Chen    def test_io_with_dsym (self):
167d71b1a274b3ba7bcc441326d661b2d69977d243Johnny Chen        """Test that process launch I/O redirection flags work properly."""
17ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        self.buildDsym ()
18ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        self.process_io_test ()
19ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
20803c97980675a9bcbb33802c3be65afff65cc301Johnny Chen    def test_io_with_dwarf (self):
217d71b1a274b3ba7bcc441326d661b2d69977d243Johnny Chen        """Test that process launch I/O redirection flags work properly."""
22ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        self.buildDwarf ()
23ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        self.process_io_test ()
24ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
25ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice    def process_io_test (self):
26b48dfa3f838a98bee8986b695ac154e7c6de55daCaroline Tice        """Test that process launch I/O redirection flags work properly."""
27ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        exe = os.path.join (os.getcwd(), "a.out")
28ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        self.expect("file " + exe,
29ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                    patterns = [ "Current executable set to .*a.out" ])
30ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
31ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
32ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        in_file = os.path.join (os.getcwd(), "input-file.txt")
33ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        out_file = os.path.join (os.getcwd(), "output-test.out")
34ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        err_file = os.path.join (os.getcwd(), "output-test.err")
35ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
36ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
37ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        # Make sure the output files do not exist before launching the process
38ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        try:
39ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            os.remove (out_file)
40ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        except OSError:
4125d88371101ab855df0acbbe487e92f7a4b4a3eaJohnny Chen            pass
42ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
43ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        try:
44ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            os.remove (err_file)
45ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        except OSError:
4625d88371101ab855df0acbbe487e92f7a4b4a3eaJohnny Chen            pass
47ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
48ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        launch_command = "process launch -i " + in_file + " -o " + out_file + " -e " + err_file
49ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
50ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        self.expect (launch_command,
51ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                     patterns = [ "Process .* launched: .*a.out" ])
52ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
53ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
54ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        success = True
55ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        err_msg = ""
56ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
57ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        # Check to see if the 'stdout' file was created
58ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        try:
59ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            out_f = open (out_file)
60ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        except IOError:
61ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            success = False
62ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            err_msg = err_msg + "   ERROR: stdout file was not created.\n"
63ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        else:
64ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            # Check to see if the 'stdout' file contains the right output
65ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            line = out_f.readline ();
66ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            if line != "This should go to stdout.\n":
67ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                success = False
68ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                err_msg = err_msg + "    ERROR: stdout file does not contain correct output.\n"
69ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                out_f.close();
70ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
71ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        # Try to delete the 'stdout' file
72ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        try:
73ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            os.remove (out_file)
74ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        except OSError:
7525d88371101ab855df0acbbe487e92f7a4b4a3eaJohnny Chen            pass
76ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
77ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        # Check to see if the 'stderr' file was created
78ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        try:
79ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            err_f = open (err_file)
80ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        except IOError:
81ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            success = False
82ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            err_msg = err_msg + "     ERROR:  stderr file was not created.\n"
83ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        else:
84ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            # Check to see if the 'stderr' file contains the right output
85ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            line = err_f.readline ()
86ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            if line != "This should go to stderr.\n":
87ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                success = False
88ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                err_msg = err_msg + "    ERROR: stderr file does not contain correct output.\n\
89ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice"
90ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice                err_f.close()
91ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
92ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        # Try to delete the 'stderr' file
93ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        try:
94ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            os.remove (err_file)
95ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        except OSError:
9625d88371101ab855df0acbbe487e92f7a4b4a3eaJohnny Chen            pass
97ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
98ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice        if not success:
99ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice            self.fail (err_msg)
100ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
101da9ab6f9e8ad1751e6f9aab402d2abf914948638Johnny Chen    d = {'CXX_SOURCES' : 'print_cwd.cpp'}
102a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
103a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
104a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen    def test_set_working_dir_with_dsym (self):
1057d71b1a274b3ba7bcc441326d661b2d69977d243Johnny Chen        """Test that '-w dir' sets the working dir when running the inferior."""
106da9ab6f9e8ad1751e6f9aab402d2abf914948638Johnny Chen        self.buildDsym(dictionary=self.d)
107da9ab6f9e8ad1751e6f9aab402d2abf914948638Johnny Chen        self.setTearDownCleanup(self.d)
108a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        self.my_working_dir_test()
109a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
110a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen    def test_set_working_dir_with_dwarf (self):
1117d71b1a274b3ba7bcc441326d661b2d69977d243Johnny Chen        """Test that '-w dir' sets the working dir when running the inferior."""
112da9ab6f9e8ad1751e6f9aab402d2abf914948638Johnny Chen        self.buildDwarf(dictionary=self.d)
113da9ab6f9e8ad1751e6f9aab402d2abf914948638Johnny Chen        self.setTearDownCleanup(self.d)
114a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        self.my_working_dir_test()
115a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
116a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen    # rdar://problem/9056462
117a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen    # The process launch flag '-w' for setting the current working directory not working?
118a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen    def my_working_dir_test (self):
119a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        """Test that '-w dir' sets the working dir when running the inferior."""
120a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        exe = os.path.join (os.getcwd(), "a.out")
121a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        self.runCmd("file " + exe)
122a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
123a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        mywd = 'my_working_dir'
124a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        out_file_name = "my_working_dir_test.out"
125a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
126a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        my_working_dir_path = os.path.join(os.getcwd(), mywd)
127a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        out_file_path = os.path.join(my_working_dir_path, out_file_name)
128a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
129a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        # Make sure the output files do not exist before launching the process
130a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        try:
131a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            os.remove (out_file_path)
132a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        except OSError:
133a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            pass
134a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
135a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        launch_command = "process launch -w %s -o %s" % (my_working_dir_path,
136a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen                                                         out_file_path)
137a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
138a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        self.expect(launch_command,
139a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen                    patterns = [ "Process .* launched: .*a.out" ])
140a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
141a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        success = True
142a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        err_msg = ""
143a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
144a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        # Check to see if the 'stdout' file was created
145a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        try:
146a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            out_f = open(out_file_path)
147a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        except IOError:
148a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            success = False
149a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            err_msg = err_msg + "ERROR: stdout file was not created.\n"
150a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        else:
151a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            # Check to see if the 'stdout' file contains the right output
152a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            line = out_f.readline();
1538c2fd48f058d7a5778b0e24fc5a1f316bb1de254Johnny Chen            if self.TraceOn():
1548c2fd48f058d7a5778b0e24fc5a1f316bb1de254Johnny Chen                print "line:", line
155a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            if not re.search(mywd, line):
156a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen                success = False
157a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen                err_msg = err_msg + "The current working directory was not set correctly.\n"
158a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen                out_f.close();
159a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
160a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        # Try to delete the 'stdout' file
161a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        try:
162a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            os.remove(out_file_path)
163a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            pass
164a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        except OSError:
165a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            pass
166a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
167a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen        if not success:
168a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen            self.fail(err_msg)
169a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
170a29321a5e6ff1da27d54e8a32a853f3680f00a18Johnny Chen
171ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Ticeif __name__ == '__main__':
172ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice    import atexit
173ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice    lldb.SBDebugger.Initialize()
174ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice    atexit.register(lambda: lldb.SBDebugger.Terminate())
175ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice    unittest2.main()
176ebf1ece5e348b78dd9ceca812977a20321fe4e96Caroline Tice
177