2010-05-06 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / memattr.c
1 /* Memory attributes support, for GDB.
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #include "defs.h"
22 #include "command.h"
23 #include "gdbcmd.h"
24 #include "memattr.h"
25 #include "target.h"
26 #include "value.h"
27 #include "language.h"
28 #include "vec.h"
29 #include "gdb_string.h"
30
31 const struct mem_attrib default_mem_attrib =
32 {
33 MEM_RW, /* mode */
34 MEM_WIDTH_UNSPECIFIED,
35 0, /* hwbreak */
36 0, /* cache */
37 0, /* verify */
38 -1 /* Flash blocksize not specified. */
39 };
40
41 const struct mem_attrib unknown_mem_attrib =
42 {
43 MEM_NONE, /* mode */
44 MEM_WIDTH_UNSPECIFIED,
45 0, /* hwbreak */
46 0, /* cache */
47 0, /* verify */
48 -1 /* Flash blocksize not specified. */
49 };
50
51
52 VEC(mem_region_s) *mem_region_list, *target_mem_region_list;
53 static int mem_number = 0;
54
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;
59
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;
64
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;
69
70 static void
71 show_inaccessible_by_default (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c,
73 const char *value)
74 {
75 if (inaccessible_by_default)
76 fprintf_filtered (file, _("\
77 Unknown memory addresses will be treated as inaccessible.\n"));
78 else
79 fprintf_filtered (file, _("\
80 Unknown memory addresses will be treated as RAM.\n"));
81 }
82
83
84 /* Predicate function which returns true if LHS should sort before RHS
85 in a list of memory regions, useful for VEC_lower_bound. */
86
87 static int
88 mem_region_lessthan (const struct mem_region *lhs,
89 const struct mem_region *rhs)
90 {
91 return lhs->lo < rhs->lo;
92 }
93
94 /* A helper function suitable for qsort, used to sort a
95 VEC(mem_region_s) by starting address. */
96
97 int
98 mem_region_cmp (const void *untyped_lhs, const void *untyped_rhs)
99 {
100 const struct mem_region *lhs = untyped_lhs;
101 const struct mem_region *rhs = untyped_rhs;
102
103 if (lhs->lo < rhs->lo)
104 return -1;
105 else if (lhs->lo == rhs->lo)
106 return 0;
107 else
108 return 1;
109 }
110
111 /* Allocate a new memory region, with default settings. */
112
113 void
114 mem_region_init (struct mem_region *new)
115 {
116 memset (new, 0, sizeof (struct mem_region));
117 new->enabled_p = 1;
118 new->attrib = default_mem_attrib;
119 }
120
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. */
124
125 static void
126 require_user_regions (int from_tty)
127 {
128 struct mem_region *m;
129 int ix, length;
130
131 /* If we're already using a user-provided list, nothing to do. */
132 if (!mem_use_target)
133 return;
134
135 /* Switch to a user-provided list (possibly a copy of the current
136 one). */
137 mem_use_target = 0;
138
139 /* If we don't have a target-provided region list yet, then
140 no need to warn. */
141 if (mem_region_list == NULL)
142 return;
143
144 /* Otherwise, let the user know how to get back. */
145 if (from_tty)
146 warning (_("Switching to manual control of memory regions; use "
147 "\"mem auto\" to fetch regions from the target again."));
148
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);
154 }
155
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. */
160
161 static void
162 require_target_regions (void)
163 {
164 if (mem_use_target && !target_mem_regions_valid)
165 {
166 target_mem_regions_valid = 1;
167 target_mem_region_list = target_memory_map ();
168 mem_region_list = target_mem_region_list;
169 }
170 }
171
172 static void
173 create_mem_region (CORE_ADDR lo, CORE_ADDR hi,
174 const struct mem_attrib *attrib)
175 {
176 struct mem_region new;
177 int i, ix;
178
179 /* lo == hi is a useless empty region */
180 if (lo >= hi && hi != 0)
181 {
182 printf_unfiltered (_("invalid memory region: low >= high\n"));
183 return;
184 }
185
186 mem_region_init (&new);
187 new.lo = lo;
188 new.hi = hi;
189
190 ix = VEC_lower_bound (mem_region_s, mem_region_list, &new,
191 mem_region_lessthan);
192
193 /* Check for an overlapping memory region. We only need to check
194 in the vicinity - at most one before and one after the
195 insertion point. */
196 for (i = ix - 1; i < ix + 1; i++)
197 {
198 struct mem_region *n;
199
200 if (i < 0)
201 continue;
202 if (i >= VEC_length (mem_region_s, mem_region_list))
203 continue;
204
205 n = VEC_index (mem_region_s, mem_region_list, i);
206
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)))
210 {
211 printf_unfiltered (_("overlapping memory region\n"));
212 return;
213 }
214 }
215
216 new.number = ++mem_number;
217 new.attrib = *attrib;
218 VEC_safe_insert (mem_region_s, mem_region_list, ix, &new);
219 }
220
221 /*
222 * Look up the memory region cooresponding to ADDR.
223 */
224 struct mem_region *
225 lookup_mem_region (CORE_ADDR addr)
226 {
227 static struct mem_region region;
228 struct mem_region *m;
229 CORE_ADDR lo;
230 CORE_ADDR hi;
231 int ix;
232
233 require_target_regions ();
234
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. */
242
243 lo = 0;
244 hi = 0;
245
246 /* Either find memory range containing ADDRESS, or set LO and HI
247 to the nearest boundaries of an existing memory range.
248
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
251 VEC_lower_bound). */
252 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
253 {
254 if (m->enabled_p == 1)
255 {
256 /* If the address is in the memory region, return that memory range. */
257 if (addr >= m->lo && (addr < m->hi || m->hi == 0))
258 return m;
259
260 /* This (correctly) won't match if m->hi == 0, representing
261 the top of the address space, because CORE_ADDR is unsigned;
262 no value of LO is less than zero. */
263 if (addr >= m->hi && lo < m->hi)
264 lo = m->hi;
265
266 /* This will never set HI to zero; if we're here and ADDR
267 is at or below M, and the region starts at zero, then ADDR
268 would have been in the region. */
269 if (addr <= m->lo && (hi == 0 || hi > m->lo))
270 hi = m->lo;
271 }
272 }
273
274 /* Because no region was found, we must cons up one based on what
275 was learned above. */
276 region.lo = lo;
277 region.hi = hi;
278
279 /* When no memory map is defined at all, we always return
280 'default_mem_attrib', so that we do not make all memory
281 inaccessible for targets that don't provide a memory map. */
282 if (inaccessible_by_default && !VEC_empty (mem_region_s, mem_region_list))
283 region.attrib = unknown_mem_attrib;
284 else
285 region.attrib = default_mem_attrib;
286
287 return &region;
288 }
289
290 /* Invalidate any memory regions fetched from the target. */
291
292 void
293 invalidate_target_mem_regions (void)
294 {
295 if (!target_mem_regions_valid)
296 return;
297
298 target_mem_regions_valid = 0;
299 VEC_free (mem_region_s, target_mem_region_list);
300 if (mem_use_target)
301 mem_region_list = NULL;
302 }
303
304 /* Clear memory region list */
305
306 static void
307 mem_clear (void)
308 {
309 VEC_free (mem_region_s, mem_region_list);
310 }
311 \f
312
313 static void
314 mem_command (char *args, int from_tty)
315 {
316 CORE_ADDR lo, hi;
317 char *tok;
318 struct mem_attrib attrib;
319
320 if (!args)
321 error_no_arg (_("No mem"));
322
323 /* For "mem auto", switch back to using a target provided list. */
324 if (strcmp (args, "auto") == 0)
325 {
326 if (mem_use_target)
327 return;
328
329 if (mem_region_list != target_mem_region_list)
330 {
331 mem_clear ();
332 mem_region_list = target_mem_region_list;
333 }
334
335 mem_use_target = 1;
336 return;
337 }
338
339 require_user_regions (from_tty);
340
341 tok = strtok (args, " \t");
342 if (!tok)
343 error (_("no lo address"));
344 lo = parse_and_eval_address (tok);
345
346 tok = strtok (NULL, " \t");
347 if (!tok)
348 error (_("no hi address"));
349 hi = parse_and_eval_address (tok);
350
351 attrib = default_mem_attrib;
352 while ((tok = strtok (NULL, " \t")) != NULL)
353 {
354 if (strcmp (tok, "rw") == 0)
355 attrib.mode = MEM_RW;
356 else if (strcmp (tok, "ro") == 0)
357 attrib.mode = MEM_RO;
358 else if (strcmp (tok, "wo") == 0)
359 attrib.mode = MEM_WO;
360
361 else if (strcmp (tok, "8") == 0)
362 attrib.width = MEM_WIDTH_8;
363 else if (strcmp (tok, "16") == 0)
364 {
365 if ((lo % 2 != 0) || (hi % 2 != 0))
366 error (_("region bounds not 16 bit aligned"));
367 attrib.width = MEM_WIDTH_16;
368 }
369 else if (strcmp (tok, "32") == 0)
370 {
371 if ((lo % 4 != 0) || (hi % 4 != 0))
372 error (_("region bounds not 32 bit aligned"));
373 attrib.width = MEM_WIDTH_32;
374 }
375 else if (strcmp (tok, "64") == 0)
376 {
377 if ((lo % 8 != 0) || (hi % 8 != 0))
378 error (_("region bounds not 64 bit aligned"));
379 attrib.width = MEM_WIDTH_64;
380 }
381
382 #if 0
383 else if (strcmp (tok, "hwbreak") == 0)
384 attrib.hwbreak = 1;
385 else if (strcmp (tok, "swbreak") == 0)
386 attrib.hwbreak = 0;
387 #endif
388
389 else if (strcmp (tok, "cache") == 0)
390 attrib.cache = 1;
391 else if (strcmp (tok, "nocache") == 0)
392 attrib.cache = 0;
393
394 #if 0
395 else if (strcmp (tok, "verify") == 0)
396 attrib.verify = 1;
397 else if (strcmp (tok, "noverify") == 0)
398 attrib.verify = 0;
399 #endif
400
401 else
402 error (_("unknown attribute: %s"), tok);
403 }
404
405 create_mem_region (lo, hi, &attrib);
406 }
407 \f
408
409 static void
410 mem_info_command (char *args, int from_tty)
411 {
412 struct mem_region *m;
413 struct mem_attrib *attrib;
414 int ix;
415
416 if (mem_use_target)
417 printf_filtered (_("Using memory regions provided by the target.\n"));
418 else
419 printf_filtered (_("Using user-defined memory regions.\n"));
420
421 require_target_regions ();
422
423 if (!mem_region_list)
424 {
425 printf_unfiltered (_("There are no memory regions defined.\n"));
426 return;
427 }
428
429 printf_filtered ("Num ");
430 printf_filtered ("Enb ");
431 printf_filtered ("Low Addr ");
432 if (gdbarch_addr_bit (target_gdbarch) > 32)
433 printf_filtered (" ");
434 printf_filtered ("High Addr ");
435 if (gdbarch_addr_bit (target_gdbarch) > 32)
436 printf_filtered (" ");
437 printf_filtered ("Attrs ");
438 printf_filtered ("\n");
439
440 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
441 {
442 char *tmp;
443 printf_filtered ("%-3d %-3c\t",
444 m->number,
445 m->enabled_p ? 'y' : 'n');
446 if (gdbarch_addr_bit (target_gdbarch) <= 32)
447 tmp = hex_string_custom ((unsigned long) m->lo, 8);
448 else
449 tmp = hex_string_custom ((unsigned long) m->lo, 16);
450
451 printf_filtered ("%s ", tmp);
452
453 if (gdbarch_addr_bit (target_gdbarch) <= 32)
454 {
455 if (m->hi == 0)
456 tmp = "0x100000000";
457 else
458 tmp = hex_string_custom ((unsigned long) m->hi, 8);
459 }
460 else
461 {
462 if (m->hi == 0)
463 tmp = "0x10000000000000000";
464 else
465 tmp = hex_string_custom ((unsigned long) m->hi, 16);
466 }
467
468 printf_filtered ("%s ", tmp);
469
470 /* Print a token for each attribute.
471
472 * FIXME: Should we output a comma after each token? It may
473 * make it easier for users to read, but we'd lose the ability
474 * to cut-and-paste the list of attributes when defining a new
475 * region. Perhaps that is not important.
476 *
477 * FIXME: If more attributes are added to GDB, the output may
478 * become cluttered and difficult for users to read. At that
479 * time, we may want to consider printing tokens only if they
480 * are different from the default attribute. */
481
482 attrib = &m->attrib;
483 switch (attrib->mode)
484 {
485 case MEM_RW:
486 printf_filtered ("rw ");
487 break;
488 case MEM_RO:
489 printf_filtered ("ro ");
490 break;
491 case MEM_WO:
492 printf_filtered ("wo ");
493 break;
494 case MEM_FLASH:
495 printf_filtered ("flash blocksize 0x%x ", attrib->blocksize);
496 break;
497 }
498
499 switch (attrib->width)
500 {
501 case MEM_WIDTH_8:
502 printf_filtered ("8 ");
503 break;
504 case MEM_WIDTH_16:
505 printf_filtered ("16 ");
506 break;
507 case MEM_WIDTH_32:
508 printf_filtered ("32 ");
509 break;
510 case MEM_WIDTH_64:
511 printf_filtered ("64 ");
512 break;
513 case MEM_WIDTH_UNSPECIFIED:
514 break;
515 }
516
517 #if 0
518 if (attrib->hwbreak)
519 printf_filtered ("hwbreak");
520 else
521 printf_filtered ("swbreak");
522 #endif
523
524 if (attrib->cache)
525 printf_filtered ("cache ");
526 else
527 printf_filtered ("nocache ");
528
529 #if 0
530 if (attrib->verify)
531 printf_filtered ("verify ");
532 else
533 printf_filtered ("noverify ");
534 #endif
535
536 printf_filtered ("\n");
537
538 gdb_flush (gdb_stdout);
539 }
540 }
541 \f
542
543 /* Enable the memory region number NUM. */
544
545 static void
546 mem_enable (int num)
547 {
548 struct mem_region *m;
549 int ix;
550
551 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
552 if (m->number == num)
553 {
554 m->enabled_p = 1;
555 return;
556 }
557 printf_unfiltered (_("No memory region number %d.\n"), num);
558 }
559
560 static void
561 mem_enable_command (char *args, int from_tty)
562 {
563 char *p = args;
564 char *p1;
565 int num;
566 struct mem_region *m;
567 int ix;
568
569 require_user_regions (from_tty);
570
571 target_dcache_invalidate ();
572
573 if (p == 0)
574 {
575 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
576 m->enabled_p = 1;
577 }
578 else
579 while (*p)
580 {
581 p1 = p;
582 while (*p1 >= '0' && *p1 <= '9')
583 p1++;
584 if (*p1 && *p1 != ' ' && *p1 != '\t')
585 error (_("Arguments must be memory region numbers."));
586
587 num = atoi (p);
588 mem_enable (num);
589
590 p = p1;
591 while (*p == ' ' || *p == '\t')
592 p++;
593 }
594 }
595 \f
596
597 /* Disable the memory region number NUM. */
598
599 static void
600 mem_disable (int num)
601 {
602 struct mem_region *m;
603 int ix;
604
605 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
606 if (m->number == num)
607 {
608 m->enabled_p = 0;
609 return;
610 }
611 printf_unfiltered (_("No memory region number %d.\n"), num);
612 }
613
614 static void
615 mem_disable_command (char *args, int from_tty)
616 {
617 char *p = args;
618 char *p1;
619 int num;
620 struct mem_region *m;
621 int ix;
622
623 require_user_regions (from_tty);
624
625 target_dcache_invalidate ();
626
627 if (p == 0)
628 {
629 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
630 m->enabled_p = 0;
631 }
632 else
633 while (*p)
634 {
635 p1 = p;
636 while (*p1 >= '0' && *p1 <= '9')
637 p1++;
638 if (*p1 && *p1 != ' ' && *p1 != '\t')
639 error (_("Arguments must be memory region numbers."));
640
641 num = atoi (p);
642 mem_disable (num);
643
644 p = p1;
645 while (*p == ' ' || *p == '\t')
646 p++;
647 }
648 }
649
650 /* Delete the memory region number NUM. */
651
652 static void
653 mem_delete (int num)
654 {
655 struct mem_region *m;
656 int ix;
657
658 if (!mem_region_list)
659 {
660 printf_unfiltered (_("No memory region number %d.\n"), num);
661 return;
662 }
663
664 for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
665 if (m->number == num)
666 break;
667
668 if (m == NULL)
669 {
670 printf_unfiltered (_("No memory region number %d.\n"), num);
671 return;
672 }
673
674 VEC_ordered_remove (mem_region_s, mem_region_list, ix);
675 }
676
677 static void
678 mem_delete_command (char *args, int from_tty)
679 {
680 char *p = args;
681 char *p1;
682 int num;
683
684 require_user_regions (from_tty);
685
686 target_dcache_invalidate ();
687
688 if (p == 0)
689 {
690 if (query (_("Delete all memory regions? ")))
691 mem_clear ();
692 dont_repeat ();
693 return;
694 }
695
696 while (*p)
697 {
698 p1 = p;
699 while (*p1 >= '0' && *p1 <= '9')
700 p1++;
701 if (*p1 && *p1 != ' ' && *p1 != '\t')
702 error (_("Arguments must be memory region numbers."));
703
704 num = atoi (p);
705 mem_delete (num);
706
707 p = p1;
708 while (*p == ' ' || *p == '\t')
709 p++;
710 }
711
712 dont_repeat ();
713 }
714
715 static void
716 dummy_cmd (char *args, int from_tty)
717 {
718 }
719 \f
720 extern initialize_file_ftype _initialize_mem; /* -Wmissing-prototype */
721
722 static struct cmd_list_element *mem_set_cmdlist;
723 static struct cmd_list_element *mem_show_cmdlist;
724
725 void
726 _initialize_mem (void)
727 {
728 add_com ("mem", class_vars, mem_command, _("\
729 Define attributes for memory region or reset memory region handling to\n\
730 target-based.\n\
731 Usage: mem auto\n\
732 mem <lo addr> <hi addr> [<mode> <width> <cache>], \n\
733 where <mode> may be rw (read/write), ro (read-only) or wo (write-only), \n\
734 <width> may be 8, 16, 32, or 64, and \n\
735 <cache> may be cache or nocache"));
736
737 add_cmd ("mem", class_vars, mem_enable_command, _("\
738 Enable memory region.\n\
739 Arguments are the code numbers of the memory regions to enable.\n\
740 Usage: enable mem <code number>\n\
741 Do \"info mem\" to see current list of code numbers."), &enablelist);
742
743 add_cmd ("mem", class_vars, mem_disable_command, _("\
744 Disable memory region.\n\
745 Arguments are the code numbers of the memory regions to disable.\n\
746 Usage: disable mem <code number>\n\
747 Do \"info mem\" to see current list of code numbers."), &disablelist);
748
749 add_cmd ("mem", class_vars, mem_delete_command, _("\
750 Delete memory region.\n\
751 Arguments are the code numbers of the memory regions to delete.\n\
752 Usage: delete mem <code number>\n\
753 Do \"info mem\" to see current list of code numbers."), &deletelist);
754
755 add_info ("mem", mem_info_command,
756 _("Memory region attributes"));
757
758 add_prefix_cmd ("mem", class_vars, dummy_cmd, _("\
759 Memory regions settings"),
760 &mem_set_cmdlist, "set mem ",
761 0/* allow-unknown */, &setlist);
762 add_prefix_cmd ("mem", class_vars, dummy_cmd, _("\
763 Memory regions settings"),
764 &mem_show_cmdlist, "show mem ",
765 0/* allow-unknown */, &showlist);
766
767 add_setshow_boolean_cmd ("inaccessible-by-default", no_class,
768 &inaccessible_by_default, _("\
769 Set handling of unknown memory regions."), _("\
770 Show handling of unknown memory regions."), _("\
771 If on, and some memory map is defined, debugger will emit errors on\n\
772 accesses to memory not defined in the memory map. If off, accesses to all\n\
773 memory addresses will be allowed."),
774 NULL,
775 show_inaccessible_by_default,
776 &mem_set_cmdlist,
777 &mem_show_cmdlist);
778 }
This page took 0.046026 seconds and 4 git commands to generate.