1 /* Memory attributes support, for GDB.
3 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
29 #include "gdb_string.h"
31 const struct mem_attrib default_mem_attrib
=
34 MEM_WIDTH_UNSPECIFIED
,
40 static struct mem_region
*mem_region_chain
= NULL
;
41 static int mem_number
= 0;
43 static struct mem_region
*
44 create_mem_region (CORE_ADDR lo
, CORE_ADDR hi
,
45 const struct mem_attrib
*attrib
)
47 struct mem_region
*n
, *new;
49 /* lo == hi is a useless empty region */
50 if (lo
>= hi
&& hi
!= 0)
52 printf_unfiltered (_("invalid memory region: low >= high\n"));
59 /* overlapping node */
60 if ((lo
>= n
->lo
&& (lo
< n
->hi
|| n
->hi
== 0))
61 || (hi
> n
->lo
&& (hi
<= n
->hi
|| n
->hi
== 0))
62 || (lo
<= n
->lo
&& (hi
>= n
->hi
|| hi
== 0)))
64 printf_unfiltered (_("overlapping memory region\n"));
70 new = xmalloc (sizeof (struct mem_region
));
73 new->number
= ++mem_number
;
75 new->attrib
= *attrib
;
77 /* link in new node */
78 new->next
= mem_region_chain
;
79 mem_region_chain
= new;
85 delete_mem_region (struct mem_region
*m
)
91 * Look up the memory region cooresponding to ADDR.
94 lookup_mem_region (CORE_ADDR addr
)
96 static struct mem_region region
;
101 /* First we initialize LO and HI so that they describe the entire
102 memory space. As we process the memory region chain, they are
103 redefined to describe the minimal region containing ADDR. LO
104 and HI are used in the case where no memory region is defined
105 that contains ADDR. If a memory region is disabled, it is
106 treated as if it does not exist. */
109 hi
= (CORE_ADDR
) ~ 0;
111 for (m
= mem_region_chain
; m
; m
= m
->next
)
113 if (m
->enabled_p
== 1)
115 if (addr
>= m
->lo
&& (addr
< m
->hi
|| m
->hi
== 0))
118 if (addr
>= m
->hi
&& lo
< m
->hi
)
121 if (addr
<= m
->lo
&& hi
> m
->lo
)
126 /* Because no region was found, we must cons up one based on what
127 was learned above. */
130 region
.attrib
= default_mem_attrib
;
136 mem_command (char *args
, int from_tty
)
140 struct mem_attrib attrib
;
143 error_no_arg (_("No mem"));
145 tok
= strtok (args
, " \t");
147 error (_("no lo address"));
148 lo
= parse_and_eval_address (tok
);
150 tok
= strtok (NULL
, " \t");
152 error (_("no hi address"));
153 hi
= parse_and_eval_address (tok
);
155 attrib
= default_mem_attrib
;
156 while ((tok
= strtok (NULL
, " \t")) != NULL
)
158 if (strcmp (tok
, "rw") == 0)
159 attrib
.mode
= MEM_RW
;
160 else if (strcmp (tok
, "ro") == 0)
161 attrib
.mode
= MEM_RO
;
162 else if (strcmp (tok
, "wo") == 0)
163 attrib
.mode
= MEM_WO
;
165 else if (strcmp (tok
, "8") == 0)
166 attrib
.width
= MEM_WIDTH_8
;
167 else if (strcmp (tok
, "16") == 0)
169 if ((lo
% 2 != 0) || (hi
% 2 != 0))
170 error (_("region bounds not 16 bit aligned"));
171 attrib
.width
= MEM_WIDTH_16
;
173 else if (strcmp (tok
, "32") == 0)
175 if ((lo
% 4 != 0) || (hi
% 4 != 0))
176 error (_("region bounds not 32 bit aligned"));
177 attrib
.width
= MEM_WIDTH_32
;
179 else if (strcmp (tok
, "64") == 0)
181 if ((lo
% 8 != 0) || (hi
% 8 != 0))
182 error (_("region bounds not 64 bit aligned"));
183 attrib
.width
= MEM_WIDTH_64
;
187 else if (strcmp (tok
, "hwbreak") == 0)
189 else if (strcmp (tok
, "swbreak") == 0)
193 else if (strcmp (tok
, "cache") == 0)
195 else if (strcmp (tok
, "nocache") == 0)
199 else if (strcmp (tok
, "verify") == 0)
201 else if (strcmp (tok
, "noverify") == 0)
206 error (_("unknown attribute: %s"), tok
);
209 create_mem_region (lo
, hi
, &attrib
);
214 mem_info_command (char *args
, int from_tty
)
216 struct mem_region
*m
;
217 struct mem_attrib
*attrib
;
219 if (!mem_region_chain
)
221 printf_unfiltered (_("There are no memory regions defined.\n"));
225 printf_filtered ("Num ");
226 printf_filtered ("Enb ");
227 printf_filtered ("Low Addr ");
228 if (TARGET_ADDR_BIT
> 32)
229 printf_filtered (" ");
230 printf_filtered ("High Addr ");
231 if (TARGET_ADDR_BIT
> 32)
232 printf_filtered (" ");
233 printf_filtered ("Attrs ");
234 printf_filtered ("\n");
236 for (m
= mem_region_chain
; m
; m
= m
->next
)
239 printf_filtered ("%-3d %-3c\t",
241 m
->enabled_p
? 'y' : 'n');
242 if (TARGET_ADDR_BIT
<= 32)
243 tmp
= hex_string_custom ((unsigned long) m
->lo
, 8);
245 tmp
= hex_string_custom ((unsigned long) m
->lo
, 16);
247 printf_filtered ("%s ", tmp
);
249 if (TARGET_ADDR_BIT
<= 32)
254 tmp
= hex_string_custom ((unsigned long) m
->hi
, 8);
259 tmp
= "0x10000000000000000";
261 tmp
= hex_string_custom ((unsigned long) m
->hi
, 16);
264 printf_filtered ("%s ", tmp
);
266 /* Print a token for each attribute.
268 * FIXME: Should we output a comma after each token? It may
269 * make it easier for users to read, but we'd lose the ability
270 * to cut-and-paste the list of attributes when defining a new
271 * region. Perhaps that is not important.
273 * FIXME: If more attributes are added to GDB, the output may
274 * become cluttered and difficult for users to read. At that
275 * time, we may want to consider printing tokens only if they
276 * are different from the default attribute. */
279 switch (attrib
->mode
)
282 printf_filtered ("rw ");
285 printf_filtered ("ro ");
288 printf_filtered ("wo ");
292 switch (attrib
->width
)
295 printf_filtered ("8 ");
298 printf_filtered ("16 ");
301 printf_filtered ("32 ");
304 printf_filtered ("64 ");
306 case MEM_WIDTH_UNSPECIFIED
:
312 printf_filtered ("hwbreak");
314 printf_filtered ("swbreak");
318 printf_filtered ("cache ");
320 printf_filtered ("nocache ");
324 printf_filtered ("verify ");
326 printf_filtered ("noverify ");
329 printf_filtered ("\n");
331 gdb_flush (gdb_stdout
);
336 /* Enable the memory region number NUM. */
341 struct mem_region
*m
;
343 for (m
= mem_region_chain
; m
; m
= m
->next
)
344 if (m
->number
== num
)
349 printf_unfiltered (_("No memory region number %d.\n"), num
);
353 mem_enable_command (char *args
, int from_tty
)
358 struct mem_region
*m
;
360 dcache_invalidate (target_dcache
);
364 for (m
= mem_region_chain
; m
; m
= m
->next
)
371 while (*p1
>= '0' && *p1
<= '9')
373 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
374 error (_("Arguments must be memory region numbers."));
380 while (*p
== ' ' || *p
== '\t')
386 /* Disable the memory region number NUM. */
389 mem_disable (int num
)
391 struct mem_region
*m
;
393 for (m
= mem_region_chain
; m
; m
= m
->next
)
394 if (m
->number
== num
)
399 printf_unfiltered (_("No memory region number %d.\n"), num
);
403 mem_disable_command (char *args
, int from_tty
)
408 struct mem_region
*m
;
410 dcache_invalidate (target_dcache
);
414 for (m
= mem_region_chain
; m
; m
= m
->next
)
421 while (*p1
>= '0' && *p1
<= '9')
423 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
424 error (_("Arguments must be memory region numbers."));
430 while (*p
== ' ' || *p
== '\t')
435 /* Clear memory region list */
440 struct mem_region
*m
;
442 while ((m
= mem_region_chain
) != 0)
444 mem_region_chain
= m
->next
;
445 delete_mem_region (m
);
449 /* Delete the memory region number NUM. */
454 struct mem_region
*m1
, *m
;
456 if (!mem_region_chain
)
458 printf_unfiltered (_("No memory region number %d.\n"), num
);
462 if (mem_region_chain
->number
== num
)
464 m1
= mem_region_chain
;
465 mem_region_chain
= m1
->next
;
466 delete_mem_region (m1
);
469 for (m
= mem_region_chain
; m
->next
; m
= m
->next
)
471 if (m
->next
->number
== num
)
475 delete_mem_region (m1
);
482 mem_delete_command (char *args
, int from_tty
)
488 dcache_invalidate (target_dcache
);
492 if (query ("Delete all memory regions? "))
501 while (*p1
>= '0' && *p1
<= '9')
503 if (*p1
&& *p1
!= ' ' && *p1
!= '\t')
504 error (_("Arguments must be memory region numbers."));
510 while (*p
== ' ' || *p
== '\t')
517 extern initialize_file_ftype _initialize_mem
; /* -Wmissing-prototype */
520 _initialize_mem (void)
522 add_com ("mem", class_vars
, mem_command
, _("\
523 Define attributes for memory region.\n\
524 Usage: mem <lo addr> <hi addr> [<mode> <width> <cache>], \n\
525 where <mode> may be rw (read/write), ro (read-only) or wo (write-only), \n\
526 <width> may be 8, 16, 32, or 64, and \n\
527 <cache> may be cache or nocache"));
529 add_cmd ("mem", class_vars
, mem_enable_command
, _("\
530 Enable memory region.\n\
531 Arguments are the code numbers of the memory regions to enable.\n\
532 Usage: enable mem <code number>\n\
533 Do \"info mem\" to see current list of code numbers."), &enablelist
);
535 add_cmd ("mem", class_vars
, mem_disable_command
, _("\
536 Disable memory region.\n\
537 Arguments are the code numbers of the memory regions to disable.\n\
538 Usage: disable mem <code number>\n\
539 Do \"info mem\" to see current list of code numbers."), &disablelist
);
541 add_cmd ("mem", class_vars
, mem_delete_command
, _("\
542 Delete memory region.\n\
543 Arguments are the code numbers of the memory regions to delete.\n\
544 Usage: delete mem <code number>\n\
545 Do \"info mem\" to see current list of code numbers."), &deletelist
);
547 add_info ("mem", mem_info_command
,
548 _("Memory region attributes"));