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 static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
47 /* We keep a cache of stack frames, each of which is a "struct
48 frame_info". The innermost one gets allocated (in
49 wait_for_inferior) each time the inferior stops; current_frame
50 points to it. Additional frames get allocated (in get_prev_frame)
51 as needed, and are chained through the next and prev fields. Any
52 time that the frame cache becomes invalid (most notably when we
53 execute something, but also if we change how we interpret the
54 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
55 which reads new symbols)), we should call reinit_frame_cache. */
59 /* Level of this frame. The inner-most (youngest) frame is at level
60 0. As you move towards the outer-most (oldest) frame, the level
61 increases. This is a cached value. It could just as easily be
62 computed by counting back from the selected frame to the inner
64 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
65 reserved to indicate a bogus frame - one that has been created
66 just to keep GDB happy (GDB always needs a frame). For the
67 moment leave this as speculation. */
70 /* The frame's low-level unwinder and corresponding cache. The
71 low-level unwinder is responsible for unwinding register values
72 for the previous frame. The low-level unwind methods are
73 selected based on the presence, or otherwise, of register unwind
74 information such as CFI. */
76 const struct frame_unwind *unwind;
78 /* Cached copy of the previous frame's resume address. */
84 /* Cached copy of the previous frame's function address. */
91 /* This frame's ID. */
95 struct frame_id value;
98 /* The frame's high-level base methods, and corresponding cache.
99 The high level base methods are selected based on the frame's
101 const struct frame_base *base;
104 /* Pointers to the next (down, inner, younger) and previous (up,
105 outer, older) frame_info's in the frame cache. */
106 struct frame_info *next; /* down, inner, younger */
108 struct frame_info *prev; /* up, outer, older */
110 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
111 could. Only valid when PREV_P is set. */
112 enum unwind_stop_reason stop_reason;
115 /* Flag to control debugging. */
119 show_frame_debug (struct ui_file *file, int from_tty,
120 struct cmd_list_element *c, const char *value)
122 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
125 /* Flag to indicate whether backtraces should stop at main et.al. */
127 static int backtrace_past_main;
129 show_backtrace_past_main (struct ui_file *file, int from_tty,
130 struct cmd_list_element *c, const char *value)
132 fprintf_filtered (file, _("\
133 Whether backtraces should continue past \"main\" is %s.\n"),
137 static int backtrace_past_entry;
139 show_backtrace_past_entry (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 the entry point of a program is %s.\n"),
147 static int backtrace_limit = INT_MAX;
149 show_backtrace_limit (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
152 fprintf_filtered (file, _("\
153 An upper bound on the number of backtrace levels is %s.\n"),
159 fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
162 fprintf_unfiltered (file, "%s=0x%s", name, paddr_nz (addr));
164 fprintf_unfiltered (file, "!%s", name);
168 fprint_frame_id (struct ui_file *file, struct frame_id id)
170 fprintf_unfiltered (file, "{");
171 fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
172 fprintf_unfiltered (file, ",");
173 fprint_field (file, "code", id.code_addr_p, id.code_addr);
174 fprintf_unfiltered (file, ",");
175 fprint_field (file, "special", id.special_addr_p, id.special_addr);
176 fprintf_unfiltered (file, "}");
180 fprint_frame_type (struct ui_file *file, enum frame_type type)
185 fprintf_unfiltered (file, "NORMAL_FRAME");
188 fprintf_unfiltered (file, "DUMMY_FRAME");
191 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
194 fprintf_unfiltered (file, "<unknown type>");
200 fprint_frame (struct ui_file *file, struct frame_info *fi)
204 fprintf_unfiltered (file, "<NULL frame>");
207 fprintf_unfiltered (file, "{");
208 fprintf_unfiltered (file, "level=%d", fi->level);
209 fprintf_unfiltered (file, ",");
210 fprintf_unfiltered (file, "type=");
211 if (fi->unwind != NULL)
212 fprint_frame_type (file, fi->unwind->type);
214 fprintf_unfiltered (file, "<unknown>");
215 fprintf_unfiltered (file, ",");
216 fprintf_unfiltered (file, "unwind=");
217 if (fi->unwind != NULL)
218 gdb_print_host_address (fi->unwind, file);
220 fprintf_unfiltered (file, "<unknown>");
221 fprintf_unfiltered (file, ",");
222 fprintf_unfiltered (file, "pc=");
223 if (fi->next != NULL && fi->next->prev_pc.p)
224 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
226 fprintf_unfiltered (file, "<unknown>");
227 fprintf_unfiltered (file, ",");
228 fprintf_unfiltered (file, "id=");
230 fprint_frame_id (file, fi->this_id.value);
232 fprintf_unfiltered (file, "<unknown>");
233 fprintf_unfiltered (file, ",");
234 fprintf_unfiltered (file, "func=");
235 if (fi->next != NULL && fi->next->prev_func.p)
236 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
238 fprintf_unfiltered (file, "<unknown>");
239 fprintf_unfiltered (file, "}");
242 /* Return a frame uniq ID that can be used to, later, re-find the
246 get_frame_id (struct frame_info *fi)
250 return null_frame_id;
255 fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
257 /* Find the unwinder. */
258 if (fi->unwind == NULL)
259 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
260 /* Find THIS frame's ID. */
261 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
265 fprintf_unfiltered (gdb_stdlog, "-> ");
266 fprint_frame_id (gdb_stdlog, fi->this_id.value);
267 fprintf_unfiltered (gdb_stdlog, " }\n");
270 return fi->this_id.value;
274 frame_unwind_id (struct frame_info *next_frame)
276 /* Use prev_frame, and not get_prev_frame. The latter will truncate
277 the frame chain, leading to this function unintentionally
278 returning a null_frame_id (e.g., when a caller requests the frame
279 ID of "main()"s caller. */
280 return get_frame_id (get_prev_frame_1 (next_frame));
283 const struct frame_id null_frame_id; /* All zeros. */
286 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
287 CORE_ADDR special_addr)
289 struct frame_id id = null_frame_id;
290 id.stack_addr = stack_addr;
292 id.code_addr = code_addr;
294 id.special_addr = special_addr;
295 id.special_addr_p = 1;
300 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
302 struct frame_id id = null_frame_id;
303 id.stack_addr = stack_addr;
305 id.code_addr = code_addr;
311 frame_id_build_wild (CORE_ADDR stack_addr)
313 struct frame_id id = null_frame_id;
314 id.stack_addr = stack_addr;
320 frame_id_p (struct frame_id l)
323 /* The frame is valid iff it has a valid stack address. */
327 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
328 fprint_frame_id (gdb_stdlog, l);
329 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
335 frame_id_eq (struct frame_id l, struct frame_id r)
338 if (!l.stack_addr_p || !r.stack_addr_p)
339 /* Like a NaN, if either ID is invalid, the result is false.
340 Note that a frame ID is invalid iff it is the null frame ID. */
342 else if (l.stack_addr != r.stack_addr)
343 /* If .stack addresses are different, the frames are different. */
345 else if (!l.code_addr_p || !r.code_addr_p)
346 /* An invalid code addr is a wild card, always succeed. */
348 else if (l.code_addr != r.code_addr)
349 /* If .code addresses are different, the frames are different. */
351 else if (!l.special_addr_p || !r.special_addr_p)
352 /* An invalid special addr is a wild card (or unused), always succeed. */
354 else if (l.special_addr == r.special_addr)
355 /* Frames are equal. */
362 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
363 fprint_frame_id (gdb_stdlog, l);
364 fprintf_unfiltered (gdb_stdlog, ",r=");
365 fprint_frame_id (gdb_stdlog, r);
366 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
371 /* Safety net to check whether frame ID L should be inner to
372 frame ID R, according to their stack addresses.
374 This method cannot be used to compare arbitrary frames, as the
375 ranges of valid stack addresses may be discontiguous (e.g. due
378 However, it can be used as safety net to discover invalid frame
379 IDs in certain circumstances. Assuming that NEXT is the immediate
380 inner frame to THIS and that NEXT and THIS are both NORMAL frames:
382 * The stack address of NEXT must be inner-than-or-equal to the stack
385 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
388 * If NEXT and THIS have different stack addresses, no other frame
389 in the frame chain may have a stack address in between.
391 Therefore, if frame_id_inner (TEST, THIS) holds, but
392 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
393 to a valid frame in the frame chain.
395 The sanity checks above cannot be performed when a SIGTRAMP frame
396 is involved, because signal handlers might be executed on a different
397 stack than the stack used by the routine that caused the signal
398 to be raised. This can happen for instance when a thread exceeds
399 its maximum stack size. In this case, certain compilers implement
400 a stack overflow strategy that cause the handler to be run on a
404 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
407 if (!l.stack_addr_p || !r.stack_addr_p)
408 /* Like NaN, any operation involving an invalid ID always fails. */
411 /* Only return non-zero when strictly inner than. Note that, per
412 comment in "frame.h", there is some fuzz here. Frameless
413 functions are not strictly inner than (same .stack but
414 different .code and/or .special address). */
415 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
418 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
419 fprint_frame_id (gdb_stdlog, l);
420 fprintf_unfiltered (gdb_stdlog, ",r=");
421 fprint_frame_id (gdb_stdlog, r);
422 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
428 frame_find_by_id (struct frame_id id)
430 struct frame_info *frame, *prev_frame;
432 /* ZERO denotes the null frame, let the caller decide what to do
433 about it. Should it instead return get_current_frame()? */
434 if (!frame_id_p (id))
437 for (frame = get_current_frame (); ; frame = prev_frame)
439 struct frame_id this = get_frame_id (frame);
440 if (frame_id_eq (id, this))
441 /* An exact match. */
444 prev_frame = get_prev_frame (frame);
448 /* As a safety net to avoid unnecessary backtracing while trying
449 to find an invalid ID, we check for a common situation where
450 we can detect from comparing stack addresses that no other
451 frame in the current frame chain can have this ID. See the
452 comment at frame_id_inner for details. */
453 if (get_frame_type (frame) == NORMAL_FRAME
454 && !frame_id_inner (get_frame_arch (frame), id, this)
455 && frame_id_inner (get_frame_arch (prev_frame), id,
456 get_frame_id (prev_frame)))
463 frame_pc_unwind (struct frame_info *this_frame)
465 if (!this_frame->prev_pc.p)
468 if (gdbarch_unwind_pc_p (get_frame_arch (this_frame)))
470 /* The right way. The `pure' way. The one true way. This
471 method depends solely on the register-unwind code to
472 determine the value of registers in THIS frame, and hence
473 the value of this frame's PC (resume address). A typical
474 implementation is no more than:
476 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
477 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
479 Note: this method is very heavily dependent on a correct
480 register-unwind implementation, it pays to fix that
481 method first; this method is frame type agnostic, since
482 it only deals with register values, it works with any
483 frame. This is all in stark contrast to the old
484 FRAME_SAVED_PC which would try to directly handle all the
485 different ways that a PC could be unwound. */
486 pc = gdbarch_unwind_pc (get_frame_arch (this_frame), this_frame);
489 internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
490 this_frame->prev_pc.value = pc;
491 this_frame->prev_pc.p = 1;
493 fprintf_unfiltered (gdb_stdlog,
494 "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
496 paddr_nz (this_frame->prev_pc.value));
498 return this_frame->prev_pc.value;
502 get_frame_func (struct frame_info *this_frame)
504 struct frame_info *next_frame = this_frame->next;
506 if (!next_frame->prev_func.p)
508 /* Make certain that this, and not the adjacent, function is
510 CORE_ADDR addr_in_block = get_frame_address_in_block (this_frame);
511 next_frame->prev_func.p = 1;
512 next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
514 fprintf_unfiltered (gdb_stdlog,
515 "{ get_frame_func (this_frame=%d) -> 0x%s }\n",
517 paddr_nz (next_frame->prev_func.addr));
519 return next_frame->prev_func.addr;
523 do_frame_register_read (void *src, int regnum, gdb_byte *buf)
525 return frame_register_read (src, regnum, buf);
529 frame_save_as_regcache (struct frame_info *this_frame)
531 struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame));
532 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
533 regcache_save (regcache, do_frame_register_read, this_frame);
534 discard_cleanups (cleanups);
539 frame_pop (struct frame_info *this_frame)
541 struct frame_info *prev_frame;
542 struct regcache *scratch;
543 struct cleanup *cleanups;
545 if (get_frame_type (this_frame) == DUMMY_FRAME)
547 /* Popping a dummy frame involves restoring more than just registers.
548 dummy_frame_pop does all the work. */
549 dummy_frame_pop (get_frame_id (this_frame));
553 /* Ensure that we have a frame to pop to. */
554 prev_frame = get_prev_frame_1 (this_frame);
557 error (_("Cannot pop the initial frame."));
559 /* Make a copy of all the register values unwound from this frame.
560 Save them in a scratch buffer so that there isn't a race between
561 trying to extract the old values from the current regcache while
562 at the same time writing new values into that same cache. */
563 scratch = frame_save_as_regcache (prev_frame);
564 cleanups = make_cleanup_regcache_xfree (scratch);
566 /* FIXME: cagney/2003-03-16: It should be possible to tell the
567 target's register cache that it is about to be hit with a burst
568 register transfer and that the sequence of register writes should
569 be batched. The pair target_prepare_to_store() and
570 target_store_registers() kind of suggest this functionality.
571 Unfortunately, they don't implement it. Their lack of a formal
572 definition can lead to targets writing back bogus values
573 (arguably a bug in the target code mind). */
574 /* Now copy those saved registers into the current regcache.
575 Here, regcache_cpy() calls regcache_restore(). */
576 regcache_cpy (get_current_regcache (), scratch);
577 do_cleanups (cleanups);
579 /* We've made right mess of GDB's local state, just discard
581 reinit_frame_cache ();
585 frame_register_unwind (struct frame_info *frame, int regnum,
586 int *optimizedp, enum lval_type *lvalp,
587 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
591 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
592 that the value proper does not need to be fetched. */
593 gdb_assert (optimizedp != NULL);
594 gdb_assert (lvalp != NULL);
595 gdb_assert (addrp != NULL);
596 gdb_assert (realnump != NULL);
597 /* gdb_assert (bufferp != NULL); */
599 value = frame_unwind_register_value (frame, regnum);
601 gdb_assert (value != NULL);
603 *optimizedp = value_optimized_out (value);
604 *lvalp = VALUE_LVAL (value);
605 *addrp = VALUE_ADDRESS (value);
606 *realnump = VALUE_REGNUM (value);
609 memcpy (bufferp, value_contents_all (value),
610 TYPE_LENGTH (value_type (value)));
612 /* Dispose of the new value. This prevents watchpoints from
613 trying to watch the saved frame pointer. */
614 release_value (value);
619 frame_register (struct frame_info *frame, int regnum,
620 int *optimizedp, enum lval_type *lvalp,
621 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
623 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
624 that the value proper does not need to be fetched. */
625 gdb_assert (optimizedp != NULL);
626 gdb_assert (lvalp != NULL);
627 gdb_assert (addrp != NULL);
628 gdb_assert (realnump != NULL);
629 /* gdb_assert (bufferp != NULL); */
631 /* Obtain the register value by unwinding the register from the next
632 (more inner frame). */
633 gdb_assert (frame != NULL && frame->next != NULL);
634 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
639 frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
645 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
650 get_frame_register (struct frame_info *frame,
651 int regnum, gdb_byte *buf)
653 frame_unwind_register (frame->next, regnum, buf);
657 frame_unwind_register_value (struct frame_info *frame, int regnum)
661 gdb_assert (frame != NULL);
665 fprintf_unfiltered (gdb_stdlog, "\
666 { frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
667 frame->level, regnum,
668 user_reg_map_regnum_to_name
669 (get_frame_arch (frame), regnum));
672 /* Find the unwinder. */
673 if (frame->unwind == NULL)
674 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
676 /* Ask this frame to unwind its register. */
677 value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
681 fprintf_unfiltered (gdb_stdlog, "->");
682 if (value_optimized_out (value))
683 fprintf_unfiltered (gdb_stdlog, " optimized out");
686 if (VALUE_LVAL (value) == lval_register)
687 fprintf_unfiltered (gdb_stdlog, " register=%d",
688 VALUE_REGNUM (value));
689 else if (VALUE_LVAL (value) == lval_memory)
690 fprintf_unfiltered (gdb_stdlog, " address=0x%s",
691 paddr_nz (VALUE_ADDRESS (value)));
693 fprintf_unfiltered (gdb_stdlog, " computed");
695 if (value_lazy (value))
696 fprintf_unfiltered (gdb_stdlog, " lazy");
700 const gdb_byte *buf = value_contents (value);
702 fprintf_unfiltered (gdb_stdlog, " bytes=");
703 fprintf_unfiltered (gdb_stdlog, "[");
704 for (i = 0; i < register_size (get_frame_arch (frame), regnum); i++)
705 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
706 fprintf_unfiltered (gdb_stdlog, "]");
710 fprintf_unfiltered (gdb_stdlog, " }\n");
717 get_frame_register_value (struct frame_info *frame, int regnum)
719 return frame_unwind_register_value (frame->next, regnum);
723 frame_unwind_register_signed (struct frame_info *frame, int regnum)
725 gdb_byte buf[MAX_REGISTER_SIZE];
726 frame_unwind_register (frame, regnum, buf);
727 return extract_signed_integer (buf, register_size (get_frame_arch (frame),
732 get_frame_register_signed (struct frame_info *frame, int regnum)
734 return frame_unwind_register_signed (frame->next, regnum);
738 frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
740 gdb_byte buf[MAX_REGISTER_SIZE];
741 frame_unwind_register (frame, regnum, buf);
742 return extract_unsigned_integer (buf, register_size (get_frame_arch (frame),
747 get_frame_register_unsigned (struct frame_info *frame, int regnum)
749 return frame_unwind_register_unsigned (frame->next, regnum);
753 put_frame_register (struct frame_info *frame, int regnum,
756 struct gdbarch *gdbarch = get_frame_arch (frame);
761 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
763 error (_("Attempt to assign to a value that was optimized out."));
768 /* FIXME: write_memory doesn't yet take constant buffers.
770 gdb_byte tmp[MAX_REGISTER_SIZE];
771 memcpy (tmp, buf, register_size (gdbarch, regnum));
772 write_memory (addr, tmp, register_size (gdbarch, regnum));
776 regcache_cooked_write (get_current_regcache (), realnum, buf);
779 error (_("Attempt to assign to an unmodifiable value."));
783 /* frame_register_read ()
785 Find and return the value of REGNUM for the specified stack frame.
786 The number of bytes copied is REGISTER_SIZE (REGNUM).
788 Returns 0 if the register value could not be found. */
791 frame_register_read (struct frame_info *frame, int regnum,
798 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
804 get_frame_register_bytes (struct frame_info *frame, int regnum,
805 CORE_ADDR offset, int len, gdb_byte *myaddr)
807 struct gdbarch *gdbarch = get_frame_arch (frame);
812 /* Skip registers wholly inside of OFFSET. */
813 while (offset >= register_size (gdbarch, regnum))
815 offset -= register_size (gdbarch, regnum);
819 /* Ensure that we will not read beyond the end of the register file.
820 This can only ever happen if the debug information is bad. */
822 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
823 for (i = regnum; i < numregs; i++)
825 int thissize = register_size (gdbarch, i);
827 break; /* This register is not available on this architecture. */
832 warning (_("Bad debug information detected: "
833 "Attempt to read %d bytes from registers."), len);
840 int curr_len = register_size (gdbarch, regnum) - offset;
844 if (curr_len == register_size (gdbarch, regnum))
846 if (!frame_register_read (frame, regnum, myaddr))
851 gdb_byte buf[MAX_REGISTER_SIZE];
852 if (!frame_register_read (frame, regnum, buf))
854 memcpy (myaddr, buf + offset, curr_len);
867 put_frame_register_bytes (struct frame_info *frame, int regnum,
868 CORE_ADDR offset, int len, const gdb_byte *myaddr)
870 struct gdbarch *gdbarch = get_frame_arch (frame);
872 /* Skip registers wholly inside of OFFSET. */
873 while (offset >= register_size (gdbarch, regnum))
875 offset -= register_size (gdbarch, regnum);
882 int curr_len = register_size (gdbarch, regnum) - offset;
886 if (curr_len == register_size (gdbarch, regnum))
888 put_frame_register (frame, regnum, myaddr);
892 gdb_byte buf[MAX_REGISTER_SIZE];
893 frame_register_read (frame, regnum, buf);
894 memcpy (buf + offset, myaddr, curr_len);
895 put_frame_register (frame, regnum, buf);
905 /* Create a sentinel frame. */
907 static struct frame_info *
908 create_sentinel_frame (struct regcache *regcache)
910 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
912 /* Explicitly initialize the sentinel frame's cache. Provide it
913 with the underlying regcache. In the future additional
914 information, such as the frame's thread will be added. */
915 frame->prologue_cache = sentinel_frame_cache (regcache);
916 /* For the moment there is only one sentinel frame implementation. */
917 frame->unwind = sentinel_frame_unwind;
918 /* Link this frame back to itself. The frame is self referential
919 (the unwound PC is the same as the pc), so make it so. */
921 /* Make the sentinel frame's ID valid, but invalid. That way all
922 comparisons with it should fail. */
923 frame->this_id.p = 1;
924 frame->this_id.value = null_frame_id;
927 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
928 fprint_frame (gdb_stdlog, frame);
929 fprintf_unfiltered (gdb_stdlog, " }\n");
934 /* Info about the innermost stack frame (contents of FP register) */
936 static struct frame_info *current_frame;
938 /* Cache for frame addresses already read by gdb. Valid only while
939 inferior is stopped. Control variables for the frame cache should
940 be local to this module. */
942 static struct obstack frame_cache_obstack;
945 frame_obstack_zalloc (unsigned long size)
947 void *data = obstack_alloc (&frame_cache_obstack, size);
948 memset (data, 0, size);
952 /* Return the innermost (currently executing) stack frame. This is
953 split into two functions. The function unwind_to_current_frame()
954 is wrapped in catch exceptions so that, even when the unwind of the
955 sentinel frame fails, the function still returns a stack frame. */
958 unwind_to_current_frame (struct ui_out *ui_out, void *args)
960 struct frame_info *frame = get_prev_frame (args);
961 /* A sentinel frame can fail to unwind, e.g., because its PC value
962 lands in somewhere like start. */
965 current_frame = frame;
970 get_current_frame (void)
972 /* First check, and report, the lack of registers. Having GDB
973 report "No stack!" or "No memory" when the target doesn't even
974 have registers is very confusing. Besides, "printcmd.exp"
975 explicitly checks that ``print $pc'' with no registers prints "No
977 if (!target_has_registers)
978 error (_("No registers."));
979 if (!target_has_stack)
980 error (_("No stack."));
981 if (!target_has_memory)
982 error (_("No memory."));
983 if (is_executing (inferior_ptid))
984 error (_("Target is executing."));
986 if (current_frame == NULL)
988 struct frame_info *sentinel_frame =
989 create_sentinel_frame (get_current_regcache ());
990 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
991 RETURN_MASK_ERROR) != 0)
993 /* Oops! Fake a current frame? Is this useful? It has a PC
994 of zero, for instance. */
995 current_frame = sentinel_frame;
998 return current_frame;
1001 /* The "selected" stack frame is used by default for local and arg
1002 access. May be zero, for no selected frame. */
1004 static struct frame_info *selected_frame;
1007 has_stack_frames (void)
1009 if (!target_has_registers || !target_has_stack || !target_has_memory)
1012 /* If the current thread is executing, don't try to read from
1014 if (is_executing (inferior_ptid))
1020 /* Return the selected frame. Always non-NULL (unless there isn't an
1021 inferior sufficient for creating a frame) in which case an error is
1025 get_selected_frame (const char *message)
1027 if (selected_frame == NULL)
1029 if (message != NULL && !has_stack_frames ())
1030 error (("%s"), message);
1031 /* Hey! Don't trust this. It should really be re-finding the
1032 last selected frame of the currently selected thread. This,
1033 though, is better than nothing. */
1034 select_frame (get_current_frame ());
1036 /* There is always a frame. */
1037 gdb_assert (selected_frame != NULL);
1038 return selected_frame;
1041 /* This is a variant of get_selected_frame() which can be called when
1042 the inferior does not have a frame; in that case it will return
1043 NULL instead of calling error(). */
1046 deprecated_safe_get_selected_frame (void)
1048 if (!has_stack_frames ())
1050 return get_selected_frame (NULL);
1053 /* Select frame FI (or NULL - to invalidate the current frame). */
1056 select_frame (struct frame_info *fi)
1060 selected_frame = fi;
1061 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1062 frame is being invalidated. */
1063 if (deprecated_selected_frame_level_changed_hook)
1064 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
1066 /* FIXME: kseitz/2002-08-28: It would be nice to call
1067 selected_frame_level_changed_event() right here, but due to limitations
1068 in the current interfaces, we would end up flooding UIs with events
1069 because select_frame() is used extensively internally.
1071 Once we have frame-parameterized frame (and frame-related) commands,
1072 the event notification can be moved here, since this function will only
1073 be called when the user's selected frame is being changed. */
1075 /* Ensure that symbols for this frame are read in. Also, determine the
1076 source language of this frame, and switch to it if desired. */
1079 /* We retrieve the frame's symtab by using the frame PC. However
1080 we cannot use the frame PC as-is, because it usually points to
1081 the instruction following the "call", which is sometimes the
1082 first instruction of another function. So we rely on
1083 get_frame_address_in_block() which provides us with a PC which
1084 is guaranteed to be inside the frame's code block. */
1085 s = find_pc_symtab (get_frame_address_in_block (fi));
1087 && s->language != current_language->la_language
1088 && s->language != language_unknown
1089 && language_mode == language_mode_auto)
1091 set_language (s->language);
1096 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1097 Always returns a non-NULL value. */
1100 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1102 struct frame_info *fi;
1106 fprintf_unfiltered (gdb_stdlog,
1107 "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1108 paddr_nz (addr), paddr_nz (pc));
1111 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1113 fi->next = create_sentinel_frame (get_current_regcache ());
1115 /* Set/update this frame's cached PC value, found in the next frame.
1116 Do this before looking for this frame's unwinder. A sniffer is
1117 very likely to read this, and the corresponding unwinder is
1118 entitled to rely that the PC doesn't magically change. */
1119 fi->next->prev_pc.value = pc;
1120 fi->next->prev_pc.p = 1;
1122 /* Select/initialize both the unwind function and the frame's type
1124 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1127 fi->this_id.value = frame_id_build (addr, pc);
1131 fprintf_unfiltered (gdb_stdlog, "-> ");
1132 fprint_frame (gdb_stdlog, fi);
1133 fprintf_unfiltered (gdb_stdlog, " }\n");
1139 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1140 innermost frame). Be careful to not fall off the bottom of the
1141 frame chain and onto the sentinel frame. */
1144 get_next_frame (struct frame_info *this_frame)
1146 if (this_frame->level > 0)
1147 return this_frame->next;
1152 /* Observer for the target_changed event. */
1155 frame_observer_target_changed (struct target_ops *target)
1157 reinit_frame_cache ();
1160 /* Flush the entire frame cache. */
1163 reinit_frame_cache (void)
1165 struct frame_info *fi;
1167 /* Tear down all frame caches. */
1168 for (fi = current_frame; fi != NULL; fi = fi->prev)
1170 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1171 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1172 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1173 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1176 /* Since we can't really be sure what the first object allocated was */
1177 obstack_free (&frame_cache_obstack, 0);
1178 obstack_init (&frame_cache_obstack);
1180 if (current_frame != NULL)
1181 annotate_frames_invalid ();
1183 current_frame = NULL; /* Invalidate cache */
1184 select_frame (NULL);
1186 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1189 /* Find where a register is saved (in memory or another register).
1190 The result of frame_register_unwind is just where it is saved
1191 relative to this particular frame. */
1194 frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1195 int *optimizedp, enum lval_type *lvalp,
1196 CORE_ADDR *addrp, int *realnump)
1198 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1200 while (this_frame != NULL)
1202 frame_register_unwind (this_frame, regnum, optimizedp, lvalp,
1203 addrp, realnump, NULL);
1208 if (*lvalp != lval_register)
1212 this_frame = get_next_frame (this_frame);
1216 /* Return a "struct frame_info" corresponding to the frame that called
1217 THIS_FRAME. Returns NULL if there is no such frame.
1219 Unlike get_prev_frame, this function always tries to unwind the
1222 static struct frame_info *
1223 get_prev_frame_1 (struct frame_info *this_frame)
1225 struct frame_info *prev_frame;
1226 struct frame_id this_id;
1227 struct gdbarch *gdbarch;
1229 gdb_assert (this_frame != NULL);
1230 gdbarch = get_frame_arch (this_frame);
1234 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1235 if (this_frame != NULL)
1236 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1238 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1239 fprintf_unfiltered (gdb_stdlog, ") ");
1242 /* Only try to do the unwind once. */
1243 if (this_frame->prev_p)
1247 fprintf_unfiltered (gdb_stdlog, "-> ");
1248 fprint_frame (gdb_stdlog, this_frame->prev);
1249 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1251 return this_frame->prev;
1254 /* If the frame unwinder hasn't been selected yet, we must do so
1255 before setting prev_p; otherwise the check for misbehaved
1256 sniffers will think that this frame's sniffer tried to unwind
1257 further (see frame_cleanup_after_sniffer). */
1258 if (this_frame->unwind == NULL)
1260 = frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1262 this_frame->prev_p = 1;
1263 this_frame->stop_reason = UNWIND_NO_REASON;
1265 /* Check that this frame's ID was valid. If it wasn't, don't try to
1266 unwind to the prev frame. Be careful to not apply this test to
1267 the sentinel frame. */
1268 this_id = get_frame_id (this_frame);
1269 if (this_frame->level >= 0 && !frame_id_p (this_id))
1273 fprintf_unfiltered (gdb_stdlog, "-> ");
1274 fprint_frame (gdb_stdlog, NULL);
1275 fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1277 this_frame->stop_reason = UNWIND_NULL_ID;
1281 /* Check that this frame's ID isn't inner to (younger, below, next)
1282 the next frame. This happens when a frame unwind goes backwards.
1283 This check is valid only if this frame and the next frame are NORMAL.
1284 See the comment at frame_id_inner for details. */
1285 if (get_frame_type (this_frame) == NORMAL_FRAME
1286 && this_frame->next->unwind->type == NORMAL_FRAME
1287 && frame_id_inner (get_frame_arch (this_frame->next), this_id,
1288 get_frame_id (this_frame->next)))
1292 fprintf_unfiltered (gdb_stdlog, "-> ");
1293 fprint_frame (gdb_stdlog, NULL);
1294 fprintf_unfiltered (gdb_stdlog, " // this frame ID is inner }\n");
1296 this_frame->stop_reason = UNWIND_INNER_ID;
1300 /* Check that this and the next frame are not identical. If they
1301 are, there is most likely a stack cycle. As with the inner-than
1302 test above, avoid comparing the inner-most and sentinel frames. */
1303 if (this_frame->level > 0
1304 && frame_id_eq (this_id, get_frame_id (this_frame->next)))
1308 fprintf_unfiltered (gdb_stdlog, "-> ");
1309 fprint_frame (gdb_stdlog, NULL);
1310 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1312 this_frame->stop_reason = UNWIND_SAME_ID;
1316 /* Check that this and the next frame do not unwind the PC register
1317 to the same memory location. If they do, then even though they
1318 have different frame IDs, the new frame will be bogus; two
1319 functions can't share a register save slot for the PC. This can
1320 happen when the prologue analyzer finds a stack adjustment, but
1323 This check does assume that the "PC register" is roughly a
1324 traditional PC, even if the gdbarch_unwind_pc method adjusts
1325 it (we do not rely on the value, only on the unwound PC being
1326 dependent on this value). A potential improvement would be
1327 to have the frame prev_pc method and the gdbarch unwind_pc
1328 method set the same lval and location information as
1329 frame_register_unwind. */
1330 if (this_frame->level > 0
1331 && gdbarch_pc_regnum (gdbarch) >= 0
1332 && get_frame_type (this_frame) == NORMAL_FRAME
1333 && get_frame_type (this_frame->next) == NORMAL_FRAME)
1335 int optimized, realnum, nrealnum;
1336 enum lval_type lval, nlval;
1337 CORE_ADDR addr, naddr;
1339 frame_register_unwind_location (this_frame,
1340 gdbarch_pc_regnum (gdbarch),
1341 &optimized, &lval, &addr, &realnum);
1342 frame_register_unwind_location (get_next_frame (this_frame),
1343 gdbarch_pc_regnum (gdbarch),
1344 &optimized, &nlval, &naddr, &nrealnum);
1346 if ((lval == lval_memory && lval == nlval && addr == naddr)
1347 || (lval == lval_register && lval == nlval && realnum == nrealnum))
1351 fprintf_unfiltered (gdb_stdlog, "-> ");
1352 fprint_frame (gdb_stdlog, NULL);
1353 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1356 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1357 this_frame->prev = NULL;
1362 /* Allocate the new frame but do not wire it in to the frame chain.
1363 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1364 frame->next to pull some fancy tricks (of course such code is, by
1365 definition, recursive). Try to prevent it.
1367 There is no reason to worry about memory leaks, should the
1368 remainder of the function fail. The allocated memory will be
1369 quickly reclaimed when the frame cache is flushed, and the `we've
1370 been here before' check above will stop repeated memory
1371 allocation calls. */
1372 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1373 prev_frame->level = this_frame->level + 1;
1375 /* Don't yet compute ->unwind (and hence ->type). It is computed
1376 on-demand in get_frame_type, frame_register_unwind, and
1379 /* Don't yet compute the frame's ID. It is computed on-demand by
1382 /* The unwound frame ID is validate at the start of this function,
1383 as part of the logic to decide if that frame should be further
1384 unwound, and not here while the prev frame is being created.
1385 Doing this makes it possible for the user to examine a frame that
1386 has an invalid frame ID.
1388 Some very old VAX code noted: [...] For the sake of argument,
1389 suppose that the stack is somewhat trashed (which is one reason
1390 that "info frame" exists). So, return 0 (indicating we don't
1391 know the address of the arglist) if we don't know what frame this
1395 this_frame->prev = prev_frame;
1396 prev_frame->next = this_frame;
1400 fprintf_unfiltered (gdb_stdlog, "-> ");
1401 fprint_frame (gdb_stdlog, prev_frame);
1402 fprintf_unfiltered (gdb_stdlog, " }\n");
1408 /* Debug routine to print a NULL frame being returned. */
1411 frame_debug_got_null_frame (struct frame_info *this_frame,
1416 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1417 if (this_frame != NULL)
1418 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1420 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1421 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1425 /* Is this (non-sentinel) frame in the "main"() function? */
1428 inside_main_func (struct frame_info *this_frame)
1430 struct minimal_symbol *msymbol;
1433 if (symfile_objfile == 0)
1435 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1436 if (msymbol == NULL)
1438 /* Make certain that the code, and not descriptor, address is
1440 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
1441 SYMBOL_VALUE_ADDRESS (msymbol),
1443 return maddr == get_frame_func (this_frame);
1446 /* Test whether THIS_FRAME is inside the process entry point function. */
1449 inside_entry_func (struct frame_info *this_frame)
1451 return (get_frame_func (this_frame) == entry_point_address ());
1454 /* Return a structure containing various interesting information about
1455 the frame that called THIS_FRAME. Returns NULL if there is entier
1456 no such frame or the frame fails any of a set of target-independent
1457 condition that should terminate the frame chain (e.g., as unwinding
1460 This function should not contain target-dependent tests, such as
1461 checking whether the program-counter is zero. */
1464 get_prev_frame (struct frame_info *this_frame)
1466 struct frame_info *prev_frame;
1468 /* There is always a frame. If this assertion fails, suspect that
1469 something should be calling get_selected_frame() or
1470 get_current_frame(). */
1471 gdb_assert (this_frame != NULL);
1473 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1474 sense to stop unwinding at a dummy frame. One place where a dummy
1475 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1476 pcsqh register (space register for the instruction at the head of the
1477 instruction queue) cannot be written directly; the only way to set it
1478 is to branch to code that is in the target space. In order to implement
1479 frame dummies on HPUX, the called function is made to jump back to where
1480 the inferior was when the user function was called. If gdb was inside
1481 the main function when we created the dummy frame, the dummy frame will
1482 point inside the main function. */
1483 if (this_frame->level >= 0
1484 && get_frame_type (this_frame) != DUMMY_FRAME
1485 && !backtrace_past_main
1486 && inside_main_func (this_frame))
1487 /* Don't unwind past main(). Note, this is done _before_ the
1488 frame has been marked as previously unwound. That way if the
1489 user later decides to enable unwinds past main(), that will
1490 automatically happen. */
1492 frame_debug_got_null_frame (this_frame, "inside main func");
1496 /* If the user's backtrace limit has been exceeded, stop. We must
1497 add two to the current level; one of those accounts for backtrace_limit
1498 being 1-based and the level being 0-based, and the other accounts for
1499 the level of the new frame instead of the level of the current
1501 if (this_frame->level + 2 > backtrace_limit)
1503 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
1507 /* If we're already inside the entry function for the main objfile,
1508 then it isn't valid. Don't apply this test to a dummy frame -
1509 dummy frame PCs typically land in the entry func. Don't apply
1510 this test to the sentinel frame. Sentinel frames should always
1511 be allowed to unwind. */
1512 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1513 wasn't checking for "main" in the minimal symbols. With that
1514 fixed asm-source tests now stop in "main" instead of halting the
1515 backtrace in weird and wonderful ways somewhere inside the entry
1516 file. Suspect that tests for inside the entry file/func were
1517 added to work around that (now fixed) case. */
1518 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1519 suggested having the inside_entry_func test use the
1520 inside_main_func() msymbol trick (along with entry_point_address()
1521 I guess) to determine the address range of the start function.
1522 That should provide a far better stopper than the current
1524 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1525 applied tail-call optimizations to main so that a function called
1526 from main returns directly to the caller of main. Since we don't
1527 stop at main, we should at least stop at the entry point of the
1529 if (!backtrace_past_entry
1530 && get_frame_type (this_frame) != DUMMY_FRAME && this_frame->level >= 0
1531 && inside_entry_func (this_frame))
1533 frame_debug_got_null_frame (this_frame, "inside entry func");
1537 /* Assume that the only way to get a zero PC is through something
1538 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1539 will never unwind a zero PC. */
1540 if (this_frame->level > 0
1541 && get_frame_type (this_frame) == NORMAL_FRAME
1542 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1543 && get_frame_pc (this_frame) == 0)
1545 frame_debug_got_null_frame (this_frame, "zero PC");
1549 return get_prev_frame_1 (this_frame);
1553 get_frame_pc (struct frame_info *frame)
1555 gdb_assert (frame->next != NULL);
1556 return frame_pc_unwind (frame->next);
1559 /* Return an address that falls within THIS_FRAME's code block. */
1562 get_frame_address_in_block (struct frame_info *this_frame)
1564 /* A draft address. */
1565 CORE_ADDR pc = get_frame_pc (this_frame);
1567 struct frame_info *next_frame = this_frame->next;
1569 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1570 Normally the resume address is inside the body of the function
1571 associated with THIS_FRAME, but there is a special case: when
1572 calling a function which the compiler knows will never return
1573 (for instance abort), the call may be the very last instruction
1574 in the calling function. The resume address will point after the
1575 call and may be at the beginning of a different function
1578 If THIS_FRAME is a signal frame or dummy frame, then we should
1579 not adjust the unwound PC. For a dummy frame, GDB pushed the
1580 resume address manually onto the stack. For a signal frame, the
1581 OS may have pushed the resume address manually and invoked the
1582 handler (e.g. GNU/Linux), or invoked the trampoline which called
1583 the signal handler - but in either case the signal handler is
1584 expected to return to the trampoline. So in both of these
1585 cases we know that the resume address is executable and
1586 related. So we only need to adjust the PC if THIS_FRAME
1587 is a normal function.
1589 If the program has been interrupted while THIS_FRAME is current,
1590 then clearly the resume address is inside the associated
1591 function. There are three kinds of interruption: debugger stop
1592 (next frame will be SENTINEL_FRAME), operating system
1593 signal or exception (next frame will be SIGTRAMP_FRAME),
1594 or debugger-induced function call (next frame will be
1595 DUMMY_FRAME). So we only need to adjust the PC if
1596 NEXT_FRAME is a normal function.
1598 We check the type of NEXT_FRAME first, since it is already
1599 known; frame type is determined by the unwinder, and since
1600 we have THIS_FRAME we've already selected an unwinder for
1602 if (get_frame_type (next_frame) == NORMAL_FRAME
1603 && get_frame_type (this_frame) == NORMAL_FRAME)
1610 pc_notcurrent (struct frame_info *frame)
1612 /* If FRAME is not the innermost frame, that normally means that
1613 FRAME->pc points at the return instruction (which is *after* the
1614 call instruction), and we want to get the line containing the
1615 call (because the call is where the user thinks the program is).
1616 However, if the next frame is either a SIGTRAMP_FRAME or a
1617 DUMMY_FRAME, then the next frame will contain a saved interrupt
1618 PC and such a PC indicates the current (rather than next)
1619 instruction/line, consequently, for such cases, want to get the
1620 line containing fi->pc. */
1621 struct frame_info *next = get_next_frame (frame);
1622 int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1627 find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1629 (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
1632 /* Per "frame.h", return the ``address'' of the frame. Code should
1633 really be using get_frame_id(). */
1635 get_frame_base (struct frame_info *fi)
1637 return get_frame_id (fi).stack_addr;
1640 /* High-level offsets into the frame. Used by the debug info. */
1643 get_frame_base_address (struct frame_info *fi)
1645 if (get_frame_type (fi) != NORMAL_FRAME)
1647 if (fi->base == NULL)
1648 fi->base = frame_base_find_by_frame (fi);
1649 /* Sneaky: If the low-level unwind and high-level base code share a
1650 common unwinder, let them share the prologue cache. */
1651 if (fi->base->unwind == fi->unwind)
1652 return fi->base->this_base (fi, &fi->prologue_cache);
1653 return fi->base->this_base (fi, &fi->base_cache);
1657 get_frame_locals_address (struct frame_info *fi)
1660 if (get_frame_type (fi) != NORMAL_FRAME)
1662 /* If there isn't a frame address method, find it. */
1663 if (fi->base == NULL)
1664 fi->base = frame_base_find_by_frame (fi);
1665 /* Sneaky: If the low-level unwind and high-level base code share a
1666 common unwinder, let them share the prologue cache. */
1667 if (fi->base->unwind == fi->unwind)
1668 return fi->base->this_locals (fi, &fi->prologue_cache);
1669 return fi->base->this_locals (fi, &fi->base_cache);
1673 get_frame_args_address (struct frame_info *fi)
1676 if (get_frame_type (fi) != NORMAL_FRAME)
1678 /* If there isn't a frame address method, find it. */
1679 if (fi->base == NULL)
1680 fi->base = frame_base_find_by_frame (fi);
1681 /* Sneaky: If the low-level unwind and high-level base code share a
1682 common unwinder, let them share the prologue cache. */
1683 if (fi->base->unwind == fi->unwind)
1684 return fi->base->this_args (fi, &fi->prologue_cache);
1685 return fi->base->this_args (fi, &fi->base_cache);
1688 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1689 or -1 for a NULL frame. */
1692 frame_relative_level (struct frame_info *fi)
1701 get_frame_type (struct frame_info *frame)
1703 if (frame->unwind == NULL)
1704 /* Initialize the frame's unwinder because that's what
1705 provides the frame's type. */
1706 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
1707 return frame->unwind->type;
1710 /* Memory access methods. */
1713 get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
1714 gdb_byte *buf, int len)
1716 read_memory (addr, buf, len);
1720 get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1723 return read_memory_integer (addr, len);
1727 get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1730 return read_memory_unsigned_integer (addr, len);
1734 safe_frame_unwind_memory (struct frame_info *this_frame,
1735 CORE_ADDR addr, gdb_byte *buf, int len)
1737 /* NOTE: target_read_memory returns zero on success! */
1738 return !target_read_memory (addr, buf, len);
1741 /* Architecture method. */
1744 get_frame_arch (struct frame_info *this_frame)
1746 /* In the future, this function will return a per-frame
1747 architecture instead of current_gdbarch. Calling the
1748 routine with a NULL value of this_frame is a bug! */
1749 gdb_assert (this_frame);
1751 return current_gdbarch;
1754 /* Stack pointer methods. */
1757 get_frame_sp (struct frame_info *this_frame)
1759 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1760 /* Normality - an architecture that provides a way of obtaining any
1761 frame inner-most address. */
1762 if (gdbarch_unwind_sp_p (gdbarch))
1763 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
1764 operate on THIS_FRAME now. */
1765 return gdbarch_unwind_sp (gdbarch, this_frame->next);
1766 /* Now things are really are grim. Hope that the value returned by
1767 the gdbarch_sp_regnum register is meaningful. */
1768 if (gdbarch_sp_regnum (gdbarch) >= 0)
1769 return get_frame_register_unsigned (this_frame,
1770 gdbarch_sp_regnum (gdbarch));
1771 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
1774 /* Return the reason why we can't unwind past FRAME. */
1776 enum unwind_stop_reason
1777 get_frame_unwind_stop_reason (struct frame_info *frame)
1779 /* If we haven't tried to unwind past this point yet, then assume
1780 that unwinding would succeed. */
1781 if (frame->prev_p == 0)
1782 return UNWIND_NO_REASON;
1784 /* Otherwise, we set a reason when we succeeded (or failed) to
1786 return frame->stop_reason;
1789 /* Return a string explaining REASON. */
1792 frame_stop_reason_string (enum unwind_stop_reason reason)
1796 case UNWIND_NULL_ID:
1797 return _("unwinder did not report frame ID");
1799 case UNWIND_INNER_ID:
1800 return _("previous frame inner to this frame (corrupt stack?)");
1802 case UNWIND_SAME_ID:
1803 return _("previous frame identical to this frame (corrupt stack?)");
1805 case UNWIND_NO_SAVED_PC:
1806 return _("frame did not save the PC");
1808 case UNWIND_NO_REASON:
1809 case UNWIND_FIRST_ERROR:
1811 internal_error (__FILE__, __LINE__,
1812 "Invalid frame stop reason");
1816 /* Clean up after a failed (wrong unwinder) attempt to unwind past
1820 frame_cleanup_after_sniffer (void *arg)
1822 struct frame_info *frame = arg;
1824 /* The sniffer should not allocate a prologue cache if it did not
1825 match this frame. */
1826 gdb_assert (frame->prologue_cache == NULL);
1828 /* No sniffer should extend the frame chain; sniff based on what is
1830 gdb_assert (!frame->prev_p);
1832 /* The sniffer should not check the frame's ID; that's circular. */
1833 gdb_assert (!frame->this_id.p);
1835 /* Clear cached fields dependent on the unwinder.
1837 The previous PC is independent of the unwinder, but the previous
1838 function is not (see get_frame_address_in_block). */
1839 frame->prev_func.p = 0;
1840 frame->prev_func.addr = 0;
1842 /* Discard the unwinder last, so that we can easily find it if an assertion
1843 in this function triggers. */
1844 frame->unwind = NULL;
1847 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
1848 Return a cleanup which should be called if unwinding fails, and
1849 discarded if it succeeds. */
1852 frame_prepare_for_sniffer (struct frame_info *frame,
1853 const struct frame_unwind *unwind)
1855 gdb_assert (frame->unwind == NULL);
1856 frame->unwind = unwind;
1857 return make_cleanup (frame_cleanup_after_sniffer, frame);
1860 extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
1862 static struct cmd_list_element *set_backtrace_cmdlist;
1863 static struct cmd_list_element *show_backtrace_cmdlist;
1866 set_backtrace_cmd (char *args, int from_tty)
1868 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
1872 show_backtrace_cmd (char *args, int from_tty)
1874 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
1878 _initialize_frame (void)
1880 obstack_init (&frame_cache_obstack);
1882 observer_attach_target_changed (frame_observer_target_changed);
1884 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
1885 Set backtrace specific variables.\n\
1886 Configure backtrace variables such as the backtrace limit"),
1887 &set_backtrace_cmdlist, "set backtrace ",
1888 0/*allow-unknown*/, &setlist);
1889 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
1890 Show backtrace specific variables\n\
1891 Show backtrace variables such as the backtrace limit"),
1892 &show_backtrace_cmdlist, "show backtrace ",
1893 0/*allow-unknown*/, &showlist);
1895 add_setshow_boolean_cmd ("past-main", class_obscure,
1896 &backtrace_past_main, _("\
1897 Set whether backtraces should continue past \"main\"."), _("\
1898 Show whether backtraces should continue past \"main\"."), _("\
1899 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1900 the backtrace at \"main\". Set this variable if you need to see the rest\n\
1901 of the stack trace."),
1903 show_backtrace_past_main,
1904 &set_backtrace_cmdlist,
1905 &show_backtrace_cmdlist);
1907 add_setshow_boolean_cmd ("past-entry", class_obscure,
1908 &backtrace_past_entry, _("\
1909 Set whether backtraces should continue past the entry point of a program."),
1911 Show whether backtraces should continue past the entry point of a program."),
1913 Normally there are no callers beyond the entry point of a program, so GDB\n\
1914 will terminate the backtrace there. Set this variable if you need to see \n\
1915 the rest of the stack trace."),
1917 show_backtrace_past_entry,
1918 &set_backtrace_cmdlist,
1919 &show_backtrace_cmdlist);
1921 add_setshow_integer_cmd ("limit", class_obscure,
1922 &backtrace_limit, _("\
1923 Set an upper bound on the number of backtrace levels."), _("\
1924 Show the upper bound on the number of backtrace levels."), _("\
1925 No more than the specified number of frames can be displayed or examined.\n\
1926 Zero is unlimited."),
1928 show_backtrace_limit,
1929 &set_backtrace_cmdlist,
1930 &show_backtrace_cmdlist);
1932 /* Debug this files internals. */
1933 add_setshow_zinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
1934 Set frame debugging."), _("\
1935 Show frame debugging."), _("\
1936 When non-zero, frame specific internal debugging is enabled."),
1939 &setdebuglist, &showdebuglist);