1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 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/>. */
24 #include "inferior.h" /* for inferior_ptid */
26 #include "user-regs.h"
27 #include "gdb_obstack.h"
28 #include "dummy-frame.h"
29 #include "sentinel-frame.h"
33 #include "frame-unwind.h"
34 #include "frame-base.h"
37 #include "observable.h"
39 #include "gdbthread.h"
41 #include "inline-frame.h"
42 #include "tracepoint.h"
45 #include "cli/cli-option.h"
47 /* The sentinel frame terminates the innermost end of the frame chain.
48 If unwound, it returns the information needed to construct an
51 The current frame, which is the innermost frame, can be found at
52 sentinel_frame->prev. */
54 static struct frame_info
*sentinel_frame
;
56 /* Number of calls to reinit_frame_cache. */
57 static unsigned int frame_cache_generation
= 0;
62 get_frame_cache_generation ()
64 return frame_cache_generation
;
67 /* The values behind the global "set backtrace ..." settings. */
68 set_backtrace_options user_set_backtrace_options
;
70 static struct frame_info
*get_prev_frame_raw (struct frame_info
*this_frame
);
71 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason
);
73 /* Status of some values cached in the frame_info object. */
75 enum cached_copy_status
77 /* Value is unknown. */
80 /* We have a value. */
83 /* Value was not saved. */
86 /* Value is unavailable. */
90 enum class frame_id_status
92 /* Frame id is not computed. */
95 /* Frame id is being computed (compute_frame_id is active). */
98 /* Frame id has been computed. */
102 /* We keep a cache of stack frames, each of which is a "struct
103 frame_info". The innermost one gets allocated (in
104 wait_for_inferior) each time the inferior stops; sentinel_frame
105 points to it. Additional frames get allocated (in get_prev_frame)
106 as needed, and are chained through the next and prev fields. Any
107 time that the frame cache becomes invalid (most notably when we
108 execute something, but also if we change how we interpret the
109 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
110 which reads new symbols)), we should call reinit_frame_cache. */
114 /* Level of this frame. The inner-most (youngest) frame is at level
115 0. As you move towards the outer-most (oldest) frame, the level
116 increases. This is a cached value. It could just as easily be
117 computed by counting back from the selected frame to the inner
119 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
120 reserved to indicate a bogus frame - one that has been created
121 just to keep GDB happy (GDB always needs a frame). For the
122 moment leave this as speculation. */
125 /* The frame's program space. */
126 struct program_space
*pspace
;
128 /* The frame's address space. */
129 const address_space
*aspace
;
131 /* The frame's low-level unwinder and corresponding cache. The
132 low-level unwinder is responsible for unwinding register values
133 for the previous frame. The low-level unwind methods are
134 selected based on the presence, or otherwise, of register unwind
135 information such as CFI. */
136 void *prologue_cache
;
137 const struct frame_unwind
*unwind
;
139 /* Cached copy of the previous frame's architecture. */
143 struct gdbarch
*arch
;
146 /* Cached copy of the previous frame's resume address. */
148 cached_copy_status status
;
149 /* Did VALUE require unmasking when being read. */
154 /* Cached copy of the previous frame's function address. */
158 cached_copy_status status
;
161 /* This frame's ID. */
165 struct frame_id value
;
168 /* The frame's high-level base methods, and corresponding cache.
169 The high level base methods are selected based on the frame's
171 const struct frame_base
*base
;
174 /* Pointers to the next (down, inner, younger) and previous (up,
175 outer, older) frame_info's in the frame cache. */
176 struct frame_info
*next
; /* down, inner, younger */
178 struct frame_info
*prev
; /* up, outer, older */
180 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
181 could. Only valid when PREV_P is set. */
182 enum unwind_stop_reason stop_reason
;
184 /* A frame specific string describing the STOP_REASON in more detail.
185 Only valid when PREV_P is set, but even then may still be NULL. */
186 const char *stop_string
;
192 set_frame_previous_pc_masked (struct frame_info
*frame
)
194 frame
->prev_pc
.masked
= true;
200 get_frame_pc_masked (const struct frame_info
*frame
)
202 gdb_assert (frame
->next
!= nullptr);
203 gdb_assert (frame
->next
->prev_pc
.status
== CC_VALUE
);
205 return frame
->next
->prev_pc
.masked
;
208 /* A frame stash used to speed up frame lookups. Create a hash table
209 to stash frames previously accessed from the frame cache for
210 quicker subsequent retrieval. The hash table is emptied whenever
211 the frame cache is invalidated. */
213 static htab_t frame_stash
;
215 /* Internal function to calculate a hash from the frame_id addresses,
216 using as many valid addresses as possible. Frames below level 0
217 are not stored in the hash table. */
220 frame_addr_hash (const void *ap
)
222 const struct frame_info
*frame
= (const struct frame_info
*) ap
;
223 const struct frame_id f_id
= frame
->this_id
.value
;
226 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
228 || f_id
.special_addr_p
);
230 if (f_id
.stack_status
== FID_STACK_VALID
)
231 hash
= iterative_hash (&f_id
.stack_addr
,
232 sizeof (f_id
.stack_addr
), hash
);
233 if (f_id
.code_addr_p
)
234 hash
= iterative_hash (&f_id
.code_addr
,
235 sizeof (f_id
.code_addr
), hash
);
236 if (f_id
.special_addr_p
)
237 hash
= iterative_hash (&f_id
.special_addr
,
238 sizeof (f_id
.special_addr
), hash
);
243 /* Internal equality function for the hash table. This function
244 defers equality operations to frame_id_eq. */
247 frame_addr_hash_eq (const void *a
, const void *b
)
249 const struct frame_info
*f_entry
= (const struct frame_info
*) a
;
250 const struct frame_info
*f_element
= (const struct frame_info
*) b
;
252 return frame_id_eq (f_entry
->this_id
.value
,
253 f_element
->this_id
.value
);
256 /* Internal function to create the frame_stash hash table. 100 seems
257 to be a good compromise to start the hash table at. */
260 frame_stash_create (void)
262 frame_stash
= htab_create (100,
268 /* Internal function to add a frame to the frame_stash hash table.
269 Returns false if a frame with the same ID was already stashed, true
273 frame_stash_add (frame_info
*frame
)
275 /* Do not try to stash the sentinel frame. */
276 gdb_assert (frame
->level
>= 0);
278 frame_info
**slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
281 /* If we already have a frame in the stack with the same id, we
282 either have a stack cycle (corrupted stack?), or some bug
283 elsewhere in GDB. In any case, ignore the duplicate and return
284 an indication to the caller. */
285 if (*slot
!= nullptr)
292 /* Internal function to search the frame stash for an entry with the
293 given frame ID. If found, return that frame. Otherwise return
296 static struct frame_info
*
297 frame_stash_find (struct frame_id id
)
299 struct frame_info dummy
;
300 struct frame_info
*frame
;
302 dummy
.this_id
.value
= id
;
303 frame
= (struct frame_info
*) htab_find (frame_stash
, &dummy
);
307 /* Internal function to invalidate the frame stash by removing all
308 entries in it. This only occurs when the frame cache is
312 frame_stash_invalidate (void)
314 htab_empty (frame_stash
);
318 scoped_restore_selected_frame::scoped_restore_selected_frame ()
320 m_fid
= get_frame_id (get_selected_frame (NULL
));
324 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
326 frame_info
*frame
= frame_find_by_id (m_fid
);
328 warning (_("Unable to restore previously selected frame."));
330 select_frame (frame
);
333 /* Flag to control debugging. */
335 unsigned int frame_debug
;
337 show_frame_debug (struct ui_file
*file
, int from_tty
,
338 struct cmd_list_element
*c
, const char *value
)
340 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
343 /* Implementation of "show backtrace past-main". */
346 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
347 struct cmd_list_element
*c
, const char *value
)
349 fprintf_filtered (file
,
350 _("Whether backtraces should "
351 "continue past \"main\" is %s.\n"),
355 /* Implementation of "show backtrace past-entry". */
358 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
359 struct cmd_list_element
*c
, const char *value
)
361 fprintf_filtered (file
, _("Whether backtraces should continue past the "
362 "entry point of a program is %s.\n"),
366 /* Implementation of "show backtrace limit". */
369 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
370 struct cmd_list_element
*c
, const char *value
)
372 fprintf_filtered (file
,
373 _("An upper bound on the number "
374 "of backtrace levels is %s.\n"),
380 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
383 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
385 fprintf_unfiltered (file
, "!%s", name
);
389 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
391 fprintf_unfiltered (file
, "{");
393 if (id
.stack_status
== FID_STACK_INVALID
)
394 fprintf_unfiltered (file
, "!stack");
395 else if (id
.stack_status
== FID_STACK_UNAVAILABLE
)
396 fprintf_unfiltered (file
, "stack=<unavailable>");
397 else if (id
.stack_status
== FID_STACK_SENTINEL
)
398 fprintf_unfiltered (file
, "stack=<sentinel>");
399 else if (id
.stack_status
== FID_STACK_OUTER
)
400 fprintf_unfiltered (file
, "stack=<outer>");
402 fprintf_unfiltered (file
, "stack=%s", hex_string (id
.stack_addr
));
404 fprintf_unfiltered (file
, ",");
406 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
407 fprintf_unfiltered (file
, ",");
409 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
411 if (id
.artificial_depth
)
412 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
414 fprintf_unfiltered (file
, "}");
418 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
423 fprintf_unfiltered (file
, "NORMAL_FRAME");
426 fprintf_unfiltered (file
, "DUMMY_FRAME");
429 fprintf_unfiltered (file
, "INLINE_FRAME");
432 fprintf_unfiltered (file
, "TAILCALL_FRAME");
435 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
438 fprintf_unfiltered (file
, "ARCH_FRAME");
441 fprintf_unfiltered (file
, "SENTINEL_FRAME");
444 fprintf_unfiltered (file
, "<unknown type>");
450 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
454 fprintf_unfiltered (file
, "<NULL frame>");
458 fprintf_unfiltered (file
, "{");
459 fprintf_unfiltered (file
, "level=%d", fi
->level
);
460 fprintf_unfiltered (file
, ",");
462 fprintf_unfiltered (file
, "type=");
463 if (fi
->unwind
!= NULL
)
464 fprint_frame_type (file
, fi
->unwind
->type
);
466 fprintf_unfiltered (file
, "<unknown>");
467 fprintf_unfiltered (file
, ",");
469 fprintf_unfiltered (file
, "unwind=");
470 if (fi
->unwind
!= NULL
)
471 gdb_print_host_address (fi
->unwind
, file
);
473 fprintf_unfiltered (file
, "<unknown>");
474 fprintf_unfiltered (file
, ",");
476 fprintf_unfiltered (file
, "pc=");
477 if (fi
->next
== NULL
|| fi
->next
->prev_pc
.status
== CC_UNKNOWN
)
478 fprintf_unfiltered (file
, "<unknown>");
479 else if (fi
->next
->prev_pc
.status
== CC_VALUE
)
481 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_pc
.value
));
482 if (fi
->next
->prev_pc
.masked
)
483 fprintf_unfiltered (file
, "[PAC]");
485 else if (fi
->next
->prev_pc
.status
== CC_NOT_SAVED
)
486 val_print_not_saved (file
);
487 else if (fi
->next
->prev_pc
.status
== CC_UNAVAILABLE
)
488 val_print_unavailable (file
);
489 fprintf_unfiltered (file
, ",");
491 fprintf_unfiltered (file
, "id=");
492 if (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
)
493 fprintf_unfiltered (file
, "<not computed>");
494 else if (fi
->this_id
.p
== frame_id_status::COMPUTING
)
495 fprintf_unfiltered (file
, "<computing>");
497 fprint_frame_id (file
, fi
->this_id
.value
);
498 fprintf_unfiltered (file
, ",");
500 fprintf_unfiltered (file
, "func=");
501 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.status
== CC_VALUE
)
502 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
504 fprintf_unfiltered (file
, "<unknown>");
505 fprintf_unfiltered (file
, "}");
508 /* Given FRAME, return the enclosing frame as found in real frames read-in from
509 inferior memory. Skip any previous frames which were made up by GDB.
510 Return FRAME if FRAME is a non-artificial frame.
511 Return NULL if FRAME is the start of an artificial-only chain. */
513 static struct frame_info
*
514 skip_artificial_frames (struct frame_info
*frame
)
516 /* Note we use get_prev_frame_always, and not get_prev_frame. The
517 latter will truncate the frame chain, leading to this function
518 unintentionally returning a null_frame_id (e.g., when the user
519 sets a backtrace limit).
521 Note that for record targets we may get a frame chain that consists
522 of artificial frames only. */
523 while (get_frame_type (frame
) == INLINE_FRAME
524 || get_frame_type (frame
) == TAILCALL_FRAME
)
526 frame
= get_prev_frame_always (frame
);
535 skip_unwritable_frames (struct frame_info
*frame
)
537 while (gdbarch_code_of_frame_writable (get_frame_arch (frame
), frame
) == 0)
539 frame
= get_prev_frame (frame
);
550 skip_tailcall_frames (struct frame_info
*frame
)
552 while (get_frame_type (frame
) == TAILCALL_FRAME
)
554 /* Note that for record targets we may get a frame chain that consists of
555 tailcall frames only. */
556 frame
= get_prev_frame (frame
);
564 /* Compute the frame's uniq ID that can be used to, later, re-find the
568 compute_frame_id (struct frame_info
*fi
)
570 gdb_assert (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
);
572 unsigned int entry_generation
= get_frame_cache_generation ();
576 /* Mark this frame's id as "being computed. */
577 fi
->this_id
.p
= frame_id_status::COMPUTING
;
580 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
583 /* Find the unwinder. */
584 if (fi
->unwind
== NULL
)
585 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
587 /* Find THIS frame's ID. */
588 /* Default to outermost if no ID is found. */
589 fi
->this_id
.value
= outer_frame_id
;
590 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
591 gdb_assert (frame_id_p (fi
->this_id
.value
));
593 /* Mark this frame's id as "computed". */
594 fi
->this_id
.p
= frame_id_status::COMPUTED
;
598 fprintf_unfiltered (gdb_stdlog
, "-> ");
599 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
600 fprintf_unfiltered (gdb_stdlog
, " }\n");
603 catch (const gdb_exception
&ex
)
605 /* On error, revert the frame id status to not computed. If the frame
606 cache generation changed, the frame object doesn't exist anymore, so
608 if (get_frame_cache_generation () == entry_generation
)
609 fi
->this_id
.p
= frame_id_status::NOT_COMPUTED
;
615 /* Return a frame uniq ID that can be used to, later, re-find the
619 get_frame_id (struct frame_info
*fi
)
622 return null_frame_id
;
624 /* It's always invalid to try to get a frame's id while it is being
626 gdb_assert (fi
->this_id
.p
!= frame_id_status::COMPUTING
);
628 if (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
)
630 /* If we haven't computed the frame id yet, then it must be that
631 this is the current frame. Compute it now, and stash the
632 result. The IDs of other frames are computed as soon as
633 they're created, in order to detect cycles. See
634 get_prev_frame_if_no_cycle. */
635 gdb_assert (fi
->level
== 0);
638 compute_frame_id (fi
);
640 /* Since this is the first frame in the chain, this should
642 bool stashed
= frame_stash_add (fi
);
643 gdb_assert (stashed
);
646 return fi
->this_id
.value
;
650 get_stack_frame_id (struct frame_info
*next_frame
)
652 return get_frame_id (skip_artificial_frames (next_frame
));
656 frame_unwind_caller_id (struct frame_info
*next_frame
)
658 struct frame_info
*this_frame
;
660 /* Use get_prev_frame_always, and not get_prev_frame. The latter
661 will truncate the frame chain, leading to this function
662 unintentionally returning a null_frame_id (e.g., when a caller
663 requests the frame ID of "main()"s caller. */
665 next_frame
= skip_artificial_frames (next_frame
);
666 if (next_frame
== NULL
)
667 return null_frame_id
;
669 this_frame
= get_prev_frame_always (next_frame
);
671 return get_frame_id (skip_artificial_frames (this_frame
));
673 return null_frame_id
;
676 const struct frame_id null_frame_id
= { 0 }; /* All zeros. */
677 const struct frame_id sentinel_frame_id
= { 0, 0, 0, FID_STACK_SENTINEL
, 0, 1, 0 };
678 const struct frame_id outer_frame_id
= { 0, 0, 0, FID_STACK_OUTER
, 0, 1, 0 };
681 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
682 CORE_ADDR special_addr
)
684 struct frame_id id
= null_frame_id
;
686 id
.stack_addr
= stack_addr
;
687 id
.stack_status
= FID_STACK_VALID
;
688 id
.code_addr
= code_addr
;
689 id
.code_addr_p
= true;
690 id
.special_addr
= special_addr
;
691 id
.special_addr_p
= true;
698 frame_id_build_unavailable_stack (CORE_ADDR code_addr
)
700 struct frame_id id
= null_frame_id
;
702 id
.stack_status
= FID_STACK_UNAVAILABLE
;
703 id
.code_addr
= code_addr
;
704 id
.code_addr_p
= true;
711 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr
,
712 CORE_ADDR special_addr
)
714 struct frame_id id
= null_frame_id
;
716 id
.stack_status
= FID_STACK_UNAVAILABLE
;
717 id
.code_addr
= code_addr
;
718 id
.code_addr_p
= true;
719 id
.special_addr
= special_addr
;
720 id
.special_addr_p
= true;
725 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
727 struct frame_id id
= null_frame_id
;
729 id
.stack_addr
= stack_addr
;
730 id
.stack_status
= FID_STACK_VALID
;
731 id
.code_addr
= code_addr
;
732 id
.code_addr_p
= true;
737 frame_id_build_wild (CORE_ADDR stack_addr
)
739 struct frame_id id
= null_frame_id
;
741 id
.stack_addr
= stack_addr
;
742 id
.stack_status
= FID_STACK_VALID
;
747 frame_id_p (frame_id l
)
749 /* The frame is valid iff it has a valid stack address. */
750 bool p
= l
.stack_status
!= FID_STACK_INVALID
;
754 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
755 fprint_frame_id (gdb_stdlog
, l
);
756 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
763 frame_id_artificial_p (frame_id l
)
768 return l
.artificial_depth
!= 0;
772 frame_id_eq (frame_id l
, frame_id r
)
776 if (l
.stack_status
== FID_STACK_INVALID
777 || r
.stack_status
== FID_STACK_INVALID
)
778 /* Like a NaN, if either ID is invalid, the result is false.
779 Note that a frame ID is invalid iff it is the null frame ID. */
781 else if (l
.stack_status
!= r
.stack_status
|| l
.stack_addr
!= r
.stack_addr
)
782 /* If .stack addresses are different, the frames are different. */
784 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
785 /* An invalid code addr is a wild card. If .code addresses are
786 different, the frames are different. */
788 else if (l
.special_addr_p
&& r
.special_addr_p
789 && l
.special_addr
!= r
.special_addr
)
790 /* An invalid special addr is a wild card (or unused). Otherwise
791 if special addresses are different, the frames are different. */
793 else if (l
.artificial_depth
!= r
.artificial_depth
)
794 /* If artificial depths are different, the frames must be different. */
797 /* Frames are equal. */
802 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
803 fprint_frame_id (gdb_stdlog
, l
);
804 fprintf_unfiltered (gdb_stdlog
, ",r=");
805 fprint_frame_id (gdb_stdlog
, r
);
806 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
812 /* Safety net to check whether frame ID L should be inner to
813 frame ID R, according to their stack addresses.
815 This method cannot be used to compare arbitrary frames, as the
816 ranges of valid stack addresses may be discontiguous (e.g. due
819 However, it can be used as safety net to discover invalid frame
820 IDs in certain circumstances. Assuming that NEXT is the immediate
821 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
823 * The stack address of NEXT must be inner-than-or-equal to the stack
826 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
829 * If NEXT and THIS have different stack addresses, no other frame
830 in the frame chain may have a stack address in between.
832 Therefore, if frame_id_inner (TEST, THIS) holds, but
833 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
834 to a valid frame in the frame chain.
836 The sanity checks above cannot be performed when a SIGTRAMP frame
837 is involved, because signal handlers might be executed on a different
838 stack than the stack used by the routine that caused the signal
839 to be raised. This can happen for instance when a thread exceeds
840 its maximum stack size. In this case, certain compilers implement
841 a stack overflow strategy that cause the handler to be run on a
845 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
849 if (l
.stack_status
!= FID_STACK_VALID
|| r
.stack_status
!= FID_STACK_VALID
)
850 /* Like NaN, any operation involving an invalid ID always fails.
851 Likewise if either ID has an unavailable stack address. */
853 else if (l
.artificial_depth
> r
.artificial_depth
854 && l
.stack_addr
== r
.stack_addr
855 && l
.code_addr_p
== r
.code_addr_p
856 && l
.special_addr_p
== r
.special_addr_p
857 && l
.special_addr
== r
.special_addr
)
859 /* Same function, different inlined functions. */
860 const struct block
*lb
, *rb
;
862 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
864 lb
= block_for_pc (l
.code_addr
);
865 rb
= block_for_pc (r
.code_addr
);
867 if (lb
== NULL
|| rb
== NULL
)
868 /* Something's gone wrong. */
871 /* This will return true if LB and RB are the same block, or
872 if the block with the smaller depth lexically encloses the
873 block with the greater depth. */
874 inner
= contained_in (lb
, rb
);
877 /* Only return non-zero when strictly inner than. Note that, per
878 comment in "frame.h", there is some fuzz here. Frameless
879 functions are not strictly inner than (same .stack but
880 different .code and/or .special address). */
881 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
885 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
886 fprint_frame_id (gdb_stdlog
, l
);
887 fprintf_unfiltered (gdb_stdlog
, ",r=");
888 fprint_frame_id (gdb_stdlog
, r
);
889 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
896 frame_find_by_id (struct frame_id id
)
898 struct frame_info
*frame
, *prev_frame
;
900 /* ZERO denotes the null frame, let the caller decide what to do
901 about it. Should it instead return get_current_frame()? */
902 if (!frame_id_p (id
))
905 /* Check for the sentinel frame. */
906 if (frame_id_eq (id
, sentinel_frame_id
))
907 return sentinel_frame
;
909 /* Try using the frame stash first. Finding it there removes the need
910 to perform the search by looping over all frames, which can be very
911 CPU-intensive if the number of frames is very high (the loop is O(n)
912 and get_prev_frame performs a series of checks that are relatively
913 expensive). This optimization is particularly useful when this function
914 is called from another function (such as value_fetch_lazy, case
915 VALUE_LVAL (val) == lval_register) which already loops over all frames,
916 making the overall behavior O(n^2). */
917 frame
= frame_stash_find (id
);
921 for (frame
= get_current_frame (); ; frame
= prev_frame
)
923 struct frame_id self
= get_frame_id (frame
);
925 if (frame_id_eq (id
, self
))
926 /* An exact match. */
929 prev_frame
= get_prev_frame (frame
);
933 /* As a safety net to avoid unnecessary backtracing while trying
934 to find an invalid ID, we check for a common situation where
935 we can detect from comparing stack addresses that no other
936 frame in the current frame chain can have this ID. See the
937 comment at frame_id_inner for details. */
938 if (get_frame_type (frame
) == NORMAL_FRAME
939 && !frame_id_inner (get_frame_arch (frame
), id
, self
)
940 && frame_id_inner (get_frame_arch (prev_frame
), id
,
941 get_frame_id (prev_frame
)))
948 frame_unwind_pc (struct frame_info
*this_frame
)
950 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
952 struct gdbarch
*prev_gdbarch
;
956 /* The right way. The `pure' way. The one true way. This
957 method depends solely on the register-unwind code to
958 determine the value of registers in THIS frame, and hence
959 the value of this frame's PC (resume address). A typical
960 implementation is no more than:
962 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
963 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
965 Note: this method is very heavily dependent on a correct
966 register-unwind implementation, it pays to fix that
967 method first; this method is frame type agnostic, since
968 it only deals with register values, it works with any
969 frame. This is all in stark contrast to the old
970 FRAME_SAVED_PC which would try to directly handle all the
971 different ways that a PC could be unwound. */
972 prev_gdbarch
= frame_unwind_arch (this_frame
);
976 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
979 catch (const gdb_exception_error
&ex
)
981 if (ex
.error
== NOT_AVAILABLE_ERROR
)
983 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
986 fprintf_unfiltered (gdb_stdlog
,
987 "{ frame_unwind_pc (this_frame=%d)"
988 " -> <unavailable> }\n",
991 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
993 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
996 fprintf_unfiltered (gdb_stdlog
,
997 "{ frame_unwind_pc (this_frame=%d)"
998 " -> <not saved> }\n",
1007 this_frame
->prev_pc
.value
= pc
;
1008 this_frame
->prev_pc
.status
= CC_VALUE
;
1010 fprintf_unfiltered (gdb_stdlog
,
1011 "{ frame_unwind_pc (this_frame=%d) "
1014 hex_string (this_frame
->prev_pc
.value
));
1018 if (this_frame
->prev_pc
.status
== CC_VALUE
)
1019 return this_frame
->prev_pc
.value
;
1020 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
1021 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1022 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
1023 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
1025 internal_error (__FILE__
, __LINE__
,
1026 "unexpected prev_pc status: %d",
1027 (int) this_frame
->prev_pc
.status
);
1031 frame_unwind_caller_pc (struct frame_info
*this_frame
)
1033 this_frame
= skip_artificial_frames (this_frame
);
1035 /* We must have a non-artificial frame. The caller is supposed to check
1036 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1038 gdb_assert (this_frame
!= NULL
);
1040 return frame_unwind_pc (this_frame
);
1044 get_frame_func_if_available (frame_info
*this_frame
, CORE_ADDR
*pc
)
1046 struct frame_info
*next_frame
= this_frame
->next
;
1048 if (next_frame
->prev_func
.status
== CC_UNKNOWN
)
1050 CORE_ADDR addr_in_block
;
1052 /* Make certain that this, and not the adjacent, function is
1054 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
1056 next_frame
->prev_func
.status
= CC_UNAVAILABLE
;
1058 fprintf_unfiltered (gdb_stdlog
,
1059 "{ get_frame_func (this_frame=%d)"
1060 " -> unavailable }\n",
1065 next_frame
->prev_func
.status
= CC_VALUE
;
1066 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
1068 fprintf_unfiltered (gdb_stdlog
,
1069 "{ get_frame_func (this_frame=%d) -> %s }\n",
1071 hex_string (next_frame
->prev_func
.addr
));
1075 if (next_frame
->prev_func
.status
== CC_UNAVAILABLE
)
1082 gdb_assert (next_frame
->prev_func
.status
== CC_VALUE
);
1084 *pc
= next_frame
->prev_func
.addr
;
1090 get_frame_func (struct frame_info
*this_frame
)
1094 if (!get_frame_func_if_available (this_frame
, &pc
))
1095 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1100 std::unique_ptr
<readonly_detached_regcache
>
1101 frame_save_as_regcache (struct frame_info
*this_frame
)
1103 auto cooked_read
= [this_frame
] (int regnum
, gdb_byte
*buf
)
1105 if (!deprecated_frame_register_read (this_frame
, regnum
, buf
))
1106 return REG_UNAVAILABLE
;
1111 std::unique_ptr
<readonly_detached_regcache
> regcache
1112 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
1118 frame_pop (struct frame_info
*this_frame
)
1120 struct frame_info
*prev_frame
;
1122 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
1124 /* Popping a dummy frame involves restoring more than just registers.
1125 dummy_frame_pop does all the work. */
1126 dummy_frame_pop (get_frame_id (this_frame
), inferior_thread ());
1130 /* Ensure that we have a frame to pop to. */
1131 prev_frame
= get_prev_frame_always (this_frame
);
1134 error (_("Cannot pop the initial frame."));
1136 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1137 entering THISFRAME. */
1138 prev_frame
= skip_tailcall_frames (prev_frame
);
1140 if (prev_frame
== NULL
)
1141 error (_("Cannot find the caller frame."));
1143 /* Make a copy of all the register values unwound from this frame.
1144 Save them in a scratch buffer so that there isn't a race between
1145 trying to extract the old values from the current regcache while
1146 at the same time writing new values into that same cache. */
1147 std::unique_ptr
<readonly_detached_regcache
> scratch
1148 = frame_save_as_regcache (prev_frame
);
1150 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1151 target's register cache that it is about to be hit with a burst
1152 register transfer and that the sequence of register writes should
1153 be batched. The pair target_prepare_to_store() and
1154 target_store_registers() kind of suggest this functionality.
1155 Unfortunately, they don't implement it. Their lack of a formal
1156 definition can lead to targets writing back bogus values
1157 (arguably a bug in the target code mind). */
1158 /* Now copy those saved registers into the current regcache. */
1159 get_current_regcache ()->restore (scratch
.get ());
1161 /* We've made right mess of GDB's local state, just discard
1163 reinit_frame_cache ();
1167 frame_register_unwind (frame_info
*next_frame
, int regnum
,
1168 int *optimizedp
, int *unavailablep
,
1169 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
1170 int *realnump
, gdb_byte
*bufferp
)
1172 struct value
*value
;
1174 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1175 that the value proper does not need to be fetched. */
1176 gdb_assert (optimizedp
!= NULL
);
1177 gdb_assert (lvalp
!= NULL
);
1178 gdb_assert (addrp
!= NULL
);
1179 gdb_assert (realnump
!= NULL
);
1180 /* gdb_assert (bufferp != NULL); */
1182 value
= frame_unwind_register_value (next_frame
, regnum
);
1184 gdb_assert (value
!= NULL
);
1186 *optimizedp
= value_optimized_out (value
);
1187 *unavailablep
= !value_entirely_available (value
);
1188 *lvalp
= VALUE_LVAL (value
);
1189 *addrp
= value_address (value
);
1190 if (*lvalp
== lval_register
)
1191 *realnump
= VALUE_REGNUM (value
);
1197 if (!*optimizedp
&& !*unavailablep
)
1198 memcpy (bufferp
, value_contents_all (value
),
1199 TYPE_LENGTH (value_type (value
)));
1201 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
1204 /* Dispose of the new value. This prevents watchpoints from
1205 trying to watch the saved frame pointer. */
1206 release_value (value
);
1210 frame_register (struct frame_info
*frame
, int regnum
,
1211 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
1212 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
1214 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1215 that the value proper does not need to be fetched. */
1216 gdb_assert (optimizedp
!= NULL
);
1217 gdb_assert (lvalp
!= NULL
);
1218 gdb_assert (addrp
!= NULL
);
1219 gdb_assert (realnump
!= NULL
);
1220 /* gdb_assert (bufferp != NULL); */
1222 /* Obtain the register value by unwinding the register from the next
1223 (more inner frame). */
1224 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
1225 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
1226 lvalp
, addrp
, realnump
, bufferp
);
1230 frame_unwind_register (frame_info
*next_frame
, int regnum
, gdb_byte
*buf
)
1236 enum lval_type lval
;
1238 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1239 &lval
, &addr
, &realnum
, buf
);
1242 throw_error (OPTIMIZED_OUT_ERROR
,
1243 _("Register %d was not saved"), regnum
);
1245 throw_error (NOT_AVAILABLE_ERROR
,
1246 _("Register %d is not available"), regnum
);
1250 get_frame_register (struct frame_info
*frame
,
1251 int regnum
, gdb_byte
*buf
)
1253 frame_unwind_register (frame
->next
, regnum
, buf
);
1257 frame_unwind_register_value (frame_info
*next_frame
, int regnum
)
1259 struct gdbarch
*gdbarch
;
1260 struct value
*value
;
1262 gdb_assert (next_frame
!= NULL
);
1263 gdbarch
= frame_unwind_arch (next_frame
);
1267 fprintf_unfiltered (gdb_stdlog
,
1268 "{ frame_unwind_register_value "
1269 "(frame=%d,regnum=%d(%s),...) ",
1270 next_frame
->level
, regnum
,
1271 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1274 /* Find the unwinder. */
1275 if (next_frame
->unwind
== NULL
)
1276 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
1278 /* Ask this frame to unwind its register. */
1279 value
= next_frame
->unwind
->prev_register (next_frame
,
1280 &next_frame
->prologue_cache
,
1285 fprintf_unfiltered (gdb_stdlog
, "->");
1286 if (value_optimized_out (value
))
1288 fprintf_unfiltered (gdb_stdlog
, " ");
1289 val_print_not_saved (gdb_stdlog
);
1293 if (VALUE_LVAL (value
) == lval_register
)
1294 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1295 VALUE_REGNUM (value
));
1296 else if (VALUE_LVAL (value
) == lval_memory
)
1297 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1299 value_address (value
)));
1301 fprintf_unfiltered (gdb_stdlog
, " computed");
1303 if (value_lazy (value
))
1304 fprintf_unfiltered (gdb_stdlog
, " lazy");
1308 const gdb_byte
*buf
= value_contents (value
);
1310 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1311 fprintf_unfiltered (gdb_stdlog
, "[");
1312 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1313 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1314 fprintf_unfiltered (gdb_stdlog
, "]");
1318 fprintf_unfiltered (gdb_stdlog
, " }\n");
1325 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1327 return frame_unwind_register_value (frame
->next
, regnum
);
1331 frame_unwind_register_signed (frame_info
*next_frame
, int regnum
)
1333 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1334 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1335 int size
= register_size (gdbarch
, regnum
);
1336 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1338 gdb_assert (value
!= NULL
);
1340 if (value_optimized_out (value
))
1342 throw_error (OPTIMIZED_OUT_ERROR
,
1343 _("Register %d was not saved"), regnum
);
1345 if (!value_entirely_available (value
))
1347 throw_error (NOT_AVAILABLE_ERROR
,
1348 _("Register %d is not available"), regnum
);
1351 LONGEST r
= extract_signed_integer (value_contents_all (value
), size
,
1354 release_value (value
);
1359 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1361 return frame_unwind_register_signed (frame
->next
, regnum
);
1365 frame_unwind_register_unsigned (frame_info
*next_frame
, int regnum
)
1367 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1368 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1369 int size
= register_size (gdbarch
, regnum
);
1370 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1372 gdb_assert (value
!= NULL
);
1374 if (value_optimized_out (value
))
1376 throw_error (OPTIMIZED_OUT_ERROR
,
1377 _("Register %d was not saved"), regnum
);
1379 if (!value_entirely_available (value
))
1381 throw_error (NOT_AVAILABLE_ERROR
,
1382 _("Register %d is not available"), regnum
);
1385 ULONGEST r
= extract_unsigned_integer (value_contents_all (value
), size
,
1388 release_value (value
);
1393 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1395 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1399 read_frame_register_unsigned (frame_info
*frame
, int regnum
,
1402 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1404 if (!value_optimized_out (regval
)
1405 && value_entirely_available (regval
))
1407 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1408 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1409 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1411 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1419 put_frame_register (struct frame_info
*frame
, int regnum
,
1420 const gdb_byte
*buf
)
1422 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1426 enum lval_type lval
;
1429 frame_register (frame
, regnum
, &optim
, &unavail
,
1430 &lval
, &addr
, &realnum
, NULL
);
1432 error (_("Attempt to assign to a register that was not saved."));
1437 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1441 get_current_regcache ()->cooked_write (realnum
, buf
);
1444 error (_("Attempt to assign to an unmodifiable value."));
1448 /* This function is deprecated. Use get_frame_register_value instead,
1449 which provides more accurate information.
1451 Find and return the value of REGNUM for the specified stack frame.
1452 The number of bytes copied is REGISTER_SIZE (REGNUM).
1454 Returns 0 if the register value could not be found. */
1457 deprecated_frame_register_read (frame_info
*frame
, int regnum
,
1462 enum lval_type lval
;
1466 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1467 &lval
, &addr
, &realnum
, myaddr
);
1469 return !optimized
&& !unavailable
;
1473 get_frame_register_bytes (frame_info
*frame
, int regnum
,
1474 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1475 int *optimizedp
, int *unavailablep
)
1477 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1482 /* Skip registers wholly inside of OFFSET. */
1483 while (offset
>= register_size (gdbarch
, regnum
))
1485 offset
-= register_size (gdbarch
, regnum
);
1489 /* Ensure that we will not read beyond the end of the register file.
1490 This can only ever happen if the debug information is bad. */
1492 numregs
= gdbarch_num_cooked_regs (gdbarch
);
1493 for (i
= regnum
; i
< numregs
; i
++)
1495 int thissize
= register_size (gdbarch
, i
);
1498 break; /* This register is not available on this architecture. */
1499 maxsize
+= thissize
;
1502 error (_("Bad debug information detected: "
1503 "Attempt to read %d bytes from registers."), len
);
1505 /* Copy the data. */
1508 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1513 if (curr_len
== register_size (gdbarch
, regnum
))
1515 enum lval_type lval
;
1519 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1520 &lval
, &addr
, &realnum
, myaddr
);
1521 if (*optimizedp
|| *unavailablep
)
1526 struct value
*value
= frame_unwind_register_value (frame
->next
,
1528 gdb_assert (value
!= NULL
);
1529 *optimizedp
= value_optimized_out (value
);
1530 *unavailablep
= !value_entirely_available (value
);
1532 if (*optimizedp
|| *unavailablep
)
1534 release_value (value
);
1538 memcpy (myaddr
, value_contents_all (value
) + offset
, curr_len
);
1539 release_value (value
);
1555 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1556 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1558 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1560 /* Skip registers wholly inside of OFFSET. */
1561 while (offset
>= register_size (gdbarch
, regnum
))
1563 offset
-= register_size (gdbarch
, regnum
);
1567 /* Copy the data. */
1570 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1575 if (curr_len
== register_size (gdbarch
, regnum
))
1577 put_frame_register (frame
, regnum
, myaddr
);
1581 struct value
*value
= frame_unwind_register_value (frame
->next
,
1583 gdb_assert (value
!= NULL
);
1585 memcpy ((char *) value_contents_writeable (value
) + offset
, myaddr
,
1587 put_frame_register (frame
, regnum
, value_contents_raw (value
));
1588 release_value (value
);
1598 /* Create a sentinel frame. */
1600 static struct frame_info
*
1601 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1603 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1606 frame
->pspace
= pspace
;
1607 frame
->aspace
= regcache
->aspace ();
1608 /* Explicitly initialize the sentinel frame's cache. Provide it
1609 with the underlying regcache. In the future additional
1610 information, such as the frame's thread will be added. */
1611 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1612 /* For the moment there is only one sentinel frame implementation. */
1613 frame
->unwind
= &sentinel_frame_unwind
;
1614 /* Link this frame back to itself. The frame is self referential
1615 (the unwound PC is the same as the pc), so make it so. */
1616 frame
->next
= frame
;
1617 /* The sentinel frame has a special ID. */
1618 frame
->this_id
.p
= frame_id_status::COMPUTED
;
1619 frame
->this_id
.value
= sentinel_frame_id
;
1622 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1623 fprint_frame (gdb_stdlog
, frame
);
1624 fprintf_unfiltered (gdb_stdlog
, " }\n");
1629 /* Cache for frame addresses already read by gdb. Valid only while
1630 inferior is stopped. Control variables for the frame cache should
1631 be local to this module. */
1633 static struct obstack frame_cache_obstack
;
1636 frame_obstack_zalloc (unsigned long size
)
1638 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1640 memset (data
, 0, size
);
1644 static struct frame_info
*get_prev_frame_always_1 (struct frame_info
*this_frame
);
1647 get_current_frame (void)
1649 struct frame_info
*current_frame
;
1651 /* First check, and report, the lack of registers. Having GDB
1652 report "No stack!" or "No memory" when the target doesn't even
1653 have registers is very confusing. Besides, "printcmd.exp"
1654 explicitly checks that ``print $pc'' with no registers prints "No
1656 if (!target_has_registers ())
1657 error (_("No registers."));
1658 if (!target_has_stack ())
1659 error (_("No stack."));
1660 if (!target_has_memory ())
1661 error (_("No memory."));
1662 /* Traceframes are effectively a substitute for the live inferior. */
1663 if (get_traceframe_number () < 0)
1664 validate_registers_access ();
1666 if (sentinel_frame
== NULL
)
1668 create_sentinel_frame (current_program_space
, get_current_regcache ());
1670 /* Set the current frame before computing the frame id, to avoid
1671 recursion inside compute_frame_id, in case the frame's
1672 unwinder decides to do a symbol lookup (which depends on the
1673 selected frame's block).
1675 This call must always succeed. In particular, nothing inside
1676 get_prev_frame_always_1 should try to unwind from the
1677 sentinel frame, because that could fail/throw, and we always
1678 want to leave with the current frame created and linked in --
1679 we should never end up with the sentinel frame as outermost
1681 current_frame
= get_prev_frame_always_1 (sentinel_frame
);
1682 gdb_assert (current_frame
!= NULL
);
1684 return current_frame
;
1687 /* The "selected" stack frame is used by default for local and arg
1688 access. May be zero, for no selected frame. */
1690 static struct frame_info
*selected_frame
;
1695 if (!target_has_registers () || !target_has_stack ()
1696 || !target_has_memory ())
1699 /* Traceframes are effectively a substitute for the live inferior. */
1700 if (get_traceframe_number () < 0)
1702 /* No current inferior, no frame. */
1703 if (inferior_ptid
== null_ptid
)
1706 thread_info
*tp
= inferior_thread ();
1707 /* Don't try to read from a dead thread. */
1708 if (tp
->state
== THREAD_EXITED
)
1711 /* ... or from a spinning thread. */
1719 /* Return the selected frame. Always non-NULL (unless there isn't an
1720 inferior sufficient for creating a frame) in which case an error is
1724 get_selected_frame (const char *message
)
1726 if (selected_frame
== NULL
)
1728 if (message
!= NULL
&& !has_stack_frames ())
1729 error (("%s"), message
);
1730 /* Hey! Don't trust this. It should really be re-finding the
1731 last selected frame of the currently selected thread. This,
1732 though, is better than nothing. */
1733 select_frame (get_current_frame ());
1735 /* There is always a frame. */
1736 gdb_assert (selected_frame
!= NULL
);
1737 return selected_frame
;
1740 /* If there is a selected frame, return it. Otherwise, return NULL. */
1743 get_selected_frame_if_set (void)
1745 return selected_frame
;
1748 /* This is a variant of get_selected_frame() which can be called when
1749 the inferior does not have a frame; in that case it will return
1750 NULL instead of calling error(). */
1753 deprecated_safe_get_selected_frame (void)
1755 if (!has_stack_frames ())
1757 return get_selected_frame (NULL
);
1760 /* Select frame FI (or NULL - to invalidate the current frame). */
1763 select_frame (struct frame_info
*fi
)
1765 selected_frame
= fi
;
1766 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1767 frame is being invalidated. */
1769 /* FIXME: kseitz/2002-08-28: It would be nice to call
1770 selected_frame_level_changed_event() right here, but due to limitations
1771 in the current interfaces, we would end up flooding UIs with events
1772 because select_frame() is used extensively internally.
1774 Once we have frame-parameterized frame (and frame-related) commands,
1775 the event notification can be moved here, since this function will only
1776 be called when the user's selected frame is being changed. */
1778 /* Ensure that symbols for this frame are read in. Also, determine the
1779 source language of this frame, and switch to it if desired. */
1784 /* We retrieve the frame's symtab by using the frame PC.
1785 However we cannot use the frame PC as-is, because it usually
1786 points to the instruction following the "call", which is
1787 sometimes the first instruction of another function. So we
1788 rely on get_frame_address_in_block() which provides us with a
1789 PC which is guaranteed to be inside the frame's code
1791 if (get_frame_address_in_block_if_available (fi
, &pc
))
1793 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
1796 && compunit_language (cust
) != current_language
->la_language
1797 && compunit_language (cust
) != language_unknown
1798 && language_mode
== language_mode_auto
)
1799 set_language (compunit_language (cust
));
1804 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1805 Always returns a non-NULL value. */
1808 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1810 struct frame_info
*fi
;
1814 fprintf_unfiltered (gdb_stdlog
,
1815 "{ create_new_frame (addr=%s, pc=%s) ",
1816 hex_string (addr
), hex_string (pc
));
1819 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1821 fi
->next
= create_sentinel_frame (current_program_space
,
1822 get_current_regcache ());
1824 /* Set/update this frame's cached PC value, found in the next frame.
1825 Do this before looking for this frame's unwinder. A sniffer is
1826 very likely to read this, and the corresponding unwinder is
1827 entitled to rely that the PC doesn't magically change. */
1828 fi
->next
->prev_pc
.value
= pc
;
1829 fi
->next
->prev_pc
.status
= CC_VALUE
;
1831 /* We currently assume that frame chain's can't cross spaces. */
1832 fi
->pspace
= fi
->next
->pspace
;
1833 fi
->aspace
= fi
->next
->aspace
;
1835 /* Select/initialize both the unwind function and the frame's type
1837 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1839 fi
->this_id
.p
= frame_id_status::COMPUTED
;
1840 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1844 fprintf_unfiltered (gdb_stdlog
, "-> ");
1845 fprint_frame (gdb_stdlog
, fi
);
1846 fprintf_unfiltered (gdb_stdlog
, " }\n");
1852 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1853 innermost frame). Be careful to not fall off the bottom of the
1854 frame chain and onto the sentinel frame. */
1857 get_next_frame (struct frame_info
*this_frame
)
1859 if (this_frame
->level
> 0)
1860 return this_frame
->next
;
1865 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1866 innermost (i.e. current) frame, return the sentinel frame. Thus,
1867 unlike get_next_frame(), NULL will never be returned. */
1870 get_next_frame_sentinel_okay (struct frame_info
*this_frame
)
1872 gdb_assert (this_frame
!= NULL
);
1874 /* Note that, due to the manner in which the sentinel frame is
1875 constructed, this_frame->next still works even when this_frame
1876 is the sentinel frame. But we disallow it here anyway because
1877 calling get_next_frame_sentinel_okay() on the sentinel frame
1878 is likely a coding error. */
1879 gdb_assert (this_frame
!= sentinel_frame
);
1881 return this_frame
->next
;
1884 /* Observer for the target_changed event. */
1887 frame_observer_target_changed (struct target_ops
*target
)
1889 reinit_frame_cache ();
1892 /* Flush the entire frame cache. */
1895 reinit_frame_cache (void)
1897 struct frame_info
*fi
;
1899 ++frame_cache_generation
;
1901 /* Tear down all frame caches. */
1902 for (fi
= sentinel_frame
; fi
!= NULL
; fi
= fi
->prev
)
1904 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1905 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1906 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1907 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1910 /* Since we can't really be sure what the first object allocated was. */
1911 obstack_free (&frame_cache_obstack
, 0);
1912 obstack_init (&frame_cache_obstack
);
1914 if (sentinel_frame
!= NULL
)
1915 annotate_frames_invalid ();
1917 sentinel_frame
= NULL
; /* Invalidate cache */
1918 select_frame (NULL
);
1919 frame_stash_invalidate ();
1921 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1924 /* Find where a register is saved (in memory or another register).
1925 The result of frame_register_unwind is just where it is saved
1926 relative to this particular frame. */
1929 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1930 int *optimizedp
, enum lval_type
*lvalp
,
1931 CORE_ADDR
*addrp
, int *realnump
)
1933 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1935 while (this_frame
!= NULL
)
1939 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1940 lvalp
, addrp
, realnump
, NULL
);
1945 if (*lvalp
!= lval_register
)
1949 this_frame
= get_next_frame (this_frame
);
1953 /* Get the previous raw frame, and check that it is not identical to
1954 same other frame frame already in the chain. If it is, there is
1955 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1956 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1957 validity tests, that compare THIS_FRAME and the next frame, we do
1958 this right after creating the previous frame, to avoid ever ending
1959 up with two frames with the same id in the frame chain. */
1961 static struct frame_info
*
1962 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1964 struct frame_info
*prev_frame
;
1966 prev_frame
= get_prev_frame_raw (this_frame
);
1968 /* Don't compute the frame id of the current frame yet. Unwinding
1969 the sentinel frame can fail (e.g., if the thread is gone and we
1970 can't thus read its registers). If we let the cycle detection
1971 code below try to compute a frame ID, then an error thrown from
1972 within the frame ID computation would result in the sentinel
1973 frame as outermost frame, which is bogus. Instead, we'll compute
1974 the current frame's ID lazily in get_frame_id. Note that there's
1975 no point in doing cycle detection when there's only one frame, so
1976 nothing is lost here. */
1977 if (prev_frame
->level
== 0)
1980 unsigned int entry_generation
= get_frame_cache_generation ();
1984 compute_frame_id (prev_frame
);
1985 if (!frame_stash_add (prev_frame
))
1987 /* Another frame with the same id was already in the stash. We just
1988 detected a cycle. */
1991 fprintf_unfiltered (gdb_stdlog
, "-> ");
1992 fprint_frame (gdb_stdlog
, NULL
);
1993 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1995 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1997 prev_frame
->next
= NULL
;
1998 this_frame
->prev
= NULL
;
2002 catch (const gdb_exception
&ex
)
2004 if (get_frame_cache_generation () == entry_generation
)
2006 prev_frame
->next
= NULL
;
2007 this_frame
->prev
= NULL
;
2016 /* Helper function for get_prev_frame_always, this is called inside a
2017 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
2018 there is no such frame. This may throw an exception. */
2020 static struct frame_info
*
2021 get_prev_frame_always_1 (struct frame_info
*this_frame
)
2023 struct gdbarch
*gdbarch
;
2025 gdb_assert (this_frame
!= NULL
);
2026 gdbarch
= get_frame_arch (this_frame
);
2030 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_always (this_frame=");
2031 if (this_frame
!= NULL
)
2032 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2034 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2035 fprintf_unfiltered (gdb_stdlog
, ") ");
2038 /* Only try to do the unwind once. */
2039 if (this_frame
->prev_p
)
2043 fprintf_unfiltered (gdb_stdlog
, "-> ");
2044 fprint_frame (gdb_stdlog
, this_frame
->prev
);
2045 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
2047 return this_frame
->prev
;
2050 /* If the frame unwinder hasn't been selected yet, we must do so
2051 before setting prev_p; otherwise the check for misbehaved
2052 sniffers will think that this frame's sniffer tried to unwind
2053 further (see frame_cleanup_after_sniffer). */
2054 if (this_frame
->unwind
== NULL
)
2055 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
2057 this_frame
->prev_p
= true;
2058 this_frame
->stop_reason
= UNWIND_NO_REASON
;
2060 /* If we are unwinding from an inline frame, all of the below tests
2061 were already performed when we unwound from the next non-inline
2062 frame. We must skip them, since we can not get THIS_FRAME's ID
2063 until we have unwound all the way down to the previous non-inline
2065 if (get_frame_type (this_frame
) == INLINE_FRAME
)
2066 return get_prev_frame_if_no_cycle (this_frame
);
2068 /* Check that this frame is unwindable. If it isn't, don't try to
2069 unwind to the prev frame. */
2070 this_frame
->stop_reason
2071 = this_frame
->unwind
->stop_reason (this_frame
,
2072 &this_frame
->prologue_cache
);
2074 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
2078 enum unwind_stop_reason reason
= this_frame
->stop_reason
;
2080 fprintf_unfiltered (gdb_stdlog
, "-> ");
2081 fprint_frame (gdb_stdlog
, NULL
);
2082 fprintf_unfiltered (gdb_stdlog
, " // %s }\n",
2083 frame_stop_reason_symbol_string (reason
));
2088 /* Check that this frame's ID isn't inner to (younger, below, next)
2089 the next frame. This happens when a frame unwind goes backwards.
2090 This check is valid only if this frame and the next frame are NORMAL.
2091 See the comment at frame_id_inner for details. */
2092 if (get_frame_type (this_frame
) == NORMAL_FRAME
2093 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
2094 && frame_id_inner (get_frame_arch (this_frame
->next
),
2095 get_frame_id (this_frame
),
2096 get_frame_id (this_frame
->next
)))
2098 CORE_ADDR this_pc_in_block
;
2099 struct minimal_symbol
*morestack_msym
;
2100 const char *morestack_name
= NULL
;
2102 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2103 this_pc_in_block
= get_frame_address_in_block (this_frame
);
2104 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
2106 morestack_name
= morestack_msym
->linkage_name ();
2107 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
2111 fprintf_unfiltered (gdb_stdlog
, "-> ");
2112 fprint_frame (gdb_stdlog
, NULL
);
2113 fprintf_unfiltered (gdb_stdlog
,
2114 " // this frame ID is inner }\n");
2116 this_frame
->stop_reason
= UNWIND_INNER_ID
;
2121 /* Check that this and the next frame do not unwind the PC register
2122 to the same memory location. If they do, then even though they
2123 have different frame IDs, the new frame will be bogus; two
2124 functions can't share a register save slot for the PC. This can
2125 happen when the prologue analyzer finds a stack adjustment, but
2128 This check does assume that the "PC register" is roughly a
2129 traditional PC, even if the gdbarch_unwind_pc method adjusts
2130 it (we do not rely on the value, only on the unwound PC being
2131 dependent on this value). A potential improvement would be
2132 to have the frame prev_pc method and the gdbarch unwind_pc
2133 method set the same lval and location information as
2134 frame_register_unwind. */
2135 if (this_frame
->level
> 0
2136 && gdbarch_pc_regnum (gdbarch
) >= 0
2137 && get_frame_type (this_frame
) == NORMAL_FRAME
2138 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
2139 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
2141 int optimized
, realnum
, nrealnum
;
2142 enum lval_type lval
, nlval
;
2143 CORE_ADDR addr
, naddr
;
2145 frame_register_unwind_location (this_frame
,
2146 gdbarch_pc_regnum (gdbarch
),
2147 &optimized
, &lval
, &addr
, &realnum
);
2148 frame_register_unwind_location (get_next_frame (this_frame
),
2149 gdbarch_pc_regnum (gdbarch
),
2150 &optimized
, &nlval
, &naddr
, &nrealnum
);
2152 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
2153 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
2157 fprintf_unfiltered (gdb_stdlog
, "-> ");
2158 fprint_frame (gdb_stdlog
, NULL
);
2159 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
2162 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2163 this_frame
->prev
= NULL
;
2168 return get_prev_frame_if_no_cycle (this_frame
);
2171 /* Return a "struct frame_info" corresponding to the frame that called
2172 THIS_FRAME. Returns NULL if there is no such frame.
2174 Unlike get_prev_frame, this function always tries to unwind the
2178 get_prev_frame_always (struct frame_info
*this_frame
)
2180 struct frame_info
*prev_frame
= NULL
;
2184 prev_frame
= get_prev_frame_always_1 (this_frame
);
2186 catch (const gdb_exception_error
&ex
)
2188 if (ex
.error
== MEMORY_ERROR
)
2190 this_frame
->stop_reason
= UNWIND_MEMORY_ERROR
;
2191 if (ex
.message
!= NULL
)
2196 /* The error needs to live as long as the frame does.
2197 Allocate using stack local STOP_STRING then assign the
2198 pointer to the frame, this allows the STOP_STRING on the
2199 frame to be of type 'const char *'. */
2200 size
= ex
.message
->size () + 1;
2201 stop_string
= (char *) frame_obstack_zalloc (size
);
2202 memcpy (stop_string
, ex
.what (), size
);
2203 this_frame
->stop_string
= stop_string
;
2214 /* Construct a new "struct frame_info" and link it previous to
2217 static struct frame_info
*
2218 get_prev_frame_raw (struct frame_info
*this_frame
)
2220 struct frame_info
*prev_frame
;
2222 /* Allocate the new frame but do not wire it in to the frame chain.
2223 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2224 frame->next to pull some fancy tricks (of course such code is, by
2225 definition, recursive). Try to prevent it.
2227 There is no reason to worry about memory leaks, should the
2228 remainder of the function fail. The allocated memory will be
2229 quickly reclaimed when the frame cache is flushed, and the `we've
2230 been here before' check above will stop repeated memory
2231 allocation calls. */
2232 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2233 prev_frame
->level
= this_frame
->level
+ 1;
2235 /* For now, assume we don't have frame chains crossing address
2237 prev_frame
->pspace
= this_frame
->pspace
;
2238 prev_frame
->aspace
= this_frame
->aspace
;
2240 /* Don't yet compute ->unwind (and hence ->type). It is computed
2241 on-demand in get_frame_type, frame_register_unwind, and
2244 /* Don't yet compute the frame's ID. It is computed on-demand by
2247 /* The unwound frame ID is validate at the start of this function,
2248 as part of the logic to decide if that frame should be further
2249 unwound, and not here while the prev frame is being created.
2250 Doing this makes it possible for the user to examine a frame that
2251 has an invalid frame ID.
2253 Some very old VAX code noted: [...] For the sake of argument,
2254 suppose that the stack is somewhat trashed (which is one reason
2255 that "info frame" exists). So, return 0 (indicating we don't
2256 know the address of the arglist) if we don't know what frame this
2260 this_frame
->prev
= prev_frame
;
2261 prev_frame
->next
= this_frame
;
2265 fprintf_unfiltered (gdb_stdlog
, "-> ");
2266 fprint_frame (gdb_stdlog
, prev_frame
);
2267 fprintf_unfiltered (gdb_stdlog
, " }\n");
2273 /* Debug routine to print a NULL frame being returned. */
2276 frame_debug_got_null_frame (struct frame_info
*this_frame
,
2281 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
2282 if (this_frame
!= NULL
)
2283 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2285 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2286 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
2290 /* Is this (non-sentinel) frame in the "main"() function? */
2293 inside_main_func (frame_info
*this_frame
)
2295 if (symfile_objfile
== nullptr)
2298 bound_minimal_symbol msymbol
2299 = lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
2300 if (msymbol
.minsym
== nullptr)
2303 /* Make certain that the code, and not descriptor, address is
2306 = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
2307 BMSYMBOL_VALUE_ADDRESS (msymbol
),
2308 current_top_target ());
2310 return maddr
== get_frame_func (this_frame
);
2313 /* Test whether THIS_FRAME is inside the process entry point function. */
2316 inside_entry_func (frame_info
*this_frame
)
2318 CORE_ADDR entry_point
;
2320 if (!entry_point_address_query (&entry_point
))
2323 return get_frame_func (this_frame
) == entry_point
;
2326 /* Return a structure containing various interesting information about
2327 the frame that called THIS_FRAME. Returns NULL if there is entier
2328 no such frame or the frame fails any of a set of target-independent
2329 condition that should terminate the frame chain (e.g., as unwinding
2332 This function should not contain target-dependent tests, such as
2333 checking whether the program-counter is zero. */
2336 get_prev_frame (struct frame_info
*this_frame
)
2341 /* There is always a frame. If this assertion fails, suspect that
2342 something should be calling get_selected_frame() or
2343 get_current_frame(). */
2344 gdb_assert (this_frame
!= NULL
);
2346 /* If this_frame is the current frame, then compute and stash
2347 its frame id prior to fetching and computing the frame id of the
2348 previous frame. Otherwise, the cycle detection code in
2349 get_prev_frame_if_no_cycle() will not work correctly. When
2350 get_frame_id() is called later on, an assertion error will
2351 be triggered in the event of a cycle between the current
2352 frame and its previous frame. */
2353 if (this_frame
->level
== 0)
2354 get_frame_id (this_frame
);
2356 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2358 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2359 sense to stop unwinding at a dummy frame. One place where a dummy
2360 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2361 pcsqh register (space register for the instruction at the head of the
2362 instruction queue) cannot be written directly; the only way to set it
2363 is to branch to code that is in the target space. In order to implement
2364 frame dummies on HPUX, the called function is made to jump back to where
2365 the inferior was when the user function was called. If gdb was inside
2366 the main function when we created the dummy frame, the dummy frame will
2367 point inside the main function. */
2368 if (this_frame
->level
>= 0
2369 && get_frame_type (this_frame
) == NORMAL_FRAME
2370 && !user_set_backtrace_options
.backtrace_past_main
2372 && inside_main_func (this_frame
))
2373 /* Don't unwind past main(). Note, this is done _before_ the
2374 frame has been marked as previously unwound. That way if the
2375 user later decides to enable unwinds past main(), that will
2376 automatically happen. */
2378 frame_debug_got_null_frame (this_frame
, "inside main func");
2382 /* If the user's backtrace limit has been exceeded, stop. We must
2383 add two to the current level; one of those accounts for backtrace_limit
2384 being 1-based and the level being 0-based, and the other accounts for
2385 the level of the new frame instead of the level of the current
2387 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2389 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2393 /* If we're already inside the entry function for the main objfile,
2394 then it isn't valid. Don't apply this test to a dummy frame -
2395 dummy frame PCs typically land in the entry func. Don't apply
2396 this test to the sentinel frame. Sentinel frames should always
2397 be allowed to unwind. */
2398 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2399 wasn't checking for "main" in the minimal symbols. With that
2400 fixed asm-source tests now stop in "main" instead of halting the
2401 backtrace in weird and wonderful ways somewhere inside the entry
2402 file. Suspect that tests for inside the entry file/func were
2403 added to work around that (now fixed) case. */
2404 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2405 suggested having the inside_entry_func test use the
2406 inside_main_func() msymbol trick (along with entry_point_address()
2407 I guess) to determine the address range of the start function.
2408 That should provide a far better stopper than the current
2410 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2411 applied tail-call optimizations to main so that a function called
2412 from main returns directly to the caller of main. Since we don't
2413 stop at main, we should at least stop at the entry point of the
2415 if (this_frame
->level
>= 0
2416 && get_frame_type (this_frame
) == NORMAL_FRAME
2417 && !user_set_backtrace_options
.backtrace_past_entry
2419 && inside_entry_func (this_frame
))
2421 frame_debug_got_null_frame (this_frame
, "inside entry func");
2425 /* Assume that the only way to get a zero PC is through something
2426 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2427 will never unwind a zero PC. */
2428 if (this_frame
->level
> 0
2429 && (get_frame_type (this_frame
) == NORMAL_FRAME
2430 || get_frame_type (this_frame
) == INLINE_FRAME
)
2431 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2432 && frame_pc_p
&& frame_pc
== 0)
2434 frame_debug_got_null_frame (this_frame
, "zero PC");
2438 return get_prev_frame_always (this_frame
);
2442 get_prev_frame_id_by_id (struct frame_id id
)
2444 struct frame_id prev_id
;
2445 struct frame_info
*frame
;
2447 frame
= frame_find_by_id (id
);
2450 prev_id
= get_frame_id (get_prev_frame (frame
));
2452 prev_id
= null_frame_id
;
2458 get_frame_pc (struct frame_info
*frame
)
2460 gdb_assert (frame
->next
!= NULL
);
2461 return frame_unwind_pc (frame
->next
);
2465 get_frame_pc_if_available (frame_info
*frame
, CORE_ADDR
*pc
)
2468 gdb_assert (frame
->next
!= NULL
);
2472 *pc
= frame_unwind_pc (frame
->next
);
2474 catch (const gdb_exception_error
&ex
)
2476 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2485 /* Return an address that falls within THIS_FRAME's code block. */
2488 get_frame_address_in_block (struct frame_info
*this_frame
)
2490 /* A draft address. */
2491 CORE_ADDR pc
= get_frame_pc (this_frame
);
2493 struct frame_info
*next_frame
= this_frame
->next
;
2495 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2496 Normally the resume address is inside the body of the function
2497 associated with THIS_FRAME, but there is a special case: when
2498 calling a function which the compiler knows will never return
2499 (for instance abort), the call may be the very last instruction
2500 in the calling function. The resume address will point after the
2501 call and may be at the beginning of a different function
2504 If THIS_FRAME is a signal frame or dummy frame, then we should
2505 not adjust the unwound PC. For a dummy frame, GDB pushed the
2506 resume address manually onto the stack. For a signal frame, the
2507 OS may have pushed the resume address manually and invoked the
2508 handler (e.g. GNU/Linux), or invoked the trampoline which called
2509 the signal handler - but in either case the signal handler is
2510 expected to return to the trampoline. So in both of these
2511 cases we know that the resume address is executable and
2512 related. So we only need to adjust the PC if THIS_FRAME
2513 is a normal function.
2515 If the program has been interrupted while THIS_FRAME is current,
2516 then clearly the resume address is inside the associated
2517 function. There are three kinds of interruption: debugger stop
2518 (next frame will be SENTINEL_FRAME), operating system
2519 signal or exception (next frame will be SIGTRAMP_FRAME),
2520 or debugger-induced function call (next frame will be
2521 DUMMY_FRAME). So we only need to adjust the PC if
2522 NEXT_FRAME is a normal function.
2524 We check the type of NEXT_FRAME first, since it is already
2525 known; frame type is determined by the unwinder, and since
2526 we have THIS_FRAME we've already selected an unwinder for
2529 If the next frame is inlined, we need to keep going until we find
2530 the real function - for instance, if a signal handler is invoked
2531 while in an inlined function, then the code address of the
2532 "calling" normal function should not be adjusted either. */
2534 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2535 next_frame
= next_frame
->next
;
2537 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2538 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2539 && (get_frame_type (this_frame
) == NORMAL_FRAME
2540 || get_frame_type (this_frame
) == TAILCALL_FRAME
2541 || get_frame_type (this_frame
) == INLINE_FRAME
))
2548 get_frame_address_in_block_if_available (frame_info
*this_frame
,
2554 *pc
= get_frame_address_in_block (this_frame
);
2556 catch (const gdb_exception_error
&ex
)
2558 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2567 find_frame_sal (frame_info
*frame
)
2569 struct frame_info
*next_frame
;
2573 if (frame_inlined_callees (frame
) > 0)
2577 /* If the current frame has some inlined callees, and we have a next
2578 frame, then that frame must be an inlined frame. In this case
2579 this frame's sal is the "call site" of the next frame's inlined
2580 function, which can not be inferred from get_frame_pc. */
2581 next_frame
= get_next_frame (frame
);
2583 sym
= get_frame_function (next_frame
);
2585 sym
= inline_skipped_symbol (inferior_thread ());
2587 /* If frame is inline, it certainly has symbols. */
2590 symtab_and_line sal
;
2591 if (SYMBOL_LINE (sym
) != 0)
2593 sal
.symtab
= symbol_symtab (sym
);
2594 sal
.line
= SYMBOL_LINE (sym
);
2597 /* If the symbol does not have a location, we don't know where
2598 the call site is. Do not pretend to. This is jarring, but
2599 we can't do much better. */
2600 sal
.pc
= get_frame_pc (frame
);
2602 sal
.pspace
= get_frame_program_space (frame
);
2606 /* If FRAME is not the innermost frame, that normally means that
2607 FRAME->pc points at the return instruction (which is *after* the
2608 call instruction), and we want to get the line containing the
2609 call (because the call is where the user thinks the program is).
2610 However, if the next frame is either a SIGTRAMP_FRAME or a
2611 DUMMY_FRAME, then the next frame will contain a saved interrupt
2612 PC and such a PC indicates the current (rather than next)
2613 instruction/line, consequently, for such cases, want to get the
2614 line containing fi->pc. */
2615 if (!get_frame_pc_if_available (frame
, &pc
))
2618 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2619 return find_pc_line (pc
, notcurrent
);
2622 /* Per "frame.h", return the ``address'' of the frame. Code should
2623 really be using get_frame_id(). */
2625 get_frame_base (struct frame_info
*fi
)
2627 return get_frame_id (fi
).stack_addr
;
2630 /* High-level offsets into the frame. Used by the debug info. */
2633 get_frame_base_address (struct frame_info
*fi
)
2635 if (get_frame_type (fi
) != NORMAL_FRAME
)
2637 if (fi
->base
== NULL
)
2638 fi
->base
= frame_base_find_by_frame (fi
);
2639 /* Sneaky: If the low-level unwind and high-level base code share a
2640 common unwinder, let them share the prologue cache. */
2641 if (fi
->base
->unwind
== fi
->unwind
)
2642 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2643 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2647 get_frame_locals_address (struct frame_info
*fi
)
2649 if (get_frame_type (fi
) != NORMAL_FRAME
)
2651 /* If there isn't a frame address method, find it. */
2652 if (fi
->base
== NULL
)
2653 fi
->base
= frame_base_find_by_frame (fi
);
2654 /* Sneaky: If the low-level unwind and high-level base code share a
2655 common unwinder, let them share the prologue cache. */
2656 if (fi
->base
->unwind
== fi
->unwind
)
2657 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2658 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2662 get_frame_args_address (struct frame_info
*fi
)
2664 if (get_frame_type (fi
) != NORMAL_FRAME
)
2666 /* If there isn't a frame address method, find it. */
2667 if (fi
->base
== NULL
)
2668 fi
->base
= frame_base_find_by_frame (fi
);
2669 /* Sneaky: If the low-level unwind and high-level base code share a
2670 common unwinder, let them share the prologue cache. */
2671 if (fi
->base
->unwind
== fi
->unwind
)
2672 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2673 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2676 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2680 frame_unwinder_is (frame_info
*fi
, const frame_unwind
*unwinder
)
2682 if (fi
->unwind
== nullptr)
2683 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2685 return fi
->unwind
== unwinder
;
2688 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2689 or -1 for a NULL frame. */
2692 frame_relative_level (struct frame_info
*fi
)
2701 get_frame_type (struct frame_info
*frame
)
2703 if (frame
->unwind
== NULL
)
2704 /* Initialize the frame's unwinder because that's what
2705 provides the frame's type. */
2706 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2707 return frame
->unwind
->type
;
2710 struct program_space
*
2711 get_frame_program_space (struct frame_info
*frame
)
2713 return frame
->pspace
;
2716 struct program_space
*
2717 frame_unwind_program_space (struct frame_info
*this_frame
)
2719 gdb_assert (this_frame
);
2721 /* This is really a placeholder to keep the API consistent --- we
2722 assume for now that we don't have frame chains crossing
2724 return this_frame
->pspace
;
2727 const address_space
*
2728 get_frame_address_space (struct frame_info
*frame
)
2730 return frame
->aspace
;
2733 /* Memory access methods. */
2736 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2737 gdb_byte
*buf
, int len
)
2739 read_memory (addr
, buf
, len
);
2743 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2746 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2747 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2749 return read_memory_integer (addr
, len
, byte_order
);
2753 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2756 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2757 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2759 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2763 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2764 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2766 /* NOTE: target_read_memory returns zero on success! */
2767 return target_read_memory (addr
, buf
, len
) == 0;
2770 /* Architecture methods. */
2773 get_frame_arch (struct frame_info
*this_frame
)
2775 return frame_unwind_arch (this_frame
->next
);
2779 frame_unwind_arch (struct frame_info
*next_frame
)
2781 if (!next_frame
->prev_arch
.p
)
2783 struct gdbarch
*arch
;
2785 if (next_frame
->unwind
== NULL
)
2786 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2788 if (next_frame
->unwind
->prev_arch
!= NULL
)
2789 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2790 &next_frame
->prologue_cache
);
2792 arch
= get_frame_arch (next_frame
);
2794 next_frame
->prev_arch
.arch
= arch
;
2795 next_frame
->prev_arch
.p
= true;
2797 fprintf_unfiltered (gdb_stdlog
,
2798 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2800 gdbarch_bfd_arch_info (arch
)->printable_name
);
2803 return next_frame
->prev_arch
.arch
;
2807 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2809 next_frame
= skip_artificial_frames (next_frame
);
2811 /* We must have a non-artificial frame. The caller is supposed to check
2812 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2814 gdb_assert (next_frame
!= NULL
);
2816 return frame_unwind_arch (next_frame
);
2819 /* Gets the language of FRAME. */
2822 get_frame_language (struct frame_info
*frame
)
2827 gdb_assert (frame
!= NULL
);
2829 /* We determine the current frame language by looking up its
2830 associated symtab. To retrieve this symtab, we use the frame
2831 PC. However we cannot use the frame PC as is, because it
2832 usually points to the instruction following the "call", which
2833 is sometimes the first instruction of another function. So
2834 we rely on get_frame_address_in_block(), it provides us with
2835 a PC that is guaranteed to be inside the frame's code
2840 pc
= get_frame_address_in_block (frame
);
2843 catch (const gdb_exception_error
&ex
)
2845 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
2851 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
2854 return compunit_language (cust
);
2857 return language_unknown
;
2860 /* Stack pointer methods. */
2863 get_frame_sp (struct frame_info
*this_frame
)
2865 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2867 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2868 operate on THIS_FRAME now. */
2869 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2872 /* Return the reason why we can't unwind past FRAME. */
2874 enum unwind_stop_reason
2875 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2877 /* Fill-in STOP_REASON. */
2878 get_prev_frame_always (frame
);
2879 gdb_assert (frame
->prev_p
);
2881 return frame
->stop_reason
;
2884 /* Return a string explaining REASON. */
2887 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
2891 #define SET(name, description) \
2892 case name: return _(description);
2893 #include "unwind_stop_reasons.def"
2897 internal_error (__FILE__
, __LINE__
,
2898 "Invalid frame stop reason");
2903 frame_stop_reason_string (struct frame_info
*fi
)
2905 gdb_assert (fi
->prev_p
);
2906 gdb_assert (fi
->prev
== NULL
);
2908 /* Return the specific string if we have one. */
2909 if (fi
->stop_string
!= NULL
)
2910 return fi
->stop_string
;
2912 /* Return the generic string if we have nothing better. */
2913 return unwind_stop_reason_to_string (fi
->stop_reason
);
2916 /* Return the enum symbol name of REASON as a string, to use in debug
2920 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
2924 #define SET(name, description) \
2925 case name: return #name;
2926 #include "unwind_stop_reasons.def"
2930 internal_error (__FILE__
, __LINE__
,
2931 "Invalid frame stop reason");
2935 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2939 frame_cleanup_after_sniffer (struct frame_info
*frame
)
2941 /* The sniffer should not allocate a prologue cache if it did not
2942 match this frame. */
2943 gdb_assert (frame
->prologue_cache
== NULL
);
2945 /* No sniffer should extend the frame chain; sniff based on what is
2947 gdb_assert (!frame
->prev_p
);
2949 /* The sniffer should not check the frame's ID; that's circular. */
2950 gdb_assert (frame
->this_id
.p
!= frame_id_status::COMPUTED
);
2952 /* Clear cached fields dependent on the unwinder.
2954 The previous PC is independent of the unwinder, but the previous
2955 function is not (see get_frame_address_in_block). */
2956 frame
->prev_func
.status
= CC_UNKNOWN
;
2957 frame
->prev_func
.addr
= 0;
2959 /* Discard the unwinder last, so that we can easily find it if an assertion
2960 in this function triggers. */
2961 frame
->unwind
= NULL
;
2964 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2965 If sniffing fails, the caller should be sure to call
2966 frame_cleanup_after_sniffer. */
2969 frame_prepare_for_sniffer (struct frame_info
*frame
,
2970 const struct frame_unwind
*unwind
)
2972 gdb_assert (frame
->unwind
== NULL
);
2973 frame
->unwind
= unwind
;
2976 static struct cmd_list_element
*set_backtrace_cmdlist
;
2977 static struct cmd_list_element
*show_backtrace_cmdlist
;
2979 /* Definition of the "set backtrace" settings that are exposed as
2980 "backtrace" command options. */
2982 using boolean_option_def
2983 = gdb::option::boolean_option_def
<set_backtrace_options
>;
2984 using uinteger_option_def
2985 = gdb::option::uinteger_option_def
<set_backtrace_options
>;
2987 const gdb::option::option_def set_backtrace_option_defs
[] = {
2989 boolean_option_def
{
2991 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_main
; },
2992 show_backtrace_past_main
, /* show_cmd_cb */
2993 N_("Set whether backtraces should continue past \"main\"."),
2994 N_("Show whether backtraces should continue past \"main\"."),
2995 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2996 the backtrace at \"main\". Set this if you need to see the rest\n\
2997 of the stack trace."),
3000 boolean_option_def
{
3002 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_entry
; },
3003 show_backtrace_past_entry
, /* show_cmd_cb */
3004 N_("Set whether backtraces should continue past the entry point of a program."),
3005 N_("Show whether backtraces should continue past the entry point of a program."),
3006 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
3007 will terminate the backtrace there. Set this if you need to see\n\
3008 the rest of the stack trace."),
3012 void _initialize_frame ();
3014 _initialize_frame ()
3016 obstack_init (&frame_cache_obstack
);
3018 frame_stash_create ();
3020 gdb::observers::target_changed
.attach (frame_observer_target_changed
);
3022 add_basic_prefix_cmd ("backtrace", class_maintenance
, _("\
3023 Set backtrace specific variables.\n\
3024 Configure backtrace variables such as the backtrace limit"),
3025 &set_backtrace_cmdlist
, "set backtrace ",
3026 0/*allow-unknown*/, &setlist
);
3027 add_show_prefix_cmd ("backtrace", class_maintenance
, _("\
3028 Show backtrace specific variables.\n\
3029 Show backtrace variables such as the backtrace limit."),
3030 &show_backtrace_cmdlist
, "show backtrace ",
3031 0/*allow-unknown*/, &showlist
);
3033 add_setshow_uinteger_cmd ("limit", class_obscure
,
3034 &user_set_backtrace_options
.backtrace_limit
, _("\
3035 Set an upper bound on the number of backtrace levels."), _("\
3036 Show the upper bound on the number of backtrace levels."), _("\
3037 No more than the specified number of frames can be displayed or examined.\n\
3038 Literal \"unlimited\" or zero means no limit."),
3040 show_backtrace_limit
,
3041 &set_backtrace_cmdlist
,
3042 &show_backtrace_cmdlist
);
3044 gdb::option::add_setshow_cmds_for_options
3045 (class_stack
, &user_set_backtrace_options
,
3046 set_backtrace_option_defs
, &set_backtrace_cmdlist
, &show_backtrace_cmdlist
);
3048 /* Debug this files internals. */
3049 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
3050 Set frame debugging."), _("\
3051 Show frame debugging."), _("\
3052 When non-zero, frame specific internal debugging is enabled."),
3055 &setdebuglist
, &showdebuglist
);