[gdb/testsuite] Factor out lib/valgrind.exp
[external/binutils.git] / gdb / testsuite / gdb.base / dump.exp
1 # Copyright 2002-2018 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 "failed to compile"
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 set filenames {}
67 set all_files {
68     intarr1.bin intarr1b.bin intarr1.ihex
69     intarr1.srec intarr1.tekhex intarr1.verilog
70     intarr2.bin intarr2b.bin intarr2.ihex
71     intarr2.srec intarr2.tekhex intarr2.verilog
72     intstr1.bin intstr1b.bin intstr1.ihex
73     intstr1.srec intstr1.tekhex intstr1.verilog
74     intstr2.bin intstr2b.bin intstr2.ihex
75     intstr2.srec intstr2.tekhex intstr2.verilog
76     intarr3.srec
77 }
78
79 # This loop sets variables dynamically -- each name listed in
80 # $ALL_FILES is both a file name and a variable name.
81 foreach file $all_files {
82     if {[is_remote host]} {
83         set this_name $file
84     } else {
85         set this_name [standard_output_file $file]
86     }
87
88     lappend filenames [set ${file} $this_name]
89 }
90
91 remote_exec host "rm -f $filenames"
92
93 # Test help (FIXME:)
94
95 # Run target program until data structs are initialized.
96
97 if { ! [ runto checkpoint1 ] } then {
98     untested "couldn't run to checkpoint"
99     return -1
100 }
101
102 # Get the endianness for the later use with endianless formats.
103
104 set endian [get_endianness]
105
106 # Now generate some dump files.
107
108 proc make_dump_file { command msg } {
109   global gdb_prompt
110
111     gdb_test_multiple "${command}" "$msg" {
112         -re ".*\[Ee\]rror.*$gdb_prompt $"      { fail $msg }
113         -re ".*\[Ww\]arning.*$gdb_prompt $"    { fail $msg }
114         -re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
115         -re ".*$gdb_prompt $"                  { pass $msg }
116     }
117 }
118
119 make_dump_file "dump val [set intarr1.bin] intarray" \
120         "dump array as value, default"
121
122 make_dump_file "dump val [set intstr1.bin] intstruct" \
123         "dump struct as value, default"
124
125 make_dump_file "dump bin val [set intarr1b.bin] intarray" \
126         "dump array as value, binary"
127
128 make_dump_file "dump bin val [set intstr1b.bin] intstruct" \
129         "dump struct as value, binary"
130
131 make_dump_file "dump srec val [set intarr1.srec] intarray" \
132         "dump array as value, srec"
133
134 make_dump_file "dump srec val [set intstr1.srec] intstruct" \
135         "dump struct as value, srec"
136
137 make_dump_file "dump ihex val [set intarr1.ihex] intarray" \
138         "dump array as value, intel hex"
139
140 make_dump_file "dump ihex val [set intstr1.ihex] intstruct" \
141         "dump struct as value, intel hex"
142
143 make_dump_file "dump tekhex val [set intarr1.tekhex] intarray" \
144         "dump array as value, tekhex"
145
146 make_dump_file "dump tekhex val [set intstr1.tekhex] intstruct" \
147         "dump struct as value, tekhex"
148
149 make_dump_file "dump verilog val [set intarr1.verilog] intarray" \
150         "dump array as value, verilog"
151
152 make_dump_file "dump verilog val [set intstr1.verilog] intstruct" \
153         "dump struct as value, verilog"
154
155 proc capture_value { expression args } {
156     global gdb_prompt
157     global expect_out
158
159     set output_string ""
160     if {[llength $args] > 0} {
161         # Convert $args into a simple string and don't use EXPRESSION
162         # in the test name.
163         set test "[join $args]; capture"
164     } {
165         set test "capture $expression"
166     }
167     gdb_test_multiple "print ${expression}" "$test" {
168         -re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
169             set output_string "$expect_out(1,string)"
170             pass "$test"
171         }
172         -re "(Cannot access memory at address \[^\r\n\]+).*$gdb_prompt $" {
173             # Even a failed value is valid
174             set output_string "$expect_out(1,string)"
175             pass "$test"
176         }
177     }
178     return $output_string
179 }
180
181 # POINTER is a pointer and this proc captures the value of POINTER along
182 # with POINTER's type.  For example, POINTER is "&intarray", this proc will
183 # call "p &intarray", capture "(int (*)[32]) 0x804a0e0", and return this
184 # string.
185
186 proc capture_pointer_with_type { pointer } {
187     global gdb_prompt
188     global expect_out
189
190     set test "capture type of pointer $pointer"
191     set output_string ""
192     gdb_test_multiple "p ${pointer}" $test {
193         -re "\\$\[0-9\]+ = .*$gdb_prompt $" {
194             # Expected output of "p ${pointer}" is like "$7 = (int (*)[32]) 0x804a0e0",
195             # and we want to extract "(int (*)[32]) 0x804a0e0" from it via
196             # following regexp.
197             if [regexp " \\(.*\\).* 0x\[0-9a-fA-F\]+" $expect_out(0,string) output_string] {
198                 # OUTPUT_STRING is expected to be like "(int (*)[32]) 0x804a0e0".
199                 pass "$test"
200             } else {
201                 fail "$test"
202             }
203         }
204     }
205
206     return $output_string
207 }
208
209 set array_start  [capture_value "/x &intarray\[0\]"]
210 set array_end    [capture_value "/x &intarray\[32\]"]
211 set struct_start [capture_value "/x &intstruct"]
212 set struct_end   [capture_value "/x &intstruct + 1"]
213
214 set array_val    [capture_value "intarray"]
215 set struct_val   [capture_value "intstruct"]
216
217 set array_ptr_type [capture_pointer_with_type "&intarray"]
218 set struct_ptr_type [capture_pointer_with_type "&intstruct"]
219
220 make_dump_file "dump mem [set intarr2.bin] $array_start $array_end" \
221         "dump array as memory, default"
222
223 make_dump_file "dump  mem [set intstr2.bin] $struct_start $struct_end" \
224         "dump struct as memory, default"
225
226 make_dump_file "dump bin mem [set intarr2b.bin] $array_start $array_end" \
227         "dump array as memory, binary"
228
229 make_dump_file "dump bin mem [set intstr2b.bin] $struct_start $struct_end" \
230         "dump struct as memory, binary"
231
232 make_dump_file "dump srec mem [set intarr2.srec] $array_start $array_end" \
233         "dump array as memory, srec"
234
235 make_dump_file "dump srec mem [set intstr2.srec] $struct_start $struct_end" \
236         "dump struct as memory, srec"
237
238 make_dump_file "dump ihex mem [set intarr2.ihex] $array_start $array_end" \
239         "dump array as memory, ihex"
240
241 make_dump_file "dump ihex mem [set intstr2.ihex] $struct_start $struct_end" \
242         "dump struct as memory, ihex"
243
244 make_dump_file "dump tekhex mem [set intarr2.tekhex] $array_start $array_end" \
245         "dump array as memory, tekhex"
246
247 make_dump_file "dump tekhex mem [set intstr2.tekhex] $struct_start $struct_end" \
248         "dump struct as memory, tekhex"
249
250 make_dump_file "dump verilog mem [set intarr2.verilog] $array_start $array_end" \
251         "dump array as memory, verilog"
252
253 make_dump_file "dump verilog mem [set intstr2.verilog] $struct_start $struct_end" \
254         "dump struct as memory, verilog"
255
256 # test complex expressions
257 make_dump_file \
258     "dump srec mem [set intarr3.srec] &intarray \(char *\) &intarray + sizeof intarray" \
259         "dump array as mem, srec, expressions"
260
261 proc test_restore_saved_value { restore_args msg oldval newval } {
262     global gdb_prompt
263     
264     gdb_test "restore $restore_args" \
265         "Restoring .*" \
266         "$msg; file restored ok"
267     if { ![string compare $oldval \
268                [capture_value $newval "$msg"]] } then { 
269         pass "$msg; value restored ok"
270     } else {
271         fail "$msg; value restored ok"
272     }
273 }
274
275 if ![string compare $is64bitonly "no"] then {
276
277   gdb_test "print zero_all ()" ".*"
278
279   test_restore_saved_value "[set intarr1.srec]" "array as value, srec" \
280         $array_val "intarray"
281
282   test_restore_saved_value "[set intstr1.srec]" "struct as value, srec" \
283         $struct_val "intstruct"
284
285   gdb_test "print zero_all ()" "void" "zero all"
286
287   test_restore_saved_value "[set intarr2.srec]" "array as memory, srec" \
288         $array_val "intarray"
289
290   test_restore_saved_value "[set intstr2.srec]" "struct as memory, srec" \
291         $struct_val "intstruct"
292
293   gdb_test "print zero_all ()" ".*"
294
295   test_restore_saved_value "[set intarr1.ihex]" "array as value, ihex" \
296         $array_val "intarray"
297
298   test_restore_saved_value "[set intstr1.ihex]" "struct as value, ihex" \
299         $struct_val "intstruct"
300
301   gdb_test "print zero_all ()" ".*"
302
303   test_restore_saved_value "[set intarr2.ihex]" "array as memory, ihex" \
304         $array_val "intarray"
305
306   test_restore_saved_value "[set intstr2.ihex]" "struct as memory, ihex" \
307         $struct_val "intstruct"
308
309   gdb_test "print zero_all ()" ".*"
310
311   test_restore_saved_value "[set intarr1.tekhex]" "array as value, tekhex" \
312         $array_val "intarray"
313
314   test_restore_saved_value "[set intstr1.tekhex]" "struct as value, tekhex" \
315         $struct_val "intstruct"
316
317   gdb_test "print zero_all ()" ".*"
318
319   test_restore_saved_value "[set intarr2.tekhex]" "array as memory, tekhex" \
320         $array_val "intarray"
321
322   test_restore_saved_value "[set intstr2.tekhex]" "struct as memory, tekhex" \
323         $struct_val "intstruct"
324 }
325
326 gdb_test "print zero_all ()" ".*"
327
328 test_restore_saved_value "[set intarr1.bin] binary $array_start" \
329         "array as value, binary" \
330         $array_val "intarray"
331
332 test_restore_saved_value "[set intstr1.bin] binary $struct_start" \
333         "struct as value, binary" \
334         $struct_val "intstruct"
335
336 gdb_test "print zero_all ()" ".*"
337
338 test_restore_saved_value "[set intarr2.bin] binary $array_start" \
339         "array as memory, binary" \
340         $array_val "intarray"
341
342 test_restore_saved_value "[set intstr2.bin] binary $struct_start" \
343         "struct as memory, binary" \
344         $struct_val "intstruct"
345
346 # test restore with offset.
347
348 set array2_start   [capture_value "/x &intarray2\[0\]"]
349 set struct2_start  [capture_value "/x &intstruct2"]
350 set array2_offset  \
351         [capture_value "(char *) &intarray2 - (char *) &intarray"]
352 set struct2_offset \
353         [capture_value "(char *) &intstruct2 - (char *) &intstruct"]
354
355 gdb_test "print zero_all ()" ".*"
356
357
358 if ![string compare $is64bitonly "no"] then {
359   test_restore_saved_value "[set intarr1.srec] $array2_offset" \
360         "array copy, srec" \
361         $array_val "intarray2"
362
363   test_restore_saved_value "[set intstr1.srec] $struct2_offset" \
364         "struct copy, srec" \
365         $struct_val "intstruct2"
366
367   gdb_test "print zero_all ()" ".*"
368
369   test_restore_saved_value "[set intarr1.ihex] $array2_offset" \
370         "array copy, ihex" \
371         $array_val "intarray2"
372
373   test_restore_saved_value "[set intstr1.ihex] $struct2_offset" \
374         "struct copy, ihex" \
375         $struct_val "intstruct2"
376
377   gdb_test "print zero_all ()" ".*"
378
379   test_restore_saved_value "[set intarr1.tekhex] $array2_offset" \
380         "array copy, tekhex" \
381         $array_val "intarray2"
382
383   test_restore_saved_value "[set intstr1.tekhex] $struct2_offset" \
384         "struct copy, tekhex" \
385         $struct_val "intstruct2"
386 }
387
388 gdb_test "print zero_all ()" ".*"
389
390 test_restore_saved_value "[set intarr1.bin] binary $array2_start" \
391         "array copy, binary" \
392         $array_val "intarray2"
393
394 test_restore_saved_value "[set intstr1.bin] binary $struct2_start" \
395         "struct copy, binary" \
396         $struct_val "intstruct2"
397
398 #
399 # test restore with start/stop addresses.
400 #
401 # For this purpose, we will restore just the third element of the array, 
402 # and check to see that adjacent elements are not modified.
403 #
404 # We will need the address and offset of the third and fourth elements.
405 #
406
407 set element3_start  [capture_value "/x &intarray\[3\]"]
408 set element4_start  [capture_value "/x &intarray\[4\]"]
409 set element3_offset \
410         [capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
411 set element4_offset \
412         [capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
413
414 if ![string compare $is64bitonly "no"] then {
415   gdb_test "print zero_all ()" ".*"
416
417   test_restore_saved_value "[set intarr1.srec] 0 $element3_start $element4_start" \
418         "array partial, srec" 4 "intarray\[3\]"
419
420   gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
421   gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
422
423   gdb_test "print zero_all ()" ".*"
424
425   test_restore_saved_value "[set intarr1.ihex] 0 $element3_start $element4_start" \
426         "array partial, ihex" 4 "intarray\[3\]"
427
428   gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
429   gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
430
431   gdb_test "print zero_all ()" ".*"
432
433   test_restore_saved_value "[set intarr1.tekhex] 0 $element3_start $element4_start" \
434         "array partial, tekhex" 4 "intarray\[3\]"
435
436   gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
437   gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
438 }
439
440 gdb_test "print zero_all ()" ".*"
441
442 test_restore_saved_value \
443     "[set intarr1.bin] binary $array_start $element3_offset $element4_offset" \
444     "array partial, binary" 4 "intarray\[3\]"
445
446 gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
447 gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
448
449 if ![string compare $is64bitonly "no"] then {
450   gdb_test "print zero_all ()" ".*" ""
451
452   # restore with expressions 
453   test_restore_saved_value \
454         "[set intarr3.srec] (char*)${array2_start}-(char*)${array_start} &intarray\[3\] &intarray\[4\]" \
455         "array partial with expressions" 4 "intarray2\[3\]"
456
457   gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
458   gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
459 }
460
461
462 # Now start a fresh gdb session, and reload the saved value files.
463
464 gdb_exit
465 gdb_start
466 gdb_file_cmd ${binfile}
467
468 # Now fix the endianness at the correct state.
469
470 gdb_test_multiple "set endian $endian" "set endianness" {
471     -re ".* (big|little) endian.*$gdb_prompt $" { 
472         pass "setting $endian endianness"
473     }
474 }
475
476 # Reload saved values one by one, and compare.
477
478 if { ![string compare $array_val \
479            [capture_value "intarray" "file binfile; intarray"]] } then {
480     fail "start with intarray un-initialized"
481 } else {
482     pass "start with intarray un-initialized"
483 }
484
485 if { ![string compare $struct_val \
486            [capture_value "intstruct" "file binfile; intstruct"]] } then {
487     fail "start with intstruct un-initialized"
488 } else {
489     pass "start with intstruct un-initialized"
490 }
491
492 proc test_reload_saved_value { filename msg oldval newval } {
493     global gdb_prompt
494     
495     gdb_file_cmd $filename
496     if { ![string compare $oldval \
497                [capture_value $newval "$msg"]] } then { 
498         pass "$msg; value restored ok"
499     } else {
500         fail "$msg; value restored ok"
501     }
502 }
503
504 # srec format can not be loaded for 64-bit-only platforms
505 if ![string compare $is64bitonly "no"] then {
506   test_reload_saved_value "[set intarr1.srec]" "reload array as value, srec" \
507         $array_val "\*$array_ptr_type"
508   test_reload_saved_value "[set intstr1.srec]" "reload struct as value, srec" \
509         $struct_val "\*$struct_ptr_type"
510   test_reload_saved_value "[set intarr2.srec]" "reload array as memory, srec" \
511         $array_val "\*$array_ptr_type"
512   test_reload_saved_value "[set intstr2.srec]" "reload struct as memory, srec" \
513         $struct_val "\*$struct_ptr_type"
514 }
515
516 # ihex format can not be loaded for 64-bit-only platforms
517 if ![string compare $is64bitonly "no"] then {
518
519   test_reload_saved_value "[set intarr1.ihex]" \
520       "reload array as value, intel hex" \
521         $array_val "\*$array_ptr_type"
522   test_reload_saved_value "[set intstr1.ihex]" \
523       "reload struct as value, intel hex" \
524         $struct_val "\*$struct_ptr_type"
525   test_reload_saved_value "[set intarr2.ihex]" \
526       "reload array as memory, intel hex" \
527         $array_val "\*$array_ptr_type"
528   test_reload_saved_value "[set intstr2.ihex]" \
529       "reload struct as memory, intel hex" \
530         $struct_val "\*$struct_ptr_type"
531 }
532
533 # tekhex format can not be loaded for 64-bit-only platforms
534 if ![string compare $is64bitonly "no"] then {
535   test_reload_saved_value "[set intarr1.tekhex]" \
536       "reload array as value, tekhex" \
537         $array_val "\*$array_ptr_type"
538   test_reload_saved_value "[set intstr1.tekhex]" \
539       "reload struct as value, tekhex" \
540         $struct_val "\*$struct_ptr_type"
541   test_reload_saved_value "[set intarr2.tekhex]" \
542       "reload array as memory, tekhex" \
543         $array_val "\*$array_ptr_type"
544   test_reload_saved_value "[set intstr2.tekhex]" \
545       "reload struct as memory, tekhex" \
546         $struct_val "\*$struct_ptr_type"
547 }
548
549 # clean up files
550
551 remote_exec host "rm -f $filenames"