blob: 82ab22f452e9ffea0028c3d0580bd7fdc0e836e2 [file] [log] [blame]
Caroline Ticeebf1ece2011-01-28 17:31:28 +00001"""
Johnny Chenb94250f2011-05-26 22:01:50 +00002Test lldb process launch flags.
Caroline Ticeebf1ece2011-01-28 17:31:28 +00003"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9
Johnny Chen803c9792011-02-25 21:36:35 +000010class ProcessLaunchTestCase(TestBase):
Caroline Ticeebf1ece2011-01-28 17:31:28 +000011
Johnny Chene2ac6de2011-06-27 22:10:42 +000012 mydir = os.path.join("functionalities", "process_launch")
Caroline Ticeebf1ece2011-01-28 17:31:28 +000013
Daniel Malea5ac8e152012-12-19 23:22:11 +000014 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
Caroline Ticeebf1ece2011-01-28 17:31:28 +000021 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chena3ed7d82012-04-06 00:56:05 +000022 @dsym_test
Johnny Chen803c9792011-02-25 21:36:35 +000023 def test_io_with_dsym (self):
Johnny Chen7d71b1a2011-03-04 19:47:52 +000024 """Test that process launch I/O redirection flags work properly."""
Caroline Ticeebf1ece2011-01-28 17:31:28 +000025 self.buildDsym ()
26 self.process_io_test ()
27
Johnny Chena3ed7d82012-04-06 00:56:05 +000028 @dwarf_test
Johnny Chen803c9792011-02-25 21:36:35 +000029 def test_io_with_dwarf (self):
Johnny Chen7d71b1a2011-03-04 19:47:52 +000030 """Test that process launch I/O redirection flags work properly."""
Caroline Ticeebf1ece2011-01-28 17:31:28 +000031 self.buildDwarf ()
32 self.process_io_test ()
33
Caroline Ticeebf1ece2011-01-28 17:31:28 +000034 def process_io_test (self):
Caroline Ticeb48dfa32011-01-28 18:31:34 +000035 """Test that process launch I/O redirection flags work properly."""
Caroline Ticeebf1ece2011-01-28 17:31:28 +000036 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:
Johnny Chen25d88372011-02-25 21:32:36 +000050 pass
Caroline Ticeebf1ece2011-01-28 17:31:28 +000051
52 try:
53 os.remove (err_file)
54 except OSError:
Johnny Chen25d88372011-02-25 21:32:36 +000055 pass
Caroline Ticeebf1ece2011-01-28 17:31:28 +000056
57 launch_command = "process launch -i " + in_file + " -o " + out_file + " -e " + err_file
Filipe Cabecinhasa9dd2a02012-07-12 14:09:25 +000058
Caroline Ticeebf1ece2011-01-28 17:31:28 +000059 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();
Filipe Cabecinhasa9dd2a02012-07-12 14:09:25 +000079
Caroline Ticeebf1ece2011-01-28 17:31:28 +000080 # Try to delete the 'stdout' file
81 try:
82 os.remove (out_file)
83 except OSError:
Johnny Chen25d88372011-02-25 21:32:36 +000084 pass
Caroline Ticeebf1ece2011-01-28 17:31:28 +000085
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:
Johnny Chen25d88372011-02-25 21:32:36 +0000105 pass
Caroline Ticeebf1ece2011-01-28 17:31:28 +0000106
107 if not success:
Caroline Ticeebf1ece2011-01-28 17:31:28 +0000108 self.fail (err_msg)
Caroline Ticeebf1ece2011-01-28 17:31:28 +0000109
Johnny Chenda9ab6f2011-02-25 23:24:25 +0000110 d = {'CXX_SOURCES' : 'print_cwd.cpp'}
Johnny Chena29321a2011-02-25 23:15:09 +0000111
112 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chena3ed7d82012-04-06 00:56:05 +0000113 @dsym_test
Johnny Chena29321a2011-02-25 23:15:09 +0000114 def test_set_working_dir_with_dsym (self):
Johnny Chen7d71b1a2011-03-04 19:47:52 +0000115 """Test that '-w dir' sets the working dir when running the inferior."""
Johnny Chenda9ab6f2011-02-25 23:24:25 +0000116 self.buildDsym(dictionary=self.d)
117 self.setTearDownCleanup(self.d)
Johnny Chena29321a2011-02-25 23:15:09 +0000118 self.my_working_dir_test()
119
Ed Maste4514d502013-07-23 19:19:23 +0000120 @skipIfFreeBSD # llvm.org/pr16684
Johnny Chena3ed7d82012-04-06 00:56:05 +0000121 @dwarf_test
Johnny Chena29321a2011-02-25 23:15:09 +0000122 def test_set_working_dir_with_dwarf (self):
Johnny Chen7d71b1a2011-03-04 19:47:52 +0000123 """Test that '-w dir' sets the working dir when running the inferior."""
Johnny Chenda9ab6f2011-02-25 23:24:25 +0000124 self.buildDwarf(dictionary=self.d)
125 self.setTearDownCleanup(self.d)
Johnny Chena29321a2011-02-25 23:15:09 +0000126 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"
Johnny Chen4c6a7522011-11-18 00:58:29 +0000137 err_file_name = "my_working_dir_test.err"
Johnny Chena29321a2011-02-25 23:15:09 +0000138
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)
Johnny Chen4c6a7522011-11-18 00:58:29 +0000141 err_file_path = os.path.join(my_working_dir_path, err_file_name)
Johnny Chena29321a2011-02-25 23:15:09 +0000142
143 # Make sure the output files do not exist before launching the process
144 try:
145 os.remove (out_file_path)
Johnny Chen4c6a7522011-11-18 00:58:29 +0000146 os.remove (err_file_path)
Johnny Chena29321a2011-02-25 23:15:09 +0000147 except OSError:
148 pass
149
Filipe Cabecinhasa9dd2a02012-07-12 14:09:25 +0000150 # 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,
Daniel Malea1e44fdd2013-01-08 14:49:22 +0000156 patterns = ["error:.* No such file or directory: %sz" % my_working_dir_path])
Filipe Cabecinhasa9dd2a02012-07-12 14:09:25 +0000157
158 # Really launch the process
Johnny Chend4d4e092012-02-06 21:07:21 +0000159 launch_command = "process launch -w %s -o %s -e %s" % (my_working_dir_path,
160 out_file_path,
161 err_file_path)
Johnny Chena29321a2011-02-25 23:15:09 +0000162
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();
Johnny Chen8c2fd482011-04-19 19:21:19 +0000178 if self.TraceOn():
179 print "line:", line
Johnny Chena29321a2011-02-25 23:15:09 +0000180 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();
Filipe Cabecinhasa9dd2a02012-07-12 14:09:25 +0000184
Johnny Chen4c6a7522011-11-18 00:58:29 +0000185 # Try to delete the 'stdout' and 'stderr' files
Johnny Chena29321a2011-02-25 23:15:09 +0000186 try:
187 os.remove(out_file_path)
Johnny Chen4c6a7522011-11-18 00:58:29 +0000188 os.remove(err_file_path)
Johnny Chena29321a2011-02-25 23:15:09 +0000189 pass
190 except OSError:
191 pass
192
193 if not success:
194 self.fail(err_msg)
195
196
Caroline Ticeebf1ece2011-01-28 17:31:28 +0000197if __name__ == '__main__':
198 import atexit
199 lldb.SBDebugger.Initialize()
200 atexit.register(lambda: lldb.SBDebugger.Terminate())
201 unittest2.main()
202