1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 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 "gdb_assert.h"
28 #include "user-regs.h"
29 #include "gdb_obstack.h"
30 #include "dummy-frame.h"
31 #include "sentinel-frame.h"
35 #include "frame-unwind.h"
36 #include "frame-base.h"
41 #include "exceptions.h"
42 #include "gdbthread.h"
44 #include "inline-frame.h"
45 #include "tracepoint.h"
49 static struct frame_info
*get_prev_frame_1 (struct frame_info
*this_frame
);
50 static struct frame_info
*get_prev_frame_raw (struct frame_info
*this_frame
);
51 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason
);
53 /* Status of some values cached in the frame_info object. */
55 enum cached_copy_status
57 /* Value is unknown. */
60 /* We have a value. */
63 /* Value was not saved. */
66 /* Value is unavailable. */
70 /* We keep a cache of stack frames, each of which is a "struct
71 frame_info". The innermost one gets allocated (in
72 wait_for_inferior) each time the inferior stops; current_frame
73 points to it. Additional frames get allocated (in get_prev_frame)
74 as needed, and are chained through the next and prev fields. Any
75 time that the frame cache becomes invalid (most notably when we
76 execute something, but also if we change how we interpret the
77 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
78 which reads new symbols)), we should call reinit_frame_cache. */
82 /* Level of this frame. The inner-most (youngest) frame is at level
83 0. As you move towards the outer-most (oldest) frame, the level
84 increases. This is a cached value. It could just as easily be
85 computed by counting back from the selected frame to the inner
87 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
88 reserved to indicate a bogus frame - one that has been created
89 just to keep GDB happy (GDB always needs a frame). For the
90 moment leave this as speculation. */
93 /* The frame's program space. */
94 struct program_space
*pspace
;
96 /* The frame's address space. */
97 struct address_space
*aspace
;
99 /* The frame's low-level unwinder and corresponding cache. The
100 low-level unwinder is responsible for unwinding register values
101 for the previous frame. The low-level unwind methods are
102 selected based on the presence, or otherwise, of register unwind
103 information such as CFI. */
104 void *prologue_cache
;
105 const struct frame_unwind
*unwind
;
107 /* Cached copy of the previous frame's architecture. */
111 struct gdbarch
*arch
;
114 /* Cached copy of the previous frame's resume address. */
116 enum cached_copy_status status
;
120 /* Cached copy of the previous frame's function address. */
127 /* This frame's ID. */
131 struct frame_id value
;
134 /* The frame's high-level base methods, and corresponding cache.
135 The high level base methods are selected based on the frame's
137 const struct frame_base
*base
;
140 /* Pointers to the next (down, inner, younger) and previous (up,
141 outer, older) frame_info's in the frame cache. */
142 struct frame_info
*next
; /* down, inner, younger */
144 struct frame_info
*prev
; /* up, outer, older */
146 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
147 could. Only valid when PREV_P is set. */
148 enum unwind_stop_reason stop_reason
;
151 /* A frame stash used to speed up frame lookups. Create a hash table
152 to stash frames previously accessed from the frame cache for
153 quicker subsequent retrieval. The hash table is emptied whenever
154 the frame cache is invalidated. */
156 static htab_t frame_stash
;
158 /* Internal function to calculate a hash from the frame_id addresses,
159 using as many valid addresses as possible. Frames below level 0
160 are not stored in the hash table. */
163 frame_addr_hash (const void *ap
)
165 const struct frame_info
*frame
= ap
;
166 const struct frame_id f_id
= frame
->this_id
.value
;
169 gdb_assert (f_id
.stack_addr_p
|| f_id
.code_addr_p
170 || f_id
.special_addr_p
);
172 if (f_id
.stack_addr_p
)
173 hash
= iterative_hash (&f_id
.stack_addr
,
174 sizeof (f_id
.stack_addr
), hash
);
175 if (f_id
.code_addr_p
)
176 hash
= iterative_hash (&f_id
.code_addr
,
177 sizeof (f_id
.code_addr
), hash
);
178 if (f_id
.special_addr_p
)
179 hash
= iterative_hash (&f_id
.special_addr
,
180 sizeof (f_id
.special_addr
), hash
);
185 /* Internal equality function for the hash table. This function
186 defers equality operations to frame_id_eq. */
189 frame_addr_hash_eq (const void *a
, const void *b
)
191 const struct frame_info
*f_entry
= a
;
192 const struct frame_info
*f_element
= b
;
194 return frame_id_eq (f_entry
->this_id
.value
,
195 f_element
->this_id
.value
);
198 /* Internal function to create the frame_stash hash table. 100 seems
199 to be a good compromise to start the hash table at. */
202 frame_stash_create (void)
204 frame_stash
= htab_create (100,
210 /* Internal function to add a frame to the frame_stash hash table.
211 Returns false if a frame with the same ID was already stashed, true
215 frame_stash_add (struct frame_info
*frame
)
217 struct frame_info
**slot
;
219 /* Do not try to stash the sentinel frame. */
220 gdb_assert (frame
->level
>= 0);
222 slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
226 /* If we already have a frame in the stack with the same id, we
227 either have a stack cycle (corrupted stack?), or some bug
228 elsewhere in GDB. In any case, ignore the duplicate and return
229 an indication to the caller. */
237 /* Internal function to search the frame stash for an entry with the
238 given frame ID. If found, return that frame. Otherwise return
241 static struct frame_info
*
242 frame_stash_find (struct frame_id id
)
244 struct frame_info dummy
;
245 struct frame_info
*frame
;
247 dummy
.this_id
.value
= id
;
248 frame
= htab_find (frame_stash
, &dummy
);
252 /* Internal function to invalidate the frame stash by removing all
253 entries in it. This only occurs when the frame cache is
257 frame_stash_invalidate (void)
259 htab_empty (frame_stash
);
262 /* Flag to control debugging. */
264 unsigned int frame_debug
;
266 show_frame_debug (struct ui_file
*file
, int from_tty
,
267 struct cmd_list_element
*c
, const char *value
)
269 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
272 /* Flag to indicate whether backtraces should stop at main et.al. */
274 static int backtrace_past_main
;
276 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
277 struct cmd_list_element
*c
, const char *value
)
279 fprintf_filtered (file
,
280 _("Whether backtraces should "
281 "continue past \"main\" is %s.\n"),
285 static int backtrace_past_entry
;
287 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
288 struct cmd_list_element
*c
, const char *value
)
290 fprintf_filtered (file
, _("Whether backtraces should continue past the "
291 "entry point of a program is %s.\n"),
295 static unsigned int backtrace_limit
= UINT_MAX
;
297 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
298 struct cmd_list_element
*c
, const char *value
)
300 fprintf_filtered (file
,
301 _("An upper bound on the number "
302 "of backtrace levels is %s.\n"),
308 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
311 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
313 fprintf_unfiltered (file
, "!%s", name
);
317 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
319 fprintf_unfiltered (file
, "{");
320 fprint_field (file
, "stack", id
.stack_addr_p
, id
.stack_addr
);
321 fprintf_unfiltered (file
, ",");
322 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
323 fprintf_unfiltered (file
, ",");
324 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
325 if (id
.artificial_depth
)
326 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
327 fprintf_unfiltered (file
, "}");
331 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
336 fprintf_unfiltered (file
, "NORMAL_FRAME");
339 fprintf_unfiltered (file
, "DUMMY_FRAME");
342 fprintf_unfiltered (file
, "INLINE_FRAME");
345 fprintf_unfiltered (file
, "TAILCALL_FRAME");
348 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
351 fprintf_unfiltered (file
, "ARCH_FRAME");
354 fprintf_unfiltered (file
, "SENTINEL_FRAME");
357 fprintf_unfiltered (file
, "<unknown type>");
363 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
367 fprintf_unfiltered (file
, "<NULL frame>");
370 fprintf_unfiltered (file
, "{");
371 fprintf_unfiltered (file
, "level=%d", fi
->level
);
372 fprintf_unfiltered (file
, ",");
373 fprintf_unfiltered (file
, "type=");
374 if (fi
->unwind
!= NULL
)
375 fprint_frame_type (file
, fi
->unwind
->type
);
377 fprintf_unfiltered (file
, "<unknown>");
378 fprintf_unfiltered (file
, ",");
379 fprintf_unfiltered (file
, "unwind=");
380 if (fi
->unwind
!= NULL
)
381 gdb_print_host_address (fi
->unwind
, file
);
383 fprintf_unfiltered (file
, "<unknown>");
384 fprintf_unfiltered (file
, ",");
385 fprintf_unfiltered (file
, "pc=");
386 if (fi
->next
== NULL
|| fi
->next
->prev_pc
.status
== CC_UNKNOWN
)
387 fprintf_unfiltered (file
, "<unknown>");
388 else if (fi
->next
->prev_pc
.status
== CC_VALUE
)
389 fprintf_unfiltered (file
, "%s",
390 hex_string (fi
->next
->prev_pc
.value
));
391 else if (fi
->next
->prev_pc
.status
== CC_NOT_SAVED
)
392 val_print_not_saved (file
);
393 else if (fi
->next
->prev_pc
.status
== CC_UNAVAILABLE
)
394 val_print_unavailable (file
);
395 fprintf_unfiltered (file
, ",");
396 fprintf_unfiltered (file
, "id=");
398 fprint_frame_id (file
, fi
->this_id
.value
);
400 fprintf_unfiltered (file
, "<unknown>");
401 fprintf_unfiltered (file
, ",");
402 fprintf_unfiltered (file
, "func=");
403 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.p
)
404 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
406 fprintf_unfiltered (file
, "<unknown>");
407 fprintf_unfiltered (file
, "}");
410 /* Given FRAME, return the enclosing frame as found in real frames read-in from
411 inferior memory. Skip any previous frames which were made up by GDB.
412 Return the original frame if no immediate previous frames exist. */
414 static struct frame_info
*
415 skip_artificial_frames (struct frame_info
*frame
)
417 while (get_frame_type (frame
) == INLINE_FRAME
418 || get_frame_type (frame
) == TAILCALL_FRAME
)
419 frame
= get_prev_frame (frame
);
424 /* Compute the frame's uniq ID that can be used to, later, re-find the
428 compute_frame_id (struct frame_info
*fi
)
430 gdb_assert (!fi
->this_id
.p
);
433 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
435 /* Find the unwinder. */
436 if (fi
->unwind
== NULL
)
437 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
438 /* Find THIS frame's ID. */
439 /* Default to outermost if no ID is found. */
440 fi
->this_id
.value
= outer_frame_id
;
441 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
442 gdb_assert (frame_id_p (fi
->this_id
.value
));
446 fprintf_unfiltered (gdb_stdlog
, "-> ");
447 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
448 fprintf_unfiltered (gdb_stdlog
, " }\n");
452 /* Return a frame uniq ID that can be used to, later, re-find the
456 get_frame_id (struct frame_info
*fi
)
459 return null_frame_id
;
461 gdb_assert (fi
->this_id
.p
);
462 return fi
->this_id
.value
;
466 get_stack_frame_id (struct frame_info
*next_frame
)
468 return get_frame_id (skip_artificial_frames (next_frame
));
472 frame_unwind_caller_id (struct frame_info
*next_frame
)
474 struct frame_info
*this_frame
;
476 /* Use get_prev_frame_1, and not get_prev_frame. The latter will truncate
477 the frame chain, leading to this function unintentionally
478 returning a null_frame_id (e.g., when a caller requests the frame
479 ID of "main()"s caller. */
481 next_frame
= skip_artificial_frames (next_frame
);
482 this_frame
= get_prev_frame_1 (next_frame
);
484 return get_frame_id (skip_artificial_frames (this_frame
));
486 return null_frame_id
;
489 const struct frame_id null_frame_id
; /* All zeros. */
490 const struct frame_id outer_frame_id
= { 0, 0, 0, 0, 0, 1, 0 };
493 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
494 CORE_ADDR special_addr
)
496 struct frame_id id
= null_frame_id
;
498 id
.stack_addr
= stack_addr
;
500 id
.code_addr
= code_addr
;
502 id
.special_addr
= special_addr
;
503 id
.special_addr_p
= 1;
508 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
510 struct frame_id id
= null_frame_id
;
512 id
.stack_addr
= stack_addr
;
514 id
.code_addr
= code_addr
;
520 frame_id_build_wild (CORE_ADDR stack_addr
)
522 struct frame_id id
= null_frame_id
;
524 id
.stack_addr
= stack_addr
;
530 frame_id_p (struct frame_id l
)
534 /* The frame is valid iff it has a valid stack address. */
536 /* outer_frame_id is also valid. */
537 if (!p
&& memcmp (&l
, &outer_frame_id
, sizeof (l
)) == 0)
541 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
542 fprint_frame_id (gdb_stdlog
, l
);
543 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
549 frame_id_artificial_p (struct frame_id l
)
554 return (l
.artificial_depth
!= 0);
558 frame_id_eq (struct frame_id l
, struct frame_id r
)
562 if (!l
.stack_addr_p
&& l
.special_addr_p
563 && !r
.stack_addr_p
&& r
.special_addr_p
)
564 /* The outermost frame marker is equal to itself. This is the
565 dodgy thing about outer_frame_id, since between execution steps
566 we might step into another function - from which we can't
567 unwind either. More thought required to get rid of
570 else if (!l
.stack_addr_p
|| !r
.stack_addr_p
)
571 /* Like a NaN, if either ID is invalid, the result is false.
572 Note that a frame ID is invalid iff it is the null frame ID. */
574 else if (l
.stack_addr
!= r
.stack_addr
)
575 /* If .stack addresses are different, the frames are different. */
577 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
578 /* An invalid code addr is a wild card. If .code addresses are
579 different, the frames are different. */
581 else if (l
.special_addr_p
&& r
.special_addr_p
582 && l
.special_addr
!= r
.special_addr
)
583 /* An invalid special addr is a wild card (or unused). Otherwise
584 if special addresses are different, the frames are different. */
586 else if (l
.artificial_depth
!= r
.artificial_depth
)
587 /* If artifical depths are different, the frames must be different. */
590 /* Frames are equal. */
595 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
596 fprint_frame_id (gdb_stdlog
, l
);
597 fprintf_unfiltered (gdb_stdlog
, ",r=");
598 fprint_frame_id (gdb_stdlog
, r
);
599 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
604 /* Safety net to check whether frame ID L should be inner to
605 frame ID R, according to their stack addresses.
607 This method cannot be used to compare arbitrary frames, as the
608 ranges of valid stack addresses may be discontiguous (e.g. due
611 However, it can be used as safety net to discover invalid frame
612 IDs in certain circumstances. Assuming that NEXT is the immediate
613 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
615 * The stack address of NEXT must be inner-than-or-equal to the stack
618 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
621 * If NEXT and THIS have different stack addresses, no other frame
622 in the frame chain may have a stack address in between.
624 Therefore, if frame_id_inner (TEST, THIS) holds, but
625 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
626 to a valid frame in the frame chain.
628 The sanity checks above cannot be performed when a SIGTRAMP frame
629 is involved, because signal handlers might be executed on a different
630 stack than the stack used by the routine that caused the signal
631 to be raised. This can happen for instance when a thread exceeds
632 its maximum stack size. In this case, certain compilers implement
633 a stack overflow strategy that cause the handler to be run on a
637 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
641 if (!l
.stack_addr_p
|| !r
.stack_addr_p
)
642 /* Like NaN, any operation involving an invalid ID always fails. */
644 else if (l
.artificial_depth
> r
.artificial_depth
645 && l
.stack_addr
== r
.stack_addr
646 && l
.code_addr_p
== r
.code_addr_p
647 && l
.special_addr_p
== r
.special_addr_p
648 && l
.special_addr
== r
.special_addr
)
650 /* Same function, different inlined functions. */
651 struct block
*lb
, *rb
;
653 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
655 lb
= block_for_pc (l
.code_addr
);
656 rb
= block_for_pc (r
.code_addr
);
658 if (lb
== NULL
|| rb
== NULL
)
659 /* Something's gone wrong. */
662 /* This will return true if LB and RB are the same block, or
663 if the block with the smaller depth lexically encloses the
664 block with the greater depth. */
665 inner
= contained_in (lb
, rb
);
668 /* Only return non-zero when strictly inner than. Note that, per
669 comment in "frame.h", there is some fuzz here. Frameless
670 functions are not strictly inner than (same .stack but
671 different .code and/or .special address). */
672 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
675 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
676 fprint_frame_id (gdb_stdlog
, l
);
677 fprintf_unfiltered (gdb_stdlog
, ",r=");
678 fprint_frame_id (gdb_stdlog
, r
);
679 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
685 frame_find_by_id (struct frame_id id
)
687 struct frame_info
*frame
, *prev_frame
;
689 /* ZERO denotes the null frame, let the caller decide what to do
690 about it. Should it instead return get_current_frame()? */
691 if (!frame_id_p (id
))
694 /* Try using the frame stash first. Finding it there removes the need
695 to perform the search by looping over all frames, which can be very
696 CPU-intensive if the number of frames is very high (the loop is O(n)
697 and get_prev_frame performs a series of checks that are relatively
698 expensive). This optimization is particularly useful when this function
699 is called from another function (such as value_fetch_lazy, case
700 VALUE_LVAL (val) == lval_register) which already loops over all frames,
701 making the overall behavior O(n^2). */
702 frame
= frame_stash_find (id
);
706 for (frame
= get_current_frame (); ; frame
= prev_frame
)
708 struct frame_id
this = get_frame_id (frame
);
710 if (frame_id_eq (id
, this))
711 /* An exact match. */
714 prev_frame
= get_prev_frame (frame
);
718 /* As a safety net to avoid unnecessary backtracing while trying
719 to find an invalid ID, we check for a common situation where
720 we can detect from comparing stack addresses that no other
721 frame in the current frame chain can have this ID. See the
722 comment at frame_id_inner for details. */
723 if (get_frame_type (frame
) == NORMAL_FRAME
724 && !frame_id_inner (get_frame_arch (frame
), id
, this)
725 && frame_id_inner (get_frame_arch (prev_frame
), id
,
726 get_frame_id (prev_frame
)))
733 frame_unwind_pc (struct frame_info
*this_frame
)
735 if (this_frame
->prev_pc
.status
== CC_UNKNOWN
)
737 if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame
)))
739 volatile struct gdb_exception ex
;
740 struct gdbarch
*prev_gdbarch
;
743 /* The right way. The `pure' way. The one true way. This
744 method depends solely on the register-unwind code to
745 determine the value of registers in THIS frame, and hence
746 the value of this frame's PC (resume address). A typical
747 implementation is no more than:
749 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
750 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
752 Note: this method is very heavily dependent on a correct
753 register-unwind implementation, it pays to fix that
754 method first; this method is frame type agnostic, since
755 it only deals with register values, it works with any
756 frame. This is all in stark contrast to the old
757 FRAME_SAVED_PC which would try to directly handle all the
758 different ways that a PC could be unwound. */
759 prev_gdbarch
= frame_unwind_arch (this_frame
);
761 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
763 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
767 if (ex
.error
== NOT_AVAILABLE_ERROR
)
769 this_frame
->prev_pc
.status
= CC_UNAVAILABLE
;
772 fprintf_unfiltered (gdb_stdlog
,
773 "{ frame_unwind_pc (this_frame=%d)"
774 " -> <unavailable> }\n",
777 else if (ex
.error
== OPTIMIZED_OUT_ERROR
)
779 this_frame
->prev_pc
.status
= CC_NOT_SAVED
;
782 fprintf_unfiltered (gdb_stdlog
,
783 "{ frame_unwind_pc (this_frame=%d)"
784 " -> <not saved> }\n",
788 throw_exception (ex
);
792 this_frame
->prev_pc
.value
= pc
;
793 this_frame
->prev_pc
.status
= CC_VALUE
;
795 fprintf_unfiltered (gdb_stdlog
,
796 "{ frame_unwind_pc (this_frame=%d) "
799 hex_string (this_frame
->prev_pc
.value
));
803 internal_error (__FILE__
, __LINE__
, _("No unwind_pc method"));
806 if (this_frame
->prev_pc
.status
== CC_VALUE
)
807 return this_frame
->prev_pc
.value
;
808 else if (this_frame
->prev_pc
.status
== CC_UNAVAILABLE
)
809 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
810 else if (this_frame
->prev_pc
.status
== CC_NOT_SAVED
)
811 throw_error (OPTIMIZED_OUT_ERROR
, _("PC not saved"));
813 internal_error (__FILE__
, __LINE__
,
814 "unexpected prev_pc status: %d",
815 (int) this_frame
->prev_pc
.status
);
819 frame_unwind_caller_pc (struct frame_info
*this_frame
)
821 return frame_unwind_pc (skip_artificial_frames (this_frame
));
825 get_frame_func_if_available (struct frame_info
*this_frame
, CORE_ADDR
*pc
)
827 struct frame_info
*next_frame
= this_frame
->next
;
829 if (!next_frame
->prev_func
.p
)
831 CORE_ADDR addr_in_block
;
833 /* Make certain that this, and not the adjacent, function is
835 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
837 next_frame
->prev_func
.p
= -1;
839 fprintf_unfiltered (gdb_stdlog
,
840 "{ get_frame_func (this_frame=%d)"
841 " -> unavailable }\n",
846 next_frame
->prev_func
.p
= 1;
847 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
849 fprintf_unfiltered (gdb_stdlog
,
850 "{ get_frame_func (this_frame=%d) -> %s }\n",
852 hex_string (next_frame
->prev_func
.addr
));
856 if (next_frame
->prev_func
.p
< 0)
863 *pc
= next_frame
->prev_func
.addr
;
869 get_frame_func (struct frame_info
*this_frame
)
873 if (!get_frame_func_if_available (this_frame
, &pc
))
874 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
879 static enum register_status
880 do_frame_register_read (void *src
, int regnum
, gdb_byte
*buf
)
882 if (!deprecated_frame_register_read (src
, regnum
, buf
))
883 return REG_UNAVAILABLE
;
889 frame_save_as_regcache (struct frame_info
*this_frame
)
891 struct address_space
*aspace
= get_frame_address_space (this_frame
);
892 struct regcache
*regcache
= regcache_xmalloc (get_frame_arch (this_frame
),
894 struct cleanup
*cleanups
= make_cleanup_regcache_xfree (regcache
);
896 regcache_save (regcache
, do_frame_register_read
, this_frame
);
897 discard_cleanups (cleanups
);
902 frame_pop (struct frame_info
*this_frame
)
904 struct frame_info
*prev_frame
;
905 struct regcache
*scratch
;
906 struct cleanup
*cleanups
;
908 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
910 /* Popping a dummy frame involves restoring more than just registers.
911 dummy_frame_pop does all the work. */
912 dummy_frame_pop (get_frame_id (this_frame
));
916 /* Ensure that we have a frame to pop to. */
917 prev_frame
= get_prev_frame_1 (this_frame
);
920 error (_("Cannot pop the initial frame."));
922 /* Ignore TAILCALL_FRAME type frames, they were executed already before
923 entering THISFRAME. */
924 while (get_frame_type (prev_frame
) == TAILCALL_FRAME
)
925 prev_frame
= get_prev_frame (prev_frame
);
927 /* Make a copy of all the register values unwound from this frame.
928 Save them in a scratch buffer so that there isn't a race between
929 trying to extract the old values from the current regcache while
930 at the same time writing new values into that same cache. */
931 scratch
= frame_save_as_regcache (prev_frame
);
932 cleanups
= make_cleanup_regcache_xfree (scratch
);
934 /* FIXME: cagney/2003-03-16: It should be possible to tell the
935 target's register cache that it is about to be hit with a burst
936 register transfer and that the sequence of register writes should
937 be batched. The pair target_prepare_to_store() and
938 target_store_registers() kind of suggest this functionality.
939 Unfortunately, they don't implement it. Their lack of a formal
940 definition can lead to targets writing back bogus values
941 (arguably a bug in the target code mind). */
942 /* Now copy those saved registers into the current regcache.
943 Here, regcache_cpy() calls regcache_restore(). */
944 regcache_cpy (get_current_regcache (), scratch
);
945 do_cleanups (cleanups
);
947 /* We've made right mess of GDB's local state, just discard
949 reinit_frame_cache ();
953 frame_register_unwind (struct frame_info
*frame
, int regnum
,
954 int *optimizedp
, int *unavailablep
,
955 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
956 int *realnump
, gdb_byte
*bufferp
)
960 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
961 that the value proper does not need to be fetched. */
962 gdb_assert (optimizedp
!= NULL
);
963 gdb_assert (lvalp
!= NULL
);
964 gdb_assert (addrp
!= NULL
);
965 gdb_assert (realnump
!= NULL
);
966 /* gdb_assert (bufferp != NULL); */
968 value
= frame_unwind_register_value (frame
, regnum
);
970 gdb_assert (value
!= NULL
);
972 *optimizedp
= value_optimized_out (value
);
973 *unavailablep
= !value_entirely_available (value
);
974 *lvalp
= VALUE_LVAL (value
);
975 *addrp
= value_address (value
);
976 *realnump
= VALUE_REGNUM (value
);
980 if (!*optimizedp
&& !*unavailablep
)
981 memcpy (bufferp
, value_contents_all (value
),
982 TYPE_LENGTH (value_type (value
)));
984 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
987 /* Dispose of the new value. This prevents watchpoints from
988 trying to watch the saved frame pointer. */
989 release_value (value
);
994 frame_register (struct frame_info
*frame
, int regnum
,
995 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
996 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
998 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
999 that the value proper does not need to be fetched. */
1000 gdb_assert (optimizedp
!= NULL
);
1001 gdb_assert (lvalp
!= NULL
);
1002 gdb_assert (addrp
!= NULL
);
1003 gdb_assert (realnump
!= NULL
);
1004 /* gdb_assert (bufferp != NULL); */
1006 /* Obtain the register value by unwinding the register from the next
1007 (more inner frame). */
1008 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
1009 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
1010 lvalp
, addrp
, realnump
, bufferp
);
1014 frame_unwind_register (struct frame_info
*frame
, int regnum
, gdb_byte
*buf
)
1020 enum lval_type lval
;
1022 frame_register_unwind (frame
, regnum
, &optimized
, &unavailable
,
1023 &lval
, &addr
, &realnum
, buf
);
1026 throw_error (OPTIMIZED_OUT_ERROR
,
1027 _("Register %d was not saved"), regnum
);
1029 throw_error (NOT_AVAILABLE_ERROR
,
1030 _("Register %d is not available"), regnum
);
1034 get_frame_register (struct frame_info
*frame
,
1035 int regnum
, gdb_byte
*buf
)
1037 frame_unwind_register (frame
->next
, regnum
, buf
);
1041 frame_unwind_register_value (struct frame_info
*frame
, int regnum
)
1043 struct gdbarch
*gdbarch
;
1044 struct value
*value
;
1046 gdb_assert (frame
!= NULL
);
1047 gdbarch
= frame_unwind_arch (frame
);
1051 fprintf_unfiltered (gdb_stdlog
,
1052 "{ frame_unwind_register_value "
1053 "(frame=%d,regnum=%d(%s),...) ",
1054 frame
->level
, regnum
,
1055 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1058 /* Find the unwinder. */
1059 if (frame
->unwind
== NULL
)
1060 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
1062 /* Ask this frame to unwind its register. */
1063 value
= frame
->unwind
->prev_register (frame
, &frame
->prologue_cache
, regnum
);
1067 fprintf_unfiltered (gdb_stdlog
, "->");
1068 if (value_optimized_out (value
))
1070 fprintf_unfiltered (gdb_stdlog
, " ");
1071 val_print_optimized_out (value
, gdb_stdlog
);
1075 if (VALUE_LVAL (value
) == lval_register
)
1076 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1077 VALUE_REGNUM (value
));
1078 else if (VALUE_LVAL (value
) == lval_memory
)
1079 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1081 value_address (value
)));
1083 fprintf_unfiltered (gdb_stdlog
, " computed");
1085 if (value_lazy (value
))
1086 fprintf_unfiltered (gdb_stdlog
, " lazy");
1090 const gdb_byte
*buf
= value_contents (value
);
1092 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1093 fprintf_unfiltered (gdb_stdlog
, "[");
1094 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1095 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1096 fprintf_unfiltered (gdb_stdlog
, "]");
1100 fprintf_unfiltered (gdb_stdlog
, " }\n");
1107 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1109 return frame_unwind_register_value (frame
->next
, regnum
);
1113 frame_unwind_register_signed (struct frame_info
*frame
, int regnum
)
1115 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
1116 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1117 int size
= register_size (gdbarch
, regnum
);
1118 gdb_byte buf
[MAX_REGISTER_SIZE
];
1120 frame_unwind_register (frame
, regnum
, buf
);
1121 return extract_signed_integer (buf
, size
, byte_order
);
1125 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1127 return frame_unwind_register_signed (frame
->next
, regnum
);
1131 frame_unwind_register_unsigned (struct frame_info
*frame
, int regnum
)
1133 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
1134 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1135 int size
= register_size (gdbarch
, regnum
);
1136 gdb_byte buf
[MAX_REGISTER_SIZE
];
1138 frame_unwind_register (frame
, regnum
, buf
);
1139 return extract_unsigned_integer (buf
, size
, byte_order
);
1143 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1145 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1149 read_frame_register_unsigned (struct frame_info
*frame
, int regnum
,
1152 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1154 if (!value_optimized_out (regval
)
1155 && value_entirely_available (regval
))
1157 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1158 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1159 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1161 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1169 put_frame_register (struct frame_info
*frame
, int regnum
,
1170 const gdb_byte
*buf
)
1172 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1176 enum lval_type lval
;
1179 frame_register (frame
, regnum
, &optim
, &unavail
,
1180 &lval
, &addr
, &realnum
, NULL
);
1182 error (_("Attempt to assign to a register that was not saved."));
1187 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1191 regcache_cooked_write (get_current_regcache (), realnum
, buf
);
1194 error (_("Attempt to assign to an unmodifiable value."));
1198 /* This function is deprecated. Use get_frame_register_value instead,
1199 which provides more accurate information.
1201 Find and return the value of REGNUM for the specified stack frame.
1202 The number of bytes copied is REGISTER_SIZE (REGNUM).
1204 Returns 0 if the register value could not be found. */
1207 deprecated_frame_register_read (struct frame_info
*frame
, int regnum
,
1212 enum lval_type lval
;
1216 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1217 &lval
, &addr
, &realnum
, myaddr
);
1219 return !optimized
&& !unavailable
;
1223 get_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1224 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1225 int *optimizedp
, int *unavailablep
)
1227 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1232 /* Skip registers wholly inside of OFFSET. */
1233 while (offset
>= register_size (gdbarch
, regnum
))
1235 offset
-= register_size (gdbarch
, regnum
);
1239 /* Ensure that we will not read beyond the end of the register file.
1240 This can only ever happen if the debug information is bad. */
1242 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1243 for (i
= regnum
; i
< numregs
; i
++)
1245 int thissize
= register_size (gdbarch
, i
);
1248 break; /* This register is not available on this architecture. */
1249 maxsize
+= thissize
;
1252 error (_("Bad debug information detected: "
1253 "Attempt to read %d bytes from registers."), len
);
1255 /* Copy the data. */
1258 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1263 if (curr_len
== register_size (gdbarch
, regnum
))
1265 enum lval_type lval
;
1269 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1270 &lval
, &addr
, &realnum
, myaddr
);
1271 if (*optimizedp
|| *unavailablep
)
1276 gdb_byte buf
[MAX_REGISTER_SIZE
];
1277 enum lval_type lval
;
1281 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1282 &lval
, &addr
, &realnum
, buf
);
1283 if (*optimizedp
|| *unavailablep
)
1285 memcpy (myaddr
, buf
+ offset
, curr_len
);
1300 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1301 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1303 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1305 /* Skip registers wholly inside of OFFSET. */
1306 while (offset
>= register_size (gdbarch
, regnum
))
1308 offset
-= register_size (gdbarch
, regnum
);
1312 /* Copy the data. */
1315 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1320 if (curr_len
== register_size (gdbarch
, regnum
))
1322 put_frame_register (frame
, regnum
, myaddr
);
1326 gdb_byte buf
[MAX_REGISTER_SIZE
];
1328 deprecated_frame_register_read (frame
, regnum
, buf
);
1329 memcpy (buf
+ offset
, myaddr
, curr_len
);
1330 put_frame_register (frame
, regnum
, buf
);
1340 /* Create a sentinel frame. */
1342 static struct frame_info
*
1343 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1345 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1348 frame
->pspace
= pspace
;
1349 frame
->aspace
= get_regcache_aspace (regcache
);
1350 /* Explicitly initialize the sentinel frame's cache. Provide it
1351 with the underlying regcache. In the future additional
1352 information, such as the frame's thread will be added. */
1353 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1354 /* For the moment there is only one sentinel frame implementation. */
1355 frame
->unwind
= &sentinel_frame_unwind
;
1356 /* Link this frame back to itself. The frame is self referential
1357 (the unwound PC is the same as the pc), so make it so. */
1358 frame
->next
= frame
;
1359 /* Make the sentinel frame's ID valid, but invalid. That way all
1360 comparisons with it should fail. */
1361 frame
->this_id
.p
= 1;
1362 frame
->this_id
.value
= null_frame_id
;
1365 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1366 fprint_frame (gdb_stdlog
, frame
);
1367 fprintf_unfiltered (gdb_stdlog
, " }\n");
1372 /* Info about the innermost stack frame (contents of FP register). */
1374 static struct frame_info
*current_frame
;
1376 /* Cache for frame addresses already read by gdb. Valid only while
1377 inferior is stopped. Control variables for the frame cache should
1378 be local to this module. */
1380 static struct obstack frame_cache_obstack
;
1383 frame_obstack_zalloc (unsigned long size
)
1385 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1387 memset (data
, 0, size
);
1391 /* Return the innermost (currently executing) stack frame. This is
1392 split into two functions. The function unwind_to_current_frame()
1393 is wrapped in catch exceptions so that, even when the unwind of the
1394 sentinel frame fails, the function still returns a stack frame. */
1397 unwind_to_current_frame (struct ui_out
*ui_out
, void *args
)
1399 struct frame_info
*frame
= get_prev_frame (args
);
1401 /* A sentinel frame can fail to unwind, e.g., because its PC value
1402 lands in somewhere like start. */
1405 current_frame
= frame
;
1410 get_current_frame (void)
1412 /* First check, and report, the lack of registers. Having GDB
1413 report "No stack!" or "No memory" when the target doesn't even
1414 have registers is very confusing. Besides, "printcmd.exp"
1415 explicitly checks that ``print $pc'' with no registers prints "No
1417 if (!target_has_registers
)
1418 error (_("No registers."));
1419 if (!target_has_stack
)
1420 error (_("No stack."));
1421 if (!target_has_memory
)
1422 error (_("No memory."));
1423 /* Traceframes are effectively a substitute for the live inferior. */
1424 if (get_traceframe_number () < 0)
1426 if (ptid_equal (inferior_ptid
, null_ptid
))
1427 error (_("No selected thread."));
1428 if (is_exited (inferior_ptid
))
1429 error (_("Invalid selected thread."));
1430 if (is_executing (inferior_ptid
))
1431 error (_("Target is executing."));
1434 if (current_frame
== NULL
)
1436 struct frame_info
*sentinel_frame
=
1437 create_sentinel_frame (current_program_space
, get_current_regcache ());
1438 if (catch_exceptions (current_uiout
, unwind_to_current_frame
,
1439 sentinel_frame
, RETURN_MASK_ERROR
) != 0)
1441 /* Oops! Fake a current frame? Is this useful? It has a PC
1442 of zero, for instance. */
1443 current_frame
= sentinel_frame
;
1446 return current_frame
;
1449 /* The "selected" stack frame is used by default for local and arg
1450 access. May be zero, for no selected frame. */
1452 static struct frame_info
*selected_frame
;
1455 has_stack_frames (void)
1457 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1460 /* Traceframes are effectively a substitute for the live inferior. */
1461 if (get_traceframe_number () < 0)
1463 /* No current inferior, no frame. */
1464 if (ptid_equal (inferior_ptid
, null_ptid
))
1467 /* Don't try to read from a dead thread. */
1468 if (is_exited (inferior_ptid
))
1471 /* ... or from a spinning thread. */
1472 if (is_executing (inferior_ptid
))
1479 /* Return the selected frame. Always non-NULL (unless there isn't an
1480 inferior sufficient for creating a frame) in which case an error is
1484 get_selected_frame (const char *message
)
1486 if (selected_frame
== NULL
)
1488 if (message
!= NULL
&& !has_stack_frames ())
1489 error (("%s"), message
);
1490 /* Hey! Don't trust this. It should really be re-finding the
1491 last selected frame of the currently selected thread. This,
1492 though, is better than nothing. */
1493 select_frame (get_current_frame ());
1495 /* There is always a frame. */
1496 gdb_assert (selected_frame
!= NULL
);
1497 return selected_frame
;
1500 /* If there is a selected frame, return it. Otherwise, return NULL. */
1503 get_selected_frame_if_set (void)
1505 return selected_frame
;
1508 /* This is a variant of get_selected_frame() which can be called when
1509 the inferior does not have a frame; in that case it will return
1510 NULL instead of calling error(). */
1513 deprecated_safe_get_selected_frame (void)
1515 if (!has_stack_frames ())
1517 return get_selected_frame (NULL
);
1520 /* Select frame FI (or NULL - to invalidate the current frame). */
1523 select_frame (struct frame_info
*fi
)
1525 selected_frame
= fi
;
1526 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1527 frame is being invalidated. */
1528 if (deprecated_selected_frame_level_changed_hook
)
1529 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi
));
1531 /* FIXME: kseitz/2002-08-28: It would be nice to call
1532 selected_frame_level_changed_event() right here, but due to limitations
1533 in the current interfaces, we would end up flooding UIs with events
1534 because select_frame() is used extensively internally.
1536 Once we have frame-parameterized frame (and frame-related) commands,
1537 the event notification can be moved here, since this function will only
1538 be called when the user's selected frame is being changed. */
1540 /* Ensure that symbols for this frame are read in. Also, determine the
1541 source language of this frame, and switch to it if desired. */
1546 /* We retrieve the frame's symtab by using the frame PC.
1547 However we cannot use the frame PC as-is, because it usually
1548 points to the instruction following the "call", which is
1549 sometimes the first instruction of another function. So we
1550 rely on get_frame_address_in_block() which provides us with a
1551 PC which is guaranteed to be inside the frame's code
1553 if (get_frame_address_in_block_if_available (fi
, &pc
))
1555 struct symtab
*s
= find_pc_symtab (pc
);
1558 && s
->language
!= current_language
->la_language
1559 && s
->language
!= language_unknown
1560 && language_mode
== language_mode_auto
)
1561 set_language (s
->language
);
1566 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1567 Always returns a non-NULL value. */
1570 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1572 struct frame_info
*fi
;
1576 fprintf_unfiltered (gdb_stdlog
,
1577 "{ create_new_frame (addr=%s, pc=%s) ",
1578 hex_string (addr
), hex_string (pc
));
1581 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1583 fi
->next
= create_sentinel_frame (current_program_space
,
1584 get_current_regcache ());
1586 /* Set/update this frame's cached PC value, found in the next frame.
1587 Do this before looking for this frame's unwinder. A sniffer is
1588 very likely to read this, and the corresponding unwinder is
1589 entitled to rely that the PC doesn't magically change. */
1590 fi
->next
->prev_pc
.value
= pc
;
1591 fi
->next
->prev_pc
.status
= CC_VALUE
;
1593 /* We currently assume that frame chain's can't cross spaces. */
1594 fi
->pspace
= fi
->next
->pspace
;
1595 fi
->aspace
= fi
->next
->aspace
;
1597 /* Select/initialize both the unwind function and the frame's type
1599 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1602 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1606 fprintf_unfiltered (gdb_stdlog
, "-> ");
1607 fprint_frame (gdb_stdlog
, fi
);
1608 fprintf_unfiltered (gdb_stdlog
, " }\n");
1614 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1615 innermost frame). Be careful to not fall off the bottom of the
1616 frame chain and onto the sentinel frame. */
1619 get_next_frame (struct frame_info
*this_frame
)
1621 if (this_frame
->level
> 0)
1622 return this_frame
->next
;
1627 /* Observer for the target_changed event. */
1630 frame_observer_target_changed (struct target_ops
*target
)
1632 reinit_frame_cache ();
1635 /* Flush the entire frame cache. */
1638 reinit_frame_cache (void)
1640 struct frame_info
*fi
;
1642 /* Tear down all frame caches. */
1643 for (fi
= current_frame
; fi
!= NULL
; fi
= fi
->prev
)
1645 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1646 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1647 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1648 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1651 /* Since we can't really be sure what the first object allocated was. */
1652 obstack_free (&frame_cache_obstack
, 0);
1653 obstack_init (&frame_cache_obstack
);
1655 if (current_frame
!= NULL
)
1656 annotate_frames_invalid ();
1658 current_frame
= NULL
; /* Invalidate cache */
1659 select_frame (NULL
);
1660 frame_stash_invalidate ();
1662 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1665 /* Find where a register is saved (in memory or another register).
1666 The result of frame_register_unwind is just where it is saved
1667 relative to this particular frame. */
1670 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1671 int *optimizedp
, enum lval_type
*lvalp
,
1672 CORE_ADDR
*addrp
, int *realnump
)
1674 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1676 while (this_frame
!= NULL
)
1680 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1681 lvalp
, addrp
, realnump
, NULL
);
1686 if (*lvalp
!= lval_register
)
1690 this_frame
= get_next_frame (this_frame
);
1694 /* Get the previous raw frame, and check that it is not identical to
1695 same other frame frame already in the chain. If it is, there is
1696 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1697 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1698 validity tests, that compare THIS_FRAME and the next frame, we do
1699 this right after creating the previous frame, to avoid ever ending
1700 up with two frames with the same id in the frame chain. */
1702 static struct frame_info
*
1703 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1705 struct frame_info
*prev_frame
;
1707 prev_frame
= get_prev_frame_raw (this_frame
);
1708 if (prev_frame
== NULL
)
1711 compute_frame_id (prev_frame
);
1712 if (frame_stash_add (prev_frame
))
1715 /* Another frame with the same id was already in the stash. We just
1716 detected a cycle. */
1719 fprintf_unfiltered (gdb_stdlog
, "-> ");
1720 fprint_frame (gdb_stdlog
, NULL
);
1721 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1723 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1725 prev_frame
->next
= NULL
;
1726 this_frame
->prev
= NULL
;
1730 /* Return a "struct frame_info" corresponding to the frame that called
1731 THIS_FRAME. Returns NULL if there is no such frame.
1733 Unlike get_prev_frame, this function always tries to unwind the
1736 static struct frame_info
*
1737 get_prev_frame_1 (struct frame_info
*this_frame
)
1739 struct gdbarch
*gdbarch
;
1741 gdb_assert (this_frame
!= NULL
);
1742 gdbarch
= get_frame_arch (this_frame
);
1746 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_1 (this_frame=");
1747 if (this_frame
!= NULL
)
1748 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1750 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1751 fprintf_unfiltered (gdb_stdlog
, ") ");
1754 /* Only try to do the unwind once. */
1755 if (this_frame
->prev_p
)
1759 fprintf_unfiltered (gdb_stdlog
, "-> ");
1760 fprint_frame (gdb_stdlog
, this_frame
->prev
);
1761 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
1763 return this_frame
->prev
;
1766 /* If the frame unwinder hasn't been selected yet, we must do so
1767 before setting prev_p; otherwise the check for misbehaved
1768 sniffers will think that this frame's sniffer tried to unwind
1769 further (see frame_cleanup_after_sniffer). */
1770 if (this_frame
->unwind
== NULL
)
1771 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
1773 this_frame
->prev_p
= 1;
1774 this_frame
->stop_reason
= UNWIND_NO_REASON
;
1776 /* If we are unwinding from an inline frame, all of the below tests
1777 were already performed when we unwound from the next non-inline
1778 frame. We must skip them, since we can not get THIS_FRAME's ID
1779 until we have unwound all the way down to the previous non-inline
1781 if (get_frame_type (this_frame
) == INLINE_FRAME
)
1782 return get_prev_frame_if_no_cycle (this_frame
);
1784 /* Check that this frame is unwindable. If it isn't, don't try to
1785 unwind to the prev frame. */
1786 this_frame
->stop_reason
1787 = this_frame
->unwind
->stop_reason (this_frame
,
1788 &this_frame
->prologue_cache
);
1790 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
1794 enum unwind_stop_reason reason
= this_frame
->stop_reason
;
1796 fprintf_unfiltered (gdb_stdlog
, "-> ");
1797 fprint_frame (gdb_stdlog
, NULL
);
1798 fprintf_unfiltered (gdb_stdlog
, " // %s }\n",
1799 frame_stop_reason_symbol_string (reason
));
1804 /* Check that this frame's ID isn't inner to (younger, below, next)
1805 the next frame. This happens when a frame unwind goes backwards.
1806 This check is valid only if this frame and the next frame are NORMAL.
1807 See the comment at frame_id_inner for details. */
1808 if (get_frame_type (this_frame
) == NORMAL_FRAME
1809 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
1810 && frame_id_inner (get_frame_arch (this_frame
->next
),
1811 get_frame_id (this_frame
),
1812 get_frame_id (this_frame
->next
)))
1814 CORE_ADDR this_pc_in_block
;
1815 struct minimal_symbol
*morestack_msym
;
1816 const char *morestack_name
= NULL
;
1818 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
1819 this_pc_in_block
= get_frame_address_in_block (this_frame
);
1820 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
1822 morestack_name
= SYMBOL_LINKAGE_NAME (morestack_msym
);
1823 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
1827 fprintf_unfiltered (gdb_stdlog
, "-> ");
1828 fprint_frame (gdb_stdlog
, NULL
);
1829 fprintf_unfiltered (gdb_stdlog
,
1830 " // this frame ID is inner }\n");
1832 this_frame
->stop_reason
= UNWIND_INNER_ID
;
1837 /* Check that this and the next frame do not unwind the PC register
1838 to the same memory location. If they do, then even though they
1839 have different frame IDs, the new frame will be bogus; two
1840 functions can't share a register save slot for the PC. This can
1841 happen when the prologue analyzer finds a stack adjustment, but
1844 This check does assume that the "PC register" is roughly a
1845 traditional PC, even if the gdbarch_unwind_pc method adjusts
1846 it (we do not rely on the value, only on the unwound PC being
1847 dependent on this value). A potential improvement would be
1848 to have the frame prev_pc method and the gdbarch unwind_pc
1849 method set the same lval and location information as
1850 frame_register_unwind. */
1851 if (this_frame
->level
> 0
1852 && gdbarch_pc_regnum (gdbarch
) >= 0
1853 && get_frame_type (this_frame
) == NORMAL_FRAME
1854 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
1855 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
1857 int optimized
, realnum
, nrealnum
;
1858 enum lval_type lval
, nlval
;
1859 CORE_ADDR addr
, naddr
;
1861 frame_register_unwind_location (this_frame
,
1862 gdbarch_pc_regnum (gdbarch
),
1863 &optimized
, &lval
, &addr
, &realnum
);
1864 frame_register_unwind_location (get_next_frame (this_frame
),
1865 gdbarch_pc_regnum (gdbarch
),
1866 &optimized
, &nlval
, &naddr
, &nrealnum
);
1868 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
1869 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
1873 fprintf_unfiltered (gdb_stdlog
, "-> ");
1874 fprint_frame (gdb_stdlog
, NULL
);
1875 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
1878 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
1879 this_frame
->prev
= NULL
;
1884 return get_prev_frame_if_no_cycle (this_frame
);
1887 /* Construct a new "struct frame_info" and link it previous to
1890 static struct frame_info
*
1891 get_prev_frame_raw (struct frame_info
*this_frame
)
1893 struct frame_info
*prev_frame
;
1895 /* Allocate the new frame but do not wire it in to the frame chain.
1896 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1897 frame->next to pull some fancy tricks (of course such code is, by
1898 definition, recursive). Try to prevent it.
1900 There is no reason to worry about memory leaks, should the
1901 remainder of the function fail. The allocated memory will be
1902 quickly reclaimed when the frame cache is flushed, and the `we've
1903 been here before' check above will stop repeated memory
1904 allocation calls. */
1905 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1906 prev_frame
->level
= this_frame
->level
+ 1;
1908 /* For now, assume we don't have frame chains crossing address
1910 prev_frame
->pspace
= this_frame
->pspace
;
1911 prev_frame
->aspace
= this_frame
->aspace
;
1913 /* Don't yet compute ->unwind (and hence ->type). It is computed
1914 on-demand in get_frame_type, frame_register_unwind, and
1917 /* Don't yet compute the frame's ID. It is computed on-demand by
1920 /* The unwound frame ID is validate at the start of this function,
1921 as part of the logic to decide if that frame should be further
1922 unwound, and not here while the prev frame is being created.
1923 Doing this makes it possible for the user to examine a frame that
1924 has an invalid frame ID.
1926 Some very old VAX code noted: [...] For the sake of argument,
1927 suppose that the stack is somewhat trashed (which is one reason
1928 that "info frame" exists). So, return 0 (indicating we don't
1929 know the address of the arglist) if we don't know what frame this
1933 this_frame
->prev
= prev_frame
;
1934 prev_frame
->next
= this_frame
;
1938 fprintf_unfiltered (gdb_stdlog
, "-> ");
1939 fprint_frame (gdb_stdlog
, prev_frame
);
1940 fprintf_unfiltered (gdb_stdlog
, " }\n");
1946 /* Debug routine to print a NULL frame being returned. */
1949 frame_debug_got_null_frame (struct frame_info
*this_frame
,
1954 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
1955 if (this_frame
!= NULL
)
1956 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1958 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1959 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
1963 /* Is this (non-sentinel) frame in the "main"() function? */
1966 inside_main_func (struct frame_info
*this_frame
)
1968 struct minimal_symbol
*msymbol
;
1971 if (symfile_objfile
== 0)
1973 msymbol
= lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
1974 if (msymbol
== NULL
)
1976 /* Make certain that the code, and not descriptor, address is
1978 maddr
= gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
1979 SYMBOL_VALUE_ADDRESS (msymbol
),
1981 return maddr
== get_frame_func (this_frame
);
1984 /* Test whether THIS_FRAME is inside the process entry point function. */
1987 inside_entry_func (struct frame_info
*this_frame
)
1989 CORE_ADDR entry_point
;
1991 if (!entry_point_address_query (&entry_point
))
1994 return get_frame_func (this_frame
) == entry_point
;
1997 /* Return a structure containing various interesting information about
1998 the frame that called THIS_FRAME. Returns NULL if there is entier
1999 no such frame or the frame fails any of a set of target-independent
2000 condition that should terminate the frame chain (e.g., as unwinding
2003 This function should not contain target-dependent tests, such as
2004 checking whether the program-counter is zero. */
2007 get_prev_frame (struct frame_info
*this_frame
)
2012 /* There is always a frame. If this assertion fails, suspect that
2013 something should be calling get_selected_frame() or
2014 get_current_frame(). */
2015 gdb_assert (this_frame
!= NULL
);
2016 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2018 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2019 sense to stop unwinding at a dummy frame. One place where a dummy
2020 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2021 pcsqh register (space register for the instruction at the head of the
2022 instruction queue) cannot be written directly; the only way to set it
2023 is to branch to code that is in the target space. In order to implement
2024 frame dummies on HPUX, the called function is made to jump back to where
2025 the inferior was when the user function was called. If gdb was inside
2026 the main function when we created the dummy frame, the dummy frame will
2027 point inside the main function. */
2028 if (this_frame
->level
>= 0
2029 && get_frame_type (this_frame
) == NORMAL_FRAME
2030 && !backtrace_past_main
2032 && inside_main_func (this_frame
))
2033 /* Don't unwind past main(). Note, this is done _before_ the
2034 frame has been marked as previously unwound. That way if the
2035 user later decides to enable unwinds past main(), that will
2036 automatically happen. */
2038 frame_debug_got_null_frame (this_frame
, "inside main func");
2042 /* If the user's backtrace limit has been exceeded, stop. We must
2043 add two to the current level; one of those accounts for backtrace_limit
2044 being 1-based and the level being 0-based, and the other accounts for
2045 the level of the new frame instead of the level of the current
2047 if (this_frame
->level
+ 2 > backtrace_limit
)
2049 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2053 /* If we're already inside the entry function for the main objfile,
2054 then it isn't valid. Don't apply this test to a dummy frame -
2055 dummy frame PCs typically land in the entry func. Don't apply
2056 this test to the sentinel frame. Sentinel frames should always
2057 be allowed to unwind. */
2058 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2059 wasn't checking for "main" in the minimal symbols. With that
2060 fixed asm-source tests now stop in "main" instead of halting the
2061 backtrace in weird and wonderful ways somewhere inside the entry
2062 file. Suspect that tests for inside the entry file/func were
2063 added to work around that (now fixed) case. */
2064 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2065 suggested having the inside_entry_func test use the
2066 inside_main_func() msymbol trick (along with entry_point_address()
2067 I guess) to determine the address range of the start function.
2068 That should provide a far better stopper than the current
2070 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2071 applied tail-call optimizations to main so that a function called
2072 from main returns directly to the caller of main. Since we don't
2073 stop at main, we should at least stop at the entry point of the
2075 if (this_frame
->level
>= 0
2076 && get_frame_type (this_frame
) == NORMAL_FRAME
2077 && !backtrace_past_entry
2079 && inside_entry_func (this_frame
))
2081 frame_debug_got_null_frame (this_frame
, "inside entry func");
2085 /* Assume that the only way to get a zero PC is through something
2086 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2087 will never unwind a zero PC. */
2088 if (this_frame
->level
> 0
2089 && (get_frame_type (this_frame
) == NORMAL_FRAME
2090 || get_frame_type (this_frame
) == INLINE_FRAME
)
2091 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2092 && frame_pc_p
&& frame_pc
== 0)
2094 frame_debug_got_null_frame (this_frame
, "zero PC");
2098 return get_prev_frame_1 (this_frame
);
2102 get_frame_pc (struct frame_info
*frame
)
2104 gdb_assert (frame
->next
!= NULL
);
2105 return frame_unwind_pc (frame
->next
);
2109 get_frame_pc_if_available (struct frame_info
*frame
, CORE_ADDR
*pc
)
2111 volatile struct gdb_exception ex
;
2113 gdb_assert (frame
->next
!= NULL
);
2115 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
2117 *pc
= frame_unwind_pc (frame
->next
);
2121 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2124 throw_exception (ex
);
2130 /* Return an address that falls within THIS_FRAME's code block. */
2133 get_frame_address_in_block (struct frame_info
*this_frame
)
2135 /* A draft address. */
2136 CORE_ADDR pc
= get_frame_pc (this_frame
);
2138 struct frame_info
*next_frame
= this_frame
->next
;
2140 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2141 Normally the resume address is inside the body of the function
2142 associated with THIS_FRAME, but there is a special case: when
2143 calling a function which the compiler knows will never return
2144 (for instance abort), the call may be the very last instruction
2145 in the calling function. The resume address will point after the
2146 call and may be at the beginning of a different function
2149 If THIS_FRAME is a signal frame or dummy frame, then we should
2150 not adjust the unwound PC. For a dummy frame, GDB pushed the
2151 resume address manually onto the stack. For a signal frame, the
2152 OS may have pushed the resume address manually and invoked the
2153 handler (e.g. GNU/Linux), or invoked the trampoline which called
2154 the signal handler - but in either case the signal handler is
2155 expected to return to the trampoline. So in both of these
2156 cases we know that the resume address is executable and
2157 related. So we only need to adjust the PC if THIS_FRAME
2158 is a normal function.
2160 If the program has been interrupted while THIS_FRAME is current,
2161 then clearly the resume address is inside the associated
2162 function. There are three kinds of interruption: debugger stop
2163 (next frame will be SENTINEL_FRAME), operating system
2164 signal or exception (next frame will be SIGTRAMP_FRAME),
2165 or debugger-induced function call (next frame will be
2166 DUMMY_FRAME). So we only need to adjust the PC if
2167 NEXT_FRAME is a normal function.
2169 We check the type of NEXT_FRAME first, since it is already
2170 known; frame type is determined by the unwinder, and since
2171 we have THIS_FRAME we've already selected an unwinder for
2174 If the next frame is inlined, we need to keep going until we find
2175 the real function - for instance, if a signal handler is invoked
2176 while in an inlined function, then the code address of the
2177 "calling" normal function should not be adjusted either. */
2179 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2180 next_frame
= next_frame
->next
;
2182 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2183 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2184 && (get_frame_type (this_frame
) == NORMAL_FRAME
2185 || get_frame_type (this_frame
) == TAILCALL_FRAME
2186 || get_frame_type (this_frame
) == INLINE_FRAME
))
2193 get_frame_address_in_block_if_available (struct frame_info
*this_frame
,
2196 volatile struct gdb_exception ex
;
2198 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
2200 *pc
= get_frame_address_in_block (this_frame
);
2202 if (ex
.reason
< 0 && ex
.error
== NOT_AVAILABLE_ERROR
)
2204 else if (ex
.reason
< 0)
2205 throw_exception (ex
);
2211 find_frame_sal (struct frame_info
*frame
, struct symtab_and_line
*sal
)
2213 struct frame_info
*next_frame
;
2217 /* If the next frame represents an inlined function call, this frame's
2218 sal is the "call site" of that inlined function, which can not
2219 be inferred from get_frame_pc. */
2220 next_frame
= get_next_frame (frame
);
2221 if (frame_inlined_callees (frame
) > 0)
2226 sym
= get_frame_function (next_frame
);
2228 sym
= inline_skipped_symbol (inferior_ptid
);
2230 /* If frame is inline, it certainly has symbols. */
2233 if (SYMBOL_LINE (sym
) != 0)
2235 sal
->symtab
= SYMBOL_SYMTAB (sym
);
2236 sal
->line
= SYMBOL_LINE (sym
);
2239 /* If the symbol does not have a location, we don't know where
2240 the call site is. Do not pretend to. This is jarring, but
2241 we can't do much better. */
2242 sal
->pc
= get_frame_pc (frame
);
2244 sal
->pspace
= get_frame_program_space (frame
);
2249 /* If FRAME is not the innermost frame, that normally means that
2250 FRAME->pc points at the return instruction (which is *after* the
2251 call instruction), and we want to get the line containing the
2252 call (because the call is where the user thinks the program is).
2253 However, if the next frame is either a SIGTRAMP_FRAME or a
2254 DUMMY_FRAME, then the next frame will contain a saved interrupt
2255 PC and such a PC indicates the current (rather than next)
2256 instruction/line, consequently, for such cases, want to get the
2257 line containing fi->pc. */
2258 if (!get_frame_pc_if_available (frame
, &pc
))
2264 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2265 (*sal
) = find_pc_line (pc
, notcurrent
);
2268 /* Per "frame.h", return the ``address'' of the frame. Code should
2269 really be using get_frame_id(). */
2271 get_frame_base (struct frame_info
*fi
)
2273 return get_frame_id (fi
).stack_addr
;
2276 /* High-level offsets into the frame. Used by the debug info. */
2279 get_frame_base_address (struct frame_info
*fi
)
2281 if (get_frame_type (fi
) != NORMAL_FRAME
)
2283 if (fi
->base
== NULL
)
2284 fi
->base
= frame_base_find_by_frame (fi
);
2285 /* Sneaky: If the low-level unwind and high-level base code share a
2286 common unwinder, let them share the prologue cache. */
2287 if (fi
->base
->unwind
== fi
->unwind
)
2288 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2289 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2293 get_frame_locals_address (struct frame_info
*fi
)
2295 if (get_frame_type (fi
) != NORMAL_FRAME
)
2297 /* If there isn't a frame address method, find it. */
2298 if (fi
->base
== NULL
)
2299 fi
->base
= frame_base_find_by_frame (fi
);
2300 /* Sneaky: If the low-level unwind and high-level base code share a
2301 common unwinder, let them share the prologue cache. */
2302 if (fi
->base
->unwind
== fi
->unwind
)
2303 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2304 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2308 get_frame_args_address (struct frame_info
*fi
)
2310 if (get_frame_type (fi
) != NORMAL_FRAME
)
2312 /* If there isn't a frame address method, find it. */
2313 if (fi
->base
== NULL
)
2314 fi
->base
= frame_base_find_by_frame (fi
);
2315 /* Sneaky: If the low-level unwind and high-level base code share a
2316 common unwinder, let them share the prologue cache. */
2317 if (fi
->base
->unwind
== fi
->unwind
)
2318 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2319 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2322 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2326 frame_unwinder_is (struct frame_info
*fi
, const struct frame_unwind
*unwinder
)
2328 if (fi
->unwind
== NULL
)
2329 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2330 return fi
->unwind
== unwinder
;
2333 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2334 or -1 for a NULL frame. */
2337 frame_relative_level (struct frame_info
*fi
)
2346 get_frame_type (struct frame_info
*frame
)
2348 if (frame
->unwind
== NULL
)
2349 /* Initialize the frame's unwinder because that's what
2350 provides the frame's type. */
2351 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2352 return frame
->unwind
->type
;
2355 struct program_space
*
2356 get_frame_program_space (struct frame_info
*frame
)
2358 return frame
->pspace
;
2361 struct program_space
*
2362 frame_unwind_program_space (struct frame_info
*this_frame
)
2364 gdb_assert (this_frame
);
2366 /* This is really a placeholder to keep the API consistent --- we
2367 assume for now that we don't have frame chains crossing
2369 return this_frame
->pspace
;
2372 struct address_space
*
2373 get_frame_address_space (struct frame_info
*frame
)
2375 return frame
->aspace
;
2378 /* Memory access methods. */
2381 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2382 gdb_byte
*buf
, int len
)
2384 read_memory (addr
, buf
, len
);
2388 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2391 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2392 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2394 return read_memory_integer (addr
, len
, byte_order
);
2398 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2401 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2402 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2404 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2408 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2409 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2411 /* NOTE: target_read_memory returns zero on success! */
2412 return !target_read_memory (addr
, buf
, len
);
2415 /* Architecture methods. */
2418 get_frame_arch (struct frame_info
*this_frame
)
2420 return frame_unwind_arch (this_frame
->next
);
2424 frame_unwind_arch (struct frame_info
*next_frame
)
2426 if (!next_frame
->prev_arch
.p
)
2428 struct gdbarch
*arch
;
2430 if (next_frame
->unwind
== NULL
)
2431 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2433 if (next_frame
->unwind
->prev_arch
!= NULL
)
2434 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2435 &next_frame
->prologue_cache
);
2437 arch
= get_frame_arch (next_frame
);
2439 next_frame
->prev_arch
.arch
= arch
;
2440 next_frame
->prev_arch
.p
= 1;
2442 fprintf_unfiltered (gdb_stdlog
,
2443 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2445 gdbarch_bfd_arch_info (arch
)->printable_name
);
2448 return next_frame
->prev_arch
.arch
;
2452 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2454 return frame_unwind_arch (skip_artificial_frames (next_frame
));
2457 /* Stack pointer methods. */
2460 get_frame_sp (struct frame_info
*this_frame
)
2462 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2464 /* Normality - an architecture that provides a way of obtaining any
2465 frame inner-most address. */
2466 if (gdbarch_unwind_sp_p (gdbarch
))
2467 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2468 operate on THIS_FRAME now. */
2469 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2470 /* Now things are really are grim. Hope that the value returned by
2471 the gdbarch_sp_regnum register is meaningful. */
2472 if (gdbarch_sp_regnum (gdbarch
) >= 0)
2473 return get_frame_register_unsigned (this_frame
,
2474 gdbarch_sp_regnum (gdbarch
));
2475 internal_error (__FILE__
, __LINE__
, _("Missing unwind SP method"));
2478 /* Return the reason why we can't unwind past FRAME. */
2480 enum unwind_stop_reason
2481 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2483 /* If we haven't tried to unwind past this point yet, then assume
2484 that unwinding would succeed. */
2485 if (frame
->prev_p
== 0)
2486 return UNWIND_NO_REASON
;
2488 /* Otherwise, we set a reason when we succeeded (or failed) to
2490 return frame
->stop_reason
;
2493 /* Return a string explaining REASON. */
2496 frame_stop_reason_string (enum unwind_stop_reason reason
)
2500 #define SET(name, description) \
2501 case name: return _(description);
2502 #include "unwind_stop_reasons.def"
2506 internal_error (__FILE__
, __LINE__
,
2507 "Invalid frame stop reason");
2511 /* Return the enum symbol name of REASON as a string, to use in debug
2515 frame_stop_reason_symbol_string (enum unwind_stop_reason reason
)
2519 #define SET(name, description) \
2520 case name: return #name;
2521 #include "unwind_stop_reasons.def"
2525 internal_error (__FILE__
, __LINE__
,
2526 "Invalid frame stop reason");
2530 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2534 frame_cleanup_after_sniffer (void *arg
)
2536 struct frame_info
*frame
= arg
;
2538 /* The sniffer should not allocate a prologue cache if it did not
2539 match this frame. */
2540 gdb_assert (frame
->prologue_cache
== NULL
);
2542 /* No sniffer should extend the frame chain; sniff based on what is
2544 gdb_assert (!frame
->prev_p
);
2546 /* The sniffer should not check the frame's ID; that's circular. */
2547 gdb_assert (!frame
->this_id
.p
);
2549 /* Clear cached fields dependent on the unwinder.
2551 The previous PC is independent of the unwinder, but the previous
2552 function is not (see get_frame_address_in_block). */
2553 frame
->prev_func
.p
= 0;
2554 frame
->prev_func
.addr
= 0;
2556 /* Discard the unwinder last, so that we can easily find it if an assertion
2557 in this function triggers. */
2558 frame
->unwind
= NULL
;
2561 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2562 Return a cleanup which should be called if unwinding fails, and
2563 discarded if it succeeds. */
2566 frame_prepare_for_sniffer (struct frame_info
*frame
,
2567 const struct frame_unwind
*unwind
)
2569 gdb_assert (frame
->unwind
== NULL
);
2570 frame
->unwind
= unwind
;
2571 return make_cleanup (frame_cleanup_after_sniffer
, frame
);
2574 extern initialize_file_ftype _initialize_frame
; /* -Wmissing-prototypes */
2576 static struct cmd_list_element
*set_backtrace_cmdlist
;
2577 static struct cmd_list_element
*show_backtrace_cmdlist
;
2580 set_backtrace_cmd (char *args
, int from_tty
)
2582 help_list (set_backtrace_cmdlist
, "set backtrace ", -1, gdb_stdout
);
2586 show_backtrace_cmd (char *args
, int from_tty
)
2588 cmd_show_list (show_backtrace_cmdlist
, from_tty
, "");
2592 _initialize_frame (void)
2594 obstack_init (&frame_cache_obstack
);
2596 frame_stash_create ();
2598 observer_attach_target_changed (frame_observer_target_changed
);
2600 add_prefix_cmd ("backtrace", class_maintenance
, set_backtrace_cmd
, _("\
2601 Set backtrace specific variables.\n\
2602 Configure backtrace variables such as the backtrace limit"),
2603 &set_backtrace_cmdlist
, "set backtrace ",
2604 0/*allow-unknown*/, &setlist
);
2605 add_prefix_cmd ("backtrace", class_maintenance
, show_backtrace_cmd
, _("\
2606 Show backtrace specific variables\n\
2607 Show backtrace variables such as the backtrace limit"),
2608 &show_backtrace_cmdlist
, "show backtrace ",
2609 0/*allow-unknown*/, &showlist
);
2611 add_setshow_boolean_cmd ("past-main", class_obscure
,
2612 &backtrace_past_main
, _("\
2613 Set whether backtraces should continue past \"main\"."), _("\
2614 Show whether backtraces should continue past \"main\"."), _("\
2615 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2616 the backtrace at \"main\". Set this variable if you need to see the rest\n\
2617 of the stack trace."),
2619 show_backtrace_past_main
,
2620 &set_backtrace_cmdlist
,
2621 &show_backtrace_cmdlist
);
2623 add_setshow_boolean_cmd ("past-entry", class_obscure
,
2624 &backtrace_past_entry
, _("\
2625 Set whether backtraces should continue past the entry point of a program."),
2627 Show whether backtraces should continue past the entry point of a program."),
2629 Normally there are no callers beyond the entry point of a program, so GDB\n\
2630 will terminate the backtrace there. Set this variable if you need to see\n\
2631 the rest of the stack trace."),
2633 show_backtrace_past_entry
,
2634 &set_backtrace_cmdlist
,
2635 &show_backtrace_cmdlist
);
2637 add_setshow_uinteger_cmd ("limit", class_obscure
,
2638 &backtrace_limit
, _("\
2639 Set an upper bound on the number of backtrace levels."), _("\
2640 Show the upper bound on the number of backtrace levels."), _("\
2641 No more than the specified number of frames can be displayed or examined.\n\
2642 Literal \"unlimited\" or zero means no limit."),
2644 show_backtrace_limit
,
2645 &set_backtrace_cmdlist
,
2646 &show_backtrace_cmdlist
);
2648 /* Debug this files internals. */
2649 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
2650 Set frame debugging."), _("\
2651 Show frame debugging."), _("\
2652 When non-zero, frame specific internal debugging is enabled."),
2655 &setdebuglist
, &showdebuglist
);