1# Copyright 2002, 2004 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 2 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, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-gdb@prep.ai.mit.edu
19
20# This file was written by Michael Snyder (msnyder@redhat.com)
21# This is a test for the gdb command "dump".
22
23if $tracelevel then {
24	strace $tracelevel
25}
26
27set prms_id 0
28set bug_id 0
29
30set testfile "dump"
31
32set srcfile  ${testfile}.c
33set binfile  ${objdir}/${subdir}/${testfile}
34set options  {debug}
35
36set is64bitonly "no"
37
38if [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"
42}
43
44if [istarget "ia64*-*-*"] then {
45    set is64bitonly "yes"
46}
47
48if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable ${options}] != "" } {
49     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
50}
51
52# Start with a fresh gdb.
53
54gdb_exit
55gdb_start
56gdb_reinitialize_dir $srcdir/$subdir
57gdb_load ${binfile}
58
59# Clean up any stale output files from previous test runs
60
61remote_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"
62
63# Test help (FIXME:)
64
65# Run target program until data structs are initialized.
66
67if { ! [ runto checkpoint1 ] } then {
68    gdb_suppress_entire_file "Program failed to run, so all tests in this file will automatically fail."
69}
70
71# Now generate some dump files.
72
73proc make_dump_file { command msg } {
74  global gdb_prompt
75
76    send_gdb "${command}\n"
77    gdb_expect {
78	-re ".*\[Ee\]rror.*$gdb_prompt $"      { fail $msg }
79	-re ".*\[Ww\]arning.*$gdb_prompt $"    { fail $msg }
80	-re ".*\[Uu\]ndefined .*$gdb_prompt $" { fail $msg }
81	-re ".*$gdb_prompt $" { pass $msg }
82	timeout                                { fail "$msg (timeout)" }
83    }
84}
85
86make_dump_file "dump val intarr1.bin intarray" \
87	"dump array as value, default"
88
89make_dump_file "dump val intstr1.bin intstruct" \
90	"dump struct as value, default"
91
92make_dump_file "dump bin val intarr1b.bin intarray" \
93	"dump array as value, binary"
94
95make_dump_file "dump bin val intstr1b.bin intstruct" \
96	"dump struct as value, binary"
97
98make_dump_file "dump srec val intarr1.srec intarray" \
99	"dump array as value, srec"
100
101make_dump_file "dump srec val intstr1.srec intstruct" \
102	"dump struct as value, srec"
103
104make_dump_file "dump ihex val intarr1.ihex intarray" \
105	"dump array as value, intel hex"
106
107make_dump_file "dump ihex val intstr1.ihex intstruct" \
108	"dump struct as value, intel hex"
109
110make_dump_file "dump tekhex val intarr1.tekhex intarray" \
111	"dump array as value, tekhex"
112
113make_dump_file "dump tekhex val intstr1.tekhex intstruct" \
114	"dump struct as value, tekhex"
115
116proc capture_value { expression args } {
117    global gdb_prompt
118    global expect_out
119
120    set output_string ""
121    if {[llength $args] > 0} {
122	# Convert $args into a simple string.
123	set test "[join $args]; capture $expression"
124    } {
125	set test "capture $expression"
126    }
127    gdb_test_multiple "print ${expression}" "$test" {
128	-re "\\$\[0-9\]+ = (\[^\r\n\]+).*$gdb_prompt $" {
129	    set output_string "$expect_out(1,string)"
130	    pass "$test"
131	}
132	-re "(Cannot access memory at address \[^\r\n\]+).*$gdb_prompt $" {
133	    # Even a failed value is valid
134	    set output_string "$expect_out(1,string)"
135	    pass "$test"
136	}
137    }
138    return $output_string
139}
140
141set array_start  [capture_value "/x &intarray\[0\]"]
142set array_end    [capture_value "/x &intarray\[32\]"]
143set struct_start [capture_value "/x &intstruct"]
144set struct_end   [capture_value "/x &intstruct + 1"]
145
146set array_val    [capture_value "intarray"]
147set struct_val   [capture_value "intstruct"]
148
149make_dump_file "dump mem intarr2.bin $array_start $array_end" \
150	"dump array as memory, default"
151
152make_dump_file "dump  mem intstr2.bin $struct_start $struct_end" \
153	"dump struct as memory, default"
154
155make_dump_file "dump bin mem intarr2b.bin $array_start $array_end" \
156	"dump array as memory, binary"
157
158make_dump_file "dump bin mem intstr2b.bin $struct_start $struct_end" \
159	"dump struct as memory, binary"
160
161make_dump_file "dump srec mem intarr2.srec $array_start $array_end" \
162	"dump array as memory, srec"
163
164make_dump_file "dump srec mem intstr2.srec $struct_start $struct_end" \
165	"dump struct as memory, srec"
166
167make_dump_file "dump ihex mem intarr2.ihex $array_start $array_end" \
168	"dump array as memory, ihex"
169
170make_dump_file "dump ihex mem intstr2.ihex $struct_start $struct_end" \
171	"dump struct as memory, ihex"
172
173make_dump_file "dump tekhex mem intarr2.tekhex $array_start $array_end" \
174	"dump array as memory, tekhex"
175
176make_dump_file "dump tekhex mem intstr2.tekhex $struct_start $struct_end" \
177	"dump struct as memory, tekhex"
178
179# test complex expressions
180make_dump_file \
181	"dump srec mem intarr3.srec &intarray \(char *\) &intarray + sizeof intarray" \
182	"dump array as mem, srec, expressions"
183
184
185# Now start a fresh gdb session, and reload the saved value files.
186
187gdb_exit
188gdb_start
189gdb_file_cmd ${binfile}
190
191# Reload saved values one by one, and compare.
192
193if { ![string compare $array_val \
194	   [capture_value "intarray" "file binfile"]] } then {
195    fail "start with intarray un-initialized"
196} else {
197    pass "start with intarray un-initialized"
198}
199
200if { ![string compare $struct_val \
201	   [capture_value "intstruct" "file binfile"]] } then {
202    fail "start with intstruct un-initialized"
203} else {
204    pass "start with intstruct un-initialized"
205}
206
207proc test_reload_saved_value { filename msg oldval newval } {
208    global gdb_prompt
209
210    gdb_file_cmd $filename
211    if { ![string compare $oldval \
212	       [capture_value $newval "$msg"]] } then {
213	pass "$msg; value restored ok"
214    } else {
215	fail "$msg; value restored ok"
216    }
217}
218
219proc test_restore_saved_value { restore_args msg oldval newval } {
220    global gdb_prompt
221
222    gdb_test "restore $restore_args" \
223	"Restoring .*" \
224	"$msg; file restored ok"
225    if { ![string compare $oldval \
226	       [capture_value $newval "$msg"]] } then {
227	pass "$msg; value restored ok"
228    } else {
229	fail "$msg; value restored ok"
230    }
231}
232
233# srec format can not be loaded for 64-bit-only platforms
234if ![string compare $is64bitonly "no"] then {
235  test_reload_saved_value "intarr1.srec" "reload array as value, srec" \
236	$array_val "intarray"
237  test_reload_saved_value "intstr1.srec" "reload struct as value, srec" \
238	$struct_val "intstruct"
239  test_reload_saved_value "intarr2.srec" "reload array as memory, srec" \
240	$array_val "intarray"
241  test_reload_saved_value "intstr2.srec" "reload struct as memory, srec" \
242	$struct_val "intstruct"
243}
244
245# ihex format can not be loaded for 64-bit-only platforms
246if ![string compare $is64bitonly "no"] then {
247
248  test_reload_saved_value "intarr1.ihex" "reload array as value, intel hex" \
249	$array_val "intarray"
250  test_reload_saved_value "intstr1.ihex" "reload struct as value, intel hex" \
251	$struct_val "intstruct"
252  test_reload_saved_value "intarr2.ihex" "reload array as memory, intel hex" \
253	$array_val "intarray"
254  test_reload_saved_value "intstr2.ihex" "reload struct as memory, intel hex" \
255	$struct_val "intstruct"
256}
257
258# tekhex format can not be loaded for 64-bit-only platforms
259if ![string compare $is64bitonly "no"] then {
260  test_reload_saved_value "intarr1.tekhex" "reload array as value, tekhex" \
261	$array_val "intarray"
262  test_reload_saved_value "intstr1.tekhex" "reload struct as value, tekhex" \
263	$struct_val "intstruct"
264  test_reload_saved_value "intarr2.tekhex" "reload array as memory, tekhex" \
265	$array_val "intarray"
266  test_reload_saved_value "intstr2.tekhex" "reload struct as memory, tekhex" \
267	$struct_val "intstruct"
268}
269
270# Start a fresh gdb session
271
272gdb_exit
273gdb_start
274gdb_reinitialize_dir $srcdir/$subdir
275gdb_load ${binfile}
276
277# Run to main.
278if { ! [ runto_main ] } then {
279    gdb_suppress_entire_file "Program failed to run, so remaining tests in this file will automatically fail."
280}
281
282if { ![string compare $array_val \
283	   [capture_value "intarray" "load binfile"]] } then {
284    fail "start with intarray un-initialized, runto main"
285} else {
286    pass "start with intarray un-initialized, runto main"
287}
288
289if { ![string compare $struct_val \
290	   [capture_value "intstruct" "load binfile"]] } then {
291    fail "start with intstruct un-initialized, runto main"
292} else {
293    pass "start with intstruct un-initialized, runto main"
294}
295
296if ![string compare $is64bitonly "no"] then {
297  test_restore_saved_value "intarr1.srec" "array as value, srec" \
298	$array_val "intarray"
299
300  test_restore_saved_value "intstr1.srec" "struct as value, srec" \
301	$struct_val "intstruct"
302
303  gdb_test "print zero_all ()" "void" "zero all"
304
305  test_restore_saved_value "intarr2.srec" "array as memory, srec" \
306	$array_val "intarray"
307
308  test_restore_saved_value "intstr2.srec" "struct as memory, srec" \
309	$struct_val "intstruct"
310
311  gdb_test "print zero_all ()" ""
312
313  test_restore_saved_value "intarr1.ihex" "array as value, ihex" \
314	$array_val "intarray"
315
316  test_restore_saved_value "intstr1.ihex" "struct as value, ihex" \
317	$struct_val "intstruct"
318
319  gdb_test "print zero_all ()" ""
320
321  test_restore_saved_value "intarr2.ihex" "array as memory, ihex" \
322	$array_val "intarray"
323
324  test_restore_saved_value "intstr2.ihex" "struct as memory, ihex" \
325	$struct_val "intstruct"
326
327  gdb_test "print zero_all ()" ""
328
329  test_restore_saved_value "intarr1.tekhex" "array as value, tekhex" \
330	$array_val "intarray"
331
332  test_restore_saved_value "intstr1.tekhex" "struct as value, tekhex" \
333	$struct_val "intstruct"
334
335  gdb_test "print zero_all ()" ""
336
337  test_restore_saved_value "intarr2.tekhex" "array as memory, tekhex" \
338	$array_val "intarray"
339
340  test_restore_saved_value "intstr2.tekhex" "struct as memory, tekhex" \
341	$struct_val "intstruct"
342}
343
344gdb_test "print zero_all ()" ""
345
346test_restore_saved_value "intarr1.bin binary $array_start" \
347	"array as value, binary" \
348	$array_val "intarray"
349
350test_restore_saved_value "intstr1.bin binary $struct_start" \
351	"struct as value, binary" \
352	$struct_val "intstruct"
353
354gdb_test "print zero_all ()" ""
355
356test_restore_saved_value "intarr2.bin binary $array_start" \
357	"array as memory, binary" \
358	$array_val "intarray"
359
360test_restore_saved_value "intstr2.bin binary $struct_start" \
361	"struct as memory, binary" \
362	$struct_val "intstruct"
363
364# test restore with offset.
365
366set array2_start   [capture_value "/x &intarray2\[0\]"]
367set struct2_start  [capture_value "/x &intstruct2"]
368set array2_offset  \
369	[capture_value "(char *) &intarray2 - (char *) &intarray"]
370set struct2_offset \
371	[capture_value "(char *) &intstruct2 - (char *) &intstruct"]
372
373gdb_test "print zero_all ()" ""
374
375
376if ![string compare $is64bitonly "no"] then {
377  test_restore_saved_value "intarr1.srec $array2_offset" \
378	"array copy, srec" \
379	$array_val "intarray2"
380
381  test_restore_saved_value "intstr1.srec $struct2_offset" \
382	"struct copy, srec" \
383	$struct_val "intstruct2"
384
385  gdb_test "print zero_all ()" ""
386
387  test_restore_saved_value "intarr1.ihex $array2_offset" \
388	"array copy, ihex" \
389	$array_val "intarray2"
390
391  test_restore_saved_value "intstr1.ihex $struct2_offset" \
392	"struct copy, ihex" \
393	$struct_val "intstruct2"
394
395  gdb_test "print zero_all ()" ""
396
397  test_restore_saved_value "intarr1.tekhex $array2_offset" \
398	"array copy, tekhex" \
399	$array_val "intarray2"
400
401  test_restore_saved_value "intstr1.tekhex $struct2_offset" \
402	"struct copy, tekhex" \
403	$struct_val "intstruct2"
404}
405
406gdb_test "print zero_all ()" ""
407
408test_restore_saved_value "intarr1.bin binary $array2_start" \
409	"array copy, binary" \
410	$array_val "intarray2"
411
412test_restore_saved_value "intstr1.bin binary $struct2_start" \
413	"struct copy, binary" \
414	$struct_val "intstruct2"
415
416#
417# test restore with start/stop addresses.
418#
419# For this purpose, we will restore just the third element of the array,
420# and check to see that adjacent elements are not modified.
421#
422# We will need the address and offset of the third and fourth elements.
423#
424
425set element3_start  [capture_value "/x &intarray\[3\]"]
426set element4_start  [capture_value "/x &intarray\[4\]"]
427set element3_offset \
428	[capture_value "/x (char *) &intarray\[3\] - (char *) &intarray\[0\]"]
429set element4_offset \
430	[capture_value "/x (char *) &intarray\[4\] - (char *) &intarray\[0\]"]
431
432if ![string compare $is64bitonly "no"] then {
433  gdb_test "print zero_all ()" ""
434
435  test_restore_saved_value "intarr1.srec 0 $element3_start $element4_start" \
436	"array partial, srec" 4 "intarray\[3\]"
437
438  gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 1"
439  gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 1"
440
441  gdb_test "print zero_all ()" ""
442
443  test_restore_saved_value "intarr1.ihex 0 $element3_start $element4_start" \
444	"array partial, ihex" 4 "intarray\[3\]"
445
446  gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 2"
447  gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 2"
448
449  gdb_test "print zero_all ()" ""
450
451  test_restore_saved_value "intarr1.tekhex 0 $element3_start $element4_start" \
452	"array partial, tekhex" 4 "intarray\[3\]"
453
454  gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 3"
455  gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 3"
456}
457
458gdb_test "print zero_all ()" ""
459
460test_restore_saved_value \
461    "intarr1.bin binary $array_start $element3_offset $element4_offset" \
462    "array partial, binary" 4 "intarray\[3\]"
463
464gdb_test "print intarray\[2\] == 0" " = 1" "element 2 not changed - 4"
465gdb_test "print intarray\[4\] == 0" " = 1" "element 4 not changed - 4"
466
467if ![string compare $is64bitonly "no"] then {
468  gdb_test "print zero_all ()" "" ""
469
470  # restore with expressions
471  test_restore_saved_value \
472	"intarr3.srec ${array2_start}-${array_start} &intarray\[3\] &intarray\[4\]" \
473	"array partial with expressions" 4 "intarray2\[3\]"
474
475  gdb_test "print intarray2\[2\] == 0" " = 1" "element 2 not changed, == 4"
476  gdb_test "print intarray2\[4\] == 0" " = 1" "element 4 not changed, == 4"
477}
478
479# clean up files
480
481remote_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"
482