gdb/doc/
[external/binutils.git] / gdb / testsuite / gdb.base / completion.exp
1 # Copyright 1998, 1999, 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # This file was written by Elena Zannoni (ezannoni@cygnus.com)
18
19 # This file is part of the gdb testsuite.
20
21 #
22 # tests for command completion
23 #
24 # Here are some useful test cases for completion.  
25 # They should be tested with both M-? and TAB.
26 #
27 #   "show output-" "radix"
28 #   "show output" "-radix"
29 #   "p" ambiguous (commands starting with p--path, print, printf, etc.)
30 #   "p "  ambiguous (all symbols)
31 #   "info t foo" no completions
32 #   "info t " no completions
33 #   "info t" ambiguous ("info target", "info terminal", etc.)
34 #   "info ajksdlfk" no completions
35 #   "info ajksdlfk " no completions
36 #   "info" " "
37 #   "info " ambiguous (all info commands)
38 #   "p \"break1" unambiguous (completes to filename "break1.c")
39 #   "p \"break1." unambiguous (should complete to "break1.c" but does not,
40 #       due to readline limitations)
41 #   "p 'arg" ambiguous (all symbols starting with arg)
42 #   "p b-arg" ambiguous (all symbols starting with arg)
43 #   "p b-" ambiguous (all symbols)
44 #   "file Make" "file" (word break hard to screw up here)
45 #   "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
46 #
47
48
49 if $tracelevel then {
50         strace $tracelevel
51         }
52
53
54 #
55 # test running programs
56 #
57
58 set testfile "break"
59 set srcfile ${testfile}.c
60 set srcfile1 ${testfile}1.c
61 set binfile ${objdir}/${subdir}/${testfile}
62
63 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}0.o" object {debug nowarnings}] != "" } {
64      untested completion.exp
65      return -1
66 }
67
68 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile}1.o" object {debug nowarnings}] != "" } {
69      untested completion.exp
70      return -1
71 }
72
73 if  { [gdb_compile "${binfile}0.o ${binfile}1.o" "${binfile}" executable {debug nowarnings}] != "" } {
74      untested completion.exp
75      return -1
76 }
77
78 if [get_compiler_info ${binfile}] {
79     return -1;
80 }
81
82 gdb_exit
83
84 gdb_start
85 gdb_reinitialize_dir $srcdir/$subdir
86 gdb_load ${binfile}
87
88 if ![runto_main] then {
89         perror "tests suppressed"
90 }
91
92 set oldtimeout1 $timeout
93 set timeout 30
94
95 set test "complete 'hfgfh'"
96 send_gdb "hfgfh\t"
97 gdb_test_multiple "" "$test" {
98     -re "^hfgfh\\\x07$" {
99         send_gdb "\n"
100         gdb_test_multiple "" $test {
101             -re "Undefined command: \"hfgfh\"\\.  Try \"help\"\\..*$gdb_prompt $" {
102                 pass "$test"
103             }
104         }
105     }
106 }
107
108 #exp_internal 0
109
110 set test "complete 'show output'"
111 send_gdb "show output\t"
112 gdb_test_multiple "" "$test" {
113     -re "^show output-radix $" {
114         send_gdb "\n"
115         gdb_test_multiple "" "$test" {
116             -re "Default output radix for printing of values is 10\\..*$gdb_prompt $" {
117                 pass "$test"
118             }
119         }
120     }
121 }
122
123 set test "complete 'show output-'"
124 send_gdb "show output-\t"
125 gdb_test_multiple "" "$test" {
126     -re "^show output-radix $" {
127         send_gdb "\n"
128         gdb_test_multiple "" "$test" {
129             -re "Default output radix for printing of values is 10\\..*$gdb_prompt $" {
130                 pass "$test"
131             }
132         }
133     }
134 }
135
136 set test "complete 'p'"
137 send_gdb "p\t"
138 gdb_test_multiple "" "$test" {
139     -re "^p\\\x07$" {
140         send_gdb "\n"
141         gdb_test_multiple "" "$test" {
142             -re "The history is empty\\..*$gdb_prompt $" {
143                 pass "$test"
144             }
145         }
146     }
147 }
148
149 set test "complete 'p '"
150 send_gdb "p \t"
151 gdb_test_multiple "" "$test" {
152     -re "^p \\\x07$" {
153         send_gdb "\n"
154         gdb_test_multiple "" "$test" {
155             -re "The history is empty\\..*$gdb_prompt $" {
156                 pass "$test"
157             }
158         }
159     }
160 }
161
162 set test "complete 'info t foo'"
163 send_gdb "info t foo\t"
164 gdb_test_multiple "" "$test" {
165     -re "^info t foo\\\x07$" {
166         send_gdb "\n"
167         gdb_test_multiple "" "$test" {
168             -re "Ambiguous info command \"t foo\": target, tasks, terminal, threads, tp, tracepoints, tvariables, types\\..*$gdb_prompt $" {
169                 pass "$test"
170             }
171         }
172     }
173 }
174
175 set test "complete 'info t'"
176 send_gdb "info t\t"
177 gdb_test_multiple "" "$test" {
178     -re "^info t\\\x07$" {
179         send_gdb "\n"
180         gdb_test_multiple "" "$test" {
181             -re "Ambiguous info command \"t\": target, tasks, terminal, threads, tp, tracepoints, tvariables, types\\..*$gdb_prompt $" {
182                 pass "$test"
183             }
184         }
185     }
186 }
187
188 set test "complete 'info t '"
189 send_gdb "info t \t"
190 gdb_test_multiple "" "$test" {
191     -re "^info t \\\x07$" {
192         send_gdb "\n"
193         gdb_test_multiple "" "$test" {
194             -re "Ambiguous info command \"t \": target, tasks, terminal, threads, tp, tracepoints, tvariables, types\\..*$gdb_prompt $" {
195                 pass "$test"
196             }
197         }
198     }
199 }
200
201 set test "complete 'info asdfgh'"
202 send_gdb "info asdfgh\t"
203 gdb_test_multiple "" "$test" {
204     -re "^info asdfgh\\\x07$" {
205         send_gdb "\n"
206         gdb_test_multiple "" "$test" {
207             -re "Undefined info command: \"asdfgh\".  Try \"help info\"\\..*$gdb_prompt $" {
208                 pass "$test"
209             }
210         }
211     }
212 }
213
214 set test "complete 'info asdfgh '"
215 send_gdb "info asdfgh \t"
216 gdb_test_multiple "" "$test" {
217     -re "^info asdfgh \\\x07$" {
218         send_gdb "\n"
219         gdb_test_multiple "" "$test" {
220             -re "Undefined info command: \"asdfgh \".  Try \"help info\"\\..*$gdb_prompt $" {
221                 pass "$test"
222             }
223         }
224     }
225 }
226
227 set test "complete 'info'"
228 send_gdb "info\t"
229 gdb_test_multiple "" "$test" {
230     -re "^info $" {
231         send_gdb "\n"
232         gdb_test_multiple "" "$test" {
233             -re "\"info\" must be followed by the name of an info command\\.\r\nList of info subcommands.*$gdb_prompt $" {
234                 pass "$test"
235             }
236         }
237     }
238 }
239
240 set test "complete 'info '"
241 send_gdb "info \t"
242 gdb_test_multiple "" "$test" {
243     -re "^info \\\x07$" {
244         send_gdb "\n"
245         gdb_test_multiple "" "$test" {
246             -re "\"info\" must be followed by the name of an info command\\.\r\nList of info subcommands:\r\n\r\n.*$gdb_prompt $" {
247                 pass "$test"
248             }
249         }
250     }
251 }
252
253 set test "complete (2) 'info '"
254 send_gdb "info \t"
255 gdb_test_multiple "" "$test" {
256     -re "^info \\\x07$" {
257         send_gdb "\t"
258         gdb_test_multiple "" "$test" {
259             -re "address.*types.*$gdb_prompt " {
260                 send_gdb "\n"
261                 gdb_test_multiple "" "$test" {
262                     -re "\"info\".*unambiguous\\..*$gdb_prompt $" {
263                         pass "$test"
264                     }
265                 }
266             }
267         }
268     }
269 }
270
271 set test "complete 'help info wat'"
272 send_gdb "help info wat\t"
273 gdb_test_multiple "" "$test" {
274     -re "^help info watchpoints $" {
275         send_gdb "\n"
276         gdb_test_multiple "" "$test" {
277             -re "Status of specified watchpoints.*\r\n.*$gdb_prompt $" {
278                 pass "$test"
279             }
280         }
281     }
282     -re "^help info wat\\\x07$" {
283         fail "$test"
284     }
285 }
286
287 set test "complete 'p \"break1'"
288 send_gdb "p \"break1\t"
289 gdb_test_multiple "" "$test" {
290     -re "^p \"break1\\\x07$" {
291         send_gdb "\n"
292         gdb_test_multiple "" "$test" {}
293     }
294     -re "^p \"break1\\.c\"$" {
295         send_gdb "\n"
296         gdb_test_multiple "" "$test" {
297             -re "$gdb_prompt $" {
298                 pass "$test"
299             }
300         }
301     }
302 }
303
304 setup_xfail "*-*-*"
305 set test "complete 'p \"break1.'"
306 send_gdb "p \"break1.\t"
307 gdb_test_multiple "" "$test" {
308     -re "^p \"break1\\.\\\x07$" {
309         send_gdb "\n"
310         gdb_test_multiple "" "$test" {}
311     }
312     -re "^p \"break1\\.c\"$" {
313         send_gdb "\n"
314         gdb_test_multiple "" "$test" {
315             -re "$gdb_prompt $" {
316                 pass "$test"
317             }
318         }
319     }
320     -re "^p \"break1\\..*$" {
321         send_gdb "\n"
322         gdb_test_multiple "" "$test" {}
323     }
324 }
325
326 set test "complete 'p 'arg'"
327 send_gdb "p 'arg\t"
328 gdb_test_multiple "" "$test" {
329     -re "^p 'arg\\\x07$" {
330         send_gdb "\n"
331         gdb_test_multiple "" "$test" {
332             -re "(Invalid character constant\\.|Unmatched single quote\\.).*$gdb_prompt $" {
333                 pass "$test"
334             }
335         }
336     }
337 }
338
339 set test "complete (2) 'p 'arg'"
340 send_gdb "p 'arg\t"
341 gdb_test_multiple "" "$test" {
342     -re "^p 'arg\\\x07$" {
343         send_gdb "\t"
344         gdb_test_multiple "" "$test" {
345             -re "argv.*$gdb_prompt " {
346                 send_gdb "\n"
347                 gdb_test_multiple "" "$test" {
348                     -re "(Invalid character constant\\.|Unmatched single quote\\.).*$gdb_prompt $" {
349                         pass "$test"
350                     }
351                 }
352             }
353             -re "(There are $decimal possibilities\\.  Do you really\r\nwish to see them all.|Display all $decimal possibilities.) \\(y or n\\)$" {
354                 send_gdb "n"
355                 gdb_test_multiple "" "$test" {
356                     -re "\\(gdb\\) p 'arg$" {
357                         send_gdb "\n"
358                         gdb_test_multiple "" "$test" {
359                             -re "(Invalid character constant\\.|Unmatched single quote\\.).*$gdb_prompt $" {
360                                 pass "$test"
361                             }
362                         }
363                     }
364                 }
365             }
366         }
367     }
368 }
369
370
371 # These tests used to try completing the shorter "p b-a".
372 # Unfortunately, on some systems, there are .o files in system
373 # libraries which declare static variables named `b'.  Of course,
374 # those variables aren't really in scope, as far as the compiler is
375 # concerned.  But GDB deliberately tries to be more liberal: if you
376 # enter an identifier that doesn't have any binding in scope, GDB will
377 # search all the program's compilation units for a static variable of
378 # the given name.
379 #
380 # This behavior can help avoid a lot of pedantry, so it's usually a
381 # good thing.  But in this test case, it causes GDB to print the value
382 # of some random variable, instead of giving us the "No symbol..."
383 # error we were expecting.
384 #
385 # For example, on S/390 linux, the file s_atan.c in libm.a declares a
386 # `b', which is a structure containing an int and a float, so GDB says
387 # ``Argument to arithmetic operation not a number or boolean'' instead
388 # of ``No symbol ...''.
389 #
390 # So, I'm hoping that there is no system with a static library variable named
391 # `no_var_by_this_name'.
392
393 set test "complete 'p no_var_named_this-arg'"
394 send_gdb "p no_var_named_this-arg\t"
395 gdb_test_multiple "" "$test" {
396     -re "^p no_var_named_this-arg\\\x07$" {
397         send_gdb "\n"
398         gdb_test_multiple "" "$test" {
399             -re "No symbol \"no_var_named_this\" in current context\\..*$gdb_prompt $" {
400                 pass "$test"
401             }
402         }
403     }
404 }
405
406 set test "complete (2) 'p no_var_named_this-arg'"
407 send_gdb "p no_var_named_this-arg\t"
408 gdb_test_multiple "" "$test" {
409     -re "^p no_var_named_this-arg\\\x07$" {
410         send_gdb "\t"
411         gdb_test_multiple "" "$test" {
412             -re "argv.*$gdb_prompt " {
413                 send_gdb "\n"
414                 gdb_test_multiple "" "$test" {
415                     -re "No symbol \"no_var_named_this\" in current context\\..*$gdb_prompt $" {
416                         pass "$test"
417                     }
418                 }
419             }
420             -re "(There are $decimal possibilities\\.  Do you really\r\nwish to see them all.|Display all $decimal possibilities.) \\(y or n\\)$" {
421                 send_gdb "n\n"
422
423                 # Eat the prompt
424                 gdb_expect {
425                     -re "$gdb_prompt " {
426                         pass "$test (eat prompt)"
427                     }
428                     timeout {
429                         fail "(timeout) $test (eat prompt)"
430                     }
431                 }
432
433                 gdb_test_multiple "" "$test" {
434                     -re "No symbol \"no_var_named_this\" in current context\\..*$gdb_prompt $" {
435                         pass "$test"
436                     }
437                 }
438             }
439         }
440     }
441 }
442
443 set test "complete (2) 'p no_var_named_this-'"
444 send_gdb "p no_var_named_this-\t"
445 gdb_test_multiple "" "$test" {
446     -re "^p no_var_named_this-\\\x07$" {
447         send_gdb "\t"
448         gdb_test_multiple "" "$test" {
449             -re "(There are $decimal possibilities\\.  Do you really\r\nwish to see them all.|Display all $decimal possibilities.) \\(y or n\\)$" {
450                 send_gdb "n\n"
451
452                 # Eat the prompt
453                 gdb_expect {
454                     -re "$gdb_prompt " {
455                         pass "$test (eat prompt)"
456                     }
457                     timeout {
458                         fail "(timeout) $test (eat prompt)"
459                     }
460                 }
461
462                 gdb_test_multiple "" "$test" {
463                     -re "No symbol \"no_var_named_this\" in current context\\..*$gdb_prompt $" {
464                         pass "$test"
465                     }
466                 }
467             }
468             -re "argv.*$gdb_prompt $" {
469                 send_gdb "\n"
470                 gdb_test_multiple "" "$test" {
471                     -re "No symbol \"no_var_named_this\" in current context\\..*$gdb_prompt $" {
472                         pass "$test"
473                     }
474                 }
475             }
476         }
477     }
478 }
479
480 set test "complete 'p values\[0\].a'"
481 send_gdb "p values\[0\].a\t"
482 gdb_test_multiple "" "$test" {
483     -re "^p values.0..a_field $" {
484         send_gdb "\n"
485         gdb_test_multiple "" "$test" {
486             -re " = 0.*$gdb_prompt $" {
487                 pass "$test"
488             }
489         }
490     }
491 }
492
493 set test "complete 'p values\[0\] . a'"
494 send_gdb "p values\[0\] . a\t"
495 gdb_test_multiple "" "$test" {
496     -re "^p values.0. . a_field $" {
497         send_gdb "\n"
498         gdb_test_multiple "" "$test" {
499             -re " = 0.*$gdb_prompt $" {
500                 pass "$test"
501             }
502         }
503     }
504 }
505
506 set test "complete 'p &values\[0\] -> a'"
507 send_gdb "p &values\[0\] -> a\t"
508 gdb_test_multiple "" "$test" {
509     -re "^p &values.0. -> a_field $" {
510         send_gdb "\n"
511         gdb_test_multiple "" "$test" {
512             -re " = .*0x\[0-9a-fA-F\]*.*$gdb_prompt $" {
513                 pass "$test"
514             }
515         }
516     }
517 }
518
519 gdb_test "complete p &values\[0\]->z" \
520     "p &values.0.->z_field" \
521     "completion of field in anonymous union"
522
523 # The following tests used to simply try to complete `${objdir}/file',
524 # and so on.  The problem is that ${objdir} can be very long; the
525 # completed filename may be more than eighty characters wide.  When
526 # this happens, readline tries to manage things, producing output that
527 # may make sense on the screen, but is rather hard for our script to
528 # recognize.
529 #
530 # In the case that motivated this change, the (gdb) prompt occupied
531 # the leftmost six columns, and `${objdir}/' was seventy-four
532 # characters long --- eighty in all.  After printing the slash,
533 # readline emitted a space, a carriage return, and then `Makefile'
534 # (the tab character being received as input after `Make'.
535 #
536 # Basically, you have to let readline do whatever it's going to do to
537 # make the screen look right.  If it happens to use a different
538 # strategy on Tuesdays to get the cursor in the right place, that's
539 # not something the testsuite should care about.
540 #
541 # So, we avoid long lines.  We `cd' to ${objdir} first, and then do
542 # the completion relative to the current directory.
543
544 # ${srcdir} may be a relative path.  We want to make sure we end up
545 # in the right directory - so make sure we know where it is.
546 set mydir [pwd]
547 cd ${srcdir}
548 set fullsrcdir [pwd]
549 cd ${mydir}
550
551 # If the directory name contains a '+' we must escape it, adding a backslash.
552 # If not, the test below will fail because it will interpret the '+' as a 
553 # regexp operator. We use string_to_regexp for this purpose.
554
555 gdb_test "cd ${fullsrcdir}" \
556          "Working directory [string_to_regexp ${fullsrcdir}].*" \
557          "cd to \${srcdir}"
558
559
560 # GDB used to fail adding / on directories, on the first try only.
561 set uniquedir ../testsuite/gdb.base/comp-dir
562 set escapeduniquedir [string_to_regexp ${uniquedir}]
563 set uniquesu subdi
564 set uniquesub ${uniquesu}r
565 set escapeuniquesub [string_to_regexp ${uniquesub}]
566 send_gdb "dir ${uniquedir}\t"
567 gdb_expect {
568         -re "${escapeduniquedir}/" {
569             pass "directory completion"
570             send_gdb "${uniquesu}\t"
571         }
572         -re "${escapeduniquedir} $" {
573             fail "directory completion (old gdb bug)"
574             send_gdb "\b/${uniquesu}\t"
575         }
576         default {
577             fail "directory completion (timeout)"
578             send_gdb "\ndir ${uniquedir}/${uniquesu}\t"
579         }
580 }
581
582 gdb_expect {
583         -re "${escapeuniquesub}/$" {
584             pass "directory completion 2"
585         }
586         timeout {
587             fail "directory completion 2"
588         }
589 }
590
591 # Empty COMMAND sends no newline while " " sends the newline we need.
592 gdb_test " " "Source directories searched: .*" "Glob remaining of directory test"
593
594 gdb_test "complete file ./gdb.base/compl" \
595     "file ./gdb.base/completion\\.exp.*" \
596     "complete-command 'file ./gdb.base/compl'"
597
598 set test "complete 'file ./gdb.base/complet'"
599 send_gdb "file ./gdb.base/complet\t"
600 gdb_test_multiple "" "$test" {
601     -re "^file ./gdb.base/completion\\.exp $" {
602         send_gdb "\n"
603         # Ignore the exact error message.
604         gdb_test_multiple "" "complete 'file ./gdb.base/complet'" {
605             -re "\r\nA program is being debugged already\\.\[\r\n\]+Are you sure you want to change the file\\? \\(y or n\\) $" {
606                 send_gdb "n\n"
607                 exp_continue
608             }
609             -re "$gdb_prompt $" {
610                 pass "$test"
611             }
612         }
613     }
614 }
615
616 set test "complete 'info func marke'"
617 send_gdb "info func marke\t"
618 gdb_test_multiple "" "$test" {
619     -re "^info func marke.*r$" {
620         send_gdb "\t\t"
621         gdb_test_multiple "" "$test" {
622             -re "marker1.*$gdb_prompt " {
623                 send_gdb "\n"
624                 gdb_test_multiple "" "$test" {
625                     -re "All functions matching regular expression \"marker\":.*File.*break1.c:\r\nint marker1\\((void|)\\);\r\nint marker2\\(int\\).*marker3\\(char.*char.*\\).*marker4\\(long( int)?\\);.*$gdb_prompt $" {
626                         pass "$test"
627                     }
628                 }
629             }
630         }
631     }
632 }
633
634
635 set test "complete 'set follow-fork-mode'"
636 send_gdb "set follow-fork-mode \t\t"
637 gdb_test_multiple "" "$test" {
638     -re "child.*parent.*$gdb_prompt " {
639         send_gdb "\n"
640         gdb_test_multiple "" "$test" {
641             -re "Requires an argument.*child.*parent.*$gdb_prompt $" {
642                 pass "$test"
643             }
644             -re "Ambiguous item \"\"\\..*$gdb_prompt $" {
645                 pass "$test"
646             }
647         }
648     }
649 }
650
651 gdb_test_no_output "complete print values\[0\].x." \
652     "field completion with invalid field"
653
654 # If there is a non-deprecated completion, it should be returned.
655 gdb_test "complete sav" "save" "test non-deprecated completion"
656 # If there is only a deprecated completion, then it should be returned.
657 gdb_test "complete save-t" "save-tracepoints" "test deprecated completion"
658
659
660 # Restore globals modified in this test...
661 set timeout $oldtimeout1
662
663 return 0