(elf_core_file_p): Move the call to elf_backend_object_p to allow the correct
[deliverable/binutils-gdb.git] / gdb / frame.c
CommitLineData
4f460812 1/* Cache and manage frames for GDB, the GNU debugger.
96cb11df
AC
2
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
5bf00f29 4 2001, 2002, 2003, 2004 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
10 the Free Software Foundation; either version 2 of the License, or
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
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23#include "defs.h"
24#include "frame.h"
25#include "target.h"
26#include "value.h"
39f77062 27#include "inferior.h" /* for inferior_ptid */
4e052eda 28#include "regcache.h"
4f460812 29#include "gdb_assert.h"
e36180d7 30#include "gdb_string.h"
eb8bc282 31#include "user-regs.h"
4c1e7e9d
AC
32#include "gdb_obstack.h"
33#include "dummy-frame.h"
a94dd1fd 34#include "sentinel-frame.h"
4c1e7e9d
AC
35#include "gdbcore.h"
36#include "annotate.h"
6e7f8b9c 37#include "language.h"
494cca16 38#include "frame-unwind.h"
da62e633 39#include "frame-base.h"
eb4f72c5
AC
40#include "command.h"
41#include "gdbcmd.h"
f4c5303c 42#include "observer.h"
c8cd9f6c 43#include "objfiles.h"
60250e8b 44#include "exceptions.h"
eb4f72c5 45
5613d8d3
AC
46static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
47
bd013d54
AC
48/* We keep a cache of stack frames, each of which is a "struct
49 frame_info". The innermost one gets allocated (in
50 wait_for_inferior) each time the inferior stops; current_frame
51 points to it. Additional frames get allocated (in get_prev_frame)
52 as needed, and are chained through the next and prev fields. Any
53 time that the frame cache becomes invalid (most notably when we
54 execute something, but also if we change how we interpret the
55 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
56 which reads new symbols)), we should call reinit_frame_cache. */
57
58struct frame_info
59{
60 /* Level of this frame. The inner-most (youngest) frame is at level
61 0. As you move towards the outer-most (oldest) frame, the level
62 increases. This is a cached value. It could just as easily be
63 computed by counting back from the selected frame to the inner
64 most frame. */
bbde78fa 65 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
bd013d54
AC
66 reserved to indicate a bogus frame - one that has been created
67 just to keep GDB happy (GDB always needs a frame). For the
68 moment leave this as speculation. */
69 int level;
70
bd013d54
AC
71 /* The frame's low-level unwinder and corresponding cache. The
72 low-level unwinder is responsible for unwinding register values
73 for the previous frame. The low-level unwind methods are
bbde78fa 74 selected based on the presence, or otherwise, of register unwind
bd013d54
AC
75 information such as CFI. */
76 void *prologue_cache;
77 const struct frame_unwind *unwind;
78
79 /* Cached copy of the previous frame's resume address. */
80 struct {
81 int p;
82 CORE_ADDR value;
83 } prev_pc;
84
85 /* Cached copy of the previous frame's function address. */
86 struct
87 {
88 CORE_ADDR addr;
89 int p;
90 } prev_func;
91
92 /* This frame's ID. */
93 struct
94 {
95 int p;
96 struct frame_id value;
97 } this_id;
98
99 /* The frame's high-level base methods, and corresponding cache.
100 The high level base methods are selected based on the frame's
101 debug info. */
102 const struct frame_base *base;
103 void *base_cache;
104
105 /* Pointers to the next (down, inner, younger) and previous (up,
106 outer, older) frame_info's in the frame cache. */
107 struct frame_info *next; /* down, inner, younger */
108 int prev_p;
109 struct frame_info *prev; /* up, outer, older */
110};
111
ac2bd0a9
AC
112/* Flag to control debugging. */
113
114static int frame_debug;
115
25d29d70
AC
116/* Flag to indicate whether backtraces should stop at main et.al. */
117
118static int backtrace_past_main;
2315ffec 119static int backtrace_past_entry;
25d29d70 120static unsigned int backtrace_limit = UINT_MAX;
eb4f72c5 121
ca73dd9d
AC
122static void
123fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
124{
125 if (p)
126 fprintf_unfiltered (file, "%s=0x%s", name, paddr_nz (addr));
127 else
128 fprintf_unfiltered (file, "!%s", name);
129}
d65fe839 130
00905d52 131void
7f78e237
AC
132fprint_frame_id (struct ui_file *file, struct frame_id id)
133{
ca73dd9d
AC
134 fprintf_unfiltered (file, "{");
135 fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
136 fprintf_unfiltered (file, ",");
137 fprint_field (file, "code", id.code_addr_p, id.code_addr);
138 fprintf_unfiltered (file, ",");
139 fprint_field (file, "special", id.special_addr_p, id.special_addr);
140 fprintf_unfiltered (file, "}");
7f78e237
AC
141}
142
143static void
144fprint_frame_type (struct ui_file *file, enum frame_type type)
145{
146 switch (type)
147 {
7f78e237
AC
148 case NORMAL_FRAME:
149 fprintf_unfiltered (file, "NORMAL_FRAME");
150 return;
151 case DUMMY_FRAME:
152 fprintf_unfiltered (file, "DUMMY_FRAME");
153 return;
154 case SIGTRAMP_FRAME:
155 fprintf_unfiltered (file, "SIGTRAMP_FRAME");
156 return;
157 default:
158 fprintf_unfiltered (file, "<unknown type>");
159 return;
160 };
161}
162
163static void
164fprint_frame (struct ui_file *file, struct frame_info *fi)
165{
166 if (fi == NULL)
167 {
168 fprintf_unfiltered (file, "<NULL frame>");
169 return;
170 }
171 fprintf_unfiltered (file, "{");
172 fprintf_unfiltered (file, "level=%d", fi->level);
173 fprintf_unfiltered (file, ",");
174 fprintf_unfiltered (file, "type=");
c1bf6f65
AC
175 if (fi->unwind != NULL)
176 fprint_frame_type (file, fi->unwind->type);
177 else
178 fprintf_unfiltered (file, "<unknown>");
7f78e237
AC
179 fprintf_unfiltered (file, ",");
180 fprintf_unfiltered (file, "unwind=");
181 if (fi->unwind != NULL)
182 gdb_print_host_address (fi->unwind, file);
183 else
184 fprintf_unfiltered (file, "<unknown>");
185 fprintf_unfiltered (file, ",");
186 fprintf_unfiltered (file, "pc=");
187 if (fi->next != NULL && fi->next->prev_pc.p)
188 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value));
189 else
190 fprintf_unfiltered (file, "<unknown>");
191 fprintf_unfiltered (file, ",");
192 fprintf_unfiltered (file, "id=");
193 if (fi->this_id.p)
194 fprint_frame_id (file, fi->this_id.value);
195 else
196 fprintf_unfiltered (file, "<unknown>");
197 fprintf_unfiltered (file, ",");
198 fprintf_unfiltered (file, "func=");
199 if (fi->next != NULL && fi->next->prev_func.p)
200 fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr));
201 else
202 fprintf_unfiltered (file, "<unknown>");
203 fprintf_unfiltered (file, "}");
204}
205
7a424e99 206/* Return a frame uniq ID that can be used to, later, re-find the
101dcfbe
AC
207 frame. */
208
7a424e99
AC
209struct frame_id
210get_frame_id (struct frame_info *fi)
101dcfbe
AC
211{
212 if (fi == NULL)
213 {
7a424e99 214 return null_frame_id;
101dcfbe 215 }
d0a55772 216 if (!fi->this_id.p)
101dcfbe 217 {
7f78e237
AC
218 if (frame_debug)
219 fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
220 fi->level);
c50901fd
AC
221 /* Find the unwinder. */
222 if (fi->unwind == NULL)
c1bf6f65
AC
223 fi->unwind = frame_unwind_find_by_frame (fi->next,
224 &fi->prologue_cache);
06c77151 225 /* Find THIS frame's ID. */
d0a55772
AC
226 fi->unwind->this_id (fi->next, &fi->prologue_cache, &fi->this_id.value);
227 fi->this_id.p = 1;
7f78e237
AC
228 if (frame_debug)
229 {
230 fprintf_unfiltered (gdb_stdlog, "-> ");
231 fprint_frame_id (gdb_stdlog, fi->this_id.value);
232 fprintf_unfiltered (gdb_stdlog, " }\n");
233 }
101dcfbe 234 }
18adea3f 235 return fi->this_id.value;
101dcfbe
AC
236}
237
5613d8d3
AC
238struct frame_id
239frame_unwind_id (struct frame_info *next_frame)
240{
241 /* Use prev_frame, and not get_prev_frame. The latter will truncate
242 the frame chain, leading to this function unintentionally
243 returning a null_frame_id (e.g., when a caller requests the frame
244 ID of "main()"s caller. */
245 return get_frame_id (get_prev_frame_1 (next_frame));
246}
247
7a424e99
AC
248const struct frame_id null_frame_id; /* All zeros. */
249
250struct frame_id
48c66725
JJ
251frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
252 CORE_ADDR special_addr)
7a424e99 253{
12b0b6de 254 struct frame_id id = null_frame_id;
d0a55772 255 id.stack_addr = stack_addr;
12b0b6de 256 id.stack_addr_p = 1;
d0a55772 257 id.code_addr = code_addr;
12b0b6de 258 id.code_addr_p = 1;
48c66725 259 id.special_addr = special_addr;
12b0b6de 260 id.special_addr_p = 1;
7a424e99
AC
261 return id;
262}
263
48c66725
JJ
264struct frame_id
265frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
266{
12b0b6de
UW
267 struct frame_id id = null_frame_id;
268 id.stack_addr = stack_addr;
269 id.stack_addr_p = 1;
270 id.code_addr = code_addr;
271 id.code_addr_p = 1;
272 return id;
273}
274
275struct frame_id
276frame_id_build_wild (CORE_ADDR stack_addr)
277{
278 struct frame_id id = null_frame_id;
279 id.stack_addr = stack_addr;
280 id.stack_addr_p = 1;
281 return id;
48c66725
JJ
282}
283
7a424e99
AC
284int
285frame_id_p (struct frame_id l)
286{
d0a55772 287 int p;
12b0b6de
UW
288 /* The frame is valid iff it has a valid stack address. */
289 p = l.stack_addr_p;
7f78e237
AC
290 if (frame_debug)
291 {
292 fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
293 fprint_frame_id (gdb_stdlog, l);
294 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
295 }
d0a55772 296 return p;
7a424e99
AC
297}
298
299int
300frame_id_eq (struct frame_id l, struct frame_id r)
301{
d0a55772 302 int eq;
12b0b6de
UW
303 if (!l.stack_addr_p || !r.stack_addr_p)
304 /* Like a NaN, if either ID is invalid, the result is false.
305 Note that a frame ID is invalid iff it is the null frame ID. */
d0a55772
AC
306 eq = 0;
307 else if (l.stack_addr != r.stack_addr)
308 /* If .stack addresses are different, the frames are different. */
309 eq = 0;
12b0b6de
UW
310 else if (!l.code_addr_p || !r.code_addr_p)
311 /* An invalid code addr is a wild card, always succeed. */
d0a55772 312 eq = 1;
48c66725
JJ
313 else if (l.code_addr != r.code_addr)
314 /* If .code addresses are different, the frames are different. */
315 eq = 0;
12b0b6de
UW
316 else if (!l.special_addr_p || !r.special_addr_p)
317 /* An invalid special addr is a wild card (or unused), always succeed. */
48c66725
JJ
318 eq = 1;
319 else if (l.special_addr == r.special_addr)
320 /* Frames are equal. */
d0a55772
AC
321 eq = 1;
322 else
4aa79dcc
AC
323 /* No luck. */
324 eq = 0;
7f78e237
AC
325 if (frame_debug)
326 {
327 fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
328 fprint_frame_id (gdb_stdlog, l);
329 fprintf_unfiltered (gdb_stdlog, ",r=");
330 fprint_frame_id (gdb_stdlog, r);
331 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
332 }
d0a55772 333 return eq;
7a424e99
AC
334}
335
336int
337frame_id_inner (struct frame_id l, struct frame_id r)
338{
d0a55772 339 int inner;
12b0b6de 340 if (!l.stack_addr_p || !r.stack_addr_p)
d0a55772
AC
341 /* Like NaN, any operation involving an invalid ID always fails. */
342 inner = 0;
343 else
344 /* Only return non-zero when strictly inner than. Note that, per
345 comment in "frame.h", there is some fuzz here. Frameless
346 functions are not strictly inner than (same .stack but
48c66725 347 different .code and/or .special address). */
d0a55772 348 inner = INNER_THAN (l.stack_addr, r.stack_addr);
7f78e237
AC
349 if (frame_debug)
350 {
351 fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
352 fprint_frame_id (gdb_stdlog, l);
353 fprintf_unfiltered (gdb_stdlog, ",r=");
354 fprint_frame_id (gdb_stdlog, r);
355 fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
356 }
d0a55772 357 return inner;
7a424e99
AC
358}
359
101dcfbe
AC
360struct frame_info *
361frame_find_by_id (struct frame_id id)
362{
363 struct frame_info *frame;
364
365 /* ZERO denotes the null frame, let the caller decide what to do
366 about it. Should it instead return get_current_frame()? */
7a424e99 367 if (!frame_id_p (id))
101dcfbe
AC
368 return NULL;
369
370 for (frame = get_current_frame ();
371 frame != NULL;
372 frame = get_prev_frame (frame))
373 {
7a424e99
AC
374 struct frame_id this = get_frame_id (frame);
375 if (frame_id_eq (id, this))
376 /* An exact match. */
377 return frame;
378 if (frame_id_inner (id, this))
379 /* Gone to far. */
101dcfbe 380 return NULL;
bbde78fa
JM
381 /* Either we're not yet gone far enough out along the frame
382 chain (inner(this,id)), or we're comparing frameless functions
7a424e99
AC
383 (same .base, different .func, no test available). Struggle
384 on until we've definitly gone to far. */
101dcfbe
AC
385 }
386 return NULL;
387}
388
f18c5a73 389CORE_ADDR
12cc2063 390frame_pc_unwind (struct frame_info *this_frame)
f18c5a73 391{
d1340264 392 if (!this_frame->prev_pc.p)
f18c5a73 393 {
12cc2063 394 CORE_ADDR pc;
cbafadeb
AC
395 if (this_frame->unwind == NULL)
396 this_frame->unwind
397 = frame_unwind_find_by_frame (this_frame->next,
398 &this_frame->prologue_cache);
399 if (this_frame->unwind->prev_pc != NULL)
400 /* A per-frame unwinder, prefer it. */
401 pc = this_frame->unwind->prev_pc (this_frame->next,
402 &this_frame->prologue_cache);
403 else if (gdbarch_unwind_pc_p (current_gdbarch))
12cc2063
AC
404 {
405 /* The right way. The `pure' way. The one true way. This
406 method depends solely on the register-unwind code to
407 determine the value of registers in THIS frame, and hence
408 the value of this frame's PC (resume address). A typical
409 implementation is no more than:
410
411 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
af1342ab 412 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
12cc2063
AC
413
414 Note: this method is very heavily dependent on a correct
415 register-unwind implementation, it pays to fix that
416 method first; this method is frame type agnostic, since
417 it only deals with register values, it works with any
418 frame. This is all in stark contrast to the old
419 FRAME_SAVED_PC which would try to directly handle all the
420 different ways that a PC could be unwound. */
421 pc = gdbarch_unwind_pc (current_gdbarch, this_frame);
422 }
12cc2063 423 else
e2e0b3e5 424 internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
d1340264
AC
425 this_frame->prev_pc.value = pc;
426 this_frame->prev_pc.p = 1;
7f78e237
AC
427 if (frame_debug)
428 fprintf_unfiltered (gdb_stdlog,
429 "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n",
430 this_frame->level,
431 paddr_nz (this_frame->prev_pc.value));
f18c5a73 432 }
d1340264 433 return this_frame->prev_pc.value;
f18c5a73
AC
434}
435
be41e9f4
AC
436CORE_ADDR
437frame_func_unwind (struct frame_info *fi)
438{
439 if (!fi->prev_func.p)
440 {
57bfe177
AC
441 /* Make certain that this, and not the adjacent, function is
442 found. */
443 CORE_ADDR addr_in_block = frame_unwind_address_in_block (fi);
be41e9f4 444 fi->prev_func.p = 1;
57bfe177 445 fi->prev_func.addr = get_pc_function_start (addr_in_block);
7f78e237
AC
446 if (frame_debug)
447 fprintf_unfiltered (gdb_stdlog,
448 "{ frame_func_unwind (fi=%d) -> 0x%s }\n",
449 fi->level, paddr_nz (fi->prev_func.addr));
be41e9f4
AC
450 }
451 return fi->prev_func.addr;
452}
453
454CORE_ADDR
455get_frame_func (struct frame_info *fi)
456{
457 return frame_func_unwind (fi->next);
458}
459
7a25a7c1 460static int
a81dcb05 461do_frame_register_read (void *src, int regnum, void *buf)
7a25a7c1 462{
a81dcb05 463 frame_register_read (src, regnum, buf);
7a25a7c1
AC
464 return 1;
465}
466
a81dcb05
AC
467struct regcache *
468frame_save_as_regcache (struct frame_info *this_frame)
469{
470 struct regcache *regcache = regcache_xmalloc (current_gdbarch);
471 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
472 regcache_save (regcache, do_frame_register_read, this_frame);
473 discard_cleanups (cleanups);
474 return regcache;
475}
476
dbe9fe58 477void
7a25a7c1
AC
478frame_pop (struct frame_info *this_frame)
479{
c1bf6f65
AC
480 /* Make a copy of all the register values unwound from this frame.
481 Save them in a scratch buffer so that there isn't a race between
482 trying to extract the old values from the current_regcache while
483 at the same time writing new values into that same cache. */
a81dcb05
AC
484 struct regcache *scratch
485 = frame_save_as_regcache (get_prev_frame_1 (this_frame));
c1bf6f65 486 struct cleanup *cleanups = make_cleanup_regcache_xfree (scratch);
c1bf6f65
AC
487
488 /* FIXME: cagney/2003-03-16: It should be possible to tell the
489 target's register cache that it is about to be hit with a burst
490 register transfer and that the sequence of register writes should
491 be batched. The pair target_prepare_to_store() and
492 target_store_registers() kind of suggest this functionality.
493 Unfortunately, they don't implement it. Their lack of a formal
494 definition can lead to targets writing back bogus values
495 (arguably a bug in the target code mind). */
496 /* Now copy those saved registers into the current regcache.
497 Here, regcache_cpy() calls regcache_restore(). */
498 regcache_cpy (current_regcache, scratch);
499 do_cleanups (cleanups);
7a25a7c1 500
7a25a7c1
AC
501 /* We've made right mess of GDB's local state, just discard
502 everything. */
dbe9fe58
AC
503 flush_cached_frames ();
504}
c689142b 505
4f460812
AC
506void
507frame_register_unwind (struct frame_info *frame, int regnum,
508 int *optimizedp, enum lval_type *lvalp,
509 CORE_ADDR *addrp, int *realnump, void *bufferp)
510{
511 struct frame_unwind_cache *cache;
512
7f78e237
AC
513 if (frame_debug)
514 {
6764ddad
AC
515 fprintf_unfiltered (gdb_stdlog, "\
516{ frame_register_unwind (frame=%d,regnum=%d(%s),...) ",
517 frame->level, regnum,
518 frame_map_regnum_to_name (frame, regnum));
7f78e237
AC
519 }
520
4f460812
AC
521 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
522 that the value proper does not need to be fetched. */
523 gdb_assert (optimizedp != NULL);
524 gdb_assert (lvalp != NULL);
525 gdb_assert (addrp != NULL);
526 gdb_assert (realnump != NULL);
527 /* gdb_assert (bufferp != NULL); */
528
a94dd1fd
AC
529 /* NOTE: cagney/2002-11-27: A program trying to unwind a NULL frame
530 is broken. There is always a frame. If there, for some reason,
bbde78fa 531 isn't a frame, there is some pretty busted code as it should have
a94dd1fd
AC
532 detected the problem before calling here. */
533 gdb_assert (frame != NULL);
4f460812 534
c50901fd
AC
535 /* Find the unwinder. */
536 if (frame->unwind == NULL)
c1bf6f65
AC
537 frame->unwind = frame_unwind_find_by_frame (frame->next,
538 &frame->prologue_cache);
c50901fd 539
6dc42492 540 /* Ask this frame to unwind its register. See comment in
bbde78fa 541 "frame-unwind.h" for why NEXT frame and this unwind cache are
6dc42492
AC
542 passed in. */
543 frame->unwind->prev_register (frame->next, &frame->prologue_cache, regnum,
544 optimizedp, lvalp, addrp, realnump, bufferp);
545
7f78e237
AC
546 if (frame_debug)
547 {
548 fprintf_unfiltered (gdb_stdlog, "->");
549 fprintf_unfiltered (gdb_stdlog, " *optimizedp=%d", (*optimizedp));
550 fprintf_unfiltered (gdb_stdlog, " *lvalp=%d", (int) (*lvalp));
551 fprintf_unfiltered (gdb_stdlog, " *addrp=0x%s", paddr_nz ((*addrp)));
552 fprintf_unfiltered (gdb_stdlog, " *bufferp=");
553 if (bufferp == NULL)
554 fprintf_unfiltered (gdb_stdlog, "<NULL>");
555 else
556 {
557 int i;
d2cf594a 558 const unsigned char *buf = bufferp;
7f78e237
AC
559 fprintf_unfiltered (gdb_stdlog, "[");
560 for (i = 0; i < register_size (current_gdbarch, regnum); i++)
561 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
562 fprintf_unfiltered (gdb_stdlog, "]");
563 }
564 fprintf_unfiltered (gdb_stdlog, " }\n");
565 }
4f460812
AC
566}
567
a216a322
AC
568void
569frame_register (struct frame_info *frame, int regnum,
570 int *optimizedp, enum lval_type *lvalp,
571 CORE_ADDR *addrp, int *realnump, void *bufferp)
572{
573 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
574 that the value proper does not need to be fetched. */
575 gdb_assert (optimizedp != NULL);
576 gdb_assert (lvalp != NULL);
577 gdb_assert (addrp != NULL);
578 gdb_assert (realnump != NULL);
579 /* gdb_assert (bufferp != NULL); */
580
a94dd1fd
AC
581 /* Obtain the register value by unwinding the register from the next
582 (more inner frame). */
583 gdb_assert (frame != NULL && frame->next != NULL);
584 frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
585 realnump, bufferp);
a216a322
AC
586}
587
135c175f 588void
5b181d62 589frame_unwind_register (struct frame_info *frame, int regnum, void *buf)
135c175f
AC
590{
591 int optimized;
592 CORE_ADDR addr;
593 int realnum;
594 enum lval_type lval;
135c175f
AC
595 frame_register_unwind (frame, regnum, &optimized, &lval, &addr,
596 &realnum, buf);
5b181d62
AC
597}
598
f0e7d0e8
AC
599void
600get_frame_register (struct frame_info *frame,
601 int regnum, void *buf)
602{
603 frame_unwind_register (frame->next, regnum, buf);
604}
605
606LONGEST
607frame_unwind_register_signed (struct frame_info *frame, int regnum)
608{
609 char buf[MAX_REGISTER_SIZE];
610 frame_unwind_register (frame, regnum, buf);
5bc602c7
AC
611 return extract_signed_integer (buf, register_size (get_frame_arch (frame),
612 regnum));
f0e7d0e8
AC
613}
614
615LONGEST
616get_frame_register_signed (struct frame_info *frame, int regnum)
617{
618 return frame_unwind_register_signed (frame->next, regnum);
619}
620
621ULONGEST
622frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
623{
624 char buf[MAX_REGISTER_SIZE];
625 frame_unwind_register (frame, regnum, buf);
5bc602c7
AC
626 return extract_unsigned_integer (buf, register_size (get_frame_arch (frame),
627 regnum));
f0e7d0e8
AC
628}
629
630ULONGEST
631get_frame_register_unsigned (struct frame_info *frame, int regnum)
632{
633 return frame_unwind_register_unsigned (frame->next, regnum);
634}
635
135c175f
AC
636void
637frame_unwind_unsigned_register (struct frame_info *frame, int regnum,
638 ULONGEST *val)
639{
d9d9c31f 640 char buf[MAX_REGISTER_SIZE];
5b181d62 641 frame_unwind_register (frame, regnum, buf);
5bc602c7
AC
642 (*val) = extract_unsigned_integer (buf,
643 register_size (get_frame_arch (frame),
644 regnum));
135c175f 645}
4f460812 646
ff2e87ac
AC
647void
648put_frame_register (struct frame_info *frame, int regnum, const void *buf)
649{
650 struct gdbarch *gdbarch = get_frame_arch (frame);
651 int realnum;
652 int optim;
653 enum lval_type lval;
654 CORE_ADDR addr;
655 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL);
656 if (optim)
8a3fe4f8 657 error (_("Attempt to assign to a value that was optimized out."));
ff2e87ac
AC
658 switch (lval)
659 {
660 case lval_memory:
661 {
662 /* FIXME: write_memory doesn't yet take constant buffers.
663 Arrrg! */
664 char tmp[MAX_REGISTER_SIZE];
665 memcpy (tmp, buf, register_size (gdbarch, regnum));
666 write_memory (addr, tmp, register_size (gdbarch, regnum));
667 break;
668 }
669 case lval_register:
670 regcache_cooked_write (current_regcache, realnum, buf);
671 break;
672 default:
8a3fe4f8 673 error (_("Attempt to assign to an unmodifiable value."));
ff2e87ac
AC
674 }
675}
676
cda5a58a 677/* frame_register_read ()
d65fe839 678
cda5a58a 679 Find and return the value of REGNUM for the specified stack frame.
5bc602c7 680 The number of bytes copied is REGISTER_SIZE (REGNUM).
d65fe839 681
cda5a58a 682 Returns 0 if the register value could not be found. */
d65fe839 683
cda5a58a
AC
684int
685frame_register_read (struct frame_info *frame, int regnum, void *myaddr)
d65fe839 686{
a216a322
AC
687 int optimized;
688 enum lval_type lval;
689 CORE_ADDR addr;
690 int realnum;
691 frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr);
d65fe839 692
bbde78fa 693 /* FIXME: cagney/2002-05-15: This test is just bogus.
c97dcfc7
AC
694
695 It indicates that the target failed to supply a value for a
696 register because it was "not available" at this time. Problem
697 is, the target still has the register and so get saved_register()
698 may be returning a value saved on the stack. */
699
d65fe839 700 if (register_cached (regnum) < 0)
cda5a58a 701 return 0; /* register value not available */
d65fe839 702
a216a322 703 return !optimized;
d65fe839 704}
e36180d7
AC
705
706
707/* Map between a frame register number and its name. A frame register
708 space is a superset of the cooked register space --- it also
709 includes builtin registers. */
710
711int
eb8bc282 712frame_map_name_to_regnum (struct frame_info *frame, const char *name, int len)
e36180d7 713{
eb8bc282 714 return user_reg_map_name_to_regnum (get_frame_arch (frame), name, len);
e36180d7
AC
715}
716
717const char *
eb8bc282 718frame_map_regnum_to_name (struct frame_info *frame, int regnum)
e36180d7 719{
eb8bc282 720 return user_reg_map_regnum_to_name (get_frame_arch (frame), regnum);
e36180d7 721}
4c1e7e9d 722
a94dd1fd
AC
723/* Create a sentinel frame. */
724
b9362cc7 725static struct frame_info *
a94dd1fd
AC
726create_sentinel_frame (struct regcache *regcache)
727{
728 struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
a94dd1fd
AC
729 frame->level = -1;
730 /* Explicitly initialize the sentinel frame's cache. Provide it
731 with the underlying regcache. In the future additional
732 information, such as the frame's thread will be added. */
6dc42492 733 frame->prologue_cache = sentinel_frame_cache (regcache);
a94dd1fd
AC
734 /* For the moment there is only one sentinel frame implementation. */
735 frame->unwind = sentinel_frame_unwind;
736 /* Link this frame back to itself. The frame is self referential
737 (the unwound PC is the same as the pc), so make it so. */
738 frame->next = frame;
50bbdbd9
AC
739 /* Make the sentinel frame's ID valid, but invalid. That way all
740 comparisons with it should fail. */
d0a55772
AC
741 frame->this_id.p = 1;
742 frame->this_id.value = null_frame_id;
7f78e237
AC
743 if (frame_debug)
744 {
745 fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
746 fprint_frame (gdb_stdlog, frame);
747 fprintf_unfiltered (gdb_stdlog, " }\n");
748 }
a94dd1fd
AC
749 return frame;
750}
751
4c1e7e9d
AC
752/* Info about the innermost stack frame (contents of FP register) */
753
754static struct frame_info *current_frame;
755
756/* Cache for frame addresses already read by gdb. Valid only while
757 inferior is stopped. Control variables for the frame cache should
758 be local to this module. */
759
760static struct obstack frame_cache_obstack;
761
762void *
479ab5a0 763frame_obstack_zalloc (unsigned long size)
4c1e7e9d 764{
479ab5a0
AC
765 void *data = obstack_alloc (&frame_cache_obstack, size);
766 memset (data, 0, size);
767 return data;
4c1e7e9d
AC
768}
769
a94dd1fd
AC
770/* Return the innermost (currently executing) stack frame. This is
771 split into two functions. The function unwind_to_current_frame()
772 is wrapped in catch exceptions so that, even when the unwind of the
773 sentinel frame fails, the function still returns a stack frame. */
774
775static int
776unwind_to_current_frame (struct ui_out *ui_out, void *args)
777{
778 struct frame_info *frame = get_prev_frame (args);
bbde78fa 779 /* A sentinel frame can fail to unwind, e.g., because its PC value
a94dd1fd
AC
780 lands in somewhere like start. */
781 if (frame == NULL)
782 return 1;
783 current_frame = frame;
784 return 0;
785}
4c1e7e9d
AC
786
787struct frame_info *
788get_current_frame (void)
789{
0a1e1ca1
AC
790 /* First check, and report, the lack of registers. Having GDB
791 report "No stack!" or "No memory" when the target doesn't even
792 have registers is very confusing. Besides, "printcmd.exp"
793 explicitly checks that ``print $pc'' with no registers prints "No
794 registers". */
a94dd1fd 795 if (!target_has_registers)
8a3fe4f8 796 error (_("No registers."));
0a1e1ca1 797 if (!target_has_stack)
8a3fe4f8 798 error (_("No stack."));
a94dd1fd 799 if (!target_has_memory)
8a3fe4f8 800 error (_("No memory."));
4c1e7e9d
AC
801 if (current_frame == NULL)
802 {
a94dd1fd
AC
803 struct frame_info *sentinel_frame =
804 create_sentinel_frame (current_regcache);
805 if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame,
1c3c7ee7 806 RETURN_MASK_ERROR) != 0)
a94dd1fd
AC
807 {
808 /* Oops! Fake a current frame? Is this useful? It has a PC
809 of zero, for instance. */
810 current_frame = sentinel_frame;
811 }
4c1e7e9d
AC
812 }
813 return current_frame;
814}
815
6e7f8b9c
AC
816/* The "selected" stack frame is used by default for local and arg
817 access. May be zero, for no selected frame. */
818
819struct frame_info *deprecated_selected_frame;
820
bbde78fa 821/* Return the selected frame. Always non-NULL (unless there isn't an
6e7f8b9c
AC
822 inferior sufficient for creating a frame) in which case an error is
823 thrown. */
824
825struct frame_info *
b04f3ab4 826get_selected_frame (const char *message)
6e7f8b9c
AC
827{
828 if (deprecated_selected_frame == NULL)
b04f3ab4
AC
829 {
830 if (message != NULL && (!target_has_registers
831 || !target_has_stack
832 || !target_has_memory))
8a3fe4f8 833 error (("%s"), message);
b04f3ab4
AC
834 /* Hey! Don't trust this. It should really be re-finding the
835 last selected frame of the currently selected thread. This,
836 though, is better than nothing. */
837 select_frame (get_current_frame ());
838 }
6e7f8b9c
AC
839 /* There is always a frame. */
840 gdb_assert (deprecated_selected_frame != NULL);
841 return deprecated_selected_frame;
842}
843
bbde78fa 844/* This is a variant of get_selected_frame() which can be called when
7dd88986 845 the inferior does not have a frame; in that case it will return
bbde78fa 846 NULL instead of calling error(). */
7dd88986
DJ
847
848struct frame_info *
849deprecated_safe_get_selected_frame (void)
850{
851 if (!target_has_registers || !target_has_stack || !target_has_memory)
852 return NULL;
b04f3ab4 853 return get_selected_frame (NULL);
7dd88986
DJ
854}
855
6e7f8b9c
AC
856/* Select frame FI (or NULL - to invalidate the current frame). */
857
858void
859select_frame (struct frame_info *fi)
860{
52f0bd74 861 struct symtab *s;
6e7f8b9c
AC
862
863 deprecated_selected_frame = fi;
bbde78fa 864 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
6e7f8b9c 865 frame is being invalidated. */
9a4105ab
AC
866 if (deprecated_selected_frame_level_changed_hook)
867 deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
6e7f8b9c
AC
868
869 /* FIXME: kseitz/2002-08-28: It would be nice to call
bbde78fa 870 selected_frame_level_changed_event() right here, but due to limitations
6e7f8b9c 871 in the current interfaces, we would end up flooding UIs with events
bbde78fa 872 because select_frame() is used extensively internally.
6e7f8b9c
AC
873
874 Once we have frame-parameterized frame (and frame-related) commands,
875 the event notification can be moved here, since this function will only
bbde78fa 876 be called when the user's selected frame is being changed. */
6e7f8b9c
AC
877
878 /* Ensure that symbols for this frame are read in. Also, determine the
879 source language of this frame, and switch to it if desired. */
880 if (fi)
881 {
7ae4c3a5 882 /* We retrieve the frame's symtab by using the frame PC. However
bbde78fa 883 we cannot use the frame PC as-is, because it usually points to
7ae4c3a5
JB
884 the instruction following the "call", which is sometimes the
885 first instruction of another function. So we rely on
886 get_frame_address_in_block() which provides us with a PC which
887 is guaranteed to be inside the frame's code block. */
888 s = find_pc_symtab (get_frame_address_in_block (fi));
6e7f8b9c
AC
889 if (s
890 && s->language != current_language->la_language
891 && s->language != language_unknown
892 && language_mode == language_mode_auto)
893 {
894 set_language (s->language);
895 }
896 }
897}
c689142b 898
4c1e7e9d
AC
899/* Create an arbitrary (i.e. address specified by user) or innermost frame.
900 Always returns a non-NULL value. */
901
902struct frame_info *
903create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
904{
905 struct frame_info *fi;
4c1e7e9d 906
7f78e237
AC
907 if (frame_debug)
908 {
909 fprintf_unfiltered (gdb_stdlog,
910 "{ create_new_frame (addr=0x%s, pc=0x%s) ",
911 paddr_nz (addr), paddr_nz (pc));
912 }
913
479ab5a0 914 fi = frame_obstack_zalloc (sizeof (struct frame_info));
4c1e7e9d 915
a94dd1fd 916 fi->next = create_sentinel_frame (current_regcache);
7df05f2b
AC
917
918 /* Select/initialize both the unwind function and the frame's type
919 based on the PC. */
82417da5 920 fi->unwind = frame_unwind_find_by_frame (fi->next, &fi->prologue_cache);
7df05f2b 921
18adea3f 922 fi->this_id.p = 1;
11889732
AC
923 deprecated_update_frame_base_hack (fi, addr);
924 deprecated_update_frame_pc_hack (fi, pc);
4c1e7e9d 925
7f78e237
AC
926 if (frame_debug)
927 {
928 fprintf_unfiltered (gdb_stdlog, "-> ");
929 fprint_frame (gdb_stdlog, fi);
930 fprintf_unfiltered (gdb_stdlog, " }\n");
931 }
932
4c1e7e9d
AC
933 return fi;
934}
935
03febf99
AC
936/* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
937 innermost frame). Be careful to not fall off the bottom of the
938 frame chain and onto the sentinel frame. */
4c1e7e9d
AC
939
940struct frame_info *
03febf99 941get_next_frame (struct frame_info *this_frame)
4c1e7e9d 942{
03febf99
AC
943 if (this_frame->level > 0)
944 return this_frame->next;
a94dd1fd
AC
945 else
946 return NULL;
4c1e7e9d
AC
947}
948
f4c5303c
OF
949/* Observer for the target_changed event. */
950
951void
952frame_observer_target_changed (struct target_ops *target)
953{
954 flush_cached_frames ();
955}
956
4c1e7e9d
AC
957/* Flush the entire frame cache. */
958
959void
960flush_cached_frames (void)
961{
962 /* Since we can't really be sure what the first object allocated was */
963 obstack_free (&frame_cache_obstack, 0);
964 obstack_init (&frame_cache_obstack);
965
966 current_frame = NULL; /* Invalidate cache */
967 select_frame (NULL);
968 annotate_frames_invalid ();
7f78e237
AC
969 if (frame_debug)
970 fprintf_unfiltered (gdb_stdlog, "{ flush_cached_frames () }\n");
4c1e7e9d
AC
971}
972
973/* Flush the frame cache, and start a new one if necessary. */
974
975void
976reinit_frame_cache (void)
977{
978 flush_cached_frames ();
979
980 /* FIXME: The inferior_ptid test is wrong if there is a corefile. */
981 if (PIDGET (inferior_ptid) != 0)
982 {
983 select_frame (get_current_frame ());
984 }
985}
986
5613d8d3
AC
987/* Return a "struct frame_info" corresponding to the frame that called
988 THIS_FRAME. Returns NULL if there is no such frame.
5bf00f29 989
5613d8d3
AC
990 Unlike get_prev_frame, this function always tries to unwind the
991 frame. */
eb4f72c5 992
5613d8d3
AC
993static struct frame_info *
994get_prev_frame_1 (struct frame_info *this_frame)
eb4f72c5
AC
995{
996 struct frame_info *prev_frame;
756e95f1 997 struct frame_id this_id;
eb4f72c5 998
5613d8d3
AC
999 gdb_assert (this_frame != NULL);
1000
7f78e237
AC
1001 if (frame_debug)
1002 {
5613d8d3 1003 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
7f78e237
AC
1004 if (this_frame != NULL)
1005 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1006 else
1007 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1008 fprintf_unfiltered (gdb_stdlog, ") ");
1009 }
1010
5613d8d3
AC
1011 /* Only try to do the unwind once. */
1012 if (this_frame->prev_p)
1013 {
1014 if (frame_debug)
1015 {
1016 fprintf_unfiltered (gdb_stdlog, "-> ");
1017 fprint_frame (gdb_stdlog, this_frame->prev);
1018 fprintf_unfiltered (gdb_stdlog, " // cached \n");
1019 }
1020 return this_frame->prev;
1021 }
1022 this_frame->prev_p = 1;
1023
5613d8d3
AC
1024 /* Check that this frame's ID was valid. If it wasn't, don't try to
1025 unwind to the prev frame. Be careful to not apply this test to
1026 the sentinel frame. */
756e95f1
MK
1027 this_id = get_frame_id (this_frame);
1028 if (this_frame->level >= 0 && !frame_id_p (this_id))
5613d8d3
AC
1029 {
1030 if (frame_debug)
1031 {
1032 fprintf_unfiltered (gdb_stdlog, "-> ");
1033 fprint_frame (gdb_stdlog, NULL);
1034 fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
1035 }
1036 return NULL;
1037 }
1038
1039 /* Check that this frame's ID isn't inner to (younger, below, next)
1040 the next frame. This happens when a frame unwind goes backwards.
adb54772
AC
1041 Exclude signal trampolines (due to sigaltstack the frame ID can
1042 go backwards) and sentinel frames (the test is meaningless). */
1043 if (this_frame->next->level >= 0
c1bf6f65 1044 && this_frame->next->unwind->type != SIGTRAMP_FRAME
756e95f1 1045 && frame_id_inner (this_id, get_frame_id (this_frame->next)))
8a3fe4f8 1046 error (_("Previous frame inner to this frame (corrupt stack?)"));
5613d8d3
AC
1047
1048 /* Check that this and the next frame are not identical. If they
1049 are, there is most likely a stack cycle. As with the inner-than
1050 test above, avoid comparing the inner-most and sentinel frames. */
1051 if (this_frame->level > 0
756e95f1 1052 && frame_id_eq (this_id, get_frame_id (this_frame->next)))
8a3fe4f8 1053 error (_("Previous frame identical to this frame (corrupt stack?)"));
5613d8d3
AC
1054
1055 /* Allocate the new frame but do not wire it in to the frame chain.
1056 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
1057 frame->next to pull some fancy tricks (of course such code is, by
1058 definition, recursive). Try to prevent it.
1059
1060 There is no reason to worry about memory leaks, should the
1061 remainder of the function fail. The allocated memory will be
1062 quickly reclaimed when the frame cache is flushed, and the `we've
1063 been here before' check above will stop repeated memory
1064 allocation calls. */
1065 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1066 prev_frame->level = this_frame->level + 1;
1067
1068 /* Don't yet compute ->unwind (and hence ->type). It is computed
1069 on-demand in get_frame_type, frame_register_unwind, and
1070 get_frame_id. */
1071
1072 /* Don't yet compute the frame's ID. It is computed on-demand by
1073 get_frame_id(). */
1074
1075 /* The unwound frame ID is validate at the start of this function,
1076 as part of the logic to decide if that frame should be further
1077 unwound, and not here while the prev frame is being created.
1078 Doing this makes it possible for the user to examine a frame that
1079 has an invalid frame ID.
1080
1081 Some very old VAX code noted: [...] For the sake of argument,
1082 suppose that the stack is somewhat trashed (which is one reason
1083 that "info frame" exists). So, return 0 (indicating we don't
1084 know the address of the arglist) if we don't know what frame this
1085 frame calls. */
1086
1087 /* Link it in. */
1088 this_frame->prev = prev_frame;
1089 prev_frame->next = this_frame;
1090
1091 if (frame_debug)
1092 {
1093 fprintf_unfiltered (gdb_stdlog, "-> ");
1094 fprint_frame (gdb_stdlog, prev_frame);
1095 fprintf_unfiltered (gdb_stdlog, " }\n");
1096 }
1097
1098 return prev_frame;
1099}
1100
1101/* Debug routine to print a NULL frame being returned. */
1102
1103static void
1104frame_debug_got_null_frame (struct ui_file *file,
1105 struct frame_info *this_frame,
1106 const char *reason)
1107{
1108 if (frame_debug)
1109 {
1110 fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
1111 if (this_frame != NULL)
1112 fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
1113 else
1114 fprintf_unfiltered (gdb_stdlog, "<NULL>");
1115 fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
1116 }
1117}
1118
c8cd9f6c
AC
1119/* Is this (non-sentinel) frame in the "main"() function? */
1120
1121static int
1122inside_main_func (struct frame_info *this_frame)
1123{
1124 struct minimal_symbol *msymbol;
1125 CORE_ADDR maddr;
1126
1127 if (symfile_objfile == 0)
1128 return 0;
1129 msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
1130 if (msymbol == NULL)
1131 return 0;
1132 /* Make certain that the code, and not descriptor, address is
1133 returned. */
1134 maddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
1135 SYMBOL_VALUE_ADDRESS (msymbol),
1136 &current_target);
1137 return maddr == get_frame_func (this_frame);
1138}
1139
2315ffec
RC
1140/* Test whether THIS_FRAME is inside the process entry point function. */
1141
1142static int
1143inside_entry_func (struct frame_info *this_frame)
1144{
1145 return (get_frame_func (this_frame) == entry_point_address ());
1146}
1147
5613d8d3
AC
1148/* Return a structure containing various interesting information about
1149 the frame that called THIS_FRAME. Returns NULL if there is entier
1150 no such frame or the frame fails any of a set of target-independent
1151 condition that should terminate the frame chain (e.g., as unwinding
1152 past main()).
1153
1154 This function should not contain target-dependent tests, such as
1155 checking whether the program-counter is zero. */
1156
1157struct frame_info *
1158get_prev_frame (struct frame_info *this_frame)
1159{
1160 struct frame_info *prev_frame;
1161
eb4f72c5
AC
1162 /* Return the inner-most frame, when the caller passes in NULL. */
1163 /* NOTE: cagney/2002-11-09: Not sure how this would happen. The
1164 caller should have previously obtained a valid frame using
1165 get_selected_frame() and then called this code - only possibility
1166 I can think of is code behaving badly.
1167
1168 NOTE: cagney/2003-01-10: Talk about code behaving badly. Check
1169 block_innermost_frame(). It does the sequence: frame = NULL;
1170 while (1) { frame = get_prev_frame (frame); .... }. Ulgh! Why
1171 it couldn't be written better, I don't know.
1172
bbde78fa 1173 NOTE: cagney/2003-01-11: I suspect what is happening in
eb4f72c5 1174 block_innermost_frame() is, when the target has no state
bbde78fa 1175 (registers, memory, ...), it is still calling this function. The
eb4f72c5
AC
1176 assumption being that this function will return NULL indicating
1177 that a frame isn't possible, rather than checking that the target
1178 has state and then calling get_current_frame() and
1179 get_prev_frame(). This is a guess mind. */
03febf99 1180 if (this_frame == NULL)
eb4f72c5
AC
1181 {
1182 /* NOTE: cagney/2002-11-09: There was a code segment here that
1183 would error out when CURRENT_FRAME was NULL. The comment
1184 that went with it made the claim ...
1185
1186 ``This screws value_of_variable, which just wants a nice
1187 clean NULL return from block_innermost_frame if there are no
1188 frames. I don't think I've ever seen this message happen
1189 otherwise. And returning NULL here is a perfectly legitimate
1190 thing to do.''
1191
1192 Per the above, this code shouldn't even be called with a NULL
03febf99 1193 THIS_FRAME. */
5613d8d3 1194 frame_debug_got_null_frame (gdb_stdlog, this_frame, "this_frame NULL");
eb4f72c5
AC
1195 return current_frame;
1196 }
1197
1198 /* There is always a frame. If this assertion fails, suspect that
1199 something should be calling get_selected_frame() or
1200 get_current_frame(). */
03febf99 1201 gdb_assert (this_frame != NULL);
eb4f72c5 1202
cc9bed83
RC
1203 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
1204 sense to stop unwinding at a dummy frame. One place where a dummy
1205 frame may have an address "inside_main_func" is on HPUX. On HPUX, the
1206 pcsqh register (space register for the instruction at the head of the
1207 instruction queue) cannot be written directly; the only way to set it
1208 is to branch to code that is in the target space. In order to implement
1209 frame dummies on HPUX, the called function is made to jump back to where
1210 the inferior was when the user function was called. If gdb was inside
1211 the main function when we created the dummy frame, the dummy frame will
1212 point inside the main function. */
03febf99 1213 if (this_frame->level >= 0
cc9bed83 1214 && get_frame_type (this_frame) != DUMMY_FRAME
25d29d70 1215 && !backtrace_past_main
c8cd9f6c
AC
1216 && inside_main_func (this_frame))
1217 /* Don't unwind past main(). Note, this is done _before_ the
1218 frame has been marked as previously unwound. That way if the
1219 user later decides to enable unwinds past main(), that will
1220 automatically happen. */
ac2bd0a9 1221 {
5613d8d3 1222 frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside main func");
ac2bd0a9
AC
1223 return NULL;
1224 }
eb4f72c5 1225
25d29d70
AC
1226 if (this_frame->level > backtrace_limit)
1227 {
8a3fe4f8 1228 error (_("Backtrace limit of %d exceeded"), backtrace_limit);
25d29d70
AC
1229 }
1230
0714963c
AC
1231 /* If we're already inside the entry function for the main objfile,
1232 then it isn't valid. Don't apply this test to a dummy frame -
bbde78fa 1233 dummy frame PCs typically land in the entry func. Don't apply
0714963c
AC
1234 this test to the sentinel frame. Sentinel frames should always
1235 be allowed to unwind. */
2f72f850
AC
1236 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
1237 wasn't checking for "main" in the minimal symbols. With that
1238 fixed asm-source tests now stop in "main" instead of halting the
bbde78fa 1239 backtrace in weird and wonderful ways somewhere inside the entry
2f72f850
AC
1240 file. Suspect that tests for inside the entry file/func were
1241 added to work around that (now fixed) case. */
0714963c
AC
1242 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
1243 suggested having the inside_entry_func test use the
bbde78fa
JM
1244 inside_main_func() msymbol trick (along with entry_point_address()
1245 I guess) to determine the address range of the start function.
0714963c
AC
1246 That should provide a far better stopper than the current
1247 heuristics. */
2315ffec
RC
1248 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
1249 applied tail-call optimizations to main so that a function called
1250 from main returns directly to the caller of main. Since we don't
1251 stop at main, we should at least stop at the entry point of the
1252 application. */
1253 if (!backtrace_past_entry
1d225535 1254 && get_frame_type (this_frame) != DUMMY_FRAME && this_frame->level >= 0
6e4c6c91 1255 && inside_entry_func (this_frame))
0714963c 1256 {
5613d8d3 1257 frame_debug_got_null_frame (gdb_stdlog, this_frame, "inside entry func");
0714963c
AC
1258 return NULL;
1259 }
1260
39ee2ff0
AC
1261 /* Assume that the only way to get a zero PC is through something
1262 like a SIGSEGV or a dummy frame, and hence that NORMAL frames
1263 will never unwind a zero PC. */
1264 if (this_frame->level > 0
1265 && get_frame_type (this_frame) == NORMAL_FRAME
1266 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1267 && get_frame_pc (this_frame) == 0)
1268 {
1269 frame_debug_got_null_frame (gdb_stdlog, this_frame, "zero PC");
1270 return NULL;
1271 }
1272
5613d8d3 1273 return get_prev_frame_1 (this_frame);
eb4f72c5
AC
1274}
1275
4c1e7e9d
AC
1276CORE_ADDR
1277get_frame_pc (struct frame_info *frame)
1278{
d1340264
AC
1279 gdb_assert (frame->next != NULL);
1280 return frame_pc_unwind (frame->next);
4c1e7e9d
AC
1281}
1282
8edd5d01
AC
1283/* Return an address of that falls within the frame's code block. */
1284
1285CORE_ADDR
1286frame_unwind_address_in_block (struct frame_info *next_frame)
1287{
1288 /* A draft address. */
1289 CORE_ADDR pc = frame_pc_unwind (next_frame);
1290
1291 /* If THIS frame is not inner most (i.e., NEXT isn't the sentinel),
1292 and NEXT is `normal' (i.e., not a sigtramp, dummy, ....) THIS
1293 frame's PC ends up pointing at the instruction fallowing the
1294 "call". Adjust that PC value so that it falls on the call
1295 instruction (which, hopefully, falls within THIS frame's code
1296 block. So far it's proved to be a very good approximation. See
bbde78fa 1297 get_frame_type() for why ->type can't be used. */
8edd5d01
AC
1298 if (next_frame->level >= 0
1299 && get_frame_type (next_frame) == NORMAL_FRAME)
1300 --pc;
1301 return pc;
1302}
1303
1304CORE_ADDR
1305get_frame_address_in_block (struct frame_info *this_frame)
1306{
1307 return frame_unwind_address_in_block (this_frame->next);
1308}
1309
1058bca7
AC
1310static int
1311pc_notcurrent (struct frame_info *frame)
1312{
1313 /* If FRAME is not the innermost frame, that normally means that
1314 FRAME->pc points at the return instruction (which is *after* the
1315 call instruction), and we want to get the line containing the
1316 call (because the call is where the user thinks the program is).
1317 However, if the next frame is either a SIGTRAMP_FRAME or a
1318 DUMMY_FRAME, then the next frame will contain a saved interrupt
1319 PC and such a PC indicates the current (rather than next)
1320 instruction/line, consequently, for such cases, want to get the
1321 line containing fi->pc. */
1322 struct frame_info *next = get_next_frame (frame);
1323 int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME);
1324 return notcurrent;
1325}
1326
1327void
1328find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
1329{
11889732 1330 (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame));
1058bca7
AC
1331}
1332
c193f6ac
AC
1333/* Per "frame.h", return the ``address'' of the frame. Code should
1334 really be using get_frame_id(). */
1335CORE_ADDR
1336get_frame_base (struct frame_info *fi)
1337{
d0a55772 1338 return get_frame_id (fi).stack_addr;
c193f6ac
AC
1339}
1340
da62e633
AC
1341/* High-level offsets into the frame. Used by the debug info. */
1342
1343CORE_ADDR
1344get_frame_base_address (struct frame_info *fi)
1345{
7df05f2b 1346 if (get_frame_type (fi) != NORMAL_FRAME)
da62e633
AC
1347 return 0;
1348 if (fi->base == NULL)
e8a89fe2 1349 fi->base = frame_base_find_by_frame (fi->next);
da62e633
AC
1350 /* Sneaky: If the low-level unwind and high-level base code share a
1351 common unwinder, let them share the prologue cache. */
1352 if (fi->base->unwind == fi->unwind)
1353 return fi->base->this_base (fi->next, &fi->prologue_cache);
1354 return fi->base->this_base (fi->next, &fi->base_cache);
1355}
1356
1357CORE_ADDR
1358get_frame_locals_address (struct frame_info *fi)
1359{
1360 void **cache;
7df05f2b 1361 if (get_frame_type (fi) != NORMAL_FRAME)
da62e633
AC
1362 return 0;
1363 /* If there isn't a frame address method, find it. */
1364 if (fi->base == NULL)
e8a89fe2 1365 fi->base = frame_base_find_by_frame (fi->next);
da62e633
AC
1366 /* Sneaky: If the low-level unwind and high-level base code share a
1367 common unwinder, let them share the prologue cache. */
1368 if (fi->base->unwind == fi->unwind)
1369 cache = &fi->prologue_cache;
1370 else
1371 cache = &fi->base_cache;
1372 return fi->base->this_locals (fi->next, cache);
1373}
1374
1375CORE_ADDR
1376get_frame_args_address (struct frame_info *fi)
1377{
1378 void **cache;
7df05f2b 1379 if (get_frame_type (fi) != NORMAL_FRAME)
da62e633
AC
1380 return 0;
1381 /* If there isn't a frame address method, find it. */
1382 if (fi->base == NULL)
e8a89fe2 1383 fi->base = frame_base_find_by_frame (fi->next);
da62e633
AC
1384 /* Sneaky: If the low-level unwind and high-level base code share a
1385 common unwinder, let them share the prologue cache. */
1386 if (fi->base->unwind == fi->unwind)
1387 cache = &fi->prologue_cache;
1388 else
1389 cache = &fi->base_cache;
1390 return fi->base->this_args (fi->next, cache);
1391}
1392
85cf597a
AC
1393/* Level of the selected frame: 0 for innermost, 1 for its caller, ...
1394 or -1 for a NULL frame. */
1395
1396int
1397frame_relative_level (struct frame_info *fi)
1398{
1399 if (fi == NULL)
1400 return -1;
1401 else
1402 return fi->level;
1403}
1404
5a203e44
AC
1405enum frame_type
1406get_frame_type (struct frame_info *frame)
1407{
c1bf6f65
AC
1408 if (frame->unwind == NULL)
1409 /* Initialize the frame's unwinder because that's what
1410 provides the frame's type. */
1411 frame->unwind = frame_unwind_find_by_frame (frame->next,
1412 &frame->prologue_cache);
1413 return frame->unwind->type;
5a203e44
AC
1414}
1415
b87efeee 1416void
2f107107 1417deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc)
b87efeee 1418{
7f78e237
AC
1419 if (frame_debug)
1420 fprintf_unfiltered (gdb_stdlog,
1421 "{ deprecated_update_frame_pc_hack (frame=%d,pc=0x%s) }\n",
1422 frame->level, paddr_nz (pc));
e0d2ae16 1423 /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are
bbde78fa 1424 maintaining a locally allocated frame object. Since such frames
e0d2ae16
AC
1425 are not in the frame chain, it isn't possible to assume that the
1426 frame has a next. Sigh. */
1427 if (frame->next != NULL)
1428 {
1429 /* While we're at it, update this frame's cached PC value, found
1430 in the next frame. Oh for the day when "struct frame_info"
1431 is opaque and this hack on hack can just go away. */
d1340264
AC
1432 frame->next->prev_pc.value = pc;
1433 frame->next->prev_pc.p = 1;
e0d2ae16 1434 }
2f107107
AC
1435}
1436
1437void
1438deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base)
1439{
7f78e237
AC
1440 if (frame_debug)
1441 fprintf_unfiltered (gdb_stdlog,
1442 "{ deprecated_update_frame_base_hack (frame=%d,base=0x%s) }\n",
1443 frame->level, paddr_nz (base));
2f107107 1444 /* See comment in "frame.h". */
d0a55772 1445 frame->this_id.value.stack_addr = base;
b87efeee
AC
1446}
1447
ae1e7417
AC
1448/* Memory access methods. */
1449
1450void
1451get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr, void *buf,
1452 int len)
1453{
1454 read_memory (addr, buf, len);
1455}
1456
1457LONGEST
1458get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
1459 int len)
1460{
1461 return read_memory_integer (addr, len);
1462}
1463
1464ULONGEST
1465get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
1466 int len)
1467{
1468 return read_memory_unsigned_integer (addr, len);
1469}
1470
304396fb
AC
1471int
1472safe_frame_unwind_memory (struct frame_info *this_frame,
1473 CORE_ADDR addr, void *buf, int len)
1474{
1f602b35
AC
1475 /* NOTE: deprecated_read_memory_nobpt returns zero on success! */
1476 return !deprecated_read_memory_nobpt (addr, buf, len);
304396fb
AC
1477}
1478
ae1e7417
AC
1479/* Architecture method. */
1480
1481struct gdbarch *
1482get_frame_arch (struct frame_info *this_frame)
1483{
1484 return current_gdbarch;
1485}
1486
a9e5fdc2
AC
1487/* Stack pointer methods. */
1488
1489CORE_ADDR
1490get_frame_sp (struct frame_info *this_frame)
1491{
1492 return frame_sp_unwind (this_frame->next);
1493}
1494
1495CORE_ADDR
1496frame_sp_unwind (struct frame_info *next_frame)
1497{
bbde78fa 1498 /* Normality - an architecture that provides a way of obtaining any
a9e5fdc2
AC
1499 frame inner-most address. */
1500 if (gdbarch_unwind_sp_p (current_gdbarch))
1501 return gdbarch_unwind_sp (current_gdbarch, next_frame);
1502 /* Things are looking grim. If it's the inner-most frame and there
bbde78fa 1503 is a TARGET_READ_SP, then that can be used. */
a9e5fdc2
AC
1504 if (next_frame->level < 0 && TARGET_READ_SP_P ())
1505 return TARGET_READ_SP ();
1506 /* Now things are really are grim. Hope that the value returned by
1507 the SP_REGNUM register is meaningful. */
1508 if (SP_REGNUM >= 0)
1509 {
1510 ULONGEST sp;
1511 frame_unwind_unsigned_register (next_frame, SP_REGNUM, &sp);
1512 return sp;
1513 }
e2e0b3e5 1514 internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
a9e5fdc2
AC
1515}
1516
b9362cc7
AC
1517extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
1518
25d29d70
AC
1519static struct cmd_list_element *set_backtrace_cmdlist;
1520static struct cmd_list_element *show_backtrace_cmdlist;
1521
1522static void
1523set_backtrace_cmd (char *args, int from_tty)
1524{
1525 help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
1526}
1527
1528static void
1529show_backtrace_cmd (char *args, int from_tty)
1530{
1531 cmd_show_list (show_backtrace_cmdlist, from_tty, "");
1532}
1533
4c1e7e9d
AC
1534void
1535_initialize_frame (void)
1536{
1537 obstack_init (&frame_cache_obstack);
eb4f72c5 1538
f4c5303c
OF
1539 observer_attach_target_changed (frame_observer_target_changed);
1540
25d29d70
AC
1541 add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, "\
1542Set backtrace specific variables.\n\
1543Configure backtrace variables such as the backtrace limit",
1544 &set_backtrace_cmdlist, "set backtrace ",
1545 0/*allow-unknown*/, &setlist);
1546 add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, "\
1547Show backtrace specific variables\n\
1548Show backtrace variables such as the backtrace limit",
1549 &show_backtrace_cmdlist, "show backtrace ",
1550 0/*allow-unknown*/, &showlist);
1551
1552 add_setshow_boolean_cmd ("past-main", class_obscure,
1553 &backtrace_past_main, "\
3b64bf98
AC
1554Set whether backtraces should continue past \"main\".", "\
1555Show whether backtraces should continue past \"main\".", "\
eb4f72c5
AC
1556Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
1557the backtrace at \"main\". Set this variable if you need to see the rest\n\
335cca0d
AC
1558of the stack trace.",
1559 NULL, /* PRINT: Whether backtraces should continue past \"main\" is %s. */
25d29d70
AC
1560 NULL, NULL, &set_backtrace_cmdlist,
1561 &show_backtrace_cmdlist);
1562
2315ffec
RC
1563 add_setshow_boolean_cmd ("past-entry", class_obscure,
1564 &backtrace_past_entry, "\
1565Set whether backtraces should continue past the entry point of a program.", "\
1566Show whether backtraces should continue past the entry point of a program.", "\
1567Normally there are no callers beyond the entry point of a program, so GDB\n\
1568will terminate the backtrace there. Set this variable if you need to see \n\
335cca0d
AC
1569the rest of the stack trace.",
1570 NULL, /* PRINT: Whether backtraces should continue past the entry point is %s. */
2315ffec
RC
1571 NULL, NULL, &set_backtrace_cmdlist,
1572 &show_backtrace_cmdlist);
1573
25d29d70
AC
1574 add_setshow_uinteger_cmd ("limit", class_obscure,
1575 &backtrace_limit, "\
3b64bf98
AC
1576Set an upper bound on the number of backtrace levels.", "\
1577Show the upper bound on the number of backtrace levels.", "\
fec74868 1578No more than the specified number of frames can be displayed or examined.\n\
335cca0d
AC
1579Zero is unlimited.",
1580 NULL, /* PRINT: An upper bound on the number of backtrace levels is %s. */
25d29d70
AC
1581 NULL, NULL, &set_backtrace_cmdlist,
1582 &show_backtrace_cmdlist);
ac2bd0a9
AC
1583
1584 /* Debug this files internals. */
cb1a6d5f
AC
1585 deprecated_add_show_from_set
1586 (add_set_cmd ("frame", class_maintenance, var_zinteger,
1587 &frame_debug, "Set frame debugging.\n\
ac2bd0a9 1588When non-zero, frame specific internal debugging is enabled.", &setdebuglist),
cb1a6d5f 1589 &showdebuglist);
4c1e7e9d 1590}
This page took 0.446614 seconds and 4 git commands to generate.