Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / frame.c
blobd3c4c9640aac088d7cc4087115991831def41324
1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "frame.h"
22 #include "target.h"
23 #include "value.h"
24 #include "inferior.h"
25 #include "regcache.h"
26 #include "user-regs.h"
27 #include "gdbsupport/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 #include "dwarf2/loc.h"
48 /* The sentinel frame terminates the innermost end of the frame chain.
49 If unwound, it returns the information needed to construct an
50 innermost frame.
52 The current frame, which is the innermost frame, can be found at
53 sentinel_frame->prev.
55 This is an optimization to be able to find the sentinel frame quickly,
56 it could otherwise be found in the frame cache. */
58 static frame_info *sentinel_frame;
60 /* Number of calls to reinit_frame_cache. */
61 static unsigned int frame_cache_generation = 0;
63 /* See frame.h. */
65 unsigned int
66 get_frame_cache_generation ()
68 return frame_cache_generation;
71 /* The values behind the global "set backtrace ..." settings. */
72 set_backtrace_options user_set_backtrace_options;
74 static frame_info_ptr get_prev_frame_raw (const frame_info_ptr &this_frame);
75 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
76 static frame_info_ptr create_new_frame (frame_id id);
78 /* Status of some values cached in the frame_info object. */
80 enum cached_copy_status
82 /* Value is unknown. */
83 CC_UNKNOWN,
85 /* We have a value. */
86 CC_VALUE,
88 /* Value was not saved. */
89 CC_NOT_SAVED,
91 /* Value is unavailable. */
92 CC_UNAVAILABLE
95 enum class frame_id_status
97 /* Frame id is not computed. */
98 NOT_COMPUTED = 0,
100 /* Frame id is being computed (compute_frame_id is active). */
101 COMPUTING,
103 /* Frame id has been computed. */
104 COMPUTED,
107 /* We keep a cache of stack frames, each of which is a "struct
108 frame_info". The innermost one gets allocated (in
109 wait_for_inferior) each time the inferior stops; sentinel_frame
110 points to it. Additional frames get allocated (in get_prev_frame)
111 as needed, and are chained through the next and prev fields. Any
112 time that the frame cache becomes invalid (most notably when we
113 execute something, but also if we change how we interpret the
114 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
115 which reads new symbols)), we should call reinit_frame_cache. */
117 struct frame_info
119 /* Return a string representation of this frame. */
120 std::string to_string () const;
122 /* Level of this frame. The inner-most (youngest) frame is at level
123 0. As you move towards the outer-most (oldest) frame, the level
124 increases. This is a cached value. It could just as easily be
125 computed by counting back from the selected frame to the inner
126 most frame. */
127 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
128 reserved to indicate a bogus frame - one that has been created
129 just to keep GDB happy (GDB always needs a frame). For the
130 moment leave this as speculation. */
131 int level;
133 /* The frame's program space. */
134 struct program_space *pspace;
136 /* The frame's address space. */
137 const address_space *aspace;
139 /* The frame's low-level unwinder and corresponding cache. The
140 low-level unwinder is responsible for unwinding register values
141 for the previous frame. The low-level unwind methods are
142 selected based on the presence, or otherwise, of register unwind
143 information such as CFI. */
144 void *prologue_cache;
145 const struct frame_unwind *unwind;
147 /* Cached copy of the previous frame's architecture. */
148 struct
150 bool p;
151 struct gdbarch *arch;
152 } prev_arch;
154 /* Cached copy of the previous frame's resume address. */
155 struct {
156 cached_copy_status status;
157 /* Did VALUE require unmasking when being read. */
158 bool masked;
159 CORE_ADDR value;
160 } prev_pc;
162 /* Cached copy of the previous frame's function address. */
163 struct
165 CORE_ADDR addr;
166 cached_copy_status status;
167 } prev_func;
169 /* This frame's ID. */
170 struct
172 frame_id_status p;
173 struct frame_id value;
174 } this_id;
176 /* The frame's high-level base methods, and corresponding cache.
177 The high level base methods are selected based on the frame's
178 debug info. */
179 const struct frame_base *base;
180 void *base_cache;
182 /* Pointers to the next (down, inner, younger) and previous (up,
183 outer, older) frame_info's in the frame cache. */
184 struct frame_info *next; /* down, inner, younger */
185 bool prev_p;
186 struct frame_info *prev; /* up, outer, older */
188 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
189 could. Only valid when PREV_P is set. */
190 enum unwind_stop_reason stop_reason;
192 /* A frame specific string describing the STOP_REASON in more detail.
193 Only valid when PREV_P is set, but even then may still be NULL. */
194 const char *stop_string;
197 /* See frame.h. */
199 void
200 set_frame_previous_pc_masked (const frame_info_ptr &frame)
202 frame->prev_pc.masked = true;
205 /* See frame.h. */
207 bool
208 get_frame_pc_masked (const frame_info_ptr &frame)
210 gdb_assert (frame->next != nullptr);
211 gdb_assert (frame->next->prev_pc.status == CC_VALUE);
213 return frame->next->prev_pc.masked;
216 /* A frame stash used to speed up frame lookups. Create a hash table
217 to stash frames previously accessed from the frame cache for
218 quicker subsequent retrieval. The hash table is emptied whenever
219 the frame cache is invalidated. */
221 static htab_t frame_stash;
223 /* Internal function to calculate a hash from the frame_id addresses,
224 using as many valid addresses as possible. Frames below level 0
225 are not stored in the hash table. */
227 static hashval_t
228 frame_addr_hash (const void *ap)
230 const frame_info *frame = (const frame_info *) ap;
231 const struct frame_id f_id = frame->this_id.value;
232 hashval_t hash = 0;
234 gdb_assert (f_id.stack_status != FID_STACK_INVALID
235 || f_id.code_addr_p
236 || f_id.special_addr_p);
238 if (f_id.stack_status == FID_STACK_VALID)
239 hash = iterative_hash (&f_id.stack_addr,
240 sizeof (f_id.stack_addr), hash);
241 if (f_id.code_addr_p)
242 hash = iterative_hash (&f_id.code_addr,
243 sizeof (f_id.code_addr), hash);
244 if (f_id.special_addr_p)
245 hash = iterative_hash (&f_id.special_addr,
246 sizeof (f_id.special_addr), hash);
248 char user_created_p = f_id.user_created_p;
249 hash = iterative_hash (&user_created_p, sizeof (user_created_p), hash);
251 return hash;
254 /* Internal equality function for the hash table. This function
255 defers equality operations to frame_id::operator==. */
257 static int
258 frame_addr_hash_eq (const void *a, const void *b)
260 const frame_info *f_entry = (const frame_info *) a;
261 const frame_info *f_element = (const frame_info *) b;
263 return f_entry->this_id.value == f_element->this_id.value;
266 /* Deletion function for the frame cache hash table. */
268 static void
269 frame_info_del (frame_info *frame)
271 if (frame->prologue_cache != nullptr
272 && frame->unwind->dealloc_cache != nullptr)
273 frame->unwind->dealloc_cache (frame, frame->prologue_cache);
275 if (frame->base_cache != nullptr
276 && frame->base->unwind->dealloc_cache != nullptr)
277 frame->base->unwind->dealloc_cache (frame, frame->base_cache);
280 /* Internal function to create the frame_stash hash table. 100 seems
281 to be a good compromise to start the hash table at. */
283 static void
284 frame_stash_create (void)
286 frame_stash = htab_create
287 (100, frame_addr_hash, frame_addr_hash_eq,
288 [] (void *p)
290 auto frame = static_cast<frame_info *> (p);
291 frame_info_del (frame);
295 /* Internal function to add a frame to the frame_stash hash table.
296 Returns false if a frame with the same ID was already stashed, true
297 otherwise. */
299 static bool
300 frame_stash_add (frame_info *frame)
302 /* Valid frame levels are -1 (sentinel frames) and above. */
303 gdb_assert (frame->level >= -1);
305 frame_info **slot = (frame_info **) htab_find_slot (frame_stash,
306 frame, INSERT);
308 /* If we already have a frame in the stack with the same id, we
309 either have a stack cycle (corrupted stack?), or some bug
310 elsewhere in GDB. In any case, ignore the duplicate and return
311 an indication to the caller. */
312 if (*slot != nullptr)
313 return false;
315 *slot = frame;
316 return true;
319 /* Internal function to search the frame stash for an entry with the
320 given frame ID. If found, return that frame. Otherwise return
321 NULL. */
323 static frame_info_ptr
324 frame_stash_find (struct frame_id id)
326 struct frame_info dummy;
327 frame_info *frame;
329 dummy.this_id.value = id;
330 frame = (frame_info *) htab_find (frame_stash, &dummy);
331 return frame_info_ptr (frame);
334 /* Internal function to invalidate the frame stash by removing all
335 entries in it. This only occurs when the frame cache is
336 invalidated. */
338 static void
339 frame_stash_invalidate (void)
341 htab_empty (frame_stash);
344 /* See frame.h */
345 scoped_restore_selected_frame::scoped_restore_selected_frame ()
347 m_lang = current_language->la_language;
348 save_selected_frame (&m_fid, &m_level);
351 /* See frame.h */
352 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
354 restore_selected_frame (m_fid, m_level);
355 set_language (m_lang);
358 /* Flag to control debugging. */
360 bool frame_debug;
362 static void
363 show_frame_debug (struct ui_file *file, int from_tty,
364 struct cmd_list_element *c, const char *value)
366 gdb_printf (file, _("Frame debugging is %s.\n"), value);
369 /* Implementation of "show backtrace past-main". */
371 static void
372 show_backtrace_past_main (struct ui_file *file, int from_tty,
373 struct cmd_list_element *c, const char *value)
375 gdb_printf (file,
376 _("Whether backtraces should "
377 "continue past \"main\" is %s.\n"),
378 value);
381 /* Implementation of "show backtrace past-entry". */
383 static void
384 show_backtrace_past_entry (struct ui_file *file, int from_tty,
385 struct cmd_list_element *c, const char *value)
387 gdb_printf (file, _("Whether backtraces should continue past the "
388 "entry point of a program is %s.\n"),
389 value);
392 /* Implementation of "show backtrace limit". */
394 static void
395 show_backtrace_limit (struct ui_file *file, int from_tty,
396 struct cmd_list_element *c, const char *value)
398 gdb_printf (file,
399 _("An upper bound on the number "
400 "of backtrace levels is %s.\n"),
401 value);
404 /* See frame.h. */
406 std::string
407 frame_id::to_string () const
409 const struct frame_id &id = *this;
411 std::string res = "{";
413 if (id.stack_status == FID_STACK_INVALID)
414 res += "!stack";
415 else if (id.stack_status == FID_STACK_UNAVAILABLE)
416 res += "stack=<unavailable>";
417 else if (id.stack_status == FID_STACK_SENTINEL)
418 res += "stack=<sentinel>";
419 else if (id.stack_status == FID_STACK_OUTER)
420 res += "stack=<outer>";
421 else
422 res += std::string ("stack=") + hex_string (id.stack_addr);
424 /* Helper function to format 'N=A' if P is true, otherwise '!N'. */
425 auto field_to_string = [] (const char *n, bool p, CORE_ADDR a) -> std::string
427 if (p)
428 return std::string (n) + "=" + core_addr_to_string (a);
429 else
430 return std::string ("!") + std::string (n);
433 res += (std::string (",")
434 + field_to_string ("code", id.code_addr_p, id.code_addr)
435 + std::string (",")
436 + field_to_string ("special", id.special_addr_p, id.special_addr));
438 if (id.artificial_depth)
439 res += ",artificial=" + std::to_string (id.artificial_depth);
440 res += "}";
441 return res;
444 /* See frame.h. */
446 const char *
447 frame_type_str (frame_type type)
449 switch (type)
451 case NORMAL_FRAME:
452 return "NORMAL_FRAME";
454 case DUMMY_FRAME:
455 return "DUMMY_FRAME";
457 case INLINE_FRAME:
458 return "INLINE_FRAME";
460 case TAILCALL_FRAME:
461 return "TAILCALL_FRAME";
463 case SIGTRAMP_FRAME:
464 return "SIGTRAMP_FRAME";
466 case ARCH_FRAME:
467 return "ARCH_FRAME";
469 case SENTINEL_FRAME:
470 return "SENTINEL_FRAME";
472 default:
473 return "<unknown type>";
477 /* See struct frame_info. */
479 std::string
480 frame_info::to_string () const
482 const frame_info *fi = this;
484 std::string res;
486 res += string_printf ("{level=%d,", fi->level);
488 if (fi->unwind != NULL)
489 res += string_printf ("type=%s,", frame_type_str (fi->unwind->type));
490 else
491 res += "type=<unknown>,";
493 if (fi->unwind != NULL)
494 res += string_printf ("unwinder=\"%s\",", fi->unwind->name);
495 else
496 res += "unwinder=<unknown>,";
498 if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
499 res += "pc=<unknown>,";
500 else if (fi->next->prev_pc.status == CC_VALUE)
501 res += string_printf ("pc=%s%s,", hex_string (fi->next->prev_pc.value),
502 fi->next->prev_pc.masked ? "[PAC]" : "");
503 else if (fi->next->prev_pc.status == CC_NOT_SAVED)
504 res += "pc=<not saved>,";
505 else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
506 res += "pc=<unavailable>,";
508 if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
509 res += "id=<not computed>,";
510 else if (fi->this_id.p == frame_id_status::COMPUTING)
511 res += "id=<computing>,";
512 else
513 res += string_printf ("id=%s,", fi->this_id.value.to_string ().c_str ());
515 if (fi->next != NULL && fi->next->prev_func.status == CC_VALUE)
516 res += string_printf ("func=%s", hex_string (fi->next->prev_func.addr));
517 else
518 res += "func=<unknown>";
520 res += "}";
522 return res;
525 /* Given FRAME, return the enclosing frame as found in real frames read-in from
526 inferior memory. Skip any previous frames which were made up by GDB.
527 Return FRAME if FRAME is a non-artificial frame.
528 Return NULL if FRAME is the start of an artificial-only chain. */
530 static frame_info_ptr
531 skip_artificial_frames (const frame_info_ptr &initial_frame)
533 /* Note we use get_prev_frame_always, and not get_prev_frame. The
534 latter will truncate the frame chain, leading to this function
535 unintentionally returning a null_frame_id (e.g., when the user
536 sets a backtrace limit).
538 Note that for record targets we may get a frame chain that consists
539 of artificial frames only. */
540 frame_info_ptr frame = initial_frame;
541 while (get_frame_type (frame) == INLINE_FRAME
542 || get_frame_type (frame) == TAILCALL_FRAME)
544 frame = get_prev_frame_always (frame);
545 if (frame == NULL)
546 break;
549 return frame;
552 frame_info_ptr
553 skip_unwritable_frames (const frame_info_ptr &initial_frame)
555 frame_info_ptr frame = initial_frame;
556 while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0)
558 frame = get_prev_frame (frame);
559 if (frame == NULL)
560 break;
563 return frame;
566 /* See frame.h. */
568 frame_info_ptr
569 skip_tailcall_frames (const frame_info_ptr &initial_frame)
571 frame_info_ptr frame = initial_frame;
572 while (get_frame_type (frame) == TAILCALL_FRAME)
574 /* Note that for record targets we may get a frame chain that consists of
575 tailcall frames only. */
576 frame = get_prev_frame (frame);
577 if (frame == NULL)
578 break;
581 return frame;
584 /* Compute the frame's uniq ID that can be used to, later, re-find the
585 frame. */
587 static void
588 compute_frame_id (const frame_info_ptr &fi)
590 FRAME_SCOPED_DEBUG_ENTER_EXIT;
592 gdb_assert (fi->this_id.p == frame_id_status::NOT_COMPUTED);
594 unsigned int entry_generation = get_frame_cache_generation ();
598 /* Mark this frame's id as "being computed. */
599 fi->this_id.p = frame_id_status::COMPUTING;
601 frame_debug_printf ("fi=%d", fi->level);
603 /* Find the unwinder. */
604 if (fi->unwind == NULL)
605 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
607 /* Find THIS frame's ID. */
608 /* Default to outermost if no ID is found. */
609 fi->this_id.value = outer_frame_id;
610 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
611 gdb_assert (frame_id_p (fi->this_id.value));
613 /* Mark this frame's id as "computed". */
614 fi->this_id.p = frame_id_status::COMPUTED;
616 frame_debug_printf (" -> %s", fi->this_id.value.to_string ().c_str ());
618 catch (const gdb_exception &ex)
620 /* On error, revert the frame id status to not computed. If the frame
621 cache generation changed, the frame object doesn't exist anymore, so
622 don't touch it. */
623 if (get_frame_cache_generation () == entry_generation)
624 fi->this_id.p = frame_id_status::NOT_COMPUTED;
626 throw;
630 /* Return a frame uniq ID that can be used to, later, re-find the
631 frame. */
633 struct frame_id
634 get_frame_id (const frame_info_ptr &fi)
636 if (fi == NULL)
637 return null_frame_id;
639 /* It's always invalid to try to get a frame's id while it is being
640 computed. */
641 gdb_assert (fi->this_id.p != frame_id_status::COMPUTING);
643 if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
645 /* If we haven't computed the frame id yet, then it must be that
646 this is the current frame. Compute it now, and stash the
647 result. The IDs of other frames are computed as soon as
648 they're created, in order to detect cycles. See
649 get_prev_frame_if_no_cycle. */
650 gdb_assert (fi->level == 0);
652 /* Compute. */
653 compute_frame_id (fi);
655 /* Since this is the first frame in the chain, this should
656 always succeed. */
657 bool stashed = frame_stash_add (fi.get ());
658 gdb_assert (stashed);
661 return fi->this_id.value;
664 struct frame_id
665 get_stack_frame_id (const frame_info_ptr &next_frame)
667 return get_frame_id (skip_artificial_frames (next_frame));
670 struct frame_id
671 frame_unwind_caller_id (const frame_info_ptr &initial_next_frame)
673 /* Use get_prev_frame_always, and not get_prev_frame. The latter
674 will truncate the frame chain, leading to this function
675 unintentionally returning a null_frame_id (e.g., when a caller
676 requests the frame ID of "main()"s caller. */
678 frame_info_ptr next_frame = skip_artificial_frames (initial_next_frame);
679 if (next_frame == NULL)
680 return null_frame_id;
682 frame_info_ptr this_frame = get_prev_frame_always (next_frame);
683 if (this_frame)
684 return get_frame_id (skip_artificial_frames (this_frame));
685 else
686 return null_frame_id;
689 const struct frame_id null_frame_id = { 0 }; /* All zeros. */
690 const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_OUTER, 0, 1, 0 };
692 struct frame_id
693 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
694 CORE_ADDR special_addr)
696 struct frame_id id = null_frame_id;
698 id.stack_addr = stack_addr;
699 id.stack_status = FID_STACK_VALID;
700 id.code_addr = code_addr;
701 id.code_addr_p = true;
702 id.special_addr = special_addr;
703 id.special_addr_p = true;
704 return id;
707 /* See frame.h. */
709 struct frame_id
710 frame_id_build_unavailable_stack (CORE_ADDR code_addr)
712 struct frame_id id = null_frame_id;
714 id.stack_status = FID_STACK_UNAVAILABLE;
715 id.code_addr = code_addr;
716 id.code_addr_p = true;
717 return id;
720 /* See frame.h. */
722 struct frame_id
723 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
724 CORE_ADDR special_addr)
726 struct frame_id id = null_frame_id;
728 id.stack_status = FID_STACK_UNAVAILABLE;
729 id.code_addr = code_addr;
730 id.code_addr_p = true;
731 id.special_addr = special_addr;
732 id.special_addr_p = true;
733 return id;
736 struct frame_id
737 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
739 struct frame_id id = null_frame_id;
741 id.stack_addr = stack_addr;
742 id.stack_status = FID_STACK_VALID;
743 id.code_addr = code_addr;
744 id.code_addr_p = true;
745 return id;
748 struct frame_id
749 frame_id_build_wild (CORE_ADDR stack_addr)
751 struct frame_id id = null_frame_id;
753 id.stack_addr = stack_addr;
754 id.stack_status = FID_STACK_VALID;
755 return id;
758 /* See frame.h. */
760 frame_id
761 frame_id_build_sentinel (CORE_ADDR stack_addr, CORE_ADDR code_addr)
763 frame_id id = null_frame_id;
765 id.stack_status = FID_STACK_SENTINEL;
766 id.special_addr_p = 1;
768 if (stack_addr != 0 || code_addr != 0)
770 /* The purpose of saving these in the sentinel frame ID is to be able to
771 differentiate the IDs of several sentinel frames that could exist
772 simultaneously in the frame cache. */
773 id.stack_addr = stack_addr;
774 id.code_addr = code_addr;
775 id.code_addr_p = 1;
778 return id;
781 bool
782 frame_id_p (frame_id l)
784 /* The frame is valid iff it has a valid stack address. */
785 bool p = l.stack_status != FID_STACK_INVALID;
787 frame_debug_printf ("l=%s -> %d", l.to_string ().c_str (), p);
789 return p;
792 bool
793 frame_id_artificial_p (frame_id l)
795 if (!frame_id_p (l))
796 return false;
798 return l.artificial_depth != 0;
801 bool
802 frame_id::operator== (const frame_id &r) const
804 bool eq;
806 if (stack_status == FID_STACK_INVALID
807 || r.stack_status == FID_STACK_INVALID)
808 /* Like a NaN, if either ID is invalid, the result is false.
809 Note that a frame ID is invalid iff it is the null frame ID. */
810 eq = false;
811 else if (stack_status != r.stack_status || stack_addr != r.stack_addr)
812 /* If .stack addresses are different, the frames are different. */
813 eq = false;
814 else if (code_addr_p && r.code_addr_p && code_addr != r.code_addr)
815 /* An invalid code addr is a wild card. If .code addresses are
816 different, the frames are different. */
817 eq = false;
818 else if (special_addr_p && r.special_addr_p
819 && special_addr != r.special_addr)
820 /* An invalid special addr is a wild card (or unused). Otherwise
821 if special addresses are different, the frames are different. */
822 eq = false;
823 else if (artificial_depth != r.artificial_depth)
824 /* If artificial depths are different, the frames must be different. */
825 eq = false;
826 else if (user_created_p != r.user_created_p)
827 eq = false;
828 else
829 /* Frames are equal. */
830 eq = true;
832 frame_debug_printf ("l=%s, r=%s -> %d",
833 to_string ().c_str (), r.to_string ().c_str (), eq);
835 return eq;
838 /* Safety net to check whether frame ID L should be inner to
839 frame ID R, according to their stack addresses.
841 This method cannot be used to compare arbitrary frames, as the
842 ranges of valid stack addresses may be discontiguous (e.g. due
843 to sigaltstack).
845 However, it can be used as safety net to discover invalid frame
846 IDs in certain circumstances. Assuming that NEXT is the immediate
847 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
849 * The stack address of NEXT must be inner-than-or-equal to the stack
850 address of THIS.
852 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
853 error has occurred.
855 * If NEXT and THIS have different stack addresses, no other frame
856 in the frame chain may have a stack address in between.
858 Therefore, if frame_id_inner (TEST, THIS) holds, but
859 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
860 to a valid frame in the frame chain.
862 The sanity checks above cannot be performed when a SIGTRAMP frame
863 is involved, because signal handlers might be executed on a different
864 stack than the stack used by the routine that caused the signal
865 to be raised. This can happen for instance when a thread exceeds
866 its maximum stack size. In this case, certain compilers implement
867 a stack overflow strategy that cause the handler to be run on a
868 different stack. */
870 static bool
871 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
873 bool inner;
875 if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
876 /* Like NaN, any operation involving an invalid ID always fails.
877 Likewise if either ID has an unavailable stack address. */
878 inner = false;
879 else if (l.artificial_depth > r.artificial_depth
880 && l.stack_addr == r.stack_addr
881 && l.code_addr_p == r.code_addr_p
882 && l.special_addr_p == r.special_addr_p
883 && l.special_addr == r.special_addr)
885 /* Same function, different inlined functions. */
886 const struct block *lb, *rb;
888 gdb_assert (l.code_addr_p && r.code_addr_p);
890 lb = block_for_pc (l.code_addr);
891 rb = block_for_pc (r.code_addr);
893 if (lb == NULL || rb == NULL)
894 /* Something's gone wrong. */
895 inner = false;
896 else
897 /* This will return true if LB and RB are the same block, or
898 if the block with the smaller depth lexically encloses the
899 block with the greater depth. */
900 inner = rb->contains (lb);
902 else
903 /* Only return non-zero when strictly inner than. Note that, per
904 comment in "frame.h", there is some fuzz here. Frameless
905 functions are not strictly inner than (same .stack but
906 different .code and/or .special address). */
907 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
909 frame_debug_printf ("is l=%s inner than r=%s? %d",
910 l.to_string ().c_str (), r.to_string ().c_str (),
911 inner);
913 return inner;
916 frame_info_ptr
917 frame_find_by_id (struct frame_id id)
919 frame_info_ptr frame, prev_frame;
921 /* ZERO denotes the null frame, let the caller decide what to do
922 about it. Should it instead return get_current_frame()? */
923 if (!frame_id_p (id))
924 return NULL;
926 /* Check for the sentinel frame. */
927 if (id == frame_id_build_sentinel (0, 0))
928 return frame_info_ptr (sentinel_frame);
930 /* Try using the frame stash first. Finding it there removes the need
931 to perform the search by looping over all frames, which can be very
932 CPU-intensive if the number of frames is very high (the loop is O(n)
933 and get_prev_frame performs a series of checks that are relatively
934 expensive). This optimization is particularly useful when this function
935 is called from another function (such as value_fetch_lazy, case
936 val->lval () == lval_register) which already loops over all frames,
937 making the overall behavior O(n^2). */
938 frame = frame_stash_find (id);
939 if (frame)
940 return frame;
942 for (frame = get_current_frame (); ; frame = prev_frame)
944 struct frame_id self = get_frame_id (frame);
946 if (id == self)
947 /* An exact match. */
948 return frame;
950 prev_frame = get_prev_frame (frame);
951 if (!prev_frame)
952 return NULL;
954 /* As a safety net to avoid unnecessary backtracing while trying
955 to find an invalid ID, we check for a common situation where
956 we can detect from comparing stack addresses that no other
957 frame in the current frame chain can have this ID. See the
958 comment at frame_id_inner for details. */
959 if (get_frame_type (frame) == NORMAL_FRAME
960 && !frame_id_inner (get_frame_arch (frame), id, self)
961 && frame_id_inner (get_frame_arch (prev_frame), id,
962 get_frame_id (prev_frame)))
963 return NULL;
965 return NULL;
968 static CORE_ADDR
969 frame_unwind_pc (const frame_info_ptr &this_frame)
971 if (this_frame->prev_pc.status == CC_UNKNOWN)
973 struct gdbarch *prev_gdbarch;
974 CORE_ADDR pc = 0;
975 bool pc_p = false;
977 /* The right way. The `pure' way. The one true way. This
978 method depends solely on the register-unwind code to
979 determine the value of registers in THIS frame, and hence
980 the value of this frame's PC (resume address). A typical
981 implementation is no more than:
983 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
984 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
986 Note: this method is very heavily dependent on a correct
987 register-unwind implementation, it pays to fix that
988 method first; this method is frame type agnostic, since
989 it only deals with register values, it works with any
990 frame. This is all in stark contrast to the old
991 FRAME_SAVED_PC which would try to directly handle all the
992 different ways that a PC could be unwound. */
993 prev_gdbarch = frame_unwind_arch (this_frame);
997 pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
998 pc_p = true;
1000 catch (const gdb_exception_error &ex)
1002 if (ex.error == NOT_AVAILABLE_ERROR)
1004 this_frame->prev_pc.status = CC_UNAVAILABLE;
1006 frame_debug_printf ("this_frame=%d -> <unavailable>",
1007 this_frame->level);
1009 else if (ex.error == OPTIMIZED_OUT_ERROR)
1011 this_frame->prev_pc.status = CC_NOT_SAVED;
1013 frame_debug_printf ("this_frame=%d -> <not saved>",
1014 this_frame->level);
1016 else
1017 throw;
1020 if (pc_p)
1022 this_frame->prev_pc.value = pc;
1023 this_frame->prev_pc.status = CC_VALUE;
1025 frame_debug_printf ("this_frame=%d -> %s",
1026 this_frame->level,
1027 hex_string (this_frame->prev_pc.value));
1031 if (this_frame->prev_pc.status == CC_VALUE)
1032 return this_frame->prev_pc.value;
1033 else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
1034 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
1035 else if (this_frame->prev_pc.status == CC_NOT_SAVED)
1036 throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
1037 else
1038 internal_error ("unexpected prev_pc status: %d",
1039 (int) this_frame->prev_pc.status);
1042 CORE_ADDR
1043 frame_unwind_caller_pc (const frame_info_ptr &initial_this_frame)
1045 frame_info_ptr this_frame = skip_artificial_frames (initial_this_frame);
1047 /* We must have a non-artificial frame. The caller is supposed to check
1048 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1049 in this case. */
1050 gdb_assert (this_frame != nullptr);
1052 return frame_unwind_pc (this_frame);
1055 bool
1056 get_frame_func_if_available (const frame_info_ptr &this_frame, CORE_ADDR *pc)
1058 frame_info *next_frame = this_frame->next;
1060 if (next_frame->prev_func.status == CC_UNKNOWN)
1062 CORE_ADDR addr_in_block;
1064 /* Make certain that this, and not the adjacent, function is
1065 found. */
1066 if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
1068 next_frame->prev_func.status = CC_UNAVAILABLE;
1070 frame_debug_printf ("this_frame=%d -> unavailable",
1071 this_frame->level);
1073 else
1075 next_frame->prev_func.status = CC_VALUE;
1076 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
1078 frame_debug_printf ("this_frame=%d -> %s",
1079 this_frame->level,
1080 hex_string (next_frame->prev_func.addr));
1084 if (next_frame->prev_func.status == CC_UNAVAILABLE)
1086 *pc = -1;
1087 return false;
1089 else
1091 gdb_assert (next_frame->prev_func.status == CC_VALUE);
1093 *pc = next_frame->prev_func.addr;
1094 return true;
1098 CORE_ADDR
1099 get_frame_func (const frame_info_ptr &this_frame)
1101 CORE_ADDR pc;
1103 if (!get_frame_func_if_available (this_frame, &pc))
1104 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
1106 return pc;
1109 std::unique_ptr<readonly_detached_regcache>
1110 frame_save_as_regcache (const frame_info_ptr &this_frame)
1112 auto cooked_read = [this_frame] (int regnum, gdb::array_view<gdb_byte> buf)
1114 if (!deprecated_frame_register_read (this_frame, regnum, buf.data ()))
1115 return REG_UNAVAILABLE;
1116 else
1117 return REG_VALID;
1120 std::unique_ptr<readonly_detached_regcache> regcache
1121 (new readonly_detached_regcache (get_frame_arch (this_frame), cooked_read));
1123 return regcache;
1126 void
1127 frame_pop (const frame_info_ptr &this_frame)
1129 frame_info_ptr prev_frame;
1131 if (get_frame_type (this_frame) == DUMMY_FRAME)
1133 /* Popping a dummy frame involves restoring more than just registers.
1134 dummy_frame_pop does all the work. */
1135 dummy_frame_pop (get_frame_id (this_frame), inferior_thread ());
1136 return;
1139 /* Ensure that we have a frame to pop to. */
1140 prev_frame = get_prev_frame_always (this_frame);
1142 if (!prev_frame)
1143 error (_("Cannot pop the initial frame."));
1145 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1146 entering THISFRAME. */
1147 prev_frame = skip_tailcall_frames (prev_frame);
1149 if (prev_frame == NULL)
1150 error (_("Cannot find the caller frame."));
1152 /* Make a copy of all the register values unwound from this frame.
1153 Save them in a scratch buffer so that there isn't a race between
1154 trying to extract the old values from the current regcache while
1155 at the same time writing new values into that same cache. */
1156 std::unique_ptr<readonly_detached_regcache> scratch
1157 = frame_save_as_regcache (prev_frame);
1159 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1160 target's register cache that it is about to be hit with a burst
1161 register transfer and that the sequence of register writes should
1162 be batched. The pair target_prepare_to_store() and
1163 target_store_registers() kind of suggest this functionality.
1164 Unfortunately, they don't implement it. Their lack of a formal
1165 definition can lead to targets writing back bogus values
1166 (arguably a bug in the target code mind). */
1167 /* Now copy those saved registers into the current regcache. */
1168 get_thread_regcache (inferior_thread ())->restore (scratch.get ());
1170 /* We've made right mess of GDB's local state, just discard
1171 everything. */
1172 reinit_frame_cache ();
1175 void
1176 frame_register_unwind (const frame_info_ptr &next_frame, int regnum,
1177 int *optimizedp, int *unavailablep,
1178 enum lval_type *lvalp, CORE_ADDR *addrp,
1179 int *realnump, gdb_byte *bufferp)
1181 struct value *value;
1183 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1184 that the value proper does not need to be fetched. */
1185 gdb_assert (optimizedp != NULL);
1186 gdb_assert (lvalp != NULL);
1187 gdb_assert (addrp != NULL);
1188 gdb_assert (realnump != NULL);
1189 /* gdb_assert (bufferp != NULL); */
1191 value = frame_unwind_register_value (next_frame, regnum);
1193 gdb_assert (value != NULL);
1195 *optimizedp = value->optimized_out ();
1196 *unavailablep = !value->entirely_available ();
1197 *lvalp = value->lval ();
1198 *addrp = value->address ();
1199 if (*lvalp == lval_register)
1200 *realnump = value->regnum ();
1201 else
1202 *realnump = -1;
1204 if (bufferp)
1206 if (!*optimizedp && !*unavailablep)
1207 memcpy (bufferp, value->contents_all ().data (),
1208 value->type ()->length ());
1209 else
1210 memset (bufferp, 0, value->type ()->length ());
1213 /* Dispose of the new value. This prevents watchpoints from
1214 trying to watch the saved frame pointer. */
1215 release_value (value);
1218 void
1219 frame_unwind_register (const frame_info_ptr &next_frame, int regnum, gdb_byte *buf)
1221 int optimized;
1222 int unavailable;
1223 CORE_ADDR addr;
1224 int realnum;
1225 enum lval_type lval;
1227 frame_register_unwind (next_frame, regnum, &optimized, &unavailable,
1228 &lval, &addr, &realnum, buf);
1230 if (optimized)
1231 throw_error (OPTIMIZED_OUT_ERROR,
1232 _("Register %d was not saved"), regnum);
1233 if (unavailable)
1234 throw_error (NOT_AVAILABLE_ERROR,
1235 _("Register %d is not available"), regnum);
1238 void
1239 get_frame_register (const frame_info_ptr &frame,
1240 int regnum, gdb_byte *buf)
1242 frame_unwind_register (frame_info_ptr (frame->next), regnum, buf);
1245 struct value *
1246 frame_unwind_register_value (const frame_info_ptr &next_frame, int regnum)
1248 FRAME_SCOPED_DEBUG_ENTER_EXIT;
1250 gdb_assert (next_frame != NULL);
1251 gdbarch *gdbarch = frame_unwind_arch (next_frame);
1252 frame_debug_printf ("frame=%d, regnum=%d(%s)",
1253 next_frame->level, regnum,
1254 user_reg_map_regnum_to_name (gdbarch, regnum));
1256 /* Find the unwinder. */
1257 if (next_frame->unwind == NULL)
1258 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
1260 /* Ask this frame to unwind its register. */
1261 value *value
1262 = next_frame->unwind->prev_register (next_frame,
1263 &next_frame->prologue_cache, regnum);
1264 if (value == nullptr)
1266 if (gdbarch_pseudo_register_read_value_p (gdbarch))
1268 /* This is a pseudo register, we don't know how how what raw registers
1269 this pseudo register is made of. Ask the gdbarch to read the
1270 value, it will itself ask the next frame to unwind the values of
1271 the raw registers it needs to compose the value of the pseudo
1272 register. */
1273 value = gdbarch_pseudo_register_read_value
1274 (gdbarch, next_frame, regnum);
1276 else if (gdbarch_pseudo_register_read_p (gdbarch))
1278 value = value::allocate_register (next_frame, regnum);
1280 /* Passing the current regcache is known to be broken, the pseudo
1281 register value will be constructed using the current raw registers,
1282 rather than reading them using NEXT_FRAME. Architectures should be
1283 migrated to gdbarch_pseudo_register_read_value. */
1284 register_status status = gdbarch_pseudo_register_read
1285 (gdbarch, get_thread_regcache (inferior_thread ()), regnum,
1286 value->contents_writeable ().data ());
1287 if (status == REG_UNAVAILABLE)
1288 value->mark_bytes_unavailable (0, value->type ()->length ());
1290 else
1291 error (_("Can't unwind value of register %d (%s)"), regnum,
1292 user_reg_map_regnum_to_name (gdbarch, regnum));
1295 if (frame_debug)
1297 string_file debug_file;
1299 gdb_printf (&debug_file, " ->");
1300 if (value->optimized_out ())
1302 gdb_printf (&debug_file, " ");
1303 val_print_not_saved (&debug_file);
1305 else
1307 if (value->lval () == lval_register)
1308 gdb_printf (&debug_file, " register=%d", value->regnum ());
1309 else if (value->lval () == lval_memory)
1310 gdb_printf (&debug_file, " address=%s",
1311 paddress (gdbarch,
1312 value->address ()));
1313 else
1314 gdb_printf (&debug_file, " computed");
1316 if (value->lazy ())
1317 gdb_printf (&debug_file, " lazy");
1318 else if (!value->entirely_available ())
1319 gdb_printf (&debug_file, " unavailable");
1320 else
1322 int i;
1323 gdb::array_view<const gdb_byte> buf = value->contents ();
1325 gdb_printf (&debug_file, " bytes=");
1326 gdb_printf (&debug_file, "[");
1327 for (i = 0; i < register_size (gdbarch, regnum); i++)
1328 gdb_printf (&debug_file, "%02x", buf[i]);
1329 gdb_printf (&debug_file, "]");
1333 frame_debug_printf ("%s", debug_file.c_str ());
1336 return value;
1339 struct value *
1340 get_frame_register_value (const frame_info_ptr &frame, int regnum)
1342 return frame_unwind_register_value (frame_info_ptr (frame->next), regnum);
1345 LONGEST
1346 frame_unwind_register_signed (const frame_info_ptr &next_frame, int regnum)
1348 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1349 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1350 struct value *value = frame_unwind_register_value (next_frame, regnum);
1352 gdb_assert (value != NULL);
1354 if (value->optimized_out ())
1356 throw_error (OPTIMIZED_OUT_ERROR,
1357 _("Register %d was not saved"), regnum);
1359 if (!value->entirely_available ())
1361 throw_error (NOT_AVAILABLE_ERROR,
1362 _("Register %d is not available"), regnum);
1365 LONGEST r = extract_signed_integer (value->contents_all (), byte_order);
1367 release_value (value);
1368 return r;
1371 LONGEST
1372 get_frame_register_signed (const frame_info_ptr &frame, int regnum)
1374 return frame_unwind_register_signed (frame_info_ptr (frame->next), regnum);
1377 ULONGEST
1378 frame_unwind_register_unsigned (const frame_info_ptr &next_frame, int regnum)
1380 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1381 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1382 int size = register_size (gdbarch, regnum);
1383 struct value *value = frame_unwind_register_value (next_frame, regnum);
1385 gdb_assert (value != NULL);
1387 if (value->optimized_out ())
1389 throw_error (OPTIMIZED_OUT_ERROR,
1390 _("Register %d was not saved"), regnum);
1392 if (!value->entirely_available ())
1394 throw_error (NOT_AVAILABLE_ERROR,
1395 _("Register %d is not available"), regnum);
1398 ULONGEST r = extract_unsigned_integer (value->contents_all ().data (),
1399 size, byte_order);
1401 release_value (value);
1402 return r;
1405 ULONGEST
1406 get_frame_register_unsigned (const frame_info_ptr &frame, int regnum)
1408 return frame_unwind_register_unsigned (frame_info_ptr (frame->next), regnum);
1411 bool
1412 read_frame_register_unsigned (const frame_info_ptr &frame, int regnum,
1413 ULONGEST *val)
1415 struct value *regval = get_frame_register_value (frame, regnum);
1417 if (!regval->optimized_out ()
1418 && regval->entirely_available ())
1420 struct gdbarch *gdbarch = get_frame_arch (frame);
1421 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1422 int size = register_size (gdbarch, regval->regnum ());
1424 *val = extract_unsigned_integer (regval->contents ().data (), size,
1425 byte_order);
1426 return true;
1429 return false;
1432 void
1433 put_frame_register (const frame_info_ptr &next_frame, int regnum,
1434 gdb::array_view<const gdb_byte> buf)
1436 gdbarch *gdbarch = frame_unwind_arch (next_frame);
1437 int realnum;
1438 int optim;
1439 int unavail;
1440 enum lval_type lval;
1441 CORE_ADDR addr;
1442 int size = register_size (gdbarch, regnum);
1444 gdb_assert (buf.size () == size);
1446 frame_register_unwind (next_frame, regnum, &optim, &unavail, &lval, &addr,
1447 &realnum, nullptr);
1448 if (optim)
1449 error (_("Attempt to assign to a register that was not saved."));
1450 switch (lval)
1452 case lval_memory:
1454 write_memory (addr, buf.data (), size);
1455 break;
1457 case lval_register:
1458 /* Not sure if that's always true... but we have a problem if not. */
1459 gdb_assert (size == register_size (gdbarch, realnum));
1461 if (realnum < gdbarch_num_regs (gdbarch)
1462 || !gdbarch_pseudo_register_write_p (gdbarch))
1463 get_thread_regcache (inferior_thread ())->cooked_write (realnum, buf);
1464 else
1465 gdbarch_pseudo_register_write (gdbarch, next_frame, realnum, buf);
1466 break;
1467 default:
1468 error (_("Attempt to assign to an unmodifiable value."));
1472 /* This function is deprecated. Use get_frame_register_value instead,
1473 which provides more accurate information.
1475 Find and return the value of REGNUM for the specified stack frame.
1476 The number of bytes copied is REGISTER_SIZE (REGNUM).
1478 Returns 0 if the register value could not be found. */
1480 bool
1481 deprecated_frame_register_read (const frame_info_ptr &frame, int regnum,
1482 gdb_byte *myaddr)
1484 int optimized;
1485 int unavailable;
1486 enum lval_type lval;
1487 CORE_ADDR addr;
1488 int realnum;
1490 frame_register_unwind (get_next_frame_sentinel_okay (frame), regnum,
1491 &optimized, &unavailable, &lval, &addr, &realnum,
1492 myaddr);
1494 return !optimized && !unavailable;
1497 bool
1498 get_frame_register_bytes (const frame_info_ptr &next_frame, int regnum,
1499 CORE_ADDR offset, gdb::array_view<gdb_byte> buffer,
1500 int *optimizedp, int *unavailablep)
1502 gdbarch *gdbarch = frame_unwind_arch (next_frame);
1504 /* Skip registers wholly inside of OFFSET. */
1505 while (offset >= register_size (gdbarch, regnum))
1507 offset -= register_size (gdbarch, regnum);
1508 regnum++;
1511 /* Ensure that we will not read beyond the end of the register file.
1512 This can only ever happen if the debug information is bad. */
1513 int maxsize = -offset;
1514 int numregs = gdbarch_num_cooked_regs (gdbarch);
1515 for (int i = regnum; i < numregs; i++)
1517 int thissize = register_size (gdbarch, i);
1519 if (thissize == 0)
1520 break; /* This register is not available on this architecture. */
1521 maxsize += thissize;
1524 if (buffer.size () > maxsize)
1525 error (_("Bad debug information detected: "
1526 "Attempt to read %zu bytes from registers."), buffer.size ());
1528 /* Copy the data. */
1529 while (!buffer.empty ())
1531 int curr_len = std::min<int> (register_size (gdbarch, regnum) - offset,
1532 buffer.size ());
1534 if (curr_len == register_size (gdbarch, regnum))
1536 enum lval_type lval;
1537 CORE_ADDR addr;
1538 int realnum;
1540 frame_register_unwind (next_frame, regnum, optimizedp, unavailablep,
1541 &lval, &addr, &realnum, buffer.data ());
1542 if (*optimizedp || *unavailablep)
1543 return false;
1545 else
1547 value *value = frame_unwind_register_value (next_frame, regnum);
1548 gdb_assert (value != NULL);
1549 *optimizedp = value->optimized_out ();
1550 *unavailablep = !value->entirely_available ();
1552 if (*optimizedp || *unavailablep)
1554 release_value (value);
1555 return false;
1558 copy (value->contents_all ().slice (offset, curr_len),
1559 buffer.slice (0, curr_len));
1560 release_value (value);
1563 buffer = buffer.slice (curr_len);
1564 offset = 0;
1565 regnum++;
1568 *optimizedp = 0;
1569 *unavailablep = 0;
1571 return true;
1574 void
1575 put_frame_register_bytes (const frame_info_ptr &next_frame, int regnum,
1576 CORE_ADDR offset,
1577 gdb::array_view<const gdb_byte> buffer)
1579 gdbarch *gdbarch = frame_unwind_arch (next_frame);
1581 /* Skip registers wholly inside of OFFSET. */
1582 while (offset >= register_size (gdbarch, regnum))
1584 offset -= register_size (gdbarch, regnum);
1585 regnum++;
1588 /* Copy the data. */
1589 while (!buffer.empty ())
1591 int curr_len = std::min<int> (register_size (gdbarch, regnum) - offset,
1592 buffer.size ());
1594 if (curr_len == register_size (gdbarch, regnum))
1595 put_frame_register (next_frame, regnum, buffer.slice (0, curr_len));
1596 else
1598 value *value = frame_unwind_register_value (next_frame, regnum);
1599 gdb_assert (value != nullptr);
1601 copy (buffer.slice (0, curr_len),
1602 value->contents_writeable ().slice (offset, curr_len));
1603 put_frame_register (next_frame, regnum, value->contents_raw ());
1604 release_value (value);
1607 buffer = buffer.slice (curr_len);
1608 offset = 0;
1609 regnum++;
1613 /* Create a sentinel frame.
1615 See frame_id_build_sentinel for the description of STACK_ADDR and
1616 CODE_ADDR. */
1618 static frame_info_ptr
1619 create_sentinel_frame (program_space *pspace, address_space *aspace,
1620 regcache *regcache, CORE_ADDR stack_addr,
1621 CORE_ADDR code_addr)
1623 frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1625 frame->level = -1;
1626 frame->pspace = pspace;
1627 frame->aspace = aspace;
1628 /* Explicitly initialize the sentinel frame's cache. Provide it
1629 with the underlying regcache. In the future additional
1630 information, such as the frame's thread will be added. */
1631 frame->prologue_cache = sentinel_frame_cache (regcache);
1632 /* For the moment there is only one sentinel frame implementation. */
1633 frame->unwind = &sentinel_frame_unwind;
1634 /* Link this frame back to itself. The frame is self referential
1635 (the unwound PC is the same as the pc), so make it so. */
1636 frame->next = frame;
1637 /* The sentinel frame has a special ID. */
1638 frame->this_id.p = frame_id_status::COMPUTED;
1639 frame->this_id.value = frame_id_build_sentinel (stack_addr, code_addr);
1641 bool added = frame_stash_add (frame);
1642 gdb_assert (added);
1644 frame_debug_printf (" -> %s", frame->to_string ().c_str ());
1646 return frame_info_ptr (frame);
1649 /* Cache for frame addresses already read by gdb. Valid only while
1650 inferior is stopped. Control variables for the frame cache should
1651 be local to this module. */
1653 static struct obstack frame_cache_obstack;
1655 void *
1656 frame_obstack_zalloc (unsigned long size)
1658 void *data = obstack_alloc (&frame_cache_obstack, size);
1660 memset (data, 0, size);
1661 return data;
1664 static frame_info_ptr get_prev_frame_always_1 (const frame_info_ptr &this_frame);
1666 frame_info_ptr
1667 get_current_frame (void)
1669 frame_info_ptr current_frame;
1671 /* First check, and report, the lack of registers. Having GDB
1672 report "No stack!" or "No memory" when the target doesn't even
1673 have registers is very confusing. Besides, "printcmd.exp"
1674 explicitly checks that ``print $pc'' with no registers prints "No
1675 registers". */
1676 if (!target_has_registers ())
1677 error (_("No registers."));
1678 if (!target_has_stack ())
1679 error (_("No stack."));
1680 if (!target_has_memory ())
1681 error (_("No memory."));
1682 /* Traceframes are effectively a substitute for the live inferior. */
1683 if (get_traceframe_number () < 0)
1684 validate_registers_access ();
1686 if (sentinel_frame == NULL)
1687 sentinel_frame =
1688 create_sentinel_frame (current_program_space,
1689 current_inferior ()->aspace.get (),
1690 get_thread_regcache (inferior_thread ()),
1691 0, 0).get ();
1693 /* Set the current frame before computing the frame id, to avoid
1694 recursion inside compute_frame_id, in case the frame's
1695 unwinder decides to do a symbol lookup (which depends on the
1696 selected frame's block).
1698 This call must always succeed. In particular, nothing inside
1699 get_prev_frame_always_1 should try to unwind from the
1700 sentinel frame, because that could fail/throw, and we always
1701 want to leave with the current frame created and linked in --
1702 we should never end up with the sentinel frame as outermost
1703 frame. */
1704 current_frame = get_prev_frame_always_1 (frame_info_ptr (sentinel_frame));
1705 gdb_assert (current_frame != NULL);
1707 return current_frame;
1710 /* The "selected" stack frame is used by default for local and arg
1711 access.
1713 The "single source of truth" for the selected frame is the
1714 SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL pair.
1716 Frame IDs can be saved/restored across reinitializing the frame
1717 cache, while frame_info pointers can't (frame_info objects are
1718 invalidated). If we know the corresponding frame_info object, it
1719 is cached in SELECTED_FRAME.
1721 If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
1722 and the target has stack and is stopped, the selected frame is the
1723 current (innermost) target frame. SELECTED_FRAME_ID is never the ID
1724 of the current (innermost) target frame. SELECTED_FRAME_LEVEL may
1725 only be 0 if the selected frame is a user-created one (created and
1726 selected through the "select-frame view" command), in which case
1727 SELECTED_FRAME_ID is the frame id derived from the user-provided
1728 addresses.
1730 If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
1731 and the target has no stack or is executing, then there's no
1732 selected frame. */
1733 static frame_id selected_frame_id = null_frame_id;
1734 static int selected_frame_level = -1;
1736 /* See frame.h. This definition should come before any definition of a static
1737 frame_info_ptr, to ensure that frame_list is destroyed after any static
1738 frame_info_ptr. This is necessary because the destructor of frame_info_ptr
1739 uses frame_list. */
1741 intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
1743 /* The cached frame_info object pointing to the selected frame.
1744 Looked up on demand by get_selected_frame. */
1745 static frame_info_ptr selected_frame;
1747 /* See frame.h. */
1749 void
1750 save_selected_frame (frame_id *frame_id, int *frame_level)
1751 noexcept
1753 *frame_id = selected_frame_id;
1754 *frame_level = selected_frame_level;
1757 /* See frame.h. */
1759 void
1760 restore_selected_frame (frame_id frame_id, int frame_level)
1761 noexcept
1763 /* Unless it is a user-created frame, save_selected_frame never returns
1764 level == 0, so we shouldn't see it here either. */
1765 gdb_assert (frame_level != 0 || frame_id.user_created_p);
1767 /* FRAME_ID can be null_frame_id only IFF frame_level is -1. */
1768 gdb_assert ((frame_level == -1 && !frame_id_p (frame_id))
1769 || (frame_level != -1 && frame_id_p (frame_id)));
1771 selected_frame_id = frame_id;
1772 selected_frame_level = frame_level;
1774 /* Will be looked up later by get_selected_frame. */
1775 selected_frame = nullptr;
1778 /* Lookup the frame_info object for the selected frame FRAME_ID /
1779 FRAME_LEVEL and cache the result.
1781 If FRAME_LEVEL > 0 and the originally selected frame isn't found,
1782 warn and select the innermost (current) frame. */
1784 static void
1785 lookup_selected_frame (struct frame_id a_frame_id, int frame_level)
1787 frame_info_ptr frame = NULL;
1788 int count;
1790 /* This either means there was no selected frame, or the selected
1791 frame was the current frame. In either case, select the current
1792 frame. */
1793 if (frame_level == -1)
1795 select_frame (get_current_frame ());
1796 return;
1799 /* This means the selected frame was a user-created one. Create a new one
1800 using the user-provided addresses, which happen to be in the frame id. */
1801 if (frame_level == 0)
1803 gdb_assert (a_frame_id.user_created_p);
1804 select_frame (create_new_frame (a_frame_id));
1805 return;
1808 /* select_frame never saves 0 in SELECTED_FRAME_LEVEL, so we
1809 shouldn't see it here. */
1810 gdb_assert (frame_level > 0);
1812 /* Restore by level first, check if the frame id is the same as
1813 expected. If that fails, try restoring by frame id. If that
1814 fails, nothing to do, just warn the user. */
1816 count = frame_level;
1817 frame = find_relative_frame (get_current_frame (), &count);
1818 if (count == 0
1819 && frame != NULL
1820 /* The frame ids must match - either both valid or both
1821 outer_frame_id. The latter case is not failsafe, but since
1822 it's highly unlikely the search by level finds the wrong
1823 frame, it's 99.9(9)% of the time (for all practical purposes)
1824 safe. */
1825 && get_frame_id (frame) == a_frame_id)
1827 /* Cool, all is fine. */
1828 select_frame (frame);
1829 return;
1832 frame = frame_find_by_id (a_frame_id);
1833 if (frame != NULL)
1835 /* Cool, refound it. */
1836 select_frame (frame);
1837 return;
1840 /* Nothing else to do, the frame layout really changed. Select the
1841 innermost stack frame. */
1842 select_frame (get_current_frame ());
1844 /* Warn the user. */
1845 if (frame_level > 0 && !current_uiout->is_mi_like_p ())
1847 warning (_("Couldn't restore frame #%d in "
1848 "current thread. Bottom (innermost) frame selected:"),
1849 frame_level);
1850 /* For MI, we should probably have a notification about current
1851 frame change. But this error is not very likely, so don't
1852 bother for now. */
1853 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1857 bool
1858 has_stack_frames ()
1860 if (!target_has_registers () || !target_has_stack ()
1861 || !target_has_memory ())
1862 return false;
1864 /* Traceframes are effectively a substitute for the live inferior. */
1865 if (get_traceframe_number () < 0)
1867 /* No current inferior, no frame. */
1868 if (inferior_ptid == null_ptid)
1869 return false;
1871 thread_info *tp = inferior_thread ();
1872 /* Don't try to read from a dead thread. */
1873 if (tp->state == THREAD_EXITED)
1874 return false;
1876 /* ... or from a spinning thread. */
1877 if (tp->executing ())
1878 return false;
1881 return true;
1884 /* See frame.h. */
1886 frame_info_ptr
1887 get_selected_frame (const char *message)
1889 if (selected_frame == NULL)
1891 if (message != NULL && !has_stack_frames ())
1892 error (("%s"), message);
1894 lookup_selected_frame (selected_frame_id, selected_frame_level);
1896 /* There is always a frame. */
1897 gdb_assert (selected_frame != NULL);
1898 return selected_frame;
1901 /* This is a variant of get_selected_frame() which can be called when
1902 the inferior does not have a frame; in that case it will return
1903 NULL instead of calling error(). */
1905 frame_info_ptr
1906 deprecated_safe_get_selected_frame (void)
1908 if (!has_stack_frames ())
1909 return NULL;
1910 return get_selected_frame (NULL);
1913 /* Invalidate the selected frame. */
1915 static void
1916 invalidate_selected_frame ()
1918 selected_frame = nullptr;
1919 selected_frame_level = -1;
1920 selected_frame_id = null_frame_id;
1923 /* See frame.h. */
1925 void
1926 select_frame (const frame_info_ptr &fi)
1928 gdb_assert (fi != nullptr);
1930 selected_frame = fi;
1931 selected_frame_level = frame_relative_level (fi);
1933 /* If the frame is a user-created one, save its level and frame id just like
1934 any other non-level-0 frame. */
1935 if (selected_frame_level == 0 && !fi->this_id.value.user_created_p)
1937 /* Treat the current frame especially -- we want to always
1938 save/restore it without warning, even if the frame ID changes
1939 (see lookup_selected_frame). E.g.:
1941 // The current frame is selected, the target had just stopped.
1943 scoped_restore_selected_frame restore_frame;
1944 some_operation_that_changes_the_stack ();
1946 // scoped_restore_selected_frame's dtor runs, but the
1947 // original frame_id can't be found. No matter whether it
1948 // is found or not, we still end up with the now-current
1949 // frame selected. Warning in lookup_selected_frame in this
1950 // case seems pointless.
1952 Also get_frame_id may access the target's registers/memory,
1953 and thus skipping get_frame_id optimizes the common case.
1955 Saving the selected frame this way makes get_selected_frame
1956 and restore_current_frame return/re-select whatever frame is
1957 the innermost (current) then. */
1958 selected_frame_level = -1;
1959 selected_frame_id = null_frame_id;
1961 else
1962 selected_frame_id = get_frame_id (fi);
1964 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1965 frame is being invalidated. */
1967 /* FIXME: kseitz/2002-08-28: It would be nice to call
1968 selected_frame_level_changed_event() right here, but due to limitations
1969 in the current interfaces, we would end up flooding UIs with events
1970 because select_frame() is used extensively internally.
1972 Once we have frame-parameterized frame (and frame-related) commands,
1973 the event notification can be moved here, since this function will only
1974 be called when the user's selected frame is being changed. */
1976 /* Ensure that symbols for this frame are read in. Also, determine the
1977 source language of this frame, and switch to it if desired. */
1978 if (fi)
1980 CORE_ADDR pc;
1982 /* We retrieve the frame's symtab by using the frame PC.
1983 However we cannot use the frame PC as-is, because it usually
1984 points to the instruction following the "call", which is
1985 sometimes the first instruction of another function. So we
1986 rely on get_frame_address_in_block() which provides us with a
1987 PC which is guaranteed to be inside the frame's code
1988 block. */
1989 if (get_frame_address_in_block_if_available (fi, &pc))
1991 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
1993 if (cust != NULL
1994 && cust->language () != current_language->la_language
1995 && cust->language () != language_unknown
1996 && language_mode == language_mode_auto)
1997 set_language (cust->language ());
2002 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
2003 Always returns a non-NULL value. */
2005 static frame_info_ptr
2006 create_new_frame (frame_id id)
2008 gdb_assert (id.user_created_p);
2009 gdb_assert (id.stack_status == frame_id_stack_status::FID_STACK_VALID);
2010 gdb_assert (id.code_addr_p);
2012 frame_debug_printf ("stack_addr=%s, core_addr=%s",
2013 hex_string (id.stack_addr), hex_string (id.code_addr));
2015 /* Avoid creating duplicate frames, search for an existing frame with that id
2016 in the stash. */
2017 frame_info_ptr frame = frame_stash_find (id);
2018 if (frame != nullptr)
2019 return frame;
2021 frame_info *fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
2023 fi->next = create_sentinel_frame (current_program_space,
2024 current_inferior ()->aspace.get (),
2025 get_thread_regcache (inferior_thread ()),
2026 id.stack_addr, id.code_addr).get ();
2028 /* Set/update this frame's cached PC value, found in the next frame.
2029 Do this before looking for this frame's unwinder. A sniffer is
2030 very likely to read this, and the corresponding unwinder is
2031 entitled to rely that the PC doesn't magically change. */
2032 fi->next->prev_pc.value = id.code_addr;
2033 fi->next->prev_pc.status = CC_VALUE;
2035 /* We currently assume that frame chain's can't cross spaces. */
2036 fi->pspace = fi->next->pspace;
2037 fi->aspace = fi->next->aspace;
2039 /* Select/initialize both the unwind function and the frame's type
2040 based on the PC. */
2041 frame_unwind_find_by_frame (frame_info_ptr (fi), &fi->prologue_cache);
2043 fi->this_id.p = frame_id_status::COMPUTED;
2044 fi->this_id.value = id;
2046 bool added = frame_stash_add (fi);
2047 gdb_assert (added);
2049 frame_debug_printf (" -> %s", fi->to_string ().c_str ());
2051 return frame_info_ptr (fi);
2054 frame_info_ptr
2055 create_new_frame (CORE_ADDR stack, CORE_ADDR pc)
2057 frame_id id = frame_id_build (stack, pc);
2058 id.user_created_p = 1;
2060 return create_new_frame (id);
2063 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
2064 innermost frame). Be careful to not fall off the bottom of the
2065 frame chain and onto the sentinel frame. */
2067 frame_info_ptr
2068 get_next_frame (const frame_info_ptr &this_frame)
2070 if (this_frame->level > 0)
2071 return frame_info_ptr (this_frame->next);
2072 else
2073 return NULL;
2076 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
2077 innermost (i.e. current) frame, return the sentinel frame. Thus,
2078 unlike get_next_frame(), NULL will never be returned. */
2080 frame_info_ptr
2081 get_next_frame_sentinel_okay (const frame_info_ptr &this_frame)
2083 gdb_assert (this_frame != NULL);
2085 /* Note that, due to the manner in which the sentinel frame is
2086 constructed, this_frame->next still works even when this_frame
2087 is the sentinel frame. But we disallow it here anyway because
2088 calling get_next_frame_sentinel_okay() on the sentinel frame
2089 is likely a coding error. */
2090 if (this_frame->this_id.p == frame_id_status::COMPUTED)
2091 gdb_assert (!is_sentinel_frame_id (this_frame->this_id.value));
2093 return frame_info_ptr (this_frame->next);
2096 /* Observer for the target_changed event. */
2098 static void
2099 frame_observer_target_changed (struct target_ops *target)
2101 reinit_frame_cache ();
2104 /* Flush the entire frame cache. */
2106 void
2107 reinit_frame_cache (void)
2109 ++frame_cache_generation;
2111 if (htab_elements (frame_stash) > 0)
2112 annotate_frames_invalid ();
2114 invalidate_selected_frame ();
2116 /* Invalidate cache. */
2117 if (sentinel_frame != nullptr)
2119 /* If frame 0's id is not computed, it is not in the frame stash, so its
2120 dealloc functions will not be called when emptying the frame stash.
2121 Call frame_info_del manually in that case. */
2122 frame_info *current_frame = sentinel_frame->prev;
2123 if (current_frame != nullptr
2124 && current_frame->this_id.p == frame_id_status::NOT_COMPUTED)
2125 frame_info_del (current_frame);
2127 sentinel_frame = nullptr;
2130 frame_stash_invalidate ();
2132 /* Since we can't really be sure what the first object allocated was. */
2133 obstack_free (&frame_cache_obstack, 0);
2134 obstack_init (&frame_cache_obstack);
2136 for (frame_info_ptr &iter : frame_info_ptr::frame_list)
2137 iter.invalidate ();
2139 frame_debug_printf ("generation=%d", frame_cache_generation);
2142 /* Find where a register is saved (in memory or another register).
2143 The result of frame_register_unwind is just where it is saved
2144 relative to this particular frame. */
2146 static void
2147 frame_register_unwind_location (const frame_info_ptr &initial_this_frame,
2148 int regnum, int *optimizedp, lval_type *lvalp,
2149 CORE_ADDR *addrp, int *realnump)
2151 gdb_assert (initial_this_frame == nullptr || initial_this_frame->level >= 0);
2153 frame_info_ptr this_frame = initial_this_frame;
2154 while (this_frame != NULL)
2156 int unavailable;
2158 frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
2159 lvalp, addrp, realnump, NULL);
2161 if (*optimizedp)
2162 break;
2164 if (*lvalp != lval_register)
2165 break;
2167 regnum = *realnump;
2168 this_frame = get_next_frame (this_frame);
2172 /* Get the previous raw frame, and check that it is not identical to
2173 same other frame frame already in the chain. If it is, there is
2174 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
2175 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
2176 validity tests, that compare THIS_FRAME and the next frame, we do
2177 this right after creating the previous frame, to avoid ever ending
2178 up with two frames with the same id in the frame chain.
2180 There is however, one case where this cycle detection is not desirable,
2181 when asking for the previous frame of an inline frame, in this case, if
2182 the previous frame is a duplicate and we return nullptr then we will be
2183 unable to calculate the frame_id of the inline frame, this in turn
2184 causes inline_frame_this_id() to fail. So for inline frames (and only
2185 for inline frames), the previous frame will always be returned, even when it
2186 has a duplicate frame_id. We're not worried about cycles in the frame
2187 chain as, if the previous frame returned here has a duplicate frame_id,
2188 then the frame_id of the inline frame, calculated based off the frame_id
2189 of the previous frame, should also be a duplicate. */
2191 static frame_info_ptr
2192 get_prev_frame_maybe_check_cycle (const frame_info_ptr &this_frame)
2194 frame_info_ptr prev_frame = get_prev_frame_raw (this_frame);
2196 /* Don't compute the frame id of the current frame yet. Unwinding
2197 the sentinel frame can fail (e.g., if the thread is gone and we
2198 can't thus read its registers). If we let the cycle detection
2199 code below try to compute a frame ID, then an error thrown from
2200 within the frame ID computation would result in the sentinel
2201 frame as outermost frame, which is bogus. Instead, we'll compute
2202 the current frame's ID lazily in get_frame_id. Note that there's
2203 no point in doing cycle detection when there's only one frame, so
2204 nothing is lost here. */
2205 if (prev_frame->level == 0)
2206 return prev_frame;
2208 unsigned int entry_generation = get_frame_cache_generation ();
2212 compute_frame_id (prev_frame);
2214 bool cycle_detection_p = get_frame_type (this_frame) != INLINE_FRAME;
2216 /* This assert checks GDB's state with respect to calculating the
2217 frame-id of THIS_FRAME, in the case where THIS_FRAME is an inline
2218 frame.
2220 If THIS_FRAME is frame #0, and is an inline frame, then we put off
2221 calculating the frame_id until we specifically make a call to
2222 get_frame_id(). As a result we can enter this function in two
2223 possible states. If GDB asked for the previous frame of frame #0
2224 then THIS_FRAME will be frame #0 (an inline frame), and the
2225 frame_id will be in the NOT_COMPUTED state. However, if GDB asked
2226 for the frame_id of frame #0, then, as getting the frame_id of an
2227 inline frame requires us to get the frame_id of the previous
2228 frame, we will still end up in here, and the frame_id status will
2229 be COMPUTING.
2231 If, instead, THIS_FRAME is at a level greater than #0 then things
2232 are simpler. For these frames we immediately compute the frame_id
2233 when the frame is initially created, and so, for those frames, we
2234 will always enter this function with the frame_id status of
2235 COMPUTING. */
2236 gdb_assert (cycle_detection_p
2237 || (this_frame->level > 0
2238 && (this_frame->this_id.p
2239 == frame_id_status::COMPUTING))
2240 || (this_frame->level == 0
2241 && (this_frame->this_id.p
2242 != frame_id_status::COMPUTED)));
2244 /* We must do the CYCLE_DETECTION_P check after attempting to add
2245 PREV_FRAME into the cache; if PREV_FRAME is unique then we do want
2246 it in the cache, but if it is a duplicate and CYCLE_DETECTION_P is
2247 false, then we don't want to unlink it. */
2248 if (!frame_stash_add (prev_frame.get ()) && cycle_detection_p)
2250 /* Another frame with the same id was already in the stash. We just
2251 detected a cycle. */
2252 frame_debug_printf (" -> nullptr // this frame has same ID");
2254 this_frame->stop_reason = UNWIND_SAME_ID;
2255 /* Unlink. */
2256 prev_frame->next = NULL;
2257 this_frame->prev = NULL;
2258 prev_frame = NULL;
2261 catch (const gdb_exception &ex)
2263 if (get_frame_cache_generation () == entry_generation)
2265 prev_frame->next = NULL;
2266 this_frame->prev = NULL;
2269 throw;
2272 return prev_frame;
2275 /* Helper function for get_prev_frame_always, this is called inside a
2276 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
2277 there is no such frame. This may throw an exception. */
2279 static frame_info_ptr
2280 get_prev_frame_always_1 (const frame_info_ptr &this_frame)
2282 FRAME_SCOPED_DEBUG_ENTER_EXIT;
2284 gdb_assert (this_frame != NULL);
2286 if (frame_debug)
2288 if (this_frame != NULL)
2289 frame_debug_printf ("this_frame=%d", this_frame->level);
2290 else
2291 frame_debug_printf ("this_frame=nullptr");
2294 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2296 /* Only try to do the unwind once. */
2297 if (this_frame->prev_p)
2299 if (this_frame->prev != nullptr)
2300 frame_debug_printf (" -> %s // cached",
2301 this_frame->prev->to_string ().c_str ());
2302 else
2303 frame_debug_printf
2304 (" -> nullptr // %s // cached",
2305 frame_stop_reason_symbol_string (this_frame->stop_reason));
2306 return frame_info_ptr (this_frame->prev);
2309 /* If the frame unwinder hasn't been selected yet, we must do so
2310 before setting prev_p; otherwise the check for misbehaved
2311 sniffers will think that this frame's sniffer tried to unwind
2312 further (see frame_cleanup_after_sniffer). */
2313 if (this_frame->unwind == NULL)
2314 frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
2316 this_frame->prev_p = true;
2317 this_frame->stop_reason = UNWIND_NO_REASON;
2319 /* If we are unwinding from an inline frame, all of the below tests
2320 were already performed when we unwound from the next non-inline
2321 frame. We must skip them, since we can not get THIS_FRAME's ID
2322 until we have unwound all the way down to the previous non-inline
2323 frame. */
2324 if (get_frame_type (this_frame) == INLINE_FRAME)
2325 return get_prev_frame_maybe_check_cycle (this_frame);
2327 /* If this_frame is the current frame, then compute and stash its
2328 frame id prior to fetching and computing the frame id of the
2329 previous frame. Otherwise, the cycle detection code in
2330 get_prev_frame_if_no_cycle() will not work correctly. When
2331 get_frame_id() is called later on, an assertion error will be
2332 triggered in the event of a cycle between the current frame and
2333 its previous frame.
2335 Note we do this after the INLINE_FRAME check above. That is
2336 because the inline frame's frame id computation needs to fetch
2337 the frame id of its previous real stack frame. I.e., we need to
2338 avoid recursion in that case. This is OK since we're sure the
2339 inline frame won't create a cycle with the real stack frame. See
2340 inline_frame_this_id. */
2341 if (this_frame->level == 0)
2342 get_frame_id (this_frame);
2344 /* Check that this frame is unwindable. If it isn't, don't try to
2345 unwind to the prev frame. */
2346 this_frame->stop_reason
2347 = this_frame->unwind->stop_reason (this_frame,
2348 &this_frame->prologue_cache);
2350 if (this_frame->stop_reason != UNWIND_NO_REASON)
2352 frame_debug_printf
2353 (" -> nullptr // %s",
2354 frame_stop_reason_symbol_string (this_frame->stop_reason));
2355 return NULL;
2358 /* Check that this frame's ID isn't inner to (younger, below, next)
2359 the next frame. This happens when a frame unwind goes backwards.
2360 This check is valid only if this frame and the next frame are NORMAL.
2361 See the comment at frame_id_inner for details. */
2362 if (get_frame_type (this_frame) == NORMAL_FRAME
2363 && this_frame->next->unwind->type == NORMAL_FRAME
2364 && frame_id_inner (get_frame_arch (frame_info_ptr (this_frame->next)),
2365 get_frame_id (this_frame),
2366 get_frame_id (frame_info_ptr (this_frame->next))))
2368 CORE_ADDR this_pc_in_block;
2369 struct minimal_symbol *morestack_msym;
2370 const char *morestack_name = NULL;
2372 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2373 this_pc_in_block = get_frame_address_in_block (this_frame);
2374 morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
2375 if (morestack_msym)
2376 morestack_name = morestack_msym->linkage_name ();
2377 if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
2379 frame_debug_printf (" -> nullptr // this frame ID is inner");
2380 this_frame->stop_reason = UNWIND_INNER_ID;
2381 return NULL;
2385 /* Check that this and the next frame do not unwind the PC register
2386 to the same memory location. If they do, then even though they
2387 have different frame IDs, the new frame will be bogus; two
2388 functions can't share a register save slot for the PC. This can
2389 happen when the prologue analyzer finds a stack adjustment, but
2390 no PC save.
2392 This check does assume that the "PC register" is roughly a
2393 traditional PC, even if the gdbarch_unwind_pc method adjusts
2394 it (we do not rely on the value, only on the unwound PC being
2395 dependent on this value). A potential improvement would be
2396 to have the frame prev_pc method and the gdbarch unwind_pc
2397 method set the same lval and location information as
2398 frame_register_unwind. */
2399 if (this_frame->level > 0
2400 && gdbarch_pc_regnum (gdbarch) >= 0
2401 && get_frame_type (this_frame) == NORMAL_FRAME
2402 && (get_frame_type (frame_info_ptr (this_frame->next)) == NORMAL_FRAME
2403 || get_frame_type (frame_info_ptr (this_frame->next)) == INLINE_FRAME))
2405 int optimized, realnum, nrealnum;
2406 enum lval_type lval, nlval;
2407 CORE_ADDR addr, naddr;
2409 frame_register_unwind_location (this_frame,
2410 gdbarch_pc_regnum (gdbarch),
2411 &optimized, &lval, &addr, &realnum);
2412 frame_register_unwind_location (get_next_frame (this_frame),
2413 gdbarch_pc_regnum (gdbarch),
2414 &optimized, &nlval, &naddr, &nrealnum);
2416 if ((lval == lval_memory && lval == nlval && addr == naddr)
2417 || (lval == lval_register && lval == nlval && realnum == nrealnum))
2419 frame_debug_printf (" -> nullptr // no saved PC");
2420 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
2421 this_frame->prev = NULL;
2422 return NULL;
2426 /* Ensure we can unwind the program counter of THIS_FRAME. */
2429 /* Calling frame_unwind_pc for the sentinel frame relies on the
2430 current_frame being set, which at this point it might not be if we
2431 are in the process of setting the current_frame after a stop (see
2432 get_current_frame).
2434 The point of this check is to ensure that the unwinder for
2435 THIS_FRAME can actually unwind the $pc, which we assume the
2436 sentinel frame unwinder can always do (it's just a read from the
2437 machine state), so we only call frame_unwind_pc for frames other
2438 than the sentinel (level -1) frame.
2440 Additionally, we don't actually care about the value of the
2441 unwound $pc, just that the call completed successfully. */
2442 if (this_frame->level >= 0)
2443 frame_unwind_pc (this_frame);
2445 catch (const gdb_exception_error &ex)
2447 if (ex.error == NOT_AVAILABLE_ERROR || ex.error == OPTIMIZED_OUT_ERROR)
2449 frame_debug_printf (" -> nullptr // no saved PC");
2450 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
2451 this_frame->prev = nullptr;
2452 return nullptr;
2455 throw;
2458 return get_prev_frame_maybe_check_cycle (this_frame);
2461 /* Return a "struct frame_info" corresponding to the frame that called
2462 THIS_FRAME. Returns NULL if there is no such frame.
2464 Unlike get_prev_frame, this function always tries to unwind the
2465 frame. */
2467 frame_info_ptr
2468 get_prev_frame_always (const frame_info_ptr &this_frame)
2470 frame_info_ptr prev_frame = NULL;
2474 prev_frame = get_prev_frame_always_1 (this_frame);
2476 catch (const gdb_exception_error &ex)
2478 if (ex.error == MEMORY_ERROR)
2480 this_frame->stop_reason = UNWIND_MEMORY_ERROR;
2481 if (ex.message != NULL)
2483 char *stop_string;
2484 size_t size;
2486 /* The error needs to live as long as the frame does.
2487 Allocate using stack local STOP_STRING then assign the
2488 pointer to the frame, this allows the STOP_STRING on the
2489 frame to be of type 'const char *'. */
2490 size = ex.message->size () + 1;
2491 stop_string = (char *) frame_obstack_zalloc (size);
2492 memcpy (stop_string, ex.what (), size);
2493 this_frame->stop_string = stop_string;
2495 prev_frame = NULL;
2497 else
2498 throw;
2501 return prev_frame;
2504 /* Construct a new "struct frame_info" and link it previous to
2505 this_frame. */
2507 static frame_info_ptr
2508 get_prev_frame_raw (const frame_info_ptr &this_frame)
2510 frame_info *prev_frame;
2512 /* Allocate the new frame but do not wire it in to the frame chain.
2513 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2514 frame->next to pull some fancy tricks (of course such code is, by
2515 definition, recursive). Try to prevent it.
2517 There is no reason to worry about memory leaks, should the
2518 remainder of the function fail. The allocated memory will be
2519 quickly reclaimed when the frame cache is flushed, and the `we've
2520 been here before' check above will stop repeated memory
2521 allocation calls. */
2522 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
2523 prev_frame->level = this_frame->level + 1;
2525 /* For now, assume we don't have frame chains crossing address
2526 spaces. */
2527 prev_frame->pspace = this_frame->pspace;
2528 prev_frame->aspace = this_frame->aspace;
2530 /* Don't yet compute ->unwind (and hence ->type). It is computed
2531 on-demand in get_frame_type, frame_register_unwind, and
2532 get_frame_id. */
2534 /* Don't yet compute the frame's ID. It is computed on-demand by
2535 get_frame_id(). */
2537 /* The unwound frame ID is validate at the start of this function,
2538 as part of the logic to decide if that frame should be further
2539 unwound, and not here while the prev frame is being created.
2540 Doing this makes it possible for the user to examine a frame that
2541 has an invalid frame ID.
2543 Some very old VAX code noted: [...] For the sake of argument,
2544 suppose that the stack is somewhat trashed (which is one reason
2545 that "info frame" exists). So, return 0 (indicating we don't
2546 know the address of the arglist) if we don't know what frame this
2547 frame calls. */
2549 /* Link it in. */
2550 this_frame->prev = prev_frame;
2551 prev_frame->next = this_frame.get ();
2553 frame_debug_printf (" -> %s", prev_frame->to_string ().c_str ());
2555 return frame_info_ptr (prev_frame);
2558 /* Debug routine to print a NULL frame being returned. */
2560 static void
2561 frame_debug_got_null_frame (const frame_info_ptr &this_frame,
2562 const char *reason)
2564 if (frame_debug)
2566 if (this_frame != NULL)
2567 frame_debug_printf ("this_frame=%d -> %s", this_frame->level, reason);
2568 else
2569 frame_debug_printf ("this_frame=nullptr -> %s", reason);
2573 /* Is this (non-sentinel) frame in the "main"() function? */
2575 static bool
2576 inside_main_func (const frame_info_ptr &this_frame)
2578 if (current_program_space->symfile_object_file == nullptr)
2579 return false;
2581 CORE_ADDR sym_addr = 0;
2582 const char *name = main_name ();
2583 bound_minimal_symbol msymbol
2584 = lookup_minimal_symbol (name, NULL,
2585 current_program_space->symfile_object_file);
2587 if (msymbol.minsym != nullptr)
2588 sym_addr = msymbol.value_address ();
2590 /* Favor a full symbol in Fortran, for the case where the Fortran main
2591 is also called "main". */
2592 if (msymbol.minsym == nullptr
2593 || get_frame_language (this_frame) == language_fortran)
2595 /* In some language (for example Fortran) there will be no minimal
2596 symbol with the name of the main function. In this case we should
2597 search the full symbols to see if we can find a match. */
2598 struct block_symbol bs = lookup_symbol (name, nullptr,
2599 SEARCH_FUNCTION_DOMAIN, nullptr);
2601 /* This lookup should always yield a block-valued symbol. */
2602 if (bs.symbol != nullptr && bs.symbol->aclass () == LOC_BLOCK)
2604 const struct block *block = bs.symbol->value_block ();
2605 gdb_assert (block != nullptr);
2606 sym_addr = block->start ();
2608 else if (msymbol.minsym == nullptr)
2609 return false;
2612 /* Convert any function descriptor addresses into the actual function
2613 code address. */
2614 sym_addr = (gdbarch_convert_from_func_ptr_addr
2615 (get_frame_arch (this_frame), sym_addr,
2616 current_inferior ()->top_target ()));
2618 return sym_addr == get_frame_func (this_frame);
2621 /* Test whether THIS_FRAME is inside the process entry point function. */
2623 static bool
2624 inside_entry_func (const frame_info_ptr &this_frame)
2626 CORE_ADDR entry_point;
2628 if (!entry_point_address_query (&entry_point))
2629 return false;
2631 return get_frame_func (this_frame) == entry_point;
2634 /* Return a structure containing various interesting information about
2635 the frame that called THIS_FRAME. Returns NULL if there is either
2636 no such frame or the frame fails any of a set of target-independent
2637 condition that should terminate the frame chain (e.g., as unwinding
2638 past main()).
2640 This function should not contain target-dependent tests, such as
2641 checking whether the program-counter is zero. */
2643 frame_info_ptr
2644 get_prev_frame (const frame_info_ptr &this_frame)
2646 FRAME_SCOPED_DEBUG_ENTER_EXIT;
2648 CORE_ADDR frame_pc;
2649 int frame_pc_p;
2651 /* There is always a frame. If this assertion fails, suspect that
2652 something should be calling get_selected_frame() or
2653 get_current_frame(). */
2654 gdb_assert (this_frame != NULL);
2656 frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
2658 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2659 sense to stop unwinding at a dummy frame. One place where a dummy
2660 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2661 pcsqh register (space register for the instruction at the head of the
2662 instruction queue) cannot be written directly; the only way to set it
2663 is to branch to code that is in the target space. In order to implement
2664 frame dummies on HPUX, the called function is made to jump back to where
2665 the inferior was when the user function was called. If gdb was inside
2666 the main function when we created the dummy frame, the dummy frame will
2667 point inside the main function. */
2668 if (this_frame->level >= 0
2669 && get_frame_type (this_frame) == NORMAL_FRAME
2670 && !user_set_backtrace_options.backtrace_past_main
2671 && frame_pc_p
2672 && inside_main_func (this_frame))
2673 /* Don't unwind past main(). Note, this is done _before_ the
2674 frame has been marked as previously unwound. That way if the
2675 user later decides to enable unwinds past main(), that will
2676 automatically happen. */
2678 frame_debug_got_null_frame (this_frame, "inside main func");
2679 return NULL;
2682 /* If the user's backtrace limit has been exceeded, stop. We must
2683 add two to the current level; one of those accounts for backtrace_limit
2684 being 1-based and the level being 0-based, and the other accounts for
2685 the level of the new frame instead of the level of the current
2686 frame. */
2687 if (this_frame->level + 2 > user_set_backtrace_options.backtrace_limit)
2689 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
2690 return NULL;
2693 /* If we're already inside the entry function for the main objfile,
2694 then it isn't valid. Don't apply this test to a dummy frame -
2695 dummy frame PCs typically land in the entry func. Don't apply
2696 this test to the sentinel frame. Sentinel frames should always
2697 be allowed to unwind. */
2698 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2699 wasn't checking for "main" in the minimal symbols. With that
2700 fixed asm-source tests now stop in "main" instead of halting the
2701 backtrace in weird and wonderful ways somewhere inside the entry
2702 file. Suspect that tests for inside the entry file/func were
2703 added to work around that (now fixed) case. */
2704 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2705 suggested having the inside_entry_func test use the
2706 inside_main_func() msymbol trick (along with entry_point_address()
2707 I guess) to determine the address range of the start function.
2708 That should provide a far better stopper than the current
2709 heuristics. */
2710 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2711 applied tail-call optimizations to main so that a function called
2712 from main returns directly to the caller of main. Since we don't
2713 stop at main, we should at least stop at the entry point of the
2714 application. */
2715 if (this_frame->level >= 0
2716 && get_frame_type (this_frame) == NORMAL_FRAME
2717 && !user_set_backtrace_options.backtrace_past_entry
2718 && frame_pc_p
2719 && inside_entry_func (this_frame))
2721 frame_debug_got_null_frame (this_frame, "inside entry func");
2722 return NULL;
2725 /* Assume that the only way to get a zero PC is through something
2726 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2727 will never unwind a zero PC. */
2728 if (this_frame->level > 0
2729 && (get_frame_type (this_frame) == NORMAL_FRAME
2730 || get_frame_type (this_frame) == INLINE_FRAME)
2731 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
2732 && frame_pc_p && frame_pc == 0)
2734 frame_debug_got_null_frame (this_frame, "zero PC");
2735 return NULL;
2738 return get_prev_frame_always (this_frame);
2741 CORE_ADDR
2742 get_frame_pc (const frame_info_ptr &frame)
2744 gdb_assert (frame->next != NULL);
2745 return frame_unwind_pc (frame_info_ptr (frame->next));
2748 bool
2749 get_frame_pc_if_available (const frame_info_ptr &frame, CORE_ADDR *pc)
2752 gdb_assert (frame->next != NULL);
2756 *pc = frame_unwind_pc (frame_info_ptr (frame->next));
2758 catch (const gdb_exception_error &ex)
2760 if (ex.error == NOT_AVAILABLE_ERROR)
2761 return false;
2762 else
2763 throw;
2766 return true;
2769 /* Return an address that falls within THIS_FRAME's code block. */
2771 CORE_ADDR
2772 get_frame_address_in_block (const frame_info_ptr &this_frame)
2774 /* A draft address. */
2775 CORE_ADDR pc = get_frame_pc (this_frame);
2777 frame_info_ptr next_frame (this_frame->next);
2779 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2780 Normally the resume address is inside the body of the function
2781 associated with THIS_FRAME, but there is a special case: when
2782 calling a function which the compiler knows will never return
2783 (for instance abort), the call may be the very last instruction
2784 in the calling function. The resume address will point after the
2785 call and may be at the beginning of a different function
2786 entirely.
2788 If THIS_FRAME is a signal frame or dummy frame, then we should
2789 not adjust the unwound PC. For a dummy frame, GDB pushed the
2790 resume address manually onto the stack. For a signal frame, the
2791 OS may have pushed the resume address manually and invoked the
2792 handler (e.g. GNU/Linux), or invoked the trampoline which called
2793 the signal handler - but in either case the signal handler is
2794 expected to return to the trampoline. So in both of these
2795 cases we know that the resume address is executable and
2796 related. So we only need to adjust the PC if THIS_FRAME
2797 is a normal function.
2799 If the program has been interrupted while THIS_FRAME is current,
2800 then clearly the resume address is inside the associated
2801 function. There are three kinds of interruption: debugger stop
2802 (next frame will be SENTINEL_FRAME), operating system
2803 signal or exception (next frame will be SIGTRAMP_FRAME),
2804 or debugger-induced function call (next frame will be
2805 DUMMY_FRAME). So we only need to adjust the PC if
2806 NEXT_FRAME is a normal function.
2808 We check the type of NEXT_FRAME first, since it is already
2809 known; frame type is determined by the unwinder, and since
2810 we have THIS_FRAME we've already selected an unwinder for
2811 NEXT_FRAME.
2813 If the next frame is inlined, we need to keep going until we find
2814 the real function - for instance, if a signal handler is invoked
2815 while in an inlined function, then the code address of the
2816 "calling" normal function should not be adjusted either. */
2818 while (get_frame_type (next_frame) == INLINE_FRAME)
2819 next_frame = frame_info_ptr (next_frame->next);
2821 if ((get_frame_type (next_frame) == NORMAL_FRAME
2822 || get_frame_type (next_frame) == TAILCALL_FRAME)
2823 && (get_frame_type (this_frame) == NORMAL_FRAME
2824 || get_frame_type (this_frame) == TAILCALL_FRAME
2825 || get_frame_type (this_frame) == INLINE_FRAME))
2826 return pc - 1;
2828 return pc;
2831 bool
2832 get_frame_address_in_block_if_available (const frame_info_ptr &this_frame,
2833 CORE_ADDR *pc)
2838 *pc = get_frame_address_in_block (this_frame);
2840 catch (const gdb_exception_error &ex)
2842 if (ex.error == NOT_AVAILABLE_ERROR)
2843 return false;
2844 throw;
2847 return true;
2850 symtab_and_line
2851 find_frame_sal (const frame_info_ptr &frame)
2853 frame_info_ptr next_frame;
2854 int notcurrent;
2855 CORE_ADDR pc;
2857 if (frame_inlined_callees (frame) > 0)
2859 struct symbol *sym;
2861 /* If the current frame has some inlined callees, and we have a next
2862 frame, then that frame must be an inlined frame. In this case
2863 this frame's sal is the "call site" of the next frame's inlined
2864 function, which can not be inferred from get_frame_pc. */
2865 next_frame = get_next_frame (frame);
2866 if (next_frame)
2867 sym = get_frame_function (next_frame);
2868 else
2869 sym = inline_skipped_symbol (inferior_thread ());
2871 /* If frame is inline, it certainly has symbols. */
2872 gdb_assert (sym);
2874 symtab_and_line sal;
2875 if (sym->line () != 0)
2877 sal.symtab = sym->symtab ();
2878 sal.line = sym->line ();
2880 else
2881 /* If the symbol does not have a location, we don't know where
2882 the call site is. Do not pretend to. This is jarring, but
2883 we can't do much better. */
2884 sal.pc = get_frame_pc (frame);
2886 sal.pspace = get_frame_program_space (frame);
2887 return sal;
2890 /* If FRAME is not the innermost frame, that normally means that
2891 FRAME->pc points at the return instruction (which is *after* the
2892 call instruction), and we want to get the line containing the
2893 call (because the call is where the user thinks the program is).
2894 However, if the next frame is either a SIGTRAMP_FRAME or a
2895 DUMMY_FRAME, then the next frame will contain a saved interrupt
2896 PC and such a PC indicates the current (rather than next)
2897 instruction/line, consequently, for such cases, want to get the
2898 line containing fi->pc. */
2899 if (!get_frame_pc_if_available (frame, &pc))
2900 return {};
2902 notcurrent = (pc != get_frame_address_in_block (frame));
2903 return find_pc_line (pc, notcurrent);
2906 /* Per "frame.h", return the ``address'' of the frame. Code should
2907 really be using get_frame_id(). */
2908 CORE_ADDR
2909 get_frame_base (const frame_info_ptr &fi)
2911 return get_frame_id (fi).stack_addr;
2914 /* High-level offsets into the frame. Used by the debug info. */
2916 CORE_ADDR
2917 get_frame_base_address (const frame_info_ptr &fi)
2919 if (get_frame_type (fi) != NORMAL_FRAME)
2920 return 0;
2921 if (fi->base == NULL)
2922 fi->base = frame_base_find_by_frame (fi);
2923 /* Sneaky: If the low-level unwind and high-level base code share a
2924 common unwinder, let them share the prologue cache. */
2925 if (fi->base->unwind == fi->unwind)
2926 return fi->base->this_base (fi, &fi->prologue_cache);
2927 return fi->base->this_base (fi, &fi->base_cache);
2930 CORE_ADDR
2931 get_frame_locals_address (const frame_info_ptr &fi)
2933 if (get_frame_type (fi) != NORMAL_FRAME)
2934 return 0;
2935 /* If there isn't a frame address method, find it. */
2936 if (fi->base == NULL)
2937 fi->base = frame_base_find_by_frame (fi);
2938 /* Sneaky: If the low-level unwind and high-level base code share a
2939 common unwinder, let them share the prologue cache. */
2940 if (fi->base->unwind == fi->unwind)
2941 return fi->base->this_locals (fi, &fi->prologue_cache);
2942 return fi->base->this_locals (fi, &fi->base_cache);
2945 CORE_ADDR
2946 get_frame_args_address (const frame_info_ptr &fi)
2948 if (get_frame_type (fi) != NORMAL_FRAME)
2949 return 0;
2950 /* If there isn't a frame address method, find it. */
2951 if (fi->base == NULL)
2952 fi->base = frame_base_find_by_frame (fi);
2953 /* Sneaky: If the low-level unwind and high-level base code share a
2954 common unwinder, let them share the prologue cache. */
2955 if (fi->base->unwind == fi->unwind)
2956 return fi->base->this_args (fi, &fi->prologue_cache);
2957 return fi->base->this_args (fi, &fi->base_cache);
2960 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2961 otherwise. */
2963 bool
2964 frame_unwinder_is (const frame_info_ptr &fi, const frame_unwind *unwinder)
2966 if (fi->unwind == nullptr)
2967 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
2969 return fi->unwind == unwinder;
2972 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2973 or -1 for a NULL frame. */
2976 frame_relative_level (const frame_info_ptr &fi)
2978 if (fi == NULL)
2979 return -1;
2980 else
2981 return fi->level;
2984 enum frame_type
2985 get_frame_type (const frame_info_ptr &frame)
2987 if (frame->unwind == NULL)
2988 /* Initialize the frame's unwinder because that's what
2989 provides the frame's type. */
2990 frame_unwind_find_by_frame (frame, &frame->prologue_cache);
2991 return frame->unwind->type;
2994 struct program_space *
2995 get_frame_program_space (const frame_info_ptr &frame)
2997 return frame->pspace;
3000 struct program_space *
3001 frame_unwind_program_space (const frame_info_ptr &this_frame)
3003 gdb_assert (this_frame);
3005 /* This is really a placeholder to keep the API consistent --- we
3006 assume for now that we don't have frame chains crossing
3007 spaces. */
3008 return this_frame->pspace;
3011 const address_space *
3012 get_frame_address_space (const frame_info_ptr &frame)
3014 return frame->aspace;
3017 /* Memory access methods. */
3019 void
3020 get_frame_memory (const frame_info_ptr &this_frame, CORE_ADDR addr,
3021 gdb::array_view<gdb_byte> buffer)
3023 read_memory (addr, buffer.data (), buffer.size ());
3026 LONGEST
3027 get_frame_memory_signed (const frame_info_ptr &this_frame, CORE_ADDR addr,
3028 int len)
3030 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3031 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3033 return read_memory_integer (addr, len, byte_order);
3036 ULONGEST
3037 get_frame_memory_unsigned (const frame_info_ptr &this_frame, CORE_ADDR addr,
3038 int len)
3040 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3041 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3043 return read_memory_unsigned_integer (addr, len, byte_order);
3046 bool
3047 safe_frame_unwind_memory (const frame_info_ptr &this_frame,
3048 CORE_ADDR addr, gdb::array_view<gdb_byte> buffer)
3050 /* NOTE: target_read_memory returns zero on success! */
3051 return target_read_memory (addr, buffer.data (), buffer.size ()) == 0;
3054 /* Architecture methods. */
3056 struct gdbarch *
3057 get_frame_arch (const frame_info_ptr &this_frame)
3059 return frame_unwind_arch (frame_info_ptr (this_frame->next));
3062 struct gdbarch *
3063 frame_unwind_arch (const frame_info_ptr &next_frame)
3065 if (!next_frame->prev_arch.p)
3067 struct gdbarch *arch;
3069 if (next_frame->unwind == NULL)
3070 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
3072 if (next_frame->unwind->prev_arch != NULL)
3073 arch = next_frame->unwind->prev_arch (next_frame,
3074 &next_frame->prologue_cache);
3075 else
3076 arch = get_frame_arch (next_frame);
3078 next_frame->prev_arch.arch = arch;
3079 next_frame->prev_arch.p = true;
3080 frame_debug_printf ("next_frame=%d -> %s",
3081 next_frame->level,
3082 gdbarch_bfd_arch_info (arch)->printable_name);
3085 return next_frame->prev_arch.arch;
3088 struct gdbarch *
3089 frame_unwind_caller_arch (const frame_info_ptr &initial_next_frame)
3091 frame_info_ptr next_frame = skip_artificial_frames (initial_next_frame);
3093 /* We must have a non-artificial frame. The caller is supposed to check
3094 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
3095 in this case. */
3096 gdb_assert (next_frame != nullptr);
3098 return frame_unwind_arch (next_frame);
3101 /* Gets the language of FRAME. */
3103 enum language
3104 get_frame_language (const frame_info_ptr &frame)
3106 CORE_ADDR pc = 0;
3107 bool pc_p = false;
3109 gdb_assert (frame!= NULL);
3111 /* We determine the current frame language by looking up its
3112 associated symtab. To retrieve this symtab, we use the frame
3113 PC. However we cannot use the frame PC as is, because it
3114 usually points to the instruction following the "call", which
3115 is sometimes the first instruction of another function. So
3116 we rely on get_frame_address_in_block(), it provides us with
3117 a PC that is guaranteed to be inside the frame's code
3118 block. */
3122 pc = get_frame_address_in_block (frame);
3123 pc_p = true;
3125 catch (const gdb_exception_error &ex)
3127 if (ex.error != NOT_AVAILABLE_ERROR)
3128 throw;
3131 if (pc_p)
3133 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
3135 if (cust != NULL)
3136 return cust->language ();
3139 return language_unknown;
3142 /* Stack pointer methods. */
3144 CORE_ADDR
3145 get_frame_sp (const frame_info_ptr &this_frame)
3147 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3149 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
3150 operate on THIS_FRAME now. */
3151 return gdbarch_unwind_sp (gdbarch, frame_info_ptr (this_frame->next));
3154 /* See frame.h. */
3156 frame_info_ptr
3157 frame_follow_static_link (const frame_info_ptr &initial_frame)
3159 const block *frame_block = get_frame_block (initial_frame, nullptr);
3160 if (frame_block == nullptr)
3161 return {};
3163 frame_block = frame_block->function_block ();
3165 const struct dynamic_prop *static_link = frame_block->static_link ();
3166 if (static_link == nullptr)
3167 return {};
3169 CORE_ADDR upper_frame_base;
3171 if (!dwarf2_evaluate_property (static_link, initial_frame, NULL, &upper_frame_base))
3172 return {};
3174 /* Now climb up the stack frame until we reach the frame we are interested
3175 in. */
3176 frame_info_ptr frame = initial_frame;
3177 for (; frame != nullptr; frame = get_prev_frame (frame))
3179 struct symbol *framefunc = get_frame_function (frame);
3181 /* Stacks can be quite deep: give the user a chance to stop this. */
3182 QUIT;
3184 /* If we don't know how to compute FRAME's base address, don't give up:
3185 maybe the frame we are looking for is upper in the stack frame. */
3186 if (framefunc != nullptr)
3188 if (const symbol_block_ops *block_ops = framefunc->block_ops ();
3189 (block_ops != nullptr
3190 && block_ops->get_frame_base != nullptr
3191 && (block_ops->get_frame_base (framefunc, frame)
3192 == upper_frame_base)))
3193 break;
3197 return frame;
3200 /* Return the reason why we can't unwind past FRAME. */
3202 enum unwind_stop_reason
3203 get_frame_unwind_stop_reason (const frame_info_ptr &frame)
3205 /* Fill-in STOP_REASON. */
3206 get_prev_frame_always (frame);
3207 gdb_assert (frame->prev_p);
3209 return frame->stop_reason;
3212 /* Return a string explaining REASON. */
3214 const char *
3215 unwind_stop_reason_to_string (enum unwind_stop_reason reason)
3217 switch (reason)
3219 #define SET(name, description) \
3220 case name: return _(description);
3221 #include "unwind_stop_reasons.def"
3222 #undef SET
3224 default:
3225 internal_error ("Invalid frame stop reason");
3229 const char *
3230 frame_stop_reason_string (const frame_info_ptr &fi)
3232 gdb_assert (fi->prev_p);
3233 gdb_assert (fi->prev == NULL);
3235 /* Return the specific string if we have one. */
3236 if (fi->stop_string != NULL)
3237 return fi->stop_string;
3239 /* Return the generic string if we have nothing better. */
3240 return unwind_stop_reason_to_string (fi->stop_reason);
3243 /* Return the enum symbol name of REASON as a string, to use in debug
3244 output. */
3246 static const char *
3247 frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
3249 switch (reason)
3251 #define SET(name, description) \
3252 case name: return #name;
3253 #include "unwind_stop_reasons.def"
3254 #undef SET
3256 default:
3257 internal_error ("Invalid frame stop reason");
3261 /* Clean up after a failed (wrong unwinder) attempt to unwind past
3262 FRAME. */
3264 void
3265 frame_cleanup_after_sniffer (const frame_info_ptr &frame)
3267 /* The sniffer should not allocate a prologue cache if it did not
3268 match this frame. */
3269 gdb_assert (frame->prologue_cache == NULL);
3271 /* No sniffer should extend the frame chain; sniff based on what is
3272 already certain. */
3273 gdb_assert (!frame->prev_p);
3275 /* The sniffer should not check the frame's ID; that's circular. */
3276 gdb_assert (frame->this_id.p != frame_id_status::COMPUTED);
3278 /* Clear cached fields dependent on the unwinder.
3280 The previous PC is independent of the unwinder, but the previous
3281 function is not (see get_frame_address_in_block). */
3282 frame->prev_func.status = CC_UNKNOWN;
3283 frame->prev_func.addr = 0;
3285 /* Discard the unwinder last, so that we can easily find it if an assertion
3286 in this function triggers. */
3287 frame->unwind = NULL;
3290 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
3291 If sniffing fails, the caller should be sure to call
3292 frame_cleanup_after_sniffer. */
3294 void
3295 frame_prepare_for_sniffer (const frame_info_ptr &frame,
3296 const struct frame_unwind *unwind)
3298 gdb_assert (frame->unwind == NULL);
3299 frame->unwind = unwind;
3302 static struct cmd_list_element *set_backtrace_cmdlist;
3303 static struct cmd_list_element *show_backtrace_cmdlist;
3305 /* Definition of the "set backtrace" settings that are exposed as
3306 "backtrace" command options. */
3308 using boolean_option_def
3309 = gdb::option::boolean_option_def<set_backtrace_options>;
3311 const gdb::option::option_def set_backtrace_option_defs[] = {
3313 boolean_option_def {
3314 "past-main",
3315 [] (set_backtrace_options *opt) { return &opt->backtrace_past_main; },
3316 show_backtrace_past_main, /* show_cmd_cb */
3317 N_("Set whether backtraces should continue past \"main\"."),
3318 N_("Show whether backtraces should continue past \"main\"."),
3319 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
3320 the backtrace at \"main\". Set this if you need to see the rest\n\
3321 of the stack trace."),
3324 boolean_option_def {
3325 "past-entry",
3326 [] (set_backtrace_options *opt) { return &opt->backtrace_past_entry; },
3327 show_backtrace_past_entry, /* show_cmd_cb */
3328 N_("Set whether backtraces should continue past the entry point of a program."),
3329 N_("Show whether backtraces should continue past the entry point of a program."),
3330 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
3331 will terminate the backtrace there. Set this if you need to see\n\
3332 the rest of the stack trace."),
3336 /* Implement the 'maintenance print frame-id' command. */
3338 static void
3339 maintenance_print_frame_id (const char *args, int from_tty)
3341 frame_info_ptr frame;
3343 /* Use the currently selected frame, or select a frame based on the level
3344 number passed by the user. */
3345 if (args == nullptr)
3346 frame = get_selected_frame ("No frame selected");
3347 else
3349 int level = value_as_long (parse_and_eval (args));
3350 frame = find_relative_frame (get_current_frame (), &level);
3353 /* Print the frame-id. */
3354 gdb_assert (frame != nullptr);
3355 gdb_printf ("frame-id for frame #%d: %s\n",
3356 frame_relative_level (frame),
3357 get_frame_id (frame).to_string ().c_str ());
3360 /* See frame-info-ptr.h. */
3362 frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
3363 : m_ptr (ptr)
3365 frame_list.push_back (*this);
3367 if (m_ptr == nullptr)
3368 return;
3370 m_cached_level = ptr->level;
3372 if (m_cached_level != 0 || m_ptr->this_id.value.user_created_p)
3373 m_cached_id = m_ptr->this_id.value;
3376 /* See frame-info-ptr.h. */
3378 frame_info *
3379 frame_info_ptr::reinflate () const
3381 /* Ensure we have a valid frame level (sentinel frame or above). */
3382 gdb_assert (m_cached_level >= -1);
3384 if (m_ptr != nullptr)
3386 /* The frame_info wasn't invalidated, no need to reinflate. */
3387 return m_ptr;
3390 if (m_cached_id.user_created_p)
3391 m_ptr = create_new_frame (m_cached_id).get ();
3392 else
3394 /* Frame #0 needs special handling, see comment in select_frame. */
3395 if (m_cached_level == 0)
3396 m_ptr = get_current_frame ().get ();
3397 else
3399 /* If we reach here without a valid frame id, it means we are trying
3400 to reinflate a frame whose id was not know at construction time.
3401 We're probably trying to reinflate a frame while computing its id
3402 which is not possible, and would indicate a problem with GDB. */
3403 gdb_assert (frame_id_p (m_cached_id));
3404 m_ptr = frame_find_by_id (m_cached_id).get ();
3408 gdb_assert (m_ptr != nullptr);
3409 return m_ptr;
3412 void _initialize_frame ();
3413 void
3414 _initialize_frame ()
3416 obstack_init (&frame_cache_obstack);
3418 frame_stash_create ();
3420 gdb::observers::target_changed.attach (frame_observer_target_changed,
3421 "frame");
3423 add_setshow_prefix_cmd ("backtrace", class_maintenance,
3424 _("\
3425 Set backtrace specific variables.\n\
3426 Configure backtrace variables such as the backtrace limit"),
3427 _("\
3428 Show backtrace specific variables.\n\
3429 Show backtrace variables such as the backtrace limit."),
3430 &set_backtrace_cmdlist, &show_backtrace_cmdlist,
3431 &setlist, &showlist);
3433 add_setshow_uinteger_cmd ("limit", class_obscure,
3434 &user_set_backtrace_options.backtrace_limit, _("\
3435 Set an upper bound on the number of backtrace levels."), _("\
3436 Show the upper bound on the number of backtrace levels."), _("\
3437 No more than the specified number of frames can be displayed or examined.\n\
3438 Literal \"unlimited\" or zero means no limit."),
3439 NULL,
3440 show_backtrace_limit,
3441 &set_backtrace_cmdlist,
3442 &show_backtrace_cmdlist);
3444 gdb::option::add_setshow_cmds_for_options
3445 (class_stack, &user_set_backtrace_options,
3446 set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist);
3448 /* Debug this files internals. */
3449 add_setshow_boolean_cmd ("frame", class_maintenance, &frame_debug, _("\
3450 Set frame debugging."), _("\
3451 Show frame debugging."), _("\
3452 When non-zero, frame specific internal debugging is enabled."),
3453 NULL,
3454 show_frame_debug,
3455 &setdebuglist, &showdebuglist);
3457 add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id,
3458 _("Print the current frame-id."),
3459 &maintenanceprintlist);