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