GDB (API)
/home/stan/gdb/src/gdb/frame.c
Go to the documentation of this file.
00001 /* Cache and manage frames for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "frame.h"
00022 #include "target.h"
00023 #include "value.h"
00024 #include "inferior.h"   /* for inferior_ptid */
00025 #include "regcache.h"
00026 #include "gdb_assert.h"
00027 #include "gdb_string.h"
00028 #include "user-regs.h"
00029 #include "gdb_obstack.h"
00030 #include "dummy-frame.h"
00031 #include "sentinel-frame.h"
00032 #include "gdbcore.h"
00033 #include "annotate.h"
00034 #include "language.h"
00035 #include "frame-unwind.h"
00036 #include "frame-base.h"
00037 #include "command.h"
00038 #include "gdbcmd.h"
00039 #include "observer.h"
00040 #include "objfiles.h"
00041 #include "exceptions.h"
00042 #include "gdbthread.h"
00043 #include "block.h"
00044 #include "inline-frame.h"
00045 #include "tracepoint.h"
00046 #include "hashtab.h"
00047 
00048 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
00049 static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
00050 
00051 /* We keep a cache of stack frames, each of which is a "struct
00052    frame_info".  The innermost one gets allocated (in
00053    wait_for_inferior) each time the inferior stops; current_frame
00054    points to it.  Additional frames get allocated (in get_prev_frame)
00055    as needed, and are chained through the next and prev fields.  Any
00056    time that the frame cache becomes invalid (most notably when we
00057    execute something, but also if we change how we interpret the
00058    frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
00059    which reads new symbols)), we should call reinit_frame_cache.  */
00060 
00061 struct frame_info
00062 {
00063   /* Level of this frame.  The inner-most (youngest) frame is at level
00064      0.  As you move towards the outer-most (oldest) frame, the level
00065      increases.  This is a cached value.  It could just as easily be
00066      computed by counting back from the selected frame to the inner
00067      most frame.  */
00068   /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
00069      reserved to indicate a bogus frame - one that has been created
00070      just to keep GDB happy (GDB always needs a frame).  For the
00071      moment leave this as speculation.  */
00072   int level;
00073 
00074   /* The frame's program space.  */
00075   struct program_space *pspace;
00076 
00077   /* The frame's address space.  */
00078   struct address_space *aspace;
00079 
00080   /* The frame's low-level unwinder and corresponding cache.  The
00081      low-level unwinder is responsible for unwinding register values
00082      for the previous frame.  The low-level unwind methods are
00083      selected based on the presence, or otherwise, of register unwind
00084      information such as CFI.  */
00085   void *prologue_cache;
00086   const struct frame_unwind *unwind;
00087 
00088   /* Cached copy of the previous frame's architecture.  */
00089   struct
00090   {
00091     int p;
00092     struct gdbarch *arch;
00093   } prev_arch;
00094 
00095   /* Cached copy of the previous frame's resume address.  */
00096   struct {
00097     int p;
00098     CORE_ADDR value;
00099   } prev_pc;
00100   
00101   /* Cached copy of the previous frame's function address.  */
00102   struct
00103   {
00104     CORE_ADDR addr;
00105     int p;
00106   } prev_func;
00107   
00108   /* This frame's ID.  */
00109   struct
00110   {
00111     int p;
00112     struct frame_id value;
00113   } this_id;
00114   
00115   /* The frame's high-level base methods, and corresponding cache.
00116      The high level base methods are selected based on the frame's
00117      debug info.  */
00118   const struct frame_base *base;
00119   void *base_cache;
00120 
00121   /* Pointers to the next (down, inner, younger) and previous (up,
00122      outer, older) frame_info's in the frame cache.  */
00123   struct frame_info *next; /* down, inner, younger */
00124   int prev_p;
00125   struct frame_info *prev; /* up, outer, older */
00126 
00127   /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
00128      could.  Only valid when PREV_P is set.  */
00129   enum unwind_stop_reason stop_reason;
00130 };
00131 
00132 /* A frame stash used to speed up frame lookups.  Create a hash table
00133    to stash frames previously accessed from the frame cache for
00134    quicker subsequent retrieval.  The hash table is emptied whenever
00135    the frame cache is invalidated.  */
00136 
00137 static htab_t frame_stash;
00138 
00139 /* Internal function to calculate a hash from the frame_id addresses,
00140    using as many valid addresses as possible.  Frames below level 0
00141    are not stored in the hash table.  */
00142 
00143 static hashval_t
00144 frame_addr_hash (const void *ap)
00145 {
00146   const struct frame_info *frame = ap;
00147   const struct frame_id f_id = frame->this_id.value;
00148   hashval_t hash = 0;
00149 
00150   gdb_assert (f_id.stack_addr_p || f_id.code_addr_p
00151               || f_id.special_addr_p);
00152 
00153   if (f_id.stack_addr_p)
00154     hash = iterative_hash (&f_id.stack_addr,
00155                            sizeof (f_id.stack_addr), hash);
00156   if (f_id.code_addr_p)
00157     hash = iterative_hash (&f_id.code_addr,
00158                            sizeof (f_id.code_addr), hash);
00159   if (f_id.special_addr_p)
00160     hash = iterative_hash (&f_id.special_addr,
00161                            sizeof (f_id.special_addr), hash);
00162 
00163   return hash;
00164 }
00165 
00166 /* Internal equality function for the hash table.  This function
00167    defers equality operations to frame_id_eq.  */
00168 
00169 static int
00170 frame_addr_hash_eq (const void *a, const void *b)
00171 {
00172   const struct frame_info *f_entry = a;
00173   const struct frame_info *f_element = b;
00174 
00175   return frame_id_eq (f_entry->this_id.value,
00176                       f_element->this_id.value);
00177 }
00178 
00179 /* Internal function to create the frame_stash hash table.  100 seems
00180    to be a good compromise to start the hash table at.  */
00181 
00182 static void
00183 frame_stash_create (void)
00184 {
00185   frame_stash = htab_create (100,
00186                              frame_addr_hash,
00187                              frame_addr_hash_eq,
00188                              NULL);
00189 }
00190 
00191 /* Internal function to add a frame to the frame_stash hash table.  Do
00192    not store frames below 0 as they may not have any addresses to
00193    calculate a hash.  */
00194 
00195 static void
00196 frame_stash_add (struct frame_info *frame)
00197 {
00198   /* Do not stash frames below level 0.  */
00199   if (frame->level >= 0)
00200     {
00201       struct frame_info **slot;
00202 
00203       slot = (struct frame_info **) htab_find_slot (frame_stash,
00204                                                     frame,
00205                                                     INSERT);
00206       *slot = frame;
00207     }
00208 }
00209 
00210 /* Internal function to search the frame stash for an entry with the
00211    given frame ID.  If found, return that frame.  Otherwise return
00212    NULL.  */
00213 
00214 static struct frame_info *
00215 frame_stash_find (struct frame_id id)
00216 {
00217   struct frame_info dummy;
00218   struct frame_info *frame;
00219 
00220   dummy.this_id.value = id;
00221   frame = htab_find (frame_stash, &dummy);
00222   return frame;
00223 }
00224 
00225 /* Internal function to invalidate the frame stash by removing all
00226    entries in it.  This only occurs when the frame cache is
00227    invalidated.  */
00228 
00229 static void
00230 frame_stash_invalidate (void)
00231 {
00232   htab_empty (frame_stash);
00233 }
00234 
00235 /* Flag to control debugging.  */
00236 
00237 unsigned int frame_debug;
00238 static void
00239 show_frame_debug (struct ui_file *file, int from_tty,
00240                   struct cmd_list_element *c, const char *value)
00241 {
00242   fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
00243 }
00244 
00245 /* Flag to indicate whether backtraces should stop at main et.al.  */
00246 
00247 static int backtrace_past_main;
00248 static void
00249 show_backtrace_past_main (struct ui_file *file, int from_tty,
00250                           struct cmd_list_element *c, const char *value)
00251 {
00252   fprintf_filtered (file,
00253                     _("Whether backtraces should "
00254                       "continue past \"main\" is %s.\n"),
00255                     value);
00256 }
00257 
00258 static int backtrace_past_entry;
00259 static void
00260 show_backtrace_past_entry (struct ui_file *file, int from_tty,
00261                            struct cmd_list_element *c, const char *value)
00262 {
00263   fprintf_filtered (file, _("Whether backtraces should continue past the "
00264                             "entry point of a program is %s.\n"),
00265                     value);
00266 }
00267 
00268 static unsigned int backtrace_limit = UINT_MAX;
00269 static void
00270 show_backtrace_limit (struct ui_file *file, int from_tty,
00271                       struct cmd_list_element *c, const char *value)
00272 {
00273   fprintf_filtered (file,
00274                     _("An upper bound on the number "
00275                       "of backtrace levels is %s.\n"),
00276                     value);
00277 }
00278 
00279 
00280 static void
00281 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
00282 {
00283   if (p)
00284     fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
00285   else
00286     fprintf_unfiltered (file, "!%s", name);
00287 }
00288 
00289 void
00290 fprint_frame_id (struct ui_file *file, struct frame_id id)
00291 {
00292   fprintf_unfiltered (file, "{");
00293   fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
00294   fprintf_unfiltered (file, ",");
00295   fprint_field (file, "code", id.code_addr_p, id.code_addr);
00296   fprintf_unfiltered (file, ",");
00297   fprint_field (file, "special", id.special_addr_p, id.special_addr);
00298   if (id.artificial_depth)
00299     fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
00300   fprintf_unfiltered (file, "}");
00301 }
00302 
00303 static void
00304 fprint_frame_type (struct ui_file *file, enum frame_type type)
00305 {
00306   switch (type)
00307     {
00308     case NORMAL_FRAME:
00309       fprintf_unfiltered (file, "NORMAL_FRAME");
00310       return;
00311     case DUMMY_FRAME:
00312       fprintf_unfiltered (file, "DUMMY_FRAME");
00313       return;
00314     case INLINE_FRAME:
00315       fprintf_unfiltered (file, "INLINE_FRAME");
00316       return;
00317     case TAILCALL_FRAME:
00318       fprintf_unfiltered (file, "TAILCALL_FRAME");
00319       return;
00320     case SIGTRAMP_FRAME:
00321       fprintf_unfiltered (file, "SIGTRAMP_FRAME");
00322       return;
00323     case ARCH_FRAME:
00324       fprintf_unfiltered (file, "ARCH_FRAME");
00325       return;
00326     case SENTINEL_FRAME:
00327       fprintf_unfiltered (file, "SENTINEL_FRAME");
00328       return;
00329     default:
00330       fprintf_unfiltered (file, "<unknown type>");
00331       return;
00332     };
00333 }
00334 
00335 static void
00336 fprint_frame (struct ui_file *file, struct frame_info *fi)
00337 {
00338   if (fi == NULL)
00339     {
00340       fprintf_unfiltered (file, "<NULL frame>");
00341       return;
00342     }
00343   fprintf_unfiltered (file, "{");
00344   fprintf_unfiltered (file, "level=%d", fi->level);
00345   fprintf_unfiltered (file, ",");
00346   fprintf_unfiltered (file, "type=");
00347   if (fi->unwind != NULL)
00348     fprint_frame_type (file, fi->unwind->type);
00349   else
00350     fprintf_unfiltered (file, "<unknown>");
00351   fprintf_unfiltered (file, ",");
00352   fprintf_unfiltered (file, "unwind=");
00353   if (fi->unwind != NULL)
00354     gdb_print_host_address (fi->unwind, file);
00355   else
00356     fprintf_unfiltered (file, "<unknown>");
00357   fprintf_unfiltered (file, ",");
00358   fprintf_unfiltered (file, "pc=");
00359   if (fi->next != NULL && fi->next->prev_pc.p)
00360     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_pc.value));
00361   else
00362     fprintf_unfiltered (file, "<unknown>");
00363   fprintf_unfiltered (file, ",");
00364   fprintf_unfiltered (file, "id=");
00365   if (fi->this_id.p)
00366     fprint_frame_id (file, fi->this_id.value);
00367   else
00368     fprintf_unfiltered (file, "<unknown>");
00369   fprintf_unfiltered (file, ",");
00370   fprintf_unfiltered (file, "func=");
00371   if (fi->next != NULL && fi->next->prev_func.p)
00372     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
00373   else
00374     fprintf_unfiltered (file, "<unknown>");
00375   fprintf_unfiltered (file, "}");
00376 }
00377 
00378 /* Given FRAME, return the enclosing frame as found in real frames read-in from
00379    inferior memory.  Skip any previous frames which were made up by GDB.
00380    Return the original frame if no immediate previous frames exist.  */
00381 
00382 static struct frame_info *
00383 skip_artificial_frames (struct frame_info *frame)
00384 {
00385   while (get_frame_type (frame) == INLINE_FRAME
00386          || get_frame_type (frame) == TAILCALL_FRAME)
00387     frame = get_prev_frame (frame);
00388 
00389   return frame;
00390 }
00391 
00392 /* Return a frame uniq ID that can be used to, later, re-find the
00393    frame.  */
00394 
00395 struct frame_id
00396 get_frame_id (struct frame_info *fi)
00397 {
00398   if (fi == NULL)
00399     return null_frame_id;
00400 
00401   if (!fi->this_id.p)
00402     {
00403       if (frame_debug)
00404         fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
00405                             fi->level);
00406       /* Find the unwinder.  */
00407       if (fi->unwind == NULL)
00408         frame_unwind_find_by_frame (fi, &fi->prologue_cache);
00409       /* Find THIS frame's ID.  */
00410       /* Default to outermost if no ID is found.  */
00411       fi->this_id.value = outer_frame_id;
00412       fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
00413       gdb_assert (frame_id_p (fi->this_id.value));
00414       fi->this_id.p = 1;
00415       if (frame_debug)
00416         {
00417           fprintf_unfiltered (gdb_stdlog, "-> ");
00418           fprint_frame_id (gdb_stdlog, fi->this_id.value);
00419           fprintf_unfiltered (gdb_stdlog, " }\n");
00420         }
00421       frame_stash_add (fi);
00422     }
00423 
00424   return fi->this_id.value;
00425 }
00426 
00427 struct frame_id
00428 get_stack_frame_id (struct frame_info *next_frame)
00429 {
00430   return get_frame_id (skip_artificial_frames (next_frame));
00431 }
00432 
00433 struct frame_id
00434 frame_unwind_caller_id (struct frame_info *next_frame)
00435 {
00436   struct frame_info *this_frame;
00437 
00438   /* Use get_prev_frame_1, and not get_prev_frame.  The latter will truncate
00439      the frame chain, leading to this function unintentionally
00440      returning a null_frame_id (e.g., when a caller requests the frame
00441      ID of "main()"s caller.  */
00442 
00443   next_frame = skip_artificial_frames (next_frame);
00444   this_frame = get_prev_frame_1 (next_frame);
00445   if (this_frame)
00446     return get_frame_id (skip_artificial_frames (this_frame));
00447   else
00448     return null_frame_id;
00449 }
00450 
00451 const struct frame_id null_frame_id; /* All zeros.  */
00452 const struct frame_id outer_frame_id = { 0, 0, 0, 0, 0, 1, 0 };
00453 
00454 struct frame_id
00455 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
00456                         CORE_ADDR special_addr)
00457 {
00458   struct frame_id id = null_frame_id;
00459 
00460   id.stack_addr = stack_addr;
00461   id.stack_addr_p = 1;
00462   id.code_addr = code_addr;
00463   id.code_addr_p = 1;
00464   id.special_addr = special_addr;
00465   id.special_addr_p = 1;
00466   return id;
00467 }
00468 
00469 struct frame_id
00470 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
00471 {
00472   struct frame_id id = null_frame_id;
00473 
00474   id.stack_addr = stack_addr;
00475   id.stack_addr_p = 1;
00476   id.code_addr = code_addr;
00477   id.code_addr_p = 1;
00478   return id;
00479 }
00480 
00481 struct frame_id
00482 frame_id_build_wild (CORE_ADDR stack_addr)
00483 {
00484   struct frame_id id = null_frame_id;
00485 
00486   id.stack_addr = stack_addr;
00487   id.stack_addr_p = 1;
00488   return id;
00489 }
00490 
00491 int
00492 frame_id_p (struct frame_id l)
00493 {
00494   int p;
00495 
00496   /* The frame is valid iff it has a valid stack address.  */
00497   p = l.stack_addr_p;
00498   /* outer_frame_id is also valid.  */
00499   if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
00500     p = 1;
00501   if (frame_debug)
00502     {
00503       fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
00504       fprint_frame_id (gdb_stdlog, l);
00505       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
00506     }
00507   return p;
00508 }
00509 
00510 int
00511 frame_id_artificial_p (struct frame_id l)
00512 {
00513   if (!frame_id_p (l))
00514     return 0;
00515 
00516   return (l.artificial_depth != 0);
00517 }
00518 
00519 int
00520 frame_id_eq (struct frame_id l, struct frame_id r)
00521 {
00522   int eq;
00523 
00524   if (!l.stack_addr_p && l.special_addr_p
00525       && !r.stack_addr_p && r.special_addr_p)
00526     /* The outermost frame marker is equal to itself.  This is the
00527        dodgy thing about outer_frame_id, since between execution steps
00528        we might step into another function - from which we can't
00529        unwind either.  More thought required to get rid of
00530        outer_frame_id.  */
00531     eq = 1;
00532   else if (!l.stack_addr_p || !r.stack_addr_p)
00533     /* Like a NaN, if either ID is invalid, the result is false.
00534        Note that a frame ID is invalid iff it is the null frame ID.  */
00535     eq = 0;
00536   else if (l.stack_addr != r.stack_addr)
00537     /* If .stack addresses are different, the frames are different.  */
00538     eq = 0;
00539   else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
00540     /* An invalid code addr is a wild card.  If .code addresses are
00541        different, the frames are different.  */
00542     eq = 0;
00543   else if (l.special_addr_p && r.special_addr_p
00544            && l.special_addr != r.special_addr)
00545     /* An invalid special addr is a wild card (or unused).  Otherwise
00546        if special addresses are different, the frames are different.  */
00547     eq = 0;
00548   else if (l.artificial_depth != r.artificial_depth)
00549     /* If artifical depths are different, the frames must be different.  */
00550     eq = 0;
00551   else
00552     /* Frames are equal.  */
00553     eq = 1;
00554 
00555   if (frame_debug)
00556     {
00557       fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
00558       fprint_frame_id (gdb_stdlog, l);
00559       fprintf_unfiltered (gdb_stdlog, ",r=");
00560       fprint_frame_id (gdb_stdlog, r);
00561       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
00562     }
00563   return eq;
00564 }
00565 
00566 /* Safety net to check whether frame ID L should be inner to
00567    frame ID R, according to their stack addresses.
00568 
00569    This method cannot be used to compare arbitrary frames, as the
00570    ranges of valid stack addresses may be discontiguous (e.g. due
00571    to sigaltstack).
00572 
00573    However, it can be used as safety net to discover invalid frame
00574    IDs in certain circumstances.  Assuming that NEXT is the immediate
00575    inner frame to THIS and that NEXT and THIS are both NORMAL frames:
00576 
00577    * The stack address of NEXT must be inner-than-or-equal to the stack
00578      address of THIS.
00579 
00580      Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
00581      error has occurred.
00582 
00583    * If NEXT and THIS have different stack addresses, no other frame
00584      in the frame chain may have a stack address in between.
00585 
00586      Therefore, if frame_id_inner (TEST, THIS) holds, but
00587      frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
00588      to a valid frame in the frame chain.
00589 
00590    The sanity checks above cannot be performed when a SIGTRAMP frame
00591    is involved, because signal handlers might be executed on a different
00592    stack than the stack used by the routine that caused the signal
00593    to be raised.  This can happen for instance when a thread exceeds
00594    its maximum stack size.  In this case, certain compilers implement
00595    a stack overflow strategy that cause the handler to be run on a
00596    different stack.  */
00597 
00598 static int
00599 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
00600 {
00601   int inner;
00602 
00603   if (!l.stack_addr_p || !r.stack_addr_p)
00604     /* Like NaN, any operation involving an invalid ID always fails.  */
00605     inner = 0;
00606   else if (l.artificial_depth > r.artificial_depth
00607            && l.stack_addr == r.stack_addr
00608            && l.code_addr_p == r.code_addr_p
00609            && l.special_addr_p == r.special_addr_p
00610            && l.special_addr == r.special_addr)
00611     {
00612       /* Same function, different inlined functions.  */
00613       struct block *lb, *rb;
00614 
00615       gdb_assert (l.code_addr_p && r.code_addr_p);
00616 
00617       lb = block_for_pc (l.code_addr);
00618       rb = block_for_pc (r.code_addr);
00619 
00620       if (lb == NULL || rb == NULL)
00621         /* Something's gone wrong.  */
00622         inner = 0;
00623       else
00624         /* This will return true if LB and RB are the same block, or
00625            if the block with the smaller depth lexically encloses the
00626            block with the greater depth.  */
00627         inner = contained_in (lb, rb);
00628     }
00629   else
00630     /* Only return non-zero when strictly inner than.  Note that, per
00631        comment in "frame.h", there is some fuzz here.  Frameless
00632        functions are not strictly inner than (same .stack but
00633        different .code and/or .special address).  */
00634     inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
00635   if (frame_debug)
00636     {
00637       fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
00638       fprint_frame_id (gdb_stdlog, l);
00639       fprintf_unfiltered (gdb_stdlog, ",r=");
00640       fprint_frame_id (gdb_stdlog, r);
00641       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
00642     }
00643   return inner;
00644 }
00645 
00646 struct frame_info *
00647 frame_find_by_id (struct frame_id id)
00648 {
00649   struct frame_info *frame, *prev_frame;
00650 
00651   /* ZERO denotes the null frame, let the caller decide what to do
00652      about it.  Should it instead return get_current_frame()?  */
00653   if (!frame_id_p (id))
00654     return NULL;
00655 
00656   /* Try using the frame stash first.  Finding it there removes the need
00657      to perform the search by looping over all frames, which can be very
00658      CPU-intensive if the number of frames is very high (the loop is O(n)
00659      and get_prev_frame performs a series of checks that are relatively
00660      expensive).  This optimization is particularly useful when this function
00661      is called from another function (such as value_fetch_lazy, case
00662      VALUE_LVAL (val) == lval_register) which already loops over all frames,
00663      making the overall behavior O(n^2).  */
00664   frame = frame_stash_find (id);
00665   if (frame)
00666     return frame;
00667 
00668   for (frame = get_current_frame (); ; frame = prev_frame)
00669     {
00670       struct frame_id this = get_frame_id (frame);
00671 
00672       if (frame_id_eq (id, this))
00673         /* An exact match.  */
00674         return frame;
00675 
00676       prev_frame = get_prev_frame (frame);
00677       if (!prev_frame)
00678         return NULL;
00679 
00680       /* As a safety net to avoid unnecessary backtracing while trying
00681          to find an invalid ID, we check for a common situation where
00682          we can detect from comparing stack addresses that no other
00683          frame in the current frame chain can have this ID.  See the
00684          comment at frame_id_inner for details.   */
00685       if (get_frame_type (frame) == NORMAL_FRAME
00686           && !frame_id_inner (get_frame_arch (frame), id, this)
00687           && frame_id_inner (get_frame_arch (prev_frame), id,
00688                              get_frame_id (prev_frame)))
00689         return NULL;
00690     }
00691   return NULL;
00692 }
00693 
00694 static int
00695 frame_unwind_pc_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
00696 {
00697   if (!this_frame->prev_pc.p)
00698     {
00699       if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
00700         {
00701           volatile struct gdb_exception ex;
00702           struct gdbarch *prev_gdbarch;
00703           CORE_ADDR pc = 0;
00704 
00705           /* The right way.  The `pure' way.  The one true way.  This
00706              method depends solely on the register-unwind code to
00707              determine the value of registers in THIS frame, and hence
00708              the value of this frame's PC (resume address).  A typical
00709              implementation is no more than:
00710            
00711              frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
00712              return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
00713 
00714              Note: this method is very heavily dependent on a correct
00715              register-unwind implementation, it pays to fix that
00716              method first; this method is frame type agnostic, since
00717              it only deals with register values, it works with any
00718              frame.  This is all in stark contrast to the old
00719              FRAME_SAVED_PC which would try to directly handle all the
00720              different ways that a PC could be unwound.  */
00721           prev_gdbarch = frame_unwind_arch (this_frame);
00722 
00723           TRY_CATCH (ex, RETURN_MASK_ERROR)
00724             {
00725               pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
00726             }
00727           if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
00728             {
00729               this_frame->prev_pc.p = -1;
00730 
00731               if (frame_debug)
00732                 fprintf_unfiltered (gdb_stdlog,
00733                                     "{ frame_unwind_pc (this_frame=%d)"
00734                                     " -> <unavailable> }\n",
00735                                     this_frame->level);
00736             }
00737           else if (ex.reason < 0)
00738             {
00739               throw_exception (ex);
00740             }
00741           else
00742             {
00743               this_frame->prev_pc.value = pc;
00744               this_frame->prev_pc.p = 1;
00745               if (frame_debug)
00746                 fprintf_unfiltered (gdb_stdlog,
00747                                     "{ frame_unwind_pc (this_frame=%d) "
00748                                     "-> %s }\n",
00749                                     this_frame->level,
00750                                     hex_string (this_frame->prev_pc.value));
00751             }
00752         }
00753       else
00754         internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
00755     }
00756   if (this_frame->prev_pc.p < 0)
00757     {
00758       *pc = -1;
00759       return 0;
00760     }
00761   else
00762     {
00763       *pc = this_frame->prev_pc.value;
00764       return 1;
00765     }
00766 }
00767 
00768 static CORE_ADDR
00769 frame_unwind_pc (struct frame_info *this_frame)
00770 {
00771   CORE_ADDR pc;
00772 
00773   if (!frame_unwind_pc_if_available (this_frame, &pc))
00774     throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
00775   else
00776     return pc;
00777 }
00778 
00779 CORE_ADDR
00780 frame_unwind_caller_pc (struct frame_info *this_frame)
00781 {
00782   return frame_unwind_pc (skip_artificial_frames (this_frame));
00783 }
00784 
00785 int
00786 frame_unwind_caller_pc_if_available (struct frame_info *this_frame,
00787                                      CORE_ADDR *pc)
00788 {
00789   return frame_unwind_pc_if_available (skip_artificial_frames (this_frame), pc);
00790 }
00791 
00792 int
00793 get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
00794 {
00795   struct frame_info *next_frame = this_frame->next;
00796 
00797   if (!next_frame->prev_func.p)
00798     {
00799       CORE_ADDR addr_in_block;
00800 
00801       /* Make certain that this, and not the adjacent, function is
00802          found.  */
00803       if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
00804         {
00805           next_frame->prev_func.p = -1;
00806           if (frame_debug)
00807             fprintf_unfiltered (gdb_stdlog,
00808                                 "{ get_frame_func (this_frame=%d)"
00809                                 " -> unavailable }\n",
00810                                 this_frame->level);
00811         }
00812       else
00813         {
00814           next_frame->prev_func.p = 1;
00815           next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
00816           if (frame_debug)
00817             fprintf_unfiltered (gdb_stdlog,
00818                                 "{ get_frame_func (this_frame=%d) -> %s }\n",
00819                                 this_frame->level,
00820                                 hex_string (next_frame->prev_func.addr));
00821         }
00822     }
00823 
00824   if (next_frame->prev_func.p < 0)
00825     {
00826       *pc = -1;
00827       return 0;
00828     }
00829   else
00830     {
00831       *pc = next_frame->prev_func.addr;
00832       return 1;
00833     }
00834 }
00835 
00836 CORE_ADDR
00837 get_frame_func (struct frame_info *this_frame)
00838 {
00839   CORE_ADDR pc;
00840 
00841   if (!get_frame_func_if_available (this_frame, &pc))
00842     throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
00843 
00844   return pc;
00845 }
00846 
00847 static enum register_status
00848 do_frame_register_read (void *src, int regnum, gdb_byte *buf)
00849 {
00850   if (!deprecated_frame_register_read (src, regnum, buf))
00851     return REG_UNAVAILABLE;
00852   else
00853     return REG_VALID;
00854 }
00855 
00856 struct regcache *
00857 frame_save_as_regcache (struct frame_info *this_frame)
00858 {
00859   struct address_space *aspace = get_frame_address_space (this_frame);
00860   struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame),
00861                                                 aspace);
00862   struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
00863 
00864   regcache_save (regcache, do_frame_register_read, this_frame);
00865   discard_cleanups (cleanups);
00866   return regcache;
00867 }
00868 
00869 void
00870 frame_pop (struct frame_info *this_frame)
00871 {
00872   struct frame_info *prev_frame;
00873   struct regcache *scratch;
00874   struct cleanup *cleanups;
00875 
00876   if (get_frame_type (this_frame) == DUMMY_FRAME)
00877     {
00878       /* Popping a dummy frame involves restoring more than just registers.
00879          dummy_frame_pop does all the work.  */
00880       dummy_frame_pop (get_frame_id (this_frame));
00881       return;
00882     }
00883 
00884   /* Ensure that we have a frame to pop to.  */
00885   prev_frame = get_prev_frame_1 (this_frame);
00886 
00887   if (!prev_frame)
00888     error (_("Cannot pop the initial frame."));
00889 
00890   /* Ignore TAILCALL_FRAME type frames, they were executed already before
00891      entering THISFRAME.  */
00892   while (get_frame_type (prev_frame) == TAILCALL_FRAME)
00893     prev_frame = get_prev_frame (prev_frame);
00894 
00895   /* Make a copy of all the register values unwound from this frame.
00896      Save them in a scratch buffer so that there isn't a race between
00897      trying to extract the old values from the current regcache while
00898      at the same time writing new values into that same cache.  */
00899   scratch = frame_save_as_regcache (prev_frame);
00900   cleanups = make_cleanup_regcache_xfree (scratch);
00901 
00902   /* FIXME: cagney/2003-03-16: It should be possible to tell the
00903      target's register cache that it is about to be hit with a burst
00904      register transfer and that the sequence of register writes should
00905      be batched.  The pair target_prepare_to_store() and
00906      target_store_registers() kind of suggest this functionality.
00907      Unfortunately, they don't implement it.  Their lack of a formal
00908      definition can lead to targets writing back bogus values
00909      (arguably a bug in the target code mind).  */
00910   /* Now copy those saved registers into the current regcache.
00911      Here, regcache_cpy() calls regcache_restore().  */
00912   regcache_cpy (get_current_regcache (), scratch);
00913   do_cleanups (cleanups);
00914 
00915   /* We've made right mess of GDB's local state, just discard
00916      everything.  */
00917   reinit_frame_cache ();
00918 }
00919 
00920 void
00921 frame_register_unwind (struct frame_info *frame, int regnum,
00922                        int *optimizedp, int *unavailablep,
00923                        enum lval_type *lvalp, CORE_ADDR *addrp,
00924                        int *realnump, gdb_byte *bufferp)
00925 {
00926   struct value *value;
00927 
00928   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
00929      that the value proper does not need to be fetched.  */
00930   gdb_assert (optimizedp != NULL);
00931   gdb_assert (lvalp != NULL);
00932   gdb_assert (addrp != NULL);
00933   gdb_assert (realnump != NULL);
00934   /* gdb_assert (bufferp != NULL); */
00935 
00936   value = frame_unwind_register_value (frame, regnum);
00937 
00938   gdb_assert (value != NULL);
00939 
00940   *optimizedp = value_optimized_out (value);
00941   *unavailablep = !value_entirely_available (value);
00942   *lvalp = VALUE_LVAL (value);
00943   *addrp = value_address (value);
00944   *realnump = VALUE_REGNUM (value);
00945 
00946   if (bufferp)
00947     {
00948       if (!*optimizedp && !*unavailablep)
00949         memcpy (bufferp, value_contents_all (value),
00950                 TYPE_LENGTH (value_type (value)));
00951       else
00952         memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
00953     }
00954 
00955   /* Dispose of the new value.  This prevents watchpoints from
00956      trying to watch the saved frame pointer.  */
00957   release_value (value);
00958   value_free (value);
00959 }
00960 
00961 void
00962 frame_register (struct frame_info *frame, int regnum,
00963                 int *optimizedp, int *unavailablep, enum lval_type *lvalp,
00964                 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
00965 {
00966   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
00967      that the value proper does not need to be fetched.  */
00968   gdb_assert (optimizedp != NULL);
00969   gdb_assert (lvalp != NULL);
00970   gdb_assert (addrp != NULL);
00971   gdb_assert (realnump != NULL);
00972   /* gdb_assert (bufferp != NULL); */
00973 
00974   /* Obtain the register value by unwinding the register from the next
00975      (more inner frame).  */
00976   gdb_assert (frame != NULL && frame->next != NULL);
00977   frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
00978                          lvalp, addrp, realnump, bufferp);
00979 }
00980 
00981 void
00982 frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
00983 {
00984   int optimized;
00985   int unavailable;
00986   CORE_ADDR addr;
00987   int realnum;
00988   enum lval_type lval;
00989 
00990   frame_register_unwind (frame, regnum, &optimized, &unavailable,
00991                          &lval, &addr, &realnum, buf);
00992 
00993   if (optimized)
00994     error (_("Register %d was optimized out"), regnum);
00995   if (unavailable)
00996     throw_error (NOT_AVAILABLE_ERROR,
00997                  _("Register %d is not available"), regnum);
00998 }
00999 
01000 void
01001 get_frame_register (struct frame_info *frame,
01002                     int regnum, gdb_byte *buf)
01003 {
01004   frame_unwind_register (frame->next, regnum, buf);
01005 }
01006 
01007 struct value *
01008 frame_unwind_register_value (struct frame_info *frame, int regnum)
01009 {
01010   struct gdbarch *gdbarch;
01011   struct value *value;
01012 
01013   gdb_assert (frame != NULL);
01014   gdbarch = frame_unwind_arch (frame);
01015 
01016   if (frame_debug)
01017     {
01018       fprintf_unfiltered (gdb_stdlog,
01019                           "{ frame_unwind_register_value "
01020                           "(frame=%d,regnum=%d(%s),...) ",
01021                           frame->level, regnum,
01022                           user_reg_map_regnum_to_name (gdbarch, regnum));
01023     }
01024 
01025   /* Find the unwinder.  */
01026   if (frame->unwind == NULL)
01027     frame_unwind_find_by_frame (frame, &frame->prologue_cache);
01028 
01029   /* Ask this frame to unwind its register.  */
01030   value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
01031 
01032   if (frame_debug)
01033     {
01034       fprintf_unfiltered (gdb_stdlog, "->");
01035       if (value_optimized_out (value))
01036         fprintf_unfiltered (gdb_stdlog, " optimized out");
01037       else
01038         {
01039           if (VALUE_LVAL (value) == lval_register)
01040             fprintf_unfiltered (gdb_stdlog, " register=%d",
01041                                 VALUE_REGNUM (value));
01042           else if (VALUE_LVAL (value) == lval_memory)
01043             fprintf_unfiltered (gdb_stdlog, " address=%s",
01044                                 paddress (gdbarch,
01045                                           value_address (value)));
01046           else
01047             fprintf_unfiltered (gdb_stdlog, " computed");
01048 
01049           if (value_lazy (value))
01050             fprintf_unfiltered (gdb_stdlog, " lazy");
01051           else
01052             {
01053               int i;
01054               const gdb_byte *buf = value_contents (value);
01055 
01056               fprintf_unfiltered (gdb_stdlog, " bytes=");
01057               fprintf_unfiltered (gdb_stdlog, "[");
01058               for (i = 0; i < register_size (gdbarch, regnum); i++)
01059                 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
01060               fprintf_unfiltered (gdb_stdlog, "]");
01061             }
01062         }
01063 
01064       fprintf_unfiltered (gdb_stdlog, " }\n");
01065     }
01066 
01067   return value;
01068 }
01069 
01070 struct value *
01071 get_frame_register_value (struct frame_info *frame, int regnum)
01072 {
01073   return frame_unwind_register_value (frame->next, regnum);
01074 }
01075 
01076 LONGEST
01077 frame_unwind_register_signed (struct frame_info *frame, int regnum)
01078 {
01079   struct gdbarch *gdbarch = frame_unwind_arch (frame);
01080   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01081   int size = register_size (gdbarch, regnum);
01082   gdb_byte buf[MAX_REGISTER_SIZE];
01083 
01084   frame_unwind_register (frame, regnum, buf);
01085   return extract_signed_integer (buf, size, byte_order);
01086 }
01087 
01088 LONGEST
01089 get_frame_register_signed (struct frame_info *frame, int regnum)
01090 {
01091   return frame_unwind_register_signed (frame->next, regnum);
01092 }
01093 
01094 ULONGEST
01095 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
01096 {
01097   struct gdbarch *gdbarch = frame_unwind_arch (frame);
01098   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01099   int size = register_size (gdbarch, regnum);
01100   gdb_byte buf[MAX_REGISTER_SIZE];
01101 
01102   frame_unwind_register (frame, regnum, buf);
01103   return extract_unsigned_integer (buf, size, byte_order);
01104 }
01105 
01106 ULONGEST
01107 get_frame_register_unsigned (struct frame_info *frame, int regnum)
01108 {
01109   return frame_unwind_register_unsigned (frame->next, regnum);
01110 }
01111 
01112 int
01113 read_frame_register_unsigned (struct frame_info *frame, int regnum,
01114                               ULONGEST *val)
01115 {
01116   struct value *regval = get_frame_register_value (frame, regnum);
01117 
01118   if (!value_optimized_out (regval)
01119       && value_entirely_available (regval))
01120     {
01121       struct gdbarch *gdbarch = get_frame_arch (frame);
01122       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01123       int size = register_size (gdbarch, VALUE_REGNUM (regval));
01124 
01125       *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
01126       return 1;
01127     }
01128 
01129   return 0;
01130 }
01131 
01132 void
01133 put_frame_register (struct frame_info *frame, int regnum,
01134                     const gdb_byte *buf)
01135 {
01136   struct gdbarch *gdbarch = get_frame_arch (frame);
01137   int realnum;
01138   int optim;
01139   int unavail;
01140   enum lval_type lval;
01141   CORE_ADDR addr;
01142 
01143   frame_register (frame, regnum, &optim, &unavail,
01144                   &lval, &addr, &realnum, NULL);
01145   if (optim)
01146     error (_("Attempt to assign to a register that was not saved."));
01147   switch (lval)
01148     {
01149     case lval_memory:
01150       {
01151         write_memory (addr, buf, register_size (gdbarch, regnum));
01152         break;
01153       }
01154     case lval_register:
01155       regcache_cooked_write (get_current_regcache (), realnum, buf);
01156       break;
01157     default:
01158       error (_("Attempt to assign to an unmodifiable value."));
01159     }
01160 }
01161 
01162 /* This function is deprecated.  Use get_frame_register_value instead,
01163    which provides more accurate information.
01164 
01165    Find and return the value of REGNUM for the specified stack frame.
01166    The number of bytes copied is REGISTER_SIZE (REGNUM).
01167 
01168    Returns 0 if the register value could not be found.  */
01169 
01170 int
01171 deprecated_frame_register_read (struct frame_info *frame, int regnum,
01172                      gdb_byte *myaddr)
01173 {
01174   int optimized;
01175   int unavailable;
01176   enum lval_type lval;
01177   CORE_ADDR addr;
01178   int realnum;
01179 
01180   frame_register (frame, regnum, &optimized, &unavailable,
01181                   &lval, &addr, &realnum, myaddr);
01182 
01183   return !optimized && !unavailable;
01184 }
01185 
01186 int
01187 get_frame_register_bytes (struct frame_info *frame, int regnum,
01188                           CORE_ADDR offset, int len, gdb_byte *myaddr,
01189                           int *optimizedp, int *unavailablep)
01190 {
01191   struct gdbarch *gdbarch = get_frame_arch (frame);
01192   int i;
01193   int maxsize;
01194   int numregs;
01195 
01196   /* Skip registers wholly inside of OFFSET.  */
01197   while (offset >= register_size (gdbarch, regnum))
01198     {
01199       offset -= register_size (gdbarch, regnum);
01200       regnum++;
01201     }
01202 
01203   /* Ensure that we will not read beyond the end of the register file.
01204      This can only ever happen if the debug information is bad.  */
01205   maxsize = -offset;
01206   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
01207   for (i = regnum; i < numregs; i++)
01208     {
01209       int thissize = register_size (gdbarch, i);
01210 
01211       if (thissize == 0)
01212         break;  /* This register is not available on this architecture.  */
01213       maxsize += thissize;
01214     }
01215   if (len > maxsize)
01216     error (_("Bad debug information detected: "
01217              "Attempt to read %d bytes from registers."), len);
01218 
01219   /* Copy the data.  */
01220   while (len > 0)
01221     {
01222       int curr_len = register_size (gdbarch, regnum) - offset;
01223 
01224       if (curr_len > len)
01225         curr_len = len;
01226 
01227       if (curr_len == register_size (gdbarch, regnum))
01228         {
01229           enum lval_type lval;
01230           CORE_ADDR addr;
01231           int realnum;
01232 
01233           frame_register (frame, regnum, optimizedp, unavailablep,
01234                           &lval, &addr, &realnum, myaddr);
01235           if (*optimizedp || *unavailablep)
01236             return 0;
01237         }
01238       else
01239         {
01240           gdb_byte buf[MAX_REGISTER_SIZE];
01241           enum lval_type lval;
01242           CORE_ADDR addr;
01243           int realnum;
01244 
01245           frame_register (frame, regnum, optimizedp, unavailablep,
01246                           &lval, &addr, &realnum, buf);
01247           if (*optimizedp || *unavailablep)
01248             return 0;
01249           memcpy (myaddr, buf + offset, curr_len);
01250         }
01251 
01252       myaddr += curr_len;
01253       len -= curr_len;
01254       offset = 0;
01255       regnum++;
01256     }
01257 
01258   *optimizedp = 0;
01259   *unavailablep = 0;
01260   return 1;
01261 }
01262 
01263 void
01264 put_frame_register_bytes (struct frame_info *frame, int regnum,
01265                           CORE_ADDR offset, int len, const gdb_byte *myaddr)
01266 {
01267   struct gdbarch *gdbarch = get_frame_arch (frame);
01268 
01269   /* Skip registers wholly inside of OFFSET.  */
01270   while (offset >= register_size (gdbarch, regnum))
01271     {
01272       offset -= register_size (gdbarch, regnum);
01273       regnum++;
01274     }
01275 
01276   /* Copy the data.  */
01277   while (len > 0)
01278     {
01279       int curr_len = register_size (gdbarch, regnum) - offset;
01280 
01281       if (curr_len > len)
01282         curr_len = len;
01283 
01284       if (curr_len == register_size (gdbarch, regnum))
01285         {
01286           put_frame_register (frame, regnum, myaddr);
01287         }
01288       else
01289         {
01290           gdb_byte buf[MAX_REGISTER_SIZE];
01291 
01292           deprecated_frame_register_read (frame, regnum, buf);
01293           memcpy (buf + offset, myaddr, curr_len);
01294           put_frame_register (frame, regnum, buf);
01295         }
01296 
01297       myaddr += curr_len;
01298       len -= curr_len;
01299       offset = 0;
01300       regnum++;
01301     }
01302 }
01303 
01304 /* Create a sentinel frame.  */
01305 
01306 static struct frame_info *
01307 create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
01308 {
01309   struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
01310 
01311   frame->level = -1;
01312   frame->pspace = pspace;
01313   frame->aspace = get_regcache_aspace (regcache);
01314   /* Explicitly initialize the sentinel frame's cache.  Provide it
01315      with the underlying regcache.  In the future additional
01316      information, such as the frame's thread will be added.  */
01317   frame->prologue_cache = sentinel_frame_cache (regcache);
01318   /* For the moment there is only one sentinel frame implementation.  */
01319   frame->unwind = &sentinel_frame_unwind;
01320   /* Link this frame back to itself.  The frame is self referential
01321      (the unwound PC is the same as the pc), so make it so.  */
01322   frame->next = frame;
01323   /* Make the sentinel frame's ID valid, but invalid.  That way all
01324      comparisons with it should fail.  */
01325   frame->this_id.p = 1;
01326   frame->this_id.value = null_frame_id;
01327   if (frame_debug)
01328     {
01329       fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
01330       fprint_frame (gdb_stdlog, frame);
01331       fprintf_unfiltered (gdb_stdlog, " }\n");
01332     }
01333   return frame;
01334 }
01335 
01336 /* Info about the innermost stack frame (contents of FP register).  */
01337 
01338 static struct frame_info *current_frame;
01339 
01340 /* Cache for frame addresses already read by gdb.  Valid only while
01341    inferior is stopped.  Control variables for the frame cache should
01342    be local to this module.  */
01343 
01344 static struct obstack frame_cache_obstack;
01345 
01346 void *
01347 frame_obstack_zalloc (unsigned long size)
01348 {
01349   void *data = obstack_alloc (&frame_cache_obstack, size);
01350 
01351   memset (data, 0, size);
01352   return data;
01353 }
01354 
01355 /* Return the innermost (currently executing) stack frame.  This is
01356    split into two functions.  The function unwind_to_current_frame()
01357    is wrapped in catch exceptions so that, even when the unwind of the
01358    sentinel frame fails, the function still returns a stack frame.  */
01359 
01360 static int
01361 unwind_to_current_frame (struct ui_out *ui_out, void *args)
01362 {
01363   struct frame_info *frame = get_prev_frame (args);
01364 
01365   /* A sentinel frame can fail to unwind, e.g., because its PC value
01366      lands in somewhere like start.  */
01367   if (frame == NULL)
01368     return 1;
01369   current_frame = frame;
01370   return 0;
01371 }
01372 
01373 struct frame_info *
01374 get_current_frame (void)
01375 {
01376   /* First check, and report, the lack of registers.  Having GDB
01377      report "No stack!" or "No memory" when the target doesn't even
01378      have registers is very confusing.  Besides, "printcmd.exp"
01379      explicitly checks that ``print $pc'' with no registers prints "No
01380      registers".  */
01381   if (!target_has_registers)
01382     error (_("No registers."));
01383   if (!target_has_stack)
01384     error (_("No stack."));
01385   if (!target_has_memory)
01386     error (_("No memory."));
01387   /* Traceframes are effectively a substitute for the live inferior.  */
01388   if (get_traceframe_number () < 0)
01389     {
01390       if (ptid_equal (inferior_ptid, null_ptid))
01391         error (_("No selected thread."));
01392       if (is_exited (inferior_ptid))
01393         error (_("Invalid selected thread."));
01394       if (is_executing (inferior_ptid))
01395         error (_("Target is executing."));
01396     }
01397 
01398   if (current_frame == NULL)
01399     {
01400       struct frame_info *sentinel_frame =
01401         create_sentinel_frame (current_program_space, get_current_regcache ());
01402       if (catch_exceptions (current_uiout, unwind_to_current_frame,
01403                             sentinel_frame, RETURN_MASK_ERROR) != 0)
01404         {
01405           /* Oops! Fake a current frame?  Is this useful?  It has a PC
01406              of zero, for instance.  */
01407           current_frame = sentinel_frame;
01408         }
01409     }
01410   return current_frame;
01411 }
01412 
01413 /* The "selected" stack frame is used by default for local and arg
01414    access.  May be zero, for no selected frame.  */
01415 
01416 static struct frame_info *selected_frame;
01417 
01418 int
01419 has_stack_frames (void)
01420 {
01421   if (!target_has_registers || !target_has_stack || !target_has_memory)
01422     return 0;
01423 
01424   /* Traceframes are effectively a substitute for the live inferior.  */
01425   if (get_traceframe_number () < 0)
01426     {
01427       /* No current inferior, no frame.  */
01428       if (ptid_equal (inferior_ptid, null_ptid))
01429         return 0;
01430 
01431       /* Don't try to read from a dead thread.  */
01432       if (is_exited (inferior_ptid))
01433         return 0;
01434 
01435       /* ... or from a spinning thread.  */
01436       if (is_executing (inferior_ptid))
01437         return 0;
01438     }
01439 
01440   return 1;
01441 }
01442 
01443 /* Return the selected frame.  Always non-NULL (unless there isn't an
01444    inferior sufficient for creating a frame) in which case an error is
01445    thrown.  */
01446 
01447 struct frame_info *
01448 get_selected_frame (const char *message)
01449 {
01450   if (selected_frame == NULL)
01451     {
01452       if (message != NULL && !has_stack_frames ())
01453         error (("%s"), message);
01454       /* Hey!  Don't trust this.  It should really be re-finding the
01455          last selected frame of the currently selected thread.  This,
01456          though, is better than nothing.  */
01457       select_frame (get_current_frame ());
01458     }
01459   /* There is always a frame.  */
01460   gdb_assert (selected_frame != NULL);
01461   return selected_frame;
01462 }
01463 
01464 /* If there is a selected frame, return it.  Otherwise, return NULL.  */
01465 
01466 struct frame_info *
01467 get_selected_frame_if_set (void)
01468 {
01469   return selected_frame;
01470 }
01471 
01472 /* This is a variant of get_selected_frame() which can be called when
01473    the inferior does not have a frame; in that case it will return
01474    NULL instead of calling error().  */
01475 
01476 struct frame_info *
01477 deprecated_safe_get_selected_frame (void)
01478 {
01479   if (!has_stack_frames ())
01480     return NULL;
01481   return get_selected_frame (NULL);
01482 }
01483 
01484 /* Select frame FI (or NULL - to invalidate the current frame).  */
01485 
01486 void
01487 select_frame (struct frame_info *fi)
01488 {
01489   selected_frame = fi;
01490   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
01491      frame is being invalidated.  */
01492   if (deprecated_selected_frame_level_changed_hook)
01493     deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
01494 
01495   /* FIXME: kseitz/2002-08-28: It would be nice to call
01496      selected_frame_level_changed_event() right here, but due to limitations
01497      in the current interfaces, we would end up flooding UIs with events
01498      because select_frame() is used extensively internally.
01499 
01500      Once we have frame-parameterized frame (and frame-related) commands,
01501      the event notification can be moved here, since this function will only
01502      be called when the user's selected frame is being changed.  */
01503 
01504   /* Ensure that symbols for this frame are read in.  Also, determine the
01505      source language of this frame, and switch to it if desired.  */
01506   if (fi)
01507     {
01508       CORE_ADDR pc;
01509 
01510       /* We retrieve the frame's symtab by using the frame PC.
01511          However we cannot use the frame PC as-is, because it usually
01512          points to the instruction following the "call", which is
01513          sometimes the first instruction of another function.  So we
01514          rely on get_frame_address_in_block() which provides us with a
01515          PC which is guaranteed to be inside the frame's code
01516          block.  */
01517       if (get_frame_address_in_block_if_available (fi, &pc))
01518         {
01519           struct symtab *s = find_pc_symtab (pc);
01520 
01521           if (s
01522               && s->language != current_language->la_language
01523               && s->language != language_unknown
01524               && language_mode == language_mode_auto)
01525             set_language (s->language);
01526         }
01527     }
01528 }
01529 
01530 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
01531    Always returns a non-NULL value.  */
01532 
01533 struct frame_info *
01534 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
01535 {
01536   struct frame_info *fi;
01537 
01538   if (frame_debug)
01539     {
01540       fprintf_unfiltered (gdb_stdlog,
01541                           "{ create_new_frame (addr=%s, pc=%s) ",
01542                           hex_string (addr), hex_string (pc));
01543     }
01544 
01545   fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
01546 
01547   fi->next = create_sentinel_frame (current_program_space,
01548                                     get_current_regcache ());
01549 
01550   /* Set/update this frame's cached PC value, found in the next frame.
01551      Do this before looking for this frame's unwinder.  A sniffer is
01552      very likely to read this, and the corresponding unwinder is
01553      entitled to rely that the PC doesn't magically change.  */
01554   fi->next->prev_pc.value = pc;
01555   fi->next->prev_pc.p = 1;
01556 
01557   /* We currently assume that frame chain's can't cross spaces.  */
01558   fi->pspace = fi->next->pspace;
01559   fi->aspace = fi->next->aspace;
01560 
01561   /* Select/initialize both the unwind function and the frame's type
01562      based on the PC.  */
01563   frame_unwind_find_by_frame (fi, &fi->prologue_cache);
01564 
01565   fi->this_id.p = 1;
01566   fi->this_id.value = frame_id_build (addr, pc);
01567 
01568   if (frame_debug)
01569     {
01570       fprintf_unfiltered (gdb_stdlog, "-> ");
01571       fprint_frame (gdb_stdlog, fi);
01572       fprintf_unfiltered (gdb_stdlog, " }\n");
01573     }
01574 
01575   return fi;
01576 }
01577 
01578 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
01579    innermost frame).  Be careful to not fall off the bottom of the
01580    frame chain and onto the sentinel frame.  */
01581 
01582 struct frame_info *
01583 get_next_frame (struct frame_info *this_frame)
01584 {
01585   if (this_frame->level > 0)
01586     return this_frame->next;
01587   else
01588     return NULL;
01589 }
01590 
01591 /* Observer for the target_changed event.  */
01592 
01593 static void
01594 frame_observer_target_changed (struct target_ops *target)
01595 {
01596   reinit_frame_cache ();
01597 }
01598 
01599 /* Flush the entire frame cache.  */
01600 
01601 void
01602 reinit_frame_cache (void)
01603 {
01604   struct frame_info *fi;
01605 
01606   /* Tear down all frame caches.  */
01607   for (fi = current_frame; fi != NULL; fi = fi->prev)
01608     {
01609       if (fi->prologue_cache && fi->unwind->dealloc_cache)
01610         fi->unwind->dealloc_cache (fi, fi->prologue_cache);
01611       if (fi->base_cache && fi->base->unwind->dealloc_cache)
01612         fi->base->unwind->dealloc_cache (fi, fi->base_cache);
01613     }
01614 
01615   /* Since we can't really be sure what the first object allocated was.  */
01616   obstack_free (&frame_cache_obstack, 0);
01617   obstack_init (&frame_cache_obstack);
01618 
01619   if (current_frame != NULL)
01620     annotate_frames_invalid ();
01621 
01622   current_frame = NULL;         /* Invalidate cache */
01623   select_frame (NULL);
01624   frame_stash_invalidate ();
01625   if (frame_debug)
01626     fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
01627 }
01628 
01629 /* Find where a register is saved (in memory or another register).
01630    The result of frame_register_unwind is just where it is saved
01631    relative to this particular frame.  */
01632 
01633 static void
01634 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
01635                                 int *optimizedp, enum lval_type *lvalp,
01636                                 CORE_ADDR *addrp, int *realnump)
01637 {
01638   gdb_assert (this_frame == NULL || this_frame->level >= 0);
01639 
01640   while (this_frame != NULL)
01641     {
01642       int unavailable;
01643 
01644       frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
01645                              lvalp, addrp, realnump, NULL);
01646 
01647       if (*optimizedp)
01648         break;
01649 
01650       if (*lvalp != lval_register)
01651         break;
01652 
01653       regnum = *realnump;
01654       this_frame = get_next_frame (this_frame);
01655     }
01656 }
01657 
01658 /* Return a "struct frame_info" corresponding to the frame that called
01659    THIS_FRAME.  Returns NULL if there is no such frame.
01660 
01661    Unlike get_prev_frame, this function always tries to unwind the
01662    frame.  */
01663 
01664 static struct frame_info *
01665 get_prev_frame_1 (struct frame_info *this_frame)
01666 {
01667   struct frame_id this_id;
01668   struct gdbarch *gdbarch;
01669 
01670   gdb_assert (this_frame != NULL);
01671   gdbarch = get_frame_arch (this_frame);
01672 
01673   if (frame_debug)
01674     {
01675       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
01676       if (this_frame != NULL)
01677         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
01678       else
01679         fprintf_unfiltered (gdb_stdlog, "<NULL>");
01680       fprintf_unfiltered (gdb_stdlog, ") ");
01681     }
01682 
01683   /* Only try to do the unwind once.  */
01684   if (this_frame->prev_p)
01685     {
01686       if (frame_debug)
01687         {
01688           fprintf_unfiltered (gdb_stdlog, "-> ");
01689           fprint_frame (gdb_stdlog, this_frame->prev);
01690           fprintf_unfiltered (gdb_stdlog, " // cached \n");
01691         }
01692       return this_frame->prev;
01693     }
01694 
01695   /* If the frame unwinder hasn't been selected yet, we must do so
01696      before setting prev_p; otherwise the check for misbehaved
01697      sniffers will think that this frame's sniffer tried to unwind
01698      further (see frame_cleanup_after_sniffer).  */
01699   if (this_frame->unwind == NULL)
01700     frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
01701 
01702   this_frame->prev_p = 1;
01703   this_frame->stop_reason = UNWIND_NO_REASON;
01704 
01705   /* If we are unwinding from an inline frame, all of the below tests
01706      were already performed when we unwound from the next non-inline
01707      frame.  We must skip them, since we can not get THIS_FRAME's ID
01708      until we have unwound all the way down to the previous non-inline
01709      frame.  */
01710   if (get_frame_type (this_frame) == INLINE_FRAME)
01711     return get_prev_frame_raw (this_frame);
01712 
01713   /* Check that this frame is unwindable.  If it isn't, don't try to
01714      unwind to the prev frame.  */
01715   this_frame->stop_reason
01716     = this_frame->unwind->stop_reason (this_frame,
01717                                        &this_frame->prologue_cache);
01718 
01719   if (this_frame->stop_reason != UNWIND_NO_REASON)
01720     return NULL;
01721 
01722   /* Check that this frame's ID was valid.  If it wasn't, don't try to
01723      unwind to the prev frame.  Be careful to not apply this test to
01724      the sentinel frame.  */
01725   this_id = get_frame_id (this_frame);
01726   if (this_frame->level >= 0 && frame_id_eq (this_id, outer_frame_id))
01727     {
01728       if (frame_debug)
01729         {
01730           fprintf_unfiltered (gdb_stdlog, "-> ");
01731           fprint_frame (gdb_stdlog, NULL);
01732           fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
01733         }
01734       this_frame->stop_reason = UNWIND_NULL_ID;
01735       return NULL;
01736     }
01737 
01738   /* Check that this frame's ID isn't inner to (younger, below, next)
01739      the next frame.  This happens when a frame unwind goes backwards.
01740      This check is valid only if this frame and the next frame are NORMAL.
01741      See the comment at frame_id_inner for details.  */
01742   if (get_frame_type (this_frame) == NORMAL_FRAME
01743       && this_frame->next->unwind->type == NORMAL_FRAME
01744       && frame_id_inner (get_frame_arch (this_frame->next), this_id,
01745                          get_frame_id (this_frame->next)))
01746     {
01747       CORE_ADDR this_pc_in_block;
01748       struct minimal_symbol *morestack_msym;
01749       const char *morestack_name = NULL;
01750       
01751       /* gcc -fsplit-stack __morestack can continue the stack anywhere.  */
01752       this_pc_in_block = get_frame_address_in_block (this_frame);
01753       morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
01754       if (morestack_msym)
01755         morestack_name = SYMBOL_LINKAGE_NAME (morestack_msym);
01756       if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
01757         {
01758           if (frame_debug)
01759             {
01760               fprintf_unfiltered (gdb_stdlog, "-> ");
01761               fprint_frame (gdb_stdlog, NULL);
01762               fprintf_unfiltered (gdb_stdlog,
01763                                   " // this frame ID is inner }\n");
01764             }
01765           this_frame->stop_reason = UNWIND_INNER_ID;
01766           return NULL;
01767         }
01768     }
01769 
01770   /* Check that this and the next frame are not identical.  If they
01771      are, there is most likely a stack cycle.  As with the inner-than
01772      test above, avoid comparing the inner-most and sentinel frames.  */
01773   if (this_frame->level > 0
01774       && frame_id_eq (this_id, get_frame_id (this_frame->next)))
01775     {
01776       if (frame_debug)
01777         {
01778           fprintf_unfiltered (gdb_stdlog, "-> ");
01779           fprint_frame (gdb_stdlog, NULL);
01780           fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
01781         }
01782       this_frame->stop_reason = UNWIND_SAME_ID;
01783       return NULL;
01784     }
01785 
01786   /* Check that this and the next frame do not unwind the PC register
01787      to the same memory location.  If they do, then even though they
01788      have different frame IDs, the new frame will be bogus; two
01789      functions can't share a register save slot for the PC.  This can
01790      happen when the prologue analyzer finds a stack adjustment, but
01791      no PC save.
01792 
01793      This check does assume that the "PC register" is roughly a
01794      traditional PC, even if the gdbarch_unwind_pc method adjusts
01795      it (we do not rely on the value, only on the unwound PC being
01796      dependent on this value).  A potential improvement would be
01797      to have the frame prev_pc method and the gdbarch unwind_pc
01798      method set the same lval and location information as
01799      frame_register_unwind.  */
01800   if (this_frame->level > 0
01801       && gdbarch_pc_regnum (gdbarch) >= 0
01802       && get_frame_type (this_frame) == NORMAL_FRAME
01803       && (get_frame_type (this_frame->next) == NORMAL_FRAME
01804           || get_frame_type (this_frame->next) == INLINE_FRAME))
01805     {
01806       int optimized, realnum, nrealnum;
01807       enum lval_type lval, nlval;
01808       CORE_ADDR addr, naddr;
01809 
01810       frame_register_unwind_location (this_frame,
01811                                       gdbarch_pc_regnum (gdbarch),
01812                                       &optimized, &lval, &addr, &realnum);
01813       frame_register_unwind_location (get_next_frame (this_frame),
01814                                       gdbarch_pc_regnum (gdbarch),
01815                                       &optimized, &nlval, &naddr, &nrealnum);
01816 
01817       if ((lval == lval_memory && lval == nlval && addr == naddr)
01818           || (lval == lval_register && lval == nlval && realnum == nrealnum))
01819         {
01820           if (frame_debug)
01821             {
01822               fprintf_unfiltered (gdb_stdlog, "-> ");
01823               fprint_frame (gdb_stdlog, NULL);
01824               fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
01825             }
01826 
01827           this_frame->stop_reason = UNWIND_NO_SAVED_PC;
01828           this_frame->prev = NULL;
01829           return NULL;
01830         }
01831     }
01832 
01833   return get_prev_frame_raw (this_frame);
01834 }
01835 
01836 /* Construct a new "struct frame_info" and link it previous to
01837    this_frame.  */
01838 
01839 static struct frame_info *
01840 get_prev_frame_raw (struct frame_info *this_frame)
01841 {
01842   struct frame_info *prev_frame;
01843 
01844   /* Allocate the new frame but do not wire it in to the frame chain.
01845      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
01846      frame->next to pull some fancy tricks (of course such code is, by
01847      definition, recursive).  Try to prevent it.
01848 
01849      There is no reason to worry about memory leaks, should the
01850      remainder of the function fail.  The allocated memory will be
01851      quickly reclaimed when the frame cache is flushed, and the `we've
01852      been here before' check above will stop repeated memory
01853      allocation calls.  */
01854   prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
01855   prev_frame->level = this_frame->level + 1;
01856 
01857   /* For now, assume we don't have frame chains crossing address
01858      spaces.  */
01859   prev_frame->pspace = this_frame->pspace;
01860   prev_frame->aspace = this_frame->aspace;
01861 
01862   /* Don't yet compute ->unwind (and hence ->type).  It is computed
01863      on-demand in get_frame_type, frame_register_unwind, and
01864      get_frame_id.  */
01865 
01866   /* Don't yet compute the frame's ID.  It is computed on-demand by
01867      get_frame_id().  */
01868 
01869   /* The unwound frame ID is validate at the start of this function,
01870      as part of the logic to decide if that frame should be further
01871      unwound, and not here while the prev frame is being created.
01872      Doing this makes it possible for the user to examine a frame that
01873      has an invalid frame ID.
01874 
01875      Some very old VAX code noted: [...]  For the sake of argument,
01876      suppose that the stack is somewhat trashed (which is one reason
01877      that "info frame" exists).  So, return 0 (indicating we don't
01878      know the address of the arglist) if we don't know what frame this
01879      frame calls.  */
01880 
01881   /* Link it in.  */
01882   this_frame->prev = prev_frame;
01883   prev_frame->next = this_frame;
01884 
01885   if (frame_debug)
01886     {
01887       fprintf_unfiltered (gdb_stdlog, "-> ");
01888       fprint_frame (gdb_stdlog, prev_frame);
01889       fprintf_unfiltered (gdb_stdlog, " }\n");
01890     }
01891 
01892   return prev_frame;
01893 }
01894 
01895 /* Debug routine to print a NULL frame being returned.  */
01896 
01897 static void
01898 frame_debug_got_null_frame (struct frame_info *this_frame,
01899                             const char *reason)
01900 {
01901   if (frame_debug)
01902     {
01903       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
01904       if (this_frame != NULL)
01905         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
01906       else
01907         fprintf_unfiltered (gdb_stdlog, "<NULL>");
01908       fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
01909     }
01910 }
01911 
01912 /* Is this (non-sentinel) frame in the "main"() function?  */
01913 
01914 static int
01915 inside_main_func (struct frame_info *this_frame)
01916 {
01917   struct minimal_symbol *msymbol;
01918   CORE_ADDR maddr;
01919 
01920   if (symfile_objfile == 0)
01921     return 0;
01922   msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
01923   if (msymbol == NULL)
01924     return 0;
01925   /* Make certain that the code, and not descriptor, address is
01926      returned.  */
01927   maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
01928                                               SYMBOL_VALUE_ADDRESS (msymbol),
01929                                               &current_target);
01930   return maddr == get_frame_func (this_frame);
01931 }
01932 
01933 /* Test whether THIS_FRAME is inside the process entry point function.  */
01934 
01935 static int
01936 inside_entry_func (struct frame_info *this_frame)
01937 {
01938   CORE_ADDR entry_point;
01939 
01940   if (!entry_point_address_query (&entry_point))
01941     return 0;
01942 
01943   return get_frame_func (this_frame) == entry_point;
01944 }
01945 
01946 /* Return a structure containing various interesting information about
01947    the frame that called THIS_FRAME.  Returns NULL if there is entier
01948    no such frame or the frame fails any of a set of target-independent
01949    condition that should terminate the frame chain (e.g., as unwinding
01950    past main()).
01951 
01952    This function should not contain target-dependent tests, such as
01953    checking whether the program-counter is zero.  */
01954 
01955 struct frame_info *
01956 get_prev_frame (struct frame_info *this_frame)
01957 {
01958   CORE_ADDR frame_pc;
01959   int frame_pc_p;
01960 
01961   /* There is always a frame.  If this assertion fails, suspect that
01962      something should be calling get_selected_frame() or
01963      get_current_frame().  */
01964   gdb_assert (this_frame != NULL);
01965   frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
01966 
01967   /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
01968      sense to stop unwinding at a dummy frame.  One place where a dummy
01969      frame may have an address "inside_main_func" is on HPUX.  On HPUX, the
01970      pcsqh register (space register for the instruction at the head of the
01971      instruction queue) cannot be written directly; the only way to set it
01972      is to branch to code that is in the target space.  In order to implement
01973      frame dummies on HPUX, the called function is made to jump back to where 
01974      the inferior was when the user function was called.  If gdb was inside 
01975      the main function when we created the dummy frame, the dummy frame will 
01976      point inside the main function.  */
01977   if (this_frame->level >= 0
01978       && get_frame_type (this_frame) == NORMAL_FRAME
01979       && !backtrace_past_main
01980       && frame_pc_p
01981       && inside_main_func (this_frame))
01982     /* Don't unwind past main().  Note, this is done _before_ the
01983        frame has been marked as previously unwound.  That way if the
01984        user later decides to enable unwinds past main(), that will
01985        automatically happen.  */
01986     {
01987       frame_debug_got_null_frame (this_frame, "inside main func");
01988       return NULL;
01989     }
01990 
01991   /* If the user's backtrace limit has been exceeded, stop.  We must
01992      add two to the current level; one of those accounts for backtrace_limit
01993      being 1-based and the level being 0-based, and the other accounts for
01994      the level of the new frame instead of the level of the current
01995      frame.  */
01996   if (this_frame->level + 2 > backtrace_limit)
01997     {
01998       frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
01999       return NULL;
02000     }
02001 
02002   /* If we're already inside the entry function for the main objfile,
02003      then it isn't valid.  Don't apply this test to a dummy frame -
02004      dummy frame PCs typically land in the entry func.  Don't apply
02005      this test to the sentinel frame.  Sentinel frames should always
02006      be allowed to unwind.  */
02007   /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
02008      wasn't checking for "main" in the minimal symbols.  With that
02009      fixed asm-source tests now stop in "main" instead of halting the
02010      backtrace in weird and wonderful ways somewhere inside the entry
02011      file.  Suspect that tests for inside the entry file/func were
02012      added to work around that (now fixed) case.  */
02013   /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
02014      suggested having the inside_entry_func test use the
02015      inside_main_func() msymbol trick (along with entry_point_address()
02016      I guess) to determine the address range of the start function.
02017      That should provide a far better stopper than the current
02018      heuristics.  */
02019   /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
02020      applied tail-call optimizations to main so that a function called 
02021      from main returns directly to the caller of main.  Since we don't
02022      stop at main, we should at least stop at the entry point of the
02023      application.  */
02024   if (this_frame->level >= 0
02025       && get_frame_type (this_frame) == NORMAL_FRAME
02026       && !backtrace_past_entry
02027       && frame_pc_p
02028       && inside_entry_func (this_frame))
02029     {
02030       frame_debug_got_null_frame (this_frame, "inside entry func");
02031       return NULL;
02032     }
02033 
02034   /* Assume that the only way to get a zero PC is through something
02035      like a SIGSEGV or a dummy frame, and hence that NORMAL frames
02036      will never unwind a zero PC.  */
02037   if (this_frame->level > 0
02038       && (get_frame_type (this_frame) == NORMAL_FRAME
02039           || get_frame_type (this_frame) == INLINE_FRAME)
02040       && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
02041       && frame_pc_p && frame_pc == 0)
02042     {
02043       frame_debug_got_null_frame (this_frame, "zero PC");
02044       return NULL;
02045     }
02046 
02047   return get_prev_frame_1 (this_frame);
02048 }
02049 
02050 CORE_ADDR
02051 get_frame_pc (struct frame_info *frame)
02052 {
02053   gdb_assert (frame->next != NULL);
02054   return frame_unwind_pc (frame->next);
02055 }
02056 
02057 int
02058 get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
02059 {
02060   volatile struct gdb_exception ex;
02061 
02062   gdb_assert (frame->next != NULL);
02063 
02064   TRY_CATCH (ex, RETURN_MASK_ERROR)
02065     {
02066       *pc = frame_unwind_pc (frame->next);
02067     }
02068   if (ex.reason < 0)
02069     {
02070       if (ex.error == NOT_AVAILABLE_ERROR)
02071         return 0;
02072       else
02073         throw_exception (ex);
02074     }
02075 
02076   return 1;
02077 }
02078 
02079 /* Return an address that falls within THIS_FRAME's code block.  */
02080 
02081 CORE_ADDR
02082 get_frame_address_in_block (struct frame_info *this_frame)
02083 {
02084   /* A draft address.  */
02085   CORE_ADDR pc = get_frame_pc (this_frame);
02086 
02087   struct frame_info *next_frame = this_frame->next;
02088 
02089   /* Calling get_frame_pc returns the resume address for THIS_FRAME.
02090      Normally the resume address is inside the body of the function
02091      associated with THIS_FRAME, but there is a special case: when
02092      calling a function which the compiler knows will never return
02093      (for instance abort), the call may be the very last instruction
02094      in the calling function.  The resume address will point after the
02095      call and may be at the beginning of a different function
02096      entirely.
02097 
02098      If THIS_FRAME is a signal frame or dummy frame, then we should
02099      not adjust the unwound PC.  For a dummy frame, GDB pushed the
02100      resume address manually onto the stack.  For a signal frame, the
02101      OS may have pushed the resume address manually and invoked the
02102      handler (e.g. GNU/Linux), or invoked the trampoline which called
02103      the signal handler - but in either case the signal handler is
02104      expected to return to the trampoline.  So in both of these
02105      cases we know that the resume address is executable and
02106      related.  So we only need to adjust the PC if THIS_FRAME
02107      is a normal function.
02108 
02109      If the program has been interrupted while THIS_FRAME is current,
02110      then clearly the resume address is inside the associated
02111      function.  There are three kinds of interruption: debugger stop
02112      (next frame will be SENTINEL_FRAME), operating system
02113      signal or exception (next frame will be SIGTRAMP_FRAME),
02114      or debugger-induced function call (next frame will be
02115      DUMMY_FRAME).  So we only need to adjust the PC if
02116      NEXT_FRAME is a normal function.
02117 
02118      We check the type of NEXT_FRAME first, since it is already
02119      known; frame type is determined by the unwinder, and since
02120      we have THIS_FRAME we've already selected an unwinder for
02121      NEXT_FRAME.
02122 
02123      If the next frame is inlined, we need to keep going until we find
02124      the real function - for instance, if a signal handler is invoked
02125      while in an inlined function, then the code address of the
02126      "calling" normal function should not be adjusted either.  */
02127 
02128   while (get_frame_type (next_frame) == INLINE_FRAME)
02129     next_frame = next_frame->next;
02130 
02131   if ((get_frame_type (next_frame) == NORMAL_FRAME
02132        || get_frame_type (next_frame) == TAILCALL_FRAME)
02133       && (get_frame_type (this_frame) == NORMAL_FRAME
02134           || get_frame_type (this_frame) == TAILCALL_FRAME
02135           || get_frame_type (this_frame) == INLINE_FRAME))
02136     return pc - 1;
02137 
02138   return pc;
02139 }
02140 
02141 int
02142 get_frame_address_in_block_if_available (struct frame_info *this_frame,
02143                                          CORE_ADDR *pc)
02144 {
02145   volatile struct gdb_exception ex;
02146 
02147   TRY_CATCH (ex, RETURN_MASK_ERROR)
02148     {
02149       *pc = get_frame_address_in_block (this_frame);
02150     }
02151   if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
02152     return 0;
02153   else if (ex.reason < 0)
02154     throw_exception (ex);
02155   else
02156     return 1;
02157 }
02158 
02159 void
02160 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
02161 {
02162   struct frame_info *next_frame;
02163   int notcurrent;
02164   CORE_ADDR pc;
02165 
02166   /* If the next frame represents an inlined function call, this frame's
02167      sal is the "call site" of that inlined function, which can not
02168      be inferred from get_frame_pc.  */
02169   next_frame = get_next_frame (frame);
02170   if (frame_inlined_callees (frame) > 0)
02171     {
02172       struct symbol *sym;
02173 
02174       if (next_frame)
02175         sym = get_frame_function (next_frame);
02176       else
02177         sym = inline_skipped_symbol (inferior_ptid);
02178 
02179       /* If frame is inline, it certainly has symbols.  */
02180       gdb_assert (sym);
02181       init_sal (sal);
02182       if (SYMBOL_LINE (sym) != 0)
02183         {
02184           sal->symtab = SYMBOL_SYMTAB (sym);
02185           sal->line = SYMBOL_LINE (sym);
02186         }
02187       else
02188         /* If the symbol does not have a location, we don't know where
02189            the call site is.  Do not pretend to.  This is jarring, but
02190            we can't do much better.  */
02191         sal->pc = get_frame_pc (frame);
02192 
02193       sal->pspace = get_frame_program_space (frame);
02194 
02195       return;
02196     }
02197 
02198   /* If FRAME is not the innermost frame, that normally means that
02199      FRAME->pc points at the return instruction (which is *after* the
02200      call instruction), and we want to get the line containing the
02201      call (because the call is where the user thinks the program is).
02202      However, if the next frame is either a SIGTRAMP_FRAME or a
02203      DUMMY_FRAME, then the next frame will contain a saved interrupt
02204      PC and such a PC indicates the current (rather than next)
02205      instruction/line, consequently, for such cases, want to get the
02206      line containing fi->pc.  */
02207   if (!get_frame_pc_if_available (frame, &pc))
02208     {
02209       init_sal (sal);
02210       return;
02211     }
02212 
02213   notcurrent = (pc != get_frame_address_in_block (frame));
02214   (*sal) = find_pc_line (pc, notcurrent);
02215 }
02216 
02217 /* Per "frame.h", return the ``address'' of the frame.  Code should
02218    really be using get_frame_id().  */
02219 CORE_ADDR
02220 get_frame_base (struct frame_info *fi)
02221 {
02222   return get_frame_id (fi).stack_addr;
02223 }
02224 
02225 /* High-level offsets into the frame.  Used by the debug info.  */
02226 
02227 CORE_ADDR
02228 get_frame_base_address (struct frame_info *fi)
02229 {
02230   if (get_frame_type (fi) != NORMAL_FRAME)
02231     return 0;
02232   if (fi->base == NULL)
02233     fi->base = frame_base_find_by_frame (fi);
02234   /* Sneaky: If the low-level unwind and high-level base code share a
02235      common unwinder, let them share the prologue cache.  */
02236   if (fi->base->unwind == fi->unwind)
02237     return fi->base->this_base (fi, &fi->prologue_cache);
02238   return fi->base->this_base (fi, &fi->base_cache);
02239 }
02240 
02241 CORE_ADDR
02242 get_frame_locals_address (struct frame_info *fi)
02243 {
02244   if (get_frame_type (fi) != NORMAL_FRAME)
02245     return 0;
02246   /* If there isn't a frame address method, find it.  */
02247   if (fi->base == NULL)
02248     fi->base = frame_base_find_by_frame (fi);
02249   /* Sneaky: If the low-level unwind and high-level base code share a
02250      common unwinder, let them share the prologue cache.  */
02251   if (fi->base->unwind == fi->unwind)
02252     return fi->base->this_locals (fi, &fi->prologue_cache);
02253   return fi->base->this_locals (fi, &fi->base_cache);
02254 }
02255 
02256 CORE_ADDR
02257 get_frame_args_address (struct frame_info *fi)
02258 {
02259   if (get_frame_type (fi) != NORMAL_FRAME)
02260     return 0;
02261   /* If there isn't a frame address method, find it.  */
02262   if (fi->base == NULL)
02263     fi->base = frame_base_find_by_frame (fi);
02264   /* Sneaky: If the low-level unwind and high-level base code share a
02265      common unwinder, let them share the prologue cache.  */
02266   if (fi->base->unwind == fi->unwind)
02267     return fi->base->this_args (fi, &fi->prologue_cache);
02268   return fi->base->this_args (fi, &fi->base_cache);
02269 }
02270 
02271 /* Return true if the frame unwinder for frame FI is UNWINDER; false
02272    otherwise.  */
02273 
02274 int
02275 frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
02276 {
02277   if (fi->unwind == NULL)
02278     frame_unwind_find_by_frame (fi, &fi->prologue_cache);
02279   return fi->unwind == unwinder;
02280 }
02281 
02282 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
02283    or -1 for a NULL frame.  */
02284 
02285 int
02286 frame_relative_level (struct frame_info *fi)
02287 {
02288   if (fi == NULL)
02289     return -1;
02290   else
02291     return fi->level;
02292 }
02293 
02294 enum frame_type
02295 get_frame_type (struct frame_info *frame)
02296 {
02297   if (frame->unwind == NULL)
02298     /* Initialize the frame's unwinder because that's what
02299        provides the frame's type.  */
02300     frame_unwind_find_by_frame (frame, &frame->prologue_cache);
02301   return frame->unwind->type;
02302 }
02303 
02304 struct program_space *
02305 get_frame_program_space (struct frame_info *frame)
02306 {
02307   return frame->pspace;
02308 }
02309 
02310 struct program_space *
02311 frame_unwind_program_space (struct frame_info *this_frame)
02312 {
02313   gdb_assert (this_frame);
02314 
02315   /* This is really a placeholder to keep the API consistent --- we
02316      assume for now that we don't have frame chains crossing
02317      spaces.  */
02318   return this_frame->pspace;
02319 }
02320 
02321 struct address_space *
02322 get_frame_address_space (struct frame_info *frame)
02323 {
02324   return frame->aspace;
02325 }
02326 
02327 /* Memory access methods.  */
02328 
02329 void
02330 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
02331                   gdb_byte *buf, int len)
02332 {
02333   read_memory (addr, buf, len);
02334 }
02335 
02336 LONGEST
02337 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
02338                          int len)
02339 {
02340   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02341   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02342 
02343   return read_memory_integer (addr, len, byte_order);
02344 }
02345 
02346 ULONGEST
02347 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
02348                            int len)
02349 {
02350   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02351   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02352 
02353   return read_memory_unsigned_integer (addr, len, byte_order);
02354 }
02355 
02356 int
02357 safe_frame_unwind_memory (struct frame_info *this_frame,
02358                           CORE_ADDR addr, gdb_byte *buf, int len)
02359 {
02360   /* NOTE: target_read_memory returns zero on success!  */
02361   return !target_read_memory (addr, buf, len);
02362 }
02363 
02364 /* Architecture methods.  */
02365 
02366 struct gdbarch *
02367 get_frame_arch (struct frame_info *this_frame)
02368 {
02369   return frame_unwind_arch (this_frame->next);
02370 }
02371 
02372 struct gdbarch *
02373 frame_unwind_arch (struct frame_info *next_frame)
02374 {
02375   if (!next_frame->prev_arch.p)
02376     {
02377       struct gdbarch *arch;
02378 
02379       if (next_frame->unwind == NULL)
02380         frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
02381 
02382       if (next_frame->unwind->prev_arch != NULL)
02383         arch = next_frame->unwind->prev_arch (next_frame,
02384                                               &next_frame->prologue_cache);
02385       else
02386         arch = get_frame_arch (next_frame);
02387 
02388       next_frame->prev_arch.arch = arch;
02389       next_frame->prev_arch.p = 1;
02390       if (frame_debug)
02391         fprintf_unfiltered (gdb_stdlog,
02392                             "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
02393                             next_frame->level,
02394                             gdbarch_bfd_arch_info (arch)->printable_name);
02395     }
02396 
02397   return next_frame->prev_arch.arch;
02398 }
02399 
02400 struct gdbarch *
02401 frame_unwind_caller_arch (struct frame_info *next_frame)
02402 {
02403   return frame_unwind_arch (skip_artificial_frames (next_frame));
02404 }
02405 
02406 /* Stack pointer methods.  */
02407 
02408 CORE_ADDR
02409 get_frame_sp (struct frame_info *this_frame)
02410 {
02411   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02412 
02413   /* Normality - an architecture that provides a way of obtaining any
02414      frame inner-most address.  */
02415   if (gdbarch_unwind_sp_p (gdbarch))
02416     /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
02417        operate on THIS_FRAME now.  */
02418     return gdbarch_unwind_sp (gdbarch, this_frame->next);
02419   /* Now things are really are grim.  Hope that the value returned by
02420      the gdbarch_sp_regnum register is meaningful.  */
02421   if (gdbarch_sp_regnum (gdbarch) >= 0)
02422     return get_frame_register_unsigned (this_frame,
02423                                         gdbarch_sp_regnum (gdbarch));
02424   internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
02425 }
02426 
02427 /* Return the reason why we can't unwind past FRAME.  */
02428 
02429 enum unwind_stop_reason
02430 get_frame_unwind_stop_reason (struct frame_info *frame)
02431 {
02432   /* If we haven't tried to unwind past this point yet, then assume
02433      that unwinding would succeed.  */
02434   if (frame->prev_p == 0)
02435     return UNWIND_NO_REASON;
02436 
02437   /* Otherwise, we set a reason when we succeeded (or failed) to
02438      unwind.  */
02439   return frame->stop_reason;
02440 }
02441 
02442 /* Return a string explaining REASON.  */
02443 
02444 const char *
02445 frame_stop_reason_string (enum unwind_stop_reason reason)
02446 {
02447   switch (reason)
02448     {
02449 #define SET(name, description) \
02450     case name: return _(description);
02451 #include "unwind_stop_reasons.def"
02452 #undef SET
02453 
02454     default:
02455       internal_error (__FILE__, __LINE__,
02456                       "Invalid frame stop reason");
02457     }
02458 }
02459 
02460 /* Clean up after a failed (wrong unwinder) attempt to unwind past
02461    FRAME.  */
02462 
02463 static void
02464 frame_cleanup_after_sniffer (void *arg)
02465 {
02466   struct frame_info *frame = arg;
02467 
02468   /* The sniffer should not allocate a prologue cache if it did not
02469      match this frame.  */
02470   gdb_assert (frame->prologue_cache == NULL);
02471 
02472   /* No sniffer should extend the frame chain; sniff based on what is
02473      already certain.  */
02474   gdb_assert (!frame->prev_p);
02475 
02476   /* The sniffer should not check the frame's ID; that's circular.  */
02477   gdb_assert (!frame->this_id.p);
02478 
02479   /* Clear cached fields dependent on the unwinder.
02480 
02481      The previous PC is independent of the unwinder, but the previous
02482      function is not (see get_frame_address_in_block).  */
02483   frame->prev_func.p = 0;
02484   frame->prev_func.addr = 0;
02485 
02486   /* Discard the unwinder last, so that we can easily find it if an assertion
02487      in this function triggers.  */
02488   frame->unwind = NULL;
02489 }
02490 
02491 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
02492    Return a cleanup which should be called if unwinding fails, and
02493    discarded if it succeeds.  */
02494 
02495 struct cleanup *
02496 frame_prepare_for_sniffer (struct frame_info *frame,
02497                            const struct frame_unwind *unwind)
02498 {
02499   gdb_assert (frame->unwind == NULL);
02500   frame->unwind = unwind;
02501   return make_cleanup (frame_cleanup_after_sniffer, frame);
02502 }
02503 
02504 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
02505 
02506 static struct cmd_list_element *set_backtrace_cmdlist;
02507 static struct cmd_list_element *show_backtrace_cmdlist;
02508 
02509 static void
02510 set_backtrace_cmd (char *args, int from_tty)
02511 {
02512   help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
02513 }
02514 
02515 static void
02516 show_backtrace_cmd (char *args, int from_tty)
02517 {
02518   cmd_show_list (show_backtrace_cmdlist, from_tty, "");
02519 }
02520 
02521 void
02522 _initialize_frame (void)
02523 {
02524   obstack_init (&frame_cache_obstack);
02525 
02526   frame_stash_create ();
02527 
02528   observer_attach_target_changed (frame_observer_target_changed);
02529 
02530   add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
02531 Set backtrace specific variables.\n\
02532 Configure backtrace variables such as the backtrace limit"),
02533                   &set_backtrace_cmdlist, "set backtrace ",
02534                   0/*allow-unknown*/, &setlist);
02535   add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
02536 Show backtrace specific variables\n\
02537 Show backtrace variables such as the backtrace limit"),
02538                   &show_backtrace_cmdlist, "show backtrace ",
02539                   0/*allow-unknown*/, &showlist);
02540 
02541   add_setshow_boolean_cmd ("past-main", class_obscure,
02542                            &backtrace_past_main, _("\
02543 Set whether backtraces should continue past \"main\"."), _("\
02544 Show whether backtraces should continue past \"main\"."), _("\
02545 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
02546 the backtrace at \"main\".  Set this variable if you need to see the rest\n\
02547 of the stack trace."),
02548                            NULL,
02549                            show_backtrace_past_main,
02550                            &set_backtrace_cmdlist,
02551                            &show_backtrace_cmdlist);
02552 
02553   add_setshow_boolean_cmd ("past-entry", class_obscure,
02554                            &backtrace_past_entry, _("\
02555 Set whether backtraces should continue past the entry point of a program."),
02556                            _("\
02557 Show whether backtraces should continue past the entry point of a program."),
02558                            _("\
02559 Normally there are no callers beyond the entry point of a program, so GDB\n\
02560 will terminate the backtrace there.  Set this variable if you need to see\n\
02561 the rest of the stack trace."),
02562                            NULL,
02563                            show_backtrace_past_entry,
02564                            &set_backtrace_cmdlist,
02565                            &show_backtrace_cmdlist);
02566 
02567   add_setshow_uinteger_cmd ("limit", class_obscure,
02568                             &backtrace_limit, _("\
02569 Set an upper bound on the number of backtrace levels."), _("\
02570 Show the upper bound on the number of backtrace levels."), _("\
02571 No more than the specified number of frames can be displayed or examined.\n\
02572 Literal \"unlimited\" or zero means no limit."),
02573                             NULL,
02574                             show_backtrace_limit,
02575                             &set_backtrace_cmdlist,
02576                             &show_backtrace_cmdlist);
02577 
02578   /* Debug this files internals.  */
02579   add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug,  _("\
02580 Set frame debugging."), _("\
02581 Show frame debugging."), _("\
02582 When non-zero, frame specific internal debugging is enabled."),
02583                              NULL,
02584                              show_frame_debug,
02585                              &setdebuglist, &showdebuglist);
02586 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines