2 Test some target commands: create, list, select, variable.
5 from __future__ import print_function
11 from lldbsuite.test.lldbtest import *
12 import lldbsuite.test.lldbutil as lldbutil
14 class targetCommandTestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
19 # Call super's setUp().
21 # Find the line numbers for our breakpoints.
22 self.line_b = line_number('b.c', '// Set break point at this line.')
23 self.line_c = line_number('c.c', '// Set break point at this line.')
25 def test_target_command(self):
26 """Test some target commands: create, list, select."""
27 da = {'C_SOURCES': 'a.c', 'EXE': 'a.out'}
28 self.build(dictionary=da)
29 self.addTearDownCleanup(dictionary=da)
31 db = {'C_SOURCES': 'b.c', 'EXE': 'b.out'}
32 self.build(dictionary=db)
33 self.addTearDownCleanup(dictionary=db)
35 dc = {'C_SOURCES': 'c.c', 'EXE': 'c.out'}
36 self.build(dictionary=dc)
37 self.addTearDownCleanup(dictionary=dc)
39 self.do_target_command()
41 # rdar://problem/9763907
42 # 'target variable' command fails if the target program has been run
43 @expectedFailureAndroid(archs=['aarch64'])
44 def test_target_variable_command(self):
45 """Test 'target variable' command before and after starting the inferior."""
46 d = {'C_SOURCES': 'globals.c', 'EXE': 'globals'}
47 self.build(dictionary=d)
48 self.addTearDownCleanup(dictionary=d)
50 self.do_target_variable_command('globals')
52 @expectedFailureAndroid(archs=['aarch64'])
53 def test_target_variable_command_no_fail(self):
54 """Test 'target variable' command before and after starting the inferior."""
55 d = {'C_SOURCES': 'globals.c', 'EXE': 'globals'}
56 self.build(dictionary=d)
57 self.addTearDownCleanup(dictionary=d)
59 self.do_target_variable_command_no_fail('globals')
61 def do_target_command(self):
62 """Exercise 'target create', 'target list', 'target select' commands."""
63 exe_a = os.path.join(os.getcwd(), "a.out")
64 exe_b = os.path.join(os.getcwd(), "b.out")
65 exe_c = os.path.join(os.getcwd(), "c.out")
67 self.runCmd("target list")
68 output = self.res.GetOutput()
69 if output.startswith("No targets"):
70 # We start from index 0.
73 # Find the largest index of the existing list.
75 pattern = re.compile("target #(\d+):")
76 for line in reversed(output.split(os.linesep)):
77 match = pattern.search(line)
79 # We will start from (index + 1) ....
80 base = int(match.group(1), 10) + 1
81 #print("base is:", base)
84 self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET)
85 self.runCmd("run", RUN_SUCCEEDED)
87 self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET)
88 lldbutil.run_break_set_by_file_and_line (self, 'b.c', self.line_b, num_expected_locations=1, loc_exact=True)
89 self.runCmd("run", RUN_SUCCEEDED)
91 self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET)
92 lldbutil.run_break_set_by_file_and_line (self, 'c.c', self.line_c, num_expected_locations=1, loc_exact=True)
93 self.runCmd("run", RUN_SUCCEEDED)
95 self.runCmd("target list")
97 self.runCmd("target select %d" % base)
98 self.runCmd("thread backtrace")
100 self.runCmd("target select %d" % (base + 2))
101 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
102 substrs = ['c.c:%d' % self.line_c,
103 'stop reason = breakpoint'])
105 self.runCmd("target select %d" % (base + 1))
106 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
107 substrs = ['b.c:%d' % self.line_b,
108 'stop reason = breakpoint'])
110 self.runCmd("target list")
112 def do_target_variable_command(self, exe_name):
113 """Exercise 'target variable' command before and after starting the inferior."""
114 self.runCmd("file " + exe_name, CURRENT_EXECUTABLE_SET)
116 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
117 substrs = ["my_global_char", "'X'"])
118 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
119 substrs = ['my_global_str', '"abc"'])
120 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
121 substrs = ['my_static_int', '228'])
122 self.expect("target variable my_global_str_ptr", matching=False,
124 self.expect("target variable *my_global_str_ptr", matching=True,
126 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
129 self.runCmd("b main")
132 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
133 substrs = ['my_global_str', '"abc"'])
134 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
135 substrs = ['my_static_int', '228'])
136 self.expect("target variable my_global_str_ptr", matching=False,
138 self.expect("target variable *my_global_str_ptr", matching=True,
140 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
142 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
143 substrs = ["my_global_char", "'X'"])
147 # rdar://problem/9763907
148 # 'target variable' command fails if the target program has been run
149 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
150 substrs = ['my_global_str', '"abc"'])
151 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
152 substrs = ['my_static_int', '228'])
153 self.expect("target variable my_global_str_ptr", matching=False,
155 self.expect("target variable *my_global_str_ptr", matching=True,
157 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
159 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
160 substrs = ["my_global_char", "'X'"])
162 def do_target_variable_command_no_fail(self, exe_name):
163 """Exercise 'target variable' command before and after starting the inferior."""
164 self.runCmd("file " + exe_name, CURRENT_EXECUTABLE_SET)
166 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
167 substrs = ["my_global_char", "'X'"])
168 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
169 substrs = ['my_global_str', '"abc"'])
170 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
171 substrs = ['my_static_int', '228'])
172 self.expect("target variable my_global_str_ptr", matching=False,
174 self.expect("target variable *my_global_str_ptr", matching=True,
176 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
179 self.runCmd("b main")
182 # New feature: you don't need to specify the variable(s) to 'target vaiable'.
183 # It will find all the global and static variables in the current compile unit.
184 self.expect("target variable",
185 substrs = ['my_global_char',
190 self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
191 substrs = ['my_global_str', '"abc"'])
192 self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
193 substrs = ['my_static_int', '228'])
194 self.expect("target variable my_global_str_ptr", matching=False,
196 self.expect("target variable *my_global_str_ptr", matching=True,
198 self.expect("target variable *my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
200 self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
201 substrs = ["my_global_char", "'X'"])