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