1"""
2Test the lldb command line completion mechanism.
3"""
4
5
6
7import os
8from multiprocessing import Process
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbplatform
13from lldbsuite.test import lldbutil
14
15
16class CommandLineCompletionTestCase(TestBase):
17
18    mydir = TestBase.compute_mydir(__file__)
19
20    NO_DEBUG_INFO_TESTCASE = True
21
22    @classmethod
23    def classCleanup(cls):
24        """Cleanup the test byproducts."""
25        try:
26            os.remove("child_send.txt")
27            os.remove("child_read.txt")
28        except:
29            pass
30
31    def test_at(self):
32        """Test that 'at' completes to 'attach '."""
33        self.complete_from_to('at', 'attach ')
34
35    def test_de(self):
36        """Test that 'de' completes to 'detach '."""
37        self.complete_from_to('de', 'detach ')
38
39    def test_frame_variable(self):
40        self.build()
41        self.main_source = "main.cpp"
42        self.main_source_spec = lldb.SBFileSpec(self.main_source)
43
44        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
45                                          '// Break here', self.main_source_spec)
46        self.assertEquals(process.GetState(), lldb.eStateStopped)
47
48        # Since CommandInterpreter has been corrected to update the current execution
49        # context at the beginning of HandleCompletion, we're here explicitly testing
50        # the scenario where "frame var" is completed without any preceding commands.
51
52        self.complete_from_to('frame variable fo',
53                              'frame variable fooo')
54        self.complete_from_to('frame variable fooo.',
55                              'frame variable fooo.')
56        self.complete_from_to('frame variable fooo.dd',
57                              'frame variable fooo.dd')
58
59        self.complete_from_to('frame variable ptr_fooo->',
60                              'frame variable ptr_fooo->')
61        self.complete_from_to('frame variable ptr_fooo->dd',
62                              'frame variable ptr_fooo->dd')
63
64        self.complete_from_to('frame variable cont',
65                              'frame variable container')
66        self.complete_from_to('frame variable container.',
67                              'frame variable container.MemberVar')
68        self.complete_from_to('frame variable container.Mem',
69                              'frame variable container.MemberVar')
70
71        self.complete_from_to('frame variable ptr_cont',
72                              'frame variable ptr_container')
73        self.complete_from_to('frame variable ptr_container->',
74                              'frame variable ptr_container->MemberVar')
75        self.complete_from_to('frame variable ptr_container->Mem',
76                              'frame variable ptr_container->MemberVar')
77
78    def test_process_attach_dash_dash_con(self):
79        """Test that 'process attach --con' completes to 'process attach --continue '."""
80        self.complete_from_to(
81            'process attach --con',
82            'process attach --continue ')
83
84    def test_process_launch_arch(self):
85        self.complete_from_to('process launch --arch ',
86                              ['mips',
87                               'arm64'])
88
89    def test_process_load(self):
90        self.build()
91        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
92        self.complete_from_to('process load Makef', 'process load Makefile')
93
94    @skipUnlessPlatform(["linux"])
95    def test_process_unload(self):
96        """Test the completion for "process unload <index>" """
97        # This tab completion should not work without a running process.
98        self.complete_from_to('process unload ',
99                              'process unload ')
100
101        self.build()
102        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
103        err = lldb.SBError()
104        self.process().LoadImage(lldb.SBFileSpec(self.getBuildArtifact("libshared.so")), err)
105        self.assertSuccess(err)
106
107        self.complete_from_to('process unload ',
108                              'process unload 0')
109
110        self.process().UnloadImage(0)
111        self.complete_from_to('process unload ',
112                              'process unload ')
113
114    def test_process_plugin_completion(self):
115        subcommands = ['attach -P', 'connect -p', 'launch -p']
116
117        for subcommand in subcommands:
118            self.complete_from_to('process ' + subcommand + ' mac',
119                                  'process ' + subcommand + ' mach-o-core')
120
121    def completions_contain_str(self, input, needle):
122        interp = self.dbg.GetCommandInterpreter()
123        match_strings = lldb.SBStringList()
124        num_matches = interp.HandleCompletion(input, len(input), 0, -1, match_strings)
125        found_needle = False
126        for match in match_strings:
127          if needle in match:
128            found_needle = True
129            break
130        self.assertTrue(found_needle, "Returned completions: " + "\n".join(match_strings))
131
132
133    @skipIfRemote
134    @skipIfReproducer
135    def test_common_completion_process_pid_and_name(self):
136        # The LLDB process itself and the process already attached to are both
137        # ignored by the process discovery mechanism, thus we need a process known
138        # to us here.
139        self.build()
140        server = self.spawnSubprocess(
141            self.getBuildArtifact("a.out"),
142            ["-x"], # Arg "-x" makes the subprocess wait for input thus it won't be terminated too early
143            install_remote=False)
144        self.assertIsNotNone(server)
145        pid = server.pid
146
147        self.completions_contain('process attach -p ', [str(pid)])
148        self.completions_contain('platform process attach -p ', [str(pid)])
149        self.completions_contain('platform process info ', [str(pid)])
150
151        self.completions_contain_str('process attach -n ', "a.out")
152        self.completions_contain_str('platform process attach -n ', "a.out")
153
154    def test_process_signal(self):
155        # The tab completion for "process signal"  won't work without a running process.
156        self.complete_from_to('process signal ',
157                              'process signal ')
158
159        # Test with a running process.
160        self.build()
161        self.main_source = "main.cpp"
162        self.main_source_spec = lldb.SBFileSpec(self.main_source)
163        lldbutil.run_to_source_breakpoint(self, '// Break here', self.main_source_spec)
164
165        self.complete_from_to('process signal ',
166                              'process signal SIG')
167        self.complete_from_to('process signal SIGPIP',
168                              'process signal SIGPIPE')
169        self.complete_from_to('process signal SIGA',
170                              ['SIGABRT',
171                               'SIGALRM'])
172
173    def test_ambiguous_long_opt(self):
174        self.completions_match('breakpoint modify --th',
175                               ['--thread-id',
176                                '--thread-index',
177                                '--thread-name'])
178
179    def test_disassemble_dash_f(self):
180        self.completions_match('disassemble -F ',
181                               ['default',
182                                'intel',
183                                'att'])
184
185    def test_plugin_load(self):
186        self.complete_from_to('plugin load ', [])
187
188    def test_log_enable(self):
189        self.complete_from_to('log enable ll', ['lldb'])
190        self.complete_from_to('log enable dw', ['dwarf'])
191        self.complete_from_to('log enable lldb al', ['all'])
192        self.complete_from_to('log enable lldb sym', ['symbol'])
193
194    def test_log_enable(self):
195        self.complete_from_to('log disable ll', ['lldb'])
196        self.complete_from_to('log disable dw', ['dwarf'])
197        self.complete_from_to('log disable lldb al', ['all'])
198        self.complete_from_to('log disable lldb sym', ['symbol'])
199
200    def test_log_list(self):
201        self.complete_from_to('log list ll', ['lldb'])
202        self.complete_from_to('log list dw', ['dwarf'])
203        self.complete_from_to('log list ll', ['lldb'])
204        self.complete_from_to('log list lldb dwa', ['dwarf'])
205
206    def test_quoted_command(self):
207        self.complete_from_to('"set',
208                              ['"settings" '])
209
210    def test_quoted_arg_with_quoted_command(self):
211        self.complete_from_to('"settings" "repl',
212                              ['"replace" '])
213
214    def test_quoted_arg_without_quoted_command(self):
215        self.complete_from_to('settings "repl',
216                              ['"replace" '])
217
218    def test_single_quote_command(self):
219        self.complete_from_to("'set",
220                              ["'settings' "])
221
222    def test_terminated_quote_command(self):
223        # This should not crash, but we don't get any
224        # reasonable completions from this.
225        self.complete_from_to("'settings'", [])
226
227    def test_process_launch_arch_arm(self):
228        self.complete_from_to('process launch --arch arm',
229                              ['arm64'])
230
231    def test_target_symbols_add_shlib(self):
232        # Doesn't seem to work, but at least it shouldn't crash.
233        self.complete_from_to('target symbols add --shlib ', [])
234
235    def test_log_file(self):
236        # Complete in our source directory which contains a 'main.cpp' file.
237        src_dir =  os.path.dirname(os.path.realpath(__file__)) + '/'
238        self.complete_from_to('log enable lldb expr -f ' + src_dir,
239                              ['main.cpp'])
240
241    def test_log_dir(self):
242        # Complete our source directory.
243        src_dir =  os.path.dirname(os.path.realpath(__file__))
244        self.complete_from_to('log enable lldb expr -f ' + src_dir,
245                              [src_dir + os.sep], turn_off_re_match=True)
246
247    # <rdar://problem/11052829>
248    def test_infinite_loop_while_completing(self):
249        """Test that 'process print hello\' completes to itself and does not infinite loop."""
250        self.complete_from_to('process print hello\\', 'process print hello\\',
251                              turn_off_re_match=True)
252
253    def test_watchpoint_co(self):
254        """Test that 'watchpoint co' completes to 'watchpoint command '."""
255        self.complete_from_to('watchpoint co', 'watchpoint command ')
256
257    def test_watchpoint_command_space(self):
258        """Test that 'watchpoint command ' completes to ['add', 'delete', 'list']."""
259        self.complete_from_to(
260            'watchpoint command ', [
261                'add', 'delete', 'list'])
262
263    def test_watchpoint_command_a(self):
264        """Test that 'watchpoint command a' completes to 'watchpoint command add '."""
265        self.complete_from_to(
266            'watchpoint command a',
267            'watchpoint command add ')
268
269    def test_watchpoint_set_ex(self):
270        """Test that 'watchpoint set ex' completes to 'watchpoint set expression '."""
271        self.complete_from_to(
272            'watchpoint set ex',
273            'watchpoint set expression ')
274
275    def test_watchpoint_set_var(self):
276        """Test that 'watchpoint set var' completes to 'watchpoint set variable '."""
277        self.complete_from_to('watchpoint set var', 'watchpoint set variable ')
278
279    def test_watchpoint_set_variable_foo(self):
280        self.build()
281        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
282        self.complete_from_to('watchpoint set variable fo', 'watchpoint set variable fooo')
283        # Only complete the first argument.
284        self.complete_from_to('watchpoint set variable fooo ', 'watchpoint set variable fooo ')
285
286    def test_help_fi(self):
287        """Test that 'help fi' completes to ['file', 'finish']."""
288        self.complete_from_to(
289            'help fi', [
290                'file', 'finish'])
291
292    def test_help_watchpoint_s(self):
293        """Test that 'help watchpoint s' completes to 'help watchpoint set '."""
294        self.complete_from_to('help watchpoint s', 'help watchpoint set ')
295
296    @expectedFailureNetBSD
297    def test_common_complete_watchpoint_ids(self):
298        subcommands = ['enable', 'disable', 'delete', 'modify', 'ignore']
299
300        # Completion should not work without a target.
301        for subcommand in subcommands:
302            self.complete_from_to('watchpoint ' + subcommand + ' ',
303                                  'watchpoint ' + subcommand + ' ')
304
305        # Create a process to provide a target and enable watchpoint setting.
306        self.build()
307        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
308
309        self.runCmd('watchpoint set variable ptr_fooo')
310        for subcommand in subcommands:
311            self.complete_from_to('watchpoint ' + subcommand + ' ', ['1'])
312
313    def test_settings_append_target_er(self):
314        """Test that 'settings append target.er' completes to 'settings append target.error-path'."""
315        self.complete_from_to(
316            'settings append target.er',
317            'settings append target.error-path')
318
319    def test_settings_insert_after_target_en(self):
320        """Test that 'settings insert-after target.env' completes to 'settings insert-after target.env-vars'."""
321        self.complete_from_to(
322            'settings insert-after target.env',
323            'settings insert-after target.env-vars')
324
325    def test_settings_insert_before_target_en(self):
326        """Test that 'settings insert-before target.env' completes to 'settings insert-before target.env-vars'."""
327        self.complete_from_to(
328            'settings insert-before target.env',
329            'settings insert-before target.env-vars')
330
331    def test_settings_replace_target_ru(self):
332        """Test that 'settings replace target.ru' completes to 'settings replace target.run-args'."""
333        self.complete_from_to(
334            'settings replace target.ru',
335            'settings replace target.run-args')
336
337    def test_settings_show_term(self):
338        self.complete_from_to(
339            'settings show term-',
340            'settings show term-width')
341
342    def test_settings_list_term(self):
343        self.complete_from_to(
344            'settings list term-',
345            'settings list term-width')
346
347    def test_settings_remove_term(self):
348        self.complete_from_to(
349            'settings remove term-',
350            'settings remove term-width')
351
352    def test_settings_s(self):
353        """Test that 'settings s' completes to ['set', 'show']."""
354        self.complete_from_to(
355            'settings s', [
356                'set', 'show'])
357
358    def test_settings_set_th(self):
359        """Test that 'settings set thread-f' completes to 'settings set thread-format'."""
360        self.complete_from_to('settings set thread-f', 'settings set thread-format')
361
362    def test_settings_s_dash(self):
363        """Test that 'settings set --g' completes to 'settings set --global'."""
364        self.complete_from_to('settings set --g', 'settings set --global')
365
366    def test_settings_clear_th(self):
367        """Test that 'settings clear thread-f' completes to 'settings clear thread-format'."""
368        self.complete_from_to(
369            'settings clear thread-f',
370            'settings clear thread-format')
371
372    def test_settings_set_ta(self):
373        """Test that 'settings set ta' completes to 'settings set target.'."""
374        self.complete_from_to(
375            'settings set target.ma',
376            'settings set target.max-')
377
378    def test_settings_set_target_exec(self):
379        """Test that 'settings set target.exec' completes to 'settings set target.exec-search-paths '."""
380        self.complete_from_to(
381            'settings set target.exec',
382            'settings set target.exec-search-paths')
383
384    def test_settings_set_target_pr(self):
385        """Test that 'settings set target.pr' completes to [
386        'target.prefer-dynamic-value', 'target.process.']."""
387        self.complete_from_to('settings set target.pr',
388                              ['target.prefer-dynamic-value',
389                               'target.process.'])
390
391    def test_settings_set_target_process(self):
392        """Test that 'settings set target.process' completes to 'settings set target.process.'."""
393        self.complete_from_to(
394            'settings set target.process',
395            'settings set target.process.')
396
397    def test_settings_set_target_process_dot(self):
398        """Test that 'settings set target.process.t' completes to 'settings set target.process.thread.'."""
399        self.complete_from_to(
400            'settings set target.process.t',
401            'settings set target.process.thread.')
402
403    def test_settings_set_target_process_thread_dot(self):
404        """Test that 'settings set target.process.thread.' completes to [
405        'target.process.thread.step-avoid-regexp', 'target.process.thread.trace-thread']."""
406        self.complete_from_to('settings set target.process.thread.',
407                              ['target.process.thread.step-avoid-regexp',
408                               'target.process.thread.trace-thread'])
409
410    def test_thread_plan_discard(self):
411        self.build()
412        (_, _, thread, _) = lldbutil.run_to_source_breakpoint(self,
413                                          'ptr_foo', lldb.SBFileSpec("main.cpp"))
414        self.assertTrue(thread)
415        self.complete_from_to('thread plan discard ', 'thread plan discard ')
416
417        source_path = os.path.join(self.getSourceDir(), "thread_plan_script.py")
418        self.runCmd("command script import '%s'"%(source_path))
419        self.runCmd("thread step-scripted -C thread_plan_script.PushPlanStack")
420        self.complete_from_to('thread plan discard ', 'thread plan discard 1')
421        self.runCmd('thread plan discard 1')
422
423    def test_target_space(self):
424        """Test that 'target ' completes to ['create', 'delete', 'list',
425        'modules', 'select', 'stop-hook', 'variable']."""
426        self.complete_from_to('target ',
427                              ['create',
428                               'delete',
429                               'list',
430                               'modules',
431                               'select',
432                               'stop-hook',
433                               'variable'])
434
435    def test_target_modules_dump_line_table(self):
436        """Tests source file completion by completing the line-table argument."""
437        self.build()
438        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
439        self.complete_from_to('target modules dump line-table main.cp',
440                              ['main.cpp'])
441
442    def test_target_modules_load_aout(self):
443        """Tests modules completion by completing the target modules load argument."""
444        self.build()
445        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
446        self.complete_from_to('target modules load a.ou',
447                              ['a.out'])
448
449    def test_target_modules_search_paths_insert(self):
450        # Completion won't work without a valid target.
451        self.complete_from_to("target modules search-paths insert ", "target modules search-paths insert ")
452        self.build()
453        target = self.dbg.CreateTarget(self.getBuildArtifact('a.out'))
454        self.assertTrue(target, VALID_TARGET)
455        self.complete_from_to("target modules search-paths insert ", "target modules search-paths insert ")
456        self.runCmd("target modules search-paths add a b")
457        self.complete_from_to("target modules search-paths insert ", "target modules search-paths insert 0")
458        # Completion only works for the first arg.
459        self.complete_from_to("target modules search-paths insert 0 ", "target modules search-paths insert 0 ")
460
461    def test_target_create_dash_co(self):
462        """Test that 'target create --co' completes to 'target variable --core '."""
463        self.complete_from_to('target create --co', 'target create --core ')
464
465    def test_target_va(self):
466        """Test that 'target va' completes to 'target variable '."""
467        self.complete_from_to('target va', 'target variable ')
468
469    def test_common_completion_thread_index(self):
470        subcommands = ['continue', 'info', 'exception', 'select',
471                       'step-in', 'step-inst', 'step-inst-over', 'step-out', 'step-over', 'step-script']
472
473        # Completion should do nothing without threads.
474        for subcommand in subcommands:
475            self.complete_from_to('thread ' + subcommand + ' ',
476                                  'thread ' + subcommand + ' ')
477
478        self.build()
479        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
480
481        # At least we have the thread at the index of 1 now.
482        for subcommand in subcommands:
483            self.complete_from_to('thread ' + subcommand + ' ', ['1'])
484
485    def test_common_completion_type_category_name(self):
486        subcommands = ['delete', 'list', 'enable', 'disable', 'define']
487        for subcommand in subcommands:
488            self.complete_from_to('type category ' + subcommand + ' ', ['default'])
489        self.complete_from_to('type filter add -w ', ['default'])
490
491    def test_command_argument_completion(self):
492        """Test completion of command arguments"""
493        self.complete_from_to("watchpoint set variable -", ["-w", "-s"])
494        self.complete_from_to('watchpoint set variable -w', 'watchpoint set variable -w ')
495        self.complete_from_to("watchpoint set variable --", ["--watch", "--size"])
496        self.complete_from_to("watchpoint set variable --w", "watchpoint set variable --watch")
497        self.complete_from_to('watchpoint set variable -w ', ['read', 'write', 'read_write'])
498        self.complete_from_to("watchpoint set variable --watch ", ["read", "write", "read_write"])
499        self.complete_from_to("watchpoint set variable --watch w", "watchpoint set variable --watch write")
500        self.complete_from_to('watchpoint set variable -w read_', 'watchpoint set variable -w read_write')
501        # Now try the same thing with a variable name (non-option argument) to
502        # test that getopts arg reshuffling doesn't confuse us.
503        self.complete_from_to("watchpoint set variable foo -", ["-w", "-s"])
504        self.complete_from_to('watchpoint set variable foo -w', 'watchpoint set variable foo -w ')
505        self.complete_from_to("watchpoint set variable foo --", ["--watch", "--size"])
506        self.complete_from_to("watchpoint set variable foo --w", "watchpoint set variable foo --watch")
507        self.complete_from_to('watchpoint set variable foo -w ', ['read', 'write', 'read_write'])
508        self.complete_from_to("watchpoint set variable foo --watch ", ["read", "write", "read_write"])
509        self.complete_from_to("watchpoint set variable foo --watch w", "watchpoint set variable foo --watch write")
510        self.complete_from_to('watchpoint set variable foo -w read_', 'watchpoint set variable foo -w read_write')
511
512    def test_command_script_delete(self):
513        self.runCmd("command script add -h test_desc -f none -s current usercmd1")
514        self.check_completion_with_desc('command script delete ', [['usercmd1', 'test_desc']])
515
516    def test_command_delete(self):
517        self.runCmd(r"command regex test_command s/^$/finish/ 's/([0-9]+)/frame select %1/'")
518        self.complete_from_to('command delete test_c', 'command delete test_command')
519
520    def test_command_unalias(self):
521        self.complete_from_to('command unalias ima', 'command unalias image')
522
523    def test_completion_description_commands(self):
524        """Test descriptions of top-level command completions"""
525        self.check_completion_with_desc("", [
526            ["command", "Commands for managing custom LLDB commands."],
527            ["breakpoint", "Commands for operating on breakpoints (see 'help b' for shorthand.)"]
528        ])
529
530        self.check_completion_with_desc("pl", [
531            ["platform", "Commands to manage and create platforms."],
532            ["plugin", "Commands for managing LLDB plugins."]
533        ])
534
535        # Just check that this doesn't crash.
536        self.check_completion_with_desc("comman", [])
537        self.check_completion_with_desc("non-existent-command", [])
538
539    def test_completion_description_command_options(self):
540        """Test descriptions of command options"""
541        # Short options
542        self.check_completion_with_desc("breakpoint set -", [
543            ["-h", "Set the breakpoint on exception catcH."],
544            ["-w", "Set the breakpoint on exception throW."]
545        ])
546
547        # Long options.
548        self.check_completion_with_desc("breakpoint set --", [
549            ["--on-catch", "Set the breakpoint on exception catcH."],
550            ["--on-throw", "Set the breakpoint on exception throW."]
551        ])
552
553        # Ambiguous long options.
554        self.check_completion_with_desc("breakpoint set --on-", [
555            ["--on-catch", "Set the breakpoint on exception catcH."],
556            ["--on-throw", "Set the breakpoint on exception throW."]
557        ])
558
559        # Unknown long option.
560        self.check_completion_with_desc("breakpoint set --Z", [
561        ])
562
563    def test_common_completion_frame_index(self):
564        self.build()
565        lldbutil.run_to_source_breakpoint(self, '// Break here', lldb.SBFileSpec("main.cpp"))
566
567        self.complete_from_to('frame select ', ['0'])
568        self.complete_from_to('thread backtrace -s ', ['0'])
569
570    def test_frame_recognizer_delete(self):
571        self.runCmd("frame recognizer add -l py_class -s module_name -n recognizer_name")
572        self.check_completion_with_desc('frame recognizer delete ', [['0', 'py_class, module module_name, symbol recognizer_name']])
573
574    def test_platform_install_local_file(self):
575        self.complete_from_to('platform target-install main.cp', 'platform target-install main.cpp')
576
577    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24489")
578    def test_symbol_name(self):
579        self.build()
580        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
581        self.complete_from_to('breakpoint set -n Fo',
582                              'breakpoint set -n Foo::Bar(int,\\ int)',
583                              turn_off_re_match=True)
584        # No completion for Qu because the candidate is
585        # (anonymous namespace)::Quux().
586        self.complete_from_to('breakpoint set -n Qu', '')
587
588    def test_completion_type_formatter_delete(self):
589        self.runCmd('type filter add --child a Aoo')
590        self.complete_from_to('type filter delete ', ['Aoo'])
591        self.runCmd('type filter add --child b -x Boo')
592        self.complete_from_to('type filter delete ', ['Boo'])
593
594        self.runCmd('type format add -f hex Coo')
595        self.complete_from_to('type format delete ', ['Coo'])
596        self.runCmd('type format add -f hex -x Doo')
597        self.complete_from_to('type format delete ', ['Doo'])
598
599        self.runCmd('type summary add -c Eoo')
600        self.complete_from_to('type summary delete ', ['Eoo'])
601        self.runCmd('type summary add -x -c Foo')
602        self.complete_from_to('type summary delete ', ['Foo'])
603
604        self.runCmd('type synthetic add Goo -l test')
605        self.complete_from_to('type synthetic delete ', ['Goo'])
606        self.runCmd('type synthetic add -x Hoo -l test')
607        self.complete_from_to('type synthetic delete ', ['Hoo'])
608
609    @skipIf(archs=no_match(['x86_64']))
610    def test_register_read_and_write_on_x86(self):
611        """Test the completion of the commands register read and write on x86"""
612
613        # The tab completion for "register read/write"  won't work without a running process.
614        self.complete_from_to('register read ',
615                              'register read ')
616        self.complete_from_to('register write ',
617                              'register write ')
618
619        self.build()
620        self.main_source_spec = lldb.SBFileSpec("main.cpp")
621        lldbutil.run_to_source_breakpoint(self, '// Break here', self.main_source_spec)
622
623        # test cases for register read
624        self.complete_from_to('register read ',
625                              ['rax',
626                               'rbx',
627                               'rcx'])
628        self.complete_from_to('register read r',
629                              ['rax',
630                               'rbx',
631                               'rcx'])
632        self.complete_from_to('register read ra',
633                              'register read rax')
634        # register read can take multiple register names as arguments
635        self.complete_from_to('register read rax ',
636                              ['rax',
637                               'rbx',
638                               'rcx'])
639        # complete with prefix '$'
640        self.completions_match('register read $rb',
641                              ['$rbx',
642                               '$rbp'])
643        self.completions_match('register read $ra',
644                              ['$rax'])
645        self.complete_from_to('register read rax $',
646                              ['\$rax',
647                               '\$rbx',
648                               '\$rcx'])
649        self.complete_from_to('register read $rax ',
650                              ['rax',
651                               'rbx',
652                               'rcx'])
653
654        # test cases for register write
655        self.complete_from_to('register write ',
656                              ['rax',
657                               'rbx',
658                               'rcx'])
659        self.complete_from_to('register write r',
660                              ['rax',
661                               'rbx',
662                               'rcx'])
663        self.complete_from_to('register write ra',
664                              'register write rax')
665        self.complete_from_to('register write rb',
666                              ['rbx',
667                               'rbp'])
668        # register write can only take exact one register name as argument
669        self.complete_from_to('register write rbx ',
670                              [])
671
672    def test_common_completion_target_stophook_ids(self):
673        subcommands = ['delete', 'enable', 'disable']
674
675        for subcommand in subcommands:
676            self.complete_from_to('target stop-hook ' + subcommand + ' ',
677                                  'target stop-hook ' + subcommand + ' ')
678
679        self.build()
680        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
681        self.runCmd('target stop-hook add test DONE')
682
683        for subcommand in subcommands:
684            self.complete_from_to('target stop-hook ' + subcommand + ' ',
685                                  'target stop-hook ' + subcommand + ' 1')
686
687        # Completion should work only on the first argument.
688        for subcommand in subcommands:
689            self.complete_from_to('target stop-hook ' + subcommand + ' 1 ',
690                                  'target stop-hook ' + subcommand + ' 1 ')
691
692    def test_common_completion_type_language(self):
693        self.complete_from_to('type category -l ', ['c'])
694
695    def test_target_modules_load_dash_u(self):
696        self.build()
697        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
698        self.complete_from_to('target modules load -u ', [target.GetModuleAtIndex(0).GetUUIDString()])
699
700    def test_complete_breakpoint_with_ids(self):
701        """These breakpoint subcommands should be completed with a list of breakpoint ids"""
702
703        subcommands = ['enable', 'disable', 'delete', 'modify', 'name add', 'name delete', 'write']
704
705        # The tab completion here is unavailable without a target
706        for subcommand in subcommands:
707            self.complete_from_to('breakpoint ' + subcommand + ' ',
708                                  'breakpoint ' + subcommand + ' ')
709
710        self.build()
711        target = self.dbg.CreateTarget(self.getBuildArtifact('a.out'))
712        self.assertTrue(target, VALID_TARGET)
713
714        bp = target.BreakpointCreateByName('main', 'a.out')
715        self.assertTrue(bp)
716        self.assertEqual(bp.GetNumLocations(), 1)
717
718        for subcommand in subcommands:
719            self.complete_from_to('breakpoint ' + subcommand + ' ',
720                                  ['1'])
721
722        bp2 = target.BreakpointCreateByName('Bar', 'a.out')
723        self.assertTrue(bp2)
724        self.assertEqual(bp2.GetNumLocations(), 1)
725
726        for subcommand in subcommands:
727            self.complete_from_to('breakpoint ' + subcommand + ' ',
728                                  ['1',
729                                   '2'])
730
731        for subcommand in subcommands:
732            self.complete_from_to('breakpoint ' + subcommand + ' 1 ',
733                                  ['1',
734                                   '2'])
735
736    def test_complete_breakpoint_with_names(self):
737        self.build()
738        target = self.dbg.CreateTarget(self.getBuildArtifact('a.out'))
739        self.assertTrue(target, VALID_TARGET)
740
741        # test breakpoint read dedicated
742        self.complete_from_to('breakpoint read -N ', 'breakpoint read -N ')
743        self.complete_from_to('breakpoint read -f breakpoints.json -N ', ['mm'])
744        self.complete_from_to('breakpoint read -f breakpoints.json -N n', 'breakpoint read -f breakpoints.json -N n')
745        self.complete_from_to('breakpoint read -f breakpoints_invalid.json -N ', 'breakpoint read -f breakpoints_invalid.json -N ')
746
747        # test common breapoint name completion
748        bp1 = target.BreakpointCreateByName('main', 'a.out')
749        self.assertTrue(bp1)
750        self.assertEqual(bp1.GetNumLocations(), 1)
751        self.complete_from_to('breakpoint set -N n', 'breakpoint set -N n')
752        self.assertTrue(bp1.AddNameWithErrorHandling("nn"))
753        self.complete_from_to('breakpoint set -N ', 'breakpoint set -N nn')
754