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