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