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 { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
50 # Start with a fresh gdb.
54 gdb_reinitialize_dir $srcdir/$subdir
57 # Clean up any stale output files from previous test runs
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"
63 # Run target program until data structs are initialized.
65 if { ! [ runto checkpoint1 ] } then {
70 # Get the endianness for the later use with endianless formats.
72 send_gdb "show endian\n"
74 -re ".* (big|little) endian.*$gdb_prompt $" {
75 set endian $expect_out(1,string)
76 pass "endianness: $endian"
79 fail "(timeout) getting target endianness"
83 # Now generate some dump files.
85 proc make_dump_file { command msg } {
88 send_gdb "${command}\n"
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)" }
98 make_dump_file "dump val intarr1.bin intarray" \
99 "dump array as value, default"
101 make_dump_file "dump val intstr1.bin intstruct" \
102 "dump struct as value, default"
104 make_dump_file "dump bin val intarr1b.bin intarray" \
105 "dump array as value, binary"
107 make_dump_file "dump bin val intstr1b.bin intstruct" \
108 "dump struct as value, binary"
110 make_dump_file "dump srec val intarr1.srec intarray" \
111 "dump array as value, srec"
113 make_dump_file "dump srec val intstr1.srec intstruct" \
114 "dump struct as value, srec"
116 make_dump_file "dump ihex val intarr1.ihex intarray" \
117 "dump array as value, intel hex"
119 make_dump_file "dump ihex val intstr1.ihex intstruct" \
120 "dump struct as value, intel hex"
122 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
123 "dump array as value, tekhex"
125 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
126 "dump struct as value, tekhex"
128 proc capture_value { expression args } {
133 if {[llength $args] > 0} {
134 # Convert $args into a simple string.
135 set test "[join $args]; capture $expression"
137 set test "capture $expression"
139 gdb_test_multiple "print ${expression}" "$test" {
140 -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
141 set output_string "$expect_out(1,string)"
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)"
150 return $output_string
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"]
158 set array_val [capture_value "intarray"]
159 set struct_val [capture_value "intstruct"]
161 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
162 "dump array as memory, default"
164 make_dump_file "dump mem intstr2.bin $struct_start $struct_end" \
165 "dump struct as memory, default"
167 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
168 "dump array as memory, binary"
170 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
171 "dump struct as memory, binary"
173 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
174 "dump array as memory, srec"
176 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
177 "dump struct as memory, srec"
179 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
180 "dump array as memory, ihex"
182 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
183 "dump struct as memory, ihex"
185 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
186 "dump array as memory, tekhex"
188 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
189 "dump struct as memory, tekhex"
191 # test complex expressions
193 "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
194 "dump array as mem, srec, expressions"
197 # Now start a fresh gdb session, and reload the saved value files.
201 gdb_file_cmd ${binfile}
203 # Now fix the endianness at the correct state.
205 send_gdb "set endian $endian\n"
207 -re ".* (big|little) endian.*$gdb_prompt $" {
208 pass "setting $endian endianness"
211 fail "(timeout) setting $endian endianness"
215 # Reload saved values one by one, and compare.
217 if { ![string compare $array_val \
218 [capture_value "intarray" "file binfile"]] } then {
219 fail "start with intarray un-initialized"
221 pass "start with intarray un-initialized"
224 if { ![string compare $struct_val \
225 [capture_value "intstruct" "file binfile"]] } then {
226 fail "start with intstruct un-initialized"
228 pass "start with intstruct un-initialized"
231 proc test_reload_saved_value { filename msg oldval newval } {
234 gdb_file_cmd $filename
235 if { ![string compare $oldval \
236 [capture_value $newval "$msg"]] } then {
237 pass "$msg; value restored ok"
239 fail "$msg; value restored ok"
243 proc test_restore_saved_value { restore_args msg oldval newval } {
246 gdb_test "restore $restore_args" \
248 "$msg; file restored ok"
249 if { ![string compare $oldval \
250 [capture_value $newval "$msg"]] } then {
251 pass "$msg; value restored ok"
253 fail "$msg; value restored ok"
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"
269 # ihex format can not be loaded for 64-bit-only platforms
270 if ![string compare $is64bitonly "no"] then {
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"
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"
294 # Start a fresh gdb session
298 gdb_reinitialize_dir $srcdir/$subdir
302 if { ! [ runto_main ] } then {
307 if { ![string compare $array_val \
308 [capture_value "intarray" "load binfile"]] } then {
309 fail "start with intarray un-initialized, runto main"
311 pass "start with intarray un-initialized, runto main"
314 if { ![string compare $struct_val \
315 [capture_value "intstruct" "load binfile"]] } then {
316 fail "start with intstruct un-initialized, runto main"
318 pass "start with intstruct un-initialized, runto main"
321 if ![string compare $is64bitonly "no"] then {
322 test_restore_saved_value "intarr1.srec" "array as value, srec" \
323 $array_val "intarray"
325 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
326 $struct_val "intstruct"
328 gdb_test "print zero_all ()" "void" "zero all"
330 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
331 $array_val "intarray"
333 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
334 $struct_val "intstruct"
336 gdb_test "print zero_all ()" ""
338 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
339 $array_val "intarray"
341 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
342 $struct_val "intstruct"
344 gdb_test "print zero_all ()" ""
346 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
347 $array_val "intarray"
349 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
350 $struct_val "intstruct"
352 gdb_test "print zero_all ()" ""
354 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
355 $array_val "intarray"
357 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
358 $struct_val "intstruct"
360 gdb_test "print zero_all ()" ""
362 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
363 $array_val "intarray"
365 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
366 $struct_val "intstruct"
369 gdb_test "print zero_all ()" ""
371 test_restore_saved_value "intarr1.bin binary $array_start" \
372 "array as value, binary" \
373 $array_val "intarray"
375 test_restore_saved_value "intstr1.bin binary $struct_start" \
376 "struct as value, binary" \
377 $struct_val "intstruct"
379 gdb_test "print zero_all ()" ""
381 test_restore_saved_value "intarr2.bin binary $array_start" \
382 "array as memory, binary" \
383 $array_val "intarray"
385 test_restore_saved_value "intstr2.bin binary $struct_start" \
386 "struct as memory, binary" \
387 $struct_val "intstruct"
389 # test restore with offset.
391 set array2_start [capture_value "/x &intarray2\[0\]"]
392 set struct2_start [capture_value "/x &intstruct2"]
394 [capture_value "(char *) &intarray2 - (char *) &intarray"]
396 [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
398 gdb_test "print zero_all ()" ""
401 if ![string compare $is64bitonly "no"] then {
402 test_restore_saved_value "intarr1.srec $array2_offset" \
404 $array_val "intarray2"
406 test_restore_saved_value "intstr1.srec $struct2_offset" \
407 "struct copy, srec" \
408 $struct_val "intstruct2"
410 gdb_test "print zero_all ()" ""
412 test_restore_saved_value "intarr1.ihex $array2_offset" \
414 $array_val "intarray2"
416 test_restore_saved_value "intstr1.ihex $struct2_offset" \
417 "struct copy, ihex" \
418 $struct_val "intstruct2"
420 gdb_test "print zero_all ()" ""
422 test_restore_saved_value "intarr1.tekhex $array2_offset" \
423 "array copy, tekhex" \
424 $array_val "intarray2"
426 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
427 "struct copy, tekhex" \
428 $struct_val "intstruct2"
431 gdb_test "print zero_all ()" ""
433 test_restore_saved_value "intarr1.bin binary $array2_start" \
434 "array copy, binary" \
435 $array_val "intarray2"
437 test_restore_saved_value "intstr1.bin binary $struct2_start" \
438 "struct copy, binary" \
439 $struct_val "intstruct2"
442 # test restore with start/stop addresses.
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.
447 # We will need the address and offset of the third and fourth elements.
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\]"]
457 if ![string compare $is64bitonly "no"] then {
458 gdb_test "print zero_all ()" ""
460 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
461 "array partial, srec" 4 "intarray\[3\]"
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"
466 gdb_test "print zero_all ()" ""
468 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
469 "array partial, ihex" 4 "intarray\[3\]"
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"
474 gdb_test "print zero_all ()" ""
476 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
477 "array partial, tekhex" 4 "intarray\[3\]"
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"
483 gdb_test "print zero_all ()" ""
485 test_restore_saved_value \
486 "intarr1.bin binary $array_start $element3_offset $element4_offset" \
487 "array partial, binary" 4 "intarray\[3\]"
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"
492 if ![string compare $is64bitonly "no"] then {
493 gdb_test "print zero_all ()" "" ""
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\]"
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"
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"