1 /* Memory attributes support, for GDB.
3 Copyright (C) 2001-2024 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/>. */
21 #include "cli/cli-cmds.h"
24 #include "target-dcache.h"
27 #include "breakpoint.h"
28 #include "cli/cli-utils.h"
32 #include "progspace.h"
34 static std::vector
<mem_region
> user_mem_region_list
, target_mem_region_list
;
35 static std::vector
<mem_region
> *mem_region_list
= &target_mem_region_list
;
36 static int mem_number
= 0;
38 /* If this flag is set, the memory region list should be automatically
39 updated from the target. If it is clear, the list is user-controlled
40 and should be left alone. */
45 return mem_region_list
== &target_mem_region_list
;
48 /* If this flag is set, we have tried to fetch the target memory regions
49 since the last time it was invalidated. If that list is still
50 empty, then the target can't supply memory regions. */
51 static bool target_mem_regions_valid
;
53 /* If this flag is set, gdb will assume that memory ranges not
54 specified by the memory map have type MEM_NONE, and will
55 emit errors on all accesses to that memory. */
56 static bool inaccessible_by_default
= true;
59 show_inaccessible_by_default (struct ui_file
*file
, int from_tty
,
60 struct cmd_list_element
*c
,
63 if (inaccessible_by_default
)
64 gdb_printf (file
, _("Unknown memory addresses will "
65 "be treated as inaccessible.\n"));
67 gdb_printf (file
, _("Unknown memory addresses "
68 "will be treated as RAM.\n"));
71 /* This function should be called before any command which would
72 modify the memory region list. It will handle switching from
73 a target-provided list to a local list, if necessary. */
76 require_user_regions (int from_tty
)
78 /* If we're already using a user-provided list, nothing to do. */
79 if (!mem_use_target ())
82 /* Switch to a user-provided list (possibly a copy of the current
84 mem_region_list
= &user_mem_region_list
;
86 /* If we don't have a target-provided region list yet, then
88 if (target_mem_region_list
.empty ())
91 /* Otherwise, let the user know how to get back. */
93 warning (_("Switching to manual control of memory regions; use "
94 "\"mem auto\" to fetch regions from the target again."));
96 /* And create a new list (copy of the target-supplied regions) for the user
98 user_mem_region_list
= target_mem_region_list
;
101 /* This function should be called before any command which would
102 read the memory region list, other than those which call
103 require_user_regions. It will handle fetching the
104 target-provided list, if necessary. */
107 require_target_regions (void)
109 if (mem_use_target () && !target_mem_regions_valid
)
111 target_mem_regions_valid
= true;
112 target_mem_region_list
= target_memory_map ();
116 /* Create a new user-defined memory region. */
119 create_user_mem_region (CORE_ADDR lo
, CORE_ADDR hi
,
120 const mem_attrib
&attrib
)
122 /* lo == hi is a useless empty region. */
123 if (lo
>= hi
&& hi
!= 0)
125 gdb_printf (_("invalid memory region: low >= high\n"));
129 mem_region
newobj (lo
, hi
, attrib
);
131 auto it
= std::lower_bound (user_mem_region_list
.begin (),
132 user_mem_region_list
.end (),
134 int ix
= std::distance (user_mem_region_list
.begin (), it
);
136 /* Check for an overlapping memory region. We only need to check
137 in the vincinity - at most one before and one after the
139 for (int i
= ix
- 1; i
< ix
+ 1; i
++)
143 if (i
>= user_mem_region_list
.size ())
146 mem_region
&n
= user_mem_region_list
[i
];
148 if ((lo
>= n
.lo
&& (lo
< n
.hi
|| n
.hi
== 0))
149 || (hi
> n
.lo
&& (hi
<= n
.hi
|| n
.hi
== 0))
150 || (lo
<= n
.lo
&& ((hi
>= n
.hi
&& n
.hi
!= 0) || hi
== 0)))
152 gdb_printf (_("overlapping memory region\n"));
157 newobj
.number
= ++mem_number
;
158 user_mem_region_list
.insert (it
, newobj
);
161 /* Look up the memory region corresponding to ADDR. */
164 lookup_mem_region (CORE_ADDR addr
)
166 static struct mem_region
region (0, 0);
170 require_target_regions ();
172 /* First we initialize LO and HI so that they describe the entire
173 memory space. As we process the memory region chain, they are
174 redefined to describe the minimal region containing ADDR. LO
175 and HI are used in the case where no memory region is defined
176 that contains ADDR. If a memory region is disabled, it is
177 treated as if it does not exist. The initial values for LO
178 and HI represent the bottom and top of memory. */
183 /* Either find memory range containing ADDR, or set LO and HI
184 to the nearest boundaries of an existing memory range.
186 If we ever want to support a huge list of memory regions, this
187 check should be replaced with a binary search (probably using
189 for (mem_region
&m
: *mem_region_list
)
191 if (m
.enabled_p
== 1)
193 /* If the address is in the memory region, return that
195 if (addr
>= m
.lo
&& (addr
< m
.hi
|| m
.hi
== 0))
198 /* This (correctly) won't match if m->hi == 0, representing
199 the top of the address space, because CORE_ADDR is unsigned;
200 no value of LO is less than zero. */
201 if (addr
>= m
.hi
&& lo
< m
.hi
)
204 /* This will never set HI to zero; if we're here and ADDR
205 is at or below M, and the region starts at zero, then ADDR
206 would have been in the region. */
207 if (addr
<= m
.lo
&& (hi
== 0 || hi
> m
.lo
))
212 /* Because no region was found, we must cons up one based on what
213 was learned above. */
217 /* When no memory map is defined at all, we always return
218 'default_mem_attrib', so that we do not make all memory
219 inaccessible for targets that don't provide a memory map. */
220 if (inaccessible_by_default
&& !mem_region_list
->empty ())
221 region
.attrib
= mem_attrib::unknown ();
223 region
.attrib
= mem_attrib ();
228 /* Invalidate any memory regions fetched from the target. */
231 invalidate_target_mem_regions (void)
233 if (!target_mem_regions_valid
)
236 target_mem_regions_valid
= false;
237 target_mem_region_list
.clear ();
240 /* Clear user-defined memory region list. */
243 user_mem_clear (void)
245 user_mem_region_list
.clear ();
250 mem_command (const char *args
, int from_tty
)
255 error_no_arg (_("No mem"));
257 /* For "mem auto", switch back to using a target provided list. */
258 if (strcmp (args
, "auto") == 0)
260 if (mem_use_target ())
264 mem_region_list
= &target_mem_region_list
;
269 require_user_regions (from_tty
);
271 std::string tok
= extract_arg (&args
);
273 error (_("no lo address"));
274 lo
= parse_and_eval_address (tok
.c_str ());
276 tok
= extract_arg (&args
);
278 error (_("no hi address"));
279 hi
= parse_and_eval_address (tok
.c_str ());
282 while ((tok
= extract_arg (&args
)) != "")
285 attrib
.mode
= MEM_RW
;
286 else if (tok
== "ro")
287 attrib
.mode
= MEM_RO
;
288 else if (tok
== "wo")
289 attrib
.mode
= MEM_WO
;
292 attrib
.width
= MEM_WIDTH_8
;
293 else if (tok
== "16")
295 if ((lo
% 2 != 0) || (hi
% 2 != 0))
296 error (_("region bounds not 16 bit aligned"));
297 attrib
.width
= MEM_WIDTH_16
;
299 else if (tok
== "32")
301 if ((lo
% 4 != 0) || (hi
% 4 != 0))
302 error (_("region bounds not 32 bit aligned"));
303 attrib
.width
= MEM_WIDTH_32
;
305 else if (tok
== "64")
307 if ((lo
% 8 != 0) || (hi
% 8 != 0))
308 error (_("region bounds not 64 bit aligned"));
309 attrib
.width
= MEM_WIDTH_64
;
313 else if (tok
== "hwbreak")
315 else if (tok
== "swbreak")
319 else if (tok
== "cache")
321 else if (tok
== "nocache")
325 else if (tok
== "verify")
327 else if (tok
== "noverify")
332 error (_("unknown attribute: %s"), tok
.c_str ());
335 create_user_mem_region (lo
, hi
, attrib
);
340 info_mem_command (const char *args
, int from_tty
)
342 if (mem_use_target ())
343 gdb_printf (_("Using memory regions provided by the target.\n"));
345 gdb_printf (_("Using user-defined memory regions.\n"));
347 require_target_regions ();
349 if (mem_region_list
->empty ())
351 gdb_printf (_("There are no memory regions defined.\n"));
357 gdb_printf ("Low Addr ");
358 if (gdbarch_addr_bit (current_inferior ()->arch ()) > 32)
360 gdb_printf ("High Addr ");
361 if (gdbarch_addr_bit (current_inferior ()->arch ()) > 32)
363 gdb_printf ("Attrs ");
366 for (const mem_region
&m
: *mem_region_list
)
370 gdb_printf ("%-3d %-3c\t",
372 m
.enabled_p
? 'y' : 'n');
373 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
374 tmp
= hex_string_custom (m
.lo
, 8);
376 tmp
= hex_string_custom (m
.lo
, 16);
378 gdb_printf ("%s ", tmp
);
380 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
385 tmp
= hex_string_custom (m
.hi
, 8);
390 tmp
= "0x10000000000000000";
392 tmp
= hex_string_custom (m
.hi
, 16);
395 gdb_printf ("%s ", tmp
);
397 /* Print a token for each attribute.
399 * FIXME: Should we output a comma after each token? It may
400 * make it easier for users to read, but we'd lose the ability
401 * to cut-and-paste the list of attributes when defining a new
402 * region. Perhaps that is not important.
404 * FIXME: If more attributes are added to GDB, the output may
405 * become cluttered and difficult for users to read. At that
406 * time, we may want to consider printing tokens only if they
407 * are different from the default attribute. */
409 switch (m
.attrib
.mode
)
421 gdb_printf ("flash blocksize 0x%x ", m
.attrib
.blocksize
);
425 switch (m
.attrib
.width
)
439 case MEM_WIDTH_UNSPECIFIED
:
445 gdb_printf ("hwbreak");
447 gdb_printf ("swbreak");
451 gdb_printf ("cache ");
453 gdb_printf ("nocache ");
457 gdb_printf ("verify ");
459 gdb_printf ("noverify ");
467 /* Enable the memory region number NUM. */
472 for (mem_region
&m
: *mem_region_list
)
478 gdb_printf (_("No memory region number %d.\n"), num
);
482 enable_mem_command (const char *args
, int from_tty
)
484 require_user_regions (from_tty
);
486 target_dcache_invalidate (current_program_space
->aspace
);
488 if (args
== NULL
|| *args
== '\0')
489 { /* Enable all mem regions. */
490 for (mem_region
&m
: *mem_region_list
)
495 number_or_range_parser
parser (args
);
496 while (!parser
.finished ())
498 int num
= parser
.get_number ();
505 /* Disable the memory region number NUM. */
508 mem_disable (int num
)
510 for (mem_region
&m
: *mem_region_list
)
516 gdb_printf (_("No memory region number %d.\n"), num
);
520 disable_mem_command (const char *args
, int from_tty
)
522 require_user_regions (from_tty
);
524 target_dcache_invalidate (current_program_space
->aspace
);
526 if (args
== NULL
|| *args
== '\0')
528 for (mem_region
&m
: *mem_region_list
)
533 number_or_range_parser
parser (args
);
534 while (!parser
.finished ())
536 int num
= parser
.get_number ();
542 /* Delete the memory region number NUM. */
547 if (!mem_region_list
)
549 gdb_printf (_("No memory region number %d.\n"), num
);
553 auto it
= std::remove_if (mem_region_list
->begin (), mem_region_list
->end (),
554 [num
] (const mem_region
&m
)
556 return m
.number
== num
;
559 if (it
!= mem_region_list
->end ())
560 mem_region_list
->erase (it
);
562 gdb_printf (_("No memory region number %d.\n"), num
);
566 delete_mem_command (const char *args
, int from_tty
)
568 require_user_regions (from_tty
);
570 target_dcache_invalidate (current_program_space
->aspace
);
572 if (args
== NULL
|| *args
== '\0')
574 if (query (_("Delete all memory regions? ")))
580 number_or_range_parser
parser (args
);
581 while (!parser
.finished ())
583 int num
= parser
.get_number ();
590 static struct cmd_list_element
*mem_set_cmdlist
;
591 static struct cmd_list_element
*mem_show_cmdlist
;
593 void _initialize_mem ();
597 add_com ("mem", class_vars
, mem_command
, _("\
598 Define attributes for memory region or reset memory region handling to "
601 mem LOW HIGH [MODE WIDTH CACHE],\n\
602 where MODE may be rw (read/write), ro (read-only) or wo (write-only),\n\
603 WIDTH may be 8, 16, 32, or 64, and\n\
604 CACHE may be cache or nocache"));
606 add_cmd ("mem", class_vars
, enable_mem_command
, _("\
607 Enable memory region.\n\
608 Arguments are the IDs of the memory regions to enable.\n\
609 Usage: enable mem [ID]...\n\
610 Do \"info mem\" to see current list of IDs."), &enablelist
);
612 add_cmd ("mem", class_vars
, disable_mem_command
, _("\
613 Disable memory region.\n\
614 Arguments are the IDs of the memory regions to disable.\n\
615 Usage: disable mem [ID]...\n\
616 Do \"info mem\" to see current list of IDs."), &disablelist
);
618 add_cmd ("mem", class_vars
, delete_mem_command
, _("\
619 Delete memory region.\n\
620 Arguments are the IDs of the memory regions to delete.\n\
621 Usage: delete mem [ID]...\n\
622 Do \"info mem\" to see current list of IDs."), &deletelist
);
624 add_info ("mem", info_mem_command
,
625 _("Memory region attributes."));
627 add_setshow_prefix_cmd ("mem", class_vars
,
628 _("Memory regions settings."),
629 _("Memory regions settings."),
630 &mem_set_cmdlist
, &mem_show_cmdlist
,
631 &setlist
, &showlist
);
633 add_setshow_boolean_cmd ("inaccessible-by-default", no_class
,
634 &inaccessible_by_default
, _("\
635 Set handling of unknown memory regions."), _("\
636 Show handling of unknown memory regions."), _("\
637 If on, and some memory map is defined, debugger will emit errors on\n\
638 accesses to memory not defined in the memory map. If off, accesses to all\n\
639 memory addresses will be allowed."),
641 show_inaccessible_by_default
,