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