1 # Copyright 1992-2014 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file was written by Fred Fish. (fnf@cygnus.com)
19 standard_testfile list0.c list1.c
21 # Need to download the header to the host.
22 gdb_remote_download host ${srcdir}/${subdir}/list0.h
24 if {[prepare_for_testing $testfile.exp $testfile [list $srcfile $srcfile2] \
29 # Create and source the file that provides information about the compiler
30 # used to compile the test case.
31 if [get_compiler_info] {
35 # The last line in the file.
36 set last_line [gdb_get_line_number "last line" "list0.c"]
38 # Regex matching the last line in the file.
39 set last_line_re "${last_line}\[ \t\]+} /\\* last line \\*/"
42 # Local utility proc just to set and verify listsize
43 # Return 1 if success, 0 if fail.
46 set set_listsize_count 0
48 proc set_listsize { arg } {
50 global set_listsize_count
52 incr set_listsize_count
53 if [gdb_test_no_output "set listsize $arg" "setting listsize to $arg #$set_listsize_count"] {
60 if [gdb_test "show listsize" "Number of source lines.* is ${arg}.*" "show listsize $arg #$set_listsize_count"] {
67 # Test display of listsize lines around a given line number.
70 proc test_listsize {} {
73 global hp_aCC_compiler
78 gdb_test "show listsize" "Number of source lines gdb will list by default is 10.*" "show default list size"
80 # Show the default lines
82 gdb_test "list" "(1\[ \t\]+#include \"list0.h\".*7\[ \t\]+x = 0;\r\n.*10\[ \t\]+foo .x\[+)\]+;)" "list default lines around main"
84 # Ensure we can limit printouts to one line
87 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\"" "list line 1 with listsize 1"
88 gdb_test "list 2" "2\[ \t\]+" "list line 2 with listsize 1"
93 if [ set_listsize 2 ] {
94 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\"\r\n2\[ \t\]+" "list line 1 with listsize 2"
95 gdb_test "list 2" "1\[ \t\]+#include \"list0.h\"\r\n2\[ \t\]+" "list line 2 with listsize 2"
96 gdb_test "list 3" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+" "list line 3 with listsize 2"
99 # Try small listsize > 1 that is an odd number
101 if [ set_listsize 3 ] {
102 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*3\[ \t\]+int main \[)(\]+" "list line 1 with listsize 3"
103 gdb_test "list 2" "1\[ \t\]+#include \"list0.h\".*3\[ \t\]+int main \[)(\]+" "list line 2 with listsize 3"
104 gdb_test "list 3" "2\[ \t\]+\r\n3\[ \t\]+int main \[(\]+\[)\]+\r\n4\[ \t\]+\{" "list line 3 with listsize 3"
107 # Try small listsize > 2 that is an even number.
109 if [ set_listsize 4 ] then {
110 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 1 with listsize 4"
111 gdb_test "list 2" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 2 with listsize 4"
113 gdb_test "list 3" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 3 with listsize 4"
114 gdb_test "list 4" "2\[ \t\]+\r\n.*5\[ \t\]+int x;.*" "list line 4 with listsize 4"
117 # Try a size larger than the entire file.
119 if [ set_listsize 100 ] then {
120 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*\r\n${last_line_re}" "list line 1 with listsize 100"
122 gdb_test "list 10" "1\[ \t\]+#include \"list0.h\".*\r\n${last_line_re}" "list line 10 with listsize 100"
125 # Try listsize of 0 which is special, and means unlimited.
128 gdb_test "list 1" "1\[ \t\]+#include .*\r\n${last_line_re}" "list line 1 with unlimited listsize"
132 # Test "list filename:number" for C include file
135 proc test_list_include_file {} {
138 setup_xfail_format "COFF"
139 gdb_test "list list0.h:1" "1\[ \t\]+/\[*\]+ An include file .*10\[ \t\]+bar \\(x\\+\\+\\);" "list line 1 in include file"
141 setup_xfail_format "COFF"
142 gdb_test "list list0.h:100" "Line number 95 out of range; .*list0.h has 3\[67\] lines." "list message for lines past EOF"
146 # Test "list filename:number" for C source file
149 proc test_list_filename_and_number {} {
154 send_gdb "list list0.c:1\n"
156 -re "1\[ \t\]+#include \"list0.h\".*10\[ \t\]+foo .x\[+)\]+;\r\n$gdb_prompt $" {
159 -re ".*$gdb_prompt $" { fail "list list0.c:1" ; gdb_suppress_tests }
160 timeout { fail "list list0.c:1 (timeout)" ; gdb_suppress_tests }
162 send_gdb "list list0.c:10\n"
164 -re "5\[ \t\]+int x;.*14\[ \t\]+foo .x\[+)\]+;\r\n$gdb_prompt $" {
167 -re ".*$gdb_prompt $" { fail "list list.c:10" ; gdb_suppress_tests }
168 timeout { fail "list list.c:10 (timeout)" ; gdb_suppress_tests }
170 send_gdb "list list1.c:1\n"
172 -re "1\[ \t\]+\#include.*4\[ \t\]+.*int oof\[ \t\]*\(.*\);\r\n.*$gdb_prompt $" {
175 -re ".*$gdb_prompt $" { fail "list list1.c:1" ; gdb_suppress_tests }
176 timeout { fail "list list1.c:1 (timeout)" ; gdb_suppress_tests }
178 send_gdb "list list1.c:12\n"
180 -re "12\[ \t\]+long_line \[(\]+.*\[)\]+;.*13\[ \t\]+\}\r\n.*$gdb_prompt $" {
183 -re ".*$gdb_prompt $" { fail "list list1.c:12" ; gdb_suppress_tests }
184 timeout { fail "list list1.c:12 (timeout)" ; gdb_suppress_tests }
186 pass "list filename:number ($testcnt tests)"
187 gdb_stop_suppressing_tests
191 # Test "list function" for C source file
194 proc test_list_function {} {
197 # gcc appears to generate incorrect debugging information for code
198 # in include files, which breaks this test.
199 # SunPRO cc is the second case below, it's also correct.
200 gdb_test "list main" "(5\[ \t\]+int x;.*8\[ \t\]+foo \[(\]+.*\[)\]+;|1\[ \t\]+#include .*7\[ \t\]+x = 0;)" "list function in source file 1"
202 # Ultrix gdb takes the second case below; it's also correct.
203 # SunPRO cc is the third case.
204 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"
206 # Test "list function" for C include file
207 # Ultrix gdb is the second case, still correct.
208 # SunPRO cc is the third case.
209 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"
212 proc test_list_forward {} {
218 send_gdb "list list0.c:10\n"
220 -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
221 -re ".*$gdb_prompt $" { fail "list list0.c:10" ; gdb_suppress_tests }
222 timeout { fail "list list0.c:10 (timeout)" ; gdb_suppress_tests }
227 -re "15\[ \t\]+foo \[(\]+.*\[)\]+;.*24\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
228 -re ".*$gdb_prompt $" { fail "list 15-24" ; gdb_suppress_tests }
229 timeout { fail "list 15-24 (timeout)" ; gdb_suppress_tests }
234 -re "25\[ \t\]+foo \[(\]+.*\[)\]+;.*34\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
235 -re ".*$gdb_prompt $" { fail "list 25-34" ; gdb_suppress_tests }
236 timeout { fail "list 25-34 (timeout)" ; gdb_suppress_tests }
241 -re "35\[ \t\]+foo \\(.*\\);.*${last_line_re}\r\n$gdb_prompt $" { incr testcnt }
242 -re ".*$gdb_prompt $" { fail "list 35-42" ; gdb_suppress_tests }
243 timeout { fail "list 35-42 (timeout)" ; gdb_suppress_tests }
246 pass "successive list commands to page forward ($testcnt tests)"
247 gdb_stop_suppressing_tests
250 # Test that repeating the list linenum command doesn't print the same
251 # lines over again. Note that this test makes sure that the argument
252 # linenum is dropped, when we repeat the previous command. 'x/5i $pc'
253 # works the same way.
255 proc test_repeat_list_command {} {
261 send_gdb "list list0.c:10\n"
263 -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
264 -re ".*$gdb_prompt $" { fail "list list0.c:10" ; gdb_suppress_tests }
265 timeout { fail "list list0.c:10 (timeout)" ; gdb_suppress_tests }
270 -re "15\[ \t\]+foo \[(\]+.*\[)\]+;.*24\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
271 -re ".*$gdb_prompt $" { fail "list 15-24" ; gdb_suppress_tests }
272 timeout { fail "list 15-24 (timeout)" ; gdb_suppress_tests }
277 -re "25\[ \t\]+foo \[(\]+.*\[)\]+;.*34\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
278 -re ".*$gdb_prompt $" { fail "list 25-34" ; gdb_suppress_tests }
279 timeout { fail "list 25-34 (timeout)" ; gdb_suppress_tests }
284 -re "35\[ \t\]+foo \\(.*\\);.*${last_line_re}\r\n$gdb_prompt $" { incr testcnt }
285 -re ".*$gdb_prompt $" { fail "list 35-42" ; gdb_suppress_tests }
286 timeout { fail "list 35-42 (timeout)" ; gdb_suppress_tests }
289 pass "repeat list commands to page forward using 'return' ($testcnt tests)"
290 gdb_stop_suppressing_tests
293 proc test_list_backwards {} {
298 send_gdb "list list0.c:33\n"
300 -re "28\[ \t\]+foo \\(.*\\);.*37\[ \t\]+\}\r\n$gdb_prompt $" { incr testcnt }
301 -re ".*$gdb_prompt $" { fail "list list0.c:33" ; gdb_suppress_tests }
302 timeout { fail "list list0.c:33 (timeout)" ; gdb_suppress_tests }
307 -re "18\[ \t\]+foo \[(\]+.*\[)\]+;.*27\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
308 -re ".*$gdb_prompt $" { fail "list 18-27" ; gdb_suppress_tests }
309 timeout { fail "list 18-27 (timeout)" ; gdb_suppress_tests }
314 -re "8\[ \t\]+foo \[(\]+.*\[)\]+;.*17\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" { incr testcnt }
315 -re ".*$gdb_prompt $" { fail "list 8-17" ; gdb_suppress_tests }
316 timeout { fail "list 8-17 (timeout)" ; gdb_suppress_tests }
321 -re "1\[ \t\]+#include .*7\[ \t\]+x = 0;\r\n$gdb_prompt $" { incr testcnt }
322 -re ".*$gdb_prompt $" { fail "list 1-7" ; gdb_suppress_tests }
323 timeout { fail "list 1-7 (timeout)" ; gdb_suppress_tests }
326 pass "$testcnt successive \"list -\" commands to page backwards"
327 gdb_stop_suppressing_tests
331 # Test "list first,last"
334 proc test_list_range {} {
339 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"
341 gdb_test "list 2,5" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+.*5\[ \t\]+int x;" "list range; line1,line2"
343 # gdb_test "list -1,6" "Line number 0 out of range; .*list0.c has 39 lines." "list range; lower bound negative"
345 # gdb_test "list -100,-40" "Line number -60 out of range; .*list0.c has 39 lines." "list range; both bounds negative"
347 set past_end [expr ${last_line} + 10]
348 set much_past_end [expr ${past_end} + 10]
350 gdb_test "list 30,${past_end}" "30\[ \t\]+foo \(.*\);.*${last_line_re}" "list range; upper bound past EOF"
352 gdb_test "list ${past_end},${much_past_end}" "Line number ${past_end} out of range; .*list0.c has ${last_line} lines." "list range; both bounds past EOF"
354 gdb_test "list list0.c:2,list1.c:17" "Specified start and end are in different files." "list range, must be same files"
358 # Test "list filename:function"
361 proc test_list_filename_and_function {} {
367 # gcc appears to generate incorrect debugging information for code
368 # in include files, which breaks this test.
369 # SunPRO cc is the second case below, it's also correct.
370 send_gdb "list list0.c:main\n"
372 -re "1\[ \t\]+#include .*10\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
375 -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
376 pass "list function in source file 1"
378 -re ".*$gdb_prompt $" { fail "list list0.c:main" }
379 timeout { fail "list list0.c:main (timeout)" }
382 # Not sure what the point of having this function be unused is.
383 # AIX is legitimately removing it.
384 setup_xfail "rs6000-*-aix*"
385 send_gdb "list list0.c:unused\n"
387 -re "40\[ \t\]+unused.*${last_line_re}\r\n$gdb_prompt $" {
390 -re "37.*42\[ \t\]+\}\r\n$gdb_prompt $" {
393 -re ".*$gdb_prompt $" { fail "list list0.c:unused" }
394 timeout { fail "list list0.c:unused (timeout)" }
396 clear_xfail "rs6000-*-aix*"
398 # gcc appears to generate incorrect debugging information for code
399 # in include files, which breaks this test.
400 # Ultrix gdb is the second case, one line different but still correct.
401 # SunPRO cc is the third case.
402 setup_xfail "rs6000-*-*" 1804
403 setup_xfail_format "COFF"
404 send_gdb "list list0.h:foo\n"
406 -re "2\[ \t\]+including file. This.*11\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
409 -re "1\[ \t\]+/. An include file.*10\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
412 -re "3\[ \t\]+.*12\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
415 -re "No source file named list0.h.\r\n$gdb_prompt $" {
416 fail "list list0.h:foo"
418 -re ".*$gdb_prompt $" { fail "list list0.h:foo" }
419 timeout { fail "list list0.h:foo (timeout)" }
422 # Ultrix gdb is the second case.
423 send_gdb "list list1.c:bar\n"
425 -re "4\[ \t\]+void.*13\[ \t\]+\}\r\n$gdb_prompt $" {
428 -re "4\[ \t\]+void.*12\[ \t\]*long_line ..;\r\n$gdb_prompt $" {
431 -re "4\[ \t\]+void.*11\[ \t\]*\r\n$gdb_prompt $" {
434 -re ".*$gdb_prompt $" { fail "list list1.c:bar" }
435 timeout { fail "list list1.c:bar (timeout)" }
438 # Not sure what the point of having this function be unused is.
439 # AIX is legitimately removing it.
440 setup_xfail "rs6000-*-aix*"
441 send_gdb "list list1.c:unused\n"
443 -re "12\[ \t\]+long_line \[(\]\[)\];.*13\[ \t\]+\}\r\n.*$gdb_prompt $" {
446 -re "14.*19\[ \t\]+\}\r\n.*$gdb_prompt $" {
449 -re ".*$gdb_prompt $" { fail "list list1.c:unused" }
450 timeout { fail "list list1.c:unused (timeout)" }
452 clear_xfail "rs6000-*-aix*"
454 pass "list filename:function ($testcnt tests)"
457 gdb_test "list 'list0.c:main'" "int main.*"
459 # Test some invalid specs
460 # The following test takes the FIXME result on most systems using
461 # DWARF. It fails to notice that main() is not in the file requested.
465 # Does this actually work ANYWHERE? I believe not, as this is an `aspect' of
466 # lookup_symbol(), where, when it is given a specific symtab which does not
467 # contain the requested symbol, it will subsequently search all of the symtabs
468 # for the requested symbol.
470 gdb_test "list list0.c:foo" "Function \"foo\" not defined in .*list0.c" "list filename:function; wrong filename rejected"
472 gdb_test "list foobar.c:main" "No source file named foobar.c.|Location not found" "list filename:function; nonexistant file"
474 gdb_test "list list0.h:foobar" "Function \"foobar\" not defined in \"list0.h\"." "list filename:function; nonexistant function"
478 proc test_forward_search {} {
481 gdb_test_no_output "set listsize 4"
482 # On SunOS4, this gives us lines 19-22. On AIX, it gives us
483 # lines 20-23. This depends on whether the line number of a function
484 # is considered to be the openbrace or the first statement--either one
486 gdb_test "list long_line" "24\[ \t\]+long_line .*"
488 gdb_test "search 4321" " not found"
490 gdb_test "search 6789" "28\[ \t\]+oof .6789.;"
492 # Test that GDB won't crash if the line being searched is extremely long.
494 set oldtimeout $timeout
495 set timeout [expr "$timeout + 300"]
496 verbose "Timeout is now $timeout seconds" 2
497 gdb_test "search 1234" ".*1234.*" "search extremely long line (> 5000 chars)"
498 set timeout $oldtimeout
499 verbose "Timeout is now $timeout seconds" 2
502 proc test_only_end {} {
503 gdb_test_no_output "set listsize 2"
504 gdb_test "list 1" "list 1\r\n1\[ \t\]\[^\r\n\]*\r\n2\[ \t\]\[^\r\n\]*"
505 gdb_test "list ,5" "list ,5\r\n4\[ \t\]\[^\r\n\]*\r\n5\[ \t\]\[^\r\n\]*"
508 # Start with a fresh gdb.
512 gdb_reinitialize_dir $srcdir/$subdir
513 gdb_file_cmd ${binfile}
515 gdb_test_no_output "set width 0"
519 if [ set_listsize 10 ] then {
520 test_list_include_file
521 test_list_filename_and_number
525 test_repeat_list_command
527 test_list_filename_and_function
532 # Follows tests that require execution.
534 # Build source listing pattern based on a line range spec string. The
535 # range can be specificed as "START-END" indicating all lines in range
536 # (inclusive); or just "LINE", indicating just that line.
538 proc build_pattern { range_spec } {
541 set range_list [split $range_spec -]
542 set range_list_len [llength $range_list]
544 set range_start [lindex $range_list 0]
545 if { $range_list_len > 2 || $range_list_len < 1} {
546 error "invalid range spec string: $range_spec"
547 } elseif { $range_list_len == 2 } {
548 set range_end [lindex $range_list 1]
550 set range_end $range_start
553 for {set i $range_start} {$i <= $range_end} {incr i} {
554 append pattern "\r\n$i\[ \t\]\[^\r\n\]*"
557 verbose -log "pattern $pattern"
561 # Test "list" command invocations right after stopping for an event.
562 # COMMAND is the actual list command, including arguments. LISTSIZE1
563 # and LISTSIZE2 are the listsizes set just before and after running
564 # the program to the stop point. COMMAND is issued twice. The first
565 # time, the lines specificed by LINERANGE1 are expected; the second
566 # time, the lines specified by LINERANGE2 are expected.
568 proc test_list {command listsize1 listsize2 linerange1 linerange2} {
569 with_test_prefix "$command after stop: $listsize1, $listsize2" {
572 clean_restart $binfile
573 if ![runto_main] then {
574 fail "Can't run to main"
578 # Test changing the listsize both before nexting, and after
579 # stopping, but before listing. Only the second listsize
580 # change should affect which lines are listed.
581 gdb_test_no_output "set listsize $listsize1"
582 gdb_test "next" "foo \\(.*"
583 gdb_test_no_output "set listsize $listsize2"
585 set pattern1 [build_pattern $linerange1]
586 set pattern2 [build_pattern $linerange2]
587 gdb_test "$command" "${pattern1}" "$command #1"
588 gdb_test "$command" "${pattern2}" "$command #2"
593 # The first "list" should center the listing around line 8, the stop
595 test_list "list" 1 10 "3-12" "13-22"
598 test_list "list" 10 10 "3-12" "13-22"
600 # Likewise, but show only one line. IOW, the first list should show
601 # line 8. Note how the listsize is 10 at the time of the stop, but
602 # before any listing had been requested. That should not affect the
603 # line range that is first listed.
604 test_list "list" 10 1 "8" "9"
606 # Likewise, but show two lines.
607 test_list "list" 10 2 "7-8" "9-10"
610 test_list "list" 10 3 "7-9" "10-12"
612 # Now test backwards. Just like "list", the first "list -" should
613 # center the listing around the stop line.
614 test_list "list -" 10 10 "3-12" "2"
616 # Likewise, but test showing 3 lines at a time.
617 test_list "list -" 10 3 "7-9" "4-6"
620 test_list "list -" 10 2 "7-8" "5-6"
622 # Test listing one line only. This case is a little special and
623 # starts showing the previous line immediately instead of repeating
625 test_list "list -" 10 1 "7" "6"
627 remote_exec build "rm -f list0.h"