gdb/addrmap.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* addrmap.c --- implementation of address map data structure.

  2.    Copyright (C) 2007-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "splay-tree.h"
  16. #include "gdb_obstack.h"
  17. #include "addrmap.h"


  18. /* The "abstract class".  */

  19. /* Functions implementing the addrmap functions for a particular
  20.    implementation.  */
  21. struct addrmap_funcs
  22. {
  23.   void (*set_empty) (struct addrmap *this,
  24.                      CORE_ADDR start, CORE_ADDR end_inclusive,
  25.                      void *obj);
  26.   void *(*find) (struct addrmap *this, CORE_ADDR addr);
  27.   struct addrmap *(*create_fixed) (struct addrmap *this,
  28.                                    struct obstack *obstack);
  29.   void (*relocate) (struct addrmap *this, CORE_ADDR offset);
  30.   int (*foreach) (struct addrmap *this, addrmap_foreach_fn fn, void *data);
  31. };


  32. struct addrmap
  33. {
  34.   const struct addrmap_funcs *funcs;
  35. };


  36. void
  37. addrmap_set_empty (struct addrmap *map,
  38.                    CORE_ADDR start, CORE_ADDR end_inclusive,
  39.                    void *obj)
  40. {
  41.   map->funcs->set_empty (map, start, end_inclusive, obj);
  42. }


  43. void *
  44. addrmap_find (struct addrmap *map, CORE_ADDR addr)
  45. {
  46.   return map->funcs->find (map, addr);
  47. }


  48. struct addrmap *
  49. addrmap_create_fixed (struct addrmap *original, struct obstack *obstack)
  50. {
  51.   return original->funcs->create_fixed (original, obstack);
  52. }


  53. /* Relocate all the addresses in MAP by OFFSET.  (This can be applied
  54.    to either mutable or immutable maps.)  */
  55. void
  56. addrmap_relocate (struct addrmap *map, CORE_ADDR offset)
  57. {
  58.   map->funcs->relocate (map, offset);
  59. }


  60. int
  61. addrmap_foreach (struct addrmap *map, addrmap_foreach_fn fn, void *data)
  62. {
  63.   return map->funcs->foreach (map, fn, data);
  64. }

  65. /* Fixed address maps.  */

  66. /* A transition: a point in an address map where the value changes.
  67.    The map maps ADDR to VALUE, but if ADDR > 0, it maps ADDR-1 to
  68.    something else.  */
  69. struct addrmap_transition
  70. {
  71.   CORE_ADDR addr;
  72.   void *value;
  73. };


  74. struct addrmap_fixed
  75. {
  76.   struct addrmap addrmap;

  77.   /* The number of transitions in TRANSITIONS.  */
  78.   size_t num_transitions;

  79.   /* An array of transitions, sorted by address.  For every point in
  80.      the map where either ADDR == 0 or ADDR is mapped to one value and
  81.      ADDR - 1 is mapped to something different, we have an entry here
  82.      containing ADDR and VALUE.  (Note that this means we always have
  83.      an entry for address 0).  */
  84.   struct addrmap_transition transitions[1];
  85. };


  86. static void
  87. addrmap_fixed_set_empty (struct addrmap *this,
  88.                    CORE_ADDR start, CORE_ADDR end_inclusive,
  89.                    void *obj)
  90. {
  91.   internal_error (__FILE__, __LINE__,
  92.                   "addrmap_fixed_set_empty: "
  93.                   "fixed addrmaps can't be changed\n");
  94. }


  95. static void *
  96. addrmap_fixed_find (struct addrmap *this, CORE_ADDR addr)
  97. {
  98.   struct addrmap_fixed *map = (struct addrmap_fixed *) this;
  99.   struct addrmap_transition *bottom = &map->transitions[0];
  100.   struct addrmap_transition *top = &map->transitions[map->num_transitions - 1];

  101.   while (bottom < top)
  102.     {
  103.       /* This needs to round towards top, or else when top = bottom +
  104.          1 (i.e., two entries are under consideration), then mid ==
  105.          bottom, and then we may not narrow the range when (mid->addr
  106.          < addr).  */
  107.       struct addrmap_transition *mid = top - (top - bottom) / 2;

  108.       if (mid->addr == addr)
  109.         {
  110.           bottom = mid;
  111.           break;
  112.         }
  113.       else if (mid->addr < addr)
  114.         /* We don't eliminate mid itself here, since each transition
  115.            covers all subsequent addresses until the next.  This is why
  116.            we must round up in computing the midpoint.  */
  117.         bottom = mid;
  118.       else
  119.         top = mid - 1;
  120.     }

  121.   return bottom->value;
  122. }


  123. static struct addrmap *
  124. addrmap_fixed_create_fixed (struct addrmap *this, struct obstack *obstack)
  125. {
  126.   internal_error (__FILE__, __LINE__,
  127.                   _("addrmap_create_fixed is not implemented yet "
  128.                     "for fixed addrmaps"));
  129. }


  130. static void
  131. addrmap_fixed_relocate (struct addrmap *this, CORE_ADDR offset)
  132. {
  133.   struct addrmap_fixed *map = (struct addrmap_fixed *) this;
  134.   size_t i;

  135.   for (i = 0; i < map->num_transitions; i++)
  136.     map->transitions[i].addr += offset;
  137. }


  138. static int
  139. addrmap_fixed_foreach (struct addrmap *this, addrmap_foreach_fn fn,
  140.                        void *data)
  141. {
  142.   struct addrmap_fixed *map = (struct addrmap_fixed *) this;
  143.   size_t i;

  144.   for (i = 0; i < map->num_transitions; i++)
  145.     {
  146.       int res = fn (data, map->transitions[i].addr, map->transitions[i].value);

  147.       if (res != 0)
  148.         return res;
  149.     }

  150.   return 0;
  151. }


  152. static const struct addrmap_funcs addrmap_fixed_funcs =
  153. {
  154.   addrmap_fixed_set_empty,
  155.   addrmap_fixed_find,
  156.   addrmap_fixed_create_fixed,
  157.   addrmap_fixed_relocate,
  158.   addrmap_fixed_foreach
  159. };



  160. /* Mutable address maps.  */

  161. struct addrmap_mutable
  162. {
  163.   struct addrmap addrmap;

  164.   /* The obstack to use for allocations for this map.  */
  165.   struct obstack *obstack;

  166.   /* A splay tree, with a node for each transition; there is a
  167.      transition at address T if T-1 and T map to different objects.

  168.      Any addresses below the first node map to NULL.  (Unlike
  169.      fixed maps, we have no entry at (CORE_ADDR) 0; it doesn't
  170.      simplify enough.)

  171.      The last region is assumed to end at CORE_ADDR_MAX.

  172.      Since we can't know whether CORE_ADDR is larger or smaller than
  173.      splay_tree_key (unsigned long) --- I think both are possible,
  174.      given all combinations of 32- and 64-bit hosts and targets ---
  175.      our keys are pointers to CORE_ADDR values.  Since the splay tree
  176.      library doesn't pass any closure pointer to the key free
  177.      function, we can't keep a freelist for keys.  Since mutable
  178.      addrmaps are only used temporarily right now, we just leak keys
  179.      from deleted nodes; they'll be freed when the obstack is freed.  */
  180.   splay_tree tree;

  181.   /* A freelist for splay tree nodes, allocated on obstack, and
  182.      chained together by their 'right' pointers.  */
  183.   splay_tree_node free_nodes;
  184. };


  185. /* Allocate a copy of CORE_ADDR in MAP's obstack.  */
  186. static splay_tree_key
  187. allocate_key (struct addrmap_mutable *map, CORE_ADDR addr)
  188. {
  189.   CORE_ADDR *key = obstack_alloc (map->obstack, sizeof (*key));

  190.   *key = addr;
  191.   return (splay_tree_key) key;
  192. }


  193. /* Type-correct wrappers for splay tree access.  */
  194. static splay_tree_node
  195. addrmap_splay_tree_lookup (struct addrmap_mutable *map, CORE_ADDR addr)
  196. {
  197.   return splay_tree_lookup (map->tree, (splay_tree_key) &addr);
  198. }


  199. static splay_tree_node
  200. addrmap_splay_tree_predecessor (struct addrmap_mutable *map, CORE_ADDR addr)
  201. {
  202.   return splay_tree_predecessor (map->tree, (splay_tree_key) &addr);
  203. }


  204. static splay_tree_node
  205. addrmap_splay_tree_successor (struct addrmap_mutable *map, CORE_ADDR addr)
  206. {
  207.   return splay_tree_successor (map->tree, (splay_tree_key) &addr);
  208. }


  209. static void
  210. addrmap_splay_tree_remove (struct addrmap_mutable *map, CORE_ADDR addr)
  211. {
  212.   splay_tree_remove (map->tree, (splay_tree_key) &addr);
  213. }


  214. static CORE_ADDR
  215. addrmap_node_key (splay_tree_node node)
  216. {
  217.   return * (CORE_ADDR *) node->key;
  218. }


  219. static void *
  220. addrmap_node_value (splay_tree_node node)
  221. {
  222.   return (void *) node->value;
  223. }


  224. static void
  225. addrmap_node_set_value (splay_tree_node node, void *value)
  226. {
  227.   node->value = (splay_tree_value) value;
  228. }


  229. static void
  230. addrmap_splay_tree_insert (struct addrmap_mutable *map,
  231.                            CORE_ADDR key, void *value)
  232. {
  233.   splay_tree_insert (map->tree,
  234.                      allocate_key (map, key),
  235.                      (splay_tree_value) value);
  236. }


  237. /* Without changing the mapping of any address, ensure that there is a
  238.    tree node at ADDR, even if it would represent a "transition" from
  239.    one value to the same value.  */
  240. static void
  241. force_transition (struct addrmap_mutable *this, CORE_ADDR addr)
  242. {
  243.   splay_tree_node n
  244.     = addrmap_splay_tree_lookup (this, addr);

  245.   if (! n)
  246.     {
  247.       n = addrmap_splay_tree_predecessor (this, addr);
  248.       addrmap_splay_tree_insert (this, addr,
  249.                                  n ? addrmap_node_value (n) : NULL);
  250.     }
  251. }


  252. static void
  253. addrmap_mutable_set_empty (struct addrmap *this,
  254.                            CORE_ADDR start, CORE_ADDR end_inclusive,
  255.                            void *obj)
  256. {
  257.   struct addrmap_mutable *map = (struct addrmap_mutable *) this;
  258.   splay_tree_node n, next;
  259.   void *prior_value;

  260.   /* If we're being asked to set all empty portions of the given
  261.      address range to empty, then probably the caller is confused.
  262.      (If that turns out to be useful in some cases, then we can change
  263.      this to simply return, since overriding NULL with NULL is a
  264.      no-op.)  */
  265.   gdb_assert (obj);

  266.   /* We take a two-pass approach, for simplicity.
  267.      - Establish transitions where we think we might need them.
  268.      - First pass: change all NULL regions to OBJ.
  269.      - Second pass: remove any unnecessary transitions.  */

  270.   /* Establish transitions at the start and end.  */
  271.   force_transition (map, start);
  272.   if (end_inclusive < CORE_ADDR_MAX)
  273.     force_transition (map, end_inclusive + 1);

  274.   /* Walk the area, changing all NULL regions to OBJ.  */
  275.   for (n = addrmap_splay_tree_lookup (map, start), gdb_assert (n);
  276.        n && addrmap_node_key (n) <= end_inclusive;
  277.        n = addrmap_splay_tree_successor (map, addrmap_node_key (n)))
  278.     {
  279.       if (! addrmap_node_value (n))
  280.         addrmap_node_set_value (n, obj);
  281.     }

  282.   /* Walk the area again, removing transitions from any value to
  283.      itself.  Be sure to visit both the transitions we forced
  284.      above.  */
  285.   n = addrmap_splay_tree_predecessor (map, start);
  286.   prior_value = n ? addrmap_node_value (n) : NULL;
  287.   for (n = addrmap_splay_tree_lookup (map, start), gdb_assert (n);
  288.        n && (end_inclusive == CORE_ADDR_MAX
  289.              || addrmap_node_key (n) <= end_inclusive + 1);
  290.        n = next)
  291.     {
  292.       next = addrmap_splay_tree_successor (map, addrmap_node_key (n));
  293.       if (addrmap_node_value (n) == prior_value)
  294.         addrmap_splay_tree_remove (map, addrmap_node_key (n));
  295.       else
  296.         prior_value = addrmap_node_value (n);
  297.     }
  298. }


  299. static void *
  300. addrmap_mutable_find (struct addrmap *this, CORE_ADDR addr)
  301. {
  302.   /* Not needed yet.  */
  303.   internal_error (__FILE__, __LINE__,
  304.                   _("addrmap_find is not implemented yet "
  305.                     "for mutable addrmaps"));
  306. }


  307. /* A function to pass to splay_tree_foreach to count the number of nodes
  308.    in the tree.  */
  309. static int
  310. splay_foreach_count (splay_tree_node n, void *closure)
  311. {
  312.   size_t *count = (size_t *) closure;

  313.   (*count)++;
  314.   return 0;
  315. }


  316. /* A function to pass to splay_tree_foreach to copy entries into a
  317.    fixed address map.  */
  318. static int
  319. splay_foreach_copy (splay_tree_node n, void *closure)
  320. {
  321.   struct addrmap_fixed *fixed = (struct addrmap_fixed *) closure;
  322.   struct addrmap_transition *t = &fixed->transitions[fixed->num_transitions];

  323.   t->addr = addrmap_node_key (n);
  324.   t->value = addrmap_node_value (n);
  325.   fixed->num_transitions++;

  326.   return 0;
  327. }


  328. static struct addrmap *
  329. addrmap_mutable_create_fixed (struct addrmap *this, struct obstack *obstack)
  330. {
  331.   struct addrmap_mutable *mutable = (struct addrmap_mutable *) this;
  332.   struct addrmap_fixed *fixed;
  333.   size_t num_transitions;

  334.   /* Count the number of transitions in the tree.  */
  335.   num_transitions = 0;
  336.   splay_tree_foreach (mutable->tree, splay_foreach_count, &num_transitions);

  337.   /* Include an extra entry for the transition at zero (which fixed
  338.      maps have, but mutable maps do not.)  */
  339.   num_transitions++;

  340.   fixed = obstack_alloc (obstack,
  341.                          (sizeof (*fixed)
  342.                           + (num_transitions
  343.                              * sizeof (fixed->transitions[0]))));
  344.   fixed->addrmap.funcs = &addrmap_fixed_funcs;
  345.   fixed->num_transitions = 1;
  346.   fixed->transitions[0].addr = 0;
  347.   fixed->transitions[0].value = NULL;

  348.   /* Copy all entries from the splay tree to the array, in order
  349.      of increasing address.  */
  350.   splay_tree_foreach (mutable->tree, splay_foreach_copy, fixed);

  351.   /* We should have filled the array.  */
  352.   gdb_assert (fixed->num_transitions == num_transitions);

  353.   return (struct addrmap *) fixed;
  354. }


  355. static void
  356. addrmap_mutable_relocate (struct addrmap *this, CORE_ADDR offset)
  357. {
  358.   /* Not needed yet.  */
  359.   internal_error (__FILE__, __LINE__,
  360.                   _("addrmap_relocate is not implemented yet "
  361.                     "for mutable addrmaps"));
  362. }


  363. /* Struct to map addrmap's foreach function to splay_tree's version.  */
  364. struct mutable_foreach_data
  365. {
  366.   addrmap_foreach_fn fn;
  367.   void *data;
  368. };


  369. /* This is a splay_tree_foreach_fn.  */

  370. static int
  371. addrmap_mutable_foreach_worker (splay_tree_node node, void *data)
  372. {
  373.   struct mutable_foreach_data *foreach_data = data;

  374.   return foreach_data->fn (foreach_data->data,
  375.                            addrmap_node_key (node),
  376.                            addrmap_node_value (node));
  377. }


  378. static int
  379. addrmap_mutable_foreach (struct addrmap *this, addrmap_foreach_fn fn,
  380.                          void *data)
  381. {
  382.   struct addrmap_mutable *mutable = (struct addrmap_mutable *) this;
  383.   struct mutable_foreach_data foreach_data;

  384.   foreach_data.fn = fn;
  385.   foreach_data.data = data;
  386.   return splay_tree_foreach (mutable->tree, addrmap_mutable_foreach_worker,
  387.                              &foreach_data);
  388. }


  389. static const struct addrmap_funcs addrmap_mutable_funcs =
  390. {
  391.   addrmap_mutable_set_empty,
  392.   addrmap_mutable_find,
  393.   addrmap_mutable_create_fixed,
  394.   addrmap_mutable_relocate,
  395.   addrmap_mutable_foreach
  396. };


  397. static void *
  398. splay_obstack_alloc (int size, void *closure)
  399. {
  400.   struct addrmap_mutable *map = closure;
  401.   splay_tree_node n;

  402.   /* We should only be asked to allocate nodes and larger things.
  403.      (If, at some point in the future, this is no longer true, we can
  404.      just round up the size to sizeof (*n).)  */
  405.   gdb_assert (size >= sizeof (*n));

  406.   if (map->free_nodes)
  407.     {
  408.       n = map->free_nodes;
  409.       map->free_nodes = n->right;
  410.       return n;
  411.     }
  412.   else
  413.     return obstack_alloc (map->obstack, size);
  414. }


  415. static void
  416. splay_obstack_free (void *obj, void *closure)
  417. {
  418.   struct addrmap_mutable *map = closure;
  419.   splay_tree_node n = obj;

  420.   /* We've asserted in the allocation function that we only allocate
  421.      nodes or larger things, so it should be safe to put whatever
  422.      we get passed back on the free list.  */
  423.   n->right = map->free_nodes;
  424.   map->free_nodes = n;
  425. }


  426. /* Compare keys as CORE_ADDR * values.  */
  427. static int
  428. splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
  429. {
  430.   CORE_ADDR a = * (CORE_ADDR *) ak;
  431.   CORE_ADDR b = * (CORE_ADDR *) bk;

  432.   /* We can't just return a-b here, because of over/underflow.  */
  433.   if (a < b)
  434.     return -1;
  435.   else if (a == b)
  436.     return 0;
  437.   else
  438.     return 1;
  439. }


  440. struct addrmap *
  441. addrmap_create_mutable (struct obstack *obstack)
  442. {
  443.   struct addrmap_mutable *map = obstack_alloc (obstack, sizeof (*map));

  444.   map->addrmap.funcs = &addrmap_mutable_funcs;
  445.   map->obstack = obstack;

  446.   /* splay_tree_new_with_allocator uses the provided allocation
  447.      function to allocate the main splay_tree structure itself, so our
  448.      free list has to be initialized before we create the tree.  */
  449.   map->free_nodes = NULL;

  450.   map->tree = splay_tree_new_with_allocator (splay_compare_CORE_ADDR_ptr,
  451.                                              NULL, /* no delete key */
  452.                                              NULL, /* no delete value */
  453.                                              splay_obstack_alloc,
  454.                                              splay_obstack_free,
  455.                                              map);

  456.   return (struct addrmap *) map;
  457. }



  458. /* Initialization.  */

  459. /* Provide a prototype to silence -Wmissing-prototypes.  */
  460. extern initialize_file_ftype _initialize_addrmap;

  461. void
  462. _initialize_addrmap (void)
  463. {
  464.   /* Make sure splay trees can actually hold the values we want to
  465.      store in them.  */
  466.   gdb_assert (sizeof (splay_tree_key) >= sizeof (CORE_ADDR *));
  467.   gdb_assert (sizeof (splay_tree_value) >= sizeof (void *));
  468. }