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 enum class frame_id_status
92 /* Frame id is not computed. */
95 /* Frame id is being computed (compute_frame_id is active). */
98 /* Frame id has been computed. */
102 /* We keep a cache of stack frames, each of which is a "struct
103 frame_info". The innermost one gets allocated (in
104 wait_for_inferior) each time the inferior stops; sentinel_frame
105 points to it. Additional frames get allocated (in get_prev_frame)
106 as needed, and are chained through the next and prev fields. Any
107 time that the frame cache becomes invalid (most notably when we
108 execute something, but also if we change how we interpret the
109 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
110 which reads new symbols)), we should call reinit_frame_cache. */
114 /* Level of this frame. The inner-most (youngest) frame is at level
115 0. As you move towards the outer-most (oldest) frame, the level
116 increases. This is a cached value. It could just as easily be
117 computed by counting back from the selected frame to the inner
119 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
120 reserved to indicate a bogus frame - one that has been created
121 just to keep GDB happy (GDB always needs a frame). For the
122 moment leave this as speculation. */
125 /* The frame's program space. */
126 struct program_space
*pspace
;
128 /* The frame's address space. */
129 const address_space
*aspace
;
131 /* The frame's low-level unwinder and corresponding cache. The
132 low-level unwinder is responsible for unwinding register values
133 for the previous frame. The low-level unwind methods are
134 selected based on the presence, or otherwise, of register unwind
135 information such as CFI. */
136 void *prologue_cache
;
137 const struct frame_unwind
*unwind
;
139 /* Cached copy of the previous frame's architecture. */
143 struct gdbarch
*arch
;
146 /* Cached copy of the previous frame's resume address. */
148 cached_copy_status status
;
149 /* Did VALUE require unmasking when being read. */
154 /* Cached copy of the previous frame's function address. */
158 cached_copy_status status
;
161 /* This frame's ID. */
165 struct frame_id value
;
168 /* The frame's high-level base methods, and corresponding cache.
169 The high level base methods are selected based on the frame's
171 const struct frame_base
*base
;
174 /* Pointers to the next (down, inner, younger) and previous (up,
175 outer, older) frame_info's in the frame cache. */
176 struct frame_info
*next
; /* down, inner, younger */
178 struct frame_info
*prev
; /* up, outer, older */
180 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
181 could. Only valid when PREV_P is set. */
182 enum unwind_stop_reason stop_reason
;
184 /* A frame specific string describing the STOP_REASON in more detail.
185 Only valid when PREV_P is set, but even then may still be NULL. */
186 const char *stop_string
;
192 set_frame_previous_pc_masked (struct frame_info
*frame
)
194 frame
->prev_pc
.masked
= true;
200 get_frame_pc_masked (const struct frame_info
*frame
)
202 gdb_assert (frame
->next
!= nullptr);
203 gdb_assert (frame
->next
->prev_pc
.status
== CC_VALUE
);
205 return frame
->next
->prev_pc
.masked
;
208 /* A frame stash used to speed up frame lookups. Create a hash table
209 to stash frames previously accessed from the frame cache for
210 quicker subsequent retrieval. The hash table is emptied whenever
211 the frame cache is invalidated. */
213 static htab_t frame_stash
;
215 /* Internal function to calculate a hash from the frame_id addresses,
216 using as many valid addresses as possible. Frames below level 0
217 are not stored in the hash table. */
220 frame_addr_hash (const void *ap
)
222 const struct frame_info
*frame
= (const struct frame_info
*) ap
;
223 const struct frame_id f_id
= frame
->this_id
.value
;
226 gdb_assert (f_id
.stack_status
!= FID_STACK_INVALID
228 || f_id
.special_addr_p
);
230 if (f_id
.stack_status
== FID_STACK_VALID
)
231 hash
= iterative_hash (&f_id
.stack_addr
,
232 sizeof (f_id
.stack_addr
), hash
);
233 if (f_id
.code_addr_p
)
234 hash
= iterative_hash (&f_id
.code_addr
,
235 sizeof (f_id
.code_addr
), hash
);
236 if (f_id
.special_addr_p
)
237 hash
= iterative_hash (&f_id
.special_addr
,
238 sizeof (f_id
.special_addr
), hash
);
243 /* Internal equality function for the hash table. This function
244 defers equality operations to frame_id_eq. */
247 frame_addr_hash_eq (const void *a
, const void *b
)
249 const struct frame_info
*f_entry
= (const struct frame_info
*) a
;
250 const struct frame_info
*f_element
= (const struct frame_info
*) b
;
252 return frame_id_eq (f_entry
->this_id
.value
,
253 f_element
->this_id
.value
);
256 /* Internal function to create the frame_stash hash table. 100 seems
257 to be a good compromise to start the hash table at. */
260 frame_stash_create (void)
262 frame_stash
= htab_create (100,
268 /* Internal function to add a frame to the frame_stash hash table.
269 Returns false if a frame with the same ID was already stashed, true
273 frame_stash_add (frame_info
*frame
)
275 /* Do not try to stash the sentinel frame. */
276 gdb_assert (frame
->level
>= 0);
278 frame_info
**slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
281 /* If we already have a frame in the stack with the same id, we
282 either have a stack cycle (corrupted stack?), or some bug
283 elsewhere in GDB. In any case, ignore the duplicate and return
284 an indication to the caller. */
285 if (*slot
!= nullptr)
292 /* Internal function to search the frame stash for an entry with the
293 given frame ID. If found, return that frame. Otherwise return
296 static struct frame_info
*
297 frame_stash_find (struct frame_id id
)
299 struct frame_info dummy
;
300 struct frame_info
*frame
;
302 dummy
.this_id
.value
= id
;
303 frame
= (struct frame_info
*) htab_find (frame_stash
, &dummy
);
307 /* Internal function to invalidate the frame stash by removing all
308 entries in it. This only occurs when the frame cache is
312 frame_stash_invalidate (void)
314 htab_empty (frame_stash
);
318 scoped_restore_selected_frame::scoped_restore_selected_frame ()
320 m_fid
= get_frame_id (get_selected_frame (NULL
));
324 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
326 frame_info
*frame
= frame_find_by_id (m_fid
);
328 warning (_("Unable to restore previously selected frame."));
330 select_frame (frame
);
333 /* Flag to control debugging. */
335 unsigned int frame_debug
;
337 show_frame_debug (struct ui_file
*file
, int from_tty
,
338 struct cmd_list_element
*c
, const char *value
)
340 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
343 /* Implementation of "show backtrace past-main". */
346 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
347 struct cmd_list_element
*c
, const char *value
)
349 fprintf_filtered (file
,
350 _("Whether backtraces should "
351 "continue past \"main\" is %s.\n"),
355 /* Implementation of "show backtrace past-entry". */
358 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
359 struct cmd_list_element
*c
, const char *value
)
361 fprintf_filtered (file
, _("Whether backtraces should continue past the "
362 "entry point of a program is %s.\n"),
366 /* Implementation of "show backtrace limit". */
369 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
370 struct cmd_list_element
*c
, const char *value
)
372 fprintf_filtered (file
,
373 _("An upper bound on the number "
374 "of backtrace levels is %s.\n"),
380 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
383 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
385 fprintf_unfiltered (file
, "!%s", name
);
389 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
391 fprintf_unfiltered (file
, "{");
393 if (id
.stack_status
== FID_STACK_INVALID
)
394 fprintf_unfiltered (file
, "!stack");
395 else if (id
.stack_status
== FID_STACK_UNAVAILABLE
)
396 fprintf_unfiltered (file
, "stack=<unavailable>");
397 else if (id
.stack_status
== FID_STACK_SENTINEL
)
398 fprintf_unfiltered (file
, "stack=<sentinel>");
400 fprintf_unfiltered (file
, "stack=%s", hex_string (id
.stack_addr
));
401 fprintf_unfiltered (file
, ",");
403 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
404 fprintf_unfiltered (file
, ",");
406 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
408 if (id
.artificial_depth
)
409 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
411 fprintf_unfiltered (file
, "}");
415 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
420 fprintf_unfiltered (file
, "NORMAL_FRAME");
423 fprintf_unfiltered (file
, "DUMMY_FRAME");
426 fprintf_unfiltered (file
, "INLINE_FRAME");
429 fprintf_unfiltered (file
, "TAILCALL_FRAME");
432 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
435 fprintf_unfiltered (file
, "ARCH_FRAME");
438 fprintf_unfiltered (file
, "SENTINEL_FRAME");
441 fprintf_unfiltered (file
, "<unknown type>");
447 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
451 fprintf_unfiltered (file
, "<NULL frame>");
455 fprintf_unfiltered (file
, "{");
456 fprintf_unfiltered (file
, "level=%d", fi
->level
);
457 fprintf_unfiltered (file
, ",");
459 fprintf_unfiltered (file
, "type=");
460 if (fi
->unwind
!= NULL
)
461 fprint_frame_type (file
, fi
->unwind
->type
);
463 fprintf_unfiltered (file
, "<unknown>");
464 fprintf_unfiltered (file
, ",");
466 fprintf_unfiltered (file
, "unwind=");
467 if (fi
->unwind
!= NULL
)
468 gdb_print_host_address (fi
->unwind
, file
);
470 fprintf_unfiltered (file
, "<unknown>");
471 fprintf_unfiltered (file
, ",");
473 fprintf_unfiltered (file
, "pc=");
474 if (fi
->next
== NULL
|| fi
->next
->prev_pc
.status
== CC_UNKNOWN
)
475 fprintf_unfiltered (file
, "<unknown>");
476 else if (fi
->next
->prev_pc
.status
== CC_VALUE
)
478 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_pc
.value
));
479 if (fi
->next
->prev_pc
.masked
)
480 fprintf_unfiltered (file
, "[PAC]");
482 else if (fi
->next
->prev_pc
.status
== CC_NOT_SAVED
)
483 val_print_not_saved (file
);
484 else if (fi
->next
->prev_pc
.status
== CC_UNAVAILABLE
)
485 val_print_unavailable (file
);
486 fprintf_unfiltered (file
, ",");
488 fprintf_unfiltered (file
, "id=");
489 if (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
)
490 fprintf_unfiltered (file
, "<not computed>");
491 else if (fi
->this_id
.p
== frame_id_status::COMPUTING
)
492 fprintf_unfiltered (file
, "<computing>");
494 fprint_frame_id (file
, fi
->this_id
.value
);
495 fprintf_unfiltered (file
, ",");
497 fprintf_unfiltered (file
, "func=");
498 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.status
== CC_VALUE
)
499 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
501 fprintf_unfiltered (file
, "<unknown>");
502 fprintf_unfiltered (file
, "}");
505 /* Given FRAME, return the enclosing frame as found in real frames read-in from
506 inferior memory. Skip any previous frames which were made up by GDB.
507 Return FRAME if FRAME is a non-artificial frame.
508 Return NULL if FRAME is the start of an artificial-only chain. */
510 static struct frame_info
*
511 skip_artificial_frames (struct frame_info
*frame
)
513 /* Note we use get_prev_frame_always, and not get_prev_frame. The
514 latter will truncate the frame chain, leading to this function
515 unintentionally returning a null_frame_id (e.g., when the user
516 sets a backtrace limit).
518 Note that for record targets we may get a frame chain that consists
519 of artificial frames only. */
520 while (get_frame_type (frame
) == INLINE_FRAME
521 || get_frame_type (frame
) == TAILCALL_FRAME
)
523 frame
= get_prev_frame_always (frame
);
532 skip_unwritable_frames (struct frame_info
*frame
)
534 while (gdbarch_code_of_frame_writable (get_frame_arch (frame
), frame
) == 0)
536 frame
= get_prev_frame (frame
);
547 skip_tailcall_frames (struct frame_info
*frame
)
549 while (get_frame_type (frame
) == TAILCALL_FRAME
)
551 /* Note that for record targets we may get a frame chain that consists of
552 tailcall frames only. */
553 frame
= get_prev_frame (frame
);
561 /* Compute the frame's uniq ID that can be used to, later, re-find the
565 compute_frame_id (struct frame_info
*fi
)
567 gdb_assert (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
);
569 unsigned int entry_generation
= get_frame_cache_generation ();
573 /* Mark this frame's id as "being computed. */
574 fi
->this_id
.p
= frame_id_status::COMPUTING
;
577 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
580 /* Find the unwinder. */
581 if (fi
->unwind
== NULL
)
582 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
584 /* Find THIS frame's ID. */
585 /* Default to outermost if no ID is found. */
586 fi
->this_id
.value
= outer_frame_id
;
587 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
588 gdb_assert (frame_id_p (fi
->this_id
.value
));
590 /* Mark this frame's id as "computed". */
591 fi
->this_id
.p
= frame_id_status::COMPUTED
;
595 fprintf_unfiltered (gdb_stdlog
, "-> ");
596 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
597 fprintf_unfiltered (gdb_stdlog
, " }\n");
600 catch (const gdb_exception
&ex
)
602 /* On error, revert the frame id status to not computed. If the frame
603 cache generation changed, the frame object doesn't exist anymore, so
605 if (get_frame_cache_generation () == entry_generation
)
606 fi
->this_id
.p
= frame_id_status::NOT_COMPUTED
;
612 /* Return a frame uniq ID that can be used to, later, re-find the
616 get_frame_id (struct frame_info
*fi
)
619 return null_frame_id
;
621 /* It's always invalid to try to get a frame's id while it is being
623 gdb_assert (fi
->this_id
.p
!= frame_id_status::COMPUTING
);
625 if (fi
->this_id
.p
== frame_id_status::NOT_COMPUTED
)
627 /* If we haven't computed the frame id yet, then it must be that
628 this is the current frame. Compute it now, and stash the
629 result. The IDs of other frames are computed as soon as
630 they're created, in order to detect cycles. See
631 get_prev_frame_if_no_cycle. */
632 gdb_assert (fi
->level
== 0);
635 compute_frame_id (fi
);
637 /* Since this is the first frame in the chain, this should
639 bool stashed
= frame_stash_add (fi
);
640 gdb_assert (stashed
);
643 return fi
->this_id
.value
;
647 get_stack_frame_id (struct frame_info
*next_frame
)
649 return get_frame_id (skip_artificial_frames (next_frame
));
653 frame_unwind_caller_id (struct frame_info
*next_frame
)
655 struct frame_info
*this_frame
;
657 /* Use get_prev_frame_always, and not get_prev_frame. The latter
658 will truncate the frame chain, leading to this function
659 unintentionally returning a null_frame_id (e.g., when a caller
660 requests the frame ID of "main()"s caller. */
662 next_frame
= skip_artificial_frames (next_frame
);
663 if (next_frame
== NULL
)
664 return null_frame_id
;
666 this_frame
= get_prev_frame_always (next_frame
);
668 return get_frame_id (skip_artificial_frames (this_frame
));
670 return null_frame_id
;
673 const struct frame_id null_frame_id
= { 0 }; /* All zeros. */
674 const struct frame_id sentinel_frame_id
= { 0, 0, 0, FID_STACK_SENTINEL
, 0, 1, 0 };
675 const struct frame_id outer_frame_id
= { 0, 0, 0, FID_STACK_INVALID
, 0, 1, 0 };
678 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
679 CORE_ADDR special_addr
)
681 struct frame_id id
= null_frame_id
;
683 id
.stack_addr
= stack_addr
;
684 id
.stack_status
= FID_STACK_VALID
;
685 id
.code_addr
= code_addr
;
686 id
.code_addr_p
= true;
687 id
.special_addr
= special_addr
;
688 id
.special_addr_p
= true;
695 frame_id_build_unavailable_stack (CORE_ADDR code_addr
)
697 struct frame_id id
= null_frame_id
;
699 id
.stack_status
= FID_STACK_UNAVAILABLE
;
700 id
.code_addr
= code_addr
;
701 id
.code_addr_p
= true;
708 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr
,
709 CORE_ADDR special_addr
)
711 struct frame_id id
= null_frame_id
;
713 id
.stack_status
= FID_STACK_UNAVAILABLE
;
714 id
.code_addr
= code_addr
;
715 id
.code_addr_p
= true;
716 id
.special_addr
= special_addr
;
717 id
.special_addr_p
= true;
722 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
724 struct frame_id id
= null_frame_id
;
726 id
.stack_addr
= stack_addr
;
727 id
.stack_status
= FID_STACK_VALID
;
728 id
.code_addr
= code_addr
;
729 id
.code_addr_p
= true;
734 frame_id_build_wild (CORE_ADDR stack_addr
)
736 struct frame_id id
= null_frame_id
;
738 id
.stack_addr
= stack_addr
;
739 id
.stack_status
= FID_STACK_VALID
;
744 frame_id_p (frame_id l
)
746 /* The frame is valid iff it has a valid stack address. */
747 bool p
= l
.stack_status
!= FID_STACK_INVALID
;
749 /* outer_frame_id is also valid. */
750 if (!p
&& memcmp (&l
, &outer_frame_id
, sizeof (l
)) == 0)
755 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
756 fprint_frame_id (gdb_stdlog
, l
);
757 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
764 frame_id_artificial_p (frame_id l
)
769 return l
.artificial_depth
!= 0;
773 frame_id_eq (frame_id l
, frame_id r
)
777 if (l
.stack_status
== FID_STACK_INVALID
&& l
.special_addr_p
778 && r
.stack_status
== FID_STACK_INVALID
&& r
.special_addr_p
)
779 /* The outermost frame marker is equal to itself. This is the
780 dodgy thing about outer_frame_id, since between execution steps
781 we might step into another function - from which we can't
782 unwind either. More thought required to get rid of
785 else if (l
.stack_status
== FID_STACK_INVALID
786 || r
.stack_status
== FID_STACK_INVALID
)
787 /* Like a NaN, if either ID is invalid, the result is false.
788 Note that a frame ID is invalid iff it is the null frame ID. */
790 else if (l
.stack_status
!= r
.stack_status
|| l
.stack_addr
!= r
.stack_addr
)
791 /* If .stack addresses are different, the frames are different. */
793 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
794 /* An invalid code addr is a wild card. If .code addresses are
795 different, the frames are different. */
797 else if (l
.special_addr_p
&& r
.special_addr_p
798 && l
.special_addr
!= r
.special_addr
)
799 /* An invalid special addr is a wild card (or unused). Otherwise
800 if special addresses are different, the frames are different. */
802 else if (l
.artificial_depth
!= r
.artificial_depth
)
803 /* If artificial depths are different, the frames must be different. */
806 /* Frames are equal. */
811 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
812 fprint_frame_id (gdb_stdlog
, l
);
813 fprintf_unfiltered (gdb_stdlog
, ",r=");
814 fprint_frame_id (gdb_stdlog
, r
);
815 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
821 /* Safety net to check whether frame ID L should be inner to
822 frame ID R, according to their stack addresses.
824 This method cannot be used to compare arbitrary frames, as the
825 ranges of valid stack addresses may be discontiguous (e.g. due
828 However, it can be used as safety net to discover invalid frame
829 IDs in certain circumstances. Assuming that NEXT is the immediate
830 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
832 * The stack address of NEXT must be inner-than-or-equal to the stack
835 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
838 * If NEXT and THIS have different stack addresses, no other frame
839 in the frame chain may have a stack address in between.
841 Therefore, if frame_id_inner (TEST, THIS) holds, but
842 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
843 to a valid frame in the frame chain.
845 The sanity checks above cannot be performed when a SIGTRAMP frame
846 is involved, because signal handlers might be executed on a different
847 stack than the stack used by the routine that caused the signal
848 to be raised. This can happen for instance when a thread exceeds
849 its maximum stack size. In this case, certain compilers implement
850 a stack overflow strategy that cause the handler to be run on a
854 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
858 if (l
.stack_status
!= FID_STACK_VALID
|| r
.stack_status
!= FID_STACK_VALID
)
859 /* Like NaN, any operation involving an invalid ID always fails.
860 Likewise if either ID has an unavailable stack address. */
862 else if (l
.artificial_depth
> r
.artificial_depth
863 && l
.stack_addr
== r
.stack_addr
864 && l
.code_addr_p
== r
.code_addr_p
865 && l
.special_addr_p
== r
.special_addr_p
866 && l
.special_addr
== r
.special_addr
)
868 /* Same function, different inlined functions. */
869 const struct block
*lb
, *rb
;
871 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
873 lb
= block_for_pc (l
.code_addr
);
874 rb
= block_for_pc (r
.code_addr
);
876 if (lb
== NULL
|| rb
== NULL
)
877 /* Something's gone wrong. */
880 /* This will return true if LB and RB are the same block, or
881 if the block with the smaller depth lexically encloses the
882 block with the greater depth. */
883 inner
= contained_in (lb
, rb
);
886 /* Only return non-zero when strictly inner than. Note that, per
887 comment in "frame.h", there is some fuzz here. Frameless
888 functions are not strictly inner than (same .stack but
889 different .code and/or .special address). */
890 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
894 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
895 fprint_frame_id (gdb_stdlog
, l
);
896 fprintf_unfiltered (gdb_stdlog
, ",r=");
897 fprint_frame_id (gdb_stdlog
, r
);
898 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
905 frame_find_by_id (struct frame_id id
)
907 struct frame_info
*frame
, *prev_frame
;
909 /* ZERO denotes the null frame, let the caller decide what to do
910 about it. Should it instead return get_current_frame()? */
911 if (!frame_id_p (id
))
914 /* Check for the sentinel frame. */
915 if (frame_id_eq (id
, sentinel_frame_id
))
916 return sentinel_frame
;
918 /* Try using the frame stash first. Finding it there removes the need
919 to perform the search by looping over all frames, which can be very
920 CPU-intensive if the number of frames is very high (the loop is O(n)
921 and get_prev_frame performs a series of checks that are relatively
922 expensive). This optimization is particularly useful when this function
923 is called from another function (such as value_fetch_lazy, case
924 VALUE_LVAL (val) == lval_register) which already loops over all frames,
925 making the overall behavior O(n^2). */
926 frame
= frame_stash_find (id
);
930 for (frame
= get_current_frame (); ; frame
= prev_frame
)
932 struct frame_id self
= get_frame_id (frame
);
934 if (frame_id_eq (id
, self
))
935 /* An exact match. */
938 prev_frame
= get_prev_frame (frame
);
942 /* As a safety net to avoid unnecessary backtracing while trying
943 to find an invalid ID, we check for a common situation where
944 we can detect from comparing stack addresses that no other
945 frame in the current frame chain can have this ID. See the
946 comment at frame_id_inner for details. */
947 if (get_frame_type (frame
) == NORMAL_FRAME
948 && !frame_id_inner (get_frame_arch (frame
), id
, self
)
949 && frame_id_inner (get_frame_arch (prev_frame
), id
,
950 get_frame_id (prev_frame
)))
957 frame_unwind_pc (struct frame_info
*this_frame
)
959 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
961 struct gdbarch
*prev_gdbarch
;
965 /* The right way. The `pure' way. The one true way. This
966 method depends solely on the register-unwind code to
967 determine the value of registers in THIS frame, and hence
968 the value of this frame's PC (resume address). A typical
969 implementation is no more than:
971 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
972 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
974 Note: this method is very heavily dependent on a correct
975 register-unwind implementation, it pays to fix that
976 method first; this method is frame type agnostic, since
977 it only deals with register values, it works with any
978 frame. This is all in stark contrast to the old
979 FRAME_SAVED_PC which would try to directly handle all the
980 different ways that a PC could be unwound. */
981 prev_gdbarch
= frame_unwind_arch (this_frame
);
985 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
988 catch (const gdb_exception_error
&ex
)
990 if (ex
.error
== NOT_AVAILABLE_ERROR
)
992 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
995 fprintf_unfiltered (gdb_stdlog
,
996 "{ frame_unwind_pc (this_frame=%d)"
997 " -> <unavailable> }\n",
1000 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
1002 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
1005 fprintf_unfiltered (gdb_stdlog
,
1006 "{ frame_unwind_pc (this_frame=%d)"
1007 " -> <not saved> }\n",
1016 this_frame
->prev_pc
.value
= pc
;
1017 this_frame
->prev_pc
.status
= CC_VALUE
;
1019 fprintf_unfiltered (gdb_stdlog
,
1020 "{ frame_unwind_pc (this_frame=%d) "
1023 hex_string (this_frame
->prev_pc
.value
));
1027 if (this_frame
->prev_pc
.status
== CC_VALUE
)
1028 return this_frame
->prev_pc
.value
;
1029 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
1030 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1031 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
1032 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
1034 internal_error (__FILE__
, __LINE__
,
1035 "unexpected prev_pc status: %d",
1036 (int) this_frame
->prev_pc
.status
);
1040 frame_unwind_caller_pc (struct frame_info
*this_frame
)
1042 this_frame
= skip_artificial_frames (this_frame
);
1044 /* We must have a non-artificial frame. The caller is supposed to check
1045 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
1047 gdb_assert (this_frame
!= NULL
);
1049 return frame_unwind_pc (this_frame
);
1053 get_frame_func_if_available (frame_info
*this_frame
, CORE_ADDR
*pc
)
1055 struct frame_info
*next_frame
= this_frame
->next
;
1057 if (next_frame
->prev_func
.status
== CC_UNKNOWN
)
1059 CORE_ADDR addr_in_block
;
1061 /* Make certain that this, and not the adjacent, function is
1063 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
1065 next_frame
->prev_func
.status
= CC_UNAVAILABLE
;
1067 fprintf_unfiltered (gdb_stdlog
,
1068 "{ get_frame_func (this_frame=%d)"
1069 " -> unavailable }\n",
1074 next_frame
->prev_func
.status
= CC_VALUE
;
1075 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
1077 fprintf_unfiltered (gdb_stdlog
,
1078 "{ get_frame_func (this_frame=%d) -> %s }\n",
1080 hex_string (next_frame
->prev_func
.addr
));
1084 if (next_frame
->prev_func
.status
== CC_UNAVAILABLE
)
1091 gdb_assert (next_frame
->prev_func
.status
== CC_VALUE
);
1093 *pc
= next_frame
->prev_func
.addr
;
1099 get_frame_func (struct frame_info
*this_frame
)
1103 if (!get_frame_func_if_available (this_frame
, &pc
))
1104 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
1109 std::unique_ptr
<readonly_detached_regcache
>
1110 frame_save_as_regcache (struct frame_info
*this_frame
)
1112 auto cooked_read
= [this_frame
] (int regnum
, gdb_byte
*buf
)
1114 if (!deprecated_frame_register_read (this_frame
, regnum
, buf
))
1115 return REG_UNAVAILABLE
;
1120 std::unique_ptr
<readonly_detached_regcache
> regcache
1121 (new readonly_detached_regcache (get_frame_arch (this_frame
), cooked_read
));
1127 frame_pop (struct frame_info
*this_frame
)
1129 struct frame_info
*prev_frame
;
1131 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
1133 /* Popping a dummy frame involves restoring more than just registers.
1134 dummy_frame_pop does all the work. */
1135 dummy_frame_pop (get_frame_id (this_frame
), inferior_thread ());
1139 /* Ensure that we have a frame to pop to. */
1140 prev_frame
= get_prev_frame_always (this_frame
);
1143 error (_("Cannot pop the initial frame."));
1145 /* Ignore TAILCALL_FRAME type frames, they were executed already before
1146 entering THISFRAME. */
1147 prev_frame
= skip_tailcall_frames (prev_frame
);
1149 if (prev_frame
== NULL
)
1150 error (_("Cannot find the caller frame."));
1152 /* Make a copy of all the register values unwound from this frame.
1153 Save them in a scratch buffer so that there isn't a race between
1154 trying to extract the old values from the current regcache while
1155 at the same time writing new values into that same cache. */
1156 std::unique_ptr
<readonly_detached_regcache
> scratch
1157 = frame_save_as_regcache (prev_frame
);
1159 /* FIXME: cagney/2003-03-16: It should be possible to tell the
1160 target's register cache that it is about to be hit with a burst
1161 register transfer and that the sequence of register writes should
1162 be batched. The pair target_prepare_to_store() and
1163 target_store_registers() kind of suggest this functionality.
1164 Unfortunately, they don't implement it. Their lack of a formal
1165 definition can lead to targets writing back bogus values
1166 (arguably a bug in the target code mind). */
1167 /* Now copy those saved registers into the current regcache. */
1168 get_current_regcache ()->restore (scratch
.get ());
1170 /* We've made right mess of GDB's local state, just discard
1172 reinit_frame_cache ();
1176 frame_register_unwind (frame_info
*next_frame
, int regnum
,
1177 int *optimizedp
, int *unavailablep
,
1178 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
1179 int *realnump
, gdb_byte
*bufferp
)
1181 struct value
*value
;
1183 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1184 that the value proper does not need to be fetched. */
1185 gdb_assert (optimizedp
!= NULL
);
1186 gdb_assert (lvalp
!= NULL
);
1187 gdb_assert (addrp
!= NULL
);
1188 gdb_assert (realnump
!= NULL
);
1189 /* gdb_assert (bufferp != NULL); */
1191 value
= frame_unwind_register_value (next_frame
, regnum
);
1193 gdb_assert (value
!= NULL
);
1195 *optimizedp
= value_optimized_out (value
);
1196 *unavailablep
= !value_entirely_available (value
);
1197 *lvalp
= VALUE_LVAL (value
);
1198 *addrp
= value_address (value
);
1199 if (*lvalp
== lval_register
)
1200 *realnump
= VALUE_REGNUM (value
);
1206 if (!*optimizedp
&& !*unavailablep
)
1207 memcpy (bufferp
, value_contents_all (value
),
1208 TYPE_LENGTH (value_type (value
)));
1210 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
1213 /* Dispose of the new value. This prevents watchpoints from
1214 trying to watch the saved frame pointer. */
1215 release_value (value
);
1219 frame_register (struct frame_info
*frame
, int regnum
,
1220 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
1221 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
1223 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
1224 that the value proper does not need to be fetched. */
1225 gdb_assert (optimizedp
!= NULL
);
1226 gdb_assert (lvalp
!= NULL
);
1227 gdb_assert (addrp
!= NULL
);
1228 gdb_assert (realnump
!= NULL
);
1229 /* gdb_assert (bufferp != NULL); */
1231 /* Obtain the register value by unwinding the register from the next
1232 (more inner frame). */
1233 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
1234 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
1235 lvalp
, addrp
, realnump
, bufferp
);
1239 frame_unwind_register (frame_info
*next_frame
, int regnum
, gdb_byte
*buf
)
1245 enum lval_type lval
;
1247 frame_register_unwind (next_frame
, regnum
, &optimized
, &unavailable
,
1248 &lval
, &addr
, &realnum
, buf
);
1251 throw_error (OPTIMIZED_OUT_ERROR
,
1252 _("Register %d was not saved"), regnum
);
1254 throw_error (NOT_AVAILABLE_ERROR
,
1255 _("Register %d is not available"), regnum
);
1259 get_frame_register (struct frame_info
*frame
,
1260 int regnum
, gdb_byte
*buf
)
1262 frame_unwind_register (frame
->next
, regnum
, buf
);
1266 frame_unwind_register_value (frame_info
*next_frame
, int regnum
)
1268 struct gdbarch
*gdbarch
;
1269 struct value
*value
;
1271 gdb_assert (next_frame
!= NULL
);
1272 gdbarch
= frame_unwind_arch (next_frame
);
1276 fprintf_unfiltered (gdb_stdlog
,
1277 "{ frame_unwind_register_value "
1278 "(frame=%d,regnum=%d(%s),...) ",
1279 next_frame
->level
, regnum
,
1280 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1283 /* Find the unwinder. */
1284 if (next_frame
->unwind
== NULL
)
1285 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
1287 /* Ask this frame to unwind its register. */
1288 value
= next_frame
->unwind
->prev_register (next_frame
,
1289 &next_frame
->prologue_cache
,
1294 fprintf_unfiltered (gdb_stdlog
, "->");
1295 if (value_optimized_out (value
))
1297 fprintf_unfiltered (gdb_stdlog
, " ");
1298 val_print_optimized_out (value
, gdb_stdlog
);
1302 if (VALUE_LVAL (value
) == lval_register
)
1303 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1304 VALUE_REGNUM (value
));
1305 else if (VALUE_LVAL (value
) == lval_memory
)
1306 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1308 value_address (value
)));
1310 fprintf_unfiltered (gdb_stdlog
, " computed");
1312 if (value_lazy (value
))
1313 fprintf_unfiltered (gdb_stdlog
, " lazy");
1317 const gdb_byte
*buf
= value_contents (value
);
1319 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1320 fprintf_unfiltered (gdb_stdlog
, "[");
1321 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1322 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1323 fprintf_unfiltered (gdb_stdlog
, "]");
1327 fprintf_unfiltered (gdb_stdlog
, " }\n");
1334 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1336 return frame_unwind_register_value (frame
->next
, regnum
);
1340 frame_unwind_register_signed (frame_info
*next_frame
, int regnum
)
1342 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1343 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1344 int size
= register_size (gdbarch
, regnum
);
1345 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1347 gdb_assert (value
!= NULL
);
1349 if (value_optimized_out (value
))
1351 throw_error (OPTIMIZED_OUT_ERROR
,
1352 _("Register %d was not saved"), regnum
);
1354 if (!value_entirely_available (value
))
1356 throw_error (NOT_AVAILABLE_ERROR
,
1357 _("Register %d is not available"), regnum
);
1360 LONGEST r
= extract_signed_integer (value_contents_all (value
), size
,
1363 release_value (value
);
1368 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1370 return frame_unwind_register_signed (frame
->next
, regnum
);
1374 frame_unwind_register_unsigned (frame_info
*next_frame
, int regnum
)
1376 struct gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
1377 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1378 int size
= register_size (gdbarch
, regnum
);
1379 struct value
*value
= frame_unwind_register_value (next_frame
, regnum
);
1381 gdb_assert (value
!= NULL
);
1383 if (value_optimized_out (value
))
1385 throw_error (OPTIMIZED_OUT_ERROR
,
1386 _("Register %d was not saved"), regnum
);
1388 if (!value_entirely_available (value
))
1390 throw_error (NOT_AVAILABLE_ERROR
,
1391 _("Register %d is not available"), regnum
);
1394 ULONGEST r
= extract_unsigned_integer (value_contents_all (value
), size
,
1397 release_value (value
);
1402 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1404 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1408 read_frame_register_unsigned (frame_info
*frame
, int regnum
,
1411 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1413 if (!value_optimized_out (regval
)
1414 && value_entirely_available (regval
))
1416 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1417 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1418 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1420 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1428 put_frame_register (struct frame_info
*frame
, int regnum
,
1429 const gdb_byte
*buf
)
1431 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1435 enum lval_type lval
;
1438 frame_register (frame
, regnum
, &optim
, &unavail
,
1439 &lval
, &addr
, &realnum
, NULL
);
1441 error (_("Attempt to assign to a register that was not saved."));
1446 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1450 get_current_regcache ()->cooked_write (realnum
, buf
);
1453 error (_("Attempt to assign to an unmodifiable value."));
1457 /* This function is deprecated. Use get_frame_register_value instead,
1458 which provides more accurate information.
1460 Find and return the value of REGNUM for the specified stack frame.
1461 The number of bytes copied is REGISTER_SIZE (REGNUM).
1463 Returns 0 if the register value could not be found. */
1466 deprecated_frame_register_read (frame_info
*frame
, int regnum
,
1471 enum lval_type lval
;
1475 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1476 &lval
, &addr
, &realnum
, myaddr
);
1478 return !optimized
&& !unavailable
;
1482 get_frame_register_bytes (frame_info
*frame
, int regnum
,
1483 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1484 int *optimizedp
, int *unavailablep
)
1486 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1491 /* Skip registers wholly inside of OFFSET. */
1492 while (offset
>= register_size (gdbarch
, regnum
))
1494 offset
-= register_size (gdbarch
, regnum
);
1498 /* Ensure that we will not read beyond the end of the register file.
1499 This can only ever happen if the debug information is bad. */
1501 numregs
= gdbarch_num_cooked_regs (gdbarch
);
1502 for (i
= regnum
; i
< numregs
; i
++)
1504 int thissize
= register_size (gdbarch
, i
);
1507 break; /* This register is not available on this architecture. */
1508 maxsize
+= thissize
;
1511 error (_("Bad debug information detected: "
1512 "Attempt to read %d bytes from registers."), len
);
1514 /* Copy the data. */
1517 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1522 if (curr_len
== register_size (gdbarch
, regnum
))
1524 enum lval_type lval
;
1528 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1529 &lval
, &addr
, &realnum
, myaddr
);
1530 if (*optimizedp
|| *unavailablep
)
1535 struct value
*value
= frame_unwind_register_value (frame
->next
,
1537 gdb_assert (value
!= NULL
);
1538 *optimizedp
= value_optimized_out (value
);
1539 *unavailablep
= !value_entirely_available (value
);
1541 if (*optimizedp
|| *unavailablep
)
1543 release_value (value
);
1547 memcpy (myaddr
, value_contents_all (value
) + offset
, curr_len
);
1548 release_value (value
);
1564 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1565 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1567 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1569 /* Skip registers wholly inside of OFFSET. */
1570 while (offset
>= register_size (gdbarch
, regnum
))
1572 offset
-= register_size (gdbarch
, regnum
);
1576 /* Copy the data. */
1579 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1584 if (curr_len
== register_size (gdbarch
, regnum
))
1586 put_frame_register (frame
, regnum
, myaddr
);
1590 struct value
*value
= frame_unwind_register_value (frame
->next
,
1592 gdb_assert (value
!= NULL
);
1594 memcpy ((char *) value_contents_writeable (value
) + offset
, myaddr
,
1596 put_frame_register (frame
, regnum
, value_contents_raw (value
));
1597 release_value (value
);
1607 /* Create a sentinel frame. */
1609 static struct frame_info
*
1610 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1612 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1615 frame
->pspace
= pspace
;
1616 frame
->aspace
= regcache
->aspace ();
1617 /* Explicitly initialize the sentinel frame's cache. Provide it
1618 with the underlying regcache. In the future additional
1619 information, such as the frame's thread will be added. */
1620 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1621 /* For the moment there is only one sentinel frame implementation. */
1622 frame
->unwind
= &sentinel_frame_unwind
;
1623 /* Link this frame back to itself. The frame is self referential
1624 (the unwound PC is the same as the pc), so make it so. */
1625 frame
->next
= frame
;
1626 /* The sentinel frame has a special ID. */
1627 frame
->this_id
.p
= frame_id_status::COMPUTED
;
1628 frame
->this_id
.value
= sentinel_frame_id
;
1631 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1632 fprint_frame (gdb_stdlog
, frame
);
1633 fprintf_unfiltered (gdb_stdlog
, " }\n");
1638 /* Cache for frame addresses already read by gdb. Valid only while
1639 inferior is stopped. Control variables for the frame cache should
1640 be local to this module. */
1642 static struct obstack frame_cache_obstack
;
1645 frame_obstack_zalloc (unsigned long size
)
1647 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1649 memset (data
, 0, size
);
1653 static struct frame_info
*get_prev_frame_always_1 (struct frame_info
*this_frame
);
1656 get_current_frame (void)
1658 struct frame_info
*current_frame
;
1660 /* First check, and report, the lack of registers. Having GDB
1661 report "No stack!" or "No memory" when the target doesn't even
1662 have registers is very confusing. Besides, "printcmd.exp"
1663 explicitly checks that ``print $pc'' with no registers prints "No
1665 if (!target_has_registers
)
1666 error (_("No registers."));
1667 if (!target_has_stack
)
1668 error (_("No stack."));
1669 if (!target_has_memory
)
1670 error (_("No memory."));
1671 /* Traceframes are effectively a substitute for the live inferior. */
1672 if (get_traceframe_number () < 0)
1673 validate_registers_access ();
1675 if (sentinel_frame
== NULL
)
1677 create_sentinel_frame (current_program_space
, get_current_regcache ());
1679 /* Set the current frame before computing the frame id, to avoid
1680 recursion inside compute_frame_id, in case the frame's
1681 unwinder decides to do a symbol lookup (which depends on the
1682 selected frame's block).
1684 This call must always succeed. In particular, nothing inside
1685 get_prev_frame_always_1 should try to unwind from the
1686 sentinel frame, because that could fail/throw, and we always
1687 want to leave with the current frame created and linked in --
1688 we should never end up with the sentinel frame as outermost
1690 current_frame
= get_prev_frame_always_1 (sentinel_frame
);
1691 gdb_assert (current_frame
!= NULL
);
1693 return current_frame
;
1696 /* The "selected" stack frame is used by default for local and arg
1697 access. May be zero, for no selected frame. */
1699 static struct frame_info
*selected_frame
;
1704 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1707 /* Traceframes are effectively a substitute for the live inferior. */
1708 if (get_traceframe_number () < 0)
1710 /* No current inferior, no frame. */
1711 if (inferior_ptid
== null_ptid
)
1714 thread_info
*tp
= inferior_thread ();
1715 /* Don't try to read from a dead thread. */
1716 if (tp
->state
== THREAD_EXITED
)
1719 /* ... or from a spinning thread. */
1727 /* Return the selected frame. Always non-NULL (unless there isn't an
1728 inferior sufficient for creating a frame) in which case an error is
1732 get_selected_frame (const char *message
)
1734 if (selected_frame
== NULL
)
1736 if (message
!= NULL
&& !has_stack_frames ())
1737 error (("%s"), message
);
1738 /* Hey! Don't trust this. It should really be re-finding the
1739 last selected frame of the currently selected thread. This,
1740 though, is better than nothing. */
1741 select_frame (get_current_frame ());
1743 /* There is always a frame. */
1744 gdb_assert (selected_frame
!= NULL
);
1745 return selected_frame
;
1748 /* If there is a selected frame, return it. Otherwise, return NULL. */
1751 get_selected_frame_if_set (void)
1753 return selected_frame
;
1756 /* This is a variant of get_selected_frame() which can be called when
1757 the inferior does not have a frame; in that case it will return
1758 NULL instead of calling error(). */
1761 deprecated_safe_get_selected_frame (void)
1763 if (!has_stack_frames ())
1765 return get_selected_frame (NULL
);
1768 /* Select frame FI (or NULL - to invalidate the current frame). */
1771 select_frame (struct frame_info
*fi
)
1773 selected_frame
= fi
;
1774 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1775 frame is being invalidated. */
1777 /* FIXME: kseitz/2002-08-28: It would be nice to call
1778 selected_frame_level_changed_event() right here, but due to limitations
1779 in the current interfaces, we would end up flooding UIs with events
1780 because select_frame() is used extensively internally.
1782 Once we have frame-parameterized frame (and frame-related) commands,
1783 the event notification can be moved here, since this function will only
1784 be called when the user's selected frame is being changed. */
1786 /* Ensure that symbols for this frame are read in. Also, determine the
1787 source language of this frame, and switch to it if desired. */
1792 /* We retrieve the frame's symtab by using the frame PC.
1793 However we cannot use the frame PC as-is, because it usually
1794 points to the instruction following the "call", which is
1795 sometimes the first instruction of another function. So we
1796 rely on get_frame_address_in_block() which provides us with a
1797 PC which is guaranteed to be inside the frame's code
1799 if (get_frame_address_in_block_if_available (fi
, &pc
))
1801 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
1804 && compunit_language (cust
) != current_language
->la_language
1805 && compunit_language (cust
) != language_unknown
1806 && language_mode
== language_mode_auto
)
1807 set_language (compunit_language (cust
));
1812 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1813 Always returns a non-NULL value. */
1816 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1818 struct frame_info
*fi
;
1822 fprintf_unfiltered (gdb_stdlog
,
1823 "{ create_new_frame (addr=%s, pc=%s) ",
1824 hex_string (addr
), hex_string (pc
));
1827 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1829 fi
->next
= create_sentinel_frame (current_program_space
,
1830 get_current_regcache ());
1832 /* Set/update this frame's cached PC value, found in the next frame.
1833 Do this before looking for this frame's unwinder. A sniffer is
1834 very likely to read this, and the corresponding unwinder is
1835 entitled to rely that the PC doesn't magically change. */
1836 fi
->next
->prev_pc
.value
= pc
;
1837 fi
->next
->prev_pc
.status
= CC_VALUE
;
1839 /* We currently assume that frame chain's can't cross spaces. */
1840 fi
->pspace
= fi
->next
->pspace
;
1841 fi
->aspace
= fi
->next
->aspace
;
1843 /* Select/initialize both the unwind function and the frame's type
1845 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1847 fi
->this_id
.p
= frame_id_status::COMPUTED
;
1848 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1852 fprintf_unfiltered (gdb_stdlog
, "-> ");
1853 fprint_frame (gdb_stdlog
, fi
);
1854 fprintf_unfiltered (gdb_stdlog
, " }\n");
1860 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1861 innermost frame). Be careful to not fall off the bottom of the
1862 frame chain and onto the sentinel frame. */
1865 get_next_frame (struct frame_info
*this_frame
)
1867 if (this_frame
->level
> 0)
1868 return this_frame
->next
;
1873 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
1874 innermost (i.e. current) frame, return the sentinel frame. Thus,
1875 unlike get_next_frame(), NULL will never be returned. */
1878 get_next_frame_sentinel_okay (struct frame_info
*this_frame
)
1880 gdb_assert (this_frame
!= NULL
);
1882 /* Note that, due to the manner in which the sentinel frame is
1883 constructed, this_frame->next still works even when this_frame
1884 is the sentinel frame. But we disallow it here anyway because
1885 calling get_next_frame_sentinel_okay() on the sentinel frame
1886 is likely a coding error. */
1887 gdb_assert (this_frame
!= sentinel_frame
);
1889 return this_frame
->next
;
1892 /* Observer for the target_changed event. */
1895 frame_observer_target_changed (struct target_ops
*target
)
1897 reinit_frame_cache ();
1900 /* Flush the entire frame cache. */
1903 reinit_frame_cache (void)
1905 struct frame_info
*fi
;
1907 ++frame_cache_generation
;
1909 /* Tear down all frame caches. */
1910 for (fi
= sentinel_frame
; fi
!= NULL
; fi
= fi
->prev
)
1912 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1913 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1914 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1915 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1918 /* Since we can't really be sure what the first object allocated was. */
1919 obstack_free (&frame_cache_obstack
, 0);
1920 obstack_init (&frame_cache_obstack
);
1922 if (sentinel_frame
!= NULL
)
1923 annotate_frames_invalid ();
1925 sentinel_frame
= NULL
; /* Invalidate cache */
1926 select_frame (NULL
);
1927 frame_stash_invalidate ();
1929 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1932 /* Find where a register is saved (in memory or another register).
1933 The result of frame_register_unwind is just where it is saved
1934 relative to this particular frame. */
1937 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1938 int *optimizedp
, enum lval_type
*lvalp
,
1939 CORE_ADDR
*addrp
, int *realnump
)
1941 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1943 while (this_frame
!= NULL
)
1947 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1948 lvalp
, addrp
, realnump
, NULL
);
1953 if (*lvalp
!= lval_register
)
1957 this_frame
= get_next_frame (this_frame
);
1961 /* Get the previous raw frame, and check that it is not identical to
1962 same other frame frame already in the chain. If it is, there is
1963 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1964 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1965 validity tests, that compare THIS_FRAME and the next frame, we do
1966 this right after creating the previous frame, to avoid ever ending
1967 up with two frames with the same id in the frame chain. */
1969 static struct frame_info
*
1970 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1972 struct frame_info
*prev_frame
;
1974 prev_frame
= get_prev_frame_raw (this_frame
);
1976 /* Don't compute the frame id of the current frame yet. Unwinding
1977 the sentinel frame can fail (e.g., if the thread is gone and we
1978 can't thus read its registers). If we let the cycle detection
1979 code below try to compute a frame ID, then an error thrown from
1980 within the frame ID computation would result in the sentinel
1981 frame as outermost frame, which is bogus. Instead, we'll compute
1982 the current frame's ID lazily in get_frame_id. Note that there's
1983 no point in doing cycle detection when there's only one frame, so
1984 nothing is lost here. */
1985 if (prev_frame
->level
== 0)
1988 unsigned int entry_generation
= get_frame_cache_generation ();
1992 compute_frame_id (prev_frame
);
1993 if (!frame_stash_add (prev_frame
))
1995 /* Another frame with the same id was already in the stash. We just
1996 detected a cycle. */
1999 fprintf_unfiltered (gdb_stdlog
, "-> ");
2000 fprint_frame (gdb_stdlog
, NULL
);
2001 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
2003 this_frame
->stop_reason
= UNWIND_SAME_ID
;
2005 prev_frame
->next
= NULL
;
2006 this_frame
->prev
= NULL
;
2010 catch (const gdb_exception
&ex
)
2012 if (get_frame_cache_generation () == entry_generation
)
2014 prev_frame
->next
= NULL
;
2015 this_frame
->prev
= NULL
;
2024 /* Helper function for get_prev_frame_always, this is called inside a
2025 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
2026 there is no such frame. This may throw an exception. */
2028 static struct frame_info
*
2029 get_prev_frame_always_1 (struct frame_info
*this_frame
)
2031 struct gdbarch
*gdbarch
;
2033 gdb_assert (this_frame
!= NULL
);
2034 gdbarch
= get_frame_arch (this_frame
);
2038 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_always (this_frame=");
2039 if (this_frame
!= NULL
)
2040 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2042 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2043 fprintf_unfiltered (gdb_stdlog
, ") ");
2046 /* Only try to do the unwind once. */
2047 if (this_frame
->prev_p
)
2051 fprintf_unfiltered (gdb_stdlog
, "-> ");
2052 fprint_frame (gdb_stdlog
, this_frame
->prev
);
2053 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
2055 return this_frame
->prev
;
2058 /* If the frame unwinder hasn't been selected yet, we must do so
2059 before setting prev_p; otherwise the check for misbehaved
2060 sniffers will think that this frame's sniffer tried to unwind
2061 further (see frame_cleanup_after_sniffer). */
2062 if (this_frame
->unwind
== NULL
)
2063 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
2065 this_frame
->prev_p
= true;
2066 this_frame
->stop_reason
= UNWIND_NO_REASON
;
2068 /* If we are unwinding from an inline frame, all of the below tests
2069 were already performed when we unwound from the next non-inline
2070 frame. We must skip them, since we can not get THIS_FRAME's ID
2071 until we have unwound all the way down to the previous non-inline
2073 if (get_frame_type (this_frame
) == INLINE_FRAME
)
2074 return get_prev_frame_if_no_cycle (this_frame
);
2076 /* Check that this frame is unwindable. If it isn't, don't try to
2077 unwind to the prev frame. */
2078 this_frame
->stop_reason
2079 = this_frame
->unwind
->stop_reason (this_frame
,
2080 &this_frame
->prologue_cache
);
2082 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
2086 enum unwind_stop_reason reason
= this_frame
->stop_reason
;
2088 fprintf_unfiltered (gdb_stdlog
, "-> ");
2089 fprint_frame (gdb_stdlog
, NULL
);
2090 fprintf_unfiltered (gdb_stdlog
, " // %s }\n",
2091 frame_stop_reason_symbol_string (reason
));
2096 /* Check that this frame's ID isn't inner to (younger, below, next)
2097 the next frame. This happens when a frame unwind goes backwards.
2098 This check is valid only if this frame and the next frame are NORMAL.
2099 See the comment at frame_id_inner for details. */
2100 if (get_frame_type (this_frame
) == NORMAL_FRAME
2101 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
2102 && frame_id_inner (get_frame_arch (this_frame
->next
),
2103 get_frame_id (this_frame
),
2104 get_frame_id (this_frame
->next
)))
2106 CORE_ADDR this_pc_in_block
;
2107 struct minimal_symbol
*morestack_msym
;
2108 const char *morestack_name
= NULL
;
2110 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
2111 this_pc_in_block
= get_frame_address_in_block (this_frame
);
2112 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
2114 morestack_name
= morestack_msym
->linkage_name ();
2115 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
2119 fprintf_unfiltered (gdb_stdlog
, "-> ");
2120 fprint_frame (gdb_stdlog
, NULL
);
2121 fprintf_unfiltered (gdb_stdlog
,
2122 " // this frame ID is inner }\n");
2124 this_frame
->stop_reason
= UNWIND_INNER_ID
;
2129 /* Check that this and the next frame do not unwind the PC register
2130 to the same memory location. If they do, then even though they
2131 have different frame IDs, the new frame will be bogus; two
2132 functions can't share a register save slot for the PC. This can
2133 happen when the prologue analyzer finds a stack adjustment, but
2136 This check does assume that the "PC register" is roughly a
2137 traditional PC, even if the gdbarch_unwind_pc method adjusts
2138 it (we do not rely on the value, only on the unwound PC being
2139 dependent on this value). A potential improvement would be
2140 to have the frame prev_pc method and the gdbarch unwind_pc
2141 method set the same lval and location information as
2142 frame_register_unwind. */
2143 if (this_frame
->level
> 0
2144 && gdbarch_pc_regnum (gdbarch
) >= 0
2145 && get_frame_type (this_frame
) == NORMAL_FRAME
2146 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
2147 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
2149 int optimized
, realnum
, nrealnum
;
2150 enum lval_type lval
, nlval
;
2151 CORE_ADDR addr
, naddr
;
2153 frame_register_unwind_location (this_frame
,
2154 gdbarch_pc_regnum (gdbarch
),
2155 &optimized
, &lval
, &addr
, &realnum
);
2156 frame_register_unwind_location (get_next_frame (this_frame
),
2157 gdbarch_pc_regnum (gdbarch
),
2158 &optimized
, &nlval
, &naddr
, &nrealnum
);
2160 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
2161 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
2165 fprintf_unfiltered (gdb_stdlog
, "-> ");
2166 fprint_frame (gdb_stdlog
, NULL
);
2167 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
2170 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
2171 this_frame
->prev
= NULL
;
2176 return get_prev_frame_if_no_cycle (this_frame
);
2179 /* Return a "struct frame_info" corresponding to the frame that called
2180 THIS_FRAME. Returns NULL if there is no such frame.
2182 Unlike get_prev_frame, this function always tries to unwind the
2186 get_prev_frame_always (struct frame_info
*this_frame
)
2188 struct frame_info
*prev_frame
= NULL
;
2192 prev_frame
= get_prev_frame_always_1 (this_frame
);
2194 catch (const gdb_exception_error
&ex
)
2196 if (ex
.error
== MEMORY_ERROR
)
2198 this_frame
->stop_reason
= UNWIND_MEMORY_ERROR
;
2199 if (ex
.message
!= NULL
)
2204 /* The error needs to live as long as the frame does.
2205 Allocate using stack local STOP_STRING then assign the
2206 pointer to the frame, this allows the STOP_STRING on the
2207 frame to be of type 'const char *'. */
2208 size
= ex
.message
->size () + 1;
2209 stop_string
= (char *) frame_obstack_zalloc (size
);
2210 memcpy (stop_string
, ex
.what (), size
);
2211 this_frame
->stop_string
= stop_string
;
2222 /* Construct a new "struct frame_info" and link it previous to
2225 static struct frame_info
*
2226 get_prev_frame_raw (struct frame_info
*this_frame
)
2228 struct frame_info
*prev_frame
;
2230 /* Allocate the new frame but do not wire it in to the frame chain.
2231 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
2232 frame->next to pull some fancy tricks (of course such code is, by
2233 definition, recursive). Try to prevent it.
2235 There is no reason to worry about memory leaks, should the
2236 remainder of the function fail. The allocated memory will be
2237 quickly reclaimed when the frame cache is flushed, and the `we've
2238 been here before' check above will stop repeated memory
2239 allocation calls. */
2240 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
2241 prev_frame
->level
= this_frame
->level
+ 1;
2243 /* For now, assume we don't have frame chains crossing address
2245 prev_frame
->pspace
= this_frame
->pspace
;
2246 prev_frame
->aspace
= this_frame
->aspace
;
2248 /* Don't yet compute ->unwind (and hence ->type). It is computed
2249 on-demand in get_frame_type, frame_register_unwind, and
2252 /* Don't yet compute the frame's ID. It is computed on-demand by
2255 /* The unwound frame ID is validate at the start of this function,
2256 as part of the logic to decide if that frame should be further
2257 unwound, and not here while the prev frame is being created.
2258 Doing this makes it possible for the user to examine a frame that
2259 has an invalid frame ID.
2261 Some very old VAX code noted: [...] For the sake of argument,
2262 suppose that the stack is somewhat trashed (which is one reason
2263 that "info frame" exists). So, return 0 (indicating we don't
2264 know the address of the arglist) if we don't know what frame this
2268 this_frame
->prev
= prev_frame
;
2269 prev_frame
->next
= this_frame
;
2273 fprintf_unfiltered (gdb_stdlog
, "-> ");
2274 fprint_frame (gdb_stdlog
, prev_frame
);
2275 fprintf_unfiltered (gdb_stdlog
, " }\n");
2281 /* Debug routine to print a NULL frame being returned. */
2284 frame_debug_got_null_frame (struct frame_info
*this_frame
,
2289 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
2290 if (this_frame
!= NULL
)
2291 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
2293 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
2294 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
2298 /* Is this (non-sentinel) frame in the "main"() function? */
2301 inside_main_func (frame_info
*this_frame
)
2303 if (symfile_objfile
== nullptr)
2306 bound_minimal_symbol msymbol
2307 = lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
2308 if (msymbol
.minsym
== nullptr)
2311 /* Make certain that the code, and not descriptor, address is
2314 = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
2315 BMSYMBOL_VALUE_ADDRESS (msymbol
),
2316 current_top_target ());
2318 return maddr
== get_frame_func (this_frame
);
2321 /* Test whether THIS_FRAME is inside the process entry point function. */
2324 inside_entry_func (frame_info
*this_frame
)
2326 CORE_ADDR entry_point
;
2328 if (!entry_point_address_query (&entry_point
))
2331 return get_frame_func (this_frame
) == entry_point
;
2334 /* Return a structure containing various interesting information about
2335 the frame that called THIS_FRAME. Returns NULL if there is entier
2336 no such frame or the frame fails any of a set of target-independent
2337 condition that should terminate the frame chain (e.g., as unwinding
2340 This function should not contain target-dependent tests, such as
2341 checking whether the program-counter is zero. */
2344 get_prev_frame (struct frame_info
*this_frame
)
2349 /* There is always a frame. If this assertion fails, suspect that
2350 something should be calling get_selected_frame() or
2351 get_current_frame(). */
2352 gdb_assert (this_frame
!= NULL
);
2354 /* If this_frame is the current frame, then compute and stash
2355 its frame id prior to fetching and computing the frame id of the
2356 previous frame. Otherwise, the cycle detection code in
2357 get_prev_frame_if_no_cycle() will not work correctly. When
2358 get_frame_id() is called later on, an assertion error will
2359 be triggered in the event of a cycle between the current
2360 frame and its previous frame. */
2361 if (this_frame
->level
== 0)
2362 get_frame_id (this_frame
);
2364 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2366 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2367 sense to stop unwinding at a dummy frame. One place where a dummy
2368 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2369 pcsqh register (space register for the instruction at the head of the
2370 instruction queue) cannot be written directly; the only way to set it
2371 is to branch to code that is in the target space. In order to implement
2372 frame dummies on HPUX, the called function is made to jump back to where
2373 the inferior was when the user function was called. If gdb was inside
2374 the main function when we created the dummy frame, the dummy frame will
2375 point inside the main function. */
2376 if (this_frame
->level
>= 0
2377 && get_frame_type (this_frame
) == NORMAL_FRAME
2378 && !user_set_backtrace_options
.backtrace_past_main
2380 && inside_main_func (this_frame
))
2381 /* Don't unwind past main(). Note, this is done _before_ the
2382 frame has been marked as previously unwound. That way if the
2383 user later decides to enable unwinds past main(), that will
2384 automatically happen. */
2386 frame_debug_got_null_frame (this_frame
, "inside main func");
2390 /* If the user's backtrace limit has been exceeded, stop. We must
2391 add two to the current level; one of those accounts for backtrace_limit
2392 being 1-based and the level being 0-based, and the other accounts for
2393 the level of the new frame instead of the level of the current
2395 if (this_frame
->level
+ 2 > user_set_backtrace_options
.backtrace_limit
)
2397 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2401 /* If we're already inside the entry function for the main objfile,
2402 then it isn't valid. Don't apply this test to a dummy frame -
2403 dummy frame PCs typically land in the entry func. Don't apply
2404 this test to the sentinel frame. Sentinel frames should always
2405 be allowed to unwind. */
2406 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2407 wasn't checking for "main" in the minimal symbols. With that
2408 fixed asm-source tests now stop in "main" instead of halting the
2409 backtrace in weird and wonderful ways somewhere inside the entry
2410 file. Suspect that tests for inside the entry file/func were
2411 added to work around that (now fixed) case. */
2412 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2413 suggested having the inside_entry_func test use the
2414 inside_main_func() msymbol trick (along with entry_point_address()
2415 I guess) to determine the address range of the start function.
2416 That should provide a far better stopper than the current
2418 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2419 applied tail-call optimizations to main so that a function called
2420 from main returns directly to the caller of main. Since we don't
2421 stop at main, we should at least stop at the entry point of the
2423 if (this_frame
->level
>= 0
2424 && get_frame_type (this_frame
) == NORMAL_FRAME
2425 && !user_set_backtrace_options
.backtrace_past_entry
2427 && inside_entry_func (this_frame
))
2429 frame_debug_got_null_frame (this_frame
, "inside entry func");
2433 /* Assume that the only way to get a zero PC is through something
2434 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2435 will never unwind a zero PC. */
2436 if (this_frame
->level
> 0
2437 && (get_frame_type (this_frame
) == NORMAL_FRAME
2438 || get_frame_type (this_frame
) == INLINE_FRAME
)
2439 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2440 && frame_pc_p
&& frame_pc
== 0)
2442 frame_debug_got_null_frame (this_frame
, "zero PC");
2446 return get_prev_frame_always (this_frame
);
2450 get_prev_frame_id_by_id (struct frame_id id
)
2452 struct frame_id prev_id
;
2453 struct frame_info
*frame
;
2455 frame
= frame_find_by_id (id
);
2458 prev_id
= get_frame_id (get_prev_frame (frame
));
2460 prev_id
= null_frame_id
;
2466 get_frame_pc (struct frame_info
*frame
)
2468 gdb_assert (frame
->next
!= NULL
);
2469 return frame_unwind_pc (frame
->next
);
2473 get_frame_pc_if_available (frame_info
*frame
, CORE_ADDR
*pc
)
2476 gdb_assert (frame
->next
!= NULL
);
2480 *pc
= frame_unwind_pc (frame
->next
);
2482 catch (const gdb_exception_error
&ex
)
2484 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2493 /* Return an address that falls within THIS_FRAME's code block. */
2496 get_frame_address_in_block (struct frame_info
*this_frame
)
2498 /* A draft address. */
2499 CORE_ADDR pc
= get_frame_pc (this_frame
);
2501 struct frame_info
*next_frame
= this_frame
->next
;
2503 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2504 Normally the resume address is inside the body of the function
2505 associated with THIS_FRAME, but there is a special case: when
2506 calling a function which the compiler knows will never return
2507 (for instance abort), the call may be the very last instruction
2508 in the calling function. The resume address will point after the
2509 call and may be at the beginning of a different function
2512 If THIS_FRAME is a signal frame or dummy frame, then we should
2513 not adjust the unwound PC. For a dummy frame, GDB pushed the
2514 resume address manually onto the stack. For a signal frame, the
2515 OS may have pushed the resume address manually and invoked the
2516 handler (e.g. GNU/Linux), or invoked the trampoline which called
2517 the signal handler - but in either case the signal handler is
2518 expected to return to the trampoline. So in both of these
2519 cases we know that the resume address is executable and
2520 related. So we only need to adjust the PC if THIS_FRAME
2521 is a normal function.
2523 If the program has been interrupted while THIS_FRAME is current,
2524 then clearly the resume address is inside the associated
2525 function. There are three kinds of interruption: debugger stop
2526 (next frame will be SENTINEL_FRAME), operating system
2527 signal or exception (next frame will be SIGTRAMP_FRAME),
2528 or debugger-induced function call (next frame will be
2529 DUMMY_FRAME). So we only need to adjust the PC if
2530 NEXT_FRAME is a normal function.
2532 We check the type of NEXT_FRAME first, since it is already
2533 known; frame type is determined by the unwinder, and since
2534 we have THIS_FRAME we've already selected an unwinder for
2537 If the next frame is inlined, we need to keep going until we find
2538 the real function - for instance, if a signal handler is invoked
2539 while in an inlined function, then the code address of the
2540 "calling" normal function should not be adjusted either. */
2542 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2543 next_frame
= next_frame
->next
;
2545 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2546 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2547 && (get_frame_type (this_frame
) == NORMAL_FRAME
2548 || get_frame_type (this_frame
) == TAILCALL_FRAME
2549 || get_frame_type (this_frame
) == INLINE_FRAME
))
2556 get_frame_address_in_block_if_available (frame_info
*this_frame
,
2562 *pc
= get_frame_address_in_block (this_frame
);
2564 catch (const gdb_exception_error
&ex
)
2566 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2575 find_frame_sal (frame_info
*frame
)
2577 struct frame_info
*next_frame
;
2581 if (frame_inlined_callees (frame
) > 0)
2585 /* If the current frame has some inlined callees, and we have a next
2586 frame, then that frame must be an inlined frame. In this case
2587 this frame's sal is the "call site" of the next frame's inlined
2588 function, which can not be inferred from get_frame_pc. */
2589 next_frame
= get_next_frame (frame
);
2591 sym
= get_frame_function (next_frame
);
2593 sym
= inline_skipped_symbol (inferior_thread ());
2595 /* If frame is inline, it certainly has symbols. */
2598 symtab_and_line sal
;
2599 if (SYMBOL_LINE (sym
) != 0)
2601 sal
.symtab
= symbol_symtab (sym
);
2602 sal
.line
= SYMBOL_LINE (sym
);
2605 /* If the symbol does not have a location, we don't know where
2606 the call site is. Do not pretend to. This is jarring, but
2607 we can't do much better. */
2608 sal
.pc
= get_frame_pc (frame
);
2610 sal
.pspace
= get_frame_program_space (frame
);
2614 /* If FRAME is not the innermost frame, that normally means that
2615 FRAME->pc points at the return instruction (which is *after* the
2616 call instruction), and we want to get the line containing the
2617 call (because the call is where the user thinks the program is).
2618 However, if the next frame is either a SIGTRAMP_FRAME or a
2619 DUMMY_FRAME, then the next frame will contain a saved interrupt
2620 PC and such a PC indicates the current (rather than next)
2621 instruction/line, consequently, for such cases, want to get the
2622 line containing fi->pc. */
2623 if (!get_frame_pc_if_available (frame
, &pc
))
2626 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2627 return find_pc_line (pc
, notcurrent
);
2630 /* Per "frame.h", return the ``address'' of the frame. Code should
2631 really be using get_frame_id(). */
2633 get_frame_base (struct frame_info
*fi
)
2635 return get_frame_id (fi
).stack_addr
;
2638 /* High-level offsets into the frame. Used by the debug info. */
2641 get_frame_base_address (struct frame_info
*fi
)
2643 if (get_frame_type (fi
) != NORMAL_FRAME
)
2645 if (fi
->base
== NULL
)
2646 fi
->base
= frame_base_find_by_frame (fi
);
2647 /* Sneaky: If the low-level unwind and high-level base code share a
2648 common unwinder, let them share the prologue cache. */
2649 if (fi
->base
->unwind
== fi
->unwind
)
2650 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2651 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2655 get_frame_locals_address (struct frame_info
*fi
)
2657 if (get_frame_type (fi
) != NORMAL_FRAME
)
2659 /* If there isn't a frame address method, find it. */
2660 if (fi
->base
== NULL
)
2661 fi
->base
= frame_base_find_by_frame (fi
);
2662 /* Sneaky: If the low-level unwind and high-level base code share a
2663 common unwinder, let them share the prologue cache. */
2664 if (fi
->base
->unwind
== fi
->unwind
)
2665 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2666 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2670 get_frame_args_address (struct frame_info
*fi
)
2672 if (get_frame_type (fi
) != NORMAL_FRAME
)
2674 /* If there isn't a frame address method, find it. */
2675 if (fi
->base
== NULL
)
2676 fi
->base
= frame_base_find_by_frame (fi
);
2677 /* Sneaky: If the low-level unwind and high-level base code share a
2678 common unwinder, let them share the prologue cache. */
2679 if (fi
->base
->unwind
== fi
->unwind
)
2680 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2681 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2684 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2688 frame_unwinder_is (frame_info
*fi
, const frame_unwind
*unwinder
)
2690 if (fi
->unwind
== nullptr)
2691 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2693 return fi
->unwind
== unwinder
;
2696 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2697 or -1 for a NULL frame. */
2700 frame_relative_level (struct frame_info
*fi
)
2709 get_frame_type (struct frame_info
*frame
)
2711 if (frame
->unwind
== NULL
)
2712 /* Initialize the frame's unwinder because that's what
2713 provides the frame's type. */
2714 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2715 return frame
->unwind
->type
;
2718 struct program_space
*
2719 get_frame_program_space (struct frame_info
*frame
)
2721 return frame
->pspace
;
2724 struct program_space
*
2725 frame_unwind_program_space (struct frame_info
*this_frame
)
2727 gdb_assert (this_frame
);
2729 /* This is really a placeholder to keep the API consistent --- we
2730 assume for now that we don't have frame chains crossing
2732 return this_frame
->pspace
;
2735 const address_space
*
2736 get_frame_address_space (struct frame_info
*frame
)
2738 return frame
->aspace
;
2741 /* Memory access methods. */
2744 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2745 gdb_byte
*buf
, int len
)
2747 read_memory (addr
, buf
, len
);
2751 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2754 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2755 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2757 return read_memory_integer (addr
, len
, byte_order
);
2761 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2764 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2765 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2767 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2771 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2772 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2774 /* NOTE: target_read_memory returns zero on success! */
2775 return target_read_memory (addr
, buf
, len
) == 0;
2778 /* Architecture methods. */
2781 get_frame_arch (struct frame_info
*this_frame
)
2783 return frame_unwind_arch (this_frame
->next
);
2787 frame_unwind_arch (struct frame_info
*next_frame
)
2789 if (!next_frame
->prev_arch
.p
)
2791 struct gdbarch
*arch
;
2793 if (next_frame
->unwind
== NULL
)
2794 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2796 if (next_frame
->unwind
->prev_arch
!= NULL
)
2797 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2798 &next_frame
->prologue_cache
);
2800 arch
= get_frame_arch (next_frame
);
2802 next_frame
->prev_arch
.arch
= arch
;
2803 next_frame
->prev_arch
.p
= true;
2805 fprintf_unfiltered (gdb_stdlog
,
2806 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2808 gdbarch_bfd_arch_info (arch
)->printable_name
);
2811 return next_frame
->prev_arch
.arch
;
2815 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2817 next_frame
= skip_artificial_frames (next_frame
);
2819 /* We must have a non-artificial frame. The caller is supposed to check
2820 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
2822 gdb_assert (next_frame
!= NULL
);
2824 return frame_unwind_arch (next_frame
);
2827 /* Gets the language of FRAME. */
2830 get_frame_language (struct frame_info
*frame
)
2835 gdb_assert (frame
!= NULL
);
2837 /* We determine the current frame language by looking up its
2838 associated symtab. To retrieve this symtab, we use the frame
2839 PC. However we cannot use the frame PC as is, because it
2840 usually points to the instruction following the "call", which
2841 is sometimes the first instruction of another function. So
2842 we rely on get_frame_address_in_block(), it provides us with
2843 a PC that is guaranteed to be inside the frame's code
2848 pc
= get_frame_address_in_block (frame
);
2851 catch (const gdb_exception_error
&ex
)
2853 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
2859 struct compunit_symtab
*cust
= find_pc_compunit_symtab (pc
);
2862 return compunit_language (cust
);
2865 return language_unknown
;
2868 /* Stack pointer methods. */
2871 get_frame_sp (struct frame_info
*this_frame
)
2873 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2875 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2876 operate on THIS_FRAME now. */
2877 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2880 /* Return the reason why we can't unwind past FRAME. */
2882 enum unwind_stop_reason
2883 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2885 /* Fill-in STOP_REASON. */
2886 get_prev_frame_always (frame
);
2887 gdb_assert (frame
->prev_p
);
2889 return frame
->stop_reason
;
2892 /* Return a string explaining REASON. */
2895 unwind_stop_reason_to_string (enum unwind_stop_reason reason
)
2899 #define SET(name, description) \
2900 case name: return _(description);
2901 #include "unwind_stop_reasons.def"
2905 internal_error (__FILE__
, __LINE__
,
2906 "Invalid frame stop reason");
2911 frame_stop_reason_string (struct frame_info
*fi
)
2913 gdb_assert (fi
->prev_p
);
2914 gdb_assert (fi
->prev
== NULL
);
2916 /* Return the specific string if we have one. */
2917 if (fi
->stop_string
!= NULL
)
2918 return fi
->stop_string
;
2920 /* Return the generic string if we have nothing better. */
2921 return unwind_stop_reason_to_string (fi
->stop_reason
);
2924 /* Return the enum symbol name of REASON as a string, to use in debug
2928 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
2932 #define SET(name, description) \
2933 case name: return #name;
2934 #include "unwind_stop_reasons.def"
2938 internal_error (__FILE__
, __LINE__
,
2939 "Invalid frame stop reason");
2943 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2947 frame_cleanup_after_sniffer (struct frame_info
*frame
)
2949 /* The sniffer should not allocate a prologue cache if it did not
2950 match this frame. */
2951 gdb_assert (frame
->prologue_cache
== NULL
);
2953 /* No sniffer should extend the frame chain; sniff based on what is
2955 gdb_assert (!frame
->prev_p
);
2957 /* The sniffer should not check the frame's ID; that's circular. */
2958 gdb_assert (frame
->this_id
.p
!= frame_id_status::COMPUTED
);
2960 /* Clear cached fields dependent on the unwinder.
2962 The previous PC is independent of the unwinder, but the previous
2963 function is not (see get_frame_address_in_block). */
2964 frame
->prev_func
.status
= CC_UNKNOWN
;
2965 frame
->prev_func
.addr
= 0;
2967 /* Discard the unwinder last, so that we can easily find it if an assertion
2968 in this function triggers. */
2969 frame
->unwind
= NULL
;
2972 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2973 If sniffing fails, the caller should be sure to call
2974 frame_cleanup_after_sniffer. */
2977 frame_prepare_for_sniffer (struct frame_info
*frame
,
2978 const struct frame_unwind
*unwind
)
2980 gdb_assert (frame
->unwind
== NULL
);
2981 frame
->unwind
= unwind
;
2984 static struct cmd_list_element
*set_backtrace_cmdlist
;
2985 static struct cmd_list_element
*show_backtrace_cmdlist
;
2987 /* Definition of the "set backtrace" settings that are exposed as
2988 "backtrace" command options. */
2990 using boolean_option_def
2991 = gdb::option::boolean_option_def
<set_backtrace_options
>;
2992 using uinteger_option_def
2993 = gdb::option::uinteger_option_def
<set_backtrace_options
>;
2995 const gdb::option::option_def set_backtrace_option_defs
[] = {
2997 boolean_option_def
{
2999 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_main
; },
3000 show_backtrace_past_main
, /* show_cmd_cb */
3001 N_("Set whether backtraces should continue past \"main\"."),
3002 N_("Show whether backtraces should continue past \"main\"."),
3003 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
3004 the backtrace at \"main\". Set this if you need to see the rest\n\
3005 of the stack trace."),
3008 boolean_option_def
{
3010 [] (set_backtrace_options
*opt
) { return &opt
->backtrace_past_entry
; },
3011 show_backtrace_past_entry
, /* show_cmd_cb */
3012 N_("Set whether backtraces should continue past the entry point of a program."),
3013 N_("Show whether backtraces should continue past the entry point of a program."),
3014 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
3015 will terminate the backtrace there. Set this if you need to see\n\
3016 the rest of the stack trace."),
3020 void _initialize_frame ();
3022 _initialize_frame ()
3024 obstack_init (&frame_cache_obstack
);
3026 frame_stash_create ();
3028 gdb::observers::target_changed
.attach (frame_observer_target_changed
);
3030 add_basic_prefix_cmd ("backtrace", class_maintenance
, _("\
3031 Set backtrace specific variables.\n\
3032 Configure backtrace variables such as the backtrace limit"),
3033 &set_backtrace_cmdlist
, "set backtrace ",
3034 0/*allow-unknown*/, &setlist
);
3035 add_show_prefix_cmd ("backtrace", class_maintenance
, _("\
3036 Show backtrace specific variables.\n\
3037 Show backtrace variables such as the backtrace limit."),
3038 &show_backtrace_cmdlist
, "show backtrace ",
3039 0/*allow-unknown*/, &showlist
);
3041 add_setshow_uinteger_cmd ("limit", class_obscure
,
3042 &user_set_backtrace_options
.backtrace_limit
, _("\
3043 Set an upper bound on the number of backtrace levels."), _("\
3044 Show the upper bound on the number of backtrace levels."), _("\
3045 No more than the specified number of frames can be displayed or examined.\n\
3046 Literal \"unlimited\" or zero means no limit."),
3048 show_backtrace_limit
,
3049 &set_backtrace_cmdlist
,
3050 &show_backtrace_cmdlist
);
3052 gdb::option::add_setshow_cmds_for_options
3053 (class_stack
, &user_set_backtrace_options
,
3054 set_backtrace_option_defs
, &set_backtrace_cmdlist
, &show_backtrace_cmdlist
);
3056 /* Debug this files internals. */
3057 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
3058 Set frame debugging."), _("\
3059 Show frame debugging."), _("\
3060 When non-zero, frame specific internal debugging is enabled."),
3063 &setdebuglist
, &showdebuglist
);