ld x86_64 tests: Accept x86-64-v3 as a needed ISA
[binutils-gdb.git] / gdb / frame.c
blob7077016ccba469c6eea4634ca51c66da66c9ac5a
1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 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"
47 /* The sentinel frame terminates the innermost end of the frame chain.
48 If unwound, it returns the information needed to construct an
49 innermost frame.
51 The current frame, which is the innermost frame, can be found at
52 sentinel_frame->prev.
54 This is an optimization to be able to find the sentinel frame quickly,
55 it could otherwise be found in the frame cache. */
57 static frame_info *sentinel_frame;
59 /* Number of calls to reinit_frame_cache. */
60 static unsigned int frame_cache_generation = 0;
62 /* See frame.h. */
64 unsigned int
65 get_frame_cache_generation ()
67 return frame_cache_generation;
70 /* The values behind the global "set backtrace ..." settings. */
71 set_backtrace_options user_set_backtrace_options;
73 static frame_info_ptr get_prev_frame_raw (frame_info_ptr this_frame);
74 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
75 static frame_info_ptr create_new_frame (frame_id id);
77 /* Status of some values cached in the frame_info object. */
79 enum cached_copy_status
81 /* Value is unknown. */
82 CC_UNKNOWN,
84 /* We have a value. */
85 CC_VALUE,
87 /* Value was not saved. */
88 CC_NOT_SAVED,
90 /* Value is unavailable. */
91 CC_UNAVAILABLE
94 enum class frame_id_status
96 /* Frame id is not computed. */
97 NOT_COMPUTED = 0,
99 /* Frame id is being computed (compute_frame_id is active). */
100 COMPUTING,
102 /* Frame id has been computed. */
103 COMPUTED,
106 /* We keep a cache of stack frames, each of which is a "struct
107 frame_info". The innermost one gets allocated (in
108 wait_for_inferior) each time the inferior stops; sentinel_frame
109 points to it. Additional frames get allocated (in get_prev_frame)
110 as needed, and are chained through the next and prev fields. Any
111 time that the frame cache becomes invalid (most notably when we
112 execute something, but also if we change how we interpret the
113 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
114 which reads new symbols)), we should call reinit_frame_cache. */
116 struct frame_info
118 /* Return a string representation of this frame. */
119 std::string to_string () const;
121 /* Level of this frame. The inner-most (youngest) frame is at level
122 0. As you move towards the outer-most (oldest) frame, the level
123 increases. This is a cached value. It could just as easily be
124 computed by counting back from the selected frame to the inner
125 most frame. */
126 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
127 reserved to indicate a bogus frame - one that has been created
128 just to keep GDB happy (GDB always needs a frame). For the
129 moment leave this as speculation. */
130 int level;
132 /* The frame's program space. */
133 struct program_space *pspace;
135 /* The frame's address space. */
136 const address_space *aspace;
138 /* The frame's low-level unwinder and corresponding cache. The
139 low-level unwinder is responsible for unwinding register values
140 for the previous frame. The low-level unwind methods are
141 selected based on the presence, or otherwise, of register unwind
142 information such as CFI. */
143 void *prologue_cache;
144 const struct frame_unwind *unwind;
146 /* Cached copy of the previous frame's architecture. */
147 struct
149 bool p;
150 struct gdbarch *arch;
151 } prev_arch;
153 /* Cached copy of the previous frame's resume address. */
154 struct {
155 cached_copy_status status;
156 /* Did VALUE require unmasking when being read. */
157 bool masked;
158 CORE_ADDR value;
159 } prev_pc;
161 /* Cached copy of the previous frame's function address. */
162 struct
164 CORE_ADDR addr;
165 cached_copy_status status;
166 } prev_func;
168 /* This frame's ID. */
169 struct
171 frame_id_status p;
172 struct frame_id value;
173 } this_id;
175 /* The frame's high-level base methods, and corresponding cache.
176 The high level base methods are selected based on the frame's
177 debug info. */
178 const struct frame_base *base;
179 void *base_cache;
181 /* Pointers to the next (down, inner, younger) and previous (up,
182 outer, older) frame_info's in the frame cache. */
183 struct frame_info *next; /* down, inner, younger */
184 bool prev_p;
185 struct frame_info *prev; /* up, outer, older */
187 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
188 could. Only valid when PREV_P is set. */
189 enum unwind_stop_reason stop_reason;
191 /* A frame specific string describing the STOP_REASON in more detail.
192 Only valid when PREV_P is set, but even then may still be NULL. */
193 const char *stop_string;
196 /* See frame.h. */
198 void
199 set_frame_previous_pc_masked (frame_info_ptr frame)
201 frame->prev_pc.masked = true;
204 /* See frame.h. */
206 bool
207 get_frame_pc_masked (frame_info_ptr frame)
209 gdb_assert (frame->next != nullptr);
210 gdb_assert (frame->next->prev_pc.status == CC_VALUE);
212 return frame->next->prev_pc.masked;
215 /* A frame stash used to speed up frame lookups. Create a hash table
216 to stash frames previously accessed from the frame cache for
217 quicker subsequent retrieval. The hash table is emptied whenever
218 the frame cache is invalidated. */
220 static htab_t frame_stash;
222 /* Internal function to calculate a hash from the frame_id addresses,
223 using as many valid addresses as possible. Frames below level 0
224 are not stored in the hash table. */
226 static hashval_t
227 frame_addr_hash (const void *ap)
229 const frame_info *frame = (const frame_info *) ap;
230 const struct frame_id f_id = frame->this_id.value;
231 hashval_t hash = 0;
233 gdb_assert (f_id.stack_status != FID_STACK_INVALID
234 || f_id.code_addr_p
235 || f_id.special_addr_p);
237 if (f_id.stack_status == FID_STACK_VALID)
238 hash = iterative_hash (&f_id.stack_addr,
239 sizeof (f_id.stack_addr), hash);
240 if (f_id.code_addr_p)
241 hash = iterative_hash (&f_id.code_addr,
242 sizeof (f_id.code_addr), hash);
243 if (f_id.special_addr_p)
244 hash = iterative_hash (&f_id.special_addr,
245 sizeof (f_id.special_addr), hash);
247 char user_created_p = f_id.user_created_p;
248 hash = iterative_hash (&user_created_p, sizeof (user_created_p), hash);
250 return hash;
253 /* Internal equality function for the hash table. This function
254 defers equality operations to frame_id::operator==. */
256 static int
257 frame_addr_hash_eq (const void *a, const void *b)
259 const frame_info *f_entry = (const frame_info *) a;
260 const frame_info *f_element = (const frame_info *) b;
262 return f_entry->this_id.value == f_element->this_id.value;
265 /* Deletion function for the frame cache hash table. */
267 static void
268 frame_info_del (frame_info *frame)
270 if (frame->prologue_cache != nullptr
271 && frame->unwind->dealloc_cache != nullptr)
272 frame->unwind->dealloc_cache (frame, frame->prologue_cache);
274 if (frame->base_cache != nullptr
275 && frame->base->unwind->dealloc_cache != nullptr)
276 frame->base->unwind->dealloc_cache (frame, frame->base_cache);
279 /* Internal function to create the frame_stash hash table. 100 seems
280 to be a good compromise to start the hash table at. */
282 static void
283 frame_stash_create (void)
285 frame_stash = htab_create
286 (100, frame_addr_hash, frame_addr_hash_eq,
287 [] (void *p)
289 auto frame = static_cast<frame_info *> (p);
290 frame_info_del (frame);
294 /* Internal function to add a frame to the frame_stash hash table.
295 Returns false if a frame with the same ID was already stashed, true
296 otherwise. */
298 static bool
299 frame_stash_add (frame_info *frame)
301 /* Valid frame levels are -1 (sentinel frames) and above. */
302 gdb_assert (frame->level >= -1);
304 frame_info **slot = (frame_info **) htab_find_slot (frame_stash,
305 frame, INSERT);
307 /* If we already have a frame in the stack with the same id, we
308 either have a stack cycle (corrupted stack?), or some bug
309 elsewhere in GDB. In any case, ignore the duplicate and return
310 an indication to the caller. */
311 if (*slot != nullptr)
312 return false;
314 *slot = frame;
315 return true;
318 /* Internal function to search the frame stash for an entry with the
319 given frame ID. If found, return that frame. Otherwise return
320 NULL. */
322 static frame_info_ptr
323 frame_stash_find (struct frame_id id)
325 struct frame_info dummy;
326 frame_info *frame;
328 dummy.this_id.value = id;
329 frame = (frame_info *) htab_find (frame_stash, &dummy);
330 return frame_info_ptr (frame);
333 /* Internal function to invalidate the frame stash by removing all
334 entries in it. This only occurs when the frame cache is
335 invalidated. */
337 static void
338 frame_stash_invalidate (void)
340 htab_empty (frame_stash);
343 /* See frame.h */
344 scoped_restore_selected_frame::scoped_restore_selected_frame ()
346 m_lang = current_language->la_language;
347 save_selected_frame (&m_fid, &m_level);
350 /* See frame.h */
351 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
353 restore_selected_frame (m_fid, m_level);
354 set_language (m_lang);
357 /* Flag to control debugging. */
359 bool frame_debug;
361 static void
362 show_frame_debug (struct ui_file *file, int from_tty,
363 struct cmd_list_element *c, const char *value)
365 gdb_printf (file, _("Frame debugging is %s.\n"), value);
368 /* Implementation of "show backtrace past-main". */
370 static void
371 show_backtrace_past_main (struct ui_file *file, int from_tty,
372 struct cmd_list_element *c, const char *value)
374 gdb_printf (file,
375 _("Whether backtraces should "
376 "continue past \"main\" is %s.\n"),
377 value);
380 /* Implementation of "show backtrace past-entry". */
382 static void
383 show_backtrace_past_entry (struct ui_file *file, int from_tty,
384 struct cmd_list_element *c, const char *value)
386 gdb_printf (file, _("Whether backtraces should continue past the "
387 "entry point of a program is %s.\n"),
388 value);
391 /* Implementation of "show backtrace limit". */
393 static void
394 show_backtrace_limit (struct ui_file *file, int from_tty,
395 struct cmd_list_element *c, const char *value)
397 gdb_printf (file,
398 _("An upper bound on the number "
399 "of backtrace levels is %s.\n"),
400 value);
403 /* See frame.h. */
405 std::string
406 frame_id::to_string () const
408 const struct frame_id &id = *this;
410 std::string res = "{";
412 if (id.stack_status == FID_STACK_INVALID)
413 res += "!stack";
414 else if (id.stack_status == FID_STACK_UNAVAILABLE)
415 res += "stack=<unavailable>";
416 else if (id.stack_status == FID_STACK_SENTINEL)
417 res += "stack=<sentinel>";
418 else if (id.stack_status == FID_STACK_OUTER)
419 res += "stack=<outer>";
420 else
421 res += std::string ("stack=") + hex_string (id.stack_addr);
423 /* Helper function to format 'N=A' if P is true, otherwise '!N'. */
424 auto field_to_string = [] (const char *n, bool p, CORE_ADDR a) -> std::string
426 if (p)
427 return std::string (n) + "=" + core_addr_to_string (a);
428 else
429 return std::string ("!") + std::string (n);
432 res += (std::string (",")
433 + field_to_string ("code", id.code_addr_p, id.code_addr)
434 + std::string (",")
435 + field_to_string ("special", id.special_addr_p, id.special_addr));
437 if (id.artificial_depth)
438 res += ",artificial=" + std::to_string (id.artificial_depth);
439 res += "}";
440 return res;
443 /* See frame.h. */
445 const char *
446 frame_type_str (frame_type type)
448 switch (type)
450 case NORMAL_FRAME:
451 return "NORMAL_FRAME";
453 case DUMMY_FRAME:
454 return "DUMMY_FRAME";
456 case INLINE_FRAME:
457 return "INLINE_FRAME";
459 case TAILCALL_FRAME:
460 return "TAILCALL_FRAME";
462 case SIGTRAMP_FRAME:
463 return "SIGTRAMP_FRAME";
465 case ARCH_FRAME:
466 return "ARCH_FRAME";
468 case SENTINEL_FRAME:
469 return "SENTINEL_FRAME";
471 default:
472 return "<unknown type>";
476 /* See struct frame_info. */
478 std::string
479 frame_info::to_string () const
481 const frame_info *fi = this;
483 std::string res;
485 res += string_printf ("{level=%d,", fi->level);
487 if (fi->unwind != NULL)
488 res += string_printf ("type=%s,", frame_type_str (fi->unwind->type));
489 else
490 res += "type=<unknown>,";
492 if (fi->unwind != NULL)
493 res += string_printf ("unwinder=\"%s\",", fi->unwind->name);
494 else
495 res += "unwinder=<unknown>,";
497 if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
498 res += "pc=<unknown>,";
499 else if (fi->next->prev_pc.status == CC_VALUE)
500 res += string_printf ("pc=%s%s,", hex_string (fi->next->prev_pc.value),
501 fi->next->prev_pc.masked ? "[PAC]" : "");
502 else if (fi->next->prev_pc.status == CC_NOT_SAVED)
503 res += "pc=<not saved>,";
504 else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
505 res += "pc=<unavailable>,";
507 if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
508 res += "id=<not computed>,";
509 else if (fi->this_id.p == frame_id_status::COMPUTING)
510 res += "id=<computing>,";
511 else
512 res += string_printf ("id=%s,", fi->this_id.value.to_string ().c_str ());
514 if (fi->next != NULL && fi->next->prev_func.status == CC_VALUE)
515 res += string_printf ("func=%s", hex_string (fi->next->prev_func.addr));
516 else
517 res += "func=<unknown>";
519 res += "}";
521 return res;
524 /* Given FRAME, return the enclosing frame as found in real frames read-in from
525 inferior memory. Skip any previous frames which were made up by GDB.
526 Return FRAME if FRAME is a non-artificial frame.
527 Return NULL if FRAME is the start of an artificial-only chain. */
529 static frame_info_ptr
530 skip_artificial_frames (frame_info_ptr frame)
532 /* Note we use get_prev_frame_always, and not get_prev_frame. The
533 latter will truncate the frame chain, leading to this function
534 unintentionally returning a null_frame_id (e.g., when the user
535 sets a backtrace limit).
537 Note that for record targets we may get a frame chain that consists
538 of artificial frames only. */
539 while (get_frame_type (frame) == INLINE_FRAME
540 || get_frame_type (frame) == TAILCALL_FRAME)
542 frame = get_prev_frame_always (frame);
543 if (frame == NULL)
544 break;
547 return frame;
550 frame_info_ptr
551 skip_unwritable_frames (frame_info_ptr frame)
553 while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0)
555 frame = get_prev_frame (frame);
556 if (frame == NULL)
557 break;
560 return frame;
563 /* See frame.h. */
565 frame_info_ptr
566 skip_tailcall_frames (frame_info_ptr frame)
568 while (get_frame_type (frame) == TAILCALL_FRAME)
570 /* Note that for record targets we may get a frame chain that consists of
571 tailcall frames only. */
572 frame = get_prev_frame (frame);
573 if (frame == NULL)
574 break;
577 return frame;
580 /* Compute the frame's uniq ID that can be used to, later, re-find the
581 frame. */
583 static void
584 compute_frame_id (frame_info_ptr fi)
586 FRAME_SCOPED_DEBUG_ENTER_EXIT;
588 gdb_assert (fi->this_id.p == frame_id_status::NOT_COMPUTED);
590 unsigned int entry_generation = get_frame_cache_generation ();
594 /* Mark this frame's id as "being computed. */
595 fi->this_id.p = frame_id_status::COMPUTING;
597 frame_debug_printf ("fi=%d", fi->level);
599 /* Find the unwinder. */
600 if (fi->unwind == NULL)
601 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
603 /* Find THIS frame's ID. */
604 /* Default to outermost if no ID is found. */
605 fi->this_id.value = outer_frame_id;
606 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
607 gdb_assert (frame_id_p (fi->this_id.value));
609 /* Mark this frame's id as "computed". */
610 fi->this_id.p = frame_id_status::COMPUTED;
612 frame_debug_printf (" -> %s", fi->this_id.value.to_string ().c_str ());
614 catch (const gdb_exception &ex)
616 /* On error, revert the frame id status to not computed. If the frame
617 cache generation changed, the frame object doesn't exist anymore, so
618 don't touch it. */
619 if (get_frame_cache_generation () == entry_generation)
620 fi->this_id.p = frame_id_status::NOT_COMPUTED;
622 throw;
626 /* Return a frame uniq ID that can be used to, later, re-find the
627 frame. */
629 struct frame_id
630 get_frame_id (frame_info_ptr fi)
632 if (fi == NULL)
633 return null_frame_id;
635 /* It's always invalid to try to get a frame's id while it is being
636 computed. */
637 gdb_assert (fi->this_id.p != frame_id_status::COMPUTING);
639 if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
641 /* If we haven't computed the frame id yet, then it must be that
642 this is the current frame. Compute it now, and stash the
643 result. The IDs of other frames are computed as soon as
644 they're created, in order to detect cycles. See
645 get_prev_frame_if_no_cycle. */
646 gdb_assert (fi->level == 0);
648 /* Compute. */
649 compute_frame_id (fi);
651 /* Since this is the first frame in the chain, this should
652 always succeed. */
653 bool stashed = frame_stash_add (fi.get ());
654 gdb_assert (stashed);
657 return fi->this_id.value;
660 struct frame_id
661 get_stack_frame_id (frame_info_ptr next_frame)
663 return get_frame_id (skip_artificial_frames (next_frame));
666 struct frame_id
667 frame_unwind_caller_id (frame_info_ptr next_frame)
669 frame_info_ptr this_frame;
671 /* Use get_prev_frame_always, and not get_prev_frame. The latter
672 will truncate the frame chain, leading to this function
673 unintentionally returning a null_frame_id (e.g., when a caller
674 requests the frame ID of "main()"s caller. */
676 next_frame = skip_artificial_frames (next_frame);
677 if (next_frame == NULL)
678 return null_frame_id;
680 this_frame = get_prev_frame_always (next_frame);
681 if (this_frame)
682 return get_frame_id (skip_artificial_frames (this_frame));
683 else
684 return null_frame_id;
687 const struct frame_id null_frame_id = { 0 }; /* All zeros. */
688 const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_OUTER, 0, 1, 0 };
690 struct frame_id
691 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
692 CORE_ADDR special_addr)
694 struct frame_id id = null_frame_id;
696 id.stack_addr = stack_addr;
697 id.stack_status = FID_STACK_VALID;
698 id.code_addr = code_addr;
699 id.code_addr_p = true;
700 id.special_addr = special_addr;
701 id.special_addr_p = true;
702 return id;
705 /* See frame.h. */
707 struct frame_id
708 frame_id_build_unavailable_stack (CORE_ADDR code_addr)
710 struct frame_id id = null_frame_id;
712 id.stack_status = FID_STACK_UNAVAILABLE;
713 id.code_addr = code_addr;
714 id.code_addr_p = true;
715 return id;
718 /* See frame.h. */
720 struct frame_id
721 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
722 CORE_ADDR special_addr)
724 struct frame_id id = null_frame_id;
726 id.stack_status = FID_STACK_UNAVAILABLE;
727 id.code_addr = code_addr;
728 id.code_addr_p = true;
729 id.special_addr = special_addr;
730 id.special_addr_p = true;
731 return id;
734 struct frame_id
735 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
737 struct frame_id id = null_frame_id;
739 id.stack_addr = stack_addr;
740 id.stack_status = FID_STACK_VALID;
741 id.code_addr = code_addr;
742 id.code_addr_p = true;
743 return id;
746 struct frame_id
747 frame_id_build_wild (CORE_ADDR stack_addr)
749 struct frame_id id = null_frame_id;
751 id.stack_addr = stack_addr;
752 id.stack_status = FID_STACK_VALID;
753 return id;
756 /* See frame.h. */
758 frame_id
759 frame_id_build_sentinel (CORE_ADDR stack_addr, CORE_ADDR code_addr)
761 frame_id id = null_frame_id;
763 id.stack_status = FID_STACK_SENTINEL;
764 id.special_addr_p = 1;
766 if (stack_addr != 0 || code_addr != 0)
768 /* The purpose of saving these in the sentinel frame ID is to be able to
769 differentiate the IDs of several sentinel frames that could exist
770 simultaneously in the frame cache. */
771 id.stack_addr = stack_addr;
772 id.code_addr = code_addr;
773 id.code_addr_p = 1;
776 return id;
779 bool
780 frame_id_p (frame_id l)
782 /* The frame is valid iff it has a valid stack address. */
783 bool p = l.stack_status != FID_STACK_INVALID;
785 frame_debug_printf ("l=%s -> %d", l.to_string ().c_str (), p);
787 return p;
790 bool
791 frame_id_artificial_p (frame_id l)
793 if (!frame_id_p (l))
794 return false;
796 return l.artificial_depth != 0;
799 bool
800 frame_id::operator== (const frame_id &r) const
802 bool eq;
804 if (stack_status == FID_STACK_INVALID
805 || r.stack_status == FID_STACK_INVALID)
806 /* Like a NaN, if either ID is invalid, the result is false.
807 Note that a frame ID is invalid iff it is the null frame ID. */
808 eq = false;
809 else if (stack_status != r.stack_status || stack_addr != r.stack_addr)
810 /* If .stack addresses are different, the frames are different. */
811 eq = false;
812 else if (code_addr_p && r.code_addr_p && code_addr != r.code_addr)
813 /* An invalid code addr is a wild card. If .code addresses are
814 different, the frames are different. */
815 eq = false;
816 else if (special_addr_p && r.special_addr_p
817 && special_addr != r.special_addr)
818 /* An invalid special addr is a wild card (or unused). Otherwise
819 if special addresses are different, the frames are different. */
820 eq = false;
821 else if (artificial_depth != r.artificial_depth)
822 /* If artificial depths are different, the frames must be different. */
823 eq = false;
824 else if (user_created_p != r.user_created_p)
825 eq = false;
826 else
827 /* Frames are equal. */
828 eq = true;
830 frame_debug_printf ("l=%s, r=%s -> %d",
831 to_string ().c_str (), r.to_string ().c_str (), eq);
833 return eq;
836 /* Safety net to check whether frame ID L should be inner to
837 frame ID R, according to their stack addresses.
839 This method cannot be used to compare arbitrary frames, as the
840 ranges of valid stack addresses may be discontiguous (e.g. due
841 to sigaltstack).
843 However, it can be used as safety net to discover invalid frame
844 IDs in certain circumstances. Assuming that NEXT is the immediate
845 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
847 * The stack address of NEXT must be inner-than-or-equal to the stack
848 address of THIS.
850 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
851 error has occurred.
853 * If NEXT and THIS have different stack addresses, no other frame
854 in the frame chain may have a stack address in between.
856 Therefore, if frame_id_inner (TEST, THIS) holds, but
857 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
858 to a valid frame in the frame chain.
860 The sanity checks above cannot be performed when a SIGTRAMP frame
861 is involved, because signal handlers might be executed on a different
862 stack than the stack used by the routine that caused the signal
863 to be raised. This can happen for instance when a thread exceeds
864 its maximum stack size. In this case, certain compilers implement
865 a stack overflow strategy that cause the handler to be run on a
866 different stack. */
868 static bool
869 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
871 bool inner;
873 if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
874 /* Like NaN, any operation involving an invalid ID always fails.
875 Likewise if either ID has an unavailable stack address. */
876 inner = false;
877 else if (l.artificial_depth > r.artificial_depth
878 && l.stack_addr == r.stack_addr
879 && l.code_addr_p == r.code_addr_p
880 && l.special_addr_p == r.special_addr_p
881 && l.special_addr == r.special_addr)
883 /* Same function, different inlined functions. */
884 const struct block *lb, *rb;
886 gdb_assert (l.code_addr_p && r.code_addr_p);
888 lb = block_for_pc (l.code_addr);
889 rb = block_for_pc (r.code_addr);
891 if (lb == NULL || rb == NULL)
892 /* Something's gone wrong. */
893 inner = false;
894 else
895 /* This will return true if LB and RB are the same block, or
896 if the block with the smaller depth lexically encloses the
897 block with the greater depth. */
898 inner = rb->contains (lb);
900 else
901 /* Only return non-zero when strictly inner than. Note that, per
902 comment in "frame.h", there is some fuzz here. Frameless
903 functions are not strictly inner than (same .stack but
904 different .code and/or .special address). */
905 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
907 frame_debug_printf ("is l=%s inner than r=%s? %d",
908 l.to_string ().c_str (), r.to_string ().c_str (),
909 inner);
911 return inner;
914 frame_info_ptr
915 frame_find_by_id (struct frame_id id)
917 frame_info_ptr frame, prev_frame;
919 /* ZERO denotes the null frame, let the caller decide what to do
920 about it. Should it instead return get_current_frame()? */
921 if (!frame_id_p (id))
922 return NULL;
924 /* Check for the sentinel frame. */
925 if (id == frame_id_build_sentinel (0, 0))
926 return frame_info_ptr (sentinel_frame);
928 /* Try using the frame stash first. Finding it there removes the need
929 to perform the search by looping over all frames, which can be very
930 CPU-intensive if the number of frames is very high (the loop is O(n)
931 and get_prev_frame performs a series of checks that are relatively
932 expensive). This optimization is particularly useful when this function
933 is called from another function (such as value_fetch_lazy, case
934 val->lval () == lval_register) which already loops over all frames,
935 making the overall behavior O(n^2). */
936 frame = frame_stash_find (id);
937 if (frame)
938 return frame;
940 for (frame = get_current_frame (); ; frame = prev_frame)
942 struct frame_id self = get_frame_id (frame);
944 if (id == self)
945 /* An exact match. */
946 return frame;
948 prev_frame = get_prev_frame (frame);
949 if (!prev_frame)
950 return NULL;
952 /* As a safety net to avoid unnecessary backtracing while trying
953 to find an invalid ID, we check for a common situation where
954 we can detect from comparing stack addresses that no other
955 frame in the current frame chain can have this ID. See the
956 comment at frame_id_inner for details. */
957 if (get_frame_type (frame) == NORMAL_FRAME
958 && !frame_id_inner (get_frame_arch (frame), id, self)
959 && frame_id_inner (get_frame_arch (prev_frame), id,
960 get_frame_id (prev_frame)))
961 return NULL;
963 return NULL;
966 static CORE_ADDR
967 frame_unwind_pc (frame_info_ptr this_frame)
969 if (this_frame->prev_pc.status == CC_UNKNOWN)
971 struct gdbarch *prev_gdbarch;
972 CORE_ADDR pc = 0;
973 bool pc_p = false;
975 /* The right way. The `pure' way. The one true way. This
976 method depends solely on the register-unwind code to
977 determine the value of registers in THIS frame, and hence
978 the value of this frame's PC (resume address). A typical
979 implementation is no more than:
981 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
982 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
984 Note: this method is very heavily dependent on a correct
985 register-unwind implementation, it pays to fix that
986 method first; this method is frame type agnostic, since
987 it only deals with register values, it works with any
988 frame. This is all in stark contrast to the old
989 FRAME_SAVED_PC which would try to directly handle all the
990 different ways that a PC could be unwound. */
991 prev_gdbarch = frame_unwind_arch (this_frame);
995 pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
996 pc_p = true;
998 catch (const gdb_exception_error &ex)
1000 if (ex.error == NOT_AVAILABLE_ERROR)
1002 this_frame->prev_pc.status = CC_UNAVAILABLE;
1004 frame_debug_printf ("this_frame=%d -> <unavailable>",
1005 this_frame->level);
1007 else if (ex.error == OPTIMIZED_OUT_ERROR)
1009 this_frame->prev_pc.status = CC_NOT_SAVED;
1011 frame_debug_printf ("this_frame=%d -> <not saved>",
1012 this_frame->level);
1014 else
1015 throw;
1018 if (pc_p)
1020 this_frame->prev_pc.value = pc;
1021 this_frame->prev_pc.status = CC_VALUE;
1023 frame_debug_printf ("this_frame=%d -> %s",
1024 this_frame->level,
1025 hex_string (this_frame->prev_pc.value));
1029 if (this_frame->prev_pc.status == CC_VALUE)
1030 return this_frame->prev_pc.value;
1031 else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
1032 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
1033 else if (this_frame->prev_pc.status == CC_NOT_SAVED)
1034 throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
1035 else
1036 internal_error ("unexpected prev_pc status: %d",
1037 (int) this_frame->prev_pc.status);
1040 CORE_ADDR
1041 frame_unwind_caller_pc (frame_info_ptr this_frame)
1043 this_frame = skip_artificial_frames (this_frame);
1045 /* We must have a non-artificial frame. The caller is supposed to check
1046 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1047 in this case. */
1048 gdb_assert (this_frame != NULL);
1050 return frame_unwind_pc (this_frame);
1053 bool
1054 get_frame_func_if_available (frame_info_ptr this_frame, CORE_ADDR *pc)
1056 frame_info *next_frame = this_frame->next;
1058 if (next_frame->prev_func.status == CC_UNKNOWN)
1060 CORE_ADDR addr_in_block;
1062 /* Make certain that this, and not the adjacent, function is
1063 found. */
1064 if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
1066 next_frame->prev_func.status = CC_UNAVAILABLE;
1068 frame_debug_printf ("this_frame=%d -> unavailable",
1069 this_frame->level);
1071 else
1073 next_frame->prev_func.status = CC_VALUE;
1074 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
1076 frame_debug_printf ("this_frame=%d -> %s",
1077 this_frame->level,
1078 hex_string (next_frame->prev_func.addr));
1082 if (next_frame->prev_func.status == CC_UNAVAILABLE)
1084 *pc = -1;
1085 return false;
1087 else
1089 gdb_assert (next_frame->prev_func.status == CC_VALUE);
1091 *pc = next_frame->prev_func.addr;
1092 return true;
1096 CORE_ADDR
1097 get_frame_func (frame_info_ptr this_frame)
1099 CORE_ADDR pc;
1101 if (!get_frame_func_if_available (this_frame, &pc))
1102 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
1104 return pc;
1107 std::unique_ptr<readonly_detached_regcache>
1108 frame_save_as_regcache (frame_info_ptr this_frame)
1110 auto cooked_read = [this_frame] (int regnum, gdb_byte *buf)
1112 if (!deprecated_frame_register_read (this_frame, regnum, buf))
1113 return REG_UNAVAILABLE;
1114 else
1115 return REG_VALID;
1118 std::unique_ptr<readonly_detached_regcache> regcache
1119 (new readonly_detached_regcache (get_frame_arch (this_frame), cooked_read));
1121 return regcache;
1124 void
1125 frame_pop (frame_info_ptr this_frame)
1127 frame_info_ptr prev_frame;
1129 if (get_frame_type (this_frame) == DUMMY_FRAME)
1131 /* Popping a dummy frame involves restoring more than just registers.
1132 dummy_frame_pop does all the work. */
1133 dummy_frame_pop (get_frame_id (this_frame), inferior_thread ());
1134 return;
1137 /* Ensure that we have a frame to pop to. */
1138 prev_frame = get_prev_frame_always (this_frame);
1140 if (!prev_frame)
1141 error (_("Cannot pop the initial frame."));
1143 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1144 entering THISFRAME. */
1145 prev_frame = skip_tailcall_frames (prev_frame);
1147 if (prev_frame == NULL)
1148 error (_("Cannot find the caller frame."));
1150 /* Make a copy of all the register values unwound from this frame.
1151 Save them in a scratch buffer so that there isn't a race between
1152 trying to extract the old values from the current regcache while
1153 at the same time writing new values into that same cache. */
1154 std::unique_ptr<readonly_detached_regcache> scratch
1155 = frame_save_as_regcache (prev_frame);
1157 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1158 target's register cache that it is about to be hit with a burst
1159 register transfer and that the sequence of register writes should
1160 be batched. The pair target_prepare_to_store() and
1161 target_store_registers() kind of suggest this functionality.
1162 Unfortunately, they don't implement it. Their lack of a formal
1163 definition can lead to targets writing back bogus values
1164 (arguably a bug in the target code mind). */
1165 /* Now copy those saved registers into the current regcache. */
1166 get_current_regcache ()->restore (scratch.get ());
1168 /* We've made right mess of GDB's local state, just discard
1169 everything. */
1170 reinit_frame_cache ();
1173 void
1174 frame_register_unwind (frame_info_ptr next_frame, int regnum,
1175 int *optimizedp, int *unavailablep,
1176 enum lval_type *lvalp, CORE_ADDR *addrp,
1177 int *realnump, gdb_byte *bufferp)
1179 struct value *value;
1181 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1182 that the value proper does not need to be fetched. */
1183 gdb_assert (optimizedp != NULL);
1184 gdb_assert (lvalp != NULL);
1185 gdb_assert (addrp != NULL);
1186 gdb_assert (realnump != NULL);
1187 /* gdb_assert (bufferp != NULL); */
1189 value = frame_unwind_register_value (next_frame, regnum);
1191 gdb_assert (value != NULL);
1193 *optimizedp = value->optimized_out ();
1194 *unavailablep = !value->entirely_available ();
1195 *lvalp = value->lval ();
1196 *addrp = value->address ();
1197 if (*lvalp == lval_register)
1198 *realnump = VALUE_REGNUM (value);
1199 else
1200 *realnump = -1;
1202 if (bufferp)
1204 if (!*optimizedp && !*unavailablep)
1205 memcpy (bufferp, value->contents_all ().data (),
1206 value->type ()->length ());
1207 else
1208 memset (bufferp, 0, value->type ()->length ());
1211 /* Dispose of the new value. This prevents watchpoints from
1212 trying to watch the saved frame pointer. */
1213 release_value (value);
1216 /* Get the value of the register that belongs to this FRAME. This
1217 function is a wrapper to the call sequence ``frame_register_unwind
1218 (get_next_frame (FRAME))''. As per frame_register_unwind(), if
1219 VALUEP is NULL, the registers value is not fetched/computed. */
1221 static void
1222 frame_register (frame_info_ptr frame, int regnum,
1223 int *optimizedp, int *unavailablep, enum lval_type *lvalp,
1224 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
1226 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1227 that the value proper does not need to be fetched. */
1228 gdb_assert (optimizedp != NULL);
1229 gdb_assert (lvalp != NULL);
1230 gdb_assert (addrp != NULL);
1231 gdb_assert (realnump != NULL);
1232 /* gdb_assert (bufferp != NULL); */
1234 /* Obtain the register value by unwinding the register from the next
1235 (more inner frame). */
1236 gdb_assert (frame != NULL && frame->next != NULL);
1237 frame_register_unwind (frame_info_ptr (frame->next), regnum, optimizedp,
1238 unavailablep, lvalp, addrp, realnump, bufferp);
1241 void
1242 frame_unwind_register (frame_info_ptr next_frame, int regnum, gdb_byte *buf)
1244 int optimized;
1245 int unavailable;
1246 CORE_ADDR addr;
1247 int realnum;
1248 enum lval_type lval;
1250 frame_register_unwind (next_frame, regnum, &optimized, &unavailable,
1251 &lval, &addr, &realnum, buf);
1253 if (optimized)
1254 throw_error (OPTIMIZED_OUT_ERROR,
1255 _("Register %d was not saved"), regnum);
1256 if (unavailable)
1257 throw_error (NOT_AVAILABLE_ERROR,
1258 _("Register %d is not available"), regnum);
1261 void
1262 get_frame_register (frame_info_ptr frame,
1263 int regnum, gdb_byte *buf)
1265 frame_unwind_register (frame_info_ptr (frame->next), regnum, buf);
1268 struct value *
1269 frame_unwind_register_value (frame_info_ptr next_frame, int regnum)
1271 FRAME_SCOPED_DEBUG_ENTER_EXIT;
1273 gdb_assert (next_frame != NULL);
1274 gdbarch *gdbarch = frame_unwind_arch (next_frame);
1275 frame_debug_printf ("frame=%d, regnum=%d(%s)",
1276 next_frame->level, regnum,
1277 user_reg_map_regnum_to_name (gdbarch, regnum));
1279 /* Find the unwinder. */
1280 if (next_frame->unwind == NULL)
1281 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
1283 /* Ask this frame to unwind its register. */
1284 value *value = next_frame->unwind->prev_register (next_frame,
1285 &next_frame->prologue_cache,
1286 regnum);
1288 if (frame_debug)
1290 string_file debug_file;
1292 gdb_printf (&debug_file, " ->");
1293 if (value->optimized_out ())
1295 gdb_printf (&debug_file, " ");
1296 val_print_not_saved (&debug_file);
1298 else
1300 if (value->lval () == lval_register)
1301 gdb_printf (&debug_file, " register=%d",
1302 VALUE_REGNUM (value));
1303 else if (value->lval () == lval_memory)
1304 gdb_printf (&debug_file, " address=%s",
1305 paddress (gdbarch,
1306 value->address ()));
1307 else
1308 gdb_printf (&debug_file, " computed");
1310 if (value->lazy ())
1311 gdb_printf (&debug_file, " lazy");
1312 else
1314 int i;
1315 gdb::array_view<const gdb_byte> buf = value->contents ();
1317 gdb_printf (&debug_file, " bytes=");
1318 gdb_printf (&debug_file, "[");
1319 for (i = 0; i < register_size (gdbarch, regnum); i++)
1320 gdb_printf (&debug_file, "%02x", buf[i]);
1321 gdb_printf (&debug_file, "]");
1325 frame_debug_printf ("%s", debug_file.c_str ());
1328 return value;
1331 struct value *
1332 get_frame_register_value (frame_info_ptr frame, int regnum)
1334 return frame_unwind_register_value (frame_info_ptr (frame->next), regnum);
1337 LONGEST
1338 frame_unwind_register_signed (frame_info_ptr next_frame, int regnum)
1340 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1341 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1342 struct value *value = frame_unwind_register_value (next_frame, regnum);
1344 gdb_assert (value != NULL);
1346 if (value->optimized_out ())
1348 throw_error (OPTIMIZED_OUT_ERROR,
1349 _("Register %d was not saved"), regnum);
1351 if (!value->entirely_available ())
1353 throw_error (NOT_AVAILABLE_ERROR,
1354 _("Register %d is not available"), regnum);
1357 LONGEST r = extract_signed_integer (value->contents_all (), byte_order);
1359 release_value (value);
1360 return r;
1363 LONGEST
1364 get_frame_register_signed (frame_info_ptr frame, int regnum)
1366 return frame_unwind_register_signed (frame_info_ptr (frame->next), regnum);
1369 ULONGEST
1370 frame_unwind_register_unsigned (frame_info_ptr next_frame, int regnum)
1372 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1373 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1374 int size = register_size (gdbarch, regnum);
1375 struct value *value = frame_unwind_register_value (next_frame, regnum);
1377 gdb_assert (value != NULL);
1379 if (value->optimized_out ())
1381 throw_error (OPTIMIZED_OUT_ERROR,
1382 _("Register %d was not saved"), regnum);
1384 if (!value->entirely_available ())
1386 throw_error (NOT_AVAILABLE_ERROR,
1387 _("Register %d is not available"), regnum);
1390 ULONGEST r = extract_unsigned_integer (value->contents_all ().data (),
1391 size, byte_order);
1393 release_value (value);
1394 return r;
1397 ULONGEST
1398 get_frame_register_unsigned (frame_info_ptr frame, int regnum)
1400 return frame_unwind_register_unsigned (frame_info_ptr (frame->next), regnum);
1403 bool
1404 read_frame_register_unsigned (frame_info_ptr frame, int regnum,
1405 ULONGEST *val)
1407 struct value *regval = get_frame_register_value (frame, regnum);
1409 if (!regval->optimized_out ()
1410 && regval->entirely_available ())
1412 struct gdbarch *gdbarch = get_frame_arch (frame);
1413 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1414 int size = register_size (gdbarch, VALUE_REGNUM (regval));
1416 *val = extract_unsigned_integer (regval->contents ().data (), size,
1417 byte_order);
1418 return true;
1421 return false;
1424 void
1425 put_frame_register (frame_info_ptr frame, int regnum,
1426 const gdb_byte *buf)
1428 struct gdbarch *gdbarch = get_frame_arch (frame);
1429 int realnum;
1430 int optim;
1431 int unavail;
1432 enum lval_type lval;
1433 CORE_ADDR addr;
1435 frame_register (frame, regnum, &optim, &unavail,
1436 &lval, &addr, &realnum, NULL);
1437 if (optim)
1438 error (_("Attempt to assign to a register that was not saved."));
1439 switch (lval)
1441 case lval_memory:
1443 write_memory (addr, buf, register_size (gdbarch, regnum));
1444 break;
1446 case lval_register:
1447 get_current_regcache ()->cooked_write (realnum, buf);
1448 break;
1449 default:
1450 error (_("Attempt to assign to an unmodifiable value."));
1454 /* This function is deprecated. Use get_frame_register_value instead,
1455 which provides more accurate information.
1457 Find and return the value of REGNUM for the specified stack frame.
1458 The number of bytes copied is REGISTER_SIZE (REGNUM).
1460 Returns 0 if the register value could not be found. */
1462 bool
1463 deprecated_frame_register_read (frame_info_ptr frame, int regnum,
1464 gdb_byte *myaddr)
1466 int optimized;
1467 int unavailable;
1468 enum lval_type lval;
1469 CORE_ADDR addr;
1470 int realnum;
1472 frame_register (frame, regnum, &optimized, &unavailable,
1473 &lval, &addr, &realnum, myaddr);
1475 return !optimized && !unavailable;
1478 bool
1479 get_frame_register_bytes (frame_info_ptr frame, int regnum,
1480 CORE_ADDR offset,
1481 gdb::array_view<gdb_byte> buffer,
1482 int *optimizedp, int *unavailablep)
1484 struct gdbarch *gdbarch = get_frame_arch (frame);
1485 int i;
1486 int maxsize;
1487 int numregs;
1489 /* Skip registers wholly inside of OFFSET. */
1490 while (offset >= register_size (gdbarch, regnum))
1492 offset -= register_size (gdbarch, regnum);
1493 regnum++;
1496 /* Ensure that we will not read beyond the end of the register file.
1497 This can only ever happen if the debug information is bad. */
1498 maxsize = -offset;
1499 numregs = gdbarch_num_cooked_regs (gdbarch);
1500 for (i = regnum; i < numregs; i++)
1502 int thissize = register_size (gdbarch, i);
1504 if (thissize == 0)
1505 break; /* This register is not available on this architecture. */
1506 maxsize += thissize;
1509 int len = buffer.size ();
1510 if (len > maxsize)
1511 error (_("Bad debug information detected: "
1512 "Attempt to read %d bytes from registers."), len);
1514 /* Copy the data. */
1515 while (len > 0)
1517 int curr_len = register_size (gdbarch, regnum) - offset;
1519 if (curr_len > len)
1520 curr_len = len;
1522 gdb_byte *myaddr = buffer.data ();
1524 if (curr_len == register_size (gdbarch, regnum))
1526 enum lval_type lval;
1527 CORE_ADDR addr;
1528 int realnum;
1530 frame_register (frame, regnum, optimizedp, unavailablep,
1531 &lval, &addr, &realnum, myaddr);
1532 if (*optimizedp || *unavailablep)
1533 return false;
1535 else
1537 struct value *value
1538 = frame_unwind_register_value (frame_info_ptr (frame->next),
1539 regnum);
1540 gdb_assert (value != NULL);
1541 *optimizedp = value->optimized_out ();
1542 *unavailablep = !value->entirely_available ();
1544 if (*optimizedp || *unavailablep)
1546 release_value (value);
1547 return false;
1550 memcpy (myaddr, value->contents_all ().data () + offset,
1551 curr_len);
1552 release_value (value);
1555 myaddr += curr_len;
1556 len -= curr_len;
1557 offset = 0;
1558 regnum++;
1561 *optimizedp = 0;
1562 *unavailablep = 0;
1564 return true;
1567 void
1568 put_frame_register_bytes (frame_info_ptr frame, int regnum,
1569 CORE_ADDR offset,
1570 gdb::array_view<const gdb_byte> buffer)
1572 struct gdbarch *gdbarch = get_frame_arch (frame);
1574 /* Skip registers wholly inside of OFFSET. */
1575 while (offset >= register_size (gdbarch, regnum))
1577 offset -= register_size (gdbarch, regnum);
1578 regnum++;
1581 int len = buffer.size ();
1582 /* Copy the data. */
1583 while (len > 0)
1585 int curr_len = register_size (gdbarch, regnum) - offset;
1587 if (curr_len > len)
1588 curr_len = len;
1590 const gdb_byte *myaddr = buffer.data ();
1591 if (curr_len == register_size (gdbarch, regnum))
1593 put_frame_register (frame, regnum, myaddr);
1595 else
1597 struct value *value
1598 = frame_unwind_register_value (frame_info_ptr (frame->next),
1599 regnum);
1600 gdb_assert (value != NULL);
1602 memcpy ((char *) value->contents_writeable ().data () + offset,
1603 myaddr, curr_len);
1604 put_frame_register (frame, regnum,
1605 value->contents_raw ().data ());
1606 release_value (value);
1609 myaddr += curr_len;
1610 len -= curr_len;
1611 offset = 0;
1612 regnum++;
1616 /* Create a sentinel frame.
1618 See frame_id_build_sentinel for the description of STACK_ADDR and
1619 CODE_ADDR. */
1621 static frame_info_ptr
1622 create_sentinel_frame (struct program_space *pspace, struct regcache *regcache,
1623 CORE_ADDR stack_addr, CORE_ADDR code_addr)
1625 frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1627 frame->level = -1;
1628 frame->pspace = pspace;
1629 frame->aspace = regcache->aspace ();
1630 /* Explicitly initialize the sentinel frame's cache. Provide it
1631 with the underlying regcache. In the future additional
1632 information, such as the frame's thread will be added. */
1633 frame->prologue_cache = sentinel_frame_cache (regcache);
1634 /* For the moment there is only one sentinel frame implementation. */
1635 frame->unwind = &sentinel_frame_unwind;
1636 /* Link this frame back to itself. The frame is self referential
1637 (the unwound PC is the same as the pc), so make it so. */
1638 frame->next = frame;
1639 /* The sentinel frame has a special ID. */
1640 frame->this_id.p = frame_id_status::COMPUTED;
1641 frame->this_id.value = frame_id_build_sentinel (stack_addr, code_addr);
1643 bool added = frame_stash_add (frame);
1644 gdb_assert (added);
1646 frame_debug_printf (" -> %s", frame->to_string ().c_str ());
1648 return frame_info_ptr (frame);
1651 /* Cache for frame addresses already read by gdb. Valid only while
1652 inferior is stopped. Control variables for the frame cache should
1653 be local to this module. */
1655 static struct obstack frame_cache_obstack;
1657 void *
1658 frame_obstack_zalloc (unsigned long size)
1660 void *data = obstack_alloc (&frame_cache_obstack, size);
1662 memset (data, 0, size);
1663 return data;
1666 static frame_info_ptr get_prev_frame_always_1 (frame_info_ptr this_frame);
1668 frame_info_ptr
1669 get_current_frame (void)
1671 frame_info_ptr current_frame;
1673 /* First check, and report, the lack of registers. Having GDB
1674 report "No stack!" or "No memory" when the target doesn't even
1675 have registers is very confusing. Besides, "printcmd.exp"
1676 explicitly checks that ``print $pc'' with no registers prints "No
1677 registers". */
1678 if (!target_has_registers ())
1679 error (_("No registers."));
1680 if (!target_has_stack ())
1681 error (_("No stack."));
1682 if (!target_has_memory ())
1683 error (_("No memory."));
1684 /* Traceframes are effectively a substitute for the live inferior. */
1685 if (get_traceframe_number () < 0)
1686 validate_registers_access ();
1688 if (sentinel_frame == NULL)
1689 sentinel_frame =
1690 create_sentinel_frame (current_program_space, get_current_regcache (),
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 (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 get_current_regcache (),
2025 id.stack_addr, id.code_addr).get ();
2027 /* Set/update this frame's cached PC value, found in the next frame.
2028 Do this before looking for this frame's unwinder. A sniffer is
2029 very likely to read this, and the corresponding unwinder is
2030 entitled to rely that the PC doesn't magically change. */
2031 fi->next->prev_pc.value = id.code_addr;
2032 fi->next->prev_pc.status = CC_VALUE;
2034 /* We currently assume that frame chain's can't cross spaces. */
2035 fi->pspace = fi->next->pspace;
2036 fi->aspace = fi->next->aspace;
2038 /* Select/initialize both the unwind function and the frame's type
2039 based on the PC. */
2040 frame_unwind_find_by_frame (frame_info_ptr (fi), &fi->prologue_cache);
2042 fi->this_id.p = frame_id_status::COMPUTED;
2043 fi->this_id.value = id;
2045 bool added = frame_stash_add (fi);
2046 gdb_assert (added);
2048 frame_debug_printf (" -> %s", fi->to_string ().c_str ());
2050 return frame_info_ptr (fi);
2053 frame_info_ptr
2054 create_new_frame (CORE_ADDR stack, CORE_ADDR pc)
2056 frame_id id = frame_id_build (stack, pc);
2057 id.user_created_p = 1;
2059 return create_new_frame (id);
2062 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
2063 innermost frame). Be careful to not fall off the bottom of the
2064 frame chain and onto the sentinel frame. */
2066 frame_info_ptr
2067 get_next_frame (frame_info_ptr this_frame)
2069 if (this_frame->level > 0)
2070 return frame_info_ptr (this_frame->next);
2071 else
2072 return NULL;
2075 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
2076 innermost (i.e. current) frame, return the sentinel frame. Thus,
2077 unlike get_next_frame(), NULL will never be returned. */
2079 frame_info_ptr
2080 get_next_frame_sentinel_okay (frame_info_ptr this_frame)
2082 gdb_assert (this_frame != NULL);
2084 /* Note that, due to the manner in which the sentinel frame is
2085 constructed, this_frame->next still works even when this_frame
2086 is the sentinel frame. But we disallow it here anyway because
2087 calling get_next_frame_sentinel_okay() on the sentinel frame
2088 is likely a coding error. */
2089 if (this_frame->this_id.p == frame_id_status::COMPUTED)
2090 gdb_assert (!is_sentinel_frame_id (this_frame->this_id.value));
2092 return frame_info_ptr (this_frame->next);
2095 /* Observer for the target_changed event. */
2097 static void
2098 frame_observer_target_changed (struct target_ops *target)
2100 reinit_frame_cache ();
2103 /* Flush the entire frame cache. */
2105 void
2106 reinit_frame_cache (void)
2108 ++frame_cache_generation;
2110 if (htab_elements (frame_stash) > 0)
2111 annotate_frames_invalid ();
2113 invalidate_selected_frame ();
2115 /* Invalidate cache. */
2116 if (sentinel_frame != nullptr)
2118 /* If frame 0's id is not computed, it is not in the frame stash, so its
2119 dealloc functions will not be called when emptying the frame stash.
2120 Call frame_info_del manually in that case. */
2121 frame_info *current_frame = sentinel_frame->prev;
2122 if (current_frame != nullptr
2123 && current_frame->this_id.p == frame_id_status::NOT_COMPUTED)
2124 frame_info_del (current_frame);
2126 sentinel_frame = nullptr;
2129 frame_stash_invalidate ();
2131 /* Since we can't really be sure what the first object allocated was. */
2132 obstack_free (&frame_cache_obstack, 0);
2133 obstack_init (&frame_cache_obstack);
2135 for (frame_info_ptr &iter : frame_info_ptr::frame_list)
2136 iter.invalidate ();
2138 frame_debug_printf ("generation=%d", frame_cache_generation);
2141 /* Find where a register is saved (in memory or another register).
2142 The result of frame_register_unwind is just where it is saved
2143 relative to this particular frame. */
2145 static void
2146 frame_register_unwind_location (frame_info_ptr this_frame, int regnum,
2147 int *optimizedp, enum lval_type *lvalp,
2148 CORE_ADDR *addrp, int *realnump)
2150 gdb_assert (this_frame == NULL || this_frame->level >= 0);
2152 while (this_frame != NULL)
2154 int unavailable;
2156 frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
2157 lvalp, addrp, realnump, NULL);
2159 if (*optimizedp)
2160 break;
2162 if (*lvalp != lval_register)
2163 break;
2165 regnum = *realnump;
2166 this_frame = get_next_frame (this_frame);
2170 /* Get the previous raw frame, and check that it is not identical to
2171 same other frame frame already in the chain. If it is, there is
2172 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
2173 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
2174 validity tests, that compare THIS_FRAME and the next frame, we do
2175 this right after creating the previous frame, to avoid ever ending
2176 up with two frames with the same id in the frame chain.
2178 There is however, one case where this cycle detection is not desirable,
2179 when asking for the previous frame of an inline frame, in this case, if
2180 the previous frame is a duplicate and we return nullptr then we will be
2181 unable to calculate the frame_id of the inline frame, this in turn
2182 causes inline_frame_this_id() to fail. So for inline frames (and only
2183 for inline frames), the previous frame will always be returned, even when it
2184 has a duplicate frame_id. We're not worried about cycles in the frame
2185 chain as, if the previous frame returned here has a duplicate frame_id,
2186 then the frame_id of the inline frame, calculated based off the frame_id
2187 of the previous frame, should also be a duplicate. */
2189 static frame_info_ptr
2190 get_prev_frame_maybe_check_cycle (frame_info_ptr this_frame)
2192 frame_info_ptr prev_frame = get_prev_frame_raw (this_frame);
2194 /* Don't compute the frame id of the current frame yet. Unwinding
2195 the sentinel frame can fail (e.g., if the thread is gone and we
2196 can't thus read its registers). If we let the cycle detection
2197 code below try to compute a frame ID, then an error thrown from
2198 within the frame ID computation would result in the sentinel
2199 frame as outermost frame, which is bogus. Instead, we'll compute
2200 the current frame's ID lazily in get_frame_id. Note that there's
2201 no point in doing cycle detection when there's only one frame, so
2202 nothing is lost here. */
2203 if (prev_frame->level == 0)
2204 return prev_frame;
2206 unsigned int entry_generation = get_frame_cache_generation ();
2210 compute_frame_id (prev_frame);
2212 bool cycle_detection_p = get_frame_type (this_frame) != INLINE_FRAME;
2214 /* This assert checks GDB's state with respect to calculating the
2215 frame-id of THIS_FRAME, in the case where THIS_FRAME is an inline
2216 frame.
2218 If THIS_FRAME is frame #0, and is an inline frame, then we put off
2219 calculating the frame_id until we specifically make a call to
2220 get_frame_id(). As a result we can enter this function in two
2221 possible states. If GDB asked for the previous frame of frame #0
2222 then THIS_FRAME will be frame #0 (an inline frame), and the
2223 frame_id will be in the NOT_COMPUTED state. However, if GDB asked
2224 for the frame_id of frame #0, then, as getting the frame_id of an
2225 inline frame requires us to get the frame_id of the previous
2226 frame, we will still end up in here, and the frame_id status will
2227 be COMPUTING.
2229 If, instead, THIS_FRAME is at a level greater than #0 then things
2230 are simpler. For these frames we immediately compute the frame_id
2231 when the frame is initially created, and so, for those frames, we
2232 will always enter this function with the frame_id status of
2233 COMPUTING. */
2234 gdb_assert (cycle_detection_p
2235 || (this_frame->level > 0
2236 && (this_frame->this_id.p
2237 == frame_id_status::COMPUTING))
2238 || (this_frame->level == 0
2239 && (this_frame->this_id.p
2240 != frame_id_status::COMPUTED)));
2242 /* We must do the CYCLE_DETECTION_P check after attempting to add
2243 PREV_FRAME into the cache; if PREV_FRAME is unique then we do want
2244 it in the cache, but if it is a duplicate and CYCLE_DETECTION_P is
2245 false, then we don't want to unlink it. */
2246 if (!frame_stash_add (prev_frame.get ()) && cycle_detection_p)
2248 /* Another frame with the same id was already in the stash. We just
2249 detected a cycle. */
2250 frame_debug_printf (" -> nullptr // this frame has same ID");
2252 this_frame->stop_reason = UNWIND_SAME_ID;
2253 /* Unlink. */
2254 prev_frame->next = NULL;
2255 this_frame->prev = NULL;
2256 prev_frame = NULL;
2259 catch (const gdb_exception &ex)
2261 if (get_frame_cache_generation () == entry_generation)
2263 prev_frame->next = NULL;
2264 this_frame->prev = NULL;
2267 throw;
2270 return prev_frame;
2273 /* Helper function for get_prev_frame_always, this is called inside a
2274 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
2275 there is no such frame. This may throw an exception. */
2277 static frame_info_ptr
2278 get_prev_frame_always_1 (frame_info_ptr this_frame)
2280 FRAME_SCOPED_DEBUG_ENTER_EXIT;
2282 gdb_assert (this_frame != NULL);
2284 if (frame_debug)
2286 if (this_frame != NULL)
2287 frame_debug_printf ("this_frame=%d", this_frame->level);
2288 else
2289 frame_debug_printf ("this_frame=nullptr");
2292 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2294 /* Only try to do the unwind once. */
2295 if (this_frame->prev_p)
2297 if (this_frame->prev != nullptr)
2298 frame_debug_printf (" -> %s // cached",
2299 this_frame->prev->to_string ().c_str ());
2300 else
2301 frame_debug_printf
2302 (" -> nullptr // %s // cached",
2303 frame_stop_reason_symbol_string (this_frame->stop_reason));
2304 return frame_info_ptr (this_frame->prev);
2307 /* If the frame unwinder hasn't been selected yet, we must do so
2308 before setting prev_p; otherwise the check for misbehaved
2309 sniffers will think that this frame's sniffer tried to unwind
2310 further (see frame_cleanup_after_sniffer). */
2311 if (this_frame->unwind == NULL)
2312 frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
2314 this_frame->prev_p = true;
2315 this_frame->stop_reason = UNWIND_NO_REASON;
2317 /* If we are unwinding from an inline frame, all of the below tests
2318 were already performed when we unwound from the next non-inline
2319 frame. We must skip them, since we can not get THIS_FRAME's ID
2320 until we have unwound all the way down to the previous non-inline
2321 frame. */
2322 if (get_frame_type (this_frame) == INLINE_FRAME)
2323 return get_prev_frame_maybe_check_cycle (this_frame);
2325 /* If this_frame is the current frame, then compute and stash its
2326 frame id prior to fetching and computing the frame id of the
2327 previous frame. Otherwise, the cycle detection code in
2328 get_prev_frame_if_no_cycle() will not work correctly. When
2329 get_frame_id() is called later on, an assertion error will be
2330 triggered in the event of a cycle between the current frame and
2331 its previous frame.
2333 Note we do this after the INLINE_FRAME check above. That is
2334 because the inline frame's frame id computation needs to fetch
2335 the frame id of its previous real stack frame. I.e., we need to
2336 avoid recursion in that case. This is OK since we're sure the
2337 inline frame won't create a cycle with the real stack frame. See
2338 inline_frame_this_id. */
2339 if (this_frame->level == 0)
2340 get_frame_id (this_frame);
2342 /* Check that this frame is unwindable. If it isn't, don't try to
2343 unwind to the prev frame. */
2344 this_frame->stop_reason
2345 = this_frame->unwind->stop_reason (this_frame,
2346 &this_frame->prologue_cache);
2348 if (this_frame->stop_reason != UNWIND_NO_REASON)
2350 frame_debug_printf
2351 (" -> nullptr // %s",
2352 frame_stop_reason_symbol_string (this_frame->stop_reason));
2353 return NULL;
2356 /* Check that this frame's ID isn't inner to (younger, below, next)
2357 the next frame. This happens when a frame unwind goes backwards.
2358 This check is valid only if this frame and the next frame are NORMAL.
2359 See the comment at frame_id_inner for details. */
2360 if (get_frame_type (this_frame) == NORMAL_FRAME
2361 && this_frame->next->unwind->type == NORMAL_FRAME
2362 && frame_id_inner (get_frame_arch (frame_info_ptr (this_frame->next)),
2363 get_frame_id (this_frame),
2364 get_frame_id (frame_info_ptr (this_frame->next))))
2366 CORE_ADDR this_pc_in_block;
2367 struct minimal_symbol *morestack_msym;
2368 const char *morestack_name = NULL;
2370 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2371 this_pc_in_block = get_frame_address_in_block (this_frame);
2372 morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
2373 if (morestack_msym)
2374 morestack_name = morestack_msym->linkage_name ();
2375 if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
2377 frame_debug_printf (" -> nullptr // this frame ID is inner");
2378 this_frame->stop_reason = UNWIND_INNER_ID;
2379 return NULL;
2383 /* Check that this and the next frame do not unwind the PC register
2384 to the same memory location. If they do, then even though they
2385 have different frame IDs, the new frame will be bogus; two
2386 functions can't share a register save slot for the PC. This can
2387 happen when the prologue analyzer finds a stack adjustment, but
2388 no PC save.
2390 This check does assume that the "PC register" is roughly a
2391 traditional PC, even if the gdbarch_unwind_pc method adjusts
2392 it (we do not rely on the value, only on the unwound PC being
2393 dependent on this value). A potential improvement would be
2394 to have the frame prev_pc method and the gdbarch unwind_pc
2395 method set the same lval and location information as
2396 frame_register_unwind. */
2397 if (this_frame->level > 0
2398 && gdbarch_pc_regnum (gdbarch) >= 0
2399 && get_frame_type (this_frame) == NORMAL_FRAME
2400 && (get_frame_type (frame_info_ptr (this_frame->next)) == NORMAL_FRAME
2401 || get_frame_type (frame_info_ptr (this_frame->next)) == INLINE_FRAME))
2403 int optimized, realnum, nrealnum;
2404 enum lval_type lval, nlval;
2405 CORE_ADDR addr, naddr;
2407 frame_register_unwind_location (this_frame,
2408 gdbarch_pc_regnum (gdbarch),
2409 &optimized, &lval, &addr, &realnum);
2410 frame_register_unwind_location (get_next_frame (this_frame),
2411 gdbarch_pc_regnum (gdbarch),
2412 &optimized, &nlval, &naddr, &nrealnum);
2414 if ((lval == lval_memory && lval == nlval && addr == naddr)
2415 || (lval == lval_register && lval == nlval && realnum == nrealnum))
2417 frame_debug_printf (" -> nullptr // no saved PC");
2418 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
2419 this_frame->prev = NULL;
2420 return NULL;
2424 return get_prev_frame_maybe_check_cycle (this_frame);
2427 /* Return a "struct frame_info" corresponding to the frame that called
2428 THIS_FRAME. Returns NULL if there is no such frame.
2430 Unlike get_prev_frame, this function always tries to unwind the
2431 frame. */
2433 frame_info_ptr
2434 get_prev_frame_always (frame_info_ptr this_frame)
2436 frame_info_ptr prev_frame = NULL;
2440 prev_frame = get_prev_frame_always_1 (this_frame);
2442 catch (const gdb_exception_error &ex)
2444 if (ex.error == MEMORY_ERROR)
2446 this_frame->stop_reason = UNWIND_MEMORY_ERROR;
2447 if (ex.message != NULL)
2449 char *stop_string;
2450 size_t size;
2452 /* The error needs to live as long as the frame does.
2453 Allocate using stack local STOP_STRING then assign the
2454 pointer to the frame, this allows the STOP_STRING on the
2455 frame to be of type 'const char *'. */
2456 size = ex.message->size () + 1;
2457 stop_string = (char *) frame_obstack_zalloc (size);
2458 memcpy (stop_string, ex.what (), size);
2459 this_frame->stop_string = stop_string;
2461 prev_frame = NULL;
2463 else
2464 throw;
2467 return prev_frame;
2470 /* Construct a new "struct frame_info" and link it previous to
2471 this_frame. */
2473 static frame_info_ptr
2474 get_prev_frame_raw (frame_info_ptr this_frame)
2476 frame_info *prev_frame;
2478 /* Allocate the new frame but do not wire it in to the frame chain.
2479 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2480 frame->next to pull some fancy tricks (of course such code is, by
2481 definition, recursive). Try to prevent it.
2483 There is no reason to worry about memory leaks, should the
2484 remainder of the function fail. The allocated memory will be
2485 quickly reclaimed when the frame cache is flushed, and the `we've
2486 been here before' check above will stop repeated memory
2487 allocation calls. */
2488 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
2489 prev_frame->level = this_frame->level + 1;
2491 /* For now, assume we don't have frame chains crossing address
2492 spaces. */
2493 prev_frame->pspace = this_frame->pspace;
2494 prev_frame->aspace = this_frame->aspace;
2496 /* Don't yet compute ->unwind (and hence ->type). It is computed
2497 on-demand in get_frame_type, frame_register_unwind, and
2498 get_frame_id. */
2500 /* Don't yet compute the frame's ID. It is computed on-demand by
2501 get_frame_id(). */
2503 /* The unwound frame ID is validate at the start of this function,
2504 as part of the logic to decide if that frame should be further
2505 unwound, and not here while the prev frame is being created.
2506 Doing this makes it possible for the user to examine a frame that
2507 has an invalid frame ID.
2509 Some very old VAX code noted: [...] For the sake of argument,
2510 suppose that the stack is somewhat trashed (which is one reason
2511 that "info frame" exists). So, return 0 (indicating we don't
2512 know the address of the arglist) if we don't know what frame this
2513 frame calls. */
2515 /* Link it in. */
2516 this_frame->prev = prev_frame;
2517 prev_frame->next = this_frame.get ();
2519 frame_debug_printf (" -> %s", prev_frame->to_string ().c_str ());
2521 return frame_info_ptr (prev_frame);
2524 /* Debug routine to print a NULL frame being returned. */
2526 static void
2527 frame_debug_got_null_frame (frame_info_ptr this_frame,
2528 const char *reason)
2530 if (frame_debug)
2532 if (this_frame != NULL)
2533 frame_debug_printf ("this_frame=%d -> %s", this_frame->level, reason);
2534 else
2535 frame_debug_printf ("this_frame=nullptr -> %s", reason);
2539 /* Is this (non-sentinel) frame in the "main"() function? */
2541 static bool
2542 inside_main_func (frame_info_ptr this_frame)
2544 if (current_program_space->symfile_object_file == nullptr)
2545 return false;
2547 CORE_ADDR sym_addr = 0;
2548 const char *name = main_name ();
2549 bound_minimal_symbol msymbol
2550 = lookup_minimal_symbol (name, NULL,
2551 current_program_space->symfile_object_file);
2553 if (msymbol.minsym != nullptr)
2554 sym_addr = msymbol.value_address ();
2556 /* Favor a full symbol in Fortran, for the case where the Fortran main
2557 is also called "main". */
2558 if (msymbol.minsym == nullptr
2559 || get_frame_language (this_frame) == language_fortran)
2561 /* In some language (for example Fortran) there will be no minimal
2562 symbol with the name of the main function. In this case we should
2563 search the full symbols to see if we can find a match. */
2564 struct block_symbol bs = lookup_symbol (name, NULL, VAR_DOMAIN, 0);
2566 /* We might have found some unrelated symbol. For example, the
2567 Rust compiler can emit both a subprogram and a namespace with
2568 the same name in the same scope; and due to how gdb's symbol
2569 tables currently work, we can't request the one we'd
2570 prefer. */
2571 if (bs.symbol != nullptr && bs.symbol->aclass () == LOC_BLOCK)
2573 const struct block *block = bs.symbol->value_block ();
2574 gdb_assert (block != nullptr);
2575 sym_addr = block->start ();
2577 else if (msymbol.minsym == nullptr)
2578 return false;
2581 /* Convert any function descriptor addresses into the actual function
2582 code address. */
2583 sym_addr = (gdbarch_convert_from_func_ptr_addr
2584 (get_frame_arch (this_frame), sym_addr,
2585 current_inferior ()->top_target ()));
2587 return sym_addr == get_frame_func (this_frame);
2590 /* Test whether THIS_FRAME is inside the process entry point function. */
2592 static bool
2593 inside_entry_func (frame_info_ptr this_frame)
2595 CORE_ADDR entry_point;
2597 if (!entry_point_address_query (&entry_point))
2598 return false;
2600 return get_frame_func (this_frame) == entry_point;
2603 /* Return a structure containing various interesting information about
2604 the frame that called THIS_FRAME. Returns NULL if there is either
2605 no such frame or the frame fails any of a set of target-independent
2606 condition that should terminate the frame chain (e.g., as unwinding
2607 past main()).
2609 This function should not contain target-dependent tests, such as
2610 checking whether the program-counter is zero. */
2612 frame_info_ptr
2613 get_prev_frame (frame_info_ptr this_frame)
2615 FRAME_SCOPED_DEBUG_ENTER_EXIT;
2617 CORE_ADDR frame_pc;
2618 int frame_pc_p;
2620 /* There is always a frame. If this assertion fails, suspect that
2621 something should be calling get_selected_frame() or
2622 get_current_frame(). */
2623 gdb_assert (this_frame != NULL);
2625 frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
2627 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2628 sense to stop unwinding at a dummy frame. One place where a dummy
2629 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2630 pcsqh register (space register for the instruction at the head of the
2631 instruction queue) cannot be written directly; the only way to set it
2632 is to branch to code that is in the target space. In order to implement
2633 frame dummies on HPUX, the called function is made to jump back to where
2634 the inferior was when the user function was called. If gdb was inside
2635 the main function when we created the dummy frame, the dummy frame will
2636 point inside the main function. */
2637 if (this_frame->level >= 0
2638 && get_frame_type (this_frame) == NORMAL_FRAME
2639 && !user_set_backtrace_options.backtrace_past_main
2640 && frame_pc_p
2641 && inside_main_func (this_frame))
2642 /* Don't unwind past main(). Note, this is done _before_ the
2643 frame has been marked as previously unwound. That way if the
2644 user later decides to enable unwinds past main(), that will
2645 automatically happen. */
2647 frame_debug_got_null_frame (this_frame, "inside main func");
2648 return NULL;
2651 /* If the user's backtrace limit has been exceeded, stop. We must
2652 add two to the current level; one of those accounts for backtrace_limit
2653 being 1-based and the level being 0-based, and the other accounts for
2654 the level of the new frame instead of the level of the current
2655 frame. */
2656 if (this_frame->level + 2 > user_set_backtrace_options.backtrace_limit)
2658 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
2659 return NULL;
2662 /* If we're already inside the entry function for the main objfile,
2663 then it isn't valid. Don't apply this test to a dummy frame -
2664 dummy frame PCs typically land in the entry func. Don't apply
2665 this test to the sentinel frame. Sentinel frames should always
2666 be allowed to unwind. */
2667 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2668 wasn't checking for "main" in the minimal symbols. With that
2669 fixed asm-source tests now stop in "main" instead of halting the
2670 backtrace in weird and wonderful ways somewhere inside the entry
2671 file. Suspect that tests for inside the entry file/func were
2672 added to work around that (now fixed) case. */
2673 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2674 suggested having the inside_entry_func test use the
2675 inside_main_func() msymbol trick (along with entry_point_address()
2676 I guess) to determine the address range of the start function.
2677 That should provide a far better stopper than the current
2678 heuristics. */
2679 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2680 applied tail-call optimizations to main so that a function called
2681 from main returns directly to the caller of main. Since we don't
2682 stop at main, we should at least stop at the entry point of the
2683 application. */
2684 if (this_frame->level >= 0
2685 && get_frame_type (this_frame) == NORMAL_FRAME
2686 && !user_set_backtrace_options.backtrace_past_entry
2687 && frame_pc_p
2688 && inside_entry_func (this_frame))
2690 frame_debug_got_null_frame (this_frame, "inside entry func");
2691 return NULL;
2694 /* Assume that the only way to get a zero PC is through something
2695 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2696 will never unwind a zero PC. */
2697 if (this_frame->level > 0
2698 && (get_frame_type (this_frame) == NORMAL_FRAME
2699 || get_frame_type (this_frame) == INLINE_FRAME)
2700 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
2701 && frame_pc_p && frame_pc == 0)
2703 frame_debug_got_null_frame (this_frame, "zero PC");
2704 return NULL;
2707 return get_prev_frame_always (this_frame);
2710 CORE_ADDR
2711 get_frame_pc (frame_info_ptr frame)
2713 gdb_assert (frame->next != NULL);
2714 return frame_unwind_pc (frame_info_ptr (frame->next));
2717 bool
2718 get_frame_pc_if_available (frame_info_ptr frame, CORE_ADDR *pc)
2721 gdb_assert (frame->next != NULL);
2725 *pc = frame_unwind_pc (frame_info_ptr (frame->next));
2727 catch (const gdb_exception_error &ex)
2729 if (ex.error == NOT_AVAILABLE_ERROR)
2730 return false;
2731 else
2732 throw;
2735 return true;
2738 /* Return an address that falls within THIS_FRAME's code block. */
2740 CORE_ADDR
2741 get_frame_address_in_block (frame_info_ptr this_frame)
2743 /* A draft address. */
2744 CORE_ADDR pc = get_frame_pc (this_frame);
2746 frame_info_ptr next_frame (this_frame->next);
2748 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2749 Normally the resume address is inside the body of the function
2750 associated with THIS_FRAME, but there is a special case: when
2751 calling a function which the compiler knows will never return
2752 (for instance abort), the call may be the very last instruction
2753 in the calling function. The resume address will point after the
2754 call and may be at the beginning of a different function
2755 entirely.
2757 If THIS_FRAME is a signal frame or dummy frame, then we should
2758 not adjust the unwound PC. For a dummy frame, GDB pushed the
2759 resume address manually onto the stack. For a signal frame, the
2760 OS may have pushed the resume address manually and invoked the
2761 handler (e.g. GNU/Linux), or invoked the trampoline which called
2762 the signal handler - but in either case the signal handler is
2763 expected to return to the trampoline. So in both of these
2764 cases we know that the resume address is executable and
2765 related. So we only need to adjust the PC if THIS_FRAME
2766 is a normal function.
2768 If the program has been interrupted while THIS_FRAME is current,
2769 then clearly the resume address is inside the associated
2770 function. There are three kinds of interruption: debugger stop
2771 (next frame will be SENTINEL_FRAME), operating system
2772 signal or exception (next frame will be SIGTRAMP_FRAME),
2773 or debugger-induced function call (next frame will be
2774 DUMMY_FRAME). So we only need to adjust the PC if
2775 NEXT_FRAME is a normal function.
2777 We check the type of NEXT_FRAME first, since it is already
2778 known; frame type is determined by the unwinder, and since
2779 we have THIS_FRAME we've already selected an unwinder for
2780 NEXT_FRAME.
2782 If the next frame is inlined, we need to keep going until we find
2783 the real function - for instance, if a signal handler is invoked
2784 while in an inlined function, then the code address of the
2785 "calling" normal function should not be adjusted either. */
2787 while (get_frame_type (next_frame) == INLINE_FRAME)
2788 next_frame = frame_info_ptr (next_frame->next);
2790 if ((get_frame_type (next_frame) == NORMAL_FRAME
2791 || get_frame_type (next_frame) == TAILCALL_FRAME)
2792 && (get_frame_type (this_frame) == NORMAL_FRAME
2793 || get_frame_type (this_frame) == TAILCALL_FRAME
2794 || get_frame_type (this_frame) == INLINE_FRAME))
2795 return pc - 1;
2797 return pc;
2800 bool
2801 get_frame_address_in_block_if_available (frame_info_ptr this_frame,
2802 CORE_ADDR *pc)
2807 *pc = get_frame_address_in_block (this_frame);
2809 catch (const gdb_exception_error &ex)
2811 if (ex.error == NOT_AVAILABLE_ERROR)
2812 return false;
2813 throw;
2816 return true;
2819 symtab_and_line
2820 find_frame_sal (frame_info_ptr frame)
2822 frame_info_ptr next_frame;
2823 int notcurrent;
2824 CORE_ADDR pc;
2826 if (frame_inlined_callees (frame) > 0)
2828 struct symbol *sym;
2830 /* If the current frame has some inlined callees, and we have a next
2831 frame, then that frame must be an inlined frame. In this case
2832 this frame's sal is the "call site" of the next frame's inlined
2833 function, which can not be inferred from get_frame_pc. */
2834 next_frame = get_next_frame (frame);
2835 if (next_frame)
2836 sym = get_frame_function (next_frame);
2837 else
2838 sym = inline_skipped_symbol (inferior_thread ());
2840 /* If frame is inline, it certainly has symbols. */
2841 gdb_assert (sym);
2843 symtab_and_line sal;
2844 if (sym->line () != 0)
2846 sal.symtab = sym->symtab ();
2847 sal.line = sym->line ();
2849 else
2850 /* If the symbol does not have a location, we don't know where
2851 the call site is. Do not pretend to. This is jarring, but
2852 we can't do much better. */
2853 sal.pc = get_frame_pc (frame);
2855 sal.pspace = get_frame_program_space (frame);
2856 return sal;
2859 /* If FRAME is not the innermost frame, that normally means that
2860 FRAME->pc points at the return instruction (which is *after* the
2861 call instruction), and we want to get the line containing the
2862 call (because the call is where the user thinks the program is).
2863 However, if the next frame is either a SIGTRAMP_FRAME or a
2864 DUMMY_FRAME, then the next frame will contain a saved interrupt
2865 PC and such a PC indicates the current (rather than next)
2866 instruction/line, consequently, for such cases, want to get the
2867 line containing fi->pc. */
2868 if (!get_frame_pc_if_available (frame, &pc))
2869 return {};
2871 notcurrent = (pc != get_frame_address_in_block (frame));
2872 return find_pc_line (pc, notcurrent);
2875 /* Per "frame.h", return the ``address'' of the frame. Code should
2876 really be using get_frame_id(). */
2877 CORE_ADDR
2878 get_frame_base (frame_info_ptr fi)
2880 return get_frame_id (fi).stack_addr;
2883 /* High-level offsets into the frame. Used by the debug info. */
2885 CORE_ADDR
2886 get_frame_base_address (frame_info_ptr fi)
2888 if (get_frame_type (fi) != NORMAL_FRAME)
2889 return 0;
2890 if (fi->base == NULL)
2891 fi->base = frame_base_find_by_frame (fi);
2892 /* Sneaky: If the low-level unwind and high-level base code share a
2893 common unwinder, let them share the prologue cache. */
2894 if (fi->base->unwind == fi->unwind)
2895 return fi->base->this_base (fi, &fi->prologue_cache);
2896 return fi->base->this_base (fi, &fi->base_cache);
2899 CORE_ADDR
2900 get_frame_locals_address (frame_info_ptr fi)
2902 if (get_frame_type (fi) != NORMAL_FRAME)
2903 return 0;
2904 /* If there isn't a frame address method, find it. */
2905 if (fi->base == NULL)
2906 fi->base = frame_base_find_by_frame (fi);
2907 /* Sneaky: If the low-level unwind and high-level base code share a
2908 common unwinder, let them share the prologue cache. */
2909 if (fi->base->unwind == fi->unwind)
2910 return fi->base->this_locals (fi, &fi->prologue_cache);
2911 return fi->base->this_locals (fi, &fi->base_cache);
2914 CORE_ADDR
2915 get_frame_args_address (frame_info_ptr fi)
2917 if (get_frame_type (fi) != NORMAL_FRAME)
2918 return 0;
2919 /* If there isn't a frame address method, find it. */
2920 if (fi->base == NULL)
2921 fi->base = frame_base_find_by_frame (fi);
2922 /* Sneaky: If the low-level unwind and high-level base code share a
2923 common unwinder, let them share the prologue cache. */
2924 if (fi->base->unwind == fi->unwind)
2925 return fi->base->this_args (fi, &fi->prologue_cache);
2926 return fi->base->this_args (fi, &fi->base_cache);
2929 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2930 otherwise. */
2932 bool
2933 frame_unwinder_is (frame_info_ptr fi, const frame_unwind *unwinder)
2935 if (fi->unwind == nullptr)
2936 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
2938 return fi->unwind == unwinder;
2941 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2942 or -1 for a NULL frame. */
2945 frame_relative_level (frame_info_ptr fi)
2947 if (fi == NULL)
2948 return -1;
2949 else
2950 return fi->level;
2953 enum frame_type
2954 get_frame_type (frame_info_ptr frame)
2956 if (frame->unwind == NULL)
2957 /* Initialize the frame's unwinder because that's what
2958 provides the frame's type. */
2959 frame_unwind_find_by_frame (frame, &frame->prologue_cache);
2960 return frame->unwind->type;
2963 struct program_space *
2964 get_frame_program_space (frame_info_ptr frame)
2966 return frame->pspace;
2969 struct program_space *
2970 frame_unwind_program_space (frame_info_ptr this_frame)
2972 gdb_assert (this_frame);
2974 /* This is really a placeholder to keep the API consistent --- we
2975 assume for now that we don't have frame chains crossing
2976 spaces. */
2977 return this_frame->pspace;
2980 const address_space *
2981 get_frame_address_space (frame_info_ptr frame)
2983 return frame->aspace;
2986 /* Memory access methods. */
2988 void
2989 get_frame_memory (frame_info_ptr this_frame, CORE_ADDR addr,
2990 gdb::array_view<gdb_byte> buffer)
2992 read_memory (addr, buffer.data (), buffer.size ());
2995 LONGEST
2996 get_frame_memory_signed (frame_info_ptr this_frame, CORE_ADDR addr,
2997 int len)
2999 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3000 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3002 return read_memory_integer (addr, len, byte_order);
3005 ULONGEST
3006 get_frame_memory_unsigned (frame_info_ptr this_frame, CORE_ADDR addr,
3007 int len)
3009 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3010 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3012 return read_memory_unsigned_integer (addr, len, byte_order);
3015 bool
3016 safe_frame_unwind_memory (frame_info_ptr this_frame,
3017 CORE_ADDR addr, gdb::array_view<gdb_byte> buffer)
3019 /* NOTE: target_read_memory returns zero on success! */
3020 return target_read_memory (addr, buffer.data (), buffer.size ()) == 0;
3023 /* Architecture methods. */
3025 struct gdbarch *
3026 get_frame_arch (frame_info_ptr this_frame)
3028 return frame_unwind_arch (frame_info_ptr (this_frame->next));
3031 struct gdbarch *
3032 frame_unwind_arch (frame_info_ptr next_frame)
3034 if (!next_frame->prev_arch.p)
3036 struct gdbarch *arch;
3038 if (next_frame->unwind == NULL)
3039 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
3041 if (next_frame->unwind->prev_arch != NULL)
3042 arch = next_frame->unwind->prev_arch (next_frame,
3043 &next_frame->prologue_cache);
3044 else
3045 arch = get_frame_arch (next_frame);
3047 next_frame->prev_arch.arch = arch;
3048 next_frame->prev_arch.p = true;
3049 frame_debug_printf ("next_frame=%d -> %s",
3050 next_frame->level,
3051 gdbarch_bfd_arch_info (arch)->printable_name);
3054 return next_frame->prev_arch.arch;
3057 struct gdbarch *
3058 frame_unwind_caller_arch (frame_info_ptr next_frame)
3060 next_frame = skip_artificial_frames (next_frame);
3062 /* We must have a non-artificial frame. The caller is supposed to check
3063 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
3064 in this case. */
3065 gdb_assert (next_frame != NULL);
3067 return frame_unwind_arch (next_frame);
3070 /* Gets the language of FRAME. */
3072 enum language
3073 get_frame_language (frame_info_ptr frame)
3075 CORE_ADDR pc = 0;
3076 bool pc_p = false;
3078 gdb_assert (frame!= NULL);
3080 /* We determine the current frame language by looking up its
3081 associated symtab. To retrieve this symtab, we use the frame
3082 PC. However we cannot use the frame PC as is, because it
3083 usually points to the instruction following the "call", which
3084 is sometimes the first instruction of another function. So
3085 we rely on get_frame_address_in_block(), it provides us with
3086 a PC that is guaranteed to be inside the frame's code
3087 block. */
3091 pc = get_frame_address_in_block (frame);
3092 pc_p = true;
3094 catch (const gdb_exception_error &ex)
3096 if (ex.error != NOT_AVAILABLE_ERROR)
3097 throw;
3100 if (pc_p)
3102 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
3104 if (cust != NULL)
3105 return cust->language ();
3108 return language_unknown;
3111 /* Stack pointer methods. */
3113 CORE_ADDR
3114 get_frame_sp (frame_info_ptr this_frame)
3116 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3118 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
3119 operate on THIS_FRAME now. */
3120 return gdbarch_unwind_sp (gdbarch, frame_info_ptr (this_frame->next));
3123 /* Return the reason why we can't unwind past FRAME. */
3125 enum unwind_stop_reason
3126 get_frame_unwind_stop_reason (frame_info_ptr frame)
3128 /* Fill-in STOP_REASON. */
3129 get_prev_frame_always (frame);
3130 gdb_assert (frame->prev_p);
3132 return frame->stop_reason;
3135 /* Return a string explaining REASON. */
3137 const char *
3138 unwind_stop_reason_to_string (enum unwind_stop_reason reason)
3140 switch (reason)
3142 #define SET(name, description) \
3143 case name: return _(description);
3144 #include "unwind_stop_reasons.def"
3145 #undef SET
3147 default:
3148 internal_error ("Invalid frame stop reason");
3152 const char *
3153 frame_stop_reason_string (frame_info_ptr fi)
3155 gdb_assert (fi->prev_p);
3156 gdb_assert (fi->prev == NULL);
3158 /* Return the specific string if we have one. */
3159 if (fi->stop_string != NULL)
3160 return fi->stop_string;
3162 /* Return the generic string if we have nothing better. */
3163 return unwind_stop_reason_to_string (fi->stop_reason);
3166 /* Return the enum symbol name of REASON as a string, to use in debug
3167 output. */
3169 static const char *
3170 frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
3172 switch (reason)
3174 #define SET(name, description) \
3175 case name: return #name;
3176 #include "unwind_stop_reasons.def"
3177 #undef SET
3179 default:
3180 internal_error ("Invalid frame stop reason");
3184 /* Clean up after a failed (wrong unwinder) attempt to unwind past
3185 FRAME. */
3187 void
3188 frame_cleanup_after_sniffer (frame_info_ptr frame)
3190 /* The sniffer should not allocate a prologue cache if it did not
3191 match this frame. */
3192 gdb_assert (frame->prologue_cache == NULL);
3194 /* No sniffer should extend the frame chain; sniff based on what is
3195 already certain. */
3196 gdb_assert (!frame->prev_p);
3198 /* The sniffer should not check the frame's ID; that's circular. */
3199 gdb_assert (frame->this_id.p != frame_id_status::COMPUTED);
3201 /* Clear cached fields dependent on the unwinder.
3203 The previous PC is independent of the unwinder, but the previous
3204 function is not (see get_frame_address_in_block). */
3205 frame->prev_func.status = CC_UNKNOWN;
3206 frame->prev_func.addr = 0;
3208 /* Discard the unwinder last, so that we can easily find it if an assertion
3209 in this function triggers. */
3210 frame->unwind = NULL;
3213 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
3214 If sniffing fails, the caller should be sure to call
3215 frame_cleanup_after_sniffer. */
3217 void
3218 frame_prepare_for_sniffer (frame_info_ptr frame,
3219 const struct frame_unwind *unwind)
3221 gdb_assert (frame->unwind == NULL);
3222 frame->unwind = unwind;
3225 static struct cmd_list_element *set_backtrace_cmdlist;
3226 static struct cmd_list_element *show_backtrace_cmdlist;
3228 /* Definition of the "set backtrace" settings that are exposed as
3229 "backtrace" command options. */
3231 using boolean_option_def
3232 = gdb::option::boolean_option_def<set_backtrace_options>;
3234 const gdb::option::option_def set_backtrace_option_defs[] = {
3236 boolean_option_def {
3237 "past-main",
3238 [] (set_backtrace_options *opt) { return &opt->backtrace_past_main; },
3239 show_backtrace_past_main, /* show_cmd_cb */
3240 N_("Set whether backtraces should continue past \"main\"."),
3241 N_("Show whether backtraces should continue past \"main\"."),
3242 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
3243 the backtrace at \"main\". Set this if you need to see the rest\n\
3244 of the stack trace."),
3247 boolean_option_def {
3248 "past-entry",
3249 [] (set_backtrace_options *opt) { return &opt->backtrace_past_entry; },
3250 show_backtrace_past_entry, /* show_cmd_cb */
3251 N_("Set whether backtraces should continue past the entry point of a program."),
3252 N_("Show whether backtraces should continue past the entry point of a program."),
3253 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
3254 will terminate the backtrace there. Set this if you need to see\n\
3255 the rest of the stack trace."),
3259 /* Implement the 'maintenance print frame-id' command. */
3261 static void
3262 maintenance_print_frame_id (const char *args, int from_tty)
3264 frame_info_ptr frame;
3266 /* Use the currently selected frame, or select a frame based on the level
3267 number passed by the user. */
3268 if (args == nullptr)
3269 frame = get_selected_frame ("No frame selected");
3270 else
3272 int level = value_as_long (parse_and_eval (args));
3273 frame = find_relative_frame (get_current_frame (), &level);
3276 /* Print the frame-id. */
3277 gdb_assert (frame != nullptr);
3278 gdb_printf ("frame-id for frame #%d: %s\n",
3279 frame_relative_level (frame),
3280 get_frame_id (frame).to_string ().c_str ());
3283 /* See frame-info-ptr.h. */
3285 frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
3286 : m_ptr (ptr)
3288 frame_list.push_back (*this);
3290 if (m_ptr == nullptr)
3291 return;
3293 m_cached_level = ptr->level;
3295 if (m_cached_level != 0 || m_ptr->this_id.value.user_created_p)
3296 m_cached_id = m_ptr->this_id.value;
3299 /* See frame-info-ptr.h. */
3301 frame_info *
3302 frame_info_ptr::reinflate () const
3304 /* Ensure we have a valid frame level (sentinel frame or above). */
3305 gdb_assert (m_cached_level >= -1);
3307 if (m_ptr != nullptr)
3309 /* The frame_info wasn't invalidated, no need to reinflate. */
3310 return m_ptr;
3313 if (m_cached_id.user_created_p)
3314 m_ptr = create_new_frame (m_cached_id).get ();
3315 else
3317 /* Frame #0 needs special handling, see comment in select_frame. */
3318 if (m_cached_level == 0)
3319 m_ptr = get_current_frame ().get ();
3320 else
3322 /* If we reach here without a valid frame id, it means we are trying
3323 to reinflate a frame whose id was not know at construction time.
3324 We're probably trying to reinflate a frame while computing its id
3325 which is not possible, and would indicate a problem with GDB. */
3326 gdb_assert (frame_id_p (m_cached_id));
3327 m_ptr = frame_find_by_id (m_cached_id).get ();
3331 gdb_assert (m_ptr != nullptr);
3332 return m_ptr;
3335 void _initialize_frame ();
3336 void
3337 _initialize_frame ()
3339 obstack_init (&frame_cache_obstack);
3341 frame_stash_create ();
3343 gdb::observers::target_changed.attach (frame_observer_target_changed,
3344 "frame");
3346 add_setshow_prefix_cmd ("backtrace", class_maintenance,
3347 _("\
3348 Set backtrace specific variables.\n\
3349 Configure backtrace variables such as the backtrace limit"),
3350 _("\
3351 Show backtrace specific variables.\n\
3352 Show backtrace variables such as the backtrace limit."),
3353 &set_backtrace_cmdlist, &show_backtrace_cmdlist,
3354 &setlist, &showlist);
3356 add_setshow_uinteger_cmd ("limit", class_obscure,
3357 &user_set_backtrace_options.backtrace_limit, _("\
3358 Set an upper bound on the number of backtrace levels."), _("\
3359 Show the upper bound on the number of backtrace levels."), _("\
3360 No more than the specified number of frames can be displayed or examined.\n\
3361 Literal \"unlimited\" or zero means no limit."),
3362 NULL,
3363 show_backtrace_limit,
3364 &set_backtrace_cmdlist,
3365 &show_backtrace_cmdlist);
3367 gdb::option::add_setshow_cmds_for_options
3368 (class_stack, &user_set_backtrace_options,
3369 set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist);
3371 /* Debug this files internals. */
3372 add_setshow_boolean_cmd ("frame", class_maintenance, &frame_debug, _("\
3373 Set frame debugging."), _("\
3374 Show frame debugging."), _("\
3375 When non-zero, frame specific internal debugging is enabled."),
3376 NULL,
3377 show_frame_debug,
3378 &setdebuglist, &showdebuglist);
3380 add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id,
3381 _("Print the current frame-id."),
3382 &maintenanceprintlist);