1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "inferior.h" /* for inferior_ptid */
26 #include "user-regs.h"
27 #include "gdb_obstack.h"
28 #include "dummy-frame.h"
29 #include "sentinel-frame.h"
33 #include "frame-unwind.h"
34 #include "frame-base.h"
37 #include "observable.h"
39 #include "gdbthread.h"
41 #include "inline-frame.h"
42 #include "tracepoint.h"
45 #include "cli/cli-option.h"
47 /* The sentinel frame terminates the innermost end of the frame chain.
48 If unwound, it returns the information needed to construct an
51 The current frame, which is the innermost frame, can be found at
52 sentinel_frame->prev. */
54 static struct frame_info
*sentinel_frame
;
56 /* Number of calls to reinit_frame_cache. */
57 static unsigned int frame_cache_generation
= 0;
62 get_frame_cache_generation ()
64 return frame_cache_generation
;
67 /* The values behind the global "set backtrace ..." settings. */
68 set_backtrace_options user_set_backtrace_options
;
70 static struct frame_info
*get_prev_frame_raw (struct frame_info
*this_frame
);
71 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason
);
73 /* Status of some values cached in the frame_info object. */
75 enum cached_copy_status
77 /* Value is unknown. */
80 /* We have a value. */
83 /* Value was not saved. */
86 /* Value is unavailable. */
90 /* We keep a cache of stack frames, each of which is a "struct
91 frame_info". The innermost one gets allocated (in
92 wait_for_inferior) each time the inferior stops; sentinel_frame
93 points to it. Additional frames get allocated (in get_prev_frame)
94 as needed, and are chained through the next and prev fields. Any
95 time that the frame cache becomes invalid (most notably when we
96 execute something, but also if we change how we interpret the
97 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
98 which reads new symbols)), we should call reinit_frame_cache. */
102 /* Level of this frame. The inner-most (youngest) frame is at level
103 0. As you move towards the outer-most (oldest) frame, the level
104 increases. This is a cached value. It could just as easily be
105 computed by counting back from the selected frame to the inner
107 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
108 reserved to indicate a bogus frame - one that has been created
109 just to keep GDB happy (GDB always needs a frame). For the
110 moment leave this as speculation. */
113 /* The frame's program space. */
114 struct program_space
*pspace
;
116 /* The frame's address space. */
117 const address_space
*aspace
;
119 /* The frame's low-level unwinder and corresponding cache. The
120 low-level unwinder is responsible for unwinding register values
121 for the previous frame. The low-level unwind methods are
122 selected based on the presence, or otherwise, of register unwind
123 information such as CFI. */
124 void *prologue_cache
;
125 const struct frame_unwind
*unwind
;
127 /* Cached copy of the previous frame's architecture. */
131 struct gdbarch
*arch
;
134 /* Cached copy of the previous frame's resume address. */
136 enum cached_copy_status status
;
137 /* Did VALUE require unmasking when being read. */
142 /* Cached copy of the previous frame's function address. */
149 /* This frame's ID. */
153 struct frame_id value
;
156 /* The frame's high-level base methods, and corresponding cache.
157 The high level base methods are selected based on the frame's
159 const struct frame_base
*base
;
162 /* Pointers to the next (down, inner, younger) and previous (up,
163 outer, older) frame_info's in the frame cache. */
164 struct frame_info
*next
; /* down, inner, younger */
166 struct frame_info
*prev
; /* up, outer, older */
168 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
169 could. Only valid when PREV_P is set. */
170 enum unwind_stop_reason stop_reason
;
172 /* A frame specific string describing the STOP_REASON in more detail.
173 Only valid when PREV_P is set, but even then may still be NULL. */
174 const char *stop_string
;
180 set_frame_previous_pc_masked (struct frame_info
*frame
)
182 frame
->prev_pc
.masked
= true;
188 get_frame_pc_masked (const struct frame_info
*frame
)
190 gdb_assert (frame
->next
!= nullptr);
191 gdb_assert (frame
->next
->prev_pc
.status
== CC_VALUE
);
193 return frame
->next
->prev_pc
.masked
;
196 /* A frame stash used to speed up frame lookups. Create a hash table
197 to stash frames previously accessed from the frame cache for
198 quicker subsequent retrieval. The hash table is emptied whenever
199 the frame cache is invalidated. */
201 static htab_t frame_stash
;
203 /* Internal function to calculate a hash from the frame_id addresses,
204 using as many valid addresses as possible. Frames below level 0
205 are not stored in the hash table. */
208 frame_addr_hash (const void *ap
)
210 const struct frame_info
*frame
= (const struct frame_info
*) ap
;
211 const struct frame_id f_id
= frame
->this_id
.value
;
214 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
216 || f_id
.special_addr_p
);
218 if (f_id
.stack_status
== FID_STACK_VALID
)
219 hash
= iterative_hash (&f_id
.stack_addr
,
220 sizeof (f_id
.stack_addr
), hash
);
221 if (f_id
.code_addr_p
)
222 hash
= iterative_hash (&f_id
.code_addr
,
223 sizeof (f_id
.code_addr
), hash
);
224 if (f_id
.special_addr_p
)
225 hash
= iterative_hash (&f_id
.special_addr
,
226 sizeof (f_id
.special_addr
), hash
);
231 /* Internal equality function for the hash table. This function
232 defers equality operations to frame_id_eq. */
235 frame_addr_hash_eq (const void *a
, const void *b
)
237 const struct frame_info
*f_entry
= (const struct frame_info
*) a
;
238 const struct frame_info
*f_element
= (const struct frame_info
*) b
;
240 return frame_id_eq (f_entry
->this_id
.value
,
241 f_element
->this_id
.value
);
244 /* Internal function to create the frame_stash hash table. 100 seems
245 to be a good compromise to start the hash table at. */
248 frame_stash_create (void)
250 frame_stash
= htab_create (100,
256 /* Internal function to add a frame to the frame_stash hash table.
257 Returns false if a frame with the same ID was already stashed, true
261 frame_stash_add (struct frame_info
*frame
)
263 struct frame_info
**slot
;
265 /* Do not try to stash the sentinel frame. */
266 gdb_assert (frame
->level
>= 0);
268 slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
272 /* If we already have a frame in the stack with the same id, we
273 either have a stack cycle (corrupted stack?), or some bug
274 elsewhere in GDB. In any case, ignore the duplicate and return
275 an indication to the caller. */
283 /* Internal function to search the frame stash for an entry with the
284 given frame ID. If found, return that frame. Otherwise return
287 static struct frame_info
*
288 frame_stash_find (struct frame_id id
)
290 struct frame_info dummy
;
291 struct frame_info
*frame
;
293 dummy
.this_id
.value
= id
;
294 frame
= (struct frame_info
*) htab_find (frame_stash
, &dummy
);
298 /* Internal function to invalidate the frame stash by removing all
299 entries in it. This only occurs when the frame cache is
303 frame_stash_invalidate (void)
305 htab_empty (frame_stash
);
309 scoped_restore_selected_frame::scoped_restore_selected_frame ()
311 m_fid
= get_frame_id (get_selected_frame (NULL
));
315 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
317 frame_info
*frame
= frame_find_by_id (m_fid
);
319 warning (_("Unable to restore previously selected frame."));
321 select_frame (frame
);
324 /* Flag to control debugging. */
326 unsigned int frame_debug
;
328 show_frame_debug (struct ui_file
*file
, int from_tty
,
329 struct cmd_list_element
*c
, const char *value
)
331 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
334 /* Implementation of "show backtrace past-main". */
337 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
338 struct cmd_list_element
*c
, const char *value
)
340 fprintf_filtered (file
,
341 _("Whether backtraces should "
342 "continue past \"main\" is %s.\n"),
346 /* Implementation of "show backtrace past-entry". */
349 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
350 struct cmd_list_element
*c
, const char *value
)
352 fprintf_filtered (file
, _("Whether backtraces should continue past the "
353 "entry point of a program is %s.\n"),
357 /* Implementation of "show backtrace limit". */
360 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
361 struct cmd_list_element
*c
, const char *value
)
363 fprintf_filtered (file
,
364 _("An upper bound on the number "
365 "of backtrace levels is %s.\n"),
371 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
374 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
376 fprintf_unfiltered (file
, "!%s", name
);
380 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
382 fprintf_unfiltered (file
, "{");
384 if (id
.stack_status
== FID_STACK_INVALID
)
385 fprintf_unfiltered (file
, "!stack");
386 else if (id
.stack_status
== FID_STACK_UNAVAILABLE
)
387 fprintf_unfiltered (file
, "stack=<unavailable>");
388 else if (id
.stack_status
== FID_STACK_SENTINEL
)
389 fprintf_unfiltered (file
, "stack=<sentinel>");
391 fprintf_unfiltered (file
, "stack=%s", hex_string (id
.stack_addr
));
392 fprintf_unfiltered (file
, ",");
394 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
395 fprintf_unfiltered (file
, ",");
397 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
399 if (id
.artificial_depth
)
400 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
402 fprintf_unfiltered (file
, "}");
406 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
411 fprintf_unfiltered (file
, "NORMAL_FRAME");
414 fprintf_unfiltered (file
, "DUMMY_FRAME");
417 fprintf_unfiltered (file
, "INLINE_FRAME");
420 fprintf_unfiltered (file
, "TAILCALL_FRAME");
423 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
426 fprintf_unfiltered (file
, "ARCH_FRAME");
429 fprintf_unfiltered (file
, "SENTINEL_FRAME");
432 fprintf_unfiltered (file
, "<unknown type>");
438 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
442 fprintf_unfiltered (file
, "<NULL frame>");
445 fprintf_unfiltered (file
, "{");
446 fprintf_unfiltered (file
, "level=%d", fi
->level
);
447 fprintf_unfiltered (file
, ",");
448 fprintf_unfiltered (file
, "type=");
449 if (fi
->unwind
!= NULL
)
450 fprint_frame_type (file
, fi
->unwind
->type
);
452 fprintf_unfiltered (file
, "<unknown>");
453 fprintf_unfiltered (file
, ",");
454 fprintf_unfiltered (file
, "unwind=");
455 if (fi
->unwind
!= NULL
)
456 gdb_print_host_address (fi
->unwind
, file
);
458 fprintf_unfiltered (file
, "<unknown>");
459 fprintf_unfiltered (file
, ",");
460 fprintf_unfiltered (file
, "pc=");
461 if (fi
->next
== NULL
|| fi
->next
->prev_pc
.status
== CC_UNKNOWN
)
462 fprintf_unfiltered (file
, "<unknown>");
463 else if (fi
->next
->prev_pc
.status
== CC_VALUE
)
465 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_pc
.value
));
466 if (fi
->next
->prev_pc
.masked
)
467 fprintf_unfiltered (file
, "[PAC]");
469 else if (fi
->next
->prev_pc
.status
== CC_NOT_SAVED
)
470 val_print_not_saved (file
);
471 else if (fi
->next
->prev_pc
.status
== CC_UNAVAILABLE
)
472 val_print_unavailable (file
);
473 fprintf_unfiltered (file
, ",");
474 fprintf_unfiltered (file
, "id=");
476 fprint_frame_id (file
, fi
->this_id
.value
);
478 fprintf_unfiltered (file
, "<unknown>");
479 fprintf_unfiltered (file
, ",");
480 fprintf_unfiltered (file
, "func=");
481 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.p
)
482 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
484 fprintf_unfiltered (file
, "<unknown>");
485 fprintf_unfiltered (file
, "}");
488 /* Given FRAME, return the enclosing frame as found in real frames read-in from
489 inferior memory. Skip any previous frames which were made up by GDB.
490 Return FRAME if FRAME is a non-artificial frame.
491 Return NULL if FRAME is the start of an artificial-only chain. */
493 static struct frame_info
*
494 skip_artificial_frames (struct frame_info
*frame
)
496 /* Note we use get_prev_frame_always, and not get_prev_frame. The
497 latter will truncate the frame chain, leading to this function
498 unintentionally returning a null_frame_id (e.g., when the user
499 sets a backtrace limit).
501 Note that for record targets we may get a frame chain that consists
502 of artificial frames only. */
503 while (get_frame_type (frame
) == INLINE_FRAME
504 || get_frame_type (frame
) == TAILCALL_FRAME
)
506 frame
= get_prev_frame_always (frame
);
515 skip_unwritable_frames (struct frame_info
*frame
)
517 while (gdbarch_code_of_frame_writable (get_frame_arch (frame
), frame
) == 0)
519 frame
= get_prev_frame (frame
);
530 skip_tailcall_frames (struct frame_info
*frame
)
532 while (get_frame_type (frame
) == TAILCALL_FRAME
)
534 /* Note that for record targets we may get a frame chain that consists of
535 tailcall frames only. */
536 frame
= get_prev_frame (frame
);
544 /* Compute the frame's uniq ID that can be used to, later, re-find the
548 compute_frame_id (struct frame_info
*fi
)
550 gdb_assert (!fi
->this_id
.p
);
553 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
555 /* Find the unwinder. */
556 if (fi
->unwind
== NULL
)
557 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
558 /* Find THIS frame's ID. */
559 /* Default to outermost if no ID is found. */
560 fi
->this_id
.value
= outer_frame_id
;
561 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
562 gdb_assert (frame_id_p (fi
->this_id
.value
));
566 fprintf_unfiltered (gdb_stdlog
, "-> ");
567 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
568 fprintf_unfiltered (gdb_stdlog
, " }\n");
572 /* Return a frame uniq ID that can be used to, later, re-find the
576 get_frame_id (struct frame_info
*fi
)
579 return null_frame_id
;
585 /* If we haven't computed the frame id yet, then it must be that
586 this is the current frame. Compute it now, and stash the
587 result. The IDs of other frames are computed as soon as
588 they're created, in order to detect cycles. See
589 get_prev_frame_if_no_cycle. */
590 gdb_assert (fi
->level
== 0);
593 compute_frame_id (fi
);
595 /* Since this is the first frame in the chain, this should
597 stashed
= frame_stash_add (fi
);
598 gdb_assert (stashed
);
601 return fi
->this_id
.value
;
605 get_stack_frame_id (struct frame_info
*next_frame
)
607 return get_frame_id (skip_artificial_frames (next_frame
));
611 frame_unwind_caller_id (struct frame_info
*next_frame
)
613 struct frame_info
*this_frame
;
615 /* Use get_prev_frame_always, and not get_prev_frame. The latter
616 will truncate the frame chain, leading to this function
617 unintentionally returning a null_frame_id (e.g., when a caller
618 requests the frame ID of "main()"s caller. */
620 next_frame
= skip_artificial_frames (next_frame
);
621 if (next_frame
== NULL
)
622 return null_frame_id
;
624 this_frame
= get_prev_frame_always (next_frame
);
626 return get_frame_id (skip_artificial_frames (this_frame
));
628 return null_frame_id
;
631 const struct frame_id null_frame_id
= { 0 }; /* All zeros. */
632 const struct frame_id sentinel_frame_id
= { 0, 0, 0, FID_STACK_SENTINEL
, 0, 1, 0 };
633 const struct frame_id outer_frame_id
= { 0, 0, 0, FID_STACK_INVALID
, 0, 1, 0 };
636 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
637 CORE_ADDR special_addr
)
639 struct frame_id id
= null_frame_id
;
641 id
.stack_addr
= stack_addr
;
642 id
.stack_status
= FID_STACK_VALID
;
643 id
.code_addr
= code_addr
;
645 id
.special_addr
= special_addr
;
646 id
.special_addr_p
= 1;
653 frame_id_build_unavailable_stack (CORE_ADDR code_addr
)
655 struct frame_id id
= null_frame_id
;
657 id
.stack_status
= FID_STACK_UNAVAILABLE
;
658 id
.code_addr
= code_addr
;
666 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr
,
667 CORE_ADDR special_addr
)
669 struct frame_id id
= null_frame_id
;
671 id
.stack_status
= FID_STACK_UNAVAILABLE
;
672 id
.code_addr
= code_addr
;
674 id
.special_addr
= special_addr
;
675 id
.special_addr_p
= 1;
680 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
682 struct frame_id id
= null_frame_id
;
684 id
.stack_addr
= stack_addr
;
685 id
.stack_status
= FID_STACK_VALID
;
686 id
.code_addr
= code_addr
;
692 frame_id_build_wild (CORE_ADDR stack_addr
)
694 struct frame_id id
= null_frame_id
;
696 id
.stack_addr
= stack_addr
;
697 id
.stack_status
= FID_STACK_VALID
;
702 frame_id_p (struct frame_id l
)
706 /* The frame is valid iff it has a valid stack address. */
707 p
= l
.stack_status
!= FID_STACK_INVALID
;
708 /* outer_frame_id is also valid. */
709 if (!p
&& memcmp (&l
, &outer_frame_id
, sizeof (l
)) == 0)
713 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
714 fprint_frame_id (gdb_stdlog
, l
);
715 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
721 frame_id_artificial_p (struct frame_id l
)
726 return (l
.artificial_depth
!= 0);
730 frame_id_eq (struct frame_id l
, struct frame_id r
)
734 if (l
.stack_status
== FID_STACK_INVALID
&& l
.special_addr_p
735 && r
.stack_status
== FID_STACK_INVALID
&& r
.special_addr_p
)
736 /* The outermost frame marker is equal to itself. This is the
737 dodgy thing about outer_frame_id, since between execution steps
738 we might step into another function - from which we can't
739 unwind either. More thought required to get rid of
742 else if (l
.stack_status
== FID_STACK_INVALID
743 || r
.stack_status
== FID_STACK_INVALID
)
744 /* Like a NaN, if either ID is invalid, the result is false.
745 Note that a frame ID is invalid iff it is the null frame ID. */
747 else if (l
.stack_status
!= r
.stack_status
|| l
.stack_addr
!= r
.stack_addr
)
748 /* If .stack addresses are different, the frames are different. */
750 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
751 /* An invalid code addr is a wild card. If .code addresses are
752 different, the frames are different. */
754 else if (l
.special_addr_p
&& r
.special_addr_p
755 && l
.special_addr
!= r
.special_addr
)
756 /* An invalid special addr is a wild card (or unused). Otherwise
757 if special addresses are different, the frames are different. */
759 else if (l
.artificial_depth
!= r
.artificial_depth
)
760 /* If artificial depths are different, the frames must be different. */
763 /* Frames are equal. */
768 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
769 fprint_frame_id (gdb_stdlog
, l
);
770 fprintf_unfiltered (gdb_stdlog
, ",r=");
771 fprint_frame_id (gdb_stdlog
, r
);
772 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
777 /* Safety net to check whether frame ID L should be inner to
778 frame ID R, according to their stack addresses.
780 This method cannot be used to compare arbitrary frames, as the
781 ranges of valid stack addresses may be discontiguous (e.g. due
784 However, it can be used as safety net to discover invalid frame
785 IDs in certain circumstances. Assuming that NEXT is the immediate
786 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
788 * The stack address of NEXT must be inner-than-or-equal to the stack
791 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
794 * If NEXT and THIS have different stack addresses, no other frame
795 in the frame chain may have a stack address in between.
797 Therefore, if frame_id_inner (TEST, THIS) holds, but
798 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
799 to a valid frame in the frame chain.
801 The sanity checks above cannot be performed when a SIGTRAMP frame
802 is involved, because signal handlers might be executed on a different
803 stack than the stack used by the routine that caused the signal
804 to be raised. This can happen for instance when a thread exceeds
805 its maximum stack size. In this case, certain compilers implement
806 a stack overflow strategy that cause the handler to be run on a
810 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
814 if (l
.stack_status
!= FID_STACK_VALID
|| r
.stack_status
!= FID_STACK_VALID
)
815 /* Like NaN, any operation involving an invalid ID always fails.
816 Likewise if either ID has an unavailable stack address. */
818 else if (l
.artificial_depth
> r
.artificial_depth
819 && l
.stack_addr
== r
.stack_addr
820 && l
.code_addr_p
== r
.code_addr_p
821 && l
.special_addr_p
== r
.special_addr_p
822 && l
.special_addr
== r
.special_addr
)
824 /* Same function, different inlined functions. */
825 const struct block
*lb
, *rb
;
827 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
829 lb
= block_for_pc (l
.code_addr
);
830 rb
= block_for_pc (r
.code_addr
);
832 if (lb
== NULL
|| rb
== NULL
)
833 /* Something's gone wrong. */
836 /* This will return true if LB and RB are the same block, or
837 if the block with the smaller depth lexically encloses the
838 block with the greater depth. */
839 inner
= contained_in (lb
, rb
);
842 /* Only return non-zero when strictly inner than. Note that, per
843 comment in "frame.h", there is some fuzz here. Frameless
844 functions are not strictly inner than (same .stack but
845 different .code and/or .special address). */
846 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
849 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
850 fprint_frame_id (gdb_stdlog
, l
);
851 fprintf_unfiltered (gdb_stdlog
, ",r=");
852 fprint_frame_id (gdb_stdlog
, r
);
853 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
859 frame_find_by_id (struct frame_id id
)
861 struct frame_info
*frame
, *prev_frame
;
863 /* ZERO denotes the null frame, let the caller decide what to do
864 about it. Should it instead return get_current_frame()? */
865 if (!frame_id_p (id
))
868 /* Check for the sentinel frame. */
869 if (frame_id_eq (id
, sentinel_frame_id
))
870 return sentinel_frame
;
872 /* Try using the frame stash first. Finding it there removes the need
873 to perform the search by looping over all frames, which can be very
874 CPU-intensive if the number of frames is very high (the loop is O(n)
875 and get_prev_frame performs a series of checks that are relatively
876 expensive). This optimization is particularly useful when this function
877 is called from another function (such as value_fetch_lazy, case
878 VALUE_LVAL (val) == lval_register) which already loops over all frames,
879 making the overall behavior O(n^2). */
880 frame
= frame_stash_find (id
);
884 for (frame
= get_current_frame (); ; frame
= prev_frame
)
886 struct frame_id self
= get_frame_id (frame
);
888 if (frame_id_eq (id
, self
))
889 /* An exact match. */
892 prev_frame
= get_prev_frame (frame
);
896 /* As a safety net to avoid unnecessary backtracing while trying
897 to find an invalid ID, we check for a common situation where
898 we can detect from comparing stack addresses that no other
899 frame in the current frame chain can have this ID. See the
900 comment at frame_id_inner for details. */
901 if (get_frame_type (frame
) == NORMAL_FRAME
902 && !frame_id_inner (get_frame_arch (frame
), id
, self
)
903 && frame_id_inner (get_frame_arch (prev_frame
), id
,
904 get_frame_id (prev_frame
)))
911 frame_unwind_pc (struct frame_info
*this_frame
)
913 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
915 struct gdbarch
*prev_gdbarch
;
919 /* The right way. The `pure' way. The one true way. This
920 method depends solely on the register-unwind code to
921 determine the value of registers in THIS frame, and hence
922 the value of this frame's PC (resume address). A typical
923 implementation is no more than:
925 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
926 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
928 Note: this method is very heavily dependent on a correct
929 register-unwind implementation, it pays to fix that
930 method first; this method is frame type agnostic, since
931 it only deals with register values, it works with any
932 frame. This is all in stark contrast to the old
933 FRAME_SAVED_PC which would try to directly handle all the
934 different ways that a PC could be unwound. */
935 prev_gdbarch
= frame_unwind_arch (this_frame
);
939 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
942 catch (const gdb_exception_error
&ex
)
944 if (ex
.error
== NOT_AVAILABLE_ERROR
)
946 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
949 fprintf_unfiltered (gdb_stdlog
,
950 "{ frame_unwind_pc (this_frame=%d)"
951 " -> <unavailable> }\n",
954 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
956 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
959 fprintf_unfiltered (gdb_stdlog
,
960 "{ frame_unwind_pc (this_frame=%d)"
961 " -> <not saved> }\n",
970 this_frame
->prev_pc
.value
= pc
;
971 this_frame
->prev_pc
.status
= CC_VALUE
;
973 fprintf_unfiltered (gdb_stdlog
,
974 "{ frame_unwind_pc (this_frame=%d) "
977 hex_string (this_frame
->prev_pc
.value
));
981 if (this_frame
->prev_pc
.status
== CC_VALUE
)
982 return this_frame
->prev_pc
.value
;
983 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
984 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
985 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
986 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
988 internal_error (__FILE__
, __LINE__
,
989 "unexpected prev_pc status: %d",
990 (int) this_frame
->prev_pc
.status
);
994 frame_unwind_caller_pc (struct frame_info
*this_frame
)
996 this_frame
= skip_artificial_frames (this_frame
);
998 /* We must have a non-artificial frame. The caller is supposed to check
999 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1001 gdb_assert (this_frame
!= NULL
);
1003 return frame_unwind_pc (this_frame
);
1007 get_frame_func_if_available (struct frame_info
*this_frame
, CORE_ADDR
*pc
)
1009 struct frame_info
*next_frame
= this_frame
->next
;
1011 if (!next_frame
->prev_func
.p
)
1013 CORE_ADDR addr_in_block
;
1015 /* Make certain that this, and not the adjacent, function is
1017 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
1019 next_frame
->prev_func
.p
= -1;
1021 fprintf_unfiltered (gdb_stdlog
,
1022 "{ get_frame_func (this_frame=%d)"
1023 " -> unavailable }\n",
1028 next_frame
->prev_func
.p
= 1;
1029 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
1031 fprintf_unfiltered (gdb_stdlog
,
1032 "{ get_frame_func (this_frame=%d) -> %s }\n",
1034 hex_string (next_frame
->prev_func
.addr
));
1038 if (next_frame
->prev_func
.p
< 0)
1045 *pc
= next_frame
->prev_func
.addr
;
1051 get_frame_func (struct frame_info
*this_frame
)
1055 if (!get_frame_func_if_available (this_frame
, &pc
))
1056 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1061 std::unique_ptr
<readonly_detached_regcache
>
1062 frame_save_as_regcache (struct frame_info
*this_frame
)
1064 auto cooked_read
= [this_frame
] (int regnum
, gdb_byte
*buf
)
1066 if (!deprecated_frame_register_read (this_frame
, regnum
, buf
))
1067 return REG_UNAVAILABLE
;
1072 std::unique_ptr
<readonly_detached_regcache
> regcache
1073 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
1079 frame_pop (struct frame_info
*this_frame
)
1081 struct frame_info
*prev_frame
;
1083 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
1085 /* Popping a dummy frame involves restoring more than just registers.
1086 dummy_frame_pop does all the work. */
1087 dummy_frame_pop (get_frame_id (this_frame
), inferior_thread ());
1091 /* Ensure that we have a frame to pop to. */
1092 prev_frame
= get_prev_frame_always (this_frame
);
1095 error (_("Cannot pop the initial frame."));
1097 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1098 entering THISFRAME. */
1099 prev_frame
= skip_tailcall_frames (prev_frame
);
1101 if (prev_frame
== NULL
)
1102 error (_("Cannot find the caller frame."));
1104 /* Make a copy of all the register values unwound from this frame.
1105 Save them in a scratch buffer so that there isn't a race between
1106 trying to extract the old values from the current regcache while
1107 at the same time writing new values into that same cache. */
1108 std::unique_ptr
<readonly_detached_regcache
> scratch
1109 = frame_save_as_regcache (prev_frame
);
1111 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1112 target's register cache that it is about to be hit with a burst
1113 register transfer and that the sequence of register writes should
1114 be batched. The pair target_prepare_to_store() and
1115 target_store_registers() kind of suggest this functionality.
1116 Unfortunately, they don't implement it. Their lack of a formal
1117 definition can lead to targets writing back bogus values
1118 (arguably a bug in the target code mind). */
1119 /* Now copy those saved registers into the current regcache. */
1120 get_current_regcache ()->restore (scratch
.get ());
1122 /* We've made right mess of GDB's local state, just discard
1124 reinit_frame_cache ();
1128 frame_register_unwind (frame_info
*next_frame
, int regnum
,
1129 int *optimizedp
, int *unavailablep
,
1130 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
1131 int *realnump
, gdb_byte
*bufferp
)
1133 struct value
*value
;
1135 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1136 that the value proper does not need to be fetched. */
1137 gdb_assert (optimizedp
!= NULL
);
1138 gdb_assert (lvalp
!= NULL
);
1139 gdb_assert (addrp
!= NULL
);
1140 gdb_assert (realnump
!= NULL
);
1141 /* gdb_assert (bufferp != NULL); */
1143 value
= frame_unwind_register_value (next_frame
, regnum
);
1145 gdb_assert (value
!= NULL
);
1147 *optimizedp
= value_optimized_out (value
);
1148 *unavailablep
= !value_entirely_available (value
);
1149 *lvalp
= VALUE_LVAL (value
);
1150 *addrp
= value_address (value
);
1151 if (*lvalp
== lval_register
)
1152 *realnump
= VALUE_REGNUM (value
);
1158 if (!*optimizedp
&& !*unavailablep
)
1159 memcpy (bufferp
, value_contents_all (value
),
1160 TYPE_LENGTH (value_type (value
)));
1162 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
1165 /* Dispose of the new value. This prevents watchpoints from
1166 trying to watch the saved frame pointer. */
1167 release_value (value
);
1171 frame_register (struct frame_info
*frame
, int regnum
,
1172 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
1173 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
1175 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1176 that the value proper does not need to be fetched. */
1177 gdb_assert (optimizedp
!= NULL
);
1178 gdb_assert (lvalp
!= NULL
);
1179 gdb_assert (addrp
!= NULL
);
1180 gdb_assert (realnump
!= NULL
);
1181 /* gdb_assert (bufferp != NULL); */
1183 /* Obtain the register value by unwinding the register from the next
1184 (more inner frame). */
1185 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
1186 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
1187 lvalp
, addrp
, realnump
, bufferp
);
1191 frame_unwind_register (frame_info
*next_frame
, int regnum
, gdb_byte
*buf
)
1197 enum lval_type lval
;
1199 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1200 &lval
, &addr
, &realnum
, buf
);
1203 throw_error (OPTIMIZED_OUT_ERROR
,
1204 _("Register %d was not saved"), regnum
);
1206 throw_error (NOT_AVAILABLE_ERROR
,
1207 _("Register %d is not available"), regnum
);
1211 get_frame_register (struct frame_info
*frame
,
1212 int regnum
, gdb_byte
*buf
)
1214 frame_unwind_register (frame
->next
, regnum
, buf
);
1218 frame_unwind_register_value (frame_info
*next_frame
, int regnum
)
1220 struct gdbarch
*gdbarch
;
1221 struct value
*value
;
1223 gdb_assert (next_frame
!= NULL
);
1224 gdbarch
= frame_unwind_arch (next_frame
);
1228 fprintf_unfiltered (gdb_stdlog
,
1229 "{ frame_unwind_register_value "
1230 "(frame=%d,regnum=%d(%s),...) ",
1231 next_frame
->level
, regnum
,
1232 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1235 /* Find the unwinder. */
1236 if (next_frame
->unwind
== NULL
)
1237 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
1239 /* Ask this frame to unwind its register. */
1240 value
= next_frame
->unwind
->prev_register (next_frame
,
1241 &next_frame
->prologue_cache
,
1246 fprintf_unfiltered (gdb_stdlog
, "->");
1247 if (value_optimized_out (value
))
1249 fprintf_unfiltered (gdb_stdlog
, " ");
1250 val_print_optimized_out (value
, gdb_stdlog
);
1254 if (VALUE_LVAL (value
) == lval_register
)
1255 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1256 VALUE_REGNUM (value
));
1257 else if (VALUE_LVAL (value
) == lval_memory
)
1258 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1260 value_address (value
)));
1262 fprintf_unfiltered (gdb_stdlog
, " computed");
1264 if (value_lazy (value
))
1265 fprintf_unfiltered (gdb_stdlog
, " lazy");
1269 const gdb_byte
*buf
= value_contents (value
);
1271 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1272 fprintf_unfiltered (gdb_stdlog
, "[");
1273 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1274 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1275 fprintf_unfiltered (gdb_stdlog
, "]");
1279 fprintf_unfiltered (gdb_stdlog
, " }\n");
1286 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1288 return frame_unwind_register_value (frame
->next
, regnum
);
1292 frame_unwind_register_signed (frame_info
*next_frame
, int regnum
)
1294 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1295 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1296 int size
= register_size (gdbarch
, regnum
);
1297 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1299 gdb_assert (value
!= NULL
);
1301 if (value_optimized_out (value
))
1303 throw_error (OPTIMIZED_OUT_ERROR
,
1304 _("Register %d was not saved"), regnum
);
1306 if (!value_entirely_available (value
))
1308 throw_error (NOT_AVAILABLE_ERROR
,
1309 _("Register %d is not available"), regnum
);
1312 LONGEST r
= extract_signed_integer (value_contents_all (value
), size
,
1315 release_value (value
);
1320 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1322 return frame_unwind_register_signed (frame
->next
, regnum
);
1326 frame_unwind_register_unsigned (frame_info
*next_frame
, int regnum
)
1328 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1329 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1330 int size
= register_size (gdbarch
, regnum
);
1331 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1333 gdb_assert (value
!= NULL
);
1335 if (value_optimized_out (value
))
1337 throw_error (OPTIMIZED_OUT_ERROR
,
1338 _("Register %d was not saved"), regnum
);
1340 if (!value_entirely_available (value
))
1342 throw_error (NOT_AVAILABLE_ERROR
,
1343 _("Register %d is not available"), regnum
);
1346 ULONGEST r
= extract_unsigned_integer (value_contents_all (value
), size
,
1349 release_value (value
);
1354 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1356 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1360 read_frame_register_unsigned (struct frame_info
*frame
, int regnum
,
1363 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1365 if (!value_optimized_out (regval
)
1366 && value_entirely_available (regval
))
1368 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1369 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1370 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1372 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1380 put_frame_register (struct frame_info
*frame
, int regnum
,
1381 const gdb_byte
*buf
)
1383 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1387 enum lval_type lval
;
1390 frame_register (frame
, regnum
, &optim
, &unavail
,
1391 &lval
, &addr
, &realnum
, NULL
);
1393 error (_("Attempt to assign to a register that was not saved."));
1398 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1402 get_current_regcache ()->cooked_write (realnum
, buf
);
1405 error (_("Attempt to assign to an unmodifiable value."));
1409 /* This function is deprecated. Use get_frame_register_value instead,
1410 which provides more accurate information.
1412 Find and return the value of REGNUM for the specified stack frame.
1413 The number of bytes copied is REGISTER_SIZE (REGNUM).
1415 Returns 0 if the register value could not be found. */
1418 deprecated_frame_register_read (struct frame_info
*frame
, int regnum
,
1423 enum lval_type lval
;
1427 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1428 &lval
, &addr
, &realnum
, myaddr
);
1430 return !optimized
&& !unavailable
;
1434 get_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1435 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1436 int *optimizedp
, int *unavailablep
)
1438 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1443 /* Skip registers wholly inside of OFFSET. */
1444 while (offset
>= register_size (gdbarch
, regnum
))
1446 offset
-= register_size (gdbarch
, regnum
);
1450 /* Ensure that we will not read beyond the end of the register file.
1451 This can only ever happen if the debug information is bad. */
1453 numregs
= gdbarch_num_cooked_regs (gdbarch
);
1454 for (i
= regnum
; i
< numregs
; i
++)
1456 int thissize
= register_size (gdbarch
, i
);
1459 break; /* This register is not available on this architecture. */
1460 maxsize
+= thissize
;
1463 error (_("Bad debug information detected: "
1464 "Attempt to read %d bytes from registers."), len
);
1466 /* Copy the data. */
1469 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1474 if (curr_len
== register_size (gdbarch
, regnum
))
1476 enum lval_type lval
;
1480 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1481 &lval
, &addr
, &realnum
, myaddr
);
1482 if (*optimizedp
|| *unavailablep
)
1487 struct value
*value
= frame_unwind_register_value (frame
->next
,
1489 gdb_assert (value
!= NULL
);
1490 *optimizedp
= value_optimized_out (value
);
1491 *unavailablep
= !value_entirely_available (value
);
1493 if (*optimizedp
|| *unavailablep
)
1495 release_value (value
);
1498 memcpy (myaddr
, value_contents_all (value
) + offset
, curr_len
);
1499 release_value (value
);
1514 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1515 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1517 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1519 /* Skip registers wholly inside of OFFSET. */
1520 while (offset
>= register_size (gdbarch
, regnum
))
1522 offset
-= register_size (gdbarch
, regnum
);
1526 /* Copy the data. */
1529 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1534 if (curr_len
== register_size (gdbarch
, regnum
))
1536 put_frame_register (frame
, regnum
, myaddr
);
1540 struct value
*value
= frame_unwind_register_value (frame
->next
,
1542 gdb_assert (value
!= NULL
);
1544 memcpy ((char *) value_contents_writeable (value
) + offset
, myaddr
,
1546 put_frame_register (frame
, regnum
, value_contents_raw (value
));
1547 release_value (value
);
1557 /* Create a sentinel frame. */
1559 static struct frame_info
*
1560 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1562 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1565 frame
->pspace
= pspace
;
1566 frame
->aspace
= regcache
->aspace ();
1567 /* Explicitly initialize the sentinel frame's cache. Provide it
1568 with the underlying regcache. In the future additional
1569 information, such as the frame's thread will be added. */
1570 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1571 /* For the moment there is only one sentinel frame implementation. */
1572 frame
->unwind
= &sentinel_frame_unwind
;
1573 /* Link this frame back to itself. The frame is self referential
1574 (the unwound PC is the same as the pc), so make it so. */
1575 frame
->next
= frame
;
1576 /* The sentinel frame has a special ID. */
1577 frame
->this_id
.p
= 1;
1578 frame
->this_id
.value
= sentinel_frame_id
;
1581 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1582 fprint_frame (gdb_stdlog
, frame
);
1583 fprintf_unfiltered (gdb_stdlog
, " }\n");
1588 /* Cache for frame addresses already read by gdb. Valid only while
1589 inferior is stopped. Control variables for the frame cache should
1590 be local to this module. */
1592 static struct obstack frame_cache_obstack
;
1595 frame_obstack_zalloc (unsigned long size
)
1597 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1599 memset (data
, 0, size
);
1603 static struct frame_info
*get_prev_frame_always_1 (struct frame_info
*this_frame
);
1606 get_current_frame (void)
1608 struct frame_info
*current_frame
;
1610 /* First check, and report, the lack of registers. Having GDB
1611 report "No stack!" or "No memory" when the target doesn't even
1612 have registers is very confusing. Besides, "printcmd.exp"
1613 explicitly checks that ``print $pc'' with no registers prints "No
1615 if (!target_has_registers
)
1616 error (_("No registers."));
1617 if (!target_has_stack
)
1618 error (_("No stack."));
1619 if (!target_has_memory
)
1620 error (_("No memory."));
1621 /* Traceframes are effectively a substitute for the live inferior. */
1622 if (get_traceframe_number () < 0)
1623 validate_registers_access ();
1625 if (sentinel_frame
== NULL
)
1627 create_sentinel_frame (current_program_space
, get_current_regcache ());
1629 /* Set the current frame before computing the frame id, to avoid
1630 recursion inside compute_frame_id, in case the frame's
1631 unwinder decides to do a symbol lookup (which depends on the
1632 selected frame's block).
1634 This call must always succeed. In particular, nothing inside
1635 get_prev_frame_always_1 should try to unwind from the
1636 sentinel frame, because that could fail/throw, and we always
1637 want to leave with the current frame created and linked in --
1638 we should never end up with the sentinel frame as outermost
1640 current_frame
= get_prev_frame_always_1 (sentinel_frame
);
1641 gdb_assert (current_frame
!= NULL
);
1643 return current_frame
;
1646 /* The "selected" stack frame is used by default for local and arg
1647 access. May be zero, for no selected frame. */
1649 static struct frame_info
*selected_frame
;
1652 has_stack_frames (void)
1654 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1657 /* Traceframes are effectively a substitute for the live inferior. */
1658 if (get_traceframe_number () < 0)
1660 /* No current inferior, no frame. */
1661 if (inferior_ptid
== null_ptid
)
1664 thread_info
*tp
= inferior_thread ();
1665 /* Don't try to read from a dead thread. */
1666 if (tp
->state
== THREAD_EXITED
)
1669 /* ... or from a spinning thread. */
1677 /* Return the selected frame. Always non-NULL (unless there isn't an
1678 inferior sufficient for creating a frame) in which case an error is
1682 get_selected_frame (const char *message
)
1684 if (selected_frame
== NULL
)
1686 if (message
!= NULL
&& !has_stack_frames ())
1687 error (("%s"), message
);
1688 /* Hey! Don't trust this. It should really be re-finding the
1689 last selected frame of the currently selected thread. This,
1690 though, is better than nothing. */
1691 select_frame (get_current_frame ());
1693 /* There is always a frame. */
1694 gdb_assert (selected_frame
!= NULL
);
1695 return selected_frame
;
1698 /* If there is a selected frame, return it. Otherwise, return NULL. */
1701 get_selected_frame_if_set (void)
1703 return selected_frame
;
1706 /* This is a variant of get_selected_frame() which can be called when
1707 the inferior does not have a frame; in that case it will return
1708 NULL instead of calling error(). */
1711 deprecated_safe_get_selected_frame (void)
1713 if (!has_stack_frames ())
1715 return get_selected_frame (NULL
);
1718 /* Select frame FI (or NULL - to invalidate the current frame). */
1721 select_frame (struct frame_info
*fi
)
1723 selected_frame
= fi
;
1724 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1725 frame is being invalidated. */
1727 /* FIXME: kseitz/2002-08-28: It would be nice to call
1728 selected_frame_level_changed_event() right here, but due to limitations
1729 in the current interfaces, we would end up flooding UIs with events
1730 because select_frame() is used extensively internally.
1732 Once we have frame-parameterized frame (and frame-related) commands,
1733 the event notification can be moved here, since this function will only
1734 be called when the user's selected frame is being changed. */
1736 /* Ensure that symbols for this frame are read in. Also, determine the
1737 source language of this frame, and switch to it if desired. */
1742 /* We retrieve the frame's symtab by using the frame PC.
1743 However we cannot use the frame PC as-is, because it usually
1744 points to the instruction following the "call", which is
1745 sometimes the first instruction of another function. So we
1746 rely on get_frame_address_in_block() which provides us with a
1747 PC which is guaranteed to be inside the frame's code
1749 if (get_frame_address_in_block_if_available (fi
, &pc
))
1751 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
1754 && compunit_language (cust
) != current_language
->la_language
1755 && compunit_language (cust
) != language_unknown
1756 && language_mode
== language_mode_auto
)
1757 set_language (compunit_language (cust
));
1762 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1763 Always returns a non-NULL value. */
1766 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1768 struct frame_info
*fi
;
1772 fprintf_unfiltered (gdb_stdlog
,
1773 "{ create_new_frame (addr=%s, pc=%s) ",
1774 hex_string (addr
), hex_string (pc
));
1777 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1779 fi
->next
= create_sentinel_frame (current_program_space
,
1780 get_current_regcache ());
1782 /* Set/update this frame's cached PC value, found in the next frame.
1783 Do this before looking for this frame's unwinder. A sniffer is
1784 very likely to read this, and the corresponding unwinder is
1785 entitled to rely that the PC doesn't magically change. */
1786 fi
->next
->prev_pc
.value
= pc
;
1787 fi
->next
->prev_pc
.status
= CC_VALUE
;
1789 /* We currently assume that frame chain's can't cross spaces. */
1790 fi
->pspace
= fi
->next
->pspace
;
1791 fi
->aspace
= fi
->next
->aspace
;
1793 /* Select/initialize both the unwind function and the frame's type
1795 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1798 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1802 fprintf_unfiltered (gdb_stdlog
, "-> ");
1803 fprint_frame (gdb_stdlog
, fi
);
1804 fprintf_unfiltered (gdb_stdlog
, " }\n");
1810 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1811 innermost frame). Be careful to not fall off the bottom of the
1812 frame chain and onto the sentinel frame. */
1815 get_next_frame (struct frame_info
*this_frame
)
1817 if (this_frame
->level
> 0)
1818 return this_frame
->next
;
1823 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1824 innermost (i.e. current) frame, return the sentinel frame. Thus,
1825 unlike get_next_frame(), NULL will never be returned. */
1828 get_next_frame_sentinel_okay (struct frame_info
*this_frame
)
1830 gdb_assert (this_frame
!= NULL
);
1832 /* Note that, due to the manner in which the sentinel frame is
1833 constructed, this_frame->next still works even when this_frame
1834 is the sentinel frame. But we disallow it here anyway because
1835 calling get_next_frame_sentinel_okay() on the sentinel frame
1836 is likely a coding error. */
1837 gdb_assert (this_frame
!= sentinel_frame
);
1839 return this_frame
->next
;
1842 /* Observer for the target_changed event. */
1845 frame_observer_target_changed (struct target_ops
*target
)
1847 reinit_frame_cache ();
1850 /* Flush the entire frame cache. */
1853 reinit_frame_cache (void)
1855 struct frame_info
*fi
;
1857 ++frame_cache_generation
;
1859 /* Tear down all frame caches. */
1860 for (fi
= sentinel_frame
; fi
!= NULL
; fi
= fi
->prev
)
1862 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1863 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1864 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1865 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1868 /* Since we can't really be sure what the first object allocated was. */
1869 obstack_free (&frame_cache_obstack
, 0);
1870 obstack_init (&frame_cache_obstack
);
1872 if (sentinel_frame
!= NULL
)
1873 annotate_frames_invalid ();
1875 sentinel_frame
= NULL
; /* Invalidate cache */
1876 select_frame (NULL
);
1877 frame_stash_invalidate ();
1879 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1882 /* Find where a register is saved (in memory or another register).
1883 The result of frame_register_unwind is just where it is saved
1884 relative to this particular frame. */
1887 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1888 int *optimizedp
, enum lval_type
*lvalp
,
1889 CORE_ADDR
*addrp
, int *realnump
)
1891 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1893 while (this_frame
!= NULL
)
1897 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1898 lvalp
, addrp
, realnump
, NULL
);
1903 if (*lvalp
!= lval_register
)
1907 this_frame
= get_next_frame (this_frame
);
1911 /* Get the previous raw frame, and check that it is not identical to
1912 same other frame frame already in the chain. If it is, there is
1913 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1914 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1915 validity tests, that compare THIS_FRAME and the next frame, we do
1916 this right after creating the previous frame, to avoid ever ending
1917 up with two frames with the same id in the frame chain. */
1919 static struct frame_info
*
1920 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1922 struct frame_info
*prev_frame
;
1924 prev_frame
= get_prev_frame_raw (this_frame
);
1926 /* Don't compute the frame id of the current frame yet. Unwinding
1927 the sentinel frame can fail (e.g., if the thread is gone and we
1928 can't thus read its registers). If we let the cycle detection
1929 code below try to compute a frame ID, then an error thrown from
1930 within the frame ID computation would result in the sentinel
1931 frame as outermost frame, which is bogus. Instead, we'll compute
1932 the current frame's ID lazily in get_frame_id. Note that there's
1933 no point in doing cycle detection when there's only one frame, so
1934 nothing is lost here. */
1935 if (prev_frame
->level
== 0)
1938 unsigned int entry_generation
= get_frame_cache_generation ();
1942 compute_frame_id (prev_frame
);
1943 if (!frame_stash_add (prev_frame
))
1945 /* Another frame with the same id was already in the stash. We just
1946 detected a cycle. */
1949 fprintf_unfiltered (gdb_stdlog
, "-> ");
1950 fprint_frame (gdb_stdlog
, NULL
);
1951 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1953 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1955 prev_frame
->next
= NULL
;
1956 this_frame
->prev
= NULL
;
1960 catch (const gdb_exception
&ex
)
1962 if (get_frame_cache_generation () == entry_generation
)
1964 prev_frame
->next
= NULL
;
1965 this_frame
->prev
= NULL
;
1974 /* Helper function for get_prev_frame_always, this is called inside a
1975 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
1976 there is no such frame. This may throw an exception. */
1978 static struct frame_info
*
1979 get_prev_frame_always_1 (struct frame_info
*this_frame
)
1981 struct gdbarch
*gdbarch
;
1983 gdb_assert (this_frame
!= NULL
);
1984 gdbarch
= get_frame_arch (this_frame
);
1988 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_always (this_frame=");
1989 if (this_frame
!= NULL
)
1990 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1992 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1993 fprintf_unfiltered (gdb_stdlog
, ") ");
1996 /* Only try to do the unwind once. */
1997 if (this_frame
->prev_p
)
2001 fprintf_unfiltered (gdb_stdlog
, "-> ");
2002 fprint_frame (gdb_stdlog
, this_frame
->prev
);
2003 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
2005 return this_frame
->prev
;
2008 /* If the frame unwinder hasn't been selected yet, we must do so
2009 before setting prev_p; otherwise the check for misbehaved
2010 sniffers will think that this frame's sniffer tried to unwind
2011 further (see frame_cleanup_after_sniffer). */
2012 if (this_frame
->unwind
== NULL
)
2013 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
2015 this_frame
->prev_p
= 1;
2016 this_frame
->stop_reason
= UNWIND_NO_REASON
;
2018 /* If we are unwinding from an inline frame, all of the below tests
2019 were already performed when we unwound from the next non-inline
2020 frame. We must skip them, since we can not get THIS_FRAME's ID
2021 until we have unwound all the way down to the previous non-inline
2023 if (get_frame_type (this_frame
) == INLINE_FRAME
)
2024 return get_prev_frame_if_no_cycle (this_frame
);
2026 /* Check that this frame is unwindable. If it isn't, don't try to
2027 unwind to the prev frame. */
2028 this_frame
->stop_reason
2029 = this_frame
->unwind
->stop_reason (this_frame
,
2030 &this_frame
->prologue_cache
);
2032 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
2036 enum unwind_stop_reason reason
= this_frame
->stop_reason
;
2038 fprintf_unfiltered (gdb_stdlog
, "-> ");
2039 fprint_frame (gdb_stdlog
, NULL
);
2040 fprintf_unfiltered (gdb_stdlog
, " // %s }\n",
2041 frame_stop_reason_symbol_string (reason
));
2046 /* Check that this frame's ID isn't inner to (younger, below, next)
2047 the next frame. This happens when a frame unwind goes backwards.
2048 This check is valid only if this frame and the next frame are NORMAL.
2049 See the comment at frame_id_inner for details. */
2050 if (get_frame_type (this_frame
) == NORMAL_FRAME
2051 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
2052 && frame_id_inner (get_frame_arch (this_frame
->next
),
2053 get_frame_id (this_frame
),
2054 get_frame_id (this_frame
->next
)))
2056 CORE_ADDR this_pc_in_block
;
2057 struct minimal_symbol
*morestack_msym
;
2058 const char *morestack_name
= NULL
;
2060 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2061 this_pc_in_block
= get_frame_address_in_block (this_frame
);
2062 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
2064 morestack_name
= morestack_msym
->linkage_name ();
2065 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
2069 fprintf_unfiltered (gdb_stdlog
, "-> ");
2070 fprint_frame (gdb_stdlog
, NULL
);
2071 fprintf_unfiltered (gdb_stdlog
,
2072 " // this frame ID is inner }\n");
2074 this_frame
->stop_reason
= UNWIND_INNER_ID
;
2079 /* Check that this and the next frame do not unwind the PC register
2080 to the same memory location. If they do, then even though they
2081 have different frame IDs, the new frame will be bogus; two
2082 functions can't share a register save slot for the PC. This can
2083 happen when the prologue analyzer finds a stack adjustment, but
2086 This check does assume that the "PC register" is roughly a
2087 traditional PC, even if the gdbarch_unwind_pc method adjusts
2088 it (we do not rely on the value, only on the unwound PC being
2089 dependent on this value). A potential improvement would be
2090 to have the frame prev_pc method and the gdbarch unwind_pc
2091 method set the same lval and location information as
2092 frame_register_unwind. */
2093 if (this_frame
->level
> 0
2094 && gdbarch_pc_regnum (gdbarch
) >= 0
2095 && get_frame_type (this_frame
) == NORMAL_FRAME
2096 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
2097 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
2099 int optimized
, realnum
, nrealnum
;
2100 enum lval_type lval
, nlval
;
2101 CORE_ADDR addr
, naddr
;
2103 frame_register_unwind_location (this_frame
,
2104 gdbarch_pc_regnum (gdbarch
),
2105 &optimized
, &lval
, &addr
, &realnum
);
2106 frame_register_unwind_location (get_next_frame (this_frame
),
2107 gdbarch_pc_regnum (gdbarch
),
2108 &optimized
, &nlval
, &naddr
, &nrealnum
);
2110 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
2111 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
2115 fprintf_unfiltered (gdb_stdlog
, "-> ");
2116 fprint_frame (gdb_stdlog
, NULL
);
2117 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
2120 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2121 this_frame
->prev
= NULL
;
2126 return get_prev_frame_if_no_cycle (this_frame
);
2129 /* Return a "struct frame_info" corresponding to the frame that called
2130 THIS_FRAME. Returns NULL if there is no such frame.
2132 Unlike get_prev_frame, this function always tries to unwind the
2136 get_prev_frame_always (struct frame_info
*this_frame
)
2138 struct frame_info
*prev_frame
= NULL
;
2142 prev_frame
= get_prev_frame_always_1 (this_frame
);
2144 catch (const gdb_exception_error
&ex
)
2146 if (ex
.error
== MEMORY_ERROR
)
2148 this_frame
->stop_reason
= UNWIND_MEMORY_ERROR
;
2149 if (ex
.message
!= NULL
)
2154 /* The error needs to live as long as the frame does.
2155 Allocate using stack local STOP_STRING then assign the
2156 pointer to the frame, this allows the STOP_STRING on the
2157 frame to be of type 'const char *'. */
2158 size
= ex
.message
->size () + 1;
2159 stop_string
= (char *) frame_obstack_zalloc (size
);
2160 memcpy (stop_string
, ex
.what (), size
);
2161 this_frame
->stop_string
= stop_string
;
2172 /* Construct a new "struct frame_info" and link it previous to
2175 static struct frame_info
*
2176 get_prev_frame_raw (struct frame_info
*this_frame
)
2178 struct frame_info
*prev_frame
;
2180 /* Allocate the new frame but do not wire it in to the frame chain.
2181 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2182 frame->next to pull some fancy tricks (of course such code is, by
2183 definition, recursive). Try to prevent it.
2185 There is no reason to worry about memory leaks, should the
2186 remainder of the function fail. The allocated memory will be
2187 quickly reclaimed when the frame cache is flushed, and the `we've
2188 been here before' check above will stop repeated memory
2189 allocation calls. */
2190 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2191 prev_frame
->level
= this_frame
->level
+ 1;
2193 /* For now, assume we don't have frame chains crossing address
2195 prev_frame
->pspace
= this_frame
->pspace
;
2196 prev_frame
->aspace
= this_frame
->aspace
;
2198 /* Don't yet compute ->unwind (and hence ->type). It is computed
2199 on-demand in get_frame_type, frame_register_unwind, and
2202 /* Don't yet compute the frame's ID. It is computed on-demand by
2205 /* The unwound frame ID is validate at the start of this function,
2206 as part of the logic to decide if that frame should be further
2207 unwound, and not here while the prev frame is being created.
2208 Doing this makes it possible for the user to examine a frame that
2209 has an invalid frame ID.
2211 Some very old VAX code noted: [...] For the sake of argument,
2212 suppose that the stack is somewhat trashed (which is one reason
2213 that "info frame" exists). So, return 0 (indicating we don't
2214 know the address of the arglist) if we don't know what frame this
2218 this_frame
->prev
= prev_frame
;
2219 prev_frame
->next
= this_frame
;
2223 fprintf_unfiltered (gdb_stdlog
, "-> ");
2224 fprint_frame (gdb_stdlog
, prev_frame
);
2225 fprintf_unfiltered (gdb_stdlog
, " }\n");
2231 /* Debug routine to print a NULL frame being returned. */
2234 frame_debug_got_null_frame (struct frame_info
*this_frame
,
2239 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
2240 if (this_frame
!= NULL
)
2241 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2243 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2244 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
2248 /* Is this (non-sentinel) frame in the "main"() function? */
2251 inside_main_func (struct frame_info
*this_frame
)
2253 struct bound_minimal_symbol msymbol
;
2256 if (symfile_objfile
== 0)
2258 msymbol
= lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
2259 if (msymbol
.minsym
== NULL
)
2261 /* Make certain that the code, and not descriptor, address is
2263 maddr
= gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
2264 BMSYMBOL_VALUE_ADDRESS (msymbol
),
2265 current_top_target ());
2266 return maddr
== get_frame_func (this_frame
);
2269 /* Test whether THIS_FRAME is inside the process entry point function. */
2272 inside_entry_func (struct frame_info
*this_frame
)
2274 CORE_ADDR entry_point
;
2276 if (!entry_point_address_query (&entry_point
))
2279 return get_frame_func (this_frame
) == entry_point
;
2282 /* Return a structure containing various interesting information about
2283 the frame that called THIS_FRAME. Returns NULL if there is entier
2284 no such frame or the frame fails any of a set of target-independent
2285 condition that should terminate the frame chain (e.g., as unwinding
2288 This function should not contain target-dependent tests, such as
2289 checking whether the program-counter is zero. */
2292 get_prev_frame (struct frame_info
*this_frame
)
2297 /* There is always a frame. If this assertion fails, suspect that
2298 something should be calling get_selected_frame() or
2299 get_current_frame(). */
2300 gdb_assert (this_frame
!= NULL
);
2302 /* If this_frame is the current frame, then compute and stash
2303 its frame id prior to fetching and computing the frame id of the
2304 previous frame. Otherwise, the cycle detection code in
2305 get_prev_frame_if_no_cycle() will not work correctly. When
2306 get_frame_id() is called later on, an assertion error will
2307 be triggered in the event of a cycle between the current
2308 frame and its previous frame. */
2309 if (this_frame
->level
== 0)
2310 get_frame_id (this_frame
);
2312 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2314 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2315 sense to stop unwinding at a dummy frame. One place where a dummy
2316 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2317 pcsqh register (space register for the instruction at the head of the
2318 instruction queue) cannot be written directly; the only way to set it
2319 is to branch to code that is in the target space. In order to implement
2320 frame dummies on HPUX, the called function is made to jump back to where
2321 the inferior was when the user function was called. If gdb was inside
2322 the main function when we created the dummy frame, the dummy frame will
2323 point inside the main function. */
2324 if (this_frame
->level
>= 0
2325 && get_frame_type (this_frame
) == NORMAL_FRAME
2326 && !user_set_backtrace_options
.backtrace_past_main
2328 && inside_main_func (this_frame
))
2329 /* Don't unwind past main(). Note, this is done _before_ the
2330 frame has been marked as previously unwound. That way if the
2331 user later decides to enable unwinds past main(), that will
2332 automatically happen. */
2334 frame_debug_got_null_frame (this_frame
, "inside main func");
2338 /* If the user's backtrace limit has been exceeded, stop. We must
2339 add two to the current level; one of those accounts for backtrace_limit
2340 being 1-based and the level being 0-based, and the other accounts for
2341 the level of the new frame instead of the level of the current
2343 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2345 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2349 /* If we're already inside the entry function for the main objfile,
2350 then it isn't valid. Don't apply this test to a dummy frame -
2351 dummy frame PCs typically land in the entry func. Don't apply
2352 this test to the sentinel frame. Sentinel frames should always
2353 be allowed to unwind. */
2354 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2355 wasn't checking for "main" in the minimal symbols. With that
2356 fixed asm-source tests now stop in "main" instead of halting the
2357 backtrace in weird and wonderful ways somewhere inside the entry
2358 file. Suspect that tests for inside the entry file/func were
2359 added to work around that (now fixed) case. */
2360 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2361 suggested having the inside_entry_func test use the
2362 inside_main_func() msymbol trick (along with entry_point_address()
2363 I guess) to determine the address range of the start function.
2364 That should provide a far better stopper than the current
2366 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2367 applied tail-call optimizations to main so that a function called
2368 from main returns directly to the caller of main. Since we don't
2369 stop at main, we should at least stop at the entry point of the
2371 if (this_frame
->level
>= 0
2372 && get_frame_type (this_frame
) == NORMAL_FRAME
2373 && !user_set_backtrace_options
.backtrace_past_entry
2375 && inside_entry_func (this_frame
))
2377 frame_debug_got_null_frame (this_frame
, "inside entry func");
2381 /* Assume that the only way to get a zero PC is through something
2382 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2383 will never unwind a zero PC. */
2384 if (this_frame
->level
> 0
2385 && (get_frame_type (this_frame
) == NORMAL_FRAME
2386 || get_frame_type (this_frame
) == INLINE_FRAME
)
2387 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2388 && frame_pc_p
&& frame_pc
== 0)
2390 frame_debug_got_null_frame (this_frame
, "zero PC");
2394 return get_prev_frame_always (this_frame
);
2398 get_prev_frame_id_by_id (struct frame_id id
)
2400 struct frame_id prev_id
;
2401 struct frame_info
*frame
;
2403 frame
= frame_find_by_id (id
);
2406 prev_id
= get_frame_id (get_prev_frame (frame
));
2408 prev_id
= null_frame_id
;
2414 get_frame_pc (struct frame_info
*frame
)
2416 gdb_assert (frame
->next
!= NULL
);
2417 return frame_unwind_pc (frame
->next
);
2421 get_frame_pc_if_available (struct frame_info
*frame
, CORE_ADDR
*pc
)
2424 gdb_assert (frame
->next
!= NULL
);
2428 *pc
= frame_unwind_pc (frame
->next
);
2430 catch (const gdb_exception_error
&ex
)
2432 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2441 /* Return an address that falls within THIS_FRAME's code block. */
2444 get_frame_address_in_block (struct frame_info
*this_frame
)
2446 /* A draft address. */
2447 CORE_ADDR pc
= get_frame_pc (this_frame
);
2449 struct frame_info
*next_frame
= this_frame
->next
;
2451 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2452 Normally the resume address is inside the body of the function
2453 associated with THIS_FRAME, but there is a special case: when
2454 calling a function which the compiler knows will never return
2455 (for instance abort), the call may be the very last instruction
2456 in the calling function. The resume address will point after the
2457 call and may be at the beginning of a different function
2460 If THIS_FRAME is a signal frame or dummy frame, then we should
2461 not adjust the unwound PC. For a dummy frame, GDB pushed the
2462 resume address manually onto the stack. For a signal frame, the
2463 OS may have pushed the resume address manually and invoked the
2464 handler (e.g. GNU/Linux), or invoked the trampoline which called
2465 the signal handler - but in either case the signal handler is
2466 expected to return to the trampoline. So in both of these
2467 cases we know that the resume address is executable and
2468 related. So we only need to adjust the PC if THIS_FRAME
2469 is a normal function.
2471 If the program has been interrupted while THIS_FRAME is current,
2472 then clearly the resume address is inside the associated
2473 function. There are three kinds of interruption: debugger stop
2474 (next frame will be SENTINEL_FRAME), operating system
2475 signal or exception (next frame will be SIGTRAMP_FRAME),
2476 or debugger-induced function call (next frame will be
2477 DUMMY_FRAME). So we only need to adjust the PC if
2478 NEXT_FRAME is a normal function.
2480 We check the type of NEXT_FRAME first, since it is already
2481 known; frame type is determined by the unwinder, and since
2482 we have THIS_FRAME we've already selected an unwinder for
2485 If the next frame is inlined, we need to keep going until we find
2486 the real function - for instance, if a signal handler is invoked
2487 while in an inlined function, then the code address of the
2488 "calling" normal function should not be adjusted either. */
2490 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2491 next_frame
= next_frame
->next
;
2493 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2494 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2495 && (get_frame_type (this_frame
) == NORMAL_FRAME
2496 || get_frame_type (this_frame
) == TAILCALL_FRAME
2497 || get_frame_type (this_frame
) == INLINE_FRAME
))
2504 get_frame_address_in_block_if_available (struct frame_info
*this_frame
,
2510 *pc
= get_frame_address_in_block (this_frame
);
2512 catch (const gdb_exception_error
&ex
)
2514 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2523 find_frame_sal (frame_info
*frame
)
2525 struct frame_info
*next_frame
;
2529 if (frame_inlined_callees (frame
) > 0)
2533 /* If the current frame has some inlined callees, and we have a next
2534 frame, then that frame must be an inlined frame. In this case
2535 this frame's sal is the "call site" of the next frame's inlined
2536 function, which can not be inferred from get_frame_pc. */
2537 next_frame
= get_next_frame (frame
);
2539 sym
= get_frame_function (next_frame
);
2541 sym
= inline_skipped_symbol (inferior_thread ());
2543 /* If frame is inline, it certainly has symbols. */
2546 symtab_and_line sal
;
2547 if (SYMBOL_LINE (sym
) != 0)
2549 sal
.symtab
= symbol_symtab (sym
);
2550 sal
.line
= SYMBOL_LINE (sym
);
2553 /* If the symbol does not have a location, we don't know where
2554 the call site is. Do not pretend to. This is jarring, but
2555 we can't do much better. */
2556 sal
.pc
= get_frame_pc (frame
);
2558 sal
.pspace
= get_frame_program_space (frame
);
2562 /* If FRAME is not the innermost frame, that normally means that
2563 FRAME->pc points at the return instruction (which is *after* the
2564 call instruction), and we want to get the line containing the
2565 call (because the call is where the user thinks the program is).
2566 However, if the next frame is either a SIGTRAMP_FRAME or a
2567 DUMMY_FRAME, then the next frame will contain a saved interrupt
2568 PC and such a PC indicates the current (rather than next)
2569 instruction/line, consequently, for such cases, want to get the
2570 line containing fi->pc. */
2571 if (!get_frame_pc_if_available (frame
, &pc
))
2574 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2575 return find_pc_line (pc
, notcurrent
);
2578 /* Per "frame.h", return the ``address'' of the frame. Code should
2579 really be using get_frame_id(). */
2581 get_frame_base (struct frame_info
*fi
)
2583 return get_frame_id (fi
).stack_addr
;
2586 /* High-level offsets into the frame. Used by the debug info. */
2589 get_frame_base_address (struct frame_info
*fi
)
2591 if (get_frame_type (fi
) != NORMAL_FRAME
)
2593 if (fi
->base
== NULL
)
2594 fi
->base
= frame_base_find_by_frame (fi
);
2595 /* Sneaky: If the low-level unwind and high-level base code share a
2596 common unwinder, let them share the prologue cache. */
2597 if (fi
->base
->unwind
== fi
->unwind
)
2598 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2599 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2603 get_frame_locals_address (struct frame_info
*fi
)
2605 if (get_frame_type (fi
) != NORMAL_FRAME
)
2607 /* If there isn't a frame address method, find it. */
2608 if (fi
->base
== NULL
)
2609 fi
->base
= frame_base_find_by_frame (fi
);
2610 /* Sneaky: If the low-level unwind and high-level base code share a
2611 common unwinder, let them share the prologue cache. */
2612 if (fi
->base
->unwind
== fi
->unwind
)
2613 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2614 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2618 get_frame_args_address (struct frame_info
*fi
)
2620 if (get_frame_type (fi
) != NORMAL_FRAME
)
2622 /* If there isn't a frame address method, find it. */
2623 if (fi
->base
== NULL
)
2624 fi
->base
= frame_base_find_by_frame (fi
);
2625 /* Sneaky: If the low-level unwind and high-level base code share a
2626 common unwinder, let them share the prologue cache. */
2627 if (fi
->base
->unwind
== fi
->unwind
)
2628 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2629 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2632 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2636 frame_unwinder_is (struct frame_info
*fi
, const struct frame_unwind
*unwinder
)
2638 if (fi
->unwind
== NULL
)
2639 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2640 return fi
->unwind
== unwinder
;
2643 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2644 or -1 for a NULL frame. */
2647 frame_relative_level (struct frame_info
*fi
)
2656 get_frame_type (struct frame_info
*frame
)
2658 if (frame
->unwind
== NULL
)
2659 /* Initialize the frame's unwinder because that's what
2660 provides the frame's type. */
2661 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2662 return frame
->unwind
->type
;
2665 struct program_space
*
2666 get_frame_program_space (struct frame_info
*frame
)
2668 return frame
->pspace
;
2671 struct program_space
*
2672 frame_unwind_program_space (struct frame_info
*this_frame
)
2674 gdb_assert (this_frame
);
2676 /* This is really a placeholder to keep the API consistent --- we
2677 assume for now that we don't have frame chains crossing
2679 return this_frame
->pspace
;
2682 const address_space
*
2683 get_frame_address_space (struct frame_info
*frame
)
2685 return frame
->aspace
;
2688 /* Memory access methods. */
2691 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2692 gdb_byte
*buf
, int len
)
2694 read_memory (addr
, buf
, len
);
2698 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2701 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2702 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2704 return read_memory_integer (addr
, len
, byte_order
);
2708 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2711 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2712 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2714 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2718 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2719 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2721 /* NOTE: target_read_memory returns zero on success! */
2722 return !target_read_memory (addr
, buf
, len
);
2725 /* Architecture methods. */
2728 get_frame_arch (struct frame_info
*this_frame
)
2730 return frame_unwind_arch (this_frame
->next
);
2734 frame_unwind_arch (struct frame_info
*next_frame
)
2736 if (!next_frame
->prev_arch
.p
)
2738 struct gdbarch
*arch
;
2740 if (next_frame
->unwind
== NULL
)
2741 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2743 if (next_frame
->unwind
->prev_arch
!= NULL
)
2744 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2745 &next_frame
->prologue_cache
);
2747 arch
= get_frame_arch (next_frame
);
2749 next_frame
->prev_arch
.arch
= arch
;
2750 next_frame
->prev_arch
.p
= 1;
2752 fprintf_unfiltered (gdb_stdlog
,
2753 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2755 gdbarch_bfd_arch_info (arch
)->printable_name
);
2758 return next_frame
->prev_arch
.arch
;
2762 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2764 next_frame
= skip_artificial_frames (next_frame
);
2766 /* We must have a non-artificial frame. The caller is supposed to check
2767 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2769 gdb_assert (next_frame
!= NULL
);
2771 return frame_unwind_arch (next_frame
);
2774 /* Gets the language of FRAME. */
2777 get_frame_language (struct frame_info
*frame
)
2782 gdb_assert (frame
!= NULL
);
2784 /* We determine the current frame language by looking up its
2785 associated symtab. To retrieve this symtab, we use the frame
2786 PC. However we cannot use the frame PC as is, because it
2787 usually points to the instruction following the "call", which
2788 is sometimes the first instruction of another function. So
2789 we rely on get_frame_address_in_block(), it provides us with
2790 a PC that is guaranteed to be inside the frame's code
2795 pc
= get_frame_address_in_block (frame
);
2798 catch (const gdb_exception_error
&ex
)
2800 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
2806 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
2809 return compunit_language (cust
);
2812 return language_unknown
;
2815 /* Stack pointer methods. */
2818 get_frame_sp (struct frame_info
*this_frame
)
2820 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2822 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2823 operate on THIS_FRAME now. */
2824 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2827 /* Return the reason why we can't unwind past FRAME. */
2829 enum unwind_stop_reason
2830 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2832 /* Fill-in STOP_REASON. */
2833 get_prev_frame_always (frame
);
2834 gdb_assert (frame
->prev_p
);
2836 return frame
->stop_reason
;
2839 /* Return a string explaining REASON. */
2842 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
2846 #define SET(name, description) \
2847 case name: return _(description);
2848 #include "unwind_stop_reasons.def"
2852 internal_error (__FILE__
, __LINE__
,
2853 "Invalid frame stop reason");
2858 frame_stop_reason_string (struct frame_info
*fi
)
2860 gdb_assert (fi
->prev_p
);
2861 gdb_assert (fi
->prev
== NULL
);
2863 /* Return the specific string if we have one. */
2864 if (fi
->stop_string
!= NULL
)
2865 return fi
->stop_string
;
2867 /* Return the generic string if we have nothing better. */
2868 return unwind_stop_reason_to_string (fi
->stop_reason
);
2871 /* Return the enum symbol name of REASON as a string, to use in debug
2875 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
2879 #define SET(name, description) \
2880 case name: return #name;
2881 #include "unwind_stop_reasons.def"
2885 internal_error (__FILE__
, __LINE__
,
2886 "Invalid frame stop reason");
2890 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2894 frame_cleanup_after_sniffer (struct frame_info
*frame
)
2896 /* The sniffer should not allocate a prologue cache if it did not
2897 match this frame. */
2898 gdb_assert (frame
->prologue_cache
== NULL
);
2900 /* No sniffer should extend the frame chain; sniff based on what is
2902 gdb_assert (!frame
->prev_p
);
2904 /* The sniffer should not check the frame's ID; that's circular. */
2905 gdb_assert (!frame
->this_id
.p
);
2907 /* Clear cached fields dependent on the unwinder.
2909 The previous PC is independent of the unwinder, but the previous
2910 function is not (see get_frame_address_in_block). */
2911 frame
->prev_func
.p
= 0;
2912 frame
->prev_func
.addr
= 0;
2914 /* Discard the unwinder last, so that we can easily find it if an assertion
2915 in this function triggers. */
2916 frame
->unwind
= NULL
;
2919 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2920 If sniffing fails, the caller should be sure to call
2921 frame_cleanup_after_sniffer. */
2924 frame_prepare_for_sniffer (struct frame_info
*frame
,
2925 const struct frame_unwind
*unwind
)
2927 gdb_assert (frame
->unwind
== NULL
);
2928 frame
->unwind
= unwind
;
2931 static struct cmd_list_element
*set_backtrace_cmdlist
;
2932 static struct cmd_list_element
*show_backtrace_cmdlist
;
2934 /* Definition of the "set backtrace" settings that are exposed as
2935 "backtrace" command options. */
2937 using boolean_option_def
2938 = gdb::option::boolean_option_def
<set_backtrace_options
>;
2939 using uinteger_option_def
2940 = gdb::option::uinteger_option_def
<set_backtrace_options
>;
2942 const gdb::option::option_def set_backtrace_option_defs
[] = {
2944 boolean_option_def
{
2946 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_main
; },
2947 show_backtrace_past_main
, /* show_cmd_cb */
2948 N_("Set whether backtraces should continue past \"main\"."),
2949 N_("Show whether backtraces should continue past \"main\"."),
2950 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2951 the backtrace at \"main\". Set this if you need to see the rest\n\
2952 of the stack trace."),
2955 boolean_option_def
{
2957 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_entry
; },
2958 show_backtrace_past_entry
, /* show_cmd_cb */
2959 N_("Set whether backtraces should continue past the entry point of a program."),
2960 N_("Show whether backtraces should continue past the entry point of a program."),
2961 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
2962 will terminate the backtrace there. Set this if you need to see\n\
2963 the rest of the stack trace."),
2967 void _initialize_frame ();
2969 _initialize_frame ()
2971 obstack_init (&frame_cache_obstack
);
2973 frame_stash_create ();
2975 gdb::observers::target_changed
.attach (frame_observer_target_changed
);
2977 add_basic_prefix_cmd ("backtrace", class_maintenance
, _("\
2978 Set backtrace specific variables.\n\
2979 Configure backtrace variables such as the backtrace limit"),
2980 &set_backtrace_cmdlist
, "set backtrace ",
2981 0/*allow-unknown*/, &setlist
);
2982 add_show_prefix_cmd ("backtrace", class_maintenance
, _("\
2983 Show backtrace specific variables.\n\
2984 Show backtrace variables such as the backtrace limit."),
2985 &show_backtrace_cmdlist
, "show backtrace ",
2986 0/*allow-unknown*/, &showlist
);
2988 add_setshow_uinteger_cmd ("limit", class_obscure
,
2989 &user_set_backtrace_options
.backtrace_limit
, _("\
2990 Set an upper bound on the number of backtrace levels."), _("\
2991 Show the upper bound on the number of backtrace levels."), _("\
2992 No more than the specified number of frames can be displayed or examined.\n\
2993 Literal \"unlimited\" or zero means no limit."),
2995 show_backtrace_limit
,
2996 &set_backtrace_cmdlist
,
2997 &show_backtrace_cmdlist
);
2999 gdb::option::add_setshow_cmds_for_options
3000 (class_stack
, &user_set_backtrace_options
,
3001 set_backtrace_option_defs
, &set_backtrace_cmdlist
, &show_backtrace_cmdlist
);
3003 /* Debug this files internals. */
3004 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
3005 Set frame debugging."), _("\
3006 Show frame debugging."), _("\
3007 When non-zero, frame specific internal debugging is enabled."),
3010 &setdebuglist
, &showdebuglist
);