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