Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 1 | """ |
Johnny Chen | b94250f | 2011-05-26 22:01:50 +0000 | [diff] [blame] | 2 | Test lldb process launch flags. |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import unittest2 |
| 7 | import lldb |
| 8 | from lldbtest import * |
| 9 | |
Johnny Chen | 803c979 | 2011-02-25 21:36:35 +0000 | [diff] [blame] | 10 | class ProcessLaunchTestCase(TestBase): |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 11 | |
Johnny Chen | e2ac6de | 2011-06-27 22:10:42 +0000 | [diff] [blame] | 12 | mydir = os.path.join("functionalities", "process_launch") |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 13 | |
Daniel Malea | 5ac8e15 | 2012-12-19 23:22:11 +0000 | [diff] [blame] | 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 | |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 21 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Johnny Chen | a3ed7d8 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 22 | @dsym_test |
Johnny Chen | 803c979 | 2011-02-25 21:36:35 +0000 | [diff] [blame] | 23 | def test_io_with_dsym (self): |
Johnny Chen | 7d71b1a | 2011-03-04 19:47:52 +0000 | [diff] [blame] | 24 | """Test that process launch I/O redirection flags work properly.""" |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 25 | self.buildDsym () |
| 26 | self.process_io_test () |
| 27 | |
Johnny Chen | a3ed7d8 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 28 | @dwarf_test |
Johnny Chen | 803c979 | 2011-02-25 21:36:35 +0000 | [diff] [blame] | 29 | def test_io_with_dwarf (self): |
Johnny Chen | 7d71b1a | 2011-03-04 19:47:52 +0000 | [diff] [blame] | 30 | """Test that process launch I/O redirection flags work properly.""" |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 31 | self.buildDwarf () |
| 32 | self.process_io_test () |
| 33 | |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 34 | def process_io_test (self): |
Caroline Tice | b48dfa3 | 2011-01-28 18:31:34 +0000 | [diff] [blame] | 35 | """Test that process launch I/O redirection flags work properly.""" |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 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: |
Johnny Chen | 25d8837 | 2011-02-25 21:32:36 +0000 | [diff] [blame] | 50 | pass |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 51 | |
| 52 | try: |
| 53 | os.remove (err_file) |
| 54 | except OSError: |
Johnny Chen | 25d8837 | 2011-02-25 21:32:36 +0000 | [diff] [blame] | 55 | pass |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 56 | |
| 57 | launch_command = "process launch -i " + in_file + " -o " + out_file + " -e " + err_file |
Filipe Cabecinhas | a9dd2a0 | 2012-07-12 14:09:25 +0000 | [diff] [blame] | 58 | |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 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(); |
Filipe Cabecinhas | a9dd2a0 | 2012-07-12 14:09:25 +0000 | [diff] [blame] | 79 | |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 80 | # Try to delete the 'stdout' file |
| 81 | try: |
| 82 | os.remove (out_file) |
| 83 | except OSError: |
Johnny Chen | 25d8837 | 2011-02-25 21:32:36 +0000 | [diff] [blame] | 84 | pass |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 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: |
Johnny Chen | 25d8837 | 2011-02-25 21:32:36 +0000 | [diff] [blame] | 105 | pass |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 106 | |
| 107 | if not success: |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 108 | self.fail (err_msg) |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 109 | |
Johnny Chen | da9ab6f | 2011-02-25 23:24:25 +0000 | [diff] [blame] | 110 | d = {'CXX_SOURCES' : 'print_cwd.cpp'} |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 111 | |
| 112 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Johnny Chen | a3ed7d8 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 113 | @dsym_test |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 114 | def test_set_working_dir_with_dsym (self): |
Johnny Chen | 7d71b1a | 2011-03-04 19:47:52 +0000 | [diff] [blame] | 115 | """Test that '-w dir' sets the working dir when running the inferior.""" |
Johnny Chen | da9ab6f | 2011-02-25 23:24:25 +0000 | [diff] [blame] | 116 | self.buildDsym(dictionary=self.d) |
| 117 | self.setTearDownCleanup(self.d) |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 118 | self.my_working_dir_test() |
| 119 | |
Ed Maste | 4514d50 | 2013-07-23 19:19:23 +0000 | [diff] [blame^] | 120 | @skipIfFreeBSD # llvm.org/pr16684 |
Johnny Chen | a3ed7d8 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 121 | @dwarf_test |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 122 | def test_set_working_dir_with_dwarf (self): |
Johnny Chen | 7d71b1a | 2011-03-04 19:47:52 +0000 | [diff] [blame] | 123 | """Test that '-w dir' sets the working dir when running the inferior.""" |
Johnny Chen | da9ab6f | 2011-02-25 23:24:25 +0000 | [diff] [blame] | 124 | self.buildDwarf(dictionary=self.d) |
| 125 | self.setTearDownCleanup(self.d) |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 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" |
Johnny Chen | 4c6a752 | 2011-11-18 00:58:29 +0000 | [diff] [blame] | 137 | err_file_name = "my_working_dir_test.err" |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 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) |
Johnny Chen | 4c6a752 | 2011-11-18 00:58:29 +0000 | [diff] [blame] | 141 | err_file_path = os.path.join(my_working_dir_path, err_file_name) |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 142 | |
| 143 | # Make sure the output files do not exist before launching the process |
| 144 | try: |
| 145 | os.remove (out_file_path) |
Johnny Chen | 4c6a752 | 2011-11-18 00:58:29 +0000 | [diff] [blame] | 146 | os.remove (err_file_path) |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 147 | except OSError: |
| 148 | pass |
| 149 | |
Filipe Cabecinhas | a9dd2a0 | 2012-07-12 14:09:25 +0000 | [diff] [blame] | 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, |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 156 | patterns = ["error:.* No such file or directory: %sz" % my_working_dir_path]) |
Filipe Cabecinhas | a9dd2a0 | 2012-07-12 14:09:25 +0000 | [diff] [blame] | 157 | |
| 158 | # Really launch the process |
Johnny Chen | d4d4e09 | 2012-02-06 21:07:21 +0000 | [diff] [blame] | 159 | launch_command = "process launch -w %s -o %s -e %s" % (my_working_dir_path, |
| 160 | out_file_path, |
| 161 | err_file_path) |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 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(); |
Johnny Chen | 8c2fd48 | 2011-04-19 19:21:19 +0000 | [diff] [blame] | 178 | if self.TraceOn(): |
| 179 | print "line:", line |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 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(); |
Filipe Cabecinhas | a9dd2a0 | 2012-07-12 14:09:25 +0000 | [diff] [blame] | 184 | |
Johnny Chen | 4c6a752 | 2011-11-18 00:58:29 +0000 | [diff] [blame] | 185 | # Try to delete the 'stdout' and 'stderr' files |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 186 | try: |
| 187 | os.remove(out_file_path) |
Johnny Chen | 4c6a752 | 2011-11-18 00:58:29 +0000 | [diff] [blame] | 188 | os.remove(err_file_path) |
Johnny Chen | a29321a | 2011-02-25 23:15:09 +0000 | [diff] [blame] | 189 | pass |
| 190 | except OSError: |
| 191 | pass |
| 192 | |
| 193 | if not success: |
| 194 | self.fail(err_msg) |
| 195 | |
| 196 | |
Caroline Tice | ebf1ece | 2011-01-28 17:31:28 +0000 | [diff] [blame] | 197 | if __name__ == '__main__': |
| 198 | import atexit |
| 199 | lldb.SBDebugger.Initialize() |
| 200 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 201 | unittest2.main() |
| 202 | |