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 /* We keep a cache of stack frames, each of which is a "struct
91 frame_info". The innermost one gets allocated (in
92 wait_for_inferior) each time the inferior stops; sentinel_frame
93 points to it. Additional frames get allocated (in get_prev_frame)
94 as needed, and are chained through the next and prev fields. Any
95 time that the frame cache becomes invalid (most notably when we
96 execute something, but also if we change how we interpret the
97 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
98 which reads new symbols)), we should call reinit_frame_cache. */
102 /* Level of this frame. The inner-most (youngest) frame is at level
103 0. As you move towards the outer-most (oldest) frame, the level
104 increases. This is a cached value. It could just as easily be
105 computed by counting back from the selected frame to the inner
107 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
108 reserved to indicate a bogus frame - one that has been created
109 just to keep GDB happy (GDB always needs a frame). For the
110 moment leave this as speculation. */
113 /* The frame's program space. */
114 struct program_space
*pspace
;
116 /* The frame's address space. */
117 const address_space
*aspace
;
119 /* The frame's low-level unwinder and corresponding cache. The
120 low-level unwinder is responsible for unwinding register values
121 for the previous frame. The low-level unwind methods are
122 selected based on the presence, or otherwise, of register unwind
123 information such as CFI. */
124 void *prologue_cache
;
125 const struct frame_unwind
*unwind
;
127 /* Cached copy of the previous frame's architecture. */
131 struct gdbarch
*arch
;
134 /* Cached copy of the previous frame's resume address. */
136 cached_copy_status status
;
137 /* Did VALUE require unmasking when being read. */
142 /* Cached copy of the previous frame's function address. */
146 cached_copy_status status
;
149 /* This frame's ID. */
153 struct frame_id value
;
156 /* The frame's high-level base methods, and corresponding cache.
157 The high level base methods are selected based on the frame's
159 const struct frame_base
*base
;
162 /* Pointers to the next (down, inner, younger) and previous (up,
163 outer, older) frame_info's in the frame cache. */
164 struct frame_info
*next
; /* down, inner, younger */
166 struct frame_info
*prev
; /* up, outer, older */
168 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
169 could. Only valid when PREV_P is set. */
170 enum unwind_stop_reason stop_reason
;
172 /* A frame specific string describing the STOP_REASON in more detail.
173 Only valid when PREV_P is set, but even then may still be NULL. */
174 const char *stop_string
;
180 set_frame_previous_pc_masked (struct frame_info
*frame
)
182 frame
->prev_pc
.masked
= true;
188 get_frame_pc_masked (const struct frame_info
*frame
)
190 gdb_assert (frame
->next
!= nullptr);
191 gdb_assert (frame
->next
->prev_pc
.status
== CC_VALUE
);
193 return frame
->next
->prev_pc
.masked
;
196 /* A frame stash used to speed up frame lookups. Create a hash table
197 to stash frames previously accessed from the frame cache for
198 quicker subsequent retrieval. The hash table is emptied whenever
199 the frame cache is invalidated. */
201 static htab_t frame_stash
;
203 /* Internal function to calculate a hash from the frame_id addresses,
204 using as many valid addresses as possible. Frames below level 0
205 are not stored in the hash table. */
208 frame_addr_hash (const void *ap
)
210 const struct frame_info
*frame
= (const struct frame_info
*) ap
;
211 const struct frame_id f_id
= frame
->this_id
.value
;
214 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
216 || f_id
.special_addr_p
);
218 if (f_id
.stack_status
== FID_STACK_VALID
)
219 hash
= iterative_hash (&f_id
.stack_addr
,
220 sizeof (f_id
.stack_addr
), hash
);
221 if (f_id
.code_addr_p
)
222 hash
= iterative_hash (&f_id
.code_addr
,
223 sizeof (f_id
.code_addr
), hash
);
224 if (f_id
.special_addr_p
)
225 hash
= iterative_hash (&f_id
.special_addr
,
226 sizeof (f_id
.special_addr
), hash
);
231 /* Internal equality function for the hash table. This function
232 defers equality operations to frame_id_eq. */
235 frame_addr_hash_eq (const void *a
, const void *b
)
237 const struct frame_info
*f_entry
= (const struct frame_info
*) a
;
238 const struct frame_info
*f_element
= (const struct frame_info
*) b
;
240 return frame_id_eq (f_entry
->this_id
.value
,
241 f_element
->this_id
.value
);
244 /* Internal function to create the frame_stash hash table. 100 seems
245 to be a good compromise to start the hash table at. */
248 frame_stash_create (void)
250 frame_stash
= htab_create (100,
256 /* Internal function to add a frame to the frame_stash hash table.
257 Returns false if a frame with the same ID was already stashed, true
261 frame_stash_add (frame_info
*frame
)
263 /* Do not try to stash the sentinel frame. */
264 gdb_assert (frame
->level
>= 0);
266 frame_info
**slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
269 /* If we already have a frame in the stack with the same id, we
270 either have a stack cycle (corrupted stack?), or some bug
271 elsewhere in GDB. In any case, ignore the duplicate and return
272 an indication to the caller. */
273 if (*slot
!= nullptr)
280 /* Internal function to search the frame stash for an entry with the
281 given frame ID. If found, return that frame. Otherwise return
284 static struct frame_info
*
285 frame_stash_find (struct frame_id id
)
287 struct frame_info dummy
;
288 struct frame_info
*frame
;
290 dummy
.this_id
.value
= id
;
291 frame
= (struct frame_info
*) htab_find (frame_stash
, &dummy
);
295 /* Internal function to invalidate the frame stash by removing all
296 entries in it. This only occurs when the frame cache is
300 frame_stash_invalidate (void)
302 htab_empty (frame_stash
);
306 scoped_restore_selected_frame::scoped_restore_selected_frame ()
308 m_fid
= get_frame_id (get_selected_frame (NULL
));
312 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
314 frame_info
*frame
= frame_find_by_id (m_fid
);
316 warning (_("Unable to restore previously selected frame."));
318 select_frame (frame
);
321 /* Flag to control debugging. */
323 unsigned int frame_debug
;
325 show_frame_debug (struct ui_file
*file
, int from_tty
,
326 struct cmd_list_element
*c
, const char *value
)
328 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
331 /* Implementation of "show backtrace past-main". */
334 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
335 struct cmd_list_element
*c
, const char *value
)
337 fprintf_filtered (file
,
338 _("Whether backtraces should "
339 "continue past \"main\" is %s.\n"),
343 /* Implementation of "show backtrace past-entry". */
346 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
347 struct cmd_list_element
*c
, const char *value
)
349 fprintf_filtered (file
, _("Whether backtraces should continue past the "
350 "entry point of a program is %s.\n"),
354 /* Implementation of "show backtrace limit". */
357 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
358 struct cmd_list_element
*c
, const char *value
)
360 fprintf_filtered (file
,
361 _("An upper bound on the number "
362 "of backtrace levels is %s.\n"),
368 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
371 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
373 fprintf_unfiltered (file
, "!%s", name
);
377 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
379 fprintf_unfiltered (file
, "{");
381 if (id
.stack_status
== FID_STACK_INVALID
)
382 fprintf_unfiltered (file
, "!stack");
383 else if (id
.stack_status
== FID_STACK_UNAVAILABLE
)
384 fprintf_unfiltered (file
, "stack=<unavailable>");
385 else if (id
.stack_status
== FID_STACK_SENTINEL
)
386 fprintf_unfiltered (file
, "stack=<sentinel>");
388 fprintf_unfiltered (file
, "stack=%s", hex_string (id
.stack_addr
));
389 fprintf_unfiltered (file
, ",");
391 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
392 fprintf_unfiltered (file
, ",");
394 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
396 if (id
.artificial_depth
)
397 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
399 fprintf_unfiltered (file
, "}");
403 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
408 fprintf_unfiltered (file
, "NORMAL_FRAME");
411 fprintf_unfiltered (file
, "DUMMY_FRAME");
414 fprintf_unfiltered (file
, "INLINE_FRAME");
417 fprintf_unfiltered (file
, "TAILCALL_FRAME");
420 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
423 fprintf_unfiltered (file
, "ARCH_FRAME");
426 fprintf_unfiltered (file
, "SENTINEL_FRAME");
429 fprintf_unfiltered (file
, "<unknown type>");
435 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
439 fprintf_unfiltered (file
, "<NULL frame>");
442 fprintf_unfiltered (file
, "{");
443 fprintf_unfiltered (file
, "level=%d", fi
->level
);
444 fprintf_unfiltered (file
, ",");
445 fprintf_unfiltered (file
, "type=");
446 if (fi
->unwind
!= NULL
)
447 fprint_frame_type (file
, fi
->unwind
->type
);
449 fprintf_unfiltered (file
, "<unknown>");
450 fprintf_unfiltered (file
, ",");
451 fprintf_unfiltered (file
, "unwind=");
452 if (fi
->unwind
!= NULL
)
453 gdb_print_host_address (fi
->unwind
, file
);
455 fprintf_unfiltered (file
, "<unknown>");
456 fprintf_unfiltered (file
, ",");
457 fprintf_unfiltered (file
, "pc=");
458 if (fi
->next
== NULL
|| fi
->next
->prev_pc
.status
== CC_UNKNOWN
)
459 fprintf_unfiltered (file
, "<unknown>");
460 else if (fi
->next
->prev_pc
.status
== CC_VALUE
)
462 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_pc
.value
));
463 if (fi
->next
->prev_pc
.masked
)
464 fprintf_unfiltered (file
, "[PAC]");
466 else if (fi
->next
->prev_pc
.status
== CC_NOT_SAVED
)
467 val_print_not_saved (file
);
468 else if (fi
->next
->prev_pc
.status
== CC_UNAVAILABLE
)
469 val_print_unavailable (file
);
470 fprintf_unfiltered (file
, ",");
471 fprintf_unfiltered (file
, "id=");
473 fprint_frame_id (file
, fi
->this_id
.value
);
475 fprintf_unfiltered (file
, "<unknown>");
476 fprintf_unfiltered (file
, ",");
477 fprintf_unfiltered (file
, "func=");
478 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.status
== CC_VALUE
)
479 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
481 fprintf_unfiltered (file
, "<unknown>");
482 fprintf_unfiltered (file
, "}");
485 /* Given FRAME, return the enclosing frame as found in real frames read-in from
486 inferior memory. Skip any previous frames which were made up by GDB.
487 Return FRAME if FRAME is a non-artificial frame.
488 Return NULL if FRAME is the start of an artificial-only chain. */
490 static struct frame_info
*
491 skip_artificial_frames (struct frame_info
*frame
)
493 /* Note we use get_prev_frame_always, and not get_prev_frame. The
494 latter will truncate the frame chain, leading to this function
495 unintentionally returning a null_frame_id (e.g., when the user
496 sets a backtrace limit).
498 Note that for record targets we may get a frame chain that consists
499 of artificial frames only. */
500 while (get_frame_type (frame
) == INLINE_FRAME
501 || get_frame_type (frame
) == TAILCALL_FRAME
)
503 frame
= get_prev_frame_always (frame
);
512 skip_unwritable_frames (struct frame_info
*frame
)
514 while (gdbarch_code_of_frame_writable (get_frame_arch (frame
), frame
) == 0)
516 frame
= get_prev_frame (frame
);
527 skip_tailcall_frames (struct frame_info
*frame
)
529 while (get_frame_type (frame
) == TAILCALL_FRAME
)
531 /* Note that for record targets we may get a frame chain that consists of
532 tailcall frames only. */
533 frame
= get_prev_frame (frame
);
541 /* Compute the frame's uniq ID that can be used to, later, re-find the
545 compute_frame_id (struct frame_info
*fi
)
547 gdb_assert (!fi
->this_id
.p
);
550 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
552 /* Find the unwinder. */
553 if (fi
->unwind
== NULL
)
554 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
555 /* Find THIS frame's ID. */
556 /* Default to outermost if no ID is found. */
557 fi
->this_id
.value
= outer_frame_id
;
558 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
559 gdb_assert (frame_id_p (fi
->this_id
.value
));
560 fi
->this_id
.p
= true;
563 fprintf_unfiltered (gdb_stdlog
, "-> ");
564 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
565 fprintf_unfiltered (gdb_stdlog
, " }\n");
569 /* Return a frame uniq ID that can be used to, later, re-find the
573 get_frame_id (struct frame_info
*fi
)
576 return null_frame_id
;
580 /* If we haven't computed the frame id yet, then it must be that
581 this is the current frame. Compute it now, and stash the
582 result. The IDs of other frames are computed as soon as
583 they're created, in order to detect cycles. See
584 get_prev_frame_if_no_cycle. */
585 gdb_assert (fi
->level
== 0);
588 compute_frame_id (fi
);
590 /* Since this is the first frame in the chain, this should
592 bool stashed
= frame_stash_add (fi
);
593 gdb_assert (stashed
);
596 return fi
->this_id
.value
;
600 get_stack_frame_id (struct frame_info
*next_frame
)
602 return get_frame_id (skip_artificial_frames (next_frame
));
606 frame_unwind_caller_id (struct frame_info
*next_frame
)
608 struct frame_info
*this_frame
;
610 /* Use get_prev_frame_always, and not get_prev_frame. The latter
611 will truncate the frame chain, leading to this function
612 unintentionally returning a null_frame_id (e.g., when a caller
613 requests the frame ID of "main()"s caller. */
615 next_frame
= skip_artificial_frames (next_frame
);
616 if (next_frame
== NULL
)
617 return null_frame_id
;
619 this_frame
= get_prev_frame_always (next_frame
);
621 return get_frame_id (skip_artificial_frames (this_frame
));
623 return null_frame_id
;
626 const struct frame_id null_frame_id
= { 0 }; /* All zeros. */
627 const struct frame_id sentinel_frame_id
= { 0, 0, 0, FID_STACK_SENTINEL
, 0, 1, 0 };
628 const struct frame_id outer_frame_id
= { 0, 0, 0, FID_STACK_INVALID
, 0, 1, 0 };
631 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
632 CORE_ADDR special_addr
)
634 struct frame_id id
= null_frame_id
;
636 id
.stack_addr
= stack_addr
;
637 id
.stack_status
= FID_STACK_VALID
;
638 id
.code_addr
= code_addr
;
639 id
.code_addr_p
= true;
640 id
.special_addr
= special_addr
;
641 id
.special_addr_p
= true;
648 frame_id_build_unavailable_stack (CORE_ADDR code_addr
)
650 struct frame_id id
= null_frame_id
;
652 id
.stack_status
= FID_STACK_UNAVAILABLE
;
653 id
.code_addr
= code_addr
;
654 id
.code_addr_p
= true;
661 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr
,
662 CORE_ADDR special_addr
)
664 struct frame_id id
= null_frame_id
;
666 id
.stack_status
= FID_STACK_UNAVAILABLE
;
667 id
.code_addr
= code_addr
;
668 id
.code_addr_p
= true;
669 id
.special_addr
= special_addr
;
670 id
.special_addr_p
= true;
675 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
677 struct frame_id id
= null_frame_id
;
679 id
.stack_addr
= stack_addr
;
680 id
.stack_status
= FID_STACK_VALID
;
681 id
.code_addr
= code_addr
;
682 id
.code_addr_p
= true;
687 frame_id_build_wild (CORE_ADDR stack_addr
)
689 struct frame_id id
= null_frame_id
;
691 id
.stack_addr
= stack_addr
;
692 id
.stack_status
= FID_STACK_VALID
;
697 frame_id_p (frame_id l
)
699 /* The frame is valid iff it has a valid stack address. */
700 bool p
= l
.stack_status
!= FID_STACK_INVALID
;
702 /* outer_frame_id is also valid. */
703 if (!p
&& memcmp (&l
, &outer_frame_id
, sizeof (l
)) == 0)
708 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
709 fprint_frame_id (gdb_stdlog
, l
);
710 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
717 frame_id_artificial_p (frame_id l
)
722 return l
.artificial_depth
!= 0;
726 frame_id_eq (frame_id l
, frame_id r
)
730 if (l
.stack_status
== FID_STACK_INVALID
&& l
.special_addr_p
731 && r
.stack_status
== FID_STACK_INVALID
&& r
.special_addr_p
)
732 /* The outermost frame marker is equal to itself. This is the
733 dodgy thing about outer_frame_id, since between execution steps
734 we might step into another function - from which we can't
735 unwind either. More thought required to get rid of
738 else if (l
.stack_status
== FID_STACK_INVALID
739 || r
.stack_status
== FID_STACK_INVALID
)
740 /* Like a NaN, if either ID is invalid, the result is false.
741 Note that a frame ID is invalid iff it is the null frame ID. */
743 else if (l
.stack_status
!= r
.stack_status
|| l
.stack_addr
!= r
.stack_addr
)
744 /* If .stack addresses are different, the frames are different. */
746 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
747 /* An invalid code addr is a wild card. If .code addresses are
748 different, the frames are different. */
750 else if (l
.special_addr_p
&& r
.special_addr_p
751 && l
.special_addr
!= r
.special_addr
)
752 /* An invalid special addr is a wild card (or unused). Otherwise
753 if special addresses are different, the frames are different. */
755 else if (l
.artificial_depth
!= r
.artificial_depth
)
756 /* If artificial depths are different, the frames must be different. */
759 /* Frames are equal. */
764 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
765 fprint_frame_id (gdb_stdlog
, l
);
766 fprintf_unfiltered (gdb_stdlog
, ",r=");
767 fprint_frame_id (gdb_stdlog
, r
);
768 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
774 /* Safety net to check whether frame ID L should be inner to
775 frame ID R, according to their stack addresses.
777 This method cannot be used to compare arbitrary frames, as the
778 ranges of valid stack addresses may be discontiguous (e.g. due
781 However, it can be used as safety net to discover invalid frame
782 IDs in certain circumstances. Assuming that NEXT is the immediate
783 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
785 * The stack address of NEXT must be inner-than-or-equal to the stack
788 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
791 * If NEXT and THIS have different stack addresses, no other frame
792 in the frame chain may have a stack address in between.
794 Therefore, if frame_id_inner (TEST, THIS) holds, but
795 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
796 to a valid frame in the frame chain.
798 The sanity checks above cannot be performed when a SIGTRAMP frame
799 is involved, because signal handlers might be executed on a different
800 stack than the stack used by the routine that caused the signal
801 to be raised. This can happen for instance when a thread exceeds
802 its maximum stack size. In this case, certain compilers implement
803 a stack overflow strategy that cause the handler to be run on a
807 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
811 if (l
.stack_status
!= FID_STACK_VALID
|| r
.stack_status
!= FID_STACK_VALID
)
812 /* Like NaN, any operation involving an invalid ID always fails.
813 Likewise if either ID has an unavailable stack address. */
815 else if (l
.artificial_depth
> r
.artificial_depth
816 && l
.stack_addr
== r
.stack_addr
817 && l
.code_addr_p
== r
.code_addr_p
818 && l
.special_addr_p
== r
.special_addr_p
819 && l
.special_addr
== r
.special_addr
)
821 /* Same function, different inlined functions. */
822 const struct block
*lb
, *rb
;
824 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
826 lb
= block_for_pc (l
.code_addr
);
827 rb
= block_for_pc (r
.code_addr
);
829 if (lb
== NULL
|| rb
== NULL
)
830 /* Something's gone wrong. */
833 /* This will return true if LB and RB are the same block, or
834 if the block with the smaller depth lexically encloses the
835 block with the greater depth. */
836 inner
= contained_in (lb
, rb
);
839 /* Only return non-zero when strictly inner than. Note that, per
840 comment in "frame.h", there is some fuzz here. Frameless
841 functions are not strictly inner than (same .stack but
842 different .code and/or .special address). */
843 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
847 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
848 fprint_frame_id (gdb_stdlog
, l
);
849 fprintf_unfiltered (gdb_stdlog
, ",r=");
850 fprint_frame_id (gdb_stdlog
, r
);
851 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
858 frame_find_by_id (struct frame_id id
)
860 struct frame_info
*frame
, *prev_frame
;
862 /* ZERO denotes the null frame, let the caller decide what to do
863 about it. Should it instead return get_current_frame()? */
864 if (!frame_id_p (id
))
867 /* Check for the sentinel frame. */
868 if (frame_id_eq (id
, sentinel_frame_id
))
869 return sentinel_frame
;
871 /* Try using the frame stash first. Finding it there removes the need
872 to perform the search by looping over all frames, which can be very
873 CPU-intensive if the number of frames is very high (the loop is O(n)
874 and get_prev_frame performs a series of checks that are relatively
875 expensive). This optimization is particularly useful when this function
876 is called from another function (such as value_fetch_lazy, case
877 VALUE_LVAL (val) == lval_register) which already loops over all frames,
878 making the overall behavior O(n^2). */
879 frame
= frame_stash_find (id
);
883 for (frame
= get_current_frame (); ; frame
= prev_frame
)
885 struct frame_id self
= get_frame_id (frame
);
887 if (frame_id_eq (id
, self
))
888 /* An exact match. */
891 prev_frame
= get_prev_frame (frame
);
895 /* As a safety net to avoid unnecessary backtracing while trying
896 to find an invalid ID, we check for a common situation where
897 we can detect from comparing stack addresses that no other
898 frame in the current frame chain can have this ID. See the
899 comment at frame_id_inner for details. */
900 if (get_frame_type (frame
) == NORMAL_FRAME
901 && !frame_id_inner (get_frame_arch (frame
), id
, self
)
902 && frame_id_inner (get_frame_arch (prev_frame
), id
,
903 get_frame_id (prev_frame
)))
910 frame_unwind_pc (struct frame_info
*this_frame
)
912 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
914 struct gdbarch
*prev_gdbarch
;
918 /* The right way. The `pure' way. The one true way. This
919 method depends solely on the register-unwind code to
920 determine the value of registers in THIS frame, and hence
921 the value of this frame's PC (resume address). A typical
922 implementation is no more than:
924 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
925 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
927 Note: this method is very heavily dependent on a correct
928 register-unwind implementation, it pays to fix that
929 method first; this method is frame type agnostic, since
930 it only deals with register values, it works with any
931 frame. This is all in stark contrast to the old
932 FRAME_SAVED_PC which would try to directly handle all the
933 different ways that a PC could be unwound. */
934 prev_gdbarch
= frame_unwind_arch (this_frame
);
938 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
941 catch (const gdb_exception_error
&ex
)
943 if (ex
.error
== NOT_AVAILABLE_ERROR
)
945 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
948 fprintf_unfiltered (gdb_stdlog
,
949 "{ frame_unwind_pc (this_frame=%d)"
950 " -> <unavailable> }\n",
953 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
955 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
958 fprintf_unfiltered (gdb_stdlog
,
959 "{ frame_unwind_pc (this_frame=%d)"
960 " -> <not saved> }\n",
969 this_frame
->prev_pc
.value
= pc
;
970 this_frame
->prev_pc
.status
= CC_VALUE
;
972 fprintf_unfiltered (gdb_stdlog
,
973 "{ frame_unwind_pc (this_frame=%d) "
976 hex_string (this_frame
->prev_pc
.value
));
980 if (this_frame
->prev_pc
.status
== CC_VALUE
)
981 return this_frame
->prev_pc
.value
;
982 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
983 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
984 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
985 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
987 internal_error (__FILE__
, __LINE__
,
988 "unexpected prev_pc status: %d",
989 (int) this_frame
->prev_pc
.status
);
993 frame_unwind_caller_pc (struct frame_info
*this_frame
)
995 this_frame
= skip_artificial_frames (this_frame
);
997 /* We must have a non-artificial frame. The caller is supposed to check
998 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1000 gdb_assert (this_frame
!= NULL
);
1002 return frame_unwind_pc (this_frame
);
1006 get_frame_func_if_available (frame_info
*this_frame
, CORE_ADDR
*pc
)
1008 struct frame_info
*next_frame
= this_frame
->next
;
1010 if (next_frame
->prev_func
.status
== CC_UNKNOWN
)
1012 CORE_ADDR addr_in_block
;
1014 /* Make certain that this, and not the adjacent, function is
1016 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
1018 next_frame
->prev_func
.status
= CC_UNAVAILABLE
;
1020 fprintf_unfiltered (gdb_stdlog
,
1021 "{ get_frame_func (this_frame=%d)"
1022 " -> unavailable }\n",
1027 next_frame
->prev_func
.status
= CC_VALUE
;
1028 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
1030 fprintf_unfiltered (gdb_stdlog
,
1031 "{ get_frame_func (this_frame=%d) -> %s }\n",
1033 hex_string (next_frame
->prev_func
.addr
));
1037 if (next_frame
->prev_func
.status
== CC_UNAVAILABLE
)
1044 gdb_assert (next_frame
->prev_func
.status
== CC_VALUE
);
1046 *pc
= next_frame
->prev_func
.addr
;
1052 get_frame_func (struct frame_info
*this_frame
)
1056 if (!get_frame_func_if_available (this_frame
, &pc
))
1057 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1062 std::unique_ptr
<readonly_detached_regcache
>
1063 frame_save_as_regcache (struct frame_info
*this_frame
)
1065 auto cooked_read
= [this_frame
] (int regnum
, gdb_byte
*buf
)
1067 if (!deprecated_frame_register_read (this_frame
, regnum
, buf
))
1068 return REG_UNAVAILABLE
;
1073 std::unique_ptr
<readonly_detached_regcache
> regcache
1074 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
1080 frame_pop (struct frame_info
*this_frame
)
1082 struct frame_info
*prev_frame
;
1084 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
1086 /* Popping a dummy frame involves restoring more than just registers.
1087 dummy_frame_pop does all the work. */
1088 dummy_frame_pop (get_frame_id (this_frame
), inferior_thread ());
1092 /* Ensure that we have a frame to pop to. */
1093 prev_frame
= get_prev_frame_always (this_frame
);
1096 error (_("Cannot pop the initial frame."));
1098 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1099 entering THISFRAME. */
1100 prev_frame
= skip_tailcall_frames (prev_frame
);
1102 if (prev_frame
== NULL
)
1103 error (_("Cannot find the caller frame."));
1105 /* Make a copy of all the register values unwound from this frame.
1106 Save them in a scratch buffer so that there isn't a race between
1107 trying to extract the old values from the current regcache while
1108 at the same time writing new values into that same cache. */
1109 std::unique_ptr
<readonly_detached_regcache
> scratch
1110 = frame_save_as_regcache (prev_frame
);
1112 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1113 target's register cache that it is about to be hit with a burst
1114 register transfer and that the sequence of register writes should
1115 be batched. The pair target_prepare_to_store() and
1116 target_store_registers() kind of suggest this functionality.
1117 Unfortunately, they don't implement it. Their lack of a formal
1118 definition can lead to targets writing back bogus values
1119 (arguably a bug in the target code mind). */
1120 /* Now copy those saved registers into the current regcache. */
1121 get_current_regcache ()->restore (scratch
.get ());
1123 /* We've made right mess of GDB's local state, just discard
1125 reinit_frame_cache ();
1129 frame_register_unwind (frame_info
*next_frame
, int regnum
,
1130 int *optimizedp
, int *unavailablep
,
1131 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
1132 int *realnump
, gdb_byte
*bufferp
)
1134 struct value
*value
;
1136 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1137 that the value proper does not need to be fetched. */
1138 gdb_assert (optimizedp
!= NULL
);
1139 gdb_assert (lvalp
!= NULL
);
1140 gdb_assert (addrp
!= NULL
);
1141 gdb_assert (realnump
!= NULL
);
1142 /* gdb_assert (bufferp != NULL); */
1144 value
= frame_unwind_register_value (next_frame
, regnum
);
1146 gdb_assert (value
!= NULL
);
1148 *optimizedp
= value_optimized_out (value
);
1149 *unavailablep
= !value_entirely_available (value
);
1150 *lvalp
= VALUE_LVAL (value
);
1151 *addrp
= value_address (value
);
1152 if (*lvalp
== lval_register
)
1153 *realnump
= VALUE_REGNUM (value
);
1159 if (!*optimizedp
&& !*unavailablep
)
1160 memcpy (bufferp
, value_contents_all (value
),
1161 TYPE_LENGTH (value_type (value
)));
1163 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
1166 /* Dispose of the new value. This prevents watchpoints from
1167 trying to watch the saved frame pointer. */
1168 release_value (value
);
1172 frame_register (struct frame_info
*frame
, int regnum
,
1173 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
1174 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
1176 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1177 that the value proper does not need to be fetched. */
1178 gdb_assert (optimizedp
!= NULL
);
1179 gdb_assert (lvalp
!= NULL
);
1180 gdb_assert (addrp
!= NULL
);
1181 gdb_assert (realnump
!= NULL
);
1182 /* gdb_assert (bufferp != NULL); */
1184 /* Obtain the register value by unwinding the register from the next
1185 (more inner frame). */
1186 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
1187 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
1188 lvalp
, addrp
, realnump
, bufferp
);
1192 frame_unwind_register (frame_info
*next_frame
, int regnum
, gdb_byte
*buf
)
1198 enum lval_type lval
;
1200 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1201 &lval
, &addr
, &realnum
, buf
);
1204 throw_error (OPTIMIZED_OUT_ERROR
,
1205 _("Register %d was not saved"), regnum
);
1207 throw_error (NOT_AVAILABLE_ERROR
,
1208 _("Register %d is not available"), regnum
);
1212 get_frame_register (struct frame_info
*frame
,
1213 int regnum
, gdb_byte
*buf
)
1215 frame_unwind_register (frame
->next
, regnum
, buf
);
1219 frame_unwind_register_value (frame_info
*next_frame
, int regnum
)
1221 struct gdbarch
*gdbarch
;
1222 struct value
*value
;
1224 gdb_assert (next_frame
!= NULL
);
1225 gdbarch
= frame_unwind_arch (next_frame
);
1229 fprintf_unfiltered (gdb_stdlog
,
1230 "{ frame_unwind_register_value "
1231 "(frame=%d,regnum=%d(%s),...) ",
1232 next_frame
->level
, regnum
,
1233 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1236 /* Find the unwinder. */
1237 if (next_frame
->unwind
== NULL
)
1238 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
1240 /* Ask this frame to unwind its register. */
1241 value
= next_frame
->unwind
->prev_register (next_frame
,
1242 &next_frame
->prologue_cache
,
1247 fprintf_unfiltered (gdb_stdlog
, "->");
1248 if (value_optimized_out (value
))
1250 fprintf_unfiltered (gdb_stdlog
, " ");
1251 val_print_optimized_out (value
, gdb_stdlog
);
1255 if (VALUE_LVAL (value
) == lval_register
)
1256 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1257 VALUE_REGNUM (value
));
1258 else if (VALUE_LVAL (value
) == lval_memory
)
1259 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1261 value_address (value
)));
1263 fprintf_unfiltered (gdb_stdlog
, " computed");
1265 if (value_lazy (value
))
1266 fprintf_unfiltered (gdb_stdlog
, " lazy");
1270 const gdb_byte
*buf
= value_contents (value
);
1272 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1273 fprintf_unfiltered (gdb_stdlog
, "[");
1274 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1275 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1276 fprintf_unfiltered (gdb_stdlog
, "]");
1280 fprintf_unfiltered (gdb_stdlog
, " }\n");
1287 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1289 return frame_unwind_register_value (frame
->next
, regnum
);
1293 frame_unwind_register_signed (frame_info
*next_frame
, int regnum
)
1295 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1296 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1297 int size
= register_size (gdbarch
, regnum
);
1298 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1300 gdb_assert (value
!= NULL
);
1302 if (value_optimized_out (value
))
1304 throw_error (OPTIMIZED_OUT_ERROR
,
1305 _("Register %d was not saved"), regnum
);
1307 if (!value_entirely_available (value
))
1309 throw_error (NOT_AVAILABLE_ERROR
,
1310 _("Register %d is not available"), regnum
);
1313 LONGEST r
= extract_signed_integer (value_contents_all (value
), size
,
1316 release_value (value
);
1321 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1323 return frame_unwind_register_signed (frame
->next
, regnum
);
1327 frame_unwind_register_unsigned (frame_info
*next_frame
, int regnum
)
1329 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1330 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1331 int size
= register_size (gdbarch
, regnum
);
1332 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1334 gdb_assert (value
!= NULL
);
1336 if (value_optimized_out (value
))
1338 throw_error (OPTIMIZED_OUT_ERROR
,
1339 _("Register %d was not saved"), regnum
);
1341 if (!value_entirely_available (value
))
1343 throw_error (NOT_AVAILABLE_ERROR
,
1344 _("Register %d is not available"), regnum
);
1347 ULONGEST r
= extract_unsigned_integer (value_contents_all (value
), size
,
1350 release_value (value
);
1355 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1357 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1361 read_frame_register_unsigned (frame_info
*frame
, int regnum
,
1364 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1366 if (!value_optimized_out (regval
)
1367 && value_entirely_available (regval
))
1369 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1370 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1371 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1373 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1381 put_frame_register (struct frame_info
*frame
, int regnum
,
1382 const gdb_byte
*buf
)
1384 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1388 enum lval_type lval
;
1391 frame_register (frame
, regnum
, &optim
, &unavail
,
1392 &lval
, &addr
, &realnum
, NULL
);
1394 error (_("Attempt to assign to a register that was not saved."));
1399 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1403 get_current_regcache ()->cooked_write (realnum
, buf
);
1406 error (_("Attempt to assign to an unmodifiable value."));
1410 /* This function is deprecated. Use get_frame_register_value instead,
1411 which provides more accurate information.
1413 Find and return the value of REGNUM for the specified stack frame.
1414 The number of bytes copied is REGISTER_SIZE (REGNUM).
1416 Returns 0 if the register value could not be found. */
1419 deprecated_frame_register_read (frame_info
*frame
, int regnum
,
1424 enum lval_type lval
;
1428 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1429 &lval
, &addr
, &realnum
, myaddr
);
1431 return !optimized
&& !unavailable
;
1435 get_frame_register_bytes (frame_info
*frame
, int regnum
,
1436 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1437 int *optimizedp
, int *unavailablep
)
1439 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1444 /* Skip registers wholly inside of OFFSET. */
1445 while (offset
>= register_size (gdbarch
, regnum
))
1447 offset
-= register_size (gdbarch
, regnum
);
1451 /* Ensure that we will not read beyond the end of the register file.
1452 This can only ever happen if the debug information is bad. */
1454 numregs
= gdbarch_num_cooked_regs (gdbarch
);
1455 for (i
= regnum
; i
< numregs
; i
++)
1457 int thissize
= register_size (gdbarch
, i
);
1460 break; /* This register is not available on this architecture. */
1461 maxsize
+= thissize
;
1464 error (_("Bad debug information detected: "
1465 "Attempt to read %d bytes from registers."), len
);
1467 /* Copy the data. */
1470 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1475 if (curr_len
== register_size (gdbarch
, regnum
))
1477 enum lval_type lval
;
1481 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1482 &lval
, &addr
, &realnum
, myaddr
);
1483 if (*optimizedp
|| *unavailablep
)
1488 struct value
*value
= frame_unwind_register_value (frame
->next
,
1490 gdb_assert (value
!= NULL
);
1491 *optimizedp
= value_optimized_out (value
);
1492 *unavailablep
= !value_entirely_available (value
);
1494 if (*optimizedp
|| *unavailablep
)
1496 release_value (value
);
1500 memcpy (myaddr
, value_contents_all (value
) + offset
, curr_len
);
1501 release_value (value
);
1517 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1518 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1520 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1522 /* Skip registers wholly inside of OFFSET. */
1523 while (offset
>= register_size (gdbarch
, regnum
))
1525 offset
-= register_size (gdbarch
, regnum
);
1529 /* Copy the data. */
1532 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1537 if (curr_len
== register_size (gdbarch
, regnum
))
1539 put_frame_register (frame
, regnum
, myaddr
);
1543 struct value
*value
= frame_unwind_register_value (frame
->next
,
1545 gdb_assert (value
!= NULL
);
1547 memcpy ((char *) value_contents_writeable (value
) + offset
, myaddr
,
1549 put_frame_register (frame
, regnum
, value_contents_raw (value
));
1550 release_value (value
);
1560 /* Create a sentinel frame. */
1562 static struct frame_info
*
1563 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1565 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1568 frame
->pspace
= pspace
;
1569 frame
->aspace
= regcache
->aspace ();
1570 /* Explicitly initialize the sentinel frame's cache. Provide it
1571 with the underlying regcache. In the future additional
1572 information, such as the frame's thread will be added. */
1573 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1574 /* For the moment there is only one sentinel frame implementation. */
1575 frame
->unwind
= &sentinel_frame_unwind
;
1576 /* Link this frame back to itself. The frame is self referential
1577 (the unwound PC is the same as the pc), so make it so. */
1578 frame
->next
= frame
;
1579 /* The sentinel frame has a special ID. */
1580 frame
->this_id
.p
= true;
1581 frame
->this_id
.value
= sentinel_frame_id
;
1584 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1585 fprint_frame (gdb_stdlog
, frame
);
1586 fprintf_unfiltered (gdb_stdlog
, " }\n");
1591 /* Cache for frame addresses already read by gdb. Valid only while
1592 inferior is stopped. Control variables for the frame cache should
1593 be local to this module. */
1595 static struct obstack frame_cache_obstack
;
1598 frame_obstack_zalloc (unsigned long size
)
1600 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1602 memset (data
, 0, size
);
1606 static struct frame_info
*get_prev_frame_always_1 (struct frame_info
*this_frame
);
1609 get_current_frame (void)
1611 struct frame_info
*current_frame
;
1613 /* First check, and report, the lack of registers. Having GDB
1614 report "No stack!" or "No memory" when the target doesn't even
1615 have registers is very confusing. Besides, "printcmd.exp"
1616 explicitly checks that ``print $pc'' with no registers prints "No
1618 if (!target_has_registers
)
1619 error (_("No registers."));
1620 if (!target_has_stack
)
1621 error (_("No stack."));
1622 if (!target_has_memory
)
1623 error (_("No memory."));
1624 /* Traceframes are effectively a substitute for the live inferior. */
1625 if (get_traceframe_number () < 0)
1626 validate_registers_access ();
1628 if (sentinel_frame
== NULL
)
1630 create_sentinel_frame (current_program_space
, get_current_regcache ());
1632 /* Set the current frame before computing the frame id, to avoid
1633 recursion inside compute_frame_id, in case the frame's
1634 unwinder decides to do a symbol lookup (which depends on the
1635 selected frame's block).
1637 This call must always succeed. In particular, nothing inside
1638 get_prev_frame_always_1 should try to unwind from the
1639 sentinel frame, because that could fail/throw, and we always
1640 want to leave with the current frame created and linked in --
1641 we should never end up with the sentinel frame as outermost
1643 current_frame
= get_prev_frame_always_1 (sentinel_frame
);
1644 gdb_assert (current_frame
!= NULL
);
1646 return current_frame
;
1649 /* The "selected" stack frame is used by default for local and arg
1650 access. May be zero, for no selected frame. */
1652 static struct frame_info
*selected_frame
;
1657 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1660 /* Traceframes are effectively a substitute for the live inferior. */
1661 if (get_traceframe_number () < 0)
1663 /* No current inferior, no frame. */
1664 if (inferior_ptid
== null_ptid
)
1667 thread_info
*tp
= inferior_thread ();
1668 /* Don't try to read from a dead thread. */
1669 if (tp
->state
== THREAD_EXITED
)
1672 /* ... or from a spinning thread. */
1680 /* Return the selected frame. Always non-NULL (unless there isn't an
1681 inferior sufficient for creating a frame) in which case an error is
1685 get_selected_frame (const char *message
)
1687 if (selected_frame
== NULL
)
1689 if (message
!= NULL
&& !has_stack_frames ())
1690 error (("%s"), message
);
1691 /* Hey! Don't trust this. It should really be re-finding the
1692 last selected frame of the currently selected thread. This,
1693 though, is better than nothing. */
1694 select_frame (get_current_frame ());
1696 /* There is always a frame. */
1697 gdb_assert (selected_frame
!= NULL
);
1698 return selected_frame
;
1701 /* If there is a selected frame, return it. Otherwise, return NULL. */
1704 get_selected_frame_if_set (void)
1706 return selected_frame
;
1709 /* This is a variant of get_selected_frame() which can be called when
1710 the inferior does not have a frame; in that case it will return
1711 NULL instead of calling error(). */
1714 deprecated_safe_get_selected_frame (void)
1716 if (!has_stack_frames ())
1718 return get_selected_frame (NULL
);
1721 /* Select frame FI (or NULL - to invalidate the current frame). */
1724 select_frame (struct frame_info
*fi
)
1726 selected_frame
= fi
;
1727 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1728 frame is being invalidated. */
1730 /* FIXME: kseitz/2002-08-28: It would be nice to call
1731 selected_frame_level_changed_event() right here, but due to limitations
1732 in the current interfaces, we would end up flooding UIs with events
1733 because select_frame() is used extensively internally.
1735 Once we have frame-parameterized frame (and frame-related) commands,
1736 the event notification can be moved here, since this function will only
1737 be called when the user's selected frame is being changed. */
1739 /* Ensure that symbols for this frame are read in. Also, determine the
1740 source language of this frame, and switch to it if desired. */
1745 /* We retrieve the frame's symtab by using the frame PC.
1746 However we cannot use the frame PC as-is, because it usually
1747 points to the instruction following the "call", which is
1748 sometimes the first instruction of another function. So we
1749 rely on get_frame_address_in_block() which provides us with a
1750 PC which is guaranteed to be inside the frame's code
1752 if (get_frame_address_in_block_if_available (fi
, &pc
))
1754 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
1757 && compunit_language (cust
) != current_language
->la_language
1758 && compunit_language (cust
) != language_unknown
1759 && language_mode
== language_mode_auto
)
1760 set_language (compunit_language (cust
));
1765 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1766 Always returns a non-NULL value. */
1769 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1771 struct frame_info
*fi
;
1775 fprintf_unfiltered (gdb_stdlog
,
1776 "{ create_new_frame (addr=%s, pc=%s) ",
1777 hex_string (addr
), hex_string (pc
));
1780 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1782 fi
->next
= create_sentinel_frame (current_program_space
,
1783 get_current_regcache ());
1785 /* Set/update this frame's cached PC value, found in the next frame.
1786 Do this before looking for this frame's unwinder. A sniffer is
1787 very likely to read this, and the corresponding unwinder is
1788 entitled to rely that the PC doesn't magically change. */
1789 fi
->next
->prev_pc
.value
= pc
;
1790 fi
->next
->prev_pc
.status
= CC_VALUE
;
1792 /* We currently assume that frame chain's can't cross spaces. */
1793 fi
->pspace
= fi
->next
->pspace
;
1794 fi
->aspace
= fi
->next
->aspace
;
1796 /* Select/initialize both the unwind function and the frame's type
1798 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1800 fi
->this_id
.p
= true;
1801 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1805 fprintf_unfiltered (gdb_stdlog
, "-> ");
1806 fprint_frame (gdb_stdlog
, fi
);
1807 fprintf_unfiltered (gdb_stdlog
, " }\n");
1813 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1814 innermost frame). Be careful to not fall off the bottom of the
1815 frame chain and onto the sentinel frame. */
1818 get_next_frame (struct frame_info
*this_frame
)
1820 if (this_frame
->level
> 0)
1821 return this_frame
->next
;
1826 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1827 innermost (i.e. current) frame, return the sentinel frame. Thus,
1828 unlike get_next_frame(), NULL will never be returned. */
1831 get_next_frame_sentinel_okay (struct frame_info
*this_frame
)
1833 gdb_assert (this_frame
!= NULL
);
1835 /* Note that, due to the manner in which the sentinel frame is
1836 constructed, this_frame->next still works even when this_frame
1837 is the sentinel frame. But we disallow it here anyway because
1838 calling get_next_frame_sentinel_okay() on the sentinel frame
1839 is likely a coding error. */
1840 gdb_assert (this_frame
!= sentinel_frame
);
1842 return this_frame
->next
;
1845 /* Observer for the target_changed event. */
1848 frame_observer_target_changed (struct target_ops
*target
)
1850 reinit_frame_cache ();
1853 /* Flush the entire frame cache. */
1856 reinit_frame_cache (void)
1858 struct frame_info
*fi
;
1860 ++frame_cache_generation
;
1862 /* Tear down all frame caches. */
1863 for (fi
= sentinel_frame
; fi
!= NULL
; fi
= fi
->prev
)
1865 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1866 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1867 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1868 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1871 /* Since we can't really be sure what the first object allocated was. */
1872 obstack_free (&frame_cache_obstack
, 0);
1873 obstack_init (&frame_cache_obstack
);
1875 if (sentinel_frame
!= NULL
)
1876 annotate_frames_invalid ();
1878 sentinel_frame
= NULL
; /* Invalidate cache */
1879 select_frame (NULL
);
1880 frame_stash_invalidate ();
1882 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1885 /* Find where a register is saved (in memory or another register).
1886 The result of frame_register_unwind is just where it is saved
1887 relative to this particular frame. */
1890 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1891 int *optimizedp
, enum lval_type
*lvalp
,
1892 CORE_ADDR
*addrp
, int *realnump
)
1894 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1896 while (this_frame
!= NULL
)
1900 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1901 lvalp
, addrp
, realnump
, NULL
);
1906 if (*lvalp
!= lval_register
)
1910 this_frame
= get_next_frame (this_frame
);
1914 /* Get the previous raw frame, and check that it is not identical to
1915 same other frame frame already in the chain. If it is, there is
1916 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1917 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1918 validity tests, that compare THIS_FRAME and the next frame, we do
1919 this right after creating the previous frame, to avoid ever ending
1920 up with two frames with the same id in the frame chain. */
1922 static struct frame_info
*
1923 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1925 struct frame_info
*prev_frame
;
1927 prev_frame
= get_prev_frame_raw (this_frame
);
1929 /* Don't compute the frame id of the current frame yet. Unwinding
1930 the sentinel frame can fail (e.g., if the thread is gone and we
1931 can't thus read its registers). If we let the cycle detection
1932 code below try to compute a frame ID, then an error thrown from
1933 within the frame ID computation would result in the sentinel
1934 frame as outermost frame, which is bogus. Instead, we'll compute
1935 the current frame's ID lazily in get_frame_id. Note that there's
1936 no point in doing cycle detection when there's only one frame, so
1937 nothing is lost here. */
1938 if (prev_frame
->level
== 0)
1941 unsigned int entry_generation
= get_frame_cache_generation ();
1945 compute_frame_id (prev_frame
);
1946 if (!frame_stash_add (prev_frame
))
1948 /* Another frame with the same id was already in the stash. We just
1949 detected a cycle. */
1952 fprintf_unfiltered (gdb_stdlog
, "-> ");
1953 fprint_frame (gdb_stdlog
, NULL
);
1954 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1956 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1958 prev_frame
->next
= NULL
;
1959 this_frame
->prev
= NULL
;
1963 catch (const gdb_exception
&ex
)
1965 if (get_frame_cache_generation () == entry_generation
)
1967 prev_frame
->next
= NULL
;
1968 this_frame
->prev
= NULL
;
1977 /* Helper function for get_prev_frame_always, this is called inside a
1978 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
1979 there is no such frame. This may throw an exception. */
1981 static struct frame_info
*
1982 get_prev_frame_always_1 (struct frame_info
*this_frame
)
1984 struct gdbarch
*gdbarch
;
1986 gdb_assert (this_frame
!= NULL
);
1987 gdbarch
= get_frame_arch (this_frame
);
1991 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_always (this_frame=");
1992 if (this_frame
!= NULL
)
1993 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1995 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1996 fprintf_unfiltered (gdb_stdlog
, ") ");
1999 /* Only try to do the unwind once. */
2000 if (this_frame
->prev_p
)
2004 fprintf_unfiltered (gdb_stdlog
, "-> ");
2005 fprint_frame (gdb_stdlog
, this_frame
->prev
);
2006 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
2008 return this_frame
->prev
;
2011 /* If the frame unwinder hasn't been selected yet, we must do so
2012 before setting prev_p; otherwise the check for misbehaved
2013 sniffers will think that this frame's sniffer tried to unwind
2014 further (see frame_cleanup_after_sniffer). */
2015 if (this_frame
->unwind
== NULL
)
2016 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
2018 this_frame
->prev_p
= true;
2019 this_frame
->stop_reason
= UNWIND_NO_REASON
;
2021 /* If we are unwinding from an inline frame, all of the below tests
2022 were already performed when we unwound from the next non-inline
2023 frame. We must skip them, since we can not get THIS_FRAME's ID
2024 until we have unwound all the way down to the previous non-inline
2026 if (get_frame_type (this_frame
) == INLINE_FRAME
)
2027 return get_prev_frame_if_no_cycle (this_frame
);
2029 /* Check that this frame is unwindable. If it isn't, don't try to
2030 unwind to the prev frame. */
2031 this_frame
->stop_reason
2032 = this_frame
->unwind
->stop_reason (this_frame
,
2033 &this_frame
->prologue_cache
);
2035 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
2039 enum unwind_stop_reason reason
= this_frame
->stop_reason
;
2041 fprintf_unfiltered (gdb_stdlog
, "-> ");
2042 fprint_frame (gdb_stdlog
, NULL
);
2043 fprintf_unfiltered (gdb_stdlog
, " // %s }\n",
2044 frame_stop_reason_symbol_string (reason
));
2049 /* Check that this frame's ID isn't inner to (younger, below, next)
2050 the next frame. This happens when a frame unwind goes backwards.
2051 This check is valid only if this frame and the next frame are NORMAL.
2052 See the comment at frame_id_inner for details. */
2053 if (get_frame_type (this_frame
) == NORMAL_FRAME
2054 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
2055 && frame_id_inner (get_frame_arch (this_frame
->next
),
2056 get_frame_id (this_frame
),
2057 get_frame_id (this_frame
->next
)))
2059 CORE_ADDR this_pc_in_block
;
2060 struct minimal_symbol
*morestack_msym
;
2061 const char *morestack_name
= NULL
;
2063 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2064 this_pc_in_block
= get_frame_address_in_block (this_frame
);
2065 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
2067 morestack_name
= morestack_msym
->linkage_name ();
2068 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
2072 fprintf_unfiltered (gdb_stdlog
, "-> ");
2073 fprint_frame (gdb_stdlog
, NULL
);
2074 fprintf_unfiltered (gdb_stdlog
,
2075 " // this frame ID is inner }\n");
2077 this_frame
->stop_reason
= UNWIND_INNER_ID
;
2082 /* Check that this and the next frame do not unwind the PC register
2083 to the same memory location. If they do, then even though they
2084 have different frame IDs, the new frame will be bogus; two
2085 functions can't share a register save slot for the PC. This can
2086 happen when the prologue analyzer finds a stack adjustment, but
2089 This check does assume that the "PC register" is roughly a
2090 traditional PC, even if the gdbarch_unwind_pc method adjusts
2091 it (we do not rely on the value, only on the unwound PC being
2092 dependent on this value). A potential improvement would be
2093 to have the frame prev_pc method and the gdbarch unwind_pc
2094 method set the same lval and location information as
2095 frame_register_unwind. */
2096 if (this_frame
->level
> 0
2097 && gdbarch_pc_regnum (gdbarch
) >= 0
2098 && get_frame_type (this_frame
) == NORMAL_FRAME
2099 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
2100 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
2102 int optimized
, realnum
, nrealnum
;
2103 enum lval_type lval
, nlval
;
2104 CORE_ADDR addr
, naddr
;
2106 frame_register_unwind_location (this_frame
,
2107 gdbarch_pc_regnum (gdbarch
),
2108 &optimized
, &lval
, &addr
, &realnum
);
2109 frame_register_unwind_location (get_next_frame (this_frame
),
2110 gdbarch_pc_regnum (gdbarch
),
2111 &optimized
, &nlval
, &naddr
, &nrealnum
);
2113 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
2114 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
2118 fprintf_unfiltered (gdb_stdlog
, "-> ");
2119 fprint_frame (gdb_stdlog
, NULL
);
2120 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
2123 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2124 this_frame
->prev
= NULL
;
2129 return get_prev_frame_if_no_cycle (this_frame
);
2132 /* Return a "struct frame_info" corresponding to the frame that called
2133 THIS_FRAME. Returns NULL if there is no such frame.
2135 Unlike get_prev_frame, this function always tries to unwind the
2139 get_prev_frame_always (struct frame_info
*this_frame
)
2141 struct frame_info
*prev_frame
= NULL
;
2145 prev_frame
= get_prev_frame_always_1 (this_frame
);
2147 catch (const gdb_exception_error
&ex
)
2149 if (ex
.error
== MEMORY_ERROR
)
2151 this_frame
->stop_reason
= UNWIND_MEMORY_ERROR
;
2152 if (ex
.message
!= NULL
)
2157 /* The error needs to live as long as the frame does.
2158 Allocate using stack local STOP_STRING then assign the
2159 pointer to the frame, this allows the STOP_STRING on the
2160 frame to be of type 'const char *'. */
2161 size
= ex
.message
->size () + 1;
2162 stop_string
= (char *) frame_obstack_zalloc (size
);
2163 memcpy (stop_string
, ex
.what (), size
);
2164 this_frame
->stop_string
= stop_string
;
2175 /* Construct a new "struct frame_info" and link it previous to
2178 static struct frame_info
*
2179 get_prev_frame_raw (struct frame_info
*this_frame
)
2181 struct frame_info
*prev_frame
;
2183 /* Allocate the new frame but do not wire it in to the frame chain.
2184 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2185 frame->next to pull some fancy tricks (of course such code is, by
2186 definition, recursive). Try to prevent it.
2188 There is no reason to worry about memory leaks, should the
2189 remainder of the function fail. The allocated memory will be
2190 quickly reclaimed when the frame cache is flushed, and the `we've
2191 been here before' check above will stop repeated memory
2192 allocation calls. */
2193 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2194 prev_frame
->level
= this_frame
->level
+ 1;
2196 /* For now, assume we don't have frame chains crossing address
2198 prev_frame
->pspace
= this_frame
->pspace
;
2199 prev_frame
->aspace
= this_frame
->aspace
;
2201 /* Don't yet compute ->unwind (and hence ->type). It is computed
2202 on-demand in get_frame_type, frame_register_unwind, and
2205 /* Don't yet compute the frame's ID. It is computed on-demand by
2208 /* The unwound frame ID is validate at the start of this function,
2209 as part of the logic to decide if that frame should be further
2210 unwound, and not here while the prev frame is being created.
2211 Doing this makes it possible for the user to examine a frame that
2212 has an invalid frame ID.
2214 Some very old VAX code noted: [...] For the sake of argument,
2215 suppose that the stack is somewhat trashed (which is one reason
2216 that "info frame" exists). So, return 0 (indicating we don't
2217 know the address of the arglist) if we don't know what frame this
2221 this_frame
->prev
= prev_frame
;
2222 prev_frame
->next
= this_frame
;
2226 fprintf_unfiltered (gdb_stdlog
, "-> ");
2227 fprint_frame (gdb_stdlog
, prev_frame
);
2228 fprintf_unfiltered (gdb_stdlog
, " }\n");
2234 /* Debug routine to print a NULL frame being returned. */
2237 frame_debug_got_null_frame (struct frame_info
*this_frame
,
2242 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
2243 if (this_frame
!= NULL
)
2244 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2246 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2247 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
2251 /* Is this (non-sentinel) frame in the "main"() function? */
2254 inside_main_func (frame_info
*this_frame
)
2256 if (symfile_objfile
== nullptr)
2259 bound_minimal_symbol msymbol
2260 = lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
2261 if (msymbol
.minsym
== nullptr)
2264 /* Make certain that the code, and not descriptor, address is
2267 = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
2268 BMSYMBOL_VALUE_ADDRESS (msymbol
),
2269 current_top_target ());
2271 return maddr
== get_frame_func (this_frame
);
2274 /* Test whether THIS_FRAME is inside the process entry point function. */
2277 inside_entry_func (frame_info
*this_frame
)
2279 CORE_ADDR entry_point
;
2281 if (!entry_point_address_query (&entry_point
))
2284 return get_frame_func (this_frame
) == entry_point
;
2287 /* Return a structure containing various interesting information about
2288 the frame that called THIS_FRAME. Returns NULL if there is entier
2289 no such frame or the frame fails any of a set of target-independent
2290 condition that should terminate the frame chain (e.g., as unwinding
2293 This function should not contain target-dependent tests, such as
2294 checking whether the program-counter is zero. */
2297 get_prev_frame (struct frame_info
*this_frame
)
2302 /* There is always a frame. If this assertion fails, suspect that
2303 something should be calling get_selected_frame() or
2304 get_current_frame(). */
2305 gdb_assert (this_frame
!= NULL
);
2307 /* If this_frame is the current frame, then compute and stash
2308 its frame id prior to fetching and computing the frame id of the
2309 previous frame. Otherwise, the cycle detection code in
2310 get_prev_frame_if_no_cycle() will not work correctly. When
2311 get_frame_id() is called later on, an assertion error will
2312 be triggered in the event of a cycle between the current
2313 frame and its previous frame. */
2314 if (this_frame
->level
== 0)
2315 get_frame_id (this_frame
);
2317 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2319 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2320 sense to stop unwinding at a dummy frame. One place where a dummy
2321 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2322 pcsqh register (space register for the instruction at the head of the
2323 instruction queue) cannot be written directly; the only way to set it
2324 is to branch to code that is in the target space. In order to implement
2325 frame dummies on HPUX, the called function is made to jump back to where
2326 the inferior was when the user function was called. If gdb was inside
2327 the main function when we created the dummy frame, the dummy frame will
2328 point inside the main function. */
2329 if (this_frame
->level
>= 0
2330 && get_frame_type (this_frame
) == NORMAL_FRAME
2331 && !user_set_backtrace_options
.backtrace_past_main
2333 && inside_main_func (this_frame
))
2334 /* Don't unwind past main(). Note, this is done _before_ the
2335 frame has been marked as previously unwound. That way if the
2336 user later decides to enable unwinds past main(), that will
2337 automatically happen. */
2339 frame_debug_got_null_frame (this_frame
, "inside main func");
2343 /* If the user's backtrace limit has been exceeded, stop. We must
2344 add two to the current level; one of those accounts for backtrace_limit
2345 being 1-based and the level being 0-based, and the other accounts for
2346 the level of the new frame instead of the level of the current
2348 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2350 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2354 /* If we're already inside the entry function for the main objfile,
2355 then it isn't valid. Don't apply this test to a dummy frame -
2356 dummy frame PCs typically land in the entry func. Don't apply
2357 this test to the sentinel frame. Sentinel frames should always
2358 be allowed to unwind. */
2359 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2360 wasn't checking for "main" in the minimal symbols. With that
2361 fixed asm-source tests now stop in "main" instead of halting the
2362 backtrace in weird and wonderful ways somewhere inside the entry
2363 file. Suspect that tests for inside the entry file/func were
2364 added to work around that (now fixed) case. */
2365 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2366 suggested having the inside_entry_func test use the
2367 inside_main_func() msymbol trick (along with entry_point_address()
2368 I guess) to determine the address range of the start function.
2369 That should provide a far better stopper than the current
2371 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2372 applied tail-call optimizations to main so that a function called
2373 from main returns directly to the caller of main. Since we don't
2374 stop at main, we should at least stop at the entry point of the
2376 if (this_frame
->level
>= 0
2377 && get_frame_type (this_frame
) == NORMAL_FRAME
2378 && !user_set_backtrace_options
.backtrace_past_entry
2380 && inside_entry_func (this_frame
))
2382 frame_debug_got_null_frame (this_frame
, "inside entry func");
2386 /* Assume that the only way to get a zero PC is through something
2387 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2388 will never unwind a zero PC. */
2389 if (this_frame
->level
> 0
2390 && (get_frame_type (this_frame
) == NORMAL_FRAME
2391 || get_frame_type (this_frame
) == INLINE_FRAME
)
2392 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2393 && frame_pc_p
&& frame_pc
== 0)
2395 frame_debug_got_null_frame (this_frame
, "zero PC");
2399 return get_prev_frame_always (this_frame
);
2403 get_prev_frame_id_by_id (struct frame_id id
)
2405 struct frame_id prev_id
;
2406 struct frame_info
*frame
;
2408 frame
= frame_find_by_id (id
);
2411 prev_id
= get_frame_id (get_prev_frame (frame
));
2413 prev_id
= null_frame_id
;
2419 get_frame_pc (struct frame_info
*frame
)
2421 gdb_assert (frame
->next
!= NULL
);
2422 return frame_unwind_pc (frame
->next
);
2426 get_frame_pc_if_available (frame_info
*frame
, CORE_ADDR
*pc
)
2429 gdb_assert (frame
->next
!= NULL
);
2433 *pc
= frame_unwind_pc (frame
->next
);
2435 catch (const gdb_exception_error
&ex
)
2437 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2446 /* Return an address that falls within THIS_FRAME's code block. */
2449 get_frame_address_in_block (struct frame_info
*this_frame
)
2451 /* A draft address. */
2452 CORE_ADDR pc
= get_frame_pc (this_frame
);
2454 struct frame_info
*next_frame
= this_frame
->next
;
2456 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2457 Normally the resume address is inside the body of the function
2458 associated with THIS_FRAME, but there is a special case: when
2459 calling a function which the compiler knows will never return
2460 (for instance abort), the call may be the very last instruction
2461 in the calling function. The resume address will point after the
2462 call and may be at the beginning of a different function
2465 If THIS_FRAME is a signal frame or dummy frame, then we should
2466 not adjust the unwound PC. For a dummy frame, GDB pushed the
2467 resume address manually onto the stack. For a signal frame, the
2468 OS may have pushed the resume address manually and invoked the
2469 handler (e.g. GNU/Linux), or invoked the trampoline which called
2470 the signal handler - but in either case the signal handler is
2471 expected to return to the trampoline. So in both of these
2472 cases we know that the resume address is executable and
2473 related. So we only need to adjust the PC if THIS_FRAME
2474 is a normal function.
2476 If the program has been interrupted while THIS_FRAME is current,
2477 then clearly the resume address is inside the associated
2478 function. There are three kinds of interruption: debugger stop
2479 (next frame will be SENTINEL_FRAME), operating system
2480 signal or exception (next frame will be SIGTRAMP_FRAME),
2481 or debugger-induced function call (next frame will be
2482 DUMMY_FRAME). So we only need to adjust the PC if
2483 NEXT_FRAME is a normal function.
2485 We check the type of NEXT_FRAME first, since it is already
2486 known; frame type is determined by the unwinder, and since
2487 we have THIS_FRAME we've already selected an unwinder for
2490 If the next frame is inlined, we need to keep going until we find
2491 the real function - for instance, if a signal handler is invoked
2492 while in an inlined function, then the code address of the
2493 "calling" normal function should not be adjusted either. */
2495 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2496 next_frame
= next_frame
->next
;
2498 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2499 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2500 && (get_frame_type (this_frame
) == NORMAL_FRAME
2501 || get_frame_type (this_frame
) == TAILCALL_FRAME
2502 || get_frame_type (this_frame
) == INLINE_FRAME
))
2509 get_frame_address_in_block_if_available (frame_info
*this_frame
,
2515 *pc
= get_frame_address_in_block (this_frame
);
2517 catch (const gdb_exception_error
&ex
)
2519 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2528 find_frame_sal (frame_info
*frame
)
2530 struct frame_info
*next_frame
;
2534 if (frame_inlined_callees (frame
) > 0)
2538 /* If the current frame has some inlined callees, and we have a next
2539 frame, then that frame must be an inlined frame. In this case
2540 this frame's sal is the "call site" of the next frame's inlined
2541 function, which can not be inferred from get_frame_pc. */
2542 next_frame
= get_next_frame (frame
);
2544 sym
= get_frame_function (next_frame
);
2546 sym
= inline_skipped_symbol (inferior_thread ());
2548 /* If frame is inline, it certainly has symbols. */
2551 symtab_and_line sal
;
2552 if (SYMBOL_LINE (sym
) != 0)
2554 sal
.symtab
= symbol_symtab (sym
);
2555 sal
.line
= SYMBOL_LINE (sym
);
2558 /* If the symbol does not have a location, we don't know where
2559 the call site is. Do not pretend to. This is jarring, but
2560 we can't do much better. */
2561 sal
.pc
= get_frame_pc (frame
);
2563 sal
.pspace
= get_frame_program_space (frame
);
2567 /* If FRAME is not the innermost frame, that normally means that
2568 FRAME->pc points at the return instruction (which is *after* the
2569 call instruction), and we want to get the line containing the
2570 call (because the call is where the user thinks the program is).
2571 However, if the next frame is either a SIGTRAMP_FRAME or a
2572 DUMMY_FRAME, then the next frame will contain a saved interrupt
2573 PC and such a PC indicates the current (rather than next)
2574 instruction/line, consequently, for such cases, want to get the
2575 line containing fi->pc. */
2576 if (!get_frame_pc_if_available (frame
, &pc
))
2579 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2580 return find_pc_line (pc
, notcurrent
);
2583 /* Per "frame.h", return the ``address'' of the frame. Code should
2584 really be using get_frame_id(). */
2586 get_frame_base (struct frame_info
*fi
)
2588 return get_frame_id (fi
).stack_addr
;
2591 /* High-level offsets into the frame. Used by the debug info. */
2594 get_frame_base_address (struct frame_info
*fi
)
2596 if (get_frame_type (fi
) != NORMAL_FRAME
)
2598 if (fi
->base
== NULL
)
2599 fi
->base
= frame_base_find_by_frame (fi
);
2600 /* Sneaky: If the low-level unwind and high-level base code share a
2601 common unwinder, let them share the prologue cache. */
2602 if (fi
->base
->unwind
== fi
->unwind
)
2603 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2604 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2608 get_frame_locals_address (struct frame_info
*fi
)
2610 if (get_frame_type (fi
) != NORMAL_FRAME
)
2612 /* If there isn't a frame address method, find it. */
2613 if (fi
->base
== NULL
)
2614 fi
->base
= frame_base_find_by_frame (fi
);
2615 /* Sneaky: If the low-level unwind and high-level base code share a
2616 common unwinder, let them share the prologue cache. */
2617 if (fi
->base
->unwind
== fi
->unwind
)
2618 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2619 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2623 get_frame_args_address (struct frame_info
*fi
)
2625 if (get_frame_type (fi
) != NORMAL_FRAME
)
2627 /* If there isn't a frame address method, find it. */
2628 if (fi
->base
== NULL
)
2629 fi
->base
= frame_base_find_by_frame (fi
);
2630 /* Sneaky: If the low-level unwind and high-level base code share a
2631 common unwinder, let them share the prologue cache. */
2632 if (fi
->base
->unwind
== fi
->unwind
)
2633 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2634 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2637 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2641 frame_unwinder_is (frame_info
*fi
, const frame_unwind
*unwinder
)
2643 if (fi
->unwind
== nullptr)
2644 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2646 return fi
->unwind
== unwinder
;
2649 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2650 or -1 for a NULL frame. */
2653 frame_relative_level (struct frame_info
*fi
)
2662 get_frame_type (struct frame_info
*frame
)
2664 if (frame
->unwind
== NULL
)
2665 /* Initialize the frame's unwinder because that's what
2666 provides the frame's type. */
2667 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2668 return frame
->unwind
->type
;
2671 struct program_space
*
2672 get_frame_program_space (struct frame_info
*frame
)
2674 return frame
->pspace
;
2677 struct program_space
*
2678 frame_unwind_program_space (struct frame_info
*this_frame
)
2680 gdb_assert (this_frame
);
2682 /* This is really a placeholder to keep the API consistent --- we
2683 assume for now that we don't have frame chains crossing
2685 return this_frame
->pspace
;
2688 const address_space
*
2689 get_frame_address_space (struct frame_info
*frame
)
2691 return frame
->aspace
;
2694 /* Memory access methods. */
2697 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2698 gdb_byte
*buf
, int len
)
2700 read_memory (addr
, buf
, len
);
2704 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2707 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2708 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2710 return read_memory_integer (addr
, len
, byte_order
);
2714 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2717 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2718 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2720 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2724 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2725 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2727 /* NOTE: target_read_memory returns zero on success! */
2728 return target_read_memory (addr
, buf
, len
) == 0;
2731 /* Architecture methods. */
2734 get_frame_arch (struct frame_info
*this_frame
)
2736 return frame_unwind_arch (this_frame
->next
);
2740 frame_unwind_arch (struct frame_info
*next_frame
)
2742 if (!next_frame
->prev_arch
.p
)
2744 struct gdbarch
*arch
;
2746 if (next_frame
->unwind
== NULL
)
2747 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2749 if (next_frame
->unwind
->prev_arch
!= NULL
)
2750 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2751 &next_frame
->prologue_cache
);
2753 arch
= get_frame_arch (next_frame
);
2755 next_frame
->prev_arch
.arch
= arch
;
2756 next_frame
->prev_arch
.p
= true;
2758 fprintf_unfiltered (gdb_stdlog
,
2759 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2761 gdbarch_bfd_arch_info (arch
)->printable_name
);
2764 return next_frame
->prev_arch
.arch
;
2768 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2770 next_frame
= skip_artificial_frames (next_frame
);
2772 /* We must have a non-artificial frame. The caller is supposed to check
2773 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2775 gdb_assert (next_frame
!= NULL
);
2777 return frame_unwind_arch (next_frame
);
2780 /* Gets the language of FRAME. */
2783 get_frame_language (struct frame_info
*frame
)
2788 gdb_assert (frame
!= NULL
);
2790 /* We determine the current frame language by looking up its
2791 associated symtab. To retrieve this symtab, we use the frame
2792 PC. However we cannot use the frame PC as is, because it
2793 usually points to the instruction following the "call", which
2794 is sometimes the first instruction of another function. So
2795 we rely on get_frame_address_in_block(), it provides us with
2796 a PC that is guaranteed to be inside the frame's code
2801 pc
= get_frame_address_in_block (frame
);
2804 catch (const gdb_exception_error
&ex
)
2806 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
2812 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
2815 return compunit_language (cust
);
2818 return language_unknown
;
2821 /* Stack pointer methods. */
2824 get_frame_sp (struct frame_info
*this_frame
)
2826 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2828 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2829 operate on THIS_FRAME now. */
2830 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2833 /* Return the reason why we can't unwind past FRAME. */
2835 enum unwind_stop_reason
2836 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2838 /* Fill-in STOP_REASON. */
2839 get_prev_frame_always (frame
);
2840 gdb_assert (frame
->prev_p
);
2842 return frame
->stop_reason
;
2845 /* Return a string explaining REASON. */
2848 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
2852 #define SET(name, description) \
2853 case name: return _(description);
2854 #include "unwind_stop_reasons.def"
2858 internal_error (__FILE__
, __LINE__
,
2859 "Invalid frame stop reason");
2864 frame_stop_reason_string (struct frame_info
*fi
)
2866 gdb_assert (fi
->prev_p
);
2867 gdb_assert (fi
->prev
== NULL
);
2869 /* Return the specific string if we have one. */
2870 if (fi
->stop_string
!= NULL
)
2871 return fi
->stop_string
;
2873 /* Return the generic string if we have nothing better. */
2874 return unwind_stop_reason_to_string (fi
->stop_reason
);
2877 /* Return the enum symbol name of REASON as a string, to use in debug
2881 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
2885 #define SET(name, description) \
2886 case name: return #name;
2887 #include "unwind_stop_reasons.def"
2891 internal_error (__FILE__
, __LINE__
,
2892 "Invalid frame stop reason");
2896 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2900 frame_cleanup_after_sniffer (struct frame_info
*frame
)
2902 /* The sniffer should not allocate a prologue cache if it did not
2903 match this frame. */
2904 gdb_assert (frame
->prologue_cache
== NULL
);
2906 /* No sniffer should extend the frame chain; sniff based on what is
2908 gdb_assert (!frame
->prev_p
);
2910 /* The sniffer should not check the frame's ID; that's circular. */
2911 gdb_assert (!frame
->this_id
.p
);
2913 /* Clear cached fields dependent on the unwinder.
2915 The previous PC is independent of the unwinder, but the previous
2916 function is not (see get_frame_address_in_block). */
2917 frame
->prev_func
.status
= CC_UNKNOWN
;
2918 frame
->prev_func
.addr
= 0;
2920 /* Discard the unwinder last, so that we can easily find it if an assertion
2921 in this function triggers. */
2922 frame
->unwind
= NULL
;
2925 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2926 If sniffing fails, the caller should be sure to call
2927 frame_cleanup_after_sniffer. */
2930 frame_prepare_for_sniffer (struct frame_info
*frame
,
2931 const struct frame_unwind
*unwind
)
2933 gdb_assert (frame
->unwind
== NULL
);
2934 frame
->unwind
= unwind
;
2937 static struct cmd_list_element
*set_backtrace_cmdlist
;
2938 static struct cmd_list_element
*show_backtrace_cmdlist
;
2940 /* Definition of the "set backtrace" settings that are exposed as
2941 "backtrace" command options. */
2943 using boolean_option_def
2944 = gdb::option::boolean_option_def
<set_backtrace_options
>;
2945 using uinteger_option_def
2946 = gdb::option::uinteger_option_def
<set_backtrace_options
>;
2948 const gdb::option::option_def set_backtrace_option_defs
[] = {
2950 boolean_option_def
{
2952 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_main
; },
2953 show_backtrace_past_main
, /* show_cmd_cb */
2954 N_("Set whether backtraces should continue past \"main\"."),
2955 N_("Show whether backtraces should continue past \"main\"."),
2956 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2957 the backtrace at \"main\". Set this if you need to see the rest\n\
2958 of the stack trace."),
2961 boolean_option_def
{
2963 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_entry
; },
2964 show_backtrace_past_entry
, /* show_cmd_cb */
2965 N_("Set whether backtraces should continue past the entry point of a program."),
2966 N_("Show whether backtraces should continue past the entry point of a program."),
2967 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
2968 will terminate the backtrace there. Set this if you need to see\n\
2969 the rest of the stack trace."),
2973 void _initialize_frame ();
2975 _initialize_frame ()
2977 obstack_init (&frame_cache_obstack
);
2979 frame_stash_create ();
2981 gdb::observers::target_changed
.attach (frame_observer_target_changed
);
2983 add_basic_prefix_cmd ("backtrace", class_maintenance
, _("\
2984 Set backtrace specific variables.\n\
2985 Configure backtrace variables such as the backtrace limit"),
2986 &set_backtrace_cmdlist
, "set backtrace ",
2987 0/*allow-unknown*/, &setlist
);
2988 add_show_prefix_cmd ("backtrace", class_maintenance
, _("\
2989 Show backtrace specific variables.\n\
2990 Show backtrace variables such as the backtrace limit."),
2991 &show_backtrace_cmdlist
, "show backtrace ",
2992 0/*allow-unknown*/, &showlist
);
2994 add_setshow_uinteger_cmd ("limit", class_obscure
,
2995 &user_set_backtrace_options
.backtrace_limit
, _("\
2996 Set an upper bound on the number of backtrace levels."), _("\
2997 Show the upper bound on the number of backtrace levels."), _("\
2998 No more than the specified number of frames can be displayed or examined.\n\
2999 Literal \"unlimited\" or zero means no limit."),
3001 show_backtrace_limit
,
3002 &set_backtrace_cmdlist
,
3003 &show_backtrace_cmdlist
);
3005 gdb::option::add_setshow_cmds_for_options
3006 (class_stack
, &user_set_backtrace_options
,
3007 set_backtrace_option_defs
, &set_backtrace_cmdlist
, &show_backtrace_cmdlist
);
3009 /* Debug this files internals. */
3010 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
3011 Set frame debugging."), _("\
3012 Show frame debugging."), _("\
3013 When non-zero, frame specific internal debugging is enabled."),
3016 &setdebuglist
, &showdebuglist
);