Avoid quadratic behavior when computing the value of a register.
[platform/upstream/binutils.git] / gdb / frame.c
1 /* Cache and manage frames for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4    2002, 2003, 2004, 2007, 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "target.h"
24 #include "value.h"
25 #include "inferior.h"   /* for inferior_ptid */
26 #include "regcache.h"
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include "user-regs.h"
30 #include "gdb_obstack.h"
31 #include "dummy-frame.h"
32 #include "sentinel-frame.h"
33 #include "gdbcore.h"
34 #include "annotate.h"
35 #include "language.h"
36 #include "frame-unwind.h"
37 #include "frame-base.h"
38 #include "command.h"
39 #include "gdbcmd.h"
40 #include "observer.h"
41 #include "objfiles.h"
42 #include "exceptions.h"
43 #include "gdbthread.h"
44 #include "block.h"
45 #include "inline-frame.h"
46
47 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
48 static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
49
50 /* We keep a cache of stack frames, each of which is a "struct
51    frame_info".  The innermost one gets allocated (in
52    wait_for_inferior) each time the inferior stops; current_frame
53    points to it.  Additional frames get allocated (in get_prev_frame)
54    as needed, and are chained through the next and prev fields.  Any
55    time that the frame cache becomes invalid (most notably when we
56    execute something, but also if we change how we interpret the
57    frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
58    which reads new symbols)), we should call reinit_frame_cache.  */
59
60 struct frame_info
61 {
62   /* Level of this frame.  The inner-most (youngest) frame is at level
63      0.  As you move towards the outer-most (oldest) frame, the level
64      increases.  This is a cached value.  It could just as easily be
65      computed by counting back from the selected frame to the inner
66      most frame.  */
67   /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
68      reserved to indicate a bogus frame - one that has been created
69      just to keep GDB happy (GDB always needs a frame).  For the
70      moment leave this as speculation.  */
71   int level;
72
73   /* The frame's low-level unwinder and corresponding cache.  The
74      low-level unwinder is responsible for unwinding register values
75      for the previous frame.  The low-level unwind methods are
76      selected based on the presence, or otherwise, of register unwind
77      information such as CFI.  */
78   void *prologue_cache;
79   const struct frame_unwind *unwind;
80
81   /* Cached copy of the previous frame's architecture.  */
82   struct
83   {
84     int p;
85     struct gdbarch *arch;
86   } prev_arch;
87
88   /* Cached copy of the previous frame's resume address.  */
89   struct {
90     int p;
91     CORE_ADDR value;
92   } prev_pc;
93   
94   /* Cached copy of the previous frame's function address.  */
95   struct
96   {
97     CORE_ADDR addr;
98     int p;
99   } prev_func;
100   
101   /* This frame's ID.  */
102   struct
103   {
104     int p;
105     struct frame_id value;
106   } this_id;
107   
108   /* The frame's high-level base methods, and corresponding cache.
109      The high level base methods are selected based on the frame's
110      debug info.  */
111   const struct frame_base *base;
112   void *base_cache;
113
114   /* Pointers to the next (down, inner, younger) and previous (up,
115      outer, older) frame_info's in the frame cache.  */
116   struct frame_info *next; /* down, inner, younger */
117   int prev_p;
118   struct frame_info *prev; /* up, outer, older */
119
120   /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
121      could.  Only valid when PREV_P is set.  */
122   enum unwind_stop_reason stop_reason;
123 };
124
125 /* A frame stash used to speed up frame lookups.  */
126
127 /* We currently only stash one frame at a time, as this seems to be
128    sufficient for now.  */
129 static struct frame_info *frame_stash = NULL;
130
131 /* Add the following FRAME to the frame stash.  */
132
133 static void
134 frame_stash_add (struct frame_info *frame)
135 {
136   frame_stash = frame;
137 }
138
139 /* Search the frame stash for an entry with the given frame ID.
140    If found, return that frame.  Otherwise return NULL.  */
141
142 static struct frame_info *
143 frame_stash_find (struct frame_id id)
144 {
145   if (frame_stash && frame_id_eq (frame_stash->this_id.value, id))
146     return frame_stash;
147
148   return NULL;
149 }
150
151 /* Invalidate the frame stash by removing all entries in it.  */
152
153 static void
154 frame_stash_invalidate (void)
155 {
156   frame_stash = NULL;
157 }
158
159 /* Flag to control debugging.  */
160
161 int frame_debug;
162 static void
163 show_frame_debug (struct ui_file *file, int from_tty,
164                   struct cmd_list_element *c, const char *value)
165 {
166   fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
167 }
168
169 /* Flag to indicate whether backtraces should stop at main et.al.  */
170
171 static int backtrace_past_main;
172 static void
173 show_backtrace_past_main (struct ui_file *file, int from_tty,
174                           struct cmd_list_element *c, const char *value)
175 {
176   fprintf_filtered (file, _("\
177 Whether backtraces should continue past \"main\" is %s.\n"),
178                     value);
179 }
180
181 static int backtrace_past_entry;
182 static void
183 show_backtrace_past_entry (struct ui_file *file, int from_tty,
184                            struct cmd_list_element *c, const char *value)
185 {
186   fprintf_filtered (file, _("\
187 Whether backtraces should continue past the entry point of a program is %s.\n"),
188                     value);
189 }
190
191 static int backtrace_limit = INT_MAX;
192 static void
193 show_backtrace_limit (struct ui_file *file, int from_tty,
194                       struct cmd_list_element *c, const char *value)
195 {
196   fprintf_filtered (file, _("\
197 An upper bound on the number of backtrace levels is %s.\n"),
198                     value);
199 }
200
201
202 static void
203 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
204 {
205   if (p)
206     fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
207   else
208     fprintf_unfiltered (file, "!%s", name);
209 }
210
211 void
212 fprint_frame_id (struct ui_file *file, struct frame_id id)
213 {
214   fprintf_unfiltered (file, "{");
215   fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
216   fprintf_unfiltered (file, ",");
217   fprint_field (file, "code", id.code_addr_p, id.code_addr);
218   fprintf_unfiltered (file, ",");
219   fprint_field (file, "special", id.special_addr_p, id.special_addr);
220   if (id.inline_depth)
221     fprintf_unfiltered (file, ",inlined=%d", id.inline_depth);
222   fprintf_unfiltered (file, "}");
223 }
224
225 static void
226 fprint_frame_type (struct ui_file *file, enum frame_type type)
227 {
228   switch (type)
229     {
230     case NORMAL_FRAME:
231       fprintf_unfiltered (file, "NORMAL_FRAME");
232       return;
233     case DUMMY_FRAME:
234       fprintf_unfiltered (file, "DUMMY_FRAME");
235       return;
236     case INLINE_FRAME:
237       fprintf_unfiltered (file, "INLINE_FRAME");
238       return;
239     case SENTINEL_FRAME:
240       fprintf_unfiltered (file, "SENTINEL_FRAME");
241       return;
242     case SIGTRAMP_FRAME:
243       fprintf_unfiltered (file, "SIGTRAMP_FRAME");
244       return;
245     case ARCH_FRAME:
246       fprintf_unfiltered (file, "ARCH_FRAME");
247       return;
248     default:
249       fprintf_unfiltered (file, "<unknown type>");
250       return;
251     };
252 }
253
254 static void
255 fprint_frame (struct ui_file *file, struct frame_info *fi)
256 {
257   if (fi == NULL)
258     {
259       fprintf_unfiltered (file, "<NULL frame>");
260       return;
261     }
262   fprintf_unfiltered (file, "{");
263   fprintf_unfiltered (file, "level=%d", fi->level);
264   fprintf_unfiltered (file, ",");
265   fprintf_unfiltered (file, "type=");
266   if (fi->unwind != NULL)
267     fprint_frame_type (file, fi->unwind->type);
268   else
269     fprintf_unfiltered (file, "<unknown>");
270   fprintf_unfiltered (file, ",");
271   fprintf_unfiltered (file, "unwind=");
272   if (fi->unwind != NULL)
273     gdb_print_host_address (fi->unwind, file);
274   else
275     fprintf_unfiltered (file, "<unknown>");
276   fprintf_unfiltered (file, ",");
277   fprintf_unfiltered (file, "pc=");
278   if (fi->next != NULL && fi->next->prev_pc.p)
279     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_pc.value));
280   else
281     fprintf_unfiltered (file, "<unknown>");
282   fprintf_unfiltered (file, ",");
283   fprintf_unfiltered (file, "id=");
284   if (fi->this_id.p)
285     fprint_frame_id (file, fi->this_id.value);
286   else
287     fprintf_unfiltered (file, "<unknown>");
288   fprintf_unfiltered (file, ",");
289   fprintf_unfiltered (file, "func=");
290   if (fi->next != NULL && fi->next->prev_func.p)
291     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
292   else
293     fprintf_unfiltered (file, "<unknown>");
294   fprintf_unfiltered (file, "}");
295 }
296
297 /* Given FRAME, return the enclosing normal frame for inlined
298    function frames.  Otherwise return the original frame.  */
299
300 static struct frame_info *
301 skip_inlined_frames (struct frame_info *frame)
302 {
303   while (get_frame_type (frame) == INLINE_FRAME)
304     frame = get_prev_frame (frame);
305
306   return frame;
307 }
308
309 /* Return a frame uniq ID that can be used to, later, re-find the
310    frame.  */
311
312 struct frame_id
313 get_frame_id (struct frame_info *fi)
314 {
315   if (fi == NULL)
316     return null_frame_id;
317
318   if (!fi->this_id.p)
319     {
320       if (frame_debug)
321         fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
322                             fi->level);
323       /* Find the unwinder.  */
324       if (fi->unwind == NULL)
325         fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
326       /* Find THIS frame's ID.  */
327       fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
328       fi->this_id.p = 1;
329       if (frame_debug)
330         {
331           fprintf_unfiltered (gdb_stdlog, "-> ");
332           fprint_frame_id (gdb_stdlog, fi->this_id.value);
333           fprintf_unfiltered (gdb_stdlog, " }\n");
334         }
335     }
336
337   frame_stash_add (fi);
338
339   return fi->this_id.value;
340 }
341
342 struct frame_id
343 get_stack_frame_id (struct frame_info *next_frame)
344 {
345   return get_frame_id (skip_inlined_frames (next_frame));
346 }
347
348 struct frame_id
349 frame_unwind_caller_id (struct frame_info *next_frame)
350 {
351   struct frame_info *this_frame;
352
353   /* Use get_prev_frame_1, and not get_prev_frame.  The latter will truncate
354      the frame chain, leading to this function unintentionally
355      returning a null_frame_id (e.g., when a caller requests the frame
356      ID of "main()"s caller.  */
357
358   next_frame = skip_inlined_frames (next_frame);
359   this_frame = get_prev_frame_1 (next_frame);
360   if (this_frame)
361     return get_frame_id (skip_inlined_frames (this_frame));
362   else
363     return null_frame_id;
364 }
365
366 const struct frame_id null_frame_id; /* All zeros.  */
367
368 struct frame_id
369 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
370                         CORE_ADDR special_addr)
371 {
372   struct frame_id id = null_frame_id;
373   id.stack_addr = stack_addr;
374   id.stack_addr_p = 1;
375   id.code_addr = code_addr;
376   id.code_addr_p = 1;
377   id.special_addr = special_addr;
378   id.special_addr_p = 1;
379   return id;
380 }
381
382 struct frame_id
383 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
384 {
385   struct frame_id id = null_frame_id;
386   id.stack_addr = stack_addr;
387   id.stack_addr_p = 1;
388   id.code_addr = code_addr;
389   id.code_addr_p = 1;
390   return id;
391 }
392
393 struct frame_id
394 frame_id_build_wild (CORE_ADDR stack_addr)
395 {
396   struct frame_id id = null_frame_id;
397   id.stack_addr = stack_addr;
398   id.stack_addr_p = 1;
399   return id;
400 }
401
402 int
403 frame_id_p (struct frame_id l)
404 {
405   int p;
406   /* The frame is valid iff it has a valid stack address.  */
407   p = l.stack_addr_p;
408   if (frame_debug)
409     {
410       fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
411       fprint_frame_id (gdb_stdlog, l);
412       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
413     }
414   return p;
415 }
416
417 int
418 frame_id_inlined_p (struct frame_id l)
419 {
420   if (!frame_id_p (l))
421     return 0;
422
423   return (l.inline_depth != 0);
424 }
425
426 int
427 frame_id_eq (struct frame_id l, struct frame_id r)
428 {
429   int eq;
430   if (!l.stack_addr_p || !r.stack_addr_p)
431     /* Like a NaN, if either ID is invalid, the result is false.
432        Note that a frame ID is invalid iff it is the null frame ID.  */
433     eq = 0;
434   else if (l.stack_addr != r.stack_addr)
435     /* If .stack addresses are different, the frames are different.  */
436     eq = 0;
437   else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
438     /* An invalid code addr is a wild card.  If .code addresses are
439        different, the frames are different.  */
440     eq = 0;
441   else if (l.special_addr_p && r.special_addr_p
442            && l.special_addr != r.special_addr)
443     /* An invalid special addr is a wild card (or unused).  Otherwise
444        if special addresses are different, the frames are different.  */
445     eq = 0;
446   else if (l.inline_depth != r.inline_depth)
447     /* If inline depths are different, the frames must be different.  */
448     eq = 0;
449   else
450     /* Frames are equal.  */
451     eq = 1;
452
453   if (frame_debug)
454     {
455       fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
456       fprint_frame_id (gdb_stdlog, l);
457       fprintf_unfiltered (gdb_stdlog, ",r=");
458       fprint_frame_id (gdb_stdlog, r);
459       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
460     }
461   return eq;
462 }
463
464 /* Safety net to check whether frame ID L should be inner to
465    frame ID R, according to their stack addresses.
466
467    This method cannot be used to compare arbitrary frames, as the
468    ranges of valid stack addresses may be discontiguous (e.g. due
469    to sigaltstack).
470
471    However, it can be used as safety net to discover invalid frame
472    IDs in certain circumstances. Assuming that NEXT is the immediate
473    inner frame to THIS and that NEXT and THIS are both NORMAL frames:
474
475    * The stack address of NEXT must be inner-than-or-equal to the stack
476      address of THIS.
477
478      Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
479      error has occurred.
480
481    * If NEXT and THIS have different stack addresses, no other frame
482      in the frame chain may have a stack address in between.
483
484      Therefore, if frame_id_inner (TEST, THIS) holds, but
485      frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
486      to a valid frame in the frame chain.
487
488    The sanity checks above cannot be performed when a SIGTRAMP frame
489    is involved, because signal handlers might be executed on a different
490    stack than the stack used by the routine that caused the signal
491    to be raised.  This can happen for instance when a thread exceeds
492    its maximum stack size. In this case, certain compilers implement
493    a stack overflow strategy that cause the handler to be run on a
494    different stack.  */
495
496 static int
497 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
498 {
499   int inner;
500   if (!l.stack_addr_p || !r.stack_addr_p)
501     /* Like NaN, any operation involving an invalid ID always fails.  */
502     inner = 0;
503   else if (l.inline_depth > r.inline_depth
504            && l.stack_addr == r.stack_addr
505            && l.code_addr_p == r.code_addr_p
506            && l.special_addr_p == r.special_addr_p
507            && l.special_addr == r.special_addr)
508     {
509       /* Same function, different inlined functions.  */
510       struct block *lb, *rb;
511
512       gdb_assert (l.code_addr_p && r.code_addr_p);
513
514       lb = block_for_pc (l.code_addr);
515       rb = block_for_pc (r.code_addr);
516
517       if (lb == NULL || rb == NULL)
518         /* Something's gone wrong.  */
519         inner = 0;
520       else
521         /* This will return true if LB and RB are the same block, or
522            if the block with the smaller depth lexically encloses the
523            block with the greater depth.  */
524         inner = contained_in (lb, rb);
525     }
526   else
527     /* Only return non-zero when strictly inner than.  Note that, per
528        comment in "frame.h", there is some fuzz here.  Frameless
529        functions are not strictly inner than (same .stack but
530        different .code and/or .special address).  */
531     inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
532   if (frame_debug)
533     {
534       fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
535       fprint_frame_id (gdb_stdlog, l);
536       fprintf_unfiltered (gdb_stdlog, ",r=");
537       fprint_frame_id (gdb_stdlog, r);
538       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
539     }
540   return inner;
541 }
542
543 struct frame_info *
544 frame_find_by_id (struct frame_id id)
545 {
546   struct frame_info *frame, *prev_frame;
547
548   /* ZERO denotes the null frame, let the caller decide what to do
549      about it.  Should it instead return get_current_frame()?  */
550   if (!frame_id_p (id))
551     return NULL;
552
553   /* Try using the frame stash first.  Finding it there removes the need
554      to perform the search by looping over all frames, which can be very
555      CPU-intensive if the number of frames is very high (the loop is O(n)
556      and get_prev_frame performs a series of checks that are relatively
557      expensive).  This optimization is particularly useful when this function
558      is called from another function (such as value_fetch_lazy, case
559      VALUE_LVAL (val) == lval_register) which already loops over all frames,
560      making the overall behavior O(n^2).  */
561   frame = frame_stash_find (id);
562   if (frame)
563     return frame;
564
565   for (frame = get_current_frame (); ; frame = prev_frame)
566     {
567       struct frame_id this = get_frame_id (frame);
568       if (frame_id_eq (id, this))
569         /* An exact match.  */
570         return frame;
571
572       prev_frame = get_prev_frame (frame);
573       if (!prev_frame)
574         return NULL;
575
576       /* As a safety net to avoid unnecessary backtracing while trying
577          to find an invalid ID, we check for a common situation where
578          we can detect from comparing stack addresses that no other
579          frame in the current frame chain can have this ID.  See the
580          comment at frame_id_inner for details.   */
581       if (get_frame_type (frame) == NORMAL_FRAME
582           && !frame_id_inner (get_frame_arch (frame), id, this)
583           && frame_id_inner (get_frame_arch (prev_frame), id,
584                              get_frame_id (prev_frame)))
585         return NULL;
586     }
587   return NULL;
588 }
589
590 static CORE_ADDR
591 frame_unwind_pc (struct frame_info *this_frame)
592 {
593   if (!this_frame->prev_pc.p)
594     {
595       CORE_ADDR pc;
596       if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
597         {
598           /* The right way.  The `pure' way.  The one true way.  This
599              method depends solely on the register-unwind code to
600              determine the value of registers in THIS frame, and hence
601              the value of this frame's PC (resume address).  A typical
602              implementation is no more than:
603            
604              frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
605              return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
606
607              Note: this method is very heavily dependent on a correct
608              register-unwind implementation, it pays to fix that
609              method first; this method is frame type agnostic, since
610              it only deals with register values, it works with any
611              frame.  This is all in stark contrast to the old
612              FRAME_SAVED_PC which would try to directly handle all the
613              different ways that a PC could be unwound.  */
614           pc = gdbarch_unwind_pc (frame_unwind_arch (this_frame), this_frame);
615         }
616       else
617         internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
618       this_frame->prev_pc.value = pc;
619       this_frame->prev_pc.p = 1;
620       if (frame_debug)
621         fprintf_unfiltered (gdb_stdlog,
622                             "{ frame_unwind_caller_pc (this_frame=%d) -> 0x%s }\n",
623                             this_frame->level,
624                             hex_string (this_frame->prev_pc.value));
625     }
626   return this_frame->prev_pc.value;
627 }
628
629 CORE_ADDR
630 frame_unwind_caller_pc (struct frame_info *this_frame)
631 {
632   return frame_unwind_pc (skip_inlined_frames (this_frame));
633 }
634
635 CORE_ADDR
636 get_frame_func (struct frame_info *this_frame)
637 {
638   struct frame_info *next_frame = this_frame->next;
639
640   if (!next_frame->prev_func.p)
641     {
642       /* Make certain that this, and not the adjacent, function is
643          found.  */
644       CORE_ADDR addr_in_block = get_frame_address_in_block (this_frame);
645       next_frame->prev_func.p = 1;
646       next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
647       if (frame_debug)
648         fprintf_unfiltered (gdb_stdlog,
649                             "{ get_frame_func (this_frame=%d) -> %s }\n",
650                             this_frame->level,
651                             hex_string (next_frame->prev_func.addr));
652     }
653   return next_frame->prev_func.addr;
654 }
655
656 static int
657 do_frame_register_read (void *src, int regnum, gdb_byte *buf)
658 {
659   return frame_register_read (src, regnum, buf);
660 }
661
662 struct regcache *
663 frame_save_as_regcache (struct frame_info *this_frame)
664 {
665   struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame));
666   struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
667   regcache_save (regcache, do_frame_register_read, this_frame);
668   discard_cleanups (cleanups);
669   return regcache;
670 }
671
672 void
673 frame_pop (struct frame_info *this_frame)
674 {
675   struct frame_info *prev_frame;
676   struct regcache *scratch;
677   struct cleanup *cleanups;
678
679   if (get_frame_type (this_frame) == DUMMY_FRAME)
680     {
681       /* Popping a dummy frame involves restoring more than just registers.
682          dummy_frame_pop does all the work.  */
683       dummy_frame_pop (get_frame_id (this_frame));
684       return;
685     }
686
687   /* Ensure that we have a frame to pop to.  */
688   prev_frame = get_prev_frame_1 (this_frame);
689
690   if (!prev_frame)
691     error (_("Cannot pop the initial frame."));
692
693   /* Make a copy of all the register values unwound from this frame.
694      Save them in a scratch buffer so that there isn't a race between
695      trying to extract the old values from the current regcache while
696      at the same time writing new values into that same cache.  */
697   scratch = frame_save_as_regcache (prev_frame);
698   cleanups = make_cleanup_regcache_xfree (scratch);
699
700   /* FIXME: cagney/2003-03-16: It should be possible to tell the
701      target's register cache that it is about to be hit with a burst
702      register transfer and that the sequence of register writes should
703      be batched.  The pair target_prepare_to_store() and
704      target_store_registers() kind of suggest this functionality.
705      Unfortunately, they don't implement it.  Their lack of a formal
706      definition can lead to targets writing back bogus values
707      (arguably a bug in the target code mind).  */
708   /* Now copy those saved registers into the current regcache.
709      Here, regcache_cpy() calls regcache_restore().  */
710   regcache_cpy (get_current_regcache (), scratch);
711   do_cleanups (cleanups);
712
713   /* We've made right mess of GDB's local state, just discard
714      everything.  */
715   reinit_frame_cache ();
716 }
717
718 void
719 frame_register_unwind (struct frame_info *frame, int regnum,
720                        int *optimizedp, enum lval_type *lvalp,
721                        CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
722 {
723   struct value *value;
724
725   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
726      that the value proper does not need to be fetched.  */
727   gdb_assert (optimizedp != NULL);
728   gdb_assert (lvalp != NULL);
729   gdb_assert (addrp != NULL);
730   gdb_assert (realnump != NULL);
731   /* gdb_assert (bufferp != NULL); */
732
733   value = frame_unwind_register_value (frame, regnum);
734
735   gdb_assert (value != NULL);
736
737   *optimizedp = value_optimized_out (value);
738   *lvalp = VALUE_LVAL (value);
739   *addrp = value_address (value);
740   *realnump = VALUE_REGNUM (value);
741
742   if (bufferp)
743     memcpy (bufferp, value_contents_all (value),
744             TYPE_LENGTH (value_type (value)));
745
746   /* Dispose of the new value.  This prevents watchpoints from
747      trying to watch the saved frame pointer.  */
748   release_value (value);
749   value_free (value);
750 }
751
752 void
753 frame_register (struct frame_info *frame, int regnum,
754                 int *optimizedp, enum lval_type *lvalp,
755                 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
756 {
757   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
758      that the value proper does not need to be fetched.  */
759   gdb_assert (optimizedp != NULL);
760   gdb_assert (lvalp != NULL);
761   gdb_assert (addrp != NULL);
762   gdb_assert (realnump != NULL);
763   /* gdb_assert (bufferp != NULL); */
764
765   /* Obtain the register value by unwinding the register from the next
766      (more inner frame).  */
767   gdb_assert (frame != NULL && frame->next != NULL);
768   frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
769                          realnump, bufferp);
770 }
771
772 void
773 frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
774 {
775   int optimized;
776   CORE_ADDR addr;
777   int realnum;
778   enum lval_type lval;
779   frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
780                          &realnum, buf);
781 }
782
783 void
784 get_frame_register (struct frame_info *frame,
785                     int regnum, gdb_byte *buf)
786 {
787   frame_unwind_register (frame->next, regnum, buf);
788 }
789
790 struct value *
791 frame_unwind_register_value (struct frame_info *frame, int regnum)
792 {
793   struct gdbarch *gdbarch;
794   struct value *value;
795
796   gdb_assert (frame != NULL);
797   gdbarch = frame_unwind_arch (frame);
798
799   if (frame_debug)
800     {
801       fprintf_unfiltered (gdb_stdlog, "\
802 { frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
803                           frame->level, regnum,
804                           user_reg_map_regnum_to_name (gdbarch, regnum));
805     }
806
807   /* Find the unwinder.  */
808   if (frame->unwind == NULL)
809     frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
810
811   /* Ask this frame to unwind its register.  */
812   value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
813
814   if (frame_debug)
815     {
816       fprintf_unfiltered (gdb_stdlog, "->");
817       if (value_optimized_out (value))
818         fprintf_unfiltered (gdb_stdlog, " optimized out");
819       else
820         {
821           if (VALUE_LVAL (value) == lval_register)
822             fprintf_unfiltered (gdb_stdlog, " register=%d",
823                                 VALUE_REGNUM (value));
824           else if (VALUE_LVAL (value) == lval_memory)
825             fprintf_unfiltered (gdb_stdlog, " address=%s",
826                                 paddress (gdbarch,
827                                           value_address (value)));
828           else
829             fprintf_unfiltered (gdb_stdlog, " computed");
830
831           if (value_lazy (value))
832             fprintf_unfiltered (gdb_stdlog, " lazy");
833           else
834             {
835               int i;
836               const gdb_byte *buf = value_contents (value);
837
838               fprintf_unfiltered (gdb_stdlog, " bytes=");
839               fprintf_unfiltered (gdb_stdlog, "[");
840               for (i = 0; i < register_size (gdbarch, regnum); i++)
841                 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
842               fprintf_unfiltered (gdb_stdlog, "]");
843             }
844         }
845
846       fprintf_unfiltered (gdb_stdlog, " }\n");
847     }
848
849   return value;
850 }
851
852 struct value *
853 get_frame_register_value (struct frame_info *frame, int regnum)
854 {
855   return frame_unwind_register_value (frame->next, regnum);
856 }
857
858 LONGEST
859 frame_unwind_register_signed (struct frame_info *frame, int regnum)
860 {
861   struct gdbarch *gdbarch = frame_unwind_arch (frame);
862   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
863   int size = register_size (gdbarch, regnum);
864   gdb_byte buf[MAX_REGISTER_SIZE];
865   frame_unwind_register (frame, regnum, buf);
866   return extract_signed_integer (buf, size, byte_order);
867 }
868
869 LONGEST
870 get_frame_register_signed (struct frame_info *frame, int regnum)
871 {
872   return frame_unwind_register_signed (frame->next, regnum);
873 }
874
875 ULONGEST
876 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
877 {
878   struct gdbarch *gdbarch = frame_unwind_arch (frame);
879   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
880   int size = register_size (gdbarch, regnum);
881   gdb_byte buf[MAX_REGISTER_SIZE];
882   frame_unwind_register (frame, regnum, buf);
883   return extract_unsigned_integer (buf, size, byte_order);
884 }
885
886 ULONGEST
887 get_frame_register_unsigned (struct frame_info *frame, int regnum)
888 {
889   return frame_unwind_register_unsigned (frame->next, regnum);
890 }
891
892 void
893 put_frame_register (struct frame_info *frame, int regnum,
894                     const gdb_byte *buf)
895 {
896   struct gdbarch *gdbarch = get_frame_arch (frame);
897   int realnum;
898   int optim;
899   enum lval_type lval;
900   CORE_ADDR addr;
901   frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
902   if (optim)
903     error (_("Attempt to assign to a value that was optimized out."));
904   switch (lval)
905     {
906     case lval_memory:
907       {
908         /* FIXME: write_memory doesn't yet take constant buffers.
909            Arrrg!  */
910         gdb_byte tmp[MAX_REGISTER_SIZE];
911         memcpy (tmp, buf, register_size (gdbarch, regnum));
912         write_memory (addr, tmp, register_size (gdbarch, regnum));
913         break;
914       }
915     case lval_register:
916       regcache_cooked_write (get_current_regcache (), realnum, buf);
917       break;
918     default:
919       error (_("Attempt to assign to an unmodifiable value."));
920     }
921 }
922
923 /* frame_register_read ()
924
925    Find and return the value of REGNUM for the specified stack frame.
926    The number of bytes copied is REGISTER_SIZE (REGNUM).
927
928    Returns 0 if the register value could not be found.  */
929
930 int
931 frame_register_read (struct frame_info *frame, int regnum,
932                      gdb_byte *myaddr)
933 {
934   int optimized;
935   enum lval_type lval;
936   CORE_ADDR addr;
937   int realnum;
938   frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
939
940   return !optimized;
941 }
942
943 int
944 get_frame_register_bytes (struct frame_info *frame, int regnum,
945                           CORE_ADDR offset, int len, gdb_byte *myaddr)
946 {
947   struct gdbarch *gdbarch = get_frame_arch (frame);
948   int i;
949   int maxsize;
950   int numregs;
951
952   /* Skip registers wholly inside of OFFSET.  */
953   while (offset >= register_size (gdbarch, regnum))
954     {
955       offset -= register_size (gdbarch, regnum);
956       regnum++;
957     }
958
959   /* Ensure that we will not read beyond the end of the register file.
960      This can only ever happen if the debug information is bad.  */
961   maxsize = -offset;
962   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
963   for (i = regnum; i < numregs; i++)
964     {
965       int thissize = register_size (gdbarch, i);
966       if (thissize == 0)
967         break;  /* This register is not available on this architecture.  */
968       maxsize += thissize;
969     }
970   if (len > maxsize)
971     {
972       warning (_("Bad debug information detected: "
973                  "Attempt to read %d bytes from registers."), len);
974       return 0;
975     }
976
977   /* Copy the data.  */
978   while (len > 0)
979     {
980       int curr_len = register_size (gdbarch, regnum) - offset;
981       if (curr_len > len)
982         curr_len = len;
983
984       if (curr_len == register_size (gdbarch, regnum))
985         {
986           if (!frame_register_read (frame, regnum, myaddr))
987             return 0;
988         }
989       else
990         {
991           gdb_byte buf[MAX_REGISTER_SIZE];
992           if (!frame_register_read (frame, regnum, buf))
993             return 0;
994           memcpy (myaddr, buf + offset, curr_len);
995         }
996
997       myaddr += curr_len;
998       len -= curr_len;
999       offset = 0;
1000       regnum++;
1001     }
1002
1003   return 1;
1004 }
1005
1006 void
1007 put_frame_register_bytes (struct frame_info *frame, int regnum,
1008                           CORE_ADDR offset, int len, const gdb_byte *myaddr)
1009 {
1010   struct gdbarch *gdbarch = get_frame_arch (frame);
1011
1012   /* Skip registers wholly inside of OFFSET.  */
1013   while (offset >= register_size (gdbarch, regnum))
1014     {
1015       offset -= register_size (gdbarch, regnum);
1016       regnum++;
1017     }
1018
1019   /* Copy the data.  */
1020   while (len > 0)
1021     {
1022       int curr_len = register_size (gdbarch, regnum) - offset;
1023       if (curr_len > len)
1024         curr_len = len;
1025
1026       if (curr_len == register_size (gdbarch, regnum))
1027         {
1028           put_frame_register (frame, regnum, myaddr);
1029         }
1030       else
1031         {
1032           gdb_byte buf[MAX_REGISTER_SIZE];
1033           frame_register_read (frame, regnum, buf);
1034           memcpy (buf + offset, myaddr, curr_len);
1035           put_frame_register (frame, regnum, buf);
1036         }
1037
1038       myaddr += curr_len;
1039       len -= curr_len;
1040       offset = 0;
1041       regnum++;
1042     }
1043 }
1044
1045 /* Create a sentinel frame.  */
1046
1047 static struct frame_info *
1048 create_sentinel_frame (struct regcache *regcache)
1049 {
1050   struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1051   frame->level = -1;
1052   /* Explicitly initialize the sentinel frame's cache.  Provide it
1053      with the underlying regcache.  In the future additional
1054      information, such as the frame's thread will be added.  */
1055   frame->prologue_cache = sentinel_frame_cache (regcache);
1056   /* For the moment there is only one sentinel frame implementation.  */
1057   frame->unwind = sentinel_frame_unwind;
1058   /* Link this frame back to itself.  The frame is self referential
1059      (the unwound PC is the same as the pc), so make it so.  */
1060   frame->next = frame;
1061   /* Make the sentinel frame's ID valid, but invalid.  That way all
1062      comparisons with it should fail.  */
1063   frame->this_id.p = 1;
1064   frame->this_id.value = null_frame_id;
1065   if (frame_debug)
1066     {
1067       fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1068       fprint_frame (gdb_stdlog, frame);
1069       fprintf_unfiltered (gdb_stdlog, " }\n");
1070     }
1071   return frame;
1072 }
1073
1074 /* Info about the innermost stack frame (contents of FP register) */
1075
1076 static struct frame_info *current_frame;
1077
1078 /* Cache for frame addresses already read by gdb.  Valid only while
1079    inferior is stopped.  Control variables for the frame cache should
1080    be local to this module.  */
1081
1082 static struct obstack frame_cache_obstack;
1083
1084 void *
1085 frame_obstack_zalloc (unsigned long size)
1086 {
1087   void *data = obstack_alloc (&frame_cache_obstack, size);
1088   memset (data, 0, size);
1089   return data;
1090 }
1091
1092 /* Return the innermost (currently executing) stack frame.  This is
1093    split into two functions.  The function unwind_to_current_frame()
1094    is wrapped in catch exceptions so that, even when the unwind of the
1095    sentinel frame fails, the function still returns a stack frame.  */
1096
1097 static int
1098 unwind_to_current_frame (struct ui_out *ui_out, void *args)
1099 {
1100   struct frame_info *frame = get_prev_frame (args);
1101   /* A sentinel frame can fail to unwind, e.g., because its PC value
1102      lands in somewhere like start.  */
1103   if (frame == NULL)
1104     return 1;
1105   current_frame = frame;
1106   return 0;
1107 }
1108
1109 struct frame_info *
1110 get_current_frame (void)
1111 {
1112   /* First check, and report, the lack of registers.  Having GDB
1113      report "No stack!" or "No memory" when the target doesn't even
1114      have registers is very confusing.  Besides, "printcmd.exp"
1115      explicitly checks that ``print $pc'' with no registers prints "No
1116      registers".  */
1117   if (!target_has_registers)
1118     error (_("No registers."));
1119   if (!target_has_stack)
1120     error (_("No stack."));
1121   if (!target_has_memory)
1122     error (_("No memory."));
1123   if (ptid_equal (inferior_ptid, null_ptid))
1124     error (_("No selected thread."));
1125   if (is_exited (inferior_ptid))
1126     error (_("Invalid selected thread."));
1127   if (is_executing (inferior_ptid))
1128     error (_("Target is executing."));
1129
1130   if (current_frame == NULL)
1131     {
1132       struct frame_info *sentinel_frame =
1133         create_sentinel_frame (get_current_regcache ());
1134       if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
1135                             RETURN_MASK_ERROR) != 0)
1136         {
1137           /* Oops! Fake a current frame?  Is this useful?  It has a PC
1138              of zero, for instance.  */
1139           current_frame = sentinel_frame;
1140         }
1141     }
1142   return current_frame;
1143 }
1144
1145 /* The "selected" stack frame is used by default for local and arg
1146    access.  May be zero, for no selected frame.  */
1147
1148 static struct frame_info *selected_frame;
1149
1150 int
1151 has_stack_frames (void)
1152 {
1153   if (!target_has_registers || !target_has_stack || !target_has_memory)
1154     return 0;
1155
1156   /* No current inferior, no frame.  */
1157   if (ptid_equal (inferior_ptid, null_ptid))
1158     return 0;
1159
1160   /* Don't try to read from a dead thread.  */
1161   if (is_exited (inferior_ptid))
1162     return 0;
1163
1164   /* ... or from a spinning thread.  */
1165   if (is_executing (inferior_ptid))
1166     return 0;
1167
1168   return 1;
1169 }
1170
1171 /* Return the selected frame.  Always non-NULL (unless there isn't an
1172    inferior sufficient for creating a frame) in which case an error is
1173    thrown.  */
1174
1175 struct frame_info *
1176 get_selected_frame (const char *message)
1177 {
1178   if (selected_frame == NULL)
1179     {
1180       if (message != NULL && !has_stack_frames ())
1181         error (("%s"), message);
1182       /* Hey!  Don't trust this.  It should really be re-finding the
1183          last selected frame of the currently selected thread.  This,
1184          though, is better than nothing.  */
1185       select_frame (get_current_frame ());
1186     }
1187   /* There is always a frame.  */
1188   gdb_assert (selected_frame != NULL);
1189   return selected_frame;
1190 }
1191
1192 /* This is a variant of get_selected_frame() which can be called when
1193    the inferior does not have a frame; in that case it will return
1194    NULL instead of calling error().  */
1195
1196 struct frame_info *
1197 deprecated_safe_get_selected_frame (void)
1198 {
1199   if (!has_stack_frames ())
1200     return NULL;
1201   return get_selected_frame (NULL);
1202 }
1203
1204 /* Select frame FI (or NULL - to invalidate the current frame).  */
1205
1206 void
1207 select_frame (struct frame_info *fi)
1208 {
1209   struct symtab *s;
1210
1211   selected_frame = fi;
1212   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
1213      frame is being invalidated.  */
1214   if (deprecated_selected_frame_level_changed_hook)
1215     deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
1216
1217   /* FIXME: kseitz/2002-08-28: It would be nice to call
1218      selected_frame_level_changed_event() right here, but due to limitations
1219      in the current interfaces, we would end up flooding UIs with events
1220      because select_frame() is used extensively internally.
1221
1222      Once we have frame-parameterized frame (and frame-related) commands,
1223      the event notification can be moved here, since this function will only
1224      be called when the user's selected frame is being changed. */
1225
1226   /* Ensure that symbols for this frame are read in.  Also, determine the
1227      source language of this frame, and switch to it if desired.  */
1228   if (fi)
1229     {
1230       /* We retrieve the frame's symtab by using the frame PC.  However
1231          we cannot use the frame PC as-is, because it usually points to
1232          the instruction following the "call", which is sometimes the
1233          first instruction of another function.  So we rely on
1234          get_frame_address_in_block() which provides us with a PC which
1235          is guaranteed to be inside the frame's code block.  */
1236       s = find_pc_symtab (get_frame_address_in_block (fi));
1237       if (s
1238           && s->language != current_language->la_language
1239           && s->language != language_unknown
1240           && language_mode == language_mode_auto)
1241         {
1242           set_language (s->language);
1243         }
1244     }
1245 }
1246         
1247 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1248    Always returns a non-NULL value.  */
1249
1250 struct frame_info *
1251 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1252 {
1253   struct frame_info *fi;
1254
1255   if (frame_debug)
1256     {
1257       fprintf_unfiltered (gdb_stdlog,
1258                           "{ create_new_frame (addr=%s, pc=%s) ",
1259                           hex_string (addr), hex_string (pc));
1260     }
1261
1262   fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1263
1264   fi->next = create_sentinel_frame (get_current_regcache ());
1265
1266   /* Set/update this frame's cached PC value, found in the next frame.
1267      Do this before looking for this frame's unwinder.  A sniffer is
1268      very likely to read this, and the corresponding unwinder is
1269      entitled to rely that the PC doesn't magically change.  */
1270   fi->next->prev_pc.value = pc;
1271   fi->next->prev_pc.p = 1;
1272
1273   /* Select/initialize both the unwind function and the frame's type
1274      based on the PC.  */
1275   fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1276
1277   fi->this_id.p = 1;
1278   fi->this_id.value = frame_id_build (addr, pc);
1279
1280   if (frame_debug)
1281     {
1282       fprintf_unfiltered (gdb_stdlog, "-> ");
1283       fprint_frame (gdb_stdlog, fi);
1284       fprintf_unfiltered (gdb_stdlog, " }\n");
1285     }
1286
1287   return fi;
1288 }
1289
1290 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1291    innermost frame).  Be careful to not fall off the bottom of the
1292    frame chain and onto the sentinel frame.  */
1293
1294 struct frame_info *
1295 get_next_frame (struct frame_info *this_frame)
1296 {
1297   if (this_frame->level > 0)
1298     return this_frame->next;
1299   else
1300     return NULL;
1301 }
1302
1303 /* Observer for the target_changed event.  */
1304
1305 static void
1306 frame_observer_target_changed (struct target_ops *target)
1307 {
1308   reinit_frame_cache ();
1309 }
1310
1311 /* Flush the entire frame cache.  */
1312
1313 void
1314 reinit_frame_cache (void)
1315 {
1316   struct frame_info *fi;
1317
1318   /* Tear down all frame caches.  */
1319   for (fi = current_frame; fi != NULL; fi = fi->prev)
1320     {
1321       if (fi->prologue_cache && fi->unwind->dealloc_cache)
1322         fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1323       if (fi->base_cache && fi->base->unwind->dealloc_cache)
1324         fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1325     }
1326
1327   /* Since we can't really be sure what the first object allocated was */
1328   obstack_free (&frame_cache_obstack, 0);
1329   obstack_init (&frame_cache_obstack);
1330
1331   if (current_frame != NULL)
1332     annotate_frames_invalid ();
1333
1334   current_frame = NULL;         /* Invalidate cache */
1335   select_frame (NULL);
1336   frame_stash_invalidate ();
1337   if (frame_debug)
1338     fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1339 }
1340
1341 /* Find where a register is saved (in memory or another register).
1342    The result of frame_register_unwind is just where it is saved
1343    relative to this particular frame.  */
1344
1345 static void
1346 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1347                                 int *optimizedp, enum lval_type *lvalp,
1348                                 CORE_ADDR *addrp, int *realnump)
1349 {
1350   gdb_assert (this_frame == NULL || this_frame->level >= 0);
1351
1352   while (this_frame != NULL)
1353     {
1354       frame_register_unwind (this_frame, regnum, optimizedp, lvalp,
1355                              addrp, realnump, NULL);
1356
1357       if (*optimizedp)
1358         break;
1359
1360       if (*lvalp != lval_register)
1361         break;
1362
1363       regnum = *realnump;
1364       this_frame = get_next_frame (this_frame);
1365     }
1366 }
1367
1368 /* Return a "struct frame_info" corresponding to the frame that called
1369    THIS_FRAME.  Returns NULL if there is no such frame.
1370
1371    Unlike get_prev_frame, this function always tries to unwind the
1372    frame.  */
1373
1374 static struct frame_info *
1375 get_prev_frame_1 (struct frame_info *this_frame)
1376 {
1377   struct frame_id this_id;
1378   struct gdbarch *gdbarch;
1379
1380   gdb_assert (this_frame != NULL);
1381   gdbarch = get_frame_arch (this_frame);
1382
1383   if (frame_debug)
1384     {
1385       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1386       if (this_frame != NULL)
1387         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1388       else
1389         fprintf_unfiltered (gdb_stdlog, "<NULL>");
1390       fprintf_unfiltered (gdb_stdlog, ") ");
1391     }
1392
1393   /* Only try to do the unwind once.  */
1394   if (this_frame->prev_p)
1395     {
1396       if (frame_debug)
1397         {
1398           fprintf_unfiltered (gdb_stdlog, "-> ");
1399           fprint_frame (gdb_stdlog, this_frame->prev);
1400           fprintf_unfiltered (gdb_stdlog, " // cached \n");
1401         }
1402       return this_frame->prev;
1403     }
1404
1405   /* If the frame unwinder hasn't been selected yet, we must do so
1406      before setting prev_p; otherwise the check for misbehaved
1407      sniffers will think that this frame's sniffer tried to unwind
1408      further (see frame_cleanup_after_sniffer).  */
1409   if (this_frame->unwind == NULL)
1410     this_frame->unwind
1411       = frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1412
1413   this_frame->prev_p = 1;
1414   this_frame->stop_reason = UNWIND_NO_REASON;
1415
1416   /* If we are unwinding from an inline frame, all of the below tests
1417      were already performed when we unwound from the next non-inline
1418      frame.  We must skip them, since we can not get THIS_FRAME's ID
1419      until we have unwound all the way down to the previous non-inline
1420      frame.  */
1421   if (get_frame_type (this_frame) == INLINE_FRAME)
1422     return get_prev_frame_raw (this_frame);
1423
1424   /* Check that this frame's ID was valid.  If it wasn't, don't try to
1425      unwind to the prev frame.  Be careful to not apply this test to
1426      the sentinel frame.  */
1427   this_id = get_frame_id (this_frame);
1428   if (this_frame->level >= 0 && !frame_id_p (this_id))
1429     {
1430       if (frame_debug)
1431         {
1432           fprintf_unfiltered (gdb_stdlog, "-> ");
1433           fprint_frame (gdb_stdlog, NULL);
1434           fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1435         }
1436       this_frame->stop_reason = UNWIND_NULL_ID;
1437       return NULL;
1438     }
1439
1440   /* Check that this frame's ID isn't inner to (younger, below, next)
1441      the next frame.  This happens when a frame unwind goes backwards.
1442      This check is valid only if this frame and the next frame are NORMAL.
1443      See the comment at frame_id_inner for details.  */
1444   if (get_frame_type (this_frame) == NORMAL_FRAME
1445       && this_frame->next->unwind->type == NORMAL_FRAME
1446       && frame_id_inner (get_frame_arch (this_frame->next), this_id,
1447                          get_frame_id (this_frame->next)))
1448     {
1449       if (frame_debug)
1450         {
1451           fprintf_unfiltered (gdb_stdlog, "-> ");
1452           fprint_frame (gdb_stdlog, NULL);
1453           fprintf_unfiltered (gdb_stdlog, " // this frame ID is inner }\n");
1454         }
1455       this_frame->stop_reason = UNWIND_INNER_ID;
1456       return NULL;
1457     }
1458
1459   /* Check that this and the next frame are not identical.  If they
1460      are, there is most likely a stack cycle.  As with the inner-than
1461      test above, avoid comparing the inner-most and sentinel frames.  */
1462   if (this_frame->level > 0
1463       && frame_id_eq (this_id, get_frame_id (this_frame->next)))
1464     {
1465       if (frame_debug)
1466         {
1467           fprintf_unfiltered (gdb_stdlog, "-> ");
1468           fprint_frame (gdb_stdlog, NULL);
1469           fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1470         }
1471       this_frame->stop_reason = UNWIND_SAME_ID;
1472       return NULL;
1473     }
1474
1475   /* Check that this and the next frame do not unwind the PC register
1476      to the same memory location.  If they do, then even though they
1477      have different frame IDs, the new frame will be bogus; two
1478      functions can't share a register save slot for the PC.  This can
1479      happen when the prologue analyzer finds a stack adjustment, but
1480      no PC save.
1481
1482      This check does assume that the "PC register" is roughly a
1483      traditional PC, even if the gdbarch_unwind_pc method adjusts
1484      it (we do not rely on the value, only on the unwound PC being
1485      dependent on this value).  A potential improvement would be
1486      to have the frame prev_pc method and the gdbarch unwind_pc
1487      method set the same lval and location information as
1488      frame_register_unwind.  */
1489   if (this_frame->level > 0
1490       && gdbarch_pc_regnum (gdbarch) >= 0
1491       && get_frame_type (this_frame) == NORMAL_FRAME
1492       && (get_frame_type (this_frame->next) == NORMAL_FRAME
1493           || get_frame_type (this_frame->next) == INLINE_FRAME))
1494     {
1495       int optimized, realnum, nrealnum;
1496       enum lval_type lval, nlval;
1497       CORE_ADDR addr, naddr;
1498
1499       frame_register_unwind_location (this_frame,
1500                                       gdbarch_pc_regnum (gdbarch),
1501                                       &optimized, &lval, &addr, &realnum);
1502       frame_register_unwind_location (get_next_frame (this_frame),
1503                                       gdbarch_pc_regnum (gdbarch),
1504                                       &optimized, &nlval, &naddr, &nrealnum);
1505
1506       if ((lval == lval_memory && lval == nlval && addr == naddr)
1507           || (lval == lval_register && lval == nlval && realnum == nrealnum))
1508         {
1509           if (frame_debug)
1510             {
1511               fprintf_unfiltered (gdb_stdlog, "-> ");
1512               fprint_frame (gdb_stdlog, NULL);
1513               fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1514             }
1515
1516           this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1517           this_frame->prev = NULL;
1518           return NULL;
1519         }
1520     }
1521
1522   return get_prev_frame_raw (this_frame);
1523 }
1524
1525 /* Construct a new "struct frame_info" and link it previous to
1526    this_frame.  */
1527
1528 static struct frame_info *
1529 get_prev_frame_raw (struct frame_info *this_frame)
1530 {
1531   struct frame_info *prev_frame;
1532
1533   /* Allocate the new frame but do not wire it in to the frame chain.
1534      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1535      frame->next to pull some fancy tricks (of course such code is, by
1536      definition, recursive).  Try to prevent it.
1537
1538      There is no reason to worry about memory leaks, should the
1539      remainder of the function fail.  The allocated memory will be
1540      quickly reclaimed when the frame cache is flushed, and the `we've
1541      been here before' check above will stop repeated memory
1542      allocation calls.  */
1543   prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1544   prev_frame->level = this_frame->level + 1;
1545
1546   /* Don't yet compute ->unwind (and hence ->type).  It is computed
1547      on-demand in get_frame_type, frame_register_unwind, and
1548      get_frame_id.  */
1549
1550   /* Don't yet compute the frame's ID.  It is computed on-demand by
1551      get_frame_id().  */
1552
1553   /* The unwound frame ID is validate at the start of this function,
1554      as part of the logic to decide if that frame should be further
1555      unwound, and not here while the prev frame is being created.
1556      Doing this makes it possible for the user to examine a frame that
1557      has an invalid frame ID.
1558
1559      Some very old VAX code noted: [...]  For the sake of argument,
1560      suppose that the stack is somewhat trashed (which is one reason
1561      that "info frame" exists).  So, return 0 (indicating we don't
1562      know the address of the arglist) if we don't know what frame this
1563      frame calls.  */
1564
1565   /* Link it in.  */
1566   this_frame->prev = prev_frame;
1567   prev_frame->next = this_frame;
1568
1569   if (frame_debug)
1570     {
1571       fprintf_unfiltered (gdb_stdlog, "-> ");
1572       fprint_frame (gdb_stdlog, prev_frame);
1573       fprintf_unfiltered (gdb_stdlog, " }\n");
1574     }
1575
1576   return prev_frame;
1577 }
1578
1579 /* Debug routine to print a NULL frame being returned.  */
1580
1581 static void
1582 frame_debug_got_null_frame (struct frame_info *this_frame,
1583                             const char *reason)
1584 {
1585   if (frame_debug)
1586     {
1587       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1588       if (this_frame != NULL)
1589         fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1590       else
1591         fprintf_unfiltered (gdb_stdlog, "<NULL>");
1592       fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1593     }
1594 }
1595
1596 /* Is this (non-sentinel) frame in the "main"() function?  */
1597
1598 static int
1599 inside_main_func (struct frame_info *this_frame)
1600 {
1601   struct minimal_symbol *msymbol;
1602   CORE_ADDR maddr;
1603
1604   if (symfile_objfile == 0)
1605     return 0;
1606   msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1607   if (msymbol == NULL)
1608     return 0;
1609   /* Make certain that the code, and not descriptor, address is
1610      returned.  */
1611   maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
1612                                               SYMBOL_VALUE_ADDRESS (msymbol),
1613                                               &current_target);
1614   return maddr == get_frame_func (this_frame);
1615 }
1616
1617 /* Test whether THIS_FRAME is inside the process entry point function.  */
1618
1619 static int
1620 inside_entry_func (struct frame_info *this_frame)
1621 {
1622   return (get_frame_func (this_frame) == entry_point_address ());
1623 }
1624
1625 /* Return a structure containing various interesting information about
1626    the frame that called THIS_FRAME.  Returns NULL if there is entier
1627    no such frame or the frame fails any of a set of target-independent
1628    condition that should terminate the frame chain (e.g., as unwinding
1629    past main()).
1630
1631    This function should not contain target-dependent tests, such as
1632    checking whether the program-counter is zero.  */
1633
1634 struct frame_info *
1635 get_prev_frame (struct frame_info *this_frame)
1636 {
1637   struct frame_info *prev_frame;
1638
1639   /* There is always a frame.  If this assertion fails, suspect that
1640      something should be calling get_selected_frame() or
1641      get_current_frame().  */
1642   gdb_assert (this_frame != NULL);
1643
1644   /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1645      sense to stop unwinding at a dummy frame.  One place where a dummy
1646      frame may have an address "inside_main_func" is on HPUX.  On HPUX, the
1647      pcsqh register (space register for the instruction at the head of the
1648      instruction queue) cannot be written directly; the only way to set it
1649      is to branch to code that is in the target space.  In order to implement
1650      frame dummies on HPUX, the called function is made to jump back to where 
1651      the inferior was when the user function was called.  If gdb was inside 
1652      the main function when we created the dummy frame, the dummy frame will 
1653      point inside the main function.  */
1654   if (this_frame->level >= 0
1655       && get_frame_type (this_frame) == NORMAL_FRAME
1656       && !backtrace_past_main
1657       && inside_main_func (this_frame))
1658     /* Don't unwind past main().  Note, this is done _before_ the
1659        frame has been marked as previously unwound.  That way if the
1660        user later decides to enable unwinds past main(), that will
1661        automatically happen.  */
1662     {
1663       frame_debug_got_null_frame (this_frame, "inside main func");
1664       return NULL;
1665     }
1666
1667   /* If the user's backtrace limit has been exceeded, stop.  We must
1668      add two to the current level; one of those accounts for backtrace_limit
1669      being 1-based and the level being 0-based, and the other accounts for
1670      the level of the new frame instead of the level of the current
1671      frame.  */
1672   if (this_frame->level + 2 > backtrace_limit)
1673     {
1674       frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
1675       return NULL;
1676     }
1677
1678   /* If we're already inside the entry function for the main objfile,
1679      then it isn't valid.  Don't apply this test to a dummy frame -
1680      dummy frame PCs typically land in the entry func.  Don't apply
1681      this test to the sentinel frame.  Sentinel frames should always
1682      be allowed to unwind.  */
1683   /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1684      wasn't checking for "main" in the minimal symbols.  With that
1685      fixed asm-source tests now stop in "main" instead of halting the
1686      backtrace in weird and wonderful ways somewhere inside the entry
1687      file.  Suspect that tests for inside the entry file/func were
1688      added to work around that (now fixed) case.  */
1689   /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1690      suggested having the inside_entry_func test use the
1691      inside_main_func() msymbol trick (along with entry_point_address()
1692      I guess) to determine the address range of the start function.
1693      That should provide a far better stopper than the current
1694      heuristics.  */
1695   /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1696      applied tail-call optimizations to main so that a function called 
1697      from main returns directly to the caller of main.  Since we don't
1698      stop at main, we should at least stop at the entry point of the
1699      application.  */
1700   if (this_frame->level >= 0
1701       && get_frame_type (this_frame) == NORMAL_FRAME
1702       && !backtrace_past_entry
1703       && inside_entry_func (this_frame))
1704     {
1705       frame_debug_got_null_frame (this_frame, "inside entry func");
1706       return NULL;
1707     }
1708
1709   /* Assume that the only way to get a zero PC is through something
1710      like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1711      will never unwind a zero PC.  */
1712   if (this_frame->level > 0
1713       && (get_frame_type (this_frame) == NORMAL_FRAME
1714           || get_frame_type (this_frame) == INLINE_FRAME)
1715       && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1716       && get_frame_pc (this_frame) == 0)
1717     {
1718       frame_debug_got_null_frame (this_frame, "zero PC");
1719       return NULL;
1720     }
1721
1722   return get_prev_frame_1 (this_frame);
1723 }
1724
1725 CORE_ADDR
1726 get_frame_pc (struct frame_info *frame)
1727 {
1728   gdb_assert (frame->next != NULL);
1729   return frame_unwind_pc (frame->next);
1730 }
1731
1732 /* Return an address that falls within THIS_FRAME's code block.  */
1733
1734 CORE_ADDR
1735 get_frame_address_in_block (struct frame_info *this_frame)
1736 {
1737   /* A draft address.  */
1738   CORE_ADDR pc = get_frame_pc (this_frame);
1739
1740   struct frame_info *next_frame = this_frame->next;
1741
1742   /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1743      Normally the resume address is inside the body of the function
1744      associated with THIS_FRAME, but there is a special case: when
1745      calling a function which the compiler knows will never return
1746      (for instance abort), the call may be the very last instruction
1747      in the calling function.  The resume address will point after the
1748      call and may be at the beginning of a different function
1749      entirely.
1750
1751      If THIS_FRAME is a signal frame or dummy frame, then we should
1752      not adjust the unwound PC.  For a dummy frame, GDB pushed the
1753      resume address manually onto the stack.  For a signal frame, the
1754      OS may have pushed the resume address manually and invoked the
1755      handler (e.g. GNU/Linux), or invoked the trampoline which called
1756      the signal handler - but in either case the signal handler is
1757      expected to return to the trampoline.  So in both of these
1758      cases we know that the resume address is executable and
1759      related.  So we only need to adjust the PC if THIS_FRAME
1760      is a normal function.
1761
1762      If the program has been interrupted while THIS_FRAME is current,
1763      then clearly the resume address is inside the associated
1764      function.  There are three kinds of interruption: debugger stop
1765      (next frame will be SENTINEL_FRAME), operating system
1766      signal or exception (next frame will be SIGTRAMP_FRAME),
1767      or debugger-induced function call (next frame will be
1768      DUMMY_FRAME).  So we only need to adjust the PC if
1769      NEXT_FRAME is a normal function.
1770
1771      We check the type of NEXT_FRAME first, since it is already
1772      known; frame type is determined by the unwinder, and since
1773      we have THIS_FRAME we've already selected an unwinder for
1774      NEXT_FRAME.
1775
1776      If the next frame is inlined, we need to keep going until we find
1777      the real function - for instance, if a signal handler is invoked
1778      while in an inlined function, then the code address of the
1779      "calling" normal function should not be adjusted either.  */
1780
1781   while (get_frame_type (next_frame) == INLINE_FRAME)
1782     next_frame = next_frame->next;
1783
1784   if (get_frame_type (next_frame) == NORMAL_FRAME
1785       && (get_frame_type (this_frame) == NORMAL_FRAME
1786           || get_frame_type (this_frame) == INLINE_FRAME))
1787     return pc - 1;
1788
1789   return pc;
1790 }
1791
1792 void
1793 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1794 {
1795   struct frame_info *next_frame;
1796   int notcurrent;
1797
1798   /* If the next frame represents an inlined function call, this frame's
1799      sal is the "call site" of that inlined function, which can not
1800      be inferred from get_frame_pc.  */
1801   next_frame = get_next_frame (frame);
1802   if (frame_inlined_callees (frame) > 0)
1803     {
1804       struct symbol *sym;
1805
1806       if (next_frame)
1807         sym = get_frame_function (next_frame);
1808       else
1809         sym = inline_skipped_symbol (inferior_ptid);
1810
1811       init_sal (sal);
1812       if (SYMBOL_LINE (sym) != 0)
1813         {
1814           sal->symtab = SYMBOL_SYMTAB (sym);
1815           sal->line = SYMBOL_LINE (sym);
1816         }
1817       else
1818         /* If the symbol does not have a location, we don't know where
1819            the call site is.  Do not pretend to.  This is jarring, but
1820            we can't do much better.  */
1821         sal->pc = get_frame_pc (frame);
1822
1823       return;
1824     }
1825
1826   /* If FRAME is not the innermost frame, that normally means that
1827      FRAME->pc points at the return instruction (which is *after* the
1828      call instruction), and we want to get the line containing the
1829      call (because the call is where the user thinks the program is).
1830      However, if the next frame is either a SIGTRAMP_FRAME or a
1831      DUMMY_FRAME, then the next frame will contain a saved interrupt
1832      PC and such a PC indicates the current (rather than next)
1833      instruction/line, consequently, for such cases, want to get the
1834      line containing fi->pc.  */
1835   notcurrent = (get_frame_pc (frame) != get_frame_address_in_block (frame));
1836   (*sal) = find_pc_line (get_frame_pc (frame), notcurrent);
1837 }
1838
1839 /* Per "frame.h", return the ``address'' of the frame.  Code should
1840    really be using get_frame_id().  */
1841 CORE_ADDR
1842 get_frame_base (struct frame_info *fi)
1843 {
1844   return get_frame_id (fi).stack_addr;
1845 }
1846
1847 /* High-level offsets into the frame.  Used by the debug info.  */
1848
1849 CORE_ADDR
1850 get_frame_base_address (struct frame_info *fi)
1851 {
1852   if (get_frame_type (fi) != NORMAL_FRAME)
1853     return 0;
1854   if (fi->base == NULL)
1855     fi->base = frame_base_find_by_frame (fi);
1856   /* Sneaky: If the low-level unwind and high-level base code share a
1857      common unwinder, let them share the prologue cache.  */
1858   if (fi->base->unwind == fi->unwind)
1859     return fi->base->this_base (fi, &fi->prologue_cache);
1860   return fi->base->this_base (fi, &fi->base_cache);
1861 }
1862
1863 CORE_ADDR
1864 get_frame_locals_address (struct frame_info *fi)
1865 {
1866   void **cache;
1867   if (get_frame_type (fi) != NORMAL_FRAME)
1868     return 0;
1869   /* If there isn't a frame address method, find it.  */
1870   if (fi->base == NULL)
1871     fi->base = frame_base_find_by_frame (fi);
1872   /* Sneaky: If the low-level unwind and high-level base code share a
1873      common unwinder, let them share the prologue cache.  */
1874   if (fi->base->unwind == fi->unwind)
1875     return fi->base->this_locals (fi, &fi->prologue_cache);
1876   return fi->base->this_locals (fi, &fi->base_cache);
1877 }
1878
1879 CORE_ADDR
1880 get_frame_args_address (struct frame_info *fi)
1881 {
1882   void **cache;
1883   if (get_frame_type (fi) != NORMAL_FRAME)
1884     return 0;
1885   /* If there isn't a frame address method, find it.  */
1886   if (fi->base == NULL)
1887     fi->base = frame_base_find_by_frame (fi);
1888   /* Sneaky: If the low-level unwind and high-level base code share a
1889      common unwinder, let them share the prologue cache.  */
1890   if (fi->base->unwind == fi->unwind)
1891     return fi->base->this_args (fi, &fi->prologue_cache);
1892   return fi->base->this_args (fi, &fi->base_cache);
1893 }
1894
1895 /* Return true if the frame unwinder for frame FI is UNWINDER; false
1896    otherwise.  */
1897
1898 int
1899 frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
1900 {
1901   if (fi->unwind == NULL)
1902     fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1903   return fi->unwind == unwinder;
1904 }
1905
1906 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1907    or -1 for a NULL frame.  */
1908
1909 int
1910 frame_relative_level (struct frame_info *fi)
1911 {
1912   if (fi == NULL)
1913     return -1;
1914   else
1915     return fi->level;
1916 }
1917
1918 enum frame_type
1919 get_frame_type (struct frame_info *frame)
1920 {
1921   if (frame->unwind == NULL)
1922     /* Initialize the frame's unwinder because that's what
1923        provides the frame's type.  */
1924     frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
1925   return frame->unwind->type;
1926 }
1927
1928 /* Memory access methods.  */
1929
1930 void
1931 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
1932                   gdb_byte *buf, int len)
1933 {
1934   read_memory (addr, buf, len);
1935 }
1936
1937 LONGEST
1938 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1939                          int len)
1940 {
1941   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1942   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1943   return read_memory_integer (addr, len, byte_order);
1944 }
1945
1946 ULONGEST
1947 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1948                            int len)
1949 {
1950   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1951   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1952   return read_memory_unsigned_integer (addr, len, byte_order);
1953 }
1954
1955 int
1956 safe_frame_unwind_memory (struct frame_info *this_frame,
1957                           CORE_ADDR addr, gdb_byte *buf, int len)
1958 {
1959   /* NOTE: target_read_memory returns zero on success!  */
1960   return !target_read_memory (addr, buf, len);
1961 }
1962
1963 /* Architecture methods.  */
1964
1965 struct gdbarch *
1966 get_frame_arch (struct frame_info *this_frame)
1967 {
1968   return frame_unwind_arch (this_frame->next);
1969 }
1970
1971 struct gdbarch *
1972 frame_unwind_arch (struct frame_info *next_frame)
1973 {
1974   if (!next_frame->prev_arch.p)
1975     {
1976       struct gdbarch *arch;
1977
1978       if (next_frame->unwind == NULL)
1979         next_frame->unwind
1980           = frame_unwind_find_by_frame (next_frame,
1981                                         &next_frame->prologue_cache);
1982
1983       if (next_frame->unwind->prev_arch != NULL)
1984         arch = next_frame->unwind->prev_arch (next_frame,
1985                                               &next_frame->prologue_cache);
1986       else
1987         arch = get_frame_arch (next_frame);
1988
1989       next_frame->prev_arch.arch = arch;
1990       next_frame->prev_arch.p = 1;
1991       if (frame_debug)
1992         fprintf_unfiltered (gdb_stdlog,
1993                             "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
1994                             next_frame->level,
1995                             gdbarch_bfd_arch_info (arch)->printable_name);
1996     }
1997
1998   return next_frame->prev_arch.arch;
1999 }
2000
2001 struct gdbarch *
2002 frame_unwind_caller_arch (struct frame_info *next_frame)
2003 {
2004   return frame_unwind_arch (skip_inlined_frames (next_frame));
2005 }
2006
2007 /* Stack pointer methods.  */
2008
2009 CORE_ADDR
2010 get_frame_sp (struct frame_info *this_frame)
2011 {
2012   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2013   /* Normality - an architecture that provides a way of obtaining any
2014      frame inner-most address.  */
2015   if (gdbarch_unwind_sp_p (gdbarch))
2016     /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2017        operate on THIS_FRAME now.  */
2018     return gdbarch_unwind_sp (gdbarch, this_frame->next);
2019   /* Now things are really are grim.  Hope that the value returned by
2020      the gdbarch_sp_regnum register is meaningful.  */
2021   if (gdbarch_sp_regnum (gdbarch) >= 0)
2022     return get_frame_register_unsigned (this_frame,
2023                                         gdbarch_sp_regnum (gdbarch));
2024   internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
2025 }
2026
2027 /* Return the reason why we can't unwind past FRAME.  */
2028
2029 enum unwind_stop_reason
2030 get_frame_unwind_stop_reason (struct frame_info *frame)
2031 {
2032   /* If we haven't tried to unwind past this point yet, then assume
2033      that unwinding would succeed.  */
2034   if (frame->prev_p == 0)
2035     return UNWIND_NO_REASON;
2036
2037   /* Otherwise, we set a reason when we succeeded (or failed) to
2038      unwind.  */
2039   return frame->stop_reason;
2040 }
2041
2042 /* Return a string explaining REASON.  */
2043
2044 const char *
2045 frame_stop_reason_string (enum unwind_stop_reason reason)
2046 {
2047   switch (reason)
2048     {
2049     case UNWIND_NULL_ID:
2050       return _("unwinder did not report frame ID");
2051
2052     case UNWIND_INNER_ID:
2053       return _("previous frame inner to this frame (corrupt stack?)");
2054
2055     case UNWIND_SAME_ID:
2056       return _("previous frame identical to this frame (corrupt stack?)");
2057
2058     case UNWIND_NO_SAVED_PC:
2059       return _("frame did not save the PC");
2060
2061     case UNWIND_NO_REASON:
2062     case UNWIND_FIRST_ERROR:
2063     default:
2064       internal_error (__FILE__, __LINE__,
2065                       "Invalid frame stop reason");
2066     }
2067 }
2068
2069 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2070    FRAME.  */
2071
2072 static void
2073 frame_cleanup_after_sniffer (void *arg)
2074 {
2075   struct frame_info *frame = arg;
2076
2077   /* The sniffer should not allocate a prologue cache if it did not
2078      match this frame.  */
2079   gdb_assert (frame->prologue_cache == NULL);
2080
2081   /* No sniffer should extend the frame chain; sniff based on what is
2082      already certain.  */
2083   gdb_assert (!frame->prev_p);
2084
2085   /* The sniffer should not check the frame's ID; that's circular.  */
2086   gdb_assert (!frame->this_id.p);
2087
2088   /* Clear cached fields dependent on the unwinder.
2089
2090      The previous PC is independent of the unwinder, but the previous
2091      function is not (see get_frame_address_in_block).  */
2092   frame->prev_func.p = 0;
2093   frame->prev_func.addr = 0;
2094
2095   /* Discard the unwinder last, so that we can easily find it if an assertion
2096      in this function triggers.  */
2097   frame->unwind = NULL;
2098 }
2099
2100 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2101    Return a cleanup which should be called if unwinding fails, and
2102    discarded if it succeeds.  */
2103
2104 struct cleanup *
2105 frame_prepare_for_sniffer (struct frame_info *frame,
2106                            const struct frame_unwind *unwind)
2107 {
2108   gdb_assert (frame->unwind == NULL);
2109   frame->unwind = unwind;
2110   return make_cleanup (frame_cleanup_after_sniffer, frame);
2111 }
2112
2113 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2114
2115 static struct cmd_list_element *set_backtrace_cmdlist;
2116 static struct cmd_list_element *show_backtrace_cmdlist;
2117
2118 static void
2119 set_backtrace_cmd (char *args, int from_tty)
2120 {
2121   help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2122 }
2123
2124 static void
2125 show_backtrace_cmd (char *args, int from_tty)
2126 {
2127   cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2128 }
2129
2130 void
2131 _initialize_frame (void)
2132 {
2133   obstack_init (&frame_cache_obstack);
2134
2135   observer_attach_target_changed (frame_observer_target_changed);
2136
2137   add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
2138 Set backtrace specific variables.\n\
2139 Configure backtrace variables such as the backtrace limit"),
2140                   &set_backtrace_cmdlist, "set backtrace ",
2141                   0/*allow-unknown*/, &setlist);
2142   add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
2143 Show backtrace specific variables\n\
2144 Show backtrace variables such as the backtrace limit"),
2145                   &show_backtrace_cmdlist, "show backtrace ",
2146                   0/*allow-unknown*/, &showlist);
2147
2148   add_setshow_boolean_cmd ("past-main", class_obscure,
2149                            &backtrace_past_main, _("\
2150 Set whether backtraces should continue past \"main\"."), _("\
2151 Show whether backtraces should continue past \"main\"."), _("\
2152 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2153 the backtrace at \"main\".  Set this variable if you need to see the rest\n\
2154 of the stack trace."),
2155                            NULL,
2156                            show_backtrace_past_main,
2157                            &set_backtrace_cmdlist,
2158                            &show_backtrace_cmdlist);
2159
2160   add_setshow_boolean_cmd ("past-entry", class_obscure,
2161                            &backtrace_past_entry, _("\
2162 Set whether backtraces should continue past the entry point of a program."),
2163                            _("\
2164 Show whether backtraces should continue past the entry point of a program."),
2165                            _("\
2166 Normally there are no callers beyond the entry point of a program, so GDB\n\
2167 will terminate the backtrace there.  Set this variable if you need to see \n\
2168 the rest of the stack trace."),
2169                            NULL,
2170                            show_backtrace_past_entry,
2171                            &set_backtrace_cmdlist,
2172                            &show_backtrace_cmdlist);
2173
2174   add_setshow_integer_cmd ("limit", class_obscure,
2175                            &backtrace_limit, _("\
2176 Set an upper bound on the number of backtrace levels."), _("\
2177 Show the upper bound on the number of backtrace levels."), _("\
2178 No more than the specified number of frames can be displayed or examined.\n\
2179 Zero is unlimited."),
2180                            NULL,
2181                            show_backtrace_limit,
2182                            &set_backtrace_cmdlist,
2183                            &show_backtrace_cmdlist);
2184
2185   /* Debug this files internals. */
2186   add_setshow_zinteger_cmd ("frame", class_maintenance, &frame_debug,  _("\
2187 Set frame debugging."), _("\
2188 Show frame debugging."), _("\
2189 When non-zero, frame specific internal debugging is enabled."),
2190                             NULL,
2191                             show_frame_debug,
2192                             &setdebuglist, &showdebuglist);
2193 }