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.
381 * If frame NEXT is the immediate inner frame to THIS, and NEXT
382 is a NORMAL frame, then the stack address of NEXT must be
383 inner-than-or-equal to the stack address of THIS.
385 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
388 * If frame NEXT is the immediate inner frame to THIS, and NEXT
389 is a NORMAL frame, and NEXT and THIS have different stack
390 addresses, no other frame in the frame chain may have a stack
393 Therefore, if frame_id_inner (TEST, THIS) holds, but
394 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
395 to a valid frame in the frame chain. */
398 frame_id_inner (struct gdbarch
*gdbarch
, struct frame_id l
, struct frame_id r
)
401 if (!l
.stack_addr_p
|| !r
.stack_addr_p
)
402 /* Like NaN, any operation involving an invalid ID always fails. */
405 /* Only return non-zero when strictly inner than. Note that, per
406 comment in "frame.h", there is some fuzz here. Frameless
407 functions are not strictly inner than (same .stack but
408 different .code and/or .special address). */
409 inner
= gdbarch_inner_than (gdbarch
, l
.stack_addr
, r
.stack_addr
);
412 fprintf_unfiltered (gdb_stdlog
, "{ frame_id_inner (l=");
413 fprint_frame_id (gdb_stdlog
, l
);
414 fprintf_unfiltered (gdb_stdlog
, ",r=");
415 fprint_frame_id (gdb_stdlog
, r
);
416 fprintf_unfiltered (gdb_stdlog
, ") -> %d }\n", inner
);
422 frame_find_by_id (struct frame_id id
)
424 struct frame_info
*frame
, *prev_frame
;
426 /* ZERO denotes the null frame, let the caller decide what to do
427 about it. Should it instead return get_current_frame()? */
428 if (!frame_id_p (id
))
431 for (frame
= get_current_frame (); ; frame
= prev_frame
)
433 struct frame_id
this = get_frame_id (frame
);
434 if (frame_id_eq (id
, this))
435 /* An exact match. */
438 prev_frame
= get_prev_frame (frame
);
442 /* As a safety net to avoid unnecessary backtracing while trying
443 to find an invalid ID, we check for a common situation where
444 we can detect from comparing stack addresses that no other
445 frame in the current frame chain can have this ID. See the
446 comment at frame_id_inner for details. */
447 if (get_frame_type (frame
) == NORMAL_FRAME
448 && !frame_id_inner (get_frame_arch (frame
), id
, this)
449 && frame_id_inner (get_frame_arch (prev_frame
), id
,
450 get_frame_id (prev_frame
)))
457 frame_pc_unwind (struct frame_info
*this_frame
)
459 if (!this_frame
->prev_pc
.p
)
462 if (gdbarch_unwind_pc_p (get_frame_arch (this_frame
)))
464 /* The right way. The `pure' way. The one true way. This
465 method depends solely on the register-unwind code to
466 determine the value of registers in THIS frame, and hence
467 the value of this frame's PC (resume address). A typical
468 implementation is no more than:
470 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
471 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
473 Note: this method is very heavily dependent on a correct
474 register-unwind implementation, it pays to fix that
475 method first; this method is frame type agnostic, since
476 it only deals with register values, it works with any
477 frame. This is all in stark contrast to the old
478 FRAME_SAVED_PC which would try to directly handle all the
479 different ways that a PC could be unwound. */
480 pc
= gdbarch_unwind_pc (get_frame_arch (this_frame
), this_frame
);
483 internal_error (__FILE__
, __LINE__
, _("No unwind_pc method"));
484 this_frame
->prev_pc
.value
= pc
;
485 this_frame
->prev_pc
.p
= 1;
487 fprintf_unfiltered (gdb_stdlog
,
488 "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
490 paddr_nz (this_frame
->prev_pc
.value
));
492 return this_frame
->prev_pc
.value
;
496 get_frame_func (struct frame_info
*this_frame
)
498 struct frame_info
*next_frame
= this_frame
->next
;
500 if (!next_frame
->prev_func
.p
)
502 /* Make certain that this, and not the adjacent, function is
504 CORE_ADDR addr_in_block
= get_frame_address_in_block (this_frame
);
505 next_frame
->prev_func
.p
= 1;
506 next_frame
->prev_func
.addr
= get_pc_function_start (addr_in_block
);
508 fprintf_unfiltered (gdb_stdlog
,
509 "{ get_frame_func (this_frame=%d) -> 0x%s }\n",
511 paddr_nz (next_frame
->prev_func
.addr
));
513 return next_frame
->prev_func
.addr
;
517 do_frame_register_read (void *src
, int regnum
, gdb_byte
*buf
)
519 return frame_register_read (src
, regnum
, buf
);
523 frame_save_as_regcache (struct frame_info
*this_frame
)
525 struct regcache
*regcache
= regcache_xmalloc (get_frame_arch (this_frame
));
526 struct cleanup
*cleanups
= make_cleanup_regcache_xfree (regcache
);
527 regcache_save (regcache
, do_frame_register_read
, this_frame
);
528 discard_cleanups (cleanups
);
533 frame_pop (struct frame_info
*this_frame
)
535 struct frame_info
*prev_frame
;
536 struct regcache
*scratch
;
537 struct cleanup
*cleanups
;
539 if (get_frame_type (this_frame
) == DUMMY_FRAME
)
541 /* Popping a dummy frame involves restoring more than just registers.
542 dummy_frame_pop does all the work. */
543 dummy_frame_pop (get_frame_id (this_frame
));
547 /* Ensure that we have a frame to pop to. */
548 prev_frame
= get_prev_frame_1 (this_frame
);
551 error (_("Cannot pop the initial frame."));
553 /* Make a copy of all the register values unwound from this frame.
554 Save them in a scratch buffer so that there isn't a race between
555 trying to extract the old values from the current regcache while
556 at the same time writing new values into that same cache. */
557 scratch
= frame_save_as_regcache (prev_frame
);
558 cleanups
= make_cleanup_regcache_xfree (scratch
);
560 /* FIXME: cagney/2003-03-16: It should be possible to tell the
561 target's register cache that it is about to be hit with a burst
562 register transfer and that the sequence of register writes should
563 be batched. The pair target_prepare_to_store() and
564 target_store_registers() kind of suggest this functionality.
565 Unfortunately, they don't implement it. Their lack of a formal
566 definition can lead to targets writing back bogus values
567 (arguably a bug in the target code mind). */
568 /* Now copy those saved registers into the current regcache.
569 Here, regcache_cpy() calls regcache_restore(). */
570 regcache_cpy (get_current_regcache (), scratch
);
571 do_cleanups (cleanups
);
573 /* We've made right mess of GDB's local state, just discard
575 reinit_frame_cache ();
579 frame_register_unwind (struct frame_info
*frame
, int regnum
,
580 int *optimizedp
, enum lval_type
*lvalp
,
581 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
585 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
586 that the value proper does not need to be fetched. */
587 gdb_assert (optimizedp
!= NULL
);
588 gdb_assert (lvalp
!= NULL
);
589 gdb_assert (addrp
!= NULL
);
590 gdb_assert (realnump
!= NULL
);
591 /* gdb_assert (bufferp != NULL); */
593 value
= frame_unwind_register_value (frame
, regnum
);
595 gdb_assert (value
!= NULL
);
597 *optimizedp
= value_optimized_out (value
);
598 *lvalp
= VALUE_LVAL (value
);
599 *addrp
= VALUE_ADDRESS (value
);
600 *realnump
= VALUE_REGNUM (value
);
603 memcpy (bufferp
, value_contents_all (value
),
604 TYPE_LENGTH (value_type (value
)));
606 /* Dispose of the new value. This prevents watchpoints from
607 trying to watch the saved frame pointer. */
608 release_value (value
);
613 frame_register (struct frame_info
*frame
, int regnum
,
614 int *optimizedp
, enum lval_type
*lvalp
,
615 CORE_ADDR
*addrp
, int *realnump
, gdb_byte
*bufferp
)
617 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
618 that the value proper does not need to be fetched. */
619 gdb_assert (optimizedp
!= NULL
);
620 gdb_assert (lvalp
!= NULL
);
621 gdb_assert (addrp
!= NULL
);
622 gdb_assert (realnump
!= NULL
);
623 /* gdb_assert (bufferp != NULL); */
625 /* Obtain the register value by unwinding the register from the next
626 (more inner frame). */
627 gdb_assert (frame
!= NULL
&& frame
->next
!= NULL
);
628 frame_register_unwind (frame
->next
, regnum
, optimizedp
, lvalp
, addrp
,
633 frame_unwind_register (struct frame_info
*frame
, int regnum
, gdb_byte
*buf
)
639 frame_register_unwind (frame
, regnum
, &optimized
, &lval
, &addr
,
644 get_frame_register (struct frame_info
*frame
,
645 int regnum
, gdb_byte
*buf
)
647 frame_unwind_register (frame
->next
, regnum
, buf
);
651 frame_unwind_register_value (struct frame_info
*frame
, int regnum
)
655 gdb_assert (frame
!= NULL
);
659 fprintf_unfiltered (gdb_stdlog
, "\
660 { frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
661 frame
->level
, regnum
,
662 user_reg_map_regnum_to_name
663 (get_frame_arch (frame
), regnum
));
666 /* Find the unwinder. */
667 if (frame
->unwind
== NULL
)
668 frame
->unwind
= frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
670 /* Ask this frame to unwind its register. */
671 value
= frame
->unwind
->prev_register (frame
, &frame
->prologue_cache
, regnum
);
675 fprintf_unfiltered (gdb_stdlog
, "->");
676 if (value_optimized_out (value
))
677 fprintf_unfiltered (gdb_stdlog
, " optimized out");
680 if (VALUE_LVAL (value
) == lval_register
)
681 fprintf_unfiltered (gdb_stdlog
, " register=%d",
682 VALUE_REGNUM (value
));
683 else if (VALUE_LVAL (value
) == lval_memory
)
684 fprintf_unfiltered (gdb_stdlog
, " address=0x%s",
685 paddr_nz (VALUE_ADDRESS (value
)));
687 fprintf_unfiltered (gdb_stdlog
, " computed");
689 if (value_lazy (value
))
690 fprintf_unfiltered (gdb_stdlog
, " lazy");
694 const gdb_byte
*buf
= value_contents (value
);
696 fprintf_unfiltered (gdb_stdlog
, " bytes=");
697 fprintf_unfiltered (gdb_stdlog
, "[");
698 for (i
= 0; i
< register_size (get_frame_arch (frame
), regnum
); i
++)
699 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
700 fprintf_unfiltered (gdb_stdlog
, "]");
704 fprintf_unfiltered (gdb_stdlog
, " }\n");
711 get_frame_register_value (struct frame_info
*frame
, int regnum
)
713 return frame_unwind_register_value (frame
->next
, regnum
);
717 frame_unwind_register_signed (struct frame_info
*frame
, int regnum
)
719 gdb_byte buf
[MAX_REGISTER_SIZE
];
720 frame_unwind_register (frame
, regnum
, buf
);
721 return extract_signed_integer (buf
, register_size (get_frame_arch (frame
),
726 get_frame_register_signed (struct frame_info
*frame
, int regnum
)
728 return frame_unwind_register_signed (frame
->next
, regnum
);
732 frame_unwind_register_unsigned (struct frame_info
*frame
, int regnum
)
734 gdb_byte buf
[MAX_REGISTER_SIZE
];
735 frame_unwind_register (frame
, regnum
, buf
);
736 return extract_unsigned_integer (buf
, register_size (get_frame_arch (frame
),
741 get_frame_register_unsigned (struct frame_info
*frame
, int regnum
)
743 return frame_unwind_register_unsigned (frame
->next
, regnum
);
747 put_frame_register (struct frame_info
*frame
, int regnum
,
750 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
755 frame_register (frame
, regnum
, &optim
, &lval
, &addr
, &realnum
, NULL
);
757 error (_("Attempt to assign to a value that was optimized out."));
762 /* FIXME: write_memory doesn't yet take constant buffers.
764 gdb_byte tmp
[MAX_REGISTER_SIZE
];
765 memcpy (tmp
, buf
, register_size (gdbarch
, regnum
));
766 write_memory (addr
, tmp
, register_size (gdbarch
, regnum
));
770 regcache_cooked_write (get_current_regcache (), realnum
, buf
);
773 error (_("Attempt to assign to an unmodifiable value."));
777 /* frame_register_read ()
779 Find and return the value of REGNUM for the specified stack frame.
780 The number of bytes copied is REGISTER_SIZE (REGNUM).
782 Returns 0 if the register value could not be found. */
785 frame_register_read (struct frame_info
*frame
, int regnum
,
792 frame_register (frame
, regnum
, &optimized
, &lval
, &addr
, &realnum
, myaddr
);
798 get_frame_register_bytes (struct frame_info
*frame
, int regnum
,
799 CORE_ADDR offset
, int len
, gdb_byte
*myaddr
)
801 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
806 /* Skip registers wholly inside of OFFSET. */
807 while (offset
>= register_size (gdbarch
, regnum
))
809 offset
-= register_size (gdbarch
, regnum
);
813 /* Ensure that we will not read beyond the end of the register file.
814 This can only ever happen if the debug information is bad. */
816 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
817 for (i
= regnum
; i
< numregs
; i
++)
819 int thissize
= register_size (gdbarch
, i
);
821 break; /* This register is not available on this architecture. */
826 warning (_("Bad debug information detected: "
827 "Attempt to read %d bytes from registers."), len
);
834 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
838 if (curr_len
== register_size (gdbarch
, regnum
))
840 if (!frame_register_read (frame
, regnum
, myaddr
))
845 gdb_byte buf
[MAX_REGISTER_SIZE
];
846 if (!frame_register_read (frame
, regnum
, buf
))
848 memcpy (myaddr
, buf
+ offset
, curr_len
);
861 put_frame_register_bytes (struct frame_info
*frame
, int regnum
,
862 CORE_ADDR offset
, int len
, const gdb_byte
*myaddr
)
864 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
866 /* Skip registers wholly inside of OFFSET. */
867 while (offset
>= register_size (gdbarch
, regnum
))
869 offset
-= register_size (gdbarch
, regnum
);
876 int curr_len
= register_size (gdbarch
, regnum
) - offset
;
880 if (curr_len
== register_size (gdbarch
, regnum
))
882 put_frame_register (frame
, regnum
, myaddr
);
886 gdb_byte buf
[MAX_REGISTER_SIZE
];
887 frame_register_read (frame
, regnum
, buf
);
888 memcpy (buf
+ offset
, myaddr
, curr_len
);
889 put_frame_register (frame
, regnum
, buf
);
899 /* Create a sentinel frame. */
901 static struct frame_info
*
902 create_sentinel_frame (struct regcache
*regcache
)
904 struct frame_info
*frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
906 /* Explicitly initialize the sentinel frame's cache. Provide it
907 with the underlying regcache. In the future additional
908 information, such as the frame's thread will be added. */
909 frame
->prologue_cache
= sentinel_frame_cache (regcache
);
910 /* For the moment there is only one sentinel frame implementation. */
911 frame
->unwind
= sentinel_frame_unwind
;
912 /* Link this frame back to itself. The frame is self referential
913 (the unwound PC is the same as the pc), so make it so. */
915 /* Make the sentinel frame's ID valid, but invalid. That way all
916 comparisons with it should fail. */
917 frame
->this_id
.p
= 1;
918 frame
->this_id
.value
= null_frame_id
;
921 fprintf_unfiltered (gdb_stdlog
, "{ create_sentinel_frame (...) -> ");
922 fprint_frame (gdb_stdlog
, frame
);
923 fprintf_unfiltered (gdb_stdlog
, " }\n");
928 /* Info about the innermost stack frame (contents of FP register) */
930 static struct frame_info
*current_frame
;
932 /* Cache for frame addresses already read by gdb. Valid only while
933 inferior is stopped. Control variables for the frame cache should
934 be local to this module. */
936 static struct obstack frame_cache_obstack
;
939 frame_obstack_zalloc (unsigned long size
)
941 void *data
= obstack_alloc (&frame_cache_obstack
, size
);
942 memset (data
, 0, size
);
946 /* Return the innermost (currently executing) stack frame. This is
947 split into two functions. The function unwind_to_current_frame()
948 is wrapped in catch exceptions so that, even when the unwind of the
949 sentinel frame fails, the function still returns a stack frame. */
952 unwind_to_current_frame (struct ui_out
*ui_out
, void *args
)
954 struct frame_info
*frame
= get_prev_frame (args
);
955 /* A sentinel frame can fail to unwind, e.g., because its PC value
956 lands in somewhere like start. */
959 current_frame
= frame
;
964 get_current_frame (void)
966 /* First check, and report, the lack of registers. Having GDB
967 report "No stack!" or "No memory" when the target doesn't even
968 have registers is very confusing. Besides, "printcmd.exp"
969 explicitly checks that ``print $pc'' with no registers prints "No
971 if (!target_has_registers
)
972 error (_("No registers."));
973 if (!target_has_stack
)
974 error (_("No stack."));
975 if (!target_has_memory
)
976 error (_("No memory."));
977 if (is_executing (inferior_ptid
))
978 error (_("Target is executing."));
980 if (current_frame
== NULL
)
982 struct frame_info
*sentinel_frame
=
983 create_sentinel_frame (get_current_regcache ());
984 if (catch_exceptions (uiout
, unwind_to_current_frame
, sentinel_frame
,
985 RETURN_MASK_ERROR
) != 0)
987 /* Oops! Fake a current frame? Is this useful? It has a PC
988 of zero, for instance. */
989 current_frame
= sentinel_frame
;
992 return current_frame
;
995 /* The "selected" stack frame is used by default for local and arg
996 access. May be zero, for no selected frame. */
998 static struct frame_info
*selected_frame
;
1001 has_stack_frames (void)
1003 if (!target_has_registers
|| !target_has_stack
|| !target_has_memory
)
1006 /* If the current thread is executing, don't try to read from
1008 if (is_executing (inferior_ptid
))
1014 /* Return the selected frame. Always non-NULL (unless there isn't an
1015 inferior sufficient for creating a frame) in which case an error is
1019 get_selected_frame (const char *message
)
1021 if (selected_frame
== NULL
)
1023 if (message
!= NULL
&& !has_stack_frames ())
1024 error (("%s"), message
);
1025 /* Hey! Don't trust this. It should really be re-finding the
1026 last selected frame of the currently selected thread. This,
1027 though, is better than nothing. */
1028 select_frame (get_current_frame ());
1030 /* There is always a frame. */
1031 gdb_assert (selected_frame
!= NULL
);
1032 return selected_frame
;
1035 /* This is a variant of get_selected_frame() which can be called when
1036 the inferior does not have a frame; in that case it will return
1037 NULL instead of calling error(). */
1040 deprecated_safe_get_selected_frame (void)
1042 if (!has_stack_frames ())
1044 return get_selected_frame (NULL
);
1047 /* Select frame FI (or NULL - to invalidate the current frame). */
1050 select_frame (struct frame_info
*fi
)
1054 selected_frame
= fi
;
1055 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1056 frame is being invalidated. */
1057 if (deprecated_selected_frame_level_changed_hook
)
1058 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi
));
1060 /* FIXME: kseitz/2002-08-28: It would be nice to call
1061 selected_frame_level_changed_event() right here, but due to limitations
1062 in the current interfaces, we would end up flooding UIs with events
1063 because select_frame() is used extensively internally.
1065 Once we have frame-parameterized frame (and frame-related) commands,
1066 the event notification can be moved here, since this function will only
1067 be called when the user's selected frame is being changed. */
1069 /* Ensure that symbols for this frame are read in. Also, determine the
1070 source language of this frame, and switch to it if desired. */
1073 /* We retrieve the frame's symtab by using the frame PC. However
1074 we cannot use the frame PC as-is, because it usually points to
1075 the instruction following the "call", which is sometimes the
1076 first instruction of another function. So we rely on
1077 get_frame_address_in_block() which provides us with a PC which
1078 is guaranteed to be inside the frame's code block. */
1079 s
= find_pc_symtab (get_frame_address_in_block (fi
));
1081 && s
->language
!= current_language
->la_language
1082 && s
->language
!= language_unknown
1083 && language_mode
== language_mode_auto
)
1085 set_language (s
->language
);
1090 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
1091 Always returns a non-NULL value. */
1094 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
1096 struct frame_info
*fi
;
1100 fprintf_unfiltered (gdb_stdlog
,
1101 "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1102 paddr_nz (addr
), paddr_nz (pc
));
1105 fi
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1107 fi
->next
= create_sentinel_frame (get_current_regcache ());
1109 /* Select/initialize both the unwind function and the frame's type
1111 fi
->unwind
= frame_unwind_find_by_frame (fi
, &fi
->prologue_cache
);
1114 fi
->this_id
.value
.stack_addr
= addr
;
1115 /* While we're at it, update this frame's cached PC value, found
1116 in the next frame. Oh for the day when "struct frame_info"
1117 is opaque and this hack on hack can just go away. */
1118 fi
->next
->prev_pc
.value
= pc
;
1119 fi
->next
->prev_pc
.p
= 1;
1123 fprintf_unfiltered (gdb_stdlog
, "-> ");
1124 fprint_frame (gdb_stdlog
, fi
);
1125 fprintf_unfiltered (gdb_stdlog
, " }\n");
1131 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1132 innermost frame). Be careful to not fall off the bottom of the
1133 frame chain and onto the sentinel frame. */
1136 get_next_frame (struct frame_info
*this_frame
)
1138 if (this_frame
->level
> 0)
1139 return this_frame
->next
;
1144 /* Observer for the target_changed event. */
1147 frame_observer_target_changed (struct target_ops
*target
)
1149 reinit_frame_cache ();
1152 /* Flush the entire frame cache. */
1155 reinit_frame_cache (void)
1157 struct frame_info
*fi
;
1159 /* Tear down all frame caches. */
1160 for (fi
= current_frame
; fi
!= NULL
; fi
= fi
->prev
)
1162 if (fi
->prologue_cache
&& fi
->unwind
->dealloc_cache
)
1163 fi
->unwind
->dealloc_cache (fi
, fi
->prologue_cache
);
1164 if (fi
->base_cache
&& fi
->base
->unwind
->dealloc_cache
)
1165 fi
->base
->unwind
->dealloc_cache (fi
, fi
->base_cache
);
1168 /* Since we can't really be sure what the first object allocated was */
1169 obstack_free (&frame_cache_obstack
, 0);
1170 obstack_init (&frame_cache_obstack
);
1172 if (current_frame
!= NULL
)
1173 annotate_frames_invalid ();
1175 current_frame
= NULL
; /* Invalidate cache */
1176 select_frame (NULL
);
1178 fprintf_unfiltered (gdb_stdlog
, "{ reinit_frame_cache () }\n");
1181 /* Find where a register is saved (in memory or another register).
1182 The result of frame_register_unwind is just where it is saved
1183 relative to this particular frame. */
1186 frame_register_unwind_location (struct frame_info
*this_frame
, int regnum
,
1187 int *optimizedp
, enum lval_type
*lvalp
,
1188 CORE_ADDR
*addrp
, int *realnump
)
1190 gdb_assert (this_frame
== NULL
|| this_frame
->level
>= 0);
1192 while (this_frame
!= NULL
)
1194 frame_register_unwind (this_frame
, regnum
, optimizedp
, lvalp
,
1195 addrp
, realnump
, NULL
);
1200 if (*lvalp
!= lval_register
)
1204 this_frame
= get_next_frame (this_frame
);
1208 /* Return a "struct frame_info" corresponding to the frame that called
1209 THIS_FRAME. Returns NULL if there is no such frame.
1211 Unlike get_prev_frame, this function always tries to unwind the
1214 static struct frame_info
*
1215 get_prev_frame_1 (struct frame_info
*this_frame
)
1217 struct frame_info
*prev_frame
;
1218 struct frame_id this_id
;
1219 struct gdbarch
*gdbarch
;
1221 gdb_assert (this_frame
!= NULL
);
1222 gdbarch
= get_frame_arch (this_frame
);
1226 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame_1 (this_frame=");
1227 if (this_frame
!= NULL
)
1228 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1230 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1231 fprintf_unfiltered (gdb_stdlog
, ") ");
1234 /* Only try to do the unwind once. */
1235 if (this_frame
->prev_p
)
1239 fprintf_unfiltered (gdb_stdlog
, "-> ");
1240 fprint_frame (gdb_stdlog
, this_frame
->prev
);
1241 fprintf_unfiltered (gdb_stdlog
, " // cached \n");
1243 return this_frame
->prev
;
1246 /* If the frame unwinder hasn't been selected yet, we must do so
1247 before setting prev_p; otherwise the check for misbehaved
1248 sniffers will think that this frame's sniffer tried to unwind
1249 further (see frame_cleanup_after_sniffer). */
1250 if (this_frame
->unwind
== NULL
)
1252 = frame_unwind_find_by_frame (this_frame
, &this_frame
->prologue_cache
);
1254 this_frame
->prev_p
= 1;
1255 this_frame
->stop_reason
= UNWIND_NO_REASON
;
1257 /* Check that this frame's ID was valid. If it wasn't, don't try to
1258 unwind to the prev frame. Be careful to not apply this test to
1259 the sentinel frame. */
1260 this_id
= get_frame_id (this_frame
);
1261 if (this_frame
->level
>= 0 && !frame_id_p (this_id
))
1265 fprintf_unfiltered (gdb_stdlog
, "-> ");
1266 fprint_frame (gdb_stdlog
, NULL
);
1267 fprintf_unfiltered (gdb_stdlog
, " // this ID is NULL }\n");
1269 this_frame
->stop_reason
= UNWIND_NULL_ID
;
1273 /* Check that this frame's ID isn't inner to (younger, below, next)
1274 the next frame. This happens when a frame unwind goes backwards.
1275 This check is valid only if the next frame is NORMAL. See the
1276 comment at frame_id_inner for details. */
1277 if (this_frame
->next
->unwind
->type
== NORMAL_FRAME
1278 && frame_id_inner (get_frame_arch (this_frame
->next
), this_id
,
1279 get_frame_id (this_frame
->next
)))
1283 fprintf_unfiltered (gdb_stdlog
, "-> ");
1284 fprint_frame (gdb_stdlog
, NULL
);
1285 fprintf_unfiltered (gdb_stdlog
, " // this frame ID is inner }\n");
1287 this_frame
->stop_reason
= UNWIND_INNER_ID
;
1291 /* Check that this and the next frame are not identical. If they
1292 are, there is most likely a stack cycle. As with the inner-than
1293 test above, avoid comparing the inner-most and sentinel frames. */
1294 if (this_frame
->level
> 0
1295 && frame_id_eq (this_id
, get_frame_id (this_frame
->next
)))
1299 fprintf_unfiltered (gdb_stdlog
, "-> ");
1300 fprint_frame (gdb_stdlog
, NULL
);
1301 fprintf_unfiltered (gdb_stdlog
, " // this frame has same ID }\n");
1303 this_frame
->stop_reason
= UNWIND_SAME_ID
;
1307 /* Check that this and the next frame do not unwind the PC register
1308 to the same memory location. If they do, then even though they
1309 have different frame IDs, the new frame will be bogus; two
1310 functions can't share a register save slot for the PC. This can
1311 happen when the prologue analyzer finds a stack adjustment, but
1314 This check does assume that the "PC register" is roughly a
1315 traditional PC, even if the gdbarch_unwind_pc method adjusts
1316 it (we do not rely on the value, only on the unwound PC being
1317 dependent on this value). A potential improvement would be
1318 to have the frame prev_pc method and the gdbarch unwind_pc
1319 method set the same lval and location information as
1320 frame_register_unwind. */
1321 if (this_frame
->level
> 0
1322 && gdbarch_pc_regnum (gdbarch
) >= 0
1323 && get_frame_type (this_frame
) == NORMAL_FRAME
1324 && get_frame_type (this_frame
->next
) == NORMAL_FRAME
)
1326 int optimized
, realnum
, nrealnum
;
1327 enum lval_type lval
, nlval
;
1328 CORE_ADDR addr
, naddr
;
1330 frame_register_unwind_location (this_frame
,
1331 gdbarch_pc_regnum (gdbarch
),
1332 &optimized
, &lval
, &addr
, &realnum
);
1333 frame_register_unwind_location (get_next_frame (this_frame
),
1334 gdbarch_pc_regnum (gdbarch
),
1335 &optimized
, &nlval
, &naddr
, &nrealnum
);
1337 if ((lval
== lval_memory
&& lval
== nlval
&& addr
== naddr
)
1338 || (lval
== lval_register
&& lval
== nlval
&& realnum
== nrealnum
))
1342 fprintf_unfiltered (gdb_stdlog
, "-> ");
1343 fprint_frame (gdb_stdlog
, NULL
);
1344 fprintf_unfiltered (gdb_stdlog
, " // no saved PC }\n");
1347 this_frame
->stop_reason
= UNWIND_NO_SAVED_PC
;
1348 this_frame
->prev
= NULL
;
1353 /* Allocate the new frame but do not wire it in to the frame chain.
1354 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1355 frame->next to pull some fancy tricks (of course such code is, by
1356 definition, recursive). Try to prevent it.
1358 There is no reason to worry about memory leaks, should the
1359 remainder of the function fail. The allocated memory will be
1360 quickly reclaimed when the frame cache is flushed, and the `we've
1361 been here before' check above will stop repeated memory
1362 allocation calls. */
1363 prev_frame
= FRAME_OBSTACK_ZALLOC (struct frame_info
);
1364 prev_frame
->level
= this_frame
->level
+ 1;
1366 /* Don't yet compute ->unwind (and hence ->type). It is computed
1367 on-demand in get_frame_type, frame_register_unwind, and
1370 /* Don't yet compute the frame's ID. It is computed on-demand by
1373 /* The unwound frame ID is validate at the start of this function,
1374 as part of the logic to decide if that frame should be further
1375 unwound, and not here while the prev frame is being created.
1376 Doing this makes it possible for the user to examine a frame that
1377 has an invalid frame ID.
1379 Some very old VAX code noted: [...] For the sake of argument,
1380 suppose that the stack is somewhat trashed (which is one reason
1381 that "info frame" exists). So, return 0 (indicating we don't
1382 know the address of the arglist) if we don't know what frame this
1386 this_frame
->prev
= prev_frame
;
1387 prev_frame
->next
= this_frame
;
1391 fprintf_unfiltered (gdb_stdlog
, "-> ");
1392 fprint_frame (gdb_stdlog
, prev_frame
);
1393 fprintf_unfiltered (gdb_stdlog
, " }\n");
1399 /* Debug routine to print a NULL frame being returned. */
1402 frame_debug_got_null_frame (struct frame_info
*this_frame
,
1407 fprintf_unfiltered (gdb_stdlog
, "{ get_prev_frame (this_frame=");
1408 if (this_frame
!= NULL
)
1409 fprintf_unfiltered (gdb_stdlog
, "%d", this_frame
->level
);
1411 fprintf_unfiltered (gdb_stdlog
, "<NULL>");
1412 fprintf_unfiltered (gdb_stdlog
, ") -> // %s}\n", reason
);
1416 /* Is this (non-sentinel) frame in the "main"() function? */
1419 inside_main_func (struct frame_info
*this_frame
)
1421 struct minimal_symbol
*msymbol
;
1424 if (symfile_objfile
== 0)
1426 msymbol
= lookup_minimal_symbol (main_name (), NULL
, symfile_objfile
);
1427 if (msymbol
== NULL
)
1429 /* Make certain that the code, and not descriptor, address is
1431 maddr
= gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame
),
1432 SYMBOL_VALUE_ADDRESS (msymbol
),
1434 return maddr
== get_frame_func (this_frame
);
1437 /* Test whether THIS_FRAME is inside the process entry point function. */
1440 inside_entry_func (struct frame_info
*this_frame
)
1442 return (get_frame_func (this_frame
) == entry_point_address ());
1445 /* Return a structure containing various interesting information about
1446 the frame that called THIS_FRAME. Returns NULL if there is entier
1447 no such frame or the frame fails any of a set of target-independent
1448 condition that should terminate the frame chain (e.g., as unwinding
1451 This function should not contain target-dependent tests, such as
1452 checking whether the program-counter is zero. */
1455 get_prev_frame (struct frame_info
*this_frame
)
1457 struct frame_info
*prev_frame
;
1459 /* Return the inner-most frame, when the caller passes in NULL. */
1460 /* NOTE: cagney/2002-11-09: Not sure how this would happen. The
1461 caller should have previously obtained a valid frame using
1462 get_selected_frame() and then called this code - only possibility
1463 I can think of is code behaving badly.
1465 NOTE: cagney/2003-01-10: Talk about code behaving badly. Check
1466 block_innermost_frame(). It does the sequence: frame = NULL;
1467 while (1) { frame = get_prev_frame (frame); .... }. Ulgh! Why
1468 it couldn't be written better, I don't know.
1470 NOTE: cagney/2003-01-11: I suspect what is happening in
1471 block_innermost_frame() is, when the target has no state
1472 (registers, memory, ...), it is still calling this function. The
1473 assumption being that this function will return NULL indicating
1474 that a frame isn't possible, rather than checking that the target
1475 has state and then calling get_current_frame() and
1476 get_prev_frame(). This is a guess mind. */
1477 if (this_frame
== NULL
)
1479 /* NOTE: cagney/2002-11-09: There was a code segment here that
1480 would error out when CURRENT_FRAME was NULL. The comment
1481 that went with it made the claim ...
1483 ``This screws value_of_variable, which just wants a nice
1484 clean NULL return from block_innermost_frame if there are no
1485 frames. I don't think I've ever seen this message happen
1486 otherwise. And returning NULL here is a perfectly legitimate
1489 Per the above, this code shouldn't even be called with a NULL
1491 frame_debug_got_null_frame (this_frame
, "this_frame NULL");
1492 return current_frame
;
1495 /* There is always a frame. If this assertion fails, suspect that
1496 something should be calling get_selected_frame() or
1497 get_current_frame(). */
1498 gdb_assert (this_frame
!= NULL
);
1500 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1501 sense to stop unwinding at a dummy frame. One place where a dummy
1502 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1503 pcsqh register (space register for the instruction at the head of the
1504 instruction queue) cannot be written directly; the only way to set it
1505 is to branch to code that is in the target space. In order to implement
1506 frame dummies on HPUX, the called function is made to jump back to where
1507 the inferior was when the user function was called. If gdb was inside
1508 the main function when we created the dummy frame, the dummy frame will
1509 point inside the main function. */
1510 if (this_frame
->level
>= 0
1511 && get_frame_type (this_frame
) != DUMMY_FRAME
1512 && !backtrace_past_main
1513 && inside_main_func (this_frame
))
1514 /* Don't unwind past main(). Note, this is done _before_ the
1515 frame has been marked as previously unwound. That way if the
1516 user later decides to enable unwinds past main(), that will
1517 automatically happen. */
1519 frame_debug_got_null_frame (this_frame
, "inside main func");
1523 /* If the user's backtrace limit has been exceeded, stop. We must
1524 add two to the current level; one of those accounts for backtrace_limit
1525 being 1-based and the level being 0-based, and the other accounts for
1526 the level of the new frame instead of the level of the current
1528 if (this_frame
->level
+ 2 > backtrace_limit
)
1530 frame_debug_got_null_frame (this_frame
, "backtrace limit exceeded");
1534 /* If we're already inside the entry function for the main objfile,
1535 then it isn't valid. Don't apply this test to a dummy frame -
1536 dummy frame PCs typically land in the entry func. Don't apply
1537 this test to the sentinel frame. Sentinel frames should always
1538 be allowed to unwind. */
1539 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1540 wasn't checking for "main" in the minimal symbols. With that
1541 fixed asm-source tests now stop in "main" instead of halting the
1542 backtrace in weird and wonderful ways somewhere inside the entry
1543 file. Suspect that tests for inside the entry file/func were
1544 added to work around that (now fixed) case. */
1545 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1546 suggested having the inside_entry_func test use the
1547 inside_main_func() msymbol trick (along with entry_point_address()
1548 I guess) to determine the address range of the start function.
1549 That should provide a far better stopper than the current
1551 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1552 applied tail-call optimizations to main so that a function called
1553 from main returns directly to the caller of main. Since we don't
1554 stop at main, we should at least stop at the entry point of the
1556 if (!backtrace_past_entry
1557 && get_frame_type (this_frame
) != DUMMY_FRAME
&& this_frame
->level
>= 0
1558 && inside_entry_func (this_frame
))
1560 frame_debug_got_null_frame (this_frame
, "inside entry func");
1564 /* Assume that the only way to get a zero PC is through something
1565 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1566 will never unwind a zero PC. */
1567 if (this_frame
->level
> 0
1568 && get_frame_type (this_frame
) == NORMAL_FRAME
1569 && get_frame_type (get_next_frame (this_frame
)) == NORMAL_FRAME
1570 && get_frame_pc (this_frame
) == 0)
1572 frame_debug_got_null_frame (this_frame
, "zero PC");
1576 return get_prev_frame_1 (this_frame
);
1580 get_frame_pc (struct frame_info
*frame
)
1582 gdb_assert (frame
->next
!= NULL
);
1583 return frame_pc_unwind (frame
->next
);
1586 /* Return an address that falls within THIS_FRAME's code block. */
1589 get_frame_address_in_block (struct frame_info
*this_frame
)
1591 /* A draft address. */
1592 CORE_ADDR pc
= get_frame_pc (this_frame
);
1594 struct frame_info
*next_frame
= this_frame
->next
;
1596 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1597 Normally the resume address is inside the body of the function
1598 associated with THIS_FRAME, but there is a special case: when
1599 calling a function which the compiler knows will never return
1600 (for instance abort), the call may be the very last instruction
1601 in the calling function. The resume address will point after the
1602 call and may be at the beginning of a different function
1605 If THIS_FRAME is a signal frame or dummy frame, then we should
1606 not adjust the unwound PC. For a dummy frame, GDB pushed the
1607 resume address manually onto the stack. For a signal frame, the
1608 OS may have pushed the resume address manually and invoked the
1609 handler (e.g. GNU/Linux), or invoked the trampoline which called
1610 the signal handler - but in either case the signal handler is
1611 expected to return to the trampoline. So in both of these
1612 cases we know that the resume address is executable and
1613 related. So we only need to adjust the PC if THIS_FRAME
1614 is a normal function.
1616 If the program has been interrupted while THIS_FRAME is current,
1617 then clearly the resume address is inside the associated
1618 function. There are three kinds of interruption: debugger stop
1619 (next frame will be SENTINEL_FRAME), operating system
1620 signal or exception (next frame will be SIGTRAMP_FRAME),
1621 or debugger-induced function call (next frame will be
1622 DUMMY_FRAME). So we only need to adjust the PC if
1623 NEXT_FRAME is a normal function.
1625 We check the type of NEXT_FRAME first, since it is already
1626 known; frame type is determined by the unwinder, and since
1627 we have THIS_FRAME we've already selected an unwinder for
1629 if (get_frame_type (next_frame
) == NORMAL_FRAME
1630 && get_frame_type (this_frame
) == NORMAL_FRAME
)
1637 pc_notcurrent (struct frame_info
*frame
)
1639 /* If FRAME is not the innermost frame, that normally means that
1640 FRAME->pc points at the return instruction (which is *after* the
1641 call instruction), and we want to get the line containing the
1642 call (because the call is where the user thinks the program is).
1643 However, if the next frame is either a SIGTRAMP_FRAME or a
1644 DUMMY_FRAME, then the next frame will contain a saved interrupt
1645 PC and such a PC indicates the current (rather than next)
1646 instruction/line, consequently, for such cases, want to get the
1647 line containing fi->pc. */
1648 struct frame_info
*next
= get_next_frame (frame
);
1649 int notcurrent
= (next
!= NULL
&& get_frame_type (next
) == NORMAL_FRAME
);
1654 find_frame_sal (struct frame_info
*frame
, struct symtab_and_line
*sal
)
1656 (*sal
) = find_pc_line (get_frame_pc (frame
), pc_notcurrent (frame
));
1659 /* Per "frame.h", return the ``address'' of the frame. Code should
1660 really be using get_frame_id(). */
1662 get_frame_base (struct frame_info
*fi
)
1664 return get_frame_id (fi
).stack_addr
;
1667 /* High-level offsets into the frame. Used by the debug info. */
1670 get_frame_base_address (struct frame_info
*fi
)
1672 if (get_frame_type (fi
) != NORMAL_FRAME
)
1674 if (fi
->base
== NULL
)
1675 fi
->base
= frame_base_find_by_frame (fi
);
1676 /* Sneaky: If the low-level unwind and high-level base code share a
1677 common unwinder, let them share the prologue cache. */
1678 if (fi
->base
->unwind
== fi
->unwind
)
1679 return fi
->base
->this_base (fi
, &fi
->prologue_cache
);
1680 return fi
->base
->this_base (fi
, &fi
->base_cache
);
1684 get_frame_locals_address (struct frame_info
*fi
)
1687 if (get_frame_type (fi
) != NORMAL_FRAME
)
1689 /* If there isn't a frame address method, find it. */
1690 if (fi
->base
== NULL
)
1691 fi
->base
= frame_base_find_by_frame (fi
);
1692 /* Sneaky: If the low-level unwind and high-level base code share a
1693 common unwinder, let them share the prologue cache. */
1694 if (fi
->base
->unwind
== fi
->unwind
)
1695 return fi
->base
->this_locals (fi
, &fi
->prologue_cache
);
1696 return fi
->base
->this_locals (fi
, &fi
->base_cache
);
1700 get_frame_args_address (struct frame_info
*fi
)
1703 if (get_frame_type (fi
) != NORMAL_FRAME
)
1705 /* If there isn't a frame address method, find it. */
1706 if (fi
->base
== NULL
)
1707 fi
->base
= frame_base_find_by_frame (fi
);
1708 /* Sneaky: If the low-level unwind and high-level base code share a
1709 common unwinder, let them share the prologue cache. */
1710 if (fi
->base
->unwind
== fi
->unwind
)
1711 return fi
->base
->this_args (fi
, &fi
->prologue_cache
);
1712 return fi
->base
->this_args (fi
, &fi
->base_cache
);
1715 /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1716 or -1 for a NULL frame. */
1719 frame_relative_level (struct frame_info
*fi
)
1728 get_frame_type (struct frame_info
*frame
)
1730 if (frame
->unwind
== NULL
)
1731 /* Initialize the frame's unwinder because that's what
1732 provides the frame's type. */
1733 frame
->unwind
= frame_unwind_find_by_frame (frame
, &frame
->prologue_cache
);
1734 return frame
->unwind
->type
;
1737 /* Memory access methods. */
1740 get_frame_memory (struct frame_info
*this_frame
, CORE_ADDR addr
,
1741 gdb_byte
*buf
, int len
)
1743 read_memory (addr
, buf
, len
);
1747 get_frame_memory_signed (struct frame_info
*this_frame
, CORE_ADDR addr
,
1750 return read_memory_integer (addr
, len
);
1754 get_frame_memory_unsigned (struct frame_info
*this_frame
, CORE_ADDR addr
,
1757 return read_memory_unsigned_integer (addr
, len
);
1761 safe_frame_unwind_memory (struct frame_info
*this_frame
,
1762 CORE_ADDR addr
, gdb_byte
*buf
, int len
)
1764 /* NOTE: target_read_memory returns zero on success! */
1765 return !target_read_memory (addr
, buf
, len
);
1768 /* Architecture method. */
1771 get_frame_arch (struct frame_info
*this_frame
)
1773 /* In the future, this function will return a per-frame
1774 architecture instead of current_gdbarch. Calling the
1775 routine with a NULL value of this_frame is a bug! */
1776 gdb_assert (this_frame
);
1778 return current_gdbarch
;
1781 /* Stack pointer methods. */
1784 get_frame_sp (struct frame_info
*this_frame
)
1786 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1787 /* Normality - an architecture that provides a way of obtaining any
1788 frame inner-most address. */
1789 if (gdbarch_unwind_sp_p (gdbarch
))
1790 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
1791 operate on THIS_FRAME now. */
1792 return gdbarch_unwind_sp (gdbarch
, this_frame
->next
);
1793 /* Now things are really are grim. Hope that the value returned by
1794 the gdbarch_sp_regnum register is meaningful. */
1795 if (gdbarch_sp_regnum (gdbarch
) >= 0)
1796 return get_frame_register_unsigned (this_frame
,
1797 gdbarch_sp_regnum (gdbarch
));
1798 internal_error (__FILE__
, __LINE__
, _("Missing unwind SP method"));
1801 /* Return the reason why we can't unwind past FRAME. */
1803 enum unwind_stop_reason
1804 get_frame_unwind_stop_reason (struct frame_info
*frame
)
1806 /* If we haven't tried to unwind past this point yet, then assume
1807 that unwinding would succeed. */
1808 if (frame
->prev_p
== 0)
1809 return UNWIND_NO_REASON
;
1811 /* Otherwise, we set a reason when we succeeded (or failed) to
1813 return frame
->stop_reason
;
1816 /* Return a string explaining REASON. */
1819 frame_stop_reason_string (enum unwind_stop_reason reason
)
1823 case UNWIND_NULL_ID
:
1824 return _("unwinder did not report frame ID");
1826 case UNWIND_INNER_ID
:
1827 return _("previous frame inner to this frame (corrupt stack?)");
1829 case UNWIND_SAME_ID
:
1830 return _("previous frame identical to this frame (corrupt stack?)");
1832 case UNWIND_NO_SAVED_PC
:
1833 return _("frame did not save the PC");
1835 case UNWIND_NO_REASON
:
1836 case UNWIND_FIRST_ERROR
:
1838 internal_error (__FILE__
, __LINE__
,
1839 "Invalid frame stop reason");
1843 /* Clean up after a failed (wrong unwinder) attempt to unwind past
1847 frame_cleanup_after_sniffer (void *arg
)
1849 struct frame_info
*frame
= arg
;
1851 /* The sniffer should not allocate a prologue cache if it did not
1852 match this frame. */
1853 gdb_assert (frame
->prologue_cache
== NULL
);
1855 /* No sniffer should extend the frame chain; sniff based on what is
1857 gdb_assert (!frame
->prev_p
);
1859 /* The sniffer should not check the frame's ID; that's circular. */
1860 gdb_assert (!frame
->this_id
.p
);
1862 /* Clear cached fields dependent on the unwinder.
1864 The previous PC is independent of the unwinder, but the previous
1865 function is not (see get_frame_address_in_block). */
1866 frame
->prev_func
.p
= 0;
1867 frame
->prev_func
.addr
= 0;
1869 /* Discard the unwinder last, so that we can easily find it if an assertion
1870 in this function triggers. */
1871 frame
->unwind
= NULL
;
1874 /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
1875 Return a cleanup which should be called if unwinding fails, and
1876 discarded if it succeeds. */
1879 frame_prepare_for_sniffer (struct frame_info
*frame
,
1880 const struct frame_unwind
*unwind
)
1882 gdb_assert (frame
->unwind
== NULL
);
1883 frame
->unwind
= unwind
;
1884 return make_cleanup (frame_cleanup_after_sniffer
, frame
);
1887 extern initialize_file_ftype _initialize_frame
; /* -Wmissing-prototypes */
1889 static struct cmd_list_element
*set_backtrace_cmdlist
;
1890 static struct cmd_list_element
*show_backtrace_cmdlist
;
1893 set_backtrace_cmd (char *args
, int from_tty
)
1895 help_list (set_backtrace_cmdlist
, "set backtrace ", -1, gdb_stdout
);
1899 show_backtrace_cmd (char *args
, int from_tty
)
1901 cmd_show_list (show_backtrace_cmdlist
, from_tty
, "");
1905 _initialize_frame (void)
1907 obstack_init (&frame_cache_obstack
);
1909 observer_attach_target_changed (frame_observer_target_changed
);
1911 add_prefix_cmd ("backtrace", class_maintenance
, set_backtrace_cmd
, _("\
1912 Set backtrace specific variables.\n\
1913 Configure backtrace variables such as the backtrace limit"),
1914 &set_backtrace_cmdlist
, "set backtrace ",
1915 0/*allow-unknown*/, &setlist
);
1916 add_prefix_cmd ("backtrace", class_maintenance
, show_backtrace_cmd
, _("\
1917 Show backtrace specific variables\n\
1918 Show backtrace variables such as the backtrace limit"),
1919 &show_backtrace_cmdlist
, "show backtrace ",
1920 0/*allow-unknown*/, &showlist
);
1922 add_setshow_boolean_cmd ("past-main", class_obscure
,
1923 &backtrace_past_main
, _("\
1924 Set whether backtraces should continue past \"main\"."), _("\
1925 Show whether backtraces should continue past \"main\"."), _("\
1926 Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1927 the backtrace at \"main\". Set this variable if you need to see the rest\n\
1928 of the stack trace."),
1930 show_backtrace_past_main
,
1931 &set_backtrace_cmdlist
,
1932 &show_backtrace_cmdlist
);
1934 add_setshow_boolean_cmd ("past-entry", class_obscure
,
1935 &backtrace_past_entry
, _("\
1936 Set whether backtraces should continue past the entry point of a program."),
1938 Show whether backtraces should continue past the entry point of a program."),
1940 Normally there are no callers beyond the entry point of a program, so GDB\n\
1941 will terminate the backtrace there. Set this variable if you need to see \n\
1942 the rest of the stack trace."),
1944 show_backtrace_past_entry
,
1945 &set_backtrace_cmdlist
,
1946 &show_backtrace_cmdlist
);
1948 add_setshow_integer_cmd ("limit", class_obscure
,
1949 &backtrace_limit
, _("\
1950 Set an upper bound on the number of backtrace levels."), _("\
1951 Show the upper bound on the number of backtrace levels."), _("\
1952 No more than the specified number of frames can be displayed or examined.\n\
1953 Zero is unlimited."),
1955 show_backtrace_limit
,
1956 &set_backtrace_cmdlist
,
1957 &show_backtrace_cmdlist
);
1959 /* Debug this files internals. */
1960 add_setshow_zinteger_cmd ("frame", class_maintenance
, &frame_debug
, _("\
1961 Set frame debugging."), _("\
1962 Show frame debugging."), _("\
1963 When non-zero, frame specific internal debugging is enabled."),
1966 &setdebuglist
, &showdebuglist
);