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