1 # Copyright 2002-2013 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".
27 if [istarget "alpha*-*-*"] then {
28 # SREC etc cannot handle 64-bit addresses. Force the test
29 # program into the low 31 bits of the address space.
30 lappend options "additional_flags=-Wl,-taso"
33 if {[istarget "spu*-*-*"]} then {
34 # The internal address format used for the combined Cell/B.E.
35 # debugger requires 64-bit.
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
44 # Start with a fresh gdb.
48 gdb_reinitialize_dir $srcdir/$subdir
50 gdb_test "dump mem /dev/null 0x10 0x20" "Cannot access memory at address 0x10" \
51 "inaccessible memory is reported"
55 # Check the address of a variable. If it is bigger than 32-bit,
56 # assume our target has 64-bit addresses that are not supported by SREC,
57 # IHEX and TEKHEX. We skip those tests then.
58 set max_32bit_address "0xffffffff"
59 set data_address [get_hexadecimal_valueof "&intarray" 0x100000000]
60 if {${data_address} > ${max_32bit_address}} then {
64 # Clean up any stale output files from previous test runs
66 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"
70 # Run target program until data structs are initialized.
72 if { ! [ runto checkpoint1 ] } then {
77 # Get the endianness for the later use with endianless formats.
79 gdb_test_multiple "show endian" "show endian" {
80 -re ".* (big|little) endian.*$gdb_prompt $" {
81 set endian $expect_out(1,string)
82 pass "endianness: $endian"
86 # Now generate some dump files.
88 proc make_dump_file { command msg } {
91 gdb_test_multiple "${command}" "$msg" {
92 -re ".*\[Ee\]rror.*$gdb_prompt $" { fail $msg }
93 -re ".*\[Ww\]arning.*$gdb_prompt $" { fail $msg }
94 -re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
95 -re ".*$gdb_prompt $" { pass $msg }
99 make_dump_file "dump val intarr1.bin intarray" \
100 "dump array as value, default"
102 make_dump_file "dump val intstr1.bin intstruct" \
103 "dump struct as value, default"
105 make_dump_file "dump bin val intarr1b.bin intarray" \
106 "dump array as value, binary"
108 make_dump_file "dump bin val intstr1b.bin intstruct" \
109 "dump struct as value, binary"
111 make_dump_file "dump srec val intarr1.srec intarray" \
112 "dump array as value, srec"
114 make_dump_file "dump srec val intstr1.srec intstruct" \
115 "dump struct as value, srec"
117 make_dump_file "dump ihex val intarr1.ihex intarray" \
118 "dump array as value, intel hex"
120 make_dump_file "dump ihex val intstr1.ihex intstruct" \
121 "dump struct as value, intel hex"
123 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
124 "dump array as value, tekhex"
126 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
127 "dump struct as value, tekhex"
129 proc capture_value { expression args } {
134 if {[llength $args] > 0} {
135 # Convert $args into a simple string and don't use EXPRESSION
137 set test "[join $args]; capture"
139 set test "capture $expression"
141 gdb_test_multiple "print ${expression}" "$test" {
142 -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
143 set output_string "$expect_out(1,string)"
146 -re "(Cannot access memory at address \[^\r\n\]+).*$gdb_prompt $" {
147 # Even a failed value is valid
148 set output_string "$expect_out(1,string)"
152 return $output_string
155 # POINTER is a pointer and this proc captures the value of POINTER along
156 # with POINTER's type. For example, POINTER is "&intarray", this proc will
157 # call "p &intarray", capture "(int (*)[32]) 0x804a0e0", and return this
160 proc capture_pointer_with_type { pointer } {
164 set test "capture type of pointer $pointer"
166 gdb_test_multiple "p ${pointer}" $test {
167 -re "\\$\[0-9\]+ = .*$gdb_prompt $" {
168 # Expected output of "p ${pointer}" is like "$7 = (int (*)[32]) 0x804a0e0",
169 # and we want to extract "(int (*)[32]) 0x804a0e0" from it via
171 if [regexp " \\(.*\\).* 0x\[0-9a-fA-F\]+" $expect_out(0,string) output_string] {
172 # OUTPUT_STRING is expected to be like "(int (*)[32]) 0x804a0e0".
180 return $output_string
183 set array_start [capture_value "/x &intarray\[0\]"]
184 set array_end [capture_value "/x &intarray\[32\]"]
185 set struct_start [capture_value "/x &intstruct"]
186 set struct_end [capture_value "/x &intstruct + 1"]
188 set array_val [capture_value "intarray"]
189 set struct_val [capture_value "intstruct"]
191 set array_ptr_type [capture_pointer_with_type "&intarray"]
192 set struct_ptr_type [capture_pointer_with_type "&intstruct"]
194 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
195 "dump array as memory, default"
197 make_dump_file "dump mem intstr2.bin $struct_start $struct_end" \
198 "dump struct as memory, default"
200 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
201 "dump array as memory, binary"
203 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
204 "dump struct as memory, binary"
206 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
207 "dump array as memory, srec"
209 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
210 "dump struct as memory, srec"
212 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
213 "dump array as memory, ihex"
215 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
216 "dump struct as memory, ihex"
218 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
219 "dump array as memory, tekhex"
221 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
222 "dump struct as memory, tekhex"
224 # test complex expressions
226 "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
227 "dump array as mem, srec, expressions"
229 proc test_restore_saved_value { restore_args msg oldval newval } {
232 gdb_test "restore $restore_args" \
234 "$msg; file restored ok"
235 if { ![string compare $oldval \
236 [capture_value $newval "$msg"]] } then {
237 pass "$msg; value restored ok"
239 fail "$msg; value restored ok"
243 if ![string compare $is64bitonly "no"] then {
245 gdb_test "print zero_all ()" ".*"
247 test_restore_saved_value "intarr1.srec" "array as value, srec" \
248 $array_val "intarray"
250 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
251 $struct_val "intstruct"
253 gdb_test "print zero_all ()" "void" "zero all"
255 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
256 $array_val "intarray"
258 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
259 $struct_val "intstruct"
261 gdb_test "print zero_all ()" ".*"
263 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
264 $array_val "intarray"
266 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
267 $struct_val "intstruct"
269 gdb_test "print zero_all ()" ".*"
271 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
272 $array_val "intarray"
274 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
275 $struct_val "intstruct"
277 gdb_test "print zero_all ()" ".*"
279 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
280 $array_val "intarray"
282 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
283 $struct_val "intstruct"
285 gdb_test "print zero_all ()" ".*"
287 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
288 $array_val "intarray"
290 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
291 $struct_val "intstruct"
294 gdb_test "print zero_all ()" ".*"
296 test_restore_saved_value "intarr1.bin binary $array_start" \
297 "array as value, binary" \
298 $array_val "intarray"
300 test_restore_saved_value "intstr1.bin binary $struct_start" \
301 "struct as value, binary" \
302 $struct_val "intstruct"
304 gdb_test "print zero_all ()" ".*"
306 test_restore_saved_value "intarr2.bin binary $array_start" \
307 "array as memory, binary" \
308 $array_val "intarray"
310 test_restore_saved_value "intstr2.bin binary $struct_start" \
311 "struct as memory, binary" \
312 $struct_val "intstruct"
314 # test restore with offset.
316 set array2_start [capture_value "/x &intarray2\[0\]"]
317 set struct2_start [capture_value "/x &intstruct2"]
319 [capture_value "(char *) &intarray2 - (char *) &intarray"]
321 [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
323 gdb_test "print zero_all ()" ".*"
326 if ![string compare $is64bitonly "no"] then {
327 test_restore_saved_value "intarr1.srec $array2_offset" \
329 $array_val "intarray2"
331 test_restore_saved_value "intstr1.srec $struct2_offset" \
332 "struct copy, srec" \
333 $struct_val "intstruct2"
335 gdb_test "print zero_all ()" ".*"
337 test_restore_saved_value "intarr1.ihex $array2_offset" \
339 $array_val "intarray2"
341 test_restore_saved_value "intstr1.ihex $struct2_offset" \
342 "struct copy, ihex" \
343 $struct_val "intstruct2"
345 gdb_test "print zero_all ()" ".*"
347 test_restore_saved_value "intarr1.tekhex $array2_offset" \
348 "array copy, tekhex" \
349 $array_val "intarray2"
351 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
352 "struct copy, tekhex" \
353 $struct_val "intstruct2"
356 gdb_test "print zero_all ()" ".*"
358 test_restore_saved_value "intarr1.bin binary $array2_start" \
359 "array copy, binary" \
360 $array_val "intarray2"
362 test_restore_saved_value "intstr1.bin binary $struct2_start" \
363 "struct copy, binary" \
364 $struct_val "intstruct2"
367 # test restore with start/stop addresses.
369 # For this purpose, we will restore just the third element of the array,
370 # and check to see that adjacent elements are not modified.
372 # We will need the address and offset of the third and fourth elements.
375 set element3_start [capture_value "/x &intarray\[3\]"]
376 set element4_start [capture_value "/x &intarray\[4\]"]
377 set element3_offset \
378 [capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
379 set element4_offset \
380 [capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
382 if ![string compare $is64bitonly "no"] then {
383 gdb_test "print zero_all ()" ".*"
385 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
386 "array partial, srec" 4 "intarray\[3\]"
388 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
389 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
391 gdb_test "print zero_all ()" ".*"
393 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
394 "array partial, ihex" 4 "intarray\[3\]"
396 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
397 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
399 gdb_test "print zero_all ()" ".*"
401 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
402 "array partial, tekhex" 4 "intarray\[3\]"
404 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
405 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
408 gdb_test "print zero_all ()" ".*"
410 test_restore_saved_value \
411 "intarr1.bin binary $array_start $element3_offset $element4_offset" \
412 "array partial, binary" 4 "intarray\[3\]"
414 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
415 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
417 if ![string compare $is64bitonly "no"] then {
418 gdb_test "print zero_all ()" ".*" ""
420 # restore with expressions
421 test_restore_saved_value \
422 "intarr3.srec (char*)${array2_start}-(char*)${array_start} &intarray\[3\] &intarray\[4\]" \
423 "array partial with expressions" 4 "intarray2\[3\]"
425 gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
426 gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
430 # Now start a fresh gdb session, and reload the saved value files.
434 gdb_file_cmd ${binfile}
436 # Now fix the endianness at the correct state.
438 gdb_test_multiple "set endian $endian" "set endianness" {
439 -re ".* (big|little) endian.*$gdb_prompt $" {
440 pass "setting $endian endianness"
444 # Reload saved values one by one, and compare.
446 if { ![string compare $array_val \
447 [capture_value "intarray" "file binfile; intarray"]] } then {
448 fail "start with intarray un-initialized"
450 pass "start with intarray un-initialized"
453 if { ![string compare $struct_val \
454 [capture_value "intstruct" "file binfile; intstruct"]] } then {
455 fail "start with intstruct un-initialized"
457 pass "start with intstruct un-initialized"
460 proc test_reload_saved_value { filename msg oldval newval } {
463 gdb_file_cmd $filename
464 if { ![string compare $oldval \
465 [capture_value $newval "$msg"]] } then {
466 pass "$msg; value restored ok"
468 fail "$msg; value restored ok"
472 # srec format can not be loaded for 64-bit-only platforms
473 if ![string compare $is64bitonly "no"] then {
474 test_reload_saved_value "intarr1.srec" "reload array as value, srec" \
475 $array_val "\*$array_ptr_type"
476 test_reload_saved_value "intstr1.srec" "reload struct as value, srec" \
477 $struct_val "\*$struct_ptr_type"
478 test_reload_saved_value "intarr2.srec" "reload array as memory, srec" \
479 $array_val "\*$array_ptr_type"
480 test_reload_saved_value "intstr2.srec" "reload struct as memory, srec" \
481 $struct_val "\*$struct_ptr_type"
484 # ihex format can not be loaded for 64-bit-only platforms
485 if ![string compare $is64bitonly "no"] then {
487 test_reload_saved_value "intarr1.ihex" "reload array as value, intel hex" \
488 $array_val "\*$array_ptr_type"
489 test_reload_saved_value "intstr1.ihex" "reload struct as value, intel hex" \
490 $struct_val "\*$struct_ptr_type"
491 test_reload_saved_value "intarr2.ihex" "reload array as memory, intel hex" \
492 $array_val "\*$array_ptr_type"
493 test_reload_saved_value "intstr2.ihex" "reload struct as memory, intel hex" \
494 $struct_val "\*$struct_ptr_type"
497 # tekhex format can not be loaded for 64-bit-only platforms
498 if ![string compare $is64bitonly "no"] then {
499 test_reload_saved_value "intarr1.tekhex" "reload array as value, tekhex" \
500 $array_val "\*$array_ptr_type"
501 test_reload_saved_value "intstr1.tekhex" "reload struct as value, tekhex" \
502 $struct_val "\*$struct_ptr_type"
503 test_reload_saved_value "intarr2.tekhex" "reload array as memory, tekhex" \
504 $array_val "\*$array_ptr_type"
505 test_reload_saved_value "intstr2.tekhex" "reload struct as memory, tekhex" \
506 $struct_val "\*$struct_ptr_type"
511 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"