2 Test some target commands: create, list, select, variable.
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__
)
20 # Call super's setUp().
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.')
27 db
= {'C_SOURCES': 'b.c', 'EXE': self
.getBuildArtifact('b.out')}
28 self
.build(dictionary
=db
)
29 self
.addTearDownCleanup(dictionary
=db
)
32 da
= {'C_SOURCES': 'a.c', 'EXE': self
.getBuildArtifact('a.out')}
33 self
.build(dictionary
=da
)
34 self
.addTearDownCleanup(dictionary
=da
)
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."""
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.
75 # Find the largest index of the existing list.
77 pattern
= re
.compile("target #(\d+):")
78 for line
in reversed(output
.split(os
.linesep
)):
79 match
= pattern
.search(line
)
81 # We will start from (index + 1) ....
82 base
= int(match
.group(1), 10) + 1
83 #print("base is:", base)
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
)
122 "target variable my_global_char",
123 VARIABLES_DISPLAYED_CORRECTLY
,
128 "target variable my_global_str",
129 VARIABLES_DISPLAYED_CORRECTLY
,
134 "target variable my_static_int",
135 VARIABLES_DISPLAYED_CORRECTLY
,
139 self
.expect("target variable my_global_str_ptr", matching
=False,
141 self
.expect("target variable *my_global_str_ptr", matching
=True,
144 "target variable *my_global_str",
145 VARIABLES_DISPLAYED_CORRECTLY
,
148 self
.runCmd("b main")
152 "target variable my_global_str",
153 VARIABLES_DISPLAYED_CORRECTLY
,
158 "target variable my_static_int",
159 VARIABLES_DISPLAYED_CORRECTLY
,
163 self
.expect("target variable my_global_str_ptr", matching
=False,
165 self
.expect("target variable *my_global_str_ptr", matching
=True,
168 "target variable *my_global_str",
169 VARIABLES_DISPLAYED_CORRECTLY
,
172 "target variable my_global_char",
173 VARIABLES_DISPLAYED_CORRECTLY
,
180 # rdar://problem/9763907
181 # 'target variable' command fails if the target program has been run
183 "target variable my_global_str",
184 VARIABLES_DISPLAYED_CORRECTLY
,
189 "target variable my_static_int",
190 VARIABLES_DISPLAYED_CORRECTLY
,
194 self
.expect("target variable my_global_str_ptr", matching
=False,
196 self
.expect("target variable *my_global_str_ptr", matching
=True,
199 "target variable *my_global_str",
200 VARIABLES_DISPLAYED_CORRECTLY
,
203 "target variable my_global_char",
204 VARIABLES_DISPLAYED_CORRECTLY
,
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
)
215 "target variable my_global_char",
216 VARIABLES_DISPLAYED_CORRECTLY
,
221 "target variable my_global_str",
222 VARIABLES_DISPLAYED_CORRECTLY
,
227 "target variable my_static_int",
228 VARIABLES_DISPLAYED_CORRECTLY
,
232 self
.expect("target variable my_global_str_ptr", matching
=False,
234 self
.expect("target variable *my_global_str_ptr", matching
=True,
237 "target variable *my_global_str",
238 VARIABLES_DISPLAYED_CORRECTLY
,
241 self
.runCmd("b main")
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
247 self
.expect("target variable",
248 substrs
=['my_global_char',
254 "target variable my_global_str",
255 VARIABLES_DISPLAYED_CORRECTLY
,
260 "target variable my_static_int",
261 VARIABLES_DISPLAYED_CORRECTLY
,
265 self
.expect("target variable my_global_str_ptr", matching
=False,
267 self
.expect("target variable *my_global_str_ptr", matching
=True,
270 "target variable *my_global_str",
271 VARIABLES_DISPLAYED_CORRECTLY
,
274 "target variable my_global_char",
275 VARIABLES_DISPLAYED_CORRECTLY
,
281 def test_target_stop_hook_disable_enable(self
):
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"'])
291 def test_target_stop_hook_delete(self
):
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"'])
299 def test_target_list_args(self
):
300 self
.expect("target list blub", error
=True,
301 substrs
=["the 'target list' command takes no arguments"])
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"])
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"])
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"])
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"])
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.
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"])
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'"])
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.
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"])
361 def test_target_delete_all(self
):
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."])
369 def test_target_delete_by_index(self
):
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"])
392 def test_target_delete_by_index_multiple(self
):
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"])
406 def test_target_delete_selected(self
):
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"])
420 def test_target_modules_search_paths_clear(self
):
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"])
429 def test_target_modules_search_paths_query(self
):
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"])
438 self
.expect("target modules search-paths query faz baz", error
=True,
439 substrs
=["query requires one argument"])