ChangeLog:
[external/binutils.git] / gdb / testsuite / gdb.base / dump.exp
1 # Copyright 2002, 2004, 2007, 2008, 2009 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 Michael Snyder (msnyder@redhat.com)
17 # This is a test for the gdb command "dump".
18
19 if $tracelevel then {
20         strace $tracelevel
21 }
22
23 set prms_id 0
24 set bug_id 0
25
26 set testfile "dump"
27
28 set srcfile  ${testfile}.c
29 set binfile  ${objdir}/${subdir}/${testfile}
30 set options  {debug}
31
32 set is64bitonly "no"
33 set endian "auto"
34
35 if [istarget "alpha*-*-*"] then {
36     # SREC etc cannot handle 64-bit addresses.  Force the test
37     # program into the low 31 bits of the address space.
38     lappend options "additional_flags=-Wl,-taso"
39 }
40
41 if {[istarget "ia64*-*-*"] || [istarget "hppa64-*-*"]} then {
42     set is64bitonly "yes"
43 }
44
45 if {[istarget "spu*-*-*"]} then {
46     # The internal address format used for the combined Cell/B.E.
47     # debugger requires 64-bit.
48     set is64bitonly "yes"
49 }
50
51 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
52      untested dump.exp
53      return -1
54 }
55
56 # Start with a fresh gdb.
57
58 gdb_exit
59 gdb_start
60 gdb_reinitialize_dir $srcdir/$subdir
61 gdb_load ${binfile}
62
63 # Clean up any stale output files from previous test runs
64
65 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"
66
67 # Test help (FIXME:)
68
69 # Run target program until data structs are initialized.
70
71 if { ! [ runto checkpoint1 ] } then {
72     untested dump.exp
73     return -1
74 }
75
76 # Get the endianness for the later use with endianless formats.
77
78 send_gdb "show endian\n"
79 gdb_expect {
80     -re ".* (big|little) endian.*$gdb_prompt $" { 
81         set endian $expect_out(1,string) 
82         pass "endianness: $endian"
83     }
84     default {
85         fail "(timeout) getting target endianness"
86     }
87 }
88
89 # Now generate some dump files.
90
91 proc make_dump_file { command msg } {
92   global gdb_prompt
93
94     send_gdb "${command}\n"
95     gdb_expect {
96         -re ".*\[Ee\]rror.*$gdb_prompt $"      { fail $msg }
97         -re ".*\[Ww\]arning.*$gdb_prompt $"    { fail $msg }
98         -re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
99         -re ".*$gdb_prompt $" { pass $msg }
100         timeout                                { fail "$msg (timeout)" }
101     }
102 }
103
104 make_dump_file "dump val intarr1.bin intarray" \
105         "dump array as value, default"
106
107 make_dump_file "dump val intstr1.bin intstruct" \
108         "dump struct as value, default"
109
110 make_dump_file "dump bin val intarr1b.bin intarray" \
111         "dump array as value, binary"
112
113 make_dump_file "dump bin val intstr1b.bin intstruct" \
114         "dump struct as value, binary"
115
116 make_dump_file "dump srec val intarr1.srec intarray" \
117         "dump array as value, srec"
118
119 make_dump_file "dump srec val intstr1.srec intstruct" \
120         "dump struct as value, srec"
121
122 make_dump_file "dump ihex val intarr1.ihex intarray" \
123         "dump array as value, intel hex"
124
125 make_dump_file "dump ihex val intstr1.ihex intstruct" \
126         "dump struct as value, intel hex"
127
128 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
129         "dump array as value, tekhex"
130
131 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
132         "dump struct as value, tekhex"
133
134 proc capture_value { expression args } {
135     global gdb_prompt
136     global expect_out
137
138     set output_string ""
139     if {[llength $args] > 0} {
140         # Convert $args into a simple string.
141         set test "[join $args]; capture $expression"
142     } {
143         set test "capture $expression"
144     }
145     gdb_test_multiple "print ${expression}" "$test" {
146         -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
147             set output_string "$expect_out(1,string)"
148             pass "$test"
149         }
150         -re "(Cannot access memory at address \[^\r\n\]+).*$gdb_prompt $" {
151             # Even a failed value is valid
152             set output_string "$expect_out(1,string)"
153             pass "$test"
154         }
155     }
156     return $output_string
157 }
158
159 set array_start  [capture_value "/x &intarray\[0\]"]
160 set array_end    [capture_value "/x &intarray\[32\]"]
161 set struct_start [capture_value "/x &intstruct"]
162 set struct_end   [capture_value "/x &intstruct + 1"]
163
164 set array_val    [capture_value "intarray"]
165 set struct_val   [capture_value "intstruct"]
166
167 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
168         "dump array as memory, default"
169
170 make_dump_file "dump  mem intstr2.bin $struct_start $struct_end" \
171         "dump struct as memory, default"
172
173 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
174         "dump array as memory, binary"
175
176 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
177         "dump struct as memory, binary"
178
179 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
180         "dump array as memory, srec"
181
182 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
183         "dump struct as memory, srec"
184
185 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
186         "dump array as memory, ihex"
187
188 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
189         "dump struct as memory, ihex"
190
191 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
192         "dump array as memory, tekhex"
193
194 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
195         "dump struct as memory, tekhex"
196
197 # test complex expressions
198 make_dump_file \
199         "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
200         "dump array as mem, srec, expressions"
201
202
203 # Now start a fresh gdb session, and reload the saved value files.
204
205 gdb_exit
206 gdb_start
207 gdb_file_cmd ${binfile}
208
209 # Now fix the endianness at the correct state.
210
211 send_gdb "set endian $endian\n"
212 gdb_expect {
213     -re ".* (big|little) endian.*$gdb_prompt $" { 
214         pass "setting $endian endianness"
215     }
216     default {
217         fail "(timeout) setting $endian endianness"
218     }
219 }
220
221 # Reload saved values one by one, and compare.
222
223 if { ![string compare $array_val \
224            [capture_value "intarray" "file binfile"]] } then {
225     fail "start with intarray un-initialized"
226 } else {
227     pass "start with intarray un-initialized"
228 }
229
230 if { ![string compare $struct_val \
231            [capture_value "intstruct" "file binfile"]] } then {
232     fail "start with intstruct un-initialized"
233 } else {
234     pass "start with intstruct un-initialized"
235 }
236
237 proc test_reload_saved_value { filename msg oldval newval } {
238     global gdb_prompt
239     
240     gdb_file_cmd $filename
241     if { ![string compare $oldval \
242                [capture_value $newval "$msg"]] } then { 
243         pass "$msg; value restored ok"
244     } else {
245         fail "$msg; value restored ok"
246     }
247 }
248
249 proc test_restore_saved_value { restore_args msg oldval newval } {
250     global gdb_prompt
251     
252     gdb_test "restore $restore_args" \
253         "Restoring .*" \
254         "$msg; file restored ok"
255     if { ![string compare $oldval \
256                [capture_value $newval "$msg"]] } then { 
257         pass "$msg; value restored ok"
258     } else {
259         fail "$msg; value restored ok"
260     }
261 }
262
263 # srec format can not be loaded for 64-bit-only platforms
264 if ![string compare $is64bitonly "no"] then {
265   test_reload_saved_value "intarr1.srec" "reload array as value, srec" \
266         $array_val "intarray"
267   test_reload_saved_value "intstr1.srec" "reload struct as value, srec" \
268         $struct_val "intstruct"
269   test_reload_saved_value "intarr2.srec" "reload array as memory, srec" \
270         $array_val "intarray"
271   test_reload_saved_value "intstr2.srec" "reload struct as memory, srec" \
272         $struct_val "intstruct"
273 }
274
275 # ihex format can not be loaded for 64-bit-only platforms
276 if ![string compare $is64bitonly "no"] then {
277
278   test_reload_saved_value "intarr1.ihex" "reload array as value, intel hex" \
279         $array_val "intarray"
280   test_reload_saved_value "intstr1.ihex" "reload struct as value, intel hex" \
281         $struct_val "intstruct"
282   test_reload_saved_value "intarr2.ihex" "reload array as memory, intel hex" \
283         $array_val "intarray"
284   test_reload_saved_value "intstr2.ihex" "reload struct as memory, intel hex" \
285         $struct_val "intstruct"
286 }
287
288 # tekhex format can not be loaded for 64-bit-only platforms
289 if ![string compare $is64bitonly "no"] then {
290   test_reload_saved_value "intarr1.tekhex" "reload array as value, tekhex" \
291         $array_val "intarray"
292   test_reload_saved_value "intstr1.tekhex" "reload struct as value, tekhex" \
293         $struct_val "intstruct"
294   test_reload_saved_value "intarr2.tekhex" "reload array as memory, tekhex" \
295         $array_val "intarray"
296   test_reload_saved_value "intstr2.tekhex" "reload struct as memory, tekhex" \
297         $struct_val "intstruct"
298 }
299
300 # Start a fresh gdb session
301
302 gdb_exit
303 gdb_start
304 gdb_reinitialize_dir $srcdir/$subdir
305 gdb_load ${binfile}
306
307 # Run to main.
308 if { ! [ runto_main ] } then {
309     untested dump.exp
310     return -1
311 }
312
313 if { ![string compare $array_val \
314            [capture_value "intarray" "load binfile"]] } then {
315     fail "start with intarray un-initialized, runto main"
316 } else {
317     pass "start with intarray un-initialized, runto main"
318 }
319
320 if { ![string compare $struct_val \
321            [capture_value "intstruct" "load binfile"]] } then {
322     fail "start with intstruct un-initialized, runto main"
323 } else {
324     pass "start with intstruct un-initialized, runto main"
325 }
326
327 if ![string compare $is64bitonly "no"] then {
328   test_restore_saved_value "intarr1.srec" "array as value, srec" \
329         $array_val "intarray"
330
331   test_restore_saved_value "intstr1.srec" "struct as value, srec" \
332         $struct_val "intstruct"
333
334   gdb_test "print zero_all ()" "void" "zero all"
335
336   test_restore_saved_value "intarr2.srec" "array as memory, srec" \
337         $array_val "intarray"
338
339   test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
340         $struct_val "intstruct"
341
342   gdb_test "print zero_all ()" ""
343
344   test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
345         $array_val "intarray"
346
347   test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
348         $struct_val "intstruct"
349
350   gdb_test "print zero_all ()" ""
351
352   test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
353         $array_val "intarray"
354
355   test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
356         $struct_val "intstruct"
357
358   gdb_test "print zero_all ()" ""
359
360   test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
361         $array_val "intarray"
362
363   test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
364         $struct_val "intstruct"
365
366   gdb_test "print zero_all ()" ""
367
368   test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
369         $array_val "intarray"
370
371   test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
372         $struct_val "intstruct"
373 }
374
375 gdb_test "print zero_all ()" ""
376
377 test_restore_saved_value "intarr1.bin binary $array_start" \
378         "array as value, binary" \
379         $array_val "intarray"
380
381 test_restore_saved_value "intstr1.bin binary $struct_start" \
382         "struct as value, binary" \
383         $struct_val "intstruct"
384
385 gdb_test "print zero_all ()" ""
386
387 test_restore_saved_value "intarr2.bin binary $array_start" \
388         "array as memory, binary" \
389         $array_val "intarray"
390
391 test_restore_saved_value "intstr2.bin binary $struct_start" \
392         "struct as memory, binary" \
393         $struct_val "intstruct"
394
395 # test restore with offset.
396
397 set array2_start   [capture_value "/x &intarray2\[0\]"]
398 set struct2_start  [capture_value "/x &intstruct2"]
399 set array2_offset  \
400         [capture_value "(char *) &intarray2 - (char *) &intarray"]
401 set struct2_offset \
402         [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
403
404 gdb_test "print zero_all ()" ""
405
406
407 if ![string compare $is64bitonly "no"] then {
408   test_restore_saved_value "intarr1.srec $array2_offset" \
409         "array copy, srec" \
410         $array_val "intarray2"
411
412   test_restore_saved_value "intstr1.srec $struct2_offset" \
413         "struct copy, srec" \
414         $struct_val "intstruct2"
415
416   gdb_test "print zero_all ()" ""
417
418   test_restore_saved_value "intarr1.ihex $array2_offset" \
419         "array copy, ihex" \
420         $array_val "intarray2"
421
422   test_restore_saved_value "intstr1.ihex $struct2_offset" \
423         "struct copy, ihex" \
424         $struct_val "intstruct2"
425
426   gdb_test "print zero_all ()" ""
427
428   test_restore_saved_value "intarr1.tekhex $array2_offset" \
429         "array copy, tekhex" \
430         $array_val "intarray2"
431
432   test_restore_saved_value "intstr1.tekhex $struct2_offset" \
433         "struct copy, tekhex" \
434         $struct_val "intstruct2"
435 }
436
437 gdb_test "print zero_all ()" ""
438
439 test_restore_saved_value "intarr1.bin binary $array2_start" \
440         "array copy, binary" \
441         $array_val "intarray2"
442
443 test_restore_saved_value "intstr1.bin binary $struct2_start" \
444         "struct copy, binary" \
445         $struct_val "intstruct2"
446
447 #
448 # test restore with start/stop addresses.
449 #
450 # For this purpose, we will restore just the third element of the array, 
451 # and check to see that adjacent elements are not modified.
452 #
453 # We will need the address and offset of the third and fourth elements.
454 #
455
456 set element3_start  [capture_value "/x &intarray\[3\]"]
457 set element4_start  [capture_value "/x &intarray\[4\]"]
458 set element3_offset \
459         [capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
460 set element4_offset \
461         [capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
462
463 if ![string compare $is64bitonly "no"] then {
464   gdb_test "print zero_all ()" ""
465
466   test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
467         "array partial, srec" 4 "intarray\[3\]"
468
469   gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
470   gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
471
472   gdb_test "print zero_all ()" ""
473
474   test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
475         "array partial, ihex" 4 "intarray\[3\]"
476
477   gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
478   gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
479
480   gdb_test "print zero_all ()" ""
481
482   test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
483         "array partial, tekhex" 4 "intarray\[3\]"
484
485   gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
486   gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
487 }
488
489 gdb_test "print zero_all ()" ""
490
491 test_restore_saved_value \
492     "intarr1.bin binary $array_start $element3_offset $element4_offset" \
493     "array partial, binary" 4 "intarray\[3\]"
494
495 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
496 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
497
498 if ![string compare $is64bitonly "no"] then {
499   gdb_test "print zero_all ()" "" ""
500
501   # restore with expressions 
502   test_restore_saved_value \
503         "intarr3.srec ${array2_start}-${array_start} &intarray\[3\] &intarray\[4\]" \
504         "array partial with expressions" 4 "intarray2\[3\]"
505
506   gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
507   gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
508 }
509
510 # clean up files
511
512 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"