2004-05-01 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / frame.c
1 /* Cache and manage frames for GDB, the GNU debugger.
2
3    Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4    2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "target.h"
26 #include "value.h"
27 #include "inferior.h"   /* for inferior_ptid */
28 #include "regcache.h"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include "user-regs.h"
32 #include "gdb_obstack.h"
33 #include "dummy-frame.h"
34 #include "sentinel-frame.h"
35 #include "gdbcore.h"
36 #include "annotate.h"
37 #include "language.h"
38 #include "frame-unwind.h"
39 #include "frame-base.h"
40 #include "command.h"
41 #include "gdbcmd.h"
42 #include "observer.h"
43
44 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
45
46 /* We keep a cache of stack frames, each of which is a "struct
47    frame_info".  The innermost one gets allocated (in
48    wait_for_inferior) each time the inferior stops; current_frame
49    points to it.  Additional frames get allocated (in get_prev_frame)
50    as needed, and are chained through the next and prev fields.  Any
51    time that the frame cache becomes invalid (most notably when we
52    execute something, but also if we change how we interpret the
53    frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
54    which reads new symbols)), we should call reinit_frame_cache.  */
55
56 struct frame_info
57 {
58   /* Level of this frame.  The inner-most (youngest) frame is at level
59      0.  As you move towards the outer-most (oldest) frame, the level
60      increases.  This is a cached value.  It could just as easily be
61      computed by counting back from the selected frame to the inner
62      most frame.  */
63   /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
64      reserved to indicate a bogus frame - one that has been created
65      just to keep GDB happy (GDB always needs a frame).  For the
66      moment leave this as speculation.  */
67   int level;
68
69   /* The frame's type.  */
70   /* FIXME: cagney/2004-05-01: Should instead just use ->unwind->type.
71      Unfortunately, legacy_get_prev_frame is still explicitly setting
72      the type.  Eliminate that method and this field can be
73      eliminated.  */
74   enum frame_type type;
75
76   /* For each register, address of where it was saved on entry to the
77      frame, or zero if it was not saved on entry to this frame.  This
78      includes special registers such as pc and fp saved in special
79      ways in the stack frame.  The SP_REGNUM is even more special, the
80      address here is the sp for the previous frame, not the address
81      where the sp was saved.  */
82   /* Allocated by frame_saved_regs_zalloc () which is called /
83      initialized by DEPRECATED_FRAME_INIT_SAVED_REGS(). */
84   CORE_ADDR *saved_regs;        /*NUM_REGS + NUM_PSEUDO_REGS*/
85
86   /* Anything extra for this structure that may have been defined in
87      the machine dependent files. */
88   /* Allocated by frame_extra_info_zalloc () which is called /
89      initialized by DEPRECATED_INIT_EXTRA_FRAME_INFO */
90   struct frame_extra_info *extra_info;
91
92   /* The frame's low-level unwinder and corresponding cache.  The
93      low-level unwinder is responsible for unwinding register values
94      for the previous frame.  The low-level unwind methods are
95      selected based on the presence, or otherwise, of register unwind
96      information such as CFI.  */
97   void *prologue_cache;
98   const struct frame_unwind *unwind;
99
100   /* Cached copy of the previous frame's resume address.  */
101   struct {
102     int p;
103     CORE_ADDR value;
104   } prev_pc;
105   
106   /* Cached copy of the previous frame's function address.  */
107   struct
108   {
109     CORE_ADDR addr;
110     int p;
111   } prev_func;
112   
113   /* This frame's ID.  */
114   struct
115   {
116     int p;
117     struct frame_id value;
118   } this_id;
119   
120   /* The frame's high-level base methods, and corresponding cache.
121      The high level base methods are selected based on the frame's
122      debug info.  */
123   const struct frame_base *base;
124   void *base_cache;
125
126   /* Pointers to the next (down, inner, younger) and previous (up,
127      outer, older) frame_info's in the frame cache.  */
128   struct frame_info *next; /* down, inner, younger */
129   int prev_p;
130   struct frame_info *prev; /* up, outer, older */
131 };
132
133 /* Flag to control debugging.  */
134
135 static int frame_debug;
136
137 /* Flag to indicate whether backtraces should stop at main et.al.  */
138
139 static int backtrace_past_main;
140 static unsigned int backtrace_limit = UINT_MAX;
141
142
143 void
144 fprint_frame_id (struct ui_file *file, struct frame_id id)
145 {
146   fprintf_unfiltered (file, "{stack=0x%s,code=0x%s,special=0x%s}",
147                       paddr_nz (id.stack_addr),
148                       paddr_nz (id.code_addr),
149                       paddr_nz (id.special_addr));
150 }
151
152 static void
153 fprint_frame_type (struct ui_file *file, enum frame_type type)
154 {
155   switch (type)
156     {
157     case UNKNOWN_FRAME:
158       fprintf_unfiltered (file, "UNKNOWN_FRAME");
159       return;
160     case NORMAL_FRAME:
161       fprintf_unfiltered (file, "NORMAL_FRAME");
162       return;
163     case DUMMY_FRAME:
164       fprintf_unfiltered (file, "DUMMY_FRAME");
165       return;
166     case SIGTRAMP_FRAME:
167       fprintf_unfiltered (file, "SIGTRAMP_FRAME");
168       return;
169     default:
170       fprintf_unfiltered (file, "<unknown type>");
171       return;
172     };
173 }
174
175 static void
176 fprint_frame (struct ui_file *file, struct frame_info *fi)
177 {
178   if (fi == NULL)
179     {
180       fprintf_unfiltered (file, "<NULL frame>");
181       return;
182     }
183   fprintf_unfiltered (file, "{");
184   fprintf_unfiltered (file, "level=%d", fi->level);
185   fprintf_unfiltered (file, ",");
186   fprintf_unfiltered (file, "type=");
187   fprint_frame_type (file, fi->type);
188   fprintf_unfiltered (file, ",");
189   fprintf_unfiltered (file, "unwind=");
190   if (fi->unwind != NULL)
191     gdb_print_host_address (fi->unwind, file);
192   else
193     fprintf_unfiltered (file, "<unknown>");
194   fprintf_unfiltered (file, ",");
195   fprintf_unfiltered (file, "pc=");
196   if (fi->next != NULL && fi->next->prev_pc.p)
197     fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
198   else
199     fprintf_unfiltered (file, "<unknown>");
200   fprintf_unfiltered (file, ",");
201   fprintf_unfiltered (file, "id=");
202   if (fi->this_id.p)
203     fprint_frame_id (file, fi->this_id.value);
204   else
205     fprintf_unfiltered (file, "<unknown>");
206   fprintf_unfiltered (file, ",");
207   fprintf_unfiltered (file, "func=");
208   if (fi->next != NULL && fi->next->prev_func.p)
209     fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
210   else
211     fprintf_unfiltered (file, "<unknown>");
212   fprintf_unfiltered (file, "}");
213 }
214
215 /* Return a frame uniq ID that can be used to, later, re-find the
216    frame.  */
217
218 struct frame_id
219 get_frame_id (struct frame_info *fi)
220 {
221   if (fi == NULL)
222     {
223       return null_frame_id;
224     }
225   if (!fi->this_id.p)
226     {
227       gdb_assert (!legacy_frame_p (current_gdbarch));
228       if (frame_debug)
229         fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
230                             fi->level);
231       /* Find the unwinder.  */
232       if (fi->unwind == NULL)
233         {
234           fi->unwind = frame_unwind_find_by_frame (fi->next,
235                                                    &fi->prologue_cache);
236           /* FIXME: cagney/2004-05-01: Should instead just use
237              ->unwind->type.  Unfortunately, legacy_get_prev_frame is
238              still explicitly setting the type.  Eliminate that method
239              and this field can be eliminated.  */
240           fi->type = fi->unwind->type;
241         }
242       /* Find THIS frame's ID.  */
243       fi->unwind->this_id (fi->next, &fi->prologue_cache, &fi->this_id.value);
244       fi->this_id.p = 1;
245       if (frame_debug)
246         {
247           fprintf_unfiltered (gdb_stdlog, "-> ");
248           fprint_frame_id (gdb_stdlog, fi->this_id.value);
249           fprintf_unfiltered (gdb_stdlog, " }\n");
250         }
251     }
252   return fi->this_id.value;
253 }
254
255 struct frame_id
256 frame_unwind_id (struct frame_info *next_frame)
257 {
258   /* Use prev_frame, and not get_prev_frame.  The latter will truncate
259      the frame chain, leading to this function unintentionally
260      returning a null_frame_id (e.g., when a caller requests the frame
261      ID of "main()"s caller.  */
262   return get_frame_id (get_prev_frame_1 (next_frame));
263 }
264
265 const struct frame_id null_frame_id; /* All zeros.  */
266
267 struct frame_id
268 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
269                         CORE_ADDR special_addr)
270 {
271   struct frame_id id;
272   id.stack_addr = stack_addr;
273   id.code_addr = code_addr;
274   id.special_addr = special_addr;
275   return id;
276 }
277
278 struct frame_id
279 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
280 {
281   return frame_id_build_special (stack_addr, code_addr, 0);
282 }
283
284 int
285 frame_id_p (struct frame_id l)
286 {
287   int p;
288   /* The .code can be NULL but the .stack cannot.  */
289   p = (l.stack_addr != 0);
290   if (frame_debug)
291     {
292       fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
293       fprint_frame_id (gdb_stdlog, l);
294       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
295     }
296   return p;
297 }
298
299 int
300 frame_id_eq (struct frame_id l, struct frame_id r)
301 {
302   int eq;
303   if (l.stack_addr == 0 || r.stack_addr == 0)
304     /* Like a NaN, if either ID is invalid, the result is false.  */
305     eq = 0;
306   else if (l.stack_addr != r.stack_addr)
307     /* If .stack addresses are different, the frames are different.  */
308     eq = 0;
309   else if (l.code_addr == 0 || r.code_addr == 0)
310     /* A zero code addr is a wild card, always succeed.  */
311     eq = 1;
312   else if (l.code_addr != r.code_addr)
313     /* If .code addresses are different, the frames are different.  */
314     eq = 0;
315   else if (l.special_addr == 0 || r.special_addr == 0)
316     /* A zero special addr is a wild card (or unused), always succeed.  */
317     eq = 1;
318   else if (l.special_addr == r.special_addr)
319     /* Frames are equal.  */
320     eq = 1;
321   else
322     /* No luck.  */
323     eq = 0;
324   if (frame_debug)
325     {
326       fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
327       fprint_frame_id (gdb_stdlog, l);
328       fprintf_unfiltered (gdb_stdlog, ",r=");
329       fprint_frame_id (gdb_stdlog, r);
330       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
331     }
332   return eq;
333 }
334
335 int
336 frame_id_inner (struct frame_id l, struct frame_id r)
337 {
338   int inner;
339   if (l.stack_addr == 0 || r.stack_addr == 0)
340     /* Like NaN, any operation involving an invalid ID always fails.  */
341     inner = 0;
342   else
343     /* Only return non-zero when strictly inner than.  Note that, per
344        comment in "frame.h", there is some fuzz here.  Frameless
345        functions are not strictly inner than (same .stack but
346        different .code and/or .special address).  */
347     inner = INNER_THAN (l.stack_addr, r.stack_addr);
348   if (frame_debug)
349     {
350       fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
351       fprint_frame_id (gdb_stdlog, l);
352       fprintf_unfiltered (gdb_stdlog, ",r=");
353       fprint_frame_id (gdb_stdlog, r);
354       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
355     }
356   return inner;
357 }
358
359 struct frame_info *
360 frame_find_by_id (struct frame_id id)
361 {
362   struct frame_info *frame;
363
364   /* ZERO denotes the null frame, let the caller decide what to do
365      about it.  Should it instead return get_current_frame()?  */
366   if (!frame_id_p (id))
367     return NULL;
368
369   for (frame = get_current_frame ();
370        frame != NULL;
371        frame = get_prev_frame (frame))
372     {
373       struct frame_id this = get_frame_id (frame);
374       if (frame_id_eq (id, this))
375         /* An exact match.  */
376         return frame;
377       if (frame_id_inner (id, this))
378         /* Gone to far.  */
379         return NULL;
380       /* Either we're not yet gone far enough out along the frame
381          chain (inner(this,id)), or we're comparing frameless functions
382          (same .base, different .func, no test available).  Struggle
383          on until we've definitly gone to far.  */
384     }
385   return NULL;
386 }
387
388 CORE_ADDR
389 frame_pc_unwind (struct frame_info *this_frame)
390 {
391   if (!this_frame->prev_pc.p)
392     {
393       CORE_ADDR pc;
394       if (gdbarch_unwind_pc_p (current_gdbarch))
395         {
396           /* The right way.  The `pure' way.  The one true way.  This
397              method depends solely on the register-unwind code to
398              determine the value of registers in THIS frame, and hence
399              the value of this frame's PC (resume address).  A typical
400              implementation is no more than:
401            
402              frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
403              return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
404
405              Note: this method is very heavily dependent on a correct
406              register-unwind implementation, it pays to fix that
407              method first; this method is frame type agnostic, since
408              it only deals with register values, it works with any
409              frame.  This is all in stark contrast to the old
410              FRAME_SAVED_PC which would try to directly handle all the
411              different ways that a PC could be unwound.  */
412           pc = gdbarch_unwind_pc (current_gdbarch, this_frame);
413         }
414       else if (this_frame->level < 0)
415         {
416           /* FIXME: cagney/2003-03-06: Old code and a sentinel
417              frame.  Do like was always done.  Fetch the PC's value
418              directly from the global registers array (via read_pc).
419              This assumes that this frame belongs to the current
420              global register cache.  The assumption is dangerous.  */
421           pc = read_pc ();
422         }
423       else if (DEPRECATED_FRAME_SAVED_PC_P ())
424         {
425           /* FIXME: cagney/2003-03-06: Old code, but not a sentinel
426              frame.  Do like was always done.  Note that this method,
427              unlike unwind_pc(), tries to handle all the different
428              frame cases directly.  It fails.  */
429           pc = DEPRECATED_FRAME_SAVED_PC (this_frame);
430         }
431       else
432         internal_error (__FILE__, __LINE__, "No gdbarch_unwind_pc method");
433       this_frame->prev_pc.value = pc;
434       this_frame->prev_pc.p = 1;
435       if (frame_debug)
436         fprintf_unfiltered (gdb_stdlog,
437                             "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
438                             this_frame->level,
439                             paddr_nz (this_frame->prev_pc.value));
440     }
441   return this_frame->prev_pc.value;
442 }
443
444 CORE_ADDR
445 frame_func_unwind (struct frame_info *fi)
446 {
447   if (!fi->prev_func.p)
448     {
449       /* Make certain that this, and not the adjacent, function is
450          found.  */
451       CORE_ADDR addr_in_block = frame_unwind_address_in_block (fi);
452       fi->prev_func.p = 1;
453       fi->prev_func.addr = get_pc_function_start (addr_in_block);
454       if (frame_debug)
455         fprintf_unfiltered (gdb_stdlog,
456                             "{ frame_func_unwind (fi=%d) -> 0x%s }\n",
457                             fi->level, paddr_nz (fi->prev_func.addr));
458     }
459   return fi->prev_func.addr;
460 }
461
462 CORE_ADDR
463 get_frame_func (struct frame_info *fi)
464 {
465   return frame_func_unwind (fi->next);
466 }
467
468 static int
469 do_frame_unwind_register (void *src, int regnum, void *buf)
470 {
471   frame_unwind_register (src, regnum, buf);
472   return 1;
473 }
474
475 void
476 frame_pop (struct frame_info *this_frame)
477 {
478   struct regcache *scratch_regcache;
479   struct cleanup *cleanups;
480
481   if (DEPRECATED_POP_FRAME_P ())
482     {
483       /* A legacy architecture that has implemented a custom pop
484          function.  All new architectures should instead be using the
485          generic code below.  */
486       DEPRECATED_POP_FRAME;
487     }
488   else
489     {
490       /* Make a copy of all the register values unwound from this
491          frame.  Save them in a scratch buffer so that there isn't a
492          race between trying to extract the old values from the
493          current_regcache while at the same time writing new values
494          into that same cache.  */
495       struct regcache *scratch = regcache_xmalloc (current_gdbarch);
496       struct cleanup *cleanups = make_cleanup_regcache_xfree (scratch);
497       regcache_save (scratch, do_frame_unwind_register, this_frame);
498       /* FIXME: cagney/2003-03-16: It should be possible to tell the
499          target's register cache that it is about to be hit with a
500          burst register transfer and that the sequence of register
501          writes should be batched.  The pair target_prepare_to_store()
502          and target_store_registers() kind of suggest this
503          functionality.  Unfortunately, they don't implement it.  Their
504          lack of a formal definition can lead to targets writing back
505          bogus values (arguably a bug in the target code mind).  */
506       /* Now copy those saved registers into the current regcache.
507          Here, regcache_cpy() calls regcache_restore().  */
508       regcache_cpy (current_regcache, scratch);
509       do_cleanups (cleanups);
510     }
511   /* We've made right mess of GDB's local state, just discard
512      everything.  */
513   flush_cached_frames ();
514 }
515
516 void
517 frame_register_unwind (struct frame_info *frame, int regnum,
518                        int *optimizedp, enum lval_type *lvalp,
519                        CORE_ADDR *addrp, int *realnump, void *bufferp)
520 {
521   struct frame_unwind_cache *cache;
522
523   if (frame_debug)
524     {
525       fprintf_unfiltered (gdb_stdlog, "\
526 { frame_register_unwind (frame=%d,regnum=%d(%s),...) ",
527                           frame->level, regnum,
528                           frame_map_regnum_to_name (frame, regnum));
529     }
530
531   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
532      that the value proper does not need to be fetched.  */
533   gdb_assert (optimizedp != NULL);
534   gdb_assert (lvalp != NULL);
535   gdb_assert (addrp != NULL);
536   gdb_assert (realnump != NULL);
537   /* gdb_assert (bufferp != NULL); */
538
539   /* NOTE: cagney/2002-11-27: A program trying to unwind a NULL frame
540      is broken.  There is always a frame.  If there, for some reason,
541      isn't a frame, there is some pretty busted code as it should have
542      detected the problem before calling here.  */
543   gdb_assert (frame != NULL);
544
545   /* Find the unwinder.  */
546   if (frame->unwind == NULL)
547     {
548       frame->unwind = frame_unwind_find_by_frame (frame->next,
549                                                   &frame->prologue_cache);
550       /* FIXME: cagney/2004-05-01: Should instead just use ->unwind->type.
551          Unfortunately, legacy_get_prev_frame is still explicitly setting
552          the type.  Eliminate that method and this field can be
553          eliminated.  */
554       frame->type = frame->unwind->type;
555     }
556
557   /* Ask this frame to unwind its register.  See comment in
558      "frame-unwind.h" for why NEXT frame and this unwind cache are
559      passed in.  */
560   frame->unwind->prev_register (frame->next, &frame->prologue_cache, regnum,
561                                 optimizedp, lvalp, addrp, realnump, bufferp);
562
563   if (frame_debug)
564     {
565       fprintf_unfiltered (gdb_stdlog, "->");
566       fprintf_unfiltered (gdb_stdlog, " *optimizedp=%d", (*optimizedp));
567       fprintf_unfiltered (gdb_stdlog, " *lvalp=%d", (int) (*lvalp));
568       fprintf_unfiltered (gdb_stdlog, " *addrp=0x%s", paddr_nz ((*addrp)));
569       fprintf_unfiltered (gdb_stdlog, " *bufferp=");
570       if (bufferp == NULL)
571         fprintf_unfiltered (gdb_stdlog, "<NULL>");
572       else
573         {
574           int i;
575           const unsigned char *buf = bufferp;
576           fprintf_unfiltered (gdb_stdlog, "[");
577           for (i = 0; i < register_size (current_gdbarch, regnum); i++)
578             fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
579           fprintf_unfiltered (gdb_stdlog, "]");
580         }
581       fprintf_unfiltered (gdb_stdlog, " }\n");
582     }
583 }
584
585 void
586 frame_register (struct frame_info *frame, int regnum,
587                 int *optimizedp, enum lval_type *lvalp,
588                 CORE_ADDR *addrp, int *realnump, void *bufferp)
589 {
590   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
591      that the value proper does not need to be fetched.  */
592   gdb_assert (optimizedp != NULL);
593   gdb_assert (lvalp != NULL);
594   gdb_assert (addrp != NULL);
595   gdb_assert (realnump != NULL);
596   /* gdb_assert (bufferp != NULL); */
597
598   /* Ulgh!  Old code that, for lval_register, sets ADDRP to the offset
599      of the register in the register cache.  It should instead return
600      the REGNUM corresponding to that register.  Translate the .  */
601   if (DEPRECATED_GET_SAVED_REGISTER_P ())
602     {
603       DEPRECATED_GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame,
604                                      regnum, lvalp);
605       /* Compute the REALNUM if the caller wants it.  */
606       if (*lvalp == lval_register)
607         {
608           int regnum;
609           for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
610             {
611               if (*addrp == register_offset_hack (current_gdbarch, regnum))
612                 {
613                   *realnump = regnum;
614                   return;
615                 }
616             }
617           internal_error (__FILE__, __LINE__,
618                           "Failed to compute the register number corresponding"
619                           " to 0x%s", paddr_d (*addrp));
620         }
621       *realnump = -1;
622       return;
623     }
624
625   /* Obtain the register value by unwinding the register from the next
626      (more inner frame).  */
627   gdb_assert (frame != NULL && frame->next != NULL);
628   frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
629                          realnump, bufferp);
630 }
631
632 void
633 frame_unwind_register (struct frame_info *frame, int regnum, void *buf)
634 {
635   int optimized;
636   CORE_ADDR addr;
637   int realnum;
638   enum lval_type lval;
639   frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
640                          &realnum, buf);
641 }
642
643 void
644 get_frame_register (struct frame_info *frame,
645                     int regnum, void *buf)
646 {
647   frame_unwind_register (frame->next, regnum, buf);
648 }
649
650 LONGEST
651 frame_unwind_register_signed (struct frame_info *frame, int regnum)
652 {
653   char buf[MAX_REGISTER_SIZE];
654   frame_unwind_register (frame, regnum, buf);
655   return extract_signed_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
656 }
657
658 LONGEST
659 get_frame_register_signed (struct frame_info *frame, int regnum)
660 {
661   return frame_unwind_register_signed (frame->next, regnum);
662 }
663
664 ULONGEST
665 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
666 {
667   char buf[MAX_REGISTER_SIZE];
668   frame_unwind_register (frame, regnum, buf);
669   return extract_unsigned_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
670 }
671
672 ULONGEST
673 get_frame_register_unsigned (struct frame_info *frame, int regnum)
674 {
675   return frame_unwind_register_unsigned (frame->next, regnum);
676 }
677
678 void
679 frame_unwind_unsigned_register (struct frame_info *frame, int regnum,
680                                 ULONGEST *val)
681 {
682   char buf[MAX_REGISTER_SIZE];
683   frame_unwind_register (frame, regnum, buf);
684   (*val) = extract_unsigned_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
685 }
686
687 void
688 put_frame_register (struct frame_info *frame, int regnum, const void *buf)
689 {
690   struct gdbarch *gdbarch = get_frame_arch (frame);
691   int realnum;
692   int optim;
693   enum lval_type lval;
694   CORE_ADDR addr;
695   frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
696   if (optim)
697     error ("Attempt to assign to a value that was optimized out.");
698   switch (lval)
699     {
700     case lval_memory:
701       {
702         /* FIXME: write_memory doesn't yet take constant buffers.
703            Arrrg!  */
704         char tmp[MAX_REGISTER_SIZE];
705         memcpy (tmp, buf, register_size (gdbarch, regnum));
706         write_memory (addr, tmp, register_size (gdbarch, regnum));
707         break;
708       }
709     case lval_register:
710       regcache_cooked_write (current_regcache, realnum, buf);
711       break;
712     default:
713       error ("Attempt to assign to an unmodifiable value.");
714     }
715 }
716
717 /* frame_register_read ()
718
719    Find and return the value of REGNUM for the specified stack frame.
720    The number of bytes copied is DEPRECATED_REGISTER_RAW_SIZE
721    (REGNUM).
722
723    Returns 0 if the register value could not be found.  */
724
725 int
726 frame_register_read (struct frame_info *frame, int regnum, void *myaddr)
727 {
728   int optimized;
729   enum lval_type lval;
730   CORE_ADDR addr;
731   int realnum;
732   frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
733
734   /* FIXME: cagney/2002-05-15: This test is just bogus.
735
736      It indicates that the target failed to supply a value for a
737      register because it was "not available" at this time.  Problem
738      is, the target still has the register and so get saved_register()
739      may be returning a value saved on the stack.  */
740
741   if (register_cached (regnum) < 0)
742     return 0;                   /* register value not available */
743
744   return !optimized;
745 }
746
747
748 /* Map between a frame register number and its name.  A frame register
749    space is a superset of the cooked register space --- it also
750    includes builtin registers.  */
751
752 int
753 frame_map_name_to_regnum (struct frame_info *frame, const char *name, int len)
754 {
755   return user_reg_map_name_to_regnum (get_frame_arch (frame), name, len);
756 }
757
758 const char *
759 frame_map_regnum_to_name (struct frame_info *frame, int regnum)
760 {
761   return user_reg_map_regnum_to_name (get_frame_arch (frame), regnum);
762 }
763
764 /* Create a sentinel frame.  */
765
766 static struct frame_info *
767 create_sentinel_frame (struct regcache *regcache)
768 {
769   struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
770   frame->type = NORMAL_FRAME;
771   frame->level = -1;
772   /* Explicitly initialize the sentinel frame's cache.  Provide it
773      with the underlying regcache.  In the future additional
774      information, such as the frame's thread will be added.  */
775   frame->prologue_cache = sentinel_frame_cache (regcache);
776   /* For the moment there is only one sentinel frame implementation.  */
777   frame->unwind = sentinel_frame_unwind;
778   /* Link this frame back to itself.  The frame is self referential
779      (the unwound PC is the same as the pc), so make it so.  */
780   frame->next = frame;
781   /* Make the sentinel frame's ID valid, but invalid.  That way all
782      comparisons with it should fail.  */
783   frame->this_id.p = 1;
784   frame->this_id.value = null_frame_id;
785   if (frame_debug)
786     {
787       fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
788       fprint_frame (gdb_stdlog, frame);
789       fprintf_unfiltered (gdb_stdlog, " }\n");
790     }
791   return frame;
792 }
793
794 /* Info about the innermost stack frame (contents of FP register) */
795
796 static struct frame_info *current_frame;
797
798 /* Cache for frame addresses already read by gdb.  Valid only while
799    inferior is stopped.  Control variables for the frame cache should
800    be local to this module.  */
801
802 static struct obstack frame_cache_obstack;
803
804 void *
805 frame_obstack_zalloc (unsigned long size)
806 {
807   void *data = obstack_alloc (&frame_cache_obstack, size);
808   memset (data, 0, size);
809   return data;
810 }
811
812 CORE_ADDR *
813 frame_saved_regs_zalloc (struct frame_info *fi)
814 {
815   fi->saved_regs = (CORE_ADDR *)
816     frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
817   return fi->saved_regs;
818 }
819
820 CORE_ADDR *
821 deprecated_get_frame_saved_regs (struct frame_info *fi)
822 {
823   return fi->saved_regs;
824 }
825
826 /* Return the innermost (currently executing) stack frame.  This is
827    split into two functions.  The function unwind_to_current_frame()
828    is wrapped in catch exceptions so that, even when the unwind of the
829    sentinel frame fails, the function still returns a stack frame.  */
830
831 static int
832 unwind_to_current_frame (struct ui_out *ui_out, void *args)
833 {
834   struct frame_info *frame = get_prev_frame (args);
835   /* A sentinel frame can fail to unwind, e.g., because its PC value
836      lands in somewhere like start.  */
837   if (frame == NULL)
838     return 1;
839   current_frame = frame;
840   return 0;
841 }
842
843 struct frame_info *
844 get_current_frame (void)
845 {
846   /* First check, and report, the lack of registers.  Having GDB
847      report "No stack!" or "No memory" when the target doesn't even
848      have registers is very confusing.  Besides, "printcmd.exp"
849      explicitly checks that ``print $pc'' with no registers prints "No
850      registers".  */
851   if (!target_has_registers)
852     error ("No registers.");
853   if (!target_has_stack)
854     error ("No stack.");
855   if (!target_has_memory)
856     error ("No memory.");
857   if (current_frame == NULL)
858     {
859       struct frame_info *sentinel_frame =
860         create_sentinel_frame (current_regcache);
861       if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
862                             NULL, RETURN_MASK_ERROR) != 0)
863         {
864           /* Oops! Fake a current frame?  Is this useful?  It has a PC
865              of zero, for instance.  */
866           current_frame = sentinel_frame;
867         }
868     }
869   return current_frame;
870 }
871
872 /* The "selected" stack frame is used by default for local and arg
873    access.  May be zero, for no selected frame.  */
874
875 struct frame_info *deprecated_selected_frame;
876
877 /* Return the selected frame.  Always non-NULL (unless there isn't an
878    inferior sufficient for creating a frame) in which case an error is
879    thrown.  */
880
881 struct frame_info *
882 get_selected_frame (void)
883 {
884   if (deprecated_selected_frame == NULL)
885     /* Hey!  Don't trust this.  It should really be re-finding the
886        last selected frame of the currently selected thread.  This,
887        though, is better than nothing.  */
888     select_frame (get_current_frame ());
889   /* There is always a frame.  */
890   gdb_assert (deprecated_selected_frame != NULL);
891   return deprecated_selected_frame;
892 }
893
894 /* This is a variant of get_selected_frame() which can be called when
895    the inferior does not have a frame; in that case it will return
896    NULL instead of calling error().  */
897
898 struct frame_info *
899 deprecated_safe_get_selected_frame (void)
900 {
901   if (!target_has_registers || !target_has_stack || !target_has_memory)
902     return NULL;
903   return get_selected_frame ();
904 }
905
906 /* Select frame FI (or NULL - to invalidate the current frame).  */
907
908 void
909 select_frame (struct frame_info *fi)
910 {
911   struct symtab *s;
912
913   deprecated_selected_frame = fi;
914   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
915      frame is being invalidated.  */
916   if (deprecated_selected_frame_level_changed_hook)
917     deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
918
919   /* FIXME: kseitz/2002-08-28: It would be nice to call
920      selected_frame_level_changed_event() right here, but due to limitations
921      in the current interfaces, we would end up flooding UIs with events
922      because select_frame() is used extensively internally.
923
924      Once we have frame-parameterized frame (and frame-related) commands,
925      the event notification can be moved here, since this function will only
926      be called when the user's selected frame is being changed. */
927
928   /* Ensure that symbols for this frame are read in.  Also, determine the
929      source language of this frame, and switch to it if desired.  */
930   if (fi)
931     {
932       /* We retrieve the frame's symtab by using the frame PC.  However
933          we cannot use the frame PC as-is, because it usually points to
934          the instruction following the "call", which is sometimes the
935          first instruction of another function.  So we rely on
936          get_frame_address_in_block() which provides us with a PC which
937          is guaranteed to be inside the frame's code block.  */
938       s = find_pc_symtab (get_frame_address_in_block (fi));
939       if (s
940           && s->language != current_language->la_language
941           && s->language != language_unknown
942           && language_mode == language_mode_auto)
943         {
944           set_language (s->language);
945         }
946     }
947 }
948
949 /* Return the register saved in the simplistic ``saved_regs'' cache.
950    If the value isn't here AND a value is needed, try the next inner
951    most frame.  */
952
953 static void
954 legacy_saved_regs_prev_register (struct frame_info *next_frame,
955                                  void **this_prologue_cache,
956                                  int regnum, int *optimizedp,
957                                  enum lval_type *lvalp, CORE_ADDR *addrp,
958                                  int *realnump, void *bufferp)
959 {
960   /* HACK: New code is passed the next frame and this cache.
961      Unfortunately, old code expects this frame.  Since this is a
962      backward compatibility hack, cheat by walking one level along the
963      prologue chain to the frame the old code expects.
964
965      Do not try this at home.  Professional driver, closed course.  */
966   struct frame_info *frame = next_frame->prev;
967   gdb_assert (frame != NULL);
968
969   if (deprecated_get_frame_saved_regs (frame) == NULL)
970     {
971       /* If nothing has initialized the saved regs, do it now.  */
972       gdb_assert (DEPRECATED_FRAME_INIT_SAVED_REGS_P ());
973       DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
974       gdb_assert (deprecated_get_frame_saved_regs (frame) != NULL);
975     }
976
977   if (deprecated_get_frame_saved_regs (frame) != NULL
978       && deprecated_get_frame_saved_regs (frame)[regnum] != 0)
979     {
980       if (regnum == SP_REGNUM)
981         {
982           /* SP register treated specially.  */
983           *optimizedp = 0;
984           *lvalp = not_lval;
985           *addrp = 0;
986           *realnump = -1;
987           if (bufferp != NULL)
988             /* NOTE: cagney/2003-05-09: In-lined store_address() with
989                it's body - store_unsigned_integer().  */
990             store_unsigned_integer (bufferp, DEPRECATED_REGISTER_RAW_SIZE (regnum),
991                                     deprecated_get_frame_saved_regs (frame)[regnum]);
992         }
993       else
994         {
995           /* Any other register is saved in memory, fetch it but cache
996              a local copy of its value.  */
997           *optimizedp = 0;
998           *lvalp = lval_memory;
999           *addrp = deprecated_get_frame_saved_regs (frame)[regnum];
1000           *realnump = -1;
1001           if (bufferp != NULL)
1002             {
1003 #if 1
1004               /* Save each register value, as it is read in, in a
1005                  frame based cache.  */
1006               void **regs = (*this_prologue_cache);
1007               if (regs == NULL)
1008                 {
1009                   int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS)
1010                                       * sizeof (void *));
1011                   regs = frame_obstack_zalloc (sizeof_cache);
1012                   (*this_prologue_cache) = regs;
1013                 }
1014               if (regs[regnum] == NULL)
1015                 {
1016                   regs[regnum]
1017                     = frame_obstack_zalloc (DEPRECATED_REGISTER_RAW_SIZE (regnum));
1018                   read_memory (deprecated_get_frame_saved_regs (frame)[regnum], regs[regnum],
1019                                DEPRECATED_REGISTER_RAW_SIZE (regnum));
1020                 }
1021               memcpy (bufferp, regs[regnum], DEPRECATED_REGISTER_RAW_SIZE (regnum));
1022 #else
1023               /* Read the value in from memory.  */
1024               read_memory (deprecated_get_frame_saved_regs (frame)[regnum], bufferp,
1025                            DEPRECATED_REGISTER_RAW_SIZE (regnum));
1026 #endif
1027             }
1028         }
1029       return;
1030     }
1031
1032   /* No luck.  Assume this and the next frame have the same register
1033      value.  Pass the unwind request down the frame chain to the next
1034      frame.  Hopefully that frame will find the register's location.  */
1035   frame_register_unwind (next_frame, regnum, optimizedp, lvalp, addrp,
1036                          realnump, bufferp);
1037 }
1038
1039 static void
1040 legacy_saved_regs_this_id (struct frame_info *next_frame,
1041                            void **this_prologue_cache,
1042                            struct frame_id *id)
1043 {
1044   /* A developer is trying to bring up a new architecture, help them
1045      by providing a default unwinder that refuses to unwind anything
1046      (the ID is always NULL).  In the case of legacy code,
1047      legacy_get_prev_frame() will have previously set ->this_id.p, so
1048      this code won't be called.  */
1049   (*id) = null_frame_id;
1050 }
1051         
1052 const struct frame_unwind legacy_saved_regs_unwinder = {
1053   /* Not really.  It gets overridden by legacy_get_prev_frame().  */
1054   UNKNOWN_FRAME,
1055   legacy_saved_regs_this_id,
1056   legacy_saved_regs_prev_register
1057 };
1058 const struct frame_unwind *legacy_saved_regs_unwind = &legacy_saved_regs_unwinder;
1059
1060
1061 /* Function: deprecated_generic_get_saved_register
1062    Find register number REGNUM relative to FRAME and put its (raw,
1063    target format) contents in *RAW_BUFFER.
1064
1065    Set *OPTIMIZED if the variable was optimized out (and thus can't be
1066    fetched).  Note that this is never set to anything other than zero
1067    in this implementation.
1068
1069    Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1070    whether the value was fetched from memory, from a register, or in a
1071    strange and non-modifiable way (e.g. a frame pointer which was
1072    calculated rather than fetched).  We will use not_lval for values
1073    fetched from generic dummy frames.
1074
1075    Set *ADDRP to the address, either in memory or as a
1076    DEPRECATED_REGISTER_BYTE offset into the registers array.  If the
1077    value is stored in a dummy frame, set *ADDRP to zero.
1078
1079    The argument RAW_BUFFER must point to aligned memory.  */
1080
1081 void
1082 deprecated_generic_get_saved_register (char *raw_buffer, int *optimized,
1083                                        CORE_ADDR *addrp,
1084                                        struct frame_info *frame, int regnum,
1085                                        enum lval_type *lval)
1086 {
1087   if (!target_has_registers)
1088     error ("No registers.");
1089
1090   /* Normal systems don't optimize out things with register numbers.  */
1091   if (optimized != NULL)
1092     *optimized = 0;
1093
1094   if (addrp)                    /* default assumption: not found in memory */
1095     *addrp = 0;
1096
1097   /* Note: since the current frame's registers could only have been
1098      saved by frames INTERIOR TO the current frame, we skip examining
1099      the current frame itself: otherwise, we would be getting the
1100      previous frame's registers which were saved by the current frame.  */
1101
1102   if (frame != NULL)
1103     {
1104       for (frame = get_next_frame (frame);
1105            frame_relative_level (frame) >= 0;
1106            frame = get_next_frame (frame))
1107         {
1108           if (get_frame_type (frame) == DUMMY_FRAME)
1109             {
1110               if (lval)         /* found it in a CALL_DUMMY frame */
1111                 *lval = not_lval;
1112               if (raw_buffer)
1113                 /* FIXME: cagney/2002-06-26: This should be via the
1114                    gdbarch_register_read() method so that it, on the
1115                    fly, constructs either a raw or pseudo register
1116                    from the raw register cache.  */
1117                 regcache_raw_read
1118                   (deprecated_find_dummy_frame_regcache (get_frame_pc (frame),
1119                                                          get_frame_base (frame)),
1120                    regnum, raw_buffer);
1121               return;
1122             }
1123
1124           DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
1125           if (deprecated_get_frame_saved_regs (frame) != NULL
1126               && deprecated_get_frame_saved_regs (frame)[regnum] != 0)
1127             {
1128               if (lval)         /* found it saved on the stack */
1129                 *lval = lval_memory;
1130               if (regnum == SP_REGNUM)
1131                 {
1132                   if (raw_buffer)       /* SP register treated specially */
1133                     /* NOTE: cagney/2003-05-09: In-line store_address()
1134                        with it's body - store_unsigned_integer().  */
1135                     store_unsigned_integer (raw_buffer,
1136                                             DEPRECATED_REGISTER_RAW_SIZE (regnum),
1137                                             deprecated_get_frame_saved_regs (frame)[regnum]);
1138                 }
1139               else
1140                 {
1141                   if (addrp)    /* any other register */
1142                     *addrp = deprecated_get_frame_saved_regs (frame)[regnum];
1143                   if (raw_buffer)
1144                     read_memory (deprecated_get_frame_saved_regs (frame)[regnum], raw_buffer,
1145                                  DEPRECATED_REGISTER_RAW_SIZE (regnum));
1146                 }
1147               return;
1148             }
1149         }
1150     }
1151
1152   /* If we get thru the loop to this point, it means the register was
1153      not saved in any frame.  Return the actual live-register value.  */
1154
1155   if (lval)                     /* found it in a live register */
1156     *lval = lval_register;
1157   if (addrp)
1158     *addrp = DEPRECATED_REGISTER_BYTE (regnum);
1159   if (raw_buffer)
1160     deprecated_read_register_gen (regnum, raw_buffer);
1161 }
1162
1163 /* Determine the frame's type based on its PC.  */
1164
1165 static enum frame_type
1166 frame_type_from_pc (CORE_ADDR pc)
1167 {
1168   if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1169       && deprecated_pc_in_call_dummy (pc, 0, 0))
1170     return DUMMY_FRAME;
1171   else
1172     return NORMAL_FRAME;
1173 }
1174
1175 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1176    Always returns a non-NULL value.  */
1177
1178 struct frame_info *
1179 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1180 {
1181   struct frame_info *fi;
1182
1183   if (frame_debug)
1184     {
1185       fprintf_unfiltered (gdb_stdlog,
1186                           "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1187                           paddr_nz (addr), paddr_nz (pc));
1188     }
1189
1190   fi = frame_obstack_zalloc (sizeof (struct frame_info));
1191
1192   fi->next = create_sentinel_frame (current_regcache);
1193
1194   /* Select/initialize both the unwind function and the frame's type
1195      based on the PC.  */
1196   fi->unwind = frame_unwind_find_by_frame (fi->next, &fi->prologue_cache);
1197   if (fi->unwind->type != UNKNOWN_FRAME)
1198     fi->type = fi->unwind->type;
1199   else
1200     fi->type = frame_type_from_pc (pc);
1201
1202   fi->this_id.p = 1;
1203   deprecated_update_frame_base_hack (fi, addr);
1204   deprecated_update_frame_pc_hack (fi, pc);
1205
1206   if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1207     DEPRECATED_INIT_EXTRA_FRAME_INFO (0, fi);
1208
1209   if (frame_debug)
1210     {
1211       fprintf_unfiltered (gdb_stdlog, "-> ");
1212       fprint_frame (gdb_stdlog, fi);
1213       fprintf_unfiltered (gdb_stdlog, " }\n");
1214     }
1215
1216   return fi;
1217 }
1218
1219 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1220    innermost frame).  Be careful to not fall off the bottom of the
1221    frame chain and onto the sentinel frame.  */
1222
1223 struct frame_info *
1224 get_next_frame (struct frame_info *this_frame)
1225 {
1226   if (this_frame->level > 0)
1227     return this_frame->next;
1228   else
1229     return NULL;
1230 }
1231
1232 /* Observer for the target_changed event.  */
1233
1234 void
1235 frame_observer_target_changed (struct target_ops *target)
1236 {
1237   flush_cached_frames ();
1238 }
1239
1240 /* Flush the entire frame cache.  */
1241
1242 void
1243 flush_cached_frames (void)
1244 {
1245   /* Since we can't really be sure what the first object allocated was */
1246   obstack_free (&frame_cache_obstack, 0);
1247   obstack_init (&frame_cache_obstack);
1248
1249   current_frame = NULL;         /* Invalidate cache */
1250   select_frame (NULL);
1251   annotate_frames_invalid ();
1252   if (frame_debug)
1253     fprintf_unfiltered (gdb_stdlog, "{ flush_cached_frames () }\n");
1254 }
1255
1256 /* Flush the frame cache, and start a new one if necessary.  */
1257
1258 void
1259 reinit_frame_cache (void)
1260 {
1261   flush_cached_frames ();
1262
1263   /* FIXME: The inferior_ptid test is wrong if there is a corefile.  */
1264   if (PIDGET (inferior_ptid) != 0)
1265     {
1266       select_frame (get_current_frame ());
1267     }
1268 }
1269
1270 /* Create the previous frame using the deprecated methods
1271    INIT_EXTRA_INFO, and INIT_FRAME_PC.  */
1272
1273 static struct frame_info *
1274 legacy_get_prev_frame (struct frame_info *this_frame)
1275 {
1276   CORE_ADDR address = 0;
1277   struct frame_info *prev;
1278   int fromleaf;
1279
1280   /* Don't frame_debug print legacy_get_prev_frame() here, just
1281      confuses the output.  */
1282
1283   /* Allocate the new frame.
1284
1285      There is no reason to worry about memory leaks, should the
1286      remainder of the function fail.  The allocated memory will be
1287      quickly reclaimed when the frame cache is flushed, and the `we've
1288      been here before' check, in get_prev_frame() will stop repeated
1289      memory allocation calls.  */
1290   prev = FRAME_OBSTACK_ZALLOC (struct frame_info);
1291   prev->level = this_frame->level + 1;
1292
1293   /* Do not completely wire it in to the frame chain.  Some (bad) code
1294      in INIT_FRAME_EXTRA_INFO tries to look along frame->prev to pull
1295      some fancy tricks (of course such code is, by definition,
1296      recursive).
1297   
1298      On the other hand, methods, such as get_frame_pc() and
1299      get_frame_base() rely on being able to walk along the frame
1300      chain.  Make certain that at least they work by providing that
1301      link.  Of course things manipulating prev can't go back.  */
1302   prev->next = this_frame;
1303
1304   /* NOTE: cagney/2002-11-18: Should have been correctly setting the
1305      frame's type here, before anything else, and not last, at the
1306      bottom of this function.  The various
1307      DEPRECATED_INIT_EXTRA_FRAME_INFO, DEPRECATED_INIT_FRAME_PC, and
1308      DEPRECATED_FRAME_INIT_SAVED_REGS methods are full of work-arounds
1309      that handle the frame not being correctly set from the start.
1310      Unfortunately those same work-arounds rely on the type defaulting
1311      to NORMAL_FRAME.  Ulgh!  The new frame code does not have this
1312      problem.  */
1313   prev->type = UNKNOWN_FRAME;
1314
1315   /* A legacy frame's ID is always computed here.  Mark it as valid.  */
1316   prev->this_id.p = 1;
1317
1318   /* Handle sentinel frame unwind as a special case.  */
1319   if (this_frame->level < 0)
1320     {
1321       /* Try to unwind the PC.  If that doesn't work, assume we've reached
1322          the oldest frame and simply return.  Is there a better sentinal
1323          value?  The unwound PC value is then used to initialize the new
1324          previous frame's type.
1325
1326          Note that the pc-unwind is intentionally performed before the
1327          frame chain.  This is ok since, for old targets, both
1328          frame_pc_unwind() (nee, DEPRECATED_FRAME_SAVED_PC) and
1329          DEPRECATED_FRAME_CHAIN()) assume THIS_FRAME's data structures
1330          have already been initialized (using
1331          DEPRECATED_INIT_EXTRA_FRAME_INFO) and hence the call order
1332          doesn't matter.
1333          
1334          By unwinding the PC first, it becomes possible to, in the case of
1335          a dummy frame, avoid also unwinding the frame ID.  This is
1336          because (well ignoring the PPC) a dummy frame can be located
1337          using THIS_FRAME's frame ID.  */
1338       
1339       deprecated_update_frame_pc_hack (prev, frame_pc_unwind (this_frame));
1340       if (get_frame_pc (prev) == 0)
1341         {
1342           /* The allocated PREV_FRAME will be reclaimed when the frame
1343              obstack is next purged.  */
1344           if (frame_debug)
1345             {
1346               fprintf_unfiltered (gdb_stdlog, "-> ");
1347               fprint_frame (gdb_stdlog, NULL);
1348               fprintf_unfiltered (gdb_stdlog,
1349                                   " // unwound legacy PC zero }\n");
1350             }
1351           return NULL;
1352         }
1353
1354       /* Set the unwind functions based on that identified PC.  Ditto
1355          for the "type" but strongly prefer the unwinder's frame type.  */
1356       prev->unwind = frame_unwind_find_by_frame (prev->next,
1357                                                  &prev->prologue_cache);
1358       if (prev->unwind->type == UNKNOWN_FRAME)
1359         prev->type = frame_type_from_pc (get_frame_pc (prev));
1360       else
1361         prev->type = prev->unwind->type;
1362
1363       /* Find the prev's frame's ID.  */
1364       if (prev->type == DUMMY_FRAME
1365           && gdbarch_unwind_dummy_id_p (current_gdbarch))
1366         {
1367           /* When unwinding a normal frame, the stack structure is
1368              determined by analyzing the frame's function's code (be
1369              it using brute force prologue analysis, or the dwarf2
1370              CFI).  In the case of a dummy frame, that simply isn't
1371              possible.  The The PC is either the program entry point,
1372              or some random address on the stack.  Trying to use that
1373              PC to apply standard frame ID unwind techniques is just
1374              asking for trouble.  */
1375           /* Use an architecture specific method to extract the prev's
1376              dummy ID from the next frame.  Note that this method uses
1377              frame_register_unwind to obtain the register values
1378              needed to determine the dummy frame's ID.  */
1379           prev->this_id.value = gdbarch_unwind_dummy_id (current_gdbarch,
1380                                                          this_frame);
1381         }
1382       else
1383         {
1384           /* We're unwinding a sentinel frame, the PC of which is
1385              pointing at a stack dummy.  Fake up the dummy frame's ID
1386              using the same sequence as is found a traditional
1387              unwinder.  Once all architectures supply the
1388              unwind_dummy_id method, this code can go away.  */
1389           prev->this_id.value = frame_id_build (deprecated_read_fp (),
1390                                                 read_pc ());
1391         }
1392
1393       /* Check that the unwound ID is valid.  */
1394       if (!frame_id_p (prev->this_id.value))
1395         {
1396           if (frame_debug)
1397             {
1398               fprintf_unfiltered (gdb_stdlog, "-> ");
1399               fprint_frame (gdb_stdlog, NULL);
1400               fprintf_unfiltered (gdb_stdlog,
1401                                   " // unwound legacy ID invalid }\n");
1402             }
1403           return NULL;
1404         }
1405
1406       /* Check that the new frame isn't inner to (younger, below,
1407          next) the old frame.  If that happens the frame unwind is
1408          going backwards.  */
1409       /* FIXME: cagney/2003-02-25: Ignore the sentinel frame since
1410          that doesn't have a valid frame ID.  Should instead set the
1411          sentinel frame's frame ID to a `sentinel'.  Leave it until
1412          after the switch to storing the frame ID, instead of the
1413          frame base, in the frame object.  */
1414
1415       /* Link it in.  */
1416       this_frame->prev = prev;
1417
1418       /* FIXME: cagney/2002-01-19: This call will go away.  Instead of
1419          initializing extra info, all frames will use the frame_cache
1420          (passed to the unwind functions) to store additional frame
1421          info.  Unfortunately legacy targets can't use
1422          legacy_get_prev_frame() to unwind the sentinel frame and,
1423          consequently, are forced to take this code path and rely on
1424          the below call to DEPRECATED_INIT_EXTRA_FRAME_INFO to
1425          initialize the inner-most frame.  */
1426       if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1427         {
1428           DEPRECATED_INIT_EXTRA_FRAME_INFO (0, prev);
1429         }
1430
1431       if (prev->type == NORMAL_FRAME)
1432         prev->this_id.value.code_addr
1433           = get_pc_function_start (prev->this_id.value.code_addr);
1434
1435       if (frame_debug)
1436         {
1437           fprintf_unfiltered (gdb_stdlog, "-> ");
1438           fprint_frame (gdb_stdlog, prev);
1439           fprintf_unfiltered (gdb_stdlog, " } // legacy innermost frame\n");
1440         }
1441       return prev;
1442     }
1443
1444   /* This code only works on normal frames.  A sentinel frame, where
1445      the level is -1, should never reach this code.  */
1446   gdb_assert (this_frame->level >= 0);
1447
1448   /* On some machines it is possible to call a function without
1449      setting up a stack frame for it.  On these machines, we
1450      define this macro to take two args; a frameinfo pointer
1451      identifying a frame and a variable to set or clear if it is
1452      or isn't leafless.  */
1453
1454   /* Still don't want to worry about this except on the innermost
1455      frame.  This macro will set FROMLEAF if THIS_FRAME is a frameless
1456      function invocation.  */
1457   if (this_frame->level == 0)
1458     /* FIXME: 2002-11-09: Frameless functions can occur anywhere in
1459        the frame chain, not just the inner most frame!  The generic,
1460        per-architecture, frame code should handle this and the below
1461        should simply be removed.  */
1462     fromleaf = (DEPRECATED_FRAMELESS_FUNCTION_INVOCATION_P ()
1463                 && DEPRECATED_FRAMELESS_FUNCTION_INVOCATION (this_frame));
1464   else
1465     fromleaf = 0;
1466
1467   if (fromleaf)
1468     /* A frameless inner-most frame.  The `FP' (which isn't an
1469        architecture frame-pointer register!) of the caller is the same
1470        as the callee.  */
1471     /* FIXME: 2002-11-09: There isn't any reason to special case this
1472        edge condition.  Instead the per-architecture code should handle
1473        it locally.  */
1474     /* FIXME: cagney/2003-06-16: This returns the inner most stack
1475        address for the previous frame, that, however, is wrong.  It
1476        should be the inner most stack address for the previous to
1477        previous frame.  This is because it is the previous to previous
1478        frame's innermost stack address that is constant through out
1479        the lifetime of the previous frame (trust me :-).  */
1480     address = get_frame_base (this_frame);
1481   else
1482     {
1483       /* Two macros defined in tm.h specify the machine-dependent
1484          actions to be performed here.
1485
1486          First, get the frame's chain-pointer.
1487
1488          If that is zero, the frame is the outermost frame or a leaf
1489          called by the outermost frame.  This means that if start
1490          calls main without a frame, we'll return 0 (which is fine
1491          anyway).
1492
1493          Nope; there's a problem.  This also returns when the current
1494          routine is a leaf of main.  This is unacceptable.  We move
1495          this to after the ffi test; I'd rather have backtraces from
1496          start go curfluy than have an abort called from main not show
1497          main.  */
1498       if (DEPRECATED_FRAME_CHAIN_P ())
1499         address = DEPRECATED_FRAME_CHAIN (this_frame);
1500       else
1501         {
1502           /* Someone is part way through coverting an old architecture
1503              to the new frame code.  Implement FRAME_CHAIN the way the
1504              new frame will.  */
1505           /* Find PREV frame's unwinder.  */
1506           prev->unwind = frame_unwind_find_by_frame (this_frame,
1507                                                      &prev->prologue_cache);
1508           /* FIXME: cagney/2004-05-01: Should instead just use
1509              ->unwind->type.  Unfortunately, legacy_get_prev_frame is
1510              still explicitly setting the type.  Eliminate that method
1511              and this field can be eliminated.  */
1512           prev->type = prev->unwind->type;
1513           /* Find PREV frame's ID.  */
1514           prev->unwind->this_id (this_frame,
1515                                  &prev->prologue_cache,
1516                                  &prev->this_id.value);
1517           prev->this_id.p = 1;
1518           address = prev->this_id.value.stack_addr;
1519         }
1520
1521       if (!legacy_frame_chain_valid (address, this_frame))
1522         {
1523           if (frame_debug)
1524             {
1525               fprintf_unfiltered (gdb_stdlog, "-> ");
1526               fprint_frame (gdb_stdlog, NULL);
1527               fprintf_unfiltered (gdb_stdlog,
1528                                   " // legacy frame chain invalid }\n");
1529             }
1530           return NULL;
1531         }
1532     }
1533   if (address == 0)
1534     {
1535       if (frame_debug)
1536         {
1537           fprintf_unfiltered (gdb_stdlog, "-> ");
1538           fprint_frame (gdb_stdlog, NULL);
1539           fprintf_unfiltered (gdb_stdlog,
1540                               " // legacy frame chain NULL }\n");
1541         }
1542       return NULL;
1543     }
1544
1545   /* Link in the already allocated prev frame.  */
1546   this_frame->prev = prev;
1547   deprecated_update_frame_base_hack (prev, address);
1548
1549   /* This change should not be needed, FIXME!  We should determine
1550      whether any targets *need* DEPRECATED_INIT_FRAME_PC to happen
1551      after DEPRECATED_INIT_EXTRA_FRAME_INFO and come up with a simple
1552      way to express what goes on here.
1553
1554      DEPRECATED_INIT_EXTRA_FRAME_INFO is called from two places:
1555      create_new_frame (where the PC is already set up) and here (where
1556      it isn't).  DEPRECATED_INIT_FRAME_PC is only called from here,
1557      always after DEPRECATED_INIT_EXTRA_FRAME_INFO.
1558
1559      The catch is the MIPS, where DEPRECATED_INIT_EXTRA_FRAME_INFO
1560      requires the PC value (which hasn't been set yet).  Some other
1561      machines appear to require DEPRECATED_INIT_EXTRA_FRAME_INFO
1562      before they can do DEPRECATED_INIT_FRAME_PC.  Phoo.
1563
1564      Assuming that some machines need DEPRECATED_INIT_FRAME_PC after
1565      DEPRECATED_INIT_EXTRA_FRAME_INFO, one possible scheme:
1566
1567      SETUP_INNERMOST_FRAME(): Default version is just create_new_frame
1568      (deprecated_read_fp ()), read_pc ()).  Machines with extra frame
1569      info would do that (or the local equivalent) and then set the
1570      extra fields.
1571
1572      SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that
1573      create_new_frame would no longer init extra frame info;
1574      SETUP_ARBITRARY_FRAME would have to do that.
1575
1576      INIT_PREV_FRAME(fromleaf, prev) Replace
1577      DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC.
1578      This should also return a flag saying whether to keep the new
1579      frame, or whether to discard it, because on some machines (e.g.
1580      mips) it is really awkward to have DEPRECATED_FRAME_CHAIN_VALID
1581      called BEFORE DEPRECATED_INIT_EXTRA_FRAME_INFO (there is no good
1582      way to get information deduced in DEPRECATED_FRAME_CHAIN_VALID
1583      into the extra fields of the new frame).  std_frame_pc(fromleaf,
1584      prev)
1585
1586      This is the default setting for INIT_PREV_FRAME.  It just does
1587      what the default DEPRECATED_INIT_FRAME_PC does.  Some machines
1588      will call it from INIT_PREV_FRAME (either at the beginning, the
1589      end, or in the middle).  Some machines won't use it.
1590
1591      kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94.  */
1592
1593   /* NOTE: cagney/2002-11-09: Just ignore the above!  There is no
1594      reason for things to be this complicated.
1595
1596      The trick is to assume that there is always a frame.  Instead of
1597      special casing the inner-most frame, create a fake frame
1598      (containing the hardware registers) that is inner to the
1599      user-visible inner-most frame (...) and then unwind from that.
1600      That way architecture code can use the standard
1601      frame_XX_unwind() functions and not differentiate between the
1602      inner most and any other case.
1603
1604      Since there is always a frame to unwind from, there is always
1605      somewhere (THIS_FRAME) to store all the info needed to construct
1606      a new (previous) frame without having to first create it.  This
1607      means that the convolution below - needing to carefully order a
1608      frame's initialization - isn't needed.
1609
1610      The irony here though, is that DEPRECATED_FRAME_CHAIN(), at least
1611      for a more up-to-date architecture, always calls
1612      FRAME_SAVED_PC(), and FRAME_SAVED_PC() computes the PC but
1613      without first needing the frame!  Instead of the convolution
1614      below, we could have simply called FRAME_SAVED_PC() and been done
1615      with it!  Note that FRAME_SAVED_PC() is being superseded by
1616      frame_pc_unwind() and that function does have somewhere to cache
1617      that PC value.  */
1618
1619   if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ())
1620     DEPRECATED_INIT_EXTRA_FRAME_INFO (fromleaf, prev);
1621
1622   /* This entry is in the frame queue now, which is good since
1623      FRAME_SAVED_PC may use that queue to figure out its value (see
1624      tm-sparc.h).  We want the PC saved in the inferior frame. */
1625   if (DEPRECATED_INIT_FRAME_PC_P ())
1626     deprecated_update_frame_pc_hack (prev,
1627                                      DEPRECATED_INIT_FRAME_PC (fromleaf,
1628                                                                prev));
1629
1630   /* If ->frame and ->pc are unchanged, we are in the process of
1631      getting ourselves into an infinite backtrace.  Some architectures
1632      check this in DEPRECATED_FRAME_CHAIN or thereabouts, but it seems
1633      like there is no reason this can't be an architecture-independent
1634      check.  */
1635   if (get_frame_base (prev) == get_frame_base (this_frame)
1636       && get_frame_pc (prev) == get_frame_pc (this_frame))
1637     {
1638       this_frame->prev = NULL;
1639       obstack_free (&frame_cache_obstack, prev);
1640       if (frame_debug)
1641         {
1642           fprintf_unfiltered (gdb_stdlog, "-> ");
1643           fprint_frame (gdb_stdlog, NULL);
1644           fprintf_unfiltered (gdb_stdlog,
1645                               " // legacy this.id == prev.id }\n");
1646         }
1647       return NULL;
1648     }
1649
1650   /* Initialize the code used to unwind the frame PREV based on the PC
1651      (and probably other architectural information).  The PC lets you
1652      check things like the debug info at that point (dwarf2cfi?) and
1653      use that to decide how the frame should be unwound.
1654
1655      If there isn't a FRAME_CHAIN, the code above will have already
1656      done this.  */
1657   if (prev->unwind == NULL)
1658     prev->unwind = frame_unwind_find_by_frame (prev->next,
1659                                                &prev->prologue_cache);
1660
1661   /* If the unwinder provides a frame type, use it.  Otherwise
1662      continue on to that heuristic mess.  */
1663   if (prev->unwind->type != UNKNOWN_FRAME)
1664     {
1665       prev->type = prev->unwind->type;
1666       if (prev->type == NORMAL_FRAME)
1667         /* FIXME: cagney/2003-06-16: would get_frame_pc() be better?  */
1668         prev->this_id.value.code_addr
1669           = get_pc_function_start (prev->this_id.value.code_addr);
1670       if (frame_debug)
1671         {
1672           fprintf_unfiltered (gdb_stdlog, "-> ");
1673           fprint_frame (gdb_stdlog, prev);
1674           fprintf_unfiltered (gdb_stdlog, " } // legacy with unwound type\n");
1675         }
1676       return prev;
1677     }
1678
1679   /* NOTE: cagney/2002-11-18: The code segments, found in
1680      create_new_frame() and get_prev_frame(), that initialize the
1681      frame's type is subtly different.  The latter only updates ->type
1682      when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME.  This stops
1683      get_prev_frame() overriding the frame's type when the INIT code
1684      has previously set it.  This is really somewhat bogus.  The
1685      initialization, as seen in create_new_frame(), should occur
1686      before the INIT function has been called.  */
1687   if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
1688       && deprecated_pc_in_call_dummy (get_frame_pc (prev), 0, 0))
1689     prev->type = DUMMY_FRAME;
1690
1691   if (prev->type == NORMAL_FRAME)
1692     prev->this_id.value.code_addr
1693       = get_pc_function_start (prev->this_id.value.code_addr);
1694
1695   if (frame_debug)
1696     {
1697       fprintf_unfiltered (gdb_stdlog, "-> ");
1698       fprint_frame (gdb_stdlog, prev);
1699       fprintf_unfiltered (gdb_stdlog, " } // legacy with confused type\n");
1700     }
1701
1702   return prev;
1703 }
1704
1705 /* Return a "struct frame_info" corresponding to the frame that called
1706    THIS_FRAME.  Returns NULL if there is no such frame.
1707
1708    Unlike get_prev_frame, this function always tries to unwind the
1709    frame.  */
1710
1711 static struct frame_info *
1712 get_prev_frame_1 (struct frame_info *this_frame)
1713 {
1714   struct frame_info *prev_frame;
1715
1716   gdb_assert (this_frame != NULL);
1717
1718   if (frame_debug)
1719     {
1720       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1721       if (this_frame != NULL)
1722         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1723       else
1724         fprintf_unfiltered (gdb_stdlog, "<NULL>");
1725       fprintf_unfiltered (gdb_stdlog, ") ");
1726     }
1727
1728   /* Only try to do the unwind once.  */
1729   if (this_frame->prev_p)
1730     {
1731       if (frame_debug)
1732         {
1733           fprintf_unfiltered (gdb_stdlog, "-> ");
1734           fprint_frame (gdb_stdlog, this_frame->prev);
1735           fprintf_unfiltered (gdb_stdlog, " // cached \n");
1736         }
1737       return this_frame->prev;
1738     }
1739   this_frame->prev_p = 1;
1740
1741   /* If any of the old frame initialization methods are around, use
1742      the legacy get_prev_frame() method.  */
1743   if (legacy_frame_p (current_gdbarch))
1744     {
1745       prev_frame = legacy_get_prev_frame (this_frame);
1746       return prev_frame;
1747     }
1748
1749   /* Check that this frame's ID was valid.  If it wasn't, don't try to
1750      unwind to the prev frame.  Be careful to not apply this test to
1751      the sentinel frame.  */
1752   if (this_frame->level >= 0 && !frame_id_p (get_frame_id (this_frame)))
1753     {
1754       if (frame_debug)
1755         {
1756           fprintf_unfiltered (gdb_stdlog, "-> ");
1757           fprint_frame (gdb_stdlog, NULL);
1758           fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1759         }
1760       return NULL;
1761     }
1762
1763   /* Check that this frame's ID isn't inner to (younger, below, next)
1764      the next frame.  This happens when a frame unwind goes backwards.
1765      Exclude signal trampolines (due to sigaltstack the frame ID can
1766      go backwards) and sentinel frames (the test is meaningless).  */
1767   if (this_frame->next->level >= 0
1768       && this_frame->next->type != SIGTRAMP_FRAME
1769       && frame_id_inner (get_frame_id (this_frame),
1770                          get_frame_id (this_frame->next)))
1771     error ("Previous frame inner to this frame (corrupt stack?)");
1772
1773   /* Check that this and the next frame are not identical.  If they
1774      are, there is most likely a stack cycle.  As with the inner-than
1775      test above, avoid comparing the inner-most and sentinel frames.  */
1776   if (this_frame->level > 0
1777       && frame_id_eq (get_frame_id (this_frame),
1778                       get_frame_id (this_frame->next)))
1779     error ("Previous frame identical to this frame (corrupt stack?)");
1780
1781   /* Allocate the new frame but do not wire it in to the frame chain.
1782      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1783      frame->next to pull some fancy tricks (of course such code is, by
1784      definition, recursive).  Try to prevent it.
1785
1786      There is no reason to worry about memory leaks, should the
1787      remainder of the function fail.  The allocated memory will be
1788      quickly reclaimed when the frame cache is flushed, and the `we've
1789      been here before' check above will stop repeated memory
1790      allocation calls.  */
1791   prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1792   prev_frame->level = this_frame->level + 1;
1793
1794   /* Don't yet compute ->unwind (and hence ->type).  It is computed
1795      on-demand in get_frame_type, frame_register_unwind, and
1796      get_frame_id.  */
1797
1798   /* Don't yet compute the frame's ID.  It is computed on-demand by
1799      get_frame_id().  */
1800
1801   /* The unwound frame ID is validate at the start of this function,
1802      as part of the logic to decide if that frame should be further
1803      unwound, and not here while the prev frame is being created.
1804      Doing this makes it possible for the user to examine a frame that
1805      has an invalid frame ID.
1806
1807      Some very old VAX code noted: [...]  For the sake of argument,
1808      suppose that the stack is somewhat trashed (which is one reason
1809      that "info frame" exists).  So, return 0 (indicating we don't
1810      know the address of the arglist) if we don't know what frame this
1811      frame calls.  */
1812
1813   /* Link it in.  */
1814   this_frame->prev = prev_frame;
1815   prev_frame->next = this_frame;
1816
1817   if (frame_debug)
1818     {
1819       fprintf_unfiltered (gdb_stdlog, "-> ");
1820       fprint_frame (gdb_stdlog, prev_frame);
1821       fprintf_unfiltered (gdb_stdlog, " }\n");
1822     }
1823
1824   return prev_frame;
1825 }
1826
1827 /* Debug routine to print a NULL frame being returned.  */
1828
1829 static void
1830 frame_debug_got_null_frame (struct ui_file *file,
1831                             struct frame_info *this_frame,
1832                             const char *reason)
1833 {
1834   if (frame_debug)
1835     {
1836       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1837       if (this_frame != NULL)
1838         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1839       else
1840         fprintf_unfiltered (gdb_stdlog, "<NULL>");
1841       fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1842     }
1843 }
1844
1845 /* Return a structure containing various interesting information about
1846    the frame that called THIS_FRAME.  Returns NULL if there is entier
1847    no such frame or the frame fails any of a set of target-independent
1848    condition that should terminate the frame chain (e.g., as unwinding
1849    past main()).
1850
1851    This function should not contain target-dependent tests, such as
1852    checking whether the program-counter is zero.  */
1853
1854 struct frame_info *
1855 get_prev_frame (struct frame_info *this_frame)
1856 {
1857   struct frame_info *prev_frame;
1858
1859   /* Return the inner-most frame, when the caller passes in NULL.  */
1860   /* NOTE: cagney/2002-11-09: Not sure how this would happen.  The
1861      caller should have previously obtained a valid frame using
1862      get_selected_frame() and then called this code - only possibility
1863      I can think of is code behaving badly.
1864
1865      NOTE: cagney/2003-01-10: Talk about code behaving badly.  Check
1866      block_innermost_frame().  It does the sequence: frame = NULL;
1867      while (1) { frame = get_prev_frame (frame); .... }.  Ulgh!  Why
1868      it couldn't be written better, I don't know.
1869
1870      NOTE: cagney/2003-01-11: I suspect what is happening in
1871      block_innermost_frame() is, when the target has no state
1872      (registers, memory, ...), it is still calling this function.  The
1873      assumption being that this function will return NULL indicating
1874      that a frame isn't possible, rather than checking that the target
1875      has state and then calling get_current_frame() and
1876      get_prev_frame().  This is a guess mind.  */
1877   if (this_frame == NULL)
1878     {
1879       /* NOTE: cagney/2002-11-09: There was a code segment here that
1880          would error out when CURRENT_FRAME was NULL.  The comment
1881          that went with it made the claim ...
1882
1883          ``This screws value_of_variable, which just wants a nice
1884          clean NULL return from block_innermost_frame if there are no
1885          frames.  I don't think I've ever seen this message happen
1886          otherwise.  And returning NULL here is a perfectly legitimate
1887          thing to do.''
1888
1889          Per the above, this code shouldn't even be called with a NULL
1890          THIS_FRAME.  */
1891       frame_debug_got_null_frame (gdb_stdlog, this_frame, "this_frame NULL");
1892       return current_frame;
1893     }
1894
1895   /* There is always a frame.  If this assertion fails, suspect that
1896      something should be calling get_selected_frame() or
1897      get_current_frame().  */
1898   gdb_assert (this_frame != NULL);
1899
1900   /* Make sure we pass an address within THIS_FRAME's code block to
1901      inside_main_func().  Otherwise, we might stop unwinding at a
1902      function which has a call instruction as its last instruction if
1903      that function immediately precedes main().  */
1904   if (this_frame->level >= 0
1905       && !backtrace_past_main
1906       && inside_main_func (get_frame_address_in_block (this_frame)))
1907     /* Don't unwind past main(), but always unwind the sentinel frame.
1908        Note, this is done _before_ the frame has been marked as
1909        previously unwound.  That way if the user later decides to
1910        allow unwinds past main(), that just happens.  */
1911     {
1912       frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside main func");
1913       return NULL;
1914     }
1915
1916   if (this_frame->level > backtrace_limit)
1917     {
1918       error ("Backtrace limit of %d exceeded", backtrace_limit);
1919     }
1920
1921   /* If we're already inside the entry function for the main objfile,
1922      then it isn't valid.  Don't apply this test to a dummy frame -
1923      dummy frame PCs typically land in the entry func.  Don't apply
1924      this test to the sentinel frame.  Sentinel frames should always
1925      be allowed to unwind.  */
1926   /* NOTE: cagney/2003-02-25: Don't enable until someone has found
1927      hard evidence that this is needed.  */
1928   /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1929      wasn't checking for "main" in the minimal symbols.  With that
1930      fixed asm-source tests now stop in "main" instead of halting the
1931      backtrace in weird and wonderful ways somewhere inside the entry
1932      file.  Suspect that tests for inside the entry file/func were
1933      added to work around that (now fixed) case.  */
1934   /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1935      suggested having the inside_entry_func test use the
1936      inside_main_func() msymbol trick (along with entry_point_address()
1937      I guess) to determine the address range of the start function.
1938      That should provide a far better stopper than the current
1939      heuristics.  */
1940   /* NOTE: cagney/2003-07-15: Need to add a "set backtrace
1941      beyond-entry-func" command so that this can be selectively
1942      disabled.  */
1943   if (0
1944 #if 0
1945       && backtrace_beyond_entry_func
1946 #endif
1947       && this_frame->type != DUMMY_FRAME && this_frame->level >= 0
1948       && inside_entry_func (this_frame))
1949     {
1950       frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside entry func");
1951       return NULL;
1952     }
1953
1954   return get_prev_frame_1 (this_frame);
1955 }
1956
1957 CORE_ADDR
1958 get_frame_pc (struct frame_info *frame)
1959 {
1960   gdb_assert (frame->next != NULL);
1961   return frame_pc_unwind (frame->next);
1962 }
1963
1964 /* Return an address of that falls within the frame's code block.  */
1965
1966 CORE_ADDR
1967 frame_unwind_address_in_block (struct frame_info *next_frame)
1968 {
1969   /* A draft address.  */
1970   CORE_ADDR pc = frame_pc_unwind (next_frame);
1971
1972   /* If THIS frame is not inner most (i.e., NEXT isn't the sentinel),
1973      and NEXT is `normal' (i.e., not a sigtramp, dummy, ....) THIS
1974      frame's PC ends up pointing at the instruction fallowing the
1975      "call".  Adjust that PC value so that it falls on the call
1976      instruction (which, hopefully, falls within THIS frame's code
1977      block.  So far it's proved to be a very good approximation.  See
1978      get_frame_type() for why ->type can't be used.  */
1979   if (next_frame->level >= 0
1980       && get_frame_type (next_frame) == NORMAL_FRAME)
1981     --pc;
1982   return pc;
1983 }
1984
1985 CORE_ADDR
1986 get_frame_address_in_block (struct frame_info *this_frame)
1987 {
1988   return frame_unwind_address_in_block (this_frame->next);
1989 }
1990
1991 static int
1992 pc_notcurrent (struct frame_info *frame)
1993 {
1994   /* If FRAME is not the innermost frame, that normally means that
1995      FRAME->pc points at the return instruction (which is *after* the
1996      call instruction), and we want to get the line containing the
1997      call (because the call is where the user thinks the program is).
1998      However, if the next frame is either a SIGTRAMP_FRAME or a
1999      DUMMY_FRAME, then the next frame will contain a saved interrupt
2000      PC and such a PC indicates the current (rather than next)
2001      instruction/line, consequently, for such cases, want to get the
2002      line containing fi->pc.  */
2003   struct frame_info *next = get_next_frame (frame);
2004   int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
2005   return notcurrent;
2006 }
2007
2008 void
2009 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
2010 {
2011   (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
2012 }
2013
2014 /* Per "frame.h", return the ``address'' of the frame.  Code should
2015    really be using get_frame_id().  */
2016 CORE_ADDR
2017 get_frame_base (struct frame_info *fi)
2018 {
2019   return get_frame_id (fi).stack_addr;
2020 }
2021
2022 /* High-level offsets into the frame.  Used by the debug info.  */
2023
2024 CORE_ADDR
2025 get_frame_base_address (struct frame_info *fi)
2026 {
2027   if (get_frame_type (fi) != NORMAL_FRAME)
2028     return 0;
2029   if (fi->base == NULL)
2030     fi->base = frame_base_find_by_frame (fi->next);
2031   /* Sneaky: If the low-level unwind and high-level base code share a
2032      common unwinder, let them share the prologue cache.  */
2033   if (fi->base->unwind == fi->unwind)
2034     return fi->base->this_base (fi->next, &fi->prologue_cache);
2035   return fi->base->this_base (fi->next, &fi->base_cache);
2036 }
2037
2038 CORE_ADDR
2039 get_frame_locals_address (struct frame_info *fi)
2040 {
2041   void **cache;
2042   if (get_frame_type (fi) != NORMAL_FRAME)
2043     return 0;
2044   /* If there isn't a frame address method, find it.  */
2045   if (fi->base == NULL)
2046     fi->base = frame_base_find_by_frame (fi->next);
2047   /* Sneaky: If the low-level unwind and high-level base code share a
2048      common unwinder, let them share the prologue cache.  */
2049   if (fi->base->unwind == fi->unwind)
2050     cache = &fi->prologue_cache;
2051   else
2052     cache = &fi->base_cache;
2053   return fi->base->this_locals (fi->next, cache);
2054 }
2055
2056 CORE_ADDR
2057 get_frame_args_address (struct frame_info *fi)
2058 {
2059   void **cache;
2060   if (get_frame_type (fi) != NORMAL_FRAME)
2061     return 0;
2062   /* If there isn't a frame address method, find it.  */
2063   if (fi->base == NULL)
2064     fi->base = frame_base_find_by_frame (fi->next);
2065   /* Sneaky: If the low-level unwind and high-level base code share a
2066      common unwinder, let them share the prologue cache.  */
2067   if (fi->base->unwind == fi->unwind)
2068     cache = &fi->prologue_cache;
2069   else
2070     cache = &fi->base_cache;
2071   return fi->base->this_args (fi->next, cache);
2072 }
2073
2074 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2075    or -1 for a NULL frame.  */
2076
2077 int
2078 frame_relative_level (struct frame_info *fi)
2079 {
2080   if (fi == NULL)
2081     return -1;
2082   else
2083     return fi->level;
2084 }
2085
2086 enum frame_type
2087 get_frame_type (struct frame_info *frame)
2088 {
2089   /* Some targets still don't use [generic] dummy frames.  Catch them
2090      here.  */
2091   if (!DEPRECATED_USE_GENERIC_DUMMY_FRAMES
2092       && deprecated_frame_in_dummy (frame))
2093     return DUMMY_FRAME;
2094
2095   /* Some legacy code, e.g, mips_init_extra_frame_info() wants
2096      to determine the frame's type prior to it being completely
2097      initialized.  Don't attempt to lazily initialize ->unwind for
2098      legacy code.  It will be initialized in legacy_get_prev_frame().  */
2099   if (frame->unwind == NULL && !legacy_frame_p (current_gdbarch))
2100     {
2101       /* Initialize the frame's unwinder because that's what
2102          provides the frame's type.  */
2103       frame->unwind = frame_unwind_find_by_frame (frame->next, 
2104                                                   &frame->prologue_cache);
2105       /* FIXME: cagney/2004-05-01: Should instead just use
2106          ->unwind->type.  Unfortunately, legacy_get_prev_frame is
2107          still explicitly setting the type.  Eliminate that method and
2108          this field can be eliminated.  */
2109       frame->type = frame->unwind->type;
2110     }
2111   if (frame->type == UNKNOWN_FRAME)
2112     return NORMAL_FRAME;
2113   else
2114     return frame->type;
2115 }
2116
2117 struct frame_extra_info *
2118 get_frame_extra_info (struct frame_info *fi)
2119 {
2120   return fi->extra_info;
2121 }
2122
2123 struct frame_extra_info *
2124 frame_extra_info_zalloc (struct frame_info *fi, long size)
2125 {
2126   fi->extra_info = frame_obstack_zalloc (size);
2127   return fi->extra_info;
2128 }
2129
2130 void
2131 deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
2132 {
2133   if (frame_debug)
2134     fprintf_unfiltered (gdb_stdlog,
2135                         "{ deprecated_update_frame_pc_hack (frame=%d,pc=0x%s) }\n",
2136                         frame->level, paddr_nz (pc));
2137   /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are
2138      maintaining a locally allocated frame object.  Since such frames
2139      are not in the frame chain, it isn't possible to assume that the
2140      frame has a next.  Sigh.  */
2141   if (frame->next != NULL)
2142     {
2143       /* While we're at it, update this frame's cached PC value, found
2144          in the next frame.  Oh for the day when "struct frame_info"
2145          is opaque and this hack on hack can just go away.  */
2146       frame->next->prev_pc.value = pc;
2147       frame->next->prev_pc.p = 1;
2148     }
2149 }
2150
2151 void
2152 deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
2153 {
2154   if (frame_debug)
2155     fprintf_unfiltered (gdb_stdlog,
2156                         "{ deprecated_update_frame_base_hack (frame=%d,base=0x%s) }\n",
2157                         frame->level, paddr_nz (base));
2158   /* See comment in "frame.h".  */
2159   frame->this_id.value.stack_addr = base;
2160 }
2161
2162 struct frame_info *
2163 deprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs,
2164                                        long sizeof_extra_info)
2165 {
2166   struct frame_info *frame = XMALLOC (struct frame_info);
2167   memset (frame, 0, sizeof (*frame));
2168   frame->this_id.p = 1;
2169   make_cleanup (xfree, frame);
2170   if (sizeof_saved_regs > 0)
2171     {
2172       frame->saved_regs = xcalloc (1, sizeof_saved_regs);
2173       make_cleanup (xfree, frame->saved_regs);
2174     }
2175   if (sizeof_extra_info > 0)
2176     {
2177       frame->extra_info = xcalloc (1, sizeof_extra_info);
2178       make_cleanup (xfree, frame->extra_info);
2179     }
2180   return frame;
2181 }
2182
2183 /* Memory access methods.  */
2184
2185 void
2186 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr, void *buf,
2187                   int len)
2188 {
2189   read_memory (addr, buf, len);
2190 }
2191
2192 LONGEST
2193 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
2194                          int len)
2195 {
2196   return read_memory_integer (addr, len);
2197 }
2198
2199 ULONGEST
2200 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
2201                            int len)
2202 {
2203   return read_memory_unsigned_integer (addr, len);
2204 }
2205
2206 int
2207 safe_frame_unwind_memory (struct frame_info *this_frame,
2208                           CORE_ADDR addr, void *buf, int len)
2209 {
2210   /* NOTE: read_memory_nobpt returns zero on success!  */
2211   return !read_memory_nobpt (addr, buf, len);
2212 }
2213
2214 /* Architecture method.  */
2215
2216 struct gdbarch *
2217 get_frame_arch (struct frame_info *this_frame)
2218 {
2219   return current_gdbarch;
2220 }
2221
2222 /* Stack pointer methods.  */
2223
2224 CORE_ADDR
2225 get_frame_sp (struct frame_info *this_frame)
2226 {
2227   return frame_sp_unwind (this_frame->next);
2228 }
2229
2230 CORE_ADDR
2231 frame_sp_unwind (struct frame_info *next_frame)
2232 {
2233   /* Normality - an architecture that provides a way of obtaining any
2234      frame inner-most address.  */
2235   if (gdbarch_unwind_sp_p (current_gdbarch))
2236     return gdbarch_unwind_sp (current_gdbarch, next_frame);
2237   /* Things are looking grim.  If it's the inner-most frame and there
2238      is a TARGET_READ_SP, then that can be used.  */
2239   if (next_frame->level < 0 && TARGET_READ_SP_P ())
2240     return TARGET_READ_SP ();
2241   /* Now things are really are grim.  Hope that the value returned by
2242      the SP_REGNUM register is meaningful.  */
2243   if (SP_REGNUM >= 0)
2244     {
2245       ULONGEST sp;
2246       frame_unwind_unsigned_register (next_frame, SP_REGNUM, &sp);
2247       return sp;
2248     }
2249   internal_error (__FILE__, __LINE__, "Missing unwind SP method");
2250 }
2251
2252
2253 int
2254 legacy_frame_p (struct gdbarch *current_gdbarch)
2255 {
2256   if (DEPRECATED_INIT_FRAME_PC_P ()
2257       || DEPRECATED_INIT_EXTRA_FRAME_INFO_P ()
2258       || DEPRECATED_FRAME_CHAIN_P ())
2259     /* No question, it's a legacy frame.  */
2260     return 1;
2261   if (gdbarch_unwind_dummy_id_p (current_gdbarch))
2262     /* No question, it's not a legacy frame (provided none of the
2263        deprecated methods checked above are present that is).  */
2264     return 0;
2265   if (DEPRECATED_TARGET_READ_FP_P ()
2266       || DEPRECATED_FP_REGNUM >= 0)
2267     /* Assume it's legacy.  If you're trying to convert a legacy frame
2268        target to the new mechanism, get rid of these.  legacy
2269        get_prev_frame() requires these when unwind_frame_id() isn't
2270        available.  */
2271     return 1;
2272   /* Default to assuming that it's brand new code, and hence not
2273      legacy.  Force it down the non-legacy path so that the new code
2274      uses the new frame mechanism from day one.  Dummy frames won't
2275      work very well but we can live with that.  */
2276   return 0;
2277 }
2278
2279 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2280
2281 static struct cmd_list_element *set_backtrace_cmdlist;
2282 static struct cmd_list_element *show_backtrace_cmdlist;
2283
2284 static void
2285 set_backtrace_cmd (char *args, int from_tty)
2286 {
2287   help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2288 }
2289
2290 static void
2291 show_backtrace_cmd (char *args, int from_tty)
2292 {
2293   cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2294 }
2295
2296 void
2297 _initialize_frame (void)
2298 {
2299   obstack_init (&frame_cache_obstack);
2300
2301   observer_attach_target_changed (frame_observer_target_changed);
2302
2303   add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, "\
2304 Set backtrace specific variables.\n\
2305 Configure backtrace variables such as the backtrace limit",
2306                   &set_backtrace_cmdlist, "set backtrace ",
2307                   0/*allow-unknown*/, &setlist);
2308   add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, "\
2309 Show backtrace specific variables\n\
2310 Show backtrace variables such as the backtrace limit",
2311                   &show_backtrace_cmdlist, "show backtrace ",
2312                   0/*allow-unknown*/, &showlist);
2313
2314   add_setshow_boolean_cmd ("past-main", class_obscure,
2315                            &backtrace_past_main, "\
2316 Set whether backtraces should continue past \"main\".\n\
2317 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2318 the backtrace at \"main\".  Set this variable if you need to see the rest\n\
2319 of the stack trace.", "\
2320 Show whether backtraces should continue past \"main\".\n\
2321 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2322 the backtrace at \"main\".  Set this variable if you need to see the rest\n\
2323 of the stack trace.",
2324                            NULL, NULL, &set_backtrace_cmdlist,
2325                            &show_backtrace_cmdlist);
2326
2327   add_setshow_uinteger_cmd ("limit", class_obscure,
2328                             &backtrace_limit, "\
2329 Set an upper bound on the number of backtrace levels.\n\
2330 No more than the specified number of frames can be displayed or examined.\n\
2331 Zero is unlimited.", "\
2332 Show the upper bound on the number of backtrace levels.",
2333                             NULL, NULL, &set_backtrace_cmdlist,
2334                             &show_backtrace_cmdlist);
2335
2336   /* Debug this files internals. */
2337   add_show_from_set (add_set_cmd ("frame", class_maintenance, var_zinteger,
2338                                   &frame_debug, "Set frame debugging.\n\
2339 When non-zero, frame specific internal debugging is enabled.", &setdebuglist),
2340                      &showdebuglist);
2341 }