1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "event-top.h"
22 #include "extract-store-integer.h"
27 #include "user-regs.h"
28 #include "gdbsupport/gdb_obstack.h"
29 #include "dummy-frame.h"
30 #include "sentinel-frame.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
37 #include "cli/cli-cmds.h"
38 #include "observable.h"
40 #include "gdbthread.h"
42 #include "inline-frame.h"
43 #include "tracepoint.h"
46 #include "cli/cli-option.h"
47 #include "dwarf2/loc.h"
49 /* The sentinel frame terminates the innermost end of the frame chain.
50 If unwound, it returns the information needed to construct an
53 The current frame, which is the innermost frame, can be found at
56 This is an optimization to be able to find the sentinel frame quickly,
57 it could otherwise be found in the frame cache. */
59 static frame_info
*sentinel_frame
;
61 /* Number of calls to reinit_frame_cache. */
62 static unsigned int frame_cache_generation
= 0;
67 get_frame_cache_generation ()
69 return frame_cache_generation
;
72 /* The values behind the global "set backtrace ..." settings. */
73 set_backtrace_options user_set_backtrace_options
;
75 static frame_info_ptr
get_prev_frame_raw (const frame_info_ptr
&this_frame
);
76 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason
);
77 static frame_info_ptr
create_new_frame (frame_id id
);
79 /* Status of some values cached in the frame_info object. */
81 enum cached_copy_status
83 /* Value is unknown. */
86 /* We have a value. */
89 /* Value was not saved. */
92 /* Value is unavailable. */
96 enum class frame_id_status
98 /* Frame id is not computed. */
101 /* Frame id is being computed (compute_frame_id is active). */
104 /* Frame id has been computed. */
108 /* We keep a cache of stack frames, each of which is a "struct
109 frame_info". The innermost one gets allocated (in
110 wait_for_inferior) each time the inferior stops; sentinel_frame
111 points to it. Additional frames get allocated (in get_prev_frame)
112 as needed, and are chained through the next and prev fields. Any
113 time that the frame cache becomes invalid (most notably when we
114 execute something, but also if we change how we interpret the
115 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
116 which reads new symbols)), we should call reinit_frame_cache. */
120 /* Return a string representation of this frame. */
121 std::string
to_string () const;
123 /* Level of this frame. The inner-most (youngest) frame is at level
124 0. As you move towards the outer-most (oldest) frame, the level
125 increases. This is a cached value. It could just as easily be
126 computed by counting back from the selected frame to the inner
128 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
129 reserved to indicate a bogus frame - one that has been created
130 just to keep GDB happy (GDB always needs a frame). For the
131 moment leave this as speculation. */
134 /* The frame's program space. */
135 struct program_space
*pspace
;
137 /* The frame's address space. */
138 const address_space
*aspace
;
140 /* The frame's low-level unwinder and corresponding cache. The
141 low-level unwinder is responsible for unwinding register values
142 for the previous frame. The low-level unwind methods are
143 selected based on the presence, or otherwise, of register unwind
144 information such as CFI. */
145 void *prologue_cache
;
146 const struct frame_unwind
*unwind
;
148 /* Cached copy of the previous frame's architecture. */
152 struct gdbarch
*arch
;
155 /* Cached copy of the previous frame's resume address. */
157 cached_copy_status status
;
158 /* Did VALUE require unmasking when being read. */
163 /* Cached copy of the previous frame's function address. */
167 cached_copy_status status
;
170 /* This frame's ID. */
174 struct frame_id value
;
177 /* The frame's high-level base methods, and corresponding cache.
178 The high level base methods are selected based on the frame's
180 const struct frame_base
*base
;
183 /* Pointers to the next (down, inner, younger) and previous (up,
184 outer, older) frame_info's in the frame cache. */
185 struct frame_info
*next
; /* down, inner, younger */
187 struct frame_info
*prev
; /* up, outer, older */
189 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
190 could. Only valid when PREV_P is set. */
191 enum unwind_stop_reason stop_reason
;
193 /* A frame specific string describing the STOP_REASON in more detail.
194 Only valid when PREV_P is set, but even then may still be NULL. */
195 const char *stop_string
;
201 set_frame_previous_pc_masked (const frame_info_ptr
&frame
)
203 frame
->prev_pc
.masked
= true;
209 get_frame_pc_masked (const frame_info_ptr
&frame
)
211 gdb_assert (frame
->next
!= nullptr);
212 gdb_assert (frame
->next
->prev_pc
.status
== CC_VALUE
);
214 return frame
->next
->prev_pc
.masked
;
217 /* A frame stash used to speed up frame lookups. Create a hash table
218 to stash frames previously accessed from the frame cache for
219 quicker subsequent retrieval. The hash table is emptied whenever
220 the frame cache is invalidated. */
222 static htab_t frame_stash
;
224 /* Internal function to calculate a hash from the frame_id addresses,
225 using as many valid addresses as possible. Frames below level 0
226 are not stored in the hash table. */
229 frame_addr_hash (const void *ap
)
231 const frame_info
*frame
= (const frame_info
*) ap
;
232 const struct frame_id f_id
= frame
->this_id
.value
;
235 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
237 || f_id
.special_addr_p
);
239 if (f_id
.stack_status
== FID_STACK_VALID
)
240 hash
= iterative_hash (&f_id
.stack_addr
,
241 sizeof (f_id
.stack_addr
), hash
);
242 if (f_id
.code_addr_p
)
243 hash
= iterative_hash (&f_id
.code_addr
,
244 sizeof (f_id
.code_addr
), hash
);
245 if (f_id
.special_addr_p
)
246 hash
= iterative_hash (&f_id
.special_addr
,
247 sizeof (f_id
.special_addr
), hash
);
249 char user_created_p
= f_id
.user_created_p
;
250 hash
= iterative_hash (&user_created_p
, sizeof (user_created_p
), hash
);
255 /* Internal equality function for the hash table. This function
256 defers equality operations to frame_id::operator==. */
259 frame_addr_hash_eq (const void *a
, const void *b
)
261 const frame_info
*f_entry
= (const frame_info
*) a
;
262 const frame_info
*f_element
= (const frame_info
*) b
;
264 return f_entry
->this_id
.value
== f_element
->this_id
.value
;
267 /* Deletion function for the frame cache hash table. */
270 frame_info_del (frame_info
*frame
)
272 if (frame
->prologue_cache
!= nullptr)
273 frame
->unwind
->dealloc_cache (frame
, frame
->prologue_cache
);
275 if (frame
->base_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 (const frame_info_ptr
&initial_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 frame_info_ptr frame
= initial_frame
;
540 while (get_frame_type (frame
) == INLINE_FRAME
541 || get_frame_type (frame
) == TAILCALL_FRAME
)
543 frame
= get_prev_frame_always (frame
);
552 skip_unwritable_frames (const frame_info_ptr
&initial_frame
)
554 frame_info_ptr frame
= initial_frame
;
555 while (gdbarch_code_of_frame_writable (get_frame_arch (frame
), frame
) == 0)
557 frame
= get_prev_frame (frame
);
568 skip_tailcall_frames (const frame_info_ptr
&initial_frame
)
570 frame_info_ptr frame
= initial_frame
;
571 while (get_frame_type (frame
) == TAILCALL_FRAME
)
573 /* Note that for record targets we may get a frame chain that consists of
574 tailcall frames only. */
575 frame
= get_prev_frame (frame
);
583 /* Compute the frame's uniq ID that can be used to, later, re-find the
587 compute_frame_id (const frame_info_ptr
&fi
)
589 FRAME_SCOPED_DEBUG_ENTER_EXIT
;
591 gdb_assert (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
);
593 unsigned int entry_generation
= get_frame_cache_generation ();
597 /* Mark this frame's id as "being computed. */
598 fi
->this_id
.p
= frame_id_status::COMPUTING
;
600 frame_debug_printf ("fi=%d", fi
->level
);
602 /* Find the unwinder. */
603 if (fi
->unwind
== NULL
)
604 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
606 /* Find THIS frame's ID. */
607 /* Default to outermost if no ID is found. */
608 fi
->this_id
.value
= outer_frame_id
;
609 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
610 gdb_assert (frame_id_p (fi
->this_id
.value
));
612 /* Mark this frame's id as "computed". */
613 fi
->this_id
.p
= frame_id_status::COMPUTED
;
615 frame_debug_printf (" -> %s", fi
->this_id
.value
.to_string ().c_str ());
617 catch (const gdb_exception
&ex
)
619 /* On error, revert the frame id status to not computed. If the frame
620 cache generation changed, the frame object doesn't exist anymore, so
622 if (get_frame_cache_generation () == entry_generation
)
623 fi
->this_id
.p
= frame_id_status::NOT_COMPUTED
;
629 /* Return a frame uniq ID that can be used to, later, re-find the
633 get_frame_id (const frame_info_ptr
&fi
)
636 return null_frame_id
;
638 /* It's always invalid to try to get a frame's id while it is being
640 gdb_assert (fi
->this_id
.p
!= frame_id_status::COMPUTING
);
642 if (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
)
644 /* If we haven't computed the frame id yet, then it must be that
645 this is the current frame. Compute it now, and stash the
646 result. The IDs of other frames are computed as soon as
647 they're created, in order to detect cycles. See
648 get_prev_frame_if_no_cycle. */
649 gdb_assert (fi
->level
== 0);
652 compute_frame_id (fi
);
654 /* Since this is the first frame in the chain, this should
656 bool stashed
= frame_stash_add (fi
.get ());
657 gdb_assert (stashed
);
660 return fi
->this_id
.value
;
664 get_stack_frame_id (const frame_info_ptr
&next_frame
)
666 return get_frame_id (skip_artificial_frames (next_frame
));
670 frame_unwind_caller_id (const frame_info_ptr
&initial_next_frame
)
672 /* Use get_prev_frame_always, and not get_prev_frame. The latter
673 will truncate the frame chain, leading to this function
674 unintentionally returning a null_frame_id (e.g., when a caller
675 requests the frame ID of "main()"s caller. */
677 frame_info_ptr next_frame
= skip_artificial_frames (initial_next_frame
);
678 if (next_frame
== NULL
)
679 return null_frame_id
;
681 frame_info_ptr this_frame
= get_prev_frame_always (next_frame
);
683 return get_frame_id (skip_artificial_frames (this_frame
));
685 return null_frame_id
;
688 const struct frame_id null_frame_id
= { 0 }; /* All zeros. */
689 const struct frame_id outer_frame_id
= { 0, 0, 0, FID_STACK_OUTER
, 0, 1, 0 };
692 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
693 CORE_ADDR special_addr
)
695 struct frame_id id
= null_frame_id
;
697 id
.stack_addr
= stack_addr
;
698 id
.stack_status
= FID_STACK_VALID
;
699 id
.code_addr
= code_addr
;
700 id
.code_addr_p
= true;
701 id
.special_addr
= special_addr
;
702 id
.special_addr_p
= true;
709 frame_id_build_unavailable_stack (CORE_ADDR code_addr
)
711 struct frame_id id
= null_frame_id
;
713 id
.stack_status
= FID_STACK_UNAVAILABLE
;
714 id
.code_addr
= code_addr
;
715 id
.code_addr_p
= true;
722 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr
,
723 CORE_ADDR special_addr
)
725 struct frame_id id
= null_frame_id
;
727 id
.stack_status
= FID_STACK_UNAVAILABLE
;
728 id
.code_addr
= code_addr
;
729 id
.code_addr_p
= true;
730 id
.special_addr
= special_addr
;
731 id
.special_addr_p
= true;
736 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
738 struct frame_id id
= null_frame_id
;
740 id
.stack_addr
= stack_addr
;
741 id
.stack_status
= FID_STACK_VALID
;
742 id
.code_addr
= code_addr
;
743 id
.code_addr_p
= true;
748 frame_id_build_wild (CORE_ADDR stack_addr
)
750 struct frame_id id
= null_frame_id
;
752 id
.stack_addr
= stack_addr
;
753 id
.stack_status
= FID_STACK_VALID
;
760 frame_id_build_sentinel (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
762 frame_id id
= null_frame_id
;
764 id
.stack_status
= FID_STACK_SENTINEL
;
765 id
.special_addr_p
= 1;
767 if (stack_addr
!= 0 || code_addr
!= 0)
769 /* The purpose of saving these in the sentinel frame ID is to be able to
770 differentiate the IDs of several sentinel frames that could exist
771 simultaneously in the frame cache. */
772 id
.stack_addr
= stack_addr
;
773 id
.code_addr
= code_addr
;
781 frame_id_p (frame_id l
)
783 /* The frame is valid iff it has a valid stack address. */
784 bool p
= l
.stack_status
!= FID_STACK_INVALID
;
786 frame_debug_printf ("l=%s -> %d", l
.to_string ().c_str (), p
);
792 frame_id_artificial_p (frame_id l
)
797 return l
.artificial_depth
!= 0;
801 frame_id::operator== (const frame_id
&r
) const
805 if (stack_status
== FID_STACK_INVALID
806 || r
.stack_status
== FID_STACK_INVALID
)
807 /* Like a NaN, if either ID is invalid, the result is false.
808 Note that a frame ID is invalid iff it is the null frame ID. */
810 else if (stack_status
!= r
.stack_status
|| stack_addr
!= r
.stack_addr
)
811 /* If .stack addresses are different, the frames are different. */
813 else if (code_addr_p
&& r
.code_addr_p
&& code_addr
!= r
.code_addr
)
814 /* An invalid code addr is a wild card. If .code addresses are
815 different, the frames are different. */
817 else if (special_addr_p
&& r
.special_addr_p
818 && special_addr
!= r
.special_addr
)
819 /* An invalid special addr is a wild card (or unused). Otherwise
820 if special addresses are different, the frames are different. */
822 else if (artificial_depth
!= r
.artificial_depth
)
823 /* If artificial depths are different, the frames must be different. */
825 else if (user_created_p
!= r
.user_created_p
)
828 /* Frames are equal. */
831 frame_debug_printf ("l=%s, r=%s -> %d",
832 to_string ().c_str (), r
.to_string ().c_str (), eq
);
837 /* Safety net to check whether frame ID L should be inner to
838 frame ID R, according to their stack addresses.
840 This method cannot be used to compare arbitrary frames, as the
841 ranges of valid stack addresses may be discontiguous (e.g. due
844 However, it can be used as safety net to discover invalid frame
845 IDs in certain circumstances. Assuming that NEXT is the immediate
846 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
848 * The stack address of NEXT must be inner-than-or-equal to the stack
851 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
854 * If NEXT and THIS have different stack addresses, no other frame
855 in the frame chain may have a stack address in between.
857 Therefore, if frame_id_inner (TEST, THIS) holds, but
858 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
859 to a valid frame in the frame chain.
861 The sanity checks above cannot be performed when a SIGTRAMP frame
862 is involved, because signal handlers might be executed on a different
863 stack than the stack used by the routine that caused the signal
864 to be raised. This can happen for instance when a thread exceeds
865 its maximum stack size. In this case, certain compilers implement
866 a stack overflow strategy that cause the handler to be run on a
870 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
874 if (l
.stack_status
!= FID_STACK_VALID
|| r
.stack_status
!= FID_STACK_VALID
)
875 /* Like NaN, any operation involving an invalid ID always fails.
876 Likewise if either ID has an unavailable stack address. */
878 else if (l
.artificial_depth
> r
.artificial_depth
879 && l
.stack_addr
== r
.stack_addr
880 && l
.code_addr_p
== r
.code_addr_p
881 && l
.special_addr_p
== r
.special_addr_p
882 && l
.special_addr
== r
.special_addr
)
884 /* Same function, different inlined functions. */
885 const struct block
*lb
, *rb
;
887 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
889 lb
= block_for_pc (l
.code_addr
);
890 rb
= block_for_pc (r
.code_addr
);
892 if (lb
== NULL
|| rb
== NULL
)
893 /* Something's gone wrong. */
896 /* This will return true if LB and RB are the same block, or
897 if the block with the smaller depth lexically encloses the
898 block with the greater depth. */
899 inner
= rb
->contains (lb
);
902 /* Only return non-zero when strictly inner than. Note that, per
903 comment in "frame.h", there is some fuzz here. Frameless
904 functions are not strictly inner than (same .stack but
905 different .code and/or .special address). */
906 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
908 frame_debug_printf ("is l=%s inner than r=%s? %d",
909 l
.to_string ().c_str (), r
.to_string ().c_str (),
916 frame_find_by_id (struct frame_id id
)
918 frame_info_ptr frame
, prev_frame
;
920 /* ZERO denotes the null frame, let the caller decide what to do
921 about it. Should it instead return get_current_frame()? */
922 if (!frame_id_p (id
))
925 /* Check for the sentinel frame. */
926 if (id
== frame_id_build_sentinel (0, 0))
927 return frame_info_ptr (sentinel_frame
);
929 /* Try using the frame stash first. Finding it there removes the need
930 to perform the search by looping over all frames, which can be very
931 CPU-intensive if the number of frames is very high (the loop is O(n)
932 and get_prev_frame performs a series of checks that are relatively
933 expensive). This optimization is particularly useful when this function
934 is called from another function (such as value_fetch_lazy, case
935 val->lval () == lval_register) which already loops over all frames,
936 making the overall behavior O(n^2). */
937 frame
= frame_stash_find (id
);
941 for (frame
= get_current_frame (); ; frame
= prev_frame
)
943 struct frame_id self
= get_frame_id (frame
);
946 /* An exact match. */
949 prev_frame
= get_prev_frame (frame
);
953 /* As a safety net to avoid unnecessary backtracing while trying
954 to find an invalid ID, we check for a common situation where
955 we can detect from comparing stack addresses that no other
956 frame in the current frame chain can have this ID. See the
957 comment at frame_id_inner for details. */
958 if (get_frame_type (frame
) == NORMAL_FRAME
959 && !frame_id_inner (get_frame_arch (frame
), id
, self
)
960 && frame_id_inner (get_frame_arch (prev_frame
), id
,
961 get_frame_id (prev_frame
)))
968 frame_unwind_pc (const frame_info_ptr
&this_frame
)
970 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
972 struct gdbarch
*prev_gdbarch
;
976 /* The right way. The `pure' way. The one true way. This
977 method depends solely on the register-unwind code to
978 determine the value of registers in THIS frame, and hence
979 the value of this frame's PC (resume address). A typical
980 implementation is no more than:
982 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
983 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
985 Note: this method is very heavily dependent on a correct
986 register-unwind implementation, it pays to fix that
987 method first; this method is frame type agnostic, since
988 it only deals with register values, it works with any
989 frame. This is all in stark contrast to the old
990 FRAME_SAVED_PC which would try to directly handle all the
991 different ways that a PC could be unwound. */
992 prev_gdbarch
= frame_unwind_arch (this_frame
);
996 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
999 catch (const gdb_exception_error
&ex
)
1001 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1003 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
1005 frame_debug_printf ("this_frame=%d -> <unavailable>",
1008 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
1010 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
1012 frame_debug_printf ("this_frame=%d -> <not saved>",
1021 this_frame
->prev_pc
.value
= pc
;
1022 this_frame
->prev_pc
.status
= CC_VALUE
;
1024 frame_debug_printf ("this_frame=%d -> %s",
1026 hex_string (this_frame
->prev_pc
.value
));
1030 if (this_frame
->prev_pc
.status
== CC_VALUE
)
1031 return this_frame
->prev_pc
.value
;
1032 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
1033 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1034 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
1035 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
1037 internal_error ("unexpected prev_pc status: %d",
1038 (int) this_frame
->prev_pc
.status
);
1042 frame_unwind_caller_pc (const frame_info_ptr
&initial_this_frame
)
1044 frame_info_ptr this_frame
= skip_artificial_frames (initial_this_frame
);
1046 /* We must have a non-artificial frame. The caller is supposed to check
1047 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1049 gdb_assert (this_frame
!= nullptr);
1051 return frame_unwind_pc (this_frame
);
1055 get_frame_func_if_available (const frame_info_ptr
&this_frame
, CORE_ADDR
*pc
)
1057 frame_info
*next_frame
= this_frame
->next
;
1059 if (next_frame
->prev_func
.status
== CC_UNKNOWN
)
1061 CORE_ADDR addr_in_block
;
1063 /* Make certain that this, and not the adjacent, function is
1065 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
1067 next_frame
->prev_func
.status
= CC_UNAVAILABLE
;
1069 frame_debug_printf ("this_frame=%d -> unavailable",
1074 next_frame
->prev_func
.status
= CC_VALUE
;
1075 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
1077 frame_debug_printf ("this_frame=%d -> %s",
1079 hex_string (next_frame
->prev_func
.addr
));
1083 if (next_frame
->prev_func
.status
== CC_UNAVAILABLE
)
1090 gdb_assert (next_frame
->prev_func
.status
== CC_VALUE
);
1092 *pc
= next_frame
->prev_func
.addr
;
1098 get_frame_func (const frame_info_ptr
&this_frame
)
1102 if (!get_frame_func_if_available (this_frame
, &pc
))
1103 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1108 std::unique_ptr
<readonly_detached_regcache
>
1109 frame_save_as_regcache (const frame_info_ptr
&this_frame
)
1111 auto cooked_read
= [this_frame
] (int regnum
, gdb::array_view
<gdb_byte
> buf
)
1113 if (!deprecated_frame_register_read (this_frame
, regnum
, buf
))
1114 return REG_UNAVAILABLE
;
1119 std::unique_ptr
<readonly_detached_regcache
> regcache
1120 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
1126 frame_pop (const frame_info_ptr
&this_frame
)
1128 frame_info_ptr prev_frame
;
1130 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
1132 /* Popping a dummy frame involves restoring more than just registers.
1133 dummy_frame_pop does all the work. */
1134 dummy_frame_pop (get_frame_id (this_frame
), inferior_thread ());
1138 /* Ensure that we have a frame to pop to. */
1139 prev_frame
= get_prev_frame_always (this_frame
);
1142 error (_("Cannot pop the initial frame."));
1144 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1145 entering THISFRAME. */
1146 prev_frame
= skip_tailcall_frames (prev_frame
);
1148 if (prev_frame
== NULL
)
1149 error (_("Cannot find the caller frame."));
1151 /* Make a copy of all the register values unwound from this frame.
1152 Save them in a scratch buffer so that there isn't a race between
1153 trying to extract the old values from the current regcache while
1154 at the same time writing new values into that same cache. */
1155 std::unique_ptr
<readonly_detached_regcache
> scratch
1156 = frame_save_as_regcache (prev_frame
);
1158 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1159 target's register cache that it is about to be hit with a burst
1160 register transfer and that the sequence of register writes should
1161 be batched. The pair target_prepare_to_store() and
1162 target_store_registers() kind of suggest this functionality.
1163 Unfortunately, they don't implement it. Their lack of a formal
1164 definition can lead to targets writing back bogus values
1165 (arguably a bug in the target code mind). */
1166 /* Now copy those saved registers into the current regcache. */
1167 get_thread_regcache (inferior_thread ())->restore (scratch
.get ());
1169 /* We've made right mess of GDB's local state, just discard
1171 reinit_frame_cache ();
1175 frame_register_unwind (const frame_info_ptr
&next_frame
, int regnum
,
1176 int *optimizedp
, int *unavailablep
,
1177 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
1179 gdb::array_view
<gdb_byte
> buffer
)
1181 struct value
*value
;
1183 /* Require all but BUFFER to be valid. An empty BUFFER indicates
1184 that the value proper does not need to be fetched. */
1185 gdb_assert (optimizedp
!= NULL
);
1186 gdb_assert (lvalp
!= NULL
);
1187 gdb_assert (addrp
!= NULL
);
1188 gdb_assert (realnump
!= NULL
);
1190 value
= frame_unwind_register_value (next_frame
, regnum
);
1192 gdb_assert (value
!= NULL
);
1194 *optimizedp
= value
->optimized_out ();
1195 *unavailablep
= !value
->entirely_available ();
1196 *lvalp
= value
->lval ();
1197 *addrp
= value
->address ();
1198 if (*lvalp
== lval_register
)
1199 *realnump
= value
->regnum ();
1203 if (!buffer
.empty ())
1205 gdb_assert (buffer
.size () >= value
->type ()->length ());
1207 if (!*optimizedp
&& !*unavailablep
)
1208 memcpy (buffer
.data (), value
->contents_all ().data (),
1209 value
->type ()->length ());
1211 memset (buffer
.data (), 0, value
->type ()->length ());
1214 /* Dispose of the new value. This prevents watchpoints from
1215 trying to watch the saved frame pointer. */
1216 release_value (value
);
1220 frame_unwind_register (const frame_info_ptr
&next_frame
, int regnum
,
1221 gdb::array_view
<gdb_byte
> buf
)
1227 enum lval_type lval
;
1229 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1230 &lval
, &addr
, &realnum
, buf
);
1233 throw_error (OPTIMIZED_OUT_ERROR
,
1234 _("Register %d was not saved"), regnum
);
1236 throw_error (NOT_AVAILABLE_ERROR
,
1237 _("Register %d is not available"), regnum
);
1241 get_frame_register (const frame_info_ptr
&frame
,
1242 int regnum
, gdb::array_view
<gdb_byte
> buf
)
1244 frame_unwind_register (frame_info_ptr (frame
->next
), regnum
, buf
);
1248 frame_unwind_register_value (const frame_info_ptr
&next_frame
, int regnum
)
1250 FRAME_SCOPED_DEBUG_ENTER_EXIT
;
1252 gdb_assert (next_frame
!= NULL
);
1253 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1254 frame_debug_printf ("frame=%d, regnum=%d(%s)",
1255 next_frame
->level
, regnum
,
1256 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1258 /* Find the unwinder. */
1259 if (next_frame
->unwind
== NULL
)
1260 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
1262 /* Ask this frame to unwind its register. */
1264 = next_frame
->unwind
->prev_register (next_frame
,
1265 &next_frame
->prologue_cache
, regnum
);
1266 if (value
== nullptr)
1268 if (gdbarch_pseudo_register_read_value_p (gdbarch
))
1270 /* This is a pseudo register, we don't know how how what raw registers
1271 this pseudo register is made of. Ask the gdbarch to read the
1272 value, it will itself ask the next frame to unwind the values of
1273 the raw registers it needs to compose the value of the pseudo
1275 value
= gdbarch_pseudo_register_read_value
1276 (gdbarch
, next_frame
, regnum
);
1278 else if (gdbarch_pseudo_register_read_p (gdbarch
))
1280 value
= value::allocate_register (next_frame
, regnum
);
1282 /* Passing the current regcache is known to be broken, the pseudo
1283 register value will be constructed using the current raw registers,
1284 rather than reading them using NEXT_FRAME. Architectures should be
1285 migrated to gdbarch_pseudo_register_read_value. */
1286 register_status status
= gdbarch_pseudo_register_read
1287 (gdbarch
, get_thread_regcache (inferior_thread ()), regnum
,
1288 value
->contents_writeable ().data ());
1289 if (status
== REG_UNAVAILABLE
)
1290 value
->mark_bytes_unavailable (0, value
->type ()->length ());
1293 error (_("Can't unwind value of register %d (%s)"), regnum
,
1294 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1299 string_file debug_file
;
1301 gdb_printf (&debug_file
, " ->");
1302 if (value
->optimized_out ())
1304 gdb_printf (&debug_file
, " ");
1305 val_print_not_saved (&debug_file
);
1309 if (value
->lval () == lval_register
)
1310 gdb_printf (&debug_file
, " register=%d", value
->regnum ());
1311 else if (value
->lval () == lval_memory
)
1312 gdb_printf (&debug_file
, " address=%s",
1314 value
->address ()));
1316 gdb_printf (&debug_file
, " computed");
1319 gdb_printf (&debug_file
, " lazy");
1320 else if (value
->entirely_available ())
1323 gdb::array_view
<const gdb_byte
> buf
= value
->contents ();
1325 gdb_printf (&debug_file
, " bytes=");
1326 gdb_printf (&debug_file
, "[");
1327 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1328 gdb_printf (&debug_file
, "%02x", buf
[i
]);
1329 gdb_printf (&debug_file
, "]");
1331 else if (value
->entirely_unavailable ())
1332 gdb_printf (&debug_file
, " unavailable");
1334 gdb_printf (&debug_file
, " partly unavailable");
1337 frame_debug_printf ("%s", debug_file
.c_str ());
1344 get_frame_register_value (const frame_info_ptr
&frame
, int regnum
)
1346 return frame_unwind_register_value (frame_info_ptr (frame
->next
), regnum
);
1350 frame_unwind_register_signed (const frame_info_ptr
&next_frame
, int regnum
)
1352 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1353 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1354 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1356 gdb_assert (value
!= NULL
);
1358 if (value
->optimized_out ())
1360 throw_error (OPTIMIZED_OUT_ERROR
,
1361 _("Register %d was not saved"), regnum
);
1363 if (!value
->entirely_available ())
1365 throw_error (NOT_AVAILABLE_ERROR
,
1366 _("Register %d is not available"), regnum
);
1369 LONGEST r
= extract_signed_integer (value
->contents_all (), byte_order
);
1371 release_value (value
);
1376 get_frame_register_signed (const frame_info_ptr
&frame
, int regnum
)
1378 return frame_unwind_register_signed (frame_info_ptr (frame
->next
), regnum
);
1382 frame_unwind_register_unsigned (const frame_info_ptr
&next_frame
, int regnum
)
1384 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1385 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1386 int size
= register_size (gdbarch
, regnum
);
1387 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1389 gdb_assert (value
!= NULL
);
1391 if (value
->optimized_out ())
1393 throw_error (OPTIMIZED_OUT_ERROR
,
1394 _("Register %d was not saved"), regnum
);
1396 if (!value
->entirely_available ())
1398 throw_error (NOT_AVAILABLE_ERROR
,
1399 _("Register %d is not available"), regnum
);
1402 ULONGEST r
= extract_unsigned_integer (value
->contents_all ().data (),
1405 release_value (value
);
1410 get_frame_register_unsigned (const frame_info_ptr
&frame
, int regnum
)
1412 return frame_unwind_register_unsigned (frame_info_ptr (frame
->next
), regnum
);
1416 read_frame_register_unsigned (const frame_info_ptr
&frame
, int regnum
,
1419 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1421 if (!regval
->optimized_out ()
1422 && regval
->entirely_available ())
1424 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1425 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1426 int size
= register_size (gdbarch
, regval
->regnum ());
1428 *val
= extract_unsigned_integer (regval
->contents ().data (), size
,
1437 put_frame_register (const frame_info_ptr
&next_frame
, int regnum
,
1438 gdb::array_view
<const gdb_byte
> buf
)
1440 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1444 enum lval_type lval
;
1446 int size
= register_size (gdbarch
, regnum
);
1448 gdb_assert (buf
.size () == size
);
1450 frame_register_unwind (next_frame
, regnum
, &optim
, &unavail
, &lval
, &addr
,
1453 error (_("Attempt to assign to a register that was not saved."));
1458 write_memory (addr
, buf
.data (), size
);
1462 /* Not sure if that's always true... but we have a problem if not. */
1463 gdb_assert (size
== register_size (gdbarch
, realnum
));
1465 if (realnum
< gdbarch_num_regs (gdbarch
)
1466 || !gdbarch_pseudo_register_write_p (gdbarch
))
1467 get_thread_regcache (inferior_thread ())->cooked_write (realnum
, buf
);
1469 gdbarch_pseudo_register_write (gdbarch
, next_frame
, realnum
, buf
);
1472 error (_("Attempt to assign to an unmodifiable value."));
1476 /* This function is deprecated. Use get_frame_register_value instead,
1477 which provides more accurate information.
1479 Find and return the value of REGNUM for the specified stack frame.
1480 The number of bytes copied is REGISTER_SIZE (REGNUM).
1482 Returns 0 if the register value could not be found. */
1485 deprecated_frame_register_read (const frame_info_ptr
&frame
, int regnum
,
1486 gdb::array_view
<gdb_byte
> myaddr
)
1490 enum lval_type lval
;
1494 frame_register_unwind (get_next_frame_sentinel_okay (frame
), regnum
,
1495 &optimized
, &unavailable
, &lval
, &addr
, &realnum
,
1498 return !optimized
&& !unavailable
;
1502 get_frame_register_bytes (const frame_info_ptr
&next_frame
, int regnum
,
1503 CORE_ADDR offset
, gdb::array_view
<gdb_byte
> buffer
,
1504 int *optimizedp
, int *unavailablep
)
1506 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1508 /* Skip registers wholly inside of OFFSET. */
1509 while (offset
>= register_size (gdbarch
, regnum
))
1511 offset
-= register_size (gdbarch
, regnum
);
1515 /* Ensure that we will not read beyond the end of the register file.
1516 This can only ever happen if the debug information is bad. */
1517 int maxsize
= -offset
;
1518 int numregs
= gdbarch_num_cooked_regs (gdbarch
);
1519 for (int i
= regnum
; i
< numregs
; i
++)
1521 int thissize
= register_size (gdbarch
, i
);
1524 break; /* This register is not available on this architecture. */
1525 maxsize
+= thissize
;
1528 if (buffer
.size () > maxsize
)
1529 error (_("Bad debug information detected: "
1530 "Attempt to read %zu bytes from registers."), buffer
.size ());
1532 /* Copy the data. */
1533 while (!buffer
.empty ())
1535 int curr_len
= std::min
<int> (register_size (gdbarch
, regnum
) - offset
,
1538 if (curr_len
== register_size (gdbarch
, regnum
))
1540 enum lval_type lval
;
1544 frame_register_unwind (next_frame
, regnum
, optimizedp
, unavailablep
,
1545 &lval
, &addr
, &realnum
, buffer
);
1546 if (*optimizedp
|| *unavailablep
)
1551 value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1552 gdb_assert (value
!= NULL
);
1553 *optimizedp
= value
->optimized_out ();
1554 *unavailablep
= !value
->entirely_available ();
1556 if (*optimizedp
|| *unavailablep
)
1558 release_value (value
);
1562 copy (value
->contents_all ().slice (offset
, curr_len
),
1563 buffer
.slice (0, curr_len
));
1564 release_value (value
);
1567 buffer
= buffer
.slice (curr_len
);
1579 put_frame_register_bytes (const frame_info_ptr
&next_frame
, int regnum
,
1581 gdb::array_view
<const gdb_byte
> buffer
)
1583 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1585 /* Skip registers wholly inside of OFFSET. */
1586 while (offset
>= register_size (gdbarch
, regnum
))
1588 offset
-= register_size (gdbarch
, regnum
);
1592 /* Copy the data. */
1593 while (!buffer
.empty ())
1595 int curr_len
= std::min
<int> (register_size (gdbarch
, regnum
) - offset
,
1598 if (curr_len
== register_size (gdbarch
, regnum
))
1599 put_frame_register (next_frame
, regnum
, buffer
.slice (0, curr_len
));
1602 value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1603 gdb_assert (value
!= nullptr);
1605 copy (buffer
.slice (0, curr_len
),
1606 value
->contents_writeable ().slice (offset
, curr_len
));
1607 put_frame_register (next_frame
, regnum
, value
->contents_raw ());
1608 release_value (value
);
1611 buffer
= buffer
.slice (curr_len
);
1617 /* Create a sentinel frame.
1619 See frame_id_build_sentinel for the description of STACK_ADDR and
1622 static frame_info_ptr
1623 create_sentinel_frame (program_space
*pspace
, address_space
*aspace
,
1624 regcache
*regcache
, CORE_ADDR stack_addr
,
1625 CORE_ADDR code_addr
)
1627 frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1630 frame
->pspace
= pspace
;
1631 frame
->aspace
= aspace
;
1632 /* Explicitly initialize the sentinel frame's cache. Provide it
1633 with the underlying regcache. In the future additional
1634 information, such as the frame's thread will be added. */
1635 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1636 /* For the moment there is only one sentinel frame implementation. */
1637 frame
->unwind
= &sentinel_frame_unwind
;
1638 /* Link this frame back to itself. The frame is self referential
1639 (the unwound PC is the same as the pc), so make it so. */
1640 frame
->next
= frame
;
1641 /* The sentinel frame has a special ID. */
1642 frame
->this_id
.p
= frame_id_status::COMPUTED
;
1643 frame
->this_id
.value
= frame_id_build_sentinel (stack_addr
, code_addr
);
1645 bool added
= frame_stash_add (frame
);
1648 frame_debug_printf (" -> %s", frame
->to_string ().c_str ());
1650 return frame_info_ptr (frame
);
1653 /* Cache for frame addresses already read by gdb. Valid only while
1654 inferior is stopped. Control variables for the frame cache should
1655 be local to this module. */
1657 static struct obstack frame_cache_obstack
;
1660 frame_obstack_zalloc (unsigned long size
)
1662 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1664 memset (data
, 0, size
);
1668 static frame_info_ptr
get_prev_frame_always_1 (const frame_info_ptr
&this_frame
);
1671 get_current_frame (void)
1673 frame_info_ptr current_frame
;
1675 /* First check, and report, the lack of registers. Having GDB
1676 report "No stack!" or "No memory" when the target doesn't even
1677 have registers is very confusing. Besides, "printcmd.exp"
1678 explicitly checks that ``print $pc'' with no registers prints "No
1680 if (!target_has_registers ())
1681 error (_("No registers."));
1682 if (!target_has_stack ())
1683 error (_("No stack."));
1684 if (!target_has_memory ())
1685 error (_("No memory."));
1686 /* Traceframes are effectively a substitute for the live inferior. */
1687 if (get_traceframe_number () < 0)
1688 validate_registers_access ();
1690 if (sentinel_frame
== NULL
)
1692 create_sentinel_frame (current_program_space
,
1693 current_inferior ()->aspace
.get (),
1694 get_thread_regcache (inferior_thread ()),
1697 /* Set the current frame before computing the frame id, to avoid
1698 recursion inside compute_frame_id, in case the frame's
1699 unwinder decides to do a symbol lookup (which depends on the
1700 selected frame's block).
1702 This call must always succeed. In particular, nothing inside
1703 get_prev_frame_always_1 should try to unwind from the
1704 sentinel frame, because that could fail/throw, and we always
1705 want to leave with the current frame created and linked in --
1706 we should never end up with the sentinel frame as outermost
1708 current_frame
= get_prev_frame_always_1 (frame_info_ptr (sentinel_frame
));
1709 gdb_assert (current_frame
!= NULL
);
1711 return current_frame
;
1714 /* The "selected" stack frame is used by default for local and arg
1717 The "single source of truth" for the selected frame is the
1718 SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL pair.
1720 Frame IDs can be saved/restored across reinitializing the frame
1721 cache, while frame_info pointers can't (frame_info objects are
1722 invalidated). If we know the corresponding frame_info object, it
1723 is cached in SELECTED_FRAME.
1725 If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
1726 and the target has stack and is stopped, the selected frame is the
1727 current (innermost) target frame. SELECTED_FRAME_ID is never the ID
1728 of the current (innermost) target frame. SELECTED_FRAME_LEVEL may
1729 only be 0 if the selected frame is a user-created one (created and
1730 selected through the "select-frame view" command), in which case
1731 SELECTED_FRAME_ID is the frame id derived from the user-provided
1734 If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
1735 and the target has no stack or is executing, then there's no
1737 static frame_id selected_frame_id
= null_frame_id
;
1738 static int selected_frame_level
= -1;
1740 /* See frame.h. This definition should come before any definition of a static
1741 frame_info_ptr, to ensure that frame_list is destroyed after any static
1742 frame_info_ptr. This is necessary because the destructor of frame_info_ptr
1745 intrusive_list
<frame_info_ptr
> frame_info_ptr::frame_list
;
1747 /* The cached frame_info object pointing to the selected frame.
1748 Looked up on demand by get_selected_frame. */
1749 static frame_info_ptr selected_frame
;
1754 save_selected_frame (frame_id
*frame_id
, int *frame_level
)
1757 *frame_id
= selected_frame_id
;
1758 *frame_level
= selected_frame_level
;
1764 restore_selected_frame (frame_id frame_id
, int frame_level
)
1767 /* Unless it is a user-created frame, save_selected_frame never returns
1768 level == 0, so we shouldn't see it here either. */
1769 gdb_assert (frame_level
!= 0 || frame_id
.user_created_p
);
1771 /* FRAME_ID can be null_frame_id only IFF frame_level is -1. */
1772 gdb_assert ((frame_level
== -1 && !frame_id_p (frame_id
))
1773 || (frame_level
!= -1 && frame_id_p (frame_id
)));
1775 selected_frame_id
= frame_id
;
1776 selected_frame_level
= frame_level
;
1778 /* Will be looked up later by get_selected_frame. */
1779 selected_frame
= nullptr;
1782 /* Lookup the frame_info object for the selected frame FRAME_ID /
1783 FRAME_LEVEL and cache the result.
1785 If FRAME_LEVEL > 0 and the originally selected frame isn't found,
1786 warn and select the innermost (current) frame. */
1789 lookup_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1791 frame_info_ptr frame
= NULL
;
1794 /* This either means there was no selected frame, or the selected
1795 frame was the current frame. In either case, select the current
1797 if (frame_level
== -1)
1799 select_frame (get_current_frame ());
1803 /* This means the selected frame was a user-created one. Create a new one
1804 using the user-provided addresses, which happen to be in the frame id. */
1805 if (frame_level
== 0)
1807 gdb_assert (a_frame_id
.user_created_p
);
1808 select_frame (create_new_frame (a_frame_id
));
1812 /* select_frame never saves 0 in SELECTED_FRAME_LEVEL, so we
1813 shouldn't see it here. */
1814 gdb_assert (frame_level
> 0);
1816 /* Restore by level first, check if the frame id is the same as
1817 expected. If that fails, try restoring by frame id. If that
1818 fails, nothing to do, just warn the user. */
1820 count
= frame_level
;
1821 frame
= find_relative_frame (get_current_frame (), &count
);
1824 /* The frame ids must match - either both valid or both
1825 outer_frame_id. The latter case is not failsafe, but since
1826 it's highly unlikely the search by level finds the wrong
1827 frame, it's 99.9(9)% of the time (for all practical purposes)
1829 && get_frame_id (frame
) == a_frame_id
)
1831 /* Cool, all is fine. */
1832 select_frame (frame
);
1836 frame
= frame_find_by_id (a_frame_id
);
1839 /* Cool, refound it. */
1840 select_frame (frame
);
1844 /* Nothing else to do, the frame layout really changed. Select the
1845 innermost stack frame. */
1846 select_frame (get_current_frame ());
1848 /* Warn the user. */
1849 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1851 warning (_("Couldn't restore frame #%d in "
1852 "current thread. Bottom (innermost) frame selected:"),
1854 /* For MI, we should probably have a notification about current
1855 frame change. But this error is not very likely, so don't
1857 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1864 if (!target_has_registers () || !target_has_stack ()
1865 || !target_has_memory ())
1868 /* Traceframes are effectively a substitute for the live inferior. */
1869 if (get_traceframe_number () < 0)
1871 /* No current inferior, no frame. */
1872 if (inferior_ptid
== null_ptid
)
1875 thread_info
*tp
= inferior_thread ();
1876 /* Don't try to read from a dead thread. */
1877 if (tp
->state
== THREAD_EXITED
)
1880 /* ... or from a spinning thread. */
1881 if (tp
->executing ())
1891 get_selected_frame (const char *message
)
1893 if (selected_frame
== NULL
)
1895 if (message
!= NULL
&& !has_stack_frames ())
1896 error (("%s"), message
);
1898 lookup_selected_frame (selected_frame_id
, selected_frame_level
);
1900 /* There is always a frame. */
1901 gdb_assert (selected_frame
!= NULL
);
1902 return selected_frame
;
1905 /* This is a variant of get_selected_frame() which can be called when
1906 the inferior does not have a frame; in that case it will return
1907 NULL instead of calling error(). */
1910 deprecated_safe_get_selected_frame (void)
1912 if (!has_stack_frames ())
1914 return get_selected_frame (NULL
);
1917 /* Invalidate the selected frame. */
1920 invalidate_selected_frame ()
1922 selected_frame
= nullptr;
1923 selected_frame_level
= -1;
1924 selected_frame_id
= null_frame_id
;
1930 select_frame (const frame_info_ptr
&fi
)
1932 gdb_assert (fi
!= nullptr);
1934 selected_frame
= fi
;
1935 selected_frame_level
= frame_relative_level (fi
);
1937 /* If the frame is a user-created one, save its level and frame id just like
1938 any other non-level-0 frame. */
1939 if (selected_frame_level
== 0 && !fi
->this_id
.value
.user_created_p
)
1941 /* Treat the current frame especially -- we want to always
1942 save/restore it without warning, even if the frame ID changes
1943 (see lookup_selected_frame). E.g.:
1945 // The current frame is selected, the target had just stopped.
1947 scoped_restore_selected_frame restore_frame;
1948 some_operation_that_changes_the_stack ();
1950 // scoped_restore_selected_frame's dtor runs, but the
1951 // original frame_id can't be found. No matter whether it
1952 // is found or not, we still end up with the now-current
1953 // frame selected. Warning in lookup_selected_frame in this
1954 // case seems pointless.
1956 Also get_frame_id may access the target's registers/memory,
1957 and thus skipping get_frame_id optimizes the common case.
1959 Saving the selected frame this way makes get_selected_frame
1960 and restore_current_frame return/re-select whatever frame is
1961 the innermost (current) then. */
1962 selected_frame_level
= -1;
1963 selected_frame_id
= null_frame_id
;
1966 selected_frame_id
= get_frame_id (fi
);
1968 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1969 frame is being invalidated. */
1971 /* FIXME: kseitz/2002-08-28: It would be nice to call
1972 selected_frame_level_changed_event() right here, but due to limitations
1973 in the current interfaces, we would end up flooding UIs with events
1974 because select_frame() is used extensively internally.
1976 Once we have frame-parameterized frame (and frame-related) commands,
1977 the event notification can be moved here, since this function will only
1978 be called when the user's selected frame is being changed. */
1980 /* Ensure that symbols for this frame are read in. Also, determine the
1981 source language of this frame, and switch to it if desired. */
1986 /* We retrieve the frame's symtab by using the frame PC.
1987 However we cannot use the frame PC as-is, because it usually
1988 points to the instruction following the "call", which is
1989 sometimes the first instruction of another function. So we
1990 rely on get_frame_address_in_block() which provides us with a
1991 PC which is guaranteed to be inside the frame's code
1993 if (get_frame_address_in_block_if_available (fi
, &pc
))
1995 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
1998 && cust
->language () != current_language
->la_language
1999 && cust
->language () != language_unknown
2000 && language_mode
== language_mode_auto
)
2001 set_language (cust
->language ());
2006 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
2007 Always returns a non-NULL value. */
2009 static frame_info_ptr
2010 create_new_frame (frame_id id
)
2012 gdb_assert (id
.user_created_p
);
2013 gdb_assert (id
.stack_status
== frame_id_stack_status::FID_STACK_VALID
);
2014 gdb_assert (id
.code_addr_p
);
2016 frame_debug_printf ("stack_addr=%s, core_addr=%s",
2017 hex_string (id
.stack_addr
), hex_string (id
.code_addr
));
2019 /* Avoid creating duplicate frames, search for an existing frame with that id
2021 frame_info_ptr frame
= frame_stash_find (id
);
2022 if (frame
!= nullptr)
2025 frame_info
*fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2027 fi
->next
= create_sentinel_frame (current_program_space
,
2028 current_inferior ()->aspace
.get (),
2029 get_thread_regcache (inferior_thread ()),
2030 id
.stack_addr
, id
.code_addr
).get ();
2032 /* Set/update this frame's cached PC value, found in the next frame.
2033 Do this before looking for this frame's unwinder. A sniffer is
2034 very likely to read this, and the corresponding unwinder is
2035 entitled to rely that the PC doesn't magically change. */
2036 fi
->next
->prev_pc
.value
= id
.code_addr
;
2037 fi
->next
->prev_pc
.status
= CC_VALUE
;
2039 /* We currently assume that frame chain's can't cross spaces. */
2040 fi
->pspace
= fi
->next
->pspace
;
2041 fi
->aspace
= fi
->next
->aspace
;
2043 /* Select/initialize both the unwind function and the frame's type
2045 frame_unwind_find_by_frame (frame_info_ptr (fi
), &fi
->prologue_cache
);
2047 fi
->this_id
.p
= frame_id_status::COMPUTED
;
2048 fi
->this_id
.value
= id
;
2050 bool added
= frame_stash_add (fi
);
2053 frame_debug_printf (" -> %s", fi
->to_string ().c_str ());
2055 return frame_info_ptr (fi
);
2059 create_new_frame (CORE_ADDR stack
, CORE_ADDR pc
)
2061 frame_id id
= frame_id_build (stack
, pc
);
2062 id
.user_created_p
= 1;
2064 return create_new_frame (id
);
2067 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
2068 innermost frame). Be careful to not fall off the bottom of the
2069 frame chain and onto the sentinel frame. */
2072 get_next_frame (const frame_info_ptr
&this_frame
)
2074 if (this_frame
->level
> 0)
2075 return frame_info_ptr (this_frame
->next
);
2080 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
2081 innermost (i.e. current) frame, return the sentinel frame. Thus,
2082 unlike get_next_frame(), NULL will never be returned. */
2085 get_next_frame_sentinel_okay (const frame_info_ptr
&this_frame
)
2087 gdb_assert (this_frame
!= NULL
);
2089 /* Note that, due to the manner in which the sentinel frame is
2090 constructed, this_frame->next still works even when this_frame
2091 is the sentinel frame. But we disallow it here anyway because
2092 calling get_next_frame_sentinel_okay() on the sentinel frame
2093 is likely a coding error. */
2094 if (this_frame
->this_id
.p
== frame_id_status::COMPUTED
)
2095 gdb_assert (!is_sentinel_frame_id (this_frame
->this_id
.value
));
2097 return frame_info_ptr (this_frame
->next
);
2100 /* Observer for the target_changed event. */
2103 frame_observer_target_changed (struct target_ops
*target
)
2105 reinit_frame_cache ();
2108 /* Flush the entire frame cache. */
2111 reinit_frame_cache (void)
2113 ++frame_cache_generation
;
2115 if (htab_elements (frame_stash
) > 0)
2116 annotate_frames_invalid ();
2118 invalidate_selected_frame ();
2120 /* Invalidate cache. */
2121 if (sentinel_frame
!= nullptr)
2123 /* If frame 0's id is not computed, it is not in the frame stash, so its
2124 dealloc functions will not be called when emptying the frame stash.
2125 Call frame_info_del manually in that case. */
2126 frame_info
*current_frame
= sentinel_frame
->prev
;
2127 if (current_frame
!= nullptr
2128 && current_frame
->this_id
.p
== frame_id_status::NOT_COMPUTED
)
2129 frame_info_del (current_frame
);
2131 sentinel_frame
= nullptr;
2134 frame_stash_invalidate ();
2136 /* Since we can't really be sure what the first object allocated was. */
2137 obstack_free (&frame_cache_obstack
, 0);
2138 obstack_init (&frame_cache_obstack
);
2140 for (frame_info_ptr
&iter
: frame_info_ptr::frame_list
)
2143 frame_debug_printf ("generation=%d", frame_cache_generation
);
2146 /* Find where a register is saved (in memory or another register).
2147 The result of frame_register_unwind is just where it is saved
2148 relative to this particular frame. */
2151 frame_register_unwind_location (const frame_info_ptr
&initial_this_frame
,
2152 int regnum
, int *optimizedp
, lval_type
*lvalp
,
2153 CORE_ADDR
*addrp
, int *realnump
)
2155 gdb_assert (initial_this_frame
== nullptr || initial_this_frame
->level
>= 0);
2157 frame_info_ptr this_frame
= initial_this_frame
;
2158 while (this_frame
!= NULL
)
2162 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
2163 lvalp
, addrp
, realnump
);
2168 if (*lvalp
!= lval_register
)
2172 this_frame
= get_next_frame (this_frame
);
2176 /* Get the previous raw frame, and check that it is not identical to
2177 same other frame frame already in the chain. If it is, there is
2178 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
2179 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
2180 validity tests, that compare THIS_FRAME and the next frame, we do
2181 this right after creating the previous frame, to avoid ever ending
2182 up with two frames with the same id in the frame chain.
2184 There is however, one case where this cycle detection is not desirable,
2185 when asking for the previous frame of an inline frame, in this case, if
2186 the previous frame is a duplicate and we return nullptr then we will be
2187 unable to calculate the frame_id of the inline frame, this in turn
2188 causes inline_frame_this_id() to fail. So for inline frames (and only
2189 for inline frames), the previous frame will always be returned, even when it
2190 has a duplicate frame_id. We're not worried about cycles in the frame
2191 chain as, if the previous frame returned here has a duplicate frame_id,
2192 then the frame_id of the inline frame, calculated based off the frame_id
2193 of the previous frame, should also be a duplicate. */
2195 static frame_info_ptr
2196 get_prev_frame_maybe_check_cycle (const frame_info_ptr
&this_frame
)
2198 frame_info_ptr prev_frame
= get_prev_frame_raw (this_frame
);
2200 /* Don't compute the frame id of the current frame yet. Unwinding
2201 the sentinel frame can fail (e.g., if the thread is gone and we
2202 can't thus read its registers). If we let the cycle detection
2203 code below try to compute a frame ID, then an error thrown from
2204 within the frame ID computation would result in the sentinel
2205 frame as outermost frame, which is bogus. Instead, we'll compute
2206 the current frame's ID lazily in get_frame_id. Note that there's
2207 no point in doing cycle detection when there's only one frame, so
2208 nothing is lost here. */
2209 if (prev_frame
->level
== 0)
2212 unsigned int entry_generation
= get_frame_cache_generation ();
2216 compute_frame_id (prev_frame
);
2218 bool cycle_detection_p
= get_frame_type (this_frame
) != INLINE_FRAME
;
2220 /* This assert checks GDB's state with respect to calculating the
2221 frame-id of THIS_FRAME, in the case where THIS_FRAME is an inline
2224 If THIS_FRAME is frame #0, and is an inline frame, then we put off
2225 calculating the frame_id until we specifically make a call to
2226 get_frame_id(). As a result we can enter this function in two
2227 possible states. If GDB asked for the previous frame of frame #0
2228 then THIS_FRAME will be frame #0 (an inline frame), and the
2229 frame_id will be in the NOT_COMPUTED state. However, if GDB asked
2230 for the frame_id of frame #0, then, as getting the frame_id of an
2231 inline frame requires us to get the frame_id of the previous
2232 frame, we will still end up in here, and the frame_id status will
2235 If, instead, THIS_FRAME is at a level greater than #0 then things
2236 are simpler. For these frames we immediately compute the frame_id
2237 when the frame is initially created, and so, for those frames, we
2238 will always enter this function with the frame_id status of
2240 gdb_assert (cycle_detection_p
2241 || (this_frame
->level
> 0
2242 && (this_frame
->this_id
.p
2243 == frame_id_status::COMPUTING
))
2244 || (this_frame
->level
== 0
2245 && (this_frame
->this_id
.p
2246 != frame_id_status::COMPUTED
)));
2248 /* We must do the CYCLE_DETECTION_P check after attempting to add
2249 PREV_FRAME into the cache; if PREV_FRAME is unique then we do want
2250 it in the cache, but if it is a duplicate and CYCLE_DETECTION_P is
2251 false, then we don't want to unlink it. */
2252 if (!frame_stash_add (prev_frame
.get ()) && cycle_detection_p
)
2254 /* Another frame with the same id was already in the stash. We just
2255 detected a cycle. */
2256 frame_debug_printf (" -> nullptr // this frame has same ID");
2258 this_frame
->stop_reason
= UNWIND_SAME_ID
;
2260 prev_frame
->next
= NULL
;
2261 this_frame
->prev
= NULL
;
2265 catch (const gdb_exception
&ex
)
2267 if (get_frame_cache_generation () == entry_generation
)
2269 prev_frame
->next
= NULL
;
2270 this_frame
->prev
= NULL
;
2279 /* Helper function for get_prev_frame_always, this is called inside a
2280 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
2281 there is no such frame. This may throw an exception. */
2283 static frame_info_ptr
2284 get_prev_frame_always_1 (const frame_info_ptr
&this_frame
)
2286 FRAME_SCOPED_DEBUG_ENTER_EXIT
;
2288 gdb_assert (this_frame
!= NULL
);
2292 if (this_frame
!= NULL
)
2293 frame_debug_printf ("this_frame=%d", this_frame
->level
);
2295 frame_debug_printf ("this_frame=nullptr");
2298 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2300 /* Only try to do the unwind once. */
2301 if (this_frame
->prev_p
)
2303 if (this_frame
->prev
!= nullptr)
2304 frame_debug_printf (" -> %s // cached",
2305 this_frame
->prev
->to_string ().c_str ());
2308 (" -> nullptr // %s // cached",
2309 frame_stop_reason_symbol_string (this_frame
->stop_reason
));
2310 return frame_info_ptr (this_frame
->prev
);
2313 /* If the frame unwinder hasn't been selected yet, we must do so
2314 before setting prev_p; otherwise the check for misbehaved
2315 sniffers will think that this frame's sniffer tried to unwind
2316 further (see frame_cleanup_after_sniffer). */
2317 if (this_frame
->unwind
== NULL
)
2318 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
2320 this_frame
->prev_p
= true;
2321 this_frame
->stop_reason
= UNWIND_NO_REASON
;
2323 /* If we are unwinding from an inline frame, all of the below tests
2324 were already performed when we unwound from the next non-inline
2325 frame. We must skip them, since we can not get THIS_FRAME's ID
2326 until we have unwound all the way down to the previous non-inline
2328 if (get_frame_type (this_frame
) == INLINE_FRAME
)
2329 return get_prev_frame_maybe_check_cycle (this_frame
);
2331 /* If this_frame is the current frame, then compute and stash its
2332 frame id prior to fetching and computing the frame id of the
2333 previous frame. Otherwise, the cycle detection code in
2334 get_prev_frame_if_no_cycle() will not work correctly. When
2335 get_frame_id() is called later on, an assertion error will be
2336 triggered in the event of a cycle between the current frame and
2339 Note we do this after the INLINE_FRAME check above. That is
2340 because the inline frame's frame id computation needs to fetch
2341 the frame id of its previous real stack frame. I.e., we need to
2342 avoid recursion in that case. This is OK since we're sure the
2343 inline frame won't create a cycle with the real stack frame. See
2344 inline_frame_this_id. */
2345 if (this_frame
->level
== 0)
2346 get_frame_id (this_frame
);
2348 /* Check that this frame is unwindable. If it isn't, don't try to
2349 unwind to the prev frame. */
2350 this_frame
->stop_reason
2351 = this_frame
->unwind
->stop_reason (this_frame
,
2352 &this_frame
->prologue_cache
);
2354 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
2357 (" -> nullptr // %s",
2358 frame_stop_reason_symbol_string (this_frame
->stop_reason
));
2362 /* Check that this frame's ID isn't inner to (younger, below, next)
2363 the next frame. This happens when a frame unwind goes backwards.
2364 This check is valid only if this frame and the next frame are NORMAL.
2365 See the comment at frame_id_inner for details. */
2366 if (get_frame_type (this_frame
) == NORMAL_FRAME
2367 && this_frame
->next
->unwind
->type () == NORMAL_FRAME
2368 && frame_id_inner (get_frame_arch (frame_info_ptr (this_frame
->next
)),
2369 get_frame_id (this_frame
),
2370 get_frame_id (frame_info_ptr (this_frame
->next
))))
2372 CORE_ADDR this_pc_in_block
;
2373 struct minimal_symbol
*morestack_msym
;
2374 const char *morestack_name
= NULL
;
2376 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2377 this_pc_in_block
= get_frame_address_in_block (this_frame
);
2378 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
2380 morestack_name
= morestack_msym
->linkage_name ();
2381 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
2383 frame_debug_printf (" -> nullptr // this frame ID is inner");
2384 this_frame
->stop_reason
= UNWIND_INNER_ID
;
2389 /* Check that this and the next frame do not unwind the PC register
2390 to the same memory location. If they do, then even though they
2391 have different frame IDs, the new frame will be bogus; two
2392 functions can't share a register save slot for the PC. This can
2393 happen when the prologue analyzer finds a stack adjustment, but
2396 This check does assume that the "PC register" is roughly a
2397 traditional PC, even if the gdbarch_unwind_pc method adjusts
2398 it (we do not rely on the value, only on the unwound PC being
2399 dependent on this value). A potential improvement would be
2400 to have the frame prev_pc method and the gdbarch unwind_pc
2401 method set the same lval and location information as
2402 frame_register_unwind. */
2403 if (this_frame
->level
> 0
2404 && gdbarch_pc_regnum (gdbarch
) >= 0
2405 && get_frame_type (this_frame
) == NORMAL_FRAME
2406 && (get_frame_type (frame_info_ptr (this_frame
->next
)) == NORMAL_FRAME
2407 || get_frame_type (frame_info_ptr (this_frame
->next
)) == INLINE_FRAME
))
2409 int optimized
, realnum
, nrealnum
;
2410 enum lval_type lval
, nlval
;
2411 CORE_ADDR addr
, naddr
;
2413 frame_register_unwind_location (this_frame
,
2414 gdbarch_pc_regnum (gdbarch
),
2415 &optimized
, &lval
, &addr
, &realnum
);
2416 frame_register_unwind_location (get_next_frame (this_frame
),
2417 gdbarch_pc_regnum (gdbarch
),
2418 &optimized
, &nlval
, &naddr
, &nrealnum
);
2420 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
2421 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
2423 frame_debug_printf (" -> nullptr // no saved PC");
2424 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2425 this_frame
->prev
= NULL
;
2430 /* Ensure we can unwind the program counter of THIS_FRAME. */
2433 /* Calling frame_unwind_pc for the sentinel frame relies on the
2434 current_frame being set, which at this point it might not be if we
2435 are in the process of setting the current_frame after a stop (see
2438 The point of this check is to ensure that the unwinder for
2439 THIS_FRAME can actually unwind the $pc, which we assume the
2440 sentinel frame unwinder can always do (it's just a read from the
2441 machine state), so we only call frame_unwind_pc for frames other
2442 than the sentinel (level -1) frame.
2444 Additionally, we don't actually care about the value of the
2445 unwound $pc, just that the call completed successfully. */
2446 if (this_frame
->level
>= 0)
2447 frame_unwind_pc (this_frame
);
2449 catch (const gdb_exception_error
&ex
)
2451 if (ex
.error
== NOT_AVAILABLE_ERROR
|| ex
.error
== OPTIMIZED_OUT_ERROR
)
2453 frame_debug_printf (" -> nullptr // no saved PC");
2454 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2455 this_frame
->prev
= nullptr;
2462 return get_prev_frame_maybe_check_cycle (this_frame
);
2465 /* Return a "struct frame_info" corresponding to the frame that called
2466 THIS_FRAME. Returns NULL if there is no such frame.
2468 Unlike get_prev_frame, this function always tries to unwind the
2472 get_prev_frame_always (const frame_info_ptr
&this_frame
)
2474 frame_info_ptr prev_frame
= NULL
;
2478 prev_frame
= get_prev_frame_always_1 (this_frame
);
2480 catch (const gdb_exception_error
&ex
)
2482 if (ex
.error
== MEMORY_ERROR
)
2484 this_frame
->stop_reason
= UNWIND_MEMORY_ERROR
;
2485 if (ex
.message
!= NULL
)
2490 /* The error needs to live as long as the frame does.
2491 Allocate using stack local STOP_STRING then assign the
2492 pointer to the frame, this allows the STOP_STRING on the
2493 frame to be of type 'const char *'. */
2494 size
= ex
.message
->size () + 1;
2495 stop_string
= (char *) frame_obstack_zalloc (size
);
2496 memcpy (stop_string
, ex
.what (), size
);
2497 this_frame
->stop_string
= stop_string
;
2508 /* Construct a new "struct frame_info" and link it previous to
2511 static frame_info_ptr
2512 get_prev_frame_raw (const frame_info_ptr
&this_frame
)
2514 frame_info
*prev_frame
;
2516 /* Allocate the new frame but do not wire it in to the frame chain.
2517 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2518 frame->next to pull some fancy tricks (of course such code is, by
2519 definition, recursive). Try to prevent it.
2521 There is no reason to worry about memory leaks, should the
2522 remainder of the function fail. The allocated memory will be
2523 quickly reclaimed when the frame cache is flushed, and the `we've
2524 been here before' check above will stop repeated memory
2525 allocation calls. */
2526 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2527 prev_frame
->level
= this_frame
->level
+ 1;
2529 /* For now, assume we don't have frame chains crossing address
2531 prev_frame
->pspace
= this_frame
->pspace
;
2532 prev_frame
->aspace
= this_frame
->aspace
;
2534 /* Don't yet compute ->unwind (and hence ->type). It is computed
2535 on-demand in get_frame_type, frame_register_unwind, and
2538 /* Don't yet compute the frame's ID. It is computed on-demand by
2541 /* The unwound frame ID is validate at the start of this function,
2542 as part of the logic to decide if that frame should be further
2543 unwound, and not here while the prev frame is being created.
2544 Doing this makes it possible for the user to examine a frame that
2545 has an invalid frame ID.
2547 Some very old VAX code noted: [...] For the sake of argument,
2548 suppose that the stack is somewhat trashed (which is one reason
2549 that "info frame" exists). So, return 0 (indicating we don't
2550 know the address of the arglist) if we don't know what frame this
2554 this_frame
->prev
= prev_frame
;
2555 prev_frame
->next
= this_frame
.get ();
2557 frame_debug_printf (" -> %s", prev_frame
->to_string ().c_str ());
2559 return frame_info_ptr (prev_frame
);
2562 /* Debug routine to print a NULL frame being returned. */
2565 frame_debug_got_null_frame (const frame_info_ptr
&this_frame
,
2570 if (this_frame
!= NULL
)
2571 frame_debug_printf ("this_frame=%d -> %s", this_frame
->level
, reason
);
2573 frame_debug_printf ("this_frame=nullptr -> %s", reason
);
2577 /* Is this (non-sentinel) frame in the "main"() function? */
2580 inside_main_func (const frame_info_ptr
&this_frame
)
2582 if (current_program_space
->symfile_object_file
== nullptr)
2585 CORE_ADDR sym_addr
= 0;
2586 const char *name
= main_name ();
2587 bound_minimal_symbol msymbol
2588 = lookup_minimal_symbol (current_program_space
, name
,
2589 current_program_space
->symfile_object_file
);
2591 if (msymbol
.minsym
!= nullptr)
2592 sym_addr
= msymbol
.value_address ();
2594 /* Favor a full symbol in Fortran, for the case where the Fortran main
2595 is also called "main". */
2596 if (msymbol
.minsym
== nullptr
2597 || get_frame_language (this_frame
) == language_fortran
)
2599 /* In some language (for example Fortran) there will be no minimal
2600 symbol with the name of the main function. In this case we should
2601 search the full symbols to see if we can find a match. */
2602 struct block_symbol bs
= lookup_symbol (name
, nullptr,
2603 SEARCH_FUNCTION_DOMAIN
, nullptr);
2605 /* This lookup should always yield a block-valued symbol. */
2606 if (bs
.symbol
!= nullptr && bs
.symbol
->aclass () == LOC_BLOCK
)
2608 const struct block
*block
= bs
.symbol
->value_block ();
2609 gdb_assert (block
!= nullptr);
2610 sym_addr
= block
->start ();
2612 else if (msymbol
.minsym
== nullptr)
2616 /* Convert any function descriptor addresses into the actual function
2618 sym_addr
= (gdbarch_convert_from_func_ptr_addr
2619 (get_frame_arch (this_frame
), sym_addr
,
2620 current_inferior ()->top_target ()));
2622 return sym_addr
== get_frame_func (this_frame
);
2625 /* Test whether THIS_FRAME is inside the process entry point function. */
2628 inside_entry_func (const frame_info_ptr
&this_frame
)
2630 CORE_ADDR entry_point
;
2632 if (!entry_point_address_query (current_program_space
, &entry_point
))
2635 return get_frame_func (this_frame
) == entry_point
;
2638 /* Return a structure containing various interesting information about
2639 the frame that called THIS_FRAME. Returns NULL if there is either
2640 no such frame or the frame fails any of a set of target-independent
2641 condition that should terminate the frame chain (e.g., as unwinding
2644 This function should not contain target-dependent tests, such as
2645 checking whether the program-counter is zero. */
2648 get_prev_frame (const frame_info_ptr
&this_frame
)
2650 FRAME_SCOPED_DEBUG_ENTER_EXIT
;
2655 /* There is always a frame. If this assertion fails, suspect that
2656 something should be calling get_selected_frame() or
2657 get_current_frame(). */
2658 gdb_assert (this_frame
!= NULL
);
2660 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2662 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2663 sense to stop unwinding at a dummy frame. One place where a dummy
2664 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2665 pcsqh register (space register for the instruction at the head of the
2666 instruction queue) cannot be written directly; the only way to set it
2667 is to branch to code that is in the target space. In order to implement
2668 frame dummies on HPUX, the called function is made to jump back to where
2669 the inferior was when the user function was called. If gdb was inside
2670 the main function when we created the dummy frame, the dummy frame will
2671 point inside the main function. */
2672 if (this_frame
->level
>= 0
2673 && get_frame_type (this_frame
) == NORMAL_FRAME
2674 && !user_set_backtrace_options
.backtrace_past_main
2676 && inside_main_func (this_frame
))
2677 /* Don't unwind past main(). Note, this is done _before_ the
2678 frame has been marked as previously unwound. That way if the
2679 user later decides to enable unwinds past main(), that will
2680 automatically happen. */
2682 frame_debug_got_null_frame (this_frame
, "inside main func");
2686 /* If the user's backtrace limit has been exceeded, stop. We must
2687 add two to the current level; one of those accounts for backtrace_limit
2688 being 1-based and the level being 0-based, and the other accounts for
2689 the level of the new frame instead of the level of the current
2691 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2693 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2697 /* If we're already inside the entry function for the main objfile,
2698 then it isn't valid. Don't apply this test to a dummy frame -
2699 dummy frame PCs typically land in the entry func. Don't apply
2700 this test to the sentinel frame. Sentinel frames should always
2701 be allowed to unwind. */
2702 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2703 wasn't checking for "main" in the minimal symbols. With that
2704 fixed asm-source tests now stop in "main" instead of halting the
2705 backtrace in weird and wonderful ways somewhere inside the entry
2706 file. Suspect that tests for inside the entry file/func were
2707 added to work around that (now fixed) case. */
2708 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2709 suggested having the inside_entry_func test use the
2710 inside_main_func() msymbol trick (along with entry_point_address()
2711 I guess) to determine the address range of the start function.
2712 That should provide a far better stopper than the current
2714 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2715 applied tail-call optimizations to main so that a function called
2716 from main returns directly to the caller of main. Since we don't
2717 stop at main, we should at least stop at the entry point of the
2719 if (this_frame
->level
>= 0
2720 && get_frame_type (this_frame
) == NORMAL_FRAME
2721 && !user_set_backtrace_options
.backtrace_past_entry
2723 && inside_entry_func (this_frame
))
2725 frame_debug_got_null_frame (this_frame
, "inside entry func");
2729 /* Assume that the only way to get a zero PC is through something
2730 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2731 will never unwind a zero PC. */
2732 if (this_frame
->level
> 0
2733 && (get_frame_type (this_frame
) == NORMAL_FRAME
2734 || get_frame_type (this_frame
) == INLINE_FRAME
)
2735 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2736 && frame_pc_p
&& frame_pc
== 0)
2738 frame_debug_got_null_frame (this_frame
, "zero PC");
2742 return get_prev_frame_always (this_frame
);
2746 get_frame_pc (const frame_info_ptr
&frame
)
2748 gdb_assert (frame
->next
!= NULL
);
2749 return frame_unwind_pc (frame_info_ptr (frame
->next
));
2753 get_frame_pc_if_available (const frame_info_ptr
&frame
, CORE_ADDR
*pc
)
2756 gdb_assert (frame
->next
!= NULL
);
2760 *pc
= frame_unwind_pc (frame_info_ptr (frame
->next
));
2762 catch (const gdb_exception_error
&ex
)
2764 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2773 /* Return an address that falls within THIS_FRAME's code block. */
2776 get_frame_address_in_block (const frame_info_ptr
&this_frame
)
2778 /* A draft address. */
2779 CORE_ADDR pc
= get_frame_pc (this_frame
);
2781 frame_info_ptr
next_frame (this_frame
->next
);
2783 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2784 Normally the resume address is inside the body of the function
2785 associated with THIS_FRAME, but there is a special case: when
2786 calling a function which the compiler knows will never return
2787 (for instance abort), the call may be the very last instruction
2788 in the calling function. The resume address will point after the
2789 call and may be at the beginning of a different function
2792 If THIS_FRAME is a signal frame or dummy frame, then we should
2793 not adjust the unwound PC. For a dummy frame, GDB pushed the
2794 resume address manually onto the stack. For a signal frame, the
2795 OS may have pushed the resume address manually and invoked the
2796 handler (e.g. GNU/Linux), or invoked the trampoline which called
2797 the signal handler - but in either case the signal handler is
2798 expected to return to the trampoline. So in both of these
2799 cases we know that the resume address is executable and
2800 related. So we only need to adjust the PC if THIS_FRAME
2801 is a normal function.
2803 If the program has been interrupted while THIS_FRAME is current,
2804 then clearly the resume address is inside the associated
2805 function. There are three kinds of interruption: debugger stop
2806 (next frame will be SENTINEL_FRAME), operating system
2807 signal or exception (next frame will be SIGTRAMP_FRAME),
2808 or debugger-induced function call (next frame will be
2809 DUMMY_FRAME). So we only need to adjust the PC if
2810 NEXT_FRAME is a normal function.
2812 We check the type of NEXT_FRAME first, since it is already
2813 known; frame type is determined by the unwinder, and since
2814 we have THIS_FRAME we've already selected an unwinder for
2817 If the next frame is inlined, we need to keep going until we find
2818 the real function - for instance, if a signal handler is invoked
2819 while in an inlined function, then the code address of the
2820 "calling" normal function should not be adjusted either. */
2822 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2823 next_frame
= frame_info_ptr (next_frame
->next
);
2825 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2826 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2827 && (get_frame_type (this_frame
) == NORMAL_FRAME
2828 || get_frame_type (this_frame
) == TAILCALL_FRAME
2829 || get_frame_type (this_frame
) == INLINE_FRAME
))
2836 get_frame_address_in_block_if_available (const frame_info_ptr
&this_frame
,
2842 *pc
= get_frame_address_in_block (this_frame
);
2844 catch (const gdb_exception_error
&ex
)
2846 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2855 find_frame_sal (const frame_info_ptr
&frame
)
2857 frame_info_ptr next_frame
;
2861 if (frame_inlined_callees (frame
) > 0)
2865 /* If the current frame has some inlined callees, and we have a next
2866 frame, then that frame must be an inlined frame. In this case
2867 this frame's sal is the "call site" of the next frame's inlined
2868 function, which can not be inferred from get_frame_pc. */
2869 next_frame
= get_next_frame (frame
);
2871 sym
= get_frame_function (next_frame
);
2873 sym
= inline_skipped_symbol (inferior_thread ());
2875 /* If frame is inline, it certainly has symbols. */
2878 symtab_and_line sal
;
2879 if (sym
->line () != 0)
2881 sal
.symtab
= sym
->symtab ();
2882 sal
.line
= sym
->line ();
2885 /* If the symbol does not have a location, we don't know where
2886 the call site is. Do not pretend to. This is jarring, but
2887 we can't do much better. */
2888 sal
.pc
= get_frame_pc (frame
);
2890 sal
.pspace
= get_frame_program_space (frame
);
2894 /* If FRAME is not the innermost frame, that normally means that
2895 FRAME->pc points at the return instruction (which is *after* the
2896 call instruction), and we want to get the line containing the
2897 call (because the call is where the user thinks the program is).
2898 However, if the next frame is either a SIGTRAMP_FRAME or a
2899 DUMMY_FRAME, then the next frame will contain a saved interrupt
2900 PC and such a PC indicates the current (rather than next)
2901 instruction/line, consequently, for such cases, want to get the
2902 line containing fi->pc. */
2903 if (!get_frame_pc_if_available (frame
, &pc
))
2906 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2907 return find_pc_line (pc
, notcurrent
);
2910 /* Per "frame.h", return the ``address'' of the frame. Code should
2911 really be using get_frame_id(). */
2913 get_frame_base (const frame_info_ptr
&fi
)
2915 return get_frame_id (fi
).stack_addr
;
2918 /* High-level offsets into the frame. Used by the debug info. */
2921 get_frame_base_address (const frame_info_ptr
&fi
)
2923 if (get_frame_type (fi
) != NORMAL_FRAME
)
2925 if (fi
->base
== NULL
)
2926 fi
->base
= frame_base_find_by_frame (fi
);
2927 /* Sneaky: If the low-level unwind and high-level base code share a
2928 common unwinder, let them share the prologue cache. */
2929 if (fi
->base
->unwind
== fi
->unwind
)
2930 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2931 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2935 get_frame_locals_address (const frame_info_ptr
&fi
)
2937 if (get_frame_type (fi
) != NORMAL_FRAME
)
2939 /* If there isn't a frame address method, find it. */
2940 if (fi
->base
== NULL
)
2941 fi
->base
= frame_base_find_by_frame (fi
);
2942 /* Sneaky: If the low-level unwind and high-level base code share a
2943 common unwinder, let them share the prologue cache. */
2944 if (fi
->base
->unwind
== fi
->unwind
)
2945 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2946 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2950 get_frame_args_address (const frame_info_ptr
&fi
)
2952 if (get_frame_type (fi
) != NORMAL_FRAME
)
2954 /* If there isn't a frame address method, find it. */
2955 if (fi
->base
== NULL
)
2956 fi
->base
= frame_base_find_by_frame (fi
);
2957 /* Sneaky: If the low-level unwind and high-level base code share a
2958 common unwinder, let them share the prologue cache. */
2959 if (fi
->base
->unwind
== fi
->unwind
)
2960 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2961 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2964 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2968 frame_unwinder_is (const frame_info_ptr
&fi
, const frame_unwind
*unwinder
)
2970 if (fi
->unwind
== nullptr)
2971 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2973 return fi
->unwind
== unwinder
;
2976 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2977 or -1 for a NULL frame. */
2980 frame_relative_level (const frame_info_ptr
&fi
)
2989 get_frame_type (const frame_info_ptr
&frame
)
2991 if (frame
->unwind
== NULL
)
2992 /* Initialize the frame's unwinder because that's what
2993 provides the frame's type. */
2994 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2995 return frame
->unwind
->type ();
2998 struct program_space
*
2999 get_frame_program_space (const frame_info_ptr
&frame
)
3001 return frame
->pspace
;
3004 struct program_space
*
3005 frame_unwind_program_space (const frame_info_ptr
&this_frame
)
3007 gdb_assert (this_frame
);
3009 /* This is really a placeholder to keep the API consistent --- we
3010 assume for now that we don't have frame chains crossing
3012 return this_frame
->pspace
;
3015 const address_space
*
3016 get_frame_address_space (const frame_info_ptr
&frame
)
3018 return frame
->aspace
;
3021 /* Memory access methods. */
3024 get_frame_memory (const frame_info_ptr
&this_frame
, CORE_ADDR addr
,
3025 gdb::array_view
<gdb_byte
> buffer
)
3027 read_memory (addr
, buffer
.data (), buffer
.size ());
3031 get_frame_memory_signed (const frame_info_ptr
&this_frame
, CORE_ADDR addr
,
3034 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
3035 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3037 return read_memory_integer (addr
, len
, byte_order
);
3041 get_frame_memory_unsigned (const frame_info_ptr
&this_frame
, CORE_ADDR addr
,
3044 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
3045 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3047 return read_memory_unsigned_integer (addr
, len
, byte_order
);
3051 safe_frame_unwind_memory (const frame_info_ptr
&this_frame
,
3052 CORE_ADDR addr
, gdb::array_view
<gdb_byte
> buffer
)
3054 /* NOTE: target_read_memory returns zero on success! */
3055 return target_read_memory (addr
, buffer
.data (), buffer
.size ()) == 0;
3058 /* Architecture methods. */
3061 get_frame_arch (const frame_info_ptr
&this_frame
)
3063 return frame_unwind_arch (frame_info_ptr (this_frame
->next
));
3067 frame_unwind_arch (const frame_info_ptr
&next_frame
)
3069 if (!next_frame
->prev_arch
.p
)
3071 struct gdbarch
*arch
;
3073 if (next_frame
->unwind
== NULL
)
3074 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
3076 arch
= next_frame
->unwind
->prev_arch (next_frame
,
3077 &next_frame
->prologue_cache
);
3079 next_frame
->prev_arch
.arch
= arch
;
3080 next_frame
->prev_arch
.p
= true;
3081 frame_debug_printf ("next_frame=%d -> %s",
3083 gdbarch_bfd_arch_info (arch
)->printable_name
);
3086 return next_frame
->prev_arch
.arch
;
3090 frame_unwind_caller_arch (const frame_info_ptr
&initial_next_frame
)
3092 frame_info_ptr next_frame
= skip_artificial_frames (initial_next_frame
);
3094 /* We must have a non-artificial frame. The caller is supposed to check
3095 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
3097 gdb_assert (next_frame
!= nullptr);
3099 return frame_unwind_arch (next_frame
);
3102 /* Gets the language of FRAME. */
3105 get_frame_language (const frame_info_ptr
&frame
)
3110 gdb_assert (frame
!= NULL
);
3112 /* We determine the current frame language by looking up its
3113 associated symtab. To retrieve this symtab, we use the frame
3114 PC. However we cannot use the frame PC as is, because it
3115 usually points to the instruction following the "call", which
3116 is sometimes the first instruction of another function. So
3117 we rely on get_frame_address_in_block(), it provides us with
3118 a PC that is guaranteed to be inside the frame's code
3123 pc
= get_frame_address_in_block (frame
);
3126 catch (const gdb_exception_error
&ex
)
3128 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
3134 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
3137 return cust
->language ();
3140 return language_unknown
;
3143 /* Stack pointer methods. */
3146 get_frame_sp (const frame_info_ptr
&this_frame
)
3148 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
3150 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
3151 operate on THIS_FRAME now. */
3152 return gdbarch_unwind_sp (gdbarch
, frame_info_ptr (this_frame
->next
));
3158 frame_follow_static_link (const frame_info_ptr
&initial_frame
)
3160 const block
*frame_block
= get_frame_block (initial_frame
, nullptr);
3161 if (frame_block
== nullptr)
3164 frame_block
= frame_block
->function_block ();
3166 const struct dynamic_prop
*static_link
= frame_block
->static_link ();
3167 if (static_link
== nullptr)
3170 CORE_ADDR upper_frame_base
;
3172 if (!dwarf2_evaluate_property (static_link
, initial_frame
, NULL
, &upper_frame_base
))
3175 /* Now climb up the stack frame until we reach the frame we are interested
3177 frame_info_ptr frame
= initial_frame
;
3178 for (; frame
!= nullptr; frame
= get_prev_frame (frame
))
3180 struct symbol
*framefunc
= get_frame_function (frame
);
3182 /* Stacks can be quite deep: give the user a chance to stop this. */
3185 /* If we don't know how to compute FRAME's base address, don't give up:
3186 maybe the frame we are looking for is upper in the stack frame. */
3187 if (framefunc
!= nullptr)
3189 if (const symbol_block_ops
*block_ops
= framefunc
->block_ops ();
3190 (block_ops
!= nullptr
3191 && block_ops
->get_frame_base
!= nullptr
3192 && (block_ops
->get_frame_base (framefunc
, frame
)
3193 == upper_frame_base
)))
3201 /* Return the reason why we can't unwind past FRAME. */
3203 enum unwind_stop_reason
3204 get_frame_unwind_stop_reason (const frame_info_ptr
&frame
)
3206 /* Fill-in STOP_REASON. */
3207 get_prev_frame_always (frame
);
3208 gdb_assert (frame
->prev_p
);
3210 return frame
->stop_reason
;
3213 /* Return a string explaining REASON. */
3216 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
3220 #define SET(name, description) \
3221 case name: return _(description);
3222 #include "unwind_stop_reasons.def"
3226 internal_error ("Invalid frame stop reason");
3231 frame_stop_reason_string (const frame_info_ptr
&fi
)
3233 gdb_assert (fi
->prev_p
);
3234 gdb_assert (fi
->prev
== NULL
);
3236 /* Return the specific string if we have one. */
3237 if (fi
->stop_string
!= NULL
)
3238 return fi
->stop_string
;
3240 /* Return the generic string if we have nothing better. */
3241 return unwind_stop_reason_to_string (fi
->stop_reason
);
3244 /* Return the enum symbol name of REASON as a string, to use in debug
3248 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
3252 #define SET(name, description) \
3253 case name: return #name;
3254 #include "unwind_stop_reasons.def"
3258 internal_error ("Invalid frame stop reason");
3262 /* Clean up after a failed (wrong unwinder) attempt to unwind past
3266 frame_cleanup_after_sniffer (const frame_info_ptr
&frame
)
3268 /* The sniffer should not allocate a prologue cache if it did not
3269 match this frame. */
3270 gdb_assert (frame
->prologue_cache
== NULL
);
3272 /* No sniffer should extend the frame chain; sniff based on what is
3274 gdb_assert (!frame
->prev_p
);
3276 /* The sniffer should not check the frame's ID; that's circular. */
3277 gdb_assert (frame
->this_id
.p
!= frame_id_status::COMPUTED
);
3279 /* Clear cached fields dependent on the unwinder.
3281 The previous PC is independent of the unwinder, but the previous
3282 function is not (see get_frame_address_in_block). */
3283 frame
->prev_func
.status
= CC_UNKNOWN
;
3284 frame
->prev_func
.addr
= 0;
3286 /* Discard the unwinder last, so that we can easily find it if an assertion
3287 in this function triggers. */
3288 frame
->unwind
= NULL
;
3291 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
3292 If sniffing fails, the caller should be sure to call
3293 frame_cleanup_after_sniffer. */
3296 frame_prepare_for_sniffer (const frame_info_ptr
&frame
,
3297 const struct frame_unwind
*unwind
)
3299 gdb_assert (frame
->unwind
== NULL
);
3300 frame
->unwind
= unwind
;
3303 static struct cmd_list_element
*set_backtrace_cmdlist
;
3304 static struct cmd_list_element
*show_backtrace_cmdlist
;
3306 /* Definition of the "set backtrace" settings that are exposed as
3307 "backtrace" command options. */
3309 using boolean_option_def
3310 = gdb::option::boolean_option_def
<set_backtrace_options
>;
3312 const gdb::option::option_def set_backtrace_option_defs
[] = {
3314 boolean_option_def
{
3316 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_main
; },
3317 show_backtrace_past_main
, /* show_cmd_cb */
3318 N_("Set whether backtraces should continue past \"main\"."),
3319 N_("Show whether backtraces should continue past \"main\"."),
3320 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
3321 the backtrace at \"main\". Set this if you need to see the rest\n\
3322 of the stack trace."),
3325 boolean_option_def
{
3327 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_entry
; },
3328 show_backtrace_past_entry
, /* show_cmd_cb */
3329 N_("Set whether backtraces should continue past the entry point of a program."),
3330 N_("Show whether backtraces should continue past the entry point of a program."),
3331 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
3332 will terminate the backtrace there. Set this if you need to see\n\
3333 the rest of the stack trace."),
3337 /* Implement the 'maintenance print frame-id' command. */
3340 maintenance_print_frame_id (const char *args
, int from_tty
)
3342 frame_info_ptr frame
;
3344 /* Use the currently selected frame, or select a frame based on the level
3345 number passed by the user. */
3346 if (args
== nullptr)
3347 frame
= get_selected_frame ("No frame selected");
3350 int level
= value_as_long (parse_and_eval (args
));
3351 frame
= find_relative_frame (get_current_frame (), &level
);
3354 /* Print the frame-id. */
3355 gdb_assert (frame
!= nullptr);
3356 gdb_printf ("frame-id for frame #%d: %s\n",
3357 frame_relative_level (frame
),
3358 get_frame_id (frame
).to_string ().c_str ());
3361 /* See frame-info-ptr.h. */
3363 frame_info_ptr::frame_info_ptr (struct frame_info
*ptr
)
3366 frame_list
.push_back (*this);
3368 if (m_ptr
== nullptr)
3371 m_cached_level
= ptr
->level
;
3373 if (m_cached_level
!= 0 || m_ptr
->this_id
.value
.user_created_p
)
3374 m_cached_id
= m_ptr
->this_id
.value
;
3377 /* See frame-info-ptr.h. */
3380 frame_info_ptr::reinflate () const
3382 /* Ensure we have a valid frame level (sentinel frame or above). */
3383 gdb_assert (m_cached_level
>= -1);
3385 if (m_ptr
!= nullptr)
3387 /* The frame_info wasn't invalidated, no need to reinflate. */
3391 if (m_cached_id
.user_created_p
)
3392 m_ptr
= create_new_frame (m_cached_id
).get ();
3395 /* Frame #0 needs special handling, see comment in select_frame. */
3396 if (m_cached_level
== 0)
3397 m_ptr
= get_current_frame ().get ();
3400 /* If we reach here without a valid frame id, it means we are trying
3401 to reinflate a frame whose id was not know at construction time.
3402 We're probably trying to reinflate a frame while computing its id
3403 which is not possible, and would indicate a problem with GDB. */
3404 gdb_assert (frame_id_p (m_cached_id
));
3405 m_ptr
= frame_find_by_id (m_cached_id
).get ();
3409 gdb_assert (m_ptr
!= nullptr);
3413 void _initialize_frame ();
3415 _initialize_frame ()
3417 obstack_init (&frame_cache_obstack
);
3419 frame_stash_create ();
3421 gdb::observers::target_changed
.attach (frame_observer_target_changed
,
3424 add_setshow_prefix_cmd ("backtrace", class_maintenance
,
3426 Set backtrace specific variables.\n\
3427 Configure backtrace variables such as the backtrace limit"),
3429 Show backtrace specific variables.\n\
3430 Show backtrace variables such as the backtrace limit."),
3431 &set_backtrace_cmdlist
, &show_backtrace_cmdlist
,
3432 &setlist
, &showlist
);
3434 add_setshow_uinteger_cmd ("limit", class_obscure
,
3435 &user_set_backtrace_options
.backtrace_limit
, _("\
3436 Set an upper bound on the number of backtrace levels."), _("\
3437 Show the upper bound on the number of backtrace levels."), _("\
3438 No more than the specified number of frames can be displayed or examined.\n\
3439 Literal \"unlimited\" or zero means no limit."),
3441 show_backtrace_limit
,
3442 &set_backtrace_cmdlist
,
3443 &show_backtrace_cmdlist
);
3445 gdb::option::add_setshow_cmds_for_options
3446 (class_stack
, &user_set_backtrace_options
,
3447 set_backtrace_option_defs
, &set_backtrace_cmdlist
, &show_backtrace_cmdlist
);
3449 /* Debug this files internals. */
3450 add_setshow_boolean_cmd ("frame", class_maintenance
, &frame_debug
, _("\
3451 Set frame debugging."), _("\
3452 Show frame debugging."), _("\
3453 When non-zero, frame specific internal debugging is enabled."),
3456 &setdebuglist
, &showdebuglist
);
3458 add_cmd ("frame-id", class_maintenance
, maintenance_print_frame_id
,
3459 _("Print the current frame-id."),
3460 &maintenanceprintlist
);