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