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/>. */
26 #include "user-regs.h"
27 #include "gdbsupport/gdb_obstack.h"
28 #include "dummy-frame.h"
29 #include "sentinel-frame.h"
33 #include "frame-unwind.h"
34 #include "frame-base.h"
37 #include "observable.h"
39 #include "gdbthread.h"
41 #include "inline-frame.h"
42 #include "tracepoint.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
51 The current frame, which is the innermost frame, can be found at
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;
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. */
84 /* We have a value. */
87 /* Value was not saved. */
90 /* Value is unavailable. */
94 enum class frame_id_status
96 /* Frame id is not computed. */
99 /* Frame id is being computed (compute_frame_id is active). */
102 /* Frame id has been 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. */
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
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. */
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. */
150 struct gdbarch
*arch
;
153 /* Cached copy of the previous frame's resume address. */
155 cached_copy_status status
;
156 /* Did VALUE require unmasking when being read. */
161 /* Cached copy of the previous frame's function address. */
165 cached_copy_status status
;
168 /* This frame's ID. */
172 struct frame_id value
;
175 /* The frame's high-level base methods, and corresponding cache.
176 The high level base methods are selected based on the frame's
178 const struct frame_base
*base
;
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 */
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
;
199 set_frame_previous_pc_masked (frame_info_ptr frame
)
201 frame
->prev_pc
.masked
= true;
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. */
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
;
233 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
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
);
253 /* Internal equality function for the hash table. This function
254 defers equality operations to frame_id::operator==. */
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. */
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. */
283 frame_stash_create (void)
285 frame_stash
= htab_create
286 (100, frame_addr_hash
, frame_addr_hash_eq
,
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
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
,
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)
318 /* Internal function to search the frame stash for an entry with the
319 given frame ID. If found, return that frame. Otherwise return
322 static frame_info_ptr
323 frame_stash_find (struct frame_id id
)
325 struct frame_info dummy
;
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
338 frame_stash_invalidate (void)
340 htab_empty (frame_stash
);
344 scoped_restore_selected_frame::scoped_restore_selected_frame ()
346 m_lang
= current_language
->la_language
;
347 save_selected_frame (&m_fid
, &m_level
);
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. */
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". */
371 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
372 struct cmd_list_element
*c
, const char *value
)
375 _("Whether backtraces should "
376 "continue past \"main\" is %s.\n"),
380 /* Implementation of "show backtrace past-entry". */
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"),
391 /* Implementation of "show backtrace limit". */
394 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
395 struct cmd_list_element
*c
, const char *value
)
398 _("An upper bound on the number "
399 "of backtrace levels is %s.\n"),
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
)
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>";
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
427 return std::string (n
) + "=" + core_addr_to_string (a
);
429 return std::string ("!") + std::string (n
);
432 res
+= (std::string (",")
433 + field_to_string ("code", id
.code_addr_p
, id
.code_addr
)
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
);
446 frame_type_str (frame_type type
)
451 return "NORMAL_FRAME";
454 return "DUMMY_FRAME";
457 return "INLINE_FRAME";
460 return "TAILCALL_FRAME";
463 return "SIGTRAMP_FRAME";
469 return "SENTINEL_FRAME";
472 return "<unknown type>";
476 /* See struct frame_info. */
479 frame_info::to_string () const
481 const frame_info
*fi
= this;
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
));
490 res
+= "type=<unknown>,";
492 if (fi
->unwind
!= NULL
)
493 res
+= string_printf ("unwinder=\"%s\",", fi
->unwind
->name
);
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>,";
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
));
517 res
+= "func=<unknown>";
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
);
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
);
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
);
580 /* Compute the frame's uniq ID that can be used to, later, re-find the
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
619 if (get_frame_cache_generation () == entry_generation
)
620 fi
->this_id
.p
= frame_id_status::NOT_COMPUTED
;
626 /* Return a frame uniq ID that can be used to, later, re-find the
630 get_frame_id (frame_info_ptr fi
)
633 return null_frame_id
;
635 /* It's always invalid to try to get a frame's id while it is being
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);
649 compute_frame_id (fi
);
651 /* Since this is the first frame in the chain, this should
653 bool stashed
= frame_stash_add (fi
.get ());
654 gdb_assert (stashed
);
657 return fi
->this_id
.value
;
661 get_stack_frame_id (frame_info_ptr next_frame
)
663 return get_frame_id (skip_artificial_frames (next_frame
));
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
);
682 return get_frame_id (skip_artificial_frames (this_frame
));
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 };
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;
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;
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;
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;
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
;
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
;
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
);
791 frame_id_artificial_p (frame_id l
)
796 return l
.artificial_depth
!= 0;
800 frame_id::operator== (const frame_id
&r
) const
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. */
809 else if (stack_status
!= r
.stack_status
|| stack_addr
!= r
.stack_addr
)
810 /* If .stack addresses are different, the frames are different. */
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. */
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. */
821 else if (artificial_depth
!= r
.artificial_depth
)
822 /* If artificial depths are different, the frames must be different. */
824 else if (user_created_p
!= r
.user_created_p
)
827 /* Frames are equal. */
830 frame_debug_printf ("l=%s, r=%s -> %d",
831 to_string ().c_str (), r
.to_string ().c_str (), 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
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
850 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
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
869 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
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. */
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. */
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
);
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 (),
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
))
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
);
940 for (frame
= get_current_frame (); ; frame
= prev_frame
)
942 struct frame_id self
= get_frame_id (frame
);
945 /* An exact match. */
948 prev_frame
= get_prev_frame (frame
);
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
)))
967 frame_unwind_pc (frame_info_ptr this_frame
)
969 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
971 struct gdbarch
*prev_gdbarch
;
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
);
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>",
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>",
1020 this_frame
->prev_pc
.value
= pc
;
1021 this_frame
->prev_pc
.status
= CC_VALUE
;
1023 frame_debug_printf ("this_frame=%d -> %s",
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"));
1036 internal_error ("unexpected prev_pc status: %d",
1037 (int) this_frame
->prev_pc
.status
);
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
1048 gdb_assert (this_frame
!= NULL
);
1050 return frame_unwind_pc (this_frame
);
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
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",
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",
1078 hex_string (next_frame
->prev_func
.addr
));
1082 if (next_frame
->prev_func
.status
== CC_UNAVAILABLE
)
1089 gdb_assert (next_frame
->prev_func
.status
== CC_VALUE
);
1091 *pc
= next_frame
->prev_func
.addr
;
1097 get_frame_func (frame_info_ptr this_frame
)
1101 if (!get_frame_func_if_available (this_frame
, &pc
))
1102 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
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
;
1118 std::unique_ptr
<readonly_detached_regcache
> regcache
1119 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
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 ());
1137 /* Ensure that we have a frame to pop to. */
1138 prev_frame
= get_prev_frame_always (this_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
1170 reinit_frame_cache ();
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
);
1204 if (!*optimizedp
&& !*unavailablep
)
1205 memcpy (bufferp
, value
->contents_all ().data (),
1206 value
->type ()->length ());
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. */
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
);
1242 frame_unwind_register (frame_info_ptr next_frame
, int regnum
, gdb_byte
*buf
)
1248 enum lval_type lval
;
1250 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1251 &lval
, &addr
, &realnum
, buf
);
1254 throw_error (OPTIMIZED_OUT_ERROR
,
1255 _("Register %d was not saved"), regnum
);
1257 throw_error (NOT_AVAILABLE_ERROR
,
1258 _("Register %d is not available"), regnum
);
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
);
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
,
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
);
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",
1306 value
->address ()));
1308 gdb_printf (&debug_file
, " computed");
1311 gdb_printf (&debug_file
, " lazy");
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 ());
1332 get_frame_register_value (frame_info_ptr frame
, int regnum
)
1334 return frame_unwind_register_value (frame_info_ptr (frame
->next
), regnum
);
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
);
1364 get_frame_register_signed (frame_info_ptr frame
, int regnum
)
1366 return frame_unwind_register_signed (frame_info_ptr (frame
->next
), regnum
);
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 (),
1393 release_value (value
);
1398 get_frame_register_unsigned (frame_info_ptr frame
, int regnum
)
1400 return frame_unwind_register_unsigned (frame_info_ptr (frame
->next
), regnum
);
1404 read_frame_register_unsigned (frame_info_ptr frame
, int regnum
,
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
,
1425 put_frame_register (frame_info_ptr frame
, int regnum
,
1426 const gdb_byte
*buf
)
1428 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1432 enum lval_type lval
;
1435 frame_register (frame
, regnum
, &optim
, &unavail
,
1436 &lval
, &addr
, &realnum
, NULL
);
1438 error (_("Attempt to assign to a register that was not saved."));
1443 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1447 get_current_regcache ()->cooked_write (realnum
, buf
);
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. */
1463 deprecated_frame_register_read (frame_info_ptr frame
, int regnum
,
1468 enum lval_type lval
;
1472 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1473 &lval
, &addr
, &realnum
, myaddr
);
1475 return !optimized
&& !unavailable
;
1479 get_frame_register_bytes (frame_info_ptr frame
, int regnum
,
1481 gdb::array_view
<gdb_byte
> buffer
,
1482 int *optimizedp
, int *unavailablep
)
1484 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1489 /* Skip registers wholly inside of OFFSET. */
1490 while (offset
>= register_size (gdbarch
, regnum
))
1492 offset
-= register_size (gdbarch
, 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. */
1499 numregs
= gdbarch_num_cooked_regs (gdbarch
);
1500 for (i
= regnum
; i
< numregs
; i
++)
1502 int thissize
= register_size (gdbarch
, i
);
1505 break; /* This register is not available on this architecture. */
1506 maxsize
+= thissize
;
1509 int len
= buffer
.size ();
1511 error (_("Bad debug information detected: "
1512 "Attempt to read %d bytes from registers."), len
);
1514 /* Copy the data. */
1517 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1522 gdb_byte
*myaddr
= buffer
.data ();
1524 if (curr_len
== register_size (gdbarch
, regnum
))
1526 enum lval_type lval
;
1530 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1531 &lval
, &addr
, &realnum
, myaddr
);
1532 if (*optimizedp
|| *unavailablep
)
1538 = frame_unwind_register_value (frame_info_ptr (frame
->next
),
1540 gdb_assert (value
!= NULL
);
1541 *optimizedp
= value
->optimized_out ();
1542 *unavailablep
= !value
->entirely_available ();
1544 if (*optimizedp
|| *unavailablep
)
1546 release_value (value
);
1550 memcpy (myaddr
, value
->contents_all ().data () + offset
,
1552 release_value (value
);
1568 put_frame_register_bytes (frame_info_ptr frame
, int regnum
,
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
);
1581 int len
= buffer
.size ();
1582 /* Copy the data. */
1585 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1590 const gdb_byte
*myaddr
= buffer
.data ();
1591 if (curr_len
== register_size (gdbarch
, regnum
))
1593 put_frame_register (frame
, regnum
, myaddr
);
1598 = frame_unwind_register_value (frame_info_ptr (frame
->next
),
1600 gdb_assert (value
!= NULL
);
1602 memcpy ((char *) value
->contents_writeable ().data () + offset
,
1604 put_frame_register (frame
, regnum
,
1605 value
->contents_raw ().data ());
1606 release_value (value
);
1616 /* Create a sentinel frame.
1618 See frame_id_build_sentinel for the description of STACK_ADDR and
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
);
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
);
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
;
1658 frame_obstack_zalloc (unsigned long size
)
1660 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1662 memset (data
, 0, size
);
1666 static frame_info_ptr
get_prev_frame_always_1 (frame_info_ptr this_frame
);
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
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
)
1690 create_sentinel_frame (current_program_space
, get_current_regcache (),
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
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
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
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
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
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
;
1750 save_selected_frame (frame_id
*frame_id
, int *frame_level
)
1753 *frame_id
= selected_frame_id
;
1754 *frame_level
= selected_frame_level
;
1760 restore_selected_frame (frame_id frame_id
, int frame_level
)
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. */
1785 lookup_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1787 frame_info_ptr frame
= NULL
;
1790 /* This either means there was no selected frame, or the selected
1791 frame was the current frame. In either case, select the current
1793 if (frame_level
== -1)
1795 select_frame (get_current_frame ());
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
));
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
);
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)
1825 && get_frame_id (frame
) == a_frame_id
)
1827 /* Cool, all is fine. */
1828 select_frame (frame
);
1832 frame
= frame_find_by_id (a_frame_id
);
1835 /* Cool, refound it. */
1836 select_frame (frame
);
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:"),
1850 /* For MI, we should probably have a notification about current
1851 frame change. But this error is not very likely, so don't
1853 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1860 if (!target_has_registers () || !target_has_stack ()
1861 || !target_has_memory ())
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
)
1871 thread_info
*tp
= inferior_thread ();
1872 /* Don't try to read from a dead thread. */
1873 if (tp
->state
== THREAD_EXITED
)
1876 /* ... or from a spinning thread. */
1877 if (tp
->executing ())
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(). */
1906 deprecated_safe_get_selected_frame (void)
1908 if (!has_stack_frames ())
1910 return get_selected_frame (NULL
);
1913 /* Invalidate the selected frame. */
1916 invalidate_selected_frame ()
1918 selected_frame
= nullptr;
1919 selected_frame_level
= -1;
1920 selected_frame_id
= null_frame_id
;
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
;
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. */
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
1989 if (get_frame_address_in_block_if_available (fi
, &pc
))
1991 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
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
2017 frame_info_ptr frame
= frame_stash_find (id
);
2018 if (frame
!= nullptr)
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
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
);
2048 frame_debug_printf (" -> %s", fi
->to_string ().c_str ());
2050 return frame_info_ptr (fi
);
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. */
2067 get_next_frame (frame_info_ptr this_frame
)
2069 if (this_frame
->level
> 0)
2070 return frame_info_ptr (this_frame
->next
);
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. */
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. */
2098 frame_observer_target_changed (struct target_ops
*target
)
2100 reinit_frame_cache ();
2103 /* Flush the entire frame cache. */
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
)
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. */
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
)
2156 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
2157 lvalp
, addrp
, realnump
, NULL
);
2162 if (*lvalp
!= lval_register
)
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)
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
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
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
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
;
2254 prev_frame
->next
= NULL
;
2255 this_frame
->prev
= 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
;
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
);
2286 if (this_frame
!= NULL
)
2287 frame_debug_printf ("this_frame=%d", this_frame
->level
);
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 ());
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
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
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
)
2351 (" -> nullptr // %s",
2352 frame_stop_reason_symbol_string (this_frame
->stop_reason
));
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
;
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
;
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
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
;
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
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
)
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
;
2470 /* Construct a new "struct frame_info" and link it previous to
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
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
2500 /* Don't yet compute the frame's ID. It is computed on-demand by
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
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. */
2527 frame_debug_got_null_frame (frame_info_ptr this_frame
,
2532 if (this_frame
!= NULL
)
2533 frame_debug_printf ("this_frame=%d -> %s", this_frame
->level
, reason
);
2535 frame_debug_printf ("this_frame=nullptr -> %s", reason
);
2539 /* Is this (non-sentinel) frame in the "main"() function? */
2542 inside_main_func (frame_info_ptr this_frame
)
2544 if (current_program_space
->symfile_object_file
== nullptr)
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
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)
2581 /* Convert any function descriptor addresses into the actual function
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. */
2593 inside_entry_func (frame_info_ptr this_frame
)
2595 CORE_ADDR entry_point
;
2597 if (!entry_point_address_query (&entry_point
))
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
2609 This function should not contain target-dependent tests, such as
2610 checking whether the program-counter is zero. */
2613 get_prev_frame (frame_info_ptr this_frame
)
2615 FRAME_SCOPED_DEBUG_ENTER_EXIT
;
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
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");
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
2656 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2658 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
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
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
2684 if (this_frame
->level
>= 0
2685 && get_frame_type (this_frame
) == NORMAL_FRAME
2686 && !user_set_backtrace_options
.backtrace_past_entry
2688 && inside_entry_func (this_frame
))
2690 frame_debug_got_null_frame (this_frame
, "inside entry func");
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");
2707 return get_prev_frame_always (this_frame
);
2711 get_frame_pc (frame_info_ptr frame
)
2713 gdb_assert (frame
->next
!= NULL
);
2714 return frame_unwind_pc (frame_info_ptr (frame
->next
));
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
)
2738 /* Return an address that falls within THIS_FRAME's code block. */
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
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
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
))
2801 get_frame_address_in_block_if_available (frame_info_ptr this_frame
,
2807 *pc
= get_frame_address_in_block (this_frame
);
2809 catch (const gdb_exception_error
&ex
)
2811 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2820 find_frame_sal (frame_info_ptr frame
)
2822 frame_info_ptr next_frame
;
2826 if (frame_inlined_callees (frame
) > 0)
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
);
2836 sym
= get_frame_function (next_frame
);
2838 sym
= inline_skipped_symbol (inferior_thread ());
2840 /* If frame is inline, it certainly has symbols. */
2843 symtab_and_line sal
;
2844 if (sym
->line () != 0)
2846 sal
.symtab
= sym
->symtab ();
2847 sal
.line
= sym
->line ();
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
);
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
))
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(). */
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. */
2886 get_frame_base_address (frame_info_ptr fi
)
2888 if (get_frame_type (fi
) != NORMAL_FRAME
)
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
);
2900 get_frame_locals_address (frame_info_ptr fi
)
2902 if (get_frame_type (fi
) != NORMAL_FRAME
)
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
);
2915 get_frame_args_address (frame_info_ptr fi
)
2917 if (get_frame_type (fi
) != NORMAL_FRAME
)
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
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
)
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
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. */
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 ());
2996 get_frame_memory_signed (frame_info_ptr this_frame
, CORE_ADDR addr
,
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
);
3006 get_frame_memory_unsigned (frame_info_ptr this_frame
, CORE_ADDR addr
,
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
);
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. */
3026 get_frame_arch (frame_info_ptr this_frame
)
3028 return frame_unwind_arch (frame_info_ptr (this_frame
->next
));
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
);
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",
3051 gdbarch_bfd_arch_info (arch
)->printable_name
);
3054 return next_frame
->prev_arch
.arch
;
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
3065 gdb_assert (next_frame
!= NULL
);
3067 return frame_unwind_arch (next_frame
);
3070 /* Gets the language of FRAME. */
3073 get_frame_language (frame_info_ptr frame
)
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
3091 pc
= get_frame_address_in_block (frame
);
3094 catch (const gdb_exception_error
&ex
)
3096 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
3102 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
3105 return cust
->language ();
3108 return language_unknown
;
3111 /* Stack pointer methods. */
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. */
3138 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
3142 #define SET(name, description) \
3143 case name: return _(description);
3144 #include "unwind_stop_reasons.def"
3148 internal_error ("Invalid frame stop reason");
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
3170 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
3174 #define SET(name, description) \
3175 case name: return #name;
3176 #include "unwind_stop_reasons.def"
3180 internal_error ("Invalid frame stop reason");
3184 /* Clean up after a failed (wrong unwinder) attempt to unwind past
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
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. */
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
{
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
{
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. */
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");
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
)
3288 frame_list
.push_back (*this);
3290 if (m_ptr
== nullptr)
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. */
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. */
3313 if (m_cached_id
.user_created_p
)
3314 m_ptr
= create_new_frame (m_cached_id
).get ();
3317 /* Frame #0 needs special handling, see comment in select_frame. */
3318 if (m_cached_level
== 0)
3319 m_ptr
= get_current_frame ().get ();
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);
3335 void _initialize_frame ();
3337 _initialize_frame ()
3339 obstack_init (&frame_cache_obstack
);
3341 frame_stash_create ();
3343 gdb::observers::target_changed
.attach (frame_observer_target_changed
,
3346 add_setshow_prefix_cmd ("backtrace", class_maintenance
,
3348 Set backtrace specific variables.\n\
3349 Configure backtrace variables such as the backtrace limit"),
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."),
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."),
3378 &setdebuglist
, &showdebuglist
);
3380 add_cmd ("frame-id", class_maintenance
, maintenance_print_frame_id
,
3381 _("Print the current frame-id."),
3382 &maintenanceprintlist
);