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