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