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