1 /* Memory attributes support, for GDB.
2 Copyright 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "gdb_string.h"
30 /* FIXME: While this conflicts with the enum defined in breakpoint.h,
31 I used them to be consistant with how breakpoints, tracepoints, and
32 displays are implemented. It doesn't lose now because breakpoint.h
40 const struct mem_attrib default_mem_attrib
=
43 MEM_WIDTH_UNSPECIFIED
,
49 static struct mem_region
*mem_region_chain
= NULL
;
50 static int mem_number
= 0;
52 static struct mem_region
*
53 create_mem_region (CORE_ADDR lo
, CORE_ADDR hi
,
54 const struct mem_attrib
*attrib
)
56 struct mem_region
*n
, *new;
60 printf_unfiltered ("invalid memory region\n");
67 /* overlapping node */
68 if ((lo
>= n
->lo
&& lo
<= n
->hi
) ||
69 (hi
>= n
->lo
&& hi
<= n
->hi
))
71 printf_unfiltered ("overlapping memory region\n");
77 new = xmalloc (sizeof (struct mem_region
));
80 new->number
= ++mem_number
;
81 new->status
= enabled
;
82 new->attrib
= *attrib
;
84 /* link in new node */
85 new->next
= mem_region_chain
;
86 mem_region_chain
= new;
92 delete_mem_region (struct mem_region
*m
)
98 * Look up the memory region cooresponding to ADDR.
101 lookup_mem_region (CORE_ADDR addr
)
103 static struct mem_region region
;
104 struct mem_region
*m
;
108 /* First we initialize LO and HI so that they describe the entire
109 memory space. As we process the memory region chain, they are
110 redefined to describe the minimal region containing ADDR. LO
111 and HI are used in the case where no memory region is defined
112 that contains ADDR. If a memory region is disabled, it is
113 treated as if it does not exist. */
116 hi
= (CORE_ADDR
) ~ 0;
118 for (m
= mem_region_chain
; m
; m
= m
->next
)
120 if (m
->status
== enabled
)
122 if (addr
>= m
->lo
&& addr
< m
->hi
)
125 if (addr
>= m
->hi
&& lo
< m
->hi
)
128 if (addr
<= m
->lo
&& hi
> m
->lo
)
133 /* Because no region was found, we must cons up one based on what
134 was learned above. */
137 region
.attrib
= default_mem_attrib
;
143 mem_command (char *args
, int from_tty
)
147 struct mem_attrib attrib
;
150 error_no_arg ("No mem");
152 tok
= strtok (args
, " \t");
154 error ("no lo address");
155 lo
= parse_and_eval_address (tok
);
157 tok
= strtok (NULL
, " \t");
159 error ("no hi address");
160 hi
= parse_and_eval_address (tok
);
162 attrib
= default_mem_attrib
;
163 while ((tok
= strtok (NULL
, " \t")) != NULL
)
165 if (strcmp (tok
, "rw") == 0)
166 attrib
.mode
= MEM_RW
;
167 else if (strcmp (tok
, "ro") == 0)
168 attrib
.mode
= MEM_RO
;
169 else if (strcmp (tok
, "wo") == 0)
170 attrib
.mode
= MEM_WO
;
172 else if (strcmp (tok
, "8") == 0)
173 attrib
.width
= MEM_WIDTH_8
;
174 else if (strcmp (tok
, "16") == 0)
176 if ((lo
% 2 != 0) || (hi
% 2 != 0))
177 error ("region bounds not 16 bit aligned");
178 attrib
.width
= MEM_WIDTH_16
;
180 else if (strcmp (tok
, "32") == 0)
182 if ((lo
% 4 != 0) || (hi
% 4 != 0))
183 error ("region bounds not 32 bit aligned");
184 attrib
.width
= MEM_WIDTH_32
;
186 else if (strcmp (tok
, "64") == 0)
188 if ((lo
% 8 != 0) || (hi
% 8 != 0))
189 error ("region bounds not 64 bit aligned");
190 attrib
.width
= MEM_WIDTH_64
;
194 else if (strcmp (tok
, "hwbreak") == 0)
195 attrib
.hwbreak
= true;
196 else if (strcmp (tok
, "swbreak") == 0)
197 attrib
.hwbreak
= false;
200 else if (strcmp (tok
, "cache") == 0)
202 else if (strcmp (tok
, "nocache") == 0)
203 attrib
.cache
= false;
206 else if (strcmp (tok
, "verify") == 0)
207 attrib
.verify
= true;
208 else if (strcmp (tok
, "noverify") == 0)
209 attrib
.verify
= false;
213 error ("unknown attribute: %s", tok
);
216 create_mem_region (lo
, hi
, &attrib
);
221 mem_info_command (char *args
, int from_tty
)
223 struct mem_region
*m
;
224 struct mem_attrib
*attrib
;
226 if (!mem_region_chain
)
228 printf_unfiltered ("There are no memory regions defined.\n");
232 printf_filtered ("Memory regions now in effect:\n");
233 for (m
= mem_region_chain
; m
; m
= m
->next
)
235 printf_filtered ("%d: %c\t",
237 m
->status
? 'y' : 'n');
238 printf_filtered ("%s - ",
239 local_hex_string_custom ((unsigned long) m
->lo
, "08l"));
240 printf_filtered ("%s\t",
241 local_hex_string_custom ((unsigned long) m
->hi
, "08l"));
243 /* Print a token for each attribute.
245 * FIXME: Should we output a comma after each token? It may
246 * make it easier for users to read, but we'd lose the ability
247 * to cut-and-paste the list of attributes when defining a new
248 * region. Perhaps that is not important.
250 * FIXME: If more attributes are added to GDB, the output may
251 * become cluttered and difficult for users to read. At that
252 * time, we may want to consider printing tokens only if they
253 * are different from the default attribute. */
256 switch (attrib
->mode
)
259 printf_filtered ("rw ");
262 printf_filtered ("ro ");
265 printf_filtered ("wo ");
269 switch (attrib
->width
)
272 printf_filtered ("8 ");
275 printf_filtered ("16 ");
278 printf_filtered ("32 ");
281 printf_filtered ("64 ");
283 case MEM_WIDTH_UNSPECIFIED
:
289 printf_filtered ("hwbreak");
291 printf_filtered ("swbreak");
295 printf_filtered ("cache ");
297 printf_filtered ("nocache ");
301 printf_filtered ("verify ");
303 printf_filtered ("noverify ");
306 printf_filtered ("\n");
308 gdb_flush (gdb_stdout
);
313 /* Enable the memory region number NUM. */
318 struct mem_region
*m
;
320 for (m
= mem_region_chain
; m
; m
= m
->next
)
321 if (m
->number
== num
)
326 printf_unfiltered ("No memory region number %d.\n", num
);
330 mem_enable_command (char *args
, int from_tty
)
335 struct mem_region
*m
;
337 dcache_invalidate (target_dcache
);
341 for (m
= mem_region_chain
; m
; m
= m
->next
)
348 while (*p1
>= '0' && *p1
<= '9')
350 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
351 error ("Arguments must be memory region numbers.");
357 while (*p
== ' ' || *p
== '\t')
363 /* Disable the memory region number NUM. */
366 mem_disable (int num
)
368 struct mem_region
*m
;
370 for (m
= mem_region_chain
; m
; m
= m
->next
)
371 if (m
->number
== num
)
373 m
->status
= disabled
;
376 printf_unfiltered ("No memory region number %d.\n", num
);
380 mem_disable_command (char *args
, int from_tty
)
385 struct mem_region
*m
;
387 dcache_invalidate (target_dcache
);
391 for (m
= mem_region_chain
; m
; m
= m
->next
)
392 m
->status
= disabled
;
398 while (*p1
>= '0' && *p1
<= '9')
400 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
401 error ("Arguments must be memory region numbers.");
407 while (*p
== ' ' || *p
== '\t')
412 /* Clear memory region list */
417 struct mem_region
*m
;
419 while ((m
= mem_region_chain
) != 0)
421 mem_region_chain
= m
->next
;
422 delete_mem_region (m
);
426 /* Delete the memory region number NUM. */
431 struct mem_region
*m1
, *m
;
433 if (!mem_region_chain
)
435 printf_unfiltered ("No memory region number %d.\n", num
);
439 if (mem_region_chain
->number
== num
)
441 m1
= mem_region_chain
;
442 mem_region_chain
= m1
->next
;
443 delete_mem_region (m1
);
446 for (m
= mem_region_chain
; m
->next
; m
= m
->next
)
448 if (m
->next
->number
== num
)
452 delete_mem_region (m1
);
459 mem_delete_command (char *args
, int from_tty
)
465 dcache_invalidate (target_dcache
);
469 if (query ("Delete all memory regions? "))
478 while (*p1
>= '0' && *p1
<= '9')
480 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
481 error ("Arguments must be memory region numbers.");
487 while (*p
== ' ' || *p
== '\t')
497 add_com ("mem", class_vars
, mem_command
,
498 "Define attributes for memory region.");
500 add_cmd ("mem", class_vars
, mem_enable_command
,
501 "Enable memory region.\n\
502 Arguments are the code numbers of the memory regions to enable.\n\
503 Do \"info mem\" to see current list of code numbers.", &enablelist
);
505 add_cmd ("mem", class_vars
, mem_disable_command
,
506 "Disable memory region.\n\
507 Arguments are the code numbers of the memory regions to disable.\n\
508 Do \"info mem\" to see current list of code numbers.", &disablelist
);
510 add_cmd ("mem", class_vars
, mem_delete_command
,
511 "Delete memory region.\n\
512 Arguments are the code numbers of the memory regions to delete.\n\
513 Do \"info mem\" to see current list of code numbers.", &deletelist
);
515 add_info ("mem", mem_info_command
,
516 "Memory region attributes");