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