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