TestTargetCommand.py revision 02b6bef8009d2410ed8f3255ce545d42ac5b9b5e
1""" 2Test some target commands: create, list, select, variable. 3""" 4 5import unittest2 6import lldb 7import sys 8from lldbtest import * 9 10class targetCommandTestCase(TestBase): 11 12 mydir = os.path.join("functionalities", "target_command") 13 14 def setUp(self): 15 # Call super's setUp(). 16 TestBase.setUp(self) 17 # Find the line numbers for our breakpoints. 18 self.line_b = line_number('b.c', '// Set break point at this line.') 19 self.line_c = line_number('c.c', '// Set break point at this line.') 20 21 def test_target_command_with_dwarf(self): 22 """Test some target commands: create, list, select.""" 23 da = {'C_SOURCES': 'a.c', 'EXE': 'a.out'} 24 self.buildDwarf(dictionary=da) 25 self.addTearDownCleanup(dictionary=da) 26 27 db = {'C_SOURCES': 'b.c', 'EXE': 'b.out'} 28 self.buildDwarf(dictionary=db) 29 self.addTearDownCleanup(dictionary=db) 30 31 dc = {'C_SOURCES': 'c.c', 'EXE': 'c.out'} 32 self.buildDwarf(dictionary=dc) 33 self.addTearDownCleanup(dictionary=dc) 34 35 self.do_target_command() 36 37 # rdar://problem/9763907 38 # 'target variable' command fails if the target program has been run 39 @unittest2.expectedFailure 40 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 41 def test_target_variable_command_with_dsym(self): 42 """Test 'target variable' command before and after starting the inferior.""" 43 d = {'C_SOURCES': 'globals.c', 'EXE': 'globals'} 44 self.buildDsym(dictionary=d) 45 self.addTearDownCleanup(dictionary=d) 46 47 self.do_target_variable_command('globals') 48 49 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 50 def test_target_variable_command_with_dsym_no_fail(self): 51 """Test 'target variable' command before and after starting the inferior.""" 52 d = {'C_SOURCES': 'globals.c', 'EXE': 'globals'} 53 self.buildDsym(dictionary=d) 54 self.addTearDownCleanup(dictionary=d) 55 56 self.do_target_variable_command_no_fail('globals') 57 58 def do_target_command(self): 59 """Exercise 'target create', 'target list', 'target select' commands.""" 60 exe_a = os.path.join(os.getcwd(), "a.out") 61 exe_b = os.path.join(os.getcwd(), "b.out") 62 exe_c = os.path.join(os.getcwd(), "c.out") 63 64 self.runCmd("target list") 65 output = self.res.GetOutput() 66 if output.startswith("No targets"): 67 # We start from index 0. 68 base = 0 69 else: 70 # Find the largest index of the existing list. 71 import re 72 pattern = re.compile("target #(\d+):") 73 for line in reversed(output.split(os.linesep)): 74 match = pattern.search(line) 75 if match: 76 # We will start from (index + 1) .... 77 base = int(match.group(1), 10) + 1 78 #print "base is:", base 79 break; 80 81 self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET) 82 self.runCmd("run", RUN_SUCCEEDED) 83 84 self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET) 85 self.runCmd("breakpoint set -f %s -l %d" % ('b.c', self.line_b), 86 BREAKPOINT_CREATED) 87 self.runCmd("run", RUN_SUCCEEDED) 88 89 self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET) 90 self.runCmd("breakpoint set -f %s -l %d" % ('c.c', self.line_c), 91 BREAKPOINT_CREATED) 92 self.runCmd("run", RUN_SUCCEEDED) 93 94 self.runCmd("target list") 95 96 self.runCmd("target select %d" % base) 97 self.runCmd("thread backtrace") 98 99 self.runCmd("target select %d" % (base + 2)) 100 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT, 101 substrs = ['c.c:%d' % self.line_c, 102 'stop reason = breakpoint']) 103 104 self.runCmd("target select %d" % (base + 1)) 105 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT, 106 substrs = ['b.c:%d' % self.line_b, 107 'stop reason = breakpoint']) 108 109 self.runCmd("target list") 110 111 def do_target_variable_command(self, exe_name): 112 """Exercise 'target variable' command before and after starting the inferior.""" 113 self.runCmd("file " + exe_name, CURRENT_EXECUTABLE_SET) 114 115 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY, 116 substrs = ["my_global_char", "'X'"]) 117 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 118 substrs = ['my_global_str', '"abc"']) 119 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY, 120 substrs = ['my_static_int', '228']) 121 self.expect("target variable my_global_str_ptr", matching=False, 122 substrs = ['"abc"']) 123 self.expect("target variable *my_global_str_ptr", matching=True, 124 substrs = ['"abc"']) 125 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 126 substrs = ['a']) 127 128 self.runCmd("b main") 129 self.runCmd("run") 130 131 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 132 substrs = ['my_global_str', '"abc"']) 133 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY, 134 substrs = ['my_static_int', '228']) 135 self.expect("target variable my_global_str_ptr", matching=False, 136 substrs = ['"abc"']) 137 self.expect("target variable *my_global_str_ptr", matching=True, 138 substrs = ['"abc"']) 139 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 140 substrs = ['a']) 141 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY, 142 substrs = ["my_global_char", "'X'"]) 143 144 self.runCmd("c") 145 146 # rdar://problem/9763907 147 # 'target variable' command fails if the target program has been run 148 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 149 substrs = ['my_global_str', '"abc"']) 150 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY, 151 substrs = ['my_static_int', '228']) 152 self.expect("target variable my_global_str_ptr", matching=False, 153 substrs = ['"abc"']) 154 self.expect("target variable *my_global_str_ptr", matching=True, 155 substrs = ['"abc"']) 156 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 157 substrs = ['a']) 158 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY, 159 substrs = ["my_global_char", "'X'"]) 160 161 def do_target_variable_command_no_fail(self, exe_name): 162 """Exercise 'target variable' command before and after starting the inferior.""" 163 self.runCmd("file " + exe_name, CURRENT_EXECUTABLE_SET) 164 165 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY, 166 substrs = ["my_global_char", "'X'"]) 167 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 168 substrs = ['my_global_str', '"abc"']) 169 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY, 170 substrs = ['my_static_int', '228']) 171 self.expect("target variable my_global_str_ptr", matching=False, 172 substrs = ['"abc"']) 173 self.expect("target variable *my_global_str_ptr", matching=True, 174 substrs = ['"abc"']) 175 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 176 substrs = ['a']) 177 178 self.runCmd("b main") 179 self.runCmd("run") 180 181 # New feature: you don't need to specify the variable(s) to 'target vaiable'. 182 # It will find all the global and static variables in the current compile unit. 183 self.expect("target variable", 184 substrs = ['my_global_char', 185 'my_global_str', 186 'my_global_str_ptr', 187 'my_static_int']) 188 189 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 190 substrs = ['my_global_str', '"abc"']) 191 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY, 192 substrs = ['my_static_int', '228']) 193 self.expect("target variable my_global_str_ptr", matching=False, 194 substrs = ['"abc"']) 195 self.expect("target variable *my_global_str_ptr", matching=True, 196 substrs = ['"abc"']) 197 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY, 198 substrs = ['a']) 199 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY, 200 substrs = ["my_global_char", "'X'"]) 201 202if __name__ == '__main__': 203 import atexit 204 lldb.SBDebugger.Initialize() 205 atexit.register(lambda: lldb.SBDebugger.Terminate()) 206 unittest2.main() 207