xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/memattr.exp (revision d536862b7d93d77932ef5de7eebdc48d76921b77)
1# Copyright 2011-2019 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 is part of the gdb testsuite
17
18# Test the memory attribute commands.
19
20standard_testfile .c
21
22if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
23    return -1
24}
25
26runto main
27
28# Delete all target-supplied memory regions.
29delete_memory_regions
30
31set mem1start -1
32set mem2start -1
33set mem3start -1
34set mem4start -1
35set mem5start -1
36
37set mem1end -1
38set mem2end -1
39set mem3end -1
40set mem4end -1
41set mem5end -1
42
43
44gdb_test_multiple "info address mem1" "get address of mem1" {
45    -re "Symbol \"mem1\" is static storage at address ($hex).*$gdb_prompt $" {
46	set mem1start $expect_out(1,string)
47    }
48}
49
50gdb_test_multiple "info address mem2" "get address of mem2" {
51    -re "Symbol \"mem2\" is static storage at address ($hex).*$gdb_prompt $" {
52	set mem2start $expect_out(1,string)
53    }
54}
55
56gdb_test_multiple "info address mem3" "get address of mem3" {
57    -re "Symbol \"mem3\" is static storage at address ($hex).*$gdb_prompt $" {
58	set mem3start $expect_out(1,string)
59    }
60}
61
62gdb_test_multiple "info address mem4" "get address of mem4" {
63    -re "Symbol \"mem4\" is static storage at address ($hex).*$gdb_prompt $" {
64	set mem4start $expect_out(1,string)
65    }
66}
67
68gdb_test_multiple "info address mem5" "get address of mem5" {
69    -re "Symbol \"mem5\" is static storage at address ($hex).*$gdb_prompt $" {
70	set mem5start $expect_out(1,string)
71    }
72}
73
74gdb_test_multiple "print &mem1\[64\]" "get end of mem1" {
75    -re "$decimal = .* ($hex).*$gdb_prompt $" {
76	set mem1end $expect_out(1,string)
77    }
78}
79
80gdb_test_multiple "print &mem2\[64\]" "get end of mem2" {
81    -re "$decimal = .* ($hex).*$gdb_prompt $" {
82	set mem2end $expect_out(1,string)
83    }
84}
85
86gdb_test_multiple "print &mem3\[64\]" "get end of mem3" {
87    -re "$decimal = .* ($hex).*$gdb_prompt $" {
88	set mem3end $expect_out(1,string)
89    }
90}
91
92gdb_test_multiple "print &mem4\[64\]" "get end of mem4" {
93    -re "$decimal = .* ($hex).*$gdb_prompt $" {
94	set mem4end $expect_out(1,string)
95    }
96}
97
98gdb_test_multiple "print &mem5\[64\]" "get end of mem5" {
99    -re "$decimal = .* ($hex).*$gdb_prompt $" {
100	set mem5end $expect_out(1,string)
101    }
102}
103
104gdb_test_no_output "mem $mem1start $mem1end wo" "create mem region 1"
105gdb_test_no_output "mem $mem2start $mem2end ro" "create mem region 2"
106gdb_test_no_output "mem $mem3start $mem3end rw" "create mem region 3"
107gdb_test_no_output "mem $mem4start $mem4end rw" "create mem region 4"
108gdb_test_no_output "mem $mem5start $mem5end rw" "create mem region 5"
109
110set see1 0
111set see2 0
112set see3 0
113set see4 0
114set see5 0
115
116set info_mem_header_pattern \
117    "info mem.*Num\[ \t\]+Enb\[ \t\]+Low\[ \t\]+Addr\[ \t\]+High\[ \t\]+Addr\[ \t\]+Attrs\[^\r\n\]*\r\n"
118
119gdb_test_multiple "info mem" "info mem(1)" {
120    -re ${info_mem_header_pattern} {
121	# Discard the header.
122	exp_continue
123    }
124    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*\r\n" {
125	set see1 1
126	exp_continue
127    }
128    -re "^2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*\r\n" {
129	set see2 1
130	exp_continue
131    }
132    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*\r\n" {
133	set see3 1
134	exp_continue
135    }
136    -re "^4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*\r\n" {
137	set see4 1
138	exp_continue
139    }
140    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*\r\n" {
141	set see5 1
142	exp_continue
143    }
144    -re "$gdb_prompt $" {
145	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
146	    pass "info mem (1)"
147	} else {
148	    fail "info mem (1)"
149	}
150    }
151}
152
153#
154# Test read-only, write-only
155#
156
157# mem1 is write only: read should fail.
158gdb_test "print mem1\[1\]" \
159    "Cannot access memory at address $hex" \
160    "mem1 cannot be read"
161
162gdb_test "print mem1\[1\] = 9" \
163    "$decimal = 9" \
164    "mem1 can be written"
165
166# mem2 is read only: write should fail.
167gdb_test "print mem2\[1\] = 9" \
168    "Cannot access memory at address $hex" \
169    "mem2 cannot be written"
170
171gdb_test "print mem2\[1\]" \
172    "$decimal = 0" \
173    "mem2 can be read"
174
175#
176# Test disable and enable
177#
178
179gdb_test_no_output "disable mem 1" "disable mem 1"
180gdb_test "info mem" "1   n  .*" "mem 1 was disabled"
181
182gdb_test_no_output "enable mem 1" "enable mem 1"
183gdb_test "info mem" "1   y  .*" "mem 1 was enabled"
184
185gdb_test_no_output "disable mem 2 4"
186
187set see1 0
188set see2 0
189set see3 0
190set see4 0
191set see5 0
192
193gdb_test_multiple "info mem" "mem 2 and 4 were disabled" {
194    -re ${info_mem_header_pattern} {
195	# Discard the header.
196	exp_continue
197    }
198    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
199	set see1 1
200	exp_continue
201    }
202    -re "^2   n  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
203	set see2 1
204	exp_continue
205    }
206    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
207	set see3 1
208	exp_continue
209    }
210    -re "^4   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
211	set see4 1
212	exp_continue
213    }
214    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
215	set see5 1
216	exp_continue
217    }
218    -re "$gdb_prompt $" {
219	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
220	    pass "mem 2 and 4 were disabled"
221	} else {
222	    fail "mem 2 and 4 were disabled"
223	}
224    }
225}
226
227gdb_test_no_output "enable mem 2-4" "enable mem 2-4"
228
229set see1 0
230set see2 0
231set see3 0
232set see4 0
233set see5 0
234
235gdb_test_multiple "info mem" "mem 2-4 were enabled" {
236    -re ${info_mem_header_pattern} {
237	# Discard the header.
238	exp_continue
239    }
240    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
241	set see1 1
242	exp_continue
243    }
244    -re "^2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
245	set see2 1
246	exp_continue
247    }
248    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
249	set see3 1
250	exp_continue
251    }
252    -re "^4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
253	set see4 1
254	exp_continue
255    }
256    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
257	set see5 1
258	exp_continue
259    }
260    -re "$gdb_prompt $" {
261	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
262	    pass "mem 2-4 were enabled"
263	} else {
264	    fail "mem 2-4 were enabled"
265	}
266    }
267}
268
269gdb_test_no_output "disable mem" "disable mem"
270
271set see1 0
272set see2 0
273set see3 0
274set see4 0
275set see5 0
276
277gdb_test_multiple "info mem" "mem 1 to 5 were disabled" {
278    -re ${info_mem_header_pattern} {
279	# Discard the header.
280	exp_continue
281    }
282    -re "^1   n  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
283	set see1 1
284	exp_continue
285    }
286    -re "^2   n  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
287	set see2 1
288	exp_continue
289    }
290    -re "^3   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
291	set see3 1
292	exp_continue
293    }
294    -re "^4   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
295	set see4 1
296	exp_continue
297    }
298    -re "^5   n  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
299	set see5 1
300	exp_continue
301    }
302    -re "$gdb_prompt $" {
303	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
304	    pass "mem 1 to 5 were disabled"
305	} else {
306	    fail "mem 1 to 5 were disabled"
307	}
308    }
309}
310
311gdb_test_no_output "enable mem" "enable mem"
312
313set see1 0
314set see2 0
315set see3 0
316set see4 0
317set see5 0
318
319gdb_test_multiple "info mem" "mem 1 to 5 were enabled" {
320    -re ${info_mem_header_pattern} {
321	# Discard the header.
322	exp_continue
323    }
324    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
325	set see1 1
326	exp_continue
327    }
328    -re "^2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
329	set see2 1
330	exp_continue
331    }
332    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
333	set see3 1
334	exp_continue
335    }
336    -re "^4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
337	set see4 1
338	exp_continue
339    }
340    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
341	set see5 1
342	exp_continue
343    }
344    -re "$gdb_prompt $" {
345	if { $see1 && $see2 && $see3 && $see4 && $see5 } then {
346	    pass "mem 1 to 5 were enabled"
347	} else {
348	    fail "mem 1 to 5 were enabled"
349	}
350    }
351}
352
353gdb_test "disable mem 7 8" \
354    "No memory region number 7.*No memory region number 8." \
355    "disable non-existant regions"
356
357#
358# Test delete
359#
360
361set see1 0
362set see2 0
363set see3 0
364set see4 0
365set see5 0
366
367gdb_test_no_output "delete mem 1" "delete mem 1"
368gdb_test_multiple "info mem" "mem 1 was deleted" {
369    -re ${info_mem_header_pattern} {
370	# Discard the header.
371	exp_continue
372    }
373    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
374	set see1 1
375	exp_continue
376    }
377    -re "^2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
378	set see2 1
379	exp_continue
380    }
381    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
382	set see3 1
383	exp_continue
384    }
385    -re "^4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
386	set see4 1
387	exp_continue
388    }
389    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
390	set see5 1
391	exp_continue
392    }
393    -re "$gdb_prompt $" {
394	if { !$see1 && $see2 && $see3 && $see4 && $see5 } then {
395	    pass "mem 1 was deleted"
396	} else {
397	    fail "mem 1 was deleted"
398	}
399    }
400}
401
402set see1 0
403set see2 0
404set see3 0
405set see4 0
406set see5 0
407
408gdb_test_no_output "delete mem 2 4" "delete mem 2 4"
409gdb_test_multiple "info mem" "mem 2 and 4 were deleted" {
410    -re ${info_mem_header_pattern} {
411	# Discard the header.
412	exp_continue
413    }
414    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
415	set see1 1
416	exp_continue
417    }
418    -re "^2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
419	set see2 1
420	exp_continue
421    }
422    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
423	set see3 1
424	exp_continue
425    }
426    -re "^4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
427	set see4 1
428	exp_continue
429    }
430    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
431	set see5 1
432	exp_continue
433    }
434    -re "$gdb_prompt $" {
435	if { !$see1 && !$see2 && $see3 && !$see4 && $see5 } then {
436	    pass "mem 2 and 4 were deleted"
437	} else {
438	    fail "mem 2 and 4 were deleted"
439	}
440    }
441}
442
443set see1 0
444set see2 0
445set see3 0
446set see4 0
447set see5 0
448
449gdb_test "delete mem 2-4" \
450    "No memory region number 2.*No memory region number 4." \
451    "delete mem 2-4"
452gdb_test_multiple "info mem" "mem 2-4 were deleted" {
453    -re ${info_mem_header_pattern} {
454	# Discard the header.
455	exp_continue
456    }
457    -re "^1   y  \[ \t\]+$hex $hex wo nocache \[^\r\n\]*.." {
458	set see1 1
459	exp_continue
460    }
461    -re "^2   y  \[ \t\]+$hex $hex ro nocache \[^\r\n\]*.." {
462	set see2 1
463	exp_continue
464    }
465    -re "^3   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
466	set see3 1
467	exp_continue
468    }
469    -re "^4   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
470	set see4 1
471	exp_continue
472    }
473    -re "^5   y  \[ \t\]+$hex $hex rw nocache \[^\r\n\]*.." {
474	set see5 1
475	exp_continue
476    }
477    -re "$gdb_prompt $" {
478	if { !$see1 && !$see2 && !$see3 && !$see4 && $see5 } then {
479	    pass "mem 2-4 were deleted"
480	} else {
481	    fail "mem 2-4 were deleted"
482	}
483    }
484}
485
486gdb_test "delete mem 8" "No memory region number 8." \
487    "delete non-existant region"
488
489#
490# Test overlapping checking
491#
492
493# Create a region that doesn't overlap (a PASS in the table).
494
495proc region_pass { region } {
496    gdb_test_no_output "mem $region ro" "$region: no-overlap"
497}
498
499# Try to create a region that overlaps (a FAIL in the table).
500
501proc region_fail { region } {
502    gdb_test "mem $region ro" "overlapping memory region" "$region: overlap"
503}
504
505# Test normal case (upper != 0)
506#
507#        lo'       hi'
508#         |--------|
509#  10 20 30 40 50 60 70 80 90
510#      |-----|                FAIL
511#         |--|                FAIL
512#            |--|             FAIL
513#               |--|          FAIL
514#               |-----|       FAIL
515#         |--------|          FAIL
516#      |--------------|       FAIL
517#      |--------------------- FAIL
518#         |------------------ FAIL
519#            |--------------- FAIL
520#      |--|                   PASS
521#                  |--|       PASS
522#                        |--- PASS
523
524# Clear the memory regions list.
525delete_memory_regions
526gdb_test_no_output "mem 0x30 0x60 ro"
527with_test_prefix "0x30 0x60" {
528    region_fail "0x20 0x40"
529    region_fail "0x30 0x40"
530    region_fail "0x40 0x50"
531    region_fail "0x50 0x60"
532    region_fail "0x50 0x70"
533    region_fail "0x30 0x60"
534    region_fail "0x20 0x70"
535    region_fail "0x20 0x0"
536    region_fail "0x30 0x0"
537    region_fail "0x40 0x0"
538    region_pass "0x20 0x30"
539    region_pass "0x60 0x70"
540    region_pass "0x80 0x0"
541}
542
543# Test special case (upper == 0)
544#
545#           lo'             hi'
546#            |---------------
547#  00 10 20 30 40 50 60 70 80
548#         |--------|          FAIL
549#            |-----|          FAIL
550#               |--|          FAIL
551#         |------------------ FAIL
552#            |--------------- FAIL
553#               |------------ FAIL
554#         |--|                PASS
555#   |--|                      PASS
556
557# Clear the memory regions list.
558delete_memory_regions
559gdb_test_no_output "mem 0x30 0x0 ro"
560with_test_prefix "0x30 0x0" {
561    region_fail "0x20 0x50"
562    region_fail "0x30 0x50"
563    region_fail "0x40 0x50"
564    region_fail "0x20 0x0"
565    region_fail "0x30 0x0"
566    region_fail "0x40 0x0"
567    region_pass "0x20 0x30"
568    region_pass "0x00 0x10"
569}
570