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