1 /* Memory attributes support, for GDB.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdb_string.h"
31 const struct mem_attrib default_mem_attrib =
34 MEM_WIDTH_UNSPECIFIED,
38 -1 /* Flash blocksize not specified. */
41 const struct mem_attrib unknown_mem_attrib =
44 MEM_WIDTH_UNSPECIFIED,
48 -1 /* Flash blocksize not specified. */
52 VEC(mem_region_s) *mem_region_list, *target_mem_region_list;
53 static int mem_number = 0;
55 /* If this flag is set, the memory region list should be automatically
56 updated from the target. If it is clear, the list is user-controlled
57 and should be left alone. */
58 static int mem_use_target = 1;
60 /* If this flag is set, we have tried to fetch the target memory regions
61 since the last time it was invalidated. If that list is still
62 empty, then the target can't supply memory regions. */
63 static int target_mem_regions_valid;
65 /* If this flag is set, gdb will assume that memory ranges not
66 specified by the memory map have type MEM_NONE, and will
67 emit errors on all accesses to that memory. */
68 static int inaccessible_by_default = 1;
71 show_inaccessible_by_default (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c,
75 if (inaccessible_by_default)
76 fprintf_filtered (file, _("Unknown memory addresses will "
77 "be treated as inaccessible.\n"));
79 fprintf_filtered (file, _("Unknown memory addresses "
80 "will be treated as RAM.\n"));
84 /* Predicate function which returns true if LHS should sort before RHS
85 in a list of memory regions, useful for VEC_lower_bound. */
88 mem_region_lessthan (const struct mem_region *lhs,
89 const struct mem_region *rhs)
91 return lhs->lo < rhs->lo;
94 /* A helper function suitable for qsort, used to sort a
95 VEC(mem_region_s) by starting address. */
98 mem_region_cmp (const void *untyped_lhs, const void *untyped_rhs)
100 const struct mem_region *lhs = untyped_lhs;
101 const struct mem_region *rhs = untyped_rhs;
103 if (lhs->lo < rhs->lo)
105 else if (lhs->lo == rhs->lo)
111 /* Allocate a new memory region, with default settings. */
114 mem_region_init (struct mem_region *new)
116 memset (new, 0, sizeof (struct mem_region));
118 new->attrib = default_mem_attrib;
121 /* This function should be called before any command which would
122 modify the memory region list. It will handle switching from
123 a target-provided list to a local list, if necessary. */
126 require_user_regions (int from_tty)
128 struct mem_region *m;
131 /* If we're already using a user-provided list, nothing to do. */
135 /* Switch to a user-provided list (possibly a copy of the current
139 /* If we don't have a target-provided region list yet, then
141 if (mem_region_list == NULL)
144 /* Otherwise, let the user know how to get back. */
146 warning (_("Switching to manual control of memory regions; use "
147 "\"mem auto\" to fetch regions from the target again."));
149 /* And create a new list for the user to modify. */
150 length = VEC_length (mem_region_s, target_mem_region_list);
151 mem_region_list = VEC_alloc (mem_region_s, length);
152 for (ix = 0; VEC_iterate (mem_region_s, target_mem_region_list, ix, m); ix++)
153 VEC_quick_push (mem_region_s, mem_region_list, m);
156 /* This function should be called before any command which would
157 read the memory region list, other than those which call
158 require_user_regions. It will handle fetching the
159 target-provided list, if necessary. */
162 require_target_regions (void)
164 if (mem_use_target && !target_mem_regions_valid)
166 target_mem_regions_valid = 1;
167 target_mem_region_list = target_memory_map ();
168 mem_region_list = target_mem_region_list;
173 create_mem_region (CORE_ADDR lo, CORE_ADDR hi,
174 const struct mem_attrib *attrib)
176 struct mem_region new;
179 /* lo == hi is a useless empty region. */
180 if (lo >= hi && hi != 0)
182 printf_unfiltered (_("invalid memory region: low >= high\n"));
186 mem_region_init (&new);
190 ix = VEC_lower_bound (mem_region_s, mem_region_list, &new,
191 mem_region_lessthan);
193 /* Check for an overlapping memory region. We only need to check
194 in the vicinity - at most one before and one after the
196 for (i = ix - 1; i < ix + 1; i++)
198 struct mem_region *n;
202 if (i >= VEC_length (mem_region_s, mem_region_list))
205 n = VEC_index (mem_region_s, mem_region_list, i);
207 if ((lo >= n->lo && (lo < n->hi || n->hi == 0))
208 || (hi > n->lo && (hi <= n->hi || n->hi == 0))
209 || (lo <= n->lo && (hi >= n->hi || hi == 0)))
211 printf_unfiltered (_("overlapping memory region\n"));
216 new.number = ++mem_number;
217 new.attrib = *attrib;
218 VEC_safe_insert (mem_region_s, mem_region_list, ix, &new);
222 * Look up the memory region cooresponding to ADDR.
225 lookup_mem_region (CORE_ADDR addr)
227 static struct mem_region region;
228 struct mem_region *m;
233 require_target_regions ();
235 /* First we initialize LO and HI so that they describe the entire
236 memory space. As we process the memory region chain, they are
237 redefined to describe the minimal region containing ADDR. LO
238 and HI are used in the case where no memory region is defined
239 that contains ADDR. If a memory region is disabled, it is
240 treated as if it does not exist. The initial values for LO
241 and HI represent the bottom and top of memory. */
246 /* Either find memory range containing ADDRESS, or set LO and HI
247 to the nearest boundaries of an existing memory range.
249 If we ever want to support a huge list of memory regions, this
250 check should be replaced with a binary search (probably using
252 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
254 if (m->enabled_p == 1)
256 /* If the address is in the memory region, return that
258 if (addr >= m->lo && (addr < m->hi || m->hi == 0))
261 /* This (correctly) won't match if m->hi == 0, representing
262 the top of the address space, because CORE_ADDR is unsigned;
263 no value of LO is less than zero. */
264 if (addr >= m->hi && lo < m->hi)
267 /* This will never set HI to zero; if we're here and ADDR
268 is at or below M, and the region starts at zero, then ADDR
269 would have been in the region. */
270 if (addr <= m->lo && (hi == 0 || hi > m->lo))
275 /* Because no region was found, we must cons up one based on what
276 was learned above. */
280 /* When no memory map is defined at all, we always return
281 'default_mem_attrib', so that we do not make all memory
282 inaccessible for targets that don't provide a memory map. */
283 if (inaccessible_by_default && !VEC_empty (mem_region_s, mem_region_list))
284 region.attrib = unknown_mem_attrib;
286 region.attrib = default_mem_attrib;
291 /* Invalidate any memory regions fetched from the target. */
294 invalidate_target_mem_regions (void)
296 if (!target_mem_regions_valid)
299 target_mem_regions_valid = 0;
300 VEC_free (mem_region_s, target_mem_region_list);
302 mem_region_list = NULL;
305 /* Clear memory region list. */
310 VEC_free (mem_region_s, mem_region_list);
315 mem_command (char *args, int from_tty)
319 struct mem_attrib attrib;
322 error_no_arg (_("No mem"));
324 /* For "mem auto", switch back to using a target provided list. */
325 if (strcmp (args, "auto") == 0)
330 if (mem_region_list != target_mem_region_list)
333 mem_region_list = target_mem_region_list;
340 require_user_regions (from_tty);
342 tok = strtok (args, " \t");
344 error (_("no lo address"));
345 lo = parse_and_eval_address (tok);
347 tok = strtok (NULL, " \t");
349 error (_("no hi address"));
350 hi = parse_and_eval_address (tok);
352 attrib = default_mem_attrib;
353 while ((tok = strtok (NULL, " \t")) != NULL)
355 if (strcmp (tok, "rw") == 0)
356 attrib.mode = MEM_RW;
357 else if (strcmp (tok, "ro") == 0)
358 attrib.mode = MEM_RO;
359 else if (strcmp (tok, "wo") == 0)
360 attrib.mode = MEM_WO;
362 else if (strcmp (tok, "8") == 0)
363 attrib.width = MEM_WIDTH_8;
364 else if (strcmp (tok, "16") == 0)
366 if ((lo % 2 != 0) || (hi % 2 != 0))
367 error (_("region bounds not 16 bit aligned"));
368 attrib.width = MEM_WIDTH_16;
370 else if (strcmp (tok, "32") == 0)
372 if ((lo % 4 != 0) || (hi % 4 != 0))
373 error (_("region bounds not 32 bit aligned"));
374 attrib.width = MEM_WIDTH_32;
376 else if (strcmp (tok, "64") == 0)
378 if ((lo % 8 != 0) || (hi % 8 != 0))
379 error (_("region bounds not 64 bit aligned"));
380 attrib.width = MEM_WIDTH_64;
384 else if (strcmp (tok, "hwbreak") == 0)
386 else if (strcmp (tok, "swbreak") == 0)
390 else if (strcmp (tok, "cache") == 0)
392 else if (strcmp (tok, "nocache") == 0)
396 else if (strcmp (tok, "verify") == 0)
398 else if (strcmp (tok, "noverify") == 0)
403 error (_("unknown attribute: %s"), tok);
406 create_mem_region (lo, hi, &attrib);
411 mem_info_command (char *args, int from_tty)
413 struct mem_region *m;
414 struct mem_attrib *attrib;
418 printf_filtered (_("Using memory regions provided by the target.\n"));
420 printf_filtered (_("Using user-defined memory regions.\n"));
422 require_target_regions ();
424 if (!mem_region_list)
426 printf_unfiltered (_("There are no memory regions defined.\n"));
430 printf_filtered ("Num ");
431 printf_filtered ("Enb ");
432 printf_filtered ("Low Addr ");
433 if (gdbarch_addr_bit (target_gdbarch) > 32)
434 printf_filtered (" ");
435 printf_filtered ("High Addr ");
436 if (gdbarch_addr_bit (target_gdbarch) > 32)
437 printf_filtered (" ");
438 printf_filtered ("Attrs ");
439 printf_filtered ("\n");
441 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
445 printf_filtered ("%-3d %-3c\t",
447 m->enabled_p ? 'y' : 'n');
448 if (gdbarch_addr_bit (target_gdbarch) <= 32)
449 tmp = hex_string_custom ((unsigned long) m->lo, 8);
451 tmp = hex_string_custom ((unsigned long) m->lo, 16);
453 printf_filtered ("%s ", tmp);
455 if (gdbarch_addr_bit (target_gdbarch) <= 32)
460 tmp = hex_string_custom ((unsigned long) m->hi, 8);
465 tmp = "0x10000000000000000";
467 tmp = hex_string_custom ((unsigned long) m->hi, 16);
470 printf_filtered ("%s ", tmp);
472 /* Print a token for each attribute.
474 * FIXME: Should we output a comma after each token? It may
475 * make it easier for users to read, but we'd lose the ability
476 * to cut-and-paste the list of attributes when defining a new
477 * region. Perhaps that is not important.
479 * FIXME: If more attributes are added to GDB, the output may
480 * become cluttered and difficult for users to read. At that
481 * time, we may want to consider printing tokens only if they
482 * are different from the default attribute. */
485 switch (attrib->mode)
488 printf_filtered ("rw ");
491 printf_filtered ("ro ");
494 printf_filtered ("wo ");
497 printf_filtered ("flash blocksize 0x%x ", attrib->blocksize);
501 switch (attrib->width)
504 printf_filtered ("8 ");
507 printf_filtered ("16 ");
510 printf_filtered ("32 ");
513 printf_filtered ("64 ");
515 case MEM_WIDTH_UNSPECIFIED:
521 printf_filtered ("hwbreak");
523 printf_filtered ("swbreak");
527 printf_filtered ("cache ");
529 printf_filtered ("nocache ");
533 printf_filtered ("verify ");
535 printf_filtered ("noverify ");
538 printf_filtered ("\n");
540 gdb_flush (gdb_stdout);
545 /* Enable the memory region number NUM. */
550 struct mem_region *m;
553 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
554 if (m->number == num)
559 printf_unfiltered (_("No memory region number %d.\n"), num);
563 mem_enable_command (char *args, int from_tty)
568 struct mem_region *m;
571 require_user_regions (from_tty);
573 target_dcache_invalidate ();
577 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
584 while (*p1 >= '0' && *p1 <= '9')
586 if (*p1 && *p1 != ' ' && *p1 != '\t')
587 error (_("Arguments must be memory region numbers."));
593 while (*p == ' ' || *p == '\t')
599 /* Disable the memory region number NUM. */
602 mem_disable (int num)
604 struct mem_region *m;
607 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
608 if (m->number == num)
613 printf_unfiltered (_("No memory region number %d.\n"), num);
617 mem_disable_command (char *args, int from_tty)
622 struct mem_region *m;
625 require_user_regions (from_tty);
627 target_dcache_invalidate ();
631 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
638 while (*p1 >= '0' && *p1 <= '9')
640 if (*p1 && *p1 != ' ' && *p1 != '\t')
641 error (_("Arguments must be memory region numbers."));
647 while (*p == ' ' || *p == '\t')
652 /* Delete the memory region number NUM. */
657 struct mem_region *m;
660 if (!mem_region_list)
662 printf_unfiltered (_("No memory region number %d.\n"), num);
666 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
667 if (m->number == num)
672 printf_unfiltered (_("No memory region number %d.\n"), num);
676 VEC_ordered_remove (mem_region_s, mem_region_list, ix);
680 mem_delete_command (char *args, int from_tty)
686 require_user_regions (from_tty);
688 target_dcache_invalidate ();
692 if (query (_("Delete all memory regions? ")))
701 while (*p1 >= '0' && *p1 <= '9')
703 if (*p1 && *p1 != ' ' && *p1 != '\t')
704 error (_("Arguments must be memory region numbers."));
710 while (*p == ' ' || *p == '\t')
718 dummy_cmd (char *args, int from_tty)
722 extern initialize_file_ftype _initialize_mem; /* -Wmissing-prototype */
724 static struct cmd_list_element *mem_set_cmdlist;
725 static struct cmd_list_element *mem_show_cmdlist;
728 _initialize_mem (void)
730 add_com ("mem", class_vars, mem_command, _("\
731 Define attributes for memory region or reset memory region handling to\n\
734 mem <lo addr> <hi addr> [<mode> <width> <cache>],\n\
735 where <mode> may be rw (read/write), ro (read-only) or wo (write-only),\n\
736 <width> may be 8, 16, 32, or 64, and\n\
737 <cache> may be cache or nocache"));
739 add_cmd ("mem", class_vars, mem_enable_command, _("\
740 Enable memory region.\n\
741 Arguments are the code numbers of the memory regions to enable.\n\
742 Usage: enable mem <code number>\n\
743 Do \"info mem\" to see current list of code numbers."), &enablelist);
745 add_cmd ("mem", class_vars, mem_disable_command, _("\
746 Disable memory region.\n\
747 Arguments are the code numbers of the memory regions to disable.\n\
748 Usage: disable mem <code number>\n\
749 Do \"info mem\" to see current list of code numbers."), &disablelist);
751 add_cmd ("mem", class_vars, mem_delete_command, _("\
752 Delete memory region.\n\
753 Arguments are the code numbers of the memory regions to delete.\n\
754 Usage: delete mem <code number>\n\
755 Do \"info mem\" to see current list of code numbers."), &deletelist);
757 add_info ("mem", mem_info_command,
758 _("Memory region attributes"));
760 add_prefix_cmd ("mem", class_vars, dummy_cmd, _("\
761 Memory regions settings"),
762 &mem_set_cmdlist, "set mem ",
763 0/* allow-unknown */, &setlist);
764 add_prefix_cmd ("mem", class_vars, dummy_cmd, _("\
765 Memory regions settings"),
766 &mem_show_cmdlist, "show mem ",
767 0/* allow-unknown */, &showlist);
769 add_setshow_boolean_cmd ("inaccessible-by-default", no_class,
770 &inaccessible_by_default, _("\
771 Set handling of unknown memory regions."), _("\
772 Show handling of unknown memory regions."), _("\
773 If on, and some memory map is defined, debugger will emit errors on\n\
774 accesses to memory not defined in the memory map. If off, accesses to all\n\
775 memory addresses will be allowed."),
777 show_inaccessible_by_default,