*** empty log message ***
[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,
4c38e0a4 4 2002, 2003, 2004, 2007, 2008, 2009, 2010 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,
7e2183cc 642 "{ frame_unwind_caller_pc (this_frame=%d) -> %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{
d37346f0
DJ
685 struct address_space *aspace = get_frame_address_space (this_frame);
686 struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame),
687 aspace);
a81dcb05
AC
688 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
689 regcache_save (regcache, do_frame_register_read, this_frame);
690 discard_cleanups (cleanups);
691 return regcache;
692}
693
dbe9fe58 694void
7a25a7c1
AC
695frame_pop (struct frame_info *this_frame)
696{
348473d5
NF
697 struct frame_info *prev_frame;
698 struct regcache *scratch;
699 struct cleanup *cleanups;
700
b89667eb
DE
701 if (get_frame_type (this_frame) == DUMMY_FRAME)
702 {
703 /* Popping a dummy frame involves restoring more than just registers.
704 dummy_frame_pop does all the work. */
705 dummy_frame_pop (get_frame_id (this_frame));
706 return;
707 }
708
348473d5
NF
709 /* Ensure that we have a frame to pop to. */
710 prev_frame = get_prev_frame_1 (this_frame);
711
712 if (!prev_frame)
713 error (_("Cannot pop the initial frame."));
714
c1bf6f65
AC
715 /* Make a copy of all the register values unwound from this frame.
716 Save them in a scratch buffer so that there isn't a race between
594f7785 717 trying to extract the old values from the current regcache while
c1bf6f65 718 at the same time writing new values into that same cache. */
348473d5
NF
719 scratch = frame_save_as_regcache (prev_frame);
720 cleanups = make_cleanup_regcache_xfree (scratch);
c1bf6f65
AC
721
722 /* FIXME: cagney/2003-03-16: It should be possible to tell the
723 target's register cache that it is about to be hit with a burst
724 register transfer and that the sequence of register writes should
725 be batched. The pair target_prepare_to_store() and
726 target_store_registers() kind of suggest this functionality.
727 Unfortunately, they don't implement it. Their lack of a formal
728 definition can lead to targets writing back bogus values
729 (arguably a bug in the target code mind). */
730 /* Now copy those saved registers into the current regcache.
731 Here, regcache_cpy() calls regcache_restore(). */
594f7785 732 regcache_cpy (get_current_regcache (), scratch);
c1bf6f65 733 do_cleanups (cleanups);
7a25a7c1 734
7a25a7c1
AC
735 /* We've made right mess of GDB's local state, just discard
736 everything. */
35f196d9 737 reinit_frame_cache ();
dbe9fe58 738}
c689142b 739
4f460812
AC
740void
741frame_register_unwind (struct frame_info *frame, int regnum,
742 int *optimizedp, enum lval_type *lvalp,
10c42a71 743 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
4f460812 744{
669fac23 745 struct value *value;
7f78e237 746
4f460812
AC
747 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
748 that the value proper does not need to be fetched. */
749 gdb_assert (optimizedp != NULL);
750 gdb_assert (lvalp != NULL);
751 gdb_assert (addrp != NULL);
752 gdb_assert (realnump != NULL);
753 /* gdb_assert (bufferp != NULL); */
754
669fac23 755 value = frame_unwind_register_value (frame, regnum);
4f460812 756
669fac23 757 gdb_assert (value != NULL);
c50901fd 758
669fac23
DJ
759 *optimizedp = value_optimized_out (value);
760 *lvalp = VALUE_LVAL (value);
42ae5230 761 *addrp = value_address (value);
669fac23 762 *realnump = VALUE_REGNUM (value);
6dc42492 763
669fac23
DJ
764 if (bufferp)
765 memcpy (bufferp, value_contents_all (value),
766 TYPE_LENGTH (value_type (value)));
767
768 /* Dispose of the new value. This prevents watchpoints from
769 trying to watch the saved frame pointer. */
770 release_value (value);
771 value_free (value);
4f460812
AC
772}
773
a216a322
AC
774void
775frame_register (struct frame_info *frame, int regnum,
776 int *optimizedp, enum lval_type *lvalp,
10c42a71 777 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
a216a322
AC
778{
779 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
780 that the value proper does not need to be fetched. */
781 gdb_assert (optimizedp != NULL);
782 gdb_assert (lvalp != NULL);
783 gdb_assert (addrp != NULL);
784 gdb_assert (realnump != NULL);
785 /* gdb_assert (bufferp != NULL); */
786
a94dd1fd
AC
787 /* Obtain the register value by unwinding the register from the next
788 (more inner frame). */
789 gdb_assert (frame != NULL && frame->next != NULL);
790 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
791 realnump, bufferp);
a216a322
AC
792}
793
135c175f 794void
10c42a71 795frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
135c175f
AC
796{
797 int optimized;
798 CORE_ADDR addr;
799 int realnum;
800 enum lval_type lval;
135c175f
AC
801 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
802 &realnum, buf);
5b181d62
AC
803}
804
f0e7d0e8
AC
805void
806get_frame_register (struct frame_info *frame,
10c42a71 807 int regnum, gdb_byte *buf)
f0e7d0e8
AC
808{
809 frame_unwind_register (frame->next, regnum, buf);
810}
811
669fac23
DJ
812struct value *
813frame_unwind_register_value (struct frame_info *frame, int regnum)
814{
36f15f55 815 struct gdbarch *gdbarch;
669fac23
DJ
816 struct value *value;
817
818 gdb_assert (frame != NULL);
36f15f55 819 gdbarch = frame_unwind_arch (frame);
669fac23
DJ
820
821 if (frame_debug)
822 {
823 fprintf_unfiltered (gdb_stdlog, "\
824{ frame_unwind_register_value (frame=%d,regnum=%d(%s),...) ",
825 frame->level, regnum,
36f15f55 826 user_reg_map_regnum_to_name (gdbarch, regnum));
669fac23
DJ
827 }
828
829 /* Find the unwinder. */
830 if (frame->unwind == NULL)
831 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
832
833 /* Ask this frame to unwind its register. */
834 value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
835
836 if (frame_debug)
837 {
838 fprintf_unfiltered (gdb_stdlog, "->");
839 if (value_optimized_out (value))
840 fprintf_unfiltered (gdb_stdlog, " optimized out");
841 else
842 {
843 if (VALUE_LVAL (value) == lval_register)
844 fprintf_unfiltered (gdb_stdlog, " register=%d",
845 VALUE_REGNUM (value));
846 else if (VALUE_LVAL (value) == lval_memory)
5af949e3
UW
847 fprintf_unfiltered (gdb_stdlog, " address=%s",
848 paddress (gdbarch,
849 value_address (value)));
669fac23
DJ
850 else
851 fprintf_unfiltered (gdb_stdlog, " computed");
852
853 if (value_lazy (value))
854 fprintf_unfiltered (gdb_stdlog, " lazy");
855 else
856 {
857 int i;
858 const gdb_byte *buf = value_contents (value);
859
860 fprintf_unfiltered (gdb_stdlog, " bytes=");
861 fprintf_unfiltered (gdb_stdlog, "[");
36f15f55 862 for (i = 0; i < register_size (gdbarch, regnum); i++)
669fac23
DJ
863 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
864 fprintf_unfiltered (gdb_stdlog, "]");
865 }
866 }
867
868 fprintf_unfiltered (gdb_stdlog, " }\n");
869 }
870
871 return value;
872}
873
874struct value *
875get_frame_register_value (struct frame_info *frame, int regnum)
876{
877 return frame_unwind_register_value (frame->next, regnum);
878}
879
f0e7d0e8
AC
880LONGEST
881frame_unwind_register_signed (struct frame_info *frame, int regnum)
882{
e17a4113
UW
883 struct gdbarch *gdbarch = frame_unwind_arch (frame);
884 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
885 int size = register_size (gdbarch, regnum);
10c42a71 886 gdb_byte buf[MAX_REGISTER_SIZE];
f0e7d0e8 887 frame_unwind_register (frame, regnum, buf);
e17a4113 888 return extract_signed_integer (buf, size, byte_order);
f0e7d0e8
AC
889}
890
891LONGEST
892get_frame_register_signed (struct frame_info *frame, int regnum)
893{
894 return frame_unwind_register_signed (frame->next, regnum);
895}
896
897ULONGEST
898frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
899{
e17a4113
UW
900 struct gdbarch *gdbarch = frame_unwind_arch (frame);
901 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
902 int size = register_size (gdbarch, regnum);
10c42a71 903 gdb_byte buf[MAX_REGISTER_SIZE];
f0e7d0e8 904 frame_unwind_register (frame, regnum, buf);
e17a4113 905 return extract_unsigned_integer (buf, size, byte_order);
f0e7d0e8
AC
906}
907
908ULONGEST
909get_frame_register_unsigned (struct frame_info *frame, int regnum)
910{
911 return frame_unwind_register_unsigned (frame->next, regnum);
912}
913
ff2e87ac 914void
10c42a71
AC
915put_frame_register (struct frame_info *frame, int regnum,
916 const gdb_byte *buf)
ff2e87ac
AC
917{
918 struct gdbarch *gdbarch = get_frame_arch (frame);
919 int realnum;
920 int optim;
921 enum lval_type lval;
922 CORE_ADDR addr;
923 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
924 if (optim)
8a3fe4f8 925 error (_("Attempt to assign to a value that was optimized out."));
ff2e87ac
AC
926 switch (lval)
927 {
928 case lval_memory:
929 {
930 /* FIXME: write_memory doesn't yet take constant buffers.
931 Arrrg! */
10c42a71 932 gdb_byte tmp[MAX_REGISTER_SIZE];
ff2e87ac
AC
933 memcpy (tmp, buf, register_size (gdbarch, regnum));
934 write_memory (addr, tmp, register_size (gdbarch, regnum));
935 break;
936 }
937 case lval_register:
594f7785 938 regcache_cooked_write (get_current_regcache (), realnum, buf);
ff2e87ac
AC
939 break;
940 default:
8a3fe4f8 941 error (_("Attempt to assign to an unmodifiable value."));
ff2e87ac
AC
942 }
943}
944
cda5a58a 945/* frame_register_read ()
d65fe839 946
cda5a58a 947 Find and return the value of REGNUM for the specified stack frame.
5bc602c7 948 The number of bytes copied is REGISTER_SIZE (REGNUM).
d65fe839 949
cda5a58a 950 Returns 0 if the register value could not be found. */
d65fe839 951
cda5a58a 952int
10c42a71
AC
953frame_register_read (struct frame_info *frame, int regnum,
954 gdb_byte *myaddr)
d65fe839 955{
a216a322
AC
956 int optimized;
957 enum lval_type lval;
958 CORE_ADDR addr;
959 int realnum;
960 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
d65fe839 961
a216a322 962 return !optimized;
d65fe839 963}
e36180d7 964
00fa51f6
UW
965int
966get_frame_register_bytes (struct frame_info *frame, int regnum,
967 CORE_ADDR offset, int len, gdb_byte *myaddr)
968{
969 struct gdbarch *gdbarch = get_frame_arch (frame);
3f27f2a4
AS
970 int i;
971 int maxsize;
68e007ca 972 int numregs;
00fa51f6
UW
973
974 /* Skip registers wholly inside of OFFSET. */
975 while (offset >= register_size (gdbarch, regnum))
976 {
977 offset -= register_size (gdbarch, regnum);
978 regnum++;
979 }
980
26fae1d6
AS
981 /* Ensure that we will not read beyond the end of the register file.
982 This can only ever happen if the debug information is bad. */
3f27f2a4 983 maxsize = -offset;
68e007ca
AS
984 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
985 for (i = regnum; i < numregs; i++)
3f27f2a4
AS
986 {
987 int thissize = register_size (gdbarch, i);
988 if (thissize == 0)
26fae1d6 989 break; /* This register is not available on this architecture. */
3f27f2a4
AS
990 maxsize += thissize;
991 }
992 if (len > maxsize)
993 {
994 warning (_("Bad debug information detected: "
995 "Attempt to read %d bytes from registers."), len);
996 return 0;
997 }
998
00fa51f6
UW
999 /* Copy the data. */
1000 while (len > 0)
1001 {
1002 int curr_len = register_size (gdbarch, regnum) - offset;
1003 if (curr_len > len)
1004 curr_len = len;
1005
1006 if (curr_len == register_size (gdbarch, regnum))
1007 {
1008 if (!frame_register_read (frame, regnum, myaddr))
1009 return 0;
1010 }
1011 else
1012 {
1013 gdb_byte buf[MAX_REGISTER_SIZE];
1014 if (!frame_register_read (frame, regnum, buf))
1015 return 0;
1016 memcpy (myaddr, buf + offset, curr_len);
1017 }
1018
765f065a 1019 myaddr += curr_len;
00fa51f6
UW
1020 len -= curr_len;
1021 offset = 0;
1022 regnum++;
1023 }
1024
1025 return 1;
1026}
1027
1028void
1029put_frame_register_bytes (struct frame_info *frame, int regnum,
1030 CORE_ADDR offset, int len, const gdb_byte *myaddr)
1031{
1032 struct gdbarch *gdbarch = get_frame_arch (frame);
1033
1034 /* Skip registers wholly inside of OFFSET. */
1035 while (offset >= register_size (gdbarch, regnum))
1036 {
1037 offset -= register_size (gdbarch, regnum);
1038 regnum++;
1039 }
1040
1041 /* Copy the data. */
1042 while (len > 0)
1043 {
1044 int curr_len = register_size (gdbarch, regnum) - offset;
1045 if (curr_len > len)
1046 curr_len = len;
1047
1048 if (curr_len == register_size (gdbarch, regnum))
1049 {
1050 put_frame_register (frame, regnum, myaddr);
1051 }
1052 else
1053 {
1054 gdb_byte buf[MAX_REGISTER_SIZE];
1055 frame_register_read (frame, regnum, buf);
1056 memcpy (buf + offset, myaddr, curr_len);
1057 put_frame_register (frame, regnum, buf);
1058 }
1059
765f065a 1060 myaddr += curr_len;
00fa51f6
UW
1061 len -= curr_len;
1062 offset = 0;
1063 regnum++;
1064 }
1065}
e36180d7 1066
a94dd1fd
AC
1067/* Create a sentinel frame. */
1068
b9362cc7 1069static struct frame_info *
6c95b8df 1070create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
a94dd1fd
AC
1071{
1072 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
a94dd1fd 1073 frame->level = -1;
6c95b8df
PA
1074 frame->pspace = pspace;
1075 frame->aspace = get_regcache_aspace (regcache);
a94dd1fd
AC
1076 /* Explicitly initialize the sentinel frame's cache. Provide it
1077 with the underlying regcache. In the future additional
1078 information, such as the frame's thread will be added. */
6dc42492 1079 frame->prologue_cache = sentinel_frame_cache (regcache);
a94dd1fd
AC
1080 /* For the moment there is only one sentinel frame implementation. */
1081 frame->unwind = sentinel_frame_unwind;
1082 /* Link this frame back to itself. The frame is self referential
1083 (the unwound PC is the same as the pc), so make it so. */
1084 frame->next = frame;
50bbdbd9
AC
1085 /* Make the sentinel frame's ID valid, but invalid. That way all
1086 comparisons with it should fail. */
d0a55772
AC
1087 frame->this_id.p = 1;
1088 frame->this_id.value = null_frame_id;
7f78e237
AC
1089 if (frame_debug)
1090 {
1091 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
1092 fprint_frame (gdb_stdlog, frame);
1093 fprintf_unfiltered (gdb_stdlog, " }\n");
1094 }
a94dd1fd
AC
1095 return frame;
1096}
1097
4c1e7e9d
AC
1098/* Info about the innermost stack frame (contents of FP register) */
1099
1100static struct frame_info *current_frame;
1101
1102/* Cache for frame addresses already read by gdb. Valid only while
1103 inferior is stopped. Control variables for the frame cache should
1104 be local to this module. */
1105
1106static struct obstack frame_cache_obstack;
1107
1108void *
479ab5a0 1109frame_obstack_zalloc (unsigned long size)
4c1e7e9d 1110{
479ab5a0
AC
1111 void *data = obstack_alloc (&frame_cache_obstack, size);
1112 memset (data, 0, size);
1113 return data;
4c1e7e9d
AC
1114}
1115
a94dd1fd
AC
1116/* Return the innermost (currently executing) stack frame. This is
1117 split into two functions. The function unwind_to_current_frame()
1118 is wrapped in catch exceptions so that, even when the unwind of the
1119 sentinel frame fails, the function still returns a stack frame. */
1120
1121static int
1122unwind_to_current_frame (struct ui_out *ui_out, void *args)
1123{
1124 struct frame_info *frame = get_prev_frame (args);
bbde78fa 1125 /* A sentinel frame can fail to unwind, e.g., because its PC value
a94dd1fd
AC
1126 lands in somewhere like start. */
1127 if (frame == NULL)
1128 return 1;
1129 current_frame = frame;
1130 return 0;
1131}
4c1e7e9d
AC
1132
1133struct frame_info *
1134get_current_frame (void)
1135{
0a1e1ca1
AC
1136 /* First check, and report, the lack of registers. Having GDB
1137 report "No stack!" or "No memory" when the target doesn't even
1138 have registers is very confusing. Besides, "printcmd.exp"
1139 explicitly checks that ``print $pc'' with no registers prints "No
1140 registers". */
a94dd1fd 1141 if (!target_has_registers)
8a3fe4f8 1142 error (_("No registers."));
0a1e1ca1 1143 if (!target_has_stack)
8a3fe4f8 1144 error (_("No stack."));
a94dd1fd 1145 if (!target_has_memory)
8a3fe4f8 1146 error (_("No memory."));
d729566a
PA
1147 if (ptid_equal (inferior_ptid, null_ptid))
1148 error (_("No selected thread."));
1149 if (is_exited (inferior_ptid))
1150 error (_("Invalid selected thread."));
8ea051c5
PA
1151 if (is_executing (inferior_ptid))
1152 error (_("Target is executing."));
1153
4c1e7e9d
AC
1154 if (current_frame == NULL)
1155 {
a94dd1fd 1156 struct frame_info *sentinel_frame =
6c95b8df 1157 create_sentinel_frame (current_program_space, get_current_regcache ());
a94dd1fd 1158 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
1c3c7ee7 1159 RETURN_MASK_ERROR) != 0)
a94dd1fd
AC
1160 {
1161 /* Oops! Fake a current frame? Is this useful? It has a PC
1162 of zero, for instance. */
1163 current_frame = sentinel_frame;
1164 }
4c1e7e9d
AC
1165 }
1166 return current_frame;
1167}
1168
6e7f8b9c
AC
1169/* The "selected" stack frame is used by default for local and arg
1170 access. May be zero, for no selected frame. */
1171
206415a3 1172static struct frame_info *selected_frame;
6e7f8b9c 1173
9d49bdc2 1174int
8ea051c5
PA
1175has_stack_frames (void)
1176{
1177 if (!target_has_registers || !target_has_stack || !target_has_memory)
1178 return 0;
1179
d729566a
PA
1180 /* No current inferior, no frame. */
1181 if (ptid_equal (inferior_ptid, null_ptid))
1182 return 0;
1183
1184 /* Don't try to read from a dead thread. */
1185 if (is_exited (inferior_ptid))
1186 return 0;
1187
1188 /* ... or from a spinning thread. */
8ea051c5
PA
1189 if (is_executing (inferior_ptid))
1190 return 0;
1191
1192 return 1;
1193}
1194
bbde78fa 1195/* Return the selected frame. Always non-NULL (unless there isn't an
6e7f8b9c
AC
1196 inferior sufficient for creating a frame) in which case an error is
1197 thrown. */
1198
1199struct frame_info *
b04f3ab4 1200get_selected_frame (const char *message)
6e7f8b9c 1201{
206415a3 1202 if (selected_frame == NULL)
b04f3ab4 1203 {
8ea051c5 1204 if (message != NULL && !has_stack_frames ())
8a3fe4f8 1205 error (("%s"), message);
b04f3ab4
AC
1206 /* Hey! Don't trust this. It should really be re-finding the
1207 last selected frame of the currently selected thread. This,
1208 though, is better than nothing. */
1209 select_frame (get_current_frame ());
1210 }
6e7f8b9c 1211 /* There is always a frame. */
206415a3
DJ
1212 gdb_assert (selected_frame != NULL);
1213 return selected_frame;
6e7f8b9c
AC
1214}
1215
bbde78fa 1216/* This is a variant of get_selected_frame() which can be called when
7dd88986 1217 the inferior does not have a frame; in that case it will return
bbde78fa 1218 NULL instead of calling error(). */
7dd88986
DJ
1219
1220struct frame_info *
1221deprecated_safe_get_selected_frame (void)
1222{
8ea051c5 1223 if (!has_stack_frames ())
7dd88986 1224 return NULL;
b04f3ab4 1225 return get_selected_frame (NULL);
7dd88986
DJ
1226}
1227
6e7f8b9c
AC
1228/* Select frame FI (or NULL - to invalidate the current frame). */
1229
1230void
1231select_frame (struct frame_info *fi)
1232{
52f0bd74 1233 struct symtab *s;
6e7f8b9c 1234
206415a3 1235 selected_frame = fi;
bbde78fa 1236 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
6e7f8b9c 1237 frame is being invalidated. */
9a4105ab
AC
1238 if (deprecated_selected_frame_level_changed_hook)
1239 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
6e7f8b9c
AC
1240
1241 /* FIXME: kseitz/2002-08-28: It would be nice to call
bbde78fa 1242 selected_frame_level_changed_event() right here, but due to limitations
6e7f8b9c 1243 in the current interfaces, we would end up flooding UIs with events
bbde78fa 1244 because select_frame() is used extensively internally.
6e7f8b9c
AC
1245
1246 Once we have frame-parameterized frame (and frame-related) commands,
1247 the event notification can be moved here, since this function will only
bbde78fa 1248 be called when the user's selected frame is being changed. */
6e7f8b9c
AC
1249
1250 /* Ensure that symbols for this frame are read in. Also, determine the
1251 source language of this frame, and switch to it if desired. */
1252 if (fi)
1253 {
7ae4c3a5 1254 /* We retrieve the frame's symtab by using the frame PC. However
bbde78fa 1255 we cannot use the frame PC as-is, because it usually points to
7ae4c3a5
JB
1256 the instruction following the "call", which is sometimes the
1257 first instruction of another function. So we rely on
1258 get_frame_address_in_block() which provides us with a PC which
1259 is guaranteed to be inside the frame's code block. */
1260 s = find_pc_symtab (get_frame_address_in_block (fi));
6e7f8b9c
AC
1261 if (s
1262 && s->language != current_language->la_language
1263 && s->language != language_unknown
1264 && language_mode == language_mode_auto)
1265 {
1266 set_language (s->language);
1267 }
1268 }
1269}
c689142b 1270
4c1e7e9d
AC
1271/* Create an arbitrary (i.e. address specified by user) or innermost frame.
1272 Always returns a non-NULL value. */
1273
1274struct frame_info *
1275create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
1276{
1277 struct frame_info *fi;
4c1e7e9d 1278
7f78e237
AC
1279 if (frame_debug)
1280 {
1281 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
1282 "{ create_new_frame (addr=%s, pc=%s) ",
1283 hex_string (addr), hex_string (pc));
7f78e237
AC
1284 }
1285
35d5d4ee 1286 fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
4c1e7e9d 1287
6c95b8df 1288 fi->next = create_sentinel_frame (current_program_space, get_current_regcache ());
7df05f2b 1289
1e275f79
PA
1290 /* Set/update this frame's cached PC value, found in the next frame.
1291 Do this before looking for this frame's unwinder. A sniffer is
1292 very likely to read this, and the corresponding unwinder is
1293 entitled to rely that the PC doesn't magically change. */
1294 fi->next->prev_pc.value = pc;
1295 fi->next->prev_pc.p = 1;
1296
6c95b8df
PA
1297 /* We currently assume that frame chain's can't cross spaces. */
1298 fi->pspace = fi->next->pspace;
1299 fi->aspace = fi->next->aspace;
1300
7df05f2b
AC
1301 /* Select/initialize both the unwind function and the frame's type
1302 based on the PC. */
669fac23 1303 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
7df05f2b 1304
18adea3f 1305 fi->this_id.p = 1;
1e275f79 1306 fi->this_id.value = frame_id_build (addr, pc);
4c1e7e9d 1307
7f78e237
AC
1308 if (frame_debug)
1309 {
1310 fprintf_unfiltered (gdb_stdlog, "-> ");
1311 fprint_frame (gdb_stdlog, fi);
1312 fprintf_unfiltered (gdb_stdlog, " }\n");
1313 }
1314
4c1e7e9d
AC
1315 return fi;
1316}
1317
03febf99
AC
1318/* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
1319 innermost frame). Be careful to not fall off the bottom of the
1320 frame chain and onto the sentinel frame. */
4c1e7e9d
AC
1321
1322struct frame_info *
03febf99 1323get_next_frame (struct frame_info *this_frame)
4c1e7e9d 1324{
03febf99
AC
1325 if (this_frame->level > 0)
1326 return this_frame->next;
a94dd1fd
AC
1327 else
1328 return NULL;
4c1e7e9d
AC
1329}
1330
f4c5303c
OF
1331/* Observer for the target_changed event. */
1332
2c0b251b 1333static void
f4c5303c
OF
1334frame_observer_target_changed (struct target_ops *target)
1335{
35f196d9 1336 reinit_frame_cache ();
f4c5303c
OF
1337}
1338
4c1e7e9d
AC
1339/* Flush the entire frame cache. */
1340
1341void
35f196d9 1342reinit_frame_cache (void)
4c1e7e9d 1343{
272dfcfd
AS
1344 struct frame_info *fi;
1345
1346 /* Tear down all frame caches. */
1347 for (fi = current_frame; fi != NULL; fi = fi->prev)
1348 {
1349 if (fi->prologue_cache && fi->unwind->dealloc_cache)
1350 fi->unwind->dealloc_cache (fi, fi->prologue_cache);
1351 if (fi->base_cache && fi->base->unwind->dealloc_cache)
1352 fi->base->unwind->dealloc_cache (fi, fi->base_cache);
1353 }
1354
4c1e7e9d
AC
1355 /* Since we can't really be sure what the first object allocated was */
1356 obstack_free (&frame_cache_obstack, 0);
1357 obstack_init (&frame_cache_obstack);
1358
0d6ba1b1
DJ
1359 if (current_frame != NULL)
1360 annotate_frames_invalid ();
1361
4c1e7e9d
AC
1362 current_frame = NULL; /* Invalidate cache */
1363 select_frame (NULL);
b83e9eb7 1364 frame_stash_invalidate ();
7f78e237 1365 if (frame_debug)
35f196d9 1366 fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
4c1e7e9d
AC
1367}
1368
e48af409
DJ
1369/* Find where a register is saved (in memory or another register).
1370 The result of frame_register_unwind is just where it is saved
5efde112 1371 relative to this particular frame. */
e48af409
DJ
1372
1373static void
1374frame_register_unwind_location (struct frame_info *this_frame, int regnum,
1375 int *optimizedp, enum lval_type *lvalp,
1376 CORE_ADDR *addrp, int *realnump)
1377{
1378 gdb_assert (this_frame == NULL || this_frame->level >= 0);
1379
1380 while (this_frame != NULL)
1381 {
1382 frame_register_unwind (this_frame, regnum, optimizedp, lvalp,
1383 addrp, realnump, NULL);
1384
1385 if (*optimizedp)
1386 break;
1387
1388 if (*lvalp != lval_register)
1389 break;
1390
1391 regnum = *realnump;
1392 this_frame = get_next_frame (this_frame);
1393 }
1394}
1395
5613d8d3
AC
1396/* Return a "struct frame_info" corresponding to the frame that called
1397 THIS_FRAME. Returns NULL if there is no such frame.
5bf00f29 1398
5613d8d3
AC
1399 Unlike get_prev_frame, this function always tries to unwind the
1400 frame. */
eb4f72c5 1401
5613d8d3
AC
1402static struct frame_info *
1403get_prev_frame_1 (struct frame_info *this_frame)
eb4f72c5 1404{
756e95f1 1405 struct frame_id this_id;
b1bd0044 1406 struct gdbarch *gdbarch;
eb4f72c5 1407
5613d8d3 1408 gdb_assert (this_frame != NULL);
b1bd0044 1409 gdbarch = get_frame_arch (this_frame);
5613d8d3 1410
7f78e237
AC
1411 if (frame_debug)
1412 {
5613d8d3 1413 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
7f78e237
AC
1414 if (this_frame != NULL)
1415 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1416 else
1417 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1418 fprintf_unfiltered (gdb_stdlog, ") ");
1419 }
1420
5613d8d3
AC
1421 /* Only try to do the unwind once. */
1422 if (this_frame->prev_p)
1423 {
1424 if (frame_debug)
1425 {
1426 fprintf_unfiltered (gdb_stdlog, "-> ");
1427 fprint_frame (gdb_stdlog, this_frame->prev);
1428 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1429 }
1430 return this_frame->prev;
1431 }
8fa75a5d 1432
0d254d6f
DJ
1433 /* If the frame unwinder hasn't been selected yet, we must do so
1434 before setting prev_p; otherwise the check for misbehaved
1435 sniffers will think that this frame's sniffer tried to unwind
1436 further (see frame_cleanup_after_sniffer). */
1437 if (this_frame->unwind == NULL)
1438 this_frame->unwind
1439 = frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
8fa75a5d 1440
5613d8d3 1441 this_frame->prev_p = 1;
55feb689 1442 this_frame->stop_reason = UNWIND_NO_REASON;
5613d8d3 1443
edb3359d
DJ
1444 /* If we are unwinding from an inline frame, all of the below tests
1445 were already performed when we unwound from the next non-inline
1446 frame. We must skip them, since we can not get THIS_FRAME's ID
1447 until we have unwound all the way down to the previous non-inline
1448 frame. */
1449 if (get_frame_type (this_frame) == INLINE_FRAME)
1450 return get_prev_frame_raw (this_frame);
1451
5613d8d3
AC
1452 /* Check that this frame's ID was valid. If it wasn't, don't try to
1453 unwind to the prev frame. Be careful to not apply this test to
1454 the sentinel frame. */
0d254d6f 1455 this_id = get_frame_id (this_frame);
005ca36a 1456 if (this_frame->level >= 0 && frame_id_eq (this_id, outer_frame_id))
5613d8d3
AC
1457 {
1458 if (frame_debug)
1459 {
1460 fprintf_unfiltered (gdb_stdlog, "-> ");
1461 fprint_frame (gdb_stdlog, NULL);
1462 fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1463 }
55feb689 1464 this_frame->stop_reason = UNWIND_NULL_ID;
5613d8d3
AC
1465 return NULL;
1466 }
1467
1468 /* Check that this frame's ID isn't inner to (younger, below, next)
1469 the next frame. This happens when a frame unwind goes backwards.
f06eadd9
JB
1470 This check is valid only if this frame and the next frame are NORMAL.
1471 See the comment at frame_id_inner for details. */
1472 if (get_frame_type (this_frame) == NORMAL_FRAME
1473 && this_frame->next->unwind->type == NORMAL_FRAME
a45ae3ed 1474 && frame_id_inner (get_frame_arch (this_frame->next), this_id,
09a7aba8 1475 get_frame_id (this_frame->next)))
55feb689
DJ
1476 {
1477 if (frame_debug)
1478 {
1479 fprintf_unfiltered (gdb_stdlog, "-> ");
1480 fprint_frame (gdb_stdlog, NULL);
1481 fprintf_unfiltered (gdb_stdlog, " // this frame ID is inner }\n");
1482 }
1483 this_frame->stop_reason = UNWIND_INNER_ID;
1484 return NULL;
1485 }
5613d8d3
AC
1486
1487 /* Check that this and the next frame are not identical. If they
1488 are, there is most likely a stack cycle. As with the inner-than
1489 test above, avoid comparing the inner-most and sentinel frames. */
1490 if (this_frame->level > 0
756e95f1 1491 && frame_id_eq (this_id, get_frame_id (this_frame->next)))
55feb689
DJ
1492 {
1493 if (frame_debug)
1494 {
1495 fprintf_unfiltered (gdb_stdlog, "-> ");
1496 fprint_frame (gdb_stdlog, NULL);
1497 fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
1498 }
1499 this_frame->stop_reason = UNWIND_SAME_ID;
1500 return NULL;
1501 }
5613d8d3 1502
e48af409
DJ
1503 /* Check that this and the next frame do not unwind the PC register
1504 to the same memory location. If they do, then even though they
1505 have different frame IDs, the new frame will be bogus; two
1506 functions can't share a register save slot for the PC. This can
1507 happen when the prologue analyzer finds a stack adjustment, but
d57df5e4
DJ
1508 no PC save.
1509
1510 This check does assume that the "PC register" is roughly a
1511 traditional PC, even if the gdbarch_unwind_pc method adjusts
1512 it (we do not rely on the value, only on the unwound PC being
1513 dependent on this value). A potential improvement would be
1514 to have the frame prev_pc method and the gdbarch unwind_pc
1515 method set the same lval and location information as
1516 frame_register_unwind. */
e48af409 1517 if (this_frame->level > 0
b1bd0044 1518 && gdbarch_pc_regnum (gdbarch) >= 0
e48af409 1519 && get_frame_type (this_frame) == NORMAL_FRAME
edb3359d
DJ
1520 && (get_frame_type (this_frame->next) == NORMAL_FRAME
1521 || get_frame_type (this_frame->next) == INLINE_FRAME))
e48af409 1522 {
32276632 1523 int optimized, realnum, nrealnum;
e48af409
DJ
1524 enum lval_type lval, nlval;
1525 CORE_ADDR addr, naddr;
1526
3e8c568d 1527 frame_register_unwind_location (this_frame,
b1bd0044 1528 gdbarch_pc_regnum (gdbarch),
3e8c568d
UW
1529 &optimized, &lval, &addr, &realnum);
1530 frame_register_unwind_location (get_next_frame (this_frame),
b1bd0044 1531 gdbarch_pc_regnum (gdbarch),
32276632 1532 &optimized, &nlval, &naddr, &nrealnum);
e48af409 1533
32276632
DJ
1534 if ((lval == lval_memory && lval == nlval && addr == naddr)
1535 || (lval == lval_register && lval == nlval && realnum == nrealnum))
e48af409
DJ
1536 {
1537 if (frame_debug)
1538 {
1539 fprintf_unfiltered (gdb_stdlog, "-> ");
1540 fprint_frame (gdb_stdlog, NULL);
1541 fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
1542 }
1543
1544 this_frame->stop_reason = UNWIND_NO_SAVED_PC;
1545 this_frame->prev = NULL;
1546 return NULL;
1547 }
1548 }
1549
edb3359d
DJ
1550 return get_prev_frame_raw (this_frame);
1551}
1552
1553/* Construct a new "struct frame_info" and link it previous to
1554 this_frame. */
1555
1556static struct frame_info *
1557get_prev_frame_raw (struct frame_info *this_frame)
1558{
1559 struct frame_info *prev_frame;
1560
5613d8d3
AC
1561 /* Allocate the new frame but do not wire it in to the frame chain.
1562 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1563 frame->next to pull some fancy tricks (of course such code is, by
1564 definition, recursive). Try to prevent it.
1565
1566 There is no reason to worry about memory leaks, should the
1567 remainder of the function fail. The allocated memory will be
1568 quickly reclaimed when the frame cache is flushed, and the `we've
1569 been here before' check above will stop repeated memory
1570 allocation calls. */
1571 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1572 prev_frame->level = this_frame->level + 1;
1573
6c95b8df
PA
1574 /* For now, assume we don't have frame chains crossing address
1575 spaces. */
1576 prev_frame->pspace = this_frame->pspace;
1577 prev_frame->aspace = this_frame->aspace;
1578
5613d8d3
AC
1579 /* Don't yet compute ->unwind (and hence ->type). It is computed
1580 on-demand in get_frame_type, frame_register_unwind, and
1581 get_frame_id. */
1582
1583 /* Don't yet compute the frame's ID. It is computed on-demand by
1584 get_frame_id(). */
1585
1586 /* The unwound frame ID is validate at the start of this function,
1587 as part of the logic to decide if that frame should be further
1588 unwound, and not here while the prev frame is being created.
1589 Doing this makes it possible for the user to examine a frame that
1590 has an invalid frame ID.
1591
1592 Some very old VAX code noted: [...] For the sake of argument,
1593 suppose that the stack is somewhat trashed (which is one reason
1594 that "info frame" exists). So, return 0 (indicating we don't
1595 know the address of the arglist) if we don't know what frame this
1596 frame calls. */
1597
1598 /* Link it in. */
1599 this_frame->prev = prev_frame;
1600 prev_frame->next = this_frame;
1601
1602 if (frame_debug)
1603 {
1604 fprintf_unfiltered (gdb_stdlog, "-> ");
1605 fprint_frame (gdb_stdlog, prev_frame);
1606 fprintf_unfiltered (gdb_stdlog, " }\n");
1607 }
1608
1609 return prev_frame;
1610}
1611
1612/* Debug routine to print a NULL frame being returned. */
1613
1614static void
d2bf72c0 1615frame_debug_got_null_frame (struct frame_info *this_frame,
5613d8d3
AC
1616 const char *reason)
1617{
1618 if (frame_debug)
1619 {
1620 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1621 if (this_frame != NULL)
1622 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1623 else
1624 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1625 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1626 }
1627}
1628
c8cd9f6c
AC
1629/* Is this (non-sentinel) frame in the "main"() function? */
1630
1631static int
1632inside_main_func (struct frame_info *this_frame)
1633{
1634 struct minimal_symbol *msymbol;
1635 CORE_ADDR maddr;
1636
1637 if (symfile_objfile == 0)
1638 return 0;
1639 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1640 if (msymbol == NULL)
1641 return 0;
1642 /* Make certain that the code, and not descriptor, address is
1643 returned. */
b1bd0044 1644 maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
c8cd9f6c
AC
1645 SYMBOL_VALUE_ADDRESS (msymbol),
1646 &current_target);
1647 return maddr == get_frame_func (this_frame);
1648}
1649
2315ffec
RC
1650/* Test whether THIS_FRAME is inside the process entry point function. */
1651
1652static int
1653inside_entry_func (struct frame_info *this_frame)
1654{
abd0a5fa
JK
1655 CORE_ADDR entry_point;
1656
1657 if (!entry_point_address_query (&entry_point))
1658 return 0;
1659
1660 return get_frame_func (this_frame) == entry_point;
2315ffec
RC
1661}
1662
5613d8d3
AC
1663/* Return a structure containing various interesting information about
1664 the frame that called THIS_FRAME. Returns NULL if there is entier
1665 no such frame or the frame fails any of a set of target-independent
1666 condition that should terminate the frame chain (e.g., as unwinding
1667 past main()).
1668
1669 This function should not contain target-dependent tests, such as
1670 checking whether the program-counter is zero. */
1671
1672struct frame_info *
1673get_prev_frame (struct frame_info *this_frame)
1674{
1675 struct frame_info *prev_frame;
1676
eb4f72c5
AC
1677 /* There is always a frame. If this assertion fails, suspect that
1678 something should be calling get_selected_frame() or
1679 get_current_frame(). */
03febf99 1680 gdb_assert (this_frame != NULL);
eb4f72c5 1681
cc9bed83
RC
1682 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1683 sense to stop unwinding at a dummy frame. One place where a dummy
1684 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1685 pcsqh register (space register for the instruction at the head of the
1686 instruction queue) cannot be written directly; the only way to set it
1687 is to branch to code that is in the target space. In order to implement
1688 frame dummies on HPUX, the called function is made to jump back to where
1689 the inferior was when the user function was called. If gdb was inside
1690 the main function when we created the dummy frame, the dummy frame will
1691 point inside the main function. */
03febf99 1692 if (this_frame->level >= 0
edb3359d 1693 && get_frame_type (this_frame) == NORMAL_FRAME
25d29d70 1694 && !backtrace_past_main
c8cd9f6c
AC
1695 && inside_main_func (this_frame))
1696 /* Don't unwind past main(). Note, this is done _before_ the
1697 frame has been marked as previously unwound. That way if the
1698 user later decides to enable unwinds past main(), that will
1699 automatically happen. */
ac2bd0a9 1700 {
d2bf72c0 1701 frame_debug_got_null_frame (this_frame, "inside main func");
ac2bd0a9
AC
1702 return NULL;
1703 }
eb4f72c5 1704
4a5e53e8
DJ
1705 /* If the user's backtrace limit has been exceeded, stop. We must
1706 add two to the current level; one of those accounts for backtrace_limit
1707 being 1-based and the level being 0-based, and the other accounts for
1708 the level of the new frame instead of the level of the current
1709 frame. */
1710 if (this_frame->level + 2 > backtrace_limit)
25d29d70 1711 {
d2bf72c0 1712 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
4a5e53e8 1713 return NULL;
25d29d70
AC
1714 }
1715
0714963c
AC
1716 /* If we're already inside the entry function for the main objfile,
1717 then it isn't valid. Don't apply this test to a dummy frame -
bbde78fa 1718 dummy frame PCs typically land in the entry func. Don't apply
0714963c
AC
1719 this test to the sentinel frame. Sentinel frames should always
1720 be allowed to unwind. */
2f72f850
AC
1721 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1722 wasn't checking for "main" in the minimal symbols. With that
1723 fixed asm-source tests now stop in "main" instead of halting the
bbde78fa 1724 backtrace in weird and wonderful ways somewhere inside the entry
2f72f850
AC
1725 file. Suspect that tests for inside the entry file/func were
1726 added to work around that (now fixed) case. */
0714963c
AC
1727 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1728 suggested having the inside_entry_func test use the
bbde78fa
JM
1729 inside_main_func() msymbol trick (along with entry_point_address()
1730 I guess) to determine the address range of the start function.
0714963c
AC
1731 That should provide a far better stopper than the current
1732 heuristics. */
2315ffec
RC
1733 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1734 applied tail-call optimizations to main so that a function called
1735 from main returns directly to the caller of main. Since we don't
1736 stop at main, we should at least stop at the entry point of the
1737 application. */
edb3359d
DJ
1738 if (this_frame->level >= 0
1739 && get_frame_type (this_frame) == NORMAL_FRAME
1740 && !backtrace_past_entry
6e4c6c91 1741 && inside_entry_func (this_frame))
0714963c 1742 {
d2bf72c0 1743 frame_debug_got_null_frame (this_frame, "inside entry func");
0714963c
AC
1744 return NULL;
1745 }
1746
39ee2ff0
AC
1747 /* Assume that the only way to get a zero PC is through something
1748 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1749 will never unwind a zero PC. */
1750 if (this_frame->level > 0
edb3359d
DJ
1751 && (get_frame_type (this_frame) == NORMAL_FRAME
1752 || get_frame_type (this_frame) == INLINE_FRAME)
39ee2ff0
AC
1753 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1754 && get_frame_pc (this_frame) == 0)
1755 {
d2bf72c0 1756 frame_debug_got_null_frame (this_frame, "zero PC");
39ee2ff0
AC
1757 return NULL;
1758 }
1759
5613d8d3 1760 return get_prev_frame_1 (this_frame);
eb4f72c5
AC
1761}
1762
4c1e7e9d
AC
1763CORE_ADDR
1764get_frame_pc (struct frame_info *frame)
1765{
d1340264 1766 gdb_assert (frame->next != NULL);
edb3359d 1767 return frame_unwind_pc (frame->next);
4c1e7e9d
AC
1768}
1769
ad1193e7 1770/* Return an address that falls within THIS_FRAME's code block. */
8edd5d01
AC
1771
1772CORE_ADDR
ad1193e7 1773get_frame_address_in_block (struct frame_info *this_frame)
8edd5d01
AC
1774{
1775 /* A draft address. */
ad1193e7 1776 CORE_ADDR pc = get_frame_pc (this_frame);
8edd5d01 1777
ad1193e7
DJ
1778 struct frame_info *next_frame = this_frame->next;
1779
1780 /* Calling get_frame_pc returns the resume address for THIS_FRAME.
1781 Normally the resume address is inside the body of the function
1782 associated with THIS_FRAME, but there is a special case: when
1783 calling a function which the compiler knows will never return
1784 (for instance abort), the call may be the very last instruction
1785 in the calling function. The resume address will point after the
1786 call and may be at the beginning of a different function
1787 entirely.
1788
1789 If THIS_FRAME is a signal frame or dummy frame, then we should
1790 not adjust the unwound PC. For a dummy frame, GDB pushed the
1791 resume address manually onto the stack. For a signal frame, the
1792 OS may have pushed the resume address manually and invoked the
1793 handler (e.g. GNU/Linux), or invoked the trampoline which called
1794 the signal handler - but in either case the signal handler is
1795 expected to return to the trampoline. So in both of these
1796 cases we know that the resume address is executable and
1797 related. So we only need to adjust the PC if THIS_FRAME
1798 is a normal function.
1799
1800 If the program has been interrupted while THIS_FRAME is current,
1801 then clearly the resume address is inside the associated
1802 function. There are three kinds of interruption: debugger stop
1803 (next frame will be SENTINEL_FRAME), operating system
1804 signal or exception (next frame will be SIGTRAMP_FRAME),
1805 or debugger-induced function call (next frame will be
1806 DUMMY_FRAME). So we only need to adjust the PC if
1807 NEXT_FRAME is a normal function.
1808
1809 We check the type of NEXT_FRAME first, since it is already
1810 known; frame type is determined by the unwinder, and since
1811 we have THIS_FRAME we've already selected an unwinder for
edb3359d
DJ
1812 NEXT_FRAME.
1813
1814 If the next frame is inlined, we need to keep going until we find
1815 the real function - for instance, if a signal handler is invoked
1816 while in an inlined function, then the code address of the
1817 "calling" normal function should not be adjusted either. */
1818
1819 while (get_frame_type (next_frame) == INLINE_FRAME)
1820 next_frame = next_frame->next;
1821
ad1193e7 1822 if (get_frame_type (next_frame) == NORMAL_FRAME
edb3359d
DJ
1823 && (get_frame_type (this_frame) == NORMAL_FRAME
1824 || get_frame_type (this_frame) == INLINE_FRAME))
ad1193e7
DJ
1825 return pc - 1;
1826
1827 return pc;
8edd5d01
AC
1828}
1829
edb3359d
DJ
1830void
1831find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1058bca7 1832{
edb3359d
DJ
1833 struct frame_info *next_frame;
1834 int notcurrent;
1835
1836 /* If the next frame represents an inlined function call, this frame's
1837 sal is the "call site" of that inlined function, which can not
1838 be inferred from get_frame_pc. */
1839 next_frame = get_next_frame (frame);
1840 if (frame_inlined_callees (frame) > 0)
1841 {
1842 struct symbol *sym;
1843
1844 if (next_frame)
1845 sym = get_frame_function (next_frame);
1846 else
1847 sym = inline_skipped_symbol (inferior_ptid);
1848
1849 init_sal (sal);
1850 if (SYMBOL_LINE (sym) != 0)
1851 {
1852 sal->symtab = SYMBOL_SYMTAB (sym);
1853 sal->line = SYMBOL_LINE (sym);
1854 }
1855 else
1856 /* If the symbol does not have a location, we don't know where
1857 the call site is. Do not pretend to. This is jarring, but
1858 we can't do much better. */
1859 sal->pc = get_frame_pc (frame);
1860
1861 return;
1862 }
1863
1058bca7
AC
1864 /* If FRAME is not the innermost frame, that normally means that
1865 FRAME->pc points at the return instruction (which is *after* the
1866 call instruction), and we want to get the line containing the
1867 call (because the call is where the user thinks the program is).
1868 However, if the next frame is either a SIGTRAMP_FRAME or a
1869 DUMMY_FRAME, then the next frame will contain a saved interrupt
1870 PC and such a PC indicates the current (rather than next)
1871 instruction/line, consequently, for such cases, want to get the
1872 line containing fi->pc. */
edb3359d
DJ
1873 notcurrent = (get_frame_pc (frame) != get_frame_address_in_block (frame));
1874 (*sal) = find_pc_line (get_frame_pc (frame), notcurrent);
1058bca7
AC
1875}
1876
c193f6ac
AC
1877/* Per "frame.h", return the ``address'' of the frame. Code should
1878 really be using get_frame_id(). */
1879CORE_ADDR
1880get_frame_base (struct frame_info *fi)
1881{
d0a55772 1882 return get_frame_id (fi).stack_addr;
c193f6ac
AC
1883}
1884
da62e633
AC
1885/* High-level offsets into the frame. Used by the debug info. */
1886
1887CORE_ADDR
1888get_frame_base_address (struct frame_info *fi)
1889{
7df05f2b 1890 if (get_frame_type (fi) != NORMAL_FRAME)
da62e633
AC
1891 return 0;
1892 if (fi->base == NULL)
86c31399 1893 fi->base = frame_base_find_by_frame (fi);
da62e633
AC
1894 /* Sneaky: If the low-level unwind and high-level base code share a
1895 common unwinder, let them share the prologue cache. */
1896 if (fi->base->unwind == fi->unwind)
669fac23
DJ
1897 return fi->base->this_base (fi, &fi->prologue_cache);
1898 return fi->base->this_base (fi, &fi->base_cache);
da62e633
AC
1899}
1900
1901CORE_ADDR
1902get_frame_locals_address (struct frame_info *fi)
1903{
1904 void **cache;
7df05f2b 1905 if (get_frame_type (fi) != NORMAL_FRAME)
da62e633
AC
1906 return 0;
1907 /* If there isn't a frame address method, find it. */
1908 if (fi->base == NULL)
86c31399 1909 fi->base = frame_base_find_by_frame (fi);
da62e633
AC
1910 /* Sneaky: If the low-level unwind and high-level base code share a
1911 common unwinder, let them share the prologue cache. */
1912 if (fi->base->unwind == fi->unwind)
669fac23
DJ
1913 return fi->base->this_locals (fi, &fi->prologue_cache);
1914 return fi->base->this_locals (fi, &fi->base_cache);
da62e633
AC
1915}
1916
1917CORE_ADDR
1918get_frame_args_address (struct frame_info *fi)
1919{
1920 void **cache;
7df05f2b 1921 if (get_frame_type (fi) != NORMAL_FRAME)
da62e633
AC
1922 return 0;
1923 /* If there isn't a frame address method, find it. */
1924 if (fi->base == NULL)
86c31399 1925 fi->base = frame_base_find_by_frame (fi);
da62e633
AC
1926 /* Sneaky: If the low-level unwind and high-level base code share a
1927 common unwinder, let them share the prologue cache. */
1928 if (fi->base->unwind == fi->unwind)
669fac23
DJ
1929 return fi->base->this_args (fi, &fi->prologue_cache);
1930 return fi->base->this_args (fi, &fi->base_cache);
da62e633
AC
1931}
1932
e7802207
TT
1933/* Return true if the frame unwinder for frame FI is UNWINDER; false
1934 otherwise. */
1935
1936int
1937frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
1938{
1939 if (fi->unwind == NULL)
1940 fi->unwind = frame_unwind_find_by_frame (fi, &fi->prologue_cache);
1941 return fi->unwind == unwinder;
1942}
1943
85cf597a
AC
1944/* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1945 or -1 for a NULL frame. */
1946
1947int
1948frame_relative_level (struct frame_info *fi)
1949{
1950 if (fi == NULL)
1951 return -1;
1952 else
1953 return fi->level;
1954}
1955
5a203e44
AC
1956enum frame_type
1957get_frame_type (struct frame_info *frame)
1958{
c1bf6f65
AC
1959 if (frame->unwind == NULL)
1960 /* Initialize the frame's unwinder because that's what
1961 provides the frame's type. */
669fac23 1962 frame->unwind = frame_unwind_find_by_frame (frame, &frame->prologue_cache);
c1bf6f65 1963 return frame->unwind->type;
5a203e44
AC
1964}
1965
6c95b8df
PA
1966struct program_space *
1967get_frame_program_space (struct frame_info *frame)
1968{
1969 return frame->pspace;
1970}
1971
1972struct program_space *
1973frame_unwind_program_space (struct frame_info *this_frame)
1974{
1975 gdb_assert (this_frame);
1976
1977 /* This is really a placeholder to keep the API consistent --- we
1978 assume for now that we don't have frame chains crossing
1979 spaces. */
1980 return this_frame->pspace;
1981}
1982
1983struct address_space *
1984get_frame_address_space (struct frame_info *frame)
1985{
1986 return frame->aspace;
1987}
1988
ae1e7417
AC
1989/* Memory access methods. */
1990
1991void
10c42a71
AC
1992get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
1993 gdb_byte *buf, int len)
ae1e7417
AC
1994{
1995 read_memory (addr, buf, len);
1996}
1997
1998LONGEST
1999get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
2000 int len)
2001{
e17a4113
UW
2002 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2003 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2004 return read_memory_integer (addr, len, byte_order);
ae1e7417
AC
2005}
2006
2007ULONGEST
2008get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
2009 int len)
2010{
e17a4113
UW
2011 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2012 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2013 return read_memory_unsigned_integer (addr, len, byte_order);
ae1e7417
AC
2014}
2015
304396fb
AC
2016int
2017safe_frame_unwind_memory (struct frame_info *this_frame,
10c42a71 2018 CORE_ADDR addr, gdb_byte *buf, int len)
304396fb 2019{
8defab1a
DJ
2020 /* NOTE: target_read_memory returns zero on success! */
2021 return !target_read_memory (addr, buf, len);
304396fb
AC
2022}
2023
36f15f55 2024/* Architecture methods. */
ae1e7417
AC
2025
2026struct gdbarch *
2027get_frame_arch (struct frame_info *this_frame)
2028{
36f15f55
UW
2029 return frame_unwind_arch (this_frame->next);
2030}
2031
2032struct gdbarch *
2033frame_unwind_arch (struct frame_info *next_frame)
2034{
2035 if (!next_frame->prev_arch.p)
2036 {
2037 struct gdbarch *arch;
0701b271 2038
36f15f55
UW
2039 if (next_frame->unwind == NULL)
2040 next_frame->unwind
2041 = frame_unwind_find_by_frame (next_frame,
2042 &next_frame->prologue_cache);
2043
2044 if (next_frame->unwind->prev_arch != NULL)
2045 arch = next_frame->unwind->prev_arch (next_frame,
2046 &next_frame->prologue_cache);
2047 else
2048 arch = get_frame_arch (next_frame);
2049
2050 next_frame->prev_arch.arch = arch;
2051 next_frame->prev_arch.p = 1;
2052 if (frame_debug)
2053 fprintf_unfiltered (gdb_stdlog,
2054 "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
2055 next_frame->level,
2056 gdbarch_bfd_arch_info (arch)->printable_name);
2057 }
2058
2059 return next_frame->prev_arch.arch;
2060}
2061
2062struct gdbarch *
2063frame_unwind_caller_arch (struct frame_info *next_frame)
2064{
2065 return frame_unwind_arch (skip_inlined_frames (next_frame));
ae1e7417
AC
2066}
2067
a9e5fdc2
AC
2068/* Stack pointer methods. */
2069
2070CORE_ADDR
2071get_frame_sp (struct frame_info *this_frame)
2072{
d56907c1 2073 struct gdbarch *gdbarch = get_frame_arch (this_frame);
bbde78fa 2074 /* Normality - an architecture that provides a way of obtaining any
a9e5fdc2 2075 frame inner-most address. */
b1bd0044 2076 if (gdbarch_unwind_sp_p (gdbarch))
d56907c1
DJ
2077 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
2078 operate on THIS_FRAME now. */
2079 return gdbarch_unwind_sp (gdbarch, this_frame->next);
a9e5fdc2 2080 /* Now things are really are grim. Hope that the value returned by
3e8c568d 2081 the gdbarch_sp_regnum register is meaningful. */
b1bd0044 2082 if (gdbarch_sp_regnum (gdbarch) >= 0)
d56907c1
DJ
2083 return get_frame_register_unsigned (this_frame,
2084 gdbarch_sp_regnum (gdbarch));
e2e0b3e5 2085 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
a9e5fdc2
AC
2086}
2087
55feb689
DJ
2088/* Return the reason why we can't unwind past FRAME. */
2089
2090enum unwind_stop_reason
2091get_frame_unwind_stop_reason (struct frame_info *frame)
2092{
2093 /* If we haven't tried to unwind past this point yet, then assume
2094 that unwinding would succeed. */
2095 if (frame->prev_p == 0)
2096 return UNWIND_NO_REASON;
2097
2098 /* Otherwise, we set a reason when we succeeded (or failed) to
2099 unwind. */
2100 return frame->stop_reason;
2101}
2102
2103/* Return a string explaining REASON. */
2104
2105const char *
2106frame_stop_reason_string (enum unwind_stop_reason reason)
2107{
2108 switch (reason)
2109 {
2110 case UNWIND_NULL_ID:
2111 return _("unwinder did not report frame ID");
2112
2113 case UNWIND_INNER_ID:
2114 return _("previous frame inner to this frame (corrupt stack?)");
2115
2116 case UNWIND_SAME_ID:
2117 return _("previous frame identical to this frame (corrupt stack?)");
2118
e48af409
DJ
2119 case UNWIND_NO_SAVED_PC:
2120 return _("frame did not save the PC");
2121
55feb689
DJ
2122 case UNWIND_NO_REASON:
2123 case UNWIND_FIRST_ERROR:
2124 default:
2125 internal_error (__FILE__, __LINE__,
2126 "Invalid frame stop reason");
2127 }
2128}
2129
669fac23
DJ
2130/* Clean up after a failed (wrong unwinder) attempt to unwind past
2131 FRAME. */
2132
2133static void
2134frame_cleanup_after_sniffer (void *arg)
2135{
2136 struct frame_info *frame = arg;
2137
2138 /* The sniffer should not allocate a prologue cache if it did not
2139 match this frame. */
2140 gdb_assert (frame->prologue_cache == NULL);
2141
2142 /* No sniffer should extend the frame chain; sniff based on what is
2143 already certain. */
2144 gdb_assert (!frame->prev_p);
2145
2146 /* The sniffer should not check the frame's ID; that's circular. */
2147 gdb_assert (!frame->this_id.p);
2148
2149 /* Clear cached fields dependent on the unwinder.
2150
2151 The previous PC is independent of the unwinder, but the previous
ad1193e7 2152 function is not (see get_frame_address_in_block). */
669fac23
DJ
2153 frame->prev_func.p = 0;
2154 frame->prev_func.addr = 0;
2155
2156 /* Discard the unwinder last, so that we can easily find it if an assertion
2157 in this function triggers. */
2158 frame->unwind = NULL;
2159}
2160
2161/* Set FRAME's unwinder temporarily, so that we can call a sniffer.
2162 Return a cleanup which should be called if unwinding fails, and
2163 discarded if it succeeds. */
2164
2165struct cleanup *
2166frame_prepare_for_sniffer (struct frame_info *frame,
2167 const struct frame_unwind *unwind)
2168{
2169 gdb_assert (frame->unwind == NULL);
2170 frame->unwind = unwind;
2171 return make_cleanup (frame_cleanup_after_sniffer, frame);
2172}
2173
b9362cc7
AC
2174extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
2175
25d29d70
AC
2176static struct cmd_list_element *set_backtrace_cmdlist;
2177static struct cmd_list_element *show_backtrace_cmdlist;
2178
2179static void
2180set_backtrace_cmd (char *args, int from_tty)
2181{
2182 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
2183}
2184
2185static void
2186show_backtrace_cmd (char *args, int from_tty)
2187{
2188 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
2189}
2190
4c1e7e9d
AC
2191void
2192_initialize_frame (void)
2193{
2194 obstack_init (&frame_cache_obstack);
eb4f72c5 2195
f4c5303c
OF
2196 observer_attach_target_changed (frame_observer_target_changed);
2197
1bedd215 2198 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
25d29d70 2199Set backtrace specific variables.\n\
1bedd215 2200Configure backtrace variables such as the backtrace limit"),
25d29d70
AC
2201 &set_backtrace_cmdlist, "set backtrace ",
2202 0/*allow-unknown*/, &setlist);
1bedd215 2203 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
25d29d70 2204Show backtrace specific variables\n\
1bedd215 2205Show backtrace variables such as the backtrace limit"),
25d29d70
AC
2206 &show_backtrace_cmdlist, "show backtrace ",
2207 0/*allow-unknown*/, &showlist);
2208
2209 add_setshow_boolean_cmd ("past-main", class_obscure,
7915a72c
AC
2210 &backtrace_past_main, _("\
2211Set whether backtraces should continue past \"main\"."), _("\
2212Show whether backtraces should continue past \"main\"."), _("\
eb4f72c5
AC
2213Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
2214the backtrace at \"main\". Set this variable if you need to see the rest\n\
7915a72c 2215of the stack trace."),
2c5b56ce 2216 NULL,
920d2a44 2217 show_backtrace_past_main,
2c5b56ce 2218 &set_backtrace_cmdlist,
25d29d70
AC
2219 &show_backtrace_cmdlist);
2220
2315ffec 2221 add_setshow_boolean_cmd ("past-entry", class_obscure,
7915a72c
AC
2222 &backtrace_past_entry, _("\
2223Set whether backtraces should continue past the entry point of a program."),
2224 _("\
2225Show whether backtraces should continue past the entry point of a program."),
2226 _("\
2315ffec
RC
2227Normally there are no callers beyond the entry point of a program, so GDB\n\
2228will terminate the backtrace there. Set this variable if you need to see \n\
7915a72c 2229the rest of the stack trace."),
2c5b56ce 2230 NULL,
920d2a44 2231 show_backtrace_past_entry,
2c5b56ce 2232 &set_backtrace_cmdlist,
2315ffec
RC
2233 &show_backtrace_cmdlist);
2234
4a5e53e8
DJ
2235 add_setshow_integer_cmd ("limit", class_obscure,
2236 &backtrace_limit, _("\
7915a72c
AC
2237Set an upper bound on the number of backtrace levels."), _("\
2238Show the upper bound on the number of backtrace levels."), _("\
fec74868 2239No more than the specified number of frames can be displayed or examined.\n\
7915a72c 2240Zero is unlimited."),
4a5e53e8
DJ
2241 NULL,
2242 show_backtrace_limit,
2243 &set_backtrace_cmdlist,
2244 &show_backtrace_cmdlist);
ac2bd0a9
AC
2245
2246 /* Debug this files internals. */
85c07804
AC
2247 add_setshow_zinteger_cmd ("frame", class_maintenance, &frame_debug, _("\
2248Set frame debugging."), _("\
2249Show frame debugging."), _("\
2250When non-zero, frame specific internal debugging is enabled."),
2251 NULL,
920d2a44 2252 show_frame_debug,
85c07804 2253 &setdebuglist, &showdebuglist);
4c1e7e9d 2254}
This page took 1.166695 seconds and 4 git commands to generate.