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
);
52 /* We keep a cache of stack frames, each of which is a "struct
53 frame_info". The innermost one gets allocated (in
54 wait_for_inferior) each time the inferior stops; current_frame
55 points to it. Additional frames get allocated (in get_prev_frame)
56 as needed, and are chained through the next and prev fields. Any
57 time that the frame cache becomes invalid (most notably when we
58 execute something, but also if we change how we interpret the
59 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
60 which reads new symbols)), we should call reinit_frame_cache. */
64 /* Level of this frame. The inner-most (youngest) frame is at level
65 0. As you move towards the outer-most (oldest) frame, the level
66 increases. This is a cached value. It could just as easily be
67 computed by counting back from the selected frame to the inner
69 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
70 reserved to indicate a bogus frame - one that has been created
71 just to keep GDB happy (GDB always needs a frame). For the
72 moment leave this as speculation. */
75 /* The frame's program space. */
76 struct program_space
*pspace
;
78 /* The frame's address space. */
79 struct address_space
*aspace
;
81 /* The frame's low-level unwinder and corresponding cache. The
82 low-level unwinder is responsible for unwinding register values
83 for the previous frame. The low-level unwind methods are
84 selected based on the presence, or otherwise, of register unwind
85 information such as CFI. */
87 const struct frame_unwind
*unwind
;
89 /* Cached copy of the previous frame's architecture. */
96 /* Cached copy of the previous frame's resume address. */
102 /* Cached copy of the previous frame's function address. */
109 /* This frame's ID. */
113 struct frame_id value
;
116 /* The frame's high-level base methods, and corresponding cache.
117 The high level base methods are selected based on the frame's
119 const struct frame_base
*base
;
122 /* Pointers to the next (down, inner, younger) and previous (up,
123 outer, older) frame_info's in the frame cache. */
124 struct frame_info
*next
; /* down, inner, younger */
126 struct frame_info
*prev
; /* up, outer, older */
128 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
129 could. Only valid when PREV_P is set. */
130 enum unwind_stop_reason stop_reason
;
133 /* A frame stash used to speed up frame lookups. Create a hash table
134 to stash frames previously accessed from the frame cache for
135 quicker subsequent retrieval. The hash table is emptied whenever
136 the frame cache is invalidated. */
138 static htab_t frame_stash
;
140 /* Internal function to calculate a hash from the frame_id addresses,
141 using as many valid addresses as possible. Frames below level 0
142 are not stored in the hash table. */
145 frame_addr_hash (const void *ap
)
147 const struct frame_info
*frame
= ap
;
148 const struct frame_id f_id
= frame
->this_id
.value
;
151 gdb_assert (f_id
.stack_addr_p
|| f_id
.code_addr_p
152 || f_id
.special_addr_p
);
154 if (f_id
.stack_addr_p
)
155 hash
= iterative_hash (&f_id
.stack_addr
,
156 sizeof (f_id
.stack_addr
), hash
);
157 if (f_id
.code_addr_p
)
158 hash
= iterative_hash (&f_id
.code_addr
,
159 sizeof (f_id
.code_addr
), hash
);
160 if (f_id
.special_addr_p
)
161 hash
= iterative_hash (&f_id
.special_addr
,
162 sizeof (f_id
.special_addr
), hash
);
167 /* Internal equality function for the hash table. This function
168 defers equality operations to frame_id_eq. */
171 frame_addr_hash_eq (const void *a
, const void *b
)
173 const struct frame_info
*f_entry
= a
;
174 const struct frame_info
*f_element
= b
;
176 return frame_id_eq (f_entry
->this_id
.value
,
177 f_element
->this_id
.value
);
180 /* Internal function to create the frame_stash hash table. 100 seems
181 to be a good compromise to start the hash table at. */
184 frame_stash_create (void)
186 frame_stash
= htab_create (100,
192 /* Internal function to add a frame to the frame_stash hash table.
193 Returns false if a frame with the same ID was already stashed, true
197 frame_stash_add (struct frame_info
*frame
)
199 struct frame_info
**slot
;
201 /* Do not try to stash the sentinel frame. */
202 gdb_assert (frame
->level
>= 0);
204 slot
= (struct frame_info
**) htab_find_slot (frame_stash
,
208 /* If we already have a frame in the stack with the same id, we
209 either have a stack cycle (corrupted stack?), or some bug
210 elsewhere in GDB. In any case, ignore the duplicate and return
211 an indication to the caller. */
219 /* Internal function to search the frame stash for an entry with the
220 given frame ID. If found, return that frame. Otherwise return
223 static struct frame_info
*
224 frame_stash_find (struct frame_id id
)
226 struct frame_info dummy
;
227 struct frame_info
*frame
;
229 dummy
.this_id
.value
= id
;
230 frame
= htab_find (frame_stash
, &dummy
);
234 /* Internal function to invalidate the frame stash by removing all
235 entries in it. This only occurs when the frame cache is
239 frame_stash_invalidate (void)
241 htab_empty (frame_stash
);
244 /* Flag to control debugging. */
246 unsigned int frame_debug
;
248 show_frame_debug (struct ui_file
*file
, int from_tty
,
249 struct cmd_list_element
*c
, const char *value
)
251 fprintf_filtered (file
, _("Frame debugging is %s.\n"), value
);
254 /* Flag to indicate whether backtraces should stop at main et.al. */
256 static int backtrace_past_main
;
258 show_backtrace_past_main (struct ui_file
*file
, int from_tty
,
259 struct cmd_list_element
*c
, const char *value
)
261 fprintf_filtered (file
,
262 _("Whether backtraces should "
263 "continue past \"main\" is %s.\n"),
267 static int backtrace_past_entry
;
269 show_backtrace_past_entry (struct ui_file
*file
, int from_tty
,
270 struct cmd_list_element
*c
, const char *value
)
272 fprintf_filtered (file
, _("Whether backtraces should continue past the "
273 "entry point of a program is %s.\n"),
277 static unsigned int backtrace_limit
= UINT_MAX
;
279 show_backtrace_limit (struct ui_file
*file
, int from_tty
,
280 struct cmd_list_element
*c
, const char *value
)
282 fprintf_filtered (file
,
283 _("An upper bound on the number "
284 "of backtrace levels is %s.\n"),
290 fprint_field (struct ui_file
*file
, const char *name
, int p
, CORE_ADDR addr
)
293 fprintf_unfiltered (file
, "%s=%s", name
, hex_string (addr
));
295 fprintf_unfiltered (file
, "!%s", name
);
299 fprint_frame_id (struct ui_file
*file
, struct frame_id id
)
301 fprintf_unfiltered (file
, "{");
302 fprint_field (file
, "stack", id
.stack_addr_p
, id
.stack_addr
);
303 fprintf_unfiltered (file
, ",");
304 fprint_field (file
, "code", id
.code_addr_p
, id
.code_addr
);
305 fprintf_unfiltered (file
, ",");
306 fprint_field (file
, "special", id
.special_addr_p
, id
.special_addr
);
307 if (id
.artificial_depth
)
308 fprintf_unfiltered (file
, ",artificial=%d", id
.artificial_depth
);
309 fprintf_unfiltered (file
, "}");
313 fprint_frame_type (struct ui_file
*file
, enum frame_type type
)
318 fprintf_unfiltered (file
, "NORMAL_FRAME");
321 fprintf_unfiltered (file
, "DUMMY_FRAME");
324 fprintf_unfiltered (file
, "INLINE_FRAME");
327 fprintf_unfiltered (file
, "TAILCALL_FRAME");
330 fprintf_unfiltered (file
, "SIGTRAMP_FRAME");
333 fprintf_unfiltered (file
, "ARCH_FRAME");
336 fprintf_unfiltered (file
, "SENTINEL_FRAME");
339 fprintf_unfiltered (file
, "<unknown type>");
345 fprint_frame (struct ui_file
*file
, struct frame_info
*fi
)
349 fprintf_unfiltered (file
, "<NULL frame>");
352 fprintf_unfiltered (file
, "{");
353 fprintf_unfiltered (file
, "level=%d", fi
->level
);
354 fprintf_unfiltered (file
, ",");
355 fprintf_unfiltered (file
, "type=");
356 if (fi
->unwind
!= NULL
)
357 fprint_frame_type (file
, fi
->unwind
->type
);
359 fprintf_unfiltered (file
, "<unknown>");
360 fprintf_unfiltered (file
, ",");
361 fprintf_unfiltered (file
, "unwind=");
362 if (fi
->unwind
!= NULL
)
363 gdb_print_host_address (fi
->unwind
, file
);
365 fprintf_unfiltered (file
, "<unknown>");
366 fprintf_unfiltered (file
, ",");
367 fprintf_unfiltered (file
, "pc=");
368 if (fi
->next
!= NULL
&& fi
->next
->prev_pc
.p
)
369 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_pc
.value
));
371 fprintf_unfiltered (file
, "<unknown>");
372 fprintf_unfiltered (file
, ",");
373 fprintf_unfiltered (file
, "id=");
375 fprint_frame_id (file
, fi
->this_id
.value
);
377 fprintf_unfiltered (file
, "<unknown>");
378 fprintf_unfiltered (file
, ",");
379 fprintf_unfiltered (file
, "func=");
380 if (fi
->next
!= NULL
&& fi
->next
->prev_func
.p
)
381 fprintf_unfiltered (file
, "%s", hex_string (fi
->next
->prev_func
.addr
));
383 fprintf_unfiltered (file
, "<unknown>");
384 fprintf_unfiltered (file
, "}");
387 /* Given FRAME, return the enclosing frame as found in real frames read-in from
388 inferior memory. Skip any previous frames which were made up by GDB.
389 Return the original frame if no immediate previous frames exist. */
391 static struct frame_info
*
392 skip_artificial_frames (struct frame_info
*frame
)
394 while (get_frame_type (frame
) == INLINE_FRAME
395 || get_frame_type (frame
) == TAILCALL_FRAME
)
396 frame
= get_prev_frame (frame
);
401 /* Compute the frame's uniq ID that can be used to, later, re-find the
405 compute_frame_id (struct frame_info
*fi
)
407 gdb_assert (!fi
->this_id
.p
);
410 fprintf_unfiltered (gdb_stdlog
, "{ compute_frame_id (fi=%d) ",
412 /* Find the unwinder. */
413 if (fi
->unwind
== NULL
)
414 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
415 /* Find THIS frame's ID. */
416 /* Default to outermost if no ID is found. */
417 fi
->this_id
.value
= outer_frame_id
;
418 fi
->unwind
->this_id (fi
, &fi
->prologue_cache
, &fi
->this_id
.value
);
419 gdb_assert (frame_id_p (fi
->this_id
.value
));
423 fprintf_unfiltered (gdb_stdlog
, "-> ");
424 fprint_frame_id (gdb_stdlog
, fi
->this_id
.value
);
425 fprintf_unfiltered (gdb_stdlog
, " }\n");
429 /* Return a frame uniq ID that can be used to, later, re-find the
433 get_frame_id (struct frame_info
*fi
)
436 return null_frame_id
;
438 gdb_assert (fi
->this_id
.p
);
439 return fi
->this_id
.value
;
443 get_stack_frame_id (struct frame_info
*next_frame
)
445 return get_frame_id (skip_artificial_frames (next_frame
));
449 frame_unwind_caller_id (struct frame_info
*next_frame
)
451 struct frame_info
*this_frame
;
453 /* Use get_prev_frame_1, and not get_prev_frame. The latter will truncate
454 the frame chain, leading to this function unintentionally
455 returning a null_frame_id (e.g., when a caller requests the frame
456 ID of "main()"s caller. */
458 next_frame
= skip_artificial_frames (next_frame
);
459 this_frame
= get_prev_frame_1 (next_frame
);
461 return get_frame_id (skip_artificial_frames (this_frame
));
463 return null_frame_id
;
466 const struct frame_id null_frame_id
; /* All zeros. */
467 const struct frame_id outer_frame_id
= { 0, 0, 0, 0, 0, 1, 0 };
470 frame_id_build_special (CORE_ADDR stack_addr
, CORE_ADDR code_addr
,
471 CORE_ADDR special_addr
)
473 struct frame_id id
= null_frame_id
;
475 id
.stack_addr
= stack_addr
;
477 id
.code_addr
= code_addr
;
479 id
.special_addr
= special_addr
;
480 id
.special_addr_p
= 1;
485 frame_id_build (CORE_ADDR stack_addr
, CORE_ADDR code_addr
)
487 struct frame_id id
= null_frame_id
;
489 id
.stack_addr
= stack_addr
;
491 id
.code_addr
= code_addr
;
497 frame_id_build_wild (CORE_ADDR stack_addr
)
499 struct frame_id id
= null_frame_id
;
501 id
.stack_addr
= stack_addr
;
507 frame_id_p (struct frame_id l
)
511 /* The frame is valid iff it has a valid stack address. */
513 /* outer_frame_id is also valid. */
514 if (!p
&& memcmp (&l
, &outer_frame_id
, sizeof (l
)) == 0)
518 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_p (l=");
519 fprint_frame_id (gdb_stdlog
, l
);
520 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", p
);
526 frame_id_artificial_p (struct frame_id l
)
531 return (l
.artificial_depth
!= 0);
535 frame_id_eq (struct frame_id l
, struct frame_id r
)
539 if (!l
.stack_addr_p
&& l
.special_addr_p
540 && !r
.stack_addr_p
&& r
.special_addr_p
)
541 /* The outermost frame marker is equal to itself. This is the
542 dodgy thing about outer_frame_id, since between execution steps
543 we might step into another function - from which we can't
544 unwind either. More thought required to get rid of
547 else if (!l
.stack_addr_p
|| !r
.stack_addr_p
)
548 /* Like a NaN, if either ID is invalid, the result is false.
549 Note that a frame ID is invalid iff it is the null frame ID. */
551 else if (l
.stack_addr
!= r
.stack_addr
)
552 /* If .stack addresses are different, the frames are different. */
554 else if (l
.code_addr_p
&& r
.code_addr_p
&& l
.code_addr
!= r
.code_addr
)
555 /* An invalid code addr is a wild card. If .code addresses are
556 different, the frames are different. */
558 else if (l
.special_addr_p
&& r
.special_addr_p
559 && l
.special_addr
!= r
.special_addr
)
560 /* An invalid special addr is a wild card (or unused). Otherwise
561 if special addresses are different, the frames are different. */
563 else if (l
.artificial_depth
!= r
.artificial_depth
)
564 /* If artifical depths are different, the frames must be different. */
567 /* Frames are equal. */
572 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_eq (l=");
573 fprint_frame_id (gdb_stdlog
, l
);
574 fprintf_unfiltered (gdb_stdlog
, ",r=");
575 fprint_frame_id (gdb_stdlog
, r
);
576 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", eq
);
581 /* Safety net to check whether frame ID L should be inner to
582 frame ID R, according to their stack addresses.
584 This method cannot be used to compare arbitrary frames, as the
585 ranges of valid stack addresses may be discontiguous (e.g. due
588 However, it can be used as safety net to discover invalid frame
589 IDs in certain circumstances. Assuming that NEXT is the immediate
590 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
592 * The stack address of NEXT must be inner-than-or-equal to the stack
595 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
598 * If NEXT and THIS have different stack addresses, no other frame
599 in the frame chain may have a stack address in between.
601 Therefore, if frame_id_inner (TEST, THIS) holds, but
602 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
603 to a valid frame in the frame chain.
605 The sanity checks above cannot be performed when a SIGTRAMP frame
606 is involved, because signal handlers might be executed on a different
607 stack than the stack used by the routine that caused the signal
608 to be raised. This can happen for instance when a thread exceeds
609 its maximum stack size. In this case, certain compilers implement
610 a stack overflow strategy that cause the handler to be run on a
614 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
618 if (!l
.stack_addr_p
|| !r
.stack_addr_p
)
619 /* Like NaN, any operation involving an invalid ID always fails. */
621 else if (l
.artificial_depth
> r
.artificial_depth
622 && l
.stack_addr
== r
.stack_addr
623 && l
.code_addr_p
== r
.code_addr_p
624 && l
.special_addr_p
== r
.special_addr_p
625 && l
.special_addr
== r
.special_addr
)
627 /* Same function, different inlined functions. */
628 struct block
*lb
, *rb
;
630 gdb_assert (l
.code_addr_p
&& r
.code_addr_p
);
632 lb
= block_for_pc (l
.code_addr
);
633 rb
= block_for_pc (r
.code_addr
);
635 if (lb
== NULL
|| rb
== NULL
)
636 /* Something's gone wrong. */
639 /* This will return true if LB and RB are the same block, or
640 if the block with the smaller depth lexically encloses the
641 block with the greater depth. */
642 inner
= contained_in (lb
, rb
);
645 /* Only return non-zero when strictly inner than. Note that, per
646 comment in "frame.h", there is some fuzz here. Frameless
647 functions are not strictly inner than (same .stack but
648 different .code and/or .special address). */
649 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
652 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
653 fprint_frame_id (gdb_stdlog
, l
);
654 fprintf_unfiltered (gdb_stdlog
, ",r=");
655 fprint_frame_id (gdb_stdlog
, r
);
656 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
662 frame_find_by_id (struct frame_id id
)
664 struct frame_info
*frame
, *prev_frame
;
666 /* ZERO denotes the null frame, let the caller decide what to do
667 about it. Should it instead return get_current_frame()? */
668 if (!frame_id_p (id
))
671 /* Try using the frame stash first. Finding it there removes the need
672 to perform the search by looping over all frames, which can be very
673 CPU-intensive if the number of frames is very high (the loop is O(n)
674 and get_prev_frame performs a series of checks that are relatively
675 expensive). This optimization is particularly useful when this function
676 is called from another function (such as value_fetch_lazy, case
677 VALUE_LVAL (val) == lval_register) which already loops over all frames,
678 making the overall behavior O(n^2). */
679 frame
= frame_stash_find (id
);
683 for (frame
= get_current_frame (); ; frame
= prev_frame
)
685 struct frame_id
this = get_frame_id (frame
);
687 if (frame_id_eq (id
, this))
688 /* An exact match. */
691 prev_frame
= get_prev_frame (frame
);
695 /* As a safety net to avoid unnecessary backtracing while trying
696 to find an invalid ID, we check for a common situation where
697 we can detect from comparing stack addresses that no other
698 frame in the current frame chain can have this ID. See the
699 comment at frame_id_inner for details. */
700 if (get_frame_type (frame
) == NORMAL_FRAME
701 && !frame_id_inner (get_frame_arch (frame
), id
, this)
702 && frame_id_inner (get_frame_arch (prev_frame
), id
,
703 get_frame_id (prev_frame
)))
710 frame_unwind_pc_if_available (struct frame_info
*this_frame
, CORE_ADDR
*pc
)
712 if (!this_frame
->prev_pc
.p
)
714 if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame
)))
716 volatile struct gdb_exception ex
;
717 struct gdbarch
*prev_gdbarch
;
720 /* The right way. The `pure' way. The one true way. This
721 method depends solely on the register-unwind code to
722 determine the value of registers in THIS frame, and hence
723 the value of this frame's PC (resume address). A typical
724 implementation is no more than:
726 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
727 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
729 Note: this method is very heavily dependent on a correct
730 register-unwind implementation, it pays to fix that
731 method first; this method is frame type agnostic, since
732 it only deals with register values, it works with any
733 frame. This is all in stark contrast to the old
734 FRAME_SAVED_PC which would try to directly handle all the
735 different ways that a PC could be unwound. */
736 prev_gdbarch
= frame_unwind_arch (this_frame
);
738 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
740 pc
= gdbarch_unwind_pc (prev_gdbarch
, this_frame
);
742 if (ex
.reason
< 0 && ex
.error
== NOT_AVAILABLE_ERROR
)
744 this_frame
->prev_pc
.p
= -1;
747 fprintf_unfiltered (gdb_stdlog
,
748 "{ frame_unwind_pc (this_frame=%d)"
749 " -> <unavailable> }\n",
752 else if (ex
.reason
< 0)
754 throw_exception (ex
);
758 this_frame
->prev_pc
.value
= pc
;
759 this_frame
->prev_pc
.p
= 1;
761 fprintf_unfiltered (gdb_stdlog
,
762 "{ frame_unwind_pc (this_frame=%d) "
765 hex_string (this_frame
->prev_pc
.value
));
769 internal_error (__FILE__
, __LINE__
, _("No unwind_pc method"));
771 if (this_frame
->prev_pc
.p
< 0)
778 *pc
= this_frame
->prev_pc
.value
;
784 frame_unwind_pc (struct frame_info
*this_frame
)
788 if (!frame_unwind_pc_if_available (this_frame
, &pc
))
789 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
795 frame_unwind_caller_pc (struct frame_info
*this_frame
)
797 return frame_unwind_pc (skip_artificial_frames (this_frame
));
801 frame_unwind_caller_pc_if_available (struct frame_info
*this_frame
,
804 return frame_unwind_pc_if_available (skip_artificial_frames (this_frame
), pc
);
808 get_frame_func_if_available (struct frame_info
*this_frame
, CORE_ADDR
*pc
)
810 struct frame_info
*next_frame
= this_frame
->next
;
812 if (!next_frame
->prev_func
.p
)
814 CORE_ADDR addr_in_block
;
816 /* Make certain that this, and not the adjacent, function is
818 if (!get_frame_address_in_block_if_available (this_frame
, &addr_in_block
))
820 next_frame
->prev_func
.p
= -1;
822 fprintf_unfiltered (gdb_stdlog
,
823 "{ get_frame_func (this_frame=%d)"
824 " -> unavailable }\n",
829 next_frame
->prev_func
.p
= 1;
830 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
832 fprintf_unfiltered (gdb_stdlog
,
833 "{ get_frame_func (this_frame=%d) -> %s }\n",
835 hex_string (next_frame
->prev_func
.addr
));
839 if (next_frame
->prev_func
.p
< 0)
846 *pc
= next_frame
->prev_func
.addr
;
852 get_frame_func (struct frame_info
*this_frame
)
856 if (!get_frame_func_if_available (this_frame
, &pc
))
857 throw_error (NOT_AVAILABLE_ERROR
, _("PC not available"));
862 static enum register_status
863 do_frame_register_read (void *src
, int regnum
, gdb_byte
*buf
)
865 if (!deprecated_frame_register_read (src
, regnum
, buf
))
866 return REG_UNAVAILABLE
;
872 frame_save_as_regcache (struct frame_info
*this_frame
)
874 struct address_space
*aspace
= get_frame_address_space (this_frame
);
875 struct regcache
*regcache
= regcache_xmalloc (get_frame_arch (this_frame
),
877 struct cleanup
*cleanups
= make_cleanup_regcache_xfree (regcache
);
879 regcache_save (regcache
, do_frame_register_read
, this_frame
);
880 discard_cleanups (cleanups
);
885 frame_pop (struct frame_info
*this_frame
)
887 struct frame_info
*prev_frame
;
888 struct regcache
*scratch
;
889 struct cleanup
*cleanups
;
891 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
893 /* Popping a dummy frame involves restoring more than just registers.
894 dummy_frame_pop does all the work. */
895 dummy_frame_pop (get_frame_id (this_frame
));
899 /* Ensure that we have a frame to pop to. */
900 prev_frame
= get_prev_frame_1 (this_frame
);
903 error (_("Cannot pop the initial frame."));
905 /* Ignore TAILCALL_FRAME type frames, they were executed already before
906 entering THISFRAME. */
907 while (get_frame_type (prev_frame
) == TAILCALL_FRAME
)
908 prev_frame
= get_prev_frame (prev_frame
);
910 /* Make a copy of all the register values unwound from this frame.
911 Save them in a scratch buffer so that there isn't a race between
912 trying to extract the old values from the current regcache while
913 at the same time writing new values into that same cache. */
914 scratch
= frame_save_as_regcache (prev_frame
);
915 cleanups
= make_cleanup_regcache_xfree (scratch
);
917 /* FIXME: cagney/2003-03-16: It should be possible to tell the
918 target's register cache that it is about to be hit with a burst
919 register transfer and that the sequence of register writes should
920 be batched. The pair target_prepare_to_store() and
921 target_store_registers() kind of suggest this functionality.
922 Unfortunately, they don't implement it. Their lack of a formal
923 definition can lead to targets writing back bogus values
924 (arguably a bug in the target code mind). */
925 /* Now copy those saved registers into the current regcache.
926 Here, regcache_cpy() calls regcache_restore(). */
927 regcache_cpy (get_current_regcache (), scratch
);
928 do_cleanups (cleanups
);
930 /* We've made right mess of GDB's local state, just discard
932 reinit_frame_cache ();
936 frame_register_unwind (struct frame_info
*frame
, int regnum
,
937 int *optimizedp
, int *unavailablep
,
938 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
939 int *realnump
, gdb_byte
*bufferp
)
943 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
944 that the value proper does not need to be fetched. */
945 gdb_assert (optimizedp
!= NULL
);
946 gdb_assert (lvalp
!= NULL
);
947 gdb_assert (addrp
!= NULL
);
948 gdb_assert (realnump
!= NULL
);
949 /* gdb_assert (bufferp != NULL); */
951 value
= frame_unwind_register_value (frame
, regnum
);
953 gdb_assert (value
!= NULL
);
955 *optimizedp
= value_optimized_out (value
);
956 *unavailablep
= !value_entirely_available (value
);
957 *lvalp
= VALUE_LVAL (value
);
958 *addrp
= value_address (value
);
959 *realnump
= VALUE_REGNUM (value
);
963 if (!*optimizedp
&& !*unavailablep
)
964 memcpy (bufferp
, value_contents_all (value
),
965 TYPE_LENGTH (value_type (value
)));
967 memset (bufferp
, 0, TYPE_LENGTH (value_type (value
)));
970 /* Dispose of the new value. This prevents watchpoints from
971 trying to watch the saved frame pointer. */
972 release_value (value
);
977 frame_register (struct frame_info
*frame
, int regnum
,
978 int *optimizedp
, int *unavailablep
, enum lval_type
*lvalp
,
979 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
981 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
982 that the value proper does not need to be fetched. */
983 gdb_assert (optimizedp
!= NULL
);
984 gdb_assert (lvalp
!= NULL
);
985 gdb_assert (addrp
!= NULL
);
986 gdb_assert (realnump
!= NULL
);
987 /* gdb_assert (bufferp != NULL); */
989 /* Obtain the register value by unwinding the register from the next
990 (more inner frame). */
991 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
992 frame_register_unwind (frame
->next
, regnum
, optimizedp
, unavailablep
,
993 lvalp
, addrp
, realnump
, bufferp
);
997 frame_unwind_register (struct frame_info
*frame
, int regnum
, gdb_byte
*buf
)
1003 enum lval_type lval
;
1005 frame_register_unwind (frame
, regnum
, &optimized
, &unavailable
,
1006 &lval
, &addr
, &realnum
, buf
);
1009 error (_("Register %d was not saved"), regnum
);
1011 throw_error (NOT_AVAILABLE_ERROR
,
1012 _("Register %d is not available"), regnum
);
1016 get_frame_register (struct frame_info
*frame
,
1017 int regnum
, gdb_byte
*buf
)
1019 frame_unwind_register (frame
->next
, regnum
, buf
);
1023 frame_unwind_register_value (struct frame_info
*frame
, int regnum
)
1025 struct gdbarch
*gdbarch
;
1026 struct value
*value
;
1028 gdb_assert (frame
!= NULL
);
1029 gdbarch
= frame_unwind_arch (frame
);
1033 fprintf_unfiltered (gdb_stdlog
,
1034 "{ frame_unwind_register_value "
1035 "(frame=%d,regnum=%d(%s),...) ",
1036 frame
->level
, regnum
,
1037 user_reg_map_regnum_to_name (gdbarch
, regnum
));
1040 /* Find the unwinder. */
1041 if (frame
->unwind
== NULL
)
1042 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
1044 /* Ask this frame to unwind its register. */
1045 value
= frame
->unwind
->prev_register (frame
, &frame
->prologue_cache
, regnum
);
1049 fprintf_unfiltered (gdb_stdlog
, "->");
1050 if (value_optimized_out (value
))
1052 fprintf_unfiltered (gdb_stdlog
, " ");
1053 val_print_optimized_out (value
, gdb_stdlog
);
1057 if (VALUE_LVAL (value
) == lval_register
)
1058 fprintf_unfiltered (gdb_stdlog
, " register=%d",
1059 VALUE_REGNUM (value
));
1060 else if (VALUE_LVAL (value
) == lval_memory
)
1061 fprintf_unfiltered (gdb_stdlog
, " address=%s",
1063 value_address (value
)));
1065 fprintf_unfiltered (gdb_stdlog
, " computed");
1067 if (value_lazy (value
))
1068 fprintf_unfiltered (gdb_stdlog
, " lazy");
1072 const gdb_byte
*buf
= value_contents (value
);
1074 fprintf_unfiltered (gdb_stdlog
, " bytes=");
1075 fprintf_unfiltered (gdb_stdlog
, "[");
1076 for (i
= 0; i
< register_size (gdbarch
, regnum
); i
++)
1077 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1078 fprintf_unfiltered (gdb_stdlog
, "]");
1082 fprintf_unfiltered (gdb_stdlog
, " }\n");
1089 get_frame_register_value (struct frame_info
*frame
, int regnum
)
1091 return frame_unwind_register_value (frame
->next
, regnum
);
1095 frame_unwind_register_signed (struct frame_info
*frame
, int regnum
)
1097 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
1098 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1099 int size
= register_size (gdbarch
, regnum
);
1100 gdb_byte buf
[MAX_REGISTER_SIZE
];
1102 frame_unwind_register (frame
, regnum
, buf
);
1103 return extract_signed_integer (buf
, size
, byte_order
);
1107 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
1109 return frame_unwind_register_signed (frame
->next
, regnum
);
1113 frame_unwind_register_unsigned (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_unsigned_integer (buf
, size
, byte_order
);
1125 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
1127 return frame_unwind_register_unsigned (frame
->next
, regnum
);
1131 read_frame_register_unsigned (struct frame_info
*frame
, int regnum
,
1134 struct value
*regval
= get_frame_register_value (frame
, regnum
);
1136 if (!value_optimized_out (regval
)
1137 && value_entirely_available (regval
))
1139 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1140 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1141 int size
= register_size (gdbarch
, VALUE_REGNUM (regval
));
1143 *val
= extract_unsigned_integer (value_contents (regval
), size
, byte_order
);
1151 put_frame_register (struct frame_info
*frame
, int regnum
,
1152 const gdb_byte
*buf
)
1154 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1158 enum lval_type lval
;
1161 frame_register (frame
, regnum
, &optim
, &unavail
,
1162 &lval
, &addr
, &realnum
, NULL
);
1164 error (_("Attempt to assign to a register that was not saved."));
1169 write_memory (addr
, buf
, register_size (gdbarch
, regnum
));
1173 regcache_cooked_write (get_current_regcache (), realnum
, buf
);
1176 error (_("Attempt to assign to an unmodifiable value."));
1180 /* This function is deprecated. Use get_frame_register_value instead,
1181 which provides more accurate information.
1183 Find and return the value of REGNUM for the specified stack frame.
1184 The number of bytes copied is REGISTER_SIZE (REGNUM).
1186 Returns 0 if the register value could not be found. */
1189 deprecated_frame_register_read (struct frame_info
*frame
, int regnum
,
1194 enum lval_type lval
;
1198 frame_register (frame
, regnum
, &optimized
, &unavailable
,
1199 &lval
, &addr
, &realnum
, myaddr
);
1201 return !optimized
&& !unavailable
;
1205 get_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1206 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
,
1207 int *optimizedp
, int *unavailablep
)
1209 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1214 /* Skip registers wholly inside of OFFSET. */
1215 while (offset
>= register_size (gdbarch
, regnum
))
1217 offset
-= register_size (gdbarch
, regnum
);
1221 /* Ensure that we will not read beyond the end of the register file.
1222 This can only ever happen if the debug information is bad. */
1224 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1225 for (i
= regnum
; i
< numregs
; i
++)
1227 int thissize
= register_size (gdbarch
, i
);
1230 break; /* This register is not available on this architecture. */
1231 maxsize
+= thissize
;
1234 error (_("Bad debug information detected: "
1235 "Attempt to read %d bytes from registers."), len
);
1237 /* Copy the data. */
1240 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1245 if (curr_len
== register_size (gdbarch
, regnum
))
1247 enum lval_type lval
;
1251 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1252 &lval
, &addr
, &realnum
, myaddr
);
1253 if (*optimizedp
|| *unavailablep
)
1258 gdb_byte buf
[MAX_REGISTER_SIZE
];
1259 enum lval_type lval
;
1263 frame_register (frame
, regnum
, optimizedp
, unavailablep
,
1264 &lval
, &addr
, &realnum
, buf
);
1265 if (*optimizedp
|| *unavailablep
)
1267 memcpy (myaddr
, buf
+ offset
, curr_len
);
1282 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
1283 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
1285 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1287 /* Skip registers wholly inside of OFFSET. */
1288 while (offset
>= register_size (gdbarch
, regnum
))
1290 offset
-= register_size (gdbarch
, regnum
);
1294 /* Copy the data. */
1297 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
1302 if (curr_len
== register_size (gdbarch
, regnum
))
1304 put_frame_register (frame
, regnum
, myaddr
);
1308 gdb_byte buf
[MAX_REGISTER_SIZE
];
1310 deprecated_frame_register_read (frame
, regnum
, buf
);
1311 memcpy (buf
+ offset
, myaddr
, curr_len
);
1312 put_frame_register (frame
, regnum
, buf
);
1322 /* Create a sentinel frame. */
1324 static struct frame_info
*
1325 create_sentinel_frame (struct program_space
*pspace
, struct regcache
*regcache
)
1327 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1330 frame
->pspace
= pspace
;
1331 frame
->aspace
= get_regcache_aspace (regcache
);
1332 /* Explicitly initialize the sentinel frame's cache. Provide it
1333 with the underlying regcache. In the future additional
1334 information, such as the frame's thread will be added. */
1335 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
1336 /* For the moment there is only one sentinel frame implementation. */
1337 frame
->unwind
= &sentinel_frame_unwind
;
1338 /* Link this frame back to itself. The frame is self referential
1339 (the unwound PC is the same as the pc), so make it so. */
1340 frame
->next
= frame
;
1341 /* Make the sentinel frame's ID valid, but invalid. That way all
1342 comparisons with it should fail. */
1343 frame
->this_id
.p
= 1;
1344 frame
->this_id
.value
= null_frame_id
;
1347 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
1348 fprint_frame (gdb_stdlog
, frame
);
1349 fprintf_unfiltered (gdb_stdlog
, " }\n");
1354 /* Info about the innermost stack frame (contents of FP register). */
1356 static struct frame_info
*current_frame
;
1358 /* Cache for frame addresses already read by gdb. Valid only while
1359 inferior is stopped. Control variables for the frame cache should
1360 be local to this module. */
1362 static struct obstack frame_cache_obstack
;
1365 frame_obstack_zalloc (unsigned long size
)
1367 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
1369 memset (data
, 0, size
);
1373 /* Return the innermost (currently executing) stack frame. This is
1374 split into two functions. The function unwind_to_current_frame()
1375 is wrapped in catch exceptions so that, even when the unwind of the
1376 sentinel frame fails, the function still returns a stack frame. */
1379 unwind_to_current_frame (struct ui_out
*ui_out
, void *args
)
1381 struct frame_info
*frame
= get_prev_frame (args
);
1383 /* A sentinel frame can fail to unwind, e.g., because its PC value
1384 lands in somewhere like start. */
1387 current_frame
= frame
;
1392 get_current_frame (void)
1394 /* First check, and report, the lack of registers. Having GDB
1395 report "No stack!" or "No memory" when the target doesn't even
1396 have registers is very confusing. Besides, "printcmd.exp"
1397 explicitly checks that ``print $pc'' with no registers prints "No
1399 if (!target_has_registers
)
1400 error (_("No registers."));
1401 if (!target_has_stack
)
1402 error (_("No stack."));
1403 if (!target_has_memory
)
1404 error (_("No memory."));
1405 /* Traceframes are effectively a substitute for the live inferior. */
1406 if (get_traceframe_number () < 0)
1408 if (ptid_equal (inferior_ptid
, null_ptid
))
1409 error (_("No selected thread."));
1410 if (is_exited (inferior_ptid
))
1411 error (_("Invalid selected thread."));
1412 if (is_executing (inferior_ptid
))
1413 error (_("Target is executing."));
1416 if (current_frame
== NULL
)
1418 struct frame_info
*sentinel_frame
=
1419 create_sentinel_frame (current_program_space
, get_current_regcache ());
1420 if (catch_exceptions (current_uiout
, unwind_to_current_frame
,
1421 sentinel_frame
, RETURN_MASK_ERROR
) != 0)
1423 /* Oops! Fake a current frame? Is this useful? It has a PC
1424 of zero, for instance. */
1425 current_frame
= sentinel_frame
;
1428 return current_frame
;
1431 /* The "selected" stack frame is used by default for local and arg
1432 access. May be zero, for no selected frame. */
1434 static struct frame_info
*selected_frame
;
1437 has_stack_frames (void)
1439 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1442 /* Traceframes are effectively a substitute for the live inferior. */
1443 if (get_traceframe_number () < 0)
1445 /* No current inferior, no frame. */
1446 if (ptid_equal (inferior_ptid
, null_ptid
))
1449 /* Don't try to read from a dead thread. */
1450 if (is_exited (inferior_ptid
))
1453 /* ... or from a spinning thread. */
1454 if (is_executing (inferior_ptid
))
1461 /* Return the selected frame. Always non-NULL (unless there isn't an
1462 inferior sufficient for creating a frame) in which case an error is
1466 get_selected_frame (const char *message
)
1468 if (selected_frame
== NULL
)
1470 if (message
!= NULL
&& !has_stack_frames ())
1471 error (("%s"), message
);
1472 /* Hey! Don't trust this. It should really be re-finding the
1473 last selected frame of the currently selected thread. This,
1474 though, is better than nothing. */
1475 select_frame (get_current_frame ());
1477 /* There is always a frame. */
1478 gdb_assert (selected_frame
!= NULL
);
1479 return selected_frame
;
1482 /* If there is a selected frame, return it. Otherwise, return NULL. */
1485 get_selected_frame_if_set (void)
1487 return selected_frame
;
1490 /* This is a variant of get_selected_frame() which can be called when
1491 the inferior does not have a frame; in that case it will return
1492 NULL instead of calling error(). */
1495 deprecated_safe_get_selected_frame (void)
1497 if (!has_stack_frames ())
1499 return get_selected_frame (NULL
);
1502 /* Select frame FI (or NULL - to invalidate the current frame). */
1505 select_frame (struct frame_info
*fi
)
1507 selected_frame
= fi
;
1508 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1509 frame is being invalidated. */
1510 if (deprecated_selected_frame_level_changed_hook
)
1511 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi
));
1513 /* FIXME: kseitz/2002-08-28: It would be nice to call
1514 selected_frame_level_changed_event() right here, but due to limitations
1515 in the current interfaces, we would end up flooding UIs with events
1516 because select_frame() is used extensively internally.
1518 Once we have frame-parameterized frame (and frame-related) commands,
1519 the event notification can be moved here, since this function will only
1520 be called when the user's selected frame is being changed. */
1522 /* Ensure that symbols for this frame are read in. Also, determine the
1523 source language of this frame, and switch to it if desired. */
1528 /* We retrieve the frame's symtab by using the frame PC.
1529 However we cannot use the frame PC as-is, because it usually
1530 points to the instruction following the "call", which is
1531 sometimes the first instruction of another function. So we
1532 rely on get_frame_address_in_block() which provides us with a
1533 PC which is guaranteed to be inside the frame's code
1535 if (get_frame_address_in_block_if_available (fi
, &pc
))
1537 struct symtab
*s
= find_pc_symtab (pc
);
1540 && s
->language
!= current_language
->la_language
1541 && s
->language
!= language_unknown
1542 && language_mode
== language_mode_auto
)
1543 set_language (s
->language
);
1548 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1549 Always returns a non-NULL value. */
1552 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1554 struct frame_info
*fi
;
1558 fprintf_unfiltered (gdb_stdlog
,
1559 "{ create_new_frame (addr=%s, pc=%s) ",
1560 hex_string (addr
), hex_string (pc
));
1563 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1565 fi
->next
= create_sentinel_frame (current_program_space
,
1566 get_current_regcache ());
1568 /* Set/update this frame's cached PC value, found in the next frame.
1569 Do this before looking for this frame's unwinder. A sniffer is
1570 very likely to read this, and the corresponding unwinder is
1571 entitled to rely that the PC doesn't magically change. */
1572 fi
->next
->prev_pc
.value
= pc
;
1573 fi
->next
->prev_pc
.p
= 1;
1575 /* We currently assume that frame chain's can't cross spaces. */
1576 fi
->pspace
= fi
->next
->pspace
;
1577 fi
->aspace
= fi
->next
->aspace
;
1579 /* Select/initialize both the unwind function and the frame's type
1581 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1584 fi
->this_id
.value
= frame_id_build (addr
, pc
);
1588 fprintf_unfiltered (gdb_stdlog
, "-> ");
1589 fprint_frame (gdb_stdlog
, fi
);
1590 fprintf_unfiltered (gdb_stdlog
, " }\n");
1596 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1597 innermost frame). Be careful to not fall off the bottom of the
1598 frame chain and onto the sentinel frame. */
1601 get_next_frame (struct frame_info
*this_frame
)
1603 if (this_frame
->level
> 0)
1604 return this_frame
->next
;
1609 /* Observer for the target_changed event. */
1612 frame_observer_target_changed (struct target_ops
*target
)
1614 reinit_frame_cache ();
1617 /* Flush the entire frame cache. */
1620 reinit_frame_cache (void)
1622 struct frame_info
*fi
;
1624 /* Tear down all frame caches. */
1625 for (fi
= current_frame
; fi
!= NULL
; fi
= fi
->prev
)
1627 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1628 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1629 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1630 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1633 /* Since we can't really be sure what the first object allocated was. */
1634 obstack_free (&frame_cache_obstack
, 0);
1635 obstack_init (&frame_cache_obstack
);
1637 if (current_frame
!= NULL
)
1638 annotate_frames_invalid ();
1640 current_frame
= NULL
; /* Invalidate cache */
1641 select_frame (NULL
);
1642 frame_stash_invalidate ();
1644 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1647 /* Find where a register is saved (in memory or another register).
1648 The result of frame_register_unwind is just where it is saved
1649 relative to this particular frame. */
1652 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1653 int *optimizedp
, enum lval_type
*lvalp
,
1654 CORE_ADDR
*addrp
, int *realnump
)
1656 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1658 while (this_frame
!= NULL
)
1662 frame_register_unwind (this_frame
, regnum
, optimizedp
, &unavailable
,
1663 lvalp
, addrp
, realnump
, NULL
);
1668 if (*lvalp
!= lval_register
)
1672 this_frame
= get_next_frame (this_frame
);
1676 /* Get the previous raw frame, and check that it is not identical to
1677 same other frame frame already in the chain. If it is, there is
1678 most likely a stack cycle, so we discard it, and mark THIS_FRAME as
1679 outermost, with UNWIND_SAME_ID stop reason. Unlike the other
1680 validity tests, that compare THIS_FRAME and the next frame, we do
1681 this right after creating the previous frame, to avoid ever ending
1682 up with two frames with the same id in the frame chain. */
1684 static struct frame_info
*
1685 get_prev_frame_if_no_cycle (struct frame_info
*this_frame
)
1687 struct frame_info
*prev_frame
;
1689 prev_frame
= get_prev_frame_raw (this_frame
);
1690 if (prev_frame
== NULL
)
1693 compute_frame_id (prev_frame
);
1694 if (frame_stash_add (prev_frame
))
1697 /* Another frame with the same id was already in the stash. We just
1698 detected a cycle. */
1701 fprintf_unfiltered (gdb_stdlog
, "-> ");
1702 fprint_frame (gdb_stdlog
, NULL
);
1703 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1705 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1707 prev_frame
->next
= NULL
;
1708 this_frame
->prev
= NULL
;
1712 /* Return a "struct frame_info" corresponding to the frame that called
1713 THIS_FRAME. Returns NULL if there is no such frame.
1715 Unlike get_prev_frame, this function always tries to unwind the
1718 static struct frame_info
*
1719 get_prev_frame_1 (struct frame_info
*this_frame
)
1721 struct frame_id this_id
;
1722 struct gdbarch
*gdbarch
;
1724 gdb_assert (this_frame
!= NULL
);
1725 gdbarch
= get_frame_arch (this_frame
);
1729 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_1 (this_frame=");
1730 if (this_frame
!= NULL
)
1731 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1733 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1734 fprintf_unfiltered (gdb_stdlog
, ") ");
1737 /* Only try to do the unwind once. */
1738 if (this_frame
->prev_p
)
1742 fprintf_unfiltered (gdb_stdlog
, "-> ");
1743 fprint_frame (gdb_stdlog
, this_frame
->prev
);
1744 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
1746 return this_frame
->prev
;
1749 /* If the frame unwinder hasn't been selected yet, we must do so
1750 before setting prev_p; otherwise the check for misbehaved
1751 sniffers will think that this frame's sniffer tried to unwind
1752 further (see frame_cleanup_after_sniffer). */
1753 if (this_frame
->unwind
== NULL
)
1754 frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
1756 this_frame
->prev_p
= 1;
1757 this_frame
->stop_reason
= UNWIND_NO_REASON
;
1759 /* If we are unwinding from an inline frame, all of the below tests
1760 were already performed when we unwound from the next non-inline
1761 frame. We must skip them, since we can not get THIS_FRAME's ID
1762 until we have unwound all the way down to the previous non-inline
1764 if (get_frame_type (this_frame
) == INLINE_FRAME
)
1765 return get_prev_frame_if_no_cycle (this_frame
);
1767 /* Check that this frame is unwindable. If it isn't, don't try to
1768 unwind to the prev frame. */
1769 this_frame
->stop_reason
1770 = this_frame
->unwind
->stop_reason (this_frame
,
1771 &this_frame
->prologue_cache
);
1773 if (this_frame
->stop_reason
!= UNWIND_NO_REASON
)
1776 /* Check that this frame is not the outermost. If it is, don't try
1777 to unwind to the prev frame. */
1778 this_id
= get_frame_id (this_frame
);
1779 if (frame_id_eq (this_id
, outer_frame_id
))
1783 fprintf_unfiltered (gdb_stdlog
, "-> ");
1784 fprint_frame (gdb_stdlog
, NULL
);
1785 fprintf_unfiltered (gdb_stdlog
, " // frame ID is outer_frame_id }\n");
1787 this_frame
->stop_reason
= UNWIND_OUTERMOST
;
1791 /* Check that this frame's ID isn't inner to (younger, below, next)
1792 the next frame. This happens when a frame unwind goes backwards.
1793 This check is valid only if this frame and the next frame are NORMAL.
1794 See the comment at frame_id_inner for details. */
1795 if (get_frame_type (this_frame
) == NORMAL_FRAME
1796 && this_frame
->next
->unwind
->type
== NORMAL_FRAME
1797 && frame_id_inner (get_frame_arch (this_frame
->next
), this_id
,
1798 get_frame_id (this_frame
->next
)))
1800 CORE_ADDR this_pc_in_block
;
1801 struct minimal_symbol
*morestack_msym
;
1802 const char *morestack_name
= NULL
;
1804 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
1805 this_pc_in_block
= get_frame_address_in_block (this_frame
);
1806 morestack_msym
= lookup_minimal_symbol_by_pc (this_pc_in_block
).minsym
;
1808 morestack_name
= SYMBOL_LINKAGE_NAME (morestack_msym
);
1809 if (!morestack_name
|| strcmp (morestack_name
, "__morestack") != 0)
1813 fprintf_unfiltered (gdb_stdlog
, "-> ");
1814 fprint_frame (gdb_stdlog
, NULL
);
1815 fprintf_unfiltered (gdb_stdlog
,
1816 " // this frame ID is inner }\n");
1818 this_frame
->stop_reason
= UNWIND_INNER_ID
;
1823 /* Check that this and the next frame do not unwind the PC register
1824 to the same memory location. If they do, then even though they
1825 have different frame IDs, the new frame will be bogus; two
1826 functions can't share a register save slot for the PC. This can
1827 happen when the prologue analyzer finds a stack adjustment, but
1830 This check does assume that the "PC register" is roughly a
1831 traditional PC, even if the gdbarch_unwind_pc method adjusts
1832 it (we do not rely on the value, only on the unwound PC being
1833 dependent on this value). A potential improvement would be
1834 to have the frame prev_pc method and the gdbarch unwind_pc
1835 method set the same lval and location information as
1836 frame_register_unwind. */
1837 if (this_frame
->level
> 0
1838 && gdbarch_pc_regnum (gdbarch
) >= 0
1839 && get_frame_type (this_frame
) == NORMAL_FRAME
1840 && (get_frame_type (this_frame
->next
) == NORMAL_FRAME
1841 || get_frame_type (this_frame
->next
) == INLINE_FRAME
))
1843 int optimized
, realnum
, nrealnum
;
1844 enum lval_type lval
, nlval
;
1845 CORE_ADDR addr
, naddr
;
1847 frame_register_unwind_location (this_frame
,
1848 gdbarch_pc_regnum (gdbarch
),
1849 &optimized
, &lval
, &addr
, &realnum
);
1850 frame_register_unwind_location (get_next_frame (this_frame
),
1851 gdbarch_pc_regnum (gdbarch
),
1852 &optimized
, &nlval
, &naddr
, &nrealnum
);
1854 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
1855 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
1859 fprintf_unfiltered (gdb_stdlog
, "-> ");
1860 fprint_frame (gdb_stdlog
, NULL
);
1861 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
1864 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
1865 this_frame
->prev
= NULL
;
1870 return get_prev_frame_if_no_cycle (this_frame
);
1873 /* Construct a new "struct frame_info" and link it previous to
1876 static struct frame_info
*
1877 get_prev_frame_raw (struct frame_info
*this_frame
)
1879 struct frame_info
*prev_frame
;
1881 /* Allocate the new frame but do not wire it in to the frame chain.
1882 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1883 frame->next to pull some fancy tricks (of course such code is, by
1884 definition, recursive). Try to prevent it.
1886 There is no reason to worry about memory leaks, should the
1887 remainder of the function fail. The allocated memory will be
1888 quickly reclaimed when the frame cache is flushed, and the `we've
1889 been here before' check above will stop repeated memory
1890 allocation calls. */
1891 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1892 prev_frame
->level
= this_frame
->level
+ 1;
1894 /* For now, assume we don't have frame chains crossing address
1896 prev_frame
->pspace
= this_frame
->pspace
;
1897 prev_frame
->aspace
= this_frame
->aspace
;
1899 /* Don't yet compute ->unwind (and hence ->type). It is computed
1900 on-demand in get_frame_type, frame_register_unwind, and
1903 /* Don't yet compute the frame's ID. It is computed on-demand by
1906 /* The unwound frame ID is validate at the start of this function,
1907 as part of the logic to decide if that frame should be further
1908 unwound, and not here while the prev frame is being created.
1909 Doing this makes it possible for the user to examine a frame that
1910 has an invalid frame ID.
1912 Some very old VAX code noted: [...] For the sake of argument,
1913 suppose that the stack is somewhat trashed (which is one reason
1914 that "info frame" exists). So, return 0 (indicating we don't
1915 know the address of the arglist) if we don't know what frame this
1919 this_frame
->prev
= prev_frame
;
1920 prev_frame
->next
= this_frame
;
1924 fprintf_unfiltered (gdb_stdlog
, "-> ");
1925 fprint_frame (gdb_stdlog
, prev_frame
);
1926 fprintf_unfiltered (gdb_stdlog
, " }\n");
1932 /* Debug routine to print a NULL frame being returned. */
1935 frame_debug_got_null_frame (struct frame_info
*this_frame
,
1940 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
1941 if (this_frame
!= NULL
)
1942 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1944 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1945 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
1949 /* Is this (non-sentinel) frame in the "main"() function? */
1952 inside_main_func (struct frame_info
*this_frame
)
1954 struct minimal_symbol
*msymbol
;
1957 if (symfile_objfile
== 0)
1959 msymbol
= lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
1960 if (msymbol
== NULL
)
1962 /* Make certain that the code, and not descriptor, address is
1964 maddr
= gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
1965 SYMBOL_VALUE_ADDRESS (msymbol
),
1967 return maddr
== get_frame_func (this_frame
);
1970 /* Test whether THIS_FRAME is inside the process entry point function. */
1973 inside_entry_func (struct frame_info
*this_frame
)
1975 CORE_ADDR entry_point
;
1977 if (!entry_point_address_query (&entry_point
))
1980 return get_frame_func (this_frame
) == entry_point
;
1983 /* Return a structure containing various interesting information about
1984 the frame that called THIS_FRAME. Returns NULL if there is entier
1985 no such frame or the frame fails any of a set of target-independent
1986 condition that should terminate the frame chain (e.g., as unwinding
1989 This function should not contain target-dependent tests, such as
1990 checking whether the program-counter is zero. */
1993 get_prev_frame (struct frame_info
*this_frame
)
1998 /* There is always a frame. If this assertion fails, suspect that
1999 something should be calling get_selected_frame() or
2000 get_current_frame(). */
2001 gdb_assert (this_frame
!= NULL
);
2002 frame_pc_p
= get_frame_pc_if_available (this_frame
, &frame_pc
);
2004 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
2005 sense to stop unwinding at a dummy frame. One place where a dummy
2006 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
2007 pcsqh register (space register for the instruction at the head of the
2008 instruction queue) cannot be written directly; the only way to set it
2009 is to branch to code that is in the target space. In order to implement
2010 frame dummies on HPUX, the called function is made to jump back to where
2011 the inferior was when the user function was called. If gdb was inside
2012 the main function when we created the dummy frame, the dummy frame will
2013 point inside the main function. */
2014 if (this_frame
->level
>= 0
2015 && get_frame_type (this_frame
) == NORMAL_FRAME
2016 && !backtrace_past_main
2018 && inside_main_func (this_frame
))
2019 /* Don't unwind past main(). Note, this is done _before_ the
2020 frame has been marked as previously unwound. That way if the
2021 user later decides to enable unwinds past main(), that will
2022 automatically happen. */
2024 frame_debug_got_null_frame (this_frame
, "inside main func");
2028 /* If the user's backtrace limit has been exceeded, stop. We must
2029 add two to the current level; one of those accounts for backtrace_limit
2030 being 1-based and the level being 0-based, and the other accounts for
2031 the level of the new frame instead of the level of the current
2033 if (this_frame
->level
+ 2 > backtrace_limit
)
2035 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
2039 /* If we're already inside the entry function for the main objfile,
2040 then it isn't valid. Don't apply this test to a dummy frame -
2041 dummy frame PCs typically land in the entry func. Don't apply
2042 this test to the sentinel frame. Sentinel frames should always
2043 be allowed to unwind. */
2044 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
2045 wasn't checking for "main" in the minimal symbols. With that
2046 fixed asm-source tests now stop in "main" instead of halting the
2047 backtrace in weird and wonderful ways somewhere inside the entry
2048 file. Suspect that tests for inside the entry file/func were
2049 added to work around that (now fixed) case. */
2050 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
2051 suggested having the inside_entry_func test use the
2052 inside_main_func() msymbol trick (along with entry_point_address()
2053 I guess) to determine the address range of the start function.
2054 That should provide a far better stopper than the current
2056 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
2057 applied tail-call optimizations to main so that a function called
2058 from main returns directly to the caller of main. Since we don't
2059 stop at main, we should at least stop at the entry point of the
2061 if (this_frame
->level
>= 0
2062 && get_frame_type (this_frame
) == NORMAL_FRAME
2063 && !backtrace_past_entry
2065 && inside_entry_func (this_frame
))
2067 frame_debug_got_null_frame (this_frame
, "inside entry func");
2071 /* Assume that the only way to get a zero PC is through something
2072 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
2073 will never unwind a zero PC. */
2074 if (this_frame
->level
> 0
2075 && (get_frame_type (this_frame
) == NORMAL_FRAME
2076 || get_frame_type (this_frame
) == INLINE_FRAME
)
2077 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
2078 && frame_pc_p
&& frame_pc
== 0)
2080 frame_debug_got_null_frame (this_frame
, "zero PC");
2084 return get_prev_frame_1 (this_frame
);
2088 get_frame_pc (struct frame_info
*frame
)
2090 gdb_assert (frame
->next
!= NULL
);
2091 return frame_unwind_pc (frame
->next
);
2095 get_frame_pc_if_available (struct frame_info
*frame
, CORE_ADDR
*pc
)
2097 volatile struct gdb_exception ex
;
2099 gdb_assert (frame
->next
!= NULL
);
2101 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
2103 *pc
= frame_unwind_pc (frame
->next
);
2107 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2110 throw_exception (ex
);
2116 /* Return an address that falls within THIS_FRAME's code block. */
2119 get_frame_address_in_block (struct frame_info
*this_frame
)
2121 /* A draft address. */
2122 CORE_ADDR pc
= get_frame_pc (this_frame
);
2124 struct frame_info
*next_frame
= this_frame
->next
;
2126 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
2127 Normally the resume address is inside the body of the function
2128 associated with THIS_FRAME, but there is a special case: when
2129 calling a function which the compiler knows will never return
2130 (for instance abort), the call may be the very last instruction
2131 in the calling function. The resume address will point after the
2132 call and may be at the beginning of a different function
2135 If THIS_FRAME is a signal frame or dummy frame, then we should
2136 not adjust the unwound PC. For a dummy frame, GDB pushed the
2137 resume address manually onto the stack. For a signal frame, the
2138 OS may have pushed the resume address manually and invoked the
2139 handler (e.g. GNU/Linux), or invoked the trampoline which called
2140 the signal handler - but in either case the signal handler is
2141 expected to return to the trampoline. So in both of these
2142 cases we know that the resume address is executable and
2143 related. So we only need to adjust the PC if THIS_FRAME
2144 is a normal function.
2146 If the program has been interrupted while THIS_FRAME is current,
2147 then clearly the resume address is inside the associated
2148 function. There are three kinds of interruption: debugger stop
2149 (next frame will be SENTINEL_FRAME), operating system
2150 signal or exception (next frame will be SIGTRAMP_FRAME),
2151 or debugger-induced function call (next frame will be
2152 DUMMY_FRAME). So we only need to adjust the PC if
2153 NEXT_FRAME is a normal function.
2155 We check the type of NEXT_FRAME first, since it is already
2156 known; frame type is determined by the unwinder, and since
2157 we have THIS_FRAME we've already selected an unwinder for
2160 If the next frame is inlined, we need to keep going until we find
2161 the real function - for instance, if a signal handler is invoked
2162 while in an inlined function, then the code address of the
2163 "calling" normal function should not be adjusted either. */
2165 while (get_frame_type (next_frame
) == INLINE_FRAME
)
2166 next_frame
= next_frame
->next
;
2168 if ((get_frame_type (next_frame
) == NORMAL_FRAME
2169 || get_frame_type (next_frame
) == TAILCALL_FRAME
)
2170 && (get_frame_type (this_frame
) == NORMAL_FRAME
2171 || get_frame_type (this_frame
) == TAILCALL_FRAME
2172 || get_frame_type (this_frame
) == INLINE_FRAME
))
2179 get_frame_address_in_block_if_available (struct frame_info
*this_frame
,
2182 volatile struct gdb_exception ex
;
2184 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
2186 *pc
= get_frame_address_in_block (this_frame
);
2188 if (ex
.reason
< 0 && ex
.error
== NOT_AVAILABLE_ERROR
)
2190 else if (ex
.reason
< 0)
2191 throw_exception (ex
);
2197 find_frame_sal (struct frame_info
*frame
, struct symtab_and_line
*sal
)
2199 struct frame_info
*next_frame
;
2203 /* If the next frame represents an inlined function call, this frame's
2204 sal is the "call site" of that inlined function, which can not
2205 be inferred from get_frame_pc. */
2206 next_frame
= get_next_frame (frame
);
2207 if (frame_inlined_callees (frame
) > 0)
2212 sym
= get_frame_function (next_frame
);
2214 sym
= inline_skipped_symbol (inferior_ptid
);
2216 /* If frame is inline, it certainly has symbols. */
2219 if (SYMBOL_LINE (sym
) != 0)
2221 sal
->symtab
= SYMBOL_SYMTAB (sym
);
2222 sal
->line
= SYMBOL_LINE (sym
);
2225 /* If the symbol does not have a location, we don't know where
2226 the call site is. Do not pretend to. This is jarring, but
2227 we can't do much better. */
2228 sal
->pc
= get_frame_pc (frame
);
2230 sal
->pspace
= get_frame_program_space (frame
);
2235 /* If FRAME is not the innermost frame, that normally means that
2236 FRAME->pc points at the return instruction (which is *after* the
2237 call instruction), and we want to get the line containing the
2238 call (because the call is where the user thinks the program is).
2239 However, if the next frame is either a SIGTRAMP_FRAME or a
2240 DUMMY_FRAME, then the next frame will contain a saved interrupt
2241 PC and such a PC indicates the current (rather than next)
2242 instruction/line, consequently, for such cases, want to get the
2243 line containing fi->pc. */
2244 if (!get_frame_pc_if_available (frame
, &pc
))
2250 notcurrent
= (pc
!= get_frame_address_in_block (frame
));
2251 (*sal
) = find_pc_line (pc
, notcurrent
);
2254 /* Per "frame.h", return the ``address'' of the frame. Code should
2255 really be using get_frame_id(). */
2257 get_frame_base (struct frame_info
*fi
)
2259 return get_frame_id (fi
).stack_addr
;
2262 /* High-level offsets into the frame. Used by the debug info. */
2265 get_frame_base_address (struct frame_info
*fi
)
2267 if (get_frame_type (fi
) != NORMAL_FRAME
)
2269 if (fi
->base
== NULL
)
2270 fi
->base
= frame_base_find_by_frame (fi
);
2271 /* Sneaky: If the low-level unwind and high-level base code share a
2272 common unwinder, let them share the prologue cache. */
2273 if (fi
->base
->unwind
== fi
->unwind
)
2274 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
2275 return fi
->base
->this_base (fi
, &fi
->base_cache
);
2279 get_frame_locals_address (struct frame_info
*fi
)
2281 if (get_frame_type (fi
) != NORMAL_FRAME
)
2283 /* If there isn't a frame address method, find it. */
2284 if (fi
->base
== NULL
)
2285 fi
->base
= frame_base_find_by_frame (fi
);
2286 /* Sneaky: If the low-level unwind and high-level base code share a
2287 common unwinder, let them share the prologue cache. */
2288 if (fi
->base
->unwind
== fi
->unwind
)
2289 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
2290 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
2294 get_frame_args_address (struct frame_info
*fi
)
2296 if (get_frame_type (fi
) != NORMAL_FRAME
)
2298 /* If there isn't a frame address method, find it. */
2299 if (fi
->base
== NULL
)
2300 fi
->base
= frame_base_find_by_frame (fi
);
2301 /* Sneaky: If the low-level unwind and high-level base code share a
2302 common unwinder, let them share the prologue cache. */
2303 if (fi
->base
->unwind
== fi
->unwind
)
2304 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
2305 return fi
->base
->this_args (fi
, &fi
->base_cache
);
2308 /* Return true if the frame unwinder for frame FI is UNWINDER; false
2312 frame_unwinder_is (struct frame_info
*fi
, const struct frame_unwind
*unwinder
)
2314 if (fi
->unwind
== NULL
)
2315 frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
2316 return fi
->unwind
== unwinder
;
2319 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
2320 or -1 for a NULL frame. */
2323 frame_relative_level (struct frame_info
*fi
)
2332 get_frame_type (struct frame_info
*frame
)
2334 if (frame
->unwind
== NULL
)
2335 /* Initialize the frame's unwinder because that's what
2336 provides the frame's type. */
2337 frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
2338 return frame
->unwind
->type
;
2341 struct program_space
*
2342 get_frame_program_space (struct frame_info
*frame
)
2344 return frame
->pspace
;
2347 struct program_space
*
2348 frame_unwind_program_space (struct frame_info
*this_frame
)
2350 gdb_assert (this_frame
);
2352 /* This is really a placeholder to keep the API consistent --- we
2353 assume for now that we don't have frame chains crossing
2355 return this_frame
->pspace
;
2358 struct address_space
*
2359 get_frame_address_space (struct frame_info
*frame
)
2361 return frame
->aspace
;
2364 /* Memory access methods. */
2367 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
2368 gdb_byte
*buf
, int len
)
2370 read_memory (addr
, buf
, len
);
2374 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
2377 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2378 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2380 return read_memory_integer (addr
, len
, byte_order
);
2384 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
2387 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2388 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2390 return read_memory_unsigned_integer (addr
, len
, byte_order
);
2394 safe_frame_unwind_memory (struct frame_info
*this_frame
,
2395 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
2397 /* NOTE: target_read_memory returns zero on success! */
2398 return !target_read_memory (addr
, buf
, len
);
2401 /* Architecture methods. */
2404 get_frame_arch (struct frame_info
*this_frame
)
2406 return frame_unwind_arch (this_frame
->next
);
2410 frame_unwind_arch (struct frame_info
*next_frame
)
2412 if (!next_frame
->prev_arch
.p
)
2414 struct gdbarch
*arch
;
2416 if (next_frame
->unwind
== NULL
)
2417 frame_unwind_find_by_frame (next_frame
, &next_frame
->prologue_cache
);
2419 if (next_frame
->unwind
->prev_arch
!= NULL
)
2420 arch
= next_frame
->unwind
->prev_arch (next_frame
,
2421 &next_frame
->prologue_cache
);
2423 arch
= get_frame_arch (next_frame
);
2425 next_frame
->prev_arch
.arch
= arch
;
2426 next_frame
->prev_arch
.p
= 1;
2428 fprintf_unfiltered (gdb_stdlog
,
2429 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2431 gdbarch_bfd_arch_info (arch
)->printable_name
);
2434 return next_frame
->prev_arch
.arch
;
2438 frame_unwind_caller_arch (struct frame_info
*next_frame
)
2440 return frame_unwind_arch (skip_artificial_frames (next_frame
));
2443 /* Stack pointer methods. */
2446 get_frame_sp (struct frame_info
*this_frame
)
2448 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2450 /* Normality - an architecture that provides a way of obtaining any
2451 frame inner-most address. */
2452 if (gdbarch_unwind_sp_p (gdbarch
))
2453 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2454 operate on THIS_FRAME now. */
2455 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
2456 /* Now things are really are grim. Hope that the value returned by
2457 the gdbarch_sp_regnum register is meaningful. */
2458 if (gdbarch_sp_regnum (gdbarch
) >= 0)
2459 return get_frame_register_unsigned (this_frame
,
2460 gdbarch_sp_regnum (gdbarch
));
2461 internal_error (__FILE__
, __LINE__
, _("Missing unwind SP method"));
2464 /* Return the reason why we can't unwind past FRAME. */
2466 enum unwind_stop_reason
2467 get_frame_unwind_stop_reason (struct frame_info
*frame
)
2469 /* If we haven't tried to unwind past this point yet, then assume
2470 that unwinding would succeed. */
2471 if (frame
->prev_p
== 0)
2472 return UNWIND_NO_REASON
;
2474 /* Otherwise, we set a reason when we succeeded (or failed) to
2476 return frame
->stop_reason
;
2479 /* Return a string explaining REASON. */
2482 frame_stop_reason_string (enum unwind_stop_reason reason
)
2486 #define SET(name, description) \
2487 case name: return _(description);
2488 #include "unwind_stop_reasons.def"
2492 internal_error (__FILE__
, __LINE__
,
2493 "Invalid frame stop reason");
2497 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2501 frame_cleanup_after_sniffer (void *arg
)
2503 struct frame_info
*frame
= arg
;
2505 /* The sniffer should not allocate a prologue cache if it did not
2506 match this frame. */
2507 gdb_assert (frame
->prologue_cache
== NULL
);
2509 /* No sniffer should extend the frame chain; sniff based on what is
2511 gdb_assert (!frame
->prev_p
);
2513 /* The sniffer should not check the frame's ID; that's circular. */
2514 gdb_assert (!frame
->this_id
.p
);
2516 /* Clear cached fields dependent on the unwinder.
2518 The previous PC is independent of the unwinder, but the previous
2519 function is not (see get_frame_address_in_block). */
2520 frame
->prev_func
.p
= 0;
2521 frame
->prev_func
.addr
= 0;
2523 /* Discard the unwinder last, so that we can easily find it if an assertion
2524 in this function triggers. */
2525 frame
->unwind
= NULL
;
2528 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2529 Return a cleanup which should be called if unwinding fails, and
2530 discarded if it succeeds. */
2533 frame_prepare_for_sniffer (struct frame_info
*frame
,
2534 const struct frame_unwind
*unwind
)
2536 gdb_assert (frame
->unwind
== NULL
);
2537 frame
->unwind
= unwind
;
2538 return make_cleanup (frame_cleanup_after_sniffer
, frame
);
2541 extern initialize_file_ftype _initialize_frame
; /* -Wmissing-prototypes */
2543 static struct cmd_list_element
*set_backtrace_cmdlist
;
2544 static struct cmd_list_element
*show_backtrace_cmdlist
;
2547 set_backtrace_cmd (char *args
, int from_tty
)
2549 help_list (set_backtrace_cmdlist
, "set backtrace ", -1, gdb_stdout
);
2553 show_backtrace_cmd (char *args
, int from_tty
)
2555 cmd_show_list (show_backtrace_cmdlist
, from_tty
, "");
2559 _initialize_frame (void)
2561 obstack_init (&frame_cache_obstack
);
2563 frame_stash_create ();
2565 observer_attach_target_changed (frame_observer_target_changed
);
2567 add_prefix_cmd ("backtrace", class_maintenance
, set_backtrace_cmd
, _("\
2568 Set backtrace specific variables.\n\
2569 Configure backtrace variables such as the backtrace limit"),
2570 &set_backtrace_cmdlist
, "set backtrace ",
2571 0/*allow-unknown*/, &setlist
);
2572 add_prefix_cmd ("backtrace", class_maintenance
, show_backtrace_cmd
, _("\
2573 Show backtrace specific variables\n\
2574 Show backtrace variables such as the backtrace limit"),
2575 &show_backtrace_cmdlist
, "show backtrace ",
2576 0/*allow-unknown*/, &showlist
);
2578 add_setshow_boolean_cmd ("past-main", class_obscure
,
2579 &backtrace_past_main
, _("\
2580 Set whether backtraces should continue past \"main\"."), _("\
2581 Show whether backtraces should continue past \"main\"."), _("\
2582 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2583 the backtrace at \"main\". Set this variable if you need to see the rest\n\
2584 of the stack trace."),
2586 show_backtrace_past_main
,
2587 &set_backtrace_cmdlist
,
2588 &show_backtrace_cmdlist
);
2590 add_setshow_boolean_cmd ("past-entry", class_obscure
,
2591 &backtrace_past_entry
, _("\
2592 Set whether backtraces should continue past the entry point of a program."),
2594 Show whether backtraces should continue past the entry point of a program."),
2596 Normally there are no callers beyond the entry point of a program, so GDB\n\
2597 will terminate the backtrace there. Set this variable if you need to see\n\
2598 the rest of the stack trace."),
2600 show_backtrace_past_entry
,
2601 &set_backtrace_cmdlist
,
2602 &show_backtrace_cmdlist
);
2604 add_setshow_uinteger_cmd ("limit", class_obscure
,
2605 &backtrace_limit
, _("\
2606 Set an upper bound on the number of backtrace levels."), _("\
2607 Show the upper bound on the number of backtrace levels."), _("\
2608 No more than the specified number of frames can be displayed or examined.\n\
2609 Literal \"unlimited\" or zero means no limit."),
2611 show_backtrace_limit
,
2612 &set_backtrace_cmdlist
,
2613 &show_backtrace_cmdlist
);
2615 /* Debug this files internals. */
2616 add_setshow_zuinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
2617 Set frame debugging."), _("\
2618 Show frame debugging."), _("\
2619 When non-zero, frame specific internal debugging is enabled."),
2622 &setdebuglist
, &showdebuglist
);