1 # Copyright 1992-2024 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 {[build_executable "failed to prepare" $testfile [list $srcfile $srcfile2] \
29 # The last line in the file.
30 set last_line [gdb_get_line_number "last line" "list0.c"]
32 # Regex matching the last line in the file.
33 set last_line_re "${last_line}\[ \t\]+} /\\* last line \\*/"
36 # Local utility proc just to set and verify listsize
37 # Return 1 if success, 0 if fail.
40 set set_listsize_count 0
42 proc set_listsize { arg } {
44 global set_listsize_count
46 incr set_listsize_count
47 if [gdb_test_no_output "set listsize $arg" "setting listsize to $arg #$set_listsize_count"] {
54 if [gdb_test "show listsize" "Number of source lines.* is ${arg}.*" "show listsize $arg #$set_listsize_count"] {
61 # Test display of listsize lines around a given line number.
64 proc test_listsize {} {
70 gdb_test "show listsize" "Number of source lines gdb will list by default is 10.*" "show default list size"
72 # Show the default lines
74 gdb_test "list" "(1\[ \t\]+#include \"list0.h\".*7\[ \t\]+x = 0;\r\n.*10\[ \t\]+foo .x\[+)\]+;)" "list default lines around main"
76 # Ensure we can limit printouts to one line
79 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\"" "list line 1 with listsize 1"
80 gdb_test "list 2" "2\[ \t\]+" "list line 2 with listsize 1"
85 if [ set_listsize 2 ] {
86 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\"\r\n2\[ \t\]+" "list line 1 with listsize 2"
87 gdb_test "list 2" "1\[ \t\]+#include \"list0.h\"\r\n2\[ \t\]+" "list line 2 with listsize 2"
88 gdb_test "list 3" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+" "list line 3 with listsize 2"
91 # Try small listsize > 1 that is an odd number
93 if [ set_listsize 3 ] {
94 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*3\[ \t\]+int main \[)(\]+" "list line 1 with listsize 3"
95 gdb_test "list 2" "1\[ \t\]+#include \"list0.h\".*3\[ \t\]+int main \[)(\]+" "list line 2 with listsize 3"
96 gdb_test "list 3" "2\[ \t\]+\r\n3\[ \t\]+int main \[(\]+\[)\]+\r\n4\[ \t\]+\{" "list line 3 with listsize 3"
99 # Try small listsize > 2 that is an even number.
101 if {[set_listsize 4]} {
102 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 1 with listsize 4"
103 gdb_test "list 2" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 2 with listsize 4"
105 gdb_test "list 3" "1\[ \t\]+#include \"list0.h\".*4\[ \t\]+\{" "list line 3 with listsize 4"
106 gdb_test "list 4" "2\[ \t\]+\r\n.*5\[ \t\]+int x;.*" "list line 4 with listsize 4"
109 # Try a size larger than the entire file.
111 if {[set_listsize 100]} {
112 gdb_test "list 1" "1\[ \t\]+#include \"list0.h\".*\r\n${last_line_re}" "list line 1 with listsize 100"
114 gdb_test "list 10" "1\[ \t\]+#include \"list0.h\".*\r\n${last_line_re}" "list line 10 with listsize 100"
117 # Try listsize of 0 which is special, and means unlimited.
120 gdb_test "list 1" "1\[ \t\]+#include .*\r\n${last_line_re}" "list line 1 with unlimited listsize"
124 # Test "list filename:number" for C include file
127 proc test_list_include_file {} {
130 setup_xfail_format "COFF"
131 gdb_test "list list0.h:1" "1\[ \t\]+/\[*\]+ An include file .*10\[ \t\]+bar \\(x\\+\\+\\);" "list line 1 in include file"
133 setup_xfail_format "COFF"
134 gdb_test "list list0.h:100" "Line number 95 out of range; .*list0.h has 3\[67\] lines." "list message for lines past EOF"
138 # Test "list filename:number" for C source file
141 proc_with_prefix test_list_filename_and_number {} {
142 gdb_test "list list0.c:1" "1\[ \t\]+#include \"list0.h\".*10\[ \t\]+foo .x\[+)\]+;"
143 gdb_test "list list0.c:10" "5\[ \t\]+int x;.*14\[ \t\]+foo .x\[+)\]+;"
144 gdb_test "list list1.c:1" "1\[ \t\]+\#include.*4\[ \t\]+.*int oof\[ \t\]*\(.*\);\r\n.*"
145 gdb_test "list list1.c:12" "12\[ \t\]+long_line \[(\]+.*\[)\]+;.*13\[ \t\]+\}\r\n.*"
149 # Test "list function" for C source file
152 proc_with_prefix test_list_function {} {
153 # gcc appears to generate incorrect debugging information for code
154 # in include files, which breaks this test.
155 # SunPRO cc is the second case below, it's also correct.
156 gdb_test "list -q main" "(5\[ \t\]+int x;.*8\[ \t\]+foo \[(\]+.*\[)\]+;|1\[ \t\]+#include .*7\[ \t\]+x = 0;)" "list function in source file 1"
158 # Ultrix gdb takes the second case below; it's also correct.
159 # SunPRO cc is the third case.
160 gdb_test "list -q 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"
162 # Test "list function" for C include file
163 # Ultrix gdb is the second case, still correct.
164 # SunPRO cc is the third case.
165 gdb_test "list -q 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"
168 proc_with_prefix test_list_forward {} {
171 gdb_test "list list0.c:10" "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;"
172 gdb_test "list" "15\[ \t\]+foo \[(\]+.*\[)\]+;.*24\[ \t\]+foo \[(\]+.*\[)\]+;" \
174 gdb_test "list" "25\[ \t\]+foo \[(\]+.*\[)\]+;.*34\[ \t\]+foo \[(\]+.*\[)\]+;" \
176 gdb_test "list" "35\[ \t\]+foo \\(.*\\);.*${last_line_re}" \
178 gdb_test "list" "End of the file was already reached, use \"list .\" to list the current location again" \
179 "list past end of file"
182 # Test that repeating the list linenum command doesn't print the same
183 # lines over again. Note that this test makes sure that the argument
184 # linenum is dropped, when we repeat the previous command. 'x/5i $pc'
185 # works the same way.
187 proc_with_prefix test_repeat_list_command {} {
190 gdb_test "list list0.c:10" "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;"
191 gdb_test " " "15\[ \t\]+foo \[(\]+.*\[)\]+;.*24\[ \t\]+foo \[(\]+.*\[)\]+;" \
193 gdb_test " " "25\[ \t\]+foo \[(\]+.*\[)\]+;.*34\[ \t\]+foo \[(\]+.*\[)\]+;" \
195 gdb_test " " "35\[ \t\]+foo \\(.*\\);.*${last_line_re}" \
197 gdb_test "list" "End of the file was already reached, use \"list .\" to list the current location again" \
198 "list past end of file"
201 proc_with_prefix test_list_backwards {} {
202 gdb_test "list list0.c:33" "28\[ \t\]+foo \\(.*\\);.*37\[ \t\]+\}"
203 gdb_test "list -" "18\[ \t\]+foo \[(\]+.*\[)\]+;.*27\[ \t\]+foo \[(\]+.*\[)\]+;" \
205 gdb_test "list -" "8\[ \t\]+foo \[(\]+.*\[)\]+;.*17\[ \t\]+foo \[(\]+.*\[)\]+;" \
207 gdb_test "list -" "1\[ \t\]+#include .*7\[ \t\]+x = 0;" \
209 gdb_test "list -" "Already at the start of .*\." \
210 "beginning of file error after \"list -\" command"
214 # Test "list first,last"
217 proc test_list_range {} {
222 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"
224 gdb_test "list 2,5" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+.*5\[ \t\]+int x;" "list range; line1,line2"
226 # gdb_test "list -1,6" "Line number 0 out of range; .*list0.c has 39 lines." "list range; lower bound negative"
228 # gdb_test "list -100,-40" "Line number -60 out of range; .*list0.c has 39 lines." "list range; both bounds negative"
230 set past_end [expr ${last_line} + 10]
231 set much_past_end [expr ${past_end} + 10]
233 gdb_test "list 30,${past_end}" "30\[ \t\]+foo \(.*\);.*${last_line_re}" "list range; upper bound past EOF"
235 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"
237 gdb_test "list list0.c:2,list1.c:17" "Specified first and last lines are in different files." "list range, must be same files"
241 # Test "list filename:function"
244 proc test_list_filename_and_function {} {
248 # gcc appears to generate incorrect debugging information for code
249 # in include files, which breaks this test.
250 # SunPRO cc is the second case below, it's also correct.
251 gdb_test_multiple "list list0.c:main" "" {
252 -re -wrap "1\[ \t\]+#include .*10\[ \t\]+foo \[(\]+.*\[)\]+;" {
255 -re -wrap "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;" {
260 # Not sure what the point of having this function be unused is.
261 # AIX is legitimately removing it.
262 setup_xfail "rs6000-*-aix*"
263 gdb_test_multiple "list list0.c:unused" "" {
264 -re -wrap "40\[ \t\]+unused.*${last_line_re}" {
267 -re -wrap "37.*42\[ \t\]+\}" {
271 clear_xfail "rs6000-*-aix*"
273 # gcc appears to generate incorrect debugging information for code
274 # in include files, which breaks this test.
275 # Ultrix gdb is the second case, one line different but still correct.
276 # SunPRO cc is the third case.
277 setup_xfail "rs6000-*-*" 1804
278 setup_xfail_format "COFF"
279 gdb_test_multiple "list list0.h:foo" "" {
280 -re -wrap "2\[ \t\]+including file. This.*11\[ \t\]+bar \[(\]+.*\[)\]+;" {
283 -re -wrap "1\[ \t\]+/. An include file.*10\[ \t\]+bar \[(\]+.*\[)\]+;" {
286 -re -wrap "3\[ \t\]+.*12\[ \t\]+bar \[(\]+.*\[)\]+;" {
289 -re -wrap "No source file named list0.h." {
294 # Ultrix gdb is the second case.
295 gdb_test_multiple "list list1.c:bar" "" {
296 -re -wrap "4\[ \t\]+void.*13\[ \t\]+\}" {
299 -re -wrap "4\[ \t\]+void.*12\[ \t\]*long_line ..;" {
302 -re -wrap "4\[ \t\]+void.*11\[ \t\]*" {
307 # Not sure what the point of having this function be unused is.
308 # AIX is legitimately removing it.
309 setup_xfail "rs6000-*-aix*"
310 gdb_test_multiple "list list1.c:unused" "" {
311 -re -wrap "12\[ \t\]+long_line \[(\]\[)\];.*13\[ \t\]+\}.*" {
314 -re -wrap "14.*19\[ \t\]+\}.*" {
318 clear_xfail "rs6000-*-aix*"
321 gdb_test "list 'list0.c:main'" "int main.*"
323 # Test some invalid specs
324 # The following test takes the FIXME result on most systems using
325 # DWARF. It fails to notice that main() is not in the file requested.
329 # Does this actually work ANYWHERE? I believe not, as this is an `aspect' of
330 # lookup_symbol(), where, when it is given a specific symtab which does not
331 # contain the requested symbol, it will subsequently search all of the symtabs
332 # for the requested symbol.
334 gdb_test "list list0.c:foo" "Function \"foo\" not defined in .*list0.c" "list filename:function; wrong filename rejected"
336 gdb_test "list foobar.c:main" "No source file named foobar.c.|Location not found" "list filename:function; nonexistant file"
338 gdb_test "list list0.h:foobar" "Function \"foobar\" not defined in \"list0.h\"." "list filename:function; nonexistant function"
342 # Test the forward-search (aka search) and the reverse-search commands.
344 proc test_forward_reverse_search {} {
347 gdb_test_no_output "set listsize 4"
348 # On SunOS4, this gives us lines 19-22. On AIX, it gives us
349 # lines 20-23. This depends on whether the line number of a function
350 # is considered to be the openbrace or the first statement--either one
352 gdb_test "list long_line" "24\[ \t\]+long_line .*"
354 gdb_test "search 4321" " not found"
356 gdb_test "search 6789" "28\[ \t\]+oof .6789.;"
358 # Try again, we shouldn't re-find the same source line. Also,
359 # while at it, test using the "forward-search" alias.
360 gdb_test "forward-search 6789" " not found"
362 # Now test backwards. First make sure we start searching from
363 # the previous line, not the current line.
364 gdb_test "reverse-search 6789" " not found"
366 # Now find something in a previous line.
367 gdb_test "reverse-search 67" "26\[ \t\]+oof .67.;"
369 # Test that GDB won't crash if the line being searched is extremely long.
371 set oldtimeout $timeout
372 set timeout [expr "$timeout + 300"]
373 verbose "Timeout is now $timeout seconds" 2
374 gdb_test "search 1234" ".*1234.*" "search extremely long line (> 5000 chars)"
375 set timeout $oldtimeout
376 verbose "Timeout is now $timeout seconds" 2
379 proc test_only_end {} {
380 gdb_test_no_output "set listsize 2"
381 gdb_test "list 1" "list 1\r\n1\[ \t\]\[^\r\n\]*\r\n2\[ \t\]\[^\r\n\]*"
382 gdb_test "list ,5" "list ,5\r\n4\[ \t\]\[^\r\n\]*\r\n5\[ \t\]\[^\r\n\]*"
385 proc test_list_invalid_args {} {
388 clean_restart ${binfile}
389 gdb_test "list -INVALID" \
390 "invalid explicit location argument, \"-INVALID\"" \
391 "first use of \"list -INVALID\""
392 gdb_test "list -INVALID" \
393 "invalid explicit location argument, \"-INVALID\"" \
394 "second use of \"list -INVALID\""
396 clean_restart ${binfile}
397 gdb_test "list +INVALID" "Function \"\\+INVALID\" not defined." \
398 "first use of \"list +INVALID\""
399 gdb_test "list +INVALID" "Function \"\\+INVALID\" not defined." \
400 "second use of \"list +INVALID\""
403 proc test_list_current_location {} {
405 # Reload the inferior to test "list ." before the inferior is started.
406 gdb_file_cmd ${binfile}
408 # Ensure that we are printing 10 lines.
409 if {![set_listsize 10]} {
413 # First guarantee that GDB prints around the main function correctly.
415 "1.*\r\n2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+.*5\[ \t\]+int x;.*" \
416 "list . with inferior not running"
419 warning "couldn't start inferior"
423 # Walk forward some lines.
424 gdb_test "until 15" ".*15.*foo.*"
426 # Test that the correct location is printed and that
427 # using just "list" will print the following lines.
428 gdb_test "list ." "10\[ \t\]+foo \(.*\);.*19\[ \t\]+foo \(.*\);" \
429 "list current line after starting"
430 gdb_test "list" "20\[ \t\]+foo \(.*\);.*29\[ \t\]+foo \(.*\);" \
431 "confirm we are printing the following lines"
433 # Test that list . will reset to current location
434 # and that an empty line lists the following lines.
435 gdb_test "list ." "10\[ \t\]+foo \(.*\);.*19\[ \t\]+foo \(.*\);" \
436 "list around current line again"
437 gdb_test " " "20\[ \t\]+foo \(.*\);.*29\[ \t\]+foo \(.*\);" \
438 "testing repeated invocations with GDB's auto-repeat"
442 gdb_file_cmd ${binfile}
444 gdb_test_no_output "set width 0"
448 if {[set_listsize 10]} {
449 test_list_include_file
450 test_list_filename_and_number
454 test_repeat_list_command
456 test_list_filename_and_function
457 test_forward_reverse_search
459 test_list_invalid_args
462 # Follows tests that require execution.
464 # Build source listing pattern based on a line range spec string. The
465 # range can be specificed as "START-END" indicating all lines in range
466 # (inclusive); or just "LINE", indicating just that line.
468 proc build_pattern { range_spec } {
471 set range_list [split $range_spec -]
472 set range_list_len [llength $range_list]
474 set range_start [lindex $range_list 0]
475 if { $range_list_len > 2 || $range_list_len < 1} {
476 error "invalid range spec string: $range_spec"
477 } elseif { $range_list_len == 2 } {
478 set range_end [lindex $range_list 1]
480 set range_end $range_start
483 for {set i $range_start} {$i <= $range_end} {incr i} {
484 append pattern "\r\n$i\[ \t\]\[^\r\n\]*"
487 verbose -log "pattern $pattern"
491 # Test "list" command invocations right after stopping for an event.
492 # COMMAND is the actual list command, including arguments. LISTSIZE1
493 # and LISTSIZE2 are the listsizes set just before and after running
494 # the program to the stop point. COMMAND is issued twice. The first
495 # time, the lines specificed by LINERANGE1 are expected; the second
496 # time, the lines specified by LINERANGE2 are expected.
498 proc test_list {command listsize1 listsize2 linerange1 linerange2} {
499 with_test_prefix "$command after stop: $listsize1, $listsize2" {
502 clean_restart $binfile
507 # Test changing the listsize both before nexting, and after
508 # stopping, but before listing. Only the second listsize
509 # change should affect which lines are listed.
510 with_test_prefix "before next" {
511 gdb_test_no_output "set listsize $listsize1"
513 gdb_test "next" "foo \\(.*"
514 with_test_prefix "after next" {
515 gdb_test_no_output "set listsize $listsize2"
518 set pattern1 [build_pattern $linerange1]
519 set pattern2 [build_pattern $linerange2]
520 gdb_test "$command" "${pattern1}" "$command #1"
521 gdb_test "$command" "${pattern2}" "$command #2"
526 # The first "list" should center the listing around line 8, the stop
528 test_list "list" 1 10 "3-12" "13-22"
531 test_list "list" 10 10 "3-12" "13-22"
533 # Likewise, but show only one line. IOW, the first list should show
534 # line 8. Note how the listsize is 10 at the time of the stop, but
535 # before any listing had been requested. That should not affect the
536 # line range that is first listed.
537 test_list "list" 10 1 "8" "9"
539 # Likewise, but show two lines.
540 test_list "list" 10 2 "7-8" "9-10"
543 test_list "list" 10 3 "7-9" "10-12"
545 # Now test backwards. Just like "list", the first "list -" should
546 # center the listing around the stop line.
547 test_list "list -" 10 10 "3-12" "2"
549 # Likewise, but test showing 3 lines at a time.
550 test_list "list -" 10 3 "7-9" "4-6"
553 test_list "list -" 10 2 "7-8" "5-6"
555 # Test listing one line only. This case is a little special and
556 # starts showing the previous line immediately instead of repeating
558 test_list "list -" 10 1 "7" "6"
560 # Test printing the location where the inferior is stopped.
561 test_list_current_location
563 remote_exec build "rm -f list0.h"