1# Copyright 2011
2# Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# This file is part of the gdb testsuite
18
19# Test the memory attribute commands.
20
21if $tracelevel then {
22    strace $tracelevel
23}
24
25set testfile "memattr"
26set srcfile  ${testfile}.c
27
28if { [prepare_for_testing $testfile.exp $testfile $srcfile] } {
29    return -1
30}
31
32runto main
33
34set mem1start -1
35set mem2start -1
36set mem3start -1
37set mem4start -1
38set mem5start -1
39
40set mem1end -1
41set mem2end -1
42set mem3end -1
43set mem4end -1
44set mem5end -1
45
46
47gdb_test_multiple "info address mem1" "get address of mem1" {
48    -re "Symbol \"mem1\" is static storage at address ($hex).*$gdb_prompt $" {
49	set mem1start $expect_out(1,string)
50    }
51}
52
53gdb_test_multiple "info address mem2" "get address of mem2" {
54    -re "Symbol \"mem2\" is static storage at address ($hex).*$gdb_prompt $" {
55	set mem2start $expect_out(1,string)
56    }
57}
58
59gdb_test_multiple "info address mem3" "get address of mem3" {
60    -re "Symbol \"mem3\" is static storage at address ($hex).*$gdb_prompt $" {
61	set mem3start $expect_out(1,string)
62    }
63}
64
65gdb_test_multiple "info address mem4" "get address of mem4" {
66    -re "Symbol \"mem4\" is static storage at address ($hex).*$gdb_prompt $" {
67	set mem4start $expect_out(1,string)
68    }
69}
70
71gdb_test_multiple "info address mem5" "get address of mem5" {
72    -re "Symbol \"mem5\" is static storage at address ($hex).*$gdb_prompt $" {
73	set mem5start $expect_out(1,string)
74    }
75}
76
77gdb_test_multiple "print &mem1\[64\]" "get end of mem1" {
78    -re "$decimal = .* ($hex).*$gdb_prompt $" {
79	set mem1end $expect_out(1,string)
80    }
81}
82
83gdb_test_multiple "print &mem2\[64\]" "get end of mem2" {
84    -re "$decimal = .* ($hex).*$gdb_prompt $" {
85	set mem2end $expect_out(1,string)
86    }
87}
88
89gdb_test_multiple "print &mem3\[64\]" "get end of mem3" {
90    -re "$decimal = .* ($hex).*$gdb_prompt $" {
91	set mem3end $expect_out(1,string)
92    }
93}
94
95gdb_test_multiple "print &mem4\[64\]" "get end of mem4" {
96    -re "$decimal = .* ($hex).*$gdb_prompt $" {
97	set mem4end $expect_out(1,string)
98    }
99}
100
101gdb_test_multiple "print &mem5\[64\]" "get end of mem5" {
102    -re "$decimal = .* ($hex).*$gdb_prompt $" {
103	set mem5end $expect_out(1,string)
104    }
105}
106
107gdb_test_no_output "mem $mem1start $mem1end wo" "create mem region 1"
108gdb_test_no_output "mem $mem2start $mem2end ro" "create mem region 2"
109gdb_test_no_output "mem $mem3start $mem3end rw" "create mem region 3"
110gdb_test_no_output "mem $mem4start $mem4end rw" "create mem region 4"
111gdb_test_no_output "mem $mem5start $mem5end rw" "create mem region 5"
112
113set see1 0
114set see2 0
115set see3 0
116set see4 0
117set see5 0
118
119gdb_test_multiple "info mem" "info mem(1)" {
120    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
121	set see1 1
122	exp_continue
123    }
124    -re "2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
125	set see2 1
126	exp_continue
127    }
128    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
129	set see3 1
130	exp_continue
131    }
132    -re "4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
133	set see4 1
134	exp_continue
135    }
136    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
137	set see5 1
138	exp_continue
139    }
140    -re "$gdb_prompt $" {
141	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
142	    pass "info mem (1)"
143	} else {
144	    fail "info mem (1)"
145	}
146    }
147}
148
149#
150# Test read-only, write-only
151#
152
153# mem1 is write only: read should fail.
154gdb_test "print mem1\[1\]" \
155    "Cannot access memory at address $hex" \
156    "mem1 cannot be read"
157
158gdb_test "print mem1\[1\] = 9" \
159    "$decimal = 9" \
160    "mem1 can be written"
161
162# mem2 is read only: write should fail.
163gdb_test "print mem2\[1\] = 9" \
164    "Cannot access memory at address $hex" \
165    "mem2 cannot be written"
166
167gdb_test "print mem2\[1\]" \
168    "$decimal = 0" \
169    "mem2 can be read"
170
171#
172# Test disable and enable
173#
174
175gdb_test_no_output "disable mem 1" "disable mem 1"
176gdb_test "info mem" "1   n  .*" "mem 1 was disabled"
177
178gdb_test_no_output "enable mem 1" "enable mem 1"
179gdb_test "info mem" "1   y  .*" "mem 1 was enabled"
180
181gdb_test_no_output "disable mem 2 4"
182
183set see1 0
184set see2 0
185set see3 0
186set see4 0
187set see5 0
188
189gdb_test_multiple "info mem" "mem 2 and 4 were disabled" {
190    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
191	set see1 1
192	exp_continue
193    }
194    -re "2   n  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
195	set see2 1
196	exp_continue
197    }
198    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
199	set see3 1
200	exp_continue
201    }
202    -re "4   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
203	set see4 1
204	exp_continue
205    }
206    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
207	set see5 1
208	exp_continue
209    }
210    -re "$gdb_prompt $" {
211	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
212	    pass "mem 2 and 4 were disabled"
213	} else {
214	    fail "mem 2 and 4 were disabled"
215	}
216    }
217}
218
219gdb_test_no_output "enable mem 2-4" "enable mem 2-4"
220
221set see1 0
222set see2 0
223set see3 0
224set see4 0
225set see5 0
226
227gdb_test_multiple "info mem" "mem 2-4 were enabled" {
228    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
229	set see1 1
230	exp_continue
231    }
232    -re "2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
233	set see2 1
234	exp_continue
235    }
236    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
237	set see3 1
238	exp_continue
239    }
240    -re "4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
241	set see4 1
242	exp_continue
243    }
244    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
245	set see5 1
246	exp_continue
247    }
248    -re "$gdb_prompt $" {
249	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
250	    pass "mem 2-4 were enabled"
251	} else {
252	    fail "mem 2-4 were enabled"
253	}
254    }
255}
256
257gdb_test_no_output "disable mem" "disable mem"
258
259set see1 0
260set see2 0
261set see3 0
262set see4 0
263set see5 0
264
265gdb_test_multiple "info mem" "mem 1 to 5 were disabled" {
266    -re "1   n  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
267	set see1 1
268	exp_continue
269    }
270    -re "2   n  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
271	set see2 1
272	exp_continue
273    }
274    -re "3   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
275	set see3 1
276	exp_continue
277    }
278    -re "4   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
279	set see4 1
280	exp_continue
281    }
282    -re "5   n  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
283	set see5 1
284	exp_continue
285    }
286    -re "$gdb_prompt $" {
287	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
288	    pass "mem 1 to 5 were disabled"
289	} else {
290	    fail "mem 1 to 5 were disabled"
291	}
292    }
293}
294
295gdb_test_no_output "enable mem" "enable mem"
296
297set see1 0
298set see2 0
299set see3 0
300set see4 0
301set see5 0
302
303gdb_test_multiple "info mem" "mem 1 to 5 were enabled" {
304    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
305	set see1 1
306	exp_continue
307    }
308    -re "2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
309	set see2 1
310	exp_continue
311    }
312    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
313	set see3 1
314	exp_continue
315    }
316    -re "4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
317	set see4 1
318	exp_continue
319    }
320    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
321	set see5 1
322	exp_continue
323    }
324    -re "$gdb_prompt $" {
325	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
326	    pass "mem 1 to 5 were enabled"
327	} else {
328	    fail "mem 1 to 5 were enabled"
329	}
330    }
331}
332
333gdb_test "disable mem 7 8" \
334    "No memory region number 7.*No memory region number 8." \
335    "disable non-existant regions"
336
337#
338# Test delete
339#
340
341set see1 0
342set see2 0
343set see3 0
344set see4 0
345set see5 0
346
347gdb_test_no_output "delete mem 1" "delete mem 1"
348gdb_test_multiple "info mem" "mem 1 was deleted" {
349    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
350	set see1 1
351	exp_continue
352    }
353    -re "2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
354	set see2 1
355	exp_continue
356    }
357    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
358	set see3 1
359	exp_continue
360    }
361    -re "4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
362	set see4 1
363	exp_continue
364    }
365    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
366	set see5 1
367	exp_continue
368    }
369    -re "$gdb_prompt $" {
370	if { !$see1 && $see2 && $see3 && $see4 && $see5 } then {
371	    pass "mem 1 was deleted"
372	} else {
373	    fail "mem 1 was deleted"
374	}
375    }
376}
377
378set see1 0
379set see2 0
380set see3 0
381set see4 0
382set see5 0
383
384gdb_test_no_output "delete mem 2 4" "delete mem 2 4"
385gdb_test_multiple "info mem" "mem 2 and 4 were deleted" {
386    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
387	set see1 1
388	exp_continue
389    }
390    -re "2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
391	set see2 1
392	exp_continue
393    }
394    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
395	set see3 1
396	exp_continue
397    }
398    -re "4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
399	set see4 1
400	exp_continue
401    }
402    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
403	set see5 1
404	exp_continue
405    }
406    -re "$gdb_prompt $" {
407	if { !$see1 && !$see2 && $see3 && !$see4 && $see5 } then {
408	    pass "mem 2 and 4 were deleted"
409	} else {
410	    fail "mem 2 and 4 were deleted"
411	}
412    }
413}
414
415set see1 0
416set see2 0
417set see3 0
418set see4 0
419set see5 0
420
421gdb_test "delete mem 2-4" \
422    "No memory region number 2.*No memory region number 4." \
423    "delete mem 2-4"
424gdb_test_multiple "info mem" "mem 2-4 were deleted" {
425    -re "1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*" {
426	set see1 1
427	exp_continue
428    }
429    -re "2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*" {
430	set see2 1
431	exp_continue
432    }
433    -re "3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
434	set see3 1
435	exp_continue
436    }
437    -re "4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*" {
438	set see4 1
439	exp_continue
440    }
441    -re "5   y  \[ \t\]+$hex $hex rw nocache .\[^\r\n\]*" {
442	set see5 1
443	exp_continue
444    }
445    -re "$gdb_prompt $" {
446	if { !$see1 && !$see2 && !$see3 && !$see4 && $see5 } then {
447	    pass "mem 2-4 were deleted"
448	} else {
449	    fail "mem 2-4 were deleted"
450	}
451    }
452}
453
454gdb_test "delete mem 8" "No memory region number 8." \
455    "delete non-existant region"
456