• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Test lldb process launch flags.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9
10class ProcessLaunchTestCase(TestBase):
11
12    mydir = os.path.join("functionalities", "process_launch")
13
14    def setUp(self):
15        # Call super's setUp().
16        TestBase.setUp(self)
17        # disable "There is a running process, kill it and restart?" prompt
18        self.runCmd("settings set auto-confirm true")
19        self.addTearDownHook(lambda: self.runCmd("settings clear auto-confirm"))
20
21    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
22    @dsym_test
23    def test_io_with_dsym (self):
24        """Test that process launch I/O redirection flags work properly."""
25        self.buildDsym ()
26        self.process_io_test ()
27
28    @dwarf_test
29    def test_io_with_dwarf (self):
30        """Test that process launch I/O redirection flags work properly."""
31        self.buildDwarf ()
32        self.process_io_test ()
33
34    def process_io_test (self):
35        """Test that process launch I/O redirection flags work properly."""
36        exe = os.path.join (os.getcwd(), "a.out")
37        self.expect("file " + exe,
38                    patterns = [ "Current executable set to .*a.out" ])
39
40
41        in_file = os.path.join (os.getcwd(), "input-file.txt")
42        out_file = os.path.join (os.getcwd(), "output-test.out")
43        err_file = os.path.join (os.getcwd(), "output-test.err")
44
45
46        # Make sure the output files do not exist before launching the process
47        try:
48            os.remove (out_file)
49        except OSError:
50            pass
51
52        try:
53            os.remove (err_file)
54        except OSError:
55            pass
56
57        launch_command = "process launch -i " + in_file + " -o " + out_file + " -e " + err_file
58
59        self.expect (launch_command,
60                     patterns = [ "Process .* launched: .*a.out" ])
61
62
63        success = True
64        err_msg = ""
65
66        # Check to see if the 'stdout' file was created
67        try:
68            out_f = open (out_file)
69        except IOError:
70            success = False
71            err_msg = err_msg + "   ERROR: stdout file was not created.\n"
72        else:
73            # Check to see if the 'stdout' file contains the right output
74            line = out_f.readline ();
75            if line != "This should go to stdout.\n":
76                success = False
77                err_msg = err_msg + "    ERROR: stdout file does not contain correct output.\n"
78                out_f.close();
79
80        # Try to delete the 'stdout' file
81        try:
82            os.remove (out_file)
83        except OSError:
84            pass
85
86        # Check to see if the 'stderr' file was created
87        try:
88            err_f = open (err_file)
89        except IOError:
90            success = False
91            err_msg = err_msg + "     ERROR:  stderr file was not created.\n"
92        else:
93            # Check to see if the 'stderr' file contains the right output
94            line = err_f.readline ()
95            if line != "This should go to stderr.\n":
96                success = False
97                err_msg = err_msg + "    ERROR: stderr file does not contain correct output.\n\
98"
99                err_f.close()
100
101        # Try to delete the 'stderr' file
102        try:
103            os.remove (err_file)
104        except OSError:
105            pass
106
107        if not success:
108            self.fail (err_msg)
109
110    d = {'CXX_SOURCES' : 'print_cwd.cpp'}
111
112    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
113    @dsym_test
114    def test_set_working_dir_with_dsym (self):
115        """Test that '-w dir' sets the working dir when running the inferior."""
116        self.buildDsym(dictionary=self.d)
117        self.setTearDownCleanup(self.d)
118        self.my_working_dir_test()
119
120    @skipIfFreeBSD # llvm.org/pr16684
121    @dwarf_test
122    def test_set_working_dir_with_dwarf (self):
123        """Test that '-w dir' sets the working dir when running the inferior."""
124        self.buildDwarf(dictionary=self.d)
125        self.setTearDownCleanup(self.d)
126        self.my_working_dir_test()
127
128    # rdar://problem/9056462
129    # The process launch flag '-w' for setting the current working directory not working?
130    def my_working_dir_test (self):
131        """Test that '-w dir' sets the working dir when running the inferior."""
132        exe = os.path.join (os.getcwd(), "a.out")
133        self.runCmd("file " + exe)
134
135        mywd = 'my_working_dir'
136        out_file_name = "my_working_dir_test.out"
137        err_file_name = "my_working_dir_test.err"
138
139        my_working_dir_path = os.path.join(os.getcwd(), mywd)
140        out_file_path = os.path.join(my_working_dir_path, out_file_name)
141        err_file_path = os.path.join(my_working_dir_path, err_file_name)
142
143        # Make sure the output files do not exist before launching the process
144        try:
145            os.remove (out_file_path)
146            os.remove (err_file_path)
147        except OSError:
148            pass
149
150        # Check that we get an error when we have a nonexisting path
151        launch_command = "process launch -w %s -o %s -e %s" % (my_working_dir_path + 'z',
152                                                               out_file_path,
153                                                               err_file_path)
154
155        self.expect(launch_command, error=True,
156                patterns = ["error:.* No such file or directory: %sz" % my_working_dir_path])
157
158        # Really launch the process
159        launch_command = "process launch -w %s -o %s -e %s" % (my_working_dir_path,
160                                                               out_file_path,
161                                                               err_file_path)
162
163        self.expect(launch_command,
164                    patterns = [ "Process .* launched: .*a.out" ])
165
166        success = True
167        err_msg = ""
168
169        # Check to see if the 'stdout' file was created
170        try:
171            out_f = open(out_file_path)
172        except IOError:
173            success = False
174            err_msg = err_msg + "ERROR: stdout file was not created.\n"
175        else:
176            # Check to see if the 'stdout' file contains the right output
177            line = out_f.readline();
178            if self.TraceOn():
179                print "line:", line
180            if not re.search(mywd, line):
181                success = False
182                err_msg = err_msg + "The current working directory was not set correctly.\n"
183                out_f.close();
184
185        # Try to delete the 'stdout' and 'stderr' files
186        try:
187            os.remove(out_file_path)
188            os.remove(err_file_path)
189            pass
190        except OSError:
191            pass
192
193        if not success:
194            self.fail(err_msg)
195
196
197if __name__ == '__main__':
198    import atexit
199    lldb.SBDebugger.Initialize()
200    atexit.register(lambda: lldb.SBDebugger.Terminate())
201    unittest2.main()
202
203