1#   Copyright 2005, 2007, 2008, 2009, 2010, 2011
2#   Free Software 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
17if $tracelevel then {
18	strace $tracelevel
19	}
20
21if { [is_remote target] || ![isnative] } then {
22    continue
23}
24
25# Until "set follow-fork-mode" and "catch fork" are implemented on
26# other targets...
27#
28if {![istarget "*-*-linux*"]} then {
29    continue
30}
31
32
33set testfile "checkpoint"
34set srcfile ${testfile}.c
35set binfile ${objdir}/${subdir}/${testfile}
36
37if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
38     untested checkpoint.exp
39     return -1
40}
41
42# Start with a fresh gdb
43
44gdb_exit
45gdb_start
46gdb_reinitialize_dir $srcdir/$subdir
47gdb_load ${binfile}
48
49global gdb_prompt
50
51#
52# This tests gdb checkpoint and restart.
53#
54
55remote_download host ${srcdir}/${subdir}/pi.txt pi.txt
56
57runto_main
58set break1_loc [gdb_get_line_number "breakpoint 1"]
59set break2_loc [gdb_get_line_number "breakpoint 2"]
60set break3_loc [gdb_get_line_number "breakpoint 3"]
61set break4_loc [gdb_get_line_number "breakpoint 4"]
62
63gdb_breakpoint $break1_loc
64gdb_test "continue" "breakpoint 1.*" "break1 start"
65
66gdb_test "checkpoint" ".*" ""
67gdb_test "continue 10" "breakpoint 1.*" "break1 two"
68
69gdb_test "checkpoint" ".*" ""
70gdb_test "continue 10" "breakpoint 1.*" "break1 three"
71
72gdb_test "checkpoint" ".*" ""
73gdb_test "continue 10" "breakpoint 1.*" "break1 four"
74
75gdb_test "checkpoint" ".*" ""
76gdb_test "continue 10" "breakpoint 1.*" "break1 five"
77
78gdb_test "checkpoint" ".*" ""
79gdb_test "continue 10" "breakpoint 1.*" "break1 six"
80
81gdb_test "checkpoint" ".*" ""
82gdb_test "continue 10" "breakpoint 1.*" "break1 seven"
83
84gdb_test "checkpoint" ".*" ""
85gdb_test "continue 10" "breakpoint 1.*" "break1 eight"
86
87gdb_test "checkpoint" ".*" ""
88gdb_test "continue 10" "breakpoint 1.*" "break1 nine"
89
90gdb_test "checkpoint" ".*" ""
91gdb_test "continue 10" "breakpoint 1.*" "break1 ten"
92
93gdb_test "checkpoint" ".*" ""
94
95gdb_test "info checkpoints" \
96    " 10 .* 9 .* 8 .* 7 .* 6 .* 5 .* 4 .* 3 .* 2 .* 1 .*" \
97    "info checkpoints one"
98
99delete_breakpoints
100gdb_breakpoint $break2_loc
101gdb_test "continue" "breakpoint 2.*" "break2 one"
102
103gdb_test "restart 1" "Switching to .*breakpoint 1.*" "restart 1 one"
104gdb_test "print i" " = 78" "verify i 1 one"
105gdb_test "step" "if .c == EOF.*" "step in 1 one"
106gdb_test "print lines" " = 1.*" "verify lines 1 one"
107
108gdb_test "restart 2" "Switching to .*breakpoint 1.*" "restart 2 one"
109gdb_test "step" "if .c == EOF.*" "step in 2 one"
110gdb_test "print i + 1 == lines * 79" " = 1" "verify i 2 one"
111gdb_test "print lines" " = 11.*" "verify lines 2 one"
112
113gdb_test "restart 3" "Switching to .*breakpoint 1.*" "restart 3 one"
114gdb_test "step" "if .c == EOF.*" "step in 3 one"
115gdb_test "print i + 1 == lines * 79" " = 1" "verify i 3 one"
116gdb_test "print lines" " = 21.*" "verify lines 3 one"
117
118gdb_test "restart 4" "Switching to .*breakpoint 1.*" "restart 4 one"
119gdb_test "step" "if .c == EOF.*" "step in 4 one"
120gdb_test "print i + 1 == lines * 79" " = 1" "verify i 4 one"
121gdb_test "print lines" " = 31.*" "verify lines 4 one"
122
123gdb_test "restart 5" "Switching to .*breakpoint 1.*" "restart 5 one"
124gdb_test "step" "if .c == EOF.*" "step in 5 one"
125gdb_test "print i + 1 == lines * 79" " = 1" "verify i 5 one"
126gdb_test "print lines" " = 41.*" "verify lines 5 one"
127
128gdb_test "restart 6" "Switching to .*breakpoint 1.*" "restart 6 one"
129gdb_test "step" "if .c == EOF.*" "step in 6 one"
130gdb_test "print i + 1 == lines * 79" " = 1" "verify i 6 one"
131gdb_test "print lines" " = 51.*" "verify lines 6 one"
132
133gdb_test "restart 7" "Switching to .*breakpoint 1.*" "restart 7 one"
134gdb_test "step" "if .c == EOF.*" "step in 7 one"
135gdb_test "print i + 1 == lines * 79" " = 1" "verify i 7 one"
136gdb_test "print lines" " = 61.*" "verify lines 7 one"
137
138gdb_test "restart 8" "Switching to .*breakpoint 1.*" "restart 8 one"
139gdb_test "step" "if .c == EOF.*" "step in 8 one"
140gdb_test "print i + 1 == lines * 79" " = 1" "verify i 8 one"
141gdb_test "print lines" " = 71.*" "verify lines 8 one"
142
143gdb_test "restart 9" "Switching to .*breakpoint 1.*" "restart 9 one"
144gdb_test "step" "if .c == EOF.*" "step in 9 one"
145gdb_test "print i + 1 == lines * 79" " = 1" "verify i 9 one"
146gdb_test "print lines" " = 81.*" "verify lines 9 one"
147
148gdb_test "restart 10" "Switching to .*breakpoint 1.*" "restart 10 one"
149gdb_test "step" "if .c == EOF.*" "step in 10 one"
150gdb_test "print i + 1 == lines * 79" " = 1" "verify i 10 one"
151gdb_test "print lines" " = 91.*" "verify lines 10 one"
152
153#
154# Now let the files be closed by the original process,
155# and diff them.
156
157gdb_test "restart 0" "Switching to .*breakpoint 2.*" "restart 0 one"
158gdb_breakpoint $break3_loc
159gdb_test "continue" "breakpoint 3.*" "break3 one"
160
161gdb_test "shell diff -s pi.txt copy1.txt" \
162    "Files pi.txt and copy1.txt are identical.*" \
163    "Diff input and output one"
164
165#
166# And now run from various checkpoints, allowing
167# various amounts of input and output.
168#
169
170gdb_breakpoint $break1_loc
171
172gdb_test "restart 1" "Switching to .*c == EOF.*" "restart 1 two"
173gdb_test "continue" ".*" ""
174gdb_test "continue 100" "breakpoint 1.*" "breakpoint 1 1 one"
175gdb_test "step" "if .c == EOF.*" "step in 1 two"
176gdb_test "print lines" " = 102.*" "verify lines 1 two"
177
178gdb_test "restart 2" "Switching to .*c == EOF.*" "restart 2 two"
179gdb_test "continue" ".*" ""
180gdb_test "continue 100" "breakpoint 1.*" "breakpoint 1 2 one"
181gdb_test "step" "if .c == EOF.*" "step in 2 two"
182gdb_test "print lines" " = 112.*" "verify lines 2 two"
183
184gdb_test "restart 3" "Switching to .*c == EOF.*" "restart 3 two"
185gdb_test "continue" ".*" ""
186gdb_test "continue 500" "breakpoint 1.*" "breakpoint 1 3 one"
187gdb_test "step" "if .c == EOF.*" "step in 3 two"
188gdb_test "print lines" " = 522.*" "verify lines 3 two"
189
190gdb_test "restart 4" "Switching to .*c == EOF.*" "restart 4 two"
191gdb_test "continue" ".*" ""
192gdb_test "continue 500" "breakpoint 1.*" "breakpoint 1 4 one"
193gdb_test "step" "if .c == EOF.*" "step in 4 two"
194gdb_test "print lines" " = 532.*" "verify lines 4 two"
195
196gdb_test "restart 5" "Switching to .*c == EOF.*" "restart 5 two"
197gdb_test "continue" ".*" ""
198gdb_test "continue 1000" "breakpoint 1.*" "breakpoint 1 5 one"
199gdb_test "step" "if .c == EOF.*" "step in 5 two"
200gdb_test "print lines" " = 1042.*" "verify lines 5 two"
201
202gdb_test "restart 6" "Switching to .*c == EOF.*" "restart 6 two"
203gdb_test "continue" ".*" ""
204gdb_test "continue 1000" "breakpoint 1.*" "breakpoint 1 6 one"
205gdb_test "step" "if .c == EOF.*" "step in 6 two"
206gdb_test "print lines" " = 1052.*" "verify lines 5 two"
207
208gdb_test "restart 7" "Switching to .*c == EOF.*" "restart 7 two"
209gdb_test "continue" ".*" ""
210gdb_test "continue 1100" "breakpoint 1.*" "breakpoint 1 7 one"
211gdb_test "step" "if .c == EOF.*" "step in 7 two"
212gdb_test "print lines" " = 1162.*" "verify lines 7 two"
213
214gdb_test "shell diff -s pi.txt copy1.txt" \
215    "Files pi.txt and copy1.txt are identical.*" \
216    "Diff input and output two"
217
218#
219# OK, now allow the original program to delete the output file,
220# and verify that the checkpoints can still write to it.
221#
222
223gdb_test "restart 0" "Switching to .*breakpoint 3.*" "restart 0 one"
224gdb_breakpoint $break4_loc
225gdb_test "continue" "breakpoint 4.*" "break4 one"
226
227gdb_test "shell diff pi.txt copy1.txt" \
228    "diff: copy1.txt: No such file or directory" \
229    "delete copy1"
230
231delete_breakpoints
232gdb_breakpoint $break2_loc
233
234gdb_test "restart 1" "if .c == EOF.*" "restart 1 three"
235gdb_test "continue" "breakpoint 2.*" "break2 1 one"
236gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 1"
237
238gdb_test "restart 2" "if .c == EOF.*" "restart 1 three"
239gdb_test "continue" "breakpoint 2.*" "break2 2 one"
240gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 2"
241
242gdb_test "restart 3" "if .c == EOF.*" "restart 1 three"
243gdb_test "continue" "breakpoint 2.*" "break2 3 one"
244gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 3"
245
246gdb_test "restart 4" "if .c == EOF.*" "restart 1 three"
247gdb_test "continue" "breakpoint 2.*" "break2 4 one"
248gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 4"
249
250gdb_test "restart 5" "if .c == EOF.*" "restart 1 three"
251gdb_test "continue" "breakpoint 2.*" "break2 5 one"
252gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 5"
253
254gdb_test "restart 6" "if .c == EOF.*" "restart 1 three"
255gdb_test "continue" "breakpoint 2.*" "break2 6 one"
256gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 6"
257
258gdb_test "restart 7" "if .c == EOF.*" "restart 1 three"
259gdb_test "continue" "breakpoint 2.*" "break2 7 one"
260gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 7"
261
262gdb_test "restart 8" "if .c == EOF.*" "restart 1 three"
263gdb_test "continue" "breakpoint 2.*" "break2 8 one"
264gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 8"
265
266gdb_test "restart 9" "if .c == EOF.*" "restart 1 three"
267gdb_test "continue" "breakpoint 2.*" "break2 9 one"
268gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 9"
269
270gdb_test "restart 10" "if .c == EOF.*" "restart 1 three"
271gdb_test "continue" "breakpoint 2.*" "break2 10 one"
272gdb_test "print ftell (out) > 100000" " = 1.*" "outfile still open 10"
273
274#
275# Now confirm that if one fork exits, we automatically switch to another one.
276#
277
278delete_breakpoints
279gdb_test "continue" \
280    "Deleting copy.*$inferior_exited_re normally.*Switching to.*" \
281    "Exit, dropped into next fork one"
282
283gdb_test "continue" \
284    "Deleting copy.*$inferior_exited_re normally.*Switching to.*" \
285    "Exit, dropped into next fork two"
286
287gdb_test "continue" \
288    "Deleting copy.*$inferior_exited_re normally.*Switching to.*" \
289    "Exit, dropped into next fork three"
290
291gdb_test "continue" \
292    "Deleting copy.*$inferior_exited_re normally.*Switching to.*" \
293    "Exit, dropped into next fork four"
294
295gdb_test "continue" \
296    "Deleting copy.*$inferior_exited_re normally.*Switching to.*" \
297    "Exit, dropped into next fork five"
298
299#
300# There should be still at least five forks left
301#
302
303gdb_test "info checkpoints" " 5 .* 4 .* 3 .* 2 .* 1 .*" \
304    "info checkpoints two"
305
306#
307# Kill should now terminate all of them.
308#
309
310gdb_test "kill" "" "kill all one" \
311    "Kill the program being debugged.*y or n. $" "y"
312
313#
314# and confirm that all are gone
315#
316
317gdb_test "restart 0" "Not found.*" "no more checkpoint 0"
318gdb_test "restart 1" "Not found.*" "no more checkpoint 1"
319gdb_test "restart 2" "Not found.*" "no more checkpoint 2"
320gdb_test "restart 3" "Not found.*" "no more checkpoint 3"
321gdb_test "restart 4" "Not found.*" "no more checkpoint 4"
322gdb_test "restart 5" "Not found.*" "no more checkpoint 5"
323gdb_test "restart 6" "Not found.*" "no more checkpoint 6"
324gdb_test "restart 7" "Not found.*" "no more checkpoint 7"
325gdb_test "restart 8" "Not found.*" "no more checkpoint 8"
326gdb_test "restart 9" "Not found.*" "no more checkpoint 9"
327gdb_test "restart 10" "Not found.*" "no more checkpoint 10"
328
329#
330# Now let's try setting a large number of checkpoints (>600)
331#
332
333gdb_exit
334gdb_start
335gdb_reinitialize_dir $srcdir/$subdir
336gdb_load ${binfile}
337
338runto_main
339gdb_breakpoint $break1_loc
340
341gdb_test "commands\nsilent\nif (lines % 2)\ncheckpoint\nend\n  continue\nend" \
342    "" \
343    "set checkpoint breakpoint"
344
345set prev_timeout $timeout
346set timeout [expr $timeout + 120]
347verbose "Timeout now $timeout sec."
348
349gdb_breakpoint $break2_loc
350gdb_test "continue" "breakpoint 2.*" "break2 with many checkpoints"
351
352set count 0
353set msg "info checkpoints with at least 600 checkpoints"
354gdb_test_multiple "info checkpoints" $msg {
355    -re "  $decimal process \[^\r\]*\r\n" {
356	incr count
357	exp_continue
358    }
359    -re "$gdb_prompt $" {
360	if { $count >= 600 } {
361	    pass $msg
362	} else {
363	    fail $msg
364	}
365    }
366}
367
368#
369# OK, kill 'em all...
370#
371
372gdb_test "kill" "" "kill all one with many checkpoints" \
373    "Kill the program being debugged.*y or n. $" "y"
374
375# Restore old timeout
376set timeout $prev_timeout
377verbose "Timeout now $timeout sec."
378
379#
380# Finished: cleanup
381#
382
383remote_exec build "rm -f pi.txt"
384