1 # Copyright 2002, 2004, 2007-2012 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file was written by Michael Snyder (msnyder@redhat.com)
17 # This is a test for the gdb command "dump".
22 set srcfile ${testfile}.c
23 set binfile ${objdir}/${subdir}/${testfile}
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"
35 if {[istarget "ia64*-*-*"] || [istarget "hppa64-*-*"]} then {
39 if {[istarget "spu*-*-*"]} then {
40 # The internal address format used for the combined Cell/B.E.
41 # debugger requires 64-bit.
45 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
50 # Start with a fresh gdb.
54 gdb_reinitialize_dir $srcdir/$subdir
56 gdb_test "dump mem /dev/null 0x10 0x20" "Cannot access memory at address 0x10" \
57 "inaccessible memory is reported"
61 # Clean up any stale output files from previous test runs
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"
67 # Run target program until data structs are initialized.
69 if { ! [ runto checkpoint1 ] } then {
74 # Get the endianness for the later use with endianless formats.
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"
83 # Now generate some dump files.
85 proc make_dump_file { command msg } {
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 }
96 make_dump_file "dump val intarr1.bin intarray" \
97 "dump array as value, default"
99 make_dump_file "dump val intstr1.bin intstruct" \
100 "dump struct as value, default"
102 make_dump_file "dump bin val intarr1b.bin intarray" \
103 "dump array as value, binary"
105 make_dump_file "dump bin val intstr1b.bin intstruct" \
106 "dump struct as value, binary"
108 make_dump_file "dump srec val intarr1.srec intarray" \
109 "dump array as value, srec"
111 make_dump_file "dump srec val intstr1.srec intstruct" \
112 "dump struct as value, srec"
114 make_dump_file "dump ihex val intarr1.ihex intarray" \
115 "dump array as value, intel hex"
117 make_dump_file "dump ihex val intstr1.ihex intstruct" \
118 "dump struct as value, intel hex"
120 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
121 "dump array as value, tekhex"
123 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
124 "dump struct as value, tekhex"
126 proc capture_value { expression args } {
131 if {[llength $args] > 0} {
132 # Convert $args into a simple string.
133 set test "[join $args]; capture $expression"
135 set test "capture $expression"
137 gdb_test_multiple "print ${expression}" "$test" {
138 -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
139 set output_string "$expect_out(1,string)"
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)"
148 return $output_string
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
156 proc capture_pointer_with_type { pointer } {
160 set test "capture type of pointer $pointer"
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
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".
176 return $output_string
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"]
184 set array_val [capture_value "intarray"]
185 set struct_val [capture_value "intstruct"]
187 set array_ptr_type [capture_pointer_with_type "&intarray"]
188 set struct_ptr_type [capture_pointer_with_type "&intstruct"]
190 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
191 "dump array as memory, default"
193 make_dump_file "dump mem intstr2.bin $struct_start $struct_end" \
194 "dump struct as memory, default"
196 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
197 "dump array as memory, binary"
199 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
200 "dump struct as memory, binary"
202 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
203 "dump array as memory, srec"
205 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
206 "dump struct as memory, srec"
208 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
209 "dump array as memory, ihex"
211 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
212 "dump struct as memory, ihex"
214 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
215 "dump array as memory, tekhex"
217 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
218 "dump struct as memory, tekhex"
220 # test complex expressions
222 "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
223 "dump array as mem, srec, expressions"
225 proc test_restore_saved_value { restore_args msg oldval newval } {
228 gdb_test "restore $restore_args" \
230 "$msg; file restored ok"
231 if { ![string compare $oldval \
232 [capture_value $newval "$msg"]] } then {
233 pass "$msg; value restored ok"
235 fail "$msg; value restored ok"
239 if ![string compare $is64bitonly "no"] then {
241 gdb_test "print zero_all ()" ".*"
243 test_restore_saved_value "intarr1.srec" "array as value, srec" \
244 $array_val "intarray"
246 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
247 $struct_val "intstruct"
249 gdb_test "print zero_all ()" "void" "zero all"
251 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
252 $array_val "intarray"
254 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
255 $struct_val "intstruct"
257 gdb_test "print zero_all ()" ".*"
259 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
260 $array_val "intarray"
262 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
263 $struct_val "intstruct"
265 gdb_test "print zero_all ()" ".*"
267 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
268 $array_val "intarray"
270 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
271 $struct_val "intstruct"
273 gdb_test "print zero_all ()" ".*"
275 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
276 $array_val "intarray"
278 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
279 $struct_val "intstruct"
281 gdb_test "print zero_all ()" ".*"
283 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
284 $array_val "intarray"
286 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
287 $struct_val "intstruct"
290 gdb_test "print zero_all ()" ".*"
292 test_restore_saved_value "intarr1.bin binary $array_start" \
293 "array as value, binary" \
294 $array_val "intarray"
296 test_restore_saved_value "intstr1.bin binary $struct_start" \
297 "struct as value, binary" \
298 $struct_val "intstruct"
300 gdb_test "print zero_all ()" ".*"
302 test_restore_saved_value "intarr2.bin binary $array_start" \
303 "array as memory, binary" \
304 $array_val "intarray"
306 test_restore_saved_value "intstr2.bin binary $struct_start" \
307 "struct as memory, binary" \
308 $struct_val "intstruct"
310 # test restore with offset.
312 set array2_start [capture_value "/x &intarray2\[0\]"]
313 set struct2_start [capture_value "/x &intstruct2"]
315 [capture_value "(char *) &intarray2 - (char *) &intarray"]
317 [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
319 gdb_test "print zero_all ()" ".*"
322 if ![string compare $is64bitonly "no"] then {
323 test_restore_saved_value "intarr1.srec $array2_offset" \
325 $array_val "intarray2"
327 test_restore_saved_value "intstr1.srec $struct2_offset" \
328 "struct copy, srec" \
329 $struct_val "intstruct2"
331 gdb_test "print zero_all ()" ".*"
333 test_restore_saved_value "intarr1.ihex $array2_offset" \
335 $array_val "intarray2"
337 test_restore_saved_value "intstr1.ihex $struct2_offset" \
338 "struct copy, ihex" \
339 $struct_val "intstruct2"
341 gdb_test "print zero_all ()" ".*"
343 test_restore_saved_value "intarr1.tekhex $array2_offset" \
344 "array copy, tekhex" \
345 $array_val "intarray2"
347 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
348 "struct copy, tekhex" \
349 $struct_val "intstruct2"
352 gdb_test "print zero_all ()" ".*"
354 test_restore_saved_value "intarr1.bin binary $array2_start" \
355 "array copy, binary" \
356 $array_val "intarray2"
358 test_restore_saved_value "intstr1.bin binary $struct2_start" \
359 "struct copy, binary" \
360 $struct_val "intstruct2"
363 # test restore with start/stop addresses.
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.
368 # We will need the address and offset of the third and fourth elements.
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\]"]
378 if ![string compare $is64bitonly "no"] then {
379 gdb_test "print zero_all ()" ".*"
381 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
382 "array partial, srec" 4 "intarray\[3\]"
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"
387 gdb_test "print zero_all ()" ".*"
389 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
390 "array partial, ihex" 4 "intarray\[3\]"
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"
395 gdb_test "print zero_all ()" ".*"
397 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
398 "array partial, tekhex" 4 "intarray\[3\]"
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"
404 gdb_test "print zero_all ()" ".*"
406 test_restore_saved_value \
407 "intarr1.bin binary $array_start $element3_offset $element4_offset" \
408 "array partial, binary" 4 "intarray\[3\]"
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"
413 if ![string compare $is64bitonly "no"] then {
414 gdb_test "print zero_all ()" ".*" ""
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\]"
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"
426 # Now start a fresh gdb session, and reload the saved value files.
430 gdb_file_cmd ${binfile}
432 # Now fix the endianness at the correct state.
434 gdb_test_multiple "set endian $endian" "set endianness" {
435 -re ".* (big|little) endian.*$gdb_prompt $" {
436 pass "setting $endian endianness"
440 # Reload saved values one by one, and compare.
442 if { ![string compare $array_val \
443 [capture_value "intarray" "file binfile"]] } then {
444 fail "start with intarray un-initialized"
446 pass "start with intarray un-initialized"
449 if { ![string compare $struct_val \
450 [capture_value "intstruct" "file binfile"]] } then {
451 fail "start with intstruct un-initialized"
453 pass "start with intstruct un-initialized"
456 proc test_reload_saved_value { filename msg oldval newval } {
459 gdb_file_cmd $filename
460 if { ![string compare $oldval \
461 [capture_value $newval "$msg"]] } then {
462 pass "$msg; value restored ok"
464 fail "$msg; value restored ok"
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"
480 # ihex format can not be loaded for 64-bit-only platforms
481 if ![string compare $is64bitonly "no"] then {
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"
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"
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"