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 resume address. */
87 /* Cached copy of the previous frame's function address. */
94 /* This frame's ID. */
98 struct frame_id value;
101 /* The frame's high-level base methods, and corresponding cache.
102 The high level base methods are selected based on the frame's
104 const struct frame_base *base;
107 /* Pointers to the next (down, inner, younger) and previous (up,
108 outer, older) frame_info's in the frame cache. */
109 struct frame_info *next; /* down, inner, younger */
111 struct frame_info *prev; /* up, outer, older */
113 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
114 could. Only valid when PREV_P is set. */
115 enum unwind_stop_reason stop_reason;
118 /* Flag to control debugging. */
122 show_frame_debug (struct ui_file *file, int from_tty,
123 struct cmd_list_element *c, const char *value)
125 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
128 /* Flag to indicate whether backtraces should stop at main et.al. */
130 static int backtrace_past_main;
132 show_backtrace_past_main (struct ui_file *file, int from_tty,
133 struct cmd_list_element *c, const char *value)
135 fprintf_filtered (file, _("\
136 Whether backtraces should continue past \"main\" is %s.\n"),
140 static int backtrace_past_entry;
142 show_backtrace_past_entry (struct ui_file *file, int from_tty,
143 struct cmd_list_element *c, const char *value)
145 fprintf_filtered (file, _("\
146 Whether backtraces should continue past the entry point of a program is %s.\n"),
150 static int backtrace_limit = INT_MAX;
152 show_backtrace_limit (struct ui_file *file, int from_tty,
153 struct cmd_list_element *c, const char *value)
155 fprintf_filtered (file, _("\
156 An upper bound on the number of backtrace levels is %s.\n"),
162 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
165 fprintf_unfiltered (file, "%s=0x%s", name, paddr_nz (addr));
167 fprintf_unfiltered (file, "!%s", name);
171 fprint_frame_id (struct ui_file *file, struct frame_id id)
173 fprintf_unfiltered (file, "{");
174 fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
175 fprintf_unfiltered (file, ",");
176 fprint_field (file, "code", id.code_addr_p, id.code_addr);
177 fprintf_unfiltered (file, ",");
178 fprint_field (file, "special", id.special_addr_p, id.special_addr);
180 fprintf_unfiltered (file, ",inlined=%d", id.inline_depth);
181 fprintf_unfiltered (file, "}");
185 fprint_frame_type (struct ui_file *file, enum frame_type type)
190 fprintf_unfiltered (file, "NORMAL_FRAME");
193 fprintf_unfiltered (file, "DUMMY_FRAME");
196 fprintf_unfiltered (file, "INLINE_FRAME");
199 fprintf_unfiltered (file, "SENTINEL_FRAME");
202 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
205 fprintf_unfiltered (file, "<unknown type>");
211 fprint_frame (struct ui_file *file, struct frame_info *fi)
215 fprintf_unfiltered (file, "<NULL frame>");
218 fprintf_unfiltered (file, "{");
219 fprintf_unfiltered (file, "level=%d", fi->level);
220 fprintf_unfiltered (file, ",");
221 fprintf_unfiltered (file, "type=");
222 if (fi->unwind != NULL)
223 fprint_frame_type (file, fi->unwind->type);
225 fprintf_unfiltered (file, "<unknown>");
226 fprintf_unfiltered (file, ",");
227 fprintf_unfiltered (file, "unwind=");
228 if (fi->unwind != NULL)
229 gdb_print_host_address (fi->unwind, file);
231 fprintf_unfiltered (file, "<unknown>");
232 fprintf_unfiltered (file, ",");
233 fprintf_unfiltered (file, "pc=");
234 if (fi->next != NULL && fi->next->prev_pc.p)
235 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
237 fprintf_unfiltered (file, "<unknown>");
238 fprintf_unfiltered (file, ",");
239 fprintf_unfiltered (file, "id=");
241 fprint_frame_id (file, fi->this_id.value);
243 fprintf_unfiltered (file, "<unknown>");
244 fprintf_unfiltered (file, ",");
245 fprintf_unfiltered (file, "func=");
246 if (fi->next != NULL && fi->next->prev_func.p)
247 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
249 fprintf_unfiltered (file, "<unknown>");
250 fprintf_unfiltered (file, "}");
253 /* Given FRAME, return the enclosing normal frame for inlined
254 function frames. Otherwise return the original frame. */
256 static struct frame_info *
257 skip_inlined_frames (struct frame_info *frame)
259 while (get_frame_type (frame) == INLINE_FRAME)
260 frame = get_prev_frame (frame);
265 /* Return a frame uniq ID that can be used to, later, re-find the
269 get_frame_id (struct frame_info *fi)
273 return null_frame_id;
278 fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
280 /* Find the unwinder. */
281 if (fi->unwind == NULL)
282 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
283 /* Find THIS frame's ID. */
284 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
288 fprintf_unfiltered (gdb_stdlog, "-> ");
289 fprint_frame_id (gdb_stdlog, fi->this_id.value);
290 fprintf_unfiltered (gdb_stdlog, " }\n");
293 return fi->this_id.value;
297 get_stack_frame_id (struct frame_info *next_frame)
299 return get_frame_id (skip_inlined_frames (next_frame));
303 frame_unwind_caller_id (struct frame_info *next_frame)
305 struct frame_info *this_frame;
307 /* Use get_prev_frame_1, and not get_prev_frame. The latter will truncate
308 the frame chain, leading to this function unintentionally
309 returning a null_frame_id (e.g., when a caller requests the frame
310 ID of "main()"s caller. */
312 next_frame = skip_inlined_frames (next_frame);
313 this_frame = get_prev_frame_1 (next_frame);
315 return get_frame_id (skip_inlined_frames (this_frame));
317 return null_frame_id;
320 const struct frame_id null_frame_id; /* All zeros. */
323 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
324 CORE_ADDR special_addr)
326 struct frame_id id = null_frame_id;
327 id.stack_addr = stack_addr;
329 id.code_addr = code_addr;
331 id.special_addr = special_addr;
332 id.special_addr_p = 1;
337 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
339 struct frame_id id = null_frame_id;
340 id.stack_addr = stack_addr;
342 id.code_addr = code_addr;
348 frame_id_build_wild (CORE_ADDR stack_addr)
350 struct frame_id id = null_frame_id;
351 id.stack_addr = stack_addr;
357 frame_id_p (struct frame_id l)
360 /* The frame is valid iff it has a valid stack address. */
364 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
365 fprint_frame_id (gdb_stdlog, l);
366 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
372 frame_id_inlined_p (struct frame_id l)
377 return (l.inline_depth != 0);
381 frame_id_eq (struct frame_id l, struct frame_id r)
384 if (!l.stack_addr_p || !r.stack_addr_p)
385 /* Like a NaN, if either ID is invalid, the result is false.
386 Note that a frame ID is invalid iff it is the null frame ID. */
388 else if (l.stack_addr != r.stack_addr)
389 /* If .stack addresses are different, the frames are different. */
391 else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
392 /* An invalid code addr is a wild card. If .code addresses are
393 different, the frames are different. */
395 else if (l.special_addr_p && r.special_addr_p
396 && l.special_addr != r.special_addr)
397 /* An invalid special addr is a wild card (or unused). Otherwise
398 if special addresses are different, the frames are different. */
400 else if (l.inline_depth != r.inline_depth)
401 /* If inline depths are different, the frames must be different. */
404 /* Frames are equal. */
409 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
410 fprint_frame_id (gdb_stdlog, l);
411 fprintf_unfiltered (gdb_stdlog, ",r=");
412 fprint_frame_id (gdb_stdlog, r);
413 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
418 /* Safety net to check whether frame ID L should be inner to
419 frame ID R, according to their stack addresses.
421 This method cannot be used to compare arbitrary frames, as the
422 ranges of valid stack addresses may be discontiguous (e.g. due
425 However, it can be used as safety net to discover invalid frame
426 IDs in certain circumstances. Assuming that NEXT is the immediate
427 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
429 * The stack address of NEXT must be inner-than-or-equal to the stack
432 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
435 * If NEXT and THIS have different stack addresses, no other frame
436 in the frame chain may have a stack address in between.
438 Therefore, if frame_id_inner (TEST, THIS) holds, but
439 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
440 to a valid frame in the frame chain.
442 The sanity checks above cannot be performed when a SIGTRAMP frame
443 is involved, because signal handlers might be executed on a different
444 stack than the stack used by the routine that caused the signal
445 to be raised. This can happen for instance when a thread exceeds
446 its maximum stack size. In this case, certain compilers implement
447 a stack overflow strategy that cause the handler to be run on a
451 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
454 if (!l.stack_addr_p || !r.stack_addr_p)
455 /* Like NaN, any operation involving an invalid ID always fails. */
457 else if (l.inline_depth > r.inline_depth
458 && l.stack_addr == r.stack_addr
459 && l.code_addr_p == r.code_addr_p
460 && l.special_addr_p == r.special_addr_p
461 && l.special_addr == r.special_addr)
463 /* Same function, different inlined functions. */
464 struct block *lb, *rb;
466 gdb_assert (l.code_addr_p && r.code_addr_p);
468 lb = block_for_pc (l.code_addr);
469 rb = block_for_pc (r.code_addr);
471 if (lb == NULL || rb == NULL)
472 /* Something's gone wrong. */
475 /* This will return true if LB and RB are the same block, or
476 if the block with the smaller depth lexically encloses the
477 block with the greater depth. */
478 inner = contained_in (lb, rb);
481 /* Only return non-zero when strictly inner than. Note that, per
482 comment in "frame.h", there is some fuzz here. Frameless
483 functions are not strictly inner than (same .stack but
484 different .code and/or .special address). */
485 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
488 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
489 fprint_frame_id (gdb_stdlog, l);
490 fprintf_unfiltered (gdb_stdlog, ",r=");
491 fprint_frame_id (gdb_stdlog, r);
492 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
498 frame_find_by_id (struct frame_id id)
500 struct frame_info *frame, *prev_frame;
502 /* ZERO denotes the null frame, let the caller decide what to do
503 about it. Should it instead return get_current_frame()? */
504 if (!frame_id_p (id))
507 for (frame = get_current_frame (); ; frame = prev_frame)
509 struct frame_id this = get_frame_id (frame);
510 if (frame_id_eq (id, this))
511 /* An exact match. */
514 prev_frame = get_prev_frame (frame);
518 /* As a safety net to avoid unnecessary backtracing while trying
519 to find an invalid ID, we check for a common situation where
520 we can detect from comparing stack addresses that no other
521 frame in the current frame chain can have this ID. See the
522 comment at frame_id_inner for details. */
523 if (get_frame_type (frame) == NORMAL_FRAME
524 && !frame_id_inner (get_frame_arch (frame), id, this)
525 && frame_id_inner (get_frame_arch (prev_frame), id,
526 get_frame_id (prev_frame)))
533 frame_unwind_pc (struct frame_info *this_frame)
535 if (!this_frame->prev_pc.p)
538 if (gdbarch_unwind_pc_p (get_frame_arch (this_frame)))
540 /* The right way. The `pure' way. The one true way. This
541 method depends solely on the register-unwind code to
542 determine the value of registers in THIS frame, and hence
543 the value of this frame's PC (resume address). A typical
544 implementation is no more than:
546 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
547 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
549 Note: this method is very heavily dependent on a correct
550 register-unwind implementation, it pays to fix that
551 method first; this method is frame type agnostic, since
552 it only deals with register values, it works with any
553 frame. This is all in stark contrast to the old
554 FRAME_SAVED_PC which would try to directly handle all the
555 different ways that a PC could be unwound. */
556 pc = gdbarch_unwind_pc (get_frame_arch (this_frame), this_frame);
559 internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
560 this_frame->prev_pc.value = pc;
561 this_frame->prev_pc.p = 1;
563 fprintf_unfiltered (gdb_stdlog,
564 "{ frame_unwind_caller_pc (this_frame=%d) -> 0x%s }\n",
566 paddr_nz (this_frame->prev_pc.value));
568 return this_frame->prev_pc.value;
572 frame_unwind_caller_pc (struct frame_info *this_frame)
574 return frame_unwind_pc (skip_inlined_frames (this_frame));
578 get_frame_func (struct frame_info *this_frame)
580 struct frame_info *next_frame = this_frame->next;
582 if (!next_frame->prev_func.p)
584 /* Make certain that this, and not the adjacent, function is
586 CORE_ADDR addr_in_block = get_frame_address_in_block (this_frame);
587 next_frame->prev_func.p = 1;
588 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
590 fprintf_unfiltered (gdb_stdlog,
591 "{ get_frame_func (this_frame=%d) -> 0x%s }\n",
593 paddr_nz (next_frame->prev_func.addr));
595 return next_frame->prev_func.addr;
599 do_frame_register_read (void *src, int regnum, gdb_byte *buf)
601 return frame_register_read (src, regnum, buf);
605 frame_save_as_regcache (struct frame_info *this_frame)
607 struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame));
608 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
609 regcache_save (regcache, do_frame_register_read, this_frame);
610 discard_cleanups (cleanups);
615 frame_pop (struct frame_info *this_frame)
617 struct frame_info *prev_frame;
618 struct regcache *scratch;
619 struct cleanup *cleanups;
621 if (get_frame_type (this_frame) == DUMMY_FRAME)
623 /* Popping a dummy frame involves restoring more than just registers.
624 dummy_frame_pop does all the work. */
625 dummy_frame_pop (get_frame_id (this_frame));
629 /* Ensure that we have a frame to pop to. */
630 prev_frame = get_prev_frame_1 (this_frame);
633 error (_("Cannot pop the initial frame."));
635 /* Make a copy of all the register values unwound from this frame.
636 Save them in a scratch buffer so that there isn't a race between
637 trying to extract the old values from the current regcache while
638 at the same time writing new values into that same cache. */
639 scratch = frame_save_as_regcache (prev_frame);
640 cleanups = make_cleanup_regcache_xfree (scratch);
642 /* FIXME: cagney/2003-03-16: It should be possible to tell the
643 target's register cache that it is about to be hit with a burst
644 register transfer and that the sequence of register writes should
645 be batched. The pair target_prepare_to_store() and
646 target_store_registers() kind of suggest this functionality.
647 Unfortunately, they don't implement it. Their lack of a formal
648 definition can lead to targets writing back bogus values
649 (arguably a bug in the target code mind). */
650 /* Now copy those saved registers into the current regcache.
651 Here, regcache_cpy() calls regcache_restore(). */
652 regcache_cpy (get_current_regcache (), scratch);
653 do_cleanups (cleanups);
655 /* We've made right mess of GDB's local state, just discard
657 reinit_frame_cache ();
661 frame_register_unwind (struct frame_info *frame, int regnum,
662 int *optimizedp, enum lval_type *lvalp,
663 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
667 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
668 that the value proper does not need to be fetched. */
669 gdb_assert (optimizedp != NULL);
670 gdb_assert (lvalp != NULL);
671 gdb_assert (addrp != NULL);
672 gdb_assert (realnump != NULL);
673 /* gdb_assert (bufferp != NULL); */
675 value = frame_unwind_register_value (frame, regnum);
677 gdb_assert (value != NULL);
679 *optimizedp = value_optimized_out (value);
680 *lvalp = VALUE_LVAL (value);
681 *addrp = value_address (value);
682 *realnump = VALUE_REGNUM (value);
685 memcpy (bufferp, value_contents_all (value),
686 TYPE_LENGTH (value_type (value)));
688 /* Dispose of the new value. This prevents watchpoints from
689 trying to watch the saved frame pointer. */
690 release_value (value);
695 frame_register (struct frame_info *frame, int regnum,
696 int *optimizedp, enum lval_type *lvalp,
697 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
699 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
700 that the value proper does not need to be fetched. */
701 gdb_assert (optimizedp != NULL);
702 gdb_assert (lvalp != NULL);
703 gdb_assert (addrp != NULL);
704 gdb_assert (realnump != NULL);
705 /* gdb_assert (bufferp != NULL); */
707 /* Obtain the register value by unwinding the register from the next
708 (more inner frame). */
709 gdb_assert (frame != NULL && frame->next != NULL);
710 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
715 frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
721 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
726 get_frame_register (struct frame_info *frame,
727 int regnum, gdb_byte *buf)
729 frame_unwind_register (frame->next, regnum, buf);
733 frame_unwind_register_value (struct frame_info *frame, int regnum)
737 gdb_assert (frame != NULL);
741 fprintf_unfiltered (gdb_stdlog, "\
742 { frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
743 frame->level, regnum,
744 user_reg_map_regnum_to_name
745 (get_frame_arch (frame), regnum));
748 /* Find the unwinder. */
749 if (frame->unwind == NULL)
750 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
752 /* Ask this frame to unwind its register. */
753 value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
757 fprintf_unfiltered (gdb_stdlog, "->");
758 if (value_optimized_out (value))
759 fprintf_unfiltered (gdb_stdlog, " optimized out");
762 if (VALUE_LVAL (value) == lval_register)
763 fprintf_unfiltered (gdb_stdlog, " register=%d",
764 VALUE_REGNUM (value));
765 else if (VALUE_LVAL (value) == lval_memory)
766 fprintf_unfiltered (gdb_stdlog, " address=0x%s",
767 paddr_nz (value_address (value)));
769 fprintf_unfiltered (gdb_stdlog, " computed");
771 if (value_lazy (value))
772 fprintf_unfiltered (gdb_stdlog, " lazy");
776 const gdb_byte *buf = value_contents (value);
778 fprintf_unfiltered (gdb_stdlog, " bytes=");
779 fprintf_unfiltered (gdb_stdlog, "[");
780 for (i = 0; i < register_size (get_frame_arch (frame), regnum); i++)
781 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
782 fprintf_unfiltered (gdb_stdlog, "]");
786 fprintf_unfiltered (gdb_stdlog, " }\n");
793 get_frame_register_value (struct frame_info *frame, int regnum)
795 return frame_unwind_register_value (frame->next, regnum);
799 frame_unwind_register_signed (struct frame_info *frame, int regnum)
801 gdb_byte buf[MAX_REGISTER_SIZE];
802 frame_unwind_register (frame, regnum, buf);
803 return extract_signed_integer (buf, register_size (get_frame_arch (frame),
808 get_frame_register_signed (struct frame_info *frame, int regnum)
810 return frame_unwind_register_signed (frame->next, regnum);
814 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
816 gdb_byte buf[MAX_REGISTER_SIZE];
817 frame_unwind_register (frame, regnum, buf);
818 return extract_unsigned_integer (buf, register_size (get_frame_arch (frame),
823 get_frame_register_unsigned (struct frame_info *frame, int regnum)
825 return frame_unwind_register_unsigned (frame->next, regnum);
829 put_frame_register (struct frame_info *frame, int regnum,
832 struct gdbarch *gdbarch = get_frame_arch (frame);
837 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
839 error (_("Attempt to assign to a value that was optimized out."));
844 /* FIXME: write_memory doesn't yet take constant buffers.
846 gdb_byte tmp[MAX_REGISTER_SIZE];
847 memcpy (tmp, buf, register_size (gdbarch, regnum));
848 write_memory (addr, tmp, register_size (gdbarch, regnum));
852 regcache_cooked_write (get_current_regcache (), realnum, buf);
855 error (_("Attempt to assign to an unmodifiable value."));
859 /* frame_register_read ()
861 Find and return the value of REGNUM for the specified stack frame.
862 The number of bytes copied is REGISTER_SIZE (REGNUM).
864 Returns 0 if the register value could not be found. */
867 frame_register_read (struct frame_info *frame, int regnum,
874 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
880 get_frame_register_bytes (struct frame_info *frame, int regnum,
881 CORE_ADDR offset, int len, gdb_byte *myaddr)
883 struct gdbarch *gdbarch = get_frame_arch (frame);
888 /* Skip registers wholly inside of OFFSET. */
889 while (offset >= register_size (gdbarch, regnum))
891 offset -= register_size (gdbarch, regnum);
895 /* Ensure that we will not read beyond the end of the register file.
896 This can only ever happen if the debug information is bad. */
898 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
899 for (i = regnum; i < numregs; i++)
901 int thissize = register_size (gdbarch, i);
903 break; /* This register is not available on this architecture. */
908 warning (_("Bad debug information detected: "
909 "Attempt to read %d bytes from registers."), len);
916 int curr_len = register_size (gdbarch, regnum) - offset;
920 if (curr_len == register_size (gdbarch, regnum))
922 if (!frame_register_read (frame, regnum, myaddr))
927 gdb_byte buf[MAX_REGISTER_SIZE];
928 if (!frame_register_read (frame, regnum, buf))
930 memcpy (myaddr, buf + offset, curr_len);
943 put_frame_register_bytes (struct frame_info *frame, int regnum,
944 CORE_ADDR offset, int len, const gdb_byte *myaddr)
946 struct gdbarch *gdbarch = get_frame_arch (frame);
948 /* Skip registers wholly inside of OFFSET. */
949 while (offset >= register_size (gdbarch, regnum))
951 offset -= register_size (gdbarch, regnum);
958 int curr_len = register_size (gdbarch, regnum) - offset;
962 if (curr_len == register_size (gdbarch, regnum))
964 put_frame_register (frame, regnum, myaddr);
968 gdb_byte buf[MAX_REGISTER_SIZE];
969 frame_register_read (frame, regnum, buf);
970 memcpy (buf + offset, myaddr, curr_len);
971 put_frame_register (frame, regnum, buf);
981 /* Create a sentinel frame. */
983 static struct frame_info *
984 create_sentinel_frame (struct regcache *regcache)
986 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
988 /* Explicitly initialize the sentinel frame's cache. Provide it
989 with the underlying regcache. In the future additional
990 information, such as the frame's thread will be added. */
991 frame->prologue_cache = sentinel_frame_cache (regcache);
992 /* For the moment there is only one sentinel frame implementation. */
993 frame->unwind = sentinel_frame_unwind;
994 /* Link this frame back to itself. The frame is self referential
995 (the unwound PC is the same as the pc), so make it so. */
997 /* Make the sentinel frame's ID valid, but invalid. That way all
998 comparisons with it should fail. */
999 frame->this_id.p = 1;
1000 frame->this_id.value = null_frame_id;
1003 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1004 fprint_frame (gdb_stdlog, frame);
1005 fprintf_unfiltered (gdb_stdlog, " }\n");
1010 /* Info about the innermost stack frame (contents of FP register) */
1012 static struct frame_info *current_frame;
1014 /* Cache for frame addresses already read by gdb. Valid only while
1015 inferior is stopped. Control variables for the frame cache should
1016 be local to this module. */
1018 static struct obstack frame_cache_obstack;
1021 frame_obstack_zalloc (unsigned long size)
1023 void *data = obstack_alloc (&frame_cache_obstack, size);
1024 memset (data, 0, size);
1028 /* Return the innermost (currently executing) stack frame. This is
1029 split into two functions. The function unwind_to_current_frame()
1030 is wrapped in catch exceptions so that, even when the unwind of the
1031 sentinel frame fails, the function still returns a stack frame. */
1034 unwind_to_current_frame (struct ui_out *ui_out, void *args)
1036 struct frame_info *frame = get_prev_frame (args);
1037 /* A sentinel frame can fail to unwind, e.g., because its PC value
1038 lands in somewhere like start. */
1041 current_frame = frame;
1046 get_current_frame (void)
1048 /* First check, and report, the lack of registers. Having GDB
1049 report "No stack!" or "No memory" when the target doesn't even
1050 have registers is very confusing. Besides, "printcmd.exp"
1051 explicitly checks that ``print $pc'' with no registers prints "No
1053 if (!target_has_registers)
1054 error (_("No registers."));
1055 if (!target_has_stack)
1056 error (_("No stack."));
1057 if (!target_has_memory)
1058 error (_("No memory."));
1059 if (ptid_equal (inferior_ptid, null_ptid))
1060 error (_("No selected thread."));
1061 if (is_exited (inferior_ptid))
1062 error (_("Invalid selected thread."));
1063 if (is_executing (inferior_ptid))
1064 error (_("Target is executing."));
1066 if (current_frame == NULL)
1068 struct frame_info *sentinel_frame =
1069 create_sentinel_frame (get_current_regcache ());
1070 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
1071 RETURN_MASK_ERROR) != 0)
1073 /* Oops! Fake a current frame? Is this useful? It has a PC
1074 of zero, for instance. */
1075 current_frame = sentinel_frame;
1078 return current_frame;
1081 /* The "selected" stack frame is used by default for local and arg
1082 access. May be zero, for no selected frame. */
1084 static struct frame_info *selected_frame;
1087 has_stack_frames (void)
1089 if (!target_has_registers || !target_has_stack || !target_has_memory)
1092 /* No current inferior, no frame. */
1093 if (ptid_equal (inferior_ptid, null_ptid))
1096 /* Don't try to read from a dead thread. */
1097 if (is_exited (inferior_ptid))
1100 /* ... or from a spinning thread. */
1101 if (is_executing (inferior_ptid))
1107 /* Return the selected frame. Always non-NULL (unless there isn't an
1108 inferior sufficient for creating a frame) in which case an error is
1112 get_selected_frame (const char *message)
1114 if (selected_frame == NULL)
1116 if (message != NULL && !has_stack_frames ())
1117 error (("%s"), message);
1118 /* Hey! Don't trust this. It should really be re-finding the
1119 last selected frame of the currently selected thread. This,
1120 though, is better than nothing. */
1121 select_frame (get_current_frame ());
1123 /* There is always a frame. */
1124 gdb_assert (selected_frame != NULL);
1125 return selected_frame;
1128 /* This is a variant of get_selected_frame() which can be called when
1129 the inferior does not have a frame; in that case it will return
1130 NULL instead of calling error(). */
1133 deprecated_safe_get_selected_frame (void)
1135 if (!has_stack_frames ())
1137 return get_selected_frame (NULL);
1140 /* Select frame FI (or NULL - to invalidate the current frame). */
1143 select_frame (struct frame_info *fi)
1147 selected_frame = fi;
1148 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1149 frame is being invalidated. */
1150 if (deprecated_selected_frame_level_changed_hook)
1151 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
1153 /* FIXME: kseitz/2002-08-28: It would be nice to call
1154 selected_frame_level_changed_event() right here, but due to limitations
1155 in the current interfaces, we would end up flooding UIs with events
1156 because select_frame() is used extensively internally.
1158 Once we have frame-parameterized frame (and frame-related) commands,
1159 the event notification can be moved here, since this function will only
1160 be called when the user's selected frame is being changed. */
1162 /* Ensure that symbols for this frame are read in. Also, determine the
1163 source language of this frame, and switch to it if desired. */
1166 /* We retrieve the frame's symtab by using the frame PC. However
1167 we cannot use the frame PC as-is, because it usually points to
1168 the instruction following the "call", which is sometimes the
1169 first instruction of another function. So we rely on
1170 get_frame_address_in_block() which provides us with a PC which
1171 is guaranteed to be inside the frame's code block. */
1172 s = find_pc_symtab (get_frame_address_in_block (fi));
1174 && s->language != current_language->la_language
1175 && s->language != language_unknown
1176 && language_mode == language_mode_auto)
1178 set_language (s->language);
1183 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1184 Always returns a non-NULL value. */
1187 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1189 struct frame_info *fi;
1193 fprintf_unfiltered (gdb_stdlog,
1194 "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1195 paddr_nz (addr), paddr_nz (pc));
1198 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1200 fi->next = create_sentinel_frame (get_current_regcache ());
1202 /* Set/update this frame's cached PC value, found in the next frame.
1203 Do this before looking for this frame's unwinder. A sniffer is
1204 very likely to read this, and the corresponding unwinder is
1205 entitled to rely that the PC doesn't magically change. */
1206 fi->next->prev_pc.value = pc;
1207 fi->next->prev_pc.p = 1;
1209 /* Select/initialize both the unwind function and the frame's type
1211 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1214 fi->this_id.value = frame_id_build (addr, pc);
1218 fprintf_unfiltered (gdb_stdlog, "-> ");
1219 fprint_frame (gdb_stdlog, fi);
1220 fprintf_unfiltered (gdb_stdlog, " }\n");
1226 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1227 innermost frame). Be careful to not fall off the bottom of the
1228 frame chain and onto the sentinel frame. */
1231 get_next_frame (struct frame_info *this_frame)
1233 if (this_frame->level > 0)
1234 return this_frame->next;
1239 /* Observer for the target_changed event. */
1242 frame_observer_target_changed (struct target_ops *target)
1244 reinit_frame_cache ();
1247 /* Flush the entire frame cache. */
1250 reinit_frame_cache (void)
1252 struct frame_info *fi;
1254 /* Tear down all frame caches. */
1255 for (fi = current_frame; fi != NULL; fi = fi->prev)
1257 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1258 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1259 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1260 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1263 /* Since we can't really be sure what the first object allocated was */
1264 obstack_free (&frame_cache_obstack, 0);
1265 obstack_init (&frame_cache_obstack);
1267 if (current_frame != NULL)
1268 annotate_frames_invalid ();
1270 current_frame = NULL; /* Invalidate cache */
1271 select_frame (NULL);
1273 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1276 /* Find where a register is saved (in memory or another register).
1277 The result of frame_register_unwind is just where it is saved
1278 relative to this particular frame. */
1281 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1282 int *optimizedp, enum lval_type *lvalp,
1283 CORE_ADDR *addrp, int *realnump)
1285 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1287 while (this_frame != NULL)
1289 frame_register_unwind (this_frame, regnum, optimizedp, lvalp,
1290 addrp, realnump, NULL);
1295 if (*lvalp != lval_register)
1299 this_frame = get_next_frame (this_frame);
1303 /* Return a "struct frame_info" corresponding to the frame that called
1304 THIS_FRAME. Returns NULL if there is no such frame.
1306 Unlike get_prev_frame, this function always tries to unwind the
1309 static struct frame_info *
1310 get_prev_frame_1 (struct frame_info *this_frame)
1312 struct frame_id this_id;
1313 struct gdbarch *gdbarch;
1315 gdb_assert (this_frame != NULL);
1316 gdbarch = get_frame_arch (this_frame);
1320 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1321 if (this_frame != NULL)
1322 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1324 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1325 fprintf_unfiltered (gdb_stdlog, ") ");
1328 /* Only try to do the unwind once. */
1329 if (this_frame->prev_p)
1333 fprintf_unfiltered (gdb_stdlog, "-> ");
1334 fprint_frame (gdb_stdlog, this_frame->prev);
1335 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1337 return this_frame->prev;
1340 /* If the frame unwinder hasn't been selected yet, we must do so
1341 before setting prev_p; otherwise the check for misbehaved
1342 sniffers will think that this frame's sniffer tried to unwind
1343 further (see frame_cleanup_after_sniffer). */
1344 if (this_frame->unwind == NULL)
1346 = frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1348 this_frame->prev_p = 1;
1349 this_frame->stop_reason = UNWIND_NO_REASON;
1351 /* If we are unwinding from an inline frame, all of the below tests
1352 were already performed when we unwound from the next non-inline
1353 frame. We must skip them, since we can not get THIS_FRAME's ID
1354 until we have unwound all the way down to the previous non-inline
1356 if (get_frame_type (this_frame) == INLINE_FRAME)
1357 return get_prev_frame_raw (this_frame);
1359 /* Check that this frame's ID was valid. If it wasn't, don't try to
1360 unwind to the prev frame. Be careful to not apply this test to
1361 the sentinel frame. */
1362 this_id = get_frame_id (this_frame);
1363 if (this_frame->level >= 0 && !frame_id_p (this_id))
1367 fprintf_unfiltered (gdb_stdlog, "-> ");
1368 fprint_frame (gdb_stdlog, NULL);
1369 fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1371 this_frame->stop_reason = UNWIND_NULL_ID;
1375 /* Check that this frame's ID isn't inner to (younger, below, next)
1376 the next frame. This happens when a frame unwind goes backwards.
1377 This check is valid only if this frame and the next frame are NORMAL.
1378 See the comment at frame_id_inner for details. */
1379 if (get_frame_type (this_frame) == NORMAL_FRAME
1380 && this_frame->next->unwind->type == NORMAL_FRAME
1381 && frame_id_inner (get_frame_arch (this_frame->next), this_id,
1382 get_frame_id (this_frame->next)))
1386 fprintf_unfiltered (gdb_stdlog, "-> ");
1387 fprint_frame (gdb_stdlog, NULL);
1388 fprintf_unfiltered (gdb_stdlog, " // this frame ID is inner }\n");
1390 this_frame->stop_reason = UNWIND_INNER_ID;
1394 /* Check that this and the next frame are not identical. If they
1395 are, there is most likely a stack cycle. As with the inner-than
1396 test above, avoid comparing the inner-most and sentinel frames. */
1397 if (this_frame->level > 0
1398 && frame_id_eq (this_id, get_frame_id (this_frame->next)))
1402 fprintf_unfiltered (gdb_stdlog, "-> ");
1403 fprint_frame (gdb_stdlog, NULL);
1404 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1406 this_frame->stop_reason = UNWIND_SAME_ID;
1410 /* Check that this and the next frame do not unwind the PC register
1411 to the same memory location. If they do, then even though they
1412 have different frame IDs, the new frame will be bogus; two
1413 functions can't share a register save slot for the PC. This can
1414 happen when the prologue analyzer finds a stack adjustment, but
1417 This check does assume that the "PC register" is roughly a
1418 traditional PC, even if the gdbarch_unwind_pc method adjusts
1419 it (we do not rely on the value, only on the unwound PC being
1420 dependent on this value). A potential improvement would be
1421 to have the frame prev_pc method and the gdbarch unwind_pc
1422 method set the same lval and location information as
1423 frame_register_unwind. */
1424 if (this_frame->level > 0
1425 && gdbarch_pc_regnum (gdbarch) >= 0
1426 && get_frame_type (this_frame) == NORMAL_FRAME
1427 && (get_frame_type (this_frame->next) == NORMAL_FRAME
1428 || get_frame_type (this_frame->next) == INLINE_FRAME))
1430 int optimized, realnum, nrealnum;
1431 enum lval_type lval, nlval;
1432 CORE_ADDR addr, naddr;
1434 frame_register_unwind_location (this_frame,
1435 gdbarch_pc_regnum (gdbarch),
1436 &optimized, &lval, &addr, &realnum);
1437 frame_register_unwind_location (get_next_frame (this_frame),
1438 gdbarch_pc_regnum (gdbarch),
1439 &optimized, &nlval, &naddr, &nrealnum);
1441 if ((lval == lval_memory && lval == nlval && addr == naddr)
1442 || (lval == lval_register && lval == nlval && realnum == nrealnum))
1446 fprintf_unfiltered (gdb_stdlog, "-> ");
1447 fprint_frame (gdb_stdlog, NULL);
1448 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1451 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1452 this_frame->prev = NULL;
1457 return get_prev_frame_raw (this_frame);
1460 /* Construct a new "struct frame_info" and link it previous to
1463 static struct frame_info *
1464 get_prev_frame_raw (struct frame_info *this_frame)
1466 struct frame_info *prev_frame;
1468 /* Allocate the new frame but do not wire it in to the frame chain.
1469 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1470 frame->next to pull some fancy tricks (of course such code is, by
1471 definition, recursive). Try to prevent it.
1473 There is no reason to worry about memory leaks, should the
1474 remainder of the function fail. The allocated memory will be
1475 quickly reclaimed when the frame cache is flushed, and the `we've
1476 been here before' check above will stop repeated memory
1477 allocation calls. */
1478 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1479 prev_frame->level = this_frame->level + 1;
1481 /* Don't yet compute ->unwind (and hence ->type). It is computed
1482 on-demand in get_frame_type, frame_register_unwind, and
1485 /* Don't yet compute the frame's ID. It is computed on-demand by
1488 /* The unwound frame ID is validate at the start of this function,
1489 as part of the logic to decide if that frame should be further
1490 unwound, and not here while the prev frame is being created.
1491 Doing this makes it possible for the user to examine a frame that
1492 has an invalid frame ID.
1494 Some very old VAX code noted: [...] For the sake of argument,
1495 suppose that the stack is somewhat trashed (which is one reason
1496 that "info frame" exists). So, return 0 (indicating we don't
1497 know the address of the arglist) if we don't know what frame this
1501 this_frame->prev = prev_frame;
1502 prev_frame->next = this_frame;
1506 fprintf_unfiltered (gdb_stdlog, "-> ");
1507 fprint_frame (gdb_stdlog, prev_frame);
1508 fprintf_unfiltered (gdb_stdlog, " }\n");
1514 /* Debug routine to print a NULL frame being returned. */
1517 frame_debug_got_null_frame (struct frame_info *this_frame,
1522 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1523 if (this_frame != NULL)
1524 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1526 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1527 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1531 /* Is this (non-sentinel) frame in the "main"() function? */
1534 inside_main_func (struct frame_info *this_frame)
1536 struct minimal_symbol *msymbol;
1539 if (symfile_objfile == 0)
1541 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1542 if (msymbol == NULL)
1544 /* Make certain that the code, and not descriptor, address is
1546 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
1547 SYMBOL_VALUE_ADDRESS (msymbol),
1549 return maddr == get_frame_func (this_frame);
1552 /* Test whether THIS_FRAME is inside the process entry point function. */
1555 inside_entry_func (struct frame_info *this_frame)
1557 return (get_frame_func (this_frame) == entry_point_address ());
1560 /* Return a structure containing various interesting information about
1561 the frame that called THIS_FRAME. Returns NULL if there is entier
1562 no such frame or the frame fails any of a set of target-independent
1563 condition that should terminate the frame chain (e.g., as unwinding
1566 This function should not contain target-dependent tests, such as
1567 checking whether the program-counter is zero. */
1570 get_prev_frame (struct frame_info *this_frame)
1572 struct frame_info *prev_frame;
1574 /* There is always a frame. If this assertion fails, suspect that
1575 something should be calling get_selected_frame() or
1576 get_current_frame(). */
1577 gdb_assert (this_frame != NULL);
1579 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1580 sense to stop unwinding at a dummy frame. One place where a dummy
1581 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1582 pcsqh register (space register for the instruction at the head of the
1583 instruction queue) cannot be written directly; the only way to set it
1584 is to branch to code that is in the target space. In order to implement
1585 frame dummies on HPUX, the called function is made to jump back to where
1586 the inferior was when the user function was called. If gdb was inside
1587 the main function when we created the dummy frame, the dummy frame will
1588 point inside the main function. */
1589 if (this_frame->level >= 0
1590 && get_frame_type (this_frame) == NORMAL_FRAME
1591 && !backtrace_past_main
1592 && inside_main_func (this_frame))
1593 /* Don't unwind past main(). Note, this is done _before_ the
1594 frame has been marked as previously unwound. That way if the
1595 user later decides to enable unwinds past main(), that will
1596 automatically happen. */
1598 frame_debug_got_null_frame (this_frame, "inside main func");
1602 /* If the user's backtrace limit has been exceeded, stop. We must
1603 add two to the current level; one of those accounts for backtrace_limit
1604 being 1-based and the level being 0-based, and the other accounts for
1605 the level of the new frame instead of the level of the current
1607 if (this_frame->level + 2 > backtrace_limit)
1609 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
1613 /* If we're already inside the entry function for the main objfile,
1614 then it isn't valid. Don't apply this test to a dummy frame -
1615 dummy frame PCs typically land in the entry func. Don't apply
1616 this test to the sentinel frame. Sentinel frames should always
1617 be allowed to unwind. */
1618 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1619 wasn't checking for "main" in the minimal symbols. With that
1620 fixed asm-source tests now stop in "main" instead of halting the
1621 backtrace in weird and wonderful ways somewhere inside the entry
1622 file. Suspect that tests for inside the entry file/func were
1623 added to work around that (now fixed) case. */
1624 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1625 suggested having the inside_entry_func test use the
1626 inside_main_func() msymbol trick (along with entry_point_address()
1627 I guess) to determine the address range of the start function.
1628 That should provide a far better stopper than the current
1630 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1631 applied tail-call optimizations to main so that a function called
1632 from main returns directly to the caller of main. Since we don't
1633 stop at main, we should at least stop at the entry point of the
1635 if (this_frame->level >= 0
1636 && get_frame_type (this_frame) == NORMAL_FRAME
1637 && !backtrace_past_entry
1638 && inside_entry_func (this_frame))
1640 frame_debug_got_null_frame (this_frame, "inside entry func");
1644 /* Assume that the only way to get a zero PC is through something
1645 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1646 will never unwind a zero PC. */
1647 if (this_frame->level > 0
1648 && (get_frame_type (this_frame) == NORMAL_FRAME
1649 || get_frame_type (this_frame) == INLINE_FRAME)
1650 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1651 && get_frame_pc (this_frame) == 0)
1653 frame_debug_got_null_frame (this_frame, "zero PC");
1657 return get_prev_frame_1 (this_frame);
1661 get_frame_pc (struct frame_info *frame)
1663 gdb_assert (frame->next != NULL);
1664 return frame_unwind_pc (frame->next);
1667 /* Return an address that falls within THIS_FRAME's code block. */
1670 get_frame_address_in_block (struct frame_info *this_frame)
1672 /* A draft address. */
1673 CORE_ADDR pc = get_frame_pc (this_frame);
1675 struct frame_info *next_frame = this_frame->next;
1677 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1678 Normally the resume address is inside the body of the function
1679 associated with THIS_FRAME, but there is a special case: when
1680 calling a function which the compiler knows will never return
1681 (for instance abort), the call may be the very last instruction
1682 in the calling function. The resume address will point after the
1683 call and may be at the beginning of a different function
1686 If THIS_FRAME is a signal frame or dummy frame, then we should
1687 not adjust the unwound PC. For a dummy frame, GDB pushed the
1688 resume address manually onto the stack. For a signal frame, the
1689 OS may have pushed the resume address manually and invoked the
1690 handler (e.g. GNU/Linux), or invoked the trampoline which called
1691 the signal handler - but in either case the signal handler is
1692 expected to return to the trampoline. So in both of these
1693 cases we know that the resume address is executable and
1694 related. So we only need to adjust the PC if THIS_FRAME
1695 is a normal function.
1697 If the program has been interrupted while THIS_FRAME is current,
1698 then clearly the resume address is inside the associated
1699 function. There are three kinds of interruption: debugger stop
1700 (next frame will be SENTINEL_FRAME), operating system
1701 signal or exception (next frame will be SIGTRAMP_FRAME),
1702 or debugger-induced function call (next frame will be
1703 DUMMY_FRAME). So we only need to adjust the PC if
1704 NEXT_FRAME is a normal function.
1706 We check the type of NEXT_FRAME first, since it is already
1707 known; frame type is determined by the unwinder, and since
1708 we have THIS_FRAME we've already selected an unwinder for
1711 If the next frame is inlined, we need to keep going until we find
1712 the real function - for instance, if a signal handler is invoked
1713 while in an inlined function, then the code address of the
1714 "calling" normal function should not be adjusted either. */
1716 while (get_frame_type (next_frame) == INLINE_FRAME)
1717 next_frame = next_frame->next;
1719 if (get_frame_type (next_frame) == NORMAL_FRAME
1720 && (get_frame_type (this_frame) == NORMAL_FRAME
1721 || get_frame_type (this_frame) == INLINE_FRAME))
1728 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1730 struct frame_info *next_frame;
1733 /* If the next frame represents an inlined function call, this frame's
1734 sal is the "call site" of that inlined function, which can not
1735 be inferred from get_frame_pc. */
1736 next_frame = get_next_frame (frame);
1737 if (frame_inlined_callees (frame) > 0)
1742 sym = get_frame_function (next_frame);
1744 sym = inline_skipped_symbol (inferior_ptid);
1747 if (SYMBOL_LINE (sym) != 0)
1749 sal->symtab = SYMBOL_SYMTAB (sym);
1750 sal->line = SYMBOL_LINE (sym);
1753 /* If the symbol does not have a location, we don't know where
1754 the call site is. Do not pretend to. This is jarring, but
1755 we can't do much better. */
1756 sal->pc = get_frame_pc (frame);
1761 /* If FRAME is not the innermost frame, that normally means that
1762 FRAME->pc points at the return instruction (which is *after* the
1763 call instruction), and we want to get the line containing the
1764 call (because the call is where the user thinks the program is).
1765 However, if the next frame is either a SIGTRAMP_FRAME or a
1766 DUMMY_FRAME, then the next frame will contain a saved interrupt
1767 PC and such a PC indicates the current (rather than next)
1768 instruction/line, consequently, for such cases, want to get the
1769 line containing fi->pc. */
1770 notcurrent = (get_frame_pc (frame) != get_frame_address_in_block (frame));
1771 (*sal) = find_pc_line (get_frame_pc (frame), notcurrent);
1774 /* Per "frame.h", return the ``address'' of the frame. Code should
1775 really be using get_frame_id(). */
1777 get_frame_base (struct frame_info *fi)
1779 return get_frame_id (fi).stack_addr;
1782 /* High-level offsets into the frame. Used by the debug info. */
1785 get_frame_base_address (struct frame_info *fi)
1787 if (get_frame_type (fi) != NORMAL_FRAME)
1789 if (fi->base == NULL)
1790 fi->base = frame_base_find_by_frame (fi);
1791 /* Sneaky: If the low-level unwind and high-level base code share a
1792 common unwinder, let them share the prologue cache. */
1793 if (fi->base->unwind == fi->unwind)
1794 return fi->base->this_base (fi, &fi->prologue_cache);
1795 return fi->base->this_base (fi, &fi->base_cache);
1799 get_frame_locals_address (struct frame_info *fi)
1802 if (get_frame_type (fi) != NORMAL_FRAME)
1804 /* If there isn't a frame address method, find it. */
1805 if (fi->base == NULL)
1806 fi->base = frame_base_find_by_frame (fi);
1807 /* Sneaky: If the low-level unwind and high-level base code share a
1808 common unwinder, let them share the prologue cache. */
1809 if (fi->base->unwind == fi->unwind)
1810 return fi->base->this_locals (fi, &fi->prologue_cache);
1811 return fi->base->this_locals (fi, &fi->base_cache);
1815 get_frame_args_address (struct frame_info *fi)
1818 if (get_frame_type (fi) != NORMAL_FRAME)
1820 /* If there isn't a frame address method, find it. */
1821 if (fi->base == NULL)
1822 fi->base = frame_base_find_by_frame (fi);
1823 /* Sneaky: If the low-level unwind and high-level base code share a
1824 common unwinder, let them share the prologue cache. */
1825 if (fi->base->unwind == fi->unwind)
1826 return fi->base->this_args (fi, &fi->prologue_cache);
1827 return fi->base->this_args (fi, &fi->base_cache);
1830 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1831 or -1 for a NULL frame. */
1834 frame_relative_level (struct frame_info *fi)
1843 get_frame_type (struct frame_info *frame)
1845 if (frame->unwind == NULL)
1846 /* Initialize the frame's unwinder because that's what
1847 provides the frame's type. */
1848 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
1849 return frame->unwind->type;
1852 /* Memory access methods. */
1855 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
1856 gdb_byte *buf, int len)
1858 read_memory (addr, buf, len);
1862 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1865 return read_memory_integer (addr, len);
1869 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1872 return read_memory_unsigned_integer (addr, len);
1876 safe_frame_unwind_memory (struct frame_info *this_frame,
1877 CORE_ADDR addr, gdb_byte *buf, int len)
1879 /* NOTE: target_read_memory returns zero on success! */
1880 return !target_read_memory (addr, buf, len);
1883 /* Architecture method. */
1886 get_frame_arch (struct frame_info *this_frame)
1888 /* In the future, this function will return a per-frame
1889 architecture instead of current_gdbarch. Calling the
1890 routine with a NULL value of this_frame is a bug! */
1891 gdb_assert (this_frame);
1893 return current_gdbarch;
1896 /* Stack pointer methods. */
1899 get_frame_sp (struct frame_info *this_frame)
1901 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1902 /* Normality - an architecture that provides a way of obtaining any
1903 frame inner-most address. */
1904 if (gdbarch_unwind_sp_p (gdbarch))
1905 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
1906 operate on THIS_FRAME now. */
1907 return gdbarch_unwind_sp (gdbarch, this_frame->next);
1908 /* Now things are really are grim. Hope that the value returned by
1909 the gdbarch_sp_regnum register is meaningful. */
1910 if (gdbarch_sp_regnum (gdbarch) >= 0)
1911 return get_frame_register_unsigned (this_frame,
1912 gdbarch_sp_regnum (gdbarch));
1913 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
1916 /* Return the reason why we can't unwind past FRAME. */
1918 enum unwind_stop_reason
1919 get_frame_unwind_stop_reason (struct frame_info *frame)
1921 /* If we haven't tried to unwind past this point yet, then assume
1922 that unwinding would succeed. */
1923 if (frame->prev_p == 0)
1924 return UNWIND_NO_REASON;
1926 /* Otherwise, we set a reason when we succeeded (or failed) to
1928 return frame->stop_reason;
1931 /* Return a string explaining REASON. */
1934 frame_stop_reason_string (enum unwind_stop_reason reason)
1938 case UNWIND_NULL_ID:
1939 return _("unwinder did not report frame ID");
1941 case UNWIND_INNER_ID:
1942 return _("previous frame inner to this frame (corrupt stack?)");
1944 case UNWIND_SAME_ID:
1945 return _("previous frame identical to this frame (corrupt stack?)");
1947 case UNWIND_NO_SAVED_PC:
1948 return _("frame did not save the PC");
1950 case UNWIND_NO_REASON:
1951 case UNWIND_FIRST_ERROR:
1953 internal_error (__FILE__, __LINE__,
1954 "Invalid frame stop reason");
1958 /* Clean up after a failed (wrong unwinder) attempt to unwind past
1962 frame_cleanup_after_sniffer (void *arg)
1964 struct frame_info *frame = arg;
1966 /* The sniffer should not allocate a prologue cache if it did not
1967 match this frame. */
1968 gdb_assert (frame->prologue_cache == NULL);
1970 /* No sniffer should extend the frame chain; sniff based on what is
1972 gdb_assert (!frame->prev_p);
1974 /* The sniffer should not check the frame's ID; that's circular. */
1975 gdb_assert (!frame->this_id.p);
1977 /* Clear cached fields dependent on the unwinder.
1979 The previous PC is independent of the unwinder, but the previous
1980 function is not (see get_frame_address_in_block). */
1981 frame->prev_func.p = 0;
1982 frame->prev_func.addr = 0;
1984 /* Discard the unwinder last, so that we can easily find it if an assertion
1985 in this function triggers. */
1986 frame->unwind = NULL;
1989 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
1990 Return a cleanup which should be called if unwinding fails, and
1991 discarded if it succeeds. */
1994 frame_prepare_for_sniffer (struct frame_info *frame,
1995 const struct frame_unwind *unwind)
1997 gdb_assert (frame->unwind == NULL);
1998 frame->unwind = unwind;
1999 return make_cleanup (frame_cleanup_after_sniffer, frame);
2002 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2004 static struct cmd_list_element *set_backtrace_cmdlist;
2005 static struct cmd_list_element *show_backtrace_cmdlist;
2008 set_backtrace_cmd (char *args, int from_tty)
2010 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2014 show_backtrace_cmd (char *args, int from_tty)
2016 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2020 _initialize_frame (void)
2022 obstack_init (&frame_cache_obstack);
2024 observer_attach_target_changed (frame_observer_target_changed);
2026 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
2027 Set backtrace specific variables.\n\
2028 Configure backtrace variables such as the backtrace limit"),
2029 &set_backtrace_cmdlist, "set backtrace ",
2030 0/*allow-unknown*/, &setlist);
2031 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
2032 Show backtrace specific variables\n\
2033 Show backtrace variables such as the backtrace limit"),
2034 &show_backtrace_cmdlist, "show backtrace ",
2035 0/*allow-unknown*/, &showlist);
2037 add_setshow_boolean_cmd ("past-main", class_obscure,
2038 &backtrace_past_main, _("\
2039 Set whether backtraces should continue past \"main\"."), _("\
2040 Show whether backtraces should continue past \"main\"."), _("\
2041 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2042 the backtrace at \"main\". Set this variable if you need to see the rest\n\
2043 of the stack trace."),
2045 show_backtrace_past_main,
2046 &set_backtrace_cmdlist,
2047 &show_backtrace_cmdlist);
2049 add_setshow_boolean_cmd ("past-entry", class_obscure,
2050 &backtrace_past_entry, _("\
2051 Set whether backtraces should continue past the entry point of a program."),
2053 Show whether backtraces should continue past the entry point of a program."),
2055 Normally there are no callers beyond the entry point of a program, so GDB\n\
2056 will terminate the backtrace there. Set this variable if you need to see \n\
2057 the rest of the stack trace."),
2059 show_backtrace_past_entry,
2060 &set_backtrace_cmdlist,
2061 &show_backtrace_cmdlist);
2063 add_setshow_integer_cmd ("limit", class_obscure,
2064 &backtrace_limit, _("\
2065 Set an upper bound on the number of backtrace levels."), _("\
2066 Show the upper bound on the number of backtrace levels."), _("\
2067 No more than the specified number of frames can be displayed or examined.\n\
2068 Zero is unlimited."),
2070 show_backtrace_limit,
2071 &set_backtrace_cmdlist,
2072 &show_backtrace_cmdlist);
2074 /* Debug this files internals. */
2075 add_setshow_zinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
2076 Set frame debugging."), _("\
2077 Show frame debugging."), _("\
2078 When non-zero, frame specific internal debugging is enabled."),
2081 &setdebuglist, &showdebuglist);