testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / memattr.c
blob735068e5a468e82965b81179784b4b36802f82e4
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/>. */
20 #include "command.h"
21 #include "cli/cli-cmds.h"
22 #include "memattr.h"
23 #include "target.h"
24 #include "target-dcache.h"
25 #include "value.h"
26 #include "language.h"
27 #include "breakpoint.h"
28 #include "cli/cli-utils.h"
29 #include <algorithm>
30 #include "gdbarch.h"
31 #include "inferior.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. */
42 static bool
43 mem_use_target ()
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;
58 static void
59 show_inaccessible_by_default (struct ui_file *file, int from_tty,
60 struct cmd_list_element *c,
61 const char *value)
63 if (inaccessible_by_default)
64 gdb_printf (file, _("Unknown memory addresses will "
65 "be treated as inaccessible.\n"));
66 else
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. */
75 static void
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 ())
80 return;
82 /* Switch to a user-provided list (possibly a copy of the current
83 one). */
84 mem_region_list = &user_mem_region_list;
86 /* If we don't have a target-provided region list yet, then
87 no need to warn. */
88 if (target_mem_region_list.empty ())
89 return;
91 /* Otherwise, let the user know how to get back. */
92 if (from_tty)
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
97 to modify. */
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. */
106 static void
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. */
118 static void
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"));
126 return;
129 mem_region newobj (lo, hi, attrib);
131 auto it = std::lower_bound (user_mem_region_list.begin (),
132 user_mem_region_list.end (),
133 newobj);
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
138 insertion point. */
139 for (int i = ix - 1; i < ix + 1; i++)
141 if (i < 0)
142 continue;
143 if (i >= user_mem_region_list.size ())
144 continue;
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"));
153 return;
157 newobj.number = ++mem_number;
158 user_mem_region_list.insert (it, newobj);
161 /* Look up the memory region corresponding to ADDR. */
163 struct mem_region *
164 lookup_mem_region (CORE_ADDR addr)
166 static struct mem_region region (0, 0);
167 CORE_ADDR lo;
168 CORE_ADDR hi;
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. */
180 lo = 0;
181 hi = 0;
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
188 VEC_lower_bound). */
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
194 memory range. */
195 if (addr >= m.lo && (addr < m.hi || m.hi == 0))
196 return &m;
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)
202 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))
208 hi = m.lo;
212 /* Because no region was found, we must cons up one based on what
213 was learned above. */
214 region.lo = lo;
215 region.hi = hi;
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 ();
222 else
223 region.attrib = mem_attrib ();
225 return &region;
228 /* Invalidate any memory regions fetched from the target. */
230 void
231 invalidate_target_mem_regions (void)
233 if (!target_mem_regions_valid)
234 return;
236 target_mem_regions_valid = false;
237 target_mem_region_list.clear ();
240 /* Clear user-defined memory region list. */
242 static void
243 user_mem_clear (void)
245 user_mem_region_list.clear ();
249 static void
250 mem_command (const char *args, int from_tty)
252 CORE_ADDR lo, hi;
254 if (!args)
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 ())
261 return;
263 user_mem_clear ();
264 mem_region_list = &target_mem_region_list;
266 return;
269 require_user_regions (from_tty);
271 std::string tok = extract_arg (&args);
272 if (tok == "")
273 error (_("no lo address"));
274 lo = parse_and_eval_address (tok.c_str ());
276 tok = extract_arg (&args);
277 if (tok == "")
278 error (_("no hi address"));
279 hi = parse_and_eval_address (tok.c_str ());
281 mem_attrib attrib;
282 while ((tok = extract_arg (&args)) != "")
284 if (tok == "rw")
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;
291 else if (tok == "8")
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;
312 #if 0
313 else if (tok == "hwbreak")
314 attrib.hwbreak = 1;
315 else if (tok == "swbreak")
316 attrib.hwbreak = 0;
317 #endif
319 else if (tok == "cache")
320 attrib.cache = 1;
321 else if (tok == "nocache")
322 attrib.cache = 0;
324 #if 0
325 else if (tok == "verify")
326 attrib.verify = 1;
327 else if (tok == "noverify")
328 attrib.verify = 0;
329 #endif
331 else
332 error (_("unknown attribute: %s"), tok.c_str ());
335 create_user_mem_region (lo, hi, attrib);
339 static void
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"));
344 else
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"));
352 return;
355 gdb_printf ("Num ");
356 gdb_printf ("Enb ");
357 gdb_printf ("Low Addr ");
358 if (gdbarch_addr_bit (current_inferior ()->arch ()) > 32)
359 gdb_printf (" ");
360 gdb_printf ("High Addr ");
361 if (gdbarch_addr_bit (current_inferior ()->arch ()) > 32)
362 gdb_printf (" ");
363 gdb_printf ("Attrs ");
364 gdb_printf ("\n");
366 for (const mem_region &m : *mem_region_list)
368 const char *tmp;
370 gdb_printf ("%-3d %-3c\t",
371 m.number,
372 m.enabled_p ? 'y' : 'n');
373 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
374 tmp = hex_string_custom (m.lo, 8);
375 else
376 tmp = hex_string_custom (m.lo, 16);
378 gdb_printf ("%s ", tmp);
380 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
382 if (m.hi == 0)
383 tmp = "0x100000000";
384 else
385 tmp = hex_string_custom (m.hi, 8);
387 else
389 if (m.hi == 0)
390 tmp = "0x10000000000000000";
391 else
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)
411 case MEM_RW:
412 gdb_printf ("rw ");
413 break;
414 case MEM_RO:
415 gdb_printf ("ro ");
416 break;
417 case MEM_WO:
418 gdb_printf ("wo ");
419 break;
420 case MEM_FLASH:
421 gdb_printf ("flash blocksize 0x%x ", m.attrib.blocksize);
422 break;
425 switch (m.attrib.width)
427 case MEM_WIDTH_8:
428 gdb_printf ("8 ");
429 break;
430 case MEM_WIDTH_16:
431 gdb_printf ("16 ");
432 break;
433 case MEM_WIDTH_32:
434 gdb_printf ("32 ");
435 break;
436 case MEM_WIDTH_64:
437 gdb_printf ("64 ");
438 break;
439 case MEM_WIDTH_UNSPECIFIED:
440 break;
443 #if 0
444 if (attrib->hwbreak)
445 gdb_printf ("hwbreak");
446 else
447 gdb_printf ("swbreak");
448 #endif
450 if (m.attrib.cache)
451 gdb_printf ("cache ");
452 else
453 gdb_printf ("nocache ");
455 #if 0
456 if (attrib->verify)
457 gdb_printf ("verify ");
458 else
459 gdb_printf ("noverify ");
460 #endif
462 gdb_printf ("\n");
467 /* Enable the memory region number NUM. */
469 static void
470 mem_enable (int num)
472 for (mem_region &m : *mem_region_list)
473 if (m.number == num)
475 m.enabled_p = 1;
476 return;
478 gdb_printf (_("No memory region number %d.\n"), num);
481 static void
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)
491 m.enabled_p = 1;
493 else
495 number_or_range_parser parser (args);
496 while (!parser.finished ())
498 int num = parser.get_number ();
499 mem_enable (num);
505 /* Disable the memory region number NUM. */
507 static void
508 mem_disable (int num)
510 for (mem_region &m : *mem_region_list)
511 if (m.number == num)
513 m.enabled_p = 0;
514 return;
516 gdb_printf (_("No memory region number %d.\n"), num);
519 static void
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)
529 m.enabled_p = false;
531 else
533 number_or_range_parser parser (args);
534 while (!parser.finished ())
536 int num = parser.get_number ();
537 mem_disable (num);
542 /* Delete the memory region number NUM. */
544 static void
545 mem_delete (int num)
547 if (!mem_region_list)
549 gdb_printf (_("No memory region number %d.\n"), num);
550 return;
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);
561 else
562 gdb_printf (_("No memory region number %d.\n"), num);
565 static void
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? ")))
575 user_mem_clear ();
576 dont_repeat ();
577 return;
580 number_or_range_parser parser (args);
581 while (!parser.finished ())
583 int num = parser.get_number ();
584 mem_delete (num);
587 dont_repeat ();
590 static struct cmd_list_element *mem_set_cmdlist;
591 static struct cmd_list_element *mem_show_cmdlist;
593 void _initialize_mem ();
594 void
595 _initialize_mem ()
597 add_com ("mem", class_vars, mem_command, _("\
598 Define attributes for memory region or reset memory region handling to "
599 "target-based.\n\
600 Usage: mem auto\n\
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."),
640 NULL,
641 show_inaccessible_by_default,
642 &mem_set_cmdlist,
643 &mem_show_cmdlist);