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