1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "inferior.h" /* for inferior_ptid */
27 #include "user-regs.h"
28 #include "gdb_obstack.h"
29 #include "dummy-frame.h"
30 #include "sentinel-frame.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
38 #include "observable.h"
40 #include "gdbthread.h"
42 #include "inline-frame.h"
43 #include "tracepoint.h"
46 #include "cli/cli-option.h"
48 /* The sentinel frame terminates the innermost end of the frame chain.
49 If unwound, it returns the information needed to construct an
52 The current frame, which is the innermost frame, can be found at
53 sentinel_frame->prev. */
55 static struct frame_info
*sentinel_frame
;
57 /* The values behind the global "set backtrace ..." settings. */
58 set_backtrace_options user_set_backtrace_options
;
60 static struct frame_info
*get_prev_frame_raw (struct frame_info
*this_frame
);
61 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason
);
63 /* Status of some values cached in the frame_info object. */
65 enum cached_copy_status
67 /* Value is unknown. */
70 /* We have a value. */
73 /* Value was not saved. */
76 /* Value is unavailable. */
80 /* We keep a cache of stack frames, each of which is a "struct
81 frame_info". The innermost one gets allocated (in
82 wait_for_inferior) each time the inferior stops; sentinel_frame
83 points to it. Additional frames get allocated (in get_prev_frame)
84 as needed, and are chained through the next and prev fields. Any
85 time that the frame cache becomes invalid (most notably when we
86 execute something, but also if we change how we interpret the
87 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
88 which reads new symbols)), we should call reinit_frame_cache. */
92 /* Level of this frame. The inner-most (youngest) frame is at level
93 0. As you move towards the outer-most (oldest) frame, the level
94 increases. This is a cached value. It could just as easily be
95 computed by counting back from the selected frame to the inner
97 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
98 reserved to indicate a bogus frame - one that has been created
99 just to keep GDB happy (GDB always needs a frame). For the
100 moment leave this as speculation. */
103 /* The frame's program space. */
104 struct program_space
*pspace
;
106 /* The frame's address space. */
107 const address_space
*aspace
;
109 /* The frame's low-level unwinder and corresponding cache. The
110 low-level unwinder is responsible for unwinding register values
111 for the previous frame. The low-level unwind methods are
112 selected based on the presence, or otherwise, of register unwind
113 information such as CFI. */
114 void *prologue_cache
;
115 const struct frame_unwind
*unwind
;
117 /* Cached copy of the previous frame's architecture. */
121 struct gdbarch
*arch
;
124 /* Cached copy of the previous frame's resume address. */
126 enum cached_copy_status status
;
127 /* Did VALUE require unmasking when being read. */
132 /* Cached copy of the previous frame's function address. */
139 /* This frame's ID. */
143 struct frame_id value
;
146 /* The frame's high-level base methods, and corresponding cache.
147 The high level base methods are selected based on the frame's
149 const struct frame_base
*base
;
152 /* Pointers to the next (down, inner, younger) and previous (up,
153 outer, older) frame_info's in the frame cache. */
154 struct frame_info
*next
; /* down, inner, younger */
156 struct frame_info
*prev
; /* up, outer, older */
158 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
159 could. Only valid when PREV_P is set. */
160 enum unwind_stop_reason stop_reason
;
162 /* A frame specific string describing the STOP_REASON in more detail.
163 Only valid when PREV_P is set, but even then may still be NULL. */
164 const char *stop_string
;
170 set_frame_previous_pc_masked (struct frame_info
*frame
)
172 frame
->prev_pc
.masked
= true;
178 get_frame_pc_masked (const struct frame_info
*frame
)
180 gdb_assert (frame
->next
!= nullptr);
181 gdb_assert (frame
->next
->prev_pc
.status
== CC_VALUE
);
183 return frame
->next
->prev_pc
.masked
;
186 /* A frame stash used to speed up frame lookups. Create a hash table
187 to stash frames previously accessed from the frame cache for
188 quicker subsequent retrieval. The hash table is emptied whenever
189 the frame cache is invalidated. */
191 static htab_t frame_stash
;
193 /* Internal function to calculate a hash from the frame_id addresses,
194 using as many valid addresses as possible. Frames below level 0
195 are not stored in the hash table. */
198 frame_addr_hash (const void *ap
)
200 const struct frame_info
*frame
= (const struct frame_info
*) ap
;
201 const struct frame_id f_id
= frame
->this_id
.value
;
204 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
206 || f_id
.special_addr_p
);
208 if (f_id
.stack_status
== FID_STACK_VALID
)
209 hash
= iterative_hash (&f_id
.stack_addr
,
210 sizeof (f_id
.stack_addr
), hash
);
211 if (f_id
.code_addr_p
)
212 hash
= iterative_hash (&f_id
.code_addr
,
213 sizeof (f_id
.code_addr
), hash
);
214 if (f_id
.special_addr_p
)
215 hash
= iterative_hash (&f_id
.special_addr
,
216 sizeof (f_id
.special_addr
), hash
);
221 /* Internal equality function for the hash table. This function
222 defers equality operations to frame_id_eq. */
225 frame_addr_hash_eq (const void *a
, const void *b
)
227 const struct frame_info
*f_entry
= (const struct frame_info
*) a
;
228 const struct frame_info
*f_element
= (const struct frame_info
*) b
;
230 return frame_id_eq (f_entry
->this_id
.value
,
231 f_element
->this_id
.value
);
234 /* Internal function to create the frame_stash hash table. 100 seems
235 to be a good compromise to start the hash table at. */
238 frame_stash_create (void)
240 frame_stash
= htab_create (100,
246 /* Internal function to add a frame to the frame_stash hash table.
247 Returns false if a frame with the same ID was already stashed, true
251 frame_stash_add (struct frame_info
*frame
)
253 struct frame_info
**slot
;
255 /* Do not try to stash the sentinel frame. */
256 gdb_assert (frame
->level
>= 0);
258 slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
262 /* If we already have a frame in the stack with the same id, we
263 either have a stack cycle (corrupted stack?), or some bug
264 elsewhere in GDB. In any case, ignore the duplicate and return
265 an indication to the caller. */
273 /* Internal function to search the frame stash for an entry with the
274 given frame ID. If found, return that frame. Otherwise return
277 static struct frame_info
*
278 frame_stash_find (struct frame_id id
)
280 struct frame_info dummy
;
281 struct frame_info
*frame
;
283 dummy
.this_id
.value
= id
;
284 frame
= (struct frame_info
*) htab_find (frame_stash
, &dummy
);
288 /* Internal function to invalidate the frame stash by removing all
289 entries in it. This only occurs when the frame cache is
293 frame_stash_invalidate (void)
295 htab_empty (frame_stash
);
299 scoped_restore_selected_frame::scoped_restore_selected_frame ()
301 m_fid
= get_frame_id (get_selected_frame (NULL
));
305 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
307 frame_info
*frame
= frame_find_by_id (m_fid
);
309 warning (_("Unable to restore previously selected frame."));
311 select_frame (frame
);
314 /* Flag to control debugging. */
316 unsigned int frame_debug
;
318 show_frame_debug (struct ui_file
*file
, int from_tty
,
319 struct cmd_list_element
*c
, const char *value
)
321 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
324 /* Implementation of "show backtrace past-main". */
327 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
328 struct cmd_list_element
*c
, const char *value
)
330 fprintf_filtered (file
,
331 _("Whether backtraces should "
332 "continue past \"main\" is %s.\n"),
336 /* Implementation of "show backtrace past-entry". */
339 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
340 struct cmd_list_element
*c
, const char *value
)
342 fprintf_filtered (file
, _("Whether backtraces should continue past the "
343 "entry point of a program is %s.\n"),
347 /* Implementation of "show backtrace limit". */
350 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
351 struct cmd_list_element
*c
, const char *value
)
353 fprintf_filtered (file
,
354 _("An upper bound on the number "
355 "of backtrace levels is %s.\n"),
361 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
364 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
366 fprintf_unfiltered (file
, "!%s", name
);
370 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
372 fprintf_unfiltered (file
, "{");
374 if (id
.stack_status
== FID_STACK_INVALID
)
375 fprintf_unfiltered (file
, "!stack");
376 else if (id
.stack_status
== FID_STACK_UNAVAILABLE
)
377 fprintf_unfiltered (file
, "stack=<unavailable>");
378 else if (id
.stack_status
== FID_STACK_SENTINEL
)
379 fprintf_unfiltered (file
, "stack=<sentinel>");
381 fprintf_unfiltered (file
, "stack=%s", hex_string (id
.stack_addr
));
382 fprintf_unfiltered (file
, ",");
384 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
385 fprintf_unfiltered (file
, ",");
387 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
389 if (id
.artificial_depth
)
390 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
392 fprintf_unfiltered (file
, "}");
396 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
401 fprintf_unfiltered (file
, "NORMAL_FRAME");
404 fprintf_unfiltered (file
, "DUMMY_FRAME");
407 fprintf_unfiltered (file
, "INLINE_FRAME");
410 fprintf_unfiltered (file
, "TAILCALL_FRAME");
413 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
416 fprintf_unfiltered (file
, "ARCH_FRAME");
419 fprintf_unfiltered (file
, "SENTINEL_FRAME");
422 fprintf_unfiltered (file
, "<unknown type>");
428 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
432 fprintf_unfiltered (file
, "<NULL frame>");
435 fprintf_unfiltered (file
, "{");
436 fprintf_unfiltered (file
, "level=%d", fi
->level
);
437 fprintf_unfiltered (file
, ",");
438 fprintf_unfiltered (file
, "type=");
439 if (fi
->unwind
!= NULL
)
440 fprint_frame_type (file
, fi
->unwind
->type
);
442 fprintf_unfiltered (file
, "<unknown>");
443 fprintf_unfiltered (file
, ",");
444 fprintf_unfiltered (file
, "unwind=");
445 if (fi
->unwind
!= NULL
)
446 gdb_print_host_address (fi
->unwind
, file
);
448 fprintf_unfiltered (file
, "<unknown>");
449 fprintf_unfiltered (file
, ",");
450 fprintf_unfiltered (file
, "pc=");
451 if (fi
->next
== NULL
|| fi
->next
->prev_pc
.status
== CC_UNKNOWN
)
452 fprintf_unfiltered (file
, "<unknown>");
453 else if (fi
->next
->prev_pc
.status
== CC_VALUE
)
455 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_pc
.value
));
456 if (fi
->next
->prev_pc
.masked
)
457 fprintf_unfiltered (file
, "[PAC]");
459 else if (fi
->next
->prev_pc
.status
== CC_NOT_SAVED
)
460 val_print_not_saved (file
);
461 else if (fi
->next
->prev_pc
.status
== CC_UNAVAILABLE
)
462 val_print_unavailable (file
);
463 fprintf_unfiltered (file
, ",");
464 fprintf_unfiltered (file
, "id=");
466 fprint_frame_id (file
, fi
->this_id
.value
);
468 fprintf_unfiltered (file
, "<unknown>");
469 fprintf_unfiltered (file
, ",");
470 fprintf_unfiltered (file
, "func=");
471 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.p
)
472 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
474 fprintf_unfiltered (file
, "<unknown>");
475 fprintf_unfiltered (file
, "}");
478 /* Given FRAME, return the enclosing frame as found in real frames read-in from
479 inferior memory. Skip any previous frames which were made up by GDB.
480 Return FRAME if FRAME is a non-artificial frame.
481 Return NULL if FRAME is the start of an artificial-only chain. */
483 static struct frame_info
*
484 skip_artificial_frames (struct frame_info
*frame
)
486 /* Note we use get_prev_frame_always, and not get_prev_frame. The
487 latter will truncate the frame chain, leading to this function
488 unintentionally returning a null_frame_id (e.g., when the user
489 sets a backtrace limit).
491 Note that for record targets we may get a frame chain that consists
492 of artificial frames only. */
493 while (get_frame_type (frame
) == INLINE_FRAME
494 || get_frame_type (frame
) == TAILCALL_FRAME
)
496 frame
= get_prev_frame_always (frame
);
505 skip_unwritable_frames (struct frame_info
*frame
)
507 while (gdbarch_code_of_frame_writable (get_frame_arch (frame
), frame
) == 0)
509 frame
= get_prev_frame (frame
);
520 skip_tailcall_frames (struct frame_info
*frame
)
522 while (get_frame_type (frame
) == TAILCALL_FRAME
)
524 /* Note that for record targets we may get a frame chain that consists of
525 tailcall frames only. */
526 frame
= get_prev_frame (frame
);
534 /* Compute the frame's uniq ID that can be used to, later, re-find the
538 compute_frame_id (struct frame_info
*fi
)
540 gdb_assert (!fi
->this_id
.p
);
543 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
545 /* Find the unwinder. */
546 if (fi
->unwind
== NULL
)
547 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
548 /* Find THIS frame's ID. */
549 /* Default to outermost if no ID is found. */
550 fi
->this_id
.value
= outer_frame_id
;
551 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
552 gdb_assert (frame_id_p (fi
->this_id
.value
));
556 fprintf_unfiltered (gdb_stdlog
, "-> ");
557 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
558 fprintf_unfiltered (gdb_stdlog
, " }\n");
562 /* Return a frame uniq ID that can be used to, later, re-find the
566 get_frame_id (struct frame_info
*fi
)
569 return null_frame_id
;
575 /* If we haven't computed the frame id yet, then it must be that
576 this is the current frame. Compute it now, and stash the
577 result. The IDs of other frames are computed as soon as
578 they're created, in order to detect cycles. See
579 get_prev_frame_if_no_cycle. */
580 gdb_assert (fi
->level
== 0);
583 compute_frame_id (fi
);
585 /* Since this is the first frame in the chain, this should
587 stashed
= frame_stash_add (fi
);
588 gdb_assert (stashed
);
591 return fi
->this_id
.value
;
595 get_stack_frame_id (struct frame_info
*next_frame
)
597 return get_frame_id (skip_artificial_frames (next_frame
));
601 frame_unwind_caller_id (struct frame_info
*next_frame
)
603 struct frame_info
*this_frame
;
605 /* Use get_prev_frame_always, and not get_prev_frame. The latter
606 will truncate the frame chain, leading to this function
607 unintentionally returning a null_frame_id (e.g., when a caller
608 requests the frame ID of "main()"s caller. */
610 next_frame
= skip_artificial_frames (next_frame
);
611 if (next_frame
== NULL
)
612 return null_frame_id
;
614 this_frame
= get_prev_frame_always (next_frame
);
616 return get_frame_id (skip_artificial_frames (this_frame
));
618 return null_frame_id
;
621 const struct frame_id null_frame_id
= { 0 }; /* All zeros. */
622 const struct frame_id sentinel_frame_id
= { 0, 0, 0, FID_STACK_SENTINEL
, 0, 1, 0 };
623 const struct frame_id outer_frame_id
= { 0, 0, 0, FID_STACK_INVALID
, 0, 1, 0 };
626 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
627 CORE_ADDR special_addr
)
629 struct frame_id id
= null_frame_id
;
631 id
.stack_addr
= stack_addr
;
632 id
.stack_status
= FID_STACK_VALID
;
633 id
.code_addr
= code_addr
;
635 id
.special_addr
= special_addr
;
636 id
.special_addr_p
= 1;
643 frame_id_build_unavailable_stack (CORE_ADDR code_addr
)
645 struct frame_id id
= null_frame_id
;
647 id
.stack_status
= FID_STACK_UNAVAILABLE
;
648 id
.code_addr
= code_addr
;
656 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr
,
657 CORE_ADDR special_addr
)
659 struct frame_id id
= null_frame_id
;
661 id
.stack_status
= FID_STACK_UNAVAILABLE
;
662 id
.code_addr
= code_addr
;
664 id
.special_addr
= special_addr
;
665 id
.special_addr_p
= 1;
670 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
672 struct frame_id id
= null_frame_id
;
674 id
.stack_addr
= stack_addr
;
675 id
.stack_status
= FID_STACK_VALID
;
676 id
.code_addr
= code_addr
;
682 frame_id_build_wild (CORE_ADDR stack_addr
)
684 struct frame_id id
= null_frame_id
;
686 id
.stack_addr
= stack_addr
;
687 id
.stack_status
= FID_STACK_VALID
;
692 frame_id_p (struct frame_id l
)
696 /* The frame is valid iff it has a valid stack address. */
697 p
= l
.stack_status
!= FID_STACK_INVALID
;
698 /* outer_frame_id and functions inlined into it are also valid. */
699 if (!p
&& l
.special_addr_p
)
703 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
704 fprint_frame_id (gdb_stdlog
, l
);
705 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
711 frame_id_artificial_p (struct frame_id l
)
716 return (l
.artificial_depth
!= 0);
720 frame_id_eq (struct frame_id l
, struct frame_id r
)
724 if (l
.stack_status
== FID_STACK_INVALID
&& l
.special_addr_p
725 && r
.stack_status
== FID_STACK_INVALID
&& r
.special_addr_p
)
726 /* The outermost frame marker, and any inline frame markers
727 derived from it, are equal to themselves. This is the dodgy
728 thing about outer_frame_id, since between execution steps
729 we might step into another function - from which we can't
730 unwind either. More thought required to get rid of
732 eq
= l
.artificial_depth
== r
.artificial_depth
;
733 else if (l
.stack_status
== FID_STACK_INVALID
734 || r
.stack_status
== FID_STACK_INVALID
)
735 /* Like a NaN, if either ID is invalid, the result is false.
736 Note that a frame ID is invalid iff it is the null frame ID. */
738 else if (l
.stack_status
!= r
.stack_status
|| l
.stack_addr
!= r
.stack_addr
)
739 /* If .stack addresses are different, the frames are different. */
741 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
742 /* An invalid code addr is a wild card. If .code addresses are
743 different, the frames are different. */
745 else if (l
.special_addr_p
&& r
.special_addr_p
746 && l
.special_addr
!= r
.special_addr
)
747 /* An invalid special addr is a wild card (or unused). Otherwise
748 if special addresses are different, the frames are different. */
750 else if (l
.artificial_depth
!= r
.artificial_depth
)
751 /* If artificial depths are different, the frames must be different. */
754 /* Frames are equal. */
759 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
760 fprint_frame_id (gdb_stdlog
, l
);
761 fprintf_unfiltered (gdb_stdlog
, ",r=");
762 fprint_frame_id (gdb_stdlog
, r
);
763 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
768 /* Safety net to check whether frame ID L should be inner to
769 frame ID R, according to their stack addresses.
771 This method cannot be used to compare arbitrary frames, as the
772 ranges of valid stack addresses may be discontiguous (e.g. due
775 However, it can be used as safety net to discover invalid frame
776 IDs in certain circumstances. Assuming that NEXT is the immediate
777 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
779 * The stack address of NEXT must be inner-than-or-equal to the stack
782 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
785 * If NEXT and THIS have different stack addresses, no other frame
786 in the frame chain may have a stack address in between.
788 Therefore, if frame_id_inner (TEST, THIS) holds, but
789 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
790 to a valid frame in the frame chain.
792 The sanity checks above cannot be performed when a SIGTRAMP frame
793 is involved, because signal handlers might be executed on a different
794 stack than the stack used by the routine that caused the signal
795 to be raised. This can happen for instance when a thread exceeds
796 its maximum stack size. In this case, certain compilers implement
797 a stack overflow strategy that cause the handler to be run on a
801 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
805 if (l
.stack_status
!= FID_STACK_VALID
|| r
.stack_status
!= FID_STACK_VALID
)
806 /* Like NaN, any operation involving an invalid ID always fails.
807 Likewise if either ID has an unavailable stack address. */
809 else if (l
.artificial_depth
> r
.artificial_depth
810 && l
.stack_addr
== r
.stack_addr
811 && l
.code_addr_p
== r
.code_addr_p
812 && l
.special_addr_p
== r
.special_addr_p
813 && l
.special_addr
== r
.special_addr
)
815 /* Same function, different inlined functions. */
816 const struct block
*lb
, *rb
;
818 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
820 lb
= block_for_pc (l
.code_addr
);
821 rb
= block_for_pc (r
.code_addr
);
823 if (lb
== NULL
|| rb
== NULL
)
824 /* Something's gone wrong. */
827 /* This will return true if LB and RB are the same block, or
828 if the block with the smaller depth lexically encloses the
829 block with the greater depth. */
830 inner
= contained_in (lb
, rb
);
833 /* Only return non-zero when strictly inner than. Note that, per
834 comment in "frame.h", there is some fuzz here. Frameless
835 functions are not strictly inner than (same .stack but
836 different .code and/or .special address). */
837 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
840 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
841 fprint_frame_id (gdb_stdlog
, l
);
842 fprintf_unfiltered (gdb_stdlog
, ",r=");
843 fprint_frame_id (gdb_stdlog
, r
);
844 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
850 frame_find_by_id (struct frame_id id
)
852 struct frame_info
*frame
, *prev_frame
;
854 /* ZERO denotes the null frame, let the caller decide what to do
855 about it. Should it instead return get_current_frame()? */
856 if (!frame_id_p (id
))
859 /* Check for the sentinel frame. */
860 if (frame_id_eq (id
, sentinel_frame_id
))
861 return sentinel_frame
;
863 /* Try using the frame stash first. Finding it there removes the need
864 to perform the search by looping over all frames, which can be very
865 CPU-intensive if the number of frames is very high (the loop is O(n)
866 and get_prev_frame performs a series of checks that are relatively
867 expensive). This optimization is particularly useful when this function
868 is called from another function (such as value_fetch_lazy, case
869 VALUE_LVAL (val) == lval_register) which already loops over all frames,
870 making the overall behavior O(n^2). */
871 frame
= frame_stash_find (id
);
875 for (frame
= get_current_frame (); ; frame
= prev_frame
)
877 struct frame_id self
= get_frame_id (frame
);
879 if (frame_id_eq (id
, self
))
880 /* An exact match. */
883 prev_frame
= get_prev_frame (frame
);
887 /* As a safety net to avoid unnecessary backtracing while trying
888 to find an invalid ID, we check for a common situation where
889 we can detect from comparing stack addresses that no other
890 frame in the current frame chain can have this ID. See the
891 comment at frame_id_inner for details. */
892 if (get_frame_type (frame
) == NORMAL_FRAME
893 && !frame_id_inner (get_frame_arch (frame
), id
, self
)
894 && frame_id_inner (get_frame_arch (prev_frame
), id
,
895 get_frame_id (prev_frame
)))
902 frame_unwind_pc (struct frame_info
*this_frame
)
904 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
906 struct gdbarch
*prev_gdbarch
;
910 /* The right way. The `pure' way. The one true way. This
911 method depends solely on the register-unwind code to
912 determine the value of registers in THIS frame, and hence
913 the value of this frame's PC (resume address). A typical
914 implementation is no more than:
916 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
917 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
919 Note: this method is very heavily dependent on a correct
920 register-unwind implementation, it pays to fix that
921 method first; this method is frame type agnostic, since
922 it only deals with register values, it works with any
923 frame. This is all in stark contrast to the old
924 FRAME_SAVED_PC which would try to directly handle all the
925 different ways that a PC could be unwound. */
926 prev_gdbarch
= frame_unwind_arch (this_frame
);
930 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
933 catch (const gdb_exception_error
&ex
)
935 if (ex
.error
== NOT_AVAILABLE_ERROR
)
937 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
940 fprintf_unfiltered (gdb_stdlog
,
941 "{ frame_unwind_pc (this_frame=%d)"
942 " -> <unavailable> }\n",
945 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
947 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
950 fprintf_unfiltered (gdb_stdlog
,
951 "{ frame_unwind_pc (this_frame=%d)"
952 " -> <not saved> }\n",
961 this_frame
->prev_pc
.value
= pc
;
962 this_frame
->prev_pc
.status
= CC_VALUE
;
964 fprintf_unfiltered (gdb_stdlog
,
965 "{ frame_unwind_pc (this_frame=%d) "
968 hex_string (this_frame
->prev_pc
.value
));
972 if (this_frame
->prev_pc
.status
== CC_VALUE
)
973 return this_frame
->prev_pc
.value
;
974 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
975 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
976 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
977 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
979 internal_error (__FILE__
, __LINE__
,
980 "unexpected prev_pc status: %d",
981 (int) this_frame
->prev_pc
.status
);
985 frame_unwind_caller_pc (struct frame_info
*this_frame
)
987 this_frame
= skip_artificial_frames (this_frame
);
989 /* We must have a non-artificial frame. The caller is supposed to check
990 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
992 gdb_assert (this_frame
!= NULL
);
994 return frame_unwind_pc (this_frame
);
998 get_frame_func_if_available (struct frame_info
*this_frame
, CORE_ADDR
*pc
)
1000 struct frame_info
*next_frame
= this_frame
->next
;
1002 if (!next_frame
->prev_func
.p
)
1004 CORE_ADDR addr_in_block
;
1006 /* Make certain that this, and not the adjacent, function is
1008 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
1010 next_frame
->prev_func
.p
= -1;
1012 fprintf_unfiltered (gdb_stdlog
,
1013 "{ get_frame_func (this_frame=%d)"
1014 " -> unavailable }\n",
1019 next_frame
->prev_func
.p
= 1;
1020 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
1022 fprintf_unfiltered (gdb_stdlog
,
1023 "{ get_frame_func (this_frame=%d) -> %s }\n",
1025 hex_string (next_frame
->prev_func
.addr
));
1029 if (next_frame
->prev_func
.p
< 0)
1036 *pc
= next_frame
->prev_func
.addr
;
1042 get_frame_func (struct frame_info
*this_frame
)
1046 if (!get_frame_func_if_available (this_frame
, &pc
))
1047 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1052 std::unique_ptr
<readonly_detached_regcache
>
1053 frame_save_as_regcache (struct frame_info
*this_frame
)
1055 auto cooked_read
= [this_frame
] (int regnum
, gdb_byte
*buf
)
1057 if (!deprecated_frame_register_read (this_frame
, regnum
, buf
))
1058 return REG_UNAVAILABLE
;
1063 std::unique_ptr
<readonly_detached_regcache
> regcache
1064 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
1070 frame_pop (struct frame_info
*this_frame
)
1072 struct frame_info
*prev_frame
;
1074 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
1076 /* Popping a dummy frame involves restoring more than just registers.
1077 dummy_frame_pop does all the work. */
1078 dummy_frame_pop (get_frame_id (this_frame
), inferior_thread ());
1082 /* Ensure that we have a frame to pop to. */
1083 prev_frame
= get_prev_frame_always (this_frame
);
1086 error (_("Cannot pop the initial frame."));
1088 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1089 entering THISFRAME. */
1090 prev_frame
= skip_tailcall_frames (prev_frame
);
1092 if (prev_frame
== NULL
)
1093 error (_("Cannot find the caller frame."));
1095 /* Make a copy of all the register values unwound from this frame.
1096 Save them in a scratch buffer so that there isn't a race between
1097 trying to extract the old values from the current regcache while
1098 at the same time writing new values into that same cache. */
1099 std::unique_ptr
<readonly_detached_regcache
> scratch
1100 = frame_save_as_regcache (prev_frame
);
1102 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1103 target's register cache that it is about to be hit with a burst
1104 register transfer and that the sequence of register writes should
1105 be batched. The pair target_prepare_to_store() and
1106 target_store_registers() kind of suggest this functionality.
1107 Unfortunately, they don't implement it. Their lack of a formal
1108 definition can lead to targets writing back bogus values
1109 (arguably a bug in the target code mind). */
1110 /* Now copy those saved registers into the current regcache. */
1111 get_current_regcache ()->restore (scratch
.get ());
1113 /* We've made right mess of GDB's local state, just discard
1115 reinit_frame_cache ();
1119 frame_register_unwind (frame_info
*next_frame
, int regnum
,
1120 int *optimizedp
, int *unavailablep
,
1121 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
1122 int *realnump
, gdb_byte
*bufferp
)
1124 struct value
*value
;
1126 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1127 that the value proper does not need to be fetched. */
1128 gdb_assert (optimizedp
!= NULL
);
1129 gdb_assert (lvalp
!= NULL
);
1130 gdb_assert (addrp
!= NULL
);
1131 gdb_assert (realnump
!= NULL
);
1132 /* gdb_assert (bufferp != NULL); */
1134 value
= frame_unwind_register_value (next_frame
, regnum
);
1136 gdb_assert (value
!= NULL
);
1138 *optimizedp
= value_optimized_out (value
);
1139 *unavailablep
= !value_entirely_available (value
);
1140 *lvalp
= VALUE_LVAL (value
);
1141 *addrp
= value_address (value
);
1142 if (*lvalp
== lval_register
)
1143 *realnump
= VALUE_REGNUM (value
);
1149 if (!*optimizedp
&& !*unavailablep
)
1150 memcpy (bufferp
, value_contents_all (value
),
1151 TYPE_LENGTH (value_type (value
)));
1153 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
1156 /* Dispose of the new value. This prevents watchpoints from
1157 trying to watch the saved frame pointer. */
1158 release_value (value
);
1162 frame_register (struct frame_info
*frame
, int regnum
,
1163 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
1164 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
1166 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1167 that the value proper does not need to be fetched. */
1168 gdb_assert (optimizedp
!= NULL
);
1169 gdb_assert (lvalp
!= NULL
);
1170 gdb_assert (addrp
!= NULL
);
1171 gdb_assert (realnump
!= NULL
);
1172 /* gdb_assert (bufferp != NULL); */
1174 /* Obtain the register value by unwinding the register from the next
1175 (more inner frame). */
1176 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
1177 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
1178 lvalp
, addrp
, realnump
, bufferp
);
1182 frame_unwind_register (frame_info
*next_frame
, int regnum
, gdb_byte
*buf
)
1188 enum lval_type lval
;
1190 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1191 &lval
, &addr
, &realnum
, buf
);
1194 throw_error (OPTIMIZED_OUT_ERROR
,
1195 _("Register %d was not saved"), regnum
);
1197 throw_error (NOT_AVAILABLE_ERROR
,
1198 _("Register %d is not available"), regnum
);
1202 get_frame_register (struct frame_info
*frame
,
1203 int regnum
, gdb_byte
*buf
)
1205 frame_unwind_register (frame
->next
, regnum
, buf
);
1209 frame_unwind_register_value (frame_info
*next_frame
, int regnum
)
1211 struct gdbarch
*gdbarch
;
1212 struct value
*value
;
1214 gdb_assert (next_frame
!= NULL
);
1215 gdbarch
= frame_unwind_arch (next_frame
);
1219 fprintf_unfiltered (gdb_stdlog
,
1220 "{ frame_unwind_register_value "
1221 "(frame=%d,regnum=%d(%s),...) ",
1222 next_frame
->level
, regnum
,
1223 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1226 /* Find the unwinder. */
1227 if (next_frame
->unwind
== NULL
)
1228 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
1230 /* Ask this frame to unwind its register. */
1231 value
= next_frame
->unwind
->prev_register (next_frame
,
1232 &next_frame
->prologue_cache
,
1237 fprintf_unfiltered (gdb_stdlog
, "->");
1238 if (value_optimized_out (value
))
1240 fprintf_unfiltered (gdb_stdlog
, " ");
1241 val_print_optimized_out (value
, gdb_stdlog
);
1245 if (VALUE_LVAL (value
) == lval_register
)
1246 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1247 VALUE_REGNUM (value
));
1248 else if (VALUE_LVAL (value
) == lval_memory
)
1249 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1251 value_address (value
)));
1253 fprintf_unfiltered (gdb_stdlog
, " computed");
1255 if (value_lazy (value
))
1256 fprintf_unfiltered (gdb_stdlog
, " lazy");
1260 const gdb_byte
*buf
= value_contents (value
);
1262 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1263 fprintf_unfiltered (gdb_stdlog
, "[");
1264 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1265 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1266 fprintf_unfiltered (gdb_stdlog
, "]");
1270 fprintf_unfiltered (gdb_stdlog
, " }\n");
1277 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1279 return frame_unwind_register_value (frame
->next
, regnum
);
1283 frame_unwind_register_signed (frame_info
*next_frame
, int regnum
)
1285 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1286 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1287 int size
= register_size (gdbarch
, regnum
);
1288 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1290 gdb_assert (value
!= NULL
);
1292 if (value_optimized_out (value
))
1294 throw_error (OPTIMIZED_OUT_ERROR
,
1295 _("Register %d was not saved"), regnum
);
1297 if (!value_entirely_available (value
))
1299 throw_error (NOT_AVAILABLE_ERROR
,
1300 _("Register %d is not available"), regnum
);
1303 LONGEST r
= extract_signed_integer (value_contents_all (value
), size
,
1306 release_value (value
);
1311 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1313 return frame_unwind_register_signed (frame
->next
, regnum
);
1317 frame_unwind_register_unsigned (frame_info
*next_frame
, int regnum
)
1319 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1320 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1321 int size
= register_size (gdbarch
, regnum
);
1322 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1324 gdb_assert (value
!= NULL
);
1326 if (value_optimized_out (value
))
1328 throw_error (OPTIMIZED_OUT_ERROR
,
1329 _("Register %d was not saved"), regnum
);
1331 if (!value_entirely_available (value
))
1333 throw_error (NOT_AVAILABLE_ERROR
,
1334 _("Register %d is not available"), regnum
);
1337 ULONGEST r
= extract_unsigned_integer (value_contents_all (value
), size
,
1340 release_value (value
);
1345 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1347 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1351 read_frame_register_unsigned (struct frame_info
*frame
, int regnum
,
1354 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1356 if (!value_optimized_out (regval
)
1357 && value_entirely_available (regval
))
1359 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1360 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1361 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1363 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1371 put_frame_register (struct frame_info
*frame
, int regnum
,
1372 const gdb_byte
*buf
)
1374 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1378 enum lval_type lval
;
1381 frame_register (frame
, regnum
, &optim
, &unavail
,
1382 &lval
, &addr
, &realnum
, NULL
);
1384 error (_("Attempt to assign to a register that was not saved."));
1389 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1393 get_current_regcache ()->cooked_write (realnum
, buf
);
1396 error (_("Attempt to assign to an unmodifiable value."));
1400 /* This function is deprecated. Use get_frame_register_value instead,
1401 which provides more accurate information.
1403 Find and return the value of REGNUM for the specified stack frame.
1404 The number of bytes copied is REGISTER_SIZE (REGNUM).
1406 Returns 0 if the register value could not be found. */
1409 deprecated_frame_register_read (struct frame_info
*frame
, int regnum
,
1414 enum lval_type lval
;
1418 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1419 &lval
, &addr
, &realnum
, myaddr
);
1421 return !optimized
&& !unavailable
;
1425 get_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1426 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1427 int *optimizedp
, int *unavailablep
)
1429 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1434 /* Skip registers wholly inside of OFFSET. */
1435 while (offset
>= register_size (gdbarch
, regnum
))
1437 offset
-= register_size (gdbarch
, regnum
);
1441 /* Ensure that we will not read beyond the end of the register file.
1442 This can only ever happen if the debug information is bad. */
1444 numregs
= gdbarch_num_cooked_regs (gdbarch
);
1445 for (i
= regnum
; i
< numregs
; i
++)
1447 int thissize
= register_size (gdbarch
, i
);
1450 break; /* This register is not available on this architecture. */
1451 maxsize
+= thissize
;
1454 error (_("Bad debug information detected: "
1455 "Attempt to read %d bytes from registers."), len
);
1457 /* Copy the data. */
1460 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1465 if (curr_len
== register_size (gdbarch
, regnum
))
1467 enum lval_type lval
;
1471 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1472 &lval
, &addr
, &realnum
, myaddr
);
1473 if (*optimizedp
|| *unavailablep
)
1478 struct value
*value
= frame_unwind_register_value (frame
->next
,
1480 gdb_assert (value
!= NULL
);
1481 *optimizedp
= value_optimized_out (value
);
1482 *unavailablep
= !value_entirely_available (value
);
1484 if (*optimizedp
|| *unavailablep
)
1486 release_value (value
);
1489 memcpy (myaddr
, value_contents_all (value
) + offset
, curr_len
);
1490 release_value (value
);
1505 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1506 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1508 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1510 /* Skip registers wholly inside of OFFSET. */
1511 while (offset
>= register_size (gdbarch
, regnum
))
1513 offset
-= register_size (gdbarch
, regnum
);
1517 /* Copy the data. */
1520 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1525 if (curr_len
== register_size (gdbarch
, regnum
))
1527 put_frame_register (frame
, regnum
, myaddr
);
1531 struct value
*value
= frame_unwind_register_value (frame
->next
,
1533 gdb_assert (value
!= NULL
);
1535 memcpy ((char *) value_contents_writeable (value
) + offset
, myaddr
,
1537 put_frame_register (frame
, regnum
, value_contents_raw (value
));
1538 release_value (value
);
1548 /* Create a sentinel frame. */
1550 static struct frame_info
*
1551 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1553 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1556 frame
->pspace
= pspace
;
1557 frame
->aspace
= regcache
->aspace ();
1558 /* Explicitly initialize the sentinel frame's cache. Provide it
1559 with the underlying regcache. In the future additional
1560 information, such as the frame's thread will be added. */
1561 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1562 /* For the moment there is only one sentinel frame implementation. */
1563 frame
->unwind
= &sentinel_frame_unwind
;
1564 /* Link this frame back to itself. The frame is self referential
1565 (the unwound PC is the same as the pc), so make it so. */
1566 frame
->next
= frame
;
1567 /* The sentinel frame has a special ID. */
1568 frame
->this_id
.p
= 1;
1569 frame
->this_id
.value
= sentinel_frame_id
;
1572 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1573 fprint_frame (gdb_stdlog
, frame
);
1574 fprintf_unfiltered (gdb_stdlog
, " }\n");
1579 /* Cache for frame addresses already read by gdb. Valid only while
1580 inferior is stopped. Control variables for the frame cache should
1581 be local to this module. */
1583 static struct obstack frame_cache_obstack
;
1586 frame_obstack_zalloc (unsigned long size
)
1588 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1590 memset (data
, 0, size
);
1594 static struct frame_info
*get_prev_frame_always_1 (struct frame_info
*this_frame
);
1597 get_current_frame (void)
1599 struct frame_info
*current_frame
;
1601 /* First check, and report, the lack of registers. Having GDB
1602 report "No stack!" or "No memory" when the target doesn't even
1603 have registers is very confusing. Besides, "printcmd.exp"
1604 explicitly checks that ``print $pc'' with no registers prints "No
1606 if (!target_has_registers
)
1607 error (_("No registers."));
1608 if (!target_has_stack
)
1609 error (_("No stack."));
1610 if (!target_has_memory
)
1611 error (_("No memory."));
1612 /* Traceframes are effectively a substitute for the live inferior. */
1613 if (get_traceframe_number () < 0)
1614 validate_registers_access ();
1616 if (sentinel_frame
== NULL
)
1618 create_sentinel_frame (current_program_space
, get_current_regcache ());
1620 /* Set the current frame before computing the frame id, to avoid
1621 recursion inside compute_frame_id, in case the frame's
1622 unwinder decides to do a symbol lookup (which depends on the
1623 selected frame's block).
1625 This call must always succeed. In particular, nothing inside
1626 get_prev_frame_always_1 should try to unwind from the
1627 sentinel frame, because that could fail/throw, and we always
1628 want to leave with the current frame created and linked in --
1629 we should never end up with the sentinel frame as outermost
1631 current_frame
= get_prev_frame_always_1 (sentinel_frame
);
1632 gdb_assert (current_frame
!= NULL
);
1634 return current_frame
;
1637 /* The "selected" stack frame is used by default for local and arg
1638 access. May be zero, for no selected frame. */
1640 static struct frame_info
*selected_frame
;
1643 has_stack_frames (void)
1645 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1648 /* Traceframes are effectively a substitute for the live inferior. */
1649 if (get_traceframe_number () < 0)
1651 /* No current inferior, no frame. */
1652 if (inferior_ptid
== null_ptid
)
1655 thread_info
*tp
= inferior_thread ();
1656 /* Don't try to read from a dead thread. */
1657 if (tp
->state
== THREAD_EXITED
)
1660 /* ... or from a spinning thread. */
1668 /* Return the selected frame. Always non-NULL (unless there isn't an
1669 inferior sufficient for creating a frame) in which case an error is
1673 get_selected_frame (const char *message
)
1675 if (selected_frame
== NULL
)
1677 if (message
!= NULL
&& !has_stack_frames ())
1678 error (("%s"), message
);
1679 /* Hey! Don't trust this. It should really be re-finding the
1680 last selected frame of the currently selected thread. This,
1681 though, is better than nothing. */
1682 select_frame (get_current_frame ());
1684 /* There is always a frame. */
1685 gdb_assert (selected_frame
!= NULL
);
1686 return selected_frame
;
1689 /* If there is a selected frame, return it. Otherwise, return NULL. */
1692 get_selected_frame_if_set (void)
1694 return selected_frame
;
1697 /* This is a variant of get_selected_frame() which can be called when
1698 the inferior does not have a frame; in that case it will return
1699 NULL instead of calling error(). */
1702 deprecated_safe_get_selected_frame (void)
1704 if (!has_stack_frames ())
1706 return get_selected_frame (NULL
);
1709 /* Select frame FI (or NULL - to invalidate the current frame). */
1712 select_frame (struct frame_info
*fi
)
1714 selected_frame
= fi
;
1715 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1716 frame is being invalidated. */
1718 /* FIXME: kseitz/2002-08-28: It would be nice to call
1719 selected_frame_level_changed_event() right here, but due to limitations
1720 in the current interfaces, we would end up flooding UIs with events
1721 because select_frame() is used extensively internally.
1723 Once we have frame-parameterized frame (and frame-related) commands,
1724 the event notification can be moved here, since this function will only
1725 be called when the user's selected frame is being changed. */
1727 /* Ensure that symbols for this frame are read in. Also, determine the
1728 source language of this frame, and switch to it if desired. */
1733 /* We retrieve the frame's symtab by using the frame PC.
1734 However we cannot use the frame PC as-is, because it usually
1735 points to the instruction following the "call", which is
1736 sometimes the first instruction of another function. So we
1737 rely on get_frame_address_in_block() which provides us with a
1738 PC which is guaranteed to be inside the frame's code
1740 if (get_frame_address_in_block_if_available (fi
, &pc
))
1742 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
1745 && compunit_language (cust
) != current_language
->la_language
1746 && compunit_language (cust
) != language_unknown
1747 && language_mode
== language_mode_auto
)
1748 set_language (compunit_language (cust
));
1753 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1754 Always returns a non-NULL value. */
1757 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1759 struct frame_info
*fi
;
1763 fprintf_unfiltered (gdb_stdlog
,
1764 "{ create_new_frame (addr=%s, pc=%s) ",
1765 hex_string (addr
), hex_string (pc
));
1768 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1770 fi
->next
= create_sentinel_frame (current_program_space
,
1771 get_current_regcache ());
1773 /* Set/update this frame's cached PC value, found in the next frame.
1774 Do this before looking for this frame's unwinder. A sniffer is
1775 very likely to read this, and the corresponding unwinder is
1776 entitled to rely that the PC doesn't magically change. */
1777 fi
->next
->prev_pc
.value
= pc
;
1778 fi
->next
->prev_pc
.status
= CC_VALUE
;
1780 /* We currently assume that frame chain's can't cross spaces. */
1781 fi
->pspace
= fi
->next
->pspace
;
1782 fi
->aspace
= fi
->next
->aspace
;
1784 /* Select/initialize both the unwind function and the frame's type
1786 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1789 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1793 fprintf_unfiltered (gdb_stdlog
, "-> ");
1794 fprint_frame (gdb_stdlog
, fi
);
1795 fprintf_unfiltered (gdb_stdlog
, " }\n");
1801 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1802 innermost frame). Be careful to not fall off the bottom of the
1803 frame chain and onto the sentinel frame. */
1806 get_next_frame (struct frame_info
*this_frame
)
1808 if (this_frame
->level
> 0)
1809 return this_frame
->next
;
1814 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1815 innermost (i.e. current) frame, return the sentinel frame. Thus,
1816 unlike get_next_frame(), NULL will never be returned. */
1819 get_next_frame_sentinel_okay (struct frame_info
*this_frame
)
1821 gdb_assert (this_frame
!= NULL
);
1823 /* Note that, due to the manner in which the sentinel frame is
1824 constructed, this_frame->next still works even when this_frame
1825 is the sentinel frame. But we disallow it here anyway because
1826 calling get_next_frame_sentinel_okay() on the sentinel frame
1827 is likely a coding error. */
1828 gdb_assert (this_frame
!= sentinel_frame
);
1830 return this_frame
->next
;
1833 /* Observer for the target_changed event. */
1836 frame_observer_target_changed (struct target_ops
*target
)
1838 reinit_frame_cache ();
1841 /* Flush the entire frame cache. */
1844 reinit_frame_cache (void)
1846 struct frame_info
*fi
;
1848 /* Tear down all frame caches. */
1849 for (fi
= sentinel_frame
; fi
!= NULL
; fi
= fi
->prev
)
1851 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1852 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1853 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1854 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1857 /* Since we can't really be sure what the first object allocated was. */
1858 obstack_free (&frame_cache_obstack
, 0);
1859 obstack_init (&frame_cache_obstack
);
1861 if (sentinel_frame
!= NULL
)
1862 annotate_frames_invalid ();
1864 sentinel_frame
= NULL
; /* Invalidate cache */
1865 select_frame (NULL
);
1866 frame_stash_invalidate ();
1868 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1871 /* Find where a register is saved (in memory or another register).
1872 The result of frame_register_unwind is just where it is saved
1873 relative to this particular frame. */
1876 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1877 int *optimizedp
, enum lval_type
*lvalp
,
1878 CORE_ADDR
*addrp
, int *realnump
)
1880 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1882 while (this_frame
!= NULL
)
1886 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1887 lvalp
, addrp
, realnump
, NULL
);
1892 if (*lvalp
!= lval_register
)
1896 this_frame
= get_next_frame (this_frame
);
1900 /* Get the previous raw frame, and check that it is not identical to
1901 same other frame frame already in the chain. If it is, there is
1902 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1903 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1904 validity tests, that compare THIS_FRAME and the next frame, we do
1905 this right after creating the previous frame, to avoid ever ending
1906 up with two frames with the same id in the frame chain. */
1908 static struct frame_info
*
1909 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1911 struct frame_info
*prev_frame
;
1913 prev_frame
= get_prev_frame_raw (this_frame
);
1915 /* Don't compute the frame id of the current frame yet. Unwinding
1916 the sentinel frame can fail (e.g., if the thread is gone and we
1917 can't thus read its registers). If we let the cycle detection
1918 code below try to compute a frame ID, then an error thrown from
1919 within the frame ID computation would result in the sentinel
1920 frame as outermost frame, which is bogus. Instead, we'll compute
1921 the current frame's ID lazily in get_frame_id. Note that there's
1922 no point in doing cycle detection when there's only one frame, so
1923 nothing is lost here. */
1924 if (prev_frame
->level
== 0)
1929 compute_frame_id (prev_frame
);
1930 if (!frame_stash_add (prev_frame
))
1932 /* Another frame with the same id was already in the stash. We just
1933 detected a cycle. */
1936 fprintf_unfiltered (gdb_stdlog
, "-> ");
1937 fprint_frame (gdb_stdlog
, NULL
);
1938 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1940 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1942 prev_frame
->next
= NULL
;
1943 this_frame
->prev
= NULL
;
1947 catch (const gdb_exception
&ex
)
1949 prev_frame
->next
= NULL
;
1950 this_frame
->prev
= NULL
;
1958 /* Helper function for get_prev_frame_always, this is called inside a
1959 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
1960 there is no such frame. This may throw an exception. */
1962 static struct frame_info
*
1963 get_prev_frame_always_1 (struct frame_info
*this_frame
)
1965 struct gdbarch
*gdbarch
;
1967 gdb_assert (this_frame
!= NULL
);
1968 gdbarch
= get_frame_arch (this_frame
);
1972 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_always (this_frame=");
1973 if (this_frame
!= NULL
)
1974 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1976 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1977 fprintf_unfiltered (gdb_stdlog
, ") ");
1980 /* Only try to do the unwind once. */
1981 if (this_frame
->prev_p
)
1985 fprintf_unfiltered (gdb_stdlog
, "-> ");
1986 fprint_frame (gdb_stdlog
, this_frame
->prev
);
1987 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
1989 return this_frame
->prev
;
1992 /* If the frame unwinder hasn't been selected yet, we must do so
1993 before setting prev_p; otherwise the check for misbehaved
1994 sniffers will think that this frame's sniffer tried to unwind
1995 further (see frame_cleanup_after_sniffer). */
1996 if (this_frame
->unwind
== NULL
)
1997 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
1999 this_frame
->prev_p
= 1;
2000 this_frame
->stop_reason
= UNWIND_NO_REASON
;
2002 /* If we are unwinding from an inline frame, all of the below tests
2003 were already performed when we unwound from the next non-inline
2004 frame. We must skip them, since we can not get THIS_FRAME's ID
2005 until we have unwound all the way down to the previous non-inline
2007 if (get_frame_type (this_frame
) == INLINE_FRAME
)
2008 return get_prev_frame_if_no_cycle (this_frame
);
2010 /* Check that this frame is unwindable. If it isn't, don't try to
2011 unwind to the prev frame. */
2012 this_frame
->stop_reason
2013 = this_frame
->unwind
->stop_reason (this_frame
,
2014 &this_frame
->prologue_cache
);
2016 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
2020 enum unwind_stop_reason reason
= this_frame
->stop_reason
;
2022 fprintf_unfiltered (gdb_stdlog
, "-> ");
2023 fprint_frame (gdb_stdlog
, NULL
);
2024 fprintf_unfiltered (gdb_stdlog
, " // %s }\n",
2025 frame_stop_reason_symbol_string (reason
));
2030 /* Check that this frame's ID isn't inner to (younger, below, next)
2031 the next frame. This happens when a frame unwind goes backwards.
2032 This check is valid only if this frame and the next frame are NORMAL.
2033 See the comment at frame_id_inner for details. */
2034 if (get_frame_type (this_frame
) == NORMAL_FRAME
2035 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
2036 && frame_id_inner (get_frame_arch (this_frame
->next
),
2037 get_frame_id (this_frame
),
2038 get_frame_id (this_frame
->next
)))
2040 CORE_ADDR this_pc_in_block
;
2041 struct minimal_symbol
*morestack_msym
;
2042 const char *morestack_name
= NULL
;
2044 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2045 this_pc_in_block
= get_frame_address_in_block (this_frame
);
2046 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
2048 morestack_name
= morestack_msym
->linkage_name ();
2049 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
2053 fprintf_unfiltered (gdb_stdlog
, "-> ");
2054 fprint_frame (gdb_stdlog
, NULL
);
2055 fprintf_unfiltered (gdb_stdlog
,
2056 " // this frame ID is inner }\n");
2058 this_frame
->stop_reason
= UNWIND_INNER_ID
;
2063 /* Check that this and the next frame do not unwind the PC register
2064 to the same memory location. If they do, then even though they
2065 have different frame IDs, the new frame will be bogus; two
2066 functions can't share a register save slot for the PC. This can
2067 happen when the prologue analyzer finds a stack adjustment, but
2070 This check does assume that the "PC register" is roughly a
2071 traditional PC, even if the gdbarch_unwind_pc method adjusts
2072 it (we do not rely on the value, only on the unwound PC being
2073 dependent on this value). A potential improvement would be
2074 to have the frame prev_pc method and the gdbarch unwind_pc
2075 method set the same lval and location information as
2076 frame_register_unwind. */
2077 if (this_frame
->level
> 0
2078 && gdbarch_pc_regnum (gdbarch
) >= 0
2079 && get_frame_type (this_frame
) == NORMAL_FRAME
2080 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
2081 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
2083 int optimized
, realnum
, nrealnum
;
2084 enum lval_type lval
, nlval
;
2085 CORE_ADDR addr
, naddr
;
2087 frame_register_unwind_location (this_frame
,
2088 gdbarch_pc_regnum (gdbarch
),
2089 &optimized
, &lval
, &addr
, &realnum
);
2090 frame_register_unwind_location (get_next_frame (this_frame
),
2091 gdbarch_pc_regnum (gdbarch
),
2092 &optimized
, &nlval
, &naddr
, &nrealnum
);
2094 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
2095 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
2099 fprintf_unfiltered (gdb_stdlog
, "-> ");
2100 fprint_frame (gdb_stdlog
, NULL
);
2101 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
2104 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2105 this_frame
->prev
= NULL
;
2110 return get_prev_frame_if_no_cycle (this_frame
);
2113 /* Return a "struct frame_info" corresponding to the frame that called
2114 THIS_FRAME. Returns NULL if there is no such frame.
2116 Unlike get_prev_frame, this function always tries to unwind the
2120 get_prev_frame_always (struct frame_info
*this_frame
)
2122 struct frame_info
*prev_frame
= NULL
;
2126 prev_frame
= get_prev_frame_always_1 (this_frame
);
2128 catch (const gdb_exception_error
&ex
)
2130 if (ex
.error
== MEMORY_ERROR
)
2132 this_frame
->stop_reason
= UNWIND_MEMORY_ERROR
;
2133 if (ex
.message
!= NULL
)
2138 /* The error needs to live as long as the frame does.
2139 Allocate using stack local STOP_STRING then assign the
2140 pointer to the frame, this allows the STOP_STRING on the
2141 frame to be of type 'const char *'. */
2142 size
= ex
.message
->size () + 1;
2143 stop_string
= (char *) frame_obstack_zalloc (size
);
2144 memcpy (stop_string
, ex
.what (), size
);
2145 this_frame
->stop_string
= stop_string
;
2156 /* Construct a new "struct frame_info" and link it previous to
2159 static struct frame_info
*
2160 get_prev_frame_raw (struct frame_info
*this_frame
)
2162 struct frame_info
*prev_frame
;
2164 /* Allocate the new frame but do not wire it in to the frame chain.
2165 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2166 frame->next to pull some fancy tricks (of course such code is, by
2167 definition, recursive). Try to prevent it.
2169 There is no reason to worry about memory leaks, should the
2170 remainder of the function fail. The allocated memory will be
2171 quickly reclaimed when the frame cache is flushed, and the `we've
2172 been here before' check above will stop repeated memory
2173 allocation calls. */
2174 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2175 prev_frame
->level
= this_frame
->level
+ 1;
2177 /* For now, assume we don't have frame chains crossing address
2179 prev_frame
->pspace
= this_frame
->pspace
;
2180 prev_frame
->aspace
= this_frame
->aspace
;
2182 /* Don't yet compute ->unwind (and hence ->type). It is computed
2183 on-demand in get_frame_type, frame_register_unwind, and
2186 /* Don't yet compute the frame's ID. It is computed on-demand by
2189 /* The unwound frame ID is validate at the start of this function,
2190 as part of the logic to decide if that frame should be further
2191 unwound, and not here while the prev frame is being created.
2192 Doing this makes it possible for the user to examine a frame that
2193 has an invalid frame ID.
2195 Some very old VAX code noted: [...] For the sake of argument,
2196 suppose that the stack is somewhat trashed (which is one reason
2197 that "info frame" exists). So, return 0 (indicating we don't
2198 know the address of the arglist) if we don't know what frame this
2202 this_frame
->prev
= prev_frame
;
2203 prev_frame
->next
= this_frame
;
2207 fprintf_unfiltered (gdb_stdlog
, "-> ");
2208 fprint_frame (gdb_stdlog
, prev_frame
);
2209 fprintf_unfiltered (gdb_stdlog
, " }\n");
2215 /* Debug routine to print a NULL frame being returned. */
2218 frame_debug_got_null_frame (struct frame_info
*this_frame
,
2223 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
2224 if (this_frame
!= NULL
)
2225 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2227 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2228 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
2232 /* Is this (non-sentinel) frame in the "main"() function? */
2235 inside_main_func (struct frame_info
*this_frame
)
2237 struct bound_minimal_symbol msymbol
;
2240 if (symfile_objfile
== 0)
2242 msymbol
= lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
2243 if (msymbol
.minsym
== NULL
)
2245 /* Make certain that the code, and not descriptor, address is
2247 maddr
= gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
2248 BMSYMBOL_VALUE_ADDRESS (msymbol
),
2249 current_top_target ());
2250 return maddr
== get_frame_func (this_frame
);
2253 /* Test whether THIS_FRAME is inside the process entry point function. */
2256 inside_entry_func (struct frame_info
*this_frame
)
2258 CORE_ADDR entry_point
;
2260 if (!entry_point_address_query (&entry_point
))
2263 return get_frame_func (this_frame
) == entry_point
;
2266 /* Return a structure containing various interesting information about
2267 the frame that called THIS_FRAME. Returns NULL if there is entier
2268 no such frame or the frame fails any of a set of target-independent
2269 condition that should terminate the frame chain (e.g., as unwinding
2272 This function should not contain target-dependent tests, such as
2273 checking whether the program-counter is zero. */
2276 get_prev_frame (struct frame_info
*this_frame
)
2281 /* There is always a frame. If this assertion fails, suspect that
2282 something should be calling get_selected_frame() or
2283 get_current_frame(). */
2284 gdb_assert (this_frame
!= NULL
);
2286 /* If this_frame is the current frame, then compute and stash
2287 its frame id prior to fetching and computing the frame id of the
2288 previous frame. Otherwise, the cycle detection code in
2289 get_prev_frame_if_no_cycle() will not work correctly. When
2290 get_frame_id() is called later on, an assertion error will
2291 be triggered in the event of a cycle between the current
2292 frame and its previous frame. */
2293 if (this_frame
->level
== 0)
2294 get_frame_id (this_frame
);
2296 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2298 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2299 sense to stop unwinding at a dummy frame. One place where a dummy
2300 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2301 pcsqh register (space register for the instruction at the head of the
2302 instruction queue) cannot be written directly; the only way to set it
2303 is to branch to code that is in the target space. In order to implement
2304 frame dummies on HPUX, the called function is made to jump back to where
2305 the inferior was when the user function was called. If gdb was inside
2306 the main function when we created the dummy frame, the dummy frame will
2307 point inside the main function. */
2308 if (this_frame
->level
>= 0
2309 && get_frame_type (this_frame
) == NORMAL_FRAME
2310 && !user_set_backtrace_options
.backtrace_past_main
2312 && inside_main_func (this_frame
))
2313 /* Don't unwind past main(). Note, this is done _before_ the
2314 frame has been marked as previously unwound. That way if the
2315 user later decides to enable unwinds past main(), that will
2316 automatically happen. */
2318 frame_debug_got_null_frame (this_frame
, "inside main func");
2322 /* If the user's backtrace limit has been exceeded, stop. We must
2323 add two to the current level; one of those accounts for backtrace_limit
2324 being 1-based and the level being 0-based, and the other accounts for
2325 the level of the new frame instead of the level of the current
2327 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2329 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2333 /* If we're already inside the entry function for the main objfile,
2334 then it isn't valid. Don't apply this test to a dummy frame -
2335 dummy frame PCs typically land in the entry func. Don't apply
2336 this test to the sentinel frame. Sentinel frames should always
2337 be allowed to unwind. */
2338 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2339 wasn't checking for "main" in the minimal symbols. With that
2340 fixed asm-source tests now stop in "main" instead of halting the
2341 backtrace in weird and wonderful ways somewhere inside the entry
2342 file. Suspect that tests for inside the entry file/func were
2343 added to work around that (now fixed) case. */
2344 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2345 suggested having the inside_entry_func test use the
2346 inside_main_func() msymbol trick (along with entry_point_address()
2347 I guess) to determine the address range of the start function.
2348 That should provide a far better stopper than the current
2350 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2351 applied tail-call optimizations to main so that a function called
2352 from main returns directly to the caller of main. Since we don't
2353 stop at main, we should at least stop at the entry point of the
2355 if (this_frame
->level
>= 0
2356 && get_frame_type (this_frame
) == NORMAL_FRAME
2357 && !user_set_backtrace_options
.backtrace_past_entry
2359 && inside_entry_func (this_frame
))
2361 frame_debug_got_null_frame (this_frame
, "inside entry func");
2365 /* Assume that the only way to get a zero PC is through something
2366 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2367 will never unwind a zero PC. */
2368 if (this_frame
->level
> 0
2369 && (get_frame_type (this_frame
) == NORMAL_FRAME
2370 || get_frame_type (this_frame
) == INLINE_FRAME
)
2371 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2372 && frame_pc_p
&& frame_pc
== 0)
2374 frame_debug_got_null_frame (this_frame
, "zero PC");
2378 return get_prev_frame_always (this_frame
);
2382 get_prev_frame_id_by_id (struct frame_id id
)
2384 struct frame_id prev_id
;
2385 struct frame_info
*frame
;
2387 frame
= frame_find_by_id (id
);
2390 prev_id
= get_frame_id (get_prev_frame (frame
));
2392 prev_id
= null_frame_id
;
2398 get_frame_pc (struct frame_info
*frame
)
2400 gdb_assert (frame
->next
!= NULL
);
2401 return frame_unwind_pc (frame
->next
);
2405 get_frame_pc_if_available (struct frame_info
*frame
, CORE_ADDR
*pc
)
2408 gdb_assert (frame
->next
!= NULL
);
2412 *pc
= frame_unwind_pc (frame
->next
);
2414 catch (const gdb_exception_error
&ex
)
2416 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2425 /* Return an address that falls within THIS_FRAME's code block. */
2428 get_frame_address_in_block (struct frame_info
*this_frame
)
2430 /* A draft address. */
2431 CORE_ADDR pc
= get_frame_pc (this_frame
);
2433 struct frame_info
*next_frame
= this_frame
->next
;
2435 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2436 Normally the resume address is inside the body of the function
2437 associated with THIS_FRAME, but there is a special case: when
2438 calling a function which the compiler knows will never return
2439 (for instance abort), the call may be the very last instruction
2440 in the calling function. The resume address will point after the
2441 call and may be at the beginning of a different function
2444 If THIS_FRAME is a signal frame or dummy frame, then we should
2445 not adjust the unwound PC. For a dummy frame, GDB pushed the
2446 resume address manually onto the stack. For a signal frame, the
2447 OS may have pushed the resume address manually and invoked the
2448 handler (e.g. GNU/Linux), or invoked the trampoline which called
2449 the signal handler - but in either case the signal handler is
2450 expected to return to the trampoline. So in both of these
2451 cases we know that the resume address is executable and
2452 related. So we only need to adjust the PC if THIS_FRAME
2453 is a normal function.
2455 If the program has been interrupted while THIS_FRAME is current,
2456 then clearly the resume address is inside the associated
2457 function. There are three kinds of interruption: debugger stop
2458 (next frame will be SENTINEL_FRAME), operating system
2459 signal or exception (next frame will be SIGTRAMP_FRAME),
2460 or debugger-induced function call (next frame will be
2461 DUMMY_FRAME). So we only need to adjust the PC if
2462 NEXT_FRAME is a normal function.
2464 We check the type of NEXT_FRAME first, since it is already
2465 known; frame type is determined by the unwinder, and since
2466 we have THIS_FRAME we've already selected an unwinder for
2469 If the next frame is inlined, we need to keep going until we find
2470 the real function - for instance, if a signal handler is invoked
2471 while in an inlined function, then the code address of the
2472 "calling" normal function should not be adjusted either. */
2474 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2475 next_frame
= next_frame
->next
;
2477 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2478 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2479 && (get_frame_type (this_frame
) == NORMAL_FRAME
2480 || get_frame_type (this_frame
) == TAILCALL_FRAME
2481 || get_frame_type (this_frame
) == INLINE_FRAME
))
2488 get_frame_address_in_block_if_available (struct frame_info
*this_frame
,
2494 *pc
= get_frame_address_in_block (this_frame
);
2496 catch (const gdb_exception_error
&ex
)
2498 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2507 find_frame_sal (frame_info
*frame
)
2509 struct frame_info
*next_frame
;
2513 /* If the next frame represents an inlined function call, this frame's
2514 sal is the "call site" of that inlined function, which can not
2515 be inferred from get_frame_pc. */
2516 next_frame
= get_next_frame (frame
);
2517 if (frame_inlined_callees (frame
) > 0)
2522 sym
= get_frame_function (next_frame
);
2524 sym
= inline_skipped_symbol (inferior_thread ());
2526 /* If frame is inline, it certainly has symbols. */
2529 symtab_and_line sal
;
2530 if (SYMBOL_LINE (sym
) != 0)
2532 sal
.symtab
= symbol_symtab (sym
);
2533 sal
.line
= SYMBOL_LINE (sym
);
2536 /* If the symbol does not have a location, we don't know where
2537 the call site is. Do not pretend to. This is jarring, but
2538 we can't do much better. */
2539 sal
.pc
= get_frame_pc (frame
);
2541 sal
.pspace
= get_frame_program_space (frame
);
2545 /* If FRAME is not the innermost frame, that normally means that
2546 FRAME->pc points at the return instruction (which is *after* the
2547 call instruction), and we want to get the line containing the
2548 call (because the call is where the user thinks the program is).
2549 However, if the next frame is either a SIGTRAMP_FRAME or a
2550 DUMMY_FRAME, then the next frame will contain a saved interrupt
2551 PC and such a PC indicates the current (rather than next)
2552 instruction/line, consequently, for such cases, want to get the
2553 line containing fi->pc. */
2554 if (!get_frame_pc_if_available (frame
, &pc
))
2557 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2558 return find_pc_line (pc
, notcurrent
);
2561 /* Per "frame.h", return the ``address'' of the frame. Code should
2562 really be using get_frame_id(). */
2564 get_frame_base (struct frame_info
*fi
)
2566 return get_frame_id (fi
).stack_addr
;
2569 /* High-level offsets into the frame. Used by the debug info. */
2572 get_frame_base_address (struct frame_info
*fi
)
2574 if (get_frame_type (fi
) != NORMAL_FRAME
)
2576 if (fi
->base
== NULL
)
2577 fi
->base
= frame_base_find_by_frame (fi
);
2578 /* Sneaky: If the low-level unwind and high-level base code share a
2579 common unwinder, let them share the prologue cache. */
2580 if (fi
->base
->unwind
== fi
->unwind
)
2581 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2582 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2586 get_frame_locals_address (struct frame_info
*fi
)
2588 if (get_frame_type (fi
) != NORMAL_FRAME
)
2590 /* If there isn't a frame address method, find it. */
2591 if (fi
->base
== NULL
)
2592 fi
->base
= frame_base_find_by_frame (fi
);
2593 /* Sneaky: If the low-level unwind and high-level base code share a
2594 common unwinder, let them share the prologue cache. */
2595 if (fi
->base
->unwind
== fi
->unwind
)
2596 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2597 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2601 get_frame_args_address (struct frame_info
*fi
)
2603 if (get_frame_type (fi
) != NORMAL_FRAME
)
2605 /* If there isn't a frame address method, find it. */
2606 if (fi
->base
== NULL
)
2607 fi
->base
= frame_base_find_by_frame (fi
);
2608 /* Sneaky: If the low-level unwind and high-level base code share a
2609 common unwinder, let them share the prologue cache. */
2610 if (fi
->base
->unwind
== fi
->unwind
)
2611 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2612 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2615 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2619 frame_unwinder_is (struct frame_info
*fi
, const struct frame_unwind
*unwinder
)
2621 if (fi
->unwind
== NULL
)
2622 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2623 return fi
->unwind
== unwinder
;
2626 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2627 or -1 for a NULL frame. */
2630 frame_relative_level (struct frame_info
*fi
)
2639 get_frame_type (struct frame_info
*frame
)
2641 if (frame
->unwind
== NULL
)
2642 /* Initialize the frame's unwinder because that's what
2643 provides the frame's type. */
2644 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2645 return frame
->unwind
->type
;
2648 struct program_space
*
2649 get_frame_program_space (struct frame_info
*frame
)
2651 return frame
->pspace
;
2654 struct program_space
*
2655 frame_unwind_program_space (struct frame_info
*this_frame
)
2657 gdb_assert (this_frame
);
2659 /* This is really a placeholder to keep the API consistent --- we
2660 assume for now that we don't have frame chains crossing
2662 return this_frame
->pspace
;
2665 const address_space
*
2666 get_frame_address_space (struct frame_info
*frame
)
2668 return frame
->aspace
;
2671 /* Memory access methods. */
2674 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2675 gdb_byte
*buf
, int len
)
2677 read_memory (addr
, buf
, len
);
2681 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2684 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2685 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2687 return read_memory_integer (addr
, len
, byte_order
);
2691 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2694 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2695 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2697 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2701 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2702 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2704 /* NOTE: target_read_memory returns zero on success! */
2705 return !target_read_memory (addr
, buf
, len
);
2708 /* Architecture methods. */
2711 get_frame_arch (struct frame_info
*this_frame
)
2713 return frame_unwind_arch (this_frame
->next
);
2717 frame_unwind_arch (struct frame_info
*next_frame
)
2719 if (!next_frame
->prev_arch
.p
)
2721 struct gdbarch
*arch
;
2723 if (next_frame
->unwind
== NULL
)
2724 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2726 if (next_frame
->unwind
->prev_arch
!= NULL
)
2727 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2728 &next_frame
->prologue_cache
);
2730 arch
= get_frame_arch (next_frame
);
2732 next_frame
->prev_arch
.arch
= arch
;
2733 next_frame
->prev_arch
.p
= 1;
2735 fprintf_unfiltered (gdb_stdlog
,
2736 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2738 gdbarch_bfd_arch_info (arch
)->printable_name
);
2741 return next_frame
->prev_arch
.arch
;
2745 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2747 next_frame
= skip_artificial_frames (next_frame
);
2749 /* We must have a non-artificial frame. The caller is supposed to check
2750 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2752 gdb_assert (next_frame
!= NULL
);
2754 return frame_unwind_arch (next_frame
);
2757 /* Gets the language of FRAME. */
2760 get_frame_language (struct frame_info
*frame
)
2765 gdb_assert (frame
!= NULL
);
2767 /* We determine the current frame language by looking up its
2768 associated symtab. To retrieve this symtab, we use the frame
2769 PC. However we cannot use the frame PC as is, because it
2770 usually points to the instruction following the "call", which
2771 is sometimes the first instruction of another function. So
2772 we rely on get_frame_address_in_block(), it provides us with
2773 a PC that is guaranteed to be inside the frame's code
2778 pc
= get_frame_address_in_block (frame
);
2781 catch (const gdb_exception_error
&ex
)
2783 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
2789 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
2792 return compunit_language (cust
);
2795 return language_unknown
;
2798 /* Stack pointer methods. */
2801 get_frame_sp (struct frame_info
*this_frame
)
2803 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2805 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2806 operate on THIS_FRAME now. */
2807 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2810 /* Return the reason why we can't unwind past FRAME. */
2812 enum unwind_stop_reason
2813 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2815 /* Fill-in STOP_REASON. */
2816 get_prev_frame_always (frame
);
2817 gdb_assert (frame
->prev_p
);
2819 return frame
->stop_reason
;
2822 /* Return a string explaining REASON. */
2825 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
2829 #define SET(name, description) \
2830 case name: return _(description);
2831 #include "unwind_stop_reasons.def"
2835 internal_error (__FILE__
, __LINE__
,
2836 "Invalid frame stop reason");
2841 frame_stop_reason_string (struct frame_info
*fi
)
2843 gdb_assert (fi
->prev_p
);
2844 gdb_assert (fi
->prev
== NULL
);
2846 /* Return the specific string if we have one. */
2847 if (fi
->stop_string
!= NULL
)
2848 return fi
->stop_string
;
2850 /* Return the generic string if we have nothing better. */
2851 return unwind_stop_reason_to_string (fi
->stop_reason
);
2854 /* Return the enum symbol name of REASON as a string, to use in debug
2858 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
2862 #define SET(name, description) \
2863 case name: return #name;
2864 #include "unwind_stop_reasons.def"
2868 internal_error (__FILE__
, __LINE__
,
2869 "Invalid frame stop reason");
2873 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2877 frame_cleanup_after_sniffer (struct frame_info
*frame
)
2879 /* The sniffer should not allocate a prologue cache if it did not
2880 match this frame. */
2881 gdb_assert (frame
->prologue_cache
== NULL
);
2883 /* No sniffer should extend the frame chain; sniff based on what is
2885 gdb_assert (!frame
->prev_p
);
2887 /* The sniffer should not check the frame's ID; that's circular. */
2888 gdb_assert (!frame
->this_id
.p
);
2890 /* Clear cached fields dependent on the unwinder.
2892 The previous PC is independent of the unwinder, but the previous
2893 function is not (see get_frame_address_in_block). */
2894 frame
->prev_func
.p
= 0;
2895 frame
->prev_func
.addr
= 0;
2897 /* Discard the unwinder last, so that we can easily find it if an assertion
2898 in this function triggers. */
2899 frame
->unwind
= NULL
;
2902 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2903 If sniffing fails, the caller should be sure to call
2904 frame_cleanup_after_sniffer. */
2907 frame_prepare_for_sniffer (struct frame_info
*frame
,
2908 const struct frame_unwind
*unwind
)
2910 gdb_assert (frame
->unwind
== NULL
);
2911 frame
->unwind
= unwind
;
2914 static struct cmd_list_element
*set_backtrace_cmdlist
;
2915 static struct cmd_list_element
*show_backtrace_cmdlist
;
2918 set_backtrace_cmd (const char *args
, int from_tty
)
2920 help_list (set_backtrace_cmdlist
, "set backtrace ", all_commands
,
2925 show_backtrace_cmd (const char *args
, int from_tty
)
2927 cmd_show_list (show_backtrace_cmdlist
, from_tty
, "");
2930 /* Definition of the "set backtrace" settings that are exposed as
2931 "backtrace" command options. */
2933 using boolean_option_def
2934 = gdb::option::boolean_option_def
<set_backtrace_options
>;
2935 using uinteger_option_def
2936 = gdb::option::uinteger_option_def
<set_backtrace_options
>;
2938 const gdb::option::option_def set_backtrace_option_defs
[] = {
2940 boolean_option_def
{
2942 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_main
; },
2943 show_backtrace_past_main
, /* show_cmd_cb */
2944 N_("Set whether backtraces should continue past \"main\"."),
2945 N_("Show whether backtraces should continue past \"main\"."),
2946 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2947 the backtrace at \"main\". Set this if you need to see the rest\n\
2948 of the stack trace."),
2951 boolean_option_def
{
2953 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_entry
; },
2954 show_backtrace_past_entry
, /* show_cmd_cb */
2955 N_("Set whether backtraces should continue past the entry point of a program."),
2956 N_("Show whether backtraces should continue past the entry point of a program."),
2957 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
2958 will terminate the backtrace there. Set this if you need to see\n\
2959 the rest of the stack trace."),
2964 _initialize_frame (void)
2966 obstack_init (&frame_cache_obstack
);
2968 frame_stash_create ();
2970 gdb::observers::target_changed
.attach (frame_observer_target_changed
);
2972 add_prefix_cmd ("backtrace", class_maintenance
, set_backtrace_cmd
, _("\
2973 Set backtrace specific variables.\n\
2974 Configure backtrace variables such as the backtrace limit"),
2975 &set_backtrace_cmdlist
, "set backtrace ",
2976 0/*allow-unknown*/, &setlist
);
2977 add_prefix_cmd ("backtrace", class_maintenance
, show_backtrace_cmd
, _("\
2978 Show backtrace specific variables.\n\
2979 Show backtrace variables such as the backtrace limit."),
2980 &show_backtrace_cmdlist
, "show backtrace ",
2981 0/*allow-unknown*/, &showlist
);
2983 add_setshow_uinteger_cmd ("limit", class_obscure
,
2984 &user_set_backtrace_options
.backtrace_limit
, _("\
2985 Set an upper bound on the number of backtrace levels."), _("\
2986 Show the upper bound on the number of backtrace levels."), _("\
2987 No more than the specified number of frames can be displayed or examined.\n\
2988 Literal \"unlimited\" or zero means no limit."),
2990 show_backtrace_limit
,
2991 &set_backtrace_cmdlist
,
2992 &show_backtrace_cmdlist
);
2994 gdb::option::add_setshow_cmds_for_options
2995 (class_stack
, &user_set_backtrace_options
,
2996 set_backtrace_option_defs
, &set_backtrace_cmdlist
, &show_backtrace_cmdlist
);
2998 /* Debug this files internals. */
2999 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
3000 Set frame debugging."), _("\
3001 Show frame debugging."), _("\
3002 When non-zero, frame specific internal debugging is enabled."),
3005 &setdebuglist
, &showdebuglist
);