gdb/frame.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Cache and manage frames for GDB, the GNU debugger.

  2.    Copyright (C) 1986-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 "frame.h"
  16. #include "target.h"
  17. #include "value.h"
  18. #include "inferior.h"        /* for inferior_ptid */
  19. #include "regcache.h"
  20. #include "user-regs.h"
  21. #include "gdb_obstack.h"
  22. #include "dummy-frame.h"
  23. #include "sentinel-frame.h"
  24. #include "gdbcore.h"
  25. #include "annotate.h"
  26. #include "language.h"
  27. #include "frame-unwind.h"
  28. #include "frame-base.h"
  29. #include "command.h"
  30. #include "gdbcmd.h"
  31. #include "observer.h"
  32. #include "objfiles.h"
  33. #include "gdbthread.h"
  34. #include "block.h"
  35. #include "inline-frame.h"
  36. #include "tracepoint.h"
  37. #include "hashtab.h"
  38. #include "valprint.h"

  39. static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
  40. static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);

  41. /* Status of some values cached in the frame_info object.  */

  42. enum cached_copy_status
  43. {
  44.   /* Value is unknown.  */
  45.   CC_UNKNOWN,

  46.   /* We have a value.  */
  47.   CC_VALUE,

  48.   /* Value was not saved.  */
  49.   CC_NOT_SAVED,

  50.   /* Value is unavailable.  */
  51.   CC_UNAVAILABLE
  52. };

  53. /* We keep a cache of stack frames, each of which is a "struct
  54.    frame_info".  The innermost one gets allocated (in
  55.    wait_for_inferior) each time the inferior stops; current_frame
  56.    points to it.  Additional frames get allocated (in get_prev_frame)
  57.    as needed, and are chained through the next and prev fields.  Any
  58.    time that the frame cache becomes invalid (most notably when we
  59.    execute something, but also if we change how we interpret the
  60.    frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
  61.    which reads new symbols)), we should call reinit_frame_cache.  */

  62. struct frame_info
  63. {
  64.   /* Level of this frame.  The inner-most (youngest) frame is at level
  65.      0.  As you move towards the outer-most (oldest) frame, the level
  66.      increases.  This is a cached value.  It could just as easily be
  67.      computed by counting back from the selected frame to the inner
  68.      most frame.  */
  69.   /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
  70.      reserved to indicate a bogus frame - one that has been created
  71.      just to keep GDB happy (GDB always needs a frame).  For the
  72.      moment leave this as speculation.  */
  73.   int level;

  74.   /* The frame's program space.  */
  75.   struct program_space *pspace;

  76.   /* The frame's address space.  */
  77.   struct address_space *aspace;

  78.   /* The frame's low-level unwinder and corresponding cache.  The
  79.      low-level unwinder is responsible for unwinding register values
  80.      for the previous frame.  The low-level unwind methods are
  81.      selected based on the presence, or otherwise, of register unwind
  82.      information such as CFI.  */
  83.   void *prologue_cache;
  84.   const struct frame_unwind *unwind;

  85.   /* Cached copy of the previous frame's architecture.  */
  86.   struct
  87.   {
  88.     int p;
  89.     struct gdbarch *arch;
  90.   } prev_arch;

  91.   /* Cached copy of the previous frame's resume address.  */
  92.   struct {
  93.     enum cached_copy_status status;
  94.     CORE_ADDR value;
  95.   } prev_pc;

  96.   /* Cached copy of the previous frame's function address.  */
  97.   struct
  98.   {
  99.     CORE_ADDR addr;
  100.     int p;
  101.   } prev_func;

  102.   /* This frame's ID.  */
  103.   struct
  104.   {
  105.     int p;
  106.     struct frame_id value;
  107.   } this_id;

  108.   /* The frame's high-level base methods, and corresponding cache.
  109.      The high level base methods are selected based on the frame's
  110.      debug info.  */
  111.   const struct frame_base *base;
  112.   void *base_cache;

  113.   /* Pointers to the next (down, inner, younger) and previous (up,
  114.      outer, older) frame_info's in the frame cache.  */
  115.   struct frame_info *next; /* down, inner, younger */
  116.   int prev_p;
  117.   struct frame_info *prev; /* up, outer, older */

  118.   /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
  119.      could.  Only valid when PREV_P is set.  */
  120.   enum unwind_stop_reason stop_reason;

  121.   /* A frame specific string describing the STOP_REASON in more detail.
  122.      Only valid when PREV_P is set, but even then may still be NULL.  */
  123.   const char *stop_string;
  124. };

  125. /* A frame stash used to speed up frame lookups.  Create a hash table
  126.    to stash frames previously accessed from the frame cache for
  127.    quicker subsequent retrieval.  The hash table is emptied whenever
  128.    the frame cache is invalidated.  */

  129. static htab_t frame_stash;

  130. /* Internal function to calculate a hash from the frame_id addresses,
  131.    using as many valid addresses as possible.  Frames below level 0
  132.    are not stored in the hash table.  */

  133. static hashval_t
  134. frame_addr_hash (const void *ap)
  135. {
  136.   const struct frame_info *frame = ap;
  137.   const struct frame_id f_id = frame->this_id.value;
  138.   hashval_t hash = 0;

  139.   gdb_assert (f_id.stack_status != FID_STACK_INVALID
  140.               || f_id.code_addr_p
  141.               || f_id.special_addr_p);

  142.   if (f_id.stack_status == FID_STACK_VALID)
  143.     hash = iterative_hash (&f_id.stack_addr,
  144.                            sizeof (f_id.stack_addr), hash);
  145.   if (f_id.code_addr_p)
  146.     hash = iterative_hash (&f_id.code_addr,
  147.                            sizeof (f_id.code_addr), hash);
  148.   if (f_id.special_addr_p)
  149.     hash = iterative_hash (&f_id.special_addr,
  150.                            sizeof (f_id.special_addr), hash);

  151.   return hash;
  152. }

  153. /* Internal equality function for the hash table.  This function
  154.    defers equality operations to frame_id_eq.  */

  155. static int
  156. frame_addr_hash_eq (const void *a, const void *b)
  157. {
  158.   const struct frame_info *f_entry = a;
  159.   const struct frame_info *f_element = b;

  160.   return frame_id_eq (f_entry->this_id.value,
  161.                       f_element->this_id.value);
  162. }

  163. /* Internal function to create the frame_stash hash table.  100 seems
  164.    to be a good compromise to start the hash table at.  */

  165. static void
  166. frame_stash_create (void)
  167. {
  168.   frame_stash = htab_create (100,
  169.                              frame_addr_hash,
  170.                              frame_addr_hash_eq,
  171.                              NULL);
  172. }

  173. /* Internal function to add a frame to the frame_stash hash table.
  174.    Returns false if a frame with the same ID was already stashed, true
  175.    otherwise.  */

  176. static int
  177. frame_stash_add (struct frame_info *frame)
  178. {
  179.   struct frame_info **slot;

  180.   /* Do not try to stash the sentinel frame.  */
  181.   gdb_assert (frame->level >= 0);

  182.   slot = (struct frame_info **) htab_find_slot (frame_stash,
  183.                                                 frame,
  184.                                                 INSERT);

  185.   /* If we already have a frame in the stack with the same id, we
  186.      either have a stack cycle (corrupted stack?), or some bug
  187.      elsewhere in GDB.  In any case, ignore the duplicate and return
  188.      an indication to the caller.  */
  189.   if (*slot != NULL)
  190.     return 0;

  191.   *slot = frame;
  192.   return 1;
  193. }

  194. /* Internal function to search the frame stash for an entry with the
  195.    given frame ID.  If found, return that frame.  Otherwise return
  196.    NULL.  */

  197. static struct frame_info *
  198. frame_stash_find (struct frame_id id)
  199. {
  200.   struct frame_info dummy;
  201.   struct frame_info *frame;

  202.   dummy.this_id.value = id;
  203.   frame = htab_find (frame_stash, &dummy);
  204.   return frame;
  205. }

  206. /* Internal function to invalidate the frame stash by removing all
  207.    entries in it.  This only occurs when the frame cache is
  208.    invalidated.  */

  209. static void
  210. frame_stash_invalidate (void)
  211. {
  212.   htab_empty (frame_stash);
  213. }

  214. /* Flag to control debugging.  */

  215. unsigned int frame_debug;
  216. static void
  217. show_frame_debug (struct ui_file *file, int from_tty,
  218.                   struct cmd_list_element *c, const char *value)
  219. {
  220.   fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
  221. }

  222. /* Flag to indicate whether backtraces should stop at main et.al.  */

  223. static int backtrace_past_main;
  224. static void
  225. show_backtrace_past_main (struct ui_file *file, int from_tty,
  226.                           struct cmd_list_element *c, const char *value)
  227. {
  228.   fprintf_filtered (file,
  229.                     _("Whether backtraces should "
  230.                       "continue past \"main\" is %s.\n"),
  231.                     value);
  232. }

  233. static int backtrace_past_entry;
  234. static void
  235. show_backtrace_past_entry (struct ui_file *file, int from_tty,
  236.                            struct cmd_list_element *c, const char *value)
  237. {
  238.   fprintf_filtered (file, _("Whether backtraces should continue past the "
  239.                             "entry point of a program is %s.\n"),
  240.                     value);
  241. }

  242. static unsigned int backtrace_limit = UINT_MAX;
  243. static void
  244. show_backtrace_limit (struct ui_file *file, int from_tty,
  245.                       struct cmd_list_element *c, const char *value)
  246. {
  247.   fprintf_filtered (file,
  248.                     _("An upper bound on the number "
  249.                       "of backtrace levels is %s.\n"),
  250.                     value);
  251. }


  252. static void
  253. fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
  254. {
  255.   if (p)
  256.     fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
  257.   else
  258.     fprintf_unfiltered (file, "!%s", name);
  259. }

  260. void
  261. fprint_frame_id (struct ui_file *file, struct frame_id id)
  262. {
  263.   fprintf_unfiltered (file, "{");

  264.   if (id.stack_status == FID_STACK_INVALID)
  265.     fprintf_unfiltered (file, "!stack");
  266.   else if (id.stack_status == FID_STACK_UNAVAILABLE)
  267.     fprintf_unfiltered (file, "stack=<unavailable>");
  268.   else
  269.     fprintf_unfiltered (file, "stack=%s", hex_string (id.stack_addr));
  270.   fprintf_unfiltered (file, ",");

  271.   fprint_field (file, "code", id.code_addr_p, id.code_addr);
  272.   fprintf_unfiltered (file, ",");

  273.   fprint_field (file, "special", id.special_addr_p, id.special_addr);

  274.   if (id.artificial_depth)
  275.     fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);

  276.   fprintf_unfiltered (file, "}");
  277. }

  278. static void
  279. fprint_frame_type (struct ui_file *file, enum frame_type type)
  280. {
  281.   switch (type)
  282.     {
  283.     case NORMAL_FRAME:
  284.       fprintf_unfiltered (file, "NORMAL_FRAME");
  285.       return;
  286.     case DUMMY_FRAME:
  287.       fprintf_unfiltered (file, "DUMMY_FRAME");
  288.       return;
  289.     case INLINE_FRAME:
  290.       fprintf_unfiltered (file, "INLINE_FRAME");
  291.       return;
  292.     case TAILCALL_FRAME:
  293.       fprintf_unfiltered (file, "TAILCALL_FRAME");
  294.       return;
  295.     case SIGTRAMP_FRAME:
  296.       fprintf_unfiltered (file, "SIGTRAMP_FRAME");
  297.       return;
  298.     case ARCH_FRAME:
  299.       fprintf_unfiltered (file, "ARCH_FRAME");
  300.       return;
  301.     case SENTINEL_FRAME:
  302.       fprintf_unfiltered (file, "SENTINEL_FRAME");
  303.       return;
  304.     default:
  305.       fprintf_unfiltered (file, "<unknown type>");
  306.       return;
  307.     };
  308. }

  309. static void
  310. fprint_frame (struct ui_file *file, struct frame_info *fi)
  311. {
  312.   if (fi == NULL)
  313.     {
  314.       fprintf_unfiltered (file, "<NULL frame>");
  315.       return;
  316.     }
  317.   fprintf_unfiltered (file, "{");
  318.   fprintf_unfiltered (file, "level=%d", fi->level);
  319.   fprintf_unfiltered (file, ",");
  320.   fprintf_unfiltered (file, "type=");
  321.   if (fi->unwind != NULL)
  322.     fprint_frame_type (file, fi->unwind->type);
  323.   else
  324.     fprintf_unfiltered (file, "<unknown>");
  325.   fprintf_unfiltered (file, ",");
  326.   fprintf_unfiltered (file, "unwind=");
  327.   if (fi->unwind != NULL)
  328.     gdb_print_host_address (fi->unwind, file);
  329.   else
  330.     fprintf_unfiltered (file, "<unknown>");
  331.   fprintf_unfiltered (file, ",");
  332.   fprintf_unfiltered (file, "pc=");
  333.   if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
  334.     fprintf_unfiltered (file, "<unknown>");
  335.   else if (fi->next->prev_pc.status == CC_VALUE)
  336.     fprintf_unfiltered (file, "%s",
  337.                         hex_string (fi->next->prev_pc.value));
  338.   else if (fi->next->prev_pc.status == CC_NOT_SAVED)
  339.     val_print_not_saved (file);
  340.   else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
  341.     val_print_unavailable (file);
  342.   fprintf_unfiltered (file, ",");
  343.   fprintf_unfiltered (file, "id=");
  344.   if (fi->this_id.p)
  345.     fprint_frame_id (file, fi->this_id.value);
  346.   else
  347.     fprintf_unfiltered (file, "<unknown>");
  348.   fprintf_unfiltered (file, ",");
  349.   fprintf_unfiltered (file, "func=");
  350.   if (fi->next != NULL && fi->next->prev_func.p)
  351.     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
  352.   else
  353.     fprintf_unfiltered (file, "<unknown>");
  354.   fprintf_unfiltered (file, "}");
  355. }

  356. /* Given FRAME, return the enclosing frame as found in real frames read-in from
  357.    inferior memory.  Skip any previous frames which were made up by GDB.
  358.    Return the original frame if no immediate previous frames exist.  */

  359. static struct frame_info *
  360. skip_artificial_frames (struct frame_info *frame)
  361. {
  362.   /* Note we use get_prev_frame_always, and not get_prev_frame.  The
  363.      latter will truncate the frame chain, leading to this function
  364.      unintentionally returning a null_frame_id (e.g., when the user
  365.      sets a backtrace limit).  This is safe, because as these frames
  366.      are made up by GDB, there must be a real frame in the chain
  367.      below.  */
  368.   while (get_frame_type (frame) == INLINE_FRAME
  369.          || get_frame_type (frame) == TAILCALL_FRAME)
  370.     frame = get_prev_frame_always (frame);

  371.   return frame;
  372. }

  373. /* Compute the frame's uniq ID that can be used to, later, re-find the
  374.    frame.  */

  375. static void
  376. compute_frame_id (struct frame_info *fi)
  377. {
  378.   gdb_assert (!fi->this_id.p);

  379.   if (frame_debug)
  380.     fprintf_unfiltered (gdb_stdlog, "{ compute_frame_id (fi=%d) ",
  381.                         fi->level);
  382.   /* Find the unwinder.  */
  383.   if (fi->unwind == NULL)
  384.     frame_unwind_find_by_frame (fi, &fi->prologue_cache);
  385.   /* Find THIS frame's ID.  */
  386.   /* Default to outermost if no ID is found.  */
  387.   fi->this_id.value = outer_frame_id;
  388.   fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
  389.   gdb_assert (frame_id_p (fi->this_id.value));
  390.   fi->this_id.p = 1;
  391.   if (frame_debug)
  392.     {
  393.       fprintf_unfiltered (gdb_stdlog, "-> ");
  394.       fprint_frame_id (gdb_stdlog, fi->this_id.value);
  395.       fprintf_unfiltered (gdb_stdlog, " }\n");
  396.     }
  397. }

  398. /* Return a frame uniq ID that can be used to, later, re-find the
  399.    frame.  */

  400. struct frame_id
  401. get_frame_id (struct frame_info *fi)
  402. {
  403.   if (fi == NULL)
  404.     return null_frame_id;

  405.   gdb_assert (fi->this_id.p);
  406.   return fi->this_id.value;
  407. }

  408. struct frame_id
  409. get_stack_frame_id (struct frame_info *next_frame)
  410. {
  411.   return get_frame_id (skip_artificial_frames (next_frame));
  412. }

  413. struct frame_id
  414. frame_unwind_caller_id (struct frame_info *next_frame)
  415. {
  416.   struct frame_info *this_frame;

  417.   /* Use get_prev_frame_always, and not get_prev_frame.  The latter
  418.      will truncate the frame chain, leading to this function
  419.      unintentionally returning a null_frame_id (e.g., when a caller
  420.      requests the frame ID of "main()"s caller.  */

  421.   next_frame = skip_artificial_frames (next_frame);
  422.   this_frame = get_prev_frame_always (next_frame);
  423.   if (this_frame)
  424.     return get_frame_id (skip_artificial_frames (this_frame));
  425.   else
  426.     return null_frame_id;
  427. }

  428. const struct frame_id null_frame_id; /* All zeros.  */
  429. const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_INVALID, 0, 1, 0 };

  430. struct frame_id
  431. frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
  432.                         CORE_ADDR special_addr)
  433. {
  434.   struct frame_id id = null_frame_id;

  435.   id.stack_addr = stack_addr;
  436.   id.stack_status = FID_STACK_VALID;
  437.   id.code_addr = code_addr;
  438.   id.code_addr_p = 1;
  439.   id.special_addr = special_addr;
  440.   id.special_addr_p = 1;
  441.   return id;
  442. }

  443. /* See frame.h.  */

  444. struct frame_id
  445. frame_id_build_unavailable_stack (CORE_ADDR code_addr)
  446. {
  447.   struct frame_id id = null_frame_id;

  448.   id.stack_status = FID_STACK_UNAVAILABLE;
  449.   id.code_addr = code_addr;
  450.   id.code_addr_p = 1;
  451.   return id;
  452. }

  453. /* See frame.h.  */

  454. struct frame_id
  455. frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
  456.                                           CORE_ADDR special_addr)
  457. {
  458.   struct frame_id id = null_frame_id;

  459.   id.stack_status = FID_STACK_UNAVAILABLE;
  460.   id.code_addr = code_addr;
  461.   id.code_addr_p = 1;
  462.   id.special_addr = special_addr;
  463.   id.special_addr_p = 1;
  464.   return id;
  465. }

  466. struct frame_id
  467. frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
  468. {
  469.   struct frame_id id = null_frame_id;

  470.   id.stack_addr = stack_addr;
  471.   id.stack_status = FID_STACK_VALID;
  472.   id.code_addr = code_addr;
  473.   id.code_addr_p = 1;
  474.   return id;
  475. }

  476. struct frame_id
  477. frame_id_build_wild (CORE_ADDR stack_addr)
  478. {
  479.   struct frame_id id = null_frame_id;

  480.   id.stack_addr = stack_addr;
  481.   id.stack_status = FID_STACK_VALID;
  482.   return id;
  483. }

  484. int
  485. frame_id_p (struct frame_id l)
  486. {
  487.   int p;

  488.   /* The frame is valid iff it has a valid stack address.  */
  489.   p = l.stack_status != FID_STACK_INVALID;
  490.   /* outer_frame_id is also valid.  */
  491.   if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
  492.     p = 1;
  493.   if (frame_debug)
  494.     {
  495.       fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
  496.       fprint_frame_id (gdb_stdlog, l);
  497.       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
  498.     }
  499.   return p;
  500. }

  501. int
  502. frame_id_artificial_p (struct frame_id l)
  503. {
  504.   if (!frame_id_p (l))
  505.     return 0;

  506.   return (l.artificial_depth != 0);
  507. }

  508. int
  509. frame_id_eq (struct frame_id l, struct frame_id r)
  510. {
  511.   int eq;

  512.   if (l.stack_status == FID_STACK_INVALID && l.special_addr_p
  513.       && r.stack_status == FID_STACK_INVALID && r.special_addr_p)
  514.     /* The outermost frame marker is equal to itself.  This is the
  515.        dodgy thing about outer_frame_id, since between execution steps
  516.        we might step into another function - from which we can't
  517.        unwind either.  More thought required to get rid of
  518.        outer_frame_id.  */
  519.     eq = 1;
  520.   else if (l.stack_status == FID_STACK_INVALID
  521.            || r.stack_status == FID_STACK_INVALID)
  522.     /* Like a NaN, if either ID is invalid, the result is false.
  523.        Note that a frame ID is invalid iff it is the null frame ID.  */
  524.     eq = 0;
  525.   else if (l.stack_status != r.stack_status || l.stack_addr != r.stack_addr)
  526.     /* If .stack addresses are different, the frames are different.  */
  527.     eq = 0;
  528.   else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
  529.     /* An invalid code addr is a wild card.  If .code addresses are
  530.        different, the frames are different.  */
  531.     eq = 0;
  532.   else if (l.special_addr_p && r.special_addr_p
  533.            && l.special_addr != r.special_addr)
  534.     /* An invalid special addr is a wild card (or unused).  Otherwise
  535.        if special addresses are different, the frames are different.  */
  536.     eq = 0;
  537.   else if (l.artificial_depth != r.artificial_depth)
  538.     /* If artifical depths are different, the frames must be different.  */
  539.     eq = 0;
  540.   else
  541.     /* Frames are equal.  */
  542.     eq = 1;

  543.   if (frame_debug)
  544.     {
  545.       fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
  546.       fprint_frame_id (gdb_stdlog, l);
  547.       fprintf_unfiltered (gdb_stdlog, ",r=");
  548.       fprint_frame_id (gdb_stdlog, r);
  549.       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
  550.     }
  551.   return eq;
  552. }

  553. /* Safety net to check whether frame ID L should be inner to
  554.    frame ID R, according to their stack addresses.

  555.    This method cannot be used to compare arbitrary frames, as the
  556.    ranges of valid stack addresses may be discontiguous (e.g. due
  557.    to sigaltstack).

  558.    However, it can be used as safety net to discover invalid frame
  559.    IDs in certain circumstances.  Assuming that NEXT is the immediate
  560.    inner frame to THIS and that NEXT and THIS are both NORMAL frames:

  561.    * The stack address of NEXT must be inner-than-or-equal to the stack
  562.      address of THIS.

  563.      Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
  564.      error has occurred.

  565.    * If NEXT and THIS have different stack addresses, no other frame
  566.      in the frame chain may have a stack address in between.

  567.      Therefore, if frame_id_inner (TEST, THIS) holds, but
  568.      frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
  569.      to a valid frame in the frame chain.

  570.    The sanity checks above cannot be performed when a SIGTRAMP frame
  571.    is involved, because signal handlers might be executed on a different
  572.    stack than the stack used by the routine that caused the signal
  573.    to be raised.  This can happen for instance when a thread exceeds
  574.    its maximum stack size.  In this case, certain compilers implement
  575.    a stack overflow strategy that cause the handler to be run on a
  576.    different stack.  */

  577. static int
  578. frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
  579. {
  580.   int inner;

  581.   if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
  582.     /* Like NaN, any operation involving an invalid ID always fails.
  583.        Likewise if either ID has an unavailable stack address.  */
  584.     inner = 0;
  585.   else if (l.artificial_depth > r.artificial_depth
  586.            && l.stack_addr == r.stack_addr
  587.            && l.code_addr_p == r.code_addr_p
  588.            && l.special_addr_p == r.special_addr_p
  589.            && l.special_addr == r.special_addr)
  590.     {
  591.       /* Same function, different inlined functions.  */
  592.       const struct block *lb, *rb;

  593.       gdb_assert (l.code_addr_p && r.code_addr_p);

  594.       lb = block_for_pc (l.code_addr);
  595.       rb = block_for_pc (r.code_addr);

  596.       if (lb == NULL || rb == NULL)
  597.         /* Something's gone wrong.  */
  598.         inner = 0;
  599.       else
  600.         /* This will return true if LB and RB are the same block, or
  601.            if the block with the smaller depth lexically encloses the
  602.            block with the greater depth.  */
  603.         inner = contained_in (lb, rb);
  604.     }
  605.   else
  606.     /* Only return non-zero when strictly inner than.  Note that, per
  607.        comment in "frame.h", there is some fuzz here.  Frameless
  608.        functions are not strictly inner than (same .stack but
  609.        different .code and/or .special address).  */
  610.     inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
  611.   if (frame_debug)
  612.     {
  613.       fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
  614.       fprint_frame_id (gdb_stdlog, l);
  615.       fprintf_unfiltered (gdb_stdlog, ",r=");
  616.       fprint_frame_id (gdb_stdlog, r);
  617.       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
  618.     }
  619.   return inner;
  620. }

  621. struct frame_info *
  622. frame_find_by_id (struct frame_id id)
  623. {
  624.   struct frame_info *frame, *prev_frame;

  625.   /* ZERO denotes the null frame, let the caller decide what to do
  626.      about it.  Should it instead return get_current_frame()?  */
  627.   if (!frame_id_p (id))
  628.     return NULL;

  629.   /* Try using the frame stash first.  Finding it there removes the need
  630.      to perform the search by looping over all frames, which can be very
  631.      CPU-intensive if the number of frames is very high (the loop is O(n)
  632.      and get_prev_frame performs a series of checks that are relatively
  633.      expensive).  This optimization is particularly useful when this function
  634.      is called from another function (such as value_fetch_lazy, case
  635.      VALUE_LVAL (val) == lval_register) which already loops over all frames,
  636.      making the overall behavior O(n^2).  */
  637.   frame = frame_stash_find (id);
  638.   if (frame)
  639.     return frame;

  640.   for (frame = get_current_frame (); ; frame = prev_frame)
  641.     {
  642.       struct frame_id this = get_frame_id (frame);

  643.       if (frame_id_eq (id, this))
  644.         /* An exact match.  */
  645.         return frame;

  646.       prev_frame = get_prev_frame (frame);
  647.       if (!prev_frame)
  648.         return NULL;

  649.       /* As a safety net to avoid unnecessary backtracing while trying
  650.          to find an invalid ID, we check for a common situation where
  651.          we can detect from comparing stack addresses that no other
  652.          frame in the current frame chain can have this ID.  See the
  653.          comment at frame_id_inner for details.   */
  654.       if (get_frame_type (frame) == NORMAL_FRAME
  655.           && !frame_id_inner (get_frame_arch (frame), id, this)
  656.           && frame_id_inner (get_frame_arch (prev_frame), id,
  657.                              get_frame_id (prev_frame)))
  658.         return NULL;
  659.     }
  660.   return NULL;
  661. }

  662. static CORE_ADDR
  663. frame_unwind_pc (struct frame_info *this_frame)
  664. {
  665.   if (this_frame->prev_pc.status == CC_UNKNOWN)
  666.     {
  667.       if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
  668.         {
  669.           volatile struct gdb_exception ex;
  670.           struct gdbarch *prev_gdbarch;
  671.           CORE_ADDR pc = 0;

  672.           /* The right way.  The `pure' way.  The one true way.  This
  673.              method depends solely on the register-unwind code to
  674.              determine the value of registers in THIS frame, and hence
  675.              the value of this frame's PC (resume address).  A typical
  676.              implementation is no more than:

  677.              frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
  678.              return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);

  679.              Note: this method is very heavily dependent on a correct
  680.              register-unwind implementation, it pays to fix that
  681.              method first; this method is frame type agnostic, since
  682.              it only deals with register values, it works with any
  683.              frame.  This is all in stark contrast to the old
  684.              FRAME_SAVED_PC which would try to directly handle all the
  685.              different ways that a PC could be unwound.  */
  686.           prev_gdbarch = frame_unwind_arch (this_frame);

  687.           TRY_CATCH (ex, RETURN_MASK_ERROR)
  688.             {
  689.               pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
  690.             }
  691.           if (ex.reason < 0)
  692.             {
  693.               if (ex.error == NOT_AVAILABLE_ERROR)
  694.                 {
  695.                   this_frame->prev_pc.status = CC_UNAVAILABLE;

  696.                   if (frame_debug)
  697.                     fprintf_unfiltered (gdb_stdlog,
  698.                                         "{ frame_unwind_pc (this_frame=%d)"
  699.                                         " -> <unavailable> }\n",
  700.                                         this_frame->level);
  701.                 }
  702.               else if (ex.error == OPTIMIZED_OUT_ERROR)
  703.                 {
  704.                   this_frame->prev_pc.status = CC_NOT_SAVED;

  705.                   if (frame_debug)
  706.                     fprintf_unfiltered (gdb_stdlog,
  707.                                         "{ frame_unwind_pc (this_frame=%d)"
  708.                                         " -> <not saved> }\n",
  709.                                         this_frame->level);
  710.                 }
  711.               else
  712.                 throw_exception (ex);
  713.             }
  714.           else
  715.             {
  716.               this_frame->prev_pc.value = pc;
  717.               this_frame->prev_pc.status = CC_VALUE;
  718.               if (frame_debug)
  719.                 fprintf_unfiltered (gdb_stdlog,
  720.                                     "{ frame_unwind_pc (this_frame=%d) "
  721.                                     "-> %s }\n",
  722.                                     this_frame->level,
  723.                                     hex_string (this_frame->prev_pc.value));
  724.             }
  725.         }
  726.       else
  727.         internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
  728.     }

  729.   if (this_frame->prev_pc.status == CC_VALUE)
  730.     return this_frame->prev_pc.value;
  731.   else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
  732.     throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
  733.   else if (this_frame->prev_pc.status == CC_NOT_SAVED)
  734.     throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
  735.   else
  736.     internal_error (__FILE__, __LINE__,
  737.                     "unexpected prev_pc status: %d",
  738.                     (int) this_frame->prev_pc.status);
  739. }

  740. CORE_ADDR
  741. frame_unwind_caller_pc (struct frame_info *this_frame)
  742. {
  743.   return frame_unwind_pc (skip_artificial_frames (this_frame));
  744. }

  745. int
  746. get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
  747. {
  748.   struct frame_info *next_frame = this_frame->next;

  749.   if (!next_frame->prev_func.p)
  750.     {
  751.       CORE_ADDR addr_in_block;

  752.       /* Make certain that this, and not the adjacent, function is
  753.          found.  */
  754.       if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
  755.         {
  756.           next_frame->prev_func.p = -1;
  757.           if (frame_debug)
  758.             fprintf_unfiltered (gdb_stdlog,
  759.                                 "{ get_frame_func (this_frame=%d)"
  760.                                 " -> unavailable }\n",
  761.                                 this_frame->level);
  762.         }
  763.       else
  764.         {
  765.           next_frame->prev_func.p = 1;
  766.           next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
  767.           if (frame_debug)
  768.             fprintf_unfiltered (gdb_stdlog,
  769.                                 "{ get_frame_func (this_frame=%d) -> %s }\n",
  770.                                 this_frame->level,
  771.                                 hex_string (next_frame->prev_func.addr));
  772.         }
  773.     }

  774.   if (next_frame->prev_func.p < 0)
  775.     {
  776.       *pc = -1;
  777.       return 0;
  778.     }
  779.   else
  780.     {
  781.       *pc = next_frame->prev_func.addr;
  782.       return 1;
  783.     }
  784. }

  785. CORE_ADDR
  786. get_frame_func (struct frame_info *this_frame)
  787. {
  788.   CORE_ADDR pc;

  789.   if (!get_frame_func_if_available (this_frame, &pc))
  790.     throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));

  791.   return pc;
  792. }

  793. static enum register_status
  794. do_frame_register_read (void *src, int regnum, gdb_byte *buf)
  795. {
  796.   if (!deprecated_frame_register_read (src, regnum, buf))
  797.     return REG_UNAVAILABLE;
  798.   else
  799.     return REG_VALID;
  800. }

  801. struct regcache *
  802. frame_save_as_regcache (struct frame_info *this_frame)
  803. {
  804.   struct address_space *aspace = get_frame_address_space (this_frame);
  805.   struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame),
  806.                                                 aspace);
  807.   struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);

  808.   regcache_save (regcache, do_frame_register_read, this_frame);
  809.   discard_cleanups (cleanups);
  810.   return regcache;
  811. }

  812. void
  813. frame_pop (struct frame_info *this_frame)
  814. {
  815.   struct frame_info *prev_frame;
  816.   struct regcache *scratch;
  817.   struct cleanup *cleanups;

  818.   if (get_frame_type (this_frame) == DUMMY_FRAME)
  819.     {
  820.       /* Popping a dummy frame involves restoring more than just registers.
  821.          dummy_frame_pop does all the work.  */
  822.       dummy_frame_pop (get_frame_id (this_frame), inferior_ptid);
  823.       return;
  824.     }

  825.   /* Ensure that we have a frame to pop to.  */
  826.   prev_frame = get_prev_frame_always (this_frame);

  827.   if (!prev_frame)
  828.     error (_("Cannot pop the initial frame."));

  829.   /* Ignore TAILCALL_FRAME type frames, they were executed already before
  830.      entering THISFRAME.  */
  831.   while (get_frame_type (prev_frame) == TAILCALL_FRAME)
  832.     prev_frame = get_prev_frame (prev_frame);

  833.   /* Make a copy of all the register values unwound from this frame.
  834.      Save them in a scratch buffer so that there isn't a race between
  835.      trying to extract the old values from the current regcache while
  836.      at the same time writing new values into that same cache.  */
  837.   scratch = frame_save_as_regcache (prev_frame);
  838.   cleanups = make_cleanup_regcache_xfree (scratch);

  839.   /* FIXME: cagney/2003-03-16: It should be possible to tell the
  840.      target's register cache that it is about to be hit with a burst
  841.      register transfer and that the sequence of register writes should
  842.      be batched.  The pair target_prepare_to_store() and
  843.      target_store_registers() kind of suggest this functionality.
  844.      Unfortunately, they don't implement it.  Their lack of a formal
  845.      definition can lead to targets writing back bogus values
  846.      (arguably a bug in the target code mind).  */
  847.   /* Now copy those saved registers into the current regcache.
  848.      Here, regcache_cpy() calls regcache_restore().  */
  849.   regcache_cpy (get_current_regcache (), scratch);
  850.   do_cleanups (cleanups);

  851.   /* We've made right mess of GDB's local state, just discard
  852.      everything.  */
  853.   reinit_frame_cache ();
  854. }

  855. void
  856. frame_register_unwind (struct frame_info *frame, int regnum,
  857.                        int *optimizedp, int *unavailablep,
  858.                        enum lval_type *lvalp, CORE_ADDR *addrp,
  859.                        int *realnump, gdb_byte *bufferp)
  860. {
  861.   struct value *value;

  862.   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
  863.      that the value proper does not need to be fetched.  */
  864.   gdb_assert (optimizedp != NULL);
  865.   gdb_assert (lvalp != NULL);
  866.   gdb_assert (addrp != NULL);
  867.   gdb_assert (realnump != NULL);
  868.   /* gdb_assert (bufferp != NULL); */

  869.   value = frame_unwind_register_value (frame, regnum);

  870.   gdb_assert (value != NULL);

  871.   *optimizedp = value_optimized_out (value);
  872.   *unavailablep = !value_entirely_available (value);
  873.   *lvalp = VALUE_LVAL (value);
  874.   *addrp = value_address (value);
  875.   *realnump = VALUE_REGNUM (value);

  876.   if (bufferp)
  877.     {
  878.       if (!*optimizedp && !*unavailablep)
  879.         memcpy (bufferp, value_contents_all (value),
  880.                 TYPE_LENGTH (value_type (value)));
  881.       else
  882.         memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
  883.     }

  884.   /* Dispose of the new value.  This prevents watchpoints from
  885.      trying to watch the saved frame pointer.  */
  886.   release_value (value);
  887.   value_free (value);
  888. }

  889. void
  890. frame_register (struct frame_info *frame, int regnum,
  891.                 int *optimizedp, int *unavailablep, enum lval_type *lvalp,
  892.                 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
  893. {
  894.   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
  895.      that the value proper does not need to be fetched.  */
  896.   gdb_assert (optimizedp != NULL);
  897.   gdb_assert (lvalp != NULL);
  898.   gdb_assert (addrp != NULL);
  899.   gdb_assert (realnump != NULL);
  900.   /* gdb_assert (bufferp != NULL); */

  901.   /* Obtain the register value by unwinding the register from the next
  902.      (more inner frame).  */
  903.   gdb_assert (frame != NULL && frame->next != NULL);
  904.   frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
  905.                          lvalp, addrp, realnump, bufferp);
  906. }

  907. void
  908. frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
  909. {
  910.   int optimized;
  911.   int unavailable;
  912.   CORE_ADDR addr;
  913.   int realnum;
  914.   enum lval_type lval;

  915.   frame_register_unwind (frame, regnum, &optimized, &unavailable,
  916.                          &lval, &addr, &realnum, buf);

  917.   if (optimized)
  918.     throw_error (OPTIMIZED_OUT_ERROR,
  919.                  _("Register %d was not saved"), regnum);
  920.   if (unavailable)
  921.     throw_error (NOT_AVAILABLE_ERROR,
  922.                  _("Register %d is not available"), regnum);
  923. }

  924. void
  925. get_frame_register (struct frame_info *frame,
  926.                     int regnum, gdb_byte *buf)
  927. {
  928.   frame_unwind_register (frame->next, regnum, buf);
  929. }

  930. struct value *
  931. frame_unwind_register_value (struct frame_info *frame, int regnum)
  932. {
  933.   struct gdbarch *gdbarch;
  934.   struct value *value;

  935.   gdb_assert (frame != NULL);
  936.   gdbarch = frame_unwind_arch (frame);

  937.   if (frame_debug)
  938.     {
  939.       fprintf_unfiltered (gdb_stdlog,
  940.                           "{ frame_unwind_register_value "
  941.                           "(frame=%d,regnum=%d(%s),...) ",
  942.                           frame->level, regnum,
  943.                           user_reg_map_regnum_to_name (gdbarch, regnum));
  944.     }

  945.   /* Find the unwinder.  */
  946.   if (frame->unwind == NULL)
  947.     frame_unwind_find_by_frame (frame, &frame->prologue_cache);

  948.   /* Ask this frame to unwind its register.  */
  949.   value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);

  950.   if (frame_debug)
  951.     {
  952.       fprintf_unfiltered (gdb_stdlog, "->");
  953.       if (value_optimized_out (value))
  954.         {
  955.           fprintf_unfiltered (gdb_stdlog, " ");
  956.           val_print_optimized_out (value, gdb_stdlog);
  957.         }
  958.       else
  959.         {
  960.           if (VALUE_LVAL (value) == lval_register)
  961.             fprintf_unfiltered (gdb_stdlog, " register=%d",
  962.                                 VALUE_REGNUM (value));
  963.           else if (VALUE_LVAL (value) == lval_memory)
  964.             fprintf_unfiltered (gdb_stdlog, " address=%s",
  965.                                 paddress (gdbarch,
  966.                                           value_address (value)));
  967.           else
  968.             fprintf_unfiltered (gdb_stdlog, " computed");

  969.           if (value_lazy (value))
  970.             fprintf_unfiltered (gdb_stdlog, " lazy");
  971.           else
  972.             {
  973.               int i;
  974.               const gdb_byte *buf = value_contents (value);

  975.               fprintf_unfiltered (gdb_stdlog, " bytes=");
  976.               fprintf_unfiltered (gdb_stdlog, "[");
  977.               for (i = 0; i < register_size (gdbarch, regnum); i++)
  978.                 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
  979.               fprintf_unfiltered (gdb_stdlog, "]");
  980.             }
  981.         }

  982.       fprintf_unfiltered (gdb_stdlog, " }\n");
  983.     }

  984.   return value;
  985. }

  986. struct value *
  987. get_frame_register_value (struct frame_info *frame, int regnum)
  988. {
  989.   return frame_unwind_register_value (frame->next, regnum);
  990. }

  991. LONGEST
  992. frame_unwind_register_signed (struct frame_info *frame, int regnum)
  993. {
  994.   struct gdbarch *gdbarch = frame_unwind_arch (frame);
  995.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  996.   int size = register_size (gdbarch, regnum);
  997.   gdb_byte buf[MAX_REGISTER_SIZE];

  998.   frame_unwind_register (frame, regnum, buf);
  999.   return extract_signed_integer (buf, size, byte_order);
  1000. }

  1001. LONGEST
  1002. get_frame_register_signed (struct frame_info *frame, int regnum)
  1003. {
  1004.   return frame_unwind_register_signed (frame->next, regnum);
  1005. }

  1006. ULONGEST
  1007. frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
  1008. {
  1009.   struct gdbarch *gdbarch = frame_unwind_arch (frame);
  1010.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1011.   int size = register_size (gdbarch, regnum);
  1012.   gdb_byte buf[MAX_REGISTER_SIZE];

  1013.   frame_unwind_register (frame, regnum, buf);
  1014.   return extract_unsigned_integer (buf, size, byte_order);
  1015. }

  1016. ULONGEST
  1017. get_frame_register_unsigned (struct frame_info *frame, int regnum)
  1018. {
  1019.   return frame_unwind_register_unsigned (frame->next, regnum);
  1020. }

  1021. int
  1022. read_frame_register_unsigned (struct frame_info *frame, int regnum,
  1023.                               ULONGEST *val)
  1024. {
  1025.   struct value *regval = get_frame_register_value (frame, regnum);

  1026.   if (!value_optimized_out (regval)
  1027.       && value_entirely_available (regval))
  1028.     {
  1029.       struct gdbarch *gdbarch = get_frame_arch (frame);
  1030.       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1031.       int size = register_size (gdbarch, VALUE_REGNUM (regval));

  1032.       *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
  1033.       return 1;
  1034.     }

  1035.   return 0;
  1036. }

  1037. void
  1038. put_frame_register (struct frame_info *frame, int regnum,
  1039.                     const gdb_byte *buf)
  1040. {
  1041.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1042.   int realnum;
  1043.   int optim;
  1044.   int unavail;
  1045.   enum lval_type lval;
  1046.   CORE_ADDR addr;

  1047.   frame_register (frame, regnum, &optim, &unavail,
  1048.                   &lval, &addr, &realnum, NULL);
  1049.   if (optim)
  1050.     error (_("Attempt to assign to a register that was not saved."));
  1051.   switch (lval)
  1052.     {
  1053.     case lval_memory:
  1054.       {
  1055.         write_memory (addr, buf, register_size (gdbarch, regnum));
  1056.         break;
  1057.       }
  1058.     case lval_register:
  1059.       regcache_cooked_write (get_current_regcache (), realnum, buf);
  1060.       break;
  1061.     default:
  1062.       error (_("Attempt to assign to an unmodifiable value."));
  1063.     }
  1064. }

  1065. /* This function is deprecated.  Use get_frame_register_value instead,
  1066.    which provides more accurate information.

  1067.    Find and return the value of REGNUM for the specified stack frame.
  1068.    The number of bytes copied is REGISTER_SIZE (REGNUM).

  1069.    Returns 0 if the register value could not be found.  */

  1070. int
  1071. deprecated_frame_register_read (struct frame_info *frame, int regnum,
  1072.                      gdb_byte *myaddr)
  1073. {
  1074.   int optimized;
  1075.   int unavailable;
  1076.   enum lval_type lval;
  1077.   CORE_ADDR addr;
  1078.   int realnum;

  1079.   frame_register (frame, regnum, &optimized, &unavailable,
  1080.                   &lval, &addr, &realnum, myaddr);

  1081.   return !optimized && !unavailable;
  1082. }

  1083. int
  1084. get_frame_register_bytes (struct frame_info *frame, int regnum,
  1085.                           CORE_ADDR offset, int len, gdb_byte *myaddr,
  1086.                           int *optimizedp, int *unavailablep)
  1087. {
  1088.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1089.   int i;
  1090.   int maxsize;
  1091.   int numregs;

  1092.   /* Skip registers wholly inside of OFFSET.  */
  1093.   while (offset >= register_size (gdbarch, regnum))
  1094.     {
  1095.       offset -= register_size (gdbarch, regnum);
  1096.       regnum++;
  1097.     }

  1098.   /* Ensure that we will not read beyond the end of the register file.
  1099.      This can only ever happen if the debug information is bad.  */
  1100.   maxsize = -offset;
  1101.   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
  1102.   for (i = regnum; i < numregs; i++)
  1103.     {
  1104.       int thissize = register_size (gdbarch, i);

  1105.       if (thissize == 0)
  1106.         break;        /* This register is not available on this architecture.  */
  1107.       maxsize += thissize;
  1108.     }
  1109.   if (len > maxsize)
  1110.     error (_("Bad debug information detected: "
  1111.              "Attempt to read %d bytes from registers."), len);

  1112.   /* Copy the data.  */
  1113.   while (len > 0)
  1114.     {
  1115.       int curr_len = register_size (gdbarch, regnum) - offset;

  1116.       if (curr_len > len)
  1117.         curr_len = len;

  1118.       if (curr_len == register_size (gdbarch, regnum))
  1119.         {
  1120.           enum lval_type lval;
  1121.           CORE_ADDR addr;
  1122.           int realnum;

  1123.           frame_register (frame, regnum, optimizedp, unavailablep,
  1124.                           &lval, &addr, &realnum, myaddr);
  1125.           if (*optimizedp || *unavailablep)
  1126.             return 0;
  1127.         }
  1128.       else
  1129.         {
  1130.           gdb_byte buf[MAX_REGISTER_SIZE];
  1131.           enum lval_type lval;
  1132.           CORE_ADDR addr;
  1133.           int realnum;

  1134.           frame_register (frame, regnum, optimizedp, unavailablep,
  1135.                           &lval, &addr, &realnum, buf);
  1136.           if (*optimizedp || *unavailablep)
  1137.             return 0;
  1138.           memcpy (myaddr, buf + offset, curr_len);
  1139.         }

  1140.       myaddr += curr_len;
  1141.       len -= curr_len;
  1142.       offset = 0;
  1143.       regnum++;
  1144.     }

  1145.   *optimizedp = 0;
  1146.   *unavailablep = 0;
  1147.   return 1;
  1148. }

  1149. void
  1150. put_frame_register_bytes (struct frame_info *frame, int regnum,
  1151.                           CORE_ADDR offset, int len, const gdb_byte *myaddr)
  1152. {
  1153.   struct gdbarch *gdbarch = get_frame_arch (frame);

  1154.   /* Skip registers wholly inside of OFFSET.  */
  1155.   while (offset >= register_size (gdbarch, regnum))
  1156.     {
  1157.       offset -= register_size (gdbarch, regnum);
  1158.       regnum++;
  1159.     }

  1160.   /* Copy the data.  */
  1161.   while (len > 0)
  1162.     {
  1163.       int curr_len = register_size (gdbarch, regnum) - offset;

  1164.       if (curr_len > len)
  1165.         curr_len = len;

  1166.       if (curr_len == register_size (gdbarch, regnum))
  1167.         {
  1168.           put_frame_register (frame, regnum, myaddr);
  1169.         }
  1170.       else
  1171.         {
  1172.           gdb_byte buf[MAX_REGISTER_SIZE];

  1173.           deprecated_frame_register_read (frame, regnum, buf);
  1174.           memcpy (buf + offset, myaddr, curr_len);
  1175.           put_frame_register (frame, regnum, buf);
  1176.         }

  1177.       myaddr += curr_len;
  1178.       len -= curr_len;
  1179.       offset = 0;
  1180.       regnum++;
  1181.     }
  1182. }

  1183. /* Create a sentinel frame.  */

  1184. static struct frame_info *
  1185. create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
  1186. {
  1187.   struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);

  1188.   frame->level = -1;
  1189.   frame->pspace = pspace;
  1190.   frame->aspace = get_regcache_aspace (regcache);
  1191.   /* Explicitly initialize the sentinel frame's cache.  Provide it
  1192.      with the underlying regcache.  In the future additional
  1193.      information, such as the frame's thread will be added.  */
  1194.   frame->prologue_cache = sentinel_frame_cache (regcache);
  1195.   /* For the moment there is only one sentinel frame implementation.  */
  1196.   frame->unwind = &sentinel_frame_unwind;
  1197.   /* Link this frame back to itself.  The frame is self referential
  1198.      (the unwound PC is the same as the pc), so make it so.  */
  1199.   frame->next = frame;
  1200.   /* Make the sentinel frame's ID valid, but invalid.  That way all
  1201.      comparisons with it should fail.  */
  1202.   frame->this_id.p = 1;
  1203.   frame->this_id.value = null_frame_id;
  1204.   if (frame_debug)
  1205.     {
  1206.       fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
  1207.       fprint_frame (gdb_stdlog, frame);
  1208.       fprintf_unfiltered (gdb_stdlog, " }\n");
  1209.     }
  1210.   return frame;
  1211. }

  1212. /* Info about the innermost stack frame (contents of FP register).  */

  1213. static struct frame_info *current_frame;

  1214. /* Cache for frame addresses already read by gdb.  Valid only while
  1215.    inferior is stopped.  Control variables for the frame cache should
  1216.    be local to this module.  */

  1217. static struct obstack frame_cache_obstack;

  1218. void *
  1219. frame_obstack_zalloc (unsigned long size)
  1220. {
  1221.   void *data = obstack_alloc (&frame_cache_obstack, size);

  1222.   memset (data, 0, size);
  1223.   return data;
  1224. }

  1225. /* Return the innermost (currently executing) stack frame.  This is
  1226.    split into two functions.  The function unwind_to_current_frame()
  1227.    is wrapped in catch exceptions so that, even when the unwind of the
  1228.    sentinel frame fails, the function still returns a stack frame.  */

  1229. static int
  1230. unwind_to_current_frame (struct ui_out *ui_out, void *args)
  1231. {
  1232.   struct frame_info *frame = get_prev_frame (args);

  1233.   /* A sentinel frame can fail to unwind, e.g., because its PC value
  1234.      lands in somewhere like start.  */
  1235.   if (frame == NULL)
  1236.     return 1;
  1237.   current_frame = frame;
  1238.   return 0;
  1239. }

  1240. struct frame_info *
  1241. get_current_frame (void)
  1242. {
  1243.   /* First check, and report, the lack of registers.  Having GDB
  1244.      report "No stack!" or "No memory" when the target doesn't even
  1245.      have registers is very confusing.  Besides, "printcmd.exp"
  1246.      explicitly checks that ``print $pc'' with no registers prints "No
  1247.      registers".  */
  1248.   if (!target_has_registers)
  1249.     error (_("No registers."));
  1250.   if (!target_has_stack)
  1251.     error (_("No stack."));
  1252.   if (!target_has_memory)
  1253.     error (_("No memory."));
  1254.   /* Traceframes are effectively a substitute for the live inferior.  */
  1255.   if (get_traceframe_number () < 0)
  1256.     {
  1257.       if (ptid_equal (inferior_ptid, null_ptid))
  1258.         error (_("No selected thread."));
  1259.       if (is_exited (inferior_ptid))
  1260.         error (_("Invalid selected thread."));
  1261.       if (is_executing (inferior_ptid))
  1262.         error (_("Target is executing."));
  1263.     }

  1264.   if (current_frame == NULL)
  1265.     {
  1266.       struct frame_info *sentinel_frame =
  1267.         create_sentinel_frame (current_program_space, get_current_regcache ());
  1268.       if (catch_exceptions (current_uiout, unwind_to_current_frame,
  1269.                             sentinel_frame, RETURN_MASK_ERROR) != 0)
  1270.         {
  1271.           /* Oops! Fake a current frame?  Is this useful?  It has a PC
  1272.              of zero, for instance.  */
  1273.           current_frame = sentinel_frame;
  1274.         }
  1275.     }
  1276.   return current_frame;
  1277. }

  1278. /* The "selected" stack frame is used by default for local and arg
  1279.    access.  May be zero, for no selected frame.  */

  1280. static struct frame_info *selected_frame;

  1281. int
  1282. has_stack_frames (void)
  1283. {
  1284.   if (!target_has_registers || !target_has_stack || !target_has_memory)
  1285.     return 0;

  1286.   /* Traceframes are effectively a substitute for the live inferior.  */
  1287.   if (get_traceframe_number () < 0)
  1288.     {
  1289.       /* No current inferior, no frame.  */
  1290.       if (ptid_equal (inferior_ptid, null_ptid))
  1291.         return 0;

  1292.       /* Don't try to read from a dead thread.  */
  1293.       if (is_exited (inferior_ptid))
  1294.         return 0;

  1295.       /* ... or from a spinning thread.  */
  1296.       if (is_executing (inferior_ptid))
  1297.         return 0;
  1298.     }

  1299.   return 1;
  1300. }

  1301. /* Return the selected frame.  Always non-NULL (unless there isn't an
  1302.    inferior sufficient for creating a frame) in which case an error is
  1303.    thrown.  */

  1304. struct frame_info *
  1305. get_selected_frame (const char *message)
  1306. {
  1307.   if (selected_frame == NULL)
  1308.     {
  1309.       if (message != NULL && !has_stack_frames ())
  1310.         error (("%s"), message);
  1311.       /* Hey!  Don't trust this.  It should really be re-finding the
  1312.          last selected frame of the currently selected thread.  This,
  1313.          though, is better than nothing.  */
  1314.       select_frame (get_current_frame ());
  1315.     }
  1316.   /* There is always a frame.  */
  1317.   gdb_assert (selected_frame != NULL);
  1318.   return selected_frame;
  1319. }

  1320. /* If there is a selected frame, return it.  Otherwise, return NULL.  */

  1321. struct frame_info *
  1322. get_selected_frame_if_set (void)
  1323. {
  1324.   return selected_frame;
  1325. }

  1326. /* This is a variant of get_selected_frame() which can be called when
  1327.    the inferior does not have a frame; in that case it will return
  1328.    NULL instead of calling error().  */

  1329. struct frame_info *
  1330. deprecated_safe_get_selected_frame (void)
  1331. {
  1332.   if (!has_stack_frames ())
  1333.     return NULL;
  1334.   return get_selected_frame (NULL);
  1335. }

  1336. /* Select frame FI (or NULL - to invalidate the current frame).  */

  1337. void
  1338. select_frame (struct frame_info *fi)
  1339. {
  1340.   selected_frame = fi;
  1341.   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
  1342.      frame is being invalidated.  */
  1343.   if (deprecated_selected_frame_level_changed_hook)
  1344.     deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));

  1345.   /* FIXME: kseitz/2002-08-28: It would be nice to call
  1346.      selected_frame_level_changed_event() right here, but due to limitations
  1347.      in the current interfaces, we would end up flooding UIs with events
  1348.      because select_frame() is used extensively internally.

  1349.      Once we have frame-parameterized frame (and frame-related) commands,
  1350.      the event notification can be moved here, since this function will only
  1351.      be called when the user's selected frame is being changed.  */

  1352.   /* Ensure that symbols for this frame are read in.  Also, determine the
  1353.      source language of this frame, and switch to it if desired.  */
  1354.   if (fi)
  1355.     {
  1356.       CORE_ADDR pc;

  1357.       /* We retrieve the frame's symtab by using the frame PC.
  1358.          However we cannot use the frame PC as-is, because it usually
  1359.          points to the instruction following the "call", which is
  1360.          sometimes the first instruction of another function.  So we
  1361.          rely on get_frame_address_in_block() which provides us with a
  1362.          PC which is guaranteed to be inside the frame's code
  1363.          block.  */
  1364.       if (get_frame_address_in_block_if_available (fi, &pc))
  1365.         {
  1366.           struct compunit_symtab *cust = find_pc_compunit_symtab (pc);

  1367.           if (cust != NULL
  1368.               && compunit_language (cust) != current_language->la_language
  1369.               && compunit_language (cust) != language_unknown
  1370.               && language_mode == language_mode_auto)
  1371.             set_language (compunit_language (cust));
  1372.         }
  1373.     }
  1374. }

  1375. /* Create an arbitrary (i.e. address specified by user) or innermost frame.
  1376.    Always returns a non-NULL value.  */

  1377. struct frame_info *
  1378. create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
  1379. {
  1380.   struct frame_info *fi;

  1381.   if (frame_debug)
  1382.     {
  1383.       fprintf_unfiltered (gdb_stdlog,
  1384.                           "{ create_new_frame (addr=%s, pc=%s) ",
  1385.                           hex_string (addr), hex_string (pc));
  1386.     }

  1387.   fi = FRAME_OBSTACK_ZALLOC (struct frame_info);

  1388.   fi->next = create_sentinel_frame (current_program_space,
  1389.                                     get_current_regcache ());

  1390.   /* Set/update this frame's cached PC value, found in the next frame.
  1391.      Do this before looking for this frame's unwinder.  A sniffer is
  1392.      very likely to read this, and the corresponding unwinder is
  1393.      entitled to rely that the PC doesn't magically change.  */
  1394.   fi->next->prev_pc.value = pc;
  1395.   fi->next->prev_pc.status = CC_VALUE;

  1396.   /* We currently assume that frame chain's can't cross spaces.  */
  1397.   fi->pspace = fi->next->pspace;
  1398.   fi->aspace = fi->next->aspace;

  1399.   /* Select/initialize both the unwind function and the frame's type
  1400.      based on the PC.  */
  1401.   frame_unwind_find_by_frame (fi, &fi->prologue_cache);

  1402.   fi->this_id.p = 1;
  1403.   fi->this_id.value = frame_id_build (addr, pc);

  1404.   if (frame_debug)
  1405.     {
  1406.       fprintf_unfiltered (gdb_stdlog, "-> ");
  1407.       fprint_frame (gdb_stdlog, fi);
  1408.       fprintf_unfiltered (gdb_stdlog, " }\n");
  1409.     }

  1410.   return fi;
  1411. }

  1412. /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
  1413.    innermost frame).  Be careful to not fall off the bottom of the
  1414.    frame chain and onto the sentinel frame.  */

  1415. struct frame_info *
  1416. get_next_frame (struct frame_info *this_frame)
  1417. {
  1418.   if (this_frame->level > 0)
  1419.     return this_frame->next;
  1420.   else
  1421.     return NULL;
  1422. }

  1423. /* Observer for the target_changed event.  */

  1424. static void
  1425. frame_observer_target_changed (struct target_ops *target)
  1426. {
  1427.   reinit_frame_cache ();
  1428. }

  1429. /* Flush the entire frame cache.  */

  1430. void
  1431. reinit_frame_cache (void)
  1432. {
  1433.   struct frame_info *fi;

  1434.   /* Tear down all frame caches.  */
  1435.   for (fi = current_frame; fi != NULL; fi = fi->prev)
  1436.     {
  1437.       if (fi->prologue_cache && fi->unwind->dealloc_cache)
  1438.         fi->unwind->dealloc_cache (fi, fi->prologue_cache);
  1439.       if (fi->base_cache && fi->base->unwind->dealloc_cache)
  1440.         fi->base->unwind->dealloc_cache (fi, fi->base_cache);
  1441.     }

  1442.   /* Since we can't really be sure what the first object allocated was.  */
  1443.   obstack_free (&frame_cache_obstack, 0);
  1444.   obstack_init (&frame_cache_obstack);

  1445.   if (current_frame != NULL)
  1446.     annotate_frames_invalid ();

  1447.   current_frame = NULL;                /* Invalidate cache */
  1448.   select_frame (NULL);
  1449.   frame_stash_invalidate ();
  1450.   if (frame_debug)
  1451.     fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
  1452. }

  1453. /* Find where a register is saved (in memory or another register).
  1454.    The result of frame_register_unwind is just where it is saved
  1455.    relative to this particular frame.  */

  1456. static void
  1457. frame_register_unwind_location (struct frame_info *this_frame, int regnum,
  1458.                                 int *optimizedp, enum lval_type *lvalp,
  1459.                                 CORE_ADDR *addrp, int *realnump)
  1460. {
  1461.   gdb_assert (this_frame == NULL || this_frame->level >= 0);

  1462.   while (this_frame != NULL)
  1463.     {
  1464.       int unavailable;

  1465.       frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
  1466.                              lvalp, addrp, realnump, NULL);

  1467.       if (*optimizedp)
  1468.         break;

  1469.       if (*lvalp != lval_register)
  1470.         break;

  1471.       regnum = *realnump;
  1472.       this_frame = get_next_frame (this_frame);
  1473.     }
  1474. }

  1475. /* Called during frame unwinding to remove a previous frame pointer from a
  1476.    frame passed in ARG.  */

  1477. static void
  1478. remove_prev_frame (void *arg)
  1479. {
  1480.   struct frame_info *this_frame, *prev_frame;

  1481.   this_frame = (struct frame_info *) arg;
  1482.   prev_frame = this_frame->prev;
  1483.   gdb_assert (prev_frame != NULL);

  1484.   prev_frame->next = NULL;
  1485.   this_frame->prev = NULL;
  1486. }

  1487. /* Get the previous raw frame, and check that it is not identical to
  1488.    same other frame frame already in the chain.  If it is, there is
  1489.    most likely a stack cycle, so we discard it, and mark THIS_FRAME as
  1490.    outermost, with UNWIND_SAME_ID stop reason.  Unlike the other
  1491.    validity tests, that compare THIS_FRAME and the next frame, we do
  1492.    this right after creating the previous frame, to avoid ever ending
  1493.    up with two frames with the same id in the frame chain.  */

  1494. static struct frame_info *
  1495. get_prev_frame_if_no_cycle (struct frame_info *this_frame)
  1496. {
  1497.   struct frame_info *prev_frame;
  1498.   struct cleanup *prev_frame_cleanup;

  1499.   prev_frame = get_prev_frame_raw (this_frame);
  1500.   if (prev_frame == NULL)
  1501.     return NULL;

  1502.   /* The cleanup will remove the previous frame that get_prev_frame_raw
  1503.      linked onto THIS_FRAME.  */
  1504.   prev_frame_cleanup = make_cleanup (remove_prev_frame, this_frame);

  1505.   compute_frame_id (prev_frame);
  1506.   if (!frame_stash_add (prev_frame))
  1507.     {
  1508.       /* Another frame with the same id was already in the stash.  We just
  1509.          detected a cycle.  */
  1510.       if (frame_debug)
  1511.         {
  1512.           fprintf_unfiltered (gdb_stdlog, "-> ");
  1513.           fprint_frame (gdb_stdlog, NULL);
  1514.           fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
  1515.         }
  1516.       this_frame->stop_reason = UNWIND_SAME_ID;
  1517.       /* Unlink.  */
  1518.       prev_frame->next = NULL;
  1519.       this_frame->prev = NULL;
  1520.       prev_frame = NULL;
  1521.     }

  1522.   discard_cleanups (prev_frame_cleanup);
  1523.   return prev_frame;
  1524. }

  1525. /* Helper function for get_prev_frame_always, this is called inside a
  1526.    TRY_CATCH block.  Return the frame that called THIS_FRAME or NULL if
  1527.    there is no such frame.  This may throw an exception.  */

  1528. static struct frame_info *
  1529. get_prev_frame_always_1 (struct frame_info *this_frame)
  1530. {
  1531.   struct gdbarch *gdbarch;

  1532.   gdb_assert (this_frame != NULL);
  1533.   gdbarch = get_frame_arch (this_frame);

  1534.   if (frame_debug)
  1535.     {
  1536.       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_always (this_frame=");
  1537.       if (this_frame != NULL)
  1538.         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
  1539.       else
  1540.         fprintf_unfiltered (gdb_stdlog, "<NULL>");
  1541.       fprintf_unfiltered (gdb_stdlog, ") ");
  1542.     }

  1543.   /* Only try to do the unwind once.  */
  1544.   if (this_frame->prev_p)
  1545.     {
  1546.       if (frame_debug)
  1547.         {
  1548.           fprintf_unfiltered (gdb_stdlog, "-> ");
  1549.           fprint_frame (gdb_stdlog, this_frame->prev);
  1550.           fprintf_unfiltered (gdb_stdlog, " // cached \n");
  1551.         }
  1552.       return this_frame->prev;
  1553.     }

  1554.   /* If the frame unwinder hasn't been selected yet, we must do so
  1555.      before setting prev_p; otherwise the check for misbehaved
  1556.      sniffers will think that this frame's sniffer tried to unwind
  1557.      further (see frame_cleanup_after_sniffer).  */
  1558.   if (this_frame->unwind == NULL)
  1559.     frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);

  1560.   this_frame->prev_p = 1;
  1561.   this_frame->stop_reason = UNWIND_NO_REASON;

  1562.   /* If we are unwinding from an inline frame, all of the below tests
  1563.      were already performed when we unwound from the next non-inline
  1564.      frame.  We must skip them, since we can not get THIS_FRAME's ID
  1565.      until we have unwound all the way down to the previous non-inline
  1566.      frame.  */
  1567.   if (get_frame_type (this_frame) == INLINE_FRAME)
  1568.     return get_prev_frame_if_no_cycle (this_frame);

  1569.   /* Check that this frame is unwindable.  If it isn't, don't try to
  1570.      unwind to the prev frame.  */
  1571.   this_frame->stop_reason
  1572.     = this_frame->unwind->stop_reason (this_frame,
  1573.                                        &this_frame->prologue_cache);

  1574.   if (this_frame->stop_reason != UNWIND_NO_REASON)
  1575.     {
  1576.       if (frame_debug)
  1577.         {
  1578.           enum unwind_stop_reason reason = this_frame->stop_reason;

  1579.           fprintf_unfiltered (gdb_stdlog, "-> ");
  1580.           fprint_frame (gdb_stdlog, NULL);
  1581.           fprintf_unfiltered (gdb_stdlog, " // %s }\n",
  1582.                               frame_stop_reason_symbol_string (reason));
  1583.         }
  1584.       return NULL;
  1585.     }

  1586.   /* Check that this frame's ID isn't inner to (younger, below, next)
  1587.      the next frame.  This happens when a frame unwind goes backwards.
  1588.      This check is valid only if this frame and the next frame are NORMAL.
  1589.      See the comment at frame_id_inner for details.  */
  1590.   if (get_frame_type (this_frame) == NORMAL_FRAME
  1591.       && this_frame->next->unwind->type == NORMAL_FRAME
  1592.       && frame_id_inner (get_frame_arch (this_frame->next),
  1593.                          get_frame_id (this_frame),
  1594.                          get_frame_id (this_frame->next)))
  1595.     {
  1596.       CORE_ADDR this_pc_in_block;
  1597.       struct minimal_symbol *morestack_msym;
  1598.       const char *morestack_name = NULL;

  1599.       /* gcc -fsplit-stack __morestack can continue the stack anywhere.  */
  1600.       this_pc_in_block = get_frame_address_in_block (this_frame);
  1601.       morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
  1602.       if (morestack_msym)
  1603.         morestack_name = MSYMBOL_LINKAGE_NAME (morestack_msym);
  1604.       if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
  1605.         {
  1606.           if (frame_debug)
  1607.             {
  1608.               fprintf_unfiltered (gdb_stdlog, "-> ");
  1609.               fprint_frame (gdb_stdlog, NULL);
  1610.               fprintf_unfiltered (gdb_stdlog,
  1611.                                   " // this frame ID is inner }\n");
  1612.             }
  1613.           this_frame->stop_reason = UNWIND_INNER_ID;
  1614.           return NULL;
  1615.         }
  1616.     }

  1617.   /* Check that this and the next frame do not unwind the PC register
  1618.      to the same memory location.  If they do, then even though they
  1619.      have different frame IDs, the new frame will be bogus; two
  1620.      functions can't share a register save slot for the PC.  This can
  1621.      happen when the prologue analyzer finds a stack adjustment, but
  1622.      no PC save.

  1623.      This check does assume that the "PC register" is roughly a
  1624.      traditional PC, even if the gdbarch_unwind_pc method adjusts
  1625.      it (we do not rely on the value, only on the unwound PC being
  1626.      dependent on this value).  A potential improvement would be
  1627.      to have the frame prev_pc method and the gdbarch unwind_pc
  1628.      method set the same lval and location information as
  1629.      frame_register_unwind.  */
  1630.   if (this_frame->level > 0
  1631.       && gdbarch_pc_regnum (gdbarch) >= 0
  1632.       && get_frame_type (this_frame) == NORMAL_FRAME
  1633.       && (get_frame_type (this_frame->next) == NORMAL_FRAME
  1634.           || get_frame_type (this_frame->next) == INLINE_FRAME))
  1635.     {
  1636.       int optimized, realnum, nrealnum;
  1637.       enum lval_type lval, nlval;
  1638.       CORE_ADDR addr, naddr;

  1639.       frame_register_unwind_location (this_frame,
  1640.                                       gdbarch_pc_regnum (gdbarch),
  1641.                                       &optimized, &lval, &addr, &realnum);
  1642.       frame_register_unwind_location (get_next_frame (this_frame),
  1643.                                       gdbarch_pc_regnum (gdbarch),
  1644.                                       &optimized, &nlval, &naddr, &nrealnum);

  1645.       if ((lval == lval_memory && lval == nlval && addr == naddr)
  1646.           || (lval == lval_register && lval == nlval && realnum == nrealnum))
  1647.         {
  1648.           if (frame_debug)
  1649.             {
  1650.               fprintf_unfiltered (gdb_stdlog, "-> ");
  1651.               fprint_frame (gdb_stdlog, NULL);
  1652.               fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
  1653.             }

  1654.           this_frame->stop_reason = UNWIND_NO_SAVED_PC;
  1655.           this_frame->prev = NULL;
  1656.           return NULL;
  1657.         }
  1658.     }

  1659.   return get_prev_frame_if_no_cycle (this_frame);
  1660. }

  1661. /* Return a "struct frame_info" corresponding to the frame that called
  1662.    THIS_FRAME.  Returns NULL if there is no such frame.

  1663.    Unlike get_prev_frame, this function always tries to unwind the
  1664.    frame.  */

  1665. struct frame_info *
  1666. get_prev_frame_always (struct frame_info *this_frame)
  1667. {
  1668.   volatile struct gdb_exception ex;
  1669.   struct frame_info *prev_frame = NULL;

  1670.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1671.     {
  1672.       prev_frame = get_prev_frame_always_1 (this_frame);
  1673.     }
  1674.   if (ex.reason < 0)
  1675.     {
  1676.       if (ex.error == MEMORY_ERROR)
  1677.         {
  1678.           this_frame->stop_reason = UNWIND_MEMORY_ERROR;
  1679.           if (ex.message != NULL)
  1680.             {
  1681.               char *stop_string;
  1682.               size_t size;

  1683.               /* The error needs to live as long as the frame does.
  1684.                  Allocate using stack local STOP_STRING then assign the
  1685.                  pointer to the frame, this allows the STOP_STRING on the
  1686.                  frame to be of type 'const char *'.  */
  1687.               size = strlen (ex.message) + 1;
  1688.               stop_string = frame_obstack_zalloc (size);
  1689.               memcpy (stop_string, ex.message, size);
  1690.               this_frame->stop_string = stop_string;
  1691.             }
  1692.           prev_frame = NULL;
  1693.         }
  1694.       else
  1695.         throw_exception (ex);
  1696.     }

  1697.   return prev_frame;
  1698. }

  1699. /* Construct a new "struct frame_info" and link it previous to
  1700.    this_frame.  */

  1701. static struct frame_info *
  1702. get_prev_frame_raw (struct frame_info *this_frame)
  1703. {
  1704.   struct frame_info *prev_frame;

  1705.   /* Allocate the new frame but do not wire it in to the frame chain.
  1706.      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
  1707.      frame->next to pull some fancy tricks (of course such code is, by
  1708.      definition, recursive).  Try to prevent it.

  1709.      There is no reason to worry about memory leaks, should the
  1710.      remainder of the function fail.  The allocated memory will be
  1711.      quickly reclaimed when the frame cache is flushed, and the `we've
  1712.      been here before' check above will stop repeated memory
  1713.      allocation calls.  */
  1714.   prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
  1715.   prev_frame->level = this_frame->level + 1;

  1716.   /* For now, assume we don't have frame chains crossing address
  1717.      spaces.  */
  1718.   prev_frame->pspace = this_frame->pspace;
  1719.   prev_frame->aspace = this_frame->aspace;

  1720.   /* Don't yet compute ->unwind (and hence ->type).  It is computed
  1721.      on-demand in get_frame_type, frame_register_unwind, and
  1722.      get_frame_id.  */

  1723.   /* Don't yet compute the frame's ID.  It is computed on-demand by
  1724.      get_frame_id().  */

  1725.   /* The unwound frame ID is validate at the start of this function,
  1726.      as part of the logic to decide if that frame should be further
  1727.      unwound, and not here while the prev frame is being created.
  1728.      Doing this makes it possible for the user to examine a frame that
  1729.      has an invalid frame ID.

  1730.      Some very old VAX code noted: [...]  For the sake of argument,
  1731.      suppose that the stack is somewhat trashed (which is one reason
  1732.      that "info frame" exists).  So, return 0 (indicating we don't
  1733.      know the address of the arglist) if we don't know what frame this
  1734.      frame calls.  */

  1735.   /* Link it in.  */
  1736.   this_frame->prev = prev_frame;
  1737.   prev_frame->next = this_frame;

  1738.   if (frame_debug)
  1739.     {
  1740.       fprintf_unfiltered (gdb_stdlog, "-> ");
  1741.       fprint_frame (gdb_stdlog, prev_frame);
  1742.       fprintf_unfiltered (gdb_stdlog, " }\n");
  1743.     }

  1744.   return prev_frame;
  1745. }

  1746. /* Debug routine to print a NULL frame being returned.  */

  1747. static void
  1748. frame_debug_got_null_frame (struct frame_info *this_frame,
  1749.                             const char *reason)
  1750. {
  1751.   if (frame_debug)
  1752.     {
  1753.       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
  1754.       if (this_frame != NULL)
  1755.         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
  1756.       else
  1757.         fprintf_unfiltered (gdb_stdlog, "<NULL>");
  1758.       fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
  1759.     }
  1760. }

  1761. /* Is this (non-sentinel) frame in the "main"() function?  */

  1762. static int
  1763. inside_main_func (struct frame_info *this_frame)
  1764. {
  1765.   struct bound_minimal_symbol msymbol;
  1766.   CORE_ADDR maddr;

  1767.   if (symfile_objfile == 0)
  1768.     return 0;
  1769.   msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
  1770.   if (msymbol.minsym == NULL)
  1771.     return 0;
  1772.   /* Make certain that the code, and not descriptor, address is
  1773.      returned.  */
  1774.   maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
  1775.                                               BMSYMBOL_VALUE_ADDRESS (msymbol),
  1776.                                               &current_target);
  1777.   return maddr == get_frame_func (this_frame);
  1778. }

  1779. /* Test whether THIS_FRAME is inside the process entry point function.  */

  1780. static int
  1781. inside_entry_func (struct frame_info *this_frame)
  1782. {
  1783.   CORE_ADDR entry_point;

  1784.   if (!entry_point_address_query (&entry_point))
  1785.     return 0;

  1786.   return get_frame_func (this_frame) == entry_point;
  1787. }

  1788. /* Return a structure containing various interesting information about
  1789.    the frame that called THIS_FRAME.  Returns NULL if there is entier
  1790.    no such frame or the frame fails any of a set of target-independent
  1791.    condition that should terminate the frame chain (e.g., as unwinding
  1792.    past main()).

  1793.    This function should not contain target-dependent tests, such as
  1794.    checking whether the program-counter is zero.  */

  1795. struct frame_info *
  1796. get_prev_frame (struct frame_info *this_frame)
  1797. {
  1798.   CORE_ADDR frame_pc;
  1799.   int frame_pc_p;

  1800.   /* There is always a frame.  If this assertion fails, suspect that
  1801.      something should be calling get_selected_frame() or
  1802.      get_current_frame().  */
  1803.   gdb_assert (this_frame != NULL);
  1804.   frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);

  1805.   /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
  1806.      sense to stop unwinding at a dummy frame.  One place where a dummy
  1807.      frame may have an address "inside_main_func" is on HPUX.  On HPUX, the
  1808.      pcsqh register (space register for the instruction at the head of the
  1809.      instruction queue) cannot be written directly; the only way to set it
  1810.      is to branch to code that is in the target space.  In order to implement
  1811.      frame dummies on HPUX, the called function is made to jump back to where
  1812.      the inferior was when the user function was called.  If gdb was inside
  1813.      the main function when we created the dummy frame, the dummy frame will
  1814.      point inside the main function.  */
  1815.   if (this_frame->level >= 0
  1816.       && get_frame_type (this_frame) == NORMAL_FRAME
  1817.       && !backtrace_past_main
  1818.       && frame_pc_p
  1819.       && inside_main_func (this_frame))
  1820.     /* Don't unwind past main().  Note, this is done _before_ the
  1821.        frame has been marked as previously unwound.  That way if the
  1822.        user later decides to enable unwinds past main(), that will
  1823.        automatically happen.  */
  1824.     {
  1825.       frame_debug_got_null_frame (this_frame, "inside main func");
  1826.       return NULL;
  1827.     }

  1828.   /* If the user's backtrace limit has been exceeded, stop.  We must
  1829.      add two to the current level; one of those accounts for backtrace_limit
  1830.      being 1-based and the level being 0-based, and the other accounts for
  1831.      the level of the new frame instead of the level of the current
  1832.      frame.  */
  1833.   if (this_frame->level + 2 > backtrace_limit)
  1834.     {
  1835.       frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
  1836.       return NULL;
  1837.     }

  1838.   /* If we're already inside the entry function for the main objfile,
  1839.      then it isn't valid.  Don't apply this test to a dummy frame -
  1840.      dummy frame PCs typically land in the entry func.  Don't apply
  1841.      this test to the sentinel frame.  Sentinel frames should always
  1842.      be allowed to unwind.  */
  1843.   /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
  1844.      wasn't checking for "main" in the minimal symbols.  With that
  1845.      fixed asm-source tests now stop in "main" instead of halting the
  1846.      backtrace in weird and wonderful ways somewhere inside the entry
  1847.      file.  Suspect that tests for inside the entry file/func were
  1848.      added to work around that (now fixed) case.  */
  1849.   /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
  1850.      suggested having the inside_entry_func test use the
  1851.      inside_main_func() msymbol trick (along with entry_point_address()
  1852.      I guess) to determine the address range of the start function.
  1853.      That should provide a far better stopper than the current
  1854.      heuristics.  */
  1855.   /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
  1856.      applied tail-call optimizations to main so that a function called
  1857.      from main returns directly to the caller of main.  Since we don't
  1858.      stop at main, we should at least stop at the entry point of the
  1859.      application.  */
  1860.   if (this_frame->level >= 0
  1861.       && get_frame_type (this_frame) == NORMAL_FRAME
  1862.       && !backtrace_past_entry
  1863.       && frame_pc_p
  1864.       && inside_entry_func (this_frame))
  1865.     {
  1866.       frame_debug_got_null_frame (this_frame, "inside entry func");
  1867.       return NULL;
  1868.     }

  1869.   /* Assume that the only way to get a zero PC is through something
  1870.      like a SIGSEGV or a dummy frame, and hence that NORMAL frames
  1871.      will never unwind a zero PC.  */
  1872.   if (this_frame->level > 0
  1873.       && (get_frame_type (this_frame) == NORMAL_FRAME
  1874.           || get_frame_type (this_frame) == INLINE_FRAME)
  1875.       && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
  1876.       && frame_pc_p && frame_pc == 0)
  1877.     {
  1878.       frame_debug_got_null_frame (this_frame, "zero PC");
  1879.       return NULL;
  1880.     }

  1881.   return get_prev_frame_always (this_frame);
  1882. }

  1883. CORE_ADDR
  1884. get_frame_pc (struct frame_info *frame)
  1885. {
  1886.   gdb_assert (frame->next != NULL);
  1887.   return frame_unwind_pc (frame->next);
  1888. }

  1889. int
  1890. get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
  1891. {
  1892.   volatile struct gdb_exception ex;

  1893.   gdb_assert (frame->next != NULL);

  1894.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1895.     {
  1896.       *pc = frame_unwind_pc (frame->next);
  1897.     }
  1898.   if (ex.reason < 0)
  1899.     {
  1900.       if (ex.error == NOT_AVAILABLE_ERROR)
  1901.         return 0;
  1902.       else
  1903.         throw_exception (ex);
  1904.     }

  1905.   return 1;
  1906. }

  1907. /* Return an address that falls within THIS_FRAME's code block.  */

  1908. CORE_ADDR
  1909. get_frame_address_in_block (struct frame_info *this_frame)
  1910. {
  1911.   /* A draft address.  */
  1912.   CORE_ADDR pc = get_frame_pc (this_frame);

  1913.   struct frame_info *next_frame = this_frame->next;

  1914.   /* Calling get_frame_pc returns the resume address for THIS_FRAME.
  1915.      Normally the resume address is inside the body of the function
  1916.      associated with THIS_FRAME, but there is a special case: when
  1917.      calling a function which the compiler knows will never return
  1918.      (for instance abort), the call may be the very last instruction
  1919.      in the calling function.  The resume address will point after the
  1920.      call and may be at the beginning of a different function
  1921.      entirely.

  1922.      If THIS_FRAME is a signal frame or dummy frame, then we should
  1923.      not adjust the unwound PC.  For a dummy frame, GDB pushed the
  1924.      resume address manually onto the stack.  For a signal frame, the
  1925.      OS may have pushed the resume address manually and invoked the
  1926.      handler (e.g. GNU/Linux), or invoked the trampoline which called
  1927.      the signal handler - but in either case the signal handler is
  1928.      expected to return to the trampoline.  So in both of these
  1929.      cases we know that the resume address is executable and
  1930.      related.  So we only need to adjust the PC if THIS_FRAME
  1931.      is a normal function.

  1932.      If the program has been interrupted while THIS_FRAME is current,
  1933.      then clearly the resume address is inside the associated
  1934.      function.  There are three kinds of interruption: debugger stop
  1935.      (next frame will be SENTINEL_FRAME), operating system
  1936.      signal or exception (next frame will be SIGTRAMP_FRAME),
  1937.      or debugger-induced function call (next frame will be
  1938.      DUMMY_FRAME).  So we only need to adjust the PC if
  1939.      NEXT_FRAME is a normal function.

  1940.      We check the type of NEXT_FRAME first, since it is already
  1941.      known; frame type is determined by the unwinder, and since
  1942.      we have THIS_FRAME we've already selected an unwinder for
  1943.      NEXT_FRAME.

  1944.      If the next frame is inlined, we need to keep going until we find
  1945.      the real function - for instance, if a signal handler is invoked
  1946.      while in an inlined function, then the code address of the
  1947.      "calling" normal function should not be adjusted either.  */

  1948.   while (get_frame_type (next_frame) == INLINE_FRAME)
  1949.     next_frame = next_frame->next;

  1950.   if ((get_frame_type (next_frame) == NORMAL_FRAME
  1951.        || get_frame_type (next_frame) == TAILCALL_FRAME)
  1952.       && (get_frame_type (this_frame) == NORMAL_FRAME
  1953.           || get_frame_type (this_frame) == TAILCALL_FRAME
  1954.           || get_frame_type (this_frame) == INLINE_FRAME))
  1955.     return pc - 1;

  1956.   return pc;
  1957. }

  1958. int
  1959. get_frame_address_in_block_if_available (struct frame_info *this_frame,
  1960.                                          CORE_ADDR *pc)
  1961. {
  1962.   volatile struct gdb_exception ex;

  1963.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1964.     {
  1965.       *pc = get_frame_address_in_block (this_frame);
  1966.     }
  1967.   if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
  1968.     return 0;
  1969.   else if (ex.reason < 0)
  1970.     throw_exception (ex);
  1971.   else
  1972.     return 1;
  1973. }

  1974. void
  1975. find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
  1976. {
  1977.   struct frame_info *next_frame;
  1978.   int notcurrent;
  1979.   CORE_ADDR pc;

  1980.   /* If the next frame represents an inlined function call, this frame's
  1981.      sal is the "call site" of that inlined function, which can not
  1982.      be inferred from get_frame_pc.  */
  1983.   next_frame = get_next_frame (frame);
  1984.   if (frame_inlined_callees (frame) > 0)
  1985.     {
  1986.       struct symbol *sym;

  1987.       if (next_frame)
  1988.         sym = get_frame_function (next_frame);
  1989.       else
  1990.         sym = inline_skipped_symbol (inferior_ptid);

  1991.       /* If frame is inline, it certainly has symbols.  */
  1992.       gdb_assert (sym);
  1993.       init_sal (sal);
  1994.       if (SYMBOL_LINE (sym) != 0)
  1995.         {
  1996.           sal->symtab = symbol_symtab (sym);
  1997.           sal->line = SYMBOL_LINE (sym);
  1998.         }
  1999.       else
  2000.         /* If the symbol does not have a location, we don't know where
  2001.            the call site is.  Do not pretend to.  This is jarring, but
  2002.            we can't do much better.  */
  2003.         sal->pc = get_frame_pc (frame);

  2004.       sal->pspace = get_frame_program_space (frame);

  2005.       return;
  2006.     }

  2007.   /* If FRAME is not the innermost frame, that normally means that
  2008.      FRAME->pc points at the return instruction (which is *after* the
  2009.      call instruction), and we want to get the line containing the
  2010.      call (because the call is where the user thinks the program is).
  2011.      However, if the next frame is either a SIGTRAMP_FRAME or a
  2012.      DUMMY_FRAME, then the next frame will contain a saved interrupt
  2013.      PC and such a PC indicates the current (rather than next)
  2014.      instruction/line, consequently, for such cases, want to get the
  2015.      line containing fi->pc.  */
  2016.   if (!get_frame_pc_if_available (frame, &pc))
  2017.     {
  2018.       init_sal (sal);
  2019.       return;
  2020.     }

  2021.   notcurrent = (pc != get_frame_address_in_block (frame));
  2022.   (*sal) = find_pc_line (pc, notcurrent);
  2023. }

  2024. /* Per "frame.h", return the ``address'' of the frame.  Code should
  2025.    really be using get_frame_id().  */
  2026. CORE_ADDR
  2027. get_frame_base (struct frame_info *fi)
  2028. {
  2029.   return get_frame_id (fi).stack_addr;
  2030. }

  2031. /* High-level offsets into the frame.  Used by the debug info.  */

  2032. CORE_ADDR
  2033. get_frame_base_address (struct frame_info *fi)
  2034. {
  2035.   if (get_frame_type (fi) != NORMAL_FRAME)
  2036.     return 0;
  2037.   if (fi->base == NULL)
  2038.     fi->base = frame_base_find_by_frame (fi);
  2039.   /* Sneaky: If the low-level unwind and high-level base code share a
  2040.      common unwinder, let them share the prologue cache.  */
  2041.   if (fi->base->unwind == fi->unwind)
  2042.     return fi->base->this_base (fi, &fi->prologue_cache);
  2043.   return fi->base->this_base (fi, &fi->base_cache);
  2044. }

  2045. CORE_ADDR
  2046. get_frame_locals_address (struct frame_info *fi)
  2047. {
  2048.   if (get_frame_type (fi) != NORMAL_FRAME)
  2049.     return 0;
  2050.   /* If there isn't a frame address method, find it.  */
  2051.   if (fi->base == NULL)
  2052.     fi->base = frame_base_find_by_frame (fi);
  2053.   /* Sneaky: If the low-level unwind and high-level base code share a
  2054.      common unwinder, let them share the prologue cache.  */
  2055.   if (fi->base->unwind == fi->unwind)
  2056.     return fi->base->this_locals (fi, &fi->prologue_cache);
  2057.   return fi->base->this_locals (fi, &fi->base_cache);
  2058. }

  2059. CORE_ADDR
  2060. get_frame_args_address (struct frame_info *fi)
  2061. {
  2062.   if (get_frame_type (fi) != NORMAL_FRAME)
  2063.     return 0;
  2064.   /* If there isn't a frame address method, find it.  */
  2065.   if (fi->base == NULL)
  2066.     fi->base = frame_base_find_by_frame (fi);
  2067.   /* Sneaky: If the low-level unwind and high-level base code share a
  2068.      common unwinder, let them share the prologue cache.  */
  2069.   if (fi->base->unwind == fi->unwind)
  2070.     return fi->base->this_args (fi, &fi->prologue_cache);
  2071.   return fi->base->this_args (fi, &fi->base_cache);
  2072. }

  2073. /* Return true if the frame unwinder for frame FI is UNWINDER; false
  2074.    otherwise.  */

  2075. int
  2076. frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
  2077. {
  2078.   if (fi->unwind == NULL)
  2079.     frame_unwind_find_by_frame (fi, &fi->prologue_cache);
  2080.   return fi->unwind == unwinder;
  2081. }

  2082. /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
  2083.    or -1 for a NULL frame.  */

  2084. int
  2085. frame_relative_level (struct frame_info *fi)
  2086. {
  2087.   if (fi == NULL)
  2088.     return -1;
  2089.   else
  2090.     return fi->level;
  2091. }

  2092. enum frame_type
  2093. get_frame_type (struct frame_info *frame)
  2094. {
  2095.   if (frame->unwind == NULL)
  2096.     /* Initialize the frame's unwinder because that's what
  2097.        provides the frame's type.  */
  2098.     frame_unwind_find_by_frame (frame, &frame->prologue_cache);
  2099.   return frame->unwind->type;
  2100. }

  2101. struct program_space *
  2102. get_frame_program_space (struct frame_info *frame)
  2103. {
  2104.   return frame->pspace;
  2105. }

  2106. struct program_space *
  2107. frame_unwind_program_space (struct frame_info *this_frame)
  2108. {
  2109.   gdb_assert (this_frame);

  2110.   /* This is really a placeholder to keep the API consistent --- we
  2111.      assume for now that we don't have frame chains crossing
  2112.      spaces.  */
  2113.   return this_frame->pspace;
  2114. }

  2115. struct address_space *
  2116. get_frame_address_space (struct frame_info *frame)
  2117. {
  2118.   return frame->aspace;
  2119. }

  2120. /* Memory access methods.  */

  2121. void
  2122. get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
  2123.                   gdb_byte *buf, int len)
  2124. {
  2125.   read_memory (addr, buf, len);
  2126. }

  2127. LONGEST
  2128. get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
  2129.                          int len)
  2130. {
  2131.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2132.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  2133.   return read_memory_integer (addr, len, byte_order);
  2134. }

  2135. ULONGEST
  2136. get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
  2137.                            int len)
  2138. {
  2139.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2140.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  2141.   return read_memory_unsigned_integer (addr, len, byte_order);
  2142. }

  2143. int
  2144. safe_frame_unwind_memory (struct frame_info *this_frame,
  2145.                           CORE_ADDR addr, gdb_byte *buf, int len)
  2146. {
  2147.   /* NOTE: target_read_memory returns zero on success!  */
  2148.   return !target_read_memory (addr, buf, len);
  2149. }

  2150. /* Architecture methods.  */

  2151. struct gdbarch *
  2152. get_frame_arch (struct frame_info *this_frame)
  2153. {
  2154.   return frame_unwind_arch (this_frame->next);
  2155. }

  2156. struct gdbarch *
  2157. frame_unwind_arch (struct frame_info *next_frame)
  2158. {
  2159.   if (!next_frame->prev_arch.p)
  2160.     {
  2161.       struct gdbarch *arch;

  2162.       if (next_frame->unwind == NULL)
  2163.         frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);

  2164.       if (next_frame->unwind->prev_arch != NULL)
  2165.         arch = next_frame->unwind->prev_arch (next_frame,
  2166.                                               &next_frame->prologue_cache);
  2167.       else
  2168.         arch = get_frame_arch (next_frame);

  2169.       next_frame->prev_arch.arch = arch;
  2170.       next_frame->prev_arch.p = 1;
  2171.       if (frame_debug)
  2172.         fprintf_unfiltered (gdb_stdlog,
  2173.                             "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
  2174.                             next_frame->level,
  2175.                             gdbarch_bfd_arch_info (arch)->printable_name);
  2176.     }

  2177.   return next_frame->prev_arch.arch;
  2178. }

  2179. struct gdbarch *
  2180. frame_unwind_caller_arch (struct frame_info *next_frame)
  2181. {
  2182.   return frame_unwind_arch (skip_artificial_frames (next_frame));
  2183. }

  2184. /* Stack pointer methods.  */

  2185. CORE_ADDR
  2186. get_frame_sp (struct frame_info *this_frame)
  2187. {
  2188.   struct gdbarch *gdbarch = get_frame_arch (this_frame);

  2189.   /* Normality - an architecture that provides a way of obtaining any
  2190.      frame inner-most address.  */
  2191.   if (gdbarch_unwind_sp_p (gdbarch))
  2192.     /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
  2193.        operate on THIS_FRAME now.  */
  2194.     return gdbarch_unwind_sp (gdbarch, this_frame->next);
  2195.   /* Now things are really are grim.  Hope that the value returned by
  2196.      the gdbarch_sp_regnum register is meaningful.  */
  2197.   if (gdbarch_sp_regnum (gdbarch) >= 0)
  2198.     return get_frame_register_unsigned (this_frame,
  2199.                                         gdbarch_sp_regnum (gdbarch));
  2200.   internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
  2201. }

  2202. /* Return the reason why we can't unwind past FRAME.  */

  2203. enum unwind_stop_reason
  2204. get_frame_unwind_stop_reason (struct frame_info *frame)
  2205. {
  2206.   /* Fill-in STOP_REASON.  */
  2207.   get_prev_frame_always (frame);
  2208.   gdb_assert (frame->prev_p);

  2209.   return frame->stop_reason;
  2210. }

  2211. /* Return a string explaining REASON.  */

  2212. const char *
  2213. unwind_stop_reason_to_string (enum unwind_stop_reason reason)
  2214. {
  2215.   switch (reason)
  2216.     {
  2217. #define SET(name, description) \
  2218.     case name: return _(description);
  2219. #include "unwind_stop_reasons.def"
  2220. #undef SET

  2221.     default:
  2222.       internal_error (__FILE__, __LINE__,
  2223.                       "Invalid frame stop reason");
  2224.     }
  2225. }

  2226. const char *
  2227. frame_stop_reason_string (struct frame_info *fi)
  2228. {
  2229.   gdb_assert (fi->prev_p);
  2230.   gdb_assert (fi->prev == NULL);

  2231.   /* Return the specific string if we have one.  */
  2232.   if (fi->stop_string != NULL)
  2233.     return fi->stop_string;

  2234.   /* Return the generic string if we have nothing better.  */
  2235.   return unwind_stop_reason_to_string (fi->stop_reason);
  2236. }

  2237. /* Return the enum symbol name of REASON as a string, to use in debug
  2238.    output.  */

  2239. static const char *
  2240. frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
  2241. {
  2242.   switch (reason)
  2243.     {
  2244. #define SET(name, description) \
  2245.     case name: return #name;
  2246. #include "unwind_stop_reasons.def"
  2247. #undef SET

  2248.     default:
  2249.       internal_error (__FILE__, __LINE__,
  2250.                       "Invalid frame stop reason");
  2251.     }
  2252. }

  2253. /* Clean up after a failed (wrong unwinder) attempt to unwind past
  2254.    FRAME.  */

  2255. static void
  2256. frame_cleanup_after_sniffer (void *arg)
  2257. {
  2258.   struct frame_info *frame = arg;

  2259.   /* The sniffer should not allocate a prologue cache if it did not
  2260.      match this frame.  */
  2261.   gdb_assert (frame->prologue_cache == NULL);

  2262.   /* No sniffer should extend the frame chain; sniff based on what is
  2263.      already certain.  */
  2264.   gdb_assert (!frame->prev_p);

  2265.   /* The sniffer should not check the frame's ID; that's circular.  */
  2266.   gdb_assert (!frame->this_id.p);

  2267.   /* Clear cached fields dependent on the unwinder.

  2268.      The previous PC is independent of the unwinder, but the previous
  2269.      function is not (see get_frame_address_in_block).  */
  2270.   frame->prev_func.p = 0;
  2271.   frame->prev_func.addr = 0;

  2272.   /* Discard the unwinder last, so that we can easily find it if an assertion
  2273.      in this function triggers.  */
  2274.   frame->unwind = NULL;
  2275. }

  2276. /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
  2277.    Return a cleanup which should be called if unwinding fails, and
  2278.    discarded if it succeeds.  */

  2279. struct cleanup *
  2280. frame_prepare_for_sniffer (struct frame_info *frame,
  2281.                            const struct frame_unwind *unwind)
  2282. {
  2283.   gdb_assert (frame->unwind == NULL);
  2284.   frame->unwind = unwind;
  2285.   return make_cleanup (frame_cleanup_after_sniffer, frame);
  2286. }

  2287. extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */

  2288. static struct cmd_list_element *set_backtrace_cmdlist;
  2289. static struct cmd_list_element *show_backtrace_cmdlist;

  2290. static void
  2291. set_backtrace_cmd (char *args, int from_tty)
  2292. {
  2293.   help_list (set_backtrace_cmdlist, "set backtrace ", all_commands,
  2294.              gdb_stdout);
  2295. }

  2296. static void
  2297. show_backtrace_cmd (char *args, int from_tty)
  2298. {
  2299.   cmd_show_list (show_backtrace_cmdlist, from_tty, "");
  2300. }

  2301. void
  2302. _initialize_frame (void)
  2303. {
  2304.   obstack_init (&frame_cache_obstack);

  2305.   frame_stash_create ();

  2306.   observer_attach_target_changed (frame_observer_target_changed);

  2307.   add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
  2308. Set backtrace specific variables.\n\
  2309. Configure backtrace variables such as the backtrace limit"),
  2310.                   &set_backtrace_cmdlist, "set backtrace ",
  2311.                   0/*allow-unknown*/, &setlist);
  2312.   add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
  2313. Show backtrace specific variables\n\
  2314. Show backtrace variables such as the backtrace limit"),
  2315.                   &show_backtrace_cmdlist, "show backtrace ",
  2316.                   0/*allow-unknown*/, &showlist);

  2317.   add_setshow_boolean_cmd ("past-main", class_obscure,
  2318.                            &backtrace_past_main, _("\
  2319. Set whether backtraces should continue past \"main\"."), _("\
  2320. Show whether backtraces should continue past \"main\"."), _("\
  2321. Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
  2322. the backtrace at \"main\".  Set this variable if you need to see the rest\n\
  2323. of the stack trace."),
  2324.                            NULL,
  2325.                            show_backtrace_past_main,
  2326.                            &set_backtrace_cmdlist,
  2327.                            &show_backtrace_cmdlist);

  2328.   add_setshow_boolean_cmd ("past-entry", class_obscure,
  2329.                            &backtrace_past_entry, _("\
  2330. Set whether backtraces should continue past the entry point of a program."),
  2331.                            _("\
  2332. Show whether backtraces should continue past the entry point of a program."),
  2333.                            _("\
  2334. Normally there are no callers beyond the entry point of a program, so GDB\n\
  2335. will terminate the backtrace there.  Set this variable if you need to see\n\
  2336. the rest of the stack trace."),
  2337.                            NULL,
  2338.                            show_backtrace_past_entry,
  2339.                            &set_backtrace_cmdlist,
  2340.                            &show_backtrace_cmdlist);

  2341.   add_setshow_uinteger_cmd ("limit", class_obscure,
  2342.                             &backtrace_limit, _("\
  2343. Set an upper bound on the number of backtrace levels."), _("\
  2344. Show the upper bound on the number of backtrace levels."), _("\
  2345. No more than the specified number of frames can be displayed or examined.\n\
  2346. Literal \"unlimited\" or zero means no limit."),
  2347.                             NULL,
  2348.                             show_backtrace_limit,
  2349.                             &set_backtrace_cmdlist,
  2350.                             &show_backtrace_cmdlist);

  2351.   /* Debug this files internals.  */
  2352.   add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug_("\
  2353. Set frame debugging."), _("\
  2354. Show frame debugging."), _("\
  2355. When non-zero, frame specific internal debugging is enabled."),
  2356.                              NULL,
  2357.                              show_frame_debug,
  2358.                              &setdebuglist, &showdebuglist);
  2359. }