1 # Copyright 2002, 2004, 2007, 2008 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 # Please email any bugs, comments, and/or additions to this file to:
17 # bug-gdb@prep.ai.mit.edu
19 # This file was written by Michael Snyder (msnyder@redhat.com)
20 # This is a test for the gdb command "dump".
31 set srcfile ${testfile}.c
32 set binfile ${objdir}/${subdir}/${testfile}
38 if [istarget "alpha*-*-*"] then {
39 # SREC etc cannot handle 64-bit addresses. Force the test
40 # program into the low 31 bits of the address space.
41 lappend options "additional_flags=-Wl,-taso"
44 if {[istarget "ia64*-*-*"] || [istarget "hppa64-*-*"]} then {
48 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
53 # Start with a fresh gdb.
57 gdb_reinitialize_dir $srcdir/$subdir
60 # Clean up any stale output files from previous test runs
62 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"
66 # Run target program until data structs are initialized.
68 if { ! [ runto checkpoint1 ] } then {
73 # Get the endianness for the later use with endianless formats.
75 send_gdb "show endian\n"
77 -re ".* (big|little) endian.*$gdb_prompt $" {
78 set endian $expect_out(1,string)
79 pass "endianness: $endian"
82 fail "(timeout) getting target endianness"
86 # Now generate some dump files.
88 proc make_dump_file { command msg } {
91 send_gdb "${command}\n"
93 -re ".*\[Ee\]rror.*$gdb_prompt $" { fail $msg }
94 -re ".*\[Ww\]arning.*$gdb_prompt $" { fail $msg }
95 -re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
96 -re ".*$gdb_prompt $" { pass $msg }
97 timeout { fail "$msg (timeout)" }
101 make_dump_file "dump val intarr1.bin intarray" \
102 "dump array as value, default"
104 make_dump_file "dump val intstr1.bin intstruct" \
105 "dump struct as value, default"
107 make_dump_file "dump bin val intarr1b.bin intarray" \
108 "dump array as value, binary"
110 make_dump_file "dump bin val intstr1b.bin intstruct" \
111 "dump struct as value, binary"
113 make_dump_file "dump srec val intarr1.srec intarray" \
114 "dump array as value, srec"
116 make_dump_file "dump srec val intstr1.srec intstruct" \
117 "dump struct as value, srec"
119 make_dump_file "dump ihex val intarr1.ihex intarray" \
120 "dump array as value, intel hex"
122 make_dump_file "dump ihex val intstr1.ihex intstruct" \
123 "dump struct as value, intel hex"
125 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
126 "dump array as value, tekhex"
128 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
129 "dump struct as value, tekhex"
131 proc capture_value { expression args } {
136 if {[llength $args] > 0} {
137 # Convert $args into a simple string.
138 set test "[join $args]; capture $expression"
140 set test "capture $expression"
142 gdb_test_multiple "print ${expression}" "$test" {
143 -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
144 set output_string "$expect_out(1,string)"
147 -re "(Cannot access memory at address \[^\r\n\]+).*$gdb_prompt $" {
148 # Even a failed value is valid
149 set output_string "$expect_out(1,string)"
153 return $output_string
156 set array_start [capture_value "/x &intarray\[0\]"]
157 set array_end [capture_value "/x &intarray\[32\]"]
158 set struct_start [capture_value "/x &intstruct"]
159 set struct_end [capture_value "/x &intstruct + 1"]
161 set array_val [capture_value "intarray"]
162 set struct_val [capture_value "intstruct"]
164 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
165 "dump array as memory, default"
167 make_dump_file "dump mem intstr2.bin $struct_start $struct_end" \
168 "dump struct as memory, default"
170 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
171 "dump array as memory, binary"
173 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
174 "dump struct as memory, binary"
176 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
177 "dump array as memory, srec"
179 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
180 "dump struct as memory, srec"
182 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
183 "dump array as memory, ihex"
185 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
186 "dump struct as memory, ihex"
188 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
189 "dump array as memory, tekhex"
191 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
192 "dump struct as memory, tekhex"
194 # test complex expressions
196 "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
197 "dump array as mem, srec, expressions"
200 # Now start a fresh gdb session, and reload the saved value files.
204 gdb_file_cmd ${binfile}
206 # Now fix the endianness at the correct state.
208 send_gdb "set endian $endian\n"
210 -re ".* (big|little) endian.*$gdb_prompt $" {
211 pass "setting $endian endianness"
214 fail "(timeout) setting $endian endianness"
218 # Reload saved values one by one, and compare.
220 if { ![string compare $array_val \
221 [capture_value "intarray" "file binfile"]] } then {
222 fail "start with intarray un-initialized"
224 pass "start with intarray un-initialized"
227 if { ![string compare $struct_val \
228 [capture_value "intstruct" "file binfile"]] } then {
229 fail "start with intstruct un-initialized"
231 pass "start with intstruct un-initialized"
234 proc test_reload_saved_value { filename msg oldval newval } {
237 gdb_file_cmd $filename
238 if { ![string compare $oldval \
239 [capture_value $newval "$msg"]] } then {
240 pass "$msg; value restored ok"
242 fail "$msg; value restored ok"
246 proc test_restore_saved_value { restore_args msg oldval newval } {
249 gdb_test "restore $restore_args" \
251 "$msg; file restored ok"
252 if { ![string compare $oldval \
253 [capture_value $newval "$msg"]] } then {
254 pass "$msg; value restored ok"
256 fail "$msg; value restored ok"
260 # srec format can not be loaded for 64-bit-only platforms
261 if ![string compare $is64bitonly "no"] then {
262 test_reload_saved_value "intarr1.srec" "reload array as value, srec" \
263 $array_val "intarray"
264 test_reload_saved_value "intstr1.srec" "reload struct as value, srec" \
265 $struct_val "intstruct"
266 test_reload_saved_value "intarr2.srec" "reload array as memory, srec" \
267 $array_val "intarray"
268 test_reload_saved_value "intstr2.srec" "reload struct as memory, srec" \
269 $struct_val "intstruct"
272 # ihex format can not be loaded for 64-bit-only platforms
273 if ![string compare $is64bitonly "no"] then {
275 test_reload_saved_value "intarr1.ihex" "reload array as value, intel hex" \
276 $array_val "intarray"
277 test_reload_saved_value "intstr1.ihex" "reload struct as value, intel hex" \
278 $struct_val "intstruct"
279 test_reload_saved_value "intarr2.ihex" "reload array as memory, intel hex" \
280 $array_val "intarray"
281 test_reload_saved_value "intstr2.ihex" "reload struct as memory, intel hex" \
282 $struct_val "intstruct"
285 # tekhex format can not be loaded for 64-bit-only platforms
286 if ![string compare $is64bitonly "no"] then {
287 test_reload_saved_value "intarr1.tekhex" "reload array as value, tekhex" \
288 $array_val "intarray"
289 test_reload_saved_value "intstr1.tekhex" "reload struct as value, tekhex" \
290 $struct_val "intstruct"
291 test_reload_saved_value "intarr2.tekhex" "reload array as memory, tekhex" \
292 $array_val "intarray"
293 test_reload_saved_value "intstr2.tekhex" "reload struct as memory, tekhex" \
294 $struct_val "intstruct"
297 # Start a fresh gdb session
301 gdb_reinitialize_dir $srcdir/$subdir
305 if { ! [ runto_main ] } then {
310 if { ![string compare $array_val \
311 [capture_value "intarray" "load binfile"]] } then {
312 fail "start with intarray un-initialized, runto main"
314 pass "start with intarray un-initialized, runto main"
317 if { ![string compare $struct_val \
318 [capture_value "intstruct" "load binfile"]] } then {
319 fail "start with intstruct un-initialized, runto main"
321 pass "start with intstruct un-initialized, runto main"
324 if ![string compare $is64bitonly "no"] then {
325 test_restore_saved_value "intarr1.srec" "array as value, srec" \
326 $array_val "intarray"
328 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
329 $struct_val "intstruct"
331 gdb_test "print zero_all ()" "void" "zero all"
333 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
334 $array_val "intarray"
336 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
337 $struct_val "intstruct"
339 gdb_test "print zero_all ()" ""
341 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
342 $array_val "intarray"
344 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
345 $struct_val "intstruct"
347 gdb_test "print zero_all ()" ""
349 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
350 $array_val "intarray"
352 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
353 $struct_val "intstruct"
355 gdb_test "print zero_all ()" ""
357 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
358 $array_val "intarray"
360 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
361 $struct_val "intstruct"
363 gdb_test "print zero_all ()" ""
365 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
366 $array_val "intarray"
368 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
369 $struct_val "intstruct"
372 gdb_test "print zero_all ()" ""
374 test_restore_saved_value "intarr1.bin binary $array_start" \
375 "array as value, binary" \
376 $array_val "intarray"
378 test_restore_saved_value "intstr1.bin binary $struct_start" \
379 "struct as value, binary" \
380 $struct_val "intstruct"
382 gdb_test "print zero_all ()" ""
384 test_restore_saved_value "intarr2.bin binary $array_start" \
385 "array as memory, binary" \
386 $array_val "intarray"
388 test_restore_saved_value "intstr2.bin binary $struct_start" \
389 "struct as memory, binary" \
390 $struct_val "intstruct"
392 # test restore with offset.
394 set array2_start [capture_value "/x &intarray2\[0\]"]
395 set struct2_start [capture_value "/x &intstruct2"]
397 [capture_value "(char *) &intarray2 - (char *) &intarray"]
399 [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
401 gdb_test "print zero_all ()" ""
404 if ![string compare $is64bitonly "no"] then {
405 test_restore_saved_value "intarr1.srec $array2_offset" \
407 $array_val "intarray2"
409 test_restore_saved_value "intstr1.srec $struct2_offset" \
410 "struct copy, srec" \
411 $struct_val "intstruct2"
413 gdb_test "print zero_all ()" ""
415 test_restore_saved_value "intarr1.ihex $array2_offset" \
417 $array_val "intarray2"
419 test_restore_saved_value "intstr1.ihex $struct2_offset" \
420 "struct copy, ihex" \
421 $struct_val "intstruct2"
423 gdb_test "print zero_all ()" ""
425 test_restore_saved_value "intarr1.tekhex $array2_offset" \
426 "array copy, tekhex" \
427 $array_val "intarray2"
429 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
430 "struct copy, tekhex" \
431 $struct_val "intstruct2"
434 gdb_test "print zero_all ()" ""
436 test_restore_saved_value "intarr1.bin binary $array2_start" \
437 "array copy, binary" \
438 $array_val "intarray2"
440 test_restore_saved_value "intstr1.bin binary $struct2_start" \
441 "struct copy, binary" \
442 $struct_val "intstruct2"
445 # test restore with start/stop addresses.
447 # For this purpose, we will restore just the third element of the array,
448 # and check to see that adjacent elements are not modified.
450 # We will need the address and offset of the third and fourth elements.
453 set element3_start [capture_value "/x &intarray\[3\]"]
454 set element4_start [capture_value "/x &intarray\[4\]"]
455 set element3_offset \
456 [capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
457 set element4_offset \
458 [capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
460 if ![string compare $is64bitonly "no"] then {
461 gdb_test "print zero_all ()" ""
463 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
464 "array partial, srec" 4 "intarray\[3\]"
466 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
467 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
469 gdb_test "print zero_all ()" ""
471 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
472 "array partial, ihex" 4 "intarray\[3\]"
474 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
475 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
477 gdb_test "print zero_all ()" ""
479 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
480 "array partial, tekhex" 4 "intarray\[3\]"
482 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
483 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
486 gdb_test "print zero_all ()" ""
488 test_restore_saved_value \
489 "intarr1.bin binary $array_start $element3_offset $element4_offset" \
490 "array partial, binary" 4 "intarray\[3\]"
492 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
493 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
495 if ![string compare $is64bitonly "no"] then {
496 gdb_test "print zero_all ()" "" ""
498 # restore with expressions
499 test_restore_saved_value \
500 "intarr3.srec ${array2_start}-${array_start} &intarray\[3\] &intarray\[4\]" \
501 "array partial with expressions" 4 "intarray2\[3\]"
503 gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
504 gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
509 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"