1 /* Memory attributes support, for GDB.
3 Copyright (C) 2001-2017 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 3 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, see <http://www.gnu.org/licenses/>. */
25 #include "target-dcache.h"
29 #include "breakpoint.h"
30 #include "cli/cli-utils.h"
33 static std::vector
<mem_region
> user_mem_region_list
, target_mem_region_list
;
34 static std::vector
<mem_region
> *mem_region_list
= &target_mem_region_list
;
35 static int mem_number
= 0;
37 /* If this flag is set, the memory region list should be automatically
38 updated from the target. If it is clear, the list is user-controlled
39 and should be left alone. */
44 return mem_region_list
== &target_mem_region_list
;
47 /* If this flag is set, we have tried to fetch the target memory regions
48 since the last time it was invalidated. If that list is still
49 empty, then the target can't supply memory regions. */
50 static bool target_mem_regions_valid
;
52 /* If this flag is set, gdb will assume that memory ranges not
53 specified by the memory map have type MEM_NONE, and will
54 emit errors on all accesses to that memory. */
55 static int inaccessible_by_default
= 1;
58 show_inaccessible_by_default (struct ui_file
*file
, int from_tty
,
59 struct cmd_list_element
*c
,
62 if (inaccessible_by_default
)
63 fprintf_filtered (file
, _("Unknown memory addresses will "
64 "be treated as inaccessible.\n"));
66 fprintf_filtered (file
, _("Unknown memory addresses "
67 "will be treated as RAM.\n"));
70 /* This function should be called before any command which would
71 modify the memory region list. It will handle switching from
72 a target-provided list to a local list, if necessary. */
75 require_user_regions (int from_tty
)
80 /* If we're already using a user-provided list, nothing to do. */
81 if (!mem_use_target ())
84 /* Switch to a user-provided list (possibly a copy of the current
86 mem_region_list
= &user_mem_region_list
;
88 /* If we don't have a target-provided region list yet, then
90 if (target_mem_region_list
.empty ())
93 /* Otherwise, let the user know how to get back. */
95 warning (_("Switching to manual control of memory regions; use "
96 "\"mem auto\" to fetch regions from the target again."));
98 /* And create a new list (copy of the target-supplied regions) for the user
100 user_mem_region_list
= target_mem_region_list
;
103 /* This function should be called before any command which would
104 read the memory region list, other than those which call
105 require_user_regions. It will handle fetching the
106 target-provided list, if necessary. */
109 require_target_regions (void)
111 if (mem_use_target () && !target_mem_regions_valid
)
113 target_mem_regions_valid
= true;
114 target_mem_region_list
= target_memory_map ();
118 /* Create a new user-defined memory region. */
121 create_user_mem_region (CORE_ADDR lo
, CORE_ADDR hi
,
122 const mem_attrib
&attrib
)
124 /* lo == hi is a useless empty region. */
125 if (lo
>= hi
&& hi
!= 0)
127 printf_unfiltered (_("invalid memory region: low >= high\n"));
131 mem_region
newobj (lo
, hi
, attrib
);
133 auto it
= std::lower_bound (user_mem_region_list
.begin (),
134 user_mem_region_list
.end (),
136 int ix
= std::distance (user_mem_region_list
.begin (), it
);
138 /* Check for an overlapping memory region. We only need to check
139 in the vicinity - at most one before and one after the
141 for (int i
= ix
- 1; i
< ix
+ 1; i
++)
145 if (i
>= user_mem_region_list
.size ())
148 mem_region
&n
= user_mem_region_list
[i
];
150 if ((lo
>= n
.lo
&& (lo
< n
.hi
|| n
.hi
== 0))
151 || (hi
> n
.lo
&& (hi
<= n
.hi
|| n
.hi
== 0))
152 || (lo
<= n
.lo
&& ((hi
>= n
.hi
&& n
.hi
!= 0) || hi
== 0)))
154 printf_unfiltered (_("overlapping memory region\n"));
159 newobj
.number
= ++mem_number
;
160 user_mem_region_list
.insert (it
, newobj
);
163 /* Look up the memory region corresponding to ADDR. */
166 lookup_mem_region (CORE_ADDR addr
)
168 static struct mem_region
region (0, 0);
169 struct mem_region
*m
;
174 require_target_regions ();
176 /* First we initialize LO and HI so that they describe the entire
177 memory space. As we process the memory region chain, they are
178 redefined to describe the minimal region containing ADDR. LO
179 and HI are used in the case where no memory region is defined
180 that contains ADDR. If a memory region is disabled, it is
181 treated as if it does not exist. The initial values for LO
182 and HI represent the bottom and top of memory. */
187 /* Either find memory range containing ADDR, or set LO and HI
188 to the nearest boundaries of an existing memory range.
190 If we ever want to support a huge list of memory regions, this
191 check should be replaced with a binary search (probably using
193 for (mem_region
&m
: *mem_region_list
)
195 if (m
.enabled_p
== 1)
197 /* If the address is in the memory region, return that
199 if (addr
>= m
.lo
&& (addr
< m
.hi
|| m
.hi
== 0))
202 /* This (correctly) won't match if m->hi == 0, representing
203 the top of the address space, because CORE_ADDR is unsigned;
204 no value of LO is less than zero. */
205 if (addr
>= m
.hi
&& lo
< m
.hi
)
208 /* This will never set HI to zero; if we're here and ADDR
209 is at or below M, and the region starts at zero, then ADDR
210 would have been in the region. */
211 if (addr
<= m
.lo
&& (hi
== 0 || hi
> m
.lo
))
216 /* Because no region was found, we must cons up one based on what
217 was learned above. */
221 /* When no memory map is defined at all, we always return
222 'default_mem_attrib', so that we do not make all memory
223 inaccessible for targets that don't provide a memory map. */
224 if (inaccessible_by_default
&& !mem_region_list
->empty ())
225 region
.attrib
= mem_attrib::unknown ();
227 region
.attrib
= mem_attrib ();
232 /* Invalidate any memory regions fetched from the target. */
235 invalidate_target_mem_regions (void)
237 if (!target_mem_regions_valid
)
240 target_mem_regions_valid
= false;
241 target_mem_region_list
.clear ();
244 /* Clear user-defined memory region list. */
247 user_mem_clear (void)
249 user_mem_region_list
.clear ();
254 mem_command (char *args
, int from_tty
)
260 error_no_arg (_("No mem"));
262 /* For "mem auto", switch back to using a target provided list. */
263 if (strcmp (args
, "auto") == 0)
265 if (mem_use_target ())
269 mem_region_list
= &target_mem_region_list
;
274 require_user_regions (from_tty
);
276 tok
= strtok (args
, " \t");
278 error (_("no lo address"));
279 lo
= parse_and_eval_address (tok
);
281 tok
= strtok (NULL
, " \t");
283 error (_("no hi address"));
284 hi
= parse_and_eval_address (tok
);
287 while ((tok
= strtok (NULL
, " \t")) != NULL
)
289 if (strcmp (tok
, "rw") == 0)
290 attrib
.mode
= MEM_RW
;
291 else if (strcmp (tok
, "ro") == 0)
292 attrib
.mode
= MEM_RO
;
293 else if (strcmp (tok
, "wo") == 0)
294 attrib
.mode
= MEM_WO
;
296 else if (strcmp (tok
, "8") == 0)
297 attrib
.width
= MEM_WIDTH_8
;
298 else if (strcmp (tok
, "16") == 0)
300 if ((lo
% 2 != 0) || (hi
% 2 != 0))
301 error (_("region bounds not 16 bit aligned"));
302 attrib
.width
= MEM_WIDTH_16
;
304 else if (strcmp (tok
, "32") == 0)
306 if ((lo
% 4 != 0) || (hi
% 4 != 0))
307 error (_("region bounds not 32 bit aligned"));
308 attrib
.width
= MEM_WIDTH_32
;
310 else if (strcmp (tok
, "64") == 0)
312 if ((lo
% 8 != 0) || (hi
% 8 != 0))
313 error (_("region bounds not 64 bit aligned"));
314 attrib
.width
= MEM_WIDTH_64
;
318 else if (strcmp (tok
, "hwbreak") == 0)
320 else if (strcmp (tok
, "swbreak") == 0)
324 else if (strcmp (tok
, "cache") == 0)
326 else if (strcmp (tok
, "nocache") == 0)
330 else if (strcmp (tok
, "verify") == 0)
332 else if (strcmp (tok
, "noverify") == 0)
337 error (_("unknown attribute: %s"), tok
);
340 create_user_mem_region (lo
, hi
, attrib
);
345 info_mem_command (char *args
, int from_tty
)
347 if (mem_use_target ())
348 printf_filtered (_("Using memory regions provided by the target.\n"));
350 printf_filtered (_("Using user-defined memory regions.\n"));
352 require_target_regions ();
354 if (mem_region_list
->empty ())
356 printf_unfiltered (_("There are no memory regions defined.\n"));
360 printf_filtered ("Num ");
361 printf_filtered ("Enb ");
362 printf_filtered ("Low Addr ");
363 if (gdbarch_addr_bit (target_gdbarch ()) > 32)
364 printf_filtered (" ");
365 printf_filtered ("High Addr ");
366 if (gdbarch_addr_bit (target_gdbarch ()) > 32)
367 printf_filtered (" ");
368 printf_filtered ("Attrs ");
369 printf_filtered ("\n");
371 for (const mem_region
&m
: *mem_region_list
)
375 printf_filtered ("%-3d %-3c\t",
377 m
.enabled_p
? 'y' : 'n');
378 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
379 tmp
= hex_string_custom (m
.lo
, 8);
381 tmp
= hex_string_custom (m
.lo
, 16);
383 printf_filtered ("%s ", tmp
);
385 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
390 tmp
= hex_string_custom (m
.hi
, 8);
395 tmp
= "0x10000000000000000";
397 tmp
= hex_string_custom (m
.hi
, 16);
400 printf_filtered ("%s ", tmp
);
402 /* Print a token for each attribute.
404 * FIXME: Should we output a comma after each token? It may
405 * make it easier for users to read, but we'd lose the ability
406 * to cut-and-paste the list of attributes when defining a new
407 * region. Perhaps that is not important.
409 * FIXME: If more attributes are added to GDB, the output may
410 * become cluttered and difficult for users to read. At that
411 * time, we may want to consider printing tokens only if they
412 * are different from the default attribute. */
414 switch (m
.attrib
.mode
)
417 printf_filtered ("rw ");
420 printf_filtered ("ro ");
423 printf_filtered ("wo ");
426 printf_filtered ("flash blocksize 0x%x ", m
.attrib
.blocksize
);
430 switch (m
.attrib
.width
)
433 printf_filtered ("8 ");
436 printf_filtered ("16 ");
439 printf_filtered ("32 ");
442 printf_filtered ("64 ");
444 case MEM_WIDTH_UNSPECIFIED
:
450 printf_filtered ("hwbreak");
452 printf_filtered ("swbreak");
456 printf_filtered ("cache ");
458 printf_filtered ("nocache ");
462 printf_filtered ("verify ");
464 printf_filtered ("noverify ");
467 printf_filtered ("\n");
469 gdb_flush (gdb_stdout
);
474 /* Enable the memory region number NUM. */
479 for (mem_region
&m
: *mem_region_list
)
485 printf_unfiltered (_("No memory region number %d.\n"), num
);
489 enable_mem_command (const char *args
, int from_tty
)
491 require_user_regions (from_tty
);
493 target_dcache_invalidate ();
495 if (args
== NULL
|| *args
== '\0')
496 { /* Enable all mem regions. */
497 for (mem_region
&m
: *mem_region_list
)
502 number_or_range_parser
parser (args
);
503 while (!parser
.finished ())
505 int num
= parser
.get_number ();
512 /* Disable the memory region number NUM. */
515 mem_disable (int num
)
517 for (mem_region
&m
: *mem_region_list
)
523 printf_unfiltered (_("No memory region number %d.\n"), num
);
527 disable_mem_command (const char *args
, int from_tty
)
529 require_user_regions (from_tty
);
531 target_dcache_invalidate ();
533 if (args
== NULL
|| *args
== '\0')
535 struct mem_region
*m
;
538 for (mem_region
&m
: *mem_region_list
)
543 number_or_range_parser
parser (args
);
544 while (!parser
.finished ())
546 int num
= parser
.get_number ();
552 /* Delete the memory region number NUM. */
557 struct mem_region
*m
;
560 if (!mem_region_list
)
562 printf_unfiltered (_("No memory region number %d.\n"), num
);
566 auto it
= std::remove_if (mem_region_list
->begin (), mem_region_list
->end (),
567 [num
] (const mem_region
&m
)
569 return m
.number
== num
;
572 if (it
!= mem_region_list
->end ())
573 mem_region_list
->erase (it
);
575 printf_unfiltered (_("No memory region number %d.\n"), num
);
579 delete_mem_command (const char *args
, int from_tty
)
581 require_user_regions (from_tty
);
583 target_dcache_invalidate ();
585 if (args
== NULL
|| *args
== '\0')
587 if (query (_("Delete all memory regions? ")))
593 number_or_range_parser
parser (args
);
594 while (!parser
.finished ())
596 int num
= parser
.get_number ();
604 dummy_cmd (const char *args
, int from_tty
)
608 static struct cmd_list_element
*mem_set_cmdlist
;
609 static struct cmd_list_element
*mem_show_cmdlist
;
612 _initialize_mem (void)
614 add_com ("mem", class_vars
, mem_command
, _("\
615 Define attributes for memory region or reset memory region handling to\n\
618 mem <lo addr> <hi addr> [<mode> <width> <cache>],\n\
619 where <mode> may be rw (read/write), ro (read-only) or wo (write-only),\n\
620 <width> may be 8, 16, 32, or 64, and\n\
621 <cache> may be cache or nocache"));
623 add_cmd ("mem", class_vars
, enable_mem_command
, _("\
624 Enable memory region.\n\
625 Arguments are the code numbers of the memory regions to enable.\n\
626 Usage: enable mem <code number>...\n\
627 Do \"info mem\" to see current list of code numbers."), &enablelist
);
629 add_cmd ("mem", class_vars
, disable_mem_command
, _("\
630 Disable memory region.\n\
631 Arguments are the code numbers of the memory regions to disable.\n\
632 Usage: disable mem <code number>...\n\
633 Do \"info mem\" to see current list of code numbers."), &disablelist
);
635 add_cmd ("mem", class_vars
, delete_mem_command
, _("\
636 Delete memory region.\n\
637 Arguments are the code numbers of the memory regions to delete.\n\
638 Usage: delete mem <code number>...\n\
639 Do \"info mem\" to see current list of code numbers."), &deletelist
);
641 add_info ("mem", info_mem_command
,
642 _("Memory region attributes"));
644 add_prefix_cmd ("mem", class_vars
, dummy_cmd
, _("\
645 Memory regions settings"),
646 &mem_set_cmdlist
, "set mem ",
647 0/* allow-unknown */, &setlist
);
648 add_prefix_cmd ("mem", class_vars
, dummy_cmd
, _("\
649 Memory regions settings"),
650 &mem_show_cmdlist
, "show mem ",
651 0/* allow-unknown */, &showlist
);
653 add_setshow_boolean_cmd ("inaccessible-by-default", no_class
,
654 &inaccessible_by_default
, _("\
655 Set handling of unknown memory regions."), _("\
656 Show handling of unknown memory regions."), _("\
657 If on, and some memory map is defined, debugger will emit errors on\n\
658 accesses to memory not defined in the memory map. If off, accesses to all\n\
659 memory addresses will be allowed."),
661 show_inaccessible_by_default
,