gdb/prologue-value.c - gdb

Data types defined

Functions defined

Source code

  1. /* Prologue value handling for GDB.
  2.    Copyright (C) 2003-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 "prologue-value.h"
  16. #include "regcache.h"


  17. /* Constructors.  */

  18. pv_t
  19. pv_unknown (void)
  20. {
  21.   pv_t v = { pvk_unknown, 0, 0 };

  22.   return v;
  23. }


  24. pv_t
  25. pv_constant (CORE_ADDR k)
  26. {
  27.   pv_t v;

  28.   v.kind = pvk_constant;
  29.   v.reg = -1;                   /* for debugging */
  30.   v.k = k;

  31.   return v;
  32. }


  33. pv_t
  34. pv_register (int reg, CORE_ADDR k)
  35. {
  36.   pv_t v;

  37.   v.kind = pvk_register;
  38.   v.reg = reg;
  39.   v.k = k;

  40.   return v;
  41. }



  42. /* Arithmetic operations.  */

  43. /* If one of *A and *B is a constant, and the other isn't, swap the
  44.    values as necessary to ensure that *B is the constant.  This can
  45.    reduce the number of cases we need to analyze in the functions
  46.    below.  */
  47. static void
  48. constant_last (pv_t *a, pv_t *b)
  49. {
  50.   if (a->kind == pvk_constant
  51.       && b->kind != pvk_constant)
  52.     {
  53.       pv_t temp = *a;
  54.       *a = *b;
  55.       *b = temp;
  56.     }
  57. }


  58. pv_t
  59. pv_add (pv_t a, pv_t b)
  60. {
  61.   constant_last (&a, &b);

  62.   /* We can add a constant to a register.  */
  63.   if (a.kind == pvk_register
  64.       && b.kind == pvk_constant)
  65.     return pv_register (a.reg, a.k + b.k);

  66.   /* We can add a constant to another constant.  */
  67.   else if (a.kind == pvk_constant
  68.            && b.kind == pvk_constant)
  69.     return pv_constant (a.k + b.k);

  70.   /* Anything else we don't know how to add.  We don't have a
  71.      representation for, say, the sum of two registers, or a multiple
  72.      of a register's value (adding a register to itself).  */
  73.   else
  74.     return pv_unknown ();
  75. }


  76. pv_t
  77. pv_add_constant (pv_t v, CORE_ADDR k)
  78. {
  79.   /* Rather than thinking of all the cases we can and can't handle,
  80.      we'll just let pv_add take care of that for us.  */
  81.   return pv_add (v, pv_constant (k));
  82. }


  83. pv_t
  84. pv_subtract (pv_t a, pv_t b)
  85. {
  86.   /* This isn't quite the same as negating B and adding it to A, since
  87.      we don't have a representation for the negation of anything but a
  88.      constant.  For example, we can't negate { pvk_register, R1, 10 },
  89.      but we do know that { pvk_register, R1, 10 } minus { pvk_register,
  90.      R1, 5 } is { pvk_constant, <ignored>, 5 }.

  91.      This means, for example, that we could subtract two stack
  92.      addresses; they're both relative to the original SP.  Since the
  93.      frame pointer is set based on the SP, its value will be the
  94.      original SP plus some constant (probably zero), so we can use its
  95.      value just fine, too.  */

  96.   constant_last (&a, &b);

  97.   /* We can subtract two constants.  */
  98.   if (a.kind == pvk_constant
  99.       && b.kind == pvk_constant)
  100.     return pv_constant (a.k - b.k);

  101.   /* We can subtract a constant from a register.  */
  102.   else if (a.kind == pvk_register
  103.            && b.kind == pvk_constant)
  104.     return pv_register (a.reg, a.k - b.k);

  105.   /* We can subtract a register from itself, yielding a constant.  */
  106.   else if (a.kind == pvk_register
  107.            && b.kind == pvk_register
  108.            && a.reg == b.reg)
  109.     return pv_constant (a.k - b.k);

  110.   /* We don't know how to subtract anything else.  */
  111.   else
  112.     return pv_unknown ();
  113. }


  114. pv_t
  115. pv_logical_and (pv_t a, pv_t b)
  116. {
  117.   constant_last (&a, &b);

  118.   /* We can 'and' two constants.  */
  119.   if (a.kind == pvk_constant
  120.       && b.kind == pvk_constant)
  121.     return pv_constant (a.k & b.k);

  122.   /* We can 'and' anything with the constant zero.  */
  123.   else if (b.kind == pvk_constant
  124.            && b.k == 0)
  125.     return pv_constant (0);

  126.   /* We can 'and' anything with ~0.  */
  127.   else if (b.kind == pvk_constant
  128.            && b.k == ~ (CORE_ADDR) 0)
  129.     return a;

  130.   /* We can 'and' a register with itself.  */
  131.   else if (a.kind == pvk_register
  132.            && b.kind == pvk_register
  133.            && a.reg == b.reg
  134.            && a.k == b.k)
  135.     return a;

  136.   /* Otherwise, we don't know.  */
  137.   else
  138.     return pv_unknown ();
  139. }



  140. /* Examining prologue values.  */

  141. int
  142. pv_is_identical (pv_t a, pv_t b)
  143. {
  144.   if (a.kind != b.kind)
  145.     return 0;

  146.   switch (a.kind)
  147.     {
  148.     case pvk_unknown:
  149.       return 1;
  150.     case pvk_constant:
  151.       return (a.k == b.k);
  152.     case pvk_register:
  153.       return (a.reg == b.reg && a.k == b.k);
  154.     default:
  155.       gdb_assert_not_reached ("unexpected prologue value kind");
  156.     }
  157. }


  158. int
  159. pv_is_constant (pv_t a)
  160. {
  161.   return (a.kind == pvk_constant);
  162. }


  163. int
  164. pv_is_register (pv_t a, int r)
  165. {
  166.   return (a.kind == pvk_register
  167.           && a.reg == r);
  168. }


  169. int
  170. pv_is_register_k (pv_t a, int r, CORE_ADDR k)
  171. {
  172.   return (a.kind == pvk_register
  173.           && a.reg == r
  174.           && a.k == k);
  175. }


  176. enum pv_boolean
  177. pv_is_array_ref (pv_t addr, CORE_ADDR size,
  178.                  pv_t array_addr, CORE_ADDR array_len,
  179.                  CORE_ADDR elt_size,
  180.                  int *i)
  181. {
  182.   /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if
  183.      addr is *before* the start of the array, then this isn't going to
  184.      be negative...  */
  185.   pv_t offset = pv_subtract (addr, array_addr);

  186.   if (offset.kind == pvk_constant)
  187.     {
  188.       /* This is a rather odd test.  We want to know if the SIZE bytes
  189.          at ADDR don't overlap the array at all, so you'd expect it to
  190.          be an || expression: "if we're completely before || we're
  191.          completely after".  But with unsigned arithmetic, things are
  192.          different: since it's a number circle, not a number line, the
  193.          right values for offset.k are actually one contiguous range.  */
  194.       if (offset.k <= -size
  195.           && offset.k >= array_len * elt_size)
  196.         return pv_definite_no;
  197.       else if (offset.k % elt_size != 0
  198.                || size != elt_size)
  199.         return pv_maybe;
  200.       else
  201.         {
  202.           *i = offset.k / elt_size;
  203.           return pv_definite_yes;
  204.         }
  205.     }
  206.   else
  207.     return pv_maybe;
  208. }



  209. /* Areas.  */


  210. /* A particular value known to be stored in an area.

  211.    Entries form a ring, sorted by unsigned offset from the area's base
  212.    register's value.  Since entries can straddle the wrap-around point,
  213.    unsigned offsets form a circle, not a number line, so the list
  214.    itself is structured the same way --- there is no inherent head.
  215.    The entry with the lowest offset simply follows the entry with the
  216.    highest offset.  Entries may abut, but never overlap.  The area's
  217.    'entry' pointer points to an arbitrary node in the ring.  */
  218. struct area_entry
  219. {
  220.   /* Links in the doubly-linked ring.  */
  221.   struct area_entry *prev, *next;

  222.   /* Offset of this entry's address from the value of the base
  223.      register.  */
  224.   CORE_ADDR offset;

  225.   /* The size of this entry.  Note that an entry may wrap around from
  226.      the end of the address space to the beginning.  */
  227.   CORE_ADDR size;

  228.   /* The value stored here.  */
  229.   pv_t value;
  230. };


  231. struct pv_area
  232. {
  233.   /* This area's base register.  */
  234.   int base_reg;

  235.   /* The mask to apply to addresses, to make the wrap-around happen at
  236.      the right place.  */
  237.   CORE_ADDR addr_mask;

  238.   /* An element of the doubly-linked ring of entries, or zero if we
  239.      have none.  */
  240.   struct area_entry *entry;
  241. };


  242. struct pv_area *
  243. make_pv_area (int base_reg, int addr_bit)
  244. {
  245.   struct pv_area *a = (struct pv_area *) xmalloc (sizeof (*a));

  246.   memset (a, 0, sizeof (*a));

  247.   a->base_reg = base_reg;
  248.   a->entry = 0;

  249.   /* Remember that shift amounts equal to the type's width are
  250.      undefined.  */
  251.   a->addr_mask = ((((CORE_ADDR) 1 << (addr_bit - 1)) - 1) << 1) | 1;

  252.   return a;
  253. }


  254. /* Delete all entries from AREA.  */
  255. static void
  256. clear_entries (struct pv_area *area)
  257. {
  258.   struct area_entry *e = area->entry;

  259.   if (e)
  260.     {
  261.       /* This needs to be a do-while loop, in order to actually
  262.          process the node being checked for in the terminating
  263.          condition.  */
  264.       do
  265.         {
  266.           struct area_entry *next = e->next;

  267.           xfree (e);
  268.           e = next;
  269.         }
  270.       while (e != area->entry);

  271.       area->entry = 0;
  272.     }
  273. }


  274. void
  275. free_pv_area (struct pv_area *area)
  276. {
  277.   clear_entries (area);
  278.   xfree (area);
  279. }


  280. static void
  281. do_free_pv_area_cleanup (void *arg)
  282. {
  283.   free_pv_area ((struct pv_area *) arg);
  284. }


  285. struct cleanup *
  286. make_cleanup_free_pv_area (struct pv_area *area)
  287. {
  288.   return make_cleanup (do_free_pv_area_cleanup, (void *) area);
  289. }


  290. int
  291. pv_area_store_would_trash (struct pv_area *area, pv_t addr)
  292. {
  293.   /* It may seem odd that pvk_constant appears here --- after all,
  294.      that's the case where we know the most about the address!  But
  295.      pv_areas are always relative to a register, and we don't know the
  296.      value of the register, so we can't compare entry addresses to
  297.      constants.  */
  298.   return (addr.kind == pvk_unknown
  299.           || addr.kind == pvk_constant
  300.           || (addr.kind == pvk_register && addr.reg != area->base_reg));
  301. }


  302. /* Return a pointer to the first entry we hit in AREA starting at
  303.    OFFSET and going forward.

  304.    This may return zero, if AREA has no entries.

  305.    And since the entries are a ring, this may return an entry that
  306.    entirely precedes OFFSET.  This is the correct behavior: depending
  307.    on the sizes involved, we could still overlap such an area, with
  308.    wrap-around.  */
  309. static struct area_entry *
  310. find_entry (struct pv_area *area, CORE_ADDR offset)
  311. {
  312.   struct area_entry *e = area->entry;

  313.   if (! e)
  314.     return 0;

  315.   /* If the next entry would be better than the current one, then scan
  316.      forward.  Since we use '<' in this loop, it always terminates.

  317.      Note that, even setting aside the addr_mask stuff, we must not
  318.      simplify this, in high school algebra fashion, to
  319.      (e->next->offset < e->offset), because of the way < interacts
  320.      with wrap-around.  We have to subtract offset from both sides to
  321.      make sure both things we're comparing are on the same side of the
  322.      discontinuity.  */
  323.   while (((e->next->offset - offset) & area->addr_mask)
  324.          < ((e->offset - offset) & area->addr_mask))
  325.     e = e->next;

  326.   /* If the previous entry would be better than the current one, then
  327.      scan backwards.  */
  328.   while (((e->prev->offset - offset) & area->addr_mask)
  329.          < ((e->offset - offset) & area->addr_mask))
  330.     e = e->prev;

  331.   /* In case there's some locality to the searches, set the area's
  332.      pointer to the entry we've found.  */
  333.   area->entry = e;

  334.   return e;
  335. }


  336. /* Return non-zero if the SIZE bytes at OFFSET would overlap ENTRY;
  337.    return zero otherwise.  AREA is the area to which ENTRY belongs.  */
  338. static int
  339. overlaps (struct pv_area *area,
  340.           struct area_entry *entry,
  341.           CORE_ADDR offset,
  342.           CORE_ADDR size)
  343. {
  344.   /* Think carefully about wrap-around before simplifying this.  */
  345.   return (((entry->offset - offset) & area->addr_mask) < size
  346.           || ((offset - entry->offset) & area->addr_mask) < entry->size);
  347. }


  348. void
  349. pv_area_store (struct pv_area *area,
  350.                pv_t addr,
  351.                CORE_ADDR size,
  352.                pv_t value)
  353. {
  354.   /* Remove any (potentially) overlapping entries.  */
  355.   if (pv_area_store_would_trash (area, addr))
  356.     clear_entries (area);
  357.   else
  358.     {
  359.       CORE_ADDR offset = addr.k;
  360.       struct area_entry *e = find_entry (area, offset);

  361.       /* Delete all entries that we would overlap.  */
  362.       while (e && overlaps (area, e, offset, size))
  363.         {
  364.           struct area_entry *next = (e->next == e) ? 0 : e->next;

  365.           e->prev->next = e->next;
  366.           e->next->prev = e->prev;

  367.           xfree (e);
  368.           e = next;
  369.         }

  370.       /* Move the area's pointer to the next remaining entry.  This
  371.          will also zero the pointer if we've deleted all the entries.  */
  372.       area->entry = e;
  373.     }

  374.   /* Now, there are no entries overlapping us, and area->entry is
  375.      either zero or pointing at the closest entry after us.  We can
  376.      just insert ourselves before that.

  377.      But if we're storing an unknown value, don't bother --- that's
  378.      the default.  */
  379.   if (value.kind == pvk_unknown)
  380.     return;
  381.   else
  382.     {
  383.       CORE_ADDR offset = addr.k;
  384.       struct area_entry *e = (struct area_entry *) xmalloc (sizeof (*e));

  385.       e->offset = offset;
  386.       e->size = size;
  387.       e->value = value;

  388.       if (area->entry)
  389.         {
  390.           e->prev = area->entry->prev;
  391.           e->next = area->entry;
  392.           e->prev->next = e->next->prev = e;
  393.         }
  394.       else
  395.         {
  396.           e->prev = e->next = e;
  397.           area->entry = e;
  398.         }
  399.     }
  400. }


  401. pv_t
  402. pv_area_fetch (struct pv_area *area, pv_t addr, CORE_ADDR size)
  403. {
  404.   /* If we have no entries, or we can't decide how ADDR relates to the
  405.      entries we do have, then the value is unknown.  */
  406.   if (! area->entry
  407.       || pv_area_store_would_trash (area, addr))
  408.     return pv_unknown ();
  409.   else
  410.     {
  411.       CORE_ADDR offset = addr.k;
  412.       struct area_entry *e = find_entry (area, offset);

  413.       /* If this entry exactly matches what we're looking for, then
  414.          we're set.  Otherwise, say it's unknown.  */
  415.       if (e->offset == offset && e->size == size)
  416.         return e->value;
  417.       else
  418.         return pv_unknown ();
  419.     }
  420. }


  421. int
  422. pv_area_find_reg (struct pv_area *area,
  423.                   struct gdbarch *gdbarch,
  424.                   int reg,
  425.                   CORE_ADDR *offset_p)
  426. {
  427.   struct area_entry *e = area->entry;

  428.   if (e)
  429.     do
  430.       {
  431.         if (e->value.kind == pvk_register
  432.             && e->value.reg == reg
  433.             && e->value.k == 0
  434.             && e->size == register_size (gdbarch, reg))
  435.           {
  436.             if (offset_p)
  437.               *offset_p = e->offset;
  438.             return 1;
  439.           }

  440.         e = e->next;
  441.       }
  442.     while (e != area->entry);

  443.   return 0;
  444. }


  445. void
  446. pv_area_scan (struct pv_area *area,
  447.               void (*func) (void *closure,
  448.                             pv_t addr,
  449.                             CORE_ADDR size,
  450.                             pv_t value),
  451.               void *closure)
  452. {
  453.   struct area_entry *e = area->entry;
  454.   pv_t addr;

  455.   addr.kind = pvk_register;
  456.   addr.reg = area->base_reg;

  457.   if (e)
  458.     do
  459.       {
  460.         addr.k = e->offset;
  461.         func (closure, addr, e->size, e->value);
  462.         e = e->next;
  463.       }
  464.     while (e != area->entry);
  465. }