84ae251bfe41fc215bea2cc081bab5786c367f24
[external/binutils.git] / gdb / testsuite / gdb.base / list.exp
1 # Copyright 1992-2015 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 $testfile.exp $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 hp_cc_compiler
73     global hp_aCC_compiler
74     global last_line_re
75
76     # Show default size
77
78     gdb_test "show listsize" "Number of source lines gdb will list by default is 10.*" "show default list size"
79     
80     # Show the default lines
81
82     gdb_test "list" "(1\[ \t\]+#include \"list0.h\".*7\[ \t\]+x = 0;\r\n.*10\[ \t\]+foo .x\[+)\]+;)" "list default lines around main"
83
84     # Ensure we can limit printouts to one line
85
86     if [set_listsize 1] {
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"
89     }    
90
91     # Try just two lines
92     
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"
97     }
98
99     # Try small listsize > 1 that is an odd number
100
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"
105     }
106
107     # Try small listsize > 2 that is an even number.
108
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"
112         
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"
115     }
116
117     # Try a size larger than the entire file.
118
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"
121         
122         gdb_test "list 10" "1\[ \t\]+#include \"list0.h\".*\r\n${last_line_re}" "list line 10 with listsize 100"
123     }
124
125     # Try listsize of 0 which is special, and means unlimited.
126
127     set_listsize 0
128     gdb_test "list 1" "1\[ \t\]+#include .*\r\n${last_line_re}" "list line 1 with unlimited listsize"
129 }
130
131 #
132 # Test "list filename:number" for C include file
133 #
134
135 proc test_list_include_file {} {
136     global gdb_prompt
137
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"
140
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"
143 }
144
145 #
146 # Test "list filename:number" for C source file
147 #
148
149 proc test_list_filename_and_number {} {
150     global gdb_prompt
151
152     set testcnt 0
153
154     send_gdb "list list0.c:1\n"
155     gdb_expect {
156         -re "1\[ \t\]+#include \"list0.h\".*10\[ \t\]+foo .x\[+)\]+;\r\n$gdb_prompt $" {
157             incr testcnt 
158         }
159         -re ".*$gdb_prompt $" { fail "list list0.c:1" ; gdb_suppress_tests }
160         timeout { fail "list list0.c:1 (timeout)" ; gdb_suppress_tests }
161     }
162     send_gdb "list list0.c:10\n"
163     gdb_expect {
164         -re "5\[ \t\]+int x;.*14\[ \t\]+foo .x\[+)\]+;\r\n$gdb_prompt $" {
165             incr testcnt 
166         }
167         -re ".*$gdb_prompt $" { fail "list list.c:10" ; gdb_suppress_tests }
168         timeout { fail "list list.c:10 (timeout)" ; gdb_suppress_tests }
169     }
170     send_gdb "list list1.c:1\n"
171     gdb_expect {
172         -re "1\[ \t\]+\#include.*4\[ \t\]+.*int oof\[ \t\]*\(.*\);\r\n.*$gdb_prompt $" {
173             incr testcnt 
174         }
175         -re ".*$gdb_prompt $" { fail "list list1.c:1" ; gdb_suppress_tests }
176         timeout { fail "list list1.c:1 (timeout)" ; gdb_suppress_tests }
177     }
178     send_gdb "list list1.c:12\n"
179     gdb_expect {
180         -re "12\[ \t\]+long_line \[(\]+.*\[)\]+;.*13\[ \t\]+\}\r\n.*$gdb_prompt $" {
181             incr testcnt 
182         }
183         -re ".*$gdb_prompt $" { fail "list list1.c:12" ; gdb_suppress_tests }
184         timeout { fail "list list1.c:12 (timeout)" ; gdb_suppress_tests }
185     }
186     pass "list filename:number ($testcnt tests)"
187     gdb_stop_suppressing_tests
188 }
189
190 #
191 # Test "list function" for C source file
192 #
193
194 proc test_list_function {} {
195     global gdb_prompt
196
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"
201
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"
205
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"
210 }
211
212 proc test_list_forward {} {
213     global gdb_prompt
214     global last_line_re
215
216     set testcnt 0
217
218     send_gdb "list list0.c:10\n"
219     gdb_expect {
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 }
223     }
224
225     send_gdb "list\n"
226     gdb_expect {
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 }
230     }
231
232     send_gdb "list\n"
233     gdb_expect {
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 }
237     }
238
239     send_gdb "list\n"
240     gdb_expect {
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 }
244     }
245
246     pass "successive list commands to page forward ($testcnt tests)"
247     gdb_stop_suppressing_tests
248 }
249
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.  
254
255 proc test_repeat_list_command {} {
256     global gdb_prompt
257     global last_line_re
258
259     set testcnt 0
260
261     send_gdb "list list0.c:10\n"
262     gdb_expect {
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 }
266     }
267
268     send_gdb "\n"
269     gdb_expect {
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 }
273     }
274
275     send_gdb "\n"
276     gdb_expect {
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 }
280     }
281
282     send_gdb "\n"
283     gdb_expect {
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 }
287     }
288
289     pass "repeat list commands to page forward using 'return' ($testcnt tests)"
290     gdb_stop_suppressing_tests
291 }
292
293 proc test_list_backwards {} {
294     global gdb_prompt
295
296     set testcnt 0
297
298     send_gdb "list list0.c:33\n"
299     gdb_expect {
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 }
303     }
304
305     send_gdb "list -\n"
306     gdb_expect {
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 }
310     }
311
312     send_gdb "list -\n"
313     gdb_expect {
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 }
317     }
318
319     send_gdb "list -\n"
320     gdb_expect {
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 }
324     }
325
326     pass "$testcnt successive \"list -\" commands to page backwards"
327     gdb_stop_suppressing_tests
328 }
329
330 #
331 # Test "list first,last"
332 #
333
334 proc test_list_range {} {
335     global gdb_prompt
336     global last_line_re
337     global last_line
338
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"
340
341     gdb_test "list 2,5" "2\[ \t\]+\r\n3\[ \t\]+int main \[)(\]+.*5\[ \t\]+int x;" "list range; line1,line2"
342
343 #    gdb_test     "list -1,6"   "Line number 0 out of range; .*list0.c has 39 lines." "list range; lower bound negative"
344
345 #    gdb_test     "list -100,-40"       "Line number -60 out of range; .*list0.c has 39 lines." "list range; both bounds negative"
346
347     set past_end [expr ${last_line} + 10]
348     set much_past_end [expr ${past_end} + 10]
349
350     gdb_test "list 30,${past_end}" "30\[ \t\]+foo \(.*\);.*${last_line_re}" "list range; upper bound past EOF"
351
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"
353
354     gdb_test "list list0.c:2,list1.c:17" "Specified start and end are in different files." "list range, must be same files"
355 }
356
357 #
358 # Test "list filename:function"
359 #
360
361 proc test_list_filename_and_function {} {
362     global gdb_prompt
363     global last_line_re
364
365     set testcnt 0
366
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"
371     gdb_expect {
372         -re "1\[ \t\]+#include .*10\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
373             incr testcnt
374         }
375         -re "5\[ \t\]+int x;.*14\[ \t\]+foo \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
376             pass "list function in source file 1"
377         }
378         -re ".*$gdb_prompt $" { fail "list list0.c:main" }
379         timeout { fail "list list0.c:main (timeout)" }
380     }
381
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"
386     gdb_expect {
387         -re "40\[ \t\]+unused.*${last_line_re}\r\n$gdb_prompt $" {
388             incr testcnt
389         }
390         -re "37.*42\[ \t\]+\}\r\n$gdb_prompt $" {
391             incr testcnt
392         }
393         -re ".*$gdb_prompt $" { fail "list list0.c:unused" }
394         timeout { fail "list list0.c:unused (timeout)" }
395     }
396     clear_xfail "rs6000-*-aix*"
397
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"
405     gdb_expect {
406         -re "2\[ \t\]+including file.  This.*11\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
407             incr testcnt
408         }
409         -re "1\[ \t\]+/. An include file.*10\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
410             incr testcnt
411         }
412         -re "3\[ \t\]+.*12\[ \t\]+bar \[(\]+.*\[)\]+;\r\n$gdb_prompt $" {
413             incr testcnt
414         }
415         -re "No source file named list0.h.\r\n$gdb_prompt $" {
416             fail "list list0.h:foo"
417         }
418         -re ".*$gdb_prompt $" { fail "list list0.h:foo" }
419         timeout { fail "list list0.h:foo (timeout)" }
420     }
421
422     # Ultrix gdb is the second case.
423     send_gdb "list list1.c:bar\n"
424     gdb_expect {
425         -re "4\[ \t\]+void.*13\[ \t\]+\}\r\n$gdb_prompt $" {
426             incr testcnt
427         }
428         -re "4\[ \t\]+void.*12\[ \t\]*long_line ..;\r\n$gdb_prompt $" {
429             incr testcnt
430         }
431         -re "4\[ \t\]+void.*11\[ \t\]*\r\n$gdb_prompt $" {
432             incr testcnt
433         }
434         -re ".*$gdb_prompt $" { fail "list list1.c:bar" }
435         timeout { fail "list list1.c:bar (timeout)" }
436     }
437
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"
442     gdb_expect {
443         -re "12\[ \t\]+long_line \[(\]\[)\];.*13\[ \t\]+\}\r\n.*$gdb_prompt $" {
444             incr testcnt
445         }
446         -re "14.*19\[ \t\]+\}\r\n.*$gdb_prompt $" {
447             incr testcnt
448         }
449         -re ".*$gdb_prompt $" { fail "list list1.c:unused" }
450         timeout { fail "list list1.c:unused (timeout)" }
451     }
452     clear_xfail "rs6000-*-aix*"
453
454     pass "list filename:function ($testcnt tests)"
455
456     # Test with quoting.
457     gdb_test "list 'list0.c:main'" "int main.*"
458
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.
462    
463     setup_xfail "*-*-*"
464
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.
469
470     gdb_test "list list0.c:foo" "Function \"foo\" not defined in .*list0.c" "list filename:function; wrong filename rejected"
471
472     gdb_test "list foobar.c:main" "No source file named foobar.c.|Location not found" "list filename:function; nonexistant file"
473
474     gdb_test "list list0.h:foobar" "Function \"foobar\" not defined in \"list0.h\"." "list filename:function; nonexistant function"
475
476 }
477
478 proc test_forward_search {} {
479         global timeout
480
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
485         # is acceptable.
486         gdb_test "list long_line" "24\[ \t\]+long_line .*"
487
488         gdb_test "search 4321" " not found"
489
490         gdb_test "search 6789" "28\[ \t\]+oof .6789.;"
491
492         # Test that GDB won't crash if the line being searched is extremely long.
493
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
500 }
501
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\]*"
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     test_only_end
530 }
531
532 # Follows tests that require execution.
533
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.
537
538 proc build_pattern { range_spec } {
539     global line_re
540
541     set range_list [split $range_spec -]
542     set range_list_len [llength $range_list]
543
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]
549     } else {
550         set range_end $range_start
551     }
552
553     for {set i $range_start} {$i <= $range_end} {incr i} {
554         append pattern "\r\n$i\[ \t\]\[^\r\n\]*"
555     }
556
557     verbose -log "pattern $pattern"
558     return $pattern
559 }
560
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.
567
568 proc test_list {command listsize1 listsize2 linerange1 linerange2} {
569     with_test_prefix "$command after stop: $listsize1, $listsize2" {
570         global binfile
571
572         clean_restart $binfile
573         if ![runto_main] then {
574             fail "Can't run to main"
575             return
576         }
577
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         with_test_prefix "before next" {
582             gdb_test_no_output "set listsize $listsize1"
583         }
584         gdb_test "next" "foo \\(.*"
585         with_test_prefix "after next" {
586             gdb_test_no_output "set listsize $listsize2"
587         }
588
589         set pattern1 [build_pattern $linerange1]
590         set pattern2 [build_pattern $linerange2]
591         gdb_test "$command" "${pattern1}" "$command #1"
592         gdb_test "$command" "${pattern2}" "$command #2"
593     }
594 }
595
596
597 # The first "list" should center the listing around line 8, the stop
598 # line.
599 test_list "list" 1 10 "3-12" "13-22"
600
601 # Likewise.
602 test_list "list" 10 10 "3-12" "13-22"
603
604 # Likewise, but show only one line.  IOW, the first list should show
605 # line 8.  Note how the listsize is 10 at the time of the stop, but
606 # before any listing had been requested.  That should not affect the
607 # line range that is first listed.
608 test_list "list" 10 1 "8" "9"
609
610 # Likewise, but show two lines.
611 test_list "list" 10 2 "7-8" "9-10"
612
613 # Three lines.
614 test_list "list" 10 3 "7-9" "10-12"
615
616 # Now test backwards.  Just like "list", the first "list -" should
617 # center the listing around the stop line.
618 test_list "list -" 10 10 "3-12" "2"
619
620 # Likewise, but test showing 3 lines at a time.
621 test_list "list -" 10 3 "7-9" "4-6"
622
623 # 2 lines at a time.
624 test_list "list -" 10 2 "7-8" "5-6"
625
626 # Test listing one line only.  This case is a little special and
627 # starts showing the previous line immediately instead of repeating
628 # the current line.
629 test_list "list -" 10 1 "7" "6"
630
631 remote_exec build "rm -f list0.h"