warn if "source" fails to open the file when from_tty == 0
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / dump.exp
1 # Copyright 2002-2013 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 # This file was written by Michael Snyder (msnyder@redhat.com)
17 # This is a test for the gdb command "dump".
18
19
20 standard_testfile
21
22 set options {debug}
23
24 set is64bitonly "no"
25 set endian "auto"
26
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"
31 }
32
33 if {[istarget "spu*-*-*"]} then {
34 # The internal address format used for the combined Cell/B.E.
35 # debugger requires 64-bit.
36 set is64bitonly "yes"
37 }
38
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
40 untested dump.exp
41 return -1
42 }
43
44 # Start with a fresh gdb.
45
46 gdb_exit
47 gdb_start
48 gdb_reinitialize_dir $srcdir/$subdir
49
50 gdb_test "dump mem /dev/null 0x10 0x20" "Cannot access memory at address 0x10" \
51 "inaccessible memory is reported"
52
53 gdb_load ${binfile}
54
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 {
61 set is64bitonly "yes"
62 }
63
64 # Clean up any stale output files from previous test runs
65
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"
67
68 # Test help (FIXME:)
69
70 # Run target program until data structs are initialized.
71
72 if { ! [ runto checkpoint1 ] } then {
73 untested dump.exp
74 return -1
75 }
76
77 # Get the endianness for the later use with endianless formats.
78
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"
83 }
84 }
85
86 # Now generate some dump files.
87
88 proc make_dump_file { command msg } {
89 global gdb_prompt
90
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 }
96 }
97 }
98
99 make_dump_file "dump val intarr1.bin intarray" \
100 "dump array as value, default"
101
102 make_dump_file "dump val intstr1.bin intstruct" \
103 "dump struct as value, default"
104
105 make_dump_file "dump bin val intarr1b.bin intarray" \
106 "dump array as value, binary"
107
108 make_dump_file "dump bin val intstr1b.bin intstruct" \
109 "dump struct as value, binary"
110
111 make_dump_file "dump srec val intarr1.srec intarray" \
112 "dump array as value, srec"
113
114 make_dump_file "dump srec val intstr1.srec intstruct" \
115 "dump struct as value, srec"
116
117 make_dump_file "dump ihex val intarr1.ihex intarray" \
118 "dump array as value, intel hex"
119
120 make_dump_file "dump ihex val intstr1.ihex intstruct" \
121 "dump struct as value, intel hex"
122
123 make_dump_file "dump tekhex val intarr1.tekhex intarray" \
124 "dump array as value, tekhex"
125
126 make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
127 "dump struct as value, tekhex"
128
129 proc capture_value { expression args } {
130 global gdb_prompt
131 global expect_out
132
133 set output_string ""
134 if {[llength $args] > 0} {
135 # Convert $args into a simple string and don't use EXPRESSION
136 # in the test name.
137 set test "[join $args]; capture"
138 } {
139 set test "capture $expression"
140 }
141 gdb_test_multiple "print ${expression}" "$test" {
142 -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
143 set output_string "$expect_out(1,string)"
144 pass "$test"
145 }
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)"
149 pass "$test"
150 }
151 }
152 return $output_string
153 }
154
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
158 # string.
159
160 proc capture_pointer_with_type { pointer } {
161 global gdb_prompt
162 global expect_out
163
164 set test "capture type of pointer $pointer"
165 set output_string ""
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
170 # following regexp.
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".
173 pass "$test"
174 } else {
175 fail "$test"
176 }
177 }
178 }
179
180 return $output_string
181 }
182
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"]
187
188 set array_val [capture_value "intarray"]
189 set struct_val [capture_value "intstruct"]
190
191 set array_ptr_type [capture_pointer_with_type "&intarray"]
192 set struct_ptr_type [capture_pointer_with_type "&intstruct"]
193
194 make_dump_file "dump mem intarr2.bin $array_start $array_end" \
195 "dump array as memory, default"
196
197 make_dump_file "dump mem intstr2.bin $struct_start $struct_end" \
198 "dump struct as memory, default"
199
200 make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
201 "dump array as memory, binary"
202
203 make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
204 "dump struct as memory, binary"
205
206 make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
207 "dump array as memory, srec"
208
209 make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
210 "dump struct as memory, srec"
211
212 make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
213 "dump array as memory, ihex"
214
215 make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
216 "dump struct as memory, ihex"
217
218 make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
219 "dump array as memory, tekhex"
220
221 make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
222 "dump struct as memory, tekhex"
223
224 # test complex expressions
225 make_dump_file \
226 "dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
227 "dump array as mem, srec, expressions"
228
229 proc test_restore_saved_value { restore_args msg oldval newval } {
230 global gdb_prompt
231
232 gdb_test "restore $restore_args" \
233 "Restoring .*" \
234 "$msg; file restored ok"
235 if { ![string compare $oldval \
236 [capture_value $newval "$msg"]] } then {
237 pass "$msg; value restored ok"
238 } else {
239 fail "$msg; value restored ok"
240 }
241 }
242
243 if ![string compare $is64bitonly "no"] then {
244
245 gdb_test "print zero_all ()" ".*"
246
247 test_restore_saved_value "intarr1.srec" "array as value, srec" \
248 $array_val "intarray"
249
250 test_restore_saved_value "intstr1.srec" "struct as value, srec" \
251 $struct_val "intstruct"
252
253 gdb_test "print zero_all ()" "void" "zero all"
254
255 test_restore_saved_value "intarr2.srec" "array as memory, srec" \
256 $array_val "intarray"
257
258 test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
259 $struct_val "intstruct"
260
261 gdb_test "print zero_all ()" ".*"
262
263 test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
264 $array_val "intarray"
265
266 test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
267 $struct_val "intstruct"
268
269 gdb_test "print zero_all ()" ".*"
270
271 test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
272 $array_val "intarray"
273
274 test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
275 $struct_val "intstruct"
276
277 gdb_test "print zero_all ()" ".*"
278
279 test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
280 $array_val "intarray"
281
282 test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
283 $struct_val "intstruct"
284
285 gdb_test "print zero_all ()" ".*"
286
287 test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
288 $array_val "intarray"
289
290 test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
291 $struct_val "intstruct"
292 }
293
294 gdb_test "print zero_all ()" ".*"
295
296 test_restore_saved_value "intarr1.bin binary $array_start" \
297 "array as value, binary" \
298 $array_val "intarray"
299
300 test_restore_saved_value "intstr1.bin binary $struct_start" \
301 "struct as value, binary" \
302 $struct_val "intstruct"
303
304 gdb_test "print zero_all ()" ".*"
305
306 test_restore_saved_value "intarr2.bin binary $array_start" \
307 "array as memory, binary" \
308 $array_val "intarray"
309
310 test_restore_saved_value "intstr2.bin binary $struct_start" \
311 "struct as memory, binary" \
312 $struct_val "intstruct"
313
314 # test restore with offset.
315
316 set array2_start [capture_value "/x &intarray2\[0\]"]
317 set struct2_start [capture_value "/x &intstruct2"]
318 set array2_offset \
319 [capture_value "(char *) &intarray2 - (char *) &intarray"]
320 set struct2_offset \
321 [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
322
323 gdb_test "print zero_all ()" ".*"
324
325
326 if ![string compare $is64bitonly "no"] then {
327 test_restore_saved_value "intarr1.srec $array2_offset" \
328 "array copy, srec" \
329 $array_val "intarray2"
330
331 test_restore_saved_value "intstr1.srec $struct2_offset" \
332 "struct copy, srec" \
333 $struct_val "intstruct2"
334
335 gdb_test "print zero_all ()" ".*"
336
337 test_restore_saved_value "intarr1.ihex $array2_offset" \
338 "array copy, ihex" \
339 $array_val "intarray2"
340
341 test_restore_saved_value "intstr1.ihex $struct2_offset" \
342 "struct copy, ihex" \
343 $struct_val "intstruct2"
344
345 gdb_test "print zero_all ()" ".*"
346
347 test_restore_saved_value "intarr1.tekhex $array2_offset" \
348 "array copy, tekhex" \
349 $array_val "intarray2"
350
351 test_restore_saved_value "intstr1.tekhex $struct2_offset" \
352 "struct copy, tekhex" \
353 $struct_val "intstruct2"
354 }
355
356 gdb_test "print zero_all ()" ".*"
357
358 test_restore_saved_value "intarr1.bin binary $array2_start" \
359 "array copy, binary" \
360 $array_val "intarray2"
361
362 test_restore_saved_value "intstr1.bin binary $struct2_start" \
363 "struct copy, binary" \
364 $struct_val "intstruct2"
365
366 #
367 # test restore with start/stop addresses.
368 #
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.
371 #
372 # We will need the address and offset of the third and fourth elements.
373 #
374
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\]"]
381
382 if ![string compare $is64bitonly "no"] then {
383 gdb_test "print zero_all ()" ".*"
384
385 test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
386 "array partial, srec" 4 "intarray\[3\]"
387
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"
390
391 gdb_test "print zero_all ()" ".*"
392
393 test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
394 "array partial, ihex" 4 "intarray\[3\]"
395
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"
398
399 gdb_test "print zero_all ()" ".*"
400
401 test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
402 "array partial, tekhex" 4 "intarray\[3\]"
403
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"
406 }
407
408 gdb_test "print zero_all ()" ".*"
409
410 test_restore_saved_value \
411 "intarr1.bin binary $array_start $element3_offset $element4_offset" \
412 "array partial, binary" 4 "intarray\[3\]"
413
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"
416
417 if ![string compare $is64bitonly "no"] then {
418 gdb_test "print zero_all ()" ".*" ""
419
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\]"
424
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"
427 }
428
429
430 # Now start a fresh gdb session, and reload the saved value files.
431
432 gdb_exit
433 gdb_start
434 gdb_file_cmd ${binfile}
435
436 # Now fix the endianness at the correct state.
437
438 gdb_test_multiple "set endian $endian" "set endianness" {
439 -re ".* (big|little) endian.*$gdb_prompt $" {
440 pass "setting $endian endianness"
441 }
442 }
443
444 # Reload saved values one by one, and compare.
445
446 if { ![string compare $array_val \
447 [capture_value "intarray" "file binfile; intarray"]] } then {
448 fail "start with intarray un-initialized"
449 } else {
450 pass "start with intarray un-initialized"
451 }
452
453 if { ![string compare $struct_val \
454 [capture_value "intstruct" "file binfile; intstruct"]] } then {
455 fail "start with intstruct un-initialized"
456 } else {
457 pass "start with intstruct un-initialized"
458 }
459
460 proc test_reload_saved_value { filename msg oldval newval } {
461 global gdb_prompt
462
463 gdb_file_cmd $filename
464 if { ![string compare $oldval \
465 [capture_value $newval "$msg"]] } then {
466 pass "$msg; value restored ok"
467 } else {
468 fail "$msg; value restored ok"
469 }
470 }
471
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"
482 }
483
484 # ihex format can not be loaded for 64-bit-only platforms
485 if ![string compare $is64bitonly "no"] then {
486
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"
495 }
496
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"
507 }
508
509 # clean up files
510
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"
This page took 0.066128 seconds and 4 git commands to generate.