1 /* Cache and manage frames for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "inferior.h" /* for inferior_ptid */
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include "user-regs.h"
30 #include "gdb_obstack.h"
31 #include "dummy-frame.h"
32 #include "sentinel-frame.h"
36 #include "frame-unwind.h"
37 #include "frame-base.h"
42 #include "exceptions.h"
43 #include "gdbthread.h"
45 #include "inline-frame.h"
47 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
48 static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
50 /* We keep a cache of stack frames, each of which is a "struct
51 frame_info". The innermost one gets allocated (in
52 wait_for_inferior) each time the inferior stops; current_frame
53 points to it. Additional frames get allocated (in get_prev_frame)
54 as needed, and are chained through the next and prev fields. Any
55 time that the frame cache becomes invalid (most notably when we
56 execute something, but also if we change how we interpret the
57 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
58 which reads new symbols)), we should call reinit_frame_cache. */
62 /* Level of this frame. The inner-most (youngest) frame is at level
63 0. As you move towards the outer-most (oldest) frame, the level
64 increases. This is a cached value. It could just as easily be
65 computed by counting back from the selected frame to the inner
67 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
68 reserved to indicate a bogus frame - one that has been created
69 just to keep GDB happy (GDB always needs a frame). For the
70 moment leave this as speculation. */
73 /* The frame's low-level unwinder and corresponding cache. The
74 low-level unwinder is responsible for unwinding register values
75 for the previous frame. The low-level unwind methods are
76 selected based on the presence, or otherwise, of register unwind
77 information such as CFI. */
79 const struct frame_unwind *unwind;
81 /* Cached copy of the previous frame's architecture. */
88 /* Cached copy of the previous frame's resume address. */
94 /* Cached copy of the previous frame's function address. */
101 /* This frame's ID. */
105 struct frame_id value;
108 /* The frame's high-level base methods, and corresponding cache.
109 The high level base methods are selected based on the frame's
111 const struct frame_base *base;
114 /* Pointers to the next (down, inner, younger) and previous (up,
115 outer, older) frame_info's in the frame cache. */
116 struct frame_info *next; /* down, inner, younger */
118 struct frame_info *prev; /* up, outer, older */
120 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
121 could. Only valid when PREV_P is set. */
122 enum unwind_stop_reason stop_reason;
125 /* Flag to control debugging. */
129 show_frame_debug (struct ui_file *file, int from_tty,
130 struct cmd_list_element *c, const char *value)
132 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
135 /* Flag to indicate whether backtraces should stop at main et.al. */
137 static int backtrace_past_main;
139 show_backtrace_past_main (struct ui_file *file, int from_tty,
140 struct cmd_list_element *c, const char *value)
142 fprintf_filtered (file, _("\
143 Whether backtraces should continue past \"main\" is %s.\n"),
147 static int backtrace_past_entry;
149 show_backtrace_past_entry (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
152 fprintf_filtered (file, _("\
153 Whether backtraces should continue past the entry point of a program is %s.\n"),
157 static int backtrace_limit = INT_MAX;
159 show_backtrace_limit (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
162 fprintf_filtered (file, _("\
163 An upper bound on the number of backtrace levels is %s.\n"),
169 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
172 fprintf_unfiltered (file, "%s=0x%s", name, paddr_nz (addr));
174 fprintf_unfiltered (file, "!%s", name);
178 fprint_frame_id (struct ui_file *file, struct frame_id id)
180 fprintf_unfiltered (file, "{");
181 fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
182 fprintf_unfiltered (file, ",");
183 fprint_field (file, "code", id.code_addr_p, id.code_addr);
184 fprintf_unfiltered (file, ",");
185 fprint_field (file, "special", id.special_addr_p, id.special_addr);
187 fprintf_unfiltered (file, ",inlined=%d", id.inline_depth);
188 fprintf_unfiltered (file, "}");
192 fprint_frame_type (struct ui_file *file, enum frame_type type)
197 fprintf_unfiltered (file, "NORMAL_FRAME");
200 fprintf_unfiltered (file, "DUMMY_FRAME");
203 fprintf_unfiltered (file, "INLINE_FRAME");
206 fprintf_unfiltered (file, "SENTINEL_FRAME");
209 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
212 fprintf_unfiltered (file, "ARCH_FRAME");
215 fprintf_unfiltered (file, "<unknown type>");
221 fprint_frame (struct ui_file *file, struct frame_info *fi)
225 fprintf_unfiltered (file, "<NULL frame>");
228 fprintf_unfiltered (file, "{");
229 fprintf_unfiltered (file, "level=%d", fi->level);
230 fprintf_unfiltered (file, ",");
231 fprintf_unfiltered (file, "type=");
232 if (fi->unwind != NULL)
233 fprint_frame_type (file, fi->unwind->type);
235 fprintf_unfiltered (file, "<unknown>");
236 fprintf_unfiltered (file, ",");
237 fprintf_unfiltered (file, "unwind=");
238 if (fi->unwind != NULL)
239 gdb_print_host_address (fi->unwind, file);
241 fprintf_unfiltered (file, "<unknown>");
242 fprintf_unfiltered (file, ",");
243 fprintf_unfiltered (file, "pc=");
244 if (fi->next != NULL && fi->next->prev_pc.p)
245 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
247 fprintf_unfiltered (file, "<unknown>");
248 fprintf_unfiltered (file, ",");
249 fprintf_unfiltered (file, "id=");
251 fprint_frame_id (file, fi->this_id.value);
253 fprintf_unfiltered (file, "<unknown>");
254 fprintf_unfiltered (file, ",");
255 fprintf_unfiltered (file, "func=");
256 if (fi->next != NULL && fi->next->prev_func.p)
257 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
259 fprintf_unfiltered (file, "<unknown>");
260 fprintf_unfiltered (file, "}");
263 /* Given FRAME, return the enclosing normal frame for inlined
264 function frames. Otherwise return the original frame. */
266 static struct frame_info *
267 skip_inlined_frames (struct frame_info *frame)
269 while (get_frame_type (frame) == INLINE_FRAME)
270 frame = get_prev_frame (frame);
275 /* Return a frame uniq ID that can be used to, later, re-find the
279 get_frame_id (struct frame_info *fi)
283 return null_frame_id;
288 fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
290 /* Find the unwinder. */
291 if (fi->unwind == NULL)
292 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
293 /* Find THIS frame's ID. */
294 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
298 fprintf_unfiltered (gdb_stdlog, "-> ");
299 fprint_frame_id (gdb_stdlog, fi->this_id.value);
300 fprintf_unfiltered (gdb_stdlog, " }\n");
303 return fi->this_id.value;
307 get_stack_frame_id (struct frame_info *next_frame)
309 return get_frame_id (skip_inlined_frames (next_frame));
313 frame_unwind_caller_id (struct frame_info *next_frame)
315 struct frame_info *this_frame;
317 /* Use get_prev_frame_1, and not get_prev_frame. The latter will truncate
318 the frame chain, leading to this function unintentionally
319 returning a null_frame_id (e.g., when a caller requests the frame
320 ID of "main()"s caller. */
322 next_frame = skip_inlined_frames (next_frame);
323 this_frame = get_prev_frame_1 (next_frame);
325 return get_frame_id (skip_inlined_frames (this_frame));
327 return null_frame_id;
330 const struct frame_id null_frame_id; /* All zeros. */
333 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
334 CORE_ADDR special_addr)
336 struct frame_id id = null_frame_id;
337 id.stack_addr = stack_addr;
339 id.code_addr = code_addr;
341 id.special_addr = special_addr;
342 id.special_addr_p = 1;
347 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
349 struct frame_id id = null_frame_id;
350 id.stack_addr = stack_addr;
352 id.code_addr = code_addr;
358 frame_id_build_wild (CORE_ADDR stack_addr)
360 struct frame_id id = null_frame_id;
361 id.stack_addr = stack_addr;
367 frame_id_p (struct frame_id l)
370 /* The frame is valid iff it has a valid stack address. */
374 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
375 fprint_frame_id (gdb_stdlog, l);
376 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
382 frame_id_inlined_p (struct frame_id l)
387 return (l.inline_depth != 0);
391 frame_id_eq (struct frame_id l, struct frame_id r)
394 if (!l.stack_addr_p || !r.stack_addr_p)
395 /* Like a NaN, if either ID is invalid, the result is false.
396 Note that a frame ID is invalid iff it is the null frame ID. */
398 else if (l.stack_addr != r.stack_addr)
399 /* If .stack addresses are different, the frames are different. */
401 else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
402 /* An invalid code addr is a wild card. If .code addresses are
403 different, the frames are different. */
405 else if (l.special_addr_p && r.special_addr_p
406 && l.special_addr != r.special_addr)
407 /* An invalid special addr is a wild card (or unused). Otherwise
408 if special addresses are different, the frames are different. */
410 else if (l.inline_depth != r.inline_depth)
411 /* If inline depths are different, the frames must be different. */
414 /* Frames are equal. */
419 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
420 fprint_frame_id (gdb_stdlog, l);
421 fprintf_unfiltered (gdb_stdlog, ",r=");
422 fprint_frame_id (gdb_stdlog, r);
423 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
428 /* Safety net to check whether frame ID L should be inner to
429 frame ID R, according to their stack addresses.
431 This method cannot be used to compare arbitrary frames, as the
432 ranges of valid stack addresses may be discontiguous (e.g. due
435 However, it can be used as safety net to discover invalid frame
436 IDs in certain circumstances. Assuming that NEXT is the immediate
437 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
439 * The stack address of NEXT must be inner-than-or-equal to the stack
442 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
445 * If NEXT and THIS have different stack addresses, no other frame
446 in the frame chain may have a stack address in between.
448 Therefore, if frame_id_inner (TEST, THIS) holds, but
449 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
450 to a valid frame in the frame chain.
452 The sanity checks above cannot be performed when a SIGTRAMP frame
453 is involved, because signal handlers might be executed on a different
454 stack than the stack used by the routine that caused the signal
455 to be raised. This can happen for instance when a thread exceeds
456 its maximum stack size. In this case, certain compilers implement
457 a stack overflow strategy that cause the handler to be run on a
461 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
464 if (!l.stack_addr_p || !r.stack_addr_p)
465 /* Like NaN, any operation involving an invalid ID always fails. */
467 else if (l.inline_depth > r.inline_depth
468 && l.stack_addr == r.stack_addr
469 && l.code_addr_p == r.code_addr_p
470 && l.special_addr_p == r.special_addr_p
471 && l.special_addr == r.special_addr)
473 /* Same function, different inlined functions. */
474 struct block *lb, *rb;
476 gdb_assert (l.code_addr_p && r.code_addr_p);
478 lb = block_for_pc (l.code_addr);
479 rb = block_for_pc (r.code_addr);
481 if (lb == NULL || rb == NULL)
482 /* Something's gone wrong. */
485 /* This will return true if LB and RB are the same block, or
486 if the block with the smaller depth lexically encloses the
487 block with the greater depth. */
488 inner = contained_in (lb, rb);
491 /* Only return non-zero when strictly inner than. Note that, per
492 comment in "frame.h", there is some fuzz here. Frameless
493 functions are not strictly inner than (same .stack but
494 different .code and/or .special address). */
495 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
498 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
499 fprint_frame_id (gdb_stdlog, l);
500 fprintf_unfiltered (gdb_stdlog, ",r=");
501 fprint_frame_id (gdb_stdlog, r);
502 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
508 frame_find_by_id (struct frame_id id)
510 struct frame_info *frame, *prev_frame;
512 /* ZERO denotes the null frame, let the caller decide what to do
513 about it. Should it instead return get_current_frame()? */
514 if (!frame_id_p (id))
517 for (frame = get_current_frame (); ; frame = prev_frame)
519 struct frame_id this = get_frame_id (frame);
520 if (frame_id_eq (id, this))
521 /* An exact match. */
524 prev_frame = get_prev_frame (frame);
528 /* As a safety net to avoid unnecessary backtracing while trying
529 to find an invalid ID, we check for a common situation where
530 we can detect from comparing stack addresses that no other
531 frame in the current frame chain can have this ID. See the
532 comment at frame_id_inner for details. */
533 if (get_frame_type (frame) == NORMAL_FRAME
534 && !frame_id_inner (get_frame_arch (frame), id, this)
535 && frame_id_inner (get_frame_arch (prev_frame), id,
536 get_frame_id (prev_frame)))
543 frame_unwind_pc (struct frame_info *this_frame)
545 if (!this_frame->prev_pc.p)
548 if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
550 /* The right way. The `pure' way. The one true way. This
551 method depends solely on the register-unwind code to
552 determine the value of registers in THIS frame, and hence
553 the value of this frame's PC (resume address). A typical
554 implementation is no more than:
556 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
557 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
559 Note: this method is very heavily dependent on a correct
560 register-unwind implementation, it pays to fix that
561 method first; this method is frame type agnostic, since
562 it only deals with register values, it works with any
563 frame. This is all in stark contrast to the old
564 FRAME_SAVED_PC which would try to directly handle all the
565 different ways that a PC could be unwound. */
566 pc = gdbarch_unwind_pc (frame_unwind_arch (this_frame), this_frame);
569 internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
570 this_frame->prev_pc.value = pc;
571 this_frame->prev_pc.p = 1;
573 fprintf_unfiltered (gdb_stdlog,
574 "{ frame_unwind_caller_pc (this_frame=%d) -> 0x%s }\n",
576 paddr_nz (this_frame->prev_pc.value));
578 return this_frame->prev_pc.value;
582 frame_unwind_caller_pc (struct frame_info *this_frame)
584 return frame_unwind_pc (skip_inlined_frames (this_frame));
588 get_frame_func (struct frame_info *this_frame)
590 struct frame_info *next_frame = this_frame->next;
592 if (!next_frame->prev_func.p)
594 /* Make certain that this, and not the adjacent, function is
596 CORE_ADDR addr_in_block = get_frame_address_in_block (this_frame);
597 next_frame->prev_func.p = 1;
598 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
600 fprintf_unfiltered (gdb_stdlog,
601 "{ get_frame_func (this_frame=%d) -> 0x%s }\n",
603 paddr_nz (next_frame->prev_func.addr));
605 return next_frame->prev_func.addr;
609 do_frame_register_read (void *src, int regnum, gdb_byte *buf)
611 return frame_register_read (src, regnum, buf);
615 frame_save_as_regcache (struct frame_info *this_frame)
617 struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame));
618 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
619 regcache_save (regcache, do_frame_register_read, this_frame);
620 discard_cleanups (cleanups);
625 frame_pop (struct frame_info *this_frame)
627 struct frame_info *prev_frame;
628 struct regcache *scratch;
629 struct cleanup *cleanups;
631 if (get_frame_type (this_frame) == DUMMY_FRAME)
633 /* Popping a dummy frame involves restoring more than just registers.
634 dummy_frame_pop does all the work. */
635 dummy_frame_pop (get_frame_id (this_frame));
639 /* Ensure that we have a frame to pop to. */
640 prev_frame = get_prev_frame_1 (this_frame);
643 error (_("Cannot pop the initial frame."));
645 /* Make a copy of all the register values unwound from this frame.
646 Save them in a scratch buffer so that there isn't a race between
647 trying to extract the old values from the current regcache while
648 at the same time writing new values into that same cache. */
649 scratch = frame_save_as_regcache (prev_frame);
650 cleanups = make_cleanup_regcache_xfree (scratch);
652 /* FIXME: cagney/2003-03-16: It should be possible to tell the
653 target's register cache that it is about to be hit with a burst
654 register transfer and that the sequence of register writes should
655 be batched. The pair target_prepare_to_store() and
656 target_store_registers() kind of suggest this functionality.
657 Unfortunately, they don't implement it. Their lack of a formal
658 definition can lead to targets writing back bogus values
659 (arguably a bug in the target code mind). */
660 /* Now copy those saved registers into the current regcache.
661 Here, regcache_cpy() calls regcache_restore(). */
662 regcache_cpy (get_current_regcache (), scratch);
663 do_cleanups (cleanups);
665 /* We've made right mess of GDB's local state, just discard
667 reinit_frame_cache ();
671 frame_register_unwind (struct frame_info *frame, int regnum,
672 int *optimizedp, enum lval_type *lvalp,
673 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
677 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
678 that the value proper does not need to be fetched. */
679 gdb_assert (optimizedp != NULL);
680 gdb_assert (lvalp != NULL);
681 gdb_assert (addrp != NULL);
682 gdb_assert (realnump != NULL);
683 /* gdb_assert (bufferp != NULL); */
685 value = frame_unwind_register_value (frame, regnum);
687 gdb_assert (value != NULL);
689 *optimizedp = value_optimized_out (value);
690 *lvalp = VALUE_LVAL (value);
691 *addrp = value_address (value);
692 *realnump = VALUE_REGNUM (value);
695 memcpy (bufferp, value_contents_all (value),
696 TYPE_LENGTH (value_type (value)));
698 /* Dispose of the new value. This prevents watchpoints from
699 trying to watch the saved frame pointer. */
700 release_value (value);
705 frame_register (struct frame_info *frame, int regnum,
706 int *optimizedp, enum lval_type *lvalp,
707 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
709 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
710 that the value proper does not need to be fetched. */
711 gdb_assert (optimizedp != NULL);
712 gdb_assert (lvalp != NULL);
713 gdb_assert (addrp != NULL);
714 gdb_assert (realnump != NULL);
715 /* gdb_assert (bufferp != NULL); */
717 /* Obtain the register value by unwinding the register from the next
718 (more inner frame). */
719 gdb_assert (frame != NULL && frame->next != NULL);
720 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
725 frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
731 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
736 get_frame_register (struct frame_info *frame,
737 int regnum, gdb_byte *buf)
739 frame_unwind_register (frame->next, regnum, buf);
743 frame_unwind_register_value (struct frame_info *frame, int regnum)
745 struct gdbarch *gdbarch;
748 gdb_assert (frame != NULL);
749 gdbarch = frame_unwind_arch (frame);
753 fprintf_unfiltered (gdb_stdlog, "\
754 { frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
755 frame->level, regnum,
756 user_reg_map_regnum_to_name (gdbarch, regnum));
759 /* Find the unwinder. */
760 if (frame->unwind == NULL)
761 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
763 /* Ask this frame to unwind its register. */
764 value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
768 fprintf_unfiltered (gdb_stdlog, "->");
769 if (value_optimized_out (value))
770 fprintf_unfiltered (gdb_stdlog, " optimized out");
773 if (VALUE_LVAL (value) == lval_register)
774 fprintf_unfiltered (gdb_stdlog, " register=%d",
775 VALUE_REGNUM (value));
776 else if (VALUE_LVAL (value) == lval_memory)
777 fprintf_unfiltered (gdb_stdlog, " address=0x%s",
778 paddr_nz (value_address (value)));
780 fprintf_unfiltered (gdb_stdlog, " computed");
782 if (value_lazy (value))
783 fprintf_unfiltered (gdb_stdlog, " lazy");
787 const gdb_byte *buf = value_contents (value);
789 fprintf_unfiltered (gdb_stdlog, " bytes=");
790 fprintf_unfiltered (gdb_stdlog, "[");
791 for (i = 0; i < register_size (gdbarch, regnum); i++)
792 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
793 fprintf_unfiltered (gdb_stdlog, "]");
797 fprintf_unfiltered (gdb_stdlog, " }\n");
804 get_frame_register_value (struct frame_info *frame, int regnum)
806 return frame_unwind_register_value (frame->next, regnum);
810 frame_unwind_register_signed (struct frame_info *frame, int regnum)
812 gdb_byte buf[MAX_REGISTER_SIZE];
813 frame_unwind_register (frame, regnum, buf);
814 return extract_signed_integer (buf, register_size (frame_unwind_arch (frame),
819 get_frame_register_signed (struct frame_info *frame, int regnum)
821 return frame_unwind_register_signed (frame->next, regnum);
825 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
827 gdb_byte buf[MAX_REGISTER_SIZE];
828 frame_unwind_register (frame, regnum, buf);
829 return extract_unsigned_integer (buf, register_size (frame_unwind_arch (frame),
834 get_frame_register_unsigned (struct frame_info *frame, int regnum)
836 return frame_unwind_register_unsigned (frame->next, regnum);
840 put_frame_register (struct frame_info *frame, int regnum,
843 struct gdbarch *gdbarch = get_frame_arch (frame);
848 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
850 error (_("Attempt to assign to a value that was optimized out."));
855 /* FIXME: write_memory doesn't yet take constant buffers.
857 gdb_byte tmp[MAX_REGISTER_SIZE];
858 memcpy (tmp, buf, register_size (gdbarch, regnum));
859 write_memory (addr, tmp, register_size (gdbarch, regnum));
863 regcache_cooked_write (get_current_regcache (), realnum, buf);
866 error (_("Attempt to assign to an unmodifiable value."));
870 /* frame_register_read ()
872 Find and return the value of REGNUM for the specified stack frame.
873 The number of bytes copied is REGISTER_SIZE (REGNUM).
875 Returns 0 if the register value could not be found. */
878 frame_register_read (struct frame_info *frame, int regnum,
885 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
891 get_frame_register_bytes (struct frame_info *frame, int regnum,
892 CORE_ADDR offset, int len, gdb_byte *myaddr)
894 struct gdbarch *gdbarch = get_frame_arch (frame);
899 /* Skip registers wholly inside of OFFSET. */
900 while (offset >= register_size (gdbarch, regnum))
902 offset -= register_size (gdbarch, regnum);
906 /* Ensure that we will not read beyond the end of the register file.
907 This can only ever happen if the debug information is bad. */
909 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
910 for (i = regnum; i < numregs; i++)
912 int thissize = register_size (gdbarch, i);
914 break; /* This register is not available on this architecture. */
919 warning (_("Bad debug information detected: "
920 "Attempt to read %d bytes from registers."), len);
927 int curr_len = register_size (gdbarch, regnum) - offset;
931 if (curr_len == register_size (gdbarch, regnum))
933 if (!frame_register_read (frame, regnum, myaddr))
938 gdb_byte buf[MAX_REGISTER_SIZE];
939 if (!frame_register_read (frame, regnum, buf))
941 memcpy (myaddr, buf + offset, curr_len);
954 put_frame_register_bytes (struct frame_info *frame, int regnum,
955 CORE_ADDR offset, int len, const gdb_byte *myaddr)
957 struct gdbarch *gdbarch = get_frame_arch (frame);
959 /* Skip registers wholly inside of OFFSET. */
960 while (offset >= register_size (gdbarch, regnum))
962 offset -= register_size (gdbarch, regnum);
969 int curr_len = register_size (gdbarch, regnum) - offset;
973 if (curr_len == register_size (gdbarch, regnum))
975 put_frame_register (frame, regnum, myaddr);
979 gdb_byte buf[MAX_REGISTER_SIZE];
980 frame_register_read (frame, regnum, buf);
981 memcpy (buf + offset, myaddr, curr_len);
982 put_frame_register (frame, regnum, buf);
992 /* Create a sentinel frame. */
994 static struct frame_info *
995 create_sentinel_frame (struct regcache *regcache)
997 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
999 /* Explicitly initialize the sentinel frame's cache. Provide it
1000 with the underlying regcache. In the future additional
1001 information, such as the frame's thread will be added. */
1002 frame->prologue_cache = sentinel_frame_cache (regcache);
1003 /* For the moment there is only one sentinel frame implementation. */
1004 frame->unwind = sentinel_frame_unwind;
1005 /* Link this frame back to itself. The frame is self referential
1006 (the unwound PC is the same as the pc), so make it so. */
1007 frame->next = frame;
1008 /* Make the sentinel frame's ID valid, but invalid. That way all
1009 comparisons with it should fail. */
1010 frame->this_id.p = 1;
1011 frame->this_id.value = null_frame_id;
1014 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1015 fprint_frame (gdb_stdlog, frame);
1016 fprintf_unfiltered (gdb_stdlog, " }\n");
1021 /* Info about the innermost stack frame (contents of FP register) */
1023 static struct frame_info *current_frame;
1025 /* Cache for frame addresses already read by gdb. Valid only while
1026 inferior is stopped. Control variables for the frame cache should
1027 be local to this module. */
1029 static struct obstack frame_cache_obstack;
1032 frame_obstack_zalloc (unsigned long size)
1034 void *data = obstack_alloc (&frame_cache_obstack, size);
1035 memset (data, 0, size);
1039 /* Return the innermost (currently executing) stack frame. This is
1040 split into two functions. The function unwind_to_current_frame()
1041 is wrapped in catch exceptions so that, even when the unwind of the
1042 sentinel frame fails, the function still returns a stack frame. */
1045 unwind_to_current_frame (struct ui_out *ui_out, void *args)
1047 struct frame_info *frame = get_prev_frame (args);
1048 /* A sentinel frame can fail to unwind, e.g., because its PC value
1049 lands in somewhere like start. */
1052 current_frame = frame;
1057 get_current_frame (void)
1059 /* First check, and report, the lack of registers. Having GDB
1060 report "No stack!" or "No memory" when the target doesn't even
1061 have registers is very confusing. Besides, "printcmd.exp"
1062 explicitly checks that ``print $pc'' with no registers prints "No
1064 if (!target_has_registers)
1065 error (_("No registers."));
1066 if (!target_has_stack)
1067 error (_("No stack."));
1068 if (!target_has_memory)
1069 error (_("No memory."));
1070 if (ptid_equal (inferior_ptid, null_ptid))
1071 error (_("No selected thread."));
1072 if (is_exited (inferior_ptid))
1073 error (_("Invalid selected thread."));
1074 if (is_executing (inferior_ptid))
1075 error (_("Target is executing."));
1077 if (current_frame == NULL)
1079 struct frame_info *sentinel_frame =
1080 create_sentinel_frame (get_current_regcache ());
1081 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
1082 RETURN_MASK_ERROR) != 0)
1084 /* Oops! Fake a current frame? Is this useful? It has a PC
1085 of zero, for instance. */
1086 current_frame = sentinel_frame;
1089 return current_frame;
1092 /* The "selected" stack frame is used by default for local and arg
1093 access. May be zero, for no selected frame. */
1095 static struct frame_info *selected_frame;
1098 has_stack_frames (void)
1100 if (!target_has_registers || !target_has_stack || !target_has_memory)
1103 /* No current inferior, no frame. */
1104 if (ptid_equal (inferior_ptid, null_ptid))
1107 /* Don't try to read from a dead thread. */
1108 if (is_exited (inferior_ptid))
1111 /* ... or from a spinning thread. */
1112 if (is_executing (inferior_ptid))
1118 /* Return the selected frame. Always non-NULL (unless there isn't an
1119 inferior sufficient for creating a frame) in which case an error is
1123 get_selected_frame (const char *message)
1125 if (selected_frame == NULL)
1127 if (message != NULL && !has_stack_frames ())
1128 error (("%s"), message);
1129 /* Hey! Don't trust this. It should really be re-finding the
1130 last selected frame of the currently selected thread. This,
1131 though, is better than nothing. */
1132 select_frame (get_current_frame ());
1134 /* There is always a frame. */
1135 gdb_assert (selected_frame != NULL);
1136 return selected_frame;
1139 /* This is a variant of get_selected_frame() which can be called when
1140 the inferior does not have a frame; in that case it will return
1141 NULL instead of calling error(). */
1144 deprecated_safe_get_selected_frame (void)
1146 if (!has_stack_frames ())
1148 return get_selected_frame (NULL);
1151 /* Select frame FI (or NULL - to invalidate the current frame). */
1154 select_frame (struct frame_info *fi)
1158 selected_frame = fi;
1159 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1160 frame is being invalidated. */
1161 if (deprecated_selected_frame_level_changed_hook)
1162 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
1164 /* FIXME: kseitz/2002-08-28: It would be nice to call
1165 selected_frame_level_changed_event() right here, but due to limitations
1166 in the current interfaces, we would end up flooding UIs with events
1167 because select_frame() is used extensively internally.
1169 Once we have frame-parameterized frame (and frame-related) commands,
1170 the event notification can be moved here, since this function will only
1171 be called when the user's selected frame is being changed. */
1173 /* Ensure that symbols for this frame are read in. Also, determine the
1174 source language of this frame, and switch to it if desired. */
1177 /* We retrieve the frame's symtab by using the frame PC. However
1178 we cannot use the frame PC as-is, because it usually points to
1179 the instruction following the "call", which is sometimes the
1180 first instruction of another function. So we rely on
1181 get_frame_address_in_block() which provides us with a PC which
1182 is guaranteed to be inside the frame's code block. */
1183 s = find_pc_symtab (get_frame_address_in_block (fi));
1185 && s->language != current_language->la_language
1186 && s->language != language_unknown
1187 && language_mode == language_mode_auto)
1189 set_language (s->language);
1194 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1195 Always returns a non-NULL value. */
1198 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1200 struct frame_info *fi;
1204 fprintf_unfiltered (gdb_stdlog,
1205 "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1206 paddr_nz (addr), paddr_nz (pc));
1209 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1211 fi->next = create_sentinel_frame (get_current_regcache ());
1213 /* Set/update this frame's cached PC value, found in the next frame.
1214 Do this before looking for this frame's unwinder. A sniffer is
1215 very likely to read this, and the corresponding unwinder is
1216 entitled to rely that the PC doesn't magically change. */
1217 fi->next->prev_pc.value = pc;
1218 fi->next->prev_pc.p = 1;
1220 /* Select/initialize both the unwind function and the frame's type
1222 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1225 fi->this_id.value = frame_id_build (addr, pc);
1229 fprintf_unfiltered (gdb_stdlog, "-> ");
1230 fprint_frame (gdb_stdlog, fi);
1231 fprintf_unfiltered (gdb_stdlog, " }\n");
1237 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1238 innermost frame). Be careful to not fall off the bottom of the
1239 frame chain and onto the sentinel frame. */
1242 get_next_frame (struct frame_info *this_frame)
1244 if (this_frame->level > 0)
1245 return this_frame->next;
1250 /* Observer for the target_changed event. */
1253 frame_observer_target_changed (struct target_ops *target)
1255 reinit_frame_cache ();
1258 /* Flush the entire frame cache. */
1261 reinit_frame_cache (void)
1263 struct frame_info *fi;
1265 /* Tear down all frame caches. */
1266 for (fi = current_frame; fi != NULL; fi = fi->prev)
1268 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1269 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1270 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1271 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1274 /* Since we can't really be sure what the first object allocated was */
1275 obstack_free (&frame_cache_obstack, 0);
1276 obstack_init (&frame_cache_obstack);
1278 if (current_frame != NULL)
1279 annotate_frames_invalid ();
1281 current_frame = NULL; /* Invalidate cache */
1282 select_frame (NULL);
1284 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1287 /* Find where a register is saved (in memory or another register).
1288 The result of frame_register_unwind is just where it is saved
1289 relative to this particular frame. */
1292 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1293 int *optimizedp, enum lval_type *lvalp,
1294 CORE_ADDR *addrp, int *realnump)
1296 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1298 while (this_frame != NULL)
1300 frame_register_unwind (this_frame, regnum, optimizedp, lvalp,
1301 addrp, realnump, NULL);
1306 if (*lvalp != lval_register)
1310 this_frame = get_next_frame (this_frame);
1314 /* Return a "struct frame_info" corresponding to the frame that called
1315 THIS_FRAME. Returns NULL if there is no such frame.
1317 Unlike get_prev_frame, this function always tries to unwind the
1320 static struct frame_info *
1321 get_prev_frame_1 (struct frame_info *this_frame)
1323 struct frame_id this_id;
1324 struct gdbarch *gdbarch;
1326 gdb_assert (this_frame != NULL);
1327 gdbarch = get_frame_arch (this_frame);
1331 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1332 if (this_frame != NULL)
1333 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1335 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1336 fprintf_unfiltered (gdb_stdlog, ") ");
1339 /* Only try to do the unwind once. */
1340 if (this_frame->prev_p)
1344 fprintf_unfiltered (gdb_stdlog, "-> ");
1345 fprint_frame (gdb_stdlog, this_frame->prev);
1346 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1348 return this_frame->prev;
1351 /* If the frame unwinder hasn't been selected yet, we must do so
1352 before setting prev_p; otherwise the check for misbehaved
1353 sniffers will think that this frame's sniffer tried to unwind
1354 further (see frame_cleanup_after_sniffer). */
1355 if (this_frame->unwind == NULL)
1357 = frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1359 this_frame->prev_p = 1;
1360 this_frame->stop_reason = UNWIND_NO_REASON;
1362 /* If we are unwinding from an inline frame, all of the below tests
1363 were already performed when we unwound from the next non-inline
1364 frame. We must skip them, since we can not get THIS_FRAME's ID
1365 until we have unwound all the way down to the previous non-inline
1367 if (get_frame_type (this_frame) == INLINE_FRAME)
1368 return get_prev_frame_raw (this_frame);
1370 /* Check that this frame's ID was valid. If it wasn't, don't try to
1371 unwind to the prev frame. Be careful to not apply this test to
1372 the sentinel frame. */
1373 this_id = get_frame_id (this_frame);
1374 if (this_frame->level >= 0 && !frame_id_p (this_id))
1378 fprintf_unfiltered (gdb_stdlog, "-> ");
1379 fprint_frame (gdb_stdlog, NULL);
1380 fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1382 this_frame->stop_reason = UNWIND_NULL_ID;
1386 /* Check that this frame's ID isn't inner to (younger, below, next)
1387 the next frame. This happens when a frame unwind goes backwards.
1388 This check is valid only if this frame and the next frame are NORMAL.
1389 See the comment at frame_id_inner for details. */
1390 if (get_frame_type (this_frame) == NORMAL_FRAME
1391 && this_frame->next->unwind->type == NORMAL_FRAME
1392 && frame_id_inner (get_frame_arch (this_frame->next), this_id,
1393 get_frame_id (this_frame->next)))
1397 fprintf_unfiltered (gdb_stdlog, "-> ");
1398 fprint_frame (gdb_stdlog, NULL);
1399 fprintf_unfiltered (gdb_stdlog, " // this frame ID is inner }\n");
1401 this_frame->stop_reason = UNWIND_INNER_ID;
1405 /* Check that this and the next frame are not identical. If they
1406 are, there is most likely a stack cycle. As with the inner-than
1407 test above, avoid comparing the inner-most and sentinel frames. */
1408 if (this_frame->level > 0
1409 && frame_id_eq (this_id, get_frame_id (this_frame->next)))
1413 fprintf_unfiltered (gdb_stdlog, "-> ");
1414 fprint_frame (gdb_stdlog, NULL);
1415 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1417 this_frame->stop_reason = UNWIND_SAME_ID;
1421 /* Check that this and the next frame do not unwind the PC register
1422 to the same memory location. If they do, then even though they
1423 have different frame IDs, the new frame will be bogus; two
1424 functions can't share a register save slot for the PC. This can
1425 happen when the prologue analyzer finds a stack adjustment, but
1428 This check does assume that the "PC register" is roughly a
1429 traditional PC, even if the gdbarch_unwind_pc method adjusts
1430 it (we do not rely on the value, only on the unwound PC being
1431 dependent on this value). A potential improvement would be
1432 to have the frame prev_pc method and the gdbarch unwind_pc
1433 method set the same lval and location information as
1434 frame_register_unwind. */
1435 if (this_frame->level > 0
1436 && gdbarch_pc_regnum (gdbarch) >= 0
1437 && get_frame_type (this_frame) == NORMAL_FRAME
1438 && (get_frame_type (this_frame->next) == NORMAL_FRAME
1439 || get_frame_type (this_frame->next) == INLINE_FRAME))
1441 int optimized, realnum, nrealnum;
1442 enum lval_type lval, nlval;
1443 CORE_ADDR addr, naddr;
1445 frame_register_unwind_location (this_frame,
1446 gdbarch_pc_regnum (gdbarch),
1447 &optimized, &lval, &addr, &realnum);
1448 frame_register_unwind_location (get_next_frame (this_frame),
1449 gdbarch_pc_regnum (gdbarch),
1450 &optimized, &nlval, &naddr, &nrealnum);
1452 if ((lval == lval_memory && lval == nlval && addr == naddr)
1453 || (lval == lval_register && lval == nlval && realnum == nrealnum))
1457 fprintf_unfiltered (gdb_stdlog, "-> ");
1458 fprint_frame (gdb_stdlog, NULL);
1459 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1462 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1463 this_frame->prev = NULL;
1468 return get_prev_frame_raw (this_frame);
1471 /* Construct a new "struct frame_info" and link it previous to
1474 static struct frame_info *
1475 get_prev_frame_raw (struct frame_info *this_frame)
1477 struct frame_info *prev_frame;
1479 /* Allocate the new frame but do not wire it in to the frame chain.
1480 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1481 frame->next to pull some fancy tricks (of course such code is, by
1482 definition, recursive). Try to prevent it.
1484 There is no reason to worry about memory leaks, should the
1485 remainder of the function fail. The allocated memory will be
1486 quickly reclaimed when the frame cache is flushed, and the `we've
1487 been here before' check above will stop repeated memory
1488 allocation calls. */
1489 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1490 prev_frame->level = this_frame->level + 1;
1492 /* Don't yet compute ->unwind (and hence ->type). It is computed
1493 on-demand in get_frame_type, frame_register_unwind, and
1496 /* Don't yet compute the frame's ID. It is computed on-demand by
1499 /* The unwound frame ID is validate at the start of this function,
1500 as part of the logic to decide if that frame should be further
1501 unwound, and not here while the prev frame is being created.
1502 Doing this makes it possible for the user to examine a frame that
1503 has an invalid frame ID.
1505 Some very old VAX code noted: [...] For the sake of argument,
1506 suppose that the stack is somewhat trashed (which is one reason
1507 that "info frame" exists). So, return 0 (indicating we don't
1508 know the address of the arglist) if we don't know what frame this
1512 this_frame->prev = prev_frame;
1513 prev_frame->next = this_frame;
1517 fprintf_unfiltered (gdb_stdlog, "-> ");
1518 fprint_frame (gdb_stdlog, prev_frame);
1519 fprintf_unfiltered (gdb_stdlog, " }\n");
1525 /* Debug routine to print a NULL frame being returned. */
1528 frame_debug_got_null_frame (struct frame_info *this_frame,
1533 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1534 if (this_frame != NULL)
1535 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1537 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1538 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1542 /* Is this (non-sentinel) frame in the "main"() function? */
1545 inside_main_func (struct frame_info *this_frame)
1547 struct minimal_symbol *msymbol;
1550 if (symfile_objfile == 0)
1552 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1553 if (msymbol == NULL)
1555 /* Make certain that the code, and not descriptor, address is
1557 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
1558 SYMBOL_VALUE_ADDRESS (msymbol),
1560 return maddr == get_frame_func (this_frame);
1563 /* Test whether THIS_FRAME is inside the process entry point function. */
1566 inside_entry_func (struct frame_info *this_frame)
1568 return (get_frame_func (this_frame) == entry_point_address ());
1571 /* Return a structure containing various interesting information about
1572 the frame that called THIS_FRAME. Returns NULL if there is entier
1573 no such frame or the frame fails any of a set of target-independent
1574 condition that should terminate the frame chain (e.g., as unwinding
1577 This function should not contain target-dependent tests, such as
1578 checking whether the program-counter is zero. */
1581 get_prev_frame (struct frame_info *this_frame)
1583 struct frame_info *prev_frame;
1585 /* There is always a frame. If this assertion fails, suspect that
1586 something should be calling get_selected_frame() or
1587 get_current_frame(). */
1588 gdb_assert (this_frame != NULL);
1590 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1591 sense to stop unwinding at a dummy frame. One place where a dummy
1592 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1593 pcsqh register (space register for the instruction at the head of the
1594 instruction queue) cannot be written directly; the only way to set it
1595 is to branch to code that is in the target space. In order to implement
1596 frame dummies on HPUX, the called function is made to jump back to where
1597 the inferior was when the user function was called. If gdb was inside
1598 the main function when we created the dummy frame, the dummy frame will
1599 point inside the main function. */
1600 if (this_frame->level >= 0
1601 && get_frame_type (this_frame) == NORMAL_FRAME
1602 && !backtrace_past_main
1603 && inside_main_func (this_frame))
1604 /* Don't unwind past main(). Note, this is done _before_ the
1605 frame has been marked as previously unwound. That way if the
1606 user later decides to enable unwinds past main(), that will
1607 automatically happen. */
1609 frame_debug_got_null_frame (this_frame, "inside main func");
1613 /* If the user's backtrace limit has been exceeded, stop. We must
1614 add two to the current level; one of those accounts for backtrace_limit
1615 being 1-based and the level being 0-based, and the other accounts for
1616 the level of the new frame instead of the level of the current
1618 if (this_frame->level + 2 > backtrace_limit)
1620 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
1624 /* If we're already inside the entry function for the main objfile,
1625 then it isn't valid. Don't apply this test to a dummy frame -
1626 dummy frame PCs typically land in the entry func. Don't apply
1627 this test to the sentinel frame. Sentinel frames should always
1628 be allowed to unwind. */
1629 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1630 wasn't checking for "main" in the minimal symbols. With that
1631 fixed asm-source tests now stop in "main" instead of halting the
1632 backtrace in weird and wonderful ways somewhere inside the entry
1633 file. Suspect that tests for inside the entry file/func were
1634 added to work around that (now fixed) case. */
1635 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1636 suggested having the inside_entry_func test use the
1637 inside_main_func() msymbol trick (along with entry_point_address()
1638 I guess) to determine the address range of the start function.
1639 That should provide a far better stopper than the current
1641 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1642 applied tail-call optimizations to main so that a function called
1643 from main returns directly to the caller of main. Since we don't
1644 stop at main, we should at least stop at the entry point of the
1646 if (this_frame->level >= 0
1647 && get_frame_type (this_frame) == NORMAL_FRAME
1648 && !backtrace_past_entry
1649 && inside_entry_func (this_frame))
1651 frame_debug_got_null_frame (this_frame, "inside entry func");
1655 /* Assume that the only way to get a zero PC is through something
1656 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1657 will never unwind a zero PC. */
1658 if (this_frame->level > 0
1659 && (get_frame_type (this_frame) == NORMAL_FRAME
1660 || get_frame_type (this_frame) == INLINE_FRAME)
1661 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1662 && get_frame_pc (this_frame) == 0)
1664 frame_debug_got_null_frame (this_frame, "zero PC");
1668 return get_prev_frame_1 (this_frame);
1672 get_frame_pc (struct frame_info *frame)
1674 gdb_assert (frame->next != NULL);
1675 return frame_unwind_pc (frame->next);
1678 /* Return an address that falls within THIS_FRAME's code block. */
1681 get_frame_address_in_block (struct frame_info *this_frame)
1683 /* A draft address. */
1684 CORE_ADDR pc = get_frame_pc (this_frame);
1686 struct frame_info *next_frame = this_frame->next;
1688 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1689 Normally the resume address is inside the body of the function
1690 associated with THIS_FRAME, but there is a special case: when
1691 calling a function which the compiler knows will never return
1692 (for instance abort), the call may be the very last instruction
1693 in the calling function. The resume address will point after the
1694 call and may be at the beginning of a different function
1697 If THIS_FRAME is a signal frame or dummy frame, then we should
1698 not adjust the unwound PC. For a dummy frame, GDB pushed the
1699 resume address manually onto the stack. For a signal frame, the
1700 OS may have pushed the resume address manually and invoked the
1701 handler (e.g. GNU/Linux), or invoked the trampoline which called
1702 the signal handler - but in either case the signal handler is
1703 expected to return to the trampoline. So in both of these
1704 cases we know that the resume address is executable and
1705 related. So we only need to adjust the PC if THIS_FRAME
1706 is a normal function.
1708 If the program has been interrupted while THIS_FRAME is current,
1709 then clearly the resume address is inside the associated
1710 function. There are three kinds of interruption: debugger stop
1711 (next frame will be SENTINEL_FRAME), operating system
1712 signal or exception (next frame will be SIGTRAMP_FRAME),
1713 or debugger-induced function call (next frame will be
1714 DUMMY_FRAME). So we only need to adjust the PC if
1715 NEXT_FRAME is a normal function.
1717 We check the type of NEXT_FRAME first, since it is already
1718 known; frame type is determined by the unwinder, and since
1719 we have THIS_FRAME we've already selected an unwinder for
1722 If the next frame is inlined, we need to keep going until we find
1723 the real function - for instance, if a signal handler is invoked
1724 while in an inlined function, then the code address of the
1725 "calling" normal function should not be adjusted either. */
1727 while (get_frame_type (next_frame) == INLINE_FRAME)
1728 next_frame = next_frame->next;
1730 if (get_frame_type (next_frame) == NORMAL_FRAME
1731 && (get_frame_type (this_frame) == NORMAL_FRAME
1732 || get_frame_type (this_frame) == INLINE_FRAME))
1739 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1741 struct frame_info *next_frame;
1744 /* If the next frame represents an inlined function call, this frame's
1745 sal is the "call site" of that inlined function, which can not
1746 be inferred from get_frame_pc. */
1747 next_frame = get_next_frame (frame);
1748 if (frame_inlined_callees (frame) > 0)
1753 sym = get_frame_function (next_frame);
1755 sym = inline_skipped_symbol (inferior_ptid);
1758 if (SYMBOL_LINE (sym) != 0)
1760 sal->symtab = SYMBOL_SYMTAB (sym);
1761 sal->line = SYMBOL_LINE (sym);
1764 /* If the symbol does not have a location, we don't know where
1765 the call site is. Do not pretend to. This is jarring, but
1766 we can't do much better. */
1767 sal->pc = get_frame_pc (frame);
1772 /* If FRAME is not the innermost frame, that normally means that
1773 FRAME->pc points at the return instruction (which is *after* the
1774 call instruction), and we want to get the line containing the
1775 call (because the call is where the user thinks the program is).
1776 However, if the next frame is either a SIGTRAMP_FRAME or a
1777 DUMMY_FRAME, then the next frame will contain a saved interrupt
1778 PC and such a PC indicates the current (rather than next)
1779 instruction/line, consequently, for such cases, want to get the
1780 line containing fi->pc. */
1781 notcurrent = (get_frame_pc (frame) != get_frame_address_in_block (frame));
1782 (*sal) = find_pc_line (get_frame_pc (frame), notcurrent);
1785 /* Per "frame.h", return the ``address'' of the frame. Code should
1786 really be using get_frame_id(). */
1788 get_frame_base (struct frame_info *fi)
1790 return get_frame_id (fi).stack_addr;
1793 /* High-level offsets into the frame. Used by the debug info. */
1796 get_frame_base_address (struct frame_info *fi)
1798 if (get_frame_type (fi) != NORMAL_FRAME)
1800 if (fi->base == NULL)
1801 fi->base = frame_base_find_by_frame (fi);
1802 /* Sneaky: If the low-level unwind and high-level base code share a
1803 common unwinder, let them share the prologue cache. */
1804 if (fi->base->unwind == fi->unwind)
1805 return fi->base->this_base (fi, &fi->prologue_cache);
1806 return fi->base->this_base (fi, &fi->base_cache);
1810 get_frame_locals_address (struct frame_info *fi)
1813 if (get_frame_type (fi) != NORMAL_FRAME)
1815 /* If there isn't a frame address method, find it. */
1816 if (fi->base == NULL)
1817 fi->base = frame_base_find_by_frame (fi);
1818 /* Sneaky: If the low-level unwind and high-level base code share a
1819 common unwinder, let them share the prologue cache. */
1820 if (fi->base->unwind == fi->unwind)
1821 return fi->base->this_locals (fi, &fi->prologue_cache);
1822 return fi->base->this_locals (fi, &fi->base_cache);
1826 get_frame_args_address (struct frame_info *fi)
1829 if (get_frame_type (fi) != NORMAL_FRAME)
1831 /* If there isn't a frame address method, find it. */
1832 if (fi->base == NULL)
1833 fi->base = frame_base_find_by_frame (fi);
1834 /* Sneaky: If the low-level unwind and high-level base code share a
1835 common unwinder, let them share the prologue cache. */
1836 if (fi->base->unwind == fi->unwind)
1837 return fi->base->this_args (fi, &fi->prologue_cache);
1838 return fi->base->this_args (fi, &fi->base_cache);
1841 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1842 or -1 for a NULL frame. */
1845 frame_relative_level (struct frame_info *fi)
1854 get_frame_type (struct frame_info *frame)
1856 if (frame->unwind == NULL)
1857 /* Initialize the frame's unwinder because that's what
1858 provides the frame's type. */
1859 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
1860 return frame->unwind->type;
1863 /* Memory access methods. */
1866 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
1867 gdb_byte *buf, int len)
1869 read_memory (addr, buf, len);
1873 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1876 return read_memory_integer (addr, len);
1880 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1883 return read_memory_unsigned_integer (addr, len);
1887 safe_frame_unwind_memory (struct frame_info *this_frame,
1888 CORE_ADDR addr, gdb_byte *buf, int len)
1890 /* NOTE: target_read_memory returns zero on success! */
1891 return !target_read_memory (addr, buf, len);
1894 /* Architecture methods. */
1897 get_frame_arch (struct frame_info *this_frame)
1899 return frame_unwind_arch (this_frame->next);
1903 frame_unwind_arch (struct frame_info *next_frame)
1905 if (!next_frame->prev_arch.p)
1907 struct gdbarch *arch;
1909 if (next_frame->unwind == NULL)
1911 = frame_unwind_find_by_frame (next_frame,
1912 &next_frame->prologue_cache);
1914 if (next_frame->unwind->prev_arch != NULL)
1915 arch = next_frame->unwind->prev_arch (next_frame,
1916 &next_frame->prologue_cache);
1918 arch = get_frame_arch (next_frame);
1920 next_frame->prev_arch.arch = arch;
1921 next_frame->prev_arch.p = 1;
1923 fprintf_unfiltered (gdb_stdlog,
1924 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
1926 gdbarch_bfd_arch_info (arch)->printable_name);
1929 return next_frame->prev_arch.arch;
1933 frame_unwind_caller_arch (struct frame_info *next_frame)
1935 return frame_unwind_arch (skip_inlined_frames (next_frame));
1938 /* Stack pointer methods. */
1941 get_frame_sp (struct frame_info *this_frame)
1943 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1944 /* Normality - an architecture that provides a way of obtaining any
1945 frame inner-most address. */
1946 if (gdbarch_unwind_sp_p (gdbarch))
1947 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
1948 operate on THIS_FRAME now. */
1949 return gdbarch_unwind_sp (gdbarch, this_frame->next);
1950 /* Now things are really are grim. Hope that the value returned by
1951 the gdbarch_sp_regnum register is meaningful. */
1952 if (gdbarch_sp_regnum (gdbarch) >= 0)
1953 return get_frame_register_unsigned (this_frame,
1954 gdbarch_sp_regnum (gdbarch));
1955 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
1958 /* Return the reason why we can't unwind past FRAME. */
1960 enum unwind_stop_reason
1961 get_frame_unwind_stop_reason (struct frame_info *frame)
1963 /* If we haven't tried to unwind past this point yet, then assume
1964 that unwinding would succeed. */
1965 if (frame->prev_p == 0)
1966 return UNWIND_NO_REASON;
1968 /* Otherwise, we set a reason when we succeeded (or failed) to
1970 return frame->stop_reason;
1973 /* Return a string explaining REASON. */
1976 frame_stop_reason_string (enum unwind_stop_reason reason)
1980 case UNWIND_NULL_ID:
1981 return _("unwinder did not report frame ID");
1983 case UNWIND_INNER_ID:
1984 return _("previous frame inner to this frame (corrupt stack?)");
1986 case UNWIND_SAME_ID:
1987 return _("previous frame identical to this frame (corrupt stack?)");
1989 case UNWIND_NO_SAVED_PC:
1990 return _("frame did not save the PC");
1992 case UNWIND_NO_REASON:
1993 case UNWIND_FIRST_ERROR:
1995 internal_error (__FILE__, __LINE__,
1996 "Invalid frame stop reason");
2000 /* Clean up after a failed (wrong unwinder) attempt to unwind past
2004 frame_cleanup_after_sniffer (void *arg)
2006 struct frame_info *frame = arg;
2008 /* The sniffer should not allocate a prologue cache if it did not
2009 match this frame. */
2010 gdb_assert (frame->prologue_cache == NULL);
2012 /* No sniffer should extend the frame chain; sniff based on what is
2014 gdb_assert (!frame->prev_p);
2016 /* The sniffer should not check the frame's ID; that's circular. */
2017 gdb_assert (!frame->this_id.p);
2019 /* Clear cached fields dependent on the unwinder.
2021 The previous PC is independent of the unwinder, but the previous
2022 function is not (see get_frame_address_in_block). */
2023 frame->prev_func.p = 0;
2024 frame->prev_func.addr = 0;
2026 /* Discard the unwinder last, so that we can easily find it if an assertion
2027 in this function triggers. */
2028 frame->unwind = NULL;
2031 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2032 Return a cleanup which should be called if unwinding fails, and
2033 discarded if it succeeds. */
2036 frame_prepare_for_sniffer (struct frame_info *frame,
2037 const struct frame_unwind *unwind)
2039 gdb_assert (frame->unwind == NULL);
2040 frame->unwind = unwind;
2041 return make_cleanup (frame_cleanup_after_sniffer, frame);
2044 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2046 static struct cmd_list_element *set_backtrace_cmdlist;
2047 static struct cmd_list_element *show_backtrace_cmdlist;
2050 set_backtrace_cmd (char *args, int from_tty)
2052 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2056 show_backtrace_cmd (char *args, int from_tty)
2058 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2062 _initialize_frame (void)
2064 obstack_init (&frame_cache_obstack);
2066 observer_attach_target_changed (frame_observer_target_changed);
2068 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
2069 Set backtrace specific variables.\n\
2070 Configure backtrace variables such as the backtrace limit"),
2071 &set_backtrace_cmdlist, "set backtrace ",
2072 0/*allow-unknown*/, &setlist);
2073 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
2074 Show backtrace specific variables\n\
2075 Show backtrace variables such as the backtrace limit"),
2076 &show_backtrace_cmdlist, "show backtrace ",
2077 0/*allow-unknown*/, &showlist);
2079 add_setshow_boolean_cmd ("past-main", class_obscure,
2080 &backtrace_past_main, _("\
2081 Set whether backtraces should continue past \"main\"."), _("\
2082 Show whether backtraces should continue past \"main\"."), _("\
2083 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2084 the backtrace at \"main\". Set this variable if you need to see the rest\n\
2085 of the stack trace."),
2087 show_backtrace_past_main,
2088 &set_backtrace_cmdlist,
2089 &show_backtrace_cmdlist);
2091 add_setshow_boolean_cmd ("past-entry", class_obscure,
2092 &backtrace_past_entry, _("\
2093 Set whether backtraces should continue past the entry point of a program."),
2095 Show whether backtraces should continue past the entry point of a program."),
2097 Normally there are no callers beyond the entry point of a program, so GDB\n\
2098 will terminate the backtrace there. Set this variable if you need to see \n\
2099 the rest of the stack trace."),
2101 show_backtrace_past_entry,
2102 &set_backtrace_cmdlist,
2103 &show_backtrace_cmdlist);
2105 add_setshow_integer_cmd ("limit", class_obscure,
2106 &backtrace_limit, _("\
2107 Set an upper bound on the number of backtrace levels."), _("\
2108 Show the upper bound on the number of backtrace levels."), _("\
2109 No more than the specified number of frames can be displayed or examined.\n\
2110 Zero is unlimited."),
2112 show_backtrace_limit,
2113 &set_backtrace_cmdlist,
2114 &show_backtrace_cmdlist);
2116 /* Debug this files internals. */
2117 add_setshow_zinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
2118 Set frame debugging."), _("\
2119 Show frame debugging."), _("\
2120 When non-zero, frame specific internal debugging is enabled."),
2123 &setdebuglist, &showdebuglist);