[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / lldb / packages / Python / lldbsuite / test / commands / target / basic / TestTargetCommand.py
blob6152a6a9673c0659c36b5f48ebfbdc754616206a
1 """
2 Test some target commands: create, list, select, variable.
3 """
5 import os
6 import stat
7 import tempfile
9 import lldb
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
15 class targetCommandTestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
19 def setUp(self):
20 # Call super's setUp().
21 TestBase.setUp(self)
22 # Find the line numbers for our breakpoints.
23 self.line_b = line_number('b.c', '// Set break point at this line.')
24 self.line_c = line_number('c.c', '// Set break point at this line.')
26 def buildB(self):
27 db = {'C_SOURCES': 'b.c', 'EXE': self.getBuildArtifact('b.out')}
28 self.build(dictionary=db)
29 self.addTearDownCleanup(dictionary=db)
31 def buildAll(self):
32 da = {'C_SOURCES': 'a.c', 'EXE': self.getBuildArtifact('a.out')}
33 self.build(dictionary=da)
34 self.addTearDownCleanup(dictionary=da)
36 self.buildB()
38 dc = {'C_SOURCES': 'c.c', 'EXE': self.getBuildArtifact('c.out')}
39 self.build(dictionary=dc)
40 self.addTearDownCleanup(dictionary=dc)
42 def test_target_command(self):
43 """Test some target commands: create, list, select."""
44 self.buildAll()
45 self.do_target_command()
47 def test_target_variable_command(self):
48 """Test 'target variable' command before and after starting the inferior."""
49 d = {'C_SOURCES': 'globals.c', 'EXE': self.getBuildArtifact('globals')}
50 self.build(dictionary=d)
51 self.addTearDownCleanup(dictionary=d)
53 self.do_target_variable_command('globals')
55 def test_target_variable_command_no_fail(self):
56 """Test 'target variable' command before and after starting the inferior."""
57 d = {'C_SOURCES': 'globals.c', 'EXE': self.getBuildArtifact('globals')}
58 self.build(dictionary=d)
59 self.addTearDownCleanup(dictionary=d)
61 self.do_target_variable_command_no_fail('globals')
63 def do_target_command(self):
64 """Exercise 'target create', 'target list', 'target select' commands."""
65 exe_a = self.getBuildArtifact("a.out")
66 exe_b = self.getBuildArtifact("b.out")
67 exe_c = self.getBuildArtifact("c.out")
69 self.runCmd("target list")
70 output = self.res.GetOutput()
71 if output.startswith("No targets"):
72 # We start from index 0.
73 base = 0
74 else:
75 # Find the largest index of the existing list.
76 import re
77 pattern = re.compile("target #(\d+):")
78 for line in reversed(output.split(os.linesep)):
79 match = pattern.search(line)
80 if match:
81 # We will start from (index + 1) ....
82 base = int(match.group(1), 10) + 1
83 #print("base is:", base)
84 break
86 self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET)
87 self.runCmd("run", RUN_SUCCEEDED)
89 self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET)
90 lldbutil.run_break_set_by_file_and_line(
91 self, 'b.c', self.line_b, num_expected_locations=1, loc_exact=True)
92 self.runCmd("run", RUN_SUCCEEDED)
94 self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET)
95 lldbutil.run_break_set_by_file_and_line(
96 self, 'c.c', self.line_c, num_expected_locations=1, loc_exact=True)
97 self.runCmd("run", RUN_SUCCEEDED)
99 self.runCmd("target list")
101 self.runCmd("target select %d" % base)
102 self.runCmd("thread backtrace")
104 self.runCmd("target select %d" % (base + 2))
105 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
106 substrs=['c.c:%d' % self.line_c,
107 'stop reason = breakpoint'])
109 self.runCmd("target select %d" % (base + 1))
110 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
111 substrs=['b.c:%d' % self.line_b,
112 'stop reason = breakpoint'])
114 self.runCmd("target list")
116 def do_target_variable_command(self, exe_name):
117 """Exercise 'target variable' command before and after starting the inferior."""
118 self.runCmd("file " + self.getBuildArtifact(exe_name),
119 CURRENT_EXECUTABLE_SET)
121 self.expect(
122 "target variable my_global_char",
123 VARIABLES_DISPLAYED_CORRECTLY,
124 substrs=[
125 "my_global_char",
126 "'X'"])
127 self.expect(
128 "target variable my_global_str",
129 VARIABLES_DISPLAYED_CORRECTLY,
130 substrs=[
131 'my_global_str',
132 '"abc"'])
133 self.expect(
134 "target variable my_static_int",
135 VARIABLES_DISPLAYED_CORRECTLY,
136 substrs=[
137 'my_static_int',
138 '228'])
139 self.expect("target variable my_global_str_ptr", matching=False,
140 substrs=['"abc"'])
141 self.expect("target variable *my_global_str_ptr", matching=True,
142 substrs=['"abc"'])
143 self.expect(
144 "target variable *my_global_str",
145 VARIABLES_DISPLAYED_CORRECTLY,
146 substrs=['a'])
148 self.runCmd("b main")
149 self.runCmd("run")
151 self.expect(
152 "target variable my_global_str",
153 VARIABLES_DISPLAYED_CORRECTLY,
154 substrs=[
155 'my_global_str',
156 '"abc"'])
157 self.expect(
158 "target variable my_static_int",
159 VARIABLES_DISPLAYED_CORRECTLY,
160 substrs=[
161 'my_static_int',
162 '228'])
163 self.expect("target variable my_global_str_ptr", matching=False,
164 substrs=['"abc"'])
165 self.expect("target variable *my_global_str_ptr", matching=True,
166 substrs=['"abc"'])
167 self.expect(
168 "target variable *my_global_str",
169 VARIABLES_DISPLAYED_CORRECTLY,
170 substrs=['a'])
171 self.expect(
172 "target variable my_global_char",
173 VARIABLES_DISPLAYED_CORRECTLY,
174 substrs=[
175 "my_global_char",
176 "'X'"])
178 self.runCmd("c")
180 # rdar://problem/9763907
181 # 'target variable' command fails if the target program has been run
182 self.expect(
183 "target variable my_global_str",
184 VARIABLES_DISPLAYED_CORRECTLY,
185 substrs=[
186 'my_global_str',
187 '"abc"'])
188 self.expect(
189 "target variable my_static_int",
190 VARIABLES_DISPLAYED_CORRECTLY,
191 substrs=[
192 'my_static_int',
193 '228'])
194 self.expect("target variable my_global_str_ptr", matching=False,
195 substrs=['"abc"'])
196 self.expect("target variable *my_global_str_ptr", matching=True,
197 substrs=['"abc"'])
198 self.expect(
199 "target variable *my_global_str",
200 VARIABLES_DISPLAYED_CORRECTLY,
201 substrs=['a'])
202 self.expect(
203 "target variable my_global_char",
204 VARIABLES_DISPLAYED_CORRECTLY,
205 substrs=[
206 "my_global_char",
207 "'X'"])
209 def do_target_variable_command_no_fail(self, exe_name):
210 """Exercise 'target variable' command before and after starting the inferior."""
211 self.runCmd("file " + self.getBuildArtifact(exe_name),
212 CURRENT_EXECUTABLE_SET)
214 self.expect(
215 "target variable my_global_char",
216 VARIABLES_DISPLAYED_CORRECTLY,
217 substrs=[
218 "my_global_char",
219 "'X'"])
220 self.expect(
221 "target variable my_global_str",
222 VARIABLES_DISPLAYED_CORRECTLY,
223 substrs=[
224 'my_global_str',
225 '"abc"'])
226 self.expect(
227 "target variable my_static_int",
228 VARIABLES_DISPLAYED_CORRECTLY,
229 substrs=[
230 'my_static_int',
231 '228'])
232 self.expect("target variable my_global_str_ptr", matching=False,
233 substrs=['"abc"'])
234 self.expect("target variable *my_global_str_ptr", matching=True,
235 substrs=['"abc"'])
236 self.expect(
237 "target variable *my_global_str",
238 VARIABLES_DISPLAYED_CORRECTLY,
239 substrs=['a'])
241 self.runCmd("b main")
242 self.runCmd("run")
244 # New feature: you don't need to specify the variable(s) to 'target vaiable'.
245 # It will find all the global and static variables in the current
246 # compile unit.
247 self.expect("target variable",
248 substrs=['my_global_char',
249 'my_global_str',
250 'my_global_str_ptr',
251 'my_static_int'])
253 self.expect(
254 "target variable my_global_str",
255 VARIABLES_DISPLAYED_CORRECTLY,
256 substrs=[
257 'my_global_str',
258 '"abc"'])
259 self.expect(
260 "target variable my_static_int",
261 VARIABLES_DISPLAYED_CORRECTLY,
262 substrs=[
263 'my_static_int',
264 '228'])
265 self.expect("target variable my_global_str_ptr", matching=False,
266 substrs=['"abc"'])
267 self.expect("target variable *my_global_str_ptr", matching=True,
268 substrs=['"abc"'])
269 self.expect(
270 "target variable *my_global_str",
271 VARIABLES_DISPLAYED_CORRECTLY,
272 substrs=['a'])
273 self.expect(
274 "target variable my_global_char",
275 VARIABLES_DISPLAYED_CORRECTLY,
276 substrs=[
277 "my_global_char",
278 "'X'"])
280 @no_debug_info_test
281 def test_target_stop_hook_disable_enable(self):
282 self.buildB()
283 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
285 self.expect("target stop-hook disable 1", error=True, substrs=['unknown stop hook id: "1"'])
286 self.expect("target stop-hook disable blub", error=True, substrs=['invalid stop hook id: "blub"'])
287 self.expect("target stop-hook enable 1", error=True, substrs=['unknown stop hook id: "1"'])
288 self.expect("target stop-hook enable blub", error=True, substrs=['invalid stop hook id: "blub"'])
290 @no_debug_info_test
291 def test_target_stop_hook_delete(self):
292 self.buildB()
293 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
295 self.expect("target stop-hook delete 1", error=True, substrs=['unknown stop hook id: "1"'])
296 self.expect("target stop-hook delete blub", error=True, substrs=['invalid stop hook id: "blub"'])
298 @no_debug_info_test
299 def test_target_list_args(self):
300 self.expect("target list blub", error=True,
301 substrs=["the 'target list' command takes no arguments"])
303 @no_debug_info_test
304 def test_target_select_no_index(self):
305 self.expect("target select", error=True,
306 substrs=["'target select' takes a single argument: a target index"])
308 @no_debug_info_test
309 def test_target_select_invalid_index(self):
310 self.runCmd("target delete --all")
311 self.expect("target select 0", error=True,
312 substrs=["index 0 is out of range since there are no active targets"])
313 self.buildB()
314 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
315 self.expect("target select 1", error=True,
316 substrs=["index 1 is out of range, valid target indexes are 0 - 0"])
319 @no_debug_info_test
320 def test_target_create_multiple_args(self):
321 self.expect("target create a b", error=True,
322 substrs=["'target create' takes exactly one executable path"])
324 @no_debug_info_test
325 def test_target_create_nonexistent_core_file(self):
326 self.expect("target create -c doesntexist", error=True,
327 substrs=["core file 'doesntexist' doesn't exist"])
329 # Write only files don't seem to be supported on Windows.
330 @skipIfWindows
331 @no_debug_info_test
332 def test_target_create_unreadable_core_file(self):
333 tf = tempfile.NamedTemporaryFile()
334 os.chmod(tf.name, stat.S_IWRITE)
335 self.expect("target create -c '" + tf.name + "'", error=True,
336 substrs=["core file '", "' is not readable"])
338 @no_debug_info_test
339 def test_target_create_nonexistent_sym_file(self):
340 self.expect("target create -s doesntexist doesntexisteither", error=True,
341 substrs=["invalid symbol file path 'doesntexist'"])
343 @skipIfWindows
344 @no_debug_info_test
345 def test_target_create_invalid_core_file(self):
346 invalid_core_path = os.path.join(self.getSourceDir(), "invalid_core_file")
347 self.expect("target create -c '" + invalid_core_path + "'", error=True,
348 substrs=["Unable to find process plug-in for core file '"])
351 # Write only files don't seem to be supported on Windows.
352 @skipIfWindows
353 @no_debug_info_test
354 def test_target_create_unreadable_sym_file(self):
355 tf = tempfile.NamedTemporaryFile()
356 os.chmod(tf.name, stat.S_IWRITE)
357 self.expect("target create -s '" + tf.name + "' no_exe", error=True,
358 substrs=["symbol file '", "' is not readable"])
360 @no_debug_info_test
361 def test_target_delete_all(self):
362 self.buildAll()
363 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
364 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
365 self.expect("target delete --all")
366 self.expect("target list", substrs=["No targets."])
368 @no_debug_info_test
369 def test_target_delete_by_index(self):
370 self.buildAll()
371 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
372 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
373 self.runCmd("file " + self.getBuildArtifact("c.out"), CURRENT_EXECUTABLE_SET)
374 self.expect("target delete 3", error=True,
375 substrs=["target index 3 is out of range, valid target indexes are 0 - 2"])
377 self.runCmd("target delete 1")
378 self.expect("target list", matching=False, substrs=["b.out"])
379 self.runCmd("target delete 1")
380 self.expect("target list", matching=False, substrs=["c.out"])
382 self.expect("target delete 1", error=True,
383 substrs=["target index 1 is out of range, the only valid index is 0"])
385 self.runCmd("target delete 0")
386 self.expect("target list", matching=False, substrs=["a.out"])
388 self.expect("target delete 0", error=True, substrs=["no targets to delete"])
389 self.expect("target delete 1", error=True, substrs=["no targets to delete"])
391 @no_debug_info_test
392 def test_target_delete_by_index_multiple(self):
393 self.buildAll()
394 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
395 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
396 self.runCmd("file " + self.getBuildArtifact("c.out"), CURRENT_EXECUTABLE_SET)
398 self.expect("target delete 0 1 2 3", error=True,
399 substrs=["target index 3 is out of range, valid target indexes are 0 - 2"])
400 self.expect("target list", substrs=["a.out", "b.out", "c.out"])
402 self.runCmd("target delete 0 1 2")
403 self.expect("target list", matching=False, substrs=["a.out", "c.out"])
405 @no_debug_info_test
406 def test_target_delete_selected(self):
407 self.buildAll()
408 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
409 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
410 self.runCmd("file " + self.getBuildArtifact("c.out"), CURRENT_EXECUTABLE_SET)
411 self.runCmd("target select 1")
412 self.runCmd("target delete")
413 self.expect("target list", matching=False, substrs=["b.out"])
414 self.runCmd("target delete")
415 self.runCmd("target delete")
416 self.expect("target list", substrs=["No targets."])
417 self.expect("target delete", error=True, substrs=["no target is currently selected"])
419 @no_debug_info_test
420 def test_target_modules_search_paths_clear(self):
421 self.buildB()
422 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
423 self.runCmd("target modules search-paths add foo bar")
424 self.runCmd("target modules search-paths add foz baz")
425 self.runCmd("target modules search-paths clear")
426 self.expect("target list", matching=False, substrs=["bar", "baz"])
428 @no_debug_info_test
429 def test_target_modules_search_paths_query(self):
430 self.buildB()
431 self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
432 self.runCmd("target modules search-paths add foo bar")
433 self.expect("target modules search-paths query foo", substrs=["bar"])
434 # Query something that doesn't exist.
435 self.expect("target modules search-paths query faz", substrs=["faz"])
437 # Invalid arguments.
438 self.expect("target modules search-paths query faz baz", error=True,
439 substrs=["query requires one argument"])