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