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