2003-02-05 Michael Chastain <mec@shout.net>
[external/binutils.git] / gdb / testsuite / gdb.base / dump.exp
1 # Copyright 2002 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 # This file was written by Michael Snyder (msnyder@redhat.com)
21 # This is a test for the gdb command "dump".
22
23 if $tracelevel then {
24         strace $tracelevel
25 }
26
27 set prms_id 0
28 set bug_id 0
29
30 set testfile "dump"
31
32 set srcfile  ${testfile}.c
33 set binfile  ${objdir}/${subdir}/${testfile}
34
35 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
36      gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
37 }
38
39 # Start with a fresh gdb.
40
41 gdb_exit
42 gdb_start
43 gdb_reinitialize_dir $srcdir/$subdir
44 gdb_load ${binfile}
45
46 # Clean up any stale output files from previous test runs
47
48 remote_exec build "rm -f intarr1.bin intarr1b.bin intarr1.ihex intarr1.srec intarr1.tekhex intarr2.bin intarr2b.bin intarr2.ihex intarr2.srec intarr2.tekhex intstr1.bin intstr1b.bin intstr1.ihex intstr1.srec intstr1.tekhex intstr2.bin intstr2b.bin intstr2.ihex intstr2.srec intstr2.tekhex intarr3.srec"
49
50 # Test help (FIXME:)
51
52 # Run target program until data structs are initialized.
53
54 if { ! [ runto checkpoint1 ] } then {
55     gdb_suppress_entire_file "Program failed to run, so all tests in this file will automatically fail."
56 }
57
58 # Now generate some dump files.
59
60 proc make_dump_file { command msg } {
61   global gdb_prompt
62
63     send_gdb "${command}\n"
64     gdb_expect {
65         -re ".*\[Ee\]rror.*$gdb_prompt $"      { fail $msg }
66         -re ".*\[Ww\]arning.*$gdb_prompt $"    { fail $msg }
67         -re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
68         -re ".*$gdb_prompt $" { pass $msg }
69         timeout                                { fail "$msg (timeout)" }
70     }
71 }
72
73 make_dump_file "dump val intarr1.bin intarray" \
74         "dump array as value, default"
75
76 make_dump_file "dump val intstr1.bin intstruct" \
77         "dump struct as value, default"
78
79 make_dump_file "dump bin val intarr1b.bin intarray" \
80         "dump array as value, binary"
81
82 make_dump_file "dump bin val intstr1b.bin intstruct" \
83         "dump struct as value, binary"
84
85 make_dump_file "dump srec val intarr1.srec intarray" \
86         "dump array as value, srec"
87
88 make_dump_file "dump srec val intstr1.srec intstruct" \
89         "dump struct as value, srec"
90
91 make_dump_file "dump ihex val intarr1.ihex intarray" \
92         "dump array as value, intel hex"
93
94 make_dump_file "dump ihex val intstr1.ihex intstruct" \
95         "dump struct as value, intel hex"
96
97 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
98         "dump array as value, tekhex"
99
100 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
101         "dump struct as value, tekhex"
102
103 proc capture_value { expression } {
104     global gdb_prompt
105     global expect_out
106
107     set output_string ""
108     send_gdb "print ${expression}\n"
109     gdb_expect {
110         -re ".*\[\r\n\]+.\[0123456789\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
111             set output_string $expect_out(1,string)
112         }
113         default {
114             fail "capture_value failed on $expression."
115         }
116     }
117     return $output_string
118 }
119
120 set array_start  [capture_value "/x &intarray\[0\]"]
121 set array_end    [capture_value "/x &intarray\[32\]"]
122 set struct_start [capture_value "/x &intstruct"]
123 set struct_end   [capture_value "/x &intstruct + 1"]
124
125 set array_val    [capture_value "intarray"]
126 set struct_val   [capture_value "intstruct"]
127
128 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
129         "dump array as memory, default"
130
131 make_dump_file "dump  mem intstr2.bin $struct_start $struct_end" \
132         "dump struct as memory, default"
133
134 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
135         "dump array as memory, binary"
136
137 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
138         "dump struct as memory, binary"
139
140 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
141         "dump array as memory, srec"
142
143 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
144         "dump struct as memory, srec"
145
146 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
147         "dump array as memory, ihex"
148
149 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
150         "dump struct as memory, ihex"
151
152 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
153         "dump array as memory, tekhex"
154
155 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
156         "dump struct as memory, tekhex"
157
158 # test complex expressions
159 make_dump_file \
160         "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
161         "dump array as mem, srec, expressions"
162
163
164 # Now start a fresh gdb session, and reload the saved value files.
165
166 gdb_exit
167 gdb_start
168 gdb_file_cmd ${binfile}
169
170 # Reload saved values one by one, and compare.
171
172 if { ![string compare $array_val [capture_value "intarray"]] } then {
173     fail "start with intarray un-initialized"
174 } else {
175     pass "start with intarray un-initialized"
176 }
177
178 if { ![string compare $struct_val [capture_value "intstruct"]] } then {
179     fail "start with intstruct un-initialized"
180 } else {
181     pass "start with intstruct un-initialized"
182 }
183
184 proc test_reload_saved_value { filename msg oldval newval } {
185     global gdb_prompt
186     
187     gdb_file_cmd $filename
188     if { ![string compare $oldval [capture_value $newval]] } then { 
189         pass $msg 
190     } else {
191         fail $msg 
192     }
193 }
194
195 proc test_restore_saved_value { restore_args msg oldval newval } {
196     global gdb_prompt
197     
198     gdb_test "restore $restore_args" \
199             "Restoring .*" \
200             "Restore command, $msg"
201
202     if { ![string compare $oldval [capture_value $newval]] } then { 
203         pass "Restored value, $msg"
204     } else {
205         fail "Restored value, $msg"
206     }
207 }
208
209 test_reload_saved_value "intarr1.srec" "reload array as value, srec" \
210         $array_val "intarray"
211 test_reload_saved_value "intstr1.srec" "reload struct as value, srec" \
212         $struct_val "intstruct"
213 test_reload_saved_value "intarr2.srec" "reload array as memory, srec" \
214         $array_val "intarray"
215 test_reload_saved_value "intstr2.srec" "reload struct as memory, srec" \
216         $struct_val "intstruct"
217
218 test_reload_saved_value "intarr1.ihex" "reload array as value, intel hex" \
219         $array_val "intarray"
220 test_reload_saved_value "intstr1.ihex" "reload struct as value, intel hex" \
221         $struct_val "intstruct"
222 test_reload_saved_value "intarr2.ihex" "reload array as memory, intel hex" \
223         $array_val "intarray"
224 test_reload_saved_value "intstr2.ihex" "reload struct as memory, intel hex" \
225         $struct_val "intstruct"
226
227 test_reload_saved_value "intarr1.tekhex" "reload array as value, tekhex" \
228         $array_val "intarray"
229 test_reload_saved_value "intstr1.tekhex" "reload struct as value, tekhex" \
230         $struct_val "intstruct"
231 test_reload_saved_value "intarr2.tekhex" "reload array as memory, tekhex" \
232         $array_val "intarray"
233 test_reload_saved_value "intstr2.tekhex" "reload struct as memory, tekhex" \
234         $struct_val "intstruct"
235
236 # Start a fresh gdb session
237
238 gdb_exit
239 gdb_start
240 gdb_reinitialize_dir $srcdir/$subdir
241 gdb_load ${binfile}
242
243 # Run to main.
244 if { ! [ runto main ] } then {
245     gdb_suppress_entire_file "Program failed to run, so remaining tests in this file will automatically fail."
246 }
247
248 if { ![string compare $array_val [capture_value "intarray"]] } then {
249     fail "start with intarray un-initialized, runto main"
250 } else {
251     pass "start with intarray un-initialized, runto main"
252 }
253
254 if { ![string compare $struct_val [capture_value "intstruct"]] } then {
255     fail "start with intstruct un-initialized, runto main"
256 } else {
257     pass "start with intstruct un-initialized, runto main"
258 }
259
260 test_restore_saved_value "intarr1.srec" "array as value, srec" \
261         $array_val "intarray"
262
263 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
264         $struct_val "intstruct"
265
266 gdb_test "print zero_all ()" "void" "zero all"
267
268 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
269         $array_val "intarray"
270
271 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
272         $struct_val "intstruct"
273
274 gdb_test "print zero_all ()" ""
275
276 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
277         $array_val "intarray"
278
279 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
280         $struct_val "intstruct"
281
282 gdb_test "print zero_all ()" ""
283
284 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
285         $array_val "intarray"
286
287 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
288         $struct_val "intstruct"
289
290 gdb_test "print zero_all ()" ""
291
292 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
293         $array_val "intarray"
294
295 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
296         $struct_val "intstruct"
297
298 gdb_test "print zero_all ()" ""
299
300 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
301         $array_val "intarray"
302
303 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
304         $struct_val "intstruct"
305
306 gdb_test "print zero_all ()" ""
307
308 test_restore_saved_value "intarr1.bin binary $array_start" \
309         "array as value, binary" \
310         $array_val "intarray"
311
312 test_restore_saved_value "intstr1.bin binary $struct_start" \
313         "struct as value, binary" \
314         $struct_val "intstruct"
315
316 gdb_test "print zero_all ()" ""
317
318 test_restore_saved_value "intarr2.bin binary $array_start" \
319         "array as memory, binary" \
320         $array_val "intarray"
321
322 test_restore_saved_value "intstr2.bin binary $struct_start" \
323         "struct as memory, binary" \
324         $struct_val "intstruct"
325
326 # test restore with offset.
327
328 set array2_start   [capture_value "/x &intarray2\[0\]"]
329 set struct2_start  [capture_value "/x &intstruct2"]
330 set array2_offset  \
331         [capture_value "/x (char *) &intarray2 - (char *) &intarray"]
332 set struct2_offset \
333         [capture_value "/x (char *) &intstruct2 - (char *) &intstruct"]
334
335 gdb_test "print zero_all ()" ""
336
337 test_restore_saved_value "intarr1.srec $array2_offset" \
338         "array copy, srec" \
339         $array_val "intarray2"
340
341 test_restore_saved_value "intstr1.srec $struct2_offset" \
342         "struct copy, srec" \
343         $struct_val "intstruct2"
344
345 gdb_test "print zero_all ()" ""
346
347 test_restore_saved_value "intarr1.ihex $array2_offset" \
348         "array copy, ihex" \
349         $array_val "intarray2"
350
351 test_restore_saved_value "intstr1.ihex $struct2_offset" \
352         "struct copy, ihex" \
353         $struct_val "intstruct2"
354
355 gdb_test "print zero_all ()" ""
356
357 test_restore_saved_value "intarr1.tekhex $array2_offset" \
358         "array copy, tekhex" \
359         $array_val "intarray2"
360
361 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
362         "struct copy, tekhex" \
363         $struct_val "intstruct2"
364
365 gdb_test "print zero_all ()" ""
366
367 test_restore_saved_value "intarr1.bin binary $array2_start" \
368         "array copy, binary" \
369         $array_val "intarray2"
370
371 test_restore_saved_value "intstr1.bin binary $struct2_start" \
372         "struct copy, binary" \
373         $struct_val "intstruct2"
374
375 #
376 # test restore with start/stop addresses.
377 #
378 # For this purpose, we will restore just the third element of the array, 
379 # and check to see that adjacent elements are not modified.
380 #
381 # We will need the address and offset of the third and fourth elements.
382 #
383
384 set element3_start  [capture_value "/x &intarray\[3\]"]
385 set element4_start  [capture_value "/x &intarray\[4\]"]
386 set element3_offset \
387         [capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
388 set element4_offset \
389         [capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
390
391 gdb_test "print zero_all ()" ""
392
393 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
394         "array partial, srec" \
395         [capture_value "4"] "intarray\[3\]"
396
397 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
398 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
399
400 gdb_test "print zero_all ()" ""
401
402 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
403         "array partial, ihex" \
404         [capture_value "4"] "intarray\[3\]"
405
406 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
407 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
408
409 gdb_test "print zero_all ()" ""
410
411 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
412         "array partial, tekhex" \
413         [capture_value "4"] "intarray\[3\]"
414
415 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
416 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
417
418 gdb_test "print zero_all ()" ""
419
420 test_restore_saved_value \
421     "intarr1.bin binary $array_start $element3_offset $element4_offset" \
422     "array partial, binary" \
423     [capture_value "4"] "intarray\[3\]"
424
425 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
426 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
427
428 gdb_test "print zero_all ()" "" ""
429
430 # restore with expressions 
431 test_restore_saved_value \
432         "intarr3.srec ${array2_start}-${array_start} &intarray\[3\] &intarray\[4\]" \
433         "array partial with expressions" \
434         [capture_value "4"] "intarray2\[3\]"
435
436 gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
437 gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
438
439
440 # clean up files
441
442 remote_exec build "rm -f intarr1.bin intarr1b.bin intarr1.ihex intarr1.srec intarr1.tekhex intarr2.bin intarr2b.bin intarr2.ihex intarr2.srec intarr2.tekhex intstr1.bin intstr1b.bin intstr1.ihex intstr1.srec intstr1.tekhex intstr2.bin intstr2b.bin intstr2.ihex intstr2.srec intstr2.tekhex intarr3.srec"
443