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