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;
126 /* Did VALUE require unmasking when being read. */
131 /* Cached copy of the previous frame's function address. */
138 /* This frame's ID. */
142 struct frame_id value;
145 /* The frame's high-level base methods, and corresponding cache.
146 The high level base methods are selected based on the frame's
148 const struct frame_base *base;
151 /* Pointers to the next (down, inner, younger) and previous (up,
152 outer, older) frame_info's in the frame cache. */
153 struct frame_info *next; /* down, inner, younger */
155 struct frame_info *prev; /* up, outer, older */
157 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
158 could. Only valid when PREV_P is set. */
159 enum unwind_stop_reason stop_reason;
161 /* A frame specific string describing the STOP_REASON in more detail.
162 Only valid when PREV_P is set, but even then may still be NULL. */
163 const char *stop_string;
169 set_frame_previous_pc_masked (struct frame_info *frame)
171 frame->prev_pc.masked = true;
177 get_frame_pc_masked (const struct frame_info *frame)
179 gdb_assert (frame->next != nullptr);
180 gdb_assert (frame->next->prev_pc.status == CC_VALUE);
182 return frame->next->prev_pc.masked;
185 /* A frame stash used to speed up frame lookups. Create a hash table
186 to stash frames previously accessed from the frame cache for
187 quicker subsequent retrieval. The hash table is emptied whenever
188 the frame cache is invalidated. */
190 static htab_t frame_stash;
192 /* Internal function to calculate a hash from the frame_id addresses,
193 using as many valid addresses as possible. Frames below level 0
194 are not stored in the hash table. */
197 frame_addr_hash (const void *ap)
199 const struct frame_info *frame = (const struct frame_info *) ap;
200 const struct frame_id f_id = frame->this_id.value;
203 gdb_assert (f_id.stack_status != FID_STACK_INVALID
205 || f_id.special_addr_p);
207 if (f_id.stack_status == FID_STACK_VALID)
208 hash = iterative_hash (&f_id.stack_addr,
209 sizeof (f_id.stack_addr), hash);
210 if (f_id.code_addr_p)
211 hash = iterative_hash (&f_id.code_addr,
212 sizeof (f_id.code_addr), hash);
213 if (f_id.special_addr_p)
214 hash = iterative_hash (&f_id.special_addr,
215 sizeof (f_id.special_addr), hash);
220 /* Internal equality function for the hash table. This function
221 defers equality operations to frame_id_eq. */
224 frame_addr_hash_eq (const void *a, const void *b)
226 const struct frame_info *f_entry = (const struct frame_info *) a;
227 const struct frame_info *f_element = (const struct frame_info *) b;
229 return frame_id_eq (f_entry->this_id.value,
230 f_element->this_id.value);
233 /* Internal function to create the frame_stash hash table. 100 seems
234 to be a good compromise to start the hash table at. */
237 frame_stash_create (void)
239 frame_stash = htab_create (100,
245 /* Internal function to add a frame to the frame_stash hash table.
246 Returns false if a frame with the same ID was already stashed, true
250 frame_stash_add (struct frame_info *frame)
252 struct frame_info **slot;
254 /* Do not try to stash the sentinel frame. */
255 gdb_assert (frame->level >= 0);
257 slot = (struct frame_info **) htab_find_slot (frame_stash,
261 /* If we already have a frame in the stack with the same id, we
262 either have a stack cycle (corrupted stack?), or some bug
263 elsewhere in GDB. In any case, ignore the duplicate and return
264 an indication to the caller. */
272 /* Internal function to search the frame stash for an entry with the
273 given frame ID. If found, return that frame. Otherwise return
276 static struct frame_info *
277 frame_stash_find (struct frame_id id)
279 struct frame_info dummy;
280 struct frame_info *frame;
282 dummy.this_id.value = id;
283 frame = (struct frame_info *) htab_find (frame_stash, &dummy);
287 /* Internal function to invalidate the frame stash by removing all
288 entries in it. This only occurs when the frame cache is
292 frame_stash_invalidate (void)
294 htab_empty (frame_stash);
298 scoped_restore_selected_frame::scoped_restore_selected_frame ()
300 m_fid = get_frame_id (get_selected_frame (NULL));
304 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
306 frame_info *frame = frame_find_by_id (m_fid);
308 warning (_("Unable to restore previously selected frame."));
310 select_frame (frame);
313 /* Flag to control debugging. */
315 unsigned int frame_debug;
317 show_frame_debug (struct ui_file *file, int from_tty,
318 struct cmd_list_element *c, const char *value)
320 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
323 /* Implementation of "show backtrace past-main". */
326 show_backtrace_past_main (struct ui_file *file, int from_tty,
327 struct cmd_list_element *c, const char *value)
329 fprintf_filtered (file,
330 _("Whether backtraces should "
331 "continue past \"main\" is %s.\n"),
335 /* Implementation of "show backtrace past-entry". */
338 show_backtrace_past_entry (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c, const char *value)
341 fprintf_filtered (file, _("Whether backtraces should continue past the "
342 "entry point of a program is %s.\n"),
346 /* Implementation of "show backtrace limit". */
349 show_backtrace_limit (struct ui_file *file, int from_tty,
350 struct cmd_list_element *c, const char *value)
352 fprintf_filtered (file,
353 _("An upper bound on the number "
354 "of backtrace levels is %s.\n"),
360 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
363 fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
365 fprintf_unfiltered (file, "!%s", name);
369 fprint_frame_id (struct ui_file *file, struct frame_id id)
371 fprintf_unfiltered (file, "{");
373 if (id.stack_status == FID_STACK_INVALID)
374 fprintf_unfiltered (file, "!stack");
375 else if (id.stack_status == FID_STACK_UNAVAILABLE)
376 fprintf_unfiltered (file, "stack=<unavailable>");
377 else if (id.stack_status == FID_STACK_SENTINEL)
378 fprintf_unfiltered (file, "stack=<sentinel>");
380 fprintf_unfiltered (file, "stack=%s", hex_string (id.stack_addr));
381 fprintf_unfiltered (file, ",");
383 fprint_field (file, "code", id.code_addr_p, id.code_addr);
384 fprintf_unfiltered (file, ",");
386 fprint_field (file, "special", id.special_addr_p, id.special_addr);
388 if (id.artificial_depth)
389 fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
391 fprintf_unfiltered (file, "}");
395 fprint_frame_type (struct ui_file *file, enum frame_type type)
400 fprintf_unfiltered (file, "NORMAL_FRAME");
403 fprintf_unfiltered (file, "DUMMY_FRAME");
406 fprintf_unfiltered (file, "INLINE_FRAME");
409 fprintf_unfiltered (file, "TAILCALL_FRAME");
412 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
415 fprintf_unfiltered (file, "ARCH_FRAME");
418 fprintf_unfiltered (file, "SENTINEL_FRAME");
421 fprintf_unfiltered (file, "<unknown type>");
427 fprint_frame (struct ui_file *file, struct frame_info *fi)
431 fprintf_unfiltered (file, "<NULL frame>");
434 fprintf_unfiltered (file, "{");
435 fprintf_unfiltered (file, "level=%d", fi->level);
436 fprintf_unfiltered (file, ",");
437 fprintf_unfiltered (file, "type=");
438 if (fi->unwind != NULL)
439 fprint_frame_type (file, fi->unwind->type);
441 fprintf_unfiltered (file, "<unknown>");
442 fprintf_unfiltered (file, ",");
443 fprintf_unfiltered (file, "unwind=");
444 if (fi->unwind != NULL)
445 gdb_print_host_address (fi->unwind, file);
447 fprintf_unfiltered (file, "<unknown>");
448 fprintf_unfiltered (file, ",");
449 fprintf_unfiltered (file, "pc=");
450 if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
451 fprintf_unfiltered (file, "<unknown>");
452 else if (fi->next->prev_pc.status == CC_VALUE)
454 fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_pc.value));
455 if (fi->next->prev_pc.masked)
456 fprintf_unfiltered (file, "[PAC]");
458 else if (fi->next->prev_pc.status == CC_NOT_SAVED)
459 val_print_not_saved (file);
460 else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
461 val_print_unavailable (file);
462 fprintf_unfiltered (file, ",");
463 fprintf_unfiltered (file, "id=");
465 fprint_frame_id (file, fi->this_id.value);
467 fprintf_unfiltered (file, "<unknown>");
468 fprintf_unfiltered (file, ",");
469 fprintf_unfiltered (file, "func=");
470 if (fi->next != NULL && fi->next->prev_func.p)
471 fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
473 fprintf_unfiltered (file, "<unknown>");
474 fprintf_unfiltered (file, "}");
477 /* Given FRAME, return the enclosing frame as found in real frames read-in from
478 inferior memory. Skip any previous frames which were made up by GDB.
479 Return FRAME if FRAME is a non-artificial frame.
480 Return NULL if FRAME is the start of an artificial-only chain. */
482 static struct frame_info *
483 skip_artificial_frames (struct frame_info *frame)
485 /* Note we use get_prev_frame_always, and not get_prev_frame. The
486 latter will truncate the frame chain, leading to this function
487 unintentionally returning a null_frame_id (e.g., when the user
488 sets a backtrace limit).
490 Note that for record targets we may get a frame chain that consists
491 of artificial frames only. */
492 while (get_frame_type (frame) == INLINE_FRAME
493 || get_frame_type (frame) == TAILCALL_FRAME)
495 frame = get_prev_frame_always (frame);
504 skip_unwritable_frames (struct frame_info *frame)
506 while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0)
508 frame = get_prev_frame (frame);
519 skip_tailcall_frames (struct frame_info *frame)
521 while (get_frame_type (frame) == TAILCALL_FRAME)
523 /* Note that for record targets we may get a frame chain that consists of
524 tailcall frames only. */
525 frame = get_prev_frame (frame);
533 /* Compute the frame's uniq ID that can be used to, later, re-find the
537 compute_frame_id (struct frame_info *fi)
539 gdb_assert (!fi->this_id.p);
542 fprintf_unfiltered (gdb_stdlog, "{ compute_frame_id (fi=%d) ",
544 /* Find the unwinder. */
545 if (fi->unwind == NULL)
546 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
547 /* Find THIS frame's ID. */
548 /* Default to outermost if no ID is found. */
549 fi->this_id.value = outer_frame_id;
550 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
551 gdb_assert (frame_id_p (fi->this_id.value));
555 fprintf_unfiltered (gdb_stdlog, "-> ");
556 fprint_frame_id (gdb_stdlog, fi->this_id.value);
557 fprintf_unfiltered (gdb_stdlog, " }\n");
561 /* Return a frame uniq ID that can be used to, later, re-find the
565 get_frame_id (struct frame_info *fi)
568 return null_frame_id;
574 /* If we haven't computed the frame id yet, then it must be that
575 this is the current frame. Compute it now, and stash the
576 result. The IDs of other frames are computed as soon as
577 they're created, in order to detect cycles. See
578 get_prev_frame_if_no_cycle. */
579 gdb_assert (fi->level == 0);
582 compute_frame_id (fi);
584 /* Since this is the first frame in the chain, this should
586 stashed = frame_stash_add (fi);
587 gdb_assert (stashed);
590 return fi->this_id.value;
594 get_stack_frame_id (struct frame_info *next_frame)
596 return get_frame_id (skip_artificial_frames (next_frame));
600 frame_unwind_caller_id (struct frame_info *next_frame)
602 struct frame_info *this_frame;
604 /* Use get_prev_frame_always, and not get_prev_frame. The latter
605 will truncate the frame chain, leading to this function
606 unintentionally returning a null_frame_id (e.g., when a caller
607 requests the frame ID of "main()"s caller. */
609 next_frame = skip_artificial_frames (next_frame);
610 if (next_frame == NULL)
611 return null_frame_id;
613 this_frame = get_prev_frame_always (next_frame);
615 return get_frame_id (skip_artificial_frames (this_frame));
617 return null_frame_id;
620 const struct frame_id null_frame_id = { 0 }; /* All zeros. */
621 const struct frame_id sentinel_frame_id = { 0, 0, 0, FID_STACK_SENTINEL, 0, 1, 0 };
622 const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_INVALID, 0, 1, 0 };
625 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
626 CORE_ADDR special_addr)
628 struct frame_id id = null_frame_id;
630 id.stack_addr = stack_addr;
631 id.stack_status = FID_STACK_VALID;
632 id.code_addr = code_addr;
634 id.special_addr = special_addr;
635 id.special_addr_p = 1;
642 frame_id_build_unavailable_stack (CORE_ADDR code_addr)
644 struct frame_id id = null_frame_id;
646 id.stack_status = FID_STACK_UNAVAILABLE;
647 id.code_addr = code_addr;
655 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
656 CORE_ADDR special_addr)
658 struct frame_id id = null_frame_id;
660 id.stack_status = FID_STACK_UNAVAILABLE;
661 id.code_addr = code_addr;
663 id.special_addr = special_addr;
664 id.special_addr_p = 1;
669 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
671 struct frame_id id = null_frame_id;
673 id.stack_addr = stack_addr;
674 id.stack_status = FID_STACK_VALID;
675 id.code_addr = code_addr;
681 frame_id_build_wild (CORE_ADDR stack_addr)
683 struct frame_id id = null_frame_id;
685 id.stack_addr = stack_addr;
686 id.stack_status = FID_STACK_VALID;
691 frame_id_p (struct frame_id l)
695 /* The frame is valid iff it has a valid stack address. */
696 p = l.stack_status != FID_STACK_INVALID;
697 /* outer_frame_id is also valid. */
698 if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
702 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
703 fprint_frame_id (gdb_stdlog, l);
704 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
710 frame_id_artificial_p (struct frame_id l)
715 return (l.artificial_depth != 0);
719 frame_id_eq (struct frame_id l, struct frame_id r)
723 if (l.stack_status == FID_STACK_INVALID && l.special_addr_p
724 && r.stack_status == FID_STACK_INVALID && r.special_addr_p)
725 /* The outermost frame marker is equal to itself. This is the
726 dodgy thing about outer_frame_id, since between execution steps
727 we might step into another function - from which we can't
728 unwind either. More thought required to get rid of
731 else if (l.stack_status == FID_STACK_INVALID
732 || r.stack_status == FID_STACK_INVALID)
733 /* Like a NaN, if either ID is invalid, the result is false.
734 Note that a frame ID is invalid iff it is the null frame ID. */
736 else if (l.stack_status != r.stack_status || l.stack_addr != r.stack_addr)
737 /* If .stack addresses are different, the frames are different. */
739 else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
740 /* An invalid code addr is a wild card. If .code addresses are
741 different, the frames are different. */
743 else if (l.special_addr_p && r.special_addr_p
744 && l.special_addr != r.special_addr)
745 /* An invalid special addr is a wild card (or unused). Otherwise
746 if special addresses are different, the frames are different. */
748 else if (l.artificial_depth != r.artificial_depth)
749 /* If artifical depths are different, the frames must be different. */
752 /* Frames are equal. */
757 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
758 fprint_frame_id (gdb_stdlog, l);
759 fprintf_unfiltered (gdb_stdlog, ",r=");
760 fprint_frame_id (gdb_stdlog, r);
761 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
766 /* Safety net to check whether frame ID L should be inner to
767 frame ID R, according to their stack addresses.
769 This method cannot be used to compare arbitrary frames, as the
770 ranges of valid stack addresses may be discontiguous (e.g. due
773 However, it can be used as safety net to discover invalid frame
774 IDs in certain circumstances. Assuming that NEXT is the immediate
775 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
777 * The stack address of NEXT must be inner-than-or-equal to the stack
780 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
783 * If NEXT and THIS have different stack addresses, no other frame
784 in the frame chain may have a stack address in between.
786 Therefore, if frame_id_inner (TEST, THIS) holds, but
787 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
788 to a valid frame in the frame chain.
790 The sanity checks above cannot be performed when a SIGTRAMP frame
791 is involved, because signal handlers might be executed on a different
792 stack than the stack used by the routine that caused the signal
793 to be raised. This can happen for instance when a thread exceeds
794 its maximum stack size. In this case, certain compilers implement
795 a stack overflow strategy that cause the handler to be run on a
799 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
803 if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
804 /* Like NaN, any operation involving an invalid ID always fails.
805 Likewise if either ID has an unavailable stack address. */
807 else if (l.artificial_depth > r.artificial_depth
808 && l.stack_addr == r.stack_addr
809 && l.code_addr_p == r.code_addr_p
810 && l.special_addr_p == r.special_addr_p
811 && l.special_addr == r.special_addr)
813 /* Same function, different inlined functions. */
814 const struct block *lb, *rb;
816 gdb_assert (l.code_addr_p && r.code_addr_p);
818 lb = block_for_pc (l.code_addr);
819 rb = block_for_pc (r.code_addr);
821 if (lb == NULL || rb == NULL)
822 /* Something's gone wrong. */
825 /* This will return true if LB and RB are the same block, or
826 if the block with the smaller depth lexically encloses the
827 block with the greater depth. */
828 inner = contained_in (lb, rb);
831 /* Only return non-zero when strictly inner than. Note that, per
832 comment in "frame.h", there is some fuzz here. Frameless
833 functions are not strictly inner than (same .stack but
834 different .code and/or .special address). */
835 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
838 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
839 fprint_frame_id (gdb_stdlog, l);
840 fprintf_unfiltered (gdb_stdlog, ",r=");
841 fprint_frame_id (gdb_stdlog, r);
842 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
848 frame_find_by_id (struct frame_id id)
850 struct frame_info *frame, *prev_frame;
852 /* ZERO denotes the null frame, let the caller decide what to do
853 about it. Should it instead return get_current_frame()? */
854 if (!frame_id_p (id))
857 /* Check for the sentinel frame. */
858 if (frame_id_eq (id, sentinel_frame_id))
859 return sentinel_frame;
861 /* Try using the frame stash first. Finding it there removes the need
862 to perform the search by looping over all frames, which can be very
863 CPU-intensive if the number of frames is very high (the loop is O(n)
864 and get_prev_frame performs a series of checks that are relatively
865 expensive). This optimization is particularly useful when this function
866 is called from another function (such as value_fetch_lazy, case
867 VALUE_LVAL (val) == lval_register) which already loops over all frames,
868 making the overall behavior O(n^2). */
869 frame = frame_stash_find (id);
873 for (frame = get_current_frame (); ; frame = prev_frame)
875 struct frame_id self = get_frame_id (frame);
877 if (frame_id_eq (id, self))
878 /* An exact match. */
881 prev_frame = get_prev_frame (frame);
885 /* As a safety net to avoid unnecessary backtracing while trying
886 to find an invalid ID, we check for a common situation where
887 we can detect from comparing stack addresses that no other
888 frame in the current frame chain can have this ID. See the
889 comment at frame_id_inner for details. */
890 if (get_frame_type (frame) == NORMAL_FRAME
891 && !frame_id_inner (get_frame_arch (frame), id, self)
892 && frame_id_inner (get_frame_arch (prev_frame), id,
893 get_frame_id (prev_frame)))
900 frame_unwind_pc (struct frame_info *this_frame)
902 if (this_frame->prev_pc.status == CC_UNKNOWN)
904 struct gdbarch *prev_gdbarch;
908 /* The right way. The `pure' way. The one true way. This
909 method depends solely on the register-unwind code to
910 determine the value of registers in THIS frame, and hence
911 the value of this frame's PC (resume address). A typical
912 implementation is no more than:
914 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
915 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
917 Note: this method is very heavily dependent on a correct
918 register-unwind implementation, it pays to fix that
919 method first; this method is frame type agnostic, since
920 it only deals with register values, it works with any
921 frame. This is all in stark contrast to the old
922 FRAME_SAVED_PC which would try to directly handle all the
923 different ways that a PC could be unwound. */
924 prev_gdbarch = frame_unwind_arch (this_frame);
928 pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
931 catch (const gdb_exception_error &ex)
933 if (ex.error == NOT_AVAILABLE_ERROR)
935 this_frame->prev_pc.status = CC_UNAVAILABLE;
938 fprintf_unfiltered (gdb_stdlog,
939 "{ frame_unwind_pc (this_frame=%d)"
940 " -> <unavailable> }\n",
943 else if (ex.error == OPTIMIZED_OUT_ERROR)
945 this_frame->prev_pc.status = CC_NOT_SAVED;
948 fprintf_unfiltered (gdb_stdlog,
949 "{ frame_unwind_pc (this_frame=%d)"
950 " -> <not saved> }\n",
959 this_frame->prev_pc.value = pc;
960 this_frame->prev_pc.status = CC_VALUE;
962 fprintf_unfiltered (gdb_stdlog,
963 "{ frame_unwind_pc (this_frame=%d) "
966 hex_string (this_frame->prev_pc.value));
970 if (this_frame->prev_pc.status == CC_VALUE)
971 return this_frame->prev_pc.value;
972 else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
973 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
974 else if (this_frame->prev_pc.status == CC_NOT_SAVED)
975 throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
977 internal_error (__FILE__, __LINE__,
978 "unexpected prev_pc status: %d",
979 (int) this_frame->prev_pc.status);
983 frame_unwind_caller_pc (struct frame_info *this_frame)
985 this_frame = skip_artificial_frames (this_frame);
987 /* We must have a non-artificial frame. The caller is supposed to check
988 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
990 gdb_assert (this_frame != NULL);
992 return frame_unwind_pc (this_frame);
996 get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
998 struct frame_info *next_frame = this_frame->next;
1000 if (!next_frame->prev_func.p)
1002 CORE_ADDR addr_in_block;
1004 /* Make certain that this, and not the adjacent, function is
1006 if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
1008 next_frame->prev_func.p = -1;
1010 fprintf_unfiltered (gdb_stdlog,
1011 "{ get_frame_func (this_frame=%d)"
1012 " -> unavailable }\n",
1017 next_frame->prev_func.p = 1;
1018 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
1020 fprintf_unfiltered (gdb_stdlog,
1021 "{ get_frame_func (this_frame=%d) -> %s }\n",
1023 hex_string (next_frame->prev_func.addr));
1027 if (next_frame->prev_func.p < 0)
1034 *pc = next_frame->prev_func.addr;
1040 get_frame_func (struct frame_info *this_frame)
1044 if (!get_frame_func_if_available (this_frame, &pc))
1045 throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
1050 std::unique_ptr<readonly_detached_regcache>
1051 frame_save_as_regcache (struct frame_info *this_frame)
1053 auto cooked_read = [this_frame] (int regnum, gdb_byte *buf)
1055 if (!deprecated_frame_register_read (this_frame, regnum, buf))
1056 return REG_UNAVAILABLE;
1061 std::unique_ptr<readonly_detached_regcache> regcache
1062 (new readonly_detached_regcache (get_frame_arch (this_frame), cooked_read));
1068 frame_pop (struct frame_info *this_frame)
1070 struct frame_info *prev_frame;
1072 if (get_frame_type (this_frame) == DUMMY_FRAME)
1074 /* Popping a dummy frame involves restoring more than just registers.
1075 dummy_frame_pop does all the work. */
1076 dummy_frame_pop (get_frame_id (this_frame), inferior_thread ());
1080 /* Ensure that we have a frame to pop to. */
1081 prev_frame = get_prev_frame_always (this_frame);
1084 error (_("Cannot pop the initial frame."));
1086 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1087 entering THISFRAME. */
1088 prev_frame = skip_tailcall_frames (prev_frame);
1090 if (prev_frame == NULL)
1091 error (_("Cannot find the caller frame."));
1093 /* Make a copy of all the register values unwound from this frame.
1094 Save them in a scratch buffer so that there isn't a race between
1095 trying to extract the old values from the current regcache while
1096 at the same time writing new values into that same cache. */
1097 std::unique_ptr<readonly_detached_regcache> scratch
1098 = frame_save_as_regcache (prev_frame);
1100 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1101 target's register cache that it is about to be hit with a burst
1102 register transfer and that the sequence of register writes should
1103 be batched. The pair target_prepare_to_store() and
1104 target_store_registers() kind of suggest this functionality.
1105 Unfortunately, they don't implement it. Their lack of a formal
1106 definition can lead to targets writing back bogus values
1107 (arguably a bug in the target code mind). */
1108 /* Now copy those saved registers into the current regcache. */
1109 get_current_regcache ()->restore (scratch.get ());
1111 /* We've made right mess of GDB's local state, just discard
1113 reinit_frame_cache ();
1117 frame_register_unwind (frame_info *next_frame, int regnum,
1118 int *optimizedp, int *unavailablep,
1119 enum lval_type *lvalp, CORE_ADDR *addrp,
1120 int *realnump, gdb_byte *bufferp)
1122 struct value *value;
1124 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1125 that the value proper does not need to be fetched. */
1126 gdb_assert (optimizedp != NULL);
1127 gdb_assert (lvalp != NULL);
1128 gdb_assert (addrp != NULL);
1129 gdb_assert (realnump != NULL);
1130 /* gdb_assert (bufferp != NULL); */
1132 value = frame_unwind_register_value (next_frame, regnum);
1134 gdb_assert (value != NULL);
1136 *optimizedp = value_optimized_out (value);
1137 *unavailablep = !value_entirely_available (value);
1138 *lvalp = VALUE_LVAL (value);
1139 *addrp = value_address (value);
1140 if (*lvalp == lval_register)
1141 *realnump = VALUE_REGNUM (value);
1147 if (!*optimizedp && !*unavailablep)
1148 memcpy (bufferp, value_contents_all (value),
1149 TYPE_LENGTH (value_type (value)));
1151 memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
1154 /* Dispose of the new value. This prevents watchpoints from
1155 trying to watch the saved frame pointer. */
1156 release_value (value);
1160 frame_register (struct frame_info *frame, int regnum,
1161 int *optimizedp, int *unavailablep, enum lval_type *lvalp,
1162 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
1164 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1165 that the value proper does not need to be fetched. */
1166 gdb_assert (optimizedp != NULL);
1167 gdb_assert (lvalp != NULL);
1168 gdb_assert (addrp != NULL);
1169 gdb_assert (realnump != NULL);
1170 /* gdb_assert (bufferp != NULL); */
1172 /* Obtain the register value by unwinding the register from the next
1173 (more inner frame). */
1174 gdb_assert (frame != NULL && frame->next != NULL);
1175 frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
1176 lvalp, addrp, realnump, bufferp);
1180 frame_unwind_register (frame_info *next_frame, int regnum, gdb_byte *buf)
1186 enum lval_type lval;
1188 frame_register_unwind (next_frame, regnum, &optimized, &unavailable,
1189 &lval, &addr, &realnum, buf);
1192 throw_error (OPTIMIZED_OUT_ERROR,
1193 _("Register %d was not saved"), regnum);
1195 throw_error (NOT_AVAILABLE_ERROR,
1196 _("Register %d is not available"), regnum);
1200 get_frame_register (struct frame_info *frame,
1201 int regnum, gdb_byte *buf)
1203 frame_unwind_register (frame->next, regnum, buf);
1207 frame_unwind_register_value (frame_info *next_frame, int regnum)
1209 struct gdbarch *gdbarch;
1210 struct value *value;
1212 gdb_assert (next_frame != NULL);
1213 gdbarch = frame_unwind_arch (next_frame);
1217 fprintf_unfiltered (gdb_stdlog,
1218 "{ frame_unwind_register_value "
1219 "(frame=%d,regnum=%d(%s),...) ",
1220 next_frame->level, regnum,
1221 user_reg_map_regnum_to_name (gdbarch, regnum));
1224 /* Find the unwinder. */
1225 if (next_frame->unwind == NULL)
1226 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
1228 /* Ask this frame to unwind its register. */
1229 value = next_frame->unwind->prev_register (next_frame,
1230 &next_frame->prologue_cache,
1235 fprintf_unfiltered (gdb_stdlog, "->");
1236 if (value_optimized_out (value))
1238 fprintf_unfiltered (gdb_stdlog, " ");
1239 val_print_optimized_out (value, gdb_stdlog);
1243 if (VALUE_LVAL (value) == lval_register)
1244 fprintf_unfiltered (gdb_stdlog, " register=%d",
1245 VALUE_REGNUM (value));
1246 else if (VALUE_LVAL (value) == lval_memory)
1247 fprintf_unfiltered (gdb_stdlog, " address=%s",
1249 value_address (value)));
1251 fprintf_unfiltered (gdb_stdlog, " computed");
1253 if (value_lazy (value))
1254 fprintf_unfiltered (gdb_stdlog, " lazy");
1258 const gdb_byte *buf = value_contents (value);
1260 fprintf_unfiltered (gdb_stdlog, " bytes=");
1261 fprintf_unfiltered (gdb_stdlog, "[");
1262 for (i = 0; i < register_size (gdbarch, regnum); i++)
1263 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1264 fprintf_unfiltered (gdb_stdlog, "]");
1268 fprintf_unfiltered (gdb_stdlog, " }\n");
1275 get_frame_register_value (struct frame_info *frame, int regnum)
1277 return frame_unwind_register_value (frame->next, regnum);
1281 frame_unwind_register_signed (frame_info *next_frame, int regnum)
1283 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1284 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1285 int size = register_size (gdbarch, regnum);
1286 struct value *value = frame_unwind_register_value (next_frame, regnum);
1288 gdb_assert (value != NULL);
1290 if (value_optimized_out (value))
1292 throw_error (OPTIMIZED_OUT_ERROR,
1293 _("Register %d was not saved"), regnum);
1295 if (!value_entirely_available (value))
1297 throw_error (NOT_AVAILABLE_ERROR,
1298 _("Register %d is not available"), regnum);
1301 LONGEST r = extract_signed_integer (value_contents_all (value), size,
1304 release_value (value);
1309 get_frame_register_signed (struct frame_info *frame, int regnum)
1311 return frame_unwind_register_signed (frame->next, regnum);
1315 frame_unwind_register_unsigned (frame_info *next_frame, int regnum)
1317 struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
1318 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1319 int size = register_size (gdbarch, regnum);
1320 struct value *value = frame_unwind_register_value (next_frame, regnum);
1322 gdb_assert (value != NULL);
1324 if (value_optimized_out (value))
1326 throw_error (OPTIMIZED_OUT_ERROR,
1327 _("Register %d was not saved"), regnum);
1329 if (!value_entirely_available (value))
1331 throw_error (NOT_AVAILABLE_ERROR,
1332 _("Register %d is not available"), regnum);
1335 ULONGEST r = extract_unsigned_integer (value_contents_all (value), size,
1338 release_value (value);
1343 get_frame_register_unsigned (struct frame_info *frame, int regnum)
1345 return frame_unwind_register_unsigned (frame->next, regnum);
1349 read_frame_register_unsigned (struct frame_info *frame, int regnum,
1352 struct value *regval = get_frame_register_value (frame, regnum);
1354 if (!value_optimized_out (regval)
1355 && value_entirely_available (regval))
1357 struct gdbarch *gdbarch = get_frame_arch (frame);
1358 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1359 int size = register_size (gdbarch, VALUE_REGNUM (regval));
1361 *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
1369 put_frame_register (struct frame_info *frame, int regnum,
1370 const gdb_byte *buf)
1372 struct gdbarch *gdbarch = get_frame_arch (frame);
1376 enum lval_type lval;
1379 frame_register (frame, regnum, &optim, &unavail,
1380 &lval, &addr, &realnum, NULL);
1382 error (_("Attempt to assign to a register that was not saved."));
1387 write_memory (addr, buf, register_size (gdbarch, regnum));
1391 get_current_regcache ()->cooked_write (realnum, buf);
1394 error (_("Attempt to assign to an unmodifiable value."));
1398 /* This function is deprecated. Use get_frame_register_value instead,
1399 which provides more accurate information.
1401 Find and return the value of REGNUM for the specified stack frame.
1402 The number of bytes copied is REGISTER_SIZE (REGNUM).
1404 Returns 0 if the register value could not be found. */
1407 deprecated_frame_register_read (struct frame_info *frame, int regnum,
1412 enum lval_type lval;
1416 frame_register (frame, regnum, &optimized, &unavailable,
1417 &lval, &addr, &realnum, myaddr);
1419 return !optimized && !unavailable;
1423 get_frame_register_bytes (struct frame_info *frame, int regnum,
1424 CORE_ADDR offset, int len, gdb_byte *myaddr,
1425 int *optimizedp, int *unavailablep)
1427 struct gdbarch *gdbarch = get_frame_arch (frame);
1432 /* Skip registers wholly inside of OFFSET. */
1433 while (offset >= register_size (gdbarch, regnum))
1435 offset -= register_size (gdbarch, regnum);
1439 /* Ensure that we will not read beyond the end of the register file.
1440 This can only ever happen if the debug information is bad. */
1442 numregs = gdbarch_num_cooked_regs (gdbarch);
1443 for (i = regnum; i < numregs; i++)
1445 int thissize = register_size (gdbarch, i);
1448 break; /* This register is not available on this architecture. */
1449 maxsize += thissize;
1452 error (_("Bad debug information detected: "
1453 "Attempt to read %d bytes from registers."), len);
1455 /* Copy the data. */
1458 int curr_len = register_size (gdbarch, regnum) - offset;
1463 if (curr_len == register_size (gdbarch, regnum))
1465 enum lval_type lval;
1469 frame_register (frame, regnum, optimizedp, unavailablep,
1470 &lval, &addr, &realnum, myaddr);
1471 if (*optimizedp || *unavailablep)
1476 struct value *value = frame_unwind_register_value (frame->next,
1478 gdb_assert (value != NULL);
1479 *optimizedp = value_optimized_out (value);
1480 *unavailablep = !value_entirely_available (value);
1482 if (*optimizedp || *unavailablep)
1484 release_value (value);
1487 memcpy (myaddr, value_contents_all (value) + offset, curr_len);
1488 release_value (value);
1503 put_frame_register_bytes (struct frame_info *frame, int regnum,
1504 CORE_ADDR offset, int len, const gdb_byte *myaddr)
1506 struct gdbarch *gdbarch = get_frame_arch (frame);
1508 /* Skip registers wholly inside of OFFSET. */
1509 while (offset >= register_size (gdbarch, regnum))
1511 offset -= register_size (gdbarch, regnum);
1515 /* Copy the data. */
1518 int curr_len = register_size (gdbarch, regnum) - offset;
1523 if (curr_len == register_size (gdbarch, regnum))
1525 put_frame_register (frame, regnum, myaddr);
1529 struct value *value = frame_unwind_register_value (frame->next,
1531 gdb_assert (value != NULL);
1533 memcpy ((char *) value_contents_writeable (value) + offset, myaddr,
1535 put_frame_register (frame, regnum, value_contents_raw (value));
1536 release_value (value);
1546 /* Create a sentinel frame. */
1548 static struct frame_info *
1549 create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
1551 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1554 frame->pspace = pspace;
1555 frame->aspace = regcache->aspace ();
1556 /* Explicitly initialize the sentinel frame's cache. Provide it
1557 with the underlying regcache. In the future additional
1558 information, such as the frame's thread will be added. */
1559 frame->prologue_cache = sentinel_frame_cache (regcache);
1560 /* For the moment there is only one sentinel frame implementation. */
1561 frame->unwind = &sentinel_frame_unwind;
1562 /* Link this frame back to itself. The frame is self referential
1563 (the unwound PC is the same as the pc), so make it so. */
1564 frame->next = frame;
1565 /* The sentinel frame has a special ID. */
1566 frame->this_id.p = 1;
1567 frame->this_id.value = sentinel_frame_id;
1570 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1571 fprint_frame (gdb_stdlog, frame);
1572 fprintf_unfiltered (gdb_stdlog, " }\n");
1577 /* Cache for frame addresses already read by gdb. Valid only while
1578 inferior is stopped. Control variables for the frame cache should
1579 be local to this module. */
1581 static struct obstack frame_cache_obstack;
1584 frame_obstack_zalloc (unsigned long size)
1586 void *data = obstack_alloc (&frame_cache_obstack, size);
1588 memset (data, 0, size);
1592 static struct frame_info *get_prev_frame_always_1 (struct frame_info *this_frame);
1595 get_current_frame (void)
1597 struct frame_info *current_frame;
1599 /* First check, and report, the lack of registers. Having GDB
1600 report "No stack!" or "No memory" when the target doesn't even
1601 have registers is very confusing. Besides, "printcmd.exp"
1602 explicitly checks that ``print $pc'' with no registers prints "No
1604 if (!target_has_registers)
1605 error (_("No registers."));
1606 if (!target_has_stack)
1607 error (_("No stack."));
1608 if (!target_has_memory)
1609 error (_("No memory."));
1610 /* Traceframes are effectively a substitute for the live inferior. */
1611 if (get_traceframe_number () < 0)
1612 validate_registers_access ();
1614 if (sentinel_frame == NULL)
1616 create_sentinel_frame (current_program_space, get_current_regcache ());
1618 /* Set the current frame before computing the frame id, to avoid
1619 recursion inside compute_frame_id, in case the frame's
1620 unwinder decides to do a symbol lookup (which depends on the
1621 selected frame's block).
1623 This call must always succeed. In particular, nothing inside
1624 get_prev_frame_always_1 should try to unwind from the
1625 sentinel frame, because that could fail/throw, and we always
1626 want to leave with the current frame created and linked in --
1627 we should never end up with the sentinel frame as outermost
1629 current_frame = get_prev_frame_always_1 (sentinel_frame);
1630 gdb_assert (current_frame != NULL);
1632 return current_frame;
1635 /* The "selected" stack frame is used by default for local and arg
1636 access. May be zero, for no selected frame. */
1638 static struct frame_info *selected_frame;
1641 has_stack_frames (void)
1643 if (!target_has_registers || !target_has_stack || !target_has_memory)
1646 /* Traceframes are effectively a substitute for the live inferior. */
1647 if (get_traceframe_number () < 0)
1649 /* No current inferior, no frame. */
1650 if (inferior_ptid == null_ptid)
1653 thread_info *tp = inferior_thread ();
1654 /* Don't try to read from a dead thread. */
1655 if (tp->state == THREAD_EXITED)
1658 /* ... or from a spinning thread. */
1666 /* Return the selected frame. Always non-NULL (unless there isn't an
1667 inferior sufficient for creating a frame) in which case an error is
1671 get_selected_frame (const char *message)
1673 if (selected_frame == NULL)
1675 if (message != NULL && !has_stack_frames ())
1676 error (("%s"), message);
1677 /* Hey! Don't trust this. It should really be re-finding the
1678 last selected frame of the currently selected thread. This,
1679 though, is better than nothing. */
1680 select_frame (get_current_frame ());
1682 /* There is always a frame. */
1683 gdb_assert (selected_frame != NULL);
1684 return selected_frame;
1687 /* If there is a selected frame, return it. Otherwise, return NULL. */
1690 get_selected_frame_if_set (void)
1692 return selected_frame;
1695 /* This is a variant of get_selected_frame() which can be called when
1696 the inferior does not have a frame; in that case it will return
1697 NULL instead of calling error(). */
1700 deprecated_safe_get_selected_frame (void)
1702 if (!has_stack_frames ())
1704 return get_selected_frame (NULL);
1707 /* Select frame FI (or NULL - to invalidate the current frame). */
1710 select_frame (struct frame_info *fi)
1712 selected_frame = fi;
1713 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1714 frame is being invalidated. */
1716 /* FIXME: kseitz/2002-08-28: It would be nice to call
1717 selected_frame_level_changed_event() right here, but due to limitations
1718 in the current interfaces, we would end up flooding UIs with events
1719 because select_frame() is used extensively internally.
1721 Once we have frame-parameterized frame (and frame-related) commands,
1722 the event notification can be moved here, since this function will only
1723 be called when the user's selected frame is being changed. */
1725 /* Ensure that symbols for this frame are read in. Also, determine the
1726 source language of this frame, and switch to it if desired. */
1731 /* We retrieve the frame's symtab by using the frame PC.
1732 However we cannot use the frame PC as-is, because it usually
1733 points to the instruction following the "call", which is
1734 sometimes the first instruction of another function. So we
1735 rely on get_frame_address_in_block() which provides us with a
1736 PC which is guaranteed to be inside the frame's code
1738 if (get_frame_address_in_block_if_available (fi, &pc))
1740 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
1743 && compunit_language (cust) != current_language->la_language
1744 && compunit_language (cust) != language_unknown
1745 && language_mode == language_mode_auto)
1746 set_language (compunit_language (cust));
1751 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1752 Always returns a non-NULL value. */
1755 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1757 struct frame_info *fi;
1761 fprintf_unfiltered (gdb_stdlog,
1762 "{ create_new_frame (addr=%s, pc=%s) ",
1763 hex_string (addr), hex_string (pc));
1766 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1768 fi->next = create_sentinel_frame (current_program_space,
1769 get_current_regcache ());
1771 /* Set/update this frame's cached PC value, found in the next frame.
1772 Do this before looking for this frame's unwinder. A sniffer is
1773 very likely to read this, and the corresponding unwinder is
1774 entitled to rely that the PC doesn't magically change. */
1775 fi->next->prev_pc.value = pc;
1776 fi->next->prev_pc.status = CC_VALUE;
1778 /* We currently assume that frame chain's can't cross spaces. */
1779 fi->pspace = fi->next->pspace;
1780 fi->aspace = fi->next->aspace;
1782 /* Select/initialize both the unwind function and the frame's type
1784 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1787 fi->this_id.value = frame_id_build (addr, pc);
1791 fprintf_unfiltered (gdb_stdlog, "-> ");
1792 fprint_frame (gdb_stdlog, fi);
1793 fprintf_unfiltered (gdb_stdlog, " }\n");
1799 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1800 innermost frame). Be careful to not fall off the bottom of the
1801 frame chain and onto the sentinel frame. */
1804 get_next_frame (struct frame_info *this_frame)
1806 if (this_frame->level > 0)
1807 return this_frame->next;
1812 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1813 innermost (i.e. current) frame, return the sentinel frame. Thus,
1814 unlike get_next_frame(), NULL will never be returned. */
1817 get_next_frame_sentinel_okay (struct frame_info *this_frame)
1819 gdb_assert (this_frame != NULL);
1821 /* Note that, due to the manner in which the sentinel frame is
1822 constructed, this_frame->next still works even when this_frame
1823 is the sentinel frame. But we disallow it here anyway because
1824 calling get_next_frame_sentinel_okay() on the sentinel frame
1825 is likely a coding error. */
1826 gdb_assert (this_frame != sentinel_frame);
1828 return this_frame->next;
1831 /* Observer for the target_changed event. */
1834 frame_observer_target_changed (struct target_ops *target)
1836 reinit_frame_cache ();
1839 /* Flush the entire frame cache. */
1842 reinit_frame_cache (void)
1844 struct frame_info *fi;
1846 /* Tear down all frame caches. */
1847 for (fi = sentinel_frame; fi != NULL; fi = fi->prev)
1849 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1850 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1851 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1852 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1855 /* Since we can't really be sure what the first object allocated was. */
1856 obstack_free (&frame_cache_obstack, 0);
1857 obstack_init (&frame_cache_obstack);
1859 if (sentinel_frame != NULL)
1860 annotate_frames_invalid ();
1862 sentinel_frame = NULL; /* Invalidate cache */
1863 select_frame (NULL);
1864 frame_stash_invalidate ();
1866 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1869 /* Find where a register is saved (in memory or another register).
1870 The result of frame_register_unwind is just where it is saved
1871 relative to this particular frame. */
1874 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1875 int *optimizedp, enum lval_type *lvalp,
1876 CORE_ADDR *addrp, int *realnump)
1878 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1880 while (this_frame != NULL)
1884 frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
1885 lvalp, addrp, realnump, NULL);
1890 if (*lvalp != lval_register)
1894 this_frame = get_next_frame (this_frame);
1898 /* Get the previous raw frame, and check that it is not identical to
1899 same other frame frame already in the chain. If it is, there is
1900 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1901 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1902 validity tests, that compare THIS_FRAME and the next frame, we do
1903 this right after creating the previous frame, to avoid ever ending
1904 up with two frames with the same id in the frame chain. */
1906 static struct frame_info *
1907 get_prev_frame_if_no_cycle (struct frame_info *this_frame)
1909 struct frame_info *prev_frame;
1911 prev_frame = get_prev_frame_raw (this_frame);
1913 /* Don't compute the frame id of the current frame yet. Unwinding
1914 the sentinel frame can fail (e.g., if the thread is gone and we
1915 can't thus read its registers). If we let the cycle detection
1916 code below try to compute a frame ID, then an error thrown from
1917 within the frame ID computation would result in the sentinel
1918 frame as outermost frame, which is bogus. Instead, we'll compute
1919 the current frame's ID lazily in get_frame_id. Note that there's
1920 no point in doing cycle detection when there's only one frame, so
1921 nothing is lost here. */
1922 if (prev_frame->level == 0)
1927 compute_frame_id (prev_frame);
1928 if (!frame_stash_add (prev_frame))
1930 /* Another frame with the same id was already in the stash. We just
1931 detected a cycle. */
1934 fprintf_unfiltered (gdb_stdlog, "-> ");
1935 fprint_frame (gdb_stdlog, NULL);
1936 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1938 this_frame->stop_reason = UNWIND_SAME_ID;
1940 prev_frame->next = NULL;
1941 this_frame->prev = NULL;
1945 catch (const gdb_exception &ex)
1947 prev_frame->next = NULL;
1948 this_frame->prev = NULL;
1956 /* Helper function for get_prev_frame_always, this is called inside a
1957 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
1958 there is no such frame. This may throw an exception. */
1960 static struct frame_info *
1961 get_prev_frame_always_1 (struct frame_info *this_frame)
1963 struct gdbarch *gdbarch;
1965 gdb_assert (this_frame != NULL);
1966 gdbarch = get_frame_arch (this_frame);
1970 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_always (this_frame=");
1971 if (this_frame != NULL)
1972 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1974 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1975 fprintf_unfiltered (gdb_stdlog, ") ");
1978 /* Only try to do the unwind once. */
1979 if (this_frame->prev_p)
1983 fprintf_unfiltered (gdb_stdlog, "-> ");
1984 fprint_frame (gdb_stdlog, this_frame->prev);
1985 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1987 return this_frame->prev;
1990 /* If the frame unwinder hasn't been selected yet, we must do so
1991 before setting prev_p; otherwise the check for misbehaved
1992 sniffers will think that this frame's sniffer tried to unwind
1993 further (see frame_cleanup_after_sniffer). */
1994 if (this_frame->unwind == NULL)
1995 frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1997 this_frame->prev_p = 1;
1998 this_frame->stop_reason = UNWIND_NO_REASON;
2000 /* If we are unwinding from an inline frame, all of the below tests
2001 were already performed when we unwound from the next non-inline
2002 frame. We must skip them, since we can not get THIS_FRAME's ID
2003 until we have unwound all the way down to the previous non-inline
2005 if (get_frame_type (this_frame) == INLINE_FRAME)
2006 return get_prev_frame_if_no_cycle (this_frame);
2008 /* Check that this frame is unwindable. If it isn't, don't try to
2009 unwind to the prev frame. */
2010 this_frame->stop_reason
2011 = this_frame->unwind->stop_reason (this_frame,
2012 &this_frame->prologue_cache);
2014 if (this_frame->stop_reason != UNWIND_NO_REASON)
2018 enum unwind_stop_reason reason = this_frame->stop_reason;
2020 fprintf_unfiltered (gdb_stdlog, "-> ");
2021 fprint_frame (gdb_stdlog, NULL);
2022 fprintf_unfiltered (gdb_stdlog, " // %s }\n",
2023 frame_stop_reason_symbol_string (reason));
2028 /* Check that this frame's ID isn't inner to (younger, below, next)
2029 the next frame. This happens when a frame unwind goes backwards.
2030 This check is valid only if this frame and the next frame are NORMAL.
2031 See the comment at frame_id_inner for details. */
2032 if (get_frame_type (this_frame) == NORMAL_FRAME
2033 && this_frame->next->unwind->type == NORMAL_FRAME
2034 && frame_id_inner (get_frame_arch (this_frame->next),
2035 get_frame_id (this_frame),
2036 get_frame_id (this_frame->next)))
2038 CORE_ADDR this_pc_in_block;
2039 struct minimal_symbol *morestack_msym;
2040 const char *morestack_name = NULL;
2042 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2043 this_pc_in_block = get_frame_address_in_block (this_frame);
2044 morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
2046 morestack_name = MSYMBOL_LINKAGE_NAME (morestack_msym);
2047 if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
2051 fprintf_unfiltered (gdb_stdlog, "-> ");
2052 fprint_frame (gdb_stdlog, NULL);
2053 fprintf_unfiltered (gdb_stdlog,
2054 " // this frame ID is inner }\n");
2056 this_frame->stop_reason = UNWIND_INNER_ID;
2061 /* Check that this and the next frame do not unwind the PC register
2062 to the same memory location. If they do, then even though they
2063 have different frame IDs, the new frame will be bogus; two
2064 functions can't share a register save slot for the PC. This can
2065 happen when the prologue analyzer finds a stack adjustment, but
2068 This check does assume that the "PC register" is roughly a
2069 traditional PC, even if the gdbarch_unwind_pc method adjusts
2070 it (we do not rely on the value, only on the unwound PC being
2071 dependent on this value). A potential improvement would be
2072 to have the frame prev_pc method and the gdbarch unwind_pc
2073 method set the same lval and location information as
2074 frame_register_unwind. */
2075 if (this_frame->level > 0
2076 && gdbarch_pc_regnum (gdbarch) >= 0
2077 && get_frame_type (this_frame) == NORMAL_FRAME
2078 && (get_frame_type (this_frame->next) == NORMAL_FRAME
2079 || get_frame_type (this_frame->next) == INLINE_FRAME))
2081 int optimized, realnum, nrealnum;
2082 enum lval_type lval, nlval;
2083 CORE_ADDR addr, naddr;
2085 frame_register_unwind_location (this_frame,
2086 gdbarch_pc_regnum (gdbarch),
2087 &optimized, &lval, &addr, &realnum);
2088 frame_register_unwind_location (get_next_frame (this_frame),
2089 gdbarch_pc_regnum (gdbarch),
2090 &optimized, &nlval, &naddr, &nrealnum);
2092 if ((lval == lval_memory && lval == nlval && addr == naddr)
2093 || (lval == lval_register && lval == nlval && realnum == nrealnum))
2097 fprintf_unfiltered (gdb_stdlog, "-> ");
2098 fprint_frame (gdb_stdlog, NULL);
2099 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
2102 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
2103 this_frame->prev = NULL;
2108 return get_prev_frame_if_no_cycle (this_frame);
2111 /* Return a "struct frame_info" corresponding to the frame that called
2112 THIS_FRAME. Returns NULL if there is no such frame.
2114 Unlike get_prev_frame, this function always tries to unwind the
2118 get_prev_frame_always (struct frame_info *this_frame)
2120 struct frame_info *prev_frame = NULL;
2124 prev_frame = get_prev_frame_always_1 (this_frame);
2126 catch (const gdb_exception_error &ex)
2128 if (ex.error == MEMORY_ERROR)
2130 this_frame->stop_reason = UNWIND_MEMORY_ERROR;
2131 if (ex.message != NULL)
2136 /* The error needs to live as long as the frame does.
2137 Allocate using stack local STOP_STRING then assign the
2138 pointer to the frame, this allows the STOP_STRING on the
2139 frame to be of type 'const char *'. */
2140 size = ex.message->size () + 1;
2141 stop_string = (char *) frame_obstack_zalloc (size);
2142 memcpy (stop_string, ex.what (), size);
2143 this_frame->stop_string = stop_string;
2154 /* Construct a new "struct frame_info" and link it previous to
2157 static struct frame_info *
2158 get_prev_frame_raw (struct frame_info *this_frame)
2160 struct frame_info *prev_frame;
2162 /* Allocate the new frame but do not wire it in to the frame chain.
2163 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2164 frame->next to pull some fancy tricks (of course such code is, by
2165 definition, recursive). Try to prevent it.
2167 There is no reason to worry about memory leaks, should the
2168 remainder of the function fail. The allocated memory will be
2169 quickly reclaimed when the frame cache is flushed, and the `we've
2170 been here before' check above will stop repeated memory
2171 allocation calls. */
2172 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
2173 prev_frame->level = this_frame->level + 1;
2175 /* For now, assume we don't have frame chains crossing address
2177 prev_frame->pspace = this_frame->pspace;
2178 prev_frame->aspace = this_frame->aspace;
2180 /* Don't yet compute ->unwind (and hence ->type). It is computed
2181 on-demand in get_frame_type, frame_register_unwind, and
2184 /* Don't yet compute the frame's ID. It is computed on-demand by
2187 /* The unwound frame ID is validate at the start of this function,
2188 as part of the logic to decide if that frame should be further
2189 unwound, and not here while the prev frame is being created.
2190 Doing this makes it possible for the user to examine a frame that
2191 has an invalid frame ID.
2193 Some very old VAX code noted: [...] For the sake of argument,
2194 suppose that the stack is somewhat trashed (which is one reason
2195 that "info frame" exists). So, return 0 (indicating we don't
2196 know the address of the arglist) if we don't know what frame this
2200 this_frame->prev = prev_frame;
2201 prev_frame->next = this_frame;
2205 fprintf_unfiltered (gdb_stdlog, "-> ");
2206 fprint_frame (gdb_stdlog, prev_frame);
2207 fprintf_unfiltered (gdb_stdlog, " }\n");
2213 /* Debug routine to print a NULL frame being returned. */
2216 frame_debug_got_null_frame (struct frame_info *this_frame,
2221 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
2222 if (this_frame != NULL)
2223 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
2225 fprintf_unfiltered (gdb_stdlog, "<NULL>");
2226 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
2230 /* Is this (non-sentinel) frame in the "main"() function? */
2233 inside_main_func (struct frame_info *this_frame)
2235 struct bound_minimal_symbol msymbol;
2238 if (symfile_objfile == 0)
2240 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
2241 if (msymbol.minsym == NULL)
2243 /* Make certain that the code, and not descriptor, address is
2245 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
2246 BMSYMBOL_VALUE_ADDRESS (msymbol),
2247 current_top_target ());
2248 return maddr == get_frame_func (this_frame);
2251 /* Test whether THIS_FRAME is inside the process entry point function. */
2254 inside_entry_func (struct frame_info *this_frame)
2256 CORE_ADDR entry_point;
2258 if (!entry_point_address_query (&entry_point))
2261 return get_frame_func (this_frame) == entry_point;
2264 /* Return a structure containing various interesting information about
2265 the frame that called THIS_FRAME. Returns NULL if there is entier
2266 no such frame or the frame fails any of a set of target-independent
2267 condition that should terminate the frame chain (e.g., as unwinding
2270 This function should not contain target-dependent tests, such as
2271 checking whether the program-counter is zero. */
2274 get_prev_frame (struct frame_info *this_frame)
2279 /* There is always a frame. If this assertion fails, suspect that
2280 something should be calling get_selected_frame() or
2281 get_current_frame(). */
2282 gdb_assert (this_frame != NULL);
2284 /* If this_frame is the current frame, then compute and stash
2285 its frame id prior to fetching and computing the frame id of the
2286 previous frame. Otherwise, the cycle detection code in
2287 get_prev_frame_if_no_cycle() will not work correctly. When
2288 get_frame_id() is called later on, an assertion error will
2289 be triggered in the event of a cycle between the current
2290 frame and its previous frame. */
2291 if (this_frame->level == 0)
2292 get_frame_id (this_frame);
2294 frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
2296 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2297 sense to stop unwinding at a dummy frame. One place where a dummy
2298 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2299 pcsqh register (space register for the instruction at the head of the
2300 instruction queue) cannot be written directly; the only way to set it
2301 is to branch to code that is in the target space. In order to implement
2302 frame dummies on HPUX, the called function is made to jump back to where
2303 the inferior was when the user function was called. If gdb was inside
2304 the main function when we created the dummy frame, the dummy frame will
2305 point inside the main function. */
2306 if (this_frame->level >= 0
2307 && get_frame_type (this_frame) == NORMAL_FRAME
2308 && !user_set_backtrace_options.backtrace_past_main
2310 && inside_main_func (this_frame))
2311 /* Don't unwind past main(). Note, this is done _before_ the
2312 frame has been marked as previously unwound. That way if the
2313 user later decides to enable unwinds past main(), that will
2314 automatically happen. */
2316 frame_debug_got_null_frame (this_frame, "inside main func");
2320 /* If the user's backtrace limit has been exceeded, stop. We must
2321 add two to the current level; one of those accounts for backtrace_limit
2322 being 1-based and the level being 0-based, and the other accounts for
2323 the level of the new frame instead of the level of the current
2325 if (this_frame->level + 2 > user_set_backtrace_options.backtrace_limit)
2327 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
2331 /* If we're already inside the entry function for the main objfile,
2332 then it isn't valid. Don't apply this test to a dummy frame -
2333 dummy frame PCs typically land in the entry func. Don't apply
2334 this test to the sentinel frame. Sentinel frames should always
2335 be allowed to unwind. */
2336 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2337 wasn't checking for "main" in the minimal symbols. With that
2338 fixed asm-source tests now stop in "main" instead of halting the
2339 backtrace in weird and wonderful ways somewhere inside the entry
2340 file. Suspect that tests for inside the entry file/func were
2341 added to work around that (now fixed) case. */
2342 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2343 suggested having the inside_entry_func test use the
2344 inside_main_func() msymbol trick (along with entry_point_address()
2345 I guess) to determine the address range of the start function.
2346 That should provide a far better stopper than the current
2348 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2349 applied tail-call optimizations to main so that a function called
2350 from main returns directly to the caller of main. Since we don't
2351 stop at main, we should at least stop at the entry point of the
2353 if (this_frame->level >= 0
2354 && get_frame_type (this_frame) == NORMAL_FRAME
2355 && !user_set_backtrace_options.backtrace_past_entry
2357 && inside_entry_func (this_frame))
2359 frame_debug_got_null_frame (this_frame, "inside entry func");
2363 /* Assume that the only way to get a zero PC is through something
2364 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2365 will never unwind a zero PC. */
2366 if (this_frame->level > 0
2367 && (get_frame_type (this_frame) == NORMAL_FRAME
2368 || get_frame_type (this_frame) == INLINE_FRAME)
2369 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
2370 && frame_pc_p && frame_pc == 0)
2372 frame_debug_got_null_frame (this_frame, "zero PC");
2376 return get_prev_frame_always (this_frame);
2380 get_prev_frame_id_by_id (struct frame_id id)
2382 struct frame_id prev_id;
2383 struct frame_info *frame;
2385 frame = frame_find_by_id (id);
2388 prev_id = get_frame_id (get_prev_frame (frame));
2390 prev_id = null_frame_id;
2396 get_frame_pc (struct frame_info *frame)
2398 gdb_assert (frame->next != NULL);
2399 return frame_unwind_pc (frame->next);
2403 get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
2406 gdb_assert (frame->next != NULL);
2410 *pc = frame_unwind_pc (frame->next);
2412 catch (const gdb_exception_error &ex)
2414 if (ex.error == NOT_AVAILABLE_ERROR)
2423 /* Return an address that falls within THIS_FRAME's code block. */
2426 get_frame_address_in_block (struct frame_info *this_frame)
2428 /* A draft address. */
2429 CORE_ADDR pc = get_frame_pc (this_frame);
2431 struct frame_info *next_frame = this_frame->next;
2433 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2434 Normally the resume address is inside the body of the function
2435 associated with THIS_FRAME, but there is a special case: when
2436 calling a function which the compiler knows will never return
2437 (for instance abort), the call may be the very last instruction
2438 in the calling function. The resume address will point after the
2439 call and may be at the beginning of a different function
2442 If THIS_FRAME is a signal frame or dummy frame, then we should
2443 not adjust the unwound PC. For a dummy frame, GDB pushed the
2444 resume address manually onto the stack. For a signal frame, the
2445 OS may have pushed the resume address manually and invoked the
2446 handler (e.g. GNU/Linux), or invoked the trampoline which called
2447 the signal handler - but in either case the signal handler is
2448 expected to return to the trampoline. So in both of these
2449 cases we know that the resume address is executable and
2450 related. So we only need to adjust the PC if THIS_FRAME
2451 is a normal function.
2453 If the program has been interrupted while THIS_FRAME is current,
2454 then clearly the resume address is inside the associated
2455 function. There are three kinds of interruption: debugger stop
2456 (next frame will be SENTINEL_FRAME), operating system
2457 signal or exception (next frame will be SIGTRAMP_FRAME),
2458 or debugger-induced function call (next frame will be
2459 DUMMY_FRAME). So we only need to adjust the PC if
2460 NEXT_FRAME is a normal function.
2462 We check the type of NEXT_FRAME first, since it is already
2463 known; frame type is determined by the unwinder, and since
2464 we have THIS_FRAME we've already selected an unwinder for
2467 If the next frame is inlined, we need to keep going until we find
2468 the real function - for instance, if a signal handler is invoked
2469 while in an inlined function, then the code address of the
2470 "calling" normal function should not be adjusted either. */
2472 while (get_frame_type (next_frame) == INLINE_FRAME)
2473 next_frame = next_frame->next;
2475 if ((get_frame_type (next_frame) == NORMAL_FRAME
2476 || get_frame_type (next_frame) == TAILCALL_FRAME)
2477 && (get_frame_type (this_frame) == NORMAL_FRAME
2478 || get_frame_type (this_frame) == TAILCALL_FRAME
2479 || get_frame_type (this_frame) == INLINE_FRAME))
2486 get_frame_address_in_block_if_available (struct frame_info *this_frame,
2492 *pc = get_frame_address_in_block (this_frame);
2494 catch (const gdb_exception_error &ex)
2496 if (ex.error == NOT_AVAILABLE_ERROR)
2505 find_frame_sal (frame_info *frame)
2507 struct frame_info *next_frame;
2511 /* If the next frame represents an inlined function call, this frame's
2512 sal is the "call site" of that inlined function, which can not
2513 be inferred from get_frame_pc. */
2514 next_frame = get_next_frame (frame);
2515 if (frame_inlined_callees (frame) > 0)
2520 sym = get_frame_function (next_frame);
2522 sym = inline_skipped_symbol (inferior_thread ());
2524 /* If frame is inline, it certainly has symbols. */
2527 symtab_and_line sal;
2528 if (SYMBOL_LINE (sym) != 0)
2530 sal.symtab = symbol_symtab (sym);
2531 sal.line = SYMBOL_LINE (sym);
2534 /* If the symbol does not have a location, we don't know where
2535 the call site is. Do not pretend to. This is jarring, but
2536 we can't do much better. */
2537 sal.pc = get_frame_pc (frame);
2539 sal.pspace = get_frame_program_space (frame);
2543 /* If FRAME is not the innermost frame, that normally means that
2544 FRAME->pc points at the return instruction (which is *after* the
2545 call instruction), and we want to get the line containing the
2546 call (because the call is where the user thinks the program is).
2547 However, if the next frame is either a SIGTRAMP_FRAME or a
2548 DUMMY_FRAME, then the next frame will contain a saved interrupt
2549 PC and such a PC indicates the current (rather than next)
2550 instruction/line, consequently, for such cases, want to get the
2551 line containing fi->pc. */
2552 if (!get_frame_pc_if_available (frame, &pc))
2555 notcurrent = (pc != get_frame_address_in_block (frame));
2556 return find_pc_line (pc, notcurrent);
2559 /* Per "frame.h", return the ``address'' of the frame. Code should
2560 really be using get_frame_id(). */
2562 get_frame_base (struct frame_info *fi)
2564 return get_frame_id (fi).stack_addr;
2567 /* High-level offsets into the frame. Used by the debug info. */
2570 get_frame_base_address (struct frame_info *fi)
2572 if (get_frame_type (fi) != NORMAL_FRAME)
2574 if (fi->base == NULL)
2575 fi->base = frame_base_find_by_frame (fi);
2576 /* Sneaky: If the low-level unwind and high-level base code share a
2577 common unwinder, let them share the prologue cache. */
2578 if (fi->base->unwind == fi->unwind)
2579 return fi->base->this_base (fi, &fi->prologue_cache);
2580 return fi->base->this_base (fi, &fi->base_cache);
2584 get_frame_locals_address (struct frame_info *fi)
2586 if (get_frame_type (fi) != NORMAL_FRAME)
2588 /* If there isn't a frame address method, find it. */
2589 if (fi->base == NULL)
2590 fi->base = frame_base_find_by_frame (fi);
2591 /* Sneaky: If the low-level unwind and high-level base code share a
2592 common unwinder, let them share the prologue cache. */
2593 if (fi->base->unwind == fi->unwind)
2594 return fi->base->this_locals (fi, &fi->prologue_cache);
2595 return fi->base->this_locals (fi, &fi->base_cache);
2599 get_frame_args_address (struct frame_info *fi)
2601 if (get_frame_type (fi) != NORMAL_FRAME)
2603 /* If there isn't a frame address method, find it. */
2604 if (fi->base == NULL)
2605 fi->base = frame_base_find_by_frame (fi);
2606 /* Sneaky: If the low-level unwind and high-level base code share a
2607 common unwinder, let them share the prologue cache. */
2608 if (fi->base->unwind == fi->unwind)
2609 return fi->base->this_args (fi, &fi->prologue_cache);
2610 return fi->base->this_args (fi, &fi->base_cache);
2613 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2617 frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
2619 if (fi->unwind == NULL)
2620 frame_unwind_find_by_frame (fi, &fi->prologue_cache);
2621 return fi->unwind == unwinder;
2624 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2625 or -1 for a NULL frame. */
2628 frame_relative_level (struct frame_info *fi)
2637 get_frame_type (struct frame_info *frame)
2639 if (frame->unwind == NULL)
2640 /* Initialize the frame's unwinder because that's what
2641 provides the frame's type. */
2642 frame_unwind_find_by_frame (frame, &frame->prologue_cache);
2643 return frame->unwind->type;
2646 struct program_space *
2647 get_frame_program_space (struct frame_info *frame)
2649 return frame->pspace;
2652 struct program_space *
2653 frame_unwind_program_space (struct frame_info *this_frame)
2655 gdb_assert (this_frame);
2657 /* This is really a placeholder to keep the API consistent --- we
2658 assume for now that we don't have frame chains crossing
2660 return this_frame->pspace;
2663 const address_space *
2664 get_frame_address_space (struct frame_info *frame)
2666 return frame->aspace;
2669 /* Memory access methods. */
2672 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
2673 gdb_byte *buf, int len)
2675 read_memory (addr, buf, len);
2679 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
2682 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2683 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2685 return read_memory_integer (addr, len, byte_order);
2689 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
2692 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2693 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2695 return read_memory_unsigned_integer (addr, len, byte_order);
2699 safe_frame_unwind_memory (struct frame_info *this_frame,
2700 CORE_ADDR addr, gdb_byte *buf, int len)
2702 /* NOTE: target_read_memory returns zero on success! */
2703 return !target_read_memory (addr, buf, len);
2706 /* Architecture methods. */
2709 get_frame_arch (struct frame_info *this_frame)
2711 return frame_unwind_arch (this_frame->next);
2715 frame_unwind_arch (struct frame_info *next_frame)
2717 if (!next_frame->prev_arch.p)
2719 struct gdbarch *arch;
2721 if (next_frame->unwind == NULL)
2722 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
2724 if (next_frame->unwind->prev_arch != NULL)
2725 arch = next_frame->unwind->prev_arch (next_frame,
2726 &next_frame->prologue_cache);
2728 arch = get_frame_arch (next_frame);
2730 next_frame->prev_arch.arch = arch;
2731 next_frame->prev_arch.p = 1;
2733 fprintf_unfiltered (gdb_stdlog,
2734 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2736 gdbarch_bfd_arch_info (arch)->printable_name);
2739 return next_frame->prev_arch.arch;
2743 frame_unwind_caller_arch (struct frame_info *next_frame)
2745 next_frame = skip_artificial_frames (next_frame);
2747 /* We must have a non-artificial frame. The caller is supposed to check
2748 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2750 gdb_assert (next_frame != NULL);
2752 return frame_unwind_arch (next_frame);
2755 /* Gets the language of FRAME. */
2758 get_frame_language (struct frame_info *frame)
2763 gdb_assert (frame!= NULL);
2765 /* We determine the current frame language by looking up its
2766 associated symtab. To retrieve this symtab, we use the frame
2767 PC. However we cannot use the frame PC as is, because it
2768 usually points to the instruction following the "call", which
2769 is sometimes the first instruction of another function. So
2770 we rely on get_frame_address_in_block(), it provides us with
2771 a PC that is guaranteed to be inside the frame's code
2776 pc = get_frame_address_in_block (frame);
2779 catch (const gdb_exception_error &ex)
2781 if (ex.error != NOT_AVAILABLE_ERROR)
2787 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
2790 return compunit_language (cust);
2793 return language_unknown;
2796 /* Stack pointer methods. */
2799 get_frame_sp (struct frame_info *this_frame)
2801 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2803 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2804 operate on THIS_FRAME now. */
2805 return gdbarch_unwind_sp (gdbarch, this_frame->next);
2808 /* Return the reason why we can't unwind past FRAME. */
2810 enum unwind_stop_reason
2811 get_frame_unwind_stop_reason (struct frame_info *frame)
2813 /* Fill-in STOP_REASON. */
2814 get_prev_frame_always (frame);
2815 gdb_assert (frame->prev_p);
2817 return frame->stop_reason;
2820 /* Return a string explaining REASON. */
2823 unwind_stop_reason_to_string (enum unwind_stop_reason reason)
2827 #define SET(name, description) \
2828 case name: return _(description);
2829 #include "unwind_stop_reasons.def"
2833 internal_error (__FILE__, __LINE__,
2834 "Invalid frame stop reason");
2839 frame_stop_reason_string (struct frame_info *fi)
2841 gdb_assert (fi->prev_p);
2842 gdb_assert (fi->prev == NULL);
2844 /* Return the specific string if we have one. */
2845 if (fi->stop_string != NULL)
2846 return fi->stop_string;
2848 /* Return the generic string if we have nothing better. */
2849 return unwind_stop_reason_to_string (fi->stop_reason);
2852 /* Return the enum symbol name of REASON as a string, to use in debug
2856 frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
2860 #define SET(name, description) \
2861 case name: return #name;
2862 #include "unwind_stop_reasons.def"
2866 internal_error (__FILE__, __LINE__,
2867 "Invalid frame stop reason");
2871 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2875 frame_cleanup_after_sniffer (struct frame_info *frame)
2877 /* The sniffer should not allocate a prologue cache if it did not
2878 match this frame. */
2879 gdb_assert (frame->prologue_cache == NULL);
2881 /* No sniffer should extend the frame chain; sniff based on what is
2883 gdb_assert (!frame->prev_p);
2885 /* The sniffer should not check the frame's ID; that's circular. */
2886 gdb_assert (!frame->this_id.p);
2888 /* Clear cached fields dependent on the unwinder.
2890 The previous PC is independent of the unwinder, but the previous
2891 function is not (see get_frame_address_in_block). */
2892 frame->prev_func.p = 0;
2893 frame->prev_func.addr = 0;
2895 /* Discard the unwinder last, so that we can easily find it if an assertion
2896 in this function triggers. */
2897 frame->unwind = NULL;
2900 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2901 If sniffing fails, the caller should be sure to call
2902 frame_cleanup_after_sniffer. */
2905 frame_prepare_for_sniffer (struct frame_info *frame,
2906 const struct frame_unwind *unwind)
2908 gdb_assert (frame->unwind == NULL);
2909 frame->unwind = unwind;
2912 static struct cmd_list_element *set_backtrace_cmdlist;
2913 static struct cmd_list_element *show_backtrace_cmdlist;
2916 set_backtrace_cmd (const char *args, int from_tty)
2918 help_list (set_backtrace_cmdlist, "set backtrace ", all_commands,
2923 show_backtrace_cmd (const char *args, int from_tty)
2925 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2928 /* Definition of the "set backtrace" settings that are exposed as
2929 "backtrace" command options. */
2931 using boolean_option_def
2932 = gdb::option::boolean_option_def<set_backtrace_options>;
2933 using uinteger_option_def
2934 = gdb::option::uinteger_option_def<set_backtrace_options>;
2936 const gdb::option::option_def set_backtrace_option_defs[] = {
2938 boolean_option_def {
2940 [] (set_backtrace_options *opt) { return &opt->backtrace_past_main; },
2941 show_backtrace_past_main, /* show_cmd_cb */
2942 N_("Set whether backtraces should continue past \"main\"."),
2943 N_("Show whether backtraces should continue past \"main\"."),
2944 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2945 the backtrace at \"main\". Set this if you need to see the rest\n\
2946 of the stack trace."),
2949 boolean_option_def {
2951 [] (set_backtrace_options *opt) { return &opt->backtrace_past_entry; },
2952 show_backtrace_past_entry, /* show_cmd_cb */
2953 N_("Set whether backtraces should continue past the entry point of a program."),
2954 N_("Show whether backtraces should continue past the entry point of a program."),
2955 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
2956 will terminate the backtrace there. Set this if you need to see\n\
2957 the rest of the stack trace."),
2962 _initialize_frame (void)
2964 obstack_init (&frame_cache_obstack);
2966 frame_stash_create ();
2968 gdb::observers::target_changed.attach (frame_observer_target_changed);
2970 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
2971 Set backtrace specific variables.\n\
2972 Configure backtrace variables such as the backtrace limit"),
2973 &set_backtrace_cmdlist, "set backtrace ",
2974 0/*allow-unknown*/, &setlist);
2975 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
2976 Show backtrace specific variables.\n\
2977 Show backtrace variables such as the backtrace limit."),
2978 &show_backtrace_cmdlist, "show backtrace ",
2979 0/*allow-unknown*/, &showlist);
2981 add_setshow_uinteger_cmd ("limit", class_obscure,
2982 &user_set_backtrace_options.backtrace_limit, _("\
2983 Set an upper bound on the number of backtrace levels."), _("\
2984 Show the upper bound on the number of backtrace levels."), _("\
2985 No more than the specified number of frames can be displayed or examined.\n\
2986 Literal \"unlimited\" or zero means no limit."),
2988 show_backtrace_limit,
2989 &set_backtrace_cmdlist,
2990 &show_backtrace_cmdlist);
2992 gdb::option::add_setshow_cmds_for_options
2993 (class_stack, &user_set_backtrace_options,
2994 set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist);
2996 /* Debug this files internals. */
2997 add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
2998 Set frame debugging."), _("\
2999 Show frame debugging."), _("\
3000 When non-zero, frame specific internal debugging is enabled."),
3003 &setdebuglist, &showdebuglist);