1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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 /* The values behind the global "set backtrace ..." settings. */
57 set_backtrace_options user_set_backtrace_options;
59 static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
60 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
62 /* Status of some values cached in the frame_info object. */
64 enum cached_copy_status
66 /* Value is unknown. */
69 /* We have a value. */
72 /* Value was not saved. */
75 /* Value is unavailable. */
79 /* We keep a cache of stack frames, each of which is a "struct
80 frame_info". The innermost one gets allocated (in
81 wait_for_inferior) each time the inferior stops; sentinel_frame
82 points to it. Additional frames get allocated (in get_prev_frame)
83 as needed, and are chained through the next and prev fields. Any
84 time that the frame cache becomes invalid (most notably when we
85 execute something, but also if we change how we interpret the
86 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
87 which reads new symbols)), we should call reinit_frame_cache. */
91 /* Level of this frame. The inner-most (youngest) frame is at level
92 0. As you move towards the outer-most (oldest) frame, the level
93 increases. This is a cached value. It could just as easily be
94 computed by counting back from the selected frame to the inner
96 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
97 reserved to indicate a bogus frame - one that has been created
98 just to keep GDB happy (GDB always needs a frame). For the
99 moment leave this as speculation. */
102 /* The frame's program space. */
103 struct program_space *pspace;
105 /* The frame's address space. */
106 const address_space *aspace;
108 /* The frame's low-level unwinder and corresponding cache. The
109 low-level unwinder is responsible for unwinding register values
110 for the previous frame. The low-level unwind methods are
111 selected based on the presence, or otherwise, of register unwind
112 information such as CFI. */
113 void *prologue_cache;
114 const struct frame_unwind *unwind;
116 /* Cached copy of the previous frame's architecture. */
120 struct gdbarch *arch;
123 /* Cached copy of the previous frame's resume address. */
125 enum cached_copy_status status;
129 /* Cached copy of the previous frame's function address. */
136 /* This frame's ID. */
140 struct frame_id value;
143 /* The frame's high-level base methods, and corresponding cache.
144 The high level base methods are selected based on the frame's
146 const struct frame_base *base;
149 /* Pointers to the next (down, inner, younger) and previous (up,
150 outer, older) frame_info's in the frame cache. */
151 struct frame_info *next; /* down, inner, younger */
153 struct frame_info *prev; /* up, outer, older */
155 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
156 could. Only valid when PREV_P is set. */
157 enum unwind_stop_reason stop_reason;
159 /* A frame specific string describing the STOP_REASON in more detail.
160 Only valid when PREV_P is set, but even then may still be NULL. */
161 const char *stop_string;
164 /* A frame stash used to speed up frame lookups. Create a hash table
165 to stash frames previously accessed from the frame cache for
166 quicker subsequent retrieval. The hash table is emptied whenever
167 the frame cache is invalidated. */
169 static htab_t frame_stash;
171 /* Internal function to calculate a hash from the frame_id addresses,
172 using as many valid addresses as possible. Frames below level 0
173 are not stored in the hash table. */
176 frame_addr_hash (const void *ap)
178 const struct frame_info *frame = (const struct frame_info *) ap;
179 const struct frame_id f_id = frame->this_id.value;
182 gdb_assert (f_id.stack_status != FID_STACK_INVALID
184 || f_id.special_addr_p);
186 if (f_id.stack_status == FID_STACK_VALID)
187 hash = iterative_hash (&f_id.stack_addr,
188 sizeof (f_id.stack_addr), hash);
189 if (f_id.code_addr_p)
190 hash = iterative_hash (&f_id.code_addr,
191 sizeof (f_id.code_addr), hash);
192 if (f_id.special_addr_p)
193 hash = iterative_hash (&f_id.special_addr,
194 sizeof (f_id.special_addr), hash);
199 /* Internal equality function for the hash table. This function
200 defers equality operations to frame_id_eq. */
203 frame_addr_hash_eq (const void *a, const void *b)
205 const struct frame_info *f_entry = (const struct frame_info *) a;
206 const struct frame_info *f_element = (const struct frame_info *) b;
208 return frame_id_eq (f_entry->this_id.value,
209 f_element->this_id.value);
212 /* Internal function to create the frame_stash hash table. 100 seems
213 to be a good compromise to start the hash table at. */
216 frame_stash_create (void)
218 frame_stash = htab_create (100,
224 /* Internal function to add a frame to the frame_stash hash table.
225 Returns false if a frame with the same ID was already stashed, true
229 frame_stash_add (struct frame_info *frame)
231 struct frame_info **slot;
233 /* Do not try to stash the sentinel frame. */
234 gdb_assert (frame->level >= 0);
236 slot = (struct frame_info **) htab_find_slot (frame_stash,
240 /* If we already have a frame in the stack with the same id, we
241 either have a stack cycle (corrupted stack?), or some bug
242 elsewhere in GDB. In any case, ignore the duplicate and return
243 an indication to the caller. */
251 /* Internal function to search the frame stash for an entry with the
252 given frame ID. If found, return that frame. Otherwise return
255 static struct frame_info *
256 frame_stash_find (struct frame_id id)
258 struct frame_info dummy;
259 struct frame_info *frame;
261 dummy.this_id.value = id;
262 frame = (struct frame_info *) htab_find (frame_stash, &dummy);
266 /* Internal function to invalidate the frame stash by removing all
267 entries in it. This only occurs when the frame cache is
271 frame_stash_invalidate (void)
273 htab_empty (frame_stash);
277 scoped_restore_selected_frame::scoped_restore_selected_frame ()
279 m_fid = get_frame_id (get_selected_frame (NULL));
283 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
285 frame_info *frame = frame_find_by_id (m_fid);
287 warning (_("Unable to restore previously selected frame."));
289 select_frame (frame);
292 /* Flag to control debugging. */
294 unsigned int frame_debug;
296 show_frame_debug (struct ui_file *file, int from_tty,
297 struct cmd_list_element *c, const char *value)
299 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
302 /* Implementation of "show backtrace past-main". */
305 show_backtrace_past_main (struct ui_file *file, int from_tty,
306 struct cmd_list_element *c, const char *value)
308 fprintf_filtered (file,
309 _("Whether backtraces should "
310 "continue past \"main\" is %s.\n"),
314 /* Implementation of "show backtrace past-entry". */
317 show_backtrace_past_entry (struct ui_file *file, int from_tty,
318 struct cmd_list_element *c, const char *value)
320 fprintf_filtered (file, _("Whether backtraces should continue past the "
321 "entry point of a program is %s.\n"),
325 /* Implementation of "show backtrace limit". */
328 show_backtrace_limit (struct ui_file *file, int from_tty,
329 struct cmd_list_element *c, const char *value)
331 fprintf_filtered (file,
332 _("An upper bound on the number "
333 "of backtrace levels is %s.\n"),
339 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
342 fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
344 fprintf_unfiltered (file, "!%s", name);
348 fprint_frame_id (struct ui_file *file, struct frame_id id)
350 fprintf_unfiltered (file, "{");
352 if (id.stack_status == FID_STACK_INVALID)
353 fprintf_unfiltered (file, "!stack");
354 else if (id.stack_status == FID_STACK_UNAVAILABLE)
355 fprintf_unfiltered (file, "stack=<unavailable>");
356 else if (id.stack_status == FID_STACK_SENTINEL)
357 fprintf_unfiltered (file, "stack=<sentinel>");
359 fprintf_unfiltered (file, "stack=%s", hex_string (id.stack_addr));
360 fprintf_unfiltered (file, ",");
362 fprint_field (file, "code", id.code_addr_p, id.code_addr);
363 fprintf_unfiltered (file, ",");
365 fprint_field (file, "special", id.special_addr_p, id.special_addr);
367 if (id.artificial_depth)
368 fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
370 fprintf_unfiltered (file, "}");
374 fprint_frame_type (struct ui_file *file, enum frame_type type)
379 fprintf_unfiltered (file, "NORMAL_FRAME");
382 fprintf_unfiltered (file, "DUMMY_FRAME");
385 fprintf_unfiltered (file, "INLINE_FRAME");
388 fprintf_unfiltered (file, "TAILCALL_FRAME");
391 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
394 fprintf_unfiltered (file, "ARCH_FRAME");
397 fprintf_unfiltered (file, "SENTINEL_FRAME");
400 fprintf_unfiltered (file, "<unknown type>");
406 fprint_frame (struct ui_file *file, struct frame_info *fi)
410 fprintf_unfiltered (file, "<NULL frame>");
413 fprintf_unfiltered (file, "{");
414 fprintf_unfiltered (file, "level=%d", fi->level);
415 fprintf_unfiltered (file, ",");
416 fprintf_unfiltered (file, "type=");
417 if (fi->unwind != NULL)
418 fprint_frame_type (file, fi->unwind->type);
420 fprintf_unfiltered (file, "<unknown>");
421 fprintf_unfiltered (file, ",");
422 fprintf_unfiltered (file, "unwind=");
423 if (fi->unwind != NULL)
424 gdb_print_host_address (fi->unwind, file);
426 fprintf_unfiltered (file, "<unknown>");
427 fprintf_unfiltered (file, ",");
428 fprintf_unfiltered (file, "pc=");
429 if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
430 fprintf_unfiltered (file, "<unknown>");
431 else if (fi->next->prev_pc.status == CC_VALUE)
432 fprintf_unfiltered (file, "%s",
433 hex_string (fi->next->prev_pc.value));
434 else if (fi->next->prev_pc.status == CC_NOT_SAVED)
435 val_print_not_saved (file);
436 else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
437 val_print_unavailable (file);
438 fprintf_unfiltered (file, ",");
439 fprintf_unfiltered (file, "id=");
441 fprint_frame_id (file, fi->this_id.value);
443 fprintf_unfiltered (file, "<unknown>");
444 fprintf_unfiltered (file, ",");
445 fprintf_unfiltered (file, "func=");
446 if (fi->next != NULL && fi->next->prev_func.p)
447 fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
449 fprintf_unfiltered (file, "<unknown>");
450 fprintf_unfiltered (file, "}");
453 /* Given FRAME, return the enclosing frame as found in real frames read-in from
454 inferior memory. Skip any previous frames which were made up by GDB.
455 Return FRAME if FRAME is a non-artificial frame.
456 Return NULL if FRAME is the start of an artificial-only chain. */
458 static struct frame_info *
459 skip_artificial_frames (struct frame_info *frame)
461 /* Note we use get_prev_frame_always, and not get_prev_frame. The
462 latter will truncate the frame chain, leading to this function
463 unintentionally returning a null_frame_id (e.g., when the user
464 sets a backtrace limit).
466 Note that for record targets we may get a frame chain that consists
467 of artificial frames only. */
468 while (get_frame_type (frame) == INLINE_FRAME
469 || get_frame_type (frame) == TAILCALL_FRAME)
471 frame = get_prev_frame_always (frame);
480 skip_unwritable_frames (struct frame_info *frame)
482 while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0)
484 frame = get_prev_frame (frame);
495 skip_tailcall_frames (struct frame_info *frame)
497 while (get_frame_type (frame) == TAILCALL_FRAME)
499 /* Note that for record targets we may get a frame chain that consists of
500 tailcall frames only. */
501 frame = get_prev_frame (frame);
509 /* Compute the frame's uniq ID that can be used to, later, re-find the
513 compute_frame_id (struct frame_info *fi)
515 gdb_assert (!fi->this_id.p);
518 fprintf_unfiltered (gdb_stdlog, "{ compute_frame_id (fi=%d) ",
520 /* Find the unwinder. */
521 if (fi->unwind == NULL)
522 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
523 /* Find THIS frame's ID. */
524 /* Default to outermost if no ID is found. */
525 fi->this_id.value = outer_frame_id;
526 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
527 gdb_assert (frame_id_p (fi->this_id.value));
531 fprintf_unfiltered (gdb_stdlog, "-> ");
532 fprint_frame_id (gdb_stdlog, fi->this_id.value);
533 fprintf_unfiltered (gdb_stdlog, " }\n");
537 /* Return a frame uniq ID that can be used to, later, re-find the
541 get_frame_id (struct frame_info *fi)
544 return null_frame_id;
550 /* If we haven't computed the frame id yet, then it must be that
551 this is the current frame. Compute it now, and stash the
552 result. The IDs of other frames are computed as soon as
553 they're created, in order to detect cycles. See
554 get_prev_frame_if_no_cycle. */
555 gdb_assert (fi->level == 0);
558 compute_frame_id (fi);
560 /* Since this is the first frame in the chain, this should
562 stashed = frame_stash_add (fi);
563 gdb_assert (stashed);
566 return fi->this_id.value;
570 get_stack_frame_id (struct frame_info *next_frame)
572 return get_frame_id (skip_artificial_frames (next_frame));
576 frame_unwind_caller_id (struct frame_info *next_frame)
578 struct frame_info *this_frame;
580 /* Use get_prev_frame_always, and not get_prev_frame. The latter
581 will truncate the frame chain, leading to this function
582 unintentionally returning a null_frame_id (e.g., when a caller
583 requests the frame ID of "main()"s caller. */
585 next_frame = skip_artificial_frames (next_frame);
586 if (next_frame == NULL)
587 return null_frame_id;
589 this_frame = get_prev_frame_always (next_frame);
591 return get_frame_id (skip_artificial_frames (this_frame));
593 return null_frame_id;
596 const struct frame_id null_frame_id = { 0 }; /* All zeros. */
597 const struct frame_id sentinel_frame_id = { 0, 0, 0, FID_STACK_SENTINEL, 0, 1, 0 };
598 const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_INVALID, 0, 1, 0 };
601 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
602 CORE_ADDR special_addr)
604 struct frame_id id = null_frame_id;
606 id.stack_addr = stack_addr;
607 id.stack_status = FID_STACK_VALID;
608 id.code_addr = code_addr;
610 id.special_addr = special_addr;
611 id.special_addr_p = 1;
618 frame_id_build_unavailable_stack (CORE_ADDR code_addr)
620 struct frame_id id = null_frame_id;
622 id.stack_status = FID_STACK_UNAVAILABLE;
623 id.code_addr = code_addr;
631 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
632 CORE_ADDR special_addr)
634 struct frame_id id = null_frame_id;
636 id.stack_status = FID_STACK_UNAVAILABLE;
637 id.code_addr = code_addr;
639 id.special_addr = special_addr;
640 id.special_addr_p = 1;
645 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
647 struct frame_id id = null_frame_id;
649 id.stack_addr = stack_addr;
650 id.stack_status = FID_STACK_VALID;
651 id.code_addr = code_addr;
657 frame_id_build_wild (CORE_ADDR stack_addr)
659 struct frame_id id = null_frame_id;
661 id.stack_addr = stack_addr;
662 id.stack_status = FID_STACK_VALID;
667 frame_id_p (struct frame_id l)
671 /* The frame is valid iff it has a valid stack address. */
672 p = l.stack_status != FID_STACK_INVALID;
673 /* outer_frame_id is also valid. */
674 if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
678 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
679 fprint_frame_id (gdb_stdlog, l);
680 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
686 frame_id_artificial_p (struct frame_id l)
691 return (l.artificial_depth != 0);
695 frame_id_eq (struct frame_id l, struct frame_id r)
699 if (l.stack_status == FID_STACK_INVALID && l.special_addr_p
700 && r.stack_status == FID_STACK_INVALID && r.special_addr_p)
701 /* The outermost frame marker is equal to itself. This is the
702 dodgy thing about outer_frame_id, since between execution steps
703 we might step into another function - from which we can't
704 unwind either. More thought required to get rid of
707 else if (l.stack_status == FID_STACK_INVALID
708 || r.stack_status == FID_STACK_INVALID)
709 /* Like a NaN, if either ID is invalid, the result is false.
710 Note that a frame ID is invalid iff it is the null frame ID. */
712 else if (l.stack_status != r.stack_status || l.stack_addr != r.stack_addr)
713 /* If .stack addresses are different, the frames are different. */
715 else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
716 /* An invalid code addr is a wild card. If .code addresses are
717 different, the frames are different. */
719 else if (l.special_addr_p && r.special_addr_p
720 && l.special_addr != r.special_addr)
721 /* An invalid special addr is a wild card (or unused). Otherwise
722 if special addresses are different, the frames are different. */
724 else if (l.artificial_depth != r.artificial_depth)
725 /* If artifical depths are different, the frames must be different. */
728 /* Frames are equal. */
733 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
734 fprint_frame_id (gdb_stdlog, l);
735 fprintf_unfiltered (gdb_stdlog, ",r=");
736 fprint_frame_id (gdb_stdlog, r);
737 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
742 /* Safety net to check whether frame ID L should be inner to
743 frame ID R, according to their stack addresses.
745 This method cannot be used to compare arbitrary frames, as the
746 ranges of valid stack addresses may be discontiguous (e.g. due
749 However, it can be used as safety net to discover invalid frame
750 IDs in certain circumstances. Assuming that NEXT is the immediate
751 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
753 * The stack address of NEXT must be inner-than-or-equal to the stack
756 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
759 * If NEXT and THIS have different stack addresses, no other frame
760 in the frame chain may have a stack address in between.
762 Therefore, if frame_id_inner (TEST, THIS) holds, but
763 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
764 to a valid frame in the frame chain.
766 The sanity checks above cannot be performed when a SIGTRAMP frame
767 is involved, because signal handlers might be executed on a different
768 stack than the stack used by the routine that caused the signal
769 to be raised. This can happen for instance when a thread exceeds
770 its maximum stack size. In this case, certain compilers implement
771 a stack overflow strategy that cause the handler to be run on a
775 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
779 if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
780 /* Like NaN, any operation involving an invalid ID always fails.
781 Likewise if either ID has an unavailable stack address. */
783 else if (l.artificial_depth > r.artificial_depth
784 && l.stack_addr == r.stack_addr
785 && l.code_addr_p == r.code_addr_p
786 && l.special_addr_p == r.special_addr_p
787 && l.special_addr == r.special_addr)
789 /* Same function, different inlined functions. */
790 const struct block *lb, *rb;
792 gdb_assert (l.code_addr_p && r.code_addr_p);
794 lb = block_for_pc (l.code_addr);
795 rb = block_for_pc (r.code_addr);
797 if (lb == NULL || rb == NULL)
798 /* Something's gone wrong. */
801 /* This will return true if LB and RB are the same block, or
802 if the block with the smaller depth lexically encloses the
803 block with the greater depth. */
804 inner = contained_in (lb, rb);
807 /* Only return non-zero when strictly inner than. Note that, per
808 comment in "frame.h", there is some fuzz here. Frameless
809 functions are not strictly inner than (same .stack but
810 different .code and/or .special address). */
811 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
814 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
815 fprint_frame_id (gdb_stdlog, l);
816 fprintf_unfiltered (gdb_stdlog, ",r=");
817 fprint_frame_id (gdb_stdlog, r);
818 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
824 frame_find_by_id (struct frame_id id)
826 struct frame_info *frame, *prev_frame;
828 /* ZERO denotes the null frame, let the caller decide what to do
829 about it. Should it instead return get_current_frame()? */
830 if (!frame_id_p (id))
833 /* Check for the sentinel frame. */
834 if (frame_id_eq (id, sentinel_frame_id))
835 return sentinel_frame;
837 /* Try using the frame stash first. Finding it there removes the need
838 to perform the search by looping over all frames, which can be very
839 CPU-intensive if the number of frames is very high (the loop is O(n)
840 and get_prev_frame performs a series of checks that are relatively
841 expensive). This optimization is particularly useful when this function
842 is called from another function (such as value_fetch_lazy, case
843 VALUE_LVAL (val) == lval_register) which already loops over all frames,
844 making the overall behavior O(n^2). */
845 frame = frame_stash_find (id);
849 for (frame = get_current_frame (); ; frame = prev_frame)
851 struct frame_id self = get_frame_id (frame);
853 if (frame_id_eq (id, self))
854 /* An exact match. */
857 prev_frame = get_prev_frame (frame);
861 /* As a safety net to avoid unnecessary backtracing while trying
862 to find an invalid ID, we check for a common situation where
863 we can detect from comparing stack addresses that no other
864 frame in the current frame chain can have this ID. See the
865 comment at frame_id_inner for details. */
866 if (get_frame_type (frame) == NORMAL_FRAME
867 && !frame_id_inner (get_frame_arch (frame), id, self)
868 && frame_id_inner (get_frame_arch (prev_frame), id,
869 get_frame_id (prev_frame)))
876 frame_unwind_pc (struct frame_info *this_frame)
878 if (this_frame->prev_pc.status == CC_UNKNOWN)
880 struct gdbarch *prev_gdbarch;
884 /* The right way. The `pure' way. The one true way. This
885 method depends solely on the register-unwind code to
886 determine the value of registers in THIS frame, and hence
887 the value of this frame's PC (resume address). A typical
888 implementation is no more than:
890 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
891 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
893 Note: this method is very heavily dependent on a correct
894 register-unwind implementation, it pays to fix that
895 method first; this method is frame type agnostic, since
896 it only deals with register values, it works with any
897 frame. This is all in stark contrast to the old
898 FRAME_SAVED_PC which would try to directly handle all the
899 different ways that a PC could be unwound. */
900 prev_gdbarch = frame_unwind_arch (this_frame);
904 pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
907 catch (const gdb_exception_error &ex)
909 if (ex.error == NOT_AVAILABLE_ERROR)
911 this_frame->prev_pc.status = CC_UNAVAILABLE;
914 fprintf_unfiltered (gdb_stdlog,
915 "{ frame_unwind_pc (this_frame=%d)"
916 " -> <unavailable> }\n",
919 else if (ex.error == OPTIMIZED_OUT_ERROR)
921 this_frame->prev_pc.status = CC_NOT_SAVED;
924 fprintf_unfiltered (gdb_stdlog,
925 "{ frame_unwind_pc (this_frame=%d)"
926 " -> <not saved> }\n",
935 this_frame->prev_pc.value = pc;
936 this_frame->prev_pc.status = CC_VALUE;
938 fprintf_unfiltered (gdb_stdlog,
939 "{ frame_unwind_pc (this_frame=%d) "
942 hex_string (this_frame->prev_pc.value));
946 if (this_frame->prev_pc.status == CC_VALUE)
947 return this_frame->prev_pc.value;
948 else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
949 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
950 else if (this_frame->prev_pc.status == CC_NOT_SAVED)
951 throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
953 internal_error (__FILE__, __LINE__,
954 "unexpected prev_pc status: %d",
955 (int) this_frame->prev_pc.status);
959 frame_unwind_caller_pc (struct frame_info *this_frame)
961 this_frame = skip_artificial_frames (this_frame);
963 /* We must have a non-artificial frame. The caller is supposed to check
964 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
966 gdb_assert (this_frame != NULL);
968 return frame_unwind_pc (this_frame);
972 get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
974 struct frame_info *next_frame = this_frame->next;
976 if (!next_frame->prev_func.p)
978 CORE_ADDR addr_in_block;
980 /* Make certain that this, and not the adjacent, function is
982 if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
984 next_frame->prev_func.p = -1;
986 fprintf_unfiltered (gdb_stdlog,
987 "{ get_frame_func (this_frame=%d)"
988 " -> unavailable }\n",
993 next_frame->prev_func.p = 1;
994 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
996 fprintf_unfiltered (gdb_stdlog,
997 "{ get_frame_func (this_frame=%d) -> %s }\n",
999 hex_string (next_frame->prev_func.addr));
1003 if (next_frame->prev_func.p < 0)
1010 *pc = next_frame->prev_func.addr;
1016 get_frame_func (struct frame_info *this_frame)
1020 if (!get_frame_func_if_available (this_frame, &pc))
1021 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
1026 std::unique_ptr<readonly_detached_regcache>
1027 frame_save_as_regcache (struct frame_info *this_frame)
1029 auto cooked_read = [this_frame] (int regnum, gdb_byte *buf)
1031 if (!deprecated_frame_register_read (this_frame, regnum, buf))
1032 return REG_UNAVAILABLE;
1037 std::unique_ptr<readonly_detached_regcache> regcache
1038 (new readonly_detached_regcache (get_frame_arch (this_frame), cooked_read));
1044 frame_pop (struct frame_info *this_frame)
1046 struct frame_info *prev_frame;
1048 if (get_frame_type (this_frame) == DUMMY_FRAME)
1050 /* Popping a dummy frame involves restoring more than just registers.
1051 dummy_frame_pop does all the work. */
1052 dummy_frame_pop (get_frame_id (this_frame), inferior_thread ());
1056 /* Ensure that we have a frame to pop to. */
1057 prev_frame = get_prev_frame_always (this_frame);
1060 error (_("Cannot pop the initial frame."));
1062 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1063 entering THISFRAME. */
1064 prev_frame = skip_tailcall_frames (prev_frame);
1066 if (prev_frame == NULL)
1067 error (_("Cannot find the caller frame."));
1069 /* Make a copy of all the register values unwound from this frame.
1070 Save them in a scratch buffer so that there isn't a race between
1071 trying to extract the old values from the current regcache while
1072 at the same time writing new values into that same cache. */
1073 std::unique_ptr<readonly_detached_regcache> scratch
1074 = frame_save_as_regcache (prev_frame);
1076 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1077 target's register cache that it is about to be hit with a burst
1078 register transfer and that the sequence of register writes should
1079 be batched. The pair target_prepare_to_store() and
1080 target_store_registers() kind of suggest this functionality.
1081 Unfortunately, they don't implement it. Their lack of a formal
1082 definition can lead to targets writing back bogus values
1083 (arguably a bug in the target code mind). */
1084 /* Now copy those saved registers into the current regcache. */
1085 get_current_regcache ()->restore (scratch.get ());
1087 /* We've made right mess of GDB's local state, just discard
1089 reinit_frame_cache ();
1093 frame_register_unwind (frame_info *next_frame, int regnum,
1094 int *optimizedp, int *unavailablep,
1095 enum lval_type *lvalp, CORE_ADDR *addrp,
1096 int *realnump, gdb_byte *bufferp)
1098 struct value *value;
1100 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1101 that the value proper does not need to be fetched. */
1102 gdb_assert (optimizedp != NULL);
1103 gdb_assert (lvalp != NULL);
1104 gdb_assert (addrp != NULL);
1105 gdb_assert (realnump != NULL);
1106 /* gdb_assert (bufferp != NULL); */
1108 value = frame_unwind_register_value (next_frame, regnum);
1110 gdb_assert (value != NULL);
1112 *optimizedp = value_optimized_out (value);
1113 *unavailablep = !value_entirely_available (value);
1114 *lvalp = VALUE_LVAL (value);
1115 *addrp = value_address (value);
1116 if (*lvalp == lval_register)
1117 *realnump = VALUE_REGNUM (value);
1123 if (!*optimizedp && !*unavailablep)
1124 memcpy (bufferp, value_contents_all (value),
1125 TYPE_LENGTH (value_type (value)));
1127 memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
1130 /* Dispose of the new value. This prevents watchpoints from
1131 trying to watch the saved frame pointer. */
1132 release_value (value);
1136 frame_register (struct frame_info *frame, int regnum,
1137 int *optimizedp, int *unavailablep, enum lval_type *lvalp,
1138 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
1140 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1141 that the value proper does not need to be fetched. */
1142 gdb_assert (optimizedp != NULL);
1143 gdb_assert (lvalp != NULL);
1144 gdb_assert (addrp != NULL);
1145 gdb_assert (realnump != NULL);
1146 /* gdb_assert (bufferp != NULL); */
1148 /* Obtain the register value by unwinding the register from the next
1149 (more inner frame). */
1150 gdb_assert (frame != NULL && frame->next != NULL);
1151 frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
1152 lvalp, addrp, realnump, bufferp);
1156 frame_unwind_register (frame_info *next_frame, int regnum, gdb_byte *buf)
1162 enum lval_type lval;
1164 frame_register_unwind (next_frame, regnum, &optimized, &unavailable,
1165 &lval, &addr, &realnum, buf);
1168 throw_error (OPTIMIZED_OUT_ERROR,
1169 _("Register %d was not saved"), regnum);
1171 throw_error (NOT_AVAILABLE_ERROR,
1172 _("Register %d is not available"), regnum);
1176 get_frame_register (struct frame_info *frame,
1177 int regnum, gdb_byte *buf)
1179 frame_unwind_register (frame->next, regnum, buf);
1183 frame_unwind_register_value (frame_info *next_frame, int regnum)
1185 struct gdbarch *gdbarch;
1186 struct value *value;
1188 gdb_assert (next_frame != NULL);
1189 gdbarch = frame_unwind_arch (next_frame);
1193 fprintf_unfiltered (gdb_stdlog,
1194 "{ frame_unwind_register_value "
1195 "(frame=%d,regnum=%d(%s),...) ",
1196 next_frame->level, regnum,
1197 user_reg_map_regnum_to_name (gdbarch, regnum));
1200 /* Find the unwinder. */
1201 if (next_frame->unwind == NULL)
1202 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
1204 /* Ask this frame to unwind its register. */
1205 value = next_frame->unwind->prev_register (next_frame,
1206 &next_frame->prologue_cache,
1211 fprintf_unfiltered (gdb_stdlog, "->");
1212 if (value_optimized_out (value))
1214 fprintf_unfiltered (gdb_stdlog, " ");
1215 val_print_optimized_out (value, gdb_stdlog);
1219 if (VALUE_LVAL (value) == lval_register)
1220 fprintf_unfiltered (gdb_stdlog, " register=%d",
1221 VALUE_REGNUM (value));
1222 else if (VALUE_LVAL (value) == lval_memory)
1223 fprintf_unfiltered (gdb_stdlog, " address=%s",
1225 value_address (value)));
1227 fprintf_unfiltered (gdb_stdlog, " computed");
1229 if (value_lazy (value))
1230 fprintf_unfiltered (gdb_stdlog, " lazy");
1234 const gdb_byte *buf = value_contents (value);
1236 fprintf_unfiltered (gdb_stdlog, " bytes=");
1237 fprintf_unfiltered (gdb_stdlog, "[");
1238 for (i = 0; i < register_size (gdbarch, regnum); i++)
1239 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1240 fprintf_unfiltered (gdb_stdlog, "]");
1244 fprintf_unfiltered (gdb_stdlog, " }\n");
1251 get_frame_register_value (struct frame_info *frame, int regnum)
1253 return frame_unwind_register_value (frame->next, regnum);
1257 frame_unwind_register_signed (frame_info *next_frame, int regnum)
1259 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1260 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1261 int size = register_size (gdbarch, regnum);
1262 struct value *value = frame_unwind_register_value (next_frame, regnum);
1264 gdb_assert (value != NULL);
1266 if (value_optimized_out (value))
1268 throw_error (OPTIMIZED_OUT_ERROR,
1269 _("Register %d was not saved"), regnum);
1271 if (!value_entirely_available (value))
1273 throw_error (NOT_AVAILABLE_ERROR,
1274 _("Register %d is not available"), regnum);
1277 LONGEST r = extract_signed_integer (value_contents_all (value), size,
1280 release_value (value);
1285 get_frame_register_signed (struct frame_info *frame, int regnum)
1287 return frame_unwind_register_signed (frame->next, regnum);
1291 frame_unwind_register_unsigned (frame_info *next_frame, int regnum)
1293 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1294 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1295 int size = register_size (gdbarch, regnum);
1296 struct value *value = frame_unwind_register_value (next_frame, regnum);
1298 gdb_assert (value != NULL);
1300 if (value_optimized_out (value))
1302 throw_error (OPTIMIZED_OUT_ERROR,
1303 _("Register %d was not saved"), regnum);
1305 if (!value_entirely_available (value))
1307 throw_error (NOT_AVAILABLE_ERROR,
1308 _("Register %d is not available"), regnum);
1311 ULONGEST r = extract_unsigned_integer (value_contents_all (value), size,
1314 release_value (value);
1319 get_frame_register_unsigned (struct frame_info *frame, int regnum)
1321 return frame_unwind_register_unsigned (frame->next, regnum);
1325 read_frame_register_unsigned (struct frame_info *frame, int regnum,
1328 struct value *regval = get_frame_register_value (frame, regnum);
1330 if (!value_optimized_out (regval)
1331 && value_entirely_available (regval))
1333 struct gdbarch *gdbarch = get_frame_arch (frame);
1334 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1335 int size = register_size (gdbarch, VALUE_REGNUM (regval));
1337 *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
1345 put_frame_register (struct frame_info *frame, int regnum,
1346 const gdb_byte *buf)
1348 struct gdbarch *gdbarch = get_frame_arch (frame);
1352 enum lval_type lval;
1355 frame_register (frame, regnum, &optim, &unavail,
1356 &lval, &addr, &realnum, NULL);
1358 error (_("Attempt to assign to a register that was not saved."));
1363 write_memory (addr, buf, register_size (gdbarch, regnum));
1367 get_current_regcache ()->cooked_write (realnum, buf);
1370 error (_("Attempt to assign to an unmodifiable value."));
1374 /* This function is deprecated. Use get_frame_register_value instead,
1375 which provides more accurate information.
1377 Find and return the value of REGNUM for the specified stack frame.
1378 The number of bytes copied is REGISTER_SIZE (REGNUM).
1380 Returns 0 if the register value could not be found. */
1383 deprecated_frame_register_read (struct frame_info *frame, int regnum,
1388 enum lval_type lval;
1392 frame_register (frame, regnum, &optimized, &unavailable,
1393 &lval, &addr, &realnum, myaddr);
1395 return !optimized && !unavailable;
1399 get_frame_register_bytes (struct frame_info *frame, int regnum,
1400 CORE_ADDR offset, int len, gdb_byte *myaddr,
1401 int *optimizedp, int *unavailablep)
1403 struct gdbarch *gdbarch = get_frame_arch (frame);
1408 /* Skip registers wholly inside of OFFSET. */
1409 while (offset >= register_size (gdbarch, regnum))
1411 offset -= register_size (gdbarch, regnum);
1415 /* Ensure that we will not read beyond the end of the register file.
1416 This can only ever happen if the debug information is bad. */
1418 numregs = gdbarch_num_cooked_regs (gdbarch);
1419 for (i = regnum; i < numregs; i++)
1421 int thissize = register_size (gdbarch, i);
1424 break; /* This register is not available on this architecture. */
1425 maxsize += thissize;
1428 error (_("Bad debug information detected: "
1429 "Attempt to read %d bytes from registers."), len);
1431 /* Copy the data. */
1434 int curr_len = register_size (gdbarch, regnum) - offset;
1439 if (curr_len == register_size (gdbarch, regnum))
1441 enum lval_type lval;
1445 frame_register (frame, regnum, optimizedp, unavailablep,
1446 &lval, &addr, &realnum, myaddr);
1447 if (*optimizedp || *unavailablep)
1452 struct value *value = frame_unwind_register_value (frame->next,
1454 gdb_assert (value != NULL);
1455 *optimizedp = value_optimized_out (value);
1456 *unavailablep = !value_entirely_available (value);
1458 if (*optimizedp || *unavailablep)
1460 release_value (value);
1463 memcpy (myaddr, value_contents_all (value) + offset, curr_len);
1464 release_value (value);
1479 put_frame_register_bytes (struct frame_info *frame, int regnum,
1480 CORE_ADDR offset, int len, const gdb_byte *myaddr)
1482 struct gdbarch *gdbarch = get_frame_arch (frame);
1484 /* Skip registers wholly inside of OFFSET. */
1485 while (offset >= register_size (gdbarch, regnum))
1487 offset -= register_size (gdbarch, regnum);
1491 /* Copy the data. */
1494 int curr_len = register_size (gdbarch, regnum) - offset;
1499 if (curr_len == register_size (gdbarch, regnum))
1501 put_frame_register (frame, regnum, myaddr);
1505 struct value *value = frame_unwind_register_value (frame->next,
1507 gdb_assert (value != NULL);
1509 memcpy ((char *) value_contents_writeable (value) + offset, myaddr,
1511 put_frame_register (frame, regnum, value_contents_raw (value));
1512 release_value (value);
1522 /* Create a sentinel frame. */
1524 static struct frame_info *
1525 create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
1527 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1530 frame->pspace = pspace;
1531 frame->aspace = regcache->aspace ();
1532 /* Explicitly initialize the sentinel frame's cache. Provide it
1533 with the underlying regcache. In the future additional
1534 information, such as the frame's thread will be added. */
1535 frame->prologue_cache = sentinel_frame_cache (regcache);
1536 /* For the moment there is only one sentinel frame implementation. */
1537 frame->unwind = &sentinel_frame_unwind;
1538 /* Link this frame back to itself. The frame is self referential
1539 (the unwound PC is the same as the pc), so make it so. */
1540 frame->next = frame;
1541 /* The sentinel frame has a special ID. */
1542 frame->this_id.p = 1;
1543 frame->this_id.value = sentinel_frame_id;
1546 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1547 fprint_frame (gdb_stdlog, frame);
1548 fprintf_unfiltered (gdb_stdlog, " }\n");
1553 /* Cache for frame addresses already read by gdb. Valid only while
1554 inferior is stopped. Control variables for the frame cache should
1555 be local to this module. */
1557 static struct obstack frame_cache_obstack;
1560 frame_obstack_zalloc (unsigned long size)
1562 void *data = obstack_alloc (&frame_cache_obstack, size);
1564 memset (data, 0, size);
1568 static struct frame_info *get_prev_frame_always_1 (struct frame_info *this_frame);
1571 get_current_frame (void)
1573 struct frame_info *current_frame;
1575 /* First check, and report, the lack of registers. Having GDB
1576 report "No stack!" or "No memory" when the target doesn't even
1577 have registers is very confusing. Besides, "printcmd.exp"
1578 explicitly checks that ``print $pc'' with no registers prints "No
1580 if (!target_has_registers)
1581 error (_("No registers."));
1582 if (!target_has_stack)
1583 error (_("No stack."));
1584 if (!target_has_memory)
1585 error (_("No memory."));
1586 /* Traceframes are effectively a substitute for the live inferior. */
1587 if (get_traceframe_number () < 0)
1588 validate_registers_access ();
1590 if (sentinel_frame == NULL)
1592 create_sentinel_frame (current_program_space, get_current_regcache ());
1594 /* Set the current frame before computing the frame id, to avoid
1595 recursion inside compute_frame_id, in case the frame's
1596 unwinder decides to do a symbol lookup (which depends on the
1597 selected frame's block).
1599 This call must always succeed. In particular, nothing inside
1600 get_prev_frame_always_1 should try to unwind from the
1601 sentinel frame, because that could fail/throw, and we always
1602 want to leave with the current frame created and linked in --
1603 we should never end up with the sentinel frame as outermost
1605 current_frame = get_prev_frame_always_1 (sentinel_frame);
1606 gdb_assert (current_frame != NULL);
1608 return current_frame;
1611 /* The "selected" stack frame is used by default for local and arg
1612 access. May be zero, for no selected frame. */
1614 static struct frame_info *selected_frame;
1617 has_stack_frames (void)
1619 if (!target_has_registers || !target_has_stack || !target_has_memory)
1622 /* Traceframes are effectively a substitute for the live inferior. */
1623 if (get_traceframe_number () < 0)
1625 /* No current inferior, no frame. */
1626 if (inferior_ptid == null_ptid)
1629 thread_info *tp = inferior_thread ();
1630 /* Don't try to read from a dead thread. */
1631 if (tp->state == THREAD_EXITED)
1634 /* ... or from a spinning thread. */
1642 /* Return the selected frame. Always non-NULL (unless there isn't an
1643 inferior sufficient for creating a frame) in which case an error is
1647 get_selected_frame (const char *message)
1649 if (selected_frame == NULL)
1651 if (message != NULL && !has_stack_frames ())
1652 error (("%s"), message);
1653 /* Hey! Don't trust this. It should really be re-finding the
1654 last selected frame of the currently selected thread. This,
1655 though, is better than nothing. */
1656 select_frame (get_current_frame ());
1658 /* There is always a frame. */
1659 gdb_assert (selected_frame != NULL);
1660 return selected_frame;
1663 /* If there is a selected frame, return it. Otherwise, return NULL. */
1666 get_selected_frame_if_set (void)
1668 return selected_frame;
1671 /* This is a variant of get_selected_frame() which can be called when
1672 the inferior does not have a frame; in that case it will return
1673 NULL instead of calling error(). */
1676 deprecated_safe_get_selected_frame (void)
1678 if (!has_stack_frames ())
1680 return get_selected_frame (NULL);
1683 /* Select frame FI (or NULL - to invalidate the current frame). */
1686 select_frame (struct frame_info *fi)
1688 selected_frame = fi;
1689 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1690 frame is being invalidated. */
1692 /* FIXME: kseitz/2002-08-28: It would be nice to call
1693 selected_frame_level_changed_event() right here, but due to limitations
1694 in the current interfaces, we would end up flooding UIs with events
1695 because select_frame() is used extensively internally.
1697 Once we have frame-parameterized frame (and frame-related) commands,
1698 the event notification can be moved here, since this function will only
1699 be called when the user's selected frame is being changed. */
1701 /* Ensure that symbols for this frame are read in. Also, determine the
1702 source language of this frame, and switch to it if desired. */
1707 /* We retrieve the frame's symtab by using the frame PC.
1708 However we cannot use the frame PC as-is, because it usually
1709 points to the instruction following the "call", which is
1710 sometimes the first instruction of another function. So we
1711 rely on get_frame_address_in_block() which provides us with a
1712 PC which is guaranteed to be inside the frame's code
1714 if (get_frame_address_in_block_if_available (fi, &pc))
1716 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
1719 && compunit_language (cust) != current_language->la_language
1720 && compunit_language (cust) != language_unknown
1721 && language_mode == language_mode_auto)
1722 set_language (compunit_language (cust));
1727 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1728 Always returns a non-NULL value. */
1731 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1733 struct frame_info *fi;
1737 fprintf_unfiltered (gdb_stdlog,
1738 "{ create_new_frame (addr=%s, pc=%s) ",
1739 hex_string (addr), hex_string (pc));
1742 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1744 fi->next = create_sentinel_frame (current_program_space,
1745 get_current_regcache ());
1747 /* Set/update this frame's cached PC value, found in the next frame.
1748 Do this before looking for this frame's unwinder. A sniffer is
1749 very likely to read this, and the corresponding unwinder is
1750 entitled to rely that the PC doesn't magically change. */
1751 fi->next->prev_pc.value = pc;
1752 fi->next->prev_pc.status = CC_VALUE;
1754 /* We currently assume that frame chain's can't cross spaces. */
1755 fi->pspace = fi->next->pspace;
1756 fi->aspace = fi->next->aspace;
1758 /* Select/initialize both the unwind function and the frame's type
1760 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1763 fi->this_id.value = frame_id_build (addr, pc);
1767 fprintf_unfiltered (gdb_stdlog, "-> ");
1768 fprint_frame (gdb_stdlog, fi);
1769 fprintf_unfiltered (gdb_stdlog, " }\n");
1775 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1776 innermost frame). Be careful to not fall off the bottom of the
1777 frame chain and onto the sentinel frame. */
1780 get_next_frame (struct frame_info *this_frame)
1782 if (this_frame->level > 0)
1783 return this_frame->next;
1788 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1789 innermost (i.e. current) frame, return the sentinel frame. Thus,
1790 unlike get_next_frame(), NULL will never be returned. */
1793 get_next_frame_sentinel_okay (struct frame_info *this_frame)
1795 gdb_assert (this_frame != NULL);
1797 /* Note that, due to the manner in which the sentinel frame is
1798 constructed, this_frame->next still works even when this_frame
1799 is the sentinel frame. But we disallow it here anyway because
1800 calling get_next_frame_sentinel_okay() on the sentinel frame
1801 is likely a coding error. */
1802 gdb_assert (this_frame != sentinel_frame);
1804 return this_frame->next;
1807 /* Observer for the target_changed event. */
1810 frame_observer_target_changed (struct target_ops *target)
1812 reinit_frame_cache ();
1815 /* Flush the entire frame cache. */
1818 reinit_frame_cache (void)
1820 struct frame_info *fi;
1822 /* Tear down all frame caches. */
1823 for (fi = sentinel_frame; fi != NULL; fi = fi->prev)
1825 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1826 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1827 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1828 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1831 /* Since we can't really be sure what the first object allocated was. */
1832 obstack_free (&frame_cache_obstack, 0);
1833 obstack_init (&frame_cache_obstack);
1835 if (sentinel_frame != NULL)
1836 annotate_frames_invalid ();
1838 sentinel_frame = NULL; /* Invalidate cache */
1839 select_frame (NULL);
1840 frame_stash_invalidate ();
1842 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1845 /* Find where a register is saved (in memory or another register).
1846 The result of frame_register_unwind is just where it is saved
1847 relative to this particular frame. */
1850 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1851 int *optimizedp, enum lval_type *lvalp,
1852 CORE_ADDR *addrp, int *realnump)
1854 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1856 while (this_frame != NULL)
1860 frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
1861 lvalp, addrp, realnump, NULL);
1866 if (*lvalp != lval_register)
1870 this_frame = get_next_frame (this_frame);
1874 /* Get the previous raw frame, and check that it is not identical to
1875 same other frame frame already in the chain. If it is, there is
1876 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1877 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1878 validity tests, that compare THIS_FRAME and the next frame, we do
1879 this right after creating the previous frame, to avoid ever ending
1880 up with two frames with the same id in the frame chain. */
1882 static struct frame_info *
1883 get_prev_frame_if_no_cycle (struct frame_info *this_frame)
1885 struct frame_info *prev_frame;
1887 prev_frame = get_prev_frame_raw (this_frame);
1889 /* Don't compute the frame id of the current frame yet. Unwinding
1890 the sentinel frame can fail (e.g., if the thread is gone and we
1891 can't thus read its registers). If we let the cycle detection
1892 code below try to compute a frame ID, then an error thrown from
1893 within the frame ID computation would result in the sentinel
1894 frame as outermost frame, which is bogus. Instead, we'll compute
1895 the current frame's ID lazily in get_frame_id. Note that there's
1896 no point in doing cycle detection when there's only one frame, so
1897 nothing is lost here. */
1898 if (prev_frame->level == 0)
1903 compute_frame_id (prev_frame);
1904 if (!frame_stash_add (prev_frame))
1906 /* Another frame with the same id was already in the stash. We just
1907 detected a cycle. */
1910 fprintf_unfiltered (gdb_stdlog, "-> ");
1911 fprint_frame (gdb_stdlog, NULL);
1912 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1914 this_frame->stop_reason = UNWIND_SAME_ID;
1916 prev_frame->next = NULL;
1917 this_frame->prev = NULL;
1921 catch (const gdb_exception &ex)
1923 prev_frame->next = NULL;
1924 this_frame->prev = NULL;
1932 /* Helper function for get_prev_frame_always, this is called inside a
1933 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
1934 there is no such frame. This may throw an exception. */
1936 static struct frame_info *
1937 get_prev_frame_always_1 (struct frame_info *this_frame)
1939 struct gdbarch *gdbarch;
1941 gdb_assert (this_frame != NULL);
1942 gdbarch = get_frame_arch (this_frame);
1946 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_always (this_frame=");
1947 if (this_frame != NULL)
1948 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1950 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1951 fprintf_unfiltered (gdb_stdlog, ") ");
1954 /* Only try to do the unwind once. */
1955 if (this_frame->prev_p)
1959 fprintf_unfiltered (gdb_stdlog, "-> ");
1960 fprint_frame (gdb_stdlog, this_frame->prev);
1961 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1963 return this_frame->prev;
1966 /* If the frame unwinder hasn't been selected yet, we must do so
1967 before setting prev_p; otherwise the check for misbehaved
1968 sniffers will think that this frame's sniffer tried to unwind
1969 further (see frame_cleanup_after_sniffer). */
1970 if (this_frame->unwind == NULL)
1971 frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1973 this_frame->prev_p = 1;
1974 this_frame->stop_reason = UNWIND_NO_REASON;
1976 /* If we are unwinding from an inline frame, all of the below tests
1977 were already performed when we unwound from the next non-inline
1978 frame. We must skip them, since we can not get THIS_FRAME's ID
1979 until we have unwound all the way down to the previous non-inline
1981 if (get_frame_type (this_frame) == INLINE_FRAME)
1982 return get_prev_frame_if_no_cycle (this_frame);
1984 /* Check that this frame is unwindable. If it isn't, don't try to
1985 unwind to the prev frame. */
1986 this_frame->stop_reason
1987 = this_frame->unwind->stop_reason (this_frame,
1988 &this_frame->prologue_cache);
1990 if (this_frame->stop_reason != UNWIND_NO_REASON)
1994 enum unwind_stop_reason reason = this_frame->stop_reason;
1996 fprintf_unfiltered (gdb_stdlog, "-> ");
1997 fprint_frame (gdb_stdlog, NULL);
1998 fprintf_unfiltered (gdb_stdlog, " // %s }\n",
1999 frame_stop_reason_symbol_string (reason));
2004 /* Check that this frame's ID isn't inner to (younger, below, next)
2005 the next frame. This happens when a frame unwind goes backwards.
2006 This check is valid only if this frame and the next frame are NORMAL.
2007 See the comment at frame_id_inner for details. */
2008 if (get_frame_type (this_frame) == NORMAL_FRAME
2009 && this_frame->next->unwind->type == NORMAL_FRAME
2010 && frame_id_inner (get_frame_arch (this_frame->next),
2011 get_frame_id (this_frame),
2012 get_frame_id (this_frame->next)))
2014 CORE_ADDR this_pc_in_block;
2015 struct minimal_symbol *morestack_msym;
2016 const char *morestack_name = NULL;
2018 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2019 this_pc_in_block = get_frame_address_in_block (this_frame);
2020 morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
2022 morestack_name = MSYMBOL_LINKAGE_NAME (morestack_msym);
2023 if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
2027 fprintf_unfiltered (gdb_stdlog, "-> ");
2028 fprint_frame (gdb_stdlog, NULL);
2029 fprintf_unfiltered (gdb_stdlog,
2030 " // this frame ID is inner }\n");
2032 this_frame->stop_reason = UNWIND_INNER_ID;
2037 /* Check that this and the next frame do not unwind the PC register
2038 to the same memory location. If they do, then even though they
2039 have different frame IDs, the new frame will be bogus; two
2040 functions can't share a register save slot for the PC. This can
2041 happen when the prologue analyzer finds a stack adjustment, but
2044 This check does assume that the "PC register" is roughly a
2045 traditional PC, even if the gdbarch_unwind_pc method adjusts
2046 it (we do not rely on the value, only on the unwound PC being
2047 dependent on this value). A potential improvement would be
2048 to have the frame prev_pc method and the gdbarch unwind_pc
2049 method set the same lval and location information as
2050 frame_register_unwind. */
2051 if (this_frame->level > 0
2052 && gdbarch_pc_regnum (gdbarch) >= 0
2053 && get_frame_type (this_frame) == NORMAL_FRAME
2054 && (get_frame_type (this_frame->next) == NORMAL_FRAME
2055 || get_frame_type (this_frame->next) == INLINE_FRAME))
2057 int optimized, realnum, nrealnum;
2058 enum lval_type lval, nlval;
2059 CORE_ADDR addr, naddr;
2061 frame_register_unwind_location (this_frame,
2062 gdbarch_pc_regnum (gdbarch),
2063 &optimized, &lval, &addr, &realnum);
2064 frame_register_unwind_location (get_next_frame (this_frame),
2065 gdbarch_pc_regnum (gdbarch),
2066 &optimized, &nlval, &naddr, &nrealnum);
2068 if ((lval == lval_memory && lval == nlval && addr == naddr)
2069 || (lval == lval_register && lval == nlval && realnum == nrealnum))
2073 fprintf_unfiltered (gdb_stdlog, "-> ");
2074 fprint_frame (gdb_stdlog, NULL);
2075 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
2078 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
2079 this_frame->prev = NULL;
2084 return get_prev_frame_if_no_cycle (this_frame);
2087 /* Return a "struct frame_info" corresponding to the frame that called
2088 THIS_FRAME. Returns NULL if there is no such frame.
2090 Unlike get_prev_frame, this function always tries to unwind the
2094 get_prev_frame_always (struct frame_info *this_frame)
2096 struct frame_info *prev_frame = NULL;
2100 prev_frame = get_prev_frame_always_1 (this_frame);
2102 catch (const gdb_exception_error &ex)
2104 if (ex.error == MEMORY_ERROR)
2106 this_frame->stop_reason = UNWIND_MEMORY_ERROR;
2107 if (ex.message != NULL)
2112 /* The error needs to live as long as the frame does.
2113 Allocate using stack local STOP_STRING then assign the
2114 pointer to the frame, this allows the STOP_STRING on the
2115 frame to be of type 'const char *'. */
2116 size = ex.message->size () + 1;
2117 stop_string = (char *) frame_obstack_zalloc (size);
2118 memcpy (stop_string, ex.what (), size);
2119 this_frame->stop_string = stop_string;
2130 /* Construct a new "struct frame_info" and link it previous to
2133 static struct frame_info *
2134 get_prev_frame_raw (struct frame_info *this_frame)
2136 struct frame_info *prev_frame;
2138 /* Allocate the new frame but do not wire it in to the frame chain.
2139 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2140 frame->next to pull some fancy tricks (of course such code is, by
2141 definition, recursive). Try to prevent it.
2143 There is no reason to worry about memory leaks, should the
2144 remainder of the function fail. The allocated memory will be
2145 quickly reclaimed when the frame cache is flushed, and the `we've
2146 been here before' check above will stop repeated memory
2147 allocation calls. */
2148 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
2149 prev_frame->level = this_frame->level + 1;
2151 /* For now, assume we don't have frame chains crossing address
2153 prev_frame->pspace = this_frame->pspace;
2154 prev_frame->aspace = this_frame->aspace;
2156 /* Don't yet compute ->unwind (and hence ->type). It is computed
2157 on-demand in get_frame_type, frame_register_unwind, and
2160 /* Don't yet compute the frame's ID. It is computed on-demand by
2163 /* The unwound frame ID is validate at the start of this function,
2164 as part of the logic to decide if that frame should be further
2165 unwound, and not here while the prev frame is being created.
2166 Doing this makes it possible for the user to examine a frame that
2167 has an invalid frame ID.
2169 Some very old VAX code noted: [...] For the sake of argument,
2170 suppose that the stack is somewhat trashed (which is one reason
2171 that "info frame" exists). So, return 0 (indicating we don't
2172 know the address of the arglist) if we don't know what frame this
2176 this_frame->prev = prev_frame;
2177 prev_frame->next = this_frame;
2181 fprintf_unfiltered (gdb_stdlog, "-> ");
2182 fprint_frame (gdb_stdlog, prev_frame);
2183 fprintf_unfiltered (gdb_stdlog, " }\n");
2189 /* Debug routine to print a NULL frame being returned. */
2192 frame_debug_got_null_frame (struct frame_info *this_frame,
2197 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
2198 if (this_frame != NULL)
2199 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
2201 fprintf_unfiltered (gdb_stdlog, "<NULL>");
2202 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
2206 /* Is this (non-sentinel) frame in the "main"() function? */
2209 inside_main_func (struct frame_info *this_frame)
2211 struct bound_minimal_symbol msymbol;
2214 if (symfile_objfile == 0)
2216 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
2217 if (msymbol.minsym == NULL)
2219 /* Make certain that the code, and not descriptor, address is
2221 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
2222 BMSYMBOL_VALUE_ADDRESS (msymbol),
2223 current_top_target ());
2224 return maddr == get_frame_func (this_frame);
2227 /* Test whether THIS_FRAME is inside the process entry point function. */
2230 inside_entry_func (struct frame_info *this_frame)
2232 CORE_ADDR entry_point;
2234 if (!entry_point_address_query (&entry_point))
2237 return get_frame_func (this_frame) == entry_point;
2240 /* Return a structure containing various interesting information about
2241 the frame that called THIS_FRAME. Returns NULL if there is entier
2242 no such frame or the frame fails any of a set of target-independent
2243 condition that should terminate the frame chain (e.g., as unwinding
2246 This function should not contain target-dependent tests, such as
2247 checking whether the program-counter is zero. */
2250 get_prev_frame (struct frame_info *this_frame)
2255 /* There is always a frame. If this assertion fails, suspect that
2256 something should be calling get_selected_frame() or
2257 get_current_frame(). */
2258 gdb_assert (this_frame != NULL);
2260 /* If this_frame is the current frame, then compute and stash
2261 its frame id prior to fetching and computing the frame id of the
2262 previous frame. Otherwise, the cycle detection code in
2263 get_prev_frame_if_no_cycle() will not work correctly. When
2264 get_frame_id() is called later on, an assertion error will
2265 be triggered in the event of a cycle between the current
2266 frame and its previous frame. */
2267 if (this_frame->level == 0)
2268 get_frame_id (this_frame);
2270 frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
2272 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2273 sense to stop unwinding at a dummy frame. One place where a dummy
2274 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2275 pcsqh register (space register for the instruction at the head of the
2276 instruction queue) cannot be written directly; the only way to set it
2277 is to branch to code that is in the target space. In order to implement
2278 frame dummies on HPUX, the called function is made to jump back to where
2279 the inferior was when the user function was called. If gdb was inside
2280 the main function when we created the dummy frame, the dummy frame will
2281 point inside the main function. */
2282 if (this_frame->level >= 0
2283 && get_frame_type (this_frame) == NORMAL_FRAME
2284 && !user_set_backtrace_options.backtrace_past_main
2286 && inside_main_func (this_frame))
2287 /* Don't unwind past main(). Note, this is done _before_ the
2288 frame has been marked as previously unwound. That way if the
2289 user later decides to enable unwinds past main(), that will
2290 automatically happen. */
2292 frame_debug_got_null_frame (this_frame, "inside main func");
2296 /* If the user's backtrace limit has been exceeded, stop. We must
2297 add two to the current level; one of those accounts for backtrace_limit
2298 being 1-based and the level being 0-based, and the other accounts for
2299 the level of the new frame instead of the level of the current
2301 if (this_frame->level + 2 > user_set_backtrace_options.backtrace_limit)
2303 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
2307 /* If we're already inside the entry function for the main objfile,
2308 then it isn't valid. Don't apply this test to a dummy frame -
2309 dummy frame PCs typically land in the entry func. Don't apply
2310 this test to the sentinel frame. Sentinel frames should always
2311 be allowed to unwind. */
2312 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2313 wasn't checking for "main" in the minimal symbols. With that
2314 fixed asm-source tests now stop in "main" instead of halting the
2315 backtrace in weird and wonderful ways somewhere inside the entry
2316 file. Suspect that tests for inside the entry file/func were
2317 added to work around that (now fixed) case. */
2318 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2319 suggested having the inside_entry_func test use the
2320 inside_main_func() msymbol trick (along with entry_point_address()
2321 I guess) to determine the address range of the start function.
2322 That should provide a far better stopper than the current
2324 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2325 applied tail-call optimizations to main so that a function called
2326 from main returns directly to the caller of main. Since we don't
2327 stop at main, we should at least stop at the entry point of the
2329 if (this_frame->level >= 0
2330 && get_frame_type (this_frame) == NORMAL_FRAME
2331 && !user_set_backtrace_options.backtrace_past_entry
2333 && inside_entry_func (this_frame))
2335 frame_debug_got_null_frame (this_frame, "inside entry func");
2339 /* Assume that the only way to get a zero PC is through something
2340 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2341 will never unwind a zero PC. */
2342 if (this_frame->level > 0
2343 && (get_frame_type (this_frame) == NORMAL_FRAME
2344 || get_frame_type (this_frame) == INLINE_FRAME)
2345 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
2346 && frame_pc_p && frame_pc == 0)
2348 frame_debug_got_null_frame (this_frame, "zero PC");
2352 return get_prev_frame_always (this_frame);
2356 get_prev_frame_id_by_id (struct frame_id id)
2358 struct frame_id prev_id;
2359 struct frame_info *frame;
2361 frame = frame_find_by_id (id);
2364 prev_id = get_frame_id (get_prev_frame (frame));
2366 prev_id = null_frame_id;
2372 get_frame_pc (struct frame_info *frame)
2374 gdb_assert (frame->next != NULL);
2375 return frame_unwind_pc (frame->next);
2379 get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
2382 gdb_assert (frame->next != NULL);
2386 *pc = frame_unwind_pc (frame->next);
2388 catch (const gdb_exception_error &ex)
2390 if (ex.error == NOT_AVAILABLE_ERROR)
2399 /* Return an address that falls within THIS_FRAME's code block. */
2402 get_frame_address_in_block (struct frame_info *this_frame)
2404 /* A draft address. */
2405 CORE_ADDR pc = get_frame_pc (this_frame);
2407 struct frame_info *next_frame = this_frame->next;
2409 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2410 Normally the resume address is inside the body of the function
2411 associated with THIS_FRAME, but there is a special case: when
2412 calling a function which the compiler knows will never return
2413 (for instance abort), the call may be the very last instruction
2414 in the calling function. The resume address will point after the
2415 call and may be at the beginning of a different function
2418 If THIS_FRAME is a signal frame or dummy frame, then we should
2419 not adjust the unwound PC. For a dummy frame, GDB pushed the
2420 resume address manually onto the stack. For a signal frame, the
2421 OS may have pushed the resume address manually and invoked the
2422 handler (e.g. GNU/Linux), or invoked the trampoline which called
2423 the signal handler - but in either case the signal handler is
2424 expected to return to the trampoline. So in both of these
2425 cases we know that the resume address is executable and
2426 related. So we only need to adjust the PC if THIS_FRAME
2427 is a normal function.
2429 If the program has been interrupted while THIS_FRAME is current,
2430 then clearly the resume address is inside the associated
2431 function. There are three kinds of interruption: debugger stop
2432 (next frame will be SENTINEL_FRAME), operating system
2433 signal or exception (next frame will be SIGTRAMP_FRAME),
2434 or debugger-induced function call (next frame will be
2435 DUMMY_FRAME). So we only need to adjust the PC if
2436 NEXT_FRAME is a normal function.
2438 We check the type of NEXT_FRAME first, since it is already
2439 known; frame type is determined by the unwinder, and since
2440 we have THIS_FRAME we've already selected an unwinder for
2443 If the next frame is inlined, we need to keep going until we find
2444 the real function - for instance, if a signal handler is invoked
2445 while in an inlined function, then the code address of the
2446 "calling" normal function should not be adjusted either. */
2448 while (get_frame_type (next_frame) == INLINE_FRAME)
2449 next_frame = next_frame->next;
2451 if ((get_frame_type (next_frame) == NORMAL_FRAME
2452 || get_frame_type (next_frame) == TAILCALL_FRAME)
2453 && (get_frame_type (this_frame) == NORMAL_FRAME
2454 || get_frame_type (this_frame) == TAILCALL_FRAME
2455 || get_frame_type (this_frame) == INLINE_FRAME))
2462 get_frame_address_in_block_if_available (struct frame_info *this_frame,
2468 *pc = get_frame_address_in_block (this_frame);
2470 catch (const gdb_exception_error &ex)
2472 if (ex.error == NOT_AVAILABLE_ERROR)
2481 find_frame_sal (frame_info *frame)
2483 struct frame_info *next_frame;
2487 /* If the next frame represents an inlined function call, this frame's
2488 sal is the "call site" of that inlined function, which can not
2489 be inferred from get_frame_pc. */
2490 next_frame = get_next_frame (frame);
2491 if (frame_inlined_callees (frame) > 0)
2496 sym = get_frame_function (next_frame);
2498 sym = inline_skipped_symbol (inferior_thread ());
2500 /* If frame is inline, it certainly has symbols. */
2503 symtab_and_line sal;
2504 if (SYMBOL_LINE (sym) != 0)
2506 sal.symtab = symbol_symtab (sym);
2507 sal.line = SYMBOL_LINE (sym);
2510 /* If the symbol does not have a location, we don't know where
2511 the call site is. Do not pretend to. This is jarring, but
2512 we can't do much better. */
2513 sal.pc = get_frame_pc (frame);
2515 sal.pspace = get_frame_program_space (frame);
2519 /* If FRAME is not the innermost frame, that normally means that
2520 FRAME->pc points at the return instruction (which is *after* the
2521 call instruction), and we want to get the line containing the
2522 call (because the call is where the user thinks the program is).
2523 However, if the next frame is either a SIGTRAMP_FRAME or a
2524 DUMMY_FRAME, then the next frame will contain a saved interrupt
2525 PC and such a PC indicates the current (rather than next)
2526 instruction/line, consequently, for such cases, want to get the
2527 line containing fi->pc. */
2528 if (!get_frame_pc_if_available (frame, &pc))
2531 notcurrent = (pc != get_frame_address_in_block (frame));
2532 return find_pc_line (pc, notcurrent);
2535 /* Per "frame.h", return the ``address'' of the frame. Code should
2536 really be using get_frame_id(). */
2538 get_frame_base (struct frame_info *fi)
2540 return get_frame_id (fi).stack_addr;
2543 /* High-level offsets into the frame. Used by the debug info. */
2546 get_frame_base_address (struct frame_info *fi)
2548 if (get_frame_type (fi) != NORMAL_FRAME)
2550 if (fi->base == NULL)
2551 fi->base = frame_base_find_by_frame (fi);
2552 /* Sneaky: If the low-level unwind and high-level base code share a
2553 common unwinder, let them share the prologue cache. */
2554 if (fi->base->unwind == fi->unwind)
2555 return fi->base->this_base (fi, &fi->prologue_cache);
2556 return fi->base->this_base (fi, &fi->base_cache);
2560 get_frame_locals_address (struct frame_info *fi)
2562 if (get_frame_type (fi) != NORMAL_FRAME)
2564 /* If there isn't a frame address method, find it. */
2565 if (fi->base == NULL)
2566 fi->base = frame_base_find_by_frame (fi);
2567 /* Sneaky: If the low-level unwind and high-level base code share a
2568 common unwinder, let them share the prologue cache. */
2569 if (fi->base->unwind == fi->unwind)
2570 return fi->base->this_locals (fi, &fi->prologue_cache);
2571 return fi->base->this_locals (fi, &fi->base_cache);
2575 get_frame_args_address (struct frame_info *fi)
2577 if (get_frame_type (fi) != NORMAL_FRAME)
2579 /* If there isn't a frame address method, find it. */
2580 if (fi->base == NULL)
2581 fi->base = frame_base_find_by_frame (fi);
2582 /* Sneaky: If the low-level unwind and high-level base code share a
2583 common unwinder, let them share the prologue cache. */
2584 if (fi->base->unwind == fi->unwind)
2585 return fi->base->this_args (fi, &fi->prologue_cache);
2586 return fi->base->this_args (fi, &fi->base_cache);
2589 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2593 frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
2595 if (fi->unwind == NULL)
2596 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
2597 return fi->unwind == unwinder;
2600 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2601 or -1 for a NULL frame. */
2604 frame_relative_level (struct frame_info *fi)
2613 get_frame_type (struct frame_info *frame)
2615 if (frame->unwind == NULL)
2616 /* Initialize the frame's unwinder because that's what
2617 provides the frame's type. */
2618 frame_unwind_find_by_frame (frame, &frame->prologue_cache);
2619 return frame->unwind->type;
2622 struct program_space *
2623 get_frame_program_space (struct frame_info *frame)
2625 return frame->pspace;
2628 struct program_space *
2629 frame_unwind_program_space (struct frame_info *this_frame)
2631 gdb_assert (this_frame);
2633 /* This is really a placeholder to keep the API consistent --- we
2634 assume for now that we don't have frame chains crossing
2636 return this_frame->pspace;
2639 const address_space *
2640 get_frame_address_space (struct frame_info *frame)
2642 return frame->aspace;
2645 /* Memory access methods. */
2648 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
2649 gdb_byte *buf, int len)
2651 read_memory (addr, buf, len);
2655 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
2658 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2659 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2661 return read_memory_integer (addr, len, byte_order);
2665 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
2668 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2669 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2671 return read_memory_unsigned_integer (addr, len, byte_order);
2675 safe_frame_unwind_memory (struct frame_info *this_frame,
2676 CORE_ADDR addr, gdb_byte *buf, int len)
2678 /* NOTE: target_read_memory returns zero on success! */
2679 return !target_read_memory (addr, buf, len);
2682 /* Architecture methods. */
2685 get_frame_arch (struct frame_info *this_frame)
2687 return frame_unwind_arch (this_frame->next);
2691 frame_unwind_arch (struct frame_info *next_frame)
2693 if (!next_frame->prev_arch.p)
2695 struct gdbarch *arch;
2697 if (next_frame->unwind == NULL)
2698 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
2700 if (next_frame->unwind->prev_arch != NULL)
2701 arch = next_frame->unwind->prev_arch (next_frame,
2702 &next_frame->prologue_cache);
2704 arch = get_frame_arch (next_frame);
2706 next_frame->prev_arch.arch = arch;
2707 next_frame->prev_arch.p = 1;
2709 fprintf_unfiltered (gdb_stdlog,
2710 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2712 gdbarch_bfd_arch_info (arch)->printable_name);
2715 return next_frame->prev_arch.arch;
2719 frame_unwind_caller_arch (struct frame_info *next_frame)
2721 next_frame = skip_artificial_frames (next_frame);
2723 /* We must have a non-artificial frame. The caller is supposed to check
2724 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2726 gdb_assert (next_frame != NULL);
2728 return frame_unwind_arch (next_frame);
2731 /* Gets the language of FRAME. */
2734 get_frame_language (struct frame_info *frame)
2739 gdb_assert (frame!= NULL);
2741 /* We determine the current frame language by looking up its
2742 associated symtab. To retrieve this symtab, we use the frame
2743 PC. However we cannot use the frame PC as is, because it
2744 usually points to the instruction following the "call", which
2745 is sometimes the first instruction of another function. So
2746 we rely on get_frame_address_in_block(), it provides us with
2747 a PC that is guaranteed to be inside the frame's code
2752 pc = get_frame_address_in_block (frame);
2755 catch (const gdb_exception_error &ex)
2757 if (ex.error != NOT_AVAILABLE_ERROR)
2763 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
2766 return compunit_language (cust);
2769 return language_unknown;
2772 /* Stack pointer methods. */
2775 get_frame_sp (struct frame_info *this_frame)
2777 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2779 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2780 operate on THIS_FRAME now. */
2781 return gdbarch_unwind_sp (gdbarch, this_frame->next);
2784 /* Return the reason why we can't unwind past FRAME. */
2786 enum unwind_stop_reason
2787 get_frame_unwind_stop_reason (struct frame_info *frame)
2789 /* Fill-in STOP_REASON. */
2790 get_prev_frame_always (frame);
2791 gdb_assert (frame->prev_p);
2793 return frame->stop_reason;
2796 /* Return a string explaining REASON. */
2799 unwind_stop_reason_to_string (enum unwind_stop_reason reason)
2803 #define SET(name, description) \
2804 case name: return _(description);
2805 #include "unwind_stop_reasons.def"
2809 internal_error (__FILE__, __LINE__,
2810 "Invalid frame stop reason");
2815 frame_stop_reason_string (struct frame_info *fi)
2817 gdb_assert (fi->prev_p);
2818 gdb_assert (fi->prev == NULL);
2820 /* Return the specific string if we have one. */
2821 if (fi->stop_string != NULL)
2822 return fi->stop_string;
2824 /* Return the generic string if we have nothing better. */
2825 return unwind_stop_reason_to_string (fi->stop_reason);
2828 /* Return the enum symbol name of REASON as a string, to use in debug
2832 frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
2836 #define SET(name, description) \
2837 case name: return #name;
2838 #include "unwind_stop_reasons.def"
2842 internal_error (__FILE__, __LINE__,
2843 "Invalid frame stop reason");
2847 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2851 frame_cleanup_after_sniffer (struct frame_info *frame)
2853 /* The sniffer should not allocate a prologue cache if it did not
2854 match this frame. */
2855 gdb_assert (frame->prologue_cache == NULL);
2857 /* No sniffer should extend the frame chain; sniff based on what is
2859 gdb_assert (!frame->prev_p);
2861 /* The sniffer should not check the frame's ID; that's circular. */
2862 gdb_assert (!frame->this_id.p);
2864 /* Clear cached fields dependent on the unwinder.
2866 The previous PC is independent of the unwinder, but the previous
2867 function is not (see get_frame_address_in_block). */
2868 frame->prev_func.p = 0;
2869 frame->prev_func.addr = 0;
2871 /* Discard the unwinder last, so that we can easily find it if an assertion
2872 in this function triggers. */
2873 frame->unwind = NULL;
2876 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2877 If sniffing fails, the caller should be sure to call
2878 frame_cleanup_after_sniffer. */
2881 frame_prepare_for_sniffer (struct frame_info *frame,
2882 const struct frame_unwind *unwind)
2884 gdb_assert (frame->unwind == NULL);
2885 frame->unwind = unwind;
2888 static struct cmd_list_element *set_backtrace_cmdlist;
2889 static struct cmd_list_element *show_backtrace_cmdlist;
2892 set_backtrace_cmd (const char *args, int from_tty)
2894 help_list (set_backtrace_cmdlist, "set backtrace ", all_commands,
2899 show_backtrace_cmd (const char *args, int from_tty)
2901 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2904 /* Definition of the "set backtrace" settings that are exposed as
2905 "backtrace" command options. */
2907 using boolean_option_def
2908 = gdb::option::boolean_option_def<set_backtrace_options>;
2909 using uinteger_option_def
2910 = gdb::option::uinteger_option_def<set_backtrace_options>;
2912 const gdb::option::option_def set_backtrace_option_defs[] = {
2914 boolean_option_def {
2916 [] (set_backtrace_options *opt) { return &opt->backtrace_past_main; },
2917 show_backtrace_past_main, /* show_cmd_cb */
2918 N_("Set whether backtraces should continue past \"main\"."),
2919 N_("Show whether backtraces should continue past \"main\"."),
2920 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2921 the backtrace at \"main\". Set this if you need to see the rest\n\
2922 of the stack trace."),
2925 boolean_option_def {
2927 [] (set_backtrace_options *opt) { return &opt->backtrace_past_entry; },
2928 show_backtrace_past_entry, /* show_cmd_cb */
2929 N_("Set whether backtraces should continue past the entry point of a program."),
2930 N_("Show whether backtraces should continue past the entry point of a program."),
2931 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
2932 will terminate the backtrace there. Set this if you need to see\n\
2933 the rest of the stack trace."),
2938 _initialize_frame (void)
2940 obstack_init (&frame_cache_obstack);
2942 frame_stash_create ();
2944 gdb::observers::target_changed.attach (frame_observer_target_changed);
2946 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
2947 Set backtrace specific variables.\n\
2948 Configure backtrace variables such as the backtrace limit"),
2949 &set_backtrace_cmdlist, "set backtrace ",
2950 0/*allow-unknown*/, &setlist);
2951 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
2952 Show backtrace specific variables\n\
2953 Show backtrace variables such as the backtrace limit"),
2954 &show_backtrace_cmdlist, "show backtrace ",
2955 0/*allow-unknown*/, &showlist);
2957 add_setshow_uinteger_cmd ("limit", class_obscure,
2958 &user_set_backtrace_options.backtrace_limit, _("\
2959 Set an upper bound on the number of backtrace levels."), _("\
2960 Show the upper bound on the number of backtrace levels."), _("\
2961 No more than the specified number of frames can be displayed or examined.\n\
2962 Literal \"unlimited\" or zero means no limit."),
2964 show_backtrace_limit,
2965 &set_backtrace_cmdlist,
2966 &show_backtrace_cmdlist);
2968 gdb::option::add_setshow_cmds_for_options
2969 (class_stack, &user_set_backtrace_options,
2970 set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist);
2972 /* Debug this files internals. */
2973 add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
2974 Set frame debugging."), _("\
2975 Show frame debugging."), _("\
2976 When non-zero, frame specific internal debugging is enabled."),
2979 &setdebuglist, &showdebuglist);