gdb/
[deliverable/binutils-gdb.git] / gdb / frame.c
... / ...
CommitLineData
1/* Cache and manage frames for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21#include "defs.h"
22#include "frame.h"
23#include "target.h"
24#include "value.h"
25#include "inferior.h" /* for inferior_ptid */
26#include "regcache.h"
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"
33#include "gdbcore.h"
34#include "annotate.h"
35#include "language.h"
36#include "frame-unwind.h"
37#include "frame-base.h"
38#include "command.h"
39#include "gdbcmd.h"
40#include "observer.h"
41#include "objfiles.h"
42#include "exceptions.h"
43#include "gdbthread.h"
44
45static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
46
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. */
56
57struct frame_info
58{
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
63 most frame. */
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. */
68 int level;
69
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. */
75 void *prologue_cache;
76 const struct frame_unwind *unwind;
77
78 /* Cached copy of the previous frame's resume address. */
79 struct {
80 int p;
81 CORE_ADDR value;
82 } prev_pc;
83
84 /* Cached copy of the previous frame's function address. */
85 struct
86 {
87 CORE_ADDR addr;
88 int p;
89 } prev_func;
90
91 /* This frame's ID. */
92 struct
93 {
94 int p;
95 struct frame_id value;
96 } this_id;
97
98 /* The frame's high-level base methods, and corresponding cache.
99 The high level base methods are selected based on the frame's
100 debug info. */
101 const struct frame_base *base;
102 void *base_cache;
103
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 */
107 int prev_p;
108 struct frame_info *prev; /* up, outer, older */
109
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;
113};
114
115/* Flag to control debugging. */
116
117int frame_debug;
118static void
119show_frame_debug (struct ui_file *file, int from_tty,
120 struct cmd_list_element *c, const char *value)
121{
122 fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
123}
124
125/* Flag to indicate whether backtraces should stop at main et.al. */
126
127static int backtrace_past_main;
128static void
129show_backtrace_past_main (struct ui_file *file, int from_tty,
130 struct cmd_list_element *c, const char *value)
131{
132 fprintf_filtered (file, _("\
133Whether backtraces should continue past \"main\" is %s.\n"),
134 value);
135}
136
137static int backtrace_past_entry;
138static void
139show_backtrace_past_entry (struct ui_file *file, int from_tty,
140 struct cmd_list_element *c, const char *value)
141{
142 fprintf_filtered (file, _("\
143Whether backtraces should continue past the entry point of a program is %s.\n"),
144 value);
145}
146
147static int backtrace_limit = INT_MAX;
148static void
149show_backtrace_limit (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
151{
152 fprintf_filtered (file, _("\
153An upper bound on the number of backtrace levels is %s.\n"),
154 value);
155}
156
157
158static void
159fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
160{
161 if (p)
162 fprintf_unfiltered (file, "%s=0x%s", name, paddr_nz (addr));
163 else
164 fprintf_unfiltered (file, "!%s", name);
165}
166
167void
168fprint_frame_id (struct ui_file *file, struct frame_id id)
169{
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, "}");
177}
178
179static void
180fprint_frame_type (struct ui_file *file, enum frame_type type)
181{
182 switch (type)
183 {
184 case NORMAL_FRAME:
185 fprintf_unfiltered (file, "NORMAL_FRAME");
186 return;
187 case DUMMY_FRAME:
188 fprintf_unfiltered (file, "DUMMY_FRAME");
189 return;
190 case SIGTRAMP_FRAME:
191 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
192 return;
193 default:
194 fprintf_unfiltered (file, "<unknown type>");
195 return;
196 };
197}
198
199static void
200fprint_frame (struct ui_file *file, struct frame_info *fi)
201{
202 if (fi == NULL)
203 {
204 fprintf_unfiltered (file, "<NULL frame>");
205 return;
206 }
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);
213 else
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);
219 else
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));
225 else
226 fprintf_unfiltered (file, "<unknown>");
227 fprintf_unfiltered (file, ",");
228 fprintf_unfiltered (file, "id=");
229 if (fi->this_id.p)
230 fprint_frame_id (file, fi->this_id.value);
231 else
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));
237 else
238 fprintf_unfiltered (file, "<unknown>");
239 fprintf_unfiltered (file, "}");
240}
241
242/* Return a frame uniq ID that can be used to, later, re-find the
243 frame. */
244
245struct frame_id
246get_frame_id (struct frame_info *fi)
247{
248 if (fi == NULL)
249 {
250 return null_frame_id;
251 }
252 if (!fi->this_id.p)
253 {
254 if (frame_debug)
255 fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
256 fi->level);
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);
262 fi->this_id.p = 1;
263 if (frame_debug)
264 {
265 fprintf_unfiltered (gdb_stdlog, "-> ");
266 fprint_frame_id (gdb_stdlog, fi->this_id.value);
267 fprintf_unfiltered (gdb_stdlog, " }\n");
268 }
269 }
270 return fi->this_id.value;
271}
272
273struct frame_id
274frame_unwind_id (struct frame_info *next_frame)
275{
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));
281}
282
283const struct frame_id null_frame_id; /* All zeros. */
284
285struct frame_id
286frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
287 CORE_ADDR special_addr)
288{
289 struct frame_id id = null_frame_id;
290 id.stack_addr = stack_addr;
291 id.stack_addr_p = 1;
292 id.code_addr = code_addr;
293 id.code_addr_p = 1;
294 id.special_addr = special_addr;
295 id.special_addr_p = 1;
296 return id;
297}
298
299struct frame_id
300frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
301{
302 struct frame_id id = null_frame_id;
303 id.stack_addr = stack_addr;
304 id.stack_addr_p = 1;
305 id.code_addr = code_addr;
306 id.code_addr_p = 1;
307 return id;
308}
309
310struct frame_id
311frame_id_build_wild (CORE_ADDR stack_addr)
312{
313 struct frame_id id = null_frame_id;
314 id.stack_addr = stack_addr;
315 id.stack_addr_p = 1;
316 return id;
317}
318
319int
320frame_id_p (struct frame_id l)
321{
322 int p;
323 /* The frame is valid iff it has a valid stack address. */
324 p = l.stack_addr_p;
325 if (frame_debug)
326 {
327 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
328 fprint_frame_id (gdb_stdlog, l);
329 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
330 }
331 return p;
332}
333
334int
335frame_id_eq (struct frame_id l, struct frame_id r)
336{
337 int eq;
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. */
341 eq = 0;
342 else if (l.stack_addr != r.stack_addr)
343 /* If .stack addresses are different, the frames are different. */
344 eq = 0;
345 else if (!l.code_addr_p || !r.code_addr_p)
346 /* An invalid code addr is a wild card, always succeed. */
347 eq = 1;
348 else if (l.code_addr != r.code_addr)
349 /* If .code addresses are different, the frames are different. */
350 eq = 0;
351 else if (!l.special_addr_p || !r.special_addr_p)
352 /* An invalid special addr is a wild card (or unused), always succeed. */
353 eq = 1;
354 else if (l.special_addr == r.special_addr)
355 /* Frames are equal. */
356 eq = 1;
357 else
358 /* No luck. */
359 eq = 0;
360 if (frame_debug)
361 {
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);
367 }
368 return eq;
369}
370
371/* Safety net to check whether frame ID L should be inner to
372 frame ID R, according to their stack addresses.
373
374 This method cannot be used to compare arbitrary frames, as the
375 ranges of valid stack addresses may be discontiguous (e.g. due
376 to sigaltstack).
377
378 However, it can be used as safety net to discover invalid frame
379 IDs in certain circumstances.
380
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.
384
385 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
386 error has occurred.
387
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
391 address in between.
392
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. */
396
397static int
398frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
399{
400 int inner;
401 if (!l.stack_addr_p || !r.stack_addr_p)
402 /* Like NaN, any operation involving an invalid ID always fails. */
403 inner = 0;
404 else
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);
410 if (frame_debug)
411 {
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);
417 }
418 return inner;
419}
420
421struct frame_info *
422frame_find_by_id (struct frame_id id)
423{
424 struct frame_info *frame, *prev_frame;
425
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))
429 return NULL;
430
431 for (frame = get_current_frame (); ; frame = prev_frame)
432 {
433 struct frame_id this = get_frame_id (frame);
434 if (frame_id_eq (id, this))
435 /* An exact match. */
436 return frame;
437
438 prev_frame = get_prev_frame (frame);
439 if (!prev_frame)
440 return NULL;
441
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)))
451 return NULL;
452 }
453 return NULL;
454}
455
456CORE_ADDR
457frame_pc_unwind (struct frame_info *this_frame)
458{
459 if (!this_frame->prev_pc.p)
460 {
461 CORE_ADDR pc;
462 if (gdbarch_unwind_pc_p (get_frame_arch (this_frame)))
463 {
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:
469
470 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
471 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
472
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);
481 }
482 else
483 internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
484 this_frame->prev_pc.value = pc;
485 this_frame->prev_pc.p = 1;
486 if (frame_debug)
487 fprintf_unfiltered (gdb_stdlog,
488 "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
489 this_frame->level,
490 paddr_nz (this_frame->prev_pc.value));
491 }
492 return this_frame->prev_pc.value;
493}
494
495CORE_ADDR
496get_frame_func (struct frame_info *this_frame)
497{
498 struct frame_info *next_frame = this_frame->next;
499
500 if (!next_frame->prev_func.p)
501 {
502 /* Make certain that this, and not the adjacent, function is
503 found. */
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);
507 if (frame_debug)
508 fprintf_unfiltered (gdb_stdlog,
509 "{ get_frame_func (this_frame=%d) -> 0x%s }\n",
510 this_frame->level,
511 paddr_nz (next_frame->prev_func.addr));
512 }
513 return next_frame->prev_func.addr;
514}
515
516static int
517do_frame_register_read (void *src, int regnum, gdb_byte *buf)
518{
519 return frame_register_read (src, regnum, buf);
520}
521
522struct regcache *
523frame_save_as_regcache (struct frame_info *this_frame)
524{
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);
529 return regcache;
530}
531
532void
533frame_pop (struct frame_info *this_frame)
534{
535 struct frame_info *prev_frame;
536 struct regcache *scratch;
537 struct cleanup *cleanups;
538
539 /* Ensure that we have a frame to pop to. */
540 prev_frame = get_prev_frame_1 (this_frame);
541
542 if (!prev_frame)
543 error (_("Cannot pop the initial frame."));
544
545 /* Make a copy of all the register values unwound from this frame.
546 Save them in a scratch buffer so that there isn't a race between
547 trying to extract the old values from the current regcache while
548 at the same time writing new values into that same cache. */
549 scratch = frame_save_as_regcache (prev_frame);
550 cleanups = make_cleanup_regcache_xfree (scratch);
551
552 /* If we are popping a dummy frame, clean up the associated
553 data as well. */
554 if (get_frame_type (this_frame) == DUMMY_FRAME)
555 dummy_frame_pop (get_frame_id (this_frame));
556
557 /* FIXME: cagney/2003-03-16: It should be possible to tell the
558 target's register cache that it is about to be hit with a burst
559 register transfer and that the sequence of register writes should
560 be batched. The pair target_prepare_to_store() and
561 target_store_registers() kind of suggest this functionality.
562 Unfortunately, they don't implement it. Their lack of a formal
563 definition can lead to targets writing back bogus values
564 (arguably a bug in the target code mind). */
565 /* Now copy those saved registers into the current regcache.
566 Here, regcache_cpy() calls regcache_restore(). */
567 regcache_cpy (get_current_regcache (), scratch);
568 do_cleanups (cleanups);
569
570 /* We've made right mess of GDB's local state, just discard
571 everything. */
572 reinit_frame_cache ();
573}
574
575void
576frame_register_unwind (struct frame_info *frame, int regnum,
577 int *optimizedp, enum lval_type *lvalp,
578 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
579{
580 struct value *value;
581
582 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
583 that the value proper does not need to be fetched. */
584 gdb_assert (optimizedp != NULL);
585 gdb_assert (lvalp != NULL);
586 gdb_assert (addrp != NULL);
587 gdb_assert (realnump != NULL);
588 /* gdb_assert (bufferp != NULL); */
589
590 value = frame_unwind_register_value (frame, regnum);
591
592 gdb_assert (value != NULL);
593
594 *optimizedp = value_optimized_out (value);
595 *lvalp = VALUE_LVAL (value);
596 *addrp = VALUE_ADDRESS (value);
597 *realnump = VALUE_REGNUM (value);
598
599 if (bufferp)
600 memcpy (bufferp, value_contents_all (value),
601 TYPE_LENGTH (value_type (value)));
602
603 /* Dispose of the new value. This prevents watchpoints from
604 trying to watch the saved frame pointer. */
605 release_value (value);
606 value_free (value);
607}
608
609void
610frame_register (struct frame_info *frame, int regnum,
611 int *optimizedp, enum lval_type *lvalp,
612 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
613{
614 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
615 that the value proper does not need to be fetched. */
616 gdb_assert (optimizedp != NULL);
617 gdb_assert (lvalp != NULL);
618 gdb_assert (addrp != NULL);
619 gdb_assert (realnump != NULL);
620 /* gdb_assert (bufferp != NULL); */
621
622 /* Obtain the register value by unwinding the register from the next
623 (more inner frame). */
624 gdb_assert (frame != NULL && frame->next != NULL);
625 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
626 realnump, bufferp);
627}
628
629void
630frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
631{
632 int optimized;
633 CORE_ADDR addr;
634 int realnum;
635 enum lval_type lval;
636 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
637 &realnum, buf);
638}
639
640void
641get_frame_register (struct frame_info *frame,
642 int regnum, gdb_byte *buf)
643{
644 frame_unwind_register (frame->next, regnum, buf);
645}
646
647struct value *
648frame_unwind_register_value (struct frame_info *frame, int regnum)
649{
650 struct value *value;
651
652 gdb_assert (frame != NULL);
653
654 if (frame_debug)
655 {
656 fprintf_unfiltered (gdb_stdlog, "\
657{ frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
658 frame->level, regnum,
659 user_reg_map_regnum_to_name
660 (get_frame_arch (frame), regnum));
661 }
662
663 /* Find the unwinder. */
664 if (frame->unwind == NULL)
665 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
666
667 /* Ask this frame to unwind its register. */
668 value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
669
670 if (frame_debug)
671 {
672 fprintf_unfiltered (gdb_stdlog, "->");
673 if (value_optimized_out (value))
674 fprintf_unfiltered (gdb_stdlog, " optimized out");
675 else
676 {
677 if (VALUE_LVAL (value) == lval_register)
678 fprintf_unfiltered (gdb_stdlog, " register=%d",
679 VALUE_REGNUM (value));
680 else if (VALUE_LVAL (value) == lval_memory)
681 fprintf_unfiltered (gdb_stdlog, " address=0x%s",
682 paddr_nz (VALUE_ADDRESS (value)));
683 else
684 fprintf_unfiltered (gdb_stdlog, " computed");
685
686 if (value_lazy (value))
687 fprintf_unfiltered (gdb_stdlog, " lazy");
688 else
689 {
690 int i;
691 const gdb_byte *buf = value_contents (value);
692
693 fprintf_unfiltered (gdb_stdlog, " bytes=");
694 fprintf_unfiltered (gdb_stdlog, "[");
695 for (i = 0; i < register_size (get_frame_arch (frame), regnum); i++)
696 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
697 fprintf_unfiltered (gdb_stdlog, "]");
698 }
699 }
700
701 fprintf_unfiltered (gdb_stdlog, " }\n");
702 }
703
704 return value;
705}
706
707struct value *
708get_frame_register_value (struct frame_info *frame, int regnum)
709{
710 return frame_unwind_register_value (frame->next, regnum);
711}
712
713LONGEST
714frame_unwind_register_signed (struct frame_info *frame, int regnum)
715{
716 gdb_byte buf[MAX_REGISTER_SIZE];
717 frame_unwind_register (frame, regnum, buf);
718 return extract_signed_integer (buf, register_size (get_frame_arch (frame),
719 regnum));
720}
721
722LONGEST
723get_frame_register_signed (struct frame_info *frame, int regnum)
724{
725 return frame_unwind_register_signed (frame->next, regnum);
726}
727
728ULONGEST
729frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
730{
731 gdb_byte buf[MAX_REGISTER_SIZE];
732 frame_unwind_register (frame, regnum, buf);
733 return extract_unsigned_integer (buf, register_size (get_frame_arch (frame),
734 regnum));
735}
736
737ULONGEST
738get_frame_register_unsigned (struct frame_info *frame, int regnum)
739{
740 return frame_unwind_register_unsigned (frame->next, regnum);
741}
742
743void
744put_frame_register (struct frame_info *frame, int regnum,
745 const gdb_byte *buf)
746{
747 struct gdbarch *gdbarch = get_frame_arch (frame);
748 int realnum;
749 int optim;
750 enum lval_type lval;
751 CORE_ADDR addr;
752 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
753 if (optim)
754 error (_("Attempt to assign to a value that was optimized out."));
755 switch (lval)
756 {
757 case lval_memory:
758 {
759 /* FIXME: write_memory doesn't yet take constant buffers.
760 Arrrg! */
761 gdb_byte tmp[MAX_REGISTER_SIZE];
762 memcpy (tmp, buf, register_size (gdbarch, regnum));
763 write_memory (addr, tmp, register_size (gdbarch, regnum));
764 break;
765 }
766 case lval_register:
767 regcache_cooked_write (get_current_regcache (), realnum, buf);
768 break;
769 default:
770 error (_("Attempt to assign to an unmodifiable value."));
771 }
772}
773
774/* frame_register_read ()
775
776 Find and return the value of REGNUM for the specified stack frame.
777 The number of bytes copied is REGISTER_SIZE (REGNUM).
778
779 Returns 0 if the register value could not be found. */
780
781int
782frame_register_read (struct frame_info *frame, int regnum,
783 gdb_byte *myaddr)
784{
785 int optimized;
786 enum lval_type lval;
787 CORE_ADDR addr;
788 int realnum;
789 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
790
791 return !optimized;
792}
793
794int
795get_frame_register_bytes (struct frame_info *frame, int regnum,
796 CORE_ADDR offset, int len, gdb_byte *myaddr)
797{
798 struct gdbarch *gdbarch = get_frame_arch (frame);
799 int i;
800 int maxsize;
801
802 /* Skip registers wholly inside of OFFSET. */
803 while (offset >= register_size (gdbarch, regnum))
804 {
805 offset -= register_size (gdbarch, regnum);
806 regnum++;
807 }
808
809 /* Detect bad debug info. */
810 maxsize = -offset;
811 for (i = regnum; i < gdbarch_num_regs (gdbarch); i++)
812 {
813 int thissize = register_size (gdbarch, i);
814 if (thissize == 0)
815 break;
816 maxsize += thissize;
817 }
818 if (len > maxsize)
819 {
820 warning (_("Bad debug information detected: "
821 "Attempt to read %d bytes from registers."), len);
822 return 0;
823 }
824
825 /* Copy the data. */
826 while (len > 0)
827 {
828 int curr_len = register_size (gdbarch, regnum) - offset;
829 if (curr_len > len)
830 curr_len = len;
831
832 if (curr_len == register_size (gdbarch, regnum))
833 {
834 if (!frame_register_read (frame, regnum, myaddr))
835 return 0;
836 }
837 else
838 {
839 gdb_byte buf[MAX_REGISTER_SIZE];
840 if (!frame_register_read (frame, regnum, buf))
841 return 0;
842 memcpy (myaddr, buf + offset, curr_len);
843 }
844
845 myaddr += curr_len;
846 len -= curr_len;
847 offset = 0;
848 regnum++;
849 }
850
851 return 1;
852}
853
854void
855put_frame_register_bytes (struct frame_info *frame, int regnum,
856 CORE_ADDR offset, int len, const gdb_byte *myaddr)
857{
858 struct gdbarch *gdbarch = get_frame_arch (frame);
859
860 /* Skip registers wholly inside of OFFSET. */
861 while (offset >= register_size (gdbarch, regnum))
862 {
863 offset -= register_size (gdbarch, regnum);
864 regnum++;
865 }
866
867 /* Copy the data. */
868 while (len > 0)
869 {
870 int curr_len = register_size (gdbarch, regnum) - offset;
871 if (curr_len > len)
872 curr_len = len;
873
874 if (curr_len == register_size (gdbarch, regnum))
875 {
876 put_frame_register (frame, regnum, myaddr);
877 }
878 else
879 {
880 gdb_byte buf[MAX_REGISTER_SIZE];
881 frame_register_read (frame, regnum, buf);
882 memcpy (buf + offset, myaddr, curr_len);
883 put_frame_register (frame, regnum, buf);
884 }
885
886 myaddr += curr_len;
887 len -= curr_len;
888 offset = 0;
889 regnum++;
890 }
891}
892
893/* Create a sentinel frame. */
894
895static struct frame_info *
896create_sentinel_frame (struct regcache *regcache)
897{
898 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
899 frame->level = -1;
900 /* Explicitly initialize the sentinel frame's cache. Provide it
901 with the underlying regcache. In the future additional
902 information, such as the frame's thread will be added. */
903 frame->prologue_cache = sentinel_frame_cache (regcache);
904 /* For the moment there is only one sentinel frame implementation. */
905 frame->unwind = sentinel_frame_unwind;
906 /* Link this frame back to itself. The frame is self referential
907 (the unwound PC is the same as the pc), so make it so. */
908 frame->next = frame;
909 /* Make the sentinel frame's ID valid, but invalid. That way all
910 comparisons with it should fail. */
911 frame->this_id.p = 1;
912 frame->this_id.value = null_frame_id;
913 if (frame_debug)
914 {
915 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
916 fprint_frame (gdb_stdlog, frame);
917 fprintf_unfiltered (gdb_stdlog, " }\n");
918 }
919 return frame;
920}
921
922/* Info about the innermost stack frame (contents of FP register) */
923
924static struct frame_info *current_frame;
925
926/* Cache for frame addresses already read by gdb. Valid only while
927 inferior is stopped. Control variables for the frame cache should
928 be local to this module. */
929
930static struct obstack frame_cache_obstack;
931
932void *
933frame_obstack_zalloc (unsigned long size)
934{
935 void *data = obstack_alloc (&frame_cache_obstack, size);
936 memset (data, 0, size);
937 return data;
938}
939
940/* Return the innermost (currently executing) stack frame. This is
941 split into two functions. The function unwind_to_current_frame()
942 is wrapped in catch exceptions so that, even when the unwind of the
943 sentinel frame fails, the function still returns a stack frame. */
944
945static int
946unwind_to_current_frame (struct ui_out *ui_out, void *args)
947{
948 struct frame_info *frame = get_prev_frame (args);
949 /* A sentinel frame can fail to unwind, e.g., because its PC value
950 lands in somewhere like start. */
951 if (frame == NULL)
952 return 1;
953 current_frame = frame;
954 return 0;
955}
956
957struct frame_info *
958get_current_frame (void)
959{
960 /* First check, and report, the lack of registers. Having GDB
961 report "No stack!" or "No memory" when the target doesn't even
962 have registers is very confusing. Besides, "printcmd.exp"
963 explicitly checks that ``print $pc'' with no registers prints "No
964 registers". */
965 if (!target_has_registers)
966 error (_("No registers."));
967 if (!target_has_stack)
968 error (_("No stack."));
969 if (!target_has_memory)
970 error (_("No memory."));
971 if (is_executing (inferior_ptid))
972 error (_("Target is executing."));
973
974 if (current_frame == NULL)
975 {
976 struct frame_info *sentinel_frame =
977 create_sentinel_frame (get_current_regcache ());
978 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
979 RETURN_MASK_ERROR) != 0)
980 {
981 /* Oops! Fake a current frame? Is this useful? It has a PC
982 of zero, for instance. */
983 current_frame = sentinel_frame;
984 }
985 }
986 return current_frame;
987}
988
989/* The "selected" stack frame is used by default for local and arg
990 access. May be zero, for no selected frame. */
991
992static struct frame_info *selected_frame;
993
994static int
995has_stack_frames (void)
996{
997 if (!target_has_registers || !target_has_stack || !target_has_memory)
998 return 0;
999
1000 /* If the current thread is executing, don't try to read from
1001 it. */
1002 if (is_executing (inferior_ptid))
1003 return 0;
1004
1005 return 1;
1006}
1007
1008/* Return the selected frame. Always non-NULL (unless there isn't an
1009 inferior sufficient for creating a frame) in which case an error is
1010 thrown. */
1011
1012struct frame_info *
1013get_selected_frame (const char *message)
1014{
1015 if (selected_frame == NULL)
1016 {
1017 if (message != NULL && !has_stack_frames ())
1018 error (("%s"), message);
1019 /* Hey! Don't trust this. It should really be re-finding the
1020 last selected frame of the currently selected thread. This,
1021 though, is better than nothing. */
1022 select_frame (get_current_frame ());
1023 }
1024 /* There is always a frame. */
1025 gdb_assert (selected_frame != NULL);
1026 return selected_frame;
1027}
1028
1029/* This is a variant of get_selected_frame() which can be called when
1030 the inferior does not have a frame; in that case it will return
1031 NULL instead of calling error(). */
1032
1033struct frame_info *
1034deprecated_safe_get_selected_frame (void)
1035{
1036 if (!has_stack_frames ())
1037 return NULL;
1038 return get_selected_frame (NULL);
1039}
1040
1041/* Select frame FI (or NULL - to invalidate the current frame). */
1042
1043void
1044select_frame (struct frame_info *fi)
1045{
1046 struct symtab *s;
1047
1048 selected_frame = fi;
1049 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
1050 frame is being invalidated. */
1051 if (deprecated_selected_frame_level_changed_hook)
1052 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
1053
1054 /* FIXME: kseitz/2002-08-28: It would be nice to call
1055 selected_frame_level_changed_event() right here, but due to limitations
1056 in the current interfaces, we would end up flooding UIs with events
1057 because select_frame() is used extensively internally.
1058
1059 Once we have frame-parameterized frame (and frame-related) commands,
1060 the event notification can be moved here, since this function will only
1061 be called when the user's selected frame is being changed. */
1062
1063 /* Ensure that symbols for this frame are read in. Also, determine the
1064 source language of this frame, and switch to it if desired. */
1065 if (fi)
1066 {
1067 /* We retrieve the frame's symtab by using the frame PC. However
1068 we cannot use the frame PC as-is, because it usually points to
1069 the instruction following the "call", which is sometimes the
1070 first instruction of another function. So we rely on
1071 get_frame_address_in_block() which provides us with a PC which
1072 is guaranteed to be inside the frame's code block. */
1073 s = find_pc_symtab (get_frame_address_in_block (fi));
1074 if (s
1075 && s->language != current_language->la_language
1076 && s->language != language_unknown
1077 && language_mode == language_mode_auto)
1078 {
1079 set_language (s->language);
1080 }
1081 }
1082}
1083
1084/* Create an arbitrary (i.e. address specified by user) or innermost frame.
1085 Always returns a non-NULL value. */
1086
1087struct frame_info *
1088create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1089{
1090 struct frame_info *fi;
1091
1092 if (frame_debug)
1093 {
1094 fprintf_unfiltered (gdb_stdlog,
1095 "{ create_new_frame (addr=0x%s, pc=0x%s) ",
1096 paddr_nz (addr), paddr_nz (pc));
1097 }
1098
1099 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
1100
1101 fi->next = create_sentinel_frame (get_current_regcache ());
1102
1103 /* Select/initialize both the unwind function and the frame's type
1104 based on the PC. */
1105 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1106
1107 fi->this_id.p = 1;
1108 deprecated_update_frame_base_hack (fi, addr);
1109 deprecated_update_frame_pc_hack (fi, pc);
1110
1111 if (frame_debug)
1112 {
1113 fprintf_unfiltered (gdb_stdlog, "-> ");
1114 fprint_frame (gdb_stdlog, fi);
1115 fprintf_unfiltered (gdb_stdlog, " }\n");
1116 }
1117
1118 return fi;
1119}
1120
1121/* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1122 innermost frame). Be careful to not fall off the bottom of the
1123 frame chain and onto the sentinel frame. */
1124
1125struct frame_info *
1126get_next_frame (struct frame_info *this_frame)
1127{
1128 if (this_frame->level > 0)
1129 return this_frame->next;
1130 else
1131 return NULL;
1132}
1133
1134/* Observer for the target_changed event. */
1135
1136void
1137frame_observer_target_changed (struct target_ops *target)
1138{
1139 reinit_frame_cache ();
1140}
1141
1142/* Flush the entire frame cache. */
1143
1144void
1145reinit_frame_cache (void)
1146{
1147 struct frame_info *fi;
1148
1149 /* Tear down all frame caches. */
1150 for (fi = current_frame; fi != NULL; fi = fi->prev)
1151 {
1152 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1153 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1154 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1155 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1156 }
1157
1158 /* Since we can't really be sure what the first object allocated was */
1159 obstack_free (&frame_cache_obstack, 0);
1160 obstack_init (&frame_cache_obstack);
1161
1162 if (current_frame != NULL)
1163 annotate_frames_invalid ();
1164
1165 current_frame = NULL; /* Invalidate cache */
1166 select_frame (NULL);
1167 if (frame_debug)
1168 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
1169}
1170
1171/* Find where a register is saved (in memory or another register).
1172 The result of frame_register_unwind is just where it is saved
1173 relative to this particular frame. */
1174
1175static void
1176frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1177 int *optimizedp, enum lval_type *lvalp,
1178 CORE_ADDR *addrp, int *realnump)
1179{
1180 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1181
1182 while (this_frame != NULL)
1183 {
1184 frame_register_unwind (this_frame, regnum, optimizedp, lvalp,
1185 addrp, realnump, NULL);
1186
1187 if (*optimizedp)
1188 break;
1189
1190 if (*lvalp != lval_register)
1191 break;
1192
1193 regnum = *realnump;
1194 this_frame = get_next_frame (this_frame);
1195 }
1196}
1197
1198/* Return a "struct frame_info" corresponding to the frame that called
1199 THIS_FRAME. Returns NULL if there is no such frame.
1200
1201 Unlike get_prev_frame, this function always tries to unwind the
1202 frame. */
1203
1204static struct frame_info *
1205get_prev_frame_1 (struct frame_info *this_frame)
1206{
1207 struct frame_info *prev_frame;
1208 struct frame_id this_id;
1209 struct gdbarch *gdbarch;
1210
1211 gdb_assert (this_frame != NULL);
1212 gdbarch = get_frame_arch (this_frame);
1213
1214 if (frame_debug)
1215 {
1216 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
1217 if (this_frame != NULL)
1218 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1219 else
1220 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1221 fprintf_unfiltered (gdb_stdlog, ") ");
1222 }
1223
1224 /* Only try to do the unwind once. */
1225 if (this_frame->prev_p)
1226 {
1227 if (frame_debug)
1228 {
1229 fprintf_unfiltered (gdb_stdlog, "-> ");
1230 fprint_frame (gdb_stdlog, this_frame->prev);
1231 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1232 }
1233 return this_frame->prev;
1234 }
1235
1236 /* If the frame unwinder hasn't been selected yet, we must do so
1237 before setting prev_p; otherwise the check for misbehaved
1238 sniffers will think that this frame's sniffer tried to unwind
1239 further (see frame_cleanup_after_sniffer). */
1240 if (this_frame->unwind == NULL)
1241 this_frame->unwind
1242 = frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
1243
1244 this_frame->prev_p = 1;
1245 this_frame->stop_reason = UNWIND_NO_REASON;
1246
1247 /* Check that this frame's ID was valid. If it wasn't, don't try to
1248 unwind to the prev frame. Be careful to not apply this test to
1249 the sentinel frame. */
1250 this_id = get_frame_id (this_frame);
1251 if (this_frame->level >= 0 && !frame_id_p (this_id))
1252 {
1253 if (frame_debug)
1254 {
1255 fprintf_unfiltered (gdb_stdlog, "-> ");
1256 fprint_frame (gdb_stdlog, NULL);
1257 fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1258 }
1259 this_frame->stop_reason = UNWIND_NULL_ID;
1260 return NULL;
1261 }
1262
1263 /* Check that this frame's ID isn't inner to (younger, below, next)
1264 the next frame. This happens when a frame unwind goes backwards.
1265 This check is valid only if the next frame is NORMAL. See the
1266 comment at frame_id_inner for details. */
1267 if (this_frame->next->unwind->type == NORMAL_FRAME
1268 && frame_id_inner (get_frame_arch (this_frame->next), this_id,
1269 get_frame_id (this_frame->next)))
1270 {
1271 if (frame_debug)
1272 {
1273 fprintf_unfiltered (gdb_stdlog, "-> ");
1274 fprint_frame (gdb_stdlog, NULL);
1275 fprintf_unfiltered (gdb_stdlog, " // this frame ID is inner }\n");
1276 }
1277 this_frame->stop_reason = UNWIND_INNER_ID;
1278 return NULL;
1279 }
1280
1281 /* Check that this and the next frame are not identical. If they
1282 are, there is most likely a stack cycle. As with the inner-than
1283 test above, avoid comparing the inner-most and sentinel frames. */
1284 if (this_frame->level > 0
1285 && frame_id_eq (this_id, get_frame_id (this_frame->next)))
1286 {
1287 if (frame_debug)
1288 {
1289 fprintf_unfiltered (gdb_stdlog, "-> ");
1290 fprint_frame (gdb_stdlog, NULL);
1291 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1292 }
1293 this_frame->stop_reason = UNWIND_SAME_ID;
1294 return NULL;
1295 }
1296
1297 /* Check that this and the next frame do not unwind the PC register
1298 to the same memory location. If they do, then even though they
1299 have different frame IDs, the new frame will be bogus; two
1300 functions can't share a register save slot for the PC. This can
1301 happen when the prologue analyzer finds a stack adjustment, but
1302 no PC save.
1303
1304 This check does assume that the "PC register" is roughly a
1305 traditional PC, even if the gdbarch_unwind_pc method adjusts
1306 it (we do not rely on the value, only on the unwound PC being
1307 dependent on this value). A potential improvement would be
1308 to have the frame prev_pc method and the gdbarch unwind_pc
1309 method set the same lval and location information as
1310 frame_register_unwind. */
1311 if (this_frame->level > 0
1312 && gdbarch_pc_regnum (gdbarch) >= 0
1313 && get_frame_type (this_frame) == NORMAL_FRAME
1314 && get_frame_type (this_frame->next) == NORMAL_FRAME)
1315 {
1316 int optimized, realnum, nrealnum;
1317 enum lval_type lval, nlval;
1318 CORE_ADDR addr, naddr;
1319
1320 frame_register_unwind_location (this_frame,
1321 gdbarch_pc_regnum (gdbarch),
1322 &optimized, &lval, &addr, &realnum);
1323 frame_register_unwind_location (get_next_frame (this_frame),
1324 gdbarch_pc_regnum (gdbarch),
1325 &optimized, &nlval, &naddr, &nrealnum);
1326
1327 if ((lval == lval_memory && lval == nlval && addr == naddr)
1328 || (lval == lval_register && lval == nlval && realnum == nrealnum))
1329 {
1330 if (frame_debug)
1331 {
1332 fprintf_unfiltered (gdb_stdlog, "-> ");
1333 fprint_frame (gdb_stdlog, NULL);
1334 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1335 }
1336
1337 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1338 this_frame->prev = NULL;
1339 return NULL;
1340 }
1341 }
1342
1343 /* Allocate the new frame but do not wire it in to the frame chain.
1344 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1345 frame->next to pull some fancy tricks (of course such code is, by
1346 definition, recursive). Try to prevent it.
1347
1348 There is no reason to worry about memory leaks, should the
1349 remainder of the function fail. The allocated memory will be
1350 quickly reclaimed when the frame cache is flushed, and the `we've
1351 been here before' check above will stop repeated memory
1352 allocation calls. */
1353 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1354 prev_frame->level = this_frame->level + 1;
1355
1356 /* Don't yet compute ->unwind (and hence ->type). It is computed
1357 on-demand in get_frame_type, frame_register_unwind, and
1358 get_frame_id. */
1359
1360 /* Don't yet compute the frame's ID. It is computed on-demand by
1361 get_frame_id(). */
1362
1363 /* The unwound frame ID is validate at the start of this function,
1364 as part of the logic to decide if that frame should be further
1365 unwound, and not here while the prev frame is being created.
1366 Doing this makes it possible for the user to examine a frame that
1367 has an invalid frame ID.
1368
1369 Some very old VAX code noted: [...] For the sake of argument,
1370 suppose that the stack is somewhat trashed (which is one reason
1371 that "info frame" exists). So, return 0 (indicating we don't
1372 know the address of the arglist) if we don't know what frame this
1373 frame calls. */
1374
1375 /* Link it in. */
1376 this_frame->prev = prev_frame;
1377 prev_frame->next = this_frame;
1378
1379 if (frame_debug)
1380 {
1381 fprintf_unfiltered (gdb_stdlog, "-> ");
1382 fprint_frame (gdb_stdlog, prev_frame);
1383 fprintf_unfiltered (gdb_stdlog, " }\n");
1384 }
1385
1386 return prev_frame;
1387}
1388
1389/* Debug routine to print a NULL frame being returned. */
1390
1391static void
1392frame_debug_got_null_frame (struct ui_file *file,
1393 struct frame_info *this_frame,
1394 const char *reason)
1395{
1396 if (frame_debug)
1397 {
1398 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1399 if (this_frame != NULL)
1400 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1401 else
1402 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1403 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1404 }
1405}
1406
1407/* Is this (non-sentinel) frame in the "main"() function? */
1408
1409static int
1410inside_main_func (struct frame_info *this_frame)
1411{
1412 struct minimal_symbol *msymbol;
1413 CORE_ADDR maddr;
1414
1415 if (symfile_objfile == 0)
1416 return 0;
1417 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1418 if (msymbol == NULL)
1419 return 0;
1420 /* Make certain that the code, and not descriptor, address is
1421 returned. */
1422 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
1423 SYMBOL_VALUE_ADDRESS (msymbol),
1424 &current_target);
1425 return maddr == get_frame_func (this_frame);
1426}
1427
1428/* Test whether THIS_FRAME is inside the process entry point function. */
1429
1430static int
1431inside_entry_func (struct frame_info *this_frame)
1432{
1433 return (get_frame_func (this_frame) == entry_point_address ());
1434}
1435
1436/* Return a structure containing various interesting information about
1437 the frame that called THIS_FRAME. Returns NULL if there is entier
1438 no such frame or the frame fails any of a set of target-independent
1439 condition that should terminate the frame chain (e.g., as unwinding
1440 past main()).
1441
1442 This function should not contain target-dependent tests, such as
1443 checking whether the program-counter is zero. */
1444
1445struct frame_info *
1446get_prev_frame (struct frame_info *this_frame)
1447{
1448 struct frame_info *prev_frame;
1449
1450 /* Return the inner-most frame, when the caller passes in NULL. */
1451 /* NOTE: cagney/2002-11-09: Not sure how this would happen. The
1452 caller should have previously obtained a valid frame using
1453 get_selected_frame() and then called this code - only possibility
1454 I can think of is code behaving badly.
1455
1456 NOTE: cagney/2003-01-10: Talk about code behaving badly. Check
1457 block_innermost_frame(). It does the sequence: frame = NULL;
1458 while (1) { frame = get_prev_frame (frame); .... }. Ulgh! Why
1459 it couldn't be written better, I don't know.
1460
1461 NOTE: cagney/2003-01-11: I suspect what is happening in
1462 block_innermost_frame() is, when the target has no state
1463 (registers, memory, ...), it is still calling this function. The
1464 assumption being that this function will return NULL indicating
1465 that a frame isn't possible, rather than checking that the target
1466 has state and then calling get_current_frame() and
1467 get_prev_frame(). This is a guess mind. */
1468 if (this_frame == NULL)
1469 {
1470 /* NOTE: cagney/2002-11-09: There was a code segment here that
1471 would error out when CURRENT_FRAME was NULL. The comment
1472 that went with it made the claim ...
1473
1474 ``This screws value_of_variable, which just wants a nice
1475 clean NULL return from block_innermost_frame if there are no
1476 frames. I don't think I've ever seen this message happen
1477 otherwise. And returning NULL here is a perfectly legitimate
1478 thing to do.''
1479
1480 Per the above, this code shouldn't even be called with a NULL
1481 THIS_FRAME. */
1482 frame_debug_got_null_frame (gdb_stdlog, this_frame, "this_frame NULL");
1483 return current_frame;
1484 }
1485
1486 /* There is always a frame. If this assertion fails, suspect that
1487 something should be calling get_selected_frame() or
1488 get_current_frame(). */
1489 gdb_assert (this_frame != NULL);
1490
1491 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1492 sense to stop unwinding at a dummy frame. One place where a dummy
1493 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1494 pcsqh register (space register for the instruction at the head of the
1495 instruction queue) cannot be written directly; the only way to set it
1496 is to branch to code that is in the target space. In order to implement
1497 frame dummies on HPUX, the called function is made to jump back to where
1498 the inferior was when the user function was called. If gdb was inside
1499 the main function when we created the dummy frame, the dummy frame will
1500 point inside the main function. */
1501 if (this_frame->level >= 0
1502 && get_frame_type (this_frame) != DUMMY_FRAME
1503 && !backtrace_past_main
1504 && inside_main_func (this_frame))
1505 /* Don't unwind past main(). Note, this is done _before_ the
1506 frame has been marked as previously unwound. That way if the
1507 user later decides to enable unwinds past main(), that will
1508 automatically happen. */
1509 {
1510 frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside main func");
1511 return NULL;
1512 }
1513
1514 /* If the user's backtrace limit has been exceeded, stop. We must
1515 add two to the current level; one of those accounts for backtrace_limit
1516 being 1-based and the level being 0-based, and the other accounts for
1517 the level of the new frame instead of the level of the current
1518 frame. */
1519 if (this_frame->level + 2 > backtrace_limit)
1520 {
1521 frame_debug_got_null_frame (gdb_stdlog, this_frame,
1522 "backtrace limit exceeded");
1523 return NULL;
1524 }
1525
1526 /* If we're already inside the entry function for the main objfile,
1527 then it isn't valid. Don't apply this test to a dummy frame -
1528 dummy frame PCs typically land in the entry func. Don't apply
1529 this test to the sentinel frame. Sentinel frames should always
1530 be allowed to unwind. */
1531 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1532 wasn't checking for "main" in the minimal symbols. With that
1533 fixed asm-source tests now stop in "main" instead of halting the
1534 backtrace in weird and wonderful ways somewhere inside the entry
1535 file. Suspect that tests for inside the entry file/func were
1536 added to work around that (now fixed) case. */
1537 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1538 suggested having the inside_entry_func test use the
1539 inside_main_func() msymbol trick (along with entry_point_address()
1540 I guess) to determine the address range of the start function.
1541 That should provide a far better stopper than the current
1542 heuristics. */
1543 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1544 applied tail-call optimizations to main so that a function called
1545 from main returns directly to the caller of main. Since we don't
1546 stop at main, we should at least stop at the entry point of the
1547 application. */
1548 if (!backtrace_past_entry
1549 && get_frame_type (this_frame) != DUMMY_FRAME && this_frame->level >= 0
1550 && inside_entry_func (this_frame))
1551 {
1552 frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside entry func");
1553 return NULL;
1554 }
1555
1556 /* Assume that the only way to get a zero PC is through something
1557 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1558 will never unwind a zero PC. */
1559 if (this_frame->level > 0
1560 && get_frame_type (this_frame) == NORMAL_FRAME
1561 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1562 && get_frame_pc (this_frame) == 0)
1563 {
1564 frame_debug_got_null_frame (gdb_stdlog, this_frame, "zero PC");
1565 return NULL;
1566 }
1567
1568 return get_prev_frame_1 (this_frame);
1569}
1570
1571CORE_ADDR
1572get_frame_pc (struct frame_info *frame)
1573{
1574 gdb_assert (frame->next != NULL);
1575 return frame_pc_unwind (frame->next);
1576}
1577
1578/* Return an address that falls within THIS_FRAME's code block. */
1579
1580CORE_ADDR
1581get_frame_address_in_block (struct frame_info *this_frame)
1582{
1583 /* A draft address. */
1584 CORE_ADDR pc = get_frame_pc (this_frame);
1585
1586 struct frame_info *next_frame = this_frame->next;
1587
1588 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1589 Normally the resume address is inside the body of the function
1590 associated with THIS_FRAME, but there is a special case: when
1591 calling a function which the compiler knows will never return
1592 (for instance abort), the call may be the very last instruction
1593 in the calling function. The resume address will point after the
1594 call and may be at the beginning of a different function
1595 entirely.
1596
1597 If THIS_FRAME is a signal frame or dummy frame, then we should
1598 not adjust the unwound PC. For a dummy frame, GDB pushed the
1599 resume address manually onto the stack. For a signal frame, the
1600 OS may have pushed the resume address manually and invoked the
1601 handler (e.g. GNU/Linux), or invoked the trampoline which called
1602 the signal handler - but in either case the signal handler is
1603 expected to return to the trampoline. So in both of these
1604 cases we know that the resume address is executable and
1605 related. So we only need to adjust the PC if THIS_FRAME
1606 is a normal function.
1607
1608 If the program has been interrupted while THIS_FRAME is current,
1609 then clearly the resume address is inside the associated
1610 function. There are three kinds of interruption: debugger stop
1611 (next frame will be SENTINEL_FRAME), operating system
1612 signal or exception (next frame will be SIGTRAMP_FRAME),
1613 or debugger-induced function call (next frame will be
1614 DUMMY_FRAME). So we only need to adjust the PC if
1615 NEXT_FRAME is a normal function.
1616
1617 We check the type of NEXT_FRAME first, since it is already
1618 known; frame type is determined by the unwinder, and since
1619 we have THIS_FRAME we've already selected an unwinder for
1620 NEXT_FRAME. */
1621 if (get_frame_type (next_frame) == NORMAL_FRAME
1622 && get_frame_type (this_frame) == NORMAL_FRAME)
1623 return pc - 1;
1624
1625 return pc;
1626}
1627
1628static int
1629pc_notcurrent (struct frame_info *frame)
1630{
1631 /* If FRAME is not the innermost frame, that normally means that
1632 FRAME->pc points at the return instruction (which is *after* the
1633 call instruction), and we want to get the line containing the
1634 call (because the call is where the user thinks the program is).
1635 However, if the next frame is either a SIGTRAMP_FRAME or a
1636 DUMMY_FRAME, then the next frame will contain a saved interrupt
1637 PC and such a PC indicates the current (rather than next)
1638 instruction/line, consequently, for such cases, want to get the
1639 line containing fi->pc. */
1640 struct frame_info *next = get_next_frame (frame);
1641 int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1642 return notcurrent;
1643}
1644
1645void
1646find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1647{
1648 (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
1649}
1650
1651/* Per "frame.h", return the ``address'' of the frame. Code should
1652 really be using get_frame_id(). */
1653CORE_ADDR
1654get_frame_base (struct frame_info *fi)
1655{
1656 return get_frame_id (fi).stack_addr;
1657}
1658
1659/* High-level offsets into the frame. Used by the debug info. */
1660
1661CORE_ADDR
1662get_frame_base_address (struct frame_info *fi)
1663{
1664 if (get_frame_type (fi) != NORMAL_FRAME)
1665 return 0;
1666 if (fi->base == NULL)
1667 fi->base = frame_base_find_by_frame (fi);
1668 /* Sneaky: If the low-level unwind and high-level base code share a
1669 common unwinder, let them share the prologue cache. */
1670 if (fi->base->unwind == fi->unwind)
1671 return fi->base->this_base (fi, &fi->prologue_cache);
1672 return fi->base->this_base (fi, &fi->base_cache);
1673}
1674
1675CORE_ADDR
1676get_frame_locals_address (struct frame_info *fi)
1677{
1678 void **cache;
1679 if (get_frame_type (fi) != NORMAL_FRAME)
1680 return 0;
1681 /* If there isn't a frame address method, find it. */
1682 if (fi->base == NULL)
1683 fi->base = frame_base_find_by_frame (fi);
1684 /* Sneaky: If the low-level unwind and high-level base code share a
1685 common unwinder, let them share the prologue cache. */
1686 if (fi->base->unwind == fi->unwind)
1687 return fi->base->this_locals (fi, &fi->prologue_cache);
1688 return fi->base->this_locals (fi, &fi->base_cache);
1689}
1690
1691CORE_ADDR
1692get_frame_args_address (struct frame_info *fi)
1693{
1694 void **cache;
1695 if (get_frame_type (fi) != NORMAL_FRAME)
1696 return 0;
1697 /* If there isn't a frame address method, find it. */
1698 if (fi->base == NULL)
1699 fi->base = frame_base_find_by_frame (fi);
1700 /* Sneaky: If the low-level unwind and high-level base code share a
1701 common unwinder, let them share the prologue cache. */
1702 if (fi->base->unwind == fi->unwind)
1703 return fi->base->this_args (fi, &fi->prologue_cache);
1704 return fi->base->this_args (fi, &fi->base_cache);
1705}
1706
1707/* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1708 or -1 for a NULL frame. */
1709
1710int
1711frame_relative_level (struct frame_info *fi)
1712{
1713 if (fi == NULL)
1714 return -1;
1715 else
1716 return fi->level;
1717}
1718
1719enum frame_type
1720get_frame_type (struct frame_info *frame)
1721{
1722 if (frame->unwind == NULL)
1723 /* Initialize the frame's unwinder because that's what
1724 provides the frame's type. */
1725 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
1726 return frame->unwind->type;
1727}
1728
1729void
1730deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
1731{
1732 if (frame_debug)
1733 fprintf_unfiltered (gdb_stdlog,
1734 "{ deprecated_update_frame_pc_hack (frame=%d,pc=0x%s) }\n",
1735 frame->level, paddr_nz (pc));
1736 /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are
1737 maintaining a locally allocated frame object. Since such frames
1738 are not in the frame chain, it isn't possible to assume that the
1739 frame has a next. Sigh. */
1740 if (frame->next != NULL)
1741 {
1742 /* While we're at it, update this frame's cached PC value, found
1743 in the next frame. Oh for the day when "struct frame_info"
1744 is opaque and this hack on hack can just go away. */
1745 frame->next->prev_pc.value = pc;
1746 frame->next->prev_pc.p = 1;
1747 }
1748}
1749
1750void
1751deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
1752{
1753 if (frame_debug)
1754 fprintf_unfiltered (gdb_stdlog,
1755 "{ deprecated_update_frame_base_hack (frame=%d,base=0x%s) }\n",
1756 frame->level, paddr_nz (base));
1757 /* See comment in "frame.h". */
1758 frame->this_id.value.stack_addr = base;
1759}
1760
1761/* Memory access methods. */
1762
1763void
1764get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
1765 gdb_byte *buf, int len)
1766{
1767 read_memory (addr, buf, len);
1768}
1769
1770LONGEST
1771get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1772 int len)
1773{
1774 return read_memory_integer (addr, len);
1775}
1776
1777ULONGEST
1778get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1779 int len)
1780{
1781 return read_memory_unsigned_integer (addr, len);
1782}
1783
1784int
1785safe_frame_unwind_memory (struct frame_info *this_frame,
1786 CORE_ADDR addr, gdb_byte *buf, int len)
1787{
1788 /* NOTE: target_read_memory returns zero on success! */
1789 return !target_read_memory (addr, buf, len);
1790}
1791
1792/* Architecture method. */
1793
1794struct gdbarch *
1795get_frame_arch (struct frame_info *this_frame)
1796{
1797 return current_gdbarch;
1798}
1799
1800/* Stack pointer methods. */
1801
1802CORE_ADDR
1803get_frame_sp (struct frame_info *this_frame)
1804{
1805 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1806 /* Normality - an architecture that provides a way of obtaining any
1807 frame inner-most address. */
1808 if (gdbarch_unwind_sp_p (gdbarch))
1809 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
1810 operate on THIS_FRAME now. */
1811 return gdbarch_unwind_sp (gdbarch, this_frame->next);
1812 /* Now things are really are grim. Hope that the value returned by
1813 the gdbarch_sp_regnum register is meaningful. */
1814 if (gdbarch_sp_regnum (gdbarch) >= 0)
1815 return get_frame_register_unsigned (this_frame,
1816 gdbarch_sp_regnum (gdbarch));
1817 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
1818}
1819
1820/* Return the reason why we can't unwind past FRAME. */
1821
1822enum unwind_stop_reason
1823get_frame_unwind_stop_reason (struct frame_info *frame)
1824{
1825 /* If we haven't tried to unwind past this point yet, then assume
1826 that unwinding would succeed. */
1827 if (frame->prev_p == 0)
1828 return UNWIND_NO_REASON;
1829
1830 /* Otherwise, we set a reason when we succeeded (or failed) to
1831 unwind. */
1832 return frame->stop_reason;
1833}
1834
1835/* Return a string explaining REASON. */
1836
1837const char *
1838frame_stop_reason_string (enum unwind_stop_reason reason)
1839{
1840 switch (reason)
1841 {
1842 case UNWIND_NULL_ID:
1843 return _("unwinder did not report frame ID");
1844
1845 case UNWIND_INNER_ID:
1846 return _("previous frame inner to this frame (corrupt stack?)");
1847
1848 case UNWIND_SAME_ID:
1849 return _("previous frame identical to this frame (corrupt stack?)");
1850
1851 case UNWIND_NO_SAVED_PC:
1852 return _("frame did not save the PC");
1853
1854 case UNWIND_NO_REASON:
1855 case UNWIND_FIRST_ERROR:
1856 default:
1857 internal_error (__FILE__, __LINE__,
1858 "Invalid frame stop reason");
1859 }
1860}
1861
1862/* Clean up after a failed (wrong unwinder) attempt to unwind past
1863 FRAME. */
1864
1865static void
1866frame_cleanup_after_sniffer (void *arg)
1867{
1868 struct frame_info *frame = arg;
1869
1870 /* The sniffer should not allocate a prologue cache if it did not
1871 match this frame. */
1872 gdb_assert (frame->prologue_cache == NULL);
1873
1874 /* No sniffer should extend the frame chain; sniff based on what is
1875 already certain. */
1876 gdb_assert (!frame->prev_p);
1877
1878 /* The sniffer should not check the frame's ID; that's circular. */
1879 gdb_assert (!frame->this_id.p);
1880
1881 /* Clear cached fields dependent on the unwinder.
1882
1883 The previous PC is independent of the unwinder, but the previous
1884 function is not (see get_frame_address_in_block). */
1885 frame->prev_func.p = 0;
1886 frame->prev_func.addr = 0;
1887
1888 /* Discard the unwinder last, so that we can easily find it if an assertion
1889 in this function triggers. */
1890 frame->unwind = NULL;
1891}
1892
1893/* Set FRAME's unwinder temporarily, so that we can call a sniffer.
1894 Return a cleanup which should be called if unwinding fails, and
1895 discarded if it succeeds. */
1896
1897struct cleanup *
1898frame_prepare_for_sniffer (struct frame_info *frame,
1899 const struct frame_unwind *unwind)
1900{
1901 gdb_assert (frame->unwind == NULL);
1902 frame->unwind = unwind;
1903 return make_cleanup (frame_cleanup_after_sniffer, frame);
1904}
1905
1906extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
1907
1908static struct cmd_list_element *set_backtrace_cmdlist;
1909static struct cmd_list_element *show_backtrace_cmdlist;
1910
1911static void
1912set_backtrace_cmd (char *args, int from_tty)
1913{
1914 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
1915}
1916
1917static void
1918show_backtrace_cmd (char *args, int from_tty)
1919{
1920 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
1921}
1922
1923void
1924_initialize_frame (void)
1925{
1926 obstack_init (&frame_cache_obstack);
1927
1928 observer_attach_target_changed (frame_observer_target_changed);
1929
1930 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
1931Set backtrace specific variables.\n\
1932Configure backtrace variables such as the backtrace limit"),
1933 &set_backtrace_cmdlist, "set backtrace ",
1934 0/*allow-unknown*/, &setlist);
1935 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
1936Show backtrace specific variables\n\
1937Show backtrace variables such as the backtrace limit"),
1938 &show_backtrace_cmdlist, "show backtrace ",
1939 0/*allow-unknown*/, &showlist);
1940
1941 add_setshow_boolean_cmd ("past-main", class_obscure,
1942 &backtrace_past_main, _("\
1943Set whether backtraces should continue past \"main\"."), _("\
1944Show whether backtraces should continue past \"main\"."), _("\
1945Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1946the backtrace at \"main\". Set this variable if you need to see the rest\n\
1947of the stack trace."),
1948 NULL,
1949 show_backtrace_past_main,
1950 &set_backtrace_cmdlist,
1951 &show_backtrace_cmdlist);
1952
1953 add_setshow_boolean_cmd ("past-entry", class_obscure,
1954 &backtrace_past_entry, _("\
1955Set whether backtraces should continue past the entry point of a program."),
1956 _("\
1957Show whether backtraces should continue past the entry point of a program."),
1958 _("\
1959Normally there are no callers beyond the entry point of a program, so GDB\n\
1960will terminate the backtrace there. Set this variable if you need to see \n\
1961the rest of the stack trace."),
1962 NULL,
1963 show_backtrace_past_entry,
1964 &set_backtrace_cmdlist,
1965 &show_backtrace_cmdlist);
1966
1967 add_setshow_integer_cmd ("limit", class_obscure,
1968 &backtrace_limit, _("\
1969Set an upper bound on the number of backtrace levels."), _("\
1970Show the upper bound on the number of backtrace levels."), _("\
1971No more than the specified number of frames can be displayed or examined.\n\
1972Zero is unlimited."),
1973 NULL,
1974 show_backtrace_limit,
1975 &set_backtrace_cmdlist,
1976 &show_backtrace_cmdlist);
1977
1978 /* Debug this files internals. */
1979 add_setshow_zinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
1980Set frame debugging."), _("\
1981Show frame debugging."), _("\
1982When non-zero, frame specific internal debugging is enabled."),
1983 NULL,
1984 show_frame_debug,
1985 &setdebuglist, &showdebuglist);
1986}
This page took 0.034765 seconds and 4 git commands to generate.