34adf3de3a37dd1bdfaaffbaefc15a46a0291aab
[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 #include "objfiles.h"
44 #include "exceptions.h"
45
46 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
47
48 /* We keep a cache of stack frames, each of which is a "struct
49    frame_info".  The innermost one gets allocated (in
50    wait_for_inferior) each time the inferior stops; current_frame
51    points to it.  Additional frames get allocated (in get_prev_frame)
52    as needed, and are chained through the next and prev fields.  Any
53    time that the frame cache becomes invalid (most notably when we
54    execute something, but also if we change how we interpret the
55    frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
56    which reads new symbols)), we should call reinit_frame_cache.  */
57
58 struct frame_info
59 {
60   /* Level of this frame.  The inner-most (youngest) frame is at level
61      0.  As you move towards the outer-most (oldest) frame, the level
62      increases.  This is a cached value.  It could just as easily be
63      computed by counting back from the selected frame to the inner
64      most frame.  */
65   /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
66      reserved to indicate a bogus frame - one that has been created
67      just to keep GDB happy (GDB always needs a frame).  For the
68      moment leave this as speculation.  */
69   int level;
70
71   /* The frame's low-level unwinder and corresponding cache.  The
72      low-level unwinder is responsible for unwinding register values
73      for the previous frame.  The low-level unwind methods are
74      selected based on the presence, or otherwise, of register unwind
75      information such as CFI.  */
76   void *prologue_cache;
77   const struct frame_unwind *unwind;
78
79   /* Cached copy of the previous frame's resume address.  */
80   struct {
81     int p;
82     CORE_ADDR value;
83   } prev_pc;
84   
85   /* Cached copy of the previous frame's function address.  */
86   struct
87   {
88     CORE_ADDR addr;
89     int p;
90   } prev_func;
91   
92   /* This frame's ID.  */
93   struct
94   {
95     int p;
96     struct frame_id value;
97   } this_id;
98   
99   /* The frame's high-level base methods, and corresponding cache.
100      The high level base methods are selected based on the frame's
101      debug info.  */
102   const struct frame_base *base;
103   void *base_cache;
104
105   /* Pointers to the next (down, inner, younger) and previous (up,
106      outer, older) frame_info's in the frame cache.  */
107   struct frame_info *next; /* down, inner, younger */
108   int prev_p;
109   struct frame_info *prev; /* up, outer, older */
110 };
111
112 /* Flag to control debugging.  */
113
114 static int frame_debug;
115
116 /* Flag to indicate whether backtraces should stop at main et.al.  */
117
118 static int backtrace_past_main;
119 static int backtrace_past_entry;
120 static unsigned int backtrace_limit = UINT_MAX;
121
122 static void
123 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
124 {
125   if (p)
126     fprintf_unfiltered (file, "%s=0x%s", name, paddr_nz (addr));
127   else
128     fprintf_unfiltered (file, "!%s", name);
129 }
130
131 void
132 fprint_frame_id (struct ui_file *file, struct frame_id id)
133 {
134   fprintf_unfiltered (file, "{");
135   fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
136   fprintf_unfiltered (file, ",");
137   fprint_field (file, "code", id.code_addr_p, id.code_addr);
138   fprintf_unfiltered (file, ",");
139   fprint_field (file, "special", id.special_addr_p, id.special_addr);
140   fprintf_unfiltered (file, "}");
141 }
142
143 static void
144 fprint_frame_type (struct ui_file *file, enum frame_type type)
145 {
146   switch (type)
147     {
148     case NORMAL_FRAME:
149       fprintf_unfiltered (file, "NORMAL_FRAME");
150       return;
151     case DUMMY_FRAME:
152       fprintf_unfiltered (file, "DUMMY_FRAME");
153       return;
154     case SIGTRAMP_FRAME:
155       fprintf_unfiltered (file, "SIGTRAMP_FRAME");
156       return;
157     default:
158       fprintf_unfiltered (file, "<unknown type>");
159       return;
160     };
161 }
162
163 static void
164 fprint_frame (struct ui_file *file, struct frame_info *fi)
165 {
166   if (fi == NULL)
167     {
168       fprintf_unfiltered (file, "<NULL frame>");
169       return;
170     }
171   fprintf_unfiltered (file, "{");
172   fprintf_unfiltered (file, "level=%d", fi->level);
173   fprintf_unfiltered (file, ",");
174   fprintf_unfiltered (file, "type=");
175   if (fi->unwind != NULL)
176     fprint_frame_type (file, fi->unwind->type);
177   else
178     fprintf_unfiltered (file, "<unknown>");
179   fprintf_unfiltered (file, ",");
180   fprintf_unfiltered (file, "unwind=");
181   if (fi->unwind != NULL)
182     gdb_print_host_address (fi->unwind, file);
183   else
184     fprintf_unfiltered (file, "<unknown>");
185   fprintf_unfiltered (file, ",");
186   fprintf_unfiltered (file, "pc=");
187   if (fi->next != NULL && fi->next->prev_pc.p)
188     fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
189   else
190     fprintf_unfiltered (file, "<unknown>");
191   fprintf_unfiltered (file, ",");
192   fprintf_unfiltered (file, "id=");
193   if (fi->this_id.p)
194     fprint_frame_id (file, fi->this_id.value);
195   else
196     fprintf_unfiltered (file, "<unknown>");
197   fprintf_unfiltered (file, ",");
198   fprintf_unfiltered (file, "func=");
199   if (fi->next != NULL && fi->next->prev_func.p)
200     fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
201   else
202     fprintf_unfiltered (file, "<unknown>");
203   fprintf_unfiltered (file, "}");
204 }
205
206 /* Return a frame uniq ID that can be used to, later, re-find the
207    frame.  */
208
209 struct frame_id
210 get_frame_id (struct frame_info *fi)
211 {
212   if (fi == NULL)
213     {
214       return null_frame_id;
215     }
216   if (!fi->this_id.p)
217     {
218       if (frame_debug)
219         fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
220                             fi->level);
221       /* Find the unwinder.  */
222       if (fi->unwind == NULL)
223         fi->unwind = frame_unwind_find_by_frame (fi->next,
224                                                  &fi->prologue_cache);
225       /* Find THIS frame's ID.  */
226       fi->unwind->this_id (fi->next, &fi->prologue_cache, &fi->this_id.value);
227       fi->this_id.p = 1;
228       if (frame_debug)
229         {
230           fprintf_unfiltered (gdb_stdlog, "-> ");
231           fprint_frame_id (gdb_stdlog, fi->this_id.value);
232           fprintf_unfiltered (gdb_stdlog, " }\n");
233         }
234     }
235   return fi->this_id.value;
236 }
237
238 struct frame_id
239 frame_unwind_id (struct frame_info *next_frame)
240 {
241   /* Use prev_frame, and not get_prev_frame.  The latter will truncate
242      the frame chain, leading to this function unintentionally
243      returning a null_frame_id (e.g., when a caller requests the frame
244      ID of "main()"s caller.  */
245   return get_frame_id (get_prev_frame_1 (next_frame));
246 }
247
248 const struct frame_id null_frame_id; /* All zeros.  */
249
250 struct frame_id
251 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
252                         CORE_ADDR special_addr)
253 {
254   struct frame_id id = null_frame_id;
255   id.stack_addr = stack_addr;
256   id.stack_addr_p = 1;
257   id.code_addr = code_addr;
258   id.code_addr_p = 1;
259   id.special_addr = special_addr;
260   id.special_addr_p = 1;
261   return id;
262 }
263
264 struct frame_id
265 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
266 {
267   struct frame_id id = null_frame_id;
268   id.stack_addr = stack_addr;
269   id.stack_addr_p = 1;
270   id.code_addr = code_addr;
271   id.code_addr_p = 1;
272   return id;
273 }
274
275 struct frame_id
276 frame_id_build_wild (CORE_ADDR stack_addr)
277 {
278   struct frame_id id = null_frame_id;
279   id.stack_addr = stack_addr;
280   id.stack_addr_p = 1;
281   return id;
282 }
283
284 int
285 frame_id_p (struct frame_id l)
286 {
287   int p;
288   /* The frame is valid iff it has a valid stack address.  */
289   p = l.stack_addr_p;
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_p || !r.stack_addr_p)
304     /* Like a NaN, if either ID is invalid, the result is false.
305        Note that a frame ID is invalid iff it is the null frame ID.  */
306     eq = 0;
307   else if (l.stack_addr != r.stack_addr)
308     /* If .stack addresses are different, the frames are different.  */
309     eq = 0;
310   else if (!l.code_addr_p || !r.code_addr_p)
311     /* An invalid code addr is a wild card, always succeed.  */
312     eq = 1;
313   else if (l.code_addr != r.code_addr)
314     /* If .code addresses are different, the frames are different.  */
315     eq = 0;
316   else if (!l.special_addr_p || !r.special_addr_p)
317     /* An invalid special addr is a wild card (or unused), always succeed.  */
318     eq = 1;
319   else if (l.special_addr == r.special_addr)
320     /* Frames are equal.  */
321     eq = 1;
322   else
323     /* No luck.  */
324     eq = 0;
325   if (frame_debug)
326     {
327       fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
328       fprint_frame_id (gdb_stdlog, l);
329       fprintf_unfiltered (gdb_stdlog, ",r=");
330       fprint_frame_id (gdb_stdlog, r);
331       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
332     }
333   return eq;
334 }
335
336 int
337 frame_id_inner (struct frame_id l, struct frame_id r)
338 {
339   int inner;
340   if (!l.stack_addr_p || !r.stack_addr_p)
341     /* Like NaN, any operation involving an invalid ID always fails.  */
342     inner = 0;
343   else
344     /* Only return non-zero when strictly inner than.  Note that, per
345        comment in "frame.h", there is some fuzz here.  Frameless
346        functions are not strictly inner than (same .stack but
347        different .code and/or .special address).  */
348     inner = INNER_THAN (l.stack_addr, r.stack_addr);
349   if (frame_debug)
350     {
351       fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
352       fprint_frame_id (gdb_stdlog, l);
353       fprintf_unfiltered (gdb_stdlog, ",r=");
354       fprint_frame_id (gdb_stdlog, r);
355       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
356     }
357   return inner;
358 }
359
360 struct frame_info *
361 frame_find_by_id (struct frame_id id)
362 {
363   struct frame_info *frame;
364
365   /* ZERO denotes the null frame, let the caller decide what to do
366      about it.  Should it instead return get_current_frame()?  */
367   if (!frame_id_p (id))
368     return NULL;
369
370   for (frame = get_current_frame ();
371        frame != NULL;
372        frame = get_prev_frame (frame))
373     {
374       struct frame_id this = get_frame_id (frame);
375       if (frame_id_eq (id, this))
376         /* An exact match.  */
377         return frame;
378       if (frame_id_inner (id, this))
379         /* Gone to far.  */
380         return NULL;
381       /* Either we're not yet gone far enough out along the frame
382          chain (inner(this,id)), or we're comparing frameless functions
383          (same .base, different .func, no test available).  Struggle
384          on until we've definitly gone to far.  */
385     }
386   return NULL;
387 }
388
389 CORE_ADDR
390 frame_pc_unwind (struct frame_info *this_frame)
391 {
392   if (!this_frame->prev_pc.p)
393     {
394       CORE_ADDR pc;
395       if (this_frame->unwind == NULL)
396         this_frame->unwind
397           = frame_unwind_find_by_frame (this_frame->next,
398                                         &this_frame->prologue_cache);
399       if (this_frame->unwind->prev_pc != NULL)
400         /* A per-frame unwinder, prefer it.  */
401         pc = this_frame->unwind->prev_pc (this_frame->next,
402                                           &this_frame->prologue_cache);
403       else if (gdbarch_unwind_pc_p (current_gdbarch))
404         {
405           /* The right way.  The `pure' way.  The one true way.  This
406              method depends solely on the register-unwind code to
407              determine the value of registers in THIS frame, and hence
408              the value of this frame's PC (resume address).  A typical
409              implementation is no more than:
410            
411              frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
412              return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
413
414              Note: this method is very heavily dependent on a correct
415              register-unwind implementation, it pays to fix that
416              method first; this method is frame type agnostic, since
417              it only deals with register values, it works with any
418              frame.  This is all in stark contrast to the old
419              FRAME_SAVED_PC which would try to directly handle all the
420              different ways that a PC could be unwound.  */
421           pc = gdbarch_unwind_pc (current_gdbarch, this_frame);
422         }
423       else
424         internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
425       this_frame->prev_pc.value = pc;
426       this_frame->prev_pc.p = 1;
427       if (frame_debug)
428         fprintf_unfiltered (gdb_stdlog,
429                             "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
430                             this_frame->level,
431                             paddr_nz (this_frame->prev_pc.value));
432     }
433   return this_frame->prev_pc.value;
434 }
435
436 CORE_ADDR
437 frame_func_unwind (struct frame_info *fi)
438 {
439   if (!fi->prev_func.p)
440     {
441       /* Make certain that this, and not the adjacent, function is
442          found.  */
443       CORE_ADDR addr_in_block = frame_unwind_address_in_block (fi);
444       fi->prev_func.p = 1;
445       fi->prev_func.addr = get_pc_function_start (addr_in_block);
446       if (frame_debug)
447         fprintf_unfiltered (gdb_stdlog,
448                             "{ frame_func_unwind (fi=%d) -> 0x%s }\n",
449                             fi->level, paddr_nz (fi->prev_func.addr));
450     }
451   return fi->prev_func.addr;
452 }
453
454 CORE_ADDR
455 get_frame_func (struct frame_info *fi)
456 {
457   return frame_func_unwind (fi->next);
458 }
459
460 static int
461 do_frame_register_read (void *src, int regnum, void *buf)
462 {
463   frame_register_read (src, regnum, buf);
464   return 1;
465 }
466
467 struct regcache *
468 frame_save_as_regcache (struct frame_info *this_frame)
469 {
470   struct regcache *regcache = regcache_xmalloc (current_gdbarch);
471   struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
472   regcache_save (regcache, do_frame_register_read, this_frame);
473   discard_cleanups (cleanups);
474   return regcache;
475 }
476
477 void
478 frame_pop (struct frame_info *this_frame)
479 {
480   /* Make a copy of all the register values unwound from this frame.
481      Save them in a scratch buffer so that there isn't a race between
482      trying to extract the old values from the current_regcache while
483      at the same time writing new values into that same cache.  */
484   struct regcache *scratch
485     = frame_save_as_regcache (get_prev_frame_1 (this_frame));
486   struct cleanup *cleanups = make_cleanup_regcache_xfree (scratch);
487
488   /* FIXME: cagney/2003-03-16: It should be possible to tell the
489      target's register cache that it is about to be hit with a burst
490      register transfer and that the sequence of register writes should
491      be batched.  The pair target_prepare_to_store() and
492      target_store_registers() kind of suggest this functionality.
493      Unfortunately, they don't implement it.  Their lack of a formal
494      definition can lead to targets writing back bogus values
495      (arguably a bug in the target code mind).  */
496   /* Now copy those saved registers into the current regcache.
497      Here, regcache_cpy() calls regcache_restore().  */
498   regcache_cpy (current_regcache, scratch);
499   do_cleanups (cleanups);
500
501   /* We've made right mess of GDB's local state, just discard
502      everything.  */
503   flush_cached_frames ();
504 }
505
506 void
507 frame_register_unwind (struct frame_info *frame, int regnum,
508                        int *optimizedp, enum lval_type *lvalp,
509                        CORE_ADDR *addrp, int *realnump, void *bufferp)
510 {
511   struct frame_unwind_cache *cache;
512
513   if (frame_debug)
514     {
515       fprintf_unfiltered (gdb_stdlog, "\
516 { frame_register_unwind (frame=%d,regnum=%d(%s),...) ",
517                           frame->level, regnum,
518                           frame_map_regnum_to_name (frame, regnum));
519     }
520
521   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
522      that the value proper does not need to be fetched.  */
523   gdb_assert (optimizedp != NULL);
524   gdb_assert (lvalp != NULL);
525   gdb_assert (addrp != NULL);
526   gdb_assert (realnump != NULL);
527   /* gdb_assert (bufferp != NULL); */
528
529   /* NOTE: cagney/2002-11-27: A program trying to unwind a NULL frame
530      is broken.  There is always a frame.  If there, for some reason,
531      isn't a frame, there is some pretty busted code as it should have
532      detected the problem before calling here.  */
533   gdb_assert (frame != NULL);
534
535   /* Find the unwinder.  */
536   if (frame->unwind == NULL)
537     frame->unwind = frame_unwind_find_by_frame (frame->next,
538                                                 &frame->prologue_cache);
539
540   /* Ask this frame to unwind its register.  See comment in
541      "frame-unwind.h" for why NEXT frame and this unwind cache are
542      passed in.  */
543   frame->unwind->prev_register (frame->next, &frame->prologue_cache, regnum,
544                                 optimizedp, lvalp, addrp, realnump, bufferp);
545
546   if (frame_debug)
547     {
548       fprintf_unfiltered (gdb_stdlog, "->");
549       fprintf_unfiltered (gdb_stdlog, " *optimizedp=%d", (*optimizedp));
550       fprintf_unfiltered (gdb_stdlog, " *lvalp=%d", (int) (*lvalp));
551       fprintf_unfiltered (gdb_stdlog, " *addrp=0x%s", paddr_nz ((*addrp)));
552       fprintf_unfiltered (gdb_stdlog, " *bufferp=");
553       if (bufferp == NULL)
554         fprintf_unfiltered (gdb_stdlog, "<NULL>");
555       else
556         {
557           int i;
558           const unsigned char *buf = bufferp;
559           fprintf_unfiltered (gdb_stdlog, "[");
560           for (i = 0; i < register_size (current_gdbarch, regnum); i++)
561             fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
562           fprintf_unfiltered (gdb_stdlog, "]");
563         }
564       fprintf_unfiltered (gdb_stdlog, " }\n");
565     }
566 }
567
568 void
569 frame_register (struct frame_info *frame, int regnum,
570                 int *optimizedp, enum lval_type *lvalp,
571                 CORE_ADDR *addrp, int *realnump, void *bufferp)
572 {
573   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
574      that the value proper does not need to be fetched.  */
575   gdb_assert (optimizedp != NULL);
576   gdb_assert (lvalp != NULL);
577   gdb_assert (addrp != NULL);
578   gdb_assert (realnump != NULL);
579   /* gdb_assert (bufferp != NULL); */
580
581   /* Obtain the register value by unwinding the register from the next
582      (more inner frame).  */
583   gdb_assert (frame != NULL && frame->next != NULL);
584   frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
585                          realnump, bufferp);
586 }
587
588 void
589 frame_unwind_register (struct frame_info *frame, int regnum, void *buf)
590 {
591   int optimized;
592   CORE_ADDR addr;
593   int realnum;
594   enum lval_type lval;
595   frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
596                          &realnum, buf);
597 }
598
599 void
600 get_frame_register (struct frame_info *frame,
601                     int regnum, void *buf)
602 {
603   frame_unwind_register (frame->next, regnum, buf);
604 }
605
606 LONGEST
607 frame_unwind_register_signed (struct frame_info *frame, int regnum)
608 {
609   char buf[MAX_REGISTER_SIZE];
610   frame_unwind_register (frame, regnum, buf);
611   return extract_signed_integer (buf, register_size (get_frame_arch (frame),
612                                                      regnum));
613 }
614
615 LONGEST
616 get_frame_register_signed (struct frame_info *frame, int regnum)
617 {
618   return frame_unwind_register_signed (frame->next, regnum);
619 }
620
621 ULONGEST
622 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
623 {
624   char buf[MAX_REGISTER_SIZE];
625   frame_unwind_register (frame, regnum, buf);
626   return extract_unsigned_integer (buf, register_size (get_frame_arch (frame),
627                                                        regnum));
628 }
629
630 ULONGEST
631 get_frame_register_unsigned (struct frame_info *frame, int regnum)
632 {
633   return frame_unwind_register_unsigned (frame->next, regnum);
634 }
635
636 void
637 frame_unwind_unsigned_register (struct frame_info *frame, int regnum,
638                                 ULONGEST *val)
639 {
640   char buf[MAX_REGISTER_SIZE];
641   frame_unwind_register (frame, regnum, buf);
642   (*val) = extract_unsigned_integer (buf,
643                                      register_size (get_frame_arch (frame),
644                                                     regnum));
645 }
646
647 void
648 put_frame_register (struct frame_info *frame, int regnum, const void *buf)
649 {
650   struct gdbarch *gdbarch = get_frame_arch (frame);
651   int realnum;
652   int optim;
653   enum lval_type lval;
654   CORE_ADDR addr;
655   frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
656   if (optim)
657     error (_("Attempt to assign to a value that was optimized out."));
658   switch (lval)
659     {
660     case lval_memory:
661       {
662         /* FIXME: write_memory doesn't yet take constant buffers.
663            Arrrg!  */
664         char tmp[MAX_REGISTER_SIZE];
665         memcpy (tmp, buf, register_size (gdbarch, regnum));
666         write_memory (addr, tmp, register_size (gdbarch, regnum));
667         break;
668       }
669     case lval_register:
670       regcache_cooked_write (current_regcache, realnum, buf);
671       break;
672     default:
673       error (_("Attempt to assign to an unmodifiable value."));
674     }
675 }
676
677 /* frame_register_read ()
678
679    Find and return the value of REGNUM for the specified stack frame.
680    The number of bytes copied is REGISTER_SIZE (REGNUM).
681
682    Returns 0 if the register value could not be found.  */
683
684 int
685 frame_register_read (struct frame_info *frame, int regnum, void *myaddr)
686 {
687   int optimized;
688   enum lval_type lval;
689   CORE_ADDR addr;
690   int realnum;
691   frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
692
693   /* FIXME: cagney/2002-05-15: This test is just bogus.
694
695      It indicates that the target failed to supply a value for a
696      register because it was "not available" at this time.  Problem
697      is, the target still has the register and so get saved_register()
698      may be returning a value saved on the stack.  */
699
700   if (register_cached (regnum) < 0)
701     return 0;                   /* register value not available */
702
703   return !optimized;
704 }
705
706
707 /* Map between a frame register number and its name.  A frame register
708    space is a superset of the cooked register space --- it also
709    includes builtin registers.  */
710
711 int
712 frame_map_name_to_regnum (struct frame_info *frame, const char *name, int len)
713 {
714   return user_reg_map_name_to_regnum (get_frame_arch (frame), name, len);
715 }
716
717 const char *
718 frame_map_regnum_to_name (struct frame_info *frame, int regnum)
719 {
720   return user_reg_map_regnum_to_name (get_frame_arch (frame), regnum);
721 }
722
723 /* Create a sentinel frame.  */
724
725 static struct frame_info *
726 create_sentinel_frame (struct regcache *regcache)
727 {
728   struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
729   frame->level = -1;
730   /* Explicitly initialize the sentinel frame's cache.  Provide it
731      with the underlying regcache.  In the future additional
732      information, such as the frame's thread will be added.  */
733   frame->prologue_cache = sentinel_frame_cache (regcache);
734   /* For the moment there is only one sentinel frame implementation.  */
735   frame->unwind = sentinel_frame_unwind;
736   /* Link this frame back to itself.  The frame is self referential
737      (the unwound PC is the same as the pc), so make it so.  */
738   frame->next = frame;
739   /* Make the sentinel frame's ID valid, but invalid.  That way all
740      comparisons with it should fail.  */
741   frame->this_id.p = 1;
742   frame->this_id.value = null_frame_id;
743   if (frame_debug)
744     {
745       fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
746       fprint_frame (gdb_stdlog, frame);
747       fprintf_unfiltered (gdb_stdlog, " }\n");
748     }
749   return frame;
750 }
751
752 /* Info about the innermost stack frame (contents of FP register) */
753
754 static struct frame_info *current_frame;
755
756 /* Cache for frame addresses already read by gdb.  Valid only while
757    inferior is stopped.  Control variables for the frame cache should
758    be local to this module.  */
759
760 static struct obstack frame_cache_obstack;
761
762 void *
763 frame_obstack_zalloc (unsigned long size)
764 {
765   void *data = obstack_alloc (&frame_cache_obstack, size);
766   memset (data, 0, size);
767   return data;
768 }
769
770 /* Return the innermost (currently executing) stack frame.  This is
771    split into two functions.  The function unwind_to_current_frame()
772    is wrapped in catch exceptions so that, even when the unwind of the
773    sentinel frame fails, the function still returns a stack frame.  */
774
775 static int
776 unwind_to_current_frame (struct ui_out *ui_out, void *args)
777 {
778   struct frame_info *frame = get_prev_frame (args);
779   /* A sentinel frame can fail to unwind, e.g., because its PC value
780      lands in somewhere like start.  */
781   if (frame == NULL)
782     return 1;
783   current_frame = frame;
784   return 0;
785 }
786
787 struct frame_info *
788 get_current_frame (void)
789 {
790   /* First check, and report, the lack of registers.  Having GDB
791      report "No stack!" or "No memory" when the target doesn't even
792      have registers is very confusing.  Besides, "printcmd.exp"
793      explicitly checks that ``print $pc'' with no registers prints "No
794      registers".  */
795   if (!target_has_registers)
796     error (_("No registers."));
797   if (!target_has_stack)
798     error (_("No stack."));
799   if (!target_has_memory)
800     error (_("No memory."));
801   if (current_frame == NULL)
802     {
803       struct frame_info *sentinel_frame =
804         create_sentinel_frame (current_regcache);
805       if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
806                             RETURN_MASK_ERROR) != 0)
807         {
808           /* Oops! Fake a current frame?  Is this useful?  It has a PC
809              of zero, for instance.  */
810           current_frame = sentinel_frame;
811         }
812     }
813   return current_frame;
814 }
815
816 /* The "selected" stack frame is used by default for local and arg
817    access.  May be zero, for no selected frame.  */
818
819 struct frame_info *deprecated_selected_frame;
820
821 /* Return the selected frame.  Always non-NULL (unless there isn't an
822    inferior sufficient for creating a frame) in which case an error is
823    thrown.  */
824
825 struct frame_info *
826 get_selected_frame (const char *message)
827 {
828   if (deprecated_selected_frame == NULL)
829     {
830       if (message != NULL && (!target_has_registers
831                               || !target_has_stack
832                               || !target_has_memory))
833         error (("%s"), message);
834       /* Hey!  Don't trust this.  It should really be re-finding the
835          last selected frame of the currently selected thread.  This,
836          though, is better than nothing.  */
837       select_frame (get_current_frame ());
838     }
839   /* There is always a frame.  */
840   gdb_assert (deprecated_selected_frame != NULL);
841   return deprecated_selected_frame;
842 }
843
844 /* This is a variant of get_selected_frame() which can be called when
845    the inferior does not have a frame; in that case it will return
846    NULL instead of calling error().  */
847
848 struct frame_info *
849 deprecated_safe_get_selected_frame (void)
850 {
851   if (!target_has_registers || !target_has_stack || !target_has_memory)
852     return NULL;
853   return get_selected_frame (NULL);
854 }
855
856 /* Select frame FI (or NULL - to invalidate the current frame).  */
857
858 void
859 select_frame (struct frame_info *fi)
860 {
861   struct symtab *s;
862
863   deprecated_selected_frame = fi;
864   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
865      frame is being invalidated.  */
866   if (deprecated_selected_frame_level_changed_hook)
867     deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
868
869   /* FIXME: kseitz/2002-08-28: It would be nice to call
870      selected_frame_level_changed_event() right here, but due to limitations
871      in the current interfaces, we would end up flooding UIs with events
872      because select_frame() is used extensively internally.
873
874      Once we have frame-parameterized frame (and frame-related) commands,
875      the event notification can be moved here, since this function will only
876      be called when the user's selected frame is being changed. */
877
878   /* Ensure that symbols for this frame are read in.  Also, determine the
879      source language of this frame, and switch to it if desired.  */
880   if (fi)
881     {
882       /* We retrieve the frame's symtab by using the frame PC.  However
883          we cannot use the frame PC as-is, because it usually points to
884          the instruction following the "call", which is sometimes the
885          first instruction of another function.  So we rely on
886          get_frame_address_in_block() which provides us with a PC which
887          is guaranteed to be inside the frame's code block.  */
888       s = find_pc_symtab (get_frame_address_in_block (fi));
889       if (s
890           && s->language != current_language->la_language
891           && s->language != language_unknown
892           && language_mode == language_mode_auto)
893         {
894           set_language (s->language);
895         }
896     }
897 }
898         
899 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
900    Always returns a non-NULL value.  */
901
902 struct frame_info *
903 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
904 {
905   struct frame_info *fi;
906
907   if (frame_debug)
908     {
909       fprintf_unfiltered (gdb_stdlog,
910                           "{ create_new_frame (addr=0x%s, pc=0x%s) ",
911                           paddr_nz (addr), paddr_nz (pc));
912     }
913
914   fi = frame_obstack_zalloc (sizeof (struct frame_info));
915
916   fi->next = create_sentinel_frame (current_regcache);
917
918   /* Select/initialize both the unwind function and the frame's type
919      based on the PC.  */
920   fi->unwind = frame_unwind_find_by_frame (fi->next, &fi->prologue_cache);
921
922   fi->this_id.p = 1;
923   deprecated_update_frame_base_hack (fi, addr);
924   deprecated_update_frame_pc_hack (fi, pc);
925
926   if (frame_debug)
927     {
928       fprintf_unfiltered (gdb_stdlog, "-> ");
929       fprint_frame (gdb_stdlog, fi);
930       fprintf_unfiltered (gdb_stdlog, " }\n");
931     }
932
933   return fi;
934 }
935
936 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
937    innermost frame).  Be careful to not fall off the bottom of the
938    frame chain and onto the sentinel frame.  */
939
940 struct frame_info *
941 get_next_frame (struct frame_info *this_frame)
942 {
943   if (this_frame->level > 0)
944     return this_frame->next;
945   else
946     return NULL;
947 }
948
949 /* Observer for the target_changed event.  */
950
951 void
952 frame_observer_target_changed (struct target_ops *target)
953 {
954   flush_cached_frames ();
955 }
956
957 /* Flush the entire frame cache.  */
958
959 void
960 flush_cached_frames (void)
961 {
962   /* Since we can't really be sure what the first object allocated was */
963   obstack_free (&frame_cache_obstack, 0);
964   obstack_init (&frame_cache_obstack);
965
966   current_frame = NULL;         /* Invalidate cache */
967   select_frame (NULL);
968   annotate_frames_invalid ();
969   if (frame_debug)
970     fprintf_unfiltered (gdb_stdlog, "{ flush_cached_frames () }\n");
971 }
972
973 /* Flush the frame cache, and start a new one if necessary.  */
974
975 void
976 reinit_frame_cache (void)
977 {
978   flush_cached_frames ();
979
980   /* FIXME: The inferior_ptid test is wrong if there is a corefile.  */
981   if (PIDGET (inferior_ptid) != 0)
982     {
983       select_frame (get_current_frame ());
984     }
985 }
986
987 /* Return a "struct frame_info" corresponding to the frame that called
988    THIS_FRAME.  Returns NULL if there is no such frame.
989
990    Unlike get_prev_frame, this function always tries to unwind the
991    frame.  */
992
993 static struct frame_info *
994 get_prev_frame_1 (struct frame_info *this_frame)
995 {
996   struct frame_info *prev_frame;
997   struct frame_id this_id;
998
999   gdb_assert (this_frame != NULL);
1000
1001   if (frame_debug)
1002     {
1003       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1004       if (this_frame != NULL)
1005         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1006       else
1007         fprintf_unfiltered (gdb_stdlog, "<NULL>");
1008       fprintf_unfiltered (gdb_stdlog, ") ");
1009     }
1010
1011   /* Only try to do the unwind once.  */
1012   if (this_frame->prev_p)
1013     {
1014       if (frame_debug)
1015         {
1016           fprintf_unfiltered (gdb_stdlog, "-> ");
1017           fprint_frame (gdb_stdlog, this_frame->prev);
1018           fprintf_unfiltered (gdb_stdlog, " // cached \n");
1019         }
1020       return this_frame->prev;
1021     }
1022   this_frame->prev_p = 1;
1023
1024   /* Check that this frame's ID was valid.  If it wasn't, don't try to
1025      unwind to the prev frame.  Be careful to not apply this test to
1026      the sentinel frame.  */
1027   this_id = get_frame_id (this_frame);
1028   if (this_frame->level >= 0 && !frame_id_p (this_id))
1029     {
1030       if (frame_debug)
1031         {
1032           fprintf_unfiltered (gdb_stdlog, "-> ");
1033           fprint_frame (gdb_stdlog, NULL);
1034           fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1035         }
1036       return NULL;
1037     }
1038
1039   /* Check that this frame's ID isn't inner to (younger, below, next)
1040      the next frame.  This happens when a frame unwind goes backwards.
1041      Exclude signal trampolines (due to sigaltstack the frame ID can
1042      go backwards) and sentinel frames (the test is meaningless).  */
1043   if (this_frame->next->level >= 0
1044       && this_frame->next->unwind->type != SIGTRAMP_FRAME
1045       && frame_id_inner (this_id, get_frame_id (this_frame->next)))
1046     error (_("Previous frame inner to this frame (corrupt stack?)"));
1047
1048   /* Check that this and the next frame are not identical.  If they
1049      are, there is most likely a stack cycle.  As with the inner-than
1050      test above, avoid comparing the inner-most and sentinel frames.  */
1051   if (this_frame->level > 0
1052       && frame_id_eq (this_id, get_frame_id (this_frame->next)))
1053     error (_("Previous frame identical to this frame (corrupt stack?)"));
1054
1055   /* Allocate the new frame but do not wire it in to the frame chain.
1056      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1057      frame->next to pull some fancy tricks (of course such code is, by
1058      definition, recursive).  Try to prevent it.
1059
1060      There is no reason to worry about memory leaks, should the
1061      remainder of the function fail.  The allocated memory will be
1062      quickly reclaimed when the frame cache is flushed, and the `we've
1063      been here before' check above will stop repeated memory
1064      allocation calls.  */
1065   prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1066   prev_frame->level = this_frame->level + 1;
1067
1068   /* Don't yet compute ->unwind (and hence ->type).  It is computed
1069      on-demand in get_frame_type, frame_register_unwind, and
1070      get_frame_id.  */
1071
1072   /* Don't yet compute the frame's ID.  It is computed on-demand by
1073      get_frame_id().  */
1074
1075   /* The unwound frame ID is validate at the start of this function,
1076      as part of the logic to decide if that frame should be further
1077      unwound, and not here while the prev frame is being created.
1078      Doing this makes it possible for the user to examine a frame that
1079      has an invalid frame ID.
1080
1081      Some very old VAX code noted: [...]  For the sake of argument,
1082      suppose that the stack is somewhat trashed (which is one reason
1083      that "info frame" exists).  So, return 0 (indicating we don't
1084      know the address of the arglist) if we don't know what frame this
1085      frame calls.  */
1086
1087   /* Link it in.  */
1088   this_frame->prev = prev_frame;
1089   prev_frame->next = this_frame;
1090
1091   if (frame_debug)
1092     {
1093       fprintf_unfiltered (gdb_stdlog, "-> ");
1094       fprint_frame (gdb_stdlog, prev_frame);
1095       fprintf_unfiltered (gdb_stdlog, " }\n");
1096     }
1097
1098   return prev_frame;
1099 }
1100
1101 /* Debug routine to print a NULL frame being returned.  */
1102
1103 static void
1104 frame_debug_got_null_frame (struct ui_file *file,
1105                             struct frame_info *this_frame,
1106                             const char *reason)
1107 {
1108   if (frame_debug)
1109     {
1110       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1111       if (this_frame != NULL)
1112         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1113       else
1114         fprintf_unfiltered (gdb_stdlog, "<NULL>");
1115       fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1116     }
1117 }
1118
1119 /* Is this (non-sentinel) frame in the "main"() function?  */
1120
1121 static int
1122 inside_main_func (struct frame_info *this_frame)
1123 {
1124   struct minimal_symbol *msymbol;
1125   CORE_ADDR maddr;
1126
1127   if (symfile_objfile == 0)
1128     return 0;
1129   msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1130   if (msymbol == NULL)
1131     return 0;
1132   /* Make certain that the code, and not descriptor, address is
1133      returned.  */
1134   maddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
1135                                               SYMBOL_VALUE_ADDRESS (msymbol),
1136                                               &current_target);
1137   return maddr == get_frame_func (this_frame);
1138 }
1139
1140 /* Test whether THIS_FRAME is inside the process entry point function.  */
1141
1142 static int
1143 inside_entry_func (struct frame_info *this_frame)
1144 {
1145   return (get_frame_func (this_frame) == entry_point_address ());
1146 }
1147
1148 /* Return a structure containing various interesting information about
1149    the frame that called THIS_FRAME.  Returns NULL if there is entier
1150    no such frame or the frame fails any of a set of target-independent
1151    condition that should terminate the frame chain (e.g., as unwinding
1152    past main()).
1153
1154    This function should not contain target-dependent tests, such as
1155    checking whether the program-counter is zero.  */
1156
1157 struct frame_info *
1158 get_prev_frame (struct frame_info *this_frame)
1159 {
1160   struct frame_info *prev_frame;
1161
1162   /* Return the inner-most frame, when the caller passes in NULL.  */
1163   /* NOTE: cagney/2002-11-09: Not sure how this would happen.  The
1164      caller should have previously obtained a valid frame using
1165      get_selected_frame() and then called this code - only possibility
1166      I can think of is code behaving badly.
1167
1168      NOTE: cagney/2003-01-10: Talk about code behaving badly.  Check
1169      block_innermost_frame().  It does the sequence: frame = NULL;
1170      while (1) { frame = get_prev_frame (frame); .... }.  Ulgh!  Why
1171      it couldn't be written better, I don't know.
1172
1173      NOTE: cagney/2003-01-11: I suspect what is happening in
1174      block_innermost_frame() is, when the target has no state
1175      (registers, memory, ...), it is still calling this function.  The
1176      assumption being that this function will return NULL indicating
1177      that a frame isn't possible, rather than checking that the target
1178      has state and then calling get_current_frame() and
1179      get_prev_frame().  This is a guess mind.  */
1180   if (this_frame == NULL)
1181     {
1182       /* NOTE: cagney/2002-11-09: There was a code segment here that
1183          would error out when CURRENT_FRAME was NULL.  The comment
1184          that went with it made the claim ...
1185
1186          ``This screws value_of_variable, which just wants a nice
1187          clean NULL return from block_innermost_frame if there are no
1188          frames.  I don't think I've ever seen this message happen
1189          otherwise.  And returning NULL here is a perfectly legitimate
1190          thing to do.''
1191
1192          Per the above, this code shouldn't even be called with a NULL
1193          THIS_FRAME.  */
1194       frame_debug_got_null_frame (gdb_stdlog, this_frame, "this_frame NULL");
1195       return current_frame;
1196     }
1197
1198   /* There is always a frame.  If this assertion fails, suspect that
1199      something should be calling get_selected_frame() or
1200      get_current_frame().  */
1201   gdb_assert (this_frame != NULL);
1202
1203   /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1204      sense to stop unwinding at a dummy frame.  One place where a dummy
1205      frame may have an address "inside_main_func" is on HPUX.  On HPUX, the
1206      pcsqh register (space register for the instruction at the head of the
1207      instruction queue) cannot be written directly; the only way to set it
1208      is to branch to code that is in the target space.  In order to implement
1209      frame dummies on HPUX, the called function is made to jump back to where 
1210      the inferior was when the user function was called.  If gdb was inside 
1211      the main function when we created the dummy frame, the dummy frame will 
1212      point inside the main function.  */
1213   if (this_frame->level >= 0
1214       && get_frame_type (this_frame) != DUMMY_FRAME
1215       && !backtrace_past_main
1216       && inside_main_func (this_frame))
1217     /* Don't unwind past main().  Note, this is done _before_ the
1218        frame has been marked as previously unwound.  That way if the
1219        user later decides to enable unwinds past main(), that will
1220        automatically happen.  */
1221     {
1222       frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside main func");
1223       return NULL;
1224     }
1225
1226   if (this_frame->level > backtrace_limit)
1227     {
1228       error (_("Backtrace limit of %d exceeded"), backtrace_limit);
1229     }
1230
1231   /* If we're already inside the entry function for the main objfile,
1232      then it isn't valid.  Don't apply this test to a dummy frame -
1233      dummy frame PCs typically land in the entry func.  Don't apply
1234      this test to the sentinel frame.  Sentinel frames should always
1235      be allowed to unwind.  */
1236   /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1237      wasn't checking for "main" in the minimal symbols.  With that
1238      fixed asm-source tests now stop in "main" instead of halting the
1239      backtrace in weird and wonderful ways somewhere inside the entry
1240      file.  Suspect that tests for inside the entry file/func were
1241      added to work around that (now fixed) case.  */
1242   /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1243      suggested having the inside_entry_func test use the
1244      inside_main_func() msymbol trick (along with entry_point_address()
1245      I guess) to determine the address range of the start function.
1246      That should provide a far better stopper than the current
1247      heuristics.  */
1248   /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1249      applied tail-call optimizations to main so that a function called 
1250      from main returns directly to the caller of main.  Since we don't
1251      stop at main, we should at least stop at the entry point of the
1252      application.  */
1253   if (!backtrace_past_entry
1254       && get_frame_type (this_frame) != DUMMY_FRAME && this_frame->level >= 0
1255       && inside_entry_func (this_frame))
1256     {
1257       frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside entry func");
1258       return NULL;
1259     }
1260
1261   /* Assume that the only way to get a zero PC is through something
1262      like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1263      will never unwind a zero PC.  */
1264   if (this_frame->level > 0
1265       && get_frame_type (this_frame) == NORMAL_FRAME
1266       && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1267       && get_frame_pc (this_frame) == 0)
1268     {
1269       frame_debug_got_null_frame (gdb_stdlog, this_frame, "zero PC");
1270       return NULL;
1271     }
1272
1273   return get_prev_frame_1 (this_frame);
1274 }
1275
1276 CORE_ADDR
1277 get_frame_pc (struct frame_info *frame)
1278 {
1279   gdb_assert (frame->next != NULL);
1280   return frame_pc_unwind (frame->next);
1281 }
1282
1283 /* Return an address of that falls within the frame's code block.  */
1284
1285 CORE_ADDR
1286 frame_unwind_address_in_block (struct frame_info *next_frame)
1287 {
1288   /* A draft address.  */
1289   CORE_ADDR pc = frame_pc_unwind (next_frame);
1290
1291   /* If THIS frame is not inner most (i.e., NEXT isn't the sentinel),
1292      and NEXT is `normal' (i.e., not a sigtramp, dummy, ....) THIS
1293      frame's PC ends up pointing at the instruction fallowing the
1294      "call".  Adjust that PC value so that it falls on the call
1295      instruction (which, hopefully, falls within THIS frame's code
1296      block.  So far it's proved to be a very good approximation.  See
1297      get_frame_type() for why ->type can't be used.  */
1298   if (next_frame->level >= 0
1299       && get_frame_type (next_frame) == NORMAL_FRAME)
1300     --pc;
1301   return pc;
1302 }
1303
1304 CORE_ADDR
1305 get_frame_address_in_block (struct frame_info *this_frame)
1306 {
1307   return frame_unwind_address_in_block (this_frame->next);
1308 }
1309
1310 static int
1311 pc_notcurrent (struct frame_info *frame)
1312 {
1313   /* If FRAME is not the innermost frame, that normally means that
1314      FRAME->pc points at the return instruction (which is *after* the
1315      call instruction), and we want to get the line containing the
1316      call (because the call is where the user thinks the program is).
1317      However, if the next frame is either a SIGTRAMP_FRAME or a
1318      DUMMY_FRAME, then the next frame will contain a saved interrupt
1319      PC and such a PC indicates the current (rather than next)
1320      instruction/line, consequently, for such cases, want to get the
1321      line containing fi->pc.  */
1322   struct frame_info *next = get_next_frame (frame);
1323   int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1324   return notcurrent;
1325 }
1326
1327 void
1328 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1329 {
1330   (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
1331 }
1332
1333 /* Per "frame.h", return the ``address'' of the frame.  Code should
1334    really be using get_frame_id().  */
1335 CORE_ADDR
1336 get_frame_base (struct frame_info *fi)
1337 {
1338   return get_frame_id (fi).stack_addr;
1339 }
1340
1341 /* High-level offsets into the frame.  Used by the debug info.  */
1342
1343 CORE_ADDR
1344 get_frame_base_address (struct frame_info *fi)
1345 {
1346   if (get_frame_type (fi) != NORMAL_FRAME)
1347     return 0;
1348   if (fi->base == NULL)
1349     fi->base = frame_base_find_by_frame (fi->next);
1350   /* Sneaky: If the low-level unwind and high-level base code share a
1351      common unwinder, let them share the prologue cache.  */
1352   if (fi->base->unwind == fi->unwind)
1353     return fi->base->this_base (fi->next, &fi->prologue_cache);
1354   return fi->base->this_base (fi->next, &fi->base_cache);
1355 }
1356
1357 CORE_ADDR
1358 get_frame_locals_address (struct frame_info *fi)
1359 {
1360   void **cache;
1361   if (get_frame_type (fi) != NORMAL_FRAME)
1362     return 0;
1363   /* If there isn't a frame address method, find it.  */
1364   if (fi->base == NULL)
1365     fi->base = frame_base_find_by_frame (fi->next);
1366   /* Sneaky: If the low-level unwind and high-level base code share a
1367      common unwinder, let them share the prologue cache.  */
1368   if (fi->base->unwind == fi->unwind)
1369     cache = &fi->prologue_cache;
1370   else
1371     cache = &fi->base_cache;
1372   return fi->base->this_locals (fi->next, cache);
1373 }
1374
1375 CORE_ADDR
1376 get_frame_args_address (struct frame_info *fi)
1377 {
1378   void **cache;
1379   if (get_frame_type (fi) != NORMAL_FRAME)
1380     return 0;
1381   /* If there isn't a frame address method, find it.  */
1382   if (fi->base == NULL)
1383     fi->base = frame_base_find_by_frame (fi->next);
1384   /* Sneaky: If the low-level unwind and high-level base code share a
1385      common unwinder, let them share the prologue cache.  */
1386   if (fi->base->unwind == fi->unwind)
1387     cache = &fi->prologue_cache;
1388   else
1389     cache = &fi->base_cache;
1390   return fi->base->this_args (fi->next, cache);
1391 }
1392
1393 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1394    or -1 for a NULL frame.  */
1395
1396 int
1397 frame_relative_level (struct frame_info *fi)
1398 {
1399   if (fi == NULL)
1400     return -1;
1401   else
1402     return fi->level;
1403 }
1404
1405 enum frame_type
1406 get_frame_type (struct frame_info *frame)
1407 {
1408   if (frame->unwind == NULL)
1409     /* Initialize the frame's unwinder because that's what
1410        provides the frame's type.  */
1411     frame->unwind = frame_unwind_find_by_frame (frame->next, 
1412                                                 &frame->prologue_cache);
1413   return frame->unwind->type;
1414 }
1415
1416 void
1417 deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
1418 {
1419   if (frame_debug)
1420     fprintf_unfiltered (gdb_stdlog,
1421                         "{ deprecated_update_frame_pc_hack (frame=%d,pc=0x%s) }\n",
1422                         frame->level, paddr_nz (pc));
1423   /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are
1424      maintaining a locally allocated frame object.  Since such frames
1425      are not in the frame chain, it isn't possible to assume that the
1426      frame has a next.  Sigh.  */
1427   if (frame->next != NULL)
1428     {
1429       /* While we're at it, update this frame's cached PC value, found
1430          in the next frame.  Oh for the day when "struct frame_info"
1431          is opaque and this hack on hack can just go away.  */
1432       frame->next->prev_pc.value = pc;
1433       frame->next->prev_pc.p = 1;
1434     }
1435 }
1436
1437 void
1438 deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
1439 {
1440   if (frame_debug)
1441     fprintf_unfiltered (gdb_stdlog,
1442                         "{ deprecated_update_frame_base_hack (frame=%d,base=0x%s) }\n",
1443                         frame->level, paddr_nz (base));
1444   /* See comment in "frame.h".  */
1445   frame->this_id.value.stack_addr = base;
1446 }
1447
1448 /* Memory access methods.  */
1449
1450 void
1451 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr, void *buf,
1452                   int len)
1453 {
1454   read_memory (addr, buf, len);
1455 }
1456
1457 LONGEST
1458 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1459                          int len)
1460 {
1461   return read_memory_integer (addr, len);
1462 }
1463
1464 ULONGEST
1465 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1466                            int len)
1467 {
1468   return read_memory_unsigned_integer (addr, len);
1469 }
1470
1471 int
1472 safe_frame_unwind_memory (struct frame_info *this_frame,
1473                           CORE_ADDR addr, void *buf, int len)
1474 {
1475   /* NOTE: deprecated_read_memory_nobpt returns zero on success!  */
1476   return !deprecated_read_memory_nobpt (addr, buf, len);
1477 }
1478
1479 /* Architecture method.  */
1480
1481 struct gdbarch *
1482 get_frame_arch (struct frame_info *this_frame)
1483 {
1484   return current_gdbarch;
1485 }
1486
1487 /* Stack pointer methods.  */
1488
1489 CORE_ADDR
1490 get_frame_sp (struct frame_info *this_frame)
1491 {
1492   return frame_sp_unwind (this_frame->next);
1493 }
1494
1495 CORE_ADDR
1496 frame_sp_unwind (struct frame_info *next_frame)
1497 {
1498   /* Normality - an architecture that provides a way of obtaining any
1499      frame inner-most address.  */
1500   if (gdbarch_unwind_sp_p (current_gdbarch))
1501     return gdbarch_unwind_sp (current_gdbarch, next_frame);
1502   /* Things are looking grim.  If it's the inner-most frame and there
1503      is a TARGET_READ_SP, then that can be used.  */
1504   if (next_frame->level < 0 && TARGET_READ_SP_P ())
1505     return TARGET_READ_SP ();
1506   /* Now things are really are grim.  Hope that the value returned by
1507      the SP_REGNUM register is meaningful.  */
1508   if (SP_REGNUM >= 0)
1509     {
1510       ULONGEST sp;
1511       frame_unwind_unsigned_register (next_frame, SP_REGNUM, &sp);
1512       return sp;
1513     }
1514   internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
1515 }
1516
1517 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
1518
1519 static struct cmd_list_element *set_backtrace_cmdlist;
1520 static struct cmd_list_element *show_backtrace_cmdlist;
1521
1522 static void
1523 set_backtrace_cmd (char *args, int from_tty)
1524 {
1525   help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
1526 }
1527
1528 static void
1529 show_backtrace_cmd (char *args, int from_tty)
1530 {
1531   cmd_show_list (show_backtrace_cmdlist, from_tty, "");
1532 }
1533
1534 void
1535 _initialize_frame (void)
1536 {
1537   obstack_init (&frame_cache_obstack);
1538
1539   observer_attach_target_changed (frame_observer_target_changed);
1540
1541   add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
1542 Set backtrace specific variables.\n\
1543 Configure backtrace variables such as the backtrace limit"),
1544                   &set_backtrace_cmdlist, "set backtrace ",
1545                   0/*allow-unknown*/, &setlist);
1546   add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
1547 Show backtrace specific variables\n\
1548 Show backtrace variables such as the backtrace limit"),
1549                   &show_backtrace_cmdlist, "show backtrace ",
1550                   0/*allow-unknown*/, &showlist);
1551
1552   add_setshow_boolean_cmd ("past-main", class_obscure,
1553                            &backtrace_past_main, _("\
1554 Set whether backtraces should continue past \"main\"."), _("\
1555 Show whether backtraces should continue past \"main\"."), _("\
1556 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1557 the backtrace at \"main\".  Set this variable if you need to see the rest\n\
1558 of the stack trace."),
1559                            NULL,
1560                            NULL, /* FIXME: i18n: Whether backtraces should continue past \"main\" is %s.  */
1561                            &set_backtrace_cmdlist,
1562                            &show_backtrace_cmdlist);
1563
1564   add_setshow_boolean_cmd ("past-entry", class_obscure,
1565                            &backtrace_past_entry, _("\
1566 Set whether backtraces should continue past the entry point of a program."),
1567                            _("\
1568 Show whether backtraces should continue past the entry point of a program."),
1569                            _("\
1570 Normally there are no callers beyond the entry point of a program, so GDB\n\
1571 will terminate the backtrace there.  Set this variable if you need to see \n\
1572 the rest of the stack trace."),
1573                            NULL,
1574                            NULL, /* FIXME: i18n: Whether backtraces should continue past the entry point is %s.  */
1575                            &set_backtrace_cmdlist,
1576                            &show_backtrace_cmdlist);
1577
1578   add_setshow_uinteger_cmd ("limit", class_obscure,
1579                             &backtrace_limit, _("\
1580 Set an upper bound on the number of backtrace levels."), _("\
1581 Show the upper bound on the number of backtrace levels."), _("\
1582 No more than the specified number of frames can be displayed or examined.\n\
1583 Zero is unlimited."),
1584                             NULL,
1585                             NULL, /* FIXME: i18n: An upper bound on the number of backtrace levels is %s.  */
1586                             &set_backtrace_cmdlist,
1587                             &show_backtrace_cmdlist);
1588
1589   /* Debug this files internals. */
1590   deprecated_add_show_from_set
1591     (add_set_cmd ("frame", class_maintenance, var_zinteger,
1592                   &frame_debug, "Set frame debugging.\n\
1593 When non-zero, frame specific internal debugging is enabled.", &setdebuglist),
1594      &showdebuglist);
1595 }