2012-01-16 Pedro Alves <palves@redhat.com>
[external/binutils.git] / gdb / testsuite / gdb.base / list.exp
1 # Copyright 1992, 1994-1999, 2002-2003, 2007-2012 Free Software
2 # 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 Fred Fish. (fnf@cygnus.com)
18
19
20 set testfile "list"
21 set binfile ${objdir}/${subdir}/${testfile}
22
23 # Need to download the header to the host.
24 remote_download host ${srcdir}/${subdir}/list0.h list0.h
25
26
27 if  { [gdb_compile "${srcdir}/${subdir}/list0.c" "${binfile}0.o" object {debug}] != "" } {
28      untested list.exp
29      return -1
30 }
31
32 if  { [gdb_compile "${srcdir}/${subdir}/list1.c" "${binfile}1.o" object {debug}] != "" } {
33      untested list.exp
34      return -1
35 }
36
37 if  { [gdb_compile "${binfile}0.o ${binfile}1.o" ${binfile} executable {debug}] != "" } {
38      untested list.exp
39      return -1
40 }
41
42
43
44 # Create and source the file that provides information about the compiler
45 # used to compile the test case.
46 if [get_compiler_info ${binfile}] {
47     return -1;
48 }
49
50 #
51 # Local utility proc just to set and verify listsize
52 # Return 1 if success, 0 if fail.
53 #
54
55 set set_listsize_count 0;
56
57 proc set_listsize { arg } {
58     global gdb_prompt
59     global set_listsize_count;
60
61     incr set_listsize_count;
62     if [gdb_test "set listsize $arg" ".*" "setting listsize to $arg #$set_listsize_count"] {
63         return 0;
64     }
65     if { $arg <= 0 } {
66         set arg "unlimited";
67     }
68
69     if [gdb_test "show listsize" "Number of source lines.* is ${arg}.*" "show listsize $arg #$set_listsize_count"] {
70         return 0;
71     }
72     return 1
73 }
74
75 #
76 # Test display of listsize lines around a given line number.
77 #
78
79 proc test_listsize {} {
80     global gdb_prompt use_gdb_stub
81     global hp_cc_compiler
82     global hp_aCC_compiler
83
84     # Show default size
85
86     gdb_test "show listsize" "Number of source lines gdb will list by default is 10.*" "show default list size"
87     
88     # Show the default lines
89     # Note that remote targets that have debugging info for _start available will
90     # list the lines there instead of main, so we skip this test for remote targets.
91     # The second case is for optimized code, it is still correct.
92     
93     if $use_gdb_stub {
94         runto_main;
95         unsupported "list default lines around main";
96     } else {
97         gdb_test "list" "(1\[ \t\]+#include \"list0.h\".*7\[ \t\]+x = 0;\r\n.*10\[ \t\]+foo .x\[+)\]+;)" "list default lines around main"
98     }
99
100     # Ensure we can limit printouts to one line
101
102     if [set_listsize 1] {
103         gdb_test "list 1" "1\[ \t\]+#include \"list0.h\"" "list line 1 with listsize 1"
104         gdb_test "list 2" "2\[ \t\]+" "list line 2 with listsize 1"
105     }    
106
107     # Try just two lines
108     
109     if [ set_listsize 2 ] {
110         gdb_test "list 1" "1\[ \t\]+#include \"list0.h\"\r\n2\[ \t\]+" "list line 1 with listsize 2"
111         gdb_test "list 2" "1\[ \t\]+#include \"list0.h\"\r\n2\[ \t\]+" "list line 2 with listsize 2"
112         gdb_test "list 3" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+" "list line 3 with listsize 2"
113     }
114
115     # Try small listsize > 1 that is an odd number
116
117     if [ set_listsize 3 ] {
118         gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*3\[ \t\]+int main \[)(\]+" "list line 1 with listsize 3"
119         gdb_test "list 2" "1\[ \t\]+#include \"list0.h\".*3\[ \t\]+int main \[)(\]+" "list line 2 with listsize 3"
120         gdb_test "list 3" "2\[ \t\]+\r\n3\[ \t\]+int main \[(\]+\[)\]+\r\n4\[ \t\]+\{" "list line 3 with listsize 3"
121     }
122
123     # Try small listsize > 2 that is an even number.
124
125     if [ set_listsize 4 ] then {
126         gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 1 with listsize 4"
127         gdb_test "list 2" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 2 with listsize 4"
128         
129         gdb_test "list 3" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 3 with listsize 4"
130         gdb_test "list 4" "2\[ \t\]+\r\n.*5\[ \t\]+int x;.*" "list line 4 with listsize 4"
131     }
132
133     # Try a size larger than the entire file.
134
135     if [ set_listsize 100 ] then {
136         gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*\r\n4\[23\]\[ \t\]+\}" "list line 1 with listsize 100"
137         
138         gdb_test "list 10" "1\[ \t\]+#include \"list0.h\".*\r\n4\[23\]\[ \t\]+\}" "list line 10 with listsize 100"
139     }
140
141     # Try listsize of 0 which suppresses printing.
142
143     set_listsize 0
144     gdb_test "list 1" "" "listsize of 0 suppresses output"
145
146     # Try listsize of -1 which is special, and means unlimited.
147
148     set_listsize -1
149     setup_xfail "*-*-*"
150     gdb_test "list 1" "1\[ \t\]+#include .*\r\n39\[ \t\]+\}" "list line 1 with unlimited listsize"
151 }
152
153 #
154 # Test "list filename:number" for C include file
155 #
156
157 proc test_list_include_file {} {
158     global gdb_prompt
159
160     setup_xfail_format "COFF"
161     gdb_test "list list0.h:1" "1\[ \t\]+/\[*\]+ An include file .*10\[ \t\]+bar \\(x\\+\\+\\);" "list line 1 in include file"
162
163     setup_xfail_format "COFF"
164     gdb_test "list list0.h:100" "Line number 95 out of range; .*list0.h has 3\[67\] lines." "list message for lines past EOF"
165 }
166
167 #
168 # Test "list filename:number" for C source file
169 #
170
171 proc test_list_filename_and_number {} {
172     global gdb_prompt
173
174     set testcnt 0
175
176     send_gdb "list list0.c:1\n"
177     gdb_expect {
178         -re "1\[ \t\]+#include \"list0.h\".*10\[ \t\]+foo .x\[+)\]+;\r\n$gdb_prompt $" {
179             incr testcnt 
180         }
181         -re ".*$gdb_prompt $" { fail "list list0.c:1" ; gdb_suppress_tests }
182         timeout { fail "list list0.c:1 (timeout)" ; gdb_suppress_tests }
183     }
184     send_gdb "list list0.c:10\n"
185     gdb_expect {
186         -re "5\[ \t\]+int x;.*14\[ \t\]+foo .x\[+)\]+;\r\n$gdb_prompt $" {
187             incr testcnt 
188         }
189         -re ".*$gdb_prompt $" { fail "list list.c:10" ; gdb_suppress_tests }
190         timeout { fail "list list.c:10 (timeout)" ; gdb_suppress_tests }
191     }
192     send_gdb "list list1.c:1\n"
193     gdb_expect {
194         -re "1\[ \t\]+\#include.*4\[ \t\]+.*int oof\[ \t\]*\(.*\);\r\n.*$gdb_prompt $" {
195             incr testcnt 
196         }
197         -re ".*$gdb_prompt $" { fail "list list1.c:1" ; gdb_suppress_tests }
198         timeout { fail "list list1.c:1 (timeout)" ; gdb_suppress_tests }
199     }
200     send_gdb "list list1.c:12\n"
201     gdb_expect {
202         -re "12\[ \t\]+long_line \[(\]+.*\[)\]+;.*13\[ \t\]+\}\r\n.*$gdb_prompt $" {
203             incr testcnt 
204         }
205         -re ".*$gdb_prompt $" { fail "list list1.c:12" ; gdb_suppress_tests }
206         timeout { fail "list list1.c:12 (timeout)" ; gdb_suppress_tests }
207     }
208     pass "list filename:number ($testcnt tests)"
209     gdb_stop_suppressing_tests;
210 }
211
212 #
213 # Test "list function" for C source file
214 #
215
216 proc test_list_function {} {
217     global gdb_prompt
218
219     # gcc appears to generate incorrect debugging information for code
220     # in include files, which breaks this test.
221     # SunPRO cc is the second case below, it's also correct.
222     gdb_test "list main" "(5\[ \t\]+int x;.*8\[ \t\]+foo \[(\]+.*\[)\]+;|1\[ \t\]+#include .*7\[ \t\]+x = 0;)" "list function in source file 1"
223
224     # Ultrix gdb takes the second case below; it's also correct.
225     # SunPRO cc is the third case.
226     gdb_test "list bar" "(4\[ \t\]+void.*\[ \t\]*long_line.*;.*bar.*9\[ \t\]*.*|1\[ \t\]+void.*8\[ \t\]+\}|1\[ \t\]+void.*7\[ \t\]*long_line ..;|7\[ \t\]+void.*14\[ \t\]+\})" "list function in source file 2"
227
228     # Test "list function" for C include file
229     # Ultrix gdb is the second case, still correct.
230     # SunPRO cc is the third case.
231     gdb_test "list foo" "(3\[ \t\]+.*12\[ \t\]+bar \[(\]+.*\[)\]+;|2\[ \t\]+including file.*11\[ \t\]+bar \[(\]+.*\[)\]+;|1\[ \t\]+/. An include file.*10\[ \t\]+bar \[(\]+.*\[)\]+;)" "list function in include file"
232 }
233
234 proc test_list_forward {} {
235     global gdb_prompt
236
237     set testcnt 0
238
239     send_gdb "list list0.c:10\n"
240     gdb_expect {
241         -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
242         -re ".*$gdb_prompt $" { fail "list list0.c:10" ; gdb_suppress_tests }
243         timeout { fail "list list0.c:10 (timeout)" ; gdb_suppress_tests }
244     }
245
246     send_gdb "list\n"
247     gdb_expect {
248         -re "15\[ \t\]+foo \[(\]+.*\[)\]+;.*24\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
249         -re ".*$gdb_prompt $" { fail "list 15-24" ; gdb_suppress_tests }
250         timeout { fail "list 15-24 (timeout)" ; gdb_suppress_tests }
251     }
252
253     send_gdb "list\n"
254     gdb_expect {
255         -re "25\[ \t\]+foo \[(\]+.*\[)\]+;.*34\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
256         -re ".*$gdb_prompt $" { fail "list 25-34" ; gdb_suppress_tests }
257         timeout { fail "list 25-34 (timeout)" ; gdb_suppress_tests }
258     }
259
260     send_gdb "list\n"
261     gdb_expect {
262         -re "35\[ \t\]+foo \\(.*\\);.*42\[ \t\]+.*\}\r\n$gdb_prompt $" { incr testcnt }
263         -re ".*$gdb_prompt $" { fail "list 35-42" ; gdb_suppress_tests }
264         timeout { fail "list 35-42 (timeout)" ; gdb_suppress_tests }
265     }
266
267     pass "successive list commands to page forward ($testcnt tests)"
268     gdb_stop_suppressing_tests;
269 }
270
271 # Test that repeating the list linenum command doesn't print the same
272 # lines over again.  Note that this test makes sure that the argument
273 # linenum is dropped, when we repeat the previous command. 'x/5i $pc'
274 # works the same way.  
275
276 proc test_repeat_list_command {} {
277     global gdb_prompt
278
279     set testcnt 0
280
281     send_gdb "list list0.c:10\n"
282     gdb_expect {
283         -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
284         -re ".*$gdb_prompt $" { fail "list list0.c:10" ; gdb_suppress_tests }
285         timeout { fail "list list0.c:10 (timeout)" ; gdb_suppress_tests }
286     }
287
288     send_gdb "\n"
289     gdb_expect {
290         -re "15\[ \t\]+foo \[(\]+.*\[)\]+;.*24\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
291         -re ".*$gdb_prompt $" { fail "list 15-24" ; gdb_suppress_tests }
292         timeout { fail "list 15-24 (timeout)" ; gdb_suppress_tests }
293     }
294
295     send_gdb "\n"
296     gdb_expect {
297         -re "25\[ \t\]+foo \[(\]+.*\[)\]+;.*34\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
298         -re ".*$gdb_prompt $" { fail "list 25-34" ; gdb_suppress_tests }
299         timeout { fail "list 25-34 (timeout)" ; gdb_suppress_tests }
300     }
301
302     send_gdb "\n"
303     gdb_expect {
304         -re "35\[ \t\]+foo \\(.*\\);.*42\[ \t\]+.*\}\r\n$gdb_prompt $" { incr testcnt }
305         -re ".*$gdb_prompt $" { fail "list 35-42" ; gdb_suppress_tests }
306         timeout { fail "list 35-42 (timeout)" ; gdb_suppress_tests }
307     }
308
309     pass "repeat list commands to page forward using 'return' ($testcnt tests)"
310     gdb_stop_suppressing_tests;
311 }
312
313 proc test_list_backwards {} {
314     global gdb_prompt
315
316     set testcnt 0
317
318     send_gdb "list list0.c:33\n"
319     gdb_expect {
320         -re "28\[ \t\]+foo \\(.*\\);.*37\[ \t\]+\}\r\n$gdb_prompt $" { incr testcnt }
321         -re ".*$gdb_prompt $" { fail "list list0.c:33" ; gdb_suppress_tests }
322         timeout { fail "list list0.c:33 (timeout)" ; gdb_suppress_tests }
323     }
324
325     send_gdb "list -\n"
326     gdb_expect {
327         -re "18\[ \t\]+foo \[(\]+.*\[)\]+;.*27\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
328         -re ".*$gdb_prompt $" { fail "list 18-27" ; gdb_suppress_tests }
329         timeout { fail "list 18-27 (timeout)" ; gdb_suppress_tests }
330     }
331
332     send_gdb "list -\n"
333     gdb_expect {
334         -re "8\[ \t\]+foo \[(\]+.*\[)\]+;.*17\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
335         -re ".*$gdb_prompt $" { fail "list 8-17" ; gdb_suppress_tests }
336         timeout { fail "list 8-17 (timeout)" ; gdb_suppress_tests }
337     }
338
339     send_gdb "list -\n"
340     gdb_expect {
341         -re "1\[ \t\]+#include .*7\[ \t\]+x = 0;\r\n$gdb_prompt $" { incr testcnt }
342         -re ".*$gdb_prompt $" { fail "list 1-7" ; gdb_suppress_tests }
343         timeout { fail "list 1-7 (timeout)" ; gdb_suppress_tests }
344     }
345
346     pass "$testcnt successive \"list -\" commands to page backwards"
347     gdb_stop_suppressing_tests;
348 }
349
350 #
351 # Test "list first,last"
352 #
353
354 proc test_list_range {} {
355     global gdb_prompt
356
357     gdb_test "list list0.c:2,list0.c:5" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+.*5\[ \t\]+int x;" "list range; filename:line1,filename:line2"
358
359     gdb_test "list 2,5" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+.*5\[ \t\]+int x;" "list range; line1,line2"
360
361 #    gdb_test     "list -1,6"   "Line number 0 out of range; .*list0.c has 39 lines." "list range; lower bound negative"
362
363 #    gdb_test     "list -100,-40"       "Line number -60 out of range; .*list0.c has 39 lines." "list range; both bounds negative"
364
365     gdb_test "list 30,45" "30\[ \t\]+foo \(.*\);.*43\[ \t\]+\}" "list range; upper bound past EOF"
366
367     gdb_test "list 45,100" "Line number 45 out of range; .*list0.c has 43 lines." "list range; both bounds past EOF"
368
369     gdb_test "list list0.c:2,list1.c:17" "Specified start and end are in different files." "list range, must be same files"
370 }
371
372 #
373 # Test "list filename:function"
374 #
375
376 proc test_list_filename_and_function {} {
377     global gdb_prompt
378
379     set testcnt 0
380
381     # gcc appears to generate incorrect debugging information for code
382     # in include files, which breaks this test.
383     # SunPRO cc is the second case below, it's also correct.
384     send_gdb "list list0.c:main\n"
385     gdb_expect {
386         -re "1\[ \t\]+#include .*10\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
387             incr testcnt
388         }
389         -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
390             pass "list function in source file 1"
391         }
392         -re ".*$gdb_prompt $" { fail "list list0.c:main" }
393         timeout { fail "list list0.c:main (timeout)" }
394     }
395
396     # Not sure what the point of having this function be unused is.
397     # AIX is legitimately removing it.
398     setup_xfail "rs6000-*-aix*"
399     send_gdb "list list0.c:unused\n"
400     gdb_expect {
401         -re "40\[ \t\]+unused.*43\[ \t\]+\}\r\n$gdb_prompt $" {
402             incr testcnt
403         }
404         -re "37.*42\[ \t\]+\}\r\n$gdb_prompt $" {
405             incr testcnt
406         }
407         -re ".*$gdb_prompt $" { fail "list list0.c:unused" }
408         timeout { fail "list list0.c:unused (timeout)" }
409     }
410     clear_xfail "rs6000-*-aix*"
411
412     # gcc appears to generate incorrect debugging information for code
413     # in include files, which breaks this test.
414     # Ultrix gdb is the second case, one line different but still correct.
415     # SunPRO cc is the third case.
416     setup_xfail "rs6000-*-*" 1804
417     setup_xfail_format "COFF"
418     send_gdb "list list0.h:foo\n"
419     gdb_expect {
420         -re "2\[ \t\]+including file.  This.*11\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
421             incr testcnt
422         }
423         -re "1\[ \t\]+/. An include file.*10\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
424             incr testcnt
425         }
426         -re "3\[ \t\]+.*12\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
427             incr testcnt
428         }
429         -re "No source file named list0.h.\r\n$gdb_prompt $" {
430             fail "list list0.h:foo"
431         }
432         -re ".*$gdb_prompt $" { fail "list list0.h:foo" }
433         timeout { fail "list list0.h:foo (timeout)" }
434     }
435
436     # Ultrix gdb is the second case.
437     send_gdb "list list1.c:bar\n"
438     gdb_expect {
439         -re "4\[ \t\]+void.*13\[ \t\]+\}\r\n$gdb_prompt $" {
440             incr testcnt
441         }
442         -re "4\[ \t\]+void.*12\[ \t\]*long_line ..;\r\n$gdb_prompt $" {
443             incr testcnt
444         }
445         -re "4\[ \t\]+void.*11\[ \t\]*\r\n$gdb_prompt $" {
446             incr testcnt
447         }
448         -re ".*$gdb_prompt $" { fail "list list1.c:bar" }
449         timeout { fail "list list1.c:bar (timeout)" }
450     }
451
452     # Not sure what the point of having this function be unused is.
453     # AIX is legitimately removing it.
454     setup_xfail "rs6000-*-aix*"
455     send_gdb "list list1.c:unused\n"
456     gdb_expect {
457         -re "12\[ \t\]+long_line \[(\]\[)\];.*13\[ \t\]+\}\r\n.*$gdb_prompt $" {
458             incr testcnt
459         }
460         -re "14.*19\[ \t\]+\}\r\n.*$gdb_prompt $" {
461             incr testcnt
462         }
463         -re ".*$gdb_prompt $" { fail "list list1.c:unused" }
464         timeout { fail "list list1.c:unused (timeout)" }
465     }
466     clear_xfail "rs6000-*-aix*"
467
468     pass "list filename:function ($testcnt tests)"
469
470     # Test with quoting.
471     gdb_test "list 'list0.c:main'" "int main.*"
472
473     # Test some invalid specs
474     # The following test takes the FIXME result on most systems using
475     # DWARF.  It fails to notice that main() is not in the file requested.
476    
477     setup_xfail "*-*-*"
478
479 # Does this actually work ANYWHERE?  I believe not, as this is an `aspect' of
480 # lookup_symbol(), where, when it is given a specific symtab which does not
481 # contain the requested symbol, it will subsequently search all of the symtabs
482 # for the requested symbol.
483
484     gdb_test "list list0.c:foo" "Function \"foo\" not defined in .*list0.c" "list filename:function; wrong filename rejected"
485
486     gdb_test "list foobar.c:main" "No source file named foobar.c.|Location not found" "list filename:function; nonexistant file"
487
488     gdb_test "list list0.h:foobar" "Function \"foobar\" not defined in \"list0.h\"." "list filename:function; nonexistant function"
489
490 }
491
492 proc test_forward_search {} {
493         global timeout
494
495         gdb_test_no_output "set listsize 4"
496         # On SunOS4, this gives us lines 19-22.  On AIX, it gives us
497         # lines 20-23.  This depends on whether the line number of a function
498         # is considered to be the openbrace or the first statement--either one
499         # is acceptable.
500         gdb_test "list long_line" "24\[ \t\]+long_line .*"
501
502         gdb_test "search 4321" " not found"
503
504         gdb_test "search 6789" "28\[ \t\]+oof .6789.;"
505
506         # Test that GDB won't crash if the line being searched is extremely long.
507
508         set oldtimeout $timeout
509         set timeout [expr "$timeout + 300"]
510         verbose "Timeout is now $timeout seconds" 2
511         gdb_test "search 1234" ".*1234.*" "search extremely long line (> 5000 chars)"
512         set timeout $oldtimeout
513         verbose "Timeout is now $timeout seconds" 2
514 }
515
516 # Start with a fresh gdb.
517
518 gdb_exit
519 gdb_start
520 gdb_reinitialize_dir $srcdir/$subdir
521 gdb_load ${binfile}
522
523 gdb_test_no_output "set width 0"
524
525 test_listsize
526 get_debug_format
527 if [ set_listsize 10 ] then {
528     test_list_include_file
529     test_list_filename_and_number
530     test_list_function
531     test_list_forward
532     test_list_backwards
533     test_repeat_list_command
534     test_list_range
535     test_list_filename_and_function
536     test_forward_search
537 }
538
539 remote_exec build "rm -f list0.h"