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