1 # Copyright 2002, 2004, 2007, 2008, 2009 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".
28 set srcfile ${testfile}.c
29 set binfile ${objdir}/${subdir}/${testfile}
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"
41 if {[istarget "ia64*-*-*"] || [istarget "hppa64-*-*"]} then {
45 if {[istarget "spu*-*-*"]} then {
46 # The internal address format used for the combined Cell/B.E.
47 # debugger requires 64-bit.
51 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
56 # Start with a fresh gdb.
60 gdb_reinitialize_dir $srcdir/$subdir
62 gdb_test "dump mem /dev/null 0x10 0x20" "Cannot access memory at address 0x10" \
63 "inaccessible memory is reported"
67 # Clean up any stale output files from previous test runs
69 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"
73 # Run target program until data structs are initialized.
75 if { ! [ runto checkpoint1 ] } then {
80 # Get the endianness for the later use with endianless formats.
82 send_gdb "show endian\n"
84 -re ".* (big|little) endian.*$gdb_prompt $" {
85 set endian $expect_out(1,string)
86 pass "endianness: $endian"
89 fail "(timeout) getting target endianness"
93 # Now generate some dump files.
95 proc make_dump_file { command msg } {
98 send_gdb "${command}\n"
100 -re ".*\[Ee\]rror.*$gdb_prompt $" { fail $msg }
101 -re ".*\[Ww\]arning.*$gdb_prompt $" { fail $msg }
102 -re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
103 -re ".*$gdb_prompt $" { pass $msg }
104 timeout { fail "$msg (timeout)" }
108 make_dump_file "dump val intarr1.bin intarray" \
109 "dump array as value, default"
111 make_dump_file "dump val intstr1.bin intstruct" \
112 "dump struct as value, default"
114 make_dump_file "dump bin val intarr1b.bin intarray" \
115 "dump array as value, binary"
117 make_dump_file "dump bin val intstr1b.bin intstruct" \
118 "dump struct as value, binary"
120 make_dump_file "dump srec val intarr1.srec intarray" \
121 "dump array as value, srec"
123 make_dump_file "dump srec val intstr1.srec intstruct" \
124 "dump struct as value, srec"
126 make_dump_file "dump ihex val intarr1.ihex intarray" \
127 "dump array as value, intel hex"
129 make_dump_file "dump ihex val intstr1.ihex intstruct" \
130 "dump struct as value, intel hex"
132 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
133 "dump array as value, tekhex"
135 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
136 "dump struct as value, tekhex"
138 proc capture_value { expression args } {
143 if {[llength $args] > 0} {
144 # Convert $args into a simple string.
145 set test "[join $args]; capture $expression"
147 set test "capture $expression"
149 gdb_test_multiple "print ${expression}" "$test" {
150 -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
151 set output_string "$expect_out(1,string)"
154 -re "(Cannot access memory at address \[^\r\n\]+).*$gdb_prompt $" {
155 # Even a failed value is valid
156 set output_string "$expect_out(1,string)"
160 return $output_string
163 set array_start [capture_value "/x &intarray\[0\]"]
164 set array_end [capture_value "/x &intarray\[32\]"]
165 set struct_start [capture_value "/x &intstruct"]
166 set struct_end [capture_value "/x &intstruct + 1"]
168 set array_val [capture_value "intarray"]
169 set struct_val [capture_value "intstruct"]
171 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
172 "dump array as memory, default"
174 make_dump_file "dump mem intstr2.bin $struct_start $struct_end" \
175 "dump struct as memory, default"
177 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
178 "dump array as memory, binary"
180 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
181 "dump struct as memory, binary"
183 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
184 "dump array as memory, srec"
186 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
187 "dump struct as memory, srec"
189 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
190 "dump array as memory, ihex"
192 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
193 "dump struct as memory, ihex"
195 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
196 "dump array as memory, tekhex"
198 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
199 "dump struct as memory, tekhex"
201 # test complex expressions
203 "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
204 "dump array as mem, srec, expressions"
207 # Now start a fresh gdb session, and reload the saved value files.
211 gdb_file_cmd ${binfile}
213 # Now fix the endianness at the correct state.
215 send_gdb "set endian $endian\n"
217 -re ".* (big|little) endian.*$gdb_prompt $" {
218 pass "setting $endian endianness"
221 fail "(timeout) setting $endian endianness"
225 # Reload saved values one by one, and compare.
227 if { ![string compare $array_val \
228 [capture_value "intarray" "file binfile"]] } then {
229 fail "start with intarray un-initialized"
231 pass "start with intarray un-initialized"
234 if { ![string compare $struct_val \
235 [capture_value "intstruct" "file binfile"]] } then {
236 fail "start with intstruct un-initialized"
238 pass "start with intstruct un-initialized"
241 proc test_reload_saved_value { filename msg oldval newval } {
244 gdb_file_cmd $filename
245 if { ![string compare $oldval \
246 [capture_value $newval "$msg"]] } then {
247 pass "$msg; value restored ok"
249 fail "$msg; value restored ok"
253 proc test_restore_saved_value { restore_args msg oldval newval } {
256 gdb_test "restore $restore_args" \
258 "$msg; file restored ok"
259 if { ![string compare $oldval \
260 [capture_value $newval "$msg"]] } then {
261 pass "$msg; value restored ok"
263 fail "$msg; value restored ok"
267 # srec format can not be loaded for 64-bit-only platforms
268 if ![string compare $is64bitonly "no"] then {
269 test_reload_saved_value "intarr1.srec" "reload array as value, srec" \
270 $array_val "intarray"
271 test_reload_saved_value "intstr1.srec" "reload struct as value, srec" \
272 $struct_val "intstruct"
273 test_reload_saved_value "intarr2.srec" "reload array as memory, srec" \
274 $array_val "intarray"
275 test_reload_saved_value "intstr2.srec" "reload struct as memory, srec" \
276 $struct_val "intstruct"
279 # ihex format can not be loaded for 64-bit-only platforms
280 if ![string compare $is64bitonly "no"] then {
282 test_reload_saved_value "intarr1.ihex" "reload array as value, intel hex" \
283 $array_val "intarray"
284 test_reload_saved_value "intstr1.ihex" "reload struct as value, intel hex" \
285 $struct_val "intstruct"
286 test_reload_saved_value "intarr2.ihex" "reload array as memory, intel hex" \
287 $array_val "intarray"
288 test_reload_saved_value "intstr2.ihex" "reload struct as memory, intel hex" \
289 $struct_val "intstruct"
292 # tekhex format can not be loaded for 64-bit-only platforms
293 if ![string compare $is64bitonly "no"] then {
294 test_reload_saved_value "intarr1.tekhex" "reload array as value, tekhex" \
295 $array_val "intarray"
296 test_reload_saved_value "intstr1.tekhex" "reload struct as value, tekhex" \
297 $struct_val "intstruct"
298 test_reload_saved_value "intarr2.tekhex" "reload array as memory, tekhex" \
299 $array_val "intarray"
300 test_reload_saved_value "intstr2.tekhex" "reload struct as memory, tekhex" \
301 $struct_val "intstruct"
304 # Start a fresh gdb session
308 gdb_reinitialize_dir $srcdir/$subdir
312 if { ! [ runto_main ] } then {
317 if { ![string compare $array_val \
318 [capture_value "intarray" "load binfile"]] } then {
319 fail "start with intarray un-initialized, runto main"
321 pass "start with intarray un-initialized, runto main"
324 if { ![string compare $struct_val \
325 [capture_value "intstruct" "load binfile"]] } then {
326 fail "start with intstruct un-initialized, runto main"
328 pass "start with intstruct un-initialized, runto main"
331 if ![string compare $is64bitonly "no"] then {
332 test_restore_saved_value "intarr1.srec" "array as value, srec" \
333 $array_val "intarray"
335 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
336 $struct_val "intstruct"
338 gdb_test "print zero_all ()" "void" "zero all"
340 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
341 $array_val "intarray"
343 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
344 $struct_val "intstruct"
346 gdb_test "print zero_all ()" ""
348 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
349 $array_val "intarray"
351 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
352 $struct_val "intstruct"
354 gdb_test "print zero_all ()" ""
356 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
357 $array_val "intarray"
359 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
360 $struct_val "intstruct"
362 gdb_test "print zero_all ()" ""
364 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
365 $array_val "intarray"
367 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
368 $struct_val "intstruct"
370 gdb_test "print zero_all ()" ""
372 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
373 $array_val "intarray"
375 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
376 $struct_val "intstruct"
379 gdb_test "print zero_all ()" ""
381 test_restore_saved_value "intarr1.bin binary $array_start" \
382 "array as value, binary" \
383 $array_val "intarray"
385 test_restore_saved_value "intstr1.bin binary $struct_start" \
386 "struct as value, binary" \
387 $struct_val "intstruct"
389 gdb_test "print zero_all ()" ""
391 test_restore_saved_value "intarr2.bin binary $array_start" \
392 "array as memory, binary" \
393 $array_val "intarray"
395 test_restore_saved_value "intstr2.bin binary $struct_start" \
396 "struct as memory, binary" \
397 $struct_val "intstruct"
399 # test restore with offset.
401 set array2_start [capture_value "/x &intarray2\[0\]"]
402 set struct2_start [capture_value "/x &intstruct2"]
404 [capture_value "(char *) &intarray2 - (char *) &intarray"]
406 [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
408 gdb_test "print zero_all ()" ""
411 if ![string compare $is64bitonly "no"] then {
412 test_restore_saved_value "intarr1.srec $array2_offset" \
414 $array_val "intarray2"
416 test_restore_saved_value "intstr1.srec $struct2_offset" \
417 "struct copy, srec" \
418 $struct_val "intstruct2"
420 gdb_test "print zero_all ()" ""
422 test_restore_saved_value "intarr1.ihex $array2_offset" \
424 $array_val "intarray2"
426 test_restore_saved_value "intstr1.ihex $struct2_offset" \
427 "struct copy, ihex" \
428 $struct_val "intstruct2"
430 gdb_test "print zero_all ()" ""
432 test_restore_saved_value "intarr1.tekhex $array2_offset" \
433 "array copy, tekhex" \
434 $array_val "intarray2"
436 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
437 "struct copy, tekhex" \
438 $struct_val "intstruct2"
441 gdb_test "print zero_all ()" ""
443 test_restore_saved_value "intarr1.bin binary $array2_start" \
444 "array copy, binary" \
445 $array_val "intarray2"
447 test_restore_saved_value "intstr1.bin binary $struct2_start" \
448 "struct copy, binary" \
449 $struct_val "intstruct2"
452 # test restore with start/stop addresses.
454 # For this purpose, we will restore just the third element of the array,
455 # and check to see that adjacent elements are not modified.
457 # We will need the address and offset of the third and fourth elements.
460 set element3_start [capture_value "/x &intarray\[3\]"]
461 set element4_start [capture_value "/x &intarray\[4\]"]
462 set element3_offset \
463 [capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
464 set element4_offset \
465 [capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
467 if ![string compare $is64bitonly "no"] then {
468 gdb_test "print zero_all ()" ""
470 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
471 "array partial, srec" 4 "intarray\[3\]"
473 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
474 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
476 gdb_test "print zero_all ()" ""
478 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
479 "array partial, ihex" 4 "intarray\[3\]"
481 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
482 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
484 gdb_test "print zero_all ()" ""
486 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
487 "array partial, tekhex" 4 "intarray\[3\]"
489 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
490 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
493 gdb_test "print zero_all ()" ""
495 test_restore_saved_value \
496 "intarr1.bin binary $array_start $element3_offset $element4_offset" \
497 "array partial, binary" 4 "intarray\[3\]"
499 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
500 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
502 if ![string compare $is64bitonly "no"] then {
503 gdb_test "print zero_all ()" "" ""
505 # restore with expressions
506 test_restore_saved_value \
507 "intarr3.srec (char*)${array2_start}-(char*)${array_start} &intarray\[3\] &intarray\[4\]" \
508 "array partial with expressions" 4 "intarray2\[3\]"
510 gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
511 gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
516 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"